Having upgraded to Electron 7.0, I've noticed this deprecation message:
(node:8308) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed.
The code in question is:
await new Promise((resolve, reject) => {
electron.protocol.registerBufferProtocol(MY_PROTOCOL,
(request, callback) => {
const uri = request.url;
if (uri) {
callback({ mimeType: 'text/plain', data: Buffer.from(uri) });
}
else {
callback({ error: -324 }); // EMPTY_RESPONSE
}
},
error => error? reject(error): resolve()
);
});
What's the proper way of calling registerBufferProtocol
now, as of Electron 7?
Having upgraded to Electron 7.0, I've noticed this deprecation message:
(node:8308) ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed.
The code in question is:
await new Promise((resolve, reject) => {
electron.protocol.registerBufferProtocol(MY_PROTOCOL,
(request, callback) => {
const uri = request.url;
if (uri) {
callback({ mimeType: 'text/plain', data: Buffer.from(uri) });
}
else {
callback({ error: -324 }); // EMPTY_RESPONSE
}
},
error => error? reject(error): resolve()
);
});
What's the proper way of calling registerBufferProtocol
now, as of Electron 7?
1 Answer
Reset to default 10It took me some noticeable time to figure out how to call registerBufferProtocol
properly with Electron 7.0, so sharing this with the munity and my future self.
According to Electron's Breaking changes document, registerBufferProtocol
is now synchronous, so calling it has actually got simpler:
electron.protocol.registerBufferProtocol(MY_PROTOCOL,
(request, callback) => {
const uri = request.url;
if (uri) {
callback({ mimeType: 'text/plain', data: Buffer.from(uri) });
}
else {
callback({ error: -324 }); // EMPTY_RESPONSE
}
});
The confusing (to me) part of the warning was that the callback
is actually not the callback
arg to the handler
, but rather is the last pletion
arg passed to the protocol.registerBufferProtocol
API itself.