forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsocketFS.ts
More file actions
124 lines (109 loc) · 2.56 KB
/
WebsocketFS.ts
File metadata and controls
124 lines (109 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* eslint-disable max-classes-per-file */
import { ApiError, ErrorCode } from "../core/api_error";
import { FileFlag } from "../core/file_flag";
import {
BFSCallback,
BFSOneArgCallback,
FileSystem,
FileSystemOptions,
SynchronousFileSystem
} from "../core/file_system";
import Stats from '../core/node_fs_stats';
export interface Socket {
emit: (data: any, cb: (answer: any) => void) => void;
dispose: () => void;
}
export interface WebsocketFSOptions {
socket: Socket;
}
export default class WebsocketFS extends SynchronousFileSystem
implements FileSystem {
public static readonly Name = "WebsocketFS";
public static readonly Options: FileSystemOptions = {
socket: {
type: "object",
description: "The socket emitter",
validator: (opt: WebsocketFSOptions, cb: BFSOneArgCallback): void => {
if (opt) {
cb();
} else {
cb(new ApiError(ErrorCode.EINVAL, `Manager is invalid`));
}
}
}
}
public static Create(
options: WebsocketFSOptions,
cb: BFSCallback<WebsocketFS>
): void {
cb(null, new WebsocketFS(options));
}
public static isAvailable(): boolean {
return true;
}
private socket: Socket;
constructor(options: WebsocketFSOptions) {
super()
this.socket = options.socket;
}
public getName(): string {
return "WebsocketFS";
}
public isReadOnly(): boolean {
return false;
}
public supportsProps(): boolean {
return false;
}
public supportsSynch(): boolean {
return true;
}
public readFile(fname: string, encoding: string | null, flag: FileFlag, cb: BFSCallback<string | Buffer>): void {
try {
this.socket.emit({
method: 'readFile',
args: {
path: fname,
encoding,
flag
}
}, ({ error, data }) => {
if (data) {
cb(null, Buffer.from(data));
} else {
cb(error)
}
})
} catch (e) {
cb(e);
}
}
public stat(p: string, isLstat: boolean | null, cb: BFSCallback<Stats>): void {
try {
this.socket.emit({
method: 'stat',
args: {
path: p,
isLstat
}
}, ({ error, data }) => {
if (data) {
cb(null, {
...data,
atime: new Date(data.atime),
mtime: new Date(data.mtime),
ctime: new Date(data.ctime),
birthtime: new Date(data.birthtime)
});
} else {
cb(error)
}
})
} catch (e) {
cb(e);
}
}
}
/*
this.statSync(p, isLstat || true)
*/