apibara 2.1.0-beta.4 → 2.1.0-beta.40

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 (119) hide show
  1. package/dist/chunks/add.mjs +16 -8
  2. package/dist/chunks/add.mjs.map +1 -0
  3. package/dist/chunks/build.mjs +4 -2
  4. package/dist/chunks/build.mjs.map +1 -0
  5. package/dist/chunks/dev.mjs +55 -21
  6. package/dist/chunks/dev.mjs.map +1 -0
  7. package/dist/chunks/init.mjs +11 -7
  8. package/dist/chunks/init.mjs.map +1 -0
  9. package/dist/chunks/prepare.mjs +4 -2
  10. package/dist/chunks/prepare.mjs.map +1 -0
  11. package/dist/chunks/start.mjs +16 -4
  12. package/dist/chunks/start.mjs.map +1 -0
  13. package/dist/chunks/write-project-info.mjs +51 -0
  14. package/dist/chunks/write-project-info.mjs.map +1 -0
  15. package/dist/cli/index.mjs +3 -1
  16. package/dist/cli/index.mjs.map +1 -0
  17. package/dist/common/index.d.mts +33 -0
  18. package/dist/common/index.d.ts +33 -0
  19. package/dist/common/index.mjs +91 -0
  20. package/dist/common/index.mjs.map +1 -0
  21. package/dist/config/index.mjs +1 -0
  22. package/dist/config/index.mjs.map +1 -0
  23. package/dist/core/index.mjs +134 -69
  24. package/dist/core/index.mjs.map +1 -0
  25. package/dist/create/index.d.mts +2 -1
  26. package/dist/create/index.d.ts +2 -1
  27. package/dist/create/index.mjs +168 -139
  28. package/dist/create/index.mjs.map +1 -0
  29. package/dist/hooks/index.mjs +6 -1
  30. package/dist/hooks/index.mjs.map +1 -0
  31. package/dist/indexer/index.d.ts +1 -0
  32. package/dist/indexer/index.mjs +1 -0
  33. package/dist/indexer/plugins.d.ts +1 -0
  34. package/dist/indexer/plugins.mjs +1 -0
  35. package/dist/indexer/testing.d.ts +1 -0
  36. package/dist/indexer/testing.mjs +1 -0
  37. package/dist/indexer/vcr.d.ts +1 -0
  38. package/dist/indexer/vcr.mjs +1 -0
  39. package/dist/rolldown/index.d.mts +7 -0
  40. package/dist/rolldown/index.d.ts +7 -0
  41. package/dist/rolldown/index.mjs +140 -0
  42. package/dist/rolldown/index.mjs.map +1 -0
  43. package/dist/runtime/dev.mjs +19 -9
  44. package/dist/runtime/internal/app.d.ts +14 -1
  45. package/dist/runtime/internal/app.mjs +26 -21
  46. package/dist/runtime/project-info.d.ts +3 -0
  47. package/dist/runtime/project-info.mjs +67 -0
  48. package/dist/runtime/start.mjs +59 -7
  49. package/dist/shared/apibara.730bb1e4.mjs +18 -0
  50. package/dist/shared/apibara.730bb1e4.mjs.map +1 -0
  51. package/dist/types/index.d.mts +24 -20
  52. package/dist/types/index.d.ts +24 -20
  53. package/dist/types/index.mjs +1 -0
  54. package/dist/types/index.mjs.map +1 -0
  55. package/package.json +33 -16
  56. package/src/cli/commands/add.ts +16 -7
  57. package/src/cli/commands/build.ts +5 -2
  58. package/src/cli/commands/dev.ts +64 -20
  59. package/src/cli/commands/init.ts +12 -7
  60. package/src/cli/commands/prepare.ts +4 -3
  61. package/src/cli/commands/start.ts +18 -3
  62. package/src/cli/commands/write-project-info.ts +56 -0
  63. package/src/cli/index.ts +2 -0
  64. package/src/common/cli.ts +40 -0
  65. package/src/common/constants.ts +6 -0
  66. package/src/common/helper.ts +86 -0
  67. package/src/common/index.ts +3 -0
  68. package/src/core/apibara.ts +7 -2
  69. package/src/core/build/build.ts +13 -5
  70. package/src/core/build/dev.ts +46 -23
  71. package/src/core/build/error.ts +9 -14
  72. package/src/core/build/prepare.ts +5 -3
  73. package/src/core/build/prod.ts +25 -16
  74. package/src/core/build/types.ts +11 -1
  75. package/src/core/config/defaults.ts +3 -0
  76. package/src/core/config/loader.ts +15 -7
  77. package/src/core/config/resolvers/runtime.resolver.ts +44 -0
  78. package/src/core/config/update.ts +6 -2
  79. package/src/core/scan.ts +1 -1
  80. package/src/create/add.ts +14 -20
  81. package/src/create/constants.ts +5 -7
  82. package/src/create/init.ts +8 -5
  83. package/src/create/templates.ts +144 -116
  84. package/src/create/types.ts +3 -0
  85. package/src/create/utils.ts +20 -7
  86. package/src/hooks/useRuntimeConfig.ts +4 -1
  87. package/src/indexer/index.ts +1 -0
  88. package/src/indexer/plugins.ts +1 -0
  89. package/src/indexer/testing.ts +1 -0
  90. package/src/indexer/vcr.ts +1 -0
  91. package/src/rolldown/config.ts +85 -0
  92. package/src/rolldown/index.ts +2 -0
  93. package/src/{rollup → rolldown}/plugins/indexers.ts +3 -3
  94. package/src/rolldown/plugins/instrumentation.ts +68 -0
  95. package/src/rolldown/plugins/static-config.ts +21 -0
  96. package/src/runtime/dev.ts +21 -8
  97. package/src/runtime/internal/app.ts +42 -29
  98. package/src/runtime/project-info.ts +90 -0
  99. package/src/runtime/start.ts +68 -6
  100. package/src/types/config.ts +27 -13
  101. package/src/types/hooks.ts +8 -5
  102. package/src/types/index.ts +1 -1
  103. package/src/types/rolldown.ts +5 -0
  104. package/src/types/virtual/indexers.d.ts +4 -1
  105. package/src/types/virtual/instrumentation.d.ts +4 -0
  106. package/src/types/virtual/static-config.d.ts +4 -0
  107. package/dist/rollup/index.d.mts +0 -6
  108. package/dist/rollup/index.d.ts +0 -6
  109. package/dist/rollup/index.mjs +0 -150
  110. package/dist/shared/apibara.1b515d04.mjs +0 -8
  111. package/src/cli/common.ts +0 -8
  112. package/src/core/config/resolvers/preset.resolver.ts +0 -9
  113. package/src/core/config/resolvers/runtime-config.resolver.ts +0 -6
  114. package/src/rollup/config.ts +0 -87
  115. package/src/rollup/index.ts +0 -2
  116. package/src/rollup/plugins/config.ts +0 -12
  117. package/src/rollup/plugins/esm-shim.ts +0 -69
  118. package/src/types/rollup.ts +0 -8
  119. package/src/types/virtual/config.d.ts +0 -3
