Skip to content

Commit

Permalink
Make Response constructor standard
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosc90 committed May 23, 2020
1 parent 491feb8 commit df08a06
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 412 deletions.
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

0 comments on commit df08a06

Please sign in to comment.