unpkg/server/BlacklistAPI.js

72 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-02-18 04:21:19 +00:00
const db = require("./utils/redis");
2017-11-11 20:18:13 +00:00
2018-04-04 05:32:32 +00:00
const blacklistSet = "blacklisted-packages";
2017-11-11 20:18:13 +00:00
function addPackage(packageName) {
return new Promise((resolve, reject) => {
2018-04-04 05:32:32 +00:00
db.sadd(blacklistSet, packageName, (error, value) => {
2017-11-11 20:18:13 +00:00
if (error) {
2018-02-18 02:00:56 +00:00
reject(error);
2017-11-11 20:18:13 +00:00
} else {
2018-02-18 02:00:56 +00:00
resolve(value === 1);
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
});
});
2017-11-11 20:18:13 +00:00
}
function removePackage(packageName) {
return new Promise((resolve, reject) => {
2018-04-04 05:32:32 +00:00
db.srem(blacklistSet, packageName, (error, value) => {
2017-11-11 20:18:13 +00:00
if (error) {
2018-02-18 02:00:56 +00:00
reject(error);
2017-11-11 20:18:13 +00:00
} else {
2018-02-18 02:00:56 +00:00
resolve(value === 1);
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
});
});
2017-11-11 20:18:13 +00:00
}
function removeAllPackages() {
return new Promise((resolve, reject) => {
2018-04-04 05:32:32 +00:00
db.del(blacklistSet, error => {
2017-11-11 20:18:13 +00:00
if (error) {
2018-02-18 02:00:56 +00:00
reject(error);
2017-11-11 20:18:13 +00:00
} else {
2018-02-18 02:00:56 +00:00
resolve();
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
});
});
2017-11-11 20:18:13 +00:00
}
function getPackages() {
return new Promise((resolve, reject) => {
2018-04-04 05:32:32 +00:00
db.smembers(blacklistSet, (error, value) => {
2017-11-11 20:18:13 +00:00
if (error) {
2018-02-18 02:00:56 +00:00
reject(error);
2017-11-11 20:18:13 +00:00
} else {
2018-02-18 02:00:56 +00:00
resolve(value);
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
});
});
2017-11-11 20:18:13 +00:00
}
2017-11-12 06:35:30 +00:00
function includesPackage(packageName) {
2017-11-11 20:18:13 +00:00
return new Promise((resolve, reject) => {
2018-04-04 05:32:32 +00:00
db.sismember(blacklistSet, packageName, (error, value) => {
2017-11-11 20:18:13 +00:00
if (error) {
2018-02-18 02:00:56 +00:00
reject(error);
2017-11-11 20:18:13 +00:00
} else {
2018-02-18 02:00:56 +00:00
resolve(value === 1);
2017-11-11 20:18:13 +00:00
}
2018-02-18 02:00:56 +00:00
});
});
2017-11-11 20:18:13 +00:00
}
module.exports = {
addPackage,
removePackage,
removeAllPackages,
getPackages,
2017-11-12 06:35:30 +00:00
includesPackage
2018-02-18 02:00:56 +00:00
};