isomorfeus-speednode 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81b835d9f31173adc0cb2ac885e942fb36bb08d69e7b259e27d4a9896de56b87
4
- data.tar.gz: '078129dc5a19ccfb29526528ba28faea8c5597eee526dafbf42c1466ceef5866'
3
+ metadata.gz: fc7add59924aa8a26faab4104731a2a2a5370f1a019417105206cb0dc38fd533
4
+ data.tar.gz: 239f6c6e4f2cf18caffca1b755c41d78c76e8d4fdcefdb319d18d6acdf9c8963
5
5
  SHA512:
6
- metadata.gz: 428af1e59a9ce800a692e18e3658920868aa1da2009050d9ef88e4b1a9618e2793f974f46dd436c8b39fe88addf4ae4583ff11c290d07f33e967a468f25908d1
7
- data.tar.gz: 956618bd396f572f0fcc683a11a0edd0a60c89b09ea142915496b8c79aafcc247e7dab0826d27ee545d9391b039ed7e9398db0990005fed05c0a75b9e470d561
6
+ metadata.gz: 77e34c80ccf9ad930f74e57efdc2529c7f91cde1b5b1b91a1ffe3cae33c7f4033c489cb19b788e50d13236a64dba994e55f842825e0ed63cf71f7b3046d91481
7
+ data.tar.gz: d2cef10f4eab5e96fbffd7c8f7b3c3cef3b7a351467dcd76485d779812a514d944d32a0a0b44fb8fe083fe457a54bd94cd4808f3b9a782ce394e30ed117c9111
data/README.md CHANGED
@@ -7,7 +7,7 @@ A fast runtime for execjs using node js. Works on Linux, BSDs, MacOS and Windows
7
7
  Inspired by [execjs-fastnode](https://github.com/jhawthorn/execjs-fastnode).
8
8
 
9
9
  ### Community and Support
10
- At the [Isomorfeus Framework Project](http://isomorfeus.com)
10
+ At the [Isomorfeus Framework Project](https://isomorfeus.com)
11
11
 
12
12
  ### Installation
13
13
 
@@ -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;
@@ -275,6 +281,17 @@ let commands = {
275
281
  let result = vm.runInContext(input.source, getContext(input.context), getContextOptions(input.context));
276
282
  return formatResult(result);
277
283
  },
284
+ scsc: function (input) {
285
+ if (input.source.match(/^\s*{/)) { input.source = "(" + input.source + ")"; }
286
+ else if (input.source.match(/^\s*function\s*\(/)) { input.source = "(" + input.source + ")"; }
287
+ scripts[input.context] = {};
288
+ scripts[input.context][input.key] = new vm.Script(input.source);
289
+ return formatResult(true);
290
+ },
291
+ evsc: function(input) {
292
+ let result = scripts[input.context][input.key].runInContext(getContext(input.context));
293
+ return formatResult(result);
294
+ },
278
295
  // ctxo: function (input) {
279
296
  // return formatResult(getContextOptions(input.context));
280
297
  // },
@@ -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
@@ -110,7 +116,7 @@ module Isomorfeus
110
116
  extract_result(result)
111
117
  end
112
118
 
113
- def extract_result(output)
119
+ def extract_result(output)
114
120
  if output[0] == 'ok'
115
121
  output[1]
116
122
  else
@@ -44,7 +44,7 @@ module Isomorfeus
44
44
  attr_reader :responder
45
45
 
46
46
  def initialize(options)
47
- @mutex = Mutex.new
47
+ @mutex = Thread::Mutex.new
48
48
  @socket_path = nil
49
49
  @options = options
50
50
  @started = false
@@ -79,29 +79,41 @@ 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)
104
- command("deleteContext", context)
112
+ @mutex.synchronize do
113
+ VMCommand.new(@socket, "deleteContext", [context]).execute rescue nil
114
+ end
115
+ rescue ThreadError
116
+ nil
105
117
  end
106
118
 
107
119
  # def context_options(context)
@@ -1,5 +1,5 @@
1
1
  module Isomorfeus
2
2
  module Speednode
3
- VERSION = '0.4.7'
3
+ VERSION = '0.5.0'
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.7
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Biedermann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-19 00:00:00.000000000 Z
11
+ date: 2022-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 3.13.9
33
+ version: 3.13.11
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 3.13.9
40
+ version: 3.13.11
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: win32-pipe
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -130,11 +130,12 @@ files:
130
130
  - lib/isomorfeus/speednode/runtime/vm.rb
131
131
  - lib/isomorfeus/speednode/runtime/vm_command.rb
132
132
  - lib/isomorfeus/speednode/version.rb
133
- homepage: http://isomorfeus.com
133
+ homepage: https://isomorfeus.com
134
134
  licenses:
135
135
  - MIT
136
136
  metadata:
137
137
  github_repo: ssh://github.com/isomorfeus/gems
138
+ source_code_uri: https://github.com/isomorfeus/isomorfeus-speednode
138
139
  post_install_message:
139
140
  rdoc_options: []
140
141
  require_paths:
@@ -143,14 +144,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
144
  requirements:
144
145
  - - ">="
145
146
  - !ruby/object:Gem::Version
146
- version: '0'
147
+ version: 3.1.0
147
148
  required_rubygems_version: !ruby/object:Gem::Requirement
148
149
  requirements:
149
150
  - - ">="
150
151
  - !ruby/object:Gem::Version
151
152
  version: '0'
152
153
  requirements: []
153
- rubygems_version: 3.2.22
154
+ rubygems_version: 3.3.3
154
155
  signing_key:
155
156
  specification_version: 4
156
157
  summary: A fast ExecJS runtime based on nodejs, tuned for Isomorfeus.