@query-farm/vgi 0.43.0 → 0.44.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,4 +1,27 @@
1
1
  var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ function __accessProp(key) {
6
+ return this[key];
7
+ }
8
+ var __toCommonJS = (from) => {
9
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
10
+ if (entry)
11
+ return entry;
12
+ entry = __defProp({}, "__esModule", { value: true });
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (var key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(entry, key))
16
+ __defProp(entry, key, {
17
+ get: __accessProp.bind(from, key),
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ __moduleCache.set(from, entry);
22
+ return entry;
23
+ };
24
+ var __moduleCache;
2
25
  var __returnValue = (v) => v;
3
26
  function __exportSetter(name, newValue) {
4
27
  this[name] = __returnValue.bind(null, newValue);
@@ -1978,6 +2001,11 @@ var init_arrow = __esm(() => {
1978
2001
  });
1979
2002
 
1980
2003
  // src/functions/storage-cf-do.ts
2004
+ var exports_storage_cf_do = {};
2005
+ __export(exports_storage_cf_do, {
2006
+ FunctionStorageCfDo: () => FunctionStorageCfDo
2007
+ });
2008
+
1981
2009
  class FunctionStorageCfDo {
1982
2010
  _baseUrl;
1983
2011
  _token;
@@ -2423,7 +2451,7 @@ function resolveStorageFromEnv() {
2423
2451
  return new FunctionStorageSqlite;
2424
2452
  }
2425
2453
  if (backend === "cloudflare-do") {
2426
- init_storage_cf_do();
2454
+ const { FunctionStorageCfDo } = (init_storage_cf_do(), __toCommonJS(exports_storage_cf_do));
2427
2455
  return FunctionStorageCfDo.fromEnv();
2428
2456
  }
2429
2457
  throw new Error(`Unknown VGI_WORKER_SHARED_STORAGE backend: '${backend}'. Supported: 'memory', 'sqlite', 'cloudflare-do'.`);
@@ -4138,6 +4166,135 @@ var VGI_RANGE_KEY = "vgi_range";
4138
4166
  var VGI_PATTERN_KEY = "vgi_pattern";
4139
4167
  var DEFAULT_MAX_WORKERS = 99999;
4140
4168
 
4169
+ // src/codec/conform.ts
4170
+ init_arrow();
4171
+ var OPTIONAL_ITEM_FIELDS = new Set(["argument_names"]);
4172
+ function children(type) {
4173
+ return type.children ?? [];
4174
+ }
4175
+ function mapValueType(type) {
4176
+ const entries = children(type)[0].type;
4177
+ return children(entries)[1].type;
4178
+ }
4179
+ function mapPairs(raw) {
4180
+ if (Array.isArray(raw))
4181
+ return raw.map((e) => Array.isArray(e) ? [e[0], e[1]] : [e?.key, e?.value]);
4182
+ if (raw instanceof Map)
4183
+ return Array.from(raw.entries());
4184
+ if (typeof raw[Symbol.iterator] === "function")
4185
+ return Array.from(raw);
4186
+ return Object.entries(raw);
4187
+ }
4188
+ function zeroValue(type) {
4189
+ if (isUtf8(type) || isDictionary(type))
4190
+ return "";
4191
+ if (isBinary(type))
4192
+ return new Uint8Array(0);
4193
+ if (isBool(type))
4194
+ return false;
4195
+ if (isInt(type))
4196
+ return type.bitWidth === 64 ? 0n : 0;
4197
+ if (isFloat(type))
4198
+ return 0;
4199
+ if (isList(type) || isMap(type))
4200
+ return [];
4201
+ if (isStruct(type)) {
4202
+ const out = {};
4203
+ for (const c of children(type))
4204
+ out[c.name] = c.nullable ? null : conformValue(c.type, undefined, false, c.name);
4205
+ return out;
4206
+ }
4207
+ return null;
4208
+ }
4209
+ var HAS_CONTAINER = new WeakMap;
4210
+ function hasContainer(type) {
4211
+ const cached = HAS_CONTAINER.get(type);
4212
+ if (cached !== undefined)
4213
+ return cached;
4214
+ let has = isList(type) || isMap(type);
4215
+ if (!has && isStruct(type))
4216
+ has = children(type).some((c) => hasContainer(c.type));
4217
+ HAS_CONTAINER.set(type, has);
4218
+ return has;
4219
+ }
4220
+ function conformValue(type, value, nullable, name) {
4221
+ if (value == null) {
4222
+ if (nullable)
4223
+ return value ?? null;
4224
+ if (isList(type) || isMap(type))
4225
+ return [];
4226
+ return value;
4227
+ }
4228
+ if (!hasContainer(type))
4229
+ return value;
4230
+ if (isList(type)) {
4231
+ const itemType = children(type)[0].type;
4232
+ const itemsNullable = OPTIONAL_ITEM_FIELDS.has(name);
4233
+ const items = Array.isArray(value) ? value : Array.from(value);
4234
+ let changed = !Array.isArray(value);
4235
+ const out = items.map((item) => {
4236
+ const next = item == null && !itemsNullable ? zeroValue(itemType) : conformValue(itemType, item, true, name);
4237
+ if (next !== item)
4238
+ changed = true;
4239
+ return next;
4240
+ });
4241
+ return changed ? out : value;
4242
+ }
4243
+ if (isMap(type)) {
4244
+ const valueType = mapValueType(type);
4245
+ const pairs = mapPairs(value);
4246
+ let changed = false;
4247
+ const out = pairs.map(([k, v]) => {
4248
+ const next = v == null ? zeroValue(valueType) : conformValue(valueType, v, true, name);
4249
+ if (next !== v)
4250
+ changed = true;
4251
+ return [k, next];
4252
+ });
4253
+ return changed ? out : value;
4254
+ }
4255
+ if (isStruct(type)) {
4256
+ let out = null;
4257
+ for (const c of children(type)) {
4258
+ const v = value[c.name];
4259
+ const next = conformValue(c.type, v, c.nullable, c.name);
4260
+ if (next !== v) {
4261
+ (out ??= { ...value })[c.name] = next;
4262
+ }
4263
+ }
4264
+ return out ?? value;
4265
+ }
4266
+ return value;
4267
+ }
4268
+ function conformRecord(schema, obj) {
4269
+ let out = null;
4270
+ for (const f of schema.fields) {
4271
+ const v = obj[f.name];
4272
+ const next = conformValue(f.type, v, f.nullable, f.name);
4273
+ if (next !== v) {
4274
+ (out ??= { ...obj })[f.name] = next;
4275
+ }
4276
+ }
4277
+ return out ?? obj;
4278
+ }
4279
+ function conformColumns(schema, columns) {
4280
+ let out = null;
4281
+ for (const f of schema.fields) {
4282
+ const col = columns[f.name];
4283
+ const rows = col ?? [undefined];
4284
+ let changed = col === undefined;
4285
+ const next = rows.map((v) => {
4286
+ const c = conformValue(f.type, v, f.nullable, f.name);
4287
+ if (c !== v)
4288
+ changed = true;
4289
+ return c;
4290
+ });
4291
+ if (changed) {
4292
+ (out ??= { ...columns })[f.name] = next;
4293
+ }
4294
+ }
4295
+ return out ?? columns;
4296
+ }
4297
+
4141
4298
  // src/protocol/serializers/shared.ts
4142
4299
  function toUint8Array2(val) {
4143
4300
  if (typeof val === "string")
@@ -6953,7 +7110,8 @@ function normalizeRequestSchemaPaths(params) {
6953
7110
  }
6954
7111
  function wrapResult(values, innerSchema) {
6955
7112
  const a = innerSchema;
6956
- const batch = batchFromColumns(Object.fromEntries(a.fields.map((f) => [f.name, [values[f.name] ?? null]])), a);
7113
+ const row = conformRecord(a, values);
7114
+ const batch = batchFromColumns(Object.fromEntries(a.fields.map((f) => [f.name, [row[f.name] ?? null]])), a);
6957
7115
  return { result: serializeBatch(batch) };
6958
7116
  }
6959
7117
  function overloadContext(req, catalogInterface) {
@@ -6979,9 +7137,12 @@ init_arrow();
6979
7137
 
6980
7138
  // src/errors.ts
6981
7139
  class VgiError extends Error {
6982
- constructor(message) {
7140
+ errorCode;
7141
+ constructor(message, code) {
6983
7142
  super(message);
6984
7143
  this.name = "VgiError";
7144
+ if (code !== undefined)
7145
+ this.errorCode = code;
6985
7146
  }
6986
7147
  }
6987
7148
 
@@ -6994,27 +7155,27 @@ class RowCountMismatchError extends VgiError {
6994
7155
 
6995
7156
  class FunctionNotFoundError extends VgiError {
6996
7157
  constructor(name, available) {
6997
- super(`Unknown function '${name}'`);
7158
+ super(`Unknown function '${name}'`, "NOT_FOUND");
6998
7159
  this.name = "FunctionNotFoundError";
6999
7160
  }
7000
7161
  }
7001
7162
 
7002
7163
  class CatalogReadOnlyError extends VgiError {
7003
7164
  constructor(operation) {
7004
- super(`catalog is read-only: ${operation} is not supported`);
7165
+ super(`catalog is read-only: ${operation} is not supported`, "FAILED_PRECONDITION");
7005
7166
  this.name = "CatalogReadOnlyError";
7006
7167
  }
7007
7168
  }
7008
7169
  class ArgumentValidationError extends VgiError {
7009
7170
  constructor(message) {
7010
- super(message);
7171
+ super(message, "INVALID_ARGUMENT");
7011
7172
  this.name = "ArgumentValidationError";
7012
7173
  }
7013
7174
  }
7014
7175
 
7015
7176
  class NoCatalogError extends VgiError {
7016
7177
  constructor() {
7017
- super("No catalog is configured for this worker");
7178
+ super("No catalog is configured for this worker", "UNIMPLEMENTED");
7018
7179
  this.name = "NoCatalogError";
7019
7180
  }
7020
7181
  }
@@ -9111,7 +9272,7 @@ function hex(buf) {
9111
9272
  // src/codec/asd.ts
9112
9273
  init_arrow();
9113
9274
  function encodeASD(schema, obj) {
9114
- return serializeBatch(batchFromRows([sortMapsForSchema(schema, obj)], schema));
9275
+ return serializeBatch(batchFromRows([sortMapsForSchema(schema, conformRecord(schema, obj))], schema));
9115
9276
  }
9116
9277
  function compareCodePoints(a, b) {
9117
9278
  const n = Math.min(a.length, b.length);
@@ -9128,7 +9289,7 @@ function compareCodePoints(a, b) {
9128
9289
  }
9129
9290
  return a.length - b.length;
9130
9291
  }
9131
- function mapPairs(raw) {
9292
+ function mapPairs2(raw) {
9132
9293
  if (Array.isArray(raw))
9133
9294
  return raw.map((e) => Array.isArray(e) ? [e[0], e[1]] : [e?.key, e?.value]);
9134
9295
  if (raw instanceof Map)
@@ -9163,7 +9324,7 @@ function sortMapsForType(type, value) {
9163
9324
  if (isMap(type)) {
9164
9325
  const entries = type.children[0].type;
9165
9326
  const valueType = entries.children[1].type;
9166
- return mapPairs(value).map(([k, v]) => [k, sortMapsForType(valueType, v)]).sort((x, y) => compareKeys(x[0], y[0]));
9327
+ return mapPairs2(value).map(([k, v]) => [k, sortMapsForType(valueType, v)]).sort((x, y) => compareKeys(x[0], y[0]));
9167
9328
  }
9168
9329
  if (isList(type)) {
9169
9330
  const child = type.children[0].type;
@@ -9421,12 +9582,12 @@ var encodeAttachCatalogInfo = (v) => encodeASD(AttachCatalogInfoSchema, {
9421
9582
  secret_ref: v.secret_ref ?? ""
9422
9583
  });
9423
9584
  function encodeScanFunctionResult(functionName, argumentsBytes, requiredExtensions = [], schemaPath = null) {
9424
- return serializeBatch(batchFromColumns({
9585
+ return serializeBatch(batchFromColumns(conformColumns(ScanFunctionResultSchema, {
9425
9586
  function_name: [functionName],
9426
9587
  arguments: [argumentsBytes],
9427
9588
  required_extensions: [requiredExtensions],
9428
9589
  schema_path: [schemaPath]
9429
- }, ScanFunctionResultSchema));
9590
+ }), ScanFunctionResultSchema));
9430
9591
  }
9431
9592
  function singleBranchResult(legacy) {
9432
9593
  return {
@@ -9441,7 +9602,7 @@ function singleBranchResult(legacy) {
9441
9602
  };
9442
9603
  }
9443
9604
  function encodeScanBranch(cols) {
9444
- return serializeBatch(batchFromColumns({
9605
+ return serializeBatch(batchFromColumns(conformColumns(ScanBranchSchema, {
9445
9606
  function_name: [cols.function_name],
9446
9607
  arguments: [cols.arguments],
9447
9608
  branch_filter: [cols.branch_filter ?? null],
@@ -9453,7 +9614,7 @@ function encodeScanBranch(cols) {
9453
9614
  format_locations: [cols.format_locations ?? null],
9454
9615
  format_options: [cols.format_options ?? null],
9455
9616
  schema_path: [cols.schema_path ?? null]
9456
- }, ScanBranchSchema));
9617
+ }), ScanBranchSchema));
9457
9618
  }
9458
9619
  function isPromise(v) {
9459
9620
  return v != null && typeof v.then === "function";
@@ -9762,7 +9923,7 @@ async function checkReservedAttachOptions(catalog) {
9762
9923
  var ATTACH_TICKET_PREFIX = "vgia1.";
9763
9924
  var ATTACH_TICKET_OPTION = RESERVED_ATTACH_OPTION;
9764
9925
  var ATTACH_TICKETS_PROTOCOL_NAME = "vgi.attach_tickets.v1";
9765
- var ATTACH_TICKETS_PROTOCOL_VERSION = "1.0.0";
9926
+ var ATTACH_TICKETS_PROTOCOL_VERSION = "2.0.0";
9766
9927
  var TICKET_ENVELOPE_VERSION = 1;
9767
9928
  var MAX_OPTIONS_BYTES = 16 * 1024;
9768
9929
  var MAX_TICKET_CHARS = 32 * 1024;
@@ -10037,8 +10198,19 @@ var SEAL_ATTACH_REQUEST_SCHEMA = schema2([
10037
10198
  ]);
10038
10199
  var ATTACH_TICKET_SCHEMA = schema2([
10039
10200
  field2("ticket", utf82(), false),
10040
- field2("expires_at", float642(), false)
10201
+ field2("expires_at", timestamp2(TimeUnit2.MICROSECOND, "UTC"), true)
10041
10202
  ]);
10203
+ function expiresAtMicros(value) {
10204
+ if (value === null)
10205
+ return null;
10206
+ const ms = value.getTime();
10207
+ if (!Number.isFinite(ms))
10208
+ throw new TypeError("AttachTicket.expires_at must be a valid Date or null");
10209
+ return BigInt(ms) * 1000n;
10210
+ }
10211
+ function encodeAttachTicket(ticket) {
10212
+ return serializeBatch(batchFromColumns({ ticket: [ticket.ticket], expires_at: [expiresAtMicros(ticket.expires_at)] }, ATTACH_TICKET_SCHEMA));
10213
+ }
10042
10214
  function resolveTicketMaxTtl(grantKeys, env) {
10043
10215
  if (grantKeys)
10044
10216
  return Math.trunc(grantKeys.maxTtlSeconds);
@@ -10125,7 +10297,7 @@ async function sealAttach2(config, request, auth) {
10125
10297
  } catch (e) {
10126
10298
  throw new AttachTicketRequestError("seal_attach request is invalid", [["request", e.message]]);
10127
10299
  }
10128
- return { ticket: token, expires_at: expiresAt === 0 ? Number.POSITIVE_INFINITY : expiresAt };
10300
+ return { ticket: token, expires_at: expiresAt === 0 ? null : new Date(expiresAt * 1000) };
10129
10301
  }
10130
10302
  function buildAttachTicketsProtocol(config) {
10131
10303
  const p = new Protocol2(ATTACH_TICKETS_PROTOCOL_NAME, { protocolVersion: ATTACH_TICKETS_PROTOCOL_VERSION });
@@ -10136,8 +10308,7 @@ function buildAttachTicketsProtocol(config) {
10136
10308
  handler: async (params, ctx) => {
10137
10309
  const request = unwrapRequest(params.request);
10138
10310
  const sealed = await sealAttach2(config, request, ctx?.auth);
10139
- const batch = batchFromColumns({ ticket: [sealed.ticket], expires_at: [sealed.expires_at] }, ATTACH_TICKET_SCHEMA);
10140
- return { result: serializeBatch(batch) };
10311
+ return { result: encodeAttachTicket(sealed) };
10141
10312
  }
10142
10313
  });
10143
10314
  return p;
@@ -10664,7 +10835,7 @@ function validatedHostedProtocols(hosting, primary) {
10664
10835
  }
10665
10836
  const name = p.name;
10666
10837
  if (name.startsWith(RESERVED_PREFIX)) {
10667
- throw new Error(`${owner} entry ${index} is named '${name}', which claims the reserved '${RESERVED_PREFIX}' prefix. ` + "Framework protocols are not supplied through this hook: reflection is hosted automatically, and " + "vgi_rpc.Identity.v1 is enabled by supplying resolveToken and/or mintGrant.");
10838
+ throw new Error(`${owner} entry ${index} is named '${name}', which claims the reserved '${RESERVED_PREFIX}' prefix. ` + "Framework protocols are not supplied through this hook: reflection is hosted automatically, and " + "vgi_rpc.Identity.v2 is enabled by supplying resolveToken and/or mintGrant.");
10668
10839
  }
10669
10840
  if (!/^[A-Za-z_][A-Za-z0-9_.]*$/.test(name)) {
10670
10841
  throw new Error(`${owner} entry ${index}: protocol name '${name}' is not an identifier, optionally dot-qualified ` + "(e.g. 'acme.Reports.v1').");
@@ -10714,7 +10885,7 @@ function resolveIntrospectPrincipals(explicit, env) {
10714
10885
  principals = raw.split(",").map((p) => p.trim()).filter((p) => p !== "");
10715
10886
  }
10716
10887
  if (principals.length === 0) {
10717
- throw new Error("This worker supplies resolveToken, which hosts the vgi_rpc.Identity.v1 protocol, but no " + `introspector allowlist was configured. Set ${INTROSPECT_PRINCIPALS_ENV} (comma-separated) or pass ` + `introspectPrincipals.
10888
+ throw new Error("This worker supplies resolveToken, which hosts the vgi_rpc.Identity.v2 protocol, but no " + `introspector allowlist was configured. Set ${INTROSPECT_PRINCIPALS_ENV} (comma-separated) or pass ` + `introspectPrincipals.
10718
10889
 
10719
10890
  ` + "There is no permissive default on purpose: introspection is a separate capability from " + "authentication, and allowing every authenticated caller lets any user resolve any other user's " + "credential to its owner. Remove resolveToken to leave the protocol unhosted entirely.");
10720
10891
  }
@@ -12000,7 +12171,7 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
12000
12171
  }
12001
12172
  attach(name, options, dataVersionSpec, implementationVersion) {
12002
12173
  if (!this.catalogs().includes(name)) {
12003
- throw new Error(`No worker handles catalog '${name}'`);
12174
+ throw new VgiError(`No worker handles catalog '${name}'`, "NOT_FOUND");
12004
12175
  }
12005
12176
  validateRequiredAttachOptions(name, this.attachOptionSpecs(name), options ?? {});
12006
12177
  const attachOpaqueData = new Uint8Array(16);
@@ -12400,11 +12571,11 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
12400
12571
  validateAtParams(atUnit, atValue);
12401
12572
  const schema = this._descriptor.schemas.find((s) => schemaPathsEqual(schemaDescriptorPath(s), schemaPath));
12402
12573
  if (!schema || !schema.tables) {
12403
- throw new Error(`Table '${name}' not found in schema ${schemaPathDisplay(schemaPath)}`);
12574
+ throw new VgiError(`Table '${name}' not found in schema ${schemaPathDisplay(schemaPath)}`, "NOT_FOUND");
12404
12575
  }
12405
12576
  const table = schema.tables.find((t) => t.name === name);
12406
12577
  if (!table) {
12407
- throw new Error(`Table '${name}' not found in schema ${schemaPathDisplay(schemaPath)}`);
12578
+ throw new VgiError(`Table '${name}' not found in schema ${schemaPathDisplay(schemaPath)}`, "NOT_FOUND");
12408
12579
  }
12409
12580
  if (atUnit && !table.supportsTimeTravel) {
12410
12581
  throw new Error(`Table ${schemaPathDisplay(schemaPath)}.${name} does not support time travel queries`);
@@ -12489,12 +12660,12 @@ function resolveIndexGroups(schema, groups) {
12489
12660
  function serializeForeignKeys(fks, schemaPath) {
12490
12661
  if (!fks || fks.length === 0)
12491
12662
  return [];
12492
- return fks.map((fk) => serializeBatch(batchFromColumns({
12663
+ return fks.map((fk) => serializeBatch(batchFromColumns(conformColumns(ForeignKeyInfoSchema, {
12493
12664
  fk_columns: [fk.columns],
12494
12665
  pk_columns: [fk.referencedColumns],
12495
12666
  referenced_table: [fk.referencedTable],
12496
12667
  referenced_schema_path: [fk.referencedSchemaPath ?? schemaPath ?? ["main"]]
12497
- }, ForeignKeyInfoSchema)));
12668
+ }), ForeignKeyInfoSchema)));
12498
12669
  }
12499
12670
  function defaultToSql(value) {
12500
12671
  if (value === null || value === undefined)
@@ -12634,7 +12805,7 @@ function serializeArgsBatch(args, argSpecSchema) {
12634
12805
  }
12635
12806
  function validateAtParams(atUnit, atValue) {
12636
12807
  if (Boolean(atUnit) !== Boolean(atValue)) {
12637
- throw new Error("at_unit and at_value must both be provided or both be absent");
12808
+ throw new VgiError("at_unit and at_value must both be provided or both be absent", "INVALID_ARGUMENT");
12638
12809
  }
12639
12810
  }
12640
12811
  function inferScalarType(val) {
@@ -12732,7 +12903,7 @@ class CompositeCatalogInterface extends CatalogInterface {
12732
12903
  return result;
12733
12904
  }
12734
12905
  }
12735
- throw new Error(`No worker handles catalog '${name}'`);
12906
+ throw new VgiError(`No worker handles catalog '${name}'`, "NOT_FOUND");
12736
12907
  }
12737
12908
  async detach(attachOpaqueData) {
12738
12909
  await this._route(attachOpaqueData).detach(attachOpaqueData);
@@ -13056,7 +13227,7 @@ class FunctionRegistry {
13056
13227
  } else if (candidates && candidates.length > 0) {
13057
13228
  const schemas = this.schemasFor(name);
13058
13229
  if (schemas.length > 0) {
13059
- throw new Error(`Function '${name}' is not registered in schema ${schemaPathDisplay(schemaPath)}. ` + `It is available in: [${schemas.map(schemaPathDisplay).join(", ")}]`);
13230
+ throw new VgiError(`Function '${name}' is not registered in schema ${schemaPathDisplay(schemaPath)}. ` + `It is available in: [${schemas.map(schemaPathDisplay).join(", ")}]`, "NOT_FOUND");
13060
13231
  }
13061
13232
  }
13062
13233
  }
@@ -13277,4 +13448,4 @@ export {
13277
13448
  utf82 as utf8
13278
13449
  };
13279
13450
 
13280
- //# debugId=D9B447159663895D64756E2164756E21
13451
+ //# debugId=FC599621AC5B851964756E2164756E21