mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
dedup type declarations (#4718)
Blob, BlobPart, BufferSource, ReferrerPolicy, BlobPart, AbortSignal, AbortSignalEventMap
This commit is contained in:
parent
0641ad0d9b
commit
da28fc1e7b
8 changed files with 16 additions and 81 deletions
|
@ -63,7 +63,7 @@ function collectSequenceNotCRLF(
|
|||
}
|
||||
|
||||
function toUint8Arrays(
|
||||
blobParts: domTypes.BlobPart[],
|
||||
blobParts: BlobPart[],
|
||||
doNormalizeLineEndingsToNative: boolean
|
||||
): Uint8Array[] {
|
||||
const ret: Uint8Array[] = [];
|
||||
|
@ -102,7 +102,7 @@ function toUint8Arrays(
|
|||
}
|
||||
|
||||
function processBlobParts(
|
||||
blobParts: domTypes.BlobPart[],
|
||||
blobParts: BlobPart[],
|
||||
options: domTypes.BlobPropertyBag
|
||||
): Uint8Array {
|
||||
const normalizeLineEndingsToNative = options.ending === "native";
|
||||
|
@ -164,17 +164,14 @@ async function readBytes(
|
|||
|
||||
// A WeakMap holding blob to byte array mapping.
|
||||
// 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;
|
||||
readonly size: number = 0;
|
||||
readonly type: string = "";
|
||||
|
||||
constructor(
|
||||
blobParts?: domTypes.BlobPart[],
|
||||
options?: domTypes.BlobPropertyBag
|
||||
) {
|
||||
constructor(blobParts?: BlobPart[], options?: domTypes.BlobPropertyBag) {
|
||||
if (arguments.length === 0) {
|
||||
this[bytesSymbol] = new Uint8Array();
|
||||
return;
|
||||
|
|
|
@ -10,7 +10,6 @@ const { Headers } = headers;
|
|||
// only namespace imports work for now, plucking out what we need
|
||||
const { FormData } = formData;
|
||||
const { TextEncoder, TextDecoder } = encoding;
|
||||
const Blob = blob.DenoBlob;
|
||||
const DenoBlob = blob.DenoBlob;
|
||||
|
||||
type ReadableStreamReader = domTypes.ReadableStreamReader;
|
||||
|
@ -21,8 +20,8 @@ interface ReadableStreamController {
|
|||
}
|
||||
|
||||
export type BodySource =
|
||||
| domTypes.Blob
|
||||
| domTypes.BufferSource
|
||||
| Blob
|
||||
| BufferSource
|
||||
| domTypes.FormData
|
||||
| domTypes.URLSearchParams
|
||||
| domTypes.ReadableStream
|
||||
|
@ -161,8 +160,8 @@ export class Body implements domTypes.Body {
|
|||
return false;
|
||||
}
|
||||
|
||||
public async blob(): Promise<domTypes.Blob> {
|
||||
return new Blob([await this.arrayBuffer()]);
|
||||
public async blob(): Promise<Blob> {
|
||||
return new DenoBlob([await this.arrayBuffer()]);
|
||||
}
|
||||
|
||||
// ref: https://fetch.spec.whatwg.org/#body-mixin
|
||||
|
|
|
@ -7,7 +7,7 @@ export class DomFileImpl extends blob.DenoBlob implements domTypes.DomFile {
|
|||
name: string;
|
||||
|
||||
constructor(
|
||||
fileBits: domTypes.BlobPart[],
|
||||
fileBits: BlobPart[],
|
||||
fileName: string,
|
||||
options?: domTypes.FilePropertyBag
|
||||
) {
|
||||
|
|
56
cli/js/web/dom_types.d.ts
vendored
56
cli/js/web/dom_types.d.ts
vendored
|
@ -17,8 +17,6 @@ and limitations under the License.
|
|||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
export type BufferSource = ArrayBufferView | ArrayBuffer;
|
||||
|
||||
export type HeadersInit =
|
||||
| Headers
|
||||
| Array<[string, string]>
|
||||
|
@ -34,16 +32,6 @@ type BodyInit =
|
|||
|
||||
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 EndingType = "transparent" | "native";
|
||||
|
@ -53,10 +41,6 @@ export interface BlobPropertyBag {
|
|||
ending?: EndingType;
|
||||
}
|
||||
|
||||
interface AbortSignalEventMap {
|
||||
abort: ProgressEvent;
|
||||
}
|
||||
|
||||
export interface ProgressEventInit extends EventInit {
|
||||
lengthComputable?: boolean;
|
||||
loaded?: number;
|
||||
|
@ -318,37 +302,6 @@ export interface FilePropertyBag extends BlobPropertyBag {
|
|||
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 {
|
||||
append(name: string, value: string | Blob, fileName?: string): void;
|
||||
delete(name: string): void;
|
||||
|
@ -362,15 +315,6 @@ export class FormData {
|
|||
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 {
|
||||
readonly body: ReadableStream<Uint8Array> | null;
|
||||
readonly bodyUsed: boolean;
|
||||
|
|
|
@ -80,7 +80,7 @@ class Body
|
|||
return this.#bodyPromise;
|
||||
}
|
||||
|
||||
async blob(): Promise<domTypes.Blob> {
|
||||
async blob(): Promise<Blob> {
|
||||
const arrayBuffer = await this.arrayBuffer();
|
||||
return new DenoBlob([arrayBuffer], {
|
||||
type: this.contentType,
|
||||
|
@ -384,7 +384,7 @@ export class Response implements domTypes.Response {
|
|||
return this.body.arrayBuffer();
|
||||
}
|
||||
|
||||
blob(): Promise<domTypes.Blob> {
|
||||
blob(): Promise<Blob> {
|
||||
if (this.#bodyViewable() || this.body == null) {
|
||||
return Promise.reject(new Error("Response body is null"));
|
||||
}
|
||||
|
|
|
@ -4,19 +4,15 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
// TODO don't disable this warning
|
||||
|
||||
import { AbortSignal, QueuingStrategySizeCallback } from "../dom_types.d.ts";
|
||||
import { QueuingStrategySizeCallback } from "../dom_types.d.ts";
|
||||
|
||||
// common stream fields
|
||||
|
||||
export const state_ = Symbol("state_");
|
||||
export const storedError_ = Symbol("storedError_");
|
||||
|
||||
// ---------
|
||||
|
||||
export type ErrorResult = any;
|
||||
|
||||
// ---------
|
||||
|
||||
export function isInteger(value: number): boolean {
|
||||
if (!isFinite(value)) {
|
||||
// covers NaN, +Infinity and -Infinity
|
||||
|
|
|
@ -25,7 +25,6 @@
|
|||
|
||||
import * as base64 from "./base64.ts";
|
||||
import { decodeUtf8 } from "./decode_utf8.ts";
|
||||
import * as domTypes from "./dom_types.d.ts";
|
||||
import { core } from "../core.ts";
|
||||
|
||||
const CONTINUE = null;
|
||||
|
@ -449,7 +448,7 @@ export class TextDecoder {
|
|||
}
|
||||
|
||||
decode(
|
||||
input?: domTypes.BufferSource,
|
||||
input?: BufferSource,
|
||||
options: TextDecodeOptions = { stream: false }
|
||||
): string {
|
||||
if (options.stream) {
|
||||
|
|
|
@ -74,7 +74,7 @@ function generateUUID(): string {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
return path.startsWith("/");
|
||||
|
@ -373,7 +373,7 @@ export class URL implements domTypes.URL {
|
|||
}
|
||||
|
||||
// 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 key = `blob:${origin}/${generateUUID()}`;
|
||||
blobURLMap.set(key, b);
|
||||
|
|
Loading…
Add table
Reference in a new issue