Convert backend code to ES modules
Bundling code now with Rollup for its flat bundling and tree shaking support. Front end code still goes through Webpack for browser support.
This commit is contained in:
1 parent
824639976f
commit
1e41c72d60
62 files changed
+718
-898
No files matched your search
@@ -1,11 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
const validators = {
|
||||
pass: require("./validators/pass"),
|
||||
test: require("./validators/test")
|
||||
};
|
||||
import pass from "./validators/pass";
|
||||
import test from "./validators/test";
|
||||
|
||||
class Authorizer {
|
||||
const validators = { pass, test };
|
||||
|
||||
export default class Authorizer {
|
||||
constructor(permissionSets) {
|
||||
this.permissionSets = permissionSets;
|
||||
this.validatorCache = new WeakMap();
|
||||
@@ -77,5 +77,3 @@ class Authorizer {
|
||||
return validator;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Authorizer;
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
const expressionParser = require("./expression-parser");
|
||||
import { map, parseLikePattern } from "./expression-parser";
|
||||
|
||||
function or(cnf1, cnf2) {
|
||||
if (cnf1.length === 0) return cnf2;
|
||||
@@ -19,10 +19,10 @@ function or(cnf1, cnf2) {
|
||||
return res;
|
||||
}
|
||||
|
||||
function booleanCnf(exp) {
|
||||
export function booleanCnf(exp) {
|
||||
const ranges = new Map();
|
||||
const likePatterns = new Map();
|
||||
expressionParser.map(exp, e => {
|
||||
map(exp, e => {
|
||||
if (!Array.isArray(e)) return e;
|
||||
if (e[0] === "LIKE" || e[0] === "NOT LIKE") {
|
||||
if (
|
||||
@@ -32,10 +32,7 @@ function booleanCnf(exp) {
|
||||
const key = JSON.stringify(e[1]);
|
||||
let s = likePatterns.get(key);
|
||||
if (!s) likePatterns.set(key, (s = new Map()));
|
||||
s.set(
|
||||
`${e[3] || ""}:${e[2]}`,
|
||||
expressionParser.parseLikePattern(e[2], e[3])
|
||||
);
|
||||
s.set(`${e[3] || ""}:${e[2]}`, parseLikePattern(e[2], e[3]));
|
||||
}
|
||||
} else if ([">", ">=", "<", "<=", "=", "<>"].includes(e[0])) {
|
||||
if (!Array.isArray(e[2])) {
|
||||
@@ -251,5 +248,3 @@ function likePatternIncludes(pat1, pat2, idx1 = 0, idx2 = 0) {
|
||||
if (idx1 === pat1.length && idx2 === pat2.length) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
exports.booleanCnf = booleanCnf;
|
||||
@@ -1,6 +1,6 @@
|
||||
"use strict";
|
||||
|
||||
const parsimmon = require("parsimmon");
|
||||
import parsimmon from "parsimmon";
|
||||
|
||||
// Turn escaped characters into real ones (e.g. "\\n" becomes "\n").
|
||||
function interpretEscapes(str) {
|
||||
@@ -22,7 +22,7 @@ function interpretEscapes(str) {
|
||||
});
|
||||
}
|
||||
|
||||
function map(exp, callback) {
|
||||
export function map(exp, callback) {
|
||||
if (!Array.isArray(exp)) return callback(exp);
|
||||
|
||||
let clone;
|
||||
@@ -271,12 +271,12 @@ const lang = parsimmon.createLanguage({
|
||||
}
|
||||
});
|
||||
|
||||
function parse(str) {
|
||||
export function parse(str) {
|
||||
if (!str) return null;
|
||||
return lang.Expression.tryParse(str);
|
||||
}
|
||||
|
||||
function stringify(exp, level = 0) {
|
||||
export function stringify(exp, level = 0) {
|
||||
if (!Array.isArray(exp)) return JSON.stringify(exp);
|
||||
|
||||
const opLevels = {
|
||||
@@ -360,7 +360,7 @@ function stringify(exp, level = 0) {
|
||||
}
|
||||
}
|
||||
|
||||
function parseLikePattern(pat, esc) {
|
||||
export function parseLikePattern(pat, esc) {
|
||||
const chars = pat.split("");
|
||||
|
||||
for (let i = 0; i < chars.length; ++i) {
|
||||
@@ -377,8 +377,3 @@ function parseLikePattern(pat, esc) {
|
||||
}
|
||||
return chars.filter(c => c);
|
||||
}
|
||||
|
||||
exports.parse = parse;
|
||||
exports.stringify = stringify;
|
||||
exports.map = map;
|
||||
exports.parseLikePattern = parseLikePattern;
|
||||
+15
-25
@@ -1,8 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
const expressionParser = require("./expression-parser");
|
||||
const expressionCnf = require("./expression-cnf");
|
||||
const satSolver = require("./sat-solver");
|
||||
import { map, parseLikePattern } from "./expression-parser";
|
||||
import { booleanCnf } from "./expression-cnf";
|
||||
import { naiveDpll } from "./sat-solver";
|
||||
|
||||
const isArray = Array.isArray;
|
||||
|
||||
@@ -26,7 +26,7 @@ function reduce(exp, callback) {
|
||||
return exp;
|
||||
}
|
||||
|
||||
function evaluate(exp, obj, now, cb) {
|
||||
export function evaluate(exp, obj, now, cb) {
|
||||
function getRegExp(pat, esc) {
|
||||
const k = `${esc || ""}:${pat}`;
|
||||
let c = regExpCache.get(exp);
|
||||
@@ -35,7 +35,7 @@ function evaluate(exp, obj, now, cb) {
|
||||
return c[k];
|
||||
}
|
||||
|
||||
return expressionParser.map(exp, e => {
|
||||
return map(exp, e => {
|
||||
if (cb) e = cb(e);
|
||||
if (!isArray(e)) return e;
|
||||
|
||||
@@ -162,7 +162,7 @@ function evaluate(exp, obj, now, cb) {
|
||||
});
|
||||
}
|
||||
|
||||
function and(exp1, exp2) {
|
||||
export function and(exp1, exp2) {
|
||||
if (!isArray(exp1)) return exp1 ? exp2 : exp1;
|
||||
if (!isArray(exp2)) return exp2 ? exp1 : exp2;
|
||||
|
||||
@@ -177,7 +177,7 @@ function and(exp1, exp2) {
|
||||
return res;
|
||||
}
|
||||
|
||||
function or(exp1, exp2) {
|
||||
export function or(exp1, exp2) {
|
||||
if (!isArray(exp1)) return exp1 ? exp1 : exp2;
|
||||
if (!isArray(exp2)) return exp2 ? exp2 : exp1;
|
||||
|
||||
@@ -192,19 +192,19 @@ function or(exp1, exp2) {
|
||||
return res;
|
||||
}
|
||||
|
||||
function not(exp) {
|
||||
export function not(exp) {
|
||||
if (isArray(exp) && exp[0] === "NOT") return exp[1];
|
||||
return ["NOT", exp];
|
||||
}
|
||||
|
||||
function subset(exp1, exp2) {
|
||||
export function subset(exp1, exp2) {
|
||||
const e = evaluate(["NOT", ["OR", ["NOT", exp1], exp2]]);
|
||||
if (!isArray(e)) return !e;
|
||||
const { vars, clauses } = expressionCnf.booleanCnf(e);
|
||||
return !satSolver.naiveDpll(clauses, vars);
|
||||
const { vars, clauses } = booleanCnf(e);
|
||||
return !naiveDpll(clauses, vars);
|
||||
}
|
||||
|
||||
function likePatternToRegExp(pat, esc = "", flags = "") {
|
||||
export function likePatternToRegExp(pat, esc = "", flags = "") {
|
||||
const convChars = {
|
||||
"-": "\\-",
|
||||
"/": "\\/",
|
||||
@@ -225,7 +225,7 @@ function likePatternToRegExp(pat, esc = "", flags = "") {
|
||||
"\\%": ".*",
|
||||
"\\_": "."
|
||||
};
|
||||
let chars = expressionParser.parseLikePattern(pat, esc);
|
||||
let chars = parseLikePattern(pat, esc);
|
||||
if (!chars.length) return new RegExp("^$", flags);
|
||||
chars = chars.map(c => convChars[c] || c);
|
||||
chars[0] = chars[0] === ".*" ? "" : "^" + chars[0];
|
||||
@@ -234,21 +234,11 @@ function likePatternToRegExp(pat, esc = "", flags = "") {
|
||||
return new RegExp(chars.join(""), flags);
|
||||
}
|
||||
|
||||
function extractParams(exp) {
|
||||
export function extractParams(exp) {
|
||||
const params = new Set();
|
||||
expressionParser.map(exp, e => {
|
||||
map(exp, e => {
|
||||
if (isArray(e) && e[0] === "PARAM") params.add(e[1]);
|
||||
return e;
|
||||
});
|
||||
return Array.from(params);
|
||||
}
|
||||
|
||||
exports.evaluate = evaluate;
|
||||
exports.and = and;
|
||||
exports.or = or;
|
||||
exports.not = not;
|
||||
exports.subset = subset;
|
||||
exports.parse = expressionParser.parse;
|
||||
exports.stringify = expressionParser.stringify;
|
||||
exports.likePatternToRegExp = likePatternToRegExp;
|
||||
exports.extractParams = extractParams;
|
||||
@@ -22,7 +22,7 @@ function getKey(obj) {
|
||||
return k;
|
||||
}
|
||||
|
||||
function memoize(func) {
|
||||
export default function memoize(func) {
|
||||
return (...args) => {
|
||||
const key = JSON.stringify(args.map(getKey)) + getKey(func);
|
||||
|
||||
@@ -43,5 +43,3 @@ const interval = setInterval(() => {
|
||||
|
||||
// Don't hold Node.js process
|
||||
if (interval.unref) interval.unref();
|
||||
|
||||
module.exports = memoize;
|
||||
@@ -167,10 +167,8 @@ function unitPropagate(clauses, varVals) {
|
||||
return derivedVars;
|
||||
}
|
||||
|
||||
function naiveDpll(clauses, maxvarnr) {
|
||||
export function naiveDpll(clauses, maxvarnr) {
|
||||
// variable values are 0 if not set, 1 if positive, -1 if negative
|
||||
const varVals = new Int32Array(maxvarnr + 1);
|
||||
return satisfiableAt(clauses, varVals, 0, 0);
|
||||
}
|
||||
|
||||
exports.naiveDpll = naiveDpll;
|
||||
@@ -1,5 +1,3 @@
|
||||
function pass() {
|
||||
export default function pass() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
module.exports = pass;
|
||||
@@ -1,4 +1,4 @@
|
||||
function test(resourceType, resource) {
|
||||
export default function test(resourceType, resource) {
|
||||
if (resourceType === "devices") {
|
||||
if (resource["Tags.test"] && resource["Tags.test"].value[0]) return 1;
|
||||
} else if (resourceType === "presets") {
|
||||
@@ -10,5 +10,3 @@ function test(resourceType, resource) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
module.exports = test;
|
||||
Reference in new issue
Block a user