From aec3c7498829a2c366e65416704854063cadbd55 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Thu, 3 Nov 2022 14:08:07 +0100 Subject: [PATCH] Bump @metamask/utils to 3.3.0 --- packages/cli/package.json | 2 +- packages/controllers/package.json | 2 +- .../examples/examples/insights/package.json | 2 +- .../examples/insights/snap.manifest.json | 2 +- packages/execution-environments/package.json | 4 +- .../src/common/validation.ts | 3 +- packages/plugin-webpack/package.json | 2 +- packages/provider/package.json | 2 +- packages/rpc-methods/package.json | 4 +- packages/utils/jest.config.js | 8 +-- packages/utils/package.json | 4 +- packages/utils/src/assert.test.ts | 60 ------------------- packages/utils/src/assert.ts | 50 ---------------- packages/utils/src/index.browser.ts | 1 - packages/utils/src/index.ts | 1 - packages/utils/src/namespace.ts | 7 ++- packages/utils/src/notification.ts | 2 +- packages/utils/src/types.ts | 3 +- yarn.lock | 41 +++++++------ 19 files changed, 45 insertions(+), 155 deletions(-) delete mode 100644 packages/utils/src/assert.test.ts delete mode 100644 packages/utils/src/assert.ts diff --git a/packages/cli/package.json b/packages/cli/package.json index 457f45f011..637e15cf9e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -48,7 +48,7 @@ "@babel/preset-typescript": "^7.16.7", "@metamask/snap-utils": "^0.22.3", "@metamask/snaps-browserify-plugin": "^0.22.3", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "babelify": "^10.0.0", "browserify": "^17.0.0", "chokidar": "^3.5.2", diff --git a/packages/controllers/package.json b/packages/controllers/package.json index b298b8e682..9d251c156f 100644 --- a/packages/controllers/package.json +++ b/packages/controllers/package.json @@ -39,7 +39,7 @@ "@metamask/rpc-methods": "^0.22.3", "@metamask/snap-types": "^0.22.3", "@metamask/snap-utils": "^0.22.3", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "@xstate/fsm": "^2.0.0", "concat-stream": "^2.0.0", "cron-parser": "^4.5.0", diff --git a/packages/examples/examples/insights/package.json b/packages/examples/examples/insights/package.json index c756f2c412..4c4cf94187 100644 --- a/packages/examples/examples/insights/package.json +++ b/packages/examples/examples/insights/package.json @@ -27,7 +27,7 @@ }, "dependencies": { "@metamask/abi-utils": "^1.0.0", - "@metamask/utils": "^3.2.0" + "@metamask/utils": "^3.3.0" }, "devDependencies": { "@lavamoat/allow-scripts": "^2.0.3", diff --git a/packages/examples/examples/insights/snap.manifest.json b/packages/examples/examples/insights/snap.manifest.json index b3afa7adda..5ff2fa4b77 100644 --- a/packages/examples/examples/insights/snap.manifest.json +++ b/packages/examples/examples/insights/snap.manifest.json @@ -7,7 +7,7 @@ "url": "https://github.com/MetaMask/snaps-monorepo.git" }, "source": { - "shasum": "l3QRoIUw1LSiuN1Y3bl88eqWDj3pc6AL5Wwp/j+ClhA=", + "shasum": "rWNQVlpSAlUXVByhpVyXxvNRTwEfabK4vS2MU6ANNwc=", "location": { "npm": { "filePath": "dist/bundle.js", diff --git a/packages/execution-environments/package.json b/packages/execution-environments/package.json index 975fb64ac4..06cdfc29a0 100644 --- a/packages/execution-environments/package.json +++ b/packages/execution-environments/package.json @@ -35,12 +35,12 @@ "@metamask/providers": "^9.0.0", "@metamask/snap-types": "^0.22.3", "@metamask/snap-utils": "^0.22.3", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "eth-rpc-errors": "^4.0.3", "pump": "^3.0.0", "ses": "^0.15.15", "stream-browserify": "^3.0.0", - "superstruct": "^0.16.5" + "superstruct": "^0.16.7" }, "devDependencies": { "@lavamoat/allow-scripts": "^2.0.3", diff --git a/packages/execution-environments/src/common/validation.ts b/packages/execution-environments/src/common/validation.ts index f77e132267..8425c787f7 100644 --- a/packages/execution-environments/src/common/validation.ts +++ b/packages/execution-environments/src/common/validation.ts @@ -1,4 +1,4 @@ -import { assertStruct, ChainIdStruct, HandlerType } from '@metamask/snap-utils'; +import { ChainIdStruct, HandlerType } from '@metamask/snap-utils'; import { SnapKeyring } from '@metamask/snap-types'; import { array, @@ -17,6 +17,7 @@ import { unknown, } from 'superstruct'; import { + assertStruct, Json, JsonRpcIdStruct, JsonRpcRequestStruct, diff --git a/packages/plugin-webpack/package.json b/packages/plugin-webpack/package.json index 52ae3bf0bd..e7877feba0 100644 --- a/packages/plugin-webpack/package.json +++ b/packages/plugin-webpack/package.json @@ -30,7 +30,7 @@ }, "dependencies": { "@metamask/snap-utils": "^0.22.3", - "@metamask/utils": "^3.2.0", + "@metamask/utils": "^3.3.0", "webpack-sources": "^3.2.3" }, "devDependencies": { diff --git a/packages/provider/package.json b/packages/provider/package.json index 034272a3f6..a76469e1e9 100644 --- a/packages/provider/package.json +++ b/packages/provider/package.json @@ -27,7 +27,7 @@ "dependencies": { "@metamask/safe-event-emitter": "^2.0.0", "@metamask/snap-types": "^0.22.3", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "nanoid": "^3.1.31" }, "devDependencies": { diff --git a/packages/rpc-methods/package.json b/packages/rpc-methods/package.json index a51f38caad..731d4eaeae 100644 --- a/packages/rpc-methods/package.json +++ b/packages/rpc-methods/package.json @@ -30,10 +30,10 @@ "@metamask/key-tree": "^5.0.2", "@metamask/snap-utils": "^0.22.3", "@metamask/types": "^1.1.0", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "eth-rpc-errors": "^4.0.2", "nanoid": "^3.1.31", - "superstruct": "^0.16.5" + "superstruct": "^0.16.7" }, "devDependencies": { "@lavamoat/allow-scripts": "^2.0.3", diff --git a/packages/utils/jest.config.js b/packages/utils/jest.config.js index b0657f1033..3fff7df8cd 100644 --- a/packages/utils/jest.config.js +++ b/packages/utils/jest.config.js @@ -15,10 +15,10 @@ module.exports = { coverageReporters: ['clover', 'json', 'lcov', 'text', 'json-summary'], coverageThreshold: { global: { - branches: 88.47, - functions: 98.21, - lines: 97.8, - statements: 97.84, + branches: 88.1, + functions: 98.18, + lines: 97.75, + statements: 97.8, }, }, moduleFileExtensions: ['js', 'json', 'jsx', 'ts', 'tsx', 'node'], diff --git a/packages/utils/package.json b/packages/utils/package.json index 7743d88a17..54930167f9 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -53,7 +53,7 @@ "@babel/core": "^7.18.6", "@babel/types": "^7.18.7", "@metamask/snap-types": "^0.22.3", - "@metamask/utils": "^3.1.0", + "@metamask/utils": "^3.3.0", "@noble/hashes": "^1.1.3", "@scure/base": "^1.1.1", "cron-parser": "^4.5.0", @@ -62,7 +62,7 @@ "rfdc": "^1.3.0", "semver": "^7.3.7", "ses": "^0.15.17", - "superstruct": "^0.16.5" + "superstruct": "^0.16.7" }, "devDependencies": { "@lavamoat/allow-scripts": "^2.0.3", diff --git a/packages/utils/src/assert.test.ts b/packages/utils/src/assert.test.ts deleted file mode 100644 index 5d06ce01c3..0000000000 --- a/packages/utils/src/assert.test.ts +++ /dev/null @@ -1,60 +0,0 @@ -import { assertStruct } from './assert'; -import { EventStruct } from './notification'; - -describe('assertStruct', () => { - it('does not throw for a valid value', () => { - expect(() => - assertStruct({ name: 'foo', data: 'bar' }, EventStruct), - ).not.toThrow(); - }); - - it('throws meaningful error messages for an invalid value', () => { - expect(() => assertStruct({ data: 'foo' }, EventStruct)).toThrow( - 'Assertion failed: At path: name -- Expected a string, but received: undefined.', - ); - - expect(() => assertStruct({ name: 1, data: 'foo' }, EventStruct)).toThrow( - 'Assertion failed: At path: name -- Expected a string, but received: 1.', - ); - }); - - it('throws with a custom error prefix', () => { - expect(() => - assertStruct({ data: 'foo' }, EventStruct, 'Invalid event'), - ).toThrow( - 'Invalid event: At path: name -- Expected a string, but received: undefined.', - ); - }); - - it('throws with a custom error class', () => { - class CustomError extends Error { - constructor({ message }: { message: string }) { - super(message); - this.name = 'CustomError'; - } - } - - expect(() => - assertStruct({ data: 'foo' }, EventStruct, 'Invalid event', CustomError), - ).toThrow( - new CustomError({ - message: - 'Invalid event: At path: name -- Expected a string, but received: undefined.', - }), - ); - }); - - it('throws with a custom error function', () => { - const CustomError = ({ message }: { message: string }) => - new Error(message); - - expect(() => - assertStruct({ data: 'foo' }, EventStruct, 'Invalid event', CustomError), - ).toThrow( - CustomError({ - message: - 'Invalid event: At path: name -- Expected a string, but received: undefined.', - }), - ); - }); -}); diff --git a/packages/utils/src/assert.ts b/packages/utils/src/assert.ts deleted file mode 100644 index 842658dd5e..0000000000 --- a/packages/utils/src/assert.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { AssertionError } from '@metamask/utils'; -import { Struct, assert as assertSuperstruct } from 'superstruct'; - -export type AssertionErrorConstructor = - | (new (args: { message: string }) => Error) - | ((args: { message: string }) => Error); - -/** - * Check if a value is a constructor. - * - * @param fn - The value to check. - * @returns `true` if the value is a constructor, or `false` otherwise. - */ -function isConstructable( - fn: AssertionErrorConstructor, -): fn is new (args: { message: string }) => Error { - return Boolean(typeof fn?.prototype?.constructor?.name === 'string'); -} - -/** - * Assert a value against a Superstruct struct. - * - * @param value - The value to validate. - * @param struct - The struct to validate against. - * @param errorPrefix - A prefix to add to the error message. Defaults to - * "Assertion failed". - * @param ErrorWrapper - The error class to throw if the assertion fails. - * Defaults to {@link AssertionError}. - * @throws If the value is not valid. - */ -export function assertStruct( - value: unknown, - struct: Struct, - errorPrefix = 'Assertion failed', - ErrorWrapper: AssertionErrorConstructor = AssertionError, -): asserts value is T { - try { - assertSuperstruct(value, struct); - } catch (error) { - if (isConstructable(ErrorWrapper)) { - throw new ErrorWrapper({ - message: `${errorPrefix}: ${error.message}.`, - }); - } else { - throw ErrorWrapper({ - message: `${errorPrefix}: ${error.message}.`, - }); - } - } -} diff --git a/packages/utils/src/index.browser.ts b/packages/utils/src/index.browser.ts index 212d387269..bb09145113 100644 --- a/packages/utils/src/index.browser.ts +++ b/packages/utils/src/index.browser.ts @@ -1,4 +1,3 @@ -export * from './assert'; export * from './caveats'; export * from './deep-clone'; export * from './default-endowments'; diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 57ee0791af..706aa037b0 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,4 +1,3 @@ -export * from './assert'; export * from './caveats'; export * from './cronjob'; export * from './deep-clone'; diff --git a/packages/utils/src/namespace.ts b/packages/utils/src/namespace.ts index f191fee160..9aab602790 100644 --- a/packages/utils/src/namespace.ts +++ b/packages/utils/src/namespace.ts @@ -13,8 +13,11 @@ import { partial, pick, } from 'superstruct'; -import { JsonRpcRequestStruct } from '@metamask/utils'; -import { AssertionErrorConstructor, assertStruct } from './assert'; +import { + JsonRpcRequestStruct, + AssertionErrorConstructor, + assertStruct, +} from '@metamask/utils'; export const CHAIN_ID_REGEX = /^(?[-a-z0-9]{3,8}):(?[-a-zA-Z0-9]{1,32})$/u; diff --git a/packages/utils/src/notification.ts b/packages/utils/src/notification.ts index 306c2fd5cc..ae287134fa 100644 --- a/packages/utils/src/notification.ts +++ b/packages/utils/src/notification.ts @@ -1,6 +1,6 @@ import { Infer, is, literal, object, string, unknown } from 'superstruct'; +import { assertStruct } from '@metamask/utils'; import { ChainIdStruct } from './namespace'; -import { assertStruct } from './assert'; export const EventStruct = object({ name: string(), diff --git a/packages/utils/src/types.ts b/packages/utils/src/types.ts index 8a5da03de2..9e9a5bc51d 100644 --- a/packages/utils/src/types.ts +++ b/packages/utils/src/types.ts @@ -2,7 +2,7 @@ import { SnapFunctionExports, SnapKeyring as Keyring, } from '@metamask/snap-types'; -import { Json } from '@metamask/utils'; +import { Json, assertStruct } from '@metamask/utils'; import { any, Infer, @@ -19,7 +19,6 @@ import { union, } from 'superstruct'; import { valid as validSemver } from 'semver'; -import { assertStruct } from './assert'; export enum NpmSnapFileNames { PackageJson = 'package.json', diff --git a/yarn.lock b/yarn.lock index e8f132470e..eba13a304c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2774,7 +2774,7 @@ __metadata: "@metamask/providers": ^9.0.0 "@metamask/snap-types": ^0.22.3 "@metamask/snap-utils": ^0.22.3 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@types/jest": ^27.5.1 "@types/node": ^17.0.36 concat: ^1.0.3 @@ -2800,7 +2800,7 @@ __metadata: rimraf: ^3.0.2 ses: ^0.15.15 stream-browserify: ^3.0.0 - superstruct: ^0.16.5 + superstruct: ^0.16.7 ts-jest: ^29.0.0 ts-loader: ^9.3.1 typescript: ^4.4.0 @@ -2842,7 +2842,7 @@ __metadata: "@metamask/safe-event-emitter": ^2.0.0 "@metamask/snap-types": ^0.22.3 "@metamask/snap-utils": ^0.22.3 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@types/jest": ^27.5.1 eslint: ^7.30.0 eslint-config-prettier: ^8.3.0 @@ -2960,7 +2960,7 @@ __metadata: "@metamask/key-tree": ^5.0.2 "@metamask/snap-utils": ^0.22.3 "@metamask/types": ^1.1.0 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@types/node": ^14.14.25 eslint: ^7.30.0 eslint-config-prettier: ^8.3.0 @@ -2976,7 +2976,7 @@ __metadata: prettier: ^2.3.2 prettier-plugin-packagejson: ^2.2.11 rimraf: ^3.0.2 - superstruct: ^0.16.5 + superstruct: ^0.16.7 ts-jest: ^29.0.0 typescript: ^4.4.0 languageName: unknown @@ -3008,7 +3008,7 @@ __metadata: "@metamask/snap-types": ^0.22.3 "@metamask/snap-utils": ^0.22.3 "@metamask/template-snap": ^0.7.0 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@peculiar/webcrypto": ^1.3.3 "@types/concat-stream": ^2.0.0 "@types/gunzip-maybe": ^1.4.0 @@ -3092,7 +3092,7 @@ __metadata: "@metamask/eslint-config-nodejs": ^9.0.0 "@metamask/eslint-config-typescript": ^9.0.1 "@metamask/snap-types": ^0.22.3 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@noble/hashes": ^1.1.3 "@scure/base": ^1.1.1 "@types/jest": ^27.5.1 @@ -3116,7 +3116,7 @@ __metadata: rimraf: ^3.0.2 semver: ^7.3.7 ses: ^0.15.17 - superstruct: ^0.16.5 + superstruct: ^0.16.7 ts-jest: ^29.0.0 typescript: ^4.4.0 languageName: unknown @@ -3177,7 +3177,7 @@ __metadata: "@metamask/eslint-config-typescript": ^9.0.1 "@metamask/snap-utils": ^0.22.3 "@metamask/snaps-browserify-plugin": ^0.22.3 - "@metamask/utils": ^3.1.0 + "@metamask/utils": ^3.3.0 "@types/browserify": ^12.0.36 "@types/init-package-json": ^1.10.0 "@types/is-url": ^1.2.28 @@ -3235,7 +3235,7 @@ __metadata: "@metamask/eslint-config-nodejs": ^9.0.0 "@metamask/eslint-config-typescript": ^9.0.1 "@metamask/snap-utils": ^0.22.3 - "@metamask/utils": ^3.2.0 + "@metamask/utils": ^3.3.0 "@types/jest": ^27.5.1 "@types/webpack-sources": ^3.2.0 eslint: ^7.30.0 @@ -3281,15 +3281,14 @@ __metadata: languageName: node linkType: hard -"@metamask/utils@npm:^3.1.0, @metamask/utils@npm:^3.2.0": - version: 3.2.0 - resolution: "@metamask/utils@npm:3.2.0" +"@metamask/utils@npm:^3.2.0, @metamask/utils@npm:^3.3.0": + version: 3.3.0 + resolution: "@metamask/utils@npm:3.3.0" dependencies: "@types/debug": ^4.1.7 debug: ^4.3.4 - fast-deep-equal: ^3.1.3 - superstruct: ^0.16.5 - checksum: 99adcbd273c69075628913259f8c3fb843291898eba813f4f5fe0bfc060ae5955e2c69e70e15b04156793f8d84edd077d1fac3f8c3927e067d0f311eef9d4469 + superstruct: ^0.16.7 + checksum: 263c26722b7339fced997cc2aa38ccb34d178f532ff025c7e789818ad5ef2317439eda694a4c0bf5ff3cabdeced3c170b9ad23d2f178ddd5fd225f6ec2bce259 languageName: node linkType: hard @@ -16022,10 +16021,10 @@ __metadata: languageName: node linkType: hard -"superstruct@npm:^0.16.5": - version: 0.16.5 - resolution: "superstruct@npm:0.16.5" - checksum: 9f843c38695b584a605ae9b028629de18a85bd0dca0e9449b4ab98bb7b9ac3d82599870acbab9fbd2ee454c6b187af7e61562e252dfadabd974191ab4ab2e3ce +"superstruct@npm:^0.16.5, superstruct@npm:^0.16.7": + version: 0.16.7 + resolution: "superstruct@npm:0.16.7" + checksum: c8c855ff6945da8a41048c6d236de7b1af5d4d9c31742b3ee54d65647c31597488620281f65e095d5efc9e2fbdaad529b8c8f2506c12569d428467c835a21477 languageName: node linkType: hard @@ -16414,7 +16413,7 @@ __metadata: "@metamask/eslint-config-typescript": ^9.0.1 "@metamask/snap-types": ^0.22.3 "@metamask/snaps-cli": ^0.22.3 - "@metamask/utils": ^3.2.0 + "@metamask/utils": ^3.3.0 eslint: ^7.30.0 eslint-config-prettier: ^8.3.0 eslint-plugin-import: ^2.23.4