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

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 (44) hide show
  1. package/dist/codec.cjs +242 -0
  2. package/dist/codec.cjs.map +1 -0
  3. package/dist/codec.d.cts +81 -0
  4. package/dist/codec.d.mts +81 -0
  5. package/dist/codec.d.ts +81 -0
  6. package/dist/codec.mjs +224 -0
  7. package/dist/codec.mjs.map +1 -0
  8. package/dist/index.cjs +18 -29
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.cts +4 -5
  11. package/dist/index.d.mts +4 -5
  12. package/dist/index.d.ts +4 -5
  13. package/dist/index.mjs +16 -20
  14. package/dist/index.mjs.map +1 -0
  15. package/dist/shared/{protocol.4b1cfe2c.d.cts → protocol.21b07506.d.mts} +399 -247
  16. package/dist/shared/{protocol.4b1cfe2c.d.mts → protocol.526c6532.d.ts} +399 -247
  17. package/dist/shared/{protocol.e39e40d6.cjs → protocol.53f81a1e.cjs} +177 -177
  18. package/dist/shared/protocol.53f81a1e.cjs.map +1 -0
  19. package/dist/shared/{protocol.991ff9ad.mjs → protocol.68fdd897.mjs} +176 -171
  20. package/dist/shared/protocol.68fdd897.mjs.map +1 -0
  21. package/dist/shared/{protocol.4b1cfe2c.d.ts → protocol.a5762a90.d.cts} +399 -247
  22. package/dist/testing/index.cjs +26 -38
  23. package/dist/testing/index.cjs.map +1 -0
  24. package/dist/testing/index.d.cts +107 -54
  25. package/dist/testing/index.d.mts +107 -54
  26. package/dist/testing/index.d.ts +107 -54
  27. package/dist/testing/index.mjs +26 -38
  28. package/dist/testing/index.mjs.map +1 -0
  29. package/package.json +7 -3
  30. package/src/client.ts +8 -13
  31. package/src/codec.ts +662 -0
  32. package/src/common.ts +70 -53
  33. package/src/config.ts +4 -4
  34. package/src/proto/google/protobuf/duration.ts +8 -6
  35. package/src/proto/stream.ts +38 -24
  36. package/src/status.ts +9 -16
  37. package/src/stream.ts +145 -144
  38. package/src/testing/mock.ts +36 -38
  39. package/src/common.test.ts +0 -67
  40. package/src/status.test.ts +0 -51
  41. package/src/stream.test-d.ts +0 -33
  42. package/src/stream.test.ts +0 -254
  43. package/src/testing/client.test.ts +0 -97
  44. package/src/testing/mock.test.ts +0 -35
package/src/common.ts CHANGED
@@ -1,71 +1,88 @@
1
- import { Schema } from "@effect/schema";
2
- import { Option } from "effect";
3
1
  import { hexToBytes, toHex } from "viem";
4
2
 
3
+ import type { Codec, CodecProto, CodecType } from "./codec";
5
4
  import * as proto from "./proto";
6
5
 
7
6
  /** Bytes encoded as a 0x-prefixed hex string. */
8
- export const Bytes = Schema.TemplateLiteral(
9
- Schema.Literal("0x"),
10
- Schema.String,
11
- );
7
+ export type Bytes = `0x${string}`;
12
8
 
