This code provides a utility function called fetchOrStream
that downloads a file from a URL or processes an existing stream, writing its content to a specified write stream. It handles both scenarios using a single function and returns a Promise for asynchronous execution.
npm run import -- "fetch file or stream"
var util = require('util');
var http = require('http');
var https = require('https');
function fetchOrStream(stream, writeStream) {
var promise;
if(typeof stream == 'object') {
promise = Promise.resolve(stream);
} else {
var protocol = stream.includes('https://') ? https : http;
promise = util.promisify(protocol.get.bind(protocol))(stream)
}
return promise
.then(() => new Promise((resolve, reject) => {
stream.pipe(writeStream)
.on("error", (err) => {
reject(err);
})
.on('finish', () => {
resolve();
});
}))
}
module.exports = fetchOrStream;
```javascript
const { promisify } = require('util');
const https = require('https');
const http = require('http');
/**
* Fetches or streams a resource based on the input.
*
* @param {string|object} stream - The URL or stream to fetch or stream.
* @param {stream.Writable} writeStream - The writable stream to pipe the fetched data into.
* @returns {Promise<void>} A promise that resolves when the streaming is complete or rejects with an error.
*/
function fetchOrStream(stream, writeStream) {
// Check if the stream is already an object (e.g. a readable stream)
if (typeof stream === 'object' && stream!== null) {
// Return a resolved promise to avoid unnecessary promises
return Promise.resolve(stream);
}
// Determine the protocol (http or https) based on the stream URL
const protocol = stream.includes('https://')? https : http;
// Use a promise to handle the protocol.get operation
const getPromise = promisify(protocol.get.bind(protocol));
// Chain promises to pipe the stream into the writeStream
return getPromise(stream)
.then((res) => {
// Pipe the response into the writeStream
res.pipe(writeStream)
.on('error', (err) => {
// Reject the promise if an error occurs
return Promise.reject(err);
})
.on('finish', () => {
// Resolve the promise when the piping is complete
return Promise.resolve();
});
});
}
module.exports = fetchOrStream;
```
This code defines a utility function called fetchOrStream
that downloads a file from a URL or processes an existing stream and writes its content to a provided write stream.
Here's a breakdown:
Dependencies:
util
: Node.js built-in module providing utility functions, including promisify
.http
: Node.js built-in module for making HTTP requests.https
: Node.js built-in module for making HTTPS requests.fetchOrStream
Function:
stream
: Either a stream object (e.g., from a previous download) or a URL string.writeStream
: A write stream object to write the downloaded data to.stream
is an object (existing stream) or a string (URL).stream
is a string, it determines the protocol (HTTP or HTTPS) and uses util.promisify
to convert the protocol.get
function into a Promise. This fetches the file from the URL.writeStream
.writeStream
.Export:
module.exports = fetchOrStream;
: Makes the fetchOrStream
function available for use in other modules.In essence, this code provides a flexible way to download files from URLs or process existing streams, handling both cases with a single function and returning a Promise for asynchronous handling.