@apibara/indexer 2.0.0-beta.4 → 2.0.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 (87) hide show
  1. package/dist/index.cjs +278 -0
  2. package/dist/index.d.cts +3 -0
  3. package/dist/index.d.mts +3 -0
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.mjs +267 -0
  6. package/dist/internal/index.cjs +10 -0
  7. package/dist/internal/index.d.cts +3 -0
  8. package/dist/internal/index.d.mts +3 -0
  9. package/dist/internal/index.d.ts +3 -0
  10. package/dist/internal/index.mjs +8 -0
  11. package/dist/internal/plugins.cjs +38 -0
  12. package/dist/internal/plugins.d.cts +13 -0
  13. package/dist/internal/plugins.d.mts +13 -0
  14. package/dist/internal/plugins.d.ts +13 -0
  15. package/dist/internal/plugins.mjs +34 -0
  16. package/dist/internal/testing.cjs +118 -0
  17. package/dist/internal/testing.d.cts +42 -0
  18. package/dist/internal/testing.d.mts +42 -0
  19. package/dist/internal/testing.d.ts +42 -0
  20. package/dist/internal/testing.mjs +113 -0
  21. package/dist/plugins/index.cjs +43 -0
  22. package/dist/plugins/index.d.cts +18 -0
  23. package/dist/plugins/index.d.mts +18 -0
  24. package/dist/plugins/index.d.ts +18 -0
  25. package/dist/plugins/index.mjs +38 -0
  26. package/dist/shared/indexer.077335f3.cjs +15 -0
  27. package/dist/shared/indexer.2416906c.cjs +29 -0
  28. package/dist/shared/indexer.601ceab0.cjs +7 -0
  29. package/dist/shared/indexer.9b21ddd2.mjs +5 -0
  30. package/dist/shared/indexer.a55ad619.mjs +12 -0
  31. package/dist/shared/indexer.fedcd831.d.cts +100 -0
  32. package/dist/shared/indexer.fedcd831.d.mts +100 -0
  33. package/dist/shared/indexer.fedcd831.d.ts +100 -0
  34. package/dist/shared/indexer.ff25c953.mjs +26 -0
  35. package/dist/testing/index.cjs +66 -0
  36. package/dist/testing/index.d.cts +12 -0
  37. package/dist/testing/index.d.mts +12 -0
  38. package/dist/testing/index.d.ts +12 -0
  39. package/dist/testing/index.mjs +60 -0
  40. package/dist/vcr/index.cjs +92 -0
  41. package/dist/vcr/index.d.cts +27 -0
  42. package/dist/vcr/index.d.mts +27 -0
  43. package/dist/vcr/index.d.ts +27 -0
  44. package/dist/vcr/index.mjs +78 -0
  45. package/package.json +43 -41
  46. package/src/compose.test.ts +76 -0
  47. package/src/compose.ts +71 -0
  48. package/src/context.ts +14 -8
  49. package/src/index.ts +0 -5
  50. package/src/indexer.test.ts +125 -186
  51. package/src/indexer.ts +274 -151
  52. package/src/internal/index.ts +6 -0
  53. package/src/internal/plugins.ts +1 -0
  54. package/src/internal/testing.ts +148 -0
  55. package/src/plugins/config.ts +4 -4
  56. package/src/plugins/context.ts +40 -0
  57. package/src/plugins/index.ts +8 -1
  58. package/src/plugins/logger.ts +30 -0
  59. package/src/plugins/persistence.ts +24 -187
  60. package/src/testing/index.ts +58 -3
  61. package/src/vcr/record.ts +6 -4
  62. package/src/vcr/replay.ts +8 -18
  63. package/src/hooks/index.ts +0 -2
  64. package/src/hooks/useKVStore.ts +0 -12
  65. package/src/hooks/useSink.ts +0 -13
  66. package/src/plugins/kv.test.ts +0 -120
  67. package/src/plugins/kv.ts +0 -132
  68. package/src/plugins/persistence.test.ts +0 -151
  69. package/src/sink.ts +0 -36
  70. package/src/sinks/csv.test.ts +0 -65
  71. package/src/sinks/csv.ts +0 -159
  72. package/src/sinks/drizzle/Int8Range.ts +0 -52
  73. package/src/sinks/drizzle/delete.ts +0 -42
  74. package/src/sinks/drizzle/drizzle.test.ts +0 -239
  75. package/src/sinks/drizzle/drizzle.ts +0 -115
  76. package/src/sinks/drizzle/index.ts +0 -6
  77. package/src/sinks/drizzle/insert.ts +0 -39
  78. package/src/sinks/drizzle/select.ts +0 -44
  79. package/src/sinks/drizzle/transaction.ts +0 -49
  80. package/src/sinks/drizzle/update.ts +0 -47
  81. package/src/sinks/drizzle/utils.ts +0 -36
  82. package/src/sinks/sqlite.test.ts +0 -99
  83. package/src/sinks/sqlite.ts +0 -170
  84. package/src/testing/helper.ts +0 -13
  85. package/src/testing/indexer.ts +0 -35
  86. package/src/testing/setup.ts +0 -59
  87. package/src/testing/vcr.ts +0 -54
