@metamask-previews/base-controller 3.2.0-preview.d32a7cc
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 +72 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/BaseController.d.ts +114 -0
- package/dist/BaseController.d.ts.map +1 -0
- package/dist/BaseController.js +146 -0
- package/dist/BaseController.js.map +1 -0
- package/dist/BaseControllerV2.d.ts +147 -0
- package/dist/BaseControllerV2.d.ts.map +1 -0
- package/dist/BaseControllerV2.js +144 -0
- package/dist/BaseControllerV2.js.map +1 -0
- package/dist/ControllerMessenger.d.ts +357 -0
- package/dist/ControllerMessenger.d.ts.map +1 -0
- package/dist/ControllerMessenger.js +375 -0
- package/dist/ControllerMessenger.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ControllerMessenger = exports.RestrictedControllerMessenger = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* A restricted controller messenger.
|
|
6
|
+
*
|
|
7
|
+
* This acts as a wrapper around the controller messenger instance that restricts access to actions
|
|
8
|
+
* and events.
|
|
9
|
+
*
|
|
10
|
+
* @template N - The namespace for this messenger. Typically this is the name of the controller or
|
|
11
|
+
* module that this messenger has been created for. The authority to publish events and register
|
|
12
|
+
* actions under this namespace is granted to this restricted messenger instance.
|
|
13
|
+
* @template Action - A type union of all Action types.
|
|
14
|
+
* @template Event - A type union of all Event types.
|
|
15
|
+
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
16
|
+
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
17
|
+
*/
|
|
18
|
+
class RestrictedControllerMessenger {
|
|
19
|
+
/**
|
|
20
|
+
* Constructs a restricted controller messenger
|
|
21
|
+
*
|
|
22
|
+
* The provided allowlists grant the ability to call the listed actions and subscribe to the
|
|
23
|
+
* listed events. The "name" provided grants ownership of any actions and events under that
|
|
24
|
+
* namespace. Ownership allows registering actions and publishing events, as well as
|
|
25
|
+
* unregistering actions and clearing event subscriptions.
|
|
26
|
+
*
|
|
27
|
+
* @param options - The controller options.
|
|
28
|
+
* @param options.controllerMessenger - The controller messenger instance that is being wrapped.
|
|
29
|
+
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
30
|
+
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
31
|
+
* restricted controller messenger returned.
|
|
32
|
+
* @param options.allowedActions - The list of actions that this restricted controller messenger
|
|
33
|
+
* should be alowed to call.
|
|
34
|
+
* @param options.allowedEvents - The list of events that this restricted controller messenger
|
|
35
|
+
* should be allowed to subscribe to.
|
|
36
|
+
*/
|
|
37
|
+
constructor({ controllerMessenger, name, allowedActions, allowedEvents, }) {
|
|
38
|
+
this.controllerMessenger = controllerMessenger;
|
|
39
|
+
this.controllerName = name;
|
|
40
|
+
this.allowedActions = allowedActions || null;
|
|
41
|
+
this.allowedEvents = allowedEvents || null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register an action handler.
|
|
45
|
+
*
|
|
46
|
+
* This will make the registered function available to call via the `call` method.
|
|
47
|
+
*
|
|
48
|
+
* The action type this handler is registered under *must* be in the current namespace.
|
|
49
|
+
*
|
|
50
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
51
|
+
* @param handler - The action handler. This function gets called when the `call` method is
|
|
52
|
+
* invoked with the given action type.
|
|
53
|
+
* @throws Will throw when a handler has been registered for this action type already.
|
|
54
|
+
* @template T - A type union of Action type strings that are namespaced by N.
|
|
55
|
+
*/
|
|
56
|
+
registerActionHandler(action, handler) {
|
|
57
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
58
|
+
if (!action.startsWith(`${this.controllerName}:`)) {
|
|
59
|
+
throw new Error(`Only allowed registering action handlers prefixed by '${this.controllerName}:'`);
|
|
60
|
+
}
|
|
61
|
+
this.controllerMessenger.registerActionHandler(action, handler);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Unregister an action handler.
|
|
65
|
+
*
|
|
66
|
+
* This will prevent this action from being called.
|
|
67
|
+
*
|
|
68
|
+
* The action type being unregistered *must* be in the current namespace.
|
|
69
|
+
*
|
|
70
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
71
|
+
* @template T - A type union of Action type strings that are namespaced by N.
|
|
72
|
+
*/
|
|
73
|
+
unregisterActionHandler(action) {
|
|
74
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
75
|
+
if (!action.startsWith(`${this.controllerName}:`)) {
|
|
76
|
+
throw new Error(`Only allowed unregistering action handlers prefixed by '${this.controllerName}:'`);
|
|
77
|
+
}
|
|
78
|
+
this.controllerMessenger.unregisterActionHandler(action);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Call an action.
|
|
82
|
+
*
|
|
83
|
+
* This function will call the action handler corresponding to the given action type, passing
|
|
84
|
+
* along any parameters given.
|
|
85
|
+
*
|
|
86
|
+
* The action type being called must be on the action allowlist.
|
|
87
|
+
*
|
|
88
|
+
* @param action - The action type. This is a unqiue identifier for this action.
|
|
89
|
+
* @param params - The action parameters. These must match the type of the parameters of the
|
|
90
|
+
* registered action handler.
|
|
91
|
+
* @throws Will throw when no handler has been registered for the given type.
|
|
92
|
+
* @template T - A type union of allowed Action type strings.
|
|
93
|
+
* @returns The action return value.
|
|
94
|
+
*/
|
|
95
|
+
call(action, ...params) {
|
|
96
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
97
|
+
if (this.allowedActions === null) {
|
|
98
|
+
throw new Error('No actions allowed');
|
|
99
|
+
}
|
|
100
|
+
else if (!this.allowedActions.includes(action)) {
|
|
101
|
+
throw new Error(`Action missing from allow list: ${action}`);
|
|
102
|
+
}
|
|
103
|
+
return this.controllerMessenger.call(action, ...params);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Publish an event.
|
|
107
|
+
*
|
|
108
|
+
* Publishes the given payload to all subscribers of the given event type.
|
|
109
|
+
*
|
|
110
|
+
* The event type being published *must* be in the current namespace.
|
|
111
|
+
*
|
|
112
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
113
|
+
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
114
|
+
* match the type of this payload.
|
|
115
|
+
* @template E - A type union of Event type strings that are namespaced by N.
|
|
116
|
+
*/
|
|
117
|
+
publish(event, ...payload) {
|
|
118
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
119
|
+
if (!event.startsWith(`${this.controllerName}:`)) {
|
|
120
|
+
throw new Error(`Only allowed publishing events prefixed by '${this.controllerName}:'`);
|
|
121
|
+
}
|
|
122
|
+
this.controllerMessenger.publish(event, ...payload);
|
|
123
|
+
}
|
|
124
|
+
subscribe(event, handler, selector) {
|
|
125
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
126
|
+
if (this.allowedEvents === null) {
|
|
127
|
+
throw new Error('No events allowed');
|
|
128
|
+
}
|
|
129
|
+
else if (!this.allowedEvents.includes(event)) {
|
|
130
|
+
throw new Error(`Event missing from allow list: ${event}`);
|
|
131
|
+
}
|
|
132
|
+
if (selector) {
|
|
133
|
+
return this.controllerMessenger.subscribe(event, handler, selector);
|
|
134
|
+
}
|
|
135
|
+
return this.controllerMessenger.subscribe(event, handler);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Unsubscribe from an event.
|
|
139
|
+
*
|
|
140
|
+
* Unregisters the given function as an event handler for the given event.
|
|
141
|
+
*
|
|
142
|
+
* The event type being unsubscribed to must be on the event allowlist.
|
|
143
|
+
*
|
|
144
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
145
|
+
* @param handler - The event handler to unregister.
|
|
146
|
+
* @throws Will throw when the given event handler is not registered for this event.
|
|
147
|
+
* @template T - A type union of allowed Event type strings.
|
|
148
|
+
*/
|
|
149
|
+
unsubscribe(event, handler) {
|
|
150
|
+
/* istanbul ignore next */ // Branches unreachable with valid types
|
|
151
|
+
if (this.allowedEvents === null) {
|
|
152
|
+
throw new Error('No events allowed');
|
|
153
|
+
}
|
|
154
|
+
else if (!this.allowedEvents.includes(event)) {
|
|
155
|
+
throw new Error(`Event missing from allow list: ${event}`);
|
|
156
|
+
}
|
|
157
|
+
this.controllerMessenger.unsubscribe(event, handler);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Clear subscriptions for a specific event.
|
|
161
|
+
*
|
|
162
|
+
* This will remove all subscribed handlers for this event.
|
|
163
|
+
*
|
|
164
|
+
* The event type being cleared *must* be in the current namespace.
|
|
165
|
+
*
|
|
166
|
+
* @param event - The event type. This is a unique identifier for this event.
|
|
167
|
+
* @template E - A type union of Event type strings that are namespaced by N.
|
|
168
|
+
*/
|
|
169
|
+
clearEventSubscriptions(event) {
|
|
170
|
+
/* istanbul ignore if */ // Branch unreachable with valid types
|
|
171
|
+
if (!event.startsWith(`${this.controllerName}:`)) {
|
|
172
|
+
throw new Error(`Only allowed clearing events prefixed by '${this.controllerName}:'`);
|
|
173
|
+
}
|
|
174
|
+
this.controllerMessenger.clearEventSubscriptions(event);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.RestrictedControllerMessenger = RestrictedControllerMessenger;
|
|
178
|
+
/**
|
|
179
|
+
* A messaging system for controllers.
|
|
180
|
+
*
|
|
181
|
+
* The controller messenger allows registering functions as 'actions' that can be called elsewhere,
|
|
182
|
+
* and it allows publishing and subscribing to events. Both actions and events are identified by
|
|
183
|
+
* unique strings.
|
|
184
|
+
*
|
|
185
|
+
* @template Action - A type union of all Action types.
|
|
186
|
+
* @template Event - A type union of all Event types.
|
|
187
|
+
*/
|
|
188
|
+
class ControllerMessenger {
|
|
189
|
+
constructor() {
|
|
190
|
+
this.actions = new Map();
|
|
191
|
+
this.events = new Map();
|
|
192
|
+
/**
|
|
193
|
+
* A cache of selector return values for their respective handlers.
|
|
194
|
+
*/
|
|
195
|
+
this.eventPayloadCache = new Map();
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Register an action handler.
|
|
199
|
+
*
|
|
200
|
+
* This will make the registered function available to call via the `call` method.
|
|
201
|
+
*
|
|
202
|
+
* @param actionType - The action type. This is a unqiue identifier for this action.
|
|
203
|
+
* @param handler - The action handler. This function gets called when the `call` method is
|
|
204
|
+
* invoked with the given action type.
|
|
205
|
+
* @throws Will throw when a handler has been registered for this action type already.
|
|
206
|
+
* @template T - A type union of Action type strings.
|
|
207
|
+
*/
|
|
208
|
+
registerActionHandler(actionType, handler) {
|
|
209
|
+
if (this.actions.has(actionType)) {
|
|
210
|
+
throw new Error(`A handler for ${actionType} has already been registered`);
|
|
211
|
+
}
|
|
212
|
+
this.actions.set(actionType, handler);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Unregister an action handler.
|
|
216
|
+
*
|
|
217
|
+
* This will prevent this action from being called.
|
|
218
|
+
*
|
|
219
|
+
* @param actionType - The action type. This is a unqiue identifier for this action.
|
|
220
|
+
* @template T - A type union of Action type strings.
|
|
221
|
+
*/
|
|
222
|
+
unregisterActionHandler(actionType) {
|
|
223
|
+
this.actions.delete(actionType);
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Unregister all action handlers.
|
|
227
|
+
*
|
|
228
|
+
* This prevents all actions from being called.
|
|
229
|
+
*/
|
|
230
|
+
clearActions() {
|
|
231
|
+
this.actions.clear();
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Call an action.
|
|
235
|
+
*
|
|
236
|
+
* This function will call the action handler corresponding to the given action type, passing
|
|
237
|
+
* along any parameters given.
|
|
238
|
+
*
|
|
239
|
+
* @param actionType - The action type. This is a unqiue identifier for this action.
|
|
240
|
+
* @param params - The action parameters. These must match the type of the parameters of the
|
|
241
|
+
* registered action handler.
|
|
242
|
+
* @throws Will throw when no handler has been registered for the given type.
|
|
243
|
+
* @template T - A type union of Action type strings.
|
|
244
|
+
* @returns The action return value.
|
|
245
|
+
*/
|
|
246
|
+
call(actionType, ...params) {
|
|
247
|
+
const handler = this.actions.get(actionType);
|
|
248
|
+
if (!handler) {
|
|
249
|
+
throw new Error(`A handler for ${actionType} has not been registered`);
|
|
250
|
+
}
|
|
251
|
+
return handler(...params);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Publish an event.
|
|
255
|
+
*
|
|
256
|
+
* Publishes the given payload to all subscribers of the given event type.
|
|
257
|
+
*
|
|
258
|
+
* Note that this method should never throw directly. Any errors from
|
|
259
|
+
* subscribers are captured and re-thrown in a timeout handler.
|
|
260
|
+
*
|
|
261
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
262
|
+
* @param payload - The event payload. The type of the parameters for each event handler must
|
|
263
|
+
* match the type of this payload.
|
|
264
|
+
* @template E - A type union of Event type strings.
|
|
265
|
+
*/
|
|
266
|
+
publish(eventType, ...payload) {
|
|
267
|
+
const subscribers = this.events.get(eventType);
|
|
268
|
+
if (subscribers) {
|
|
269
|
+
for (const [handler, selector] of subscribers.entries()) {
|
|
270
|
+
try {
|
|
271
|
+
if (selector) {
|
|
272
|
+
const previousValue = this.eventPayloadCache.get(handler);
|
|
273
|
+
const newValue = selector(...payload);
|
|
274
|
+
if (newValue !== previousValue) {
|
|
275
|
+
this.eventPayloadCache.set(handler, newValue);
|
|
276
|
+
handler(newValue, previousValue);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
handler(...payload);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
catch (error) {
|
|
284
|
+
// Throw error after timeout so that it is capured as a console error
|
|
285
|
+
// (and by Sentry) without interrupting the event publishing.
|
|
286
|
+
setTimeout(() => {
|
|
287
|
+
throw error;
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
subscribe(eventType, handler, selector) {
|
|
294
|
+
let subscribers = this.events.get(eventType);
|
|
295
|
+
if (!subscribers) {
|
|
296
|
+
subscribers = new Map();
|
|
297
|
+
this.events.set(eventType, subscribers);
|
|
298
|
+
}
|
|
299
|
+
subscribers.set(handler, selector);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Unsubscribe from an event.
|
|
303
|
+
*
|
|
304
|
+
* Unregisters the given function as an event handler for the given event.
|
|
305
|
+
*
|
|
306
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
307
|
+
* @param handler - The event handler to unregister.
|
|
308
|
+
* @throws Will throw when the given event handler is not registered for this event.
|
|
309
|
+
* @template E - A type union of Event type strings.
|
|
310
|
+
*/
|
|
311
|
+
unsubscribe(eventType, handler) {
|
|
312
|
+
const subscribers = this.events.get(eventType);
|
|
313
|
+
if (!subscribers || !subscribers.has(handler)) {
|
|
314
|
+
throw new Error(`Subscription not found for event: ${eventType}`);
|
|
315
|
+
}
|
|
316
|
+
const selector = subscribers.get(handler);
|
|
317
|
+
if (selector) {
|
|
318
|
+
this.eventPayloadCache.delete(handler);
|
|
319
|
+
}
|
|
320
|
+
subscribers.delete(handler);
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Clear subscriptions for a specific event.
|
|
324
|
+
*
|
|
325
|
+
* This will remove all subscribed handlers for this event.
|
|
326
|
+
*
|
|
327
|
+
* @param eventType - The event type. This is a unique identifier for this event.
|
|
328
|
+
* @template E - A type union of Event type strings.
|
|
329
|
+
*/
|
|
330
|
+
clearEventSubscriptions(eventType) {
|
|
331
|
+
this.events.delete(eventType);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Clear all subscriptions.
|
|
335
|
+
*
|
|
336
|
+
* This will remove all subscribed handlers for all events.
|
|
337
|
+
*/
|
|
338
|
+
clearSubscriptions() {
|
|
339
|
+
this.events.clear();
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Get a restricted controller messenger
|
|
343
|
+
*
|
|
344
|
+
* Returns a wrapper around the controller messenger instance that restricts access to actions
|
|
345
|
+
* and events. The provided allowlists grant the ability to call the listed actions and subscribe
|
|
346
|
+
* to the listed events. The "name" provided grants ownership of any actions and events under
|
|
347
|
+
* that namespace. Ownership allows registering actions and publishing events, as well as
|
|
348
|
+
* unregistering actions and clearing event subscriptions.
|
|
349
|
+
*
|
|
350
|
+
* @param options - Controller messenger options.
|
|
351
|
+
* @param options.name - The name of the thing this messenger will be handed to (e.g. the
|
|
352
|
+
* controller name). This grants "ownership" of actions and events under this namespace to the
|
|
353
|
+
* restricted controller messenger returned.
|
|
354
|
+
* @param options.allowedActions - The list of actions that this restricted controller messenger
|
|
355
|
+
* should be alowed to call.
|
|
356
|
+
* @param options.allowedEvents - The list of events that this restricted controller messenger
|
|
357
|
+
* should be allowed to subscribe to.
|
|
358
|
+
* @template N - The namespace for this messenger. Typically this is the name of the controller or
|
|
359
|
+
* module that this messenger has been created for. The authority to publish events and register
|
|
360
|
+
* actions under this namespace is granted to this restricted messenger instance.
|
|
361
|
+
* @template AllowedAction - A type union of the 'type' string for any allowed actions.
|
|
362
|
+
* @template AllowedEvent - A type union of the 'type' string for any allowed events.
|
|
363
|
+
* @returns The restricted controller messenger.
|
|
364
|
+
*/
|
|
365
|
+
getRestricted({ name, allowedActions, allowedEvents, }) {
|
|
366
|
+
return new RestrictedControllerMessenger({
|
|
367
|
+
controllerMessenger: this,
|
|
368
|
+
name,
|
|
369
|
+
allowedActions,
|
|
370
|
+
allowedEvents,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
exports.ControllerMessenger = ControllerMessenger;
|
|
375
|
+
//# sourceMappingURL=ControllerMessenger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ControllerMessenger.js","sourceRoot":"","sources":["../src/ControllerMessenger.ts"],"names":[],"mappings":";;;AA4EA;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B,EAAE,IAAI;YACzB,IAAI;YACJ,cAAc;YACd,aAAa;SACd,CAAC,CAAC;IACL,CAAC;CACF;AA7RD,kDA6RC","sourcesContent":["export type ActionHandler<Action, ActionType> = (\n ...args: ExtractActionParameters<Action, ActionType>\n) => ExtractActionResponse<Action, ActionType>;\nexport type ExtractActionParameters<Action, T> = Action extends {\n type: T;\n handler: (...args: infer H) => any;\n}\n ? H\n : never;\nexport type ExtractActionResponse<Action, T> = Action extends {\n type: T;\n handler: (...args: any) => infer H;\n}\n ? H\n : never;\n\nexport type ExtractEventHandler<Event, T> = Event extends {\n type: T;\n payload: infer P;\n}\n ? P extends unknown[]\n ? (...payload: P) => void\n : never\n : never;\nexport type ExtractEventPayload<Event, T> = Event extends {\n type: T;\n payload: infer P;\n}\n ? P\n : never;\n\nexport type GenericEventHandler = (...args: unknown[]) => void;\n\nexport type SelectorFunction<Args extends unknown[], ReturnValue> = (\n ...args: Args\n) => ReturnValue;\nexport type SelectorEventHandler<SelectorReturnValue> = (\n newValue: SelectorReturnValue,\n previousValue: SelectorReturnValue | undefined,\n) => void;\n\nexport type ActionConstraint = {\n type: string;\n handler: (...args: any) => unknown;\n};\nexport type EventConstraint = { type: string; payload: unknown[] };\n\ntype EventSubscriptionMap = Map<\n GenericEventHandler | SelectorEventHandler<unknown>,\n SelectorFunction<any, unknown> | undefined\n>;\n\n/**\n * A namespaced string\n *\n * This type verifies that the string T is prefixed by the string Name followed by a colon.\n *\n * @template Name - The namespace we're checking for.\n * @template T - The full string, including the namespace.\n */\nexport type Namespaced<Name extends string, T> = T extends `${Name}:${string}`\n ? T\n : never;\n\ntype NarrowToNamespace<T, Namespace extends string> = T extends {\n type: `${Namespace}:${string}`;\n}\n ? T\n : never;\n\ntype NarrowToAllowed<T, Allowed extends string> = T extends {\n type: Allowed;\n}\n ? T\n : never;\n\n/**\n * A restricted controller messenger.\n *\n * This acts as a wrapper around the controller messenger instance that restricts access to actions\n * and events.\n *\n * @template N - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n */\nexport class RestrictedControllerMessenger<\n N extends string,\n Action extends ActionConstraint,\n Event extends EventConstraint,\n AllowedAction extends string,\n AllowedEvent extends string,\n> {\n private readonly controllerMessenger: ControllerMessenger<\n ActionConstraint,\n EventConstraint\n >;\n\n private readonly controllerName: N;\n\n private readonly allowedActions: AllowedAction[] | null;\n\n private readonly allowedEvents: AllowedEvent[] | null;\n\n /**\n * Constructs a restricted controller messenger\n *\n * The provided allowlists grant the ability to call the listed actions and subscribe to the\n * listed events. The \"name\" provided grants ownership of any actions and events under that\n * namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - The controller options.\n * @param options.controllerMessenger - The controller messenger instance that is being wrapped.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n */\n constructor({\n controllerMessenger,\n name,\n allowedActions,\n allowedEvents,\n }: {\n controllerMessenger: ControllerMessenger<ActionConstraint, EventConstraint>;\n name: N;\n allowedActions?: AllowedAction[];\n allowedEvents?: AllowedEvent[];\n }) {\n this.controllerMessenger = controllerMessenger;\n this.controllerName = name;\n this.allowedActions = allowedActions || null;\n this.allowedEvents = allowedEvents || null;\n }\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * The action type this handler is registered under *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template T - A type union of Action type strings that are namespaced by N.\n */\n registerActionHandler<T extends Namespaced<N, Action['type']>>(\n action: T,\n handler: ActionHandler<Action, T>,\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!action.startsWith(`${this.controllerName}:`)) {\n throw new Error(\n `Only allowed registering action handlers prefixed by '${this.controllerName}:'`,\n );\n }\n this.controllerMessenger.registerActionHandler(action, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * The action type being unregistered *must* be in the current namespace.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @template T - A type union of Action type strings that are namespaced by N.\n */\n unregisterActionHandler<T extends Namespaced<N, Action['type']>>(action: T) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!action.startsWith(`${this.controllerName}:`)) {\n throw new Error(\n `Only allowed unregistering action handlers prefixed by '${this.controllerName}:'`,\n );\n }\n this.controllerMessenger.unregisterActionHandler(action);\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * The action type being called must be on the action allowlist.\n *\n * @param action - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template T - A type union of allowed Action type strings.\n * @returns The action return value.\n */\n call<T extends AllowedAction & string>(\n action: T,\n ...params: ExtractActionParameters<Action, T>\n ): ExtractActionResponse<Action, T> {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.allowedActions === null) {\n throw new Error('No actions allowed');\n } else if (!this.allowedActions.includes(action)) {\n throw new Error(`Action missing from allow list: ${action}`);\n }\n return this.controllerMessenger.call(action, ...params);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * The event type being published *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template E - A type union of Event type strings that are namespaced by N.\n */\n publish<E extends Namespaced<N, Event['type']>>(\n event: E,\n ...payload: ExtractEventPayload<Event, E>\n ) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!event.startsWith(`${this.controllerName}:`)) {\n throw new Error(\n `Only allowed publishing events prefixed by '${this.controllerName}:'`,\n );\n }\n this.controllerMessenger.publish(event, ...payload);\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template E - A type union of Event type strings.\n */\n subscribe<E extends AllowedEvent & string>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * The event type being subscribed to must be on the event allowlist.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template E - A type union of Event type strings.\n * @template V - The selector return value.\n */\n subscribe<E extends AllowedEvent & string, V>(\n eventType: E,\n handler: SelectorEventHandler<V>,\n selector: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ): void;\n\n subscribe<E extends AllowedEvent & string, V>(\n event: E,\n handler: ExtractEventHandler<Event, E>,\n selector?: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ) {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.allowedEvents === null) {\n throw new Error('No events allowed');\n } else if (!this.allowedEvents.includes(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n\n if (selector) {\n return this.controllerMessenger.subscribe(event, handler, selector);\n }\n return this.controllerMessenger.subscribe(event, handler);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * The event type being unsubscribed to must be on the event allowlist.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template T - A type union of allowed Event type strings.\n */\n unsubscribe<E extends AllowedEvent & string>(\n event: E,\n handler: ExtractEventHandler<Event, E>,\n ) {\n /* istanbul ignore next */ // Branches unreachable with valid types\n if (this.allowedEvents === null) {\n throw new Error('No events allowed');\n } else if (!this.allowedEvents.includes(event)) {\n throw new Error(`Event missing from allow list: ${event}`);\n }\n this.controllerMessenger.unsubscribe(event, handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * The event type being cleared *must* be in the current namespace.\n *\n * @param event - The event type. This is a unique identifier for this event.\n * @template E - A type union of Event type strings that are namespaced by N.\n */\n clearEventSubscriptions<E extends Namespaced<N, Event['type']>>(event: E) {\n /* istanbul ignore if */ // Branch unreachable with valid types\n if (!event.startsWith(`${this.controllerName}:`)) {\n throw new Error(\n `Only allowed clearing events prefixed by '${this.controllerName}:'`,\n );\n }\n this.controllerMessenger.clearEventSubscriptions(event);\n }\n}\n\n/**\n * A messaging system for controllers.\n *\n * The controller messenger allows registering functions as 'actions' that can be called elsewhere,\n * and it allows publishing and subscribing to events. Both actions and events are identified by\n * unique strings.\n *\n * @template Action - A type union of all Action types.\n * @template Event - A type union of all Event types.\n */\nexport class ControllerMessenger<\n Action extends ActionConstraint,\n Event extends EventConstraint,\n> {\n private readonly actions = new Map<Action['type'], unknown>();\n\n private readonly events = new Map<Event['type'], EventSubscriptionMap>();\n\n /**\n * A cache of selector return values for their respective handlers.\n */\n private readonly eventPayloadCache = new Map<\n GenericEventHandler,\n unknown | undefined\n >();\n\n /**\n * Register an action handler.\n *\n * This will make the registered function available to call via the `call` method.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param handler - The action handler. This function gets called when the `call` method is\n * invoked with the given action type.\n * @throws Will throw when a handler has been registered for this action type already.\n * @template T - A type union of Action type strings.\n */\n registerActionHandler<T extends Action['type']>(\n actionType: T,\n handler: ActionHandler<Action, T>,\n ) {\n if (this.actions.has(actionType)) {\n throw new Error(\n `A handler for ${actionType} has already been registered`,\n );\n }\n this.actions.set(actionType, handler);\n }\n\n /**\n * Unregister an action handler.\n *\n * This will prevent this action from being called.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @template T - A type union of Action type strings.\n */\n unregisterActionHandler<T extends Action['type']>(actionType: T) {\n this.actions.delete(actionType);\n }\n\n /**\n * Unregister all action handlers.\n *\n * This prevents all actions from being called.\n */\n clearActions() {\n this.actions.clear();\n }\n\n /**\n * Call an action.\n *\n * This function will call the action handler corresponding to the given action type, passing\n * along any parameters given.\n *\n * @param actionType - The action type. This is a unqiue identifier for this action.\n * @param params - The action parameters. These must match the type of the parameters of the\n * registered action handler.\n * @throws Will throw when no handler has been registered for the given type.\n * @template T - A type union of Action type strings.\n * @returns The action return value.\n */\n call<T extends Action['type']>(\n actionType: T,\n ...params: ExtractActionParameters<Action, T>\n ): ExtractActionResponse<Action, T> {\n const handler = this.actions.get(actionType) as ActionHandler<Action, T>;\n if (!handler) {\n throw new Error(`A handler for ${actionType} has not been registered`);\n }\n return handler(...params);\n }\n\n /**\n * Publish an event.\n *\n * Publishes the given payload to all subscribers of the given event type.\n *\n * Note that this method should never throw directly. Any errors from\n * subscribers are captured and re-thrown in a timeout handler.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param payload - The event payload. The type of the parameters for each event handler must\n * match the type of this payload.\n * @template E - A type union of Event type strings.\n */\n publish<E extends Event['type']>(\n eventType: E,\n ...payload: ExtractEventPayload<Event, E>\n ) {\n const subscribers = this.events.get(eventType);\n\n if (subscribers) {\n for (const [handler, selector] of subscribers.entries()) {\n try {\n if (selector) {\n const previousValue = this.eventPayloadCache.get(handler);\n const newValue = selector(...payload);\n\n if (newValue !== previousValue) {\n this.eventPayloadCache.set(handler, newValue);\n handler(newValue, previousValue);\n }\n } else {\n (handler as GenericEventHandler)(...payload);\n }\n } catch (error) {\n // Throw error after timeout so that it is capured as a console error\n // (and by Sentry) without interrupting the event publishing.\n setTimeout(() => {\n throw error;\n });\n }\n }\n }\n }\n\n /**\n * Subscribe to an event.\n *\n * Registers the given function as an event handler for the given event type.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event handler must\n * match the type of the payload for this event type.\n * @template E - A type union of Event type strings.\n */\n subscribe<E extends Event['type']>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\n ): void;\n\n /**\n * Subscribe to an event, with a selector.\n *\n * Registers the given handler function as an event handler for the given\n * event type. When an event is published, its payload is first passed to the\n * selector. The event handler is only called if the selector's return value\n * differs from its last known return value.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler. The type of the parameters for this event\n * handler must match the return type of the selector.\n * @param selector - The selector function used to select relevant data from\n * the event payload. The type of the parameters for this selector must match\n * the type of the payload for this event type.\n * @template E - A type union of Event type strings.\n * @template V - The selector return value.\n */\n subscribe<E extends Event['type'], V>(\n eventType: E,\n handler: SelectorEventHandler<V>,\n selector: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ): void;\n\n subscribe<E extends Event['type'], V>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\n selector?: SelectorFunction<ExtractEventPayload<Event, E>, V>,\n ): void {\n let subscribers = this.events.get(eventType);\n if (!subscribers) {\n subscribers = new Map();\n this.events.set(eventType, subscribers);\n }\n\n subscribers.set(handler, selector);\n }\n\n /**\n * Unsubscribe from an event.\n *\n * Unregisters the given function as an event handler for the given event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @param handler - The event handler to unregister.\n * @throws Will throw when the given event handler is not registered for this event.\n * @template E - A type union of Event type strings.\n */\n unsubscribe<E extends Event['type']>(\n eventType: E,\n handler: ExtractEventHandler<Event, E>,\n ) {\n const subscribers = this.events.get(eventType);\n\n if (!subscribers || !subscribers.has(handler)) {\n throw new Error(`Subscription not found for event: ${eventType}`);\n }\n\n const selector = subscribers.get(handler);\n if (selector) {\n this.eventPayloadCache.delete(handler);\n }\n\n subscribers.delete(handler);\n }\n\n /**\n * Clear subscriptions for a specific event.\n *\n * This will remove all subscribed handlers for this event.\n *\n * @param eventType - The event type. This is a unique identifier for this event.\n * @template E - A type union of Event type strings.\n */\n clearEventSubscriptions<E extends Event['type']>(eventType: E) {\n this.events.delete(eventType);\n }\n\n /**\n * Clear all subscriptions.\n *\n * This will remove all subscribed handlers for all events.\n */\n clearSubscriptions() {\n this.events.clear();\n }\n\n /**\n * Get a restricted controller messenger\n *\n * Returns a wrapper around the controller messenger instance that restricts access to actions\n * and events. The provided allowlists grant the ability to call the listed actions and subscribe\n * to the listed events. The \"name\" provided grants ownership of any actions and events under\n * that namespace. Ownership allows registering actions and publishing events, as well as\n * unregistering actions and clearing event subscriptions.\n *\n * @param options - Controller messenger options.\n * @param options.name - The name of the thing this messenger will be handed to (e.g. the\n * controller name). This grants \"ownership\" of actions and events under this namespace to the\n * restricted controller messenger returned.\n * @param options.allowedActions - The list of actions that this restricted controller messenger\n * should be alowed to call.\n * @param options.allowedEvents - The list of events that this restricted controller messenger\n * should be allowed to subscribe to.\n * @template N - The namespace for this messenger. Typically this is the name of the controller or\n * module that this messenger has been created for. The authority to publish events and register\n * actions under this namespace is granted to this restricted messenger instance.\n * @template AllowedAction - A type union of the 'type' string for any allowed actions.\n * @template AllowedEvent - A type union of the 'type' string for any allowed events.\n * @returns The restricted controller messenger.\n */\n getRestricted<\n N extends string,\n AllowedAction extends string,\n AllowedEvent extends string,\n >({\n name,\n allowedActions,\n allowedEvents,\n }: {\n name: N;\n allowedActions?: Extract<Action['type'], AllowedAction>[];\n allowedEvents?: Extract<Event['type'], AllowedEvent>[];\n }): RestrictedControllerMessenger<\n N,\n NarrowToNamespace<Action, N> | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, N> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n > {\n return new RestrictedControllerMessenger<\n N,\n NarrowToNamespace<Action, N> | NarrowToAllowed<Action, AllowedAction>,\n NarrowToNamespace<Event, N> | NarrowToAllowed<Event, AllowedEvent>,\n AllowedAction,\n AllowedEvent\n >({\n controllerMessenger: this,\n name,\n allowedActions,\n allowedEvents,\n });\n }\n}\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type { BaseConfig, BaseState, Listener } from './BaseController';
|
|
2
|
+
export { BaseController } from './BaseController';
|
|
3
|
+
export type { Listener as ListenerV2, StateDeriver, StateMetadata, StatePropertyMetadata, } from './BaseControllerV2';
|
|
4
|
+
export { BaseController as BaseControllerV2, getAnonymizedState, getPersistentState, } from './BaseControllerV2';
|
|
5
|
+
export * from './ControllerMessenger';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,YAAY,EACV,QAAQ,IAAI,UAAU,EACtB,YAAY,EACZ,aAAa,EACb,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,cAAc,IAAI,gBAAgB,EAClC,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAC5B,cAAc,uBAAuB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.getPersistentState = exports.getAnonymizedState = exports.BaseControllerV2 = exports.BaseController = void 0;
|
|
18
|
+
var BaseController_1 = require("./BaseController");
|
|
19
|
+
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return BaseController_1.BaseController; } });
|
|
20
|
+
var BaseControllerV2_1 = require("./BaseControllerV2");
|
|
21
|
+
Object.defineProperty(exports, "BaseControllerV2", { enumerable: true, get: function () { return BaseControllerV2_1.BaseController; } });
|
|
22
|
+
Object.defineProperty(exports, "getAnonymizedState", { enumerable: true, get: function () { return BaseControllerV2_1.getAnonymizedState; } });
|
|
23
|
+
Object.defineProperty(exports, "getPersistentState", { enumerable: true, get: function () { return BaseControllerV2_1.getPersistentState; } });
|
|
24
|
+
__exportStar(require("./ControllerMessenger"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AACA,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AAOvB,uDAI4B;AAH1B,oHAAA,cAAc,OAAoB;AAClC,sHAAA,kBAAkB,OAAA;AAClB,sHAAA,kBAAkB,OAAA;AAEpB,wDAAsC","sourcesContent":["export type { BaseConfig, BaseState, Listener } from './BaseController';\nexport { BaseController } from './BaseController';\nexport type {\n Listener as ListenerV2,\n StateDeriver,\n StateMetadata,\n StatePropertyMetadata,\n} from './BaseControllerV2';\nexport {\n BaseController as BaseControllerV2,\n getAnonymizedState,\n getPersistentState,\n} from './BaseControllerV2';\nexport * from './ControllerMessenger';\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@metamask-previews/base-controller",
|
|
3
|
+
"version": "3.2.0-preview.d32a7cc",
|
|
4
|
+
"description": "Provides scaffolding for controllers as well a communication system for all controllers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"MetaMask",
|
|
7
|
+
"Ethereum"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/base-controller#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/core/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/MetaMask/core.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"main": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build:docs": "typedoc",
|
|
25
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/base-controller",
|
|
26
|
+
"publish:preview": "yarn npm publish --tag preview",
|
|
27
|
+
"test": "jest",
|
|
28
|
+
"test:watch": "jest --watch"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@metamask/utils": "^6.2.0",
|
|
32
|
+
"immer": "^9.0.6"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@metamask/auto-changelog": "^3.1.0",
|
|
36
|
+
"@types/jest": "^27.4.1",
|
|
37
|
+
"@types/sinon": "^9.0.10",
|
|
38
|
+
"deepmerge": "^4.2.2",
|
|
39
|
+
"jest": "^27.5.1",
|
|
40
|
+
"sinon": "^9.2.4",
|
|
41
|
+
"ts-jest": "^27.1.4",
|
|
42
|
+
"typedoc": "^0.22.15",
|
|
43
|
+
"typedoc-plugin-missing-exports": "^0.22.6",
|
|
44
|
+
"typescript": "~4.6.3"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=16.0.0"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"registry": "https://registry.npmjs.org/"
|
|
52
|
+
}
|
|
53
|
+
}
|