@apibara/protocol 2.1.0-beta.41 → 2.1.0-beta.43
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 +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/rpc/index.cjs +12 -0
- package/dist/rpc/index.cjs.map +1 -0
- package/dist/rpc/index.d.cts +6 -0
- package/dist/rpc/index.d.mts +6 -0
- package/dist/rpc/index.d.ts +6 -0
- package/dist/rpc/index.mjs +3 -0
- package/dist/rpc/index.mjs.map +1 -0
- package/dist/shared/protocol.6a091343.d.mts +102 -0
- package/dist/shared/protocol.91a69be4.cjs +518 -0
- package/dist/shared/protocol.91a69be4.cjs.map +1 -0
- package/dist/shared/protocol.aec6eac7.d.ts +102 -0
- package/dist/shared/protocol.d171ebd2.d.cts +102 -0
- package/dist/shared/protocol.ea189418.mjs +512 -0
- package/dist/shared/protocol.ea189418.mjs.map +1 -0
- package/package.json +7 -1
- package/src/index.ts +2 -0
- package/src/rpc/chain-tracker.ts +310 -0
- package/src/rpc/client.ts +51 -0
- package/src/rpc/config.ts +86 -0
- package/src/rpc/data-stream.ts +348 -0
- package/src/rpc/helpers.ts +9 -0
- package/src/rpc/index.ts +13 -0
package/dist/index.cjs
CHANGED
|
@@ -6,6 +6,7 @@ const assert = require('node:assert');
|
|
|
6
6
|
const consola = require('consola');
|
|
7
7
|
const niceGrpc = require('nice-grpc');
|
|
8
8
|
require('protobufjs/minimal.js');
|
|
9
|
+
const rpc_index = require('./shared/protocol.91a69be4.cjs');
|
|
9
10
|
require('viem');
|
|
10
11
|
require('long');
|
|
11
12
|
|
|
@@ -223,6 +224,7 @@ exports.ClientError = niceGrpc.ClientError;
|
|
|
223
224
|
exports.Metadata = niceGrpc.Metadata;
|
|
224
225
|
exports.ServerError = niceGrpc.ServerError;
|
|
225
226
|
exports.Status = niceGrpc.Status;
|
|
227
|
+
exports.rpc = rpc_index.index;
|
|
226
228
|
exports.GrpcClient = GrpcClient;
|
|
227
229
|
exports.RateGauge = RateGauge;
|
|
228
230
|
exports.StatusRequest = StatusRequest;
|
package/dist/index.cjs.map
CHANGED
|
@@ -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 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,CAA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AAA,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;AAAA,IACb,OAAA;AAAA,IACA,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,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,6 +1,7 @@
|
|
|
1
1
|
import { s as stream } from './shared/protocol.0e734e33.cjs';
|
|
2
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
|
+
export { i as rpc } from './shared/protocol.d171ebd2.cjs';
|
|
4
5
|
export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
|
|
5
6
|
import './codec.cjs';
|
|
6
7
|
import 'nice-grpc-common';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { s as stream } from './shared/protocol.21e66b9e.mjs';
|
|
2
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
|
+
export { i as rpc } from './shared/protocol.6a091343.mjs';
|
|
4
5
|
export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
|
|
5
6
|
import './codec.mjs';
|
|
6
7
|
import 'nice-grpc-common';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { s as stream } from './shared/protocol.8fb09325.js';
|
|
2
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
|
+
export { i as rpc } from './shared/protocol.aec6eac7.js';
|
|
4
5
|
export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
|
|
5
6
|
import './codec.js';
|
|
6
7
|
import 'nice-grpc-common';
|
package/dist/index.mjs
CHANGED
|
@@ -6,6 +6,7 @@ import consola from 'consola';
|
|
|
6
6
|
import { createChannel, createClient as createClient$1, Metadata } from 'nice-grpc';
|
|
7
7
|
export { ClientError, Metadata, ServerError, Status } from 'nice-grpc';
|
|
8
8
|
import 'protobufjs/minimal.js';
|
|
9
|
+
export { i as rpc } from './shared/protocol.ea189418.mjs';
|
|
9
10
|
import 'viem';
|
|
10
11
|
import 'long';
|
|
11
12
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -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 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,M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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.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,MA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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;;;;"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const rpc_index = require('../shared/protocol.91a69be4.cjs');
|
|
4
|
+
require('viem');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.RpcClient = rpc_index.RpcClient;
|
|
9
|
+
exports.RpcDataStream = rpc_index.RpcDataStream;
|
|
10
|
+
exports.RpcStreamConfig = rpc_index.RpcStreamConfig;
|
|
11
|
+
exports.createRpcClient = rpc_index.createRpcClient;
|
|
12
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.d171ebd2.cjs';
|
|
2
|
+
import '../shared/protocol.0e734e33.cjs';
|
|
3
|
+
import '../codec.cjs';
|
|
4
|
+
import 'nice-grpc-common';
|
|
5
|
+
import 'protobufjs/minimal.js';
|
|
6
|
+
import 'nice-grpc';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.6a091343.mjs';
|
|
2
|
+
import '../shared/protocol.21e66b9e.mjs';
|
|
3
|
+
import '../codec.mjs';
|
|
4
|
+
import 'nice-grpc-common';
|
|
5
|
+
import 'protobufjs/minimal.js';
|
|
6
|
+
import 'nice-grpc';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { B as BlockInfo, c as FetchBlockByNumberArgs, d as FetchBlockByNumberResult, F as FetchBlockRangeArgs, b as FetchBlockRangeResult, a as FetchBlockResult, e as FetchCursorArgs, f as RpcClient, h as RpcDataStream, R as RpcStreamConfig, V as ValidateFilterResult, g as createRpcClient } from '../shared/protocol.aec6eac7.js';
|
|
2
|
+
import '../shared/protocol.8fb09325.js';
|
|
3
|
+
import '../codec.js';
|
|
4
|
+
import 'nice-grpc-common';
|
|
5
|
+
import 'protobufjs/minimal.js';
|
|
6
|
+
import 'nice-grpc';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { C as Cursor, B as Bytes, w as Client, S as StatusRequest, u as ClientCallOptions, f as StatusResponse, l as StreamDataRequest, v as StreamDataOptions, r as StreamDataResponse } from './protocol.21e66b9e.mjs';
|
|
2
|
+
|
|
3
|
+
type FetchBlockRangeArgs<TFilter> = {
|
|
4
|
+
startBlock: bigint;
|
|
5
|
+
finalizedBlock: bigint;
|
|
6
|
+
filter: TFilter;
|
|
7
|
+
};
|
|
8
|
+
type FetchBlockRangeResult<TBlock> = {
|
|
9
|
+
startBlock: bigint;
|
|
10
|
+
endBlock: bigint;
|
|
11
|
+
data: FetchBlockResult<TBlock>[];
|
|
12
|
+
};
|
|
13
|
+
type FetchBlockResult<TBlock> = {
|
|
14
|
+
block: TBlock | null;
|
|
15
|
+
cursor: Cursor | undefined;
|
|
16
|
+
endCursor: Cursor;
|
|
17
|
+
};
|
|
18
|
+
type BlockInfo = {
|
|
19
|
+
blockNumber: bigint;
|
|
20
|
+
blockHash: Bytes;
|
|
21
|
+
parentBlockHash: Bytes;
|
|
22
|
+
};
|
|
23
|
+
type FetchBlockByNumberArgs<TFilter> = {
|
|
24
|
+
blockNumber: bigint;
|
|
25
|
+
expectedParentBlockHash: Bytes;
|
|
26
|
+
filter: TFilter;
|
|
27
|
+
};
|
|
28
|
+
type FetchBlockByNumberResult<TBlock> = {
|
|
29
|
+
status: "success";
|
|
30
|
+
data: FetchBlockResult<TBlock>;
|
|
31
|
+
blockInfo: BlockInfo;
|
|
32
|
+
} | {
|
|
33
|
+
status: "reorg";
|
|
34
|
+
blockInfo: BlockInfo;
|
|
35
|
+
};
|
|
36
|
+
type FetchCursorArgs = {
|
|
37
|
+
blockTag: "latest" | "finalized";
|
|
38
|
+
blockNumber?: undefined;
|
|
39
|
+
blockHash?: undefined;
|
|
40
|
+
} | {
|
|
41
|
+
blockTag?: undefined;
|
|
42
|
+
blockNumber: bigint;
|
|
43
|
+
blockHash?: undefined;
|
|
44
|
+
} | {
|
|
45
|
+
blockTag?: undefined;
|
|
46
|
+
blockNumber?: undefined;
|
|
47
|
+
blockHash: Bytes;
|
|
48
|
+
};
|
|
49
|
+
type ValidateFilterResult = {
|
|
50
|
+
valid: true;
|
|
51
|
+
error?: undefined;
|
|
52
|
+
} | {
|
|
53
|
+
valid: false;
|
|
54
|
+
error: string;
|
|
55
|
+
};
|
|
56
|
+
declare abstract class RpcStreamConfig<TFilter, TBlock> {
|
|
57
|
+
abstract headRefreshIntervalMs(): number;
|
|
58
|
+
abstract finalizedRefreshIntervalMs(): number;
|
|
59
|
+
abstract fetchCursor(args: FetchCursorArgs): Promise<BlockInfo | null>;
|
|
60
|
+
abstract validateFilter(filter: TFilter): ValidateFilterResult;
|
|
61
|
+
abstract fetchBlockRange(args: FetchBlockRangeArgs<TFilter>): Promise<FetchBlockRangeResult<TBlock>>;
|
|
62
|
+
abstract fetchBlockByNumber(args: FetchBlockByNumberArgs<TFilter>): Promise<FetchBlockByNumberResult<TBlock>>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
declare class RpcClient<TFilter, TBlock> implements Client<TFilter, TBlock> {
|
|
66
|
+
private config;
|
|
67
|
+
constructor(config: RpcStreamConfig<TFilter, TBlock>);
|
|
68
|
+
status(_request?: StatusRequest, _options?: ClientCallOptions): Promise<StatusResponse>;
|
|
69
|
+
streamData(request: StreamDataRequest<TFilter>, options?: StreamDataOptions): AsyncIterable<StreamDataResponse<TBlock>>;
|
|
70
|
+
}
|
|
71
|
+
declare function createRpcClient<TFilter, TBlock>(config: RpcStreamConfig<TFilter, TBlock>): RpcClient<TFilter, TBlock>;
|
|
72
|
+
|
|
73
|
+
declare class RpcDataStream<TFilter, TBlock> {
|
|
74
|
+
private config;
|
|
75
|
+
private request;
|
|
76
|
+
private options?;
|
|
77
|
+
private heartbeatIntervalMs;
|
|
78
|
+
constructor(config: RpcStreamConfig<TFilter, TBlock>, request: StreamDataRequest<TFilter>, options?: StreamDataOptions | undefined);
|
|
79
|
+
[Symbol.asyncIterator](): AsyncIterator<StreamDataResponse<TBlock>>;
|
|
80
|
+
private initialize;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
type index_BlockInfo = BlockInfo;
|
|
84
|
+
type index_FetchBlockByNumberArgs<TFilter> = FetchBlockByNumberArgs<TFilter>;
|
|
85
|
+
type index_FetchBlockByNumberResult<TBlock> = FetchBlockByNumberResult<TBlock>;
|
|
86
|
+
type index_FetchBlockRangeArgs<TFilter> = FetchBlockRangeArgs<TFilter>;
|
|
87
|
+
type index_FetchBlockRangeResult<TBlock> = FetchBlockRangeResult<TBlock>;
|
|
88
|
+
type index_FetchBlockResult<TBlock> = FetchBlockResult<TBlock>;
|
|
89
|
+
type index_FetchCursorArgs = FetchCursorArgs;
|
|
90
|
+
type index_RpcClient<TFilter, TBlock> = RpcClient<TFilter, TBlock>;
|
|
91
|
+
declare const index_RpcClient: typeof RpcClient;
|
|
92
|
+
type index_RpcDataStream<TFilter, TBlock> = RpcDataStream<TFilter, TBlock>;
|
|
93
|
+
declare const index_RpcDataStream: typeof RpcDataStream;
|
|
94
|
+
type index_RpcStreamConfig<TFilter, TBlock> = RpcStreamConfig<TFilter, TBlock>;
|
|
95
|
+
declare const index_RpcStreamConfig: typeof RpcStreamConfig;
|
|
96
|
+
type index_ValidateFilterResult = ValidateFilterResult;
|
|
97
|
+
declare const index_createRpcClient: typeof createRpcClient;
|
|
98
|
+
declare namespace index {
|
|
99
|
+
export { type index_BlockInfo as BlockInfo, type index_FetchBlockByNumberArgs as FetchBlockByNumberArgs, type index_FetchBlockByNumberResult as FetchBlockByNumberResult, type index_FetchBlockRangeArgs as FetchBlockRangeArgs, type index_FetchBlockRangeResult as FetchBlockRangeResult, type index_FetchBlockResult as FetchBlockResult, type index_FetchCursorArgs as FetchCursorArgs, index_RpcClient as RpcClient, index_RpcDataStream as RpcDataStream, index_RpcStreamConfig as RpcStreamConfig, type index_ValidateFilterResult as ValidateFilterResult, index_createRpcClient as createRpcClient };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { type BlockInfo as B, type FetchBlockRangeArgs as F, RpcStreamConfig as R, type ValidateFilterResult as V, type FetchBlockResult as a, type FetchBlockRangeResult as b, type FetchBlockByNumberArgs as c, type FetchBlockByNumberResult as d, type FetchCursorArgs as e, RpcClient as f, createRpcClient as g, RpcDataStream as h, index as i };
|