|
@@ -4,23 +4,28 @@ const streams = []; |
|
|
|
|
|
|
|
|
onmessage = function(e) {
|
|
|
onmessage = function(e) {
|
|
|
switch (e.data[0]) {
|
|
|
switch (e.data[0]) {
|
|
|
case 'precreatePaths':
|
|
|
|
|
|
precreatePaths(e.data[1]);
|
|
|
|
|
|
postMessage([ 'precreatePathsResult' ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'parseStream':
|
|
|
|
|
|
parseStream(e.data[1]);
|
|
|
|
|
|
postMessage([ 'parseStreamResult' ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'listDirectory': {
|
|
|
|
|
|
const lst = listDirectory(rootDir, e.data[1], e.data[2]);
|
|
|
|
|
|
postMessage([ 'listDirectoryResult', lst ])
|
|
|
|
|
|
break; }
|
|
|
|
|
|
case 'getData': {
|
|
|
|
|
|
postMessage([ 'getDataResult', rootDir, streams ]);
|
|
|
|
|
|
break; }
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw Error('Unknown verb: ' + e.data[0]);
|
|
|
|
|
|
|
|
|
case 'precreatePaths':
|
|
|
|
|
|
precreatePaths(e.data[1]);
|
|
|
|
|
|
postMessage([ 'precreatePathsResult' ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'parseStream':
|
|
|
|
|
|
parseStream(e.data[1]);
|
|
|
|
|
|
postMessage([ 'parseStreamResult' ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'listDirectory': {
|
|
|
|
|
|
const lst = listDirectory(rootDir, e.data[1], e.data[2]);
|
|
|
|
|
|
postMessage([ 'listDirectoryResult', lst ])
|
|
|
|
|
|
break; }
|
|
|
|
|
|
case 'getData':
|
|
|
|
|
|
postMessage([ 'getDataResult', rootDir, streams ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 'getFile':
|
|
|
|
|
|
postMessage([ 'getFileResult', getFile(rootDir, streams, e.data[1]) ]);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default: {
|
|
|
|
|
|
const err = Error('Unknown verb: ' + e.data[0]);
|
|
|
|
|
|
postMessage([ 'error', err.message ]);
|
|
|
|
|
|
throw err; }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
@@ -141,3 +146,41 @@ function listDirectory(rootDir, path, lenient=false) { |
|
|
res = res.concat(files.map(k => [ 'f', k, dir[k][1] ]));
|
|
|
res = res.concat(files.map(k => [ 'f', k, dir[k][1] ]));
|
|
|
return res;
|
|
|
return res;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getFile(rootDir, streams, path) {
|
|
|
|
|
|
if (typeof(path) === 'string')
|
|
|
|
|
|
path = path.split('/');
|
|
|
|
|
|
|
|
|
|
|
|
if (path.length < 2)
|
|
|
|
|
|
throw Error('Invalid file path');
|
|
|
|
|
|
|
|
|
|
|
|
const name = path[path.length - 1];
|
|
|
|
|
|
|
|
|
|
|
|
const dir = findDir(rootDir, path.slice(0, path.length - 1));
|
|
|
|
|
|
|
|
|
|
|
|
if (!(name in dir))
|
|
|
|
|
|
throw Error('File not found');
|
|
|
|
|
|
|
|
|
|
|
|
if (!(dir[name] instanceof Array))
|
|
|
|
|
|
throw Error('Path points to a directory not a file');
|
|
|
|
|
|
|
|
|
|
|
|
let file = dir[name];
|
|
|
|
|
|
file = [ file[0].map(seg => {
|
|
|
|
|
|
const stm = streams[seg[0]];
|
|
|
|
|
|
const used = stm.map(loc => !( loc[2] <= seg[1] || loc[1] >= seg[1] + seg[2] ) );
|
|
|
|
|
|
const start = used.indexOf(true);
|
|
|
|
|
|
const end = used.lastIndexOf(true) + 1;
|
|
|
|
|
|
if (start === -1)
|
|
|
|
|
|
return [];
|
|
|
|
|
|
const res = [];
|
|
|
|
|
|
for (let i = start; i < end; i++) {
|
|
|
|
|
|
const loc = stm[i];
|
|
|
|
|
|
res.push([ loc[0], Math.max(0, seg[1] - loc[1]),
|
|
|
|
|
|
Math.min(loc[2] - loc[1], seg[1] + seg[2] - loc[1]) ]);
|
|
|
|
|
|
}
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}), file[1] ];
|
|
|
|
|
|
file[0] = file[0].reduce((a, b) => a.concat(b));
|
|
|
|
|
|
|
|
|
|
|
|
return file;
|
|
|
|
|
|
}
|