@metamask/snaps-utils 0.24.1 → 0.26.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/caveats.d.ts +5 -1
- package/dist/caveats.js +4 -0
- package/dist/caveats.js.map +1 -1
- package/dist/eval-worker.js.map +1 -1
- package/dist/eval.js +1 -1
- package/dist/eval.js.map +1 -1
- package/dist/fs.js.map +1 -1
- package/dist/index.browser.d.ts +1 -1
- package/dist/index.browser.js +1 -1
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/json-rpc.d.ts +19 -1
- package/dist/json-rpc.js +25 -1
- package/dist/json-rpc.js.map +1 -1
- package/dist/manifest/manifest.js +6 -6
- package/dist/manifest/manifest.js.map +1 -1
- package/dist/manifest/validation.d.ts +28 -2
- package/dist/manifest/validation.js +7 -3
- package/dist/manifest/validation.js.map +1 -1
- package/dist/mock.js +4 -1
- package/dist/mock.js.map +1 -1
- package/dist/namespace.d.ts +1 -1
- package/dist/namespace.js +4 -2
- package/dist/namespace.js.map +1 -1
- package/dist/notification.js +1 -1
- package/dist/notification.js.map +1 -1
- package/dist/npm.js.map +1 -1
- package/dist/object.js.map +1 -1
- package/dist/post-process.js +6 -4
- package/dist/post-process.js.map +1 -1
- package/dist/snaps.d.ts +7 -2
- package/dist/snaps.js +54 -5
- package/dist/snaps.js.map +1 -1
- package/dist/types.d.ts +31 -14
- package/dist/types.js +29 -9
- package/dist/types.js.map +1 -1
- package/dist/versions.d.ts +89 -12
- package/dist/versions.js +72 -17
- package/dist/versions.js.map +1 -1
- package/dist/virtual-file/VirtualFile.d.ts +33 -0
- package/dist/virtual-file/VirtualFile.js +62 -0
- package/dist/virtual-file/VirtualFile.js.map +1 -0
- package/dist/virtual-file/index.browser.d.ts +1 -0
- package/dist/virtual-file/index.browser.js +18 -0
- package/dist/virtual-file/index.browser.js.map +1 -0
- package/dist/virtual-file/index.d.ts +2 -0
- package/dist/virtual-file/index.js +19 -0
- package/dist/virtual-file/index.js.map +1 -0
- package/dist/virtual-file/toVirtualFile.d.ts +21 -0
- package/dist/virtual-file/toVirtualFile.js +33 -0
- package/dist/virtual-file/toVirtualFile.js.map +1 -0
- package/package.json +21 -17
- package/dist/url.d.ts +0 -7
- package/dist/url.js +0 -19
- package/dist/url.js.map +0 -1
package/dist/types.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SNAP_EXPORT_NAMES = exports.HandlerType = exports.SNAP_STREAM_NAMES = exports.SnapValidationFailureReason = exports.SnapIdPrefixes = exports.assertIsNpmSnapPackageJson = exports.isNpmSnapPackageJson = exports.NpmSnapPackageJsonStruct = exports.NameStruct = exports.
|
|
3
|
+
exports.isValidUrl = exports.uri = exports.SNAP_EXPORT_NAMES = exports.HandlerType = exports.SNAP_STREAM_NAMES = exports.SnapValidationFailureReason = exports.SnapIdPrefixes = exports.assertIsNpmSnapPackageJson = exports.isNpmSnapPackageJson = exports.NpmSnapPackageJsonStruct = exports.NameStruct = exports.NpmSnapFileNames = void 0;
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
|
-
const semver_1 = require("semver");
|
|
6
5
|
const superstruct_1 = require("superstruct");
|
|
6
|
+
const versions_1 = require("./versions");
|
|
7
7
|
var NpmSnapFileNames;
|
|
8
8
|
(function (NpmSnapFileNames) {
|
|
9
9
|
NpmSnapFileNames["PackageJson"] = "package.json";
|
|
10
10
|
NpmSnapFileNames["Manifest"] = "snap.manifest.json";
|
|
11
11
|
})(NpmSnapFileNames = exports.NpmSnapFileNames || (exports.NpmSnapFileNames = {}));
|
|
12
|
-
/**
|
|
13
|
-
* A struct for validating a version string.
|
|
14
|
-
*/
|
|
15
|
-
exports.VersionStruct = (0, superstruct_1.refine)((0, superstruct_1.string)(), 'Version', (value) => {
|
|
16
|
-
return (0, semver_1.valid)(value) !== null;
|
|
17
|
-
});
|
|
18
12
|
exports.NameStruct = (0, superstruct_1.size)((0, superstruct_1.pattern)((0, superstruct_1.string)(), /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/u), 1, 214);
|
|
19
13
|
// Note we use `type` instead of `object` here, because the latter does not
|
|
20
14
|
// allow unknown keys.
|
|
21
15
|
exports.NpmSnapPackageJsonStruct = (0, superstruct_1.type)({
|
|
22
|
-
version:
|
|
16
|
+
version: versions_1.VersionStruct,
|
|
23
17
|
name: exports.NameStruct,
|
|
24
18
|
main: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
|
|
25
19
|
repository: (0, superstruct_1.optional)((0, superstruct_1.object)({
|
|
@@ -50,6 +44,7 @@ exports.assertIsNpmSnapPackageJson = assertIsNpmSnapPackageJson;
|
|
|
50
44
|
/**
|
|
51
45
|
* The possible prefixes for snap ids.
|
|
52
46
|
*/
|
|
47
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
53
48
|
var SnapIdPrefixes;
|
|
54
49
|
(function (SnapIdPrefixes) {
|
|
55
50
|
SnapIdPrefixes["npm"] = "npm:";
|
|
@@ -66,11 +61,13 @@ var SnapValidationFailureReason;
|
|
|
66
61
|
SnapValidationFailureReason["RepositoryMismatch"] = "\"repository\" field mismatch";
|
|
67
62
|
SnapValidationFailureReason["ShasumMismatch"] = "\"shasum\" field mismatch";
|
|
68
63
|
})(SnapValidationFailureReason = exports.SnapValidationFailureReason || (exports.SnapValidationFailureReason = {}));
|
|
64
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
69
65
|
var SNAP_STREAM_NAMES;
|
|
70
66
|
(function (SNAP_STREAM_NAMES) {
|
|
71
67
|
SNAP_STREAM_NAMES["JSON_RPC"] = "jsonRpc";
|
|
72
68
|
SNAP_STREAM_NAMES["COMMAND"] = "command";
|
|
73
69
|
})(SNAP_STREAM_NAMES = exports.SNAP_STREAM_NAMES || (exports.SNAP_STREAM_NAMES = {}));
|
|
70
|
+
/* eslint-enable @typescript-eslint/naming-convention */
|
|
74
71
|
var HandlerType;
|
|
75
72
|
(function (HandlerType) {
|
|
76
73
|
HandlerType["OnRpcRequest"] = "onRpcRequest";
|
|
@@ -79,4 +76,27 @@ var HandlerType;
|
|
|
79
76
|
HandlerType["OnCronjob"] = "onCronjob";
|
|
80
77
|
})(HandlerType = exports.HandlerType || (exports.HandlerType = {}));
|
|
81
78
|
exports.SNAP_EXPORT_NAMES = Object.values(HandlerType);
|
|
79
|
+
const uri = (opts = {}) => (0, superstruct_1.refine)((0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.instance)(URL)]), 'uri', (value) => {
|
|
80
|
+
try {
|
|
81
|
+
const url = new URL(value);
|
|
82
|
+
const UrlStruct = (0, superstruct_1.type)(opts);
|
|
83
|
+
(0, superstruct_1.assert)(url, UrlStruct);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
catch (_a) {
|
|
87
|
+
return `Expected URL, got "${value.toString()}".`;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
exports.uri = uri;
|
|
91
|
+
/**
|
|
92
|
+
* Returns whether a given value is a valid URL.
|
|
93
|
+
*
|
|
94
|
+
* @param url - The value to check.
|
|
95
|
+
* @param opts - Optional constraints for url checking.
|
|
96
|
+
* @returns Whether `url` is valid URL or not.
|
|
97
|
+
*/
|
|
98
|
+
function isValidUrl(url, opts = {}) {
|
|
99
|
+
return (0, superstruct_1.is)(url, (0, exports.uri)(opts));
|
|
100
|
+
}
|
|
101
|
+
exports.isValidUrl = isValidUrl;
|
|
82
102
|
//# sourceMappingURL=types.js.map
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAIA,2CAAqD;AACrD,
|
|
1
|
+
{"version":3,"file":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AE,IAAI,CAAC;KACnD;AACH,CAAC,CAAC,CAAC;AAXQ,QAAA,GAAG,OAWX;AAEL;;;;;;GAMG;AACH,SAAgB,UAAU,CACxB,GAAY,EACZ,OAAwB,EAAE;IAE1B,OAAO,IAAA,gBAAE,EAAC,GAAG,EAAE,IAAA,WAAG,EAAC,IAAI,CAAC,CAAC,CAAC;AAC5B,CAAC;AALD,gCAKC","sourcesContent":["import {\n SnapFunctionExports,\n SnapKeyring as Keyring,\n} from '@metamask/snaps-types';\nimport { assertStruct, Json } from '@metamask/utils';\nimport {\n Infer,\n instance,\n is,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n assert as assertSuperstruct,\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\ntype UriOptions<T extends string> = {\n protocol?: Struct<T>;\n hash?: Struct<T>;\n port?: Struct<T>;\n hostname?: Struct<T>;\n pathname?: Struct<T>;\n search?: Struct<T>;\n};\nexport const uri = (opts: UriOptions<any> = {}) =>\n refine(union([string(), instance(URL)]), 'uri', (value) => {\n try {\n const url = new URL(value);\n\n const UrlStruct = type(opts);\n assertSuperstruct(url, UrlStruct);\n return true;\n } catch {\n return `Expected URL, got \"${value.toString()}\".`;\n }\n });\n\n/**\n * Returns whether a given value is a valid URL.\n *\n * @param url - The value to check.\n * @param opts - Optional constraints for url checking.\n * @returns Whether `url` is valid URL or not.\n */\nexport function isValidUrl(\n url: unknown,\n opts: UriOptions<any> = {},\n): url is string | URL {\n return is(url, uri(opts));\n}\n"]}
|
package/dist/versions.d.ts
CHANGED
|
@@ -1,5 +1,80 @@
|
|
|
1
1
|
import { Json } from '@metamask/utils';
|
|
2
|
-
|
|
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,15 @@ 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:
|
|
85
|
+
export declare function gtVersion(version1: SemVerVersion, version2: SemVerVersion): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Checks whether a SemVer version is greater than all possibilities in a range.
|
|
88
|
+
*
|
|
89
|
+
* @param version - A SemvVer version.
|
|
90
|
+
* @param range - The range to check against.
|
|
91
|
+
* @returns `version > range`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function gtRange(version: SemVerVersion, range: SemVerRange): boolean;
|
|
11
94
|
/**
|
|
12
95
|
* Returns whether a SemVer version satisfies a SemVer range.
|
|
13
96
|
*
|
|
@@ -15,7 +98,7 @@ export declare function gtVersion(version1: string, version2: string): boolean;
|
|
|
15
98
|
* @param versionRange - The SemVer version range to check against.
|
|
16
99
|
* @returns Whether the version satisfied the version range.
|
|
17
100
|
*/
|
|
18
|
-
export declare function satisfiesVersionRange(version:
|
|
101
|
+
export declare function satisfiesVersionRange(version: SemVerVersion, versionRange: SemVerRange): boolean;
|
|
19
102
|
/**
|
|
20
103
|
* Return the highest version in the list that satisfies the range, or `null` if
|
|
21
104
|
* none of them do. For the satisfaction check, pre-release versions will only
|
|
@@ -26,7 +109,7 @@ export declare function satisfiesVersionRange(version: string, versionRange: str
|
|
|
26
109
|
* @returns The highest version in the list that satisfies the range,
|
|
27
110
|
* or `null` if none of them do.
|
|
28
111
|
*/
|
|
29
|
-
export declare function getTargetVersion(versions:
|
|
112
|
+
export declare function getTargetVersion(versions: SemVerVersion[], versionRange: SemVerRange): SemVerVersion | null;
|
|
30
113
|
/**
|
|
31
114
|
* Parse a version received by some subject attempting to access a snap.
|
|
32
115
|
*
|
|
@@ -34,11 +117,5 @@ export declare function getTargetVersion(versions: string[], versionRange: strin
|
|
|
34
117
|
* @returns `*` if the version is `undefined` or `latest", otherwise returns
|
|
35
118
|
* the specified version.
|
|
36
119
|
*/
|
|
37
|
-
export declare function
|
|
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;
|
|
120
|
+
export declare function resolveVersionRange(version?: Json): [error: undefined, range: SemVerRange] | [error: Error, range: undefined];
|
|
121
|
+
export {};
|
package/dist/versions.js
CHANGED
|
@@ -1,8 +1,63 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.resolveVersionRange = exports.getTargetVersion = exports.satisfiesVersionRange = exports.gtRange = 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,9 +66,20 @@ 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
|
|
69
|
+
return (0, semver_1.gt)(version1, version2);
|
|
15
70
|
}
|
|
16
71
|
exports.gtVersion = gtVersion;
|
|
72
|
+
/**
|
|
73
|
+
* Checks whether a SemVer version is greater than all possibilities in a range.
|
|
74
|
+
*
|
|
75
|
+
* @param version - A SemvVer version.
|
|
76
|
+
* @param range - The range to check against.
|
|
77
|
+
* @returns `version > range`.
|
|
78
|
+
*/
|
|
79
|
+
function gtRange(version, range) {
|
|
80
|
+
return (0, semver_1.gtr)(version, range);
|
|
81
|
+
}
|
|
82
|
+
exports.gtRange = gtRange;
|
|
17
83
|
/**
|
|
18
84
|
* Returns whether a SemVer version satisfies a SemVer range.
|
|
19
85
|
*
|
|
@@ -56,22 +122,11 @@ exports.getTargetVersion = getTargetVersion;
|
|
|
56
122
|
* @returns `*` if the version is `undefined` or `latest", otherwise returns
|
|
57
123
|
* the specified version.
|
|
58
124
|
*/
|
|
59
|
-
function
|
|
125
|
+
function resolveVersionRange(version) {
|
|
60
126
|
if (version === undefined || version === 'latest') {
|
|
61
|
-
return exports.DEFAULT_REQUESTED_SNAP_VERSION;
|
|
127
|
+
return [undefined, exports.DEFAULT_REQUESTED_SNAP_VERSION];
|
|
62
128
|
}
|
|
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);
|
|
129
|
+
return (0, superstruct_1.validate)(version, exports.VersionRangeStruct);
|
|
75
130
|
}
|
|
76
|
-
exports.
|
|
131
|
+
exports.resolveVersionRange = resolveVersionRange;
|
|
77
132
|
//# sourceMappingURL=versions.js.map
|
package/dist/versions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"versions.js","sourceRoot":"","sources":["../src/versions.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"versions.js","sourceRoot":"","sources":["../src/versions.ts"],"names":[],"mappings":";;;AAAA,2CAAqD;AACrD,mCAOgB;AAChB,6CAAmE;AAItD,QAAA,8BAA8B,GAAG,GAAkB,CAAC;AA8CjE;;GAEG;AACU,QAAA,aAAa,G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EAAE,KAAkB;IAChE,OAAO,IAAA,YAAS,EAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AACnC,CAAC;AAFD,0BAEC;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 gtr as gtrSemver,\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 * Checks whether a SemVer version is greater than all possibilities in a range.\n *\n * @param version - A SemvVer version.\n * @param range - The range to check against.\n * @returns `version > range`.\n */\nexport function gtRange(version: SemVerVersion, range: SemVerRange): boolean {\n return gtrSemver(version, range);\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"]}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.
|
|
3
|
+
*
|
|
4
|
+
* This type can be augmented to register custom `data` types.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* declare module '@metamask/snaps-utils' {
|
|
8
|
+
* interface DataMap {
|
|
9
|
+
* // `file.data.name` is typed as `string`
|
|
10
|
+
* name: string
|
|
11
|
+
* }
|
|
12
|
+
* }
|
|
13
|
+
*/
|
|
14
|
+
export interface DataMap {
|
|
15
|
+
}
|
|
16
|
+
export declare type Value = string | Uint8Array;
|
|
17
|
+
export declare type Compatible<Result = unknown> = string | Uint8Array | Options<Result>;
|
|
18
|
+
export declare type Data = Record<string, unknown> & Partial<DataMap>;
|
|
19
|
+
export declare type Options<Result = unknown> = {
|
|
20
|
+
value: Value;
|
|
21
|
+
path?: string;
|
|
22
|
+
data?: Data;
|
|
23
|
+
result?: Result;
|
|
24
|
+
};
|
|
25
|
+
export declare class VirtualFile<Result = unknown> {
|
|
26
|
+
constructor(value?: Compatible<Result>);
|
|
27
|
+
value: Value;
|
|
28
|
+
result: Result;
|
|
29
|
+
data: Data;
|
|
30
|
+
path: string;
|
|
31
|
+
toString(encoding?: string): string;
|
|
32
|
+
clone(): VirtualFile<Result>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VirtualFile = void 0;
|
|
4
|
+
// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review
|
|
5
|
+
// TODO(ritave): Streaming vfile contents similar to vinyl maybe?
|
|
6
|
+
// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile
|
|
7
|
+
// similar to unified instead of throwing "ProgrammaticallyFixableErrors".
|
|
8
|
+
//
|
|
9
|
+
// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.
|
|
10
|
+
// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because
|
|
11
|
+
// we're calculating checksums based on original path.
|
|
12
|
+
const utils_1 = require("@metamask/utils");
|
|
13
|
+
const deep_clone_1 = require("../deep-clone");
|
|
14
|
+
class VirtualFile {
|
|
15
|
+
constructor(value) {
|
|
16
|
+
var _a, _b, _c, _d;
|
|
17
|
+
let options;
|
|
18
|
+
if (typeof value === 'string' || value instanceof Uint8Array) {
|
|
19
|
+
options = { value };
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
options = value;
|
|
23
|
+
}
|
|
24
|
+
this.value = (_a = options === null || options === void 0 ? void 0 : options.value) !== null && _a !== void 0 ? _a : '';
|
|
25
|
+
// This situations happens when there's no .result used,
|
|
26
|
+
// we expect the file to have default generic in that situation:
|
|
27
|
+
// VirtualFile<unknown> which will handle undefined properly
|
|
28
|
+
//
|
|
29
|
+
// While not 100% type safe, it'll be way less frustrating to work with.
|
|
30
|
+
// The alternative would be to have VirtualFile.result be Result | undefined
|
|
31
|
+
// and that would result in needing to branch out and check in all situations.
|
|
32
|
+
//
|
|
33
|
+
// In short, optimizing for most common use case.
|
|
34
|
+
this.result = (_b = options === null || options === void 0 ? void 0 : options.result) !== null && _b !== void 0 ? _b : undefined;
|
|
35
|
+
this.data = (_c = options === null || options === void 0 ? void 0 : options.data) !== null && _c !== void 0 ? _c : {};
|
|
36
|
+
this.path = (_d = options === null || options === void 0 ? void 0 : options.path) !== null && _d !== void 0 ? _d : '/';
|
|
37
|
+
}
|
|
38
|
+
toString(encoding) {
|
|
39
|
+
if (typeof this.value === 'string') {
|
|
40
|
+
(0, utils_1.assert)(encoding === undefined, 'Tried to encode string.');
|
|
41
|
+
return this.value;
|
|
42
|
+
}
|
|
43
|
+
const decoder = new TextDecoder(encoding);
|
|
44
|
+
return decoder.decode(this.value);
|
|
45
|
+
}
|
|
46
|
+
clone() {
|
|
47
|
+
const vfile = new VirtualFile();
|
|
48
|
+
if (typeof this.value === 'string') {
|
|
49
|
+
vfile.value = this.value;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
// deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array
|
|
53
|
+
vfile.value = this.value.slice(0);
|
|
54
|
+
}
|
|
55
|
+
vfile.result = (0, deep_clone_1.deepClone)(this.result);
|
|
56
|
+
vfile.data = (0, deep_clone_1.deepClone)(this.data);
|
|
57
|
+
vfile.path = this.path;
|
|
58
|
+
return vfile;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.VirtualFile = VirtualFile;
|
|
62
|
+
//# sourceMappingURL=VirtualFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"VirtualFile.js","sourceRoot":"","sources":["../../src/virtual-file/VirtualFile.ts"],"names":[],"mappings":";;;AAAA,0HAA0H;AAC1H,iEAAiE;AACjE,2FAA2F;AAC3F,wFAAwF;AACxF,EAAE;AACF,oHAAoH;AACpH,qGAAqG;AACrG,sDAAsD;AACtD,2CAAyC;AAEzC,8CAA0C;AA+B1C,MAAa,WAAW;IACtB,YAAY,KA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AAM;YACL,mFAAmF;YACnF,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;SACnC;QACD,KAAK,CAAC,MAAM,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QACtC,KAAK,CAAC,IAAI,GAAG,IAAA,sBAAS,EAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AAtDD,kCAsDC","sourcesContent":["// TODO(ritave): Move into separate package @metamask/vfile / @metamask/utils + @metamask/to-vfile when passes code review\n// TODO(ritave): Streaming vfile contents similar to vinyl maybe?\n// TODO(ritave): Move fixing manifest in cli and bundler plugins to write messages to vfile\n// similar to unified instead of throwing \"ProgrammaticallyFixableErrors\".\n//\n// Using https://github.com/vfile/vfile would be helpful, but they only support ESM and we need to support CommonJS.\n// https://github.com/gulpjs/vinyl is also good, but they normalize paths, which we can't do, because\n// we're calculating checksums based on original path.\nimport { assert } from '@metamask/utils';\n\nimport { deepClone } from '../deep-clone';\n\n/**\n * This map registers the type of the {@link VirtualFile.data} key of a {@link VirtualFile}.\n *\n * This type can be augmented to register custom `data` types.\n *\n * @example\n * declare module '@metamask/snaps-utils' {\n * interface DataMap {\n * // `file.data.name` is typed as `string`\n * name: string\n * }\n * }\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-empty-interface\nexport interface DataMap {}\n\nexport type Value = string | Uint8Array;\nexport type Compatible<Result = unknown> =\n | string\n | Uint8Array\n | Options<Result>;\nexport type Data = Record<string, unknown> & Partial<DataMap>;\nexport type Options<Result = unknown> = {\n value: Value;\n path?: string;\n data?: Data;\n result?: Result;\n};\n\nexport class VirtualFile<Result = unknown> {\n constructor(value?: Compatible<Result>) {\n let options: Options | undefined;\n if (typeof value === 'string' || value instanceof Uint8Array) {\n options = { value };\n } else {\n options = value;\n }\n\n this.value = options?.value ?? '';\n // This situations happens when there's no .result used,\n // we expect the file to have default generic in that situation:\n // VirtualFile<unknown> which will handle undefined properly\n //\n // While not 100% type safe, it'll be way less frustrating to work with.\n // The alternative would be to have VirtualFile.result be Result | undefined\n // and that would result in needing to branch out and check in all situations.\n //\n // In short, optimizing for most common use case.\n this.result = options?.result ?? (undefined as any);\n this.data = options?.data ?? {};\n this.path = options?.path ?? '/';\n }\n\n value: Value;\n\n result: Result;\n\n data: Data;\n\n path: string;\n\n toString(encoding?: string) {\n if (typeof this.value === 'string') {\n assert(encoding === undefined, 'Tried to encode string.');\n return this.value;\n }\n const decoder = new TextDecoder(encoding);\n return decoder.decode(this.value);\n }\n\n clone() {\n const vfile = new VirtualFile<Result>();\n if (typeof this.value === 'string') {\n vfile.value = this.value;\n } else {\n // deep-clone doesn't clone Buffer properly, even if it's a sub-class of Uint8Array\n vfile.value = this.value.slice(0);\n }\n vfile.result = deepClone(this.result);\n vfile.data = deepClone(this.data);\n vfile.path = this.path;\n return vfile;\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './VirtualFile';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./VirtualFile"), exports);
|
|
18
|
+
//# sourceMappingURL=index.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../../src/virtual-file/index.browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B","sourcesContent":["export * from './VirtualFile';\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./toVirtualFile"), exports);
|
|
18
|
+
__exportStar(require("./VirtualFile"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/virtual-file/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC;AAChC,gDAA8B","sourcesContent":["export * from './toVirtualFile';\nexport * from './VirtualFile';\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import fsPromises from 'fs/promises';
|
|
4
|
+
import { VirtualFile } from './VirtualFile';
|
|
5
|
+
/**
|
|
6
|
+
* Reads a file from filesystem and creates a vfile.
|
|
7
|
+
*
|
|
8
|
+
* @param path - Filesystem path to load the contents from.
|
|
9
|
+
* @param encoding - Optional encoding to pass down to fs.readFile.
|
|
10
|
+
* @returns Promise returning VFile with loaded file contents.
|
|
11
|
+
*/
|
|
12
|
+
export declare function readVirtualFile(path: string, encoding?: BufferEncoding | null): Promise<VirtualFile<unknown>>;
|
|
13
|
+
declare type WriteVFileOptions = Exclude<Parameters<typeof fsPromises['writeFile']>[2], undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Writes vfile to filesystem.
|
|
16
|
+
*
|
|
17
|
+
* @param vfile - The vfile to write.
|
|
18
|
+
* @param options - Options to pass down to fs.writeFile.
|
|
19
|
+
*/
|
|
20
|
+
export declare function writeVirtualFile(vfile: VirtualFile, options?: WriteVFileOptions): Promise<void>;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.writeVirtualFile = exports.readVirtualFile = void 0;
|
|
7
|
+
const promises_1 = __importDefault(require("fs/promises"));
|
|
8
|
+
const VirtualFile_1 = require("./VirtualFile");
|
|
9
|
+
/**
|
|
10
|
+
* Reads a file from filesystem and creates a vfile.
|
|
11
|
+
*
|
|
12
|
+
* @param path - Filesystem path to load the contents from.
|
|
13
|
+
* @param encoding - Optional encoding to pass down to fs.readFile.
|
|
14
|
+
* @returns Promise returning VFile with loaded file contents.
|
|
15
|
+
*/
|
|
16
|
+
async function readVirtualFile(path, encoding = null) {
|
|
17
|
+
return new VirtualFile_1.VirtualFile({
|
|
18
|
+
path,
|
|
19
|
+
value: await promises_1.default.readFile(path, { encoding }),
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
exports.readVirtualFile = readVirtualFile;
|
|
23
|
+
/**
|
|
24
|
+
* Writes vfile to filesystem.
|
|
25
|
+
*
|
|
26
|
+
* @param vfile - The vfile to write.
|
|
27
|
+
* @param options - Options to pass down to fs.writeFile.
|
|
28
|
+
*/
|
|
29
|
+
async function writeVirtualFile(vfile, options) {
|
|
30
|
+
return promises_1.default.writeFile(vfile.path, vfile.value, options);
|
|
31
|
+
}
|
|
32
|
+
exports.writeVirtualFile = writeVirtualFile;
|
|
33
|
+
//# sourceMappingURL=toVirtualFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toVirtualFile.js","sourceRoot":"","sources":["../../src/virtual-file/toVirtualFile.ts"],"names":[],"mappings":";;;;;;AAAA,2DAAqC;AAErC,+CAA4C;AAE5C;;;;;;GAMG;AACI,KAAK,UAAU,eAAe,CACnC,IAAY,EACZ,WAAkC,IAAI;IAEtC,OAAO,IAAI,yBAAW,CAAC;QACrB,IAAI;QACJ,KAAK,EAAE,MAAM,kBAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,QAAQ,EAAE,CAAC;KACrD,CAAC,CAAC;AACL,CAAC;AARD,0CAQC;AAOD;;;;;GAKG;AACI,KAAK,UAAU,gBAAgB,CACpC,KAAkB,EAClB,OAA2B;IAE3B,OAAO,kBAAU,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;AAChE,CAAC;AALD,4CAKC","sourcesContent":["import fsPromises from 'fs/promises';\n\nimport { VirtualFile } from './VirtualFile';\n\n/**\n * Reads a file from filesystem and creates a vfile.\n *\n * @param path - Filesystem path to load the contents from.\n * @param encoding - Optional encoding to pass down to fs.readFile.\n * @returns Promise returning VFile with loaded file contents.\n */\nexport async function readVirtualFile(\n path: string,\n encoding: BufferEncoding | null = null,\n) {\n return new VirtualFile({\n path,\n value: await fsPromises.readFile(path, { encoding }),\n });\n}\n\ntype WriteVFileOptions = Exclude<\n Parameters<typeof fsPromises['writeFile']>[2],\n undefined\n>;\n\n/**\n * Writes vfile to filesystem.\n *\n * @param vfile - The vfile to write.\n * @param options - Options to pass down to fs.writeFile.\n */\nexport async function writeVirtualFile(\n vfile: VirtualFile,\n options?: WriteVFileOptions,\n) {\n return fsPromises.writeFile(vfile.path, vfile.value, options);\n}\n"]}
|