@apibara/protocol 2.1.0-beta.3 → 2.1.0-beta.30

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 (36) hide show
  1. package/dist/codec.cjs +241 -0
  2. package/dist/codec.d.cts +81 -0
  3. package/dist/codec.d.mts +81 -0
  4. package/dist/codec.d.ts +81 -0
  5. package/dist/codec.mjs +223 -0
  6. package/dist/index.cjs +17 -29
  7. package/dist/index.d.cts +4 -5
  8. package/dist/index.d.mts +4 -5
  9. package/dist/index.d.ts +4 -5
  10. package/dist/index.mjs +15 -20
  11. package/dist/shared/{protocol.4b1cfe2c.d.cts → protocol.21b07506.d.mts} +399 -247
  12. package/dist/shared/{protocol.4b1cfe2c.d.mts → protocol.526c6532.d.ts} +399 -247
  13. package/dist/shared/{protocol.e39e40d6.cjs → protocol.53f81a1e.cjs} +176 -177
  14. package/dist/shared/{protocol.991ff9ad.mjs → protocol.68fdd897.mjs} +175 -171
  15. package/dist/shared/{protocol.4b1cfe2c.d.ts → protocol.a5762a90.d.cts} +399 -247
  16. package/dist/testing/index.cjs +25 -38
  17. package/dist/testing/index.d.cts +107 -54
  18. package/dist/testing/index.d.mts +107 -54
  19. package/dist/testing/index.d.ts +107 -54
  20. package/dist/testing/index.mjs +25 -38
  21. package/package.json +7 -3
  22. package/src/client.ts +8 -13
  23. package/src/codec.ts +662 -0
  24. package/src/common.ts +70 -53
  25. package/src/config.ts +4 -4
  26. package/src/proto/google/protobuf/duration.ts +8 -6
  27. package/src/proto/stream.ts +38 -24
  28. package/src/status.ts +9 -16
  29. package/src/stream.ts +145 -144
  30. package/src/testing/mock.ts +36 -38
  31. package/src/common.test.ts +0 -67
  32. package/src/status.test.ts +0 -51
  33. package/src/stream.test-d.ts +0 -33
  34. package/src/stream.test.ts +0 -254
  35. package/src/testing/client.test.ts +0 -97
  36. package/src/testing/mock.test.ts +0 -35
package/src/stream.ts CHANGED
@@ -1,179 +1,180 @@
1
- import { Schema } from "@effect/schema";
2
-
1
+ import {
2
+ ArrayCodec,
3
+ BigIntCodec,
4
+ type Codec,
5
+ type CodecType,
6
+ MessageCodec,
7
+ MutableArrayCodec,
8
+ NumberCodec,
9
+ OneOfCodec,
10
+ OptionalCodec,
11
+ StringCodec,
12
+ UndefinedCodec,
13
+ } from "./codec";
3
14
  import { Cursor } from "./common";
4
15
  import * as proto from "./proto";
5
16
 
6
17
  /** Data finality. */
