MCPcopy Create free account
hub / github.com/nodejs/node / wrapRequestBody

Function wrapRequestBody

deps/undici/src/lib/core/util.js:34–73  ·  view source on GitHub ↗

* @param {*} body * @returns {*}

(body)

Source from the content-addressed store, hash-verified

32 * @returns {*}
33 */
34function wrapRequestBody (body) {
35 if (isStream(body)) {
36 // TODO (fix): Provide some way for the user to cache the file to e.g. /tmp
37 // so that it can be dispatched again?
38 // TODO (fix): Do we need 100-expect support to provide a way to do this properly?
39 if (bodyLength(body) === 0) {
40 body
41 .on('data', function () {
42 assert(false)
43 })
44 }
45
46 if (typeof body.readableDidRead !== 'boolean') {
47 body[kBodyUsed] = false
48 EE.prototype.on.call(body, 'data', function () {
49 this[kBodyUsed] = true
50 })
51 }
52
53 return body
54 } else if (body && typeof body.pipeTo === 'function') {
55 // TODO (fix): We can't access ReadableStream internal state
56 // to determine whether or not it has been disturbed. This is just
57 // a workaround.
58 return new BodyAsyncIterable(body)
59 } else if (body && isFormDataLike(body)) {
60 return body
61 } else if (
62 body &&
63 typeof body !== 'string' &&
64 !ArrayBuffer.isView(body) &&
65 isIterable(body)
66 ) {
67 // TODO: Should we allow re-using iterable if !this.opts.idempotent
68 // or through some other flag?
69 return new BodyAsyncIterable(body)
70 } else {
71 return body
72 }
73}
74
75/**
76 * @param {*} obj

Callers 1

constructorMethod · 0.50

Calls 7

isStreamFunction · 0.70
bodyLengthFunction · 0.70
isFormDataLikeFunction · 0.70
isIterableFunction · 0.70
assertFunction · 0.50
onMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected