@secondlayer/shared 7.8.0 → 7.9.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/src/archive/derived-stage-report.d.ts +99 -0
- package/dist/src/archive/derived-stage-report.js +64 -0
- package/dist/src/archive/derived-stage-report.js.map +10 -0
- package/dist/src/archive/disk-guard.d.ts +46 -0
- package/dist/src/archive/disk-guard.js +69 -0
- package/dist/src/archive/disk-guard.js.map +10 -0
- package/dist/src/archive/per-height-digest.d.ts +99 -0
- package/dist/src/archive/per-height-digest.js +194 -0
- package/dist/src/archive/per-height-digest.js.map +11 -0
- package/dist/src/archive/range-digest.d.ts +7 -0
- package/dist/src/archive/semantic-digest-builder.d.ts +1094 -0
- package/dist/src/archive/semantic-digest-builder.js +302 -0
- package/dist/src/archive/semantic-digest-builder.js.map +11 -0
- package/dist/src/archive/semantic-digest.d.ts +131 -0
- package/dist/src/archive/semantic-digest.js +143 -0
- package/dist/src/archive/semantic-digest.js.map +10 -0
- package/dist/src/db/index.d.ts +11 -1
- package/dist/src/db/index.js +26 -1
- package/dist/src/db/index.js.map +6 -5
- package/dist/src/db/queries/chain-reorgs.d.ts +7 -0
- package/dist/src/db/queries/chain-reorgs.js +25 -1
- package/dist/src/db/queries/chain-reorgs.js.map +6 -5
- package/dist/src/db/queries/contracts.d.ts +7 -0
- package/dist/src/db/queries/instance.d.ts +1038 -0
- package/dist/src/db/queries/instance.js +92 -0
- package/dist/src/db/queries/instance.js.map +10 -0
- package/dist/src/db/queries/integrity.d.ts +7 -0
- package/dist/src/db/queries/subgraph-gaps.d.ts +7 -0
- package/dist/src/db/queries/subgraph-operations.d.ts +7 -0
- package/dist/src/db/queries/subgraphs.d.ts +10 -1
- package/dist/src/db/queries/subgraphs.js +47 -23
- package/dist/src/db/queries/subgraphs.js.map +7 -7
- package/dist/src/db/queries/subscriptions.d.ts +7 -0
- package/dist/src/db/queries/subscriptions.js +47 -31
- package/dist/src/db/queries/subscriptions.js.map +5 -5
- package/dist/src/db/schema.d.ts +8 -1
- package/dist/src/index.d.ts +11 -1
- package/dist/src/index.js +26 -1
- package/dist/src/index.js.map +6 -5
- package/dist/src/node/client.d.ts +11 -1
- package/dist/src/node/client.js +42 -4
- package/dist/src/node/client.js.map +3 -3
- package/dist/src/node/local-client.d.ts +7 -0
- package/dist/src/node/nakamoto.d.ts +22 -7
- package/dist/src/node/nakamoto.js +46 -5
- package/dist/src/node/nakamoto.js.map +3 -3
- package/migrations/0115_instances.ts +33 -0
- package/package.json +25 -1
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __returnValue = (v) => v;
|
|
4
|
+
function __exportSetter(name, newValue) {
|
|
5
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
6
|
+
}
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
set: __exportSetter.bind(all, name)
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/db/queries/instance.ts
|
|
18
|
+
var INSTANCE_NETWORKS = ["mainnet", "testnet", "devnet"];
|
|
19
|
+
|
|
20
|
+
class InstanceNetworkMismatchError extends Error {
|
|
21
|
+
existing;
|
|
22
|
+
requested;
|
|
23
|
+
name = "InstanceNetworkMismatchError";
|
|
24
|
+
constructor(existing, requested) {
|
|
25
|
+
super(`instance network is ${existing}; cannot change to ${requested}`);
|
|
26
|
+
this.existing = existing;
|
|
27
|
+
this.requested = requested;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class InvalidInstanceNetworkError extends Error {
|
|
32
|
+
value;
|
|
33
|
+
name = "InvalidInstanceNetworkError";
|
|
34
|
+
constructor(value) {
|
|
35
|
+
super(`instance network must be mainnet, testnet, or devnet (got ${value})`);
|
|
36
|
+
this.value = value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function parseInstanceNetwork(value) {
|
|
40
|
+
const network = value.trim().toLowerCase();
|
|
41
|
+
if (INSTANCE_NETWORKS.includes(network)) {
|
|
42
|
+
return network;
|
|
43
|
+
}
|
|
44
|
+
throw new InvalidInstanceNetworkError(value);
|
|
45
|
+
}
|
|
46
|
+
function instanceNetworkFromEnv(env = process.env) {
|
|
47
|
+
return parseInstanceNetwork(env.STACKS_NETWORK ?? env.NETWORK ?? "mainnet");
|
|
48
|
+
}
|
|
49
|
+
async function getInstance(db) {
|
|
50
|
+
const row = await db.selectFrom("instances").select(["id", "network", "created_at"]).executeTakeFirst();
|
|
51
|
+
return row ? toInstance(row) : null;
|
|
52
|
+
}
|
|
53
|
+
async function ensureInstance(db, input) {
|
|
54
|
+
const existing = await getInstance(db);
|
|
55
|
+
if (existing) {
|
|
56
|
+
if (existing.network !== input.network) {
|
|
57
|
+
throw new InstanceNetworkMismatchError(existing.network, input.network);
|
|
58
|
+
}
|
|
59
|
+
return existing;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const inserted = await db.insertInto("instances").values({ network: input.network }).returning(["id", "network", "created_at"]).executeTakeFirstOrThrow();
|
|
63
|
+
return toInstance(inserted);
|
|
64
|
+
} catch {
|
|
65
|
+
const raced = await getInstance(db);
|
|
66
|
+
if (!raced)
|
|
67
|
+
throw new Error("failed to create instance");
|
|
68
|
+
if (raced.network !== input.network) {
|
|
69
|
+
throw new InstanceNetworkMismatchError(raced.network, input.network);
|
|
70
|
+
}
|
|
71
|
+
return raced;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function toInstance(row) {
|
|
75
|
+
return {
|
|
76
|
+
id: row.id,
|
|
77
|
+
network: parseInstanceNetwork(row.network),
|
|
78
|
+
created_at: row.created_at
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export {
|
|
82
|
+
parseInstanceNetwork,
|
|
83
|
+
instanceNetworkFromEnv,
|
|
84
|
+
getInstance,
|
|
85
|
+
ensureInstance,
|
|
86
|
+
InvalidInstanceNetworkError,
|
|
87
|
+
InstanceNetworkMismatchError,
|
|
88
|
+
INSTANCE_NETWORKS
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
//# debugId=52C8BB0A1418347D64756E2164756E21
|
|
92
|
+
//# sourceMappingURL=instance.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/db/queries/instance.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { Kysely } from \"kysely\";\nimport type { Database } from \"../types.ts\";\n\nexport const INSTANCE_NETWORKS = [\"mainnet\", \"testnet\", \"devnet\"] as const;\nexport type InstanceNetwork = (typeof INSTANCE_NETWORKS)[number];\n\nexport type InstanceRow = {\n\tid: string;\n\tnetwork: InstanceNetwork;\n\tcreated_at: Date;\n};\n\nexport class InstanceNetworkMismatchError extends Error {\n\treadonly name = \"InstanceNetworkMismatchError\";\n\tconstructor(\n\t\treadonly existing: InstanceNetwork,\n\t\treadonly requested: InstanceNetwork,\n\t) {\n\t\tsuper(`instance network is ${existing}; cannot change to ${requested}`);\n\t}\n}\n\nexport class InvalidInstanceNetworkError extends Error {\n\treadonly name = \"InvalidInstanceNetworkError\";\n\tconstructor(readonly value: string) {\n\t\tsuper(\n\t\t\t`instance network must be mainnet, testnet, or devnet (got ${value})`,\n\t\t);\n\t}\n}\n\nexport function parseInstanceNetwork(value: string): InstanceNetwork {\n\tconst network = value.trim().toLowerCase();\n\tif ((INSTANCE_NETWORKS as readonly string[]).includes(network)) {\n\t\treturn network as InstanceNetwork;\n\t}\n\tthrow new InvalidInstanceNetworkError(value);\n}\n\n/** STACKS_NETWORK, then NETWORK, then mainnet. */\nexport function instanceNetworkFromEnv(\n\tenv: NodeJS.ProcessEnv = process.env,\n): InstanceNetwork {\n\treturn parseInstanceNetwork(env.STACKS_NETWORK ?? env.NETWORK ?? \"mainnet\");\n}\n\nexport async function getInstance(\n\tdb: Kysely<Database>,\n): Promise<InstanceRow | null> {\n\tconst row = await db\n\t\t.selectFrom(\"instances\")\n\t\t.select([\"id\", \"network\", \"created_at\"])\n\t\t.executeTakeFirst();\n\treturn row ? toInstance(row) : null;\n}\n\n/**\n * Return the singleton instance, creating it on first call.\n * A second call with a different network is refused — the network is\n * immutable for the life of the database.\n */\nexport async function ensureInstance(\n\tdb: Kysely<Database>,\n\tinput: { network: InstanceNetwork },\n): Promise<InstanceRow> {\n\tconst existing = await getInstance(db);\n\tif (existing) {\n\t\tif (existing.network !== input.network) {\n\t\t\tthrow new InstanceNetworkMismatchError(existing.network, input.network);\n\t\t}\n\t\treturn existing;\n\t}\n\ttry {\n\t\tconst inserted = await db\n\t\t\t.insertInto(\"instances\")\n\t\t\t.values({ network: input.network })\n\t\t\t.returning([\"id\", \"network\", \"created_at\"])\n\t\t\t.executeTakeFirstOrThrow();\n\t\treturn toInstance(inserted);\n\t} catch {\n\t\t// Lost the singleton race — the other writer won.\n\t\tconst raced = await getInstance(db);\n\t\tif (!raced) throw new Error(\"failed to create instance\");\n\t\tif (raced.network !== input.network) {\n\t\t\tthrow new InstanceNetworkMismatchError(raced.network, input.network);\n\t\t}\n\t\treturn raced;\n\t}\n}\n\nfunction toInstance(row: {\n\tid: string;\n\tnetwork: string;\n\tcreated_at: Date;\n}): InstanceRow {\n\treturn {\n\t\tid: row.id,\n\t\tnetwork: parseInstanceNetwork(row.network),\n\t\tcreated_at: row.created_at,\n\t};\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAGO,IAAM,oBAAoB,CAAC,WAAW,WAAW,QAAQ;AAAA;AASzD,MAAM,qCAAqC,MAAM;AAAA,EAG7C;AAAA,EACA;AAAA,EAHD,OAAO;AAAA,EAChB,WAAW,CACD,UACA,WACR;AAAA,IACD,MAAM,uBAAuB,8BAA8B,WAAW;AAAA,IAH7D;AAAA,IACA;AAAA;AAIX;AAAA;AAEO,MAAM,oCAAoC,MAAM;AAAA,EAEjC;AAAA,EADZ,OAAO;AAAA,EAChB,WAAW,CAAU,OAAe;AAAA,IACnC,MACC,6DAA6D,QAC9D;AAAA,IAHoB;AAAA;AAKtB;AAEO,SAAS,oBAAoB,CAAC,OAAgC;AAAA,EACpE,MAAM,UAAU,MAAM,KAAK,EAAE,YAAY;AAAA,EACzC,IAAK,kBAAwC,SAAS,OAAO,GAAG;AAAA,IAC/D,OAAO;AAAA,EACR;AAAA,EACA,MAAM,IAAI,4BAA4B,KAAK;AAAA;AAIrC,SAAS,sBAAsB,CACrC,MAAyB,QAAQ,KACf;AAAA,EAClB,OAAO,qBAAqB,IAAI,kBAAkB,IAAI,WAAW,SAAS;AAAA;AAG3E,eAAsB,WAAW,CAChC,IAC8B;AAAA,EAC9B,MAAM,MAAM,MAAM,GAChB,WAAW,WAAW,EACtB,OAAO,CAAC,MAAM,WAAW,YAAY,CAAC,EACtC,iBAAiB;AAAA,EACnB,OAAO,MAAM,WAAW,GAAG,IAAI;AAAA;AAQhC,eAAsB,cAAc,CACnC,IACA,OACuB;AAAA,EACvB,MAAM,WAAW,MAAM,YAAY,EAAE;AAAA,EACrC,IAAI,UAAU;AAAA,IACb,IAAI,SAAS,YAAY,MAAM,SAAS;AAAA,MACvC,MAAM,IAAI,6BAA6B,SAAS,SAAS,MAAM,OAAO;AAAA,IACvE;AAAA,IACA,OAAO;AAAA,EACR;AAAA,EACA,IAAI;AAAA,IACH,MAAM,WAAW,MAAM,GACrB,WAAW,WAAW,EACtB,OAAO,EAAE,SAAS,MAAM,QAAQ,CAAC,EACjC,UAAU,CAAC,MAAM,WAAW,YAAY,CAAC,EACzC,wBAAwB;AAAA,IAC1B,OAAO,WAAW,QAAQ;AAAA,IACzB,MAAM;AAAA,IAEP,MAAM,QAAQ,MAAM,YAAY,EAAE;AAAA,IAClC,IAAI,CAAC;AAAA,MAAO,MAAM,IAAI,MAAM,2BAA2B;AAAA,IACvD,IAAI,MAAM,YAAY,MAAM,SAAS;AAAA,MACpC,MAAM,IAAI,6BAA6B,MAAM,SAAS,MAAM,OAAO;AAAA,IACpE;AAAA,IACA,OAAO;AAAA;AAAA;AAIT,SAAS,UAAU,CAAC,KAIJ;AAAA,EACf,OAAO;AAAA,IACN,IAAI,IAAI;AAAA,IACR,SAAS,qBAAqB,IAAI,OAAO;AAAA,IACzC,YAAY,IAAI;AAAA,EACjB;AAAA;",
|
|
8
|
+
"debugId": "52C8BB0A1418347D64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -191,6 +191,12 @@ interface ApiKeysTable {
|
|
|
191
191
|
revoked_at: Date | null;
|
|
192
192
|
created_at: Generated<Date>;
|
|
193
193
|
}
|
|
194
|
+
/** Singleton self-host / publisher identity. Network is immutable. */
|
|
195
|
+
interface InstancesTable {
|
|
196
|
+
id: Generated<string>;
|
|
197
|
+
network: string;
|
|
198
|
+
created_at: Generated<Date>;
|
|
199
|
+
}
|
|
194
200
|
interface AccountsTable {
|
|
195
201
|
id: Generated<string>;
|
|
196
202
|
/** NULL for unclaimed ghost accounts (anonymous self-serve keys). */
|
|
@@ -733,6 +739,7 @@ interface Database {
|
|
|
733
739
|
contracts: ContractsTable;
|
|
734
740
|
subgraphs: SubgraphsTable;
|
|
735
741
|
api_keys: ApiKeysTable;
|
|
742
|
+
instances: InstancesTable;
|
|
736
743
|
accounts: AccountsTable;
|
|
737
744
|
sessions: SessionsTable;
|
|
738
745
|
magic_links: MagicLinksTable;
|
|
@@ -191,6 +191,12 @@ interface ApiKeysTable {
|
|
|
191
191
|
revoked_at: Date | null;
|
|
192
192
|
created_at: Generated<Date>;
|
|
193
193
|
}
|
|
194
|
+
/** Singleton self-host / publisher identity. Network is immutable. */
|
|
195
|
+
interface InstancesTable {
|
|
196
|
+
id: Generated<string>;
|
|
197
|
+
network: string;
|
|
198
|
+
created_at: Generated<Date>;
|
|
199
|
+
}
|
|
194
200
|
interface AccountsTable {
|
|
195
201
|
id: Generated<string>;
|
|
196
202
|
/** NULL for unclaimed ghost accounts (anonymous self-serve keys). */
|
|
@@ -733,6 +739,7 @@ interface Database {
|
|
|
733
739
|
contracts: ContractsTable;
|
|
734
740
|
subgraphs: SubgraphsTable;
|
|
735
741
|
api_keys: ApiKeysTable;
|
|
742
|
+
instances: InstancesTable;
|
|
736
743
|
accounts: AccountsTable;
|
|
737
744
|
sessions: SessionsTable;
|
|
738
745
|
magic_links: MagicLinksTable;
|
|
@@ -191,6 +191,12 @@ interface ApiKeysTable {
|
|
|
191
191
|
revoked_at: Date | null;
|
|
192
192
|
created_at: Generated<Date>;
|
|
193
193
|
}
|
|
194
|
+
/** Singleton self-host / publisher identity. Network is immutable. */
|
|
195
|
+
interface InstancesTable {
|
|
196
|
+
id: Generated<string>;
|
|
197
|
+
network: string;
|
|
198
|
+
created_at: Generated<Date>;
|
|
199
|
+
}
|
|
194
200
|
interface AccountsTable {
|
|
195
201
|
id: Generated<string>;
|
|
196
202
|
/** NULL for unclaimed ghost accounts (anonymous self-serve keys). */
|
|
@@ -733,6 +739,7 @@ interface Database {
|
|
|
733
739
|
contracts: ContractsTable;
|
|
734
740
|
subgraphs: SubgraphsTable;
|
|
735
741
|
api_keys: ApiKeysTable;
|
|
742
|
+
instances: InstancesTable;
|
|
736
743
|
accounts: AccountsTable;
|
|
737
744
|
sessions: SessionsTable;
|
|
738
745
|
magic_links: MagicLinksTable;
|
|
@@ -192,6 +192,12 @@ interface ApiKeysTable {
|
|
|
192
192
|
revoked_at: Date | null;
|
|
193
193
|
created_at: Generated<Date>;
|
|
194
194
|
}
|
|
195
|
+
/** Singleton self-host / publisher identity. Network is immutable. */
|
|
196
|
+
interface InstancesTable {
|
|
197
|
+
id: Generated<string>;
|
|
198
|
+
network: string;
|
|
199
|
+
created_at: Generated<Date>;
|
|
200
|
+
}
|
|
195
201
|
interface AccountsTable {
|
|
196
202
|
id: Generated<string>;
|
|
197
203
|
/** NULL for unclaimed ghost accounts (anonymous self-serve keys). */
|
|
@@ -734,6 +740,7 @@ interface Database {
|
|
|
734
740
|
contracts: ContractsTable;
|
|
735
741
|
subgraphs: SubgraphsTable;
|
|
736
742
|
api_keys: ApiKeysTable;
|
|
743
|
+
instances: InstancesTable;
|
|
737
744
|
accounts: AccountsTable;
|
|
738
745
|
sessions: SessionsTable;
|
|
739
746
|
magic_links: MagicLinksTable;
|
|
@@ -1025,6 +1032,8 @@ declare function resolveSubgraphRawClient(subgraph: Subgraph): ReturnType<typeof
|
|
|
1025
1032
|
* Platform-mode deploys use `pgSchemaNameFor(accountId, name)`.
|
|
1026
1033
|
*/
|
|
1027
1034
|
declare function pgSchemaName(subgraphName: string): string;
|
|
1035
|
+
/** OSS writes empty account_id — name is the local unique key. */
|
|
1036
|
+
declare function localSubgraphAccountId(accountId?: string): string;
|
|
1028
1037
|
/**
|
|
1029
1038
|
* Account-scoped schema name. Matches migration 0028's rename pattern:
|
|
1030
1039
|
* subgraph_{first8charsOfAccountId, dashes-as-underscores}_{name}
|
|
@@ -1131,4 +1140,4 @@ declare function recordLiveProgress(db: Kysely<Database>, name: string, lastProc
|
|
|
1131
1140
|
* forward write between the rewind and the fork block's reprocess. Do not
|
|
1132
1141
|
* call this from the live catch-up path. */
|
|
1133
1142
|
declare function rewindLiveProgress(db: Kysely<Database>, name: string, lastProcessedBlock: number): Promise<void>;
|
|
1134
|
-
export { updateSubgraphVisibility, updateSubgraphStatus, updateSubgraphHandlerPath, updateSubgraphExpiry, subgraphDatabaseUrl, rewindLiveProgress, resolveSubgraphRawClient, resolveSubgraphDb, registerSubgraph, recordSubgraphProcessed, recordLiveProgress, recordLiveError, pgSchemaNameFor, pgSchemaName, listSubgraphs, isByoSubgraph, getSubgraph, findPublicSubgraphByName, encryptDatabaseUrl, deleteSubgraph };
|
|
1143
|
+
export { updateSubgraphVisibility, updateSubgraphStatus, updateSubgraphHandlerPath, updateSubgraphExpiry, subgraphDatabaseUrl, rewindLiveProgress, resolveSubgraphRawClient, resolveSubgraphDb, registerSubgraph, recordSubgraphProcessed, recordLiveProgress, recordLiveError, pgSchemaNameFor, pgSchemaName, localSubgraphAccountId, listSubgraphs, isByoSubgraph, getSubgraph, findPublicSubgraphByName, encryptDatabaseUrl, deleteSubgraph };
|
|
@@ -138,6 +138,22 @@ class Logger {
|
|
|
138
138
|
}
|
|
139
139
|
var logger = new Logger;
|
|
140
140
|
|
|
141
|
+
// src/mode.ts
|
|
142
|
+
var VALID_MODES = ["oss", "platform"];
|
|
143
|
+
function getInstanceMode() {
|
|
144
|
+
const raw = process.env.INSTANCE_MODE?.trim().toLowerCase();
|
|
145
|
+
if (raw && VALID_MODES.includes(raw)) {
|
|
146
|
+
return raw;
|
|
147
|
+
}
|
|
148
|
+
return "oss";
|
|
149
|
+
}
|
|
150
|
+
function isPlatformMode() {
|
|
151
|
+
return getInstanceMode() === "platform";
|
|
152
|
+
}
|
|
153
|
+
function isOssMode() {
|
|
154
|
+
return getInstanceMode() === "oss";
|
|
155
|
+
}
|
|
156
|
+
|
|
141
157
|
// src/db/jsonb.ts
|
|
142
158
|
import { sql } from "kysely";
|
|
143
159
|
function jsonb(value) {
|
|
@@ -723,6 +739,7 @@ var TABLE_TO_DB = {
|
|
|
723
739
|
bns_marketplace_events: "source",
|
|
724
740
|
bns_names: "source",
|
|
725
741
|
bns_namespaces: "source",
|
|
742
|
+
instances: "target",
|
|
726
743
|
accounts: "target",
|
|
727
744
|
api_keys: "target",
|
|
728
745
|
sessions: "target",
|
|
@@ -785,10 +802,17 @@ function evictIfNeeded() {
|
|
|
785
802
|
const evicted = lruEntry;
|
|
786
803
|
evicted.db.destroy().catch(() => {}).then(() => evicted.rawClient.end({ timeout: 5 })).catch(() => {});
|
|
787
804
|
}
|
|
805
|
+
function applicationDatabaseUrl() {
|
|
806
|
+
return process.env.DATABASE_URL || DEFAULT_URL;
|
|
807
|
+
}
|
|
788
808
|
function resolveSourceUrl() {
|
|
809
|
+
if (!isPlatformMode())
|
|
810
|
+
return applicationDatabaseUrl();
|
|
789
811
|
return process.env.SOURCE_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL;
|
|
790
812
|
}
|
|
791
813
|
function resolveTargetUrl() {
|
|
814
|
+
if (!isPlatformMode())
|
|
815
|
+
return applicationDatabaseUrl();
|
|
792
816
|
return process.env.TARGET_DATABASE_URL || process.env.DATABASE_URL || DEFAULT_URL;
|
|
793
817
|
}
|
|
794
818
|
function describeDbUrl(url) {
|
|
@@ -927,22 +951,6 @@ async function closeDb() {
|
|
|
927
951
|
}
|
|
928
952
|
pools.clear();
|
|
929
953
|
}
|
|
930
|
-
// src/mode.ts
|
|
931
|
-
var VALID_MODES = ["oss", "platform"];
|
|
932
|
-
function getInstanceMode() {
|
|
933
|
-
const raw = process.env.INSTANCE_MODE?.trim().toLowerCase();
|
|
934
|
-
if (raw && VALID_MODES.includes(raw)) {
|
|
935
|
-
return raw;
|
|
936
|
-
}
|
|
937
|
-
return "oss";
|
|
938
|
-
}
|
|
939
|
-
function isPlatformMode() {
|
|
940
|
-
return getInstanceMode() === "platform";
|
|
941
|
-
}
|
|
942
|
-
function isOssMode() {
|
|
943
|
-
return getInstanceMode() === "oss";
|
|
944
|
-
}
|
|
945
|
-
|
|
946
954
|
// src/crypto/secrets.ts
|
|
947
955
|
import { createCipheriv, createDecipheriv, randomBytes } from "node:crypto";
|
|
948
956
|
import {
|
|
@@ -1090,6 +1098,11 @@ function pgSchemaName(subgraphName) {
|
|
|
1090
1098
|
const safeName = subgraphName.replace(/-/g, "_");
|
|
1091
1099
|
return `subgraph_${safeName}`;
|
|
1092
1100
|
}
|
|
1101
|
+
function localSubgraphAccountId(accountId) {
|
|
1102
|
+
if (!isPlatformMode())
|
|
1103
|
+
return "";
|
|
1104
|
+
return accountId ?? "";
|
|
1105
|
+
}
|
|
1093
1106
|
function pgSchemaNameFor(accountId, subgraphName) {
|
|
1094
1107
|
if (!accountId)
|
|
1095
1108
|
return pgSchemaName(subgraphName);
|
|
@@ -1098,19 +1111,21 @@ function pgSchemaNameFor(accountId, subgraphName) {
|
|
|
1098
1111
|
return `subgraph_${accountPrefix}_${safeName}`;
|
|
1099
1112
|
}
|
|
1100
1113
|
async function registerSubgraph(db, data) {
|
|
1101
|
-
|
|
1114
|
+
const accountId = localSubgraphAccountId(data.accountId);
|
|
1115
|
+
const values = {
|
|
1102
1116
|
name: data.name,
|
|
1103
1117
|
version: data.version,
|
|
1104
1118
|
definition: jsonb(data.definition),
|
|
1105
1119
|
schema_hash: data.schemaHash,
|
|
1106
1120
|
handler_path: data.handlerPath,
|
|
1107
|
-
account_id:
|
|
1121
|
+
account_id: accountId,
|
|
1108
1122
|
handler_code: data.handlerCode ?? null,
|
|
1109
1123
|
source_code: data.sourceCode ?? null,
|
|
1110
1124
|
schema_name: data.schemaName ?? null,
|
|
1111
1125
|
start_block: data.startBlock ?? 0,
|
|
1112
1126
|
database_url_enc: data.databaseUrlEnc ?? null
|
|
1113
|
-
}
|
|
1127
|
+
};
|
|
1128
|
+
const updateSet = {
|
|
1114
1129
|
version: data.version,
|
|
1115
1130
|
definition: jsonb(data.definition),
|
|
1116
1131
|
schema_hash: data.schemaHash,
|
|
@@ -1121,18 +1136,26 @@ async function registerSubgraph(db, data) {
|
|
|
1121
1136
|
start_block: data.startBlock ?? 0,
|
|
1122
1137
|
database_url_enc: data.databaseUrlEnc ?? null,
|
|
1123
1138
|
updated_at: new Date
|
|
1124
|
-
}
|
|
1139
|
+
};
|
|
1140
|
+
if (!isPlatformMode()) {
|
|
1141
|
+
const existing = await db.selectFrom("subgraphs").select("id").where("name", "=", data.name).executeTakeFirst();
|
|
1142
|
+
if (existing) {
|
|
1143
|
+
return await db.updateTable("subgraphs").set({ ...updateSet, account_id: "" }).where("id", "=", existing.id).returningAll().executeTakeFirstOrThrow();
|
|
1144
|
+
}
|
|
1145
|
+
return await db.insertInto("subgraphs").values(values).returningAll().executeTakeFirstOrThrow();
|
|
1146
|
+
}
|
|
1147
|
+
return await db.insertInto("subgraphs").values(values).onConflict((oc) => oc.columns(["name", "account_id"]).doUpdateSet(updateSet)).returningAll().executeTakeFirstOrThrow();
|
|
1125
1148
|
}
|
|
1126
1149
|
async function getSubgraph(db, name, accountId) {
|
|
1127
1150
|
let query = db.selectFrom("subgraphs").selectAll().where("name", "=", name);
|
|
1128
|
-
if (accountId !== undefined) {
|
|
1151
|
+
if (isPlatformMode() && accountId !== undefined) {
|
|
1129
1152
|
query = query.where("account_id", "=", accountId);
|
|
1130
1153
|
}
|
|
1131
1154
|
return await query.executeTakeFirst() ?? null;
|
|
1132
1155
|
}
|
|
1133
1156
|
async function listSubgraphs(db, accountId) {
|
|
1134
1157
|
let query = db.selectFrom("subgraphs").selectAll();
|
|
1135
|
-
if (accountId !== undefined) {
|
|
1158
|
+
if (isPlatformMode() && accountId !== undefined) {
|
|
1136
1159
|
query = query.where("account_id", "=", accountId);
|
|
1137
1160
|
}
|
|
1138
1161
|
return query.execute();
|
|
@@ -1224,6 +1247,7 @@ export {
|
|
|
1224
1247
|
recordLiveError,
|
|
1225
1248
|
pgSchemaNameFor,
|
|
1226
1249
|
pgSchemaName,
|
|
1250
|
+
localSubgraphAccountId,
|
|
1227
1251
|
listSubgraphs,
|
|
1228
1252
|
isByoSubgraph,
|
|
1229
1253
|
getSubgraph,
|
|
@@ -1232,5 +1256,5 @@ export {
|
|
|
1232
1256
|
deleteSubgraph
|
|
1233
1257
|
};
|
|
1234
1258
|
|
|
1235
|
-
//# debugId=
|
|
1259
|
+
//# debugId=BD59655AE696BBAF64756E2164756E21
|
|
1236
1260
|
//# sourceMappingURL=subgraphs.js.map
|