13
- export type Bytes = typeof Bytes.Type;
14
-
15
- export const BytesFromUint8Array = Schema.requiredToOptional(
16
- Schema.Uint8ArrayFromSelf,
17
- Bytes,
18
- {
19
- decode(value) {
20
- if (value.length === 0) {
21
- return Option.none();
22
- }
23
- return Option.some(toHex(value));
24
- },
25
- encode(value) {
26
- return value.pipe(
27
- Option.map(hexToBytes),
28
- Option.getOrElse(() => new Uint8Array(0)),
29
- );
30
- },
9
+ export const BytesFromUint8Array: Codec<
10
+ `0x${string}` | undefined,
11
+ Uint8Array | undefined
12
+ > = {
13
+ decode(value) {
14
+ if (!value || value?.length === 0) {
15
+ return undefined;
16
+ }
17
+ return toHex(value);
18
+ },
19
+ encode(value) {
20
+ if (value === undefined) {
21
+ return new Uint8Array(0);
22
+ }
23
+ return hexToBytes(value);
31
24
  },
32
- );
25
+ };
33
26
 
34
- /** Represent a position in the stream. */
35
- export const _Cursor = Schema.Struct({
36
- /** The block number. */
37
- orderKey: Schema.BigIntFromSelf,
38
- /** The block hash, if any. */
39
- uniqueKey: BytesFromUint8Array,
40
- });
27
+ type _CursorApp = {
28
+ orderKey: bigint;
29
+ uniqueKey?: Bytes | undefined;
30
+ };
31
+
32
+ type _CursorProto = proto.stream.Cursor;
41
33
 
34
+ /** Represent a position in the stream. */
35
+ export const Cursor: Codec<_CursorApp, _CursorProto> = {
36
+ decode(value) {
37
+ const { orderKey, uniqueKey } = value;
38
+ return {
39
+ orderKey: orderKey ?? 0n,
40
+ uniqueKey: BytesFromUint8Array.decode(uniqueKey),
41
+ };
42
+ },
43
+ encode(value) {
44
+ const { orderKey, uniqueKey } = value;
45
+ return {
46
+ orderKey,
47
+ uniqueKey: BytesFromUint8Array.encode(uniqueKey),
48
+ };
49
+ },
50
+ };
42
51
  /** The Cursor protobuf representation. */
43
- export interface CursorProto extends Schema.Schema.Encoded<typeof _Cursor> {}
44
- export interface Cursor extends Schema.Schema.Type<typeof _Cursor> {}
45
- export const Cursor: Schema.Schema<Cursor, CursorProto> = _Cursor;
52
+ export type CursorProto = CodecProto<typeof Cursor>;
53
+ export type Cursor = CodecType<typeof Cursor>;
46
54
  export const createCursor = (props: Cursor) => props;
47
55
 
48
- export const cursorToProto = Schema.encodeSync(Cursor);
49
- export const cursorFromProto = Schema.decodeSync(Cursor);
50
-
51
- export const CursorFromBytes = Schema.transform(
52
- Schema.Uint8ArrayFromSelf,
53
- Cursor,
54
- {
55
- decode(value) {
56
- return proto.stream.Cursor.decode(value);
57
- },
58
- encode(value) {
59
- return proto.stream.Cursor.encode(value).finish();
60
- },
56
+ export const CursorFromBytes: Codec<Cursor, Uint8Array> = {
57
+ encode(value) {
58
+ const { orderKey, uniqueKey } = value;
59
+ return proto.stream.Cursor.encode({
60
+ orderKey,
61
+ uniqueKey: BytesFromUint8Array.encode(uniqueKey),
62
+ }).finish();
61
63
  },
62
- );
63
-
64
- export const cursorToBytes = Schema.encodeSync(CursorFromBytes);
65
- export const cursorFromBytes = Schema.decodeSync(CursorFromBytes);
64
+ decode(value) {
65
+ const { orderKey, uniqueKey } = proto.stream.Cursor.decode(value);
66
+ return {
67
+ orderKey: orderKey ?? 0n,
68
+ uniqueKey: BytesFromUint8Array.decode(uniqueKey),
69
+ };
70
+ },
71
+ };
66
72
 
67
73
  export function isCursor(value: unknown): value is Cursor {
68
- return Schema.is(Cursor)(value);
74
+ if (typeof value !== "object" || value === null) {
75
+ return false;
76
+ }
77
+
78
+ const { orderKey, uniqueKey } = value as Cursor;
79
+
80
+ return (
81
+ typeof orderKey === "bigint" &&
82
+ (uniqueKey === null ||
83
+ uniqueKey === undefined ||
84
+ (typeof uniqueKey === "string" && uniqueKey.startsWith("0x")))
85
+ );
69
86
  }
70
87
 
71
88
  /** Normalize a cursor.
package/src/config.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { Schema } from "@effect/schema";
2
-
1
+ import type { Codec } from "./codec";
3
2
  import { StreamDataRequest, StreamDataResponse } from "./stream";
4
3
 
5
4
  /** Configure a DNA stream. */
@@ -8,9 +7,10 @@ export class StreamConfig<TFilter, TBlock> {
8
7
  private response;
9
8
 
10
9
  constructor(
11
- private filter: Schema.Schema<TFilter, Uint8Array, never>,
12
- private block: Schema.Schema<TBlock | null, Uint8Array, never>,
10
+ private filter: Codec<TFilter, Uint8Array>,
11
+ private block: Codec<TBlock | null, Uint8Array>,
13
12
  public mergeFilter: (a: TFilter, b: TFilter) => TFilter,
13
+ public name: string,
14
14
  ) {
15
15
  this.request = StreamDataRequest(this.filter);
16
16
  this.response = StreamDataResponse(this.block);
@@ -76,7 +76,9 @@ export interface Duration {
76
76
  * to +315,576,000,000 inclusive. Note: these bounds are computed from:
77
77
  * 60 sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years
78
78
  */
79
- readonly seconds: bigint;
79
+ readonly seconds?:
80
+ | bigint
81
+ | undefined;
80
82
  /**
81
83
  * Signed fractions of a second at nanosecond resolution of the span
82
84
  * of time. Durations less than one second are represented with a 0
@@ -85,7 +87,7 @@ export interface Duration {
85
87
  * of the same sign as the `seconds` field. Must be from -999,999,999
86
88
  * to +999,999,999 inclusive.
87
89
  */
88
- readonly nanos: number;
90
+ readonly nanos?: number | undefined;
89
91
  }
90
92
 
91
93
  function createBaseDuration(): Duration {
@@ -94,13 +96,13 @@ function createBaseDuration(): Duration {
94
96
 
95
97
  export const Duration = {
96
98
  encode(message: Duration, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
97
- if (message.seconds !== BigInt("0")) {
99
+ if (message.seconds !== undefined && message.seconds !== BigInt("0")) {
98
100
  if (BigInt.asIntN(64, message.seconds) !== message.seconds) {
99
101
  throw new globalThis.Error("value provided for field message.seconds of type int64 too large");
100
102
  }
101
103
  writer.uint32(8).int64(message.seconds.toString());
102
104
  }
103
- if (message.nanos !== 0) {
105
+ if (message.nanos !== undefined && message.nanos !== 0) {
104
106
  writer.uint32(16).int32(message.nanos);
105
107
  }
106
108
  return writer;
@@ -145,10 +147,10 @@ export const Duration = {
145
147
 
146
148
  toJSON(message: Duration): unknown {
147
149
  const obj: any = {};
148
- if (message.seconds !== BigInt("0")) {
150
+ if (message.seconds !== undefined && message.seconds !== BigInt("0")) {
149
151
  obj.seconds = message.seconds.toString();
150
152
  }
151
- if (message.nanos !== 0) {
153
+ if (message.nanos !== undefined && message.nanos !== 0) {
152
154
  obj.nanos = Math.round(message.nanos);
153
155
  }
154
156
  return obj;
@@ -112,13 +112,15 @@ export interface Cursor {
112
112
  *
113
113
  * This is usually the block or slot number.
114
114
  */
115
- readonly orderKey: bigint;
115
+ readonly orderKey?:
116
+ | bigint
117
+ | undefined;
116
118
  /**
117
119
  * Key used to discriminate branches in the stream.
118
120
  *
119
121
  * This is usually the hash of the block.
120
122
  */
121
- readonly uniqueKey: Uint8Array;
123
+ readonly uniqueKey?: Uint8Array | undefined;
122
124
  }
123
125
 
124
126
  /** Request for the `Status` method. */
@@ -163,7 +165,9 @@ export interface StreamDataRequest {
163
165
  | DataFinality
164
166
  | undefined;
165
167
  /** Filters used to generate data. */
166
- readonly filter: readonly Uint8Array[];
168
+ readonly filter?:
169
+ | readonly Uint8Array[]
170
+ | undefined;
167
171
  /**
168
172
  * Heartbeat interval.
169
173
  *
@@ -195,7 +199,7 @@ export interface Invalidate {
195
199
  | Cursor
196
200
  | undefined;
197
201
  /** List of blocks that were removed from the chain. */
198
- readonly removed: readonly Cursor[];
202
+ readonly removed?: readonly Cursor[] | undefined;
199
203
  }
200
204
 
201
205
  /** Move the finalized block forward. */
@@ -227,15 +231,19 @@ export interface Data {
227
231
  | Cursor
228
232
  | undefined;
229
233
  /** The finality status of the block. */
230
- readonly finality: DataFinality;
234
+ readonly finality?:
235
+ | DataFinality
236
+ | undefined;
231
237
  /**
232
238
  * The block data.
233
239
  *
234
240
  * This message contains chain-specific data serialized using protobuf.
235
241
  */
236
- readonly data: readonly Uint8Array[];
242
+ readonly data?:
243
+ | readonly Uint8Array[]
244
+ | undefined;
237
245
  /** The production mode of the block. */
238
- readonly production: DataProduction;
246
+ readonly production?: DataProduction | undefined;
239
247
  }
240
248
 
241
249
  /** Sent to clients to check if stream is still connected. */
@@ -256,13 +264,13 @@ function createBaseCursor(): Cursor {
256
264
 
257
265
  export const Cursor = {
258
266
  encode(message: Cursor, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
259
- if (message.orderKey !== BigInt("0")) {
267
+ if (message.orderKey !== undefined && message.orderKey !== BigInt("0")) {
260
268
  if (BigInt.asUintN(64, message.orderKey) !== message.orderKey) {
261
269
  throw new globalThis.Error("value provided for field message.orderKey of type uint64 too large");
262
270
  }
263
271
  writer.uint32(8).uint64(message.orderKey.toString());
264
272
  }
265
- if (message.uniqueKey.length !== 0) {
273
+ if (message.uniqueKey !== undefined && message.uniqueKey.length !== 0) {
266
274
  writer.uint32(18).bytes(message.uniqueKey);
267
275
  }
268
276
  return writer;
@@ -307,10 +315,10 @@ export const Cursor = {
307
315
 
308
316
  toJSON(message: Cursor): unknown {
309
317
  const obj: any = {};
310
- if (message.orderKey !== BigInt("0")) {
318
+ if (message.orderKey !== undefined && message.orderKey !== BigInt("0")) {
311
319
  obj.orderKey = message.orderKey.toString();
312
320
  }
313
- if (message.uniqueKey.length !== 0) {
321
+ if (message.uniqueKey !== undefined && message.uniqueKey.length !== 0) {
314
322
  obj.uniqueKey = base64FromBytes(message.uniqueKey);
315
323
  }
316
324
  return obj;
@@ -494,8 +502,10 @@ export const StreamDataRequest = {
494
502
  if (message.finality !== undefined) {
495
503
  writer.uint32(16).int32(message.finality);
496
504
  }
497
- for (const v of message.filter) {
498
- writer.uint32(26).bytes(v!);
505
+ if (message.filter !== undefined && message.filter.length !== 0) {
506
+ for (const v of message.filter) {
507
+ writer.uint32(26).bytes(v!);
508
+ }
499
509
  }
500
510
  if (message.heartbeatInterval !== undefined) {
501
511
  Duration.encode(message.heartbeatInterval, writer.uint32(34).fork()).ldelim();
@@ -529,7 +539,7 @@ export const StreamDataRequest = {
529
539
  break;
530
540
  }
531
541
 
532
- message.filter.push(reader.bytes());
542
+ message.filter!.push(reader.bytes());
533
543
  continue;
534
544
  case 4:
535
545
  if (tag !== 34) {
@@ -755,8 +765,10 @@ export const Invalidate = {
755
765
  if (message.cursor !== undefined) {
756
766
  Cursor.encode(message.cursor, writer.uint32(10).fork()).ldelim();
757
767
  }
758
- for (const v of message.removed) {
759
- Cursor.encode(v!, writer.uint32(18).fork()).ldelim();
768
+ if (message.removed !== undefined && message.removed.length !== 0) {
769
+ for (const v of message.removed) {
770
+ Cursor.encode(v!, writer.uint32(18).fork()).ldelim();
771
+ }
760
772
  }
761
773
  return writer;
762
774
  },
@@ -780,7 +792,7 @@ export const Invalidate = {
780
792
  break;
781
793
  }
782
794
 
783
- message.removed.push(Cursor.decode(reader, reader.uint32()));
795
+ message.removed!.push(Cursor.decode(reader, reader.uint32()));
784
796
  continue;
785
797
  }
786
798
  if ((tag & 7) === 4 || tag === 0) {
@@ -893,13 +905,15 @@ export const Data = {
893
905
  if (message.endCursor !== undefined) {
894
906
  Cursor.encode(message.endCursor, writer.uint32(18).fork()).ldelim();
895
907
  }
896
- if (message.finality !== 0) {
908
+ if (message.finality !== undefined && message.finality !== 0) {
897
909
  writer.uint32(24).int32(message.finality);
898
910
  }
899
- for (const v of message.data) {
900
- writer.uint32(34).bytes(v!);
911
+ if (message.data !== undefined && message.data.length !== 0) {
912
+ for (const v of message.data) {
913
+ writer.uint32(34).bytes(v!);
914
+ }
901
915
  }
902
- if (message.production !== 0) {
916
+ if (message.production !== undefined && message.production !== 0) {
903
917
  writer.uint32(40).int32(message.production);
904
918
  }
905
919
  return writer;
@@ -938,7 +952,7 @@ export const Data = {
938
952
  break;
939
953
  }
940
954
 
941
- message.data.push(reader.bytes());
955
+ message.data!.push(reader.bytes());
942
956
  continue;
943
957
  case 5:
944
958
  if (tag !== 40) {
@@ -974,13 +988,13 @@ export const Data = {
974
988
  if (message.endCursor !== undefined) {
975
989
  obj.endCursor = Cursor.toJSON(message.endCursor);
976
990
  }
977
- if (message.finality !== 0) {
991
+ if (message.finality !== undefined && message.finality !== 0) {
978
992
  obj.finality = dataFinalityToJSON(message.finality);
979
993
  }
980
994
  if (message.data?.length) {
981
995
  obj.data = message.data.map((e) => base64FromBytes(e));
982
996
  }
983
- if (message.production !== 0) {
997
+ if (message.production !== undefined && message.production !== 0) {
984
998
  obj.production = dataProductionToJSON(message.production);
985
999
  }
986
1000
  return obj;
package/src/status.ts CHANGED
@@ -1,24 +1,17 @@
1
- import { Schema } from "@effect/schema";
2
-
1
+ import { type CodecType, MessageCodec, OptionalCodec } from "./codec";
3
2
  import { Cursor } from "./common";
4
3
 
5
4
  /** The request to the `status` endpoint. */
6
- export const StatusRequest = Schema.Struct({});
7
-
8
- export type StatusRequest = typeof StatusRequest.Type;
5
+ export const StatusRequest = MessageCodec({});
9
6
 
10
- export const statusRequestToProto = Schema.encodeSync(StatusRequest);
11
- export const statusRequestFromProto = Schema.decodeSync(StatusRequest);
7
+ export type StatusRequest = CodecType<typeof StatusRequest>;
12
8
 
13
9
  /** The response from the `status` endpoint. */
14
- export const StatusResponse = Schema.Struct({
15
- currentHead: Schema.optional(Cursor),
16
- lastIngested: Schema.optional(Cursor),
17
- finalized: Schema.optional(Cursor),
18
- starting: Schema.optional(Cursor),
10
+ export const StatusResponse = MessageCodec({
11
+ currentHead: OptionalCodec(Cursor),
12
+ lastIngested: OptionalCodec(Cursor),
13
+ finalized: OptionalCodec(Cursor),
14
+ starting: OptionalCodec(Cursor),
19
15
  });
20
16
 
21
- export type StatusResponse = typeof StatusResponse.Type;
22
-
23
- export const statusResponseToProto = Schema.encodeSync(StatusResponse);
24
- export const statusResponseFromProto = Schema.decodeSync(StatusResponse);
17
+ export type StatusResponse = CodecType<typeof StatusResponse>;