protocol.registerSchemesAsPrivileged method
registerSchemesAsPrivileged(customSchemes) registers schemes as standard, secure, bypassing content security policy for resources, allowing ServiceWorker registration, supporting fetch API, and streaming video/audio. This method can only be used before the app ready event and can be called only once. It accepts an array of CustomScheme objects.
registerSchemesAsPrivileged privileges capabilities
The privileges object in registerSchemesAsPrivileged can specify: bypassCSP (bool) to bypass Content Security Policy, standard (bool) to adhere to RFC 3986 generic URI syntax for relative/absolute resource resolution, secure (bool), supportFetchAPI (bool), allowServiceWorkers (bool), and stream (bool) for protocols using streams to configure video/audio buffering.
standard scheme allows FileSystem API access
Registering a scheme as standard allows access to files through the FileSystem API. Without this, the renderer throws a security error for the scheme.
web storage APIs disabled for non-standard schemes
By default web storage apis (localStorage, sessionStorage, webSQL, indexedDB, cookies) are disabled for non-standard schemes. To replace the http protocol with a custom protocol, the custom protocol must be registered as a standard scheme.
stream flag for video and audio protocols
Protocols that use streams (http and stream protocols) should set stream: true. The <video> and <audio> HTML elements expect protocols to buffer their responses by default. The stream flag configures those elements to correctly expect streaming responses.
protocol.handle method
protocol.handle(scheme, handler) registers a protocol handler for the given scheme. Requests to URLs with this scheme delegate to the handler. The handler receives a GlobalRequest object and must return either a Response or Promise<Response>. The scheme is the part before the colon in a URL.
protocol.handle example with custom responses
Example showing protocol.handle() usage:
const { app, net, protocol } = require('electron')
const path = require('node:path')
const { pathToFileURL } = require('node:url')
protocol.registerSchemesAsPrivileged([
{
scheme: 'app',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true
}
}
])
app.whenReady().then(() => {
protocol.handle('app', (req) => {
const { host, pathname } = new URL(req.url)
if (host === 'bundle') {
if (pathname === '/') {
return new Response('<h1>hello, world</h1>', {
headers: { 'content-type': 'text/html' }
})
}
const pathToServe = path.resolve(__dirname, pathname)
const relativePath = path.relative(__dirname, pathToServe)
const isSafe = relativePath && !relativePath.startsWith('..') && !path.isAbsolute(relativePath)
if (!isSafe) {
return new Response('bad', {
status: 400,
headers: { 'content-type': 'text/html' }
})
}
return net.fetch(pathToFileURL(pathToServe).toString())
} else if (host === 'api') {
return net.fetch('https://api.my-server.com/' + pathname, {
method: req.method,
headers: req.headers,
body: req.body
})
}
})
})
protocol.unhandle method
protocol.unhandle(scheme) removes a protocol handler previously registered with protocol.handle().
protocol.isProtocolHandled method
protocol.isProtocolHandled(scheme) returns a boolean indicating whether the scheme is already handled.
protocol.registerFileProtocol deprecated
protocol.registerFileProtocol(scheme, handler) is deprecated. The register*Protocol and intercept*Protocol methods have been replaced with protocol.handle(). This method registered a protocol that sends a file as the response, with handler receiving ProtocolRequest and a callback.
protocol.registerBufferProtocol example
Example of registerBufferProtocol:
protocol.registerBufferProtocol('atom', (request, callback) => {
callback({ mimeType: 'text/html', data: Buffer.from('<h5>Response</h5>') })
})
protocol.registerStreamProtocol deprecated
protocol.registerStreamProtocol(scheme, handler) is deprecated in favor of protocol.handle(). It registered a protocol that sends a stream as a response. The handler receives ProtocolRequest and callback, which should be called with either a ReadableStream object or an object with a data property.
protocol.registerStreamProtocol example with PassThrough
Example of registerStreamProtocol with PassThrough stream:
const { protocol } = require('electron')
const { PassThrough } = require('node:stream')
function createStream (text) {
const rv = new PassThrough()
rv.push(text)
rv.push(null)
return rv
}
protocol.registerStreamProtocol('atom', (request, callback) => {
callback({
statusCode: 200,
headers: {
'content-type': 'text/html'
},
data: createStream('<h5>Response</h5>')
})
})
protocol.registerStreamProtocol example with file stream
Example of registerStreamProtocol returning a file stream:
protocol.registerStreamProtocol('atom', (request, callback) => {
callback(fs.createReadStream('index.html'))
})
Any object that implements the readable stream API (emits data/end/error events) can be passed.
protocol.unregisterProtocol deprecated
protocol.unregisterProtocol(scheme) is deprecated in favor of protocol.unhandle(). It unregisters a custom protocol and returns a boolean indicating whether the protocol was successfully unregistered.
protocol.isProtocolRegistered deprecated
protocol.isProtocolRegistered(scheme) is deprecated in favor of protocol.isProtocolHandled(). It returns a boolean indicating whether the scheme is already registered.
protocol.interceptBufferProtocol deprecated
protocol.interceptBufferProtocol(scheme, handler) is deprecated in favor of protocol.handle(). It intercepts a scheme protocol and uses handler as the protocol's new handler which sends a Buffer as a response.
protocol.interceptHttpProtocol deprecated
protocol.interceptHttpProtocol(scheme, handler) is deprecated in favor of protocol.handle(). It intercepts a scheme protocol and uses handler as the protocol's new handler which sends a new HTTP request as a response.
protocol.interceptStreamProtocol deprecated
protocol.interceptStreamProtocol(scheme, handler) is deprecated in favor of protocol.handle(). It is the same as registerStreamProtocol, except that it replaces an existing protocol handler.
protocol.uninterceptProtocol deprecated
protocol.uninterceptProtocol(scheme) is deprecated in favor of protocol.unhandle(). It removes the interceptor installed for the scheme and restores its original handler, returning a boolean indicating success.
protocol.isProtocolIntercepted deprecated
protocol.isProtocolIntercepted(scheme) is deprecated in favor of protocol.isProtocolHandled(). It returns a boolean indicating whether the scheme is already intercepted.