Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for streamable response body #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dankeboy36
Copy link

@dankeboy36 dankeboy36 commented Jan 30, 2025

Proof of concept: https://github.com/dankeboy36/request-light-stream

index.js

// @ts-check

const { createWriteStream, promises: fs } = require('node:fs');
const path = require('node:path');
const { Readable } = require('node:stream');
const { pipeline } = require('node:stream/promises');

const ProgressBar = require('progress');
const { xhr } = require('request-light-stream');

const url =
  'https://downloads.arduino.cc/arduino-cli/arduino-cli_1.1.1_macOS_ARM64.tar.gz';

xhr({ url, responseType: 'stream' }).then(async (response) => {
  if (response.status !== 200) throw new Error(`Failed to download: ${response.status}`);
  if (!response.body) throw new Error('Response body is empty');

  const tempDirPath = await fs.mkdtemp(path.join(__dirname, 'arduino-cli-'));
  const tempFilePath = path.join(tempDirPath, 'arduino-cli.tar.gz');
  const destination = createWriteStream(tempFilePath);
  const source = Readable.fromWeb(response.body);

  const contentLengthValues = response.headers['content-length'] ?? '0';
  const contentLengthValue = Array.isArray(contentLengthValues)
    ? contentLengthValues[0]
    : contentLengthValues;
  const contentLength = parseInt(contentLengthValue, 10);

  const bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: contentLength,
  });

  source.on('data', (chunk) => {
    bar.tick(chunk.length);
  });

  source.on('end', () => {
    console.log('Downloaded', tempFilePath);
  });

  await pipeline(source, destination);
});

package.json:

{
  "name": "request-light-stream-test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "progress": "^2.0.3",
    "request-light-stream": "^1.0.1"
  },
  "devDependencies": {
    "@types/progress": "^2.0.7"
  }
}

In-action:

microsoft__node-request-light__35.mov
microsoft__node-request-light__35_code.mov

Closes #34

@dankeboy36
Copy link
Author

@microsoft-github-policy-service agree

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Closes microsoft#34

Signed-off-by: dankeboy36 <dankeboy36@gmail.com>
Signed-off-by: dankeboy36 <dankeboy36@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Support ReadableStream response body
1 participant