space-data-module-sdk 0.5.4 → 0.5.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "space-data-module-sdk",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
4
4
  "description": "Module SDK for building, validating, signing, and deploying WebAssembly modules on the Space Data Network.",
5
5
  "type": "module",
6
6
  "types": "./src/index.d.ts",
@@ -54,6 +54,7 @@ export interface SharedEmceptionSession {
54
54
  }
55
55
 
56
56
  export function createSharedEmceptionSession(): SharedEmceptionSession;
57
+ export function createIsolatedEmceptionSession(): SharedEmceptionSession;
57
58
  export function loadSharedEmception(): Promise<unknown>;
58
59
  export function withSharedEmception<T>(
59
60
  task: (handle: SharedEmceptionHandle) => T | Promise<T>,
@@ -1,6 +1,7 @@
1
1
  import path from "node:path";
2
2
 
3
3
  import {
4
+ createEmceptionController,
4
5
  getSharedEmceptionController,
5
6
  loadEmception,
6
7
  runWithEmceptionLock,
@@ -180,6 +181,10 @@ export function createSharedEmceptionSession() {
180
181
  return new SharedEmceptionSession();
181
182
  }
182
183
 
184
+ export function createIsolatedEmceptionSession() {
185
+ return new SharedEmceptionSession(createEmceptionController());
186
+ }
187
+
183
188
  export async function loadSharedEmception() {
184
189
  return loadEmception();
185
190
  }
@@ -225,6 +225,10 @@ export function getSharedEmceptionController() {
225
225
  return sharedEmceptionController;
226
226
  }
227
227
 
228
+ export function createEmceptionController() {
229
+ return new EmceptionController();
230
+ }
231
+
228
232
  export async function loadEmception() {
229
233
  return sharedEmceptionController.load();
230
234
  }
@@ -18,6 +18,7 @@ export type {
18
18
  } from "./emception.js";
19
19
 
20
20
  export {
21
+ createIsolatedEmceptionSession,
21
22
  createSharedEmceptionSession,
22
23
  loadSharedEmception,
23
24
  withSharedEmception,
@@ -6,6 +6,7 @@ export {
6
6
  } from "./compileModule.js";
7
7
 
8
8
  export {
9
+ createIsolatedEmceptionSession,
9
10
  createSharedEmceptionSession,
10
11
  loadSharedEmception,
11
12
  withSharedEmception,
@@ -175,6 +175,44 @@ function validateStringField(issues, value, location, label) {
175
175
  return true;
176
176
  }
177
177
 
178
+ function validateCapabilityEntry(capability, issues, location) {
179
+ if (isNonEmptyString(capability)) {
180
+ return capability;
181
+ }
182
+ if (!capability || typeof capability !== "object" || Array.isArray(capability)) {
183
+ pushIssue(
184
+ issues,
185
+ "error",
186
+ "invalid-capability",
187
+ "Capability entries must be non-empty strings or host capability records.",
188
+ location,
189
+ );
190
+ return null;
191
+ }
192
+ if (!validateStringField(issues, capability.capability, `${location}.capability`, "Capability id")) {
193
+ return null;
194
+ }
195
+ validateOptionalStringField(
196
+ issues,
197
+ capability.scope,
198
+ `${location}.scope`,
199
+ "Capability scope",
200
+ );
201
+ validateOptionalBooleanField(
202
+ issues,
203
+ capability.required,
204
+ `${location}.required`,
205
+ "Capability required",
206
+ );
207
+ validateOptionalStringField(
208
+ issues,
209
+ capability.description,
210
+ `${location}.description`,
211
+ "Capability description",
212
+ );
213
+ return capability.capability;
214
+ }
215
+
178
216
  function validateIntegerField(
179
217
  issues,
180
218
  value,
@@ -1066,8 +1104,9 @@ export function validatePluginManifest(manifest, options = {}) {
1066
1104
  validateStringField(issues, manifest.version, `${sourceName}.version`, "version");
1067
1105
  validateStringField(issues, manifest.pluginFamily, `${sourceName}.pluginFamily`, "pluginFamily");
1068
1106
 
1069
- const declaredCapabilities = manifest.capabilities;
1070
- if (!Array.isArray(declaredCapabilities)) {
1107
+ const rawDeclaredCapabilities = manifest.capabilities;
1108
+ let declaredCapabilities = null;
1109
+ if (!Array.isArray(rawDeclaredCapabilities)) {
1071
1110
  pushIssue(
1072
1111
  issues,
1073
1112
  "warning",
@@ -1076,38 +1115,39 @@ export function validatePluginManifest(manifest, options = {}) {
1076
1115
  `${sourceName}.capabilities`,
1077
1116
  );
1078
1117
  } else {
1118
+ declaredCapabilities = [];
1079
1119
  const seenCapabilities = new Set();
1080
- for (const capability of declaredCapabilities) {
1081
- if (!isNonEmptyString(capability)) {
1082
- pushIssue(
1083
- issues,
1084
- "error",
1085
- "invalid-capability",
1086
- "Capability entries must be non-empty strings.",
1087
- `${sourceName}.capabilities`,
1088
- );
1089
- continue;
1120
+ rawDeclaredCapabilities.forEach((capability, index) => {
1121
+ const normalizedCapability = validateCapabilityEntry(
1122
+ capability,
1123
+ issues,
1124
+ `${sourceName}.capabilities[${index}]`,
1125
+ );
1126
+ if (!normalizedCapability) {
1127
+ return;
1090
1128
  }
1091
- if (seenCapabilities.has(capability)) {
1129
+ declaredCapabilities.push(normalizedCapability);
1130
+ if (seenCapabilities.has(normalizedCapability)) {
1092
1131
  pushIssue(
1093
1132
  issues,
1094
1133
  "warning",
1095
1134
  "duplicate-capability",
1096
- `Capability "${capability}" is declared more than once.`,
1135
+ `Capability "${normalizedCapability}" is declared more than once.`,
1097
1136
  `${sourceName}.capabilities`,
1098
1137
  );
1138
+ return;
1099
1139
  }
1100
- seenCapabilities.add(capability);
1101
- if (!RecommendedCapabilitySet.has(capability)) {
1140
+ seenCapabilities.add(normalizedCapability);
1141
+ if (!RecommendedCapabilitySet.has(normalizedCapability)) {
1102
1142
  pushIssue(
1103
1143
  issues,
1104
1144
  "warning",
1105
1145
  "noncanonical-capability",
1106
- `Capability "${capability}" is not in the current canonical SDN coarse capability set.`,
1146
+ `Capability "${normalizedCapability}" is not in the current canonical SDN coarse capability set.`,
1107
1147
  `${sourceName}.capabilities`,
1108
1148
  );
1109
1149
  }
1110
- }
1150
+ });
1111
1151
  }
1112
1152
  validateRuntimeTargets(
1113
1153
  manifest.runtimeTargets,
package/src/host/abi.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { bytesToBase64 } from "../utils/encoding.js";
2
+
1
3
  const textDecoder = new TextDecoder();
2
4
  const textEncoder = new TextEncoder();
3
5
 
@@ -14,6 +16,7 @@ export const NodeHostSyncHostcallOperations = Object.freeze([
14
16
  "clock.now",
15
17
  "clock.monotonicNow",
16
18
  "clock.nowIso",
19
+ "random.bytes",
17
20
  "schedule.parse",
18
21
  "schedule.matches",
19
22
  "schedule.next",
@@ -105,6 +108,47 @@ function isPromiseLike(value) {
105
108
  );
106
109
  }
107
110
 
111
+ function encodeHostcallValue(value) {
112
+ if (value === undefined) {
113
+ return undefined;
114
+ }
115
+ if (
116
+ value instanceof Uint8Array ||
117
+ value instanceof ArrayBuffer ||
118
+ ArrayBuffer.isView(value)
119
+ ) {
120
+ const bytes =
121
+ value instanceof Uint8Array
122
+ ? value
123
+ : new Uint8Array(
124
+ value.buffer ?? value,
125
+ value.byteOffset ?? 0,
126
+ value.byteLength ?? value.byteLength,
127
+ );
128
+ return {
129
+ __type: "bytes",
130
+ base64: bytesToBase64(bytes),
131
+ };
132
+ }
133
+ if (value instanceof Date) {
134
+ return value.toISOString();
135
+ }
136
+ if (typeof value === "bigint") {
137
+ return value.toString();
138
+ }
139
+ if (Array.isArray(value)) {
140
+ return value.map((entry) => encodeHostcallValue(entry));
141
+ }
142
+ if (value && typeof value === "object") {
143
+ return Object.fromEntries(
144
+ Object.entries(value)
145
+ .filter(([, entry]) => entry !== undefined)
146
+ .map(([key, entry]) => [key, encodeHostcallValue(entry)]),
147
+ );
148
+ }
149
+ return value;
150
+ }
151
+
108
152
  export function dispatchNodeHostSyncOperation(host, operation, params = null) {
109
153
  const normalized = assertNonEmptyString(operation, "Hostcall operation");
110
154
  switch (normalized) {
@@ -124,6 +168,8 @@ export function dispatchNodeHostSyncOperation(host, operation, params = null) {
124
168
  return host.clock.monotonicNow();
125
169
  case "clock.nowIso":
126
170
  return host.clock.nowIso();
171
+ case "random.bytes":
172
+ return host.random.bytes(params?.length);
127
173
  case "schedule.parse":
128
174
  return host.schedule.parse(params?.expression);
129
175
  case "schedule.matches":
@@ -202,7 +248,7 @@ export function createJsonHostcallBridge(options = {}) {
202
248
  }
203
249
  setEnvelope(HOSTCALL_STATUS_OK, {
204
250
  ok: true,
205
- result,
251
+ result: encodeHostcallValue(result),
206
252
  });
207
253
  return HOSTCALL_STATUS_OK;
208
254
  } catch (error) {
@@ -1,6 +1,7 @@
1
1
  import dgram from "node:dgram";
2
2
  import net from "node:net";
3
3
  import path from "node:path";
4
+ import { randomBytes as nodeRandomBytes } from "node:crypto";
4
5
  import { spawn } from "node:child_process";
5
6
  import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
6
7
  import { performance } from "node:perf_hooks";
@@ -21,7 +22,6 @@ import {
21
22
  ed25519Sign,
22
23
  ed25519Verify,
23
24
  hkdfBytes,
24
- randomBytes,
25
25
  secp256k1PublicKey,
26
26
  secp256k1SignDigest,
27
27
  secp256k1VerifyDigest,
@@ -1422,10 +1422,10 @@ export class NodeHost {
1422
1422
  });
1423
1423
 
1424
1424
  this.random = Object.freeze({
1425
- bytes: async (length) =>
1426
- this.#withCapability("random", "random.bytes", async () => {
1425
+ bytes: (length) =>
1426
+ this.#withCapability("random", "random.bytes", () => {
1427
1427
  const size = assertNonNegativeInteger(length, "Random byte length");
1428
- return randomBytes(size);
1428
+ return nodeRandomBytes(size);
1429
1429
  }),
1430
1430
  });
1431
1431
 
package/src/index.d.ts CHANGED
@@ -638,6 +638,7 @@ export type {
638
638
  } from "./compiler/emception.js";
639
639
 
640
640
  export {
641
+ createIsolatedEmceptionSession,
641
642
  createSharedEmceptionSession,
642
643
  loadSharedEmception,
643
644
  withSharedEmception,
@@ -924,7 +925,7 @@ export class NodeHost {
924
925
  nowIso(): string;
925
926
  };
926
927
  random: {
927
- bytes(length: number): Promise<Uint8Array>;
928
+ bytes(length: number): Uint8Array;
928
929
  };
929
930
  timers: {
930
931
  delay(ms: number, options?: { signal?: unknown }): Promise<void>;
@@ -1,6 +1,10 @@
1
1
  import * as flatbuffers from "flatbuffers";
2
2
 
3
3
  import {
4
+ CapabilityKind,
5
+ DrainPolicy,
6
+ HostCapabilityT,
7
+ PluginFamily,
4
8
  PluginManifest,
5
9
  PluginManifestT,
6
10
  } from "../generated/orbpro/manifest.js";
@@ -21,6 +25,88 @@ function toByteBuffer(data) {
21
25
  );
22
26
  }
23
27
 
28
+ function normalizeEnumName(name, { separator = "_", lowercase = true } = {}) {
29
+ if (typeof name !== "string" || name.length === 0) {
30
+ return null;
31
+ }
32
+ const normalized = name.trim();
33
+ if (!normalized) {
34
+ return null;
35
+ }
36
+ const joined = normalized.replace(/_/g, separator);
37
+ return lowercase ? joined.toLowerCase() : joined;
38
+ }
39
+
40
+ function normalizePluginFamilyName(value) {
41
+ if (typeof value === "number" && typeof PluginFamily[value] === "string") {
42
+ return normalizeEnumName(PluginFamily[value], { separator: "_" });
43
+ }
44
+ return normalizeEnumName(value, { separator: "_" });
45
+ }
46
+
47
+ function normalizeDrainPolicyName(value) {
48
+ if (typeof value === "number" && typeof DrainPolicy[value] === "string") {
49
+ return normalizeEnumName(DrainPolicy[value], { separator: "-" });
50
+ }
51
+ return normalizeEnumName(value, { separator: "-" });
52
+ }
53
+
54
+ function normalizeCapabilityName(value) {
55
+ if (
56
+ typeof value === "number" &&
57
+ typeof CapabilityKind[value] === "string"
58
+ ) {
59
+ return normalizeEnumName(CapabilityKind[value], { separator: "_" });
60
+ }
61
+ return normalizeEnumName(value, { separator: "_" });
62
+ }
63
+
64
+ function normalizeDecodedCapabilities(value) {
65
+ if (!Array.isArray(value)) {
66
+ return [];
67
+ }
68
+ return value
69
+ .map((entry) => {
70
+ if (typeof entry === "string") {
71
+ return normalizeCapabilityName(entry);
72
+ }
73
+ if (!(entry instanceof HostCapabilityT) && (!entry || typeof entry !== "object")) {
74
+ return null;
75
+ }
76
+ const capability = normalizeCapabilityName(entry.capability);
77
+ if (!capability) {
78
+ return null;
79
+ }
80
+ const scope =
81
+ typeof entry.scope === "string" && entry.scope.trim().length > 0
82
+ ? entry.scope.trim()
83
+ : null;
84
+ const description =
85
+ typeof entry.description === "string" &&
86
+ entry.description.trim().length > 0
87
+ ? entry.description.trim()
88
+ : null;
89
+ const required = entry.required !== false;
90
+ if (!scope && !description && required) {
91
+ return capability;
92
+ }
93
+ return {
94
+ capability,
95
+ ...(scope ? { scope } : {}),
96
+ ...(required === false ? { required: false } : {}),
97
+ ...(description ? { description } : {}),
98
+ };
99
+ })
100
+ .filter(Boolean);
101
+ }
102
+
103
+ function normalizeDecodedMethod(method = {}) {
104
+ return {
105
+ ...method,
106
+ drainPolicy: normalizeDrainPolicyName(method.drainPolicy),
107
+ };
108
+ }
109
+
24
110
  export function decodePluginManifest(data) {
25
111
  const bb = toByteBuffer(data);
26
112
  if (!PluginManifest.bufferHasIdentifier(bb)) {
@@ -29,6 +115,11 @@ export function decodePluginManifest(data) {
29
115
  const unpacked = PluginManifest.getRootAsPluginManifest(bb).unpack();
30
116
  return {
31
117
  ...unpacked,
118
+ pluginFamily: normalizePluginFamilyName(unpacked.pluginFamily),
119
+ capabilities: normalizeDecodedCapabilities(unpacked.capabilities),
120
+ methods: Array.isArray(unpacked.methods)
121
+ ? unpacked.methods.map((method) => normalizeDecodedMethod(method))
122
+ : [],
32
123
  invokeSurfaces: Array.isArray(unpacked.invokeSurfaces)
33
124
  ? unpacked.invokeSurfaces
34
125
  .map((value) => normalizeInvokeSurfaceName(value))
@@ -32,11 +32,11 @@ const CapabilitySurfaceMatrix = Object.freeze({
32
32
  wasi: true,
33
33
  standaloneWasi: true,
34
34
  wasmedge: true,
35
- syncHostcall: false,
35
+ syncHostcall: true,
36
36
  nodeHostApi: true,
37
37
  notes: [
38
38
  "WASI random_get is available to standalone guests.",
39
- "The current sync hostcall ABI does not expose random.bytes.",
39
+ "The sync hostcall ABI exposes random.bytes through a canonical base64 byte envelope.",
40
40
  ],
41
41
  }),
42
42
  timers: Object.freeze({