-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
167 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type * as http from 'node:http' | ||
import type * as https from 'node:https' | ||
import type * as stream from 'node:stream' | ||
|
||
export async function request( | ||
transport: typeof http | typeof https, | ||
opt: https.RequestOptions, | ||
body: Buffer | string | stream.Readable | null = null, | ||
): Promise<http.IncomingMessage> { | ||
return new Promise<http.IncomingMessage>((resolve, reject) => { | ||
const requestObj = transport.request(opt, (resp) => { | ||
resolve(resp) | ||
}) | ||
|
||
requestObj.on('error', (e: unknown) => { | ||
reject(e) | ||
}) | ||
|
||
if (body) { | ||
if (!Buffer.isBuffer(body) && typeof body !== 'string') { | ||
body.on('error', reject) | ||
} | ||
|
||
requestObj.end(body) | ||
} else { | ||
requestObj.end(null) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type http from 'node:http' | ||
import type stream from 'node:stream' | ||
|
||
export async function readAsBuffer(res: stream.Readable): Promise<Buffer> { | ||
return new Promise((resolve, reject) => { | ||
const body: Buffer[] = [] | ||
res | ||
.on('data', (chunk: Buffer) => body.push(chunk)) | ||
.on('error', (e) => reject(e)) | ||
.on('end', () => resolve(Buffer.concat(body))) | ||
}) | ||
} | ||
|
||
export async function readAsString(res: http.IncomingMessage): Promise<string> { | ||
const body = await readAsBuffer(res) | ||
return body.toString() | ||
} | ||
|
||
export async function drainResponse(res: stream.Readable): Promise<void> { | ||
return new Promise((resolve, reject) => { | ||
res | ||
.on('data', () => {}) | ||
.on('error', (e) => reject(e)) | ||
.on('end', () => resolve()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters