@gjsify/buffer 0.4.21 → 0.4.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import"./_virtual/_rolldown/runtime.js";const e=`ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`,t=new Uint8Array(256);for(let n=0;n<64;n++)t[e.charCodeAt(n)]=n;function atobPolyfill(e){if(typeof globalThis.atob==`function`)return globalThis.atob(e);let n=e.replace(/[=\s]/g,``),r=``,i=0,a=0;for(let e=0;e<n.length;e++)i=i<<6|t[n.charCodeAt(e)],a+=6,a>=8&&(a-=8,r+=String.fromCharCode(i>>a&255));return r}function btoaPolyfill(t){if(typeof globalThis.btoa==`function`)return globalThis.btoa(t);let n=``,r=0;for(;r+2<t.length;r+=3){let i=t.charCodeAt(r)<<16|t.charCodeAt(r+1)<<8|t.charCodeAt(r+2);n+=e[i>>18&63]+e[i>>12&63]+e[i>>6&63]+e[i&63]}if(r+1===t.length){let i=t.charCodeAt(r)<<16;n+=e[i>>18&63]+e[i>>12&63]+`==`}else if(r+2===t.length){let i=t.charCodeAt(r)<<16|t.charCodeAt(r+1)<<8;n+=e[i>>18&63]+e[i>>12&63]+e[i>>6&63]+`=`}return n}function base64Decode(e){let n=e.replace(/[=\s]/g,``),r=new Uint8Array(n.length*3>>2),i=0,a=0,o=0;for(let e=0;e<n.length;e++)i=i<<6|t[n.charCodeAt(e)],a+=6,a>=8&&(a-=8,r[o++]=i>>a&255);return r.subarray(0,o)}function base64Encode(t){let n=``,r=t.length;for(let i=0;i<r;i+=3){let a=t[i],o=i+1<r?t[i+1]:0,s=i+2<r?t[i+2]:0;n+=e[a>>2],n+=e[(a&3)<<4|o>>4],n+=i+1<r?e[(o&15)<<2|s>>6]:`=`,n+=i+2<r?e[s&63]:`=`}return n}export{atobPolyfill,base64Decode,base64Encode,btoaPolyfill};
@@ -0,0 +1 @@
1
+ import"./_virtual/_rolldown/runtime.js";import{base64Decode as e,btoaPolyfill as t}from"./base64.js";const n=new TextEncoder,r=new TextDecoder;function encodeString(t,r){switch(r){case`utf8`:return n.encode(t);case`ascii`:{let e=new Uint8Array(t.length);for(let n=0;n<t.length;n++)e[n]=t.charCodeAt(n)&127;return e}case`latin1`:{let e=new Uint8Array(t.length);for(let n=0;n<t.length;n++)e[n]=t.charCodeAt(n)&255;return e}case`base64`:return e(t.replace(/-/g,`+`).replace(/_/g,`/`));case`base64url`:return encodeString(t.replace(/-/g,`+`).replace(/_/g,`/`),`base64`);case`hex`:{let e=t.length>>>1,n=new Uint8Array(e);for(let r=0;r<e;r++){let e=parseInt(t[r*2],16),i=parseInt(t[r*2+1],16);if(Number.isNaN(e)||Number.isNaN(i))break;n[r]=e<<4|i}return n}case`utf16le`:{let e=new Uint8Array(t.length*2);for(let n=0;n<t.length;n++){let r=t.charCodeAt(n);e[n*2]=r&255,e[n*2+1]=r>>8&255}return e}default:return n.encode(t)}}function decodeString(e,n,i,a){let o=i!==void 0||a!==void 0?e.subarray(i??0,a??e.length):e;switch(n){case`utf8`:return r.decode(o);case`ascii`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]&127);return e}case`latin1`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]);return e}case`base64`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]);return t(e)}case`base64url`:return decodeString(o,`base64`).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``);case`hex`:{let e=``;for(let t=0;t<o.length;t++)e+=o[t].toString(16).padStart(2,`0`);return e}case`utf16le`:{let e=``;for(let t=0;t+1<o.length;t+=2)e+=String.fromCharCode(o[t]|o[t+1]<<8);return e}default:return r.decode(o)}}function checkOffset(e,t,n){if(e+t>n)throw RangeError(`Attempt to access memory outside buffer bounds`)}export{checkOffset,decodeString,encodeString};
package/lib/esm/buffer.js CHANGED
@@ -1 +1 @@
1
- import"./_virtual/_rolldown/runtime.js";import{base64Decode as e,btoaPolyfill as t,checkEncoding as n,normalizeEncoding as r}from"@gjsify/utils";const i=new TextEncoder,a=new TextDecoder,o=typeof SharedArrayBuffer<`u`;function encodeString(t,n){switch(n){case`utf8`:return i.encode(t);case`ascii`:{let e=new Uint8Array(t.length);for(let n=0;n<t.length;n++)e[n]=t.charCodeAt(n)&127;return e}case`latin1`:{let e=new Uint8Array(t.length);for(let n=0;n<t.length;n++)e[n]=t.charCodeAt(n)&255;return e}case`base64`:return e(t.replace(/-/g,`+`).replace(/_/g,`/`));case`base64url`:return encodeString(t.replace(/-/g,`+`).replace(/_/g,`/`),`base64`);case`hex`:{let e=t.length>>>1,n=new Uint8Array(e);for(let r=0;r<e;r++){let e=parseInt(t[r*2],16),i=parseInt(t[r*2+1],16);if(Number.isNaN(e)||Number.isNaN(i))break;n[r]=e<<4|i}return n}case`utf16le`:{let e=new Uint8Array(t.length*2);for(let n=0;n<t.length;n++){let r=t.charCodeAt(n);e[n*2]=r&255,e[n*2+1]=r>>8&255}return e}default:return i.encode(t)}}function decodeString(e,n,r,i){let o=r!==void 0||i!==void 0?e.subarray(r??0,i??e.length):e;switch(n){case`utf8`:return a.decode(o);case`ascii`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]&127);return e}case`latin1`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]);return e}case`base64`:{let e=``;for(let t=0;t<o.length;t++)e+=String.fromCharCode(o[t]);return t(e)}case`base64url`:return decodeString(o,`base64`).replace(/\+/g,`-`).replace(/\//g,`_`).replace(/=+$/,``);case`hex`:{let e=``;for(let t=0;t<o.length;t++)e+=o[t].toString(16).padStart(2,`0`);return e}case`utf16le`:{let e=``;for(let t=0;t+1<o.length;t+=2)e+=String.fromCharCode(o[t]|o[t+1]<<8);return e}default:return a.decode(o)}}function checkOffset(e,t,n){if(e+t>n)throw RangeError(`Attempt to access memory outside buffer bounds`)}var s=class Buffer extends Uint8Array{static _isBuffer=!0;static alloc(e,t,n){if(typeof e!=`number`)throw TypeError(`The "size" argument must be of type number. Received type ${typeof e}`);if(e<0)throw RangeError(`The value "${e}" is invalid for option "size"`);let r=new Buffer(e);return t!==void 0&&t!==0&&r.fill(t,0,e,n),r}static allocUnsafe(e){if(typeof e!=`number`)throw TypeError(`The "size" argument must be of type number. Received type ${typeof e}`);return new Buffer(e)}static allocUnsafeSlow(e){return Buffer.allocUnsafe(e)}static from(e,t,i){if(typeof e==`string`){let i=r(t);if(t&&typeof t==`string`){let e=(``+t).toLowerCase().replace(/-/g,``);[`utf8`,`ascii`,`latin1`,`binary`,`base64`,`base64url`,`hex`,`ucs2`,`utf16le`,``].includes(e)||n(t)}let a=encodeString(e,i);return new Buffer(a.buffer,a.byteOffset,a.byteLength)}if(ArrayBuffer.isView(e)){let t=new Buffer(e.buffer,e.byteOffset,e.byteLength),n=new Buffer(t.length);return n.set(t),n}if(e instanceof ArrayBuffer){let n=t||0;return new Buffer(e,n,i===void 0?e.byteLength-n:i)}if(o&&e instanceof SharedArrayBuffer){let n=t||0,r=i===void 0?e.byteLength-n:i;return new Buffer(new Uint8Array(e,n,r))}if(typeof e==`object`&&e&&typeof e.viewBytes==`function`&&typeof e.byteLength==`number`&&e.constructor?.name===`SharedBuffer`){let n=e,r=t||0,a=i===void 0?n.byteLength-r:i,o=n.viewBytes(r,a);return new Buffer(o.buffer,o.byteOffset,o.byteLength)}if(Array.isArray(e)){let t=new Buffer(e.length);for(let n=0;n<e.length;n++)t[n]=e[n]&255;return t}throw TypeError(`The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array`)}static isBuffer(e){return e instanceof Buffer}static isEncoding(e){if(typeof e!=`string`)return!1;let t=e.toLowerCase().replace(/-/g,``);return[`utf8`,`ascii`,`latin1`,`binary`,`base64`,`base64url`,`hex`,`ucs2`,`utf16le`].includes(t)}static byteLength(e,t){if(typeof e!=`string`){if(ArrayBuffer.isView(e)||e instanceof ArrayBuffer||o&&e instanceof SharedArrayBuffer)return e.byteLength;throw TypeError(`The "string" argument must be one of type string, Buffer, or ArrayBuffer`)}switch(r(t)){case`utf8`:return i.encode(e).byteLength;case`ascii`:case`latin1`:return e.length;case`base64`:case`base64url`:{let t=e.length;for(;t>0&&(e[t-1]===`=`||e[t-1]===` `);)t--;return t*3>>>2}case`hex`:return e.length>>>1;case`utf16le`:return e.length*2;default:return i.encode(e).byteLength}}static compare(e,t){if(!(e instanceof Uint8Array)||!(t instanceof Uint8Array))throw TypeError(`Arguments must be Buffers or Uint8Arrays`);let n=Math.min(e.length,t.length);for(let r=0;r<n;r++){if(e[r]<t[r])return-1;if(e[r]>t[r])return 1}return e.length<t.length?-1:+(e.length>t.length)}static concat(e,t){if(e.length===0)return Buffer.alloc(0);if(t===void 0){t=0;for(let n=0;n<e.length;n++)t+=e[n].length}let n=Buffer.alloc(t),r=0;for(let i=0;i<e.length;i++){let a=e[i],o=Math.min(a.length,t-r);o<=0||(n.set(a.subarray(0,o),r),r+=o)}return n}static poolSize=8192;toString(e,t,n){let i=r(e);return decodeString(this,i,t,n)}toJSON(){return{type:`Buffer`,data:Array.from(this)}}equals(e){if(!(e instanceof Uint8Array))throw TypeError(`Argument must be a Buffer or Uint8Array`);if(this.length!==e.length)return!1;for(let t=0;t<this.length;t++)if(this[t]!==e[t])return!1;return!0}compare(e,t,n,r,i){if(!(e instanceof Uint8Array))throw TypeError(`Argument must be a Buffer or Uint8Array`);let a=r!==void 0||i!==void 0?this.subarray(r??0,i??this.length):this,o=t!==void 0||n!==void 0?e.subarray(t??0,n??e.length):e;return Buffer.compare(a,o)}copy(e,t=0,n=0,r){let i=r??this.length,a=Math.min(i-n,e.length-t);return a<=0?0:(e.set(this.subarray(n,n+a),t),a)}slice(e,t){let n=e??0,r=t??this.length,i=super.subarray(n,r);return new Buffer(i.buffer,i.byteOffset,i.byteLength)}subarray(e,t){let n=super.subarray(e,t);return new Buffer(n.buffer,n.byteOffset,n.byteLength)}write(e,t,n,i){t??=0;let a=encodeString(e,r(i||(typeof n==`string`?n:void 0))),o=n!==void 0&&typeof n==`number`?Math.min(n,this.length-t):this.length-t,s=Math.min(a.length,o);return this.set(a.subarray(0,s),t),s}fill(e,t,n,i){let a=t??0,o=n??this.length;if(typeof e==`number`)super.fill(e&255,a,o);else if(typeof e==`string`){let t=encodeString(e,r(i));if(t.length===0)super.fill(0,a,o);else if(t.length===1)super.fill(t[0],a,o);else for(let e=a;e<o;e++)this[e]=t[(e-a)%t.length]}else if(e instanceof Uint8Array)if(e.length===0)super.fill(0,a,o);else for(let t=a;t<o;t++)this[t]=e[(t-a)%e.length];return this}indexOf(e,t,n){if(typeof e==`number`)return super.indexOf(e&255,t);let i=typeof e==`string`?encodeString(e,r(n)):e,a=t??0;outer:for(let e=a;e<=this.length-i.length;e++){for(let t=0;t<i.length;t++)if(this[e+t]!==i[t])continue outer;return e}return-1}lastIndexOf(e,t,n){if(typeof e==`number`)return t===void 0?super.lastIndexOf(e&255):super.lastIndexOf(e&255,t);let i=typeof e==`string`?encodeString(e,r(n)):e,a=t===void 0?this.length-i.length:Math.min(t,this.length-i.length);outer:for(let e=a;e>=0;e--){for(let t=0;t<i.length;t++)if(this[e+t]!==i[t])continue outer;return e}return-1}includes(e,t,n){return this.indexOf(e,t,n)!==-1}readUInt8(e=0){return checkOffset(e,1,this.length),this[e]}readUInt16BE(e=0){return checkOffset(e,2,this.length),this[e]<<8|this[e+1]}readUInt16LE(e=0){return checkOffset(e,2,this.length),this[e]|this[e+1]<<8}readUInt32BE(e=0){return checkOffset(e,4,this.length),this[e]*16777216+(this[e+1]<<16|this[e+2]<<8|this[e+3])}readUInt32LE(e=0){return checkOffset(e,4,this.length),this[e+3]*16777216+(this[e+2]<<16|this[e+1]<<8|this[e])>>>0}readInt8(e=0){return checkOffset(e,1,this.length),this[e]|(this[e]&128?4294967040:0)}readInt16BE(e=0){checkOffset(e,2,this.length);let t=this[e]<<8|this[e+1];return t&32768?t|4294901760:t}readInt16LE(e=0){checkOffset(e,2,this.length);let t=this[e]|this[e+1]<<8;return t&32768?t|4294901760:t}readInt32BE(e=0){return checkOffset(e,4,this.length),this[e]<<24|this[e+1]<<16|this[e+2]<<8|this[e+3]}readInt32LE(e=0){return checkOffset(e,4,this.length),this[e]|this[e+1]<<8|this[e+2]<<16|this[e+3]<<24}readFloatBE(e=0){return checkOffset(e,4,this.length),new DataView(this.buffer,this.byteOffset+e,4).getFloat32(0,!1)}readFloatLE(e=0){return checkOffset(e,4,this.length),new DataView(this.buffer,this.byteOffset+e,4).getFloat32(0,!0)}readDoubleBE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getFloat64(0,!1)}readDoubleLE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getFloat64(0,!0)}readBigInt64BE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getBigInt64(0,!1)}readBigInt64LE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getBigInt64(0,!0)}readBigUInt64BE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getBigUint64(0,!1)}readBigUInt64LE(e=0){return checkOffset(e,8,this.length),new DataView(this.buffer,this.byteOffset+e,8).getBigUint64(0,!0)}readUIntBE(e,t){checkOffset(e,t,this.length);let n=0;for(let r=0;r<t;r++)n=n*256+this[e+r];return n}readUIntLE(e,t){checkOffset(e,t,this.length);let n=0,r=1;for(let i=0;i<t;i++)n+=this[e+i]*r,r*=256;return n}readIntBE(e,t){checkOffset(e,t,this.length);let n=0;for(let r=0;r<t;r++)n=n*256+this[e+r];return n>=2**(8*t-1)&&(n-=2**(8*t)),n}readIntLE(e,t){checkOffset(e,t,this.length);let n=0,r=1;for(let i=0;i<t;i++)n+=this[e+i]*r,r*=256;return n>=2**(8*t-1)&&(n-=2**(8*t)),n}writeUInt8(e,t=0){return checkOffset(t,1,this.length),this[t]=e&255,t+1}writeUInt16BE(e,t=0){return checkOffset(t,2,this.length),this[t]=e>>>8&255,this[t+1]=e&255,t+2}writeUInt16LE(e,t=0){return checkOffset(t,2,this.length),this[t]=e&255,this[t+1]=e>>>8&255,t+2}writeUInt32BE(e,t=0){return checkOffset(t,4,this.length),this[t]=e>>>24&255,this[t+1]=e>>>16&255,this[t+2]=e>>>8&255,this[t+3]=e&255,t+4}writeUInt32LE(e,t=0){return checkOffset(t,4,this.length),this[t]=e&255,this[t+1]=e>>>8&255,this[t+2]=e>>>16&255,this[t+3]=e>>>24&255,t+4}writeInt8(e,t=0){return checkOffset(t,1,this.length),e<0&&(e=255+e+1),this[t]=e&255,t+1}writeInt16BE(e,t=0){return checkOffset(t,2,this.length),this[t]=e>>>8&255,this[t+1]=e&255,t+2}writeInt16LE(e,t=0){return checkOffset(t,2,this.length),this[t]=e&255,this[t+1]=e>>>8&255,t+2}writeInt32BE(e,t=0){return checkOffset(t,4,this.length),this[t]=e>>>24&255,this[t+1]=e>>>16&255,this[t+2]=e>>>8&255,this[t+3]=e&255,t+4}writeInt32LE(e,t=0){return checkOffset(t,4,this.length),this[t]=e&255,this[t+1]=e>>>8&255,this[t+2]=e>>>16&255,this[t+3]=e>>>24&255,t+4}writeFloatBE(e,t=0){return checkOffset(t,4,this.length),new DataView(this.buffer,this.byteOffset+t,4).setFloat32(0,e,!1),t+4}writeFloatLE(e,t=0){return checkOffset(t,4,this.length),new DataView(this.buffer,this.byteOffset+t,4).setFloat32(0,e,!0),t+4}writeDoubleBE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setFloat64(0,e,!1),t+8}writeDoubleLE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setFloat64(0,e,!0),t+8}writeBigInt64BE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setBigInt64(0,e,!1),t+8}writeBigInt64LE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setBigInt64(0,e,!0),t+8}writeBigUInt64BE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setBigUint64(0,e,!1),t+8}writeBigUInt64LE(e,t=0){return checkOffset(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).setBigUint64(0,e,!0),t+8}swap16(){let e=this.length;if(e%2!=0)throw RangeError(`Buffer size must be a multiple of 16-bits`);for(let t=0;t<e;t+=2){let e=this[t];this[t]=this[t+1],this[t+1]=e}return this}swap32(){let e=this.length;if(e%4!=0)throw RangeError(`Buffer size must be a multiple of 32-bits`);for(let t=0;t<e;t+=4){let e=this[t],n=this[t+1];this[t]=this[t+3],this[t+1]=this[t+2],this[t+2]=n,this[t+3]=e}return this}swap64(){let e=this.length;if(e%8!=0)throw RangeError(`Buffer size must be a multiple of 64-bits`);for(let t=0;t<e;t+=8){let e=this[t],n=this[t+1],r=this[t+2],i=this[t+3];this[t]=this[t+7],this[t+1]=this[t+6],this[t+2]=this[t+5],this[t+3]=this[t+4],this[t+4]=i,this[t+5]=r,this[t+6]=n,this[t+7]=e}return this}};const c=2**31-1,l=2**28-16,u={MAX_LENGTH:c,MAX_STRING_LENGTH:l};var SlowBuffer=class extends s{constructor(e){super(e)}};export{s as Buffer,SlowBuffer,u as constants,c as kMaxLength,l as kStringMaxLength};
1
+ import"./_virtual/_rolldown/runtime.js";import{checkOffset as e,decodeString as t,encodeString as n}from"./buffer-codec.js";import{checkEncoding as r,normalizeEncoding as i}from"./encoding.js";const a=typeof SharedArrayBuffer<`u`;var o=class Buffer extends Uint8Array{static _isBuffer=!0;static alloc(e,t,n){if(typeof e!=`number`)throw TypeError(`The "size" argument must be of type number. Received type ${typeof e}`);if(e<0)throw RangeError(`The value "${e}" is invalid for option "size"`);let r=new Buffer(e);return t!==void 0&&t!==0&&r.fill(t,0,e,n),r}static allocUnsafe(e){if(typeof e!=`number`)throw TypeError(`The "size" argument must be of type number. Received type ${typeof e}`);return new Buffer(e)}static allocUnsafeSlow(e){return Buffer.allocUnsafe(e)}static from(e,t,o){if(typeof e==`string`){let a=i(t);if(t&&typeof t==`string`){let e=(``+t).toLowerCase().replace(/-/g,``);[`utf8`,`ascii`,`latin1`,`binary`,`base64`,`base64url`,`hex`,`ucs2`,`utf16le`,``].includes(e)||r(t)}let o=n(e,a);return new Buffer(o.buffer,o.byteOffset,o.byteLength)}if(ArrayBuffer.isView(e)){let t=new Buffer(e.buffer,e.byteOffset,e.byteLength),n=new Buffer(t.length);return n.set(t),n}if(e instanceof ArrayBuffer){let n=t||0;return new Buffer(e,n,o===void 0?e.byteLength-n:o)}if(a&&e instanceof SharedArrayBuffer){let n=t||0,r=o===void 0?e.byteLength-n:o;return new Buffer(new Uint8Array(e,n,r))}if(typeof e==`object`&&e&&typeof e.viewBytes==`function`&&typeof e.byteLength==`number`&&e.constructor?.name===`SharedBuffer`){let n=e,r=t||0,i=o===void 0?n.byteLength-r:o,a=n.viewBytes(r,i);return new Buffer(a.buffer,a.byteOffset,a.byteLength)}if(Array.isArray(e)){let t=new Buffer(e.length);for(let n=0;n<e.length;n++)t[n]=e[n]&255;return t}throw TypeError(`The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array`)}static isBuffer(e){return e instanceof Buffer}static isEncoding(e){if(typeof e!=`string`)return!1;let t=e.toLowerCase().replace(/-/g,``);return[`utf8`,`ascii`,`latin1`,`binary`,`base64`,`base64url`,`hex`,`ucs2`,`utf16le`].includes(t)}static byteLength(e,t){if(typeof e!=`string`){if(ArrayBuffer.isView(e)||e instanceof ArrayBuffer||a&&e instanceof SharedArrayBuffer)return e.byteLength;throw TypeError(`The "string" argument must be one of type string, Buffer, or ArrayBuffer`)}switch(i(t)){case`utf8`:return new TextEncoder().encode(e).byteLength;case`ascii`:case`latin1`:return e.length;case`base64`:case`base64url`:{let t=e.length;for(;t>0&&(e[t-1]===`=`||e[t-1]===` `);)t--;return t*3>>>2}case`hex`:return e.length>>>1;case`utf16le`:return e.length*2;default:return new TextEncoder().encode(e).byteLength}}static compare(e,t){if(!(e instanceof Uint8Array)||!(t instanceof Uint8Array))throw TypeError(`Arguments must be Buffers or Uint8Arrays`);let n=Math.min(e.length,t.length);for(let r=0;r<n;r++){if(e[r]<t[r])return-1;if(e[r]>t[r])return 1}return e.length<t.length?-1:+(e.length>t.length)}static concat(e,t){if(e.length===0)return Buffer.alloc(0);if(t===void 0){t=0;for(let n=0;n<e.length;n++)t+=e[n].length}let n=Buffer.alloc(t),r=0;for(let i=0;i<e.length;i++){let a=e[i],o=Math.min(a.length,t-r);o<=0||(n.set(a.subarray(0,o),r),r+=o)}return n}static poolSize=8192;toString(e,n,r){let a=i(e);return t(this,a,n,r)}toJSON(){return{type:`Buffer`,data:Array.from(this)}}equals(e){if(!(e instanceof Uint8Array))throw TypeError(`Argument must be a Buffer or Uint8Array`);if(this.length!==e.length)return!1;for(let t=0;t<this.length;t++)if(this[t]!==e[t])return!1;return!0}compare(e,t,n,r,i){if(!(e instanceof Uint8Array))throw TypeError(`Argument must be a Buffer or Uint8Array`);let a=r!==void 0||i!==void 0?this.subarray(r??0,i??this.length):this,o=t!==void 0||n!==void 0?e.subarray(t??0,n??e.length):e;return Buffer.compare(a,o)}copy(e,t=0,n=0,r){let i=r??this.length,a=Math.min(i-n,e.length-t);return a<=0?0:(e.set(this.subarray(n,n+a),t),a)}slice(e,t){let n=e??0,r=t??this.length,i=super.subarray(n,r);return new Buffer(i.buffer,i.byteOffset,i.byteLength)}subarray(e,t){let n=super.subarray(e,t);return new Buffer(n.buffer,n.byteOffset,n.byteLength)}write(e,t,r,a){t??=0;let o=n(e,i(a||(typeof r==`string`?r:void 0))),s=r!==void 0&&typeof r==`number`?Math.min(r,this.length-t):this.length-t,c=Math.min(o.length,s);return this.set(o.subarray(0,c),t),c}fill(e,t,r,a){let o=t??0,s=r??this.length;if(typeof e==`number`)super.fill(e&255,o,s);else if(typeof e==`string`){let t=n(e,i(a));if(t.length===0)super.fill(0,o,s);else if(t.length===1)super.fill(t[0],o,s);else for(let e=o;e<s;e++)this[e]=t[(e-o)%t.length]}else if(e instanceof Uint8Array)if(e.length===0)super.fill(0,o,s);else for(let t=o;t<s;t++)this[t]=e[(t-o)%e.length];return this}indexOf(e,t,r){if(typeof e==`number`)return super.indexOf(e&255,t);let a=typeof e==`string`?n(e,i(r)):e,o=t??0;outer:for(let e=o;e<=this.length-a.length;e++){for(let t=0;t<a.length;t++)if(this[e+t]!==a[t])continue outer;return e}return-1}lastIndexOf(e,t,r){if(typeof e==`number`)return t===void 0?super.lastIndexOf(e&255):super.lastIndexOf(e&255,t);let a=typeof e==`string`?n(e,i(r)):e,o=t===void 0?this.length-a.length:Math.min(t,this.length-a.length);outer:for(let e=o;e>=0;e--){for(let t=0;t<a.length;t++)if(this[e+t]!==a[t])continue outer;return e}return-1}includes(e,t,n){return this.indexOf(e,t,n)!==-1}readUInt8(t=0){return e(t,1,this.length),this[t]}readUInt16BE(t=0){return e(t,2,this.length),this[t]<<8|this[t+1]}readUInt16LE(t=0){return e(t,2,this.length),this[t]|this[t+1]<<8}readUInt32BE(t=0){return e(t,4,this.length),this[t]*16777216+(this[t+1]<<16|this[t+2]<<8|this[t+3])}readUInt32LE(t=0){return e(t,4,this.length),this[t+3]*16777216+(this[t+2]<<16|this[t+1]<<8|this[t])>>>0}readInt8(t=0){return e(t,1,this.length),this[t]|(this[t]&128?4294967040:0)}readInt16BE(t=0){e(t,2,this.length);let n=this[t]<<8|this[t+1];return n&32768?n|4294901760:n}readInt16LE(t=0){e(t,2,this.length);let n=this[t]|this[t+1]<<8;return n&32768?n|4294901760:n}readInt32BE(t=0){return e(t,4,this.length),this[t]<<24|this[t+1]<<16|this[t+2]<<8|this[t+3]}readInt32LE(t=0){return e(t,4,this.length),this[t]|this[t+1]<<8|this[t+2]<<16|this[t+3]<<24}readFloatBE(t=0){return e(t,4,this.length),new DataView(this.buffer,this.byteOffset+t,4).getFloat32(0,!1)}readFloatLE(t=0){return e(t,4,this.length),new DataView(this.buffer,this.byteOffset+t,4).getFloat32(0,!0)}readDoubleBE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getFloat64(0,!1)}readDoubleLE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getFloat64(0,!0)}readBigInt64BE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getBigInt64(0,!1)}readBigInt64LE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getBigInt64(0,!0)}readBigUInt64BE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getBigUint64(0,!1)}readBigUInt64LE(t=0){return e(t,8,this.length),new DataView(this.buffer,this.byteOffset+t,8).getBigUint64(0,!0)}readUIntBE(t,n){e(t,n,this.length);let r=0;for(let e=0;e<n;e++)r=r*256+this[t+e];return r}readUIntLE(t,n){e(t,n,this.length);let r=0,i=1;for(let e=0;e<n;e++)r+=this[t+e]*i,i*=256;return r}readIntBE(t,n){e(t,n,this.length);let r=0;for(let e=0;e<n;e++)r=r*256+this[t+e];return r>=2**(8*n-1)&&(r-=2**(8*n)),r}readIntLE(t,n){e(t,n,this.length);let r=0,i=1;for(let e=0;e<n;e++)r+=this[t+e]*i,i*=256;return r>=2**(8*n-1)&&(r-=2**(8*n)),r}writeUInt8(t,n=0){return e(n,1,this.length),this[n]=t&255,n+1}writeUInt16BE(t,n=0){return e(n,2,this.length),this[n]=t>>>8&255,this[n+1]=t&255,n+2}writeUInt16LE(t,n=0){return e(n,2,this.length),this[n]=t&255,this[n+1]=t>>>8&255,n+2}writeUInt32BE(t,n=0){return e(n,4,this.length),this[n]=t>>>24&255,this[n+1]=t>>>16&255,this[n+2]=t>>>8&255,this[n+3]=t&255,n+4}writeUInt32LE(t,n=0){return e(n,4,this.length),this[n]=t&255,this[n+1]=t>>>8&255,this[n+2]=t>>>16&255,this[n+3]=t>>>24&255,n+4}writeInt8(t,n=0){return e(n,1,this.length),t<0&&(t=255+t+1),this[n]=t&255,n+1}writeInt16BE(t,n=0){return e(n,2,this.length),this[n]=t>>>8&255,this[n+1]=t&255,n+2}writeInt16LE(t,n=0){return e(n,2,this.length),this[n]=t&255,this[n+1]=t>>>8&255,n+2}writeInt32BE(t,n=0){return e(n,4,this.length),this[n]=t>>>24&255,this[n+1]=t>>>16&255,this[n+2]=t>>>8&255,this[n+3]=t&255,n+4}writeInt32LE(t,n=0){return e(n,4,this.length),this[n]=t&255,this[n+1]=t>>>8&255,this[n+2]=t>>>16&255,this[n+3]=t>>>24&255,n+4}writeFloatBE(t,n=0){return e(n,4,this.length),new DataView(this.buffer,this.byteOffset+n,4).setFloat32(0,t,!1),n+4}writeFloatLE(t,n=0){return e(n,4,this.length),new DataView(this.buffer,this.byteOffset+n,4).setFloat32(0,t,!0),n+4}writeDoubleBE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setFloat64(0,t,!1),n+8}writeDoubleLE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setFloat64(0,t,!0),n+8}writeBigInt64BE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setBigInt64(0,t,!1),n+8}writeBigInt64LE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setBigInt64(0,t,!0),n+8}writeBigUInt64BE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setBigUint64(0,t,!1),n+8}writeBigUInt64LE(t,n=0){return e(n,8,this.length),new DataView(this.buffer,this.byteOffset+n,8).setBigUint64(0,t,!0),n+8}swap16(){let e=this.length;if(e%2!=0)throw RangeError(`Buffer size must be a multiple of 16-bits`);for(let t=0;t<e;t+=2){let e=this[t];this[t]=this[t+1],this[t+1]=e}return this}swap32(){let e=this.length;if(e%4!=0)throw RangeError(`Buffer size must be a multiple of 32-bits`);for(let t=0;t<e;t+=4){let e=this[t],n=this[t+1];this[t]=this[t+3],this[t+1]=this[t+2],this[t+2]=n,this[t+3]=e}return this}swap64(){let e=this.length;if(e%8!=0)throw RangeError(`Buffer size must be a multiple of 64-bits`);for(let t=0;t<e;t+=8){let e=this[t],n=this[t+1],r=this[t+2],i=this[t+3];this[t]=this[t+7],this[t+1]=this[t+6],this[t+2]=this[t+5],this[t+3]=this[t+4],this[t+4]=i,this[t+5]=r,this[t+6]=n,this[t+7]=e}return this}};const s=2**31-1,c=2**28-16,l={MAX_LENGTH:s,MAX_STRING_LENGTH:c};var SlowBuffer=class extends o{constructor(e){super(e)}};export{o as Buffer,SlowBuffer,l as constants,s as kMaxLength,c as kStringMaxLength};
@@ -0,0 +1 @@
1
+ import"./_virtual/_rolldown/runtime.js";const e=[`utf8`,`ascii`,`latin1`,`binary`,`base64`,`base64url`,`hex`,`ucs2`,`utf16le`];function normalizeEncoding(e){if(!e||e===`utf8`||e===`utf-8`)return`utf8`;switch((``+e).toLowerCase().replace(/-/g,``)){case`utf8`:return`utf8`;case`ascii`:return`ascii`;case`latin1`:case`binary`:return`latin1`;case`base64`:return`base64`;case`base64url`:return`base64url`;case`hex`:return`hex`;case`ucs2`:case`utf16le`:return`utf16le`;default:return`utf8`}}function checkEncoding(t){let n=(``+t).toLowerCase().replace(/-/g,``);if(!e.includes(n))throw TypeError(`Unknown encoding: ${t}`)}export{checkEncoding,normalizeEncoding};
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{Blob as e,File as t}from"./blob.js";import{Buffer as n,SlowBuffer as r,constants as i,kMaxLength as a,kStringMaxLength as o}from"./buffer.js";const s=globalThis.atob,c=globalThis.btoa,l=50;var u={Buffer:n,SlowBuffer:r,Blob:e,File:t,atob:s,btoa:c,kMaxLength:a,kStringMaxLength:o,constants:i,INSPECT_MAX_BYTES:50};export{e as Blob,n as Buffer,t as File,l as INSPECT_MAX_BYTES,r as SlowBuffer,s as atob,c as btoa,i as constants,u as default,a as kMaxLength,o as kStringMaxLength};
1
+ import{atobPolyfill as e,base64Decode as t,base64Encode as n,btoaPolyfill as r}from"./base64.js";import{Blob as i,File as a}from"./blob.js";import{checkEncoding as o,normalizeEncoding as s}from"./encoding.js";import{Buffer as c,SlowBuffer as l,constants as u,kMaxLength as d,kStringMaxLength as f}from"./buffer.js";const p=globalThis.atob,m=globalThis.btoa,h=50;var g={Buffer:c,SlowBuffer:l,Blob:i,File:a,atob:p,btoa:m,kMaxLength:d,kStringMaxLength:f,constants:u,INSPECT_MAX_BYTES:50};export{i as Blob,c as Buffer,a as File,h as INSPECT_MAX_BYTES,l as SlowBuffer,p as atob,e as atobPolyfill,t as base64Decode,n as base64Encode,m as btoa,r as btoaPolyfill,o as checkEncoding,u as constants,g as default,d as kMaxLength,f as kStringMaxLength,s as normalizeEncoding};
@@ -0,0 +1,8 @@
1
+ /** Decode a base64 string to a binary string. Uses native atob when available. */
2
+ export declare function atobPolyfill(str: string): string;
3
+ /** Encode a binary string to base64. Uses native btoa when available. */
4
+ export declare function btoaPolyfill(str: string): string;
5
+ /** Decode a base64 string directly to Uint8Array (avoids lossy atob string round-trip). */
6
+ export declare function base64Decode(str: string): Uint8Array;
7
+ /** Encode a Uint8Array to base64 string. */
8
+ export declare function base64Encode(bytes: Uint8Array): string;
@@ -0,0 +1,3 @@
1
+ export declare function encodeString(str: string, encoding: BufferEncoding): Uint8Array;
2
+ export declare function decodeString(buf: Uint8Array, encoding: BufferEncoding, start?: number, end?: number): string;
3
+ export declare function checkOffset(offset: number, ext: number, length: number): void;
@@ -0,0 +1,11 @@
1
+ /** Canonical encoding names (matches BufferEncoding from @types/node). */
2
+ export type Encoding = 'utf8' | 'ascii' | 'latin1' | 'base64' | 'base64url' | 'hex' | 'utf16le' | 'binary' | 'ucs2' | 'ucs-2' | 'utf-8';
3
+ /**
4
+ * Normalize an encoding string to a canonical encoding value.
5
+ * Returns 'utf8' as default for undefined/null/empty input.
6
+ */
7
+ export declare function normalizeEncoding(enc?: string): Encoding;
8
+ /**
9
+ * Check that an encoding string is valid. Throws TypeError if not.
10
+ */
11
+ export declare function checkEncoding(encoding: string): void;
@@ -1,5 +1,7 @@
1
1
  import { Buffer, SlowBuffer, kMaxLength, kStringMaxLength, constants } from './buffer.js';
