@@ -16,29 +16,36 @@ const ignoreTypes: Partial<Record<NodeType, keyof ParseOptions>> = {
16
16
17
17
/**
18
18
* Async generator function for parsing a streamed XML document
19
- * @param url URL to fetch and parse
19
+ * @param input URL to fetch and parse (or a ReadableStream)
20
20
* @param options Parsing options {@link ParseOptions}
21
21
* @returns Yields parsed XML nodes {@link Node}
22
22
*/
23
23
export async function * parse (
24
- url : string | URL ,
24
+ input : string | URL | ReadableStream ,
25
25
options ?: ParseOptions
26
26
) : AsyncGenerator < Node , Node | void , void > {
27
- url = new URL ( url ) ;
28
-
29
27
const document = new Node ( '@document' ) ;
30
-
31
28
try {
32
29
const init = { ...options ?. fetchOptions } ;
33
30
if ( options ?. signal ) {
34
31
init . signal = options . signal ;
35
32
}
36
- const response = await fetch ( url , init ) ;
37
- if ( ! response . ok || ! response . body ) {
38
- throw new Error ( `Bad response` ) ;
33
+
34
+ let source : ReadableStream ;
35
+
36
+ // Fetch stream if URL is provided as input
37
+ if ( typeof input === 'string' || input instanceof URL ) {
38
+ input = new URL ( input ) ;
39
+ const response = await fetch ( input , init ) ;
40
+ if ( ! response . ok || ! response . body ) {
41
+ throw new Error ( `Bad response` ) ;
42
+ }
43
+ source = response . body ;
44
+ } else {
45
+ source = input ;
39
46
}
40
47
41
- const stream = response . body
48
+ const stream = source
42
49
. pipeThrough ( new TextDecoderStream ( ) )
43
50
. pipeThrough ( new XMLStream ( ) , {
44
51
signal : options ?. signal
0 commit comments