From cb887fb0c15ecd2e1a784c98c4c42ebf1c066f89 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Sat, 25 Nov 2023 13:02:06 +0200 Subject: [PATCH] fix: sandboxing issue --- src/index.ts | 13 +++---------- src/realm.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 src/realm.ts diff --git a/src/index.ts b/src/index.ts index e051b72..0db26e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,4 @@ -import type { BunyanLikeLogger } from './decorator'; -import { Bunyamin } from './decorator'; -import { noopLogger } from './noopLogger'; +import realm from './realm'; export * from './noopLogger'; export * from './traceEventStream'; @@ -8,12 +6,7 @@ export * from './uniteTraceEvents'; export * from './wrapLogger'; export * from './is-debug'; -const threadGroups: any[] = []; -export const bunyamin = new Bunyamin({ logger: noopLogger(), threadGroups }); -export const nobunyamin = new Bunyamin({ - logger: noopLogger(), - threadGroups, - immutable: true, -}); +export const bunyamin = realm.bunyamin; +export const nobunyamin = realm.nobunyamin; export default bunyamin; diff --git a/src/realm.ts b/src/realm.ts new file mode 100644 index 0000000..c38d2cb --- /dev/null +++ b/src/realm.ts @@ -0,0 +1,31 @@ +import type { BunyanLikeLogger } from './decorator'; +import { Bunyamin } from './decorator'; +import { noopLogger } from './noopLogger'; + +type Realm = { + bunyamin: Bunyamin; + nobunyamin: Bunyamin; +}; + +function create() { + const threadGroups: any[] = []; + const bunyamin = new Bunyamin({ logger: noopLogger(), threadGroups }); + const nobunyamin = new Bunyamin({ + logger: noopLogger(), + threadGroups, + immutable: true, + }); + + return { bunyamin, nobunyamin }; +} + +function getCached(): Realm | undefined { + return (globalThis as any).__BUNYAMIN__; +} + +function setCached(realm: Realm) { + (globalThis as any).__BUNYAMIN__ = realm; + return realm; +} + +export default setCached(getCached() ?? create());