pax_global_header00006660000000000000000000000064132701101620014503gustar00rootroot0000000000000052 comment=0764871db4b1a115842c36e1efd520013401fe28 node-request-capture-har-1.2.2/000077500000000000000000000000001327011016200163315ustar00rootroot00000000000000node-request-capture-har-1.2.2/.editorconfig000066400000000000000000000002231327011016200210030ustar00rootroot00000000000000root = true [*] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true node-request-capture-har-1.2.2/.gitignore000066400000000000000000000000651327011016200203220ustar00rootroot00000000000000.idea output node_modules request-har-capture.test.jsnode-request-capture-har-1.2.2/.travis.yml000066400000000000000000000001011327011016200204320ustar00rootroot00000000000000language: node_js node_js: - "stable" script: "npm run travis" node-request-capture-har-1.2.2/LICENSE.txt000066400000000000000000000020661327011016200201600ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2016 Lars Thorup Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. node-request-capture-har-1.2.2/README.md000066400000000000000000000035221327011016200176120ustar00rootroot00000000000000# request-capture-har > Wrapper for [`request` module](https://www.npmjs.com/package/request) that saves all network traffic data as a HAR file. [![Build Status](https://travis-ci.org/paulirish/request-capture-har.png)](https://travis-ci.org/paulirish/request-capture-har) [![NPM request-capture-har package](https://img.shields.io/npm/v/request-capture-har.svg)](https://npmjs.org/package/request-capture-har) **Compatibility** request >= 2.81.0 recommended, as it has much more detailed timings via `timingPhases`. request >= v2.75.0 required, at a minimum. ### Usage ```js // wrap around your request module const RCH = require('request-capture-har'); const requestCaptureHar = new RCH(require('request')); // ... // `requestCaptureHar.request` is your `request` module's API. // ... requestCaptureHar.request(uri, options, callback); // Save HAR file to disk requestCaptureHar.saveHar(`network-waterfall_${new Date().toISOString()}.har`); // You can also clear any collected traffic requestCaptureHar.clearHar(); ``` This repo is a fork of [larsthorup's `node-request-har-capture`](https://github.com/larsthorup/node-request-har-capture). Instead of monkey-patching `request-promise`, the API allows you to pass in the general `request` module. We also added better support for transfer timings. ![image](https://cloud.githubusercontent.com/assets/39191/18031306/9401070c-6c8f-11e6-994d-03e6b8b511e4.png) _Above is a HAR captured by using `request-capture-har` from within `npm` to capture an `npm install`._ ### Background This is especially useful for capturing all test traffic from your back-end test suite, for doing auto mocking in your front-end test suite. See this project for an example: https://github.com/larsthorup/http-auto-mock-demo. Blog post about this technique: http://www.zealake.com/2015/01/05/unit-test-your-service-integration-layer/ node-request-capture-har-1.2.2/package.json000066400000000000000000000015351327011016200206230ustar00rootroot00000000000000{ "name": "request-capture-har", "version": "1.2.2", "description": "Wrapper for request module that saves all traffic as a HAR file, useful for auto mocking a client", "main": "request-capture-har.js", "scripts": { "test": "semistandard", "travis": "npm test && node request-capture-har.js" }, "repository": { "type": "git", "url": "git+https://github.com/paulirish/node-request-capture-har.git" }, "keywords": [ "http", "request", "har" ], "author": "Lars Thorup (http://github.com/larsthorup)", "license": "MIT", "bugs": { "url": "https://github.com/paulirish/node-request-capture-har/issues" }, "homepage": "https://github.com/paulirish/node-request-capture-har#readme", "files": [ "request-capture-har.js" ], "devDependencies": { "semistandard": "^8.0.0" } } node-request-capture-har-1.2.2/request-capture-har.js000066400000000000000000000074411327011016200225760ustar00rootroot00000000000000var fs = require('fs'); var pkg = require('./package.json'); function buildHarHeaders (headers) { return headers ? Object.keys(headers).map(function (key) { return { name: key, // header values are required to be strings value: headers[key].toString() }; }) : []; } function appendPostData (entry, request) { if (!request.body) return; entry.request.postData = { mimeType: 'application/octet-stream', text: request.body }; } function toMs (num) { return Math.round(num * 1000) / 1000; } function HarWrapper (requestModule) { this.requestModule = requestModule; this.clear(); } HarWrapper.prototype.request = function (options) { // include detailed timing data in response object Object.assign(options, { time: true }); var self = this; // make call to true request module return this.requestModule(options, function (err, incomingMessage, response) { // create new har entry with reponse timings if (!err) { self.entries.push(self.buildHarEntry(incomingMessage)); } // fire any callback provided in options, as request has ignored it // https://github.com/request/request/blob/v2.75.0/index.js#L40 if (typeof options.callback === 'function') { options.callback.apply(null, arguments); } }); }; HarWrapper.prototype.clear = function () { this.entries = []; this.earliestTime = new Date(2099, 1, 1); }; HarWrapper.prototype.saveHar = function (fileName) { var httpArchive = { log: { version: '1.2', creator: {name: 'request-capture-har', version: pkg.version}, pages: [{ startedDateTime: new Date(this.earliestTime).toISOString(), id: 'request-capture-har', title: 'request-capture-har', pageTimings: { } }], entries: this.entries } }; fs.writeFileSync(fileName, JSON.stringify(httpArchive, null, 2)); }; HarWrapper.prototype.buildTimings = function (entry, response) { var startTs = response.request.startTime; var endTs = startTs + response.elapsedTime; var totalTime = endTs - startTs; if (new Date(startTs) < this.earliestTime) { this.earliestTime = new Date(startTs); } entry.startedDateTime = new Date(startTs).toISOString(); entry.time = totalTime; // new timing data added in request 2.81.0 if (response.timingPhases) { entry.timings = { 'blocked': toMs(response.timingPhases.wait), 'dns': toMs(response.timingPhases.dns), 'connect': toMs(response.timingPhases.tcp), 'send': 0, 'wait': toMs(response.timingPhases.firstByte), 'receive': toMs(response.timingPhases.download) }; return; } var responseStartTs = response.request.response.responseStartTime; var waitingTime = responseStartTs - startTs; var receiveTime = endTs - responseStartTs; entry.timings = { send: 0, wait: waitingTime, receive: receiveTime }; }; HarWrapper.prototype.buildHarEntry = function (response) { var entry = { request: { method: response.request.method, url: response.request.uri.href, httpVersion: 'HTTP/' + response.httpVersion, cookies: [], headers: buildHarHeaders(response.request.headers), queryString: [], headersSize: -1, bodySize: -1 }, response: { status: response.statusCode, statusText: response.statusMessage, httpVersion: 'HTTP/' + response.httpVersion, cookies: [], headers: buildHarHeaders(response.headers), _transferSize: response.body.length, content: { size: response.body.length, mimeType: response.headers['content-type'] }, redirectURL: '', headersSize: -1, bodySize: -1 }, cache: {} }; this.buildTimings(entry, response); appendPostData(entry, response.request); return entry; }; module.exports = HarWrapper; node-request-capture-har-1.2.2/test/000077500000000000000000000000001327011016200173105ustar00rootroot00000000000000node-request-capture-har-1.2.2/test/istanbul.conf.js000066400000000000000000000002621327011016200224130ustar00rootroot00000000000000module.exports = { verbose: false, reporting: { print: 'none', dir: './output/coverage', reports: [ 'lcov', 'json', 'text-summary' ] } }; node-request-capture-har-1.2.2/test/mocha.main.js000066400000000000000000000000531327011016200216560ustar00rootroot00000000000000var chai = require('chai'); chai.should();