react-native-nitro-buffer 0.0.1

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 (31) hide show
  1. package/README.md +129 -0
  2. package/cpp/HybridNitroBuffer.cpp +497 -0
  3. package/cpp/HybridNitroBuffer.hpp +44 -0
  4. package/lib/Buffer.d.ts +79 -0
  5. package/lib/Buffer.js +642 -0
  6. package/lib/NitroBuffer.nitro.d.ts +18 -0
  7. package/lib/NitroBuffer.nitro.js +2 -0
  8. package/lib/index.d.ts +12 -0
  9. package/lib/index.js +34 -0
  10. package/lib/utils.d.ts +7 -0
  11. package/lib/utils.js +90 -0
  12. package/nitrogen/generated/.gitattributes +1 -0
  13. package/nitrogen/generated/android/NitroBuffer+autolinking.cmake +81 -0
  14. package/nitrogen/generated/android/NitroBuffer+autolinking.gradle +27 -0
  15. package/nitrogen/generated/android/NitroBufferOnLoad.cpp +44 -0
  16. package/nitrogen/generated/android/NitroBufferOnLoad.hpp +25 -0
  17. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitro_buffer/NitroBufferOnLoad.kt +35 -0
  18. package/nitrogen/generated/ios/NitroBuffer+autolinking.rb +60 -0
  19. package/nitrogen/generated/ios/NitroBuffer-Swift-Cxx-Bridge.cpp +17 -0
  20. package/nitrogen/generated/ios/NitroBuffer-Swift-Cxx-Bridge.hpp +27 -0
  21. package/nitrogen/generated/ios/NitroBuffer-Swift-Cxx-Umbrella.hpp +38 -0
  22. package/nitrogen/generated/ios/NitroBufferAutolinking.mm +35 -0
  23. package/nitrogen/generated/ios/NitroBufferAutolinking.swift +12 -0
  24. package/nitrogen/generated/shared/c++/HybridNitroBufferSpec.cpp +32 -0
  25. package/nitrogen/generated/shared/c++/HybridNitroBufferSpec.hpp +74 -0
  26. package/package.json +55 -0
  27. package/react-native-nitro-buffer.podspec +40 -0
  28. package/src/Buffer.ts +675 -0
  29. package/src/NitroBuffer.nitro.ts +19 -0
  30. package/src/index.ts +18 -0
  31. package/src/utils.ts +76 -0
