From e1f5da119608f78653b2b85188a31ebefb2e8db1 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Tue, 29 Aug 2023 09:20:17 +0800 Subject: [PATCH] * Replace deprecated registerBufferProtocol --- src/main/index.js | 111 +++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 66 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 6b9e505e725ee..ed44741e305a7 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -341,88 +341,67 @@ function runApp() { const imageCache = new ImageCache() - protocol.registerBufferProtocol('imagecache', (request, callback) => { - // Remove `imagecache://` prefix - const url = decodeURIComponent(request.url.substring(13)) - if (imageCache.has(url)) { - const cached = imageCache.get(url) - - callback(cached) - return - } - - const newRequest = net.request({ - method: request.method, - url - }) - - // Electron doesn't allow certain headers to be set: - // https://www.electronjs.org/docs/latest/api/client-request#requestsetheadername-value - // also blacklist Origin and Referrer as we don't want to let YouTube know about them - const blacklistedHeaders = ['content-length', 'host', 'trailer', 'te', 'upgrade', 'cookie2', 'keep-alive', 'transfer-encoding', 'origin', 'referrer'] - - for (const header of Object.keys(request.headers)) { - if (!blacklistedHeaders.includes(header.toLowerCase())) { - newRequest.setHeader(header, request.headers[header]) + protocol.handle('imagecache', (request) => { + return new Promise((resolve, reject) => { + const url = decodeURIComponent(request.url.substring(13)) + if (imageCache.has(url)) { + const cached = imageCache.get(url) + + resolve(new Response(cached.data, { + headers: { 'content-type': cached.mimeType } + })) + return } - } - newRequest.on('response', (response) => { - const chunks = [] - response.on('data', (chunk) => { - chunks.push(chunk) + const newRequest = net.request({ + method: request.method, + url }) - response.on('end', () => { - const data = Buffer.concat(chunks) - - const expiryTimestamp = extractExpiryTimestamp(response.headers) - const mimeType = response.headers['content-type'] + // Electron doesn't allow certain headers to be set: + // https://www.electronjs.org/docs/latest/api/client-request#requestsetheadername-value + // also blacklist Origin and Referrer as we don't want to let YouTube know about them + const blacklistedHeaders = ['content-length', 'host', 'trailer', 'te', 'upgrade', 'cookie2', 'keep-alive', 'transfer-encoding', 'origin', 'referrer'] - imageCache.add(url, mimeType, data, expiryTimestamp) + for (const header of Object.keys(request.headers)) { + if (!blacklistedHeaders.includes(header.toLowerCase())) { + newRequest.setHeader(header, request.headers[header]) + } + } - // eslint-disable-next-line n/no-callback-literal - callback({ - mimeType, - data: data + newRequest.on('response', (response) => { + const chunks = [] + response.on('data', (chunk) => { + chunks.push(chunk) }) - }) - response.on('error', (error) => { - console.error('image cache error', error) + response.on('end', () => { + const data = Buffer.concat(chunks) - // error objects don't get serialised properly - // https://stackoverflow.com/a/53624454 - - const errorJson = JSON.stringify(error, (key, value) => { - if (value instanceof Error) { - return { - // Pull all enumerable properties, supporting properties on custom Errors - ...value, - // Explicitly pull Error's non-enumerable properties - name: value.name, - message: value.message, - stack: value.stack - } - } + const expiryTimestamp = extractExpiryTimestamp(response.headers) + const mimeType = response.headers['content-type'] - return value + imageCache.add(url, mimeType, data, expiryTimestamp) + + // eslint-disable-next-line n/no-callback-literal + resolve(new Response(data, { + headers: { 'content-type': mimeType } + })) }) - // eslint-disable-next-line n/no-callback-literal - callback({ - statusCode: response.statusCode ?? 400, - mimeType: 'application/json', - data: Buffer.from(errorJson) + response.on('error', (error) => { + console.error('image cache error', error) + reject(error) }) }) - }) - newRequest.on('error', (err) => { - console.error(err) - }) + newRequest.on('error', (error) => { + console.error('image cache error', error) + reject(error) + }) - newRequest.end() + newRequest.end() + }) }) const imageRequestFilter = { urls: ['https://*/*', 'http://*/*'] }