Skip to content

A lightweight Node.js request library with proxy support and streamable responses, intended for use by VSCode extensions

License

Notifications You must be signed in to change notification settings

dankeboy36/request-light-stream

 
 

Repository files navigation

request-light-stream

npm Package NPM Downloads Build Status License: MIT

A lightweight request library intended to be used by VSCode extensions.

import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light-stream';

const headers = { 'Accept-Encoding': 'gzip, deflate' };
return xhr({ url: url, followRedirects: 5, headers }).then(response => {
    return response.responseText;
}, (error: XHRResponse) => {
    throw new Error(error.responseText || getErrorStatusDescription(error.status) || error.toString());
});

node/client.ts:

import { Readable } from 'node:stream'
import { xhr } from 'request-light-stream';

const response = await xhr({ url: url, responseType: 'stream' });
const readable = Readable.fromWeb(response.body);

let data = '';
for await (const chunk of readable) {
    data += chunk;
}

browser/client.ts:

import { xhr } from 'request-light-stream';

const response = await xhr({ url: url, responseType: 'stream' });
const reader = response.body.getReader();

const decoder = new TextDecoder('utf-8');
let data = '';
let done: boolean, value: Uint8Array | undefined;
while (!done) {
    ({ done, value } = await reader.read());
    if (value) {
        data += decoder.decode(value, { stream: true });
    }
}

About

A lightweight Node.js request library with proxy support and streamable responses, intended for use by VSCode extensions

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Languages

  • TypeScript 89.4%
  • JavaScript 10.6%