serviceWorker.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import { v4 as uuid } from 'uuid'
  2. const OError = require('@overleaf/o-error')
  3. const COMPILE_REQUEST_MATCHER = /^\/project\/[0-9a-f]{24}\/compile$/
  4. const PDF_REQUEST_MATCHER = /^\/project\/[0-9a-f]{24}\/.*\/output.pdf$/
  5. const PDF_JS_CHUNK_SIZE = 128 * 1024
  6. const MAX_SUBREQUEST_COUNT = 8
  7. const MAX_SUBREQUEST_BYTES = 4 * PDF_JS_CHUNK_SIZE
  8. // Each compile request defines a context (essentially the specific pdf file for
  9. // that compile), requests for that pdf file can use the hashes in the compile
  10. // response, which are stored in the context.
  11. const pdfContext = new Map()
  12. function registerPdfContext(clientId, path, context) {
  13. let clientMap = pdfContext.get(clientId)
  14. if (!clientMap) {
  15. clientMap = new Map()
  16. pdfContext.set(clientId, clientMap)
  17. // clean up old client maps
  18. expirePdfContexts()
  19. }
  20. // we only need to keep the last 3 contexts
  21. for (const key of clientMap.keys()) {
  22. if (clientMap.size < 3) {
  23. break
  24. }
  25. clientMap.delete(key) // the map keys are returned in insertion order, so we are deleting the oldest entry here
  26. }
  27. clientMap.set(path, context)
  28. }
  29. function getPdfContext(clientId, path) {
  30. const clientMap = pdfContext.get(clientId)
  31. const context = clientMap && clientMap.get(path)
  32. return context
  33. }
  34. function expirePdfContexts() {
  35. // discard client maps for clients that are no longer connected
  36. const currentClientSet = new Set()
  37. self.clients.matchAll().then(function (clientList) {
  38. clientList.forEach(client => {
  39. currentClientSet.add(client.id)
  40. })
  41. pdfContext.forEach((map, clientId) => {
  42. if (!currentClientSet.has(clientId)) {
  43. pdfContext.delete(clientId)
  44. }
  45. })
  46. })
  47. }
  48. const METRICS = {
  49. id: uuid(),
  50. epoch: Date.now(),
  51. cachedBytes: 0,
  52. fetchedBytes: 0,
  53. requestedBytes: 0,
  54. }
  55. /**
  56. *
  57. * @param {number} size
  58. * @param {number} cachedBytes
  59. * @param {number} fetchedBytes
  60. */
  61. function trackStats({ size, cachedBytes, fetchedBytes }) {
  62. METRICS.cachedBytes += cachedBytes
  63. METRICS.fetchedBytes += fetchedBytes
  64. METRICS.requestedBytes += size
  65. }
  66. /**
  67. * @param {boolean} sizeDiffers
  68. * @param {boolean} mismatch
  69. * @param {boolean} success
  70. */
  71. function trackChunkVerify({ sizeDiffers, mismatch, success }) {
  72. if (sizeDiffers) {
  73. METRICS.chunkVerifySizeDiffers |= 0
  74. METRICS.chunkVerifySizeDiffers += 1
  75. }
  76. if (mismatch) {
  77. METRICS.chunkVerifyMismatch |= 0
  78. METRICS.chunkVerifyMismatch += 1
  79. }
  80. if (success) {
  81. METRICS.chunkVerifySuccess |= 0
  82. METRICS.chunkVerifySuccess += 1
  83. }
  84. }
  85. /**
  86. * @param {Array} chunks
  87. */
  88. function countBytes(chunks) {
  89. return chunks.reduce((totalBytes, chunk) => {
  90. return totalBytes + (chunk.end - chunk.start)
  91. }, 0)
  92. }
  93. /**
  94. * @param {FetchEvent} event
  95. */
  96. function onFetch(event) {
  97. const url = new URL(event.request.url)
  98. const path = url.pathname
  99. if (path.match(COMPILE_REQUEST_MATCHER)) {
  100. return processCompileRequest(event)
  101. }
  102. if (path.match(PDF_REQUEST_MATCHER)) {
  103. const ctx = getPdfContext(event.clientId, path)
  104. if (ctx) {
  105. return processPdfRequest(event, ctx)
  106. }
  107. }
  108. // other request, ignore
  109. }
  110. function processCompileRequest(event) {
  111. event.respondWith(
  112. fetch(event.request).then(response => {
  113. if (response.status !== 200) return response
  114. return response.json().then(body => {
  115. handleCompileResponse(event, response, body)
  116. // Send the service workers metrics to the frontend.
  117. body.serviceWorkerMetrics = METRICS
  118. return new Response(JSON.stringify(body), response)
  119. })
  120. })
  121. )
  122. }
  123. /**
  124. * @param {Request} request
  125. * @param {Object} file
  126. * @return {Response}
  127. */
  128. function handleProbeRequest(request, file) {
  129. // PDF.js starts the pdf download with a probe request that has no
  130. // range headers on it.
  131. // Upon seeing the response headers, it decides whether to upgrade the
  132. // transport to chunked requests or keep reading the response body.
  133. // For small PDFs (2*chunkSize = 2*128kB) it just sends one request.
  134. // We will fetch all the ranges in bulk and emit them.
  135. // For large PDFs it sends this probe request, aborts that request before
  136. // reading any data and then sends multiple range requests.
  137. // It would be wasteful to action this probe request with all the ranges
  138. // that are available in the PDF and serve the full PDF content to
  139. // PDF.js for the probe request.
  140. // We are emitting a dummy response to the probe request instead.
  141. // It triggers the chunked transfer and subsequent fewer ranges need to be
  142. // requested -- only those of visible pages in the pdf viewer.
  143. // https://github.com/mozilla/pdf.js/blob/6fd899dc443425747098935207096328e7b55eb2/src/display/network_utils.js#L43-L47
  144. const pdfJSWillUseChunkedTransfer = file.size > 2 * PDF_JS_CHUNK_SIZE
  145. const isRangeRequest = request.headers.has('Range')
  146. if (!isRangeRequest && pdfJSWillUseChunkedTransfer) {
  147. const headers = new Headers()
  148. headers.set('Accept-Ranges', 'bytes')
  149. headers.set('Content-Length', file.size)
  150. headers.set('Content-Type', 'application/pdf')
  151. return new Response('', {
  152. headers,
  153. status: 200,
  154. statusText: 'OK',
  155. })
  156. }
  157. }
  158. /**
  159. *
  160. * @param {FetchEvent} event
  161. * @param {Object} file
  162. * @param {string} clsiServerId
  163. * @param {string} compileGroup
  164. * @param {Date} pdfCreatedAt
  165. */
  166. function processPdfRequest(
  167. event,
  168. { file, clsiServerId, compileGroup, pdfCreatedAt }
  169. ) {
  170. const response = handleProbeRequest(event.request, file)
  171. if (response) {
  172. return event.respondWith(response)
  173. }
  174. const verifyChunks = event.request.url.includes('verify_chunks=true')
  175. const rangeHeader =
  176. event.request.headers.get('Range') || `bytes=0-${file.size - 1}`
  177. const [start, last] = rangeHeader
  178. .slice('bytes='.length)
  179. .split('-')
  180. .map(i => parseInt(i, 10))
  181. const end = last + 1
  182. // Check that handling the range request won't trigger excessive subrequests,
  183. // (to avoid unwanted latency compared to the original request).
  184. const chunks = getMatchingChunks(file.ranges, start, end)
  185. const dynamicChunks = getInterleavingDynamicChunks(chunks, start, end)
  186. const chunksSize = countBytes(chunks)
  187. if (chunks.length + dynamicChunks.length > MAX_SUBREQUEST_COUNT) {
  188. // fall back to the original range request when splitting the range creates
  189. // too many subrequests.
  190. return
  191. }
  192. if (
  193. chunksSize > MAX_SUBREQUEST_BYTES &&
  194. !(dynamicChunks.length === 0 && chunks.length === 1)
  195. ) {
  196. // fall back to the original range request when a very large amount of
  197. // object data would be requested, unless it is the only object in the
  198. // request.
  199. return
  200. }
  201. // URL prefix is /project/:id/user/:id/build/... or /project/:id/build/...
  202. // for authenticated and unauthenticated users respectively.
  203. const perUserPrefix = file.url.slice(0, file.url.indexOf('/build/'))
  204. const requests = chunks
  205. .map(chunk => {
  206. const path = `${perUserPrefix}/content/${file.contentId}/${chunk.hash}`
  207. const url = new URL(path, event.request.url)
  208. if (clsiServerId) {
  209. url.searchParams.set('clsiserverid', clsiServerId)
  210. }
  211. if (compileGroup) {
  212. url.searchParams.set('compileGroup', compileGroup)
  213. }
  214. return { chunk, url: url.toString() }
  215. })
  216. .concat(
  217. dynamicChunks.map(chunk => {
  218. const { start, end } = chunk
  219. return {
  220. chunk,
  221. url: event.request.url,
  222. init: { headers: { Range: `bytes=${start}-${end - 1}` } },
  223. }
  224. })
  225. )
  226. const size = end - start
  227. let cachedBytes = 0
  228. let fetchedBytes = 0
  229. const reAssembledBlob = new Uint8Array(size)
  230. event.respondWith(
  231. Promise.all(
  232. requests.map(({ chunk, url, init }) =>
  233. fetch(url, init)
  234. .then(response => {
  235. if (!(response.status === 206 || response.status === 200)) {
  236. throw new OError(
  237. 'non successful response status: ' + response.status
  238. )
  239. }
  240. const blobFetchDate = getServerTime(response)
  241. const blobSize = getResponseSize(response)
  242. if (blobFetchDate && blobSize) {
  243. const chunkSize =
  244. Math.min(end, chunk.end) - Math.max(start, chunk.start)
  245. // Example: 2MB PDF, 1MB image, 128KB PDF.js chunk.
  246. // | pdf.js chunk |
  247. // | A BIG IMAGE BLOB |
  248. // | THE FULL PDF |
  249. if (blobFetchDate < pdfCreatedAt) {
  250. cachedBytes += chunkSize
  251. } else {
  252. // Blobs are fetched in bulk.
  253. fetchedBytes += blobSize
  254. }
  255. }
  256. return response.arrayBuffer()
  257. })
  258. .then(arrayBuffer => {
  259. return {
  260. chunk,
  261. data: backFillObjectContext(chunk, arrayBuffer),
  262. }
  263. })
  264. .catch(error => {
  265. throw OError.tag(error, 'cannot fetch chunk', { url })
  266. })
  267. )
  268. )
  269. .then(responses => {
  270. responses.forEach(({ chunk, data }) => {
  271. // overlap:
  272. // | REQUESTED_RANGE |
  273. // | CHUNK |
  274. const offsetStart = Math.max(start - chunk.start, 0)
  275. // overlap:
  276. // | REQUESTED_RANGE |
  277. // | CHUNK |
  278. const offsetEnd = Math.max(chunk.end - end, 0)
  279. if (offsetStart > 0 || offsetEnd > 0) {
  280. // compute index positions for slice to handle case where offsetEnd=0
  281. const chunkSize = chunk.end - chunk.start
  282. data = data.slice(offsetStart, chunkSize - offsetEnd)
  283. }
  284. const insertPosition = Math.max(chunk.start - start, 0)
  285. reAssembledBlob.set(data, insertPosition)
  286. })
  287. let verifyProcess = Promise.resolve(reAssembledBlob)
  288. if (verifyChunks) {
  289. verifyProcess = fetch(event.request)
  290. .then(response => response.arrayBuffer())
  291. .then(arrayBuffer => {
  292. const fullBlob = new Uint8Array(arrayBuffer)
  293. const metrics = {}
  294. if (reAssembledBlob.byteLength !== fullBlob.byteLength) {
  295. metrics.sizeDiffers = true
  296. } else if (
  297. !reAssembledBlob.every((v, idx) => v === fullBlob[idx])
  298. ) {
  299. metrics.mismatch = true
  300. } else {
  301. metrics.success = true
  302. }
  303. trackChunkVerify(metrics)
  304. if (metrics.success === true) {
  305. return reAssembledBlob
  306. } else {
  307. return fullBlob
  308. }
  309. })
  310. }
  311. return verifyProcess.then(blob => {
  312. trackStats({ size, cachedBytes, fetchedBytes })
  313. return new Response(blob, {
  314. status: 206,
  315. headers: {
  316. 'Accept-Ranges': 'bytes',
  317. 'Content-Length': size,
  318. 'Content-Range': `bytes ${start}-${last}/${file.size}`,
  319. 'Content-Type': 'application/pdf',
  320. },
  321. })
  322. })
  323. })
  324. .catch(error => {
  325. reportError(event, OError.tag(error, 'failed to compose pdf response'))
  326. return fetch(event.request)
  327. })
  328. )
  329. }
  330. /**
  331. *
  332. * @param {Response} response
  333. */
  334. function getServerTime(response) {
  335. const raw = response.headers.get('Date')
  336. if (!raw) return new Date()
  337. return new Date(raw)
  338. }
  339. /**
  340. *
  341. * @param {Response} response
  342. */
  343. function getResponseSize(response) {
  344. const raw = response.headers.get('Content-Length')
  345. if (!raw) return 0
  346. return parseInt(raw, 10)
  347. }
  348. /**
  349. * @param {FetchEvent} event
  350. * @param {Response} response
  351. * @param {Object} body
  352. */
  353. function handleCompileResponse(event, response, body) {
  354. if (!body || body.status !== 'success') return
  355. const pdfCreatedAt = getServerTime(response)
  356. for (const file of body.outputFiles) {
  357. if (file.path !== 'output.pdf') continue // not the pdf used for rendering
  358. if (file.ranges) {
  359. file.ranges.forEach(backFillEdgeBounds)
  360. const { clsiServerId, compileGroup } = body
  361. registerPdfContext(event.clientId, file.url, {
  362. pdfCreatedAt,
  363. file,
  364. clsiServerId,
  365. compileGroup,
  366. })
  367. }
  368. break
  369. }
  370. }
  371. const ENCODER = new TextEncoder()
  372. function backFillEdgeBounds(chunk) {
  373. if (chunk.objectId) {
  374. chunk.objectId = ENCODER.encode(chunk.objectId)
  375. chunk.start -= chunk.objectId.byteLength
  376. }
  377. return chunk
  378. }
  379. /**
  380. * @param chunk
  381. * @param {ArrayBuffer} arrayBuffer
  382. * @return {Uint8Array}
  383. */
  384. function backFillObjectContext(chunk, arrayBuffer) {
  385. if (!chunk.objectId) {
  386. // This is a dynamic chunk
  387. return new Uint8Array(arrayBuffer)
  388. }
  389. const { start, end, objectId } = chunk
  390. const header = Uint8Array.from(objectId)
  391. const fullBuffer = new Uint8Array(end - start)
  392. fullBuffer.set(header, 0)
  393. fullBuffer.set(new Uint8Array(arrayBuffer), objectId.length)
  394. return fullBuffer
  395. }
  396. /**
  397. * @param {Array} chunks
  398. * @param {number} start
  399. * @param {number} end
  400. * @returns {Array}
  401. */
  402. function getMatchingChunks(chunks, start, end) {
  403. const matchingChunks = []
  404. for (const chunk of chunks) {
  405. if (chunk.end <= start) {
  406. // no overlap:
  407. // | REQUESTED_RANGE |
  408. // | CHUNK |
  409. continue
  410. }
  411. if (chunk.start >= end) {
  412. // no overlap:
  413. // | REQUESTED_RANGE |
  414. // | CHUNK |
  415. break
  416. }
  417. matchingChunks.push(chunk)
  418. }
  419. return matchingChunks
  420. }
  421. /**
  422. * @param {Array} chunks
  423. * @param {number} start
  424. * @param {number} end
  425. * @returns {Array}
  426. */
  427. function getInterleavingDynamicChunks(chunks, start, end) {
  428. const dynamicChunks = []
  429. for (const chunk of chunks) {
  430. if (start < chunk.start) {
  431. dynamicChunks.push({ start, end: chunk.start })
  432. }
  433. start = chunk.end
  434. }
  435. if (start < end) {
  436. dynamicChunks.push({ start, end })
  437. }
  438. return dynamicChunks
  439. }
  440. /**
  441. * @param {FetchEvent} event
  442. */
  443. function onFetchWithErrorHandling(event) {
  444. try {
  445. onFetch(event)
  446. } catch (error) {
  447. reportError(event, OError.tag(error, 'low level error in onFetch'))
  448. }
  449. }
  450. // listen to all network requests
  451. self.addEventListener('fetch', onFetchWithErrorHandling)
  452. // complete setup ASAP
  453. self.addEventListener('install', event => {
  454. event.waitUntil(self.skipWaiting())
  455. })
  456. self.addEventListener('activate', event => {
  457. event.waitUntil(self.clients.claim())
  458. })
  459. /**
  460. *
  461. * @param {FetchEvent} event
  462. * @param {Error} error
  463. */
  464. function reportError(event, error) {
  465. self.clients
  466. .get(event.clientId)
  467. .then(client => {
  468. if (!client) {
  469. // The client disconnected.
  470. return
  471. }
  472. client.postMessage(
  473. JSON.stringify({
  474. extra: { url: event.request.url, info: OError.getFullInfo(error) },
  475. error: {
  476. name: error.name,
  477. message: error.message,
  478. stack: OError.getFullStack(error),
  479. },
  480. })
  481. )
  482. })
  483. .catch(() => {})
  484. }