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

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 (83) hide show
  1. package/dist/chunks/add.mjs +44 -0
  2. package/dist/chunks/build.mjs +3 -3
  3. package/dist/chunks/dev.mjs +22 -18
  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 +61 -97
  11. package/dist/create/index.d.mts +17 -0
  12. package/dist/create/index.d.ts +17 -0
  13. package/dist/create/index.mjs +981 -0
  14. package/dist/rollup/index.d.mts +2 -1
  15. package/dist/rollup/index.d.ts +2 -1
  16. package/dist/rollup/index.mjs +130 -167
  17. package/dist/runtime/dev.d.ts +3 -0
  18. package/dist/runtime/dev.mjs +55 -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 +56 -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 +41 -0
  27. package/dist/types/index.d.mts +22 -19
  28. package/dist/types/index.d.ts +22 -19
  29. package/package.json +35 -13
  30. package/runtime-meta.d.ts +2 -0
  31. package/runtime-meta.mjs +7 -0
  32. package/src/cli/commands/add.ts +44 -0
  33. package/src/cli/commands/build.ts +5 -3
  34. package/src/cli/commands/dev.ts +28 -18
  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 +2 -0
  42. package/src/core/build/dev.ts +1 -0
  43. package/src/core/build/error.ts +0 -1
  44. package/src/core/build/prepare.ts +5 -2
  45. package/src/core/build/prod.ts +10 -6
  46. package/src/core/build/types.ts +4 -95
  47. package/src/core/config/defaults.ts +1 -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 +2 -3
  51. package/src/core/path.ts +11 -0
  52. package/src/core/scan.ts +40 -0
  53. package/src/create/add.ts +238 -0
  54. package/src/create/colors.ts +15 -0
  55. package/src/create/constants.ts +98 -0
  56. package/src/create/index.ts +2 -0
  57. package/src/create/init.ts +175 -0
  58. package/src/create/templates.ts +468 -0
  59. package/src/create/types.ts +34 -0
  60. package/src/create/utils.ts +422 -0
  61. package/src/rollup/config.ts +67 -189
  62. package/src/rollup/index.ts +1 -0
  63. package/src/rollup/plugins/config.ts +12 -0
  64. package/src/rollup/plugins/esm-shim.ts +69 -0
  65. package/src/rollup/plugins/indexers.ts +17 -0
  66. package/src/runtime/dev.ts +64 -0
  67. package/src/runtime/index.ts +2 -0
  68. package/src/runtime/internal/app.ts +78 -0
  69. package/src/runtime/internal/logger.ts +70 -0
  70. package/src/runtime/start.ts +48 -0
  71. package/src/types/apibara.ts +8 -0
  72. package/src/types/config.ts +28 -27
  73. package/src/types/hooks.ts +1 -0
  74. package/src/types/virtual/config.d.ts +3 -0
  75. package/src/types/virtual/indexers.d.ts +10 -0
  76. package/dist/internal/citty/index.d.mts +0 -1
  77. package/dist/internal/citty/index.d.ts +0 -1
  78. package/dist/internal/citty/index.mjs +0 -1
  79. package/dist/internal/consola/index.d.mts +0 -2
  80. package/dist/internal/consola/index.d.ts +0 -2
  81. package/dist/internal/consola/index.mjs +0 -1
  82. package/src/internal/citty/index.ts +0 -1
  83. package/src/internal/consola/index.ts +0 -1
@@ -1,5 +1,6 @@
1
1
  import { Apibara, RollupConfig } from 'apibara/types';
2
+ export { Plugin } from 'rollup';
2
3
 
3
- declare const getRollupConfig: (apibara: Apibara, dev?: boolean) => RollupConfig;
4
+ declare function getRollupConfig(apibara: Apibara): RollupConfig;
4
5
 
5
6
  export { getRollupConfig };
@@ -1,5 +1,6 @@
1
1
  import { Apibara, RollupConfig } from 'apibara/types';
2
+ export { Plugin } from 'rollup';
2
3
 
3
- declare const getRollupConfig: (apibara: Apibara, dev?: boolean) => RollupConfig;
4
+ declare function getRollupConfig(apibara: Apibara): RollupConfig;
4
5
 
