Skip to content

Commit

Permalink
AG-18132 handle getScriptletFunction calls on unknown scriptlet names
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from fix/AG-18132 to release/v1.8

Squashed commit of the following:

commit 0beb472
Merge: 481ed62 7fd04c9
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Dec 19 19:55:37 2022 +0300

    Merge branch 'release/v1.8' into fix/AG-18132

commit 481ed62
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Fri Dec 16 21:57:14 2022 +0300

    add comment

commit dc38b52
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Thu Dec 15 21:41:26 2022 +0300

    refactor and update readme

commit 35440f5
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Thu Dec 15 19:53:30 2022 +0300

    Handle getScriptletFunction calls on unknown scriptlet names
  • Loading branch information
stanislav-atr committed Dec 19, 2022
1 parent 7fd04c9 commit f0fbd11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ And also there is a module at `dist/scriptlets.js` which has been exported to a

```javascript
/**
* Returns scriptlet code
* Returns scriptlet code by param
* @param {Source} source
* @returns {string}
* @returns {string|null} scriptlet code
* @throws on unknown scriptlet name
*/
scriptlets.invoke(source);
```
Expand Down
14 changes: 11 additions & 3 deletions src/scriptlets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@ import { getScriptletFunction } from '../../tmp/scriptlets-func';
* Returns scriptlet code by param
* @param {Source} source
* @returns {string|null} scriptlet code
* @throws on unknown scriptlet name
*/
function getScriptletCode(source) {
if (!validator.isValidScriptletName(source.name)) {
return null;
}

const scriptletFunction = getScriptletFunction(source.name).toString();
const scriptletFunction = getScriptletFunction(source.name);
// In case isValidScriptletName check will pass invalid scriptlet name,
// for example when there is a bad alias
if (typeof scriptletFunction !== 'function') {
throw new Error(`Error: cannot invoke scriptlet with name: '${source.name}'`);
}
const scriptletFunctionString = scriptletFunction.toString();

const result = source.engine === 'corelibs' || source.engine === 'test'
? wrapInNonameFunc(scriptletFunction)
: passSourceAndProps(source, scriptletFunction);
? wrapInNonameFunc(scriptletFunctionString)
: passSourceAndProps(source, scriptletFunctionString);
return result;
}

Expand Down

0 comments on commit f0fbd11

Please sign in to comment.