space-data-module-sdk 0.9.1 → 0.9.3

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.
@@ -7,6 +7,7 @@ import {
7
7
  decodeHostCall,
8
8
  decodeSqlValues,
9
9
  encodeHostCallResult,
10
+ encodeModuleConfig,
10
11
  encodeSqlValues,
11
12
  } from "../standards/runtimeContracts.js";
12
13
  import * as hostOps from "../generated/runtime/aligned/HostOps_aligned.js";
@@ -192,28 +193,32 @@ function sqlCell(v) {
192
193
  if (typeof v === "string") return { t: "str", v };
193
194
  throw failed(`storage.query_sandboxed: a cell is a ${Array.isArray(v) ? "list" : typeof v} (nested values are not SQL values)`);
194
195
  }
195
- /** plugin.getConfig: the config flattened into dotted-path entries ("targets.0.provider_id"), keys sorted. */
196
- function configEntries(config) {
197
- const entries = [];
198
- const walk = (prefix, value) => {
199
- if (value === null || value === undefined) return;
200
- if (Array.isArray(value)) { value.forEach((item, i) => walk(`${prefix}.${i}`, item)); return; }
201
- if (typeof value === "object" && !(value instanceof Date)) {
202
- for (const key of Object.keys(value).sort()) walk(prefix ? `${prefix}.${key}` : key, value[key]);
203
- return;
204
- }
205
- if (typeof value === "boolean") entries.push({ KEY: prefix, KIND: 2, BOOL_VALUE: value });
206
- else if (typeof value === "string") entries.push({ KEY: prefix, KIND: 0, STRING_VALUE: value });
207
- else if (typeof value === "number" || typeof value === "bigint") entries.push({ KEY: prefix, KIND: 1, NUMBER_VALUE: Number(value) });
208
- else throw failed(`plugin.getConfig: ${prefix} is a ${value instanceof Date ? "Date" : typeof value}`);
209
- };
210
- walk("", config ?? {});
211
- for (const e of entries) {
212
- if (textEncoder.encode(e.KEY).length > 255 || textEncoder.encode(e.STRING_VALUE ?? "").length > 255) {
213
- throw failed(`plugin.getConfig: ${e.KEY} does not fit a ConfigEntry (255 bytes)`, "too-large");
214
- }
196
+ /**
197
+ * plugin.getConfig: the config as one ModuleConfig FlatBuffer (MCFG) on raw "config" - number arrays NumberList, string
198
+ * arrays StringList, nested objects and arrays of objects TableList, keys sorted (encodeModuleConfig). Unset values
199
+ * (null / undefined) are left out; a value with no config kind (a Date, a function, a list mixing kinds) is refused.
200
+ */
201
+ function configOf(value, path) {
202
+ if (value === null || value === undefined) return undefined;
203
+ if (typeof value === "bigint") return Number(value);
204
+ if (Array.isArray(value)) return value.filter((v) => v !== null && v !== undefined).map((v, i) => configOf(v, `${path}.${i}`));
205
+ if (typeof value === "object") {
206
+ if (value instanceof Date || ArrayBuffer.isView(value) || value instanceof ArrayBuffer) throw failed(`plugin.getConfig: ${path} is a ${value.constructor?.name ?? "object"}`);
207
+ const out = {};
208
+ for (const key of Object.keys(value)) { const v = configOf(value[key], path ? `${path}.${key}` : key); if (v !== undefined) out[key] = v; }
209
+ return out;
210
+ }
211
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return value;
212
+ throw failed(`plugin.getConfig: ${path} is a ${typeof value}`);
213
+ }
214
+ function moduleConfig(config) {
215
+ const plain = configOf(config ?? {}, "") ?? {};
216
+ if (Array.isArray(plain) || typeof plain !== "object") throw failed("plugin.getConfig: the config is not an object");
217
+ try {
218
+ return encodeModuleConfig(plain);
219
+ } catch (error) {
220
+ throw failed(`plugin.getConfig: ${error.message}`);
215
221
  }
216
- return entries;
217
222
  }
218
223
  function secretRequest({ params, raw }) {
219
224
  const request = { id: str(params?.ID) };
@@ -333,7 +338,7 @@ const ADAPTERS = {
333
338
  "crypto.ed25519.verify": { in: ({ raw }) => ({ message: raw.data, signature: raw.signature, publicKey: raw.public_key }), out: bool },
334
339
 
335
340
  // ---- the Go node's operations, through the embedder's adapters (adapterOperations.js) ------------------------------
336
- "plugin.getConfig": { in: () => ({}), out: (v) => ({ lists: { entries: configEntries(v) } }) },
341
+ "plugin.getConfig": { in: () => ({}), out: (v) => ({ raw: { config: moduleConfig(v) } }) },
337
342
  "node.peerId": { in: () => ({}), out: nodeText("peer ID") },
338
343
  "node.publicKey": { in: () => ({}), out: nodeText("public key") },
339
344
  "secrets.get": { in: secretRequest, out: (v) => ({ result: fit("SecretResult", { ID: str(v?.id), USERNAME: str(v?.username), CONFIGURED: true }), raw: { secret: bytesOf(v?.secret) ?? new Uint8Array(0) } }) },
@@ -133,7 +133,8 @@ function entryOf(key, value) {
133
133
  else throw new Error(`ModuleConfig: ${key} has no config value kind (${value === null ? "null" : typeof value})`);
134
134
  return e;
135
135
  }
136
- const entriesOf = (object) => Object.keys(object || {}).filter((k) => object[k] !== undefined).sort().map((k) => entryOf(k, object[k]));
136
+ // A key whose value is null or undefined is left out, as the Go node does: "not configured".
137
+ const entriesOf = (object) => Object.keys(object || {}).filter((k) => object[k] !== undefined && object[k] !== null).sort().map((k) => entryOf(k, object[k]));
137
138
  function valueOf(e) {
138
139
  switch (e.KIND) {
139
140
  case configValueKind.String: return e.STRING_VALUE || "";
@@ -489,7 +490,7 @@ export const HOST_OPERATIONS = Object.freeze({
489
490
  "random.bytes": op("RandomBytesParams", null, "random", { rawOut: ["data"] }),
490
491
  "node.publicKey": op(null, null, null, { rawOut: ["value"] }),
491
492
  "node.peerId": op(null, null, null, { rawOut: ["value"] }),
492
- "plugin.getConfig": op(null, null, null, { listsOut: { entries: "ConfigEntry" } }),
493
+ "plugin.getConfig": op(null, null, null, { rawOut: ["config"] }),
493
494
  // network
494
495
  "http.request": op("HttpParams", "HttpResult", "http", { rawIn: ["url", "headers", "body"], rawOut: ["url", "headers", "body"] }),
495
496
  "websocket.exchange": op("WebsocketParams", "WebsocketResult", "websocket", { rawIn: ["url", "protocols", "message"], rawOut: ["url", "body"] }),