@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,301 @@
1
+ // src/node/parallel/worker_pool.ts
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import { fileURLToPath } from "url";
5
+ import { Worker } from "worker_threads";
6
+
7
+ // src/node/parallel/coverage_flush.ts
8
+ import { takeCoverage } from "v8";
9
+ var FLUSH_REQUEST = "__shelby_cov_flush_request__";
10
+ var FLUSH_ACK = "__shelby_cov_flush_ack__";
11
+ var coverageEnabled = () => Boolean(process.env.NODE_V8_COVERAGE);
12
+ var isType = (msg, type) => typeof msg === "object" && msg !== null && msg.type === type;
13
+ function flushWorkerCoverage(worker, timeoutMs = 2e3) {
14
+ if (!coverageEnabled()) return Promise.resolve();
15
+ return new Promise((resolve) => {
16
+ const onMessage = (msg) => {
17
+ if (isType(msg, FLUSH_ACK)) finish();
18
+ };
19
+ const finish = () => {
20
+ clearTimeout(timer);
21
+ worker.off("message", onMessage);
22
+ resolve();
23
+ };
24
+ const timer = setTimeout(finish, timeoutMs);
25
+ worker.on("message", onMessage);
26
+ try {
27
+ worker.postMessage({ type: FLUSH_REQUEST });
28
+ } catch {
29
+ finish();
30
+ }
31
+ });
32
+ }
33
+
34
+ // src/node/parallel/worker_pool.ts
35
+ var WorkerPool = class {
36
+ workers = [];
37
+ queue = [];
38
+ maxWorkers;
39
+ workerPath;
40
+ isTerminating = false;
41
+ activeTasks = /* @__PURE__ */ new Set();
42
+ inFlightCount = 0;
43
+ slotWaiters = [];
44
+ constructor(maxWorkers, workerPath) {
45
+ if (!Number.isSafeInteger(maxWorkers) || maxWorkers < 1) {
46
+ throw new Error(
47
+ `maxWorkers must be a positive safe integer, got ${maxWorkers}`
48
+ );
49
+ }
50
+ this.maxWorkers = maxWorkers;
51
+ this.workerPath = workerPath;
52
+ }
53
+ /**
54
+ * Resolve the path to a worker script relative to the calling module.
55
+ *
56
+ * Pass `import.meta.url` from the subclass file along with the script name
57
+ * (without extension). The extension is inferred from the calling module's
58
+ * own extension so that both the TypeScript source (.ts) and the compiled
59
+ * output (.mjs / .js) are handled correctly.
60
+ *
61
+ * @example
62
+ * constructor(maxWorkers: number) {
63
+ * super(maxWorkers, WorkerPool.resolveWorkerPath(import.meta.url, "my_worker"));
64
+ * }
65
+ */
66
+ static resolveWorkerPath(importMetaUrl, scriptName) {
67
+ const currentPath = fileURLToPath(importMetaUrl);
68
+ const dir = path.dirname(currentPath);
69
+ const ext = path.extname(currentPath);
70
+ const workerExt = ext === ".mjs" ? ".mjs" : ext === ".js" ? ".js" : ".ts";
71
+ if (workerExt === ".ts") {
72
+ const srcSegment = `${path.sep}src${path.sep}`;
73
+ const srcIdx = currentPath.indexOf(srcSegment);
74
+ if (srcIdx !== -1) {
75
+ const packageRoot = currentPath.slice(0, srcIdx);
76
+ const relFromSrc = path.relative(path.join(packageRoot, "src"), dir);
77
+ return path.join(
78
+ packageRoot,
79
+ "dist",
80
+ "workers",
81
+ relFromSrc,
82
+ `${scriptName}.js`
83
+ );
84
+ }
85
+ }
86
+ let workerPath = path.join(dir, `${scriptName}${workerExt}`);
87
+ if (fs.existsSync(workerPath)) {
88
+ return workerPath;
89
+ }
90
+ workerPath = path.join(dir, "workers", `${scriptName}${workerExt}`);
91
+ if (fs.existsSync(workerPath)) {
92
+ return workerPath;
93
+ }
94
+ return path.join(dir, `${scriptName}${workerExt}`);
95
+ }
96
+ initialize() {
97
+ this.isTerminating = false;
98
+ if (!fs.existsSync(this.workerPath)) {
99
+ throw new Error(
100
+ `Could not find worker script at ${this.workerPath}. Running as ${path.extname(this.workerPath)}, expected worker at same extension.`
101
+ );
102
+ }
103
+ for (let i = 0; i < this.maxWorkers; i++) {
104
+ this.workers.push({ worker: new Worker(this.workerPath), busy: false });
105
+ }
106
+ }
107
+ isInitialized() {
108
+ return this.workers.length > 0;
109
+ }
110
+ /**
111
+ * Acquire a back-pressure slot before submitting work to the pool.
112
+ * Resolves immediately if fewer than `maxWorkers` chunksets are in flight;
113
+ * otherwise waits until a slot is released. Callers must call `releaseSlot()`
114
+ * when the submitted task settles (resolve or reject).
115
+ *
116
+ * Moving back-pressure into the pool ensures a single global bound on
117
+ * buffered memory regardless of how many concurrent callers share the pool.
118
+ */
119
+ acquireSlot() {
120
+ if (this.inFlightCount < this.maxWorkers) {
121
+ this.inFlightCount++;
122
+ return Promise.resolve();
123
+ }
124
+ return new Promise((resolve, reject) => {
125
+ this.slotWaiters.push({
126
+ resolve: () => {
127
+ this.inFlightCount++;
128
+ resolve();
129
+ },
130
+ reject
131
+ });
132
+ });
133
+ }
134
+ releaseSlot() {
135
+ this.inFlightCount--;
136
+ const next = this.slotWaiters.shift();
137
+ if (next) {
138
+ next.resolve();
139
+ }
140
+ }
141
+ enqueueTask(task) {
142
+ this.activeTasks.add(task);
143
+ if (this.isTerminating) {
144
+ this.activeTasks.delete(task);
145
+ task.reject(
146
+ new Error("Worker pool is being terminated - cannot process new tasks")
147
+ );
148
+ return;
149
+ }
150
+ const availableWorker = this.workers.find((w) => !w.busy);
151
+ if (availableWorker) {
152
+ this.executeTask(availableWorker, task);
153
+ } else {
154
+ this.queue.push(task);
155
+ }
156
+ }
157
+ processNextQueuedTask(preferredWorker) {
158
+ const nextTask = this.queue.shift();
159
+ if (!nextTask) return;
160
+ if (preferredWorker && !preferredWorker.busy && preferredWorker.worker) {
161
+ this.executeTask(preferredWorker, nextTask);
162
+ return;
163
+ }
164
+ const availableWorker = this.workers.find((w) => !w.busy && w.worker);
165
+ if (availableWorker) {
166
+ this.executeTask(availableWorker, nextTask);
167
+ } else {
168
+ this.queue.unshift(nextTask);
169
+ }
170
+ }
171
+ rejectAllQueuedTasks(error) {
172
+ const tasks = this.queue.splice(0);
173
+ for (const task of tasks) {
174
+ this.activeTasks.delete(task);
175
+ task.reject(error);
176
+ }
177
+ }
178
+ rejectAllActiveTasks(error) {
179
+ const tasks = Array.from(this.activeTasks);
180
+ this.activeTasks.clear();
181
+ for (const task of tasks) {
182
+ task.reject(error);
183
+ }
184
+ }
185
+ executeTask(workerWrapper, task) {
186
+ workerWrapper.busy = true;
187
+ const cleanup = () => {
188
+ workerWrapper.worker.off("message", messageHandler);
189
+ workerWrapper.worker.off("error", errorHandler);
190
+ workerWrapper.worker.off("exit", exitHandler);
191
+ workerWrapper.busy = false;
192
+ };
193
+ const messageHandler = (message) => {
194
+ cleanup();
195
+ this.activeTasks.delete(task);
196
+ if (message.success && message.result !== void 0) {
197
+ task.resolve(message.result);
198
+ } else {
199
+ task.reject(new Error(message.error ?? "Worker processing failed"));
200
+ }
201
+ this.processNextQueuedTask(workerWrapper);
202
+ };
203
+ const errorHandler = (error) => {
204
+ workerWrapper.worker.off("message", messageHandler);
205
+ workerWrapper.worker.off("error", errorHandler);
206
+ workerWrapper.busy = false;
207
+ this.activeTasks.delete(task);
208
+ task.reject(error);
209
+ };
210
+ const exitHandler = (exitCode) => {
211
+ workerWrapper.worker.off("message", messageHandler);
212
+ workerWrapper.worker.off("error", errorHandler);
213
+ workerWrapper.worker.off("exit", exitHandler);
214
+ workerWrapper.busy = false;
215
+ this.activeTasks.delete(task);
216
+ task.reject(
217
+ new Error(`Worker terminated unexpectedly with exit code ${exitCode}`)
218
+ );
219
+ if (this.isTerminating) return;
220
+ try {
221
+ const newWorker = new Worker(this.workerPath);
222
+ if (this.isTerminating) {
223
+ newWorker.terminate().catch(() => {
224
+ });
225
+ return;
226
+ }
227
+ workerWrapper.worker = newWorker;
228
+ this.processNextQueuedTask(workerWrapper);
229
+ } catch (_error) {
230
+ const index = this.workers.indexOf(workerWrapper);
231
+ if (index > -1) this.workers.splice(index, 1);
232
+ if (this.workers.length === 0) {
233
+ this.rejectAllQueuedTasks(
234
+ new Error(
235
+ "All workers have died and could not be replaced - worker pool is non-functional"
236
+ )
237
+ );
238
+ return;
239
+ }
240
+ this.processNextQueuedTask();
241
+ }
242
+ };
243
+ workerWrapper.worker.on("message", messageHandler);
244
+ workerWrapper.worker.on("error", errorHandler);
245
+ workerWrapper.worker.on("exit", exitHandler);
246
+ this.postTaskMessage(workerWrapper.worker, task);
247
+ }
248
+ /**
249
+ * Terminate all workers and reject all pending tasks.
250
+ *
251
+ * This pool is NOT designed to be reused after termination.
252
+ * Create a new instance if a fresh pool is needed.
253
+ */
254
+ async terminate() {
255
+ this.isTerminating = true;
256
+ const terminationError = new Error(
257
+ "Worker pool is being terminated - task cancelled"
258
+ );
259
+ this.rejectAllQueuedTasks(terminationError);
260
+ this.rejectAllActiveTasks(terminationError);
261
+ const waiters = this.slotWaiters.splice(0);
262
+ for (const waiter of waiters) {
263
+ waiter.reject(terminationError);
264
+ }
265
+ const terminatePromises = this.workers.map(async (workerWrapper) => {
266
+ const worker = workerWrapper.worker;
267
+ await flushWorkerCoverage(worker);
268
+ await new Promise((resolve) => {
269
+ let resolved = false;
270
+ const doResolve = () => {
271
+ if (!resolved) {
272
+ resolved = true;
273
+ resolve();
274
+ }
275
+ };
276
+ let timeoutId;
277
+ const exitHandler = () => {
278
+ if (timeoutId) clearTimeout(timeoutId);
279
+ doResolve();
280
+ };
281
+ worker.removeAllListeners("message");
282
+ worker.removeAllListeners("error");
283
+ worker.once("exit", exitHandler);
284
+ timeoutId = setTimeout(() => {
285
+ worker.removeListener("exit", exitHandler);
286
+ doResolve();
287
+ }, 2e3);
288
+ worker.terminate().catch(() => {
289
+ });
290
+ });
291
+ });
292
+ await Promise.all(terminatePromises);
293
+ this.workers = [];
294
+ this.queue = [];
295
+ this.inFlightCount = 0;
296
+ this.isTerminating = false;
297
+ }
298
+ };
299
+ export {
300
+ WorkerPool
301
+ };
@@ -1,7 +1,11 @@
1
- import {
2
- makeChunk
3
- } from "../chunk-A4IG6GSE.mjs";
4
- import "../chunk-7P6ASYW6.mjs";
1
+ // src/node/testUtil.ts
2
+ function makeChunk(n) {
3
+ const c = Buffer.alloc(n);
4
+ for (let i = 0; i < n; ++i) {
5
+ c[i] = i % 256;
6
+ }
7
+ return c;
8
+ }
5
9
  export {
6
10
  makeChunk
7
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shelby-protocol/sdk",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -14,19 +14,28 @@
14
14
  "types": "./dist/node/index.d.ts",
15
15
  "import": "./dist/node/index.mjs"
16
16
  },
