@gjsify/buffer 0.3.21 → 0.4.3

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
+ var e=Object.defineProperty,__name=(t,n)=>e(t,`name`,{value:n,configurable:!0});export{__name};
package/lib/esm/blob.js CHANGED
@@ -1 +1 @@
1
- const e=new TextEncoder;var t=class BlobPolyfill{_parts;size;type;constructor(t,n){this._parts=t||[],this.type=n?.type||``,this.size=this._parts.reduce((t,n)=>typeof n==`string`?t+e.encode(n).byteLength:n instanceof ArrayBuffer||ArrayBuffer.isView(n)?t+n.byteLength:n&&typeof n.size==`number`?t+n.size:t,0)}async bytes(){let e=await this.arrayBuffer();return new Uint8Array(e)}async text(){return new TextDecoder().decode(await this.arrayBuffer())}async arrayBuffer(){let t=[];for(let n of this._parts)if(typeof n==`string`)t.push(e.encode(n));else if(n instanceof ArrayBuffer)t.push(new Uint8Array(n));else if(ArrayBuffer.isView(n))t.push(new Uint8Array(n.buffer,n.byteOffset,n.byteLength));else if(n&&typeof n.arrayBuffer==`function`){let e=await n.arrayBuffer();t.push(new Uint8Array(e))}let n=t.reduce((e,t)=>e+t.byteLength,0),r=new Uint8Array(n),i=0;for(let e of t)r.set(e,i),i+=e.byteLength;return r.buffer}slice(e,t,n){return new BlobPolyfill([],{type:n})}stream(){throw Error(`Blob.stream() not implemented`)}},FilePolyfill=class extends t{name;lastModified;webkitRelativePath=``;constructor(e,t,n){super(e,n),this.name=t,this.lastModified=n?.lastModified??Date.now()}};const n=globalThis.Blob??t,r=globalThis.File??FilePolyfill;export{n as Blob,r as File};
1
+ import"./_virtual/_rolldown/runtime.js";const e=new TextEncoder;var t=class BlobPolyfill{_parts;size;type;constructor(t,n){this._parts=t||[],this.type=n?.type||``,this.size=this._parts.reduce((t,n)=>typeof n==`string`?t+e.encode(n).byteLength:n instanceof ArrayBuffer||ArrayBuffer.isView(n)?t+n.byteLength:n&&typeof n.size==`number`?t+n.size:t,0)}async bytes(){let e=await this.arrayBuffer();return new Uint8Array(e)}async text(){return new TextDecoder().decode(await this.arrayBuffer())}async arrayBuffer(){let t=[];for(let n of this._parts)if(typeof n==`string`)t.push(e.encode(n));else if(n instanceof ArrayBuffer)t.push(new Uint8Array(n));else if(ArrayBuffer.isView(n))t.push(new Uint8Array(n.buffer,n.byteOffset,n.byteLength));else if(n&&typeof n.arrayBuffer==`function`){let e=await n.arrayBuffer();t.push(new Uint8Array(e))}let n=t.reduce((e,t)=>e+t.byteLength,0),r=new Uint8Array(n),i=0;for(let e of t)r.set(e,i),i+=e.byteLength;return r.buffer}slice(e,t,n){return new BlobPolyfill([],{type:n})}stream(){let e=this;return new ReadableStream({async pull(t){let n=await e.arrayBuffer();t.enqueue(new Uint8Array(n)),t.close()}})}},FilePolyfill=class extends t{name;lastModified;webkitRelativePath=``;constructor(e,t,n){super(e,n),this.name=t,this.lastModified=n?.lastModified??Date.now()}};const n=globalThis.Blob??t,r=globalThis.File??FilePolyfill;export{n as Blob,r as File};
package/lib/esm/buffer.js CHANGED
@@ -1 +1 @@
1
- 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(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{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(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,15 +1,4 @@
1
- declare class BlobPolyfill implements Blob {
2
- _parts: BlobPart[];
3
- readonly size: number;
4
- readonly type: string;
5
- constructor(parts?: BlobPart[], options?: BlobPropertyBag);
6
- bytes(): Promise<Uint8Array<ArrayBuffer>>;
7
- text(): Promise<string>;
8
- arrayBuffer(): Promise<ArrayBuffer>;
9
- slice(start?: number, end?: number, type?: string): Blob;
10
- stream(): ReadableStream;
11
- }
12
- declare const Blob: typeof BlobPolyfill | {
1
+ declare const Blob: {
13
2
  new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
14
3
  prototype: Blob;
15
4
  };
@@ -9,17 +9,6 @@ declare const _default: {
9
9
  Buffer: typeof Buffer;
10
10
  SlowBuffer: typeof SlowBuffer;
11
11
  Blob: {
12
- new (parts?: BlobPart[], options?: BlobPropertyBag): {
13
- _parts: BlobPart[];
14
- readonly size: number;
15
- readonly type: string;
16
- bytes(): Promise<Uint8Array<ArrayBuffer>>;
17
- text(): Promise<string>;
18
- arrayBuffer(): Promise<ArrayBuffer>;
19
- slice(start?: number, end?: number, type?: string): Blob;
20
- stream(): ReadableStream;
21
- };
22
- } | {
23
12
  new (blobParts?: BlobPart[], options?: BlobPropertyBag): Blob;
24
13
  prototype: Blob;
25
14
  };
package/package.json CHANGED
@@ -1,48 +1,51 @@
1
1
  {
2
- "name": "@gjsify/buffer",
3
- "version": "0.3.21",
4
- "description": "Node.js buffer module for Gjs",
5
- "type": "module",
6
- "module": "lib/esm/index.js",
7
- "types": "lib/types/index.d.ts",
8
- "exports": {
9
- ".": {
10
- "types": "./lib/types/index.d.ts",
11
- "default": "./lib/esm/index.js"
2
+ "name": "@gjsify/buffer",
3
+ "version": "0.4.3",
4
+ "description": "Node.js buffer module for Gjs",
5
+ "type": "module",
6
+ "module": "lib/esm/index.js",
7
+ "types": "lib/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/types/index.d.ts",
11
+ "default": "./lib/esm/index.js"
12
+ },
13
+ "./register": {
14
+ "types": "./lib/types/register.d.ts",
15
+ "default": "./lib/esm/register.js"
16
+ }
12
17
  },
13
- "./register": {
14
- "types": "./lib/types/register.d.ts",
15
- "default": "./lib/esm/register.js"
18
+ "files": [
19
+ "lib"
20
+ ],
21
+ "sideEffects": [
22
+ "./lib/esm/register.js"
23
+ ],
24
+ "scripts": {
25
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
26
+ "check": "tsc --noEmit",
27
+ "build": "gjsify run build:gjsify && gjsify run build:types",
28
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
29
+ "build:types": "tsc",
30
+ "build:test": "gjsify run build:test:gjs && gjsify run build:test:node",
31
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
32
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
33
+ "test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:node && gjsify run test:gjs",
34
+ "test:gjs": "gjsify run test.gjs.mjs",
35
+ "test:node": "node test.node.mjs"
36
+ },
37
+ "keywords": [
38
+ "gjs",
39
+ "node",
40
+ "buffer"
41
+ ],
42
+ "devDependencies": {
43
+ "@gjsify/cli": "workspace:^",
44
+ "@gjsify/unit": "workspace:^",
45
+ "@types/node": "^25.6.2",
46
+ "typescript": "^6.0.3"
47
+ },
48
+ "dependencies": {
49
+ "@gjsify/utils": "workspace:^"
16
50
  }
17
- },
18
- "sideEffects": [
19
- "./lib/esm/register.js"
20
- ],
21
- "scripts": {
22
- "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
23
- "check": "tsc --noEmit",
24
- "build": "yarn build:gjsify && yarn build:types",
25
- "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
26
- "build:types": "tsc",
27
- "build:test": "yarn build:test:gjs && yarn build:test:node",
28
- "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
29
- "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
30
- "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
31
- "test:gjs": "gjsify run test.gjs.mjs",
32
- "test:node": "node test.node.mjs"
33
- },
34
- "keywords": [
35
- "gjs",
36
- "node",
37
- "buffer"
38
- ],
39
- "devDependencies": {
40
- "@gjsify/cli": "^0.3.21",
41
- "@gjsify/unit": "^0.3.21",
42
- "@types/node": "^25.6.2",
43
- "typescript": "^6.0.3"
44
- },
45
- "dependencies": {
46
- "@gjsify/utils": "^0.3.21"
47
- }
48
- }
51
+ }
package/src/blob.ts DELETED
@@ -1,77 +0,0 @@
1
- // WHATWG Blob/File polyfill for GJS
2
- // Reference: Node.js lib/buffer.js (Blob), refs/deno/ext/web/09_file.js
3
- // Node.js exports Blob from 'node:buffer' since v18.
4
- // On GJS, globalThis.Blob is not available — this provides a minimal polyfill.
5
-
6
- const _encoder = new TextEncoder();
7
-
8
- class BlobPolyfill implements Blob {
9
- _parts: BlobPart[];
10
- readonly size: number;
11
- readonly type: string;
12
-
13
- constructor(parts?: BlobPart[], options?: BlobPropertyBag) {
14
- this._parts = parts || [];
15
- this.type = options?.type || '';
16
- this.size = this._parts.reduce((acc: number, part: BlobPart) => {
17
- if (typeof part === 'string') return acc + _encoder.encode(part).byteLength;
18
- if (part instanceof ArrayBuffer) return acc + part.byteLength;
19
- if (ArrayBuffer.isView(part)) return acc + part.byteLength;
20
- if (part && typeof (part as Blob).size === 'number') return acc + (part as Blob).size;
21
- return acc;
22
- }, 0);
23
- }
24
-
25
- async bytes(): Promise<Uint8Array<ArrayBuffer>> {
26
- const ab = await this.arrayBuffer();
27
- return new Uint8Array(ab);
28
- }
29
-
30
- async text(): Promise<string> {
31
- return new TextDecoder().decode(await this.arrayBuffer());
32
- }
33
-
34
- async arrayBuffer(): Promise<ArrayBuffer> {
35
- const chunks: Uint8Array[] = [];
36
- for (const part of this._parts) {
37
- if (typeof part === 'string') chunks.push(_encoder.encode(part));
38
- else if (part instanceof ArrayBuffer) chunks.push(new Uint8Array(part));
39
- else if (ArrayBuffer.isView(part)) chunks.push(new Uint8Array(part.buffer as ArrayBuffer, part.byteOffset, part.byteLength));
40
- else if (part && typeof (part as Blob).arrayBuffer === 'function') {
41
- const ab = await (part as Blob).arrayBuffer();
42
- chunks.push(new Uint8Array(ab));
43
- }
44
- }
45
- const total = chunks.reduce((a, c) => a + c.byteLength, 0);
46
- const result = new Uint8Array(total);
47
- let offset = 0;
48
- for (const c of chunks) { result.set(c, offset); offset += c.byteLength; }
49
- return result.buffer as ArrayBuffer;
50
- }
51
-
52
- slice(start?: number, end?: number, type?: string): Blob {
53
- return new BlobPolyfill([], { type }) as unknown as Blob;
54
- }
55
-
56
- stream(): ReadableStream {
57
- throw new Error('Blob.stream() not implemented');
58
- }
59
- }
60
-
61
- class FilePolyfill extends BlobPolyfill {
62
- readonly name: string;
63
- readonly lastModified: number;
64
- readonly webkitRelativePath: string = '';
65
-
66
- constructor(parts: BlobPart[], name: string, options?: FilePropertyBag) {
67
- super(parts, options);
68
- this.name = name;
69
- this.lastModified = options?.lastModified ?? Date.now();
70
- }
71
- }
72
-
73
- // Use native if available, polyfill otherwise
74
- const Blob = globalThis.Blob ?? BlobPolyfill;
75
- const File = (globalThis as any).File ?? FilePolyfill;
76
-
77
- export { Blob, File };