space-data-module-sdk 0.9.16 → 0.10.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 (55) hide show
  1. package/docs/_shell/consumer-assets.html +1 -1
  2. package/docs/assets/sdn-chrome/sdn-chrome.js +1 -1
  3. package/docs/assets/vendor/sdn-stack-nav/{1.0.1/sdn-stack-nav.24eb3001afc5448a90cef87e53bb4e599c0ae4dc9e14bafb6d1236e0233c6008.js → 1.2.0/sdn-stack-nav.ba268c55a1551bcacc390ac01347c319335a1660787b55b4b239df00fc3c1a4c.js} +5 -0
  4. package/docs/browser-wasmedge-isomorphic.html +1 -1
  5. package/docs/byo-wasm-quickstart.html +1 -1
  6. package/docs/conformance.html +1 -1
  7. package/docs/events-abi.html +1 -1
  8. package/docs/families/analytics.html +1 -1
  9. package/docs/families/attitude.html +1 -1
  10. package/docs/families/behavior.html +1 -1
  11. package/docs/families/breakup.html +1 -1
  12. package/docs/families/conjunction.html +1 -1
  13. package/docs/families/data-source.html +1 -1
  14. package/docs/families/effects.html +1 -1
  15. package/docs/families/environment.html +1 -1
  16. package/docs/families/estimation.html +1 -1
  17. package/docs/families/gnc.html +1 -1
  18. package/docs/families/maneuver.html +1 -1
  19. package/docs/families/obstruction.html +1 -1
  20. package/docs/families/propagator.html +1 -1
  21. package/docs/families/propulsion.html +1 -1
  22. package/docs/families/reentry.html +1 -1
  23. package/docs/families/rf.html +1 -1
  24. package/docs/families/scheduler.html +1 -1
  25. package/docs/families/sensor.html +1 -1
  26. package/docs/families/signature.html +1 -1
  27. package/docs/flatsql-host-contract.html +16 -44
  28. package/docs/flatsql-host-contract.md +35 -107
  29. package/docs/flatsql-streaming-standard.html +14 -9
  30. package/docs/flatsql-streaming-standard.md +15 -10
  31. package/docs/gpu-module-abi.html +1 -1
  32. package/docs/index.html +1 -1
  33. package/docs/isomorphic-pthreads.html +1 -1
  34. package/docs/isomorphic-sdn-runtime-plan.html +1 -1
  35. package/docs/language-runtime-matrix.html +1 -1
  36. package/docs/module-bundle-runtime-plan.html +1 -1
  37. package/docs/module-publication-standard.html +1 -1
  38. package/docs/node-red-default-node-parity.html +1 -1
  39. package/docs/protect-and-sign.html +1 -1
  40. package/docs/protocol-installation.html +1 -1
  41. package/docs/publication-submission.html +1 -1
  42. package/docs/secrets-capability.html +1 -1
  43. package/docs/testing-harness.html +2 -2
  44. package/docs/testing-harness.md +7 -1
  45. package/docs/tri-runtime-parity-gate.html +1 -1
  46. package/docs/tri-runtime-parity.html +1 -1
  47. package/package.json +5 -5
  48. package/parity/gate.json +3 -3
  49. package/src/index.d.ts +25 -11
  50. package/src/runtime-host/flatbufferStreamIngestor.js +49 -170
  51. package/src/runtime-host/flatsqlQueryProtocol.js +76 -0
  52. package/src/runtime-host/flatsqlRuntimeStore.js +79 -278
  53. package/src/runtime-host/index.js +3 -1
  54. package/src/testing/parityGate.js +1 -1
  55. /package/docs/assets/vendor/sdn-stack-nav/{1.0.1 → 1.2.0}/sdn-stack-nav.36a36359ce18322185e9ff179f88175bab67d5ad84a14d9c08a54f2ff27267e7.css +0 -0
@@ -1,288 +1,89 @@
1
- import { DirectAccessor, FlatSQLDatabase } from "flatsql";
2
- import { decodeGuestText } from "../utils/guestText.js";
1
+ import { FlatSQL } from "flatsql";
2
+ import { encodeFlatSqlQuery, decodeFlatSqlResult } from "./flatsqlQueryProtocol.js";
3
3
 
