Skip to content

Commit 2404922

Browse files
authored
Merge pull request #12 from ARyaskov/dev
v2.3.2
2 parents a8cea32 + 1e02028 commit 2404922

33 files changed

Lines changed: 134 additions & 81 deletions

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@riaskov/nevo-messaging",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"description": "Microservices messaging framework for NestJS with NATS/Kafka/SocketIO/HTTP transport, MessagePack, retry/circuit-breaker, OTel, schema validation, DLQ, idempotency, saga, outbox, metrics, health, DevTools",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -50,7 +50,6 @@
5050
"license": "MIT",
5151
"peerDependencies": {
5252
"@msgpack/msgpack": "^3.1.3",
53-
"@napi-rs/zstd": "^1.0.0",
5453
"@nats-io/jetstream": "^3.0.0",
5554
"@nats-io/nats-core": "^3.0.0",
5655
"@nats-io/transport-node": "^3.0.0",
@@ -145,9 +144,6 @@
145144
},
146145
"cacheable-lookup": {
147146
"optional": true
148-
},
149-
"@napi-rs/zstd": {
150-
"optional": true
151147
}
152148
},
153149
"devDependencies": {

pnpm-lock.yaml

Lines changed: 34 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli/contract.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
import { promises as fs } from "node:fs"
33
import * as path from "node:path"
44
import { randomUUID } from "node:crypto"
5+
import { createRequire } from "node:module"
56
import { generateContractModule } from "./generate"
67
import { NEVO_CONTRACT_METHOD } from "../common/contract"
78
import type { ServiceContract } from "../common/contract"
89

10+
const nodeRequire = createRequire(__filename)
11+
912
interface CliOptions {
1013
transport: "http" | "nats"
1114
service: string
@@ -111,7 +114,7 @@ async function fetchContractNats(opts: CliOptions): Promise<ServiceContract> {
111114
let connect: any
112115
try {
113116
// nats.js v3 split the legacy `nats` package: `connect` lives in transport-node.
114-
connect = require("@nats-io/transport-node").connect
117+
connect = nodeRequire("@nats-io/transport-node").connect
115118
} catch {
116119
throw new Error("Missing optional dependency '@nats-io/transport-node'. Install with: npm i @nats-io/transport-node")
117120
}

src/common/base.client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { randomUUID } from "node:crypto"
22
import {
3-
MessagePayload,
43
MessageRequest,
54
MicroserviceConfig,
65
TransportClientOptions,

src/common/base.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ export abstract class BaseMessageController {
280280
const nowMs = Date.now()
281281
const startMs = nowMs
282282
const { method, uuid, params, meta } = this.extractMessageData(data)
283-
let success = true
283+
const success = true
284284

285285
// Establish a chain context for the duration of this handler so any
286286
// outbound calls picks up the same chain id via AsyncLocalStorage.

src/common/circuit-breaker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { createRequire } from "node:module"
12
import { CircuitOpenError, MessagingError } from "./errors"
23
import { ErrorCode } from "./error-code"
34
import type { CircuitBreakerOptions } from "./types"
45
import { getDevToolsBus, DevToolsBus } from "./devtools"
56
import type { DevToolsRegistry } from "./devtools-registry"
67

8+
const nodeRequire = createRequire(__filename)
9+
710
export type CircuitState = "closed" | "open" | "half-open"
811

912
export interface ResolvedCircuitOptions {
@@ -41,7 +44,7 @@ export class CircuitBreakerRegistry {
4144
if (deps?.registry) {
4245
this.registry = deps.registry
4346
} else {
44-
const { getDevToolsRegistry } = require("./devtools-registry") as typeof import("./devtools-registry")
47+
const { getDevToolsRegistry } = nodeRequire("./devtools-registry") as typeof import("./devtools-registry")
4548
this.registry = getDevToolsRegistry()
4649
}
4750
}

src/common/codec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { createRequire } from "node:module"
12
import { bigIntReplacer, makeBigIntReviver } from "./bigint.utils"
23
import { MessagingError } from "./errors"
34
import { ErrorCode } from "./error-code"
45

6+
const nodeRequire = createRequire(__filename)
7+
58
const legacyReviver = makeBigIntReviver({ acceptLegacy: true })
69

710
export type CodecName = "msgpack" | "json" | "json-fast" | string
@@ -109,7 +112,7 @@ let sharedMsgpack: { encoder: { encode(v: unknown): Uint8Array }; decoder: { dec
109112
function getOrCreateSharedMsgpack(): { encoder: { encode(v: unknown): Uint8Array }; decoder: { decode(b: Uint8Array): unknown } } {
110113
if (sharedMsgpack) return sharedMsgpack
111114
try {
112-
const mp = require("@msgpack/msgpack") as typeof import("@msgpack/msgpack")
115+
const mp = nodeRequire("@msgpack/msgpack") as typeof import("@msgpack/msgpack")
113116
const EncoderCtor = (mp as unknown as { Encoder?: new (opts: object) => { encode(v: unknown): Uint8Array } }).Encoder
114117
const DecoderCtor = (mp as unknown as { Decoder?: new (opts: object) => { decode(b: Uint8Array): unknown } }).Decoder
115118
const opts = { useBigInt64: true }

src/common/compression-worker.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Worker } from "node:worker_threads"
2-
import * as path from "node:path"
32
import * as os from "node:os"
4-
import { getDefaultLogger, NevoLogger } from "./logger"
3+
import { NevoLogger } from "./logger"
54

65
const WORKER_INLINE_SOURCE = `
76
const { parentPort } = require("node:worker_threads")
@@ -84,12 +83,10 @@ export interface CompressionWorkerOptions {
8483
logger?: NevoLogger
8584
}
8685

87-
let logger: NevoLogger | null = null
8886
let cfg: CompressionWorkerOptions = {}
8987

9088
export function configureCompressionWorker(opts: CompressionWorkerOptions): void {
9189
cfg = opts
92-
logger = opts.logger ?? getDefaultLogger().child({ component: "compression-worker" })
9390
if (opts.enabled) getPool(opts.poolSize ?? Math.max(1, Math.min(4, os.cpus().length - 1)))
9491
}
9592

src/common/compression.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import {
66
constants as zlibConstants
77
} from "node:zlib"
88
import { gzip, gunzip, deflate, inflate } from "node:zlib"
9+
import * as nodeZlib from "node:zlib"
10+
import { createRequire } from "node:module"
911
import { promisify } from "node:util"
12+
13+
const nodeRequire = createRequire(__filename)
1014
import type { CompressionOptions } from "./types"
1115
import { PayloadTooLargeError } from "./errors"
1216

@@ -42,22 +46,7 @@ let zstdAsync: { compress: (b: Uint8Array, opts?: object) => Promise<Buffer>; de
4246
function tryLoadZstd(): boolean {
4347
if (zstdSync && zstdAsync) return true
4448
try {
45-
const napi: any = require("@napi-rs/zstd")
46-
if (napi && typeof napi.compressSync === "function") {
47-
zstdSync = {
48-
compress: (b) => Buffer.from(napi.compressSync(Buffer.from(b))),
49-
decompress: (b) => Buffer.from(napi.decompressSync(Buffer.from(b)))
50-
}
51-
zstdAsync = {
52-
compress: async (b) => Buffer.from(await napi.compress(Buffer.from(b))),
53-
decompress: async (b) => Buffer.from(await napi.decompress(Buffer.from(b)))
54-
}
55-
return true
56-
}
57-
} catch {}
58-
try {
59-
const zlib = require("node:zlib") as typeof import("node:zlib")
60-
const z: any = zlib
49+
const z: any = nodeZlib
6150
if (typeof z.zstdCompressSync === "function") {
6251
zstdSync = {
6352
compress: (b, opts) => z.zstdCompressSync(b, opts),
@@ -91,7 +80,7 @@ export async function maybeCompressAsync(buf: Uint8Array, opts: ResolvedCompress
9180
return { data: buf, encoding: "identity" }
9281
}
9382
try {
94-
const { isCompressionWorkerEnabled, compressionWorkerThreshold, workerCompress } = require("./compression-worker") as typeof import("./compression-worker")
83+
const { isCompressionWorkerEnabled, compressionWorkerThreshold, workerCompress } = nodeRequire("./compression-worker") as typeof import("./compression-worker")
9584
if (isCompressionWorkerEnabled() && buf.byteLength >= compressionWorkerThreshold()) {
9685
const algo: "gzip" | "deflate" | "zstd" = opts.algorithm === "zstd" && tryLoadZstd() ? "zstd" : opts.algorithm === "deflate" ? "deflate" : "gzip"
9786
const data = await workerCompress(buf, algo, opts.level)
@@ -219,7 +208,7 @@ export async function maybeDecompressAsync(buf: Uint8Array, encoding?: string, m
219208
async function tryWorkerDecompress(buf: Uint8Array, encoding: "gzip" | "deflate", maxOutputBytes?: number): Promise<Uint8Array | null> {
220209
let mod: typeof import("./compression-worker")
221210
try {
222-
mod = require("./compression-worker") as typeof import("./compression-worker")
211+
mod = nodeRequire("./compression-worker") as typeof import("./compression-worker")
223212
} catch {
224213
return null
225214
}

src/common/contract-poller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { setTimeout as sleep } from "node:timers/promises"
21
import { NEVO_CONTRACT_METHOD, ServiceContract, SchemaDescriptor } from "./contract"
32

43
export const NEVO_CONTRACT_CHANGED_METHOD = "nevo.contract.changed"

0 commit comments

Comments
 (0)