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

Fetch polyfill #17468

Merged
merged 28 commits into from Aug 20, 2018
Merged

Fetch polyfill #17468

merged 28 commits into from Aug 20, 2018

Conversation

prateekbh
Copy link
Member

Writing a fetch polyfill to be used by AMP runtime.

  • Uses the existing fetch poly/pony fill
  • Adds a convertor function for standard Response creationg

import {utf8Encode} from '../utils/bytes';

/** @private @enum {number} Allowed fetch responses. */
const allowedFetchTypes_ = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename to allowedFetchTypes

Copy link
Member Author

Choose a reason for hiding this comment

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

done

import {parseJson} from '../json';
import {utf8Encode} from '../utils/bytes';

/** @private @enum {number} Allowed fetch responses. */
Copy link
Contributor

Choose a reason for hiding this comment

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

@private annotation has no meaning here.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

* status: number,
* statusText: string,
* responseText: string,
* responseXML: ?Document,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is unneeded now.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

*/
class FetchResponse {
/**
* @param {!XMLHttpRequest|!XDomainRequest|!XMLHttpRequestDef} xhr
Copy link
Contributor

Choose a reason for hiding this comment

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

XDomainRequest is dropped.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

data.statusText = String(init.statusText);
}

return new FetchResponse(/** @type {XMLHttpRequestDef} */(data));
Copy link
Contributor

Choose a reason for hiding this comment

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

It's kinda weird that new Response() instanceof Response is false. I think the only difficulty to making Response be the main class is extracting out the headers from XML?

Copy link
Member Author

Choose a reason for hiding this comment

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

not really, i can make this a sub class of FetchResponse and just do super(blah)

Copy link
Contributor

Choose a reason for hiding this comment

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

Not really

Can you explain?

Copy link
Member Author

Choose a reason for hiding this comment

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

