@query-farm/vgi 0.48.4 → 0.49.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 (60) hide show
  1. package/README.md +52 -1
  2. package/dist/attach-secret-keyfile.d.ts +11 -0
  3. package/dist/attach-secret-options.d.ts +96 -0
  4. package/dist/catalog/attach-option.d.ts +6 -0
  5. package/dist/catalog/composite.d.ts +2 -0
  6. package/dist/catalog/descriptors.d.ts +11 -0
  7. package/dist/catalog/interface.d.ts +54 -2
  8. package/dist/catalog/scan-planning.d.ts +76 -0
  9. package/dist/client-entry.js +32 -4
  10. package/dist/client-entry.js.map +9 -8
  11. package/dist/generated/vgi-client.d.ts +1 -0
  12. package/dist/generated/vgi-protocol-schemas.d.ts +1 -1
  13. package/dist/generated/vgi-service.d.ts +5 -1
  14. package/dist/index.core.d.ts +3 -0
  15. package/dist/index.js +3245 -2538
  16. package/dist/index.js.map +32 -29
  17. package/dist/protocol/dispatch.d.ts +13 -2
  18. package/dist/protocol/handlers/catalog/admin.d.ts +1 -1
  19. package/dist/protocol/handlers/catalog/index.d.ts +1 -1
  20. package/dist/protocol/handlers/catalog/index_methods.d.ts +1 -1
  21. package/dist/protocol/handlers/catalog/macro.d.ts +1 -1
  22. package/dist/protocol/handlers/catalog/shared.d.ts +28 -3
  23. package/dist/protocol/handlers/catalog/table.d.ts +1 -1
  24. package/dist/protocol/handlers/catalog/view.d.ts +1 -1
  25. package/dist/protocol/handlers/function.d.ts +2 -0
  26. package/dist/protocol/handlers/table-buffering.d.ts +1 -1
  27. package/dist/serve-entry.d.ts +5 -0
  28. package/dist/serve-entry.js +2026 -101
  29. package/dist/serve-entry.js.map +27 -20
  30. package/dist/worker-cf-entry.d.ts +2 -2
  31. package/dist/worker-cf-entry.js +823 -103
  32. package/dist/worker-cf-entry.js.map +27 -24
  33. package/dist/worker.d.ts +10 -0
  34. package/package.json +1 -1
  35. package/src/attach-secret-keyfile.ts +185 -0
  36. package/src/attach-secret-options.ts +418 -0
  37. package/src/catalog/attach-option.ts +6 -0
  38. package/src/catalog/column-metadata.ts +7 -0
  39. package/src/catalog/composite.ts +5 -0
  40. package/src/catalog/descriptors.ts +11 -0
  41. package/src/catalog/interface.ts +97 -2
  42. package/src/catalog/read-only.ts +14 -1
  43. package/src/catalog/scan-planning.ts +314 -0
  44. package/src/generated/vgi-client.ts +2 -1
  45. package/src/generated/vgi-protocol-schemas.ts +6 -2
  46. package/src/generated/vgi-service.ts +7 -2
  47. package/src/index.core.ts +21 -0
  48. package/src/protocol/dispatch.ts +16 -4
  49. package/src/protocol/handlers/catalog/admin.ts +33 -18
  50. package/src/protocol/handlers/catalog/index.ts +6 -5
  51. package/src/protocol/handlers/catalog/index_methods.ts +5 -3
  52. package/src/protocol/handlers/catalog/macro.ts +7 -5
  53. package/src/protocol/handlers/catalog/shared.ts +52 -6
  54. package/src/protocol/handlers/catalog/table.ts +43 -30
  55. package/src/protocol/handlers/catalog/view.ts +9 -7
  56. package/src/protocol/handlers/function.ts +11 -5
  57. package/src/protocol/handlers/table-buffering.ts +2 -1
  58. package/src/serve-entry.ts +6 -1
  59. package/src/worker-cf-entry.ts +9 -0
  60. package/src/worker.ts +12 -0
package/src/index.core.ts CHANGED
@@ -239,6 +239,16 @@ export {
239
239
  RESERVED_ATTACH_OPTION,
240
240
  ReservedAttachOptionError,
241
241
  } from "./catalog/attach-option.js";
