@inversealtruism/csd-client 0.1.12 → 0.1.13

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/dist/index.cjs CHANGED
@@ -42,17 +42,18 @@ var CsdClient = class {
42
42
  this.retries = Math.max(0, opts.retries ?? 0);
43
43
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
44
44
  }
45
- async req(path, init) {
45
+ async req(path, init, opts) {
46
+ const maxRetries = opts?.noRetry ? 0 : this.retries;
46
47
  let lastErr;
47
48
  for (let attempt = 0; ; attempt++) {
48
49
  try {
49
50
  const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
50
- if (r.status >= 500 && attempt < this.retries) {
51
+ if (r.status >= 500 && attempt < maxRetries) {
51
52
  lastErr = new Error(`HTTP ${r.status}`);
52
53
  } else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
53
54
  else return await this.readCapped(r, path);
54
55
  } catch (e) {
55
- if (attempt >= this.retries) throw e;
56
+ if (attempt >= maxRetries) throw e;
56
57
  lastErr = e;
57
58
  }
58
59
  const cap = Math.min(5e3, 250 * 2 ** attempt);
@@ -74,7 +75,8 @@ var CsdClient = class {
74
75
  const body = r.body;
75
76
  if (!body || typeof body.getReader !== "function") {
76
77
  const t = await r.text();
77
- if (t.length > max) throw new Error(`GET ${path} \u2192 response too large (${t.length} > ${max} bytes)`);
78
+ const byteLen = new TextEncoder().encode(t).length;
79
+ if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
78
80
  return JSON.parse(t);
79
81
  }
80
82
  const reader = body.getReader();
@@ -106,8 +108,8 @@ var CsdClient = class {
106
108
  get(path) {
107
109
  return this.req(path);
108
110
  }
109
- post(path, body) {
110
- return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
111
+ post(path, body, opts) {
112
+ return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }, opts);
111
113
  }
112
114
  // The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
113
115
  // can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
@@ -139,8 +141,11 @@ var CsdClient = class {
139
141
  utxos(addr, opts = {}) {
140
142
  return this.get(`/utxos/${addr}${opts.available === false ? "" : "?available=true"}`);
141
143
  }
144
+ // getOk: a not-found proposal returns {ok:false}@200; without this the caller reads .domain/.uri off a
145
+ // malformed object instead of seeing the error (audit M6). (tx() deliberately keeps its bare get — its
146
+ // {ok:false} is a documented VALID "not yet in a block" state that waitForTx/verifyInputValues handle.)
142
147
  proposal(id) {
143
- return this.get(`/proposal/${id}`);
148
+ return this.getOk(`/proposal/${id}`);
144
149
  }
145
150
  proposals(domain, limit = 40) {
146
151
  return this.get(`/proposals/${encodeURIComponent(domain)}/${limit}`);
@@ -186,7 +191,7 @@ var CsdClient = class {
186
191
  * alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
187
192
  */
188
193
  submit(nodeJsonTx) {
189
- return this.post("/tx/submit", { tx: nodeJsonTx });
194
+ return this.post("/tx/submit", { tx: nodeJsonTx }, { noRetry: true });
190
195
  }
191
196
  /** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
192
197
  async submitOrThrow(nodeJsonTx) {
@@ -223,13 +228,14 @@ async function verifyInputValues(client, inputs) {
223
228
  }
224
229
  const body = info?.tx ?? info;
225
230
  if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
226
- let tx;
231
+ let tx, idHex;
227
232
  try {
228
233
  tx = rpcTxToTx(body);
234
+ idHex = (0, import_csd_codec.txid)(tx);
229
235
  } catch {
230
236
  return { ok: false, total: 0 };
231
237
  }
232
- if (norm((0, import_csd_codec.txid)(tx)) !== norm(i.txid)) return { ok: false, total: 0 };
238
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
233
239
  const out = tx.outputs[i.vout];
234
240
  if (!out) return { ok: false, total: 0 };
235
241
  const v = Number(out.value);
package/dist/index.js CHANGED
@@ -15,17 +15,18 @@ var CsdClient = class {
15
15
  this.retries = Math.max(0, opts.retries ?? 0);
16
16
  if (!this.f) throw new Error("no fetch available \u2014 pass opts.fetch");
17
17
  }
18
- async req(path, init) {
18
+ async req(path, init, opts) {
19
+ const maxRetries = opts?.noRetry ? 0 : this.retries;
19
20
  let lastErr;
20
21
  for (let attempt = 0; ; attempt++) {
21
22
  try {
22
23
  const r = await this.f(`${this.base}${path}`, { ...init, signal: AbortSignal.timeout(this.timeoutMs) });
23
- if (r.status >= 500 && attempt < this.retries) {
24
+ if (r.status >= 500 && attempt < maxRetries) {
24
25
  lastErr = new Error(`HTTP ${r.status}`);
25
26
  } else if (!r.ok) throw new Error(`${init?.method ?? "GET"} ${path} \u2192 HTTP ${r.status}`);
26
27
  else return await this.readCapped(r, path);
27
28
  } catch (e) {
28
- if (attempt >= this.retries) throw e;
29
+ if (attempt >= maxRetries) throw e;
29
30
  lastErr = e;
30
31
  }
31
32
  const cap = Math.min(5e3, 250 * 2 ** attempt);
@@ -47,7 +48,8 @@ var CsdClient = class {
47
48
  const body = r.body;
48
49
  if (!body || typeof body.getReader !== "function") {
49
50
  const t = await r.text();
50
- if (t.length > max) throw new Error(`GET ${path} \u2192 response too large (${t.length} > ${max} bytes)`);
51
+ const byteLen = new TextEncoder().encode(t).length;
52
+ if (byteLen > max) throw new Error(`GET ${path} \u2192 response too large (${byteLen} > ${max} bytes)`);
51
53
  return JSON.parse(t);
52
54
  }
53
55
  const reader = body.getReader();
@@ -79,8 +81,8 @@ var CsdClient = class {
79
81
  get(path) {
80
82
  return this.req(path);
81
83
  }
82
- post(path, body) {
83
- return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
84
+ post(path, body, opts) {
85
+ return this.req(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }, opts);
84
86
  }
85
87
  // The node returns application errors as `{ok:false, err}` with HTTP **200**, so a bare `get()`
86
88
  // can't see them. For endpoints whose `{ok:false}` result is useless to the caller (a missing
@@ -112,8 +114,11 @@ var CsdClient = class {
112
114
  utxos(addr, opts = {}) {
113
115
  return this.get(`/utxos/${addr}${opts.available === false ? "" : "?available=true"}`);
114
116
  }
117
+ // getOk: a not-found proposal returns {ok:false}@200; without this the caller reads .domain/.uri off a
118
+ // malformed object instead of seeing the error (audit M6). (tx() deliberately keeps its bare get — its
119
+ // {ok:false} is a documented VALID "not yet in a block" state that waitForTx/verifyInputValues handle.)
115
120
  proposal(id) {
116
- return this.get(`/proposal/${id}`);
121
+ return this.getOk(`/proposal/${id}`);
117
122
  }
118
123
  proposals(domain, limit = 40) {
119
124
  return this.get(`/proposals/${encodeURIComponent(domain)}/${limit}`);
@@ -159,7 +164,7 @@ var CsdClient = class {
159
164
  * alone mistakes a rejected tx for a broadcast one. Use `submitOrThrow` if you want a hard failure.
160
165
  */
161
166
  submit(nodeJsonTx) {
162
- return this.post("/tx/submit", { tx: nodeJsonTx });
167
+ return this.post("/tx/submit", { tx: nodeJsonTx }, { noRetry: true });
163
168
  }
164
169
  /** As `submit`, but throws on node rejection (`ok:false`) instead of returning a misleading txid. */
165
170
  async submitOrThrow(nodeJsonTx) {
@@ -196,13 +201,14 @@ async function verifyInputValues(client, inputs) {
196
201
  }
197
202
  const body = info?.tx ?? info;
198
203
  if (!body || !Array.isArray(body.outputs) || !Array.isArray(body.inputs)) return { ok: false, total: 0 };
199
- let tx;
204
+ let tx, idHex;
200
205
  try {
201
206
  tx = rpcTxToTx(body);
207
+ idHex = codecTxid(tx);
202
208
  } catch {
203
209
  return { ok: false, total: 0 };
204
210
  }
205
- if (norm(codecTxid(tx)) !== norm(i.txid)) return { ok: false, total: 0 };
211
+ if (norm(idHex) !== norm(i.txid)) return { ok: false, total: 0 };
206
212
  const out = tx.outputs[i.vout];
207
213
  if (!out) return { ok: false, total: 0 };
208
214
  const v = Number(out.value);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inversealtruism/csd-client",
3
- "version": "0.1.12",
3
+ "version": "0.1.13",
4
4
  "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
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -19,12 +19,12 @@
19
19
  "README.md"
20
20
  ],
21
21
  "dependencies": {
22
- "@inversealtruism/csd-codec": "0.1.12"
22
+ "@inversealtruism/csd-codec": "0.1.13"
23
23
  },
24
24
  "license": "MIT",
25
25
  "sideEffects": false,
26
26
  "devDependencies": {
27
- "@inversealtruism/csd-vectors": "0.1.12"
27
+ "@inversealtruism/csd-vectors": "0.1.13"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public"