@powerhousedao/reactor-api 6.2.3-dev.64 → 6.2.3-dev.66
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/{adapter-gateway-apollo-DwAmdCYz.mjs → adapter-gateway-apollo-D-EdZ4e-.mjs} +10 -12
- package/dist/adapter-gateway-apollo-D-EdZ4e-.mjs.map +1 -0
- package/dist/{adapter-gateway-mercurius-BhO0nwH_.mjs → adapter-gateway-mercurius-CdxLge5p.mjs} +3 -11
- package/dist/adapter-gateway-mercurius-CdxLge5p.mjs.map +1 -0
- package/dist/{base-document-models-69WRTSfH.mjs → base-document-models-uUQKgXcf.mjs} +176 -4
- package/dist/base-document-models-uUQKgXcf.mjs.map +1 -0
- package/dist/{create-schema-DLTPkNpK.d.mts → create-schema-BUk-IQCT.d.mts} +16 -10
- package/dist/create-schema-BUk-IQCT.d.mts.map +1 -0
- package/dist/index.d.mts +15 -14
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +832 -361
- package/dist/index.mjs.map +1 -1
- package/dist/package-subgraph-check-CsTPajAN.mjs +137 -0
- package/dist/package-subgraph-check-CsTPajAN.mjs.map +1 -0
- package/dist/{pglite-node-Ow9i5Rb4.mjs → pglite-node-C-UQXrjU.mjs} +8 -5
- package/dist/{pglite-node-Ow9i5Rb4.mjs.map → pglite-node-C-UQXrjU.mjs.map} +1 -1
- package/dist/src/graphql/subgraph-types.d.mts +17 -11
- package/dist/src/graphql/subgraph-types.d.mts.map +1 -1
- package/dist/src/graphql/subgraph-types.mjs +8 -99
- package/dist/src/graphql/subgraph-types.mjs.map +1 -1
- package/dist/src/packages/vite-loader.d.mts +1 -1
- package/dist/src/packages/vite-loader.d.mts.map +1 -1
- package/dist/src/packages/vite-loader.mjs +34 -44
- package/dist/src/packages/vite-loader.mjs.map +1 -1
- package/dist/src/pglite/pglite-node.d.mts.map +1 -1
- package/dist/src/pglite/pglite-node.mjs +3 -3
- package/dist/{subgraph-extraction-CcgWd0De.mjs → subgraph-extraction-cSpPp3Hq.mjs} +5 -4
- package/dist/{subgraph-extraction-CcgWd0De.mjs.map → subgraph-extraction-cSpPp3Hq.mjs.map} +1 -1
- package/dist/subgraph-schema-owner-BEjuk2Xv.mjs +10 -0
- package/dist/subgraph-schema-owner-BEjuk2Xv.mjs.map +1 -0
- package/dist/{types-D3pD6fvQ.d.mts → types-CCI5IDgT.d.mts} +4 -5
- package/dist/{types-D3pD6fvQ.d.mts.map → types-CCI5IDgT.d.mts.map} +1 -1
- package/package.json +13 -13
- package/dist/adapter-gateway-apollo-DTcAJpfN.mjs +0 -4
- package/dist/adapter-gateway-apollo-DwAmdCYz.mjs.map +0 -1
- package/dist/adapter-gateway-mercurius-BhO0nwH_.mjs.map +0 -1
- package/dist/adapter-gateway-stitching-969ZPot-.mjs +0 -4
- package/dist/adapter-gateway-stitching-ClVkjojb.mjs +0 -333
- package/dist/adapter-gateway-stitching-ClVkjojb.mjs.map +0 -1
- package/dist/base-document-models-69WRTSfH.mjs.map +0 -1
- package/dist/create-schema-DLTPkNpK.d.mts.map +0 -1
- package/dist/websocket-_SH5aBcy.mjs +0 -18
- package/dist/websocket-_SH5aBcy.mjs.map +0 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="b6799fec-d901-5d42-aa53-ec8d3e900e23")}catch(e){}}();
|
|
3
|
+
import { D as generatedTypesOf, I as schema_default$1, N as reservedTypeNames, T as findSubgraphTypeCollisions, _ as DocumentModelSchemaCache, a as CORE_ORIGIN, c as RegistrationOrder, d as decideAdmission, f as describeOrigin, h as collectSubgraphClaims, i as inspectionTypeDefs, j as getGeneratedDocumentModelTypes, k as getDocumentModelSubgraphName, l as admitDocumentModels, m as supergraphClaimLosers, n as systemTypeDefs, o as DOCUMENT_MODEL_ORIGIN, p as documentModelSubgraphSources, r as schema_default, t as baseDocumentModels } from "./base-document-models-uUQKgXcf.mjs";
|
|
4
|
+
import "./subgraph-schema-owner-BEjuk2Xv.mjs";
|
|
5
|
+
import { typedefs } from "@powerhousedao/analytics-engine-graphql";
|
|
6
|
+
import { gql } from "graphql-tag";
|
|
7
|
+
//#region src/graphql/package-subgraph-check.ts
|
|
8
|
+
/** The type definitions of every core subgraph, by subgraph name. */
|
|
9
|
+
const coreSubgraphTypeDefs = /* @__PURE__ */ new Map([
|
|
10
|
+
["analytics", gql(typedefs)],
|
|
11
|
+
["system", systemTypeDefs],
|
|
12
|
+
["r", gql(schema_default)],
|
|
13
|
+
["inspection", inspectionTypeDefs],
|
|
14
|
+
["auth", gql(schema_default$1)]
|
|
15
|
+
]);
|
|
16
|
+
const PACKAGE_ORIGIN = "package";
|
|
17
|
+
function claimsOf(typeDefs, generated) {
|
|
18
|
+
try {
|
|
19
|
+
return collectSubgraphClaims(typeDefs, generated);
|
|
20
|
+
} catch {
|
|
21
|
+
return /* @__PURE__ */ new Map();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function describeModelSubgraphRejection(rejection) {
|
|
25
|
+
switch (rejection.reason) {
|
|
26
|
+
case "core name": return `its subgraph name is taken by a subgraph from ${describeOrigin(CORE_ORIGIN)}`;
|
|
27
|
+
case "rank": return `its subgraph name is taken by a subgraph from ${describeOrigin(rejection.holderOrigin)}`;
|
|
28
|
+
case "collision": return `its subgraph defines generated types: ${rejection.collisions.join(", ")}`;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function packageConflict(rejection, owners) {
|
|
32
|
+
switch (rejection.reason) {
|
|
33
|
+
case "core name": return {
|
|
34
|
+
nameHeldBy: "core",
|
|
35
|
+
claims: []
|
|
36
|
+
};
|
|
37
|
+
case "rank": return {
|
|
38
|
+
nameHeldBy: "document model",
|
|
39
|
+
claims: []
|
|
40
|
+
};
|
|
41
|
+
case "collision": return { claims: rejection.collisions.map((claim) => ({
|
|
42
|
+
claim,
|
|
43
|
+
owner: owners.get(claim) ?? "document-model schema"
|
|
44
|
+
})) };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Server admission replayed for one package; hosts and other packages unknown. */
|
|
48
|
+
function checkPackageSubgraphs(subgraphs, documentModels) {
|
|
49
|
+
const packageModels = documentModels.map((global) => ({ documentModel: { global } }));
|
|
50
|
+
const { admitted, schemas, refusals } = admitDocumentModels(new DocumentModelSchemaCache().resolve([...baseDocumentModels.map(([, model]) => model), ...packageModels]), reservedTypeNames(coreSubgraphTypeDefs.values()));
|
|
51
|
+
const generated = getGeneratedDocumentModelTypes(admitted, schemas);
|
|
52
|
+
const fixed = generatedTypesOf([]).names;
|
|
53
|
+
const generatedOwners = /* @__PURE__ */ new Map();
|
|
54
|
+
for (const model of admitted) {
|
|
55
|
+
const owner = getDocumentModelSubgraphName(model.documentModel.global);
|
|
56
|
+
const { names } = getGeneratedDocumentModelTypes([model], schemas);
|
|
57
|
+
for (const name of names) if (!fixed.has(name)) generatedOwners.set(name, owner);
|
|
58
|
+
}
|
|
59
|
+
const holders = /* @__PURE__ */ new Map();
|
|
60
|
+
const order = new RegistrationOrder();
|
|
61
|
+
const register = (entry) => {
|
|
62
|
+
const holder = holders.get(entry.name);
|
|
63
|
+
const admission = decideAdmission(entry, entry.origin, holder ? [{
|
|
64
|
+
holder,
|
|
65
|
+
origin: holder.origin
|
|
66
|
+
}] : [], () => {
|
|
67
|
+
try {
|
|
68
|
+
return findSubgraphTypeCollisions(generated, entry.typeDefs, entry.ownTypeNames);
|
|
69
|
+
} catch {
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
if (admission.action === "add") {
|
|
74
|
+
holders.set(entry.name, entry);
|
|
75
|
+
order.record(entry.origin, entry.name);
|
|
76
|
+
}
|
|
77
|
+
return admission;
|
|
78
|
+
};
|
|
79
|
+
for (const [name, typeDefs] of coreSubgraphTypeDefs) register({
|
|
80
|
+
name,
|
|
81
|
+
origin: CORE_ORIGIN,
|
|
82
|
+
typeDefs
|
|
83
|
+
});
|
|
84
|
+
const refusedModels = refusals.filter(({ model }) => packageModels.includes(model)).map(({ model, reason, subgraphOnly }) => ({
|
|
85
|
+
name: model.documentModel.global.name,
|
|
86
|
+
reason,
|
|
87
|
+
subgraphOnly
|
|
88
|
+
}));
|
|
89
|
+
for (const { model, types } of documentModelSubgraphSources(admitted, schemas)) {
|
|
90
|
+
const admission = register({
|
|
91
|
+
name: getDocumentModelSubgraphName(model.documentModel.global),
|
|
92
|
+
origin: DOCUMENT_MODEL_ORIGIN,
|
|
93
|
+
typeDefs: types.typeDefs,
|
|
94
|
+
ownTypeNames: types.typeNames
|
|
95
|
+
});
|
|
96
|
+
if (admission.action === "reject" && packageModels.includes(model)) refusedModels.push({
|
|
97
|
+
name: model.documentModel.global.name,
|
|
98
|
+
reason: describeModelSubgraphRejection(admission),
|
|
99
|
+
subgraphOnly: true
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const conflicts = /* @__PURE__ */ new Map();
|
|
103
|
+
for (const { name, typeDefs } of subgraphs) {
|
|
104
|
+
const admission = register({
|
|
105
|
+
name,
|
|
106
|
+
origin: PACKAGE_ORIGIN,
|
|
107
|
+
typeDefs
|
|
108
|
+
});
|
|
109
|
+
if (admission.action === "reject") conflicts.set(name, packageConflict(admission, generatedOwners));
|
|
110
|
+
else if (admission.action === "add" && admission.replaces.length > 0) conflicts.set(name, {
|
|
111
|
+
nameHeldBy: "package",
|
|
112
|
+
claims: []
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
const excluded = supergraphClaimLosers(Array.from(holders.values(), (entry) => ({
|
|
116
|
+
member: entry,
|
|
117
|
+
name: entry.name,
|
|
118
|
+
origin: entry.origin,
|
|
119
|
+
claims: claimsOf(entry.typeDefs, generated.names)
|
|
120
|
+
})), order);
|
|
121
|
+
for (const [entry, claims] of excluded) {
|
|
122
|
+
if (entry.origin !== PACKAGE_ORIGIN) continue;
|
|
123
|
+
conflicts.set(entry.name, {
|
|
124
|
+
...conflicts.get(entry.name),
|
|
125
|
+
claims
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
conflicts,
|
|
130
|
+
refusedModels
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
export { checkPackageSubgraphs };
|
|
135
|
+
|
|
136
|
+
//# sourceMappingURL=package-subgraph-check-CsTPajAN.mjs.map
|
|
137
|
+
//# debugId=b6799fec-d901-5d42-aa53-ec8d3e900e23
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package-subgraph-check-CsTPajAN.mjs","sources":["../src/graphql/package-subgraph-check.ts"],"sourcesContent":["import { typedefs as analyticsTypeDefs } from \"@powerhousedao/analytics-engine-graphql\";\nimport type { DocumentModelGlobalState } from \"@powerhousedao/shared/document-model\";\nimport type { DocumentNode } from \"graphql\";\nimport { gql } from \"graphql-tag\";\nimport { baseDocumentModels } from \"../packages/base-document-models.js\";\nimport {\n DocumentModelSchemaCache,\n type DocumentModelSource,\n findSubgraphTypeCollisions,\n generatedTypesOf,\n getDocumentModelSubgraphName,\n getGeneratedDocumentModelTypes,\n reservedTypeNames,\n} from \"../utils/create-schema.js\";\nimport authSchema from \"./auth/schema.graphql\";\nimport { inspectionTypeDefs } from \"./inspection/schema.js\";\nimport reactorSchema from \"./reactor/schema.graphql\";\nimport {\n type Admission,\n admitDocumentModels,\n CORE_ORIGIN,\n decideAdmission,\n describeOrigin,\n DOCUMENT_MODEL_ORIGIN,\n documentModelSubgraphSources,\n RegistrationOrder,\n type SubgraphOrigin,\n supergraphClaimLosers,\n} from \"./subgraph-admission.js\";\nimport {\n collectSubgraphClaims,\n type SubgraphClaims,\n} from \"./subgraph-type-ownership.js\";\nimport { systemTypeDefs } from \"./system/schema.js\";\nimport { DOCUMENT_MODEL_SCHEMA_OWNER } from \"./subgraph-schema-owner.js\";\nimport type {\n PackageSubgraph,\n PackageSubgraphConflicts,\n PackageSubgraphReport,\n RefusedDocumentModel,\n} from \"./subgraph-types.js\";\n\n/** The type definitions of every core subgraph, by subgraph name. */\nexport const coreSubgraphTypeDefs: ReadonlyMap<string, DocumentNode> = new Map([\n [\"analytics\", gql(analyticsTypeDefs)],\n [\"system\", systemTypeDefs],\n [\"r\", gql(reactorSchema)],\n [\"inspection\", inspectionTypeDefs],\n [\"auth\", gql(authSchema)],\n]);\n\nconst PACKAGE_ORIGIN: SubgraphOrigin = \"package\";\n\ntype Registered = {\n name: string;\n origin: SubgraphOrigin;\n typeDefs: DocumentNode;\n ownTypeNames?: ReadonlySet<string>;\n};\n\ntype Rejection = Extract<Admission<Registered>, { action: \"reject\" }>;\n\nfunction claimsOf(typeDefs: DocumentNode, generated: ReadonlySet<string>) {\n try {\n return collectSubgraphClaims(typeDefs, generated);\n } catch {\n // Malformed typeDefs fail where the schema is built, so claim nothing.\n return new Map() as SubgraphClaims;\n }\n}\n\nfunction describeModelSubgraphRejection(rejection: Rejection): string {\n switch (rejection.reason) {\n case \"core name\":\n return `its subgraph name is taken by a subgraph from ${describeOrigin(CORE_ORIGIN)}`;\n case \"rank\":\n return `its subgraph name is taken by a subgraph from ${describeOrigin(rejection.holderOrigin)}`;\n case \"collision\":\n return `its subgraph defines generated types: ${rejection.collisions.join(\", \")}`;\n }\n}\n\nfunction packageConflict(\n rejection: Rejection,\n owners: ReadonlyMap<string, string>,\n): PackageSubgraphConflicts {\n switch (rejection.reason) {\n case \"core name\":\n return { nameHeldBy: \"core\", claims: [] };\n case \"rank\":\n return { nameHeldBy: \"document model\", claims: [] };\n case \"collision\":\n return {\n claims: rejection.collisions.map((claim) => ({\n claim,\n owner: owners.get(claim) ?? DOCUMENT_MODEL_SCHEMA_OWNER,\n })),\n };\n }\n}\n\n/** Server admission replayed for one package; hosts and other packages unknown. */\nexport function checkPackageSubgraphs(\n subgraphs: readonly PackageSubgraph[],\n documentModels: readonly DocumentModelGlobalState[],\n): PackageSubgraphReport {\n const packageModels: DocumentModelSource[] = documentModels.map((global) => ({\n documentModel: { global },\n }));\n const { admitted, schemas, refusals } = admitDocumentModels(\n new DocumentModelSchemaCache<DocumentModelSource>().resolve([\n ...baseDocumentModels.map(([, model]) => model),\n ...packageModels,\n ]),\n reservedTypeNames(coreSubgraphTypeDefs.values()),\n );\n const generated = getGeneratedDocumentModelTypes(admitted, schemas);\n const fixed = generatedTypesOf([]).names;\n const generatedOwners = new Map<string, string>();\n for (const model of admitted) {\n const owner = getDocumentModelSubgraphName(model.documentModel.global);\n const { names } = getGeneratedDocumentModelTypes([model], schemas);\n for (const name of names) {\n if (!fixed.has(name)) generatedOwners.set(name, owner);\n }\n }\n\n const holders = new Map<string, Registered>();\n const order = new RegistrationOrder();\n const register = (entry: Registered): Admission<Registered> => {\n const holder = holders.get(entry.name);\n const admission = decideAdmission(\n entry,\n entry.origin,\n holder ? [{ holder, origin: holder.origin }] : [],\n () => {\n try {\n return findSubgraphTypeCollisions(\n generated,\n entry.typeDefs,\n entry.ownTypeNames,\n );\n } catch {\n return [];\n }\n },\n );\n if (admission.action === \"add\") {\n holders.set(entry.name, entry);\n order.record(entry.origin, entry.name);\n }\n return admission;\n };\n\n for (const [name, typeDefs] of coreSubgraphTypeDefs) {\n register({ name, origin: CORE_ORIGIN, typeDefs });\n }\n\n const refusedModels: RefusedDocumentModel[] = refusals\n .filter(({ model }) => packageModels.includes(model))\n .map(({ model, reason, subgraphOnly }) => ({\n name: model.documentModel.global.name,\n reason,\n subgraphOnly,\n }));\n for (const { model, types } of documentModelSubgraphSources(\n admitted,\n schemas,\n )) {\n const admission = register({\n name: getDocumentModelSubgraphName(model.documentModel.global),\n origin: DOCUMENT_MODEL_ORIGIN,\n typeDefs: types.typeDefs,\n ownTypeNames: types.typeNames,\n });\n if (admission.action === \"reject\" && packageModels.includes(model)) {\n refusedModels.push({\n name: model.documentModel.global.name,\n reason: describeModelSubgraphRejection(admission),\n subgraphOnly: true,\n });\n }\n }\n\n const conflicts = new Map<string, PackageSubgraphConflicts>();\n for (const { name, typeDefs } of subgraphs) {\n const admission = register({ name, origin: PACKAGE_ORIGIN, typeDefs });\n if (admission.action === \"reject\") {\n conflicts.set(name, packageConflict(admission, generatedOwners));\n } else if (admission.action === \"add\" && admission.replaces.length > 0) {\n conflicts.set(name, { nameHeldBy: \"package\", claims: [] });\n }\n }\n\n const excluded = supergraphClaimLosers(\n Array.from(holders.values(), (entry) => ({\n member: entry,\n name: entry.name,\n origin: entry.origin,\n claims: claimsOf(entry.typeDefs, generated.names),\n })),\n order,\n );\n for (const [entry, claims] of excluded) {\n if (entry.origin !== PACKAGE_ORIGIN) continue;\n conflicts.set(entry.name, { ...conflicts.get(entry.name), claims });\n }\n return { conflicts, refusedModels };\n}\n"],"names":["analyticsTypeDefs","reactorSchema","authSchema"],"mappings":";;;;;;;;AA2CA,MAAa,uCAA0D,IAAI,IAAI;CAC7E,CAAC,aAAa,IAAIA,QAAiB,CAAC;CACpC,CAAC,UAAU,cAAc;CACzB,CAAC,KAAK,IAAIC,cAAa,CAAC;CACxB,CAAC,cAAc,kBAAkB;CACjC,CAAC,QAAQ,IAAIC,gBAAU,CAAC;AAC1B,CAAC;AAED,MAAM,iBAAiC;AAWvC,SAAS,SAAS,UAAwB,WAAgC;CACxE,IAAI;EACF,OAAO,sBAAsB,UAAU,SAAS;CAClD,QAAQ;EAEN,uBAAO,IAAI,IAAI;CACjB;AACF;AAEA,SAAS,+BAA+B,WAA8B;CACpE,QAAQ,UAAU,QAAlB;EACE,KAAK,aACH,OAAO,iDAAiD,eAAe,WAAW;EACpF,KAAK,QACH,OAAO,iDAAiD,eAAe,UAAU,YAAY;EAC/F,KAAK,aACH,OAAO,yCAAyC,UAAU,WAAW,KAAK,IAAI;CAClF;AACF;AAEA,SAAS,gBACP,WACA,QAC0B;CAC1B,QAAQ,UAAU,QAAlB;EACE,KAAK,aACH,O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,GACF,KACF;CACA,KAAK,MAAM,CAAC,OAAO,WAAW,UAAU;EACtC,IAAI,MAAM,WAAW,gBAAgB;EACrC,UAAU,IAAI,MAAM,MAAM;GAAE,GAAG,UAAU,IAAI,MAAM,IAAI;GAAG;EAAO,CAAC;CACpE;CACA,OAAO;EAAE;EAAW;CAAc;AACpC","debug_id":"b6799fec-d901-5d42-aa53-ec8d3e900e23"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="3247c4a7-6c83-5f29-9c5e-66bdf6069d43")}catch(e){}}();
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import nodeFs, { promises } from "node:fs";
|
|
5
5
|
import { PGlite, protocol } from "@electric-sql/pglite";
|
|
@@ -173,6 +173,9 @@ const EIO = 29;
|
|
|
173
173
|
function resolvePgliteFsync(env) {
|
|
174
174
|
return env.PH_PGLITE_FSYNC !== "0";
|
|
175
175
|
}
|
|
176
|
+
function isReadOnly(stream) {
|
|
177
|
+
return stream.flags !== void 0 && (stream.flags & 3) === 0;
|
|
178
|
+
}
|
|
176
179
|
const WASM_ENTRY_POINTS = [
|
|
177
180
|
"_interactive_one",
|
|
178
181
|
"_interactive_write",
|
|
@@ -260,7 +263,7 @@ function createDurableNodeFs(Base, dataDir, options = {}) {
|
|
|
260
263
|
}
|
|
261
264
|
fsyncStream(stream) {
|
|
262
265
|
if (!this.hostSyncs) return 0;
|
|
263
|
-
if (typeof stream.nfd !== "number") return 0;
|
|
266
|
+
if (typeof stream.nfd !== "number" || isReadOnly(stream)) return 0;
|
|
264
267
|
try {
|
|
265
268
|
hostFs.fsyncSync(stream.nfd);
|
|
266
269
|
} catch (err) {
|
|
@@ -277,7 +280,7 @@ function createDurableNodeFs(Base, dataDir, options = {}) {
|
|
|
277
280
|
} catch {
|
|
278
281
|
return -ERRNO_CODES.EBADF;
|
|
279
282
|
}
|
|
280
|
-
if (typeof stream.nfd !== "number") return 0;
|
|
283
|
+
if (typeof stream.nfd !== "number" || isReadOnly(stream)) return 0;
|
|
281
284
|
try {
|
|
282
285
|
hostFs.fdatasyncSync(stream.nfd);
|
|
283
286
|
} catch (err) {
|
|
@@ -569,5 +572,5 @@ async function removeStalePgliteFiles(dir, logger) {
|
|
|
569
572
|
//#endregion
|
|
570
573
|
export { convertSnapshotDir as a, extractSnapshot as c, syncDirectory as d, syncTree as f, PgliteMaintenance as h, removeStalePgliteFiles as i, createDurableNodeFs as l, DEFAULT_VACUUM_FULL_ABOVE_BYTES as m, openCurrentPgliteForVerify as n, recoverConversion as o, DEFAULT_MAINTENANCE_INTERVAL_MS as p, preparePgliteDataDir as r, SNAPSHOT_FILE_NAME as s, CURRENT_PGLITE_MAJOR as t, resolvePgliteFsync as u };
|
|
571
574
|
|
|
572
|
-
//# sourceMappingURL=pglite-node-
|
|
573
|
-
//# debugId=
|
|
575
|
+
//# sourceMappingURL=pglite-node-C-UQXrjU.mjs.map
|
|
576
|
+
//# debugId=3247c4a7-6c83-5f29-9c5e-66bdf6069d43
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pglite-node-Ow9i5Rb4.mjs","sources":["../src/pglite/maintenance.ts","../src/pglite/sync-tree.ts","../src/pglite/durable-node-fs.ts","../src/pglite/snapshot-reader.ts","../src/pglite/convert-snapshot-dir.ts","../src/pglite/preflight.ts"],"sourcesContent":["import { protocol, type PGlite } from \"@electric-sql/pglite\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\n\nexport const DEFAULT_MAINTENANCE_INTERVAL_MS = 5 * 60 * 1000;\nexport const DEFAULT_VACUUM_FULL_ABOVE_BYTES = 256 * 1024 * 1024;\nconst MAINTENANCE_RETRY_MS = 1000;\n\nexport type MaintenanceOutcome =\n | \"vacuum\"\n | \"vacuum-full\"\n | \"idle\"\n | \"not-ready\"\n | \"in-transaction\"\n | \"failed\";\n\nexport interface MaintenanceLogger {\n warn(message: string): void;\n}\n\nexport interface MaintenanceOptions {\n /** VACUUM + CHECKPOINT cadence; PGlite has no autovacuum. Default 5 min; 0 disables. */\n intervalMs?: number;\n /** First pass runs VACUUM FULL when `base/` exceeds this at open. Default 256 MB; 0 disables. */\n vacuumFullAboveBytes?: number;\n logger?: MaintenanceLogger;\n}\n\nexport class PgliteMaintenance {\n private readonly intervalMs: number;\n private readonly vacuumFullAboveBytes: number;\n private readonly logger?: MaintenanceLogger;\n\n private pg?: PGlite;\n private label = \"\";\n private closing = false;\n private syncsSinceMaintenance = 0;\n private vacuumFullPending = false;\n private timer?: ReturnType<typeof setTimeout>;\n private inFlight?: Promise<MaintenanceOutcome>;\n\n constructor(options: MaintenanceOptions = {}) {\n this.intervalMs = Math.max(\n 0,\n options.intervalMs ?? DEFAULT_MAINTENANCE_INTERVAL_MS,\n );\n this.vacuumFullAboveBytes = Math.max(\n 0,\n options.vacuumFullAboveBytes ?? DEFAULT_VACUUM_FULL_ABOVE_BYTES,\n );\n this.logger = options.logger;\n }\n\n get scheduled(): boolean {\n return this.timer !== undefined;\n }\n\n get running(): boolean {\n return this.inFlight !== undefined;\n }\n\n start(pg: PGlite, label: string, baseBytes: number): void {\n this.pg = pg;\n this.label = label;\n this.vacuumFullPending =\n this.vacuumFullAboveBytes > 0 && baseBytes > this.vacuumFullAboveBytes;\n this.schedule(this.intervalMs);\n }\n\n noteSync(): void {\n this.syncsSinceMaintenance++;\n }\n\n async stop(): Promise<void> {\n this.closing = true;\n this.cancel();\n await this.inFlight;\n }\n\n async run(): Promise<MaintenanceOutcome> {\n const pg = this.pg;\n if (this.closing || !pg?.ready) return \"not-ready\";\n // pg.close() does not take the query mutex; recheck before each statement.\n const isReady = (): boolean => pg.ready && !this.closing;\n if (this.syncsSinceMaintenance === 0 && !this.vacuumFullPending) {\n return \"idle\";\n }\n const full = this.vacuumFullPending;\n let outcome: MaintenanceOutcome;\n try {\n outcome = await pg._runExclusiveQuery(async () => {\n // isInTransaction() misses START TRANSACTION; ReadyForQuery status does not.\n if (!isReady()) return \"not-ready\";\n const { messages } = await pg.execProtocol(\n protocol.serialize.query(\"\"),\n { syncToFs: false },\n );\n if (transactionStatus(messages) !== \"I\") return \"in-transaction\";\n if (full) {\n this.logger?.warn(\n `DurableNodeFs: ${this.label}/base exceeds ${this.vacuumFullAboveBytes} bytes; running VACUUM FULL`,\n );\n }\n for (const statement of [\n full ? \"VACUUM FULL\" : \"VACUUM\",\n \"CHECKPOINT\",\n ]) {\n if (!isReady()) return \"not-ready\";\n await pg.execProtocol(protocol.serialize.query(statement), {\n syncToFs: false,\n });\n }\n return full ? \"vacuum-full\" : \"vacuum\";\n });\n } catch (err) {\n this.logger?.warn(\n `DurableNodeFs maintenance failed: ${errorMessage(err)}`,\n );\n return \"failed\";\n }\n if (outcome !== \"vacuum\" && outcome !== \"vacuum-full\") return outcome;\n this.syncsSinceMaintenance = 0;\n this.vacuumFullPending = false;\n return outcome;\n }\n\n private schedule(delayMs: number): void {\n if (this.intervalMs === 0 || this.closing) return;\n this.timer = setTimeout(() => {\n this.timer = undefined;\n this.inFlight = this.run()\n .then((outcome) => {\n this.schedule(\n outcome === \"in-transaction\"\n ? Math.min(MAINTENANCE_RETRY_MS, this.intervalMs)\n : this.intervalMs,\n );\n return outcome;\n })\n .finally(() => {\n this.inFlight = undefined;\n });\n }, delayMs);\n this.timer.unref();\n }\n\n private cancel(): void {\n if (!this.timer) return;\n clearTimeout(this.timer);\n this.timer = undefined;\n }\n}\n\n/** Total size of the regular files under `dir`; 0 when `dir` is missing. */\nexport async function dirBytes(dir: string): Promise<number> {\n let entries;\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") return 0;\n throw err;\n }\n let total = 0;\n for (const entry of entries) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n total += await dirBytes(full);\n } else if (entry.isFile()) {\n total += (await fs.stat(full)).size;\n }\n }\n return total;\n}\n\nfunction transactionStatus(messages: readonly { name: string }[]): unknown {\n for (let i = messages.length - 1; i >= 0; i--) {\n const m = messages[i];\n if (m.name === \"readyForQuery\") return (m as { status?: unknown }).status;\n }\n return undefined;\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n","import nodeFs from \"node:fs\";\nimport path from \"node:path\";\n\nexport interface SyncTreeHostFs {\n fsyncSync(fd: number): void;\n}\n\nexport interface SyncTreeResult {\n files: number;\n dirs: number;\n}\n\n/** One pass over a finished tree; interleaving fsync with writes costs seconds. */\nexport function syncTree(\n root: string,\n hostFs: SyncTreeHostFs = nodeFs,\n): SyncTreeResult {\n const result: SyncTreeResult = { files: 0, dirs: 0 };\n const pending = [path.resolve(root)];\n while (pending.length > 0) {\n const dir = pending.pop()!;\n for (const entry of nodeFs.readdirSync(dir, { withFileTypes: true })) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n pending.push(full);\n } else if (entry.isFile()) {\n syncFile(full, hostFs);\n result.files++;\n }\n }\n syncDirectory(dir, hostFs);\n result.dirs++;\n }\n return result;\n}\n\n// Windows refuses FlushFileBuffers on a read-only handle (EPERM).\nfunction syncFile(file: string, hostFs: SyncTreeHostFs): void {\n const fd = nodeFs.openSync(file, \"r+\");\n try {\n hostFs.fsyncSync(fd);\n } finally {\n nodeFs.closeSync(fd);\n }\n}\n\n/** fsyncs a directory's entries; false where the platform refuses (Windows). */\nexport function syncDirectory(\n dir: string,\n hostFs: SyncTreeHostFs = nodeFs,\n): boolean {\n let fd: number;\n try {\n fd = nodeFs.openSync(dir, \"r\");\n } catch {\n return false;\n }\n try {\n hostFs.fsyncSync(fd);\n return true;\n } catch {\n return false;\n } finally {\n nodeFs.closeSync(fd);\n }\n}\n","import type { PGlite } from \"@electric-sql/pglite\";\nimport { ERRNO_CODES } from \"@electric-sql/pglite/basefs\";\nimport type { NodeFS } from \"@electric-sql/pglite/nodefs\";\nimport nodeFs from \"node:fs\";\nimport path from \"node:path\";\nimport { dirBytes, PgliteMaintenance } from \"./maintenance.js\";\nimport { syncTree } from \"./sync-tree.js\";\n\nexport type NodeFsClass = typeof NodeFS;\n\n// Emscripten/WASI errno; the basefs ERRNO_CODES export lacks it.\nconst EIO = 29;\n\nexport interface DurableNodeFsOptions {\n /** Default true; `PH_PGLITE_FSYNC=0` turns it off (power-loss durability off). */\n fsync?: boolean;\n /** Default 5 min; 0 disables. */\n maintenanceIntervalMs?: number;\n /** `base/` size at open; default 256 MB; 0 disables. */\n vacuumFullAboveBytes?: number;\n logger?: { warn(message: string): void };\n /** Emscripten abort (a Postgres PANIC, e.g. ENOSPC on WAL). */\n onAbort?: (what: unknown) => void;\n hostFs?: Pick<typeof nodeFs, \"fsyncSync\" | \"fdatasyncSync\">;\n}\n\nexport type DurableNodeFs = InstanceType<NodeFsClass> & {\n readonly maintenance: PgliteMaintenance;\n};\n\nexport function resolvePgliteFsync(\n env: Record<string, string | undefined>,\n): boolean {\n return env.PH_PGLITE_FSYNC !== \"0\";\n}\n\ninterface NodeFsStream {\n nfd?: number;\n}\n\ninterface EmscriptenFs {\n filesystems: { NODEFS: { stream_ops: Record<string, unknown> } };\n getStreamChecked(fd: number): NodeFsStream;\n quit(): void;\n}\n\ninterface EmscriptenMod {\n FS: EmscriptenFs;\n [wasmExport: string]: unknown;\n}\n\n// PGlite re-enters wasm from its own error handler after a query fails; on an\n// aborted runtime that spins forever, so every entry point is cut off first.\nconst WASM_ENTRY_POINTS = [\n \"_interactive_one\",\n \"_interactive_write\",\n \"_pgl_backend\",\n \"_pgl_initdb\",\n \"_pgl_shutdown\",\n];\n\nfunction poisonAfterAbort(mod: EmscriptenMod | undefined, what: unknown): void {\n if (!mod) return;\n const message = `PGlite aborted: ${String(what) || \"wasm runtime abort\"}`;\n for (const name of WASM_ENTRY_POINTS) {\n if (typeof mod[name] !== \"function\") continue;\n mod[name] = () => {\n throw new Error(message);\n };\n }\n // pg.close() rejects before closeFs(), so release the host fds here.\n try {\n mod.FS.quit();\n } catch {\n // already quit by an earlier abort\n }\n}\n\ntype SyscallImport = ((fd: number) => number) & { sig?: string };\n\ninterface WasmImports {\n env: Record<string, unknown> & { __syscall_fdatasync?: SyscallImport };\n}\n\ntype InstantiateWasm = (\n imports: WasmImports,\n done: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,\n) => unknown;\n\ninterface EmscriptenOpts {\n preRun?: ((mod: EmscriptenMod) => void)[];\n instantiateWasm?: InstantiateWasm;\n onAbort?: (what: unknown) => void;\n}\n\ntype InitOpts = Parameters<InstanceType<NodeFsClass>[\"init\"]>[1];\n\n/** Built over the passed-in class so PG16 dirs on `pglite-legacy-02` get the same behaviour. */\nexport function createDurableNodeFs(\n Base: NodeFsClass,\n dataDir: string,\n options: DurableNodeFsOptions = {},\n): DurableNodeFs {\n const fsync = options.fsync ?? true;\n const hostFs = options.hostFs ?? nodeFs;\n const logger = options.logger;\n const onAbort = options.onAbort;\n const resolvedDir = path.resolve(dataDir);\n const maintenance = new PgliteMaintenance({\n intervalMs: options.maintenanceIntervalMs,\n vacuumFullAboveBytes: options.vacuumFullAboveBytes,\n logger,\n });\n\n class Durable extends Base {\n readonly maintenance = maintenance;\n private mod?: EmscriptenMod;\n // Off from initialSyncFs of a fresh dir until PGlite's first syncToFs.\n private hostSyncs = fsync;\n private freshInit = false;\n\n async init(\n pg: PGlite,\n opts: InitOpts,\n ): Promise<{ emscriptenOpts: InitOpts }> {\n const { emscriptenOpts: base } = await super.init(pg, opts);\n const baseOpts = base as EmscriptenOpts;\n const inner = baseOpts.instantiateWasm;\n if (fsync && !inner) {\n throw new Error(\"PGlite did not supply instantiateWasm\");\n }\n const emscriptenOpts: EmscriptenOpts = {\n ...baseOpts,\n onAbort: (what) => {\n poisonAfterAbort(this.mod, what);\n void maintenance.stop();\n onAbort?.(what);\n },\n preRun: [\n ...(baseOpts.preRun ?? []),\n (mod) => {\n this.mod = mod;\n if (!fsync) return;\n mod.FS.filesystems.NODEFS.stream_ops.fsync = (\n stream: NodeFsStream,\n ) => this.fsyncStream(stream);\n },\n ],\n instantiateWasm:\n fsync && inner\n ? (imports, done) => {\n const original = imports.env.__syscall_fdatasync;\n const replacement: SyscallImport = (fd) => this.fdatasync(fd);\n replacement.sig = original?.sig;\n imports.env.__syscall_fdatasync = replacement;\n return inner(imports, done);\n }\n : inner,\n };\n return { emscriptenOpts: emscriptenOpts as InitOpts };\n }\n\n async initialSyncFs(): Promise<void> {\n await super.initialSyncFs();\n // No PG_VERSION: initdb runs next; PGlite calls syncToFs once before ready.\n this.freshInit =\n fsync && !nodeFs.existsSync(path.join(resolvedDir, \"PG_VERSION\"));\n if (this.freshInit) this.hostSyncs = false;\n const baseBytes = await dirBytes(path.join(resolvedDir, \"base\"));\n maintenance.start(this.pg!, resolvedDir, baseBytes);\n }\n\n async syncToFs(relaxedDurability?: boolean): Promise<void> {\n maintenance.noteSync();\n await super.syncToFs(relaxedDurability);\n if (!this.freshInit) return;\n this.freshInit = false;\n this.hostSyncs = true;\n syncTree(resolvedDir, hostFs);\n }\n\n async closeFs(): Promise<void> {\n try {\n await maintenance.stop();\n } finally {\n await super.closeFs();\n }\n }\n\n // WASI fd_sync convention: 0 or a positive errno.\n private fsyncStream(stream: NodeFsStream): number {\n if (!this.hostSyncs) return 0;\n // Directory streams carry no host fd; only regular files do.\n if (typeof stream.nfd !== \"number\") return 0;\n try {\n hostFs.fsyncSync(stream.nfd);\n } catch (err) {\n logger?.warn(`DurableNodeFs fsync failed: ${String(err)}`);\n return EIO;\n }\n return 0;\n }\n\n // Linux syscall convention: 0 or a negative errno.\n private fdatasync(fd: number): number {\n if (!this.hostSyncs) return 0;\n let stream: NodeFsStream;\n try {\n stream = this.mod!.FS.getStreamChecked(fd);\n } catch {\n return -ERRNO_CODES.EBADF;\n }\n if (typeof stream.nfd !== \"number\") return 0;\n try {\n hostFs.fdatasyncSync(stream.nfd);\n } catch (err) {\n logger?.warn(`DurableNodeFs fdatasync failed: ${String(err)}`);\n return -EIO;\n }\n return 0;\n }\n }\n\n return new Durable(dataDir) as DurableNodeFs;\n}\n","import { promises as fs } from \"node:fs\";\nimport type { FileHandle } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { syncTree, type SyncTreeHostFs } from \"./sync-tree.js\";\n\n// v1: magic, u32 version, u32 count; then u8 type, u32 mode, path, u32 len, data.\nexport const SNAPSHOT_FILE_NAME = \"snapshot.bin\";\nconst MAGIC = new Uint8Array([0x50, 0x47, 0x4c, 0x41]);\nconst FORMAT_VERSION = 1;\nconst HEADER_SIZE = 12;\nconst ENTRY_PREFIX_SIZE = 9;\nconst SKIPPED_ENTRIES = new Set([\"postmaster.pid\"]);\n// Node rejects a single read above 2^31-1 bytes.\nconst DEFAULT_IO_CHUNK_SIZE = 64 * 1024 * 1024;\n\nexport interface ExtractSnapshotOptions {\n chunkSize?: number;\n /** Default true: one fsync pass over the tree after the last write. */\n sync?: boolean;\n hostFs?: SyncTreeHostFs;\n}\n\nexport interface ExtractedSnapshot {\n pgVersionMajor: number;\n /** `global/pg_control` bytes; absent only in synthetic trees. */\n pgControl?: Buffer;\n /** Entries written to disk; skipped entries are not counted. */\n entries: number;\n /** Size of the snapshot file. */\n bytes: number;\n}\n\n/** Unpacks a snapshot into the existing `outDir`. */\nexport async function extractSnapshot(\n snapshotPath: string,\n outDir: string,\n options: ExtractSnapshotOptions = {},\n): Promise<ExtractedSnapshot> {\n const fh = await fs.open(snapshotPath, \"r\");\n try {\n const { size } = await fh.stat();\n return await extractFromHandle(fh, size, outDir, options);\n } finally {\n await fh.close();\n }\n}\n\nasync function extractFromHandle(\n fh: FileHandle,\n fileSize: number,\n outDir: string,\n options: ExtractSnapshotOptions,\n): Promise<ExtractedSnapshot> {\n const root = path.resolve(outDir);\n const chunkSize = Math.min(\n options.chunkSize ?? DEFAULT_IO_CHUNK_SIZE,\n Math.max(1, fileSize),\n );\n const reader = new ChunkedReader(fh, chunkSize);\n\n const header = await reader.take(HEADER_SIZE);\n for (let i = 0; i < MAGIC.length; i++) {\n if (header[i] !== MAGIC[i]) {\n throw new Error(\"snapshot.bin: invalid snapshot magic\");\n }\n }\n const version = header.readUInt32LE(4);\n if (version !== FORMAT_VERSION) {\n throw new Error(`snapshot.bin: unsupported snapshot version ${version}`);\n }\n const count = header.readUInt32LE(8);\n\n const decoder = new TextDecoder();\n let pgVersionText: string | undefined;\n let pgControl: Buffer | undefined;\n let written = 0;\n\n for (let i = 0; i < count; i++) {\n const prefix = await reader.take(ENTRY_PREFIX_SIZE);\n const type = prefix.readUInt8(0);\n const mode = prefix.readUInt32LE(1) & 0o7777;\n const pathLen = prefix.readUInt32LE(5);\n const relPath = decoder.decode(await reader.take(pathLen));\n const dataLen = (await reader.take(4)).readUInt32LE(0);\n const data = await reader.take(dataLen);\n\n const full = resolveInside(root, relPath);\n if (SKIPPED_ENTRIES.has(relPath)) continue;\n\n if (type === 0) {\n await fs.mkdir(full, { recursive: true });\n await fs.chmod(full, dirMode(mode));\n } else if (type === 1) {\n await fs.mkdir(path.dirname(full), { recursive: true });\n await fs.writeFile(full, data);\n await fs.chmod(full, mode);\n if (relPath === \"PG_VERSION\") pgVersionText = data.toString(\"utf8\");\n if (relPath === \"global/pg_control\") pgControl = data;\n } else {\n throw new Error(`snapshot.bin: unknown entry type ${type}`);\n }\n written++;\n }\n\n if (pgVersionText === undefined) {\n throw new Error(\"snapshot.bin: no PG_VERSION entry\");\n }\n const pgVersionMajor = Number.parseInt(pgVersionText.trim(), 10);\n if (!Number.isInteger(pgVersionMajor) || pgVersionMajor <= 0) {\n throw new Error(\n `snapshot.bin: unreadable PG_VERSION ${JSON.stringify(pgVersionText)}`,\n );\n }\n\n if (options.sync ?? true) syncTree(root, options.hostFs);\n\n return { pgVersionMajor, pgControl, entries: written, bytes: fileSize };\n}\n\nfunction resolveInside(root: string, relPath: string): string {\n const full = path.resolve(root, relPath);\n if (full !== root && !full.startsWith(root + path.sep)) {\n throw new Error(`snapshot.bin: entry escapes the data dir: ${relPath}`);\n }\n return full;\n}\n\n/** NTFS has no execute bit; a Windows snapshot can carry untraversable dirs. */\nexport function dirMode(mode: number): number {\n return (mode & 0o7777) | 0o111;\n}\n\nclass ChunkedReader {\n private readonly buf: Buffer;\n private start = 0;\n private end = 0;\n private position = 0;\n\n constructor(\n private readonly fh: Pick<FileHandle, \"read\">,\n chunkSize: number,\n ) {\n this.buf = Buffer.allocUnsafe(Math.max(1, chunkSize));\n }\n\n async take(n: number): Promise<Buffer> {\n const out = Buffer.allocUnsafe(n);\n let filled = 0;\n while (filled < n) {\n if (this.start === this.end) {\n if (n - filled >= this.buf.byteLength) {\n filled += await this.readInto(out, filled, n - filled);\n continue;\n }\n this.start = 0;\n this.end = await this.readInto(this.buf, 0, this.buf.byteLength);\n }\n const k = Math.min(this.end - this.start, n - filled);\n this.buf.copy(out, filled, this.start, this.start + k);\n this.start += k;\n filled += k;\n }\n return out;\n }\n\n private async readInto(\n target: Buffer,\n offset: number,\n length: number,\n ): Promise<number> {\n const { bytesRead } = await this.fh.read(\n target,\n offset,\n Math.min(length, this.buf.byteLength),\n this.position,\n );\n if (bytesRead <= 0) {\n throw new Error(\"snapshot.bin: truncated snapshot\");\n }\n this.position += bytesRead;\n return bytesRead;\n }\n}\n","import type { ILogger } from \"document-model\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { extractSnapshot, SNAPSHOT_FILE_NAME } from \"./snapshot-reader.js\";\nimport { syncDirectory, syncTree, type SyncTreeHostFs } from \"./sync-tree.js\";\n\nexport interface VerifyHandle {\n query<T>(sql: string): Promise<{ rows: T[] }>;\n close(): Promise<void>;\n}\n\nexport type ConversionStep =\n | \"extract\"\n | \"verify\"\n | \"renameOld\"\n | \"renameNew\"\n | \"removeOld\";\n\nexport interface ConversionDeps {\n /** Opens `dataDir` with the stock NodeFS of the PGlite module for `major`. */\n openForVerify: (major: number, dataDir: string) => Promise<VerifyHandle>;\n logger: ILogger;\n /** Test hook: throw after a named step to simulate a crash there. */\n afterStep?: (step: ConversionStep) => void | Promise<void>;\n hostFs?: SyncTreeHostFs;\n}\n\n// Backoff doubles per retry; five keeps a scanner-held file to ~3 s.\nconst RM_OPTIONS = {\n recursive: true,\n force: true,\n maxRetries: 5,\n retryDelay: 100,\n} as const;\n\ninterface Siblings {\n dir: string;\n converting: string;\n old: string;\n snapshot: string;\n}\n\nfunction siblingsOf(dir: string): Siblings {\n const resolved = path.resolve(dir);\n return {\n dir: resolved,\n converting: `${resolved}.converting`,\n old: `${resolved}.old`,\n snapshot: path.join(resolved, SNAPSHOT_FILE_NAME),\n };\n}\n\n/** Settles the debris of an interrupted conversion; runs before it at boot. */\nexport async function recoverConversion(\n dir: string,\n deps: ConversionDeps,\n): Promise<void> {\n const s = siblingsOf(dir);\n const [dirExists, convertingExists, oldExists] = await Promise.all([\n exists(s.dir),\n exists(s.converting),\n exists(s.old),\n ]);\n\n if (!dirExists) {\n if (convertingExists) {\n // Died between the two renames: finish the swap.\n await fs.rename(s.converting, s.dir);\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n deps.logger.warn(\n `PGlite conversion of ${s.dir} was interrupted after the swap started; completed it`,\n );\n return;\n }\n if (oldExists) {\n throw new Error(\n `PGlite data dir ${s.dir} is missing but ${s.old} exists; refusing to initialize beside it. Inspect and rename or remove ${s.old}.`,\n );\n }\n return;\n }\n\n const snapshotExists = await exists(s.snapshot);\n if (snapshotExists) {\n // The snapshot is authoritative; anything beside it is debris.\n if (convertingExists) await fs.rm(s.converting, RM_OPTIONS);\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n return;\n }\n\n // dir is a converted PGDATA; anything beside it is superseded.\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n if (convertingExists) {\n deps.logger.warn(\n `Removing stray ${s.converting}; ${s.dir} holds no snapshot and is authoritative`,\n );\n await fs.rm(s.converting, RM_OPTIONS);\n }\n}\n\n/** Extracts into `.converting`, verifies by opening it, swaps through `.old`. */\nexport async function convertSnapshotDir(\n dir: string,\n deps: ConversionDeps,\n): Promise<\"converted\" | \"none\"> {\n const s = siblingsOf(dir);\n if (!(await exists(s.snapshot))) return \"none\";\n\n const startedAt = performance.now();\n await fs.rm(s.converting, RM_OPTIONS);\n await fs.mkdir(s.converting, { mode: 0o700 });\n\n let snapshotBytes: number;\n let entries: number;\n try {\n ({ bytes: snapshotBytes, entries } = await extractAndVerify(s, deps));\n } catch (err) {\n await fs.rm(s.converting, RM_OPTIONS);\n throw err;\n }\n\n // Parent synced after each rename: a power loss lands on one recovery row.\n const parent = path.dirname(s.dir);\n await fs.rename(s.dir, s.old);\n syncDirectory(parent, deps.hostFs);\n await deps.afterStep?.(\"renameOld\");\n await fs.rename(s.converting, s.dir);\n syncDirectory(parent, deps.hostFs);\n await deps.afterStep?.(\"renameNew\");\n try {\n await fs.rm(s.old, RM_OPTIONS);\n } catch (err) {\n // The converted dir is in place; recoverConversion removes .old next boot.\n deps.logger.warn(\n `Could not remove ${s.old} after conversion; it is removed at the next boot: ${String(err)}`,\n );\n }\n await deps.afterStep?.(\"removeOld\");\n\n const convertedBytes = await treeBytes(s.dir);\n const durationMs = Math.round(performance.now() - startedAt);\n deps.logger.info(\n `Converted PGlite snapshot at ${s.dir}: snapshot ${snapshotBytes} bytes -> ${convertedBytes} bytes on disk, ${entries} entries, ${durationMs} ms`,\n );\n return \"converted\";\n}\n\nasync function extractAndVerify(\n s: Siblings,\n deps: ConversionDeps,\n): Promise<{ bytes: number; entries: number }> {\n const extracted = await extractSnapshot(s.snapshot, s.converting, {\n sync: false,\n });\n await deps.afterStep?.(\"extract\");\n\n if (!extracted.pgControl) {\n throw new Error(`${s.snapshot} has no global/pg_control entry`);\n }\n const expectedIdentifier = extracted.pgControl.readBigUInt64LE(0).toString();\n const handle = await deps.openForVerify(\n extracted.pgVersionMajor,\n s.converting,\n );\n try {\n await verifyOpenStore(handle, expectedIdentifier);\n } finally {\n // Must close before the renames; Windows refuses to rename an open dir.\n await handle.close();\n }\n await fs.rm(path.join(s.converting, \"postmaster.pid\"), { force: true });\n if (await exists(path.join(s.converting, SNAPSHOT_FILE_NAME))) {\n throw new Error(\n `Extracted PGlite data dir ${s.converting} contains ${SNAPSHOT_FILE_NAME}`,\n );\n }\n // One pass covers the extraction and the verify open's own writes.\n syncTree(s.converting, deps.hostFs);\n await deps.afterStep?.(\"verify\");\n return { bytes: extracted.bytes, entries: extracted.entries };\n}\n\nasync function verifyOpenStore(\n handle: VerifyHandle,\n expectedIdentifier: string,\n): Promise<void> {\n // numeric parses to a float above 2^53; compare as text.\n const ident = await handle.query<{ system_identifier: string }>(\n \"SELECT system_identifier::text AS system_identifier FROM pg_control_system()\",\n );\n const actual = ident.rows[0]?.system_identifier;\n if (actual !== expectedIdentifier) {\n throw new Error(\n `PGlite system identifier mismatch after extraction: pg_control says ${expectedIdentifier}, server says ${String(actual)}`,\n );\n }\n const tables = await handle.query<{ qualified: string }>(\n \"SELECT format('%I.%I', schemaname, tablename) AS qualified FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY 1\",\n );\n for (const { qualified } of tables.rows) {\n await handle.query(`SELECT count(*) FROM ${qualified}`);\n }\n}\n\nasync function exists(p: string): Promise<boolean> {\n try {\n await fs.lstat(p);\n return true;\n } catch {\n return false;\n }\n}\n\nasync function treeBytes(dir: string): Promise<number> {\n let total = 0;\n const entries = await fs.readdir(dir, { withFileTypes: true });\n for (const entry of entries) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n total += await treeBytes(full);\n } else if (entry.isFile()) {\n total += (await fs.stat(full)).size;\n }\n }\n return total;\n}\n","import { PGlite } from \"@electric-sql/pglite\";\nimport { NodeFS } from \"@electric-sql/pglite/nodefs\";\nimport type { ILogger } from \"document-model\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport {\n convertSnapshotDir,\n recoverConversion,\n type ConversionDeps,\n type VerifyHandle,\n} from \"./convert-snapshot-dir.js\";\n\n/** PG major of the pinned @electric-sql/pglite; it exports none, so the test checks initdb. */\nexport const CURRENT_PGLITE_MAJOR = 17;\n\n/** `openForVerify` over the loaded PGlite module only. */\nexport async function openCurrentPgliteForVerify(\n major: number,\n dataDir: string,\n): Promise<VerifyHandle> {\n if (major !== CURRENT_PGLITE_MAJOR) {\n throw new Error(\n `PGlite snapshot at ${dataDir} is PG${major}; this process only opens PG${CURRENT_PGLITE_MAJOR} data dirs`,\n );\n }\n const pg = new PGlite({ fs: new NodeFS(dataDir) });\n await pg.waitReady;\n return pg;\n}\n\n/** Converts a snapshot store and clears the files a previous run leaves behind. */\nexport async function preparePgliteDataDir(\n dir: string,\n deps: ConversionDeps,\n): Promise<void> {\n await recoverConversion(dir, deps);\n await convertSnapshotDir(dir, deps);\n await removeStalePgliteFiles(dir, deps.logger);\n}\n\n/** PGlite leaves `postmaster.pid` after every close and `pg_wal/xlogtemp.*` after some. */\nexport async function removeStalePgliteFiles(\n dir: string,\n logger: Pick<ILogger, \"debug\">,\n): Promise<void> {\n const lockfile = path.join(dir, \"postmaster.pid\");\n let hadLockfile = true;\n try {\n await fs.unlink(lockfile);\n } catch {\n hadLockfile = false;\n }\n if (hadLockfile) logger.debug(`Removed PGlite lockfile ${lockfile}`);\n\n const walDir = path.join(dir, \"pg_wal\");\n let names: string[];\n try {\n names = await fs.readdir(walDir);\n } catch {\n return;\n }\n for (const name of names) {\n if (!name.startsWith(\"xlogtemp.\")) continue;\n await fs.rm(path.join(walDir, name), { force: true });\n logger.debug(`Removed PGlite temp WAL file ${path.join(walDir, name)}`);\n }\n}\n"],"names":["fs","fs","fs","fs"],"mappings":";;;;;;;;AAIA,MAAa,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I;OACxB,IAAI,MAAM,OAAO,GACtB,UAAU,MAAMA,SAAG,KAAK,IAAI,EAAA,CAAG;CAEnC;CACA,OAAO;AACT;;;;ACpNA,MAAa,uBAAuB;;AAGpC,eAAsB,2BACpB,OACA,SACuB;CACvB,IAAI,UAAA,IACF,MAAM,IAAI,MACR,sBAAsB,QAAQ,QAAQ,MAAM,yCAC9C;CAEF,MAAM,KAAK,IAAI,OAAO,EAAE,IAAI,IAAI,OAAO,OAAO,EAAE,CAAC;CACjD,MAAM,GAAG;CACT,OAAO;AACT;;AAGA,eAAsB,qBACpB,KACA,MACe;CACf,MAAM,kBAAkB,KAAK,IAAI;CACjC,MAAM,mBAAmB,KAAK,IAAI;CAClC,MAAM,uBAAuB,KAAK,KAAK,MAAM;AAC/C;;AAGA,eAAsB,uBACpB,KACA,QACe;CACf,MAAM,WAAW,KAAK,KAAK,KAAK,gBAAgB;CAChD,IAAI,cAAc;CAClB,IAAI;EACF,MAAMC,SAAG,OAAO,QAAQ;CAC1B,QAAQ;EACN,cAAc;CAChB;CACA,IAAI,aAAa,OAAO,MAAM,2BAA2B,UAAU;CAEnE,MAAM,SAAS,KAAK,KAAK,KAAK,QAAQ;CACtC,IAAI;CACJ,IAAI;EACF,QAAQ,MAAMA,SAAG,QAAQ,MAAM;CACjC,QAAQ;EACN;CACF;CACA,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,CAAC,KAAK,WAAW,WAAW,GAAG;EACnC,MAAMA,SAAG,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG,EAAE,OAAO,KAAK,CAAC;EACpD,OAAO,MAAM,gCAAgC,KAAK,KAAK,QAAQ,IAAI,GAAG;CACxE;AACF","debug_id":"55b8b30a-561d-551d-9fe3-6288c8b09ae1"}
|
|
1
|
+
{"version":3,"file":"pglite-node-C-UQXrjU.mjs","sources":["../src/pglite/maintenance.ts","../src/pglite/sync-tree.ts","../src/pglite/durable-node-fs.ts","../src/pglite/snapshot-reader.ts","../src/pglite/convert-snapshot-dir.ts","../src/pglite/preflight.ts"],"sourcesContent":["import { protocol, type PGlite } from \"@electric-sql/pglite\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\n\nexport const DEFAULT_MAINTENANCE_INTERVAL_MS = 5 * 60 * 1000;\nexport const DEFAULT_VACUUM_FULL_ABOVE_BYTES = 256 * 1024 * 1024;\nconst MAINTENANCE_RETRY_MS = 1000;\n\nexport type MaintenanceOutcome =\n | \"vacuum\"\n | \"vacuum-full\"\n | \"idle\"\n | \"not-ready\"\n | \"in-transaction\"\n | \"failed\";\n\nexport interface MaintenanceLogger {\n warn(message: string): void;\n}\n\nexport interface MaintenanceOptions {\n /** VACUUM + CHECKPOINT cadence; PGlite has no autovacuum. Default 5 min; 0 disables. */\n intervalMs?: number;\n /** First pass runs VACUUM FULL when `base/` exceeds this at open. Default 256 MB; 0 disables. */\n vacuumFullAboveBytes?: number;\n logger?: MaintenanceLogger;\n}\n\nexport class PgliteMaintenance {\n private readonly intervalMs: number;\n private readonly vacuumFullAboveBytes: number;\n private readonly logger?: MaintenanceLogger;\n\n private pg?: PGlite;\n private label = \"\";\n private closing = false;\n private syncsSinceMaintenance = 0;\n private vacuumFullPending = false;\n private timer?: ReturnType<typeof setTimeout>;\n private inFlight?: Promise<MaintenanceOutcome>;\n\n constructor(options: MaintenanceOptions = {}) {\n this.intervalMs = Math.max(\n 0,\n options.intervalMs ?? DEFAULT_MAINTENANCE_INTERVAL_MS,\n );\n this.vacuumFullAboveBytes = Math.max(\n 0,\n options.vacuumFullAboveBytes ?? DEFAULT_VACUUM_FULL_ABOVE_BYTES,\n );\n this.logger = options.logger;\n }\n\n get scheduled(): boolean {\n return this.timer !== undefined;\n }\n\n get running(): boolean {\n return this.inFlight !== undefined;\n }\n\n start(pg: PGlite, label: string, baseBytes: number): void {\n this.pg = pg;\n this.label = label;\n this.vacuumFullPending =\n this.vacuumFullAboveBytes > 0 && baseBytes > this.vacuumFullAboveBytes;\n this.schedule(this.intervalMs);\n }\n\n noteSync(): void {\n this.syncsSinceMaintenance++;\n }\n\n async stop(): Promise<void> {\n this.closing = true;\n this.cancel();\n await this.inFlight;\n }\n\n async run(): Promise<MaintenanceOutcome> {\n const pg = this.pg;\n if (this.closing || !pg?.ready) return \"not-ready\";\n // pg.close() does not take the query mutex; recheck before each statement.\n const isReady = (): boolean => pg.ready && !this.closing;\n if (this.syncsSinceMaintenance === 0 && !this.vacuumFullPending) {\n return \"idle\";\n }\n const full = this.vacuumFullPending;\n let outcome: MaintenanceOutcome;\n try {\n outcome = await pg._runExclusiveQuery(async () => {\n // isInTransaction() misses START TRANSACTION; ReadyForQuery status does not.\n if (!isReady()) return \"not-ready\";\n const { messages } = await pg.execProtocol(\n protocol.serialize.query(\"\"),\n { syncToFs: false },\n );\n if (transactionStatus(messages) !== \"I\") return \"in-transaction\";\n if (full) {\n this.logger?.warn(\n `DurableNodeFs: ${this.label}/base exceeds ${this.vacuumFullAboveBytes} bytes; running VACUUM FULL`,\n );\n }\n for (const statement of [\n full ? \"VACUUM FULL\" : \"VACUUM\",\n \"CHECKPOINT\",\n ]) {\n if (!isReady()) return \"not-ready\";\n await pg.execProtocol(protocol.serialize.query(statement), {\n syncToFs: false,\n });\n }\n return full ? \"vacuum-full\" : \"vacuum\";\n });\n } catch (err) {\n this.logger?.warn(\n `DurableNodeFs maintenance failed: ${errorMessage(err)}`,\n );\n return \"failed\";\n }\n if (outcome !== \"vacuum\" && outcome !== \"vacuum-full\") return outcome;\n this.syncsSinceMaintenance = 0;\n this.vacuumFullPending = false;\n return outcome;\n }\n\n private schedule(delayMs: number): void {\n if (this.intervalMs === 0 || this.closing) return;\n this.timer = setTimeout(() => {\n this.timer = undefined;\n this.inFlight = this.run()\n .then((outcome) => {\n this.schedule(\n outcome === \"in-transaction\"\n ? Math.min(MAINTENANCE_RETRY_MS, this.intervalMs)\n : this.intervalMs,\n );\n return outcome;\n })\n .finally(() => {\n this.inFlight = undefined;\n });\n }, delayMs);\n this.timer.unref();\n }\n\n private cancel(): void {\n if (!this.timer) return;\n clearTimeout(this.timer);\n this.timer = undefined;\n }\n}\n\n/** Total size of the regular files under `dir`; 0 when `dir` is missing. */\nexport async function dirBytes(dir: string): Promise<number> {\n let entries;\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === \"ENOENT\") return 0;\n throw err;\n }\n let total = 0;\n for (const entry of entries) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n total += await dirBytes(full);\n } else if (entry.isFile()) {\n total += (await fs.stat(full)).size;\n }\n }\n return total;\n}\n\nfunction transactionStatus(messages: readonly { name: string }[]): unknown {\n for (let i = messages.length - 1; i >= 0; i--) {\n const m = messages[i];\n if (m.name === \"readyForQuery\") return (m as { status?: unknown }).status;\n }\n return undefined;\n}\n\nfunction errorMessage(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n","import nodeFs from \"node:fs\";\nimport path from \"node:path\";\n\nexport interface SyncTreeHostFs {\n fsyncSync(fd: number): void;\n}\n\nexport interface SyncTreeResult {\n files: number;\n dirs: number;\n}\n\n/** One pass over a finished tree; interleaving fsync with writes costs seconds. */\nexport function syncTree(\n root: string,\n hostFs: SyncTreeHostFs = nodeFs,\n): SyncTreeResult {\n const result: SyncTreeResult = { files: 0, dirs: 0 };\n const pending = [path.resolve(root)];\n while (pending.length > 0) {\n const dir = pending.pop()!;\n for (const entry of nodeFs.readdirSync(dir, { withFileTypes: true })) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n pending.push(full);\n } else if (entry.isFile()) {\n syncFile(full, hostFs);\n result.files++;\n }\n }\n syncDirectory(dir, hostFs);\n result.dirs++;\n }\n return result;\n}\n\n// Windows refuses FlushFileBuffers on a read-only handle (EPERM).\nfunction syncFile(file: string, hostFs: SyncTreeHostFs): void {\n const fd = nodeFs.openSync(file, \"r+\");\n try {\n hostFs.fsyncSync(fd);\n } finally {\n nodeFs.closeSync(fd);\n }\n}\n\n/** fsyncs a directory's entries; false where the platform refuses (Windows). */\nexport function syncDirectory(\n dir: string,\n hostFs: SyncTreeHostFs = nodeFs,\n): boolean {\n let fd: number;\n try {\n fd = nodeFs.openSync(dir, \"r\");\n } catch {\n return false;\n }\n try {\n hostFs.fsyncSync(fd);\n return true;\n } catch {\n return false;\n } finally {\n nodeFs.closeSync(fd);\n }\n}\n","import type { PGlite } from \"@electric-sql/pglite\";\nimport { ERRNO_CODES } from \"@electric-sql/pglite/basefs\";\nimport type { NodeFS } from \"@electric-sql/pglite/nodefs\";\nimport nodeFs from \"node:fs\";\nimport path from \"node:path\";\nimport { dirBytes, PgliteMaintenance } from \"./maintenance.js\";\nimport { syncTree } from \"./sync-tree.js\";\n\nexport type NodeFsClass = typeof NodeFS;\n\n// Emscripten/WASI errno; the basefs ERRNO_CODES export lacks it.\nconst EIO = 29;\n\nexport interface DurableNodeFsOptions {\n /** Default true; `PH_PGLITE_FSYNC=0` turns it off (power-loss durability off). */\n fsync?: boolean;\n /** Default 5 min; 0 disables. */\n maintenanceIntervalMs?: number;\n /** `base/` size at open; default 256 MB; 0 disables. */\n vacuumFullAboveBytes?: number;\n logger?: { warn(message: string): void };\n /** Emscripten abort (a Postgres PANIC, e.g. ENOSPC on WAL). */\n onAbort?: (what: unknown) => void;\n hostFs?: Pick<typeof nodeFs, \"fsyncSync\" | \"fdatasyncSync\">;\n}\n\nexport type DurableNodeFs = InstanceType<NodeFsClass> & {\n readonly maintenance: PgliteMaintenance;\n};\n\nexport function resolvePgliteFsync(\n env: Record<string, string | undefined>,\n): boolean {\n return env.PH_PGLITE_FSYNC !== \"0\";\n}\n\ninterface NodeFsStream {\n nfd?: number;\n /** Emscripten open flags; NODEFS opens the host fd with the same access mode. */\n flags?: number;\n}\n\n// Only PGlite's pg_flush_data (fsync on pre_sync_fname's O_RDONLY fd) syncs a\n// read-only fd: the result is discarded and an O_RDWR fsync of the same file\n// follows. Skipped, as Windows refuses FlushFileBuffers without write access.\nfunction isReadOnly(stream: NodeFsStream): boolean {\n return stream.flags !== undefined && (stream.flags & 3) === 0;\n}\n\ninterface EmscriptenFs {\n filesystems: { NODEFS: { stream_ops: Record<string, unknown> } };\n getStreamChecked(fd: number): NodeFsStream;\n quit(): void;\n}\n\ninterface EmscriptenMod {\n FS: EmscriptenFs;\n [wasmExport: string]: unknown;\n}\n\n// PGlite re-enters wasm from its own error handler after a query fails; on an\n// aborted runtime that spins forever, so every entry point is cut off first.\nconst WASM_ENTRY_POINTS = [\n \"_interactive_one\",\n \"_interactive_write\",\n \"_pgl_backend\",\n \"_pgl_initdb\",\n \"_pgl_shutdown\",\n];\n\nfunction poisonAfterAbort(mod: EmscriptenMod | undefined, what: unknown): void {\n if (!mod) return;\n const message = `PGlite aborted: ${String(what) || \"wasm runtime abort\"}`;\n for (const name of WASM_ENTRY_POINTS) {\n if (typeof mod[name] !== \"function\") continue;\n mod[name] = () => {\n throw new Error(message);\n };\n }\n // pg.close() rejects before closeFs(), so release the host fds here.\n try {\n mod.FS.quit();\n } catch {\n // already quit by an earlier abort\n }\n}\n\ntype SyscallImport = ((fd: number) => number) & { sig?: string };\n\ninterface WasmImports {\n env: Record<string, unknown> & { __syscall_fdatasync?: SyscallImport };\n}\n\ntype InstantiateWasm = (\n imports: WasmImports,\n done: (instance: WebAssembly.Instance, module: WebAssembly.Module) => void,\n) => unknown;\n\ninterface EmscriptenOpts {\n preRun?: ((mod: EmscriptenMod) => void)[];\n instantiateWasm?: InstantiateWasm;\n onAbort?: (what: unknown) => void;\n}\n\ntype InitOpts = Parameters<InstanceType<NodeFsClass>[\"init\"]>[1];\n\n/** Built over the passed-in class so PG16 dirs on `pglite-legacy-02` get the same behaviour. */\nexport function createDurableNodeFs(\n Base: NodeFsClass,\n dataDir: string,\n options: DurableNodeFsOptions = {},\n): DurableNodeFs {\n const fsync = options.fsync ?? true;\n const hostFs = options.hostFs ?? nodeFs;\n const logger = options.logger;\n const onAbort = options.onAbort;\n const resolvedDir = path.resolve(dataDir);\n const maintenance = new PgliteMaintenance({\n intervalMs: options.maintenanceIntervalMs,\n vacuumFullAboveBytes: options.vacuumFullAboveBytes,\n logger,\n });\n\n class Durable extends Base {\n readonly maintenance = maintenance;\n private mod?: EmscriptenMod;\n // Off from initialSyncFs of a fresh dir until PGlite's first syncToFs.\n private hostSyncs = fsync;\n private freshInit = false;\n\n async init(\n pg: PGlite,\n opts: InitOpts,\n ): Promise<{ emscriptenOpts: InitOpts }> {\n const { emscriptenOpts: base } = await super.init(pg, opts);\n const baseOpts = base as EmscriptenOpts;\n const inner = baseOpts.instantiateWasm;\n if (fsync && !inner) {\n throw new Error(\"PGlite did not supply instantiateWasm\");\n }\n const emscriptenOpts: EmscriptenOpts = {\n ...baseOpts,\n onAbort: (what) => {\n poisonAfterAbort(this.mod, what);\n void maintenance.stop();\n onAbort?.(what);\n },\n preRun: [\n ...(baseOpts.preRun ?? []),\n (mod) => {\n this.mod = mod;\n if (!fsync) return;\n mod.FS.filesystems.NODEFS.stream_ops.fsync = (\n stream: NodeFsStream,\n ) => this.fsyncStream(stream);\n },\n ],\n instantiateWasm:\n fsync && inner\n ? (imports, done) => {\n const original = imports.env.__syscall_fdatasync;\n const replacement: SyscallImport = (fd) => this.fdatasync(fd);\n replacement.sig = original?.sig;\n imports.env.__syscall_fdatasync = replacement;\n return inner(imports, done);\n }\n : inner,\n };\n return { emscriptenOpts: emscriptenOpts as InitOpts };\n }\n\n async initialSyncFs(): Promise<void> {\n await super.initialSyncFs();\n // No PG_VERSION: initdb runs next; PGlite calls syncToFs once before ready.\n this.freshInit =\n fsync && !nodeFs.existsSync(path.join(resolvedDir, \"PG_VERSION\"));\n if (this.freshInit) this.hostSyncs = false;\n const baseBytes = await dirBytes(path.join(resolvedDir, \"base\"));\n maintenance.start(this.pg!, resolvedDir, baseBytes);\n }\n\n async syncToFs(relaxedDurability?: boolean): Promise<void> {\n maintenance.noteSync();\n await super.syncToFs(relaxedDurability);\n if (!this.freshInit) return;\n this.freshInit = false;\n this.hostSyncs = true;\n syncTree(resolvedDir, hostFs);\n }\n\n async closeFs(): Promise<void> {\n try {\n await maintenance.stop();\n } finally {\n await super.closeFs();\n }\n }\n\n // WASI fd_sync convention: 0 or a positive errno.\n private fsyncStream(stream: NodeFsStream): number {\n if (!this.hostSyncs) return 0;\n // Directory streams carry no host fd; only regular files do.\n if (typeof stream.nfd !== \"number\" || isReadOnly(stream)) return 0;\n try {\n hostFs.fsyncSync(stream.nfd);\n } catch (err) {\n logger?.warn(`DurableNodeFs fsync failed: ${String(err)}`);\n return EIO;\n }\n return 0;\n }\n\n // Linux syscall convention: 0 or a negative errno.\n private fdatasync(fd: number): number {\n if (!this.hostSyncs) return 0;\n let stream: NodeFsStream;\n try {\n stream = this.mod!.FS.getStreamChecked(fd);\n } catch {\n return -ERRNO_CODES.EBADF;\n }\n if (typeof stream.nfd !== \"number\" || isReadOnly(stream)) return 0;\n try {\n hostFs.fdatasyncSync(stream.nfd);\n } catch (err) {\n logger?.warn(`DurableNodeFs fdatasync failed: ${String(err)}`);\n return -EIO;\n }\n return 0;\n }\n }\n\n return new Durable(dataDir) as DurableNodeFs;\n}\n","import { promises as fs } from \"node:fs\";\nimport type { FileHandle } from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { syncTree, type SyncTreeHostFs } from \"./sync-tree.js\";\n\n// v1: magic, u32 version, u32 count; then u8 type, u32 mode, path, u32 len, data.\nexport const SNAPSHOT_FILE_NAME = \"snapshot.bin\";\nconst MAGIC = new Uint8Array([0x50, 0x47, 0x4c, 0x41]);\nconst FORMAT_VERSION = 1;\nconst HEADER_SIZE = 12;\nconst ENTRY_PREFIX_SIZE = 9;\nconst SKIPPED_ENTRIES = new Set([\"postmaster.pid\"]);\n// Node rejects a single read above 2^31-1 bytes.\nconst DEFAULT_IO_CHUNK_SIZE = 64 * 1024 * 1024;\n\nexport interface ExtractSnapshotOptions {\n chunkSize?: number;\n /** Default true: one fsync pass over the tree after the last write. */\n sync?: boolean;\n hostFs?: SyncTreeHostFs;\n}\n\nexport interface ExtractedSnapshot {\n pgVersionMajor: number;\n /** `global/pg_control` bytes; absent only in synthetic trees. */\n pgControl?: Buffer;\n /** Entries written to disk; skipped entries are not counted. */\n entries: number;\n /** Size of the snapshot file. */\n bytes: number;\n}\n\n/** Unpacks a snapshot into the existing `outDir`. */\nexport async function extractSnapshot(\n snapshotPath: string,\n outDir: string,\n options: ExtractSnapshotOptions = {},\n): Promise<ExtractedSnapshot> {\n const fh = await fs.open(snapshotPath, \"r\");\n try {\n const { size } = await fh.stat();\n return await extractFromHandle(fh, size, outDir, options);\n } finally {\n await fh.close();\n }\n}\n\nasync function extractFromHandle(\n fh: FileHandle,\n fileSize: number,\n outDir: string,\n options: ExtractSnapshotOptions,\n): Promise<ExtractedSnapshot> {\n const root = path.resolve(outDir);\n const chunkSize = Math.min(\n options.chunkSize ?? DEFAULT_IO_CHUNK_SIZE,\n Math.max(1, fileSize),\n );\n const reader = new ChunkedReader(fh, chunkSize);\n\n const header = await reader.take(HEADER_SIZE);\n for (let i = 0; i < MAGIC.length; i++) {\n if (header[i] !== MAGIC[i]) {\n throw new Error(\"snapshot.bin: invalid snapshot magic\");\n }\n }\n const version = header.readUInt32LE(4);\n if (version !== FORMAT_VERSION) {\n throw new Error(`snapshot.bin: unsupported snapshot version ${version}`);\n }\n const count = header.readUInt32LE(8);\n\n const decoder = new TextDecoder();\n let pgVersionText: string | undefined;\n let pgControl: Buffer | undefined;\n let written = 0;\n\n for (let i = 0; i < count; i++) {\n const prefix = await reader.take(ENTRY_PREFIX_SIZE);\n const type = prefix.readUInt8(0);\n const mode = prefix.readUInt32LE(1) & 0o7777;\n const pathLen = prefix.readUInt32LE(5);\n const relPath = decoder.decode(await reader.take(pathLen));\n const dataLen = (await reader.take(4)).readUInt32LE(0);\n const data = await reader.take(dataLen);\n\n const full = resolveInside(root, relPath);\n if (SKIPPED_ENTRIES.has(relPath)) continue;\n\n if (type === 0) {\n await fs.mkdir(full, { recursive: true });\n await fs.chmod(full, dirMode(mode));\n } else if (type === 1) {\n await fs.mkdir(path.dirname(full), { recursive: true });\n await fs.writeFile(full, data);\n await fs.chmod(full, mode);\n if (relPath === \"PG_VERSION\") pgVersionText = data.toString(\"utf8\");\n if (relPath === \"global/pg_control\") pgControl = data;\n } else {\n throw new Error(`snapshot.bin: unknown entry type ${type}`);\n }\n written++;\n }\n\n if (pgVersionText === undefined) {\n throw new Error(\"snapshot.bin: no PG_VERSION entry\");\n }\n const pgVersionMajor = Number.parseInt(pgVersionText.trim(), 10);\n if (!Number.isInteger(pgVersionMajor) || pgVersionMajor <= 0) {\n throw new Error(\n `snapshot.bin: unreadable PG_VERSION ${JSON.stringify(pgVersionText)}`,\n );\n }\n\n if (options.sync ?? true) syncTree(root, options.hostFs);\n\n return { pgVersionMajor, pgControl, entries: written, bytes: fileSize };\n}\n\nfunction resolveInside(root: string, relPath: string): string {\n const full = path.resolve(root, relPath);\n if (full !== root && !full.startsWith(root + path.sep)) {\n throw new Error(`snapshot.bin: entry escapes the data dir: ${relPath}`);\n }\n return full;\n}\n\n/** NTFS has no execute bit; a Windows snapshot can carry untraversable dirs. */\nexport function dirMode(mode: number): number {\n return (mode & 0o7777) | 0o111;\n}\n\nclass ChunkedReader {\n private readonly buf: Buffer;\n private start = 0;\n private end = 0;\n private position = 0;\n\n constructor(\n private readonly fh: Pick<FileHandle, \"read\">,\n chunkSize: number,\n ) {\n this.buf = Buffer.allocUnsafe(Math.max(1, chunkSize));\n }\n\n async take(n: number): Promise<Buffer> {\n const out = Buffer.allocUnsafe(n);\n let filled = 0;\n while (filled < n) {\n if (this.start === this.end) {\n if (n - filled >= this.buf.byteLength) {\n filled += await this.readInto(out, filled, n - filled);\n continue;\n }\n this.start = 0;\n this.end = await this.readInto(this.buf, 0, this.buf.byteLength);\n }\n const k = Math.min(this.end - this.start, n - filled);\n this.buf.copy(out, filled, this.start, this.start + k);\n this.start += k;\n filled += k;\n }\n return out;\n }\n\n private async readInto(\n target: Buffer,\n offset: number,\n length: number,\n ): Promise<number> {\n const { bytesRead } = await this.fh.read(\n target,\n offset,\n Math.min(length, this.buf.byteLength),\n this.position,\n );\n if (bytesRead <= 0) {\n throw new Error(\"snapshot.bin: truncated snapshot\");\n }\n this.position += bytesRead;\n return bytesRead;\n }\n}\n","import type { ILogger } from \"document-model\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { extractSnapshot, SNAPSHOT_FILE_NAME } from \"./snapshot-reader.js\";\nimport { syncDirectory, syncTree, type SyncTreeHostFs } from \"./sync-tree.js\";\n\nexport interface VerifyHandle {\n query<T>(sql: string): Promise<{ rows: T[] }>;\n close(): Promise<void>;\n}\n\nexport type ConversionStep =\n | \"extract\"\n | \"verify\"\n | \"renameOld\"\n | \"renameNew\"\n | \"removeOld\";\n\nexport interface ConversionDeps {\n /** Opens `dataDir` with the stock NodeFS of the PGlite module for `major`. */\n openForVerify: (major: number, dataDir: string) => Promise<VerifyHandle>;\n logger: ILogger;\n /** Test hook: throw after a named step to simulate a crash there. */\n afterStep?: (step: ConversionStep) => void | Promise<void>;\n hostFs?: SyncTreeHostFs;\n}\n\n// Backoff doubles per retry; five keeps a scanner-held file to ~3 s.\nconst RM_OPTIONS = {\n recursive: true,\n force: true,\n maxRetries: 5,\n retryDelay: 100,\n} as const;\n\ninterface Siblings {\n dir: string;\n converting: string;\n old: string;\n snapshot: string;\n}\n\nfunction siblingsOf(dir: string): Siblings {\n const resolved = path.resolve(dir);\n return {\n dir: resolved,\n converting: `${resolved}.converting`,\n old: `${resolved}.old`,\n snapshot: path.join(resolved, SNAPSHOT_FILE_NAME),\n };\n}\n\n/** Settles the debris of an interrupted conversion; runs before it at boot. */\nexport async function recoverConversion(\n dir: string,\n deps: ConversionDeps,\n): Promise<void> {\n const s = siblingsOf(dir);\n const [dirExists, convertingExists, oldExists] = await Promise.all([\n exists(s.dir),\n exists(s.converting),\n exists(s.old),\n ]);\n\n if (!dirExists) {\n if (convertingExists) {\n // Died between the two renames: finish the swap.\n await fs.rename(s.converting, s.dir);\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n deps.logger.warn(\n `PGlite conversion of ${s.dir} was interrupted after the swap started; completed it`,\n );\n return;\n }\n if (oldExists) {\n throw new Error(\n `PGlite data dir ${s.dir} is missing but ${s.old} exists; refusing to initialize beside it. Inspect and rename or remove ${s.old}.`,\n );\n }\n return;\n }\n\n const snapshotExists = await exists(s.snapshot);\n if (snapshotExists) {\n // The snapshot is authoritative; anything beside it is debris.\n if (convertingExists) await fs.rm(s.converting, RM_OPTIONS);\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n return;\n }\n\n // dir is a converted PGDATA; anything beside it is superseded.\n if (oldExists) await fs.rm(s.old, RM_OPTIONS);\n if (convertingExists) {\n deps.logger.warn(\n `Removing stray ${s.converting}; ${s.dir} holds no snapshot and is authoritative`,\n );\n await fs.rm(s.converting, RM_OPTIONS);\n }\n}\n\n/** Extracts into `.converting`, verifies by opening it, swaps through `.old`. */\nexport async function convertSnapshotDir(\n dir: string,\n deps: ConversionDeps,\n): Promise<\"converted\" | \"none\"> {\n const s = siblingsOf(dir);\n if (!(await exists(s.snapshot))) return \"none\";\n\n const startedAt = performance.now();\n await fs.rm(s.converting, RM_OPTIONS);\n await fs.mkdir(s.converting, { mode: 0o700 });\n\n let snapshotBytes: number;\n let entries: number;\n try {\n ({ bytes: snapshotBytes, entries } = await extractAndVerify(s, deps));\n } catch (err) {\n await fs.rm(s.converting, RM_OPTIONS);\n throw err;\n }\n\n // Parent synced after each rename: a power loss lands on one recovery row.\n const parent = path.dirname(s.dir);\n await fs.rename(s.dir, s.old);\n syncDirectory(parent, deps.hostFs);\n await deps.afterStep?.(\"renameOld\");\n await fs.rename(s.converting, s.dir);\n syncDirectory(parent, deps.hostFs);\n await deps.afterStep?.(\"renameNew\");\n try {\n await fs.rm(s.old, RM_OPTIONS);\n } catch (err) {\n // The converted dir is in place; recoverConversion removes .old next boot.\n deps.logger.warn(\n `Could not remove ${s.old} after conversion; it is removed at the next boot: ${String(err)}`,\n );\n }\n await deps.afterStep?.(\"removeOld\");\n\n const convertedBytes = await treeBytes(s.dir);\n const durationMs = Math.round(performance.now() - startedAt);\n deps.logger.info(\n `Converted PGlite snapshot at ${s.dir}: snapshot ${snapshotBytes} bytes -> ${convertedBytes} bytes on disk, ${entries} entries, ${durationMs} ms`,\n );\n return \"converted\";\n}\n\nasync function extractAndVerify(\n s: Siblings,\n deps: ConversionDeps,\n): Promise<{ bytes: number; entries: number }> {\n const extracted = await extractSnapshot(s.snapshot, s.converting, {\n sync: false,\n });\n await deps.afterStep?.(\"extract\");\n\n if (!extracted.pgControl) {\n throw new Error(`${s.snapshot} has no global/pg_control entry`);\n }\n const expectedIdentifier = extracted.pgControl.readBigUInt64LE(0).toString();\n const handle = await deps.openForVerify(\n extracted.pgVersionMajor,\n s.converting,\n );\n try {\n await verifyOpenStore(handle, expectedIdentifier);\n } finally {\n // Must close before the renames; Windows refuses to rename an open dir.\n await handle.close();\n }\n await fs.rm(path.join(s.converting, \"postmaster.pid\"), { force: true });\n if (await exists(path.join(s.converting, SNAPSHOT_FILE_NAME))) {\n throw new Error(\n `Extracted PGlite data dir ${s.converting} contains ${SNAPSHOT_FILE_NAME}`,\n );\n }\n // One pass covers the extraction and the verify open's own writes.\n syncTree(s.converting, deps.hostFs);\n await deps.afterStep?.(\"verify\");\n return { bytes: extracted.bytes, entries: extracted.entries };\n}\n\nasync function verifyOpenStore(\n handle: VerifyHandle,\n expectedIdentifier: string,\n): Promise<void> {\n // numeric parses to a float above 2^53; compare as text.\n const ident = await handle.query<{ system_identifier: string }>(\n \"SELECT system_identifier::text AS system_identifier FROM pg_control_system()\",\n );\n const actual = ident.rows[0]?.system_identifier;\n if (actual !== expectedIdentifier) {\n throw new Error(\n `PGlite system identifier mismatch after extraction: pg_control says ${expectedIdentifier}, server says ${String(actual)}`,\n );\n }\n const tables = await handle.query<{ qualified: string }>(\n \"SELECT format('%I.%I', schemaname, tablename) AS qualified FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema') ORDER BY 1\",\n );\n for (const { qualified } of tables.rows) {\n await handle.query(`SELECT count(*) FROM ${qualified}`);\n }\n}\n\nasync function exists(p: string): Promise<boolean> {\n try {\n await fs.lstat(p);\n return true;\n } catch {\n return false;\n }\n}\n\nasync function treeBytes(dir: string): Promise<number> {\n let total = 0;\n const entries = await fs.readdir(dir, { withFileTypes: true });\n for (const entry of entries) {\n const full = path.join(dir, entry.name);\n if (entry.isDirectory()) {\n total += await treeBytes(full);\n } else if (entry.isFile()) {\n total += (await fs.stat(full)).size;\n }\n }\n return total;\n}\n","import { PGlite } from \"@electric-sql/pglite\";\nimport { NodeFS } from \"@electric-sql/pglite/nodefs\";\nimport type { ILogger } from \"document-model\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport {\n convertSnapshotDir,\n recoverConversion,\n type ConversionDeps,\n type VerifyHandle,\n} from \"./convert-snapshot-dir.js\";\n\n/** PG major of the pinned @electric-sql/pglite; it exports none, so the test checks initdb. */\nexport const CURRENT_PGLITE_MAJOR = 17;\n\n/** `openForVerify` over the loaded PGlite module only. */\nexport async function openCurrentPgliteForVerify(\n major: number,\n dataDir: string,\n): Promise<VerifyHandle> {\n if (major !== CURRENT_PGLITE_MAJOR) {\n throw new Error(\n `PGlite snapshot at ${dataDir} is PG${major}; this process only opens PG${CURRENT_PGLITE_MAJOR} data dirs`,\n );\n }\n const pg = new PGlite({ fs: new NodeFS(dataDir) });\n await pg.waitReady;\n return pg;\n}\n\n/** Converts a snapshot store and clears the files a previous run leaves behind. */\nexport async function preparePgliteDataDir(\n dir: string,\n deps: ConversionDeps,\n): Promise<void> {\n await recoverConversion(dir, deps);\n await convertSnapshotDir(dir, deps);\n await removeStalePgliteFiles(dir, deps.logger);\n}\n\n/** PGlite leaves `postmaster.pid` after every close and `pg_wal/xlogtemp.*` after some. */\nexport async function removeStalePgliteFiles(\n dir: string,\n logger: Pick<ILogger, \"debug\">,\n): Promise<void> {\n const lockfile = path.join(dir, \"postmaster.pid\");\n let hadLockfile = true;\n try {\n await fs.unlink(lockfile);\n } catch {\n hadLockfile = false;\n }\n if (hadLockfile) logger.debug(`Removed PGlite lockfile ${lockfile}`);\n\n const walDir = path.join(dir, \"pg_wal\");\n let names: string[];\n try {\n names = await fs.readdir(walDir);\n } catch {\n return;\n }\n for (const name of names) {\n if (!name.startsWith(\"xlogtemp.\")) continue;\n await fs.rm(path.join(walDir, name), { force: true });\n logger.debug(`Removed PGlite temp WAL file ${path.join(walDir, name)}`);\n }\n}\n"],"names":["fs","fs","fs","fs"],"mappings":";;;;;;;;AAIA,MAAa,kCAAkC;AAC/C,MAAa,kCAAkC;AAC/C,MAAM,uBAAuB;AAsB7B,IAAa,oBAAb,MAA+B;CAC7B;CACA;CACA;CAEA;CACA,QAAgB;CAChB,UAAkB;CAClB,wBAAgC;CAChC,oBAA4B;CAC5B;CACA;CAEA,YAAY,UAA8B,CAAC,GAAG;EAC5C,KAAK,aAAa,KAAK,IACrB,GACA,QAAQ,cAAA,GACV;EACA,KAAK,uBAAuB,KAAK,IAC/B,GACA,QAAQ,wBAAA,SACV;EACA,KAAK,SAAS,QAAQ;CACxB;CAEA,IAAI,YAAqB;EACvB,OAAO,KAAK,UAAU,KAAA;CACxB;CAEA,IAAI,UAAmB;EACrB,OAAO,KAAK,aAAa,KAAA;CAC3B;CAEA,MAAM,IAAY,OAAe,WAAyB;EACxD,KAAK,KAAK;EACV,KAAK,QAAQ;EACb,KAAK,oBACH,KAAK,uBAAuB,KAAK,YAAY,KAAK;EACpD,KAAK,SAAS,KAAK,UAAU;CAC/B;CAEA,WAAiB;EACf,KAAK;CACP;CAEA,MAAM,OAAsB;EAC1B,KAAK,UAAU;EACf,KAAK,OAAO;EACZ,MAAM,KAAK;CACb;CAEA,MAAM,MAAmC;EACvC,MAAM,KAAK,KAAK;EAChB,IAAI,KAAK,WAAW,CAAC,IAAI,OAAO,OAAO;EAEvC,MAAM,gBAAyB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AAS,KAAK,KAAK,KAAK,QAAQ;CACtC,IAAI;CACJ,IAAI;EACF,QAAQ,MAAMA,SAAG,QAAQ,MAAM;CACjC,QAAQ;EACN;CACF;CACA,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,CAAC,KAAK,WAAW,WAAW,GAAG;EACnC,MAAMA,SAAG,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG,EAAE,OAAO,KAAK,CAAC;EACpD,OAAO,MAAM,gCAAgC,KAAK,KAAK,QAAQ,IAAI,GAAG;CACxE;AACF","debug_id":"3247c4a7-6c83-5f29-9c5e-66bdf6069d43"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as ReservedTypeNames } from "../../create-schema-BUk-IQCT.mjs";
|
|
2
2
|
import { DocumentNode } from "graphql";
|
|
3
3
|
import { DocumentModelGlobalState } from "@powerhousedao/shared/document-model";
|
|
4
4
|
//#region src/graphql/subgraph-type-ownership.d.ts
|
|
@@ -7,31 +7,37 @@ type OwnershipConflict = {
|
|
|
7
7
|
owner: string;
|
|
8
8
|
};
|
|
9
9
|
//#endregion
|
|
10
|
-
//#region src/graphql/subgraph-
|
|
10
|
+
//#region src/graphql/subgraph-schema-owner.d.ts
|
|
11
11
|
/** Owner of the types every generated document-model schema carries. */
|
|
12
12
|
export declare const DOCUMENT_MODEL_SCHEMA_OWNER = "document-model schema";
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/graphql/subgraph-types.d.ts
|
|
15
15
|
/** A package's own subgraph, in the order the package exports it. */
|
|
16
16
|
export type PackageSubgraph = {
|
|
17
17
|
name: string;
|
|
18
18
|
typeDefs: DocumentNode;
|
|
19
19
|
};
|
|
20
20
|
export type PackageSubgraphConflicts = {
|
|
21
|
-
/**
|
|
22
|
-
nameHeldBy?: "core" | "document model";
|
|
21
|
+
/** Who takes the name; "package" is a later same-name subgraph. */
|
|
22
|
+
nameHeldBy?: "core" | "document model" | "package";
|
|
23
23
|
/** Types and root fields the subgraph loses. */
|
|
24
24
|
claims: OwnershipConflict[];
|
|
25
25
|
};
|
|
26
|
+
export type RefusedDocumentModel = {
|
|
27
|
+
name: string;
|
|
28
|
+
reason: string;
|
|
29
|
+
/** Only the model's own subgraph is refused; its types are served. */
|
|
30
|
+
subgraphOnly: boolean;
|
|
31
|
+
};
|
|
26
32
|
export type PackageSubgraphReport = {
|
|
27
33
|
/** By subgraph name; subgraphs without a conflict are left out. */
|
|
28
34
|
conflicts: Map<string, PackageSubgraphConflicts>;
|
|
29
|
-
/**
|
|
30
|
-
|
|
35
|
+
/** The package's models, or their subgraphs, the server refuses. */
|
|
36
|
+
refusedModels: RefusedDocumentModel[];
|
|
31
37
|
};
|
|
32
|
-
/**
|
|
33
|
-
export declare function findPackageSubgraphConflicts(subgraphs: readonly PackageSubgraph[], documentModels: readonly DocumentModelGlobalState[]): PackageSubgraphReport
|
|
38
|
+
/** Loaded on call: the check pulls in the base models and core schemas. */
|
|
39
|
+
export declare function findPackageSubgraphConflicts(subgraphs: readonly PackageSubgraph[], documentModels: readonly DocumentModelGlobalState[]): Promise<PackageSubgraphReport>;
|
|
34
40
|
//#endregion
|
|
35
|
-
export {
|
|
41
|
+
export type { OwnershipConflict, ReservedTypeNames };
|
|
36
42
|
export {};
|
|
37
43
|
//# sourceMappingURL=subgraph-types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subgraph-types.d.mts","names":[],"sources":["../../../src/graphql/subgraph-type-ownership.ts","../../../src/graphql/subgraph-types.ts"],"mappings":";;;;KAqJY;EAAsB;EAAe;;;;;
|
|
1
|
+
{"version":3,"file":"subgraph-types.d.mts","names":[],"sources":["../../../src/graphql/subgraph-type-ownership.ts","../../../src/graphql/subgraph-schema-owner.ts","../../../src/graphql/subgraph-types.ts"],"mappings":";;;;KAqJY;EAAsB;EAAe;;;;;qBCpJpC;;;;YCSD;EAAoB;EAAc,UAAU;;YAE5C;;EAEV;;EAEA,QAAQ;;YAGE;EACV;EACA;;EAEA;;YAGU;;EAEV,WAAW,YAAY;;EAEvB,eAAe;;;wBAIK,6BACpB,oBAAoB,mBACpB,yBAAyB,6BACxB,QAAQ"}
|
|
@@ -1,105 +1,14 @@
|
|
|
1
1
|
|
|
2
|
-
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="
|
|
3
|
-
import {
|
|
4
|
-
import { typedefs } from "@powerhousedao/analytics-engine-graphql";
|
|
5
|
-
import { gql } from "graphql-tag";
|
|
2
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="31efa70a-ce7c-5c46-b58e-114594b7ff58")}catch(e){}}();
|
|
3
|
+
import { t as DOCUMENT_MODEL_SCHEMA_OWNER } from "../../subgraph-schema-owner-BEjuk2Xv.mjs";
|
|
6
4
|
//#region src/graphql/subgraph-types.ts
|
|
7
|
-
/**
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
["analytics", gql(typedefs)],
|
|
12
|
-
["system", systemTypeDefs],
|
|
13
|
-
["r", gql(schema_default)],
|
|
14
|
-
["inspection", inspectionTypeDefs],
|
|
15
|
-
["auth", gql(schema_default$1)]
|
|
16
|
-
]);
|
|
17
|
-
function claimsOf(typeDefs, generated) {
|
|
18
|
-
try {
|
|
19
|
-
return collectSubgraphClaims(typeDefs, generated);
|
|
20
|
-
} catch {
|
|
21
|
-
return /* @__PURE__ */ new Map();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
/** Package subgraphs the server refuses or excludes; host subgraphs unchecked. */
|
|
25
|
-
function findPackageSubgraphConflicts(subgraphs, documentModels) {
|
|
26
|
-
const models = [];
|
|
27
|
-
const modelSubgraphs = [];
|
|
28
|
-
const skippedModels = [];
|
|
29
|
-
const allModels = [...baseDocumentModels.map(([, model]) => model.documentModel.global), ...documentModels];
|
|
30
|
-
for (const model of allModels) {
|
|
31
|
-
let typeDefs;
|
|
32
|
-
try {
|
|
33
|
-
generatedTypesOf([model]);
|
|
34
|
-
if (hasOperationSchemas(model)) typeDefs = documentModelSubgraphTypes(model).typeDefs;
|
|
35
|
-
} catch {
|
|
36
|
-
skippedModels.push(model.name);
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
models.push(model);
|
|
40
|
-
const name = getDocumentModelSubgraphName(model);
|
|
41
|
-
if (typeDefs && !modelSubgraphs.some((owner) => owner.name === name)) modelSubgraphs.push({
|
|
42
|
-
name,
|
|
43
|
-
typeDefs
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
const generated = generatedTypesOf(models);
|
|
47
|
-
const fixed = generatedTypesOf([]).names;
|
|
48
|
-
const generatedOwners = /* @__PURE__ */ new Map();
|
|
49
|
-
for (const model of models) for (const name of generatedTypesOf([model]).names) if (!fixed.has(name)) generatedOwners.set(name, getDocumentModelSubgraphName(model));
|
|
50
|
-
const conflicts = /* @__PURE__ */ new Map();
|
|
51
|
-
const admitted = [];
|
|
52
|
-
for (const subgraph of subgraphs) {
|
|
53
|
-
if (coreSubgraphTypeDefs.has(subgraph.name)) {
|
|
54
|
-
conflicts.set(subgraph.name, {
|
|
55
|
-
nameHeldBy: "core",
|
|
56
|
-
claims: []
|
|
57
|
-
});
|
|
58
|
-
continue;
|
|
59
|
-
}
|
|
60
|
-
if (modelSubgraphs.some((owner) => owner.name === subgraph.name)) {
|
|
61
|
-
conflicts.set(subgraph.name, {
|
|
62
|
-
nameHeldBy: "document model",
|
|
63
|
-
claims: []
|
|
64
|
-
});
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
let collisions;
|
|
68
|
-
try {
|
|
69
|
-
collisions = findSubgraphTypeCollisions(generated, subgraph.typeDefs);
|
|
70
|
-
} catch {
|
|
71
|
-
collisions = [];
|
|
72
|
-
}
|
|
73
|
-
if (collisions.length > 0) {
|
|
74
|
-
conflicts.set(subgraph.name, { claims: collisions.map((claim) => ({
|
|
75
|
-
claim,
|
|
76
|
-
owner: generatedOwners.get(claim) ?? "document-model schema"
|
|
77
|
-
})) });
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
admitted.push(subgraph);
|
|
81
|
-
}
|
|
82
|
-
const packageNames = new Set(admitted.map(({ name }) => name));
|
|
83
|
-
const excluded = resolveClaimOwnership([
|
|
84
|
-
...Array.from(coreSubgraphTypeDefs, ([name, typeDefs]) => ({
|
|
85
|
-
name,
|
|
86
|
-
typeDefs
|
|
87
|
-
})),
|
|
88
|
-
...modelSubgraphs,
|
|
89
|
-
...admitted
|
|
90
|
-
].map(({ name, typeDefs }) => ({
|
|
91
|
-
candidate: name,
|
|
92
|
-
name,
|
|
93
|
-
claims: claimsOf(typeDefs, generated.names)
|
|
94
|
-
})));
|
|
95
|
-
for (const [name, claims] of excluded) if (packageNames.has(name)) conflicts.set(name, { claims });
|
|
96
|
-
return {
|
|
97
|
-
conflicts,
|
|
98
|
-
skippedModels
|
|
99
|
-
};
|
|
5
|
+
/** Loaded on call: the check pulls in the base models and core schemas. */
|
|
6
|
+
async function findPackageSubgraphConflicts(subgraphs, documentModels) {
|
|
7
|
+
const { checkPackageSubgraphs } = await import("../../package-subgraph-check-CsTPajAN.mjs");
|
|
8
|
+
return checkPackageSubgraphs(subgraphs, documentModels);
|
|
100
9
|
}
|
|
101
10
|
//#endregion
|
|
102
|
-
export { DOCUMENT_MODEL_SCHEMA_OWNER,
|
|
11
|
+
export { DOCUMENT_MODEL_SCHEMA_OWNER, findPackageSubgraphConflicts };
|
|
103
12
|
|
|
104
13
|
//# sourceMappingURL=subgraph-types.mjs.map
|
|
105
|
-
//# debugId=
|
|
14
|
+
//# debugId=31efa70a-ce7c-5c46-b58e-114594b7ff58
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subgraph-types.mjs","sources":["../../../src/graphql/subgraph-types.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"subgraph-types.mjs","sources":["../../../src/graphql/subgraph-types.ts"],"sourcesContent":["import type { DocumentModelGlobalState } from \"@powerhousedao/shared/document-model\";\nimport type { DocumentNode } from \"graphql\";\nimport type { OwnershipConflict } from \"./subgraph-type-ownership.js\";\n\nexport type { ReservedTypeNames } from \"../utils/create-schema.js\";\nexport type { OwnershipConflict } from \"./subgraph-type-ownership.js\";\n\nexport { DOCUMENT_MODEL_SCHEMA_OWNER } from \"./subgraph-schema-owner.js\";\n\n/** A package's own subgraph, in the order the package exports it. */\nexport type PackageSubgraph = { name: string; typeDefs: DocumentNode };\n\nexport type PackageSubgraphConflicts = {\n /** Who takes the name; \"package\" is a later same-name subgraph. */\n nameHeldBy?: \"core\" | \"document model\" | \"package\";\n /** Types and root fields the subgraph loses. */\n claims: OwnershipConflict[];\n};\n\nexport type RefusedDocumentModel = {\n name: string;\n reason: string;\n /** Only the model's own subgraph is refused; its types are served. */\n subgraphOnly: boolean;\n};\n\nexport type PackageSubgraphReport = {\n /** By subgraph name; subgraphs without a conflict are left out. */\n conflicts: Map<string, PackageSubgraphConflicts>;\n /** The package's models, or their subgraphs, the server refuses. */\n refusedModels: RefusedDocumentModel[];\n};\n\n/** Loaded on call: the check pulls in the base models and core schemas. */\nexport async function findPackageSubgraphConflicts(\n subgraphs: readonly PackageSubgraph[],\n documentModels: readonly DocumentModelGlobalState[],\n): Promise<PackageSubgraphReport> {\n const { checkPackageSubgraphs } = await import(\"./package-subgraph-check.js\");\n return checkPackageSubgraphs(subgraphs, documentModels);\n}\n"],"names":[],"mappings":";;;;;AAkCA,eAAsB,6BACpB,WACA,gBACgC;CAChC,MAAM,EAAE,0BAA0B,MAAM,OAAO;CAC/C,OAAO,sBAAsB,WAAW,cAAc;AACxD","debug_id":"31efa70a-ce7c-5c46-b58e-114594b7ff58"}
|