package/package.json000644 0000001365 13254563245011603 0ustar00000000 000000 { "name": "peek-stream", "version": "1.1.3", "description": "Transform stream that lets you peek the first line before deciding how to parse it", "main": "index.js", "dependencies": { "buffer-from": "^1.0.0", "duplexify": "^3.5.0", "through2": "^2.0.3" }, "devDependencies": { "concat-stream": "^1.6.0", "tape": "^4.6.3" }, "scripts": { "test": "tape test.js" }, "repository": { "type": "git", "url": "git://github.com/mafintosh/peek-stream" }, "keywords": [ "peek", "stream", "parse", "swap" ], "author": "Mathias Buus", "license": "MIT", "bugs": { "url": "https://github.com/mafintosh/peek-stream/issues" }, "homepage": "https://github.com/mafintosh/peek-stream" } package/.travis.yml000644 0000000046 13254563227011421 0ustar00000000 000000 language: node_js node_js: - "0.10" package/example.js000644 0000001552 13254563227011304 0ustar00000000 000000 var peek = require('peek-stream') var ldjson = require('ldjson-stream') var csv = require('csv-parser') var isCSV = function(data) { return data.toString().indexOf(',') > -1 } var isJSON = function(data) { try { JSON.parse(data) return true } catch (err) { return false } } var parser = function() { return peek(function(data, swap) { // maybe it is JSON? if (isJSON(data)) return swap(null, ldjson()) // maybe it is CSV? if (isCSV(data)) return swap(null, csv()) // we do not know - bail swap(new Error('No parser available')) }) } var parse = parser() parse.write('{"hello":"world"}\n{"hello":"another"}\n') parse.on('data', function(data) { console.log('from ldj:', data) }) var parse = parser() parse.write('test,header\nvalue-1,value-2\n') parse.on('data', function(data) { console.log('from csv:', data) }) package/index.js000644 0000003664 13254563227010766 0ustar00000000 000000 var duplexify = require('duplexify') var through = require('through2') var bufferFrom = require('buffer-from') var noop = function() {} var isObject = function(data) { return !Buffer.isBuffer(data) && typeof data !== 'string' } var peek = function(opts, onpeek) { if (typeof opts === 'number') opts = {maxBuffer:opts} if (typeof opts === 'function') return peek(null, opts) if (!opts) opts = {} var maxBuffer = typeof opts.maxBuffer === 'number' ? opts.maxBuffer : 65535 var strict = opts.strict var newline = opts.newline !== false var buffer = [] var bufferSize = 0 var dup = duplexify.obj() var peeker = through.obj({highWaterMark:1}, function(data, enc, cb) { if (isObject(data)) return ready(data, null, cb) if (!Buffer.isBuffer(data)) data = bufferFrom(data) if (newline) { var nl = Array.prototype.indexOf.call(data, 10) if (nl > 0 && data[nl-1] === 13) nl-- if (nl > -1) { buffer.push(data.slice(0, nl)) return ready(Buffer.concat(buffer), data.slice(nl), cb) } } buffer.push(data) bufferSize += data.length if (bufferSize < maxBuffer) return cb() if (strict) return cb(new Error('No newline found')) ready(Buffer.concat(buffer), null, cb) }) var onpreend = function() { if (strict) return dup.destroy(new Error('No newline found')) dup.cork() ready(Buffer.concat(buffer), null, function(err) { if (err) return dup.destroy(err) dup.uncork() }) } var ready = function(data, overflow, cb) { dup.removeListener('preend', onpreend) onpeek(data, function(err, parser) { if (err) return cb(err) dup.setWritable(parser) dup.setReadable(parser) if (data) parser.write(data) if (overflow) parser.write(overflow) overflow = buffer = peeker = null // free the data cb() }) } dup.on('preend', onpreend) dup.setWritable(peeker) return dup } module.exports = peek package/LICENSE000644 0000002066 13254563227010321 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2014 Mathias Buus 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.package/README.md000644 0000003375 13254563227010577 0ustar00000000 000000 # peek-stream Transform stream that lets you peek the first line before deciding how to parse it ``` npm install peek-stream ``` [![build status](http://img.shields.io/travis/mafintosh/peek-stream.svg?style=flat)](http://travis-ci.org/mafintosh/peek-stream) ![dat](http://img.shields.io/badge/Development%20sponsored%20by-dat-green.svg?style=flat) ## Usage ``` js var peek = require('peek-stream') var ldjson = require('ldjson-stream') var csv = require('csv-parser') var isCSV = function(data) { return data.toString().indexOf(',') > -1 } var isJSON = function(data) { try { JSON.parse(data) return true } catch (err) { return false } } // call parser to create a new parser var parser = function() { return peek(function(data, swap) { // maybe it is JSON? if (isJSON(data)) return swap(null, ldjson()) // maybe it is CSV? if (isCSV(data)) return swap(null, csv()) // we do not know - bail swap(new Error('No parser available')) }) } ``` The above parser will be able to parse both line delimited JSON and CSV ``` js var parse = parser() parse.write('{"hello":"world"}\n{"hello":"another"}\n') parse.on('data', function(data) { console.log(data) // prints {hello:'world'} and {hello:'another'} }) ``` Or ``` js var parse = parser() parse.write('test,header\nvalue-1,value-2\n') parse.on('data', function(data) { console.log(data) // prints {test:'value-1', header:'value-2'} }) ``` Per default `data` is the first line (or the first `65535` bytes if no newline is found). To change the max buffer pass an options map to the constructor ``` js var parse = peek({ maxBuffer: 10000 }, function(data, swap) { ... }) ``` If you want to emit an error if no newline is found set `strict: true` as well. ## License MITpackage/test.js000644 0000003160 13254563227010625 0ustar00000000 000000 var peek = require('./') var tape = require('tape') var concat = require('concat-stream') var through = require('through2') var uppercase = function(data, enc, cb) { cb(null, data.toString().toUpperCase()) } tape('swap to uppercase', function(t) { var p = peek(function(data, swap) { swap(null, through(uppercase)) }) p.pipe(concat(function(data) { t.same(data.toString(), 'HELLO\nWORLD\n') t.end() })) p.write('hello\n') p.write('world\n') p.end() }) tape('swap to uppercase no newline', function(t) { var p = peek(function(data, swap) { swap(null, through(uppercase)) }) p.pipe(concat(function(data) { t.same(data.toString(), 'HELLOWORLD') t.end() })) p.write('hello') p.write('world') p.end() }) tape('swap to uppercase async', function(t) { var p = peek(function(data, swap) { setTimeout(function() { swap(null, through(uppercase)) }, 100) }) p.pipe(concat(function(data) { t.same(data.toString(), 'HELLO\nWORLD\n') t.end() })) p.write('hello\n') p.write('world\n') p.end() }) tape('swap to error', function(t) { var p = peek(function(data, swap) { swap(new Error('nogo')) }) p.on('error', function(err) { t.ok(err) t.same(err.message, 'nogo') t.end() }) p.write('hello\n') p.write('world\n') p.end() }) tape('swap to error async', function(t) { var p = peek(function(data, swap) { setTimeout(function() { swap(new Error('nogo')) }, 100) }) p.on('error', function(err) { t.ok(err) t.same(err.message, 'nogo') t.end() }) p.write('hello\n') p.write('world\n') p.end() })