jstime_core 0.66.0

Another JS Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// Node.js Buffer API
// https://nodejs.org/api/buffer.html
// Minimal implementation compatible with Node.js

'use strict';

// eslint-disable-next-line no-unused-expressions
(({ bufferAlloc, bufferFrom, bufferConcat, bufferByteLength, bufferCompare, bufferIsEncoding }) => {
  // Constants for encoding
  const kMaxLength = 0xFFFFFFFF;

  // List of supported encodings
  const ENCODINGS = ['utf8', 'utf-8', 'hex', 'base64', 'base64url', 'latin1', 'binary', 'ascii'];

  /**
   * Buffer class - extends Uint8Array
   * Note: In Node.js, Buffer extends Uint8Array. We simulate this by adding
   * Buffer-specific methods to Uint8Array instances created by our native functions.
   */
  class Buffer extends Uint8Array {
    /**
     * Allocates a new Buffer of size bytes.
     * @param {number} size - The desired length of the new Buffer.
     * @param {number|string|Buffer} [fill] - A value to pre-fill the Buffer with.
     * @param {string} [encoding='utf8'] - If fill is a string, this is its encoding.
     * @returns {Buffer}
     */
    static alloc(size, fill, encoding) {
      if (typeof size !== 'number') {
        throw new TypeError('The "size" argument must be of type number');
      }
      if (size < 0 || size > kMaxLength) {
        throw new RangeError(`The value "${size}" is invalid for option "size"`);
      }
      const buf = bufferAlloc(size, fill, encoding);
      return Buffer._wrapUint8Array(buf);
    }

    /**
     * Allocates a new Buffer of size bytes (unsafe, not zeroed).
     * @param {number} size - The desired length of the new Buffer.
     * @returns {Buffer}
     */
    static allocUnsafe(size) {
      if (typeof size !== 'number') {
        throw new TypeError('The "size" argument must be of type number');
      }
      if (size < 0 || size > kMaxLength) {
        throw new RangeError(`The value "${size}" is invalid for option "size"`);
      }
      // In our implementation, allocUnsafe is the same as alloc since we always
      // create new backing stores
      const buf = bufferAlloc(size);
      return Buffer._wrapUint8Array(buf);
    }

    /**
     * Allocates a new Buffer of size bytes (unsafe, slow, not zeroed).
     * @param {number} size - The desired length of the new Buffer.
     * @returns {Buffer}
     */
    static allocUnsafeSlow(size) {
      return Buffer.allocUnsafe(size);
    }

    /**
     * Creates a new Buffer from various sources.
     * @param {string|Array|Buffer|ArrayBuffer|Object} value - Source value.
     * @param {string|number} [encodingOrOffset] - Encoding for strings, or offset for ArrayBuffer.
     * @param {number} [length] - Length for ArrayBuffer slice.
     * @returns {Buffer}
     */
    static from(value, encodingOrOffset, length) {
      if (value === null || value === undefined) {
        throw new TypeError('The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array');
      }

      // Handle string
      if (typeof value === 'string') {
        const buf = bufferFrom(value, encodingOrOffset || 'utf8');
        return Buffer._wrapUint8Array(buf);
      }

      // Handle ArrayBuffer with offset and length
      if (value instanceof ArrayBuffer) {
        const offset = typeof encodingOrOffset === 'number' ? encodingOrOffset : 0;
        const len = typeof length === 'number' ? length : value.byteLength - offset;
        const view = new Uint8Array(value, offset, len);
        const buf = bufferFrom(view);
        return Buffer._wrapUint8Array(buf);
      }

      // Handle array-like, Buffer, TypedArray
      const buf = bufferFrom(value);
      return Buffer._wrapUint8Array(buf);
    }

    /**
     * Returns the concatenation of the Buffer instances in list.
     * @param {Array<Buffer|Uint8Array>} list - List of Buffers to concatenate.
     * @param {number} [totalLength] - Total length of the resulting Buffer.
     * @returns {Buffer}
     */
    static concat(list, totalLength) {
      if (!Array.isArray(list)) {
        throw new TypeError('The "list" argument must be an instance of Array');
      }
      if (list.length === 0) {
        return Buffer.alloc(0);
      }
      const buf = bufferConcat(list, totalLength);
      return Buffer._wrapUint8Array(buf);
    }

    /**
     * Returns the byte length of a string when encoded.
     * @param {string|Buffer|ArrayBuffer} string - The value to get byte length of.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {number}
     */
    static byteLength(string, encoding) {
      return bufferByteLength(string, encoding || 'utf8');
    }

    /**
     * Compares two Buffer instances.
     * @param {Buffer} buf1 - First Buffer.
     * @param {Buffer} buf2 - Second Buffer.
     * @returns {number} -1, 0, or 1.
     */
    static compare(buf1, buf2) {
      if (!(buf1 instanceof Uint8Array) || !(buf2 instanceof Uint8Array)) {
        throw new TypeError('The "buf1" and "buf2" arguments must be one of type Buffer or Uint8Array');
      }
      return bufferCompare(buf1, buf2);
    }

    /**
     * Returns true if encoding is a supported character encoding.
     * @param {string} encoding - The encoding to test.
     * @returns {boolean}
     */
    static isEncoding(encoding) {
      return bufferIsEncoding(encoding);
    }

    /**
     * Returns true if obj is a Buffer.
     * @param {*} obj - Object to test.
     * @returns {boolean}
     */
    static isBuffer(obj) {
      return obj instanceof Buffer || (obj instanceof Uint8Array && obj._isBuffer === true);
    }

    /**
     * Wraps a Uint8Array with Buffer methods.
     * @private
     */
    static _wrapUint8Array(arr) {
      // Mark as buffer
      Object.defineProperty(arr, '_isBuffer', { value: true, writable: false, enumerable: false });

      // Add Buffer prototype methods
      Object.setPrototypeOf(arr, Buffer.prototype);

      return arr;
    }

    /**
     * Returns the maximum number of bytes allowed for a Buffer.
     */
    static get poolSize() {
      return 8192;
    }

    /**
     * Writes string to buf at offset according to encoding.
     * @param {string} string - String to write.
     * @param {number} [offset=0] - Offset to start writing at.
     * @param {number} [length] - Maximum number of bytes to write.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {number} Number of bytes written.
     */
    write(string, offset, length, encoding) {
      // Handle various argument combinations
      if (typeof offset === 'string') {
        encoding = offset;
        offset = 0;
        length = this.length;
      } else if (typeof length === 'string') {
        encoding = length;
        length = this.length - offset;
      }

      offset = offset || 0;
      length = length !== undefined ? length : this.length - offset;
      encoding = encoding || 'utf8';

      if (typeof string !== 'string') {
        throw new TypeError('Argument must be a string');
      }

      const encoded = Buffer.from(string, encoding);
      const bytesToWrite = Math.min(length, encoded.length, this.length - offset);

      for (let i = 0; i < bytesToWrite; i++) {
        this[offset + i] = encoded[i];
      }

      return bytesToWrite;
    }

    /**
     * Decodes buf to a string according to encoding.
     * @param {string} [encoding='utf8'] - The encoding.
     * @param {number} [start=0] - Byte offset to start decoding at.
     * @param {number} [end] - Byte offset to stop decoding at.
     * @returns {string}
     */
    toString(encoding, start, end) {
      encoding = encoding || 'utf8';
      start = start || 0;
      end = end !== undefined ? end : this.length;

      if (start >= end) {
        return '';
      }

      const slice = this.subarray(start, end);
      const enc = encoding.toLowerCase();

      switch (enc) {
        case 'utf8':
        case 'utf-8':
          return new TextDecoder('utf-8').decode(slice);

        case 'hex':
          let hex = '';
          for (let i = 0; i < slice.length; i++) {
            hex += slice[i].toString(16).padStart(2, '0');
          }
          return hex;

        case 'base64': {
          // Use btoa for base64 encoding
          let binary = '';
          for (let i = 0; i < slice.length; i++) {
            binary += String.fromCharCode(slice[i]);
          }
          return btoa(binary);
        }

        case 'base64url': {
          // URL-safe base64
          let binary = '';
          for (let i = 0; i < slice.length; i++) {
            binary += String.fromCharCode(slice[i]);
          }
          return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
        }

        case 'latin1':
        case 'binary':
        case 'ascii': {
          let str = '';
          for (let i = 0; i < slice.length; i++) {
            str += String.fromCharCode(slice[i]);
          }
          return str;
        }

        default:
          return new TextDecoder('utf-8').decode(slice);
      }
    }

    /**
     * Returns a JSON representation of buf.
     * @returns {Object}
     */
    toJSON() {
      return {
        type: 'Buffer',
        data: Array.from(this)
      };
    }

    /**
     * Compares buf with target.
     * @param {Buffer|Uint8Array} target - Buffer to compare to.
     * @param {number} [targetStart=0] - Offset in target to start comparison.
     * @param {number} [targetEnd=target.length] - Offset in target to end comparison.
     * @param {number} [sourceStart=0] - Offset in buf to start comparison.
     * @param {number} [sourceEnd=buf.length] - Offset in buf to end comparison.
     * @returns {number} -1, 0, or 1.
     */
    compare(target, targetStart, targetEnd, sourceStart, sourceEnd) {
      if (!(target instanceof Uint8Array)) {
        throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array');
      }

      targetStart = targetStart || 0;
      targetEnd = targetEnd !== undefined ? targetEnd : target.length;
      sourceStart = sourceStart || 0;
      sourceEnd = sourceEnd !== undefined ? sourceEnd : this.length;

      const source = this.subarray(sourceStart, sourceEnd);
      const dest = target.subarray(targetStart, targetEnd);

      return Buffer.compare(source, dest);
    }

    /**
     * Returns true if buf and otherBuffer have exactly the same bytes.
     * @param {Buffer|Uint8Array} otherBuffer - Buffer to compare to.
     * @returns {boolean}
     */
    equals(otherBuffer) {
      if (!(otherBuffer instanceof Uint8Array)) {
        throw new TypeError('The "otherBuffer" argument must be one of type Buffer or Uint8Array');
      }
      return this.compare(otherBuffer) === 0;
    }

    /**
     * Copies data from a region of buf to a region in target.
     * @param {Buffer|Uint8Array} target - Target Buffer.
     * @param {number} [targetStart=0] - Offset in target to start writing.
     * @param {number} [sourceStart=0] - Offset in buf to start copying from.
     * @param {number} [sourceEnd=buf.length] - Offset in buf to stop copying.
     * @returns {number} Number of bytes copied.
     */
    copy(target, targetStart, sourceStart, sourceEnd) {
      if (!(target instanceof Uint8Array)) {
        throw new TypeError('The "target" argument must be one of type Buffer or Uint8Array');
      }

      targetStart = targetStart || 0;
      sourceStart = sourceStart || 0;
      sourceEnd = sourceEnd !== undefined ? sourceEnd : this.length;

      const bytesToCopy = Math.min(sourceEnd - sourceStart, target.length - targetStart);

      for (let i = 0; i < bytesToCopy; i++) {
        target[targetStart + i] = this[sourceStart + i];
      }

      return bytesToCopy;
    }

    /**
     * Fills buf with the specified value.
     * @param {string|Buffer|Uint8Array|number} value - Value to fill with.
     * @param {number} [offset=0] - Offset to start filling.
     * @param {number} [end=buf.length] - Offset to stop filling.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {Buffer} A reference to buf.
     */
    fill(value, offset, end, encoding) {
      if (typeof offset === 'string') {
        encoding = offset;
        offset = 0;
        end = this.length;
      } else if (typeof end === 'string') {
        encoding = end;
        end = this.length;
      }

      offset = offset || 0;
      end = end !== undefined ? end : this.length;
      encoding = encoding || 'utf8';

      let fillValue;
      if (typeof value === 'number') {
        fillValue = [value & 0xFF];
      } else if (typeof value === 'string') {
        const encoded = Buffer.from(value, encoding);
        fillValue = Array.from(encoded);
      } else if (value instanceof Uint8Array) {
        fillValue = Array.from(value);
      } else {
        fillValue = [0];
      }

      if (fillValue.length === 0) {
        fillValue = [0];
      }

      let fillIndex = 0;
      for (let i = offset; i < end; i++) {
        this[i] = fillValue[fillIndex % fillValue.length];
        fillIndex++;
      }

      return this;
    }

    /**
     * Returns the index of the first occurrence of value in buf.
     * @param {string|Buffer|Uint8Array|number} value - Value to search for.
     * @param {number} [byteOffset=0] - Offset to begin searching.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {number} Index of first occurrence, or -1.
     */
    indexOf(value, byteOffset, encoding) {
      return this._indexOf(value, byteOffset, encoding, true);
    }

    /**
     * Returns the index of the last occurrence of value in buf.
     * @param {string|Buffer|Uint8Array|number} value - Value to search for.
     * @param {number} [byteOffset] - Offset to begin searching.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {number} Index of last occurrence, or -1.
     */
    lastIndexOf(value, byteOffset, encoding) {
      return this._indexOf(value, byteOffset, encoding, false);
    }

    /**
     * Internal indexOf implementation.
     * @private
     */
    _indexOf(value, byteOffset, encoding, first) {
      if (typeof byteOffset === 'string') {
        encoding = byteOffset;
        byteOffset = first ? 0 : this.length;
      }

      byteOffset = byteOffset || (first ? 0 : this.length);
      encoding = encoding || 'utf8';

      let searchBytes;
      if (typeof value === 'number') {
        searchBytes = [value & 0xFF];
      } else if (typeof value === 'string') {
        const encoded = Buffer.from(value, encoding);
        searchBytes = Array.from(encoded);
      } else if (value instanceof Uint8Array) {
        searchBytes = Array.from(value);
      } else {
        return -1;
      }

      if (searchBytes.length === 0) {
        return first ? 0 : this.length;
      }

      if (first) {
        // Forward search
        for (let i = byteOffset; i <= this.length - searchBytes.length; i++) {
          let found = true;
          for (let j = 0; j < searchBytes.length; j++) {
            if (this[i + j] !== searchBytes[j]) {
              found = false;
              break;
            }
          }
          if (found) {
            return i;
          }
        }
      } else {
        // Backward search
        const startIndex = Math.min(byteOffset, this.length - searchBytes.length);
        for (let i = startIndex; i >= 0; i--) {
          let found = true;
          for (let j = 0; j < searchBytes.length; j++) {
            if (this[i + j] !== searchBytes[j]) {
              found = false;
              break;
            }
          }
          if (found) {
            return i;
          }
        }
      }

      return -1;
    }

    /**
     * Returns true if value is found in buf.
     * @param {string|Buffer|Uint8Array|number} value - Value to search for.
     * @param {number} [byteOffset=0] - Offset to begin searching.
     * @param {string} [encoding='utf8'] - The encoding.
     * @returns {boolean}
     */
    includes(value, byteOffset, encoding) {
      return this.indexOf(value, byteOffset, encoding) !== -1;
    }

    /**
     * Returns a new Buffer that references the same memory as the original.
     * @param {number} [start=0] - Start offset.
     * @param {number} [end=buf.length] - End offset.
     * @returns {Buffer}
     */
    slice(start, end) {
      const slice = super.subarray(start, end);
      return Buffer._wrapUint8Array(slice);
    }

    /**
     * Returns a new Buffer that references the same memory as the original.
     * @param {number} [start=0] - Start offset.
     * @param {number} [end=buf.length] - End offset.
     * @returns {Buffer}
     */
    subarray(start, end) {
      const sub = super.subarray(start, end);
      return Buffer._wrapUint8Array(sub);
    }

    // Read methods
    readUInt8(offset = 0) {
      return this[offset];
    }

    readUInt16LE(offset = 0) {
      return this[offset] | (this[offset + 1] << 8);
    }

    readUInt16BE(offset = 0) {
      return (this[offset] << 8) | this[offset + 1];
    }

    readUInt32LE(offset = 0) {
      return (this[offset] | (this[offset + 1] << 8) | (this[offset + 2] << 16)) + this[offset + 3] * 0x1000000;
    }

    readUInt32BE(offset = 0) {
      return this[offset] * 0x1000000 + ((this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3]);
    }

    readInt8(offset = 0) {
      const val = this[offset];
      return val & 0x80 ? val - 0x100 : val;
    }

    readInt16LE(offset = 0) {
      const val = this[offset] | (this[offset + 1] << 8);
      return val & 0x8000 ? val - 0x10000 : val;
    }

    readInt16BE(offset = 0) {
      const val = (this[offset] << 8) | this[offset + 1];
      return val & 0x8000 ? val - 0x10000 : val;
    }

    readInt32LE(offset = 0) {
      return this[offset] | (this[offset + 1] << 8) | (this[offset + 2] << 16) | (this[offset + 3] << 24);
    }

    readInt32BE(offset = 0) {
      return (this[offset] << 24) | (this[offset + 1] << 16) | (this[offset + 2] << 8) | this[offset + 3];
    }

    readFloatLE(offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 4);
      return view.getFloat32(0, true);
    }

    readFloatBE(offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 4);
      return view.getFloat32(0, false);
    }

    readDoubleLE(offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 8);
      return view.getFloat64(0, true);
    }

    readDoubleBE(offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 8);
      return view.getFloat64(0, false);
    }

    // Write methods
    writeUInt8(value, offset = 0) {
      this[offset] = value & 0xFF;
      return offset + 1;
    }

    writeUInt16LE(value, offset = 0) {
      this[offset] = value & 0xFF;
      this[offset + 1] = (value >>> 8) & 0xFF;
      return offset + 2;
    }

    writeUInt16BE(value, offset = 0) {
      this[offset] = (value >>> 8) & 0xFF;
      this[offset + 1] = value & 0xFF;
      return offset + 2;
    }

    writeUInt32LE(value, offset = 0) {
      this[offset] = value & 0xFF;
      this[offset + 1] = (value >>> 8) & 0xFF;
      this[offset + 2] = (value >>> 16) & 0xFF;
      this[offset + 3] = (value >>> 24) & 0xFF;
      return offset + 4;
    }

    writeUInt32BE(value, offset = 0) {
      this[offset] = (value >>> 24) & 0xFF;
      this[offset + 1] = (value >>> 16) & 0xFF;
      this[offset + 2] = (value >>> 8) & 0xFF;
      this[offset + 3] = value & 0xFF;
      return offset + 4;
    }

    writeInt8(value, offset = 0) {
      if (value < 0) value = 0x100 + value;
      this[offset] = value & 0xFF;
      return offset + 1;
    }

    writeInt16LE(value, offset = 0) {
      if (value < 0) value = 0x10000 + value;
      this[offset] = value & 0xFF;
      this[offset + 1] = (value >>> 8) & 0xFF;
      return offset + 2;
    }

    writeInt16BE(value, offset = 0) {
      if (value < 0) value = 0x10000 + value;
      this[offset] = (value >>> 8) & 0xFF;
      this[offset + 1] = value & 0xFF;
      return offset + 2;
    }

    writeInt32LE(value, offset = 0) {
      this[offset] = value & 0xFF;
      this[offset + 1] = (value >>> 8) & 0xFF;
      this[offset + 2] = (value >>> 16) & 0xFF;
      this[offset + 3] = (value >>> 24) & 0xFF;
      return offset + 4;
    }

    writeInt32BE(value, offset = 0) {
      this[offset] = (value >>> 24) & 0xFF;
      this[offset + 1] = (value >>> 16) & 0xFF;
      this[offset + 2] = (value >>> 8) & 0xFF;
      this[offset + 3] = value & 0xFF;
      return offset + 4;
    }

    writeFloatLE(value, offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 4);
      view.setFloat32(0, value, true);
      return offset + 4;
    }

    writeFloatBE(value, offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 4);
      view.setFloat32(0, value, false);
      return offset + 4;
    }

    writeDoubleLE(value, offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 8);
      view.setFloat64(0, value, true);
      return offset + 8;
    }

    writeDoubleBE(value, offset = 0) {
      const view = new DataView(this.buffer, this.byteOffset + offset, 8);
      view.setFloat64(0, value, false);
      return offset + 8;
    }

    /**
     * Swaps byte order in-place (16-bit).
     * @returns {Buffer}
     */
    swap16() {
      if (this.length % 2 !== 0) {
        throw new RangeError('Buffer size must be a multiple of 16-bits');
      }
      for (let i = 0; i < this.length; i += 2) {
        const t = this[i];
        this[i] = this[i + 1];
        this[i + 1] = t;
      }
      return this;
    }

    /**
     * Swaps byte order in-place (32-bit).
     * @returns {Buffer}
     */
    swap32() {
      if (this.length % 4 !== 0) {
        throw new RangeError('Buffer size must be a multiple of 32-bits');
      }
      for (let i = 0; i < this.length; i += 4) {
        const t0 = this[i];
        const t1 = this[i + 1];
        this[i] = this[i + 3];
        this[i + 1] = this[i + 2];
        this[i + 2] = t1;
        this[i + 3] = t0;
      }
      return this;
    }

    /**
     * Swaps byte order in-place (64-bit).
     * @returns {Buffer}
     */
    swap64() {
      if (this.length % 8 !== 0) {
        throw new RangeError('Buffer size must be a multiple of 64-bits');
      }
      for (let i = 0; i < this.length; i += 8) {
        const t0 = this[i];
        const t1 = this[i + 1];
        const t2 = this[i + 2];
        const t3 = this[i + 3];
        this[i] = this[i + 7];
        this[i + 1] = this[i + 6];
        this[i + 2] = this[i + 5];
        this[i + 3] = this[i + 4];
        this[i + 4] = t3;
        this[i + 5] = t2;
        this[i + 6] = t1;
        this[i + 7] = t0;
      }
      return this;
    }
  }

  // Constants
  Object.defineProperty(Buffer, 'kMaxLength', {
    value: kMaxLength,
    writable: false,
    enumerable: false,
    configurable: false
  });

  // Export the buffer module
  const bufferModule = {
    Buffer,
    kMaxLength,
    constants: {
      MAX_LENGTH: kMaxLength,
      MAX_STRING_LENGTH: kMaxLength
    },
    // For compatibility
    INSPECT_MAX_BYTES: 50,
    // SlowBuffer is deprecated but included for compatibility
    SlowBuffer: Buffer
  };

  // Make it available via import
  globalThis.__node_modules = globalThis.__node_modules || {};
  globalThis.__node_modules['node:buffer'] = bufferModule;
  globalThis.__node_modules['buffer'] = bufferModule;

  // Also expose Buffer globally (like Node.js)
  globalThis.Buffer = Buffer;
});