@@ -2,7 +2,7 @@ import {isReadableStream} from 'is-stream';
2
2
import { ponyfill } from './web-stream.js' ;
3
3
4
4
export const getAsyncIterable = stream => {
5
- if ( isReadableStream ( stream , { checkOpen : false } ) ) {
5
+ if ( isReadableStream ( stream , { checkOpen : false } ) && nodeImports . on !== undefined ) {
6
6
return getStreamIterable ( stream ) ;
7
7
}
8
8
@@ -22,16 +22,12 @@ const {toString} = Object.prototype;
22
22
23
23
// The default iterable for Node.js streams does not allow for multiple readers at once, so we re-implement it
24
24
const getStreamIterable = async function * ( stream ) {
25
- if ( nodeImports === undefined ) {
26
- await loadNodeImports ( ) ;
27
- }
28
-
29
25
const controller = new AbortController ( ) ;
30
26
const state = { } ;
31
27
handleStreamEnd ( stream , controller , state ) ;
32
28
33
29
try {
34
- for await ( const [ chunk ] of nodeImports . events . on ( stream , 'data' , { signal : controller . signal } ) ) {
30
+ for await ( const [ chunk ] of nodeImports . on ( stream , 'data' , { signal : controller . signal } ) ) {
35
31
yield chunk ;
36
32
}
37
33
} catch ( error ) {
@@ -51,21 +47,14 @@ const getStreamIterable = async function * (stream) {
51
47
52
48
const handleStreamEnd = async ( stream , controller , state ) => {
53
49
try {
54
- await nodeImports . streamPromises . finished ( stream , { cleanup : true , readable : true , writable : false , error : false } ) ;
50
+ await nodeImports . finished ( stream , { cleanup : true , readable : true , writable : false , error : false } ) ;
55
51
} catch ( error ) {
56
52
state . error = error ;
57
53
} finally {
58
54
controller . abort ( ) ;
59
55
}
60
56
} ;
61
57
62
- // Use dynamic imports to support browsers
63
- const loadNodeImports = async ( ) => {
64
- const [ events , streamPromises ] = await Promise . all ( [
65
- import ( 'node:events' ) ,
66
- import ( 'node:stream/promises' ) ,
67
- ] ) ;
68
- nodeImports = { events, streamPromises} ;
69
- } ;
70
-
71
- let nodeImports ;
58
+ // Loaded by the Node entrypoint, but not by the browser one.
59
+ // This prevents using dynamic imports.
60
+ export const nodeImports = { } ;
0 commit comments