Skip to content

Commit

Permalink
dedup various type definitions (#4741)
Browse files Browse the repository at this point in the history
FormData FilePropertyBag DomFile BlobPropertyBag RequestCache 
RequestCredentials RequestDestination RequestMode RequestRedirect 
ResponseType
  • Loading branch information
ry authored Apr 14, 2020
1 parent 360c05f commit ff60b31
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 159 deletions.
2 changes: 1 addition & 1 deletion cli/js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const windowOrWorkerGlobalScopeProperties = {
URL: nonEnumerable(url.URLImpl),
URLSearchParams: nonEnumerable(urlSearchParams.URLSearchParamsImpl),
Headers: nonEnumerable(headers.HeadersImpl),
FormData: nonEnumerable(formData.FormData),
FormData: nonEnumerable(formData.FormDataImpl),
TextEncoder: nonEnumerable(textEncoding.TextEncoder),
TextDecoder: nonEnumerable(textEncoding.TextDecoder),
ReadableStream: nonEnumerable(streams.ReadableStream),
Expand Down
23 changes: 14 additions & 9 deletions cli/js/tests/form_data_test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts";

unitTest(function formDataHasCorrectNameProp(): void {
import {
unitTest,
assert,
assertEquals,
assertStrContains,
} from "./test_util.ts";

unitTest({ ignore: true }, function formDataHasCorrectNameProp(): void {
assertEquals(FormData.name, "FormData");
});

Expand Down Expand Up @@ -141,9 +146,9 @@ unitTest(function formDataParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`FormData.${method} requires at least 1 argument, but only 0 present`
`${method} requires at least 1 argument, but only 0 present`
);
});

Expand All @@ -165,9 +170,9 @@ unitTest(function formDataParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`FormData.${method} requires at least 2 arguments, but only 0 present`
`${method} requires at least 2 arguments, but only 0 present`
);

hasThrown = 0;
Expand All @@ -185,9 +190,9 @@ unitTest(function formDataParamsArgumentsCheck(): void {
}
}
assertEquals(hasThrown, 2);
assertEquals(
assertStrContains(
errMsg,
`FormData.${method} requires at least 2 arguments, but only 1 present`
`${method} requires at least 2 arguments, but only 1 present`
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions cli/js/web/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function toUint8Arrays(

function processBlobParts(
blobParts: BlobPart[],
options: domTypes.BlobPropertyBag
options: BlobPropertyBag
): Uint8Array {
const normalizeLineEndingsToNative = options.ending === "native";
// ArrayBuffer.transfer is not yet implemented in V8, so we just have to
Expand Down Expand Up @@ -171,7 +171,7 @@ export class DenoBlob implements Blob {
readonly size: number = 0;
readonly type: string = "";

constructor(blobParts?: BlobPart[], options?: domTypes.BlobPropertyBag) {
constructor(blobParts?: BlobPart[], options?: BlobPropertyBag) {
if (arguments.length === 0) {
this[bytesSymbol] = new Uint8Array();
return;
Expand Down
6 changes: 2 additions & 4 deletions cli/js/web/body.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import * as formData from "./form_data.ts";
import * as blob from "./blob.ts";
import * as encoding from "./text_encoding.ts";
import * as domTypes from "./dom_types.d.ts";
import { ReadableStream } from "./streams/mod.ts";

// only namespace imports work for now, plucking out what we need
const { FormData } = formData;
const { TextEncoder, TextDecoder } = encoding;
const DenoBlob = blob.DenoBlob;

Expand All @@ -19,7 +17,7 @@ interface ReadableStreamController {
export type BodySource =
| Blob
| BufferSource
| domTypes.FormData
| FormData
| URLSearchParams
| domTypes.ReadableStream
| string;
Expand Down Expand Up @@ -162,7 +160,7 @@ export class Body implements domTypes.Body {
}

// ref: https://fetch.spec.whatwg.org/#body-mixin
public async formData(): Promise<domTypes.FormData> {
public async formData(): Promise<FormData> {
const formData = new FormData();
const enc = new TextEncoder();
if (hasHeaderValueOf(this.contentType, "multipart/form-data")) {
Expand Down
5 changes: 2 additions & 3 deletions cli/js/web/dom_file.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.d.ts";
import * as blob from "./blob.ts";

export class DomFileImpl extends blob.DenoBlob implements domTypes.DomFile {
export class DomFileImpl extends blob.DenoBlob implements File {
lastModified: number;
name: string;

constructor(
fileBits: BlobPart[],
fileName: string,
options?: domTypes.FilePropertyBag
options?: FilePropertyBag
) {
const { lastModified = Date.now(), ...blobPropertyBag } = options ?? {};
super(fileBits, blobPropertyBag);
Expand Down
129 changes: 0 additions & 129 deletions cli/js/web/dom_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ type BodyInit =

export type RequestInfo = Request | string;

export type FormDataEntryValue = DomFile | string;

export type EndingType = "transparent" | "native";

export interface BlobPropertyBag {
type?: string;
ending?: EndingType;
}

export interface ProgressEventInit extends EventInit {
lengthComputable?: boolean;
loaded?: number;
Expand Down Expand Up @@ -260,33 +251,6 @@ interface NodeListOf<TNode extends Node> extends NodeList {
values(): IterableIterator<TNode>;
}

export interface DomFile extends Blob {
readonly lastModified: number;
readonly name: string;
}

export interface DomFileConstructor {
new (bits: BlobPart[], filename: string, options?: FilePropertyBag): DomFile;
prototype: DomFile;
}

export interface FilePropertyBag extends BlobPropertyBag {
lastModified?: number;
}

export class FormData {
append(name: string, value: string | Blob, fileName?: string): void;
delete(name: string): void;
get(name: string): FormDataEntryValue | null;
getAll(name: string): FormDataEntryValue[];
has(name: string): boolean;
set(name: string, value: string | Blob, fileName?: string): void;
[Symbol.iterator](): IterableIterator<[string, FormDataEntryValue]>;
entries(): IterableIterator<[string, FormDataEntryValue]>;
keys(): IterableIterator<string>;
values(): IterableIterator<FormDataEntryValue>;
}

export interface Body {
readonly body: ReadableStream<Uint8Array> | null;
readonly bodyUsed: boolean;
Expand Down Expand Up @@ -442,62 +406,6 @@ export interface ReadableStreamBYOBRequest {
respond(bytesWritten: number): void;
respondWithNewView(view: ArrayBufferView): void;
}
/* TODO reenable these interfaces. These are needed to enable WritableStreams in js/streams/
export interface WritableStream<W = any> {
readonly locked: boolean;
abort(reason?: any): Promise<void>;
getWriter(): WritableStreamDefaultWriter<W>;
}
TODO reenable these interfaces. These are needed to enable WritableStreams in js/streams/
export interface UnderlyingSink<W = any> {
abort?: WritableStreamErrorCallback;
close?: WritableStreamDefaultControllerCloseCallback;
start?: WritableStreamDefaultControllerStartCallback;
type?: undefined;
write?: WritableStreamDefaultControllerWriteCallback<W>;
}
export interface PipeOptions {
preventAbort?: boolean;
preventCancel?: boolean;
preventClose?: boolean;
signal?: AbortSignal;
}
export interface WritableStreamDefaultWriter<W = any> {
readonly closed: Promise<void>;
readonly desiredSize: number | null;
readonly ready: Promise<void>;
abort(reason?: any): Promise<void>;
close(): Promise<void>;
releaseLock(): void;
write(chunk: W): Promise<void>;
}
export interface WritableStreamErrorCallback {
(reason: any): void | PromiseLike<void>;
}
export interface WritableStreamDefaultControllerCloseCallback {
(): void | PromiseLike<void>;
}
export interface WritableStreamDefaultControllerStartCallback {
(controller: WritableStreamDefaultController): void | PromiseLike<void>;
}
export interface WritableStreamDefaultControllerWriteCallback<W> {
(chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<
void
>;
}
export interface WritableStreamDefaultController {
error(error?: any): void;
}
*/

export interface QueuingStrategy<T = any> {
highWaterMark?: number;
Expand All @@ -508,43 +416,6 @@ export interface QueuingStrategySizeCallback<T = any> {
(chunk: T): number;
}

type RequestCache =
| "default"
| "no-store"
| "reload"
| "no-cache"
| "force-cache"
| "only-if-cached";
type RequestCredentials = "omit" | "same-origin" | "include";
type RequestDestination =
| ""
| "audio"
| "audioworklet"
| "document"
| "embed"
| "font"
| "image"
| "manifest"
| "object"
| "paintworklet"
| "report"
| "script"
| "sharedworker"
| "style"
| "track"
| "video"
| "worker"
| "xslt";
type RequestMode = "navigate" | "same-origin" | "no-cors" | "cors";
type RequestRedirect = "follow" | "nofollow" | "error" | "manual";
export type ResponseType =
| "basic"
| "cors"
| "default"
| "error"
| "opaque"
| "opaqueredirect";

export interface RequestInit {
body?: BodyInit | null;
cache?: RequestCache;
Expand Down
9 changes: 4 additions & 5 deletions cli/js/web/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as io from "../io.ts";
import { read } from "../ops/io.ts";
import { close } from "../ops/resources.ts";
import { Buffer } from "../buffer.ts";
import { FormData } from "./form_data.ts";
import { fetch as opFetch, FetchResponse } from "../ops/fetch.ts";
import { DomFileImpl } from "./dom_file.ts";

Expand Down Expand Up @@ -85,7 +84,7 @@ class Body
}

// ref: https://fetch.spec.whatwg.org/#body-mixin
async formData(): Promise<domTypes.FormData> {
async formData(): Promise<FormData> {
const formData = new FormData();
const enc = new TextEncoder();
if (hasHeaderValueOf(this.contentType, "multipart/form-data")) {
Expand Down Expand Up @@ -274,7 +273,7 @@ class Body
}

export class Response implements domTypes.Response {
readonly type: domTypes.ResponseType;
readonly type: ResponseType;
readonly redirected: boolean;
headers: Headers;
readonly trailer: Promise<Headers>;
Expand All @@ -287,7 +286,7 @@ export class Response implements domTypes.Response {
headersList: Array<[string, string]>,
rid: number,
redirected_: boolean,
readonly type_: null | domTypes.ResponseType = "default",
readonly type_: null | ResponseType = "default",
body_: null | Body = null
) {
this.trailer = createResolvable();
Expand Down Expand Up @@ -388,7 +387,7 @@ export class Response implements domTypes.Response {
return this.body.blob();
}

formData(): Promise<domTypes.FormData> {
formData(): Promise<FormData> {
if (this.#bodyViewable() || this.body == null) {
return Promise.reject(new Error("Response body is null"));
}
Expand Down
11 changes: 5 additions & 6 deletions cli/js/web/form_data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import * as domTypes from "./dom_types.d.ts";
import * as blob from "./blob.ts";
import * as domFile from "./dom_file.ts";
import { DomIterableMixin } from "./dom_iterable.ts";
Expand All @@ -8,7 +7,7 @@ import { requiredArguments } from "./util.ts";
const dataSymbol = Symbol("data");

class FormDataBase {
[dataSymbol]: Array<[string, domTypes.FormDataEntryValue]> = [];
[dataSymbol]: Array<[string, FormDataEntryValue]> = [];

append(name: string, value: string): void;
append(name: string, value: domFile.DomFileImpl): void;
Expand Down Expand Up @@ -45,7 +44,7 @@ class FormDataBase {
}
}

getAll(name: string): domTypes.FormDataEntryValue[] {
getAll(name: string): FormDataEntryValue[] {
requiredArguments("FormData.getAll", arguments.length, 1);
name = String(name);
const values = [];
Expand All @@ -58,7 +57,7 @@ class FormDataBase {
return values;
}

get(name: string): domTypes.FormDataEntryValue | null {
get(name: string): FormDataEntryValue | null {
requiredArguments("FormData.get", arguments.length, 1);
name = String(name);
for (const entry of this[dataSymbol]) {
Expand Down Expand Up @@ -133,8 +132,8 @@ class FormDataBase {
}
}

export class FormData extends DomIterableMixin<
export class FormDataImpl extends DomIterableMixin<
string,
domTypes.FormDataEntryValue,
FormDataEntryValue,
typeof FormDataBase
>(FormDataBase, dataSymbol) {}

0 comments on commit ff60b31

Please sign in to comment.