Skip to content
Open
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
22 changes: 11 additions & 11 deletions server/ServerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2967,29 +2967,29 @@ export type UnknownFunction = (...args: unknown[]) => unknown
* }
* </code></pre>
*/
// Cache to reuse wrapped functions and avoid creating new instances every call
const withTrimCache = new WeakMap<object, object>();

export function withTrim<CB extends UnknownFunction>(callbackFn: CB, trimArguments= true, trimResult= true, useSignatureFrom?: UnknownFunction): CB {
// Validity checks:
if (typeof callbackFn !== "function") {
throw new Error("Unsupported resource type")
}
if(!isClientCallback(callbackFn)) {
throw new Error("The passed argument is not a client callback function.");
}

const cached = withTrimCache.get(callbackFn as object);
if (cached) return cached as CB;

const clientCallback = callbackFn as any as ClientCallback;

//@ts-ignore
return (...args: unknown[]) => {
const wrapped = (...args: unknown[]) => {
return clientCallback._validateAndCall(args, trimArguments, trimResult, useSignatureFrom);
}
}
};

/**
*
* @param params: pre-computed origin and destination
* @param errorHints Error messages will be added here
* @return if origin and destination are allowed by the "allowedOrigins" option
*/
withTrimCache.set(callbackFn as object, wrapped as object);
return wrapped as CB;
}
export function originIsAllowed(params: { origin?: string, destination?: string, allowedOrigins: AllowedOriginsOptions }, errorHints?: string[]): boolean {
function isSameOrigin() {
return params.destination !== undefined && params.origin !== undefined && (params.origin === params.destination);
Expand Down