Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

duktape-vm

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

duktape-vm - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

5

build/index.d.ts
export interface DuktapeExport {
destroy: () => void;
eval: (js: string, safe?: boolean) => string;
onMessage: (callback: (msg: string) => void) => void;
message: (msg: string) => void;
}
export declare const DuktapeVM: (init?: string) => Promise<DuktapeExport>;
export declare const uuid: () => string;
export declare const DuktapeVM: (init: string) => Promise<DuktapeExport>;

2

package.json
{
"name": "duktape-vm",
"version": "0.0.1",
"version": "0.0.2",
"description": "Javascript VM running in WebAssembly",

@@ -5,0 +5,0 @@ "main": "build/ducktape-vm.js",

@@ -12,2 +12,3 @@ # Duktape Javascript Virtual Machine

- VM supports ES5 and earlier javascript.
- Supports bidirectional messaging.
- Only 140KB gzipped.

@@ -22,5 +23,5 @@

DuktapeVM(`
// Provide variables and data to VM instance
// vm_breakout() allows you to run code in the parent window/global space.
const vm = await DuktapeVM(`
// Provide data and methods to VM instance
// vm_breakout() allows you to eval code in the parent window/global space.
// vm_breakout is only available here, it's not available in subsequent "vm.eval" calls.

@@ -34,13 +35,28 @@

exports.someObj = ${JSON.stringify(obj)};
`);
`).then((vm) => {
vm.eval("2 + 2") // returns "4";
vm.eval("exports.console('hello, world!')") // prints "hello, world!" to the browser console
vm.eval("exports.someObj.key") // returns "value";
// instance is ready
vm.eval("2 + 2") // returns "4";
vm.eval("exports.console('hello, world!')") // prints "hello, world!" to the browser console
vm.eval("exports.someObj.key") // returns "value";
vm.eval(`
exports.onMessage(function(msg) {
// got message from parent!
})
// send message to parent
exports.message("some message to parent");
`);
// destroy instance
vm.destroy();
// send message to instance
vm.message("this is a message to the vm");
// listen for messages from vm
vm.onMessage((msg) => {
console.log("Got message from vm: " + msg);
})
// destroy instance
vm.destroy();
```

@@ -70,9 +86,9 @@

<!-- Webassembly Only Version (Fast with 75% browser support), 140KB -->
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.1/build/duktape-vm.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.2/build/duktape-vm.min.js"></script>
<!-- AsmJS Only Version (Slower with 95% browser support), 150KB -->
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.1/build/duktape-vm.min.asm.js"></script>
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.2/build/duktape-vm.min.asm.js"></script>
<!-- Webassembly AND AsmJS Version (Fast with 95% browser support), 280KB -->
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.1/build/duktape-vm.min.both.js"></script>
<script src="https://cdn.jsdelivr.net/npm/duktape-vm@0.0.2/build/duktape-vm.min.both.js"></script>
```

@@ -79,0 +95,0 @@

const wasm = require("../duktape/dukweb.c");
const self: any = (typeof window !== "undefined" ? window : global);
export interface DuktapeExport {
destroy: () => void;
eval: (js: string, safe?: boolean) => string;
onMessage: (callback: (msg: string) => void) => void;
message: (msg: string) => void;
}
export const DuktapeVM = function(init: string = ""):Promise<DuktapeExport> {
export const uuid = (): string => {
let r, s, b = "";
return [b, b, b, b, b, b, b, b].reduce((prev: string, cur: any, i: number): string => {
r = Math.round(Math.random() * Math.pow(2, 16));
s = (i === 3 ? 4 : (i === 4 ? (r % 16 & 0x3 | 0x8).toString(16) : b));
r = r.toString(16);
while (r.length < 4) r = "0" + r;
return prev + ([2, 3, 4, 5].indexOf(i) > -1 ? "-" : b) + (s + r).slice(0, 4);
}, b);
};
export const DuktapeVM = function(init: string):Promise<DuktapeExport> {
const id = uuid();
self.duktape[id] = [];
return new Promise((res, rej) => {

@@ -15,2 +34,3 @@ wasm.init().then((mod) => {

destroy: () => {
delete self.duktape[id];
mod.emModule.cwrap('dukweb_close', 'void', [ ])();

@@ -23,2 +43,8 @@ },

return (mod.emModule.cwrap('dukweb_eval', 'string', [ 'string' ]) as any)(code);
},
onMessage: (callback: (msg: string) => void) => {
self.duktape[id].push(callback);
},
message: (msg: string) => {
expo.eval(`_messageCBs.forEach(function(cb) { cb(${msg}) })`)
}

@@ -29,2 +55,3 @@ }

expo.eval(`
var _messageCBs = [];
var exports = {},

@@ -35,4 +62,11 @@ vm_breakout = this.emscripten_run_script,

};
exports.message = function(message) {
vm_breakout("duktape['${id}'].forEach(function(fn) { fn(" + message + ") })");
}
exports.onMessage = function(cb) {
_messageCBs.push(cb);
}
${init || ""}
`, false);
if (init) expo.eval(init, false);
res(expo);

@@ -43,4 +77,5 @@ });

if (typeof window !== "undefined") {
(window as any).DuktapeVM = DuktapeVM;
if (!self.duktape) {
self.duktape = {};
self.DuktapeVM = DuktapeVM;
}

@@ -12,3 +12,3 @@ const path = require('path');

path: path.join(__dirname, 'build'),
filename: env.target === "node" ? '[name].js' : (env.asm ? '[name].min.asm.js' : (env.both ? '[name].min.both.js' : '[name].min.asm.js')),
filename: env.target === "node" ? '[name].js' : (env.asm ? '[name].min.asm.js' : (env.both ? '[name].min.both.js' : '[name].min.js')),
libraryTarget: 'umd',

@@ -29,2 +29,5 @@ umdNamedDefine: true

},
node: {
global: false
},
optimization: {

@@ -47,3 +50,3 @@ minimize: env.target === "node" ? false : true

// fetchFiles: true,
asmJs: env.asm ? true : (env.both ? true : false),
asmJs: env.asm || env.both ? true : false,
wasm: env.asm ? false : true,

@@ -50,0 +53,0 @@ fullEnv: true

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc