@apibara/protocol 2.1.0-beta.37 → 2.1.0-beta.39

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 CHANGED
@@ -2,15 +2,17 @@
2
2
 
3
3
  const config = require('./shared/protocol.53f81a1e.cjs');
4
4
  const codec = require('./codec.cjs');
5
+ const assert = require('node:assert');
6
+ const consola = require('consola');
5
7
  const niceGrpc = require('nice-grpc');
6
8
  require('protobufjs/minimal.js');
7
- const assert = require('node:assert');
8
9
  require('viem');
9
10
  require('long');
10
11
 
11
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
13
 
13
14
  const assert__default = /*#__PURE__*/_interopDefaultCompat(assert);
15
+ const consola__default = /*#__PURE__*/_interopDefaultCompat(consola);
14
16
 
15
17
  const index = {
16
18
  __proto__: null,
@@ -52,6 +54,27 @@ function createClient(config$1, streamUrl, options = {}) {
52
54
  );
53
55
  return new GrpcClient(config$1, client);
54
56
  }
57
+ function createAuthenticatedClient(config, streamUrl, options) {
58
+ const dnaToken = process.env.DNA_TOKEN;
59
+ if (!dnaToken) {
60
+ consola__default.warn(
61
+ "DNA_TOKEN environment variable is not set. Trying to connect without authentication."
62
+ );
63
+ }
64
+ return createClient(config, streamUrl, {
65
+ ...options,
66
+ defaultCallOptions: {
67
+ ...options?.defaultCallOptions ?? {},
68
+ "*": {
69
+ metadata: niceGrpc.Metadata({
70
+ Authorization: `Bearer ${dnaToken}`
71
+ }),
72
+ // metadata cant be overrided with spread as its a class so we override it fully if user provided it.
73
+ ...options?.defaultCallOptions?.["*"] ?? {}
74
+ }
75
+ }
76
+ });
77
+ }
55
78
  class GrpcClient {
56
79
  constructor(config, client) {
57
80
  this.config = config;
@@ -206,6 +229,7 @@ exports.StatusRequest = StatusRequest;
206
229
  exports.StatusResponse = StatusResponse;
207
230
  exports.StreamDataIterable = StreamDataIterable;
208
231
  exports.TimeoutError = TimeoutError;
232
+ exports.createAuthenticatedClient = createAuthenticatedClient;
209
233
  exports.createClient = createClient;
210
234
  exports.proto = index;
211
235
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/status.ts","../src/client.ts","../src/rate.ts"],"sourcesContent":["import { type CodecType, MessageCodec, OptionalCodec } from \"./codec\";\nimport { Cursor } from \"./common\";\n\n/** The request to the `status` endpoint. */\nexport const StatusRequest = MessageCodec({});\n\nexport type StatusRequest = CodecType<typeof StatusRequest>;\n\n/** The response from the `status` endpoint. */\nexport const StatusResponse = MessageCodec({\n currentHead: OptionalCodec(Cursor),\n lastIngested: OptionalCodec(Cursor),\n finalized: OptionalCodec(Cursor),\n starting: OptionalCodec(Cursor),\n});\n\nexport type StatusResponse = CodecType<typeof StatusResponse>;\n","import {\n type ChannelCredentials,\n type ChannelOptions,\n type DefaultCallOptions,\n type NormalizedServiceDefinition,\n createChannel,\n createClient as grpcCreateClient,\n} from \"nice-grpc\";\n\nimport * as proto from \"./proto\";\n\nimport assert from \"node:assert\";\nimport type { Codec } from \"./codec\";\nimport type { Cursor } from \"./common\";\nimport type { StreamConfig } from \"./config\";\nimport { StatusRequest, StatusResponse } from \"./status\";\nimport { type StreamDataRequest, StreamDataResponse } from \"./stream\";\n\nexport { ClientError, ServerError, Status, Metadata } from \"nice-grpc\";\n\nconst DEFAULT_TIMEOUT_MS = 45_000;\n\nexport class TimeoutError extends Error {\n constructor(timeout: number) {\n super(`No message received in ${timeout}ms`);\n this.name = \"TimeoutError\";\n }\n}\n\n/** Client call options. */\nexport interface ClientCallOptions {\n signal?: AbortSignal;\n}\n\nexport interface StreamDataOptions extends ClientCallOptions {\n /** Stop at the specified cursor (inclusive). */\n endingCursor?: Cursor;\n /** Timeout between messages, in milliseconds. */\n timeout?: number;\n}\n\n/** DNA client. */\nexport interface Client<TFilter, TBlock> {\n /** Fetch the DNA stream status. */\n status(\n request?: StatusRequest,\n options?: ClientCallOptions,\n ): Promise<StatusResponse>;\n\n /** Start streaming data from the DNA server. */\n streamData(\n request: StreamDataRequest<TFilter>,\n options?: StreamDataOptions,\n ): AsyncIterable<StreamDataResponse<TBlock>>;\n}\n\nexport type CreateClientOptions = {\n defaultCallOptions?: DefaultCallOptions<\n NormalizedServiceDefinition<proto.stream.DnaStreamDefinition>\n >;\n credentials?: ChannelCredentials;\n channelOptions?: ChannelOptions;\n};\n\n/** Create a client connecting to the DNA grpc service. */\nexport function createClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options: CreateClientOptions = {},\n) {\n const channel = createChannel(\n streamUrl,\n options?.credentials,\n options?.channelOptions,\n );\n\n const client: proto.stream.DnaStreamClient = grpcCreateClient(\n proto.stream.DnaStreamDefinition,\n channel,\n options?.defaultCallOptions,\n );\n\n return new GrpcClient(config, client);\n}\n\nexport class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {\n private encodeRequest;\n\n constructor(\n private config: StreamConfig<TFilter, TBlock>,\n private client: proto.stream.DnaStreamClient,\n ) {\n this.encodeRequest = config.Request.encode;\n }\n\n async status(request?: StatusRequest, options?: ClientCallOptions) {\n const response = await this.client.status(\n StatusRequest.encode(request ?? {}),\n options,\n );\n return StatusResponse.decode(response);\n }\n\n streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions) {\n const it = this.client.streamData(this.encodeRequest(request), options);\n return new StreamDataIterable(it, this.config.Block, options);\n }\n}\n\nexport class StreamDataIterable<TBlock> {\n constructor(\n private it: AsyncIterable<proto.stream.StreamDataResponse>,\n private schema: Codec<TBlock, Uint8Array>,\n private options?: StreamDataOptions,\n ) {}\n\n [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> {\n const inner = this.it[Symbol.asyncIterator]();\n const schema = StreamDataResponse(this.schema);\n const decoder = schema.decode;\n const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};\n let shouldStop = false;\n\n let clock: string | number | NodeJS.Timeout | undefined;\n\n return {\n async next() {\n if (shouldStop) {\n return { done: true, value: undefined };\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: any is ok\n const t: Promise<{ done: boolean; value: any }> = new Promise(\n (_, reject) => {\n clock = setTimeout(() => {\n reject(new TimeoutError(timeout));\n }, timeout);\n },\n );\n\n try {\n const { done, value } = await Promise.race([inner.next(), t]);\n\n clearTimeout(clock);\n\n if (done || value.message === undefined) {\n return { done: true, value: undefined };\n }\n\n const decodedMessage = decoder(value.message);\n\n if (endingCursor) {\n assert(value.message.$case === \"data\");\n assert(decodedMessage._tag === \"data\");\n\n const { orderKey, uniqueKey } = endingCursor;\n const endCursor = decodedMessage.data.endCursor;\n\n // Check if the orderKey matches\n if (orderKey === endCursor?.orderKey) {\n // If a uniqueKey is specified, it must also match\n if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {\n shouldStop = true;\n return { done: false, value: decodedMessage };\n }\n }\n }\n\n return {\n done: false,\n value: decodedMessage,\n };\n } finally {\n clearTimeout(clock);\n }\n },\n };\n }\n}\n","/** Track data rate using high precision timers. */\nexport class RateGauge {\n private interval: number;\n private prev?: bigint;\n private rateMs?: number;\n private var: number;\n\n constructor(intervalSeconds: number) {\n // Convert seconds to milliseconds.\n this.interval = intervalSeconds * 1_000;\n this.var = 0;\n }\n\n public record(items: number) {\n // Compute the exponential moving average of the rate.\n const prev = this.prev;\n const now = process.hrtime.bigint();\n this.prev = now;\n\n if (!prev) {\n return;\n }\n\n const deltaMs = Number(now - prev) / 1_000_000;\n // rate in items/ms.\n const rateMs = items / deltaMs;\n\n if (this.rateMs === undefined) {\n this.rateMs = rateMs;\n this.var = 0;\n return;\n }\n\n const alpha = 1 - Math.exp(-deltaMs / this.interval);\n this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;\n\n const diff = rateMs - this.rateMs;\n const incr = alpha * diff;\n this.var = (1 - alpha) * (this.var + incr * diff);\n }\n\n /** Returns the average rate per second. */\n public average() {\n if (this.rateMs === undefined) {\n return undefined;\n }\n return this.rateMs * 1_000;\n }\n\n /** Returns the variance. */\n public variance() {\n return this.var;\n }\n}\n"],"names":["MessageCodec","OptionalCodec","Cursor","config","createChannel","grpcCreateClient","proto.stream.DnaStreamDefinition","__publicField","StreamDataResponse","assert"],"mappings":";;;;;;;;;;;;;;;;;;;;AAIa,MAAA,aAAA,GAAgBA,kBAAa,CAAA,EAAE,EAAA;AAKrC,MAAM,iBAAiBA,kBAAa,CAAA;AAAA,EACzC,WAAA,EAAaC,oBAAcC,aAAM,CAAA;AAAA,EACjC,YAAA,EAAcD,oBAAcC,aAAM,CAAA;AAAA,EAClC,SAAA,EAAWD,oBAAcC,aAAM,CAAA;AAAA,EAC/B,QAAA,EAAUD,oBAAcC,aAAM,CAAA;AAChC,CAAC;;;;;;;;ACMD,MAAM,kBAAqB,GAAA,IAAA,CAAA;AAEpB,MAAM,qBAAqB,KAAM,CAAA;AAAA,EACtC,YAAY,OAAiB,EAAA;AAC3B,IAAM,KAAA,CAAA,CAAA,uBAAA,EAA0B,OAAO,CAAI,EAAA,CAAA,CAAA,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAO,GAAA,cAAA,CAAA;AAAA,GACd;AACF,CAAA;AAsCO,SAAS,YACd,CAAAC,QAAA,EACA,SACA,EAAA,OAAA,GAA+B,EAC/B,EAAA;AACA,EAAA,MAAM,OAAU,GAAAC,sBAAA;AAAA,IACd,SAAA;AAAA,IACA,OAAS,EAAA,WAAA;AAAA,IACT,OAAS,EAAA,cAAA;AAAA,GACX,CAAA;AAEA,EAAA,MAAM,MAAuC,GAAAC,qBAAA;AAAA,IAC3CC,0BAAa;AAAA,IACb,OAAA;AAAA,IACA,OAAS,EAAA,kBAAA;AAAA,GACX,CAAA;AAEA,EAAO,OAAA,IAAI,UAAW,CAAAH,QAAA,EAAQ,MAAM,CAAA,CAAA;AACtC,CAAA;AAEO,MAAM,UAA+D,CAAA;AAAA,EAG1E,WAAA,CACU,QACA,MACR,EAAA;AAFQ,IAAA,IAAA,C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CAAC,IAAM,EAAA;AACT,MAAA,OAAA;AAAA,KACF;AAEA,IAAA,MAAM,OAAU,GAAA,MAAA,CAAO,GAAM,GAAA,IAAI,CAAI,GAAA,GAAA,CAAA;AAErC,IAAA,MAAM,SAAS,KAAQ,GAAA,OAAA,CAAA;AAEvB,IAAI,IAAA,IAAA,CAAK,WAAW,KAAW,CAAA,EAAA;AAC7B,MAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AACd,MAAA,IAAA,CAAK,GAAM,GAAA,CAAA,CAAA;AACX,MAAA,OAAA;AAAA,KACF;AAEA,IAAA,MAAM,QAAQ,CAAI,GAAA,IAAA,CAAK,IAAI,CAAC,OAAA,GAAU,KAAK,QAAQ,CAAA,CAAA;AACnD,IAAA,IAAA,CAAK,MAAS,GAAA,KAAA,GAAQ,MAAU,GAAA,CAAA,CAAA,GAAI,SAAS,IAAK,CAAA,MAAA,CAAA;AAElD,IAAM,MAAA,IAAA,GAAO,SAAS,IAAK,CAAA,MAAA,CAAA;AAC3B,IAAA,MAAM,OAAO,KAAQ,GAAA,IAAA,CAAA;AACrB,IAAA,IAAA,CAAK,GAAO,GAAA,CAAA,CAAA,GAAI,KAAU,KAAA,IAAA,CAAK,MAAM,IAAO,GAAA,IAAA,CAAA,CAAA;AAAA,GAC9C;AAAA;AAAA,EAGO,OAAU,GAAA;AACf,IAAI,IAAA,IAAA,CAAK,WAAW,KAAW,CAAA,EAAA;AAC7B,MAAO,OAAA,KAAA,CAAA,CAAA;AAAA,KACT;AACA,IAAA,OAAO,KAAK,MAAS,GAAA,GAAA,CAAA;AAAA,GACvB;AAAA;AAAA,EAGO,QAAW,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,GAAA,CAAA;AAAA,GACd;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/status.ts","../src/client.ts","../src/rate.ts"],"sourcesContent":["import { type CodecType, MessageCodec, OptionalCodec } from \"./codec\";\nimport { Cursor } from \"./common\";\n\n/** The request to the `status` endpoint. */\nexport const StatusRequest = MessageCodec({});\n\nexport type StatusRequest = CodecType<typeof StatusRequest>;\n\n/** The response from the `status` endpoint. */\nexport const StatusResponse = MessageCodec({\n currentHead: OptionalCodec(Cursor),\n lastIngested: OptionalCodec(Cursor),\n finalized: OptionalCodec(Cursor),\n starting: OptionalCodec(Cursor),\n});\n\nexport type StatusResponse = CodecType<typeof StatusResponse>;\n","import assert from \"node:assert\";\n\nimport consola from \"consola\";\nimport {\n type ChannelCredentials,\n type ChannelOptions,\n type DefaultCallOptions,\n Metadata,\n type NormalizedServiceDefinition,\n createChannel,\n createClient as grpcCreateClient,\n} from \"nice-grpc\";\n\nimport * as proto from \"./proto\";\n\nimport type { Codec } from \"./codec\";\nimport type { Cursor } from \"./common\";\nimport type { StreamConfig } from \"./config\";\nimport { StatusRequest, StatusResponse } from \"./status\";\nimport { type StreamDataRequest, StreamDataResponse } from \"./stream\";\n\nexport { ClientError, ServerError, Status, Metadata } from \"nice-grpc\";\n\nconst DEFAULT_TIMEOUT_MS = 45_000;\n\nexport class TimeoutError extends Error {\n constructor(timeout: number) {\n super(`No message received in ${timeout}ms`);\n this.name = \"TimeoutError\";\n }\n}\n\n/** Client call options. */\nexport interface ClientCallOptions {\n signal?: AbortSignal;\n}\n\nexport interface StreamDataOptions extends ClientCallOptions {\n /** Stop at the specified cursor (inclusive). */\n endingCursor?: Cursor;\n /** Timeout between messages, in milliseconds. */\n timeout?: number;\n}\n\n/** DNA client. */\nexport interface Client<TFilter, TBlock> {\n /** Fetch the DNA stream status. */\n status(\n request?: StatusRequest,\n options?: ClientCallOptions,\n ): Promise<StatusResponse>;\n\n /** Start streaming data from the DNA server. */\n streamData(\n request: StreamDataRequest<TFilter>,\n options?: StreamDataOptions,\n ): AsyncIterable<StreamDataResponse<TBlock>>;\n}\n\nexport type CreateClientOptions = {\n defaultCallOptions?: DefaultCallOptions<\n NormalizedServiceDefinition<proto.stream.DnaStreamDefinition>\n >;\n credentials?: ChannelCredentials;\n channelOptions?: ChannelOptions;\n};\n\n/** Create a client connecting to the DNA grpc service. */\nexport function createClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options: CreateClientOptions = {},\n) {\n const channel = createChannel(\n streamUrl,\n options?.credentials,\n options?.channelOptions,\n );\n\n const client: proto.stream.DnaStreamClient = grpcCreateClient(\n proto.stream.DnaStreamDefinition,\n channel,\n options?.defaultCallOptions,\n );\n\n return new GrpcClient(config, client);\n}\n\nexport function createAuthenticatedClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options?: CreateClientOptions,\n) {\n const dnaToken = process.env.DNA_TOKEN;\n if (!dnaToken) {\n consola.warn(\n \"DNA_TOKEN environment variable is not set. Trying to connect without authentication.\",\n );\n }\n\n return createClient(config, streamUrl, {\n ...options,\n defaultCallOptions: {\n ...(options?.defaultCallOptions ?? {}),\n \"*\": {\n metadata: Metadata({\n Authorization: `Bearer ${dnaToken}`,\n }),\n // metadata cant be overrided with spread as its a class so we override it fully if user provided it.\n ...(options?.defaultCallOptions?.[\"*\"] ?? {}),\n },\n },\n });\n}\n\nexport class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {\n private encodeRequest;\n\n constructor(\n private config: StreamConfig<TFilter, TBlock>,\n private client: proto.stream.DnaStreamClient,\n ) {\n this.encodeRequest = config.Request.encode;\n }\n\n async status(request?: StatusRequest, options?: ClientCallOptions) {\n const response = await this.client.status(\n StatusRequest.encode(request ?? {}),\n options,\n );\n return StatusResponse.decode(response);\n }\n\n streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions) {\n const it = this.client.streamData(this.encodeRequest(request), options);\n return new StreamDataIterable(it, this.config.Block, options);\n }\n}\n\nexport class StreamDataIterable<TBlock> {\n constructor(\n private it: AsyncIterable<proto.stream.StreamDataResponse>,\n private schema: Codec<TBlock, Uint8Array>,\n private options?: StreamDataOptions,\n ) {}\n\n [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> {\n const inner = this.it[Symbol.asyncIterator]();\n const schema = StreamDataResponse(this.schema);\n const decoder = schema.decode;\n const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};\n let shouldStop = false;\n\n let clock: string | number | NodeJS.Timeout | undefined;\n\n return {\n async next() {\n if (shouldStop) {\n return { done: true, value: undefined };\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: any is ok\n const t: Promise<{ done: boolean; value: any }> = new Promise(\n (_, reject) => {\n clock = setTimeout(() => {\n reject(new TimeoutError(timeout));\n }, timeout);\n },\n );\n\n try {\n const { done, value } = await Promise.race([inner.next(), t]);\n\n clearTimeout(clock);\n\n if (done || value.message === undefined) {\n return { done: true, value: undefined };\n }\n\n const decodedMessage = decoder(value.message);\n\n if (endingCursor) {\n assert(value.message.$case === \"data\");\n assert(decodedMessage._tag === \"data\");\n\n const { orderKey, uniqueKey } = endingCursor;\n const endCursor = decodedMessage.data.endCursor;\n\n // Check if the orderKey matches\n if (orderKey === endCursor?.orderKey) {\n // If a uniqueKey is specified, it must also match\n if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {\n shouldStop = true;\n return { done: false, value: decodedMessage };\n }\n }\n }\n\n return {\n done: false,\n value: decodedMessage,\n };\n } finally {\n clearTimeout(clock);\n }\n },\n };\n }\n}\n","/** Track data rate using high precision timers. */\nexport class RateGauge {\n private interval: number;\n private prev?: bigint;\n private rateMs?: number;\n private var: number;\n\n constructor(intervalSeconds: number) {\n // Convert seconds to milliseconds.\n this.interval = intervalSeconds * 1_000;\n this.var = 0;\n }\n\n public record(items: number) {\n // Compute the exponential moving average of the rate.\n const prev = this.prev;\n const now = process.hrtime.bigint();\n this.prev = now;\n\n if (!prev) {\n return;\n }\n\n const deltaMs = Number(now - prev) / 1_000_000;\n // rate in items/ms.\n const rateMs = items / deltaMs;\n\n if (this.rateMs === undefined) {\n this.rateMs = rateMs;\n this.var = 0;\n return;\n }\n\n const alpha = 1 - Math.exp(-deltaMs / this.interval);\n this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;\n\n const diff = rateMs - this.rateMs;\n const incr = alpha * diff;\n this.var = (1 - alpha) * (this.var + incr * diff);\n }\n\n /** Returns the average rate per second. */\n public average() {\n if (this.rateMs === undefined) {\n return undefined;\n }\n return this.rateMs * 1_000;\n }\n\n /** Returns the variance. */\n public variance() {\n return this.var;\n }\n}\n"],"names":["MessageCodec","OptionalCodec","Cursor","config","createChannel","grpcCreateClient","proto.stream.DnaStreamDefinition","consola","Metadata","__publicField","StreamDataResponse","assert"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAIa,MAAA,aAAA,GAAgBA,kBAAa,CAAA,EAAE,EAAA;AAKrC,MAAM,iBAAiBA,kBAAa,CAAA;AAAA,EACzC,WAAA,EAAaC,oBAAcC,aAAM,CAAA;AAAA,EACjC,YAAA,EAAcD,oBAAcC,aAAM,CAAA;AAAA,EAClC,SAAA,EAAWD,oBAAcC,aAAM,CAAA;AAAA,EAC/B,QAAA,EAAUD,oBAAcC,aAAM,CAAA;AAChC,CAAC;;;;;;;;ACSD,MAAM,kBAAqB,GAAA,IAAA,CAAA;AAEpB,MAAM,qBAAqB,KAAM,CAAA;AAAA,EACtC,YAAY,OAAiB,EAAA;AAC3B,IAAM,KAAA,CAAA,CAAA,uBAAA,EAA0B,OAAO,CAAI,EAAA,CAAA,CAAA,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAO,GAAA,cAAA,CAAA;AAAA,GACd;AACF,CAAA;AAsCO,SAAS,YACd,CAAAC,QAAA,EACA,SACA,EAAA,OAAA,GAA+B,EAC/B,EAAA;AACA,EAAA,MAAM,OAAU,GAAAC,sBAAA;AAAA,IACd,SAAA;AAAA,IACA,OAAS,EAAA,WAAA;AAAA,IACT,OAAS,EAAA,cAAA;AAAA,GACX,CAAA;AAEA,EAAA,MAAM,MAAuC,GAAAC,qBAAA;AAAA,IAC3CC,0BAAa;A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M,QAAQ,CAAI,GAAA,IAAA,CAAK,IAAI,CAAC,OAAA,GAAU,KAAK,QAAQ,CAAA,CAAA;AACnD,IAAA,IAAA,CAAK,MAAS,GAAA,KAAA,GAAQ,MAAU,GAAA,CAAA,CAAA,GAAI,SAAS,IAAK,CAAA,MAAA,CAAA;AAElD,IAAM,MAAA,IAAA,GAAO,SAAS,IAAK,CAAA,MAAA,CAAA;AAC3B,IAAA,MAAM,OAAO,KAAQ,GAAA,IAAA,CAAA;AACrB,IAAA,IAAA,CAAK,GAAO,GAAA,CAAA,CAAA,GAAI,KAAU,KAAA,IAAA,CAAK,MAAM,IAAO,GAAA,IAAA,CAAA,CAAA;AAAA,GAC9C;AAAA;AAAA,EAGO,OAAU,GAAA;AACf,IAAI,IAAA,IAAA,CAAK,WAAW,KAAW,CAAA,EAAA;AAC7B,MAAO,OAAA,KAAA,CAAA,CAAA;AAAA,KACT;AACA,IAAA,OAAO,KAAK,MAAS,GAAA,GAAA,CAAA;AAAA,GACvB;AAAA;AAAA,EAGO,QAAW,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,GAAA,CAAA;AAAA,GACd;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { s as stream } from './shared/protocol.a5762a90.cjs';
2
- export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, z as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.a5762a90.cjs';
1
+ import { s as stream } from './shared/protocol.0e734e33.cjs';
2
+ export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.0e734e33.cjs';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
4
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
5
5
  import './codec.cjs';
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { s as stream } from './shared/protocol.21b07506.mjs';
2
- export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, z as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.21b07506.mjs';
1
+ import { s as stream } from './shared/protocol.21e66b9e.mjs';
2
+ export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.21e66b9e.mjs';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
4
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
5
5
  import './codec.mjs';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { s as stream } from './shared/protocol.526c6532.js';
2
- export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, z as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.526c6532.js';
1
+ import { s as stream } from './shared/protocol.8fb09325.js';
2
+ export { B as Bytes, b as BytesFromUint8Array, w as Client, u as ClientCallOptions, x as CreateClientOptions, C as Cursor, e as CursorFromBytes, c as CursorProto, q as Data, g as DataFinality, h as DataProduction, D as DnaStreamClient, a as DnaStreamDefinition, k as Duration, j as DurationCodec, F as Finalize, G as GrpcClient, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, S as StatusRequest, f as StatusResponse, o as StdErr, m as StdOut, t as StreamConfig, A as StreamDataIterable, v as StreamDataOptions, l as StreamDataRequest, r as StreamDataResponse, p as SystemMessage, T as TimeoutError, z as createAuthenticatedClient, y as createClient, d as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.8fb09325.js';
3
3
  import _m0 from 'protobufjs/minimal.js';
4
4
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
5
5
  import './codec.js';
package/dist/index.mjs CHANGED
@@ -1,10 +1,11 @@
1
1
  import { s as stream, t as testing, C as Cursor, D as DnaStreamDefinition, S as StreamDataResponse } from './shared/protocol.68fdd897.mjs';
2
2
  export { B as BytesFromUint8Array, a as CursorFromBytes, k as Data, b as DataFinality, d as DataProduction, e as DurationCodec, F as Finalize, H as Heartbeat, I as Invalidate, R as ResponseWithoutData, h as StdErr, g as StdOut, l as StreamConfig, f as StreamDataRequest, j as SystemMessage, c as createCursor, i as isCursor, n as normalizeCursor } from './shared/protocol.68fdd897.mjs';
3
3
  import { MessageCodec, OptionalCodec } from './codec.mjs';
4
- import { createChannel, createClient as createClient$1 } from 'nice-grpc';
4
+ import assert from 'node:assert';
5
+ import consola from 'consola';
6
+ import { createChannel, createClient as createClient$1, Metadata } from 'nice-grpc';
5
7
  export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
6
8
  import 'protobufjs/minimal.js';
7
- import assert from 'node:assert';
8
9
  import 'viem';
9
10
  import 'long';
10
11
 
@@ -48,6 +49,27 @@ function createClient(config, streamUrl, options = {}) {
48
49
  );
49
50
  return new GrpcClient(config, client);
50
51
  }
52
+ function createAuthenticatedClient(config, streamUrl, options) {
53
+ const dnaToken = process.env.DNA_TOKEN;
54
+ if (!dnaToken) {
55
+ consola.warn(
56
+ "DNA_TOKEN environment variable is not set. Trying to connect without authentication."
57
+ );
58
+ }
59
+ return createClient(config, streamUrl, {
60
+ ...options,
61
+ defaultCallOptions: {
62
+ ...options?.defaultCallOptions ?? {},
63
+ "*": {
64
+ metadata: Metadata({
65
+ Authorization: `Bearer ${dnaToken}`
66
+ }),
67
+ // metadata cant be overrided with spread as its a class so we override it fully if user provided it.
68
+ ...options?.defaultCallOptions?.["*"] ?? {}
69
+ }
70
+ }
71
+ });
72
+ }
51
73
  class GrpcClient {
52
74
  constructor(config, client) {
53
75
  this.config = config;
@@ -171,5 +193,5 @@ class RateGauge {
171
193
  }
172
194
  }
173
195
 
174
- export { Cursor, DnaStreamDefinition, GrpcClient, RateGauge, StatusRequest, StatusResponse, StreamDataIterable, StreamDataResponse, TimeoutError, createClient, index as proto };
196
+ export { Cursor, DnaStreamDefinition, GrpcClient, RateGauge, StatusRequest, StatusResponse, StreamDataIterable, StreamDataResponse, TimeoutError, createAuthenticatedClient, createClient, index as proto };
175
197
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/status.ts","../src/client.ts","../src/rate.ts"],"sourcesContent":["import { type CodecType, MessageCodec, OptionalCodec } from \"./codec\";\nimport { Cursor } from \"./common\";\n\n/** The request to the `status` endpoint. */\nexport const StatusRequest = MessageCodec({});\n\nexport type StatusRequest = CodecType<typeof StatusRequest>;\n\n/** The response from the `status` endpoint. */\nexport const StatusResponse = MessageCodec({\n currentHead: OptionalCodec(Cursor),\n lastIngested: OptionalCodec(Cursor),\n finalized: OptionalCodec(Cursor),\n starting: OptionalCodec(Cursor),\n});\n\nexport type StatusResponse = CodecType<typeof StatusResponse>;\n","import {\n type ChannelCredentials,\n type ChannelOptions,\n type DefaultCallOptions,\n type NormalizedServiceDefinition,\n createChannel,\n createClient as grpcCreateClient,\n} from \"nice-grpc\";\n\nimport * as proto from \"./proto\";\n\nimport assert from \"node:assert\";\nimport type { Codec } from \"./codec\";\nimport type { Cursor } from \"./common\";\nimport type { StreamConfig } from \"./config\";\nimport { StatusRequest, StatusResponse } from \"./status\";\nimport { type StreamDataRequest, StreamDataResponse } from \"./stream\";\n\nexport { ClientError, ServerError, Status, Metadata } from \"nice-grpc\";\n\nconst DEFAULT_TIMEOUT_MS = 45_000;\n\nexport class TimeoutError extends Error {\n constructor(timeout: number) {\n super(`No message received in ${timeout}ms`);\n this.name = \"TimeoutError\";\n }\n}\n\n/** Client call options. */\nexport interface ClientCallOptions {\n signal?: AbortSignal;\n}\n\nexport interface StreamDataOptions extends ClientCallOptions {\n /** Stop at the specified cursor (inclusive). */\n endingCursor?: Cursor;\n /** Timeout between messages, in milliseconds. */\n timeout?: number;\n}\n\n/** DNA client. */\nexport interface Client<TFilter, TBlock> {\n /** Fetch the DNA stream status. */\n status(\n request?: StatusRequest,\n options?: ClientCallOptions,\n ): Promise<StatusResponse>;\n\n /** Start streaming data from the DNA server. */\n streamData(\n request: StreamDataRequest<TFilter>,\n options?: StreamDataOptions,\n ): AsyncIterable<StreamDataResponse<TBlock>>;\n}\n\nexport type CreateClientOptions = {\n defaultCallOptions?: DefaultCallOptions<\n NormalizedServiceDefinition<proto.stream.DnaStreamDefinition>\n >;\n credentials?: ChannelCredentials;\n channelOptions?: ChannelOptions;\n};\n\n/** Create a client connecting to the DNA grpc service. */\nexport function createClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options: CreateClientOptions = {},\n) {\n const channel = createChannel(\n streamUrl,\n options?.credentials,\n options?.channelOptions,\n );\n\n const client: proto.stream.DnaStreamClient = grpcCreateClient(\n proto.stream.DnaStreamDefinition,\n channel,\n options?.defaultCallOptions,\n );\n\n return new GrpcClient(config, client);\n}\n\nexport class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {\n private encodeRequest;\n\n constructor(\n private config: StreamConfig<TFilter, TBlock>,\n private client: proto.stream.DnaStreamClient,\n ) {\n this.encodeRequest = config.Request.encode;\n }\n\n async status(request?: StatusRequest, options?: ClientCallOptions) {\n const response = await this.client.status(\n StatusRequest.encode(request ?? {}),\n options,\n );\n return StatusResponse.decode(response);\n }\n\n streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions) {\n const it = this.client.streamData(this.encodeRequest(request), options);\n return new StreamDataIterable(it, this.config.Block, options);\n }\n}\n\nexport class StreamDataIterable<TBlock> {\n constructor(\n private it: AsyncIterable<proto.stream.StreamDataResponse>,\n private schema: Codec<TBlock, Uint8Array>,\n private options?: StreamDataOptions,\n ) {}\n\n [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> {\n const inner = this.it[Symbol.asyncIterator]();\n const schema = StreamDataResponse(this.schema);\n const decoder = schema.decode;\n const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};\n let shouldStop = false;\n\n let clock: string | number | NodeJS.Timeout | undefined;\n\n return {\n async next() {\n if (shouldStop) {\n return { done: true, value: undefined };\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: any is ok\n const t: Promise<{ done: boolean; value: any }> = new Promise(\n (_, reject) => {\n clock = setTimeout(() => {\n reject(new TimeoutError(timeout));\n }, timeout);\n },\n );\n\n try {\n const { done, value } = await Promise.race([inner.next(), t]);\n\n clearTimeout(clock);\n\n if (done || value.message === undefined) {\n return { done: true, value: undefined };\n }\n\n const decodedMessage = decoder(value.message);\n\n if (endingCursor) {\n assert(value.message.$case === \"data\");\n assert(decodedMessage._tag === \"data\");\n\n const { orderKey, uniqueKey } = endingCursor;\n const endCursor = decodedMessage.data.endCursor;\n\n // Check if the orderKey matches\n if (orderKey === endCursor?.orderKey) {\n // If a uniqueKey is specified, it must also match\n if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {\n shouldStop = true;\n return { done: false, value: decodedMessage };\n }\n }\n }\n\n return {\n done: false,\n value: decodedMessage,\n };\n } finally {\n clearTimeout(clock);\n }\n },\n };\n }\n}\n","/** Track data rate using high precision timers. */\nexport class RateGauge {\n private interval: number;\n private prev?: bigint;\n private rateMs?: number;\n private var: number;\n\n constructor(intervalSeconds: number) {\n // Convert seconds to milliseconds.\n this.interval = intervalSeconds * 1_000;\n this.var = 0;\n }\n\n public record(items: number) {\n // Compute the exponential moving average of the rate.\n const prev = this.prev;\n const now = process.hrtime.bigint();\n this.prev = now;\n\n if (!prev) {\n return;\n }\n\n const deltaMs = Number(now - prev) / 1_000_000;\n // rate in items/ms.\n const rateMs = items / deltaMs;\n\n if (this.rateMs === undefined) {\n this.rateMs = rateMs;\n this.var = 0;\n return;\n }\n\n const alpha = 1 - Math.exp(-deltaMs / this.interval);\n this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;\n\n const diff = rateMs - this.rateMs;\n const incr = alpha * diff;\n this.var = (1 - alpha) * (this.var + incr * diff);\n }\n\n /** Returns the average rate per second. */\n public average() {\n if (this.rateMs === undefined) {\n return undefined;\n }\n return this.rateMs * 1_000;\n }\n\n /** Returns the variance. */\n public variance() {\n return this.var;\n }\n}\n"],"names":["grpcCreateClient","proto.stream.DnaStreamDefinition","__publicField"],"mappings":";;;;;;;;;;;;;;;;AAIa,MAAA,aAAA,GAAgB,YAAa,CAAA,EAAE,EAAA;AAKrC,MAAM,iBAAiB,YAAa,CAAA;AAAA,EACzC,WAAA,EAAa,cAAc,MAAM,CAAA;AAAA,EACjC,YAAA,EAAc,cAAc,MAAM,CAAA;AAAA,EAClC,SAAA,EAAW,cAAc,MAAM,CAAA;AAAA,EAC/B,QAAA,EAAU,cAAc,MAAM,CAAA;AAChC,CAAC;;;;;;;;ACMD,MAAM,kBAAqB,GAAA,IAAA,CAAA;AAEpB,MAAM,qBAAqB,KAAM,CAAA;AAAA,EACtC,YAAY,OAAiB,EAAA;AAC3B,IAAM,KAAA,CAAA,CAAA,uBAAA,EAA0B,OAAO,CAAI,EAAA,CAAA,CAAA,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAO,GAAA,cAAA,CAAA;AAAA,GACd;AACF,CAAA;AAsCO,SAAS,YACd,CAAA,MAAA,EACA,SACA,EAAA,OAAA,GAA+B,EAC/B,EAAA;AACA,EAAA,MAAM,OAAU,GAAA,aAAA;AAAA,IACd,SAAA;AAAA,IACA,OAAS,EAAA,WAAA;AAAA,IACT,OAAS,EAAA,cAAA;AAAA,GACX,CAAA;AAEA,EAAA,MAAM,MAAuC,GAAAA,cAAA;AAAA,IAC3CC,mBAAa;AAAA,IACb,OAAA;AAAA,IACA,OAAS,EAAA,kBAAA;AAA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AAA,CAAA;AAAA,GACd;AACF;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/status.ts","../src/client.ts","../src/rate.ts"],"sourcesContent":["import { type CodecType, MessageCodec, OptionalCodec } from \"./codec\";\nimport { Cursor } from \"./common\";\n\n/** The request to the `status` endpoint. */\nexport const StatusRequest = MessageCodec({});\n\nexport type StatusRequest = CodecType<typeof StatusRequest>;\n\n/** The response from the `status` endpoint. */\nexport const StatusResponse = MessageCodec({\n currentHead: OptionalCodec(Cursor),\n lastIngested: OptionalCodec(Cursor),\n finalized: OptionalCodec(Cursor),\n starting: OptionalCodec(Cursor),\n});\n\nexport type StatusResponse = CodecType<typeof StatusResponse>;\n","import assert from \"node:assert\";\n\nimport consola from \"consola\";\nimport {\n type ChannelCredentials,\n type ChannelOptions,\n type DefaultCallOptions,\n Metadata,\n type NormalizedServiceDefinition,\n createChannel,\n createClient as grpcCreateClient,\n} from \"nice-grpc\";\n\nimport * as proto from \"./proto\";\n\nimport type { Codec } from \"./codec\";\nimport type { Cursor } from \"./common\";\nimport type { StreamConfig } from \"./config\";\nimport { StatusRequest, StatusResponse } from \"./status\";\nimport { type StreamDataRequest, StreamDataResponse } from \"./stream\";\n\nexport { ClientError, ServerError, Status, Metadata } from \"nice-grpc\";\n\nconst DEFAULT_TIMEOUT_MS = 45_000;\n\nexport class TimeoutError extends Error {\n constructor(timeout: number) {\n super(`No message received in ${timeout}ms`);\n this.name = \"TimeoutError\";\n }\n}\n\n/** Client call options. */\nexport interface ClientCallOptions {\n signal?: AbortSignal;\n}\n\nexport interface StreamDataOptions extends ClientCallOptions {\n /** Stop at the specified cursor (inclusive). */\n endingCursor?: Cursor;\n /** Timeout between messages, in milliseconds. */\n timeout?: number;\n}\n\n/** DNA client. */\nexport interface Client<TFilter, TBlock> {\n /** Fetch the DNA stream status. */\n status(\n request?: StatusRequest,\n options?: ClientCallOptions,\n ): Promise<StatusResponse>;\n\n /** Start streaming data from the DNA server. */\n streamData(\n request: StreamDataRequest<TFilter>,\n options?: StreamDataOptions,\n ): AsyncIterable<StreamDataResponse<TBlock>>;\n}\n\nexport type CreateClientOptions = {\n defaultCallOptions?: DefaultCallOptions<\n NormalizedServiceDefinition<proto.stream.DnaStreamDefinition>\n >;\n credentials?: ChannelCredentials;\n channelOptions?: ChannelOptions;\n};\n\n/** Create a client connecting to the DNA grpc service. */\nexport function createClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options: CreateClientOptions = {},\n) {\n const channel = createChannel(\n streamUrl,\n options?.credentials,\n options?.channelOptions,\n );\n\n const client: proto.stream.DnaStreamClient = grpcCreateClient(\n proto.stream.DnaStreamDefinition,\n channel,\n options?.defaultCallOptions,\n );\n\n return new GrpcClient(config, client);\n}\n\nexport function createAuthenticatedClient<TFilter, TBlock>(\n config: StreamConfig<TFilter, TBlock>,\n streamUrl: string,\n options?: CreateClientOptions,\n) {\n const dnaToken = process.env.DNA_TOKEN;\n if (!dnaToken) {\n consola.warn(\n \"DNA_TOKEN environment variable is not set. Trying to connect without authentication.\",\n );\n }\n\n return createClient(config, streamUrl, {\n ...options,\n defaultCallOptions: {\n ...(options?.defaultCallOptions ?? {}),\n \"*\": {\n metadata: Metadata({\n Authorization: `Bearer ${dnaToken}`,\n }),\n // metadata cant be overrided with spread as its a class so we override it fully if user provided it.\n ...(options?.defaultCallOptions?.[\"*\"] ?? {}),\n },\n },\n });\n}\n\nexport class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {\n private encodeRequest;\n\n constructor(\n private config: StreamConfig<TFilter, TBlock>,\n private client: proto.stream.DnaStreamClient,\n ) {\n this.encodeRequest = config.Request.encode;\n }\n\n async status(request?: StatusRequest, options?: ClientCallOptions) {\n const response = await this.client.status(\n StatusRequest.encode(request ?? {}),\n options,\n );\n return StatusResponse.decode(response);\n }\n\n streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions) {\n const it = this.client.streamData(this.encodeRequest(request), options);\n return new StreamDataIterable(it, this.config.Block, options);\n }\n}\n\nexport class StreamDataIterable<TBlock> {\n constructor(\n private it: AsyncIterable<proto.stream.StreamDataResponse>,\n private schema: Codec<TBlock, Uint8Array>,\n private options?: StreamDataOptions,\n ) {}\n\n [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>> {\n const inner = this.it[Symbol.asyncIterator]();\n const schema = StreamDataResponse(this.schema);\n const decoder = schema.decode;\n const { endingCursor, timeout = DEFAULT_TIMEOUT_MS } = this.options ?? {};\n let shouldStop = false;\n\n let clock: string | number | NodeJS.Timeout | undefined;\n\n return {\n async next() {\n if (shouldStop) {\n return { done: true, value: undefined };\n }\n\n // biome-ignore lint/suspicious/noExplicitAny: any is ok\n const t: Promise<{ done: boolean; value: any }> = new Promise(\n (_, reject) => {\n clock = setTimeout(() => {\n reject(new TimeoutError(timeout));\n }, timeout);\n },\n );\n\n try {\n const { done, value } = await Promise.race([inner.next(), t]);\n\n clearTimeout(clock);\n\n if (done || value.message === undefined) {\n return { done: true, value: undefined };\n }\n\n const decodedMessage = decoder(value.message);\n\n if (endingCursor) {\n assert(value.message.$case === \"data\");\n assert(decodedMessage._tag === \"data\");\n\n const { orderKey, uniqueKey } = endingCursor;\n const endCursor = decodedMessage.data.endCursor;\n\n // Check if the orderKey matches\n if (orderKey === endCursor?.orderKey) {\n // If a uniqueKey is specified, it must also match\n if (!uniqueKey || uniqueKey === endCursor.uniqueKey) {\n shouldStop = true;\n return { done: false, value: decodedMessage };\n }\n }\n }\n\n return {\n done: false,\n value: decodedMessage,\n };\n } finally {\n clearTimeout(clock);\n }\n },\n };\n }\n}\n","/** Track data rate using high precision timers. */\nexport class RateGauge {\n private interval: number;\n private prev?: bigint;\n private rateMs?: number;\n private var: number;\n\n constructor(intervalSeconds: number) {\n // Convert seconds to milliseconds.\n this.interval = intervalSeconds * 1_000;\n this.var = 0;\n }\n\n public record(items: number) {\n // Compute the exponential moving average of the rate.\n const prev = this.prev;\n const now = process.hrtime.bigint();\n this.prev = now;\n\n if (!prev) {\n return;\n }\n\n const deltaMs = Number(now - prev) / 1_000_000;\n // rate in items/ms.\n const rateMs = items / deltaMs;\n\n if (this.rateMs === undefined) {\n this.rateMs = rateMs;\n this.var = 0;\n return;\n }\n\n const alpha = 1 - Math.exp(-deltaMs / this.interval);\n this.rateMs = alpha * rateMs + (1 - alpha) * this.rateMs;\n\n const diff = rateMs - this.rateMs;\n const incr = alpha * diff;\n this.var = (1 - alpha) * (this.var + incr * diff);\n }\n\n /** Returns the average rate per second. */\n public average() {\n if (this.rateMs === undefined) {\n return undefined;\n }\n return this.rateMs * 1_000;\n }\n\n /** Returns the variance. */\n public variance() {\n return this.var;\n }\n}\n"],"names":["grpcCreateClient","proto.stream.DnaStreamDefinition","__publicField"],"mappings":";;;;;;;;;;;;;;;;;AAIa,MAAA,aAAA,GAAgB,YAAa,CAAA,EAAE,EAAA;AAKrC,MAAM,iBAAiB,YAAa,CAAA;AAAA,EACzC,WAAA,EAAa,cAAc,MAAM,CAAA;AAAA,EACjC,YAAA,EAAc,cAAc,MAAM,CAAA;AAAA,EAClC,SAAA,EAAW,cAAc,MAAM,CAAA;AAAA,EAC/B,QAAA,EAAU,cAAc,MAAM,CAAA;AAChC,CAAC;;;;;;;;ACSD,MAAM,kBAAqB,GAAA,IAAA,CAAA;AAEpB,MAAM,qBAAqB,KAAM,CAAA;AAAA,EACtC,YAAY,OAAiB,EAAA;AAC3B,IAAM,KAAA,CAAA,CAAA,uBAAA,EAA0B,OAAO,CAAI,EAAA,CAAA,CAAA,CAAA;AAC3C,IAAA,IAAA,CAAK,IAAO,GAAA,cAAA,CAAA;AAAA,GACd;AACF,CAAA;AAsCO,SAAS,YACd,CAAA,MAAA,EACA,SACA,EAAA,OAAA,GAA+B,EAC/B,EAAA;AACA,EAAA,MAAM,OAAU,GAAA,aAAA;AAAA,IACd,SAAA;AAAA,IACA,OAAS,EAAA,WAAA;AAAA,IACT,OAAS,EAAA,cAAA;AAAA,GACX,CAAA;AAEA,EAAA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AACX,MAAA,OAAA;AAAA,KACF;AAEA,IAAA,MAAM,QAAQ,CAAI,GAAA,IAAA,CAAK,IAAI,CAAC,OAAA,GAAU,KAAK,QAAQ,CAAA,CAAA;AACnD,IAAA,IAAA,CAAK,MAAS,GAAA,KAAA,GAAQ,MAAU,GAAA,CAAA,CAAA,GAAI,SAAS,IAAK,CAAA,MAAA,CAAA;AAElD,IAAM,MAAA,IAAA,GAAO,SAAS,IAAK,CAAA,MAAA,CAAA;AAC3B,IAAA,MAAM,OAAO,KAAQ,GAAA,IAAA,CAAA;AACrB,IAAA,IAAA,CAAK,GAAO,GAAA,CAAA,CAAA,GAAI,KAAU,KAAA,IAAA,CAAK,MAAM,IAAO,GAAA,IAAA,CAAA,CAAA;AAAA,GAC9C;AAAA;AAAA,EAGO,OAAU,GAAA;AACf,IAAI,IAAA,IAAA,CAAK,WAAW,KAAW,CAAA,EAAA;AAC7B,MAAO,OAAA,KAAA,CAAA,CAAA;AAAA,KACT;AACA,IAAA,OAAO,KAAK,MAAS,GAAA,GAAA,CAAA;AAAA,GACvB;AAAA;AAAA,EAGO,QAAW,GAAA;AAChB,IAAA,OAAO,IAAK,CAAA,GAAA,CAAA;AAAA,GACd;AACF;;;;"}
@@ -872,6 +872,7 @@ type CreateClientOptions = {
872
872
  };
