@metamask/snaps-utils 0.24.0 → 0.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAIA,2CAAqD;AACrD,mCAA8C;AAC9C,6CAUqB;AAGrB,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,gDAA4B,CAAA;IAC5B,mDAA+B,CAAA;AACjC,CAAC,EAHW,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAG3B;AAED;;GAEG;AACU,QAAA,aAAa,GAAG,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE;IACjE,OAAO,IAAA,cAAW,EAAC,KAAK,CAAC,KAAK,IAAI,CAAC;AACrC,CAAC,CAAC,CAAC;AAEU,QAAA,UAAU,GAAG,IAAA,kBAAI,EAC5B,IAAA,qBAAO,EACL,IAAA,oBAAM,GAAE,EACR,6DAA6D,CAC9D,EACD,CAAC,EACD,GAAG,CACJ,CAAC;AAEF,2EAA2E;AAC3E,sBAAsB;AACT,QAAA,wBAAwB,GAAG,IAAA,kBAAI,EAAC;IAC3C,OAAO,EAAE,qBAAa;IACtB,IAAI,EAAE,kBAAU;IAChB,IAAI,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;IAC3C,UAAU,EAAE,IAAA,sBAAQ,EAClB,IAAA,oBAAM,EAAC;QACL,IAAI,EAAE,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC;QACjC,GAAG,EAAE,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC;KACjC,CAAC,CACH;CACF,CAAC,CAAC;AAKH;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,gCAAwB,CAAC,CAAC;AAC7C,CAAC;AAJD,oDAIC;AAED;;;;;GAKG;AACH,SAAgB,0BAA0B,CACxC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,gCAAwB,EACxB,IAAI,gBAAgB,CAAC,WAAW,cAAc,CAC/C,CAAC;AACJ,CAAC;AARD,gEAQC;AAuBD;;GAEG;AACH,IAAY,cAGX;AAHD,WAAY,cAAc;IACxB,8BAAY,CAAA;IACZ,kCAAgB,CAAA;AAClB,CAAC,EAHW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAGzB;AAID;;;GAGG;AACH,IAAY,2BAKX;AALD,WAAY,2BAA2B;IACrC,uEAAsC,CAAA;IACtC,6EAA4C,CAAA;IAC5C,mFAAkD,CAAA;IAClD,2EAA0C,CAAA;AAC5C,CAAC,EALW,2BAA2B,GAA3B,mCAA2B,KAA3B,mCAA2B,QAKtC;AAED,IAAY,iBAGX;AAHD,WAAY,iBAAiB;IAC3B,yCAAoB,CAAA;IACpB,wCAAmB,CAAA;AACrB,CAAC,EAHW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAG5B;AAED,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,4CAA6B,CAAA;IAC7B,8CAA+B,CAAA;IAC/B,sCAAuB,CAAA;IACvB,sCAAuB,CAAA;AACzB,CAAC,EALW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAKtB;AAEY,QAAA,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC","sourcesContent":["import {\n SnapFunctionExports,\n SnapKeyring as Keyring,\n} from '@metamask/snaps-types';\nimport { assertStruct, Json } from '@metamask/utils';\nimport { valid as validSemver } from 'semver';\nimport {\n Infer,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n type,\n} from 'superstruct';\nimport { SnapManifest } from './manifest/validation';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\n/**\n * A struct for validating a version string.\n */\nexport const VersionStruct = refine(string(), 'Version', (value) => {\n return validSemver(value) !== null;\n});\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: Json;\n packageJson?: Json;\n sourceCode?: string;\n svgIcon?: string;\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: SnapManifest;\n packageJson: NpmSnapPackageJson;\n sourceCode: string;\n svgIcon?: string;\n};\n\n/**\n * The possible prefixes for snap ids.\n */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n\nexport type SnapId = string;\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n\nexport enum HandlerType {\n OnRpcRequest = 'onRpcRequest',\n OnTransaction = 'onTransaction',\n SnapKeyring = 'keyring',\n OnCronjob = 'onCronjob',\n}\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\n// The snap is the callee\nexport type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;\n\ntype ObjectParameters<\n Type extends Record<string, (...args: any[]) => unknown>,\n> = Parameters<Type[keyof Type]>;\n\ntype KeyringParameter<Fn> = Fn extends (...args: any[]) => unknown\n ? Parameters<Fn>\n : never;\n\ntype KeyringParameters = KeyringParameter<Keyring[keyof Keyring]>;\n\nexport type SnapExportsParameters =\n | ObjectParameters<SnapFunctionExports>\n | KeyringParameters;\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAIA,2CAAqD;AACrD,6CASqB;AAGrB,yCAA2C;AAE3C,IAAY,gBAGX;AAHD,WAAY,gBAAgB;IAC1B,gDAA4B,CAAA;IAC5B,mDAA+B,CAAA;AACjC,CAAC,EAHW,gBAAgB,GAAhB,wBAAgB,KAAhB,wBAAgB,QAG3B;AAEY,QAAA,UAAU,GAAG,IAAA,kBAAI,EAC5B,IAAA,qBAAO,EACL,IAAA,oBAAM,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tC;AAED,yDAAyD;AACzD,IAAY,iBAGX;AAHD,WAAY,iBAAiB;IAC3B,yCAAoB,CAAA;IACpB,wCAAmB,CAAA;AACrB,CAAC,EAHW,iBAAiB,GAAjB,yBAAiB,KAAjB,yBAAiB,QAG5B;AACD,wDAAwD;AAExD,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,4CAA6B,CAAA;IAC7B,8CAA+B,CAAA;IAC/B,sCAAuB,CAAA;IACvB,sCAAuB,CAAA;AACzB,CAAC,EALW,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAKtB;AAEY,QAAA,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC","sourcesContent":["import {\n SnapFunctionExports,\n SnapKeyring as Keyring,\n} from '@metamask/snaps-types';\nimport { assertStruct, Json } from '@metamask/utils';\nimport {\n Infer,\n is,\n object,\n optional,\n pattern,\n size,\n string,\n type,\n} from 'superstruct';\n\nimport { SnapManifest } from './manifest/validation';\nimport { VersionStruct } from './versions';\n\nexport enum NpmSnapFileNames {\n PackageJson = 'package.json',\n Manifest = 'snap.manifest.json',\n}\n\nexport const NameStruct = size(\n pattern(\n string(),\n /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$/u,\n ),\n 1,\n 214,\n);\n\n// Note we use `type` instead of `object` here, because the latter does not\n// allow unknown keys.\nexport const NpmSnapPackageJsonStruct = type({\n version: VersionStruct,\n name: NameStruct,\n main: optional(size(string(), 1, Infinity)),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n});\n\nexport type NpmSnapPackageJson = Infer<typeof NpmSnapPackageJsonStruct> &\n Record<string, any>;\n\n/**\n * Check if the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link NpmSnapPackageJson} object.\n */\nexport function isNpmSnapPackageJson(\n value: unknown,\n): value is NpmSnapPackageJson {\n return is(value, NpmSnapPackageJsonStruct);\n}\n\n/**\n * Asserts that the given value is a valid {@link NpmSnapPackageJson} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link NpmSnapPackageJson} object.\n */\nexport function assertIsNpmSnapPackageJson(\n value: unknown,\n): asserts value is NpmSnapPackageJson {\n assertStruct(\n value,\n NpmSnapPackageJsonStruct,\n `\"${NpmSnapFileNames.PackageJson}\" is invalid`,\n );\n}\n\n/**\n * An object for storing parsed but unvalidated Snap file contents.\n */\nexport type UnvalidatedSnapFiles = {\n manifest?: Json;\n packageJson?: Json;\n sourceCode?: string;\n svgIcon?: string;\n};\n\n/**\n * An object for storing the contents of Snap files that have passed JSON\n * Schema validation, or are non-empty if they are strings.\n */\nexport type SnapFiles = {\n manifest: SnapManifest;\n packageJson: NpmSnapPackageJson;\n sourceCode: string;\n svgIcon?: string;\n};\n\n/**\n * The possible prefixes for snap ids.\n */\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SnapIdPrefixes {\n npm = 'npm:',\n local = 'local:',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapId = string;\n\n/**\n * Snap validation failure reason codes that are programmatically fixable\n * if validation occurs during development.\n */\nexport enum SnapValidationFailureReason {\n NameMismatch = '\"name\" field mismatch',\n VersionMismatch = '\"version\" field mismatch',\n RepositoryMismatch = '\"repository\" field mismatch',\n ShasumMismatch = '\"shasum\" field mismatch',\n}\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport enum SNAP_STREAM_NAMES {\n JSON_RPC = 'jsonRpc',\n COMMAND = 'command',\n}\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport enum HandlerType {\n OnRpcRequest = 'onRpcRequest',\n OnTransaction = 'onTransaction',\n SnapKeyring = 'keyring',\n OnCronjob = 'onCronjob',\n}\n\nexport const SNAP_EXPORT_NAMES = Object.values(HandlerType);\n\nexport type SnapRpcHookArgs = {\n origin: string;\n handler: HandlerType;\n request: Record<string, unknown>;\n};\n\n// The snap is the callee\nexport type SnapRpcHook = (options: SnapRpcHookArgs) => Promise<unknown>;\n\ntype ObjectParameters<\n Type extends Record<string, (...args: any[]) => unknown>,\n> = Parameters<Type[keyof Type]>;\n\ntype KeyringParameter<Fn> = Fn extends (...args: any[]) => unknown\n ? Parameters<Fn>\n : never;\n\ntype KeyringParameters = KeyringParameter<Keyring[keyof Keyring]>;\n\nexport type SnapExportsParameters =\n | ObjectParameters<SnapFunctionExports>\n | KeyringParameters;\n\n// We use a symbol property name instead of { _type: Brand }, because that would show up in IDE suggestions,\n// while internal symbols do not.\ndeclare const brand: unique symbol;\nexport type Opaque<Base, Brand extends symbol> = Base & { [brand]: Brand };\n"]}
@@ -1,5 +1,80 @@
1
1
  import { Json } from '@metamask/utils';
