better code

Massimo Melina committed Nov 27, 2023 at 17:05 UTC a4a743509d5f1fa21d89aaa369f4670aad9a4d2e
2 files changed +9 -20
src/misc.ts
+1 -1
@@ -121,7 +121,7 @@ export function asyncGeneratorToReadable<T>(generator: AsyncIterable<T>) {
121 })
122 }
123
124 -// produces as soon as a promise resolves, not sequentially
124 +// produces as promises resolve, not sequentially
125 export class AsapStream<T> extends Readable {
126 finished = false
127 constructor(private promises: Promise<T>[]) {
src/util-http.ts
+8 -19
@@ -4,6 +4,8 @@ import https, { RequestOptions } from 'node:https'
4 import http, { IncomingMessage } from 'node:http'
5 import { Readable } from 'node:stream'
6 import _ from 'lodash'
7 +import { text as stream2string } from 'node:stream/consumers'
8 +export { stream2string }
9
10 // in case the response is not 2xx, it will throw and the error object is the Response object
11 export async function httpString(url: string, options?: XRequestOptions): Promise<string> {
@@ -13,23 +15,6 @@ export async function httpString(url: string, options?: XRequestOptions): Promis
15 return await stream2string(res)
16 }
17
16 -export async function stream2string(stream: Readable): Promise<string> {
17 - return new Promise((resolve, reject) => {
18 - let data = ''
19 - stream.on('data', chunk =>
20 - data += chunk)
21 - stream.on('error', reject)
22 - stream.on('end', () => {
23 - try {
24 - resolve(data)
25 - }
26 - catch(e) {
27 - reject(e)
28 - }
29 - })
30 - })
31 -}
32 -
18 export interface XRequestOptions extends RequestOptions {
19 body?: string | Buffer | Readable
20 // basic cookie store
@@ -39,10 +24,14 @@ export interface XRequestOptions extends RequestOptions {
24
25 export function httpStream(url: string, { body, jar, noRedirect, ...options }: XRequestOptions ={}): Promise<IncomingMessage> {
26 return new Promise((resolve, reject) => {
42 - if (body)
27 + options.headers ??= {}
28 + if (body) {
29 options.method ||= 'POST'
30 + if (!(body instanceof Readable))
31 + options.headers['Content-Length'] ??= Buffer.byteLength(body)
32 + }
33 if (jar)
45 - (options.headers ||= {}).cookie = _.map(jar, (v,k) => `${k}=${v}; `).join('')
34 + options.headers.cookie = _.map(jar, (v,k) => `${k}=${v}; `).join('')
35 + (options.headers.cookie || '') // preserve parameter
36 const proto = url.startsWith('https:') ? https : http
37 const req = proto.request(url, options, res => {