@wener/utils 1.1.46 → 1.1.47

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.
@@ -57,16 +57,23 @@ export class ArrayBuffers {
57
57
  }
58
58
  return this.toUint8Array(ArrayBuffers.from(v, 'hex'));
59
59
  };
60
- static resize = (v, newByteLength)=>{
60
+ static resize = (v, newByteLength, maxByteLength)=>{
61
61
  if (newByteLength === undefined || newByteLength === null) {
62
62
  return v;
63
63
  }
64
- if ('resize' in v && v.resizable) {
65
- v.resize(newByteLength);
66
- return v;
64
+ // Chrome 111, Nodejs 20
65
+ if ('resize' in v && typeof v.resize === 'function') {
66
+ if ('resizable' in v && v.resizable) {
67
+ if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {
68
+ v.resize(newByteLength);
69
+ return v;
70
+ }
71
+ }
67
72
  }
68
73
  const old = v;
69
- const newBuf = new ArrayBuffer(newByteLength);
74
+ const newBuf = new ArrayBuffer(newByteLength, {
75
+ maxByteLength: maxByteLength
76
+ });
70
77
  const oldView = new Uint8Array(old);
71
78
  const newView = new Uint8Array(newBuf);
72
79
  newView.set(oldView);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/io/ArrayBuffers.ts"],"sourcesContent":["import { classOf } from '../langs/classOf';\nimport { getGlobalThis } from '../runtime/getGlobalThis';\nimport { decodeBase64ToArrayBuffer, encodeArrayBufferToBase64 } from './base64';\nimport { isBuffer } from './isBuffer';\n\n/**\n * Various utils to work with {@link ArrayBuffer}\n *\n * @see https://github.com/tc39/proposal-arraybuffer-base64\n * @see https://github.com/tc39/proposal-resizablearraybuffer\n */\nexport interface ArrayBuffers {\n /**\n * isArrayBuffer check if the given value is an {@link ArrayBuffer}\n */\n isArrayBuffer(v: any): v is ArrayBuffer;\n\n /**\n * slice the given view with the given offset and length, will handle the {@link Buffer} as well\n *\n * @see {@link https://nodejs.org/api/buffer.html#bufslicestart-end Buffer.slice}\n */\n slice<T extends ArrayBufferView>(o: T, start?: number, end?: number): T;\n\n /**\n * asView convert the given value to given {@link TypedArray} view\n *\n * TypedArray can be {@link Buffer}, will avoid copy\n */\n asView<C extends ArrayBufferViewConstructor<unknown>>(\n TypedArray: C,\n v: BufferSource,\n byteOffset?: number,\n byteLength?: number,\n ): InstanceType<C>;\n\n /**\n * toString convert the given {@link BufferSource} to string\n */\n toString(v: BufferSource | string, encoding?: ToStringEncoding): string;\n\n /**\n * Returns true if encoding is the name of a supported character encoding, or false otherwise.\n */\n isEncoding(v?: string): v is ToStringEncoding;\n\n toJSON<T = any>(v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any): T;\n\n /**\n * from convert the given value to {@link ArrayBuffer}\n */\n from(v: string | BufferSource, encoding?: ToStringEncoding): ArrayBuffer;\n\n from<C extends ArrayBufferViewConstructor<unknown>>(\n v: string | BufferSource,\n encoding: ToStringEncoding,\n TypedArray: C,\n ): C;\n\n /**\n * concat the given {@link BufferSource} to a new {@link ArrayBuffer}\n */\n concat(buffers: Array<BufferSource>, result?: ArrayBuffer, offset?: number): ArrayBuffer;\n\n fromBase64(v: string): Uint8Array;\n\n fromHex(v: string): Uint8Array;\n\n toBase64(v: BufferSource): string;\n\n toHex(v: BufferSource): string;\n\n resize(v: ArrayBuffer, newByteLength: number): ArrayBuffer;\n\n // truncate(v: ArrayBufferLike, len?: number): ArrayBufferLike;\n}\n\ntype ToStringEncoding =\n | 'ascii'\n | 'utf16le'\n // | 'utf-16le'\n | 'ucs2'\n | 'ucs-2'\n | 'base64'\n | 'base64url'\n | 'latin1'\n | 'binary'\n | 'utf8'\n | 'utf-8'\n | 'hex';\n\nexport class ArrayBuffers {\n static #nativeBufferAllowed: boolean = true;\n static #isBufferAvailable: undefined | boolean;\n\n static isNativeBufferAvailable() {\n // eslint-disable-next-line no-return-assign\n return (this.#isBufferAvailable ??= !(getGlobalThis().Buffer as any)?.isPollyfill?.());\n }\n\n static isNativeBufferAllowed() {\n return this.#nativeBufferAllowed && this.#isBufferAvailable;\n }\n\n static setNativeBufferAllowed(v: boolean) {\n this.#nativeBufferAllowed = v;\n }\n\n static isArrayBuffer = (v: any): v is ArrayBuffer => {\n return v instanceof ArrayBuffer;\n };\n\n static toArrayBuffer(v: BufferSource): ArrayBuffer {\n return v instanceof ArrayBuffer ? v : (v.buffer as ArrayBuffer);\n }\n\n static toUint8Array(v: BufferSource) {\n return ArrayBuffers.asView(Uint8Array, v);\n }\n\n static toBase64 = (v: BufferSource) => {\n if ('toBase64' in Uint8Array.prototype) {\n return this.toUint8Array(v).toBase64();\n }\n\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('base64');\n }\n\n return encodeArrayBufferToBase64(this.toArrayBuffer(v));\n };\n\n static toHex = (v: BufferSource) => {\n if ('toHex' in Uint8Array.prototype) {\n return this.toUint8Array(v).toHex();\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('hex');\n }\n return ArrayBuffers.toString(v, 'hex');\n };\n\n static fromBase64 = (v: string) => {\n if ('fromBase64' in Uint8Array) {\n return Uint8Array.fromBase64(v);\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(v, 'base64');\n }\n return this.toUint8Array(decodeBase64ToArrayBuffer(v));\n };\n\n static fromHex = (v: string) => {\n if ('fromHex' in Uint8Array) {\n return Uint8Array.fromHex(v);\n }\n return this.toUint8Array(ArrayBuffers.from(v, 'hex'));\n };\n\n static resize = (v: ArrayBufferLike, newByteLength?: number) => {\n if (newByteLength === undefined || newByteLength === null) {\n return v;\n }\n\n if ('resize' in v && (v as any).resizable) {\n (v as any).resize(newByteLength);\n return v;\n }\n\n const old = v;\n const newBuf = new ArrayBuffer(newByteLength);\n const oldView = new Uint8Array(old);\n const newView = new Uint8Array(newBuf);\n newView.set(oldView);\n return newBuf;\n };\n\n static slice = (o: TypedArray, start?: number, end?: number) => {\n // NodeJS Buffer slice is not the same as UInt8Array slice\n // https://nodejs.org/api/buffer.html#bufslicestart-end\n if (isBuffer(o)) {\n return Uint8Array.prototype.slice.call(o, start, end);\n }\n return o.slice(start, end);\n };\n\n static asView = <C extends ArrayBufferViewConstructor<unknown>, I extends InstanceType<C>>(\n TypedArray: C,\n v: BufferSource,\n byteOffset?: number,\n byteLength?: number,\n ): I => {\n if (v instanceof TypedArray && (byteOffset ?? 0) === 0 && byteLength === undefined) {\n return v as I;\n }\n if (ArrayBuffer.isView(v) || isBuffer(v)) {\n if (ArrayBuffers.isNativeBufferAllowed() && (TypedArray as any) === Buffer) {\n // new Buffer() is deprecated\n return Buffer.from(v.buffer, byteOffset, byteLength) as I;\n }\n return new TypedArray(v.buffer, v.byteOffset + (byteOffset ?? 0), byteLength ?? v.byteLength) as I;\n }\n return new TypedArray(v, byteOffset, byteLength) as I;\n };\n\n static toString = (buf: BufferSource | string, encoding: ToStringEncoding = 'utf8') => {\n // 'ascii' 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'\n if (typeof buf === 'string') {\n switch (encoding) {\n case 'base64':\n return btoa(buf);\n case 'utf-8':\n case 'utf8':\n return buf;\n default:\n throw new Error(`[ArrayBuffers.toString] Unsupported encoding for string: ${encoding}`);\n }\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, buf)).toString(encoding);\n }\n // reference\n // https://github.com/feross/buffer/blob/master/index.js\n switch (encoding) {\n case 'hex': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return [...view].map((b) => hexLookupTable[b]).join('');\n }\n case 'base64': {\n return encodeArrayBufferToBase64(ArrayBuffers.asView(Uint8Array, buf));\n }\n case 'utf8':\n // falls through\n case 'utf-8':\n return new TextDecoder().decode(buf as any);\n case 'ascii': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return String.fromCharCode(...view.map((v) => v & 0x7f));\n }\n case 'latin1':\n // falls through\n case 'binary': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return String.fromCharCode(...view);\n }\n case 'ucs2':\n // falls through\n case 'ucs-2':\n // case 'utf-16le':\n // falls through\n case 'utf16le': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n let res = '';\n // If length is odd, the last 8 bits must be ignored (same as node.js)\n for (let i = 0; i < view.length - 1; i += 2) {\n res += String.fromCharCode(view[i] + view[i + 1] * 256);\n }\n return res;\n }\n default:\n throw new Error(`[ArrayBuffers.toString] Unknown encoding: ${encoding}`);\n }\n };\n\n static toJSON = (v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any) => {\n return JSON.parse(ArrayBuffers.toString(v), reviver);\n };\n\n static alloc = (size: number, fill?: string | number, encoding?: ToStringEncoding) => {\n if (fill !== undefined) {\n if (typeof fill === 'number') {\n return new Uint8Array(size).fill(fill);\n }\n // as cast\n // https://stackoverflow.com/questions/73994091\n return ArrayBuffers.asView(Uint8Array, ArrayBuffers.from(fill, encoding)).slice(0, size);\n }\n return new ArrayBuffer(size);\n };\n\n static from = (\n v: string | BufferSource | ArrayLike<number> | Iterable<number>,\n encoding: ToStringEncoding = 'utf8',\n view?: any,\n ): any => {\n if (view) {\n return this.asView(view, this.from(v, encoding));\n }\n if (!v) {\n return new ArrayBuffer(0);\n }\n if (typeof v === 'string') {\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(v, encoding);\n }\n\n switch (encoding) {\n case 'utf-8':\n // falls through\n case 'utf8':\n return new TextEncoder().encode(v).buffer;\n case 'base64':\n // replaceAll need higher version of nodejs\n return decodeBase64ToArrayBuffer(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));\n case 'hex':\n return new Uint8Array(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))).buffer;\n default:\n throw new Error(`[ArrayBuffers.from] Unknown encoding: ${encoding}`);\n }\n }\n if (v instanceof ArrayBuffer) {\n return v;\n }\n // lost length\n if (ArrayBuffer.isView(v) || isBuffer(v)) {\n if (v.byteOffset !== 0) {\n // return v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength)\n throw new Error('ArrayBuffers.from do not support view with offset');\n }\n return v.buffer;\n }\n if (Array.isArray(v)) {\n return new Uint8Array(v);\n }\n const type = classOf(v);\n throw new TypeError(`ArrayBuffers.from unsupported type ${type}`);\n };\n\n static isEncoding = (encoding?: string) => {\n switch (String(encoding).toLowerCase()) {\n case 'hex':\n case 'utf8':\n case 'utf-8':\n case 'ascii':\n case 'latin1':\n case 'binary':\n case 'base64':\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n // case 'utf-16le':\n return true;\n default:\n return false;\n }\n };\n\n // static truncate = (a: ArrayBufferLike, len?: number) => {\n // if (len === 0) {\n // return new ArrayBuffer(0);\n // }\n // if (!len) {\n // return a;\n // }\n // if (a.byteLength > len) {\n // return a.slice(0, len);\n // } else if (a.byteLength < len) {\n // let b = new Uint8Array(len);\n // b.set(new Uint8Array(a));\n // return b;\n // }\n // return a;\n // };\n\n static concat = (buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0) => {\n // https://stackoverflow.com/questions/10786128/appending-arraybuffers\n\n const length = buffers.reduce((a, b) => a + b.byteLength, 0);\n const r = result ? new Uint8Array(result) : new Uint8Array(length);\n for (const buffer of buffers) {\n if (!buffer?.byteLength) continue;\n let n: Uint8Array;\n if (buffer instanceof ArrayBuffer) {\n n = new Uint8Array(buffer);\n } else if (ArrayBuffer.isView(buffer)) {\n n = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);\n } else {\n throw new Error(`ArrayBuffers.concat unsupported type ${classOf(buffer)}`);\n }\n r.set(n, offset);\n offset += buffer.byteLength;\n }\n return r.buffer;\n };\n}\n\nexport type TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | BigUint64Array\n | BigInt64Array\n | Float32Array\n | Float64Array;\n\ntype ArrayBufferViewConstructor<T> = new (buffer: ArrayBufferLike, byteOffset?: number, byteLength?: number) => T;\n\nconst hexLookupTable = (function () {\n const alphabet = '0123456789abcdef';\n const table = new Array(256);\n for (let i = 0; i < 16; ++i) {\n const i16 = i * 16;\n for (let j = 0; j < 16; ++j) {\n table[i16 + j] = alphabet[i] + alphabet[j];\n }\n }\n return table;\n})();\n\ndeclare global {\n interface ArrayBuffer {\n resize?: (newByteLength: number) => void;\n resizable?: boolean;\n }\n\n interface SharedArrayBuffer {\n resize?: (newByteLength: number) => void;\n resizable?: boolean;\n }\n\n interface Uint8Array {\n toBase64(): string;\n\n toHex(): string;\n }\n\n interface Uint8ArrayConstructor {\n fromBase64(v: string): Uint8Array;\n\n fromHex(v: string): Uint8Array;\n }\n}\n"],"names":["classOf","getGlobalThis","decodeBase64ToArrayBuffer","encodeArrayBufferToBase64","isBuffer","ArrayBuffers","nativeBufferAllowed","isBufferAvailable","isNativeBufferAvailable","Buffer","isPollyfill","isNativeBufferAllowed","setNativeBufferAllowed","v","isArrayBuffer","ArrayBuffer","toArrayBuffer","buffer","toUint8Array","asView","Uint8Array","toBase64","prototype","from","toString","toHex","fromBase64","fromHex","resize","newByteLength","undefined","resizable","old","newBuf","oldView","newView","set","slice","o","start","end","call","TypedArray","byteOffset","byteLength","isView","buf","encoding","btoa","Error","view","map","b","hexLookupTable","join","TextDecoder","decode","String","fromCharCode","res","i","length","toJSON","reviver","JSON","parse","alloc","size","fill","TextEncoder","encode","replace","match","byte","parseInt","Array","isArray","type","TypeError","isEncoding","toLowerCase","concat","buffers","result","offset","reduce","a","r","n","alphabet","table","i16","j"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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EAAG;QAC3B,MAAM+B,MAAM/B,IAAI;QAChB,IAAK,IAAIgC,IAAI,GAAGA,IAAI,IAAI,EAAEA,EAAG;YAC3BF,KAAK,CAACC,MAAMC,EAAE,GAAGH,QAAQ,CAAC7B,EAAE,GAAG6B,QAAQ,CAACG,EAAE;QAC5C;IACF;IACA,OAAOF;AACT"}
1
+ {"version":3,"sources":["../../src/io/ArrayBuffers.ts"],"sourcesContent":["import { classOf } from '../langs/classOf';\nimport { getGlobalThis } from '../runtime/getGlobalThis';\nimport { decodeBase64ToArrayBuffer, encodeArrayBufferToBase64 } from './base64';\nimport { isBuffer } from './isBuffer';\n\n/**\n * Various utils to work with {@link ArrayBuffer}\n *\n * @see https://github.com/tc39/proposal-arraybuffer-base64\n * @see https://github.com/tc39/proposal-resizablearraybuffer\n */\nexport interface ArrayBuffers {\n /**\n * isArrayBuffer check if the given value is an {@link ArrayBuffer}\n */\n isArrayBuffer(v: any): v is ArrayBuffer;\n\n /**\n * slice the given view with the given offset and length, will handle the {@link Buffer} as well\n *\n * @see {@link https://nodejs.org/api/buffer.html#bufslicestart-end Buffer.slice}\n */\n slice<T extends ArrayBufferView>(o: T, start?: number, end?: number): T;\n\n /**\n * asView convert the given value to given {@link TypedArray} view\n *\n * TypedArray can be {@link Buffer}, will avoid copy\n */\n asView<C extends ArrayBufferViewConstructor<unknown>>(\n TypedArray: C,\n v: BufferSource,\n byteOffset?: number,\n byteLength?: number,\n ): InstanceType<C>;\n\n /**\n * toString convert the given {@link BufferSource} to string\n */\n toString(v: BufferSource | string, encoding?: ToStringEncoding): string;\n\n /**\n * Returns true if encoding is the name of a supported character encoding, or false otherwise.\n */\n isEncoding(v?: string): v is ToStringEncoding;\n\n toJSON<T = any>(v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any): T;\n\n /**\n * from convert the given value to {@link ArrayBuffer}\n */\n from(v: string | BufferSource, encoding?: ToStringEncoding): ArrayBuffer;\n\n from<C extends ArrayBufferViewConstructor<unknown>>(\n v: string | BufferSource,\n encoding: ToStringEncoding,\n TypedArray: C,\n ): C;\n\n /**\n * concat the given {@link BufferSource} to a new {@link ArrayBuffer}\n */\n concat(buffers: Array<BufferSource>, result?: ArrayBuffer, offset?: number): ArrayBuffer;\n\n fromBase64(v: string): Uint8Array;\n\n fromHex(v: string): Uint8Array;\n\n toBase64(v: BufferSource): string;\n\n toHex(v: BufferSource): string;\n\n resize(v: ArrayBuffer, newByteLength: number): ArrayBuffer;\n\n // truncate(v: ArrayBufferLike, len?: number): ArrayBufferLike;\n}\n\ntype ToStringEncoding =\n | 'ascii'\n | 'utf16le'\n // | 'utf-16le'\n | 'ucs2'\n | 'ucs-2'\n | 'base64'\n | 'base64url'\n | 'latin1'\n | 'binary'\n | 'utf8'\n | 'utf-8'\n | 'hex';\n\nexport class ArrayBuffers {\n static #nativeBufferAllowed: boolean = true;\n static #isBufferAvailable: undefined | boolean;\n\n static isNativeBufferAvailable() {\n // eslint-disable-next-line no-return-assign\n return (this.#isBufferAvailable ??= !(getGlobalThis().Buffer as any)?.isPollyfill?.());\n }\n\n static isNativeBufferAllowed() {\n return this.#nativeBufferAllowed && this.#isBufferAvailable;\n }\n\n static setNativeBufferAllowed(v: boolean) {\n this.#nativeBufferAllowed = v;\n }\n\n static isArrayBuffer = (v: any): v is ArrayBuffer => {\n return v instanceof ArrayBuffer;\n };\n\n static toArrayBuffer(v: BufferSource): ArrayBuffer {\n return v instanceof ArrayBuffer ? v : (v.buffer as ArrayBuffer);\n }\n\n static toUint8Array(v: BufferSource) {\n return ArrayBuffers.asView(Uint8Array, v);\n }\n\n static toBase64 = (v: BufferSource) => {\n if ('toBase64' in Uint8Array.prototype) {\n return this.toUint8Array(v).toBase64();\n }\n\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('base64');\n }\n\n return encodeArrayBufferToBase64(this.toArrayBuffer(v));\n };\n\n static toHex = (v: BufferSource) => {\n if ('toHex' in Uint8Array.prototype) {\n return this.toUint8Array(v).toHex();\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('hex');\n }\n return ArrayBuffers.toString(v, 'hex');\n };\n\n static fromBase64 = (v: string) => {\n if ('fromBase64' in Uint8Array) {\n return Uint8Array.fromBase64(v);\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(v, 'base64');\n }\n return this.toUint8Array(decodeBase64ToArrayBuffer(v));\n };\n\n static fromHex = (v: string) => {\n if ('fromHex' in Uint8Array) {\n return Uint8Array.fromHex(v);\n }\n return this.toUint8Array(ArrayBuffers.from(v, 'hex'));\n };\n\n static resize = (v: ArrayBufferLike, newByteLength?: number, maxByteLength?: number): ArrayBuffer => {\n if (newByteLength === undefined || newByteLength === null) {\n return v;\n }\n\n // Chrome 111, Nodejs 20\n if ('resize' in v && typeof v.resize === 'function') {\n if ('resizable' in v && v.resizable) {\n if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {\n v.resize(newByteLength);\n return v;\n }\n }\n }\n\n const old = v;\n const newBuf = new ArrayBuffer(newByteLength, { maxByteLength: maxByteLength });\n const oldView = new Uint8Array(old);\n const newView = new Uint8Array(newBuf);\n newView.set(oldView);\n return newBuf;\n };\n\n static slice = (o: TypedArray, start?: number, end?: number) => {\n // NodeJS Buffer slice is not the same as UInt8Array slice\n // https://nodejs.org/api/buffer.html#bufslicestart-end\n if (isBuffer(o)) {\n return Uint8Array.prototype.slice.call(o, start, end);\n }\n return o.slice(start, end);\n };\n\n static asView = <C extends ArrayBufferViewConstructor<unknown>, I extends InstanceType<C>>(\n TypedArray: C,\n v: BufferSource,\n byteOffset?: number,\n byteLength?: number,\n ): I => {\n if (v instanceof TypedArray && (byteOffset ?? 0) === 0 && byteLength === undefined) {\n return v as I;\n }\n if (ArrayBuffer.isView(v) || isBuffer(v)) {\n if (ArrayBuffers.isNativeBufferAllowed() && (TypedArray as any) === Buffer) {\n // new Buffer() is deprecated\n return Buffer.from(v.buffer, byteOffset, byteLength) as I;\n }\n return new TypedArray(v.buffer, v.byteOffset + (byteOffset ?? 0), byteLength ?? v.byteLength) as I;\n }\n return new TypedArray(v, byteOffset, byteLength) as I;\n };\n\n static toString = (buf: BufferSource | string, encoding: ToStringEncoding = 'utf8') => {\n // 'ascii' 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'\n if (typeof buf === 'string') {\n switch (encoding) {\n case 'base64':\n return btoa(buf);\n case 'utf-8':\n case 'utf8':\n return buf;\n default:\n throw new Error(`[ArrayBuffers.toString] Unsupported encoding for string: ${encoding}`);\n }\n }\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(ArrayBuffers.asView(Uint8Array, buf)).toString(encoding);\n }\n // reference\n // https://github.com/feross/buffer/blob/master/index.js\n switch (encoding) {\n case 'hex': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return [...view].map((b) => hexLookupTable[b]).join('');\n }\n case 'base64': {\n return encodeArrayBufferToBase64(ArrayBuffers.asView(Uint8Array, buf));\n }\n case 'utf8':\n // falls through\n case 'utf-8':\n return new TextDecoder().decode(buf as any);\n case 'ascii': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return String.fromCharCode(...view.map((v) => v & 0x7f));\n }\n case 'latin1':\n // falls through\n case 'binary': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n return String.fromCharCode(...view);\n }\n case 'ucs2':\n // falls through\n case 'ucs-2':\n // case 'utf-16le':\n // falls through\n case 'utf16le': {\n const view: Uint8Array = ArrayBuffers.asView(Uint8Array, buf);\n let res = '';\n // If length is odd, the last 8 bits must be ignored (same as node.js)\n for (let i = 0; i < view.length - 1; i += 2) {\n res += String.fromCharCode(view[i] + view[i + 1] * 256);\n }\n return res;\n }\n default:\n throw new Error(`[ArrayBuffers.toString] Unknown encoding: ${encoding}`);\n }\n };\n\n static toJSON = (v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any) => {\n return JSON.parse(ArrayBuffers.toString(v), reviver);\n };\n\n static alloc = (size: number, fill?: string | number, encoding?: ToStringEncoding) => {\n if (fill !== undefined) {\n if (typeof fill === 'number') {\n return new Uint8Array(size).fill(fill);\n }\n // as cast\n // https://stackoverflow.com/questions/73994091\n return ArrayBuffers.asView(Uint8Array, ArrayBuffers.from(fill, encoding)).slice(0, size);\n }\n return new ArrayBuffer(size);\n };\n\n static from = (\n v: string | BufferSource | ArrayLike<number> | Iterable<number>,\n encoding: ToStringEncoding = 'utf8',\n view?: any,\n ): any => {\n if (view) {\n return this.asView(view, this.from(v, encoding));\n }\n if (!v) {\n return new ArrayBuffer(0);\n }\n if (typeof v === 'string') {\n if (ArrayBuffers.isNativeBufferAllowed()) {\n return Buffer.from(v, encoding);\n }\n\n switch (encoding) {\n case 'utf-8':\n // falls through\n case 'utf8':\n return new TextEncoder().encode(v).buffer;\n case 'base64':\n // replaceAll need higher version of nodejs\n return decodeBase64ToArrayBuffer(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));\n case 'hex':\n return new Uint8Array(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))).buffer;\n default:\n throw new Error(`[ArrayBuffers.from] Unknown encoding: ${encoding}`);\n }\n }\n if (v instanceof ArrayBuffer) {\n return v;\n }\n // lost length\n if (ArrayBuffer.isView(v) || isBuffer(v)) {\n if (v.byteOffset !== 0) {\n // return v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength)\n throw new Error('ArrayBuffers.from do not support view with offset');\n }\n return v.buffer;\n }\n if (Array.isArray(v)) {\n return new Uint8Array(v);\n }\n const type = classOf(v);\n throw new TypeError(`ArrayBuffers.from unsupported type ${type}`);\n };\n\n static isEncoding = (encoding?: string) => {\n switch (String(encoding).toLowerCase()) {\n case 'hex':\n case 'utf8':\n case 'utf-8':\n case 'ascii':\n case 'latin1':\n case 'binary':\n case 'base64':\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n // case 'utf-16le':\n return true;\n default:\n return false;\n }\n };\n\n // static truncate = (a: ArrayBufferLike, len?: number) => {\n // if (len === 0) {\n // return new ArrayBuffer(0);\n // }\n // if (!len) {\n // return a;\n // }\n // if (a.byteLength > len) {\n // return a.slice(0, len);\n // } else if (a.byteLength < len) {\n // let b = new Uint8Array(len);\n // b.set(new Uint8Array(a));\n // return b;\n // }\n // return a;\n // };\n\n static concat = (buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0) => {\n // https://stackoverflow.com/questions/10786128/appending-arraybuffers\n\n const length = buffers.reduce((a, b) => a + b.byteLength, 0);\n const r = result ? new Uint8Array(result) : new Uint8Array(length);\n for (const buffer of buffers) {\n if (!buffer?.byteLength) continue;\n let n: Uint8Array;\n if (buffer instanceof ArrayBuffer) {\n n = new Uint8Array(buffer);\n } else if (ArrayBuffer.isView(buffer)) {\n n = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);\n } else {\n throw new Error(`ArrayBuffers.concat unsupported type ${classOf(buffer)}`);\n }\n r.set(n, offset);\n offset += buffer.byteLength;\n }\n return r.buffer;\n };\n}\n\nexport type TypedArray =\n | Uint8Array\n | Uint8ClampedArray\n | Uint16Array\n | Uint32Array\n | Int8Array\n | Int16Array\n | Int32Array\n | BigUint64Array\n | BigInt64Array\n | Float32Array\n | Float64Array;\n\ntype ArrayBufferViewConstructor<T> = new (buffer: ArrayBufferLike, byteOffset?: number, byteLength?: number) => T;\n\nconst hexLookupTable = (function () {\n const alphabet = '0123456789abcdef';\n const table = new Array(256);\n for (let i = 0; i < 16; ++i) {\n const i16 = i * 16;\n for (let j = 0; j < 16; ++j) {\n table[i16 + j] = alphabet[i] + alphabet[j];\n }\n }\n return table;\n})();\n\ndeclare global {\n interface ArrayBuffer {\n resize?: (newByteLength: number) => void;\n resizable?: boolean;\n }\n\n interface SharedArrayBuffer {\n resize?: (newByteLength: number) => void;\n resizable?: boolean;\n }\n\n interface Uint8Array {\n toBase64(): string;\n\n toHex(): string;\n }\n\n interface Uint8ArrayConstructor {\n fromBase64(v: string): Uint8Array;\n\n fromHex(v: string): Uint8Array;\n }\n}\n"],"names":["classOf","getGlobalThis","decodeBase64ToArrayBuffer","encodeArrayBufferToBase64","isBuffer","ArrayBuffers","nativeBufferAllowed","isBufferAvailable","isNativeBufferAvailable","Buffer","isPollyfill","isNativeBufferAllowed","setNativeBufferAllowed","v","isArrayBuffer","ArrayBuffer","toArrayBuffer","buffer","toUint8Array","asView","Uint8Array","toBase64","prototype","from","toString","toHex","fromBase64","fromHex","resize","newByteLength","maxByteLength","undefined","resizable","old","newBuf","oldView","newView","set","slice","o","start","end","call","TypedArray","byteOffset","byteLength","isView","buf","encoding","btoa","Error","view","map","b","hexLookupTable","join","TextDecoder","decode","String","fromCharCode","res","i","length","toJSON","reviver","JSON","parse","alloc","size","fill","TextEncoder","encode","replace","match","byte","parseInt","Array","isArray","type","TypeError","isEncoding","toLowerCase","concat","buffers","result","offset","reduce","a","r","n","alphabet","table","i16","j"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,OAAO,QAAQ,mBAAmB;AAC3C,SAASC,aAAa,QAAQ,2BAA2B;AACzD,SAASC,yBAAyB,EAAEC,yBAAyB,QAAQ,WAAW;AAChF,SAASC,QAAQ,QAAQ,aAAa;AAwFtC,OAAO,MAAMC;IACX,OAAO,CAACC,mBAAmB,GAAY,KAAK;IAC5C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B,QAAQ,CAACG,EAAE;QAC5C;IACF;IACA,OAAOF;AACT"}
@@ -1,4 +1,3 @@
1
- import { isDefined } from '../langs/isDefined.js';
2
1
  import { ArrayBuffers } from './ArrayBuffers.js';
3
2
  function asBuffer(o) {
4
3
  if (o instanceof ArrayBuffer) {
@@ -64,15 +63,7 @@ function asBuffer(o) {
64
63
  }
65
64
  }
66
65
  resize(newLength) {
67
- // Chrome 111, Nodejs 20
68
- let buf = this.buffer;
69
- if (buf.resize && (!isDefined(buf.resizable) || buf.resizable)) {
70
- buf.resize(newLength);
71
- } else {
72
- let newBuffer = new ArrayBuffer(newLength);
73
- new Uint8Array(newBuffer).set(new Uint8Array(buf));
74
- this.buffer = newBuffer;
75
- }
66
+ this.buffer = ArrayBuffers.resize(this.buffer, newLength, Math.ceil(newLength * 1.2));
76
67
  }
77
68
  writeByte(value) {
78
69
  this.willWrite(1);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/io/ByteBuffer.ts"],"sourcesContent":["import { isDefined } from '../langs/isDefined';\nimport { ArrayBuffers } from './ArrayBuffers';\n\ntype AnyBuffer = BufferSource | ArrayBufferLike;\n\nfunction asBuffer(o: AnyBuffer) {\n if (o instanceof ArrayBuffer) {\n return o;\n }\n if (ArrayBuffer.isView(o)) {\n // 保留 offset&length\n if (o.byteLength !== o.buffer.byteLength) {\n // ArrayBuffer 没有 subarray\n // if ('subarray' in o.buffer) {\n // return (o.buffer as any).subarray(o.byteOffset, o.byteOffset + o.byteLength);\n // }\n return o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength);\n }\n return o.buffer;\n }\n return o;\n}\n\n// function asView(o: AnyBuffer) {\n// if (o instanceof DataView) {\n// return o;\n// }\n// if (ArrayBuffer.isView(o)) {\n// // 不 clone 也能保留 offset&length\n// return new DataView(o.buffer, o.byteOffset, o.byteLength);\n// }\n// return new DataView(o);\n// }\n\n/**\n * @see {@link https://www.egret.uk/docs/egretengine/engine/egret.ByteArray egret.ByteArray}\n * @see {@link https://github.com/protobufjs/bytebuffer.js protobufjs/bytebuffer.js}\n * @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html ByteBuf}\n */\nexport class ByteBuffer {\n position = 0;\n\n #buffer: ArrayBufferLike;\n #view: DataView;\n\n constructor(buffer: AnyBuffer = new ArrayBuffer(0, { maxByteLength: 1024 })) {\n this.#buffer = asBuffer(buffer);\n // NOTE prefer view over buffer, avoid the slice overhead ?\n this.#view = new DataView(this.#buffer);\n }\n\n get view() {\n return this.#view;\n }\n\n get buffer(): ArrayBufferLike {\n return this.#buffer;\n }\n\n set buffer(buffer: AnyBuffer) {\n this.#buffer = asBuffer(buffer);\n this.#view = new DataView(this.#buffer);\n }\n\n get length() {\n return this.view.byteLength;\n }\n\n set length(length: number) {\n this.resize(length);\n }\n\n private willWrite(length: number) {\n if (this.remaining() < length) {\n this.resize(this.length + length);\n }\n }\n\n resize(newLength: number) {\n // Chrome 111, Nodejs 20\n let buf = this.buffer;\n if (buf.resize && (!isDefined(buf.resizable) || buf.resizable)) {\n buf.resize(newLength);\n } else {\n let newBuffer = new ArrayBuffer(newLength);\n new Uint8Array(newBuffer).set(new Uint8Array(buf));\n this.buffer = newBuffer;\n }\n }\n\n writeByte(value: number) {\n this.willWrite(1);\n this.view.setUint8(this.position++, value);\n }\n\n writeBytes(bytes: ArrayBufferLike, len: number = bytes.byteLength) {\n this.willWrite(len);\n if (len !== bytes.byteLength) bytes = bytes.slice(0, len);\n new Uint8Array(this.buffer).set(new Uint8Array(bytes), this.position);\n this.position += bytes.byteLength;\n }\n\n writeInt8(value: number) {\n this.willWrite(1);\n this.view.setInt8(this.position, value);\n this.position += 1;\n }\n\n writeUint8(value: number) {\n this.willWrite(1);\n this.view.setUint8(this.position, value);\n this.position += 1;\n }\n\n writeInt16(value: number) {\n this.willWrite(2);\n this.view.setInt16(this.position, value);\n this.position += 2;\n }\n\n writeUint16(value: number) {\n this.willWrite(2);\n this.view.setUint16(this.position, value);\n this.position += 2;\n }\n\n writeInt32(value: number) {\n this.willWrite(4);\n this.view.setInt32(this.position, value);\n this.position += 4;\n }\n\n writeUint32(value: number) {\n this.willWrite(4);\n this.view.setUint32(this.position, value);\n this.position += 4;\n }\n\n writeInt64(value: bigint) {\n this.willWrite(8);\n this.view.setBigInt64(this.position, value);\n this.position += 8;\n }\n\n writeUint64(value: bigint) {\n this.willWrite(8);\n this.view.setBigUint64(this.position, value);\n this.position += 8;\n }\n\n writeFloat32(value: number) {\n this.willWrite(4);\n this.view.setFloat32(this.position, value);\n this.position += 4;\n }\n\n writeFloat64(value: number) {\n this.willWrite(8);\n this.view.setFloat64(this.position, value);\n this.position += 8;\n }\n\n writeBoolean(value: boolean) {\n this.writeByte(value ? 1 : 0);\n }\n\n writeString(value: string): void;\n writeString(value: string, len?: number): void;\n writeString(value: string, len?: number) {\n let encoder = new TextEncoder();\n let bytes = encoder.encode(value);\n this.writeBytes(bytes, len);\n }\n\n readByte() {\n return this.view.getUint8(this.position++);\n }\n\n readBytes(length: number) {\n let bytes = this.buffer.slice(this.position, this.position + length);\n this.position += length;\n return bytes;\n }\n\n readInt8() {\n let value = this.view.getInt8(this.position);\n this.position += 1;\n return value;\n }\n\n readUint8() {\n let value = this.view.getUint8(this.position);\n this.position += 1;\n return value;\n }\n\n readInt16() {\n let value = this.view.getInt16(this.position);\n this.position += 2;\n return value;\n }\n\n readUint16() {\n let value = this.view.getUint16(this.position);\n this.position += 2;\n return value;\n }\n\n readInt32() {\n let value = this.view.getInt32(this.position);\n this.position += 4;\n return value;\n }\n\n readUint32() {\n let value = this.view.getUint32(this.position);\n this.position += 4;\n return value;\n }\n\n readInt64() {\n let value = this.view.getBigInt64(this.position);\n this.position += 8;\n return safeNumber(value);\n }\n\n readUint64() {\n let value = this.view.getBigUint64(this.position);\n this.position += 8;\n return safeNumber(value);\n }\n\n readFloat32() {\n let value = this.view.getFloat32(this.position);\n this.position += 4;\n return value;\n }\n\n readFloat64() {\n let value = this.view.getFloat64(this.position);\n this.position += 8;\n return value;\n }\n\n readBoolean() {\n return this.readByte() === 1;\n }\n\n readString(length: number) {\n let bytes = this.readBytes(length);\n let decoder = new TextDecoder();\n let a = new Uint8Array(bytes);\n let idx = a.indexOf(0);\n if (idx !== -1) {\n bytes = bytes.slice(0, idx);\n }\n return decoder.decode(bytes);\n }\n\n readHexString(length: number) {\n let bytes = this.readBytes(length);\n return ArrayBuffers.toHex(bytes);\n }\n\n writeInt24(value: number) {\n this.writeUint8(value & 0xff);\n this.writeUint16(value >> 8);\n }\n\n readInt24() {\n return this.readUint8() | (this.readUint16() << 8);\n }\n\n writeZero(length: number) {\n this.writeBytes(new Uint8Array(length).buffer);\n }\n\n writeValue(typ: TypedValue['type'], val: TypedValue['value']): void;\n writeValue(tv: TypedValue): void;\n writeValue(a: any, b?: any) {\n const tv: TypedValue = typeof a === 'string' ? { type: a, value: b } : a;\n const { type, value, length } = tv;\n switch (type) {\n case 'uint8':\n this.writeUint8(value);\n break;\n case 'byte':\n case 'int8':\n this.writeInt8(value);\n break;\n case 'uint16':\n this.writeUint16(value);\n break;\n case 'int16':\n this.writeInt16(value);\n break;\n case 'uint32':\n this.writeUint32(value);\n break;\n case 'int32':\n this.writeInt32(value);\n break;\n case 'float32':\n this.writeFloat32(value);\n break;\n case 'float64':\n this.writeFloat64(value);\n break;\n case 'string':\n this.writeString(value, length);\n break;\n case 'boolean':\n this.writeBoolean(value);\n break;\n case 'bytes':\n this.writeBytes(value);\n break;\n default:\n throw new Error(`Unknown type ${type}`);\n }\n }\n\n readInt() {\n return this.readInt32();\n }\n\n readFloat() {\n return this.readFloat32();\n }\n\n readDouble() {\n return this.readFloat64();\n }\n\n remaining() {\n return this.view.byteLength - this.position;\n }\n\n toUint8Array() {\n return new Uint8Array(this.buffer);\n }\n\n toHex() {\n return ArrayBuffers.toHex(this.buffer);\n }\n\n toBase64() {\n return ArrayBuffers.toBase64(this.buffer);\n }\n}\n\nexport interface TypedValue {\n type:\n | 'byte'\n | 'bytes'\n | 'uint8'\n | 'int8'\n | 'uint16'\n | 'int16'\n | 'uint32'\n | 'int32'\n | 'float32'\n | 'float64'\n | 'string'\n | 'boolean';\n value: any;\n length?: number;\n}\n\nfunction safeNumber(n: bigint) {\n if (n > Number.MAX_SAFE_INTEGER) {\n return n;\n }\n return Number(n);\n}\n"],"names":["isDefined","ArrayBuffers","asBuffer","o","ArrayBuffer","isView","byteLength","buffer","slice","byteOffset","ByteBuffer","position","view","constructor","maxByteLength","DataView","length","resize","willWrite","remaining","newLength","buf","resizable","newBuffer","Uint8Array","set","writeByte","value","setUint8","writeBytes","bytes","len","writeInt8","setInt8","writeUint8","writeInt16","setInt16","writeUint16","setUint16","writeInt32","setInt32","writeUint32","setUint32","writeInt64","setBigInt64","writeUint64","setBigUint64","writeFloat32","setFloat32","writeFloat64","setFloat64","writeBoolean","writeString","encoder","TextEncoder","encode","readByte","getUint8","readBytes","readInt8","getInt8","readUint8","readInt16","getInt16","readUint16","getUint16","readInt32","getInt32","readUint32","getUint32","readInt64","getBigInt64","safeNumber","readUint64","getBigUint64","readFloat32","getFloat32","readFloat64","getFloat64","readBoolean","readString","decoder","TextDecoder","a","idx","indexOf","decode","readHexString","toHex","writeInt24","readInt24","writeZero","writeValue","b","tv","type","Error","readInt","readFloat","readDouble","toUint8Array","toBase64","n","Number","MAX_SAFE_INTEGER"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,SAAS,QAAQ,qBA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BACH,IAAI,CAACmC,YAAY,CAACxB;gBAClB;YACF,KAAK;gBACH,IAAI,CAACE,UAAU,CAACF;gBAChB;YACF;gBACE,MAAM,IAAIqE,MAAM,CAAC,aAAa,EAAED,KAAK,CAAC;QAC1C;IACF;IAEAE,UAAU;QACR,OAAO,IAAI,CAAC/B,SAAS;IACvB;IAEAgC,YAAY;QACV,OAAO,IAAI,CAACvB,WAAW;IACzB;IAEAwB,aAAa;QACX,OAAO,IAAI,CAACtB,WAAW;IACzB;IAEA1D,YAAY;QACV,OAAO,IAAI,CAACP,IAAI,CAACN,UAAU,GAAG,IAAI,CAACK,QAAQ;IAC7C;IAEAyF,eAAe;QACb,OAAO,IAAI5E,WAAW,IAAI,CAACjB,MAAM;IACnC;IAEAiF,QAAQ;QACN,OAAOvF,aAAauF,KAAK,CAAC,IAAI,CAACjF,MAAM;IACvC;IAEA8F,WAAW;QACT,OAAOpG,aAAaoG,QAAQ,CAAC,IAAI,CAAC9F,MAAM;IAC1C;AACF;AAoBA,SAASiE,WAAW8B,CAAS;IAC3B,IAAIA,IAAIC,OAAOC,gBAAgB,EAAE;QAC/B,OAAOF;IACT;IACA,OAAOC,OAAOD;AAChB"}
1
+ {"version":3,"sources":["../../src/io/ByteBuffer.ts"],"sourcesContent":["import { isDefined } from '../langs/isDefined';\nimport { ArrayBuffers } from './ArrayBuffers';\n\ntype AnyBuffer = BufferSource | ArrayBufferLike;\n\nfunction asBuffer(o: AnyBuffer) {\n if (o instanceof ArrayBuffer) {\n return o;\n }\n if (ArrayBuffer.isView(o)) {\n // 保留 offset&length\n if (o.byteLength !== o.buffer.byteLength) {\n // ArrayBuffer 没有 subarray\n // if ('subarray' in o.buffer) {\n // return (o.buffer as any).subarray(o.byteOffset, o.byteOffset + o.byteLength);\n // }\n return o.buffer.slice(o.byteOffset, o.byteOffset + o.byteLength);\n }\n return o.buffer;\n }\n return o;\n}\n\n// function asView(o: AnyBuffer) {\n// if (o instanceof DataView) {\n// return o;\n// }\n// if (ArrayBuffer.isView(o)) {\n// // 不 clone 也能保留 offset&length\n// return new DataView(o.buffer, o.byteOffset, o.byteLength);\n// }\n// return new DataView(o);\n// }\n\n/**\n * @see {@link https://www.egret.uk/docs/egretengine/engine/egret.ByteArray egret.ByteArray}\n * @see {@link https://github.com/protobufjs/bytebuffer.js protobufjs/bytebuffer.js}\n * @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html ByteBuf}\n */\nexport class ByteBuffer {\n position = 0;\n\n #buffer: ArrayBufferLike;\n #view: DataView;\n\n constructor(buffer: AnyBuffer = new ArrayBuffer(0, { maxByteLength: 1024 })) {\n this.#buffer = asBuffer(buffer);\n // NOTE prefer view over buffer, avoid the slice overhead ?\n this.#view = new DataView(this.#buffer);\n }\n\n get view() {\n return this.#view;\n }\n\n get buffer(): ArrayBufferLike {\n return this.#buffer;\n }\n\n set buffer(buffer: AnyBuffer) {\n this.#buffer = asBuffer(buffer);\n this.#view = new DataView(this.#buffer);\n }\n\n get length() {\n return this.view.byteLength;\n }\n\n set length(length: number) {\n this.resize(length);\n }\n\n private willWrite(length: number) {\n if (this.remaining() < length) {\n this.resize(this.length + length);\n }\n }\n\n resize(newLength: number) {\n this.buffer = ArrayBuffers.resize(this.buffer, newLength, Math.ceil(newLength * 1.2));\n }\n\n writeByte(value: number) {\n this.willWrite(1);\n this.view.setUint8(this.position++, value);\n }\n\n writeBytes(bytes: ArrayBufferLike, len: number = bytes.byteLength) {\n this.willWrite(len);\n if (len !== bytes.byteLength) bytes = bytes.slice(0, len);\n new Uint8Array(this.buffer).set(new Uint8Array(bytes), this.position);\n this.position += bytes.byteLength;\n }\n\n writeInt8(value: number) {\n this.willWrite(1);\n this.view.setInt8(this.position, value);\n this.position += 1;\n }\n\n writeUint8(value: number) {\n this.willWrite(1);\n this.view.setUint8(this.position, value);\n this.position += 1;\n }\n\n writeInt16(value: number) {\n this.willWrite(2);\n this.view.setInt16(this.position, value);\n this.position += 2;\n }\n\n writeUint16(value: number) {\n this.willWrite(2);\n this.view.setUint16(this.position, value);\n this.position += 2;\n }\n\n writeInt32(value: number) {\n this.willWrite(4);\n this.view.setInt32(this.position, value);\n this.position += 4;\n }\n\n writeUint32(value: number) {\n this.willWrite(4);\n this.view.setUint32(this.position, value);\n this.position += 4;\n }\n\n writeInt64(value: bigint) {\n this.willWrite(8);\n this.view.setBigInt64(this.position, value);\n this.position += 8;\n }\n\n writeUint64(value: bigint) {\n this.willWrite(8);\n this.view.setBigUint64(this.position, value);\n this.position += 8;\n }\n\n writeFloat32(value: number) {\n this.willWrite(4);\n this.view.setFloat32(this.position, value);\n this.position += 4;\n }\n\n writeFloat64(value: number) {\n this.willWrite(8);\n this.view.setFloat64(this.position, value);\n this.position += 8;\n }\n\n writeBoolean(value: boolean) {\n this.writeByte(value ? 1 : 0);\n }\n\n writeString(value: string): void;\n writeString(value: string, len?: number): void;\n writeString(value: string, len?: number) {\n let encoder = new TextEncoder();\n let bytes = encoder.encode(value);\n this.writeBytes(bytes, len);\n }\n\n readByte() {\n return this.view.getUint8(this.position++);\n }\n\n readBytes(length: number) {\n let bytes = this.buffer.slice(this.position, this.position + length);\n this.position += length;\n return bytes;\n }\n\n readInt8() {\n let value = this.view.getInt8(this.position);\n this.position += 1;\n return value;\n }\n\n readUint8() {\n let value = this.view.getUint8(this.position);\n this.position += 1;\n return value;\n }\n\n readInt16() {\n let value = this.view.getInt16(this.position);\n this.position += 2;\n return value;\n }\n\n readUint16() {\n let value = this.view.getUint16(this.position);\n this.position += 2;\n return value;\n }\n\n readInt32() {\n let value = this.view.getInt32(this.position);\n this.position += 4;\n return value;\n }\n\n readUint32() {\n let value = this.view.getUint32(this.position);\n this.position += 4;\n return value;\n }\n\n readInt64() {\n let value = this.view.getBigInt64(this.position);\n this.position += 8;\n return safeNumber(value);\n }\n\n readUint64() {\n let value = this.view.getBigUint64(this.position);\n this.position += 8;\n return safeNumber(value);\n }\n\n readFloat32() {\n let value = this.view.getFloat32(this.position);\n this.position += 4;\n return value;\n }\n\n readFloat64() {\n let value = this.view.getFloat64(this.position);\n this.position += 8;\n return value;\n }\n\n readBoolean() {\n return this.readByte() === 1;\n }\n\n readString(length: number) {\n let bytes = this.readBytes(length);\n let decoder = new TextDecoder();\n let a = new Uint8Array(bytes);\n let idx = a.indexOf(0);\n if (idx !== -1) {\n bytes = bytes.slice(0, idx);\n }\n return decoder.decode(bytes);\n }\n\n readHexString(length: number) {\n let bytes = this.readBytes(length);\n return ArrayBuffers.toHex(bytes);\n }\n\n writeInt24(value: number) {\n this.writeUint8(value & 0xff);\n this.writeUint16(value >> 8);\n }\n\n readInt24() {\n return this.readUint8() | (this.readUint16() << 8);\n }\n\n writeZero(length: number) {\n this.writeBytes(new Uint8Array(length).buffer);\n }\n\n writeValue(typ: TypedValue['type'], val: TypedValue['value']): void;\n writeValue(tv: TypedValue): void;\n writeValue(a: any, b?: any) {\n const tv: TypedValue = typeof a === 'string' ? { type: a, value: b } : a;\n const { type, value, length } = tv;\n switch (type) {\n case 'uint8':\n this.writeUint8(value);\n break;\n case 'byte':\n case 'int8':\n this.writeInt8(value);\n break;\n case 'uint16':\n this.writeUint16(value);\n break;\n case 'int16':\n this.writeInt16(value);\n break;\n case 'uint32':\n this.writeUint32(value);\n break;\n case 'int32':\n this.writeInt32(value);\n break;\n case 'float32':\n this.writeFloat32(value);\n break;\n case 'float64':\n this.writeFloat64(value);\n break;\n case 'string':\n this.writeString(value, length);\n break;\n case 'boolean':\n this.writeBoolean(value);\n break;\n case 'bytes':\n this.writeBytes(value);\n break;\n default:\n throw new Error(`Unknown type ${type}`);\n }\n }\n\n readInt() {\n return this.readInt32();\n }\n\n readFloat() {\n return this.readFloat32();\n }\n\n readDouble() {\n return this.readFloat64();\n }\n\n remaining() {\n return this.view.byteLength - this.position;\n }\n\n toUint8Array() {\n return new Uint8Array(this.buffer);\n }\n\n toHex() {\n return ArrayBuffers.toHex(this.buffer);\n }\n\n toBase64() {\n return ArrayBuffers.toBase64(this.buffer);\n }\n}\n\nexport interface TypedValue {\n type:\n | 'byte'\n | 'bytes'\n | 'uint8'\n | 'int8'\n | 'uint16'\n | 'int16'\n | 'uint32'\n | 'int32'\n | 'float32'\n | 'float64'\n | 'string'\n | 'boolean';\n value: any;\n length?: number;\n}\n\nfunction safeNumber(n: bigint) {\n if (n > Number.MAX_SAFE_INTEGER) {\n return n;\n }\n return Number(n);\n}\n"],"names":["ArrayBuffers","asBuffer","o","ArrayBuffer","isView","byteLength","buffer","slice","byteOffset","ByteBuffer","position","view","constructor","maxByteLength","DataView","length","resize","willWrite","remaining","newLength","Math","ceil","writeByte","value","setUint8","writeBytes","bytes","len","Uint8Array","set","writeInt8","setInt8","writeUint8","writeInt16","setInt16","writeUint16","setUint16","writeInt32","setInt32","writeUint32","setUint32","writeInt64","setBigInt64","writeUint64","setBigUint64","writeFloat32","setFloat32","writeFloat64","setFloat64","writeBoolean","writeString","encoder","TextEncoder","encode","readByte","getUint8","readBytes","readInt8","getInt8","readUint8","readInt16","getInt16","readUint16","getUint16","readInt32","getInt32","readUint32","getUint32","readInt64","getBigInt64","safeNumber","readUint64","getBigUint64","readFloat32","getFloat32","readFloat64","getFloat64","readBoolean","readString","decoder","TextDecoder","a","idx","indexOf","decode","readHexString","toHex","writeInt24","readInt24","writeZero","writeValue","b","tv","type","Error","readInt","readFloat","readDouble","toUint8Array","toBase64","n","Number","MAX_SAFE_INTEGER"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AACA,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,CAACvB,WAAW;IACzB;IAEAwB,aAAa;QACX,OAAO,IAAI,CAACtB,WAAW;IACzB;IAEAzD,YAAY;QACV,OAAO,IAAI,CAACP,IAAI,CAACN,UAAU,GAAG,IAAI,CAACK,QAAQ;IAC7C;IAEAwF,eAAe;QACb,OAAO,IAAItE,WAAW,IAAI,CAACtB,MAAM;IACnC;IAEAgF,QAAQ;QACN,OAAOtF,aAAasF,KAAK,CAAC,IAAI,CAAChF,MAAM;IACvC;IAEA6F,WAAW;QACT,OAAOnG,aAAamG,QAAQ,CAAC,IAAI,CAAC7F,MAAM;IAC1C;AACF;AAoBA,SAASgE,WAAW8B,CAAS;IAC3B,IAAIA,IAAIC,OAAOC,gBAAgB,EAAE;QAC/B,OAAOF;IACT;IACA,OAAOC,OAAOD;AAChB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wener/utils",
3
- "version": "1.1.46",
3
+ "version": "1.1.47",
4
4
  "type": "module",
5
5
  "description": "Utils for daily use",
6
6
  "repository": {
@@ -157,18 +157,23 @@ export class ArrayBuffers {
157
157
  return this.toUint8Array(ArrayBuffers.from(v, 'hex'));
158
158
  };
159
159
 
160
- static resize = (v: ArrayBufferLike, newByteLength?: number) => {
160
+ static resize = (v: ArrayBufferLike, newByteLength?: number, maxByteLength?: number): ArrayBuffer => {
161
161
  if (newByteLength === undefined || newByteLength === null) {
162
162
  return v;
163
163
  }
164
164
 
165
- if ('resize' in v && (v as any).resizable) {
166
- (v as any).resize(newByteLength);
167
- return v;
165
+ // Chrome 111, Nodejs 20
166
+ if ('resize' in v && typeof v.resize === 'function') {
167
+ if ('resizable' in v && v.resizable) {
168
+ if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {
169
+ v.resize(newByteLength);
170
+ return v;
171
+ }
172
+ }
168
173
  }
169
174
 
170
175
  const old = v;
171
- const newBuf = new ArrayBuffer(newByteLength);
176
+ const newBuf = new ArrayBuffer(newByteLength, { maxByteLength: maxByteLength });
172
177
  const oldView = new Uint8Array(old);
173
178
  const newView = new Uint8Array(newBuf);
174
179
  newView.set(oldView);
@@ -2,13 +2,19 @@ import { assert, test } from 'vitest';
2
2
  import { ByteBuffer } from './ByteBuffer';
3
3
 
4
4
  test('ByteBuffer', async () => {
5
- let buf = new ByteBuffer();
6
- buf.writeString('Hello');
7
- buf.writeString('World');
8
- assert.equal(buf.length, 10);
9
- assert.equal(buf.position, 10);
10
- buf.length = 5;
11
- assert.equal(buf.length, 5);
12
- buf.position = 0;
13
- assert.equal(buf.readString(5), 'Hello');
5
+ {
6
+ let buf = new ByteBuffer();
7
+ buf.writeString('Hello');
8
+ buf.writeString('World');
9
+ assert.equal(buf.length, 10);
10
+ assert.equal(buf.position, 10);
11
+ buf.length = 5;
12
+ assert.equal(buf.length, 5);
13
+ buf.position = 0;
14
+ assert.equal(buf.readString(5), 'Hello');
15
+ }
16
+ {
17
+ let buf = new ByteBuffer();
18
+ buf.writeBytes(new Uint8Array(1025));
19
+ }
14
20
  });
@@ -77,15 +77,7 @@ export class ByteBuffer {
77
77
  }
78
78
 
79
79
  resize(newLength: number) {
80
- // Chrome 111, Nodejs 20
81
- let buf = this.buffer;
82
- if (buf.resize && (!isDefined(buf.resizable) || buf.resizable)) {
83
- buf.resize(newLength);
84
- } else {
85
- let newBuffer = new ArrayBuffer(newLength);
86
- new Uint8Array(newBuffer).set(new Uint8Array(buf));
87
- this.buffer = newBuffer;
88
- }
80
+ this.buffer = ArrayBuffers.resize(this.buffer, newLength, Math.ceil(newLength * 1.2));
89
81
  }
90
82
 
91
83
  writeByte(value: number) {