@metamask/utils
Advanced tools
Comparing version 8.1.0 to 8.2.0
@@ -9,2 +9,14 @@ # Changelog | ||
## [8.2.0] | ||
### Added | ||
- Add struct utils for validating JSON objects with optional values ([#136](https://github.com/MetaMask/utils/pull/136)) | ||
- Add filesystem utils ([#148](https://github.com/MetaMask/utils/pull/148)) | ||
- Add error utils ([#146](https://github.com/MetaMask/utils/pull/146), [#151](https://github.com/MetaMask/utils/pull/151)) | ||
- Add base64 encoding and decoding functions ([#145](https://github.com/MetaMask/utils/pull/145)) | ||
### Changed | ||
- Use `tsup` for bundling ([#144](https://github.com/MetaMask/utils/pull/144)) | ||
- This makes the package fully compliant with ES modules. | ||
- Bump `@ethereumjs/tx` from `4.1.2` to `4.2.0` ([#133](https://github.com/MetaMask/utils/pull/133)) | ||
## [8.1.0] | ||
@@ -196,3 +208,4 @@ ### Changed | ||
[Unreleased]: https://github.com/MetaMask/utils/compare/v8.1.0...HEAD | ||
[Unreleased]: https://github.com/MetaMask/utils/compare/v8.2.0...HEAD | ||
[8.2.0]: https://github.com/MetaMask/utils/compare/v8.1.0...v8.2.0 | ||
[8.1.0]: https://github.com/MetaMask/utils/compare/v8.0.0...v8.1.0 | ||
@@ -199,0 +212,0 @@ [8.0.0]: https://github.com/MetaMask/utils/compare/v7.1.0...v8.0.0 |
@@ -65,2 +65,9 @@ import type { Hex } from './hex'; | ||
/** | ||
* Convert a `Uint8Array` to a base64 encoded string. | ||
* | ||
* @param bytes - The bytes to convert to a base64 encoded string. | ||
* @returns The base64 encoded string. | ||
*/ | ||
export declare function bytesToBase64(bytes: Uint8Array): string; | ||
/** | ||
* Convert a hexadecimal string to a `Uint8Array`. The string can optionally be | ||
@@ -116,2 +123,9 @@ * prefixed with `0x`. It accepts even and odd length strings. | ||
/** | ||
* Convert a base64 encoded string to a `Uint8Array`. | ||
* | ||
* @param value - The base64 encoded string to convert to bytes. | ||
* @returns The bytes as `Uint8Array`. | ||
*/ | ||
export declare function base64ToBytes(value: string): Uint8Array; | ||
/** | ||
* Convert a byte-like value to a `Uint8Array`. The value can be a `Uint8Array`, | ||
@@ -118,0 +132,0 @@ * a `bigint`, a `number`, or a `string`. |
@@ -9,2 +9,3 @@ export * from './assert'; | ||
export * from './encryption-types'; | ||
export * from './errors'; | ||
export * from './hex'; | ||
@@ -11,0 +12,0 @@ export * from './json'; |
@@ -1,2 +0,4 @@ | ||
import type { Infer, Struct } from 'superstruct'; | ||
import type { Infer } from 'superstruct'; | ||
import { Struct } from 'superstruct'; | ||
import type { ObjectSchema, Optionalize, Simplify } from 'superstruct/dist/utils'; | ||
import type { AssertionErrorConstructor } from './assert'; | ||
@@ -10,2 +12,66 @@ /** | ||
/** | ||
* A helper type to make properties with `undefined` in their type optional, but | ||
* not `undefined` itself. | ||
* | ||
* @example | ||
* ```ts | ||
* type Foo = ObjectOptional<{ foo: string | undefined }>; | ||
* // Foo is equivalent to { foo?: string } | ||
* ``` | ||
*/ | ||
export declare type ObjectOptional<Schema extends Record<string, unknown>> = { | ||
[Key in keyof Schema as Schema[Key] extends ExactOptionalGuard ? Key : never]?: Schema[Key] extends ExactOptionalGuard & infer Original ? Original : never; | ||
} & { | ||
[Key in keyof Schema as Schema[Key] extends ExactOptionalGuard ? never : Key]: Schema[Key]; | ||
}; | ||
/** | ||
* An object type with support for exact optionals. This is used by the `object` | ||
* struct. This uses the {@link ObjectOptional} helper to make properties with | ||
* `undefined` in their type optional, but not `undefined` itself. | ||
*/ | ||
export declare type ObjectType<Schema extends ObjectSchema> = Simplify<ObjectOptional<Optionalize<{ | ||
[Key in keyof Schema]: Infer<Schema[Key]>; | ||
}>>>; | ||
/** | ||
* A struct to check if the given value is a valid object, with support for | ||
* {@link exactOptional} types. | ||
* | ||
* @param schema - The schema of the object. | ||
* @returns A struct to check if the given value is an object. | ||
*/ | ||
export declare const object: <Schema extends ObjectSchema>(schema: Schema) => Struct<Simplify<ObjectOptional<Optionalize<{ [Key in keyof Schema]: Infer<Schema[Key]>; }>>>, unknown>; | ||
declare const exactOptionalSymbol: unique symbol; | ||
declare type ExactOptionalGuard = { | ||
_exactOptionalGuard?: typeof exactOptionalSymbol; | ||
}; | ||
/** | ||
* A struct which allows the property of an object to be absent, or to be present | ||
* as long as it's valid and not set to `undefined`. | ||
* | ||
* This struct should be used in conjunction with the {@link object} from this | ||
* library, to get proper type inference. | ||
* | ||
* @param struct - The struct to check the value against, if present. | ||
* @returns A struct to check if the given value is valid, or not present. | ||
* @example | ||
* ```ts | ||
* const struct = object({ | ||
* foo: exactOptional(string()), | ||
* bar: exactOptional(number()), | ||
* baz: optional(boolean()), | ||
* qux: unknown(), | ||
* }); | ||
* | ||
* type Type = Infer<typeof struct>; | ||
* // Type is equivalent to: | ||
* // { | ||
* // foo?: string; | ||
* // bar?: number; | ||
* // baz?: boolean | undefined; | ||
* // qux: unknown; | ||
* // } | ||
* ``` | ||
*/ | ||
export declare function exactOptional<Type, Schema>(struct: Struct<Type, Schema>): Struct<Type & ExactOptionalGuard, Schema>; | ||
/** | ||
* A struct to check if the given value is a valid JSON-serializable value. | ||
@@ -70,12 +136,7 @@ * | ||
export declare const JsonRpcErrorStruct: Struct<{ | ||
data?: Json & ExactOptionalGuard; | ||
stack?: string; | ||
code: number; | ||
message: string; | ||
data?: Json | undefined; | ||
stack?: string | undefined; | ||
}, { | ||
code: Struct<number, null>; | ||
message: Struct<string, null>; | ||
data: Struct<Json | undefined, unknown>; | ||
stack: Struct<string | undefined, null>; | ||
}>; | ||
}, unknown>; | ||
/** | ||
@@ -96,14 +157,9 @@ * Mark a certain key of a type as optional. | ||
export declare const JsonRpcRequestStruct: Struct<{ | ||
params?: (Json[] | Record<string, Json>) & ExactOptionalGuard; | ||
id: string | number | null; | ||
method: string; | ||
jsonrpc: "2.0"; | ||
params?: Json[] | Record<string, Json> | undefined; | ||
}, { | ||
id: Struct<string | number | null, null>; | ||
jsonrpc: Struct<"2.0", "2.0">; | ||
method: Struct<string, null>; | ||
params: Struct<Json[] | Record<string, Json> | undefined, null>; | ||
}>; | ||
export declare type InferWithParams<Type extends Struct<any>, Params extends JsonRpcParams> = Omit<Infer<Type>, 'params'> & { | ||
params?: Exclude<Params, undefined>; | ||
}, unknown>; | ||
export declare type InferWithParams<Type extends Struct<any>, Params extends JsonRpcParams> = Infer<Type> & { | ||
params?: Params; | ||
}; | ||
@@ -115,10 +171,6 @@ /** | ||
export declare const JsonRpcNotificationStruct: Struct<{ | ||
params?: (Json[] | Record<string, Json>) & ExactOptionalGuard; | ||
method: string; | ||
jsonrpc: "2.0"; | ||
params?: Json[] | Record<string, Json> | undefined; | ||
}, { | ||
jsonrpc: Struct<"2.0", "2.0">; | ||
method: Struct<string, null>; | ||
params: Struct<Json[] | Record<string, Json> | undefined, null>; | ||
}>; | ||
}, unknown>; | ||
/** | ||
@@ -166,6 +218,6 @@ * A JSON-RPC notification object. | ||
error?: { | ||
data?: Json & ExactOptionalGuard; | ||
stack?: string; | ||
code: number; | ||
message: string; | ||
data?: Json | undefined; | ||
stack?: string | undefined; | ||
} | undefined; | ||
@@ -177,12 +229,7 @@ }, { | ||
error: Struct<{ | ||
data?: Json & ExactOptionalGuard; | ||
stack?: string; | ||
code: number; | ||
message: string; | ||
data?: Json | undefined; | ||
stack?: string | undefined; | ||
} | undefined, { | ||
code: Struct<number, null>; | ||
message: Struct<string, null>; | ||
data: Struct<Json | undefined, unknown>; | ||
stack: Struct<string | undefined, null>; | ||
}>; | ||
} | undefined, unknown>; | ||
}>; | ||
@@ -199,7 +246,3 @@ /** | ||
result: Json; | ||
}, { | ||
id: Struct<string | number | null, null>; | ||
jsonrpc: Struct<"2.0", "2.0">; | ||
result: Struct<Json, unknown>; | ||
}>; | ||
}, unknown>; | ||
/** | ||
@@ -215,7 +258,3 @@ * A successful JSON-RPC response object. | ||
jsonrpc: "2.0"; | ||
}, { | ||
id: Struct<string | number | null, null>; | ||
jsonrpc: Struct<"2.0", "2.0">; | ||
error: Struct<JsonRpcError, unknown>; | ||
}>; | ||
}, unknown>; | ||
/** | ||
@@ -222,0 +261,0 @@ * A failed JSON-RPC response object. |
{ | ||
"name": "@metamask/utils", | ||
"version": "8.1.0", | ||
"version": "8.2.0", | ||
"description": "Various JavaScript/TypeScript utilities of wide relevance to the MetaMask codebase", | ||
@@ -14,26 +14,25 @@ "homepage": "https://github.com/MetaMask/utils#readme", | ||
"license": "ISC", | ||
"sideEffects": false, | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/cjs/index.js", | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js", | ||
"types": "./dist/types/index.d.ts" | ||
}, | ||
"./node": { | ||
"import": "./dist/node.mjs", | ||
"require": "./dist/node.js", | ||
"types": "./dist/types/node.d.ts" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/esm/index.js", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/types/index.d.ts", | ||
"files": [ | ||
"dist/cjs/**", | ||
"dist/esm/**", | ||
"dist/types/**" | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn build:source && yarn build:types", | ||
"build:cjs": "swc src --out-dir dist/cjs --config-file .swcrc.build.json --config module.type=commonjs", | ||
"build:clean": "rimraf dist && yarn build", | ||
"build": "tsup && yarn build:types", | ||
"build:docs": "typedoc", | ||
"build:esm": "swc src --out-dir dist/esm --config-file .swcrc.build.json --config module.type=es6 && yarn build:esm:package", | ||
"build:esm:package": "echo >dist/esm/package.json \"{\\\"type\\\":\\\"module\\\"}\"", | ||
"build:source": "yarn build:esm && yarn build:cjs", | ||
"build:types": "tsc --project tsconfig.build.json", | ||
@@ -57,6 +56,8 @@ "lint": "yarn lint:eslint && yarn lint:constraints && yarn lint:misc --check && yarn lint:dependencies --check && yarn lint:changelog", | ||
"dependencies": { | ||
"@ethereumjs/tx": "^4.1.2", | ||
"@ethereumjs/tx": "^4.2.0", | ||
"@noble/hashes": "^1.3.1", | ||
"@scure/base": "^1.1.3", | ||
"@types/debug": "^4.1.7", | ||
"debug": "^4.3.4", | ||
"pony-cause": "^2.1.10", | ||
"semver": "^7.5.4", | ||
@@ -73,9 +74,8 @@ "superstruct": "^1.0.3" | ||
"@metamask/eslint-config-typescript": "^12.0.0", | ||
"@swc/cli": "^0.1.62", | ||
"@swc/core": "^1.3.66", | ||
"@types/jest": "^28.1.7", | ||
"@types/jest-when": "^3.5.3", | ||
"@types/node": "^17.0.23", | ||
"@typescript-eslint/eslint-plugin": "^5.43.0", | ||
"@typescript-eslint/parser": "^5.43.0", | ||
"depcheck": "^1.4.3", | ||
"depcheck": "^1.4.7", | ||
"eslint": "^8.44.0", | ||
@@ -91,5 +91,5 @@ "eslint-config-prettier": "^8.8.0", | ||
"jest-it-up": "^2.0.2", | ||
"jest-when": "^3.6.0", | ||
"prettier": "^2.7.1", | ||
"prettier-plugin-packagejson": "^2.3.0", | ||
"rimraf": "^3.0.2", | ||
"stdio-mock": "^1.2.0", | ||
@@ -99,2 +99,3 @@ "ts-jest": "^29.0.3", | ||
"tsd": "^0.24.1", | ||
"tsup": "^7.2.0", | ||
"typedoc": "^0.23.15", | ||
@@ -114,3 +115,3 @@ "typescript": "~4.8.4" | ||
"@lavamoat/preinstall-always-fail": false, | ||
"@swc/core": true | ||
"tsup>esbuild": true | ||
} | ||
@@ -117,0 +118,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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
479271
220
5482
8
2
1
+ Added@scure/base@^1.1.3
+ Addedpony-cause@^2.1.10
+ Addedpony-cause@2.1.11(transitive)
Updated@ethereumjs/tx@^4.2.0