├── .npmrc ├── test ├── bundle │ ├── bar.js │ ├── main.js │ ├── xyz.js │ └── foo.js └── bundle.js ├── example ├── browser │ ├── bar.js │ ├── main.js │ ├── xyz.js │ └── foo.js └── bundle.js ├── .travis.yml ├── package.json ├── LICENSE ├── index.js └── readme.markdown /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /test/bundle/bar.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n) { 2 | return n * 100; 3 | }; 4 | -------------------------------------------------------------------------------- /test/bundle/main.js: -------------------------------------------------------------------------------- 1 | var foo = require('./foo'); 2 | console.log('main: ' + foo(5)); 3 | -------------------------------------------------------------------------------- /test/bundle/xyz.js: -------------------------------------------------------------------------------- 1 | var foo = require('./foo'); 2 | console.log('xyz: ' + foo(6)); 3 | -------------------------------------------------------------------------------- /example/browser/bar.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n) { 2 | return n * 100; 3 | }; 4 | -------------------------------------------------------------------------------- /example/browser/main.js: -------------------------------------------------------------------------------- 1 | var foo = require('./foo'); 2 | console.log('main: ' + foo(5)); 3 | -------------------------------------------------------------------------------- /example/browser/xyz.js: -------------------------------------------------------------------------------- 1 | var foo = require('./foo'); 2 | console.log('xyz: ' + foo(6)); 3 | -------------------------------------------------------------------------------- /test/bundle/foo.js: -------------------------------------------------------------------------------- 1 | var bar = require('./bar'); 2 | 3 | module.exports = function (n) { 4 | return n * 111 + bar(n); 5 | }; 6 | -------------------------------------------------------------------------------- /example/browser/foo.js: -------------------------------------------------------------------------------- 1 | var bar = require('./bar'); 2 | 3 | module.exports = function (n) { 4 | return n * 111 + bar(n); 5 | }; 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "12" 4 | - "11" 5 | - "10" 6 | - "8" 7 | - "6" 8 | - "4" 9 | - "iojs" 10 | - "0.12" 11 | - "0.10" 12 | - "0.8" 13 | before_install: 14 | # Old npm certs are untrusted https://github.com/npm/npm/issues/20191 15 | - 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ] || [ "${TRAVIS_NODE_VERSION}" = "0.8" ]; then export NPM_CONFIG_STRICT_SSL=false; fi' 16 | - 'nvm install-latest-npm' 17 | -------------------------------------------------------------------------------- /example/bundle.js: -------------------------------------------------------------------------------- 1 | var splicer = require('../'); 2 | var through = require('through2'); 3 | var deps = require('module-deps'); 4 | var pack = require('browser-pack'); 5 | 6 | var pipeline = splicer.obj([ 7 | 'deps', [ deps() ], 8 | 'pack', [ pack({ raw: true }) ] 9 | ]); 10 | 11 | pipeline.get('deps').push(through.obj(function (row, enc, next) { 12 | row.source = row.source.toUpperCase(); 13 | this.push(row); 14 | next(); 15 | })); 16 | 17 | pipeline.pipe(process.stdout); 18 | 19 | pipeline.end(__dirname + '/browser/main.js'); 20 | -------------------------------------------------------------------------------- /test/bundle.js: -------------------------------------------------------------------------------- 1 | var test = require('tape'); 2 | var splicer = require('../'); 3 | var through = require('through2'); 4 | var deps = require('module-deps'); 5 | var pack = require('browser-pack'); 6 | var concat = require('concat-stream'); 7 | 8 | test('bundle', function (t) { 9 | t.plan(1); 10 | 11 | var pipeline = splicer.obj([ 12 | 'deps', [ deps() ], 13 | 'pack', [ pack({ raw: true }) ] 14 | ]); 15 | pipeline.pipe(concat(function (body) { 16 | Function([ 'console' ], body.toString('utf8'))({ log: log }); 17 | function log (msg) { 18 | t.equal(msg, 'main: 56055'); 19 | } 20 | })); 21 | 22 | pipeline.get('deps').push(through.obj(function (row, enc, next) { 23 | row.source = row.source.replace(/111/g, '11111'); 24 | this.push(row); 25 | next(); 26 | })); 27 | 28 | pipeline.end(__dirname + '/bundle/main.js'); 29 | }); 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "labeled-stream-splicer", 3 | "version": "2.0.2", 4 | "description": "stream splicer with labels", 5 | "main": "index.js", 6 | "dependencies": { 7 | "inherits": "^2.0.1", 8 | "stream-splicer": "^2.0.0" 9 | }, 10 | "devDependencies": { 11 | "browser-pack": "^6.1.0", 12 | "concat-stream": "^1.4.6", 13 | "module-deps": "^6.2.0", 14 | "tape": "^4.10.1", 15 | "through2": "^1.0.0" 16 | }, 17 | "scripts": { 18 | "test": "tape test/*.js" 19 | }, 20 | "repository": { 21 | "type": "git", 22 | "url": "git://github.com/browserify/labeled-stream-splicer.git" 23 | }, 24 | "homepage": "https://github.com/browserify/labeled-stream-splicer", 25 | "keywords": [ 26 | "splice", 27 | "stream", 28 | "labels", 29 | "mutable", 30 | "pipeline" 31 | ], 32 | "author": { 33 | "name": "James Halliday", 34 | "email": "mail@substack.net", 35 | "url": "http://substack.net" 36 | }, 37 | "license": "MIT" 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) James Halliday and browserify 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 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Splicer = require('stream-splicer'); 2 | var inherits = require('inherits'); 3 | 4 | module.exports = Labeled; 5 | inherits(Labeled, Splicer); 6 | 7 | module.exports.obj = function (streams, opts) { 8 | if (!opts) opts = {}; 9 | opts.objectMode = true; 10 | return new Labeled(streams, opts); 11 | }; 12 | 13 | function Labeled (streams, opts) { 14 | if (!(this instanceof Labeled)) return new Labeled(streams, opts); 15 | Splicer.call(this, [], opts); 16 | 17 | var reps = []; 18 | for (var i = 0; i < streams.length; i++) { 19 | var s = streams[i]; 20 | if (typeof s === 'string') continue; 21 | if (Array.isArray(s)) { 22 | s = new Labeled(s, opts); 23 | } 24 | if (i >= 0 && typeof streams[i-1] === 'string') { 25 | s.label = streams[i-1]; 26 | } 27 | reps.push(s); 28 | } 29 | if (typeof streams[i-1] === 'string') { 30 | reps.push(new Labeled([], opts)); 31 | } 32 | this.splice.apply(this, [0,0].concat(reps)); 33 | } 34 | 35 | Labeled.prototype.indexOf = function (stream) { 36 | if (typeof stream === 'string') { 37 | for (var i = 0; i < this._streams.length; i++) { 38 | if (this._streams[i].label === stream) return i; 39 | } 40 | return -1; 41 | } 42 | else { 43 | return Splicer.prototype.indexOf.call(this, stream); 44 | } 45 | }; 46 | 47 | Labeled.prototype.get = function (key) { 48 | if (typeof key === 'string') { 49 | var ix = this.indexOf(key); 50 | if (ix < 0) return undefined; 51 | return this._streams[ix]; 52 | } 53 | else return Splicer.prototype.get.call(this, key); 54 | }; 55 | 56 | Labeled.prototype.splice = function (key) { 57 | var ix; 58 | if (typeof key === 'string') { 59 | ix = this.indexOf(key); 60 | } 61 | else ix = key; 62 | var args = [ ix ].concat([].slice.call(arguments, 1)); 63 | return Splicer.prototype.splice.apply(this, args); 64 | }; 65 | -------------------------------------------------------------------------------- /readme.markdown: -------------------------------------------------------------------------------- 1 | # labeled-stream-splicer 2 | 3 | [stream splicer](https://npmjs.org/package/stream-splicer) with labels 4 | 5 | [![build status](https://secure.travis-ci.org/browserify/labeled-stream-splicer.png?branch=master)](http://travis-ci.org/browserify/labeled-stream-splicer) 6 | 7 | # example 8 | 9 | Here's an example that exposes a label for `deps` and `pack`: 10 | 11 | ``` js 12 | var splicer = require('labeled-stream-splicer'); 13 | var through = require('through2'); 14 | var deps = require('module-deps'); 15 | var pack = require('browser-pack'); 16 | var lstream = require('lstream'); 17 | 18 | var pipeline = splicer.obj([ 19 | 'deps', [ deps() ], 20 | 'pack', [ pack({ raw: true }) ] 21 | ]); 22 | 23 | pipeline.get('deps').unshift(lstream()); 24 | 25 | pipeline.get('deps').push(through.obj(function (row, enc, next) { 26 | row.source = row.source.toUpperCase(); 27 | this.push(row); 28 | next(); 29 | })); 30 | 31 | process.stdin.pipe(pipeline).pipe(process.stdout); 32 | ``` 33 | 34 | Here the `deps` sub-pipeline is augmented with a post-transformation that 35 | uppercases its source input. 36 | 37 | # methods 38 | 39 | ``` js 40 | var splicer = require('labeled-stream-splicer') 41 | ``` 42 | 43 | The API is the same as 44 | [stream-splicer](https://npmjs.org/package/stream-splicer), 45 | except that `pipeline.get()`, `pipeline.splice()`, and `pipeline.indexOf()` can 46 | accept string labels in addition to numeric indexes. 47 | 48 | ## var pipeline = splicer(streams, opts) 49 | 50 | Create a `pipeline` duplex stream given an array of `streams`. Each `stream` 51 | will be piped to the next. Writes to `pipeline` get written to the first stream 52 | and data for reads from `pipeline` come from the last stream. 53 | 54 | To signify a label, a stream may have a `.label` property or a string may be 55 | placed in the `streams` array. 56 | 57 | For example, for streams `[ a, 'foo', b, c, 'bar', d ]`, this pipeline is 58 | constructed internally: 59 | 60 | ``` 61 | a.pipe(b).pipe(c).pipe(d) 62 | ``` 63 | 64 | with a label `'foo`' that points to `b` and a label `'bar'` that points to `d`. 65 | If `a` or `c` has a `.label` property, that label would be used for addressing. 66 | 67 | Input will get written into `a`. Output will be read from `d`. 68 | 69 | If any of the elements in `streams` are arrays, they will be converted into 70 | nested labeled pipelines. This is useful if you want to expose a hookable 71 | pipeline with grouped insertion points. 72 | 73 | ## var pipeline = splicer.obj(streams, opts) 74 | 75 | Create a `pipeline` with `opts.objectMode` set to true for convenience. 76 | 77 | ## var removed = pipeline.splice(index, howMany, stream, ...) 78 | 79 | Splice the pipeline starting at `index`, removing `howMany` streams and 80 | replacing them with each additional `stream` argument provided. 81 | 82 | The streams that were removed from the splice and returned. 83 | 84 | `index` can be an integer index or a label. 85 | 86 | ## pipeline.push(stream, ...) 87 | 88 | Push one or more streams to the end of the pipeline. 89 | 90 | The stream arguments may have a `label` property that will be used for string 91 | lookups. 92 | 93 | ## var stream = pipeline.pop() 94 | 95 | Pop a stream from the end of the pipeline. 96 | 97 | ## pipeline.unshift(stream, ...) 98 | 99 | Unshift one or more streams to the begining of the pipeline. 100 | 101 | The stream arguments may have a `label` property that will be used for string 102 | lookups. 103 | 104 | ## var stream = pipeline.shift() 105 | 106 | Shift a stream from the begining of the pipeline. 107 | 108 | ## var stream = pipeline.get(index) 109 | 110 | Return the stream at index `index`. 111 | 112 | `index` can be an integer or a string label. 113 | 114 | # install 115 | 116 | With [npm](https://npmjs.org) do: 117 | 118 | ``` 119 | npm install labeled-stream-splicer 120 | ``` 121 | 122 | # license 123 | 124 | MIT 125 | --------------------------------------------------------------------------------