Comparing version 0.0.1-beta.1 to 0.0.1-beta.2
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -25,14 +34,21 @@ var fs = require("fs-extra"); | ||
}; | ||
var map = new Map(); | ||
var ensure = function (key) { | ||
if (map.has(key)) { | ||
return map.get(key); | ||
} | ||
var file = fileName(key); | ||
if (!fs.existsSync(file)) { | ||
fs.outputFileSync(file, '', 'utf-8'); | ||
return []; | ||
map.set(key, []); | ||
return map.get(key); | ||
} | ||
else { | ||
var content = fs.readFileSync(file, 'utf-8'); | ||
return content.split('\n') | ||
var its = content.split('\n') | ||
.filter(function (line) { return !!line.trim(); }) | ||
.map(function (line) { return decode(line) || { title: '', url: '' }; }) | ||
.filter(function (it) { return it.title && it.url; }); | ||
map.set(key, its); | ||
return its; | ||
} | ||
@@ -43,5 +59,15 @@ }; | ||
var items = ensure(key); | ||
var idx = items.findIndex(function (it) { return it.title === item.title && it.url === it.url; }); | ||
var isToRemove = idx !== -1; | ||
if (isToRemove) { | ||
items.splice(idx, 1); | ||
} | ||
items.push(item); | ||
if (items.length >= MAX * 2) { | ||
fs.writeFileSync(fileName(key), items.slice(0, MAX).map(encode).join('\n')); | ||
var isOverDoubleMax = items.length > MAX * 2; | ||
if (isOverDoubleMax) { | ||
var len = items.length - MAX; | ||
items.splice(0, len); | ||
} | ||
if (isToRemove || isOverDoubleMax) { | ||
fs.writeFileSync(fileName(key), items.map(encode).join('\n') + '\n'); | ||
callback(); | ||
@@ -53,4 +79,9 @@ } | ||
}, | ||
set: function (key, items, callback) { | ||
map.set(key, items); | ||
fs.writeFileSync(fileName(key), items.slice(0, MAX).map(encode).join('\n')); | ||
callback(); | ||
}, | ||
get: function (key) { | ||
return ensure(key).slice(0, MAX); | ||
return __spreadArray([], ensure(key), true).reverse().slice(0, MAX); | ||
}, | ||
@@ -78,2 +109,15 @@ }; | ||
}); | ||
app.post('/set', function (req, res) { | ||
var _a = req.body, key = _a.key, items = _a.items; | ||
if (typeof key === 'string' && | ||
Array.isArray(items) && | ||
items.every(function (it) { return typeof it.title === 'string' && typeof it.url === 'string'; })) { | ||
cache.set(key, items, function () { | ||
res.sendStatus(200); | ||
}); | ||
} | ||
else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
app.get('/list', function (req, res) { | ||
@@ -80,0 +124,0 @@ var _a = req.query, key = _a.key, search = _a.search; |
{ | ||
"name": "afr-ls", | ||
"version": "0.0.1-beta.1", | ||
"version": "0.0.1-beta.2", | ||
"description": "Alfred List", | ||
"bin": "lib/bin.js", | ||
"scripts": { | ||
"start": "ts-node ./src/server.ts", | ||
"watch": "tsc --watch", | ||
"start": "node ./lib/server.ts", | ||
"build": "tsc" | ||
@@ -24,5 +25,4 @@ }, | ||
"@types/node-fetch": "^2.6.4", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
@@ -29,4 +29,10 @@ import * as fs from 'fs-extra'; | ||
const ensure = (key: string) => { | ||
const map = new Map<string, Array<Item>>(); | ||
const ensure = (key: string): Item[] => { | ||
if (map.has(key)) { | ||
return map.get(key)!; | ||
} | ||
const file = fileName(key); | ||
@@ -36,9 +42,12 @@ | ||
fs.outputFileSync(file, '', 'utf-8'); | ||
return []; | ||
map.set(key, []); | ||
return map.get(key)!; | ||
} else { | ||
const content = fs.readFileSync(file, 'utf-8'); | ||
return content.split('\n') | ||
const its = content.split('\n') | ||
.filter(line => !!line.trim()) | ||
.map(line => decode(line) || { title: '', url: '' }) | ||
.filter(it => it.title && it.url); | ||
map.set(key, its); | ||
return its; | ||
} | ||
@@ -55,6 +64,18 @@ }; | ||
const items = ensure(key); | ||
const idx = items.findIndex(it => it.title === item.title && it.url === it.url); | ||
const isToRemove = idx !== -1; | ||
if (isToRemove) { | ||
items.splice(idx, 1); | ||
} | ||
items.push(item); | ||
if (items.length >= MAX * 2) { | ||
fs.writeFileSync(fileName(key), items.slice(0, MAX).map(encode).join('\n')); | ||
const isOverDoubleMax = items.length > MAX * 2; | ||
if (isOverDoubleMax) { | ||
const len = items.length - MAX; | ||
items.splice(0, len); | ||
} | ||
if (isToRemove || isOverDoubleMax) { | ||
fs.writeFileSync(fileName(key), items.map(encode).join('\n') + '\n'); | ||
callback(); | ||
@@ -66,4 +87,14 @@ } else { | ||
set: ( | ||
key: string, | ||
items: Array<Item>, | ||
callback: () => void | ||
) => { | ||
map.set(key, items); | ||
fs.writeFileSync(fileName(key), items.slice(0, MAX).map(encode).join('\n')); | ||
callback(); | ||
}, | ||
get: (key: string) => { | ||
return ensure(key).slice(0, MAX); | ||
return [...ensure(key)].reverse().slice(0, MAX); | ||
}, | ||
@@ -98,2 +129,17 @@ | ||
app.post('/set', (req, res) => { | ||
const { key, items } = req.body; | ||
if ( | ||
typeof key === 'string' && | ||
Array.isArray(items) && | ||
items.every(it => typeof it.title === 'string' && typeof it.url === 'string') | ||
) { | ||
cache.set(key, items, () => { | ||
res.sendStatus(200); | ||
}); | ||
} else { | ||
res.sendStatus(400); | ||
} | ||
}); | ||
app.get('/list', (req, res) => { | ||
@@ -115,2 +161,2 @@ const { key, search } = req.query; | ||
app.listen(PORT, 'localhost', () => { console.log(`Listening on port ${PORT}`) }); | ||
app.listen(PORT, 'localhost', () => { console.log(`Listening on port ${PORT}`) }); |
11254
5
332