-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document-fetcher using utils functions #17206
Conversation
@jridgewell i've split it xhr class into utility functions, and everything seems to work, please have a look |
* cloneable request. | ||
* @private | ||
*/ | ||
export function toStructuredCloneable(input, init) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
* @return {!FetchResponse|!Response} The deserialized regular response. | ||
* @private | ||
*/ | ||
export function fromStructuredCloneable(response, responseType) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
user().assert(isObject(response), 'Object expected: %s', response); | ||
|
||
const isDocumentType = responseType == 'document'; | ||
if (typeof Response === 'function' && !isDocumentType) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
* `Promise<undefined>` otherwise. | ||
* @private | ||
*/ | ||
export function getViewerInterceptResponse(win, ampdocSingle, input, init) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
* @param {!FetchInitDef} init The options of the XHR which may get | ||
* intercepted. | ||
*/ | ||
export function setupInput(win, input, init) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
* @param {string=} opt_accept The HTTP Accept header value. | ||
* @return {!FetchInitDef} | ||
*/ | ||
export function setupInit(opt_init, opt_accept) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This piece needs review.
Its a mix of logic operating on init here: https://github.com/ampproject/amphtml/blob/master/src/service/xhr-impl.js#L139-L142
and in original setupInit function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
* @param {?FetchInitDef=} init | ||
* @return {!FetchInitDef} | ||
*/ | ||
export function setupAMPCors(win, input, init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs review
a function forked from here: https://github.com/ampproject/amphtml/blob/master/src/service/xhr-impl.js#L375-L396
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
* @return {string} | ||
* @private | ||
*/ | ||
function normalizeMethod_(method) { |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
* @param {!../utils/xhr-utils.FetchInitDef=} init | ||
* @return {!FetchResponse} | ||
*/ | ||
export function verifyAmpCORSHeaders(win, response, init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a post processing function forked from here: https://github.com/ampproject/amphtml/blob/master/src/service/xhr-impl.js#L398-L414
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
return response; | ||
} | ||
|
||
// TODO(prateekbh): move everything below this line into the polyfill |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Save my nits for a followup PR.
* @param {string=} opt_accept The HTTP Accept header value. | ||
* @return {!FetchInitDef} | ||
*/ | ||
export function setupInit(opt_init, opt_accept) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
src/utils/xhr-utils.js
Outdated
export function toStructuredCloneable(input, init) { | ||
const newInit = Object.assign({}, init); | ||
if (isFormDataWrapper(init.body)) { | ||
newInit.headers = newInit.headers || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This should be guaranteed.
xhr.responseType = init.responseType; | ||
} | ||
|
||
if (init.headers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This should be guaranteed.
src/document-fetcher.js
Outdated
@@ -0,0 +1,145 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we drop this file until the document fetch PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure
const currentOrigin = getWinOrigin(win); | ||
const targetOrigin = parseUrlDeprecated(input).origin; | ||
if (currentOrigin == targetOrigin) { | ||
init['headers'] = init['headers'] || {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This should be guaranteed.
src/utils/xhr-utils.js
Outdated
init['headers']['AMP-Same-Origin'] = 'true'; | ||
} | ||
// In edge a `TypeMismatchError` is thrown when body is set to null. | ||
dev().assert(init.body !== null, 'fetch `body` can not be `null`'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: seems like we could move this into setupInit
.
* @param {?FetchInitDef=} init | ||
* @return {!FetchInitDef} | ||
*/ | ||
export function setupAMPCors(win, input, init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
* @param {!../utils/xhr-utils.FetchInitDef=} init | ||
* @return {!FetchResponse} | ||
*/ | ||
export function verifyAmpCORSHeaders(win, response, init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved.
Codecov Report
@@ Coverage Diff @@
## master #17206 +/- ##
==========================================
+ Coverage 77.78% 77.8% +0.01%
==========================================
Files 562 563 +1
Lines 41199 41205 +6
==========================================
+ Hits 32048 32058 +10
+ Misses 9151 9147 -4
Continue to review full report at Codecov.
|
maybeIntercept_
,fetchAmpCors_
,fromStructuredCloneable_
,toStructuredCloneable_
) as utility functions.xhr-impl
anddocument-fetcher
use these utility functions.Next PR will shift all uses of
fetchDocument
todocument-fetcher
.