@metamask/snaps-utils 0.28.0 → 0.29.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.
Files changed (43) hide show
  1. package/dist/caveats.d.ts +5 -1
  2. package/dist/caveats.js +4 -0
  3. package/dist/caveats.js.map +1 -1
  4. package/dist/checksum.d.ts +21 -0
  5. package/dist/checksum.js +42 -0
  6. package/dist/checksum.js.map +1 -0
  7. package/dist/eval-worker.js +1 -0
  8. package/dist/eval-worker.js.map +1 -1
  9. package/dist/fs.d.ts +2 -11
  10. package/dist/fs.js +8 -17
  11. package/dist/fs.js.map +1 -1
  12. package/dist/handlers.d.ts +0 -7
  13. package/dist/handlers.js.map +1 -1
  14. package/dist/index.browser.d.ts +2 -0
  15. package/dist/index.browser.js +2 -0
  16. package/dist/index.browser.js.map +1 -1
  17. package/dist/index.d.ts +2 -0
  18. package/dist/index.js +2 -0
  19. package/dist/index.js.map +1 -1
  20. package/dist/logging.d.ts +49 -0
  21. package/dist/logging.js +67 -0
  22. package/dist/logging.js.map +1 -0
  23. package/dist/manifest/manifest.d.ts +16 -6
  24. package/dist/manifest/manifest.js +85 -48
  25. package/dist/manifest/manifest.js.map +1 -1
  26. package/dist/manifest/validation.d.ts +4 -4
  27. package/dist/manifest/validation.js +2 -5
  28. package/dist/manifest/validation.js.map +1 -1
  29. package/dist/namespace.d.ts +3 -3
  30. package/dist/npm.js +5 -4
  31. package/dist/npm.js.map +1 -1
  32. package/dist/post-process.js +2 -1
  33. package/dist/post-process.js.map +1 -1
  34. package/dist/snaps.d.ts +25 -22
  35. package/dist/snaps.js +60 -24
  36. package/dist/snaps.js.map +1 -1
  37. package/dist/types.d.ts +21 -8
  38. package/dist/types.js +3 -1
  39. package/dist/types.js.map +1 -1
  40. package/dist/virtual-file/toVirtualFile.d.ts +1 -1
  41. package/dist/virtual-file/toVirtualFile.js +3 -6
  42. package/dist/virtual-file/toVirtualFile.js.map +1 -1
  43. package/package.json +5 -3
@@ -14,7 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
17
+ exports.validateNpmSnapManifest = exports.getWritableManifest = exports.getSnapIcon = exports.getSnapSourceCode = exports.fixManifest = exports.checkManifest = void 0;
18
18
  const utils_1 = require("@metamask/utils");
19
19
  const fast_deep_equal_1 = __importDefault(require("fast-deep-equal"));
20
20
  const fs_1 = require("fs");
@@ -24,6 +24,7 @@ const fs_2 = require("../fs");
24
24
  const npm_1 = require("../npm");
25
25
  const snaps_1 = require("../snaps");
26
26
  const types_1 = require("../types");
