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 1.0.0 to 2.0.0

11

CHANGELOG.md

@@ -9,2 +9,10 @@ # Changelog

## [2.0.0]
### Added
- Add more JSON utils ([#8](https://github.com/MetaMask/utils/pull/8))
### Changed
- **BREAKING:** Refactor and expand time utils ([#9](https://github.com/MetaMask/utils/pull/9))
- Adds a new function, `inMilliseconds`, and moves the time constants into a TypeScript `enum`.
## [1.0.0]

@@ -14,3 +22,4 @@ ### Added

[Unreleased]: https://github.com/MetaMask/utils/compare/v1.0.0...HEAD
[Unreleased]: https://github.com/MetaMask/utils/compare/v2.0.0...HEAD
[2.0.0]: https://github.com/MetaMask/utils/compare/v1.0.0...v2.0.0
[1.0.0]: https://github.com/MetaMask/utils/releases/tag/v1.0.0

@@ -61,2 +61,31 @@ /**

/**
* Type guard to narrow a JSON-RPC request or notification object to a
* notification.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
* @returns Whether the specified JSON-RPC message is a notification.
*/
export declare function isJsonRpcNotification<T>(requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>): requestOrNotification is JsonRpcNotification<T>;
/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* notification.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
*/
export declare function assertIsJsonRpcNotification<T>(requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>): asserts requestOrNotification is JsonRpcNotification<T>;
/**
* Type guard to narrow a JSON-RPC request or notification object to a request.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
* @returns Whether the specified JSON-RPC message is a request.
*/
export declare function isJsonRpcRequest<T>(requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>): requestOrNotification is JsonRpcRequest<T>;
/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* request.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
*/
export declare function assertIsJsonRpcRequest<T>(requestOrNotification: JsonRpcNotification<T> | JsonRpcRequest<T>): asserts requestOrNotification is JsonRpcRequest<T>;
/**
* A successful JSON-RPC response object.

@@ -87,6 +116,2 @@ *

/**
* ATTN: Assumes that only one of the `result` and `error` properties is
* present on the `response`, as guaranteed by e.g.
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
*
* Type guard to narrow a JsonRpcResponse object to a success (or failure).

@@ -100,6 +125,8 @@ *

/**
* ATTN: Assumes that only one of the `result` and `error` properties is
* present on the `response`, as guaranteed by e.g.
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
* Type assertion to narrow a JsonRpcResponse object to a success (or failure).
*
* @param response - The response object to check.
*/
export declare function assertIsJsonRpcSuccess<T>(response: JsonRpcResponse<T>): asserts response is JsonRpcSuccess<T>;
/**
* Type guard to narrow a JsonRpcResponse object to a failure (or success).

@@ -112,1 +139,37 @@ *

export declare function isJsonRpcFailure(response: JsonRpcResponse<unknown>): response is JsonRpcFailure;
/**
* Type assertion to narrow a JsonRpcResponse object to a failure (or success).
*
* @param response - The response object to check.
*/
export declare function assertIsJsonRpcFailure(response: JsonRpcResponse<unknown>): asserts response is JsonRpcFailure;
declare type JsonRpcValidatorOptions = {
permitEmptyString?: boolean;
permitFractions?: boolean;
permitNull?: boolean;
};
/**
* Gets a function for validating JSON-RPC request / response `id` values.
*
* By manipulating the options of this factory, you can control the behavior
* of the resulting validator for some edge cases. This is useful because e.g.
* `null` should sometimes but not always be permitted.
*
* Note that the empty string (`''`) is always permitted by the JSON-RPC
* specification, but that kind of sucks and you may want to forbid it in some
* instances anyway.
*
* For more details, see the
* [JSON-RPC Specification](https://www.jsonrpc.org/specification).
*
* @param options - An options object.
* @param options.permitEmptyString - Whether the empty string (i.e. `''`)
* should be treated as a valid ID. Default: `true`
* @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)
* should be treated as valid IDs. Default: `false`
* @param options.permitNull - Whether `null` should be treated as a valid ID.
* Default: `true`
* @returns The JSON-RPC ID validator function.
*/
export declare function getJsonRpcIdValidator(options?: JsonRpcValidatorOptions): (id: unknown) => id is JsonRpcId;
export {};

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

Object.defineProperty(exports, "__esModule", { value: true });
exports.isJsonRpcFailure = exports.isJsonRpcSuccess = exports.jsonrpc2 = exports.isValidJson = void 0;
exports.getJsonRpcIdValidator = exports.assertIsJsonRpcFailure = exports.isJsonRpcFailure = exports.assertIsJsonRpcSuccess = exports.isJsonRpcSuccess = exports.assertIsJsonRpcRequest = exports.isJsonRpcRequest = exports.assertIsJsonRpcNotification = exports.isJsonRpcNotification = exports.jsonrpc2 = exports.isValidJson = void 0;
const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));

@@ -30,6 +30,47 @@ const misc_1 = require("./misc");

/**
* ATTN: Assumes that only one of the `result` and `error` properties is
* present on the `response`, as guaranteed by e.g.
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
* Type guard to narrow a JSON-RPC request or notification object to a
* notification.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
* @returns Whether the specified JSON-RPC message is a notification.
*/
function isJsonRpcNotification(requestOrNotification) {
return !misc_1.hasProperty(requestOrNotification, 'id');
}
exports.isJsonRpcNotification = isJsonRpcNotification;
/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* notification.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
*/
function assertIsJsonRpcNotification(requestOrNotification) {
if (!isJsonRpcNotification(requestOrNotification)) {
throw new Error('Not a JSON-RPC notification.');
}
}
exports.assertIsJsonRpcNotification = assertIsJsonRpcNotification;
/**
* Type guard to narrow a JSON-RPC request or notification object to a request.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
* @returns Whether the specified JSON-RPC message is a request.
*/
function isJsonRpcRequest(requestOrNotification) {
return misc_1.hasProperty(requestOrNotification, 'id');
}
exports.isJsonRpcRequest = isJsonRpcRequest;
/**
* Assertion type guard to narrow a JSON-RPC request or notification object to a
* request.
*
* @param requestOrNotification - The JSON-RPC request or notification to check.
*/
function assertIsJsonRpcRequest(requestOrNotification) {
if (!isJsonRpcRequest(requestOrNotification)) {
throw new Error('Not a JSON-RPC request.');
}
}
exports.assertIsJsonRpcRequest = assertIsJsonRpcRequest;
/**
* Type guard to narrow a JsonRpcResponse object to a success (or failure).

@@ -46,6 +87,13 @@ *

/**
* ATTN: Assumes that only one of the `result` and `error` properties is
* present on the `response`, as guaranteed by e.g.
* [`JsonRpcEngine.handle`](https://github.com/MetaMask/json-rpc-engine/blob/main/src/JsonRpcEngine.ts).
* Type assertion to narrow a JsonRpcResponse object to a success (or failure).
*
* @param response - The response object to check.
*/
function assertIsJsonRpcSuccess(response) {
if (!isJsonRpcSuccess(response)) {
throw new Error('Not a successful JSON-RPC response.');
}
}
exports.assertIsJsonRpcSuccess = assertIsJsonRpcSuccess;
/**
* Type guard to narrow a JsonRpcResponse object to a failure (or success).

@@ -61,2 +109,53 @@ *

exports.isJsonRpcFailure = isJsonRpcFailure;
/**
* Type assertion to narrow a JsonRpcResponse object to a failure (or success).
*
* @param response - The response object to check.
*/
function assertIsJsonRpcFailure(response) {
if (!isJsonRpcFailure(response)) {
throw new Error('Not a failed JSON-RPC response.');
}
}
exports.assertIsJsonRpcFailure = assertIsJsonRpcFailure;
/**
* Gets a function for validating JSON-RPC request / response `id` values.
*
* By manipulating the options of this factory, you can control the behavior
* of the resulting validator for some edge cases. This is useful because e.g.
* `null` should sometimes but not always be permitted.
*
* Note that the empty string (`''`) is always permitted by the JSON-RPC
* specification, but that kind of sucks and you may want to forbid it in some
* instances anyway.
*
* For more details, see the
* [JSON-RPC Specification](https://www.jsonrpc.org/specification).
*
* @param options - An options object.
* @param options.permitEmptyString - Whether the empty string (i.e. `''`)
* should be treated as a valid ID. Default: `true`
* @param options.permitFractions - Whether fractional numbers (e.g. `1.2`)
* should be treated as valid IDs. Default: `false`
* @param options.permitNull - Whether `null` should be treated as a valid ID.
* Default: `true`
* @returns The JSON-RPC ID validator function.
*/
function getJsonRpcIdValidator(options) {
const { permitEmptyString, permitFractions, permitNull } = Object.assign({ permitEmptyString: true, permitFractions: false, permitNull: true }, options);
/**
* Type guard for {@link JsonRpcId}.
*
* @param id - The JSON-RPC ID value to check.
* @returns Whether the given ID is valid per the options given to the
* factory.
*/
const isValidJsonRpcId = (id) => {
return Boolean((typeof id === 'number' && (permitFractions || Number.isInteger(id))) ||
(typeof id === 'string' && (permitEmptyString || id.length > 0)) ||
(permitNull && id === null));
};
return isValidJsonRpcId;
}
exports.getJsonRpcIdValidator = getJsonRpcIdValidator;
//# sourceMappingURL=json.js.map

53

dist/time.d.ts
/**
* A millisecond.
* Common duration constants, in milliseconds.
*/
export declare const MILLISECOND = 1;
export declare enum Duration {
/**
* A millisecond.
*/
Millisecond = 1,
/**
* A second, in milliseconds.
*/
Second = 1000,
/**
* A minute, in milliseconds.
*/
Minute = 60000,
/**
* An hour, in milliseconds.
*/
Hour = 3600000,
/**
* A day, in milliseconds.
*/
Day = 86400000,
/**
* A week, in milliseconds.
*/
Week = 604800000,
/**
* A year, in milliseconds.
*/
Year = 31536000000
}
/**
* A second, in milliseconds.
* Calculates the millisecond value of the specified number of units of time.
*
* @param count - The number of units of time.
* @param duration - The unit of time to count.
* @returns The count multiplied by the specified duration.
*/
export declare const SECOND = 1000;
export declare function inMilliseconds(count: number, duration: Duration): number;
/**
* A minute, in milliseconds.
*/
export declare const MINUTE = 60000;
/**
* An hour, in milliseconds.
*/
export declare const HOUR = 3600000;
/**
* A day, in milliseconds.
*/
export declare const DAY = 86400000;
/**
* Gets the milliseconds since a particular Unix epoch timestamp.

@@ -23,0 +44,0 @@ *

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeSince = exports.DAY = exports.HOUR = exports.MINUTE = exports.SECOND = exports.MILLISECOND = void 0;
exports.timeSince = exports.inMilliseconds = exports.Duration = void 0;
/**
* A millisecond.
* Common duration constants, in milliseconds.
*/
exports.MILLISECOND = 1;
var Duration;
(function (Duration) {
/**
* A millisecond.
*/
Duration[Duration["Millisecond"] = 1] = "Millisecond";
/**
* A second, in milliseconds.
*/
Duration[Duration["Second"] = 1000] = "Second";
/**
* A minute, in milliseconds.
*/
Duration[Duration["Minute"] = 60000] = "Minute";
/**
* An hour, in milliseconds.
*/
Duration[Duration["Hour"] = 3600000] = "Hour";
/**
* A day, in milliseconds.
*/
Duration[Duration["Day"] = 86400000] = "Day";
/**
* A week, in milliseconds.
*/
Duration[Duration["Week"] = 604800000] = "Week";
/**
* A year, in milliseconds.
*/
Duration[Duration["Year"] = 31536000000] = "Year";
})(Duration = exports.Duration || (exports.Duration = {}));
const isNonNegativeInteger = (number) => Number.isInteger(number) && number >= 0;
const assertIsNonNegativeInteger = (number, name) => {
if (!isNonNegativeInteger(number)) {
throw new Error(`"${name}" must be a non-negative integer. Received: "${number}".`);
}
};
/**
* A second, in milliseconds.
* Calculates the millisecond value of the specified number of units of time.
*
* @param count - The number of units of time.
* @param duration - The unit of time to count.
* @returns The count multiplied by the specified duration.
*/
exports.SECOND = 1000; // MILLISECOND * 1000
function inMilliseconds(count, duration) {
assertIsNonNegativeInteger(count, 'count');
return count * duration;
}
exports.inMilliseconds = inMilliseconds;
/**
* A minute, in milliseconds.
*/
exports.MINUTE = 60000; // SECOND * 60
/**
* An hour, in milliseconds.
*/
exports.HOUR = 3600000; // MINUTE * 60
/**
* A day, in milliseconds.
*/
exports.DAY = 86400000; // HOUR * 24
/**
* Gets the milliseconds since a particular Unix epoch timestamp.

@@ -31,2 +63,3 @@ *

function timeSince(timestamp) {
assertIsNonNegativeInteger(timestamp, 'timestamp');
return Date.now() - timestamp;

@@ -33,0 +66,0 @@ }

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

@@ -5,0 +5,0 @@ "repository": {

@@ -15,3 +15,3 @@ # MetaMask Utils

The full API documentation for the latest published version of this library is [available here](https://metamask.github.io/eth-sig-util/index.html).
The full API documentation for the latest published version of this library is [available here](https://metamask.github.io/utils/index.html).

@@ -18,0 +18,0 @@ ## Contributing

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