# protocol (NetworkService) (Draft) This document describes the new protocol APIs based on the [NetworkService](https://www.chromium.org/servicification). We don't currently have an estimate of when we will enable the `NetworkService` by default in Electron, but as Chromium is already removing non-`NetworkService` code, we will probably switch before Electron 10. The content of this document should be moved to `protocol.md` after we have enabled the `NetworkService` by default in Electron. > Register a custom protocol and intercept existing protocol requests. Process: [Main](../glossary.md#main-process) An example of implementing a protocol that has the same effect as the `file://` protocol: ```javascript const { app, protocol } = require('electron') const path = require('path') app.on('ready', () => { protocol.registerFileProtocol('atom', (request, callback) => { const url = request.url.substr(7) callback({ path: path.normalize(`${__dirname}/${url}`) }) }) }) ``` **Note:** All methods unless specified can only be used after the `ready` event of the `app` module gets emitted. ## Using `protocol` with a custom `partition` or `session` A protocol is registered to a specific Electron [`session`](./session.md) object. If you don't specify a session, then your `protocol` will be applied to the default session that Electron uses. However, if you define a `partition` or `session` on your `browserWindow`'s `webPreferences`, then that window will use a different session and your custom protocol will not work if you just use `electron.protocol.XXX`. To have your custom protocol work in combination with a custom session, you need to register it to that session explicitly. ```javascript const { session, app, protocol } = require('electron') const path = require('path') app.on('ready', () => { const partition = 'persist:example' const ses = session.fromPartition(partition) ses.protocol.registerFileProtocol('atom', (request, callback) => { const url = request.url.substr(7) callback({ path: path.normalize(`${__dirname}/${url}`) }) }) mainWindow = new BrowserWindow({ webPreferences: { partition } }) }) ``` ## Methods The `protocol` module has the following methods: ### `protocol.registerSchemesAsPrivileged(customSchemes)` * `customSchemes` [CustomScheme[]](structures/custom-scheme.md) **Note:** This method can only be used before the `ready` event of the `app` module gets emitted and can be called only once. Registers the `scheme` as standard, secure, bypasses content security policy for resources, allows registering ServiceWorker and supports fetch API. Specify a privilege with the value of `true` to enable the capability. An example of registering a privileged scheme, that bypasses Content Security Policy: ```javascript const { protocol } = require('electron') protocol.registerSchemesAsPrivileged([ { scheme: 'foo', privileges: { bypassCSP: true } } ]) ``` A standard scheme adheres to what RFC 3986 calls [generic URI syntax](https://tools.ietf.org/html/rfc3986#section-3). For example `http` and `https` are standard schemes, while `file` is not. Registering a scheme as standard allows relative and absolute resources to be resolved correctly when served. Otherwise the scheme will behave like the `file` protocol, but without the ability to resolve relative URLs. For example when you load following page with custom protocol without registering it as standard scheme, the image will not be loaded because non-standard schemes can not recognize relative URLs: ```html
``` Registering a scheme as standard will allow access to files through the [FileSystem API][file-system-api]. Otherwise the renderer will throw a security error for the scheme. By default web storage apis (localStorage, sessionStorage, webSQL, indexedDB, cookies) are disabled for non standard schemes. So in general if you want to register a custom protocol to replace the `http` protocol, you have to register it as a standard scheme. ### `protocol.registerFileProtocol(scheme, handler)` * `scheme` String * `handler` Function * `request` ProtocolRequest * `callback` Function * `response` (String | [ProtocolResponse](structures/protocol-response.md)) Registers a protocol of `scheme` that will send a file as the response. The `handler` will be called with `request` and `callback` where `request` is an incoming request for the `scheme`. To handle the `request`, the `callback` should be called with either the file's path or an object that has a `path` property, e.g. `callback(filePath)` or `callback({ path: filePath })`. The `filePath` must be an absolute path. By default the `scheme` is treated like `http:`, which is parsed differently from protocols that follow the "generic URI syntax" like `file:`. ### `protocol.registerBufferProtocol(scheme, handler)` * `scheme` String * `handler` Function * `request` ProtocolRequest * `callback` Function * `response` (Buffer | [ProtocolResponse](structures/protocol-response.md)) Registers a protocol of `scheme` that will send a `Buffer` as a response. The usage is the same with `registerFileProtocol`, except that the `callback` should be called with either a `Buffer` object or an object that has the `data` property. Example: ```javascript protocol.registerBufferProtocol('atom', (request, callback) => { callback({ mimeType: 'text/html', data: Buffer.from('