stream-match
Advanced tools
+14
-16
@@ -1,19 +0,17 @@ | ||
| const streamMatch = (stream, pattern) => | ||
| new Promise(resolve => { | ||
| const match = | ||
| typeof pattern === 'string' | ||
| ? buf => buf.includes(pattern) | ||
| : buf => pattern.exec(buf) | ||
| let buf = '' | ||
| const onData = data => { | ||
| buf += data | ||
| const res = match(buf) | ||
| if (res) { | ||
| stream.removeListener('data', onData) | ||
| resolve(res) | ||
| } | ||
| const streamMatch = async (stream, pattern) => { | ||
| const match = | ||
| typeof pattern === 'string' | ||
| ? buf => buf.includes(pattern) | ||
| : buf => pattern.exec(buf) | ||
| let buf = '' | ||
| for await (const data of stream) { | ||
| buf += data | ||
| const res = match(buf) | ||
| if (res) { | ||
| return res | ||
| } | ||
| stream.on('data', onData) | ||
| }) | ||
| } | ||
| throw new Error('stream ended before pattern matched') | ||
| } | ||
| export default streamMatch |
+1
-1
| { | ||
| "name": "stream-match", | ||
| "version": "4.0.0", | ||
| "version": "4.0.1", | ||
| "license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Match a string in a stream. Zero dependencies", |
+14
-1
@@ -55,3 +55,3 @@ import test from 'test' | ||
| test('string with backslashes', async t => { | ||
| test('string with backslashes', async () => { | ||
| const stream = new PassThrough() | ||
@@ -78,1 +78,14 @@ let matched = false | ||
| }) | ||
| test('end without match', async () => { | ||
| const stream = new PassThrough() | ||
| stream.end() | ||
| await assert.rejects( | ||
| match(stream, 'beep'), | ||
| { | ||
| name: 'Error', | ||
| message: 'stream ended before pattern matched' | ||
| } | ||
| ) | ||
| }) |
3468
5.47%94
10.59%