diff --git a/CHANGELOG.md b/CHANGELOG.md index 7592507..c6bc561 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [4.0.1] - 2020-09-19 +- Update README +- Add tests for parser errors + ## [4.0.0] - 2020-09-19 - Fixes #6 by introducing the stream interface (`parse`, `stringify` and `resync` are now stream-based functions) - Add `parseSync` and `stringifySync` as synchronous version of `parse` and `stringify` diff --git a/README.md b/README.md index 4eeddb5..9e71609 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ inputStream .pipe(outputStream) ``` -Besides the stream functions, this module also provides synchronous functions like `parseSync` and `stringifySync`. However, you should avoid them and rather use the stream-based functions for better performance: +Besides the stream functions, this module also provides synchronous functions like `parseSync` and `stringifySync`. However, you should avoid them and use the stream-based functions for better performance: ```ts import { parseSync, stringifySync } from 'subtitle' @@ -116,13 +116,13 @@ inputStream .on('finish', () => console.log('parser has finished')) ``` -Check out the [Examples](#examples) section for more examples. +Check out the [Examples](#examples) section for more use cases. ### parseSync - `parseSync(input: string): Node[]` -> **NOTE**: For better perfomance, consider to use the stream-based `parse` function +> **NOTE**: For better perfomance, consider using the stream-based `parse` function It receives a string containing a SRT or VTT content and returns an array of nodes: @@ -172,13 +172,13 @@ inputStream .pipe(stringify({ format: 'WebVTT' })) ``` -Check out the [Examples](#examples) section for more examples. +Check out the [Examples](#examples) section for more use cases. ### stringifySync - `stringify(nodes: Node[], options: { format: 'SRT' | 'vtt }): string` -> **NOTE**: For better perfomance, consider to use the stream-based `stringify` function +> **NOTE**: For better perfomance, consider using the stream-based `stringify` function It receives an array of captions and returns a string in SRT (default), but it also supports VTT format through the options. diff --git a/package.json b/package.json index 96a4976..3ff8109 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "subtitle", - "version": "4.0.0", + "version": "4.0.1", "description": "Stream-based library for parsing and manipulating subtitles", "repository": { "type": "git", diff --git a/src/parse.ts b/src/parse.ts index e892202..e67b5bd 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -13,8 +13,7 @@ export const parse = () => { try { parser.parseLine(chunk.toString()) } catch (err) { - next(err) - return + return next(err) } next() diff --git a/src/utils.ts b/src/utils.ts index 74e02fb..b70d7e7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -6,6 +6,7 @@ export const padLeft = (value: number, length = 2): string => export const createDuplex = (options: DuplexOptions) => new Duplex({ objectMode: true, + autoDestroy: false, read() {}, ...options }) diff --git a/test/parse.test.ts b/test/parse.test.ts index f45ced7..27d4a53 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -1,16 +1,35 @@ -import { fixtures, getFixtureStream, getFixture, pipeline } from '../test-utils' +import * as utils from '../test-utils' import { parse } from '../src' -test.each(fixtures)('parse SRT fixture: %s', async fixture => { - const buffer = await pipeline(getFixtureStream(fixture, 'srt').pipe(parse())) - const expected = JSON.parse(await getFixture(fixture, 'srt.json')) +test.each(utils.fixtures)('parse SRT fixture: %s', async fixture => { + const buffer = await utils.pipeline( + utils.getFixtureStream(fixture, 'srt').pipe(parse()) + ) + const expected = JSON.parse(await utils.getFixture(fixture, 'srt.json')) expect(buffer).toEqual(expected) }) -test.each(fixtures)('parse VTT fixture: %s', async fixture => { - const buffer = await pipeline(getFixtureStream(fixture, 'vtt').pipe(parse())) - const expected = JSON.parse(await getFixture(fixture, 'vtt.json')) +test.each(utils.fixtures)('parse VTT fixture: %s', async fixture => { + const buffer = await utils.pipeline( + utils.getFixtureStream(fixture, 'vtt').pipe(parse()) + ) + const expected = JSON.parse(await utils.getFixture(fixture, 'vtt.json')) expect(buffer).toEqual(expected) }) + +test('error handling', done => { + const stream = utils.createStreamFromString(` +1 +Foo Bar +{{ THIS IS A INVALID TIMESTAMP }} +`) + + stream.pipe(parse()).on('error', err => { + expect(err).toEqual( + new Error(`expected timestamp at row 2, but received: "Foo Bar"`) + ) + done() + }) +})