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

Implemente clone for FetchResponse #1054

Merged
merged 3 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions js/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class FetchResponse implements domTypes.Response {
readonly trailer: Promise<domTypes.Headers>;
//private bodyChunks: Uint8Array[] = [];
private first = true;
private bodyData: ArrayBuffer;
private bodyWaiter: Resolvable<ArrayBuffer>;

constructor(
Expand All @@ -138,6 +139,7 @@ class FetchResponse implements domTypes.Response {
this.bodyWaiter = createResolvable();
this.trailer = createResolvable();
this.headers = new DenoHeaders(headersList);
this.bodyData = body_;
setTimeout(() => {
this.bodyWaiter.resolve(body_);
}, 0);
Expand Down Expand Up @@ -175,8 +177,19 @@ class FetchResponse implements domTypes.Response {
}

clone(): domTypes.Response {
notImplemented();
return {} as domTypes.Response;
if (this.bodyUsed) {
throw new TypeError(
"Failed to execute 'clone' on 'Response': Response body is already used"
);
}

const iterators = this.headers.entries();
const headersList: Array<[string, string]> = [];
for (const header of iterators) {
headersList.push(header);
}

return new FetchResponse(this.status, this.bodyData.slice(0), headersList);
}

onHeader?: (res: FetchResponse) => void;
Expand Down
13 changes: 13 additions & 0 deletions js/fetch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ testPerm({ net: true }, async function fetchBlob() {
assertEqual(blob.size, Number(headers.get("Content-Length")));
});

testPerm({ net: true }, async function responseClone() {
const response = await fetch("https://localhost:4545/package.json");
const response1 = response.clone();
assert(response !== response1);
assertEqual(response.status, response1.status);
assertEqual(response.statusText, response1.statusText);
const ab = await response.arrayBuffer();
const ab1 = await response1.arrayBuffer();
for (let i = 0; i < ab.byteLength; i++) {
assertEqual(ab[i], ab1[i]);
}
});

// Logic heavily copied from web-platform-tests, make
// sure pass mostly header basic test
/* tslint:disable-next-line:max-line-length */
Expand Down