google storage api | test stream to output | upload files to google cloud | Search

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.

Run example

npm run import -- "fetch file or stream"

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;

What the code could have been:

```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:

  1. Dependencies:

  2. fetchOrStream Function:

  3. Export:

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.