Skip to content

Commit

Permalink
* Replace deprecated registerBufferProtocol
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed Aug 29, 2023
1 parent f80baf3 commit e1f5da1
Showing 1 changed file with 45 additions and 66 deletions.
111 changes: 45 additions & 66 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://*/*'] }
Expand Down

0 comments on commit e1f5da1

Please sign in to comment.