Socket
Socket
Sign inDemoInstall

@metamask/utils

Package Overview
Dependencies
Maintainers
7
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/utils - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

CHANGELOG.md

9

dist/bytes.d.ts

@@ -0,1 +1,2 @@

import { Hex } from './hex';
export declare type Bytes = bigint | number | string | Uint8Array;

@@ -22,3 +23,3 @@ /**

*/
export declare function bytesToHex(bytes: Uint8Array): string;
export declare function bytesToHex(bytes: Uint8Array): Hex;
/**

@@ -68,2 +69,4 @@ * Convert a `Uint8Array` to a `bigint`.

*
* If the value is "0x", an empty `Uint8Array` is returned.
*
* @param value - The hexadecimal string to convert to bytes.

@@ -117,2 +120,6 @@ * @returns The bytes as `Uint8Array`.

*
* This will attempt to guess the type of the value based on its type and
* contents. For more control over the conversion, use the more specific
* conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
*
* If the value is a `string`, and it is prefixed with `0x`, it will be

@@ -119,0 +126,0 @@ * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a

@@ -73,2 +73,5 @@ "use strict";

assertIsBytes(bytes);
if (bytes.length === 0) {
return '0x';
}
const lookupTable = getPrecomputedHexValues();

@@ -144,3 +147,3 @@ const hex = new Array(bytes.length);

assertIsBytes(bytes);
return new TextDecoder(undefined).decode(bytes);
return new TextDecoder().decode(bytes);
}

@@ -152,2 +155,4 @@ exports.bytesToString = bytesToString;

*
* If the value is "0x", an empty `Uint8Array` is returned.
*
* @param value - The hexadecimal string to convert to bytes.

@@ -157,2 +162,7 @@ * @returns The bytes as `Uint8Array`.

function hexToBytes(value) {
var _a;
// "0x" is often used as empty byte array.
if (((_a = value === null || value === void 0 ? void 0 : value.toLowerCase) === null || _a === void 0 ? void 0 : _a.call(value)) === '0x') {
return new Uint8Array();
}
(0, hex_1.assertIsHexString)(value);

@@ -274,2 +284,6 @@ // Remove the `0x` prefix if it exists, and pad the string to have an even

*
* This will attempt to guess the type of the value based on its type and
* contents. For more control over the conversion, use the more specific
* conversion functions, such as {@link hexToBytes} or {@link stringToBytes}.
*
* If the value is a `string`, and it is prefixed with `0x`, it will be

@@ -276,0 +290,0 @@ * interpreted as a hexadecimal string. Otherwise, it will be interpreted as a

@@ -0,1 +1,5 @@

import { Struct } from 'superstruct';
export declare type Hex = `0x${string}`;
export declare const HexStruct: Struct<string, null>;
export declare const StrictHexStruct: Struct<`0x${string}`, null>;
/**

@@ -9,2 +13,10 @@ * Check if a string is a valid hex string.

/**
* Strictly check if a string is a valid hex string. A valid hex string must
* start with the "0x"-prefix.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex string.
*/
export declare function isStrictHexString(value: unknown): value is Hex;
/**
* Assert that a value is a valid hex string.

@@ -17,2 +29,10 @@ *

/**
* Assert that a value is a valid hex string. A valid hex string must start with
* the "0x"-prefix.
*
* @param value - The value to check.
* @throws If the value is not a valid hex string.
*/
export declare function assertIsStrictHexString(value: unknown): asserts value is Hex;
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -24,3 +44,3 @@ * prefix, it is returned as-is.

