https://github.com/hungtcs/pico-sandbox
Simple javascript expressions sandbox implement via Function
https://github.com/hungtcs/pico-sandbox
sandbox
Last synced: 3 months ago
JSON representation
Simple javascript expressions sandbox implement via Function
- Host: GitHub
- URL: https://github.com/hungtcs/pico-sandbox
- Owner: hungtcs
- Created: 2022-06-20T10:03:06.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-13T01:03:04.000Z (over 3 years ago)
- Last Synced: 2025-02-27T05:50:06.853Z (over 1 year ago)
- Topics: sandbox
- Language: TypeScript
- Homepage:
- Size: 223 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Pico Sandbox
Simple javascript expressions sandbox implement via Function.
## Warning
The implementation of pico-sandbox is not absolutely secure, as it cannot restrict the JavaScript prototype chain. For example, the following code can bypass the limitations of the sandbox.
```js
''.constructor.__proto__.constructor("alert()")()
```
## Installation
```shell
npm install pico-sandbox
```
## Examples
```typescript
import { Sandbox } from 'pico-sandbox';
const parser = Sandbox.compile('a + b');
const result = parser({ a: 1, b: 2 });
console.log(result); // 3
```
### Throw On Undefined
```typescript
const parser = Sandbox.compile('a + b', { throwOnUndefined: true });
const result = parser({ a: 1 }); // Error: b is not defined
```
### ES6 Template
```typescript
import { Sandbox } from 'pico-sandbox';
function compileTemplate(template: string, options: SandboxOptions = {}) {
return Sandbox.compile(`\`${ template }\``, options);
}
const parser = compileTemplate('Hello ${ name }!');
const result = parser({ name: 'Pico' });
console.log(result); // "Hello Pico!"
```
## Compile Options
- **throwOnUndefined**: throw `ReferenceError` if variable is undefined in expression