@ziplayer/plugin 0.2.1-dev-2 → 0.2.1-dev-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,13 +1,3 @@
|
|
|
1
1
|
import { Readable } from "stream";
|
|
2
|
-
/**
|
|
3
|
-
* Converts a Web ReadableStream to a Node.js Readable stream
|
|
4
|
-
* with proper backpressure handling to prevent memory bloat
|
|
5
|
-
*
|
|
6
|
-
* Optimization:
|
|
7
|
-
* - Respects highWaterMark (64KB buffer)
|
|
8
|
-
* - Handles backpressure by pausing reads when buffer is full
|
|
9
|
-
* - Batches small chunks to reduce overhead
|
|
10
|
-
* - Lazy evaluation - only reads when consumer is ready
|
|
11
|
-
*/
|
|
12
2
|
export declare function webStreamToNodeStream(webStream: ReadableStream, highWaterMark?: number): Readable;
|
|
13
3
|
//# sourceMappingURL=stream-converter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-converter.d.ts","sourceRoot":"","sources":["../../src/utils/stream-converter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC
|
|
1
|
+
{"version":3,"file":"stream-converter.d.ts","sourceRoot":"","sources":["../../src/utils/stream-converter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,cAAc,EAAE,aAAa,GAAE,MAAkB,GAAG,QAAQ,CAkB5G"}
|
|
@@ -2,134 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.webStreamToNodeStream = webStreamToNodeStream;
|
|
4
4
|
const stream_1 = require("stream");
|
|
5
|
-
/**
|
|
6
|
-
* Converts a Web ReadableStream to a Node.js Readable stream
|
|
7
|
-
* with proper backpressure handling to prevent memory bloat
|
|
8
|
-
*
|
|
9
|
-
* Optimization:
|
|
10
|
-
* - Respects highWaterMark (64KB buffer)
|
|
11
|
-
* - Handles backpressure by pausing reads when buffer is full
|
|
12
|
-
* - Batches small chunks to reduce overhead
|
|
13
|
-
* - Lazy evaluation - only reads when consumer is ready
|
|
14
|
-
*/
|
|
15
5
|
function webStreamToNodeStream(webStream, highWaterMark = 64 * 1024) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
// Set buffer size to 64KB - balance between memory and throughput
|
|
22
|
-
highWaterMark: highWaterMark,
|
|
23
|
-
read(size) {
|
|
24
|
-
// Resume reading when stream is ready for more data
|
|
25
|
-
if (!isReading && pumpActive && reader) {
|
|
26
|
-
isReading = true;
|
|
27
|
-
pump().catch((error) => {
|
|
28
|
-
if (pumpActive) {
|
|
29
|
-
console.error("[stream-converter] Pump error:", error);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
destroy(error, callback) {
|
|
35
|
-
// Gracefully stop the pump when stream is destroyed
|
|
36
|
-
pumpActive = false;
|
|
37
|
-
// Cancel reader if active
|
|
38
|
-
if (reader) {
|
|
39
|
-
try {
|
|
40
|
-
reader.cancel().catch(() => {
|
|
41
|
-
// Ignore cancel errors
|
|
42
|
-
});
|
|
43
|
-
reader = null;
|
|
44
|
-
}
|
|
45
|
-
catch { }
|
|
46
|
-
}
|
|
47
|
-
// Abort any pending operations
|
|
48
|
-
if (abortController) {
|
|
49
|
-
try {
|
|
50
|
-
abortController.abort();
|
|
51
|
-
}
|
|
52
|
-
catch { }
|
|
53
|
-
abortController = null;
|
|
54
|
-
}
|
|
55
|
-
callback(error);
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
// Create abort controller for graceful shutdown
|
|
59
|
-
abortController = new AbortController();
|
|
60
|
-
// Create a reader from the Web Stream
|
|
61
|
-
reader = webStream.getReader();
|
|
62
|
-
// Read chunks with backpressure support
|
|
63
|
-
const pump = async () => {
|
|
64
|
-
try {
|
|
65
|
-
while (pumpActive && reader) {
|
|
6
|
+
const reader = webStream.getReader();
|
|
7
|
+
return new stream_1.Readable({
|
|
8
|
+
highWaterMark: highWaterMark ?? 64 * 1024,
|
|
9
|
+
async read() {
|
|
10
|
+
try {
|
|
66
11
|
const { done, value } = await reader.read();
|
|
67
|
-
// Check if pump was stopped during read
|
|
68
|
-
if (!pumpActive || !reader) {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
12
|
if (done) {
|
|
72
|
-
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
if (value && pumpActive) {
|
|
76
|
-
// Convert to Buffer and push
|
|
77
|
-
const buffer = Buffer.from(value);
|
|
78
|
-
// Check backpressure: push() returns false if internal buffer is full
|
|
79
|
-
// This means we should pause and let the consumer catch up
|
|
80
|
-
const shouldContinue = nodeStream.push(buffer);
|
|
81
|
-
if (!shouldContinue) {
|
|
82
|
-
// Internal buffer is full, pause reading
|
|
83
|
-
isReading = false;
|
|
84
|
-
break; // Exit pump, will resume when consumer calls read()
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
// Only destroy if pump is still active and stream exists
|
|
91
|
-
if (pumpActive) {
|
|
92
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
93
|
-
// Ignore "Controller is already closed" and stream cancelled errors
|
|
94
|
-
if (errorMsg.includes("Controller is already closed") ||
|
|
95
|
-
errorMsg.includes("already been cancelled") ||
|
|
96
|
-
errorMsg.includes("stream closed") ||
|
|
97
|
-
errorMsg.includes("aborted")) {
|
|
98
|
-
// Stream was destroyed externally, just end cleanly
|
|
99
|
-
nodeStream.push(null);
|
|
13
|
+
this.push(null);
|
|
100
14
|
}
|
|
101
15
|
else {
|
|
102
|
-
|
|
103
|
-
nodeStream.destroy(error);
|
|
16
|
+
this.push(value);
|
|
104
17
|
}
|
|
105
18
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
// Mark as not reading
|
|
109
|
-
isReading = false;
|
|
110
|
-
// Cleanup reader when pump ends
|
|
111
|
-
try {
|
|
112
|
-
if (reader) {
|
|
113
|
-
await reader.cancel();
|
|
114
|
-
reader = null;
|
|
115
|
-
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
this.destroy(err);
|
|
116
21
|
}
|
|
117
|
-
|
|
118
|
-
pumpActive = false;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
// Start initial pump when stream is requested
|
|
122
|
-
// Note: pump will be called by read() callback for backpressure compliance
|
|
123
|
-
setImmediate(() => {
|
|
124
|
-
if (pumpActive && reader && !isReading) {
|
|
125
|
-
isReading = true;
|
|
126
|
-
pump().catch((error) => {
|
|
127
|
-
if (pumpActive) {
|
|
128
|
-
console.error("[stream-converter] Initial pump error:", error);
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
22
|
+
},
|
|
132
23
|
});
|
|
133
|
-
return nodeStream;
|
|
134
24
|
}
|
|
135
25
|
//# sourceMappingURL=stream-converter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream-converter.js","sourceRoot":"","sources":["../../src/utils/stream-converter.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"stream-converter.js","sourceRoot":"","sources":["../../src/utils/stream-converter.ts"],"names":[],"mappings":";;AAEA,sDAkBC;AApBD,mCAAkC;AAElC,SAAgB,qBAAqB,CAAC,SAAyB,EAAE,gBAAwB,EAAE,GAAG,IAAI;IACjG,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,EAAE,CAAC;IAErC,OAAO,IAAI,iBAAQ,CAAC;QACnB,aAAa,EAAE,aAAa,IAAI,EAAE,GAAG,IAAI;QACzC,KAAK,CAAC,IAAI;YACT,IAAI,CAAC;gBACJ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;gBAC5C,IAAI,IAAI,EAAE,CAAC;oBACV,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACjB,CAAC;qBAAM,CAAC;oBACP,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;gBAClB,CAAC;YACF,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACd,IAAI,CAAC,OAAO,CAAC,GAAY,CAAC,CAAC;YAC5B,CAAC;QACF,CAAC;KACD,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,147 +1,21 @@
|
|
|
1
1
|
import { Readable } from "stream";
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* Converts a Web ReadableStream to a Node.js Readable stream
|
|
5
|
-
* with proper backpressure handling to prevent memory bloat
|
|
6
|
-
*
|
|
7
|
-
* Optimization:
|
|
8
|
-
* - Respects highWaterMark (64KB buffer)
|
|
9
|
-
* - Handles backpressure by pausing reads when buffer is full
|
|
10
|
-
* - Batches small chunks to reduce overhead
|
|
11
|
-
* - Lazy evaluation - only reads when consumer is ready
|
|
12
|
-
*/
|
|
13
3
|
export function webStreamToNodeStream(webStream: ReadableStream, highWaterMark: number = 64 * 1024): Readable {
|
|
14
|
-
|
|
15
|
-
let pumpActive = true;
|
|
16
|
-
let abortController: AbortController | null = null;
|
|
17
|
-
let isReading = false; // Prevent concurrent reads
|
|
4
|
+
const reader = webStream.getReader();
|
|
18
5
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
read(size?: number) {
|
|
24
|
-
// Resume reading when stream is ready for more data
|
|
25
|
-
if (!isReading && pumpActive && reader) {
|
|
26
|
-
isReading = true;
|
|
27
|
-
pump().catch((error) => {
|
|
28
|
-
if (pumpActive) {
|
|
29
|
-
console.error("[stream-converter] Pump error:", error);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
destroy(error: Error | null, callback: (error?: Error | null) => void) {
|
|
36
|
-
// Gracefully stop the pump when stream is destroyed
|
|
37
|
-
pumpActive = false;
|
|
38
|
-
|
|
39
|
-
// Cancel reader if active
|
|
40
|
-
if (reader) {
|
|
41
|
-
try {
|
|
42
|
-
reader.cancel().catch(() => {
|
|
43
|
-
// Ignore cancel errors
|
|
44
|
-
});
|
|
45
|
-
reader = null;
|
|
46
|
-
} catch {}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// Abort any pending operations
|
|
50
|
-
if (abortController) {
|
|
51
|
-
try {
|
|
52
|
-
abortController.abort();
|
|
53
|
-
} catch {}
|
|
54
|
-
abortController = null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
callback(error);
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Create abort controller for graceful shutdown
|
|
62
|
-
abortController = new AbortController();
|
|
63
|
-
|
|
64
|
-
// Create a reader from the Web Stream
|
|
65
|
-
reader = webStream.getReader();
|
|
66
|
-
|
|
67
|
-
// Read chunks with backpressure support
|
|
68
|
-
const pump = async () => {
|
|
69
|
-
try {
|
|
70
|
-
while (pumpActive && reader) {
|
|
6
|
+
return new Readable({
|
|
7
|
+
highWaterMark: highWaterMark ?? 64 * 1024,
|
|
8
|
+
async read() {
|
|
9
|
+
try {
|
|
71
10
|
const { done, value } = await reader.read();
|
|
72
|
-
|
|
73
|
-
// Check if pump was stopped during read
|
|
74
|
-
if (!pumpActive || !reader) {
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
11
|
if (done) {
|
|
79
|
-
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (value && pumpActive) {
|
|
84
|
-
// Convert to Buffer and push
|
|
85
|
-
const buffer = Buffer.from(value);
|
|
86
|
-
|
|
87
|
-
// Check backpressure: push() returns false if internal buffer is full
|
|
88
|
-
// This means we should pause and let the consumer catch up
|
|
89
|
-
const shouldContinue = nodeStream.push(buffer);
|
|
90
|
-
|
|
91
|
-
if (!shouldContinue) {
|
|
92
|
-
// Internal buffer is full, pause reading
|
|
93
|
-
isReading = false;
|
|
94
|
-
break; // Exit pump, will resume when consumer calls read()
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
} catch (error) {
|
|
99
|
-
// Only destroy if pump is still active and stream exists
|
|
100
|
-
if (pumpActive) {
|
|
101
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
102
|
-
|
|
103
|
-
// Ignore "Controller is already closed" and stream cancelled errors
|
|
104
|
-
if (
|
|
105
|
-
errorMsg.includes("Controller is already closed") ||
|
|
106
|
-
errorMsg.includes("already been cancelled") ||
|
|
107
|
-
errorMsg.includes("stream closed") ||
|
|
108
|
-
errorMsg.includes("aborted")
|
|
109
|
-
) {
|
|
110
|
-
// Stream was destroyed externally, just end cleanly
|
|
111
|
-
nodeStream.push(null);
|
|
12
|
+
this.push(null);
|
|
112
13
|
} else {
|
|
113
|
-
|
|
114
|
-
nodeStream.destroy(error as Error);
|
|
14
|
+
this.push(value);
|
|
115
15
|
}
|
|
16
|
+
} catch (err) {
|
|
17
|
+
this.destroy(err as Error);
|
|
116
18
|
}
|
|
117
|
-
}
|
|
118
|
-
// Mark as not reading
|
|
119
|
-
isReading = false;
|
|
120
|
-
|
|
121
|
-
// Cleanup reader when pump ends
|
|
122
|
-
try {
|
|
123
|
-
if (reader) {
|
|
124
|
-
await reader.cancel();
|
|
125
|
-
reader = null;
|
|
126
|
-
}
|
|
127
|
-
} catch {}
|
|
128
|
-
|
|
129
|
-
pumpActive = false;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// Start initial pump when stream is requested
|
|
134
|
-
// Note: pump will be called by read() callback for backpressure compliance
|
|
135
|
-
setImmediate(() => {
|
|
136
|
-
if (pumpActive && reader && !isReading) {
|
|
137
|
-
isReading = true;
|
|
138
|
-
pump().catch((error) => {
|
|
139
|
-
if (pumpActive) {
|
|
140
|
-
console.error("[stream-converter] Initial pump error:", error);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}
|
|
19
|
+
},
|
|
144
20
|
});
|
|
145
|
-
|
|
146
|
-
return nodeStream;
|
|
147
21
|
}
|