MCPcopy
hub / github.com/mswjs/msw / waitForClientRequest

Function waitForClientRequest

test/support/utils.ts:17–49  ·  view source on GitHub ↗
(request: ClientRequest)

Source from the content-addressed store, hash-verified

15}
16
17export async function waitForClientRequest(request: ClientRequest): Promise<{
18 response: IncomingMessage
19 responseText: string
20}> {
21 return new Promise((resolve, reject) => {
22 request.once('error', (error) => {
23 /**
24 * @note Since Node.js v20, Node.js may throw an AggregateError
25 * that doesn't have the `message` property and thus won't be handled
26 * here correctly. Instead, use the error's `code` as the rejection reason.
27 * The code stays consistent across Node.js versions.
28 */
29 reject('code' in error ? error.code : error)
30 })
31 request.once('abort', () => reject(new Error('Request was aborted')))
32
33 request.on('response', (response) => {
34 response.once('error', reject)
35
36 const responseChunks: Array<Buffer> = []
37
38 response.on('data', (chunk) => {
39 responseChunks.push(Buffer.from(chunk))
40 })
41 response.once('end', () => {
42 resolve({
43 response,
44 responseText: Buffer.concat(responseChunks).toString('utf8'),
45 })
46 })
47 })
48 })
49}

Calls 3

fromMethod · 0.80
toStringMethod · 0.80
resolveFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…