package/lib/Buffer.js ADDED
@@ -0,0 +1,642 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Buffer = void 0;
4
+ const react_native_nitro_modules_1 = require("react-native-nitro-modules");
5
+ // Lazily load the native module
6
+ let _native;
7
+ function getNative() {
8
+ if (!_native) {
9
+ _native = react_native_nitro_modules_1.NitroModules.createHybridObject('NitroBuffer');
10
+ }
11
+ return _native;
12
+ }
13
+ class Buffer extends Uint8Array {
14
+ constructor(arg, encodingOrOffset, length) {
15
+ // Handling different constructor arguments roughly
16
+ if (typeof arg === 'number') {
17
+ super(arg);
18
+ }
19
+ else if (typeof arg === 'string') {
20
+ const encoding = encodingOrOffset || 'utf8';
21
+ const len = getNative().byteLength(arg, encoding);
22
+ super(len);
23
+ getNative().write(this.buffer, arg, 0, len, encoding);
24
+ }
25
+ else if (arg instanceof Uint8Array) {
26
+ super(arg);
27
+ }
28
+ else if (arg instanceof ArrayBuffer) {
29
+ super(arg, encodingOrOffset, length);
30
+ }
31
+ else if (Object.prototype.toString.call(arg) === '[object ArrayBuffer]') {
32
+ super(arg, encodingOrOffset, length);
33
+ }
34
+ else if (typeof arg === 'object' && arg !== null && arg.type === 'Buffer' && Array.isArray(arg.data)) {
35
+ // Buffer.from({ type: 'Buffer', data: [...] })
36
+ super(arg.data);
37
+ }
38
+ else {
39
+ super(arg);
40
+ }
41
+ }
42
+ get parent() {
43
+ return this.buffer;
44
+ }
45
+ static from(value, encodingOrOffset, length) {
46
+ if (typeof value === 'string') {
47
+ return new Buffer(value, encodingOrOffset);
48
+ }
49
+ if (value instanceof Uint8Array) {
50
+ // Copy
51
+ const buf = new Buffer(value.length);
52
+ buf.set(value);
53
+ return buf;
54
+ }
55
+ if (value instanceof ArrayBuffer) {
56
+ return new Buffer(value, encodingOrOffset, length);
57
+ }
58
+ if (Array.isArray(value)) {
59
+ return new Buffer(new Uint8Array(value).buffer);
60
+ }
61
+ throw new TypeError('Unsupported type for Buffer.from');
62
+ }
63
+ static alloc(size, fill, encoding) {
64
+ // Fast path: use Uint8Array directly to avoid Buffer constructor overhead
65
+ const buf = new Uint8Array(size);
66
+ Object.setPrototypeOf(buf, Buffer.prototype);
67
+ if (fill !== undefined) {
68
+ buf.fill(fill, encoding);
69
+ }
70
+ return buf;
71
+ }
72
+ static allocUnsafe(size) {
73
+ // In JS, memory is always zeroed for security, so allocUnsafe is same as alloc usually.
74
+ // Unless we recycled buffers (not implemented).
75
+ // Using native allocUnsafe might give uninitialized memory, but bridge overhead (~0.05ms)
76
+ // might outweight the zeroing cost (~0.02ms).
77
+ // So we stick to JS native alloc for now.
78
+ const buf = new Uint8Array(size);
79
+ Object.setPrototypeOf(buf, Buffer.prototype);
80
+ return buf;
81
+ }
82
+ static allocUnsafeSlow(size) {
83
+ return Buffer.allocUnsafe(size);
84
+ }
85
+ static byteLength(string, encoding = 'utf8') {
86
+ return getNative().byteLength(string, encoding);
87
+ }
88
+ static isBuffer(obj) {
89
+ return obj instanceof Buffer;
90
+ }
91
+ static compare(buf1, buf2) {
92
+ return getNative().compare(buf1.buffer, buf1.byteOffset, buf1.byteLength, buf2.buffer, buf2.byteOffset, buf2.byteLength);
93
+ }
94
+ static copyBytesFrom(view, offset, length) {
95
+ if (offset === undefined)
96
+ offset = 0;
97
+ if (length === undefined)
98
+ length = view.byteLength - offset;
99
+ if (offset < 0 || length < 0 || offset + length > view.byteLength) {
100
+ throw new RangeError('offset or length out of bounds');
101
+ }
102
+ const buf = Buffer.allocUnsafe(length);
103
+ const src = new Uint8Array(view.buffer, view.byteOffset + offset, length);
104
+ buf.set(src);
105
+ return buf;
106
+ }
107
+ static concat(list, totalLength) {
108
+ if (totalLength === undefined) {
109
+ totalLength = list.reduce((acc, curr) => acc + curr.length, 0);
110
+ }
111
+ const buf = Buffer.allocUnsafe(totalLength);
112
+ let offset = 0;
113
+ for (const item of list) {
114
+ buf.set(item, offset);
115
+ offset += item.length;
116
+ }
117
+ return buf;
118
+ }
119
+ write(string, offset, length, encoding) {
120
+ // Argument handling
121
+ if (offset === undefined) {
122
+ offset = 0;
123
+ length = this.length;
124
+ encoding = 'utf8';
125
+ }
126
+ else if (length === undefined && typeof offset === 'string') {
127
+ encoding = offset;
128
+ offset = 0;
129
+ length = this.length;
130
+ }
131
+ else if (length === undefined) {
132
+ length = this.length - offset;
133
+ encoding = 'utf8';
134
+ }
135
+ else if (encoding === undefined) {
136
+ encoding = 'utf8';
137
+ }
138
+ return getNative().write(this.buffer, string, this.byteOffset + offset, length, encoding);
139
+ }
140
+ toString(encoding, start, end) {
141
+ if (encoding === undefined)
142
+ encoding = 'utf8';
143
+ if (start === undefined)
144
+ start = 0;
145
+ if (end === undefined)
146
+ end = this.length;
147
+ if (start < 0)
148
+ start = 0;
149
+ if (end > this.length)
150
+ end = this.length;
151
+ if (start >= end)
152
+ return '';
153
+ return getNative().decode(this.buffer, this.byteOffset + start, end - start, encoding);
154
+ }
155
+ indexOf(value, byteOffset, encoding) {
156
+ if (typeof value === 'string') {
157
+ const needle = Buffer.from(value, encoding);
158
+ return getNative().indexOfBuffer(this.buffer, needle.buffer, byteOffset || 0, this.length);
159
+ }
160
+ if (value instanceof Uint8Array) {
161
+ return getNative().indexOfBuffer(this.buffer, value.buffer, byteOffset || 0, this.length);
162
+ }
163
+ if (typeof value === 'number') {
164
+ return getNative().indexOf(this.buffer, value, byteOffset || 0, this.length);
165
+ }
166
+ throw new TypeError('"value" argument must be string, number or Buffer');
167
+ }
168
+ lastIndexOf(value, byteOffset, encoding) {
169
+ // Node.js defaults byteOffset to buf.length - 1
170
+ // If byteOffset is provided, it is the index to start searching backwards from.
171
+ // We implement this by defining a window [0, limit] where limit is calculated.
172
+ if (byteOffset === undefined || byteOffset > this.length) {
173
+ byteOffset = this.length;
174
+ }
175
+ if (typeof value === 'string') {
176
+ const needle = Buffer.from(value, encoding);
177
+ if (needle.length === 0)
178
+ return -1;
179
+ // We search in [0, byteOffset + needle.length].
180
+ // The match must start at <= byteOffset.
181
+ // So the match must be fully contained in [0, byteOffset + needle.length].
182
+ return getNative().lastIndexOfBuffer(this.buffer, needle.buffer, 0, byteOffset + needle.length);
183
+ }
184
+ if (value instanceof Uint8Array) {
185
+ if (value.length === 0)
186
+ return -1;
187
+ return getNative().lastIndexOfBuffer(this.buffer, value.buffer, 0, byteOffset + value.length);
188
+ }
189
+ if (typeof value === 'number') {
190
+ // Search in [0, byteOffset + 1]. The last byte checked is at byteOffset.
191
+ return getNative().lastIndexOfByte(this.buffer, value, 0, byteOffset + 1);
192
+ }
193
+ throw new TypeError('"value" argument must be string, number or Buffer');
194
+ }
195
+ includes(value, byteOffset, encoding) {
196
+ return this.indexOf(value, byteOffset, encoding) !== -1;
197
+ }
198
+ fill(value, offset, end, encoding) {
199
+ // Handle argument shifting
200
+ if (typeof offset === 'string') {
201
+ encoding = offset;
202
+ offset = 0;
203
+ end = this.length;
204
+ }
205
+ else if (typeof end === 'string') {
206
+ encoding = end;
207
+ end = this.length;
208
+ }
209
+ if (offset === undefined)
210
+ offset = 0;
211
+ if (end === undefined)
212
+ end = this.length;
213
+ const len = end - offset;
214
+ if (len <= 0)
215
+ return this;
216
+ if (typeof value === 'number') {
217
+ getNative().fill(this.buffer, value, this.byteOffset + offset, len);
218
+ return this;
219
+ }
220
+ let valBuf;
221
+ if (typeof value === 'string') {
222
+ valBuf = Buffer.from(value, encoding);
223
+ }
224
+ else if (value instanceof Uint8Array) {
225
+ valBuf = value;
226
+ }
227
+ else {
228
+ throw new TypeError('"value" argument must be string, number or Buffer');
229
+ }
230
+ if (valBuf.length === 0)
231
+ return this;
232
+ // Native fillBuffer takes ArrayBuffer, assumes generic fill logic
233
+ getNative().fillBuffer(this.buffer, valBuf.buffer, this.byteOffset + offset, len);
234
+ return this;
235
+ }
236
+ compare(target, targetStart, targetEnd, sourceStart, sourceEnd) {
237
+ if (targetStart === undefined)
238
+ targetStart = 0;
239
+ if (targetEnd === undefined)
240
+ targetEnd = target.length;
241
+ if (sourceStart === undefined)
242
+ sourceStart = 0;
243
+ if (sourceEnd === undefined)
244
+ sourceEnd = this.length;
245
+ return getNative().compare(this.buffer, this.byteOffset + sourceStart, sourceEnd - sourceStart, target.buffer, target.byteOffset + targetStart, targetEnd - targetStart);
246
+ }
247
+ slice(start, end) {
248
+ // Uint8Array.prototype.slice returns a copy (new buffer).
249
+ // Buffer.prototype.slice (legacy) returned a view.
250
+ // Buffer.prototype.subarray returns a view.
251
+ // In Node.js Buffer.slice is deprecated and acts like subarray? No, Buffer.slice returns a view (unlike Uint8Array.slice which returns copy).
252
+ // Wait, Node Buffer.slice returns a NEW Buffer that references the SAME memory.
253
+ // But Uint8Array.prototype.slice returns a COPY.
254
+ // So we should override slice to return a view (subarray behavior) but wrapped in Buffer?
255
+ // Actually Node documentation says: "The Buffer.prototype.slice() method is deprecated... use subarray() instead."
256
+ // BUT for compatibility, often libs expect slice to duplicate memory? No, Buffer.slice(start, end) shares memory.
257
+ // Uint8Array.prototype.slice(start, end) copies memory.
258
+ // So we should override slice to be compatible with Node behavior (view), OR standard Uint8Array behavior (copy).
259
+ // Node's `slice` shares memory. `Uint8Array`'s `slice` copies. This is a conflict.
260
+ // Let's implement `subarray` which returns a Buffer view.
261
+ const sub = this.subarray(start, end);
262
+ // sub is Uint8Array. We need to cast it to Buffer.
263
+ // We can use Object.setPrototypeOf or just new Buffer via constructor that takes Uint8Array
264
+ // but that constructor usually copies?
265
+ // `new Buffer(uint8Array)` copies.
266
+ // We need `new Buffer(arrayBuffer, byteOffset, length)`.
267
+ return new Buffer(sub.buffer, sub.byteOffset, sub.byteLength);
268
+ }
269
+ subarray(begin, end) {
270
+ const sub = super.subarray(begin, end);
271
+ // Ensure we return a Buffer, not just a Uint8Array
272
+ if (sub instanceof Buffer)
273
+ return sub;
274
+ // Wrap the Uint8Array's memory view into a new Buffer
275
+ // Note: new Buffer(arrayBuffer, byteOffset, length) creates a view
276
+ return new Buffer(sub.buffer, sub.byteOffset, sub.byteLength);
277
+ }
278
+ // ================== Read Methods ==================
279
+ readInt8(offset = 0) {
280
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt8(offset);
281
+ }
282
+ readUInt8(offset = 0) {
283
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint8(offset);
284
+ }
285
+ readInt16LE(offset = 0) {
286
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(offset, true);
287
+ }
288
+ readInt16BE(offset = 0) {
289
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(offset, false);
290
+ }
291
+ readUInt16LE(offset = 0) {
292
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint16(offset, true);
293
+ }
294
+ readUInt16BE(offset = 0) {
295
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint16(offset, false);
296
+ }
297
+ readInt32LE(offset = 0) {
298
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(offset, true);
299
+ }
300
+ readInt32BE(offset = 0) {
301
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(offset, false);
302
+ }
303
+ readUInt32LE(offset = 0) {
304
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint32(offset, true);
305
+ }
306
+ readUInt32BE(offset = 0) {
307
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint32(offset, false);
308
+ }
309
+ readBigInt64LE(offset = 0) {
310
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigInt64(offset, true);
311
+ }
312
+ readBigInt64BE(offset = 0) {
313
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigInt64(offset, false);
314
+ }
315
+ readBigUInt64LE(offset = 0) {
316
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigUint64(offset, true);
317
+ }
318
+ readBigUInt64BE(offset = 0) {
319
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getBigUint64(offset, false);
320
+ }
321
+ readFloatLE(offset = 0) {
322
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat32(offset, true);
323
+ }
324
+ readFloatBE(offset = 0) {
325
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat32(offset, false);
326
+ }
327
+ readDoubleLE(offset = 0) {
328
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat64(offset, true);
329
+ }
330
+ readDoubleBE(offset = 0) {
331
+ return new DataView(this.buffer, this.byteOffset, this.byteLength).getFloat64(offset, false);
332
+ }
333
+ readIntLE(offset, byteLength) {
334
+ if (offset === undefined)
335
+ throw new TypeError('"offset" is invalid');
336
+ if (byteLength > 6 || byteLength < 0)
337
+ throw new RangeError('"byteLength" out of range');
338
+ offset = offset >>> 0;
339
+ byteLength = byteLength >>> 0;
340
+ let val = this[offset];
341
+ let mul = 1;
342
+ let i = 0;
343
+ while (++i < byteLength && (mul *= 0x100)) {
344
+ val += this[offset + i] * mul;
345
+ }
346
+ mul *= 0x80;
347
+ if (val >= mul)
348
+ val -= Math.pow(2, 8 * byteLength);
349
+ return val;
350
+ }
351
+ readIntBE(offset, byteLength) {
352
+ if (offset === undefined)
353
+ throw new TypeError('"offset" is invalid');
354
+ if (byteLength > 6 || byteLength < 0)
355
+ throw new RangeError('"byteLength" out of range');
356
+ offset = offset >>> 0;
357
+ byteLength = byteLength >>> 0;
358
+ let i = byteLength;
359
+ let mul = 1;
360
+ let val = this[offset + --i];
361
+ while (i > 0 && (mul *= 0x100)) {
362
+ val += this[offset + --i] * mul;
363
+ }
364
+ mul *= 0x80;
365
+ if (val >= mul)
366
+ val -= Math.pow(2, 8 * byteLength);
367
+ return val;
368
+ }
369
+ readUIntLE(offset, byteLength) {
370
+ if (offset === undefined)
371
+ throw new TypeError('"offset" is invalid');
372
+ if (byteLength > 6 || byteLength < 0)
373
+ throw new RangeError('"byteLength" out of range');
374
+ offset = offset >>> 0;
375
+ byteLength = byteLength >>> 0;
376
+ let val = this[offset];
377
+ let mul = 1;
378
+ let i = 0;
379
+ while (++i < byteLength && (mul *= 0x100)) {
380
+ val += this[offset + i] * mul;
381
+ }
382
+ return val;
383
+ }
384
+ readUIntBE(offset, byteLength) {
385
+ if (offset === undefined)
386
+ throw new TypeError('"offset" is invalid');
387
+ if (byteLength > 6 || byteLength < 0)
388
+ throw new RangeError('"byteLength" out of range');
389
+ offset = offset >>> 0;
390
+ byteLength = byteLength >>> 0;
391
+ let i = byteLength;
392
+ let mul = 1;
393
+ let val = this[offset + --i];
394
+ while (i > 0 && (mul *= 0x100)) {
395
+ val += this[offset + --i] * mul;
396
+ }
397
+ return val;
398
+ }
399
+ // ================== Write Methods ==================
400
+ writeInt8(value, offset = 0) {
401
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setInt8(offset, value);
402
+ return offset + 1;
403
+ }
404
+ writeUInt8(value, offset = 0) {
405
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setUint8(offset, value);
406
+ return offset + 1;
407
+ }
408
+ writeInt16LE(value, offset = 0) {
409
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(offset, value, true);
410
+ return offset + 2;
411
+ }
412
+ writeInt16BE(value, offset = 0) {
413
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(offset, value, false);
414
+ return offset + 2;
415
+ }
416
+ writeUInt16LE(value, offset = 0) {
417
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(offset, value, true);
418
+ return offset + 2;
419
+ }
420
+ writeUInt16BE(value, offset = 0) {
421
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(offset, value, false);
422
+ return offset + 2;
423
+ }
424
+ writeInt32LE(value, offset = 0) {
425
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32(offset, value, true);
426
+ return offset + 4;
427
+ }
428
+ writeInt32BE(value, offset = 0) {
429
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32(offset, value, false);
430
+ return offset + 4;
431
+ }
432
+ writeUInt32LE(value, offset = 0) {
433
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(offset, value, true);
434
+ return offset + 4;
435
+ }
436
+ writeUInt32BE(value, offset = 0) {
437
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(offset, value, false);
438
+ return offset + 4;
439
+ }
440
+ writeBigInt64LE(value, offset = 0) {
441
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(offset, value, true);
442
+ return offset + 8;
443
+ }
444
+ writeBigInt64BE(value, offset = 0) {
445
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(offset, value, false);
446
+ return offset + 8;
447
+ }
448
+ writeBigUInt64LE(value, offset = 0) {
449
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(offset, value, true);
450
+ return offset + 8;
451
+ }
452
+ writeBigUInt64BE(value, offset = 0) {
453
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(offset, value, false);
454
+ return offset + 8;
455
+ }
456
+ writeFloatLE(value, offset = 0) {
457
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(offset, value, true);
458
+ return offset + 4;
459
+ }
460
+ writeFloatBE(value, offset = 0) {
461
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(offset, value, false);
462
+ return offset + 4;
463
+ }
464
+ writeDoubleLE(value, offset = 0) {
465
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(offset, value, true);
466
+ return offset + 8;
467
+ }
468
+ writeDoubleBE(value, offset = 0) {
469
+ new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(offset, value, false);
470
+ return offset + 8;
471
+ }
472
+ writeIntLE(value, offset, byteLength) {
473
+ if (offset === undefined)
474
+ throw new TypeError('"offset" is invalid');
475
+ if (byteLength > 6 || byteLength < 0)
476
+ throw new RangeError('"byteLength" out of range');
477
+ offset = offset >>> 0;
478
+ byteLength = byteLength >>> 0;
479
+ let i = 0;
480
+ let mul = 1;
481
+ let sub = 0;
482
+ this[offset] = value & 0xFF;
483
+ while (++i < byteLength && (mul *= 0x100)) {
484
+ if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
485
+ sub = 1;
486
+ }
487
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
488
+ }
489
+ return offset + byteLength;
490
+ }
491
+ writeIntBE(value, offset, byteLength) {
492
+ if (offset === undefined)
493
+ throw new TypeError('"offset" is invalid');
494
+ if (byteLength > 6 || byteLength < 0)
495
+ throw new RangeError('"byteLength" out of range');
496
+ offset = offset >>> 0;
497
+ byteLength = byteLength >>> 0;
498
+ let i = byteLength - 1;
499
+ let mul = 1;
500
+ let sub = 0;
501
+ this[offset + i] = value & 0xFF;
502
+ while (--i >= 0 && (mul *= 0x100)) {
503
+ if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
504
+ sub = 1;
505
+ }
506
+ this[offset + i] = ((value / mul) >> 0) - sub & 0xFF;
507
+ }
508
+ return offset + byteLength;
509
+ }
510
+ writeUIntLE(value, offset, byteLength) {
511
+ if (offset === undefined)
512
+ throw new TypeError('"offset" is invalid');
513
+ if (byteLength > 6 || byteLength < 0)
514
+ throw new RangeError('"byteLength" out of range');
515
+ offset = offset >>> 0;
516
+ byteLength = byteLength >>> 0;
517
+ let i = 0;
518
+ let mul = 1;
519
+ this[offset] = value & 0xFF;
520
+ while (++i < byteLength && (mul *= 0x100)) {
521
+ this[offset + i] = (value / mul) & 0xFF;
522
+ }
523
+ return offset + byteLength;
524
+ }
525
+ writeUIntBE(value, offset, byteLength) {
526
+ if (offset === undefined)
527
+ throw new TypeError('"offset" is invalid');
528
+ if (byteLength > 6 || byteLength < 0)
529
+ throw new RangeError('"byteLength" out of range');
530
+ offset = offset >>> 0;
531
+ byteLength = byteLength >>> 0;
532
+ let i = byteLength - 1;
533
+ let mul = 1;
534
+ this[offset + i] = value & 0xFF;
535
+ while (--i >= 0 && (mul *= 0x100)) {
536
+ this[offset + i] = (value / mul) & 0xFF;
537
+ }
538
+ return offset + byteLength;
539
+ }
540
+ // ================== Utils ==================
541
+ toJSON() {
542
+ return {
543
+ type: 'Buffer',
544
+ data: Array.from(this)
545
+ };
546
+ }
547
+ static isEncoding(encoding) {
548
+ switch (encoding.toLowerCase()) {
549
+ case 'utf8':
550
+ case 'utf-8':
551
+ case 'hex':
552
+ case 'base64':
553
+ case 'binary':
554
+ case 'latin1':
555
+ case 'ascii':
556
+ case 'utf16le':
557
+ case 'ucs2':
558
+ return true;
559
+ default:
560
+ return false;
561
+ }
562
+ }
563
+ swap16() {
564
+ const len = this.length;
565
+ if (len % 2 !== 0)
566
+ throw new RangeError('Buffer size must be a multiple of 16-bits');
567
+ for (let i = 0; i < len; i += 2) {
568
+ const v = this[i];
569
+ this[i] = this[i + 1];
570
+ this[i + 1] = v;
571
+ }
572
+ return this;
573
+ }
574
+ swap32() {
575
+ const len = this.length;
576
+ if (len % 4 !== 0)
577
+ throw new RangeError('Buffer size must be a multiple of 32-bits');
578
+ for (let i = 0; i < len; i += 4) {
579
+ const v0 = this[i];
580
+ const v1 = this[i + 1];
581
+ const v2 = this[i + 2];
582
+ const v3 = this[i + 3];
583
+ this[i] = v3;
584
+ this[i + 1] = v2;
585
+ this[i + 2] = v1;
586
+ this[i + 3] = v0;
587
+ }
588
+ return this;
589
+ }
590
+ swap64() {
591
+ const len = this.length;
592
+ if (len % 8 !== 0)
593
+ throw new RangeError('Buffer size must be a multiple of 64-bits');
594
+ for (let i = 0; i < len; i += 8) {
595
+ const v0 = this[i];
596
+ const v1 = this[i + 1];
597
+ const v2 = this[i + 2];
598
+ const v3 = this[i + 3];
599
+ const v4 = this[i + 4];
600
+ const v5 = this[i + 5];
601
+ const v6 = this[i + 6];
602
+ const v7 = this[i + 7];
603
+ this[i] = v7;
604
+ this[i + 1] = v6;
605
+ this[i + 2] = v5;
606
+ this[i + 3] = v4;
607
+ this[i + 4] = v3;
608
+ this[i + 5] = v2;
609
+ this[i + 6] = v1;
610
+ this[i + 7] = v0;
611
+ }
612
+ return this;
613
+ }
614
+ copy(target, targetStart, sourceStart, sourceEnd) {
615
+ if (!target)
616
+ throw new TypeError('argument must be a Buffer');
617
+ if (targetStart === undefined)
618
+ targetStart = 0;
619
+ if (sourceStart === undefined)
620
+ sourceStart = 0;
621
+ if (sourceEnd === undefined)
622
+ sourceEnd = this.length;
623
+ if (targetStart >= target.length)
624
+ return 0;
625
+ if (sourceStart >= sourceEnd)
626
+ return 0;
627
+ if (sourceStart < 0)
628
+ sourceStart = 0;
629
+ if (sourceEnd > this.length)
630
+ sourceEnd = this.length;
631
+ if (targetStart < 0)
632
+ targetStart = 0;
633
+ let len = sourceEnd - sourceStart;
634
+ if (target.length - targetStart < len) {
635
+ len = target.length - targetStart;
636
+ }
637
+ target.set(this.subarray(sourceStart, sourceStart + len), targetStart);
638
+ return len;
639
+ }
640
+ }
641
+ exports.Buffer = Buffer;
642
+ Buffer.poolSize = 8192;
@@ -0,0 +1,18 @@
1
+ import { type HybridObject } from 'react-native-nitro-modules';
2
+ export interface NitroBuffer extends HybridObject<{
3
+ ios: 'c++';
4
+ android: 'c++';
5
+ }> {
6
+ alloc(size: number): ArrayBuffer;
7
+ allocUnsafe(size: number): ArrayBuffer;
8
+ byteLength(string: string, encoding: string): number;
9
+ write(buffer: ArrayBuffer, string: string, offset: number, length: number, encoding: string): number;
10
+ decode(buffer: ArrayBuffer, offset: number, length: number, encoding: string): string;
11
+ compare(a: ArrayBuffer, aOffset: number, aLength: number, b: ArrayBuffer, bOffset: number, bLength: number): number;
12
+ fill(buffer: ArrayBuffer, value: number, offset: number, length: number): void;
13
+ indexOf(buffer: ArrayBuffer, value: number, offset: number, length: number): number;
14
+ indexOfBuffer(buffer: ArrayBuffer, needle: ArrayBuffer, offset: number, length: number): number;
15
+ lastIndexOfByte(buffer: ArrayBuffer, value: number, offset: number, length: number): number;
16
+ lastIndexOfBuffer(buffer: ArrayBuffer, needle: ArrayBuffer, offset: number, length: number): number;
17
+ fillBuffer(buffer: ArrayBuffer, value: ArrayBuffer, offset: number, length: number): void;
18
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { Buffer } from './Buffer';
2
+ export * from './utils';
3
+ export declare const INSPECT_MAX_BYTES = 50;
4
+ export declare const kMaxLength = 2147483647;
5
+ export declare const kStringMaxLength = 536870888;
6
+ export declare const constants: {
7
+ MAX_LENGTH: number;
8
+ MAX_STRING_LENGTH: number;
9
+ };
10
+ export declare class SlowBuffer extends Uint8Array {
11
+ constructor(size: number);
12
+ }