2
- export declare const DEFAULT_REQUESTED_SNAP_VERSION = "*";
2
+ import { Struct } from 'superstruct';
3
+ import { Opaque } from './types';
4
+ export declare const DEFAULT_REQUESTED_SNAP_VERSION: SemVerRange;
5
+ /**
6
+ * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for SemVer ranges.
7
+ *
8
+ * @example Use {@link assertIsSemVerRange} and {@link isValidSemVerRange} to cast to proper type.
9
+ * ```typescript
10
+ * const unsafeRange: string = dataFromUser();
11
+ * assertIsSemVerRange(unsafeRange);
12
+ * unsafeRange
13
+ * // ^? SemVerRange
14
+ * ```
15
+ * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.
16
+ * ```typescript
17
+ * const unsafeRange: string = dataFromUser();
18
+ * unsafeRange as SemVerRange;
19
+ * // ^? SemVerRange
20
+ * ```
21
+ * @see {@link assertIsSemVerRange}
22
+ * @see {@link isValidSemVerRange}
23
+ */
24
+ export declare type SemVerRange = Opaque<string, typeof semVerRange>;
25
+ declare const semVerRange: unique symbol;
26
+ /**
27
+ * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for singular SemVer version.
28
+ *
29
+ * @example Use {@link assertIsSemVerVersion} and {@link isValidSemVerVersion} to cast to proper type.
30
+ * ```typescript
31
+ * const unsafeVersion: string = dataFromUser();
32
+ * assertIsSemVerVersion(unsafeRange);
33
+ * unsafeVersion
34
+ * // ^? SemVerVersion
35
+ * ```
36
+ * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.
37
+ * ```typescript
38
+ * const unsafeVersion: string = dataFromUser();
39
+ * unsafeRange as SemVerVersion;
40
+ * // ^? SemVerVersion
41
+ * ```
42
+ * @see {@link assertIsSemVerVersion}
43
+ * @see {@link isValidSemVerVersion}
44
+ */
45
+ export declare type SemVerVersion = Opaque<string, typeof semVerVersion>;
46
+ declare const semVerVersion: unique symbol;
47
+ /**
48
+ * A struct for validating a version string.
49
+ */
50
+ export declare const VersionStruct: Struct<SemVerVersion, null>;
51
+ export declare const VersionRangeStruct: Struct<SemVerRange, null>;
52
+ /**
53
+ * Checks whether a SemVer version is valid.
54
+ *
55
+ * @param version - A potential version.
56
+ * @returns `true` if the version is valid, and `false` otherwise.
57
+ */
58
+ export declare function isValidSemVerVersion(version: unknown): version is SemVerVersion;
59
+ /**
60
+ * Checks whether a SemVer version range is valid.
61
+ *
62
+ * @param versionRange - A potential version range.
63
+ * @returns `true` if the version range is valid, and `false` otherwise.
64
+ */
65
+ export declare function isValidSemVerRange(versionRange: unknown): versionRange is SemVerRange;
66
+ /**
67
+ * Asserts that a value is a valid concrete SemVer version.
68
+ *
69
+ * @param version - A potential SemVer concrete version.
70
+ */
71
+ export declare function assertIsSemVerVersion(version: unknown): asserts version is SemVerVersion;
72
+ /**
73
+ * Asserts that a value is a valid SemVer range.
74
+ *
75
+ * @param range - A potential SemVer range.
76
+ */
77
+ export declare function assertIsSemVerRange(range: unknown): asserts range is SemVerRange;
3
78
  /**
4
79
  * Checks whether a SemVer version is greater than another.
5
80
  *
@@ -7,7 +82,7 @@ export declare const DEFAULT_REQUESTED_SNAP_VERSION = "*";
7
82
  * @param version2 - The right-hand version.
8
83
  * @returns `version1 > version2`.
9
84
  */
