Skip to content

Commit c25ad3e

Browse files
committed
use hooks manager as a private field
1 parent c3e0680 commit c25ad3e

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

packages/bruno-js/src/bru.js

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const { jar: createCookieJar } = require('@usebruno/requests').cookies;
66

77
const variableNameRegex = /^[\w-.]*$/;
88

9-
// Private WeakMap to store HookManager instances - truly private, not accessible from outside
10-
const hookManagerStore = new WeakMap();
11-
129
class Bru {
10+
// Private class field - truly private, not accessible from outside the class
11+
#hookManager;
12+
1313
constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, collectionName, promptVariables, hookManager) {
1414
this.envVariables = envVariables || {};
1515
this.runtimeVariables = runtimeVariables || {};
@@ -22,10 +22,8 @@ class Bru {
2222
this.oauth2CredentialVariables = oauth2CredentialVariables || {};
2323
this.collectionPath = collectionPath;
2424
this.collectionName = collectionName;
25-
// Store HookManager in WeakMap - truly private, not accessible via instance properties
26-
if (hookManager) {
27-
hookManagerStore.set(this, hookManager);
28-
}
25+
// Store HookManager in private class field - truly private, not accessible from outside
26+
this.#hookManager = hookManager || null;
2927
this._initializeHooksConvenienceMethods();
3028
this.sendRequest = sendRequest;
3129
this.cookies = {
@@ -291,11 +289,10 @@ class Bru {
291289
/**
292290
* Initialize hooks convenience methods if hookManager is available
293291
* This creates a namespaced hooks object with only the convenience methods
294-
* The HookManager itself is kept private in a WeakMap - truly inaccessible from outside
292+
* The HookManager itself is kept private using a private class field - truly inaccessible from outside
295293
*/
296294
_initializeHooksConvenienceMethods() {
297-
const hookManager = hookManagerStore.get(this);
298-
if (!hookManager) {
295+
if (!this.#hookManager) {
299296
// Create empty hooks object if no hookManager
300297
this.hooks = {
301298
runner: {},
@@ -306,38 +303,34 @@ class Bru {
306303

307304
// Create namespaced hooks object with only convenience methods
308305
// Users cannot access the HookManager directly (no .on() or .call() methods)
309-
// The HookManager is stored in a WeakMap and is truly private
306+
// The HookManager is stored in a private class field and is truly private
310307
this.hooks = {
311308
runner: {
312309
onBeforeCollectionRun: (handler) => {
313-
const hm = hookManagerStore.get(this);
314-
if (!hm) {
310+
if (!this.#hookManager) {
315311
throw new Error('HookManager is not available');
316312
}
317-
return hm.on('runner:beforeCollectionRun', handler);
313+
return this.#hookManager.on('runner:beforeCollectionRun', handler);
318314
},
319315
onAfterCollectionRun: (handler) => {
320-
const hm = hookManagerStore.get(this);
321-
if (!hm) {
316+
if (!this.#hookManager) {
322317
throw new Error('HookManager is not available');
323318
}
324-
return hm.on('runner:afterCollectionRun', handler);
319+
return this.#hookManager.on('runner:afterCollectionRun', handler);
325320
}
326321
},
327322
http: {
328323
onBeforeRequest: (handler) => {
329-
const hm = hookManagerStore.get(this);
330-
if (!hm) {
324+
if (!this.#hookManager) {
331325
throw new Error('HookManager is not available');
332326
}
333-
return hm.on('http:beforeRequest', handler);
327+
return this.#hookManager.on('http:beforeRequest', handler);
334328
},
335329
onAfterResponse: (handler) => {
336-
const hm = hookManagerStore.get(this);
337-
if (!hm) {
330+
if (!this.#hookManager) {
338331
throw new Error('HookManager is not available');
339332
}
340-
return hm.on('http:afterResponse', handler);
333+
return this.#hookManager.on('http:afterResponse', handler);
341334
}
342335
}
343336
};

packages/bruno-tests/hooks-comprehensive-tests/hooks/beforeRequest/async-before-request.bru

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ get {
1111
}
1212

1313
script:hooks {
14-
bru.hooks.http.onBeforeRequest(async({ req }) => {
14+
bru.hooks.http.onBeforeRequest(async ({ req }) => {
1515
console.log('[beforeRequest] Starting async hook...');
1616

1717
// Test: Can use async/await

0 commit comments

Comments
 (0)