242
+ // ATTACH options (secret ones included) as functions and transaction hooks see them.
243
+ export {
244
+ attachOptions,
245
+ attachOption,
246
+ type AttachOptionValues,
247
+ AttachOptionsUnavailableError,
248
+ AttachSecretKeyUnavailableError,
249
+ ATTACH_SECRET_OPTIONS_INFO,
250
+ } from "./attach-secret-options.js";
251
+ export { hostLocalAttachSecretKeyPath, ATTACH_SECRET_KEY_FILE_ENV } from "./attach-secret-keyfile.js";
242
252
  export {
243
253
  ATTACH_TICKET_OPTION,
244
254
  ATTACH_TICKET_PREFIX,
@@ -262,6 +272,17 @@ export {
262
272
 
263
273
  // Column statistics (for inlining on TableInfo.column_statistics).
264
274
  export { type ColumnStatistics, serializeColumnStatistics } from "./util/statistics.js";
275
+ // Protocol 3.2 filtered scan planning (TableInfo.scan_planning, tableScanBranchesPlan).
276
+ export {
277
+ ScanPlanningFilters,
278
+ filtersMayMatch,
279
+ validateScanPlanning,
280
+ SCAN_PLANNING_BIND,
281
+ SCAN_PLANNING_FILTERED,
282
+ SCAN_PLANNING_MODES,
283
+ type ScanPlanningMode,
284
+ type ScanCandidateStatistics,
285
+ } from "./catalog/scan-planning.js";
265
286
 
266
287
  // Byte utilities
267
288
  export { toUint8Array } from "./util/bytes.js";
@@ -30,6 +30,17 @@ export interface ProtocolConfig {
30
30
  * unsealed — OS process ownership already enforces identity there.
31
31
  */
32
32
  signingKey?: Uint8Array | undefined;
33
+ /**
34
+ * Optional root key for secret ATTACH options (`AttachOptionSpec.secret`),
35
+ * which the framework encrypts inside `attach_opaque_data` so functions and
36
+ * transaction hooks can read them with `attachOptions()`. Defaults to the
37
+ * signing key when there is one (HTTP), else the host-local key file
38
+ * (`$VGI_ATTACH_SECRET_KEY_FILE`, or `vgi/attach-secret-options.key` under
39
+ * `$XDG_RUNTIME_DIR` / the per-user temp dir). Any length; the AEAD key is
40
+ * always derived from it with HKDF-SHA256. Every worker process that may
41
+ * receive a given attach must use the same key.
42
+ */
43
+ attachSecretKey?: Uint8Array | undefined;
33
44
  }
34
45
 
35
46
  /**
@@ -69,11 +80,11 @@ export { VGI_PROTOCOL_NAME };
69
80
  * hash covers method names, types and the params/result/header schemas only
70
81
  * (WIRE_PROTOCOL.md §14).
71
82
  *
72
- * It changes only with vgi.v3's protocol version (currently 3.1.0). Asserted
83
+ * It changes only with vgi.v3's protocol version (currently 3.2.0). Asserted
73
84
  * by src/protocol/__tests__/vgi-v3-hash.test.ts; if that test fails, the
74
85
  * surface drifted from the reference -- fix the schemas, do not bump this.
75
86
  */
76
- export const VGI_V3_PROTOCOL_HASH = "50e9404173bee3262e96b8939eb5c425463336abca2a17916812d68b6e0f6cad";
87
+ export const VGI_V3_PROTOCOL_HASH = "171ac8263bf22ffdd5dd4ac2c3d3c4e1dc186a7ceba45d386f324af8c9c6fced";
77
88
 
78
89
  /**
79
90
  * This worker's `vgi.v3` implementation: the generated UNIMPLEMENTED default
@@ -85,11 +96,12 @@ export function buildVgiService(config: ProtocolConfig): VgiService {
85
96
  ...functionHandlers({
86
97
  registry: config.registry,
87
98
  signingKey: config.signingKey,
99
+ attachSecretKey: config.attachSecretKey,
88
100
  catalogInterface: config.catalogInterface,
89
101
  }),
90
102
  ...aggregateHandlers(config.registry),
91
- ...tableBufferingHandlers(config.registry, config.signingKey),
92
- ...catalogHandlers(config.catalogInterface, config.signingKey),
103
+ ...tableBufferingHandlers(config.registry, config.signingKey, config.attachSecretKey),
104
+ ...catalogHandlers(config.catalogInterface, config.signingKey, config.attachSecretKey),
93
105
  };
94
106
  }
95
107
 
@@ -33,6 +33,8 @@ import {
33
33
  import { toUint8Array } from "../../../util/bytes.js";
34
34
  import { redeemAttachTicket } from "../../../attach-ticket.js";
35
35
  import { currentRequestAuth } from "../../../request-auth.js";
36
+ import { secretAttachOptionNames } from "../../../attach-secret-options.js";
37
+ import { deserializeAttachOptionSpec } from "../../../catalog/attach-option.js";
36
38
  import { decodeDictValue } from "../../../util/arrow/index.js";
37
39
  import {
38
40
  unwrapRequest,
@@ -45,11 +47,13 @@ import {
45
47
  sealAttach,
46
48
  openAttach,
47
49
  sealTransaction,
50
+ type OpaqueKeys,
48
51
  } from "./shared.js";
49
52
 
50
- export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
53
+ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
54
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
51
55
  // catalog_catalogs
52
- const catalogCatalogs = catalogHandler(signingKey, async () => {
56
+ const catalogCatalogs = catalogHandler(keys, async () => {
53
57
  const cat = getCatalog();
54
58
  // Each catalog advertised as an IPC-serialized CatalogInfo
55
59
  // {name, implementation_version?, data_version_spec?}. Versioned workers
@@ -121,10 +125,21 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
121
125
  // Seal the attach value into an AEAD envelope bound to the caller's
122
126
  // identity before it leaves the worker (HTTP transport; pass-through
123
127
  // when there is no signing key).
128
+ // The framework packs the attach's options after the catalog's bytes —
129
+ // secret ones (declared `secret: true`) encrypted with a worker-held key —
130
+ // so every function call and transaction hook of this attach can read them
131
+ // with attachOptions(), on every transport, without a catalog ever putting
132
+ // a credential into its own attach bytes.
133
+ const optionsIpc = innerParams.options == null ? null : toUint8Array(innerParams.options);
134
+ const secretNames =
135
+ optionsIpc && optionsIpc.length > 0
136
+ ? await secretAttachOptionNames(cat as never, innerParams.name, deserializeAttachOptionSpec)
137
+ : new Set<string>();
124
138
  const sealedAttach = await sealAttach(
125
139
  toUint8Array(result.attach_opaque_data),
126
140
  (ctx as CallContext | undefined)?.auth,
127
141
  signingKey,
142
+ { optionsIpc, secretNames, attachSecretKey },
128
143
  );
129
144
  // The generated builder emits every column of the pinned
130
145
  // CatalogAttachResult schema, filling vgi-python's defaults for any a
@@ -142,28 +157,28 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
142
157
  };
143
158
 
144
159
  // catalog_detach
145
- const catalogDetach = catalogHandler(signingKey, async (params) => {
160
+ const catalogDetach = catalogHandler(keys, async (params) => {
146
161
  const cat = getCatalog();
147
162
  await cat.detach(toUint8Array(params.attach_opaque_data));
148
163
  return {};
149
164
  });
150
165
 
151
166
  // catalog_create
152
- const catalogCreate = catalogHandler(signingKey, async (params) => {
167
+ const catalogCreate = catalogHandler(keys, async (params) => {
153
168
  const cat = getCatalog();
154
169
  await cat.create(params.name, params.on_conflict, params.options);
155
170
  return {};
156
171
  });
157
172
 
158
173
  // catalog_drop
159
- const catalogDrop = catalogHandler(signingKey, async (params) => {
174
+ const catalogDrop = catalogHandler(keys, async (params) => {
160
175
  const cat = getCatalog();
161
176
  await cat.drop(params.name);
162
177
  return {};
163
178
  });
164
179
 
165
180
  // catalog_version
166
- const catalogVersion = catalogHandler(signingKey, async (params) => {
181
+ const catalogVersion = catalogHandler(keys, async (params) => {
167
182
  const cat = getCatalog();
168
183
  const version = await cat.version(
169
184
  toUint8Array(params.attach_opaque_data),
@@ -179,7 +194,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
179
194
  const auth = (ctx as CallContext | undefined)?.auth;
180
195
  const sealedAttach =
181
196
  params.attach_opaque_data != null ? toUint8Array(params.attach_opaque_data) : new Uint8Array(0);
182
- const attachPlain = await openAttach(sealedAttach, auth, signingKey);
197
+ const attachPlain = await openAttach(sealedAttach, auth, signingKey, attachSecretKey);
183
198
  const cat = getCatalog();
184
199
  const txPlain = await cat.transactionBegin(attachPlain);
185
200
  // Seal the transaction value, binding it to the caller's identity and
@@ -190,7 +205,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
190
205
  };
191
206
 
192
207
  // catalog_transaction_commit
193
- const catalogTransactionCommit = catalogHandler(signingKey, async (params) => {
208
+ const catalogTransactionCommit = catalogHandler(keys, async (params) => {
194
209
  const cat = getCatalog();
195
210
  await cat.transactionCommit(
196
211
  toUint8Array(params.attach_opaque_data),
@@ -200,7 +215,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
200
215
  });
201
216
 
202
217
  // catalog_transaction_rollback
203
- const catalogTransactionRollback = catalogHandler(signingKey, async (params) => {
218
+ const catalogTransactionRollback = catalogHandler(keys, async (params) => {
204
219
  const cat = getCatalog();
205
220
  await cat.transactionRollback(
206
221
  toUint8Array(params.attach_opaque_data),
@@ -210,7 +225,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
210
225
  });
211
226
 
212
227
  // catalog_schemas
213
- const catalogSchemas = catalogHandler(signingKey, async (params) => {
228
+ const catalogSchemas = catalogHandler(keys, async (params) => {
214
229
  const cat = getCatalog();
215
230
  const schemas = await cat.schemas(
216
231
  toUint8Array(params.attach_opaque_data),
@@ -229,7 +244,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
229
244
  // rules, the content-hash etag and the frozen-catalog cache.
230
245
  const catalogContents: VgiService["catalogContents"] = async (params, ctx) => {
231
246
  const sealedAttach = toUint8Array(params.attach_opaque_data).slice();
232
- return catalogHandler(signingKey, async (opened, context) => {
247
+ return catalogHandler(keys, async (opened, context) => {
233
248
  const ifNoneMatch = opened.if_none_match == null ? null : String(opened.if_none_match);
234
249
  return serveCatalogContents(getCatalog(), toUint8Array(opened.attach_opaque_data), ifNoneMatch, context, sealedAttach);
235
250
  })(params, ctx);
@@ -237,7 +252,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
237
252
 
238
253
  // catalog_copy_from_formats — catalog-level (not schema-scoped). Lists the
239
254
  // custom COPY ... FROM formats this catalog advertises; empty list when none.
240
- const catalogCopyFromFormats = catalogHandler(signingKey, async (params) => {
255
+ const catalogCopyFromFormats = catalogHandler(keys, async (params) => {
241
256
  const cat = getCatalog();
242
257
  const formats = await cat.copyFromFormats(
243
258
  toUint8Array(params.attach_opaque_data),
@@ -249,7 +264,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
249
264
  });
250
265
 
251
266
  // catalog_schema_get
252
- const catalogSchemaGet = catalogHandler(signingKey, async (params) => {
267
+ const catalogSchemaGet = catalogHandler(keys, async (params) => {
253
268
  const cat = getCatalog();
254
269
  const info = await cat.schemaGet(
255
270
  toUint8Array(params.attach_opaque_data),
@@ -262,7 +277,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
262
277
  });
263
278
 
264
279
  // catalog_schema_create
265
- const catalogSchemaCreate = catalogHandler(signingKey, async (params) => {
280
+ const catalogSchemaCreate = catalogHandler(keys, async (params) => {
266
281
  if (params.options != null) throw new MethodNotImplementedError("catalog_schema_create requires negotiated vgi.schema.options.create.v1");
267
282
  const cat = getCatalog();
268
283
  await cat.schemaCreate(
@@ -277,7 +292,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
277
292
  });
278
293
 
279
294
  // catalog_schema_drop
280
- const catalogSchemaDrop = catalogHandler(signingKey, async (params) => {
295
+ const catalogSchemaDrop = catalogHandler(keys, async (params) => {
281
296
  const cat = getCatalog();
282
297
  await cat.schemaDrop(
283
298
  toUint8Array(params.attach_opaque_data),
@@ -290,7 +305,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
290
305
  });
291
306
 
292
307
  // catalog_schema_contents_tables
293
- const catalogSchemaContentsTables = catalogHandler(signingKey, async (params) => {
308
+ const catalogSchemaContentsTables = catalogHandler(keys, async (params) => {
294
309
  const cat = getCatalog();
295
310
  const tables = await cat.schemaContentsTables(
296
311
  toUint8Array(params.attach_opaque_data),
@@ -303,7 +318,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
303
318
  });
304
319
 
305
320
  // catalog_schema_contents_views
306
- const catalogSchemaContentsViews = catalogHandler(signingKey, async (params) => {
321
+ const catalogSchemaContentsViews = catalogHandler(keys, async (params) => {
307
322
  const cat = getCatalog();
308
323
  const views = await cat.schemaContentsViews(
309
324
  toUint8Array(params.attach_opaque_data),
@@ -316,7 +331,7 @@ export function catalogAdminHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
316
331
  });
317
332
 
318
333
  // catalog_schema_contents_functions
319
- const catalogSchemaContentsFunctions = catalogHandler(signingKey, async (params) => {
334
+ const catalogSchemaContentsFunctions = catalogHandler(keys, async (params) => {
320
335
  const cat = getCatalog();
321
336
  const funcs = await cat.schemaContentsFunctions(
322
337
  toUint8Array(params.attach_opaque_data),
@@ -14,13 +14,14 @@ import { catalogIndexHandlers } from "./index_methods.js";
14
14
  export function catalogHandlers(
15
15
  catalog: CatalogInterface | undefined,
16
16
  signingKey?: Uint8Array,
17
+ attachSecretKey?: Uint8Array,
17
18
  ): Partial<VgiService> {
18
19
  const getCatalog = makeGetCatalog(catalog);
19
20
  return {
20
- ...catalogAdminHandlers(getCatalog, signingKey),
21
- ...catalogTableHandlers(getCatalog, signingKey),
22
- ...catalogViewHandlers(getCatalog, signingKey),
23
- ...catalogMacroHandlers(getCatalog, signingKey),
24
- ...catalogIndexHandlers(getCatalog, signingKey),
21
+ ...catalogAdminHandlers(getCatalog, signingKey, attachSecretKey),
22
+ ...catalogTableHandlers(getCatalog, signingKey, attachSecretKey),
23
+ ...catalogViewHandlers(getCatalog, signingKey, attachSecretKey),
24
+ ...catalogMacroHandlers(getCatalog, signingKey, attachSecretKey),
25
+ ...catalogIndexHandlers(getCatalog, signingKey, attachSecretKey),
25
26
  };
26
27
  }
@@ -14,11 +14,13 @@ import {
14
14
  import {
15
15
  type GetCatalog,
16
16
  catalogHandler,
17
+ type OpaqueKeys,
17
18
  } from "./shared.js";
18
19
 
19
- export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
20
+ export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
21
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
20
22
  // catalog_schema_contents_indexes
21
- const catalogSchemaContentsIndexes = catalogHandler(signingKey, async (params) => {
23
+ const catalogSchemaContentsIndexes = catalogHandler(keys, async (params) => {
22
24
  const cat = getCatalog();
23
25
  const indexes = await cat.schemaContentsIndexes(
24
26
  toUint8Array(params.attach_opaque_data),
@@ -31,7 +33,7 @@ export function catalogIndexHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
31
33
  });
32
34
 
33
35
  // catalog_index_get
34
- const catalogIndexGet = catalogHandler(signingKey, async (params) => {
36
+ const catalogIndexGet = catalogHandler(keys, async (params) => {
35
37
  const cat = getCatalog();
36
38
  const info = await cat.indexGet(
37
39
  toUint8Array(params.attach_opaque_data),
@@ -19,11 +19,13 @@ import {
19
19
  import {
20
20
  type GetCatalog,
21
21
  catalogHandler,
22
+ type OpaqueKeys,
22
23
  } from "./shared.js";
23
24
 
24
- export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array): Partial<VgiService> {
25
+ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8Array, attachSecretKey?: Uint8Array): Partial<VgiService> {
26
+ const keys: OpaqueKeys = { signingKey, attachSecretKey };
25
27
  // catalog_macro_get
26
- const catalogMacroGet = catalogHandler(signingKey, async (params) => {
28
+ const catalogMacroGet = catalogHandler(keys, async (params) => {
27
29
  const cat = getCatalog();
28
30
  const info = await cat.macroGet(
29
31
  toUint8Array(params.attach_opaque_data),
@@ -37,7 +39,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
37
39
  });
38
40
 
39
41
  // catalog_macro_create
40
- const catalogMacroCreate = catalogHandler(signingKey, async (params) => {
42
+ const catalogMacroCreate = catalogHandler(keys, async (params) => {
41
43
  const cat = getCatalog();
42
44
  // catalogHandler already flattened the wrapped request.
43
45
  const innerParams = decodeASD<MacroCreateRequest>(MacroCreateRequestSchema, encodeASD(MacroCreateRequestSchema, params));
@@ -55,7 +57,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
55
57
  });
56
58
 
57
59
  // catalog_macro_drop
58
- const catalogMacroDrop = catalogHandler(signingKey, async (params) => {
60
+ const catalogMacroDrop = catalogHandler(keys, async (params) => {
59
61
  const cat = getCatalog();
60
62
  await cat.macroDrop(
61
63
  toUint8Array(params.attach_opaque_data),
@@ -68,7 +70,7 @@ export function catalogMacroHandlers(getCatalog: GetCatalog, signingKey?: Uint8A
68
70
  });
69
71
 
70
72
  // catalog_schema_contents_macros
71
- const catalogSchemaContentsMacros = catalogHandler(signingKey, async (params) => {
73
+ const catalogSchemaContentsMacros = catalogHandler(keys, async (params) => {
72
74
  const cat = getCatalog();
73
75
  const macros = await cat.schemaContentsMacros(
74
76
  toUint8Array(params.attach_opaque_data),
@@ -17,6 +17,30 @@ import {
17
17
  ATTACH_UUID_LEN,
18
18
  TRANSACTION_ENVELOPE_VERSION,
19
19
  } from "../../../crypto.js";
20
+ import { openFrameworkAttach, packAttachOptionsTrailer } from "../../../attach-secret-options.js";
21
+
22
+ /**
23
+ * The keys a catalog handler group opens opaque values with: the HTTP signing
24
+ * key (absent on stdio / unix) and the optional explicit root key for secret
25
+ * ATTACH options (`attachSecretKey`; see ../../../attach-secret-options.ts).
26
+ */
27
+ export interface OpaqueKeys {
28
+ signingKey?: Uint8Array | undefined;
29
+ attachSecretKey?: Uint8Array | undefined;
30
+ }
31
+
32
+ /**
33
+ * The options a catalog_attach packs into the attach value after the catalog's
34
+ * own bytes (secret ones encrypted). See ../../../attach-secret-options.ts.
35
+ */
36
+ export interface AttachOptionsToPack {
37
+ /** The request's `options` IPC batch (null / empty: no options, no trailer). */
38
+ optionsIpc: Uint8Array | null | undefined;
39
+ /** Lower-cased names of the options declared `secret: true`. */
40
+ secretNames: ReadonlySet<string>;
41
+ /** Explicit root key for the secret options, if configured. */
42
+ attachSecretKey?: Uint8Array | undefined;
43
+ }
20
44
 
21
45
  export type GetCatalog = () => CatalogInterface;
22
46
 
@@ -36,17 +60,31 @@ export type GetCatalog = () => CatalogInterface;
36
60
  * across re-seals and globally unique, unlike the random-nonce ciphertext or
37
61
  * the (possibly non-unique) catalog bytes. openAttach strips the UUID back off
38
62
  * so the catalog only ever sees its own bytes. Mirrors vgi-python/-go.
63
+ *
64
+ * With `options`, the attach's ATTACH options are packed after the catalog's
65
+ * bytes (secret ones encrypted, never plaintext) so `attachOptions()` can hand
66
+ * them to every function call and transaction hook of this attach.
39
67
  */
40
68
  export async function sealAttach(
41
69
  plaintext: Uint8Array,
42
70
  auth: AuthContext | undefined,
43
71
  signingKey: Uint8Array | undefined,
72
+ options?: AttachOptionsToPack,
44
73
  ): Promise<Uint8Array> {
45
74
  const uuid = new Uint8Array(ATTACH_UUID_LEN);
46
75
  crypto.getRandomValues(uuid);
47
- const minted = new Uint8Array(ATTACH_UUID_LEN + plaintext.length);
76
+ // The attach's options ride after the catalog's bytes, the secret ones
77
+ // encrypted under a key bound to this uuid (empty when there are none).
78
+ const trailer = options
79
+ ? await packAttachOptionsTrailer(uuid, options.optionsIpc, options.secretNames, {
80
+ explicit: options.attachSecretKey,
81
+ signingKey,
82
+ })
83
+ : new Uint8Array(0);
84
+ const minted = new Uint8Array(ATTACH_UUID_LEN + plaintext.length + trailer.length);
48
85
  minted.set(uuid, 0);
49
86
  minted.set(plaintext, ATTACH_UUID_LEN);
87
+ minted.set(trailer, ATTACH_UUID_LEN + plaintext.length);
50
88
  if (!signingKey) return minted;
51
89
  return sealBytes(minted, signingKey, attachAad(auth), ATTACH_ENVELOPE_VERSION);
52
90
  }
@@ -73,9 +111,12 @@ export async function openAttach(
73
111
  envelope: Uint8Array,
74
112
  auth: AuthContext | undefined,
75
113
  signingKey: Uint8Array | undefined,
114
+ attachSecretKey?: Uint8Array,
76
115
  ): Promise<Uint8Array> {
77
116
  const full = await openAttachFull(envelope, auth, signingKey);
78
- return full.length < ATTACH_UUID_LEN ? full : full.subarray(ATTACH_UUID_LEN);
117
+ // Strips the uuid and the options trailer, decrypting secret options and
118
+ // recording all of them for attachOptions().
119
+ return openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
79
120
  }
80
121
 
81
122
  /** Hex of a byte array (lowercase). */
@@ -154,8 +195,9 @@ export async function openTransaction(
154
195
  async function unwrapParamsOpaque(
155
196
  params: Record<string, any>,
156
197
  ctx: CallContext | undefined,
157
- signingKey: Uint8Array | undefined,
198
+ keys: OpaqueKeys,
158
199
  ): Promise<void> {
200
+ const { signingKey, attachSecretKey } = keys;
159
201
  const auth = ctx?.auth;
160
202
  let sealedAttach: Uint8Array = new Uint8Array(0);
161
203
  if (params.attach_opaque_data != null) {
@@ -171,7 +213,7 @@ async function unwrapParamsOpaque(
171
213
  const full = signingKey
172
214
  ? await openBytes(env, signingKey, attachAad(auth), ATTACH_ENVELOPE_VERSION, "attach_opaque_data")
173
215
  : env;
174
- params.attach_opaque_data = full.length < ATTACH_UUID_LEN ? full : full.subarray(ATTACH_UUID_LEN);
216
+ params.attach_opaque_data = await openFrameworkAttach(full, { explicit: attachSecretKey, signingKey });
175
217
  }
176
218
  }
177
219
  // transaction_opaque_data is sealed only on HTTP (no UUID prefix); subprocess
@@ -197,9 +239,13 @@ async function unwrapParamsOpaque(
197
239
  * bespoke. The body sees the flattened params as a plain record.
198
240
  */
199
241
  export function catalogHandler<R>(
200
- signingKey: Uint8Array | undefined,
242
+ keysOrSigningKey: OpaqueKeys | Uint8Array | undefined,
201
243
  handler: (params: Record<string, any>, ctx: CallContext) => R | Promise<R>,
202
244
  ): (params: object, ctx: CallContext) => Promise<R> {
245
+ const keys: OpaqueKeys =
246
+ keysOrSigningKey instanceof Uint8Array || keysOrSigningKey === undefined
247
+ ? { signingKey: keysOrSigningKey }
248
+ : keysOrSigningKey;
203
249
  return async (wire: object, ctx: CallContext) => {
204
250
  let params = wire as Record<string, any>;
205
251
  // catalog_create / catalog_table_create / catalog_macro_create (and
@@ -214,7 +260,7 @@ export function catalogHandler<R>(
214
260
  params = unwrapRequest(params.request);
215
261
  }
216
262
  normalizeRequestSchemaPaths(params);
217
- await unwrapParamsOpaque(params, ctx, signingKey);
263
+ await unwrapParamsOpaque(params, ctx, keys);
218
264
  return handler(params, ctx);
219
265
  };
220
266
  }