Upgrade electron from 22.x to 27.x and replace deprecated protocol.registerBufferProtocol (#3967)

* ^ Upgrade electron from 22.x to 27.x

* * Replace deprecated registerBufferProtocol
This commit is contained in:
PikachuEXE 2023-10-21 05:39:07 +08:00 committed by GitHub
parent b55ad5d327
commit 4043961912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 79 deletions

View File

@ -90,7 +90,7 @@
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.8.1",
"css-minimizer-webpack-plugin": "^5.0.1",
"electron": "^22.3.26",
"electron": "^27.0.0",
"electron-builder": "^24.6.4",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",

View File

@ -359,88 +359,65 @@ 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)
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)
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])
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)
// 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']
const expiryTimestamp = extractExpiryTimestamp(response.headers)
const mimeType = response.headers['content-type']
for (const header of Object.keys(request.headers)) {
if (!blacklistedHeaders.includes(header.toLowerCase())) {
newRequest.setHeader(header, request.headers[header])
}
}
imageCache.add(url, mimeType, data, expiryTimestamp)
newRequest.on('response', (response) => {
const chunks = []
response.on('data', (chunk) => {
chunks.push(chunk)
})
// eslint-disable-next-line n/no-callback-literal
callback({
mimeType,
data: data
response.on('end', () => {
const data = Buffer.concat(chunks)
const expiryTimestamp = extractExpiryTimestamp(response.headers)
const mimeType = response.headers['content-type']
imageCache.add(url, mimeType, data, expiryTimestamp)
resolve(new Response(data, {
headers: { 'content-type': mimeType }
}))
})
response.on('error', (error) => {
console.error('image cache error', error)
reject(error)
})
})
response.on('error', (error) => {
console.error('image cache error', error)
// 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
}
}
return value
})
// eslint-disable-next-line n/no-callback-literal
callback({
statusCode: response.statusCode ?? 400,
mimeType: 'application/json',
data: Buffer.from(errorJson)
})
newRequest.on('error', (err) => {
console.error(err)
})
newRequest.end()
})
newRequest.on('error', (err) => {
console.error(err)
})
newRequest.end()
})
const imageRequestFilter = { urls: ['https://*/*', 'http://*/*'] }

View File

@ -1665,10 +1665,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.33.tgz#3c1879b276dc63e73030bb91165e62a4509cd506"
integrity sha512-miWq2m2FiQZmaHfdZNcbpp9PuXg34W5JZ5CrJ/BaS70VuhoJENBEQybeiYSaPBRNq6KQGnjfEnc/F3PN++D+XQ==
"@types/node@^16.11.26":
version "16.11.45"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.45.tgz#155b13a33c665ef2b136f7f245fa525da419e810"
integrity sha512-3rKg/L5x0rofKuuUt5zlXzOnKyIHXmIu5R8A0TuNDMF2062/AOIDBciFIjToLEJ/9F9DzkHNot+BpNsMI1OLdQ==
"@types/node@^18.11.18":
version "18.17.12"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.17.12.tgz#c6bd7413a13e6ad9cfb7e97dd5c4e904c1821e50"
integrity sha512-d6xjC9fJ/nSnfDeU0AMDsaJyb1iHsqCSOdi84w4u+SlN/UgQdY5tRhpMzaFYsI4mnpvgTivEaQd0yOUhAtOnEQ==
"@types/normalize-package-data@^2.4.0":
version "2.4.1"
@ -3446,13 +3446,13 @@ electron-to-chromium@^1.4.535:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.554.tgz#04e09c2ee31dc0f1546174033809b54cc372740b"
integrity sha512-Q0umzPJjfBrrj8unkONTgbKQXzXRrH7sVV7D9ea2yBV3Oaogz991yhbpfvo2LMNkJItmruXTEzVpP9cp7vaIiQ==
electron@^22.3.26:
version "22.3.26"
resolved "https://registry.yarnpkg.com/electron/-/electron-22.3.26.tgz#7d15714fee3735901df579195d109bdbca6fd02e"
integrity sha512-er0Lhn4WzX/bd+CBsg0KLYtEHys0gISCuZ7g8RMZhy2PGG3W31sF1TaXV90gzp/nPHQHdpKFE9frSMiDSGJ02g==
electron@^27.0.0:
version "27.0.0"
resolved "https://registry.yarnpkg.com/electron/-/electron-27.0.0.tgz#bb6c45881e531b2ec1c7cc46c47aba773f38ee14"
integrity sha512-mr3Zoy82l8XKK/TgguE5FeNeHZ9KHXIGIpUMjbjZWIREfAv+X2Q3vdX6RG0Pmi1K23AFAxANXQezIHBA2Eypwg==
dependencies:
"@electron/get" "^2.0.0"
"@types/node" "^16.11.26"
"@types/node" "^18.11.18"
extract-zip "^2.0.1"
emoji-regex@^10.0.0: