Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit efa98bf

Browse files
committedApr 22, 2019
Implement fs.createReadStream
This is used by the file tree to copy files.
1 parent eab2d44 commit efa98bf

File tree

2 files changed

+43
-3
lines changed
  • packages/protocol/src

2 files changed

+43
-3
lines changed
 

‎packages/protocol/src/browser/modules/fs.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as fs from "fs";
22
import { callbackify } from "util";
33
import { ClientProxy, Batch } from "../../common/proxy";
44
import { IEncodingOptions, IEncodingOptionsCallback } from "../../common/util";
5-
import { FsModuleProxy, Stats as IStats, WatcherProxy, WriteStreamProxy } from "../../node/modules/fs";
6-
import { Writable } from "./stream";
5+
import { FsModuleProxy, ReadStreamProxy, Stats as IStats, WatcherProxy, WriteStreamProxy } from "../../node/modules/fs";
6+
import { Readable, Writable } from "./stream";
77

88
// tslint:disable no-any
99
// tslint:disable completed-docs
@@ -48,6 +48,20 @@ class Watcher extends ClientProxy<WatcherProxy> implements fs.FSWatcher {
4848
}
4949
}
5050

51+
class ReadStream extends Readable<ReadStreamProxy> implements fs.ReadStream {
52+
public get bytesRead(): number {
53+
throw new Error("not implemented");
54+
}
55+
56+
public get path(): string | Buffer {
57+
throw new Error("not implemented");
58+
}
59+
60+
public close(): void {
61+
this.catch(this.proxy.close());
62+
}
63+
}
64+
5165
class WriteStream extends Writable<WriteStreamProxy> implements fs.WriteStream {
5266
public get bytesWritten(): number {
5367
throw new Error("not implemented");
@@ -110,6 +124,10 @@ export class FsModule {
110124
);
111125
}
112126

127+
public createReadStream = (path: fs.PathLike, options?: any): fs.ReadStream => {
128+
return new ReadStream(this.proxy.createReadStream(path, options));
129+
}
130+
113131
public createWriteStream = (path: fs.PathLike, options?: any): fs.WriteStream => {
114132
return new WriteStream(this.proxy.createWriteStream(path, options));
115133
}

‎packages/protocol/src/node/modules/fs.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from "fs";
22
import { promisify } from "util";
33
import { ServerProxy } from "../../common/proxy";
44
import { IEncodingOptions } from "../../common/util";
5-
import { WritableProxy } from "./stream";
5+
import { ReadableProxy, WritableProxy } from "./stream";
66

77
// tslint:disable completed-docs
88

@@ -37,6 +37,23 @@ export interface Stats {
3737
_isSocket: boolean;
3838
}
3939

40+
export class ReadStreamProxy extends ReadableProxy<fs.ReadStream> {
41+
public async close(): Promise<void> {
42+
this.stream.close();
43+
}
44+
45+
public async dispose(): Promise<void> {
46+
await super.dispose();
47+
this.stream.close();
48+
}
49+
50+
// tslint:disable-next-line no-any
51+
public async onEvent(cb: (event: string, ...args: any[]) => void): Promise<void> {
52+
await super.onEvent(cb);
53+
this.stream.on("open", (fd) => cb("open", fd));
54+
}
55+
}
56+
4057
export class WriteStreamProxy extends WritableProxy<fs.WriteStream> {
4158
public async close(): Promise<void> {
4259
this.stream.close();
@@ -105,6 +122,11 @@ export class FsModuleProxy {
105122
return promisify(fs.copyFile)(src, dest, flags);
106123
}
107124

125+
// tslint:disable-next-line no-any
126+
public async createReadStream(path: fs.PathLike, options?: any): Promise<ReadStreamProxy> {
127+
return new ReadStreamProxy(fs.createReadStream(path, options));
128+
}
129+
108130
// tslint:disable-next-line no-any
109131
public async createWriteStream(path: fs.PathLike, options?: any): Promise<WriteStreamProxy> {
110132
return new WriteStreamProxy(fs.createWriteStream(path, options));

0 commit comments

Comments
 (0)
Please sign in to comment.