MCPcopy
hub / github.com/nodejs/undici / extractBody

Function extractBody

lib/web/fetch/body.js:39–243  ·  view source on GitHub ↗

* Extract a body with type from a byte sequence or BodyInit object * * @param {import('../../../types').BodyInit} object - The BodyInit object to extract from * @param {boolean} [keepalive=false] - If true, indicates that the body * @returns {[{stream: ReadableStream, source: any, length: number

(object, keepalive = false)

Source from the content-addressed store, hash-verified

37 * @see https://fetch.spec.whatwg.org/#concept-bodyinit-extract
38 */
39function extractBody (object, keepalive = false) {
40 // 1. Let stream be null.
41 let stream = null
42 let controller = null
43
44 // 2. If object is a ReadableStream object, then set stream to object.
45 if (webidl.is.ReadableStream(object)) {
46 stream = object
47 } else if (webidl.is.Blob(object)) {
48 // 3. Otherwise, if object is a Blob object, set stream to the
49 // result of running object’s get stream.
50 stream = object.stream()
51 } else {
52 // 4. Otherwise, set stream to a new ReadableStream object, and set
53 // up stream with byte reading support.
54 stream = new ReadableStream({
55 pull () {},
56 start (c) {
57 controller = c
58 },
59 cancel () {},
60 type: 'bytes'
61 })
62 }
63
64 // 5. Assert: stream is a ReadableStream object.
65 assert(webidl.is.ReadableStream(stream))
66
67 // 6. Let action be null.
68 let action = null
69
70 // 7. Let source be null.
71 let source = null
72
73 // 8. Let length be null.
74 let length = null
75
76 // 9. Let type be null.
77 let type = null
78
79 // 10. Switch on object:
80 if (typeof object === 'string') {
81 // Set source to the UTF-8 encoding of object.
82 // Note: setting source to a Uint8Array here breaks some mocking assumptions.
83 source = object
84
85 // Set type to `text/plain;charset=UTF-8`.
86 type = 'text/plain;charset=UTF-8'
87 } else if (webidl.is.URLSearchParams(object)) {
88 // URLSearchParams
89
90 // spec says to run application/x-www-form-urlencoded on body.list
91 // this is implemented in Node.js as apart of an URLSearchParams instance toString method
92 // See: https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/lib/internal/url.js#L490
93 // and https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/lib/internal/url.js#L1100
94
95 // Set source to the result of running the application/x-www-form-urlencoded serializer with object’s list.
96 source = object.toString()

Callers 7

writeH1Function · 0.85
writeH2Function · 0.85
schemeFetchFunction · 0.85
constructorMethod · 0.85
jsonMethod · 0.85
constructorMethod · 0.85
safelyExtractBodyFunction · 0.85

Calls 8

formdataEscapeFunction · 0.85
normalizeLinefeedsFunction · 0.85
ReadableStreamFromFunction · 0.85
readableStreamCloseFunction · 0.85
BufferSourceMethod · 0.80
toStringMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…