@btc-vision/bitcoin 7.0.0-alpha.2 → 7.0.0-alpha.4

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 (134) hide show
  1. package/README.md +455 -155
  2. package/browser/chunks/WorkerSigningPool.sequential-DHha7j0b.js +113 -0
  3. package/browser/ecc/context.d.ts +22 -21
  4. package/browser/ecc/context.d.ts.map +1 -1
  5. package/browser/ecc/index.d.ts +1 -1
  6. package/browser/ecc/index.d.ts.map +1 -1
  7. package/browser/ecc/types.d.ts +10 -123
  8. package/browser/ecc/types.d.ts.map +1 -1
  9. package/browser/env.d.ts +13 -0
  10. package/browser/env.d.ts.map +1 -0
  11. package/browser/index.d.ts +3 -3
  12. package/browser/index.d.ts.map +1 -1
  13. package/browser/index.js +5790 -4295
  14. package/browser/io/index.d.ts +0 -1
  15. package/browser/io/index.d.ts.map +1 -1
  16. package/browser/payments/p2tr.d.ts.map +1 -1
  17. package/browser/psbt/types.d.ts +2 -68
  18. package/browser/psbt/types.d.ts.map +1 -1
  19. package/browser/psbt.d.ts +9 -11
  20. package/browser/psbt.d.ts.map +1 -1
  21. package/browser/types.d.ts +1 -1
  22. package/browser/types.d.ts.map +1 -1
  23. package/browser/workers/WorkerSigningPool.d.ts +6 -0
  24. package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
  25. package/browser/workers/WorkerSigningPool.node.d.ts +6 -0
  26. package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
  27. package/browser/workers/WorkerSigningPool.sequential.d.ts +69 -0
  28. package/browser/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  29. package/browser/workers/WorkerSigningPool.worklet.d.ts +64 -0
  30. package/browser/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  31. package/browser/workers/index.d.ts +2 -2
  32. package/browser/workers/index.d.ts.map +1 -1
  33. package/browser/workers/index.react-native.d.ts +28 -0
  34. package/browser/workers/index.react-native.d.ts.map +1 -0
  35. package/browser/workers/psbt-parallel.d.ts +2 -3
  36. package/browser/workers/psbt-parallel.d.ts.map +1 -1
  37. package/browser/workers/types.d.ts +12 -0
  38. package/browser/workers/types.d.ts.map +1 -1
  39. package/build/ecc/context.d.ts +22 -21
  40. package/build/ecc/context.d.ts.map +1 -1
  41. package/build/ecc/context.js +19 -114
  42. package/build/ecc/context.js.map +1 -1
  43. package/build/ecc/index.d.ts +1 -1
  44. package/build/ecc/index.d.ts.map +1 -1
  45. package/build/ecc/types.d.ts +7 -126
  46. package/build/ecc/types.d.ts.map +1 -1
  47. package/build/ecc/types.js +4 -1
  48. package/build/ecc/types.js.map +1 -1
  49. package/build/env.d.ts +13 -0
  50. package/build/env.d.ts.map +1 -0
  51. package/build/env.js +198 -0
  52. package/build/env.js.map +1 -0
  53. package/build/index.d.ts +4 -3
  54. package/build/index.d.ts.map +1 -1
  55. package/build/index.js +2 -1
  56. package/build/index.js.map +1 -1
  57. package/build/io/index.d.ts +0 -1
  58. package/build/io/index.d.ts.map +1 -1
  59. package/build/io/index.js +0 -2
  60. package/build/io/index.js.map +1 -1
  61. package/build/payments/p2tr.d.ts.map +1 -1
  62. package/build/payments/p2tr.js +2 -3
  63. package/build/payments/p2tr.js.map +1 -1
  64. package/build/psbt/types.d.ts +2 -68
  65. package/build/psbt/types.d.ts.map +1 -1
  66. package/build/psbt.d.ts +9 -11
  67. package/build/psbt.d.ts.map +1 -1
  68. package/build/psbt.js +38 -53
  69. package/build/psbt.js.map +1 -1
  70. package/build/tsconfig.build.tsbuildinfo +1 -1
  71. package/build/types.d.ts +1 -1
  72. package/build/types.d.ts.map +1 -1
  73. package/build/types.js +2 -16
  74. package/build/types.js.map +1 -1
  75. package/build/workers/WorkerSigningPool.d.ts +6 -0
  76. package/build/workers/WorkerSigningPool.d.ts.map +1 -1
  77. package/build/workers/WorkerSigningPool.js +8 -0
  78. package/build/workers/WorkerSigningPool.js.map +1 -1
  79. package/build/workers/WorkerSigningPool.node.d.ts +6 -0
  80. package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
  81. package/build/workers/WorkerSigningPool.node.js +9 -2
  82. package/build/workers/WorkerSigningPool.node.js.map +1 -1
  83. package/build/workers/WorkerSigningPool.sequential.d.ts +78 -0
  84. package/build/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  85. package/build/workers/WorkerSigningPool.sequential.js +160 -0
  86. package/build/workers/WorkerSigningPool.sequential.js.map +1 -0
  87. package/build/workers/WorkerSigningPool.worklet.d.ts +79 -0
  88. package/build/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  89. package/build/workers/WorkerSigningPool.worklet.js +388 -0
  90. package/build/workers/WorkerSigningPool.worklet.js.map +1 -0
  91. package/build/workers/index.d.ts +2 -2
  92. package/build/workers/index.d.ts.map +1 -1
  93. package/build/workers/index.js +9 -0
  94. package/build/workers/index.js.map +1 -1
  95. package/build/workers/index.react-native.d.ts +28 -0
  96. package/build/workers/index.react-native.d.ts.map +1 -0
  97. package/build/workers/index.react-native.js +67 -0
  98. package/build/workers/index.react-native.js.map +1 -0
  99. package/build/workers/psbt-parallel.d.ts +2 -3
  100. package/build/workers/psbt-parallel.d.ts.map +1 -1
  101. package/build/workers/psbt-parallel.js +4 -4
  102. package/build/workers/psbt-parallel.js.map +1 -1
  103. package/build/workers/types.d.ts +12 -0
  104. package/build/workers/types.d.ts.map +1 -1
  105. package/package.json +14 -4
  106. package/src/ecc/context.ts +26 -147
  107. package/src/ecc/index.ts +2 -2
  108. package/src/ecc/types.ts +7 -138
  109. package/src/env.ts +237 -0
  110. package/src/index.ts +2 -4
  111. package/src/io/index.ts +0 -3
  112. package/src/payments/p2tr.ts +2 -2
  113. package/src/psbt/types.ts +2 -84
  114. package/src/psbt.ts +63 -121
  115. package/src/types.ts +5 -28
  116. package/src/workers/WorkerSigningPool.node.ts +10 -2
  117. package/src/workers/WorkerSigningPool.sequential.ts +190 -0
  118. package/src/workers/WorkerSigningPool.ts +9 -0
  119. package/src/workers/WorkerSigningPool.worklet.ts +519 -0
  120. package/src/workers/index.react-native.ts +110 -0
  121. package/src/workers/index.ts +10 -1
  122. package/src/workers/psbt-parallel.ts +8 -8
  123. package/src/workers/types.ts +16 -0
  124. package/test/env.spec.ts +418 -0
  125. package/test/workers-pool.spec.ts +43 -0
  126. package/test/workers-sequential.spec.ts +669 -0
  127. package/test/workers-worklet.spec.ts +500 -0
  128. package/browser/io/MemoryPool.d.ts +0 -220
  129. package/browser/io/MemoryPool.d.ts.map +0 -1
  130. package/build/io/MemoryPool.d.ts +0 -220
  131. package/build/io/MemoryPool.d.ts.map +0 -1
  132. package/build/io/MemoryPool.js +0 -309
  133. package/build/io/MemoryPool.js.map +0 -1
  134. package/src/io/MemoryPool.ts +0 -343
