I am trying to send a Readable Stream as Response to a CloudFlare Worker. I am using ytdl-core npm module to fetch the Readable Stream.
Here is the entry point for the worker.
const getAudio = (videoID, quality = "lowest") => {
const audioStream = ytdl("http://www.youtube.com/watch?v=" + videoID, {
quality,
filter: "audioonly",
});
audioStream.on("error", (e) => {
console.log(`Error: ${e.message}`);
});
return audioStream;
};
const handleRequest = (request) => {
const url = new URL(request.url);
const videoID = url.searchParams.get("v");
if (typeof videoID === "string") {
const nodeStream = getAudio(videoID);
const webStream = nodeToWebStream(nodeStream);
let { readable, writable } = new TransformStream();
webStream.pipeTo(writable);
return new Response(readable, webStream);
} else return new Response("Hello World");
};
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
Here is the Webpack Config
const webpack = require("webpack");
module.exports = {
target: "webworker",
entry: "./index.js",
plugins: [
new webpack.ProvidePlugin({
ReadableStream: ["web-streams-polyfill/ponyfill", "ReadableStream"],
ytdl: "ytdl-core",
nodeToWebStream: "readable-stream-node-to-web",
}),
],
};
I am converting the Nodejs Readable Stream to a Web Readable Stream and then piping it to the Writable Stream part of the Transform Stream as mentioned in the Cloudflare Docs. But I am encountering an error like this:
TypeError: ReadableStream.prototype.pipeTo's first argument must be a WritableStream
at e.pipeTo (worker.js:11:89436)
at r (worker.js:9:33134)
at worker.js:9:33241 at line 10, col 89434
{
"exceptionDetails": {
"columnNumber": 89434,
"exception": {
"className": "TypeError",
"description": "TypeError: ReadableStream.prototype.pipeTo's first argument must be a WritableStream\n at e.pipeTo (worker.js:11:89436)\n at r (worker.js:9:33134)\n at worker.js:9:33241",
"preview": null,
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 10,
"text": "Uncaught (in promise)",
"url": "worker.js"
},
"timestamp": 1636696072821
}
TypeError: Cannot read properties of undefined (reading 'protocol')
at Object.u.request (worker.js:9:11037)
at Object.o.request (worker.js:11:21923)
at O (worker.js:8:27433)
at p.run (worker.js:1:2260)
at d (worker.js:1:1754) at line 8, col 11035
{
"exceptionDetails": {
"columnNumber": 11035,
"exception": {
"className": "TypeError",
"description": "TypeError: Cannot read properties of undefined (reading 'protocol')\n at Object.u.request (worker.js:9:11037)\n at Object.o.request (worker.js:11:21923)\n at O (worker.js:8:27433)\n at p.run (worker.js:1:2260)\n at d (worker.js:1:1754)",
"preview": null,
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 8,
"text": "Uncaught",
"url": "worker.js"
},
"timestamp": 1636696072821
}
TypeError: Cannot read properties of undefined (reading 'protocol') at line 0, col -2
{
"exceptionDetails": {
"columnNumber": -2,
"exception": {
"className": "TypeError",
"description": "TypeError: Cannot read properties of undefined (reading 'protocol')",
"preview": null,
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 0,
"text": "Uncaught (async)",
"url": "undefined"
},
"timestamp": 1636696072821
}
TypeError: Cannot read properties of undefined (reading 'protocol')
at Object.u.request (worker.js:9:11037)
at Object.o.request (worker.js:11:21923)
at O (worker.js:8:27433)
at p.run (worker.js:1:2260)
at d (worker.js:1:1754)
at f (worker.js:1:1662) at line 8, col 11035
{
"exceptionDetails": {
"columnNumber": 11035,
"exception": {
"className": "TypeError",
"description": "TypeError: Cannot read properties of undefined (reading 'protocol')\n at Object.u.request (worker.js:9:11037)\n at Object.o.request (worker.js:11:21923)\n at O (worker.js:8:27433)\n at p.run (worker.js:1:2260)\n at d (worker.js:1:1754)\n at f (worker.js:1:1662)",
"preview": null,
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 8,
"text": "Uncaught",
"url": "worker.js"
},
"timestamp": 1636696072821
}
TypeError: Cannot read properties of undefined (reading 'protocol') at line 0, col -2
{
"exceptionDetails": {
"columnNumber": -2,
"exception": {
"className": "TypeError",
"description": "TypeError: Cannot read properties of undefined (reading 'protocol')",
"preview": null,
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 0,
"text": "Uncaught (async)",
"url": "undefined"
},
"timestamp": 1636696072821
}
Error: The script will never generate a response. at line 0, col -2
{
"exceptionDetails": {
"columnNumber": -2,
"exception": {
"className": "Error",
"description": "Error: The script will never generate a response.",
"preview": {
"description": "Error: The script will never generate a response.",
"entries": null,
"overflow": false,
"properties": [
{
"name": "stack",
"subtype": null,
"type": "string",
"value": "Error: The script will never generate a response.",
"valuePreview": null
},
{
"name": "message",
"subtype": null,
"type": "string",
"value": "The script will never generate a response.",
"valuePreview": null
}
],
"subtype": "error",
"type": "object"
},
"subtype": "error",
"type": "object",
"value": null
},
"lineNumber": 0,
"text": "Uncaught (in response)",
"url": "undefined"
},
"timestamp": 1636696073821
}
Any help is Appreciated.