Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with "ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed" (Electron 7.0)

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?

like image 754
noseratio Avatar asked Oct 22 '19 23:10

noseratio


1 Answers

It took me some noticeable time to figure out how to call registerBufferProtocol properly with Electron 7.0, so sharing this with the community 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 completion arg passed to the protocol.registerBufferProtocol API itself.

like image 180
noseratio Avatar answered Oct 28 '22 01:10

noseratio