10
- export declare function gtVersion(version1: string, version2: string): boolean;
85
+ export declare function gtVersion(version1: SemVerVersion, version2: SemVerVersion): boolean;
11
86
  /**
12
87
  * Returns whether a SemVer version satisfies a SemVer range.
13
88
  *
@@ -15,7 +90,7 @@ export declare function gtVersion(version1: string, version2: string): boolean;
15
90
  * @param versionRange - The SemVer version range to check against.
16
91
  * @returns Whether the version satisfied the version range.
17
92
  */
18
- export declare function satisfiesVersionRange(version: string, versionRange: string): boolean;
93
+ export declare function satisfiesVersionRange(version: SemVerVersion, versionRange: SemVerRange): boolean;
19
94
  /**
20
95
  * Return the highest version in the list that satisfies the range, or `null` if
21
96
  * none of them do. For the satisfaction check, pre-release versions will only
@@ -26,7 +101,7 @@ export declare function satisfiesVersionRange(version: string, versionRange: str
26
101
  * @returns The highest version in the list that satisfies the range,
27
102
  * or `null` if none of them do.
28
103
  */
29
- export declare function getTargetVersion(versions: string[], versionRange: string): string | null;
104
+ export declare function getTargetVersion(versions: SemVerVersion[], versionRange: SemVerRange): SemVerVersion | null;
30
105
  /**
31
106
  * Parse a version received by some subject attempting to access a snap.
32
107
  *
@@ -34,11 +109,5 @@ export declare function getTargetVersion(versions: string[], versionRange: strin
34
109
  * @returns `*` if the version is `undefined` or `latest", otherwise returns
35
110
  * the specified version.
36
111
  */