5
6
  export { getRollupConfig };
@@ -1,13 +1,90 @@
1
- import { existsSync } from 'node:fs';
2
1
  import { builtinModules } from 'node:module';
3
2
  import commonjs from '@rollup/plugin-commonjs';
4
3
  import json from '@rollup/plugin-json';
5
4
  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';
5
+ import { join } from 'pathe';
6
+ import defu from 'defu';
7
+ import virtual from '@rollup/plugin-virtual';
8
+ import MagicString from 'magic-string';
9
+ import { hash } from 'ohash';
9
10
 
10
- const getRollupConfig = (apibara, dev = false) => {
11
+ function appConfig(apibara) {
12
+ return virtual({
13
+ "#apibara-internal-virtual/config": `
14
+ import * as projectConfig from '${apibara.options._c12.configFile}';
15
+
16
+ export const config = projectConfig.default;
17
+ `
18
+ });
19
+ }
20
+
21
+ function esmShim() {
22
+ const ESMShim = `
23
+ // -- Shims --
24
+ import cjsUrl from 'node:url';
25
+ import cjsPath from 'node:path';
26
+ const __filename = cjsUrl.fileURLToPath(import.meta.url);
27
+ const __dirname = cjsPath.dirname(__filename);
28
+ // -- End Shims --
29
+ `;
30
+ const CJSyntaxRegex = /__filename|__dirname/;
31
+ return {
32
+ name: "esm-shim",
33
+ renderChunk(code, _chunk, opts) {
34
+ if (opts.format === "es") {
35
+ if (code.includes(ESMShim) || !CJSyntaxRegex.test(code)) {
36
+ return null;
37
+ }
38
+ let endIndexOfLastImport = -1;
39
+ for (const match of code.matchAll(/^import\s.*';$/gm)) {
40
+ if (match.length === 0 || typeof match.index !== "number") {
41
+ continue;
42
+ }
43
+ endIndexOfLastImport = match.index + match[0].length;
44
+ }
45
+ const s = new MagicString(code);
46
+ s.appendRight(endIndexOfLastImport, ESMShim);
47
+ const sourceMap = s.generateMap({
48
+ includeContent: true
49
+ });
50
+ let sourcesContent;
51
+ if (Array.isArray(sourceMap.sourcesContent)) {
52
+ sourcesContent = [];
53
+ for (let i = 0; i < sourceMap.sourcesContent.length; i++) {
54
+ const content = sourceMap.sourcesContent[i];
55
+ if (typeof content === "string") {
56
+ sourcesContent.push(content);
57
+ }
58
+ }
59
+ }
60
+ return {
61
+ code: s.toString(),
62
+ map: {
63
+ ...sourceMap,
64
+ sourcesContent
65
+ }
66
+ };
67
+ }
68
+ return null;
69
+ }
70
+ };
71
+ }
72
+
73
+ function indexers(apibara) {
74
+ const indexers2 = [...new Set(apibara.indexers)];
75
+ return virtual({
76
+ "#apibara-internal-virtual/indexers": `
77
+ ${indexers2.map((i) => `import _${hash(i)} from '${i.indexer}';`).join("\n")}
78
+
79
+ export const indexers = [
80
+ ${indexers2.map((i) => `{ name: "${i.name}", indexer: _${hash(i)} }`).join(",\n")}
81
+ ];
82
+ `
83
+ });
84
+ }
85
+
86
+ const runtimeDependencies = ["better-sqlite3", "@electric-sql/pglite"];
87
+ function getRollupConfig(apibara) {
11
88
  const extensions = [
12
89
  ".ts",
13
90
  ".mjs",
@@ -17,171 +94,57 @@ const getRollupConfig = (apibara, dev = false) => {
17
94
  ".tsx",
18
95
  ".jsx"
19
96
  ];
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;
97
+ const rollupConfig = defu(
98
+ // biome-ignore lint/suspicious/noExplicitAny: apibara.options.rollupConfig is typed
99
+ apibara.options.rollupConfig,
100
+ {
101
+ input: apibara.options.entry,
102
+ output: {
103
+ dir: join(apibara.options.outputDir || "./.apibara/build"),
104
+ format: "esm",
105
+ exports: "auto",
106
+ entryFileNames: "[name].mjs",
107
+ chunkFileNames: "chunks/[name]-[hash].mjs",
108
+ generatedCode: {
109
+ constBindings: true
146
110
  },
147
- load(id) {
148
- if (id === "virtual:main.ts") {
149
- return mainContent;
150
- }
151
- return null;
111
+ sourcemap: true,
112
+ sourcemapExcludeSources: true,
113
+ sourcemapIgnoreList(relativePath, sourcemapPath) {
114
+ return relativePath.includes("node_modules");
152
115
  }
153
116
  },
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"]
117
+ plugins: [],
118
+ onwarn(warning, rollupWarn) {
119
+ if (!["CIRCULAR_DEPENDENCY", "EVAL", "THIS_IS_UNDEFINED"].includes(
120
+ warning.code || ""
121
+ ) && !warning.message.includes("Unsupported source map comment") && !warning.message.includes("@__PURE__") && !warning.message.includes("/*#__PURE__*/")) {
122
+ rollupWarn(warning);
167
123
  }
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
- };
124
+ },
125
+ treeshake: true,
126
+ external: [...builtinModules, ...runtimeDependencies]
127
+ }
128
+ );
129
+ rollupConfig.plugins.push(esmShim());
130
+ rollupConfig.plugins.push(json());
131
+ rollupConfig.plugins.push(
132
+ commonjs({
133
+ strictRequires: true,
134
+ requireReturnsDefault: "auto",
135
+ ...apibara.options.commonJS
136
+ })
137
+ );
138
+ rollupConfig.plugins.push(
139
+ nodeResolve({
140
+ extensions,
141
+ preferBuiltins: true,
142
+ mainFields: ["main"]
143
+ })
144
+ );
145
+ rollupConfig.plugins.push(indexers(apibara));
146
+ rollupConfig.plugins.push(appConfig(apibara));
147
+ return rollupConfig;
148
+ }
186
149
 
187
150
  export { getRollupConfig };
@@ -0,0 +1,3 @@
1
+ export declare const mainCli: import("citty").CommandDef<import("citty").ArgsDef>;
2
+ declare const _default: {};
3
+ export default _default;
@@ -0,0 +1,55 @@
1
+ import { runWithReconnect } from "@apibara/indexer";
2
+ import { createClient } from "@apibara/protocol";
3
+ import { defineCommand, runMain } from "citty";
4
+ import { availableIndexers, createIndexer } from "./internal/app.mjs";
5
+ const startCommand = defineCommand({
6
+ meta: {
7
+ name: "start",
8
+ description: "Start the indexer"
9
+ },
10
+ args: {
11
+ indexers: {
12
+ type: "string",
13
+ description: "Which indexers to run"
14
+ },
15
+ preset: {
16
+ type: "string",
17
+ description: "Preset to use"
18
+ }
19
+ },
20
+ async run({ args }) {
21
+ const { indexers: indexersArgs, preset } = args;
22
+ let selectedIndexers = availableIndexers;
23
+ if (indexersArgs) {
24
+ selectedIndexers = indexersArgs.split(",");
25
+ }
26
+ for (const indexer of selectedIndexers) {
27
+ if (!availableIndexers.includes(indexer)) {
28
+ throw new Error(
29
+ `Specified indexer "${indexer}" but it was not defined`
30
+ );
31
+ }
32
+ }
33
+ await Promise.all(
34
+ selectedIndexers.map(async (indexer) => {
35
+ const indexerInstance = createIndexer(indexer, preset);
36
+ const client = createClient(
37
+ indexerInstance.streamConfig,
38
+ indexerInstance.options.streamUrl
39
+ );
40
+ await runWithReconnect(client, indexerInstance);
41
+ })
42
+ );
43
+ }
44
+ });
45
+ export const mainCli = defineCommand({
46
+ meta: {
47
+ name: "indexer-dev-runner",
48
+ description: "Run indexer in dev mode"
49
+ },
50
+ subCommands: {
51
+ start: () => startCommand
52
+ }
53
+ });
54
+ runMain(mainCli);
55
+ export default {};
@@ -0,0 +1,2 @@
1
+ export { createIndexer } from "./internal/app";
2
+ export { createLogger } from "./internal/logger";
@@ -0,0 +1,2 @@
1
+ export { createIndexer } from "./internal/app.mjs";
2
+ export { createLogger } from "./internal/logger.mjs";
@@ -0,0 +1,2 @@
1
+ export declare const availableIndexers: any;
2
+ export declare function createIndexer(indexerName: string, preset?: string): import("@apibara/indexer").Indexer<unknown, unknown>;
@@ -0,0 +1,56 @@
1
+ import { createIndexer as _createIndexer } from "@apibara/indexer";
2
+ import {
3
+ internalContext
4
+ } from "@apibara/indexer/internal/plugins";
5
+ import {
6
+ inMemoryPersistence,
7
+ logger
8
+ } from "@apibara/indexer/plugins";
9
+ import { config } from "#apibara-internal-virtual/config";
10
+ import { indexers } from "#apibara-internal-virtual/indexers";
11
+ import { createLogger } from "./logger.mjs";
12
+ export const availableIndexers = indexers.map((i) => i.name);
13
+ export function createIndexer(indexerName, preset) {
14
+ let runtimeConfig = { ...config.runtimeConfig };
15
+ if (preset) {
16
+ if (config.presets === void 0) {
17
+ throw new Error(
18
+ `Specified preset "${preset}" but no presets were defined`
19
+ );
20
+ }
21
+ if (config.presets[preset] === void 0) {
22
+ throw new Error(`Specified preset "${preset}" but it was not defined`);
23
+ }
24
+ const presetValue = config.presets[preset];
25
+ runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
26
+ }
27
+ const indexerDefinition = indexers.find((i) => i.name === indexerName);
28
+ if (indexerDefinition === void 0) {
29
+ throw new Error(
30
+ `Specified indexer "${indexerName}" but it was not defined`
31
+ );
32
+ }
33
+ const definition = typeof indexerDefinition.indexer === "function" ? indexerDefinition.indexer(runtimeConfig) : indexerDefinition.indexer;
34
+ let reporter = createLogger({
35
+ indexer: indexerName,
36
+ preset,
37
+ indexers: availableIndexers
38
+ });
39
+ if (config.logger) {
40
+ reporter = config.logger({
41
+ indexer: indexerName,
42
+ preset,
43
+ indexers: availableIndexers
44
+ });
45
+ }
46
+ definition.plugins = [
47
+ internalContext({
48
+ indexerName,
49
+ availableIndexers
50
+ }),
51
+ inMemoryPersistence(),
52
+ ...definition.plugins ?? [],
53
+ logger({ logger: reporter })
54
+ ];
55
+ return _createIndexer(definition);
56
+ }
@@ -0,0 +1,14 @@
1
+ import type { ConsolaOptions, ConsolaReporter, LogObject } from "consola";
2
+ declare class DefaultReporter implements ConsolaReporter {
3
+ private tag;
4
+ constructor(indexer: string, indexers: string[], preset?: string);
5
+ log(logObj: LogObject, ctx: {
6
+ options: ConsolaOptions;
7
+ }): void;
8
+ }
9
+ export declare function createLogger({ indexer, indexers, preset, }: {
10
+ indexer: string;
11
+ indexers: string[];
12
+ preset?: string;
13
+ }): DefaultReporter;
14
+ export {};
@@ -0,0 +1,45 @@
1
+ import { colors, getColor } from "consola/utils";
2
+ import { murmurHash } from "ohash";
3
+ const INDEXER_COLOR_MAP = [
4
+ colors.red,
5
+ colors.green,
6
+ colors.yellow,
7
+ colors.blue,
8
+ colors.magenta,
9
+ colors.cyan
10
+ ];
11
+ const TYPE_COLOR_MAP = {
12
+ info: "cyan",
13
+ fail: "red",
14
+ success: "green",
15
+ ready: "green",
16
+ start: "magenta"
17
+ };
18
+ const LEVEL_COLOR_MAP = {
19
+ 0: "red",
20
+ 1: "yellow"
21
+ };
22
+ const MAX_INDEXER_NAME_LENGTH = 20;
23
+ class DefaultReporter {
24
+ tag;
25
+ constructor(indexer, indexers, preset) {
26
+ const color = INDEXER_COLOR_MAP[murmurHash(indexer) % INDEXER_COLOR_MAP.length];
27
+ const presetLength = preset ? preset.length : 0;
28
+ const longestIndexerName = Math.max(...indexers.map((i) => i.length), indexer.length) + presetLength;
29
+ const paddedIndexer = `${indexer}${preset ? `:${preset} ` : ""}`.padEnd(longestIndexerName, " ").slice(0, Math.min(longestIndexerName, MAX_INDEXER_NAME_LENGTH));
30
+ this.tag = color(`${paddedIndexer} |`);
31
+ }
32
+ log(logObj, ctx) {
33
+ const { args } = logObj;
34
+ const typeColor = TYPE_COLOR_MAP[logObj.type] || LEVEL_COLOR_MAP[logObj.level] || "gray";
35
+ const type = getColor(typeColor, "white")(logObj.type);
36
+ console.log(`${this.tag} ${type}`, ...args);
37
+ }
38
+ }
39
+ export function createLogger({
40
+ indexer,
41
+ indexers,
42
+ preset
43
+ }) {
44
+ return new DefaultReporter(indexer, indexers, preset);
45
+ }
@@ -0,0 +1,3 @@
1
+ export declare const mainCli: import("citty").CommandDef<import("citty").ArgsDef>;
2
+ declare const _default: {};
3
+ export default _default;
@@ -0,0 +1,41 @@
1
+ import { runWithReconnect } from "@apibara/indexer";
2
+ import { createClient } from "@apibara/protocol";
3
+ import { defineCommand, runMain } from "citty";
4
+ import { createIndexer } from "./internal/app.mjs";
5
+ const startCommand = defineCommand({
6
+ meta: {
7
+ name: "start",
8
+ description: "Start the indexer"
9
+ },
10
+ args: {
11
+ indexer: {
12
+ type: "string",
13
+ description: "Indexer name",
14
+ required: true
15
+ },
16
+ preset: {
17
+ type: "string",
18
+ description: "Preset to use"
19
+ }
20
+ },
21
+ async run({ args }) {
22
+ const { indexer, preset } = args;
23
+ const indexerInstance = createIndexer(indexer, preset);
24
+ const client = createClient(
25
+ indexerInstance.streamConfig,
26
+ indexerInstance.options.streamUrl
27
+ );
28
+ await runWithReconnect(client, indexerInstance);
29
+ }
30
+ });
31
+ export const mainCli = defineCommand({
32
+ meta: {
33
+ name: "indexer-runner",
34
+ description: "Run an indexer"
35
+ },
36
+ subCommands: {
37
+ start: () => startCommand
38
+ }
39
+ });
40
+ runMain(mainCli);
41
+ export default {};
@@ -1,9 +1,9 @@
1
1
  import { ConsolaInstance } from 'consola';
2
2
  import { NestedHooks, Hookable } from 'hookable';
3
- import { Sink } from '@apibara/indexer';
3
+ import { ConsolaReporter } from '@apibara/indexer/plugins';
4
+ import { RollupCommonJSOptions } from '@rollup/plugin-commonjs';
4
5
  import { C12InputConfig, WatchConfigOptions, ResolvedConfig, ConfigWatcher } from 'c12';
5
6
  import { WatchOptions } from 'chokidar';
6
- import { TSConfig } from 'pkg-types';
7
7
  import { InputOptions, OutputOptions } from 'rollup';
8
8
 
9
9
  type DeepPartial<T> = T extends Record<string, any> ? {
@@ -17,23 +17,26 @@ type RollupConfig = InputOptions & {
17
17
  interface ApibaraHooks {
18
18
  "rollup:before": (apibara: Apibara, rollupConfig: RollupConfig) => void;
19
19
  compiled: (apibara: Apibara) => void;
20
+ "dev:restart": () => void;
20
21
  "dev:reload": () => void;
21
22
  "rollup:reload": () => void;
22
23
  restart: () => void;
23
24
  close: () => void;
24
25
  }
25
26
 
27
+ type LoggerFactory = ({ indexer, preset, }: {
28
+ indexer: string;
29
+ indexers: string[];
30
+ preset?: string;
31
+ }) => ConsolaReporter;
26
32
  /**
27
33
  * Apibara Config type (apibara.config)
28
34
  */
29
- interface ApibaraConfig<T extends Record<string, DeepPartial<ApibaraConfig<T, R>>> = {}, R extends Record<string, unknown> = {}> extends DeepPartial<Omit<ApibaraOptions<T, R>, "preset" | "presets" | "dev">>, C12InputConfig<ApibaraConfig<T, R>> {
30
- sink?: {
31
- default: () => Sink;
32
- [key: string]: () => Sink;
33
- };
35
+ interface ApibaraConfig<T extends Record<string, DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>> = Record<string, never>, R extends Record<string, unknown> = Record<string, never>> extends Partial<Omit<ApibaraOptions<T, R>, "preset" | "presets" | "dev">>, C12InputConfig<ApibaraConfig<T, R>> {
34
36
  runtimeConfig?: R;
35
37
  presets?: T;
36
38
  preset?: keyof T;
39
+ logger?: LoggerFactory;
37
40
  }
38
41
  type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
39
42
  /**
@@ -43,13 +46,9 @@ interface LoadConfigOptions {
43
46
  watch?: boolean;
44
47
  c12?: WatchConfigOptions;
45
48
  }
46
- interface ApibaraOptions<T extends Record<string, DeepPartial<ApibaraConfig<T, R>>> = {}, R extends Record<string, unknown> = {}> {
49
+ interface ApibaraOptions<T extends Record<string, DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>> = Record<string, never>, R extends Record<string, unknown> = Record<string, never>> {
47
50
  _config: ApibaraConfig<T, R>;
48
51
  _c12: ResolvedConfig<ApibaraConfig<T, R>> | ConfigWatcher<ApibaraConfig<T, R>>;
49
- sink: {
50
- default: () => Sink;
51
- [key: string]: () => Sink;
52
- };
53
52
  presets?: T;
54
53
  preset?: keyof T;
55
54
  debug: boolean;
@@ -57,26 +56,30 @@ interface ApibaraOptions<T extends Record<string, DeepPartial<ApibaraConfig<T, R
57
56
  rootDir: string;
58
57
  buildDir: string;
59
58
  outputDir: string;
59
+ indexersDir: string;
60
60
  dev: boolean;
61
61
  watchOptions: WatchOptions;
62
62
  hooks: NestedHooks<ApibaraHooks>;
63
- rollupConfig?: RollupConfig;
63
+ logger?: LoggerFactory;
64
+ rollupConfig?: Partial<RollupConfig>;
65
+ sourceMap?: boolean;
64
66
  entry: string;
65
- minify: boolean;
67
+ commonJS?: RollupCommonJSOptions;
66
68
  typescript: {
67
69
  strict?: boolean;
68
70
  internalPaths?: boolean;
69
71
  generateRuntimeConfigTypes?: boolean;
70
- generateTsConfig?: boolean;
71
- /** the path of the generated `tsconfig.json`, relative to buildDir */
72
- tsconfigPath: string;
73
- tsConfig?: Partial<TSConfig>;
74
72
  };
75
73
  }
76
74
 
75
+ type IndexerDefinition = {
76
+ name: string;
77
+ indexer: string;
78
+ };
77
79
  interface Apibara {
78
80
  options: ApibaraOptions;
79
81
  hooks: Hookable<ApibaraHooks>;
82
+ indexers: IndexerDefinition[];
80
83
  logger: ConsolaInstance;
81
84
  close: () => Promise<void>;
82
85
  updateConfig: (config: ApibaraDynamicConfig) => void | Promise<void>;
@@ -84,4 +87,4 @@ interface Apibara {
84
87
 
85
88
  type ApibaraRuntimeConfig = Record<string, unknown>;
86
89
 
87
- export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, LoadConfigOptions, RollupConfig };
90
+ export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RollupConfig };