apibara 2.0.0-beta.9 → 2.1.0-beta.10

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.
Files changed (90) hide show
  1. package/dist/chunks/add.mjs +49 -0
  2. package/dist/chunks/build.mjs +3 -3
  3. package/dist/chunks/dev.mjs +41 -19
  4. package/dist/chunks/init.mjs +37 -0
  5. package/dist/chunks/prepare.mjs +0 -2
  6. package/dist/chunks/start.mjs +56 -0
  7. package/dist/cli/index.mjs +5 -1
  8. package/dist/config/index.d.mts +1 -1
  9. package/dist/config/index.d.ts +1 -1
  10. package/dist/core/index.mjs +127 -134
  11. package/dist/create/index.d.mts +18 -0
  12. package/dist/create/index.d.ts +18 -0
  13. package/dist/create/index.mjs +1025 -0
  14. package/dist/rolldown/index.d.mts +7 -0
  15. package/dist/rolldown/index.d.ts +7 -0
  16. package/dist/rolldown/index.mjs +90 -0
  17. package/dist/runtime/dev.d.ts +3 -0
  18. package/dist/runtime/dev.mjs +58 -0
  19. package/dist/runtime/index.d.ts +2 -0
  20. package/dist/runtime/index.mjs +2 -0
  21. package/dist/runtime/internal/app.d.ts +2 -0
  22. package/dist/runtime/internal/app.mjs +64 -0
  23. package/dist/runtime/internal/logger.d.ts +14 -0
  24. package/dist/runtime/internal/logger.mjs +45 -0
  25. package/dist/runtime/start.d.ts +3 -0
  26. package/dist/runtime/start.mjs +46 -0
  27. package/dist/types/index.d.mts +35 -29
  28. package/dist/types/index.d.ts +35 -29
  29. package/package.json +40 -22
  30. package/runtime-meta.d.ts +2 -0
  31. package/runtime-meta.mjs +7 -0
  32. package/src/cli/commands/add.ts +50 -0
  33. package/src/cli/commands/build.ts +5 -3
  34. package/src/cli/commands/dev.ts +50 -19
  35. package/src/cli/commands/init.ts +36 -0
  36. package/src/cli/commands/prepare.ts +0 -2
  37. package/src/cli/commands/start.ts +61 -0
  38. package/src/cli/index.ts +3 -0
  39. package/src/config/index.ts +5 -4
  40. package/src/core/apibara.ts +4 -2
  41. package/src/core/build/build.ts +15 -5
  42. package/src/core/build/dev.ts +44 -22
  43. package/src/core/build/error.ts +9 -15
  44. package/src/core/build/prepare.ts +5 -2
  45. package/src/core/build/prod.ts +24 -15
  46. package/src/core/build/types.ts +12 -95
  47. package/src/core/config/defaults.ts +4 -4
  48. package/src/core/config/loader.ts +1 -0
  49. package/src/core/config/resolvers/runtime-config.resolver.ts +1 -1
  50. package/src/core/config/update.ts +3 -4
  51. package/src/core/path.ts +11 -0
  52. package/src/core/scan.ts +40 -0
  53. package/src/create/add.ts +239 -0
  54. package/src/create/colors.ts +15 -0
  55. package/src/create/constants.ts +97 -0
  56. package/src/create/index.ts +2 -0
  57. package/src/create/init.ts +178 -0
  58. package/src/create/templates.ts +501 -0
  59. package/src/create/types.ts +34 -0
  60. package/src/create/utils.ts +422 -0
  61. package/src/rolldown/config.ts +83 -0
  62. package/src/rolldown/index.ts +2 -0
  63. package/src/rolldown/plugins/config.ts +13 -0
  64. package/src/rolldown/plugins/indexers.ts +17 -0
  65. package/src/runtime/dev.ts +67 -0
  66. package/src/runtime/index.ts +2 -0
  67. package/src/runtime/internal/app.ts +86 -0
  68. package/src/runtime/internal/logger.ts +70 -0
  69. package/src/runtime/start.ts +53 -0
  70. package/src/types/apibara.ts +8 -0
  71. package/src/types/config.ts +37 -31
  72. package/src/types/hooks.ts +8 -4
  73. package/src/types/index.ts +1 -1
  74. package/src/types/rolldown.ts +5 -0
  75. package/src/types/virtual/config.d.ts +3 -0
  76. package/src/types/virtual/indexers.d.ts +13 -0
  77. package/dist/internal/citty/index.d.mts +0 -1
  78. package/dist/internal/citty/index.d.ts +0 -1
  79. package/dist/internal/citty/index.mjs +0 -1
  80. package/dist/internal/consola/index.d.mts +0 -2
  81. package/dist/internal/consola/index.d.ts +0 -2
  82. package/dist/internal/consola/index.mjs +0 -1
  83. package/dist/rollup/index.d.mts +0 -5
  84. package/dist/rollup/index.d.ts +0 -5
  85. package/dist/rollup/index.mjs +0 -187
  86. package/src/internal/citty/index.ts +0 -1
  87. package/src/internal/consola/index.ts +0 -1
  88. package/src/rollup/config.ts +0 -209
  89. package/src/rollup/index.ts +0 -1
  90. package/src/types/rollup.ts +0 -8
