-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhandle.js
More file actions
205 lines (164 loc) · 5.18 KB
/
handle.js
File metadata and controls
205 lines (164 loc) · 5.18 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import * as duplex from "@progrium/duplex";
export class WanixHandle {
constructor(port) {
const sess = new duplex.Session(new duplex.PortConn(port));
this.peer = new duplex.Peer(sess, new duplex.CBORCodec());
}
async readDir(name) {
return (await this.peer.call("ReadDir", [name])).value;
}
async makeDir(name) {
await this.peer.call("Mkdir", [name]);
}
async makeDirAll(name) {
await this.peer.call("MkdirAll", [name]);
}
async bind(name, newname) {
await this.peer.call("Bind", [name, newname]);
}
async unbind(name, newname) {
await this.peer.call("Unbind", [name, newname]);
}
async readFile(name) {
return (await this.peer.call("ReadFile", [name])).value;
}
// not sure if readFile approach is good, but this is an option for now
async readFile2(name) {
const rd = await this.openReadable(name);
const response = new Response(rd); // cute trick
return new Uint8Array(await response.arrayBuffer());
}
async readText(name) {
return (new TextDecoder()).decode(await this.readFile(name));
}
async waitFor(name, timeoutMs=1000) {
await this.peer.call("WaitFor", [name, timeoutMs]);
}
async stat(name) {
return (await this.peer.call("Stat", [name])).value;
}
async writeFile(name, contents) {
if (typeof contents === "string") {
contents = (new TextEncoder()).encode(contents);
}
return (await this.peer.call("WriteFile", [name, contents])).value;
}
async appendFile(name, contents) {
if (typeof contents === "string") {
contents = (new TextEncoder()).encode(contents);
}
return (await this.peer.call("AppendFile", [name, contents])).value;
}
async rename(oldname, newname) {
await this.peer.call("Rename", [oldname, newname]);
}
async copy(oldname, newname) {
await this.peer.call("Copy", [oldname, newname]);
}
async remove(name) {
await this.peer.call("Remove", [name]);
}
async removeAll(name) {
await this.peer.call("RemoveAll", [name]);
}
async truncate(name, size) {
await this.peer.call("Truncate", [name, size]);
}
async create(name) {
return (await this.peer.call("Create", [name])).value;
}
async open(name) {
return (await this.peer.call("Open", [name])).value;
}
async openFile(name, flags, mode) {
return (await this.peer.call("OpenFile", [name, flags, mode])).value;
}
async read(fd, count) {
return (await this.peer.call("Read", [fd, count])).value;
}
async write(fd, data) {
return (await this.peer.call("Write", [fd, data])).value;
}
async writeAt(fd, data, offset) {
return (await this.peer.call("WriteAt", [fd, data, offset])).value;
}
async close(fd) {
return (await this.peer.call("Close", [fd])).value;
}
async sync(fd) {
return (await this.peer.call("Sync", [fd])).value;
}
async fstat(fd) {
return (await this.peer.call("Fstat", [fd])).value;
}
async lstat(name) {
return (await this.peer.call("Lstat", [name])).value;
}
async chmod(name, mode) {
await this.peer.call("Chmod", [name, mode]);
}
async chown(name, uid, gid) {
await this.peer.call("Chown", [name, uid, gid]);
}
async fchmod(fd, mode) {
await this.peer.call("Fchmod", [fd, mode]);
}
async fchown(fd, uid, gid) {
await this.peer.call("Fchown", [fd, uid, gid]);
}
async ftruncate(fd, length) {
await this.peer.call("Ftruncate", [fd, length]);
}
async readlink(name) {
return (await this.peer.call("Readlink", [name])).value;
}
async symlink(oldname, newname) {
await this.peer.call("Symlink", [oldname, newname]);
}
async chtimes(name, atime, mtime) {
await this.peer.call("Chtimes", [name, atime, mtime]);
}
async openReadable(name) {
const fd = await this.open(name);
return this.readable(fd);
}
async openWritable(name) {
const fd = await this.openFile(name, 1, 0);
return this.writable(fd);
}
writable(fd) {
const self = this;
return new WritableStream({
write(chunk) {
return self.write(fd, chunk);
},
});
}
readable(fd) {
const self = this;
return new ReadableStream({
async pull(controller) {
const data = await self.read(fd, 1024);
if (data === null) {
controller.close();
}
controller.enqueue(data);
},
});
}
}
// for safari
if (!ReadableStream.prototype[Symbol.asyncIterator]) {
ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
const reader = this.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) return;
yield value;
}
} finally {
reader.releaseLock();
}
};
}