2022-07-28 10:38:55 -04:00
|
|
|
import OError from '@overleaf/o-error'
|
2022-07-06 07:06:53 -04:00
|
|
|
import { fallbackRequest, fetchRange } from './pdf-caching'
|
|
|
|
import { captureException } from '../../../infrastructure/error-reporter'
|
2022-07-20 04:15:41 -04:00
|
|
|
import { getPdfCachingMetrics } from './metrics'
|
2022-08-01 07:31:05 -04:00
|
|
|
import {
|
|
|
|
cachedUrlLookupEnabled,
|
|
|
|
enablePdfCaching,
|
|
|
|
prefetchingEnabled,
|
|
|
|
prefetchLargeEnabled,
|
|
|
|
trackPdfDownloadEnabled,
|
|
|
|
} from './pdf-caching-flags'
|
2022-11-07 09:24:37 -05:00
|
|
|
import { isNetworkError } from '../../../utils/isNetworkError'
|
2022-07-06 07:06:53 -04:00
|
|
|
|
2022-11-07 09:24:17 -05:00
|
|
|
// 30 seconds: The shutdown grace period of a clsi pre-emp instance.
|
|
|
|
const STALE_OUTPUT_REQUEST_THRESHOLD_MS = 30 * 1000
|
|
|
|
|
2022-07-06 07:06:53 -04:00
|
|
|
export function generatePdfCachingTransportFactory(PDFJS) {
|
2022-07-20 04:32:05 -04:00
|
|
|
// NOTE: The custom transport can be used for tracking download volume.
|
|
|
|
if (!enablePdfCaching && !trackPdfDownloadEnabled) {
|
2022-07-06 07:06:53 -04:00
|
|
|
return () => null
|
|
|
|
}
|
2022-08-01 07:31:05 -04:00
|
|
|
const usageScore = new Map()
|
|
|
|
const cachedUrls = new Map()
|
2022-07-20 04:15:41 -04:00
|
|
|
const metrics = Object.assign(getPdfCachingMetrics(), {
|
2022-07-06 07:06:53 -04:00
|
|
|
failedCount: 0,
|
2022-08-01 07:16:29 -04:00
|
|
|
failedOnce: false,
|
2022-08-01 07:31:05 -04:00
|
|
|
tooMuchBandwidthCount: 0,
|
2022-07-06 07:06:53 -04:00
|
|
|
tooManyRequestsCount: 0,
|
|
|
|
cachedCount: 0,
|
|
|
|
cachedBytes: 0,
|
|
|
|
fetchedCount: 0,
|
|
|
|
fetchedBytes: 0,
|
2022-10-25 05:23:59 -04:00
|
|
|
latencyComputeMax: 0,
|
|
|
|
latencyComputeTotal: 0,
|
2022-07-06 07:06:53 -04:00
|
|
|
requestedCount: 0,
|
|
|
|
requestedBytes: 0,
|
2022-08-01 07:31:05 -04:00
|
|
|
oldUrlHitCount: 0,
|
|
|
|
oldUrlMissCount: 0,
|
2022-07-20 04:32:05 -04:00
|
|
|
enablePdfCaching,
|
2022-08-01 07:31:05 -04:00
|
|
|
prefetchingEnabled,
|
|
|
|
prefetchLargeEnabled,
|
|
|
|
cachedUrlLookupEnabled,
|
2022-07-20 04:15:41 -04:00
|
|
|
})
|
2022-07-08 04:15:13 -04:00
|
|
|
const verifyChunks =
|
|
|
|
new URLSearchParams(window.location.search).get('verify_chunks') === 'true'
|
2022-07-06 07:06:53 -04:00
|
|
|
|
|
|
|
class PDFDataRangeTransport extends PDFJS.PDFDataRangeTransport {
|
2022-08-01 07:16:29 -04:00
|
|
|
constructor({ url, pdfFile, abortController, handleFetchError }) {
|
2022-07-06 07:06:53 -04:00
|
|
|
super(pdfFile.size, new Uint8Array())
|
|
|
|
this.url = url
|
|
|
|
this.pdfFile = pdfFile
|
2022-11-07 09:24:28 -05:00
|
|
|
// Clone the chunks as the objectId field is encoded to a Uint8Array.
|
|
|
|
this.pdfRanges = pdfFile.ranges.map(r => Object.assign({}, r))
|
2022-08-01 07:16:29 -04:00
|
|
|
this.handleFetchError = handleFetchError
|
|
|
|
this.abortController = abortController
|
2022-11-07 09:24:17 -05:00
|
|
|
this.startTime = performance.now()
|
2022-07-20 04:15:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
abort() {
|
|
|
|
this.abortController.abort()
|
2022-07-06 07:06:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
requestDataRange(start, end) {
|
2022-07-20 04:15:58 -04:00
|
|
|
const abortSignal = this.abortController.signal
|
2022-11-07 09:24:28 -05:00
|
|
|
const getDebugInfo = () => ({
|
|
|
|
// Sentry does not serialize objects in twice nested objects.
|
|
|
|
// Move the ranges to the root level to see them in Sentry.
|
|
|
|
pdfRanges: this.pdfRanges,
|
|
|
|
pdfFile: Object.assign({}, this.pdfFile, {
|
|
|
|
ranges: '[extracted]',
|
|
|
|
// Hide prefetched chunks as these include binary blobs.
|
|
|
|
prefetched: this.pdfFile.prefetched?.length,
|
|
|
|
}),
|
2022-07-28 10:38:55 -04:00
|
|
|
pdfUrl: this.url,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
metrics,
|
2022-11-07 09:24:28 -05:00
|
|
|
})
|
2022-11-07 09:24:17 -05:00
|
|
|
|
|
|
|
const isStaleOutputRequest = () =>
|
|
|
|
performance.now() - this.startTime > STALE_OUTPUT_REQUEST_THRESHOLD_MS
|
|
|
|
const is404 = err => err.message === 'non successful response status: 404'
|
|
|
|
const isFromOutputPDFRequest = err =>
|
2022-11-03 08:45:11 -04:00
|
|
|
OError.getFullInfo(err).url === this.url
|
|
|
|
|
2022-11-07 09:24:17 -05:00
|
|
|
// Do not consider "expected 404s" and network errors as pdf caching
|
|
|
|
// failures.
|
|
|
|
// "expected 404s" here include:
|
|
|
|
// - any stale download request
|
|
|
|
// Example: The user returns to a browser tab after 1h and scrolls.
|
|
|
|
// - requests for the main output.pdf file
|
|
|
|
// A fallback request would not be able to retrieve the PDF either.
|
|
|
|
const isExpectedError = err =>
|
|
|
|
(is404(err) || isNetworkError(err)) &&
|
|
|
|
(isStaleOutputRequest() || isFromOutputPDFRequest(err))
|
|
|
|
|
2022-07-06 07:06:53 -04:00
|
|
|
fetchRange({
|
|
|
|
url: this.url,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
file: this.pdfFile,
|
|
|
|
metrics,
|
2022-08-01 07:31:05 -04:00
|
|
|
usageScore,
|
|
|
|
cachedUrls,
|
2022-07-08 04:15:13 -04:00
|
|
|
verifyChunks,
|
2022-08-01 07:31:05 -04:00
|
|
|
prefetchingEnabled,
|
|
|
|
prefetchLargeEnabled,
|
|
|
|
cachedUrlLookupEnabled,
|
2022-07-20 04:15:58 -04:00
|
|
|
abortSignal,
|
2022-07-06 07:06:53 -04:00
|
|
|
})
|
|
|
|
.catch(err => {
|
2022-08-01 07:16:29 -04:00
|
|
|
if (abortSignal.aborted) return
|
2022-11-07 09:24:17 -05:00
|
|
|
if (isExpectedError(err)) {
|
2022-11-08 09:40:51 -05:00
|
|
|
if (is404(err)) {
|
|
|
|
// A regular pdf-js request would have seen this 404 as well.
|
|
|
|
} else {
|
|
|
|
// Flaky network, switch back to regular pdf-js requests.
|
|
|
|
metrics.failedCount++
|
|
|
|
metrics.failedOnce = true
|
|
|
|
}
|
2022-07-28 10:39:05 -04:00
|
|
|
throw new PDFJS.MissingPDFException()
|
|
|
|
}
|
2022-07-06 07:06:53 -04:00
|
|
|
metrics.failedCount++
|
2022-08-01 07:16:29 -04:00
|
|
|
metrics.failedOnce = true
|
2022-07-20 04:32:05 -04:00
|
|
|
if (!enablePdfCaching) {
|
|
|
|
throw err // This was a fallback request already. Do not retry.
|
|
|
|
}
|
2022-11-07 09:24:28 -05:00
|
|
|
err = OError.tag(err, 'optimized pdf download error', getDebugInfo())
|
2022-07-28 10:38:55 -04:00
|
|
|
console.error(err)
|
2022-11-08 06:48:44 -05:00
|
|
|
captureException(err, {
|
|
|
|
tags: {
|
|
|
|
fromPdfCaching: true,
|
|
|
|
isFromOutputPDFRequest: isFromOutputPDFRequest(err),
|
|
|
|
},
|
|
|
|
})
|
2022-11-03 08:45:11 -04:00
|
|
|
return fallbackRequest({
|
|
|
|
url: this.url,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
abortSignal,
|
|
|
|
}).catch(err => {
|
2022-11-07 09:24:17 -05:00
|
|
|
if (isExpectedError(err)) {
|
2022-11-03 08:45:11 -04:00
|
|
|
err = new PDFJS.MissingPDFException()
|
|
|
|
}
|
|
|
|
throw err
|
|
|
|
})
|
2022-07-06 07:06:53 -04:00
|
|
|
})
|
|
|
|
.then(blob => {
|
2022-08-01 07:16:29 -04:00
|
|
|
if (abortSignal.aborted) return
|
2022-07-06 07:06:53 -04:00
|
|
|
this.onDataRange(start, blob)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2022-08-01 07:16:29 -04:00
|
|
|
if (abortSignal.aborted) return
|
2022-11-07 09:24:28 -05:00
|
|
|
err = OError.tag(err, 'fatal pdf download error', getDebugInfo())
|
2022-07-28 10:38:55 -04:00
|
|
|
console.error(err)
|
2022-07-28 10:39:05 -04:00
|
|
|
if (!(err instanceof PDFJS.MissingPDFException)) {
|
2022-11-08 06:48:44 -05:00
|
|
|
captureException(err, {
|
|
|
|
tags: {
|
|
|
|
fromPdfCaching: true,
|
|
|
|
isFromOutputPDFRequest: isFromOutputPDFRequest(err),
|
|
|
|
},
|
|
|
|
})
|
2022-07-28 10:39:05 -04:00
|
|
|
}
|
2022-08-01 07:16:29 -04:00
|
|
|
// Signal error for (subsequent) page load.
|
|
|
|
this.handleFetchError(err)
|
2022-07-06 07:06:53 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 07:16:29 -04:00
|
|
|
return function ({ url, pdfFile, abortController, handleFetchError }) {
|
|
|
|
if (metrics.failedOnce) {
|
2022-07-20 04:15:58 -04:00
|
|
|
// Disable pdf caching once any fetch request failed.
|
|
|
|
// Be trigger-happy here until we reached a stable state of the feature.
|
|
|
|
return null
|
|
|
|
}
|
2022-10-25 05:23:59 -04:00
|
|
|
// Latency is collected per preview cycle.
|
|
|
|
metrics.latencyComputeMax = 0
|
|
|
|
metrics.latencyComputeTotal = 0
|
2022-08-01 07:16:29 -04:00
|
|
|
return new PDFDataRangeTransport({
|
|
|
|
url,
|
|
|
|
pdfFile,
|
|
|
|
abortController,
|
|
|
|
handleFetchError,
|
|
|
|
})
|
2022-07-06 07:06:53 -04:00
|
|
|
}
|
|
|
|
}
|