-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_pipeline_generator.js
58 lines (51 loc) · 1.64 KB
/
stream_pipeline_generator.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const { pipeline } = require("node:stream/promises");
const fs = require("node:fs");
async function processChunk(chunk, signal) {
// Check if the operation is aborted
if (signal && signal.aborted) {
throw new Error("Pipeline aborted due to timeout.");
}
// Example processing: Convert chunk to uppercase
return chunk.toUpperCase();
}
async function executePipeline(signal) {
// Simulate a delay of 10 seconds
await new Promise((resolve, reject) => setTimeout(resolve, 1000));
// Check if the operation is aborted
if (signal && signal.aborted) {
throw new Error("Pipeline aborted due to timeout.");
}
await pipeline(
fs.createReadStream("lowercase.txt"),
async function* (source) {
source.setEncoding("utf8");
for await (const chunk of source) {
yield processChunk(chunk, signal);
}
},
fs.createWriteStream("uppercase.txt")
);
}
async function createAbortController(timeoutDuration) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), timeoutDuration);
return { controller, timeout };
}
async function run() {
const { controller, timeout } = await createAbortController(5000);
try {
await executePipeline(controller.signal);
console.log("Pipeline succeeded.");
} catch (err) {
if (err.message === "Pipeline aborted due to timeout.") {
console.log("Pipeline aborted due to timeout.");
} else if (err.name === "AbortError") {
console.log("Pipeline aborted by the user.");
} else {
console.error("Pipeline failed:", err);
}
} finally {
clearTimeout(timeout);
}
}
run().catch(console.error);