@shelby-protocol/sdk 0.3.0 → 0.4.0

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