2014-10-30 14:27:29 +00:00
|
|
|
app = require 'app'
|
|
|
|
throw new Error('Can not initialize protocol module before app is ready') unless app.isReady()
|
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol = process.atomBinding('protocol').protocol
|
2013-08-29 12:38:04 +00:00
|
|
|
EventEmitter = require('events').EventEmitter
|
2013-08-25 07:07:07 +00:00
|
|
|
|
2014-04-21 08:33:32 +00:00
|
|
|
protocol.__proto__ = EventEmitter.prototype
|
2013-08-29 12:38:04 +00:00
|
|
|
|
2015-07-07 14:43:13 +00:00
|
|
|
GetWrappedCallback = (scheme, callback, notification) ->
|
|
|
|
wrappedCallback = (error) ->
|
|
|
|
if not callback?
|
|
|
|
if error
|
|
|
|
throw error
|
|
|
|
else
|
|
|
|
protocol.emit notification, scheme
|
|
|
|
else
|
|
|
|
callback error, scheme
|
|
|
|
|
|
|
|
# Compatibility with old api.
|
|
|
|
protocol.registerProtocol = (scheme, handler, callback) ->
|
|
|
|
protocol._registerProtocol scheme, handler, GetWrappedCallback(scheme, callback, 'registered')
|
|
|
|
|
|
|
|
protocol.unregisterProtocol = (scheme, callback) ->
|
|
|
|
protocol._unregisterProtocol scheme, GetWrappedCallback(scheme, callback, 'unregistered')
|
|
|
|
|
|
|
|
protocol.interceptProtocol = (scheme, handler, callback) ->
|
|
|
|
protocol._interceptProtocol scheme, handler, GetWrappedCallback(scheme, callback, 'intercepted')
|
|
|
|
|
|
|
|
protocol.uninterceptProtocol = (scheme, callback) ->
|
|
|
|
protocol._uninterceptProtocol scheme, GetWrappedCallback(scheme, callback, 'unintercepted')
|
|
|
|
|
2013-08-29 12:38:04 +00:00
|
|
|
protocol.RequestStringJob =
|
2013-08-25 07:07:07 +00:00
|
|
|
class RequestStringJob
|
|
|
|
constructor: ({mimeType, charset, data}) ->
|
|
|
|
if typeof data isnt 'string' and not data instanceof Buffer
|
|
|
|
throw new TypeError('Data should be string or Buffer')
|
|
|
|
|
|
|
|
@mimeType = mimeType ? 'text/plain'
|
|
|
|
@charset = charset ? 'UTF-8'
|
|
|
|
@data = String data
|
2013-08-25 08:06:29 +00:00
|
|
|
|
2015-03-16 12:53:45 +00:00
|
|
|
protocol.RequestBufferJob =
|
|
|
|
class RequestBufferJob
|
|
|
|
constructor: ({mimeType, encoding, data}) ->
|
|
|
|
if not data instanceof Buffer
|
|
|
|
throw new TypeError('Data should be Buffer')
|
|
|
|
|
|
|
|
@mimeType = mimeType ? 'application/octet-stream'
|
|
|
|
@encoding = encoding ? 'utf8'
|
|
|
|
@data = new Buffer(data)
|
|
|
|
|
2013-08-29 12:38:04 +00:00
|
|
|
protocol.RequestFileJob =
|
2013-08-25 08:06:29 +00:00
|
|
|
class RequestFileJob
|
|
|
|
constructor: (@path) ->
|
2013-08-29 12:38:04 +00:00
|
|
|
|
2015-05-07 07:42:35 +00:00
|
|
|
protocol.RequestErrorJob =
|
|
|
|
class RequestErrorJob
|
|
|
|
constructor: (@error) ->
|
|
|
|
|
2015-05-29 15:54:00 +00:00
|
|
|
protocol.RequestHttpJob =
|
|
|
|
class RequestHttpJob
|
2015-06-17 03:20:09 +00:00
|
|
|
constructor: ({@url, @method, @referrer}) ->
|
2015-05-29 15:54:00 +00:00
|
|
|
|
2013-08-29 12:38:04 +00:00
|
|
|
module.exports = protocol
|