|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +Object.defineProperty(exports, '__esModule', { value: true }); |
| 4 | + |
| 5 | +const filters = new Map(); |
| 6 | +const limiters = new Map(); |
| 7 | + |
| 8 | +function filter (name, handler) { |
| 9 | + if (typeof name !== 'string') { |
| 10 | + throw new TypeError('First argument must be a string.') |
| 11 | + } |
| 12 | + if (typeof handler !== 'function') { |
| 13 | + throw new TypeError('Second argument must be a function.') |
| 14 | + } |
| 15 | + if (filters.has(name)) { |
| 16 | + throw new Error(`A filter named ${name} is already registered.`) |
| 17 | + } |
| 18 | + filters.set(name, handler); |
| 19 | + return this |
| 20 | +} |
| 21 | + |
| 22 | +function limiter (name, handler) { |
| 23 | + if (typeof name !== 'string') { |
| 24 | + throw new TypeError('First argument must be a string.') |
| 25 | + } |
| 26 | + if (typeof handler !== 'function') { |
| 27 | + throw new TypeError('Second argument must be a function.') |
| 28 | + } |
| 29 | + if (limiters.has(name)) { |
| 30 | + throw new Error(`A limiter named ${name} is already registered.`) |
| 31 | + } |
| 32 | + limiters.set(name, handler); |
| 33 | + return this |
| 34 | +} |
| 35 | + |
| 36 | +function compileRawExpression (src) { |
| 37 | + return new Function('context', 'tempVars', // eslint-disable-line |
| 38 | + `const sandbox = $nxCompileToSandbox(context, tempVars) |
| 39 | + try { with (sandbox) { return ${src} } } catch (err) { |
| 40 | + if (!(err instanceof TypeError)) throw err |
| 41 | + } |
| 42 | + $nxClearSandbox()`) |
| 43 | +} |
| 44 | + |
| 45 | +function compileRawCode (src) { |
| 46 | + return new Function('context', 'tempVars', // eslint-disable-line |
| 47 | + `const sandbox = $nxCompileToSandbox(context, tempVars) |
| 48 | + with (sandbox) { ${src} } |
| 49 | + $nxClearSandbox()`) |
| 50 | +} |
| 51 | + |
| 52 | +const filterRegex = /(?:[^\|]|\|\|)+/g; |
| 53 | +const limiterRegex = /(?:[^&]|&&)+/g; |
| 54 | +const argsRegex = /\S+/g; |
| 55 | + |
| 56 | +function parseExpression (src) { |
| 57 | + const tokens = src.match(filterRegex); |
| 58 | + if (tokens.length === 1) { |
| 59 | + return compileRawExpression(tokens[0]) |
| 60 | + } |
| 61 | + |
| 62 | + const expression = { |
| 63 | + exec: compileRawExpression(tokens[0]), |
| 64 | + filters: [] |
| 65 | + }; |
| 66 | + for (let i = 1; i < tokens.length; i++) { |
| 67 | + let filterTokens = tokens[i].match(argsRegex) || []; |
| 68 | + const filterName = filterTokens.shift(); |
| 69 | + const effect = filters.get(filterName); |
| 70 | + if (!effect) { |
| 71 | + throw new Error(`There is no filter named: ${filterName}.`) |
| 72 | + } |
| 73 | + expression.filters.push({effect, argExpressions: filterTokens.map(compileRawExpression)}); |
| 74 | + } |
| 75 | + return expression |
| 76 | +} |
| 77 | + |
| 78 | +function parseCode (src) { |
| 79 | + const tokens = src.match(limiterRegex); |
| 80 | + if (tokens.length === 1) { |
| 81 | + return compileRawCode(tokens[0]) |
| 82 | + } |
| 83 | + |
| 84 | + const code = { |
| 85 | + exec: compileRawCode(tokens[0]), |
| 86 | + limiters: [] |
| 87 | + }; |
| 88 | + for (let i = 1; i < tokens.length; i++) { |
| 89 | + const limiterTokens = tokens[i].match(argsRegex) || []; |
| 90 | + const limiterName = limiterTokens.shift(); |
| 91 | + const effect = limiters.get(limiterName); |
| 92 | + if (!effect) { |
| 93 | + throw new Error(`There is no limiter named: ${limiterName}.`) |
| 94 | + } |
| 95 | + code.limiters.push({effect, argExpressions: limiterTokens.map(compileRawExpression)}); |
| 96 | + } |
| 97 | + return code |
| 98 | +} |
| 99 | + |
| 100 | +const expressionCache = new Map(); |
| 101 | +const codeCache = new Map(); |
| 102 | + |
| 103 | +function compileExpression (src) { |
| 104 | + if (typeof src !== 'string') { |
| 105 | + throw new TypeError('First argument must be a string.') |
| 106 | + } |
| 107 | + let expression = expressionCache.get(src); |
| 108 | + if (!expression) { |
| 109 | + expression = parseExpression(src); |
| 110 | + expressionCache.set(src, expression); |
| 111 | + } |
| 112 | + |
| 113 | + if (typeof expression === 'function') { |
| 114 | + return expression |
| 115 | + } |
| 116 | + |
| 117 | + return function evaluateExpression (context, tempVars) { |
| 118 | + let value = expression.exec(context, tempVars); |
| 119 | + for (let filter of expression.filters) { |
| 120 | + const args = filter.argExpressions.map(evaluateArgExpression, context); |
| 121 | + value = filter.effect(value, ...args); |
| 122 | + } |
| 123 | + return value |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function compileCode (src) { |
| 128 | + if (typeof src !== 'string') { |
| 129 | + throw new TypeError('First argument must be a string.') |
| 130 | + } |
| 131 | + let code = codeCache.get(src); |
| 132 | + if (!code) { |
| 133 | + code = parseCode(src); |
| 134 | + codeCache.set(src, code); |
| 135 | + } |
| 136 | + |
| 137 | + if (typeof code === 'function') { |
| 138 | + return code |
| 139 | + } |
| 140 | + |
| 141 | + const context = {}; |
| 142 | + return function evaluateCode (state, tempVars) { |
| 143 | + let i = 0; |
| 144 | + function next () { |
| 145 | + Object.assign(context, tempVars); |
| 146 | + if (i < code.limiters.length) { |
| 147 | + const limiter = code.limiters[i++]; |
| 148 | + const args = limiter.argExpressions.map(evaluateArgExpression, state); |
| 149 | + limiter.effect(next, context, ...args); |
| 150 | + } else { |
| 151 | + code.exec(state, tempVars); |
| 152 | + } |
| 153 | + } |
| 154 | + next(); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +function evaluateArgExpression (argExpression) { |
| 159 | + return argExpression(this) |
| 160 | +} |
| 161 | + |
| 162 | +const hasHandler = { has }; |
| 163 | +const allHandlers = { has, get }; |
| 164 | +const globals = new Set(); |
| 165 | +let temp; |
| 166 | + |
| 167 | +let globalObj; |
| 168 | +if (typeof window !== 'undefined') globalObj = window; // eslint-disable-line |
| 169 | +else if (typeof global !== 'undefined') globalObj = global; // eslint-disable-line |
| 170 | +else if (typeof self !== 'undefined') globalObj = self; // eslint-disable-line |
| 171 | +globalObj.$nxCompileToSandbox = toSandbox; |
| 172 | +globalObj.$nxClearSandbox = clearSandbox; |
| 173 | + |
| 174 | +function expose (...globalNames) { |
| 175 | + for (let globalName of globalNames) { |
| 176 | + globals.add(globalName); |
| 177 | + } |
| 178 | + return this |
| 179 | +} |
| 180 | + |
| 181 | +function hide (...globalNames) { |
| 182 | + for (let globalName of globalNames) { |
| 183 | + globals.delete(globalName); |
| 184 | + } |
| 185 | + return this |
| 186 | +} |
| 187 | + |
| 188 | +function hideAll () { |
| 189 | + globals.clear(); |
| 190 | + return this |
| 191 | +} |
| 192 | + |
| 193 | +function has (target, key) { |
| 194 | + return globals.has(key) ? (key in target) : true |
| 195 | +} |
| 196 | + |
| 197 | +function get (target, key) { |
| 198 | + return key in temp ? temp[key] : target[key] |
| 199 | +} |
| 200 | + |
| 201 | +function toSandbox (obj, tempVars) { |
| 202 | + if (tempVars) { |
| 203 | + temp = tempVars; |
| 204 | + return new Proxy(obj, allHandlers) |
| 205 | + } |
| 206 | + return new Proxy(obj, hasHandler) |
| 207 | +} |
| 208 | + |
| 209 | +function clearSandbox () { |
| 210 | + temp = undefined; |
| 211 | +} |
| 212 | + |
| 213 | +exports.compileExpression = compileExpression; |
| 214 | +exports.compileCode = compileCode; |
| 215 | +exports.compileRawExpression = compileRawExpression; |
| 216 | +exports.compileRawCode = compileRawCode; |
| 217 | +exports.expose = expose; |
| 218 | +exports.hide = hide; |
| 219 | +exports.hideAll = hideAll; |
| 220 | +exports.filters = filters; |
| 221 | +exports.limiters = limiters; |
| 222 | +exports.filter = filter; |
| 223 | +exports.limiter = limiter; |
0 commit comments