37
- export declare function resolveVersion(version?: Json): Json;
38
- /**
39
- * Checks whether a SemVer version range is valid.
40
- *
41
- * @param versionRange - A potential version range.
42
- * @returns `true` if the version range is valid, and `false` otherwise.
43
- */
44
- export declare function isValidSnapVersionRange(versionRange: unknown): versionRange is string;
112
+ export declare function resolveVersionRange(version?: Json): [error: undefined, range: SemVerRange] | [error: Error, range: undefined];
113
+ export {};
package/dist/versions.js CHANGED
@@ -1,8 +1,63 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isValidSnapVersionRange = exports.resolveVersion = exports.getTargetVersion = exports.satisfiesVersionRange = exports.gtVersion = exports.DEFAULT_REQUESTED_SNAP_VERSION = void 0;
3
+ exports.resolveVersionRange = exports.getTargetVersion = exports.satisfiesVersionRange = exports.gtVersion = exports.assertIsSemVerRange = exports.assertIsSemVerVersion = exports.isValidSemVerRange = exports.isValidSemVerVersion = exports.VersionRangeStruct = exports.VersionStruct = exports.DEFAULT_REQUESTED_SNAP_VERSION = void 0;
4
+ const utils_1 = require("@metamask/utils");
4
5
  const semver_1 = require("semver");
6
+ const superstruct_1 = require("superstruct");
5
7
  exports.DEFAULT_REQUESTED_SNAP_VERSION = '*';
8
+ /**
9
+ * A struct for validating a version string.
10
+ */
11
+ exports.VersionStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Version', (value) => {
12
+ if ((0, semver_1.valid)(value) === null) {
13
+ return `Expected SemVer version, got "${value}"`;
14
+ }
15
+ return true;
16
+ });
17
+ exports.VersionRangeStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Version range', (value) => {
18
+ if ((0, semver_1.validRange)(value) === null) {
19
+ return `Expected SemVer range, got "${value}"`;
20
+ }
21
+ return true;
22
+ });
23
+ /**
24
+ * Checks whether a SemVer version is valid.
25
+ *
26
+ * @param version - A potential version.
27
+ * @returns `true` if the version is valid, and `false` otherwise.
28
+ */
29
+ function isValidSemVerVersion(version) {
30
+ return (0, superstruct_1.is)(version, exports.VersionStruct);
31
+ }
32
+ exports.isValidSemVerVersion = isValidSemVerVersion;
33
+ /**
34
+ * Checks whether a SemVer version range is valid.
35
+ *
36
+ * @param versionRange - A potential version range.
37
+ * @returns `true` if the version range is valid, and `false` otherwise.
38
+ */
39
+ function isValidSemVerRange(versionRange) {
40
+ return (0, superstruct_1.is)(versionRange, exports.VersionRangeStruct);
41
+ }
42
+ exports.isValidSemVerRange = isValidSemVerRange;
43
+ /**
44
+ * Asserts that a value is a valid concrete SemVer version.
45
+ *
46
+ * @param version - A potential SemVer concrete version.
47
+ */
48
+ function assertIsSemVerVersion(version) {
49
+ (0, utils_1.assertStruct)(version, exports.VersionStruct);
50
+ }
51
+ exports.assertIsSemVerVersion = assertIsSemVerVersion;
52
+ /**
53
+ * Asserts that a value is a valid SemVer range.
54
+ *
55
+ * @param range - A potential SemVer range.
56
+ */
57
+ function assertIsSemVerRange(range) {
58
+ (0, utils_1.assertStruct)(range, exports.VersionRangeStruct);
59
+ }
60
+ exports.assertIsSemVerRange = assertIsSemVerRange;
6
61
  /**
7
62
  * Checks whether a SemVer version is greater than another.
8
63
  *
@@ -11,7 +66,7 @@ exports.DEFAULT_REQUESTED_SNAP_VERSION = '*';
11
66
  * @returns `version1 > version2`.
12
67
  */
