Merge branch 'staging' of github.com:xwiki-labs/cryptpad into staging
commit
ff151869de
@ -0,0 +1,61 @@
|
||||
define([
|
||||
'/bower_components/chainpad/chainpad.dist.js',
|
||||
], function (ChainPad) {
|
||||
var Diff = ChainPad.Diff;
|
||||
|
||||
var isSpace = function (S, i) {
|
||||
return /^\s$/.test(S.charAt(i));
|
||||
};
|
||||
|
||||
var leadingBoundary = function (S, offset) {
|
||||
if (/\s/.test(S.charAt(offset))) { return offset; }
|
||||
while (offset > 0) {
|
||||
offset--;
|
||||
if (isSpace(S, offset)) { offset++; break; }
|
||||
}
|
||||
return offset;
|
||||
};
|
||||
|
||||
var trailingBoundary = function (S, offset) {
|
||||
if (isSpace(S, offset)) { return offset; }
|
||||
while (offset < S.length && !/\s/.test(S.charAt(offset))) {
|
||||
offset++;
|
||||
}
|
||||
return offset;
|
||||
};
|
||||
|
||||
var opsToWords = function (previous, current) {
|
||||
var output = [];
|
||||
Diff.diff(previous, current).forEach(function (op) {
|
||||
// ignore deleted sections...
|
||||
var offset = op.offset;
|
||||
var toInsert = op.toInsert;
|
||||
|
||||
// given an operation, check whether it is a word fragment,
|
||||
// if it is, expand it to its word boundaries
|
||||
var first = current.slice(leadingBoundary(current, offset), offset);
|
||||
var last = current.slice(offset + toInsert.length, trailingBoundary(current, offset + toInsert.length));
|
||||
|
||||
var result = first + toInsert + last;
|
||||
// concat-in-place
|
||||
Array.prototype.push.apply(output, result.split(/\s+/));
|
||||
});
|
||||
return output.filter(Boolean);
|
||||
};
|
||||
|
||||
var runningDiff = function (getter, f, time) {
|
||||
var last = getter();
|
||||
// first time through, send all the words :D
|
||||
f(opsToWords("", last));
|
||||
return setInterval(function () {
|
||||
var current = getter();
|
||||
|
||||
// find inserted words...
|
||||
var words = opsToWords(last, current);
|
||||
last = current;
|
||||
f(words);
|
||||
}, time);
|
||||
};
|
||||
|
||||
return runningDiff;
|
||||
});
|
@ -0,0 +1,93 @@
|
||||
var Fs = require("fs");
|
||||
var Path = require("path");
|
||||
|
||||
var nThen = require("nthen");
|
||||
var config = require("./config");
|
||||
|
||||
var root = Path.resolve(config.taskPath || './tasks');
|
||||
|
||||
var dirs;
|
||||
var nt;
|
||||
|
||||
var queue = function (f) {
|
||||
nt = nt.nThen(f);
|
||||
};
|
||||
|
||||
var tryParse = function (s) {
|
||||
try { return JSON.parse(s); }
|
||||
catch (e) { return null; }
|
||||
};
|
||||
|
||||
var CURRENT = +new Date();
|
||||
|
||||
var handleTask = function (str, path, cb) {
|
||||
var task = tryParse(str);
|
||||
if (!Array.isArray(task)) {
|
||||
console.error('invalid task: not array');
|
||||
return cb();
|
||||
}
|
||||
if (task.length < 2) {
|
||||
console.error('invalid task: too small');
|
||||
return cb();
|
||||
}
|
||||
|
||||
var time = task[0];
|
||||
var command = task[1];
|
||||
var args = task.slice(2);
|
||||
|
||||
if (time > CURRENT) {
|
||||
// not time for this task yet
|
||||
console.log('not yet time');
|
||||
return cb();
|
||||
}
|
||||
|
||||
nThen(function () {
|
||||
switch (command) {
|
||||
case 'EXPIRE':
|
||||
console.log("expiring: %s", args[0]);
|
||||
// TODO actually remove the file...
|
||||
break;
|
||||
default:
|
||||
console.log("unknown command", command);
|
||||
}
|
||||
}).nThen(function () {
|
||||
// remove the file...
|
||||
Fs.unlink(path, function (err) {
|
||||
if (err) { console.error(err); }
|
||||
cb();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
nt = nThen(function (w) {
|
||||
Fs.readdir(root, w(function (e, list) {
|
||||
if (e) { throw e; }
|
||||
dirs = list;
|
||||
}));
|
||||
}).nThen(function () {
|
||||
dirs.forEach(function (dir) {
|
||||
queue(function (w) {
|
||||
console.log('recursing into %s', dir);
|
||||
Fs.readdir(Path.join(root, dir), w(function (e, list) {
|
||||
list.forEach(function (fn) {
|
||||
queue(function (w) {
|
||||
var filePath = Path.join(root, dir, fn);
|
||||
var cb = w();
|
||||
|
||||
console.log("processing file at %s", filePath);
|
||||
Fs.readFile(filePath, 'utf8', function (e, str) {
|
||||
if (e) {
|
||||
console.error(e);
|
||||
return void cb();
|
||||
}
|
||||
|
||||
handleTask(str, filePath, cb);
|
||||
});
|
||||
});
|
||||
});
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue