Skip to content
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

Merged
merged 20 commits into from
Aug 2, 2018
Merged

Conversation

prateekbh
Copy link
Member

@prateekbh prateekbh commented Jul 31, 2018

  • Implements common functions (maybeIntercept_, fetchAmpCors_, fromStructuredCloneable_, toStructuredCloneable_) as utility functions.
  • xhr-impl and document-fetcher use these utility functions.
  • No one uses document-fetcher for now(this is just to keep the files to review count as low as possible).
  • Fixes Task Add commit hooks #1 in Move AMP runtime to standard fetch  #16707

Next PR will shift all uses of fetchDocument to document-fetcher.

@prateekbh prateekbh changed the title [DO-NOT-REVIEW] document-fetcher using utils functions Document-fetcher using utils functions Jul 31, 2018
@prateekbh
Copy link
Member Author

@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.

* @return {!FetchResponse|!Response} The deserialized regular response.
* @private
*/
export function fromStructuredCloneable(response, responseType) {

This comment was marked as resolved.

user().assert(isObject(response), 'Object expected: %s', response);

const isDocumentType = responseType == 'document';
if (typeof Response === 'function' && !isDocumentType) {

This comment was marked as resolved.

* `Promise<undefined>` otherwise.
* @private
*/
export function getViewerInterceptResponse(win, ampdocSingle, input, init) {

This comment was marked as resolved.

* @param {!FetchInitDef} init The options of the XHR which may get
* intercepted.
*/
export function setupInput(win, input, init) {

This comment was marked as resolved.

* @param {string=} opt_accept The HTTP Accept header value.
* @return {!FetchInitDef}
*/
export function setupInit(opt_init, opt_accept) {
Copy link
Member Author

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

Copy link
Contributor

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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

* @param {!../utils/xhr-utils.FetchInitDef=} init
* @return {!FetchResponse}
*/
export function verifyAmpCORSHeaders(win, response, init) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

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.

@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
@ampproject ampproject deleted a comment from prateekbh Jul 31, 2018
Copy link
Contributor

@jridgewell jridgewell left a 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

export function toStructuredCloneable(input, init) {
const newInit = Object.assign({}, init);
if (isFormDataWrapper(init.body)) {
newInit.headers = newInit.headers || {};
Copy link
Contributor

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) {
Copy link
Contributor

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.

@@ -0,0 +1,145 @@
/**
Copy link
Contributor

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?

Copy link
Member Author

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'] || {};
Copy link
Contributor

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.

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`');
Copy link
Contributor

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) {
Copy link
Contributor

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

@codecov-io
Copy link

codecov-io commented Aug 2, 2018

Codecov Report

Merging #17206 into master will increase coverage by 0.01%.
The diff coverage is 93.5%.

@@            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
Flag Coverage Δ
#integration_tests 36.1% <37.01%> (-0.02%) ⬇️
#unit_tests 76.85% <93.5%> (+0.02%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 85e69dc...d24b898. Read the comment docs.

@prateekbh prateekbh merged commit 940f1d3 into ampproject:master Aug 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants