From 20f812d17628dbb6257fa324085a2ec9cd173d59 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Thu, 3 Oct 2019 23:26:56 +0200 Subject: [PATCH] Deprecate whatwg-streams in favor for DOM types (#38785) TypeScript 3.2 shipped with built-in type definitions for streams, so this types package is no longer needed. --- notNeededPackages.json | 6 + types/whatwg-streams/index.d.ts | 167 ------- types/whatwg-streams/tsconfig.json | 24 - types/whatwg-streams/tslint.json | 80 --- types/whatwg-streams/whatwg-streams-tests.ts | 485 ------------------- 5 files changed, 6 insertions(+), 756 deletions(-) delete mode 100644 types/whatwg-streams/index.d.ts delete mode 100644 types/whatwg-streams/tsconfig.json delete mode 100644 types/whatwg-streams/tslint.json delete mode 100644 types/whatwg-streams/whatwg-streams-tests.ts diff --git a/notNeededPackages.json b/notNeededPackages.json index 75ceea5c81..11a7531466 100644 --- a/notNeededPackages.json +++ b/notNeededPackages.json @@ -3906,6 +3906,12 @@ "sourceRepoURL": "https://github.com/neutrinojs/webpack-chain", "asOfVersion": "5.2.0" }, + { + "libraryName": "typescript", + "typingsPackageName": "whatwg-streams", + "sourceRepoURL": "https://streams.spec.whatwg.org", + "asOfVersion": "3.2.1" + }, { "libraryName": "winston", "typingsPackageName": "winston", diff --git a/types/whatwg-streams/index.d.ts b/types/whatwg-streams/index.d.ts deleted file mode 100644 index 008029b2d9..0000000000 --- a/types/whatwg-streams/index.d.ts +++ /dev/null @@ -1,167 +0,0 @@ -// Type definitions for Streams API -// Project: https://github.com/whatwg/streams -// Definitions by: Kagami Sascha Rosylight -// Konstantin Simon Maria Möllers -// Mattias Buelens -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 - -export interface ReadableStreamSource { - start?(controller: ReadableStreamDefaultController): void | Promise; - pull?(controller: ReadableStreamDefaultController): void | Promise; - cancel?(reason: any): void | Promise; -} - -export interface ReadableByteStreamSource { - start?(controller: ReadableByteStreamController): void | Promise; - pull?(controller: ReadableByteStreamController): void | Promise; - cancel?(reason: any): void | Promise; - - type: "bytes"; - autoAllocateChunkSize?: number; -} - -export interface QueuingStrategy { - size?(chunk: T): number; - highWaterMark?: number; -} - -export interface PipeOptions { - preventClose?: boolean; - preventAbort?: boolean; - preventCancel?: boolean; -} - -export interface WritableReadablePair, U extends ReadableStream> { - writable: T; - readable: U; -} - -export interface ReadResult { - done: boolean; - value: T; -} - -declare class ReadableStream { - constructor(underlyingSource?: ReadableStreamSource, strategy?: QueuingStrategy); - constructor(underlyingSource?: ReadableByteStreamSource, strategy?: QueuingStrategy); - - readonly locked: boolean; - - cancel(reason: any): Promise; - getReader(): ReadableStreamDefaultReader; - getReader({ mode }: { mode: "byob" }): ReadableStreamBYOBReader; - pipeThrough>({ writable, readable }: WritableReadablePair, T>, options?: PipeOptions): T; - pipeTo(dest: WritableStream, options?: PipeOptions): Promise; - tee(): [ReadableStream, ReadableStream]; -} - -declare class ReadableStreamDefaultReader { - constructor(stream: ReadableStream); - - readonly closed: Promise; - - cancel(reason: any): Promise; - read(): Promise>; - releaseLock(): void; -} - -declare class ReadableStreamBYOBReader { - constructor(stream: ReadableStream); - - readonly closed: Promise; - - cancel(reason: any): Promise; - read(view: T): Promise>; - releaseLock(): void; -} - -declare class ReadableStreamDefaultController { - readonly desiredSize: number | null; - - close(): void; - enqueue(chunk: R): void; - error(e: any): void; -} - -declare class ReadableByteStreamController { - readonly byobRequest: ReadableStreamBYOBRequest | undefined; - readonly desiredSize: number | null; - - close(): void; - enqueue(chunk: ArrayBufferView): void; - error(e: any): void; -} - -declare class ReadableStreamBYOBRequest { - readonly view: Uint8Array; - - respond(bytesWritten: number): void; - respondWithNewView(view: ArrayBufferView): void; -} - -interface WritableStreamSink { - start?(controller: WritableStreamDefaultController): void | Promise; - write?(chunk: W, controller?: WritableStreamDefaultController): void | Promise; - close?(controller: WritableStreamDefaultController): void | Promise; - abort?(reason: any): void | Promise; -} - -declare class WritableStream { - constructor(underlyingSink?: WritableStreamSink, strategy?: QueuingStrategy); - - readonly locked: boolean; - - abort(reason: any): Promise; - getWriter(): WritableStreamDefaultWriter; -} - -declare class WritableStreamDefaultWriter { - constructor(stream: WritableStream); - - readonly closed: Promise; - readonly desiredSize: number | null; - readonly ready: Promise; - - abort(reason: any): Promise; - close(): Promise; - releaseLock(): void; - write(chunk: W): Promise; -} - -declare class WritableStreamDefaultController { - error(e: any): void; -} - -declare class ByteLengthQueuingStrategy { - constructor({ highWaterMark }: { highWaterMark: number }); - - size(chunk: T): number | undefined; -} - -declare class CountQueuingStrategy { - constructor({ highWaterMark }: { highWaterMark: number }); - - size(): 1; -} - -export interface TransformStreamTransformer { - start?(controller: TransformStreamDefaultController): void | Promise; - transform?(chunk: W, controller: TransformStreamDefaultController): void | Promise; - flush?(controller: TransformStreamDefaultController): void | Promise; -} - -declare class TransformStream implements WritableReadablePair, ReadableStream> { - constructor(transformer?: TransformStreamTransformer, writableStrategy?: QueuingStrategy, readableStrategy?: QueuingStrategy); - - readonly readable: ReadableStream; - readonly writable: WritableStream; -} - -declare class TransformStreamDefaultController { - enqueue(chunk: R): void; - error(reason: any): void; - terminate(): void; - - readonly desiredSize: number | null; -} diff --git a/types/whatwg-streams/tsconfig.json b/types/whatwg-streams/tsconfig.json deleted file mode 100644 index bf84ec6496..0000000000 --- a/types/whatwg-streams/tsconfig.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compilerOptions": { - "module": "commonjs", - "lib": [ - "es6", - "dom" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "whatwg-streams-tests.ts" - ] -} \ No newline at end of file diff --git a/types/whatwg-streams/tslint.json b/types/whatwg-streams/tslint.json deleted file mode 100644 index 3d59f55fda..0000000000 --- a/types/whatwg-streams/tslint.json +++ /dev/null @@ -1,80 +0,0 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "npm-naming": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} diff --git a/types/whatwg-streams/whatwg-streams-tests.ts b/types/whatwg-streams/whatwg-streams-tests.ts deleted file mode 100644 index fb5c03e67c..0000000000 --- a/types/whatwg-streams/whatwg-streams-tests.ts +++ /dev/null @@ -1,485 +0,0 @@ -/// - -import { - ReadableStream, ReadableStreamSource, WritableStream, - ReadableStreamDefaultController, WritableStreamSink, WritableStreamDefaultController, ReadableByteStreamController, - TransformStream, TransformStreamDefaultController, TransformStreamTransformer -} from "whatwg-streams"; - -// Examples taken from https://streams.spec.whatwg.org/#creating-examples - -// 8.1. A readable stream with an underlying push source (no backpressure support) - -function makeReadableWebSocketStream(url: string, protocols: string | string[]) { - const ws = new WebSocket(url, protocols); - ws.binaryType = "arraybuffer"; - - return new ReadableStream({ - start(controller) { - ws.onmessage = event => controller.enqueue(event.data); - ws.onclose = () => controller.close(); - ws.onerror = () => controller.error(new Error("The WebSocket errored!")); - }, - - cancel() { - ws.close(); - } - }); -} - -{ - const writableStream = new WritableStream(); - - const webSocketStream = makeReadableWebSocketStream("wss://example.com:443/", "protocol"); - - webSocketStream.pipeTo(writableStream) - .then(() => console.log("All data successfully written!")) - .catch(e => console.error("Something went wrong!", e)); -} - - -// 8.2. A readable stream with an underlying push source and backpressure support - -function makeReadableBackpressureSocketStream(host: string, port: number) { - const socket = createBackpressureSocket(host, port); - - return new ReadableStream({ - start(controller) { - socket.ondata = (event: any) => { - controller.enqueue(event.data); - - if (controller.desiredSize! <= 0) { - // The internal queue is full, so propagate - // the backpressure signal to the underlying source. - socket.readStop(); - } - }; - - socket.onend = () => controller.close(); - socket.onerror = () => controller.error(new Error("The socket errored!")); - }, - - pull() { - // This is called if the internal queue has been emptied, but the - // stream’s consumer still wants more data. In that case, restart - // the flow of data if we have previously paused it. - socket.readStart(); - }, - - cancel() { - socket.close(); - } - }); - - function createBackpressureSocket(host: string, port: number): any { }; -} - - -// 8.3. A readable byte stream with an underlying push source (no backpressure support) - -const DEFAULT_CHUNK_SIZE = 65536; - -function makeUDPSocketStream(host: string, port: number) { - const socket = createUDPSocket(host, port); - - return new ReadableStream({ - type: "bytes", - - start(controller: ReadableByteStreamController) { - readRepeatedly().catch(e => controller.error(e)); - - function readRepeatedly(): Promise { - return socket.select2().then(() => { - // Since the socket can become readable even when there’s - // no pending BYOB requests, we need to handle both cases. - let bytesRead: number; - if (controller.byobRequest) { - const v = controller.byobRequest.view; - bytesRead = socket.readInto(v.buffer, v.byteOffset, v.byteLength); - controller.byobRequest.respond(bytesRead); - } else { - const buffer = new ArrayBuffer(DEFAULT_CHUNK_SIZE); - bytesRead = socket.readInto(buffer, 0, DEFAULT_CHUNK_SIZE); - controller.enqueue(new Uint8Array(buffer, 0, bytesRead)); - } - - if (bytesRead === 0) { - controller.close(); - return; - } - - return readRepeatedly(); - }); - } - }, - - cancel() { - socket.close(); - } - }); - - function createUDPSocket(host: string, port: number): any { }; -} - - -// 8.4. A readable stream with an underlying pull source - -//const fs = require("pr/fs"); // https://github.com/jden/pr -interface fs { - open(path: string | Buffer, flags: string | number): Promise; - read(fd: number, buffer: Buffer, offset: number, length: number, position: number): Promise; - write(fd: number, buffer: Buffer | string, offset: number, length: number): Promise; - close(fd: number): Promise; -} -let fs: fs; - -const CHUNK_SIZE = 1024; - -function makeReadableFileStream(filename: string) { - let fd: number; - let position = 0; - - return new ReadableStream({ - start() { - return fs.open(filename, "r").then(result => { - fd = result; - }); - }, - - pull(controller) { - const buffer = new ArrayBuffer(CHUNK_SIZE); - - return fs.read(fd, buffer as any, 0, CHUNK_SIZE, position).then(bytesRead => { - if (bytesRead === 0) { - return fs.close(fd).then(() => controller.close()); - } else { - position += bytesRead; - controller.enqueue(new Uint8Array(buffer, 0, bytesRead)); - } - }); - }, - - cancel() { - return fs.close(fd); - } - }); -} - - -// 8.5. A readable byte stream with an underlying pull source - -//const fs = require("pr/fs"); // https://github.com/jden/pr -//const DEFAULT_CHUNK_SIZE = 1024; - -function makeReadableByteFileStream(filename: string) { - let fd: number; - let position = 0; - - return new ReadableStream({ - type: "bytes", - - start() { - return fs.open(filename, "r").then(result => { - fd = result; - }); - }, - - pull(controller: ReadableByteStreamController) { - // Even when the consumer is using the default reader, the auto-allocation - // feature allocates a buffer and passes it to us via byobRequest. - const v = controller.byobRequest!.view; - - return fs.read(fd, v.buffer as any, v.byteOffset, v.byteLength, position).then(bytesRead => { - if (bytesRead === 0) { - return fs.close(fd).then(() => controller.close()); - } else { - position += bytesRead; - controller.byobRequest!.respond(bytesRead); - } - }); - }, - - cancel() { - return fs.close(fd); - }, - - autoAllocateChunkSize: DEFAULT_CHUNK_SIZE - }); -} - - -// 8.6. A writable stream with no backpressure or success signals - -function makeWritableWebSocketStream(url: string, protocols: string | string[]) { - const ws = new WebSocket(url, protocols); - - return new WritableStream({ - start(controller) { - ws.onerror = () => controller.error(new Error("The WebSocket errored!")); - return new Promise(resolve => ws.onopen = () => resolve()); - }, - - write(chunk) { - ws.send(chunk); - // Return immediately, since the web socket gives us no easy way to tell - // when the write completes. - }, - - close() { - return new Promise((resolve, reject) => { - ws.onclose = () => resolve(); - ws.close(); - }); - } - }); -} - -{ - const readableStream = new ReadableStream(); - - const webSocketStream = makeWritableWebSocketStream("wss://example.com:443/", "protocol"); - - readableStream.pipeTo(webSocketStream) - .then(() => console.log("All data successfully written!")) - .catch(e => console.error("Something went wrong!", e)); -} - - -// 8.7. A writable stream with backpressure and success signals - -//const fs = require("pr/fs"); // https://github.com/jden/pr - -function makeWritableFileStream(filename: string) { - let fd: number; - - return new WritableStream({ - start() { - return fs.open(filename, "w").then(result => { - fd = result; - }); - }, - - write(chunk) { - return fs.write(fd, chunk, 0, chunk.length); - }, - - close() { - return fs.close(fd); - } - }); -} - -{ - const fileStream = makeWritableFileStream("/example/path/on/fs.txt"); - const writer = fileStream.getWriter(); - - writer.write("To stream, or not to stream\n"); - writer.write("That is the question\n"); - - writer.close() - .then(() => console.log("chunks written and stream closed successfully!")) - .catch(e => console.error(e)); -} - - -// 8.8. A { readable, writable } stream pair wrapping the same underlying resource - - -function streamifyWebSocket(url: string, protocol: string) { - const ws = new WebSocket(url, protocol); - ws.binaryType = "arraybuffer"; - - return { - readable: new ReadableStream(new WebSocketSource(ws)), - writable: new WritableStream(new WebSocketSink(ws)) - }; -} - -class WebSocketSource implements ReadableStreamSource { - private readonly _ws: WebSocket; - - constructor(ws: WebSocket) { - this._ws = ws; - } - - start(controller: ReadableStreamDefaultController) { - this._ws.onmessage = event => controller.enqueue(event.data); - this._ws.onclose = () => controller.close(); - - this._ws.addEventListener("error", () => { - controller.error(new Error("The WebSocket errored!")); - }); - } - - cancel() { - this._ws.close(); - } -} - -class WebSocketSink implements WritableStreamSink { - private readonly _ws: WebSocket; - - constructor(ws: WebSocket) { - this._ws = ws; - } - - start(controller: WritableStreamDefaultController) { - this._ws.addEventListener("error", () => { - controller.error(new Error("The WebSocket errored!")); - }); - - return new Promise(resolve => this._ws.onopen = () => resolve()); - } - - write(chunk: any) { - this._ws.send(chunk); - } - - close() { - return new Promise((resolve, reject) => { - this._ws.onclose = () => resolve(); - this._ws.close(); - }); - } -} - -{ - const streamyWS = streamifyWebSocket("wss://example.com:443/", "protocol"); - const writer = streamyWS.writable.getWriter(); - const reader = streamyWS.readable.getReader(); - - writer.write("Hello"); - writer.write("web socket!"); - - reader.read().then(({ value, done }) => { - console.log("The web socket says: ", value); - }); -} - - -// 8.9. A transform stream that replaces template tags - - -type Dictionary = { [key: string]: T } - -declare function fetch(input?: Request | string): Promise; -declare interface FetchEvent { - readonly request: Request; - respondWith(promise: Promise): void; -} -declare class Request { - readonly url: string; -} -declare class Response { - constructor(body?: ReadableStream); - readonly body: ReadableStream; -} -declare class TextDecoderStream extends TransformStream { -} -declare class TextEncoderStream extends TransformStream { -} - -class LipFuzzTransformer implements TransformStreamTransformer { - substitutions: Dictionary; - partialChunk: string; - lastIndex: number | undefined; - - constructor(substitutions: Dictionary) { - this.substitutions = substitutions; - this.partialChunk = ""; - this.lastIndex = undefined; - } - - transform(chunk: string, controller: TransformStreamDefaultController) { - chunk = this.partialChunk + chunk; - this.partialChunk = ""; - // lastIndex is the index of the first character after the last substitution. - this.lastIndex = 0; - chunk = chunk.replace(/\{\{([a-zA-Z0-9_-]+)\}\}/g, this.replaceTag.bind(this)); - // Regular expression for an incomplete template at the end of a string. - const partialAtEndRegexp = /\{(\{([a-zA-Z0-9_-]+(\})?)?)?$/g; - // Avoid looking at any characters that have already been substituted. - partialAtEndRegexp.lastIndex = this.lastIndex; - this.lastIndex = undefined; - const match = partialAtEndRegexp.exec(chunk); - if (match) { - this.partialChunk = chunk.substring(match.index); - chunk = chunk.substring(0, match.index); - } - controller.enqueue(chunk); - } - - flush(controller: TransformStreamDefaultController) { - if (this.partialChunk.length > 0) { - controller.enqueue(this.partialChunk); - } - } - - replaceTag(match: string, p1: string, offset: number) { - let replacement = this.substitutions[p1]; - if (replacement === undefined) { - replacement = ""; - } - this.lastIndex = offset + replacement.length; - return replacement; - } -} - -{ - const userName = ""; - const displayName = ""; - const icon = ""; - const date = ""; - const fetchEvent = {} as FetchEvent; - - const data = { userName, displayName, icon, date }; - const ts = new TransformStream(new LipFuzzTransformer(data)); - - fetchEvent.respondWith( - fetch(fetchEvent.request.url).then(response => { - const transformedBody = response.body - // Decode the binary-encoded response to string - .pipeThrough(new TextDecoderStream()) - // Apply the LipFuzzTransformer - .pipeThrough(ts) - // Encode the transformed string - .pipeThrough(new TextEncoderStream()); - return new Response(transformedBody); - }) - ); -} - - -// 8.10. A transform stream created from a sync mapper function - -function mapperTransformStream(mapperFunction: (chunk: W) => R) { - return new TransformStream({ - transform(chunk, controller) { - controller.enqueue(mapperFunction(chunk)); - } - }); -} - -{ - const ts = mapperTransformStream((chunk: string) => chunk.toUpperCase()); - const writer = ts.writable.getWriter(); - const reader = ts.readable.getReader(); - - writer.write("No need to shout"); - - // Logs "NO NEED TO SHOUT": - reader.read().then(({ value }) => console.log(value)); - -} - -{ - const ts = mapperTransformStream((chunk: string) => JSON.parse(chunk)); - const writer = ts.writable.getWriter(); - const reader = ts.readable.getReader(); - - writer.write("[1, "); - - // Logs a SyntaxError, twice: - reader.read().catch(e => console.error(e)); - writer.write("{}").catch(e => console.error(e)); -}