13
68
  function gtVersion(version1, version2) {
14
- return (0, semver_1.gt)(version1, version2, { includePrerelease: true });
69
+ return (0, semver_1.gt)(version1, version2);
15
70
  }
16
71
  exports.gtVersion = gtVersion;
17
72
  /**
@@ -56,22 +111,11 @@ exports.getTargetVersion = getTargetVersion;
56
111
  * @returns `*` if the version is `undefined` or `latest", otherwise returns
57
112
  * the specified version.
58
113
  */
59
- function resolveVersion(version) {
114
+ function resolveVersionRange(version) {
60
115
  if (version === undefined || version === 'latest') {
61
- return exports.DEFAULT_REQUESTED_SNAP_VERSION;
116
+ return [undefined, exports.DEFAULT_REQUESTED_SNAP_VERSION];
62
117
  }
63
- return version;
64
- }
65
- exports.resolveVersion = resolveVersion;
66
- /**
67
- * Checks whether a SemVer version range is valid.
68
- *
69
- * @param versionRange - A potential version range.
70
- * @returns `true` if the version range is valid, and `false` otherwise.
71
- */
72
- function isValidSnapVersionRange(versionRange) {
73
- return Boolean(typeof versionRange === 'string' &&
74
- (0, semver_1.validRange)(versionRange, { includePrerelease: true }) !== null);
118
+ return (0, superstruct_1.validate)(version, exports.VersionRangeStruct);
75
119
  }
76
- exports.isValidSnapVersionRange = isValidSnapVersionRange;
120
+ exports.resolveVersionRange = resolveVersionRange;
77
121
  //# sourceMappingURL=versions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"versions.js","sourceRoot":"","sources":["../src/versions.ts"],"names":[],"mappings":";;;AACA,mCAKgB;AAEH,QAAA,8BAA8B,GAAG,GAAG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Semver,\n satisfies as satisfiesSemver,\n validRange as validRangeSemver,\n} from 'semver';\n\nexport const DEFAULT_REQUESTED_SNAP_VERSION = '*';\n\n/**\n * Checks whether a SemVer version is greater than another.\n *\n * @param version1 - The left-hand version.\n * @param version2 - The right-hand version.\n * @returns `version1 > version2`.\n */\nexport function gtVersion(version1: string, version2: string): boolean {\n return gtSemver(version1, version2, { includePrerelease: true });\n}\n\n/**\n * Returns whether a SemVer version satisfies a SemVer range.\n *\n * @param version - The SemVer version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns Whether the version satisfied the version range.\n */\nexport function satisfiesVersionRange(\n version: string,\n versionRange: string,\n): boolean {\n return satisfiesSemver(version, versionRange, {\n includePrerelease: true,\n });\n}\n\n/**\n * Return the highest version in the list that satisfies the range, or `null` if\n * none of them do. For the satisfaction check, pre-release versions will only\n * be checked if no satisfactory non-prerelease version is found first.\n *\n * @param versions - The list of version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns The highest version in the list that satisfies the range,\n * or `null` if none of them do.\n */\nexport function getTargetVersion(\n versions: string[],\n versionRange: string,\n): string | null {\n const maxSatisfyingNonPreRelease = maxSatisfyingSemver(\n versions,\n versionRange,\n );\n\n // By default don't use pre-release versions\n if (maxSatisfyingNonPreRelease) {\n return maxSatisfyingNonPreRelease;\n }\n\n // If no satisfying release version is found by default, try pre-release versions\n return maxSatisfyingSemver(versions, versionRange, {\n includePrerelease: true,\n });\n}\n\n/**\n * Parse a version received by some subject attempting to access a snap.\n *\n * @param version - The received version value.\n * @returns `*` if the version is `undefined` or `latest\", otherwise returns\n * the specified version.\n */\nexport function resolveVersion(version?: Json): Json {\n if (version === undefined || version === 'latest') {\n return DEFAULT_REQUESTED_SNAP_VERSION;\n }\n return version;\n}\n\n/**\n * Checks whether a SemVer version range is valid.\n *\n * @param versionRange - A potential version range.\n * @returns `true` if the version range is valid, and `false` otherwise.\n */\nexport function isValidSnapVersionRange(\n versionRange: unknown,\n): versionRange is string {\n return Boolean(\n typeof versionRange === 'string' &&\n validRangeSemver(versionRange, { includePrerelease: true }) !== null,\n );\n}\n"]}
1
+ {"version":3,"file":"versions.js","sourceRoot":"","sources":["../src/versions.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,mCAMgB;AAChB,6CAAmE;AAItD,QAAA,8BAA8B,GAAG,GAAkB,CAAC;AA8CjE;;GAEG;AACU,QAAA,aAAa,GAAG,IAAA,oBAAM,EACjC,IAAA,oBAAM,GAA4C,EAClD,SAAS,EACT,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,IAAA,cAAkB,EAAC,KAAK,CAAC,KAAK,IAAI,EAAE;QACtC,OAAO,iCAAiC,KAAK,GAAG,CAAC;KAClD;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CACF,CAAC;AAEW,QAAA,kBAAkB,GAAG,IAAA,oBAAM,EACtC,IAAA,oBAAM,GAA0C,EAChD,eAAe,EACf,CAAC,KAAK,EAAE,EAAE;IACR,IAAI,IAAA,mBAAgB,EAAC,KAAK,CAAC,KAAK,IAAI,EAAE;QACpC,OAAO,+BAA+B,KAAK,GAAG,CAAC;KAChD;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CACF,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,OAAgB;IAEhB,OAAO,IAAA,gBAAE,EAAC,OAAO,EAAE,qBAAa,CAAC,CAAC;AACpC,CAAC;AAJD,oDAIC;AAED;;;;;GAKG;AACH,SAAgB,kBAAkB,CAChC,YAAqB;IAErB,OAAO,IAAA,gBAAE,EAAC,YAAY,EAAE,0BAAkB,CAAC,CAAC;AAC9C,CAAC;AAJD,gDAIC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CACnC,OAAgB;IAEhB,IAAA,oBAAY,EAAC,OAAO,EAAE,qBAAa,CAAC,CAAC;AACvC,CAAC;AAJD,sDAIC;AAED;;;;GAIG;AACH,SAAgB,mBAAmB,CACjC,KAAc;IAEd,IAAA,oBAAY,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC1C,CAAC;AAJD,kDAIC;AAED;;;;;;GAMG;AACH,SAAgB,SAAS,CACvB,QAAuB,EACvB,QAAuB;IAEvB,OAAO,IAAA,WAAQ,EAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;AACtC,CAAC;AALD,8BAKC;AAED;;;;;;GAMG;AACH,SAAgB,qBAAqB,CACnC,OAAsB,EACtB,YAAyB;IAEzB,OAAO,IAAA,kBAAe,EAAC,OAAO,EAAE,YAAY,EAAE;QAC5C,iBAAiB,EAAE,IAAI;KACxB,CAAC,CAAC;AACL,CAAC;AAPD,sDAOC;AAED;;;;;;;;;GASG;AACH,SAAgB,gBAAgB,CAC9B,QAAyB,EACzB,YAAyB;IAEzB,MAAM,0BAA0B,GAAG,IAAA,sBAAmB,EACpD,QAAQ,EACR,YAAY,CACb,CAAC;IAEF,4CAA4C;IAC5C,IAAI,0BAA0B,EAAE;QAC9B,OAAO,0BAA0B,CAAC;KACnC;IAED,iFAAiF;IACjF,OAAO,IAAA,sBAAmB,EAAC,QAAQ,EAAE,YAAY,EAAE;QACjD,iBAAiB,EAAE,IAAI;KACxB,CAAC,CAAC;AACL,CAAC;AAlBD,4CAkBC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,OAAc;IAEd,IAAI,OAAO,KAAK,SAAS,IAAI,OAAO,KAAK,QAAQ,EAAE;QACjD,OAAO,CAAC,SAAS,EAAE,sCAA8B,CAAC,CAAC;KACpD;IACD,OAAO,IAAA,sBAAQ,EAAC,OAAO,EAAE,0BAAkB,CAAC,CAAC;AAC/C,CAAC;AAPD,kDAOC","sourcesContent":["import { assertStruct, Json } from '@metamask/utils';\nimport {\n gt as gtSemver,\n maxSatisfying as maxSatisfyingSemver,\n satisfies as satisfiesSemver,\n valid as validSemVerVersion,\n validRange as validSemVerRange,\n} from 'semver';\nimport { is, refine, string, Struct, validate } from 'superstruct';\n\nimport { Opaque } from './types';\n\nexport const DEFAULT_REQUESTED_SNAP_VERSION = '*' as SemVerRange;\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for SemVer ranges.\n *\n * @example Use {@link assertIsSemVerRange} and {@link isValidSemVerRange} to cast to proper type.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * assertIsSemVerRange(unsafeRange);\n * unsafeRange\n * // ^? SemVerRange\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeRange: string = dataFromUser();\n * unsafeRange as SemVerRange;\n * // ^? SemVerRange\n * ```\n * @see {@link assertIsSemVerRange}\n * @see {@link isValidSemVerRange}\n */\nexport type SemVerRange = Opaque<string, typeof semVerRange>;\ndeclare const semVerRange: unique symbol;\n\n/**\n * {@link https://codemix.com/opaque-types-in-javascript/ Opaque} type for singular SemVer version.\n *\n * @example Use {@link assertIsSemVerVersion} and {@link isValidSemVerVersion} to cast to proper type.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * assertIsSemVerVersion(unsafeRange);\n * unsafeVersion\n * // ^? SemVerVersion\n * ```\n * @example If you know what you're doing and want to side-step type safety, casting from a string works correctly.\n * ```typescript\n * const unsafeVersion: string = dataFromUser();\n * unsafeRange as SemVerVersion;\n * // ^? SemVerVersion\n * ```\n * @see {@link assertIsSemVerVersion}\n * @see {@link isValidSemVerVersion}\n */\nexport type SemVerVersion = Opaque<string, typeof semVerVersion>;\ndeclare const semVerVersion: unique symbol;\n\n/**\n * A struct for validating a version string.\n */\nexport const VersionStruct = refine<SemVerVersion, null>(\n string() as unknown as Struct<SemVerVersion, null>,\n 'Version',\n (value) => {\n if (validSemVerVersion(value) === null) {\n return `Expected SemVer version, got \"${value}\"`;\n }\n return true;\n },\n);\n\nexport const VersionRangeStruct = refine<SemVerRange, null>(\n string() as unknown as Struct<SemVerRange, null>,\n 'Version range',\n (value) => {\n if (validSemVerRange(value) === null) {\n return `Expected SemVer range, got \"${value}\"`;\n }\n return true;\n },\n);\n\n/**\n * Checks whether a SemVer version is valid.\n *\n * @param version - A potential version.\n * @returns `true` if the version is valid, and `false` otherwise.\n */\nexport function isValidSemVerVersion(\n version: unknown,\n): version is SemVerVersion {\n return is(version, VersionStruct);\n}\n\n/**\n * Checks whether a SemVer version range is valid.\n *\n * @param versionRange - A potential version range.\n * @returns `true` if the version range is valid, and `false` otherwise.\n */\nexport function isValidSemVerRange(\n versionRange: unknown,\n): versionRange is SemVerRange {\n return is(versionRange, VersionRangeStruct);\n}\n\n/**\n * Asserts that a value is a valid concrete SemVer version.\n *\n * @param version - A potential SemVer concrete version.\n */\nexport function assertIsSemVerVersion(\n version: unknown,\n): asserts version is SemVerVersion {\n assertStruct(version, VersionStruct);\n}\n\n/**\n * Asserts that a value is a valid SemVer range.\n *\n * @param range - A potential SemVer range.\n */\nexport function assertIsSemVerRange(\n range: unknown,\n): asserts range is SemVerRange {\n assertStruct(range, VersionRangeStruct);\n}\n\n/**\n * Checks whether a SemVer version is greater than another.\n *\n * @param version1 - The left-hand version.\n * @param version2 - The right-hand version.\n * @returns `version1 > version2`.\n */\nexport function gtVersion(\n version1: SemVerVersion,\n version2: SemVerVersion,\n): boolean {\n return gtSemver(version1, version2);\n}\n\n/**\n * Returns whether a SemVer version satisfies a SemVer range.\n *\n * @param version - The SemVer version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns Whether the version satisfied the version range.\n */\nexport function satisfiesVersionRange(\n version: SemVerVersion,\n versionRange: SemVerRange,\n): boolean {\n return satisfiesSemver(version, versionRange, {\n includePrerelease: true,\n });\n}\n\n/**\n * Return the highest version in the list that satisfies the range, or `null` if\n * none of them do. For the satisfaction check, pre-release versions will only\n * be checked if no satisfactory non-prerelease version is found first.\n *\n * @param versions - The list of version to check.\n * @param versionRange - The SemVer version range to check against.\n * @returns The highest version in the list that satisfies the range,\n * or `null` if none of them do.\n */\nexport function getTargetVersion(\n versions: SemVerVersion[],\n versionRange: SemVerRange,\n): SemVerVersion | null {\n const maxSatisfyingNonPreRelease = maxSatisfyingSemver(\n versions,\n versionRange,\n );\n\n // By default don't use pre-release versions\n if (maxSatisfyingNonPreRelease) {\n return maxSatisfyingNonPreRelease;\n }\n\n // If no satisfying release version is found by default, try pre-release versions\n return maxSatisfyingSemver(versions, versionRange, {\n includePrerelease: true,\n });\n}\n\n/**\n * Parse a version received by some subject attempting to access a snap.\n *\n * @param version - The received version value.\n * @returns `*` if the version is `undefined` or `latest\", otherwise returns\n * the specified version.\n */\nexport function resolveVersionRange(\n version?: Json,\n): [error: undefined, range: SemVerRange] | [error: Error, range: undefined] {\n if (version === undefined || version === 'latest') {\n return [undefined, DEFAULT_REQUESTED_SNAP_VERSION];\n }\n return validate(version, VersionRangeStruct);\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/snaps-utils",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/MetaMask/snaps-monorepo.git"
@@ -52,7 +52,7 @@
52
52
  "dependencies": {
53
53
  "@babel/core": "^7.18.6",
54
54
  "@babel/types": "^7.18.7",
55
- "@metamask/snaps-types": "^0.24.0",
55
+ "@metamask/snaps-types": "^0.25.0",
56
56
  "@metamask/utils": "^3.3.1",
57
57
  "@noble/hashes": "^1.1.3",
58
58
  "@scure/base": "^1.1.1",
@@ -66,29 +66,31 @@
66
66
  },
67
67
  "devDependencies": {
68
68
  "@lavamoat/allow-scripts": "^2.0.3",
69
- "@metamask/auto-changelog": "^2.6.0",
70
- "@metamask/eslint-config": "^9.0.0",
71
- "@metamask/eslint-config-jest": "^9.0.0",
72
- "@metamask/eslint-config-nodejs": "^9.0.0",
73
- "@metamask/eslint-config-typescript": "^9.0.1",
69
+ "@metamask/auto-changelog": "^3.1.0",
70
+ "@metamask/eslint-config": "^11.0.0",
71
+ "@metamask/eslint-config-jest": "^11.0.0",
72
+ "@metamask/eslint-config-nodejs": "^11.0.1",
73
+ "@metamask/eslint-config-typescript": "^11.0.0",
74
74
  "@types/jest": "^27.5.1",
75
75
  "@types/semver": "^7.3.10",
76
+ "@typescript-eslint/eslint-plugin": "^5.42.1",
77
+ "@typescript-eslint/parser": "^5.42.1",
76
78
  "deepmerge": "^4.2.2",
77
- "eslint": "^7.30.0",
78
- "eslint-config-prettier": "^8.3.0",
79
- "eslint-plugin-import": "^2.23.4",
80
- "eslint-plugin-jest": "^24.4.0",
81
- "eslint-plugin-jsdoc": "^36.1.0",
79
+ "eslint": "^8.27.0",
80
+ "eslint-config-prettier": "^8.5.0",
81
+ "eslint-plugin-import": "^2.26.0",
82
+ "eslint-plugin-jest": "^27.1.5",
83
+ "eslint-plugin-jsdoc": "^39.6.2",
82
84
  "eslint-plugin-node": "^11.1.0",
83
- "eslint-plugin-prettier": "^3.4.0",
85
+ "eslint-plugin-prettier": "^4.2.1",
84
86
  "jest": "^29.0.2",
85
87
  "jest-it-up": "^2.0.0",
86
88
  "memfs": "^3.4.7",
87
- "prettier": "^2.3.2",
89
+ "prettier": "^2.7.1",
88
90
  "prettier-plugin-packagejson": "^2.2.11",
89
91
  "rimraf": "^3.0.2",
90
92
  "ts-jest": "^29.0.0",
91
- "typescript": "^4.4.0"
93
+ "typescript": "~4.8.4"
92
94
  },
93
95
  "engines": {
94
96
  "node": ">=16.0.0"