@wener/utils 1.1.53 → 1.1.56

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.
Files changed (68) hide show
  1. package/lib/arrays/arrayFromAsync.js +11 -3
  2. package/lib/asyncs/AsyncInterval.js +11 -3
  3. package/lib/asyncs/Promises.js +6 -5
  4. package/lib/asyncs/createAsyncIterator.js +11 -3
  5. package/lib/asyncs/createLazyPromise.test.js +11 -3
  6. package/lib/asyncs/generatorOfStream.js +11 -3
  7. package/lib/browsers/download.js +11 -3
  8. package/lib/browsers/loaders.js +11 -3
  9. package/lib/crypto/hashing.js +11 -3
  10. package/lib/crypto/hashing.test.js +11 -3
  11. package/lib/crypto/pem/pem.js +1 -1
  12. package/lib/errors/Errors.js +201 -2
  13. package/lib/fetch/createFetchWith.js +11 -3
  14. package/lib/fetch/dumpRequest.js +12 -4
  15. package/lib/fetch/dumpRequest.test.js +11 -3
  16. package/lib/fetch/dumpResponse.js +11 -3
  17. package/lib/fetch/dumpResponse.test.js +11 -3
  18. package/lib/index.js +9 -8
  19. package/lib/io/ArrayBuffers.js +559 -2
  20. package/lib/io/ByteBuffer.test.js +11 -3
  21. package/lib/io/parseDataUri.js +11 -3
  22. package/lib/io/parseDataUri.test.js +31 -11
  23. package/lib/langs/AsyncCloser.js +11 -3
  24. package/lib/langs/deepFreeze.js +5 -5
  25. package/lib/langs/mixin.js +6 -12
  26. package/lib/langs/mixin.test.js +53 -5
  27. package/lib/langs/mixin2.js +26 -0
  28. package/lib/langs/parseBoolean.js +3 -2
  29. package/lib/langs/parseDate.js +20 -0
  30. package/lib/langs/shallowEqual.js +5 -5
  31. package/lib/libs/ms.js +1 -1
  32. package/lib/maths/random.js +12 -4
  33. package/lib/objects/merge/isMergeableObject.js +1 -1
  34. package/lib/objects/merge/merge.js +1 -1
  35. package/lib/objects/merge/merge.test.js +11 -3
  36. package/lib/objects/set.js +10 -2
  37. package/lib/objects/set.test.js +2 -2
  38. package/lib/scripts/getGenerateContext.js +13 -5
  39. package/lib/server/fetch/createFetchWithProxyByNodeFetch.js +11 -3
  40. package/lib/server/fetch/createFetchWithProxyByUndici.js +11 -3
  41. package/lib/server/polyfill/polyfillBrowser.js +11 -3
  42. package/lib/server/polyfill/polyfillBrowser.test.js +11 -3
  43. package/lib/server/polyfill/polyfillCrypto.js +11 -3
  44. package/lib/server/polyfill/polyfillJsDom.js +31 -11
  45. package/lib/strings/renderTemplate.test.js +2 -2
  46. package/lib/web/getRandomValues.js +1 -1
  47. package/lib/web/structuredClone.js +2 -2
  48. package/package.json +8 -4
  49. package/src/asyncs/Promises.ts +2 -1
  50. package/src/asyncs/timeout.ts +1 -1
  51. package/src/crypto/hashing.ts +7 -6
  52. package/src/crypto/pem/pem.ts +3 -2
  53. package/src/errors/Errors.ts +106 -1
  54. package/src/fetch/dumpRequest.ts +1 -1
  55. package/src/index.ts +10 -8
  56. package/src/io/ArrayBuffers.ts +676 -1
  57. package/src/langs/mixin.test.ts +35 -5
  58. package/src/langs/mixin.ts +46 -65
  59. package/src/langs/mixin2.ts +80 -0
  60. package/src/langs/parseBoolean.ts +9 -1
  61. package/src/langs/parseDate.ts +13 -0
  62. package/src/objects/set.ts +10 -1
  63. package/src/types.d.ts +1 -1
  64. package/tsconfig.json +8 -14
  65. package/lib/errors/Errors.mod.js +0 -206
  66. package/lib/io/ArrayBuffers.mod.js +0 -531
  67. package/src/errors/Errors.mod.ts +0 -104
  68. package/src/io/ArrayBuffers.mod.ts +0 -670
