{"_id":"diagnostic-channel","_rev":"2912451","name":"diagnostic-channel","description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dist-tags":{"latest":"1.1.0"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"time":{"modified":"2023-02-01T08:39:56.000Z","created":"2017-05-04T22:49:46.415Z","1.1.0":"2022-01-31T21:41:39.967Z","1.0.0":"2021-05-10T18:40:18.739Z","0.3.1":"2019-10-31T18:44:35.526Z","0.3.0":"2019-10-29T19:30:14.572Z","0.2.0":"2017-08-22T21:32:10.671Z","0.1.0":"2017-05-04T23:09:56.763Z","0.0.0":"2017-05-04T22:49:46.415Z"},"users":{},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"versions":{"1.1.0":{"name":"diagnostic-channel","version":"1.1.0","main":"./dist/src/channel.js","types":"./dist/src/channel.d.ts","scripts":{"build":"tsc","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./dist","test":"mocha ./dist/tests/**/*.js","debug":"mocha --inspect-brk ./dist/tests/**/*.js"},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"~8.0.0","mocha":"^3.2.0","rimraf":"^2.6.1","sinon":"1.17.6","tslint":"^5.0.0","typescript":"4.1.2"},"license":"MIT","gitHead":"e3176c0c9dd371e9eaea6f9c103a9b49c9742538","_id":"diagnostic-channel@1.1.0","_nodeVersion":"16.13.2","_npmVersion":"8.1.2","dist":{"shasum":"6985e9dfedfbc072d91dc4388477e4087147756e","size":6092,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-1.1.0.tgz"},"_npmUser":{"name":"msftapplicationinsights","email":"appinsightssdk@microsoft.com"},"directories":{},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/diagnostic-channel_1.1.0_1643665299843_0.6998975054800174"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-01-31T21:41:45.274Z","publish_time":1643665299967,"_cnpm_publish_time":1643665299967},"1.0.0":{"name":"diagnostic-channel","version":"1.0.0","main":"./dist/src/channel.js","types":"./dist/src/channel.d.ts","scripts":{"build":"tsc","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./dist","test":"mocha ./dist/tests/**/*.js"},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"~8.0.0","mocha":"^3.2.0","rimraf":"^2.6.1","tslint":"^5.0.0","typescript":"4.1.2"},"license":"MIT","_id":"diagnostic-channel@1.0.0","_nodeVersion":"14.16.1","_npmVersion":"6.14.12","_npmUser":{"name":"msftapplicationinsights","email":"appinsightssdk@microsoft.com"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"dist":{"shasum":"6d5611784ddb3d48086dd45a07d7431a945200aa","size":5657,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-1.0.0.tgz"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/diagnostic-channel_1.0.0_1620672018357_0.611400362826042"},"_hasShrinkwrap":false,"publish_time":1620672018739,"_cnpm_publish_time":1620672018739,"_cnpmcore_publish_time":"2021-12-16T17:07:43.165Z"},"0.3.1":{"name":"diagnostic-channel","version":"0.3.1","main":"./dist/src/channel.js","types":"./dist/src/channel.d.ts","scripts":{"build":"tsc","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./dist","test":"mocha ./dist/tests/**/*.js"},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"^7.0.12","mocha":"^3.2.0","rimraf":"^2.6.1","tslint":"^5.0.0","typescript":"^2.2.1"},"license":"MIT","_id":"diagnostic-channel@0.3.1","_nodeVersion":"11.10.0","_npmVersion":"6.7.0","dist":{"shasum":"7faa143e107f861be3046539eb4908faab3f53fd","size":5583,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-0.3.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-0.3.1.tgz"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmUser":{"name":"msftapplicationinsights","email":"appinsightssdk@microsoft.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/diagnostic-channel_0.3.1_1572547475404_0.8233597228135112"},"_hasShrinkwrap":false,"publish_time":1572547475526,"_cnpm_publish_time":1572547475526,"_cnpmcore_publish_time":"2021-12-16T17:07:43.489Z"},"0.3.0":{"name":"diagnostic-channel","version":"0.3.0","main":"./dist/src/channel.js","types":"./dist/src/channel.d.ts","scripts":{"build":"tsc","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./dist","test":"mocha ./dist/tests/**/*.js"},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"^7.0.12","mocha":"^3.2.0","rimraf":"^2.6.1","tslint":"^5.0.0","typescript":"^2.2.1"},"license":"MIT","_id":"diagnostic-channel@0.3.0","_nodeVersion":"11.10.0","_npmVersion":"6.7.0","dist":{"shasum":"7a3cdcefe9caa50ca7e8075be889029f8affb352","size":5644,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-0.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-0.3.0.tgz"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmUser":{"name":"msftapplicationinsights","email":"appinsightssdk@microsoft.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/diagnostic-channel_0.3.0_1572377414409_0.9670192371999604"},"_hasShrinkwrap":false,"publish_time":1572377414572,"_cnpm_publish_time":1572377414572,"_cnpmcore_publish_time":"2021-12-16T17:07:43.714Z"},"0.2.0":{"name":"diagnostic-channel","version":"0.2.0","main":"./dist/src/channel.js","types":"./dist/src/channel.d.ts","scripts":{"build":"tsc","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./dist","test":"mocha ./dist/tests/**/*.js"},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"repository":{"type":"git","url":"git+https://github.com/Microsoft/node-diagnostic-channel.git"},"description":"Provides a context-saving pub/sub channel to connect diagnostic event publishers and subscribers","dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"^7.0.12","mocha":"^3.2.0","rimraf":"^2.6.1","tslint":"^5.0.0","typescript":"^2.2.1"},"files":["dist/src/**/*.d.ts","dist/src/**/*.js","LICENSE","README.md","package.json"],"license":"MIT","_id":"diagnostic-channel@0.2.0","_shasum":"cc99af9612c23fb1fff13612c72f2cbfaa8d5a17","_resolved":"file:diagnostic-channel-0.2.0.tgz","_from":"diagnostic-channel-0.2.0.tgz","_npmVersion":"3.10.10","_nodeVersion":"6.11.1","_npmUser":{"name":"jackhorton","email":"jackpeterhorton@gmail.com"},"dist":{"shasum":"cc99af9612c23fb1fff13612c72f2cbfaa8d5a17","size":5492,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-0.2.0.tgz"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/diagnostic-channel-0.2.0.tgz_1503437530539_0.09619695995934308"},"directories":{},"publish_time":1503437530671,"_hasShrinkwrap":false,"_cnpm_publish_time":1503437530671,"_cnpmcore_publish_time":"2021-12-16T17:07:43.928Z"},"0.1.0":{"name":"diagnostic-channel","version":"0.1.0","main":"./.dist/channel.js","types":"./.dist/channel.d.ts","scripts":{"build":"tsc --outDir ./.dist","lint":"tslint -c tslint.json -p tsconfig.json","clean":"rimraf ./.dist","prepublish":"npm run lint && npm run build","test":"npm run build && mocha ./.dist/tests/**/*.js"},"dependencies":{"semver":"^5.3.0"},"devDependencies":{"@types/mocha":"^2.2.40","@types/node":"^7.0.12","mocha":"^3.2.0","rimraf":"^2.6.1","tslint":"^5.0.0","typescript":"^2.2.1"},"files":[".dist/*.d.ts",".dist/*.js","LICENSE","README.md","package.json"],"license":"MIT","description":"This package is intended to connect event 'publishers' to event 'subscribers' in such a way that the 'context' of the event is preserved. The exact nature of 'context' is deliberately left unspecified.","_id":"diagnostic-channel@0.1.0","_shasum":"7a6ad8ad5066bac544da0a379ba174ba362a57cf","_resolved":"file:diagnostic-channel-0.1.0.tgz","_from":"diagnostic-channel-0.1.0.tgz","_npmVersion":"4.1.2","_nodeVersion":"7.7.4","_npmUser":{"name":"jithomso","email":"jithomso@microsoft.com"},"dist":{"shasum":"7a6ad8ad5066bac544da0a379ba174ba362a57cf","size":5579,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-0.1.0.tgz"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/diagnostic-channel-0.1.0.tgz_1493939395456_0.8265960759017617"},"directories":{},"publish_time":1493939396763,"_hasShrinkwrap":false,"_cnpm_publish_time":1493939396763,"_cnpmcore_publish_time":"2021-12-16T17:07:44.168Z"},"0.0.0":{"name":"diagnostic-channel","version":"0.0.0","main":"index.js","license":"MIT","gitHead":"90891f134683e693d9023de10fcb80295255ced5","_id":"diagnostic-channel@0.0.0","scripts":{},"_shasum":"3ce0992261abadf62eb78ac9519cba26bdb63a14","_from":".","_npmVersion":"3.10.10","_nodeVersion":"6.9.4","_npmUser":{"name":"microsoft","email":"npmjs@microsoft.com"},"dist":{"shasum":"3ce0992261abadf62eb78ac9519cba26bdb63a14","size":225,"noattachment":false,"key":"/diagnostic-channel/-/diagnostic-channel-0.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/diagnostic-channel/download/diagnostic-channel-0.0.0.tgz"},"maintainers":[{"name":"npm:msftapplicationinsights","email":""}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/diagnostic-channel-0.0.0.tgz_1493938186152_0.9742514376994222"},"directories":{},"publish_time":1493938186415,"_hasShrinkwrap":false,"_cnpm_publish_time":1493938186415,"_cnpmcore_publish_time":"2021-12-16T17:07:44.354Z"}},"readme":"# diagnostic-channel\r\n\r\nThis package provides a channel to connect diagnostic event publishers and subscribers. It includes a way to preserve context for the published event too. What that context is and contains is deliberately left unspecified.\r\n\r\n## Usage\r\n\r\n### Subscribe to an event:\r\n\r\n```js\r\nconst channel = require('diagnostic-channel').channel;\r\n\r\nchannel.subscribe('someEvent', function (event) {\r\n    // do something about the event\r\n});\r\n```\r\n\r\nThe properties of the `event` object passed to the subscriber handler function\r\nare determined by the publisher.\r\n\r\n### Publish an event\r\n\r\n```js\r\nvar someData = { myField: \"myData\" };\r\nconst channel = require('diagnostic-channel').channel;\r\n\r\nchannel.publish('someEvent', someData);\r\n```\r\n\r\n### Publish an event with context\r\n\r\nPreserving context on publication may require some additional effort. A\r\ncorrelation handler can be added with the `addContextPreservation` method, and\r\nthen the `bindToContext` method can be used to set up context using the\r\nprovided handler before calling its received function.\r\n\r\n```js\r\nchannel.addContextPreservation((callback) => {\r\n    return Zone.current.wrap(callback);\r\n});\r\n\r\nfunction doWork(args, callback) {\r\n    // In some context...\r\n    doBatchedAsyncWork(args, channel.bindToContext((result) => {\r\n        channel.publish('someEvent', {result: result});\r\n        callback(result);\r\n    }))\r\n}\r\n```\r\n\r\n## API\r\n\r\n### subscribe\r\n\r\n`channel.subscribe(name: string, callback: (event: any) => void): void`\r\n\r\nRegister the callback to be called when `publish` is invoked with a matching name. The callback will be given the object that is passed to the `publish` call.\r\n\r\nIf the callback throws an error, it will be silently ignored. If the callback modifies the event object, any subsequent subscribers will see the modified object, and it may also impact the original code's execution.\r\n\r\n### publish\r\n\r\n`channel.publish(name: string, event: any): void`\r\n\r\nTrigger each subscribed callback for the same named event, passing the `event` object to each.\r\n\r\nSubscribers may modify the event object.\r\n\r\n### unsubscribe\r\n\r\n`channel.unsubscribe(name: string, callback: (event: any) => void): void`\r\n\r\nRemove a previously registered callback from the named event. This uses function equality so it must be a reference to the same function, not an equivalent function.\r\n\r\n### addContextPreservation\r\n\r\n`channel.addContextPreservation(preserver: (callback: Function) => Function)`\r\n\r\nPushes the provided context preservation function onto a 'stack' of functions to preserve context.\r\n\r\nThe context preservation function `preserver` is expected to capture the current context and return a function that when invoked restores this preserved context and only then calls the provided callback with the originally provided arguments. Before returning, this second function should also restore the previous context.\r\n\r\nA simple example preserving the Zone.js context:\r\n\r\n```js\r\nchannel.addContextPreservation((callback) => {\r\n    return Zone.current.wrap(callback);\r\n});\r\n```\r\n\r\nA more general, but somewhat contrived, example, where the 'context' is a global object called `context`:\r\n\r\n```js\r\nvar context = { value: 1 };\r\n\r\nchannel.addContextPreservation((callback) => {\r\n    var preservedContext = context;\r\n    return function () {\r\n        var priorContext = context;\r\n        context = preservedContext;\r\n        var result = callback.apply(this, arguments);\r\n        context = priorContext;\r\n        return result;\r\n    }\r\n});\r\n```\r\n\r\n### bindToContext\r\n\r\n`channel.bindToContext(callback: Function)`\r\n\r\nReturns a function which will call the callback after applying each of the registered context preservation functions, and return the result of the callback after unwinding each of the context preservation functions.\r\n\r\nFor example, when using Zone.js:\r\n\r\n```js\r\nchannel.addContextPreservation((callback) => Zone.current.wrap(callback));\r\n\r\nvar z1 = Zone.current.fork({name: 'zone 1'});\r\nvar z2 = Zone.current.fork({name: 'zone 2'});\r\n\r\nvar z1BoundFunc = z1.run(() => channel.bindToContext(() => Zone.current.name));\r\n\r\nvar result = z2.run(() => z1BoundFunc());\r\n```\r\n\r\nBecause the function was bound in `zone 1`, `result` will be `zone 1`.\r\n\r\n### registerMonkeyPatch\r\n\r\n`channel.registerMonkeyPatch(packageName: string, patcher: {versionSpecifier: string, patch: (any, path: string) => any})`;\r\n\r\nIn order to inject publishing and context preservation behavior into third party libraries, we support monkey patching libraries as they are `require`'d.\r\n\r\nCalling this function will register a candidate monkey patcher to be applied when a future `require(packageName)` is called. If the package's version is a semver match for the `versionSpecifier` range, then the original object for that package is passed to the `patch` function, along with the path to the module, and the `patch` function should return a patched version which will end up as the result of the `require`.\r\n\r\nFor a simple example where we patch a `doSomethingAsync` method of the `foo` module to preserve the current context when invoking a callback:\r\n\r\n```js\r\nfunction patchFunction(originalPackage, packagePath) {\r\n    var originalFooAsync = foo.doSomethingAsync;\r\n    foo.doSomethingAsync = function () {\r\n        var callback = arguments[arguments.length-1];\r\n        if (callback && typeof callback == 'function') {\r\n            arguments[arguments.length-1] = channel.bindToContext(callback);\r\n        }\r\n        return originalFooAsync.apply(this, arguments);\r\n    }\r\n    return originalPackage;\r\n}\r\n\r\nvar patcher = {\r\n    versionSpecifier: \">= 1.0.0 < 2.0.0\",\r\n    patch: patchFunction\r\n};\r\n\r\nchannel.registerMonkeyPatch('foo', patcher);\r\n\r\nvar foo = require('foo');\r\n// Now foo.doSomethingAsync will be the patched version, assuming that the version of the foo package found by require() falls within the 1.0.0 - 2.0.0 range.\r\n```\r\n\r\n","_attachments":{},"homepage":"https://github.com/Microsoft/node-diagnostic-channel","bugs":{"url":"https://github.com/Microsoft/node-diagnostic-channel/issues"},"license":"MIT"}