class Response extends FetchResponse{
  constructor(body, init={}) {
    // all the current logic of functionResponse
   // instead of return new FetchResponse(/** @type {XMLHttpRequestDef} */(data));
   super(/** @type {XMLHttpRequestDef} */(data));
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I understand the subclassing. I meant what else is the blocker for just using Response besides header parsing?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok, the fetchPolyfill is using FetchReponse as passing xhr in the constructor.
Just using Response class would just mean a re-write of that part and some additional code there

*/
export function install(win) {
if (!win.fetch) {
win.prototype.fetch = /** @type {function(string, RequestInit):!Promise} */ (fetchPolyfill);
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be on win, not win.prototype.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

export function install(win) {
if (!win.fetch) {
win.prototype.fetch = /** @type {function(string, RequestInit):!Promise} */ (fetchPolyfill);
win.Response = Response;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Object.defineProperty

* serialized response returned by the viewer.
* @typedef {{
* status: number,
* statusText: string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't look like this is used.

Copy link
Member Author

Choose a reason for hiding this comment

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

added to the FetchResponse

* @param {!XMLHttpRequest|!XDomainRequest|!XMLHttpRequestDef} xhr
*/
constructor(xhr) {
/** @private @const {!XMLHttpRequest|!XDomainRequest|!XMLHttpRequestDef} */
Copy link
Contributor

Choose a reason for hiding this comment

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

XDomainRequest again.

Copy link
Member Author

Choose a reason for hiding this comment

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

removed

*/
class FetchResponseHeaders {
/**
* @param {!XMLHttpRequest|!XDomainRequest|!XMLHttpRequestDef} xhr
Copy link
Contributor

Choose a reason for hiding this comment

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

XDomainRequest again.

Copy link
Member Author

Choose a reason for hiding this comment

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

woops, removed

* @return {string}
*/
getResponseHeader(name) {
return lowercasedHeaders[String(name).toLowerCase()] || null;
Copy link
Contributor

Choose a reason for hiding this comment

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

hasOwnProperty, since the value could be an empty string.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

data.statusText = String(init.statusText);
}

return new FetchResponse(/** @type {XMLHttpRequestDef} */(data));
Copy link
Contributor

Choose a reason for hiding this comment

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

Not really

Can you explain?

export function install(win) {
if (!win.fetch) {
win.fetch = /** @type {function(string, RequestInit):!Promise} */ (fetchPolyfill);
win.Response = Response;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Object.defineProperty.

Copy link
Member Author

Choose a reason for hiding this comment

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

is there a specific thing we want to configure with Object.defineProperty?

Copy link
Contributor

Choose a reason for hiding this comment

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

Object.getOwnPropertyDescriptor(window, 'Response')
{value: ƒ, writable: true, enumerable: false, configurable: true}
Object.getOwnPropertyDescriptor(window, 'fetch')
{value: ƒ, writable: true, enumerable: true, configurable: true}

Enumerability, in particular, has been a source of countless polyfill bugs.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Member Author

Choose a reason for hiding this comment

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

I am curious, can u list one such example?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha! I have read that before, but now I got the complete understanding. Thanks

/** @const {number} */
this.status = this.xhr_.status;

this.statusText = this.xhr_.statusText;
Copy link
Contributor

Choose a reason for hiding this comment

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

Needs annotations. But why add it?

Copy link
Member Author

Choose a reason for hiding this comment

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

It'll align better with the standard definition, without much effort.

* @return {string}
*/
getResponseHeader(name) {
return hasOwn(lowercasedHeaders, name) ?
Copy link
Contributor

Choose a reason for hiding this comment

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

name needs to be lowercased here, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

/**
* Returns instance of Response.
* @param {?ResponseBodyInit=} body
* @param {!ResponseInit=} init
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are ResponseInit and ResponseBodyInit defined?

Copy link
Member Author

Choose a reason for hiding this comment

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

They are native to closure compiler.

},
}, init);

init.status = init.status || 200;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this necessary? It also overrides a status of 0.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed it for only undefined. because closure's types say it can be a number or undefined

String(init.headers[key]);
}
}
if (init.status) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We already did this above?

Copy link
Member Author

Choose a reason for hiding this comment

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

removed

Object.defineProperty(win, 'fetch', {
value: fetch,
writable: true,
enumerable: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

fetch is enumerable. Also configurable.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Object.defineProperty(win, 'Response', {
value: Response,
writable: true,
enumerable: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

configurable: true

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Copy link
Member Author

@prateekbh prateekbh left a comment

Choose a reason for hiding this comment

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

Thanks the exhaustive review... 👍

/** @const {number} */
this.status = this.xhr_.status;

this.statusText = this.xhr_.statusText;
Copy link
Member Author

Choose a reason for hiding this comment

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

It'll align better with the standard definition, without much effort.

/**
* Returns instance of Response.
* @param {?ResponseBodyInit=} body
* @param {!ResponseInit=} 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.

They are native to closure compiler.

* @return {string}
*/
getResponseHeader(name) {
return hasOwn(lowercasedHeaders, name) ?
Copy link
Member Author

Choose a reason for hiding this comment

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

done

},
}, init);

init.status = init.status || 200;
Copy link
Member Author

Choose a reason for hiding this comment

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

fixed it for only undefined. because closure's types say it can be a number or undefined

String(init.headers[key]);
}
}
if (init.status) {
Copy link
Member Author

Choose a reason for hiding this comment

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

removed

Object.defineProperty(win, 'fetch', {
value: fetch,
writable: true,
enumerable: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

done

Object.defineProperty(win, 'Response', {
value: Response,
writable: true,
enumerable: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

done

@prateekbh prateekbh merged commit 43f9a86 into ampproject:master Aug 20, 2018
@prateekbh prateekbh deleted the fet-polyfill branch August 20, 2018 19:29
Enriqe pushed a commit to Enriqe/amphtml that referenced this pull request Nov 28, 2018
* reverting skip

* adding fetch polyfill

* WIP polyfill tests

* WIP fetch-polyfill tests

* passing tests

* adding more test

* ading more tests

* ading more tests

* Update dep-check-config.js

* Update polyfills.js

* resolving comments

* removing extra test

* response as a class

* resolving comments

* fixing type related issues

* renaming file to avoid test conflict

* adding copyright
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.

None yet

3 participants