[Pkg-javascript-commits] [node-request-capture-har] 01/03: Import Upstream version 1.1.4
Paolo Greppi
paolog-guest at moszumanska.debian.org
Mon Dec 12 09:47:21 UTC 2016
This is an automated email from the git hooks/post-receive script.
paolog-guest pushed a commit to branch master
in repository node-request-capture-har.
commit c185e2a983255f26eca9cc956bbc8c294bd22379
Author: Paolo Greppi <paolo.greppi at libpf.com>
Date: Mon Dec 12 09:39:52 2016 +0000
Import Upstream version 1.1.4
---
LICENSE.txt | 21 ++++++++++
README.md | 34 ++++++++++++++++
package.json | 31 +++++++++++++++
request-capture-har.js | 106 +++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 192 insertions(+)
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..32d5610
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,21 @@
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..197ac31
--- /dev/null
+++ b/README.md
@@ -0,0 +1,34 @@
+# 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/
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..f71f0ad
--- /dev/null
+++ b/package.json
@@ -0,0 +1,31 @@
+{
+ "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 <lars at zealake.com> (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"
+ }
+}
diff --git a/request-capture-har.js b/request-capture-har.js
new file mode 100644
index 0000000..54bedc3
--- /dev/null
+++ b/request-capture-har.js
@@ -0,0 +1,106 @@
+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;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-request-capture-har.git
More information about the Pkg-javascript-commits
mailing list