2016-09-15 13:59:40 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
const {EventEmitter} = require('events')
|
2016-09-15 13:59:40 +00:00
|
|
|
const binding = process.atomBinding('net')
|
2016-09-19 09:21:09 +00:00
|
|
|
const {net, Net} = binding
|
|
|
|
const {URLRequest} = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
Object.setPrototypeOf(Net.prototype, EventEmitter.prototype)
|
|
|
|
Object.setPrototypeOf(URLRequest.prototype, EventEmitter.prototype)
|
|
|
|
|
2016-09-19 13:06:13 +00:00
|
|
|
class URLResponse extends EventEmitter {
|
2016-09-21 07:23:00 +00:00
|
|
|
constructor(request) {
|
|
|
|
super();
|
|
|
|
this.request = request;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get statusCode() {
|
|
|
|
return this.request.statusCode;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get statusMessage() {
|
|
|
|
return this.request.statusMessage;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get headers() {
|
|
|
|
return this.request.rawResponseHeaders;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersion() {
|
|
|
|
return `${this.httpVersionMajor}.${this.httpVersionMinor}`;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersionMajor() {
|
|
|
|
return this.request.httpVersionMajor;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
get httpVersionMinor() {
|
|
|
|
return this.request.httpVersionMinor;
|
|
|
|
}
|
|
|
|
|
|
|
|
get rawHeaders() {
|
|
|
|
return this.request.rawResponseHeaders;
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Net.prototype.request = function(options, callback) {
|
2016-09-21 07:23:00 +00:00
|
|
|
let request = new URLRequest(options)
|
2016-09-19 13:06:13 +00:00
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
if (callback) {
|
|
|
|
request.once('response', callback)
|
|
|
|
}
|
2016-09-19 13:06:13 +00:00
|
|
|
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
return request
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
URLRequest.prototype._emitRequestEvent = function(name) {
|
|
|
|
if (name === 'response') {
|
2016-09-19 13:06:13 +00:00
|
|
|
this.response = new URLResponse(this);
|
2016-09-21 07:23:00 +00:00
|
|
|
this.emit(name, this.response);
|
|
|
|
} else {
|
|
|
|
this.emit.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
URLRequest.prototype._emitResponseEvent = function() {
|
|
|
|
this.response.emit.apply(this.response, arguments);
|
2016-09-19 13:06:13 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 07:23:00 +00:00
|
|
|
|
2016-09-19 09:21:09 +00:00
|
|
|
module.exports = net
|
2016-09-15 13:59:40 +00:00
|
|
|
|