@@ -0,0 +1,70 @@
1
+ import type {
2
+ ConsolaOptions,
3
+ ConsolaReporter,
4
+ LogLevel,
5
+ LogObject,
6
+ LogType,
7
+ } from "consola";
8
+ import { type ColorName, colors, getColor } from "consola/utils";
9
+ import { murmurHash } from "ohash";
10
+
11
+ const INDEXER_COLOR_MAP = [
12
+ colors.red,
13
+ colors.green,
14
+ colors.yellow,
15
+ colors.blue,
16
+ colors.magenta,
17
+ colors.cyan,
18
+ ];
19
+
20
+ const TYPE_COLOR_MAP: { [k in LogType]?: string } = {
21
+ info: "cyan",
22
+ fail: "red",
23
+ success: "green",
24
+ ready: "green",
25
+ start: "magenta",
26
+ };
27
+
28
+ const LEVEL_COLOR_MAP: { [k in LogLevel]?: string } = {
29
+ 0: "red",
30
+ 1: "yellow",
31
+ };
32
+
33
+ const MAX_INDEXER_NAME_LENGTH = 20;
34
+
35
+ class DefaultReporter implements ConsolaReporter {
36
+ private tag: string;
37
+
38
+ constructor(indexer: string, indexers: string[], preset?: string) {
39
+ const color =
40
+ INDEXER_COLOR_MAP[murmurHash(indexer) % INDEXER_COLOR_MAP.length];
41
+
42
+ const presetLength = preset ? preset.length : 0;
43
+
44
+ const longestIndexerName =
45
+ Math.max(...indexers.map((i) => i.length), indexer.length) + presetLength;
46
+
47
+ const paddedIndexer = `${indexer}${preset ? `:${preset} ` : ""}`
48
+ .padEnd(longestIndexerName, " ")
49
+ .slice(0, Math.min(longestIndexerName, MAX_INDEXER_NAME_LENGTH));
50
+
51
+ this.tag = color(`${paddedIndexer} |`);
52
+ }
53
+
54
+ log(logObj: LogObject, ctx: { options: ConsolaOptions }) {
55
+ const { args } = logObj;
56
+ const typeColor =
57
+ TYPE_COLOR_MAP[logObj.type] || LEVEL_COLOR_MAP[logObj.level] || "gray";
58
+
59
+ const type = getColor(typeColor as ColorName, "white")(logObj.type);
60
+ console.log(`${this.tag} ${type}`, ...args);
61
+ }
62
+ }
63
+
64
+ export function createLogger({
65
+ indexer,
66
+ indexers,
67
+ preset,
68
+ }: { indexer: string; indexers: string[]; preset?: string }) {
69
+ return new DefaultReporter(indexer, indexers, preset);
70
+ }
@@ -0,0 +1,53 @@
1
+ import { runWithReconnect } from "@apibara/indexer";
2
+ import { createClient } from "@apibara/protocol";
3
+ import { defineCommand, runMain } from "citty";
4
+ import consola from "consola";
5
+ import { createIndexer } from "./internal/app";
6
+
7
+ const startCommand = defineCommand({
8
+ meta: {
9
+ name: "start",
10
+ description: "Start the indexer",
11
+ },
12
+ args: {
13
+ indexer: {
14
+ type: "string",
15
+ description: "Indexer name",
16
+ required: true,
17
+ },
18
+ preset: {
19
+ type: "string",
20
+ description: "Preset to use",
21
+ },
22
+ },
23
+ async run({ args }) {
24
+ const { indexer, preset } = args;
25
+
26
+ const indexerInstance = createIndexer(indexer, preset);
27
+ if (!indexerInstance) {
28
+ consola.error(`Specified indexer "${indexer}" but it was not defined`);
29
+ process.exit(1);
30
+ }
31
+
32
+ const client = createClient(
33
+ indexerInstance.streamConfig,
34
+ indexerInstance.options.streamUrl,
35
+ );
36
+
37
+ await runWithReconnect(client, indexerInstance);
38
+ },
39
+ });
40
+
41
+ export const mainCli = defineCommand({
42
+ meta: {
43
+ name: "indexer-runner",
44
+ description: "Run an indexer",
45
+ },
46
+ subCommands: {
47
+ start: () => startCommand,
48
+ },
49
+ });
50
+
51
+ runMain(mainCli);
52
+
53
+ export default {};
@@ -3,9 +3,17 @@ import type { Hookable } from "hookable";
3
3
  import type { ApibaraDynamicConfig, ApibaraOptions } from "./config";
