space-data-module-sdk 0.10.4 → 0.12.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.
@@ -1,7 +1,33 @@
1
1
  import { createFlatSqlRuntimeStore } from "./flatsqlRuntimeStore.js";
2
2
  import { createFlatBufferStreamIngestor } from "./flatbufferStreamIngestor.js";
3
- import { createRuntimeRegionStore } from "./runtimeRegionStore.js";
3
+ import {
4
+ createArenaBuffer,
5
+ createRuntimeRegionStore,
6
+ isSharedBuffer,
7
+ } from "./runtimeRegionStore.js";
4
8
  import { createModuleRegistry } from "./moduleRegistry.js";
9
+ import {
10
+ SCR_GRID_STATE_FLOATS,
11
+ SCR_GRID_STATE_BYTES,
12
+ SCR_RADIUS_BAND_FLOATS,
13
+ SCR_RADIUS_BAND_BYTES,
14
+ SCR_ROLE,
15
+ SCR_ROLE_MASK_ALL,
16
+ SCR_GRID_LAYOUT_ID,
17
+ DEFAULT_PROPAGATOR_ID,
18
+ SUPPORTED_PROPAGATOR_IDS,
19
+ DECLARED_ORBIT_THEORIES,
20
+ TheoryMismatchError,
21
+ StaleGridLeaseError,
22
+ resolvePropagatorId,
23
+ normalizeDeclaredTheory,
24
+ createPropagatorRegistry,
25
+ routeCatalogRecordsByTheory,
26
+ gridCacheKey,
27
+ encodeScrGridFlatBuffer,
28
+ decodeScrGridFlatBuffer,
29
+ createSharedEphemerisGridManager,
30
+ } from "./ephemerisGridStore.js";
5
31
 
