https://github.com/Lokomojo/duktype
Duktape for NodeJS/Typescript
https://github.com/Lokomojo/duktype
Last synced: 8 months ago
JSON representation
Duktape for NodeJS/Typescript
- Host: GitHub
- URL: https://github.com/Lokomojo/duktype
- Owner: CasperTech
- Created: 2020-05-09T21:45:47.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T20:25:04.000Z (over 3 years ago)
- Last Synced: 2024-10-18T07:40:32.456Z (over 1 year ago)
- Language: C
- Size: 2.67 MB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Duktype
> Duktape for Typescript/Node.JS
[](https://badge.fury.io/js/%40caspertech%2Fduktype)
[](https://travis-ci.org/CasperTech/duktype)
[](https://snyk.io/test/npm/@caspertech/duktype)
[](https://david-dm.org/CasperTech/duktype.svg)
## About
Duktype (Duktape + Typescript) aims to be a complete node.js binding for the Duktape Javascript engine, complete with Typescript typings.
This allows you to run user scripts in a completely isolated environment.
This is a native C++ module, with no dependencies - it *should* compile on any platform.
## Security
Duktape and V8 don't share memory, so as long as Duktape itself remains secure ([exploits have existed](https://elakkod.se/blog/write-up-sec-t-2019-mirc2077/) in the past), it should not be possible for user scripts to escape their sandbox.
Note: The vulnerability flagged on the badge above is internal to node-cmake, the build system, and doesn't affect this package.
## Install
```bash
npm install --save @caspertech/duktype
```
## Usage
```typescript
import * as duktype from '@caspertech/duktype';
// Create a context which everything runs in
const ctx = new duktype.Context();
// Get the global object
const glob = ctx.getGlobalObject();
// Add a method to the global object
glob.setProperty('print', (any: any, number: any, of: any, args: any) =>
{
console.log(any);
});
// Run some code which calls the method
ctx.eval(`print("I'm calling from Duktape!");`);
// Now maybe you want a console.log - let's add a console object
const obj = glob.createObject('console');
// Let's set another string property which is the tag to use for logging
obj.setProperty('tag', 'QUACK');
// Add a log method to console. You can pass parameters of almost any type, let's try a Date..
obj.setProperty('log', (date: Date, message: string) =>
{
const tag = obj.getProperty('tag');
console.log(date.toISOString() + ': [' + tag + '] ' + message);
});
// Let's run this thing
ctx.eval(`
print('Tag is: ' + console.tag);
console.log(new Date(), 'Console.log works!');
`);
```
## License
[MIT](http://vjpr.mit-license.org)