isomorfeus-speednode 0.4.9 → 0.5.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6386529e41b6302d4eca02b23e030fdcd9ffdc74133635628b936be45b528116
4
- data.tar.gz: bdf7a437e537de0f778af542b3ddf3ac152a9ee0e88a9702efe0e2ef73428f9e
3
+ metadata.gz: 676da9269c67e955ccfe546dbbbc82bf079e796d1070dd8d699c6523dbc537ba
4
+ data.tar.gz: b78918192805e28a455e9c5d45594fdc3b683d93ddc6f0a38f13207afecb808a
5
5
  SHA512:
6
- metadata.gz: 1f0685793f799e6dc50794c02d68ba5676e01d67f0fd4fcf788c998540fe136b81eaa84ce252928ca1add83f0e00f5e3784dcb6de3af74f057060b3b65f66c41
7
- data.tar.gz: b658cb58bcac9a2c7888799a76d32486d25fa6767ace2b9be86ac012a54b4566251b62dc615cab906003614e95b7d5cddbf3bf6e1e46261646a76b0e37392d66
6
+ metadata.gz: b933335621ed51658ade944312fea14e1a8b89ec71585e4b99a46fe3a2dc6c94ccc073f6013281ed19ad07d889e91a09bfad75784c9f5004c8a35e3124b4dd2f
7
+ data.tar.gz: 17c4dd51349c07013c961d714daee25f06c524edae7bb876cc0c78f7eda9519ea8be8c966840b86a08f019ef6b44540b31af09d52e5e8fd36c2576a84b702c44
data/README.md CHANGED
@@ -53,6 +53,15 @@ Evaluation in a permissive context:
53
53
  ExecJS.permissive_eval('1+1')
54
54
  ```
55
55
 
56
+ ### Storing scripts for repeated execution
57
+
58
+ Scripts can be precompiled and stored for repeated execution, which leads to a significant performance improvement, especially for larger scripts:
59
+ ```ruby
60
+ context = ExecJS.compile('Test = "test"')
61
+ context.add_script(key: 'super', source: some_large_javascript) # will compile and store the script
62
+ context.eval_script(:key: 'super') # will run the precompiled script in the context
63
+ ```
64
+
56
65
  ### Async function support
57
66
 
58
67
  Its possible to call async functions synchronously from ruby using Context#await:
@@ -4,7 +4,13 @@ const vm = require('vm');
4
4
  const net = require('net');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
+ let crypto_var = null;
8
+ try {
9
+ crypto_var = require('crypto');
10
+ } catch (err) {}
11
+ const crypto = crypto_var;
7
12
  let contexts = {};
13
+ let scripts = {};
8
14
  let process_exit = false;
9
15
 
10
16
  /*** circular-json, originally taken from https://raw.githubusercontent.com/WebReflection/circular-json/
@@ -200,7 +206,7 @@ function createCompatibleContext(uuid, options) {
200
206
  }
201
207
 
202
208
  function createPermissiveContext(uuid, options) {
203
- let c = vm.createContext({ __responder_socket: false, process: { release: { name: "node" }, env: process.env }, Buffer, clearTimeout, fs, net, os, require, setTimeout });
209
+ let c = vm.createContext({ __responder_socket: false, process: { release: { name: "node" }, env: process.env }, Buffer, clearTimeout, crypto, fs, net, os, require, setTimeout });
204
210
  vm.runInContext('global = globalThis;', c);
205
211
  contexts[uuid] = { context: c, options: options };
206
212
  return c;
@@ -259,6 +265,7 @@ let commands = {
259
265
  },
260
266
  deleteContext: function(uuid) {
261
267
  delete contexts[uuid];
268
+ delete scripts[uuid]
262
269
  return [1];
263
270
  },
264
271
  exit: function(code) {
@@ -275,6 +282,15 @@ let commands = {
275
282
  let result = vm.runInContext(input.source, getContext(input.context), getContextOptions(input.context));
276
283
  return formatResult(result);
277
284
  },
285
+ scsc: function (input) {
286
+ if (!scripts[input.context]) { scripts[input.context] = {}; }
287
+ scripts[input.context][input.key] = new vm.Script(input.source);
288
+ return formatResult(true);
289
+ },
290
+ evsc: function(input) {
291
+ let result = scripts[input.context][input.key].runInContext(getContext(input.context));
292
+ return formatResult(result);
293
+ },
278
294
  // ctxo: function (input) {
279
295
  // return formatResult(getContextOptions(input.context));
280
296
  // },
@@ -7,7 +7,6 @@ module Isomorfeus
7
7
  end
8
8
 
9
9
  def initialize(runtime, source = "", options = {})
10
-
11
10
  @runtime = runtime
12
11
  @uuid = SecureRandom.uuid
13
12
  @permissive = !!options.delete(:permissive)
@@ -69,6 +68,14 @@ module Isomorfeus
69
68
  raw_exec("(function(){#{source}})()")
70
69
  end
71
70
 
71
+ def eval_script(key:)
72
+ extract_result(@vm.evsc(@uuid, key))
73
+ end
74
+
75
+ def add_script(key:, source:)
76
+ extract_result(@vm.scsc(@uuid, key, encode(source)))
77
+ end
78
+
72
79
  def permissive?
73
80
  @permissive
74
81
  end
@@ -89,7 +96,6 @@ module Isomorfeus
89
96
  extract_result(@vm.eval(@uuid, encode(source)))
90
97
  end
91
98
 
92
-
93
99
  def raw_exec(source)
94
100
  extract_result(@vm.exec(@uuid, encode(source)))
95
101
  end
@@ -79,25 +79,33 @@ module Isomorfeus
79
79
  nil
80
80
  end
81
81
 
82
+ def evsc(context, key)
83
+ command('evsc', { 'context' => context, 'key' => key })
84
+ end
85
+
86
+ def scsc(context, key, source)
87
+ command('scsc', { 'context' => context, 'key' => key, 'source' => source })
88
+ end
89
+
82
90
  def eval(context, source)
83
- command("eval", {'context' => context, 'source' => source})
91
+ command('eval', {'context' => context, 'source' => source})
84
92
  end
85
93
 
86
94
  def exec(context, source)
87
- command("exec", {'context' => context, 'source' => source})
95
+ command('exec', {'context' => context, 'source' => source})
88
96
  end
89
97
 
90
98
  def create(context, source, options)
91
- command("create", {'context' => context, 'source' => source, 'options' => options})
99
+ command('create', {'context' => context, 'source' => source, 'options' => options})
92
100
  end
93
101
 
94
102
  def createp(context, source, options)
95
- command("createp", {'context' => context, 'source' => source, 'options' => options})
103
+ command('createp', {'context' => context, 'source' => source, 'options' => options})
96
104
  end
97
105
 
98
106
  def attach(context, func)
99
107
  create_responder(context) unless responder
100
- command("attach", {'context' => context, 'func' => func })
108
+ command('attach', {'context' => context, 'func' => func })
101
109
  end
102
110
 
103
111
  def delete_context(context)
@@ -1,5 +1,5 @@
1
1
  module Isomorfeus
2
2
  module Speednode
3
- VERSION = '0.4.9'
3
+ VERSION = '0.5.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isomorfeus-speednode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.9
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-08 00:00:00.000000000 Z
11
+ date: 2022-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 5.14.4
75
+ version: 5.15.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 5.14.4
82
+ version: 5.15.0
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rake
85
85
  requirement: !ruby/object:Gem::Requirement