@apibara/protocol 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.
package/dist/index.cjs ADDED
@@ -0,0 +1,222 @@
1
+ 'use strict';
2
+
3
+ const config = require('./shared/protocol.e39e40d6.cjs');
4
+ const schema = require('@effect/schema');
5
+ const niceGrpc = require('nice-grpc');
6
+ require('protobufjs/minimal.js');
7
+ const assert = require('node:assert');
8
+ require('effect');
9
+ require('viem');
10
+ require('long');
11
+
12
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
13
+
14
+ const assert__default = /*#__PURE__*/_interopDefaultCompat(assert);
15
+
16
+ const index = {
17
+ __proto__: null,
18
+ stream: config.stream,
19
+ testing: config.testing
20
+ };
21
+
22
+ const StatusRequest = schema.Schema.Struct({});
23
+ const statusRequestToProto = schema.Schema.encodeSync(StatusRequest);
24
+ const statusRequestFromProto = schema.Schema.decodeSync(StatusRequest);
25
+ const StatusResponse = schema.Schema.Struct({
26
+ currentHead: schema.Schema.optional(config.Cursor),
27
+ lastIngested: schema.Schema.optional(config.Cursor),
28
+ finalized: schema.Schema.optional(config.Cursor),
29
+ starting: schema.Schema.optional(config.Cursor)
30
+ });
31
+ const statusResponseToProto = schema.Schema.encodeSync(StatusResponse);
32
+ const statusResponseFromProto = schema.Schema.decodeSync(StatusResponse);
33
+
34
+ var __defProp$1 = Object.defineProperty;
35
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
36
+ var __publicField$1 = (obj, key, value) => {
37
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
38
+ return value;
39
+ };
40
+ const DEFAULT_TIMEOUT_MS = 45e3;
41
+ class TimeoutError extends Error {
42
+ constructor(timeout) {
43
+ super(`No message received in ${timeout}ms`);
44
+ this.name = "TimeoutError";
45
+ }
46
+ }
47
+ function createClient(config$1, streamUrl, options = {}) {
48
+ const channel = niceGrpc.createChannel(
49
+ streamUrl,
50
+ options?.credentials,
51
+ options?.channelOptions
52
+ );
53
+ const client = niceGrpc.createClient(
54
+ config.DnaStreamDefinition,
55
+ channel,
56
+ options?.defaultCallOptions
57
+ );
58
+ return new GrpcClient(config$1, client);
59
+ }
60
+ class GrpcClient {
61
+ constructor(config, client) {
62
+ this.config = config;
63
+ this.client = client;
64
+ __publicField$1(this, "encodeRequest");
65
+ this.encodeRequest = schema.Schema.encodeSync(config.Request);
66
+ }
67
+ async status(request, options) {
68
+ const response = await this.client.status(
69
+ statusRequestToProto(request ?? {}),
70
+ options
71
+ );
72
+ return statusResponseFromProto(response);
73
+ }
74
+ streamData(request, options) {
75
+ const it = this.client.streamData(this.encodeRequest(request), options);
76
+ return new StreamDataIterable(it, this.config.Block, options);
77
+ }
78
+ }
79
+ class StreamDataIterable {
80
+ constructor(it, schema, options) {
81
+ this.it = it;
82
+ this.schema = schema;
83
+ this.options = options;
84
+ }
85
+ [Symbol.asyncIterator]() {
86
+ const inner = this.it[Symbol.asyncIterator]();
87
+ const schema$1 = config.StreamDataResponse(this.schema);
88
+ const decoder = schema.Schema.decodeSync(schema$1);
89
+ const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};
90
+ let shouldStop = false;
91
+ let clock;
92
+ return {
93
+ async next() {
94
+ if (shouldStop) {
95
+ return { done: true, value: void 0 };
96
+ }
97
+ const t = new Promise(
98
+ (_, reject) => {
99
+ clock = setTimeout(() => {
100
+ reject(new TimeoutError(timeout));
101
+ }, timeout);
102
+ }
103
+ );
104
+ try {
105
+ const { done, value } = await Promise.race([inner.next(), t]);
106
+ clearTimeout(clock);
107
+ if (done || value.message === void 0) {
108
+ return { done: true, value: void 0 };
109
+ }
110
+ const decodedMessage = decoder(value.message);
111
+ if (endingCursor) {
112
+ assert__default(value.message.$case === "data");
113
+ assert__default(decodedMessage._tag === "data");
114
+ const { orderKey, uniqueKey } = endingCursor;
115
+ const endCursor = decodedMessage.data.endCursor;
116
+ if (orderKey === endCursor?.orderKey) {
117
+ if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {
118
+ shouldStop = true;
119
+ return { done: false, value: decodedMessage };
120
+ }
121
+ }
122
+ }
123
+ return {
124
+ done: false,
125
+ value: decodedMessage
126
+ };
127
+ } finally {
128
+ clearTimeout(clock);
129
+ }
130
+ }
131
+ };
132
+ }
133
+ }
134
+
135
+ var __defProp = Object.defineProperty;
136
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
137
+ var __publicField = (obj, key, value) => {
138
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
139
+ return value;
140
+ };
141
+ class RateGauge {
142
+ constructor(intervalSeconds) {
143
+ __publicField(this, "interval");
144
+ __publicField(this, "prev");
145
+ __publicField(this, "rateMs");
146
+ __publicField(this, "var");
147
+ this.interval = intervalSeconds * 1e3;
148
+ this.var = 0;
149
+ }
150
+ record(items) {
151
+ const prev = this.prev;
152
+ const now = process.hrtime.bigint();
153
+ this.prev = now;
154
+ if (!prev) {
155
+ return;
156
+ }
157
+ const deltaMs = Number(now - prev) / 1e6;
158
+ const rateMs = items / deltaMs;
159
+ if (this.rateMs === void 0) {
160
+ this.rateMs = rateMs;
161
+ this.var = 0;
162
+ return;
163
+ }
164
+ const alpha = 1 - Math.exp(-deltaMs / this.interval);
165
+ this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;
166
+ const diff = rateMs - this.rateMs;
167
+ const incr = alpha * diff;
168
+ this.var = (1 - alpha) * (this.var + incr * diff);
169
+ }
170
+ /** Returns the average rate per second. */
171
+ average() {
172
+ if (this.rateMs === void 0) {
173
+ return void 0;
174
+ }
175
+ return this.rateMs * 1e3;
176
+ }
177
+ /** Returns the variance. */
178
+ variance() {
179
+ return this.var;
180
+ }
181
+ }
182
+
183
+ exports.Bytes = config.Bytes;
184
+ exports.BytesFromUint8Array = config.BytesFromUint8Array;
185
+ exports.Cursor = config.Cursor;
186
+ exports.CursorFromBytes = config.CursorFromBytes;
187
+ exports.Data = config.Data;
188
+ exports.DataFinality = config.DataFinality;
189
+ exports.DataProduction = config.DataProduction;
190
+ exports.DnaStreamDefinition = config.DnaStreamDefinition;
191
+ exports.Duration = config.Duration;
192
+ exports.Finalize = config.Finalize;
193
+ exports.Heartbeat = config.Heartbeat;
194
+ exports.Invalidate = config.Invalidate;
195
+ exports.StdErr = config.StdErr;
196
+ exports.StdOut = config.StdOut;
197
+ exports.StreamConfig = config.StreamConfig;
198
+ exports.StreamDataRequest = config.StreamDataRequest;
199
+ exports.StreamDataResponse = config.StreamDataResponse;
200
+ exports.SystemMessage = config.SystemMessage;
201
+ exports._Cursor = config._Cursor;
202
+ exports.createCursor = config.createCursor;
203
+ exports.cursorFromBytes = config.cursorFromBytes;
204
+ exports.cursorFromProto = config.cursorFromProto;
205
+ exports.cursorToBytes = config.cursorToBytes;
206
+ exports.cursorToProto = config.cursorToProto;
207
+ exports.isCursor = config.isCursor;
208
+ exports.normalizeCursor = config.normalizeCursor;
209
+ exports.ClientError = niceGrpc.ClientError;
210
+ exports.Status = niceGrpc.Status;
211
+ exports.GrpcClient = GrpcClient;
212
+ exports.RateGauge = RateGauge;
213
+ exports.StatusRequest = StatusRequest;
214
+ exports.StatusResponse = StatusResponse;
215
+ exports.StreamDataIterable = StreamDataIterable;
216
+ exports.TimeoutError = TimeoutError;
217
+ exports.createClient = createClient;
218
+ exports.proto = index;
219
+ exports.statusRequestFromProto = statusRequestFromProto;
220
+ exports.statusRequestToProto = statusRequestToProto;
221
+ exports.statusResponseFromProto = statusResponseFromProto;
222
+ exports.statusResponseToProto = statusResponseToProto;
@@ -0,0 +1,71 @@
1
+ import { s as stream } from './shared/protocol.4b1cfe2c.cjs';
2
+ export { B as Bytes, b as BytesFromUint8Array, J as Client, E as ClientCallOptions, K as CreateClientOptions, c as Cursor, g as CursorFromBytes, C as CursorProto, y as Data, q as DataFinality, r as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, t as Duration, F as Finalize, M as GrpcClient, H as Heartbeat, I as Invalidate, S as StatusRequest, m as StatusResponse, w as StdErr, v as StdOut, A as StreamConfig, N as StreamDataIterable, G as StreamDataOptions, u as StreamDataRequest, z as StreamDataResponse, x as SystemMessage, T as TimeoutError, _ as _Cursor, L as createClient, d as createCursor, i as cursorFromBytes, f as cursorFromProto, h as cursorToBytes, e as cursorToProto, j as isCursor, n as normalizeCursor, l as statusRequestFromProto, k as statusRequestToProto, p as statusResponseFromProto, o as statusResponseToProto } from './shared/protocol.4b1cfe2c.cjs';
3
+ import _m0 from 'protobufjs/minimal.js';
4
+ export { ClientError, Status } from 'nice-grpc';
5
+ import '@effect/schema';
6
+ import 'nice-grpc-common';
7
+ import '@effect/schema/AST';
8
+
9
+ declare const protobufPackage = "dna.v2.testing";
10
+ interface MockFilter {
11
+ readonly filter?: string | undefined;
12
+ }
13
+ declare const MockFilter: {
14
+ encode(message: MockFilter, writer?: _m0.Writer): _m0.Writer;
15
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockFilter;
16
+ fromJSON(object: any): MockFilter;
17
+ toJSON(message: MockFilter): unknown;
18
+ create(base?: DeepPartial<MockFilter>): MockFilter;
19
+ fromPartial(object: DeepPartial<MockFilter>): MockFilter;
20
+ };
21
+ interface MockBlock {
22
+ readonly data?: string | undefined;
23
+ }
24
+ declare const MockBlock: {
25
+ encode(message: MockBlock, writer?: _m0.Writer): _m0.Writer;
26
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockBlock;
27
+ fromJSON(object: any): MockBlock;
28
+ toJSON(message: MockBlock): unknown;
29
+ create(base?: DeepPartial<MockBlock>): MockBlock;
30
+ fromPartial(object: DeepPartial<MockBlock>): MockBlock;
31
+ };
32
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined;
33
+ type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
34
+ readonly $case: string;
35
+ } ? {
36
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
37
+ } & {
38
+ readonly $case: T["$case"];
39
+ } : T extends {} ? {
40
+ [K in keyof T]?: DeepPartial<T[K]>;
41
+ } : Partial<T>;
42
+
43
+ type testing_DeepPartial<T> = DeepPartial<T>;
44
+ declare const testing_MockBlock: typeof MockBlock;
45
+ declare const testing_MockFilter: typeof MockFilter;
46
+ declare const testing_protobufPackage: typeof protobufPackage;
47
+ declare namespace testing {
48
+ export { type testing_DeepPartial as DeepPartial, testing_MockBlock as MockBlock, testing_MockFilter as MockFilter, testing_protobufPackage as protobufPackage };
49
+ }
50
+
51
+ declare const index_stream: typeof stream;
52
+ declare const index_testing: typeof testing;
53
+ declare namespace index {
54
+ export { index_stream as stream, index_testing as testing };
55
+ }
56
+
57
+ /** Track data rate using high precision timers. */
58
+ declare class RateGauge {
59
+ private interval;
60
+ private prev?;
61
+ private rateMs?;
62
+ private var;
63
+ constructor(intervalSeconds: number);
64
+ record(items: number): void;
65
+ /** Returns the average rate per second. */
66
+ average(): number | undefined;
67
+ /** Returns the variance. */
68
+ variance(): number;
69
+ }
70
+
71
+ export { RateGauge, index as proto };
@@ -0,0 +1,71 @@
1
+ import { s as stream } from './shared/protocol.4b1cfe2c.mjs';
2
+ export { B as Bytes, b as BytesFromUint8Array, J as Client, E as ClientCallOptions, K as CreateClientOptions, c as Cursor, g as CursorFromBytes, C as CursorProto, y as Data, q as DataFinality, r as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, t as Duration, F as Finalize, M as GrpcClient, H as Heartbeat, I as Invalidate, S as StatusRequest, m as StatusResponse, w as StdErr, v as StdOut, A as StreamConfig, N as StreamDataIterable, G as StreamDataOptions, u as StreamDataRequest, z as StreamDataResponse, x as SystemMessage, T as TimeoutError, _ as _Cursor, L as createClient, d as createCursor, i as cursorFromBytes, f as cursorFromProto, h as cursorToBytes, e as cursorToProto, j as isCursor, n as normalizeCursor, l as statusRequestFromProto, k as statusRequestToProto, p as statusResponseFromProto, o as statusResponseToProto } from './shared/protocol.4b1cfe2c.mjs';
3
+ import _m0 from 'protobufjs/minimal.js';
4
+ export { ClientError, Status } from 'nice-grpc';
5
+ import '@effect/schema';
6
+ import 'nice-grpc-common';
7
+ import '@effect/schema/AST';
8
+
9
+ declare const protobufPackage = "dna.v2.testing";
10
+ interface MockFilter {
11
+ readonly filter?: string | undefined;
12
+ }
13
+ declare const MockFilter: {
14
+ encode(message: MockFilter, writer?: _m0.Writer): _m0.Writer;
15
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockFilter;
16
+ fromJSON(object: any): MockFilter;
17
+ toJSON(message: MockFilter): unknown;
18
+ create(base?: DeepPartial<MockFilter>): MockFilter;
19
+ fromPartial(object: DeepPartial<MockFilter>): MockFilter;
20
+ };
21
+ interface MockBlock {
22
+ readonly data?: string | undefined;
23
+ }
24
+ declare const MockBlock: {
25
+ encode(message: MockBlock, writer?: _m0.Writer): _m0.Writer;
26
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockBlock;
27
+ fromJSON(object: any): MockBlock;
28
+ toJSON(message: MockBlock): unknown;
29
+ create(base?: DeepPartial<MockBlock>): MockBlock;
30
+ fromPartial(object: DeepPartial<MockBlock>): MockBlock;
31
+ };
32
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined;
33
+ type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
34
+ readonly $case: string;
35
+ } ? {
36
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
37
+ } & {
38
+ readonly $case: T["$case"];
39
+ } : T extends {} ? {
40
+ [K in keyof T]?: DeepPartial<T[K]>;
41
+ } : Partial<T>;
42
+
43
+ type testing_DeepPartial<T> = DeepPartial<T>;
44
+ declare const testing_MockBlock: typeof MockBlock;
45
+ declare const testing_MockFilter: typeof MockFilter;
46
+ declare const testing_protobufPackage: typeof protobufPackage;
47
+ declare namespace testing {
48
+ export { type testing_DeepPartial as DeepPartial, testing_MockBlock as MockBlock, testing_MockFilter as MockFilter, testing_protobufPackage as protobufPackage };
49
+ }
50
+
51
+ declare const index_stream: typeof stream;
52
+ declare const index_testing: typeof testing;
53
+ declare namespace index {
54
+ export { index_stream as stream, index_testing as testing };
55
+ }
56
+
57
+ /** Track data rate using high precision timers. */
58
+ declare class RateGauge {
59
+ private interval;
60
+ private prev?;
61
+ private rateMs?;
62
+ private var;
63
+ constructor(intervalSeconds: number);
64
+ record(items: number): void;
65
+ /** Returns the average rate per second. */
66
+ average(): number | undefined;
67
+ /** Returns the variance. */
68
+ variance(): number;
69
+ }
70
+
71
+ export { RateGauge, index as proto };
@@ -0,0 +1,71 @@
1
+ import { s as stream } from './shared/protocol.4b1cfe2c.js';
2
+ export { B as Bytes, b as BytesFromUint8Array, J as Client, E as ClientCallOptions, K as CreateClientOptions, c as Cursor, g as CursorFromBytes, C as CursorProto, y as Data, q as DataFinality, r as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, t as Duration, F as Finalize, M as GrpcClient, H as Heartbeat, I as Invalidate, S as StatusRequest, m as StatusResponse, w as StdErr, v as StdOut, A as StreamConfig, N as StreamDataIterable, G as StreamDataOptions, u as StreamDataRequest, z as StreamDataResponse, x as SystemMessage, T as TimeoutError, _ as _Cursor, L as createClient, d as createCursor, i as cursorFromBytes, f as cursorFromProto, h as cursorToBytes, e as cursorToProto, j as isCursor, n as normalizeCursor, l as statusRequestFromProto, k as statusRequestToProto, p as statusResponseFromProto, o as statusResponseToProto } from './shared/protocol.4b1cfe2c.js';
3
+ import _m0 from 'protobufjs/minimal.js';
4
+ export { ClientError, Status } from 'nice-grpc';
5
+ import '@effect/schema';
6
+ import 'nice-grpc-common';
7
+ import '@effect/schema/AST';
8
+
9
+ declare const protobufPackage = "dna.v2.testing";
10
+ interface MockFilter {
11
+ readonly filter?: string | undefined;
12
+ }
13
+ declare const MockFilter: {
14
+ encode(message: MockFilter, writer?: _m0.Writer): _m0.Writer;
15
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockFilter;
16
+ fromJSON(object: any): MockFilter;
17
+ toJSON(message: MockFilter): unknown;
18
+ create(base?: DeepPartial<MockFilter>): MockFilter;
19
+ fromPartial(object: DeepPartial<MockFilter>): MockFilter;
20
+ };
21
+ interface MockBlock {
22
+ readonly data?: string | undefined;
23
+ }
24
+ declare const MockBlock: {
25
+ encode(message: MockBlock, writer?: _m0.Writer): _m0.Writer;
26
+ decode(input: _m0.Reader | Uint8Array, length?: number): MockBlock;
27
+ fromJSON(object: any): MockBlock;
28
+ toJSON(message: MockBlock): unknown;
29
+ create(base?: DeepPartial<MockBlock>): MockBlock;
30
+ fromPartial(object: DeepPartial<MockBlock>): MockBlock;
31
+ };
32
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | bigint | undefined;
33
+ type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {
34
+ readonly $case: string;
35
+ } ? {
36
+ [K in keyof Omit<T, "$case">]?: DeepPartial<T[K]>;
37
+ } & {
38
+ readonly $case: T["$case"];
39
+ } : T extends {} ? {
40
+ [K in keyof T]?: DeepPartial<T[K]>;
41
+ } : Partial<T>;
42
+
43
+ type testing_DeepPartial<T> = DeepPartial<T>;
44
+ declare const testing_MockBlock: typeof MockBlock;
45
+ declare const testing_MockFilter: typeof MockFilter;
46
+ declare const testing_protobufPackage: typeof protobufPackage;
47
+ declare namespace testing {
48
+ export { type testing_DeepPartial as DeepPartial, testing_MockBlock as MockBlock, testing_MockFilter as MockFilter, testing_protobufPackage as protobufPackage };
49
+ }
50
+
51
+ declare const index_stream: typeof stream;
52
+ declare const index_testing: typeof testing;
53
+ declare namespace index {
54
+ export { index_stream as stream, index_testing as testing };
55
+ }
56
+
57
+ /** Track data rate using high precision timers. */
58
+ declare class RateGauge {
59
+ private interval;
60
+ private prev?;
61
+ private rateMs?;
62
+ private var;
63
+ constructor(intervalSeconds: number);
64
+ record(items: number): void;
65
+ /** Returns the average rate per second. */
66
+ average(): number | undefined;
67
+ /** Returns the variance. */
68
+ variance(): number;
69
+ }
70
+
71
+ export { RateGauge, index as proto };
package/dist/index.mjs ADDED
@@ -0,0 +1,179 @@
1
+ import { s as stream, t as testing, C as Cursor, D as DnaStreamDefinition, S as StreamDataResponse } from './shared/protocol.991ff9ad.mjs';
2
+ export { B as Bytes, a as BytesFromUint8Array, e as CursorFromBytes, q as Data, h as DataFinality, j as DataProduction, k as Duration, F as Finalize, H as Heartbeat, I as Invalidate, o as StdErr, m as StdOut, r as StreamConfig, l as StreamDataRequest, p as SystemMessage, _ as _Cursor, c as createCursor, g as cursorFromBytes, d as cursorFromProto, f as cursorToBytes, b as cursorToProto, i as isCursor, n as normalizeCursor } from './shared/protocol.991ff9ad.mjs';
3
+ import { Schema } from '@effect/schema';
4
+ import { createChannel, createClient as createClient$1 } from 'nice-grpc';
5
+ export { ClientError, Status } from 'nice-grpc';
6
+ import 'protobufjs/minimal.js';
7
+ import assert from 'node:assert';
8
+ import 'effect';
9
+ import 'viem';
10
+ import 'long';
11
+
12
+ const index = {
13
+ __proto__: null,
14
+ stream: stream,
15
+ testing: testing
16
+ };
17
+
18
+ const StatusRequest = Schema.Struct({});
19
+ const statusRequestToProto = Schema.encodeSync(StatusRequest);
20
+ const statusRequestFromProto = Schema.decodeSync(StatusRequest);
21
+ const StatusResponse = Schema.Struct({
22
+ currentHead: Schema.optional(Cursor),
23
+ lastIngested: Schema.optional(Cursor),
24
+ finalized: Schema.optional(Cursor),
25
+ starting: Schema.optional(Cursor)
26
+ });
27
+ const statusResponseToProto = Schema.encodeSync(StatusResponse);
28
+ const statusResponseFromProto = Schema.decodeSync(StatusResponse);
29
+
30
+ var __defProp$1 = Object.defineProperty;
31
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
32
+ var __publicField$1 = (obj, key, value) => {
33
+ __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
34
+ return value;
35
+ };
36
+ const DEFAULT_TIMEOUT_MS = 45e3;
37
+ class TimeoutError extends Error {
38
+ constructor(timeout) {
39
+ super(`No message received in ${timeout}ms`);
40
+ this.name = "TimeoutError";
41
+ }
42
+ }
43
+ function createClient(config, streamUrl, options = {}) {
44
+ const channel = createChannel(
45
+ streamUrl,
46
+ options?.credentials,
47
+ options?.channelOptions
48
+ );
49
+ const client = createClient$1(
50
+ DnaStreamDefinition,
51
+ channel,
52
+ options?.defaultCallOptions
53
+ );
54
+ return new GrpcClient(config, client);
55
+ }
56
+ class GrpcClient {
57
+ constructor(config, client) {
58
+ this.config = config;
59
+ this.client = client;
60
+ __publicField$1(this, "encodeRequest");
61
+ this.encodeRequest = Schema.encodeSync(config.Request);
62
+ }
63
+ async status(request, options) {
64
+ const response = await this.client.status(
65
+ statusRequestToProto(request ?? {}),
66
+ options
67
+ );
68
+ return statusResponseFromProto(response);
69
+ }
70
+ streamData(request, options) {
71
+ const it = this.client.streamData(this.encodeRequest(request), options);
72
+ return new StreamDataIterable(it, this.config.Block, options);
73
+ }
74
+ }
75
+ class StreamDataIterable {
76
+ constructor(it, schema, options) {
77
+ this.it = it;
78
+ this.schema = schema;
79
+ this.options = options;
80
+ }
81
+ [Symbol.asyncIterator]() {
82
+ const inner = this.it[Symbol.asyncIterator]();
83
+ const schema = StreamDataResponse(this.schema);
84
+ const decoder = Schema.decodeSync(schema);
85
+ const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};
86
+ let shouldStop = false;
87
+ let clock;
88
+ return {
89
+ async next() {
90
+ if (shouldStop) {
91
+ return { done: true, value: void 0 };
92
+ }
93
+ const t = new Promise(
94
+ (_, reject) => {
95
+ clock = setTimeout(() => {
96
+ reject(new TimeoutError(timeout));
97
+ }, timeout);
98
+ }
99
+ );
100
+ try {
101
+ const { done, value } = await Promise.race([inner.next(), t]);
102
+ clearTimeout(clock);
103
+ if (done || value.message === void 0) {
104
+ return { done: true, value: void 0 };
105
+ }
106
+ const decodedMessage = decoder(value.message);
107
+ if (endingCursor) {
108
+ assert(value.message.$case === "data");
109
+ assert(decodedMessage._tag === "data");
110
+ const { orderKey, uniqueKey } = endingCursor;
111
+ const endCursor = decodedMessage.data.endCursor;
112
+ if (orderKey === endCursor?.orderKey) {
113
+ if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {
114
+ shouldStop = true;
115
+ return { done: false, value: decodedMessage };
116
+ }
117
+ }
118
+ }
119
+ return {
120
+ done: false,
121
+ value: decodedMessage
122
+ };
123
+ } finally {
124
+ clearTimeout(clock);
125
+ }
126
+ }
127
+ };
128
+ }
129
+ }
130
+
131
+ var __defProp = Object.defineProperty;
132
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
133
+ var __publicField = (obj, key, value) => {
134
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
135
+ return value;
136
+ };
137
+ class RateGauge {
138
+ constructor(intervalSeconds) {
139
+ __publicField(this, "interval");
140
+ __publicField(this, "prev");
141
+ __publicField(this, "rateMs");
142
+ __publicField(this, "var");
143
+ this.interval = intervalSeconds * 1e3;
144
+ this.var = 0;
145
+ }
146
+ record(items) {
147
+ const prev = this.prev;
148
+ const now = process.hrtime.bigint();
149
+ this.prev = now;
150
+ if (!prev) {
151
+ return;
152
+ }
153
+ const deltaMs = Number(now - prev) / 1e6;
154
+ const rateMs = items / deltaMs;
155
+ if (this.rateMs === void 0) {
156
+ this.rateMs = rateMs;
157
+ this.var = 0;
158
+ return;
159
+ }
160
+ const alpha = 1 - Math.exp(-deltaMs / this.interval);
161
+ this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;
162
+ const diff = rateMs - this.rateMs;
163
+ const incr = alpha * diff;
164
+ this.var = (1 - alpha) * (this.var + incr * diff);
165
+ }
166
+ /** Returns the average rate per second. */
167
+ average() {
168
+ if (this.rateMs === void 0) {
169
+ return void 0;
170
+ }
171
+ return this.rateMs * 1e3;
172
+ }
173
+ /** Returns the variance. */
174
+ variance() {
175
+ return this.var;
176
+ }
177
+ }
178
+
179
+ export { Cursor, DnaStreamDefinition, GrpcClient, RateGauge, StatusRequest, StatusResponse, StreamDataIterable, StreamDataResponse, TimeoutError, createClient, index as proto, statusRequestFromProto, statusRequestToProto, statusResponseFromProto, statusResponseToProto };