apibara 2.1.0-beta.12 → 2.1.0-beta.14

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.
@@ -2,10 +2,11 @@ import { existsSync } from 'node:fs';
2
2
  import { builtinModules } from 'node:module';
3
3
  import replace from '@rollup/plugin-replace';
4
4
  import defu from 'defu';
5
- import { join } from 'pathe';
5
+ import { join as join$1 } from 'pathe';
6
6
  import { s as serialize } from '../shared/apibara.af330c7d.mjs';
7
7
  import virtual from '@rollup/plugin-virtual';
8
8
  import { hash } from 'ohash';
9
+ import { join } from 'node:path';
9
10
 
10
11
  function appConfig(apibara) {
11
12
  return virtual({
@@ -42,6 +43,45 @@ function indexers(apibara) {
42
43
  });
43
44
  }
44
45
 
46
+ function instrumentation(apibara) {
47
+ const instrumentationPath = join(
48
+ apibara.options._c12.cwd,
49
+ `instrumentation.${apibara.options._c12.configFile?.endsWith(".ts") ? "ts" : "js"}`
50
+ );
51
+ if (!existsSync(instrumentationPath)) {
52
+ return virtual({
53
+ "#apibara-internal-virtual/instrumentation": `
54
+ let register = undefined;
55
+ let logger = undefined;
56
+
57
+ export { register, logger };
58
+ `
59
+ });
60
+ }
61
+ return virtual({
62
+ "#apibara-internal-virtual/instrumentation": `
63
+ let register = undefined;
64
+ let logger = undefined;
65
+
66
+ try {
67
+ const instrumentation = require('${instrumentationPath}');
68
+
69
+ if (instrumentation?.register && typeof instrumentation.register === "function") {
70
+ register = instrumentation.register;
71
+ }
72
+
73
+ if (instrumentation?.logger && typeof instrumentation.logger === "function") {
74
+ logger = instrumentation.logger;
75
+ }
76
+ } catch {
77
+ // Silently handle any require errors
78
+ }
79
+
80
+ export { register, logger };
81
+ `
82
+ });
83
+ }
84
+
45
85
  const runtimeDependencies = [
46
86
  "better-sqlite3",
47
87
  "@electric-sql/pglite",
@@ -62,7 +102,7 @@ function getRolldownConfig(apibara) {
62
102
  ".jsx"
63
103
  ];
64
104
  const tsConfigExists = existsSync(
65
- join(apibara.options.rootDir, "tsconfig.json")
105
+ join$1(apibara.options.rootDir, "tsconfig.json")
66
106
  );
67
107
  const rolldownConfig = defu(
68
108
  // biome-ignore lint/suspicious/noExplicitAny: apibara.options.rolldownConfig is typed
@@ -71,7 +111,7 @@ function getRolldownConfig(apibara) {
71
111
  platform: "node",
72
112
  input: apibara.options.entry,
73
113
  output: {
74
- dir: join(apibara.options.outputDir || "./.apibara/build"),
114
+ dir: join$1(apibara.options.outputDir || "./.apibara/build"),
75
115
  format: "esm",
76
116
  entryFileNames: "[name].mjs",
77
117
  chunkFileNames: "chunks/[name]-[hash].mjs",
@@ -104,6 +144,7 @@ function getRolldownConfig(apibara) {
104
144
  }
105
145
  })
106
146
  );
147
+ rolldownConfig.plugins?.push(instrumentation(apibara));
107
148
  rolldownConfig.plugins?.push(indexers(apibara));
108
149
  rolldownConfig.plugins?.push(appConfig());
109
150
  return rolldownConfig;
@@ -9,6 +9,7 @@ import {
9
9
  import consola from "consola";
10
10
  import { config } from "#apibara-internal-virtual/config";
11
11
  import { indexers } from "#apibara-internal-virtual/indexers";
12
+ import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
12
13
  import { createLogger } from "./logger.mjs";
13
14
  export const availableIndexers = indexers.map((i) => i.name);
14
15
  export function createIndexer(indexerName, preset) {
@@ -39,11 +40,21 @@ export function createIndexer(indexerName, preset) {
39
40
  return;
40
41
  }
41
42
  const definition = typeof indexerModule === "function" ? indexerModule(runtimeConfig) : indexerModule;
42
- const reporter = createLogger({
43
+ let reporter = createLogger({
43
44
  indexer: indexerName,
44
45
  preset,
45
46
  indexers: availableIndexers
46
47
  });
48
+ if (instrumentationLogger) {
49
+ const _reporter = instrumentationLogger({
50
+ indexer: indexerName,
51
+ preset,
52
+ indexers: availableIndexers
53
+ });
54
+ if (_reporter && "log" in _reporter) {
55
+ reporter = _reporter;
56
+ }
57
+ }
47
58
  definition.plugins = [
48
59
  internalContext({
49
60
  indexerName,
@@ -2,6 +2,7 @@ import { runWithReconnect } from "@apibara/indexer";
2
2
  import { createClient } from "@apibara/protocol";
3
3
  import { defineCommand, runMain } from "citty";
4
4
  import consola from "consola";
5
+ import { register } from "#apibara-internal-virtual/instrumentation";
5
6
  import { createIndexer } from "./internal/app.mjs";
6
7
  const startCommand = defineCommand({
7
8
  meta: {
@@ -30,6 +31,11 @@ const startCommand = defineCommand({
30
31
  indexerInstance.streamConfig,
31
32
  indexerInstance.options.streamUrl
32
33
  );
34
+ if (register) {
35
+ consola.start("Registering from instrumentation");
36
+ await register();
37
+ consola.success("Registered from instrumentation");
38
+ }
33
39
  await runWithReconnect(client, indexerInstance);
34
40
  }
35
41
  });
@@ -18,11 +18,13 @@ interface ApibaraHooks {
18
18
  close: () => void;
19
19
  }
20
20
 
21
- type LoggerFactory = ({ indexer, preset, }: {
21
+ type RegisterFn = () => Promise<void>;
22
+ type LoggerFactoryFn = ({ indexer, indexers, preset, }: LoggerFactoryArgs) => ConsolaReporter;
23
+ type LoggerFactoryArgs = {
22
24
  indexer: string;
23
25
  indexers: string[];
24
26
  preset?: string;
25
- }) => ConsolaReporter;
27
+ };
26
28
  /**
27
29
  * Apibara Config type (apibara.config)
28
30
  */
@@ -30,7 +32,6 @@ interface ApibaraConfig<T extends Record<string, DeepPartial<Pick<ApibaraConfig<
30
32
  runtimeConfig?: R;
31
33
  presets?: T;
32
34
  preset?: keyof T;
33
- logger?: LoggerFactory;
34
35
  }
35
36
  type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
36
37
  /**
@@ -54,7 +55,6 @@ interface ApibaraOptions<T extends Record<string, DeepPartial<Pick<ApibaraConfig
54
55
  dev: boolean;
55
56
  watchOptions: WatchOptions["watch"];
56
57
  hooks: NestedHooks<ApibaraHooks>;
57
- logger?: LoggerFactory;
58
58
  rolldownConfig?: Partial<RolldownOptions>;
59
59
  /**
60
60
  * @deprecated Use rolldownConfig instead. This option will be removed in future releases.
@@ -90,4 +90,4 @@ type RolldownConfig = InputOptions & {
90
90
 
91
91
  type ApibaraRuntimeConfig = Record<string, unknown>;
92
92
 
93
- export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RolldownConfig };
93
+ export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactoryArgs, LoggerFactoryFn, RegisterFn, RolldownConfig };
@@ -18,11 +18,13 @@ interface ApibaraHooks {
18
18
  close: () => void;
19
19
  }
20
20
 
21
- type LoggerFactory = ({ indexer, preset, }: {
21
+ type RegisterFn = () => Promise<void>;
22
+ type LoggerFactoryFn = ({ indexer, indexers, preset, }: LoggerFactoryArgs) => ConsolaReporter;
23
+ type LoggerFactoryArgs = {
22
24
  indexer: string;
23
25
  indexers: string[];
24
26
  preset?: string;
25
- }) => ConsolaReporter;
27
+ };
26
28
  /**
27
29
  * Apibara Config type (apibara.config)
28
30
  */
@@ -30,7 +32,6 @@ interface ApibaraConfig<T extends Record<string, DeepPartial<Pick<ApibaraConfig<
30
32
  runtimeConfig?: R;
31
33
  presets?: T;
32
34
  preset?: keyof T;
33
- logger?: LoggerFactory;
34
35
  }
35
36
  type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
36
37
  /**
@@ -54,7 +55,6 @@ interface ApibaraOptions<T extends Record<string, DeepPartial<Pick<ApibaraConfig
54
55
  dev: boolean;
55
56
  watchOptions: WatchOptions["watch"];
56
57
  hooks: NestedHooks<ApibaraHooks>;
57
- logger?: LoggerFactory;
58
58
  rolldownConfig?: Partial<RolldownOptions>;
59
59
  /**
60
60
  * @deprecated Use rolldownConfig instead. This option will be removed in future releases.
@@ -90,4 +90,4 @@ type RolldownConfig = InputOptions & {
90
90
 
91
91
  type ApibaraRuntimeConfig = Record<string, unknown>;
92
92
 
93
- export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactory, RolldownConfig };
93
+ export type { Apibara, ApibaraConfig, ApibaraDynamicConfig, ApibaraHooks, ApibaraOptions, ApibaraRuntimeConfig, DeepPartial, IndexerDefinition, LoadConfigOptions, LoggerFactoryArgs, LoggerFactoryFn, RegisterFn, RolldownConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apibara",
3
- "version": "2.1.0-beta.12",
3
+ "version": "2.1.0-beta.14",
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.12",
81
+ "@apibara/starknet": "2.1.0-beta.14",
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,8 @@
89
89
  "vitest": "^1.6.0"
90
90
  },
91
91
  "dependencies": {
92
- "@apibara/indexer": "2.1.0-beta.12",
93
- "@apibara/protocol": "2.1.0-beta.12",
92
+ "@apibara/indexer": "2.1.0-beta.14",
93
+ "@apibara/protocol": "2.1.0-beta.14",
94
94
  "@rollup/plugin-replace": "^6.0.2",
95
95
  "@rollup/plugin-virtual": "^3.0.2",
96
96
  "c12": "^1.11.1",
@@ -12,6 +12,7 @@ import type {
12
12
  import { serialize } from "../utils/helper";
13
13
  import { appConfig } from "./plugins/config";
14
14
  import { indexers } from "./plugins/indexers";
15
+ import { instrumentation } from "./plugins/instrumentation";
15
16
 
16
17
  const runtimeDependencies = [
17
18
  "better-sqlite3",
@@ -86,6 +87,8 @@ export function getRolldownConfig(apibara: Apibara): RolldownOptions {
86
87
  },
87
88
  }) as RolldownPluginOption,
88
89
  );
90
+
91
+ rolldownConfig.plugins?.push(instrumentation(apibara));
89
92
  rolldownConfig.plugins?.push(indexers(apibara));
90
93
  rolldownConfig.plugins?.push(appConfig(apibara));
91
94
 
@@ -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" : "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
+ }
@@ -11,6 +11,7 @@ import {
11
11
  import consola from "consola";
12
12
  import { config } from "#apibara-internal-virtual/config";
13
13
  import { indexers } from "#apibara-internal-virtual/indexers";
14
+ import { logger as instrumentationLogger } from "#apibara-internal-virtual/instrumentation";
14
15
  import { createLogger } from "./logger";
15
16
 
16
17
  export const availableIndexers = indexers.map((i) => i.name);
@@ -56,28 +57,26 @@ export function createIndexer(indexerName: string, preset?: string) {
56
57
  ? indexerModule(runtimeConfig)
57
58
  : indexerModule;
58
59
 
59
- const reporter: ConsolaReporter = createLogger({
60
+ let reporter: ConsolaReporter = createLogger({
60
61
  indexer: indexerName,
61
62
  preset,
62
63
  indexers: availableIndexers,
63
64
  });
64
65
 
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
- // }
66
+ // Check if a custom logger is provided through instrumentation
67
+ if (instrumentationLogger) {
68
+ // Create a reporter using the custom logger function
69
+ const _reporter = instrumentationLogger({
70
+ indexer: indexerName,
71
+ preset,
72
+ indexers: availableIndexers,
73
+ });
74
+
75
+ // If the reporter is valid (has a log method), use it instead of the default
76
+ if (_reporter && "log" in _reporter) {
77
+ reporter = _reporter;
78
+ }
79
+ }
81
80
 
82
81
  // Put the in-memory persistence plugin first so that it can be overridden by any user-defined
83
82
  // persistence plugin.
@@ -2,6 +2,7 @@ import { runWithReconnect } from "@apibara/indexer";
2
2
  import { createClient } from "@apibara/protocol";
3
3
  import { defineCommand, runMain } from "citty";
4
4
  import consola from "consola";
5
+ import { register } from "#apibara-internal-virtual/instrumentation";
5
6
  import { createIndexer } from "./internal/app";
6
7
 
7
8
  const startCommand = defineCommand({
@@ -34,6 +35,12 @@ const startCommand = defineCommand({
34
35
  indexerInstance.options.streamUrl,
35
36
  );
36
37
 
38
+ if (register) {
39
+ consola.start("Registering from instrumentation");
40
+ await register();
41
+ consola.success("Registered from instrumentation");
42
+ }
43
+
37
44
  await runWithReconnect(client, indexerInstance);
38
45
  },
39
46
  });
@@ -11,10 +11,19 @@ import type { RolldownOptions } from "rolldown";
11
11
  import type { DeepPartial } from "./_utils";
12
12
  import type { ApibaraHooks } from "./hooks";
13
13
 
14
- export type LoggerFactory = ({
14
+ export type RegisterFn = () => Promise<void>;
15
+
16
+ export type LoggerFactoryFn = ({
15
17
  indexer,
18
+ indexers,
16
19
  preset,
17
- }: { indexer: string; indexers: string[]; preset?: string }) => ConsolaReporter;
20
+ }: LoggerFactoryArgs) => ConsolaReporter;
21
+
22
+ export type LoggerFactoryArgs = {
23
+ indexer: string;
24
+ indexers: string[];
25
+ preset?: string;
26
+ };
18
27
 
19
28
  /**
20
29
  * Apibara Config type (apibara.config)
@@ -30,7 +39,6 @@ export interface ApibaraConfig<
30
39
  runtimeConfig?: R;
31
40
  presets?: T;
32
41
  preset?: keyof T;
33
- logger?: LoggerFactory;
34
42
  }
35
43
 
36
44
  export type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
@@ -75,9 +83,6 @@ export interface ApibaraOptions<
75
83
  // Hooks
76
84
  hooks: NestedHooks<ApibaraHooks>;
77
85
 
78
- // Logging
79
- logger?: LoggerFactory;
80
-
81
86
  // Rolldown
82
87
  rolldownConfig?: Partial<RolldownOptions>;
83
88
 
@@ -0,0 +1,4 @@
1
+ import type { LoggerFactoryFn, RegisterFn } from "apibara/types";
2
+
3
+ export const register: RegisterFn | undefined = undefined;
4
+ export const logger: LoggerFactoryFn | undefined = undefined;