6
32
  function normalizeCapabilityId(capability) {
7
33
  const normalized = String(capability ?? "").trim();
@@ -49,12 +75,46 @@ export function createRuntimeHost(options = {}) {
49
75
  const rows = options.rows ?? createFlatSqlRuntimeStore(options.rowStore);
50
76
  const regions = options.regions ?? createRuntimeRegionStore();
51
77
  const moduleRegistry = options.moduleRegistry ?? createModuleRegistry();
78
+ const propagators =
79
+ options.propagators ?? createPropagatorRegistry(options.propagatorAdapters);
80
+ const ephemerisGrids =
81
+ options.ephemerisGrids ??
82
+ createSharedEphemerisGridManager({ regions, propagators });
52
83
  const capabilities = createCapabilityRegistry(options.capabilities);
53
84
 
54
85
  return {
55
86
  rows,
56
87
  regions,
57
88
  moduleRegistry,
89
+ propagators,
90
+ ephemerisGrids,
91
+ registerPropagator(id, adapter) {
92
+ return propagators.registerPropagator(id, adapter);
93
+ },
94
+ listPropagators() {
95
+ return propagators.listPropagators();
96
+ },
97
+ populateEphemerisGrid(spec) {
98
+ return ephemerisGrids.populateGrid(spec);
99
+ },
100
+ getEphemerisGrid(regionId) {
101
+ return ephemerisGrids.getGrid(regionId);
102
+ },
103
+ attachEphemerisGrid(regionId, opts) {
104
+ return ephemerisGrids.attachGridModule(regionId, opts);
105
+ },
106
+ detachEphemerisGrid(lease) {
107
+ if (!lease || typeof lease.detach !== "function") {
108
+ return false;
109
+ }
110
+ return lease.detach();
111
+ },
112
+ spliceEphemerisGrid(spec) {
113
+ return ephemerisGrids.spliceGridObjects(spec);
114
+ },
115
+ invalidateEphemerisGrid(filter) {
116
+ return ephemerisGrids.invalidateGrid(filter);
117
+ },
58
118
  listCapabilities() {
59
119
  return Array.from(capabilities.keys()).sort();
60
120
  },
@@ -106,10 +166,32 @@ export function createRuntimeHost(options = {}) {
106
166
  }
107
167
 
108
168
  export {
169
+ createArenaBuffer,
109
170
  createFlatBufferStreamIngestor,
110
171
  createFlatSqlRuntimeStore,
111
172
  createModuleRegistry,
112
173
  createRuntimeRegionStore,
174
+ isSharedBuffer,
175
+ SCR_GRID_STATE_FLOATS,
176
+ SCR_GRID_STATE_BYTES,
177
+ SCR_RADIUS_BAND_FLOATS,
178
+ SCR_RADIUS_BAND_BYTES,
179
+ SCR_ROLE,
180
+ SCR_ROLE_MASK_ALL,
181
+ SCR_GRID_LAYOUT_ID,
182
+ DEFAULT_PROPAGATOR_ID,
183
+ SUPPORTED_PROPAGATOR_IDS,
184
+ DECLARED_ORBIT_THEORIES,
185
+ TheoryMismatchError,
186
+ StaleGridLeaseError,
187
+ resolvePropagatorId,
188
+ normalizeDeclaredTheory,
189
+ createPropagatorRegistry,
190
+ routeCatalogRecordsByTheory,
191
+ gridCacheKey,
192
+ encodeScrGridFlatBuffer,
193
+ decodeScrGridFlatBuffer,
194
+ createSharedEphemerisGridManager,
113
195
  };
114
196
  // Flow-tier validating method registry (vendored sdn-flow semantics; distinct
115
197
  // from createModuleRegistry's module-lifecycle registry).
@@ -48,7 +48,7 @@ function toRecordBytes(value, recordByteLength) {
48
48
  bytes.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
49
49
  return bytes;
50
50
  }
51
- if (value instanceof ArrayBuffer) {
51
+ if (value instanceof ArrayBuffer || isSharedBuffer(value)) {
52
52
  if (value.byteLength > recordByteLength) {
53
53
  throw new RangeError("record bytes exceed recordByteLength");
54
54
  }
@@ -58,6 +58,34 @@ function toRecordBytes(value, recordByteLength) {
58
58
  throw new TypeError("runtime region records must be byte-oriented");
59
59
  }
60
60
 
61
+ function toExactUint8Array(value, label) {
62
+ if (value instanceof Uint8Array) {
63
+ return value;
64
+ }
65
+ if (ArrayBuffer.isView(value)) {
66
+ return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
67
+ }
68
+ if (value instanceof ArrayBuffer || isSharedBuffer(value)) {
69
+ return new Uint8Array(value);
70
+ }
71
+ throw new TypeError(`${label} must be byte-oriented`);
72
+ }
73
+
74
+ export function isSharedBuffer(buffer) {
75
+ return (
76
+ typeof SharedArrayBuffer === "function" &&
77
+ buffer instanceof SharedArrayBuffer
78
+ );
79
+ }
80
+
81
+ export function createArenaBuffer(byteLength, { shared = true } = {}) {
82
+ const normalizedLength = normalizePositiveInteger(byteLength, "byteLength");
83
+ if (shared && typeof SharedArrayBuffer === "function") {
84
+ return new SharedArrayBuffer(normalizedLength);
85
+ }
86
+ return new ArrayBuffer(normalizedLength);
87
+ }
88
+
61
89
  export function createRuntimeRegionStore() {
62
90
  let nextRegionId = 1;
63
91
  const regions = new Map();
@@ -69,6 +97,9 @@ export function createRuntimeRegionStore() {
69
97
  "recordCount",
70
98
  );
71
99
  }
100
+ if (region.kind === "contiguous") {
101
+ return region.recordCount;
102
+ }
72
103
  return region.records.length;
73
104
  }
74
105
 
@@ -78,6 +109,22 @@ export function createRuntimeRegionStore() {
78
109
  if (!region) {
79
110
  return null;
80
111
  }
112
+ if (region.kind === "contiguous") {
113
+ return {
114
+ regionId: region.regionId,
115
+ layoutId: region.layoutId,
116
+ recordByteLength: region.recordByteLength,
117
+ alignment: region.alignment,
118
+ recordCount: region.recordCount,
119
+ kind: "contiguous",
120
+ shared: region.shared,
121
+ generation: region.generation,
122
+ byteLength: region.bytes.byteLength,
123
+ activeConsumers: region.consumers.size,
124
+ invalidated: region.invalidated,
125
+ metadata: region.metadata,
126
+ };
127
+ }
81
128
  return {
82
129
  regionId: region.regionId,
83
130
  layoutId: region.layoutId,
@@ -103,6 +150,10 @@ export function createRuntimeRegionStore() {
103
150
  ),
104
151
  alignment: normalizeStrictlyPositiveInteger(alignment, "alignment"),
105
152
  records: [],
153
+ generation: 1,
154
+ consumers: new Map(),
155
+ nextLeaseId: 1,
156
+ invalidated: false,
106
157
  };
107
158
  region.records = Array.from(initialRecords, (record) =>
108
159
  toRecordBytes(record, region.recordByteLength),
@@ -111,6 +162,61 @@ export function createRuntimeRegionStore() {
111
162
  return describeRegion(region.regionId);
112
163
  }
113
164
 
165
+ function allocateContiguousRegion({
166
+ layoutId,
167
+ recordByteLength,
168
+ alignment = 8,
169
+ recordCount = 0,
170
+ shared = true,
171
+ initialBytes = null,
172
+ metadata = null,
173
+ }) {
174
+ const normalizedLayoutId = normalizeRequiredString(layoutId, "layoutId");
175
+ const normalizedRecordByteLength = normalizeStrictlyPositiveInteger(
176
+ recordByteLength,
177
+ "recordByteLength",
178
+ );
179
+ const normalizedAlignment = normalizeStrictlyPositiveInteger(
180
+ alignment,
181
+ "alignment",
182
+ );
183
+ const normalizedRecordCount = normalizePositiveInteger(
184
+ recordCount,
185
+ "recordCount",
186
+ );
187
+ const byteLength = normalizedRecordCount * normalizedRecordByteLength;
188
+ const buffer = createArenaBuffer(byteLength, { shared: Boolean(shared) });
189
+ const bytes = new Uint8Array(buffer, 0, byteLength);
190
+ if (initialBytes !== null && initialBytes !== undefined) {
191
+ const source = toExactUint8Array(initialBytes, "initialBytes");
192
+ if (source.byteLength > byteLength) {
193
+ throw new RangeError(
194
+ `initialBytes length (${source.byteLength}) exceeds allocated region byteLength (${byteLength}).`,
195
+ );
196
+ }
197
+ bytes.set(source, 0);
198
+ }
199
+
200
+ const region = {
201
+ regionId: nextRegionId++,
202
+ kind: "contiguous",
203
+ layoutId: normalizedLayoutId,
204
+ recordByteLength: normalizedRecordByteLength,
205
+ alignment: normalizedAlignment,
206
+ recordCount: normalizedRecordCount,
207
+ buffer,
208
+ bytes,
209
+ shared: isSharedBuffer(buffer),
210
+ generation: 1,
211
+ consumers: new Map(),
212
+ nextLeaseId: 1,
213
+ invalidated: false,
214
+ metadata: metadata ?? null,
215
+ };
216
+ regions.set(region.regionId, region);
217
+ return describeRegion(region.regionId);
218
+ }
219
+
114
220
  function registerExternalRegion({
115
221
  layoutId,
116
222
  recordByteLength,
@@ -134,6 +240,10 @@ export function createRuntimeRegionStore() {
134
240
  resolveRecordView,
135
241
  "resolveRecordView",
136
242
  ),
243
+ generation: 1,
244
+ consumers: new Map(),
245
+ nextLeaseId: 1,
246
+ invalidated: false,
137
247
  };
138
248
  if (region.getRecordCount === undefined) {
139
249
  region.records.length = normalizePositiveInteger(recordCount, "recordCount");
@@ -151,6 +261,9 @@ export function createRuntimeRegionStore() {
151
261
  if (typeof region.getRecordCount === "function") {
152
262
  throw new Error("Cannot set recordCount for externally counted regions");
153
263
  }
264
+ if (region.kind === "contiguous") {
265
+ return resizeContiguousRegion(normalizedRegionId, recordCount);
266
+ }
154
267
  const normalizedRecordCount = normalizePositiveInteger(
155
268
  recordCount,
156
269
  "recordCount",
@@ -165,7 +278,37 @@ export function createRuntimeRegionStore() {
165
278
  return describeRegion(normalizedRegionId);
166
279
  }
167
280
 
168
- function resolveRecord({ regionId, recordIndex }) {
281
+ function getRegionArena(regionId) {
282
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
283
+ const region = regions.get(normalizedRegionId);
284
+ if (!region || region.invalidated) {
285
+ return null;
286
+ }
287
+ if (region.kind !== "contiguous") {
288
+ throw new TypeError(
289
+ `Region ${normalizedRegionId} (${region.kind}) does not have a contiguous arena.`,
290
+ );
291
+ }
292
+ return {
293
+ regionId: region.regionId,
294
+ buffer: region.buffer,
295
+ byteOffset: region.bytes.byteOffset,
296
+ byteLength: region.bytes.byteLength,
297
+ bytes: region.bytes,
298
+ shared: region.shared,
299
+ alignment: region.alignment,
300
+ generation: region.generation,
301
+ recordCount: region.recordCount,
302
+ recordByteLength: region.recordByteLength,
303
+ };
304
+ }
305
+
306
+ function updateContiguousRecord({
307
+ regionId,
308
+ recordIndex,
309
+ bytes,
310
+ bumpGeneration = false,
311
+ }) {
169
312
  const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
170
313
  const normalizedRecordIndex = normalizePositiveInteger(
171
314
  recordIndex,
@@ -173,6 +316,203 @@ export function createRuntimeRegionStore() {
173
316
  );
174
317
  const region = regions.get(normalizedRegionId);
175
318
  if (!region) {
319
+ throw new Error(`Unknown region ${normalizedRegionId}.`);
320
+ }
321
+ if (region.invalidated) {
322
+ throw new Error(`Region ${normalizedRegionId} has been invalidated.`);
323
+ }
324
+ if (region.kind !== "contiguous") {
325
+ throw new TypeError(
326
+ `Region ${normalizedRegionId} is not a contiguous arena region.`,
327
+ );
328
+ }
329
+ if (normalizedRecordIndex >= region.recordCount) {
330
+ throw new RangeError(
331
+ `recordIndex ${normalizedRecordIndex} out of bounds for region ${normalizedRegionId} (recordCount=${region.recordCount}).`,
332
+ );
333
+ }
334
+ const source = toRecordBytes(bytes, region.recordByteLength);
335
+ region.bytes.set(
336
+ source,
337
+ normalizedRecordIndex * region.recordByteLength,
338
+ );
339
+ if (bumpGeneration) {
340
+ region.generation += 1;
341
+ }
342
+ return {
343
+ regionId: region.regionId,
344
+ recordIndex: normalizedRecordIndex,
345
+ generation: region.generation,
346
+ byteLength: region.recordByteLength,
347
+ };
348
+ }
349
+
350
+ function resizeContiguousRegion(regionId, newRecordCount, { shared } = {}) {
351
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
352
+ const normalizedRecordCount = normalizePositiveInteger(
353
+ newRecordCount,
354
+ "recordCount",
355
+ );
356
+ const region = regions.get(normalizedRegionId);
357
+ if (!region) {
358
+ return null;
359
+ }
360
+ if (region.invalidated) {
361
+ throw new Error(`Region ${normalizedRegionId} has been invalidated.`);
362
+ }
363
+ if (region.kind !== "contiguous") {
364
+ throw new TypeError(
365
+ `Region ${normalizedRegionId} is not a contiguous arena region.`,
366
+ );
367
+ }
368
+ const useShared = shared === undefined ? region.shared : Boolean(shared);
369
+ const nextByteLength = normalizedRecordCount * region.recordByteLength;
370
+ const nextBuffer = createArenaBuffer(nextByteLength, { shared: useShared });
371
+ const nextBytes = new Uint8Array(nextBuffer, 0, nextByteLength);
372
+ const copyBytes = Math.min(region.bytes.byteLength, nextByteLength);
373
+ if (copyBytes > 0) {
374
+ nextBytes.set(region.bytes.subarray(0, copyBytes), 0);
375
+ }
376
+ region.buffer = nextBuffer;
377
+ region.bytes = nextBytes;
378
+ region.shared = isSharedBuffer(nextBuffer);
379
+ region.recordCount = normalizedRecordCount;
380
+ region.generation += 1;
381
+ return describeRegion(normalizedRegionId);
382
+ }
383
+
384
+ function bumpRegionGeneration(regionId) {
385
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
386
+ const region = regions.get(normalizedRegionId);
387
+ if (!region) {
388
+ return null;
389
+ }
390
+ region.generation += 1;
391
+ return region.generation;
392
+ }
393
+
394
+ function invalidateRegion(regionId) {
395
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
396
+ const region = regions.get(normalizedRegionId);
397
+ if (!region) {
398
+ return false;
399
+ }
400
+ region.invalidated = true;
401
+ region.generation += 1;
402
+ region.consumers.clear();
403
+ return true;
404
+ }
405
+
406
+ function attachRegionConsumer(
407
+ regionId,
408
+ { consumerId, mode = "read-only", roleMask = null } = {},
409
+ ) {
410
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
411
+ const normalizedConsumerId = normalizeRequiredString(
412
+ consumerId,
413
+ "consumerId",
414
+ );
415
+ if (mode !== "read-only") {
416
+ throw new Error(
417
+ `Shared region consumer "${normalizedConsumerId}" must attach with mode "read-only" (got "${String(mode)}").`,
418
+ );
419
+ }
420
+ const region = regions.get(normalizedRegionId);
421
+ if (!region) {
422
+ throw new Error(`Cannot attach consumer to unknown region ${normalizedRegionId}.`);
423
+ }
424
+ if (region.invalidated) {
425
+ throw new Error(`Cannot attach consumer to invalidated region ${normalizedRegionId}.`);
426
+ }
427
+ const leaseId = region.nextLeaseId++;
428
+ const attachedGeneration = region.generation;
429
+ const entry = {
430
+ leaseId,
431
+ regionId: normalizedRegionId,
432
+ consumerId: normalizedConsumerId,
433
+ mode: "read-only",
434
+ roleMask,
435
+ generation: attachedGeneration,
436
+ attachedAt: Date.now(),
437
+ };
438
+ region.consumers.set(leaseId, entry);
439
+
440
+ function isCurrent() {
441
+ return (
442
+ !region.invalidated &&
443
+ region.generation === attachedGeneration &&
444
+ region.consumers.has(leaseId)
445
+ );
446
+ }
447
+
448
+ function assertCurrent() {
449
+ if (!region.consumers.has(leaseId)) {
450
+ throw new Error(
451
+ `Region consumer lease ${leaseId} (${normalizedConsumerId}) on region ${normalizedRegionId} is detached or invalidated.`,
452
+ );
453
+ }
454
+ if (region.invalidated) {
455
+ throw new Error(
456
+ `Region ${normalizedRegionId} has been invalidated; lease ${leaseId} (${normalizedConsumerId}) is stale.`,
457
+ );
458
+ }
459
+ if (region.generation !== attachedGeneration) {
460
+ throw new Error(
461
+ `Region ${normalizedRegionId} generation mismatch for lease ${leaseId} (${normalizedConsumerId}): attached at generation ${attachedGeneration}, current generation is ${region.generation}.`,
462
+ );
463
+ }
464
+ return true;
465
+ }
466
+
467
+ function detach() {
468
+ return region.consumers.delete(leaseId);
469
+ }
470
+
471
+ return Object.freeze({
472
+ leaseId,
473
+ regionId: normalizedRegionId,
474
+ consumerId: normalizedConsumerId,
475
+ mode: "read-only",
476
+ roleMask,
477
+ generation: attachedGeneration,
478
+ isCurrent,
479
+ assertCurrent,
480
+ detach,
481
+ });
482
+ }
483
+
484
+ function detachRegionConsumer(regionId, leaseId) {
485
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
486
+ const normalizedLeaseId = normalizeStrictlyPositiveInteger(
487
+ leaseId,
488
+ "leaseId",
489
+ );
490
+ const region = regions.get(normalizedRegionId);
491
+ if (!region) {
492
+ return false;
493
+ }
494
+ return region.consumers.delete(normalizedLeaseId);
495
+ }
496
+
497
+ function listRegionConsumers(regionId) {
498
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
499
+ const region = regions.get(normalizedRegionId);
500
+ if (!region || region.invalidated) {
501
+ return [];
502
+ }
503
+ return Array.from(region.consumers.values(), (entry) =>
504
+ Object.freeze({ ...entry }),
505
+ );
506
+ }
507
+
508
+ function resolveRecord({ regionId, recordIndex }) {
509
+ const normalizedRegionId = normalizePositiveInteger(regionId, "regionId");
510
+ const normalizedRecordIndex = normalizePositiveInteger(
511
+ recordIndex,
512
+ "recordIndex",
513
+ );
514
+ const region = regions.get(normalizedRegionId);
515
+ if (!region || region.invalidated) {
176
516
  return null;
177
517
  }
178
518
  if (normalizedRecordIndex >= getRegionRecordCount(region)) {
@@ -181,6 +521,20 @@ export function createRuntimeRegionStore() {
181
521
  if (region.kind === "external") {
182
522
  return null;
183
523
  }
524
+ if (region.kind === "contiguous") {
525
+ const offset = normalizedRecordIndex * region.recordByteLength;
526
+ return {
527
+ regionId: region.regionId,
528
+ recordIndex: normalizedRecordIndex,
529
+ layoutId: region.layoutId,
530
+ recordByteLength: region.recordByteLength,
531
+ alignment: region.alignment,
532
+ byteLength: region.recordByteLength,
533
+ bytes: cloneBytes(
534
+ region.bytes.subarray(offset, offset + region.recordByteLength),
535
+ ),
536
+ };
537
+ }
184
538
  return {
185
539
  regionId: region.regionId,
186
540
  recordIndex: normalizedRecordIndex,
@@ -199,12 +553,28 @@ export function createRuntimeRegionStore() {
199
553
  "recordIndex",
200
554
  );
201
555
  const region = regions.get(normalizedRegionId);
202
- if (!region) {
556
+ if (!region || region.invalidated) {
203
557
  return null;
204
558
  }
205
559
  if (normalizedRecordIndex >= getRegionRecordCount(region)) {
206
560
  return null;
207
561
  }
562
+ if (region.kind === "contiguous") {
563
+ const offset = normalizedRecordIndex * region.recordByteLength;
564
+ return {
565
+ regionId: region.regionId,
566
+ recordIndex: normalizedRecordIndex,
567
+ layoutId: region.layoutId,
568
+ recordByteLength: region.recordByteLength,
569
+ alignment: region.alignment,
570
+ byteLength: region.recordByteLength,
571
+ byteOffset: region.bytes.byteOffset + offset,
572
+ buffer: region.buffer,
573
+ shared: region.shared,
574
+ generation: region.generation,
575
+ bytes: region.bytes.subarray(offset, offset + region.recordByteLength),
576
+ };
577
+ }
208
578
  if (region.kind !== "external") {
209
579
  return resolveRecord({
210
580
  regionId: normalizedRegionId,
@@ -236,10 +606,19 @@ export function createRuntimeRegionStore() {
236
606
 
237
607
  return {
238
608
  allocateRegion,
609
+ allocateContiguousRegion,
239
610
  describeRegion,
240
611
  registerExternalRegion,
241
612
  resolveRecord,
242
613
  resolveRecordView,
243
614
  setRegionRecordCount,
615
+ getRegionArena,
616
+ updateContiguousRecord,
617
+ resizeContiguousRegion,
618
+ bumpRegionGeneration,
619
+ invalidateRegion,
620
+ attachRegionConsumer,
621
+ detachRegionConsumer,
622
+ listRegionConsumers,
244
623
  };
245
624
  }