-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(csv-parse): implement TransformStream (#445)
* feat(csv-parse): implement TransformStream * feat(csv-parse): add test api.web_stream and fix controller.terminate * feat(csv-parse): added errors handle to TransformStream * test(csv-parse): error thrown by webstream * build(csv-parse): exclude web_stream ts test from ci in node 14 * fix(csv-parse): satisfy test and re-habilitate node stream import --------- Co-authored-by: David Worms <david@adaltas.com>
- Loading branch information
Showing
5 changed files
with
110 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import { Options } from './index.js'; | ||
|
||
declare function parse(options?: Options): TransformStream; | ||
// export default parse; | ||
export { parse }; | ||
|
||
export { | ||
CastingContext, CastingFunction, CastingDateFunction, | ||
ColumnOption, Options, Info, CsvErrorCode, CsvError | ||
} from './index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
import { Options } from './index.js'; | ||
|
||
declare function parse(options?: Options): TransformStream; | ||
// export default parse; | ||
export { parse }; | ||
|
||
export { | ||
CastingContext, CastingFunction, CastingDateFunction, | ||
ColumnOption, Options, Info, CsvErrorCode, CsvError | ||
} from './index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
import { TransformStream } from "node:stream/web"; | ||
import { TransformStream, CountQueuingStrategy } from "node:stream/web"; | ||
import { transform } from "./api/index.js"; | ||
|
||
const parse = (opts) => { | ||
const api = transform(opts); | ||
return new TransformStream({ | ||
async transform(chunk, controller) { | ||
api.parse( | ||
chunk, | ||
false, | ||
(record) => { | ||
controller.enqueue(record); | ||
}, | ||
() => { | ||
controller.close(); | ||
}, | ||
); | ||
}, | ||
async flush(controller) { | ||
api.parse( | ||
undefined, | ||
true, | ||
(record) => { | ||
controller.enqueue(record); | ||
}, | ||
() => { | ||
controller.close(); | ||
}, | ||
); | ||
let controller; | ||
const enqueue = (record) => { | ||
controller.enqueue(record); | ||
}; | ||
const terminate = () => { | ||
controller.terminate(); | ||
}; | ||
return new TransformStream( | ||
{ | ||
start(ctr) { | ||
controller = ctr; | ||
}, | ||
transform(chunk) { | ||
const error = api.parse(chunk, false, enqueue, terminate); | ||
|
||
if (error) { | ||
controller.error(error); | ||
throw error; | ||
} | ||
}, | ||
flush() { | ||
const error = api.parse(undefined, true, enqueue, terminate); | ||
|
||
if (error) { | ||
controller.error(error); | ||
throw error; | ||
} | ||
}, | ||
}, | ||
}); | ||
new CountQueuingStrategy({ highWaterMark: 1024 }), | ||
new CountQueuingStrategy({ highWaterMark: 1024 }), | ||
); | ||
}; | ||
|
||
export { parse }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'should' | ||
import {parse as parseStream} from '../lib/stream.js' | ||
import { CsvError } from '../lib/index.js' | ||
|
||
describe('API Web Stream', () => { | ||
|
||
describe('stream/web/TransformStream', () => { | ||
|
||
it('simple parse', async () => { | ||
const stream = parseStream(); | ||
const writer = stream.writable.getWriter(); | ||
const reader = stream.readable.getReader(); | ||
await writer.write(Buffer.from("A,B,C\nD,E,F")); | ||
await writer.close(); | ||
await reader.read().should.finally.eql({ | ||
done: false, | ||
value: ['A', 'B', 'C'], | ||
}); | ||
await reader.read().should.finally.eql({ | ||
done: false, | ||
value: ['D', 'E', 'F'], | ||
}); | ||
await reader.read().should.finally.eql({ | ||
done: true, | ||
value: undefined, | ||
}); | ||
}) | ||
|
||
it("cat error parse", async function () { | ||
const stream = parseStream(); | ||
const writer = stream.writable.getWriter(); | ||
try { | ||
await writer.write(Buffer.from("A,B,C\nD,E")); | ||
await writer.close(); | ||
throw Error("Shall not be called"); | ||
} catch (err) { | ||
if (!(err instanceof CsvError)) { | ||
throw Error("Invalid error type"); | ||
} | ||
err.code.should.eql("CSV_RECORD_INCONSISTENT_FIELDS_LENGTH"); | ||
} | ||
}); | ||
|
||
}) | ||
}) |