27
+ const virtual_file_1 = require("../virtual-file");
27
28
  const MANIFEST_SORT_ORDER = {
28
29
  version: 1,
29
30
  description: 2,
@@ -45,22 +46,18 @@ const MANIFEST_SORT_ORDER = {
45
46
  * were encountered during processing of the manifest files.
46
47
  */
47
48
  async function checkManifest(basePath, writeManifest = true, sourceCode) {
48
- var _a, _b, _c;
49
49
  const warnings = [];
50
50
  const errors = [];
51
51
  let updated = false;
52
- const unvalidatedManifest = await (0, fs_2.readSnapJsonFile)(basePath, types_1.NpmSnapFileNames.Manifest);
53
- const iconPath = unvalidatedManifest && typeof unvalidatedManifest === 'object'
54
- ? /* istanbul ignore next */
55
- (_c = (_b = (_a = unvalidatedManifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.iconPath
56
- : /* istanbul ignore next */
57
- undefined;
52
+ const manifestPath = path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest);
53
+ const manifestFile = await (0, fs_2.readJsonFile)(manifestPath);
54
+ const unvalidatedManifest = manifestFile.result;
55
+ const packageFile = await (0, fs_2.readJsonFile)(path_1.default.join(basePath, types_1.NpmSnapFileNames.PackageJson));
58
56
  const snapFiles = {
59
- manifest: unvalidatedManifest,
60
- packageJson: await (0, fs_2.readSnapJsonFile)(basePath, types_1.NpmSnapFileNames.PackageJson),
61
- sourceCode: sourceCode !== null && sourceCode !== void 0 ? sourceCode : (await getSnapSourceCode(basePath, unvalidatedManifest)),
62
- svgIcon: iconPath &&
63
- (await fs_1.promises.readFile(path_1.default.join(basePath, iconPath), 'utf8')),
57
+ manifest: manifestFile,
58
+ packageJson: packageFile,
59
+ sourceCode: await getSnapSourceCode(basePath, unvalidatedManifest, sourceCode),
60
+ svgIcon: await getSnapIcon(basePath, unvalidatedManifest),
64
61
  };
65
62
  let manifest;
66
63
  try {
@@ -101,9 +98,10 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
101
98
  throw error;
102
99
  }
103
100
  }
104
- // TypeScript doesn't see that the 'manifest' variable must be of type
105
- // SnapManifest at this point, so we cast it.
106
- const validatedManifest = manifest;
101
+ // TypeScript assumes `manifest` can still be undefined, that is not the case.
102
+ // But we assert to keep TypeScript happy.
103
+ (0, utils_1.assert)(manifest);
104
+ const validatedManifest = manifest.result;
107
105
  // Check presence of recommended keys
108
106
  const recommendedFields = ['repository'];
109
107
  const missingRecommendedFields = recommendedFields.filter((key) => !validatedManifest[key]);
@@ -114,7 +112,10 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
114
112
  }
115
113
  if (writeManifest) {
116
114
  try {
117
- await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`);
115
+ const newManifest = `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\n`;
116
+ if (updated || newManifest !== manifestFile.value) {
117
+ await fs_1.promises.writeFile(path_1.default.join(basePath, types_1.NpmSnapFileNames.Manifest), newManifest);
118
+ }
118
119
  }
119
120
  catch (error) {
120
121
  // Note: This error isn't pushed to the errors array, because it's not an
@@ -135,28 +136,31 @@ exports.checkManifest = checkManifest;
135
136
  * @returns A copy of the manifest file where the cause of the error is fixed.
136
137
  */
137
138
  function fixManifest(snapFiles, error) {
138
- const { manifest, packageJson, sourceCode } = snapFiles;
139
- const manifestCopy = (0, deep_clone_1.deepClone)(manifest);
139
+ const { manifest, packageJson } = snapFiles;
140
+ const clonedFile = manifest.clone();
141
+ const manifestCopy = clonedFile.result;
140
142
  switch (error.reason) {
141
143
  case types_1.SnapValidationFailureReason.NameMismatch:
142
- manifestCopy.source.location.npm.packageName = packageJson.name;
144
+ manifestCopy.source.location.npm.packageName = packageJson.result.name;
143
145
  break;
144
146
  case types_1.SnapValidationFailureReason.VersionMismatch:
145
- manifestCopy.version = packageJson.version;
147
+ manifestCopy.version = packageJson.result.version;
146
148
  break;
147
149
  case types_1.SnapValidationFailureReason.RepositoryMismatch:
148
- manifestCopy.repository = packageJson.repository
149
- ? (0, deep_clone_1.deepClone)(packageJson.repository)
150
+ manifestCopy.repository = packageJson.result.repository
151
+ ? (0, deep_clone_1.deepClone)(packageJson.result.repository)
150
152
  : undefined;
151
153
  break;
152
154
  case types_1.SnapValidationFailureReason.ShasumMismatch:
153
- manifestCopy.source.shasum = (0, snaps_1.getSnapSourceShasum)(sourceCode);
155
+ manifestCopy.source.shasum = (0, snaps_1.getSnapChecksum)(snapFiles);
154
156
  break;
155
157
  /* istanbul ignore next */
156
158
  default:
157
159
  (0, utils_1.assertExhaustive)(error.reason);
158
160
  }
159
- return manifestCopy;
161
+ clonedFile.result = manifestCopy;
162
+ clonedFile.value = JSON.stringify(manifestCopy);
163
+ return clonedFile;
160
164
  }
161
165
  exports.fixManifest = fixManifest;
162
166
  /**
@@ -165,24 +169,59 @@ exports.fixManifest = fixManifest;
165
169
  *
166
170
  * @param basePath - The path to the folder with the manifest files.
167
171
  * @param manifest - The unvalidated Snap manifest file contents.
172
+ * @param sourceCode - Override source code for plugins.
168
173
  * @returns The contents of the bundle file, if any.
169
174
  */
170
- async function getSnapSourceCode(basePath, manifest) {
175
+ async function getSnapSourceCode(basePath, manifest, sourceCode) {
171
176
  var _a, _b, _c;
172
- if (manifest && typeof manifest === 'object' && !Array.isArray(manifest)) {
173
- const sourceFilePath = (_c = (_b = (_a = manifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.filePath;
174
- try {
175
- return sourceFilePath
176
- ? await fs_1.promises.readFile(path_1.default.join(basePath, sourceFilePath), 'utf8')
177
- : undefined;
178
- }
179
- catch (error) {
180
- throw new Error(`Failed to read Snap bundle file: ${error.message}`);
181
- }
177
+ if (!(0, utils_1.isPlainObject)(manifest)) {
178
+ return undefined;
179
+ }
180
+ const sourceFilePath = (_c = (_b = (_a = manifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.filePath;
181
+ if (!sourceFilePath) {
182
+ return undefined;
183
+ }
184
+ if (sourceCode) {
185
+ return new virtual_file_1.VirtualFile({
186
+ path: path_1.default.join(basePath, sourceFilePath),
187
+ value: sourceCode,
188
+ });
189
+ }
190
+ try {
191
+ const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, sourceFilePath), 'utf8');
192
+ return virtualFile;
193
+ }
194
+ catch (error) {
195
+ throw new Error(`Failed to read Snap bundle file: ${error.message}`);
182
196
  }
183
- return undefined;
184
197
  }
185
198
  exports.getSnapSourceCode = getSnapSourceCode;
199
+ /**
200
+ * Given an unvalidated Snap manifest, attempts to extract the location of the
201
+ * icon and read the file.
202
+ *
203
+ * @param basePath - The path to the folder with the manifest files.
204
+ * @param manifest - The unvalidated Snap manifest file contents.
205
+ * @returns The contents of the icon, if any.
206
+ */
207
+ async function getSnapIcon(basePath, manifest) {
208
+ var _a, _b, _c;
209
+ if (!(0, utils_1.isPlainObject)(manifest)) {
210
+ return undefined;
211
+ }
212
+ const iconPath = (_c = (_b = (_a = manifest.source) === null || _a === void 0 ? void 0 : _a.location) === null || _b === void 0 ? void 0 : _b.npm) === null || _c === void 0 ? void 0 : _c.iconPath;
213
+ if (!iconPath) {
214
+ return undefined;
215
+ }
216
+ try {
217
+ const virtualFile = await (0, virtual_file_1.readVirtualFile)(path_1.default.join(basePath, iconPath), 'utf8');
218
+ return virtualFile;
219
+ }
220
+ catch (error) {
221
+ throw new Error(`Failed to read Snap icon file: ${error.message}`);
222
+ }
223
+ }
224
+ exports.getSnapIcon = getSnapIcon;
186
225
  /**
187
226
  * Sorts the given manifest in our preferred sort order and removes the
188
227
  * `repository` field if it is falsy (it may be `null`).
@@ -207,16 +246,15 @@ exports.getWritableManifest = getWritableManifest;
207
246
  * @param snapFiles.manifest - The npm Snap manifest to validate.
208
247
  * @param snapFiles.packageJson - The npm Snap's `package.json`.
209
248
  * @param snapFiles.sourceCode - The Snap's source code.
210
- * @returns A tuple containing the validated snap manifest, snap source code,
211
- * and `package.json`.
249
+ * @param snapFiles.svgIcon - The Snap's optional icon.
212
250
  */
213
- function validateNpmSnapManifest({ manifest, packageJson, sourceCode, }) {
214
- const packageJsonName = packageJson.name;
215
- const packageJsonVersion = packageJson.version;
216
- const packageJsonRepository = packageJson.repository;
217
- const manifestPackageName = manifest.source.location.npm.packageName;
218
- const manifestPackageVersion = manifest.version;
219
- const manifestRepository = manifest.repository;
251
+ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, svgIcon, }) {
252
+ const packageJsonName = packageJson.result.name;
253
+ const packageJsonVersion = packageJson.result.version;
254
+ const packageJsonRepository = packageJson.result.repository;
255
+ const manifestPackageName = manifest.result.source.location.npm.packageName;
256
+ const manifestPackageVersion = manifest.result.version;
257
+ const manifestRepository = manifest.result.repository;
220
258
  if (packageJsonName !== manifestPackageName) {
221
259
  throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" npm package name ("${manifestPackageName}") does not match the "${types_1.NpmSnapFileNames.PackageJson}" "name" field ("${packageJsonName}").`, types_1.SnapValidationFailureReason.NameMismatch);
222
260
  }
@@ -230,8 +268,7 @@ function validateNpmSnapManifest({ manifest, packageJson, sourceCode, }) {
230
268
  !(0, fast_deep_equal_1.default)(packageJsonRepository, manifestRepository)) {
231
269
  throw new snaps_1.ProgrammaticallyFixableSnapError(`"${types_1.NpmSnapFileNames.Manifest}" "repository" field does not match the "${types_1.NpmSnapFileNames.PackageJson}" "repository" field.`, types_1.SnapValidationFailureReason.RepositoryMismatch);
232
270
  }
233
- (0, snaps_1.validateSnapShasum)(manifest, sourceCode, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
234
- return [manifest, sourceCode, packageJson];
271
+ (0, snaps_1.validateSnapShasum)({ manifest, sourceCode, svgIcon }, `"${types_1.NpmSnapFileNames.Manifest}" "shasum" field does not match computed shasum.`);
235
272
  }
236
273
  exports.validateNpmSnapManifest = validateNpmSnapManifest;
237
274
  //# sourceMappingURL=manifest.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,2CAAyD;AACzD,sEAAwC;AACxC,2BAAoC;AACpC,gDAA6B;AAE7B,8CAA0C;AAC1C,8BAAyC;AACzC,gCAAyC;AACzC,oCAIkB;AAClB,oCAMkB;AAGlB,MAAM,mBAAmB,GAAuC;IAC9D,OAAO,EAAE,CAAC;IACV,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC;IACT,kBAAkB,EAAE,CAAC;IACrB,eAAe,EAAE,CAAC;CACnB,CAAC;AAuBF;;;;;;;;;;GAUG;AACI,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,aAAa,GAAG,IAAI,EACpB,UAAmB;;IAEnB,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,MAAM,MAAM,GAAa,EAAE,CAAC;IAE5B,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,MAAM,mB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,EAAC,qBAAqB,EAAE,kBAAkB,CAAC,EACrD;QACA,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,4CAA4C,wBAAgB,CAAC,WAAW,uBAAuB,EAC5H,mCAA2B,CAAC,kBAAkB,CAC/C,CAAC;KACH;IAED,IAAA,0BAAkB,EAChB,QAAQ,EACR,UAAU,EACV,IAAI,wBAAgB,CAAC,QAAQ,kDAAkD,CAChF,CAAC;IACF,OAAO,CAAC,QAAQ,EAAE,UAAU,EAAE,WAAW,CAAC,CAAC;AAC7C,CAAC;AA7CD,0DA6CC","sourcesContent":["import { Json, assertExhaustive } from '@metamask/utils';\nimport deepEqual from 'fast-deep-equal';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport { deepClone } from '../deep-clone';\nimport { readSnapJsonFile } from '../fs';\nimport { validateNpmSnap } from '../npm';\nimport {\n getSnapSourceShasum,\n ProgrammaticallyFixableSnapError,\n validateSnapShasum,\n} from '../snaps';\nimport {\n NpmSnapFileNames,\n NpmSnapPackageJson,\n SnapFiles,\n SnapValidationFailureReason,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { SnapManifest } from './validation';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n version: 1,\n description: 2,\n proposedName: 3,\n repository: 4,\n source: 5,\n initialPermissions: 6,\n manifestVersion: 7,\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was updated.\n * @property warnings - An array of warnings that were encountered during\n * processing of the manifest files. These warnings are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n * @property errors - An array of errors that were encountered during\n * processing of the manifest files. These errors are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n */\nexport type CheckManifestResult = {\n manifest: SnapManifest;\n updated?: boolean;\n warnings: string[];\n errors: string[];\n};\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param writeManifest - Whether to write the fixed manifest to disk.\n * @param sourceCode - The source code of the Snap.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n writeManifest = true,\n sourceCode?: string,\n): Promise<CheckManifestResult> {\n const warnings: string[] = [];\n const errors: string[] = [];\n\n let updated = false;\n\n const unvalidatedManifest = await readSnapJsonFile(\n basePath,\n NpmSnapFileNames.Manifest,\n );\n\n const iconPath =\n unvalidatedManifest && typeof unvalidatedManifest === 'object'\n ? /* istanbul ignore next */\n (unvalidatedManifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath\n : /* istanbul ignore next */\n undefined;\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: unvalidatedManifest,\n packageJson: await readSnapJsonFile(basePath, NpmSnapFileNames.PackageJson),\n sourceCode:\n sourceCode ?? (await getSnapSourceCode(basePath, unvalidatedManifest)),\n svgIcon:\n iconPath &&\n (await fs.readFile(pathUtils.join(basePath, iconPath), 'utf8')),\n };\n\n let manifest: SnapManifest | undefined;\n try {\n ({ manifest } = validateNpmSnap(snapFiles));\n } catch (error) {\n if (error instanceof ProgrammaticallyFixableSnapError) {\n errors.push(error.message);\n\n // If we get here, the files at least have the correct shape.\n const partiallyValidatedFiles = snapFiles as SnapFiles;\n\n let isInvalid = true;\n let currentError = error;\n const maxAttempts = Object.keys(SnapValidationFailureReason).length;\n\n // Attempt to fix all fixable validation failure reasons. All such reasons\n // are enumerated by the `SnapValidationFailureReason` enum, so we only\n // attempt to fix the manifest the same amount of times as there are\n // reasons in the enum.\n for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {\n manifest = fixManifest(\n manifest\n ? { ...partiallyValidatedFiles, manifest }\n : partiallyValidatedFiles,\n currentError,\n );\n\n try {\n validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });\n\n isInvalid = false;\n } catch (nextValidationError) {\n currentError = nextValidationError;\n /* istanbul ignore next: this should be impossible */\n if (\n !(\n nextValidationError instanceof ProgrammaticallyFixableSnapError\n ) ||\n (attempts === maxAttempts && !isInvalid)\n ) {\n throw new Error(\n `Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\\n${error.message}`,\n );\n }\n\n errors.push(currentError.message);\n }\n }\n\n updated = true;\n } else {\n throw error;\n }\n }\n\n // TypeScript doesn't see that the 'manifest' variable must be of type\n // SnapManifest at this point, so we cast it.\n const validatedManifest = manifest as SnapManifest;\n\n // Check presence of recommended keys\n const recommendedFields = ['repository'] as const;\n\n const missingRecommendedFields = recommendedFields.filter(\n (key) => !validatedManifest[key],\n );\n\n if (missingRecommendedFields.length > 0) {\n warnings.push(\n `Missing recommended package.json properties:\\n${missingRecommendedFields.reduce(\n (allMissing, currentField) => {\n return `${allMissing}\\t${currentField}\\n`;\n },\n '',\n )}`,\n );\n }\n\n if (writeManifest) {\n try {\n await fs.writeFile(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n `${JSON.stringify(getWritableManifest(validatedManifest), null, 2)}\\n`,\n );\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(`Failed to update snap.manifest.json: ${error.message}`);\n }\n }\n\n return { manifest: validatedManifest, updated, warnings, errors };\n}\n\n/**\n * Given the relevant Snap files (manifest, `package.json`, and bundle) and a\n * Snap manifest validation error, fixes the fault in the manifest that caused\n * the error.\n *\n * @param snapFiles - The contents of all Snap files.\n * @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.\n * @returns A copy of the manifest file where the cause of the error is fixed.\n */\nexport function fixManifest(\n snapFiles: SnapFiles,\n error: ProgrammaticallyFixableSnapError,\n): SnapManifest {\n const { manifest, packageJson, sourceCode } = snapFiles;\n const manifestCopy = deepClone(manifest);\n\n switch (error.reason) {\n case SnapValidationFailureReason.NameMismatch:\n manifestCopy.source.location.npm.packageName = packageJson.name;\n break;\n\n case SnapValidationFailureReason.VersionMismatch:\n manifestCopy.version = packageJson.version;\n break;\n\n case SnapValidationFailureReason.RepositoryMismatch:\n manifestCopy.repository = packageJson.repository\n ? deepClone(packageJson.repository)\n : undefined;\n break;\n\n case SnapValidationFailureReason.ShasumMismatch:\n manifestCopy.source.shasum = getSnapSourceShasum(sourceCode);\n break;\n\n /* istanbul ignore next */\n default:\n assertExhaustive(error.reason);\n }\n\n return manifestCopy;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n): Promise<string | undefined> {\n if (manifest && typeof manifest === 'object' && !Array.isArray(manifest)) {\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n try {\n return sourceFilePath\n ? await fs.readFile(pathUtils.join(basePath, sourceFilePath), 'utf8')\n : undefined;\n } catch (error) {\n throw new Error(`Failed to read Snap bundle file: ${error.message}`);\n }\n }\n\n return undefined;\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n\n/**\n * Validates the fields of an npm Snap manifest that has already passed JSON\n * Schema validation.\n *\n * @param snapFiles - The relevant snap files to validate.\n * @param snapFiles.manifest - The npm Snap manifest to validate.\n * @param snapFiles.packageJson - The npm Snap's `package.json`.\n * @param snapFiles.sourceCode - The Snap's source code.\n * @returns A tuple containing the validated snap manifest, snap source code,\n * and `package.json`.\n */\nexport function validateNpmSnapManifest({\n manifest,\n packageJson,\n sourceCode,\n}: SnapFiles): [SnapManifest, string, NpmSnapPackageJson] {\n const packageJsonName = packageJson.name;\n const packageJsonVersion = packageJson.version;\n const packageJsonRepository = packageJson.repository;\n\n const manifestPackageName = manifest.source.location.npm.packageName;\n const manifestPackageVersion = manifest.version;\n const manifestRepository = manifest.repository;\n\n if (packageJsonName !== manifestPackageName) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package name (\"${manifestPackageName}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"name\" field (\"${packageJsonName}\").`,\n SnapValidationFailureReason.NameMismatch,\n );\n }\n\n if (packageJsonVersion !== manifestPackageVersion) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package version (\"${manifestPackageVersion}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"version\" field (\"${packageJsonVersion}\").`,\n SnapValidationFailureReason.VersionMismatch,\n );\n }\n\n if (\n // The repository may be `undefined` in package.json but can only be defined\n // or `null` in the Snap manifest due to TS@<4.4 issues.\n (packageJsonRepository || manifestRepository) &&\n !deepEqual(packageJsonRepository, manifestRepository)\n ) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" \"repository\" field does not match the \"${NpmSnapFileNames.PackageJson}\" \"repository\" field.`,\n SnapValidationFailureReason.RepositoryMismatch,\n );\n }\n\n validateSnapShasum(\n manifest,\n sourceCode,\n `\"${NpmSnapFileNames.Manifest}\" \"shasum\" field does not match computed shasum.`,\n );\n return [manifest, sourceCode, packageJson];\n}\n"]}
1
+ {"version":3,"file":"manifest.js","sourceRoot":"","sources":["../../src/manifest/manifest.ts"],"names":[],"mappings":";;;;;;;;;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,0BAA0B,wBAAgB,CAAC,WAAW,oBAAoB,eAAe,KAAK,EACtK,mCAA2B,CAAC,YAAY,CACzC,CAAC;KACH;IAED,IAAI,kBAAkB,KAAK,sBAAsB,EAAE;QACjD,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,2BAA2B,sBAAsB,0BAA0B,wBAAgB,CAAC,WAAW,uBAAuB,kBAAkB,KAAK,EAClL,mCAA2B,CAAC,eAAe,CAC5C,CAAC;KACH;IAED;IACE,4EAA4E;IAC5E,wDAAwD;IACxD,CAAC,qBAAqB,IAAI,kBAAkB,CAAC;QAC7C,CAAC,IAAA,yBAAS,EAAC,qBAAqB,EAAE,kBAAkB,CAAC,EACrD;QACA,MAAM,IAAI,wCAAgC,CACxC,IAAI,wBAAgB,CAAC,QAAQ,4CAA4C,wBAAgB,CAAC,WAAW,uBAAuB,EAC5H,mCAA2B,CAAC,kBAAkB,CAC/C,CAAC;KACH;IAED,IAAA,0BAAkB,EAChB,EAAE,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,EACjC,IAAI,wBAAgB,CAAC,QAAQ,kDAAkD,CAChF,CAAC;AACJ,CAAC;AA5CD,0DA4CC","sourcesContent":["import { Json, assertExhaustive, assert, isPlainObject } from '@metamask/utils';\nimport deepEqual from 'fast-deep-equal';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport { deepClone } from '../deep-clone';\nimport { readJsonFile } from '../fs';\nimport { validateNpmSnap } from '../npm';\nimport {\n getSnapChecksum,\n ProgrammaticallyFixableSnapError,\n validateSnapShasum,\n} from '../snaps';\nimport {\n NpmSnapFileNames,\n SnapFiles,\n SnapValidationFailureReason,\n UnvalidatedSnapFiles,\n} from '../types';\nimport { readVirtualFile, VirtualFile } from '../virtual-file';\nimport { SnapManifest } from './validation';\n\nconst MANIFEST_SORT_ORDER: Record<keyof SnapManifest, number> = {\n version: 1,\n description: 2,\n proposedName: 3,\n repository: 4,\n source: 5,\n initialPermissions: 6,\n manifestVersion: 7,\n};\n\n/**\n * The result from the `checkManifest` function.\n *\n * @property manifest - The fixed manifest object.\n * @property updated - Whether the manifest was updated.\n * @property warnings - An array of warnings that were encountered during\n * processing of the manifest files. These warnings are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n * @property errors - An array of errors that were encountered during\n * processing of the manifest files. These errors are not logged to the\n * console automatically, so depending on the environment the function is called\n * in, a different method for logging can be used.\n */\nexport type CheckManifestResult = {\n manifest: SnapManifest;\n updated?: boolean;\n warnings: string[];\n errors: string[];\n};\n\n/**\n * Validates a snap.manifest.json file. Attempts to fix the manifest and write\n * the fixed version to disk if `writeManifest` is true. Throws if validation\n * fails.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param writeManifest - Whether to write the fixed manifest to disk.\n * @param sourceCode - The source code of the Snap.\n * @returns Whether the manifest was updated, and an array of warnings that\n * were encountered during processing of the manifest files.\n */\nexport async function checkManifest(\n basePath: string,\n writeManifest = true,\n sourceCode?: string,\n): Promise<CheckManifestResult> {\n const warnings: string[] = [];\n const errors: string[] = [];\n\n let updated = false;\n\n const manifestPath = pathUtils.join(basePath, NpmSnapFileNames.Manifest);\n const manifestFile = await readJsonFile(manifestPath);\n const unvalidatedManifest = manifestFile.result;\n\n const packageFile = await readJsonFile(\n pathUtils.join(basePath, NpmSnapFileNames.PackageJson),\n );\n\n const snapFiles: UnvalidatedSnapFiles = {\n manifest: manifestFile,\n packageJson: packageFile,\n sourceCode: await getSnapSourceCode(\n basePath,\n unvalidatedManifest,\n sourceCode,\n ),\n svgIcon: await getSnapIcon(basePath, unvalidatedManifest),\n };\n\n let manifest: VirtualFile<SnapManifest> | undefined;\n try {\n ({ manifest } = validateNpmSnap(snapFiles));\n } catch (error) {\n if (error instanceof ProgrammaticallyFixableSnapError) {\n errors.push(error.message);\n\n // If we get here, the files at least have the correct shape.\n const partiallyValidatedFiles = snapFiles as SnapFiles;\n\n let isInvalid = true;\n let currentError = error;\n const maxAttempts = Object.keys(SnapValidationFailureReason).length;\n\n // Attempt to fix all fixable validation failure reasons. All such reasons\n // are enumerated by the `SnapValidationFailureReason` enum, so we only\n // attempt to fix the manifest the same amount of times as there are\n // reasons in the enum.\n for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {\n manifest = fixManifest(\n manifest\n ? { ...partiallyValidatedFiles, manifest }\n : partiallyValidatedFiles,\n currentError,\n );\n\n try {\n validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });\n\n isInvalid = false;\n } catch (nextValidationError) {\n currentError = nextValidationError;\n /* istanbul ignore next: this should be impossible */\n if (\n !(\n nextValidationError instanceof ProgrammaticallyFixableSnapError\n ) ||\n (attempts === maxAttempts && !isInvalid)\n ) {\n throw new Error(\n `Internal error: Failed to fix manifest. This is a bug, please report it. Reason:\\n${error.message}`,\n );\n }\n\n errors.push(currentError.message);\n }\n }\n\n updated = true;\n } else {\n throw error;\n }\n }\n\n // TypeScript assumes `manifest` can still be undefined, that is not the case.\n // But we assert to keep TypeScript happy.\n assert(manifest);\n\n const validatedManifest = manifest.result;\n\n // Check presence of recommended keys\n const recommendedFields = ['repository'] as const;\n\n const missingRecommendedFields = recommendedFields.filter(\n (key) => !validatedManifest[key],\n );\n\n if (missingRecommendedFields.length > 0) {\n warnings.push(\n `Missing recommended package.json properties:\\n${missingRecommendedFields.reduce(\n (allMissing, currentField) => {\n return `${allMissing}\\t${currentField}\\n`;\n },\n '',\n )}`,\n );\n }\n\n if (writeManifest) {\n try {\n const newManifest = `${JSON.stringify(\n getWritableManifest(validatedManifest),\n null,\n 2,\n )}\\n`;\n\n if (updated || newManifest !== manifestFile.value) {\n await fs.writeFile(\n pathUtils.join(basePath, NpmSnapFileNames.Manifest),\n newManifest,\n );\n }\n } catch (error) {\n // Note: This error isn't pushed to the errors array, because it's not an\n // error in the manifest itself.\n throw new Error(`Failed to update snap.manifest.json: ${error.message}`);\n }\n }\n\n return { manifest: validatedManifest, updated, warnings, errors };\n}\n\n/**\n * Given the relevant Snap files (manifest, `package.json`, and bundle) and a\n * Snap manifest validation error, fixes the fault in the manifest that caused\n * the error.\n *\n * @param snapFiles - The contents of all Snap files.\n * @param error - The {@link ProgrammaticallyFixableSnapError} that was thrown.\n * @returns A copy of the manifest file where the cause of the error is fixed.\n */\nexport function fixManifest(\n snapFiles: SnapFiles,\n error: ProgrammaticallyFixableSnapError,\n): VirtualFile<SnapManifest> {\n const { manifest, packageJson } = snapFiles;\n const clonedFile = manifest.clone();\n const manifestCopy = clonedFile.result;\n\n switch (error.reason) {\n case SnapValidationFailureReason.NameMismatch:\n manifestCopy.source.location.npm.packageName = packageJson.result.name;\n break;\n\n case SnapValidationFailureReason.VersionMismatch:\n manifestCopy.version = packageJson.result.version;\n break;\n\n case SnapValidationFailureReason.RepositoryMismatch:\n manifestCopy.repository = packageJson.result.repository\n ? deepClone(packageJson.result.repository)\n : undefined;\n break;\n\n case SnapValidationFailureReason.ShasumMismatch:\n manifestCopy.source.shasum = getSnapChecksum(snapFiles);\n break;\n\n /* istanbul ignore next */\n default:\n assertExhaustive(error.reason);\n }\n\n clonedFile.result = manifestCopy;\n clonedFile.value = JSON.stringify(manifestCopy);\n return clonedFile;\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * bundle source file location and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @param sourceCode - Override source code for plugins.\n * @returns The contents of the bundle file, if any.\n */\nexport async function getSnapSourceCode(\n basePath: string,\n manifest: Json,\n sourceCode?: string,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const sourceFilePath = (manifest as Partial<SnapManifest>).source?.location\n ?.npm?.filePath;\n\n if (!sourceFilePath) {\n return undefined;\n }\n\n if (sourceCode) {\n return new VirtualFile({\n path: pathUtils.join(basePath, sourceFilePath),\n value: sourceCode,\n });\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, sourceFilePath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read Snap bundle file: ${error.message}`);\n }\n}\n\n/**\n * Given an unvalidated Snap manifest, attempts to extract the location of the\n * icon and read the file.\n *\n * @param basePath - The path to the folder with the manifest files.\n * @param manifest - The unvalidated Snap manifest file contents.\n * @returns The contents of the icon, if any.\n */\nexport async function getSnapIcon(\n basePath: string,\n manifest: Json,\n): Promise<VirtualFile | undefined> {\n if (!isPlainObject(manifest)) {\n return undefined;\n }\n\n const iconPath = (manifest as Partial<SnapManifest>).source?.location?.npm\n ?.iconPath;\n\n if (!iconPath) {\n return undefined;\n }\n\n try {\n const virtualFile = await readVirtualFile(\n pathUtils.join(basePath, iconPath),\n 'utf8',\n );\n return virtualFile;\n } catch (error) {\n throw new Error(`Failed to read Snap icon file: ${error.message}`);\n }\n}\n\n/**\n * Sorts the given manifest in our preferred sort order and removes the\n * `repository` field if it is falsy (it may be `null`).\n *\n * @param manifest - The manifest to sort and modify.\n * @returns The disk-ready manifest.\n */\nexport function getWritableManifest(manifest: SnapManifest): SnapManifest {\n const { repository, ...remaining } = manifest;\n\n const keys = Object.keys(\n repository ? { ...remaining, repository } : remaining,\n ) as (keyof SnapManifest)[];\n\n const writableManifest = keys\n .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])\n .reduce<Partial<SnapManifest>>(\n (result, key) => ({\n ...result,\n [key]: manifest[key],\n }),\n {},\n );\n\n return writableManifest as SnapManifest;\n}\n\n/**\n * Validates the fields of an npm Snap manifest that has already passed JSON\n * Schema validation.\n *\n * @param snapFiles - The relevant snap files to validate.\n * @param snapFiles.manifest - The npm Snap manifest to validate.\n * @param snapFiles.packageJson - The npm Snap's `package.json`.\n * @param snapFiles.sourceCode - The Snap's source code.\n * @param snapFiles.svgIcon - The Snap's optional icon.\n */\nexport function validateNpmSnapManifest({\n manifest,\n packageJson,\n sourceCode,\n svgIcon,\n}: SnapFiles) {\n const packageJsonName = packageJson.result.name;\n const packageJsonVersion = packageJson.result.version;\n const packageJsonRepository = packageJson.result.repository;\n\n const manifestPackageName = manifest.result.source.location.npm.packageName;\n const manifestPackageVersion = manifest.result.version;\n const manifestRepository = manifest.result.repository;\n\n if (packageJsonName !== manifestPackageName) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package name (\"${manifestPackageName}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"name\" field (\"${packageJsonName}\").`,\n SnapValidationFailureReason.NameMismatch,\n );\n }\n\n if (packageJsonVersion !== manifestPackageVersion) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" npm package version (\"${manifestPackageVersion}\") does not match the \"${NpmSnapFileNames.PackageJson}\" \"version\" field (\"${packageJsonVersion}\").`,\n SnapValidationFailureReason.VersionMismatch,\n );\n }\n\n if (\n // The repository may be `undefined` in package.json but can only be defined\n // or `null` in the Snap manifest due to TS@<4.4 issues.\n (packageJsonRepository || manifestRepository) &&\n !deepEqual(packageJsonRepository, manifestRepository)\n ) {\n throw new ProgrammaticallyFixableSnapError(\n `\"${NpmSnapFileNames.Manifest}\" \"repository\" field does not match the \"${NpmSnapFileNames.PackageJson}\" \"repository\" field.`,\n SnapValidationFailureReason.RepositoryMismatch,\n );\n }\n\n validateSnapShasum(\n { manifest, sourceCode, svgIcon },\n `\"${NpmSnapFileNames.Manifest}\" \"shasum\" field does not match computed shasum.`,\n );\n}\n"]}
@@ -194,6 +194,7 @@ export declare const SnapManifestStruct: Struct<{
194
194
  description: string;
195
195
  proposedName: string;
196
196
  source: {
197
+ shasum: string;
197
198
  location: {
198
199
  npm: {
199
200
  filePath: string;
@@ -202,7 +203,6 @@ export declare const SnapManifestStruct: Struct<{
202
203
  iconPath?: string | undefined;
203
204
  };
204
205
  };
205
- shasum: string;
206
206
  };
207
207
  initialPermissions: {
208
208
  'endowment:long-running'?: {} | undefined;
@@ -268,6 +268,7 @@ export declare const SnapManifestStruct: Struct<{
268
268
  url: Struct<string, null>;
269
269
  }>;
270
270
  source: Struct<{
271
+ shasum: string;
271
272
  location: {
272
273
  npm: {
273
274
  filePath: string;
@@ -276,7 +277,6 @@ export declare const SnapManifestStruct: Struct<{
276
277
  iconPath?: string | undefined;
277
278
  };
278
279
  };
279
- shasum: string;
280
280
  }, {
281
281
  shasum: Struct<string, null>;
282
282
  location: Struct<{
@@ -293,8 +293,8 @@ export declare const SnapManifestStruct: Struct<{
293
293
  registry: "https://registry.npmjs.org" | "https://registry.npmjs.org/";
294
294
  iconPath?: string | undefined;
295
295
  }, {
296
- filePath: Struct<string, unknown>;
297
- iconPath: Struct<string | undefined, unknown>;
296
+ filePath: Struct<string, null>;
297
+ iconPath: Struct<string | undefined, null>;
298
298
  packageName: Struct<string, null>;
299
299
  registry: Struct<"https://registry.npmjs.org" | "https://registry.npmjs.org/", null>;
300
300
  }>;
@@ -7,7 +7,6 @@ const cronjob_1 = require("../cronjob");
7
7
  const entropy_1 = require("../entropy");
8
8
  const json_rpc_1 = require("../json-rpc");
9
9
  const namespace_1 = require("../namespace");
10
- const path_1 = require("../path");
11
10
  const types_1 = require("../types");
12
11
  // BIP-43 purposes that cannot be used for entropy derivation. These are in the
13
12
  // string form, ending with `'`.
@@ -68,8 +67,6 @@ exports.PermissionsStruct = (0, superstruct_1.type)({
68
67
  namespaces: namespace_1.NamespacesStruct,
69
68
  })),
70
69
  });
71
- /* eslint-enable @typescript-eslint/naming-convention */
72
- const relativePath = (struct) => (0, superstruct_1.coerce)(struct, struct, (value) => (0, path_1.normalizeRelative)(value));
73
70
  exports.SnapManifestStruct = (0, superstruct_1.object)({
74
71
  version: utils_1.VersionStruct,
75
72
  description: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, 280),
@@ -82,8 +79,8 @@ exports.SnapManifestStruct = (0, superstruct_1.object)({
82
79
  shasum: utils_1.ChecksumStruct,
83
80
  location: (0, superstruct_1.object)({
84
81
  npm: (0, superstruct_1.object)({
85
- filePath: relativePath((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
86
- iconPath: (0, superstruct_1.optional)(relativePath((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity))),
82
+ filePath: (0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity),
83
+ iconPath: (0, superstruct_1.optional)((0, superstruct_1.size)((0, superstruct_1.string)(), 1, Infinity)),
87
84
  packageName: types_1.NameStruct,
88
85
  registry: (0, superstruct_1.union)([
89
86
  (0, superstruct_1.literal)('https://registry.npmjs.org'),
@@ -1 +1 @@
1
- {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/manifest/validation.ts"],"names":[],"mappings":";;;AAAA,2CAA8E;AAC9E,6CAmBqB;AAErB,wCAA6D;AAC7D,wCAA6E;AAC7E,0CAA+C;AAC/C,4CAAgD;AAChD,kCAA4C;AAC5C,oCAAwD;AAExD,+EAA+E;AAC/E,gCAAgC;AAChC,MAAM,kBAAkB,GAAa;IACnC,2BAAiB;IACjB,sCAA4B;CAC7B,CAAC;AAEF,MAAM,iBAAiB,GAAG,UAAU,CAAC;AACxB,QAAA,eAAe,GAAG,IAAA,oBAAM,EACnC,IAAA,mBAAK,EAAC,IAAA,oBAAM,GAAE,CAAC,EACf,aAAa,EACb,CAAC,IAAI,EAAE,EAAE;IACP,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QACrB,OAAO,uDAAuD,CAAC;KAChE;IAED,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;QACnB,OAAO,2BAA2B,CAAC;KACpC;IAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACnB,OAAO,6CAA6C,CAAC;KACtD;IAED,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE;QAC/D,OAAO,oDAAoD,CAA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AO,IAAA,gBAAE,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,IAAI,wBAAgB,CAAC,QAAQ,cAAc,CAC5C,CAAC;AACJ,CAAC;AARD,oDAQC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,KAAc;IAC/C,qEAAqE;IACrE,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC3C,CAAC;AAHD,gDAGC","sourcesContent":["import { assertStruct, ChecksumStruct, VersionStruct } from '@metamask/utils';\nimport {\n array,\n boolean,\n coerce,\n create,\n enums,\n Infer,\n integer,\n is,\n literal,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n} from 'superstruct';\n\nimport { CronjobSpecificationArrayStruct } from '../cronjob';\nimport { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';\nimport { RpcOriginsStruct } from '../json-rpc';\nimport { NamespacesStruct } from '../namespace';\nimport { normalizeRelative } from '../path';\nimport { NameStruct, NpmSnapFileNames } from '../types';\n\n// BIP-43 purposes that cannot be used for entropy derivation. These are in the\n// string form, ending with `'`.\nconst FORBIDDEN_PURPOSES: string[] = [\n SIP_6_MAGIC_VALUE,\n STATE_ENCRYPTION_MAGIC_VALUE,\n];\n\nconst BIP32_INDEX_REGEX = /^\\d+'?$/u;\nexport const Bip32PathStruct = refine(\n array(string()),\n 'BIP-32 path',\n (path) => {\n if (path.length === 0) {\n return 'Path must be a non-empty BIP-32 derivation path array';\n }\n\n if (path[0] !== 'm') {\n return 'Path must start with \"m\".';\n }\n\n if (path.length < 3) {\n return 'Paths must have a length of at least three.';\n }\n\n if (path.slice(1).some((part) => !BIP32_INDEX_REGEX.test(part))) {\n return 'Path must be a valid BIP-32 derivation path array.';\n }\n\n if (FORBIDDEN_PURPOSES.includes(path[1])) {\n return `The purpose \"${path[1]}\" is not allowed for entropy derivation.`;\n }\n\n return true;\n },\n);\n\nexport const bip32entropy = <T extends { path: string[]; curve: string }, S>(\n struct: Struct<T, S>,\n) =>\n refine(struct, 'BIP-32 entropy', (value) => {\n if (\n value.curve === 'ed25519' &&\n value.path.slice(1).some((part) => !part.endsWith(\"'\"))\n ) {\n return 'Ed25519 does not support unhardened paths.';\n }\n\n return true;\n });\n\n// Used outside @metamask/snap-utils\nexport const Bip32EntropyStruct = bip32entropy(\n type({\n path: Bip32PathStruct,\n curve: enums(['ed25519', 'secp256k1']),\n }),\n);\n\nexport type Bip32Entropy = Infer<typeof Bip32EntropyStruct>;\n\nexport const SnapGetBip32EntropyPermissionsStruct = size(\n array(Bip32EntropyStruct),\n 1,\n Infinity,\n);\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const PermissionsStruct = type({\n 'endowment:long-running': optional(object({})),\n 'endowment:network-access': optional(object({})),\n 'endowment:transaction-insight': optional(\n object({\n allowTransactionOrigin: optional(boolean()),\n }),\n ),\n 'endowment:cronjob': optional(\n object({ jobs: CronjobSpecificationArrayStruct }),\n ),\n 'endowment:rpc': optional(RpcOriginsStruct),\n snap_confirm: optional(object({})),\n snap_manageState: optional(object({})),\n snap_notify: optional(object({})),\n snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip44Entropy: optional(\n size(\n array(object({ coinType: size(integer(), 0, 2 ** 32 - 1) })),\n 1,\n Infinity,\n ),\n ),\n snap_getEntropy: optional(object({})),\n 'endowment:keyring': optional(\n object({\n namespaces: NamespacesStruct,\n }),\n ),\n});\n/* eslint-enable @typescript-eslint/naming-convention */\n\nconst relativePath = <Type extends string>(struct: Struct<Type>) =>\n coerce(struct, struct, (value) => normalizeRelative(value));\n\nexport type SnapPermissions = Infer<typeof PermissionsStruct>;\n\nexport const SnapManifestStruct = object({\n version: VersionStruct,\n description: size(string(), 1, 280),\n proposedName: size(\n pattern(\n string(),\n /^(?:[A-Za-z0-9-_]+( [A-Za-z0-9-_]+)*)|(?:(?:@[A-Za-z0-9-*~][A-Za-z0-9-*._~]*\\/)?[A-Za-z0-9-~][A-Za-z0-9-._~]*)$/u,\n ),\n 1,\n 214,\n ),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n source: object({\n shasum: ChecksumStruct,\n location: object({\n npm: object({\n filePath: relativePath(size(string(), 1, Infinity)),\n iconPath: optional(relativePath(size(string(), 1, Infinity))),\n packageName: NameStruct,\n registry: union([\n literal('https://registry.npmjs.org'),\n literal('https://registry.npmjs.org/'),\n ]),\n }),\n }),\n }),\n initialPermissions: PermissionsStruct,\n manifestVersion: literal('0.1'),\n});\n\nexport type SnapManifest = Infer<typeof SnapManifestStruct>;\n\n/**\n * Check if the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link SnapManifest} object.\n */\nexport function isSnapManifest(value: unknown): value is SnapManifest {\n return is(value, SnapManifestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link SnapManifest} object.\n */\nexport function assertIsSnapManifest(\n value: unknown,\n): asserts value is SnapManifest {\n assertStruct(\n value,\n SnapManifestStruct,\n `\"${NpmSnapFileNames.Manifest}\" is invalid`,\n );\n}\n\n/**\n * Creates a {@link SnapManifest} object from JSON.\n *\n *\n * @param value - The value to check.\n * @throws If the value cannot be coerced to a {@link SnapManifest} object.\n * @returns The created {@link SnapManifest} object.\n */\nexport function createSnapManifest(value: unknown): SnapManifest {\n // TODO: Add a utility to prefix these errors similar to assertStruct\n return create(value, SnapManifestStruct);\n}\n"]}
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/manifest/validation.ts"],"names":[],"mappings":";;;AAA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oBAAM,EAAC;gBACV,QAAQ,EAAE,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC;gBACrC,QAAQ,EAAE,IAAA,sBAAQ,EAAC,IAAA,kBAAI,EAAC,IAAA,oBAAM,GAAE,EAAE,CAAC,EAAE,QAAQ,CAAC,CAAC;gBAC/C,WAAW,EAAE,kBAAU;gBACvB,QAAQ,EAAE,IAAA,mBAAK,EAAC;oBACd,IAAA,qBAAO,EAAC,4BAA4B,CAAC;oBACrC,IAAA,qBAAO,EAAC,6BAA6B,CAAC;iBACvC,CAAC;aACH,CAAC;SACH,CAAC;KACH,CAAC;IACF,kBAAkB,EAAE,yBAAiB;IACrC,eAAe,EAAE,IAAA,qBAAO,EAAC,KAAK,CAAC;CAChC,CAAC,CAAC;AAIH;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,KAAc;IAC3C,OAAO,IAAA,gBAAE,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AACvC,CAAC;AAFD,wCAEC;AAED;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,IAAA,oBAAY,EACV,KAAK,EACL,0BAAkB,EAClB,IAAI,wBAAgB,CAAC,QAAQ,cAAc,CAC5C,CAAC;AACJ,CAAC;AARD,oDAQC;AAED;;;;;;;GAOG;AACH,SAAgB,kBAAkB,CAAC,KAAc;IAC/C,qEAAqE;IACrE,OAAO,IAAA,oBAAM,EAAC,KAAK,EAAE,0BAAkB,CAAC,CAAC;AAC3C,CAAC;AAHD,gDAGC","sourcesContent":["import { assertStruct, ChecksumStruct, VersionStruct } from '@metamask/utils';\nimport {\n array,\n boolean,\n create,\n enums,\n Infer,\n integer,\n is,\n literal,\n object,\n optional,\n pattern,\n refine,\n size,\n string,\n Struct,\n type,\n union,\n} from 'superstruct';\n\nimport { CronjobSpecificationArrayStruct } from '../cronjob';\nimport { SIP_6_MAGIC_VALUE, STATE_ENCRYPTION_MAGIC_VALUE } from '../entropy';\nimport { RpcOriginsStruct } from '../json-rpc';\nimport { NamespacesStruct } from '../namespace';\nimport { NameStruct, NpmSnapFileNames } from '../types';\n\n// BIP-43 purposes that cannot be used for entropy derivation. These are in the\n// string form, ending with `'`.\nconst FORBIDDEN_PURPOSES: string[] = [\n SIP_6_MAGIC_VALUE,\n STATE_ENCRYPTION_MAGIC_VALUE,\n];\n\nconst BIP32_INDEX_REGEX = /^\\d+'?$/u;\nexport const Bip32PathStruct = refine(\n array(string()),\n 'BIP-32 path',\n (path) => {\n if (path.length === 0) {\n return 'Path must be a non-empty BIP-32 derivation path array';\n }\n\n if (path[0] !== 'm') {\n return 'Path must start with \"m\".';\n }\n\n if (path.length < 3) {\n return 'Paths must have a length of at least three.';\n }\n\n if (path.slice(1).some((part) => !BIP32_INDEX_REGEX.test(part))) {\n return 'Path must be a valid BIP-32 derivation path array.';\n }\n\n if (FORBIDDEN_PURPOSES.includes(path[1])) {\n return `The purpose \"${path[1]}\" is not allowed for entropy derivation.`;\n }\n\n return true;\n },\n);\n\nexport const bip32entropy = <T extends { path: string[]; curve: string }, S>(\n struct: Struct<T, S>,\n) =>\n refine(struct, 'BIP-32 entropy', (value) => {\n if (\n value.curve === 'ed25519' &&\n value.path.slice(1).some((part) => !part.endsWith(\"'\"))\n ) {\n return 'Ed25519 does not support unhardened paths.';\n }\n\n return true;\n });\n\n// Used outside @metamask/snap-utils\nexport const Bip32EntropyStruct = bip32entropy(\n type({\n path: Bip32PathStruct,\n curve: enums(['ed25519', 'secp256k1']),\n }),\n);\n\nexport type Bip32Entropy = Infer<typeof Bip32EntropyStruct>;\n\nexport const SnapGetBip32EntropyPermissionsStruct = size(\n array(Bip32EntropyStruct),\n 1,\n Infinity,\n);\n\n/* eslint-disable @typescript-eslint/naming-convention */\nexport const PermissionsStruct = type({\n 'endowment:long-running': optional(object({})),\n 'endowment:network-access': optional(object({})),\n 'endowment:transaction-insight': optional(\n object({\n allowTransactionOrigin: optional(boolean()),\n }),\n ),\n 'endowment:cronjob': optional(\n object({ jobs: CronjobSpecificationArrayStruct }),\n ),\n 'endowment:rpc': optional(RpcOriginsStruct),\n snap_confirm: optional(object({})),\n snap_manageState: optional(object({})),\n snap_notify: optional(object({})),\n snap_getBip32Entropy: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip32PublicKey: optional(SnapGetBip32EntropyPermissionsStruct),\n snap_getBip44Entropy: optional(\n size(\n array(object({ coinType: size(integer(), 0, 2 ** 32 - 1) })),\n 1,\n Infinity,\n ),\n ),\n snap_getEntropy: optional(object({})),\n 'endowment:keyring': optional(\n object({\n namespaces: NamespacesStruct,\n }),\n ),\n});\n/* eslint-enable @typescript-eslint/naming-convention */\n\nexport type SnapPermissions = Infer<typeof PermissionsStruct>;\n\nexport const SnapManifestStruct = object({\n version: VersionStruct,\n description: size(string(), 1, 280),\n proposedName: size(\n pattern(\n string(),\n /^(?:[A-Za-z0-9-_]+( [A-Za-z0-9-_]+)*)|(?:(?:@[A-Za-z0-9-*~][A-Za-z0-9-*._~]*\\/)?[A-Za-z0-9-~][A-Za-z0-9-._~]*)$/u,\n ),\n 1,\n 214,\n ),\n repository: optional(\n object({\n type: size(string(), 1, Infinity),\n url: size(string(), 1, Infinity),\n }),\n ),\n source: object({\n shasum: ChecksumStruct,\n location: object({\n npm: object({\n filePath: size(string(), 1, Infinity),\n iconPath: optional(size(string(), 1, Infinity)),\n packageName: NameStruct,\n registry: union([\n literal('https://registry.npmjs.org'),\n literal('https://registry.npmjs.org/'),\n ]),\n }),\n }),\n }),\n initialPermissions: PermissionsStruct,\n manifestVersion: literal('0.1'),\n});\n\nexport type SnapManifest = Infer<typeof SnapManifestStruct>;\n\n/**\n * Check if the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link SnapManifest} object.\n */\nexport function isSnapManifest(value: unknown): value is SnapManifest {\n return is(value, SnapManifestStruct);\n}\n\n/**\n * Assert that the given value is a valid {@link SnapManifest} object.\n *\n * @param value - The value to check.\n * @throws If the value is not a valid {@link SnapManifest} object.\n */\nexport function assertIsSnapManifest(\n value: unknown,\n): asserts value is SnapManifest {\n assertStruct(\n value,\n SnapManifestStruct,\n `\"${NpmSnapFileNames.Manifest}\" is invalid`,\n );\n}\n\n/**\n * Creates a {@link SnapManifest} object from JSON.\n *\n *\n * @param value - The value to check.\n * @throws If the value cannot be coerced to a {@link SnapManifest} object.\n * @returns The created {@link SnapManifest} object.\n */\nexport function createSnapManifest(value: unknown): SnapManifest {\n // TODO: Add a utility to prefix these errors similar to assertStruct\n return create(value, SnapManifestStruct);\n}\n"]}
@@ -94,8 +94,8 @@ export declare const RequestNamespaceStruct: import("superstruct").Struct<{
94
94
  }>;
95
95
  export declare type RequestNamespace = Infer<typeof RequestNamespaceStruct>;
96
96
  export declare const SessionNamespaceStruct: import("superstruct").Struct<{
97
- accounts: string[];
98
97
  chains: string[];
98
+ accounts: string[];
99
99
  methods?: string[] | undefined;
100
100
  events?: string[] | undefined;
101
101
  }, {
@@ -124,15 +124,15 @@ export declare const NamespacesStruct: import("superstruct").Struct<Record<strin
124
124
  export declare type Namespaces = Infer<typeof NamespacesStruct>;
125
125
  export declare const SessionStruct: import("superstruct").Struct<{
126
126
  namespaces: Record<string, {
127
- accounts: string[];
128
127
  chains: string[];
128
+ accounts: string[];
129
129
  methods?: string[] | undefined;
130
130
  events?: string[] | undefined;
131
131
  }>;
132
132
  }, {
133
133
  namespaces: import("superstruct").Struct<Record<string, {
134
- accounts: string[];
135
134
  chains: string[];
135
+ accounts: string[];
136
136
  methods?: string[] | undefined;
137
137
  events?: string[] | undefined;
138
138
  }>, null>;
package/dist/npm.js CHANGED
@@ -35,18 +35,18 @@ function validateNpmSnap(snapFiles, errorPrefix) {
35
35
  // Typecast: We are assured that the required files exist if we get here.
36
36
  const { manifest, packageJson, sourceCode, svgIcon } = snapFiles;
37
37
  try {
38
- (0, validation_1.assertIsSnapManifest)(manifest);
38
+ (0, validation_1.assertIsSnapManifest)(manifest.result);
39
39
  }
40
40
  catch (error) {
41
41
  throw new Error(`${errorPrefix !== null && errorPrefix !== void 0 ? errorPrefix : ''}${error.message}`);
42
42
  }
43
43
  const validatedManifest = manifest;
44
- const { iconPath } = validatedManifest.source.location.npm;
44
+ const { iconPath } = validatedManifest.result.source.location.npm;
45
45
  if (iconPath && !svgIcon) {
46
46
  throw new Error(`Missing file "${iconPath}".`);
47
47
  }
48
48
  try {
49
- (0, types_1.assertIsNpmSnapPackageJson)(packageJson);
49
+ (0, types_1.assertIsNpmSnapPackageJson)(packageJson.result);
50
50
  }
51
51
  catch (error) {
52
52
  throw new Error(`${errorPrefix !== null && errorPrefix !== void 0 ? errorPrefix : ''}${error.message}`);
@@ -56,9 +56,10 @@ function validateNpmSnap(snapFiles, errorPrefix) {
56
56
  manifest: validatedManifest,
57
57
  packageJson: validatedPackageJson,
58
58
  sourceCode,
59
+ svgIcon,
59
60
  });
60
61
  if (svgIcon) {
61
- if (Buffer.byteLength(svgIcon, 'utf8') > exports.SVG_MAX_BYTE_SIZE) {
62
+ if (Buffer.byteLength(svgIcon.value, 'utf8') > exports.SVG_MAX_BYTE_SIZE) {
62
63
  throw new Error(`${errorPrefix !== null && errorPrefix !== void 0 ? errorPrefix : ''}The specified SVG icon exceeds the maximum size of ${exports.SVG_MAX_BYTE_SIZE_TEXT}.`);
63
64
  }
64
65
  }
package/dist/npm.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"npm.js","sourceRoot":"","sources":["../src/npm.ts"],"names":[],"mappings":";;;AAAA,kDAA8D;AAC9D,sDAA6D;AAC7D,mCAKiB;AAEJ,QAAA,iBAAiB,GAAG,MAAO,CAAC;AAC5B,QAAA,sBAAsB,GAAG,GAAG,IAAI,CAAC,KAAK,CACjD,yBAAiB,GAAG,IAAI,CACzB,IAAI,CAAC;AAEO,QAAA,mBAAmB,GAAG;IACjC,UAAU;IACV,aAAa;IACb,YAAY;CACJ,CAAC;AAEE,QAAA,mBAAmB,GAAG;IACjC,Q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,WAAW,CAAC;IAEzC,IAAA,kCAAuB,EAAC;QACtB,QAAQ,EAAE,iBAAiB;QAC3B,WAAW,EAAE,oBAAoB;QACjC,UAAU;KACX,CAAC,CAAC;IAEH,IAAI,OAAO,EAAE;QACX,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,yBAAiB,EAAE;YAC1D,MAAM,IAAI,KAAK,CACb,GACE,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,EACjB,sDAAsD,8BAAsB,GAAG,CAChF,CAAC;SACH;KACF;IAED,OAAO;QACL,QAAQ,EAAE,iBAAiB;QAC3B,WAAW,EAAE,oBAAoB;QACjC,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC;AAvDD,0CAuDC","sourcesContent":["import { validateNpmSnapManifest } from './manifest/manifest';\nimport { assertIsSnapManifest } from './manifest/validation';\nimport {\n assertIsNpmSnapPackageJson,\n NpmSnapFileNames,\n SnapFiles,\n UnvalidatedSnapFiles,\n} from './types';\n\nexport const SVG_MAX_BYTE_SIZE = 100_000;\nexport const SVG_MAX_BYTE_SIZE_TEXT = `${Math.floor(\n SVG_MAX_BYTE_SIZE / 1000,\n)}kb`;\n\nexport const EXPECTED_SNAP_FILES = [\n 'manifest',\n 'packageJson',\n 'sourceCode',\n] as const;\n\nexport const SnapFileNameFromKey = {\n manifest: NpmSnapFileNames.Manifest,\n packageJson: NpmSnapFileNames.PackageJson,\n sourceCode: 'source code bundle',\n} as const;\n\n/**\n * Validates the files extracted from an npm Snap package tarball by ensuring\n * that they're non-empty and that the Json files match their respective schemas\n * and the Snaps publishing specification.\n *\n * @param snapFiles - The object containing the expected Snap file contents,\n * if any.\n * @param errorPrefix - The prefix of the error message.\n * @returns A tuple of the Snap manifest object and the Snap source code.\n */\nexport function validateNpmSnap(\n snapFiles: UnvalidatedSnapFiles,\n errorPrefix?: `${string}: `,\n): SnapFiles {\n EXPECTED_SNAP_FILES.forEach((key) => {\n if (!snapFiles[key]) {\n throw new Error(\n `${errorPrefix ?? ''}Missing file \"${SnapFileNameFromKey[key]}\".`,\n );\n }\n });\n\n // Typecast: We are assured that the required files exist if we get here.\n const { manifest, packageJson, sourceCode, svgIcon } = snapFiles as SnapFiles;\n try {\n assertIsSnapManifest(manifest);\n } catch (error) {\n throw new Error(`${errorPrefix ?? ''}${error.message}`);\n }\n const validatedManifest = manifest;\n\n const { iconPath } = validatedManifest.source.location.npm;\n if (iconPath && !svgIcon) {\n throw new Error(`Missing file \"${iconPath}\".`);\n }\n\n try {\n assertIsNpmSnapPackageJson(packageJson);\n } catch (error) {\n throw new Error(`${errorPrefix ?? ''}${error.message}`);\n }\n const validatedPackageJson = packageJson;\n\n validateNpmSnapManifest({\n manifest: validatedManifest,\n packageJson: validatedPackageJson,\n sourceCode,\n });\n\n if (svgIcon) {\n if (Buffer.byteLength(svgIcon, 'utf8') > SVG_MAX_BYTE_SIZE) {\n throw new Error(\n `${\n errorPrefix ?? ''\n }The specified SVG icon exceeds the maximum size of ${SVG_MAX_BYTE_SIZE_TEXT}.`,\n );\n }\n }\n\n return {\n manifest: validatedManifest,\n packageJson: validatedPackageJson,\n sourceCode,\n svgIcon,\n };\n}\n"]}
1
+ {"version":3,"file":"npm.js","sourceRoot":"","sources":["../src/npm.ts"],"names":[],"mappings":";;;AAAA,kDAA8D;AAC9D,sDAA6D;AAC7D,mCAKiB;AAEJ,QAAA,iBAAiB,GAAG,MAAO,CAAC;AAC5B,QAAA,sBAAsB,GAAG,GAAG,IAAI,CAAC,KAAK,CACjD,yBAAiB,GAAG,IAAI,CACzB,IAAI,CAAC;AAEO,QAAA,mBAAmB,GAAG;IACjC,UAAU;IACV,aAAa;IACb,YAAY;CACJ,CAAC;AAEE,QAAA,mBAAmB,GAAG;IACjC,QAAQ,EAAE,wBAAgB,CAAC,QAAQ;IACnC,WAAW,EAAE,wBAAgB,CAAC,WAAW;IACzC,UAAU,EAAE,oBAAoB;CACxB,CAAC;AAEX;;;;;;;;;GASG;AACH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C,UAAU;QACV,OAAO;KACR,CAAC,CAAC;IAEH,IAAI,OAAO,EAAE;QACX,IAAI,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,yBAAiB,EAAE;YAChE,MAAM,IAAI,KAAK,CACb,GACE,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,EACjB,sDAAsD,8BAAsB,GAAG,CAChF,CAAC;SACH;KACF;IAED,OAAO;QACL,QAAQ,EAAE,iBAAiB;QAC3B,WAAW,EAAE,oBAAoB;QACjC,UAAU;QACV,OAAO;KACR,CAAC;AACJ,CAAC;AAxDD,0CAwDC","sourcesContent":["import { validateNpmSnapManifest } from './manifest/manifest';\nimport { assertIsSnapManifest } from './manifest/validation';\nimport {\n assertIsNpmSnapPackageJson,\n NpmSnapFileNames,\n SnapFiles,\n UnvalidatedSnapFiles,\n} from './types';\n\nexport const SVG_MAX_BYTE_SIZE = 100_000;\nexport const SVG_MAX_BYTE_SIZE_TEXT = `${Math.floor(\n SVG_MAX_BYTE_SIZE / 1000,\n)}kb`;\n\nexport const EXPECTED_SNAP_FILES = [\n 'manifest',\n 'packageJson',\n 'sourceCode',\n] as const;\n\nexport const SnapFileNameFromKey = {\n manifest: NpmSnapFileNames.Manifest,\n packageJson: NpmSnapFileNames.PackageJson,\n sourceCode: 'source code bundle',\n} as const;\n\n/**\n * Validates the files extracted from an npm Snap package tarball by ensuring\n * that they're non-empty and that the Json files match their respective schemas\n * and the Snaps publishing specification.\n *\n * @param snapFiles - The object containing the expected Snap file contents,\n * if any.\n * @param errorPrefix - The prefix of the error message.\n * @returns A tuple of the Snap manifest object and the Snap source code.\n */\nexport function validateNpmSnap(\n snapFiles: UnvalidatedSnapFiles,\n errorPrefix?: `${string}: `,\n): SnapFiles {\n EXPECTED_SNAP_FILES.forEach((key) => {\n if (!snapFiles[key]) {\n throw new Error(\n `${errorPrefix ?? ''}Missing file \"${SnapFileNameFromKey[key]}\".`,\n );\n }\n });\n\n // Typecast: We are assured that the required files exist if we get here.\n const { manifest, packageJson, sourceCode, svgIcon } = snapFiles as SnapFiles;\n try {\n assertIsSnapManifest(manifest.result);\n } catch (error) {\n throw new Error(`${errorPrefix ?? ''}${error.message}`);\n }\n const validatedManifest = manifest;\n\n const { iconPath } = validatedManifest.result.source.location.npm;\n if (iconPath && !svgIcon) {\n throw new Error(`Missing file \"${iconPath}\".`);\n }\n\n try {\n assertIsNpmSnapPackageJson(packageJson.result);\n } catch (error) {\n throw new Error(`${errorPrefix ?? ''}${error.message}`);\n }\n const validatedPackageJson = packageJson;\n\n validateNpmSnapManifest({\n manifest: validatedManifest,\n packageJson: validatedPackageJson,\n sourceCode,\n svgIcon,\n });\n\n if (svgIcon) {\n if (Buffer.byteLength(svgIcon.value, 'utf8') > SVG_MAX_BYTE_SIZE) {\n throw new Error(\n `${\n errorPrefix ?? ''\n }The specified SVG icon exceeds the maximum size of ${SVG_MAX_BYTE_SIZE_TEXT}.`,\n );\n }\n }\n\n return {\n manifest: validatedManifest,\n packageJson: validatedPackageJson,\n sourceCode,\n svgIcon,\n };\n}\n"]}
@@ -55,7 +55,8 @@ function breakTokens(value) {
55
55
  * elements and string literal expressions.
56
56
  */
57
57
  function breakTokensTemplateLiteral(value) {
58
- // @ts-expect-error `matchAll` is not available in ES2017, but this code
58
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
59
+ // @ts-ignore `matchAll` is not available in ES2017, but this code
59
60
  // should only be used in environments where the function is supported.
60
61
  const matches = Array.from(value.matchAll(TOKEN_REGEX));
61
62
  if (matches.length > 0) {