|
|
@@ -15,7 +15,6 @@ import { debugConsole } from '@/utils/debugging'
|
|
|
import { PDFJS } from './pdf-js'
|
|
|
import { sendMB } from '@/infrastructure/event-tracking'
|
|
|
import getMeta from '@/utils/meta'
|
|
|
-import { PDFFile, PDFRange } from '@ol-types/compile'
|
|
|
|
|
|
// 30 seconds: The shutdown grace period of a clsi pre-emp instance.
|
|
|
const STALE_OUTPUT_REQUEST_THRESHOLD_MS = 30 * 1000
|
|
|
@@ -52,26 +51,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
new URLSearchParams(window.location.search).get('verify_chunks') === 'true'
|
|
|
|
|
|
class PDFDataRangeTransport extends PDFJS.PDFDataRangeTransport {
|
|
|
- url: string
|
|
|
- pdfFile: PDFFile
|
|
|
- abortController: AbortController
|
|
|
- leanPdfRanges: PDFRange[]
|
|
|
- handleFetchError: (error: any) => void
|
|
|
- startTime: number
|
|
|
- sentEventFallbackToClsiCache: boolean
|
|
|
- queryForChunks: string
|
|
|
-
|
|
|
- constructor({
|
|
|
- url,
|
|
|
- pdfFile,
|
|
|
- abortController,
|
|
|
- handleFetchError,
|
|
|
- }: {
|
|
|
- url: string
|
|
|
- pdfFile: PDFFile
|
|
|
- abortController: AbortController
|
|
|
- handleFetchError: (error: any) => void
|
|
|
- }) {
|
|
|
+ constructor({ url, pdfFile, abortController, handleFetchError }) {
|
|
|
super(pdfFile.size, new Uint8Array())
|
|
|
this.url = url
|
|
|
pdfFile.ranges = pdfFile.ranges || []
|
|
|
@@ -95,7 +75,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
this.abortController.abort()
|
|
|
}
|
|
|
|
|
|
- requestDataRange(start: number, end: number) {
|
|
|
+ requestDataRange(start, end) {
|
|
|
let recordFallbackToClsiCache = false
|
|
|
const abortSignal = this.abortController.signal
|
|
|
const getDebugInfo = () => ({
|
|
|
@@ -115,12 +95,9 @@ export function generatePdfCachingTransportFactory() {
|
|
|
|
|
|
const isStaleOutputRequest = () =>
|
|
|
performance.now() - this.startTime > STALE_OUTPUT_REQUEST_THRESHOLD_MS
|
|
|
- const is404 = (err: any) =>
|
|
|
- (OError.getFullInfo(err) as { statusCode: number }).statusCode === 404
|
|
|
- const isFromOutputPDFRequest = (err: any) =>
|
|
|
- (OError.getFullInfo(err) as { url?: string }).url?.includes?.(
|
|
|
- '/output.pdf'
|
|
|
- ) === true
|
|
|
+ const is404 = err => OError.getFullInfo(err).statusCode === 404
|
|
|
+ const isFromOutputPDFRequest = err =>
|
|
|
+ OError.getFullInfo(err).url?.includes?.('/output.pdf') === true
|
|
|
|
|
|
// Do not consider "expected 404s" and network errors as pdf caching
|
|
|
// failures.
|
|
|
@@ -129,11 +106,11 @@ export function generatePdfCachingTransportFactory() {
|
|
|
// 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: any) =>
|
|
|
+ const isExpectedError = err =>
|
|
|
(is404(err) || isNetworkError(err)) &&
|
|
|
(isStaleOutputRequest() || isFromOutputPDFRequest(err))
|
|
|
|
|
|
- const usesCache = (url: string) => {
|
|
|
+ const usesCache = url => {
|
|
|
if (!url) return false
|
|
|
const u = new URL(url)
|
|
|
return (
|
|
|
@@ -144,10 +121,10 @@ export function generatePdfCachingTransportFactory() {
|
|
|
u.searchParams.get('clsiserverid')?.startsWith('clsi-cache-'))
|
|
|
)
|
|
|
}
|
|
|
- const canTryFromCache = (err: any) => {
|
|
|
+ const canTryFromCache = err => {
|
|
|
if (!fallBackToClsiCache) return false
|
|
|
if (!is404(err)) return false
|
|
|
- return !usesCache((OError.getFullInfo(err) as { url: string }).url)
|
|
|
+ return !usesCache(OError.getFullInfo(err).url)
|
|
|
}
|
|
|
const getOutputPDFURLFromCache = () => {
|
|
|
if (usesCache(this.url)) return this.url
|
|
|
@@ -180,10 +157,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
return blob
|
|
|
})
|
|
|
.catch(err => {
|
|
|
- const { statusCode, url } = OError.getFullInfo(err) as {
|
|
|
- statusCode: number
|
|
|
- url: string
|
|
|
- }
|
|
|
+ const { statusCode, url } = OError.getFullInfo(err)
|
|
|
throw OError.tag(
|
|
|
new PDFJS.ResponseException(undefined, statusCode, true),
|
|
|
'cache-fallback',
|
|
|
@@ -192,8 +166,6 @@ export function generatePdfCachingTransportFactory() {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- // @ts-ignore is incorrectly inferring the type of fetchRange.
|
|
|
- // Remove this when we convert pdf-caching.js to typescript.
|
|
|
fetchRange({
|
|
|
url: this.url,
|
|
|
start,
|
|
|
@@ -225,10 +197,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
metrics.failedCount++
|
|
|
metrics.failedOnce = true
|
|
|
}
|
|
|
- const { statusCode, url } = OError.getFullInfo(err) as {
|
|
|
- statusCode: number
|
|
|
- url: string
|
|
|
- }
|
|
|
+ const { statusCode, url } = OError.getFullInfo(err)
|
|
|
throw OError.tag(
|
|
|
new PDFJS.ResponseException(undefined, statusCode, true),
|
|
|
'caching',
|
|
|
@@ -257,10 +226,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
}).catch(err => {
|
|
|
if (canTryFromCache(err)) return fetchFromCache()
|
|
|
if (isExpectedError(err)) {
|
|
|
- const { statusCode, url } = OError.getFullInfo(err) as {
|
|
|
- statusCode: number
|
|
|
- url: string
|
|
|
- }
|
|
|
+ const { statusCode, url } = OError.getFullInfo(err)
|
|
|
throw OError.tag(
|
|
|
new PDFJS.ResponseException(undefined, statusCode, true),
|
|
|
'fallback',
|
|
|
@@ -280,7 +246,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
ageMS: Math.ceil(performance.now() - this.startTime),
|
|
|
})
|
|
|
}
|
|
|
- this.onDataRange(start, blob ?? null)
|
|
|
+ this.onDataRange(start, blob)
|
|
|
})
|
|
|
.catch(err => {
|
|
|
if (abortSignal.aborted) return
|
|
|
@@ -300,17 +266,7 @@ export function generatePdfCachingTransportFactory() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return function ({
|
|
|
- url,
|
|
|
- pdfFile,
|
|
|
- abortController,
|
|
|
- handleFetchError,
|
|
|
- }: {
|
|
|
- url: string
|
|
|
- pdfFile: PDFFile
|
|
|
- abortController: AbortController
|
|
|
- handleFetchError: (error: any) => void
|
|
|
- }) {
|
|
|
+ return function ({ url, pdfFile, abortController, handleFetchError }) {
|
|
|
if (metrics.failedOnce) {
|
|
|
// Disable pdf caching once any fetch request failed.
|
|
|
// Be trigger-happy here until we reached a stable state of the feature.
|