diff options
Diffstat (limited to 'js/body.ts')
-rw-r--r-- | js/body.ts | 78 |
1 files changed, 1 insertions, 77 deletions
diff --git a/js/body.ts b/js/body.ts index fdf7fef06..b1f1af8a5 100644 --- a/js/body.ts +++ b/js/body.ts @@ -1,27 +1,17 @@ -import * as streams from "@stardazed/streams"; import * as formData from "./form_data"; import * as blob from "./blob"; import * as encoding from "./text_encoding"; import * as headers from "./headers"; - import * as domTypes from "./dom_types"; const { Headers } = headers; // only namespace imports work for now, plucking out what we need -const { ReadableStream } = streams; const { FormData } = formData; const { TextEncoder, TextDecoder } = encoding; const Blob = blob.DenoBlob; const DenoBlob = blob.DenoBlob; -type ReadableStreamReader = domTypes.ReadableStreamReader; - -interface ReadableStreamController { - enqueue(chunk: string | ArrayBuffer): void; - close(): void; -} - export type BodySource = | domTypes.Blob | domTypes.BufferSource @@ -47,8 +37,6 @@ function validateBodyType(owner: Body, bodySource: BodySource): boolean { return true; } else if (typeof bodySource === "string") { return true; - } else if (bodySource instanceof ReadableStream) { - return true; } else if (bodySource instanceof FormData) { return true; } else if (!bodySource) { @@ -59,58 +47,6 @@ function validateBodyType(owner: Body, bodySource: BodySource): boolean { ); } -function concatenate(...arrays: Uint8Array[]): ArrayBuffer { - let totalLength = 0; - for (const arr of arrays) { - totalLength += arr.length; - } - const result = new Uint8Array(totalLength); - let offset = 0; - for (const arr of arrays) { - result.set(arr, offset); - offset += arr.length; - } - return result.buffer as ArrayBuffer; -} - -function bufferFromStream(stream: ReadableStreamReader): Promise<ArrayBuffer> { - return new Promise( - (resolve, reject): void => { - const parts: Uint8Array[] = []; - const encoder = new TextEncoder(); - // recurse - (function pump(): void { - stream - .read() - .then( - ({ done, value }): void => { - if (done) { - return resolve(concatenate(...parts)); - } - - if (typeof value === "string") { - parts.push(encoder.encode(value)); - } else if (value instanceof ArrayBuffer) { - parts.push(new Uint8Array(value)); - } else if (!value) { - // noop for undefined - } else { - reject("unhandled type on stream read"); - } - - return pump(); - } - ) - .catch( - (err): void => { - reject(err); - } - ); - })(); - } - ); -} - function getHeaderValueParams(value: string): Map<string, string> { const params = new Map(); // Forced to do so for some Map constructor param mismatch @@ -145,17 +81,8 @@ export class Body implements domTypes.Body { if (this._stream) { return this._stream; } - if (this._bodySource instanceof ReadableStream) { - // @ts-ignore - this._stream = this._bodySource; - } if (typeof this._bodySource === "string") { - this._stream = new ReadableStream({ - start(controller: ReadableStreamController): void { - controller.enqueue(this._bodySource); - controller.close(); - } - }); + throw Error("not implemented"); } return this._stream; } @@ -332,9 +259,6 @@ export class Body implements domTypes.Body { } else if (typeof this._bodySource === "string") { const enc = new TextEncoder(); return enc.encode(this._bodySource).buffer as ArrayBuffer; - } else if (this._bodySource instanceof ReadableStream) { - // @ts-ignore - return bufferFromStream(this._bodySource.getReader()); } else if (this._bodySource instanceof FormData) { const enc = new TextEncoder(); return enc.encode(this._bodySource.toString()).buffer as ArrayBuffer; |