@@ -1,309 +0,0 @@
1
- /**
2
- * Memory pool using SharedArrayBuffer for zero-allocation operations.
3
- *
4
- * Provides thread-safe memory allocation using Atomics for concurrent access.
5
- * Ideal for parsing operations where temporary buffers are needed.
6
- *
7
- * @packageDocumentation
8
- */
9
- /**
10
- * Thread-safe memory pool using SharedArrayBuffer.
11
- *
12
- * Allocates memory from a pre-allocated SharedArrayBuffer pool,
13
- * eliminating garbage collection pressure from repeated allocations.
14
- *
15
- * Uses Atomics for thread-safe allocation when used with Workers.
16
- *
17
- * @example
18
- * ```typescript
19
- * import { MemoryPool } from '@btc-vision/bitcoin';
20
- *
21
- * // Create a 1MB pool
22
- * const pool = new MemoryPool(1024 * 1024);
23
- *
24
- * // Allocate memory for transaction parsing
25
- * const txBuffer = pool.alloc(500);
26
- * // ... use txBuffer ...
27
- *
28
- * // Reset pool when done (all allocations become invalid)
29
- * pool.reset();
30
- * ```
31
- */
32
- export class MemoryPool {
33
- /**
34
- * The underlying SharedArrayBuffer.
35
- */
36
- #buffer;
37
- /**
38
- * View for data allocations.
39
- */
40
- #data;
41
- /**
42
- * Control array for atomic offset management.
43
- * Located at the end of the buffer.
44
- */
45
- #control;
46
- /**
47
- * Maximum allocatable size (buffer size minus control bytes).
48
- */
49
- #maxSize;
50
- /**
51
- * Creates a new MemoryPool.
52
- *
53
- * @param size - Size of the pool in bytes (default 4MB)
54
- *
55
- * @example
56
- * ```typescript
57
- * import { MemoryPool } from '@btc-vision/bitcoin';
58
- *
59
- * // 4MB pool (default)
60
- * const pool = new MemoryPool();
61
- *
62
- * // 16MB pool
63
- * const largePool = new MemoryPool(16 * 1024 * 1024);
64
- * ```
65
- */
66
- constructor(size = 4 * 1024 * 1024) {
67
- // Add 4 bytes for atomic control (current offset)
68
- const totalSize = size + 4;
69
- this.#buffer = new SharedArrayBuffer(totalSize);
70
- this.#data = new Uint8Array(this.#buffer, 0, size);
71
- this.#control = new Int32Array(this.#buffer, size, 1);
72
- this.#maxSize = size;
73
- }
74
- /**
75
- * Total capacity of the pool in bytes.
76
- */
77
- get capacity() {
78
- return this.#maxSize;
79
- }
80
- /**
81
- * Current allocation offset (bytes used).
82
- *
83
- * Uses Atomics for thread-safe reading.
84
- */
85
- get used() {
86
- return Atomics.load(this.#control, 0);
87
- }
88
- /**
89
- * Remaining available bytes.
90
- */
91
- get available() {
92
- return this.#maxSize - this.used;
93
- }
94
- /**
95
- * The underlying SharedArrayBuffer.
96
- *
97
- * Can be transferred to Workers for shared memory access.
98
- */
99
- get sharedBuffer() {
100
- return this.#buffer;
101
- }
102
- /**
103
- * Creates a MemoryPool from an existing SharedArrayBuffer.
104
- *
105
- * Useful for sharing a pool between Workers.
106
- *
107
- * @param buffer - Existing SharedArrayBuffer
108
- * @returns A new MemoryPool instance wrapping the buffer
109
- *
110
- * @example
111
- * ```typescript
112
- * // Main thread
113
- * const pool = new MemoryPool(1024 * 1024);
114
- * worker.postMessage({ buffer: pool.sharedBuffer });
115
- *
116
- * // Worker thread
117
- * self.onmessage = (e) => {
118
- * const pool = MemoryPool.fromSharedBuffer(e.data.buffer);
119
- * const mem = pool.alloc(100);
120
- * };
121
- * ```
122
- */
123
- static fromSharedBuffer(buffer) {
124
- const pool = Object.create(MemoryPool.prototype);
125
- const size = buffer.byteLength - 4;
126
- // Use Object.defineProperty to set private fields on the created object
127
- Object.defineProperty(pool, '#buffer', { value: buffer });
128
- Object.defineProperty(pool, '#data', { value: new Uint8Array(buffer, 0, size) });
129
- Object.defineProperty(pool, '#control', { value: new Int32Array(buffer, size, 1) });
130
- Object.defineProperty(pool, '#maxSize', { value: size });
131
- return pool;
132
- }
133
- /**
134
- * Allocates memory from the pool.
135
- *
136
- * Thread-safe using Atomics.add for concurrent access.
137
- *
138
- * @param size - Number of bytes to allocate
139
- * @returns Uint8Array view into the pool
140
- * @throws RangeError if pool is exhausted
141
- *
142
- * @example
143
- * ```typescript
144
- * const pool = new MemoryPool(1024);
145
- * const buf1 = pool.alloc(100); // First 100 bytes
146
- * const buf2 = pool.alloc(200); // Next 200 bytes
147
- * ```
148
- */
149
- alloc(size) {
150
- if (size <= 0) {
151
- throw new RangeError('Allocation size must be positive');
152
- }
153
- // Atomically reserve space
154
- const oldOffset = Atomics.add(this.#control, 0, size);
155
- if (oldOffset + size > this.#maxSize) {
156
- // Undo the allocation
157
- Atomics.sub(this.#control, 0, size);
158
- throw new RangeError(`MemoryPool exhausted: requested ${size} bytes, only ${this.#maxSize - oldOffset} available`);
159
- }
160
- return this.#data.subarray(oldOffset, oldOffset + size);
161
- }
162
- /**
163
- * Allocates memory and fills it with zeros.
164
- *
165
- * @param size - Number of bytes to allocate
166
- * @returns Uint8Array view into the pool, filled with zeros
167
- * @throws RangeError if pool is exhausted
168
- */
169
- allocZeroed(size) {
170
- const mem = this.alloc(size);
171
- mem.fill(0);
172
- return mem;
173
- }
174
- /**
175
- * Allocates memory for a specific typed array.
176
- *
177
- * @param length - Number of elements
178
- * @param bytesPerElement - Size of each element in bytes
179
- * @returns Uint8Array view (use constructor of target type on underlying buffer)
180
- * @throws RangeError if pool is exhausted
181
- *
182
- * @example
183
- * ```typescript
184
- * const pool = new MemoryPool(1024);
185
- * const bytes = pool.allocTyped(10, 4); // 40 bytes for 10 Uint32
186
- * const u32View = new Uint32Array(bytes.buffer, bytes.byteOffset, 10);
187
- * ```
188
- */
189
- allocTyped(length, bytesPerElement) {
190
- return this.alloc(length * bytesPerElement);
191
- }
192
- /**
193
- * Resets the pool, making all memory available again.
194
- *
195
- * WARNING: All previously allocated views become invalid.
196
- * Only call when you're sure no references are held.
197
- *
198
- * Thread-safe using Atomics.store.
199
- *
200
- * @example
201
- * ```typescript
202
- * const pool = new MemoryPool(1024);
203
- * const buf = pool.alloc(100);
204
- * // ... use buf ...
205
- * pool.reset(); // buf is now invalid!
206
- * ```
207
- */
208
- reset() {
209
- Atomics.store(this.#control, 0, 0);
210
- }
211
- /**
212
- * Checks if the pool can accommodate an allocation.
213
- *
214
- * @param size - Number of bytes needed
215
- * @returns True if allocation would succeed
216
- */
217
- canAlloc(size) {
218
- return this.available >= size;
219
- }
220
- }
221
- /**
222
- * Non-shared memory pool using regular ArrayBuffer.
223
- *
224
- * Use when SharedArrayBuffer is not available (e.g., browsers without
225
- * proper COOP/COEP headers) or when thread-safety is not needed.
226
- *
227
- * @example
228
- * ```typescript
229
- * import { SimpleMemoryPool } from '@btc-vision/bitcoin';
230
- *
231
- * const pool = new SimpleMemoryPool(1024 * 1024);
232
- * const buf = pool.alloc(100);
233
- * ```
234
- */
235
- export class SimpleMemoryPool {
236
- #data;
237
- #offset = 0;
238
- /**
239
- * Creates a new SimpleMemoryPool.
240
- *
241
- * @param size - Size of the pool in bytes
242
- */
243
- constructor(size) {
244
- this.#data = new Uint8Array(size);
245
- }
246
- /**
247
- * Total capacity of the pool in bytes.
248
- */
249
- get capacity() {
250
- return this.#data.length;
251
- }
252
- /**
253
- * Current allocation offset (bytes used).
254
- */
255
- get used() {
256
- return this.#offset;
257
- }
258
- /**
259
- * Remaining available bytes.
260
- */
261
- get available() {
262
- return this.#data.length - this.#offset;
263
- }
264
- /**
265
- * Allocates memory from the pool.
266
- *
267
- * @param size - Number of bytes to allocate
268
- * @returns Uint8Array view into the pool
269
- * @throws RangeError if pool is exhausted
270
- */
271
- alloc(size) {
272
- if (size <= 0) {
273
- throw new RangeError('Allocation size must be positive');
274
- }
275
- if (this.#offset + size > this.#data.length) {
276
- throw new RangeError(`SimpleMemoryPool exhausted: requested ${size} bytes, only ${this.available} available`);
277
- }
278
- const start = this.#offset;
279
- this.#offset += size;
280
- return this.#data.subarray(start, this.#offset);
281
- }
282
- /**
283
- * Allocates memory and fills it with zeros.
284
- *
285
- * @param size - Number of bytes to allocate
286
- * @returns Uint8Array view into the pool, filled with zeros
287
- */
288
- allocZeroed(size) {
289
- const mem = this.alloc(size);
290
- mem.fill(0);
291
- return mem;
292
- }
293
- /**
294
- * Resets the pool, making all memory available again.
295
- */
296
- reset() {
297
- this.#offset = 0;
298
- }
299
- /**
300
- * Checks if the pool can accommodate an allocation.
301
- *
302
- * @param size - Number of bytes needed
303
- * @returns True if allocation would succeed
304
- */
305
- canAlloc(size) {
306
- return this.available >= size;
307
- }
308
- }
309
- //# sourceMappingURL=MemoryPool.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MemoryPool.js","sourceRoot":"","sources":["../../src/io/MemoryPool.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,UAAU;IACnB;;OAEG;IACM,OAAO,CAAoB;IAEpC;;OAEG;IACM,KAAK,CAAa;IAE3B;;;OAGG;IACM,QAAQ,CAAa;IAE9B;;OAEG;IACM,QAAQ,CAAS;IAE1B;;;;;;;;;;;;;;;OAeG;IACH,YAAmB,OAAe,CAAC,GAAG,IAAI,GAAG,IAAI;QAC7C,kDAAkD;QAClD,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC,CAAC;QAC3B,IAAI,CAAC,OAAO,GAAG,IAAI,iBAAiB,CAAC,SAAS,CAAC,CAAC;QAChD,IAAI,CAAC,KAAK,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;QACnD,IAAI,CAAC,QAAQ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,CAAC;IACf,CAAC;IAED;;OAEG;IACI,KAAK;QACR,IAAI,CAAC,OAAO,GAAG,CAAC,CAAC;IACrB,CAAC;IAED;;;;;OAKG;IACI,QAAQ,CAAC,IAAY;QACxB,OAAO,IAAI,CAAC,SAAS,IAAI,IAAI,CAAC;IAClC,CAAC;CACJ"}
@@ -1,343 +0,0 @@
1
- /**
2
- * Memory pool using SharedArrayBuffer for zero-allocation operations.
3
- *
4
- * Provides thread-safe memory allocation using Atomics for concurrent access.
5
- * Ideal for parsing operations where temporary buffers are needed.
6
- *
7
- * @packageDocumentation
8
- */
9
-
10
- /**
11
- * Thread-safe memory pool using SharedArrayBuffer.
12
- *
13
- * Allocates memory from a pre-allocated SharedArrayBuffer pool,
14
- * eliminating garbage collection pressure from repeated allocations.
15
- *
16
- * Uses Atomics for thread-safe allocation when used with Workers.
17
- *
18
- * @example
19
- * ```typescript
20
- * import { MemoryPool } from '@btc-vision/bitcoin';
21
- *
22
- * // Create a 1MB pool
23
- * const pool = new MemoryPool(1024 * 1024);
24
- *
25
- * // Allocate memory for transaction parsing
26
- * const txBuffer = pool.alloc(500);
27
- * // ... use txBuffer ...
28
- *
29
- * // Reset pool when done (all allocations become invalid)
30
- * pool.reset();
31
- * ```
32
- */
33
- export class MemoryPool {
34
- /**
35
- * The underlying SharedArrayBuffer.
36
- */
37
- readonly #buffer: SharedArrayBuffer;
38
-
39
- /**
40
- * View for data allocations.
41
- */
42
- readonly #data: Uint8Array;
43
-
44
- /**
45
- * Control array for atomic offset management.
46
- * Located at the end of the buffer.
47
- */
48
- readonly #control: Int32Array;
49
-
50
- /**
51
- * Maximum allocatable size (buffer size minus control bytes).
52
- */
53
- readonly #maxSize: number;
54
-
55
- /**
56
- * Creates a new MemoryPool.
57
- *
58
- * @param size - Size of the pool in bytes (default 4MB)
59
- *
60
- * @example
61
- * ```typescript
62
- * import { MemoryPool } from '@btc-vision/bitcoin';
63
- *
64
- * // 4MB pool (default)
65
- * const pool = new MemoryPool();
66
- *
67
- * // 16MB pool
68
- * const largePool = new MemoryPool(16 * 1024 * 1024);
69
- * ```
70
- */
71
- public constructor(size: number = 4 * 1024 * 1024) {
72
- // Add 4 bytes for atomic control (current offset)
73
- const totalSize = size + 4;
74
- this.#buffer = new SharedArrayBuffer(totalSize);
75
- this.#data = new Uint8Array(this.#buffer, 0, size);
76
- this.#control = new Int32Array(this.#buffer, size, 1);
77
- this.#maxSize = size;
78
- }
79
-
80
- /**
81
- * Total capacity of the pool in bytes.
82
- */
83
- public get capacity(): number {
84
- return this.#maxSize;
85
- }
86
-
87
- /**
88
- * Current allocation offset (bytes used).
89
- *
90
- * Uses Atomics for thread-safe reading.
91
- */
92
- public get used(): number {
93
- return Atomics.load(this.#control, 0);
94
- }
95
-
96
- /**
97
- * Remaining available bytes.
98
- */
99
- public get available(): number {
100
- return this.#maxSize - this.used;
101
- }
102
-
103
- /**
104
- * The underlying SharedArrayBuffer.
105
- *
106
- * Can be transferred to Workers for shared memory access.
107
- */
108
- public get sharedBuffer(): SharedArrayBuffer {
109
- return this.#buffer;
110
- }
111
-
112
- /**
113
- * Creates a MemoryPool from an existing SharedArrayBuffer.
114
- *
115
- * Useful for sharing a pool between Workers.
116
- *
117
- * @param buffer - Existing SharedArrayBuffer
118
- * @returns A new MemoryPool instance wrapping the buffer
119
- *
120
- * @example
121
- * ```typescript
122
- * // Main thread
123
- * const pool = new MemoryPool(1024 * 1024);
124
- * worker.postMessage({ buffer: pool.sharedBuffer });
125
- *
126
- * // Worker thread
127
- * self.onmessage = (e) => {
128
- * const pool = MemoryPool.fromSharedBuffer(e.data.buffer);
129
- * const mem = pool.alloc(100);
130
- * };
131
- * ```
132
- */
133
- public static fromSharedBuffer(buffer: SharedArrayBuffer): MemoryPool {
134
- const pool = Object.create(MemoryPool.prototype) as MemoryPool;
135
- const size = buffer.byteLength - 4;
136
-
137
- // Use Object.defineProperty to set private fields on the created object
138
- Object.defineProperty(pool, '#buffer', { value: buffer });
139
- Object.defineProperty(pool, '#data', { value: new Uint8Array(buffer, 0, size) });
140
- Object.defineProperty(pool, '#control', { value: new Int32Array(buffer, size, 1) });
141
- Object.defineProperty(pool, '#maxSize', { value: size });
142
-
143
- return pool;
144
- }
145
-
146
- /**
147
- * Allocates memory from the pool.
148
- *
149
- * Thread-safe using Atomics.add for concurrent access.
150
- *
151
- * @param size - Number of bytes to allocate
152
- * @returns Uint8Array view into the pool
153
- * @throws RangeError if pool is exhausted
154
- *
155
- * @example
156
- * ```typescript
157
- * const pool = new MemoryPool(1024);
158
- * const buf1 = pool.alloc(100); // First 100 bytes
159
- * const buf2 = pool.alloc(200); // Next 200 bytes
160
- * ```
161
- */
162
- public alloc(size: number): Uint8Array {
163
- if (size <= 0) {
164
- throw new RangeError('Allocation size must be positive');
165
- }
166
-
167
- // Atomically reserve space
168
- const oldOffset = Atomics.add(this.#control, 0, size);
169
-
170
- if (oldOffset + size > this.#maxSize) {
171
- // Undo the allocation
172
- Atomics.sub(this.#control, 0, size);
173
- throw new RangeError(
174
- `MemoryPool exhausted: requested ${size} bytes, only ${this.#maxSize - oldOffset} available`,
175
- );
176
- }
177
-
178
- return this.#data.subarray(oldOffset, oldOffset + size);
179
- }
180
-
181
- /**
182
- * Allocates memory and fills it with zeros.
183
- *
184
- * @param size - Number of bytes to allocate
185
- * @returns Uint8Array view into the pool, filled with zeros
186
- * @throws RangeError if pool is exhausted
187
- */
188
- public allocZeroed(size: number): Uint8Array {
189
- const mem = this.alloc(size);
190
- mem.fill(0);
191
- return mem;
192
- }
193
-
194
- /**
195
- * Allocates memory for a specific typed array.
196
- *
197
- * @param length - Number of elements
198
- * @param bytesPerElement - Size of each element in bytes
199
- * @returns Uint8Array view (use constructor of target type on underlying buffer)
200
- * @throws RangeError if pool is exhausted
201
- *
202
- * @example
203
- * ```typescript
204
- * const pool = new MemoryPool(1024);
205
- * const bytes = pool.allocTyped(10, 4); // 40 bytes for 10 Uint32
206
- * const u32View = new Uint32Array(bytes.buffer, bytes.byteOffset, 10);
207
- * ```
208
- */
209
- public allocTyped(length: number, bytesPerElement: number): Uint8Array {
210
- return this.alloc(length * bytesPerElement);
211
- }
212
-
213
- /**
214
- * Resets the pool, making all memory available again.
215
- *
216
- * WARNING: All previously allocated views become invalid.
217
- * Only call when you're sure no references are held.
218
- *
219
- * Thread-safe using Atomics.store.
220
- *
221
- * @example
222
- * ```typescript
223
- * const pool = new MemoryPool(1024);
224
- * const buf = pool.alloc(100);
225
- * // ... use buf ...
226
- * pool.reset(); // buf is now invalid!
227
- * ```
228
- */
229
- public reset(): void {
230
- Atomics.store(this.#control, 0, 0);
231
- }
232
-
233
- /**
234
- * Checks if the pool can accommodate an allocation.
235
- *
236
- * @param size - Number of bytes needed
237
- * @returns True if allocation would succeed
238
- */
239
- public canAlloc(size: number): boolean {
240
- return this.available >= size;
241
- }
242
- }
243
-
244
- /**
245
- * Non-shared memory pool using regular ArrayBuffer.
246
- *
247
- * Use when SharedArrayBuffer is not available (e.g., browsers without
248
- * proper COOP/COEP headers) or when thread-safety is not needed.
249
- *
250
- * @example
251
- * ```typescript
252
- * import { SimpleMemoryPool } from '@btc-vision/bitcoin';
253
- *
254
- * const pool = new SimpleMemoryPool(1024 * 1024);
255
- * const buf = pool.alloc(100);
256
- * ```
257
- */
258
- export class SimpleMemoryPool {
259
- readonly #data: Uint8Array;
260
- #offset: number = 0;
261
-
262
- /**
263
- * Creates a new SimpleMemoryPool.
264
- *
265
- * @param size - Size of the pool in bytes
266
- */
267
- public constructor(size: number) {
268
- this.#data = new Uint8Array(size);
269
- }
270
-
271
- /**
272
- * Total capacity of the pool in bytes.
273
- */
274
- public get capacity(): number {
275
- return this.#data.length;
276
- }
277
-
278
- /**
279
- * Current allocation offset (bytes used).
280
- */
281
- public get used(): number {
282
- return this.#offset;
283
- }
284
-
285
- /**
286
- * Remaining available bytes.
287
- */
288
- public get available(): number {
289
- return this.#data.length - this.#offset;
290
- }
291
-
292
- /**
293
- * Allocates memory from the pool.
294
- *
295
- * @param size - Number of bytes to allocate
296
- * @returns Uint8Array view into the pool
297
- * @throws RangeError if pool is exhausted
298
- */
299
- public alloc(size: number): Uint8Array {
300
- if (size <= 0) {
301
- throw new RangeError('Allocation size must be positive');
302
- }
303
-
304
- if (this.#offset + size > this.#data.length) {
305
- throw new RangeError(
306
- `SimpleMemoryPool exhausted: requested ${size} bytes, only ${this.available} available`,
307
- );
308
- }
309
-
310
- const start = this.#offset;
311
- this.#offset += size;
312
- return this.#data.subarray(start, this.#offset);
313
- }
314
-
315
- /**
316
- * Allocates memory and fills it with zeros.
317
- *
318
- * @param size - Number of bytes to allocate
319
- * @returns Uint8Array view into the pool, filled with zeros
320
- */
321
- public allocZeroed(size: number): Uint8Array {
322
- const mem = this.alloc(size);
323
- mem.fill(0);
324
- return mem;
325
- }
326
-
327
- /**
328
- * Resets the pool, making all memory available again.
329
- */
330
- public reset(): void {
331
- this.#offset = 0;
332
- }
333
-
334
- /**
335
- * Checks if the pool can accommodate an allocation.
336
- *
337
- * @param size - Number of bytes needed
338
- * @returns True if allocation would succeed
339
- */
340
- public canAlloc(size: number): boolean {
341
- return this.available >= size;
342
- }
343
- }