@@ -4,7 +4,7 @@ import * as prettier from "prettier";
4
4
  import prompts from "prompts";
5
5
  import { blue, cyan, red, yellow } from "./colors";
6
6
  import { dnaUrls, networks } from "./constants";
7
- import type { Chain, Language, Network, PkgInfo } from "./types";
7
+ import type { Chain, FileExtension, Language, Network, PkgInfo } from "./types";
8
8
 
9
9
  export function isEmpty(path: string) {
10
10
  const files = fs.readdirSync(path);
@@ -31,7 +31,8 @@ export function validateLanguage(language?: string, throwError = false) {
31
31
  language === "typescript" ||
32
32
  language === "ts" ||
33
33
  language === "javascript" ||
34
- language === "js"
34
+ language === "js" ||
35
+ language === "mjs"
35
36
  ) {
36
37
  return true;
37
38
  }
@@ -49,7 +50,7 @@ export function getLanguageFromAlias(alias: string): Language {
49
50
  if (alias === "ts" || alias === "typescript") {
50
51
  return "typescript";
51
52
  }
52
- if (alias === "js" || alias === "javascript") {
53
+ if (alias === "js" || alias === "javascript" || alias === "mjs") {
53
54
  return "javascript";
54
55
  }
55
56
 
@@ -184,19 +185,31 @@ export function validateDnaUrl(dnaUrl?: string, throwError = false) {
184
185
  export function hasApibaraConfig(cwd: string): boolean {
185
186
  const configPathJS = path.join(cwd, "apibara.config.js");
186
187
  const configPathTS = path.join(cwd, "apibara.config.ts");
188
+ const configPathMJS = path.join(cwd, "apibara.config.mjs");
187
189
 
188
- return fs.existsSync(configPathJS) || fs.existsSync(configPathTS);
190
+ return (
191
+ fs.existsSync(configPathJS) ||
192
+ fs.existsSync(configPathTS) ||
193
+ fs.existsSync(configPathMJS)
194
+ );
189
195
  }
190
196
 
191
- export function getApibaraConfigLanguage(cwd: string): Language {
197
+ export function getApibaraConfigLanguage(cwd: string): {
198
+ language: Language;
199
+ extension: FileExtension;
200
+ } {
192
201
  const configPathJS = path.join(cwd, "apibara.config.js");
193
202
  const configPathTS = path.join(cwd, "apibara.config.ts");
203
+ const configPathMJS = path.join(cwd, "apibara.config.mjs");
194
204
 
205
+ if (fs.existsSync(configPathMJS)) {
206
+ return { language: "javascript", extension: "mjs" };
207
+ }
195
208
  if (fs.existsSync(configPathJS)) {
196
- return "javascript";
209
+ return { language: "javascript", extension: "js" };
197
210
  }
198
211
  if (fs.existsSync(configPathTS)) {
199
- return "typescript";
212
+ return { language: "typescript", extension: "ts" };
200
213
  }
201
214
 
202
215
  throw new Error(red("✖") + " No apibara.config found");
@@ -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(process.env.APIBARA_RUNTIME_CONFIG || "{}");
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";
@@ -0,0 +1,85 @@
1
+ import { existsSync } from "node:fs";
2
+ import { builtinModules } from "node:module";
3
+ import type { Apibara } from "apibara/types";
4
+ import defu from "defu";
5
+ import { join } from "pathe";
6
+ import type {
7
+ ConfigExport,
8
+ RolldownOptions,
9
+ RolldownPluginOption,
10
+ } from "rolldown";
11
+ import { indexers } from "./plugins/indexers";
12
+ import { instrumentation } from "./plugins/instrumentation";
13
+ import { staticConfig } from "./plugins/static-config";
14
+
15
+ const runtimeDependencies = [
16
+ "better-sqlite3",
17
+ "@electric-sql/pglite",
18
+ "pg",
19
+ // https://socket.io/docs/v4/server-installation/#additional-packages
20
+ "utf-8-validate",
21
+ "bufferutil",
22
+ // was giving unresolved import warnings from `node-fetch` library.
23
+ "encoding",
24
+ ];
25
+
26
+ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
27
+ const extensions: string[] = [
28
+ ".ts",
29
+ ".mjs",
30
+ ".js",
31
+ ".json",
32
+ ".node",
33
+ ".tsx",
34
+ ".jsx",
35
+ ];
36
+
37
+ const tsConfigExists = existsSync(
38
+ join(apibara.options.rootDir, "tsconfig.json"),
39
+ );
40
+
41
+ const rolldownConfig: RolldownOptions & {
42
+ plugins: RolldownPluginOption[];
43
+ } = defu(
44
+ // biome-ignore lint/suspicious/noExplicitAny: apibara.options.rolldownConfig is typed
45
+ apibara.options.rolldownConfig as any,
46
+ <ConfigExport>{
47
+ platform: "node",
48
+ input: apibara.options.entry,
49
+ output: {
50
+ dir: join(apibara.options.outputDir || "./.apibara/build"),
51
+ format: "esm",
52
+ entryFileNames: "[name].mjs",
53
+ chunkFileNames: "chunks/[name]-[hash].mjs",
54
+ sourcemap: true,
55
+ },
56
+ plugins: [],
57
+ onwarn(warning, rolldownWarn) {
58
+ if (
59
+ !["CIRCULAR_DEPENDENCY", "EVAL", "THIS_IS_UNDEFINED"].includes(
60
+ warning.code || "",
61
+ ) &&
62
+ !warning.message.includes("Unsupported source map comment") &&
63
+ !warning.message.includes("@__PURE__") &&
64
+ !warning.message.includes("/*#__PURE__*/")
65
+ ) {
66
+ rolldownWarn(warning);
67
+ }
68
+ },
69
+ resolve: {
70
+ extensions,
71
+ preferBuiltins: !!apibara.options.node,
72
+ mainFields: ["main"],
73
+ exportConditions: apibara.options.exportConditions,
74
+ tsconfigFilename: tsConfigExists ? "tsconfig.json" : undefined,
75
+ },
76
+ treeshake: true,
77
+ external: [...builtinModules, ...runtimeDependencies],
78
+ },
79
+ );
80
+
81
+ rolldownConfig.plugins?.push(staticConfig(apibara));
82
+ rolldownConfig.plugins?.push(instrumentation(apibara));
83
+ rolldownConfig.plugins?.push(indexers(apibara));
84
+ return rolldownConfig;
85
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./config";
2
+ export type { Plugin } from "rolldown";
@@ -1,17 +1,17 @@
1
1
  import virtual from "@rollup/plugin-virtual";
2
2
  import type { Apibara } from "apibara/types";
3
3
  import { hash } from "ohash";
4
+ import type { RolldownPluginOption } from "rolldown";
4
5
 
5
6
  export function indexers(apibara: Apibara) {
6
7
  const indexers = [...new Set(apibara.indexers)];
7
-
8
8
  return virtual({
9
9
  "#apibara-internal-virtual/indexers": `
10
- ${indexers.map((i) => `import _${hash(i)} from '${i.indexer}';`).join("\n")}
10
+ ${indexers.map((i) => `import * as _${hash(i)} from '${i.indexer}';`).join("\n")}
11
11
 
12
12
  export const indexers = [
13
13
  ${indexers.map((i) => `{ name: "${i.name}", indexer: _${hash(i)} }`).join(",\n")}
14
14
  ];
15
15
  `,
16
- });
16
+ }) as RolldownPluginOption;
17
17
  }
@@ -0,0 +1,68 @@
1
+ import { existsSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import virtual from "@rollup/plugin-virtual";
4
+ import type { Apibara } from "apibara/types";
5
+ import type { RolldownPluginOption } from "rolldown";
6
+
7
+ export function instrumentation(apibara: Apibara) {
8
+ const instrumentationPath = join(
9
+ apibara.options._c12.cwd!,
10
+ `instrumentation.${apibara.options._c12.configFile?.endsWith(".ts") ? "ts" : apibara.options._c12.configFile?.endsWith(".mjs") ? "mjs" : "js"}`,
11
+ );
12
+
13
+ if (!existsSync(instrumentationPath)) {
14
+ return virtual({
15
+ "#apibara-internal-virtual/instrumentation": `
16
+ let register = undefined;
17
+ let logger = undefined;
18
+
19
+ export { register, logger };
20
+ `,
21
+ }) as RolldownPluginOption;
22
+ }
23
+
24
+ /**
25
+ * We are importing the instrumentation file inline with "require" instead of "import" at the top of the file to avoid warnings from rolldown
26
+ * when some methods are not defined in the instrumentation file.
27
+ *
28
+ * Example warning:
29
+ *
30
+ * [IMPORT_IS_UNDEFINED] Warning: Import `logger` will always be undefined because there is no matching export in 'instrumentation.ts'
31
+ * ╭─[virtual:#apibara-internal-virtual/instrumentation:11:35]
32
+ * │
33
+ * 11 │ if (instrumentation && typeof instrumentation.logger === "function") {
34
+ * │ ───────────┬──────────
35
+ * │ ╰────────────
36
+ * ────╯
37
+
38
+ * [IMPORT_IS_UNDEFINED] Warning: Import `logger` will always be undefined because there is no matching export in 'instrumentation.ts'
39
+ * ╭─[virtual:#apibara-internal-virtual/instrumentation:12:16]
40
+ * │
41
+ * 12 │ logger = instrumentation.logger;
42
+ * │ ───────────┬──────────
43
+ * │ ╰────────────
44
+ * ────╯
45
+ */
46
+ return virtual({
47
+ "#apibara-internal-virtual/instrumentation": `
48
+ let register = undefined;
49
+ let logger = undefined;
50
+
51
+ try {
52
+ const instrumentation = require('${instrumentationPath}');
53
+
54
+ if (instrumentation?.register && typeof instrumentation.register === "function") {
55
+ register = instrumentation.register;
56
+ }
57
+
58
+ if (instrumentation?.logger && typeof instrumentation.logger === "function") {
59
+ logger = instrumentation.logger;
60
+ }
61
+ } catch {
62
+ // Silently handle any require errors
63
+ }
64
+
65
+ export { register, logger };
66
+ `,
67
+ }) as RolldownPluginOption;
68
+ }
@@ -0,0 +1,21 @@
1
+ import virtual from "@rollup/plugin-virtual";
2
+ import { USER_ENV_APIBARA_RUNTIME_CONFIG } from "apibara/common";
3
+ import type { Apibara } from "apibara/types";
4
+ import type { RolldownPluginOption } from "rolldown";
5
+
6
+ export function staticConfig(apibara: Apibara) {
7
+ const presetString = apibara.options.preset ?? "";
8
+ const presetsStringified = JSON.stringify(apibara.options.presets ?? {});
9
+ const runtimeConfigStringified = JSON.stringify(
10
+ apibara.options.runtimeConfig ?? {},
11
+ );
12
+
13
+ return virtual({
14
+ "#apibara-internal-virtual/static-config": `
15
+ export const preset = ${presetString ? `"${presetString}"` : "undefined"};
16
+ export const presets = ${presetsStringified};
17
+ export const runtimeConfig = ${runtimeConfigStringified};
18
+ export const userEnvRuntimeConfig = JSON.parse(process.env.${USER_ENV_APIBARA_RUNTIME_CONFIG} ?? "{}");
19
+ `,
20
+ }) as RolldownPluginOption;
21
+ }
@@ -1,6 +1,8 @@
1
1
  import { runWithReconnect } from "@apibara/indexer";
2
- import { createClient } from "@apibara/protocol";
2
+ import { createAuthenticatedClient } from "@apibara/protocol";
3
+ import { getRuntimeDataFromEnv } from "apibara/common";
3
4
  import { defineCommand, runMain } from "citty";
5
+ import { blueBright } from "picocolors";
4
6
  import { availableIndexers, createIndexer } from "./internal/app";
5
7
 
6
8
  const startCommand = defineCommand({
@@ -13,13 +15,11 @@ const startCommand = defineCommand({
13
15
  type: "string",
14
16
  description: "Which indexers to run",
15
17
  },
16
- preset: {
17
- type: "string",
18
- description: "Preset to use",
19
- },
20
18
  },
21
19
  async run({ args }) {
22
- const { indexers: indexersArgs, preset } = args;
20
+ const { indexers: indexersArgs } = args;
21
+
22
+ const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
23
23
 
24
24
  let selectedIndexers = availableIndexers;
25
25
  if (indexersArgs) {
@@ -36,13 +36,26 @@ const startCommand = defineCommand({
36
36
 
37
37
  await Promise.all(
38
38
  selectedIndexers.map(async (indexer) => {
39
- const indexerInstance = createIndexer(indexer, preset);
39
+ const { indexer: indexerInstance, logger } =
40
+ createIndexer({
41
+ indexerName: indexer,
42
+ processedRuntimeConfig,
43
+ preset,
44
+ }) ?? {};
45
+ if (!indexerInstance) {
46
+ return;
47
+ }
40
48
 
41
- const client = createClient(
49
+ const client = createAuthenticatedClient(
42
50
  indexerInstance.streamConfig,
43
51
  indexerInstance.options.streamUrl,
52
+ indexerInstance.options.clientOptions,
44
53
  );
45
54
 
55
+ if (logger) {
56
+ logger.info(`Indexer ${blueBright(indexer)} started`);
57
+ }
58
+
46
59
  await runWithReconnect(client, indexerInstance);
47
60
  }),
48
61
  );
@@ -8,32 +8,28 @@ import {
8
8
  inMemoryPersistence,
9
9
  logger,
10
10
  } from "@apibara/indexer/plugins";
11
- import { config } from "#apibara-internal-virtual/config";
11
+ import consola from "consola";
12
12
  import { indexers } from "#apibara-internal-virtual/indexers";
13
+ import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
13
14
  import { createLogger } from "./logger";
14
15
 
15
16
  export const availableIndexers = indexers.map((i) => i.name);
16
17
 
17
- export function createIndexer(indexerName: string, preset?: string) {
18
- let runtimeConfig: Record<string, unknown> = { ...config.runtimeConfig };
19
-
20
- if (preset) {
21
- if (config.presets === undefined) {
22
- throw new Error(
23
- `Specified preset "${preset}" but no presets were defined`,
24
- );
25
- }
26
-
27
- if (config.presets[preset] === undefined) {
28
- throw new Error(`Specified preset "${preset}" but it was not defined`);
29
- }
30
-
31
- const presetValue = config.presets[preset] as {
32
- runtimeConfig: Record<string, unknown>;
33
- };
34
- runtimeConfig = { ...runtimeConfig, ...presetValue.runtimeConfig };
35
- }
36
-
18
+ export function createIndexer({
19
+ indexerName,
20
+ processedRuntimeConfig,
21
+ preset,
22
+ }: {
23
+ indexerName: string;
24
+ /**
25
+ * Final processed runtime config to be used by the indexer
26
+ */
27
+ processedRuntimeConfig: Record<string, unknown>;
28
+ /**
29
+ * Preset name which was used to generate the runtime config
30
+ */
31
+ preset?: string;
32
+ }) {
37
33
  const indexerDefinition = indexers.find((i) => i.name === indexerName);
38
34
 
39
35
  if (indexerDefinition === undefined) {
@@ -42,10 +38,18 @@ export function createIndexer(indexerName: string, preset?: string) {
42
38
  );
43
39
  }
44
40
 
41
+ const indexerModule = indexerDefinition.indexer?.default;
42
+ if (indexerModule === undefined) {
43
+ consola.warn(
44
+ `Specified indexer "${indexerName}" but it does not export a default. Ignoring.`,
45
+ );
46
+ return;
47
+ }
48
+
45
49
  const definition =
46
- typeof indexerDefinition.indexer === "function"
47
- ? indexerDefinition.indexer(runtimeConfig)
48
- : indexerDefinition.indexer;
50
+ typeof indexerModule === "function"
51
+ ? indexerModule(processedRuntimeConfig)
52
+ : indexerModule;
49
53
 
50
54
  let reporter: ConsolaReporter = createLogger({
51
55
  indexer: indexerName,
@@ -53,26 +57,35 @@ export function createIndexer(indexerName: string, preset?: string) {
53
57
  indexers: availableIndexers,
54
58
  });
55
59
 
56
- if (config.logger) {
57
- reporter = config.logger({
60
+ // Check if a custom logger is provided through instrumentation
61
+ if (instrumentationLogger) {
62
+ // Create a reporter using the custom logger function
63
+ const _reporter = instrumentationLogger({
58
64
  indexer: indexerName,
59
65
  preset,
60
66
  indexers: availableIndexers,
61
67
  });
68
+
69
+ // If the reporter is valid (has a log method), use it instead of the default
70
+ if (_reporter && "log" in _reporter) {
71
+ reporter = _reporter;
72
+ }
62
73
  }
63
74
 
64
75
  // Put the in-memory persistence plugin first so that it can be overridden by any user-defined
65
76
  // persistence plugin.
66
- // Put the logger last since we want to override any user-defined logger.
67
77
  definition.plugins = [
68
78
  internalContext({
69
79
  indexerName,
70
80
  availableIndexers,
71
81
  } as InternalContext),
82
+ logger({ logger: reporter }),
72
83
  inMemoryPersistence(),
73
84
  ...(definition.plugins ?? []),
74
- logger({ logger: reporter }),
75
85
  ];
76
86
 
77
- return _createIndexer(definition);
87
+ return {
88
+ indexer: _createIndexer(definition),
89
+ logger: consola.create({ reporters: [reporter] }),
90
+ };
78
91
  }
@@ -0,0 +1,90 @@
1
+ import { writeFileSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import {
4
+ getProcessedRuntimeConfig,
5
+ getRuntimeDataFromEnv,
6
+ } from "apibara/common";
7
+ import { defineCommand, runMain } from "citty";
8
+ import { availableIndexers, createIndexer } from "./internal/app";
9
+
10
+ type ProjectInfo = {
11
+ indexers: {
12
+ [indexerName: string]: {
13
+ [presetName: string]: {
14
+ type: string;
15
+ isFactory: boolean;
16
+ };
17
+ };
18
+ };
19
+ };
20
+
21
+ const startCommand = defineCommand({
22
+ meta: {
23
+ name: "write-project-info",
24
+ description: "Write json-encoded information about the project.",
25
+ },
26
+ args: {
27
+ "build-dir": {
28
+ type: "string",
29
+ description: "project build directory",
30
+ },
31
+ },
32
+ async run({ args }) {
33
+ const projectInfo: ProjectInfo = {
34
+ indexers: {},
35
+ };
36
+
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
+ });
49
+ for (const indexer of availableIndexers) {
50
+ const { indexer: indexerInstance } =
51
+ createIndexer({
52
+ indexerName: indexer,
53
+ processedRuntimeConfig,
54
+ preset,
55
+ }) ?? {};
56
+ if (!indexerInstance) {
57
+ continue;
58
+ }
59
+ projectInfo.indexers[indexer] = {
60
+ ...(projectInfo.indexers[indexer] ?? {}),
61
+ [preset]: {
62
+ type: indexerInstance.streamConfig.name,
63
+ isFactory: indexerInstance.options.factory !== undefined,
64
+ },
65
+ };
66
+ }
67
+ }
68
+
69
+ const projectInfoPath = resolve(
70
+ args["build-dir"] ?? ".apibara",
71
+ "project-info.json",
72
+ );
73
+
74
+ writeFileSync(projectInfoPath, JSON.stringify(projectInfo, null, 2));
75
+ },
76
+ });
77
+
78
+ export const mainCli = defineCommand({
79
+ meta: {
80
+ name: "write-project-info-runner",
81
+ description: "Write json-encoded information about the project.",
82
+ },
83
+ subCommands: {
84
+ start: () => startCommand,
85
+ },
86
+ });
87
+
88
+ runMain(mainCli);
89
+
90
+ export default {};
@@ -1,6 +1,21 @@
1
1
  import { runWithReconnect } from "@apibara/indexer";
2
- import { createClient } from "@apibara/protocol";
2
+ import { createAuthenticatedClient } from "@apibara/protocol";
3
+ import {
4
+ checkForUnknownArgs,
5
+ getProcessedRuntimeConfig,
6
+ getRuntimeDataFromEnv,
7
+ } from "apibara/common";
3
8
  import { defineCommand, runMain } from "citty";
9
+ import consola from "consola";
10
+ import { blueBright } from "picocolors";
11
+ import { register } from "#apibara-internal-virtual/instrumentation";
12
+ // used when running with node .apibara/build/start.mjs as these values are made static during build time (except userEnvRuntimeConfig)
13
+ import {
14
+ preset as originalPreset,
15
+ presets,
16
+ runtimeConfig,
17
+ userEnvRuntimeConfig,
18
+ } from "#apibara-internal-virtual/static-config";
4
19
  import { createIndexer } from "./internal/app";
5
20
 
6
21
  const startCommand = defineCommand({
@@ -16,19 +31,66 @@ const startCommand = defineCommand({
16
31
  },
17
32
  preset: {
18
33
  type: "string",
19
- description: "Preset to use",
34
+ description: "Preset name",
35
+ },
36
+ standalone: {
37
+ type: "boolean",
38
+ default: true,
39
+ description:
40
+ "--standalone: can run the indexer just with node and without apibara cli \n\t\t\t --no-standalone: must run the indexer with apibara cli",
20
41
  },
21
42
  },
22
- async run({ args }) {
23
- const { indexer, preset } = args;
43
+ async run({ args, cmd }) {
44
+ const { indexer, preset: argPreset, standalone } = args;
45
+ await checkForUnknownArgs(args, cmd);
46
+
47
+ let preset: string | undefined;
48
+ let processedRuntimeConfig: Record<string, unknown> | undefined;
49
+
50
+ if (standalone) {
51
+ // when user does node .apibara/build/start.mjs
52
+ preset = argPreset ?? originalPreset;
53
+ processedRuntimeConfig = getProcessedRuntimeConfig({
54
+ preset,
55
+ presets,
56
+ runtimeConfig,
57
+ userEnvRuntimeConfig,
58
+ });
59
+ } else {
60
+ // When user does apibara start
61
+ const envResult = getRuntimeDataFromEnv();
62
+ preset = envResult.preset;
63
+ processedRuntimeConfig = envResult.processedRuntimeConfig;
64
+ }
24
65
 
25
- const indexerInstance = createIndexer(indexer, preset);
66
+ const { indexer: indexerInstance, logger } =
67
+ createIndexer({
68
+ indexerName: indexer,
69
+ processedRuntimeConfig,
70
+ preset,
71
+ }) ?? {};
26
72
 
27
- const client = createClient(
73
+ if (!indexerInstance) {
74
+ consola.error(`Specified indexer "${indexer}" but it was not defined`);
75
+ process.exit(1);
76
+ }
77
+
78
+ const client = createAuthenticatedClient(
28
79
  indexerInstance.streamConfig,
29
80
  indexerInstance.options.streamUrl,
81
+ indexerInstance.options.clientOptions,
30
82
  );
31
83
 
84
+ if (register) {
85
+ consola.start("Registering from instrumentation");
86
+ await register();
87
+ consola.success("Registered from instrumentation");
88
+ }
89
+
90
+ if (logger) {
91
+ logger.info(`Indexer ${blueBright(indexer)} started`);
92
+ }
93
+
32
94
  await runWithReconnect(client, indexerInstance);
33
95
  },
34
96
  });