Skip to content

Commit 7b9a4bb

Browse files
committed
fixup: remove node imports from non-test code
1 parent 162f7ee commit 7b9a4bb

File tree

9 files changed

+46
-84
lines changed

9 files changed

+46
-84
lines changed

lib/parsed-policy-statement.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RulesLogic } from "json-logic-js";
2-
import { randomUUID } from "node:crypto";
2+
import { createRandomId } from "./utils/random-id.js";
33
import { PolicyStatement } from "./types.js";
44
import { arrayify } from "./utils/arr.js";
55
import { traverseRulesLogic } from "./utils/logic.js";
@@ -100,7 +100,7 @@ export function parsePolicyStatement(
100100
const { action, constraint, effect } = statement;
101101
const actions = arrayify(action);
102102

103-
const sid = opts.sid ?? statement.sid ?? randomUUID();
103+
const sid = opts.sid ?? statement.sid ?? createRandomId();
104104
const gid = opts.gid;
105105

106106
return {

lib/policy-resolver.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import assert from "node:assert";
2-
import { describe, it, mock } from "node:test";
2+
import { beforeEach, describe, it, mock } from "node:test";
33
import { parsePolicyStatement } from "./parsed-policy-statement.js";
44
import { PolicyResolver } from "./policy-resolver.js";
55
import { IndexedStatementsStore } from "./store/index.js";

lib/policy-resolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import pathExists from "just-has";
22
import jsonLogic from "json-logic-js";
33
import { LRUCache } from "lru-cache";
4-
import { randomUUID } from "node:crypto";
54
import { CachedStatementsStore } from "./store/cached-statements-store.js";
65
import { PolicyStatementStore } from "./store/types.js";
6+
import { createRandomId } from "./utils/random-id.js";
77
import {
88
ParsedPolicyStatement,
99
parsePolicyStatement,
@@ -58,7 +58,7 @@ export class PolicyResolver {
5858
docs.forEach((doc) => {
5959
validator.validate(doc);
6060

61-
const gid = doc.id ?? randomUUID();
61+
const gid = doc.id ?? createRandomId();
6262

6363
const parsed = arrayify(doc.statement).map((statement) =>
6464
parsePolicyStatement(statement),

lib/store/cached-statements-store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { LRUCache } from "lru-cache";
2-
import assert from "node:assert";
32
import { ParsedPolicyStatement } from "../parsed-policy-statement.js";
43
import { TypedEmitter } from "../utils/events.js";
54
import { IndexedStatementsStore } from "./indexed-statements-store.js";
@@ -72,7 +71,9 @@ export class CachedStatementsStore
7271
#mustGet(sid: string): ParsedPolicyStatement {
7372
const statement = this.#store.get(sid);
7473

75-
assert.ok(statement, `no statement with sid '${sid}'`);
74+
if (!statement) {
75+
throw new Error(`no statement with sid '${sid}'`);
76+
}
7677

7778
return statement;
7879
}

lib/store/indexed-statements-store.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import uniq from "just-unique";
2-
import assert from "node:assert";
32
import { ParsedPolicyStatement } from "../parsed-policy-statement.js";
43
import { TypedEmitter } from "../utils/events.js";
54
import {
@@ -154,7 +153,9 @@ export class IndexedStatementsStore
154153
#mustGet(sid: string): ParsedPolicyStatement {
155154
const statement = this.#statements.get(sid);
156155

157-
assert.ok(statement, `no statement with sid '${sid}'`);
156+
if (!statement) {
157+
throw new Error(`no statement with sid '${sid}'`);
158+
}
158159

159160
return statement;
160161
}

lib/utils/events.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import EventEmitter from "node:events";
2-
31
type EventMap = Record<string, unknown>;
42

53
type EventKey<T extends EventMap> = string & keyof T;
@@ -12,20 +10,39 @@ export interface Emitter<T extends EventMap> {
1210
}
1311

1412
export class TypedEmitter<T extends EventMap> implements Emitter<T> {
15-
private emitter = new EventEmitter();
13+
private listeners = new Map<string, Set<EventReceiver<unknown>>>();
14+
1615
on<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>) {
17-
this.emitter.on(eventName, fn);
16+
const listeners =
17+
this.listeners.get(eventName) ?? new Set<EventReceiver<unknown>>();
18+
listeners.add(fn as EventReceiver<unknown>);
19+
this.listeners.set(eventName, listeners);
1820
}
1921

2022
off<K extends EventKey<T>>(eventName: K, fn: EventReceiver<T[K]>) {
21-
this.emitter.off(eventName, fn);
23+
const listeners = this.listeners.get(eventName);
24+
if (!listeners) {
25+
return;
26+
}
27+
28+
listeners.delete(fn as EventReceiver<unknown>);
29+
if (listeners.size === 0) {
30+
this.listeners.delete(eventName);
31+
}
2232
}
2333

2434
emit<K extends EventKey<T>>(eventName: K, params: T[K]) {
25-
this.emitter.emit(eventName, params);
35+
const listeners = this.listeners.get(eventName);
36+
if (!listeners) {
37+
return;
38+
}
39+
40+
for (const listener of listeners) {
41+
listener(params);
42+
}
2643
}
2744

2845
removeAllListeners() {
29-
this.emitter.removeAllListeners();
46+
this.listeners.clear();
3047
}
3148
}

lib/utils/random-id.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function createRandomId(): string {
2+
const cryptoApi = globalThis.crypto;
3+
if (cryptoApi && typeof cryptoApi.randomUUID === "function") {
4+
return cryptoApi.randomUUID();
5+
}
6+
7+
const timestamp = Date.now().toString(36);
8+
const random = Math.random().toString(36).slice(2, 14);
9+
return `${timestamp}-${random}`;
10+
}

package-lock.json

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

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "@tsconfig/node20/tsconfig.json",
33
"compilerOptions": {
44
"resolveJsonModule": true,
5+
"types": ["node"]
56
},
67
"exclude": ["dist"]
78
}

0 commit comments

Comments
 (0)