Skip to content

Commit

Permalink
fix: symbol key access and explicit constructor error (#113)
Browse files Browse the repository at this point in the history
* When accessing symbol keys without context, an error is no longer thrown.
* When creating ClsService without passing als, an error is thrown immediately.
  • Loading branch information
Papooch authored Jan 29, 2024
1 parent 653a167 commit 0d4e97b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
26 changes: 26 additions & 0 deletions packages/core/src/lib/cls.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ describe('ClsService', () => {
expect(service.has(sym)).toEqual(true);
});
});

it('checks key presence without context (string key)', () => {
expect(service.has('d')).toEqual(false);
});

it('checks key presence without context (symbol key)', () => {
expect(service.has(Symbol('sym'))).toEqual(false);
});
});

describe('edge cases', () => {
Expand All @@ -153,6 +161,18 @@ describe('ClsService', () => {
});
});

it('returns undefined if trying to get a value without context (string key)', () => {
expect(service.get('key')).toBeUndefined();
});

it('returns undefined if trying to get a value without context (symbol key)', () => {
expect(service.get(Symbol('xx'))).toBeUndefined();
});

it('returns undefined if trying to get request ID without context (symbol key)', () => {
expect(service.getId()).toBeUndefined();
});

it('throws error if trying to set a value without context', () => {
expect(() => service.set('key', 123)).toThrowError();
});
Expand Down Expand Up @@ -233,6 +253,12 @@ describe('ClsService', () => {
});
});

describe('manual creation', () => {
it('should fail with error', () => {
expect(() => new ClsService(undefined as any)).toThrowError();
});
});

describe('typing', () => {
interface IStore extends ClsStore {
a: string;
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/lib/cls.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import { CLS_ID } from './cls.constants';
import { ClsContextOptions, ClsStore } from './cls.options';

export class ClsService<S extends ClsStore = ClsStore> {
constructor(private readonly als: AsyncLocalStorage<any>) {}
constructor(private readonly als: AsyncLocalStorage<any>) {
if (!als) {
throw new Error(
`Cannot create ClsService because no AsyncLocalStorage instance was provided.\nPlease make sure that ClsService is only provided by the ClsModule and not constructed manually or added to the providers array.`,
);
}
}

/**
* Set (or overrides) a value on the CLS context.
Expand Down Expand Up @@ -79,7 +85,7 @@ export class ClsService<S extends ClsStore = ClsStore> {
const store = this.als.getStore();
if (!key) return store;
if (typeof key === 'symbol') {
return store[key];
return store?.[key];
}
return getValueFromPath(store as S, key as any) as any;
}
Expand All @@ -95,7 +101,7 @@ export class ClsService<S extends ClsStore = ClsStore> {
has(key: string | symbol): boolean {
const store = this.als.getStore();
if (typeof key === 'symbol') {
return store[key] !== undefined;
return store?.[key] !== undefined;
}
return getValueFromPath(store as S, key as any) !== undefined;
}
Expand Down

0 comments on commit 0d4e97b

Please sign in to comment.