@wener/utils 1.1.48 → 1.1.50
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.
- package/lib/arrays/MaybeArray.js.map +1 -1
- package/lib/arrays/arrayFromAsync.js.map +1 -1
- package/lib/asyncs/AsyncInterval.js.map +1 -1
- package/lib/asyncs/MaybePromise.js.map +1 -1
- package/lib/asyncs/Promises.js +9 -1
- package/lib/asyncs/Promises.js.map +1 -1
- package/lib/asyncs/createAsyncIterator.js.map +1 -1
- package/lib/asyncs/createLazyPromise.js +52 -51
- package/lib/asyncs/createLazyPromise.js.map +1 -1
- package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/generatorOfStream.js.map +1 -1
- package/lib/asyncs/isIterator.js.map +1 -1
- package/lib/asyncs/isPromise.js.map +1 -1
- package/lib/asyncs/isThenable.js.map +1 -1
- package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
- package/lib/asyncs/promiseOfCallback.js.map +1 -1
- package/lib/asyncs/timeout.js +4 -4
- package/lib/asyncs/timeout.js.map +1 -1
- package/lib/browsers/copy.js.map +1 -1
- package/lib/browsers/download.js.map +1 -1
- package/lib/browsers/getFileFromDataTransfer.js.map +1 -1
- package/lib/browsers/loaders.js.map +1 -1
- package/lib/cn/division/DivisionCode.js.map +1 -1
- package/lib/cn/division/binarySearch.js.map +1 -1
- package/lib/cn/formatChineseAmount.js.map +1 -1
- package/lib/cn/id/Mod11.js.map +1 -1
- package/lib/cn/id/ResidentIdNumber.js.map +1 -1
- package/lib/cn/id/types.js.map +1 -1
- package/lib/cn/index.js.map +1 -1
- package/lib/cn/parseChineseNumber.js.map +1 -1
- package/lib/cn/pinyin/cartesianProduct.js.map +1 -1
- package/lib/cn/pinyin/loader.js.map +1 -1
- package/lib/cn/pinyin/preload.js.map +1 -1
- package/lib/cn/pinyin/toPinyinPure.js.map +1 -1
- package/lib/cn/pinyin/transform.js.map +1 -1
- package/lib/cn/types.js +3 -0
- package/lib/cn/types.js.map +1 -0
- package/lib/cn/uscc/Mod31.js.map +1 -1
- package/lib/cn/uscc/USCC.js.map +1 -1
- package/lib/cn/uscc/isUSCC.js.map +1 -1
- package/lib/crypto/base.js.map +1 -1
- package/lib/crypto/getNodeCrypto.js +1 -0
- package/lib/crypto/getNodeCrypto.js.map +1 -1
- package/lib/crypto/getRandomValues.js.map +1 -1
- package/lib/crypto/hashing.js.map +1 -1
- package/lib/crypto/md5.bench.js.map +1 -1
- package/lib/crypto/md5.d.js.map +1 -1
- package/lib/crypto/md5.js.map +1 -1
- package/lib/crypto/pem/pem.js.map +1 -1
- package/lib/crypto/randomUUID.js.map +1 -1
- package/lib/crypto/randomUUIDv7.js.map +1 -1
- package/lib/crypto/ulid.js.map +1 -1
- package/lib/emitter/types.js.map +1 -1
- package/lib/errors/Errors.js +38 -17
- package/lib/errors/Errors.js.map +1 -1
- package/lib/fetch/HttpStatus.js.map +1 -1
- package/lib/fetch/createFetchWith.js.map +1 -1
- package/lib/fetch/createFetchWithLogging.js.map +1 -1
- package/lib/fetch/createFetchWithRetry.js.map +1 -1
- package/lib/fetch/dumpRequest.js.map +1 -1
- package/lib/fetch/dumpResponse.js.map +1 -1
- package/lib/fetch/http.types.js.map +1 -1
- package/lib/fetch/index.js.map +1 -1
- package/lib/fetch/types.js.map +1 -1
- package/lib/i18n/createTranslate.js.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/io/AbstractEncoding.js.map +1 -1
- package/lib/io/ArrayBuffer.test-d.js.map +1 -1
- package/lib/io/ArrayBuffers.js +217 -173
- package/lib/io/ArrayBuffers.js.map +1 -1
- package/lib/io/Buffer.js.map +1 -1
- package/lib/io/ByteBuffer.js +233 -34
- package/lib/io/ByteBuffer.js.map +1 -1
- package/lib/io/base64.js +2 -2
- package/lib/io/base64.js.map +1 -1
- package/lib/io/dump.js.map +1 -1
- package/lib/io/isBuffer.js.map +1 -1
- package/lib/io/isTransferable.js.map +1 -1
- package/lib/io/parseDataUri.js.map +1 -1
- package/lib/langs/MaybeFunction.js.map +1 -1
- package/lib/langs/classOf.js.map +1 -1
- package/lib/langs/deepEqual.js.map +1 -1
- package/lib/langs/deepFreeze.js.map +1 -1
- package/lib/langs/getGlobalStates.js +17 -0
- package/lib/langs/getGlobalStates.js.map +1 -0
- package/lib/langs/getObjectId.js +18 -0
- package/lib/langs/getObjectId.js.map +1 -0
- package/lib/langs/isClass.js.map +1 -1
- package/lib/langs/isDefined.js.map +1 -1
- package/lib/langs/isEmptyObject.js.map +1 -1
- package/lib/langs/isFunction.js.map +1 -1
- package/lib/langs/isNullish.js +5 -0
- package/lib/langs/isNullish.js.map +1 -0
- package/lib/langs/isPlainObject.js.map +1 -1
- package/lib/langs/memoize.js.map +1 -1
- package/lib/langs/mixin.js.map +1 -1
- package/lib/langs/parseBoolean.js.map +1 -1
- package/lib/langs/shallowClone.js.map +1 -1
- package/lib/langs/shallowEqual.js.map +1 -1
- package/lib/libs/ms.js.map +1 -1
- package/lib/logging/Logger.js.map +1 -1
- package/lib/logging/createChildLogger.js.map +1 -1
- package/lib/logging/createLogger.js.map +1 -1
- package/lib/logging/createNoopLogger.js.map +1 -1
- package/lib/logging/slog.js.map +1 -1
- package/lib/maths/clamp.js +7 -6
- package/lib/maths/clamp.js.map +1 -1
- package/lib/maths/random.js.map +1 -1
- package/lib/mitt/index.js.map +1 -1
- package/lib/modules/isModule.js.map +1 -1
- package/lib/modules/parseModuleId.js.map +1 -1
- package/lib/objects/computeIfAbsent.js.map +1 -1
- package/lib/objects/get.js.map +1 -1
- package/lib/objects/get.test-d.js.map +1 -1
- package/lib/objects/merge/index.js.map +1 -1
- package/lib/objects/merge/isMergeableObject.js.map +1 -1
- package/lib/objects/merge/merge.js.map +1 -1
- package/lib/objects/parseObjectPath.js.map +1 -1
- package/lib/objects/set.js.map +1 -1
- package/lib/runtime/AsyncCloser.js.map +1 -1
- package/lib/runtime/Closer.js.map +1 -1
- package/lib/runtime/getGlobalThis.js.map +1 -1
- package/lib/runtime/structuredClone.js.map +1 -1
- package/lib/schema/typebox/index.js.map +1 -1
- package/lib/schema/typebox/typebox.js.map +1 -1
- package/lib/scripts/getGenerateContext.js.map +1 -1
- package/lib/server/crypto/md5.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxy.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxyByNodeFetch.js.map +1 -1
- package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
- package/lib/server/getPackageDir.js.map +1 -1
- package/lib/server/index.js.map +1 -1
- package/lib/server/jsdom.js.map +1 -1
- package/lib/server/node-fetch.js.map +1 -1
- package/lib/server/polyfill/polyfillBrowser.js.map +1 -1
- package/lib/server/polyfill/polyfillCrypto.js.map +1 -1
- package/lib/server/polyfill/polyfillFetch.js.map +1 -1
- package/lib/server/polyfill/polyfillJsDom.js.map +1 -1
- package/lib/server/polyfill/polyfillWebSocket.js.map +1 -1
- package/lib/server/ws.js.map +1 -1
- package/lib/strings/camelCase.js.map +1 -1
- package/lib/strings/formatBytes.js.map +1 -1
- package/lib/strings/renderTemplate.js.map +1 -1
- package/lib/typedoc.js.map +1 -1
- package/lib/types.d.js.map +1 -1
- package/lib/validations/asserts.js.map +1 -1
- package/lib/validations/isUUID.js.map +1 -1
- package/lib/validations/parseTimestamp.js.map +1 -1
- package/package.json +4 -4
- package/src/asyncs/Promises.ts +12 -2
- package/src/asyncs/createLazyPromise.test.ts +52 -13
- package/src/asyncs/createLazyPromise.ts +66 -61
- package/src/asyncs/firstOfAsyncIterator.ts +1 -1
- package/src/asyncs/nextOfAsyncIterator.ts +1 -1
- package/src/asyncs/timeout.ts +4 -4
- package/src/cn/types.ts +11 -0
- package/src/crypto/getNodeCrypto.ts +1 -0
- package/src/errors/Errors.ts +37 -16
- package/src/fetch/createFetchWith.ts +1 -1
- package/src/fetch/createFetchWithRetry.ts +1 -1
- package/src/index.ts +2 -0
- package/src/io/ArrayBuffers.base64.test.ts +7 -0
- package/src/io/ArrayBuffers.ts +276 -271
- package/src/io/ByteBuffer.test.ts +26 -14
- package/src/io/ByteBuffer.ts +270 -38
- package/src/io/base64.ts +2 -2
- package/src/langs/getGlobalStates.ts +21 -0
- package/src/langs/getObjectId.ts +20 -0
- package/src/langs/isNullish.ts +3 -0
- package/src/langs/mixin.test.ts +4 -0
- package/src/logging/slog.ts +1 -1
- package/src/maths/clamp.test.ts +5 -1
- package/src/maths/clamp.ts +7 -7
- package/src/objects/merge/merge.test.ts +1 -1
- package/src/schema/typebox/typebox.ts +1 -1
- package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
- package/tsconfig.json +4 -1
- package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
- package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
- package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
- package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
- package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
- package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
- package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
- package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
- package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/index.js +0 -29
- package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
- package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
- package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
- package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
- package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
- package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
- package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
- package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
- package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
- package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
- package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
- package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
- package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
- package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
- package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
- package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
- package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
- package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
- package/lib/schema/typebox/gen/index.js +0 -3
- package/lib/schema/typebox/gen/index.js.map +0 -1
- package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
- package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
- package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
- package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
- package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
- package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
- package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
- package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
- package/src/schema/typebox/gen/codegen/index.ts +0 -29
- package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
- package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
- package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
- package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
- package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
- package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
- package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
- package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
- package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
- package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
- package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
- package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
- package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
- package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
- package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
- package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
- package/src/schema/typebox/gen/gen.test.ts +0 -12
- package/src/schema/typebox/gen/index.ts +0 -1
- /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
|
@@ -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, 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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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qC,iBAAiB,AAAC;IACtB,MAAMoC,WAAW;IACjB,MAAMC,QAAQ,IAAIf,MAAM;IACxB,IAAK,IAAIf,IAAI,GAAGA,IAAI,IAAI,EAAEA,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 { decodeBase64ToUint8Array, 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-resizablearraybuffer\n */\nexport namespace ArrayBuffers {\n /*\nUint8Array to/from base64 and hex\nStage 3\nUint8Array.fromBase64, Uint8Array.prototype.toBase64\nUint8Array.fromHex, Uint8Array.prototype.toHex\nhttps://github.com/tc39/proposal-arraybuffer-base64\n */\n\n /*\nIn-Place Resizable and Growable ArrayBuffers\nStage 4\nChrome 111, Nodejs 20, Safari 16.4\n\nSharedArrayBuffer & ArrayBuffer\nconstructor(byteLength, {maxByteLength})\nprototype.resize(newByteLength)\nprototype.slice(start, end)\nprototype.{resizable,maxByteLength}\nhttps://github.com/tc39/proposal-resizablearraybuffer\n */\n\n export type BinaryStringEncoding =\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\n let nativeBufferAllowed: boolean = true;\n let isBufferAvailable: undefined | boolean;\n\n /**\n * isNativeBufferAvailable check if the native {@link Buffer} is available\n */\n export function isNativeBufferAvailable(): boolean {\n // eslint-disable-next-line no-return-assign\n return (isBufferAvailable ??= !(getGlobalThis().Buffer as any)?.isPollyfill?.());\n }\n\n export function isNativeBufferAllowed(): boolean {\n return Boolean(nativeBufferAllowed && isBufferAvailable);\n }\n\n export function setNativeBufferAllowed(v: boolean): void {\n nativeBufferAllowed = v;\n }\n\n /**\n * isArrayBuffer check if the given value is an {@link ArrayBuffer}\n */\n export function isArrayBuffer(v: any): v is ArrayBuffer {\n return v instanceof ArrayBuffer;\n }\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 export function slice<T extends TypedArray>(o: T, start?: number, end?: number): T {\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) as T;\n }\n return o.slice(start, end) as T;\n }\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 export function asView<C extends ArrayBufferViewConstructor<unknown>>(\n TypedArray: C,\n v: BufferSource,\n byteOffset?: number,\n byteLength?: number,\n ): InstanceType<C> {\n if (v instanceof TypedArray && (byteOffset ?? 0) === 0 && byteLength === undefined) {\n return v as InstanceType<C>;\n }\n if (ArrayBuffer.isView(v) || isBuffer(v)) {\n if (isNativeBufferAllowed() && (TypedArray as any) === Buffer) {\n // new Buffer() is deprecated\n return Buffer.from(v.buffer, byteOffset, byteLength) as InstanceType<C>;\n }\n return new TypedArray(v.buffer, v.byteOffset + (byteOffset ?? 0), byteLength ?? v.byteLength) as InstanceType<C>;\n }\n return new TypedArray(v, byteOffset, byteLength) as InstanceType<C>;\n }\n\n /**\n * toString convert the given {@link BufferSource} to string\n */\n export function toString(source: BufferSource | string, encoding: BinaryStringEncoding = 'utf8'): string {\n // 'ascii' 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'\n if (typeof source === 'string') {\n switch (encoding) {\n case 'base64':\n return btoa(source);\n case 'utf-8':\n case 'utf8':\n return source;\n default:\n throw new Error(`[ArrayBuffers.toString] Unsupported encoding for string: ${encoding}`);\n }\n }\n let u8 = asView(Uint8Array, source);\n if (isNativeBufferAllowed()) {\n return Buffer.from(u8).toString(encoding);\n }\n // reference\n // https://github.com/feross/buffer/blob/master/index.js\n switch (encoding) {\n case 'hex': {\n return [...u8].map((b) => hexLookupTable[b]).join('');\n }\n case 'base64': {\n return encodeArrayBufferToBase64(u8);\n }\n case 'utf8':\n // falls through\n case 'utf-8':\n return new TextDecoder().decode(source);\n case 'ascii': {\n return String.fromCharCode(...u8.map((v) => v & 0x7f));\n }\n case 'latin1':\n // falls through\n case 'binary': {\n return String.fromCharCode(...u8);\n }\n case 'ucs2':\n // falls through\n case 'ucs-2':\n // case 'utf-16le':\n // falls through\n case 'utf16le': {\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 < u8.length - 1; i += 2) {\n res += String.fromCharCode(u8[i] + u8[i + 1] * 256);\n }\n return res;\n }\n default:\n throw new Error(`[ArrayBuffers.toString] Unknown encoding: ${encoding}`);\n }\n }\n\n function normalizeEncoding(encoding: string | undefined) {\n switch (encoding?.toLowerCase()) {\n case 'utf-8':\n case 'utf8':\n return 'utf8';\n case 'utf-16le':\n case 'ucs2':\n case 'ucs-2':\n return 'utf16le';\n case 'hex':\n case 'ascii':\n case 'latin1':\n case 'binary':\n case 'base64':\n case 'utf16le':\n return encoding;\n default:\n return undefined;\n }\n }\n\n /**\n * Returns true if encoding is the name of a supported character encoding, or false otherwise.\n */\n export function isEncoding(v?: string): v is BinaryStringEncoding {\n return normalizeEncoding(v) !== undefined;\n }\n\n export function toJSON<T = any>(v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any): T {\n return JSON.parse(toString(v), reviver);\n }\n\n /**\n * from convert the given value to {@link ArrayBuffer} like\n */\n export function from(\n src: string | BufferSource | Array<number> | Iterable<number>,\n encoding?: BinaryStringEncoding,\n ): ArrayBuffer | TypedArray;\n /**\n * from convert the given value to {@link TypedArray}\n */\n export function from<C extends ArrayBufferViewConstructor<unknown>>(\n src: string | BufferSource | Array<number> | Iterable<number>,\n encoding: BinaryStringEncoding,\n TypedArray: C,\n ): InstanceType<C>;\n export function from(\n src: string | BufferSource | Array<number> | Iterable<number>,\n encoding: BinaryStringEncoding = 'utf8',\n view?: any,\n ): any {\n if (!src) {\n return new (view || ArrayBuffer)(0);\n }\n if (isBufferSource(src)) {\n return view ? asView(view, src) : src;\n }\n // Array<number> | Iterable<number>\n if ((typeof src !== 'string' && isIterable(src)) || Array.isArray(src)) {\n return (view || Uint8Array).from(src);\n }\n if (view) {\n return asView(view, from(src, encoding));\n }\n if (typeof src === 'string') {\n // is string\n if (isNativeBufferAllowed()) {\n return Buffer.from(src, encoding);\n }\n switch (encoding) {\n case 'utf-8':\n // falls through\n case 'utf8':\n return new TextEncoder().encode(src).buffer;\n case 'base64':\n // replaceAll need higher version of nodejs\n // return decodeBase64ToArrayBuffer(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));\n return fromBase64(src);\n case 'hex':\n // return new Uint8Array(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))).buffer;\n return fromHex(src);\n default:\n throw new Error(`ArrayBuffers.from unsupported encoding: ${encoding}`);\n }\n }\n\n const type = classOf(src);\n throw new TypeError(`ArrayBuffers.from unsupported type ${type}`);\n }\n\n /**\n * concat the given {@link BufferSource} to a new {@link ArrayBuffer}\n */\n export function concat(buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0): ArrayBuffer {\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 export function fromBase64(v: string): Uint8Array {\n if ('fromBase64' in Uint8Array) {\n return Uint8Array.fromBase64(v);\n }\n if (isNativeBufferAllowed()) {\n return Buffer.from(v, 'base64');\n }\n return decodeBase64ToUint8Array(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));\n }\n\n export function fromHex(v: string): Uint8Array {\n if ('fromHex' in Uint8Array) {\n return Uint8Array.fromHex(v);\n }\n if (isNativeBufferAllowed()) {\n return Buffer.from(v, 'hex');\n }\n return new Uint8Array(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16)));\n }\n\n /**\n * toBase64 convert the given {@link BufferSource} to base64 string\n * @param source if string, will be encoded as utf8\n */\n export function toBase64(source: BufferSource | string): string {\n if (typeof source === 'string') {\n source = new TextEncoder().encode(source);\n }\n if ('toBase64' in Uint8Array.prototype) {\n return toUint8Array(source).toBase64();\n }\n if (isNativeBufferAllowed()) {\n return Buffer.from(asView(Uint8Array, source)).toString('base64');\n }\n return encodeArrayBufferToBase64(toArrayBuffer(source));\n }\n\n export function toHex(v: BufferSource): string {\n if ('toHex' in Uint8Array.prototype) {\n return toUint8Array(v).toHex();\n }\n if (isNativeBufferAllowed()) {\n return Buffer.from(asView(Uint8Array, v)).toString('hex');\n }\n return toString(v, 'hex');\n }\n\n export function resize(v: ArrayBuffer, 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 as ArrayBuffer;\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 export function toArrayBuffer(v: BufferSource): ArrayBuffer {\n if (v instanceof ArrayBuffer) {\n return v;\n }\n if (ArrayBuffer.isView(v)) {\n if (v.byteOffset > 0) {\n throw new Error('ArrayBuffers.toArrayBuffer do not support view with offset');\n }\n return v.buffer;\n }\n throw new Error(`ArrayBuffers.toArrayBuffer unsupported type ${classOf(v)}`);\n }\n\n export function toUint8Array(v: BufferSource): Uint8Array {\n return asView(Uint8Array, v);\n }\n\n export function alloc(size: number, fill?: string | number, encoding?: BinaryStringEncoding): ArrayBuffer {\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 asView(Uint8Array, from(fill, encoding)).slice(0, size);\n }\n return new ArrayBuffer(size);\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 ArrayBufferConstructor {\n new (byteLength: number, opts?: { maxByteLength?: number }): ArrayBuffer;\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\nfunction isIterable(obj: any): obj is Iterable<any> {\n return typeof obj?.[Symbol.iterator] === 'function';\n}\n\nfunction isBufferSource(v: any): v is BufferSource {\n return ArrayBuffer.isView(v) || v instanceof ArrayBuffer;\n}\n"],"names":["classOf","getGlobalThis","decodeBase64ToUint8Array","encodeArrayBufferToBase64","isBuffer","ArrayBuffers","nativeBufferAllowed","isBufferAvailable","isNativeBufferAvailable","Buffer","isPollyfill","isNativeBufferAllowed","Boolean","setNativeBufferAllowed","v","isArrayBuffer","ArrayBuffer","slice","o","start","end","Uint8Array","prototype","call","asView","TypedArray","byteOffset","byteLength","undefined","isView","from","buffer","toString","source","encoding","btoa","Error","u8","map","b","hexLookupTable","join","TextDecoder","decode","String","fromCharCode","res","i","length","normalizeEncoding","toLowerCase","isEncoding","toJSON","reviver","JSON","parse","src","view","isBufferSource","isIterable","Array","isArray","TextEncoder","encode","fromBase64","fromHex","type","TypeError","concat","buffers","result","offset","reduce","a","r","n","set","replace","match","byte","parseInt","toBase64","toUint8Array","toArrayBuffer","toHex","resize","newByteLength","maxByteLength","resizable","old","newBuf","oldView","newView","alloc","size","fill","alphabet","table","i16","j","obj","Symbol","iterator"],"mappings":"AAAA,SAASA,OAAO,QAAQ,mBAAmB;AAC3C,SAASC,aAAa,QAAQ,2BAA2B;AACzD,SAASC,wBAAwB,EAAEC,yBAAyB,QAAQ,WAAW;AAC/E,SAASC,QAAQ,QAAQ,aAAa;;UAOrBC;IAoCf,IAAIC,sBAA+B;IACnC,IAAIC;IAKG,SAASC;QACd,4CAA4C;QAC5C,OAAQD,sBAAsB,CAAEN,gBAAgBQ,MAAM,EAAUC;IAClE;IANA;;GAEC,gBACeF,0BAAAA;IAKT,SAASG;QACd,OAAOC,QAAQN,uBAAuBC;IACxC;iBAFgBI,wBAAAA;IAIT,SAASE,uBAAu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HANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/io/Buffer.ts"],"sourcesContent":["import { ArrayBuffers } from './ArrayBuffers';\nimport { isBuffer } from './isBuffer';\n\n/**\n * Buffer is a polyfill version of NodeJS Buffer\n */\nexport class Buffer extends Uint8Array {\n // constructor(buffer: ArrayBufferLike, byteOffset?: number, length?: number) {\n // super(buffer, byteOffset, length);\n // }\n\n static get isPolyfill() {\n return true;\n }\n\n static isBuffer(v: any): v is Buffer {\n return v instanceof Buffer || isBuffer(v);\n }\n\n static from(array: string | BufferSource | ArrayLike<number> | Iterable<number>, arg2?: any): Buffer {\n // todo mapfn\n return new Buffer(ArrayBuffers.from(array, arg2) as ArrayBuffer);\n }\n\n static isEncoding = ArrayBuffers.isEncoding;\n\n toString(encoding?: string): string {\n return ArrayBuffers.toString(this, encoding as any);\n }\n}\n"],"names":["ArrayBuffers","isBuffer","Buffer","Uint8Array","isPolyfill","v","from","array","arg2","isEncoding","toString","encoding"],"
|
|
1
|
+
{"version":3,"sources":["../../src/io/Buffer.ts"],"sourcesContent":["import { ArrayBuffers } from './ArrayBuffers';\nimport { isBuffer } from './isBuffer';\n\n/**\n * Buffer is a polyfill version of NodeJS Buffer\n */\nexport class Buffer extends Uint8Array {\n // constructor(buffer: ArrayBufferLike, byteOffset?: number, length?: number) {\n // super(buffer, byteOffset, length);\n // }\n\n static get isPolyfill() {\n return true;\n }\n\n static isBuffer(v: any): v is Buffer {\n return v instanceof Buffer || isBuffer(v);\n }\n\n static from(array: string | BufferSource | ArrayLike<number> | Iterable<number>, arg2?: any): Buffer {\n // todo mapfn\n return new Buffer(ArrayBuffers.from(array, arg2) as ArrayBuffer);\n }\n\n static isEncoding = ArrayBuffers.isEncoding;\n\n toString(encoding?: string): string {\n return ArrayBuffers.toString(this, encoding as any);\n }\n}\n"],"names":["ArrayBuffers","isBuffer","Buffer","Uint8Array","isPolyfill","v","from","array","arg2","isEncoding","toString","encoding"],"mappings":"AAAA,SAASA,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,QAAQ,QAAQ,aAAa;AAEtC;;CAEC,GACD,OAAO,MAAMC,eAAeC;IAC1B,+EAA+E;IAC/E,uCAAuC;IACvC,IAAI;IAEJ,WAAWC,aAAa;QACtB,OAAO;IACT;IAEA,OAAOH,SAASI,CAAM,EAAe;QACnC,OAAOA,aAAaH,UAAUD,SAASI;IACzC;IAEA,OAAOC,KAAKC,KAAmE,EAAEC,IAAU,EAAU;QACnG,aAAa;QACb,OAAO,IAAIN,OAAOF,aAAaM,IAAI,CAACC,OAAOC;IAC7C;IAEA,OAAOC,aAAaT,aAAaS,UAAU,CAAC;IAE5CC,SAASC,QAAiB,EAAU;QAClC,OAAOX,aAAaU,QAAQ,CAAC,IAAI,EAAEC;IACrC;AACF"}
|
package/lib/io/ByteBuffer.js
CHANGED
|
@@ -29,17 +29,35 @@ function asBuffer(o) {
|
|
|
29
29
|
/**
|
|
30
30
|
* @see {@link https://www.egret.uk/docs/egretengine/engine/egret.ByteArray egret.ByteArray}
|
|
31
31
|
* @see {@link https://github.com/protobufjs/bytebuffer.js protobufjs/bytebuffer.js}
|
|
32
|
-
* @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html ByteBuf}
|
|
32
|
+
* @see {@link https://netty.io/4.1/api/io/netty/buffer/ByteBuf.html io.netty.buffer.ByteBuf}
|
|
33
|
+
* @see {@link https://nodejs.org/api/buffer.html node:buffer}
|
|
34
|
+
* @see {@link https://www.postgresql.org/docs/current/datatype.html PostgreSQL Data Types}
|
|
33
35
|
*/ export class ByteBuffer {
|
|
34
36
|
position = 0;
|
|
35
37
|
#buffer;
|
|
36
38
|
#view;
|
|
39
|
+
#bytes;
|
|
40
|
+
// #endian: 'big' | 'little' = 'big';
|
|
41
|
+
#bigEndian = true;
|
|
37
42
|
constructor(buffer = new ArrayBuffer(0, {
|
|
38
43
|
maxByteLength: 1024
|
|
39
44
|
})){
|
|
40
45
|
this.#buffer = asBuffer(buffer);
|
|
41
46
|
// NOTE prefer view over buffer, avoid the slice overhead ?
|
|
42
47
|
this.#view = new DataView(this.#buffer);
|
|
48
|
+
this.#bytes = new Uint8Array(this.#buffer);
|
|
49
|
+
}
|
|
50
|
+
get littleEndian() {
|
|
51
|
+
return !this.#bigEndian;
|
|
52
|
+
}
|
|
53
|
+
set littleEndian(v) {
|
|
54
|
+
this.#bigEndian = !v;
|
|
55
|
+
}
|
|
56
|
+
get bigEndian() {
|
|
57
|
+
return this.#bigEndian;
|
|
58
|
+
}
|
|
59
|
+
set bigEndian(v) {
|
|
60
|
+
this.#bigEndian = v;
|
|
43
61
|
}
|
|
44
62
|
get view() {
|
|
45
63
|
return this.#view;
|
|
@@ -50,6 +68,10 @@ function asBuffer(o) {
|
|
|
50
68
|
set buffer(buffer) {
|
|
51
69
|
this.#buffer = asBuffer(buffer);
|
|
52
70
|
this.#view = new DataView(this.#buffer);
|
|
71
|
+
this.#bytes = new Uint8Array(this.#buffer);
|
|
72
|
+
}
|
|
73
|
+
get bytes() {
|
|
74
|
+
return this.#bytes;
|
|
53
75
|
}
|
|
54
76
|
get length() {
|
|
55
77
|
return this.view.byteLength;
|
|
@@ -57,12 +79,8 @@ function asBuffer(o) {
|
|
|
57
79
|
set length(length) {
|
|
58
80
|
this.resize(length);
|
|
59
81
|
}
|
|
60
|
-
willWrite(length) {
|
|
61
|
-
if (this.remaining() < length) {
|
|
62
|
-
this.resize(this.position + length);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
82
|
resize(newLength) {
|
|
83
|
+
// 1.2 for buffer growth
|
|
66
84
|
this.buffer = ArrayBuffers.resize(this.buffer, newLength, Math.ceil(newLength * 1.2));
|
|
67
85
|
}
|
|
68
86
|
writeByte(value) {
|
|
@@ -71,8 +89,12 @@ function asBuffer(o) {
|
|
|
71
89
|
}
|
|
72
90
|
writeBytes(bytes, len = bytes.byteLength) {
|
|
73
91
|
this.willWrite(len);
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
// ensure len first
|
|
93
|
+
this.bytes.set(new Uint8Array(bytes).subarray(0, len), this.position);
|
|
94
|
+
if (len > bytes.byteLength) {
|
|
95
|
+
// fill zero
|
|
96
|
+
this.bytes.fill(0, this.position + bytes.byteLength, this.position + len);
|
|
97
|
+
}
|
|
76
98
|
this.position += len;
|
|
77
99
|
}
|
|
78
100
|
writeInt8(value) {
|
|
@@ -87,52 +109,71 @@ function asBuffer(o) {
|
|
|
87
109
|
}
|
|
88
110
|
writeInt16(value) {
|
|
89
111
|
this.willWrite(2);
|
|
90
|
-
this.view.setInt16(this.position, value);
|
|
112
|
+
this.view.setInt16(this.position, value, this.littleEndian);
|
|
91
113
|
this.position += 2;
|
|
92
114
|
}
|
|
93
115
|
writeUint16(value) {
|
|
94
116
|
this.willWrite(2);
|
|
95
|
-
this.view.setUint16(this.position, value);
|
|
117
|
+
this.view.setUint16(this.position, value, this.littleEndian);
|
|
96
118
|
this.position += 2;
|
|
97
119
|
}
|
|
120
|
+
writeUint24(value) {
|
|
121
|
+
this.willWrite(3);
|
|
122
|
+
this.view.setUint8(this.position, value & 0xff);
|
|
123
|
+
this.view.setUint16(this.position + 1, value >> 8, this.littleEndian);
|
|
124
|
+
this.position += 3;
|
|
125
|
+
}
|
|
98
126
|
writeInt32(value) {
|
|
99
127
|
this.willWrite(4);
|
|
100
|
-
this.view.setInt32(this.position, value);
|
|
128
|
+
this.view.setInt32(this.position, value, this.littleEndian);
|
|
101
129
|
this.position += 4;
|
|
102
130
|
}
|
|
103
131
|
writeUint32(value) {
|
|
104
132
|
this.willWrite(4);
|
|
105
|
-
this.view.setUint32(this.position, value);
|
|
133
|
+
this.view.setUint32(this.position, value, this.littleEndian);
|
|
106
134
|
this.position += 4;
|
|
107
135
|
}
|
|
108
136
|
writeInt64(value) {
|
|
109
137
|
this.willWrite(8);
|
|
110
|
-
this.view.setBigInt64(this.position, value);
|
|
138
|
+
this.view.setBigInt64(this.position, bigintOf(value), this.littleEndian);
|
|
111
139
|
this.position += 8;
|
|
112
140
|
}
|
|
113
141
|
writeUint64(value) {
|
|
114
142
|
this.willWrite(8);
|
|
115
|
-
this.view.setBigUint64(this.position, value);
|
|
143
|
+
this.view.setBigUint64(this.position, bigintOf(value), this.littleEndian);
|
|
116
144
|
this.position += 8;
|
|
117
145
|
}
|
|
118
146
|
writeFloat32(value) {
|
|
119
147
|
this.willWrite(4);
|
|
120
|
-
this.view.setFloat32(this.position, value);
|
|
148
|
+
this.view.setFloat32(this.position, value, this.littleEndian);
|
|
121
149
|
this.position += 4;
|
|
122
150
|
}
|
|
123
151
|
writeFloat64(value) {
|
|
124
152
|
this.willWrite(8);
|
|
125
|
-
this.view.setFloat64(this.position, value);
|
|
153
|
+
this.view.setFloat64(this.position, value, this.littleEndian);
|
|
126
154
|
this.position += 8;
|
|
127
155
|
}
|
|
128
156
|
writeBoolean(value) {
|
|
129
157
|
this.writeByte(value ? 1 : 0);
|
|
130
158
|
}
|
|
131
159
|
writeString(value, len) {
|
|
132
|
-
let
|
|
133
|
-
let bytes = encoder.encode(value);
|
|
160
|
+
let bytes = this.encodeText(value);
|
|
134
161
|
this.writeBytes(bytes, len);
|
|
135
162
|
}
|
|
163
|
+
readSizeString(n) {
|
|
164
|
+
return this.readString(requireNumber(this.readUint(n)));
|
|
165
|
+
}
|
|
166
|
+
writeSizeString(value, n) {
|
|
167
|
+
let out = this.encodeText(value);
|
|
168
|
+
this.writeUint(out.length, n);
|
|
169
|
+
this.writeBytes(out);
|
|
170
|
+
}
|
|
171
|
+
encodeText(value) {
|
|
172
|
+
return new TextEncoder().encode(value);
|
|
173
|
+
}
|
|
174
|
+
decodeText(bytes) {
|
|
175
|
+
return new TextDecoder().decode(bytes);
|
|
176
|
+
}
|
|
136
177
|
readByte() {
|
|
137
178
|
return this.view.getUint8(this.position++);
|
|
138
179
|
}
|
|
@@ -151,72 +192,101 @@ function asBuffer(o) {
|
|
|
151
192
|
this.position += 1;
|
|
152
193
|
return value;
|
|
153
194
|
}
|
|
195
|
+
readUnsignedByte() {
|
|
196
|
+
return this.readUint8();
|
|
197
|
+
}
|
|
198
|
+
readThrough(end) {
|
|
199
|
+
// readUntil 不包含 end
|
|
200
|
+
let idx = this.bytes.indexOf(end, this.position);
|
|
201
|
+
if (idx === -1) {
|
|
202
|
+
throw new Error(`Byte ${end} not found starting from position ${this.position}`);
|
|
203
|
+
}
|
|
204
|
+
let bytes = this.buffer.slice(this.position, idx + 1);
|
|
205
|
+
this.position = idx + 1;
|
|
206
|
+
return bytes;
|
|
207
|
+
}
|
|
154
208
|
readInt16() {
|
|
155
|
-
let value = this.view.getInt16(this.position);
|
|
209
|
+
let value = this.view.getInt16(this.position, this.littleEndian);
|
|
156
210
|
this.position += 2;
|
|
157
211
|
return value;
|
|
158
212
|
}
|
|
159
213
|
readUint16() {
|
|
160
|
-
let value = this.view.getUint16(this.position);
|
|
214
|
+
let value = this.view.getUint16(this.position, this.littleEndian);
|
|
161
215
|
this.position += 2;
|
|
162
216
|
return value;
|
|
163
217
|
}
|
|
164
218
|
readInt32() {
|
|
165
|
-
let value = this.view.getInt32(this.position);
|
|
219
|
+
let value = this.view.getInt32(this.position, this.littleEndian);
|
|
166
220
|
this.position += 4;
|
|
167
221
|
return value;
|
|
168
222
|
}
|
|
169
223
|
readUint32() {
|
|
170
|
-
let value = this.view.getUint32(this.position);
|
|
224
|
+
let value = this.view.getUint32(this.position, this.littleEndian);
|
|
171
225
|
this.position += 4;
|
|
172
226
|
return value;
|
|
173
227
|
}
|
|
174
228
|
readInt64() {
|
|
175
|
-
let value = this.view.getBigInt64(this.position);
|
|
229
|
+
let value = this.view.getBigInt64(this.position, this.littleEndian);
|
|
176
230
|
this.position += 8;
|
|
177
231
|
return safeNumber(value);
|
|
178
232
|
}
|
|
179
233
|
readUint64() {
|
|
180
|
-
let value = this.view.getBigUint64(this.position);
|
|
234
|
+
let value = this.view.getBigUint64(this.position, this.littleEndian);
|
|
181
235
|
this.position += 8;
|
|
182
236
|
return safeNumber(value);
|
|
183
237
|
}
|
|
184
238
|
readFloat32() {
|
|
185
|
-
let value = this.view.getFloat32(this.position);
|
|
239
|
+
let value = this.view.getFloat32(this.position, this.littleEndian);
|
|
186
240
|
this.position += 4;
|
|
187
241
|
return value;
|
|
188
242
|
}
|
|
189
243
|
readFloat64() {
|
|
190
|
-
let value = this.view.getFloat64(this.position);
|
|
244
|
+
let value = this.view.getFloat64(this.position, this.littleEndian);
|
|
191
245
|
this.position += 8;
|
|
192
246
|
return value;
|
|
193
247
|
}
|
|
194
248
|
readBoolean() {
|
|
249
|
+
// or !== 0?
|
|
195
250
|
return this.readByte() === 1;
|
|
196
251
|
}
|
|
197
252
|
readString(length) {
|
|
198
253
|
let bytes = this.readBytes(length);
|
|
199
|
-
let decoder = new TextDecoder();
|
|
200
254
|
let a = new Uint8Array(bytes);
|
|
201
255
|
let idx = a.indexOf(0);
|
|
202
256
|
if (idx !== -1) {
|
|
203
257
|
bytes = bytes.slice(0, idx);
|
|
204
258
|
}
|
|
205
|
-
return
|
|
259
|
+
return this.decodeText(bytes);
|
|
206
260
|
}
|
|
207
261
|
readHexString(length) {
|
|
208
262
|
let bytes = this.readBytes(length);
|
|
209
263
|
return ArrayBuffers.toHex(bytes);
|
|
210
264
|
}
|
|
265
|
+
writeHexString(value, len) {
|
|
266
|
+
this.writeBytes(ArrayBuffers.fromHex(value), len);
|
|
267
|
+
}
|
|
211
268
|
writeInt24(value) {
|
|
269
|
+
// fixme byte order
|
|
212
270
|
this.writeUint8(value & 0xff);
|
|
213
271
|
this.writeUint16(value >> 8);
|
|
214
272
|
}
|
|
215
273
|
readInt24() {
|
|
216
|
-
|
|
274
|
+
// fixme recheck
|
|
275
|
+
let value = this.readUint24();
|
|
276
|
+
if (value & 0x800000) {
|
|
277
|
+
// If the sign bit is set, extend the sign
|
|
278
|
+
value |= 0xff000000;
|
|
279
|
+
}
|
|
280
|
+
return value;
|
|
281
|
+
}
|
|
282
|
+
readUint24() {
|
|
283
|
+
// fixme recheck
|
|
284
|
+
return this.readUint8() | this.readUint8() << 8 | this.readUint8() << 16;
|
|
217
285
|
}
|
|
218
286
|
writeZero(length) {
|
|
219
|
-
this.
|
|
287
|
+
this.willWrite(length);
|
|
288
|
+
this.bytes.fill(0, this.position, this.position + length);
|
|
289
|
+
this.position += length;
|
|
220
290
|
}
|
|
221
291
|
writeValue(a, b) {
|
|
222
292
|
const tv = typeof a === 'string' ? {
|
|
@@ -238,16 +308,26 @@ function asBuffer(o) {
|
|
|
238
308
|
case 'int16':
|
|
239
309
|
this.writeInt16(value);
|
|
240
310
|
break;
|
|
311
|
+
case 'uint24':
|
|
312
|
+
this.writeUint24(value);
|
|
313
|
+
break;
|
|
314
|
+
case 'int24':
|
|
315
|
+
this.writeInt24(value);
|
|
316
|
+
break;
|
|
241
317
|
case 'uint32':
|
|
318
|
+
case 'uint':
|
|
242
319
|
this.writeUint32(value);
|
|
243
320
|
break;
|
|
244
321
|
case 'int32':
|
|
322
|
+
case 'int':
|
|
245
323
|
this.writeInt32(value);
|
|
246
324
|
break;
|
|
247
325
|
case 'float32':
|
|
326
|
+
case 'float':
|
|
248
327
|
this.writeFloat32(value);
|
|
249
328
|
break;
|
|
250
329
|
case 'float64':
|
|
330
|
+
case 'double':
|
|
251
331
|
this.writeFloat64(value);
|
|
252
332
|
break;
|
|
253
333
|
case 'string':
|
|
@@ -263,18 +343,120 @@ function asBuffer(o) {
|
|
|
263
343
|
throw new Error(`Unknown type ${type}`);
|
|
264
344
|
}
|
|
265
345
|
}
|
|
266
|
-
|
|
267
|
-
return this.
|
|
346
|
+
readUnsignedShort() {
|
|
347
|
+
return this.readUint16();
|
|
348
|
+
}
|
|
349
|
+
readShort() {
|
|
350
|
+
return this.readInt16();
|
|
351
|
+
}
|
|
352
|
+
readInt(bytesOrBits = 32) {
|
|
353
|
+
switch(bytesOrBits){
|
|
354
|
+
case 1:
|
|
355
|
+
case 8:
|
|
356
|
+
return this.readInt8();
|
|
357
|
+
case 2:
|
|
358
|
+
case 16:
|
|
359
|
+
return this.readInt16();
|
|
360
|
+
case 3:
|
|
361
|
+
case 24:
|
|
362
|
+
return this.readInt24();
|
|
363
|
+
case 4:
|
|
364
|
+
case 32:
|
|
365
|
+
return this.readInt32();
|
|
366
|
+
// skip 8 byte for bits
|
|
367
|
+
case 64:
|
|
368
|
+
return this.readInt64();
|
|
369
|
+
default:
|
|
370
|
+
throw new Error(`Unknown int${bytesOrBits}`);
|
|
371
|
+
}
|
|
268
372
|
}
|
|
269
|
-
|
|
270
|
-
|
|
373
|
+
readUint(bytesOrBits = 32) {
|
|
374
|
+
switch(bytesOrBits){
|
|
375
|
+
case 1:
|
|
376
|
+
case 8:
|
|
377
|
+
return this.readUint8();
|
|
378
|
+
case 2:
|
|
379
|
+
case 16:
|
|
380
|
+
return this.readUint16();
|
|
381
|
+
case 3:
|
|
382
|
+
case 24:
|
|
383
|
+
return this.readUint24();
|
|
384
|
+
case 4:
|
|
385
|
+
case 32:
|
|
386
|
+
return this.readUint32();
|
|
387
|
+
// skip 8 byte for bits
|
|
388
|
+
case 64:
|
|
389
|
+
return this.readUint64();
|
|
390
|
+
default:
|
|
391
|
+
throw new Error(`Unexpected int size: ${bytesOrBits}`);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
writeUint(value, bytesOrBits = 32) {
|
|
395
|
+
switch(bytesOrBits){
|
|
396
|
+
case 1:
|
|
397
|
+
case 8:
|
|
398
|
+
this.writeUint8(value);
|
|
399
|
+
break;
|
|
400
|
+
case 2:
|
|
401
|
+
case 16:
|
|
402
|
+
this.writeUint16(value);
|
|
403
|
+
break;
|
|
404
|
+
case 3:
|
|
405
|
+
case 24:
|
|
406
|
+
this.writeUint24(value);
|
|
407
|
+
break;
|
|
408
|
+
case 4:
|
|
409
|
+
case 32:
|
|
410
|
+
this.writeUint32(value);
|
|
411
|
+
break;
|
|
412
|
+
case 64:
|
|
413
|
+
this.writeUint64(value);
|
|
414
|
+
break;
|
|
415
|
+
default:
|
|
416
|
+
throw new Error(`Unexpected uint size: ${bytesOrBits}`);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
readFloat(n = 32) {
|
|
420
|
+
switch(n){
|
|
421
|
+
case 4:
|
|
422
|
+
case 32:
|
|
423
|
+
return this.readFloat32();
|
|
424
|
+
case 8:
|
|
425
|
+
case 64:
|
|
426
|
+
return this.readFloat64();
|
|
427
|
+
default:
|
|
428
|
+
throw new Error(`Unexpected float size: ${n}`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
writeFloat(value, n = 32) {
|
|
432
|
+
switch(n){
|
|
433
|
+
case 4:
|
|
434
|
+
case 32:
|
|
435
|
+
this.writeFloat32(value);
|
|
436
|
+
break;
|
|
437
|
+
case 8:
|
|
438
|
+
case 64:
|
|
439
|
+
this.writeFloat64(value);
|
|
440
|
+
break;
|
|
441
|
+
default:
|
|
442
|
+
throw new Error(`Unexpected float size: ${n}`);
|
|
443
|
+
}
|
|
271
444
|
}
|
|
272
445
|
readDouble() {
|
|
273
446
|
return this.readFloat64();
|
|
274
447
|
}
|
|
448
|
+
writeDouble(value) {
|
|
449
|
+
this.writeFloat64(value);
|
|
450
|
+
}
|
|
275
451
|
remaining() {
|
|
276
452
|
return this.length - this.position;
|
|
277
453
|
}
|
|
454
|
+
hasRemaining() {
|
|
455
|
+
return this.remaining() > 0;
|
|
456
|
+
}
|
|
457
|
+
readRemaining() {
|
|
458
|
+
return this.readBytes(this.remaining());
|
|
459
|
+
}
|
|
278
460
|
toUint8Array() {
|
|
279
461
|
return new Uint8Array(this.buffer);
|
|
280
462
|
}
|
|
@@ -284,6 +466,11 @@ function asBuffer(o) {
|
|
|
284
466
|
toBase64() {
|
|
285
467
|
return ArrayBuffers.toBase64(this.buffer);
|
|
286
468
|
}
|
|
469
|
+
willWrite(length) {
|
|
470
|
+
if (this.remaining() < length) {
|
|
471
|
+
this.resize(this.position + length);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
287
474
|
}
|
|
288
475
|
function safeNumber(n) {
|
|
289
476
|
if (n > Number.MAX_SAFE_INTEGER) {
|
|
@@ -291,5 +478,17 @@ function safeNumber(n) {
|
|
|
291
478
|
}
|
|
292
479
|
return Number(n);
|
|
293
480
|
}
|
|
481
|
+
function bigintOf(n) {
|
|
482
|
+
if (typeof n === 'bigint') {
|
|
483
|
+
return n;
|
|
484
|
+
}
|
|
485
|
+
return BigInt(n);
|
|
486
|
+
}
|
|
487
|
+
function requireNumber(n) {
|
|
488
|
+
if (typeof n !== 'number') {
|
|
489
|
+
throw new Error(`Expected number, got ${n}`);
|
|
490
|
+
}
|
|
491
|
+
return n;
|
|
492
|
+
}
|
|
294
493
|
|
|
295
494
|
//# sourceMappingURL=ByteBuffer.js.map
|