-
Notifications
You must be signed in to change notification settings - Fork 342
/
background.ts
270 lines (244 loc) · 9.43 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import browser from 'webextension-polyfill'
import { transformPageHTML } from '@worldbrain/memex-stemmer/lib/transform-page-html.service-worker'
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 { setStorageMiddleware } from './storage/middleware'
import { getFirebase } from './util/firebase-app-initialized'
import setupDataSeeders from 'src/util/tests/seed-data'
import {
createServerStorage,
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 { SharedListRoleID } from '@worldbrain/memex-common/lib/content-sharing/types'
import { initFirestoreSyncTriggerListener } from '@worldbrain/memex-common/lib/personal-cloud/backend/utils-mv2'
import { setupOmnibar } from 'src/omnibar'
import delay from './util/delay'
import { fetchPageData } from '@worldbrain/memex-common/lib/page-indexing/fetch-page-data'
import fetchAndExtractPdfContent from '@worldbrain/memex-common/lib/page-indexing/fetch-page-data/fetch-pdf-data.browser'
import { CloudflareImageSupportBackend } from '@worldbrain/memex-common/lib/image-support/backend'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'
let __debugCounter = 0
let __BGInitAttemptCounter = 0
const __maxBGInitRetryAttempts = 20
export async function main(): Promise<void> {
__debugCounter = 0
const rpcManager = setupRpcConnection({
browserAPIs: browser,
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 serverStorageManager = createServerStorageManager()
const serverStorage = await createServerStorage(serverStorageManager, {
autoPkType: 'string',
})
const storageManager = initStorex()
const persistentStorageManager = createPersistentStorageManager({
idbImplementation: {
factory: self.indexedDB,
range: self.IDBKeyRange,
},
})
const authServices = createAuthServices({
backend: process.env.NODE_ENV === 'test' ? 'memory' : 'firebase',
})
const services = createServices({
backend: process.env.NODE_ENV === 'test' ? 'memory' : 'firebase',
serverStorage,
authService: authServices.auth,
})
__debugCounter++
const fetch = globalThis.fetch.bind(
globalThis,
) as typeof globalThis['fetch']
const backgroundModules = createBackgroundModules({
manifestVersion: '2',
authServices,
services,
serverStorage,
analyticsManager: analytics,
localStorageChangesManager,
fetchPageData: async (url) =>
fetchPageData({
url,
fetch,
transformPageHTML,
domParser: (html) =>
new DOMParser().parseFromString(html, 'text/html'),
opts: { includePageContent: true, includeFavIcon: true },
}).run(),
fetchPDFData: async (url) =>
fetchAndExtractPdfContent(url, {
fetch,
pdfJSWorkerSrc: browser.runtime.getURL('/build/pdf.worker.js'),
}),
fetch,
browserAPIs: browser,
captureException,
storageManager,
persistentStorageManager,
callFirebaseFunction: async <Returns>(name: string, ...args: any[]) => {
const callable = firebase.functions().httpsCallable(name)
try {
const result = await callable(...args)
return result.data as Promise<Returns>
} catch (err) {
captureException(err, {
extra: {
name,
...args,
},
})
throw err
}
},
setupSyncTriggerListener: initFirestoreSyncTriggerListener(firebase),
imageSupportBackend: new CloudflareImageSupportBackend({
env:
process.env.NODE_ENV === 'production'
? 'production'
: 'staging',
}),
backendEnv:
process.env.NODE_ENV === 'production' ? 'production' : 'staging',
})
__debugCounter++
registerBackgroundModuleCollections({
storageManager,
persistentStorageManager,
backgroundModules,
})
await storageManager.finishInitialization()
await persistentStorageManager.finishInitialization()
__debugCounter++
__debugCounter++
const { setStorageLoggingEnabled } = setStorageMiddleware(
storageManager,
backgroundModules,
)
__debugCounter++
await setupBackgroundModules(backgroundModules, storageManager, browser)
__debugCounter++
navigator?.storage
?.persist?.()
.catch((err) =>
captureException(
new Error(
`Error occurred on navigator.storage.persist() call: ${err.message}`,
),
),
)
__debugCounter++
setStorex(storageManager)
setupOmnibar({
bgModules: backgroundModules,
browserAPIs: browser,
})
// 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,
analyticsBG: backgroundModules.analyticsBG,
})
__debugCounter++
// Attach interesting features onto global globalThis scope for interested users
globalThis['getDb'] = getDb
globalThis['storageMan'] = storageManager
globalThis['bgModules'] = backgroundModules
globalThis['serverStorage'] = serverStorage
globalThis['services'] = services
globalThis['analytics'] = analytics
globalThis['dataSeeders'] = setupDataSeeders(storageManager)
globalThis['setStorageLoggingEnabled'] = setStorageLoggingEnabled
globalThis['normalizeUrl'] = normalizeUrl
if (process.env.NODE_ENV === 'development') {
globalThis['selfTests'] = createSelfTests({
serverStorage,
storageManager,
backgroundModules,
persistentStorageManager,
localStorage: browser.storage.local,
})
}
rpcManager.unpause()
__debugCounter++
}
const handleError = async (originalError: Error) => {
const noMoreAttempts = __BGInitAttemptCounter++ >= __maxBGInitRetryAttempts
const error = new Error(
noMoreAttempts
? `BG INIT LOGIC RETRIES EXHAUSTED: `
: `` +
`Error occurred during background script setup: ${originalError.message} - debug counter: ${__debugCounter}`,
)
if (originalError.stack) {
error.stack = originalError.stack
}
console.error('BG init logic encountered an error:', error)
captureException(error)
if (noMoreAttempts) {
throw originalError
}
await delay(10000)
await main().catch(handleError)
}
main().catch(handleError)