mirror of
https://github.com/hanzomsg/nats.js.git
synced 2026-10-30 20:31:36 +00:00
* refactor: replace `parse` with `parseArgs` and update dependencies in examples and configurations - Migrated from `@std/flags` to `@std/cli` across various scripts. - Updated related imports, arguments parsing logic, and versioned dependencies. Signed-off-by: Alberto Ricart <[email protected]> * chore(deps): bump `@types/node` to ^25.0.0 across all packages Signed-off-by: Alberto Ricart <[email protected]> * chore(release): bump all packages to version 3.3.0 Signed-off-by: Alberto Ricart <[email protected]> * chore(workflows): update Deno to 2.6.x, Node to 25.x in CI configurations Signed-off-by: Alberto Ricart <[email protected]> * fix(node_transport): specify `Uint8Array` type for `frame` argument in `socket.on("data")` listener v25, the inferred type changed to string | NonSharedBuffer Signed-off-by: Alberto Ricart <[email protected]> * chore(workflows): update GitHub Actions to use latest versions of `checkout`, `setup-node`, and `setup-deno` Signed-off-by: Alberto Ricart <[email protected]> --------- Signed-off-by: Alberto Ricart <[email protected]>
85 lines
1.9 KiB
TypeScript
Executable File
85 lines
1.9 KiB
TypeScript
Executable File
#!/usr/bin/env deno run --allow-all --unstable
|
|
|
|
import { parseArgs } from "@std/cli";
|
|
import {
|
|
connect,
|
|
type ConnectionOptions,
|
|
credsAuthenticator,
|
|
delay,
|
|
} from "@nats-io/transport-deno";
|
|
|
|
const argv = parseArgs(
|
|
Deno.args,
|
|
{
|
|
alias: {
|
|
"s": ["server"],
|
|
"c": ["count"],
|
|
"i": ["interval"],
|
|
"t": ["timeout"],
|
|
"f": ["creds"],
|
|
},
|
|
default: {
|
|
s: "127.0.0.1:4222",
|
|
c: 1,
|
|
i: 0,
|
|
t: 1000,
|
|
},
|
|
boolean: ["debug"],
|
|
string: ["server", "count", "interval", "headers", "creds"],
|
|
},
|
|
);
|
|
|
|
const opts = { servers: argv.s } as ConnectionOptions;
|
|
const subject = String(argv._[0]);
|
|
const payload = String(argv._[1]) || "";
|
|
const count =
|
|
((argv.c == -1 ? Number.MAX_SAFE_INTEGER : argv.c) || 1) as number;
|
|
const interval = (argv.i || 0) as number;
|
|
|
|
if (argv.debug) {
|
|
opts.debug = true;
|
|
}
|
|
|
|
if (argv.h || argv.help || !subject) {
|
|
console.log(
|
|
"Usage: nats-pub [-s server] [--creds=/path/file.creds] [-c <count>=1] [-t <timeout>=1000] [-i <interval>=0] [--headers] subject [msg]",
|
|
);
|
|
console.log("to request forever, specify -c=-1 or --count=-1");
|
|
Deno.exit(1);
|
|
}
|
|
|
|
if (argv.creds) {
|
|
const data = await Deno.readFile(argv.creds);
|
|
opts.authenticator = credsAuthenticator(data);
|
|
}
|
|
|
|
const nc = await connect(opts);
|
|
nc.closed()
|
|
.then((err) => {
|
|
if (err) {
|
|
console.error(`closed with an error: ${err.message}`);
|
|
}
|
|
});
|
|
|
|
for (let i = 1; i <= count; i++) {
|
|
await nc.request(subject, payload, { timeout: argv.t as number })
|
|
.then((m) => {
|
|
console.log(`[${i}]: ${m.string()}`);
|
|
if (argv.headers && m.headers) {
|
|
const h = [];
|
|
for (const [key, value] of m.headers) {
|
|
h.push(`${key}=${value}`);
|
|
}
|
|
console.log(`\t${h.join(";")}`);
|
|
}
|
|
})
|
|
.catch((err) => {
|
|
console.log(`[${i}]: request failed: ${err.message}`);
|
|
});
|
|
if (interval) {
|
|
await delay(interval);
|
|
}
|
|
}
|
|
await nc.flush();
|
|
await nc.close();
|