@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;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,cAAc,EAAE,aAAa,GAAE,MAAkB,GAAG,QAAQ,CAsI5G"}
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
- let reader = null;
17
- let pumpActive = true;
18
- let abortController = null;
19
- let isReading = false; // Prevent concurrent reads
20
- const nodeStream = new stream_1.Readable({
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
- nodeStream.push(null); // End the stream
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
- // Real error, report it
103
- nodeStream.destroy(error);
16
+ this.push(value);
104
17
  }
105
18
  }
106
- }
107
- finally {
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
- catch { }
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":";;AAYA,sDAsIC;AAlJD,mCAAkC;AAElC;;;;;;;;;GASG;AACH,SAAgB,qBAAqB,CAAC,SAAyB,EAAE,gBAAwB,EAAE,GAAG,IAAI;IACjG,IAAI,MAAM,GAAuC,IAAI,CAAC;IACtD,IAAI,UAAU,GAAG,IAAI,CAAC;IACtB,IAAI,eAAe,GAA2B,IAAI,CAAC;IACnD,IAAI,SAAS,GAAG,KAAK,CAAC,CAAC,2BAA2B;IAElD,MAAM,UAAU,GAAG,IAAI,iBAAQ,CA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AI,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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ziplayer/plugin",
3
- "version": "0.2.1-dev-2",
3
+ "version": "0.2.1-dev-4",
4
4
  "description": "A modular Discord voice player with plugin system",
5
5
  "keywords": [
6
6
  "ZiPlayer",
@@ -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
- let reader: ReadableStreamDefaultReader | null = null;
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
- const nodeStream = new Readable({
20
- // Set buffer size to 64KB - balance between memory and throughput
21
- highWaterMark: highWaterMark,
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
- nodeStream.push(null); // End the stream
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
- // Real error, report it
114
- nodeStream.destroy(error as Error);
14
+ this.push(value);
115
15
  }
16
+ } catch (err) {
17
+ this.destroy(err as Error);
116
18
  }
117
- } finally {
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
  }