Skip to content

Commit

Permalink
chore(core): add contextSpec object shape validation for better debug…
Browse files Browse the repository at this point in the history
…ging (#12616)
  • Loading branch information
HuiSF committed Nov 22, 2023
1 parent 4ac6d53 commit a0f9e26
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
16 changes: 15 additions & 1 deletion packages/core/__tests__/adapterCore/serverContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const mockTokenProvider = {
};
const mockCredentialAndIdentityProvider = {
getCredentialsAndIdentityId: jest.fn(),
clearCredentials: jest.fn(),
clearCredentialsAndIdentityId: jest.fn(),
};

describe('serverContext', () => {
Expand Down Expand Up @@ -91,4 +91,18 @@ describe('serverContext', () => {
);
});
});

describe('passing invalid contextSpec', () => {
it('should throw exception if the contextSpec is invalid', () => {
[
{ bad: 'token' },
{ token: { bad: 'value' } },
{ token: { value: 'bad-value' } },
].forEach(invalidContextSpec => {
expect(() =>
getAmplifyServerContext(invalidContextSpec as any)
).toThrowError('Invalid `contextSpec`.');
});
});
});
});
36 changes: 33 additions & 3 deletions packages/core/src/adapterCore/serverContext/serverContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { AmplifyServer } from './types';
import { serverContextRegistry } from './serverContextRegistry';
import { AmplifyClass } from '../../singleton';
import { LibraryOptions, ResourcesConfig } from '../../singleton/types';
import { AmplifyServerContextError } from '../error';

/**
* Creates an Amplify server context.
Expand Down Expand Up @@ -32,15 +33,19 @@ export const createAmplifyServerContext = (
export const getAmplifyServerContext = (
contextSpec: AmplifyServer.ContextSpec
): AmplifyServer.Context => {
assertContextSpec(contextSpec);
const context = serverContextRegistry.get(contextSpec);

if (context) {
return context;
}

throw new Error(
'Attempted to get the Amplify Server Context that may have been destroyed.'
);
throw new AmplifyServerContextError({
message:
'Attempted to get the Amplify Server Context that may have been destroyed.',
recoverySuggestion:
'Ensure always call Amplify APIs within `runWithAmplifyServerContext` function, and do not attempt to reuse `contextSpec` object.',
});
};

/**
Expand All @@ -52,3 +57,28 @@ export const destroyAmplifyServerContext = (
): void => {
serverContextRegistry.deregister(contextSpec);
};

const assertContextSpec = (contextSpec: AmplifyServer.ContextSpec) => {
let invalid = false;

if (!Object.prototype.hasOwnProperty.call(contextSpec, 'token')) {
invalid = true;
} else if (
!Object.prototype.hasOwnProperty.call(contextSpec.token, 'value')
) {
invalid = true;
} else if (
Object.prototype.toString.call(contextSpec.token.value) !==
'[object Symbol]'
) {
invalid = true;
}

if (invalid) {
throw new AmplifyServerContextError({
message: 'Invalid `contextSpec`.',
recoverySuggestion:
'Ensure to use the `contextSpec` object injected by `runWithAmplifyServerContext` function.',
});
}
};

0 comments on commit a0f9e26

Please sign in to comment.