@shelby-protocol/sdk 0.3.0 → 0.3.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 (106) hide show
  1. package/dist/browser/index.d.ts +12 -3
  2. package/dist/browser/index.mjs +3717 -142
  3. package/dist/core/aptos-explorer.mjs +46 -5
  4. package/dist/core/blobs.mjs +12 -4
  5. package/dist/core/chunk.mjs +58 -9
  6. package/dist/core/clients/ShelbyBlobClient.d.ts +0 -43
  7. package/dist/core/clients/ShelbyBlobClient.mjs +1084 -17
  8. package/dist/core/clients/ShelbyClient.d.ts +12 -2
  9. package/dist/core/clients/ShelbyClient.mjs +2915 -23
  10. package/dist/core/clients/ShelbyClientConfig.mjs +0 -1
  11. package/dist/core/clients/ShelbyMetadataClient.d.ts +0 -18
  12. package/dist/core/clients/ShelbyMetadataClient.mjs +306 -6
  13. package/dist/core/clients/ShelbyMicropaymentChannelClient.d.ts +7 -49
  14. package/dist/core/clients/ShelbyMicropaymentChannelClient.mjs +839 -13
  15. package/dist/core/clients/ShelbyPlacementGroupClient.mjs +307 -8
  16. package/dist/core/clients/ShelbyRPCClient.d.ts +106 -3
  17. package/dist/core/clients/ShelbyRPCClient.mjs +1261 -12
  18. package/dist/core/clients/index.d.ts +2 -1
  19. package/dist/core/clients/index.mjs +3420 -39
  20. package/dist/core/clients/utils.mjs +65 -6
  21. package/dist/core/commitments.mjs +270 -11
  22. package/dist/core/constants.d.ts +2 -2
  23. package/dist/core/constants.mjs +39 -16
  24. package/dist/core/erasure/clay-codes.mjs +129 -4
  25. package/dist/core/erasure/constants.mjs +33 -10
  26. package/dist/core/erasure/default.mjs +191 -9
  27. package/dist/core/erasure/index.mjs +266 -23
  28. package/dist/core/erasure/reed-solomon.mjs +68 -4
  29. package/dist/core/erasure/utils.mjs +0 -2
  30. package/dist/core/errors.mjs +29 -8
  31. package/dist/core/index.d.ts +1 -1
  32. package/dist/core/index.mjs +3711 -142
  33. package/dist/core/layout.mjs +37 -5
  34. package/dist/core/networks.mjs +10 -5
  35. package/dist/core/operations/generated/sdk.d.ts +9 -0
  36. package/dist/core/operations/generated/sdk.mjs +183 -18
  37. package/dist/core/operations/index.mjs +284 -24
  38. package/dist/core/promises.mjs +4 -4
  39. package/dist/core/rpc-responses.mjs +29 -10
  40. package/dist/core/shelby-explorer.mjs +31 -6
  41. package/dist/core/strings.mjs +0 -2
  42. package/dist/core/types/blobs.mjs +0 -1
  43. package/dist/core/types/index.mjs +230 -9
  44. package/dist/core/types/payments.d.ts +6 -6
  45. package/dist/core/types/payments.mjs +230 -5
  46. package/dist/core/types/placement_groups.mjs +0 -1
  47. package/dist/core/types/storage_providers.mjs +0 -1
  48. package/dist/core/utils.mjs +94 -8
  49. package/dist/node/clients/ShelbyNodeClient.d.ts +1 -0
  50. package/dist/node/clients/ShelbyNodeClient.mjs +2919 -24
  51. package/dist/node/clients/index.d.ts +1 -0
  52. package/dist/node/clients/index.mjs +2920 -26
  53. package/dist/node/index.d.ts +1 -1
  54. package/dist/node/index.mjs +3734 -149
  55. package/dist/node/parallel/commitment_worker.d.ts +9 -0
  56. package/dist/node/parallel/commitment_worker.mjs +298 -0
  57. package/dist/node/parallel/commitment_worker_pool.d.ts +35 -0
  58. package/dist/node/parallel/commitment_worker_pool.mjs +286 -0
  59. package/dist/node/parallel/default_commitment_worker_pool.d.ts +24 -0
  60. package/dist/node/parallel/default_commitment_worker_pool.mjs +302 -0
  61. package/dist/node/parallel/index.d.ts +11 -0
  62. package/dist/node/parallel/index.mjs +563 -0
  63. package/dist/node/parallel/parallel_commitments.d.ts +32 -0
  64. package/dist/node/parallel/parallel_commitments.mjs +561 -0
  65. package/dist/node/parallel/worker_pool.d.ts +74 -0
  66. package/dist/node/parallel/worker_pool.mjs +248 -0
  67. package/dist/node/testUtil.mjs +8 -4
  68. package/package.json +6 -2
  69. package/dist/chunk-3NRBHSMQ.mjs +0 -12
  70. package/dist/chunk-5BVSWXQJ.mjs +0 -801
  71. package/dist/chunk-6FJNAUU5.mjs +0 -278
  72. package/dist/chunk-6XSZRDLJ.mjs +0 -375
  73. package/dist/chunk-7P6ASYW6.mjs +0 -9
  74. package/dist/chunk-A4IG6GSE.mjs +0 -21
  75. package/dist/chunk-AD2G3QYD.mjs +0 -0
  76. package/dist/chunk-AUQDI5BS.mjs +0 -43
  77. package/dist/chunk-AY3HBWXA.mjs +0 -51
  78. package/dist/chunk-BDSW5PHM.mjs +0 -37
  79. package/dist/chunk-C6RQ3AEU.mjs +0 -51
  80. package/dist/chunk-CQ6QPIZK.mjs +0 -37
  81. package/dist/chunk-D6GQHO6G.mjs +0 -15
  82. package/dist/chunk-EM67QTMR.mjs +0 -0
  83. package/dist/chunk-I6NG5GNL.mjs +0 -8
  84. package/dist/chunk-JTXYKO3U.mjs +0 -38
  85. package/dist/chunk-KG2TMLEV.mjs +0 -207
  86. package/dist/chunk-M3CP5TFP.mjs +0 -39
  87. package/dist/chunk-M7W43K2Q.mjs +0 -505
  88. package/dist/chunk-MB7C7VQF.mjs +0 -0
  89. package/dist/chunk-MNCQBEMS.mjs +0 -55
  90. package/dist/chunk-MQUVYMNQ.mjs +0 -0
  91. package/dist/chunk-NEO7Z3ZQ.mjs +0 -49
  92. package/dist/chunk-NHWWORCH.mjs +0 -72
  93. package/dist/chunk-OEK24ON7.mjs +0 -135
  94. package/dist/chunk-QQ57OGQ2.mjs +0 -0
  95. package/dist/chunk-RNNJXTX2.mjs +0 -154
  96. package/dist/chunk-S6Q4OG7H.mjs +0 -83
  97. package/dist/chunk-TET3DJEO.mjs +0 -199
  98. package/dist/chunk-UEZNZBJO.mjs +0 -104
  99. package/dist/chunk-V46D5NBW.mjs +0 -11
  100. package/dist/chunk-V4B3GAMQ.mjs +0 -42
  101. package/dist/chunk-WFWRBNTW.mjs +0 -412
  102. package/dist/chunk-WTICJPDB.mjs +0 -0
  103. package/dist/chunk-XNEIWM4O.mjs +0 -0
  104. package/dist/chunk-Z4FZ7W6L.mjs +0 -39
  105. package/dist/chunk-Z7RFCADT.mjs +0 -0
  106. package/dist/chunk-ZHXCVRZX.mjs +0 -0
