@inversealtruism/csd-client 0.1.14 → 0.1.16
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.
- package/README.md +1 -1
- package/dist/index.cjs +13 -11
- package/dist/index.d.cts +11 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -11
- package/package.json +15 -7
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Typed HTTP RPC client for a Compute Substrate node (or the Cairn proxy / a discovered gateway): tip, block, tx, utxos, proposals, top, domains, mempool, submit, templates.
|
|
4
4
|
|
|
5
|
-
Part of the [Compute Substrate SDK](https://github.com/InverseAltruism/csd-sdk) (L0). Zero `Buffer
|
|
5
|
+
Part of the [Compute Substrate SDK](https://github.com/InverseAltruism/csd-sdk) (L0). Zero `Buffer`, runs in Node, browsers, and MV3 service workers. Deps: `@noble/*` only.
|
|
6
6
|
|
|
7
7
|
```
|
|
8
8
|
npm i @inversealtruism/csd-client
|
package/dist/index.cjs
CHANGED
|
@@ -33,9 +33,11 @@ var CsdClient = class {
|
|
|
33
33
|
timeoutMs;
|
|
34
34
|
retries;
|
|
35
35
|
maxBytes;
|
|
36
|
+
parseJson;
|
|
36
37
|
constructor(opts) {
|
|
37
38
|
this.base = opts.baseUrl.replace(/\/+$/, "");
|
|
38
39
|
this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
|
|
40
|
+
this.parseJson = opts.parseJson ?? JSON.parse;
|
|
39
41
|
const gf = globalThis.fetch;
|
|
40
42
|
this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
|
|
41
43
|
this.timeoutMs = opts.timeoutMs ?? 1e4;
|
|
@@ -50,10 +52,10 @@ var CsdClient = class {
|
|
|
50
52
|
const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
|
|
51
53
|
if (r.status >= 500 && attempt < maxRetries) {
|
|
52
54
|
lastErr = new Error(`HTTP ${r.status}`);
|
|
53
|
-
} else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
|
|
55
|
+
} else if (!r.ok) throw Object.assign(new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`), { terminal: r.status < 500 });
|
|
54
56
|
else return await this.readCapped(r, path);
|
|
55
57
|
} catch (e) {
|
|
56
|
-
if (attempt >= maxRetries) throw e;
|
|
58
|
+
if (attempt >= maxRetries || e?.terminal) throw e;
|
|
57
59
|
lastErr = e;
|
|
58
60
|
}
|
|
59
61
|
const cap = Math.min(5e3, 250 * 2 ** attempt);
|
|
@@ -77,7 +79,7 @@ var CsdClient = class {
|
|
|
77
79
|
const t = await r.text();
|
|
78
80
|
const byteLen = new TextEncoder().encode(t).length;
|
|
79
81
|
if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
|
|
80
|
-
return
|
|
82
|
+
return this.parseJson(t);
|
|
81
83
|
}
|
|
82
84
|
const reader = body.getReader();
|
|
83
85
|
const chunks = [];
|
|
@@ -103,7 +105,7 @@ var CsdClient = class {
|
|
|
103
105
|
buf.set(c, off);
|
|
104
106
|
off += c.length;
|
|
105
107
|
}
|
|
106
|
-
return
|
|
108
|
+
return this.parseJson(new TextDecoder().decode(buf));
|
|
107
109
|
}
|
|
108
110
|
get(path) {
|
|
109
111
|
return this.req(path);
|
|
@@ -224,24 +226,24 @@ async function verifyInputValues(client, inputs) {
|
|
|
224
226
|
try {
|
|
225
227
|
info = await client.tx(i.txid);
|
|
226
228
|
} catch {
|
|
227
|
-
return { ok: false, total: 0 };
|
|
229
|
+
return { ok: false, total: 0, badInput: i };
|
|
228
230
|
}
|
|
229
231
|
const body = info?.tx ?? info;
|
|
230
|
-
if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
|
|
232
|
+
if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0, badInput: i };
|
|
231
233
|
let tx, idHex;
|
|
232
234
|
try {
|
|
233
235
|
tx = rpcTxToTx(body);
|
|
234
236
|
idHex = (0, import_csd_codec.txid)(tx);
|
|
235
237
|
} catch {
|
|
236
|
-
return { ok: false, total: 0 };
|
|
238
|
+
return { ok: false, total: 0, badInput: i };
|
|
237
239
|
}
|
|
238
|
-
if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
|
|
240
|
+
if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0, badInput: i };
|
|
239
241
|
const out = tx.outputs[i.vout];
|
|
240
|
-
if (!out) return { ok: false, total: 0 };
|
|
242
|
+
if (!out) return { ok: false, total: 0, badInput: i };
|
|
241
243
|
const v = Number(out.value);
|
|
242
|
-
if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0 };
|
|
244
|
+
if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0, badInput: i };
|
|
243
245
|
total += v;
|
|
244
|
-
if (!Number.isSafeInteger(total)) return { ok: false, total: 0 };
|
|
246
|
+
if (!Number.isSafeInteger(total)) return { ok: false, total: 0, badInput: i };
|
|
245
247
|
}
|
|
246
248
|
return { ok: true, total };
|
|
247
249
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -118,6 +118,12 @@ interface ClientOptions {
|
|
|
118
118
|
* path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
|
|
119
119
|
*/
|
|
120
120
|
maxResponseBytes?: number;
|
|
121
|
+
/**
|
|
122
|
+
* M1: optional JSON body parser (default `JSON.parse`). A verifier that must preserve u64 fields
|
|
123
|
+
* (e.g. a Propose's `expires_epoch`, unbounded on-chain) past 2^53 injects a source-preserving
|
|
124
|
+
* reviver here so the exact value survives to the txid recompute. The default is unchanged.
|
|
125
|
+
*/
|
|
126
|
+
parseJson?: (text: string) => unknown;
|
|
121
127
|
}
|
|
122
128
|
declare class CsdClient {
|
|
123
129
|
private readonly base;
|
|
@@ -125,6 +131,7 @@ declare class CsdClient {
|
|
|
125
131
|
private readonly timeoutMs;
|
|
126
132
|
private readonly retries;
|
|
127
133
|
private readonly maxBytes;
|
|
134
|
+
private readonly parseJson;
|
|
128
135
|
constructor(opts: ClientOptions);
|
|
129
136
|
private req;
|
|
130
137
|
/**
|
|
@@ -194,6 +201,10 @@ declare function verifyInputValues(client: {
|
|
|
194
201
|
}[]): Promise<{
|
|
195
202
|
ok: boolean;
|
|
196
203
|
total: number;
|
|
204
|
+
badInput?: {
|
|
205
|
+
txid: string;
|
|
206
|
+
vout: number;
|
|
207
|
+
};
|
|
197
208
|
}>;
|
|
198
209
|
/** Convert a node /block header JSON into the codec's BlockHeader. */
|
|
199
210
|
declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
|
package/dist/index.d.ts
CHANGED
|
@@ -118,6 +118,12 @@ interface ClientOptions {
|
|
|
118
118
|
* path (audit M2). Default 16 MiB ≫ any real block; raise it for an unusually large block source.
|
|
119
119
|
*/
|
|
120
120
|
maxResponseBytes?: number;
|
|
121
|
+
/**
|
|
122
|
+
* M1: optional JSON body parser (default `JSON.parse`). A verifier that must preserve u64 fields
|
|
123
|
+
* (e.g. a Propose's `expires_epoch`, unbounded on-chain) past 2^53 injects a source-preserving
|
|
124
|
+
* reviver here so the exact value survives to the txid recompute. The default is unchanged.
|
|
125
|
+
*/
|
|
126
|
+
parseJson?: (text: string) => unknown;
|
|
121
127
|
}
|
|
122
128
|
declare class CsdClient {
|
|
123
129
|
private readonly base;
|
|
@@ -125,6 +131,7 @@ declare class CsdClient {
|
|
|
125
131
|
private readonly timeoutMs;
|
|
126
132
|
private readonly retries;
|
|
127
133
|
private readonly maxBytes;
|
|
134
|
+
private readonly parseJson;
|
|
128
135
|
constructor(opts: ClientOptions);
|
|
129
136
|
private req;
|
|
130
137
|
/**
|
|
@@ -194,6 +201,10 @@ declare function verifyInputValues(client: {
|
|
|
194
201
|
}[]): Promise<{
|
|
195
202
|
ok: boolean;
|
|
196
203
|
total: number;
|
|
204
|
+
badInput?: {
|
|
205
|
+
txid: string;
|
|
206
|
+
vout: number;
|
|
207
|
+
};
|
|
197
208
|
}>;
|
|
198
209
|
/** Convert a node /block header JSON into the codec's BlockHeader. */
|
|
199
210
|
declare function rpcHeaderToHeader(h: RpcHeaderJson): BlockHeader;
|
package/dist/index.js
CHANGED
|
@@ -6,9 +6,11 @@ var CsdClient = class {
|
|
|
6
6
|
timeoutMs;
|
|
7
7
|
retries;
|
|
8
8
|
maxBytes;
|
|
9
|
+
parseJson;
|
|
9
10
|
constructor(opts) {
|
|
10
11
|
this.base = opts.baseUrl.replace(/\/+$/, "");
|
|
11
12
|
this.maxBytes = Math.max(1, opts.maxResponseBytes ?? 16 * 1024 * 1024);
|
|
13
|
+
this.parseJson = opts.parseJson ?? JSON.parse;
|
|
12
14
|
const gf = globalThis.fetch;
|
|
13
15
|
this.f = opts.fetch ?? (gf ? gf.bind(globalThis) : gf);
|
|
14
16
|
this.timeoutMs = opts.timeoutMs ?? 1e4;
|
|
@@ -23,10 +25,10 @@ var CsdClient = class {
|
|
|
23
25
|
const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
|
|
24
26
|
if (r.status >= 500 && attempt < maxRetries) {
|
|
25
27
|
lastErr = new Error(`HTTP ${r.status}`);
|
|
26
|
-
} else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
|
|
28
|
+
} else if (!r.ok) throw Object.assign(new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`), { terminal: r.status < 500 });
|
|
27
29
|
else return await this.readCapped(r, path);
|
|
28
30
|
} catch (e) {
|
|
29
|
-
if (attempt >= maxRetries) throw e;
|
|
31
|
+
if (attempt >= maxRetries || e?.terminal) throw e;
|
|
30
32
|
lastErr = e;
|
|
31
33
|
}
|
|
32
34
|
const cap = Math.min(5e3, 250 * 2 ** attempt);
|
|
@@ -50,7 +52,7 @@ var CsdClient = class {
|
|
|
50
52
|
const t = await r.text();
|
|
51
53
|
const byteLen = new TextEncoder().encode(t).length;
|
|
52
54
|
if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
|
|
53
|
-
return
|
|
55
|
+
return this.parseJson(t);
|
|
54
56
|
}
|
|
55
57
|
const reader = body.getReader();
|
|
56
58
|
const chunks = [];
|
|
@@ -76,7 +78,7 @@ var CsdClient = class {
|
|
|
76
78
|
buf.set(c, off);
|
|
77
79
|
off += c.length;
|
|
78
80
|
}
|
|
79
|
-
return
|
|
81
|
+
return this.parseJson(new TextDecoder().decode(buf));
|
|
80
82
|
}
|
|
81
83
|
get(path) {
|
|
82
84
|
return this.req(path);
|
|
@@ -197,24 +199,24 @@ async function verifyInputValues(client, inputs) {
|
|
|
197
199
|
try {
|
|
198
200
|
info = await client.tx(i.txid);
|
|
199
201
|
} catch {
|
|
200
|
-
return { ok: false, total: 0 };
|
|
202
|
+
return { ok: false, total: 0, badInput: i };
|
|
201
203
|
}
|
|
202
204
|
const body = info?.tx ?? info;
|
|
203
|
-
if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
|
|
205
|
+
if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0, badInput: i };
|
|
204
206
|
let tx, idHex;
|
|
205
207
|
try {
|
|
206
208
|
tx = rpcTxToTx(body);
|
|
207
209
|
idHex = codecTxid(tx);
|
|
208
210
|
} catch {
|
|
209
|
-
return { ok: false, total: 0 };
|
|
211
|
+
return { ok: false, total: 0, badInput: i };
|
|
210
212
|
}
|
|
211
|
-
if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
|
|
213
|
+
if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0, badInput: i };
|
|
212
214
|
const out = tx.outputs[i.vout];
|
|
213
|
-
if (!out) return { ok: false, total: 0 };
|
|
215
|
+
if (!out) return { ok: false, total: 0, badInput: i };
|
|
214
216
|
const v = Number(out.value);
|
|
215
|
-
if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0 };
|
|
217
|
+
if (!Number.isSafeInteger(v) || v <= 0) return { ok: false, total: 0, badInput: i };
|
|
216
218
|
total += v;
|
|
217
|
-
if (!Number.isSafeInteger(total)) return { ok: false, total: 0 };
|
|
219
|
+
if (!Number.isSafeInteger(total)) return { ok: false, total: 0, badInput: i };
|
|
218
220
|
}
|
|
219
221
|
return { ok: true, total };
|
|
220
222
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inversealtruism/csd-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.16",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=18"
|
|
6
|
+
},
|
|
4
7
|
"description": "Typed HTTP RPC client for a Compute Substrate node (or the Cairn proxy / a discovered gateway) — tip/block/tx/utxos/proposals/top/domains/mempool/submit/templates.",
|
|
5
8
|
"type": "module",
|
|
6
9
|
"main": "./dist/index.cjs",
|
|
@@ -8,9 +11,14 @@
|
|
|
8
11
|
"types": "./dist/index.d.ts",
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
"import": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"require": {
|
|
19
|
+
"types": "./dist/index.d.cts",
|
|
20
|
+
"default": "./dist/index.cjs"
|
|
21
|
+
}
|
|
14
22
|
}
|
|
15
23
|
},
|
|
16
24
|
"files": [
|
|
@@ -19,12 +27,12 @@
|
|
|
19
27
|
"README.md"
|
|
20
28
|
],
|
|
21
29
|
"dependencies": {
|
|
22
|
-
"@inversealtruism/csd-codec": "0.1.
|
|
30
|
+
"@inversealtruism/csd-codec": "0.1.16"
|
|
23
31
|
},
|
|
24
32
|
"license": "MIT",
|
|
25
33
|
"sideEffects": false,
|
|
26
34
|
"devDependencies": {
|
|
27
|
-
"@inversealtruism/csd-vectors": "0.1.
|
|
35
|
+
"@inversealtruism/csd-vectors": "0.1.15"
|
|
28
36
|
},
|
|
29
37
|
"publishConfig": {
|
|
30
38
|
"access": "public"
|
|
@@ -37,6 +45,6 @@
|
|
|
37
45
|
"homepage": "https://cairn-substrate.com",
|
|
38
46
|
"scripts": {
|
|
39
47
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
40
|
-
"test": "tsx test/client.test.ts"
|
|
48
|
+
"test": "tsx test/client.test.ts && tsx test/hostile.test.ts"
|
|
41
49
|
}
|
|
42
50
|
}
|