MCPcopy
hub / github.com/ampproject/amphtml / fromStructuredCloneable

Function fromStructuredCloneable

src/utils/xhr-utils.js:118–162  ·  view source on GitHub ↗
(response, responseType)

Source from the content-addressed store, hash-verified

116 * @private
117 */
118export function fromStructuredCloneable(response, responseType) {
119 userAssert(isObject(response), 'Object expected: %s', response);
120
121 const isDocumentType = responseType == 'document';
122 if (!isDocumentType) {
123 // Use native `Response` type if available for performance. If response
124 // type is `document`, we must fall back to `FetchResponse` polyfill
125 // because callers would then rely on the `responseXML` property being
126 // present, which is not supported by the Response type.
127 return new Response(response['body'], response['init']);
128 }
129
130 const lowercasedHeaders = map();
131 const data = {
132 status: 200,
133 statusText: 'OK',
134 /**
135 * @param {string} name
136 * @return {string}
137 */
138 getResponseHeader(name) {
139 return lowercasedHeaders[String(name).toLowerCase()] || null;
140 },
141 };
142
143 if (response['init']) {
144 const init = response['init'];
145 if (isArray(init.headers)) {
146 /** @type {!Array} */ (init.headers).forEach((entry) => {
147 const headerName = entry[0];
148 const headerValue = entry[1];
149 lowercasedHeaders[String(headerName).toLowerCase()] =
150 String(headerValue);
151 });
152 }
153 if (init.status) {
154 data.status = parseInt(init.status, 10);
155 }
156 if (init.statusText) {
157 data.statusText = String(init.statusText);
158 }
159 }
160
161 return new Response(response['body'] ? String(response['body']) : '', data);
162}
163
164/**
165 * Intercepts the XHR and proxies it through the viewer if necessary.

Callers 1

Calls 6

userAssertFunction · 0.90
isObjectFunction · 0.90
mapFunction · 0.90
isArrayFunction · 0.85
StringFunction · 0.50
forEachMethod · 0.45

Tested by

no test coverage detected