node: process.stdin is a stream.Readable and process.stdout is a stream.Writable (#20493)

* Readable.wrap returns this

* Add missing properties to interface ReadableStream and WritableStream; change interfaces in fs, dgram and net into classes

* Move all additional properties from ReadableStream interface to ReadStream interface (and Write...)

* hexo-fs should re-export classes from 'fs'

* process.stdin._destroy is a function
This commit is contained in:
Piotr Roszatycki
2017-10-17 16:44:21 +02:00
committed by Andy
parent 04a54cc722
commit 8ef0c39ccf
6 changed files with 57 additions and 45 deletions

View File

@@ -119,4 +119,4 @@ blob._write(buffer, 10);
blob._write(buffer, 10, (err: Error | null) => {});
/* Stream */
const strm: NodeJS.ReadWriteStream = new fb.Stream(blob);
const strm = new fb.Stream(blob);

View File

@@ -12,6 +12,8 @@
* Original document is [here](https://www.npmjs.com/package/firebird).
*/
declare module 'firebird' {
import * as stream from 'stream';
/**
* @see createConnection() method will create Firebird Connection object for you
*/
@@ -453,24 +455,14 @@ declare module 'firebird' {
* You may pipe strm to/from NodeJS Stream objects (fs or socket).
* You may also look at [NodeJS Streams reference](https://nodejs.org/api/stream.html).
*/
class Stream implements NodeJS.ReadWriteStream {
class Stream extends stream.Stream {
constructor(blob: FBBlob);
/* Following lines is JUST AS NodeJS.ReadStream, NodeJS.WriteStream, and NodeJS.Emmiter */
/* tslint:disable */
/* NodeJS.ReadStream */
readable: boolean;
read(size?: number): string | Buffer;
setEncoding(encoding: string | null): this;
pause(): this;
resume(): this;
isPaused(): boolean;
pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T;
unpipe<T extends NodeJS.WritableStream>(destination?: T): this;
unshift(chunk: string): void;
unshift(chunk: Buffer): void;
wrap(oldStream: NodeJS.ReadableStream): NodeJS.ReadableStream;
/* NodeJS.WriteStream */
writable: boolean;
@@ -480,22 +472,9 @@ declare module 'firebird' {
end(buffer: Buffer, cb?: Function): void;
end(str: string, cb?: Function): void;
end(str: string, encoding?: string, cb?: Function): void;
/* EventEmitter */
addListener(event: string | symbol, listener: Function): this;
on(event: string | symbol, listener: Function): this;
once(event: string | symbol, listener: Function): this;
removeListener(event: string | symbol, listener: Function): this;
removeAllListeners(event?: string | symbol): this;
setMaxListeners(n: number): this;
getMaxListeners(): number;
listeners(event: string | symbol): Function[];
emit(event: string | symbol, ...args: any[]): boolean;
listenerCount(type: string | symbol): number;
prependListener(event: string | symbol, listener: Function): this;
prependOnceListener(event: string | symbol, listener: Function): this;
eventNames(): (string | symbol)[];
destroy(error?: Error): void;
/* tslint:enable */
check_destroyed(): void;
}
}

View File

@@ -427,9 +427,7 @@ export function writeFile(
export function writeFileSync(path: string, data: any, options?: string | { encoding?: string | null; mode?: string | number; flag?: string }): void;
// Static classes
export let Stats: Stats;
export let ReadStream: ReadStream;
export let WriteStream: WriteStream;
export { Stats, ReadStream, WriteStream } from 'graceful-fs';
// util
export function escapeEOL(str: string): string;

31
types/node/index.d.ts vendored
View File

@@ -345,7 +345,7 @@ declare namespace NodeJS {
unpipe<T extends WritableStream>(destination?: T): this;
unshift(chunk: string): void;
unshift(chunk: Buffer): void;
wrap(oldStream: ReadableStream): ReadableStream;
wrap(oldStream: ReadableStream): this;
}
export interface WritableStream extends EventEmitter {
@@ -438,10 +438,21 @@ declare namespace NodeJS {
export interface WriteStream extends Socket {
columns?: number;
rows?: number;
_write(chunk: any, encoding: string, callback: Function): void;
_destroy(err: Error, callback: Function): void;
_final(callback: Function): void;
setDefaultEncoding(encoding: string): this;
cork(): void;
uncork(): void;
destroy(error?: Error): void;
}
export interface ReadStream extends Socket {
isRaw?: boolean;
setRawMode?(mode: boolean): void;
_read(size: number): void;
_destroy(err: Error, callback: Function): void;
push(chunk: any, encoding?: string): boolean;
destroy(error?: Error): void;
}
export interface Process extends EventEmitter {
@@ -2430,7 +2441,9 @@ declare module "net" {
export type SocketConnectOpts = TcpSocketConnectOpts | IpcSocketConnectOpts;
export interface Socket extends stream.Duplex {
export class Socket extends stream.Duplex {
constructor(options?: { fd?: number; allowHalfOpen?: boolean; readable?: boolean; writable?: boolean; });
// Extended base methods
write(buffer: Buffer): boolean;
write(buffer: Buffer, cb?: Function): boolean;
@@ -2544,10 +2557,6 @@ declare module "net" {
prependOnceListener(event: "timeout", listener: () => void): this;
}
export var Socket: {
new(options?: SocketConstructorOpts): Socket;
};
export interface ListenOptions {
port?: number;
host?: string;
@@ -2679,7 +2688,7 @@ declare module "dgram" {
export function createSocket(type: SocketType, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
export function createSocket(options: SocketOptions, callback?: (msg: Buffer, rinfo: RemoteInfo) => void): Socket;
export interface Socket extends events.EventEmitter {
export class Socket extends events.EventEmitter {
send(msg: Buffer | String | any[], port: number, address: string, callback?: (error: Error | null, bytes: number) => void): void;
send(msg: Buffer | String | any[], offset: number, length: number, port: number, address: string, callback?: (error: Error | null, bytes: number) => void): void;
bind(port?: number, address?: string, callback?: () => void): void;
@@ -2757,7 +2766,7 @@ declare module "fs" {
*/
export type PathLike = string | Buffer | URL;
export interface Stats {
export class Stats {
isFile(): boolean;
isDirectory(): boolean;
isBlockDevice(): boolean;
@@ -2814,7 +2823,7 @@ declare module "fs" {
prependOnceListener(event: "error", listener: (error: Error) => void): this;
}
export interface ReadStream extends stream.Readable {
export class ReadStream extends stream.Readable {
close(): void;
destroy(): void;
bytesRead: number;
@@ -2846,7 +2855,7 @@ declare module "fs" {
prependOnceListener(event: "close", listener: () => void): this;
}
export interface WriteStream extends stream.Writable {
export class WriteStream extends stream.Writable {
close(): void;
bytesWritten: number;
path: string | Buffer;
@@ -5084,7 +5093,7 @@ declare module "stream" {
isPaused(): boolean;
unpipe<T extends NodeJS.WritableStream>(destination?: T): this;
unshift(chunk: any): void;
wrap(oldStream: NodeJS.ReadableStream): Readable;
wrap(oldStream: NodeJS.ReadableStream): this;
push(chunk: any, encoding?: string): boolean;
_destroy(err: Error, callback: Function): void;
destroy(error?: Error): void;

View File

@@ -2066,6 +2066,36 @@ namespace child_process_tests {
let _sendHandle: net.Socket | net.Server = sendHandle;
});
}
{
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write(`data: ${chunk}`);
}
});
process.stdin.on('end', () => {
process.stdout.write('end');
});
process.stdin.pipe(process.stdout);
console.log(process.stdin.isTTY);
console.log(process.stdout.isTTY);
console.log(process.stdin instanceof net.Socket);
console.log(process.stdout instanceof fs.ReadStream);
var stdin: stream.Readable = process.stdin;
console.log(stdin instanceof net.Socket);
console.log(stdin instanceof fs.ReadStream);
var stdout: stream.Writable = process.stdout;
console.log(stdout instanceof net.Socket);
console.log(stdout instanceof fs.WriteStream);
}
}
//////////////////////////////////////////////////////////////////////

View File

@@ -24,10 +24,6 @@ interface TestFile extends File {
_base?: string;
}
declare module 'fs' {
class Stats { }
}
var pipe: (streams: [NodeJS.ReadableStream, NodeJS.WritableStream], cb: (err?: Error) => void) => void = miss.pipe;
var from: (values: any[]) => NodeJS.ReadableStream = miss.from;
var concat: (fn: (d: Buffer) => void) => NodeJS.WritableStream = miss.concat;