@metamask/snaps-utils 0.30.0 → 0.32.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 (50) hide show
  1. package/dist/cronjob.d.ts +3 -3
  2. package/dist/cronjob.js +4 -7
  3. package/dist/cronjob.js.map +1 -1
  4. package/dist/eval-worker.js +24 -7
  5. package/dist/eval-worker.js.map +1 -1
  6. package/dist/eval.js +1 -2
  7. package/dist/eval.js.map +1 -1
  8. package/dist/fs.d.ts +9 -0
  9. package/dist/fs.js +24 -1
  10. package/dist/fs.js.map +1 -1
  11. package/dist/handlers.d.ts +2 -5
  12. package/dist/handlers.js.map +1 -1
  13. package/dist/iframe.js +1 -0
  14. package/dist/iframe.js.map +1 -1
  15. package/dist/iframe.test.browser.d.ts +1 -0
  16. package/dist/iframe.test.browser.js +15 -0
  17. package/dist/iframe.test.browser.js.map +1 -0
  18. package/dist/index.browser.d.ts +0 -2
  19. package/dist/index.browser.js +0 -2
  20. package/dist/index.browser.js.map +1 -1
  21. package/dist/index.d.ts +0 -2
  22. package/dist/index.js +0 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/manifest/manifest.js +13 -20
  25. package/dist/manifest/manifest.js.map +1 -1
  26. package/dist/manifest/validation.d.ts +17 -12
  27. package/dist/manifest/validation.js +2 -0
  28. package/dist/manifest/validation.js.map +1 -1
  29. package/dist/mock.js +3 -3
  30. package/dist/mock.js.map +1 -1
  31. package/dist/namespace.js +2 -2
  32. package/dist/namespace.js.map +1 -1
  33. package/dist/npm.js +4 -4
  34. package/dist/npm.js.map +1 -1
  35. package/dist/post-process.js +5 -7
  36. package/dist/post-process.js.map +1 -1
  37. package/dist/snaps.d.ts +10 -6
  38. package/dist/snaps.js +16 -22
  39. package/dist/snaps.js.map +1 -1
  40. package/dist/types.js +1 -1
  41. package/dist/types.js.map +1 -1
  42. package/dist/virtual-file/VirtualFile.js +4 -5
  43. package/dist/virtual-file/VirtualFile.js.map +1 -1
  44. package/package.json +33 -11
  45. package/dist/flatMap.d.ts +0 -22
  46. package/dist/flatMap.js +0 -38
  47. package/dist/flatMap.js.map +0 -1
  48. package/dist/object.d.ts +0 -8
  49. package/dist/object.js +0 -15
  50. package/dist/object.js.map +0 -1
package/dist/cronjob.d.ts CHANGED
@@ -21,13 +21,13 @@ export declare type CronExpression = Infer<typeof CronExpressionStruct>;
21
21
  */
22
22
  export declare function parseCronExpression(expression: string | object): import("cron-parser").CronExpression<false>;
