package/package.json000644 333342 011610 0000001476 12767740247013050 0ustar00000000 000000 { "name": "request-capture-har", "version": "1.1.4", "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" }, "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" } } package/README.md000644 333342 011610 0000003443 12767740174012034 0ustar00000000 000000 # 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) [request >= v2.75.0](https://github.com/request/request/releases) required. ### Usage ```js // wrap around your request module const RequestCaptureHar = require('request-capture-har'); const requestCaptureHar = new RequestCaptureHar(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/ package/request-capture-har.js000644 333342 011610 0000005454 12764155547015021 0ustar00000000 000000 var fs = require('fs'); var pkg = require('./package.json'); function buildHarHeaders (headers) { return headers ? Object.keys(headers).map(function (key) { return { name: key, value: headers[key] }; }) : []; } function buildPostData (body) { return body ? { mimeType: 'application/json', text: body } : null; } function HarWrapper (requestModule) { this.requestModule = requestModule; this.clear(); } HarWrapper.prototype.request = function (options) { Object.assign(options, { time: true }); var self = this; return this.requestModule(options, function (err, incomingMessage, response) { if (err) return; self.entries.push(self.buildHarEntry(incomingMessage)); }); }; 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.buildHarEntry = function (response) { var startTimestamp = response.request.startTime; var responseStartTimestamp = response.request.response.responseStartTime; var endTimestamp = startTimestamp + response.elapsedTime; var waitingTime = responseStartTimestamp - startTimestamp; var totalTime = endTimestamp - startTimestamp; var receiveTime = endTimestamp - responseStartTimestamp; this.earliestTime = Math.min(new Date(startTimestamp), this.earliestTime); var entry = { startedDateTime: new Date(startTimestamp).toISOString(), time: totalTime, request: { method: response.request.method, url: response.request.uri.href, httpVersion: 'HTTP/' + response.httpVersion, cookies: [], headers: buildHarHeaders(response.request.headers), queryString: [], postData: buildPostData(response.request.body), 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: {}, timings: { send: -1, wait: waitingTime, receive: receiveTime } }; return entry; }; module.exports = HarWrapper; package/LICENSE.txt000644 333342 011610 0000002066 12760454002012361 0ustar00000000 000000 The 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.