最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dealing with "ProtocolDeprecateCallback: The callback argument of protocol module APIs is no longer needed

programmeradmin2浏览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?

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?

Share Improve this question asked Oct 22, 2019 at 23:40 noserationoseratio 61.7k36 gold badges223 silver badges500 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

It 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论