17
+ "./node/parallel": {
18
+ "types": "./dist/node/parallel/index.d.ts",
19
+ "import": "./dist/node/parallel/index.mjs"
20
+ },
21
+ "./sp": {
22
+ "types": "./dist/core/sp/index.d.ts",
23
+ "import": "./dist/core/sp/index.mjs"
24
+ },
17
25
  "./browser": {
18
26
  "types": "./dist/browser/index.d.ts",
19
27
  "import": "./dist/browser/index.mjs"
20
28
  }
21
29
  },
22
30
  "dependencies": {
31
+ "@bufbuild/protobuf": "^2.8.0",
23
32
  "graphql": "^16.11.0",
24
33
  "graphql-request": "^7.2.0",
25
34
  "graphql-tag": "^2.12.6",
26
35
  "p-limit": "7.1.1",
27
36
  "zod": "^3.24.3",
28
- "@shelby-protocol/clay-codes": "0.0.2",
29
- "@shelby-protocol/reed-solomon": "0.0.1"
37
+ "@shelby-protocol/reed-solomon": "0.0.1",
38
+ "@shelby-protocol/clay-codes": "0.0.2"
30
39
  },
31
40
  "devDependencies": {
32
41
  "@aptos-labs/ts-sdk": "5.2.1",
@@ -49,12 +58,14 @@
49
58
  "scripts": {
50
59
  "lint": "biome check .",
51
60
  "fmt": "biome check . --write",
52
- "generate": "graphql-codegen",
61
+ "generate": "graphql-codegen && (cd ../.. && pnpm exec buf generate --template buf.gen.server.yaml --path proto/rpc_server.proto)",
53
62
  "generate:watch": "graphql-codegen --watch",
63
+ "generate:schema": "node scripts/dump-schema.mjs",
54
64
  "verify:config": "tsx scripts/verify-config.ts",
55
65
  "build": "tsc --noEmit && (rimraf dist; tsup) && pnpm run verify:config",
56
66
  "test": "vitest dev",
57
- "test:once": "vitest run",
67
+ "test:once": "pnpm run build && vitest run",
68
+ "test:integration": "vitest run --config vitest.integration.config.ts --reporter=verbose",
58
69
  "benchmark:erasure": "tsx benchmark/erasure.ts"
59
70
  }
60
71
  }
@@ -1,12 +0,0 @@
1
- import {
2
- normalizeAddress
3
- } from "./chunk-UEZNZBJO.mjs";
4
-
5
- // src/core/blobs.ts
6
- var createBlobKey = (params) => {
7
- return `@${normalizeAddress(params.account).toStringLongWithoutPrefix()}/${params.blobName}`;
8
- };
9
-
10
- export {
11
- createBlobKey
12
- };