4
4
  import type { ApibaraHooks } from "./hooks";
5
5
 
6
+ export type IndexerDefinition = {
7
+ // Name of the indexer.
8
+ name: string;
9
+ // Path to the indexer file.
10
+ indexer: string;
11
+ };
12
+
6
13
  export interface Apibara {
7
14
  options: ApibaraOptions;
8
15
  hooks: Hookable<ApibaraHooks>;
16
+ indexers: IndexerDefinition[];
9
17
  logger: ConsolaInstance;
10
18
  close: () => Promise<void>;
11
19
  updateConfig: (config: ApibaraDynamicConfig) => void | Promise<void>;
@@ -1,34 +1,36 @@
1
- import type { Sink } from "@apibara/indexer";
1
+ import type { ConsolaReporter } from "@apibara/indexer/plugins";
2
2
  import type {
3
3
  C12InputConfig,
4
4
  ConfigWatcher,
5
5
  ResolvedConfig,
6
6
  WatchConfigOptions,
7
7
  } from "c12";
8
- import type { WatchOptions } from "chokidar";
9
8
  import type { NestedHooks } from "hookable";
10
- import type { TSConfig } from "pkg-types";
9
+ import type { WatchOptions } from "rolldown";
10
+ import type { RolldownOptions } from "rolldown";
11
11
  import type { DeepPartial } from "./_utils";
12
12
  import type { ApibaraHooks } from "./hooks";
13
- import type { RollupConfig } from "./rollup";
13
+
14
+ export type LoggerFactory = ({
15
+ indexer,
16
+ preset,
17
+ }: { indexer: string; indexers: string[]; preset?: string }) => ConsolaReporter;
14
18
 
15
19
  /**
16
20
  * Apibara Config type (apibara.config)
17
21
  */
18
22
  export interface ApibaraConfig<
19
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
20
- T extends Record<string, DeepPartial<ApibaraConfig<T, R>>> = {},
21
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
22
- R extends Record<string, unknown> = {},
23
- > extends DeepPartial<Omit<ApibaraOptions<T, R>, "preset" | "presets" | "dev">>,
23
+ T extends Record<
24
+ string,
25
+ DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>
26
+ > = Record<string, never>,
27
+ R extends Record<string, unknown> = Record<string, never>,
28
+ > extends Partial<Omit<ApibaraOptions<T, R>, "preset" | "presets" | "dev">>,
24
29
  C12InputConfig<ApibaraConfig<T, R>> {
25
- sink?: {
26
- default: () => Sink;
27
- [key: string]: () => Sink;
28
- };
29
30
  runtimeConfig?: R;
30
31
  presets?: T;
31
32
  preset?: keyof T;
33
+ logger?: LoggerFactory;
32
34
  }
33
35
 
34
36
  export type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
@@ -42,10 +44,11 @@ export interface LoadConfigOptions {
42
44
  }
43
45
 
44
46
  export interface ApibaraOptions<
45
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
46
- T extends Record<string, DeepPartial<ApibaraConfig<T, R>>> = {},
47
- // biome-ignore lint/complexity/noBannedTypes: <explanation>
48
- R extends Record<string, unknown> = {},
47
+ T extends Record<
48
+ string,
49
+ DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>
50
+ > = Record<string, never>,
51
+ R extends Record<string, unknown> = Record<string, never>,
49
52
  > {
50
53
  // Internal
51
54
  _config: ApibaraConfig<T, R>;
@@ -53,12 +56,6 @@ export interface ApibaraOptions<
53
56
  | ResolvedConfig<ApibaraConfig<T, R>>
54
57
  | ConfigWatcher<ApibaraConfig<T, R>>;
55
58
 
56
- // Sink
57
- sink: {
58
- default: () => Sink;
59
- [key: string]: () => Sink;
60
- };
61
-
62
59
  // Presets
63
60
  presets?: T;
64
61
  preset?: keyof T;
@@ -69,25 +66,34 @@ export interface ApibaraOptions<
69
66
  rootDir: string;
70
67
  buildDir: string;
71
68
  outputDir: string;
69
+ indexersDir: string;
70
+
72
71
  // Dev
73
72
  dev: boolean;
74
- watchOptions: WatchOptions;
73
+ watchOptions: WatchOptions["watch"];
75
74
 
76
75
  // Hooks
77
76
  hooks: NestedHooks<ApibaraHooks>;
78
- // Rollup
79
- rollupConfig?: RollupConfig;
77
+
78
+ // Logging
79
+ logger?: LoggerFactory;
80
+
81
+ // Rolldown
82
+ rolldownConfig?: Partial<RolldownOptions>;
83
+
84
+ /**
85
+ * @deprecated Use rolldownConfig instead. This option will be removed in future releases.
86
+ */
87
+ rollupConfig?: unknown;
88
+ sourceMap?: boolean;
80
89
  entry: string;
81
- minify: boolean;
90
+ node: boolean;
91
+ exportConditions?: string[];
82
92
 
83
93
  // Advanced
84
94
  typescript: {
85
95
  strict?: boolean;
86
96
  internalPaths?: boolean;
87
97
  generateRuntimeConfigTypes?: boolean;
88
- generateTsConfig?: boolean;
89
- /** the path of the generated `tsconfig.json`, relative to buildDir */
90
- tsconfigPath: string;
91
- tsConfig?: Partial<TSConfig>;
92
98
  };
93
99
  }
@@ -1,11 +1,15 @@
1
+ import type { RolldownOptions } from "rolldown";
1
2
  import type { Apibara } from "./apibara";
2
- import type { RollupConfig } from "./rollup";
3
3
 
4
4
  export interface ApibaraHooks {
5
- "rollup:before": (apibara: Apibara, rollupConfig: RollupConfig) => void;
5
+ "rolldown:before": (
6
+ apibara: Apibara,
7
+ rolldownConfig: RolldownOptions,
8
+ ) => void;
6
9
  compiled: (apibara: Apibara) => void;
7
- "dev:reload": () => void;
8
- "rollup:reload": () => void;
10
+ "dev:restart": () => Promise<void>;
11
+ "dev:reload": () => Promise<void>;
12
+ "rolldown:reload": () => Promise<void>;
9
13
  restart: () => void;
10
14
  close: () => void;
11
15
  }
@@ -2,5 +2,5 @@ export * from "./apibara";
2
2
  export * from "./_utils";
3
3
  export * from "./config";
4
4
  export * from "./hooks";
5
- export * from "./rollup";
5
+ export * from "./rolldown";
6
6
  export * from "./runtime";
@@ -0,0 +1,5 @@
1
+ import type { InputOptions, OutputOptions } from "rolldown";
2
+
3
+ export type RolldownConfig = InputOptions & {
4
+ output: OutputOptions;
5
+ };
@@ -0,0 +1,3 @@
1
+ import type { ApibaraConfig } from "apibara/types";
2
+
3
+ export const config: ApibaraConfig = {};
@@ -0,0 +1,13 @@
1
+ import type { IndexerWithStreamConfig } from "@apibara/indexer";
2
+ import type { ApibaraRuntimeConfig } from "apibara/types";
3
+
4
+ export type IndexerConstructor =
5
+ | ((
6
+ runtimeConfig: ApibaraRuntimeConfig,
7
+ ) => IndexerWithStreamConfig<unknown, unknown, unknown>)
8
+ | IndexerWithStreamConfig<unknown, unknown, unknown>;
9
+
10
+ export const indexers: {
11
+ name: string;
12
+ indexer: { default?: IndexerConstructor | undefined };
13
+ }[] = [];
@@ -1 +0,0 @@
1
- export * from 'citty';
@@ -1 +0,0 @@
1
- export * from 'citty';
@@ -1 +0,0 @@
1
- export * from 'citty';
@@ -1,2 +0,0 @@
1
- export * from 'consola';
2
- import 'citty';
@@ -1,2 +0,0 @@
1
- export * from 'consola';
2
- import 'citty';
@@ -1 +0,0 @@
1
- export * from 'consola';
@@ -1,5 +0,0 @@
1
- import { Apibara, RollupConfig } from 'apibara/types';
2
-
3
- declare const getRollupConfig: (apibara: Apibara, dev?: boolean) => RollupConfig;
4
-
5
- export { getRollupConfig };
@@ -1,5 +0,0 @@
1
- import { Apibara, RollupConfig } from 'apibara/types';
2
-
3
- declare const getRollupConfig: (apibara: Apibara, dev?: boolean) => RollupConfig;
4
-
5
- export { getRollupConfig };
@@ -1,187 +0,0 @@
1
- import { existsSync } from 'node:fs';
2
- import { builtinModules } from 'node:module';
3
- import commonjs from '@rollup/plugin-commonjs';
4
- import json from '@rollup/plugin-json';
5
- import { nodeResolve } from '@rollup/plugin-node-resolve';
6
- import typescript from '@rollup/plugin-typescript';
7
- import fse from 'fs-extra';
8
- import { join, basename } from 'pathe';
9
-
10
- const getRollupConfig = (apibara, dev = false) => {
11
- const extensions = [
12
- ".ts",
13
- ".mjs",
14
- ".js",
15
- ".json",
16
- ".node",
17
- ".tsx",
18
- ".jsx"
19
- ];
20
- const indexerDir = join(apibara.options.rootDir, "indexers");
21
- const configPath = join(apibara.options.rootDir, "./apibara.config.ts");
22
- if (!existsSync(indexerDir)) {
23
- throw new Error(`Indexers directory not found: ${indexerDir}`);
24
- }
25
- if (!existsSync(configPath)) {
26
- throw new Error(`Config file not found: ${configPath}`);
27
- }
28
- let indexerFiles = [];
29
- try {
30
- indexerFiles = fse.readdirSync(indexerDir).filter((file) => file.endsWith(".indexer.ts"));
31
- if (indexerFiles.length === 0) {
32
- console.warn(`No indexer files found in ${indexerDir}`);
33
- }
34
- } catch (error) {
35
- console.error(`Error reading indexers directory: ${error}`);
36
- }
37
- const indexerImports = indexerFiles.map(
38
- (file, index) => `import indexer${index} from '${join(indexerDir, file)}';`
39
- ).join("\n");
40
- const mainContent = `
41
- import { createClient } from "@apibara/protocol";
42
- import { createIndexer, run } from "@apibara/indexer";
43
- import { consola as _consola } from "apibara/internal/consola";
44
- import { defineCommand, runMain } from "apibara/internal/citty";
45
- import config from './${configPath}';
46
-
47
- const consola = _consola.withTag("Apibara | ");
48
-
49
- ${indexerImports}
50
-
51
- const indexers = {
52
- ${indexerFiles.map(
53
- (file, index) => `'${basename(file, ".indexer.ts")}': indexer${index},`
54
- ).join("\n ")}
55
- };
56
-
57
- const command = defineCommand({
58
- meta: {
59
- name: "run-indexers",
60
- description: "Run Apibara indexers",
61
- },
62
- args: {
63
- indexers: {
64
- type: "string",
65
- description: "Comma-separated list of indexers to run",
66
- },
67
- preset: {
68
- type: "string",
69
- description: "Preset to use",
70
- },
71
- sink: {
72
- type: "string",
73
- description: "Sink to use",
74
- },
75
- },
76
- async run({ args }) {
77
- const selectedIndexers = args.indexers ? args.indexers.split(',') : Object.keys(indexers);
78
- const preset = args.preset || config.preset || 'default';
79
- const sinkName = args.sink || 'default';
80
-
81
- // Apply preset
82
- let runtimeConfig = { ...config.runtimeConfig };
83
- if (preset && config.presets && config.presets[preset]) {
84
- runtimeConfig = { ...runtimeConfig, ...config.presets[preset].runtimeConfig };
85
- }
86
-
87
- // Get sink function
88
- const sinkFunction = config.sink?.[sinkName];
89
- if (!sinkFunction) {
90
- throw new Error(\`Sink \${sinkName} not found\`);
91
- }
92
-
93
- await Promise.all(selectedIndexers.map(async (name) => {
94
- if (!indexers[name]) {
95
- console.error(\`Indexer \${name} not found\`);
96
- return;
97
- }
98
-
99
- const indexerConfig = indexers[name]
100
- const indexer = typeof indexerConfig === 'function'
101
- ? await createIndexer(indexerConfig(runtimeConfig))
102
- : createIndexer(indexerConfig);
103
-
104
- const client = createClient(indexer.streamConfig, indexer.options.streamUrl);
105
- const sink = sinkFunction();
106
-
107
- try {
108
- consola.log("Running Indexer: ", name);
109
- await run(client, indexer, sink);
110
- } catch (error) {
111
- consola.error(\`Error in indexer \${name}:\`, error);
112
- }
113
- }));
114
- },
115
- });
116
-
117
- runMain(command);
118
- `;
119
- return {
120
- input: {
121
- main: "virtual:main.ts"
122
- },
123
- output: {
124
- dir: join(apibara.options.outputDir || "./.apibara/build"),
125
- format: "esm",
126
- exports: "auto",
127
- entryFileNames: "[name].mjs",
128
- chunkFileNames: "chunks/[name]-[hash].mjs",
129
- generatedCode: {
130
- constBindings: true
131
- },
132
- sourcemap: true,
133
- sourcemapExcludeSources: true,
134
- sourcemapIgnoreList(relativePath, sourcemapPath) {
135
- return relativePath.includes("node_modules");
136
- }
137
- },
138
- plugins: [
139
- {
140
- name: "virtual",
141
- resolveId(id) {
142
- if (id === "virtual:main.ts") {
143
- return id;
144
- }
145
- return null;
146
- },
147
- load(id) {
148
- if (id === "virtual:main.ts") {
149
- return mainContent;
150
- }
151
- return null;
152
- }
153
- },
154
- nodeResolve({
155
- extensions,
156
- preferBuiltins: true
157
- }),
158
- commonjs(),
159
- json(),
160
- typescript({
161
- tsconfig: join(apibara.options.rootDir, "./tsconfig.json"),
162
- compilerOptions: {
163
- outDir: join(apibara.options.outputDir || "./.apibara/build"),
164
- declarationDir: join(apibara.options.outputDir || "./.apibara/build"),
165
- noEmit: false,
166
- types: ["node"]
167
- }
168
- })
169
- ],
170
- onwarn(warning, rollupWarn) {
171
- if (!["CIRCULAR_DEPENDENCY", "EVAL"].includes(warning.code || "") && !warning.message.includes("Unsupported source map comment") && !warning.message.includes("@__PURE__")) {
172
- rollupWarn(warning);
173
- }
174
- },
175
- treeshake: true,
176
- external: [
177
- ...builtinModules,
178
- "@apibara/indexer",
179
- "@apibara/protocol",
180
- "@apibara/evm",
181
- "@apibara/starknet",
182
- "@apibara/beaconchain"
183
- ]
184
- };
185
- };
186
-
187
- export { getRollupConfig };
@@ -1 +0,0 @@
1
- export * from "citty";
@@ -1 +0,0 @@
1
- export * from "consola";