@subsquid/util-internal-data-source 0.0.1-portal-api.6f7a57

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,36 @@
1
+ import { Range } from '@subsquid/util-internal-range';
2
+ export interface BlockRef {
3
+ number: number;
4
+ hash: string;
5
+ }
6
+ export interface DataSourceStreamOptions {
7
+ range?: Range;
8
+ stopOnHead?: boolean;
9
+ parentHash?: string;
10
+ }
11
+ export interface BlockBatch<B> {
12
+ blocks: B[];
13
+ finalizedHead?: BlockRef;
14
+ }
15
+ export type DataSourceStream<B> = AsyncIterable<BlockBatch<B>>;
16
+ export interface DataSource<B> {
17
+ getHead(): Promise<BlockRef | undefined>;
18
+ getFinalizedHead(): Promise<BlockRef | undefined>;
19
+ getFinalizedStream(req: DataSourceStreamOptions): DataSourceStream<B>;
20
+ getStream(req: DataSourceStreamOptions): DataSourceStream<B>;
21
+ }
22
+ export declare class ForkException extends Error {
23
+ readonly prevBlocks: BlockRef[];
24
+ readonly isSqdForkException = true;
25
+ constructor(expectedParentHash: string, nextBlock: number, prevBlocks: BlockRef[]);
26
+ get name(): string;
27
+ }
28
+ export declare function isForkException(err: unknown): err is ForkException;
29
+ export declare class DataConsistencyError extends Error {
30
+ readonly isSqdDataConsistencyError = true;
31
+ }
32
+ export declare class BlockConsistencyError extends DataConsistencyError {
33
+ constructor(ref: BlockRef, errorMsg?: string);
34
+ }
35
+ export declare function isDataConsistencyError(err: unknown): err is Error;
36
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,EAAC,MAAM,+BAA+B,CAAA;AAGnD,MAAM,WAAW,QAAQ;IACrB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;CACf;AAGD,MAAM,WAAW,uBAAuB;I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0,0 +1,46 @@
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(expectedParentHash, nextBlock, prevBlocks) {
12
+ (0, assert_1.default)(prevBlocks.length > 0);
13
+ let last = prevBlocks[prevBlocks.length - 1];
14
+ super(`expected ${nextBlock} to have parent ${last.number}#${expectedParentHash}, but got ${last.number}#${last.hash}`);
15
+ this.prevBlocks = prevBlocks;
16
+ this.isSqdForkException = true;
17
+ }
18
+ get name() {
19
+ return 'ForkException';
20
+ }
21
+ }
22
+ exports.ForkException = ForkException;
23
+ function isForkException(err) {
24
+ return err instanceof Error && !!err.isSqdForkException;
25
+ }
26
+ class DataConsistencyError extends Error {
27
+ constructor() {
28
+ super(...arguments);
29
+ this.isSqdDataConsistencyError = true;
30
+ }
31
+ }
32
+ exports.DataConsistencyError = DataConsistencyError;
33
+ class BlockConsistencyError extends DataConsistencyError {
34
+ constructor(ref, errorMsg) {
35
+ let msg = `Failed to fetch block ${ref.number}#${ref.hash}`;
36
+ if (errorMsg) {
37
+ msg += ': ' + errorMsg;
38
+ }
39
+ super(msg);
40
+ }
41
+ }
42
+ exports.BlockConsistencyError = BlockConsistencyError;
43
+ function isDataConsistencyError(err) {
44
+ return err instanceof Error && !!err.isSqdDataConsistencyError;
45
+ }
46
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAyDA,0CAEC;AAmBD,wDAEC;AAhFD,oDAA2B;AAsC3B,MAAa,aAAc,SAAQ,KAAK;IAGpC,YACI,kBAA0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Definition of DataSource abstraction",
5
+ "license": "GPL-3.0-or-later",
6
+ "repository": "git@github.com:subsquid/squid.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": "^18.18.14",
20
+ "typescript": "5.5.4"
21
+ },
22
+ "scripts": {
23
+ "build": "rm -rf lib && tsc"
24
+ }
25
+ }
package/src/index.ts ADDED
@@ -0,0 +1,81 @@
1
+ import assert from 'assert'
2
+ import {Range} from '@subsquid/util-internal-range'
3
+
4
+
5
+ export interface BlockRef {
6
+ number: number
7
+ hash: string
8
+ }
9
+
10
+
11
+ export interface DataSourceStreamOptions {
12
+ range?: Range
13
+ stopOnHead?: boolean
14
+ parentHash?: string
15
+ }
16
+
17
+
18
+ export interface BlockBatch<B> {
19
+ blocks: B[]
20
+ finalizedHead?: BlockRef
21
+ }
22
+
23
+
24
+ export type DataSourceStream<B> = AsyncIterable<BlockBatch<B>>
25
+
26
+
27
+ export interface DataSource<B> {
28
+ getHead(): Promise<BlockRef | undefined>
29
+
30
+ getFinalizedHead(): Promise<BlockRef | undefined>
31
+
32
+ // FIXME: maybe it's better to pass it ias an option to `getStream`
33
+ getFinalizedStream(req: DataSourceStreamOptions): DataSourceStream<B>
34
+
35
+ getStream(req: DataSourceStreamOptions): DataSourceStream<B>
36
+ }
37
+
38
+
39
+ export class ForkException extends Error {
40
+ readonly isSqdForkException = true
41
+
42
+ constructor(
43
+ expectedParentHash: string,
44
+ nextBlock: number,
45
+ public readonly prevBlocks: BlockRef[]
46
+ ) {
47
+ assert(prevBlocks.length > 0)
48
+ let last = prevBlocks[prevBlocks.length - 1]
49
+ super(`expected ${nextBlock} to have parent ${last.number}#${expectedParentHash}, but got ${last.number}#${last.hash}`)
50
+ }
51
+
52
+ get name(): string {
53
+ return 'ForkException'
54
+ }
55
+ }
56
+
57
+
58
+ export function isForkException(err: unknown): err is ForkException {
59
+ return err instanceof Error && !!(err as any).isSqdForkException
60
+ }
61
+
62
+
63
+ export class DataConsistencyError extends Error {
64
+ readonly isSqdDataConsistencyError = true
65
+ }
66
+
67
+
68
+ export class BlockConsistencyError extends DataConsistencyError {
69
+ constructor(ref: BlockRef, errorMsg?: string) {
70
+ let msg = `Failed to fetch block ${ref.number}#${ref.hash}`
71
+ if (errorMsg) {
72
+ msg += ': ' + errorMsg
73
+ }
74
+ super(msg)
75
+ }
76
+ }
77
+
78
+
79
+ export function isDataConsistencyError(err: unknown): err is Error {
80
+ return err instanceof Error && !!(err as any).isSqdDataConsistencyError
81
+ }