Skip to content

Commit

Permalink
[FIX] added fix from #701
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed May 30, 2024
1 parent b3e1e13 commit 3e5e9e2
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
37 changes: 17 additions & 20 deletions bin/cjs-fix-imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseArgs } from "jsr:@std/[email protected]/parse-args"
import { parseArgs } from "jsr:@std/[email protected]/parse-args";
import {
basename,
extname,
Expand All @@ -7,21 +7,19 @@ import {
} from "https://deno.land/[email protected]/path/mod.ts";

const argv = parseArgs(
Deno.args,
{
alias: {
o: ["out"],
},
boolean: true,
string: ["out"],
default: {
o: "lib",
},
Deno.args,
{
alias: {
o: ["out"],
},
boolean: true,
string: ["out"],
default: {
o: "lib",
},
},
);



// resolve the specified directories to fq
let dirs = (argv._ as string[]).map((n) => {
return resolve(n);
Expand Down Expand Up @@ -51,27 +49,26 @@ if (argv.debug) {

if (!dirs.length || argv.h || argv.help) {
console.log(
`deno run --allow-all cjs-fix-imports [--debug] [--out build/] dir/ dir2/`,
`deno run --allow-all cjs-fix-imports [--debug] [--out build/] dir/ dir2/`,
);
Deno.exit(1);
}

// create out if not exist
await Deno.lstat(out)
.catch(async () => {
await Deno.mkdir(out);
});
.catch(async () => {
await Deno.mkdir(out);
});

// process each file - remove extensions from requires/import
for (const fn of files) {
const data = await Deno.readFile(fn);
let txt = new TextDecoder().decode(data);


let mod = txt.replace(/jsr:@nats-io\/nkeys/gim, "nkeys.js");
mod = mod.replace(/jsr:@nats-io\/nuid/gim, "nuid");
mod = mod.replace(/jsr:@nats-io\/nats-core/gim, "@nats-io/nats-core")
if(!fn.endsWith("nkeys.ts") && !fn.endsWith("nuid.ts")) {
mod = mod.replace(/jsr:@nats-io\/nats-core/gim, "@nats-io/nats-core");
if (!fn.endsWith("nkeys.ts") && !fn.endsWith("nuid.ts")) {
mod = mod.replace(/from\s+"(\S+).[t|j]s"/gim, 'from "$1"');
}

Expand Down
2 changes: 1 addition & 1 deletion nats-base-client/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class NatsError extends Error {
message: string;
// TODO: on major version this should change to a number/enum
code: string;
permissionContext?: { operation: string; subject: string };
permissionContext?: { operation: string; subject: string; queue?: string };
chainedError?: Error;
// these are for supporting jetstream
api_error?: ApiError;
Expand Down
8 changes: 7 additions & 1 deletion nats-base-client/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export class Subscriptions {
let sub;
if (ctx.operation === "subscription") {
sub = subs.find((s) => {
return s.subject === ctx.subject;
return s.subject === ctx.subject && s.queue === ctx.queue;
});
}
if (ctx.operation === "publish") {
Expand Down Expand Up @@ -696,7 +696,13 @@ export class ProtocolHandler implements Dispatcher<ParserEvent> {
err.permissionContext = {
operation: m[1].toLowerCase(),
subject: m[2],
queue: undefined,
};

const qm = s.match(/using queue "(\S+)"/);
if (qm) {
err.permissionContext.queue = qm[1];
}
}
return err;
} else if (t.indexOf("authorization violation") !== -1) {
Expand Down
49 changes: 49 additions & 0 deletions nats-base-client/tests/auth_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
assertArrayIncludes,
assertEquals,
assertRejects,
assertStringIncludes,
fail,
} from "jsr:@std/assert";
import { connect } from "./connect.ts";
Expand Down Expand Up @@ -1257,3 +1258,51 @@ Deno.test("auth - request context", async () => {

await cleanup(ns, nc, a);
});

Deno.test("auth - sub permission reload", async () => {
const conf = {
authorization: {
users: [{
user: "a",
password: "a",
permissions: { subscribe: ["q A", "h"] },
}],
},
};

const { ns, nc } = await _setup(connect, conf, {
user: "a",
pass: "a",
debug: true,
});

const qA = deferred();
nc.subscribe("q", {
queue: "A",
callback: (err, msg) => {
if (err) {
qA.reject(err);
}
},
});

const qBad = deferred<NatsError>();
nc.subscribe("q", {
queue: "bad",
callback: (err, msg) => {
if (err) {
qBad.resolve(err);
}
},
});
await nc.flush();

const err = await qBad;
qA.resolve();

await qA;

assertEquals(err.code, ErrorCode.PermissionsViolation);
assertStringIncludes(err.message, 'using queue "bad"');
await cleanup(ns, nc);
});
6 changes: 5 additions & 1 deletion test_helpers/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,11 @@ export function toConf(o: any, indent?: string): string {
buf.push(`${pad}${k}: ${v}`);
}
} else {
buf.push(pad + v);
if(v.includes(" ")) {
buf.push(`${pad}"${v}"`)
} else {
buf.push(pad + v);
}
}
}
}
Expand Down

0 comments on commit 3e5e9e2

Please sign in to comment.