@metamask/snaps-sdk 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -1
- package/dist/cjs/errors.js +8 -3
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/index.js +6 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internals/error-wrappers.js +15 -1
- package/dist/cjs/internals/error-wrappers.js.map +1 -1
- package/dist/cjs/internals/index.js +1 -0
- package/dist/cjs/internals/index.js.map +1 -1
- package/dist/cjs/internals/svg.js +49 -0
- package/dist/cjs/internals/svg.js.map +1 -0
- package/dist/cjs/ui/components/image.js +2 -7
- package/dist/cjs/ui/components/image.js.map +1 -1
- package/dist/esm/errors.js +8 -3
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internals/error-wrappers.js +15 -1
- package/dist/esm/internals/error-wrappers.js.map +1 -1
- package/dist/esm/internals/index.js +1 -0
- package/dist/esm/internals/index.js.map +1 -1
- package/dist/esm/internals/svg.js +41 -0
- package/dist/esm/internals/svg.js.map +1 -0
- package/dist/esm/ui/components/image.js +1 -1
- package/dist/esm/ui/components/image.js.map +1 -1
- package/dist/types/error-wrappers.d.ts +464 -48
- package/dist/types/errors.d.ts +2 -4
- package/dist/types/index.d.ts +1 -1
- package/dist/types/internals/error-wrappers.d.ts +29 -3
- package/dist/types/internals/index.d.ts +1 -0
- package/dist/types/internals/svg.d.ts +14 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [2.1.0]
|
|
10
|
+
### Changed
|
|
11
|
+
- Improve support for Snap errors without a message ([#2176](https://github.com/MetaMask/snaps/pull/2176))
|
|
12
|
+
- You can now add data to an error without having to specify a message. For example:
|
|
13
|
+
```ts
|
|
14
|
+
throw new MethodNotFoundError({ method: 'some method name' });
|
|
15
|
+
```
|
|
16
|
+
- Strip empty `data` from Snap errors ([#2179](https://github.com/MetaMask/snaps/pull/2179))
|
|
17
|
+
|
|
9
18
|
## [2.0.0]
|
|
10
19
|
### Changed
|
|
11
20
|
- **BREAKING:** Update name lookup API types ([#2113](https://github.com/MetaMask/snaps/pull/2113))
|
|
@@ -55,7 +64,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
55
64
|
### Added
|
|
56
65
|
- Initial release of this package.
|
|
57
66
|
|
|
58
|
-
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@2.
|
|
67
|
+
[Unreleased]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@2.1.0...HEAD
|
|
68
|
+
[2.1.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@2.0.0...@metamask/snaps-sdk@2.1.0
|
|
59
69
|
[2.0.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.4.0...@metamask/snaps-sdk@2.0.0
|
|
60
70
|
[1.4.0]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.3.2...@metamask/snaps-sdk@1.4.0
|
|
61
71
|
[1.3.2]: https://github.com/MetaMask/snaps/compare/@metamask/snaps-sdk@1.3.1...@metamask/snaps-sdk@1.3.2
|
package/dist/cjs/errors.js
CHANGED
|
@@ -101,7 +101,9 @@ class SnapError extends Error {
|
|
|
101
101
|
code: this.code,
|
|
102
102
|
message: this.message,
|
|
103
103
|
stack: this.stack,
|
|
104
|
-
|
|
104
|
+
...this.data ? {
|
|
105
|
+
data: this.data
|
|
106
|
+
} : {}
|
|
105
107
|
}
|
|
106
108
|
}
|
|
107
109
|
};
|
|
@@ -146,10 +148,13 @@ class SnapError extends Error {
|
|
|
146
148
|
});
|
|
147
149
|
_class_private_field_set(this, _message, message);
|
|
148
150
|
_class_private_field_set(this, _code, (0, _internals.getErrorCode)(error));
|
|
149
|
-
|
|
151
|
+
const mergedData = {
|
|
150
152
|
...(0, _internals.getErrorData)(error),
|
|
151
153
|
...data
|
|
152
|
-
}
|
|
154
|
+
};
|
|
155
|
+
if (Object.keys(mergedData).length > 0) {
|
|
156
|
+
_class_private_field_set(this, _data, mergedData);
|
|
157
|
+
}
|
|
153
158
|
_class_private_field_set(this, _stack, super.stack);
|
|
154
159
|
}
|
|
155
160
|
}
|
package/dist/cjs/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import type { Json, JsonRpcError } from '@metamask/utils';\n\nimport {\n getErrorCode,\n getErrorData,\n getErrorMessage,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import type { Json, JsonRpcError } from '@metamask/utils';\n\nimport {\n getErrorCode,\n getErrorData,\n getErrorMessage,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data?: Record<string, Json>;\n\n readonly #stack?: string;\n\n /**\n * Create a new `SnapError`.\n *\n * @param error - The error to create the `SnapError` from. If this is a\n * `string`, it will be used as the error message. If this is an `Error`, its\n * `message` property will be used as the error message. If this is a\n * `JsonRpcError`, its `message` property will be used as the error message\n * and its `code` property will be used as the error code. Otherwise, the\n * error will be converted to a string and used as the error message.\n * @param data - Additional data to include in the error. This will be merged\n * with the error data, if any.\n */\n constructor(\n error: string | Error | JsonRpcError,\n data: Record<string, Json> = {},\n ) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#message = message;\n this.#code = getErrorCode(error);\n\n const mergedData = { ...getErrorData(error), ...data };\n if (Object.keys(mergedData).length > 0) {\n this.#data = mergedData;\n }\n\n this.#stack = super.stack;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'SnapError';\n }\n\n /**\n * The error code.\n *\n * @returns The error code.\n */\n get code() {\n return this.#code;\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n // This line is covered, but Jest doesn't pick it up for some reason.\n /* istanbul ignore next */\n get message() {\n return this.#message;\n }\n\n /**\n * Additional data for the error.\n *\n * @returns Additional data for the error.\n */\n get data() {\n return this.#data;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n // This line is covered, but Jest doesn't pick it up for some reason.\n /* istanbul ignore next */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapError {\n return {\n code: SNAP_ERROR_CODE,\n message: SNAP_ERROR_MESSAGE,\n data: {\n cause: {\n code: this.code,\n message: this.message,\n stack: this.stack,\n ...(this.data ? { data: this.data } : {}),\n },\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * A serialized {@link SnapError}. It's JSON-serializable, so it can be sent\n * over the RPC. The original error is wrapped in the `cause` property.\n *\n * @property code - The error code. This is always `-31002`.\n * @property message - The error message. This is always `'Snap Error'`.\n * @property data - The error data.\n * @property data.cause - The cause of the error.\n * @property data.cause.code - The error code.\n * @property data.cause.message - The error message.\n * @property data.cause.stack - The error stack.\n * @property data.cause.data - Additional data for the error.\n * @see SnapError\n */\nexport type SerializedSnapError = {\n code: typeof SNAP_ERROR_CODE;\n message: typeof SNAP_ERROR_MESSAGE;\n data: {\n cause: JsonRpcError;\n };\n};\n"],"names":["SnapError","Error","name","code","message","data","stack","toJSON","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","cause","serialize","constructor","error","getErrorMessage","getErrorCode","mergedData","getErrorData","Object","keys","length"],"mappings":";;;;+BAcaA;;;eAAAA;;;2BANN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAOI,qCAEA,wCAEA,qCAEA;AAPJ,MAAMA,kBAAkBC;IAuC7B;;;;GAIC,GACD,IAAIC,OAAO;QACT,OAAO;IACT;IAEA;;;;GAIC,GACD,IAAIC,OAAO;QACT,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,qEAAqE;IACrE,wBAAwB,GACxB,IAAIC,UAAU;QACZ,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,IAAIC,OAAO;QACT,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,qEAAqE;IACrE,wBAAwB,GACxB,IAAIC,QAAQ;QACV,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACDC,SAA8B;QAC5B,OAAO;YACLJ,MAAMK,0BAAe;YACrBJ,SAASK,6BAAkB;YAC3BJ,MAAM;gBACJK,OAAO;oBACLP,MAAM,IAAI,CAACA,IAAI;oBACfC,SAAS,IAAI,CAACA,OAAO;oBACrBE,OAAO,IAAI,CAACA,KAAK;oBACjB,GAAI,IAAI,CAACD,IAAI,GAAG;wBAAEA,MAAM,IAAI,CAACA,IAAI;oBAAC,IAAI,CAAC,CAAC;gBAC1C;YACF;QACF;IACF;IAEA;;;;;GAKC,GACDM,YAAY;QACV,OAAO,IAAI,CAACJ,MAAM;IACpB;IA3GA;;;;;;;;;;;GAWC,GACDK,YACEC,KAAoC,EACpCR,OAA6B,CAAC,CAAC,CAC/B;QACA,MAAMD,UAAUU,IAAAA,0BAAe,EAACD;QAChC,KAAK,CAACT;QAzBR,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;uCAqBQA,UAAUA;uCACVD,OAAOY,IAAAA,uBAAY,EAACF;QAE1B,MAAMG,aAAa;YAAE,GAAGC,IAAAA,uBAAY,EAACJ,MAAM;YAAE,GAAGR,IAAI;QAAC;QACrD,IAAIa,OAAOC,IAAI,CAACH,YAAYI,MAAM,GAAG,GAAG;2CAChCf,OAAOW;QACf;uCAEMV,QAAQ,KAAK,CAACA;IACtB;AAgFF"}
|
package/dist/cjs/index.js
CHANGED
|
@@ -34,6 +34,12 @@ _export(exports, {
|
|
|
34
34
|
enumValue: function() {
|
|
35
35
|
return _internals.enumValue;
|
|
36
36
|
},
|
|
37
|
+
parseSvg: function() {
|
|
38
|
+
return _internals.parseSvg;
|
|
39
|
+
},
|
|
40
|
+
isSvg: function() {
|
|
41
|
+
return _internals.isSvg;
|
|
42
|
+
},
|
|
37
43
|
assert: function() {
|
|
38
44
|
return _utils.assert;
|
|
39
45
|
}
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './error-wrappers';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","assert"],"mappings":"AAAA,gFAAgF;;;;;;;;;;;;IAG9EA,YAAY;eAAZA,uBAAY;;IACZC,eAAe;eAAfA,0BAAe;;IACfC,aAAa;eAAbA,wBAAa;;IACbC,eAAe;eAAfA,0BAAe;;IACfC,kBAAkB;eAAlBA,6BAAkB;;IAClBC,OAAO;eAAPA,kBAAO;;IACPC,KAAK;eAALA,gBAAK;;IACLC,SAAS;eAATA,oBAAS;;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n parseSvg,\n isSvg,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './error-wrappers';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","parseSvg","isSvg","assert"],"mappings":"AAAA,gFAAgF;;;;;;;;;;;;IAG9EA,YAAY;eAAZA,uBAAY;;IACZC,eAAe;eAAfA,0BAAe;;IACfC,aAAa;eAAbA,wBAAa;;IACbC,eAAe;eAAfA,0BAAe;;IACfC,kBAAkB;eAAlBA,6BAAkB;;IAClBC,OAAO;eAAPA,kBAAO;;IACPC,KAAK;eAALA,gBAAK;;IACLC,SAAS;eAATA,oBAAS;;IACTC,QAAQ;eAARA,mBAAQ;;IACRC,KAAK;eAALA,gBAAK;;IAUEC,MAAM;eAANA,aAAM;;;2BATR;uBASgB;qBAET;qBACA;qBACA;qBACA;qBACA"}
|
|
@@ -11,7 +11,21 @@ Object.defineProperty(exports, "createSnapError", {
|
|
|
11
11
|
const _errors = require("../errors");
|
|
12
12
|
function createSnapError(fn) {
|
|
13
13
|
return class SnapJsonRpcError extends _errors.SnapError {
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Create a new `SnapJsonRpcError` from a message and data.
|
|
16
|
+
*
|
|
17
|
+
* @param message - The message to create the error from.
|
|
18
|
+
* @param data - The data to create the error from.
|
|
19
|
+
*/ constructor(message, data){
|
|
20
|
+
if (typeof message === 'object') {
|
|
21
|
+
const error = fn();
|
|
22
|
+
super({
|
|
23
|
+
code: error.code,
|
|
24
|
+
message: error.message,
|
|
25
|
+
data: message
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
15
29
|
const error = fn(message);
|
|
16
30
|
super({
|
|
17
31
|
code: error.code,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/error-wrappers.ts"],"sourcesContent":["import type { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\n\nimport { SnapError } from '../errors';\n\nexport type JsonRpcErrorFunction = typeof rpcErrors.parse;\n\n/**\n * Create a `SnapError` class from an error function from\n * `@metamask/rpc-errors`. This is useful for creating custom error classes\n * which can be thrown by a Snap.\n *\n * The created class will inherit the message, code, and data properties from\n * the error function.\n *\n * @param fn - The error function to create the class from.\n * @returns The created `SnapError` class.\n */\nexport function createSnapError(fn: JsonRpcErrorFunction) {\n return class SnapJsonRpcError extends SnapError {\n constructor(message?: string
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/error-wrappers.ts"],"sourcesContent":["import type { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\n\nimport { SnapError } from '../errors';\n\nexport type JsonRpcErrorFunction = typeof rpcErrors.parse;\n\n/**\n * Create a `SnapError` class from an error function from\n * `@metamask/rpc-errors`. This is useful for creating custom error classes\n * which can be thrown by a Snap.\n *\n * The created class will inherit the message, code, and data properties from\n * the error function.\n *\n * @param fn - The error function to create the class from.\n * @returns The created `SnapError` class.\n */\nexport function createSnapError(fn: JsonRpcErrorFunction) {\n return class SnapJsonRpcError extends SnapError {\n /**\n * Create a new `SnapJsonRpcError` from a message.\n *\n * @param message - The message to create the error from.\n */\n constructor(message?: string);\n\n /**\n * Create a new `SnapJsonRpcError` from data.\n *\n * @param data - The data to create the error from.\n */\n constructor(data?: Record<string, Json>);\n\n /**\n * Create a new `SnapJsonRpcError` from a message and data.\n *\n * @param message - The message to create the error from.\n * @param data - The data to create the error from.\n */\n constructor(\n message?: string | Record<string, Json>,\n data?: Record<string, Json>,\n );\n\n /**\n * Create a new `SnapJsonRpcError` from a message and data.\n *\n * @param message - The message to create the error from.\n * @param data - The data to create the error from.\n */\n constructor(\n message?: string | Record<string, Json>,\n data?: Record<string, Json>,\n ) {\n if (typeof message === 'object') {\n const error = fn();\n super({\n code: error.code,\n message: error.message,\n data: message,\n });\n\n return;\n }\n\n const error = fn(message);\n super({\n code: error.code,\n message: error.message,\n data,\n });\n }\n };\n}\n"],"names":["createSnapError","fn","SnapJsonRpcError","SnapError","constructor","message","data","error","code"],"mappings":";;;;+BAkBgBA;;;eAAAA;;;wBAfU;AAenB,SAASA,gBAAgBC,EAAwB;IACtD,OAAO,MAAMC,yBAAyBC,iBAAS;QA0B7C;;;;;KAKC,GACDC,YACEC,OAAuC,EACvCC,IAA2B,CAC3B;YACA,IAAI,OAAOD,YAAY,UAAU;gBAC/B,MAAME,QAAQN;gBACd,KAAK,CAAC;oBACJO,MAAMD,MAAMC,IAAI;oBAChBH,SAASE,MAAMF,OAAO;oBACtBC,MAAMD;gBACR;gBAEA;YACF;YAEA,MAAME,QAAQN,GAAGI;YACjB,KAAK,CAAC;gBACJG,MAAMD,MAAMC,IAAI;gBAChBH,SAASE,MAAMF,OAAO;gBACtBC;YACF;QACF;IACF;AACF"}
|
|
@@ -6,6 +6,7 @@ _export_star(require("./error-wrappers"), exports);
|
|
|
6
6
|
_export_star(require("./errors"), exports);
|
|
7
7
|
_export_star(require("./helpers"), exports);
|
|
8
8
|
_export_star(require("./structs"), exports);
|
|
9
|
+
_export_star(require("./svg"), exports);
|
|
9
10
|
function _export_star(from, to) {
|
|
10
11
|
Object.keys(from).forEach(function(k) {
|
|
11
12
|
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA"}
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\nexport * from './svg';\n"],"names":[],"mappings":";;;;qBAAc;qBACA;qBACA;qBACA;qBACA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
_export(exports, {
|
|
12
|
+
parseSvg: function() {
|
|
13
|
+
return parseSvg;
|
|
14
|
+
},
|
|
15
|
+
isSvg: function() {
|
|
16
|
+
return isSvg;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const _utils = require("@metamask/utils");
|
|
20
|
+
const _fastxmlparser = require("fast-xml-parser");
|
|
21
|
+
function parseSvg(svg) {
|
|
22
|
+
try {
|
|
23
|
+
const trimmed = svg.trim();
|
|
24
|
+
(0, _utils.assert)(trimmed.length > 0);
|
|
25
|
+
const parser = new _fastxmlparser.XMLParser({
|
|
26
|
+
ignoreAttributes: false,
|
|
27
|
+
parseAttributeValue: true
|
|
28
|
+
});
|
|
29
|
+
const parsed = parser.parse(trimmed, true);
|
|
30
|
+
(0, _utils.assert)((0, _utils.hasProperty)(parsed, 'svg'));
|
|
31
|
+
// Empty SVGs are not returned as objects
|
|
32
|
+
if (!(0, _utils.isObject)(parsed.svg)) {
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
return parsed.svg;
|
|
36
|
+
} catch {
|
|
37
|
+
throw new Error('Snap icon must be a valid SVG.');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function isSvg(svg) {
|
|
41
|
+
try {
|
|
42
|
+
parseSvg(svg);
|
|
43
|
+
return true;
|
|
44
|
+
} catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=svg.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/svg.ts"],"sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { XMLParser } from 'fast-xml-parser';\n\n/**\n * Parse and validate a string as an SVG.\n *\n * @param svg - An SVG string.\n * @returns The SVG, its attributes and children in an object format.\n */\nexport function parseSvg(svg: string) {\n try {\n const trimmed = svg.trim();\n\n assert(trimmed.length > 0);\n\n const parser = new XMLParser({\n ignoreAttributes: false,\n parseAttributeValue: true,\n });\n const parsed = parser.parse(trimmed, true);\n\n assert(hasProperty(parsed, 'svg'));\n\n // Empty SVGs are not returned as objects\n if (!isObject(parsed.svg)) {\n return {};\n }\n\n return parsed.svg;\n } catch {\n throw new Error('Snap icon must be a valid SVG.');\n }\n}\n\n/**\n * Validate that a string is a valid SVG.\n *\n * @param svg - An SVG string.\n * @returns True if the SVG is valid otherwise false.\n */\nexport function isSvg(svg: string) {\n try {\n parseSvg(svg);\n return true;\n } catch {\n return false;\n }\n}\n"],"names":["parseSvg","isSvg","svg","trimmed","trim","assert","length","parser","XMLParser","ignoreAttributes","parseAttributeValue","parsed","parse","hasProperty","isObject","Error"],"mappings":";;;;;;;;;;;IASgBA,QAAQ;eAARA;;IA+BAC,KAAK;eAALA;;;uBAxC8B;+BACpB;AAQnB,SAASD,SAASE,GAAW;IAClC,IAAI;QACF,MAAMC,UAAUD,IAAIE,IAAI;QAExBC,IAAAA,aAAM,EAACF,QAAQG,MAAM,GAAG;QAExB,MAAMC,SAAS,IAAIC,wBAAS,CAAC;YAC3BC,kBAAkB;YAClBC,qBAAqB;QACvB;QACA,MAAMC,SAASJ,OAAOK,KAAK,CAACT,SAAS;QAErCE,IAAAA,aAAM,EAACQ,IAAAA,kBAAW,EAACF,QAAQ;QAE3B,yCAAyC;QACzC,IAAI,CAACG,IAAAA,eAAQ,EAACH,OAAOT,GAAG,GAAG;YACzB,OAAO,CAAC;QACV;QAEA,OAAOS,OAAOT,GAAG;IACnB,EAAE,OAAM;QACN,MAAM,IAAIa,MAAM;IAClB;AACF;AAQO,SAASd,MAAMC,GAAW;IAC/B,IAAI;QACFF,SAASE;QACT,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF"}
|
|
@@ -19,18 +19,13 @@ _export(exports, {
|
|
|
19
19
|
return image;
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
|
-
const _issvg = /*#__PURE__*/ _interop_require_default(require("is-svg"));
|
|
23
22
|
const _superstruct = require("superstruct");
|
|
23
|
+
const _internals = require("../../internals");
|
|
24
24
|
const _builder = require("../builder");
|
|
25
25
|
const _nodes = require("../nodes");
|
|
26
|
-
function _interop_require_default(obj) {
|
|
27
|
-
return obj && obj.__esModule ? obj : {
|
|
28
|
-
default: obj
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
26
|
function svg() {
|
|
32
27
|
return (0, _superstruct.refine)((0, _superstruct.string)(), 'SVG', (value)=>{
|
|
33
|
-
if (!(0,
|
|
28
|
+
if (!(0, _internals.isSvg)(value)) {
|
|
34
29
|
return 'Value is not a valid SVG.';
|
|
35
30
|
}
|
|
36
31
|
return true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/components/image.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/components/image.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, literal, object, refine, string } from 'superstruct';\n\nimport { isSvg } from '../../internals';\nimport { createBuilder } from '../builder';\nimport { NodeStruct, NodeType } from '../nodes';\n\n/**\n * Get a Struct that validates a string as a valid SVG.\n *\n * @returns A Struct that validates a string as a valid SVG.\n * @internal\n */\nexport function svg() {\n return refine(string(), 'SVG', (value) => {\n if (!isSvg(value)) {\n return 'Value is not a valid SVG.';\n }\n\n return true;\n });\n}\n\nexport const ImageStruct = assign(\n NodeStruct,\n object({\n type: literal(NodeType.Image),\n value: svg(),\n }),\n);\n\n/**\n * An image node, that renders an SVG image.\n *\n * @property type - The type of the node. Must be the string `image`.\n * @property value - The SVG image to be rendered.\n */\nexport type Image = Infer<typeof ImageStruct>;\n\n/**\n * Create an {@link Image} node.\n *\n * @param args - The node arguments. This can either be a string, or an object\n * with the `value` property.\n * @param args.value - The SVG image to be rendered. Must be a valid SVG string.\n * @returns The image node as object. Other image formats are supported by\n * embedding them as data URLs in the SVG.\n * @example\n * const node = image({ value: '<svg />' });\n * const node = image('<svg />');\n */\nexport const image = createBuilder(NodeType.Image, ImageStruct, ['value']);\n"],"names":["svg","ImageStruct","image","refine","string","value","isSvg","assign","NodeStruct","object","type","literal","NodeType","Image","createBuilder"],"mappings":";;;;;;;;;;;IAagBA,GAAG;eAAHA;;IAUHC,WAAW;eAAXA;;IA4BAC,KAAK;eAALA;;;6BAlD2C;2BAElC;yBACQ;uBACO;AAQ9B,SAASF;IACd,OAAOG,IAAAA,mBAAM,EAACC,IAAAA,mBAAM,KAAI,OAAO,CAACC;QAC9B,IAAI,CAACC,IAAAA,gBAAK,EAACD,QAAQ;YACjB,OAAO;QACT;QAEA,OAAO;IACT;AACF;AAEO,MAAMJ,cAAcM,IAAAA,mBAAM,EAC/BC,iBAAU,EACVC,IAAAA,mBAAM,EAAC;IACLC,MAAMC,IAAAA,oBAAO,EAACC,eAAQ,CAACC,KAAK;IAC5BR,OAAOL;AACT;AAuBK,MAAME,QAAQY,IAAAA,sBAAa,EAACF,eAAQ,CAACC,KAAK,EAAEZ,aAAa;IAAC;CAAQ"}
|
package/dist/esm/errors.js
CHANGED
|
@@ -94,7 +94,9 @@ var _code = /*#__PURE__*/ new WeakMap(), _message = /*#__PURE__*/ new WeakMap(),
|
|
|
94
94
|
code: this.code,
|
|
95
95
|
message: this.message,
|
|
96
96
|
stack: this.stack,
|
|
97
|
-
|
|
97
|
+
...this.data ? {
|
|
98
|
+
data: this.data
|
|
99
|
+
} : {}
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
};
|
|
@@ -139,10 +141,13 @@ var _code = /*#__PURE__*/ new WeakMap(), _message = /*#__PURE__*/ new WeakMap(),
|
|
|
139
141
|
});
|
|
140
142
|
_class_private_field_set(this, _message, message);
|
|
141
143
|
_class_private_field_set(this, _code, getErrorCode(error));
|
|
142
|
-
|
|
144
|
+
const mergedData = {
|
|
143
145
|
...getErrorData(error),
|
|
144
146
|
...data
|
|
145
|
-
}
|
|
147
|
+
};
|
|
148
|
+
if (Object.keys(mergedData).length > 0) {
|
|
149
|
+
_class_private_field_set(this, _data, mergedData);
|
|
150
|
+
}
|
|
146
151
|
_class_private_field_set(this, _stack, super.stack);
|
|
147
152
|
}
|
|
148
153
|
}
|
package/dist/esm/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import type { Json, JsonRpcError } from '@metamask/utils';\n\nimport {\n getErrorCode,\n getErrorData,\n getErrorMessage,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data
|
|
1
|
+
{"version":3,"sources":["../../src/errors.ts"],"sourcesContent":["import type { Json, JsonRpcError } from '@metamask/utils';\n\nimport {\n getErrorCode,\n getErrorData,\n getErrorMessage,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n} from './internals';\n\n/**\n * A generic error which can be thrown by a Snap, without it causing the Snap to\n * crash.\n */\nexport class SnapError extends Error {\n readonly #code: number;\n\n readonly #message: string;\n\n readonly #data?: Record<string, Json>;\n\n readonly #stack?: string;\n\n /**\n * Create a new `SnapError`.\n *\n * @param error - The error to create the `SnapError` from. If this is a\n * `string`, it will be used as the error message. If this is an `Error`, its\n * `message` property will be used as the error message. If this is a\n * `JsonRpcError`, its `message` property will be used as the error message\n * and its `code` property will be used as the error code. Otherwise, the\n * error will be converted to a string and used as the error message.\n * @param data - Additional data to include in the error. This will be merged\n * with the error data, if any.\n */\n constructor(\n error: string | Error | JsonRpcError,\n data: Record<string, Json> = {},\n ) {\n const message = getErrorMessage(error);\n super(message);\n\n this.#message = message;\n this.#code = getErrorCode(error);\n\n const mergedData = { ...getErrorData(error), ...data };\n if (Object.keys(mergedData).length > 0) {\n this.#data = mergedData;\n }\n\n this.#stack = super.stack;\n }\n\n /**\n * The error name.\n *\n * @returns The error name.\n */\n get name() {\n return 'SnapError';\n }\n\n /**\n * The error code.\n *\n * @returns The error code.\n */\n get code() {\n return this.#code;\n }\n\n /**\n * The error message.\n *\n * @returns The error message.\n */\n // This line is covered, but Jest doesn't pick it up for some reason.\n /* istanbul ignore next */\n get message() {\n return this.#message;\n }\n\n /**\n * Additional data for the error.\n *\n * @returns Additional data for the error.\n */\n get data() {\n return this.#data;\n }\n\n /**\n * The error stack.\n *\n * @returns The error stack.\n */\n // This line is covered, but Jest doesn't pick it up for some reason.\n /* istanbul ignore next */\n get stack() {\n return this.#stack;\n }\n\n /**\n * Convert the error to a JSON object.\n *\n * @returns The JSON object.\n */\n toJSON(): SerializedSnapError {\n return {\n code: SNAP_ERROR_CODE,\n message: SNAP_ERROR_MESSAGE,\n data: {\n cause: {\n code: this.code,\n message: this.message,\n stack: this.stack,\n ...(this.data ? { data: this.data } : {}),\n },\n },\n };\n }\n\n /**\n * Serialize the error to a JSON object. This is called by\n * `@metamask/rpc-errors` when serializing the error.\n *\n * @returns The JSON object.\n */\n serialize() {\n return this.toJSON();\n }\n}\n\n/**\n * A serialized {@link SnapError}. It's JSON-serializable, so it can be sent\n * over the RPC. The original error is wrapped in the `cause` property.\n *\n * @property code - The error code. This is always `-31002`.\n * @property message - The error message. This is always `'Snap Error'`.\n * @property data - The error data.\n * @property data.cause - The cause of the error.\n * @property data.cause.code - The error code.\n * @property data.cause.message - The error message.\n * @property data.cause.stack - The error stack.\n * @property data.cause.data - Additional data for the error.\n * @see SnapError\n */\nexport type SerializedSnapError = {\n code: typeof SNAP_ERROR_CODE;\n message: typeof SNAP_ERROR_MESSAGE;\n data: {\n cause: JsonRpcError;\n };\n};\n"],"names":["getErrorCode","getErrorData","getErrorMessage","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","SnapError","Error","name","code","message","data","stack","toJSON","cause","serialize","constructor","error","mergedData","Object","keys","length"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,SACEA,YAAY,EACZC,YAAY,EACZC,eAAe,EACfC,eAAe,EACfC,kBAAkB,QACb,cAAc;IAOV,qCAEA,wCAEA,qCAEA;AAXX;;;CAGC,GACD,OAAO,MAAMC,kBAAkBC;IAuC7B;;;;GAIC,GACD,IAAIC,OAAO;QACT,OAAO;IACT;IAEA;;;;GAIC,GACD,IAAIC,OAAO;QACT,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,qEAAqE;IACrE,wBAAwB,GACxB,IAAIC,UAAU;QACZ,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,IAAIC,OAAO;QACT,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACD,qEAAqE;IACrE,wBAAwB,GACxB,IAAIC,QAAQ;QACV,gCAAO,IAAI,EAAEA;IACf;IAEA;;;;GAIC,GACDC,SAA8B;QAC5B,OAAO;YACLJ,MAAML;YACNM,SAASL;YACTM,MAAM;gBACJG,OAAO;oBACLL,MAAM,IAAI,CAACA,IAAI;oBACfC,SAAS,IAAI,CAACA,OAAO;oBACrBE,OAAO,IAAI,CAACA,KAAK;oBACjB,GAAI,IAAI,CAACD,IAAI,GAAG;wBAAEA,MAAM,IAAI,CAACA,IAAI;oBAAC,IAAI,CAAC,CAAC;gBAC1C;YACF;QACF;IACF;IAEA;;;;;GAKC,GACDI,YAAY;QACV,OAAO,IAAI,CAACF,MAAM;IACpB;IA3GA;;;;;;;;;;;GAWC,GACDG,YACEC,KAAoC,EACpCN,OAA6B,CAAC,CAAC,CAC/B;QACA,MAAMD,UAAUP,gBAAgBc;QAChC,KAAK,CAACP;QAzBR,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;QAEA,gCAAS;;mBAAT,KAAA;;uCAqBQA,UAAUA;uCACVD,OAAOR,aAAagB;QAE1B,MAAMC,aAAa;YAAE,GAAGhB,aAAae,MAAM;YAAE,GAAGN,IAAI;QAAC;QACrD,IAAIQ,OAAOC,IAAI,CAACF,YAAYG,MAAM,GAAG,GAAG;2CAChCV,OAAOO;QACf;uCAEMN,QAAQ,KAAK,CAACA;IACtB;AAgFF"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Only internals that are used by other Snaps packages should be exported here.
|
|
2
|
-
export { getErrorData, getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE, literal, union, enumValue } from './internals';
|
|
2
|
+
export { getErrorData, getErrorMessage, getErrorStack, SNAP_ERROR_CODE, SNAP_ERROR_MESSAGE, literal, union, enumValue, parseSvg, isSvg } from './internals';
|
|
3
3
|
export { assert } from '@metamask/utils';
|
|
4
4
|
export * from './errors';
|
|
5
5
|
export * from './error-wrappers';
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './error-wrappers';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","assert"],"mappings":"AAAA,gFAAgF;AAEhF,SACEA,YAAY,EACZC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,EAClBC,OAAO,EACPC,KAAK,EACLC,SAAS,
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["// Only internals that are used by other Snaps packages should be exported here.\nexport type { EnumToUnion } from './internals';\nexport {\n getErrorData,\n getErrorMessage,\n getErrorStack,\n SNAP_ERROR_CODE,\n SNAP_ERROR_MESSAGE,\n literal,\n union,\n enumValue,\n parseSvg,\n isSvg,\n} from './internals';\n\n// Re-exported from `@metamask/utils` for convenience.\nexport type {\n Json,\n JsonRpcError,\n JsonRpcRequest,\n JsonRpcParams,\n} from '@metamask/utils';\nexport { assert } from '@metamask/utils';\n\nexport * from './errors';\nexport * from './error-wrappers';\nexport * from './images';\nexport * from './types';\nexport * from './ui';\n"],"names":["getErrorData","getErrorMessage","getErrorStack","SNAP_ERROR_CODE","SNAP_ERROR_MESSAGE","literal","union","enumValue","parseSvg","isSvg","assert"],"mappings":"AAAA,gFAAgF;AAEhF,SACEA,YAAY,EACZC,eAAe,EACfC,aAAa,EACbC,eAAe,EACfC,kBAAkB,EAClBC,OAAO,EACPC,KAAK,EACLC,SAAS,EACTC,QAAQ,EACRC,KAAK,QACA,cAAc;AASrB,SAASC,MAAM,QAAQ,kBAAkB;AAEzC,cAAc,WAAW;AACzB,cAAc,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,UAAU;AACxB,cAAc,OAAO"}
|
|
@@ -11,7 +11,21 @@ import { SnapError } from '../errors';
|
|
|
11
11
|
* @returns The created `SnapError` class.
|
|
12
12
|
*/ export function createSnapError(fn) {
|
|
13
13
|
return class SnapJsonRpcError extends SnapError {
|
|
14
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Create a new `SnapJsonRpcError` from a message and data.
|
|
16
|
+
*
|
|
17
|
+
* @param message - The message to create the error from.
|
|
18
|
+
* @param data - The data to create the error from.
|
|
19
|
+
*/ constructor(message, data){
|
|
20
|
+
if (typeof message === 'object') {
|
|
21
|
+
const error = fn();
|
|
22
|
+
super({
|
|
23
|
+
code: error.code,
|
|
24
|
+
message: error.message,
|
|
25
|
+
data: message
|
|
26
|
+
});
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
15
29
|
const error = fn(message);
|
|
16
30
|
super({
|
|
17
31
|
code: error.code,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/error-wrappers.ts"],"sourcesContent":["import type { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\n\nimport { SnapError } from '../errors';\n\nexport type JsonRpcErrorFunction = typeof rpcErrors.parse;\n\n/**\n * Create a `SnapError` class from an error function from\n * `@metamask/rpc-errors`. This is useful for creating custom error classes\n * which can be thrown by a Snap.\n *\n * The created class will inherit the message, code, and data properties from\n * the error function.\n *\n * @param fn - The error function to create the class from.\n * @returns The created `SnapError` class.\n */\nexport function createSnapError(fn: JsonRpcErrorFunction) {\n return class SnapJsonRpcError extends SnapError {\n constructor(message?: string
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/error-wrappers.ts"],"sourcesContent":["import type { rpcErrors } from '@metamask/rpc-errors';\nimport type { Json } from '@metamask/utils';\n\nimport { SnapError } from '../errors';\n\nexport type JsonRpcErrorFunction = typeof rpcErrors.parse;\n\n/**\n * Create a `SnapError` class from an error function from\n * `@metamask/rpc-errors`. This is useful for creating custom error classes\n * which can be thrown by a Snap.\n *\n * The created class will inherit the message, code, and data properties from\n * the error function.\n *\n * @param fn - The error function to create the class from.\n * @returns The created `SnapError` class.\n */\nexport function createSnapError(fn: JsonRpcErrorFunction) {\n return class SnapJsonRpcError extends SnapError {\n /**\n * Create a new `SnapJsonRpcError` from a message.\n *\n * @param message - The message to create the error from.\n */\n constructor(message?: string);\n\n /**\n * Create a new `SnapJsonRpcError` from data.\n *\n * @param data - The data to create the error from.\n */\n constructor(data?: Record<string, Json>);\n\n /**\n * Create a new `SnapJsonRpcError` from a message and data.\n *\n * @param message - The message to create the error from.\n * @param data - The data to create the error from.\n */\n constructor(\n message?: string | Record<string, Json>,\n data?: Record<string, Json>,\n );\n\n /**\n * Create a new `SnapJsonRpcError` from a message and data.\n *\n * @param message - The message to create the error from.\n * @param data - The data to create the error from.\n */\n constructor(\n message?: string | Record<string, Json>,\n data?: Record<string, Json>,\n ) {\n if (typeof message === 'object') {\n const error = fn();\n super({\n code: error.code,\n message: error.message,\n data: message,\n });\n\n return;\n }\n\n const error = fn(message);\n super({\n code: error.code,\n message: error.message,\n data,\n });\n }\n };\n}\n"],"names":["SnapError","createSnapError","fn","SnapJsonRpcError","constructor","message","data","error","code"],"mappings":"AAGA,SAASA,SAAS,QAAQ,YAAY;AAItC;;;;;;;;;;CAUC,GACD,OAAO,SAASC,gBAAgBC,EAAwB;IACtD,OAAO,MAAMC,yBAAyBH;QA0BpC;;;;;KAKC,GACDI,YACEC,OAAuC,EACvCC,IAA2B,CAC3B;YACA,IAAI,OAAOD,YAAY,UAAU;gBAC/B,MAAME,QAAQL;gBACd,KAAK,CAAC;oBACJM,MAAMD,MAAMC,IAAI;oBAChBH,SAASE,MAAMF,OAAO;oBACtBC,MAAMD;gBACR;gBAEA;YACF;YAEA,MAAME,QAAQL,GAAGG;YACjB,KAAK,CAAC;gBACJG,MAAMD,MAAMC,IAAI;gBAChBH,SAASE,MAAMF,OAAO;gBACtBC;YACF;QACF;IACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\n"],"names":[],"mappings":"AAAA,cAAc,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,YAAY;AAC1B,cAAc,YAAY"}
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/index.ts"],"sourcesContent":["export * from './error-wrappers';\nexport * from './errors';\nexport * from './helpers';\nexport * from './structs';\nexport * from './svg';\n"],"names":[],"mappings":"AAAA,cAAc,mBAAmB;AACjC,cAAc,WAAW;AACzB,cAAc,YAAY;AAC1B,cAAc,YAAY;AAC1B,cAAc,QAAQ"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { assert, hasProperty, isObject } from '@metamask/utils';
|
|
2
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
3
|
+
/**
|
|
4
|
+
* Parse and validate a string as an SVG.
|
|
5
|
+
*
|
|
6
|
+
* @param svg - An SVG string.
|
|
7
|
+
* @returns The SVG, its attributes and children in an object format.
|
|
8
|
+
*/ export function parseSvg(svg) {
|
|
9
|
+
try {
|
|
10
|
+
const trimmed = svg.trim();
|
|
11
|
+
assert(trimmed.length > 0);
|
|
12
|
+
const parser = new XMLParser({
|
|
13
|
+
ignoreAttributes: false,
|
|
14
|
+
parseAttributeValue: true
|
|
15
|
+
});
|
|
16
|
+
const parsed = parser.parse(trimmed, true);
|
|
17
|
+
assert(hasProperty(parsed, 'svg'));
|
|
18
|
+
// Empty SVGs are not returned as objects
|
|
19
|
+
if (!isObject(parsed.svg)) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
return parsed.svg;
|
|
23
|
+
} catch {
|
|
24
|
+
throw new Error('Snap icon must be a valid SVG.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Validate that a string is a valid SVG.
|
|
29
|
+
*
|
|
30
|
+
* @param svg - An SVG string.
|
|
31
|
+
* @returns True if the SVG is valid otherwise false.
|
|
32
|
+
*/ export function isSvg(svg) {
|
|
33
|
+
try {
|
|
34
|
+
parseSvg(svg);
|
|
35
|
+
return true;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=svg.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/internals/svg.ts"],"sourcesContent":["import { assert, hasProperty, isObject } from '@metamask/utils';\nimport { XMLParser } from 'fast-xml-parser';\n\n/**\n * Parse and validate a string as an SVG.\n *\n * @param svg - An SVG string.\n * @returns The SVG, its attributes and children in an object format.\n */\nexport function parseSvg(svg: string) {\n try {\n const trimmed = svg.trim();\n\n assert(trimmed.length > 0);\n\n const parser = new XMLParser({\n ignoreAttributes: false,\n parseAttributeValue: true,\n });\n const parsed = parser.parse(trimmed, true);\n\n assert(hasProperty(parsed, 'svg'));\n\n // Empty SVGs are not returned as objects\n if (!isObject(parsed.svg)) {\n return {};\n }\n\n return parsed.svg;\n } catch {\n throw new Error('Snap icon must be a valid SVG.');\n }\n}\n\n/**\n * Validate that a string is a valid SVG.\n *\n * @param svg - An SVG string.\n * @returns True if the SVG is valid otherwise false.\n */\nexport function isSvg(svg: string) {\n try {\n parseSvg(svg);\n return true;\n } catch {\n return false;\n }\n}\n"],"names":["assert","hasProperty","isObject","XMLParser","parseSvg","svg","trimmed","trim","length","parser","ignoreAttributes","parseAttributeValue","parsed","parse","Error","isSvg"],"mappings":"AAAA,SAASA,MAAM,EAAEC,WAAW,EAAEC,QAAQ,QAAQ,kBAAkB;AAChE,SAASC,SAAS,QAAQ,kBAAkB;AAE5C;;;;;CAKC,GACD,OAAO,SAASC,SAASC,GAAW;IAClC,IAAI;QACF,MAAMC,UAAUD,IAAIE,IAAI;QAExBP,OAAOM,QAAQE,MAAM,GAAG;QAExB,MAAMC,SAAS,IAAIN,UAAU;YAC3BO,kBAAkB;YAClBC,qBAAqB;QACvB;QACA,MAAMC,SAASH,OAAOI,KAAK,CAACP,SAAS;QAErCN,OAAOC,YAAYW,QAAQ;QAE3B,yCAAyC;QACzC,IAAI,CAACV,SAASU,OAAOP,GAAG,GAAG;YACzB,OAAO,CAAC;QACV;QAEA,OAAOO,OAAOP,GAAG;IACnB,EAAE,OAAM;QACN,MAAM,IAAIS,MAAM;IAClB;AACF;AAEA;;;;;CAKC,GACD,OAAO,SAASC,MAAMV,GAAW;IAC/B,IAAI;QACFD,SAASC;QACT,OAAO;IACT,EAAE,OAAM;QACN,OAAO;IACT;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/ui/components/image.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../../src/ui/components/image.ts"],"sourcesContent":["import type { Infer } from 'superstruct';\nimport { assign, literal, object, refine, string } from 'superstruct';\n\nimport { isSvg } from '../../internals';\nimport { createBuilder } from '../builder';\nimport { NodeStruct, NodeType } from '../nodes';\n\n/**\n * Get a Struct that validates a string as a valid SVG.\n *\n * @returns A Struct that validates a string as a valid SVG.\n * @internal\n */\nexport function svg() {\n return refine(string(), 'SVG', (value) => {\n if (!isSvg(value)) {\n return 'Value is not a valid SVG.';\n }\n\n return true;\n });\n}\n\nexport const ImageStruct = assign(\n NodeStruct,\n object({\n type: literal(NodeType.Image),\n value: svg(),\n }),\n);\n\n/**\n * An image node, that renders an SVG image.\n *\n * @property type - The type of the node. Must be the string `image`.\n * @property value - The SVG image to be rendered.\n */\nexport type Image = Infer<typeof ImageStruct>;\n\n/**\n * Create an {@link Image} node.\n *\n * @param args - The node arguments. This can either be a string, or an object\n * with the `value` property.\n * @param args.value - The SVG image to be rendered. Must be a valid SVG string.\n * @returns The image node as object. Other image formats are supported by\n * embedding them as data URLs in the SVG.\n * @example\n * const node = image({ value: '<svg />' });\n * const node = image('<svg />');\n */\nexport const image = createBuilder(NodeType.Image, ImageStruct, ['value']);\n"],"names":["assign","literal","object","refine","string","isSvg","createBuilder","NodeStruct","NodeType","svg","value","ImageStruct","type","Image","image"],"mappings":"AACA,SAASA,MAAM,EAAEC,OAAO,EAAEC,MAAM,EAAEC,MAAM,EAAEC,MAAM,QAAQ,cAAc;AAEtE,SAASC,KAAK,QAAQ,kBAAkB;AACxC,SAASC,aAAa,QAAQ,aAAa;AAC3C,SAASC,UAAU,EAAEC,QAAQ,QAAQ,WAAW;AAEhD;;;;;CAKC,GACD,OAAO,SAASC;IACd,OAAON,OAAOC,UAAU,OAAO,CAACM;QAC9B,IAAI,CAACL,MAAMK,QAAQ;YACjB,OAAO;QACT;QAEA,OAAO;IACT;AACF;AAEA,OAAO,MAAMC,cAAcX,OACzBO,YACAL,OAAO;IACLU,MAAMX,QAAQO,SAASK,KAAK;IAC5BH,OAAOD;AACT,IACA;AAUF;;;;;;;;;;;CAWC,GACD,OAAO,MAAMK,QAAQR,cAAcE,SAASK,KAAK,EAAEF,aAAa;IAAC;CAAQ,EAAE"}
|