@wener/utils 1.1.49 → 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.map +1 -1
- package/lib/asyncs/createAsyncIterator.js.map +1 -1
- 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.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.map +1 -1
- 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.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.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.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.map +1 -1
- package/lib/langs/getObjectId.js +1 -1
- package/lib/langs/getObjectId.js.map +1 -1
- 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.map +1 -1
- 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.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/errors/Errors.ts +37 -16
- package/src/io/ArrayBuffers.base64.test.ts +7 -0
- package/src/io/ArrayBuffers.ts +272 -271
- package/src/io/base64.ts +2 -2
- package/src/langs/getObjectId.ts +1 -1
package/lib/io/ArrayBuffers.js
CHANGED
|
@@ -1,150 +1,106 @@
|
|
|
1
1
|
import { classOf } from '../langs/classOf.js';
|
|
2
2
|
import { getGlobalThis } from '../runtime/getGlobalThis.js';
|
|
3
|
-
import {
|
|
3
|
+
import { decodeBase64ToUint8Array, encodeArrayBufferToBase64 } from './base64.js';
|
|
4
4
|
import { isBuffer } from './isBuffer.js';
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
export var ArrayBuffers;
|
|
6
|
+
(function(ArrayBuffers) {
|
|
7
|
+
let nativeBufferAllowed = true;
|
|
8
|
+
let isBufferAvailable;
|
|
9
|
+
function isNativeBufferAvailable() {
|
|
9
10
|
// eslint-disable-next-line no-return-assign
|
|
10
|
-
return
|
|
11
|
+
return isBufferAvailable ??= !getGlobalThis().Buffer?.isPollyfill?.();
|
|
11
12
|
}
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* isNativeBufferAvailable check if the native {@link Buffer} is available
|
|
15
|
+
*/ ArrayBuffers.isNativeBufferAvailable = isNativeBufferAvailable;
|
|
16
|
+
function isNativeBufferAllowed() {
|
|
17
|
+
return Boolean(nativeBufferAllowed && isBufferAvailable);
|
|
14
18
|
}
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
ArrayBuffers.isNativeBufferAllowed = isNativeBufferAllowed;
|
|
20
|
+
function setNativeBufferAllowed(v) {
|
|
21
|
+
nativeBufferAllowed = v;
|
|
17
22
|
}
|
|
18
|
-
|
|
23
|
+
ArrayBuffers.setNativeBufferAllowed = setNativeBufferAllowed;
|
|
24
|
+
function isArrayBuffer(v) {
|
|
19
25
|
return v instanceof ArrayBuffer;
|
|
20
|
-
};
|
|
21
|
-
static toArrayBuffer(v) {
|
|
22
|
-
return v instanceof ArrayBuffer ? v : v.buffer;
|
|
23
26
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if ('toBase64' in Uint8Array.prototype) {
|
|
29
|
-
return this.toUint8Array(v).toBase64();
|
|
30
|
-
}
|
|
31
|
-
if (ArrayBuffers.isNativeBufferAllowed()) {
|
|
32
|
-
return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('base64');
|
|
33
|
-
}
|
|
34
|
-
return encodeArrayBufferToBase64(this.toArrayBuffer(v));
|
|
35
|
-
};
|
|
36
|
-
static toHex = (v)=>{
|
|
37
|
-
if ('toHex' in Uint8Array.prototype) {
|
|
38
|
-
return this.toUint8Array(v).toHex();
|
|
39
|
-
}
|
|
40
|
-
if (ArrayBuffers.isNativeBufferAllowed()) {
|
|
41
|
-
return Buffer.from(ArrayBuffers.asView(Uint8Array, v)).toString('hex');
|
|
42
|
-
}
|
|
43
|
-
return ArrayBuffers.toString(v, 'hex');
|
|
44
|
-
};
|
|
45
|
-
static fromBase64 = (v)=>{
|
|
46
|
-
if ('fromBase64' in Uint8Array) {
|
|
47
|
-
return Uint8Array.fromBase64(v);
|
|
48
|
-
}
|
|
49
|
-
if (ArrayBuffers.isNativeBufferAllowed()) {
|
|
50
|
-
return Buffer.from(v, 'base64');
|
|
51
|
-
}
|
|
52
|
-
return this.toUint8Array(decodeBase64ToArrayBuffer(v));
|
|
53
|
-
};
|
|
54
|
-
static fromHex = (v)=>{
|
|
55
|
-
if ('fromHex' in Uint8Array) {
|
|
56
|
-
return Uint8Array.fromHex(v);
|
|
57
|
-
}
|
|
58
|
-
return this.toUint8Array(ArrayBuffers.from(v, 'hex'));
|
|
59
|
-
};
|
|
60
|
-
static resize = (v, newByteLength, maxByteLength)=>{
|
|
61
|
-
if (newByteLength === undefined || newByteLength === null) {
|
|
62
|
-
return v;
|
|
63
|
-
}
|
|
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
|
-
}
|
|
72
|
-
}
|
|
73
|
-
const old = v;
|
|
74
|
-
const newBuf = new ArrayBuffer(newByteLength, {
|
|
75
|
-
maxByteLength: maxByteLength
|
|
76
|
-
});
|
|
77
|
-
const oldView = new Uint8Array(old);
|
|
78
|
-
const newView = new Uint8Array(newBuf);
|
|
79
|
-
newView.set(oldView);
|
|
80
|
-
return newBuf;
|
|
81
|
-
};
|
|
82
|
-
static slice = (o, start, end)=>{
|
|
27
|
+
/**
|
|
28
|
+
* isArrayBuffer check if the given value is an {@link ArrayBuffer}
|
|
29
|
+
*/ ArrayBuffers.isArrayBuffer = isArrayBuffer;
|
|
30
|
+
function slice(o, start, end) {
|
|
83
31
|
// NodeJS Buffer slice is not the same as UInt8Array slice
|
|
84
32
|
// https://nodejs.org/api/buffer.html#bufslicestart-end
|
|
85
33
|
if (isBuffer(o)) {
|
|
86
34
|
return Uint8Array.prototype.slice.call(o, start, end);
|
|
87
35
|
}
|
|
88
36
|
return o.slice(start, end);
|
|
89
|
-
}
|
|
90
|
-
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* slice the given view with the given offset and length, will handle the {@link Buffer} as well
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://nodejs.org/api/buffer.html#bufslicestart-end Buffer.slice}
|
|
42
|
+
*/ ArrayBuffers.slice = slice;
|
|
43
|
+
function asView(TypedArray, v, byteOffset, byteLength) {
|
|
91
44
|
if (v instanceof TypedArray && (byteOffset ?? 0) === 0 && byteLength === undefined) {
|
|
92
45
|
return v;
|
|
93
46
|
}
|
|
94
47
|
if (ArrayBuffer.isView(v) || isBuffer(v)) {
|
|
95
|
-
if (
|
|
48
|
+
if (isNativeBufferAllowed() && TypedArray === Buffer) {
|
|
96
49
|
// new Buffer() is deprecated
|
|
97
50
|
return Buffer.from(v.buffer, byteOffset, byteLength);
|
|
98
51
|
}
|
|
99
52
|
return new TypedArray(v.buffer, v.byteOffset + (byteOffset ?? 0), byteLength ?? v.byteLength);
|
|
100
53
|
}
|
|
101
54
|
return new TypedArray(v, byteOffset, byteLength);
|
|
102
|
-
}
|
|
103
|
-
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* asView convert the given value to given {@link TypedArray} view
|
|
58
|
+
*
|
|
59
|
+
* TypedArray can be {@link Buffer}, will avoid copy
|
|
60
|
+
*/ ArrayBuffers.asView = asView;
|
|
61
|
+
function toString(source, encoding = 'utf8') {
|
|
104
62
|
// 'ascii' 'utf16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'
|
|
105
|
-
if (typeof
|
|
63
|
+
if (typeof source === 'string') {
|
|
106
64
|
switch(encoding){
|
|
107
65
|
case 'base64':
|
|
108
|
-
return btoa(
|
|
66
|
+
return btoa(source);
|
|
109
67
|
case 'utf-8':
|
|
110
68
|
case 'utf8':
|
|
111
|
-
return
|
|
69
|
+
return source;
|
|
112
70
|
default:
|
|
113
71
|
throw new Error(`[ArrayBuffers.toString] Unsupported encoding for string: ${encoding}`);
|
|
114
72
|
}
|
|
115
73
|
}
|
|
116
|
-
|
|
117
|
-
|
|
74
|
+
let u8 = asView(Uint8Array, source);
|
|
75
|
+
if (isNativeBufferAllowed()) {
|
|
76
|
+
return Buffer.from(u8).toString(encoding);
|
|
118
77
|
}
|
|
119
78
|
// reference
|
|
120
79
|
// https://github.com/feross/buffer/blob/master/index.js
|
|
121
80
|
switch(encoding){
|
|
122
81
|
case 'hex':
|
|
123
82
|
{
|
|
124
|
-
const view = ArrayBuffers.asView(Uint8Array, buf);
|
|
125
83
|
return [
|
|
126
|
-
...
|
|
84
|
+
...u8
|
|
127
85
|
].map((b)=>hexLookupTable[b]).join('');
|
|
128
86
|
}
|
|
129
87
|
case 'base64':
|
|
130
88
|
{
|
|
131
|
-
return encodeArrayBufferToBase64(
|
|
89
|
+
return encodeArrayBufferToBase64(u8);
|
|
132
90
|
}
|
|
133
91
|
case 'utf8':
|
|
134
92
|
// falls through
|
|
135
93
|
case 'utf-8':
|
|
136
|
-
return new TextDecoder().decode(
|
|
94
|
+
return new TextDecoder().decode(source);
|
|
137
95
|
case 'ascii':
|
|
138
96
|
{
|
|
139
|
-
|
|
140
|
-
return String.fromCharCode(...view.map((v)=>v & 0x7f));
|
|
97
|
+
return String.fromCharCode(...u8.map((v)=>v & 0x7f));
|
|
141
98
|
}
|
|
142
99
|
case 'latin1':
|
|
143
100
|
// falls through
|
|
144
101
|
case 'binary':
|
|
145
102
|
{
|
|
146
|
-
|
|
147
|
-
return String.fromCharCode(...view);
|
|
103
|
+
return String.fromCharCode(...u8);
|
|
148
104
|
}
|
|
149
105
|
case 'ucs2':
|
|
150
106
|
// falls through
|
|
@@ -153,109 +109,90 @@ export class ArrayBuffers {
|
|
|
153
109
|
// falls through
|
|
154
110
|
case 'utf16le':
|
|
155
111
|
{
|
|
156
|
-
const view = ArrayBuffers.asView(Uint8Array, buf);
|
|
157
112
|
let res = '';
|
|
158
113
|
// If length is odd, the last 8 bits must be ignored (same as node.js)
|
|
159
|
-
for(let i = 0; i <
|
|
160
|
-
res += String.fromCharCode(
|
|
114
|
+
for(let i = 0; i < u8.length - 1; i += 2){
|
|
115
|
+
res += String.fromCharCode(u8[i] + u8[i + 1] * 256);
|
|
161
116
|
}
|
|
162
117
|
return res;
|
|
163
118
|
}
|
|
164
119
|
default:
|
|
165
120
|
throw new Error(`[ArrayBuffers.toString] Unknown encoding: ${encoding}`);
|
|
166
121
|
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* toString convert the given {@link BufferSource} to string
|
|
125
|
+
*/ ArrayBuffers.toString = toString;
|
|
126
|
+
function normalizeEncoding(encoding) {
|
|
127
|
+
switch(encoding?.toLowerCase()){
|
|
128
|
+
case 'utf-8':
|
|
129
|
+
case 'utf8':
|
|
130
|
+
return 'utf8';
|
|
131
|
+
case 'utf-16le':
|
|
132
|
+
case 'ucs2':
|
|
133
|
+
case 'ucs-2':
|
|
134
|
+
return 'utf16le';
|
|
135
|
+
case 'hex':
|
|
136
|
+
case 'ascii':
|
|
137
|
+
case 'latin1':
|
|
138
|
+
case 'binary':
|
|
139
|
+
case 'base64':
|
|
140
|
+
case 'utf16le':
|
|
141
|
+
return encoding;
|
|
142
|
+
default:
|
|
143
|
+
return undefined;
|
|
179
144
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
145
|
+
}
|
|
146
|
+
function isEncoding(v) {
|
|
147
|
+
return normalizeEncoding(v) !== undefined;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Returns true if encoding is the name of a supported character encoding, or false otherwise.
|
|
151
|
+
*/ ArrayBuffers.isEncoding = isEncoding;
|
|
152
|
+
function toJSON(v, reviver) {
|
|
153
|
+
return JSON.parse(toString(v), reviver);
|
|
154
|
+
}
|
|
155
|
+
ArrayBuffers.toJSON = toJSON;
|
|
156
|
+
function from(src, encoding = 'utf8', view) {
|
|
157
|
+
if (!src) {
|
|
158
|
+
return new (view || ArrayBuffer)(0);
|
|
185
159
|
}
|
|
186
|
-
if (
|
|
187
|
-
return
|
|
160
|
+
if (isBufferSource(src)) {
|
|
161
|
+
return view ? asView(view, src) : src;
|
|
188
162
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
163
|
+
// Array<number> | Iterable<number>
|
|
164
|
+
if (typeof src !== 'string' && isIterable(src) || Array.isArray(src)) {
|
|
165
|
+
return (view || Uint8Array).from(src);
|
|
166
|
+
}
|
|
167
|
+
if (view) {
|
|
168
|
+
return asView(view, from(src, encoding));
|
|
169
|
+
}
|
|
170
|
+
if (typeof src === 'string') {
|
|
171
|
+
// is string
|
|
172
|
+
if (isNativeBufferAllowed()) {
|
|
173
|
+
return Buffer.from(src, encoding);
|
|
192
174
|
}
|
|
193
175
|
switch(encoding){
|
|
194
176
|
case 'utf-8':
|
|
195
177
|
// falls through
|
|
196
178
|
case 'utf8':
|
|
197
|
-
return new TextEncoder().encode(
|
|
179
|
+
return new TextEncoder().encode(src).buffer;
|
|
198
180
|
case 'base64':
|
|
199
181
|
// replaceAll need higher version of nodejs
|
|
200
|
-
return decodeBase64ToArrayBuffer(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));
|
|
182
|
+
// return decodeBase64ToArrayBuffer(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));
|
|
183
|
+
return fromBase64(src);
|
|
201
184
|
case 'hex':
|
|
202
|
-
return new Uint8Array(v.match(/.{1,2}/g)
|
|
185
|
+
// return new Uint8Array(v.match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))).buffer;
|
|
186
|
+
return fromHex(src);
|
|
203
187
|
default:
|
|
204
|
-
throw new Error(`
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (v instanceof ArrayBuffer) {
|
|
208
|
-
return v;
|
|
209
|
-
}
|
|
210
|
-
// lost length
|
|
211
|
-
if (ArrayBuffer.isView(v) || isBuffer(v)) {
|
|
212
|
-
if (v.byteOffset !== 0) {
|
|
213
|
-
// return v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength)
|
|
214
|
-
throw new Error('ArrayBuffers.from do not support view with offset');
|
|
188
|
+
throw new Error(`ArrayBuffers.from unsupported encoding: ${encoding}`);
|
|
215
189
|
}
|
|
216
|
-
return v.buffer;
|
|
217
|
-
}
|
|
218
|
-
if (Array.isArray(v)) {
|
|
219
|
-
return new Uint8Array(v);
|
|
220
190
|
}
|
|
221
|
-
const type = classOf(
|
|
191
|
+
const type = classOf(src);
|
|
222
192
|
throw new TypeError(`ArrayBuffers.from unsupported type ${type}`);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
case 'hex':
|
|
227
|
-
case 'utf8':
|
|
228
|
-
case 'utf-8':
|
|
229
|
-
case 'ascii':
|
|
230
|
-
case 'latin1':
|
|
231
|
-
case 'binary':
|
|
232
|
-
case 'base64':
|
|
233
|
-
case 'ucs2':
|
|
234
|
-
case 'ucs-2':
|
|
235
|
-
case 'utf16le':
|
|
236
|
-
// case 'utf-16le':
|
|
237
|
-
return true;
|
|
238
|
-
default:
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
// static truncate = (a: ArrayBufferLike, len?: number) => {
|
|
243
|
-
// if (len === 0) {
|
|
244
|
-
// return new ArrayBuffer(0);
|
|
245
|
-
// }
|
|
246
|
-
// if (!len) {
|
|
247
|
-
// return a;
|
|
248
|
-
// }
|
|
249
|
-
// if (a.byteLength > len) {
|
|
250
|
-
// return a.slice(0, len);
|
|
251
|
-
// } else if (a.byteLength < len) {
|
|
252
|
-
// let b = new Uint8Array(len);
|
|
253
|
-
// b.set(new Uint8Array(a));
|
|
254
|
-
// return b;
|
|
255
|
-
// }
|
|
256
|
-
// return a;
|
|
257
|
-
// };
|
|
258
|
-
static concat = (buffers, result, offset = 0)=>{
|
|
193
|
+
}
|
|
194
|
+
ArrayBuffers.from = from;
|
|
195
|
+
function concat(buffers, result, offset = 0) {
|
|
259
196
|
// https://stackoverflow.com/questions/10786128/appending-arraybuffers
|
|
260
197
|
const length = buffers.reduce((a, b)=>a + b.byteLength, 0);
|
|
261
198
|
const r = result ? new Uint8Array(result) : new Uint8Array(length);
|
|
@@ -273,8 +210,109 @@ export class ArrayBuffers {
|
|
|
273
210
|
offset += buffer.byteLength;
|
|
274
211
|
}
|
|
275
212
|
return r.buffer;
|
|
276
|
-
}
|
|
277
|
-
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* concat the given {@link BufferSource} to a new {@link ArrayBuffer}
|
|
216
|
+
*/ ArrayBuffers.concat = concat;
|
|
217
|
+
function fromBase64(v) {
|
|
218
|
+
if ('fromBase64' in Uint8Array) {
|
|
219
|
+
return Uint8Array.fromBase64(v);
|
|
220
|
+
}
|
|
221
|
+
if (isNativeBufferAllowed()) {
|
|
222
|
+
return Buffer.from(v, 'base64');
|
|
223
|
+
}
|
|
224
|
+
return decodeBase64ToUint8Array(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));
|
|
225
|
+
}
|
|
226
|
+
ArrayBuffers.fromBase64 = fromBase64;
|
|
227
|
+
function fromHex(v) {
|
|
228
|
+
if ('fromHex' in Uint8Array) {
|
|
229
|
+
return Uint8Array.fromHex(v);
|
|
230
|
+
}
|
|
231
|
+
if (isNativeBufferAllowed()) {
|
|
232
|
+
return Buffer.from(v, 'hex');
|
|
233
|
+
}
|
|
234
|
+
return new Uint8Array(v.match(/.{1,2}/g).map((byte)=>parseInt(byte, 16)));
|
|
235
|
+
}
|
|
236
|
+
ArrayBuffers.fromHex = fromHex;
|
|
237
|
+
function toBase64(source) {
|
|
238
|
+
if (typeof source === 'string') {
|
|
239
|
+
source = new TextEncoder().encode(source);
|
|
240
|
+
}
|
|
241
|
+
if ('toBase64' in Uint8Array.prototype) {
|
|
242
|
+
return toUint8Array(source).toBase64();
|
|
243
|
+
}
|
|
244
|
+
if (isNativeBufferAllowed()) {
|
|
245
|
+
return Buffer.from(asView(Uint8Array, source)).toString('base64');
|
|
246
|
+
}
|
|
247
|
+
return encodeArrayBufferToBase64(toArrayBuffer(source));
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* toBase64 convert the given {@link BufferSource} to base64 string
|
|
251
|
+
* @param source if string, will be encoded as utf8
|
|
252
|
+
*/ ArrayBuffers.toBase64 = toBase64;
|
|
253
|
+
function toHex(v) {
|
|
254
|
+
if ('toHex' in Uint8Array.prototype) {
|
|
255
|
+
return toUint8Array(v).toHex();
|
|
256
|
+
}
|
|
257
|
+
if (isNativeBufferAllowed()) {
|
|
258
|
+
return Buffer.from(asView(Uint8Array, v)).toString('hex');
|
|
259
|
+
}
|
|
260
|
+
return toString(v, 'hex');
|
|
261
|
+
}
|
|
262
|
+
ArrayBuffers.toHex = toHex;
|
|
263
|
+
function resize(v, newByteLength, maxByteLength) {
|
|
264
|
+
if (newByteLength === undefined || newByteLength === null) {
|
|
265
|
+
return v;
|
|
266
|
+
}
|
|
267
|
+
// Chrome 111, Nodejs 20
|
|
268
|
+
if ('resize' in v && typeof v.resize === 'function') {
|
|
269
|
+
if ('resizable' in v && v.resizable) {
|
|
270
|
+
if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {
|
|
271
|
+
v.resize(newByteLength);
|
|
272
|
+
return v;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
const old = v;
|
|
277
|
+
const newBuf = new ArrayBuffer(newByteLength, {
|
|
278
|
+
maxByteLength: maxByteLength
|
|
279
|
+
});
|
|
280
|
+
const oldView = new Uint8Array(old);
|
|
281
|
+
const newView = new Uint8Array(newBuf);
|
|
282
|
+
newView.set(oldView);
|
|
283
|
+
return newBuf;
|
|
284
|
+
}
|
|
285
|
+
ArrayBuffers.resize = resize;
|
|
286
|
+
function toArrayBuffer(v) {
|
|
287
|
+
if (v instanceof ArrayBuffer) {
|
|
288
|
+
return v;
|
|
289
|
+
}
|
|
290
|
+
if (ArrayBuffer.isView(v)) {
|
|
291
|
+
if (v.byteOffset > 0) {
|
|
292
|
+
throw new Error('ArrayBuffers.toArrayBuffer do not support view with offset');
|
|
293
|
+
}
|
|
294
|
+
return v.buffer;
|
|
295
|
+
}
|
|
296
|
+
throw new Error(`ArrayBuffers.toArrayBuffer unsupported type ${classOf(v)}`);
|
|
297
|
+
}
|
|
298
|
+
ArrayBuffers.toArrayBuffer = toArrayBuffer;
|
|
299
|
+
function toUint8Array(v) {
|
|
300
|
+
return asView(Uint8Array, v);
|
|
301
|
+
}
|
|
302
|
+
ArrayBuffers.toUint8Array = toUint8Array;
|
|
303
|
+
function alloc(size, fill, encoding) {
|
|
304
|
+
if (fill !== undefined) {
|
|
305
|
+
if (typeof fill === 'number') {
|
|
306
|
+
return new Uint8Array(size).fill(fill);
|
|
307
|
+
}
|
|
308
|
+
// as cast
|
|
309
|
+
// https://stackoverflow.com/questions/73994091
|
|
310
|
+
return asView(Uint8Array, from(fill, encoding)).slice(0, size);
|
|
311
|
+
}
|
|
312
|
+
return new ArrayBuffer(size);
|
|
313
|
+
}
|
|
314
|
+
ArrayBuffers.alloc = alloc;
|
|
315
|
+
})(ArrayBuffers || (ArrayBuffers = {}));
|
|
278
316
|
const hexLookupTable = function() {
|
|
279
317
|
const alphabet = '0123456789abcdef';
|
|
280
318
|
const table = new Array(256);
|
|
@@ -286,5 +324,11 @@ const hexLookupTable = function() {
|
|
|
286
324
|
}
|
|
287
325
|
return table;
|
|
288
326
|
}();
|
|
327
|
+
function isIterable(obj) {
|
|
328
|
+
return typeof obj?.[Symbol.iterator] === 'function';
|
|
329
|
+
}
|
|
330
|
+
function isBufferSource(v) {
|
|
331
|
+
return ArrayBuffer.isView(v) || v instanceof ArrayBuffer;
|
|
332
|
+
}
|
|
289
333
|
|
|
290
334
|
//# sourceMappingURL=ArrayBuffers.js.map
|
|
@@ -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: 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 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): 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\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"],"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,EAA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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,uBAAuBC,CAAU;QAC/CR,sBAAsBQ;IACxB;iBAFgBD,yBAAAA;IAOT,SAASE,cAAcD,CAAM;QAClC,OAAOA,aAAaE;IACtB;IALA;;GAEC,gBACeD,gBAAAA;IAST,SAASE,MAA4BC,CAAI,EAAEC,K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AAIK,MAAM,CAAC,4CAA4C,EAAEpC,QAAQc,IAAI;IAC7E;iBAXgBqE,gBAAAA;IAaT,SAASD,aAAapE,CAAe;QAC1C,OAAOU,OAAOH,YAAYP;IAC5B;iBAFgBoE,eAAAA;IAIT,SAASW,MAAMC,IAAY,EAAEC,IAAsB,EAAE7D,QAA+B;QACzF,IAAI6D,SAASnE,WAAW;YACtB,IAAI,OAAOmE,SAAS,UAAU;gBAC5B,OAAO,IAAI1E,WAAWyE,MAAMC,IAAI,CAACA;YACnC;YACA,UAAU;YACV,+CAA+C;YAC/C,OAAOvE,OAAOH,YAAYS,KAAKiE,MAAM7D,WAAWjB,KAAK,CAAC,GAAG6E;QAC3D;QACA,OAAO,IAAI9E,YAAY8E;IACzB;iBAVgBD,QAAAA;AAWlB,GApXiBxF,iBAAAA;AAqYjB,MAAMmC,iBAAiB,AAAC;IACtB,MAAMwD,WAAW;IACjB,MAAMC,QAAQ,IAAIrC,MAAM;IACxB,IAAK,IAAIb,IAAI,GAAGA,IAAI,IAAI,EAAEA,EAAG;QAC3B,MAAMmD,MAAMnD,IAAI;QAChB,IAAK,IAAIoD,IAAI,GAAGA,IAAI,IAAI,EAAEA,EAAG;YAC3BF,KAAK,CAACC,MAAMC,EAAE,GAAGH,QAAQ,CAACjD,EAAE,GAAGiD,QAAQ,CAACG,EAAE;QAC5C;IACF;IACA,OAAOF;AACT;AA8BA,SAAStC,WAAWyC,GAAQ;IAC1B,OAAO,OAAOA,KAAK,CAACC,OAAOC,QAAQ,CAAC,KAAK;AAC3C;AAEA,SAAS5C,eAAe5C,CAAM;IAC5B,OAAOE,YAAYa,MAAM,CAACf,MAAMA,aAAaE;AAC/C"}
|
package/lib/io/Buffer.js.map
CHANGED
|
@@ -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"}
|