@@ -10,36 +10,44 @@ import { Readable, Writable } from 'stream';
1010
1111export async function htmlRewritingStream ( content : string ) : Promise < {
1212 rewriter : import ( 'parse5-html-rewriting-stream' ) ;
13- transformedContent : Promise < string > ;
13+ transformedContent : ( ) => Promise < string > ;
1414} > {
1515 const chunks : Buffer [ ] = [ ] ;
1616 const rewriter = new ( await import ( 'parse5-html-rewriting-stream' ) ) . default ( ) ;
1717
1818 return {
1919 rewriter,
20- transformedContent : new Promise ( ( resolve ) => {
21- new Readable ( {
22- encoding : 'utf8' ,
23- read ( ) : void {
24- this . push ( Buffer . from ( content ) ) ;
25- this . push ( null ) ;
26- } ,
27- } )
28- . pipe ( rewriter )
29- . pipe (
30- new Writable ( {
31- write ( chunk : string | Buffer , encoding : string | undefined , callback : Function ) : void {
32- chunks . push (
33- typeof chunk === 'string' ? Buffer . from ( chunk , encoding as BufferEncoding ) : chunk ,
34- ) ;
35- callback ( ) ;
36- } ,
37- final ( callback : ( error ?: Error ) => void ) : void {
38- callback ( ) ;
39- resolve ( Buffer . concat ( chunks ) . toString ( ) ) ;
40- } ,
41- } ) ,
42- ) ;
43- } ) ,
20+ transformedContent : ( ) => {
21+ return new Promise ( ( resolve ) => {
22+ new Readable ( {
23+ encoding : 'utf8' ,
24+ read ( ) : void {
25+ this . push ( Buffer . from ( content ) ) ;
26+ this . push ( null ) ;
27+ } ,
28+ } )
29+ . pipe ( rewriter )
30+ . pipe (
31+ new Writable ( {
32+ write (
33+ chunk : string | Buffer ,
34+ encoding : string | undefined ,
35+ callback : Function ,
36+ ) : void {
37+ chunks . push (
38+ typeof chunk === 'string'
39+ ? Buffer . from ( chunk , encoding as BufferEncoding )
40+ : chunk ,
41+ ) ;
42+ callback ( ) ;
43+ } ,
44+ final ( callback : ( error ?: Error ) => void ) : void {
45+ callback ( ) ;
46+ resolve ( Buffer . concat ( chunks ) . toString ( ) ) ;
47+ } ,
48+ } ) ,
49+ ) ;
50+ } ) ;
51+ } ,
4452 } ;
4553}
0 commit comments