forked from manuels/unix-toolbox.js-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.js
230 lines (185 loc) · 5.44 KB
/
interface.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(function(exports){
var Promise = function() {
var callbacks = [];
var result = null;
this.then = function(cb) {
if(result !== null)
cb.apply(this, result);
else
callbacks.push(cb);
return this;
};
this.fulfil = function() {
result = arguments;
for(var i in callbacks) {
callbacks[i].apply(this, result);
}
}
};
exports.Interface = function(worker_script) {
this.worker = new Worker(worker_script);
this.promises = [];
var self = this;
this.worker.onmessage = function(ev) {
var obj;
try{
obj = JSON.parse(ev.data);
}
catch(e) {
return;
}
if('id' in obj)
self.promises[obj.id].fulfil(obj);
if(obj.cmd) {
if(obj.cmd == 'stdout' && typeof(self.on_stdout) === 'function')
self.on_stdout(obj.contents+'\n')
if(obj.cmd == 'stderr' && typeof(self.on_stderr) === 'function')
self.on_stderr(obj.contents+'\n')
}
};
/*
* Download a url and import the file into the virtual filesystem
*
* real_url: Url to download
* pseudo_path: Destination path in the virtual fs
if it ends with a slash, the original file name will appended
mkdir: Boolean, create pseudo_path if it does not exist (default: false)
*
* returns a Promise
*/
this.addUrl = function(real_url, pseudo_path, mkdir) {
mkdir = mkdir || false;
var opt_filename = this._analysePath(real_url).filename;
var dst = this._analysePath(pseudo_path, opt_filename);
var prom = new Promise();
self.promises.push(prom);
var payload = function() {
self.worker.postMessage(JSON.stringify({
cmd: 'addUrl',
id: (self.promises.length-1),
real_url: real_url,
pseudo_path: dst.path,
pseudo_name: dst.filename
}))
};
if(mkdir)
self.mkdir(dst.path).then(payload);
else
payload();
return prom;
},
self._analysePath = function(path_in, opt_filename) {
var is_absolute = (path_in[0] === '/');
var is_path_only = (path_in[path_in.length-1] === '/');
var filename, path;
if(is_path_only) {
filename = opt_filename || '';
path = path_in;
}
else {
var elements = path_in.split('/');
filename = elements[elements.length-1];
path = path_in.substr(0, path_in.length-filename.length);
}
return {
filename: filename,
path: path
};
}
self.mkdir = function(pseudo_path) {
var prom = new Promise();
self.promises.push(prom);
self.worker.postMessage(JSON.stringify({
cmd: 'mkdir',
id: (self.promises.length-1),
pseudo_path: '/',
pseudo_name: pseudo_path
}));
return prom;
},
self.getFiles = function() {
var prom = new Promise();
var contents = {};
var pseudo_files = Array.prototype.slice.call(arguments);
for(var i in pseudo_files)
(function(fname) {
self.getFile(fname).then(function(c) {
contents[fname] = c;
if(Object.keys(contents).length == pseudo_files.length)
prom.fulfil(contents);
});
})(pseudo_files[i]);
return prom;
}
self.getFile = function(pseudo_file) {
var file = self._analysePath(pseudo_file);
var prom1 = new Promise();
self.promises.push(prom1);
self.worker.postMessage(JSON.stringify({
cmd: 'getFile',
id: (self.promises.length-1),
pseudo_path: file.path,
pseudo_name: file.filename
}));
var prom2 = new Promise();
var chunks = [];
prom1.then(function(msg) {
var id = msg.chunk_id;
chunks[id] = msg.contents;
var complete = true;
for(var i = 0; i < msg.chunk_count; i++) {
if(typeof(chunks[i]) === 'undefined') {
complete = false;
break;
}
}
if(complete) {
prom2.fulfil(chunks.join(''), file.path, file.filename);
}
});
return prom2;
},
this.addData = function(contents, pseudo_path) {
var dst = self._analysePath(pseudo_path);
var prom = new Promise();
self.promises.push(prom);
self.worker.postMessage(JSON.stringify({
cmd: 'addData',
id: (self.promises.length-1),
contents: contents,
pseudo_path: dst.path,
pseudo_name: dst.filename
}));
return prom;
},
this.allDone = function() {
var prom = new Promise();
var N = this.promises.length;
for(var i = 0; i < this.promises.length; i++) {
this.promises[i].then(function() {
N--;
if(N == 0)
prom.fulfil();
});
}
if(this.promises.length === 0)
prom.fulfil();
return prom;
}
this.run = function() {
var prom = new Promise();
self.promises.push(prom);
var args = [];
for(var i = 0; i < arguments.length; i++)
args.push(arguments[i]);
self.worker.postMessage(JSON.stringify({
cmd: 'run',
id: (self.promises.length-1),
args: args
}));
return prom;
}
exports.Interface.Promise = Promise;
return this;
}
})(window);