Skip to content

Conversation

@dankeboy36
Copy link
Contributor

@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
Contributor Author

@microsoft-github-policy-service agree

@aeschli aeschli enabled auto-merge (squash) June 30, 2025 17:41
@aeschli
Copy link
Contributor

aeschli commented Jun 30, 2025

Very cool, thanks @dankeboy36 !

@vs-code-engineering vs-code-engineering bot added this to the July 2025 milestone Jun 30, 2025
@aeschli aeschli merged commit 29fcfb7 into microsoft:main Jun 30, 2025
2 checks passed
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

3 participants