Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core,toolkit): relocate customJwt local vm handler #6524

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/core/src/libraries/jwt-customizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { buildErrorResponse, runScriptFunctionInLocalVm } from '@logto/core-kit/custom-jwt';
import {
LogtoJwtTokenKeyType,
jwtCustomizerUserContextGuard,
Expand All @@ -22,6 +21,8 @@
import {
LocalVmError,
getJwtCustomizerScripts,
runScriptFunctionInLocalVm,
buildLocalVmErrorBody,
type CustomJwtDeployRequestBody,
} from '#src/utils/custom-jwt/index.js';

Expand Down Expand Up @@ -53,13 +54,13 @@
}

throw new LocalVmError(
buildErrorResponse(error),
buildLocalVmErrorBody(error),
error instanceof SyntaxError || error instanceof TypeError ? 422 : 500
);
}
}

constructor(

Check warning on line 63 in packages/core/src/libraries/jwt-customizer.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/core/src/libraries/jwt-customizer.ts#L63

[max-params] Constructor has too many parameters (5). Maximum allowed is 4.
private readonly queries: Queries,
private readonly logtoConfigs: LogtoConfigLibrary,
private readonly cloudConnection: CloudConnectionLibrary,
Expand Down
10 changes: 0 additions & 10 deletions packages/core/src/utils/custom-jwt/custom-jwt.ts

This file was deleted.

14 changes: 12 additions & 2 deletions packages/core/src/utils/custom-jwt/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export * from './custom-jwt.js';
import { LogtoJwtTokenKey, type JwtCustomizerType } from '@logto/schemas';

import { type CustomJwtDeployRequestBody } from './types.js';

export * from './types.js';
export * from './local-vm-error.js';
export * from './local-vm.js';

export const getJwtCustomizerScripts = (jwtCustomizers: Partial<JwtCustomizerType>) => {
// eslint-disable-next-line no-restricted-syntax -- enable to infer the type using `Object.fromEntries`
return Object.fromEntries(
Object.values(LogtoJwtTokenKey).map((key) => [key, { production: jwtCustomizers[key]?.script }])
) as CustomJwtDeployRequestBody;
};

Check warning on line 13 in packages/core/src/utils/custom-jwt/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/utils/custom-jwt/index.ts#L9-L13

Added lines #L9 - L13 were not covered by tests
17 changes: 0 additions & 17 deletions packages/core/src/utils/custom-jwt/local-vm-error.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { types } from 'node:util';
import { runInNewContext } from 'node:vm';

import { ResponseError } from '@withtyped/client';

/**
* Extend the ResponseError from @withtyped/client.
* This class is used to parse the error from the local VM to the WithTyped client response error.
* So we can unify the error handling and display logic for both local VM and Cloud version.
*/
export class LocalVmError extends ResponseError {
constructor(errorBody: Record<string, unknown>, statusCode: number) {
super(
new Response(
new Blob([JSON.stringify(errorBody)], {
type: 'application/json',
}),
{
status: statusCode,
}
)
);
}
}

/**
* This function is used to execute a named function in a customized code script in a local
* virtual machine with the given payload as input.
Expand Down Expand Up @@ -39,3 +62,18 @@ export const runScriptFunctionInLocalVm = async (

return result;
};

/**
* Build the error body for the local VM error.
*
* @remarks
*
* Catch the error from vm module, and build the error body.
* Use `isNativeError` to check if the error is an instance of `Error`.
* If the error comes from `node:vm` module, then it will not be an instance of `Error` but can be captured by `isNativeError`.
*
*/
export const buildLocalVmErrorBody = (error: unknown) =>
types.isNativeError(error)
? { message: error.message, stack: error.stack }
: { message: String(error) };
10 changes: 0 additions & 10 deletions packages/toolkit/core-kit/src/custom-jwt/error-handling.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/toolkit/core-kit/src/custom-jwt/index.ts

This file was deleted.

Loading