@query-farm/vgi 0.44.0 → 0.45.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.
- package/dist/arrow/impl-arrowjs/index.d.ts +1 -1
- package/dist/arrow/impl-arrowjs/ipc.d.ts +6 -0
- package/dist/arrow/impl-flechette/build.d.ts +2 -2
- package/dist/arrow/impl-flechette/canonical.d.ts +1 -1
- package/dist/arrow/impl-flechette/index.d.ts +1 -1
- package/dist/arrow/impl-flechette/ipc.d.ts +6 -0
- package/dist/arrow/index.d.ts +1 -1
- package/dist/catalog/contents-codec.d.ts +7 -0
- package/dist/catalog/contents-digest.d.ts +2 -1
- package/dist/catalog/interface.d.ts +3 -0
- package/dist/catalog/publication-envelope.d.ts +1 -0
- package/dist/catalog/publication.d.ts +142 -0
- package/dist/catalog/s3-publication.d.ts +38 -0
- package/dist/client-entry.js +136 -25
- package/dist/client-entry.js.map +13 -12
- package/dist/codec/asd.d.ts +4 -0
- package/dist/generated/vgi-client.d.ts +6 -14
- package/dist/generated/vgi-protocol-schemas.d.ts +2 -0
- package/dist/generated/vgi-protocol-types.d.ts +26 -3
- package/dist/generated/vgi-service.d.ts +3 -3
- package/dist/index.d.ts +4 -0
- package/dist/index.js +739 -81
- package/dist/index.js.map +24 -20
- package/dist/protocol/dispatch.d.ts +1 -1
- package/dist/protocol/handlers/catalog/contents.d.ts +4 -4
- package/dist/serve-entry.js +610 -78
- package/dist/serve-entry.js.map +19 -16
- package/dist/worker-cf-entry.js +626 -86
- package/dist/worker-cf-entry.js.map +21 -18
- package/package.json +3 -3
- package/src/arrow/impl-arrowjs/index.ts +1 -0
- package/src/arrow/impl-arrowjs/ipc.ts +23 -0
- package/src/arrow/impl-flechette/build.ts +4 -2
- package/src/arrow/impl-flechette/canonical.ts +7 -2
- package/src/arrow/impl-flechette/index.ts +1 -0
- package/src/arrow/impl-flechette/ipc.ts +19 -0
- package/src/arrow/index.ts +1 -1
- package/src/catalog/contents-codec.ts +77 -0
- package/src/catalog/contents-digest.ts +20 -17
- package/src/catalog/interface.ts +3 -0
- package/src/catalog/publication-envelope.ts +62 -0
- package/src/catalog/publication.ts +290 -0
- package/src/catalog/read-only.ts +0 -2
- package/src/catalog/s3-publication.ts +81 -0
- package/src/client/client.ts +2 -19
- package/src/codec/asd.ts +40 -1
- package/src/codec/conform.ts +2 -1
- package/src/generated/vgi-client.ts +7 -16
- package/src/generated/vgi-protocol-schemas.ts +13 -6
- package/src/generated/vgi-protocol-types.ts +39 -5
- package/src/generated/vgi-service.ts +4 -3
- package/src/index.ts +5 -0
- package/src/protocol/dispatch.ts +1 -1
- package/src/protocol/handlers/catalog/admin.ts +7 -4
- package/src/protocol/handlers/catalog/contents.ts +50 -35
package/dist/worker-cf-entry.js
CHANGED
|
@@ -1729,7 +1729,26 @@ function deserializeBatch(bytes) {
|
|
|
1729
1729
|
}
|
|
1730
1730
|
return batches[0];
|
|
1731
1731
|
}
|
|
1732
|
+
function deserializeBatches(bytes) {
|
|
1733
|
+
return [...RecordBatchReader.from(bytes)];
|
|
1734
|
+
}
|
|
1735
|
+
function serializeBatches(batches, schema) {
|
|
1736
|
+
const writer = new RecordBatchStreamWriter;
|
|
1737
|
+
writer.reset(undefined, schema);
|
|
1738
|
+
for (const batch of batches)
|
|
1739
|
+
writer._writeRecordBatch(batch);
|
|
1740
|
+
writer.close();
|
|
1741
|
+
return writer.toUint8Array(true);
|
|
1742
|
+
}
|
|
1743
|
+
function serializeCollectionRows(schema, rows, maxBatchRows) {
|
|
1744
|
+
const batch = batchFromRows(rows, schema);
|
|
1745
|
+
const batches = [];
|
|
1746
|
+
for (let offset = 0;offset < batch.numRows; offset += maxBatchRows)
|
|
1747
|
+
batches.push(batch.slice(offset, offset + maxBatchRows));
|
|
1748
|
+
return serializeBatches(batches, schema);
|
|
1749
|
+
}
|
|
1732
1750
|
var init_ipc = __esm(() => {
|
|
1751
|
+
init_build();
|
|
1733
1752
|
init_empty();
|
|
1734
1753
|
});
|
|
1735
1754
|
|
|
@@ -1883,6 +1902,7 @@ __export(exports_impl_arrowjs, {
|
|
|
1883
1902
|
decodeDictValue: () => decodeDictValue,
|
|
1884
1903
|
denseUnion: () => denseUnion,
|
|
1885
1904
|
deserializeBatch: () => deserializeBatch,
|
|
1905
|
+
deserializeBatches: () => deserializeBatches,
|
|
1886
1906
|
deserializeSchema: () => deserializeSchema,
|
|
1887
1907
|
dictionary: () => dictionary,
|
|
1888
1908
|
duration: () => duration,
|
|
@@ -1912,6 +1932,8 @@ __export(exports_impl_arrowjs, {
|
|
|
1912
1932
|
safeNumber: () => safeNumber,
|
|
1913
1933
|
schema: () => schema,
|
|
1914
1934
|
serializeBatch: () => serializeBatch,
|
|
1935
|
+
serializeBatches: () => serializeBatches,
|
|
1936
|
+
serializeCollectionRows: () => serializeCollectionRows,
|
|
1915
1937
|
serializeSchema: () => serializeSchema,
|
|
1916
1938
|
sparseUnion: () => sparseUnion,
|
|
1917
1939
|
struct: () => struct,
|
|
@@ -4186,7 +4208,9 @@ function mapPairs(raw) {
|
|
|
4186
4208
|
return Object.entries(raw);
|
|
4187
4209
|
}
|
|
4188
4210
|
function zeroValue(type) {
|
|
4189
|
-
if (
|
|
4211
|
+
if (isDictionary(type))
|
|
4212
|
+
return zeroValue(type.dictionary);
|
|
4213
|
+
if (isUtf8(type))
|
|
4190
4214
|
return "";
|
|
4191
4215
|
if (isBinary(type))
|
|
4192
4216
|
return new Uint8Array(0);
|
|
@@ -4839,7 +4863,7 @@ import {
|
|
|
4839
4863
|
Protocol
|
|
4840
4864
|
} from "@query-farm/vgi-rpc";
|
|
4841
4865
|
var VGI_PROTOCOL_NAME = "vgi.v2";
|
|
4842
|
-
var VGI_PROTOCOL_VERSION = "2.
|
|
4866
|
+
var VGI_PROTOCOL_VERSION = "2.2.0";
|
|
4843
4867
|
function exchangeStream(callbacks) {
|
|
4844
4868
|
return { kind: "exchange", ...callbacks };
|
|
4845
4869
|
}
|
|
@@ -6098,6 +6122,7 @@ function serializeColumnStatistics(stats, _cacheMaxAgeSeconds) {
|
|
|
6098
6122
|
|
|
6099
6123
|
// src/generated/vgi-protocol-schemas.ts
|
|
6100
6124
|
init_arrow();
|
|
6125
|
+
var VGI_CATALOG_CONTRACT_HASH = "121e8fe1c074bce00a6d0fcec49d7a90f05e45498cd9ca8a5a3b4a9ae2377a2d";
|
|
6101
6126
|
var CatalogInfoSchema = schema2([
|
|
6102
6127
|
field2("name", utf82(), false),
|
|
6103
6128
|
field2("implementation_version", utf82(), true),
|
|
@@ -6109,7 +6134,6 @@ var CatalogInfoSchema = schema2([
|
|
|
6109
6134
|
var SchemaInfoSchema = schema2([
|
|
6110
6135
|
field2("comment", utf82(), true),
|
|
6111
6136
|
field2("tags", map2(field2("key", utf82(), false), field2("value", utf82(), true), false), false),
|
|
6112
|
-
field2("attach_opaque_data", binary2(), false),
|
|
6113
6137
|
field2("path", list2(field2("item", utf82(), true)), false),
|
|
6114
6138
|
field2("estimated_object_count", map2(field2("key", utf82(), false), field2("value", int642(), true), false), true)
|
|
6115
6139
|
]);
|
|
@@ -6150,9 +6174,9 @@ var FunctionInfoSchema = schema2([
|
|
|
6150
6174
|
field2("name", utf82(), false),
|
|
6151
6175
|
field2("schema_path", list2(field2("item", utf82(), true)), false),
|
|
6152
6176
|
field2("function_type", dictionary2(utf82(), int162()), false),
|
|
6153
|
-
field2("arguments", binary2(), false),
|
|
6154
|
-
field2("output_schema", binary2(), false),
|
|
6155
|
-
field2("parameter_default_values", binary2(), true),
|
|
6177
|
+
field2("arguments", dictionary2(binary2(), int322()), false),
|
|
6178
|
+
field2("output_schema", dictionary2(binary2(), int322()), false),
|
|
6179
|
+
field2("parameter_default_values", dictionary2(binary2(), int322()), true),
|
|
6156
6180
|
field2("stability", dictionary2(utf82(), int162()), true),
|
|
6157
6181
|
field2("null_handling", dictionary2(utf82(), int162()), true),
|
|
6158
6182
|
field2("argument_monotonicity", list2(field2("item", utf82(), true)), true),
|
|
@@ -6613,7 +6637,12 @@ var CatalogContentsResultSchema = schema2([
|
|
|
6613
6637
|
field2("catalog_version", int642(), false),
|
|
6614
6638
|
field2("etag", utf82(), true),
|
|
6615
6639
|
field2("not_modified", bool2(), false),
|
|
6616
|
-
field2("schemas",
|
|
6640
|
+
field2("schemas", binary2(), true),
|
|
6641
|
+
field2("tables", binary2(), true),
|
|
6642
|
+
field2("views", binary2(), true),
|
|
6643
|
+
field2("functions", binary2(), true),
|
|
6644
|
+
field2("macros", binary2(), true),
|
|
6645
|
+
field2("indexes", binary2(), true)
|
|
6617
6646
|
]);
|
|
6618
6647
|
var CatalogCopyFromFormatsResultSchema = schema2([
|
|
6619
6648
|
field2("items", list2(field2("item", binary2(), true)), false)
|
|
@@ -9351,6 +9380,109 @@ function sortMapsForSchema(schema, obj) {
|
|
|
9351
9380
|
}
|
|
9352
9381
|
return out ?? obj;
|
|
9353
9382
|
}
|
|
9383
|
+
function normalizeValue(raw, type) {
|
|
9384
|
+
if (raw == null)
|
|
9385
|
+
return null;
|
|
9386
|
+
if (isList(type)) {
|
|
9387
|
+
const childType = type.children[0].type;
|
|
9388
|
+
const out = [];
|
|
9389
|
+
for (const item of raw)
|
|
9390
|
+
out.push(normalizeValue(item, childType));
|
|
9391
|
+
return out;
|
|
9392
|
+
}
|
|
9393
|
+
if (isMap(type)) {
|
|
9394
|
+
const entries = type.children[0].type;
|
|
9395
|
+
const valueType = entries.children[1].type;
|
|
9396
|
+
const out = {};
|
|
9397
|
+
const put = (k, v) => {
|
|
9398
|
+
out[String(k)] = normalizeValue(v, valueType);
|
|
9399
|
+
};
|
|
9400
|
+
if (raw[Symbol.iterator]) {
|
|
9401
|
+
for (const entry of raw) {
|
|
9402
|
+
if (Array.isArray(entry)) {
|
|
9403
|
+
put(entry[0], entry[1]);
|
|
9404
|
+
} else if (entry && typeof entry === "object") {
|
|
9405
|
+
put(entry.key ?? entry[0], entry.value ?? entry[1]);
|
|
9406
|
+
}
|
|
9407
|
+
}
|
|
9408
|
+
} else if (typeof raw === "object") {
|
|
9409
|
+
for (const [k, v] of Object.entries(raw))
|
|
9410
|
+
put(k, v);
|
|
9411
|
+
}
|
|
9412
|
+
return out;
|
|
9413
|
+
}
|
|
9414
|
+
if (isStruct(type)) {
|
|
9415
|
+
const childFields = type.children;
|
|
9416
|
+
const out = {};
|
|
9417
|
+
for (const cf of childFields) {
|
|
9418
|
+
out[cf.name] = normalizeValue(raw[cf.name], cf.type);
|
|
9419
|
+
}
|
|
9420
|
+
return out;
|
|
9421
|
+
}
|
|
9422
|
+
if (isDictionary(type)) {
|
|
9423
|
+
return normalizeValue(raw, type.dictionary);
|
|
9424
|
+
}
|
|
9425
|
+
if (isBinary(type)) {
|
|
9426
|
+
return toUint8Array3(raw);
|
|
9427
|
+
}
|
|
9428
|
+
if (isInt(type) && type.bitWidth === 64) {
|
|
9429
|
+
if (typeof raw === "bigint") {
|
|
9430
|
+
if (raw >= BigInt(Number.MIN_SAFE_INTEGER) && raw <= BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
9431
|
+
return Number(raw);
|
|
9432
|
+
}
|
|
9433
|
+
return raw;
|
|
9434
|
+
}
|
|
9435
|
+
return raw;
|
|
9436
|
+
}
|
|
9437
|
+
if (isBool(type)) {
|
|
9438
|
+
return Boolean(raw);
|
|
9439
|
+
}
|
|
9440
|
+
return raw;
|
|
9441
|
+
}
|
|
9442
|
+
function toUint8Array3(val) {
|
|
9443
|
+
if (val instanceof Uint8Array)
|
|
9444
|
+
return val;
|
|
9445
|
+
if (val instanceof ArrayBuffer)
|
|
9446
|
+
return new Uint8Array(val);
|
|
9447
|
+
if (val && val.buffer instanceof ArrayBuffer) {
|
|
9448
|
+
return new Uint8Array(val.buffer, val.byteOffset, val.byteLength);
|
|
9449
|
+
}
|
|
9450
|
+
return new Uint8Array(0);
|
|
9451
|
+
}
|
|
9452
|
+
function encodeASDCollection(schema, records) {
|
|
9453
|
+
if (records.length === 0)
|
|
9454
|
+
return null;
|
|
9455
|
+
return serializeCollectionRows(schema, records.map((record) => sortMapsForSchema(schema, conformRecord(schema, record))), 1024);
|
|
9456
|
+
}
|
|
9457
|
+
function decodeASDCollection(schema, bytes) {
|
|
9458
|
+
if (bytes == null)
|
|
9459
|
+
return [];
|
|
9460
|
+
const output = [];
|
|
9461
|
+
const batches = deserializeBatches(bytes);
|
|
9462
|
+
const validateSchema = (actualSchema) => {
|
|
9463
|
+
if (actualSchema.fields.length !== schema.fields.length || schema.fields.some((field, i) => {
|
|
9464
|
+
const actual = actualSchema.fields[i];
|
|
9465
|
+
return actual.name !== field.name || actual.nullable !== field.nullable || typeSignature(actual.type) !== typeSignature(field.type);
|
|
9466
|
+
}))
|
|
9467
|
+
throw new Error("catalog collection has an incompatible metadata schema");
|
|
9468
|
+
};
|
|
9469
|
+
if (batches.length === 0)
|
|
9470
|
+
validateSchema(deserializeSchema(bytes));
|
|
9471
|
+
for (const batch of batches) {
|
|
9472
|
+
validateSchema(batch.schema);
|
|
9473
|
+
for (let row = 0;row < batch.numRows; row++) {
|
|
9474
|
+
const item = {};
|
|
9475
|
+
for (const field of schema.fields) {
|
|
9476
|
+
const value = batch.getChild(field.name)?.get(row);
|
|
9477
|
+
if (value == null && !field.nullable)
|
|
9478
|
+
throw new Error(`catalog collection has null ${field.name}`);
|
|
9479
|
+
item[field.name] = normalizeValue(value, field.type);
|
|
9480
|
+
}
|
|
9481
|
+
output.push(item);
|
|
9482
|
+
}
|
|
9483
|
+
}
|
|
9484
|
+
return output;
|
|
9485
|
+
}
|
|
9354
9486
|
|
|
9355
9487
|
// src/generated/vgi-client.ts
|
|
9356
9488
|
var encodeCatalogInfo = (v) => encodeASD(CatalogInfoSchema, v);
|
|
@@ -9384,68 +9516,111 @@ function buildCatalogAttachResult(init) {
|
|
|
9384
9516
|
supports_catalog_contents: init.supports_catalog_contents ?? false
|
|
9385
9517
|
};
|
|
9386
9518
|
}
|
|
9387
|
-
function buildSchemaContents(init) {
|
|
9388
|
-
return {
|
|
9389
|
-
path: init.path,
|
|
9390
|
-
schema: init.schema,
|
|
9391
|
-
tables: init.tables ?? [],
|
|
9392
|
-
views: init.views ?? [],
|
|
9393
|
-
scalar_functions: init.scalar_functions ?? [],
|
|
9394
|
-
aggregate_functions: init.aggregate_functions ?? [],
|
|
9395
|
-
table_functions: init.table_functions ?? [],
|
|
9396
|
-
scalar_macros: init.scalar_macros ?? [],
|
|
9397
|
-
table_macros: init.table_macros ?? [],
|
|
9398
|
-
indexes: init.indexes ?? []
|
|
9399
|
-
};
|
|
9400
|
-
}
|
|
9401
9519
|
function buildCatalogContentsResponse(init) {
|
|
9402
9520
|
return {
|
|
9403
9521
|
catalog_version: init.catalog_version,
|
|
9404
9522
|
etag: init.etag ?? null,
|
|
9405
9523
|
not_modified: init.not_modified ?? false,
|
|
9406
|
-
schemas: init.schemas ??
|
|
9524
|
+
schemas: init.schemas ?? null,
|
|
9525
|
+
tables: init.tables ?? null,
|
|
9526
|
+
views: init.views ?? null,
|
|
9527
|
+
functions: init.functions ?? null,
|
|
9528
|
+
macros: init.macros ?? null,
|
|
9529
|
+
indexes: init.indexes ?? null
|
|
9407
9530
|
};
|
|
9408
9531
|
}
|
|
9409
9532
|
|
|
9410
|
-
// src/catalog/
|
|
9411
|
-
|
|
9412
|
-
|
|
9413
|
-
|
|
9414
|
-
|
|
9415
|
-
}
|
|
9416
|
-
|
|
9417
|
-
|
|
9418
|
-
|
|
9419
|
-
|
|
9420
|
-
|
|
9421
|
-
|
|
9422
|
-
|
|
9423
|
-
|
|
9424
|
-
|
|
9533
|
+
// src/protocol/handlers/catalog/contents.ts
|
|
9534
|
+
import { ExternalRef as ExternalRef2 } from "@query-farm/vgi-rpc";
|
|
9535
|
+
|
|
9536
|
+
// src/catalog/publication.ts
|
|
9537
|
+
init_arrow();
|
|
9538
|
+
import { ExternalRef, publishExternalResult, resolveExternalLocation } from "@query-farm/vgi-rpc";
|
|
9539
|
+
|
|
9540
|
+
// src/catalog/contents-codec.ts
|
|
9541
|
+
var schemaPathKey2 = (path) => JSON.stringify(path.map((part) => part.toLowerCase()));
|
|
9542
|
+
var CATALOG_STREAM_KINDS = ["schemas", "tables", "views", "functions", "macros", "indexes"];
|
|
9543
|
+
function encodeCatalogStreams(inventory) {
|
|
9544
|
+
const streams = {
|
|
9545
|
+
schemas: encodeASDCollection(SchemaInfoSchema, inventory.map((s) => s.schema)),
|
|
9546
|
+
tables: encodeASDCollection(TableInfoSchema, inventory.flatMap((s) => s.tables)),
|
|
9547
|
+
views: encodeASDCollection(ViewInfoSchema, inventory.flatMap((s) => s.views)),
|
|
9548
|
+
functions: encodeASDCollection(FunctionInfoSchema, inventory.flatMap((s) => [...s.scalar_functions, ...s.aggregate_functions, ...s.table_functions])),
|
|
9549
|
+
macros: encodeASDCollection(MacroInfoSchema, inventory.flatMap((s) => [...s.scalar_macros, ...s.table_macros])),
|
|
9550
|
+
indexes: encodeASDCollection(IndexInfoSchema, inventory.flatMap((s) => s.indexes))
|
|
9551
|
+
};
|
|
9552
|
+
return streams;
|
|
9425
9553
|
}
|
|
9426
|
-
function
|
|
9427
|
-
if (
|
|
9428
|
-
|
|
9429
|
-
|
|
9430
|
-
|
|
9431
|
-
|
|
9432
|
-
ENCODED.set(info, encoded);
|
|
9554
|
+
function decodeCatalogStreams(response, ifNoneMatch) {
|
|
9555
|
+
if (response.not_modified) {
|
|
9556
|
+
if (response.etag == null || ifNoneMatch !== undefined && response.etag !== ifNoneMatch || CATALOG_STREAM_KINDS.some((kind) => response[kind] != null)) {
|
|
9557
|
+
throw new Error("invalid catalog_contents not_modified response");
|
|
9558
|
+
}
|
|
9559
|
+
return [];
|
|
9433
9560
|
}
|
|
9434
|
-
|
|
9561
|
+
const infos = decodeASDCollection(SchemaInfoSchema, response.schemas);
|
|
9562
|
+
const schemas = [];
|
|
9563
|
+
const index = new Map;
|
|
9564
|
+
for (const schema of infos) {
|
|
9565
|
+
if (!Array.isArray(schema.path) || schema.path.length === 0)
|
|
9566
|
+
throw new Error("catalog schema has an empty path");
|
|
9567
|
+
const key = schemaPathKey2(schema.path);
|
|
9568
|
+
if (index.has(key))
|
|
9569
|
+
throw new Error("catalog contains duplicate schema paths");
|
|
9570
|
+
if (schema.path.length > 1 && !index.has(schemaPathKey2(schema.path.slice(0, -1)))) {
|
|
9571
|
+
throw new Error("catalog schema is missing its preceding parent");
|
|
9572
|
+
}
|
|
9573
|
+
const entry = { schema, tables: [], views: [], scalar_functions: [], aggregate_functions: [], table_functions: [], scalar_macros: [], table_macros: [], indexes: [] };
|
|
9574
|
+
index.set(key, entry);
|
|
9575
|
+
schemas.push(entry);
|
|
9576
|
+
}
|
|
9577
|
+
const owner = (item) => {
|
|
9578
|
+
const entry = index.get(schemaPathKey2(item.schema_path));
|
|
9579
|
+
if (!entry)
|
|
9580
|
+
throw new Error("catalog object references an absent schema");
|
|
9581
|
+
return entry;
|
|
9582
|
+
};
|
|
9583
|
+
for (const item of decodeASDCollection(TableInfoSchema, response.tables))
|
|
9584
|
+
owner(item).tables.push(item);
|
|
9585
|
+
for (const item of decodeASDCollection(ViewInfoSchema, response.views))
|
|
9586
|
+
owner(item).views.push(item);
|
|
9587
|
+
for (const item of decodeASDCollection(FunctionInfoSchema, response.functions)) {
|
|
9588
|
+
const entry = owner(item);
|
|
9589
|
+
switch (String(item.function_type).toUpperCase()) {
|
|
9590
|
+
case "SCALAR":
|
|
9591
|
+
entry.scalar_functions.push(item);
|
|
9592
|
+
break;
|
|
9593
|
+
case "AGGREGATE":
|
|
9594
|
+
entry.aggregate_functions.push(item);
|
|
9595
|
+
break;
|
|
9596
|
+
case "TABLE":
|
|
9597
|
+
case "TABLE_BUFFERING":
|
|
9598
|
+
entry.table_functions.push(item);
|
|
9599
|
+
break;
|
|
9600
|
+
default:
|
|
9601
|
+
throw new Error(`unknown catalog function type ${item.function_type}`);
|
|
9602
|
+
}
|
|
9603
|
+
}
|
|
9604
|
+
for (const item of decodeASDCollection(MacroInfoSchema, response.macros)) {
|
|
9605
|
+
const entry = owner(item);
|
|
9606
|
+
switch (String(item.macro_type).toUpperCase()) {
|
|
9607
|
+
case "SCALAR":
|
|
9608
|
+
entry.scalar_macros.push(item);
|
|
9609
|
+
break;
|
|
9610
|
+
case "TABLE":
|
|
9611
|
+
entry.table_macros.push(item);
|
|
9612
|
+
break;
|
|
9613
|
+
default:
|
|
9614
|
+
throw new Error(`unknown catalog macro type ${item.macro_type}`);
|
|
9615
|
+
}
|
|
9616
|
+
}
|
|
9617
|
+
for (const item of decodeASDCollection(IndexInfoSchema, response.indexes))
|
|
9618
|
+
owner(item).indexes.push(item);
|
|
9619
|
+
return schemas;
|
|
9435
9620
|
}
|
|
9436
9621
|
|
|
9437
9622
|
// src/catalog/contents-digest.ts
|
|
9438
|
-
|
|
9439
|
-
"tables",
|
|
9440
|
-
"views",
|
|
9441
|
-
"scalar_functions",
|
|
9442
|
-
"aggregate_functions",
|
|
9443
|
-
"table_functions",
|
|
9444
|
-
"scalar_macros",
|
|
9445
|
-
"table_macros",
|
|
9446
|
-
"indexes"
|
|
9447
|
-
];
|
|
9448
|
-
async function catalogContentsDigest(schemas) {
|
|
9623
|
+
async function catalogContentsDigest(streams, version = 0) {
|
|
9449
9624
|
const utf8 = new TextEncoder;
|
|
9450
9625
|
const parts = [];
|
|
9451
9626
|
let total = 0;
|
|
@@ -9465,12 +9640,23 @@ async function catalogContentsDigest(schemas) {
|
|
|
9465
9640
|
for (const v of values)
|
|
9466
9641
|
chunk(v);
|
|
9467
9642
|
};
|
|
9468
|
-
|
|
9469
|
-
|
|
9470
|
-
|
|
9471
|
-
|
|
9472
|
-
|
|
9473
|
-
|
|
9643
|
+
if (Array.isArray(streams)) {
|
|
9644
|
+
count(streams.length);
|
|
9645
|
+
for (const entry of streams) {
|
|
9646
|
+
chunks(entry.path.map((p) => utf8.encode(p)));
|
|
9647
|
+
chunk(entry.schema);
|
|
9648
|
+
for (const kind of ["tables", "views", "scalar_functions", "aggregate_functions", "table_functions", "scalar_macros", "table_macros", "indexes"])
|
|
9649
|
+
chunks(entry[kind]);
|
|
9650
|
+
}
|
|
9651
|
+
} else {
|
|
9652
|
+
chunk(utf8.encode("vgi.catalog-contents.2.2"));
|
|
9653
|
+
count(version);
|
|
9654
|
+
for (const kind of CATALOG_STREAM_KINDS) {
|
|
9655
|
+
const value = streams[kind];
|
|
9656
|
+
count(value == null ? 0 : 1);
|
|
9657
|
+
if (value != null)
|
|
9658
|
+
chunk(value);
|
|
9659
|
+
}
|
|
9474
9660
|
}
|
|
9475
9661
|
const buf = new Uint8Array(total);
|
|
9476
9662
|
let off = 0;
|
|
@@ -9485,7 +9671,297 @@ async function catalogContentsDigest(schemas) {
|
|
|
9485
9671
|
return hex;
|
|
9486
9672
|
}
|
|
9487
9673
|
|
|
9674
|
+
// src/catalog/publication-envelope.ts
|
|
9675
|
+
function validateCatalogPublicationEnvelope(bytes) {
|
|
9676
|
+
try {
|
|
9677
|
+
const stream = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
9678
|
+
let position = 0, messages = 0;
|
|
9679
|
+
while (position < bytes.length) {
|
|
9680
|
+
let length = stream.getInt32(position, true);
|
|
9681
|
+
position += 4;
|
|
9682
|
+
if (length === -1) {
|
|
9683
|
+
length = stream.getInt32(position, true);
|
|
9684
|
+
position += 4;
|
|
9685
|
+
}
|
|
9686
|
+
if (length === 0) {
|
|
9687
|
+
if (position !== bytes.length)
|
|
9688
|
+
throw new Error("trailing data");
|
|
9689
|
+
break;
|
|
9690
|
+
}
|
|
9691
|
+
if (length < 4 || length > bytes.length - position)
|
|
9692
|
+
throw new Error("invalid message length");
|
|
9693
|
+
const head = new DataView(bytes.buffer, bytes.byteOffset + position, length);
|
|
9694
|
+
const tableField = (table, field) => {
|
|
9695
|
+
const vtable = table - head.getInt32(table, true);
|
|
9696
|
+
const vlength = head.getUint16(vtable, true);
|
|
9697
|
+
if (vlength < 4 || vlength % 2 || vtable + vlength > length)
|
|
9698
|
+
throw new Error("invalid vtable");
|
|
9699
|
+
if (4 + 2 * field >= vlength)
|
|
9700
|
+
return null;
|
|
9701
|
+
const offset = head.getUint16(vtable + 4 + 2 * field, true);
|
|
9702
|
+
return offset === 0 ? null : table + offset;
|
|
9703
|
+
};
|
|
9704
|
+
const follow = (pointer) => pointer + head.getUint32(pointer, true);
|
|
9705
|
+
const root = head.getUint32(0, true);
|
|
9706
|
+
const typePosition = tableField(root, 1);
|
|
9707
|
+
const type = typePosition == null ? 0 : head.getUint8(typePosition);
|
|
9708
|
+
if (type !== (messages === 0 ? 1 : messages === 1 ? 3 : -1))
|
|
9709
|
+
throw new Error("expected one schema and one record batch");
|
|
9710
|
+
if (type === 3) {
|
|
9711
|
+
const header = tableField(root, 2);
|
|
9712
|
+
if (header == null)
|
|
9713
|
+
throw new Error("missing record header");
|
|
9714
|
+
const rows = tableField(follow(header), 0);
|
|
9715
|
+
if (rows == null || head.getBigInt64(rows, true) !== 1n)
|
|
9716
|
+
throw new Error("expected one response row");
|
|
9717
|
+
}
|
|
9718
|
+
const metadata = tableField(root, 4);
|
|
9719
|
+
if (metadata != null) {
|
|
9720
|
+
const vector = follow(metadata), count = head.getUint32(vector, true);
|
|
9721
|
+
if (count > length / 4)
|
|
9722
|
+
throw new Error("invalid metadata vector");
|
|
9723
|
+
for (let i = 0;i < count; i++) {
|
|
9724
|
+
const keyPointer = tableField(follow(vector + 4 + 4 * i), 0);
|
|
9725
|
+
if (keyPointer == null)
|
|
9726
|
+
continue;
|
|
9727
|
+
const keyStart = follow(keyPointer), keyLength = head.getUint32(keyStart, true);
|
|
9728
|
+
if (keyStart + 4 + keyLength > length)
|
|
9729
|
+
throw new Error("invalid metadata key");
|
|
9730
|
+
const key = new TextDecoder().decode(new Uint8Array(head.buffer, head.byteOffset + keyStart + 4, keyLength));
|
|
9731
|
+
if (key === "vgi_rpc.location" || key === "vgi_rpc.log_level")
|
|
9732
|
+
throw new Error("pointer/log metadata is not a catalog result");
|
|
9733
|
+
}
|
|
9734
|
+
}
|
|
9735
|
+
const bodyPosition = tableField(root, 3);
|
|
9736
|
+
const bodyLength = bodyPosition == null ? 0 : Number(head.getBigInt64(bodyPosition, true));
|
|
9737
|
+
position += length;
|
|
9738
|
+
if (!Number.isSafeInteger(bodyLength) || bodyLength < 0 || bodyLength > bytes.length - position)
|
|
9739
|
+
throw new Error("invalid body length");
|
|
9740
|
+
position += bodyLength;
|
|
9741
|
+
messages++;
|
|
9742
|
+
}
|
|
9743
|
+
if (messages !== 2)
|
|
9744
|
+
throw new Error("missing response batch");
|
|
9745
|
+
} catch (error) {
|
|
9746
|
+
throw new Error(`invalid published RPC envelope: ${error instanceof Error ? error.message : String(error)}`);
|
|
9747
|
+
}
|
|
9748
|
+
}
|
|
9749
|
+
|
|
9750
|
+
// src/catalog/publication.ts
|
|
9751
|
+
var OUTER_SCHEMA = schema2([field2("result", binary2(), false)]);
|
|
9752
|
+
var DEFAULT_MAX = 64 * 1024 * 1024;
|
|
9753
|
+
var utf83 = new TextEncoder;
|
|
9754
|
+
var equalBytes3 = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
9755
|
+
async function digest(value) {
|
|
9756
|
+
const bytes = new Uint8Array(await crypto.subtle.digest("SHA-256", utf83.encode(value)));
|
|
9757
|
+
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
9758
|
+
}
|
|
9759
|
+
function assertRenewedPublication(before, after) {
|
|
9760
|
+
const unchanged = (m) => JSON.stringify([m.key, m.formatVersion, m.protocolName, m.protocolVersion, m.contractHash, m.codecRevision, m.catalogVersion, m.etag, m.sha256, m.rawSize, m.storedSize, m.objectId, m.retainUntil, m.stable, m.validated]);
|
|
9761
|
+
if (unchanged(before) !== unchanged(after))
|
|
9762
|
+
throw new Error("renewal changed immutable catalog identity");
|
|
9763
|
+
}
|
|
9764
|
+
var manifestSame = (a, b) => a.manifestId === b.manifestId;
|
|
9765
|
+
class CatalogSnapshotManager {
|
|
9766
|
+
policy;
|
|
9767
|
+
cached = new Map;
|
|
9768
|
+
pending = new Map;
|
|
9769
|
+
constructor(policy) {
|
|
9770
|
+
this.policy = policy;
|
|
9771
|
+
for (const value of [policy.maxRawBytes, policy.maxStoredBytes, policy.maxCachedManifests]) {
|
|
9772
|
+
if (value != null && (!Number.isSafeInteger(value) || value <= 0))
|
|
9773
|
+
throw new Error("catalog publication limits must be positive safe integers");
|
|
9774
|
+
}
|
|
9775
|
+
if (policy.referenceGraceMs != null && (!Number.isSafeInteger(policy.referenceGraceMs) || policy.referenceGraceMs < 0))
|
|
9776
|
+
throw new Error("catalog publication reference grace must be a nonnegative safe integer");
|
|
9777
|
+
}
|
|
9778
|
+
usable(m) {
|
|
9779
|
+
const deadline = Date.now() + (this.policy.referenceGraceMs ?? 60000);
|
|
9780
|
+
return (m.stable || m.expiresAt != null) && (m.expiresAt == null || m.expiresAt > deadline) && (m.retainUntil == null || m.retainUntil > deadline);
|
|
9781
|
+
}
|
|
9782
|
+
async key(identity) {
|
|
9783
|
+
for (const value of [identity.catalogNamespace, identity.sourceRevision, identity.visibilityScope])
|
|
9784
|
+
if (!value)
|
|
9785
|
+
throw new Error("published catalogs require explicit namespace, revision and visibility scope");
|
|
9786
|
+
if (!Number.isSafeInteger(identity.catalogVersion))
|
|
9787
|
+
throw new Error("catalog version is not a safe integer");
|
|
9788
|
+
return digest(JSON.stringify([
|
|
9789
|
+
VGI_PROTOCOL_NAME,
|
|
9790
|
+
VGI_PROTOCOL_VERSION,
|
|
9791
|
+
VGI_CATALOG_CONTRACT_HASH,
|
|
9792
|
+
1,
|
|
9793
|
+
identity.catalogNamespace,
|
|
9794
|
+
identity.sourceRevision,
|
|
9795
|
+
identity.visibilityScope,
|
|
9796
|
+
identity.catalogVersion,
|
|
9797
|
+
this.policy.repository.profile,
|
|
9798
|
+
this.policy.compression ?? "identity"
|
|
9799
|
+
]));
|
|
9800
|
+
}
|
|
9801
|
+
remember(m) {
|
|
9802
|
+
this.cached.delete(m.key);
|
|
9803
|
+
this.cached.set(m.key, Object.freeze({ ...m }));
|
|
9804
|
+
while (this.cached.size > (this.policy.maxCachedManifests ?? 128))
|
|
9805
|
+
this.cached.delete(this.cached.keys().next().value);
|
|
9806
|
+
}
|
|
9807
|
+
async invalidate(key) {
|
|
9808
|
+
await this.pending.get(key)?.catch(() => {
|
|
9809
|
+
return;
|
|
9810
|
+
});
|
|
9811
|
+
const m = this.cached.get(key) ?? await this.policy.repository.lookup(key);
|
|
9812
|
+
this.cached.delete(key);
|
|
9813
|
+
if (m)
|
|
9814
|
+
await this.policy.repository.invalidate(key, m);
|
|
9815
|
+
}
|
|
9816
|
+
async validate(m, key, identity, expected) {
|
|
9817
|
+
if (m.formatVersion !== 1 || typeof m.manifestId !== "string" || !m.manifestId || m.key !== key || m.protocolName !== VGI_PROTOCOL_NAME || m.protocolVersion !== VGI_PROTOCOL_VERSION || m.contractHash !== VGI_CATALOG_CONTRACT_HASH || m.codecRevision !== 1 || m.catalogVersion !== identity.catalogVersion || m.validated !== true || typeof m.stable !== "boolean" || typeof m.etag !== "string" || !/^[0-9a-f]{64}$/.test(m.sha256) || !Number.isSafeInteger(m.rawSize) || !Number.isSafeInteger(m.storedSize) || m.rawSize <= 0 || m.storedSize <= 0 || m.rawSize > (this.policy.maxRawBytes ?? DEFAULT_MAX) || m.storedSize > (this.policy.maxStoredBytes ?? DEFAULT_MAX) || !this.usable(m))
|
|
9818
|
+
throw new Error("invalid or expired catalog publication manifest");
|
|
9819
|
+
const ref = new ExternalRef(m.url, m.sha256);
|
|
9820
|
+
const configuredFetch = this.policy.repository.resolver.fetch ?? fetch;
|
|
9821
|
+
const resolved = await resolveExternalLocation(ref.pointerBatch(OUTER_SCHEMA), {
|
|
9822
|
+
...this.policy.repository.resolver,
|
|
9823
|
+
fetch: (input, init) => configuredFetch(input, { ...init, signal: AbortSignal.any([...init?.signal ? [init.signal] : [], AbortSignal.timeout(30000)]) }),
|
|
9824
|
+
maxFetchBytes: Math.min(this.policy.maxStoredBytes ?? DEFAULT_MAX, this.policy.repository.resolver.maxFetchBytes ?? Infinity),
|
|
9825
|
+
maxDecompressedBytes: Math.min(this.policy.maxRawBytes ?? DEFAULT_MAX, this.policy.repository.resolver.maxDecompressedBytes ?? Infinity),
|
|
9826
|
+
validateFetchedIpc: (bytes) => {
|
|
9827
|
+
this.policy.repository.resolver.validateFetchedIpc?.(bytes);
|
|
9828
|
+
if (bytes.length !== m.rawSize)
|
|
9829
|
+
throw new Error("catalog publication raw size mismatch");
|
|
9830
|
+
validateCatalogPublicationEnvelope(bytes);
|
|
9831
|
+
}
|
|
9832
|
+
});
|
|
9833
|
+
if (resolved.numRows !== 1 || resolved.schema.fields.length !== 1 || resolved.schema.fields[0].name !== "result" || typeSignature(resolved.schema.fields[0].type) !== typeSignature(binary2()) || resolved.schema.fields[0].nullable)
|
|
9834
|
+
throw new Error("invalid published RPC envelope");
|
|
9835
|
+
const bytes = resolved.getChild("result")?.get(0);
|
|
9836
|
+
if (!(bytes instanceof Uint8Array))
|
|
9837
|
+
throw new Error("published catalog has no result bytes");
|
|
9838
|
+
const decoded = decodeASDCollection(CatalogContentsResultSchema, bytes);
|
|
9839
|
+
if (decoded.length !== 1)
|
|
9840
|
+
throw new Error("published catalog must contain exactly one response row");
|
|
9841
|
+
const response = decoded[0];
|
|
9842
|
+
if (response.not_modified || Number(response.catalog_version) !== identity.catalogVersion || response.etag !== m.etag)
|
|
9843
|
+
throw new Error("published catalog version or ETag mismatch");
|
|
9844
|
+
decodeCatalogStreams(response);
|
|
9845
|
+
if (expected)
|
|
9846
|
+
for (const kind of CATALOG_STREAM_KINDS) {
|
|
9847
|
+
const a = response[kind], b = expected[kind];
|
|
9848
|
+
if (a == null !== (b == null) || a != null && b != null && !equalBytes3(a, b))
|
|
9849
|
+
throw new Error("publication readback changed catalog metadata");
|
|
9850
|
+
}
|
|
9851
|
+
}
|
|
9852
|
+
async import(manifest, identity) {
|
|
9853
|
+
const key = await this.key(identity);
|
|
9854
|
+
await this.validate(manifest, key, identity);
|
|
9855
|
+
const winner = await this.policy.repository.commitIfAbsent(manifest);
|
|
9856
|
+
if (!manifestSame(manifest, winner))
|
|
9857
|
+
await this.validate(winner, key, identity);
|
|
9858
|
+
this.remember(winner);
|
|
9859
|
+
return winner;
|
|
9860
|
+
}
|
|
9861
|
+
async warm(identity, build) {
|
|
9862
|
+
const key = await this.key(identity);
|
|
9863
|
+
const cached = this.cached.get(key);
|
|
9864
|
+
if (cached && this.usable(cached))
|
|
9865
|
+
return cached;
|
|
9866
|
+
const active = this.pending.get(key);
|
|
9867
|
+
if (active)
|
|
9868
|
+
return active;
|
|
9869
|
+
const pending = this.recoverOrPublish(key, identity, build);
|
|
9870
|
+
this.pending.set(key, pending);
|
|
9871
|
+
try {
|
|
9872
|
+
const m = await pending;
|
|
9873
|
+
this.remember(m);
|
|
9874
|
+
return m;
|
|
9875
|
+
} finally {
|
|
9876
|
+
if (this.pending.get(key) === pending)
|
|
9877
|
+
this.pending.delete(key);
|
|
9878
|
+
}
|
|
9879
|
+
}
|
|
9880
|
+
async recoverOrPublish(key, identity, build) {
|
|
9881
|
+
const repository = this.policy.repository;
|
|
9882
|
+
let existing = await repository.lookup(key);
|
|
9883
|
+
if (existing) {
|
|
9884
|
+
try {
|
|
9885
|
+
if (!this.usable(existing) && repository.renew) {
|
|
9886
|
+
const renewed = await repository.renew(existing);
|
|
9887
|
+
if (renewed) {
|
|
9888
|
+
assertRenewedPublication(existing, renewed);
|
|
9889
|
+
existing = renewed;
|
|
9890
|
+
}
|
|
9891
|
+
}
|
|
9892
|
+
await this.validate(existing, key, identity);
|
|
9893
|
+
return existing;
|
|
9894
|
+
} catch {
|
|
9895
|
+
await repository.invalidate(key, existing);
|
|
9896
|
+
}
|
|
9897
|
+
}
|
|
9898
|
+
const response = await build();
|
|
9899
|
+
if (response.not_modified || Number(response.catalog_version) !== identity.catalogVersion)
|
|
9900
|
+
throw new Error("publication requires a full snapshot of the requested version");
|
|
9901
|
+
decodeCatalogStreams(response);
|
|
9902
|
+
response.etag ??= await catalogContentsDigest(response, identity.catalogVersion);
|
|
9903
|
+
const payload = encodeASD(CatalogContentsResultSchema, response);
|
|
9904
|
+
let rawSize = 0, storedSize = 0;
|
|
9905
|
+
const bounded = { upload: async (data, encoding) => {
|
|
9906
|
+
if (encoding)
|
|
9907
|
+
throw new Error("catalog publication uploader requires identity encoding");
|
|
9908
|
+
rawSize = data.byteLength;
|
|
9909
|
+
if (rawSize > (this.policy.maxRawBytes ?? DEFAULT_MAX))
|
|
9910
|
+
throw new Error("catalog publication exceeds byte limit");
|
|
9911
|
+
let body = data;
|
|
9912
|
+
const compression = this.policy.compression ?? "identity";
|
|
9913
|
+
if (compression !== "identity" && compression !== "gzip" && compression !== "zstd")
|
|
9914
|
+
throw new Error("catalog publication compression must be identity, gzip or zstd");
|
|
9915
|
+
if (compression === "gzip") {
|
|
9916
|
+
body = new Uint8Array(await new Response(new Blob([data]).stream().pipeThrough(new CompressionStream("gzip"))).arrayBuffer());
|
|
9917
|
+
} else if (compression === "zstd") {
|
|
9918
|
+
const runtime = globalThis.Bun;
|
|
9919
|
+
if (typeof runtime?.zstdCompress === "function") {
|
|
9920
|
+
body = new Uint8Array(await runtime.zstdCompress(data));
|
|
9921
|
+
} else if (typeof process !== "undefined" && process.versions?.node) {
|
|
9922
|
+
const nodeZlib = "node:zlib";
|
|
9923
|
+
const zlib = await import(nodeZlib);
|
|
9924
|
+
if (typeof zlib.zstdCompressSync !== "function")
|
|
9925
|
+
throw new Error("zstd catalog publication requires Node >=22.15, Bun, or an encoder-capable runtime");
|
|
9926
|
+
body = new Uint8Array(zlib.zstdCompressSync(data));
|
|
9927
|
+
} else {
|
|
9928
|
+
throw new Error("zstd catalog publication requires an encoder-capable runtime; use gzip or identity here");
|
|
9929
|
+
}
|
|
9930
|
+
}
|
|
9931
|
+
storedSize = body.byteLength;
|
|
9932
|
+
if (storedSize > (this.policy.maxStoredBytes ?? DEFAULT_MAX))
|
|
9933
|
+
throw new Error("catalog publication exceeds byte limit");
|
|
9934
|
+
return repository.storage.upload(body, compression === "identity" ? "" : compression);
|
|
9935
|
+
} };
|
|
9936
|
+
const ref = await publishExternalResult(OUTER_SCHEMA, { result: payload }, bounded, { includeSha256: true });
|
|
9937
|
+
const m = {
|
|
9938
|
+
formatVersion: 1,
|
|
9939
|
+
manifestId: crypto.randomUUID(),
|
|
9940
|
+
key,
|
|
9941
|
+
protocolName: VGI_PROTOCOL_NAME,
|
|
9942
|
+
protocolVersion: VGI_PROTOCOL_VERSION,
|
|
9943
|
+
contractHash: VGI_CATALOG_CONTRACT_HASH,
|
|
9944
|
+
codecRevision: 1,
|
|
9945
|
+
catalogVersion: identity.catalogVersion,
|
|
9946
|
+
etag: response.etag,
|
|
9947
|
+
sha256: ref.sha256,
|
|
9948
|
+
rawSize,
|
|
9949
|
+
storedSize,
|
|
9950
|
+
url: ref.url,
|
|
9951
|
+
validated: true,
|
|
9952
|
+
...await repository.describe(ref.url)
|
|
9953
|
+
};
|
|
9954
|
+
await this.validate(m, key, identity, response);
|
|
9955
|
+
const winner = await repository.commitIfAbsent(m);
|
|
9956
|
+
if (!manifestSame(m, winner))
|
|
9957
|
+
await this.validate(winner, key, identity);
|
|
9958
|
+
return winner;
|
|
9959
|
+
}
|
|
9960
|
+
}
|
|
9961
|
+
|
|
9488
9962
|
// src/protocol/handlers/catalog/contents.ts
|
|
9963
|
+
var schemaPathKey3 = (path) => JSON.stringify(path.map((part) => part.toLowerCase()));
|
|
9964
|
+
var PUBLICATIONS = new WeakMap;
|
|
9489
9965
|
var CACHE = new WeakMap;
|
|
9490
9966
|
async function buildCatalogContents(cat, owner, attach, version, ifNoneMatch) {
|
|
9491
9967
|
const result = await cat.catalogContents(attach, ifNoneMatch);
|
|
@@ -9500,39 +9976,74 @@ async function buildCatalogContents(cat, owner, attach, version, ifNoneMatch) {
|
|
|
9500
9976
|
return buildCatalogContentsResponse({ catalog_version: version, etag: etag0, not_modified: true });
|
|
9501
9977
|
}
|
|
9502
9978
|
const infos = [...result.schemas ?? []];
|
|
9503
|
-
const keys = infos.map((c) =>
|
|
9979
|
+
const keys = infos.map((c) => schemaPathKey3(c.schema.path));
|
|
9504
9980
|
const keySet = new Set(keys);
|
|
9505
9981
|
if (keySet.size !== keys.length)
|
|
9506
9982
|
throw new Error("catalog_contents returned duplicate schema paths");
|
|
9507
9983
|
for (const c of infos) {
|
|
9508
|
-
if (c.schema.path.length > 1 && !keySet.has(
|
|
9984
|
+
if (c.schema.path.length > 1 && !keySet.has(schemaPathKey3(c.schema.path.slice(0, -1)))) {
|
|
9509
9985
|
throw new Error(`catalog_contents returned schema path ${JSON.stringify(c.schema.path)} without its parent`);
|
|
9510
9986
|
}
|
|
9511
9987
|
}
|
|
9512
|
-
const
|
|
9513
|
-
path: [...c.schema.path],
|
|
9514
|
-
schema: encodeSchemaInfo(c.schema),
|
|
9515
|
-
tables: c.tables.map((t) => encodeTableInfo(t)),
|
|
9516
|
-
views: c.views.map((v) => encodeViewInfo(v)),
|
|
9517
|
-
scalar_functions: c.scalar_functions.map((f) => encodeFunctionInfoOnce(f)),
|
|
9518
|
-
aggregate_functions: c.aggregate_functions.map((f) => encodeFunctionInfoOnce(f)),
|
|
9519
|
-
table_functions: c.table_functions.map((f) => encodeFunctionInfoOnce(f)),
|
|
9520
|
-
scalar_macros: c.scalar_macros.map((m) => encodeMacroInfo(m)),
|
|
9521
|
-
table_macros: c.table_macros.map((m) => encodeMacroInfo(m)),
|
|
9522
|
-
indexes: c.indexes.map((i) => encodeIndexInfo(i))
|
|
9523
|
-
}));
|
|
9988
|
+
const streams = encodeCatalogStreams(infos.sort((a, b) => a.schema.path.length - b.schema.path.length));
|
|
9524
9989
|
let etag = etag0;
|
|
9525
9990
|
if (etag === null && owner.catalogContentsEtag === "content-hash") {
|
|
9526
|
-
etag = await catalogContentsDigest(
|
|
9991
|
+
etag = await catalogContentsDigest(streams, version);
|
|
9527
9992
|
}
|
|
9528
9993
|
if (etag !== null && ifNoneMatch !== null && etag === ifNoneMatch) {
|
|
9529
9994
|
return buildCatalogContentsResponse({ catalog_version: version, etag, not_modified: true });
|
|
9530
9995
|
}
|
|
9531
|
-
|
|
9996
|
+
const response = buildCatalogContentsResponse({ catalog_version: version, etag, ...streams });
|
|
9997
|
+
decodeCatalogStreams(response);
|
|
9998
|
+
return response;
|
|
9532
9999
|
}
|
|
9533
|
-
async function serveCatalogContents(cat, attach, ifNoneMatch) {
|
|
10000
|
+
async function serveCatalogContents(cat, attach, ifNoneMatch, callContext, sealedAttach = attach) {
|
|
9534
10001
|
const owner = cat.catalogContentsOwner(attach);
|
|
9535
10002
|
const version = Number(await cat.version(attach));
|
|
10003
|
+
const policy = owner.catalogSnapshotPolicy;
|
|
10004
|
+
if (policy?.delivery === "published") {
|
|
10005
|
+
const request = { catalog: cat, attachOpaqueData: attach, callContext, catalogVersion: version };
|
|
10006
|
+
const inline = (etag) => wrapResult(buildCatalogContentsResponse({ catalog_version: version, etag, not_modified: true }), CatalogContentsResultSchema);
|
|
10007
|
+
const cheapEtag = await policy.etag?.(request);
|
|
10008
|
+
if (cheapEtag != null && cheapEtag === ifNoneMatch)
|
|
10009
|
+
return inline(cheapEtag);
|
|
10010
|
+
let manager = PUBLICATIONS.get(policy);
|
|
10011
|
+
if (!manager) {
|
|
10012
|
+
manager = new CatalogSnapshotManager(policy);
|
|
10013
|
+
PUBLICATIONS.set(policy, manager);
|
|
10014
|
+
}
|
|
10015
|
+
const scopeBytes = new Uint8Array(await crypto.subtle.digest("SHA-256", sealedAttach));
|
|
10016
|
+
const attachmentScope = "attachment:" + Array.from(scopeBytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
10017
|
+
const identify = async (input) => {
|
|
10018
|
+
const id = await policy.identity(input);
|
|
10019
|
+
return { ...id, visibilityScope: id.visibilityScope ?? attachmentScope };
|
|
10020
|
+
};
|
|
10021
|
+
const identity = await identify(request);
|
|
10022
|
+
if (identity.catalogVersion !== version)
|
|
10023
|
+
throw new Error("snapshot identity version differs from the attached catalog");
|
|
10024
|
+
const assertCurrentIdentity = async () => {
|
|
10025
|
+
const currentVersion = Number(await cat.version(attach));
|
|
10026
|
+
const current = await identify({ ...request, catalogVersion: currentVersion });
|
|
10027
|
+
if (currentVersion !== version || await manager.key(identity) !== await manager.key(current))
|
|
10028
|
+
throw new Error("catalog changed while resolving its publication");
|
|
10029
|
+
};
|
|
10030
|
+
const buildSnapshot = async (validator) => {
|
|
10031
|
+
const response = await buildCatalogContents(cat, owner, attach, version, validator);
|
|
10032
|
+
await assertCurrentIdentity();
|
|
10033
|
+
return response;
|
|
10034
|
+
};
|
|
10035
|
+
try {
|
|
10036
|
+
const manifest = await manager.warm(identity, () => buildSnapshot(null));
|
|
10037
|
+
await assertCurrentIdentity();
|
|
10038
|
+
if (manifest.etag === ifNoneMatch)
|
|
10039
|
+
return inline(manifest.etag);
|
|
10040
|
+
return new ExternalRef2(manifest.url, manifest.sha256);
|
|
10041
|
+
} catch (error) {
|
|
10042
|
+
if (policy.onPublicationFailure !== "ordinary")
|
|
10043
|
+
throw error;
|
|
10044
|
+
return wrapResult(await buildSnapshot(ifNoneMatch), CatalogContentsResultSchema);
|
|
10045
|
+
}
|
|
10046
|
+
}
|
|
9536
10047
|
if (owner.catalogContentsCacheable()) {
|
|
9537
10048
|
let pending = CACHE.get(owner);
|
|
9538
10049
|
let snapshot = pending ? await pending.catch(() => null) : null;
|
|
@@ -9561,6 +10072,33 @@ async function serveCatalogContents(cat, attach, ifNoneMatch) {
|
|
|
9561
10072
|
}
|
|
9562
10073
|
return wrapResult(await buildCatalogContents(cat, owner, attach, version, ifNoneMatch), CatalogContentsResultSchema);
|
|
9563
10074
|
}
|
|
10075
|
+
|
|
10076
|
+
// src/catalog/item-encoding.ts
|
|
10077
|
+
var ENCODED = new WeakMap;
|
|
10078
|
+
function freezeCatalogItem(item) {
|
|
10079
|
+
deepFreeze(item);
|
|
10080
|
+
return item;
|
|
10081
|
+
}
|
|
10082
|
+
function deepFreeze(value) {
|
|
10083
|
+
if (value === null || typeof value !== "object" || Object.isFrozen(value) || ArrayBuffer.isView(value))
|
|
10084
|
+
return;
|
|
10085
|
+
const proto = Object.getPrototypeOf(value);
|
|
10086
|
+
if (proto !== Object.prototype && proto !== Array.prototype && proto !== null)
|
|
10087
|
+
return;
|
|
10088
|
+
for (const child of Object.values(value))
|
|
10089
|
+
deepFreeze(child);
|
|
10090
|
+
Object.freeze(value);
|
|
10091
|
+
}
|
|
10092
|
+
function encodeFunctionInfoOnce(info) {
|
|
10093
|
+
if (!Object.isFrozen(info))
|
|
10094
|
+
return encodeFunctionInfo(info);
|
|
10095
|
+
let encoded = ENCODED.get(info);
|
|
10096
|
+
if (encoded === undefined) {
|
|
10097
|
+
encoded = encodeFunctionInfo(info);
|
|
10098
|
+
ENCODED.set(info, encoded);
|
|
10099
|
+
}
|
|
10100
|
+
return encoded;
|
|
10101
|
+
}
|
|
9564
10102
|
// src/catalog/interface.ts
|
|
9565
10103
|
var encodeCopyFromFormatInfo = (v) => encodeASD(CopyFromFormatInfoSchema, {
|
|
9566
10104
|
comment: v.comment ?? null,
|
|
@@ -9621,6 +10159,7 @@ function isPromise(v) {
|
|
|
9621
10159
|
}
|
|
9622
10160
|
|
|
9623
10161
|
class CatalogInterface {
|
|
10162
|
+
catalogSnapshotPolicy = null;
|
|
9624
10163
|
catalogContentsEtag = null;
|
|
9625
10164
|
catalogContentsAttachIndependent = false;
|
|
9626
10165
|
catalogVersionFrozen = false;
|
|
@@ -10394,10 +10933,13 @@ function catalogAdminHandlers(getCatalog, signingKey) {
|
|
|
10394
10933
|
items: schemas.map((s) => encodeSchemaInfo(s))
|
|
10395
10934
|
}, CatalogSchemasResultSchema);
|
|
10396
10935
|
});
|
|
10397
|
-
const catalogContents =
|
|
10398
|
-
const
|
|
10399
|
-
return
|
|
10400
|
-
|
|
10936
|
+
const catalogContents = async (params, ctx) => {
|
|
10937
|
+
const sealedAttach = toUint8Array(params.attach_opaque_data).slice();
|
|
10938
|
+
return catalogHandler(signingKey, async (opened, context) => {
|
|
10939
|
+
const ifNoneMatch = opened.if_none_match == null ? null : String(opened.if_none_match);
|
|
10940
|
+
return serveCatalogContents(getCatalog(), toUint8Array(opened.attach_opaque_data), ifNoneMatch, context, sealedAttach);
|
|
10941
|
+
})(params, ctx);
|
|
10942
|
+
};
|
|
10401
10943
|
const catalogCopyFromFormats = catalogHandler(signingKey, async (params) => {
|
|
10402
10944
|
const cat = getCatalog();
|
|
10403
10945
|
const formats = await cat.copyFromFormats(toUint8Array(params.attach_opaque_data), params.transaction_opaque_data ? toUint8Array(params.transaction_opaque_data) : undefined);
|
|
@@ -12221,7 +12763,6 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
|
|
|
12221
12763
|
}
|
|
12222
12764
|
schemas(attachOpaqueData, transactionOpaqueData) {
|
|
12223
12765
|
return this._descriptor.schemas.map((s) => ({
|
|
12224
|
-
attach_opaque_data: this.itemAttachId(attachOpaqueData),
|
|
12225
12766
|
path: schemaDescriptorPath(s),
|
|
12226
12767
|
comment: s.comment ?? null,
|
|
12227
12768
|
tags: s.tags ?? {},
|
|
@@ -12253,7 +12794,6 @@ class ReadOnlyCatalogInterface extends CatalogInterface {
|
|
|
12253
12794
|
if (!desc)
|
|
12254
12795
|
return null;
|
|
12255
12796
|
return {
|
|
12256
|
-
attach_opaque_data: this.itemAttachId(attachOpaqueData),
|
|
12257
12797
|
path: schemaDescriptorPath(desc),
|
|
12258
12798
|
comment: desc.comment ?? null,
|
|
12259
12799
|
tags: desc.tags ?? {},
|
|
@@ -13448,4 +13988,4 @@ export {
|
|
|
13448
13988
|
utf82 as utf8
|
|
13449
13989
|
};
|
|
13450
13990
|
|
|
13451
|
-
//# debugId=
|
|
13991
|
+
//# debugId=E0D4B40A7639489164756E2164756E21
|