├── .eslintignore ├── .eslintrc.yml ├── .gitignore ├── .prettierignore ├── .prettierrc.yml ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── index.js ├── package-lock.json ├── package.json └── tests └── index.js /.eslintignore: -------------------------------------------------------------------------------- 1 | /coverage 2 | /node_modules 3 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | env: 2 | node: true 3 | 4 | parserOptions: 5 | ecmaVersion: 9 6 | # Override eslint-config-standard, which incorrectly sets this to "module", 7 | # though that setting is only for ES6 modules, not CommonJS modules. 8 | sourceType: 'script' 9 | 10 | extends: 11 | - 'eslint:recommended' 12 | - standard 13 | - prettier 14 | 15 | rules: 16 | # Override some recommended rules. 17 | no-unused-vars: ['error', { 'args': 'none' }] 18 | no-empty: ['error', { 'allowEmptyCatch': true }] 19 | 20 | # Nock additions. 21 | strict: ['error', 'safe'] 22 | no-loop-func: 'error' 23 | no-var: 'error' 24 | prefer-const: 'error' 25 | object-shorthand: ['error', 'properties'] 26 | prefer-template: 'error' 27 | arrow-body-style: ['error', 'as-needed'] 28 | prefer-destructuring: 29 | [ 30 | 'error', 31 | { 32 | 'VariableDeclarator': { 'array': false, 'object': true }, 33 | 'AssignmentExpression': { 'array': false, 'object': false }, 34 | }, 35 | ] 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /coverage 3 | /.nyc_output 4 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | package.json 2 | package-lock.json 3 | /.nyc_output 4 | /coverage 5 | -------------------------------------------------------------------------------- /.prettierrc.yml: -------------------------------------------------------------------------------- 1 | semi: false 2 | singleQuote: true 3 | trailingComma: es5 4 | bracketSpacing: true 5 | endOfLine: lf 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | cache: npm 3 | 4 | # Trigger a push build on master and greenkeeper branches + PRs build on every branches 5 | # Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147) 6 | branches: 7 | only: 8 | - master 9 | - /^greenkeeper.*$/ 10 | - beta # semantic-release preview releases 11 | - next # semantic-release @next releases 12 | - /^\d+\.x$/ # semantic-release maintenance releases 13 | 14 | stages: 15 | - test 16 | - name: release 17 | if: branch =~ /^(\d+\.x|master|next|beta)$/ AND type IN (push) 18 | 19 | jobs: 20 | include: 21 | - stage: test 22 | node_js: 10 23 | - node_js: 8 24 | - stage: release 25 | node_js: lts/* 26 | env: semantic-release 27 | script: npx semantic-release 28 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | - Using welcoming and inclusive language 12 | - Being respectful of differing viewpoints and experiences 13 | - Gracefully accepting constructive criticism 14 | - Focusing on what is best for the community 15 | - Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | - The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | - Trolling, insulting/derogatory comments, and personal or political attacks 21 | - Public or private harassment 22 | - Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | - Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at nock+coc@martynus.net or coc+nock@maintainer.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | This is a pretty small repository, but an important tool for the Nock ecosystem. If you notice a bug, open an issue! If you think something could be improved, open a PR! 4 | 5 | Please make sure your additions pass the linting and tests scripts in the `package.json`. 6 | 7 | Also, please abide by the [Code of Conduct](CODE_OF_CONDUCT.md). Thanks. 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2015-2019 Pedro Teixeira and other contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # propagate 2 | 3 | [![npm](https://img.shields.io/npm/v/propagate.svg?style=flat-square)][npmjs] 4 | [![Build Status](https://img.shields.io/travis/nock/propagate/master.svg?style=flat-square)][build] 5 | [![Coverage](https://img.shields.io/coveralls/github/nock/propagate.svg?style=flat-square)][coverage] 6 | 7 | [npmjs]: https://www.npmjs.com/package/propagate 8 | [build]: https://travis-ci.org/nock/propagate 9 | [coverage]: https://coveralls.io/github/nock/propagate 10 | 11 | Propagate events from one event emitter into another. 12 | 13 | ## Install 14 | 15 | ```bash 16 | $ npm install propagate 17 | ``` 18 | 19 | ## Propagate 20 | 21 | ```javascript 22 | const ee1 = new EventEmitter() 23 | const ee2 = new EventEmitter() 24 | propagate(ee1, ee2) 25 | 26 | ee2.on('event', function(a, b) { 27 | console.log('got propagated event', a, b) 28 | }) 29 | 30 | ee1.emit('event', 'a', 'b') 31 | ``` 32 | 33 | ## Unpropagate 34 | 35 | You can unpropagate by ending the propagation like this: 36 | 37 | ```javascript 38 | const ee1 = new EventEmitter() 39 | const ee2 = new EventEmitter() 40 | const p = propagate(ee1, ee2) 41 | 42 | // ... 43 | 44 | p.end() 45 | ``` 46 | 47 | ## Only propagate certain events: 48 | 49 | ```javascript 50 | const ee1 = new EventEmitter() 51 | const ee2 = new EventEmitter() 52 | const p = propagate(['event1', 'event2'], ee1, ee2) 53 | ``` 54 | 55 | ## Propagate certain events as other events: 56 | 57 | ```javascript 58 | const ee1 = new EventEmitter() 59 | const ee2 = new EventEmitter() 60 | const p = propagate( 61 | { 62 | event1: 'other-event1', 63 | event2: 'other-event2', 64 | }, 65 | ee1, 66 | ee2 67 | ) 68 | ``` 69 | 70 | # License 71 | 72 | MIT 73 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function propagateKeyedEvents(keyedEventNames, source, dest) { 4 | const keyedListeners = {} 5 | Object.entries(keyedEventNames).forEach(([srcEventName, dstEventName]) => { 6 | keyedListeners[srcEventName] = (...args) => { 7 | dest.emit(dstEventName, ...args) 8 | } 9 | }) 10 | 11 | Object.entries(keyedListeners).forEach(([srcEventName, listener]) => { 12 | source.on(srcEventName, listener) 13 | }) 14 | 15 | return { 16 | end() { 17 | Object.entries(keyedListeners).forEach(([srcEventName, listener]) => { 18 | source.removeListener(srcEventName, listener) 19 | }) 20 | }, 21 | } 22 | } 23 | 24 | function propagateAllEvents(source, dest) { 25 | const oldEmit = source.emit 26 | 27 | // Returns true if the event had listeners, false otherwise. 28 | // https://nodejs.org/api/events.html#events_emitter_emit_eventname_args 29 | source.emit = (eventName, ...args) => { 30 | const oldEmitHadListeners = oldEmit.call(source, eventName, ...args) 31 | const destEmitHadListeners = dest.emit(eventName, ...args) 32 | return oldEmitHadListeners || destEmitHadListeners 33 | } 34 | 35 | return { 36 | end() { 37 | source.emit = oldEmit 38 | }, 39 | } 40 | } 41 | 42 | module.exports = function propagate(events, source, dest) { 43 | if (arguments.length < 3) { 44 | dest = source 45 | source = events 46 | events = undefined 47 | } 48 | 49 | if (events === undefined) { 50 | return propagateAllEvents(source, dest) 51 | } 52 | 53 | let keyedEventNames 54 | if (Array.isArray(events)) { 55 | keyedEventNames = {} 56 | events.forEach(eventName => { 57 | keyedEventNames[eventName] = eventName 58 | }) 59 | } else if (typeof events === 'object') { 60 | keyedEventNames = events 61 | } else { 62 | const eventName = events 63 | keyedEventNames = { [eventName]: eventName } 64 | } 65 | 66 | return propagateKeyedEvents(keyedEventNames, source, dest) 67 | } 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "propagate", 3 | "description": "Propagate events from one event emitter into another", 4 | "keywords": [ 5 | "event", 6 | "events", 7 | "emitter", 8 | "eventemitter", 9 | "propagation" 10 | ], 11 | "version": "0.0.0-development", 12 | "author": "Pedro Teixeira ", 13 | "contributors": [ 14 | { 15 | "name": "Pedro Teixeira" 16 | } 17 | ], 18 | "repository": "nock/propagate", 19 | "homepage": "http://github.com/nock/propagate#readme", 20 | "bugs": "http://github.com/nock/propagate/issues", 21 | "engines": { 22 | "node": ">= 8" 23 | }, 24 | "dependencies": {}, 25 | "devDependencies": { 26 | "eslint": "^5.16.0", 27 | "eslint-config-prettier": "^4.1.0", 28 | "eslint-config-standard": "^12.0.0", 29 | "eslint-plugin-import": "^2.16.0", 30 | "eslint-plugin-node": "^8.0.1", 31 | "eslint-plugin-promise": "^4.1.1", 32 | "eslint-plugin-standard": "^4.0.0", 33 | "prettier": "^1.16.4", 34 | "semantic-release": "16.0.0-beta.22", 35 | "tap": "^12.6.1" 36 | }, 37 | "scripts": { 38 | "lint": "eslint \"**/*.js\"", 39 | "unit": "tap --100 tests/index.js", 40 | "coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html", 41 | "prettier": "prettier --write \"**/*.@(js|md|yml|json)\"", 42 | "prettier:check": "prettier --check \"**/*.@(js|md|yml|json)\"", 43 | "pretest": "npm run lint", 44 | "test": "npm run -s unit", 45 | "posttest": "npm run -s prettier:check" 46 | }, 47 | "nyc": { 48 | "exclude": [ 49 | "tests/" 50 | ] 51 | }, 52 | "license": "MIT" 53 | } 54 | -------------------------------------------------------------------------------- /tests/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const { test } = require('tap') 4 | const { EventEmitter } = require('events') 5 | const propagate = require('..') 6 | 7 | test('propagates events', t => { 8 | t.plan(16) 9 | const ee1 = new EventEmitter() 10 | const ee2 = new EventEmitter() 11 | propagate(ee1, ee2) 12 | 13 | ee2.on('event-1', (a, b, c) => { 14 | t.equal(a, 'a') 15 | t.equal(b, 'b') 16 | t.equal(c, undefined) 17 | }) 18 | 19 | ee2.on('event-2', (a, b, c) => { 20 | t.equal(a, 'c') 21 | t.equal(b, 'd') 22 | t.equal(c, undefined) 23 | }) 24 | 25 | t.true(ee1.emit('event-1', 'a', 'b')) 26 | t.true(ee1.emit('event-1', 'a', 'b')) 27 | t.true(ee1.emit('event-2', 'c', 'd')) 28 | t.true(ee1.emit('event-2', 'c', 'd')) 29 | }) 30 | 31 | test('propagates can end', t => { 32 | t.plan(3) 33 | 34 | const ee1 = new EventEmitter() 35 | const ee2 = new EventEmitter() 36 | const prop = propagate(ee1, ee2) 37 | 38 | ee2.on('event', () => { 39 | t.ok('true', 'propagated') 40 | }) 41 | 42 | t.true(ee1.emit('event')) 43 | prop.end() 44 | t.false(ee1.emit('event')) 45 | }) 46 | 47 | test('after propagation old one still emits', t => { 48 | t.plan(4) 49 | 50 | const ee1 = new EventEmitter() 51 | const ee2 = new EventEmitter() 52 | const prop = propagate(ee1, ee2) 53 | 54 | ee1.on('event', () => { 55 | t.ok('true', 'propagated') 56 | }) 57 | 58 | t.true(ee1.emit('event')) 59 | prop.end() 60 | t.true(ee1.emit('event')) 61 | }) 62 | 63 | test('emit on source before destination', t => { 64 | t.plan(2) 65 | 66 | const source = new EventEmitter() 67 | const dest = new EventEmitter() 68 | 69 | // Set up test case for "propagate all" 70 | // `count` should have been incremented by handler on source when handler on dest is invoked 71 | let count = 0 72 | propagate(source, dest) 73 | source.on('event', () => { 74 | count++ 75 | }) 76 | dest.on('event', () => { 77 | t.equal(count, 1, 'emit on source first') 78 | }) 79 | 80 | // Emit the events for assertion 81 | t.true(source.emit('event')) 82 | }) 83 | 84 | test('is able to propagate only certain events', t => { 85 | t.plan(6) 86 | const ee1 = new EventEmitter() 87 | const ee2 = new EventEmitter() 88 | // propagate only event-1 and event-2, leaving out 89 | const p = propagate(['event-1', 'event-2'], ee1, ee2) 90 | 91 | ee2.on('event-1', () => { 92 | t.ok(true, 'event 1 received') 93 | }) 94 | 95 | ee2.on('event-2', (a, b, c) => { 96 | t.ok(true, 'event 2 received') 97 | }) 98 | 99 | ee2.on('event-3', (a, b, c) => { 100 | t.fail('event 3 should not have been received') 101 | }) 102 | 103 | t.true(ee1.emit('event-1')) 104 | t.true(ee1.emit('event-2')) 105 | t.false(ee1.emit('event-3')) 106 | 107 | p.end() 108 | 109 | t.false(ee1.emit('event-1')) 110 | }) 111 | 112 | test('is able to propagate and map certain events', t => { 113 | t.plan(6) 114 | const ee1 = new EventEmitter() 115 | const ee2 = new EventEmitter() 116 | // propagate only event-1 and event-2, leaving out 117 | const p = propagate( 118 | { 119 | 'event-1': 'other-event-1', 120 | 'event-2': 'other-event-2', 121 | }, 122 | ee1, 123 | ee2 124 | ) 125 | 126 | ee2.on('other-event-1', () => { 127 | t.ok(true, 'event 1 received') 128 | }) 129 | 130 | ee2.on('other-event-2', (a, b, c) => { 131 | t.ok(true, 'event 2 received') 132 | }) 133 | 134 | ee2.on('event-3', (a, b, c) => { 135 | t.fail('event 3 should not have been received') 136 | }) 137 | 138 | t.true(ee1.emit('event-1')) 139 | t.true(ee1.emit('event-2')) 140 | t.false(ee1.emit('event-3')) 141 | 142 | p.end() 143 | 144 | t.false(ee1.emit('event-1')) 145 | }) 146 | 147 | test('is able to propagate a single event', t => { 148 | t.plan(5) 149 | const ee1 = new EventEmitter() 150 | const ee2 = new EventEmitter() 151 | const p = propagate('event-1', ee1, ee2) 152 | 153 | ee2.on('event-1', () => { 154 | t.ok(true, 'event 1 received') 155 | }) 156 | 157 | ee2.on('event-2', (a, b, c) => { 158 | t.fail('event 3 should not have been received') 159 | }) 160 | 161 | t.true(ee1.emit('event-1')) 162 | t.false(ee1.emit('event-2')) 163 | 164 | p.end() 165 | 166 | t.false(ee1.emit('event-1')) 167 | t.false(ee1.emit('event-2')) 168 | }) 169 | --------------------------------------------------------------------------------