@@ -1,670 +0,0 @@
1
- import { classOf } from '../langs/classOf';
2
- import { getGlobalThis } from '../web/getGlobalThis';
3
- import { decodeBase64ToUint8Array, encodeArrayBufferToBase64 } from './base64';
4
- import { isBuffer } from './isBuffer';
5
- import type { Bytes, TypedArray } from './types';
6
-
7
- /**
8
- * Various utils to work with {@link ArrayBuffer}
9
- *
10
- * @see https://github.com/tc39/proposal-resizablearraybuffer
11
- */
12
- /*
13
- Uint8Array to/from base64 and hex
14
- Stage 3
15
- Uint8Array.fromBase64, Uint8Array.prototype.toBase64
16
- Uint8Array.fromHex, Uint8Array.prototype.toHex
17
- https://github.com/tc39/proposal-arraybuffer-base64
18
-
19
- Unicode routines (UTF8, UTF16, UTF32) and Base64
20
- used by Node.js, WebKit/Safari, Ladybird, Cloudflare Workers, Bun
21
- https://github.com/simdutf/simdutf
22
- */
23
-
24
- /*
25
- In-Place Resizable and Growable ArrayBuffers
26
- Stage 4
27
- Chrome 111, Nodejs 20, Safari 16.4
28
-
29
- SharedArrayBuffer & ArrayBuffer
30
- constructor(byteLength, {maxByteLength})
31
- prototype.resize(newByteLength)
32
- prototype.slice(start, end)
33
- prototype.{resizable,maxByteLength}
34
- https://github.com/tc39/proposal-resizablearraybuffer
35
- */
36
-
37
- export type BinaryStringEncoding =
38
- | 'ascii'
39
- | 'utf16le'
40
- // | 'utf-16le'
41
- | 'ucs2'
42
- | 'ucs-2'
43
- | 'base64'
44
- | 'base64url'
45
- | 'latin1'
46
- | 'binary'
47
- | 'utf8'
48
- | 'utf-8'
49
- | 'hex';
50
-
51
- let nativeBufferAllowed: boolean = true;
52
- let isBufferAvailable: undefined | boolean;
53
- let textEncoder: TextEncoder;
54
- let textDecoder: TextDecoder;
55
-
56
- function decode(v: AllowSharedBufferSource): string {
57
- // need icu full data
58
- // if (encoding) return new TextDecoder(encoding).decode(v);
59
- return (textDecoder ||= new TextDecoder()).decode(v);
60
- }
61
-
62
- function encode(v: string): Bytes;
63
- function encode<T>(v: string | T): T | Bytes;
64
- function encode<T>(v: string | T): T | Bytes {
65
- if (typeof v === 'string') {
66
- return (textEncoder ||= new TextEncoder()).encode(v);
67
- }
68
- return v;
69
- }
70
-
71
- /**
72
- * isNativeBufferAvailable check if the native {@link Buffer} is available
73
- */
74
- export function isNativeBufferAvailable(): boolean {
75
- // eslint-disable-next-line no-return-assign
76
- return (isBufferAvailable ??= !(getGlobalThis().Buffer as any)?.isPollyfill?.());
77
- }
78
-
79
- export function isNativeBufferAllowed(): boolean {
80
- return Boolean(nativeBufferAllowed && isBufferAvailable);
81
- }
82
-
83
- export function setNativeBufferAllowed(v: boolean): void {
84
- nativeBufferAllowed = v;
85
- }
86
-
87
- export function isArrayBuffer(v: unknown): v is ArrayBuffer {
88
- return v instanceof ArrayBuffer;
89
- }
90
-
91
- /**
92
- * slice the given view with the given offset and length, will handle the {@link Buffer} as well
93
- *
94
- * @see {@link https://nodejs.org/api/buffer.html#bufslicestart-end Buffer.slice}
95
- */
96
- export function slice<T extends TypedArray>(o: T, start?: number, end?: number): T {
97
- // NodeJS Buffer slice is not the same as UInt8Array slice
98
- // https://nodejs.org/api/buffer.html#bufslicestart-end
99
- if (isBuffer(o)) {
100
- return Uint8Array.prototype.slice.call(o, start, end) as T;
101
- }
102
- return o.slice(start, end) as T;
103
- }
104
-
105
- /**
106
- * asView convert the given value to given {@link TypedArray} view
107
- *
108
- * TypedArray can be {@link Buffer}, will avoid copy
109
- */
110
- export function asView<C extends ArrayBufferViewConstructor<unknown>>(
111
- TypedArray: C,
112
- v: BufferSource | TypedArray,
113
- byteOffset?: number,
114
- byteLength?: number,
115
- ): InstanceType<C> {
116
- if (v instanceof TypedArray && (byteOffset ?? 0) === 0 && byteLength === undefined) {
117
- return v as InstanceType<C>;
118
- }
119
- if (ArrayBuffer.isView(v) || isBuffer(v)) {
120
- if (isNativeBufferAllowed() && (TypedArray as any) === Buffer) {
121
- // new Buffer() is deprecated
122
- return Buffer.from(v.buffer, byteOffset, byteLength) as InstanceType<C>;
123
- }
124
- return new TypedArray(v.buffer, v.byteOffset + (byteOffset ?? 0), byteLength ?? v.byteLength) as InstanceType<C>;
125
- }
126
- return new TypedArray(v, byteOffset, byteLength) as InstanceType<C>;
127
- }
128
-
129
- /**
130
- * toString convert the given {@link BufferSource} to string
131
- */
132
- export function toString(source: BufferSource | TypedArray | string, encoding: BinaryStringEncoding = 'utf8'): string {
133
- if (typeof source === 'string') {
134
- switch (encoding) {
135
- case 'base64':
136
- return btoa(source);
137
- case 'utf-8':
138
- case 'utf8':
139
- return source;
140
- default:
141
- throw new Error(`[ArrayBuffers.toString] Unsupported encoding for string: ${encoding}`);
142
- }
143
- }
144
-
145
- const u8 = asView(Uint8Array, source);
146
- if (isNativeBufferAllowed()) {
147
- return Buffer.from(u8).toString(encoding);
148
- }
149
-
150
- // reference: https://github.com/feross/buffer/blob/master/index.js
151
- switch (encoding) {
152
- case 'hex': {
153
- return toHexString(u8);
154
- }
155
- case 'base64': {
156
- return toBase64(u8);
157
- }
158
- case 'utf8':
159
- case 'utf-8':
160
- return decode(source);
161
- case 'ascii': {
162
- return toAsciiString(u8);
163
- }
164
- case 'latin1':
165
- case 'binary': {
166
- return toLatin1String(u8);
167
- }
168
- case 'ucs2':
169
- case 'ucs-2':
170
- case 'utf16le': {
171
- return toUtf16LeString(u8);
172
- }
173
- default:
174
- throw new Error(`[ArrayBuffers.toString] Unknown encoding: ${encoding}`);
175
- }
176
- }
177
-
178
- /**
179
- * Normalize encoding string to standard form
180
- * @param encoding - The encoding string to normalize
181
- * @returns Normalized encoding or undefined if invalid
182
- */
183
- function normalizeEncoding(encoding: string | undefined): BinaryStringEncoding | undefined {
184
- switch (encoding?.toLowerCase()) {
185
- case 'utf-8':
186
- case 'utf8':
187
- return 'utf8';
188
- case 'utf-16le':
189
- case 'ucs2':
190
- case 'ucs-2':
191
- return 'utf16le';
192
- case 'hex':
193
- case 'ascii':
194
- case 'latin1':
195
- case 'binary':
196
- case 'base64':
197
- case 'utf16le':
198
- return encoding as BinaryStringEncoding;
199
- default:
200
- return undefined;
201
- }
202
- }
203
-
204
- /**
205
- * Check if the given string is a supported character encoding
206
- * @param v - The string to check
207
- * @returns True if the encoding is supported, false otherwise
208
- */
209
- export function isEncoding(v?: string): v is BinaryStringEncoding {
210
- return normalizeEncoding(v) !== undefined;
211
- }
212
-
213
- export function toJSON<T = any>(v: BufferSource | string, reviver?: (this: any, key: string, value: any) => any): T {
214
- return JSON.parse(toString(v), reviver);
215
- }
216
-
217
- /**
218
- * from convert the given value to {@link ArrayBuffer} like
219
- */
220
- export function from(
221
- src: string | BufferSource | ArrayLike<number> | Iterable<number>,
222
- encoding?: BinaryStringEncoding,
223
- ): ArrayBuffer | TypedArray;
224
- /**
225
- * from convert the given value to {@link TypedArray}
226
- */
227
- export function from<C extends ArrayBufferViewConstructor<unknown>>(
228
- src: string | BufferSource | ArrayLike<number> | Iterable<number>,
229
- encoding: BinaryStringEncoding,
230
- TypedArray: C,
231
- ): InstanceType<C>;
232
- export function from(
233
- src: string | BufferSource | ArrayLike<number> | Iterable<number>,
234
- encoding: BinaryStringEncoding = 'utf8',
235
- view?: any,
236
- ): any {
237
- if (!src) {
238
- return new (view || ArrayBuffer)(0);
239
- }
240
-
241
- if (isBufferSource(src)) {
242
- return view ? asView(view, src) : src;
243
- }
244
-
245
- // Array<number> | Iterable<number>
246
- if ((typeof src !== 'string' && isIterable(src)) || Array.isArray(src)) {
247
- return (view || Uint8Array).from(src as ArrayLike<number>);
248
- }
249
-
250
- if (view) {
251
- return asView(view, from(src, encoding));
252
- }
253
-
254
- if (typeof src === 'string') {
255
- if (isNativeBufferAllowed()) {
256
- return Buffer.from(src, encoding);
257
- }
258
-
259
- switch (encoding) {
260
- case 'utf-8':
261
- case 'utf8':
262
- return encode(src).buffer;
263
- case 'base64':
264
- return fromBase64(src);
265
- case 'hex':
266
- return fromHex(src);
267
- default:
268
- throw new Error(`ArrayBuffers.from unsupported encoding: ${encoding}`);
269
- }
270
- }
271
-
272
- const type = classOf(src);
273
- throw new TypeError(`ArrayBuffers.from unsupported type ${type}`);
274
- }
275
-
276
- /**
277
- * concat the given {@link BufferSource} to a new {@link ArrayBuffer}
278
- */
279
- export function concat(buffers: Array<BufferSource>, result?: ArrayBuffer, offset = 0): ArrayBuffer {
280
- if (!Array.isArray(buffers) || buffers.length === 0) {
281
- return new ArrayBuffer(0);
282
- }
283
-
284
- const length = buffers.reduce((a, b) => a + (b?.byteLength ?? 0), 0);
285
- const r = result ? new Uint8Array(result) : new Uint8Array(length);
286
-
287
- for (const buffer of buffers) {
288
- if (!buffer?.byteLength) continue;
289
-
290
- let n: Uint8Array;
291
- if (buffer instanceof ArrayBuffer) {
292
- n = new Uint8Array(buffer);
293
- } else if (ArrayBuffer.isView(buffer)) {
294
- n = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
295
- } else {
296
- throw new Error(`ArrayBuffers.concat unsupported type ${classOf(buffer)}`);
297
- }
298
-
299
- r.set(n, offset);
300
- offset += buffer.byteLength;
301
- }
302
-
303
- return r.buffer;
304
- }
305
-
306
- export function fromBase64(v: string, encoding?: undefined): Bytes;
307
- export function fromBase64(v: string, encoding: BinaryStringEncoding): string;
308
- export function fromBase64(v: string, encoding?: BinaryStringEncoding): Bytes | string {
309
- if (encoding) {
310
- return toString(fromBase64(v), encoding);
311
- }
312
-
313
- if ('fromBase64' in Uint8Array && typeof Uint8Array.fromBase64 === 'function') {
314
- return Uint8Array.fromBase64(v);
315
- }
316
-
317
- if (isNativeBufferAllowed()) {
318
- return Buffer.from(v, 'base64');
319
- }
320
-
321
- // Clean the base64 string by removing invalid characters
322
- return decodeBase64ToUint8Array(v.replace(/[^0-9a-zA-Z=+/_]/g, ''));
323
- }
324
-
325
- export function fromHex(v: string, encoding?: undefined): Bytes;
326
- export function fromHex(v: string, encoding: BinaryStringEncoding): string;
327
- export function fromHex(v: string, encoding?: BinaryStringEncoding): Uint8Array | string {
328
- if (encoding) {
329
- return toString(fromHex(v), encoding);
330
- }
331
-
332
- if ('fromHex' in Uint8Array && typeof Uint8Array.fromHex === 'function') {
333
- return Uint8Array.fromHex(v);
334
- }
335
-
336
- if (isNativeBufferAllowed()) {
337
- return Buffer.from(v, 'hex');
338
- }
339
-
340
- // Handle odd-length hex strings by padding with leading zero
341
- const cleanHex = v.length % 2 === 1 ? '0' + v : v;
342
- const matches = cleanHex.match(/.{1,2}/g);
343
- if (!matches) {
344
- throw new Error('Invalid hex string');
345
- }
346
- const num = matches.map((byte) => parseInt(byte, 16));
347
- return new Uint8Array(num);
348
- }
349
-
350
- /**
351
- * toBase64 convert the given {@link BufferSource} to base64 string
352
- * @param source if string, will be encoded as utf8
353
- */
354
- export function toBase64(source: BufferSource | string): string {
355
- source = encode(source);
356
-
357
- if ('toBase64' in Uint8Array.prototype) {
358
- return (toUint8Array(source) as Uint8Array2).toBase64();
359
- }
360
-
361
- if (isNativeBufferAllowed()) {
362
- return Buffer.from(asView(Uint8Array, source)).toString('base64');
363
- }
364
-
365
- return encodeArrayBufferToBase64(toArrayBuffer(source));
366
- }
367
-
368
- export function toHex(v: BufferSource | string): string {
369
- v = encode(v);
370
-
371
- if ('toHex' in Uint8Array.prototype) {
372
- return (toUint8Array(v) as Uint8Array2).toHex();
373
- }
374
-
375
- if (isNativeBufferAllowed()) {
376
- return Buffer.from(asView(Uint8Array, v)).toString('hex');
377
- }
378
-
379
- return toString(v, 'hex');
380
- }
381
-
382
- export function resize(v: ArrayBuffer, newByteLength?: number, maxByteLength?: number): ArrayBuffer {
383
- if (newByteLength === undefined || newByteLength === null) {
384
- return v;
385
- }
386
-
387
- // Chrome 111, Nodejs 20 - use native resize if available
388
- if ('resize' in v && typeof v.resize === 'function') {
389
- if ('resizable' in v && v.resizable) {
390
- if ('maxByteLength' in v && typeof v.maxByteLength === 'number' && v.maxByteLength >= newByteLength) {
391
- v.resize(newByteLength);
392
- return v as ArrayBuffer;
393
- }
394
- }
395
- }
396
-
397
- // Fallback: create new buffer and copy data
398
- const old = v;
399
- const newBuf = new (ArrayBuffer as ArrayBuffer2Constructor)(newByteLength, { maxByteLength: maxByteLength });
400
- const oldView = new Uint8Array(old);
401
- const newView = new Uint8Array(newBuf);
402
- newView.set(oldView);
403
- return newBuf;
404
- }
405
-
406
- export function toArrayBuffer(v: BufferSource): ArrayBuffer {
407
- if (v instanceof ArrayBuffer) {
408
- return v;
409
- }
410
-
411
- if (ArrayBuffer.isView(v)) {
412
- if (v.byteOffset > 0) {
413
- throw new Error('ArrayBuffers.toArrayBuffer does not support view with offset');
414
- }
415
- return v.buffer;
416
- }
417
-
418
- throw new Error(`ArrayBuffers.toArrayBuffer unsupported type ${classOf(v)}`);
419
- }
420
-
421
- export function toUint8Array(v: BufferSource): Bytes {
422
- return asView(Uint8Array, v);
423
- }
424
-
425
- /**
426
- * Allocate a new ArrayBuffer or Uint8Array with optional fill value
427
- * @param size - The size in bytes to allocate
428
- * @param fill - Optional fill value (number or string)
429
- * @param encoding - Encoding for string fill value (default: 'utf8')
430
- * @returns ArrayBuffer or Uint8Array
431
- */
432
- export function alloc(size: number, fill?: string | number, encoding?: BinaryStringEncoding): ArrayBuffer | Bytes {
433
- if (fill !== undefined) {
434
- if (typeof fill === 'number') {
435
- return new Uint8Array(size).fill(fill);
436
- }
437
- // Convert string to buffer and slice to size
438
- // https://stackoverflow.com/questions/73994091
439
- return asView(Uint8Array, from(fill, encoding)).slice(0, size);
440
- }
441
- return new ArrayBuffer(size);
442
- }
443
-
444
- type ArrayBufferViewConstructor<T> = new (buffer: ArrayBufferLike, byteOffset?: number, byteLength?: number) => T;
445
-
446
- // Helper functions for string conversion
447
- /**
448
- * Convert Uint8Array to hex string efficiently
449
- * @param u8 - The Uint8Array to convert
450
- * @returns Hex string representation
451
- */
452
- function toHexString(u8: Uint8Array): string {
453
- let result = '';
454
- for (let i = 0; i < u8.length; i++) {
455
- result += hexLookupTable[u8[i]];
456
- }
457
- return result;
458
- }
459
-
460
- /**
461
- * Convert Uint8Array to ASCII string
462
- * @param u8 - The Uint8Array to convert
463
- * @returns ASCII string representation
464
- */
465
- function toAsciiString(u8: Uint8Array): string {
466
- let result = '';
467
- for (let i = 0; i < u8.length; i++) {
468
- result += String.fromCharCode(u8[i] & 0x7f);
469
- }
470
- return result;
471
- }
472
-
473
- /**
474
- * Convert Uint8Array to Latin1 string
475
- * @param u8 - The Uint8Array to convert
476
- * @returns Latin1 string representation
477
- */
478
- function toLatin1String(u8: Uint8Array): string {
479
- let result = '';
480
- for (let i = 0; i < u8.length; i++) {
481
- result += String.fromCharCode(u8[i]);
482
- }
483
- return result;
484
- }
485
-
486
- /**
487
- * Convert Uint8Array to UTF-16LE string
488
- * @param u8 - The Uint8Array to convert
489
- * @returns UTF-16LE string representation
490
- */
491
- function toUtf16LeString(u8: Uint8Array): string {
492
- let result = '';
493
- // If length is odd, the last 8 bits must be ignored (same as node.js)
494
- for (let i = 0; i < u8.length - 1; i += 2) {
495
- result += String.fromCharCode(u8[i] + u8[i + 1] * 256);
496
- }
497
- return result;
498
- }
499
-
500
- // base16 lookup table for efficient hex conversion
501
- const hexLookupTable = (function () {
502
- const alphabet = '0123456789abcdef';
503
- const table = new Array(256);
504
- for (let i = 0; i < 16; ++i) {
505
- const i16 = i * 16;
506
- for (let j = 0; j < 16; ++j) {
507
- table[i16 + j] = alphabet[i] + alphabet[j];
508
- }
509
- }
510
- return table;
511
- })();
512
-
513
- // avoid declare global
514
-
515
- interface Uint8Array2 extends Uint8Array {
516
- toBase64(): string;
517
-
518
- toHex(): string;
519
- }
520
-
521
- type IArrayBuffer = (ArrayBuffer | SharedArrayBuffer) & {
522
- resize(newByteLength: number): void;
523
- resizable: boolean;
524
- maxByteLength: number;
525
- };
526
-
527
- interface ArrayBuffer2Constructor {
528
- new (byteLength: number, opts?: { maxByteLength?: number }): ArrayBuffer;
529
- }
530
-
531
- // Helper functions for internal use
532
- function isIterable<T>(obj: unknown): obj is Iterable<T> {
533
- return obj != null && typeof (obj as any)?.[Symbol.iterator] === 'function';
534
- }
535
-
536
- function isBufferSource(value: unknown): value is BufferSource {
537
- return ArrayBuffer.isView(value) || value instanceof ArrayBuffer;
538
- }
539
-
540
- /**
541
- * Check if two BufferSources are equal
542
- * @param a - First buffer source
543
- * @param b - Second buffer source
544
- * @returns True if buffers are equal, false otherwise
545
- */
546
- export function equals(a: BufferSource, b: BufferSource): boolean {
547
- if (a === b) return true;
548
- const aView = asView(Uint8Array, a);
549
- const bView = asView(Uint8Array, b);
550
-
551
- if (aView.length !== bView.length) {
552
- return false;
553
- }
554
-
555
- for (let i = 0; i < aView.length; i++) {
556
- if (aView[i] !== bView[i]) {
557
- return false;
558
- }
559
- }
560
-
561
- return true;
562
- }
563
-
564
- /**
565
- * Compare two BufferSources lexicographically
566
- * @param a - First buffer source
567
- * @param b - Second buffer source
568
- * @returns -1 if a < b, 0 if a === b, 1 if a > b
569
- */
570
- export function compare(a: BufferSource, b: BufferSource): number {
571
- if (a === b) return 0;
572
- const aView = asView(Uint8Array, a);
573
- const bView = asView(Uint8Array, b);
574
-
575
- const minLength = Math.min(aView.length, bView.length);
576
-
577
- for (let i = 0; i < minLength; i++) {
578
- if (aView[i] < bView[i]) return -1;
579
- if (aView[i] > bView[i]) return 1;
580
- }
581
-
582
- return aView.length - bView.length;
583
- }
584
-
585
- /**
586
- * Check if a BufferSource starts with another BufferSource
587
- * @param buffer - The buffer to check
588
- * @param prefix - The prefix to check for
589
- * @returns True if buffer starts with prefix, false otherwise
590
- */
591
- export function startsWith(buffer: BufferSource, prefix: BufferSource): boolean {
592
- const bufferView = asView(Uint8Array, buffer);
593
- const prefixView = asView(Uint8Array, prefix);
594
-
595
- if (prefixView.length > bufferView.length) {
596
- return false;
597
- }
598
-
599
- for (let i = 0; i < prefixView.length; i++) {
600
- if (bufferView[i] !== prefixView[i]) {
601
- return false;
602
- }
603
- }
604
-
605
- return true;
606
- }
607
-
608
- /**
609
- * Check if a BufferSource ends with another BufferSource
610
- * @param buffer - The buffer to check
611
- * @param suffix - The suffix to check for
612
- * @returns True if buffer ends with suffix, false otherwise
613
- */
614
- export function endsWith(buffer: BufferSource, suffix: BufferSource): boolean {
615
- const bufferView = asView(Uint8Array, buffer);
616
- const suffixView = asView(Uint8Array, suffix);
617
-
618
- if (suffixView.length > bufferView.length) {
619
- return false;
620
- }
621
-
622
- const offset = bufferView.length - suffixView.length;
623
- for (let i = 0; i < suffixView.length; i++) {
624
- if (bufferView[offset + i] !== suffixView[i]) {
625
- return false;
626
- }
627
- }
628
-
629
- return true;
630
- }
631
-
632
- /**
633
- * Find the index of a sub-buffer within a buffer
634
- * @param buffer - The buffer to search in
635
- * @param search - The sub-buffer to search for
636
- * @param startIndex - Starting index for search (default: 0)
637
- * @returns Index of first occurrence, or -1 if not found
638
- */
639
- export function indexOf(buffer: BufferSource, search: BufferSource, startIndex = 0): number {
640
- const bufferView = asView(Uint8Array, buffer);
641
- const searchView = asView(Uint8Array, search);
642
-
643
- if (searchView.length === 0) return startIndex;
644
- if (searchView.length > bufferView.length) return -1;
645
-
646
- for (let i = startIndex; i <= bufferView.length - searchView.length; i++) {
647
- let found = true;
648
- for (let j = 0; j < searchView.length; j++) {
649
- if (bufferView[i + j] !== searchView[j]) {
650
- found = false;
651
- break;
652
- }
653
- }
654
- if (found) return i;
655
- }
656
-
657
- return -1;
658
- }
659
-
660
- /**
661
- * Get a sub-buffer from a buffer
662
- * @param buffer - The source buffer
663
- * @param start - Start index (inclusive)
664
- * @param end - End index (exclusive, optional)
665
- * @returns New Uint8Array containing the sub-buffer
666
- */
667
- export function subarray(buffer: BufferSource, start: number, end?: number): Uint8Array {
668
- const view = asView(Uint8Array, buffer);
669
- return view.subarray(start, end);
670
- }