-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathhtml-rewriting-stream.ts
34 lines (30 loc) · 1.01 KB
/
html-rewriting-stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { loadEsmModule } from '../load-esm';
export async function htmlRewritingStream(content: string): Promise<{
rewriter: import('parse5-html-rewriting-stream').RewritingStream;
transformedContent: () => Promise<string>;
}> {
const { RewritingStream } = await loadEsmModule<typeof import('parse5-html-rewriting-stream')>(
'parse5-html-rewriting-stream',
);
const rewriter = new RewritingStream();
return {
rewriter,
transformedContent: () =>
pipeline(Readable.from(content), rewriter, async function (source) {
const chunks = [];
for await (const chunk of source) {
chunks.push(Buffer.from(chunk));
}
return Buffer.concat(chunks).toString('utf-8');
}),
};
}