@subsquid/util-internal-data-source 0.0.0

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.
package/lib/index.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ import type { FiniteRange } from '@subsquid/util-internal-range';
2
+ export interface BlockRef {
3
+ number: number;
4
+ hash: string;
5
+ }
6
+ export interface StreamRequest {
7
+ from: number;
8
+ to?: number;
9
+ parentHash?: string;
10
+ }
11
+ /**
12
+ * @deprecated Use {@link StreamRequest} instead.
13
+ */
14
+ export type DataSourceStreamOptions = StreamRequest;
15
+ export interface BlockBatch<B> {
16
+ blocks: B[];
17
+ finalizedHead?: BlockRef;
18
+ }
19
+ export type BlockStream<B> = AsyncIterable<BlockBatch<B>>;
20
+ export interface DataSource<B> {
21
+ getHead(): Promise<BlockRef>;
22
+ getFinalizedHead(): Promise<BlockRef>;
23
+ getFinalizedStream(req: StreamRequest): BlockStream<B>;
24
+ getStream(req: StreamRequest): BlockStream<B>;
25
+ getBlocksCountInRange?(range: FiniteRange): number;
26
+ }
27
+ export declare class ForkException extends Error {
28
+ readonly expectedParentHash: string;
29
+ readonly previousBlocks: BlockRef[];
30
+ readonly isSqdForkException = true;
31
+ constructor(blockNumber: number, expectedParentHash: string, previousBlocks: BlockRef[]);
32
+ get name(): string;
33
+ }
34
+ export declare function isForkException(err: unknown): err is ForkException;
35
+ export declare class DataConsistencyError extends Error {
36
+ readonly isSqdDataConsistencyError = true;
37
+ }
38
+ export declare class BlockConsistencyError extends DataConsistencyError {
39
+ constructor(ref: BlockRef, errorMsg?: string);
40
+ }
41
+ export declare function isDataConsistencyError(err: unknown): err is Error;
42
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,+BAA+B,CAAA;AAG9D,MAAM,WAAW,QAAQ;IACrB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a,qBAAsB,SAAQ,oBAAoB;gBAC/C,GAAG,EAAE,QAAQ,EAAE,QAAQ,CAAC,EAAE,MAAM;CAO/C;AAGD,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,OAAO,GAAG,GAAG,IAAI,KAAK,CAEjE"}
package/lib/index.js ADDED
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BlockConsistencyError = exports.DataConsistencyError = exports.ForkException = void 0;
7
+ exports.isForkException = isForkException;
8
+ exports.isDataConsistencyError = isDataConsistencyError;
9
+ const assert_1 = __importDefault(require("assert"));
10
+ class ForkException extends Error {
11
+ constructor(blockNumber, expectedParentHash, previousBlocks) {
12
+ (0, assert_1.default)(previousBlocks.length > 0);
13
+ let last = previousBlocks[previousBlocks.length - 1];
14
+ super(`expected ${blockNumber} to have parent ${last.number}#${expectedParentHash}, but got ${last.number}#${last.hash}`);
15
+ this.expectedParentHash = expectedParentHash;
16
+ this.previousBlocks = previousBlocks;
17
+ this.isSqdForkException = true;
18
+ }
19
+ get name() {
20
+ return 'ForkException';
21
+ }
22
+ }
23
+ exports.ForkException = ForkException;
24
+ function isForkException(err) {
25
+ return err instanceof Error && !!err.isSqdForkException;
26
+ }
27
+ class DataConsistencyError extends Error {
28
+ constructor() {
29
+ super(...arguments);
30
+ this.isSqdDataConsistencyError = true;
31
+ }
32
+ }
33
+ exports.DataConsistencyError = DataConsistencyError;
34
+ class BlockConsistencyError extends DataConsistencyError {
35
+ constructor(ref, errorMsg) {
36
+ let msg = `Failed to fetch block ${ref.number}#${ref.hash}`;
37
+ if (errorMsg) {
38
+ msg += ': ' + errorMsg;
39
+ }
40
+ super(msg);
41
+ }
42
+ }
43
+ exports.BlockConsistencyError = BlockConsistencyError;
44
+ function isDataConsistencyError(err) {
45
+ return err instanceof Error && !!err.isSqdDataCon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CAAC;CACJ;AARD,sDAQC;AAGD,SAAgB,sBAAsB,CAAC,GAAY;IAC/C,OAAO,GAAG,YAAY,KAAK,IAAI,CAAC,CAAE,GAAW,CAAC,yBAAyB,CAAA;AAC3E,CAAC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@subsquid/util-internal-data-source",
3
+ "version": "0.0.0",
4
+ "description": "Definition of DataSource abstraction",
5
+ "license": "GPL-3.0-or-later",
6
+ "repository": "git@github.com:subsquid/squid-sdk.git",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "main": "lib/index.js",
11
+ "files": [
12
+ "lib",
13
+ "src"
14
+ ],
15
+ "dependencies": {
16
+ "@subsquid/util-internal-range": "^0.3.0"
17
+ },
18
+ "devDependencies": {
19
+ "@types/node": "^24.0.0",
20
+ "typescript": "5.5.4",
21
+ "vitest": "4.1.5"
22
+ },
23
+ "scripts": {
24
+ "build": "rm -rf lib && tsc",
25
+ "test": "vitest --run"
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,89 @@
1
+ import assert from 'assert'
2
+ import type {FiniteRange} from '@subsquid/util-internal-range'
3
+
4
+
5
+ export interface BlockRef {
6
+ number: number
7
+ hash: string
8
+ }
9
+
10
+
11
+ export interface StreamRequest {
12
+ from: number
13
+ to?: number
14
+ parentHash?: string
15
+ }
16
+
17
+
18
+ /**
19
+ * @deprecated Use {@link StreamRequest} instead.
20
+ */
21
+ export type DataSourceStreamOptions = StreamRequest
22
+
23
+
24
+ export interface BlockBatch<B> {
25
+ blocks: B[]
26
+ finalizedHead?: BlockRef
27
+ }
28
+
29
+
30
+ export type BlockStream<B> = AsyncIterable<BlockBatch<B>>
31
+
32
+
33
+ export interface DataSource<B> {
34
+ getHead(): Promise<BlockRef>
35
+
36
+ getFinalizedHead(): Promise<BlockRef>
37
+
38
+ // FIXME: maybe it's better to pass it as an option to `getStream`
39
+ getFinalizedStream(req: StreamRequest): BlockStream<B>
40
+
41
+ getStream(req: StreamRequest): BlockStream<B>
42
+
43
+ getBlocksCountInRange?(range: FiniteRange): number
44
+ }
45
+
46
+
47
+ export class ForkException extends Error {
48
+ readonly isSqdForkException = true
49
+
50
+ constructor(
51
+ blockNumber: number,
52
+ public readonly expectedParentHash: string,
53
+ public readonly previousBlocks: BlockRef[]
54
+ ) {
55
+ assert(previousBlocks.length > 0)
56
+ let last = previousBlocks[previousBlocks.length - 1]
57
+ super(`expected ${blockNumber} to have parent ${last.number}#${expectedParentHash}, but got ${last.number}#${last.hash}`)
58
+ }
59
+
60
+ get name(): string {
61
+ return 'ForkException'
62
+ }
63
+ }
64
+
65
+
66
+ export function isForkException(err: unknown): err is ForkException {
67
+ return err instanceof Error && !!(err as any).isSqdForkException
68
+ }
69
+
70
+
71
+ export class DataConsistencyError extends Error {
72
+ readonly isSqdDataConsistencyError = true
73
+ }
74
+
75
+
76
+ export class BlockConsistencyError extends DataConsistencyError {
77
+ constructor(ref: BlockRef, errorMsg?: string) {
78
+ let msg = `Failed to fetch block ${ref.number}#${ref.hash}`
79
+ if (errorMsg) {
80
+ msg += ': ' + errorMsg
81
+ }
82
+ super(msg)
83
+ }
84
+ }
85
+
86
+
87
+ export function isDataConsistencyError(err: unknown): err is Error {
88
+ return err instanceof Error && !!(err as any).isSqdDataConsistencyError
89
+ }