diff --git a/nats-base-client/authenticator.ts b/nats-base-client/authenticator.ts index 535f297a..9df7ee41 100644 --- a/nats-base-client/authenticator.ts +++ b/nats-base-client/authenticator.ts @@ -14,17 +14,16 @@ */ import { nkeys } from "./nkeys.ts"; import { TD, TE } from "./encoders.ts"; -import { +import type { Auth, Authenticator, - ErrorCode, JwtAuth, - NatsError, NKeyAuth, NoAuth, TokenAuth, UserPass, } from "./core.ts"; +import { ErrorCode, NatsError } from "./core.ts"; export function multiAuthenticator(authenticators: Authenticator[]) { return (nonce?: string): Auth => { diff --git a/nats-base-client/bench.ts b/nats-base-client/bench.ts index d0e93227..de15d34c 100644 --- a/nats-base-client/bench.ts +++ b/nats-base-client/bench.ts @@ -17,7 +17,8 @@ import { Empty } from "./types.ts"; import { nuid } from "./nuid.ts"; import { deferred, Perf } from "./util.ts"; import type { NatsConnectionImpl } from "./nats.ts"; -import { ErrorCode, NatsConnection, NatsError } from "./core.ts"; +import { ErrorCode, NatsError } from "./core.ts"; +import type { NatsConnection } from "./core.ts"; export class Metric { name: string; diff --git a/nats-base-client/core.ts b/nats-base-client/core.ts index 35b9dc36..51f89aa1 100644 --- a/nats-base-client/core.ts +++ b/nats-base-client/core.ts @@ -194,6 +194,8 @@ export class NatsError extends Error { } } +export type MsgCallback = (err: NatsError | null, msg: T) => void; + /** * Subscription Options */ @@ -221,7 +223,7 @@ export interface SubOpts { * @param err * @param msg */ - callback?: (err: NatsError | null, msg: T) => void; + callback?: MsgCallback; } export interface DnsResolveFn { diff --git a/nats-base-client/deno.json b/nats-base-client/deno.json index 79920d36..ddcda428 100644 --- a/nats-base-client/deno.json +++ b/nats-base-client/deno.json @@ -2,12 +2,15 @@ "name": "@nats-io/nats-core", "version": "3.0.0-1", "exports": { - ".": "./src/mod.ts" + ".": "./mod.ts", + "./internal": "./internal_mod.ts" }, "publish": { "include": [ - "./src/**/*", - "jsr.json" + "./**/*" ] + }, + "imports": { + "jsr:@nats-io/nkeys": "jsr:@nats-io/nkeys@1.1.1-1" } } diff --git a/nats-base-client/encoders.ts b/nats-base-client/encoders.ts index d5a49444..8dd989f2 100644 --- a/nats-base-client/encoders.ts +++ b/nats-base-client/encoders.ts @@ -12,10 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export const Empty = new Uint8Array(0); +export const Empty: Uint8Array = new Uint8Array(0); -export const TE = new TextEncoder(); -export const TD = new TextDecoder(); +export const TE: TextEncoder = new TextEncoder(); +export const TD: TextDecoder = new TextDecoder(); function concat(...bufs: Uint8Array[]): Uint8Array { let max = 0; diff --git a/nats-base-client/headers.ts b/nats-base-client/headers.ts index 815ab4a7..07fddbc3 100644 --- a/nats-base-client/headers.ts +++ b/nats-base-client/headers.ts @@ -16,7 +16,8 @@ // Heavily inspired by Golang's https://golang.org/src/net/http/header.go import { TD, TE } from "./encoders.ts"; -import { ErrorCode, Match, MsgHdrs, NatsError } from "./core.ts"; +import type { MsgHdrs } from "./core.ts"; +import { ErrorCode, Match, NatsError } from "./core.ts"; // https://www.ietf.org/rfc/rfc822.txt // 3.1.2. STRUCTURE OF HEADER FIELDS @@ -82,7 +83,7 @@ export class MsgHdrsImpl implements MsgHdrs { this.headers = new Map(); } - [Symbol.iterator]() { + [Symbol.iterator](): IterableIterator<[string, string[]]> { return this.headers.entries(); } @@ -276,7 +277,7 @@ export class MsgHdrsImpl implements MsgHdrs { }); } - get hasError() { + get hasError(): boolean { return this._code >= 300; } diff --git a/nats-base-client/heartbeats.ts b/nats-base-client/heartbeats.ts index e32ef9be..2a56b7a5 100644 --- a/nats-base-client/heartbeats.ts +++ b/nats-base-client/heartbeats.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 The NATS Authors + * Copyright 2020-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,9 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Deferred, deferred } from "./util.ts"; -import { DebugEvents, Status } from "./core.ts"; +import type { Deferred } from "./util.ts"; +import { deferred } from "./util.ts"; +import type { Status } from "./core.ts"; +import { DebugEvents } from "./core.ts"; export interface PH { flush(p?: Deferred): Promise; diff --git a/nats-base-client/ipparser.ts b/nats-base-client/ipparser.ts index f9ca7e83..6082ded3 100644 --- a/nats-base-client/ipparser.ts +++ b/nats-base-client/ipparser.ts @@ -43,7 +43,7 @@ export function ipV4(a: number, b: number, c: number, d: number): Uint8Array { return ip; } -export function isIP(h: string) { +export function isIP(h: string): boolean { return parseIP(h) !== undefined; } diff --git a/nats-base-client/msg.ts b/nats-base-client/msg.ts index 8193554e..6598cf86 100644 --- a/nats-base-client/msg.ts +++ b/nats-base-client/msg.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020-2023 The NATS Authors + * Copyright 2020-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,16 +15,16 @@ import { MsgHdrsImpl } from "./headers.ts"; import type { MsgArg } from "./parser.ts"; import { Empty, TD } from "./encoders.ts"; -import { Codec, JSONCodec } from "./codec.ts"; -import { - ErrorCode, +import type { Codec } from "./codec.ts"; +import { JSONCodec } from "./codec.ts"; +import type { Msg, MsgHdrs, - NatsError, Publisher, RequestInfo, ReviverFn, } from "./core.ts"; +import { ErrorCode, NatsError } from "./core.ts"; export function isRequestError(msg: Msg): NatsError | null { // NATS core only considers errors 503s on messages that have no payload diff --git a/nats-base-client/muxsubscription.ts b/nats-base-client/muxsubscription.ts index 5ce3ffbf..f49bef73 100644 --- a/nats-base-client/muxsubscription.ts +++ b/nats-base-client/muxsubscription.ts @@ -13,7 +13,8 @@ * limitations under the License. */ import { isRequestError } from "./msg.ts"; -import { createInbox, ErrorCode, Msg, NatsError, Request } from "./core.ts"; +import type { Msg, MsgCallback, Request } from "./core.ts"; +import { createInbox, ErrorCode, NatsError } from "./core.ts"; export class MuxSubscription { baseInbox!: string; @@ -82,7 +83,7 @@ export class MuxSubscription { return false; } - dispatcher() { + dispatcher(): MsgCallback { return (err: NatsError | null, m: Msg) => { const token = this.getToken(m); if (token) { diff --git a/nats-base-client/nats.ts b/nats-base-client/nats.ts index caef8363..9f4bdce0 100644 --- a/nats-base-client/nats.ts +++ b/nats-base-client/nats.ts @@ -18,22 +18,21 @@ import { ProtocolHandler, SubscriptionImpl } from "./protocol.ts"; import { Empty } from "./encoders.ts"; import { NatsError } from "./types.ts"; -import type { SemVer } from "./semver.ts"; -import { Features, parseSemVer } from "./semver.ts"; +import type { Features, SemVer } from "./semver.ts"; +import { parseSemVer } from "./semver.ts"; import { parseOptions } from "./options.ts"; import { QueuedIteratorImpl } from "./queued_iterator.ts"; -import { - RequestMany, - RequestManyOptionsInternal, - RequestOne, -} from "./request.ts"; +import { RequestMany, RequestOne } from "./request.ts"; + +import type { RequestManyOptionsInternal } from "./request.ts"; + import { isRequestError } from "./msg.ts"; -import { +import { createInbox, ErrorCode, RequestStrategy } from "./core.ts"; + +import type { ConnectionOptions, Context, - createInbox, - ErrorCode, Msg, NatsConnection, Payload, @@ -41,7 +40,6 @@ import { QueuedIterator, RequestManyOptions, RequestOptions, - RequestStrategy, ServerInfo, Stats, Status, @@ -115,14 +113,14 @@ export class NatsConnectionImpl implements NatsConnection { this.protocol.publish(subject, data, options); } - publishMessage(msg: Msg) { + publishMessage(msg: Msg): void { return this.publish(msg.subject, msg.data, { reply: msg.reply, headers: msg.headers, }); } - respondMessage(msg: Msg) { + respondMessage(msg: Msg): boolean { if (msg.reply) { this.publish(msg.reply, msg.data, { reply: msg.reply, diff --git a/nats-base-client/nkeys.ts b/nats-base-client/nkeys.ts index fe97cc84..3f33d2c6 100644 --- a/nats-base-client/nkeys.ts +++ b/nats-base-client/nkeys.ts @@ -1 +1 @@ -export * as nkeys from "https://raw.githubusercontent.com/nats-io/nkeys.js/v1.0.4/modules/esm/mod.ts"; +export * as nkeys from "jsr:@nats-io/nkeys"; diff --git a/nats-base-client/options.ts b/nats-base-client/options.ts index daea87d3..fb8ab7eb 100644 --- a/nats-base-client/options.ts +++ b/nats-base-client/options.ts @@ -15,20 +15,14 @@ import { extend } from "./util.ts"; import { defaultPort, getResolveFn } from "./transport.ts"; -import { createInbox, ServerInfo } from "./core.ts"; +import type { Authenticator, ConnectionOptions, ServerInfo } from "./core.ts"; +import { createInbox, DEFAULT_HOST, ErrorCode, NatsError } from "./core.ts"; import { multiAuthenticator, noAuthFn, tokenAuthenticator, usernamePasswordAuthenticator, } from "./authenticator.ts"; -import { - Authenticator, - ConnectionOptions, - DEFAULT_HOST, - ErrorCode, - NatsError, -} from "./core.ts"; export const DEFAULT_MAX_RECONNECT_ATTEMPTS = 10; export const DEFAULT_JITTER = 100; diff --git a/nats-base-client/parser.ts b/nats-base-client/parser.ts index f8747fc0..550a9f00 100644 --- a/nats-base-client/parser.ts +++ b/nats-base-client/parser.ts @@ -15,7 +15,7 @@ */ import { DenoBuffer } from "./denobuffer.ts"; import { TD } from "./encoders.ts"; -import { Dispatcher } from "./core.ts"; +import type { Dispatcher } from "./core.ts"; export enum Kind { OK, diff --git a/nats-base-client/protocol.ts b/nats-base-client/protocol.ts index 58bd4761..0fd3910b 100644 --- a/nats-base-client/protocol.ts +++ b/nats-base-client/protocol.ts @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 The NATS Authors + * Copyright 2018-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,38 +13,35 @@ * limitations under the License. */ import { decode, Empty, encode, TE } from "./encoders.ts"; -import { - CR_LF, - CRLF, - getResolveFn, - newTransport, - Transport, -} from "./transport.ts"; -import { Deferred, deferred, delay, extend, Timeout, timeout } from "./util.ts"; +import { CR_LF, CRLF, getResolveFn, newTransport } from "./transport.ts"; +import type { Transport } from "./transport.ts"; +import { deferred, delay, extend, timeout } from "./util.ts"; +import type { Deferred, Timeout } from "./util.ts"; import { DataBuffer } from "./databuffer.ts"; -import { ServerImpl, Servers } from "./servers.ts"; -import { +import { Servers } from "./servers.ts"; +import type { ServerImpl } from "./servers.ts"; +import type { DispatchedFn, IngestionFilterFn, IngestionFilterFnResult, ProtocolFilterFn, - QueuedIteratorImpl, } from "./queued_iterator.ts"; +import { QueuedIteratorImpl } from "./queued_iterator.ts"; import type { MsgHdrsImpl } from "./headers.ts"; import { MuxSubscription } from "./muxsubscription.ts"; -import { Heartbeat, PH } from "./heartbeats.ts"; -import { Kind, MsgArg, Parser, ParserEvent } from "./parser.ts"; +import { Heartbeat } from "./heartbeats.ts"; +import type { PH } from "./heartbeats.ts"; +import type { MsgArg, ParserEvent } from "./parser.ts"; +import { Kind, Parser } from "./parser.ts"; import { MsgImpl } from "./msg.ts"; import { Features, parseSemVer } from "./semver.ts"; -import { +import { DebugEvents, ErrorCode, Events, NatsError } from "./core.ts"; + +import type { Base, ConnectionOptions, - DebugEvents, Dispatcher, - ErrorCode, - Events, Msg, - NatsError, Payload, Publisher, PublishOptions, @@ -56,6 +53,7 @@ import { Subscription, SubscriptionOptions, } from "./core.ts"; + import { DEFAULT_MAX_PING_OUT, DEFAULT_PING_INTERVAL, @@ -691,7 +689,7 @@ export class ProtocolHandler implements Dispatcher { return h; } - static toError(s: string) { + static toError(s: string): NatsError { const t = s ? s.toLowerCase() : ""; if (t.indexOf("permissions violation") !== -1) { const err = new NatsError(s, ErrorCode.PermissionsViolation); @@ -964,7 +962,7 @@ export class ProtocolHandler implements Dispatcher { } } - _subunsub(s: SubscriptionImpl) { + _subunsub(s: SubscriptionImpl): SubscriptionImpl { this._sub(s); if (s.max) { this.unsubscribe(s, s.max); diff --git a/nats-base-client/queued_iterator.ts b/nats-base-client/queued_iterator.ts index dc26d292..17793510 100644 --- a/nats-base-client/queued_iterator.ts +++ b/nats-base-client/queued_iterator.ts @@ -12,8 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Deferred, deferred } from "./util.ts"; -import { ErrorCode, NatsError, QueuedIterator } from "./core.ts"; +import type { Deferred } from "./util.ts"; +import { deferred } from "./util.ts"; +import type { QueuedIterator } from "./core.ts"; +import { ErrorCode, NatsError } from "./core.ts"; export type IngestionFilterFnResult = { ingest: boolean; protocol: boolean }; @@ -83,7 +85,7 @@ export class QueuedIteratorImpl implements QueuedIterator { this.yielding = false; } - [Symbol.asyncIterator]() { + [Symbol.asyncIterator](): AsyncIterator { return this.iterate(); } diff --git a/nats-base-client/request.ts b/nats-base-client/request.ts index 7cc77333..97579224 100644 --- a/nats-base-client/request.ts +++ b/nats-base-client/request.ts @@ -12,18 +12,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Deferred, deferred, Timeout, timeout } from "./util.ts"; -import { MuxSubscription } from "./muxsubscription.ts"; +import type { Deferred, Timeout } from "./util.ts"; +import { deferred, timeout } from "./util.ts"; +import type { MuxSubscription } from "./muxsubscription.ts"; import { nuid } from "./nuid.ts"; -import { - ErrorCode, +import type { Msg, - NatsError, Request, RequestManyOptions, RequestOptions, - RequestStrategy, } from "./core.ts"; +import { ErrorCode, NatsError, RequestStrategy } from "./core.ts"; export class BaseRequest { token: string; diff --git a/nats-base-client/semver.ts b/nats-base-client/semver.ts index 65706ab3..8bb95280 100644 --- a/nats-base-client/semver.ts +++ b/nats-base-client/semver.ts @@ -73,7 +73,7 @@ export class Features { /** * Removes all disabled entries */ - resetDisabled() { + resetDisabled(): void { this.disabled.length = 0; this.update(this.server); } @@ -82,16 +82,16 @@ export class Features { * Disables a particular feature. * @param f */ - disable(f: Feature) { + disable(f: Feature): void { this.disabled.push(f); this.update(this.server); } - isDisabled(f: Feature) { + isDisabled(f: Feature): boolean { return this.disabled.indexOf(f) !== -1; } - update(v: SemVer | string) { + update(v: SemVer | string): void { if (typeof v === "string") { v = parseSemVer(v); } @@ -122,7 +122,7 @@ export class Features { * @param f * @param requires */ - set(f: Feature, requires: string) { + set(f: Feature, requires: string): void { this.features.set(f, { min: requires, ok: compare(this.server, parseSemVer(requires)) >= 0, diff --git a/nats-base-client/servers.ts b/nats-base-client/servers.ts index e7c28205..8aa22f36 100644 --- a/nats-base-client/servers.ts +++ b/nats-base-client/servers.ts @@ -1,5 +1,5 @@ /* - * Copyright 2018-2022 The NATS Authors + * Copyright 2018-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,14 +15,13 @@ import { defaultPort, getUrlParseFn } from "./transport.ts"; import { shuffle } from "./util.ts"; import { isIP } from "./ipparser.ts"; -import { - DEFAULT_HOST, - DEFAULT_PORT, +import type { DnsResolveFn, Server, ServerInfo, ServersChanged, } from "./core.ts"; +import { DEFAULT_HOST, DEFAULT_PORT } from "./core.ts"; export function isIPV4OrHostname(hp: string): boolean { if (hp.indexOf(".") !== -1) { diff --git a/nats-base-client/transport.ts b/nats-base-client/transport.ts index 8466b294..9007889f 100644 --- a/nats-base-client/transport.ts +++ b/nats-base-client/transport.ts @@ -1,5 +1,5 @@ /* - * Copyright 2020-2021 The NATS Authors + * Copyright 2020-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,13 +14,14 @@ */ import { TD } from "./encoders.ts"; -import { +import type { ConnectionOptions, - DEFAULT_PORT, DnsResolveFn, Server, URLParseFn, } from "./core.ts"; + +import { DEFAULT_PORT } from "./core.ts"; import { DataBuffer } from "./databuffer.ts"; let transportConfig: TransportFactory; diff --git a/nats-base-client/typedsub.ts b/nats-base-client/typedsub.ts index 56766c2e..09e55555 100644 --- a/nats-base-client/typedsub.ts +++ b/nats-base-client/typedsub.ts @@ -1,5 +1,5 @@ /* - * Copyright 2021 The NATS Authors + * Copyright 2021-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,25 +12,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import { Deferred, deferred } from "./util.ts"; +import type { Deferred } from "./util.ts"; +import { deferred } from "./util.ts"; import type { DispatchedFn, IngestionFilterFn, ProtocolFilterFn, } from "./queued_iterator.ts"; import { QueuedIteratorImpl } from "./queued_iterator.ts"; -import { - ErrorCode, +import type { Msg, NatsConnection, - NatsError, Sub, SubOpts, Subscription, SubscriptionOptions, } from "./core.ts"; -import { SubscriptionImpl } from "./protocol.ts"; +import { ErrorCode, NatsError } from "./core.ts"; +import type { SubscriptionImpl } from "./protocol.ts"; /** * Converts a NATS message into some other type. Implementers are expected to: diff --git a/nats-base-client/util.ts b/nats-base-client/util.ts index c69fc933..d4de3d5c 100644 --- a/nats-base-client/util.ts +++ b/nats-base-client/util.ts @@ -1,5 +1,5 @@ /* - * Copyright 2018-2023 The NATS Authors + * Copyright 2018-2024 The NATS Authors * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,7 +14,8 @@ */ // deno-lint-ignore-file no-explicit-any import { TD } from "./encoders.ts"; -import { ErrorCode, Nanos, NatsError } from "./core.ts"; +import type { Nanos } from "./core.ts"; +import { ErrorCode, NatsError } from "./core.ts"; export type ValueResult = { isError: false; @@ -298,6 +299,6 @@ export function nanos(millis: number): Nanos { * Convert the specified Nanos into millis * @param ns */ -export function millis(ns: Nanos) { +export function millis(ns: Nanos): number { return Math.floor(ns / 1000000); }