From 3889492dec692a620f246f6a838f34a0078c0eab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=98=BF=E6=9E=97?= <724159997@qq.com>
Date: Thu, 26 Sep 2019 07:04:49 +0800
Subject: [PATCH] [@types/node] add `destroyed` property for Readable and
Writable (#38237)
* [@types/node] add `destroyed` property for Readable and Writable
1. https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_readable_destroyed
2. https://nodejs.org/dist/latest-v12.x/docs/api/stream.html#stream_writable_destroyed
* [@types/node] add test for readable.destroyed/writable.destroyed
1. Property 'destroyed' is missing in type 'WriteStream' but required in type 'Writable'
2. check `destroyed`
* fixed: replace Readable with ReadableStream
---
types/derhuerst__cli-on-key/index.d.ts | 4 +---
types/node/stream.d.ts | 2 ++
types/node/test/global.ts | 21 ++++++++++++++++-----
types/yazl/index.d.ts | 7 +++----
types/yazl/yazl-tests.ts | 3 +--
5 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/types/derhuerst__cli-on-key/index.d.ts b/types/derhuerst__cli-on-key/index.d.ts
index 59b1c2172a..02a5295fbe 100644
--- a/types/derhuerst__cli-on-key/index.d.ts
+++ b/types/derhuerst__cli-on-key/index.d.ts
@@ -5,8 +5,6 @@
///
-import stream = require("stream");
-
declare namespace listen {
interface Key {
name?: string;
@@ -23,7 +21,7 @@ declare namespace listen {
}
declare function listen(
- stream: stream.Readable,
+ stream: NodeJS.ReadStream,
callback: listen.Callback
): listen.OffKeyPress;
diff --git a/types/node/stream.d.ts b/types/node/stream.d.ts
index f4424f8c10..f02fcb4ae8 100644
--- a/types/node/stream.d.ts
+++ b/types/node/stream.d.ts
@@ -26,6 +26,7 @@ declare module "stream" {
readable: boolean;
readonly readableHighWaterMark: number;
readonly readableLength: number;
+ destroyed: boolean;
constructor(opts?: ReadableOptions);
_read(size: number): void;
read(size?: number): any;
@@ -119,6 +120,7 @@ declare module "stream" {
readonly writableFinished: boolean;
readonly writableHighWaterMark: number;
readonly writableLength: number;
+ destroyed: boolean;
constructor(opts?: WritableOptions);
_write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
_writev?(chunks: Array<{ chunk: any, encoding: string }>, callback: (error?: Error | null) => void): void;
diff --git a/types/node/test/global.ts b/types/node/test/global.ts
index 1155e2f4a5..cc8b32bf7d 100644
--- a/types/node/test/global.ts
+++ b/types/node/test/global.ts
@@ -32,10 +32,21 @@ import { Readable, Writable } from 'stream';
const a: NodeJS.TypedArray = new Buffer(123);
{
- const stdin: Readable = process.stdin;
let writableFinished: boolean;
- const stdout: Writable = process.stdout;
- writableFinished = process.stdout.writableFinished;
- const stderr: Writable = process.stderr;
- writableFinished = process.stderr.writableFinished;
+ const readable: Readable = new Readable({
+ read() {
+ this.push('hello');
+ this.push('world');
+ this.push(null);
+ },
+ });
+ readable.destroyed;
+ const writable: Writable = new Writable({
+ write(chunk, _, cb) {
+ cb();
+ },
+ });
+ readable.pipe(writable);
+ writableFinished = writable.writableFinished;
+ writable.destroyed;
}
diff --git a/types/yazl/index.d.ts b/types/yazl/index.d.ts
index 41b18b8a15..04bdcbb612 100644
--- a/types/yazl/index.d.ts
+++ b/types/yazl/index.d.ts
@@ -7,7 +7,6 @@
///
-import { Readable } from 'stream';
import { Buffer } from 'buffer';
export interface Options {
@@ -37,10 +36,10 @@ export interface DosDateTime {
export class ZipFile {
addFile(realPath: string, metadataPath: string, options?: Partial): void;
- outputStream: Readable;
- addReadStream(input: Readable, metadataPath: string, options?: Partial): void;
+ outputStream: NodeJS.ReadableStream;
+ addReadStream(input: NodeJS.ReadableStream, metadataPath: string, options?: Partial): void;
addBuffer(buffer: Buffer, metadataPath: string, options?: Partial): void;
- end(optoins?: EndOptions, finalSizeCallback?: () => void): void;
+ end(options?: EndOptions, finalSizeCallback?: () => void): void;
addEmptyDirectory(metadataPath: string, options?: Partial): void;
dateToDosDateTime(jsDate: Date): DosDateTime;
diff --git a/types/yazl/yazl-tests.ts b/types/yazl/yazl-tests.ts
index 50e371a62a..52a7269582 100644
--- a/types/yazl/yazl-tests.ts
+++ b/types/yazl/yazl-tests.ts
@@ -1,5 +1,4 @@
import { ZipFile } from "yazl";
-import { Readable } from "stream";
import fs = require('fs');
const zipfile = new ZipFile();
@@ -7,7 +6,7 @@ zipfile.addFile("file1.txt", "file1.txt");
// (add only files, not directories)
zipfile.addFile("path/to/file.txt", "path/in/zipfile.txt");
// pipe() can be called any time after the constructor
-// $ExpectType Readable
+// $ExpectType ReadableStream
zipfile.outputStream;
zipfile.outputStream.pipe(fs.createWriteStream("output.zip")).on("close", () => {
console.log("done");