From a9377467a68c882bb64cfd00270fcafe230ade45 Mon Sep 17 00:00:00 2001 From: Aditya <38064122+bettercallav@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:29:17 +1000 Subject: [PATCH] fix: docs for toError, fromError, replacer and reviver (no longer being used) related: #10 --- src/utils/utils.ts | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index cec0611..8dbcab2 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -216,20 +216,11 @@ function parseJSONRPCMessage( 'Message structure did not match a `JSONRPCMessage`', ); } - -/** - * Replacer function for serialising errors over RPC (used by `JSON.stringify` - * in `fromError`) - * Polykey errors are handled by their inbuilt `toJSON` method , so this only - * serialises other errors - */ - /** - * Serializes Error instances into RPC errors - * Use this on the sending side to send exceptions - * Do not send exceptions to clients you do not trust - * If sending to an agent (rather than a client), set sensitive to true to - * prevent sensitive information from being sent over the network + * Serializes an ErrorRPC instance into a JSONValue object suitable for RPC. + * @param {ErrorRPC} error - The ErrorRPC instance to serialize. + * @param {any} [id] - Optional id for the error object in the RPC response. + * @returns {JSONValue} The serialized ErrorRPC instance. */ function fromError(error: ErrorRPC, id?: any): JSONValue { const data: { [key: string]: JSONValue } = { @@ -267,6 +258,10 @@ const standardErrors = { ErrorRPCRemote, ErrorRPC, }; +/** + * Creates a replacer function that omits a specific key during serialization. + * @returns {Function} The replacer function. + */ const createReplacer = () => { return (keyToRemove) => { return (key, value) => { @@ -300,14 +295,16 @@ const createReplacer = () => { }; }; }; +/** + * The replacer function to customize the serialization process. + */ const replacer = createReplacer(); /** - * Reviver function for deserialising errors sent over RPC (used by - * `JSON.parse` in `toError`) - * The final result returned will always be an error - if the deserialised - * data is of an unknown type then this will be wrapped as an - * `ErrorPolykeyUnknown` + * Reviver function for deserializing errors sent over RPC. + * @param {string} key - The key in the JSON object. + * @param {any} value - The value corresponding to the key in the JSON object. + * @returns {any} The reconstructed error object or the original value. */ function reviver(key: string, value: any): any { // If the value is an error then reconstruct it @@ -368,7 +365,13 @@ function reviver(key: string, value: any): any { return value; } } - +/** + * Deserializes an error response object into an ErrorRPCRemote instance. + * @param {any} errorResponse - The error response object. + * @param {any} [metadata] - Optional metadata for the deserialized error. + * @returns {ErrorRPCRemote} The deserialized ErrorRPCRemote instance. + * @throws {TypeError} If the errorResponse object is invalid. + */ function toError(errorResponse: any, metadata?: any): ErrorRPCRemote { if ( typeof errorResponse !== 'object' ||