873
873
  /** Create a client connecting to the DNA grpc service. */
874
874
  declare function createClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
+ declare function createAuthenticatedClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
876
  declare class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {
876
877
  private config;
877
878
  private client;
@@ -905,4 +906,4 @@ declare class StreamDataIterable<TBlock> {
905
906
  [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>>;
906
907
  }
907
908
 
908
- export { Cursor$1 as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, DataFinality$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataProduction$1 as J, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, StreamDataIterable as z };
909
+ export { StreamDataIterable as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, Cursor$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataFinality$1 as J, DataProduction$1 as K, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, createAuthenticatedClient as z };
@@ -872,6 +872,7 @@ type CreateClientOptions = {
872
872
  };
873
873
  /** Create a client connecting to the DNA grpc service. */
874
874
  declare function createClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
+ declare function createAuthenticatedClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
876
  declare class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {
876
877
  private config;
877
878
  private client;
@@ -905,4 +906,4 @@ declare class StreamDataIterable<TBlock> {
905
906
  [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>>;
906
907
  }
907
908
 
908
- export { Cursor$1 as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, DataFinality$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataProduction$1 as J, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, StreamDataIterable as z };
909
+ export { StreamDataIterable as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, Cursor$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataFinality$1 as J, DataProduction$1 as K, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, createAuthenticatedClient as z };
@@ -872,6 +872,7 @@ type CreateClientOptions = {
872
872
  };
873
873
  /** Create a client connecting to the DNA grpc service. */
874
874
  declare function createClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
+ declare function createAuthenticatedClient<TFilter, TBlock>(config: StreamConfig<TFilter, TBlock>, streamUrl: string, options?: CreateClientOptions): GrpcClient<TFilter, TBlock>;
875
876
  declare class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {
876
877
  private config;
877
878
  private client;
@@ -905,4 +906,4 @@ declare class StreamDataIterable<TBlock> {
905
906
  [Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>>;
906
907
  }
907
908
 
908
- export { Cursor$1 as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, DataFinality$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataProduction$1 as J, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, StreamDataIterable as z };
909
+ export { StreamDataIterable as A, type Bytes as B, Cursor as C, type DnaStreamClient as D, Cursor$1 as E, Finalize as F, GrpcClient as G, Heartbeat as H, Invalidate as I, DataFinality$1 as J, DataProduction$1 as K, ResponseWithoutData as R, StatusRequest as S, TimeoutError as T, DnaStreamDefinition as a, BytesFromUint8Array as b, type CursorProto as c, createCursor as d, CursorFromBytes as e, StatusResponse as f, DataFinality as g, DataProduction as h, isCursor as i, DurationCodec as j, type Duration as k, StreamDataRequest as l, StdOut as m, normalizeCursor as n, StdErr as o, SystemMessage as p, Data as q, StreamDataResponse as r, stream as s, StreamConfig as t, type ClientCallOptions as u, type StreamDataOptions as v, type Client as w, type CreateClientOptions as x, createClient as y, createAuthenticatedClient as z };
@@ -1,4 +1,4 @@
1
- import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, A as Cursor, E as DataFinality, J as DataProduction } from '../shared/protocol.a5762a90.cjs';
1
+ import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, E as Cursor, J as DataFinality, K as DataProduction } from '../shared/protocol.0e734e33.cjs';
2
2
  import { MessageCodec, Codec, CodecType } from '../codec.cjs';
3
3
  import 'nice-grpc-common';
4
4
  import 'protobufjs/minimal.js';
@@ -1,4 +1,4 @@
1
- import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, A as Cursor, E as DataFinality, J as DataProduction } from '../shared/protocol.21b07506.mjs';
1
+ import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, E as Cursor, J as DataFinality, K as DataProduction } from '../shared/protocol.21e66b9e.mjs';
2
2
  import { MessageCodec, Codec, CodecType } from '../codec.mjs';
3
3
  import 'nice-grpc-common';
4
4
  import 'protobufjs/minimal.js';
@@ -1,4 +1,4 @@
1
- import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, A as Cursor, E as DataFinality, J as DataProduction } from '../shared/protocol.526c6532.js';
1
+ import { w as Client, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, t as StreamConfig, B as Bytes, E as Cursor, J as DataFinality, K as DataProduction } from '../shared/protocol.8fb09325.js';
2
2
  import { MessageCodec, Codec, CodecType } from '../codec.js';
3
3
  import 'nice-grpc-common';
4
4
  import 'protobufjs/minimal.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apibara/protocol",
3
- "version": "2.1.0-beta.37",
3
+ "version": "2.1.0-beta.39",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -47,6 +47,7 @@
47
47
  "vitest": "^1.6.0"
48
48
  },
