space-data-module-sdk 0.9.0 → 0.9.1
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 +1 -1
- package/schemas/runtime/HostOps.fbs +24 -22
- package/schemas/runtime/IngestRequest.fbs +3 -0
- package/src/capabilities.js +3 -0
- package/src/generated/runtime/aligned/HostOps_aligned.js +133 -144
- package/src/generated/runtime/aligned/HostOps_aligned.ts +138 -153
- package/src/generated/runtime/aligned/HostOps_enums.js +1 -3
- package/src/generated/runtime/aligned/HostOps_enums.ts +0 -1
- package/src/generated/runtime/cpp/HostOps_aligned.h +111 -118
- package/src/generated/runtime/cpp/IngestRequest_generated.h +70 -5
- package/src/generated/runtime/sdn/runtime/Provenance.js +37 -5
- package/src/generated/runtime/sdn/runtime/Provenance.ts +42 -5
- package/src/host/abi.js +12 -3
- package/src/host/adapterOperations.js +79 -0
- package/src/host/browserHost.js +63 -2
- package/src/host/cpp/typedHostcall.hpp +51 -0
- package/src/host/hostWorkerBundleSources.js +3 -3
- package/src/host/nodeHost.js +49 -0
- package/src/host/typedDispatch.js +353 -1
- package/src/index.d.ts +25 -2
- package/src/standards/runtimeContracts.js +67 -7
|
@@ -2,6 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import * as flatbuffers from "flatbuffers";
|
|
5
|
+
import { Header } from "./Header.js";
|
|
5
6
|
class Provenance {
|
|
6
7
|
constructor() {
|
|
7
8
|
__publicField(this, "bb", null);
|
|
@@ -94,8 +95,20 @@ class Provenance {
|
|
|
94
95
|
const offset = this.bb.__offset(this.bb_pos, 32);
|
|
95
96
|
return offset ? !!this.bb.readInt8(this.bb_pos + offset) : false;
|
|
96
97
|
}
|
|
98
|
+
/**
|
|
99
|
+
* Descriptive provenance the parser has no field above for - the method, units, the date range covered, counts
|
|
100
|
+
* of what was dropped or filled - as name / value pairs, kept in order (a name may repeat). Values are text.
|
|
101
|
+
*/
|
|
102
|
+
ATTRIBUTES(index, obj) {
|
|
103
|
+
const offset = this.bb.__offset(this.bb_pos, 34);
|
|
104
|
+
return offset ? (obj || new Header()).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null;
|
|
105
|
+
}
|
|
106
|
+
attributesLength() {
|
|
107
|
+
const offset = this.bb.__offset(this.bb_pos, 34);
|
|
108
|
+
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
|
|
109
|
+
}
|
|
97
110
|
static startProvenance(builder) {
|
|
98
|
-
builder.startObject(
|
|
111
|
+
builder.startObject(16);
|
|
99
112
|
}
|
|
100
113
|
static addSourceUrl(builder, SOURCE_URLOffset) {
|
|
101
114
|
builder.addFieldOffset(0, SOURCE_URLOffset, 0);
|
|
@@ -172,11 +185,24 @@ class Provenance {
|
|
|
172
185
|
static addFromCache(builder, FROM_CACHE) {
|
|
173
186
|
builder.addFieldInt8(14, +FROM_CACHE, 0);
|
|
174
187
|
}
|
|
188
|
+
static addAttributes(builder, ATTRIBUTESOffset) {
|
|
189
|
+
builder.addFieldOffset(15, ATTRIBUTESOffset, 0);
|
|
190
|
+
}
|
|
191
|
+
static createAttributesVector(builder, data) {
|
|
192
|
+
builder.startVector(4, data.length, 4);
|
|
193
|
+
for (let i = data.length - 1; i >= 0; i--) {
|
|
194
|
+
builder.addOffset(data[i]);
|
|
195
|
+
}
|
|
196
|
+
return builder.endVector();
|
|
197
|
+
}
|
|
198
|
+
static startAttributesVector(builder, numElems) {
|
|
199
|
+
builder.startVector(4, numElems, 4);
|
|
200
|
+
}
|
|
175
201
|
static endProvenance(builder) {
|
|
176
202
|
const offset = builder.endObject();
|
|
177
203
|
return offset;
|
|
178
204
|
}
|
|
179
|
-
static createProvenance(builder, SOURCE_URLOffset, HTTP_STATUS, ETAGOffset, LAST_MODIFIEDOffset, CONTENT_TYPEOffset, RETRIEVED_MS, PARSEROffset, PARSER_VERSIONOffset, SOURCE_SHA256Offset, NORMALIZED_SHA256Offset, NORMALIZED_COUNT, SCHEMASOffset, SCHEMA_COUNTSOffset, WARNINGSOffset, FROM_CACHE) {
|
|
205
|
+
static createProvenance(builder, SOURCE_URLOffset, HTTP_STATUS, ETAGOffset, LAST_MODIFIEDOffset, CONTENT_TYPEOffset, RETRIEVED_MS, PARSEROffset, PARSER_VERSIONOffset, SOURCE_SHA256Offset, NORMALIZED_SHA256Offset, NORMALIZED_COUNT, SCHEMASOffset, SCHEMA_COUNTSOffset, WARNINGSOffset, FROM_CACHE, ATTRIBUTESOffset) {
|
|
180
206
|
Provenance.startProvenance(builder);
|
|
181
207
|
Provenance.addSourceUrl(builder, SOURCE_URLOffset);
|
|
182
208
|
Provenance.addHttpStatus(builder, HTTP_STATUS);
|
|
@@ -193,6 +219,7 @@ class Provenance {
|
|
|
193
219
|
Provenance.addSchemaCounts(builder, SCHEMA_COUNTSOffset);
|
|
194
220
|
Provenance.addWarnings(builder, WARNINGSOffset);
|
|
195
221
|
Provenance.addFromCache(builder, FROM_CACHE);
|
|
222
|
+
Provenance.addAttributes(builder, ATTRIBUTESOffset);
|
|
196
223
|
return Provenance.endProvenance(builder);
|
|
197
224
|
}
|
|
198
225
|
unpack() {
|
|
@@ -211,7 +238,8 @@ class Provenance {
|
|
|
211
238
|
this.bb.createScalarList(this.SCHEMAS.bind(this), this.schemasLength()),
|
|
212
239
|
this.bb.createScalarList(this.SCHEMA_COUNTS.bind(this), this.schemaCountsLength()),
|
|
213
240
|
this.bb.createScalarList(this.WARNINGS.bind(this), this.warningsLength()),
|
|
214
|
-
this.FROM_CACHE()
|
|
241
|
+
this.FROM_CACHE(),
|
|
242
|
+
this.bb.createObjList(this.ATTRIBUTES.bind(this), this.attributesLength())
|
|
215
243
|
);
|
|
216
244
|
}
|
|
217
245
|
unpackTo(_o) {
|
|
@@ -230,10 +258,11 @@ class Provenance {
|
|
|
230
258
|
_o.SCHEMA_COUNTS = this.bb.createScalarList(this.SCHEMA_COUNTS.bind(this), this.schemaCountsLength());
|
|
231
259
|
_o.WARNINGS = this.bb.createScalarList(this.WARNINGS.bind(this), this.warningsLength());
|
|
232
260
|
_o.FROM_CACHE = this.FROM_CACHE();
|
|
261
|
+
_o.ATTRIBUTES = this.bb.createObjList(this.ATTRIBUTES.bind(this), this.attributesLength());
|
|
233
262
|
}
|
|
234
263
|
}
|
|
235
264
|
class ProvenanceT {
|
|
236
|
-
constructor(SOURCE_URL = null, HTTP_STATUS = 0, ETAG = null, LAST_MODIFIED = null, CONTENT_TYPE = null, RETRIEVED_MS = BigInt("0"), PARSER = null, PARSER_VERSION = null, SOURCE_SHA256 = null, NORMALIZED_SHA256 = null, NORMALIZED_COUNT = BigInt("0"), SCHEMAS = [], SCHEMA_COUNTS = [], WARNINGS = [], FROM_CACHE = false) {
|
|
265
|
+
constructor(SOURCE_URL = null, HTTP_STATUS = 0, ETAG = null, LAST_MODIFIED = null, CONTENT_TYPE = null, RETRIEVED_MS = BigInt("0"), PARSER = null, PARSER_VERSION = null, SOURCE_SHA256 = null, NORMALIZED_SHA256 = null, NORMALIZED_COUNT = BigInt("0"), SCHEMAS = [], SCHEMA_COUNTS = [], WARNINGS = [], FROM_CACHE = false, ATTRIBUTES = []) {
|
|
237
266
|
__publicField(this, "SOURCE_URL", SOURCE_URL);
|
|
238
267
|
__publicField(this, "HTTP_STATUS", HTTP_STATUS);
|
|
239
268
|
__publicField(this, "ETAG", ETAG);
|
|
@@ -249,6 +278,7 @@ class ProvenanceT {
|
|
|
249
278
|
__publicField(this, "SCHEMA_COUNTS", SCHEMA_COUNTS);
|
|
250
279
|
__publicField(this, "WARNINGS", WARNINGS);
|
|
251
280
|
__publicField(this, "FROM_CACHE", FROM_CACHE);
|
|
281
|
+
__publicField(this, "ATTRIBUTES", ATTRIBUTES);
|
|
252
282
|
}
|
|
253
283
|
pack(builder) {
|
|
254
284
|
const SOURCE_URL = this.SOURCE_URL !== null ? builder.createString(this.SOURCE_URL) : 0;
|
|
@@ -262,6 +292,7 @@ class ProvenanceT {
|
|
|
262
292
|
const SCHEMAS = Provenance.createSchemasVector(builder, builder.createObjectOffsetList(this.SCHEMAS));
|
|
263
293
|
const SCHEMA_COUNTS = Provenance.createSchemaCountsVector(builder, this.SCHEMA_COUNTS);
|
|
264
294
|
const WARNINGS = Provenance.createWarningsVector(builder, builder.createObjectOffsetList(this.WARNINGS));
|
|
295
|
+
const ATTRIBUTES = Provenance.createAttributesVector(builder, builder.createObjectOffsetList(this.ATTRIBUTES));
|
|
265
296
|
return Provenance.createProvenance(
|
|
266
297
|
builder,
|
|
267
298
|
SOURCE_URL,
|
|
@@ -278,7 +309,8 @@ class ProvenanceT {
|
|
|
278
309
|
SCHEMAS,
|
|
279
310
|
SCHEMA_COUNTS,
|
|
280
311
|
WARNINGS,
|
|
281
|
-
this.FROM_CACHE
|
|
312
|
+
this.FROM_CACHE,
|
|
313
|
+
ATTRIBUTES
|
|
282
314
|
);
|
|
283
315
|
}
|
|
284
316
|
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import * as flatbuffers from 'flatbuffers';
|
|
6
6
|
|
|
7
|
+
import { Header, HeaderT } from './Header.js';
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
/**
|
|
@@ -149,8 +150,22 @@ FROM_CACHE():boolean {
|
|
|
149
150
|
return offset ? !!this.bb!.readInt8(this.bb_pos + offset) : false;
|
|
150
151
|
}
|
|
151
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Descriptive provenance the parser has no field above for - the method, units, the date range covered, counts
|
|
155
|
+
* of what was dropped or filled - as name / value pairs, kept in order (a name may repeat). Values are text.
|
|
156
|
+
*/
|
|
157
|
+
ATTRIBUTES(index: number, obj?:Header):Header|null {
|
|
158
|
+
const offset = this.bb!.__offset(this.bb_pos, 34);
|
|
159
|
+
return offset ? (obj || new Header()).__init(this.bb!.__indirect(this.bb!.__vector(this.bb_pos + offset) + index * 4), this.bb!) : null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
attributesLength():number {
|
|
163
|
+
const offset = this.bb!.__offset(this.bb_pos, 34);
|
|
164
|
+
return offset ? this.bb!.__vector_len(this.bb_pos + offset) : 0;
|
|
165
|
+
}
|
|
166
|
+
|
|
152
167
|
static startProvenance(builder:flatbuffers.Builder) {
|
|
153
|
-
builder.startObject(
|
|
168
|
+
builder.startObject(16);
|
|
154
169
|
}
|
|
155
170
|
|
|
156
171
|
static addSourceUrl(builder:flatbuffers.Builder, SOURCE_URLOffset:flatbuffers.Offset) {
|
|
@@ -249,12 +264,28 @@ static addFromCache(builder:flatbuffers.Builder, FROM_CACHE:boolean) {
|
|
|
249
264
|
builder.addFieldInt8(14, +FROM_CACHE, +false);
|
|
250
265
|
}
|
|
251
266
|
|
|
267
|
+
static addAttributes(builder:flatbuffers.Builder, ATTRIBUTESOffset:flatbuffers.Offset) {
|
|
268
|
+
builder.addFieldOffset(15, ATTRIBUTESOffset, 0);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
static createAttributesVector(builder:flatbuffers.Builder, data:flatbuffers.Offset[]):flatbuffers.Offset {
|
|
272
|
+
builder.startVector(4, data.length, 4);
|
|
273
|
+
for (let i = data.length - 1; i >= 0; i--) {
|
|
274
|
+
builder.addOffset(data[i]!);
|
|
275
|
+
}
|
|
276
|
+
return builder.endVector();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
static startAttributesVector(builder:flatbuffers.Builder, numElems:number) {
|
|
280
|
+
builder.startVector(4, numElems, 4);
|
|
281
|
+
}
|
|
282
|
+
|
|
252
283
|
static endProvenance(builder:flatbuffers.Builder):flatbuffers.Offset {
|
|
253
284
|
const offset = builder.endObject();
|
|
254
285
|
return offset;
|
|
255
286
|
}
|
|
256
287
|
|
|
257
|
-
static createProvenance(builder:flatbuffers.Builder, SOURCE_URLOffset:flatbuffers.Offset, HTTP_STATUS:number, ETAGOffset:flatbuffers.Offset, LAST_MODIFIEDOffset:flatbuffers.Offset, CONTENT_TYPEOffset:flatbuffers.Offset, RETRIEVED_MS:bigint, PARSEROffset:flatbuffers.Offset, PARSER_VERSIONOffset:flatbuffers.Offset, SOURCE_SHA256Offset:flatbuffers.Offset, NORMALIZED_SHA256Offset:flatbuffers.Offset, NORMALIZED_COUNT:bigint, SCHEMASOffset:flatbuffers.Offset, SCHEMA_COUNTSOffset:flatbuffers.Offset, WARNINGSOffset:flatbuffers.Offset, FROM_CACHE:boolean):flatbuffers.Offset {
|
|
288
|
+
static createProvenance(builder:flatbuffers.Builder, SOURCE_URLOffset:flatbuffers.Offset, HTTP_STATUS:number, ETAGOffset:flatbuffers.Offset, LAST_MODIFIEDOffset:flatbuffers.Offset, CONTENT_TYPEOffset:flatbuffers.Offset, RETRIEVED_MS:bigint, PARSEROffset:flatbuffers.Offset, PARSER_VERSIONOffset:flatbuffers.Offset, SOURCE_SHA256Offset:flatbuffers.Offset, NORMALIZED_SHA256Offset:flatbuffers.Offset, NORMALIZED_COUNT:bigint, SCHEMASOffset:flatbuffers.Offset, SCHEMA_COUNTSOffset:flatbuffers.Offset, WARNINGSOffset:flatbuffers.Offset, FROM_CACHE:boolean, ATTRIBUTESOffset:flatbuffers.Offset):flatbuffers.Offset {
|
|
258
289
|
Provenance.startProvenance(builder);
|
|
259
290
|
Provenance.addSourceUrl(builder, SOURCE_URLOffset);
|
|
260
291
|
Provenance.addHttpStatus(builder, HTTP_STATUS);
|
|
@@ -271,6 +302,7 @@ static createProvenance(builder:flatbuffers.Builder, SOURCE_URLOffset:flatbuffer
|
|
|
271
302
|
Provenance.addSchemaCounts(builder, SCHEMA_COUNTSOffset);
|
|
272
303
|
Provenance.addWarnings(builder, WARNINGSOffset);
|
|
273
304
|
Provenance.addFromCache(builder, FROM_CACHE);
|
|
305
|
+
Provenance.addAttributes(builder, ATTRIBUTESOffset);
|
|
274
306
|
return Provenance.endProvenance(builder);
|
|
275
307
|
}
|
|
276
308
|
|
|
@@ -290,7 +322,8 @@ unpack(): ProvenanceT {
|
|
|
290
322
|
this.bb!.createScalarList<string>(this.SCHEMAS.bind(this), this.schemasLength()),
|
|
291
323
|
this.bb!.createScalarList<bigint>(this.SCHEMA_COUNTS.bind(this), this.schemaCountsLength()),
|
|
292
324
|
this.bb!.createScalarList<string>(this.WARNINGS.bind(this), this.warningsLength()),
|
|
293
|
-
this.FROM_CACHE()
|
|
325
|
+
this.FROM_CACHE(),
|
|
326
|
+
this.bb!.createObjList<Header, HeaderT>(this.ATTRIBUTES.bind(this), this.attributesLength())
|
|
294
327
|
);
|
|
295
328
|
}
|
|
296
329
|
|
|
@@ -311,6 +344,7 @@ unpackTo(_o: ProvenanceT): void {
|
|
|
311
344
|
_o.SCHEMA_COUNTS = this.bb!.createScalarList<bigint>(this.SCHEMA_COUNTS.bind(this), this.schemaCountsLength());
|
|
312
345
|
_o.WARNINGS = this.bb!.createScalarList<string>(this.WARNINGS.bind(this), this.warningsLength());
|
|
313
346
|
_o.FROM_CACHE = this.FROM_CACHE();
|
|
347
|
+
_o.ATTRIBUTES = this.bb!.createObjList<Header, HeaderT>(this.ATTRIBUTES.bind(this), this.attributesLength());
|
|
314
348
|
}
|
|
315
349
|
}
|
|
316
350
|
|
|
@@ -330,7 +364,8 @@ constructor(
|
|
|
330
364
|
public SCHEMAS: (string)[] = [],
|
|
331
365
|
public SCHEMA_COUNTS: (bigint)[] = [],
|
|
332
366
|
public WARNINGS: (string)[] = [],
|
|
333
|
-
public FROM_CACHE: boolean = false
|
|
367
|
+
public FROM_CACHE: boolean = false,
|
|
368
|
+
public ATTRIBUTES: (HeaderT)[] = []
|
|
334
369
|
){}
|
|
335
370
|
|
|
336
371
|
|
|
@@ -346,6 +381,7 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
|
|
|
346
381
|
const SCHEMAS = Provenance.createSchemasVector(builder, builder.createObjectOffsetList(this.SCHEMAS));
|
|
347
382
|
const SCHEMA_COUNTS = Provenance.createSchemaCountsVector(builder, this.SCHEMA_COUNTS);
|
|
348
383
|
const WARNINGS = Provenance.createWarningsVector(builder, builder.createObjectOffsetList(this.WARNINGS));
|
|
384
|
+
const ATTRIBUTES = Provenance.createAttributesVector(builder, builder.createObjectOffsetList(this.ATTRIBUTES));
|
|
349
385
|
|
|
350
386
|
return Provenance.createProvenance(builder,
|
|
351
387
|
SOURCE_URL,
|
|
@@ -362,7 +398,8 @@ pack(builder:flatbuffers.Builder): flatbuffers.Offset {
|
|
|
362
398
|
SCHEMAS,
|
|
363
399
|
SCHEMA_COUNTS,
|
|
364
400
|
WARNINGS,
|
|
365
|
-
this.FROM_CACHE
|
|
401
|
+
this.FROM_CACHE,
|
|
402
|
+
ATTRIBUTES
|
|
366
403
|
);
|
|
367
404
|
}
|
|
368
405
|
}
|
package/src/host/abi.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
} from "./hostcallWire.js";
|
|
7
7
|
import { isTypedHostCall } from "../standards/runtimeContracts.js";
|
|
8
8
|
import { answerTypedHostCall } from "./typedDispatch.js";
|
|
9
|
+
import { ADAPTER_OPERATIONS } from "./adapterOperations.js";
|
|
9
10
|
|
|
10
11
|
const textDecoder = new TextDecoder();
|
|
11
12
|
|
|
@@ -19,6 +20,7 @@ export const SyncHostcallOperations = Object.freeze([
|
|
|
19
20
|
"host.listSupportedCapabilities",
|
|
20
21
|
"host.listOperations",
|
|
21
22
|
"host.hasCapability",
|
|
23
|
+
"plugin.getConfig",
|
|
22
24
|
"clock.now",
|
|
23
25
|
"clock.monotonicNow",
|
|
24
26
|
"clock.nowIso",
|
|
@@ -209,6 +211,9 @@ export function dispatchHostSyncOperation(host, operation, params = null) {
|
|
|
209
211
|
return host.listOperations();
|
|
210
212
|
case "host.hasCapability":
|
|
211
213
|
return host.hasCapability(params?.capability);
|
|
214
|
+
case "plugin.getConfig":
|
|
215
|
+
if (typeof host.plugin?.getConfig !== "function") break;
|
|
216
|
+
return host.plugin.getConfig();
|
|
212
217
|
case "clock.now":
|
|
213
218
|
return host.clock.now();
|
|
214
219
|
case "clock.monotonicNow":
|
|
@@ -304,10 +309,14 @@ export function dispatchHostSyncOperation(host, operation, params = null) {
|
|
|
304
309
|
normalized,
|
|
305
310
|
);
|
|
306
311
|
default:
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
312
|
+
// the embedder adapters' operations (secrets, storage, p2p, node): synchronous when the adapter answers so
|
|
313
|
+
if (ADAPTER_OPERATIONS[normalized] && typeof host.serveAdapterOperation === "function") {
|
|
314
|
+
return host.serveAdapterOperation(normalized, params ?? {});
|
|
315
|
+
}
|
|
310
316
|
}
|
|
317
|
+
throw new Error(
|
|
318
|
+
`Operation "${normalized}" is not available in the synchronous hostcall ABI.`,
|
|
319
|
+
);
|
|
311
320
|
}
|
|
312
321
|
|
|
313
322
|
export function dispatchNodeHostSyncOperation(host, operation, params = null) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The Go node's capability operations that the SDK JS hosts (NodeHost, BrowserHost) serve through an embedder's
|
|
2
|
+
// capability adapters: secrets, storage, p2p and node status. Each adapter method takes and returns the same
|
|
3
|
+
// handler-shaped object the Go node's capability handler does (sdn-server internal/modulert/caps), except that bytes
|
|
4
|
+
// are Uint8Array, never base64. typedDispatch.js maps them to and from the typed hostcall's records and frames.
|
|
5
|
+
//
|
|
6
|
+
// capabilityAdapters: {
|
|
7
|
+
// secrets: { get, status, put, clear }, // {id, username?, secret?}
|
|
8
|
+
// storage: { write, query, delete, expire, ingest_with_source, // storage.<method>
|
|
9
|
+
// flatsql_query_stream, flatsql_epoch_stream, query_sandboxed, query_surface, flatsql_cache_key },
|
|
10
|
+
// p2p: { peers_snapshot, standards_snapshot, pnm_history, latest_dataset },
|
|
11
|
+
// node: { peerId, publicKey, status, activity }, // peerId / publicKey may be strings
|
|
12
|
+
// }
|
|
13
|
+
//
|
|
14
|
+
// An adapter may instead implement invoke(method, params). Methods may answer synchronously (then the operation also
|
|
15
|
+
// works on the synchronous hostcall bridge) or with a Promise.
|
|
16
|
+
|
|
17
|
+
/** operation -> { capability (null: none), adapter, method } */
|
|
18
|
+
export const ADAPTER_OPERATIONS = Object.freeze({
|
|
19
|
+
"node.peerId": { capability: null, adapter: "node", method: "peerId" },
|
|
20
|
+
"node.publicKey": { capability: null, adapter: "node", method: "publicKey" },
|
|
21
|
+
"secrets.get": { capability: "secrets", adapter: "secrets", method: "get" },
|
|
22
|
+
"secrets.status": { capability: "secrets", adapter: "secrets", method: "status" },
|
|
23
|
+
"secrets.put": { capability: "secrets", adapter: "secrets", method: "put" },
|
|
24
|
+
"secrets.clear": { capability: "secrets", adapter: "secrets", method: "clear" },
|
|
25
|
+
"storage.write": { capability: "storage_write", adapter: "storage", method: "write" },
|
|
26
|
+
"storage.query": { capability: "storage_query", adapter: "storage", method: "query" },
|
|
27
|
+
"storage.delete": { capability: "storage_write", adapter: "storage", method: "delete" },
|
|
28
|
+
"storage.expire": { capability: "storage_write", adapter: "storage", method: "expire" },
|
|
29
|
+
"storage.ingest_with_source": { capability: "storage_ingest", adapter: "storage", method: "ingest_with_source" },
|
|
30
|
+
"storage.flatsql_query_stream": { capability: "storage_query", adapter: "storage", method: "flatsql_query_stream" },
|
|
31
|
+
"storage.flatsql_epoch_stream": { capability: "storage_query", adapter: "storage", method: "flatsql_epoch_stream" },
|
|
32
|
+
"storage.query_sandboxed": { capability: "storage_query", adapter: "storage", method: "query_sandboxed" },
|
|
33
|
+
"storage.query_surface": { capability: "storage_query", adapter: "storage", method: "query_surface" },
|
|
34
|
+
"storage.flatsql_cache_key": { capability: "storage_query", adapter: "storage", method: "flatsql_cache_key" },
|
|
35
|
+
"p2p.peers_snapshot": { capability: "p2p_read", adapter: "p2p", method: "peers_snapshot" },
|
|
36
|
+
"p2p.standards_snapshot": { capability: "p2p_read", adapter: "p2p", method: "standards_snapshot" },
|
|
37
|
+
"p2p.pnm_history": { capability: "p2p_read", adapter: "p2p", method: "pnm_history" },
|
|
38
|
+
"p2p.latest_dataset": { capability: "p2p_read", adapter: "p2p", method: "latest_dataset" },
|
|
39
|
+
"node_status_read.status": { capability: "node_status_read", adapter: "node", method: "status" },
|
|
40
|
+
"node_activity_read.activity": { capability: "node_activity_read", adapter: "node", method: "activity" },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** The capabilities those operations need. */
|
|
44
|
+
export const ADAPTER_CAPABILITIES = Object.freeze(
|
|
45
|
+
[...new Set(Object.values(ADAPTER_OPERATIONS).map((o) => o.capability).filter(Boolean))],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
/** The adapters a host's options name: options.<name> or options.capabilityAdapters.<name> (and its aliases). */
|
|
49
|
+
export function resolveServiceAdapters(options = {}) {
|
|
50
|
+
const adapters = options.capabilityAdapters && typeof options.capabilityAdapters === "object" ? options.capabilityAdapters : {};
|
|
51
|
+
return {
|
|
52
|
+
secrets: options.secrets ?? adapters.secrets ?? null,
|
|
53
|
+
storage: options.storage ?? adapters.storage ?? adapters.storage_write ?? adapters.storage_query ?? adapters.storage_ingest ?? null,
|
|
54
|
+
p2p: options.p2p ?? adapters.p2p ?? adapters.p2p_read ?? null,
|
|
55
|
+
node: options.node ?? adapters.node ?? adapters.node_status_read ?? adapters.node_activity_read ?? null,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The host's plugin config (options.config): a plain object, {} when not given. */
|
|
60
|
+
export function normalizePluginConfig(config) {
|
|
61
|
+
if (config === undefined || config === null) return {};
|
|
62
|
+
if (typeof config !== "object" || Array.isArray(config)) throw new TypeError("Host config must be an object.");
|
|
63
|
+
return structuredClone(config);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const unsupported = (message) => Object.assign(new Error(message), { hostcallCode: "unsupported" });
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Call `method` on an adapter: its function (sync or async), a plain value it carries under that name
|
|
70
|
+
* (node.peerId: "12D3Koo..."), or adapter.invoke(method, params).
|
|
71
|
+
*/
|
|
72
|
+
export function callServiceAdapter(adapter, label, method, params) {
|
|
73
|
+
if (!adapter || typeof adapter !== "object") throw unsupported(`${label} adapter is not configured for this host.`);
|
|
74
|
+
const member = adapter[method];
|
|
75
|
+
if (typeof member === "function") return member.call(adapter, params);
|
|
76
|
+
if (member !== undefined) return member;
|
|
77
|
+
if (typeof adapter.invoke === "function") return adapter.invoke(method, params);
|
|
78
|
+
throw unsupported(`${label} adapter does not implement "${method}" or invoke().`);
|
|
79
|
+
}
|
package/src/host/browserHost.js
CHANGED
|
@@ -19,6 +19,13 @@ import {
|
|
|
19
19
|
nextCronOccurrence,
|
|
20
20
|
} from "./cron.js";
|
|
21
21
|
import { createBrowserEdgeShims } from "./browserEdgeShims.js";
|
|
22
|
+
import {
|
|
23
|
+
ADAPTER_CAPABILITIES,
|
|
24
|
+
ADAPTER_OPERATIONS,
|
|
25
|
+
callServiceAdapter,
|
|
26
|
+
normalizePluginConfig,
|
|
27
|
+
resolveServiceAdapters,
|
|
28
|
+
} from "./adapterOperations.js";
|
|
22
29
|
|
|
23
30
|
export const BrowserHostSupportedCapabilities = Object.freeze([
|
|
24
31
|
"clock",
|
|
@@ -46,6 +53,8 @@ export const BrowserHostSupportedCapabilities = Object.freeze([
|
|
|
46
53
|
"storage_query",
|
|
47
54
|
"storage_write",
|
|
48
55
|
"logging",
|
|
56
|
+
// served through the embedder's capability adapters (adapterOperations.js)
|
|
57
|
+
...ADAPTER_CAPABILITIES.filter((c) => c !== "storage_query" && c !== "storage_write"),
|
|
49
58
|
]);
|
|
50
59
|
|
|
51
60
|
export const BrowserHostSupportedOperations = Object.freeze([
|
|
@@ -54,6 +63,7 @@ export const BrowserHostSupportedOperations = Object.freeze([
|
|
|
54
63
|
"host.listSupportedCapabilities",
|
|
55
64
|
"host.listOperations",
|
|
56
65
|
"host.hasCapability",
|
|
66
|
+
"plugin.getConfig",
|
|
57
67
|
"clock.now",
|
|
58
68
|
"clock.monotonicNow",
|
|
59
69
|
"clock.nowIso",
|
|
@@ -109,6 +119,9 @@ export const BrowserHostSupportedOperations = Object.freeze([
|
|
|
109
119
|
"crypto.ed25519.publicKeyFromSeed",
|
|
110
120
|
"crypto.ed25519.sign",
|
|
111
121
|
"crypto.ed25519.verify",
|
|
122
|
+
...Object.keys(ADAPTER_OPERATIONS).filter(
|
|
123
|
+
(op) => !["storage.write", "storage.query", "storage.delete"].includes(op),
|
|
124
|
+
),
|
|
112
125
|
]);
|
|
113
126
|
|
|
114
127
|
export class BrowserHostCapabilityError extends Error {
|
|
@@ -434,6 +447,18 @@ export class BrowserHost {
|
|
|
434
447
|
|
|
435
448
|
this._grantedCapabilities = granted;
|
|
436
449
|
this._contextStore = options.contextStore ?? new Map();
|
|
450
|
+
this._serviceAdapters = resolveServiceAdapters(options);
|
|
451
|
+
this._config = normalizePluginConfig(options.config);
|
|
452
|
+
|
|
453
|
+
// The module's config (options.config), as the Go node's plugin.getConfig serves it: no capability needed.
|
|
454
|
+
this.plugin = Object.freeze({
|
|
455
|
+
getConfig: () => structuredClone(this._config),
|
|
456
|
+
});
|
|
457
|
+
// The node's identity, from the embedder's node adapter (values or functions): no capability needed.
|
|
458
|
+
this.node = Object.freeze({
|
|
459
|
+
peerId: () => this.serveAdapterOperation("node.peerId"),
|
|
460
|
+
publicKey: () => this.serveAdapterOperation("node.publicKey"),
|
|
461
|
+
});
|
|
437
462
|
this.filesystemRoot = filesystem?.filesystemRoot ?? "/";
|
|
438
463
|
|
|
439
464
|
// --- Capability objects (frozen, browser-native) ---
|
|
@@ -678,10 +703,20 @@ export class BrowserHost {
|
|
|
678
703
|
});
|
|
679
704
|
|
|
680
705
|
// Storage + pubsub mirror the Go node's module capability contract
|
|
681
|
-
// (sdn-server internal/modulert/caps)
|
|
682
|
-
//
|
|
706
|
+
// (sdn-server internal/modulert/caps), with bytes as Uint8Array, never
|
|
707
|
+
// base64: storage.write {schema, data: Uint8Array} -> {cid};
|
|
708
|
+
// storage.query {schema, day, entity_id, norad_cat_id, limit} -> [{CID,
|
|
709
|
+
// RowID, PeerID, Timestamp, MaterializedAt, SourceTags: {ProviderID,
|
|
710
|
+
// SourceName, BatchID, ProducerPeerID, License}, Data: Uint8Array}];
|
|
683
711
|
// storage.delete {schema, cid}; pubsub.publish {topic, data:utf8};
|
|
684
712
|
// pubsub.subscribe/unsubscribe {topic}; pubsub.list_topics {} -> {topics}.
|
|
713
|
+
// The storage adapter's other methods (storage.expire, ingest_with_source,
|
|
714
|
+
// flatsql_query_stream, flatsql_epoch_stream, query_sandboxed,
|
|
715
|
+
// query_surface, flatsql_cache_key) are served by serveAdapterOperation()
|
|
716
|
+
// with the same handler shapes: record streams, archives, provenance JSON
|
|
717
|
+
// and answered streams ({rows, columns, stream: Uint8Array} or
|
|
718
|
+
// {ref: {token, size, frames, fnv1a64}}) are Uint8Array too. See
|
|
719
|
+
// adapterOperations.js and the typed mapping in typedDispatch.js.
|
|
685
720
|
this.storage = Object.freeze({
|
|
686
721
|
write: async (params = {}) => {
|
|
687
722
|
this.#assertCapability("storage_write", "storage.write");
|
|
@@ -1022,9 +1057,32 @@ export class BrowserHost {
|
|
|
1022
1057
|
this.#assertCapability(capability, operation);
|
|
1023
1058
|
}
|
|
1024
1059
|
|
|
1060
|
+
/**
|
|
1061
|
+
* Serve one of the operations the embedder's capability adapters answer (ADAPTER_OPERATIONS: secrets, storage,
|
|
1062
|
+
* p2p, node): the capability check, then the adapter's method with the handler-shaped params. Synchronous when
|
|
1063
|
+
* the adapter answers synchronously.
|
|
1064
|
+
*/
|
|
1065
|
+
serveAdapterOperation(operation, params = {}) {
|
|
1066
|
+
const served = ADAPTER_OPERATIONS[operation];
|
|
1067
|
+
if (!served) {
|
|
1068
|
+
throw new Error(`Unknown browser host operation "${operation}".`);
|
|
1069
|
+
}
|
|
1070
|
+
if (served.capability) {
|
|
1071
|
+
this.#assertCapability(served.capability, operation);
|
|
1072
|
+
}
|
|
1073
|
+
return callServiceAdapter(
|
|
1074
|
+
this._serviceAdapters[served.adapter],
|
|
1075
|
+
served.adapter,
|
|
1076
|
+
served.method,
|
|
1077
|
+
params ?? {},
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
|
|
1025
1081
|
async invoke(operation, params = {}) {
|
|
1026
1082
|
const normalized = String(operation ?? "").trim();
|
|
1027
1083
|
switch (normalized) {
|
|
1084
|
+
case "plugin.getConfig":
|
|
1085
|
+
return this.plugin.getConfig();
|
|
1028
1086
|
case "host.runtimeTarget":
|
|
1029
1087
|
return this.runtimeTarget;
|
|
1030
1088
|
case "host.listCapabilities":
|
|
@@ -1165,6 +1223,9 @@ export class BrowserHost {
|
|
|
1165
1223
|
params.publicKey,
|
|
1166
1224
|
);
|
|
1167
1225
|
default:
|
|
1226
|
+
if (ADAPTER_OPERATIONS[normalized]) {
|
|
1227
|
+
return this.serveAdapterOperation(normalized, params);
|
|
1228
|
+
}
|
|
1168
1229
|
throw new Error(`Unknown browser host operation "${normalized}".`);
|
|
1169
1230
|
}
|
|
1170
1231
|
}
|
|
@@ -196,6 +196,57 @@ inline Response call(const char* op, const char* params_type, const Params& para
|
|
|
196
196
|
/// Call an operation without a "params" record (clock.now, plugin.getConfig, filesystem.readFile with raw "path", ...).
|
|
197
197
|
inline Response call(const char* op, std::initializer_list<Frame> frames = {}) { return call_frames(op, std::vector<Frame>(frames)); }
|
|
198
198
|
|
|
199
|
+
/// SQL values (raw "params" of storage.flatsql_query_stream / query_sandboxed / flatsql_cache_key, raw "cells" of a
|
|
200
|
+
/// query_sandboxed rows answer) in FlatSQL's tagged parameter encoding: value after value, a u8 tag, a u32
|
|
201
|
+
/// little-endian payload length, the payload. Text and bytes of any length.
|
|
202
|
+
/// sdm_piv::SqlValues p; p.i64(25544).str("ISS (ZARYA)").bytes(key, 32);
|
|
203
|
+
/// sdm_piv::call("storage.flatsql_query_stream", "SqlStreamParams", s, {sdm_piv::raw("sql", sql), p.frame()});
|
|
204
|
+
enum SqlTag : uint8_t { kSqlNull = 0, kSqlBool = 1, kSqlI64 = 2, kSqlF64 = 3, kSqlStr = 4, kSqlBytes = 5 };
|
|
205
|
+
class SqlValues {
|
|
206
|
+
public:
|
|
207
|
+
SqlValues& null() { return put(kSqlNull, nullptr, 0); }
|
|
208
|
+
SqlValues& boolean(bool v) { const uint8_t b = v ? 1 : 0; return put(kSqlBool, &b, 1); }
|
|
209
|
+
SqlValues& i64(int64_t v) { uint8_t b[8]; for (int i = 0; i < 8; ++i) b[i] = static_cast<uint8_t>(static_cast<uint64_t>(v) >> (8 * i)); return put(kSqlI64, b, 8); }
|
|
210
|
+
SqlValues& f64(double v) { uint64_t u; std::memcpy(&u, &v, 8); return i64_bits(kSqlF64, u); }
|
|
211
|
+
SqlValues& str(std::string_view v) { return put(kSqlStr, reinterpret_cast<const uint8_t*>(v.data()), v.size()); }
|
|
212
|
+
SqlValues& bytes(const uint8_t* data, size_t size) { return put(kSqlBytes, data, size); }
|
|
213
|
+
std::string_view view() const { return bytes_; }
|
|
214
|
+
/// The raw frame (default port "params"); the values must outlive the call.
|
|
215
|
+
Frame frame(const char* port = "params") const { return raw(port, bytes_); }
|
|
216
|
+
|
|
217
|
+
private:
|
|
218
|
+
SqlValues& i64_bits(uint8_t tag, uint64_t u) { uint8_t b[8]; for (int i = 0; i < 8; ++i) b[i] = static_cast<uint8_t>(u >> (8 * i)); return put(tag, b, 8); }
|
|
219
|
+
SqlValues& put(uint8_t tag, const uint8_t* data, size_t size) {
|
|
220
|
+
bytes_.push_back(static_cast<char>(tag));
|
|
221
|
+
for (int i = 0; i < 4; ++i) bytes_.push_back(static_cast<char>((static_cast<uint32_t>(size) >> (8 * i)) & 0xff));
|
|
222
|
+
if (size) bytes_.append(reinterpret_cast<const char*>(data), size);
|
|
223
|
+
return *this;
|
|
224
|
+
}
|
|
225
|
+
std::string bytes_;
|
|
226
|
+
};
|
|
227
|
+
/// One decoded SQL value: its tag and payload (in place).
|
|
228
|
+
struct SqlValue {
|
|
229
|
+
uint8_t tag;
|
|
230
|
+
std::string_view payload;
|
|
231
|
+
bool as_bool() const { return !payload.empty() && payload[0] != 0; }
|
|
232
|
+
int64_t as_i64() const { uint64_t u = 0; for (size_t i = 0; i < 8 && i < payload.size(); ++i) u |= static_cast<uint64_t>(static_cast<uint8_t>(payload[i])) << (8 * i); return static_cast<int64_t>(u); }
|
|
233
|
+
double as_f64() const { const uint64_t u = static_cast<uint64_t>(as_i64()); double d; std::memcpy(&d, &u, 8); return d; }
|
|
234
|
+
};
|
|
235
|
+
/// The values of a tagged SQL value run; false when it is malformed.
|
|
236
|
+
inline bool read_sql_values(std::string_view bytes, std::vector<SqlValue>& out) {
|
|
237
|
+
out.clear();
|
|
238
|
+
size_t at = 0;
|
|
239
|
+
while (at < bytes.size()) {
|
|
240
|
+
if (bytes.size() - at < 5) return false;
|
|
241
|
+
uint32_t size = 0;
|
|
242
|
+
for (int i = 0; i < 4; ++i) size |= static_cast<uint32_t>(static_cast<uint8_t>(bytes[at + 1 + i])) << (8 * i);
|
|
243
|
+
if (bytes.size() - at - 5 < size) return false;
|
|
244
|
+
out.push_back({static_cast<uint8_t>(bytes[at]), bytes.substr(at + 5, size)});
|
|
245
|
+
at += 5 + size;
|
|
246
|
+
}
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
|
|
199
250
|
} // namespace sdm_piv
|
|
200
251
|
|
|
201
252
|
#endif // SDM_PIV_HOSTCALL_HPP
|