Conversation
|
This looks well on its way. I realized the instructions in the README for running tests was out of date, so I just pushed a change for that, but it sounds like you might have already figured out how to run them. |
27b6a22 to
3288888
Compare
|
Actually, I think the "Has a url property" and "Has a redirected property" tests might be So I think this is ready for review. A couple of things that I removed:
The motivation for these changes was to gently push towards web standards and maybe a version that could work in the browser ( The I was wrong, |
|
Thanks for your great work on this. I'd like to merge it after we review. It would be a major version increment so we would need an upgrade guide also but I could write that. It probably wouldn't be comprehensive since it will be impossible to know every difference between node-fetch and native fetch, but the changes to tests will be a good foundation for knowing the major differences. Native fetch Response does seem to have |
|
I'm not sure where to make the change for the |
I pushed a change to your branch for it. The properties weren't writable like I was hoping (they were getters) so I used All tests are passing now! I will try to take a closer look Sunday or Monday and do an upgrade guide. I might have some more comments but I think this is likely at least 95% of the way there. |
|
I was trying something with: that had worked for
Thanks! This was fun (definitely more challenging than I had originally thought)! This will be my first real PR to an open-source project 🎉 |
|
You may also be able to replace |
|
I can also commit my |
| } | ||
|
|
||
| public override clone(): Response { | ||
| return new NFCResponse(super.body as ReadableStream, this.metaData, this.ejectFromCache, this.returnedFromCache, this.isCacheMiss) |
There was a problem hiding this comment.
This won't quite work because it doesn't clone the body stream, so if you clone a response and then try to read the bodies of both the original and the clone, it will error. I just pushed up a test for that, currently failing.
However the more I think about it, I do think it's worthwhile to try to find a solution that returns an NFCResponse here from clone(). This approach appears to work:
_url = '';
_redirected: boolean = false
override get url() {
return this._url;
}
override get redirected() {
return this._redirected;
}
constructor(
bodyStream: ReadableStream,
public metaData: Omit<ResponseInit, 'headers'> & {
url: string;
redirected: boolean;
counter: number;
headers: Record<string, string[]>;
},
public ejectFromCache: () => Promise<unknown>,
public returnedFromCache: boolean,
public isCacheMiss = false,
) {
super(
bodyStream,
metaData as any
);
this._url = metaData.url;
this._redirected = metaData.redirected
}
public override clone(): NFCResponse {
const superClone = super.clone() as NFCResponse;
Object.setPrototypeOf(superClone, NFCResponse.prototype);
superClone._url = this.url;
superClone._redirected = this.redirected;
superClone.ejectFromCache = this.ejectFromCache;
superClone.returnedFromCache = this.returnedFromCache;
superClone.isCacheMiss = this.isCacheMiss;
return superClone;
}It would also be possible to replace the createNFCResponseClass() function with just a normal class export. That hackery was only needed to support CommonJS using dynamic imports from node-fetch.
There was a problem hiding this comment.
This may be preferable:
public override clone(): NFCResponse {
const clonedBody = super.clone();
return new NFCResponse(
clonedBody.body as ReadableStream,
{
url: this.url,
redirected: this.redirected,
status: this.status,
statusText: this.statusText,
headers: NFCResponse.serializeHeaders(this),
counter: this.metaData.counter,
},
this.ejectFromCache,
this.returnedFromCache,
this.isCacheMiss,
);
}Due to the caveats about changing an object's prototype.
There was a problem hiding this comment.
Definitely reads better than Object.defineProperty().
When would this be needed? It seems to be building okay without that, when I run |
|
I think in the output you'll find Although, now that I think about it, you can just replace all instances of |
Ah maybe this module can support browser someday, but for now it's not necessary. |
This may eventually address #31.