2
2
  export { Buffer, SlowBuffer, kMaxLength, kStringMaxLength, constants, };
3
+ export { normalizeEncoding, checkEncoding, type Encoding } from './encoding.js';
4
+ export { base64Encode, base64Decode, atobPolyfill, btoaPolyfill } from './base64.js';
3
5
  import { Blob as BlobImpl, File as FileImpl } from './blob.js';
4
6
  export { BlobImpl as Blob, FileImpl as File };
5
7
  export declare const atob: typeof globalThis.atob;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/buffer",
3
- "version": "0.4.21",
3
+ "version": "0.4.22",
4
4
  "description": "Node.js buffer module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -40,12 +40,12 @@
40
40
  "buffer"
41
41
  ],
42
42
  "devDependencies": {
43
- "@gjsify/cli": "^0.4.21",
44
- "@gjsify/unit": "^0.4.21",
45
- "@types/node": "^25.6.2",
43
+ "@gjsify/cli": "^0.4.22",
44
+ "@gjsify/unit": "^0.4.22",
45
+ "@types/node": "^25.9.1",
46
46
  "typescript": "^6.0.3"
47
47
  },
48
48
  "dependencies": {
49
- "@gjsify/utils": "^0.4.21"
49
+ "@gjsify/utils": "^0.4.22"
50
50
  }
51
51
  }