0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-04 01:44:26 -05:00

dedup type declarations (#4718)

Blob, BlobPart, BufferSource, ReferrerPolicy, BlobPart, AbortSignal, AbortSignalEventMap
This commit is contained in:
Ryan Dahl 2020-04-11 16:25:31 -04:00 committed by GitHub
parent 0641ad0d9b
commit da28fc1e7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 16 additions and 81 deletions

View file

@ -63,7 +63,7 @@ function collectSequenceNotCRLF(
} }
function toUint8Arrays( function toUint8Arrays(
blobParts: domTypes.BlobPart[], blobParts: BlobPart[],
doNormalizeLineEndingsToNative: boolean doNormalizeLineEndingsToNative: boolean
): Uint8Array[] { ): Uint8Array[] {
const ret: Uint8Array[] = []; const ret: Uint8Array[] = [];
@ -102,7 +102,7 @@ function toUint8Arrays(
} }
function processBlobParts( function processBlobParts(
blobParts: domTypes.BlobPart[], blobParts: BlobPart[],
options: domTypes.BlobPropertyBag options: domTypes.BlobPropertyBag
): Uint8Array { ): Uint8Array {
const normalizeLineEndingsToNative = options.ending === "native"; const normalizeLineEndingsToNative = options.ending === "native";
@ -164,17 +164,14 @@ async function readBytes(
// A WeakMap holding blob to byte array mapping. // A WeakMap holding blob to byte array mapping.
// Ensures it does not impact garbage collection. // Ensures it does not impact garbage collection.
export const blobBytesWeakMap = new WeakMap<domTypes.Blob, Uint8Array>(); export const blobBytesWeakMap = new WeakMap<Blob, Uint8Array>();
export class DenoBlob implements domTypes.Blob { export class DenoBlob implements Blob {
[bytesSymbol]: Uint8Array; [bytesSymbol]: Uint8Array;
readonly size: number = 0; readonly size: number = 0;
readonly type: string = ""; readonly type: string = "";
constructor( constructor(blobParts?: BlobPart[], options?: domTypes.BlobPropertyBag) {
blobParts?: domTypes.BlobPart[],
options?: domTypes.BlobPropertyBag
) {
if (arguments.length === 0) { if (arguments.length === 0) {
this[bytesSymbol] = new Uint8Array(); this[bytesSymbol] = new Uint8Array();
return; return;

View file

@ -10,7 +10,6 @@ const { Headers } = headers;
// only namespace imports work for now, plucking out what we need // only namespace imports work for now, plucking out what we need
const { FormData } = formData; const { FormData } = formData;
const { TextEncoder, TextDecoder } = encoding; const { TextEncoder, TextDecoder } = encoding;
const Blob = blob.DenoBlob;
const DenoBlob = blob.DenoBlob; const DenoBlob = blob.DenoBlob;
type ReadableStreamReader = domTypes.ReadableStreamReader; type ReadableStreamReader = domTypes.ReadableStreamReader;
@ -21,8 +20,8 @@ interface ReadableStreamController {
} }
export type BodySource = export type BodySource =
| domTypes.Blob | Blob
| domTypes.BufferSource | BufferSource
| domTypes.FormData | domTypes.FormData
| domTypes.URLSearchParams | domTypes.URLSearchParams
| domTypes.ReadableStream | domTypes.ReadableStream
@ -161,8 +160,8 @@ export class Body implements domTypes.Body {
return false; return false;
} }
public async blob(): Promise<domTypes.Blob> { public async blob(): Promise<Blob> {
return new Blob([await this.arrayBuffer()]); return new DenoBlob([await this.arrayBuffer()]);
} }
// ref: https://fetch.spec.whatwg.org/#body-mixin // ref: https://fetch.spec.whatwg.org/#body-mixin

View file

@ -7,7 +7,7 @@ export class DomFileImpl extends blob.DenoBlob implements domTypes.DomFile {
name: string; name: string;
constructor( constructor(
fileBits: domTypes.BlobPart[], fileBits: BlobPart[],
fileName: string, fileName: string,
options?: domTypes.FilePropertyBag options?: domTypes.FilePropertyBag
) { ) {

View file

@ -17,8 +17,6 @@ and limitations under the License.
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
export type BufferSource = ArrayBufferView | ArrayBuffer;
export type HeadersInit = export type HeadersInit =
| Headers | Headers
| Array<[string, string]> | Array<[string, string]>
@ -34,16 +32,6 @@ type BodyInit =
export type RequestInfo = Request | string; export type RequestInfo = Request | string;
type ReferrerPolicy =
| ""
| "no-referrer"
| "no-referrer-when-downgrade"
| "origin-only"
| "origin-when-cross-origin"
| "unsafe-url";
export type BlobPart = BufferSource | Blob | string;
export type FormDataEntryValue = DomFile | string; export type FormDataEntryValue = DomFile | string;
export type EndingType = "transparent" | "native"; export type EndingType = "transparent" | "native";
@ -53,10 +41,6 @@ export interface BlobPropertyBag {
ending?: EndingType; ending?: EndingType;
} }
interface AbortSignalEventMap {
abort: ProgressEvent;
}
export interface ProgressEventInit extends EventInit { export interface ProgressEventInit extends EventInit {
lengthComputable?: boolean; lengthComputable?: boolean;
loaded?: number; loaded?: number;
@ -318,37 +302,6 @@ export interface FilePropertyBag extends BlobPropertyBag {
lastModified?: number; lastModified?: number;
} }
interface ProgressEvent extends Event {
readonly lengthComputable: boolean;
readonly loaded: number;
readonly total: number;
}
export interface AbortSignal extends EventTarget {
readonly aborted: boolean;
onabort: ((this: AbortSignal, ev: ProgressEvent) => any) | null;
addEventListener<K extends keyof AbortSignalEventMap>(
type: K,
listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any,
options?: boolean | AddEventListenerOptions
): void;
addEventListener(
type: string,
listener: EventListener,
options?: boolean | AddEventListenerOptions
): void;
removeEventListener<K extends keyof AbortSignalEventMap>(
type: K,
listener: (this: AbortSignal, ev: AbortSignalEventMap[K]) => any,
options?: boolean | EventListenerOptions
): void;
removeEventListener(
type: string,
listener: EventListener,
options?: boolean | EventListenerOptions
): void;
}
export class FormData { export class FormData {
append(name: string, value: string | Blob, fileName?: string): void; append(name: string, value: string | Blob, fileName?: string): void;
delete(name: string): void; delete(name: string): void;
@ -362,15 +315,6 @@ export class FormData {
values(): IterableIterator<FormDataEntryValue>; values(): IterableIterator<FormDataEntryValue>;
} }
export interface Blob {
readonly size: number;
readonly type: string;
slice(start?: number, end?: number, contentType?: string): Blob;
stream(): ReadableStream;
text(): Promise<string>;
arrayBuffer(): Promise<ArrayBuffer>;
}
export interface Body { export interface Body {
readonly body: ReadableStream<Uint8Array> | null; readonly body: ReadableStream<Uint8Array> | null;
readonly bodyUsed: boolean; readonly bodyUsed: boolean;

View file

@ -80,7 +80,7 @@ class Body
return this.#bodyPromise; return this.#bodyPromise;
} }
async blob(): Promise<domTypes.Blob> { async blob(): Promise<Blob> {
const arrayBuffer = await this.arrayBuffer(); const arrayBuffer = await this.arrayBuffer();
return new DenoBlob([arrayBuffer], { return new DenoBlob([arrayBuffer], {
type: this.contentType, type: this.contentType,
@ -384,7 +384,7 @@ export class Response implements domTypes.Response {
return this.body.arrayBuffer(); return this.body.arrayBuffer();
} }
blob(): Promise<domTypes.Blob> { blob(): Promise<Blob> {
if (this.#bodyViewable() || this.body == null) { if (this.#bodyViewable() || this.body == null) {
return Promise.reject(new Error("Response body is null")); return Promise.reject(new Error("Response body is null"));
} }

View file

@ -4,19 +4,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
// TODO don't disable this warning // TODO don't disable this warning
import { AbortSignal, QueuingStrategySizeCallback } from "../dom_types.d.ts"; import { QueuingStrategySizeCallback } from "../dom_types.d.ts";
// common stream fields // common stream fields
export const state_ = Symbol("state_"); export const state_ = Symbol("state_");
export const storedError_ = Symbol("storedError_"); export const storedError_ = Symbol("storedError_");
// ---------
export type ErrorResult = any; export type ErrorResult = any;
// ---------
export function isInteger(value: number): boolean { export function isInteger(value: number): boolean {
if (!isFinite(value)) { if (!isFinite(value)) {
// covers NaN, +Infinity and -Infinity // covers NaN, +Infinity and -Infinity

View file

@ -25,7 +25,6 @@
import * as base64 from "./base64.ts"; import * as base64 from "./base64.ts";
import { decodeUtf8 } from "./decode_utf8.ts"; import { decodeUtf8 } from "./decode_utf8.ts";
import * as domTypes from "./dom_types.d.ts";
import { core } from "../core.ts"; import { core } from "../core.ts";
const CONTINUE = null; const CONTINUE = null;
@ -449,7 +448,7 @@ export class TextDecoder {
} }
decode( decode(
input?: domTypes.BufferSource, input?: BufferSource,
options: TextDecodeOptions = { stream: false } options: TextDecodeOptions = { stream: false }
): string { ): string {
if (options.stream) { if (options.stream) {

View file

@ -74,7 +74,7 @@ function generateUUID(): string {
} }
// Keep it outside of URL to avoid any attempts of access. // Keep it outside of URL to avoid any attempts of access.
export const blobURLMap = new Map<string, domTypes.Blob>(); export const blobURLMap = new Map<string, Blob>();
function isAbsolutePath(path: string): boolean { function isAbsolutePath(path: string): boolean {
return path.startsWith("/"); return path.startsWith("/");
@ -373,7 +373,7 @@ export class URL implements domTypes.URL {
} }
// TODO(kevinkassimo): implement MediaSource version in the future. // TODO(kevinkassimo): implement MediaSource version in the future.
static createObjectURL(b: domTypes.Blob): string { static createObjectURL(b: Blob): string {
const origin = globalThis.location.origin || "http://deno-opaque-origin"; const origin = globalThis.location.origin || "http://deno-opaque-origin";
const key = `blob:${origin}/${generateUUID()}`; const key = `blob:${origin}/${generateUUID()}`;
blobURLMap.set(key, b); blobURLMap.set(key, b);