4
- const RUNTIME_ROW_TABLE = "RuntimeHostRow";
5
- const RUNTIME_ROW_SCHEMA = `
6
- table RuntimeHostRow {
7
- schemaFileId: string;
8
- rowId: ulong;
9
- }
10
- `;
11
-
12
- const encoder = new TextEncoder();
13
-
14
- function clonePayload(payload) {
15
- if (payload === null || payload === undefined) {
16
- return payload ?? null;
17
- }
18
- if (typeof structuredClone === "function") {
19
- return structuredClone(payload);
20
- }
21
- return clonePayloadFallback(payload, new WeakMap());
22
- }
23
-
24
- function clonePayloadFallback(payload, seen) {
25
- if (payload instanceof Date) {
26
- return new Date(payload.getTime());
27
- }
28
- if (payload instanceof RegExp) {
29
- return new RegExp(payload.source, payload.flags);
30
- }
31
- if (payload instanceof Map) {
32
- if (seen.has(payload)) {
33
- return seen.get(payload);
34
- }
35
- const cloned = new Map();
36
- seen.set(payload, cloned);
37
- for (const [key, value] of payload.entries()) {
38
- cloned.set(clonePayloadFallback(key, seen), clonePayloadFallback(value, seen));
39
- }
40
- return cloned;
41
- }
42
- if (payload instanceof Set) {
43
- if (seen.has(payload)) {
44
- return seen.get(payload);
45
- }
46
- const cloned = new Set();
47
- seen.set(payload, cloned);
48
- for (const value of payload.values()) {
49
- cloned.add(clonePayloadFallback(value, seen));
50
- }
51
- return cloned;
52
- }
53
- if (ArrayBuffer.isView(payload)) {
54
- if (payload instanceof DataView) {
55
- return new DataView(
56
- payload.buffer.slice(
57
- payload.byteOffset,
58
- payload.byteOffset + payload.byteLength,
59
- ),
60
- );
61
- }
62
- const clonedBuffer = payload.buffer.slice(
63
- payload.byteOffset,
64
- payload.byteOffset + payload.byteLength,
65
- );
66
- return new payload.constructor(clonedBuffer, 0, payload.length);
67
- }
68
- if (payload instanceof ArrayBuffer) {
69
- return payload.slice(0);
70
- }
71
- if (Array.isArray(payload)) {
72
- if (seen.has(payload)) {
73
- return seen.get(payload);
74
- }
75
- const cloned = [];
76
- seen.set(payload, cloned);
77
- for (const value of payload) {
78
- cloned.push(clonePayloadFallback(value, seen));
79
- }
80
- return cloned;
81
- }
82
- if (typeof payload === "object") {
83
- if (seen.has(payload)) {
84
- return seen.get(payload);
85
- }
86
- const prototype = Object.getPrototypeOf(payload);
87
- const cloned = Object.create(prototype ?? Object.prototype);
88
- seen.set(payload, cloned);
89
- for (const key of Reflect.ownKeys(payload)) {
90
- const descriptor = Object.getOwnPropertyDescriptor(payload, key);
91
- if (!descriptor) {
92
- continue;
93
- }
94
- if ("value" in descriptor) {
95
- descriptor.value = clonePayloadFallback(descriptor.value, seen);
96
- }
97
- Object.defineProperty(cloned, key, descriptor);
98
- }
99
- return cloned;
100
- }
101
- return payload;
102
- }
103
-
104
- function normalizeSchemaFileId(value) {
105
- if (typeof value !== "string" || value.trim().length === 0) {
106
- throw new TypeError("schemaFileId must be a non-empty string");
107
- }
108
- return value.trim();
109
- }
110
-
111
- function normalizeRowHandle(handle) {
112
- if (!handle || typeof handle !== "object") {
113
- throw new TypeError("row handle is required");
114
- }
115
- const schemaFileId = normalizeSchemaFileId(handle.schemaFileId);
116
- const rowId = Number(handle.rowId);
117
- if (!Number.isInteger(rowId) || rowId <= 0) {
118
- throw new TypeError("rowId must be a positive integer");
4
+ const quote = name => `"${name.replaceAll('"', '""')}"`;
5
+ const fileId = value => {
6
+ if (typeof value !== "string" || new TextEncoder().encode(value).length !== 4) {
7
+ throw new TypeError("schemaFileId must be the four-byte FlatBuffer file identifier");
119
8
  }
120
- return { schemaFileId, rowId };
121
- }
122
-
123
- function escapeSqlStringLiteral(value) {
124
- return String(value).replaceAll("'", "''");
125
- }
126
-
127
- function encodeRuntimeRowMetadata({ schemaFileId, rowId }) {
128
- const schemaBytes = encoder.encode(normalizeSchemaFileId(schemaFileId));
129
- const encoded = new Uint8Array(4 + schemaBytes.byteLength + 8);
130
- const view = new DataView(encoded.buffer);
131
- view.setUint32(0, schemaBytes.byteLength, true);
132
- encoded.set(schemaBytes, 4);
133
- view.setBigUint64(4 + schemaBytes.byteLength, BigInt(rowId), true);
134
- return encoded;
135
- }
9
+ return value;
10
+ };
136
11
 
137
- function decodeRuntimeRowMetadata(data) {
138
- const bytes =
139
- data instanceof Uint8Array
140
- ? data
141
- : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
142
- if (bytes.byteLength < 12) {
143
- throw new Error("Invalid runtime row metadata buffer.");
144
- }
145
- const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
146
- const schemaLength = view.getUint32(0, true);
147
- const schemaStart = 4;
148
- const schemaEnd = schemaStart + schemaLength;
149
- const rowIdOffset = schemaEnd;
150
- if (schemaEnd + 8 > bytes.byteLength) {
151
- throw new Error("Invalid runtime row metadata layout.");
152
- }
153
- return {
154
- schemaFileId: decodeGuestText(bytes.subarray(schemaStart, schemaEnd)),
155
- rowId: Number(view.getBigUint64(rowIdOffset, true)),
12
+ /** Durable binary records. Schema compilation and result rendering belong to
13
+ * the caller; the engine owns row IDs, deduplication, indexes and persistence. */
14
+ export function createFlatSqlRuntimeStore(options = {}) {
15
+ const schemas = new Map();
16
+ let opening, closed = false;
17
+ const database = () => {
18
+ if (closed) throw new Error("Runtime row store is closed");
19
+ return opening ??= (async () => {
20
+ const db = options.database ?? await FlatSQL.open(options.engine);
21
+ try {
22
+ for (const spec of options.schemas ?? []) {
23
+ const id = fileId(spec.fileIdentifier);
24
+ await db.registerSchema(spec); schemas.set(id, spec);
25
+ }
26
+ return db;
27
+ } catch (error) { await db.close(); throw error; }
28
+ })();
156
29
  };
157
- }
158
-
159
- function buildRowKey(schemaFileId, rowId) {
160
- return `${normalizeSchemaFileId(schemaFileId)}:${Number(rowId)}`;
161
- }
162
-
163
- function createRuntimeRowAccessor() {
164
- const accessor = new DirectAccessor();
165
- accessor.registerAccessor(RUNTIME_ROW_TABLE, (data, path) => {
166
- const row = decodeRuntimeRowMetadata(data);
167
- let current = row;
168
- for (const segment of Array.isArray(path) ? path : []) {
169
- if (current === null || current === undefined) {
170
- return null;
171
- }
172
- current = current[segment];
30
+ async function registerSchema(schema) {
31
+ const id = fileId(schema.fileIdentifier);
32
+ await (await database()).registerSchema(schema);
33
+ schemas.set(id, schema);
34
+ }
35
+ async function schemaFor(id) {
36
+ id = fileId(id);
37
+ await database();
38
+ if (!schemas.has(id)) {
39
+ const spec = (options.schemas ?? []).find(s => s.fileIdentifier === id)
40
+ ?? await options.resolveSchema?.(id);
41
+ if (!spec || spec.fileIdentifier !== id) throw new Error(`No binary schema registered for ${id}`);
42
+ await registerSchema(spec);
173
43
  }
174
- return current ?? null;
175
- });
176
- accessor.registerBuilder(RUNTIME_ROW_TABLE, (fields) =>
177
- encodeRuntimeRowMetadata({
178
- schemaFileId: fields.schemaFileId,
179
- rowId: Number(fields.rowId),
180
- }),
181
- );
182
- return accessor;
183
- }
184
-
185
- function cloneQueryResult(result) {
186
- return {
187
- columns: Array.from(result?.columns ?? []),
188
- rows: (result?.rows ?? []).map((row) =>
189
- Array.isArray(row) ? row.map((value) => clonePayload(value)) : row,
190
- ),
191
- rowCount: Number(result?.rowCount ?? 0),
192
- };
193
- }
194
-
195
- function rowViewFromQueryRow(row, payloadStore) {
196
- const schemaFileId = String(row[0]);
197
- const rowId = Number(row[1]);
198
- return {
199
- handle: {
200
- schemaFileId,
201
- rowId,
202
- },
203
- payload: clonePayload(payloadStore.get(buildRowKey(schemaFileId, rowId))),
204
- };
205
- }
206
-
207
- export function createFlatSqlRuntimeStore(options = {}) {
208
- const accessor = options.accessor ?? createRuntimeRowAccessor();
209
- const payloadStore = options.payloadStore ?? new Map();
210
- const database =
211
- options.database ??
212
- FlatSQLDatabase.fromSchema(
213
- RUNTIME_ROW_SCHEMA,
214
- accessor,
215
- options.databaseName ?? "runtime-host",
216
- );
217
- const nextRowIdBySchema = new Map();
218
- const existingRows = database.query(
219
- `SELECT schemaFileId, rowId FROM ${RUNTIME_ROW_TABLE} ORDER BY schemaFileId, rowId`,
220
- );
221
- for (const row of existingRows.rows ?? []) {
222
- const schemaFileId = normalizeSchemaFileId(row[0]);
223
- const rowId = Number(row[1]);
224
- if (Number.isInteger(rowId) && rowId > 0) {
225
- nextRowIdBySchema.set(
226
- schemaFileId,
227
- Math.max(nextRowIdBySchema.get(schemaFileId) ?? 0, rowId),
228
- );
44
+ return schemas.get(id);
45
+ }
46
+ async function query(sql, params = [], limits = {}) {
47
+ return decodeFlatSqlResult(await (await database()).execute(encodeFlatSqlQuery(sql, params), limits));
48
+ }
49
+ async function appendRow({ schemaFileId, payload }) {
50
+ const schema = await schemaFor(schemaFileId);
51
+ if (!(payload instanceof Uint8Array) || payload.length < 8) throw new TypeError("payload must be a FlatBuffer Uint8Array");
52
+ if (String.fromCharCode(...payload.subarray(4, 8)) !== schema.fileIdentifier) throw new Error("Record file identifier does not match its schema");
53
+ const frame = new Uint8Array(payload.length + 4);
54
+ new DataView(frame.buffer).setUint32(0, payload.length, true);
55
+ frame.set(payload, 4);
56
+ const receipt = decodeFlatSqlResult(await (await database()).ingest(schema.name, frame));
57
+ const row = receipt.rows[0];
58
+ if (receipt.rows.length !== 1 || Number(row[1]) === 0) throw new Error(`FlatSQL rejected record: ${row?.[3] ?? "missing receipt"}`);
59
+ const rowId = Number(row[2]);
60
+ if (!Number.isSafeInteger(rowId) || rowId < 1) throw new RangeError("Row ID exceeds the host identity range");
61
+ return { schemaFileId: schema.fileIdentifier, rowId };
62
+ }
63
+ async function listRows(schemaFileId) {
64
+ const ids = schemaFileId == null ? [...new Set([...schemas.keys(), ...(options.schemas ?? []).map(s => s.fileIdentifier)])].sort() : [fileId(schemaFileId)];
65
+ const out = [];
66
+ for (const id of ids) {
67
+ const schema = await schemaFor(id);
68
+ const result = await query(`SELECT _rowid,_data FROM ${quote(schema.name)} ORDER BY _rowid`);
69
+ for (const [rowId, payload] of result.rows) {
70
+ const value = Number(rowId);
71
+ if (!Number.isSafeInteger(value) || value < 1) throw new RangeError("Row ID exceeds the host identity range");
72
+ out.push({ handle: { schemaFileId: id, rowId: value }, payload });
73
+ }
229
74
  }
75
+ return out;
230
76
  }
231
-
232
- function appendRow({ schemaFileId, payload = null }) {
233
- const normalizedSchemaFileId = normalizeSchemaFileId(schemaFileId);
234
- const nextRowId = (nextRowIdBySchema.get(normalizedSchemaFileId) ?? 0) + 1;
235
- nextRowIdBySchema.set(normalizedSchemaFileId, nextRowId);
236
- database.insert(RUNTIME_ROW_TABLE, {
237
- schemaFileId: normalizedSchemaFileId,
238
- rowId: nextRowId,
239
- });
240
- payloadStore.set(
241
- buildRowKey(normalizedSchemaFileId, nextRowId),
242
- clonePayload(payload),
243
- );
244
- return {
245
- schemaFileId: normalizedSchemaFileId,
246
- rowId: nextRowId,
247
- };
248
- }
249
-
250
- function resolveRow(handle) {
251
- const normalizedHandle = normalizeRowHandle(handle);
252
- return (
253
- listRows(normalizedHandle.schemaFileId).find(
254
- (row) => row.handle.rowId === normalizedHandle.rowId,
255
- ) ?? null
256
- );
257
- }
258
-
259
- function listRows(schemaFileId = null) {
260
- const normalizedSchemaFileId =
261
- schemaFileId === null || schemaFileId === undefined
262
- ? null
263
- : normalizeSchemaFileId(schemaFileId);
264
- const result =
265
- normalizedSchemaFileId === null
266
- ? database.query(
267
- `SELECT schemaFileId, rowId FROM ${RUNTIME_ROW_TABLE} ORDER BY schemaFileId, rowId`,
268
- )
269
- : database.query(
270
- `SELECT schemaFileId, rowId FROM ${RUNTIME_ROW_TABLE} WHERE schemaFileId = '${escapeSqlStringLiteral(normalizedSchemaFileId)}' ORDER BY rowId`,
271
- );
272
- return (result.rows ?? []).map((row) => rowViewFromQueryRow(row, payloadStore));
77
+ async function resolveRow(handle) {
78
+ if (!Number.isSafeInteger(handle?.rowId) || handle.rowId < 1) throw new TypeError("rowId must be a positive safe integer");
79
+ const schema = await schemaFor(handle.schemaFileId);
80
+ const result = await query(`SELECT _data FROM ${quote(schema.name)} WHERE _rowid=?1`, [handle.rowId]);
81
+ return result.rows.length ? { handle: { schemaFileId: schema.fileIdentifier, rowId: handle.rowId }, payload: result.rows[0][0] } : null;
273
82
  }
274
-
275
- function query(sql) {
276
- if (typeof sql !== "string" || sql.trim().length === 0) {
277
- throw new TypeError("sql must be a non-empty string");
278
- }
279
- return cloneQueryResult(database.query(sql));
83
+ async function close() {
84
+ if (closed) return;
85
+ closed = true;
86
+ if (opening) await (await opening).close();
280
87
  }
281
-
282
- return {
283
- appendRow,
284
- listRows,
285
- query,
286
- resolveRow,
287
- };
88
+ return { registerSchema, appendRow, listRows, query, resolveRow, close };
288
89
  }
@@ -46,7 +46,7 @@ function listCapabilityOperations(capabilities) {
46
46
  }
47
47
 
48
48
  export function createRuntimeHost(options = {}) {
49
- const rows = options.rows ?? createFlatSqlRuntimeStore();
49
+ const rows = options.rows ?? createFlatSqlRuntimeStore(options.rowStore);
50
50
  const regions = options.regions ?? createRuntimeRegionStore();
51
51
  const moduleRegistry = options.moduleRegistry ?? createModuleRegistry();
52
52
  const capabilities = createCapabilityRegistry(options.capabilities);
@@ -114,3 +114,5 @@ export {
114
114
  // Flow-tier validating method registry (vendored sdn-flow semantics; distinct
115
115
  // from createModuleRegistry's module-lifecycle registry).
116
116
  export { MethodRegistry } from "./methodRegistry.js";
117
+
118
+ export { encodeFlatSqlQuery, decodeFlatSqlResult } from "./flatsqlQueryProtocol.js";
@@ -279,7 +279,7 @@ export function makeExternalArtifact(spec) {
279
279
  // --- WasmEdge probe lanes -------------------------------------------------------
280
280
 
281
281
  const UNKNOWN_IMPORT_RE =
282
- /unknown import[\s\S]*?When linking module:\s*"([^"]*)"\s*,\s*function name:\s*"([^"]*)"/;
282
+ /unknown import[\s\S]*?When linking module:\s*"([^"]*)"\s*,\s*(?:function|memory|table|global) name:\s*"([^"]*)"/;
283
283
 
284
284
  /**
285
285
  * Classify a bare-WasmEdge probe run.