{"_id":"istanbul-middleware","_rev":"260126","name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","dist-tags":{"latest":"0.2.2"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"time":{"modified":"2021-06-03T16:42:33.000Z","created":"2013-04-12T04:15:27.342Z","0.2.2":"2015-11-26T06:25:28.463Z","0.2.1":"2015-10-17T06:03:46.327Z","0.2.0":"2014-07-05T16:35:53.316Z","0.1.1":"2013-09-07T14:29:49.151Z","0.1.0":"2013-04-12T04:15:27.342Z"},"users":{},"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"repository":{"type":"git","url":"git+ssh://git@github.com/gotwarlost/istanbul-middleware.git"},"versions":{"0.2.2":{"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","version":"0.2.2","repository":{"type":"git","url":"git+ssh://git@github.com/gotwarlost/istanbul-middleware.git"},"scripts":{"pretest":"jshint --config ./node_modules/yui-lint/jshint.json lib/*js","test":"./test/run.sh"},"contributors":[{"name":"@arboleya"},{"name":"@harking"},{"name":"@mikaturunen"},{"name":"@jelmertanen"},{"name":"@tkmunzwa"},{"name":"@mcollina"},{"name":"@lpinca"}],"dependencies":{"istanbul":"0.4.x","express":"4.x","body-parser":"~1.12.3","archiver":"0.14.x"},"devDependencies":{"mkdirp":"*","rimraf":"*","jshint":"*","async":"*","yui-lint":"*"},"engines":{"node":">=0.6.x"},"license":"BSD","gitHead":"d09c92e1f70716348780fe612afc84ccda794b87","bugs":{"url":"https://github.com/gotwarlost/istanbul-middleware/issues"},"homepage":"https://github.com/gotwarlost/istanbul-middleware#readme","_id":"istanbul-middleware@0.2.2","_shasum":"83c4c13c128e1a0d6a147792391af3c15a8ab8e0","_from":".","_npmVersion":"2.12.0","_nodeVersion":"0.10.26","_npmUser":{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"dist":{"shasum":"83c4c13c128e1a0d6a147792391af3c15a8ab8e0","size":9979,"noattachment":false,"key":"/istanbul-middleware/-/istanbul-middleware-0.2.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/istanbul-middleware/download/istanbul-middleware-0.2.2.tgz"},"directories":{},"publish_time":1448519128463,"_cnpm_publish_time":1448519128463,"_hasShrinkwrap":false},"0.2.1":{"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","version":"0.2.1","repository":{"type":"git","url":"git@github.com:gotwarlost/istanbul-middleware.git"},"scripts":{"pretest":"jshint --config ./node_modules/yui-lint/jshint.json lib/*js","test":"./test/run.sh"},"contributors":[{"name":"@arboleya"},{"name":"@harking"},{"name":"@mikaturunen"},{"name":"@jelmertanen"},{"name":"@tkmunzwa"},{"name":"@mcollina"}],"dependencies":{"istanbul":"0.4.x","express":"4.x","body-parser":"~1.12.3","archiver":"0.14.x"},"devDependencies":{"mkdirp":"*","rimraf":"*","jshint":"*","async":"*","yui-lint":"*"},"engines":{"node":">=0.6.x"},"license":"BSD","gitHead":"5ae35a785e47af786e99603162890128e81c7b58","bugs":{"url":"https://github.com/gotwarlost/istanbul-middleware/issues"},"homepage":"https://github.com/gotwarlost/istanbul-middleware","_id":"istanbul-middleware@0.2.1","_shasum":"04695ba2ae9503f0faaff03bc4a752c0ae965288","_from":".","_npmVersion":"2.5.1","_nodeVersion":"0.12.0","_npmUser":{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"dist":{"shasum":"04695ba2ae9503f0faaff03bc4a752c0ae965288","size":9951,"noattachment":false,"key":"/istanbul-middleware/-/istanbul-middleware-0.2.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/istanbul-middleware/download/istanbul-middleware-0.2.1.tgz"},"directories":{},"publish_time":1445061826327,"_cnpm_publish_time":1445061826327,"_hasShrinkwrap":false},"0.2.0":{"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","version":"0.2.0","repository":{"type":"git","url":"git@github.com:gotwarlost/istanbul-middleware.git"},"scripts":{"pretest":"jshint --config ./node_modules/yui-lint/jshint.json lib/*js","test":"./test/run.sh"},"contributors":[{"name":"@arboleya"},{"name":"@harking"},{"name":"@mikaturunen"},{"name":"@jelmertanen"},{"name":"@tkmunzwa"}],"dependencies":{"istanbul":"0.2.x","express":"4.x","body-parser":"~1.4.3","archiver":"0.10.x"},"devDependencies":{"mkdirp":"*","rimraf":"*","jshint":"*","async":"*","yui-lint":"*"},"engines":{"node":">=0.6.x"},"license":"BSD","gitHead":"f8e8a3fa78eb27b8b6f2d81d286e05e083371b97","bugs":{"url":"https://github.com/gotwarlost/istanbul-middleware/issues"},"homepage":"https://github.com/gotwarlost/istanbul-middleware","_id":"istanbul-middleware@0.2.0","_shasum":"82c7951a044e94322cda679e95b0c40290d358d6","_from":".","_npmVersion":"1.4.16","_npmUser":{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"dist":{"shasum":"82c7951a044e94322cda679e95b0c40290d358d6","size":9919,"noattachment":false,"key":"/istanbul-middleware/-/istanbul-middleware-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/istanbul-middleware/download/istanbul-middleware-0.2.0.tgz"},"directories":{},"publish_time":1404578153316,"_cnpm_publish_time":1404578153316,"_hasShrinkwrap":false},"0.1.1":{"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","version":"0.1.1","repository":{"type":"git","url":"git@github.com:gotwarlost/istanbul-middleware.git"},"scripts":{"pretest":"jshint --config ./node_modules/yui-lint/jshint.json lib/*js","test":"./test/run.sh"},"dependencies":{"istanbul":"0.1.x","express":"3.x","archiver":"0.4.x"},"devDependencies":{"mkdirp":"*","rimraf":"*","jshint":"*","async":"*","yui-lint":"*"},"engines":{"node":">=0.6.x"},"license":"BSD","readmeFilename":"README.md","_id":"istanbul-middleware@0.1.1","dist":{"shasum":"ed0797344470738aff1bfdc590d18926bffa69ee","size":8421,"noattachment":false,"key":"/istanbul-middleware/-/istanbul-middleware-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/istanbul-middleware/download/istanbul-middleware-0.1.1.tgz"},"_from":".","_npmVersion":"1.2.14","_npmUser":{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"directories":{},"publish_time":1378564189151,"_cnpm_publish_time":1378564189151,"_hasShrinkwrap":false},"0.1.0":{"author":{"name":"Krishnan Anantheswaran","email":"kananthmail-github@yahoo-inc.com"},"name":"istanbul-middleware","description":"Connect middleware for Istanbul to handle server-side as well as browser coverage use-cases for node apps","version":"0.1.0","repository":{"type":"git","url":"git@github.com:gotwarlost/istanbul-middleware.git"},"scripts":{"pretest":"jshint --config ./node_modules/yui-lint/jshint.json lib/*js","test":"./test/run.sh"},"dependencies":{"istanbul":"0.1.x","express":"3.x","archiver":"0.4.x"},"devDependencies":{"mkdirp":"*","rimraf":"*","jshint":"*","async":"*","yui-lint":"*"},"engines":{"node":">=0.6.x"},"license":"BSD","readmeFilename":"README.md","_id":"istanbul-middleware@0.1.0","dist":{"shasum":"368479e0c6ae21558cc7cff84bb41dcac1da949f","size":8386,"noattachment":false,"key":"/istanbul-middleware/-/istanbul-middleware-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/istanbul-middleware/download/istanbul-middleware-0.1.0.tgz"},"_from":".","_npmVersion":"1.2.14","_npmUser":{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"},"maintainers":[{"name":"gotwarlost","email":"kananthmail-github@yahoo.com"}],"directories":{},"publish_time":1365740127342,"_cnpm_publish_time":1365740127342,"_hasShrinkwrap":false}},"readme":"istanbul-middleware\n===================\n\n[![Build Status](https://travis-ci.org/gotwarlost/istanbul-middleware.png?branch=master)](https://travis-ci.org/gotwarlost/istanbul-middleware) [![Dependency Status](https://gemnasium.com/gotwarlost/istanbul-middleware.png)](https://gemnasium.com/gotwarlost/istanbul-middleware)\n\nConnect middleware for getting code coverage numbers in functional tests for nodejs apps using istanbul.\n\nRun the sample app at `test/app` to get a feel for how this works.\n\nAll of this is experimental and is known to work for narrow use-cases such as an express3 app. YMMV.\n\nServer-side code coverage\n-------------------------\n\nThis involves:\n\n* hooking `require()` in the server process\n* exposing coverage information on special endpoints (e.g. `/coverage`)\n* allowing reset of coverage numbers to ensure clean slate\n* allowing users to download coverage reports after tests have run\n\n```javascript\nvar im = require('istanbul-middleware'),\n    isCoverageEnabled = (process.env.COVERAGE == \"true\"); // or a mechanism of your choice\n\n//before your code is require()-ed, hook the loader for coverage\nif (isCoverageEnabled) {\n    console.log('Hook loader for coverage - ensure this is not production!');\n    im.hookLoader(__dirname);\n        // cover all files except under node_modules\n        // see API for other options\n}\n\n// now require the rest of your code\nvar stuff = require('./lib'),\n    express = require('express'),\n    app = express();\n\n// set up basic middleware\n// ...\n\n// add the coverage handler\nif (isCoverageEnabled) {\n    //enable coverage endpoints under /coverage\n    app.use('/coverage', im.createHandler());\n}\n\n//add your router and other endpoints\n//...\n\napp.listen(80);\n```\n\nThe above snippet adds the following endpoints to your app under `/coverage`\n\n<table>\n<thead>\n    <tr>\n        <th>URL</th>\n        <th>Description</th>\n    </tr>\n</thead>\n<tbody>\n    <tr>\n        <td><code>GET&nbsp;/</code></td>\n        <td>\n            Dynamic code coverage HTML report showing live coverage.\n            Clickable  with drill-downs just like the static version\n        </td>\n    </tr>\n    <tr>\n        <td><code>POST&nbsp;/reset</code></td>\n        <td>Reset coverage to baseline numbers</td>\n    </tr>\n    <tr>\n        <td><code>GET&nbsp;/download</code></td>\n        <td>Download a zip file with coverage JSON, HTML and lcov reports</td>\n    </tr>\n    <tr>\n        <td><code>POST&nbsp;/client</code></td>\n        <td>\n            Allows you to post a coverage object for client-side code coverage from the browser.\n            Must be a JSON object with a <code>Content-type: application/json</code> header.\n            This object is aggregated with the stats already present on the server\n        </td>\n    </tr>\n</tbody>\n</table>\n\nClient-side coverage\n--------------------\n\nThis involves:\n\n* Delivering instrumented code instead of the original Javascript to the browser\n* Having your tests post the coverage information to the server (see `POST /client` endpoint above)\nusing the `window.__coverage__` object. You need to figure out how to do this using your favorite test runner.\n* Aggregating the client and server coverage numbers. This is automatically done for you by the server-side middleware.\n\nThe API for this is _highly_ experimental given the number of moving parts. But, it roughly looks like this:\n\n```javascript\nvar path = require('path'),\n    im = require('istanbul-middleware');\n\n// do the server side stuff as described above\n\n// add a client handler at the appropriate place\n// (before your static handler, for example)\n// all JS files under here will be sent instrumented to the browser\n// see API below for additional options (e.g. ignoring framework code)\napp.use(im.createClientHandler(__dirname));\n\n// however, it will only reliably work for simple cases\n// such as absolute URLs to the JS.\n// you still need to post the coverage numbers to the\n//server from your browser tests\n```\n\nYou can write your own custom middleware and completely ignore this library's client handler. As in:\n\n```javascript\napp.use(function (req, res, next) {\n    if (isJSRequiringCoverage(req)) {\n        var file = getFilePath(req), //translate request to file name to be delivered\n            code = readTheCodeFromFile(file), //use async for a real implementation\n            instrumenter = im.getInstrumenter();\n        res.send(instrumenter.instrumentSync(code, file));\n            //exception handling omitted for brevity\n    } else {\n        next();\n    }\n});\n```\n\nAPI\n---\n\n### `istanbulMiddleware.hookLoader(rootOrMatcher, instrumenterOpts)`\n\nhooks `require` for coverage using istanbul.\n\n`rootOrMatcher` can be:\n\n* a string in which case it is assumed to be the root under which you want to cover all files except those under `node_modules`\n* a function in which case it is assumed to be a match function with signature `fn(filePath)`\nthat should return `true` when the supplied `filePath` should be covered and `false` otherwise\n\n`instrumenterOpts` is an optional object with additional options to be passed to the istanbul instrumenter. See the\nAPI docs in istanbul for more information. In addition, options can also contain the `postLoadHook` key that is\npassed to `istanbul.hook.hookRequire()`\n\n\n### `istanbulMiddleware.createHandler(opts)`\n\nreturns connect middleware that exposes additional endpoints for coverage. Endpoints exposed are documented in the summary.\n\n`opts` is optional and currently only supports one flag.\n\n* `resetOnGet` - boolean to allow resets of coverage to baseline numbers using `GET` in addition to `POST`\n\n### `istanbulMiddleware.createClientHandler(root, opts)`\n\nreturns connect middleware similar to the `static` middleware to return instrumented JS to the client.\nThe default behavior of the middleware is to intercept all `GET` requests to Javascript files and return the\ninstrumented version by deriving the path of the file from the URL, instrumenting the code and sending the\ninstrumented version in the response.\n\n`opts` is an optional object with the following supported keys:\n\n* `matcher` - a function of the form `fn(request)` that returns true if instrumentation\nis required and false otherwise.\n* `pathTransformer` - a function of the form `fn(request)` that inspects the request URL\nand returns the physical path to the JS file on the filesystem.\n\nAn example of a matcher function could be:\n\n```javascript\nfunction ignoreFrameworks(req) {\n    var parsed = require('url').parse(req.url);\n    return parsed.pathname && parsed.pathname.match(/\\.js$/) && !parsed.pathname.match(/jquery/);\n}\n```\n\nFor all other cases where the client handler provided by this library is not good enough, just write your own\nmiddleware as documented in the summary.\n\n### `istanbulMiddleware.getInstrumenter()`\n\nreturns the instrumenter object that is created as a side-effect of the `hookLoader` call. Useful for custom\nclient-side instrumentation to ensure that the instrumentation is done with the same options for all code.\n\nThird-party libraries\n---------------------\n\nThe following third-party libraries are used by this module:\n\n* express: https://github.com/visionmedia/express -  to implement the middleware\n* archiver: https://github.com/ctalkington/node-archiver - for zip functionality\n\n","_attachments":{},"homepage":"https://github.com/gotwarlost/istanbul-middleware#readme","bugs":{"url":"https://github.com/gotwarlost/istanbul-middleware/issues"},"license":"BSD"}