overleaf/services/web/frontend/js/features/pdf-preview/util/pdf-caching-transport.js
Jakob Ackermann f11e1a83cd Merge pull request #8776 from overleaf/jpa-drop-service-worker
[web] goodbye service worker

GitOrigin-RevId: ce85d4850faba15c5877ce1f3e78026de30c6eae
2022-07-11 08:03:57 +00:00

64 lines
1.7 KiB
JavaScript

import { fallbackRequest, fetchRange } from './pdf-caching'
import getMeta from '../../../utils/meta'
import { captureException } from '../../../infrastructure/error-reporter'
import { setCachingMetrics } from './metrics'
export function generatePdfCachingTransportFactory(PDFJS) {
if (getMeta('ol-pdfCachingMode') !== 'enabled') {
return () => null
}
const cached = new Set()
const metrics = {
failedCount: 0,
tooLargeOverheadCount: 0,
tooManyRequestsCount: 0,
cachedCount: 0,
cachedBytes: 0,
fetchedCount: 0,
fetchedBytes: 0,
requestedCount: 0,
requestedBytes: 0,
}
const verifyChunks =
new URLSearchParams(window.location.search).get('verify_chunks') === 'true'
setCachingMetrics(metrics)
class PDFDataRangeTransport extends PDFJS.PDFDataRangeTransport {
constructor(url, pdfFile, reject) {
super(pdfFile.size, new Uint8Array())
this.url = url
this.pdfFile = pdfFile
this.reject = reject
}
requestDataRange(start, end) {
fetchRange({
url: this.url,
start,
end,
file: this.pdfFile,
metrics,
cached,
verifyChunks,
})
.catch(err => {
metrics.failedCount++
console.error('optimized pdf download error', err)
captureException(err)
return fallbackRequest({ url: this.url, start, end })
})
.then(blob => {
this.onDataRange(start, blob)
})
.catch(err => {
console.error('fatal pdf download error', err)
captureException(err)
this.reject(err)
})
}
}
return function (url, pdfFile, reject) {
return new PDFDataRangeTransport(url, pdfFile, reject)
}
}