1.0.2 • Published 5 months ago
request-light-stream v1.0.2
request-light-stream
A lightweight request library intended to be used by VSCode extensions.
- NodeJS and browser main entry points
- proxy support: Use
configure
orHTTP_PROXY
andHTTPS_PROXY
env variables to configure the HTTP proxy addresses. ReadableStream
responsebody
(microsoft/node-request-light#34).
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 });
}
}