-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a437da
commit 1641ce3
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
const url = require('url'); | ||
|
||
class Timings { | ||
constructor() { | ||
this.blocked; | ||
this.dns; | ||
this.connect; | ||
this.send; | ||
this.wait; | ||
this.receive; | ||
this.ssl; | ||
} | ||
} | ||
|
||
class Request { | ||
constructor(request) { | ||
this.method = request.method; | ||
this.url = request.path; | ||
this.httpVersion = 'HTTP/' + request.httpVersion; | ||
this.headers = Object.keys(request._headers).map((key) => { | ||
return { | ||
name: key, | ||
value: request._headers[key] | ||
}; | ||
}); // Array | ||
var query = url.parse(request.path, true).query; | ||
this.queryString = Object.keys(query).map((key) => { | ||
return { | ||
name: key, | ||
value: query[key], | ||
comment: '' | ||
}; | ||
}); // Array | ||
var cookie = request._headers.cookie; | ||
this.cookies = (cookie ? cookie.split('; ') : []) | ||
.map((pair) => { | ||
var [name, value] = pair.split('='); | ||
return { | ||
name, | ||
value, | ||
expires: null, | ||
httpOnly: false, | ||
secure: false | ||
}; | ||
}); | ||
this.headersSize = Buffer.byteLength(request._header); // | ||
this.bodySize = request.connection._bytesDispatched - this.headersSize; // | ||
} | ||
} | ||
|
||
class Response { | ||
constructor(response) { | ||
this.status = response.statusCode; | ||
this.statusText = response.statusMessage; | ||
this.httpVersion = 'HTTP/' + response.httpVersion; | ||
this.headers = Object.keys(response.headers).map((key) => { | ||
return { | ||
name: key, | ||
value: response.headers[key] | ||
}; | ||
}); | ||
var cookie = response.headers['set-cookie']; | ||
this.cookies = cookie; | ||
this.content = { | ||
size: 0, | ||
mimeType: response.headers['content-type'], | ||
compression: 0, | ||
text: '' | ||
}; | ||
this.redirectURL = ''; | ||
this.headersSize = 0; | ||
this.bodySize = 0; | ||
this._transferSize = this.headersSize + this.bodySize; | ||
} | ||
} | ||
|
||
class Entry { | ||
constructor(req, res) { | ||
this.startedDateTime = undefined; // '2017-01-16T03:15:42.068Z' | ||
this.time = undefined; // 79.48899996699765 | ||
this.request = new Request(req); | ||
this.response = new Response(res); | ||
this.timings = new Timings(); | ||
this.serverIPAddress = req.socket.remoteAddress; | ||
} | ||
} | ||
|
||
module.exports = Entry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const httpx = require('./'); | ||
const Entry = require('./lib/entry'); | ||
|
||
var url = 'https://opencollective.com/mochajs/sponsors/badge.svg'; | ||
|
||
httpx.request(url, { | ||
enableCompress: true | ||
}).then((response) => { | ||
var entry = new Entry(response.req, response); | ||
console.log(JSON.stringify(entry, null, 2)); | ||
return httpx.read(response, 'utf8'); | ||
}).then((content) => { | ||
console.log(content); | ||
}, (err) => { | ||
// on error | ||
console.error(err.stack); | ||
}); |