7
- export const DataFinality = Schema.transform(
8
- Schema.Enums(proto.stream.DataFinality),
9
- Schema.Literal("finalized", "accepted", "pending", "unknown"),
10
- {
11
- decode(value) {
12
- const enumMap = {
13
- [proto.stream.DataFinality.FINALIZED]: "finalized",
14
- [proto.stream.DataFinality.ACCEPTED]: "accepted",
15
- [proto.stream.DataFinality.PENDING]: "pending",
16
- [proto.stream.DataFinality.UNKNOWN]: "unknown",
17
- [proto.stream.DataFinality.UNRECOGNIZED]: "unknown",
18
- } as const;
19
-
20
- return enumMap[value] ?? "unknown";
21
- },
22
- encode(value) {
23
- const enumMap = {
24
- finalized: proto.stream.DataFinality.FINALIZED,
25
- accepted: proto.stream.DataFinality.ACCEPTED,
26
- pending: proto.stream.DataFinality.PENDING,
27
- unknown: proto.stream.DataFinality.UNKNOWN,
28
- };
29
-
30
- return enumMap[value] ?? proto.stream.DataFinality.UNKNOWN;
31
- },
18
+ export const DataFinality: Codec<
19
+ "finalized" | "accepted" | "pending" | "unknown",
20
+ proto.stream.DataFinality
21
+ > = {
22
+ encode(x) {
23
+ const enumMap = {
24
+ finalized: proto.stream.DataFinality.FINALIZED,
25
+ accepted: proto.stream.DataFinality.ACCEPTED,
26
+ pending: proto.stream.DataFinality.PENDING,
27
+ unknown: proto.stream.DataFinality.UNKNOWN,
28
+ };
29
+
30
+ return enumMap[x] ?? proto.stream.DataFinality.UNKNOWN;
31
+ },
32
+ decode(p) {
33
+ const enumMap = {
34
+ [proto.stream.DataFinality.FINALIZED]: "finalized",
35
+ [proto.stream.DataFinality.ACCEPTED]: "accepted",
36
+ [proto.stream.DataFinality.PENDING]: "pending",
37
+ [proto.stream.DataFinality.UNKNOWN]: "unknown",
38
+ [proto.stream.DataFinality.UNRECOGNIZED]: "unknown",
39
+ } as const;
40
+
41
+ return enumMap[p] ?? "unknown";
32
42
  },
33
- );
43
+ };
34
44
 
35
- export type DataFinality = typeof DataFinality.Type;
45
+ export type DataFinality = CodecType<typeof DataFinality>;
36
46
 
37
47
  /** Data production mode. */
38
- export const DataProduction = Schema.transform(
39
- Schema.Enums(proto.stream.DataProduction),
40
- Schema.Literal("backfill", "live", "unknown"),
41
- {
42
- decode(value) {
43
- const enumMap = {
44
- [proto.stream.DataProduction.BACKFILL]: "backfill",
45
- [proto.stream.DataProduction.LIVE]: "live",
46
- [proto.stream.DataProduction.UNKNOWN]: "unknown",
47
- [proto.stream.DataProduction.UNRECOGNIZED]: "unknown",
48
- } as const;
49
-
50
- return enumMap[value] ?? "unknown";
51
- },
52
- encode(value) {
53
- const enumMap = {
54
- backfill: proto.stream.DataProduction.BACKFILL,
55
- live: proto.stream.DataProduction.LIVE,
56
- unknown: proto.stream.DataProduction.UNKNOWN,
57
- };
58
-
59
- return enumMap[value] ?? proto.stream.DataProduction.UNKNOWN;
60
- },
48
+ export const DataProduction: Codec<
49
+ "backfill" | "live" | "unknown",
50
+ proto.stream.DataProduction
51
+ > = {
52
+ encode(x) {
53
+ switch (x) {
54
+ case "backfill":
55
+ return proto.stream.DataProduction.BACKFILL;
56
+ case "live":
57
+ return proto.stream.DataProduction.LIVE;
58
+ case "unknown":
59
+ return proto.stream.DataProduction.UNKNOWN;
60
+ default:
61
+ return proto.stream.DataProduction.UNRECOGNIZED;
62
+ }
63
+ },
64
+ decode(p) {
65
+ const enumMap = {
66
+ [proto.stream.DataProduction.BACKFILL]: "backfill",
67
+ [proto.stream.DataProduction.LIVE]: "live",
68
+ [proto.stream.DataProduction.UNKNOWN]: "unknown",
69
+ [proto.stream.DataProduction.UNRECOGNIZED]: "unknown",
70
+ } as const;
71
+
72
+ return enumMap[p] ?? "unknown";
61
73
  },
62
- );
74
+ };
63
75
 
64
- export type DataProduction = typeof DataProduction.Type;
76
+ export type DataProduction = CodecType<typeof DataProduction>;
65
77
 
66
- export const Duration = Schema.Struct({
67
- seconds: Schema.BigIntFromSelf,
68
- nanos: Schema.Number,
78
+ export const DurationCodec = MessageCodec({
79
+ seconds: BigIntCodec,
80
+ nanos: NumberCodec,
69
81
  });
70
82
 
83
+ export type Duration = CodecType<typeof DurationCodec>;
84
+
71
85
  /** Create a `StreamDataRequest` with the given filter schema. */
72
- export const StreamDataRequest = <TA, TR>(
73
- filter: Schema.Schema<TA, Uint8Array, TR>,
74
- ) =>
75
- Schema.Struct({
76
- finality: Schema.optional(DataFinality),
77
- startingCursor: Schema.optional(Cursor),
78
- filter: Schema.mutable(Schema.Array(filter)),
79
- heartbeatInterval: Schema.optional(Duration),
86
+ export const StreamDataRequest = <TA>(filter: Codec<TA, Uint8Array>) =>
87
+ MessageCodec({
88
+ finality: OptionalCodec(DataFinality),
89
+ startingCursor: OptionalCodec(Cursor),
90
+ filter: MutableArrayCodec(filter),
91
+ heartbeatInterval: OptionalCodec(DurationCodec),
80
92
  });
81
93
 
82
- export type StreamDataRequest<TA> = {
83
- finality?: DataFinality | undefined;
84
- startingCursor?: Cursor | undefined;
85
- filter: TA[];
86
- };
94
+ export type StreamDataRequest<TA> = CodecType<
95
+ ReturnType<typeof StreamDataRequest<TA>>
96
+ >;
87
97
 
88
- export const Invalidate = Schema.Struct({
89
- _tag: tag("invalidate"),
90
- invalidate: Schema.Struct({
91
- cursor: Schema.optional(Cursor),
92
- }),
98
+ export const Invalidate = MessageCodec({
99
+ cursor: OptionalCodec(Cursor),
93
100
  });
94
101
 
95
- export type Invalidate = typeof Invalidate.Type;
102
+ export type Invalidate = CodecType<typeof Invalidate>;
96
103
 
97
- export const Finalize = Schema.Struct({
98
- _tag: tag("finalize"),
99
- finalize: Schema.Struct({
100
- cursor: Schema.optional(Cursor),
101
- }),
104
+ export const Finalize = MessageCodec({
105
+ cursor: OptionalCodec(Cursor),
102
106
  });
103
107
 
104
- export type Finalize = typeof Finalize.Type;
108
+ export type Finalize = CodecType<typeof Finalize>;
105
109
 
106
- export const Heartbeat = Schema.Struct({
107
- _tag: tag("heartbeat"),
108
- });
110
+ // TODO: Double check this; This is a hack to make the heartbeat variant undefined
111
+ export const Heartbeat = UndefinedCodec;
109
112
 
110
- export type Heartbeat = typeof Heartbeat.Type;
113
+ export type Heartbeat = CodecType<typeof Heartbeat>;
111
114
 
112
- export const StdOut = Schema.Struct({
113
- _tag: tag("stdout"),
114
- stdout: Schema.String,
115
- });
115
+ export const StdOut = StringCodec;
116
116
 
117
- export type StdOut = typeof StdOut.Type;
117
+ export type StdOut = CodecType<typeof StdOut>;
118
118
 
119
- export const StdErr = Schema.Struct({
120
- _tag: tag("stderr"),
121
- stderr: Schema.String,
122
- });
119
+ export const StdErr = StringCodec;
123
120
 
124
- export type StdErr = typeof StdErr.Type;
121
+ export type StdErr = CodecType<typeof StdErr>;
125
122
 
126
- export const SystemMessage = Schema.Struct({
127
- _tag: tag("systemMessage"),
128
- systemMessage: Schema.Struct({
129
- output: Schema.Union(StdOut, StdErr),
123
+ export const SystemMessage = MessageCodec({
124
+ output: OneOfCodec({
125
+ stdout: StdOut,
126
+ stderr: StdErr,
130
127
  }),
131
128
  });
132
129
 
133
- export type SystemMessage = typeof SystemMessage.Type;
134
-
135
- export const Data = <TA, TR>(
136
- schema: Schema.Schema<TA | null, Uint8Array, TR>,
137
- ) =>
138
- Schema.Struct({
139
- _tag: tag("data"),
140
- data: Schema.Struct({
141
- cursor: Schema.optional(Cursor),
142
- endCursor: Cursor,
143
- finality: DataFinality,
144
- production: DataProduction,
145
- data: Schema.Array(schema),
146
- }),
130
+ export type SystemMessage = CodecType<typeof SystemMessage>;
131
+
132
+ const _DataOrNull = <TA>(
133
+ schema: Codec<TA, Uint8Array>,
134
+ ): Codec<TA | null, Uint8Array> => ({
135
+ encode(x) {
136
+ if (x === null) {
137
+ return new Uint8Array();
138
+ }
139
+ return schema.encode(x);
140
+ },
141
+ decode(p) {
142
+ if (p.length === 0) {
143
+ return null;
144
+ }
145
+ return schema.decode(p);
146
+ },
147
+ });
148
+
149
+ export const Data = <TA>(schema: Codec<TA | null, Uint8Array>) =>
150
+ MessageCodec({
151
+ cursor: OptionalCodec(Cursor),
152
+ endCursor: Cursor,
153
+ finality: DataFinality,
154
+ production: DataProduction,
155
+ data: ArrayCodec(_DataOrNull(schema)),
147
156
  });
148
157
 
149
- export const StreamDataResponse = <TA, TR>(
150
- data: Schema.Schema<TA | null, Uint8Array, TR>,
151
- ) => Schema.Union(Data(data), Invalidate, Finalize, Heartbeat, SystemMessage);
152
-
153
- const ResponseWithoutData = Schema.Union(
154
- Invalidate,
155
- Finalize,
156
- Heartbeat,
157
- SystemMessage,
158
- );
159
- type ResponseWithoutData = typeof ResponseWithoutData.Type;
160
-
161
- export type StreamDataResponse<TA> =
162
- | ResponseWithoutData
163
- | {
164
- _tag: "data";
165
- data: {
166
- cursor?: Cursor | undefined;
167
- endCursor: Cursor;
168
- finality: DataFinality;
169
- production: DataProduction;
170
- data: readonly (TA | null)[];
171
- };
172
- };
158
+ export type Data<TA> = CodecType<ReturnType<typeof Data<TA>>>;
159
+
160
+ export const StreamDataResponse = <TA>(schema: Codec<TA | null, Uint8Array>) =>
161
+ OneOfCodec({
162
+ data: Data(schema),
163
+ invalidate: Invalidate,
164
+ finalize: Finalize,
165
+ heartbeat: Heartbeat,
166
+ systemMessage: SystemMessage,
167
+ });
168
+
169
+ export const ResponseWithoutData = OneOfCodec({
170
+ invalidate: Invalidate,
171
+ finalize: Finalize,
172
+ heartbeat: Heartbeat,
173
+ systemMessage: SystemMessage,
174
+ });
175
+
176
+ export type ResponseWithoutData = CodecType<typeof ResponseWithoutData>;
173
177
 
174
- function tag<T extends string>(tag: T) {
175
- return Schema.Literal(tag).pipe(
176
- Schema.propertySignature,
177
- Schema.fromKey("$case"),
178
- );
179
- }
178
+ export type StreamDataResponse<TA> = CodecType<
179
+ ReturnType<typeof StreamDataResponse<TA>>
180
+ >;
@@ -1,53 +1,49 @@
1
- import { Schema } from "@effect/schema";
1
+ import {
2
+ type Codec,
3
+ type CodecType,
4
+ MessageCodec,
5
+ OptionalCodec,
6
+ StringCodec,
7
+ } from "../codec";
2
8
  import { StreamConfig } from "../config";
3
9
  import * as proto from "../proto";
4
10
  import { StreamDataResponse } from "../stream";
5
11
 
6
- export const MockFilter = Schema.Struct({
7
- filter: Schema.optional(Schema.String),
12
+ export const MockFilter = MessageCodec({
13
+ filter: OptionalCodec(StringCodec),
8
14
  });
9
15
 
10
- export type MockFilter = typeof MockFilter.Type;
16
+ export type MockFilter = CodecType<typeof MockFilter>;
11
17
 
12
- export const MockFilterFromBytes = Schema.transform(
13
- Schema.Uint8ArrayFromSelf,
14
- MockFilter,
15
- {
16
- strict: false,
17
- decode(value) {
18
- return proto.testing.MockFilter.decode(value);
19
- },
20
- encode(value) {
21
- return proto.testing.MockFilter.encode(value).finish();
22
- },
18
+ export const MockFilterFromBytes: Codec<MockFilter, Uint8Array> = {
19
+ decode(value) {
20
+ return proto.testing.MockFilter.decode(value);
23
21
  },
24
- );
22
+ encode(value) {
23
+ return proto.testing.MockFilter.encode(value).finish();
24
+ },
25
+ };
25
26
 
26
- const MockBlock = Schema.Struct({
27
- data: Schema.optional(Schema.String),
27
+ const MockBlock = MessageCodec({
28
+ data: OptionalCodec(StringCodec),
28
29
  });
29
30
 
30
- export type MockBlock = typeof MockBlock.Type;
31
+ export type MockBlock = CodecType<typeof MockBlock>;
31
32
 
32
- export const MockBlockFromBytes = Schema.transform(
33
- Schema.Uint8ArrayFromSelf,
34
- Schema.NullOr(MockBlock),
35
- {
36
- strict: false,
37
- decode(value) {
38
- if (value.length === 0) {
39
- return null;
40
- }
41
- return proto.testing.MockBlock.decode(value);
42
- },
43
- encode(value) {
44
- if (value === null) {
45
- return new Uint8Array();
46
- }
47
- return proto.testing.MockBlock.encode(value).finish();
48
- },
33
+ export const MockBlockFromBytes: Codec<MockBlock | null, Uint8Array> = {
34
+ decode(value) {
35
+ if (value.length === 0) {
36
+ return null;
37
+ }
38
+ return proto.testing.MockBlock.decode(value);
49
39
  },
50
- );
40
+ encode(value) {
41
+ if (value === null) {
42
+ return new Uint8Array();
43
+ }
44
+ return proto.testing.MockBlock.encode(value).finish();
45
+ },
46
+ };
51
47
 
52
48
  /** For testing, simply concatenate the values of `.filter` */
53
49
  function mergeMockFilter(a: MockFilter, b: MockFilter): MockFilter {
@@ -65,7 +61,9 @@ export const MockStream = new StreamConfig(
65
61
  MockFilterFromBytes,
66
62
  MockBlockFromBytes,
67
63
  mergeMockFilter,
64
+ "mock",
68
65
  );
69
66
 
70
67
  export const MockStreamResponse = StreamDataResponse(MockBlockFromBytes);
71
- export type MockStreamResponse = typeof MockStreamResponse.Type;
68
+
69
+ export type MockStreamResponse = CodecType<typeof MockStreamResponse>;
@@ -1,67 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { createCursor, cursorFromProto, cursorToProto } from "./common";
3
-
4
- describe("Cursor", () => {
5
- describe("proto", () => {
6
- it("should encode and decode (with uniqueKey)", () => {
7
- const cursor = createCursor({
8
- orderKey: 123n,
9
- uniqueKey: "0xcafecafe",
10
- });
11
-
12
- const proto = cursorToProto(cursor);
13
- expect(proto).toMatchInlineSnapshot(`
14
- {
15
- "orderKey": 123n,
16
- "uniqueKey": Uint8Array [
17
- 202,
18
- 254,
19
- 202,
20
- 254,
21
- ],
22
- }
23
- `);
24
- const back = cursorFromProto(proto);
25
- expect(back).toEqual(cursor);
26
- });
27
-
28
- it("should encode and decode (without uniqueKey)", () => {
29
- const cursor = createCursor({
30
- orderKey: 123n,
31
- });
32
-
33
- const proto = cursorToProto(cursor);
34
- expect(proto).toMatchInlineSnapshot(`
35
- {
36
- "orderKey": 123n,
37
- "uniqueKey": Uint8Array [],
38
- }
39
- `);
40
- const back = cursorFromProto(proto);
41
- expect(back).toEqual(cursor);
42
- });
43
- });
44
-
45
- describe("bytes", () => {
46
- it("should encode and decode (with uniqueKey)", () => {
47
- const cursor = createCursor({
48
- orderKey: 123n,
49
- uniqueKey: "0xcafecafe",
50
- });
51
-
52
- const proto = cursorToProto(cursor);
53
- const back = cursorFromProto(proto);
54
- expect(back).toEqual(cursor);
55
- });
56
-
57
- it("should encode and decode (without uniqueKey)", () => {
58
- const cursor = createCursor({
59
- orderKey: 123n,
60
- });
61
-
62
- const proto = cursorToProto(cursor);
63
- const back = cursorFromProto(proto);
64
- expect(back).toEqual(cursor);
65
- });
66
- });
67
- });
@@ -1,51 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
-
3
- import {
4
- StatusResponse,
5
- statusRequestFromProto,
6
- statusRequestToProto,
7
- statusResponseFromProto,
8
- statusResponseToProto,
9
- } from "./status";
10
-
11
- describe("StatusRequest", () => {
12
- describe("proto", () => {
13
- it("should encode and decode", () => {
14
- const proto = statusRequestToProto({});
15
- expect(proto).toMatchInlineSnapshot("{}");
16
- const back = statusRequestFromProto(proto);
17
- expect(back).toEqual({});
18
- });
19
- });
20
- });
21
-
22
- describe("StatusResponse", () => {
23
- describe("proto", () => {
24
- it("should encode and decode", () => {
25
- const response = StatusResponse.make({
26
- currentHead: {
27
- orderKey: 123n,
28
- },
29
- lastIngested: {
30
- orderKey: 123n,
31
- },
32
- });
33
-
34
- const proto = statusResponseToProto(response);
35
- expect(proto).toMatchInlineSnapshot(`
36
- {
37
- "currentHead": {
38
- "orderKey": 123n,
39
- "uniqueKey": Uint8Array [],
40
- },
41
- "lastIngested": {
42
- "orderKey": 123n,
43
- "uniqueKey": Uint8Array [],
44
- },
45
- }
46
- `);
47
- const back = statusResponseFromProto(proto);
48
- expect(back).toEqual(response);
49
- });
50
- });
51
- });
@@ -1,33 +0,0 @@
1
- import { Schema } from "@effect/schema";
2
- import { test } from "vitest";
3
-
4
- import { Data } from "./stream";
5
-
6
- const Inner = Schema.Struct({
7
- data: Schema.String,
8
- });
9
-
10
- const Good = Schema.transform(Schema.Uint8ArrayFromSelf, Schema.NullOr(Inner), {
11
- decode(value) {
12
- throw new Error("not implemented");
13
- },
14
- encode(value) {
15
- throw new Error("not implemented");
16
- },
17
- });
18
-
19
- const Bad = Schema.transform(Schema.Uint8ArrayFromSelf, Inner, {
20
- decode(value) {
21
- throw new Error("not implemented");
22
- },
23
- encode(value) {
24
- throw new Error("not implemented");
25
- },
26
- });
27
-
28
- test("Data", () => {
29
- const GoodData = Data(Good);
30
-
31
- // @ts-expect-error
32
- const BadData = Data(Bad);
33
- });