+23
-4
@@ -34,6 +34,9 @@ 'use strict'; | ||
| return close; | ||
| if (method === "$rejectPendingCalls") { | ||
| return rejectPendingCalls; | ||
| } | ||
| if (method === "$closed") | ||
| return closed; | ||
| if (method === "then" && !eventNames.includes("then") && !("then" in functions)) | ||
| return undefined; | ||
| return void 0; | ||
| const sendEvent = (...args) => { | ||
@@ -53,3 +56,3 @@ post(serialize({ m: method, a: args, t: TYPE_REQUEST })); | ||
| } finally { | ||
| _promise = undefined; | ||
| _promise = void 0; | ||
| } | ||
@@ -82,6 +85,11 @@ } | ||
| }); | ||
| function close(error) { | ||
| function close(customError) { | ||
| closed = true; | ||
| rpcPromiseMap.forEach(({ reject, method }) => { | ||
| reject(error || new Error(`[birpc] rpc is closed, cannot call "${method}"`)); | ||
| const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`); | ||
| if (customError) { | ||
| customError.cause ??= error; | ||
| return reject(customError); | ||
| } | ||
| reject(error); | ||
| }); | ||
@@ -91,2 +99,13 @@ rpcPromiseMap.clear(); | ||
| } | ||
| function rejectPendingCalls(handler) { | ||
| const entries = Array.from(rpcPromiseMap.values()); | ||
| const handlerResults = entries.map(({ method, reject }) => { | ||
| if (!handler) { | ||
| return reject(new Error(`[birpc]: rejected pending call "${method}".`)); | ||
| } | ||
| return handler({ method, reject }); | ||
| }); | ||
| rpcPromiseMap.clear(); | ||
| return handlerResults; | ||
| } | ||
| async function onMessage(data, ...extra) { | ||
@@ -93,0 +112,0 @@ let msg; |
+9
-0
@@ -100,3 +100,5 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never; | ||
| $closed: boolean; | ||
| $rejectPendingCalls: (handler?: PendingCallHandler) => Promise<void>[]; | ||
| }; | ||
| type PendingCallHandler = (options: Pick<PromiseEntry, 'method' | 'reject'>) => void | Promise<void>; | ||
| type BirpcGroupReturn<RemoteFunctions> = { | ||
@@ -111,3 +113,10 @@ [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>; | ||
| } | ||
| interface PromiseEntry { | ||
| resolve: (arg: any) => void; | ||
| reject: (error: any) => void; | ||
| method: string; | ||
| timeoutId?: ReturnType<typeof setTimeout>; | ||
| } | ||
| declare const DEFAULT_TIMEOUT = 60000; | ||
| declare const setTimeout: typeof globalThis.setTimeout; | ||
| declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>; | ||
@@ -114,0 +123,0 @@ declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[]; |
+9
-0
@@ -100,3 +100,5 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never; | ||
| $closed: boolean; | ||
| $rejectPendingCalls: (handler?: PendingCallHandler) => Promise<void>[]; | ||
| }; | ||
| type PendingCallHandler = (options: Pick<PromiseEntry, 'method' | 'reject'>) => void | Promise<void>; | ||
| type BirpcGroupReturn<RemoteFunctions> = { | ||
@@ -111,3 +113,10 @@ [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>; | ||
| } | ||
| interface PromiseEntry { | ||
| resolve: (arg: any) => void; | ||
| reject: (error: any) => void; | ||
| method: string; | ||
| timeoutId?: ReturnType<typeof setTimeout>; | ||
| } | ||
| declare const DEFAULT_TIMEOUT = 60000; | ||
| declare const setTimeout: typeof globalThis.setTimeout; | ||
| declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>; | ||
@@ -114,0 +123,0 @@ declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[]; |
+9
-0
@@ -100,3 +100,5 @@ type ArgumentsType<T> = T extends (...args: infer A) => any ? A : never; | ||
| $closed: boolean; | ||
| $rejectPendingCalls: (handler?: PendingCallHandler) => Promise<void>[]; | ||
| }; | ||
| type PendingCallHandler = (options: Pick<PromiseEntry, 'method' | 'reject'>) => void | Promise<void>; | ||
| type BirpcGroupReturn<RemoteFunctions> = { | ||
@@ -111,3 +113,10 @@ [K in keyof RemoteFunctions]: BirpcGroupFn<RemoteFunctions[K]>; | ||
| } | ||
| interface PromiseEntry { | ||
| resolve: (arg: any) => void; | ||
| reject: (error: any) => void; | ||
| method: string; | ||
| timeoutId?: ReturnType<typeof setTimeout>; | ||
| } | ||
| declare const DEFAULT_TIMEOUT = 60000; | ||
| declare const setTimeout: typeof globalThis.setTimeout; | ||
| declare function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(functions: LocalFunctions, options: BirpcOptions<RemoteFunctions>): BirpcReturn<RemoteFunctions, LocalFunctions>; | ||
@@ -114,0 +123,0 @@ declare function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[]; |
+23
-4
@@ -32,6 +32,9 @@ const TYPE_REQUEST = "q"; | ||
| return close; | ||
| if (method === "$rejectPendingCalls") { | ||
| return rejectPendingCalls; | ||
| } | ||
| if (method === "$closed") | ||
| return closed; | ||
| if (method === "then" && !eventNames.includes("then") && !("then" in functions)) | ||
| return undefined; | ||
| return void 0; | ||
| const sendEvent = (...args) => { | ||
@@ -51,3 +54,3 @@ post(serialize({ m: method, a: args, t: TYPE_REQUEST })); | ||
| } finally { | ||
| _promise = undefined; | ||
| _promise = void 0; | ||
| } | ||
@@ -80,6 +83,11 @@ } | ||
| }); | ||
| function close(error) { | ||
| function close(customError) { | ||
| closed = true; | ||
| rpcPromiseMap.forEach(({ reject, method }) => { | ||
| reject(error || new Error(`[birpc] rpc is closed, cannot call "${method}"`)); | ||
| const error = new Error(`[birpc] rpc is closed, cannot call "${method}"`); | ||
| if (customError) { | ||
| customError.cause ??= error; | ||
| return reject(customError); | ||
| } | ||
| reject(error); | ||
| }); | ||
@@ -89,2 +97,13 @@ rpcPromiseMap.clear(); | ||
| } | ||
| function rejectPendingCalls(handler) { | ||
| const entries = Array.from(rpcPromiseMap.values()); | ||
| const handlerResults = entries.map(({ method, reject }) => { | ||
| if (!handler) { | ||
| return reject(new Error(`[birpc]: rejected pending call "${method}".`)); | ||
| } | ||
| return handler({ method, reject }); | ||
| }); | ||
| rpcPromiseMap.clear(); | ||
| return handlerResults; | ||
| } | ||
| async function onMessage(data, ...extra) { | ||
@@ -91,0 +110,0 @@ let msg; |
+1
-1
| { | ||
| "name": "birpc", | ||
| "type": "module", | ||
| "version": "2.4.0", | ||
| "version": "2.5.0", | ||
| "description": "Message based Two-way remote procedure call", | ||
@@ -6,0 +6,0 @@ "author": "Anthony Fu <anthonyfu117@hotmail.com>", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
34686
7.34%573
8.94%0
-100%