Skip to content

Commit 374f0af

Browse files
committed
polyfill fix for Safari stream async iterator
1 parent ad5e0cd commit 374f0af

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
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.7.0",
3+
"version": "0.8.0",
44
"exports": {
55
".": "./mod.ts",
66
"./node": "./src/node.ts",

package.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.7.0",
3+
"version": "0.8.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/dbushell/xml-streamify.git"

src/parse.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ const ignoreTypes: Partial<Record<NodeType, keyof ParseOptions>> = {
1414
[NodeType.DOCTYPE]: 'ignoreDoctype'
1515
} as const;
1616

17+
/**
18+
* Polyfill for `stream[Symbol.asyncIterator]`
19+
*/
20+
async function* asyncIterableStream(
21+
stream: ReadableStream<[NodeType, string]>,
22+
): AsyncGenerator<[NodeType, string], void, unknown> {
23+
const reader = stream.getReader();
24+
try {
25+
while (true) {
26+
const { done, value } = await reader.read();
27+
if (done) break;
28+
yield value;
29+
}
30+
} finally {
31+
reader.releaseLock();
32+
}
33+
}
34+
1735
/**
1836
* Async generator function for parsing a streamed XML document
1937
* @param input URL to fetch and parse (or a ReadableStream)
@@ -51,6 +69,13 @@ export async function* parse(
5169
signal: options?.signal
5270
});
5371

72+
// Apply polyfill if necessary
73+
if (typeof stream[Symbol.asyncIterator] !== 'function') {
74+
stream[Symbol.asyncIterator] = async function* () {
75+
yield* asyncIterableStream(stream);
76+
};
77+
}
78+
5479
// Set root document as current node
5580
let node = document;
5681

0 commit comments

Comments
 (0)