@joai/warps-vm-browser 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @joai/warps-vm-browser
2
+
3
+ Browser VM runtime for Warps SDK output transformations. Safely executes transformation code in a Web Worker sandbox.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @joai/warps-vm-browser
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { createBrowserTransformRunner } from '@joai/warps-vm-browser'
15
+ import { WarpClient } from '@joai/warps'
16
+
17
+ const config = {
18
+ env: 'mainnet',
19
+ transform: {
20
+ runner: createBrowserTransformRunner(),
21
+ },
22
+ // ... rest of config
23
+ }
24
+
25
+ const client = new WarpClient(config, { chains: [...] })
26
+ ```
27
+
28
+ ## Features
29
+
30
+ - Safe code execution in Web Workers
31
+ - Sandboxed environment
32
+ - Supports arrow functions, regular functions, and expressions
33
+ - Error handling and isolation
34
+
35
+ ## How It Works
36
+
37
+ The browser VM uses Web Workers to execute transformation code in isolation, preventing access to the main thread's context and ensuring security.
38
+
39
+ ## Example Transformation
40
+
41
+ ```typescript
42
+ // Warp output transformation
43
+ const transform = (results) => {
44
+ return {
45
+ value: results.amount * 2,
46
+ formatted: `$${results.amount.toFixed(2)}`
47
+ }
48
+ }
49
+ ```
@@ -0,0 +1,6 @@
1
+ import { TransformRunner } from '@joai/warps';
2
+
3
+ declare const runInVm: (code: string, results: any) => Promise<any>;
4
+ declare const createBrowserTransformRunner: () => TransformRunner;
5
+
6
+ export { createBrowserTransformRunner, runInVm };
@@ -0,0 +1,6 @@
1
+ import { TransformRunner } from '@joai/warps';
2
+
3
+ declare const runInVm: (code: string, results: any) => Promise<any>;
4
+ declare const createBrowserTransformRunner: () => TransformRunner;
5
+
6
+ export { createBrowserTransformRunner, runInVm };
@@ -0,0 +1,32 @@
1
+ "use strict";var i=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var l=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var p=(r,t)=>{for(var n in t)i(r,n,{get:t[n],enumerable:!0})},g=(r,t,n,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of l(t))!m.call(r,e)&&e!==n&&i(r,e,{get:()=>t[e],enumerable:!(s=f(t,e))||s.enumerable});return r};var w=r=>g(i({},"__esModule",{value:!0}),r);var h={};p(h,{createBrowserTransformRunner:()=>y,runInVm:()=>c});module.exports=w(h);var c=async(r,t)=>new Promise((n,s)=>{try{let e=new Blob([`
2
+ self.onmessage = function(e) {
3
+ try {
4
+ const results = e.data;
5
+ let output;
6
+
7
+ // Create a safe function from the code without using eval
8
+ let transformFunction;
9
+
10
+ // Handle arrow function syntax: () => { return ... }
11
+ if (${JSON.stringify(r.trim())}.startsWith('(') && ${JSON.stringify(r)}.includes('=>')) {
12
+ // For arrow functions, we need to create a function that returns the result
13
+ transformFunction = new Function('results', \`return (\${${JSON.stringify(r)}})(results);\`);
14
+ }
15
+ // Handle regular function syntax: function() { return ... }
16
+ else if (${JSON.stringify(r.trim())}.startsWith('function')) {
17
+ // For regular functions, we need to create a function that returns the result
18
+ transformFunction = new Function('results', \`return (\${${JSON.stringify(r)}})(results);\`);
19
+ }
20
+ // Handle direct expression: results.value * 2
21
+ else {
22
+ // For direct expressions, create a function that returns the expression result
23
+ transformFunction = new Function('results', \`return \${${JSON.stringify(r)}};\`);
24
+ }
25
+
26
+ output = transformFunction(results);
27
+ self.postMessage({ result: output });
28
+ } catch (error) {
29
+ self.postMessage({ error: error.toString() });
30
+ }
31
+ };
32
+ `],{type:"application/javascript"}),u=URL.createObjectURL(e),o=new Worker(u);o.onmessage=function(a){a.data.error?s(new Error(a.data.error)):n(a.data.result),o.terminate(),URL.revokeObjectURL(u)},o.onerror=function(a){s(new Error(`Error in transform: ${a.message}`)),o.terminate(),URL.revokeObjectURL(u)},o.postMessage(t)}catch(e){return s(e)}}),y=()=>({run:c});0&&(module.exports={createBrowserTransformRunner,runInVm});
@@ -0,0 +1,32 @@
1
+ var i=async(r,a)=>new Promise((u,n)=>{try{let s=new Blob([`
2
+ self.onmessage = function(e) {
3
+ try {
4
+ const results = e.data;
5
+ let output;
6
+
7
+ // Create a safe function from the code without using eval
8
+ let transformFunction;
9
+
10
+ // Handle arrow function syntax: () => { return ... }
11
+ if (${JSON.stringify(r.trim())}.startsWith('(') && ${JSON.stringify(r)}.includes('=>')) {
12
+ // For arrow functions, we need to create a function that returns the result
13
+ transformFunction = new Function('results', \`return (\${${JSON.stringify(r)}})(results);\`);
14
+ }
15
+ // Handle regular function syntax: function() { return ... }
16
+ else if (${JSON.stringify(r.trim())}.startsWith('function')) {
17
+ // For regular functions, we need to create a function that returns the result
18
+ transformFunction = new Function('results', \`return (\${${JSON.stringify(r)}})(results);\`);
19
+ }
20
+ // Handle direct expression: results.value * 2
21
+ else {
22
+ // For direct expressions, create a function that returns the expression result
23
+ transformFunction = new Function('results', \`return \${${JSON.stringify(r)}};\`);
24
+ }
25
+
26
+ output = transformFunction(results);
27
+ self.postMessage({ result: output });
28
+ } catch (error) {
29
+ self.postMessage({ error: error.toString() });
30
+ }
31
+ };
32
+ `],{type:"application/javascript"}),o=URL.createObjectURL(s),t=new Worker(o);t.onmessage=function(e){e.data.error?n(new Error(e.data.error)):u(e.data.result),t.terminate(),URL.revokeObjectURL(o)},t.onerror=function(e){n(new Error(`Error in transform: ${e.message}`)),t.terminate(),URL.revokeObjectURL(o)},t.postMessage(a)}catch(s){return n(s)}}),c=()=>({run:i});export{c as createBrowserTransformRunner,i as runInVm};
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@joai/warps-vm-browser",
3
+ "version": "1.0.0-beta.10",
4
+ "type": "module",
5
+ "types": "dist/runInVm.d.ts",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "dependencies": {
10
+ "@joai/warps": "^3.0.0-beta.198"
11
+ },
12
+ "license": "MIT",
13
+ "scripts": {
14
+ "build": "tsup",
15
+ "test": "jest --env=jsdom"
16
+ },
17
+ "devDependencies": {
18
+ "jest": "^30.2.0",
19
+ "ts-jest": "^29.4.6"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/runInVm.d.ts",
24
+ "import": "./dist/runInVm.mjs",
25
+ "require": "./dist/runInVm.js",
26
+ "default": "./dist/runInVm.mjs"
27
+ }
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ }
32
+ }