Add documentation for protocol module.
This commit is contained in:
parent
c7fed48c4a
commit
128d9c78db
2 changed files with 70 additions and 1 deletions
|
@ -31,8 +31,9 @@ Browser side modules:
|
||||||
* [menu](menu.md)
|
* [menu](menu.md)
|
||||||
* [menu-item](menu-item.md)
|
* [menu-item](menu-item.md)
|
||||||
* [power-monitor](power-monitor.md)
|
* [power-monitor](power-monitor.md)
|
||||||
|
* [protocol](protocol.md)
|
||||||
|
|
||||||
Common modules:
|
Common modules:
|
||||||
|
|
||||||
* [clipboard](clipboard.md)
|
* [clipboard](clipboard.md)
|
||||||
* [shell](shell.md)
|
* [shell](shell.md)
|
||||||
|
|
68
docs/protocol.md
Normal file
68
docs/protocol.md
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
## Synopsis
|
||||||
|
|
||||||
|
The `protocol` module can register a new protocol or intercept an existing
|
||||||
|
protocol, so you can custom the response to the requests for vairous protocols.
|
||||||
|
|
||||||
|
An example of implementing a protocol that has the same effect with the
|
||||||
|
`file://` protocol:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var protocol = require('protocol');
|
||||||
|
protocol.registerProtocol('atom', function(request) {
|
||||||
|
var path = request.url.substr(7)
|
||||||
|
return new protocol.RequestFileJob(path);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## protocol.registerProtocol(scheme, handler)
|
||||||
|
|
||||||
|
* `scheme` String
|
||||||
|
* `handler` Function
|
||||||
|
|
||||||
|
Registers a custom protocol of `scheme`, the `handler` would be called with
|
||||||
|
`handler(request)` when the a request with registered `scheme` is made.
|
||||||
|
|
||||||
|
You need to return a request job in the `handler` to specify which type of
|
||||||
|
response you would like to send.
|
||||||
|
|
||||||
|
## protocol.unregisterProtocol(scheme)
|
||||||
|
|
||||||
|
* `scheme` String
|
||||||
|
|
||||||
|
Unregisters the custom protocol of `scheme`.
|
||||||
|
|
||||||
|
## protocol.isHandledProtocol(scheme)
|
||||||
|
|
||||||
|
* `scheme` String
|
||||||
|
|
||||||
|
Returns whether the `scheme` can be handled already.
|
||||||
|
|
||||||
|
## protocol.interceptProtocol(scheme, handler)
|
||||||
|
|
||||||
|
* `scheme` String
|
||||||
|
* `handler` Function
|
||||||
|
|
||||||
|
Intercepts an existing protocol with `scheme`, returning `null` or `undefined`
|
||||||
|
in `handler` would use the original protocol handler to handle the request.
|
||||||
|
|
||||||
|
## protocol.uninterceptProtocol(scheme)
|
||||||
|
|
||||||
|
* `scheme` String
|
||||||
|
|
||||||
|
Unintercepts a protocol.
|
||||||
|
|
||||||
|
## Class: protocol.RequestFileJob(path)
|
||||||
|
|
||||||
|
* `path` String
|
||||||
|
|
||||||
|
Create a request job which would query a file of `path` and set corresponding
|
||||||
|
mime types.
|
||||||
|
|
||||||
|
## Class: protocol.RequestStringJob(options)
|
||||||
|
|
||||||
|
* `options` Object
|
||||||
|
* `mimeType` String - Default is `text/plain`
|
||||||
|
* `charset` String - Default is `UTF-8`
|
||||||
|
* `data` String
|
||||||
|
|
||||||
|
Create a request job which sends a string as response.
|
Loading…
Reference in a new issue