@@ -0,0 +1,100 @@
1
+ import { StreamDataRequest, StreamDataOptions, Cursor, StreamDataResponse, Invalidate, Finalize, Heartbeat, SystemMessage, DataFinality, StreamConfig, Client } from '@apibara/protocol';
2
+ import { NestedHooks, Hookable } from 'hookable';
3
+
4
+ interface IndexerContext extends Record<string, any> {
5
+ }
6
+ declare function useIndexerContext(): IndexerContext;
7
+
8
+ type NextFunction = () => Promise<void>;
9
+ type MiddlewareFunction<C> = (context: C, next: NextFunction) => Promise<void>;
10
+
11
+ type IndexerPlugin<TFilter, TBlock> = (indexer: Indexer<TFilter, TBlock>) => void;
12
+ declare function defineIndexerPlugin<TFilter, TBlock>(def: IndexerPlugin<TFilter, TBlock>): IndexerPlugin<TFilter, TBlock>;
13
+
14
+ type UseMiddlewareFunction = (fn: MiddlewareFunction<IndexerContext>) => void;
15
+ interface IndexerHooks<TFilter, TBlock> {
16
+ "run:before": () => void;
17
+ "run:after": () => void;
18
+ "connect:before": ({ request, options, }: {
19
+ request: StreamDataRequest<TFilter>;
20
+ options: StreamDataOptions;
21
+ }) => void;
22
+ "connect:after": ({ request, }: {
23
+ request: StreamDataRequest<TFilter>;
24
+ }) => void;
25
+ "connect:factory": ({ request, endCursor, }: {
26
+ request: StreamDataRequest<TFilter>;
27
+ endCursor?: Cursor;
28
+ }) => void;
29
+ "handler:middleware": ({ use }: {
30
+ use: UseMiddlewareFunction;
31
+ }) => void;
32
+ message: ({ message }: {
33
+ message: StreamDataResponse<TBlock>;
34
+ }) => void;
35
+ "message:invalidate": ({ message }: {
36
+ message: Invalidate;
37
+ }) => void;
38
+ "message:finalize": ({ message }: {
39
+ message: Finalize;
40
+ }) => void;
41
+ "message:heartbeat": ({ message }: {
42
+ message: Heartbeat;
43
+ }) => void;
44
+ "message:systemMessage": ({ message }: {
45
+ message: SystemMessage;
46
+ }) => void;
47
+ }
48
+ type IndexerStartingCursor = {
49
+ startingCursor?: never;
50
+ startingBlock: bigint;
51
+ } | {
52
+ startingCursor: Cursor;
53
+ startingBlock?: never;
54
+ } | {
55
+ startingCursor?: never;
56
+ startingBlock?: never;
57
+ };
58
+ type IndexerConfig<TFilter, TBlock> = {
59
+ streamUrl: string;
60
+ filter: TFilter;
61
+ finality?: DataFinality;
62
+ factory?: ({ block, context, }: {
63
+ block: TBlock;
64
+ context: IndexerContext;
65
+ }) => Promise<{
66
+ filter?: TFilter;
67
+ }>;
68
+ transform: (args: {
69
+ block: TBlock;
70
+ cursor?: Cursor | undefined;
71
+ endCursor?: Cursor | undefined;
72
+ finality: DataFinality;
73
+ context: IndexerContext;
74
+ }) => Promise<void>;
75
+ hooks?: NestedHooks<IndexerHooks<TFilter, TBlock>>;
76
+ plugins?: ReadonlyArray<IndexerPlugin<TFilter, TBlock>>;
77
+ debug?: boolean;
78
+ } & IndexerStartingCursor;
79
+ type IndexerWithStreamConfig<TFilter, TBlock> = IndexerConfig<TFilter, TBlock> & {
80
+ streamConfig: StreamConfig<TFilter, TBlock>;
81
+ };
82
+ declare function defineIndexer<TFilter, TBlock>(streamConfig: StreamConfig<TFilter, TBlock>): (config: IndexerConfig<TFilter, TBlock>) => IndexerWithStreamConfig<TFilter, TBlock>;
83
+ interface Indexer<TFilter, TBlock> {
84
+ streamConfig: StreamConfig<TFilter, TBlock>;
85
+ options: IndexerConfig<TFilter, TBlock>;
86
+ hooks: Hookable<IndexerHooks<TFilter, TBlock>>;
87
+ }
88
+ declare function createIndexer<TFilter, TBlock>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock>): Indexer<TFilter, TBlock>;
89
+ interface ReconnectOptions {
90
+ maxRetries?: number;
91
+ retryDelay?: number;
92
+ maxWait?: number;
93
+ }
94
+ declare function runWithReconnect<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, options?: ReconnectOptions): Promise<void>;
95
+ interface RunOptions {
96
+ onConnect?: () => void | Promise<void>;
97
+ }
98
+ declare function run<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, runOptions?: RunOptions): Promise<void>;
99
+
100
+ export { type IndexerWithStreamConfig as I, type ReconnectOptions as R, type UseMiddlewareFunction as U, type Indexer as a, type IndexerPlugin as b, type IndexerConfig as c, defineIndexerPlugin as d, type IndexerHooks as e, type IndexerStartingCursor as f, defineIndexer as g, createIndexer as h, type RunOptions as i, run as j, runWithReconnect as r, useIndexerContext as u };
@@ -0,0 +1,100 @@
1
+ import { StreamDataRequest, StreamDataOptions, Cursor, StreamDataResponse, Invalidate, Finalize, Heartbeat, SystemMessage, DataFinality, StreamConfig, Client } from '@apibara/protocol';
2
+ import { NestedHooks, Hookable } from 'hookable';
3
+
4
+ interface IndexerContext extends Record<string, any> {
5
+ }
6
+ declare function useIndexerContext(): IndexerContext;
7
+
8
+ type NextFunction = () => Promise<void>;
9
+ type MiddlewareFunction<C> = (context: C, next: NextFunction) => Promise<void>;
10
+
11
+ type IndexerPlugin<TFilter, TBlock> = (indexer: Indexer<TFilter, TBlock>) => void;
12
+ declare function defineIndexerPlugin<TFilter, TBlock>(def: IndexerPlugin<TFilter, TBlock>): IndexerPlugin<TFilter, TBlock>;
13
+
14
+ type UseMiddlewareFunction = (fn: MiddlewareFunction<IndexerContext>) => void;
15
+ interface IndexerHooks<TFilter, TBlock> {
16
+ "run:before": () => void;
17
+ "run:after": () => void;
18
+ "connect:before": ({ request, options, }: {
19
+ request: StreamDataRequest<TFilter>;
20
+ options: StreamDataOptions;
21
+ }) => void;
22
+ "connect:after": ({ request, }: {
23
+ request: StreamDataRequest<TFilter>;
24
+ }) => void;
25
+ "connect:factory": ({ request, endCursor, }: {
26
+ request: StreamDataRequest<TFilter>;
27
+ endCursor?: Cursor;
28
+ }) => void;
29
+ "handler:middleware": ({ use }: {
30
+ use: UseMiddlewareFunction;
31
+ }) => void;
32
+ message: ({ message }: {
33
+ message: StreamDataResponse<TBlock>;
34
+ }) => void;
35
+ "message:invalidate": ({ message }: {
36
+ message: Invalidate;
37
+ }) => void;
38
+ "message:finalize": ({ message }: {
39
+ message: Finalize;
40
+ }) => void;
41
+ "message:heartbeat": ({ message }: {
42
+ message: Heartbeat;
43
+ }) => void;
44
+ "message:systemMessage": ({ message }: {
45
+ message: SystemMessage;
46
+ }) => void;
47
+ }
48
+ type IndexerStartingCursor = {
49
+ startingCursor?: never;
50
+ startingBlock: bigint;
51
+ } | {
52
+ startingCursor: Cursor;
53
+ startingBlock?: never;
54
+ } | {
55
+ startingCursor?: never;
56
+ startingBlock?: never;
57
+ };
58
+ type IndexerConfig<TFilter, TBlock> = {
59
+ streamUrl: string;
60
+ filter: TFilter;
61
+ finality?: DataFinality;
62
+ factory?: ({ block, context, }: {
63
+ block: TBlock;
64
+ context: IndexerContext;
65
+ }) => Promise<{
66
+ filter?: TFilter;
67
+ }>;
68
+ transform: (args: {
69
+ block: TBlock;
70
+ cursor?: Cursor | undefined;
71
+ endCursor?: Cursor | undefined;
72
+ finality: DataFinality;
73
+ context: IndexerContext;
74
+ }) => Promise<void>;
75
+ hooks?: NestedHooks<IndexerHooks<TFilter, TBlock>>;
76
+ plugins?: ReadonlyArray<IndexerPlugin<TFilter, TBlock>>;
77
+ debug?: boolean;
78
+ } & IndexerStartingCursor;
79
+ type IndexerWithStreamConfig<TFilter, TBlock> = IndexerConfig<TFilter, TBlock> & {
80
+ streamConfig: StreamConfig<TFilter, TBlock>;
81
+ };
82
+ declare function defineIndexer<TFilter, TBlock>(streamConfig: StreamConfig<TFilter, TBlock>): (config: IndexerConfig<TFilter, TBlock>) => IndexerWithStreamConfig<TFilter, TBlock>;
83
+ interface Indexer<TFilter, TBlock> {
84
+ streamConfig: StreamConfig<TFilter, TBlock>;
85
+ options: IndexerConfig<TFilter, TBlock>;
86
+ hooks: Hookable<IndexerHooks<TFilter, TBlock>>;
87
+ }
88
+ declare function createIndexer<TFilter, TBlock>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock>): Indexer<TFilter, TBlock>;
89
+ interface ReconnectOptions {
90
+ maxRetries?: number;
91
+ retryDelay?: number;
92
+ maxWait?: number;
93
+ }
94
+ declare function runWithReconnect<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, options?: ReconnectOptions): Promise<void>;
95
+ interface RunOptions {
96
+ onConnect?: () => void | Promise<void>;
97
+ }
98
+ declare function run<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, runOptions?: RunOptions): Promise<void>;
99
+
100
+ export { type IndexerWithStreamConfig as I, type ReconnectOptions as R, type UseMiddlewareFunction as U, type Indexer as a, type IndexerPlugin as b, type IndexerConfig as c, defineIndexerPlugin as d, type IndexerHooks as e, type IndexerStartingCursor as f, defineIndexer as g, createIndexer as h, type RunOptions as i, run as j, runWithReconnect as r, useIndexerContext as u };
@@ -0,0 +1,100 @@
1
+ import { StreamDataRequest, StreamDataOptions, Cursor, StreamDataResponse, Invalidate, Finalize, Heartbeat, SystemMessage, DataFinality, StreamConfig, Client } from '@apibara/protocol';
2
+ import { NestedHooks, Hookable } from 'hookable';
3
+
4
+ interface IndexerContext extends Record<string, any> {
5
+ }
6
+ declare function useIndexerContext(): IndexerContext;
7
+
8
+ type NextFunction = () => Promise<void>;
9
+ type MiddlewareFunction<C> = (context: C, next: NextFunction) => Promise<void>;
10
+
11
+ type IndexerPlugin<TFilter, TBlock> = (indexer: Indexer<TFilter, TBlock>) => void;
12
+ declare function defineIndexerPlugin<TFilter, TBlock>(def: IndexerPlugin<TFilter, TBlock>): IndexerPlugin<TFilter, TBlock>;
13
+
14
+ type UseMiddlewareFunction = (fn: MiddlewareFunction<IndexerContext>) => void;
15
+ interface IndexerHooks<TFilter, TBlock> {
16
+ "run:before": () => void;
17
+ "run:after": () => void;
18
+ "connect:before": ({ request, options, }: {
19
+ request: StreamDataRequest<TFilter>;
20
+ options: StreamDataOptions;
21
+ }) => void;
22
+ "connect:after": ({ request, }: {
23
+ request: StreamDataRequest<TFilter>;
24
+ }) => void;
25
+ "connect:factory": ({ request, endCursor, }: {
26
+ request: StreamDataRequest<TFilter>;
27
+ endCursor?: Cursor;
28
+ }) => void;
29
+ "handler:middleware": ({ use }: {
30
+ use: UseMiddlewareFunction;
31
+ }) => void;
32
+ message: ({ message }: {
33
+ message: StreamDataResponse<TBlock>;
34
+ }) => void;
35
+ "message:invalidate": ({ message }: {
36
+ message: Invalidate;
37
+ }) => void;
38
+ "message:finalize": ({ message }: {
39
+ message: Finalize;
40
+ }) => void;
41
+ "message:heartbeat": ({ message }: {
42
+ message: Heartbeat;
43
+ }) => void;
44
+ "message:systemMessage": ({ message }: {
45
+ message: SystemMessage;
46
+ }) => void;
47
+ }
48
+ type IndexerStartingCursor = {
49
+ startingCursor?: never;
50
+ startingBlock: bigint;
51
+ } | {
52
+ startingCursor: Cursor;
53
+ startingBlock?: never;
54
+ } | {
55
+ startingCursor?: never;
56
+ startingBlock?: never;
57
+ };
58
+ type IndexerConfig<TFilter, TBlock> = {
59
+ streamUrl: string;
60
+ filter: TFilter;
61
+ finality?: DataFinality;
62
+ factory?: ({ block, context, }: {
63
+ block: TBlock;
64
+ context: IndexerContext;
65
+ }) => Promise<{
66
+ filter?: TFilter;
67
+ }>;
68
+ transform: (args: {
69
+ block: TBlock;
70
+ cursor?: Cursor | undefined;
71
+ endCursor?: Cursor | undefined;
72
+ finality: DataFinality;
73
+ context: IndexerContext;
74
+ }) => Promise<void>;
75
+ hooks?: NestedHooks<IndexerHooks<TFilter, TBlock>>;
76
+ plugins?: ReadonlyArray<IndexerPlugin<TFilter, TBlock>>;
77
+ debug?: boolean;
78
+ } & IndexerStartingCursor;
79
+ type IndexerWithStreamConfig<TFilter, TBlock> = IndexerConfig<TFilter, TBlock> & {
80
+ streamConfig: StreamConfig<TFilter, TBlock>;
81
+ };
82
+ declare function defineIndexer<TFilter, TBlock>(streamConfig: StreamConfig<TFilter, TBlock>): (config: IndexerConfig<TFilter, TBlock>) => IndexerWithStreamConfig<TFilter, TBlock>;
83
+ interface Indexer<TFilter, TBlock> {
84
+ streamConfig: StreamConfig<TFilter, TBlock>;
85
+ options: IndexerConfig<TFilter, TBlock>;
86
+ hooks: Hookable<IndexerHooks<TFilter, TBlock>>;
87
+ }
88
+ declare function createIndexer<TFilter, TBlock>({ streamConfig, ...options }: IndexerWithStreamConfig<TFilter, TBlock>): Indexer<TFilter, TBlock>;
89
+ interface ReconnectOptions {
90
+ maxRetries?: number;
91
+ retryDelay?: number;
92
+ maxWait?: number;
93
+ }
94
+ declare function runWithReconnect<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, options?: ReconnectOptions): Promise<void>;
95
+ interface RunOptions {
96
+ onConnect?: () => void | Promise<void>;
97
+ }
98
+ declare function run<TFilter, TBlock>(client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, runOptions?: RunOptions): Promise<void>;
99
+
100
+ export { type IndexerWithStreamConfig as I, type ReconnectOptions as R, type UseMiddlewareFunction as U, type Indexer as a, type IndexerPlugin as b, type IndexerConfig as c, defineIndexerPlugin as d, type IndexerHooks as e, type IndexerStartingCursor as f, defineIndexer as g, createIndexer as h, type RunOptions as i, run as j, runWithReconnect as r, useIndexerContext as u };
@@ -0,0 +1,26 @@
1
+ import { consola } from 'consola';
2
+ import { u as useIndexerContext } from './indexer.a55ad619.mjs';
3
+ import { d as defineIndexerPlugin } from './indexer.9b21ddd2.mjs';
4
+
5
+ function logger({
6
+ logger: logger2
7
+ } = {}) {
8
+ return defineIndexerPlugin((indexer) => {
9
+ indexer.hooks.hook("run:before", () => {
10
+ const ctx = useIndexerContext();
11
+ if (logger2) {
12
+ ctx.logger = consola.create({ reporters: [logger2] });
13
+ } else {
14
+ ctx.logger = consola.create({});
15
+ }
16
+ });
17
+ });
18
+ }
19
+ function useLogger() {
20
+ const ctx = useIndexerContext();
21
+ if (!ctx?.logger)
22
+ throw new Error("Logger plugin is not available in context");
23
+ return ctx.logger;
24
+ }
25
+
26
+ export { logger as l, useLogger as u };
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ const protocol = require('@apibara/protocol');
4
+ const ci = require('ci-info');
5
+ const index = require('../index.cjs');
6
+ const internal_plugins = require('../internal/plugins.cjs');
7
+ const logger = require('../shared/indexer.2416906c.cjs');
8
+ const vcr_index = require('../vcr/index.cjs');
9
+ require('consola');
10
+ require('hookable');
11
+ require('node:assert');
12
+ require('../shared/indexer.077335f3.cjs');
13
+ require('node:async_hooks');
14
+ require('unctx');
15
+ require('@opentelemetry/api');
16
+ require('../shared/indexer.601ceab0.cjs');
17
+ require('node:fs/promises');
18
+ require('node:path');
19
+ require('node:fs');
20
+ require('@apibara/protocol/testing');
21
+
22
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
23
+
24
+ const ci__default = /*#__PURE__*/_interopDefaultCompat(ci);
25
+
26
+ function createVcr() {
27
+ return {
28
+ async run(cassetteName, indexerConfig, range) {
29
+ const vcrConfig = {
30
+ cassetteDir: "cassettes"
31
+ };
32
+ const cassetteOptions = {
33
+ name: cassetteName,
34
+ startingCursor: {
35
+ orderKey: range.fromBlock
36
+ },
37
+ endingCursor: {
38
+ orderKey: range.toBlock
39
+ }
40
+ };
41
+ indexerConfig.plugins = [
42
+ internal_plugins.internalContext({
43
+ indexerName: cassetteName,
44
+ availableIndexers: [cassetteName]
45
+ }),
46
+ logger.logger(),
47
+ ...indexerConfig.plugins ?? []
48
+ ];
49
+ const indexer = index.createIndexer(indexerConfig);
50
+ if (!vcr_index.isCassetteAvailable(vcrConfig, cassetteName)) {
51
+ if (ci__default.isCI) {
52
+ throw new Error("Cannot record cassette in CI");
53
+ }
54
+ const client = protocol.createClient(
55
+ indexer.streamConfig,
56
+ indexer.options.streamUrl
57
+ );
58
+ await vcr_index.record(vcrConfig, client, indexer, cassetteOptions);
59
+ } else {
60
+ await vcr_index.replay(vcrConfig, indexer, cassetteName);
61
+ }
62
+ }
63
+ };
64
+ }
65
+
66
+ exports.createVcr = createVcr;
@@ -0,0 +1,12 @@
1
+ import { I as IndexerWithStreamConfig } from '../shared/indexer.fedcd831.cjs';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+
5
+ declare function createVcr(): {
6
+ run<TFilter, TBlock>(cassetteName: string, indexerConfig: IndexerWithStreamConfig<TFilter, TBlock>, range: {
7
+ fromBlock: bigint;
8
+ toBlock: bigint;
9
+ }): Promise<void>;
10
+ };
11
+
12
+ export { createVcr };
@@ -0,0 +1,12 @@
1
+ import { I as IndexerWithStreamConfig } from '../shared/indexer.fedcd831.mjs';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+
5
+ declare function createVcr(): {
6
+ run<TFilter, TBlock>(cassetteName: string, indexerConfig: IndexerWithStreamConfig<TFilter, TBlock>, range: {
7
+ fromBlock: bigint;
8
+ toBlock: bigint;
9
+ }): Promise<void>;
10
+ };
11
+
12
+ export { createVcr };
@@ -0,0 +1,12 @@
1
+ import { I as IndexerWithStreamConfig } from '../shared/indexer.fedcd831.js';
2
+ import '@apibara/protocol';
3
+ import 'hookable';
4
+
5
+ declare function createVcr(): {
6
+ run<TFilter, TBlock>(cassetteName: string, indexerConfig: IndexerWithStreamConfig<TFilter, TBlock>, range: {
7
+ fromBlock: bigint;
8
+ toBlock: bigint;
9
+ }): Promise<void>;
10
+ };
11
+
12
+ export { createVcr };
@@ -0,0 +1,60 @@
1
+ import { createClient } from '@apibara/protocol';
2
+ import ci from 'ci-info';
3
+ import { createIndexer } from '../index.mjs';
4
+ import { internalContext } from '../internal/plugins.mjs';
5
+ import { l as logger } from '../shared/indexer.ff25c953.mjs';
6
+ import { isCassetteAvailable, record, replay } from '../vcr/index.mjs';
7
+ import 'consola';
8
+ import 'hookable';
9
+ import 'node:assert';
10
+ import '../shared/indexer.a55ad619.mjs';
11
+ import 'node:async_hooks';
12
+ import 'unctx';
13
+ import '@opentelemetry/api';
14
+ import '../shared/indexer.9b21ddd2.mjs';
15
+ import 'node:fs/promises';
16
+ import 'node:path';
17
+ import 'node:fs';
18
+ import '@apibara/protocol/testing';
19
+
20
+ function createVcr() {
21
+ return {
22
+ async run(cassetteName, indexerConfig, range) {
23
+ const vcrConfig = {
24
+ cassetteDir: "cassettes"
25
+ };
26
+ const cassetteOptions = {
27
+ name: cassetteName,
28
+ startingCursor: {
29
+ orderKey: range.fromBlock
30
+ },
31
+ endingCursor: {
32
+ orderKey: range.toBlock
33
+ }
34
+ };
35
+ indexerConfig.plugins = [
36
+ internalContext({
37
+ indexerName: cassetteName,
38
+ availableIndexers: [cassetteName]
39
+ }),
40
+ logger(),
41
+ ...indexerConfig.plugins ?? []
42
+ ];
43
+ const indexer = createIndexer(indexerConfig);
44
+ if (!isCassetteAvailable(vcrConfig, cassetteName)) {
45
+ if (ci.isCI) {
46
+ throw new Error("Cannot record cassette in CI");
47
+ }
48
+ const client = createClient(
49
+ indexer.streamConfig,
50
+ indexer.options.streamUrl
51
+ );
52
+ await record(vcrConfig, client, indexer, cassetteOptions);
53
+ } else {
54
+ await replay(vcrConfig, indexer, cassetteName);
55
+ }
56
+ }
57
+ };
58
+ }
59
+
60
+ export { createVcr };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ const fs$1 = require('node:fs/promises');
4
+ const path = require('node:path');
5
+ const index = require('../index.cjs');
6
+ const assert = require('node:assert');
7
+ const fs = require('node:fs');
8
+ const testing = require('@apibara/protocol/testing');
9
+ require('@apibara/protocol');
10
+ require('consola');
11
+ require('hookable');
12
+ require('../shared/indexer.077335f3.cjs');
13
+ require('node:async_hooks');
14
+ require('unctx');
15
+ require('@opentelemetry/api');
16
+
17
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
18
+
19
+ const fs__default$1 = /*#__PURE__*/_interopDefaultCompat(fs$1);
20
+ const path__default = /*#__PURE__*/_interopDefaultCompat(path);
21
+ const assert__default = /*#__PURE__*/_interopDefaultCompat(assert);
22
+ const fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
23
+
24
+ function deserialize(str) {
25
+ return JSON.parse(
26
+ str,
27
+ (_, value) => typeof value === "string" && value.match(/^\d+n$/) ? BigInt(value.slice(0, -1)) : value
28
+ );
29
+ }
30
+ function serialize(obj) {
31
+ return JSON.stringify(
32
+ obj,
33
+ (_, value) => typeof value === "bigint" ? `${value.toString()}n` : value,
34
+ " "
35
+ );
36
+ }
37
+ function isCassetteAvailable(vcrConfig, cassetteName) {
38
+ const filePath = path__default.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
39
+ return fs__default.existsSync(filePath);
40
+ }
41
+
42
+ async function record(vcrConfig, client, indexer, cassetteOptions) {
43
+ const messages = [];
44
+ indexer.hooks.addHooks({
45
+ "connect:before"({ options, request }) {
46
+ request.startingCursor = cassetteOptions.startingCursor;
47
+ options.endingCursor = cassetteOptions.endingCursor;
48
+ },
49
+ message({ message }) {
50
+ messages.push(message);
51
+ },
52
+ async "run:after"() {
53
+ const output = {
54
+ filter: indexer.options.filter,
55
+ messages
56
+ };
57
+ await fs__default$1.mkdir(vcrConfig.cassetteDir, { recursive: true });
58
+ const filePath = path__default.join(
59
+ vcrConfig.cassetteDir,
60
+ `${cassetteOptions.name}.json`
61
+ );
62
+ await fs__default$1.writeFile(filePath, serialize(output), { flag: "w" });
63
+ }
64
+ });
65
+ await index.run(client, indexer);
66
+ }
67
+
68
+ async function replay(vcrConfig, indexer, cassetteName) {
69
+ const client = loadCassette(vcrConfig, cassetteName);
70
+ await index.run(client, indexer);
71
+ }
72
+ function loadCassette(vcrConfig, cassetteName) {
73
+ const filePath = path__default.join(vcrConfig.cassetteDir, `${cassetteName}.json`);
74
+ const data = fs__default.readFileSync(filePath, "utf8");
75
+ const cassetteData = deserialize(data);
76
+ const { filter, messages } = cassetteData;
77
+ return new testing.MockClient((request, options) => {
78
+ assert__default.deepStrictEqual(
79
+ request.filter,
80
+ [filter],
81
+ "Indexer and cassette filter mismatch. Hint: delete the cassette and run again."
82
+ );
83
+ return messages;
84
+ });
85
+ }
86
+
87
+ exports.deserialize = deserialize;
88
+ exports.isCassetteAvailable = isCassetteAvailable;
89
+ exports.loadCassette = loadCassette;
90
+ exports.record = record;
91
+ exports.replay = replay;
92
+ exports.serialize = serialize;
@@ -0,0 +1,27 @@
1
+ import { Cursor, StreamDataResponse, Client } from '@apibara/protocol';
2
+ import { a as Indexer } from '../shared/indexer.fedcd831.cjs';
3
+ import 'hookable';
4
+
5
+ type VcrConfig = {
6
+ cassetteDir: string;
7
+ };
8
+ type CassetteOptions = {
9
+ name: string;
10
+ startingCursor: Cursor;
11
+ endingCursor: Cursor;
12
+ };
13
+
14
+ declare function deserialize(str: string): any;
15
+ declare function serialize(obj: Record<string, unknown>): string;
16
+ declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
17
+
18
+ type CassetteDataType<TFilter, TBlock> = {
19
+ filter: TFilter;
20
+ messages: StreamDataResponse<TBlock>[];
21
+ };
22
+ declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, cassetteOptions: CassetteOptions): Promise<void>;
23
+
24
+ declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock>, cassetteName: string): Promise<void>;
25
+ declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
26
+
27
+ export { type CassetteDataType, type CassetteOptions, type VcrConfig, deserialize, isCassetteAvailable, loadCassette, record, replay, serialize };
@@ -0,0 +1,27 @@
1
+ import { Cursor, StreamDataResponse, Client } from '@apibara/protocol';
2
+ import { a as Indexer } from '../shared/indexer.fedcd831.mjs';
3
+ import 'hookable';
4
+
5
+ type VcrConfig = {
6
+ cassetteDir: string;
7
+ };
8
+ type CassetteOptions = {
9
+ name: string;
10
+ startingCursor: Cursor;
11
+ endingCursor: Cursor;
12
+ };
13
+
14
+ declare function deserialize(str: string): any;
15
+ declare function serialize(obj: Record<string, unknown>): string;
16
+ declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
17
+
18
+ type CassetteDataType<TFilter, TBlock> = {
19
+ filter: TFilter;
20
+ messages: StreamDataResponse<TBlock>[];
21
+ };
22
+ declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, cassetteOptions: CassetteOptions): Promise<void>;
23
+
24
+ declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock>, cassetteName: string): Promise<void>;
25
+ declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
26
+
27
+ export { type CassetteDataType, type CassetteOptions, type VcrConfig, deserialize, isCassetteAvailable, loadCassette, record, replay, serialize };
@@ -0,0 +1,27 @@
1
+ import { Cursor, StreamDataResponse, Client } from '@apibara/protocol';
2
+ import { a as Indexer } from '../shared/indexer.fedcd831.js';
3
+ import 'hookable';
4
+
5
+ type VcrConfig = {
6
+ cassetteDir: string;
7
+ };
8
+ type CassetteOptions = {
9
+ name: string;
10
+ startingCursor: Cursor;
11
+ endingCursor: Cursor;
12
+ };
13
+
14
+ declare function deserialize(str: string): any;
15
+ declare function serialize(obj: Record<string, unknown>): string;
16
+ declare function isCassetteAvailable(vcrConfig: VcrConfig, cassetteName: string): boolean;
17
+
18
+ type CassetteDataType<TFilter, TBlock> = {
19
+ filter: TFilter;
20
+ messages: StreamDataResponse<TBlock>[];
21
+ };
22
+ declare function record<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, client: Client<TFilter, TBlock>, indexer: Indexer<TFilter, TBlock>, cassetteOptions: CassetteOptions): Promise<void>;
23
+
24
+ declare function replay<TFilter, TBlock, TTxnParams>(vcrConfig: VcrConfig, indexer: Indexer<TFilter, TBlock>, cassetteName: string): Promise<void>;
25
+ declare function loadCassette<TFilter, TBlock>(vcrConfig: VcrConfig, cassetteName: string): Client<TFilter, TBlock>;
26
+
27
+ export { type CassetteDataType, type CassetteOptions, type VcrConfig, deserialize, isCassetteAvailable, loadCassette, record, replay, serialize };