apibara 2.1.0-beta.30 → 2.1.0-beta.31
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/chunks/add.mjs +1 -0
- package/dist/chunks/add.mjs.map +1 -0
- package/dist/chunks/build.mjs +1 -0
- package/dist/chunks/build.mjs.map +1 -0
- package/dist/chunks/dev.mjs +14 -9
- package/dist/chunks/dev.mjs.map +1 -0
- package/dist/chunks/init.mjs +1 -0
- package/dist/chunks/init.mjs.map +1 -0
- package/dist/chunks/prepare.mjs +1 -0
- package/dist/chunks/prepare.mjs.map +1 -0
- package/dist/chunks/start.mjs +3 -8
- package/dist/chunks/start.mjs.map +1 -0
- package/dist/chunks/write-project-info.mjs +1 -0
- package/dist/chunks/write-project-info.mjs.map +1 -0
- package/dist/cli/index.mjs +1 -0
- package/dist/cli/index.mjs.map +1 -0
- package/dist/common/index.d.mts +28 -0
- package/dist/common/index.d.ts +28 -0
- package/dist/common/index.mjs +64 -0
- package/dist/common/index.mjs.map +1 -0
- package/dist/config/index.mjs +1 -0
- package/dist/config/index.mjs.map +1 -0
- package/dist/core/index.mjs +41 -6
- package/dist/core/index.mjs.map +1 -0
- package/dist/create/index.mjs +3 -4
- package/dist/create/index.mjs.map +1 -0
- package/dist/hooks/index.mjs +6 -1
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/indexer/index.d.ts +1 -0
- package/dist/indexer/index.mjs +1 -0
- package/dist/indexer/plugins.d.ts +1 -0
- package/dist/indexer/plugins.mjs +1 -0
- package/dist/indexer/testing.d.ts +1 -0
- package/dist/indexer/testing.mjs +1 -0
- package/dist/indexer/vcr.d.ts +1 -0
- package/dist/indexer/vcr.mjs +1 -0
- package/dist/rolldown/index.mjs +1 -38
- package/dist/rolldown/index.mjs.map +1 -0
- package/dist/runtime/dev.mjs +10 -7
- package/dist/runtime/internal/app.d.ts +11 -1
- package/dist/runtime/internal/app.mjs +12 -13
- package/dist/runtime/project-info.mjs +17 -3
- package/dist/runtime/start.mjs +10 -7
- package/dist/shared/apibara.63c9a277.mjs +1 -0
- package/dist/shared/apibara.63c9a277.mjs.map +1 -0
- package/dist/shared/apibara.730bb1e4.mjs +1 -0
- package/dist/shared/apibara.730bb1e4.mjs.map +1 -0
- package/dist/types/index.d.mts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.mjs +1 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +24 -4
- package/src/cli/commands/dev.ts +10 -9
- package/src/cli/commands/start.ts +2 -8
- package/src/common/constants.ts +6 -0
- package/src/common/helper.ts +86 -0
- package/src/common/index.ts +2 -0
- package/src/core/apibara.ts +2 -2
- package/src/core/build/dev.ts +2 -0
- package/src/core/config/loader.ts +3 -1
- package/src/core/config/resolvers/runtime.resolver.ts +44 -0
- package/src/core/config/update.ts +6 -2
- package/src/create/constants.ts +0 -1
- package/src/create/templates.ts +2 -3
- package/src/hooks/useRuntimeConfig.ts +4 -1
- package/src/indexer/index.ts +1 -0
- package/src/indexer/plugins.ts +1 -0
- package/src/indexer/testing.ts +1 -0
- package/src/indexer/vcr.ts +1 -0
- package/src/rolldown/config.ts +0 -27
- package/src/runtime/dev.ts +10 -6
- package/src/runtime/internal/app.ts +19 -14
- package/src/runtime/project-info.ts +21 -3
- package/src/runtime/start.ts +10 -6
- package/src/types/config.ts +4 -1
- package/dist/runtime/internal/helper.d.ts +0 -12
- package/dist/runtime/internal/helper.mjs +0 -33
- package/src/core/config/resolvers/preset.resolver.ts +0 -12
- package/src/rolldown/plugins/config.ts +0 -17
- package/src/runtime/internal/helper.ts +0 -55
- package/src/types/virtual/config.d.ts +0 -6
package/src/cli/commands/dev.ts
CHANGED
|
@@ -9,8 +9,8 @@ import { blueBright, gray } from "../../create/colors";
|
|
|
9
9
|
import { checkForUnknownArgs, commonArgs } from "../common";
|
|
10
10
|
|
|
11
11
|
// Hot module reloading key regex
|
|
12
|
-
// for only runtimeConfig.* keys
|
|
13
|
-
const hmrKeyRe = /^runtimeConfig
|
|
12
|
+
// for only runtimeConfig.*, presets.* keys & preset key
|
|
13
|
+
const hmrKeyRe = /^(runtimeConfig\.|presets\.|preset$)/;
|
|
14
14
|
|
|
15
15
|
export default defineCommand({
|
|
16
16
|
meta: {
|
|
@@ -79,12 +79,12 @@ export default defineCommand({
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
apibara.logger.info(
|
|
82
|
-
`Config updated:
|
|
83
|
-
${diff.map((entry) => ` ${entry.toString()}`).join("\n")}`,
|
|
82
|
+
`Config updated: \n${diff.map((entry) => ` ${entry.toString()}`).join("\n")}`,
|
|
84
83
|
);
|
|
85
84
|
|
|
86
85
|
await (diff.every((e) => hmrKeyRe.test(e.key))
|
|
87
|
-
? apibara
|
|
86
|
+
? // in hot reload we only update the runtime values & restart indexers,no build step, apibara instance remains the same
|
|
87
|
+
apibara.updateConfig(newConfig.config || {}) // Hot reload
|
|
88
88
|
: reload()); // Full reload
|
|
89
89
|
},
|
|
90
90
|
},
|
|
@@ -92,7 +92,9 @@ export default defineCommand({
|
|
|
92
92
|
true,
|
|
93
93
|
);
|
|
94
94
|
|
|
95
|
-
apibara.hooks.hookOnce("restart",
|
|
95
|
+
apibara.hooks.hookOnce("restart", async () => {
|
|
96
|
+
await reload();
|
|
97
|
+
});
|
|
96
98
|
|
|
97
99
|
apibara.options.entry = join(runtimeDir, "dev.mjs");
|
|
98
100
|
|
|
@@ -110,9 +112,9 @@ export default defineCommand({
|
|
|
110
112
|
|
|
111
113
|
apibara.hooks.hook("dev:reload", async () => {
|
|
112
114
|
if (childProcess) {
|
|
113
|
-
apibara.logger.info("Restarting indexers");
|
|
114
115
|
await killProcess(childProcess);
|
|
115
116
|
childProcess = undefined;
|
|
117
|
+
apibara.logger.info("Restarting indexers");
|
|
116
118
|
} else {
|
|
117
119
|
apibara.logger.info("Starting indexers");
|
|
118
120
|
|
|
@@ -130,7 +132,6 @@ export default defineCommand({
|
|
|
130
132
|
resolve(apibara.options.outputDir || "./.apibara/build", "dev.mjs"),
|
|
131
133
|
"start",
|
|
132
134
|
...(args.indexers ? ["--indexers", args.indexers] : []),
|
|
133
|
-
...(args.preset ? ["--preset", args.preset] : []),
|
|
134
135
|
];
|
|
135
136
|
|
|
136
137
|
childProcess = spawn("node", childArgs, {
|
|
@@ -140,7 +141,7 @@ export default defineCommand({
|
|
|
140
141
|
childProcess.on("close", (code, signal) => {
|
|
141
142
|
childProcess = undefined;
|
|
142
143
|
console.log();
|
|
143
|
-
apibara.logger.
|
|
144
|
+
apibara.logger.info(
|
|
144
145
|
`Indexers process exited${
|
|
145
146
|
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
146
147
|
}`,
|
|
@@ -50,13 +50,7 @@ export default defineCommand({
|
|
|
50
50
|
|
|
51
51
|
await apibara.close();
|
|
52
52
|
|
|
53
|
-
const childArgs = [
|
|
54
|
-
entry,
|
|
55
|
-
"start",
|
|
56
|
-
"--indexer",
|
|
57
|
-
indexer,
|
|
58
|
-
...(preset ? ["--preset", preset] : []),
|
|
59
|
-
];
|
|
53
|
+
const childArgs = [entry, "start", "--indexer", indexer];
|
|
60
54
|
|
|
61
55
|
const childProcess = spawn("node", childArgs, {
|
|
62
56
|
stdio: "inherit",
|
|
@@ -64,7 +58,7 @@ export default defineCommand({
|
|
|
64
58
|
|
|
65
59
|
childProcess.on("close", (code, signal) => {
|
|
66
60
|
console.log();
|
|
67
|
-
apibara.logger.
|
|
61
|
+
apibara.logger.info(
|
|
68
62
|
`Indexers process exited${
|
|
69
63
|
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
70
64
|
}`,
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const ENV_INTERNAL_APIBARA_RUNTIME = "_APIBARA_RUNTIME";
|
|
2
|
+
export const ENV_INTERNAL_APIBARA_PRESET = "_APIBARA_PRESET";
|
|
3
|
+
export const ENV_INTERNAL_APIBARA_PRESETS = "_APIBARA_PRESETS";
|
|
4
|
+
export const ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME =
|
|
5
|
+
"_APIBARA_PROCESSED_RUNTIME";
|
|
6
|
+
export const USER_ENV_APIBARA_RUNTIME_CONFIG = "APIBARA_RUNTIME_CONFIG";
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import defu from "defu";
|
|
2
|
+
import {
|
|
3
|
+
ENV_INTERNAL_APIBARA_PRESET,
|
|
4
|
+
ENV_INTERNAL_APIBARA_PRESETS,
|
|
5
|
+
ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME,
|
|
6
|
+
ENV_INTERNAL_APIBARA_RUNTIME,
|
|
7
|
+
USER_ENV_APIBARA_RUNTIME_CONFIG,
|
|
8
|
+
} from "./constants";
|
|
9
|
+
|
|
10
|
+
export function getRuntimeDataFromEnv() {
|
|
11
|
+
const processedRuntimeConfig: Record<string, unknown> = JSON.parse(
|
|
12
|
+
process.env[ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME] ?? "{}",
|
|
13
|
+
);
|
|
14
|
+
const preset: string | undefined = process.env[ENV_INTERNAL_APIBARA_PRESET];
|
|
15
|
+
const presets: Record<string, unknown> | undefined = JSON.parse(
|
|
16
|
+
process.env[ENV_INTERNAL_APIBARA_PRESETS] ?? "{}",
|
|
17
|
+
);
|
|
18
|
+
const runtimeConfig: Record<string, unknown> | undefined = JSON.parse(
|
|
19
|
+
process.env[ENV_INTERNAL_APIBARA_RUNTIME] ?? "{}",
|
|
20
|
+
);
|
|
21
|
+
const userEnvRuntimeConfig: Record<string, unknown> | undefined = JSON.parse(
|
|
22
|
+
process.env[USER_ENV_APIBARA_RUNTIME_CONFIG] ?? "{}",
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
userEnvRuntimeConfig,
|
|
27
|
+
processedRuntimeConfig,
|
|
28
|
+
preset,
|
|
29
|
+
presets,
|
|
30
|
+
runtimeConfig,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Get the merged runtime config from the user env overrided runtime config, presets and defaults.
|
|
36
|
+
* Priority (Highest to lowest):
|
|
37
|
+
* 1. User env overrided runtime config
|
|
38
|
+
* 2. Preset
|
|
39
|
+
* 3. Defaults
|
|
40
|
+
*/
|
|
41
|
+
export function getProcessedRuntimeConfig({
|
|
42
|
+
preset,
|
|
43
|
+
presets,
|
|
44
|
+
runtimeConfig,
|
|
45
|
+
userEnvRuntimeConfig,
|
|
46
|
+
}: {
|
|
47
|
+
preset?: string;
|
|
48
|
+
presets?: Record<string, unknown>;
|
|
49
|
+
runtimeConfig?: Record<string, unknown>;
|
|
50
|
+
userEnvRuntimeConfig?: Record<string, unknown>;
|
|
51
|
+
}) {
|
|
52
|
+
let _runtimeConfig: Record<string, unknown> = { ...runtimeConfig };
|
|
53
|
+
|
|
54
|
+
if (preset) {
|
|
55
|
+
if (presets === undefined) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
`Specified preset "${preset}" but no presets were defined`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (presets[preset] === undefined) {
|
|
62
|
+
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const presetValue = presets[preset] as {
|
|
66
|
+
runtimeConfig: Record<string, unknown>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Preset applied
|
|
70
|
+
_runtimeConfig = defu(presetValue.runtimeConfig, _runtimeConfig);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (userEnvRuntimeConfig) {
|
|
74
|
+
try {
|
|
75
|
+
// Environment runtime config applied
|
|
76
|
+
_runtimeConfig = defu(userEnvRuntimeConfig, _runtimeConfig);
|
|
77
|
+
} catch (error) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
"Failed to parse runtime config from process.env.APIBARA_RUNTIME_CONFIG. Please ensure it is a valid JSON string.",
|
|
80
|
+
{ cause: error },
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return _runtimeConfig;
|
|
86
|
+
}
|
package/src/core/apibara.ts
CHANGED
|
@@ -28,8 +28,8 @@ export async function createApibara(
|
|
|
28
28
|
hooks: createHooks(),
|
|
29
29
|
close: () => apibara.hooks.callHook("close"),
|
|
30
30
|
logger: consola.withTag("apibara"),
|
|
31
|
-
async updateConfig(
|
|
32
|
-
updateApibaraConfig(apibara,
|
|
31
|
+
async updateConfig(newConfig: ApibaraDynamicConfig) {
|
|
32
|
+
updateApibaraConfig(apibara, newConfig);
|
|
33
33
|
},
|
|
34
34
|
};
|
|
35
35
|
|
package/src/core/build/dev.ts
CHANGED
|
@@ -7,8 +7,10 @@ import { loadConfig, watchConfig } from "c12";
|
|
|
7
7
|
import { klona } from "klona/full";
|
|
8
8
|
import { ApibaraDefaults } from "./defaults";
|
|
9
9
|
import { resolvePathOptions } from "./resolvers/paths.resolver";
|
|
10
|
+
import { runtimeConfigResolver } from "./resolvers/runtime.resolver";
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
// runtimeConfigResolver handles assigning runtime values to env
|
|
13
|
+
const configResolvers = [resolvePathOptions, runtimeConfigResolver] as const;
|
|
12
14
|
|
|
13
15
|
export async function loadOptions(
|
|
14
16
|
configOverrides: ApibaraConfig = {},
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ENV_INTERNAL_APIBARA_PRESET,
|
|
3
|
+
ENV_INTERNAL_APIBARA_PRESETS,
|
|
4
|
+
ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME,
|
|
5
|
+
ENV_INTERNAL_APIBARA_RUNTIME,
|
|
6
|
+
USER_ENV_APIBARA_RUNTIME_CONFIG,
|
|
7
|
+
getProcessedRuntimeConfig,
|
|
8
|
+
} from "apibara/common";
|
|
9
|
+
import type { ApibaraOptions } from "apibara/types";
|
|
10
|
+
|
|
11
|
+
export function runtimeConfigResolver(options: Partial<ApibaraOptions>) {
|
|
12
|
+
try {
|
|
13
|
+
const { runtimeConfig, preset, presets } = options;
|
|
14
|
+
|
|
15
|
+
if (runtimeConfig) {
|
|
16
|
+
process.env[ENV_INTERNAL_APIBARA_RUNTIME] = JSON.stringify(runtimeConfig);
|
|
17
|
+
}
|
|
18
|
+
if (preset) {
|
|
19
|
+
process.env[ENV_INTERNAL_APIBARA_PRESET] = preset;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (presets) {
|
|
23
|
+
process.env[ENV_INTERNAL_APIBARA_PRESETS] = JSON.stringify(presets);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const userEnvRuntimeConfig = JSON.parse(
|
|
27
|
+
process.env[USER_ENV_APIBARA_RUNTIME_CONFIG] ?? "{}",
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// This is final processed runtime config that will be used by the indexer and useRuntimeConfig hook
|
|
31
|
+
process.env[ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME] = JSON.stringify(
|
|
32
|
+
getProcessedRuntimeConfig({
|
|
33
|
+
preset,
|
|
34
|
+
presets,
|
|
35
|
+
runtimeConfig,
|
|
36
|
+
userEnvRuntimeConfig,
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new Error("Failed to process & set runtime environment variables", {
|
|
41
|
+
cause: error,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { Apibara, ApibaraDynamicConfig } from "apibara/types";
|
|
2
|
+
import { runtimeConfigResolver } from "./resolvers/runtime.resolver";
|
|
2
3
|
|
|
3
4
|
export async function updateApibaraConfig(
|
|
4
5
|
apibara: Apibara,
|
|
5
|
-
|
|
6
|
+
newConfig: ApibaraDynamicConfig,
|
|
6
7
|
) {
|
|
7
|
-
|
|
8
|
+
// applies new config values to env again during hot reload
|
|
9
|
+
runtimeConfigResolver(newConfig);
|
|
8
10
|
apibara.logger.success("Apibara config hot reloaded!");
|
|
11
|
+
// we simply stop indexers and restart them with updated runtime values
|
|
12
|
+
await apibara.hooks.callHook("dev:reload");
|
|
9
13
|
}
|
package/src/create/constants.ts
CHANGED
package/src/create/templates.ts
CHANGED
|
@@ -22,7 +22,6 @@ export function generatePackageJson(isTypeScript: boolean) {
|
|
|
22
22
|
...(isTypeScript && { typecheck: "tsc --noEmit" }),
|
|
23
23
|
},
|
|
24
24
|
dependencies: {
|
|
25
|
-
"@apibara/indexer": packageVersions["@apibara/indexer"],
|
|
26
25
|
"@apibara/protocol": packageVersions["@apibara/protocol"],
|
|
27
26
|
apibara: packageVersions.apibara,
|
|
28
27
|
},
|
|
@@ -70,8 +69,8 @@ export function generateIndexer({
|
|
|
70
69
|
chain,
|
|
71
70
|
language,
|
|
72
71
|
}: IndexerOptions) {
|
|
73
|
-
return `import { defineIndexer } from "
|
|
74
|
-
import { useLogger } from "
|
|
72
|
+
return `import { defineIndexer } from "apibara/indexer";
|
|
73
|
+
import { useLogger } from "apibara/plugins";
|
|
75
74
|
${storage === "postgres" ? `import { drizzleStorage } from "@apibara/plugin-drizzle";` : ""}
|
|
76
75
|
${storage === "postgres" ? `import { drizzle } from "@apibara/plugin-drizzle";` : ""}
|
|
77
76
|
${
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
import { ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME } from "apibara/common";
|
|
1
2
|
import type { ApibaraRuntimeConfig } from "apibara/types";
|
|
2
3
|
|
|
3
4
|
export function useRuntimeConfig(): ApibaraRuntimeConfig {
|
|
4
|
-
return JSON.parse(
|
|
5
|
+
return JSON.parse(
|
|
6
|
+
process.env[ENV_INTERNAL_APIBARA_PROCESSED_RUNTIME] || "{}",
|
|
7
|
+
);
|
|
5
8
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@apibara/indexer";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@apibara/indexer/plugins";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@apibara/indexer/testing";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@apibara/indexer/vcr";
|
package/src/rolldown/config.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { builtinModules } from "node:module";
|
|
3
|
-
import replace from "@rollup/plugin-replace";
|
|
4
3
|
import type { Apibara } from "apibara/types";
|
|
5
4
|
import defu from "defu";
|
|
6
5
|
import { join } from "pathe";
|
|
@@ -9,7 +8,6 @@ import type {
|
|
|
9
8
|
RolldownOptions,
|
|
10
9
|
RolldownPluginOption,
|
|
11
10
|
} from "rolldown";
|
|
12
|
-
import { appConfig } from "./plugins/config";
|
|
13
11
|
import { indexers } from "./plugins/indexers";
|
|
14
12
|
import { instrumentation } from "./plugins/instrumentation";
|
|
15
13
|
|
|
@@ -79,33 +77,8 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
|
|
|
79
77
|
},
|
|
80
78
|
);
|
|
81
79
|
|
|
82
|
-
rolldownConfig.plugins?.push(
|
|
83
|
-
replace({
|
|
84
|
-
preventAssignment: true,
|
|
85
|
-
values: {
|
|
86
|
-
"process.env.APIBARA_CONFIG": getSerializedRuntimeConfig(apibara),
|
|
87
|
-
},
|
|
88
|
-
}) as RolldownPluginOption,
|
|
89
|
-
);
|
|
90
|
-
|
|
91
80
|
rolldownConfig.plugins?.push(instrumentation(apibara));
|
|
92
81
|
rolldownConfig.plugins?.push(indexers(apibara));
|
|
93
|
-
rolldownConfig.plugins?.push(appConfig(apibara));
|
|
94
82
|
|
|
95
83
|
return rolldownConfig;
|
|
96
84
|
}
|
|
97
|
-
|
|
98
|
-
function getSerializedRuntimeConfig(apibara: Apibara) {
|
|
99
|
-
try {
|
|
100
|
-
return JSON.stringify({
|
|
101
|
-
runtimeConfig: apibara.options.runtimeConfig,
|
|
102
|
-
preset: apibara.options.preset,
|
|
103
|
-
presets: apibara.options.presets,
|
|
104
|
-
});
|
|
105
|
-
} catch (error) {
|
|
106
|
-
throw new Error(
|
|
107
|
-
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.",
|
|
108
|
-
{ cause: error },
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
}
|
package/src/runtime/dev.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
+
import { getRuntimeDataFromEnv } from "apibara/common";
|
|
2
3
|
import { defineCommand, runMain } from "citty";
|
|
3
4
|
import { blueBright } from "picocolors";
|
|
4
5
|
import {
|
|
@@ -17,13 +18,11 @@ const startCommand = defineCommand({
|
|
|
17
18
|
type: "string",
|
|
18
19
|
description: "Which indexers to run",
|
|
19
20
|
},
|
|
20
|
-
preset: {
|
|
21
|
-
type: "string",
|
|
22
|
-
description: "Preset to use",
|
|
23
|
-
},
|
|
24
21
|
},
|
|
25
22
|
async run({ args }) {
|
|
26
|
-
const { indexers: indexersArgs
|
|
23
|
+
const { indexers: indexersArgs } = args;
|
|
24
|
+
|
|
25
|
+
const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
|
|
27
26
|
|
|
28
27
|
let selectedIndexers = availableIndexers;
|
|
29
28
|
if (indexersArgs) {
|
|
@@ -41,7 +40,11 @@ const startCommand = defineCommand({
|
|
|
41
40
|
await Promise.all(
|
|
42
41
|
selectedIndexers.map(async (indexer) => {
|
|
43
42
|
const { indexer: indexerInstance, logger } =
|
|
44
|
-
createIndexer(
|
|
43
|
+
createIndexer({
|
|
44
|
+
indexerName: indexer,
|
|
45
|
+
processedRuntimeConfig,
|
|
46
|
+
preset,
|
|
47
|
+
}) ?? {};
|
|
45
48
|
if (!indexerInstance) {
|
|
46
49
|
return;
|
|
47
50
|
}
|
|
@@ -49,6 +52,7 @@ const startCommand = defineCommand({
|
|
|
49
52
|
const client = createAuthenticatedClient(
|
|
50
53
|
indexerInstance.streamConfig,
|
|
51
54
|
indexerInstance.options.streamUrl,
|
|
55
|
+
indexerInstance.options.clientOptions,
|
|
52
56
|
);
|
|
53
57
|
|
|
54
58
|
if (logger) {
|
|
@@ -15,25 +15,27 @@ import {
|
|
|
15
15
|
createClient,
|
|
16
16
|
} from "@apibara/protocol";
|
|
17
17
|
import consola from "consola";
|
|
18
|
-
import { config } from "#apibara-internal-virtual/config";
|
|
19
18
|
import { indexers } from "#apibara-internal-virtual/indexers";
|
|
20
19
|
import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
|
|
21
|
-
import { getProcessedRuntimeConfig } from "./helper";
|
|
22
20
|
import { createLogger } from "./logger";
|
|
23
21
|
|
|
24
22
|
export const availableIndexers = indexers.map((i) => i.name);
|
|
25
23
|
|
|
26
|
-
export function createIndexer(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
export function createIndexer({
|
|
25
|
+
indexerName,
|
|
26
|
+
processedRuntimeConfig,
|
|
27
|
+
preset,
|
|
28
|
+
}: {
|
|
29
|
+
indexerName: string;
|
|
30
|
+
/**
|
|
31
|
+
* Final processed runtime config to be used by the indexer
|
|
32
|
+
*/
|
|
33
|
+
processedRuntimeConfig: Record<string, unknown>;
|
|
34
|
+
/**
|
|
35
|
+
* Preset name which was used to generate the runtime config
|
|
36
|
+
*/
|
|
37
|
+
preset?: string;
|
|
38
|
+
}) {
|
|
37
39
|
const indexerDefinition = indexers.find((i) => i.name === indexerName);
|
|
38
40
|
|
|
39
41
|
if (indexerDefinition === undefined) {
|
|
@@ -52,7 +54,7 @@ export function createIndexer(indexerName: string, preset?: string) {
|
|
|
52
54
|
|
|
53
55
|
const definition =
|
|
54
56
|
typeof indexerModule === "function"
|
|
55
|
-
? indexerModule(
|
|
57
|
+
? indexerModule(processedRuntimeConfig)
|
|
56
58
|
: indexerModule;
|
|
57
59
|
|
|
58
60
|
let reporter: ConsolaReporter = createLogger({
|
|
@@ -109,10 +111,13 @@ export function createAuthenticatedClient(
|
|
|
109
111
|
return createClient(config, streamUrl, {
|
|
110
112
|
...options,
|
|
111
113
|
defaultCallOptions: {
|
|
114
|
+
...(options?.defaultCallOptions ?? {}),
|
|
112
115
|
"*": {
|
|
113
116
|
metadata: Metadata({
|
|
114
117
|
Authorization: `Bearer ${dnaToken}`,
|
|
115
118
|
}),
|
|
119
|
+
// metadata cant be overrided with spread as its a class so we override it fully if user provided it.
|
|
120
|
+
...(options?.defaultCallOptions?.["*"] ?? {}),
|
|
116
121
|
},
|
|
117
122
|
},
|
|
118
123
|
});
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { writeFileSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
getProcessedRuntimeConfig,
|
|
5
|
+
getRuntimeDataFromEnv,
|
|
6
|
+
} from "apibara/common";
|
|
3
7
|
import { defineCommand, runMain } from "citty";
|
|
4
|
-
import { config } from "#apibara-internal-virtual/config";
|
|
5
8
|
import { availableIndexers, createIndexer } from "./internal/app";
|
|
6
9
|
|
|
7
10
|
type ProjectInfo = {
|
|
@@ -31,10 +34,25 @@ const startCommand = defineCommand({
|
|
|
31
34
|
indexers: {},
|
|
32
35
|
};
|
|
33
36
|
|
|
34
|
-
|
|
37
|
+
// get all original runtime data from env
|
|
38
|
+
const { presets, runtimeConfig, userEnvRuntimeConfig } =
|
|
39
|
+
getRuntimeDataFromEnv();
|
|
40
|
+
|
|
41
|
+
for (const preset of Object.keys(presets ?? {})) {
|
|
42
|
+
// process runtime config for each preset
|
|
43
|
+
const processedRuntimeConfig = getProcessedRuntimeConfig({
|
|
44
|
+
preset,
|
|
45
|
+
presets,
|
|
46
|
+
runtimeConfig,
|
|
47
|
+
userEnvRuntimeConfig,
|
|
48
|
+
});
|
|
35
49
|
for (const indexer of availableIndexers) {
|
|
36
50
|
const { indexer: indexerInstance } =
|
|
37
|
-
createIndexer(
|
|
51
|
+
createIndexer({
|
|
52
|
+
indexerName: indexer,
|
|
53
|
+
processedRuntimeConfig,
|
|
54
|
+
preset,
|
|
55
|
+
}) ?? {};
|
|
38
56
|
if (!indexerInstance) {
|
|
39
57
|
continue;
|
|
40
58
|
}
|
package/src/runtime/start.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
+
import { getRuntimeDataFromEnv } from "apibara/common";
|
|
2
3
|
import { defineCommand, runMain } from "citty";
|
|
3
4
|
import consola from "consola";
|
|
4
5
|
import { blueBright } from "picocolors";
|
|
@@ -16,16 +17,18 @@ const startCommand = defineCommand({
|
|
|
16
17
|
description: "Indexer name",
|
|
17
18
|
required: true,
|
|
18
19
|
},
|
|
19
|
-
preset: {
|
|
20
|
-
type: "string",
|
|
21
|
-
description: "Preset to use",
|
|
22
|
-
},
|
|
23
20
|
},
|
|
24
21
|
async run({ args }) {
|
|
25
|
-
const { indexer
|
|
22
|
+
const { indexer } = args;
|
|
23
|
+
|
|
24
|
+
const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
|
|
26
25
|
|
|
27
26
|
const { indexer: indexerInstance, logger } =
|
|
28
|
-
createIndexer(
|
|
27
|
+
createIndexer({
|
|
28
|
+
indexerName: indexer,
|
|
29
|
+
processedRuntimeConfig,
|
|
30
|
+
preset,
|
|
31
|
+
}) ?? {};
|
|
29
32
|
if (!indexerInstance) {
|
|
30
33
|
consola.error(`Specified indexer "${indexer}" but it was not defined`);
|
|
31
34
|
process.exit(1);
|
|
@@ -34,6 +37,7 @@ const startCommand = defineCommand({
|
|
|
34
37
|
const client = createAuthenticatedClient(
|
|
35
38
|
indexerInstance.streamConfig,
|
|
36
39
|
indexerInstance.options.streamUrl,
|
|
40
|
+
indexerInstance.options.clientOptions,
|
|
37
41
|
);
|
|
38
42
|
|
|
39
43
|
if (register) {
|
package/src/types/config.ts
CHANGED
|
@@ -41,7 +41,10 @@ export interface ApibaraConfig<
|
|
|
41
41
|
preset?: keyof T;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
export type ApibaraDynamicConfig = Pick<
|
|
44
|
+
export type ApibaraDynamicConfig = Pick<
|
|
45
|
+
ApibaraConfig,
|
|
46
|
+
"runtimeConfig" | "preset" | "presets"
|
|
47
|
+
>;
|
|
45
48
|
|
|
46
49
|
/**
|
|
47
50
|
* Config loader options
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Get the merged runtime config from the process.env.APIBARA_RUNTIME_CONFIG, presets and defaults.
|
|
3
|
-
* Priority (Highest to lowest):
|
|
4
|
-
* 1. process.env.APIBARA_RUNTIME_CONFIG
|
|
5
|
-
* 2. Preset
|
|
6
|
-
* 3. Defaults
|
|
7
|
-
*/
|
|
8
|
-
export declare function getProcessedRuntimeConfig({ preset, presets, runtimeConfig, }: {
|
|
9
|
-
preset?: string;
|
|
10
|
-
presets?: Record<string, unknown>;
|
|
11
|
-
runtimeConfig?: Record<string, unknown>;
|
|
12
|
-
}): Record<string, unknown>;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import defu from "defu";
|
|
2
|
-
export function getProcessedRuntimeConfig({
|
|
3
|
-
preset,
|
|
4
|
-
presets,
|
|
5
|
-
runtimeConfig
|
|
6
|
-
}) {
|
|
7
|
-
let _runtimeConfig = { ...runtimeConfig };
|
|
8
|
-
const envRuntimeConfig = process.env.APIBARA_RUNTIME_CONFIG;
|
|
9
|
-
if (preset) {
|
|
10
|
-
if (presets === void 0) {
|
|
11
|
-
throw new Error(
|
|
12
|
-
`Specified preset "${preset}" but no presets were defined`
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
if (presets[preset] === void 0) {
|
|
16
|
-
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
17
|
-
}
|
|
18
|
-
const presetValue = presets[preset];
|
|
19
|
-
_runtimeConfig = defu(presetValue.runtimeConfig, _runtimeConfig);
|
|
20
|
-
}
|
|
21
|
-
if (envRuntimeConfig) {
|
|
22
|
-
try {
|
|
23
|
-
const envRuntimeConfigValue = JSON.parse(envRuntimeConfig);
|
|
24
|
-
_runtimeConfig = defu(envRuntimeConfigValue, _runtimeConfig);
|
|
25
|
-
} catch (error) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
"Failed to parse runtime config from process.env.APIBARA_RUNTIME_CONFIG. Please ensure it is a valid JSON string.",
|
|
28
|
-
{ cause: error }
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return _runtimeConfig;
|
|
33
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { ApibaraOptions } from "apibara/types";
|
|
2
|
-
import defu from "defu";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @note This resolver is not in use currently, as we resolve presets in runtime files.
|
|
6
|
-
*/
|
|
7
|
-
export async function presetResolver(options: ApibaraOptions) {
|
|
8
|
-
if (options.preset && options.presets?.[options.preset]) {
|
|
9
|
-
const new_options = defu(options.presets[options.preset], options);
|
|
10
|
-
Object.assign(options, new_options);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import virtual from "@rollup/plugin-virtual";
|
|
2
|
-
import type { Apibara } from "apibara/types";
|
|
3
|
-
import type { RolldownPluginOption } from "rolldown";
|
|
4
|
-
|
|
5
|
-
export function appConfig(apibara: Apibara) {
|
|
6
|
-
return virtual({
|
|
7
|
-
"#apibara-internal-virtual/config": `
|
|
8
|
-
const serializedConfig = \`process.env.APIBARA_CONFIG\`;
|
|
9
|
-
|
|
10
|
-
if (serializedConfig === undefined || serializedConfig === "") {
|
|
11
|
-
throw new Error("APIBARA_CONFIG is not defined");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export const config = JSON.parse(serializedConfig);
|
|
15
|
-
`,
|
|
16
|
-
}) as RolldownPluginOption;
|
|
17
|
-
}
|