From 281bdb5baf338198c783c190b15e7551f866a358 Mon Sep 17 00:00:00 2001 From: Tom North Date: Sat, 25 Mar 2023 15:07:14 +0000 Subject: [PATCH] Move secret masking immediately after instantiation --- dist/index.js | 411 +++++++++++++++++++++++++------------------------- src/action.js | 2 +- 2 files changed, 208 insertions(+), 205 deletions(-) diff --git a/dist/index.js b/dist/index.js index 2b5e349..5886c40 100644 --- a/dist/index.js +++ b/dist/index.js @@ -10150,7 +10150,7 @@ const functions = (() => { var isSequence = utils.isSequence; var isFunction = utils.isFunction; var isLambda = utils.isLambda; - var isPromise = utils.isPromise; + var isIterable = utils.isIterable; var getFunctionArity = utils.getFunctionArity; var deepEquals = utils.isDeepEqual; var stringToArray = utils.stringToArray; @@ -10451,10 +10451,10 @@ const functions = (() => { * @param {string} str - the string to match against * @returns {object} - structure that represents the match(es) */ - async function evaluateMatcher(matcher, str) { + function* evaluateMatcher(matcher, str) { var result = matcher.apply(this, [str]); // eslint-disable-line no-useless-call - if(isPromise(result)) { - result = await result; + if(isIterable(result)) { + result = yield * result; } if(result && !(typeof result.start === 'number' || result.end === 'number' || Array.isArray(result.groups) || isFunction(result.next))) { // the matcher function didn't return the correct structure @@ -10472,7 +10472,7 @@ const functions = (() => { * @param {String} token - substring or regex to find * @returns {Boolean} - true if str contains token */ - async function contains(str, token) { + function* contains(str, token) { // undefined inputs always return undefined if (typeof str === 'undefined') { return undefined; @@ -10483,7 +10483,7 @@ const functions = (() => { if (typeof token === 'string') { result = (str.indexOf(token) !== -1); } else { - var matches = await evaluateMatcher(token, str); + var matches = yield* evaluateMatcher(token, str); result = (typeof matches !== 'undefined'); } @@ -10497,7 +10497,7 @@ const functions = (() => { * @param {Integer} [limit] - max number of matches to return * @returns {Array} The array of match objects */ - async function match(str, regex, limit) { + function* match(str, regex, limit) { // undefined inputs always return undefined if (typeof str === 'undefined') { return undefined; @@ -10517,7 +10517,7 @@ const functions = (() => { if (typeof limit === 'undefined' || limit > 0) { var count = 0; - var matches = await evaluateMatcher(regex, str); + var matches = yield* evaluateMatcher(regex, str); if (typeof matches !== 'undefined') { while (typeof matches !== 'undefined' && (typeof limit === 'undefined' || count < limit)) { result.push({ @@ -10525,7 +10525,7 @@ const functions = (() => { index: matches.start, groups: matches.groups }); - matches = await evaluateMatcher(matches.next); + matches = yield* evaluateMatcher(matches.next); count++; } } @@ -10542,7 +10542,7 @@ const functions = (() => { * @param {Integer} [limit] - max number of matches to return * @returns {Array} The array of match objects */ - async function replace(str, pattern, replacement, limit) { + function* replace(str, pattern, replacement, limit) { // undefined inputs always return undefined if (typeof str === 'undefined') { return undefined; @@ -10640,13 +10640,13 @@ const functions = (() => { } result += str.substring(position); } else { - var matches = await evaluateMatcher(pattern, str); + var matches = yield* evaluateMatcher(pattern, str); if (typeof matches !== 'undefined') { while (typeof matches !== 'undefined' && (typeof limit === 'undefined' || count < limit)) { result += str.substring(position, matches.start); var replacedWith = replacer.apply(self, [matches]); - if (isPromise(replacedWith)) { - replacedWith = await replacedWith; + if (isIterable(replacedWith)) { + replacedWith = yield* replacedWith; } // check replacedWith is a string if (typeof replacedWith === 'string') { @@ -10661,7 +10661,7 @@ const functions = (() => { } position = matches.start + matches.match.length; count++; - matches = await evaluateMatcher(matches.next); + matches = yield* evaluateMatcher(matches.next); } result += str.substring(position); } else { @@ -10831,7 +10831,7 @@ const functions = (() => { * @param {Integer} [limit] - max number of substrings * @returns {Array} The array of string */ - async function split(str, separator, limit) { + function* split(str, separator, limit) { // undefined inputs always return undefined if (typeof str === 'undefined') { return undefined; @@ -10854,13 +10854,13 @@ const functions = (() => { result = str.split(separator, limit); } else { var count = 0; - var matches = await evaluateMatcher(separator, str); + var matches = yield* evaluateMatcher(separator, str); if (typeof matches !== 'undefined') { var start = 0; while (typeof matches !== 'undefined' && (typeof limit === 'undefined' || count < limit)) { result.push(str.substring(start, matches.start)); start = matches.end; - matches = await evaluateMatcher(matches.next); + matches = yield* evaluateMatcher(matches.next); count++; } if (typeof limit === 'undefined' || count < limit) { @@ -11336,8 +11336,6 @@ const functions = (() => { result = arg; } else if (typeof arg === 'string' && /^-?[0-9]+(\.[0-9]+)?([Ee][-+]?[0-9]+)?$/.test(arg) && !isNaN(parseFloat(arg)) && isFinite(arg)) { result = parseFloat(arg); - } else if (typeof arg === 'string' && /^(0[xX][0-9A-Fa-f]+)|(0[oO][0-7]+)|(0[bB][0-1]+)$/.test(arg)) { - result = Number(arg); } else if (arg === true) { // boolean true casts to 1 result = 1; @@ -11605,7 +11603,7 @@ const functions = (() => { * @param {Function} func - function to apply * @returns {Array} Map array */ - async function map(arr, func) { + function* map(arr, func) { // undefined inputs always return undefined if (typeof arr === 'undefined') { return undefined; @@ -11616,7 +11614,7 @@ const functions = (() => { for (var i = 0; i < arr.length; i++) { var func_args = hofFuncArgs(func, arr[i], i, arr); // invoke func - var res = await func.apply(this, func_args); + var res = yield* func.apply(this, func_args); if (typeof res !== 'undefined') { result.push(res); } @@ -11631,7 +11629,7 @@ const functions = (() => { * @param {Function} func - predicate function * @returns {Array} Map array */ - async function filter(arr, func) { + function* filter(arr, func) { // eslint-disable-line require-yield // undefined inputs always return undefined if (typeof arr === 'undefined') { return undefined; @@ -11643,7 +11641,7 @@ const functions = (() => { var entry = arr[i]; var func_args = hofFuncArgs(func, entry, i, arr); // invoke func - var res = await func.apply(this, func_args); + var res = yield* func.apply(this, func_args); if (boolean(res)) { result.push(entry); } @@ -11659,7 +11657,7 @@ const functions = (() => { * @param {Function} [func] - predicate function * @returns {*} Matching element */ - async function single(arr, func) { + function* single(arr, func) { // eslint-disable-line require-yield // undefined inputs always return undefined if (typeof arr === 'undefined') { return undefined; @@ -11674,7 +11672,7 @@ const functions = (() => { if (typeof func !== 'undefined') { var func_args = hofFuncArgs(func, entry, i, arr); // invoke func - var res = await func.apply(this, func_args); + var res = yield* func.apply(this, func_args); positiveResult = boolean(res); } if (positiveResult) { @@ -11733,7 +11731,7 @@ const functions = (() => { * @param {Object} init - Initial value * @returns {*} Result */ - async function foldLeft(sequence, func, init) { + function* foldLeft(sequence, func, init) { // undefined inputs always return undefined if (typeof sequence === 'undefined') { return undefined; @@ -11767,7 +11765,7 @@ const functions = (() => { if (arity >= 4) { args.push(sequence); } - result = await func.apply(this, args); + result = yield* func.apply(this, args); index++; } @@ -11939,13 +11937,13 @@ const functions = (() => { * @param {*} func - the function to apply to each key/value pair * @returns {Array} - the resultant array */ - async function each(obj, func) { + function* each(obj, func) { var result = createSequence(); for (var key in obj) { var func_args = hofFuncArgs(func, obj[key], key, obj); // invoke func - var val = await func.apply(this, func_args); + var val = yield* func.apply(this, func_args); if(typeof val !== 'undefined') { result.push(val); } @@ -12030,7 +12028,7 @@ const functions = (() => { * @param {*} comparator - comparator function * @returns {Array} - sorted array */ - async function sort(arr, comparator) { + function* sort(arr, comparator) { // undefined inputs always return undefined if (typeof arr === 'undefined') { return undefined; @@ -12051,7 +12049,7 @@ const functions = (() => { }; } - comp = async function (a, b) { + comp = function* (a, b) { // eslint-disable-line require-yield return a > b; }; } else { @@ -12059,41 +12057,41 @@ const functions = (() => { comp = comparator; } - var merge = async function (l, r) { - var merge_iter = async function (result, left, right) { + var merge = function* (l, r) { + var merge_iter = function* (result, left, right) { if (left.length === 0) { Array.prototype.push.apply(result, right); } else if (right.length === 0) { Array.prototype.push.apply(result, left); - } else if (await comp(left[0], right[0])) { // invoke the comparator function + } else if (yield* comp(left[0], right[0])) { // invoke the comparator function // if it returns true - swap left and right result.push(right[0]); - await merge_iter(result, left, right.slice(1)); + yield* merge_iter(result, left, right.slice(1)); } else { // otherwise keep the same order result.push(left[0]); - await merge_iter(result, left.slice(1), right); + yield* merge_iter(result, left.slice(1), right); } }; var merged = []; - await merge_iter(merged, l, r); + yield* merge_iter(merged, l, r); return merged; }; - var msort = async function (array) { + var msort = function* (array) { if (!Array.isArray(array) || array.length <= 1) { return array; } else { var middle = Math.floor(array.length / 2); var left = array.slice(0, middle); var right = array.slice(middle); - left = await msort(left); - right = await msort(right); - return await merge(left, right); + left = yield* msort(left); + right = yield* msort(right); + return yield* merge(left, right); } }; - var result = await msort(arr); + var result = yield* msort(arr); return result; } @@ -12168,14 +12166,14 @@ const functions = (() => { * @param {object} func - the predicate function (lambda or native) * @returns {object} - sifted object */ - async function sift(arg, func) { + function* sift(arg, func) { var result = {}; for (var item in arg) { var entry = arg[item]; var func_args = hofFuncArgs(func, entry, item, arg); // invoke func - var res = await func.apply(this, func_args); + var res = yield* func.apply(this, func_args); if (boolean(res)) { result[item] = entry; } @@ -12239,7 +12237,6 @@ var jsonata = (function() { var isFunction = utils.isFunction; var isLambda = utils.isLambda; var isIterable = utils.isIterable; - var isPromise = utils.isPromise; var getFunctionArity = utils.getFunctionArity; var isDeepEqual = utils.isDeepEqual; @@ -12254,23 +12251,23 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluate(expr, input, environment) { + function* evaluate(expr, input, environment) { var result; var entryCallback = environment.lookup('__evaluate_entry'); if(entryCallback) { - await entryCallback(expr, input, environment); + entryCallback(expr, input, environment); } switch (expr.type) { case 'path': - result = await evaluatePath(expr, input, environment); + result = yield * evaluatePath(expr, input, environment); break; case 'binary': - result = await evaluateBinary(expr, input, environment); + result = yield * evaluateBinary(expr, input, environment); break; case 'unary': - result = await evaluateUnary(expr, input, environment); + result = yield * evaluateUnary(expr, input, environment); break; case 'name': result = evaluateName(expr, input, environment); @@ -12290,19 +12287,19 @@ var jsonata = (function() { result = environment.lookup(expr.slot.label); break; case 'condition': - result = await evaluateCondition(expr, input, environment); + result = yield * evaluateCondition(expr, input, environment); break; case 'block': - result = await evaluateBlock(expr, input, environment); + result = yield * evaluateBlock(expr, input, environment); break; case 'bind': - result = await evaluateBindExpression(expr, input, environment); + result = yield * evaluateBindExpression(expr, input, environment); break; case 'regex': result = evaluateRegex(expr, input, environment); break; case 'function': - result = await evaluateFunction(expr, input, environment); + result = yield * evaluateFunction(expr, input, environment); break; case 'variable': result = evaluateVariable(expr, input, environment); @@ -12311,29 +12308,40 @@ var jsonata = (function() { result = evaluateLambda(expr, input, environment); break; case 'partial': - result = await evaluatePartialApplication(expr, input, environment); + result = yield * evaluatePartialApplication(expr, input, environment); break; case 'apply': - result = await evaluateApplyExpression(expr, input, environment); + result = yield * evaluateApplyExpression(expr, input, environment); break; case 'transform': result = evaluateTransformExpression(expr, input, environment); break; } + if(environment.async && + (typeof result === 'undefined' || result === null || typeof result.then !== 'function')) { + result = Promise.resolve(result); + } + if(environment.async && typeof result.then === 'function' && expr.nextFunction && typeof result[expr.nextFunction] === 'function') { + // although this is a 'thenable', it is chaining a different function + // so don't yield since yielding will trigger the .then() + } else { + result = yield result; + } + if (Object.prototype.hasOwnProperty.call(expr, 'predicate')) { for(var ii = 0; ii < expr.predicate.length; ii++) { - result = await evaluateFilter(expr.predicate[ii].expr, result, environment); + result = yield * evaluateFilter(expr.predicate[ii].expr, result, environment); } } if (expr.type !== 'path' && Object.prototype.hasOwnProperty.call(expr, 'group')) { - result = await evaluateGroupExpression(expr.group, result, environment); + result = yield * evaluateGroupExpression(expr.group, result, environment); } var exitCallback = environment.lookup('__evaluate_exit'); if(exitCallback) { - await exitCallback(expr, input, environment, result); + exitCallback(expr, input, environment, result); } if(result && isSequence(result) && !result.tupleStream) { @@ -12358,7 +12366,7 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluatePath(expr, input, environment) { + function* evaluatePath(expr, input, environment) { var inputSequence; // expr is an array of steps // if the first step is a variable reference ($...), including root reference ($$), @@ -12384,12 +12392,12 @@ var jsonata = (function() { // if the first step is an explicit array constructor, then just evaluate that (i.e. don't iterate over a context array) if(ii === 0 && step.consarray) { - resultSequence = await evaluate(step, inputSequence, environment); + resultSequence = yield * evaluate(step, inputSequence, environment); } else { if(isTupleStream) { - tupleBindings = await evaluateTupleStep(step, inputSequence, tupleBindings, environment); + tupleBindings = yield * evaluateTupleStep(step, inputSequence, tupleBindings, environment); } else { - resultSequence = await evaluateStep(step, inputSequence, environment, ii === expr.steps.length - 1); + resultSequence = yield * evaluateStep(step, inputSequence, environment, ii === expr.steps.length - 1); } } @@ -12424,7 +12432,7 @@ var jsonata = (function() { } if (expr.hasOwnProperty('group')) { - resultSequence = await evaluateGroupExpression(expr.group, isTupleStream ? tupleBindings : resultSequence, environment) + resultSequence = yield* evaluateGroupExpression(expr.group, isTupleStream ? tupleBindings : resultSequence, environment) } return resultSequence; @@ -12446,12 +12454,12 @@ var jsonata = (function() { * @param {boolean} lastStep - flag the last step in a path * @returns {*} Evaluated input data */ - async function evaluateStep(expr, input, environment, lastStep) { + function* evaluateStep(expr, input, environment, lastStep) { var result; if(expr.type === 'sort') { - result = await evaluateSortExpression(expr, input, environment); + result = yield* evaluateSortExpression(expr, input, environment); if(expr.stages) { - result = await evaluateStages(expr.stages, result, environment); + result = yield* evaluateStages(expr.stages, result, environment); } return result; } @@ -12459,10 +12467,10 @@ var jsonata = (function() { result = createSequence(); for(var ii = 0; ii < input.length; ii++) { - var res = await evaluate(expr, input[ii], environment); + var res = yield * evaluate(expr, input[ii], environment); if(expr.stages) { for(var ss = 0; ss < expr.stages.length; ss++) { - res = await evaluateFilter(expr.stages[ss].expr, res, environment); + res = yield* evaluateFilter(expr.stages[ss].expr, res, environment); } } if(typeof res !== 'undefined') { @@ -12489,13 +12497,13 @@ var jsonata = (function() { return resultSequence; } - async function evaluateStages(stages, input, environment) { + function* evaluateStages(stages, input, environment) { var result = input; for(var ss = 0; ss < stages.length; ss++) { var stage = stages[ss]; switch(stage.type) { case 'filter': - result = await evaluateFilter(stage.expr, result, environment); + result = yield * evaluateFilter(stage.expr, result, environment); break; case 'index': for(var ee = 0; ee < result.length; ee++) { @@ -12516,13 +12524,13 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateTupleStep(expr, input, tupleBindings, environment) { + function* evaluateTupleStep(expr, input, tupleBindings, environment) { var result; if(expr.type === 'sort') { if(tupleBindings) { - result = await evaluateSortExpression(expr, tupleBindings, environment); + result = yield* evaluateSortExpression(expr, tupleBindings, environment); } else { - var sorted = await evaluateSortExpression(expr, input, environment); + var sorted = yield* evaluateSortExpression(expr, input, environment); result = createSequence(); result.tupleStream = true; for(var ss = 0; ss < sorted.length; ss++) { @@ -12532,7 +12540,7 @@ var jsonata = (function() { } } if(expr.stages) { - result = await evaluateStages(expr.stages, result, environment); + result = yield* evaluateStages(expr.stages, result, environment); } return result; } @@ -12546,7 +12554,7 @@ var jsonata = (function() { for(var ee = 0; ee < tupleBindings.length; ee++) { stepEnv = createFrameFromTuple(environment, tupleBindings[ee]); - var res = await evaluate(expr, tupleBindings[ee]['@'], stepEnv); + var res = yield* evaluate(expr, tupleBindings[ee]['@'], stepEnv); // res is the binding sequence for the output tuple stream if(typeof res !== 'undefined') { if (!Array.isArray(res)) { @@ -12577,7 +12585,7 @@ var jsonata = (function() { } if(expr.stages) { - result = await evaluateStages(expr.stages, result, environment); + result = yield * evaluateStages(expr.stages, result, environment); } return result; @@ -12590,7 +12598,7 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Result after applying predicates */ - async function evaluateFilter(predicate, input, environment) { + function* evaluateFilter(predicate, input, environment) { var results = createSequence(); if( input && input.tupleStream) { results.tupleStream = true; @@ -12621,7 +12629,7 @@ var jsonata = (function() { context = item['@']; env = createFrameFromTuple(environment, item); } - var res = await evaluate(predicate, context, env); + var res = yield* evaluate(predicate, context, env); if (isNumeric(res)) { res = [res]; } @@ -12652,16 +12660,16 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateBinary(expr, input, environment) { + function * evaluateBinary(expr, input, environment) { var result; - var lhs = await evaluate(expr.lhs, input, environment); + var lhs = yield * evaluate(expr.lhs, input, environment); var op = expr.value; //defer evaluation of RHS to allow short-circuiting - var evalrhs = async () => await evaluate(expr.rhs, input, environment); + var evalrhs = function*(){return yield * evaluate(expr.rhs, input, environment);}; if (op === "and" || op === "or") { try { - return await evaluateBooleanExpression(lhs, evalrhs, op); + return yield * evaluateBooleanExpression(lhs, evalrhs, op); } catch(err) { err.position = expr.position; err.token = op; @@ -12669,7 +12677,7 @@ var jsonata = (function() { } } - var rhs = await evalrhs(); + var rhs = yield * evalrhs(); try { switch (op) { case '+': @@ -12714,12 +12722,12 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateUnary(expr, input, environment) { + function* evaluateUnary(expr, input, environment) { var result; switch (expr.value) { case '-': - result = await evaluate(expr.expression, input, environment); + result = yield * evaluate(expr.expression, input, environment); if(typeof result === 'undefined') { result = undefined; } else if (isNumeric(result)) { @@ -12737,13 +12745,9 @@ var jsonata = (function() { case '[': // array constructor - evaluate each item result = []; - let generators = await Promise.all(expr.expressions - .map(async (item, idx) => { - environment.isParallelCall = idx > 0 - return [item, await evaluate(item, input, environment)] - })); - for (let generator of generators) { - var [item, value] = generator; + for(var ii = 0; ii < expr.expressions.length; ii++) { + var item = expr.expressions[ii]; + var value = yield * evaluate(item, input, environment); if (typeof value !== 'undefined') { if(item.value === '[') { result.push(value); @@ -12762,7 +12766,7 @@ var jsonata = (function() { break; case '{': // object constructor - apply grouping - result = await evaluateGroupExpression(expr, input, environment); + result = yield * evaluateGroupExpression(expr, input, environment); break; } @@ -13053,17 +13057,17 @@ var jsonata = (function() { * @param {Object} op - opcode * @returns {*} Result */ - async function evaluateBooleanExpression(lhs, evalrhs, op) { + function * evaluateBooleanExpression(lhs, evalrhs, op) { var result; var lBool = boolize(lhs); switch (op) { case 'and': - result = lBool && boolize(await evalrhs()); + result = lBool && boolize(yield * evalrhs()); break; case 'or': - result = lBool || boolize(await evalrhs()); + result = lBool || boolize(yield * evalrhs()); break; } return result; @@ -13103,7 +13107,7 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {{}} Evaluated input data */ - async function evaluateGroupExpression(expr, input, environment) { + function* evaluateGroupExpression(expr, input, environment) { var result = {}; var groups = {}; var reduce = input && input.tupleStream ? true : false; @@ -13121,7 +13125,7 @@ var jsonata = (function() { var env = reduce ? createFrameFromTuple(environment, item) : environment; for(var pairIndex = 0; pairIndex < expr.lhs.length; pairIndex++) { var pair = expr.lhs[pairIndex]; - var key = await evaluate(pair[0], reduce ? item['@'] : item, env); + var key = yield * evaluate(pair[0], reduce ? item['@'] : item, env); // key has to be a string if (typeof key !== 'string' && key !== undefined) { throw { @@ -13157,8 +13161,8 @@ var jsonata = (function() { } // iterate over the groups to evaluate the 'value' expression - let generators = await Promise.all(Object.keys(groups).map(async (key, idx) => { - let entry = groups[key]; + for (key in groups) { + entry = groups[key]; var context = entry.data; var env = environment; if (reduce) { @@ -13167,12 +13171,7 @@ var jsonata = (function() { delete tuple['@']; env = createFrameFromTuple(environment, tuple); } - environment.isParallelCall = idx > 0 - return [key, await evaluate(expr.lhs[entry.exprIndex][1], context, env)]; - })); - - for (let generator of generators) { - var [key, value] = await generator; + var value = yield * evaluate(expr.lhs[entry.exprIndex][1], context, env); if(typeof value !== 'undefined') { result[key] = value; } @@ -13256,10 +13255,10 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateBindExpression(expr, input, environment) { + function* evaluateBindExpression(expr, input, environment) { // The RHS is the expression to evaluate // The LHS is the name of the variable to bind to - should be a VARIABLE token (enforced by parser) - var value = await evaluate(expr.rhs, input, environment); + var value = yield * evaluate(expr.rhs, input, environment); environment.bind(expr.lhs.value, value); return value; } @@ -13271,13 +13270,13 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateCondition(expr, input, environment) { + function* evaluateCondition(expr, input, environment) { var result; - var condition = await evaluate(expr.condition, input, environment); + var condition = yield * evaluate(expr.condition, input, environment); if (fn.boolean(condition)) { - result = await evaluate(expr.then, input, environment); + result = yield * evaluate(expr.then, input, environment); } else if (typeof expr.else !== 'undefined') { - result = await evaluate(expr.else, input, environment); + result = yield * evaluate(expr.else, input, environment); } return result; } @@ -13289,7 +13288,7 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateBlock(expr, input, environment) { + function* evaluateBlock(expr, input, environment) { var result; // create a new frame to limit the scope of variable assignments // TODO, only do this if the post-parse stage has flagged this as required @@ -13297,7 +13296,7 @@ var jsonata = (function() { // invoke each expression in turn // only return the result of the last one for(var ii = 0; ii < expr.expressions.length; ii++) { - result = await evaluate(expr.expressions[ii], input, frame); + result = yield * evaluate(expr.expressions[ii], input, frame); } return result; @@ -13376,16 +13375,17 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Ordered sequence */ - async function evaluateSortExpression(expr, input, environment) { + function* evaluateSortExpression(expr, input, environment) { var result; // evaluate the lhs, then sort the results in order according to rhs expression + //var lhs = yield * evaluate(expr.lhs, input, environment); var lhs = input; var isTupleSort = input.tupleStream ? true : false; // sort the lhs array // use comparator function - var comparator = async function(a, b) { + var comparator = function*(a, b) { // eslint-disable-line require-yield // expr.terms is an array of order-by in priority order var comp = 0; for(var index = 0; comp === 0 && index < expr.terms.length; index++) { @@ -13397,7 +13397,7 @@ var jsonata = (function() { context = a['@']; env = createFrameFromTuple(environment, a); } - var aa = await evaluate(term.expression, context, env); + var aa = yield * evaluate(term.expression, context, env); //evaluate the sort term in the context of b context = b; env = environment; @@ -13405,7 +13405,7 @@ var jsonata = (function() { context = b['@']; env = createFrameFromTuple(environment, b); } - var bb = await evaluate(term.expression, context, env); + var bb = yield * evaluate(term.expression, context, env); // type checks var atype = typeof aa; @@ -13462,7 +13462,7 @@ var jsonata = (function() { input: input }; // the `focus` is passed in as the `this` for the invoked function - result = await fn.sort.apply(focus, [lhs, comparator]); + result = yield * fn.sort.apply(focus, [lhs, comparator]); return result; } @@ -13476,7 +13476,7 @@ var jsonata = (function() { */ function evaluateTransformExpression(expr, input, environment) { // create a function to implement the transform definition - var transformer = async function (obj) { // signature <(oa):o> + var transformer = function*(obj) { // signature <(oa):o> // undefined inputs always return undefined if(typeof obj === 'undefined') { return undefined; @@ -13492,8 +13492,8 @@ var jsonata = (function() { position: expr.position }; } - var result = await apply(cloneFunction, [obj], null, environment); - var matches = await evaluate(expr.pattern, result, environment); + var result = yield * apply(cloneFunction, [obj], null, environment); + var matches = yield * evaluate(expr.pattern, result, environment); if(typeof matches !== 'undefined') { if(!Array.isArray(matches)) { matches = [matches]; @@ -13501,7 +13501,7 @@ var jsonata = (function() { for(var ii = 0; ii < matches.length; ii++) { var match = matches[ii]; // evaluate the update value for each match - var update = await evaluate(expr.update, match, environment); + var update = yield * evaluate(expr.update, match, environment); // update must be an object var updateType = typeof update; if(updateType !== 'undefined') { @@ -13522,7 +13522,7 @@ var jsonata = (function() { // delete, if specified, must be an array of strings (or single string) if(typeof expr.delete !== 'undefined') { - var deletions = await evaluate(expr.delete, match, environment); + var deletions = yield * evaluate(expr.delete, match, environment); if(typeof deletions !== 'undefined') { var val = deletions; if (!Array.isArray(deletions)) { @@ -13562,16 +13562,16 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateApplyExpression(expr, input, environment) { + function* evaluateApplyExpression(expr, input, environment) { var result; - var lhs = await evaluate(expr.lhs, input, environment); + var lhs = yield * evaluate(expr.lhs, input, environment); if(expr.rhs.type === 'function') { // this is a function _invocation_; invoke it with lhs expression as the first argument - result = await evaluateFunction(expr.rhs, input, environment, { context: lhs }); + result = yield * evaluateFunction(expr.rhs, input, environment, { context: lhs }); } else { - var func = await evaluate(expr.rhs, input, environment); + var func = yield * evaluate(expr.rhs, input, environment); if(!isFunction(func)) { throw { @@ -13585,10 +13585,10 @@ var jsonata = (function() { if(isFunction(lhs)) { // this is function chaining (func1 ~> func2) // λ($f, $g) { λ($x){ $g($f($x)) } } - var chain = await evaluate(chainAST, null, environment); - result = await apply(chain, [lhs, func], null, environment); + var chain = yield * evaluate(chainAST, null, environment); + result = yield * apply(chain, [lhs, func], null, environment); } else { - result = await apply(func, [lhs], null, environment); + result = yield * apply(func, [lhs], null, environment); } } @@ -13603,14 +13603,14 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluateFunction(expr, input, environment, applyto) { + function* evaluateFunction(expr, input, environment, applyto) { var result; // create the procedure // can't assume that expr.procedure is a lambda type directly // could be an expression that evaluates to a function (e.g. variable reference, parens expr etc. // evaluate it generically first, then check that it is a function. Throw error if not. - var proc = await evaluate(expr.procedure, input, environment); + var proc = yield * evaluate(expr.procedure, input, environment); if (typeof proc === 'undefined' && expr.procedure.type === 'path' && environment.lookup(expr.procedure.steps[0].value)) { // help the user out here if they simply forgot the leading $ @@ -13628,12 +13628,12 @@ var jsonata = (function() { } // eager evaluation - evaluate the arguments for (var jj = 0; jj < expr.arguments.length; jj++) { - const arg = await evaluate(expr.arguments[jj], input, environment); + const arg = yield* evaluate(expr.arguments[jj], input, environment); if(isFunction(arg)) { // wrap this in a closure - const closure = async function (...params) { + const closure = function* (...params) { // invoke func - return await apply(arg, params, null, environment); + return yield * apply(arg, params, null, environment); }; closure.arity = getFunctionArity(arg); evaluatedArgs.push(closure); @@ -13648,7 +13648,7 @@ var jsonata = (function() { proc.token = procName; proc.position = expr.position; } - result = await apply(proc, evaluatedArgs, input, environment); + result = yield * apply(proc, evaluatedArgs, input, environment); } catch (err) { if(!err.position) { // add the position field to the error @@ -13671,24 +13671,24 @@ var jsonata = (function() { * @param {Object} environment - environment * @returns {*} Result of procedure */ - async function apply(proc, args, input, environment) { + function* apply(proc, args, input, environment) { var result; - result = await applyInner(proc, args, input, environment); + result = yield * applyInner(proc, args, input, environment); while(isLambda(result) && result.thunk === true) { // trampoline loop - this gets invoked as a result of tail-call optimization // the function returned a tail-call thunk // unpack it, evaluate its arguments, and apply the tail call - var next = await evaluate(result.body.procedure, result.input, result.environment); + var next = yield * evaluate(result.body.procedure, result.input, result.environment); if(result.body.procedure.type === 'variable') { next.token = result.body.procedure.value; } next.position = result.body.procedure.position; var evaluatedArgs = []; for(var ii = 0; ii < result.body.arguments.length; ii++) { - evaluatedArgs.push(await evaluate(result.body.arguments[ii], result.input, result.environment)); + evaluatedArgs.push(yield * evaluate(result.body.arguments[ii], result.input, result.environment)); } - result = await applyInner(next, evaluatedArgs, input, environment); + result = yield * applyInner(next, evaluatedArgs, input, environment); } return result; } @@ -13701,7 +13701,7 @@ var jsonata = (function() { * @param {Object} environment - environment * @returns {*} Result of procedure */ - async function applyInner(proc, args, input, environment) { + function* applyInner(proc, args, input, environment) { var result; try { var validatedArgs = args; @@ -13710,7 +13710,7 @@ var jsonata = (function() { } if (isLambda(proc)) { - result = await applyProcedure(proc, validatedArgs); + result = yield* applyProcedure(proc, validatedArgs); } else if (proc && proc._jsonata_function === true) { var focus = { environment: environment, @@ -13721,19 +13721,17 @@ var jsonata = (function() { // `proc.implementation` might be a generator function // and `result` might be a generator - if so, yield if (isIterable(result)) { - result = result.next().value; - } - if (isPromise(result)) { - result = await result; + result = yield* result; } } else if (typeof proc === 'function') { // typically these are functions that are returned by the invocation of plugin functions // the `input` is being passed in as the `this` for the invoked function // this is so that functions that return objects containing functions can chain - // e.g. await (await $func()) + // e.g. $func().next().next() result = proc.apply(input, validatedArgs); - if (isPromise(result)) { - result = await result; + /* istanbul ignore next */ + if (isIterable(result)) { + result = yield* result; } } else { throw { @@ -13773,8 +13771,8 @@ var jsonata = (function() { if(expr.thunk === true) { procedure.thunk = true; } - procedure.apply = async function(self, args) { - return await apply(procedure, args, input, !!self ? self.environment : environment); + procedure.apply = function*(self, args) { + return yield * apply(procedure, args, input, self.environment); }; return procedure; } @@ -13786,7 +13784,7 @@ var jsonata = (function() { * @param {Object} environment - Environment * @returns {*} Evaluated input data */ - async function evaluatePartialApplication(expr, input, environment) { + function* evaluatePartialApplication(expr, input, environment) { // partially apply a function var result; // evaluate the arguments @@ -13796,11 +13794,11 @@ var jsonata = (function() { if (arg.type === 'operator' && arg.value === '?') { evaluatedArgs.push(arg); } else { - evaluatedArgs.push(await evaluate(arg, input, environment)); + evaluatedArgs.push(yield * evaluate(arg, input, environment)); } } // lookup the procedure - var proc = await evaluate(expr.procedure, input, environment); + var proc = yield * evaluate(expr.procedure, input, environment); if (typeof proc === 'undefined' && expr.procedure.type === 'path' && environment.lookup(expr.procedure.steps[0].value)) { // help the user out here if they simply forgot the leading $ throw { @@ -13849,7 +13847,7 @@ var jsonata = (function() { * @param {Array} args - Arguments * @returns {*} Result of procedure */ - async function applyProcedure(proc, args) { + function* applyProcedure(proc, args) { var result; var env = createFrame(proc.environment); proc.arguments.forEach(function (param, index) { @@ -13857,9 +13855,9 @@ var jsonata = (function() { }); if (typeof proc.body === 'function') { // this is a lambda that wraps a native function - generated by partially evaluating a native - result = await applyNativeFunction(proc.body, env); + result = yield * applyNativeFunction(proc.body, env); } else { - result = await evaluate(proc.body, proc.input, env); + result = yield * evaluate(proc.body, proc.input, env); } return result; } @@ -13921,7 +13919,7 @@ var jsonata = (function() { * @param {Object} env - Environment * @returns {*} Result of applying native function */ - async function applyNativeFunction(proc, env) { + function* applyNativeFunction(proc, env) { var sigArgs = getNativeFunctionArguments(proc); // generate the array of arguments for invoking the function - look them up in the environment var args = sigArgs.map(function (sigArg) { @@ -13932,8 +13930,8 @@ var jsonata = (function() { environment: env }; var result = proc.apply(focus, args); - if (isPromise(result)) { - result = await result; + if(isIterable(result)) { + result = yield * result; } return result; } @@ -13973,7 +13971,7 @@ var jsonata = (function() { * @param {string} expr - expression to evaluate * @returns {*} - result of evaluating the expression */ - async function functionEval(expr, focus) { + function* functionEval(expr, focus) { // undefined inputs always return undefined if(typeof expr === 'undefined') { return undefined; @@ -14001,7 +13999,7 @@ var jsonata = (function() { }; } try { - var result = await evaluate(ast, input, this.environment); + var result = yield* evaluate(ast, input, this.environment); } catch(err) { // error evaluating the expression passed to $eval populateMessage(err); @@ -14052,7 +14050,6 @@ var jsonata = (function() { }, timestamp: enclosingEnvironment ? enclosingEnvironment.timestamp : null, async: enclosingEnvironment ? enclosingEnvironment.async : false, - isParallelCall: enclosingEnvironment ? enclosingEnvironment.isParallelCall : false, global: enclosingEnvironment ? enclosingEnvironment.global : { ancestry: [ null ] } @@ -14298,7 +14295,7 @@ var jsonata = (function() { } return { - evaluate: async function (input, bindings, callback) { + evaluate: function (input, bindings, callback) { // throw if the expression compiled with syntax errors if(typeof errors !== 'undefined') { var err = { @@ -14333,17 +14330,39 @@ var jsonata = (function() { input.outerWrapper = true; } - var it; - try { - it = await evaluate(ast, input, exec_env); - if (typeof callback === "function") { - callback(null, it); + var result, it; + // if a callback function is supplied, then drive the generator in a promise chain + if(typeof callback === 'function') { + exec_env.async = true; + var catchHandler = function (err) { + populateMessage(err); // possible side-effects on `err` + callback(err, null); + }; + var thenHandler = function (response) { + result = it.next(response); + if (result.done) { + callback(null, result.value); + } else { + result.value.then(thenHandler).catch(catchHandler); + } + }; + it = evaluate(ast, input, exec_env); + result = it.next(); + result.value.then(thenHandler).catch(catchHandler); + } else { + // no callback function - drive the generator to completion synchronously + try { + it = evaluate(ast, input, exec_env); + result = it.next(); + while (!result.done) { + result = it.next(result.value); + } + return result.value; + } catch (err) { + // insert error message into structure + populateMessage(err); // possible side-effects on `err` + throw err; } - return it; - } catch (err) { - // insert error message into structure - populateMessage(err); // possible side-effects on `err` - throw err; } }, assign: function (name, value) { @@ -16177,7 +16196,9 @@ const utils = (() => { } // istanbul ignore next - var iteratorSymbol = (typeof Symbol === "function" ? Symbol : {}).iterator || "@@iterator"; + var $Symbol = typeof Symbol === "function" ? Symbol : {}; + // istanbul ignore next + var iteratorSymbol = $Symbol.iterator || "@@iterator"; /** * @param {Object} arg - expression to test @@ -16244,19 +16265,6 @@ const utils = (() => { return false; } - /** - * @param {Object} arg - expression to test - * @returns {boolean} - true if it is a promise - */ - function isPromise(arg) { - return ( - typeof arg === 'object' && - arg !== null && - 'then' in arg && - typeof arg.then === 'function' - ); - } - /** * converts a string to an array of characters * @param {string} str - the input string @@ -16281,8 +16289,7 @@ const utils = (() => { isIterable, getFunctionArity, isDeepEqual, - stringToArray, - isPromise + stringToArray }; })(); @@ -18500,7 +18507,6 @@ async function exportSecrets() { const vaultNamespace = core.getInput('namespace', { required: false }); const extraHeaders = parseHeadersInput('extraHeaders', { required: false }); const exportEnv = core.getInput('exportEnv', { required: false }) != 'false'; - const outputToken = (core.getInput('outputToken', { required: false }) || 'false').toLowerCase() != 'false'; const exportToken = (core.getInput('exportToken', { required: false }) || 'false').toLowerCase() != 'false'; const secretsInput = core.getInput('secrets', { required: false }); @@ -18560,11 +18566,8 @@ async function exportSecrets() { defaultOptions.headers['X-Vault-Token'] = vaultToken; const client = got.extend(defaultOptions); - core.setSecret(vaultToken) - if (outputToken === true) { - core.setOutput('vault_token', `${vaultToken}`); - } if (exportToken === true) { + command.issue('add-mask', vaultToken); core.exportVariable('VAULT_TOKEN', `${vaultToken}`); } @@ -18594,7 +18597,7 @@ async function exportSecrets() { for (const line of value.replace(/\r/g, '').split('\n')) { if (line.length > 0) { - core.setSecret(line); + command.issue('add-mask', line); } } if (exportEnv) { @@ -18943,7 +18946,7 @@ async function getSecrets(secretRequests, client) { responseCache.set(requestPath, body); } catch (error) { const {response} = error; - if (response?.statusCode === 404) { + if (response.statusCode === 404) { throw Error(`Unable to retrieve result for "${path}" because it was not found: ${response.body.trim()}`) } throw error @@ -18958,7 +18961,7 @@ async function getSecrets(secretRequests, client) { selector = "data." + selector } - const value = await selectData(body, selector); + const value = selectData(body, selector); results.push({ request: secretRequest, value, @@ -18973,12 +18976,12 @@ async function getSecrets(secretRequests, client) { * @param {object} data * @param {string} selector */ -async function selectData(data, selector) { +function selectData(data, selector) { const ata = jsonata(selector); - let result = JSON.stringify(await ata.evaluate(data)); + let result = JSON.stringify(ata.evaluate(data)); // Compat for custom engines if (!result && ((ata.ast().type === "path" && ata.ast()['steps'].length === 1) || ata.ast().type === "string") && selector !== 'data' && 'data' in data) { - result = JSON.stringify(await jsonata(`data.${selector}`).evaluate(data)); + result = JSON.stringify(jsonata(`data.${selector}`).evaluate(data)); } else if (!result) { throw Error(`Unable to retrieve result for ${selector}. No match data was found. Double check your Key or Selector.`); } diff --git a/src/action.js b/src/action.js index ab5e9f7..0bd8b91 100644 --- a/src/action.js +++ b/src/action.js @@ -70,10 +70,10 @@ async function exportSecrets() { } const vaultToken = await retrieveToken(vaultMethod, got.extend(defaultOptions)); + core.setSecret(vaultToken) defaultOptions.headers['X-Vault-Token'] = vaultToken; const client = got.extend(defaultOptions); - core.setSecret(vaultToken) if (outputToken === true) { core.setOutput('vault_token', `${vaultToken}`); }