forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.ts
224 lines (202 loc) · 7.67 KB
/
background.ts
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import 'core-js'
import browser from 'webextension-polyfill'
import initStorex from './search/memex-storex'
import getDb, { setStorex } from './search/get-db'
import {
setupRpcConnection,
setupRemoteFunctionsImplementations,
} from 'src/util/webextensionRPC'
import { StorageChangesManager } from 'src/util/storage-changes'
// Features that require manual instantiation to setup
import createNotification from 'src/util/notifications'
// Features that auto-setup
import './analytics/background'
import './imports/background'
// import './omnibar'
import analytics from './analytics'
import {
createBackgroundModules,
setupBackgroundModules,
registerBackgroundModuleCollections,
} from './background-script/setup'
import { FetchPageDataProcessor } from 'src/page-analysis/background/fetch-page-data-processor'
import fetchPageData from 'src/page-analysis/background/fetch-page-data'
import pipeline from 'src/search/pipeline'
import { setStorageMiddleware } from './storage/middleware'
import { getFirebase } from './util/firebase-app-initialized'
import setupDataSeeders from 'src/util/tests/seed-data'
import {
createLazyServerStorage,
createServerStorageManager,
} from './storage/server'
import { createServices } from './services'
import initSentry, { captureException } from 'src/util/raven'
import { createSelfTests } from './tests/self-tests'
import { createPersistentStorageManager } from './storage/persistent-storage'
import { createAuthServices } from './services/local-services'
import {
SharedListKey,
SharedListRoleID,
} from '@worldbrain/memex-common/lib/content-sharing/types'
import { initFirestoreSyncTriggerListener } from '@worldbrain/memex-common/lib/personal-cloud/backend/utils'
let __debugCounter = 0
export async function main() {
const rpcManager = setupRpcConnection({
sideName: 'background',
role: 'background',
paused: true,
})
const firebase = getFirebase()
const localStorageChangesManager = new StorageChangesManager({
storage: browser.storage,
})
initSentry({})
if (process.env.USE_FIREBASE_EMULATOR === 'true') {
firebase.firestore().settings({
host: 'localhost:8080',
ssl: false,
})
firebase.database().useEmulator('localhost', 9000)
firebase.firestore().useEmulator('localhost', 8080)
firebase.auth().useEmulator('http://localhost:9099/')
firebase.functions().useEmulator('localhost', 5001)
firebase.storage().useEmulator('localhost', 9199)
}
const getServerStorage = createLazyServerStorage(
createServerStorageManager,
{
autoPkType: 'string',
},
)
const fetchPageDataProcessor = new FetchPageDataProcessor({
fetchPageData,
pagePipeline: pipeline,
})
const storageManager = initStorex()
const persistentStorageManager = createPersistentStorageManager({
idbImplementation: {
factory: self.indexedDB,
range: self.IDBKeyRange,
},
})
const authServices = createAuthServices({
backend: process.env.NODE_ENV === 'test' ? 'memory' : 'firebase',
getServerStorage,
})
const servicesPromise = createServices({
backend: process.env.NODE_ENV === 'test' ? 'memory' : 'firebase',
getServerStorage,
authService: authServices.auth,
})
__debugCounter++
const backgroundModules = createBackgroundModules({
manifestVersion: '2',
authServices,
servicesPromise,
getServerStorage,
analyticsManager: analytics,
localStorageChangesManager,
fetchPageDataProcessor,
browserAPIs: browser,
captureException,
storageManager,
persistentStorageManager,
callFirebaseFunction: async <Returns>(name: string, ...args: any[]) => {
const callable = firebase.functions().httpsCallable(name)
const result = await callable(...args)
return result.data as Promise<Returns>
},
setupSyncTriggerListener: initFirestoreSyncTriggerListener(firebase),
})
__debugCounter++
registerBackgroundModuleCollections({
storageManager,
persistentStorageManager,
backgroundModules,
})
await storageManager.finishInitialization()
await persistentStorageManager.finishInitialization()
__debugCounter++
__debugCounter++
const { setStorageLoggingEnabled } = setStorageMiddleware(storageManager, {
storexHub: backgroundModules.storexHub,
contentSharing: backgroundModules.contentSharing,
personalCloud: backgroundModules.personalCloud,
})
__debugCounter++
await setupBackgroundModules(backgroundModules, storageManager)
__debugCounter++
navigator?.storage
?.persist?.()
.catch((err) =>
captureException(
new Error(
`Error occurred on navigator.storage.persist() call: ${err.message}`,
),
),
)
__debugCounter++
setStorex(storageManager)
// Gradually moving all remote function registrations here
setupRemoteFunctionsImplementations({
auth: backgroundModules.auth.remoteFunctions,
analytics: backgroundModules.analytics.remoteFunctions,
subscription: {
getCheckoutLink:
backgroundModules.auth.subscriptionService.getCheckoutLink,
getManageLink:
backgroundModules.auth.subscriptionService.getManageLink,
getCurrentUserClaims:
backgroundModules.auth.subscriptionService.getCurrentUserClaims,
},
notifications: { create: createNotification } as any,
bookmarks: backgroundModules.bookmarks.remoteFunctions,
// features: backgroundModules.features,
featuresBeta: backgroundModules.featuresBeta,
tags: backgroundModules.tags.remoteFunctions,
collections: backgroundModules.customLists.remoteFunctions,
pageActivityIndicator:
backgroundModules.pageActivityIndicator.remoteFunctions,
readablePageArchives: backgroundModules.readable.remoteFunctions,
copyPaster: backgroundModules.copyPaster.remoteFunctions,
contentSharing: backgroundModules.contentSharing.remoteFunctions,
personalCloud: backgroundModules.personalCloud.remoteFunctions,
pdf: backgroundModules.pdfBg.remoteFunctions,
})
__debugCounter++
const services = await servicesPromise
services.contentSharing.preKeyGeneration = async (params: {
key: Pick<SharedListKey, 'roleID' | 'disabled'>
}) => {
if (params.key.roleID > SharedListRoleID.Commenter) {
await backgroundModules.personalCloud.waitForSync()
}
}
__debugCounter++
// Attach interesting features onto global globalThis scope for interested users
globalThis['getDb'] = getDb
globalThis['storageMan'] = storageManager
globalThis['bgModules'] = backgroundModules
globalThis['analytics'] = analytics
globalThis['dataSeeders'] = setupDataSeeders(storageManager)
globalThis['setStorageLoggingEnabled'] = setStorageLoggingEnabled
globalThis['selfTests'] = createSelfTests({
backgroundModules,
storageManager,
persistentStorageManager,
getServerStorage,
localStorage: browser.storage.local,
})
rpcManager.unpause()
__debugCounter++
}
main().catch((originalError) => {
const error = new Error(
`Error occurred during background script setup: ${originalError.message} - debug counter: ${__debugCounter}`,
)
if (originalError.stack) {
error.stack = originalError.stack
}
captureException(error)
throw originalError
})