/
Applications.js
73 lines (63 loc) · 2.86 KB
/
Applications.js
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
import WebDeviceInterface from './web_device_interface.js'
import FakeWebCrypto from './fake_web_crypto.js'
import * as Defaults from './Defaults.js'
export function createApplicationWithOptions({ identifier, environment, platform, host, crypto, device, syncCallsThresholdPerMinute }) {
if (!device) {
device = new WebDeviceInterface()
device.environment = environment
}
return new SNApplication({
environment: environment || Environment.Web,
platform: platform || Platform.MacWeb,
deviceInterface: device,
crypto: crypto || new FakeWebCrypto(),
alertService: {
confirm: async () => true,
alert: async () => {},
blockingDialog: () => () => {},
},
identifier: identifier || `${Math.random()}`,
defaultHost: host || Defaults.getDefaultHost(),
appVersion: Defaults.getAppVersion(),
webSocketUrl: Defaults.getDefaultWebSocketUrl(),
apiVersion: ApiVersion.v0,
syncCallsThresholdPerMinute,
})
}
export function createApplication(identifier, environment, platform, host, crypto, syncCallsThresholdPerMinute) {
return createApplicationWithOptions({ identifier, environment, platform, host, crypto, syncCallsThresholdPerMinute })
}
export function createApplicationWithFakeCrypto(identifier, environment, platform, host) {
return createApplication(identifier, environment, platform, host, new FakeWebCrypto())
}
export function createApplicationWithRealCrypto(identifier, environment, platform, host) {
return createApplication(identifier, environment, platform, host, new SNWebCrypto())
}
export async function createAppWithRandNamespace(environment, platform) {
const namespace = Math.random().toString(36).substring(2, 15)
return createApplication(namespace, environment, platform)
}
export async function createInitAppWithFakeCrypto(environment, platform) {
const namespace = Math.random().toString(36).substring(2, 15)
return createAndInitializeApplication(namespace, environment, platform, undefined, new FakeWebCrypto())
}
export async function createInitAppWithRealCrypto(environment, platform) {
const namespace = Math.random().toString(36).substring(2, 15)
return createAndInitializeApplication(namespace, environment, platform, undefined, new SNWebCrypto())
}
export async function createAndInitializeApplication(namespace, environment, platform, host, crypto) {
const application = createApplication(namespace, environment, platform, host, crypto)
await initializeApplication(application)
return application
}
export async function initializeApplication(application) {
await application.prepareForLaunch({
receiveChallenge: (challenge) => {
console.warn('Factory received potentially unhandled challenge', challenge)
if (challenge.reason !== ChallengeReason.Custom) {
throw Error("Factory application shouldn't have challenges")
}
},
})
await application.launch(true)
}