Comparing version 0.0.1-beta.8 to 0.0.1-beta.9
@@ -56,3 +56,3 @@ "use strict"; | ||
var child_process_1 = require("child_process"); | ||
var filebase = (function () { | ||
var fb = (function () { | ||
var HOME = (0, child_process_1.execSync)('echo $HOME').toString().trim(); | ||
@@ -128,117 +128,129 @@ var CACHE_DIR = "".concat(HOME, "/.afr-ls-content"); | ||
})(); | ||
var start = function (port, max) { | ||
var start = function (port, max, filebase) { | ||
if (port === void 0) { port = 8515; } | ||
if (max === void 0) { max = 100; } | ||
var database = (function () { | ||
var map = new Map(); | ||
var encode = function (it) { return it.url + ' ' + it.icon + ' ' + it.title; }; | ||
var decode = function (s) { | ||
var i = s.indexOf(' '); | ||
if (i === -1) { | ||
return null; | ||
if (filebase === void 0) { filebase = fb; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var database, app, server; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
database = (function () { | ||
var map = new Map(); | ||
var encode = function (it) { return it.url + ' ' + it.icon + ' ' + it.title; }; | ||
var decode = function (s) { | ||
var i = s.indexOf(' '); | ||
if (i === -1) { | ||
return null; | ||
} | ||
var url = s.substring(0, i); | ||
var rest = s.substring(i + 1); | ||
var j = rest.indexOf(' '); | ||
if (j === -1) { | ||
return null; | ||
} | ||
var icon = rest.substring(0, j); | ||
var title = rest.substring(j + 1); | ||
if (url && icon && title) { | ||
return { title: title, url: url, icon: icon }; | ||
} | ||
else { | ||
return null; | ||
} | ||
}; | ||
var ensure = function (key) { | ||
if (!map.has(key)) { | ||
var lines = filebase.readLines(key); | ||
var items = lines | ||
.map(function (line) { return decode(line) || { title: '', url: '', icon: '' }; }) | ||
.filter(function (it) { return it.title && it.url && it.icon; }); | ||
map.set(key, items); | ||
} | ||
return map.get(key); | ||
}; | ||
return { | ||
push: function (key, item) { | ||
var items = ensure(key); | ||
var idx = items.findIndex(function (it) { return it.url === item.url; }); | ||
var isToRemove = idx !== -1; | ||
if (isToRemove) { | ||
items.splice(idx, 1); | ||
} | ||
items.push(item); | ||
var isOverDoubleMax = items.length > max * 2; | ||
if (isOverDoubleMax) { | ||
var len = items.length - max; | ||
items.splice(0, len); | ||
} | ||
if (isToRemove || isOverDoubleMax) { | ||
filebase.writeLines(key, items.map(encode)); | ||
} | ||
else { | ||
filebase.appendLine(key, encode(item)); | ||
} | ||
}, | ||
set: function (key, items) { | ||
filebase.writeLines(key, items.slice(0, max).map(encode)); | ||
map.set(key, items); | ||
}, | ||
get: function (key) { | ||
return __spreadArray([], ensure(key), true).reverse().slice(0, max); | ||
}, | ||
}; | ||
})(); | ||
app = express(); | ||
app.use(cors()); | ||
app.use(express.static(path.resolve(__dirname, '../public'))); | ||
app.use(express.json()); | ||
app.post('/append', function (req, res) { | ||
var key = req.query.key; | ||
var _a = req.body, title = _a.title, url = _a.url, icon = _a.icon; | ||
if (typeof key === 'string' && | ||
typeof title === 'string' && | ||
typeof url === 'string' && | ||
typeof icon === 'string') { | ||
database.push(key, { title: title, url: url, icon: icon }); | ||
exports.assetsbase.download(icon).then(function (f) { return res.sendStatus(200); }); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
app.post('/set', function (req, res) { | ||
var key = req.query.key; | ||
var items = req.body; | ||
if (typeof key === 'string' && | ||
Array.isArray(items) && | ||
items.every(function (it) { return typeof it.title === 'string' && typeof it.url === 'string'; })) { | ||
database.set(key, items); | ||
res.sendStatus(200); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
app.get('/list', function (req, res) { | ||
var _a = req.query, key = _a.key, search = _a.search; | ||
if (typeof key === 'string') { | ||
var items = database | ||
.get(key) | ||
.filter(typeof search === 'string' ? | ||
(function (it) { return it.title.toLowerCase().includes(search.toLowerCase()); }) : | ||
function () { return true; }); | ||
res.send(JSON.stringify(items)); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
return [4 /*yield*/, (new Promise(function (resolve) { | ||
var server = app.listen(port, 'localhost', function () { return resolve(server); }); | ||
}))]; | ||
case 1: | ||
server = _a.sent(); | ||
return [2 /*return*/, server]; | ||
} | ||
var url = s.substring(0, i); | ||
var rest = s.substring(i + 1); | ||
var j = rest.indexOf(' '); | ||
if (j === -1) { | ||
return null; | ||
} | ||
var icon = rest.substring(0, j); | ||
var title = rest.substring(j + 1); | ||
if (url && icon && title) { | ||
return { title: title, url: url, icon: icon }; | ||
} | ||
else { | ||
return null; | ||
} | ||
}; | ||
var ensure = function (key) { | ||
if (!map.has(key)) { | ||
var lines = filebase.readLines(key); | ||
var items = lines | ||
.map(function (line) { return decode(line) || { title: '', url: '', icon: '' }; }) | ||
.filter(function (it) { return it.title && it.url && it.icon; }); | ||
map.set(key, items); | ||
} | ||
return map.get(key); | ||
}; | ||
return { | ||
push: function (key, item) { | ||
var items = ensure(key); | ||
var idx = items.findIndex(function (it) { return it.url === item.url; }); | ||
var isToRemove = idx !== -1; | ||
if (isToRemove) { | ||
items.splice(idx, 1); | ||
} | ||
items.push(item); | ||
var isOverDoubleMax = items.length > max * 2; | ||
if (isOverDoubleMax) { | ||
var len = items.length - max; | ||
items.splice(0, len); | ||
} | ||
if (isToRemove || isOverDoubleMax) { | ||
filebase.writeLines(key, items.map(encode)); | ||
} | ||
else { | ||
filebase.appendLine(key, encode(item)); | ||
} | ||
}, | ||
set: function (key, items) { | ||
filebase.writeLines(key, items.slice(0, max).map(encode)); | ||
map.set(key, items); | ||
}, | ||
get: function (key) { | ||
return __spreadArray([], ensure(key), true).reverse().slice(0, max); | ||
}, | ||
}; | ||
})(); | ||
var app = express(); | ||
app.use(cors()); | ||
app.use(express.static(path.resolve(__dirname, '../public'))); | ||
app.use(express.json()); | ||
app.post('/append', function (req, res) { | ||
var key = req.query.key; | ||
var _a = req.body, title = _a.title, url = _a.url, icon = _a.icon; | ||
if (typeof key === 'string' && | ||
typeof title === 'string' && | ||
typeof url === 'string' && | ||
typeof icon === 'string') { | ||
database.push(key, { title: title, url: url, icon: icon }); | ||
exports.assetsbase.download(icon).then(function (f) { return res.sendStatus(200); }); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
}); | ||
app.post('/set', function (req, res) { | ||
var key = req.query.key; | ||
var items = req.body; | ||
if (typeof key === 'string' && | ||
Array.isArray(items) && | ||
items.every(function (it) { return typeof it.title === 'string' && typeof it.url === 'string'; })) { | ||
database.set(key, items); | ||
res.sendStatus(200); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
app.get('/list', function (req, res) { | ||
var _a = req.query, key = _a.key, search = _a.search; | ||
if (typeof key === 'string') { | ||
var items = database | ||
.get(key) | ||
.filter(typeof search === 'string' ? | ||
(function (it) { return it.title.toLowerCase().includes(search.toLowerCase()); }) : | ||
function () { return true; }); | ||
res.send(JSON.stringify(items)); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
return new Promise(function (res) { return app.listen(port, 'localhost', function () { | ||
res(); | ||
}); }); | ||
}; | ||
exports.start = start; |
{ | ||
"name": "afr-ls", | ||
"version": "0.0.1-beta.8", | ||
"version": "0.0.1-beta.9", | ||
"description": "Alfred List", | ||
@@ -5,0 +5,0 @@ "bin": "lib/bin.js", |
@@ -11,3 +11,3 @@ import * as fs from 'fs-extra'; | ||
const filebase = (() => { | ||
const fb = (() => { | ||
@@ -96,6 +96,7 @@ const HOME = execSync('echo $HOME').toString().trim(); | ||
export const start = ( | ||
export const start = async ( | ||
port: number = 8515, | ||
max: number = 100, | ||
): Promise<void> => { | ||
filebase: typeof fb = fb, | ||
) => { | ||
@@ -240,9 +241,7 @@ const database = (() => { | ||
return new Promise(res => app.listen( | ||
port, | ||
'localhost', | ||
() => { | ||
res(); | ||
} | ||
)); | ||
const server = await (new Promise<ReturnType<(typeof app)['listen']>>(resolve => { | ||
const server = app.listen(port, 'localhost', () => resolve(server)); | ||
})); | ||
return server; | ||
}; |
@@ -5,4 +5,4 @@ import axios from 'axios'; | ||
type FileBase = Exclude<Parameters<typeof start>[0], undefined>; | ||
type Server = ReturnType<typeof start>; | ||
type FileBase = Exclude<Parameters<typeof start>[2], undefined>; | ||
type Server = Awaited<ReturnType<typeof start>>; | ||
@@ -42,7 +42,11 @@ | ||
beforeAll((done) => { | ||
server = start( | ||
start( | ||
PORT, | ||
MAX, | ||
fileBase, | ||
MAX, | ||
PORT, | ||
() => axios.get(`http://localhost:${PORT}`).then(() => done()).catch(e => done(e)) | ||
).then( | ||
(s) => { | ||
axios.get(`http://localhost:${PORT}`).then(() => done()).catch(e => done(e)); | ||
server = s; | ||
} | ||
); | ||
@@ -49,0 +53,0 @@ }); |
35991
734