23
23
  export declare const CronjobSpecificationStruct: import("superstruct").Struct<{
24
- expression: string;
25
24
  request: {
26
25
  method: string;
27
26
  params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
28
27
  id?: string | number | null | undefined;
29
28
  jsonrpc?: "2.0" | undefined;
30
29
  };
30
+ expression: string;
31
31
  }, {
32
32
  expression: import("superstruct").Struct<string, null>;
33
33
  request: import("superstruct").Struct<{
@@ -51,21 +51,21 @@ export declare type CronjobSpecification = Infer<typeof CronjobSpecificationStru
51
51
  */
52
52
  export declare function isCronjobSpecification(value: unknown): boolean;
53
53
  export declare const CronjobSpecificationArrayStruct: import("superstruct").Struct<{
54
- expression: string;
55
54
  request: {
56
55
  method: string;
57
56
  params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
58
57
  id?: string | number | null | undefined;
59
58
  jsonrpc?: "2.0" | undefined;
60
59
  };
61
- }[], import("superstruct").Struct<{
62
60
  expression: string;
61
+ }[], import("superstruct").Struct<{
63
62
  request: {
64
63
  method: string;
65
64
  params?: Record<string, import("@metamask/utils").Json> | import("@metamask/utils").Json[] | undefined;
66
65
  id?: string | number | null | undefined;
67
66
  jsonrpc?: "2.0" | undefined;
68
67
  };
68
+ expression: string;
69
69
  }, {
70
70
  expression: import("superstruct").Struct<string, null>;
71
71
  request: import("superstruct").Struct<{
package/dist/cronjob.js CHANGED
@@ -11,15 +11,12 @@ exports.CronExpressionStruct = (0, superstruct_1.refine)((0, superstruct_1.coerc
11
11
  dayOfMonth: (0, superstruct_1.optional)((0, superstruct_1.string)()),
12
12
  month: (0, superstruct_1.optional)((0, superstruct_1.string)()),
13
13
  dayOfWeek: (0, superstruct_1.optional)((0, superstruct_1.string)()),
14
- }), (value) => {
15
- var _a, _b, _c, _d, _e;
16
- return `${(_a = value.minute) !== null && _a !== void 0 ? _a : '*'} ${(_b = value.hour) !== null && _b !== void 0 ? _b : '*'} ${(_c = value.dayOfMonth) !== null && _c !== void 0 ? _c : '*'} ${(_d = value.month) !== null && _d !== void 0 ? _d : '*'} ${(_e = value.dayOfWeek) !== null && _e !== void 0 ? _e : '*'}`;
17
- }), 'CronExpression', (value) => {
14
+ }), (value) => `${value.minute ?? '*'} ${value.hour ?? '*'} ${value.dayOfMonth ?? '*'} ${value.month ?? '*'} ${value.dayOfWeek ?? '*'}`), 'CronExpression', (value) => {
18
15
  try {
19
16
  (0, cron_parser_1.parseExpression)(value);
20
17
  return true;
21
18
  }
22
- catch (_a) {
19
+ catch {
23
20
  return false;
24
21
  }
25
22
  });
@@ -49,7 +46,7 @@ function isCronjobSpecification(value) {
49
46
  (0, superstruct_1.create)(value, exports.CronjobSpecificationStruct);
50
47
  return true;
51
48
  }
52
- catch (_a) {
49
+ catch {
53
50
  return false;
54
51
  }
55
52
  }
@@ -66,7 +63,7 @@ function isCronjobSpecificationArray(value) {
66
63
  (0, superstruct_1.create)(value, exports.CronjobSpecificationArrayStruct);
67
64
  return true;
68
65
  }
69
- catch (_a) {
66
+ catch {
70
67
  return false;
71
68
  }
72
69
  }
@@ -1 +1 @@
1
- {"version":3,"file":"cronjob.js","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":";;;AAAA,2CAAuD;AACvD,6CAA8C;AAC9C,6CAaqB;AAER,QAAA,uBAAuB,GAAG,IAAA,oBAAM,EAC3C,IAAA,qBAAO,EAAC,IAAA,kBAAI,EAAC,4BAAoB,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EACtD,IA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BAAM,EAAC;IAC/C,UAAU,EAAE,4BAAoB;IAChC,OAAO,EAAE,+BAAuB;CACjC,CAAC,CAAC;AAGH;;;;;GAKG;AACH,SAAgB,sBAAsB,CAAC,KAAc;IACnD,IAAI;QACF,IAAA,oBAAM,EAAC,KAAK,EAAE,kCAA0B,CAAC,CAAC;QAC1C,OAAO,IAAI,CAAC;KACb;IAAC,WAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAPD,wDAOC;AAEY,QAAA,+BAA+B,GAAG,IAAA,mBAAK,EAClD,kCAA0B,CAC3B,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,2BAA2B,CAAC,KAAc;IACxD,IAAI;QACF,IAAA,oBAAM,EAAC,KAAK,EAAE,uCAA+B,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC;KACb;IAAC,WAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAPD,kEAOC","sourcesContent":["import { JsonRpcRequestStruct } from '@metamask/utils';\nimport { parseExpression } from 'cron-parser';\nimport {\n array,\n assign,\n coerce,\n create,\n Infer,\n object,\n omit,\n optional,\n partial,\n pick,\n refine,\n string,\n} from 'superstruct';\n\nexport const CronjobRpcRequestStruct = assign(\n partial(pick(JsonRpcRequestStruct, ['id', 'jsonrpc'])),\n omit(JsonRpcRequestStruct, ['id', 'jsonrpc']),\n);\nexport type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;\n\nexport const CronExpressionStruct = refine(\n coerce(\n string(),\n object({\n minute: optional(string()),\n hour: optional(string()),\n dayOfMonth: optional(string()),\n month: optional(string()),\n dayOfWeek: optional(string()),\n }),\n (value) =>\n `${value.minute ?? '*'} ${value.hour ?? '*'} ${value.dayOfMonth ?? '*'} ${\n value.month ?? '*'\n } ${value.dayOfWeek ?? '*'}`,\n ),\n 'CronExpression',\n (value) => {\n try {\n parseExpression(value);\n return true;\n } catch {\n return false;\n }\n },\n);\n\nexport type CronExpression = Infer<typeof CronExpressionStruct>;\n\n/**\n * Parses a cron expression.\n *\n * @param expression - Expression to parse.\n * @returns A CronExpression class instance.\n */\nexport function parseCronExpression(expression: string | object) {\n const ensureStringExpression = create(expression, CronExpressionStruct);\n return parseExpression(ensureStringExpression);\n}\n\nexport const CronjobSpecificationStruct = object({\n expression: CronExpressionStruct,\n request: CronjobRpcRequestStruct,\n});\nexport type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;\n\n/**\n * Check if the given value is a {@link CronjobSpecification} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link CronjobSpecification} object.\n */\nexport function isCronjobSpecification(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationStruct);\n return true;\n } catch {\n return false;\n }\n}\n\nexport const CronjobSpecificationArrayStruct = array(\n CronjobSpecificationStruct,\n);\n\n/**\n * Check if the given value is an array of {@link CronjobSpecification} objects.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid array of {@link CronjobSpecification} objects.\n */\nexport function isCronjobSpecificationArray(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationArrayStruct);\n return true;\n } catch {\n return false;\n }\n}\n"]}
1
+ {"version":3,"file":"cronjob.js","sourceRoot":"","sources":["../src/cronjob.ts"],"names":[],"mappings":";;;AAAA,2CAAuD;AACvD,6CAA8C;AAC9C,6CAaqB;AAER,QAAA,uBAAuB,GAAG,IAAA,oBAAM,EAC3C,IAAA,qBAAO,EAAC,IAAA,kBAAI,EAAC,4BAAoB,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,EACtD,IAAA,kBAAI,EAAC,4BAAoB,EAAE,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAC9C,CAAC;AAGW,QAAA,oBAAoB,GAAG,IAAA,oBAAM,EACxC,IAAA,oBAAM,EACJ,IAAA,oBAAM,GAAE,EACR,IAAA,oBAAM,EAAC;IACL,MAAM,EAAE,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;IAC1B,IAAI,EAAE,IAAA,sBA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EAClD,kCAA0B,CAC3B,CAAC;AAEF;;;;;GAKG;AACH,SAAgB,2BAA2B,CAAC,KAAc;IACxD,IAAI;QACF,IAAA,oBAAM,EAAC,KAAK,EAAE,uCAA+B,CAAC,CAAC;QAC/C,OAAO,IAAI,CAAC;KACb;IAAC,MAAM;QACN,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAPD,kEAOC","sourcesContent":["import { JsonRpcRequestStruct } from '@metamask/utils';\nimport { parseExpression } from 'cron-parser';\nimport {\n array,\n assign,\n coerce,\n create,\n Infer,\n object,\n omit,\n optional,\n partial,\n pick,\n refine,\n string,\n} from 'superstruct';\n\nexport const CronjobRpcRequestStruct = assign(\n partial(pick(JsonRpcRequestStruct, ['id', 'jsonrpc'])),\n omit(JsonRpcRequestStruct, ['id', 'jsonrpc']),\n);\nexport type CronjobRpcRequest = Infer<typeof CronjobRpcRequestStruct>;\n\nexport const CronExpressionStruct = refine(\n coerce(\n string(),\n object({\n minute: optional(string()),\n hour: optional(string()),\n dayOfMonth: optional(string()),\n month: optional(string()),\n dayOfWeek: optional(string()),\n }),\n (value) =>\n `${value.minute ?? '*'} ${value.hour ?? '*'} ${value.dayOfMonth ?? '*'} ${\n value.month ?? '*'\n } ${value.dayOfWeek ?? '*'}`,\n ),\n 'CronExpression',\n (value) => {\n try {\n parseExpression(value);\n return true;\n } catch {\n return false;\n }\n },\n);\n\nexport type CronExpression = Infer<typeof CronExpressionStruct>;\n\n/**\n * Parses a cron expression.\n *\n * @param expression - Expression to parse.\n * @returns A CronExpression class instance.\n */\nexport function parseCronExpression(expression: string | object) {\n const ensureStringExpression = create(expression, CronExpressionStruct);\n return parseExpression(ensureStringExpression);\n}\n\nexport const CronjobSpecificationStruct = object({\n expression: CronExpressionStruct,\n request: CronjobRpcRequestStruct,\n});\nexport type CronjobSpecification = Infer<typeof CronjobSpecificationStruct>;\n\n/**\n * Check if the given value is a {@link CronjobSpecification} object.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid {@link CronjobSpecification} object.\n */\nexport function isCronjobSpecification(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationStruct);\n return true;\n } catch {\n return false;\n }\n}\n\nexport const CronjobSpecificationArrayStruct = array(\n CronjobSpecificationStruct,\n);\n\n/**\n * Check if the given value is an array of {@link CronjobSpecification} objects.\n *\n * @param value - The value to check.\n * @returns Whether the value is a valid array of {@link CronjobSpecification} objects.\n */\nexport function isCronjobSpecificationArray(value: unknown): boolean {\n try {\n create(value, CronjobSpecificationArrayStruct);\n return true;\n } catch {\n return false;\n }\n}\n"]}
@@ -5,11 +5,20 @@ require("ses/lockdown");
5
5
  const fs_1 = require("fs");
6
6
  const mock_1 = require("./mock");
7
7
  const types_1 = require("./types");
8
- lockdown(Object.assign({ consoleTaming: 'unsafe', errorTaming: 'unsafe', mathTaming: 'unsafe', dateTaming: 'unsafe', overrideTaming: 'severe' }, (process.env.NODE_ENV === 'test'
9
- ? {
10
- domainTaming: 'unsafe',
11
- }
12
- : {})));
8
+ lockdown({
9
+ consoleTaming: 'unsafe',
10
+ errorTaming: 'unsafe',
11
+ mathTaming: 'unsafe',
12
+ dateTaming: 'unsafe',
13
+ overrideTaming: 'severe',
14
+ // TODO: See if there's an easier way to do this. This file is ran in a
15
+ // separate process, so we can't mock SES with Jest.
16
+ ...(process.env.NODE_ENV === 'test'
17
+ ? {
18
+ domainTaming: 'unsafe',
19
+ }
20
+ : {}),
21
+ });
13
22
  /**
14
23
  * Get mock endowments that don't do anything. This is useful for running the
15
24
  * eval, for snaps that try to communicate with the extension on initialisation,
@@ -19,11 +28,19 @@ lockdown(Object.assign({ consoleTaming: 'unsafe', errorTaming: 'unsafe', mathTam
19
28
  */
20
29
  function getMockEndowments() {
21
30
  const endowments = (0, mock_1.generateMockEndowments)();
22
- return Object.assign(Object.assign({}, endowments), { window: endowments, self: endowments });
31
+ return {
32
+ ...endowments,
33
+ window: endowments,
34
+ self: endowments,
35
+ };
23
36
  }
24
37
  const snapFilePath = process.argv[2];
25
38
  const snapModule = { exports: {} };
26
- new Compartment(Object.assign(Object.assign({}, getMockEndowments()), { module: snapModule, exports: snapModule.exports })).evaluate((0, fs_1.readFileSync)(snapFilePath, 'utf8'));
39
+ new Compartment({
40
+ ...getMockEndowments(),
41
+ module: snapModule,
42
+ exports: snapModule.exports,
43
+ }).evaluate((0, fs_1.readFileSync)(snapFilePath, 'utf8'));
27
44
  const invalidExports = Object.keys(snapModule.exports).filter((snapExport) => !types_1.SNAP_EXPORT_NAMES.includes(snapExport));
28
45
  if (invalidExports.length > 0) {
29
46
  // eslint-disable-next-line no-console
@@ -1 +1 @@
1
- {"version":3,"file":"eval-worker.js","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":";;AAAA,uDAAuD;AACvD,wBAAsB;AAEtB,2BAAkC;AAElC,iCAAgD;AAChD,mCAAyD;AAIzD,QAAQ,iBACN,aAAa,EAAE,QAAQ,EACvB,WAAW,EAAE,QAAQ,EACrB,UAAU,EAAE,QAAQ,EACpB,UAAU,EAAE,QAAQ,EACpB,cAAc,EAAE,QAAQ,IAIrB,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM;IACjC,CAAC,CAAC;QACE,YAAY,EAAE,QAAQ;KACvB;IACH,CAAC,CAAC,EAAE,CAAC,EACP,CAAC;AAEH;;;;;;GAMG;AACH,SAAS,iBAAiB;IACxB,MAAM,UAAU,GAAG,IAAA,6BAAsB,GAAE,CAAC;IAC5C,uCACK,UAAU,KACb,MAAM,EAAE,UAAU,EAClB,IAAI,EAAE,UAAU,IAChB;AACJ,CAAC;AAED,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAErC,MAAM,UAAU,GAAsB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEtD,IAAI,WAAW,iCACV,iBAAiB,EAAE,KACtB,MAAM,EAAE,UAAU,EAClB,OAAO,EAAE,UAAU,CAAC,OAAO,IAC3B,CAAC,QAAQ,CAAC,IAAA,iBAAY,EAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;AAEhD,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAC3D,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,yBAAiB,CAAC,QAAQ,CAAC,UAAyB,CAAC,CACvE,CAAC;AAEF,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;IAC7B,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,mCAAmC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;CAC9E;AAED,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,8BAA8B","sourcesContent":["// eslint-disable-next-line import/no-unassigned-import\nimport 'ses/lockdown';\n\nimport { readFileSync } from 'fs';\n\nimport { generateMockEndowments } from './mock';\nimport { HandlerType, SNAP_EXPORT_NAMES } from './types';\n\ndeclare let lockdown: any, Compartment: any;\n\nlockdown({\n consoleTaming: 'unsafe',\n errorTaming: 'unsafe',\n mathTaming: 'unsafe',\n dateTaming: 'unsafe',\n overrideTaming: 'severe',\n\n // TODO: See if there's an easier way to do this. This file is ran in a\n // separate process, so we can't mock SES with Jest.\n ...(process.env.NODE_ENV === 'test'\n ? {\n domainTaming: 'unsafe',\n }\n : {}),\n});\n\n/**\n * Get mock endowments that don't do anything. This is useful for running the\n * eval, for snaps that try to communicate with the extension on initialisation,\n * for example.\n *\n * @returns The mock endowments.\n */\nfunction getMockEndowments() {\n const endowments = generateMockEndowments();\n return {\n ...endowments,\n window: endowments,\n self: endowments,\n };\n}\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nnew Compartment({\n ...getMockEndowments(),\n module: snapModule,\n exports: snapModule.exports,\n}).evaluate(readFileSync(snapFilePath, 'utf8'));\n\nconst invalidExports = Object.keys(snapModule.exports).filter(\n (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n);\n\nif (invalidExports.length > 0) {\n // eslint-disable-next-line no-console\n console.warn(`Invalid snap exports detected:\\n${invalidExports.join('\\n')}`);\n}\n\nsetTimeout(() => process.exit(0), 1000); // Hack to ensure worker exits\n"]}
1
+ {"version":3,"file":"eval-worker.js","sourceRoot":"","sources":["../src/eval-worker.ts"],"names":[],"mappings":";;AAAA,uDAAuD;AACvD,wBAAsB;AAEtB,2BAAkC;AAElC,iCAAgD;AAChD,mCAAyD;AAIzD,QAAQ,CAAC;IACP,aAAa,EAAE,QAAQ;IACvB,WAAW,EAAE,QAAQ;IACrB,UAAU,EAAE,QAAQ;IACpB,UAAU,EAAE,QAAQ;IACpB,cAAc,EAAE,QAAQ;IAExB,uEAAuE;IACvE,oDAAoD;IACpD,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM;QACjC,CAAC,CAAC;YACE,YAAY,EAAE,QAAQ;SACvB;QACH,CAAC,CAAC,EAAE,CAAC;CACR,CAAC,CAAC;AAEH;;;;;;GAMG;AACH,SAAS,iBAAiB;IACxB,MAAM,UAAU,GAAG,IAAA,6BAAsB,GAAE,CAAC;IAC5C,OAAO;QACL,GAAG,UAAU;QACb,MAAM,EAAE,UAAU;QAClB,IAAI,EAAE,UAAU;KACjB,CAAC;AACJ,CAAC;AAED,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAErC,MAAM,UAAU,GAAsB,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC;AAEtD,IAAI,WAAW,CAAC;IACd,GAAG,iBAAiB,EAAE;IACtB,MAAM,EAAE,UAAU;IAClB,OAAO,EAAE,UAAU,CAAC,OAAO;CAC5B,CAAC,CAAC,QAAQ,CAAC,IAAA,iBAAY,EAAC,YAAY,EAAE,MAAM,CAAC,CAAC,CAAC;AAEhD,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAC3D,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC,yBAAiB,CAAC,QAAQ,CAAC,UAAyB,CAAC,CACvE,CAAC;AAEF,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;IAC7B,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,mCAAmC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;CAC9E;AAED,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,8BAA8B","sourcesContent":["// eslint-disable-next-line import/no-unassigned-import\nimport 'ses/lockdown';\n\nimport { readFileSync } from 'fs';\n\nimport { generateMockEndowments } from './mock';\nimport { HandlerType, SNAP_EXPORT_NAMES } from './types';\n\ndeclare let lockdown: any, Compartment: any;\n\nlockdown({\n consoleTaming: 'unsafe',\n errorTaming: 'unsafe',\n mathTaming: 'unsafe',\n dateTaming: 'unsafe',\n overrideTaming: 'severe',\n\n // TODO: See if there's an easier way to do this. This file is ran in a\n // separate process, so we can't mock SES with Jest.\n ...(process.env.NODE_ENV === 'test'\n ? {\n domainTaming: 'unsafe',\n }\n : {}),\n});\n\n/**\n * Get mock endowments that don't do anything. This is useful for running the\n * eval, for snaps that try to communicate with the extension on initialisation,\n * for example.\n *\n * @returns The mock endowments.\n */\nfunction getMockEndowments() {\n const endowments = generateMockEndowments();\n return {\n ...endowments,\n window: endowments,\n self: endowments,\n };\n}\n\nconst snapFilePath = process.argv[2];\n\nconst snapModule: { exports?: any } = { exports: {} };\n\nnew Compartment({\n ...getMockEndowments(),\n module: snapModule,\n exports: snapModule.exports,\n}).evaluate(readFileSync(snapFilePath, 'utf8'));\n\nconst invalidExports = Object.keys(snapModule.exports).filter(\n (snapExport) => !SNAP_EXPORT_NAMES.includes(snapExport as HandlerType),\n);\n\nif (invalidExports.length > 0) {\n // eslint-disable-next-line no-console\n console.warn(`Invalid snap exports detected:\\n${invalidExports.join('\\n')}`);\n}\n\nsetTimeout(() => process.exit(0), 1000); // Hack to ensure worker exits\n"]}
package/dist/eval.js CHANGED
@@ -4,7 +4,6 @@ exports.evalBundle = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  const path_1 = require("path");
6
6
  const fs_1 = require("./fs");
7
- const WORKER_PATH = (0, path_1.join)(__dirname, 'eval-worker.js');
8
7
  /**
9
8
  * Spawn a new process to run the provided bundle in.
10
9
  *
@@ -15,7 +14,7 @@ const WORKER_PATH = (0, path_1.join)(__dirname, 'eval-worker.js');
15
14
  async function evalBundle(bundlePath) {
16
15
  await (0, fs_1.validateFilePath)(bundlePath);
17
16
  return new Promise((resolve, reject) => {
18
- const worker = (0, child_process_1.fork)(WORKER_PATH, [bundlePath]);
17
+ const worker = (0, child_process_1.fork)((0, path_1.join)(__dirname, 'eval-worker.js'), [bundlePath]);
19
18
  worker.on('exit', (exitCode) => {
20
19
  if (exitCode === 0) {
21
20
  return resolve(null);
package/dist/eval.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"eval.js","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":";;;AAAA,iDAAqC;AACrC,+BAA4B;AAE5B,6BAAwC;AAExC,MAAM,WAAW,GAAG,IAAA,WAAI,EAAC,SAAS,EAAE,gBAAgB,CAAC,CAAC;AAEtD;;;;;;GAMG;AACI,KAAK,UAAU,UAAU,CAAC,UAAkB;IACjD,MAAM,IAAA,qBAAgB,EAAC,UAAU,CAAC,CAAC;IAEnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,MAAM,GAAG,IAAA,oBAAI,EAAC,WAAW,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;QAE/C,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE;YACrC,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;aACtB;YAED,OAAO,MAAM,CACX,IAAI,KAAK,CAAC,2CAA2C,QAAQ,EAAE,CAAC,CACjE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAhBD,gCAgBC","sourcesContent":["import { fork } from 'child_process';\nimport { join } from 'path';\n\nimport { validateFilePath } from './fs';\n\nconst WORKER_PATH = join(__dirname, 'eval-worker.js');\n\n/**\n * Spawn a new process to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport async function evalBundle(bundlePath: string): Promise<null> {\n await validateFilePath(bundlePath);\n\n return new Promise((resolve, reject) => {\n const worker = fork(WORKER_PATH, [bundlePath]);\n\n worker.on('exit', (exitCode: number) => {\n if (exitCode === 0) {\n return resolve(null);\n }\n\n return reject(\n new Error(`Process exited with non-zero exit code: ${exitCode}`),\n );\n });\n });\n}\n"]}
1
+ {"version":3,"file":"eval.js","sourceRoot":"","sources":["../src/eval.ts"],"names":[],"mappings":";;;AAAA,iDAAqC;AACrC,+BAA4B;AAE5B,6BAAwC;AAExC;;;;;;GAMG;AACI,KAAK,UAAU,UAAU,CAAC,UAAkB;IACjD,MAAM,IAAA,qBAAgB,EAAC,UAAU,CAAC,CAAC;IAEnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,MAAM,MAAM,GAAG,IAAA,oBAAI,EAAC,IAAA,WAAI,EAAC,SAAS,EAAE,gBAAgB,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;QAErE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,QAAgB,EAAE,EAAE;YACrC,IAAI,QAAQ,KAAK,CAAC,EAAE;gBAClB,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;aACtB;YAED,OAAO,MAAM,CACX,IAAI,KAAK,CAAC,2CAA2C,QAAQ,EAAE,CAAC,CACjE,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAhBD,gCAgBC","sourcesContent":["import { fork } from 'child_process';\nimport { join } from 'path';\n\nimport { validateFilePath } from './fs';\n\n/**\n * Spawn a new process to run the provided bundle in.\n *\n * @param bundlePath - The path to the bundle to run.\n * @returns `null` if the worker ran successfully.\n * @throws If the worker failed to run successfully.\n */\nexport async function evalBundle(bundlePath: string): Promise<null> {\n await validateFilePath(bundlePath);\n\n return new Promise((resolve, reject) => {\n const worker = fork(join(__dirname, 'eval-worker.js'), [bundlePath]);\n\n worker.on('exit', (exitCode: number) => {\n if (exitCode === 0) {\n return resolve(null);\n }\n\n return reject(\n new Error(`Process exited with non-zero exit code: ${exitCode}`),\n );\n });\n });\n}\n"]}
package/dist/fs.d.ts CHANGED
@@ -58,3 +58,12 @@ export declare function validateFilePath(filePath: string): Promise<boolean>;
58
58
  * @throws If the directory does not exist or could not be created.
59
59
  */
60
60
  export declare function validateDirPath(dirPath: string, createDir: boolean): Promise<boolean>;
61
+ /**
62
+ * Creates a temporary file with a given name and content, writes it to disk and calls the provided function.
63
+ * This function handles deletion of the temporary file after usage.
64
+ *
65
+ * @param fileName - The name of the temporary file.
66
+ * @param fileContents - The content of the temporary file.
67
+ * @param fn - The callback function to call when the temporary file has been created.
68
+ */
69
+ export declare function useTemporaryFile(fileName: string, fileContents: string, fn: (path: string) => Promise<unknown>): Promise<void>;
package/dist/fs.js CHANGED
@@ -3,8 +3,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.validateDirPath = exports.validateFilePath = exports.validateOutfileName = exports.getOutfilePath = exports.readJsonFile = exports.isFile = exports.isDirectory = void 0;
6
+ exports.useTemporaryFile = exports.validateDirPath = exports.validateFilePath = exports.validateOutfileName = exports.getOutfilePath = exports.readJsonFile = exports.isFile = exports.isDirectory = void 0;
7
7
  const fs_1 = require("fs");
8
+ const os_1 = __importDefault(require("os"));
8
9
  const path_1 = __importDefault(require("path"));
9
10
  const virtual_file_1 = require("./virtual-file");
10
11
  /**
@@ -132,4 +133,26 @@ async function validateDirPath(dirPath, createDir) {
132
133
  return true;
133
134
  }
134
135
  exports.validateDirPath = validateDirPath;
136
+ /**
137
+ * Creates a temporary file with a given name and content, writes it to disk and calls the provided function.
138
+ * This function handles deletion of the temporary file after usage.
139
+ *
140
+ * @param fileName - The name of the temporary file.
141
+ * @param fileContents - The content of the temporary file.
142
+ * @param fn - The callback function to call when the temporary file has been created.
143
+ */
144
+ async function useTemporaryFile(fileName, fileContents, fn) {
145
+ const filePath = path_1.default.join(os_1.default.tmpdir(), fileName);
146
+ await fs_1.promises.mkdir(path_1.default.dirname(filePath), { recursive: true });
147
+ await fs_1.promises.writeFile(filePath, fileContents);
148
+ try {
149
+ await fn(filePath);
150
+ }
151
+ finally {
152
+ if (await isFile(filePath)) {
153
+ await fs_1.promises.unlink(filePath);
154
+ }
155
+ }
156
+ }
157
+ exports.useTemporaryFile = useTemporaryFile;
135
158
  //# sourceMappingURL=fs.js.map
package/dist/fs.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"fs.js","sourceRoot":"","sources":["../src/fs.ts"],"names":[],"mappings":";;;;;;AACA,2BAAoC;AACpC,gDAA6B;AAE7B,iDAA8D;AAE9D;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAC/B,UAAkB,EAClB,SAAkB;IAElB,IAAI;QACF,MAAM,KAAK,GAAG,MAAM,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC;KAC5B;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE;YAC3B,IAAI,CAAC,SAAS,EAAE;gB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GAOG;AACH,SAAgB,mBAAmB,CAAC,QAAgB;IAClD,IACE,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;QACzB,QAAQ,KAAK,KAAK;QAClB,cAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,KAAK,QAAQ,EACzC;QACA,MAAM,IAAI,KAAK,CAAC,yBAAyB,QAAQ,sBAAsB,CAAC,CAAC;KAC1E;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AATD,kDASC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,gBAAgB,CAAC,QAAgB;IACrD,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,oBAAoB,QAAQ,oCAAoC,CACjE,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AARD,4CAQC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,eAAe,CACnC,OAAe,EACf,SAAkB;IAElB,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACrD,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,oBAAoB,OAAO,+CAA+C,CAC3E,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAXD,0CAWC","sourcesContent":["import { Json } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport pathUtils from 'path';\n\nimport { readVirtualFile, VirtualFile } from './virtual-file';\n\n/**\n * Checks whether the given path string resolves to an existing directory, and\n * optionally creates the directory if it doesn't exist.\n *\n * @param pathString - The path string to check.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns Whether the given path is an existing directory.\n */\nexport async function isDirectory(\n pathString: string,\n createDir: boolean,\n): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isDirectory();\n } catch (error) {\n if (error.code === 'ENOENT') {\n if (!createDir) {\n return false;\n }\n\n await fs.mkdir(pathString, { recursive: true });\n return true;\n }\n\n return false;\n }\n}\n\n/**\n * Checks whether the given path string resolves to an existing file.\n *\n * @param pathString - The path string to check.\n * @returns Whether the given path is an existing file.\n */\nexport async function isFile(pathString: string): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isFile();\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Reads a `.json` file, parses its contents, and returns them.\n *\n * @param pathString - The path to the JSON file.\n * @returns The parsed contents of the JSON file.\n */\nexport async function readJsonFile<Type extends Json = Json>(\n pathString: string,\n): Promise<VirtualFile<Type>> {\n if (!pathString.endsWith('.json')) {\n throw new Error('The specified file must be a \".json\" file.');\n }\n\n let file;\n try {\n file = await readVirtualFile(pathString, 'utf8');\n } catch (error) {\n if (error.code === 'ENOENT') {\n throw new Error(\n `Could not find '${pathString}'. Please ensure that the file exists.`,\n );\n }\n\n throw error;\n }\n file.result = JSON.parse(file.toString());\n return file as VirtualFile<Type>;\n}\n\n/**\n * Gets the complete out file path from an output file name and parent\n * directory path.\n *\n * @param outDir - The path to the out file's parent directory.\n * @param outFileName - The out file's name.\n * @returns The complete path to the out file.\n */\nexport function getOutfilePath(outDir: string, outFileName: string): string {\n return pathUtils.join(outDir, outFileName || 'bundle.js');\n}\n\n/**\n * Ensures that the outfile name is just a `.js` file name.\n * Throws on validation failure.\n *\n * @param filename - The file name to validate.\n * @returns `true` if validation succeeded.\n * @throws If the file name is invalid.\n */\nexport function validateOutfileName(filename: string): boolean {\n if (\n !filename.endsWith('.js') ||\n filename === '.js' ||\n pathUtils.basename(filename) !== filename\n ) {\n throw new Error(`Invalid outfile name: ${filename}. Must be a .js file`);\n }\n return true;\n}\n\n/**\n * Validates a file path. Throws on validation failure.\n *\n * @param filePath - The file path to validate.\n * @returns `true` if validation succeeded.\n * @throws If the path does not resolve to a file.\n */\nexport async function validateFilePath(filePath: string): Promise<boolean> {\n const exists = await isFile(filePath);\n if (!exists) {\n throw new Error(\n `Invalid params: '${filePath}' is not a file or does not exist.`,\n );\n }\n return true;\n}\n\n/**\n * Validates a directory path. Throws on validation failure.\n *\n * @param dirPath - The directory path to validate.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns `true` if validation succeeded or the directory was created.\n * @throws If the directory does not exist or could not be created.\n */\nexport async function validateDirPath(\n dirPath: string,\n createDir: boolean,\n): Promise<boolean> {\n const exists = await isDirectory(dirPath, createDir);\n if (!exists) {\n throw new Error(\n `Invalid params: '${dirPath}' is not a directory or could not be created.`,\n );\n }\n return true;\n}\n"]}
1
+ {"version":3,"file":"fs.js","sourceRoot":"","sources":["../src/fs.ts"],"names":[],"mappings":";;;;;;AACA,2BAAoC;AACpC,4CAAoB;AACpB,gDAA6B;AAE7B,iDAA8D;AAE9D;;;;;;;GAOG;AACI,KAAK,UAAU,WAAW,CAC/B,UAAkB,EAClB,SAAkB;IAElB,IAAI;QACF,MAAM,KAAK,GAAG,MAAM,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC;KAC5B;IAAC,OAAO,KAAK,EAAE;QACd,IAAI,KAAK,CAAC,IAAI,KAAK,QAAQ,EAAE;YAC3B,IAAI,CAAC,SAAS,EAAE;gBACd,OAAO,KAAK,CAAC;aACd;YAED,MAAM,aAAE,CAAC,KAAK,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAChD,OAAO,IAAI,CAAC;SACb;QAED,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAnBD,kCAmBC;AAED;;;;;GAKG;AACI,KAAK,UAAU,MAAM,CAAC,UAAkB;IAC7C,IAAI;QACF,MAAM,KAAK,GAAG,MAAM,aAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACxC,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;KACvB;IAAC,OAAO,KAAK,EAAE;QACd,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AAPD,wBAOC;AA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AI,KAAK,CACb,oBAAoB,QAAQ,oCAAoC,CACjE,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AARD,4CAQC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,eAAe,CACnC,OAAe,EACf,SAAkB;IAElB,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IACrD,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,oBAAoB,OAAO,+CAA+C,CAC3E,CAAC;KACH;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAXD,0CAWC;AAED;;;;;;;GAOG;AACI,KAAK,UAAU,gBAAgB,CACpC,QAAgB,EAChB,YAAoB,EACpB,EAAsC;IAEtC,MAAM,QAAQ,GAAG,cAAS,CAAC,IAAI,CAAC,YAAE,CAAC,MAAM,EAAE,EAAE,QAAQ,CAAC,CAAC;IACvD,MAAM,aAAE,CAAC,KAAK,CAAC,cAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACjE,MAAM,aAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;IAC3C,IAAI;QACF,MAAM,EAAE,CAAC,QAAQ,CAAC,CAAC;KACpB;YAAS;QACR,IAAI,MAAM,MAAM,CAAC,QAAQ,CAAC,EAAE;YAC1B,MAAM,aAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;SAC3B;KACF;AACH,CAAC;AAfD,4CAeC","sourcesContent":["import { Json } from '@metamask/utils';\nimport { promises as fs } from 'fs';\nimport os from 'os';\nimport pathUtils from 'path';\n\nimport { readVirtualFile, VirtualFile } from './virtual-file';\n\n/**\n * Checks whether the given path string resolves to an existing directory, and\n * optionally creates the directory if it doesn't exist.\n *\n * @param pathString - The path string to check.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns Whether the given path is an existing directory.\n */\nexport async function isDirectory(\n pathString: string,\n createDir: boolean,\n): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isDirectory();\n } catch (error) {\n if (error.code === 'ENOENT') {\n if (!createDir) {\n return false;\n }\n\n await fs.mkdir(pathString, { recursive: true });\n return true;\n }\n\n return false;\n }\n}\n\n/**\n * Checks whether the given path string resolves to an existing file.\n *\n * @param pathString - The path string to check.\n * @returns Whether the given path is an existing file.\n */\nexport async function isFile(pathString: string): Promise<boolean> {\n try {\n const stats = await fs.stat(pathString);\n return stats.isFile();\n } catch (error) {\n return false;\n }\n}\n\n/**\n * Reads a `.json` file, parses its contents, and returns them.\n *\n * @param pathString - The path to the JSON file.\n * @returns The parsed contents of the JSON file.\n */\nexport async function readJsonFile<Type extends Json = Json>(\n pathString: string,\n): Promise<VirtualFile<Type>> {\n if (!pathString.endsWith('.json')) {\n throw new Error('The specified file must be a \".json\" file.');\n }\n\n let file;\n try {\n file = await readVirtualFile(pathString, 'utf8');\n } catch (error) {\n if (error.code === 'ENOENT') {\n throw new Error(\n `Could not find '${pathString}'. Please ensure that the file exists.`,\n );\n }\n\n throw error;\n }\n file.result = JSON.parse(file.toString());\n return file as VirtualFile<Type>;\n}\n\n/**\n * Gets the complete out file path from an output file name and parent\n * directory path.\n *\n * @param outDir - The path to the out file's parent directory.\n * @param outFileName - The out file's name.\n * @returns The complete path to the out file.\n */\nexport function getOutfilePath(outDir: string, outFileName: string): string {\n return pathUtils.join(outDir, outFileName || 'bundle.js');\n}\n\n/**\n * Ensures that the outfile name is just a `.js` file name.\n * Throws on validation failure.\n *\n * @param filename - The file name to validate.\n * @returns `true` if validation succeeded.\n * @throws If the file name is invalid.\n */\nexport function validateOutfileName(filename: string): boolean {\n if (\n !filename.endsWith('.js') ||\n filename === '.js' ||\n pathUtils.basename(filename) !== filename\n ) {\n throw new Error(`Invalid outfile name: ${filename}. Must be a .js file`);\n }\n return true;\n}\n\n/**\n * Validates a file path. Throws on validation failure.\n *\n * @param filePath - The file path to validate.\n * @returns `true` if validation succeeded.\n * @throws If the path does not resolve to a file.\n */\nexport async function validateFilePath(filePath: string): Promise<boolean> {\n const exists = await isFile(filePath);\n if (!exists) {\n throw new Error(\n `Invalid params: '${filePath}' is not a file or does not exist.`,\n );\n }\n return true;\n}\n\n/**\n * Validates a directory path. Throws on validation failure.\n *\n * @param dirPath - The directory path to validate.\n * @param createDir - Whether to create the directory if it doesn't exist.\n * @returns `true` if validation succeeded or the directory was created.\n * @throws If the directory does not exist or could not be created.\n */\nexport async function validateDirPath(\n dirPath: string,\n createDir: boolean,\n): Promise<boolean> {\n const exists = await isDirectory(dirPath, createDir);\n if (!exists) {\n throw new Error(\n `Invalid params: '${dirPath}' is not a directory or could not be created.`,\n );\n }\n return true;\n}\n\n/**\n * Creates a temporary file with a given name and content, writes it to disk and calls the provided function.\n * This function handles deletion of the temporary file after usage.\n *\n * @param fileName - The name of the temporary file.\n * @param fileContents - The content of the temporary file.\n * @param fn - The callback function to call when the temporary file has been created.\n */\nexport async function useTemporaryFile(\n fileName: string,\n fileContents: string,\n fn: (path: string) => Promise<unknown>,\n): Promise<void> {\n const filePath = pathUtils.join(os.tmpdir(), fileName);\n await fs.mkdir(pathUtils.dirname(filePath), { recursive: true });\n await fs.writeFile(filePath, fileContents);\n try {\n await fn(filePath);\n } finally {\n if (await isFile(filePath)) {\n await fs.unlink(filePath);\n }\n }\n}\n"]}
@@ -18,15 +18,12 @@ export declare type OnRpcRequestHandler = (args: {
18
18
  /**
19
19
  * The response from a snap's `onTransaction` handler.
20
20
  *
21
- * @property insights - The insights object. This is a key-value map of the
22
- * insights that the snap has about the transaction. The keys are the insight
23
- * names, and the values are the insight values. The insight values must be
24
- * JSON-serializable.
21
+ * @property content - A custom UI component, that will be shown in MetaMask. Can be created using `@metamask/snaps-ui`.
25
22
  *
26
23
  * If the snap has no insights about the transaction, this should be `null`.
27
24
  */
28
25
  export declare type OnTransactionResponse = {
29
- content: Component;
26
+ content: Component | null;
30
27
  };
31
28
  /**
32
29
  * The `onTransaction` handler. This is called whenever a transaction is
@@ -1 +1 @@
1
- {"version":3,"file":"handlers.js","sourceRoot":"","sources":["../src/handlers.ts"],"names":[],"mappings":"","sourcesContent":["import { Component } from '@metamask/snaps-ui';\nimport { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { AccountId, ChainId, RequestArguments } from './namespace';\n\n/**\n * The `onRpcRequest` handler. This is called whenever a JSON-RPC request is\n * made to the snap.\n *\n * @param args - The request arguments.\n * @param args.origin - The origin of the request. This can be the ID of another\n * snap, or the URL of a dapp.\n * @param args.request - The JSON-RPC request sent to the snap.\n * @returns The JSON-RPC response. This must be a JSON-serializable value.\n */\nexport type OnRpcRequestHandler = (args: {\n origin: string;\n request: JsonRpcRequest<Json[] | Record<string, Json>>;\n}) => Promise<unknown>;\n\n/**\n * The response from a snap's `onTransaction` handler.\n *\n * @property insights - The insights object. This is a key-value map of the\n * insights that the snap has about the transaction. The keys are the insight\n * names, and the values are the insight values. The insight values must be\n * JSON-serializable.\n *\n * If the snap has no insights about the transaction, this should be `null`.\n */\nexport type OnTransactionResponse = {\n content: Component;\n};\n\n/**\n * The `onTransaction` handler. This is called whenever a transaction is\n * submitted to the snap. It can return insights about the transaction, which\n * will be displayed to the user.\n *\n * @param args - The request arguments.\n * @param args.transaction - The transaction object.\n * @param args.chainId - The CAIP-2 chain ID of the network the transaction is\n * being submitted to.\n * @param args.transactionOrigin - The origin of the transaction. This is the\n * URL of the dapp that submitted the transaction.\n * @returns Insights about the transaction. See {@link OnTransactionResponse}.\n */\n// TODO: Improve type.\nexport type OnTransactionHandler = (args: {\n transaction: { [key: string]: Json };\n chainId: string;\n transactionOrigin?: string;\n}) => Promise<OnTransactionResponse>;\n\n/**\n * The `onCronjob` handler. This is called on a regular interval, as defined by\n * the snap's manifest.\n *\n * @param args - The request arguments.\n * @param args.request - The JSON-RPC request sent to the snap.\n */\nexport type OnCronjobHandler = (args: {\n request: JsonRpcRequest<Json[] | Record<string, Json>>;\n}) => Promise<unknown>;\n\n/**\n * A request sent to the `handleRequest` handler of a snap keyring.\n *\n * @property chainId - The CAIP-2 chain ID of the network the request is being\n * made to.\n * @property origin - The origin of the request. This can be the ID of another\n * snap, or the URL of a dapp.\n */\nexport type KeyringRequest = {\n chainId: ChainId;\n origin: string;\n};\n\n/**\n * A keyring event, which consists of a {@link KeyringRequest}, combined with\n * the name of the event.\n */\nexport type KeyringEvent = KeyringRequest & { eventName: string };\n\n/**\n * A snap keyring object, which can be exported as `keyring` in a snap.\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface SnapKeyring {\n getAccounts(): Promise<AccountId[]>;\n handleRequest(\n data: KeyringRequest & {\n request: RequestArguments;\n },\n ): Promise<Json>;\n on(data: KeyringEvent, listener: (...args: Json[]) => void): void;\n off(data: KeyringEvent): void;\n\n addAccount?(chainId: ChainId): Promise<AccountId>;\n removeAccount?(accountId: AccountId): Promise<void>;\n\n importAccount?(chainId: ChainId, data: Json): Promise<AccountId>;\n exportAccount?(accountId: AccountId): Promise<Json>;\n}\n\n/**\n * All the function-based handlers that a snap can implement.\n */\nexport type SnapFunctionExports = {\n onRpcRequest?: OnRpcRequestHandler;\n onTransaction?: OnTransactionHandler;\n onCronjob?: OnCronjobHandler;\n};\n\n/**\n * All handlers that a snap can implement.\n */\nexport type SnapExports = SnapFunctionExports & {\n keyring?: SnapKeyring;\n};\n"]}
1
+ {"version":3,"file":"handlers.js","sourceRoot":"","sources":["../src/handlers.ts"],"names":[],"mappings":"","sourcesContent":["import { Component } from '@metamask/snaps-ui';\nimport { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { AccountId, ChainId, RequestArguments } from './namespace';\n\n/**\n * The `onRpcRequest` handler. This is called whenever a JSON-RPC request is\n * made to the snap.\n *\n * @param args - The request arguments.\n * @param args.origin - The origin of the request. This can be the ID of another\n * snap, or the URL of a dapp.\n * @param args.request - The JSON-RPC request sent to the snap.\n * @returns The JSON-RPC response. This must be a JSON-serializable value.\n */\nexport type OnRpcRequestHandler = (args: {\n origin: string;\n request: JsonRpcRequest<Json[] | Record<string, Json>>;\n}) => Promise<unknown>;\n\n/**\n * The response from a snap's `onTransaction` handler.\n *\n * @property content - A custom UI component, that will be shown in MetaMask. Can be created using `@metamask/snaps-ui`.\n *\n * If the snap has no insights about the transaction, this should be `null`.\n */\nexport type OnTransactionResponse = {\n content: Component | null;\n};\n\n/**\n * The `onTransaction` handler. This is called whenever a transaction is\n * submitted to the snap. It can return insights about the transaction, which\n * will be displayed to the user.\n *\n * @param args - The request arguments.\n * @param args.transaction - The transaction object.\n * @param args.chainId - The CAIP-2 chain ID of the network the transaction is\n * being submitted to.\n * @param args.transactionOrigin - The origin of the transaction. This is the\n * URL of the dapp that submitted the transaction.\n * @returns Insights about the transaction. See {@link OnTransactionResponse}.\n */\n// TODO: Improve type.\nexport type OnTransactionHandler = (args: {\n transaction: { [key: string]: Json };\n chainId: string;\n transactionOrigin?: string;\n}) => Promise<OnTransactionResponse>;\n\n/**\n * The `onCronjob` handler. This is called on a regular interval, as defined by\n * the snap's manifest.\n *\n * @param args - The request arguments.\n * @param args.request - The JSON-RPC request sent to the snap.\n */\nexport type OnCronjobHandler = (args: {\n request: JsonRpcRequest<Json[] | Record<string, Json>>;\n}) => Promise<unknown>;\n\n/**\n * A request sent to the `handleRequest` handler of a snap keyring.\n *\n * @property chainId - The CAIP-2 chain ID of the network the request is being\n * made to.\n * @property origin - The origin of the request. This can be the ID of another\n * snap, or the URL of a dapp.\n */\nexport type KeyringRequest = {\n chainId: ChainId;\n origin: string;\n};\n\n/**\n * A keyring event, which consists of a {@link KeyringRequest}, combined with\n * the name of the event.\n */\nexport type KeyringEvent = KeyringRequest & { eventName: string };\n\n/**\n * A snap keyring object, which can be exported as `keyring` in a snap.\n */\n// eslint-disable-next-line @typescript-eslint/consistent-type-definitions\nexport interface SnapKeyring {\n getAccounts(): Promise<AccountId[]>;\n handleRequest(\n data: KeyringRequest & {\n request: RequestArguments;\n },\n ): Promise<Json>;\n on(data: KeyringEvent, listener: (...args: Json[]) => void): void;\n off(data: KeyringEvent): void;\n\n addAccount?(chainId: ChainId): Promise<AccountId>;\n removeAccount?(accountId: AccountId): Promise<void>;\n\n importAccount?(chainId: ChainId, data: Json): Promise<AccountId>;\n exportAccount?(accountId: AccountId): Promise<Json>;\n}\n\n/**\n * All the function-based handlers that a snap can implement.\n */\nexport type SnapFunctionExports = {\n onRpcRequest?: OnRpcRequestHandler;\n onTransaction?: OnTransactionHandler;\n onCronjob?: OnCronjobHandler;\n};\n\n/**\n * All handlers that a snap can implement.\n */\nexport type SnapExports = SnapFunctionExports & {\n keyring?: SnapKeyring;\n};\n"]}
package/dist/iframe.js CHANGED
@@ -16,6 +16,7 @@ async function createWindow(uri, jobId) {
16
16
  // The order of operations appears to matter for everything except this
17
17
  // attribute. We may as well set it here.
18
18
  iframe.setAttribute('id', jobId);
19
+ iframe.setAttribute('data-testid', 'snaps-iframe');
19
20
  // For the sandbox property to have any effect it needs to be set before the iframe is appended.
20
21
  // We apply this property as a principle of least authority (POLA)
21
22
  // measure.
@@ -1 +1 @@
1
- {"version":3,"file":"iframe.js","sourceRoot":"","sources":["../src/iframe.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACI,KAAK,UAAU,YAAY,CAChC,GAAW,EACX,KAAa;IAEb,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAChD,uEAAuE;QACvE,yCAAyC;QACzC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAEjC,gGAAgG;QAChG,kEAAkE;QAClE,WAAW;QACX,qFAAqF;QACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAEhD,uEAAuE;QACvE,yEAAyE;QACzE,uEAAuE;QACvE,wEAAwE;QACxE,uBAAuB;QACvB,EAAE;QACF,mEAAmE;QACnE,kCAAkC;QAClC,EAAE;QACF,mGAAmG;QACnG,iJAAiJ;QACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACnC,IAAI,MAAM,CAAC,aAAa,EAAE;gBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;aAC/B;iBAAM;gBACL,qEAAqE;gBACrE,mBAAmB;gBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AA5CD,oCA4CC","sourcesContent":["/**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\nexport async function createWindow(\n uri: string,\n jobId: string,\n): Promise<Window> {\n return await new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n\n // For the sandbox property to have any effect it needs to be set before the iframe is appended.\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n });\n}\n"]}
1
+ {"version":3,"file":"iframe.js","sourceRoot":"","sources":["../src/iframe.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACI,KAAK,UAAU,YAAY,CAChC,GAAW,EACX,KAAa;IAEb,OAAO,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC3C,MAAM,MAAM,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAChD,uEAAuE;QACvE,yCAAyC;QACzC,MAAM,CAAC,YAAY,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QACjC,MAAM,CAAC,YAAY,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC;QAEnD,gGAAgG;QAChG,kEAAkE;QAClE,WAAW;QACX,qFAAqF;QACrF,MAAM,CAAC,YAAY,CAAC,SAAS,EAAE,eAAe,CAAC,CAAC;QAEhD,uEAAuE;QACvE,yEAAyE;QACzE,uEAAuE;QACvE,wEAAwE;QACxE,uBAAuB;QACvB,EAAE;QACF,mEAAmE;QACnE,kCAAkC;QAClC,EAAE;QACF,mGAAmG;QACnG,iJAAiJ;QACjJ,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAChC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;QAElC,MAAM,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACnC,IAAI,MAAM,CAAC,aAAa,EAAE;gBACxB,OAAO,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;aAC/B;iBAAM;gBACL,qEAAqE;gBACrE,mBAAmB;gBACnB,MAAM,CACJ,IAAI,KAAK,CACP,qDAAqD,KAAK,IAAI,CAC/D,CACF,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AA7CD,oCA6CC","sourcesContent":["/**\n * Creates the iframe to be used as the execution environment. This may run\n * forever if the iframe never loads, but the promise should be wrapped in\n * an initialization timeout in the SnapController.\n *\n * @param uri - The iframe URI.\n * @param jobId - The job id.\n * @returns A promise that resolves to the contentWindow of the iframe.\n */\nexport async function createWindow(\n uri: string,\n jobId: string,\n): Promise<Window> {\n return await new Promise((resolve, reject) => {\n const iframe = document.createElement('iframe');\n // The order of operations appears to matter for everything except this\n // attribute. We may as well set it here.\n iframe.setAttribute('id', jobId);\n iframe.setAttribute('data-testid', 'snaps-iframe');\n\n // For the sandbox property to have any effect it needs to be set before the iframe is appended.\n // We apply this property as a principle of least authority (POLA)\n // measure.\n // Ref: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox\n iframe.setAttribute('sandbox', 'allow-scripts');\n\n // In the past, we've had problems that appear to be symptomatic of the\n // iframe firing the `load` event before its scripts are actually loaded,\n // which has prevented snaps from executing properly. Therefore, we set\n // the `src` attribute and append the iframe to the DOM before attaching\n // the `load` listener.\n //\n // `load` should only fire when \"all dependent resources\" have been\n // loaded, which includes scripts.\n //\n // MDN article for `load` event: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event\n // Re: `load` firing twice: https://stackoverflow.com/questions/10781880/dynamically-created-iframe-triggers-onload-event-twice/15880489#15880489\n iframe.setAttribute('src', uri);\n document.body.appendChild(iframe);\n\n iframe.addEventListener('load', () => {\n if (iframe.contentWindow) {\n resolve(iframe.contentWindow);\n } else {\n // We don't know of a case when this would happen, but better to fail\n // fast if it does.\n reject(\n new Error(\n `iframe.contentWindow not present on load for job \"${jobId}\".`,\n ),\n );\n }\n });\n });\n}\n"]}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const iframe_1 = require("./iframe");
4
+ const IFRAME_URL = `http://localhost:4569`;
5
+ const MOCK_JOB_ID = 'job-id';
6
+ describe('createWindow', () => {
7
+ it('creates an iframe window with the provided job ID as the iframe ID', async () => {
8
+ const window = await (0, iframe_1.createWindow)(IFRAME_URL, MOCK_JOB_ID);
9
+ const iframe = document.getElementById(MOCK_JOB_ID);
10
+ expect(iframe).toBeDefined();
11
+ expect(iframe.contentWindow).toBe(window);
12
+ expect(iframe.id).toBe(MOCK_JOB_ID);
13
+ });
14
+ });
15
+ //# sourceMappingURL=iframe.test.browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iframe.test.browser.js","sourceRoot":"","sources":["../src/iframe.test.browser.ts"],"names":[],"mappings":";;AAAA,qCAAwC;AAExC,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAE3C,MAAM,WAAW,GAAG,QAAQ,CAAC;AAE7B,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;IAC5B,EAAE,CAAC,oEAAoE,EAAE,KAAK,IAAI,EAAE;QAClF,MAAM,MAAM,GAAG,MAAM,IAAA,qBAAY,EAAC,UAAU,EAAE,WAAW,CAAC,CAAC;QAC3D,MAAM,MAAM,GAAG,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAsB,CAAC;QAEzE,MAAM,CAAC,MAAM,CAAC,CAAC,WAAW,EAAE,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1C,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { createWindow } from './iframe';\n\nconst IFRAME_URL = `http://localhost:4569`;\n\nconst MOCK_JOB_ID = 'job-id';\n\ndescribe('createWindow', () => {\n it('creates an iframe window with the provided job ID as the iframe ID', async () => {\n const window = await createWindow(IFRAME_URL, MOCK_JOB_ID);\n const iframe = document.getElementById(MOCK_JOB_ID) as HTMLIFrameElement;\n\n expect(iframe).toBeDefined();\n expect(iframe.contentWindow).toBe(window);\n expect(iframe.id).toBe(MOCK_JOB_ID);\n });\n});\n"]}
@@ -4,7 +4,6 @@ export * from './cronjob';
4
4
  export * from './deep-clone';
5
5
  export * from './default-endowments';
6
6
  export * from './entropy';
7
- export * from './flatMap';
8
7
  export * from './handlers';
9
8
  export * from './iframe';
10
9
  export * from './json-rpc';
@@ -12,7 +11,6 @@ export * from './logging';
12
11
  export * from './manifest/index.browser';
13
12
  export * from './namespace';
14
13
  export * from './notification';
15
- export * from './object';
16
14
  export * from './path';
17
15
  export * from './snaps';
18
16
  export * from './types';
@@ -20,7 +20,6 @@ __exportStar(require("./cronjob"), exports);
20
20
  __exportStar(require("./deep-clone"), exports);
21
21
  __exportStar(require("./default-endowments"), exports);
22
22
  __exportStar(require("./entropy"), exports);
23
- __exportStar(require("./flatMap"), exports);
24
23
  __exportStar(require("./handlers"), exports);
25
24
  __exportStar(require("./iframe"), exports);
26
25
  __exportStar(require("./json-rpc"), exports);
@@ -28,7 +27,6 @@ __exportStar(require("./logging"), exports);
28
27
  __exportStar(require("./manifest/index.browser"), exports);
29
28
  __exportStar(require("./namespace"), exports);
30
29
  __exportStar(require("./notification"), exports);
31
- __exportStar(require("./object"), exports);
32
30
  __exportStar(require("./path"), exports);
33
31
  __exportStar(require("./snaps"), exports);
34
32
  __exportStar(require("./types"), exports);
@@ -1 +1 @@
1
- {"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,6CAA2B;AAC3B,4CAA0B;AAC1B,+CAA6B;AAC7B,uDAAqC;AACrC,4CAA0B;AAC1B,4CAA0B;AAC1B,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,4CAA0B;AAC1B,2DAAyC;AACzC,8CAA4B;AAC5B,iDAA+B;AAC/B,2CAAyB;AACzB,yCAAuB;AACvB,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,+DAA6C","sourcesContent":["export * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './flatMap';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './notification';\nexport * from './object';\nexport * from './path';\nexport * from './snaps';\nexport * from './types';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"]}
1
+ {"version":3,"file":"index.browser.js","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,6CAA2B;AAC3B,4CAA0B;AAC1B,+CAA6B;AAC7B,uDAAqC;AACrC,4CAA0B;AAC1B,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,4CAA0B;AAC1B,2DAAyC;AACzC,8CAA4B;AAC5B,iDAA+B;AAC/B,yCAAuB;AACvB,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,+DAA6C","sourcesContent":["export * from './caveats';\nexport * from './checksum';\nexport * from './cronjob';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest/index.browser';\nexport * from './namespace';\nexport * from './notification';\nexport * from './path';\nexport * from './snaps';\nexport * from './types';\nexport * from './versions';\nexport * from './virtual-file/index.browser';\n"]}
package/dist/index.d.ts CHANGED
@@ -5,7 +5,6 @@ export * from './deep-clone';
5
5
  export * from './default-endowments';
6
6
  export * from './entropy';
7
7
  export * from './eval';
8
- export * from './flatMap';
9
8
  export * from './fs';
10
9
  export * from './handlers';
11
10
  export * from './iframe';
@@ -16,7 +15,6 @@ export * from './mock';
16
15
  export * from './namespace';
17
16
  export * from './notification';
18
17
  export * from './npm';
19
- export * from './object';
20
18
  export * from './path';
21
19
  export * from './post-process';
22
20
  export * from './snaps';
package/dist/index.js CHANGED
@@ -21,7 +21,6 @@ __exportStar(require("./deep-clone"), exports);
21
21
  __exportStar(require("./default-endowments"), exports);
22
22
  __exportStar(require("./entropy"), exports);
23
23
  __exportStar(require("./eval"), exports);
24
- __exportStar(require("./flatMap"), exports);
25
24
  __exportStar(require("./fs"), exports);
26
25
  __exportStar(require("./handlers"), exports);
27
26
  __exportStar(require("./iframe"), exports);
@@ -32,7 +31,6 @@ __exportStar(require("./mock"), exports);
32
31
  __exportStar(require("./namespace"), exports);
33
32
  __exportStar(require("./notification"), exports);
34
33
  __exportStar(require("./npm"), exports);
35
- __exportStar(require("./object"), exports);
36
34
  __exportStar(require("./path"), exports);
37
35
  __exportStar(require("./post-process"), exports);
38
36
  __exportStar(require("./snaps"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,4CAA0B;AAC1B,6CAA2B;AAC3B,+CAA6B;AAC7B,uDAAqC;AACrC,4CAA0B;AAC1B,yCAAuB;AACvB,4CAA0B;AAC1B,uCAAqB;AACrB,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,4CAA0B;AAC1B,6CAA2B;AAC3B,yCAAuB;AACvB,8CAA4B;AAC5B,iDAA+B;AAC/B,wCAAsB;AACtB,2CAAyB;AACzB,yCAAuB;AACvB,iDAA+B;AAC/B,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,iDAA+B","sourcesContent":["export * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './eval';\nexport * from './flatMap';\nexport * from './fs';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './notification';\nexport * from './npm';\nexport * from './object';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './types';\nexport * from './versions';\nexport * from './virtual-file';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA0B;AAC1B,4CAA0B;AAC1B,6CAA2B;AAC3B,+CAA6B;AAC7B,uDAAqC;AACrC,4CAA0B;AAC1B,yCAAuB;AACvB,uCAAqB;AACrB,6CAA2B;AAC3B,2CAAyB;AACzB,6CAA2B;AAC3B,4CAA0B;AAC1B,6CAA2B;AAC3B,yCAAuB;AACvB,8CAA4B;AAC5B,iDAA+B;AAC/B,wCAAsB;AACtB,yCAAuB;AACvB,iDAA+B;AAC/B,0CAAwB;AACxB,0CAAwB;AACxB,6CAA2B;AAC3B,iDAA+B","sourcesContent":["export * from './caveats';\nexport * from './cronjob';\nexport * from './checksum';\nexport * from './deep-clone';\nexport * from './default-endowments';\nexport * from './entropy';\nexport * from './eval';\nexport * from './fs';\nexport * from './handlers';\nexport * from './iframe';\nexport * from './json-rpc';\nexport * from './logging';\nexport * from './manifest';\nexport * from './mock';\nexport * from './namespace';\nexport * from './notification';\nexport * from './npm';\nexport * from './path';\nexport * from './post-process';\nexport * from './snaps';\nexport * from './types';\nexport * from './versions';\nexport * from './virtual-file';\n"]}
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
@@ -77,9 +66,10 @@ async function checkManifest(basePath, writeManifest = true, sourceCode) {
77
66
  // reasons in the enum.
78
67
  for (let attempts = 1; isInvalid && attempts <= maxAttempts; attempts++) {
79
68
  manifest = fixManifest(manifest
80
- ? Object.assign(Object.assign({}, partiallyValidatedFiles), { manifest }) : partiallyValidatedFiles, currentError);
69
+ ? { ...partiallyValidatedFiles, manifest }
70
+ : partiallyValidatedFiles, currentError);
81
71
  try {
82
- validateNpmSnapManifest(Object.assign(Object.assign({}, partiallyValidatedFiles), { manifest }));
72
+ validateNpmSnapManifest({ ...partiallyValidatedFiles, manifest });
83
73
  isInvalid = false;
84
74
  }
85
75
  catch (nextValidationError) {
@@ -173,11 +163,11 @@ exports.fixManifest = fixManifest;
173
163
  * @returns The contents of the bundle file, if any.
174
164
  */
175
165
  async function getSnapSourceCode(basePath, manifest, sourceCode) {
176
- var _a, _b, _c;
177
166
  if (!(0, utils_1.isPlainObject)(manifest)) {
178
167
  return undefined;
179
168
  }
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;
169
+ const sourceFilePath = manifest.source?.location
170
+ ?.npm?.filePath;
181
171
  if (!sourceFilePath) {
182
172
  return undefined;
183
173
  }
@@ -205,11 +195,11 @@ exports.getSnapSourceCode = getSnapSourceCode;
205
195
  * @returns The contents of the icon, if any.
206
196
  */
207
197
  async function getSnapIcon(basePath, manifest) {
208
- var _a, _b, _c;
209
198
  if (!(0, utils_1.isPlainObject)(manifest)) {
210
199
  return undefined;
211
200
  }
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;
201
+ const iconPath = manifest.source?.location?.npm
202
+ ?.iconPath;
213
203
  if (!iconPath) {
214
204
  return undefined;
215
205
  }
@@ -230,11 +220,14 @@ exports.getSnapIcon = getSnapIcon;
230
220
  * @returns The disk-ready manifest.
231
221
  */
232
222
  function getWritableManifest(manifest) {
233
- const { repository } = manifest, remaining = __rest(manifest, ["repository"]);
234
- const keys = Object.keys(repository ? Object.assign(Object.assign({}, remaining), { repository }) : remaining);
223
+ const { repository, ...remaining } = manifest;
224
+ const keys = Object.keys(repository ? { ...remaining, repository } : remaining);
235
225
  const writableManifest = keys
236
226
  .sort((a, b) => MANIFEST_SORT_ORDER[a] - MANIFEST_SORT_ORDER[b])
237
- .reduce((result, key) => (Object.assign(Object.assign({}, result), { [key]: manifest[key] })), {});
227
+ .reduce((result, key) => ({
228
+ ...result,
229
+ [key]: manifest[key],
230
+ }), {});
238
231
  return writableManifest;
239
232
  }
240
233
  exports.getWritableManifest = getWritableManifest;