apibara 2.1.0-beta.13 → 2.1.0-beta.15
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/dev.mjs +6 -6
- package/dist/chunks/start.mjs +9 -1
- package/dist/core/index.mjs +10 -32
- package/dist/hooks/index.mjs +1 -3
- package/dist/rolldown/index.mjs +3 -12
- package/dist/runtime/dev.mjs +6 -3
- package/dist/runtime/internal/app.d.ts +2 -0
- package/dist/runtime/internal/app.mjs +29 -13
- package/dist/runtime/internal/helper.d.ts +12 -0
- package/dist/runtime/internal/helper.mjs +33 -0
- package/dist/runtime/start.mjs +2 -3
- package/package.json +5 -5
- package/src/cli/commands/dev.ts +8 -6
- package/src/cli/commands/start.ts +12 -1
- package/src/core/config/loader.ts +13 -7
- package/src/core/config/resolvers/preset.resolver.ts +3 -0
- package/src/hooks/useRuntimeConfig.ts +1 -2
- package/src/rolldown/config.ts +2 -3
- package/src/rolldown/plugins/config.ts +1 -9
- package/src/runtime/dev.ts +6 -3
- package/src/runtime/internal/app.ts +39 -17
- package/src/runtime/internal/helper.ts +55 -0
- package/src/runtime/start.ts +2 -3
- package/dist/shared/apibara.af330c7d.mjs +0 -15
- package/src/core/config/resolvers/runtime-config.resolver.ts +0 -25
- package/src/utils/helper.ts +0 -15
package/dist/chunks/dev.mjs
CHANGED
|
@@ -95,12 +95,12 @@ const dev = defineCommand({
|
|
|
95
95
|
childProcess = spawn("node", childArgs, {
|
|
96
96
|
stdio: "inherit"
|
|
97
97
|
});
|
|
98
|
-
childProcess.on("close", (code) => {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
childProcess.on("close", (code, signal) => {
|
|
99
|
+
console.log();
|
|
100
|
+
apibara.logger.log(
|
|
101
|
+
`Indexers process exited${code !== null ? ` with code ${colors.red(code)}` : ""}`
|
|
102
|
+
);
|
|
103
|
+
process.exit(code ?? 0);
|
|
104
104
|
});
|
|
105
105
|
});
|
|
106
106
|
};
|
package/dist/chunks/start.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'node:child_process';
|
|
2
2
|
import { createApibara } from 'apibara/core';
|
|
3
3
|
import { defineCommand } from 'citty';
|
|
4
|
+
import { colors } from 'consola/utils';
|
|
4
5
|
import fse from 'fs-extra';
|
|
5
6
|
import { resolve } from 'pathe';
|
|
6
7
|
import { c as commonArgs } from '../shared/apibara.1b515d04.mjs';
|
|
@@ -48,9 +49,16 @@ const start = defineCommand({
|
|
|
48
49
|
indexer,
|
|
49
50
|
...preset ? ["--preset", preset] : []
|
|
50
51
|
];
|
|
51
|
-
spawn("node", childArgs, {
|
|
52
|
+
const childProcess = spawn("node", childArgs, {
|
|
52
53
|
stdio: "inherit"
|
|
53
54
|
});
|
|
55
|
+
childProcess.on("close", (code, signal) => {
|
|
56
|
+
console.log();
|
|
57
|
+
apibara.logger.log(
|
|
58
|
+
`Indexers process exited${code !== null ? ` with code ${colors.red(code)}` : ""}`
|
|
59
|
+
);
|
|
60
|
+
process.exit(code ?? 0);
|
|
61
|
+
});
|
|
54
62
|
}
|
|
55
63
|
});
|
|
56
64
|
|
package/dist/core/index.mjs
CHANGED
|
@@ -3,12 +3,11 @@ import { createHooks } from 'hookable';
|
|
|
3
3
|
import { watchConfig, loadConfig } from 'c12';
|
|
4
4
|
import { klona } from 'klona/full';
|
|
5
5
|
import { resolve, join, basename, isAbsolute, relative, dirname } from 'pathe';
|
|
6
|
-
import defu from 'defu';
|
|
7
|
-
import { s as serialize } from '../shared/apibara.af330c7d.mjs';
|
|
8
6
|
import fse from 'fs-extra';
|
|
9
7
|
import { getRolldownConfig } from 'apibara/rolldown';
|
|
10
8
|
import { colors } from 'consola/utils';
|
|
11
9
|
import { watch } from 'chokidar';
|
|
10
|
+
import defu from 'defu';
|
|
12
11
|
import { debounce } from 'perfect-debounce';
|
|
13
12
|
import * as rolldown from 'rolldown';
|
|
14
13
|
import fsp from 'node:fs/promises';
|
|
@@ -39,38 +38,17 @@ async function resolvePathOptions(options) {
|
|
|
39
38
|
}
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
if (options.preset && options.presets?.[options.preset]) {
|
|
44
|
-
const new_options = defu(options.presets[options.preset], options);
|
|
45
|
-
Object.assign(options, new_options);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async function resolveRuntimeConfigOptions(options) {
|
|
50
|
-
const { preset, presets } = options;
|
|
51
|
-
let runtimeConfig = { ...options.runtimeConfig };
|
|
52
|
-
if (preset) {
|
|
53
|
-
if (presets === void 0) {
|
|
54
|
-
throw new Error(
|
|
55
|
-
`Specified preset "${preset}" but no presets were defined`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
if (presets[preset] === void 0) {
|
|
59
|
-
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
60
|
-
}
|
|
61
|
-
const presetValue = presets[preset];
|
|
62
|
-
runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
|
|
63
|
-
}
|
|
64
|
-
process.env.APIBARA_RUNTIME_CONFIG = serialize(runtimeConfig);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const configResolvers = [
|
|
68
|
-
resolvePathOptions,
|
|
69
|
-
resolveRuntimeConfigOptions,
|
|
70
|
-
presetResolver
|
|
71
|
-
];
|
|
41
|
+
const configResolvers = [resolvePathOptions];
|
|
72
42
|
async function loadOptions(configOverrides = {}, opts = {}, dev = false) {
|
|
73
43
|
const options = await _loadUserConfig(configOverrides, opts, dev);
|
|
44
|
+
try {
|
|
45
|
+
JSON.stringify(options.runtimeConfig);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"Non-serializable runtimeConfig. Please ensure the config is serializable.",
|
|
49
|
+
{ cause: error }
|
|
50
|
+
);
|
|
51
|
+
}
|
|
74
52
|
for (const resolver of configResolvers) {
|
|
75
53
|
await resolver(options);
|
|
76
54
|
}
|
package/dist/hooks/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { d as deserialize } from '../shared/apibara.af330c7d.mjs';
|
|
2
|
-
|
|
3
1
|
function useRuntimeConfig() {
|
|
4
|
-
return
|
|
2
|
+
return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG_HOOK_DATA || "{}");
|
|
5
3
|
}
|
|
6
4
|
|
|
7
5
|
export { useRuntimeConfig };
|
package/dist/rolldown/index.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import { builtinModules } from 'node:module';
|
|
|
3
3
|
import replace from '@rollup/plugin-replace';
|
|
4
4
|
import defu from 'defu';
|
|
5
5
|
import { join as join$1 } from 'pathe';
|
|
6
|
-
import { s as serialize } from '../shared/apibara.af330c7d.mjs';
|
|
7
6
|
import virtual from '@rollup/plugin-virtual';
|
|
8
7
|
import { hash } from 'ohash';
|
|
9
8
|
import { join } from 'node:path';
|
|
@@ -17,15 +16,7 @@ function appConfig(apibara) {
|
|
|
17
16
|
throw new Error("APIBARA_CONFIG is not defined");
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
return JSON.parse(str, (_, value) =>
|
|
22
|
-
typeof value === "string" && value.match(/^\\d+n$/)
|
|
23
|
-
? BigInt(value.slice(0, -1))
|
|
24
|
-
: value,
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const config = deserialize(serializedConfig);
|
|
19
|
+
export const config = JSON.parse(serializedConfig);
|
|
29
20
|
`
|
|
30
21
|
});
|
|
31
22
|
}
|
|
@@ -151,14 +142,14 @@ function getRolldownConfig(apibara) {
|
|
|
151
142
|
}
|
|
152
143
|
function getSerializedRuntimeConfig(apibara) {
|
|
153
144
|
try {
|
|
154
|
-
return
|
|
145
|
+
return JSON.stringify({
|
|
155
146
|
runtimeConfig: apibara.options.runtimeConfig,
|
|
156
147
|
preset: apibara.options.preset,
|
|
157
148
|
presets: apibara.options.presets
|
|
158
149
|
});
|
|
159
150
|
} catch (error) {
|
|
160
151
|
throw new Error(
|
|
161
|
-
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.
|
|
152
|
+
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.",
|
|
162
153
|
{ cause: error }
|
|
163
154
|
);
|
|
164
155
|
}
|
package/dist/runtime/dev.mjs
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
-
import { createClient } from "@apibara/protocol";
|
|
3
2
|
import { defineCommand, runMain } from "citty";
|
|
4
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
availableIndexers,
|
|
5
|
+
createAuthenticatedClient,
|
|
6
|
+
createIndexer
|
|
7
|
+
} from "./internal/app.mjs";
|
|
5
8
|
const startCommand = defineCommand({
|
|
6
9
|
meta: {
|
|
7
10
|
name: "start",
|
|
@@ -36,7 +39,7 @@ const startCommand = defineCommand({
|
|
|
36
39
|
if (!indexerInstance) {
|
|
37
40
|
return;
|
|
38
41
|
}
|
|
39
|
-
const client =
|
|
42
|
+
const client = createAuthenticatedClient(
|
|
40
43
|
indexerInstance.streamConfig,
|
|
41
44
|
indexerInstance.options.streamUrl
|
|
42
45
|
);
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
+
import { type CreateClientOptions, type StreamConfig } from "@apibara/protocol";
|
|
1
2
|
export declare const availableIndexers: any;
|
|
2
3
|
export declare function createIndexer(indexerName: string, preset?: string): import("@apibara/indexer").Indexer<unknown, unknown> | undefined;
|
|
4
|
+
export declare function createAuthenticatedClient(config: StreamConfig<unknown, unknown>, streamUrl: string, options?: CreateClientOptions): import("@apibara/protocol").GrpcClient<unknown, unknown>;
|
|
@@ -6,26 +6,24 @@ import {
|
|
|
6
6
|
inMemoryPersistence,
|
|
7
7
|
logger
|
|
8
8
|
} from "@apibara/indexer/plugins";
|
|
9
|
+
import {
|
|
10
|
+
Metadata,
|
|
11
|
+
createClient
|
|
12
|
+
} from "@apibara/protocol";
|
|
9
13
|
import consola from "consola";
|
|
10
14
|
import { config } from "#apibara-internal-virtual/config";
|
|
11
15
|
import { indexers } from "#apibara-internal-virtual/indexers";
|
|
12
16
|
import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
|
|
17
|
+
import { getProcessedRuntimeConfig } from "./helper.mjs";
|
|
13
18
|
import { createLogger } from "./logger.mjs";
|
|
14
19
|
export const availableIndexers = indexers.map((i) => i.name);
|
|
15
20
|
export function createIndexer(indexerName, preset) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
if (config.presets[preset] === void 0) {
|
|
24
|
-
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
25
|
-
}
|
|
26
|
-
const presetValue = config.presets[preset];
|
|
27
|
-
runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
|
|
28
|
-
}
|
|
21
|
+
const runtimeConfig = getProcessedRuntimeConfig({
|
|
22
|
+
preset,
|
|
23
|
+
presets: config.presets,
|
|
24
|
+
runtimeConfig: config.runtimeConfig
|
|
25
|
+
});
|
|
26
|
+
process.env.APIBARA_RUNTIME_CONFIG_HOOK_DATA = JSON.stringify(runtimeConfig);
|
|
29
27
|
const indexerDefinition = indexers.find((i) => i.name === indexerName);
|
|
30
28
|
if (indexerDefinition === void 0) {
|
|
31
29
|
throw new Error(
|
|
@@ -66,3 +64,21 @@ export function createIndexer(indexerName, preset) {
|
|
|
66
64
|
];
|
|
67
65
|
return _createIndexer(definition);
|
|
68
66
|
}
|
|
67
|
+
export function createAuthenticatedClient(config2, streamUrl, options) {
|
|
68
|
+
const dnaToken = process.env.DNA_TOKEN;
|
|
69
|
+
if (!dnaToken) {
|
|
70
|
+
consola.warn(
|
|
71
|
+
"DNA_TOKEN environment variable is not set. Trying to connect without authentication."
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return createClient(config2, streamUrl, {
|
|
75
|
+
...options,
|
|
76
|
+
defaultCallOptions: {
|
|
77
|
+
"*": {
|
|
78
|
+
metadata: Metadata({
|
|
79
|
+
Authorization: `Bearer ${dnaToken}`
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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>;
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
package/dist/runtime/start.mjs
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
-
import { createClient } from "@apibara/protocol";
|
|
3
2
|
import { defineCommand, runMain } from "citty";
|
|
4
3
|
import consola from "consola";
|
|
5
4
|
import { register } from "#apibara-internal-virtual/instrumentation";
|
|
6
|
-
import { createIndexer } from "./internal/app.mjs";
|
|
5
|
+
import { createAuthenticatedClient, createIndexer } from "./internal/app.mjs";
|
|
7
6
|
const startCommand = defineCommand({
|
|
8
7
|
meta: {
|
|
9
8
|
name: "start",
|
|
@@ -27,7 +26,7 @@ const startCommand = defineCommand({
|
|
|
27
26
|
consola.error(`Specified indexer "${indexer}" but it was not defined`);
|
|
28
27
|
process.exit(1);
|
|
29
28
|
}
|
|
30
|
-
const client =
|
|
29
|
+
const client = createAuthenticatedClient(
|
|
31
30
|
indexerInstance.streamConfig,
|
|
32
31
|
indexerInstance.options.streamUrl
|
|
33
32
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apibara",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/core/index.mjs",
|
|
6
6
|
"exports": {
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"playground:add": "pnpm playground add --dir playground"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@apibara/starknet": "2.1.0-beta.
|
|
81
|
+
"@apibara/starknet": "2.1.0-beta.15",
|
|
82
82
|
"@types/fs-extra": "^11.0.4",
|
|
83
83
|
"@types/node": "^20.14.0",
|
|
84
84
|
"@types/prompts": "^2.4.9",
|
|
@@ -89,14 +89,14 @@
|
|
|
89
89
|
"vitest": "^1.6.0"
|
|
90
90
|
},
|
|
91
91
|
"dependencies": {
|
|
92
|
-
"@apibara/indexer": "2.1.0-beta.
|
|
93
|
-
"@apibara/protocol": "2.1.0-beta.
|
|
92
|
+
"@apibara/indexer": "2.1.0-beta.15",
|
|
93
|
+
"@apibara/protocol": "2.1.0-beta.15",
|
|
94
94
|
"@rollup/plugin-replace": "^6.0.2",
|
|
95
95
|
"@rollup/plugin-virtual": "^3.0.2",
|
|
96
96
|
"c12": "^1.11.1",
|
|
97
97
|
"chokidar": "^3.6.0",
|
|
98
98
|
"citty": "^0.1.6",
|
|
99
|
-
"consola": "^3.2
|
|
99
|
+
"consola": "^3.4.2",
|
|
100
100
|
"defu": "^6.1.4",
|
|
101
101
|
"fs-extra": "^11.2.0",
|
|
102
102
|
"hookable": "^5.5.3",
|
package/src/cli/commands/dev.ts
CHANGED
|
@@ -118,12 +118,14 @@ export default defineCommand({
|
|
|
118
118
|
stdio: "inherit",
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
-
childProcess.on("close", (code) => {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
121
|
+
childProcess.on("close", (code, signal) => {
|
|
122
|
+
console.log();
|
|
123
|
+
apibara.logger.log(
|
|
124
|
+
`Indexers process exited${
|
|
125
|
+
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
126
|
+
}`,
|
|
127
|
+
);
|
|
128
|
+
process.exit(code ?? 0);
|
|
127
129
|
});
|
|
128
130
|
});
|
|
129
131
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { createApibara } from "apibara/core";
|
|
3
3
|
import { defineCommand } from "citty";
|
|
4
|
+
import { colors } from "consola/utils";
|
|
4
5
|
import fse from "fs-extra";
|
|
5
6
|
import { resolve } from "pathe";
|
|
6
7
|
import { commonArgs } from "../common";
|
|
@@ -55,8 +56,18 @@ export default defineCommand({
|
|
|
55
56
|
...(preset ? ["--preset", preset] : []),
|
|
56
57
|
];
|
|
57
58
|
|
|
58
|
-
spawn("node", childArgs, {
|
|
59
|
+
const childProcess = spawn("node", childArgs, {
|
|
59
60
|
stdio: "inherit",
|
|
60
61
|
});
|
|
62
|
+
|
|
63
|
+
childProcess.on("close", (code, signal) => {
|
|
64
|
+
console.log();
|
|
65
|
+
apibara.logger.log(
|
|
66
|
+
`Indexers process exited${
|
|
67
|
+
code !== null ? ` with code ${colors.red(code)}` : ""
|
|
68
|
+
}`,
|
|
69
|
+
);
|
|
70
|
+
process.exit(code ?? 0);
|
|
71
|
+
});
|
|
61
72
|
},
|
|
62
73
|
});
|
|
@@ -7,14 +7,8 @@ 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 { presetResolver } from "./resolvers/preset.resolver";
|
|
11
|
-
import { resolveRuntimeConfigOptions } from "./resolvers/runtime-config.resolver";
|
|
12
10
|
|
|
13
|
-
const configResolvers = [
|
|
14
|
-
resolvePathOptions,
|
|
15
|
-
resolveRuntimeConfigOptions,
|
|
16
|
-
presetResolver,
|
|
17
|
-
] as const;
|
|
11
|
+
const configResolvers = [resolvePathOptions] as const;
|
|
18
12
|
|
|
19
13
|
export async function loadOptions(
|
|
20
14
|
configOverrides: ApibaraConfig = {},
|
|
@@ -22,9 +16,21 @@ export async function loadOptions(
|
|
|
22
16
|
dev = false,
|
|
23
17
|
): Promise<ApibaraOptions> {
|
|
24
18
|
const options = await _loadUserConfig(configOverrides, opts, dev);
|
|
19
|
+
|
|
20
|
+
// Check if the runtimeConfig is serializable
|
|
21
|
+
try {
|
|
22
|
+
JSON.stringify(options.runtimeConfig);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
"Non-serializable runtimeConfig. Please ensure the config is serializable.",
|
|
26
|
+
{ cause: error },
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
25
30
|
for (const resolver of configResolvers) {
|
|
26
31
|
await resolver(options);
|
|
27
32
|
}
|
|
33
|
+
|
|
28
34
|
return options;
|
|
29
35
|
}
|
|
30
36
|
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { ApibaraOptions } from "apibara/types";
|
|
2
2
|
import defu from "defu";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @note This resolver is not in use currently, as we resolve presets in runtime files.
|
|
6
|
+
*/
|
|
4
7
|
export async function presetResolver(options: ApibaraOptions) {
|
|
5
8
|
if (options.preset && options.presets?.[options.preset]) {
|
|
6
9
|
const new_options = defu(options.presets[options.preset], options);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { ApibaraRuntimeConfig } from "apibara/types";
|
|
2
|
-
import { deserialize } from "../utils/helper";
|
|
3
2
|
|
|
4
3
|
export function useRuntimeConfig(): ApibaraRuntimeConfig {
|
|
5
|
-
return
|
|
4
|
+
return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG_HOOK_DATA || "{}");
|
|
6
5
|
}
|
package/src/rolldown/config.ts
CHANGED
|
@@ -9,7 +9,6 @@ import type {
|
|
|
9
9
|
RolldownOptions,
|
|
10
10
|
RolldownPluginOption,
|
|
11
11
|
} from "rolldown";
|
|
12
|
-
import { serialize } from "../utils/helper";
|
|
13
12
|
import { appConfig } from "./plugins/config";
|
|
14
13
|
import { indexers } from "./plugins/indexers";
|
|
15
14
|
import { instrumentation } from "./plugins/instrumentation";
|
|
@@ -97,14 +96,14 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
|
|
|
97
96
|
|
|
98
97
|
function getSerializedRuntimeConfig(apibara: Apibara) {
|
|
99
98
|
try {
|
|
100
|
-
return
|
|
99
|
+
return JSON.stringify({
|
|
101
100
|
runtimeConfig: apibara.options.runtimeConfig,
|
|
102
101
|
preset: apibara.options.preset,
|
|
103
102
|
presets: apibara.options.presets,
|
|
104
103
|
});
|
|
105
104
|
} catch (error) {
|
|
106
105
|
throw new Error(
|
|
107
|
-
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.
|
|
106
|
+
"Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable.",
|
|
108
107
|
{ cause: error },
|
|
109
108
|
);
|
|
110
109
|
}
|
|
@@ -11,15 +11,7 @@ export function appConfig(apibara: Apibara) {
|
|
|
11
11
|
throw new Error("APIBARA_CONFIG is not defined");
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
return JSON.parse(str, (_, value) =>
|
|
16
|
-
typeof value === "string" && value.match(/^\\d+n$/)
|
|
17
|
-
? BigInt(value.slice(0, -1))
|
|
18
|
-
: value,
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export const config = deserialize(serializedConfig);
|
|
14
|
+
export const config = JSON.parse(serializedConfig);
|
|
23
15
|
`,
|
|
24
16
|
}) as RolldownPluginOption;
|
|
25
17
|
}
|
package/src/runtime/dev.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
-
import { createClient } from "@apibara/protocol";
|
|
3
2
|
import { defineCommand, runMain } from "citty";
|
|
4
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
availableIndexers,
|
|
5
|
+
createAuthenticatedClient,
|
|
6
|
+
createIndexer,
|
|
7
|
+
} from "./internal/app";
|
|
5
8
|
|
|
6
9
|
const startCommand = defineCommand({
|
|
7
10
|
meta: {
|
|
@@ -41,7 +44,7 @@ const startCommand = defineCommand({
|
|
|
41
44
|
return;
|
|
42
45
|
}
|
|
43
46
|
|
|
44
|
-
const client =
|
|
47
|
+
const client = createAuthenticatedClient(
|
|
45
48
|
indexerInstance.streamConfig,
|
|
46
49
|
indexerInstance.options.streamUrl,
|
|
47
50
|
);
|
|
@@ -8,33 +8,31 @@ import {
|
|
|
8
8
|
inMemoryPersistence,
|
|
9
9
|
logger,
|
|
10
10
|
} from "@apibara/indexer/plugins";
|
|
11
|
+
import {
|
|
12
|
+
type CreateClientOptions,
|
|
13
|
+
Metadata,
|
|
14
|
+
type StreamConfig,
|
|
15
|
+
createClient,
|
|
16
|
+
} from "@apibara/protocol";
|
|
11
17
|
import consola from "consola";
|
|
12
18
|
import { config } from "#apibara-internal-virtual/config";
|
|
13
19
|
import { indexers } from "#apibara-internal-virtual/indexers";
|
|
14
20
|
import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
|
|
21
|
+
import { getProcessedRuntimeConfig } from "./helper";
|
|
15
22
|
import { createLogger } from "./logger";
|
|
16
23
|
|
|
17
24
|
export const availableIndexers = indexers.map((i) => i.name);
|
|
18
25
|
|
|
19
26
|
export function createIndexer(indexerName: string, preset?: string) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (config.presets[preset] === undefined) {
|
|
30
|
-
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
31
|
-
}
|
|
27
|
+
// Get merged runtime config from preset and process.env.APIBARA_RUNTIME_CONFIG and defaults.
|
|
28
|
+
const runtimeConfig = getProcessedRuntimeConfig({
|
|
29
|
+
preset,
|
|
30
|
+
presets: config.presets,
|
|
31
|
+
runtimeConfig: config.runtimeConfig,
|
|
32
|
+
});
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
};
|
|
36
|
-
runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
|
|
37
|
-
}
|
|
34
|
+
// Set the runtime config in the environment so that it can be used by the useRuntimeConfig hook.
|
|
35
|
+
process.env.APIBARA_RUNTIME_CONFIG_HOOK_DATA = JSON.stringify(runtimeConfig);
|
|
38
36
|
|
|
39
37
|
const indexerDefinition = indexers.find((i) => i.name === indexerName);
|
|
40
38
|
|
|
@@ -92,3 +90,27 @@ export function createIndexer(indexerName: string, preset?: string) {
|
|
|
92
90
|
|
|
93
91
|
return _createIndexer(definition);
|
|
94
92
|
}
|
|
93
|
+
|
|
94
|
+
export function createAuthenticatedClient(
|
|
95
|
+
config: StreamConfig<unknown, unknown>,
|
|
96
|
+
streamUrl: string,
|
|
97
|
+
options?: CreateClientOptions,
|
|
98
|
+
) {
|
|
99
|
+
const dnaToken = process.env.DNA_TOKEN;
|
|
100
|
+
if (!dnaToken) {
|
|
101
|
+
consola.warn(
|
|
102
|
+
"DNA_TOKEN environment variable is not set. Trying to connect without authentication.",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return createClient(config, streamUrl, {
|
|
107
|
+
...options,
|
|
108
|
+
defaultCallOptions: {
|
|
109
|
+
"*": {
|
|
110
|
+
metadata: Metadata({
|
|
111
|
+
Authorization: `Bearer ${dnaToken}`,
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import defu from "defu";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get the merged runtime config from the process.env.APIBARA_RUNTIME_CONFIG, presets and defaults.
|
|
5
|
+
* Priority (Highest to lowest):
|
|
6
|
+
* 1. process.env.APIBARA_RUNTIME_CONFIG
|
|
7
|
+
* 2. Preset
|
|
8
|
+
* 3. Defaults
|
|
9
|
+
*/
|
|
10
|
+
export function getProcessedRuntimeConfig({
|
|
11
|
+
preset,
|
|
12
|
+
presets,
|
|
13
|
+
runtimeConfig,
|
|
14
|
+
}: {
|
|
15
|
+
preset?: string;
|
|
16
|
+
presets?: Record<string, unknown>;
|
|
17
|
+
runtimeConfig?: Record<string, unknown>;
|
|
18
|
+
}) {
|
|
19
|
+
let _runtimeConfig: Record<string, unknown> = { ...runtimeConfig };
|
|
20
|
+
const envRuntimeConfig = process.env.APIBARA_RUNTIME_CONFIG;
|
|
21
|
+
|
|
22
|
+
if (preset) {
|
|
23
|
+
if (presets === undefined) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
`Specified preset "${preset}" but no presets were defined`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (presets[preset] === undefined) {
|
|
30
|
+
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const presetValue = presets[preset] as {
|
|
34
|
+
runtimeConfig: Record<string, unknown>;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Preset applied
|
|
38
|
+
_runtimeConfig = defu(presetValue.runtimeConfig, _runtimeConfig);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (envRuntimeConfig) {
|
|
42
|
+
try {
|
|
43
|
+
// Environment runtime config applied
|
|
44
|
+
const envRuntimeConfigValue = JSON.parse(envRuntimeConfig);
|
|
45
|
+
_runtimeConfig = defu(envRuntimeConfigValue, _runtimeConfig);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
throw new Error(
|
|
48
|
+
"Failed to parse runtime config from process.env.APIBARA_RUNTIME_CONFIG. Please ensure it is a valid JSON string.",
|
|
49
|
+
{ cause: error },
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return _runtimeConfig;
|
|
55
|
+
}
|
package/src/runtime/start.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { runWithReconnect } from "@apibara/indexer";
|
|
2
|
-
import { createClient } from "@apibara/protocol";
|
|
3
2
|
import { defineCommand, runMain } from "citty";
|
|
4
3
|
import consola from "consola";
|
|
5
4
|
import { register } from "#apibara-internal-virtual/instrumentation";
|
|
6
|
-
import { createIndexer } from "./internal/app";
|
|
5
|
+
import { createAuthenticatedClient, createIndexer } from "./internal/app";
|
|
7
6
|
|
|
8
7
|
const startCommand = defineCommand({
|
|
9
8
|
meta: {
|
|
@@ -30,7 +29,7 @@ const startCommand = defineCommand({
|
|
|
30
29
|
process.exit(1);
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
const client =
|
|
32
|
+
const client = createAuthenticatedClient(
|
|
34
33
|
indexerInstance.streamConfig,
|
|
35
34
|
indexerInstance.options.streamUrl,
|
|
36
35
|
);
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
function deserialize(str) {
|
|
2
|
-
return JSON.parse(
|
|
3
|
-
str,
|
|
4
|
-
(_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
|
|
5
|
-
);
|
|
6
|
-
}
|
|
7
|
-
function serialize(obj) {
|
|
8
|
-
return JSON.stringify(
|
|
9
|
-
obj,
|
|
10
|
-
(_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
|
|
11
|
-
" "
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export { deserialize as d, serialize as s };
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { ApibaraOptions } from "apibara/types";
|
|
2
|
-
import { serialize } from "../../../utils/helper";
|
|
3
|
-
|
|
4
|
-
export async function resolveRuntimeConfigOptions(options: ApibaraOptions) {
|
|
5
|
-
const { preset, presets } = options;
|
|
6
|
-
let runtimeConfig: Record<string, unknown> = { ...options.runtimeConfig };
|
|
7
|
-
|
|
8
|
-
if (preset) {
|
|
9
|
-
if (presets === undefined) {
|
|
10
|
-
throw new Error(
|
|
11
|
-
`Specified preset "${preset}" but no presets were defined`,
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (presets[preset] === undefined) {
|
|
16
|
-
throw new Error(`Specified preset "${preset}" but it was not defined`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const presetValue = presets[preset] as {
|
|
20
|
-
runtimeConfig: Record<string, unknown>;
|
|
21
|
-
};
|
|
22
|
-
runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
|
|
23
|
-
}
|
|
24
|
-
process.env.APIBARA_RUNTIME_CONFIG = serialize(runtimeConfig);
|
|
25
|
-
}
|
package/src/utils/helper.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export function deserialize<T>(str: string): T {
|
|
2
|
-
return JSON.parse(str, (_, value) =>
|
|
3
|
-
typeof value === "string" && value.match(/^\d+n$/)
|
|
4
|
-
? BigInt(value.slice(0, -1))
|
|
5
|
-
: value,
|
|
6
|
-
) as T;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function serialize<T extends Record<string, unknown>>(obj: T): string {
|
|
10
|
-
return JSON.stringify(
|
|
11
|
-
obj,
|
|
12
|
-
(_, value) => (typeof value === "bigint" ? `${value.toString()}n` : value),
|
|
13
|
-
"\t",
|
|
14
|
-
);
|
|
15
|
-
}
|