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
@@ -0,0 +1,12 @@
1
+ import virtual from "@rollup/plugin-virtual";
2
+ import type { Apibara } from "apibara/types";
3
+
4
+ export function appConfig(apibara: Apibara) {
5
+ return virtual({
6
+ "#apibara-internal-virtual/config": `
7
+ import * as projectConfig from '${apibara.options._c12.configFile}';
8
+
9
+ export const config = projectConfig.default;
10
+ `,
11
+ });
12
+ }
@@ -0,0 +1,69 @@
1
+ import MagicString from "magic-string";
2
+ import type { Plugin } from "rollup";
3
+
4
+ /**
5
+ * An alternative to @rollup/plugin-esm-shim
6
+ */
7
+ export function esmShim(): Plugin {
8
+ const ESMShim = `
9
+ // -- Shims --
10
+ import cjsUrl from 'node:url';
11
+ import cjsPath from 'node:path';
12
+ const __filename = cjsUrl.fileURLToPath(import.meta.url);
13
+ const __dirname = cjsPath.dirname(__filename);
14
+ // -- End Shims --
15
+ `;
16
+
17
+ const CJSyntaxRegex = /__filename|__dirname/;
18
+
19
+ return {
20
+ name: "esm-shim",
21
+
22
+ renderChunk(code, _chunk, opts) {
23
+ if (opts.format === "es") {
24
+ if (code.includes(ESMShim) || !CJSyntaxRegex.test(code)) {
25
+ return null;
26
+ }
27
+
28
+ let endIndexOfLastImport = -1;
29
+
30
+ // Find the last import statement and its ending index
31
+ for (const match of code.matchAll(/^import\s.*';$/gm)) {
32
+ if (match.length === 0 || typeof match.index !== "number") {
33
+ continue;
34
+ }
35
+
36
+ endIndexOfLastImport = match.index + match[0].length;
37
+ }
38
+
39
+ const s = new MagicString(code);
40
+ s.appendRight(endIndexOfLastImport, ESMShim);
41
+
42
+ const sourceMap = s.generateMap({
43
+ includeContent: true,
44
+ });
45
+
46
+ let sourcesContent: string[] | undefined;
47
+ if (Array.isArray(sourceMap.sourcesContent)) {
48
+ sourcesContent = [];
49
+ for (let i = 0; i < sourceMap.sourcesContent.length; i++) {
50
+ const content = sourceMap.sourcesContent[i];
51
+ if (typeof content === "string") {
52
+ sourcesContent.push(content);
53
+ }
54
+ }
55
+ }
56
+
57
+ return {
58
+ code: s.toString(),
59
+ map: {
60
+ ...sourceMap,
61
+ sourcesContent,
62
+ },
63
+ };
64
+ }
65
+
66
+ return null;
67
+ },
68
+ };
69
+ }
@@ -0,0 +1,17 @@
1
+ import virtual from "@rollup/plugin-virtual";
2
+ import type { Apibara } from "apibara/types";
3
+ import { hash } from "ohash";
4
+
5
+ export function indexers(apibara: Apibara) {
6
+ const indexers = [...new Set(apibara.indexers)];
7
+
8
+ return virtual({
9
+ "#apibara-internal-virtual/indexers": `
10
+ ${indexers.map((i) => `import _${hash(i)} from '${i.indexer}';`).join("\n")}
11
+
12
+ export const indexers = [
13
+ ${indexers.map((i) => `{ name: "${i.name}", indexer: _${hash(i)} }`).join(",\n")}
14
+ ];
15
+ `,
16
+ });
17
+ }
@@ -0,0 +1,64 @@
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";
5
+
6
+ const startCommand = defineCommand({
7
+ meta: {
8
+ name: "start",
9
+ description: "Start the indexer",
10
+ },
11
+ args: {
12
+ indexers: {
13
+ type: "string",
14
+ description: "Which indexers to run",
15
+ },
16
+ preset: {
17
+ type: "string",
18
+ description: "Preset to use",
19
+ },
20
+ },
21
+ async run({ args }) {
22
+ const { indexers: indexersArgs, preset } = args;
23
+
24
+ let selectedIndexers = availableIndexers;
25
+ if (indexersArgs) {
26
+ selectedIndexers = indexersArgs.split(",");
27
+ }
28
+
29
+ for (const indexer of selectedIndexers) {
30
+ if (!availableIndexers.includes(indexer)) {
31
+ throw new Error(
32
+ `Specified indexer "${indexer}" but it was not defined`,
33
+ );
34
+ }
35
+ }
36
+
37
+ await Promise.all(
38
+ selectedIndexers.map(async (indexer) => {
39
+ const indexerInstance = createIndexer(indexer, preset);
40
+
41
+ const client = createClient(
42
+ indexerInstance.streamConfig,
43
+ indexerInstance.options.streamUrl,
44
+ );
45
+
46
+ await runWithReconnect(client, indexerInstance);
47
+ }),
48
+ );
49
+ },
50
+ });
51
+
52
+ export const mainCli = defineCommand({
53
+ meta: {
54
+ name: "indexer-dev-runner",
55
+ description: "Run indexer in dev mode",
56
+ },
57
+ subCommands: {
58
+ start: () => startCommand,
59
+ },
60
+ });
61
+
62
+ runMain(mainCli);
63
+
64
+ export default {};
@@ -0,0 +1,2 @@
1
+ export { createIndexer } from "./internal/app";
2
+ export { createLogger } from "./internal/logger";
@@ -0,0 +1,78 @@
1
+ import { createIndexer as _createIndexer } from "@apibara/indexer";
2
+ import {
3
+ type InternalContext,
4
+ internalContext,
5
+ } from "@apibara/indexer/internal/plugins";
6
+ import {
7
+ type ConsolaReporter,
8
+ inMemoryPersistence,
9
+ logger,
10
+ } from "@apibara/indexer/plugins";
11
+ import { config } from "#apibara-internal-virtual/config";
12
+ import { indexers } from "#apibara-internal-virtual/indexers";
13
+ import { createLogger } from "./logger";
14
+
15
+ export const availableIndexers = indexers.map((i) => i.name);
16
+
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
+
37
+ const indexerDefinition = indexers.find((i) => i.name === indexerName);
38
+
39
+ if (indexerDefinition === undefined) {
40
+ throw new Error(
41
+ `Specified indexer "${indexerName}" but it was not defined`,
42
+ );
43
+ }
44
+
45
+ const definition =
46
+ typeof indexerDefinition.indexer === "function"
47
+ ? indexerDefinition.indexer(runtimeConfig)
48
+ : indexerDefinition.indexer;
49
+
50
+ let reporter: ConsolaReporter = createLogger({
51
+ indexer: indexerName,
52
+ preset,
53
+ indexers: availableIndexers,
54
+ });
55
+
56
+ if (config.logger) {
57
+ reporter = config.logger({
58
+ indexer: indexerName,
59
+ preset,
60
+ indexers: availableIndexers,
61
+ });
62
+ }
63
+
64
+ // Put the in-memory persistence plugin first so that it can be overridden by any user-defined
65
+ // persistence plugin.
66
+ // Put the logger last since we want to override any user-defined logger.
67
+ definition.plugins = [
68
+ internalContext({
69
+ indexerName,
70
+ availableIndexers,
71
+ } as InternalContext),
72
+ inMemoryPersistence(),
73
+ ...(definition.plugins ?? []),
74
+ logger({ logger: reporter }),
75
+ ];
76
+
77
+ return _createIndexer(definition);
78
+ }
@@ -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,48 @@
1
+ import { runWithReconnect } from "@apibara/indexer";
2
+ import { createClient } from "@apibara/protocol";
3
+ import { defineCommand, runMain } from "citty";
4
+ import { createIndexer } from "./internal/app";
5
+
6
+ const startCommand = defineCommand({
7
+ meta: {
8
+ name: "start",
9
+ description: "Start the indexer",
10
+ },
11
+ args: {
12
+ indexer: {
13
+ type: "string",
14
+ description: "Indexer name",
15
+ required: true,
16
+ },
17
+ preset: {
18
+ type: "string",
19
+ description: "Preset to use",
20
+ },
21
+ },
22
+ async run({ args }) {
23
+ const { indexer, preset } = args;
24
+
25
+ const indexerInstance = createIndexer(indexer, preset);
26
+
27
+ const client = createClient(
28
+ indexerInstance.streamConfig,
29
+ indexerInstance.options.streamUrl,
30
+ );
31
+
32
+ await runWithReconnect(client, indexerInstance);
33
+ },
34
+ });
35
+
36
+ export const mainCli = defineCommand({
37
+ meta: {
38
+ name: "indexer-runner",
39
+ description: "Run an indexer",
40
+ },
41
+ subCommands: {
42
+ start: () => startCommand,
43
+ },
44
+ });
45
+
46
+ runMain(mainCli);
47
+
48
+ 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,4 +1,5 @@
1
- import type { Sink } from "@apibara/indexer";
1
+ import type { ConsolaReporter } from "@apibara/indexer/plugins";
2
+ import type { RollupCommonJSOptions } from "@rollup/plugin-commonjs";
2
3
  import type {
3
4
  C12InputConfig,
4
5
  ConfigWatcher,
@@ -7,28 +8,30 @@ import type {
7
8
  } from "c12";
8
9
  import type { WatchOptions } from "chokidar";
9
10
  import type { NestedHooks } from "hookable";
10
- import type { TSConfig } from "pkg-types";
11
11
  import type { DeepPartial } from "./_utils";
12
12
  import type { ApibaraHooks } from "./hooks";
13
13
  import type { RollupConfig } from "./rollup";
14
14
 
15
+ export type LoggerFactory = ({
16
+ indexer,
17
+ preset,
18
+ }: { indexer: string; indexers: string[]; preset?: string }) => ConsolaReporter;
19
+
15
20
  /**
16
21
  * Apibara Config type (apibara.config)
17
22
  */
18
23
  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">>,
24
+ T extends Record<
25
+ string,
26
+ DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>
27
+ > = Record<string, never>,
28
+ R extends Record<string, unknown> = Record<string, never>,
29
+ > extends Partial<Omit<ApibaraOptions<T, R>, "preset" | "presets" | "dev">>,
24
30
  C12InputConfig<ApibaraConfig<T, R>> {
25
- sink?: {
26
- default: () => Sink;
27
- [key: string]: () => Sink;
28
- };
29
31
  runtimeConfig?: R;
30
32
  presets?: T;
31
33
  preset?: keyof T;
34
+ logger?: LoggerFactory;
32
35
  }
33
36
 
34
37
  export type ApibaraDynamicConfig = Pick<ApibaraConfig, "runtimeConfig">;
@@ -42,10 +45,11 @@ export interface LoadConfigOptions {
42
45
  }
43
46
 
44
47
  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> = {},
48
+ T extends Record<
49
+ string,
50
+ DeepPartial<Pick<ApibaraConfig<T, R>, "runtimeConfig">>
51
+ > = Record<string, never>,
52
+ R extends Record<string, unknown> = Record<string, never>,
49
53
  > {
50
54
  // Internal
51
55
  _config: ApibaraConfig<T, R>;
@@ -53,12 +57,6 @@ export interface ApibaraOptions<
53
57
  | ResolvedConfig<ApibaraConfig<T, R>>
54
58
  | ConfigWatcher<ApibaraConfig<T, R>>;
55
59
 
56
- // Sink
57
- sink: {
58
- default: () => Sink;
59
- [key: string]: () => Sink;
60
- };
61
-
62
60
  // Presets
63
61
  presets?: T;
64
62
  preset?: keyof T;
@@ -69,25 +67,28 @@ export interface ApibaraOptions<
69
67
  rootDir: string;
70
68
  buildDir: string;
71
69
  outputDir: string;
70
+ indexersDir: string;
71
+
72
72
  // Dev
73
73
  dev: boolean;
74
74
  watchOptions: WatchOptions;
75
75
 
76
76
  // Hooks
77
77
  hooks: NestedHooks<ApibaraHooks>;
78
+
79
+ // Logging
80
+ logger?: LoggerFactory;
81
+
78
82
  // Rollup
79
- rollupConfig?: RollupConfig;
83
+ rollupConfig?: Partial<RollupConfig>;
84
+ sourceMap?: boolean;
80
85
  entry: string;
81
- minify: boolean;
86
+ commonJS?: RollupCommonJSOptions;
82
87
 
83
88
  // Advanced
84
89
  typescript: {
85
90
  strict?: boolean;
86
91
  internalPaths?: boolean;
87
92
  generateRuntimeConfigTypes?: boolean;
88
- generateTsConfig?: boolean;
89
- /** the path of the generated `tsconfig.json`, relative to buildDir */
90
- tsconfigPath: string;
91
- tsConfig?: Partial<TSConfig>;
92
93
  };
93
94
  }
@@ -4,6 +4,7 @@ import type { RollupConfig } from "./rollup";
4
4
  export interface ApibaraHooks {
5
5
  "rollup:before": (apibara: Apibara, rollupConfig: RollupConfig) => void;
6
6
  compiled: (apibara: Apibara) => void;
7
+ "dev:restart": () => void;
7
8
  "dev:reload": () => void;
8
9
  "rollup:reload": () => void;
9
10
  restart: () => void;
@@ -0,0 +1,3 @@
1
+ import type { ApibaraConfig } from "apibara/types";
2
+
3
+ export const config: ApibaraConfig = {};
@@ -0,0 +1,10 @@
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: { name: string; indexer: IndexerConstructor }[] = [];
@@ -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 +0,0 @@
1
- export * from "citty";
@@ -1 +0,0 @@
1
- export * from "consola";