Skip to content

Commit a3cf385

Browse files
committed
replace internal TextDecoder with TextDecoderStream pipe
1 parent ddc172f commit a3cf385

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dbushell/xml-streamify",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"exports": {
55
".": "./mod.ts",
66
"./node": "./src/node.ts",

src/parse.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ export async function* parse(
3838
throw new Error(`Bad response`);
3939
}
4040

41-
const stream = response.body.pipeThrough(new XMLStream(), {
42-
signal: options?.signal
43-
});
41+
const stream = response.body
42+
.pipeThrough(new TextDecoderStream())
43+
.pipeThrough(new XMLStream(), {
44+
signal: options?.signal
45+
});
4446

4547
// Set root document as current node
4648
let node = document;

src/stream.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,22 @@ const ENTITIES = {
3131
} as const;
3232

3333
/** Transformer object for `TransformStream` constructed by `XMLStream` */
34-
export const transformer: Transformer<Uint8Array, [NodeType, string]> & {
34+
export const transformer: Transformer<string, [NodeType, string]> & {
3535
buf: string;
3636
state: State;
3737
previous: [State, number];
38-
decoder: TextDecoder;
3938
} = {
4039
buf: '',
4140
state: StateType.SKIP,
4241
previous: [StateType.SKIP, -1],
43-
decoder: new TextDecoder(),
4442
flush(controller) {
4543
// Buffer should be empty if document is well-formed
4644
if (this.buf.length > 0) {
4745
controller.enqueue([NodeType.TEXT, this.buf]);
4846
}
4947
},
5048
transform(chunk, controller) {
51-
this.buf += this.decoder.decode(chunk);
49+
this.buf += chunk;
5250
while (this.buf.length) {
5351
// Break if no progress is made (entity may straddle chunk boundary)
5452
if (
@@ -98,7 +96,7 @@ export const transformer: Transformer<Uint8Array, [NodeType, string]> & {
9896
};
9997

10098
/** Transform a binary XML stream into a stream of structured XML data */
101-
export class XMLStream extends TransformStream<Uint8Array, [NodeType, string]> {
99+
export class XMLStream extends TransformStream<string, [NodeType, string]> {
102100
constructor() {
103101
super({...transformer});
104102
}

0 commit comments

Comments
 (0)