MCPcopy
hub / github.com/axios/axios / createCaptureTransport

Function createCaptureTransport

tests/smoke/cjs/tests/formData.smoke.test.cjs:9–54  ·  view source on GitHub ↗
(buildResponse)

Source from the content-addressed store, hash-verified

7const describeWithFormData = NODE_VERSION < 18 ? describe.skip : describe;
8
9const createCaptureTransport = (buildResponse) => {
10 return {
11 request(options, onResponse) {
12 const chunks = [];
13
14 const req = new Writable({
15 write(chunk, _encoding, callback) {
16 chunks.push(Buffer.from(chunk));
17 callback();
18 },
19 });
20
21 req.destroyed = false;
22 req.setTimeout = () => {};
23 req.write = req.write.bind(req);
24 req.close = () => {
25 req.destroyed = true;
26 };
27
28 const originalDestroy = req.destroy.bind(req);
29 req.destroy = (...args) => {
30 req.destroyed = true;
31 return originalDestroy(...args);
32 };
33
34 const originalEnd = req.end.bind(req);
35 req.end = (...args) => {
36 originalEnd(...args);
37
38 const body = Buffer.concat(chunks);
39 const response = buildResponse ? buildResponse(body, options) : {};
40
41 const res = new PassThrough();
42 res.statusCode = response.statusCode !== undefined ? response.statusCode : 200;
43 res.statusMessage = response.statusMessage || 'OK';
44 res.headers = response.headers || { 'content-type': 'application/json' };
45 res.req = req;
46
47 onResponse(res);
48 res.end(response.body || JSON.stringify({ ok: true }));
49 };
50
51 return req;
52 },
53 };
54};
55
56const bodyAsUtf8 = (value) => {
57 return Buffer.isBuffer(value) ? value.toString('utf8') : String(value);

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected