apibara 2.1.0-beta.10 → 2.1.0-beta.12

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.
@@ -45,7 +45,8 @@ const dev = defineCommand({
45
45
  }
46
46
  apibara = await createApibara(
47
47
  {
48
- rootDir
48
+ rootDir,
49
+ preset: args.preset
49
50
  },
50
51
  {
51
52
  watch: true,
@@ -26,7 +26,8 @@ const start = defineCommand({
26
26
  const { indexer, preset } = args;
27
27
  const rootDir = resolve(args.dir || args._dir || ".");
28
28
  const apibara = await createApibara({
29
- rootDir
29
+ rootDir,
30
+ preset
30
31
  });
31
32
  apibara.logger.start(
32
33
  `Starting indexer ${indexer}${preset ? ` with preset ${preset}` : ""}`
@@ -4,6 +4,7 @@ import { watchConfig, loadConfig } from 'c12';
4
4
  import { klona } from 'klona/full';
5
5
  import { resolve, join, basename, isAbsolute, relative, dirname } from 'pathe';
6
6
  import defu from 'defu';
7
+ import { s as serialize } from '../shared/apibara.af330c7d.mjs';
7
8
  import fse from 'fs-extra';
8
9
  import { getRolldownConfig } from 'apibara/rolldown';
9
10
  import { colors } from 'consola/utils';
@@ -46,8 +47,21 @@ async function presetResolver(options) {
46
47
  }
47
48
 
48
49
  async function resolveRuntimeConfigOptions(options) {
49
- options.runtimeConfig = { ...options.runtimeConfig };
50
- process.env.APIBARA_RUNTIME_CONFIG = JSON.stringify(options.runtimeConfig);
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);
51
65
  }
52
66
 
53
67
  const configResolvers = [
@@ -1,5 +1,7 @@
1
+ import { d as deserialize } from '../shared/apibara.af330c7d.mjs';
2
+
1
3
  function useRuntimeConfig() {
2
- return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG || "{}");
4
+ return deserialize(process.env.APIBARA_RUNTIME_CONFIG || "{}");
3
5
  }
4
6
 
5
7
  export { useRuntimeConfig };
@@ -1,16 +1,30 @@
1
1
  import { existsSync } from 'node:fs';
2
2
  import { builtinModules } from 'node:module';
3
+ import replace from '@rollup/plugin-replace';
3
4
  import defu from 'defu';
4
5
  import { join } from 'pathe';
6
+ import { s as serialize } from '../shared/apibara.af330c7d.mjs';
5
7
  import virtual from '@rollup/plugin-virtual';
6
8
  import { hash } from 'ohash';
7
9
 
8
10
  function appConfig(apibara) {
9
11
  return virtual({
10
12
  "#apibara-internal-virtual/config": `
11
- import * as projectConfig from '${apibara.options._c12.configFile}';
13
+ const serializedConfig = \`process.env.APIBARA_CONFIG\`;
12
14
 
13
- export const config = projectConfig.default;
15
+ if (serializedConfig === undefined || serializedConfig === "") {
16
+ throw new Error("APIBARA_CONFIG is not defined");
17
+ }
18
+
19
+ function deserialize(str) {
20
+ return JSON.parse(str, (_, value) =>
21
+ typeof value === "string" && value.match(/^\\d+n$/)
22
+ ? BigInt(value.slice(0, -1))
23
+ : value,
24
+ );
25
+ }
26
+
27
+ export const config = deserialize(serializedConfig);
14
28
  `
15
29
  });
16
30
  }
@@ -82,9 +96,31 @@ function getRolldownConfig(apibara) {
82
96
  external: [...builtinModules, ...runtimeDependencies]
83
97
  }
84
98
  );
99
+ rolldownConfig.plugins?.push(
100
+ replace({
101
+ preventAssignment: true,
102
+ values: {
103
+ "process.env.APIBARA_CONFIG": getSerializedRuntimeConfig(apibara)
104
+ }
105
+ })
106
+ );
85
107
  rolldownConfig.plugins?.push(indexers(apibara));
86
- rolldownConfig.plugins?.push(appConfig(apibara));
108
+ rolldownConfig.plugins?.push(appConfig());
87
109
  return rolldownConfig;
88
110
  }
111
+ function getSerializedRuntimeConfig(apibara) {
112
+ try {
113
+ return serialize({
114
+ runtimeConfig: apibara.options.runtimeConfig,
115
+ preset: apibara.options.preset,
116
+ presets: apibara.options.presets
117
+ });
118
+ } catch (error) {
119
+ throw new Error(
120
+ "Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable. BigInt values are supported, but functions, symbols, etc. are not. Check your configuration for non-serializable values.",
121
+ { cause: error }
122
+ );
123
+ }
124
+ }
89
125
 
90
126
  export { getRolldownConfig };
@@ -39,18 +39,11 @@ export function createIndexer(indexerName, preset) {
39
39
  return;
40
40
  }
41
41
  const definition = typeof indexerModule === "function" ? indexerModule(runtimeConfig) : indexerModule;
42
- let reporter = createLogger({
42
+ const reporter = createLogger({
43
43
  indexer: indexerName,
44
44
  preset,
45
45
  indexers: availableIndexers
46
46
  });
47
- if (config.logger) {
48
- reporter = config.logger({
49
- indexer: indexerName,
50
- preset,
51
- indexers: availableIndexers
52
- });
53
- }
54
47
  definition.plugins = [
55
48
  internalContext({
56
49
  indexerName,
@@ -0,0 +1,15 @@
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apibara",
3
- "version": "2.1.0-beta.10",
3
+ "version": "2.1.0-beta.12",
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.10",
81
+ "@apibara/starknet": "2.1.0-beta.12",
82
82
  "@types/fs-extra": "^11.0.4",
83
83
  "@types/node": "^20.14.0",
84
84
  "@types/prompts": "^2.4.9",
@@ -89,8 +89,9 @@
89
89
  "vitest": "^1.6.0"
90
90
  },
91
91
  "dependencies": {
92
- "@apibara/indexer": "2.1.0-beta.10",
93
- "@apibara/protocol": "2.1.0-beta.10",
92
+ "@apibara/indexer": "2.1.0-beta.12",
93
+ "@apibara/protocol": "2.1.0-beta.12",
94
+ "@rollup/plugin-replace": "^6.0.2",
94
95
  "@rollup/plugin-virtual": "^3.0.2",
95
96
  "c12": "^1.11.1",
96
97
  "chokidar": "^3.6.0",
@@ -56,6 +56,7 @@ export default defineCommand({
56
56
  apibara = await createApibara(
57
57
  {
58
58
  rootDir,
59
+ preset: args.preset,
59
60
  },
60
61
  {
61
62
  watch: true,
@@ -28,6 +28,7 @@ export default defineCommand({
28
28
 
29
29
  const apibara = await createApibara({
30
30
  rootDir,
31
+ preset,
31
32
  });
32
33
 
33
34
  apibara.logger.start(
@@ -1,6 +1,25 @@
1
1
  import type { ApibaraOptions } from "apibara/types";
2
+ import { serialize } from "../../../utils/helper";
2
3
 
3
4
  export async function resolveRuntimeConfigOptions(options: ApibaraOptions) {
4
- options.runtimeConfig = { ...options.runtimeConfig };
5
- process.env.APIBARA_RUNTIME_CONFIG = JSON.stringify(options.runtimeConfig);
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);
6
25
  }
@@ -1,5 +1,6 @@
1
1
  import type { ApibaraRuntimeConfig } from "apibara/types";
2
+ import { deserialize } from "../utils/helper";
2
3
 
3
4
  export function useRuntimeConfig(): ApibaraRuntimeConfig {
4
- return JSON.parse(process.env.APIBARA_RUNTIME_CONFIG || "{}");
5
+ return deserialize(process.env.APIBARA_RUNTIME_CONFIG || "{}");
5
6
  }
@@ -1,5 +1,6 @@
1
1
  import { existsSync } from "node:fs";
2
2
  import { builtinModules } from "node:module";
3
+ import replace from "@rollup/plugin-replace";
3
4
  import type { Apibara } from "apibara/types";
4
5
  import defu from "defu";
5
6
  import { join } from "pathe";
@@ -8,6 +9,7 @@ import type {
8
9
  RolldownOptions,
9
10
  RolldownPluginOption,
10
11
  } from "rolldown";
12
+ import { serialize } from "../utils/helper";
11
13
  import { appConfig } from "./plugins/config";
12
14
  import { indexers } from "./plugins/indexers";
13
15
 
@@ -76,8 +78,31 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
76
78
  },
77
79
  );
78
80
 
81
+ rolldownConfig.plugins?.push(
82
+ replace({
83
+ preventAssignment: true,
84
+ values: {
85
+ "process.env.APIBARA_CONFIG": getSerializedRuntimeConfig(apibara),
86
+ },
87
+ }) as RolldownPluginOption,
88
+ );
79
89
  rolldownConfig.plugins?.push(indexers(apibara));
80
90
  rolldownConfig.plugins?.push(appConfig(apibara));
81
91
 
82
92
  return rolldownConfig;
83
93
  }
94
+
95
+ function getSerializedRuntimeConfig(apibara: Apibara) {
96
+ try {
97
+ return serialize({
98
+ runtimeConfig: apibara.options.runtimeConfig,
99
+ preset: apibara.options.preset,
100
+ presets: apibara.options.presets,
101
+ });
102
+ } catch (error) {
103
+ throw new Error(
104
+ "Failed to serialize runtime config. Please ensure all values in your runtime configuration are JSON serializable. BigInt values are supported, but functions, symbols, etc. are not. Check your configuration for non-serializable values.",
105
+ { cause: error },
106
+ );
107
+ }
108
+ }
@@ -5,9 +5,21 @@ import type { RolldownPluginOption } from "rolldown";
5
5
  export function appConfig(apibara: Apibara) {
6
6
  return virtual({
7
7
  "#apibara-internal-virtual/config": `
8
- import * as projectConfig from '${apibara.options._c12.configFile}';
8
+ const serializedConfig = \`process.env.APIBARA_CONFIG\`;
9
9
 
10
- export const config = projectConfig.default;
10
+ if (serializedConfig === undefined || serializedConfig === "") {
11
+ throw new Error("APIBARA_CONFIG is not defined");
12
+ }
13
+
14
+ function deserialize(str) {
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);
11
23
  `,
12
24
  }) as RolldownPluginOption;
13
25
  }
@@ -56,19 +56,28 @@ export function createIndexer(indexerName: string, preset?: string) {
56
56
  ? indexerModule(runtimeConfig)
57
57
  : indexerModule;
58
58
 
59
- let reporter: ConsolaReporter = createLogger({
59
+ const reporter: ConsolaReporter = createLogger({
60
60
  indexer: indexerName,
61
61
  preset,
62
62
  indexers: availableIndexers,
63
63
  });
64
64
 
65
- if (config.logger) {
66
- reporter = config.logger({
67
- indexer: indexerName,
68
- preset,
69
- indexers: availableIndexers,
70
- });
71
- }
65
+ // TODO: Add support for custom logger
66
+ //
67
+ // Custom logger support is temporarily disabled to prevent bundling issues.
68
+ // Previously, when using the config object directly, Rollup would bundle everything
69
+ // referenced in apibara.config, including plugins. Now we serialize only the
70
+ // essential config properties (runtimeConfig, preset, presets) during build time,
71
+ // which prevents unwanted bundling but also means we can't access the logger
72
+ // configuration.
73
+ //
74
+ // if (config.logger) {
75
+ // reporter = config.logger({
76
+ // indexer: indexerName,
77
+ // preset,
78
+ // indexers: availableIndexers,
79
+ // });
80
+ // }
72
81
 
73
82
  // Put the in-memory persistence plugin first so that it can be overridden by any user-defined
74
83
  // persistence plugin.
@@ -1,3 +1,6 @@
1
1
  import type { ApibaraConfig } from "apibara/types";
2
2
 
3
- export const config: ApibaraConfig = {};
3
+ export const config: Pick<
4
+ ApibaraConfig,
5
+ "runtimeConfig" | "preset" | "presets"
6
+ > = {};
@@ -0,0 +1,15 @@
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
+ }