[@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
This commit is contained in:
阿林 2019-09-26 07:04:49 +08:00 committed by Ben Lichtman
parent b3f478f123
commit 3889492dec
5 changed files with 23 additions and 14 deletions

View File

@ -5,8 +5,6 @@
/// <reference types="node" />
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;

View File

@ -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;

View File

@ -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;
}

View File

@ -7,7 +7,6 @@
/// <reference types="node" />
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<Options>): void;
outputStream: Readable;
addReadStream(input: Readable, metadataPath: string, options?: Partial<ReadStreamOptions>): void;
outputStream: NodeJS.ReadableStream;
addReadStream(input: NodeJS.ReadableStream, metadataPath: string, options?: Partial<ReadStreamOptions>): void;
addBuffer(buffer: Buffer, metadataPath: string, options?: Partial<Options>): void;
end(optoins?: EndOptions, finalSizeCallback?: () => void): void;
end(options?: EndOptions, finalSizeCallback?: () => void): void;
addEmptyDirectory(metadataPath: string, options?: Partial<DirectoryOptions>): void;
dateToDosDateTime(jsDate: Date): DosDateTime;

View File

@ -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");