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 02411bf
Showing 1 changed file with 44 additions and 66 deletions.
110 changes: 44 additions & 66 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,88 +341,66 @@ 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
}
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
}

const newRequest = net.request({
method: request.method,
url
})
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']
// 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])
for (const header of Object.keys(request.headers)) {
if (!blacklistedHeaders.includes(header.toLowerCase())) {
newRequest.setHeader(header, request.headers[header])
}
}
}

newRequest.on('response', (response) => {
const chunks = []
response.on('data', (chunk) => {
chunks.push(chunk)
})
newRequest.on('response', (response) => {
const chunks = []
response.on('data', (chunk) => {
chunks.push(chunk)
})

response.on('end', () => {
const data = Buffer.concat(chunks)
response.on('end', () => {
const data = Buffer.concat(chunks)

const expiryTimestamp = extractExpiryTimestamp(response.headers)
const mimeType = response.headers['content-type']
const expiryTimestamp = extractExpiryTimestamp(response.headers)
const mimeType = response.headers['content-type']

imageCache.add(url, mimeType, data, expiryTimestamp)
imageCache.add(url, mimeType, data, expiryTimestamp)

// eslint-disable-next-line n/no-callback-literal
callback({
mimeType,
data: data
// eslint-disable-next-line n/no-callback-literal
resolve(new Response(data, {
headers: { 'content-type': mimeType }
}))
})
})

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
response.on('error', (error) => {
console.error('image cache error', error)
reject(error)
})
})

// 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.on('error', (err) => {
console.error(err)
newRequest.end()
})

newRequest.end()
})

const imageRequestFilter = { urls: ['https://*/*', 'http://*/*'] }
Expand Down

0 comments on commit 02411bf

Please sign in to comment.