49
49
  "dependencies": {
50
+ "consola": "^3.4.2",
50
51
  "long": "^5.2.3",
51
52
  "nice-grpc": "^2.1.8",
52
53
  "nice-grpc-common": "^2.0.2",
package/src/client.ts CHANGED
@@ -1,7 +1,11 @@
1
+ import assert from "node:assert";
2
+
3
+ import consola from "consola";
1
4
  import {
2
5
  type ChannelCredentials,
3
6
  type ChannelOptions,
4
7
  type DefaultCallOptions,
8
+ Metadata,
5
9
  type NormalizedServiceDefinition,
6
10
  createChannel,
7
11
  createClient as grpcCreateClient,
@@ -9,7 +13,6 @@ import {
9
13
 
10
14
  import * as proto from "./proto";
11
15
 
12
- import assert from "node:assert";
13
16
  import type { Codec } from "./codec";
14
17
  import type { Cursor } from "./common";
15
18
  import type { StreamConfig } from "./config";
@@ -83,6 +86,33 @@ export function createClient<TFilter, TBlock>(
83
86
  return new GrpcClient(config, client);
84
87
  }
85
88
 
89
+ export function createAuthenticatedClient<TFilter, TBlock>(
90
+ config: StreamConfig<TFilter, TBlock>,
91
+ streamUrl: string,
92
+ options?: CreateClientOptions,
93
+ ) {
94
+ const dnaToken = process.env.DNA_TOKEN;
95
+ if (!dnaToken) {
96
+ consola.warn(
97
+ "DNA_TOKEN environment variable is not set. Trying to connect without authentication.",
98
+ );
99
+ }
100
+
101
+ return createClient(config, streamUrl, {
102
+ ...options,
103
+ defaultCallOptions: {
104
+ ...(options?.defaultCallOptions ?? {}),
105
+ "*": {
106
+ metadata: Metadata({
107
+ Authorization: `Bearer ${dnaToken}`,
108
+ }),
109
+ // metadata cant be overrided with spread as its a class so we override it fully if user provided it.
110
+ ...(options?.defaultCallOptions?.["*"] ?? {}),
111
+ },
112
+ },
113
+ });
114
+ }
115
+
86
116
  export class GrpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {
87
117
  private encodeRequest;
88
118