@@ -0,0 +1,302 @@
1
+ // src/node/parallel/default_commitment_worker_pool.ts
2
+ import { availableParallelism } from "os";
3
+
4
+ // src/node/parallel/worker_pool.ts
5
+ import fs from "fs";
6
+ import path from "path";
7
+ import { fileURLToPath } from "url";
8
+ import { Worker } from "worker_threads";
9
+ var WorkerPool = class {
10
+ workers = [];
11
+ queue = [];
12
+ maxWorkers;
13
+ workerPath;
14
+ isTerminating = false;
15
+ activeTasks = /* @__PURE__ */ new Set();
16
+ inFlightCount = 0;
17
+ slotWaiters = [];
18
+ constructor(maxWorkers, workerPath) {
19
+ if (!Number.isSafeInteger(maxWorkers) || maxWorkers < 1) {
20
+ throw new Error(
21
+ `maxWorkers must be a positive safe integer, got ${maxWorkers}`
22
+ );
23
+ }
24
+ this.maxWorkers = maxWorkers;
25
+ this.workerPath = workerPath;
26
+ }
27
+ /**
28
+ * Resolve the path to a worker script relative to the calling module.
29
+ *
30
+ * Pass `import.meta.url` from the subclass file along with the script name
31
+ * (without extension). The extension is inferred from the calling module's
32
+ * own extension so that both the TypeScript source (.ts) and the compiled
33
+ * output (.mjs / .js) are handled correctly.
34
+ *
35
+ * @example
36
+ * constructor(maxWorkers: number) {
37
+ * super(maxWorkers, WorkerPool.resolveWorkerPath(import.meta.url, "my_worker"));
38
+ * }
39
+ */
40
+ static resolveWorkerPath(importMetaUrl, scriptName) {
41
+ const currentPath = fileURLToPath(importMetaUrl);
42
+ const dir = path.dirname(currentPath);
43
+ const ext = path.extname(currentPath);
44
+ const workerExt = ext === ".mjs" ? ".mjs" : ext === ".js" ? ".js" : ".ts";
45
+ return path.join(dir, `${scriptName}${workerExt}`);
46
+ }
47
+ initialize() {
48
+ this.isTerminating = false;
49
+ if (!fs.existsSync(this.workerPath)) {
50
+ throw new Error(
51
+ `Could not find worker script at ${this.workerPath}. Running as ${path.extname(this.workerPath)}, expected worker at same extension.`
52
+ );
53
+ }
54
+ for (let i = 0; i < this.maxWorkers; i++) {
55
+ this.workers.push({ worker: new Worker(this.workerPath), busy: false });
56
+ }
57
+ }
58
+ isInitialized() {
59
+ return this.workers.length > 0;
60
+ }
61
+ /**
62
+ * Acquire a back-pressure slot before submitting work to the pool.
63
+ * Resolves immediately if fewer than `maxWorkers` chunksets are in flight;
64
+ * otherwise waits until a slot is released. Callers must call `releaseSlot()`
65
+ * when the submitted task settles (resolve or reject).
66
+ *
67
+ * Moving back-pressure into the pool ensures a single global bound on
68
+ * buffered memory regardless of how many concurrent callers share the pool.
69
+ */
70
+ acquireSlot() {
71
+ if (this.inFlightCount < this.maxWorkers) {
72
+ this.inFlightCount++;
73
+ return Promise.resolve();
74
+ }
75
+ return new Promise((resolve, reject) => {
76
+ this.slotWaiters.push({
77
+ resolve: () => {
78
+ this.inFlightCount++;
79
+ resolve();
80
+ },
81
+ reject
82
+ });
83
+ });
84
+ }
85
+ releaseSlot() {
86
+ this.inFlightCount--;
87
+ const next = this.slotWaiters.shift();
88
+ if (next) {
89
+ next.resolve();
90
+ }
91
+ }
92
+ enqueueTask(task) {
93
+ this.activeTasks.add(task);
94
+ if (this.isTerminating) {
95
+ this.activeTasks.delete(task);
96
+ task.reject(
97
+ new Error("Worker pool is being terminated - cannot process new tasks")
98
+ );
99
+ return;
100
+ }
101
+ const availableWorker = this.workers.find((w) => !w.busy);
102
+ if (availableWorker) {
103
+ this.executeTask(availableWorker, task);
104
+ } else {
105
+ this.queue.push(task);
106
+ }
107
+ }
108
+ processNextQueuedTask(preferredWorker) {
109
+ const nextTask = this.queue.shift();
110
+ if (!nextTask) return;
111
+ if (preferredWorker && !preferredWorker.busy && preferredWorker.worker) {
112
+ this.executeTask(preferredWorker, nextTask);
113
+ return;
114
+ }
115
+ const availableWorker = this.workers.find((w) => !w.busy && w.worker);
116
+ if (availableWorker) {
117
+ this.executeTask(availableWorker, nextTask);
118
+ } else {
119
+ this.queue.unshift(nextTask);
120
+ }
121
+ }
122
+ rejectAllQueuedTasks(error) {
123
+ const tasks = this.queue.splice(0);
124
+ for (const task of tasks) {
125
+ this.activeTasks.delete(task);
126
+ task.reject(error);
127
+ }
128
+ }
129
+ rejectAllActiveTasks(error) {
130
+ const tasks = Array.from(this.activeTasks);
131
+ this.activeTasks.clear();
132
+ for (const task of tasks) {
133
+ task.reject(error);
134
+ }
135
+ }
136
+ executeTask(workerWrapper, task) {
137
+ workerWrapper.busy = true;
138
+ const cleanup = () => {
139
+ workerWrapper.worker.off("message", messageHandler);
140
+ workerWrapper.worker.off("error", errorHandler);
141
+ workerWrapper.worker.off("exit", exitHandler);
142
+ workerWrapper.busy = false;
143
+ };
144
+ const messageHandler = (message) => {
145
+ cleanup();
146
+ this.activeTasks.delete(task);
147
+ if (message.success && message.result !== void 0) {
148
+ task.resolve(message.result);
149
+ } else {
150
+ task.reject(new Error(message.error ?? "Worker processing failed"));
151
+ }
152
+ this.processNextQueuedTask(workerWrapper);
153
+ };
154
+ const errorHandler = (error) => {
155
+ workerWrapper.worker.off("message", messageHandler);
156
+ workerWrapper.worker.off("error", errorHandler);
157
+ workerWrapper.busy = false;
158
+ this.activeTasks.delete(task);
159
+ task.reject(error);
160
+ };
161
+ const exitHandler = (exitCode) => {
162
+ workerWrapper.worker.off("message", messageHandler);
163
+ workerWrapper.worker.off("error", errorHandler);
164
+ workerWrapper.worker.off("exit", exitHandler);
165
+ workerWrapper.busy = false;
166
+ this.activeTasks.delete(task);
167
+ task.reject(
168
+ new Error(`Worker terminated unexpectedly with exit code ${exitCode}`)
169
+ );
170
+ if (this.isTerminating) return;
171
+ try {
172
+ const newWorker = new Worker(this.workerPath);
173
+ if (this.isTerminating) {
174
+ newWorker.terminate().catch(() => {
175
+ });
176
+ return;
177
+ }
178
+ workerWrapper.worker = newWorker;
179
+ this.processNextQueuedTask(workerWrapper);
180
+ } catch (_error) {
181
+ const index = this.workers.indexOf(workerWrapper);
182
+ if (index > -1) this.workers.splice(index, 1);
183
+ if (this.workers.length === 0) {
184
+ this.rejectAllQueuedTasks(
185
+ new Error(
186
+ "All workers have died and could not be replaced - worker pool is non-functional"
187
+ )
188
+ );
189
+ return;
190
+ }
191
+ this.processNextQueuedTask();
192
+ }
193
+ };
194
+ workerWrapper.worker.on("message", messageHandler);
195
+ workerWrapper.worker.on("error", errorHandler);
196
+ workerWrapper.worker.on("exit", exitHandler);
197
+ this.postTaskMessage(workerWrapper.worker, task);
198
+ }
199
+ /**
200
+ * Terminate all workers and reject all pending tasks.
201
+ *
202
+ * This pool is NOT designed to be reused after termination.
203
+ * Create a new instance if a fresh pool is needed.
204
+ */
205
+ async terminate() {
206
+ this.isTerminating = true;
207
+ const terminationError = new Error(
208
+ "Worker pool is being terminated - task cancelled"
209
+ );
210
+ this.rejectAllQueuedTasks(terminationError);
211
+ this.rejectAllActiveTasks(terminationError);
212
+ const waiters = this.slotWaiters.splice(0);
213
+ for (const waiter of waiters) {
214
+ waiter.reject(terminationError);
215
+ }
216
+ const terminatePromises = this.workers.map((workerWrapper) => {
217
+ return new Promise((resolve) => {
218
+ const worker = workerWrapper.worker;
219
+ let resolved = false;
220
+ const doResolve = () => {
221
+ if (!resolved) {
222
+ resolved = true;
223
+ resolve();
224
+ }
225
+ };
226
+ let timeoutId;
227
+ const exitHandler = () => {
228
+ if (timeoutId) clearTimeout(timeoutId);
229
+ doResolve();
230
+ };
231
+ worker.removeAllListeners("message");
232
+ worker.removeAllListeners("error");
233
+ worker.once("exit", exitHandler);
234
+ timeoutId = setTimeout(() => {
235
+ worker.removeListener("exit", exitHandler);
236
+ doResolve();
237
+ }, 2e3);
238
+ worker.terminate().catch(() => {
239
+ });
240
+ });
241
+ });
242
+ await Promise.all(terminatePromises);
243
+ this.workers = [];
244
+ this.queue = [];
245
+ this.inFlightCount = 0;
246
+ this.isTerminating = false;
247
+ }
248
+ };
249
+
250
+ // src/node/parallel/commitment_worker_pool.ts
251
+ var CommitmentWorkerPool = class extends WorkerPool {
252
+ constructor(maxWorkers) {
253
+ super(
254
+ maxWorkers,
255
+ WorkerPool.resolveWorkerPath(import.meta.url, "commitment_worker")
256
+ );
257
+ }
258
+ postTaskMessage(worker, task) {
259
+ worker.postMessage(
260
+ {
261
+ chunksetData: task.chunksetData,
262
+ encoding: task.encoding
263
+ },
264
+ [task.chunksetData.buffer]
265
+ );
266
+ }
267
+ /**
268
+ * Dispatch a chunkset to a worker for encoding. Returns a promise that
269
+ * resolves to the chunkset's commitment once the worker finishes.
270
+ *
271
+ * The caller must ensure `chunksetData` is not used after this call
272
+ * (its underlying buffer is transferred to the worker thread).
273
+ */
274
+ encodeChunkset(chunksetIdx, chunksetData, encoding) {
275
+ return new Promise((resolve, reject) => {
276
+ const task = {
277
+ chunksetIdx,
278
+ chunksetData,
279
+ encoding,
280
+ resolve,
281
+ reject
282
+ };
283
+ this.enqueueTask(task);
284
+ });
285
+ }
286
+ };
287
+
288
+ // src/node/parallel/default_commitment_worker_pool.ts
289
+ function defaultCommitmentWorkerCount() {
290
+ return Math.max(1, availableParallelism() - 1);
291
+ }
292
+ function createDefaultCommitmentWorkerPool(options = {}) {
293
+ const pool = new CommitmentWorkerPool(
294
+ options.workers ?? defaultCommitmentWorkerCount()
295
+ );
296
+ pool.initialize();
297
+ return pool;
298
+ }
299
+ export {
300
+ createDefaultCommitmentWorkerPool,
301
+ defaultCommitmentWorkerCount
302
+ };
@@ -0,0 +1,11 @@
1
+ export { CommitmentEncoding } from './commitment_worker.js';
2
+ export { CommitmentWorkerPool } from './commitment_worker_pool.js';
3
+ export { CommitmentWorkerPoolOptions, createDefaultCommitmentWorkerPool, defaultCommitmentWorkerCount } from './default_commitment_worker_pool.js';
4
+ export { generateCommitmentsParallel } from './parallel_commitments.js';
5
+ export { WorkerPool, WorkerPoolTask } from './worker_pool.js';
6
+ import 'node:worker_threads';
7
+ import '../../core/commitments.js';
8
+ import '@aptos-labs/ts-sdk';
9
+ import 'zod';
10
+ import '../../clay-codes-DdXABBDx.js';
11
+ import '@shelby-protocol/clay-codes';