*/
export declare function add0x(hex: string): string;
export declare function add0x(hex: string): Hex;
/**

@@ -27,0 +47,0 @@ * Remove the `0x`-prefix from a hexadecimal string. If the string doesn't have

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.remove0x = exports.add0x = exports.assertIsHexString = exports.isHexString = void 0;
exports.remove0x = exports.add0x = exports.assertIsStrictHexString = exports.assertIsHexString = exports.isStrictHexString = exports.isHexString = exports.StrictHexStruct = exports.HexStruct = void 0;
const superstruct_1 = require("superstruct");
const assert_1 = require("./assert");
const HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
exports.HexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:0x)?[0-9a-f]+$/iu);
exports.StrictHexStruct = (0, superstruct_1.pattern)((0, superstruct_1.string)(), /^0x[0-9a-f]+$/iu);
/**

@@ -14,6 +15,17 @@ * Check if a string is a valid hex string.

function isHexString(value) {
return (0, superstruct_1.is)(value, HexStruct);
return (0, superstruct_1.is)(value, exports.HexStruct);
}
exports.isHexString = isHexString;
/**
* Strictly check if a string is a valid hex string. A valid hex string must
* start with the "0x"-prefix.
*
* @param value - The value to check.
* @returns Whether the value is a valid hex string.
*/
function isStrictHexString(value) {
return (0, superstruct_1.is)(value, exports.StrictHexStruct);
}
exports.isStrictHexString = isStrictHexString;
/**
* Assert that a value is a valid hex string.

@@ -29,2 +41,13 @@ *

/**
* Assert that a value is a valid hex string. A valid hex string must start with
* the "0x"-prefix.
*
* @param value - The value to check.
* @throws If the value is not a valid hex string.
*/
function assertIsStrictHexString(value) {
(0, assert_1.assert)(isStrictHexString(value), 'Value must be a hexadecimal string, starting with "0x".');
}
exports.assertIsStrictHexString = assertIsStrictHexString;
/**
* Add the `0x`-prefix to a hexadecimal string. If the string already has the

@@ -37,5 +60,8 @@ * prefix, it is returned as-is.

function add0x(hex) {
if (hex.startsWith('0x') || hex.startsWith('0X')) {
if (hex.startsWith('0x')) {
return hex;
}
if (hex.startsWith('0X')) {
return `0x${hex.substring(2)}`;
}
return `0x${hex}`;

@@ -42,0 +68,0 @@ }

export * from './assert';
export * from './bytes';
export * from './coercers';
export * from './collections';

@@ -8,2 +9,3 @@ export * from './hex';

export * from './misc';
export * from './number';
export * from './time';

@@ -19,2 +19,3 @@ "use strict";

__exportStar(require("./bytes"), exports);
__exportStar(require("./coercers"), exports);
__exportStar(require("./collections"), exports);

@@ -25,3 +26,4 @@ __exportStar(require("./hex"), exports);

__exportStar(require("./misc"), exports);
__exportStar(require("./number"), exports);
__exportStar(require("./time"), exports);
//# sourceMappingURL=index.js.map

@@ -94,4 +94,4 @@ import { Infer, Struct } from 'superstruct';

/**
* Type guard to narrow a JSON-RPC request or notification object to a
* notification.
* Type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcNotification}.
*

@@ -103,4 +103,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* notification.
* Assertion type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcNotification}.
*

@@ -111,3 +111,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Type guard to narrow a JSON-RPC request or notification object to a request.
* Type guard to narrow a {@link JsonRpcRequest} or @link JsonRpcNotification}
* object to a {@link JsonRpcRequest}.
*

@@ -119,4 +120,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* request.
* Assertion type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcRequest}.
*

@@ -126,2 +127,34 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

export declare function assertIsJsonRpcRequest(requestOrNotification: unknown): asserts requestOrNotification is JsonRpcRequest<JsonRpcParams>;
export declare const PendingJsonRpcResponseStruct: Struct<{
id: string | number | null;
jsonrpc: "2.0";
result: unknown;
error?: {
code: number;
data: unknown;
message: string;
stack?: string | undefined;
} | undefined;
}, {
id: Struct<string | number | null, null>;
jsonrpc: Struct<"2.0", "2.0">;
result: Struct<unknown, null>;
error: Struct<{
code: number;
data: unknown;
message: string;
stack?: string | undefined;
} | undefined, {
code: Struct<number, null>;
message: Struct<string, null>;
data: Struct<unknown, null>;
stack: Struct<string | undefined, null>;
}>;
}>;
/**
* A JSON-RPC response object that has not yet been resolved.
*/
export declare type PendingJsonRpcResponse<Result extends Json> = Omit<Infer<typeof PendingJsonRpcResponseStruct>, 'result'> & {
result?: Result;
};
export declare const JsonRpcSuccessStruct: Struct<{

@@ -172,4 +205,20 @@ id: string | number | null;

/**
* Type guard to check if a value is a JsonRpcResponse.
* Type guard to check whether specified JSON-RPC response is a
* {@link PendingJsonRpcResponse}.
*
* @param response - The JSON-RPC response to check.
* @returns Whether the specified JSON-RPC response is pending.
*/
export declare function isPendingJsonRpcResponse(response: unknown): response is PendingJsonRpcResponse<Json>;
/**
* Assert that the specified JSON-RPC response is a
* {@link PendingJsonRpcResponse}.
*
* @param response - The JSON-RPC response to check.
* @throws If the specified JSON-RPC response is not pending.
*/
export declare function assertIsPendingJsonRpcResponse(response: unknown): asserts response is PendingJsonRpcResponse<Json>;
/**
* Type guard to check if a value is a {@link JsonRpcResponse}.
*
* @param response - The object to check.

@@ -180,3 +229,3 @@ * @returns Whether the object is a JsonRpcResponse.

/**
* Type assertion to check if a value is a JsonRpcResponse.
* Type assertion to check if a value is a {@link JsonRpcResponse}.
*

@@ -187,3 +236,4 @@ * @param response - The response to check.

/**
* Type guard to narrow a JsonRpcResponse object to a success (or failure).
* Type guard to narrow a {@link JsonRpcResponse} object to a success
* (or failure).
*

@@ -195,3 +245,4 @@ * @param response - The response object to check.

/**
* Type assertion to narrow a JsonRpcResponse object to a success (or failure).
* Type assertion to narrow a {@link JsonRpcResponse} object to a success
* (or failure).
*

@@ -202,3 +253,4 @@ * @param response - The response object to check.

/**
* Type guard to narrow a JsonRpcResponse object to a failure (or success).
* Type guard to narrow a {@link JsonRpcResponse} object to a failure
* (or success).
*

@@ -211,3 +263,4 @@ * @param response - The response object to check.

/**
* Type assertion to narrow a JsonRpcResponse object to a failure (or success).
* Type assertion to narrow a {@link JsonRpcResponse} object to a failure
* (or success).
*

@@ -214,0 +267,0 @@ * @param response - The response object to check.

67

dist/json.js

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.validateJsonAndGetSize = exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcResponse = exports.isJsonRpcResponse = exports.JsonRpcResponseStruct = exports.JsonRpcFailureStruct = exports.JsonRpcSuccessStruct = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.JsonRpcNotificationStruct = exports.JsonRpcRequestStruct = exports.JsonRpcParamsStruct = exports.JsonRpcErrorStruct = exports.JsonRpcIdStruct = exports.JsonRpcVersionStruct = exports.jsonrpc2 = exports.isValidJson = exports.JsonStruct = void 0;
exports.validateJsonAndGetSize = exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcResponse = exports.isJsonRpcResponse = exports.assertIsPendingJsonRpcResponse = exports.isPendingJsonRpcResponse = exports.JsonRpcResponseStruct = exports.JsonRpcFailureStruct = exports.JsonRpcSuccessStruct = exports.PendingJsonRpcResponseStruct = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.JsonRpcNotificationStruct = exports.JsonRpcRequestStruct = exports.JsonRpcParamsStruct = exports.JsonRpcErrorStruct = exports.JsonRpcIdStruct = exports.JsonRpcVersionStruct = exports.jsonrpc2 = exports.isValidJson = exports.JsonStruct = void 0;
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));

@@ -66,4 +66,4 @@ const superstruct_1 = require("superstruct");

/**
* Type guard to narrow a JSON-RPC request or notification object to a
* notification.
* Type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcNotification}.
*

@@ -78,4 +78,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* notification.
* Assertion type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcNotification}.
*

@@ -95,3 +95,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Type guard to narrow a JSON-RPC request or notification object to a request.
* Type guard to narrow a {@link JsonRpcRequest} or @link JsonRpcNotification}
* object to a {@link JsonRpcRequest}.
*

@@ -106,4 +107,4 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* request.
* Assertion type guard to narrow a {@link JsonRpcRequest} or
* {@link JsonRpcNotification} object to a {@link JsonRpcRequest}.
*

@@ -122,2 +123,8 @@ * @param requestOrNotification - The JSON-RPC request or notification to check.

exports.assertIsJsonRpcRequest = assertIsJsonRpcRequest;
exports.PendingJsonRpcResponseStruct = (0, superstruct_1.object)({
id: exports.JsonRpcIdStruct,
jsonrpc: exports.JsonRpcVersionStruct,
result: (0, superstruct_1.optional)((0, superstruct_1.unknown)()),
error: (0, superstruct_1.optional)(exports.JsonRpcErrorStruct),
});
exports.JsonRpcSuccessStruct = (0, superstruct_1.object)({

@@ -138,4 +145,32 @@ id: exports.JsonRpcIdStruct,

/**
* Type guard to check if a value is a JsonRpcResponse.
* Type guard to check whether specified JSON-RPC response is a
* {@link PendingJsonRpcResponse}.
*
* @param response - The JSON-RPC response to check.
* @returns Whether the specified JSON-RPC response is pending.
*/
function isPendingJsonRpcResponse(response) {
return (0, superstruct_1.is)(response, exports.PendingJsonRpcResponseStruct);
}
exports.isPendingJsonRpcResponse = isPendingJsonRpcResponse;
/**
* Assert that the specified JSON-RPC response is a
* {@link PendingJsonRpcResponse}.
*
* @param response - The JSON-RPC response to check.
* @throws If the specified JSON-RPC response is not pending.
*/
function assertIsPendingJsonRpcResponse(response) {
try {
(0, superstruct_1.assert)(response, exports.PendingJsonRpcResponseStruct);
}
catch (error) {
const message = isErrorWithMessage(error) ? error.message : error;
throw new Error(`Not a pending JSON-RPC response: ${message}.`);
}
}
exports.assertIsPendingJsonRpcResponse = assertIsPendingJsonRpcResponse;
/**
* Type guard to check if a value is a {@link JsonRpcResponse}.
*
* @param response - The object to check.

@@ -149,3 +184,3 @@ * @returns Whether the object is a JsonRpcResponse.

/**
* Type assertion to check if a value is a JsonRpcResponse.
* Type assertion to check if a value is a {@link JsonRpcResponse}.
*

@@ -165,3 +200,4 @@ * @param response - The response to check.

/**
* Type guard to narrow a JsonRpcResponse object to a success (or failure).
* Type guard to narrow a {@link JsonRpcResponse} object to a success
* (or failure).
*

@@ -176,3 +212,4 @@ * @param response - The response object to check.

/**
* Type assertion to narrow a JsonRpcResponse object to a success (or failure).
* Type assertion to narrow a {@link JsonRpcResponse} object to a success
* (or failure).
*

@@ -192,3 +229,4 @@ * @param response - The response object to check.

/**
* Type guard to narrow a JsonRpcResponse object to a failure (or success).
* Type guard to narrow a {@link JsonRpcResponse} object to a failure
* (or success).
*

@@ -204,3 +242,4 @@ * @param response - The response object to check.

/**
* Type assertion to narrow a JsonRpcResponse object to a failure (or success).
* Type assertion to narrow a {@link JsonRpcResponse} object to a failure
* (or success).
*

@@ -207,0 +246,0 @@ * @param response - The response object to check.

{
"name": "@metamask/utils",
"version": "3.1.0",
"version": "3.2.0",
"description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase.",

@@ -25,3 +25,5 @@ "repository": {

"prepublishOnly": "yarn build:clean && yarn lint && yarn test",
"test": "jest",
"test": "yarn test:source && yarn test:types",
"test:source": "jest",
"test:types": "tsd",
"test:watch": "jest --watch"

@@ -62,2 +64,3 @@ },

"ts-jest": "^28.0.8",
"tsd": "^0.24.1",
"typedoc": "^0.23.10",

@@ -78,3 +81,6 @@ "typescript": "~4.7.4"

}
},
"tsd": {
"directory": "src"
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc