Comparing version 0.1.7-alpha.0 to 0.1.7
import { encode, decode } from "@msgpack/msgpack"; | ||
import { isErrorLike } from "../lib/isErrorLike.js"; | ||
import { isPlainObject } from "../lib/isPlainObject.js"; | ||
import { isR19ErrorLike } from "../lib/isR19ErrorLike.js"; | ||
import { replaceLeaves } from "../lib/replaceLeaves.js"; | ||
import { isErrorLike } from "../lib/isErrorLike.js"; | ||
import { R19Error } from "../R19Error.js"; | ||
const createArbitrarilyNestedFunction = (handler, path = []) => { | ||
@@ -19,4 +22,11 @@ return new Proxy(() => void 0, { | ||
const resolvedFetch = args.fetch || globalThis.fetch.bind(globalThis); | ||
return createArbitrarilyNestedFunction(async (path, fnArgs) => { | ||
const preparedProcedureArgs = await replaceLeaves(fnArgs[0], async (value) => { | ||
return createArbitrarilyNestedFunction(async (procedurePath, fnArgs) => { | ||
const procedureArgs = fnArgs[0]; | ||
if (procedureArgs !== void 0 && !isPlainObject(procedureArgs)) { | ||
throw new R19Error("r19 only supports a single object procedure argument, but something else was provided.", { | ||
procedurePath, | ||
procedureArgs | ||
}); | ||
} | ||
const preparedProcedureArgs = await replaceLeaves(procedureArgs, async (value) => { | ||
if (value instanceof Blob) { | ||
@@ -26,3 +36,6 @@ return new Uint8Array(await value.arrayBuffer()); | ||
if (typeof value === "function") { | ||
throw new Error("r19 does not support function arguments."); | ||
throw new R19Error("r19 does not support function arguments.", { | ||
procedurePath, | ||
procedureArgs | ||
}); | ||
} | ||
@@ -32,3 +45,3 @@ return value; | ||
const body = encode({ | ||
procedurePath: path, | ||
procedurePath, | ||
procedureArgs: preparedProcedureArgs | ||
@@ -47,3 +60,10 @@ }, { ignoreUndefined: true }); | ||
const resError = resObject.error; | ||
if (isErrorLike(resError)) { | ||
if (isR19ErrorLike(resError)) { | ||
const error = new R19Error(resError.message, { | ||
procedurePath, | ||
procedureArgs | ||
}); | ||
error.stack = resError.stack; | ||
throw error; | ||
} else if (isErrorLike(resError)) { | ||
const error = new Error(resError.message); | ||
@@ -54,3 +74,5 @@ error.name = resError.name; | ||
} else { | ||
throw new Error("An unexpected response was received from the RPC server.", { | ||
throw new R19Error("An unexpected response was received from the RPC server.", { | ||
procedurePath, | ||
procedureArgs, | ||
cause: resObject | ||
@@ -57,0 +79,0 @@ }); |
import { Buffer } from "node:buffer"; | ||
import { decode, encode } from "@msgpack/msgpack"; | ||
import { isErrorLike } from "./lib/isErrorLike.js"; | ||
import { isR19ErrorLike } from "./lib/isR19ErrorLike.js"; | ||
import { replaceLeaves } from "./lib/replaceLeaves.js"; | ||
import { R19Error } from "./R19Error.js"; | ||
const findProcedure = (procedures, path) => { | ||
@@ -31,11 +33,10 @@ path = [...path]; | ||
if (!procedure) { | ||
const rawBody = { | ||
name: "RPCError", | ||
message: `Invalid procedure name: ${clientArgs.procedurePath.join(".")}` | ||
}; | ||
const body = encode(rawBody); | ||
(_a = args.onError) == null ? void 0 : _a.call(args, { | ||
error: new Error(`${rawBody.name}: ${rawBody.message}`), | ||
...clientArgs | ||
const error = new R19Error(`Invalid procedure name: ${clientArgs.procedurePath.join(".")}`, { | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs | ||
}); | ||
const body = encode({ | ||
error | ||
}, { ignoreUndefined: true }); | ||
(_a = args.onError) == null ? void 0 : _a.call(args, { error, ...clientArgs }); | ||
return { | ||
@@ -57,5 +58,3 @@ body, | ||
res = await replaceLeaves(res, async (value) => { | ||
var _a2; | ||
if (isErrorLike(value)) { | ||
(_a2 = args.onError) == null ? void 0 : _a2.call(args, { error: value, ...clientArgs }); | ||
return { | ||
@@ -68,3 +67,6 @@ name: value.name, | ||
if (typeof value === "function") { | ||
throw new Error("r19 does not support function return values."); | ||
throw new R19Error("r19 does not support function return values.", { | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs | ||
}); | ||
} | ||
@@ -76,3 +78,3 @@ return value; | ||
const body = encode({ | ||
error: { | ||
error: isR19ErrorLike(error) ? error : { | ||
name: error.name, | ||
@@ -102,9 +104,9 @@ message: error.message, | ||
if (error instanceof Error) { | ||
console.error(error); | ||
const body = encode({ | ||
error: { | ||
name: "RPCError", | ||
message: "Unable to serialize server response. Check the server log for details." | ||
} | ||
const rpcError = new R19Error("Unable to serialize server response. Check the server log for details.", { | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs, | ||
cause: error | ||
}); | ||
console.error(rpcError); | ||
const body = encode(rpcError); | ||
(_c = args.onError) == null ? void 0 : _c.call(args, { error, ...clientArgs }); | ||
@@ -111,0 +113,0 @@ return { |
export { CreateRPCMiddlewareArgs, RPCMiddleware, createRPCMiddleware, } from "./createRPCMiddleware"; | ||
export { ProceduresFromInstance, proceduresFromInstance, OmittableProcedures, } from "./proceduresFromInstance"; | ||
export { handleRPCRequest } from "./handleRPCRequest"; | ||
export { R19Error } from "./R19Error"; | ||
export { Procedure, Procedures, ExtractProcedures, OnErrorEventHandler, } from "./types"; |
import { createRPCMiddleware } from "./createRPCMiddleware.js"; | ||
import { proceduresFromInstance } from "./proceduresFromInstance.js"; | ||
import { handleRPCRequest } from "./handleRPCRequest.js"; | ||
import { R19Error } from "./R19Error.js"; | ||
export { | ||
R19Error, | ||
createRPCMiddleware, | ||
@@ -6,0 +8,0 @@ handleRPCRequest, |
@@ -5,3 +5,4 @@ export type ErrorLike = { | ||
stack?: string; | ||
cause?: unknown; | ||
}; | ||
export declare const isErrorLike: (error: unknown) => error is ErrorLike; |
{ | ||
"name": "r19", | ||
"version": "0.1.7-alpha.0", | ||
"version": "0.1.7", | ||
"description": "Simple remote procedure calls in TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -40,3 +40,3 @@ # r19 | ||
// An optional error event handler | ||
onError: ({ error, procedurePath, procedureArgs }) => { ... } | ||
onError: ({ error, procedurePath, procedureArgs }) => { ... }, | ||
}); | ||
@@ -43,0 +43,0 @@ |
import { encode, decode } from "@msgpack/msgpack"; | ||
import { isErrorLike } from "../lib/isErrorLike"; | ||
import { isPlainObject } from "../lib/isPlainObject"; | ||
import { isR19ErrorLike } from "../lib/isR19ErrorLike"; | ||
import { replaceLeaves } from "../lib/replaceLeaves"; | ||
import { isErrorLike } from "../lib/isErrorLike"; | ||
import { Procedures, Procedure, ProcedureCallServerResponse } from "../types"; | ||
import { R19Error } from "../R19Error"; | ||
@@ -95,5 +98,17 @@ const createArbitrarilyNestedFunction = <T>( | ||
return createArbitrarilyNestedFunction(async (path, fnArgs) => { | ||
return createArbitrarilyNestedFunction(async (procedurePath, fnArgs) => { | ||
const procedureArgs = fnArgs[0] as Record<string, unknown>; | ||
if (procedureArgs !== undefined && !isPlainObject(procedureArgs)) { | ||
throw new R19Error( | ||
"r19 only supports a single object procedure argument, but something else was provided.", | ||
{ | ||
procedurePath, | ||
procedureArgs, | ||
}, | ||
); | ||
} | ||
const preparedProcedureArgs = await replaceLeaves( | ||
fnArgs[0], | ||
procedureArgs, | ||
async (value) => { | ||
@@ -105,3 +120,6 @@ if (value instanceof Blob) { | ||
if (typeof value === "function") { | ||
throw new Error("r19 does not support function arguments."); | ||
throw new R19Error("r19 does not support function arguments.", { | ||
procedurePath, | ||
procedureArgs, | ||
}); | ||
} | ||
@@ -115,3 +133,3 @@ | ||
{ | ||
procedurePath: path, | ||
procedurePath: procedurePath, | ||
procedureArgs: preparedProcedureArgs, | ||
@@ -138,3 +156,11 @@ }, | ||
if (isErrorLike(resError)) { | ||
if (isR19ErrorLike(resError)) { | ||
const error = new R19Error(resError.message, { | ||
procedurePath, | ||
procedureArgs, | ||
}); | ||
error.stack = resError.stack; | ||
throw error; | ||
} else if (isErrorLike(resError)) { | ||
const error = new Error(resError.message); | ||
@@ -146,5 +172,7 @@ error.name = resError.name; | ||
} else { | ||
throw new Error( | ||
throw new R19Error( | ||
"An unexpected response was received from the RPC server.", | ||
{ | ||
procedurePath, | ||
procedureArgs, | ||
cause: resObject, | ||
@@ -151,0 +179,0 @@ }, |
@@ -5,2 +5,3 @@ import { Buffer } from "node:buffer"; | ||
import { isErrorLike } from "./lib/isErrorLike"; | ||
import { isR19ErrorLike } from "./lib/isR19ErrorLike"; | ||
import { replaceLeaves } from "./lib/replaceLeaves"; | ||
@@ -14,2 +15,3 @@ | ||
} from "./types"; | ||
import { R19Error } from "./R19Error"; | ||
@@ -71,13 +73,19 @@ const findProcedure = ( | ||
if (!procedure) { | ||
const rawBody = { | ||
name: "RPCError", | ||
message: `Invalid procedure name: ${clientArgs.procedurePath.join(".")}`, | ||
}; | ||
const body = encode(rawBody); | ||
const error = new R19Error( | ||
`Invalid procedure name: ${clientArgs.procedurePath.join(".")}`, | ||
{ | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs, | ||
}, | ||
); | ||
args.onError?.({ | ||
error: new Error(`${rawBody.name}: ${rawBody.message}`), | ||
...clientArgs, | ||
}); | ||
const body = encode( | ||
{ | ||
error, | ||
}, | ||
{ ignoreUndefined: true }, | ||
); | ||
args.onError?.({ error, ...clientArgs }); | ||
return { | ||
@@ -108,4 +116,2 @@ body, | ||
if (isErrorLike(value)) { | ||
args.onError?.({ error: value, ...clientArgs }); | ||
return { | ||
@@ -120,3 +126,6 @@ name: value.name, | ||
if (typeof value === "function") { | ||
throw new Error("r19 does not support function return values."); | ||
throw new R19Error("r19 does not support function return values.", { | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs, | ||
}); | ||
} | ||
@@ -130,8 +139,12 @@ | ||
{ | ||
error: { | ||
name: error.name, | ||
message: error.message, | ||
stack: | ||
process.env.NODE_ENV === "development" ? error.stack : undefined, | ||
}, | ||
error: isR19ErrorLike(error) | ||
? error | ||
: { | ||
name: error.name, | ||
message: error.message, | ||
stack: | ||
process.env.NODE_ENV === "development" | ||
? error.stack | ||
: undefined, | ||
}, | ||
}, | ||
@@ -167,12 +180,15 @@ { ignoreUndefined: true }, | ||
if (error instanceof Error) { | ||
console.error(error); | ||
const body = encode({ | ||
error: { | ||
name: "RPCError", | ||
message: | ||
"Unable to serialize server response. Check the server log for details.", | ||
const rpcError = new R19Error( | ||
"Unable to serialize server response. Check the server log for details.", | ||
{ | ||
procedurePath: clientArgs.procedurePath, | ||
procedureArgs: clientArgs.procedureArgs, | ||
cause: error, | ||
}, | ||
}); | ||
); | ||
console.error(rpcError); | ||
const body = encode(rpcError); | ||
args.onError?.({ error, ...clientArgs }); | ||
@@ -179,0 +195,0 @@ |
@@ -15,2 +15,4 @@ export { | ||
export { R19Error } from "./R19Error"; | ||
export { | ||
@@ -17,0 +19,0 @@ Procedure, |
@@ -5,2 +5,3 @@ export type ErrorLike = { | ||
stack?: string; | ||
cause?: unknown; | ||
}; | ||
@@ -7,0 +8,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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.
Found 1 instance in 1 package
123469
71
1538
0