@metamask/snaps-jest 4.0.1 → 5.0.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 +16 -1
- package/README.md +66 -179
- package/dist/cjs/environment.js +21 -82
- package/dist/cjs/environment.js.map +1 -1
- package/dist/cjs/helpers.js +118 -43
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/internals/environment.js +1 -1
- package/dist/cjs/internals/environment.js.map +1 -1
- package/dist/cjs/internals/index.js +1 -4
- package/dist/cjs/internals/index.js.map +1 -1
- package/dist/cjs/internals/request.js +42 -94
- package/dist/cjs/internals/request.js.map +1 -1
- package/dist/cjs/internals/server.js +0 -4
- package/dist/cjs/internals/server.js.map +1 -1
- package/dist/cjs/internals/simulation/constants.js +29 -0
- package/dist/cjs/internals/simulation/constants.js.map +1 -0
- package/dist/cjs/internals/simulation/controllers.js +95 -0
- package/dist/cjs/internals/simulation/controllers.js.map +1 -0
- package/dist/cjs/internals/simulation/files.js +22 -0
- package/dist/cjs/internals/simulation/files.js.map +1 -0
- package/dist/cjs/internals/simulation/index.js +24 -0
- package/dist/cjs/internals/simulation/index.js.map +1 -0
- package/dist/cjs/internals/simulation/interface.js +98 -0
- package/dist/cjs/internals/simulation/interface.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/constants.js +84 -0
- package/dist/cjs/internals/simulation/methods/constants.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/encryption.js +33 -0
- package/dist/cjs/internals/simulation/methods/hooks/encryption.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/get-locale.js +17 -0
- package/dist/cjs/internals/simulation/methods/hooks/get-locale.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/index.js +25 -0
- package/dist/cjs/internals/simulation/methods/hooks/index.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/interface.js +26 -0
- package/dist/cjs/internals/simulation/methods/hooks/interface.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/notifications.js +66 -0
- package/dist/cjs/internals/simulation/methods/hooks/notifications.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/show-dialog.js +43 -0
- package/dist/cjs/internals/simulation/methods/hooks/show-dialog.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/hooks/state.js +80 -0
- package/dist/cjs/internals/simulation/methods/hooks/state.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/index.js +20 -0
- package/dist/cjs/internals/simulation/methods/index.js.map +1 -0
- package/dist/cjs/internals/simulation/methods/specifications.js +81 -0
- package/dist/cjs/internals/simulation/methods/specifications.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/engine.js +31 -0
- package/dist/cjs/internals/simulation/middleware/engine.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/index.js +20 -0
- package/dist/cjs/internals/simulation/middleware/index.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/accounts.js +30 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/accounts.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/index.js +20 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/index.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/middleware.js +37 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/middleware.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/provider-state.js +23 -0
- package/dist/cjs/internals/simulation/middleware/internal-methods/provider-state.js.map +1 -0
- package/dist/cjs/internals/simulation/middleware/mock.js +23 -0
- package/dist/cjs/internals/simulation/middleware/mock.js.map +1 -0
- package/dist/cjs/internals/simulation/options.js +24 -0
- package/dist/cjs/internals/simulation/options.js.map +1 -0
- package/dist/cjs/internals/simulation/simulation.js +124 -0
- package/dist/cjs/internals/simulation/simulation.js.map +1 -0
- package/dist/cjs/internals/simulation/store/index.js +23 -0
- package/dist/cjs/internals/simulation/store/index.js.map +1 -0
- package/dist/cjs/internals/simulation/store/mocks.js +52 -0
- package/dist/cjs/internals/simulation/store/mocks.js.map +1 -0
- package/dist/cjs/internals/simulation/store/notifications.js +52 -0
- package/dist/cjs/internals/simulation/store/notifications.js.map +1 -0
- package/dist/cjs/internals/simulation/store/state.js +64 -0
- package/dist/cjs/internals/simulation/store/state.js.map +1 -0
- package/dist/cjs/internals/simulation/store/store.js +57 -0
- package/dist/cjs/internals/simulation/store/store.js.map +1 -0
- package/dist/cjs/internals/simulation/store/ui.js +48 -0
- package/dist/cjs/internals/simulation/store/ui.js.map +1 -0
- package/dist/cjs/internals/structs.js +43 -0
- package/dist/cjs/internals/structs.js.map +1 -1
- package/dist/cjs/matchers.js +5 -2
- package/dist/cjs/matchers.js.map +1 -1
- package/dist/cjs/options.js +0 -6
- package/dist/cjs/options.js.map +1 -1
- package/dist/esm/environment.js +22 -83
- package/dist/esm/environment.js.map +1 -1
- package/dist/esm/helpers.js +127 -46
- package/dist/esm/helpers.js.map +1 -1
- package/dist/esm/internals/environment.js +1 -1
- package/dist/esm/internals/environment.js.map +1 -1
- package/dist/esm/internals/index.js +1 -5
- package/dist/esm/internals/index.js.map +1 -1
- package/dist/esm/internals/request.js +61 -104
- package/dist/esm/internals/request.js.map +1 -1
- package/dist/esm/internals/server.js +1 -5
- package/dist/esm/internals/server.js.map +1 -1
- package/dist/esm/internals/simulation/constants.js +12 -0
- package/dist/esm/internals/simulation/constants.js.map +1 -0
- package/dist/esm/internals/simulation/controllers.js +90 -0
- package/dist/esm/internals/simulation/controllers.js.map +1 -0
- package/dist/esm/internals/simulation/files.js +19 -0
- package/dist/esm/internals/simulation/files.js.map +1 -0
- package/dist/esm/internals/simulation/index.js +7 -0
- package/dist/esm/internals/simulation/index.js.map +1 -0
- package/dist/esm/internals/simulation/interface.js +95 -0
- package/dist/esm/internals/simulation/interface.js.map +1 -0
- package/dist/esm/internals/simulation/methods/constants.js +69 -0
- package/dist/esm/internals/simulation/methods/constants.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/encryption.js +39 -0
- package/dist/esm/internals/simulation/methods/hooks/encryption.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/get-locale.js +13 -0
- package/dist/esm/internals/simulation/methods/hooks/get-locale.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/index.js +8 -0
- package/dist/esm/internals/simulation/methods/hooks/index.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/interface.js +18 -0
- package/dist/esm/internals/simulation/methods/hooks/interface.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/notifications.js +58 -0
- package/dist/esm/internals/simulation/methods/hooks/notifications.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/show-dialog.js +38 -0
- package/dist/esm/internals/simulation/methods/hooks/show-dialog.js.map +1 -0
- package/dist/esm/internals/simulation/methods/hooks/state.js +74 -0
- package/dist/esm/internals/simulation/methods/hooks/state.js.map +1 -0
- package/dist/esm/internals/simulation/methods/index.js +3 -0
- package/dist/esm/internals/simulation/methods/index.js.map +1 -0
- package/dist/esm/internals/simulation/methods/specifications.js +84 -0
- package/dist/esm/internals/simulation/methods/specifications.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/engine.js +33 -0
- package/dist/esm/internals/simulation/middleware/engine.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/index.js +3 -0
- package/dist/esm/internals/simulation/middleware/index.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/accounts.js +31 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/accounts.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/index.js +3 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/index.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/middleware.js +37 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/middleware.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/provider-state.js +23 -0
- package/dist/esm/internals/simulation/middleware/internal-methods/provider-state.js.map +1 -0
- package/dist/esm/internals/simulation/middleware/mock.js +18 -0
- package/dist/esm/internals/simulation/middleware/mock.js.map +1 -0
- package/dist/esm/internals/simulation/options.js +20 -0
- package/dist/esm/internals/simulation/options.js.map +1 -0
- package/dist/esm/internals/simulation/simulation.js +128 -0
- package/dist/esm/internals/simulation/simulation.js.map +1 -0
- package/dist/esm/internals/simulation/store/index.js +6 -0
- package/dist/esm/internals/simulation/store/index.js.map +1 -0
- package/dist/esm/internals/simulation/store/mocks.js +32 -0
- package/dist/esm/internals/simulation/store/mocks.js.map +1 -0
- package/dist/esm/internals/simulation/store/notifications.js +30 -0
- package/dist/esm/internals/simulation/store/notifications.js.map +1 -0
- package/dist/esm/internals/simulation/store/state.js +47 -0
- package/dist/esm/internals/simulation/store/state.js.map +1 -0
- package/dist/esm/internals/simulation/store/store.js +50 -0
- package/dist/esm/internals/simulation/store/store.js.map +1 -0
- package/dist/esm/internals/simulation/store/ui.js +21 -0
- package/dist/esm/internals/simulation/store/ui.js.map +1 -0
- package/dist/esm/internals/structs.js +38 -1
- package/dist/esm/internals/structs.js.map +1 -1
- package/dist/esm/matchers.js +5 -2
- package/dist/esm/matchers.js.map +1 -1
- package/dist/esm/options.js +1 -7
- package/dist/esm/options.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/types/environment.d.ts +14 -15
- package/dist/types/helpers.d.ts +64 -3
- package/dist/types/internals/index.d.ts +1 -4
- package/dist/types/internals/request.d.ts +38 -74
- package/dist/types/internals/simulation/constants.d.ts +13 -0
- package/dist/types/internals/simulation/controllers.d.ts +40 -0
- package/dist/types/internals/simulation/files.d.ts +11 -0
- package/dist/types/internals/simulation/index.d.ts +5 -0
- package/dist/types/internals/simulation/interface.d.ts +25 -0
- package/dist/types/internals/simulation/methods/constants.d.ts +10 -0
- package/dist/types/internals/simulation/methods/hooks/encryption.d.ts +29 -0
- package/dist/types/internals/simulation/methods/hooks/get-locale.d.ts +9 -0
- package/dist/types/internals/simulation/methods/hooks/index.d.ts +6 -0
- package/dist/types/internals/simulation/methods/hooks/interface.d.ts +16 -0
- package/dist/types/internals/simulation/methods/hooks/notifications.d.ts +16 -0
- package/dist/types/internals/simulation/methods/hooks/show-dialog.d.ts +9 -0
- package/dist/types/internals/simulation/methods/hooks/state.d.ts +22 -0
- package/dist/types/internals/simulation/methods/index.d.ts +1 -0
- package/dist/types/internals/simulation/methods/specifications.d.ts +56 -0
- package/dist/types/internals/simulation/middleware/engine.d.ts +26 -0
- package/dist/types/internals/simulation/middleware/index.d.ts +1 -0
- package/dist/types/internals/simulation/middleware/internal-methods/accounts.d.ts +18 -0
- package/dist/types/internals/simulation/middleware/internal-methods/index.d.ts +1 -0
- package/dist/types/internals/simulation/middleware/internal-methods/middleware.d.ts +22 -0
- package/dist/types/internals/simulation/middleware/internal-methods/provider-state.d.ts +14 -0
- package/dist/types/internals/simulation/middleware/mock.d.ts +10 -0
- package/dist/types/internals/simulation/options.d.ts +37 -0
- package/dist/types/internals/simulation/simulation.d.ts +98 -0
- package/dist/types/internals/simulation/store/index.d.ts +4 -0
- package/dist/types/internals/simulation/store/mocks.d.ts +35 -0
- package/dist/types/internals/simulation/store/notifications.d.ts +44 -0
- package/dist/types/internals/simulation/store/state.d.ts +55 -0
- package/dist/types/internals/simulation/store/store.d.ts +22 -0
- package/dist/types/internals/simulation/store/ui.d.ts +25 -0
- package/dist/types/internals/structs.d.ts +153 -0
- package/dist/types/options.d.ts +3 -35
- package/dist/types/types.d.ts +88 -41
- package/package.json +21 -12
- package/dist/cjs/internals/interface.js +0 -103
- package/dist/cjs/internals/interface.js.map +0 -1
- package/dist/cjs/internals/network.js +0 -148
- package/dist/cjs/internals/network.js.map +0 -1
- package/dist/cjs/internals/types.js +0 -6
- package/dist/cjs/internals/types.js.map +0 -1
- package/dist/cjs/internals/wait-for.js +0 -63
- package/dist/cjs/internals/wait-for.js.map +0 -1
- package/dist/esm/internals/interface.js +0 -100
- package/dist/esm/internals/interface.js.map +0 -1
- package/dist/esm/internals/network.js +0 -143
- package/dist/esm/internals/network.js.map +0 -1
- package/dist/esm/internals/types.js +0 -3
- package/dist/esm/internals/types.js.map +0 -1
- package/dist/esm/internals/wait-for.js +0 -63
- package/dist/esm/internals/wait-for.js.map +0 -1
- package/dist/types/internals/interface.d.ts +0 -25
- package/dist/types/internals/network.d.ts +0 -87
- package/dist/types/internals/types.d.ts +0 -18
- package/dist/types/internals/wait-for.d.ts +0 -38
|
@@ -1,148 +0,0 @@
|
|
|
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
|
-
MockOptionsStruct: function() {
|
|
13
|
-
return MockOptionsStruct;
|
|
14
|
-
},
|
|
15
|
-
mock: function() {
|
|
16
|
-
return mock;
|
|
17
|
-
},
|
|
18
|
-
mockJsonRpc: function() {
|
|
19
|
-
return mockJsonRpc;
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
const _snapssimulator = require("@metamask/snaps-simulator");
|
|
23
|
-
const _utils = require("@metamask/utils");
|
|
24
|
-
const _superstruct = require("superstruct");
|
|
25
|
-
const _logger = require("./logger");
|
|
26
|
-
/**
|
|
27
|
-
* The default headers to use for mocked responses. These headers are used to
|
|
28
|
-
* enable CORS.
|
|
29
|
-
*/ const DEFAULT_HEADERS = {
|
|
30
|
-
/* eslint-disable @typescript-eslint/naming-convention */ 'Access-Control-Allow-Origin': '*',
|
|
31
|
-
'Access-Control-Allow-Credentials': 'true',
|
|
32
|
-
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
33
|
-
'Access-Control-Allow-Headers': 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
|
|
34
|
-
};
|
|
35
|
-
const log = (0, _utils.createModuleLogger)(_logger.rootLogger, 'network');
|
|
36
|
-
const MockOptionsBaseStruct = (0, _superstruct.object)({
|
|
37
|
-
response: (0, _superstruct.defaulted)((0, _superstruct.object)({
|
|
38
|
-
status: (0, _superstruct.defaulted)((0, _superstruct.number)(), 200),
|
|
39
|
-
headers: (0, _superstruct.defaulted)((0, _superstruct.record)((0, _superstruct.string)(), (0, _superstruct.unknown)()), DEFAULT_HEADERS),
|
|
40
|
-
contentType: (0, _superstruct.defaulted)((0, _superstruct.string)(), 'text/plain'),
|
|
41
|
-
// Note: We default to a newline here, because the fetch request never
|
|
42
|
-
// resolves if the body is empty.
|
|
43
|
-
body: (0, _superstruct.defaulted)((0, _superstruct.string)(), '\n')
|
|
44
|
-
}), {})
|
|
45
|
-
});
|
|
46
|
-
const MockOptionsUrlStruct = (0, _superstruct.object)({
|
|
47
|
-
url: (0, _superstruct.union)([
|
|
48
|
-
(0, _superstruct.string)(),
|
|
49
|
-
(0, _superstruct.regexp)()
|
|
50
|
-
]),
|
|
51
|
-
partial: (0, _superstruct.optional)((0, _superstruct.boolean)())
|
|
52
|
-
});
|
|
53
|
-
const MockOptionsConditionStruct = (0, _superstruct.object)({
|
|
54
|
-
condition: (0, _superstruct.func)()
|
|
55
|
-
});
|
|
56
|
-
const MockOptionsStruct = (0, _superstruct.union)([
|
|
57
|
-
(0, _superstruct.assign)(MockOptionsBaseStruct, MockOptionsUrlStruct),
|
|
58
|
-
(0, _superstruct.assign)(MockOptionsBaseStruct, MockOptionsConditionStruct)
|
|
59
|
-
]);
|
|
60
|
-
/**
|
|
61
|
-
* Check if the given URL matches the given request, or if the condition
|
|
62
|
-
* function returns `true`.
|
|
63
|
-
*
|
|
64
|
-
* @param request - The request to check.
|
|
65
|
-
* @param options - The options for the network mocking.
|
|
66
|
-
* @returns Whether the URL matches the request.
|
|
67
|
-
*/ function matches(request, options) {
|
|
68
|
-
if ('url' in options) {
|
|
69
|
-
const { url, partial } = options;
|
|
70
|
-
if (typeof url === 'string') {
|
|
71
|
-
if (partial) {
|
|
72
|
-
return request.url().startsWith(url);
|
|
73
|
-
}
|
|
74
|
-
return url === request.url();
|
|
75
|
-
}
|
|
76
|
-
return url.test(request.url());
|
|
77
|
-
}
|
|
78
|
-
const { condition } = options;
|
|
79
|
-
return condition(request);
|
|
80
|
-
}
|
|
81
|
-
async function mock(page, options) {
|
|
82
|
-
await page.setRequestInterception(true);
|
|
83
|
-
const parsedOptions = (0, _superstruct.create)(options, MockOptionsStruct);
|
|
84
|
-
/**
|
|
85
|
-
* The mock handler.
|
|
86
|
-
*
|
|
87
|
-
* @param request - The request to handle.
|
|
88
|
-
*/ function handler(request) {
|
|
89
|
-
// If the request is already handled, Puppeteer will throw an error if we
|
|
90
|
-
// try to continue the request.
|
|
91
|
-
if (request.isInterceptResolutionHandled()) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
if (!matches(request, parsedOptions)) {
|
|
95
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
96
|
-
request.continue();
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
log('Mocking request to %s', request.url());
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
101
|
-
request.respond(parsedOptions.response);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Unmock the page.
|
|
105
|
-
*/ async function unmock() {
|
|
106
|
-
await page.setRequestInterception(false);
|
|
107
|
-
page.off('request', handler);
|
|
108
|
-
}
|
|
109
|
-
page.on('request', handler);
|
|
110
|
-
return {
|
|
111
|
-
unmock
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
const MockJsonRpcOptionsStruct = (0, _superstruct.object)({
|
|
115
|
-
method: (0, _superstruct.string)(),
|
|
116
|
-
result: _utils.UnsafeJsonStruct
|
|
117
|
-
});
|
|
118
|
-
async function mockJsonRpc(page, { method, result }) {
|
|
119
|
-
return await mock(page, {
|
|
120
|
-
condition: (request)=>{
|
|
121
|
-
if (request.url() !== _snapssimulator.JSON_RPC_ENDPOINT) {
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
const body = request.postData();
|
|
125
|
-
if (!body) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
try {
|
|
129
|
-
const json = JSON.parse(body);
|
|
130
|
-
return json.method === method;
|
|
131
|
-
} catch (error) {
|
|
132
|
-
log(`Unable to mock "${method}" request to Ethereum provider: %s`, error.message);
|
|
133
|
-
return false;
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
response: {
|
|
137
|
-
status: 200,
|
|
138
|
-
contentType: 'application/json',
|
|
139
|
-
body: JSON.stringify({
|
|
140
|
-
jsonrpc: '2.0',
|
|
141
|
-
id: 1,
|
|
142
|
-
result
|
|
143
|
-
})
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
//# sourceMappingURL=network.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/network.ts"],"sourcesContent":["import { JSON_RPC_ENDPOINT } from '@metamask/snaps-simulator';\nimport { createModuleLogger, UnsafeJsonStruct } from '@metamask/utils';\nimport type { Page, HTTPRequest } from 'puppeteer';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n assign,\n boolean,\n create,\n defaulted,\n number,\n object,\n optional,\n record,\n regexp,\n string,\n union,\n unknown,\n func,\n} from 'superstruct';\n\nimport type { DeepPartial } from '../types';\nimport { rootLogger } from './logger';\n\n/**\n * The default headers to use for mocked responses. These headers are used to\n * enable CORS.\n */\nconst DEFAULT_HEADERS = {\n /* eslint-disable @typescript-eslint/naming-convention */\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Credentials': 'true',\n 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',\n 'Access-Control-Allow-Headers':\n 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers',\n /* eslint-enable @typescript-eslint/naming-convention */\n};\n\nconst log = createModuleLogger(rootLogger, 'network');\n\nexport type Unmock = () => Promise<void>;\n\nexport type Mock = {\n /**\n * A function that can be used to unmock the URL.\n */\n unmock: Unmock;\n};\n\n/**\n * A function that can return `true` if the given request should be mocked, or\n * false if not.\n *\n * @param request - The request to check.\n * @returns Whether to mock the request.\n */\nexport type ConditionFunction = (request: HTTPRequest) => boolean;\n\nconst MockOptionsBaseStruct = object({\n response: defaulted(\n object({\n status: defaulted(number(), 200),\n headers: defaulted(record(string(), unknown()), DEFAULT_HEADERS),\n contentType: defaulted(string(), 'text/plain'),\n\n // Note: We default to a newline here, because the fetch request never\n // resolves if the body is empty.\n body: defaulted(string(), '\\n'),\n }),\n {},\n ),\n});\n\nconst MockOptionsUrlStruct = object({\n url: union([string(), regexp()]),\n partial: optional(boolean()),\n});\n\nconst MockOptionsConditionStruct = object({\n condition: func() as unknown as Struct<ConditionFunction, null>,\n});\n\nexport const MockOptionsStruct = union([\n assign(MockOptionsBaseStruct, MockOptionsUrlStruct),\n assign(MockOptionsBaseStruct, MockOptionsConditionStruct),\n]);\n\n/**\n * The options for the network mocking.\n *\n * @property url - The URL to mock. If a string is provided, the URL will be\n * matched exactly. If a RegExp is provided, the URL will be matched against it.\n * This option is incompatible with the `condition` option.\n * @property partial - If enabled, the request will be mocked if the URL starts\n * with the given URL. This option is ignored if a RegExp is provided to the\n * `url` option. This option is incompatible with the `condition` option.\n * @property condition - A function which gets the {@link HTTPRequest} as\n * parameter and returns a boolean to indicate whether the response should be\n * mocked or not. This option is incompatible with the `url` and `partial`\n * options.\n * @property response - The response to send for the request.\n * @property response.status - The status code to send for the response.\n * Defaults to `200`.\n * @property response.headers - The headers to send for the response. Defaults\n * to headers that enable CORS.\n * @property response.contentType - The content type to send for the response.\n * Defaults to `text/plain`.\n */\nexport type MockOptions = Infer<typeof MockOptionsStruct>;\n\n/**\n * Check if the given URL matches the given request, or if the condition\n * function returns `true`.\n *\n * @param request - The request to check.\n * @param options - The options for the network mocking.\n * @returns Whether the URL matches the request.\n */\nfunction matches(request: HTTPRequest, options: MockOptions) {\n if ('url' in options) {\n const { url, partial } = options;\n if (typeof url === 'string') {\n if (partial) {\n return request.url().startsWith(url);\n }\n\n return url === request.url();\n }\n\n return url.test(request.url());\n }\n\n const { condition } = options;\n return condition(request);\n}\n\n/**\n * Enable network mocking for the given page, and all its sub-frames.\n *\n * @param page - The page to enable network mocking on.\n * @param options - The options for the network mocking.\n * @returns A {@link Mock} object, with an `unmock` function.\n */\nexport async function mock(\n page: Page,\n options: DeepPartial<MockOptions>,\n): Promise<Mock> {\n await page.setRequestInterception(true);\n\n const parsedOptions = create(options, MockOptionsStruct);\n\n /**\n * The mock handler.\n *\n * @param request - The request to handle.\n */\n function handler(request: HTTPRequest) {\n // If the request is already handled, Puppeteer will throw an error if we\n // try to continue the request.\n if (request.isInterceptResolutionHandled()) {\n return;\n }\n\n if (!matches(request, parsedOptions)) {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n request.continue();\n return;\n }\n\n log('Mocking request to %s', request.url());\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n request.respond(parsedOptions.response);\n }\n\n /**\n * Unmock the page.\n */\n async function unmock() {\n await page.setRequestInterception(false);\n page.off('request', handler);\n }\n\n page.on('request', handler);\n\n return {\n unmock,\n };\n}\n\nconst MockJsonRpcOptionsStruct = object({\n method: string(),\n result: UnsafeJsonStruct,\n});\n\nexport type MockJsonRpcOptions = Infer<typeof MockJsonRpcOptionsStruct>;\n\n/**\n * Mock an Ethereum JSON-RPC request. This intercepts all requests to the\n * Ethereum provider, and returns the `result` instead.\n *\n * @param page - The page to enable network JSON-RPC mocking on.\n * @param options - The options for the JSON-RPC mock.\n * @param options.method - The JSON-RPC method to mock. Any other methods will be\n * forwarded to the provider.\n * @param options.result - The JSON response to return.\n * @returns A {@link Mock} object, with an `unmock` function.\n */\nexport async function mockJsonRpc(\n page: Page,\n { method, result }: MockJsonRpcOptions,\n) {\n return await mock(page, {\n condition: (request: HTTPRequest) => {\n if (request.url() !== JSON_RPC_ENDPOINT) {\n return false;\n }\n\n const body = request.postData();\n if (!body) {\n return false;\n }\n\n try {\n const json = JSON.parse(body);\n return json.method === method;\n } catch (error) {\n log(\n `Unable to mock \"${method}\" request to Ethereum provider: %s`,\n error.message,\n );\n return false;\n }\n },\n response: {\n status: 200,\n contentType: 'application/json',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 1,\n result,\n }),\n },\n });\n}\n"],"names":["MockOptionsStruct","mock","mockJsonRpc","DEFAULT_HEADERS","log","createModuleLogger","rootLogger","MockOptionsBaseStruct","object","response","defaulted","status","number","headers","record","string","unknown","contentType","body","MockOptionsUrlStruct","url","union","regexp","partial","optional","boolean","MockOptionsConditionStruct","condition","func","assign","matches","request","options","startsWith","test","page","setRequestInterception","parsedOptions","create","handler","isInterceptResolutionHandled","continue","respond","unmock","off","on","MockJsonRpcOptionsStruct","method","result","UnsafeJsonStruct","JSON_RPC_ENDPOINT","postData","json","JSON","parse","error","message","stringify","jsonrpc","id"],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/types.ts"],"names":[],"mappings":""}
|
|
@@ -1,63 +0,0 @@
|
|
|
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
|
-
waitFor: function() {
|
|
13
|
-
return waitFor;
|
|
14
|
-
},
|
|
15
|
-
waitForResponse: function() {
|
|
16
|
-
return waitForResponse;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const _utils = require("@metamask/utils");
|
|
20
|
-
const _pptrtestinglibrary = require("pptr-testing-library");
|
|
21
|
-
const _logger = require("./logger");
|
|
22
|
-
const log = (0, _utils.createModuleLogger)(_logger.rootLogger, 'wait-for');
|
|
23
|
-
async function waitFor(fn, { timeout = 3000, message } = {}) {
|
|
24
|
-
try {
|
|
25
|
-
let result;
|
|
26
|
-
await (0, _pptrtestinglibrary.waitFor)(// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
27
|
-
async ()=>{
|
|
28
|
-
// Puppeteer's `waitFor` function does not support returning a value
|
|
29
|
-
// from the condition function, so we need to use a variable outside
|
|
30
|
-
// the scope of the function.
|
|
31
|
-
result = await fn();
|
|
32
|
-
}, {
|
|
33
|
-
timeout
|
|
34
|
-
});
|
|
35
|
-
(0, _utils.assert)(result !== undefined);
|
|
36
|
-
return result;
|
|
37
|
-
} catch (error) {
|
|
38
|
-
if (message) {
|
|
39
|
-
throw new Error(message);
|
|
40
|
-
}
|
|
41
|
-
throw error;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async function waitForResponse(page, type) {
|
|
45
|
-
log('Waiting for response of type %s.', type);
|
|
46
|
-
return await page.evaluate(async (_type)=>{
|
|
47
|
-
return new Promise((resolve)=>{
|
|
48
|
-
window.__SIMULATOR_API__.dispatch({
|
|
49
|
-
type: `${_type}/clearResponse`
|
|
50
|
-
});
|
|
51
|
-
const unsubscribe = window.__SIMULATOR_API__.subscribe(()=>{
|
|
52
|
-
const state = window.__SIMULATOR_API__.getState();
|
|
53
|
-
const { pending, response } = state[_type];
|
|
54
|
-
if (!pending && response) {
|
|
55
|
-
unsubscribe();
|
|
56
|
-
resolve(response);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}, type);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//# sourceMappingURL=wait-for.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/wait-for.ts"],"sourcesContent":["import type { HandlerType } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport { waitFor as waitForPuppeteer } from 'pptr-testing-library';\nimport type { Page } from 'puppeteer';\n\nimport type { SnapResponse } from '../types';\nimport { rootLogger } from './logger';\n\nexport type WaitForOptions = {\n /**\n * The timeout in milliseconds.\n */\n timeout?: number;\n\n /**\n * The error message to throw if the condition is not met.\n */\n message?: string;\n};\n\nconst log = createModuleLogger(rootLogger, 'wait-for');\n\n/**\n * Wait for a condition to be true. This is a wrapper around\n * `pptr-testing-library`'s `waitFor` function, with the addition of a custom\n * error message.\n *\n * @param fn - The condition to wait for.\n * @param options - The options.\n * @param options.timeout - The timeout in milliseconds.\n * @param options.message - The error message to throw if the condition is not\n * met.\n * @returns A promise that resolves when the condition is met. The promise\n * resolves to the return value of the condition function.\n */\nexport async function waitFor<Result>(\n fn: () => Promise<Result>,\n { timeout = 3000, message }: WaitForOptions = {},\n) {\n try {\n let result: Result | undefined;\n\n await waitForPuppeteer(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => {\n // Puppeteer's `waitFor` function does not support returning a value\n // from the condition function, so we need to use a variable outside\n // the scope of the function.\n result = await fn();\n },\n {\n timeout,\n },\n );\n\n assert(result !== undefined);\n return result;\n } catch (error) {\n if (message) {\n throw new Error(message);\n }\n\n throw error;\n }\n}\n\n/**\n * Wait for a JSON-RPC response.\n *\n * @param page - The page to wait for the response on.\n * @param type - The type of response to wait for.\n * @returns The JSON-RPC response.\n */\nexport async function waitForResponse(\n page: Page,\n type:\n | HandlerType.OnTransaction\n | HandlerType.OnRpcRequest\n | HandlerType.OnCronjob,\n) {\n log('Waiting for response of type %s.', type);\n\n return await page.evaluate(async (_type) => {\n return new Promise<SnapResponse['response']>((resolve) => {\n window.__SIMULATOR_API__.dispatch({\n type: `${_type}/clearResponse`,\n });\n\n const unsubscribe = window.__SIMULATOR_API__.subscribe(() => {\n const state = window.__SIMULATOR_API__.getState();\n const { pending, response } = state[_type];\n\n if (!pending && response) {\n unsubscribe();\n\n resolve(response);\n }\n });\n });\n }, type);\n}\n"],"names":["waitFor","waitForResponse","log","createModuleLogger","rootLogger","fn","timeout","message","result","waitForPuppeteer","assert","undefined","error","Error","page","type","evaluate","_type","Promise","resolve","window","__SIMULATOR_API__","dispatch","unsubscribe","subscribe","state","getState","pending","response"],"mappings":";;;;;;;;;;;IAmCsBA,OAAO;eAAPA;;IAsCAC,eAAe;eAAfA;;;uBAxEqB;oCACC;wBAIjB;AAc3B,MAAMC,MAAMC,IAAAA,yBAAkB,EAACC,kBAAU,EAAE;AAepC,eAAeJ,QACpBK,EAAyB,EACzB,EAAEC,UAAU,IAAI,EAAEC,OAAO,EAAkB,GAAG,CAAC,CAAC;IAEhD,IAAI;QACF,IAAIC;QAEJ,MAAMC,IAAAA,2BAAgB,EACpB,kEAAkE;QAClE;YACE,oEAAoE;YACpE,oEAAoE;YACpE,6BAA6B;YAC7BD,SAAS,MAAMH;QACjB,GACA;YACEC;QACF;QAGFI,IAAAA,aAAM,EAACF,WAAWG;QAClB,OAAOH;IACT,EAAE,OAAOI,OAAO;QACd,IAAIL,SAAS;YACX,MAAM,IAAIM,MAAMN;QAClB;QAEA,MAAMK;IACR;AACF;AASO,eAAeX,gBACpBa,IAAU,EACVC,IAGyB;IAEzBb,IAAI,oCAAoCa;IAExC,OAAO,MAAMD,KAAKE,QAAQ,CAAC,OAAOC;QAChC,OAAO,IAAIC,QAAkC,CAACC;YAC5CC,OAAOC,iBAAiB,CAACC,QAAQ,CAAC;gBAChCP,MAAM,CAAC,EAAEE,MAAM,cAAc,CAAC;YAChC;YAEA,MAAMM,cAAcH,OAAOC,iBAAiB,CAACG,SAAS,CAAC;gBACrD,MAAMC,QAAQL,OAAOC,iBAAiB,CAACK,QAAQ;gBAC/C,MAAM,EAAEC,OAAO,EAAEC,QAAQ,EAAE,GAAGH,KAAK,CAACR,MAAM;gBAE1C,IAAI,CAACU,WAAWC,UAAU;oBACxBL;oBAEAJ,QAAQS;gBACV;YACF;QACF;IACF,GAAGb;AACL"}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { DialogType } from '@metamask/snaps-sdk';
|
|
2
|
-
import { assert } from '@metamask/utils';
|
|
3
|
-
import { create } from 'superstruct';
|
|
4
|
-
import { SnapOptionsStruct } from './structs';
|
|
5
|
-
import { waitFor } from './wait-for';
|
|
6
|
-
/**
|
|
7
|
-
* Get the current snap user interface (i.e., dialog). This will throw an error
|
|
8
|
-
* if the snap does not show a user interface within the timeout.
|
|
9
|
-
*
|
|
10
|
-
* @param page - The page to get the interface from.
|
|
11
|
-
* @param options - The options to use.
|
|
12
|
-
* @param options.timeout - The timeout in milliseconds to use. Defaults to
|
|
13
|
-
* `1000`.
|
|
14
|
-
* @returns The user interface object.
|
|
15
|
-
*/ export async function getInterface(page, options = {}) {
|
|
16
|
-
const { timeout } = create(options, SnapOptionsStruct);
|
|
17
|
-
const { type, node: content } = await waitFor(async ()=>{
|
|
18
|
-
const ui = await page.evaluate(()=>{
|
|
19
|
-
const state = window.__SIMULATOR_API__.getState();
|
|
20
|
-
return state.simulation.ui;
|
|
21
|
-
});
|
|
22
|
-
assert(ui);
|
|
23
|
-
return ui;
|
|
24
|
-
}, {
|
|
25
|
-
timeout,
|
|
26
|
-
message: 'Timed out waiting for snap interface to be shown.'
|
|
27
|
-
});
|
|
28
|
-
switch(type){
|
|
29
|
-
case DialogType.Alert:
|
|
30
|
-
return {
|
|
31
|
-
type: 'alert',
|
|
32
|
-
content,
|
|
33
|
-
ok: async ()=>{
|
|
34
|
-
await page.evaluate(()=>{
|
|
35
|
-
window.__SIMULATOR_API__.dispatch({
|
|
36
|
-
type: 'simulation/resolveUserInterface',
|
|
37
|
-
payload: null
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
case DialogType.Confirmation:
|
|
43
|
-
return {
|
|
44
|
-
type: 'confirmation',
|
|
45
|
-
content,
|
|
46
|
-
ok: async ()=>{
|
|
47
|
-
await page.evaluate(()=>{
|
|
48
|
-
window.__SIMULATOR_API__.dispatch({
|
|
49
|
-
type: 'simulation/resolveUserInterface',
|
|
50
|
-
payload: true
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
cancel: async ()=>{
|
|
55
|
-
await page.evaluate(()=>{
|
|
56
|
-
window.__SIMULATOR_API__.dispatch({
|
|
57
|
-
type: 'simulation/resolveUserInterface',
|
|
58
|
-
payload: false
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
case DialogType.Prompt:
|
|
64
|
-
return {
|
|
65
|
-
type: 'prompt',
|
|
66
|
-
content,
|
|
67
|
-
ok: async (value)=>{
|
|
68
|
-
await page.evaluate((payload)=>{
|
|
69
|
-
window.__SIMULATOR_API__.dispatch({
|
|
70
|
-
type: 'simulation/resolveUserInterface',
|
|
71
|
-
payload
|
|
72
|
-
});
|
|
73
|
-
}, value);
|
|
74
|
-
},
|
|
75
|
-
cancel: async ()=>{
|
|
76
|
-
await page.evaluate(()=>{
|
|
77
|
-
window.__SIMULATOR_API__.dispatch({
|
|
78
|
-
type: 'simulation/resolveUserInterface',
|
|
79
|
-
payload: null
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
default:
|
|
85
|
-
throw new Error(`Unknown or unsupported dialog type: ${String(type)}.`);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get the text of the notifications.
|
|
90
|
-
*
|
|
91
|
-
* @param page - The page to get the notifications from.
|
|
92
|
-
* @param requestId - The ID of the request to get the notifications for.
|
|
93
|
-
* @returns The text of the notifications, in order of appearance.
|
|
94
|
-
*/ export async function getNotifications(page, requestId) {
|
|
95
|
-
return await page.evaluate((id)=>{
|
|
96
|
-
return window.__SIMULATOR_API__.getNotifications(id);
|
|
97
|
-
}, requestId);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
//# sourceMappingURL=interface.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/interface.ts"],"sourcesContent":["import { DialogType } from '@metamask/snaps-sdk';\nimport { assert } from '@metamask/utils';\nimport type { Page } from 'puppeteer';\nimport { create } from 'superstruct';\n\nimport type { SnapInterface, SnapOptions } from '../types';\nimport { SnapOptionsStruct } from './structs';\nimport { waitFor } from './wait-for';\n\n/**\n * Get the current snap user interface (i.e., dialog). This will throw an error\n * if the snap does not show a user interface within the timeout.\n *\n * @param page - The page to get the interface from.\n * @param options - The options to use.\n * @param options.timeout - The timeout in milliseconds to use. Defaults to\n * `1000`.\n * @returns The user interface object.\n */\nexport async function getInterface(\n page: Page,\n options: SnapOptions = {},\n): Promise<SnapInterface> {\n const { timeout } = create(options, SnapOptionsStruct);\n\n const { type, node: content } = await waitFor(\n async () => {\n const ui = await page.evaluate(() => {\n const state = window.__SIMULATOR_API__.getState();\n return state.simulation.ui;\n });\n\n assert(ui);\n return ui;\n },\n {\n timeout,\n message: 'Timed out waiting for snap interface to be shown.',\n },\n );\n\n switch (type) {\n case DialogType.Alert:\n return {\n type: 'alert',\n content,\n\n ok: async () => {\n await page.evaluate(() => {\n window.__SIMULATOR_API__.dispatch({\n type: 'simulation/resolveUserInterface',\n payload: null,\n });\n });\n },\n };\n\n case DialogType.Confirmation:\n return {\n type: 'confirmation',\n content,\n\n ok: async () => {\n await page.evaluate(() => {\n window.__SIMULATOR_API__.dispatch({\n type: 'simulation/resolveUserInterface',\n payload: true,\n });\n });\n },\n\n cancel: async () => {\n await page.evaluate(() => {\n window.__SIMULATOR_API__.dispatch({\n type: 'simulation/resolveUserInterface',\n payload: false,\n });\n });\n },\n };\n\n case DialogType.Prompt:\n return {\n type: 'prompt',\n content,\n\n ok: async (value) => {\n await page.evaluate((payload) => {\n window.__SIMULATOR_API__.dispatch({\n type: 'simulation/resolveUserInterface',\n payload,\n });\n }, value);\n },\n\n cancel: async () => {\n await page.evaluate(() => {\n window.__SIMULATOR_API__.dispatch({\n type: 'simulation/resolveUserInterface',\n payload: null,\n });\n });\n },\n };\n\n default:\n throw new Error(`Unknown or unsupported dialog type: ${String(type)}.`);\n }\n}\n\n/**\n * Get the text of the notifications.\n *\n * @param page - The page to get the notifications from.\n * @param requestId - The ID of the request to get the notifications for.\n * @returns The text of the notifications, in order of appearance.\n */\nexport async function getNotifications(page: Page, requestId: string) {\n return await page.evaluate((id) => {\n return window.__SIMULATOR_API__.getNotifications(id);\n }, requestId);\n}\n"],"names":["DialogType","assert","create","SnapOptionsStruct","waitFor","getInterface","page","options","timeout","type","node","content","ui","evaluate","state","window","__SIMULATOR_API__","getState","simulation","message","Alert","ok","dispatch","payload","Confirmation","cancel","Prompt","value","Error","String","getNotifications","requestId","id"],"mappings":"AAAA,SAASA,UAAU,QAAQ,sBAAsB;AACjD,SAASC,MAAM,QAAQ,kBAAkB;AAEzC,SAASC,MAAM,QAAQ,cAAc;AAGrC,SAASC,iBAAiB,QAAQ,YAAY;AAC9C,SAASC,OAAO,QAAQ,aAAa;AAErC;;;;;;;;;CASC,GACD,OAAO,eAAeC,aACpBC,IAAU,EACVC,UAAuB,CAAC,CAAC;IAEzB,MAAM,EAAEC,OAAO,EAAE,GAAGN,OAAOK,SAASJ;IAEpC,MAAM,EAAEM,IAAI,EAAEC,MAAMC,OAAO,EAAE,GAAG,MAAMP,QACpC;QACE,MAAMQ,KAAK,MAAMN,KAAKO,QAAQ,CAAC;YAC7B,MAAMC,QAAQC,OAAOC,iBAAiB,CAACC,QAAQ;YAC/C,OAAOH,MAAMI,UAAU,CAACN,EAAE;QAC5B;QAEAX,OAAOW;QACP,OAAOA;IACT,GACA;QACEJ;QACAW,SAAS;IACX;IAGF,OAAQV;QACN,KAAKT,WAAWoB,KAAK;YACnB,OAAO;gBACLX,MAAM;gBACNE;gBAEAU,IAAI;oBACF,MAAMf,KAAKO,QAAQ,CAAC;wBAClBE,OAAOC,iBAAiB,CAACM,QAAQ,CAAC;4BAChCb,MAAM;4BACNc,SAAS;wBACX;oBACF;gBACF;YACF;QAEF,KAAKvB,WAAWwB,YAAY;YAC1B,OAAO;gBACLf,MAAM;gBACNE;gBAEAU,IAAI;oBACF,MAAMf,KAAKO,QAAQ,CAAC;wBAClBE,OAAOC,iBAAiB,CAACM,QAAQ,CAAC;4BAChCb,MAAM;4BACNc,SAAS;wBACX;oBACF;gBACF;gBAEAE,QAAQ;oBACN,MAAMnB,KAAKO,QAAQ,CAAC;wBAClBE,OAAOC,iBAAiB,CAACM,QAAQ,CAAC;4BAChCb,MAAM;4BACNc,SAAS;wBACX;oBACF;gBACF;YACF;QAEF,KAAKvB,WAAW0B,MAAM;YACpB,OAAO;gBACLjB,MAAM;gBACNE;gBAEAU,IAAI,OAAOM;oBACT,MAAMrB,KAAKO,QAAQ,CAAC,CAACU;wBACnBR,OAAOC,iBAAiB,CAACM,QAAQ,CAAC;4BAChCb,MAAM;4BACNc;wBACF;oBACF,GAAGI;gBACL;gBAEAF,QAAQ;oBACN,MAAMnB,KAAKO,QAAQ,CAAC;wBAClBE,OAAOC,iBAAiB,CAACM,QAAQ,CAAC;4BAChCb,MAAM;4BACNc,SAAS;wBACX;oBACF;gBACF;YACF;QAEF;YACE,MAAM,IAAIK,MAAM,CAAC,oCAAoC,EAAEC,OAAOpB,MAAM,CAAC,CAAC;IAC1E;AACF;AAEA;;;;;;CAMC,GACD,OAAO,eAAeqB,iBAAiBxB,IAAU,EAAEyB,SAAiB;IAClE,OAAO,MAAMzB,KAAKO,QAAQ,CAAC,CAACmB;QAC1B,OAAOjB,OAAOC,iBAAiB,CAACc,gBAAgB,CAACE;IACnD,GAAGD;AACL"}
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { JSON_RPC_ENDPOINT } from '@metamask/snaps-simulator';
|
|
2
|
-
import { createModuleLogger, UnsafeJsonStruct } from '@metamask/utils';
|
|
3
|
-
import { assign, boolean, create, defaulted, number, object, optional, record, regexp, string, union, unknown, func } from 'superstruct';
|
|
4
|
-
import { rootLogger } from './logger';
|
|
5
|
-
/**
|
|
6
|
-
* The default headers to use for mocked responses. These headers are used to
|
|
7
|
-
* enable CORS.
|
|
8
|
-
*/ const DEFAULT_HEADERS = {
|
|
9
|
-
/* eslint-disable @typescript-eslint/naming-convention */ 'Access-Control-Allow-Origin': '*',
|
|
10
|
-
'Access-Control-Allow-Credentials': 'true',
|
|
11
|
-
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
12
|
-
'Access-Control-Allow-Headers': 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
|
|
13
|
-
};
|
|
14
|
-
const log = createModuleLogger(rootLogger, 'network');
|
|
15
|
-
const MockOptionsBaseStruct = object({
|
|
16
|
-
response: defaulted(object({
|
|
17
|
-
status: defaulted(number(), 200),
|
|
18
|
-
headers: defaulted(record(string(), unknown()), DEFAULT_HEADERS),
|
|
19
|
-
contentType: defaulted(string(), 'text/plain'),
|
|
20
|
-
// Note: We default to a newline here, because the fetch request never
|
|
21
|
-
// resolves if the body is empty.
|
|
22
|
-
body: defaulted(string(), '\n')
|
|
23
|
-
}), {})
|
|
24
|
-
});
|
|
25
|
-
const MockOptionsUrlStruct = object({
|
|
26
|
-
url: union([
|
|
27
|
-
string(),
|
|
28
|
-
regexp()
|
|
29
|
-
]),
|
|
30
|
-
partial: optional(boolean())
|
|
31
|
-
});
|
|
32
|
-
const MockOptionsConditionStruct = object({
|
|
33
|
-
condition: func()
|
|
34
|
-
});
|
|
35
|
-
export const MockOptionsStruct = union([
|
|
36
|
-
assign(MockOptionsBaseStruct, MockOptionsUrlStruct),
|
|
37
|
-
assign(MockOptionsBaseStruct, MockOptionsConditionStruct)
|
|
38
|
-
]);
|
|
39
|
-
/**
|
|
40
|
-
* Check if the given URL matches the given request, or if the condition
|
|
41
|
-
* function returns `true`.
|
|
42
|
-
*
|
|
43
|
-
* @param request - The request to check.
|
|
44
|
-
* @param options - The options for the network mocking.
|
|
45
|
-
* @returns Whether the URL matches the request.
|
|
46
|
-
*/ function matches(request, options) {
|
|
47
|
-
if ('url' in options) {
|
|
48
|
-
const { url, partial } = options;
|
|
49
|
-
if (typeof url === 'string') {
|
|
50
|
-
if (partial) {
|
|
51
|
-
return request.url().startsWith(url);
|
|
52
|
-
}
|
|
53
|
-
return url === request.url();
|
|
54
|
-
}
|
|
55
|
-
return url.test(request.url());
|
|
56
|
-
}
|
|
57
|
-
const { condition } = options;
|
|
58
|
-
return condition(request);
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Enable network mocking for the given page, and all its sub-frames.
|
|
62
|
-
*
|
|
63
|
-
* @param page - The page to enable network mocking on.
|
|
64
|
-
* @param options - The options for the network mocking.
|
|
65
|
-
* @returns A {@link Mock} object, with an `unmock` function.
|
|
66
|
-
*/ export async function mock(page, options) {
|
|
67
|
-
await page.setRequestInterception(true);
|
|
68
|
-
const parsedOptions = create(options, MockOptionsStruct);
|
|
69
|
-
/**
|
|
70
|
-
* The mock handler.
|
|
71
|
-
*
|
|
72
|
-
* @param request - The request to handle.
|
|
73
|
-
*/ function handler(request) {
|
|
74
|
-
// If the request is already handled, Puppeteer will throw an error if we
|
|
75
|
-
// try to continue the request.
|
|
76
|
-
if (request.isInterceptResolutionHandled()) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (!matches(request, parsedOptions)) {
|
|
80
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
81
|
-
request.continue();
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
log('Mocking request to %s', request.url());
|
|
85
|
-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
86
|
-
request.respond(parsedOptions.response);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Unmock the page.
|
|
90
|
-
*/ async function unmock() {
|
|
91
|
-
await page.setRequestInterception(false);
|
|
92
|
-
page.off('request', handler);
|
|
93
|
-
}
|
|
94
|
-
page.on('request', handler);
|
|
95
|
-
return {
|
|
96
|
-
unmock
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
const MockJsonRpcOptionsStruct = object({
|
|
100
|
-
method: string(),
|
|
101
|
-
result: UnsafeJsonStruct
|
|
102
|
-
});
|
|
103
|
-
/**
|
|
104
|
-
* Mock an Ethereum JSON-RPC request. This intercepts all requests to the
|
|
105
|
-
* Ethereum provider, and returns the `result` instead.
|
|
106
|
-
*
|
|
107
|
-
* @param page - The page to enable network JSON-RPC mocking on.
|
|
108
|
-
* @param options - The options for the JSON-RPC mock.
|
|
109
|
-
* @param options.method - The JSON-RPC method to mock. Any other methods will be
|
|
110
|
-
* forwarded to the provider.
|
|
111
|
-
* @param options.result - The JSON response to return.
|
|
112
|
-
* @returns A {@link Mock} object, with an `unmock` function.
|
|
113
|
-
*/ export async function mockJsonRpc(page, { method, result }) {
|
|
114
|
-
return await mock(page, {
|
|
115
|
-
condition: (request)=>{
|
|
116
|
-
if (request.url() !== JSON_RPC_ENDPOINT) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
const body = request.postData();
|
|
120
|
-
if (!body) {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
try {
|
|
124
|
-
const json = JSON.parse(body);
|
|
125
|
-
return json.method === method;
|
|
126
|
-
} catch (error) {
|
|
127
|
-
log(`Unable to mock "${method}" request to Ethereum provider: %s`, error.message);
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
response: {
|
|
132
|
-
status: 200,
|
|
133
|
-
contentType: 'application/json',
|
|
134
|
-
body: JSON.stringify({
|
|
135
|
-
jsonrpc: '2.0',
|
|
136
|
-
id: 1,
|
|
137
|
-
result
|
|
138
|
-
})
|
|
139
|
-
}
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
//# sourceMappingURL=network.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/network.ts"],"sourcesContent":["import { JSON_RPC_ENDPOINT } from '@metamask/snaps-simulator';\nimport { createModuleLogger, UnsafeJsonStruct } from '@metamask/utils';\nimport type { Page, HTTPRequest } from 'puppeteer';\nimport type { Infer, Struct } from 'superstruct';\nimport {\n assign,\n boolean,\n create,\n defaulted,\n number,\n object,\n optional,\n record,\n regexp,\n string,\n union,\n unknown,\n func,\n} from 'superstruct';\n\nimport type { DeepPartial } from '../types';\nimport { rootLogger } from './logger';\n\n/**\n * The default headers to use for mocked responses. These headers are used to\n * enable CORS.\n */\nconst DEFAULT_HEADERS = {\n /* eslint-disable @typescript-eslint/naming-convention */\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Credentials': 'true',\n 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',\n 'Access-Control-Allow-Headers':\n 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers',\n /* eslint-enable @typescript-eslint/naming-convention */\n};\n\nconst log = createModuleLogger(rootLogger, 'network');\n\nexport type Unmock = () => Promise<void>;\n\nexport type Mock = {\n /**\n * A function that can be used to unmock the URL.\n */\n unmock: Unmock;\n};\n\n/**\n * A function that can return `true` if the given request should be mocked, or\n * false if not.\n *\n * @param request - The request to check.\n * @returns Whether to mock the request.\n */\nexport type ConditionFunction = (request: HTTPRequest) => boolean;\n\nconst MockOptionsBaseStruct = object({\n response: defaulted(\n object({\n status: defaulted(number(), 200),\n headers: defaulted(record(string(), unknown()), DEFAULT_HEADERS),\n contentType: defaulted(string(), 'text/plain'),\n\n // Note: We default to a newline here, because the fetch request never\n // resolves if the body is empty.\n body: defaulted(string(), '\\n'),\n }),\n {},\n ),\n});\n\nconst MockOptionsUrlStruct = object({\n url: union([string(), regexp()]),\n partial: optional(boolean()),\n});\n\nconst MockOptionsConditionStruct = object({\n condition: func() as unknown as Struct<ConditionFunction, null>,\n});\n\nexport const MockOptionsStruct = union([\n assign(MockOptionsBaseStruct, MockOptionsUrlStruct),\n assign(MockOptionsBaseStruct, MockOptionsConditionStruct),\n]);\n\n/**\n * The options for the network mocking.\n *\n * @property url - The URL to mock. If a string is provided, the URL will be\n * matched exactly. If a RegExp is provided, the URL will be matched against it.\n * This option is incompatible with the `condition` option.\n * @property partial - If enabled, the request will be mocked if the URL starts\n * with the given URL. This option is ignored if a RegExp is provided to the\n * `url` option. This option is incompatible with the `condition` option.\n * @property condition - A function which gets the {@link HTTPRequest} as\n * parameter and returns a boolean to indicate whether the response should be\n * mocked or not. This option is incompatible with the `url` and `partial`\n * options.\n * @property response - The response to send for the request.\n * @property response.status - The status code to send for the response.\n * Defaults to `200`.\n * @property response.headers - The headers to send for the response. Defaults\n * to headers that enable CORS.\n * @property response.contentType - The content type to send for the response.\n * Defaults to `text/plain`.\n */\nexport type MockOptions = Infer<typeof MockOptionsStruct>;\n\n/**\n * Check if the given URL matches the given request, or if the condition\n * function returns `true`.\n *\n * @param request - The request to check.\n * @param options - The options for the network mocking.\n * @returns Whether the URL matches the request.\n */\nfunction matches(request: HTTPRequest, options: MockOptions) {\n if ('url' in options) {\n const { url, partial } = options;\n if (typeof url === 'string') {\n if (partial) {\n return request.url().startsWith(url);\n }\n\n return url === request.url();\n }\n\n return url.test(request.url());\n }\n\n const { condition } = options;\n return condition(request);\n}\n\n/**\n * Enable network mocking for the given page, and all its sub-frames.\n *\n * @param page - The page to enable network mocking on.\n * @param options - The options for the network mocking.\n * @returns A {@link Mock} object, with an `unmock` function.\n */\nexport async function mock(\n page: Page,\n options: DeepPartial<MockOptions>,\n): Promise<Mock> {\n await page.setRequestInterception(true);\n\n const parsedOptions = create(options, MockOptionsStruct);\n\n /**\n * The mock handler.\n *\n * @param request - The request to handle.\n */\n function handler(request: HTTPRequest) {\n // If the request is already handled, Puppeteer will throw an error if we\n // try to continue the request.\n if (request.isInterceptResolutionHandled()) {\n return;\n }\n\n if (!matches(request, parsedOptions)) {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n request.continue();\n return;\n }\n\n log('Mocking request to %s', request.url());\n\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n request.respond(parsedOptions.response);\n }\n\n /**\n * Unmock the page.\n */\n async function unmock() {\n await page.setRequestInterception(false);\n page.off('request', handler);\n }\n\n page.on('request', handler);\n\n return {\n unmock,\n };\n}\n\nconst MockJsonRpcOptionsStruct = object({\n method: string(),\n result: UnsafeJsonStruct,\n});\n\nexport type MockJsonRpcOptions = Infer<typeof MockJsonRpcOptionsStruct>;\n\n/**\n * Mock an Ethereum JSON-RPC request. This intercepts all requests to the\n * Ethereum provider, and returns the `result` instead.\n *\n * @param page - The page to enable network JSON-RPC mocking on.\n * @param options - The options for the JSON-RPC mock.\n * @param options.method - The JSON-RPC method to mock. Any other methods will be\n * forwarded to the provider.\n * @param options.result - The JSON response to return.\n * @returns A {@link Mock} object, with an `unmock` function.\n */\nexport async function mockJsonRpc(\n page: Page,\n { method, result }: MockJsonRpcOptions,\n) {\n return await mock(page, {\n condition: (request: HTTPRequest) => {\n if (request.url() !== JSON_RPC_ENDPOINT) {\n return false;\n }\n\n const body = request.postData();\n if (!body) {\n return false;\n }\n\n try {\n const json = JSON.parse(body);\n return json.method === method;\n } catch (error) {\n log(\n `Unable to mock \"${method}\" request to Ethereum provider: %s`,\n error.message,\n );\n return false;\n }\n },\n response: {\n status: 200,\n contentType: 'application/json',\n body: JSON.stringify({\n jsonrpc: '2.0',\n id: 1,\n result,\n }),\n },\n });\n}\n"],"names":["JSON_RPC_ENDPOINT","createModuleLogger","UnsafeJsonStruct","assign","boolean","create","defaulted","number","object","optional","record","regexp","string","union","unknown","func","rootLogger","DEFAULT_HEADERS","log","MockOptionsBaseStruct","response","status","headers","contentType","body","MockOptionsUrlStruct","url","partial","MockOptionsConditionStruct","condition","MockOptionsStruct","matches","request","options","startsWith","test","mock","page","setRequestInterception","parsedOptions","handler","isInterceptResolutionHandled","continue","respond","unmock","off","on","MockJsonRpcOptionsStruct","method","result","mockJsonRpc","postData","json","JSON","parse","error","message","stringify","jsonrpc","id"],"mappings":"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"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/types.ts"],"sourcesContent":["import type { NotificationType, EnumToUnion } from '@metamask/snaps-sdk';\nimport type { ApplicationState, Dispatch } from '@metamask/snaps-simulator';\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/consistent-type-definitions\n interface Window {\n // This API is injected into the page by the simulator. It allows us to\n // dispatch actions to the simulator, and read the state directly from the\n // Redux store.\n // eslint-disable-next-line @typescript-eslint/naming-convention\n __SIMULATOR_API__: {\n dispatch: Dispatch;\n subscribe: (listener: () => void) => () => void;\n getState: () => ApplicationState;\n getRequestId: () => string;\n getNotifications: (requestId: string) => {\n id: string;\n message: string;\n type: EnumToUnion<NotificationType>;\n }[];\n };\n }\n}\n\nexport {};\n"],"names":[],"mappings":"AAAA,WAwBU"}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { assert, createModuleLogger } from '@metamask/utils';
|
|
2
|
-
import { waitFor as waitForPuppeteer } from 'pptr-testing-library';
|
|
3
|
-
import { rootLogger } from './logger';
|
|
4
|
-
const log = createModuleLogger(rootLogger, 'wait-for');
|
|
5
|
-
/**
|
|
6
|
-
* Wait for a condition to be true. This is a wrapper around
|
|
7
|
-
* `pptr-testing-library`'s `waitFor` function, with the addition of a custom
|
|
8
|
-
* error message.
|
|
9
|
-
*
|
|
10
|
-
* @param fn - The condition to wait for.
|
|
11
|
-
* @param options - The options.
|
|
12
|
-
* @param options.timeout - The timeout in milliseconds.
|
|
13
|
-
* @param options.message - The error message to throw if the condition is not
|
|
14
|
-
* met.
|
|
15
|
-
* @returns A promise that resolves when the condition is met. The promise
|
|
16
|
-
* resolves to the return value of the condition function.
|
|
17
|
-
*/ export async function waitFor(fn, { timeout = 3000, message } = {}) {
|
|
18
|
-
try {
|
|
19
|
-
let result;
|
|
20
|
-
await waitForPuppeteer(// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
21
|
-
async ()=>{
|
|
22
|
-
// Puppeteer's `waitFor` function does not support returning a value
|
|
23
|
-
// from the condition function, so we need to use a variable outside
|
|
24
|
-
// the scope of the function.
|
|
25
|
-
result = await fn();
|
|
26
|
-
}, {
|
|
27
|
-
timeout
|
|
28
|
-
});
|
|
29
|
-
assert(result !== undefined);
|
|
30
|
-
return result;
|
|
31
|
-
} catch (error) {
|
|
32
|
-
if (message) {
|
|
33
|
-
throw new Error(message);
|
|
34
|
-
}
|
|
35
|
-
throw error;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Wait for a JSON-RPC response.
|
|
40
|
-
*
|
|
41
|
-
* @param page - The page to wait for the response on.
|
|
42
|
-
* @param type - The type of response to wait for.
|
|
43
|
-
* @returns The JSON-RPC response.
|
|
44
|
-
*/ export async function waitForResponse(page, type) {
|
|
45
|
-
log('Waiting for response of type %s.', type);
|
|
46
|
-
return await page.evaluate(async (_type)=>{
|
|
47
|
-
return new Promise((resolve)=>{
|
|
48
|
-
window.__SIMULATOR_API__.dispatch({
|
|
49
|
-
type: `${_type}/clearResponse`
|
|
50
|
-
});
|
|
51
|
-
const unsubscribe = window.__SIMULATOR_API__.subscribe(()=>{
|
|
52
|
-
const state = window.__SIMULATOR_API__.getState();
|
|
53
|
-
const { pending, response } = state[_type];
|
|
54
|
-
if (!pending && response) {
|
|
55
|
-
unsubscribe();
|
|
56
|
-
resolve(response);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
});
|
|
60
|
-
}, type);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
//# sourceMappingURL=wait-for.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/internals/wait-for.ts"],"sourcesContent":["import type { HandlerType } from '@metamask/snaps-utils';\nimport { assert, createModuleLogger } from '@metamask/utils';\nimport { waitFor as waitForPuppeteer } from 'pptr-testing-library';\nimport type { Page } from 'puppeteer';\n\nimport type { SnapResponse } from '../types';\nimport { rootLogger } from './logger';\n\nexport type WaitForOptions = {\n /**\n * The timeout in milliseconds.\n */\n timeout?: number;\n\n /**\n * The error message to throw if the condition is not met.\n */\n message?: string;\n};\n\nconst log = createModuleLogger(rootLogger, 'wait-for');\n\n/**\n * Wait for a condition to be true. This is a wrapper around\n * `pptr-testing-library`'s `waitFor` function, with the addition of a custom\n * error message.\n *\n * @param fn - The condition to wait for.\n * @param options - The options.\n * @param options.timeout - The timeout in milliseconds.\n * @param options.message - The error message to throw if the condition is not\n * met.\n * @returns A promise that resolves when the condition is met. The promise\n * resolves to the return value of the condition function.\n */\nexport async function waitFor<Result>(\n fn: () => Promise<Result>,\n { timeout = 3000, message }: WaitForOptions = {},\n) {\n try {\n let result: Result | undefined;\n\n await waitForPuppeteer(\n // eslint-disable-next-line @typescript-eslint/no-misused-promises\n async () => {\n // Puppeteer's `waitFor` function does not support returning a value\n // from the condition function, so we need to use a variable outside\n // the scope of the function.\n result = await fn();\n },\n {\n timeout,\n },\n );\n\n assert(result !== undefined);\n return result;\n } catch (error) {\n if (message) {\n throw new Error(message);\n }\n\n throw error;\n }\n}\n\n/**\n * Wait for a JSON-RPC response.\n *\n * @param page - The page to wait for the response on.\n * @param type - The type of response to wait for.\n * @returns The JSON-RPC response.\n */\nexport async function waitForResponse(\n page: Page,\n type:\n | HandlerType.OnTransaction\n | HandlerType.OnRpcRequest\n | HandlerType.OnCronjob,\n) {\n log('Waiting for response of type %s.', type);\n\n return await page.evaluate(async (_type) => {\n return new Promise<SnapResponse['response']>((resolve) => {\n window.__SIMULATOR_API__.dispatch({\n type: `${_type}/clearResponse`,\n });\n\n const unsubscribe = window.__SIMULATOR_API__.subscribe(() => {\n const state = window.__SIMULATOR_API__.getState();\n const { pending, response } = state[_type];\n\n if (!pending && response) {\n unsubscribe();\n\n resolve(response);\n }\n });\n });\n }, type);\n}\n"],"names":["assert","createModuleLogger","waitFor","waitForPuppeteer","rootLogger","log","fn","timeout","message","result","undefined","error","Error","waitForResponse","page","type","evaluate","_type","Promise","resolve","window","__SIMULATOR_API__","dispatch","unsubscribe","subscribe","state","getState","pending","response"],"mappings":"AACA,SAASA,MAAM,EAAEC,kBAAkB,QAAQ,kBAAkB;AAC7D,SAASC,WAAWC,gBAAgB,QAAQ,uBAAuB;AAInE,SAASC,UAAU,QAAQ,WAAW;AActC,MAAMC,MAAMJ,mBAAmBG,YAAY;AAE3C;;;;;;;;;;;;CAYC,GACD,OAAO,eAAeF,QACpBI,EAAyB,EACzB,EAAEC,UAAU,IAAI,EAAEC,OAAO,EAAkB,GAAG,CAAC,CAAC;IAEhD,IAAI;QACF,IAAIC;QAEJ,MAAMN,iBACJ,kEAAkE;QAClE;YACE,oEAAoE;YACpE,oEAAoE;YACpE,6BAA6B;YAC7BM,SAAS,MAAMH;QACjB,GACA;YACEC;QACF;QAGFP,OAAOS,WAAWC;QAClB,OAAOD;IACT,EAAE,OAAOE,OAAO;QACd,IAAIH,SAAS;YACX,MAAM,IAAII,MAAMJ;QAClB;QAEA,MAAMG;IACR;AACF;AAEA;;;;;;CAMC,GACD,OAAO,eAAeE,gBACpBC,IAAU,EACVC,IAGyB;IAEzBV,IAAI,oCAAoCU;IAExC,OAAO,MAAMD,KAAKE,QAAQ,CAAC,OAAOC;QAChC,OAAO,IAAIC,QAAkC,CAACC;YAC5CC,OAAOC,iBAAiB,CAACC,QAAQ,CAAC;gBAChCP,MAAM,CAAC,EAAEE,MAAM,cAAc,CAAC;YAChC;YAEA,MAAMM,cAAcH,OAAOC,iBAAiB,CAACG,SAAS,CAAC;gBACrD,MAAMC,QAAQL,OAAOC,iBAAiB,CAACK,QAAQ;gBAC/C,MAAM,EAAEC,OAAO,EAAEC,QAAQ,EAAE,GAAGH,KAAK,CAACR,MAAM;gBAE1C,IAAI,CAACU,WAAWC,UAAU;oBACxBL;oBAEAJ,QAAQS;gBACV;YACF;QACF;IACF,GAAGb;AACL"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Page } from 'puppeteer';
|
|
2
|
-
import type { SnapInterface, SnapOptions } from '../types';
|
|
3
|
-
/**
|
|
4
|
-
* Get the current snap user interface (i.e., dialog). This will throw an error
|
|
5
|
-
* if the snap does not show a user interface within the timeout.
|
|
6
|
-
*
|
|
7
|
-
* @param page - The page to get the interface from.
|
|
8
|
-
* @param options - The options to use.
|
|
9
|
-
* @param options.timeout - The timeout in milliseconds to use. Defaults to
|
|
10
|
-
* `1000`.
|
|
11
|
-
* @returns The user interface object.
|
|
12
|
-
*/
|
|
13
|
-
export declare function getInterface(page: Page, options?: SnapOptions): Promise<SnapInterface>;
|
|
14
|
-
/**
|
|
15
|
-
* Get the text of the notifications.
|
|
16
|
-
*
|
|
17
|
-
* @param page - The page to get the notifications from.
|
|
18
|
-
* @param requestId - The ID of the request to get the notifications for.
|
|
19
|
-
* @returns The text of the notifications, in order of appearance.
|
|
20
|
-
*/
|
|
21
|
-
export declare function getNotifications(page: Page, requestId: string): Promise<{
|
|
22
|
-
id: string;
|
|
23
|
-
message: string;
|
|
24
|
-
type: "native" | "inApp";
|
|
25
|
-
}[]>;
|