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

fix(cli/web/fetch): Make Response constructor standard #5787

Merged
merged 11 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 7 additions & 14 deletions cli/js/lib.deno.shared_globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,12 @@ declare const Request: {
new (input: RequestInfo, init?: RequestInit): Request;
};

interface ResponseInit {
headers?: HeadersInit;
status?: number;
statusText?: string;
}

type ResponseType =
| "basic"
| "cors"
Expand All @@ -945,20 +951,7 @@ interface Response extends Body {

declare const Response: {
prototype: Response;

// TODO(#4667) Response constructor is non-standard.
// new(body?: BodyInit | null, init?: ResponseInit): Response;
new (
url: string,
status: number,
statusText: string,
headersList: Array<[string, string]>,
rid: number,
redirected_: boolean,
type_?: null | ResponseType,
body_?: null | Body
): Response;

new (body?: BodyInit | null, init?: ResponseInit): Response;
error(): Response;
redirect(url: string, status?: number): Response;
};
Expand Down
21 changes: 10 additions & 11 deletions cli/js/web/body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ import { ReadableStreamImpl } from "./streams/readable_stream.ts";
const { TextEncoder, TextDecoder } = encoding;
const DenoBlob = blob.DenoBlob;

export type BodySource =
| Blob
| BufferSource
| FormData
| URLSearchParams
| ReadableStream
| string;

function validateBodyType(owner: Body, bodySource: BodySource): boolean {
function validateBodyType(owner: Body, bodySource: BodyInit | null): boolean {
if (
bodySource instanceof Int8Array ||
bodySource instanceof Int16Array ||
Expand Down Expand Up @@ -75,6 +67,8 @@ function bufferFromStream(stream: ReadableStreamReader): Promise<ArrayBuffer> {
parts.push(encoder.encode(value));
} else if (value instanceof ArrayBuffer) {
parts.push(new Uint8Array(value));
} else if (value instanceof Uint8Array) {
parts.push(value);
} else if (!value) {
// noop for undefined
} else {
Expand Down Expand Up @@ -113,7 +107,10 @@ export const BodyUsedError =
export class Body implements domTypes.Body {
protected _stream: ReadableStreamImpl<string | ArrayBuffer> | null;

constructor(protected _bodySource: BodySource, readonly contentType: string) {
constructor(
protected _bodySource: BodyInit | null,
readonly contentType: string
) {
validateBodyType(this, _bodySource);
this._bodySource = _bodySource;
this.contentType = contentType;
Expand Down Expand Up @@ -149,7 +146,9 @@ export class Body implements domTypes.Body {
}

public async blob(): Promise<Blob> {
return new DenoBlob([await this.arrayBuffer()]);
return new DenoBlob([await this.arrayBuffer()], {
type: this.contentType,
});
}

// ref: https://fetch.spec.whatwg.org/#body-mixin
Expand Down
1 change: 0 additions & 1 deletion cli/js/web/dom_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ export interface Response extends Body {
readonly redirected: boolean;
readonly status: number;
readonly statusText: string;
readonly trailer: Promise<Headers>;
readonly type: ResponseType;
readonly url: string;
clone(): Response;
Expand Down
Loading