1.0.2 • Published 5 months ago

request-light-stream v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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 });
    }
}
1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago