-
Notifications
You must be signed in to change notification settings - Fork 650
Expand file tree
/
Copy pathcommonEndowmentFactory.ts
More file actions
95 lines (87 loc) · 3.03 KB
/
commonEndowmentFactory.ts
File metadata and controls
95 lines (87 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { NotifyFunction } from '../BaseSnapExecutor';
import { rootRealmGlobal } from '../globalObject';
import consoleEndowment from './console';
import crypto from './crypto';
import date from './date';
import interval from './interval';
import math from './math';
import network from './network';
import textDecoder from './textDecoder';
import textEncoder from './textEncoder';
import timeout from './timeout';
export type EndowmentFactoryOptions = {
snapId?: string;
notify?: NotifyFunction;
};
export type EndowmentFactory = {
names: readonly string[];
factory: (options?: EndowmentFactoryOptions) => { [key: string]: unknown };
};
export type CommonEndowmentSpecification = {
endowment: unknown;
name: string;
bind?: boolean;
};
// Array of common endowments
const commonEndowments: CommonEndowmentSpecification[] = [
{ endowment: AbortController, name: 'AbortController' },
{ endowment: AbortSignal, name: 'AbortSignal' },
{ endowment: ArrayBuffer, name: 'ArrayBuffer' },
{ endowment: atob, name: 'atob', bind: true },
{ endowment: BigInt, name: 'BigInt' },
{ endowment: BigInt64Array, name: 'BigInt64Array' },
{ endowment: BigUint64Array, name: 'BigUint64Array' },
{ endowment: btoa, name: 'btoa', bind: true },
{ endowment: DataView, name: 'DataView' },
{ endowment: Float32Array, name: 'Float32Array' },
{ endowment: Float64Array, name: 'Float64Array' },
{ endowment: Intl, name: 'Intl' },
{ endowment: Int8Array, name: 'Int8Array' },
{ endowment: Int16Array, name: 'Int16Array' },
{ endowment: Int32Array, name: 'Int32Array' },
{ endowment: globalThis.isSecureContext, name: 'isSecureContext' },
{ endowment: Uint8Array, name: 'Uint8Array' },
{ endowment: Uint8ClampedArray, name: 'Uint8ClampedArray' },
{ endowment: Uint16Array, name: 'Uint16Array' },
{ endowment: Uint32Array, name: 'Uint32Array' },
{ endowment: URL, name: 'URL' },
{ endowment: WebAssembly, name: 'WebAssembly' },
];
/**
* Creates a consolidated collection of common endowments.
* This function will return factories for all common endowments including
* the additionally attenuated. All hardened with SES.
*
* @returns An object with common endowments.
*/
const buildCommonEndowments = (): EndowmentFactory[] => {
const endowmentFactories: EndowmentFactory[] = [
crypto,
interval,
math,
network,
timeout,
textDecoder,
textEncoder,
date,
consoleEndowment,
];
commonEndowments.forEach((endowmentSpecification) => {
const endowment = {
names: [endowmentSpecification.name] as const,
factory: () => {
const boundEndowment =
typeof endowmentSpecification.endowment === 'function' &&
endowmentSpecification.bind
? endowmentSpecification.endowment.bind(rootRealmGlobal)
: endowmentSpecification.endowment;
return {
[endowmentSpecification.name]: harden(boundEndowment),
} as const;
},
};
endowmentFactories.push(endowment);
});
return endowmentFactories;
};
export default buildCommonEndowments;