execjs-pcruntime 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rubocop.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/execjs-pcruntime.gemspec +32 -0
- data/lib/execjs/pcruntime/context_process_runtime.rb +268 -0
- data/lib/execjs/pcruntime/runner.js +38 -0
- data/lib/execjs/pcruntime/runtimes.rb +17 -0
- data/lib/execjs/pcruntime/version.rb +7 -0
- data/lib/execjs/pcruntime.rb +6 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77461be271505a3cd4f7cb935147fa28686999ea670eb3973c09081b3ec4ac5f
|
4
|
+
data.tar.gz: 6128b62988059ab70234f7b1145bed2eede524236632fc2e9c85f0913ded3c1a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1fd2524a2926f54ea485eb4487a4ee39b6453aebd92d79f1f204887af23e9dfe535830e23bf81a0517c51409ac67f1243ca7cdbb3bb19fb8db0549662db0e4b6
|
7
|
+
data.tar.gz: 21a8aaf06830cccf8604a5172ae220c290ae31184cdb28582b3daba968afff9d85b69603544dc3cfbc4b37daaa228eb3aae30fd07999ba7887c65b4e708f525e
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at shintaro.otsuka@st.inc. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in execjs-pcruntime.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
gem 'execjs', '~> 2.0'
|
11
|
+
|
12
|
+
group :development do
|
13
|
+
gem 'rubocop', require: false
|
14
|
+
gem 'rubocop-minitest', require: false
|
15
|
+
gem 'rubocop-performance', require: false
|
16
|
+
gem 'rubocop-rake', require: false
|
17
|
+
end
|
18
|
+
|
19
|
+
group :test do
|
20
|
+
gem 'minitest', '~> 5.0'
|
21
|
+
gem 'rake', '~> 10.0'
|
22
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 White-Green
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ExecJS::PCRuntime
|
2
|
+
|
3
|
+
Accelerated ExecJS Runtime by launching Node.js Processes per Context, not per execution.
|
4
|
+
|
5
|
+
inspired by [execjs-fastnode](https://github.com/jhawthorn/execjs-fastnode)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'execjs-pcruntime'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
```
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Once installed, the Runtime will automatically be replaced.
|
23
|
+
If you want to set it, you can use environment variable `EXECJS_RUNTIME=PCRuntime`.
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/execjs-pcruntime. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the Execjs::Pcruntime project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/execjs-pcruntime/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rake/testtask'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb', 'test/test_execjs.rb']
|
10
|
+
end
|
11
|
+
|
12
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'execjs/pcruntime'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'execjs/pcruntime/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'execjs-pcruntime'
|
9
|
+
spec.version = Execjs::PCRuntime::VERSION
|
10
|
+
spec.authors = ['White-Green']
|
11
|
+
spec.required_ruby_version = '>3.0.0'
|
12
|
+
spec.summary = 'Fast ExecJS Runtime using Process as a Context.'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
19
|
+
spec.metadata['source_code_uri'] = 'https://github.com/heyinc/execjs-pcruntime'
|
20
|
+
spec.metadata['rubygems_mfa_required'] = 'true'
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.require_paths = ['lib']
|
32
|
+
end
|
@@ -0,0 +1,268 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'execjs/runtime'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'json'
|
6
|
+
require 'net/protocol'
|
7
|
+
require 'net/http'
|
8
|
+
|
9
|
+
module ExecJS
|
10
|
+
module PCRuntime
|
11
|
+
# override ExecJS::Runtime
|
12
|
+
class ContextProcessRuntime < Runtime
|
13
|
+
# override ExecJS::Runtime::Context
|
14
|
+
class Context < Runtime::Context
|
15
|
+
# @param [String] runtime Instance of ContextProcessRuntime
|
16
|
+
# @param [String] source JavaScript source code that Runtime load at startup
|
17
|
+
# @param [any] options
|
18
|
+
def initialize(runtime, source = '', options = {})
|
19
|
+
super(runtime, source, options)
|
20
|
+
|
21
|
+
# @type [JSRuntimeHandle]
|
22
|
+
@runtime = runtime.create_runtime_handle
|
23
|
+
|
24
|
+
# load initial source to Context
|
25
|
+
@runtime.evaluate(source.encode('UTF-8'))
|
26
|
+
end
|
27
|
+
|
28
|
+
# override ExecJS::Runtime::Context#eval
|
29
|
+
# @param [String] source
|
30
|
+
# @param [any] _options
|
31
|
+
def eval(source, _options = {})
|
32
|
+
return unless /\S/.match?(source)
|
33
|
+
|
34
|
+
@runtime.evaluate("(#{source.encode('UTF-8')})")
|
35
|
+
end
|
36
|
+
|
37
|
+
# override ExecJS::Runtime::Context#exec
|
38
|
+
# @param [String] source
|
39
|
+
# @param [any] _options
|
40
|
+
def exec(source, _options = {})
|
41
|
+
@runtime.evaluate("(()=>{#{source.encode('UTF-8')}})()")
|
42
|
+
end
|
43
|
+
|
44
|
+
# override ExecJS::Runtime:Context#call
|
45
|
+
# @param [String] identifier
|
46
|
+
# @param [Array<_ToJson>] args
|
47
|
+
def call(identifier, *args)
|
48
|
+
@runtime.evaluate("(#{identifier}).apply(this, #{::JSON.generate(args)})")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Handle of JavaScript Runtime
|
53
|
+
# launch Runtime by .new and finished on finalizer
|
54
|
+
class JSRuntimeHandle
|
55
|
+
# @param [Array<String>] binary Launch command for the node(or similar JavaScript Runtime) binary,
|
56
|
+
# such as ['node'], ['deno', 'run'].
|
57
|
+
# @param [String] initial_source Path of .js Runtime loads at startup.
|
58
|
+
def initialize(binary, initial_source)
|
59
|
+
Dir::Tmpname.create 'execjs_pcruntime' do |path|
|
60
|
+
# Dir::Tmpname.create rescues Errno::EEXIST and retry block
|
61
|
+
# So, raise it if failed to create Process.
|
62
|
+
@runtime_pid = create_process(path, *binary, initial_source) || raise(Errno::EEXIST)
|
63
|
+
@socket_path = path
|
64
|
+
end
|
65
|
+
ObjectSpace.define_finalizer(self, self.class.finalizer(@runtime_pid))
|
66
|
+
end
|
67
|
+
|
68
|
+
# Evaluate JavaScript source code and return the result.
|
69
|
+
# @param [String] source JavaScript source code
|
70
|
+
# @return [object]
|
71
|
+
def evaluate(source)
|
72
|
+
post_request(@socket_path, '/eval', 'text/javascript', source)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Create a procedure to kill the Process that has specified pid.
|
76
|
+
# It used as the finalizer of JSRuntimeHandle.
|
77
|
+
# @param [Integer] pid
|
78
|
+
def self.finalizer(pid)
|
79
|
+
proc do
|
80
|
+
err = kill_process(pid)
|
81
|
+
warn err.full_message unless err.nil?
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Kill the Process that has specified pid.
|
86
|
+
# If raised error then return it.
|
87
|
+
# @param [Integer] pid
|
88
|
+
# @return [StandardError, nil] return error iff an error is raised
|
89
|
+
def self.kill_process(pid)
|
90
|
+
Process.kill(:KILL, pid)
|
91
|
+
nil
|
92
|
+
rescue StandardError => e
|
93
|
+
e
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
# Attempt to execute the block several times, spacing out the attempts over a certain period.
|
99
|
+
# @param [Integer] times maximum number of attempts
|
100
|
+
# @yieldreturn [Boolean] true iff succeed execute
|
101
|
+
# @return [Boolean] true if the block attempt is successful, false if the maximum number of attempts is reached
|
102
|
+
def delayed_retries(times)
|
103
|
+
while times.positive?
|
104
|
+
return true if yield
|
105
|
+
|
106
|
+
sleep 0.05
|
107
|
+
times -= 1
|
108
|
+
end
|
109
|
+
false
|
110
|
+
end
|
111
|
+
|
112
|
+
# Launch JavaScript Runtime Process.
|
113
|
+
# @param [String] socket_path path used for the UNIX domain socket
|
114
|
+
# it is passed at Runtime through the PORT environment variable
|
115
|
+
# @param [Array<String>] command command to start the Runtime such as ['node', 'runner.js']
|
116
|
+
# @return [Integer, nil] if the Process successfully launches, return its pid. if it fails return nil
|
117
|
+
def create_process(socket_path, *command)
|
118
|
+
pid = Process.spawn({ 'PORT' => socket_path }, *command)
|
119
|
+
|
120
|
+
unless delayed_retries(20) { File.exist?(socket_path) }
|
121
|
+
self.class.kill_process(pid)
|
122
|
+
return nil
|
123
|
+
end
|
124
|
+
|
125
|
+
begin
|
126
|
+
post_request(socket_path, '/')
|
127
|
+
rescue StandardError
|
128
|
+
self.class.kill_process(pid)
|
129
|
+
return nil
|
130
|
+
end
|
131
|
+
pid
|
132
|
+
end
|
133
|
+
|
134
|
+
# Create a socket connected to the Process.
|
135
|
+
# @return [Net::BufferedIO]
|
136
|
+
def create_socket(socket_path)
|
137
|
+
Net::BufferedIO.new(UNIXSocket.new(socket_path))
|
138
|
+
end
|
139
|
+
|
140
|
+
# Send request to JavaScript Runtime.
|
141
|
+
# @param [String] socket_path path of the UNIX domain socket
|
142
|
+
# @param [String] path Path on HTTP request such as '/eval'
|
143
|
+
# @param [String, nil] content_type Content-type of body
|
144
|
+
# @param [String, nil] body body of HTTP request
|
145
|
+
# @return [object?]
|
146
|
+
# There seems to be no particular meaning in dividing it any further and it's a simple sequential process
|
147
|
+
# so suppressing lint errors.
|
148
|
+
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize
|
149
|
+
def post_request(socket_path, path, content_type = nil, body = nil)
|
150
|
+
socket = create_socket socket_path
|
151
|
+
|
152
|
+
# timeout occurred during the test
|
153
|
+
socket.read_timeout *= 100
|
154
|
+
socket.write_timeout *= 100
|
155
|
+
|
156
|
+
request = Net::HTTP::Post.new(path)
|
157
|
+
request['Connection'] = 'close'
|
158
|
+
unless content_type.nil?
|
159
|
+
request['Content-Type'] = content_type
|
160
|
+
request.body = body
|
161
|
+
end
|
162
|
+
|
163
|
+
# Net::HTTPGenericRequest#exec
|
164
|
+
# I'd rather not use it as it's marked for 'internal use only', but I can't find a good alternative.
|
165
|
+
request.exec(socket, '1.1', path)
|
166
|
+
|
167
|
+
# Adopting RuboCop's proposal changes the operation and causes an infinite loop
|
168
|
+
# rubocop:disable Lint/Loop
|
169
|
+
begin
|
170
|
+
response = Net::HTTPResponse.read_new(socket)
|
171
|
+
end while response.is_a?(Net::HTTPContinue)
|
172
|
+
# rubocop:enable Lint/Loop
|
173
|
+
response.reading_body(socket, request.response_body_permitted?) {}
|
174
|
+
|
175
|
+
if response.code == '200'
|
176
|
+
result = response.body
|
177
|
+
::JSON.parse(response.body, create_additions: false) if /\S/.match?(result)
|
178
|
+
else
|
179
|
+
message, stack = response.body.split "\0"
|
180
|
+
error_class = /SyntaxError:/.match?(message) ? RuntimeError : ProgramError
|
181
|
+
error = error_class.new(message)
|
182
|
+
error.set_backtrace(stack)
|
183
|
+
raise error
|
184
|
+
end
|
185
|
+
end
|
186
|
+
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize
|
187
|
+
end
|
188
|
+
|
189
|
+
attr_reader :name
|
190
|
+
|
191
|
+
# @param [String] name name of Runtime
|
192
|
+
# @param [Array<String>] command candidates for JavaScript Runtime commands such as ['deno run', 'node']
|
193
|
+
# @param [String] runner_path path of the .js file to run in the Runtime
|
194
|
+
def initialize(name, command, runner_path = File.expand_path('runner.js', __dir__), deprecated: false)
|
195
|
+
super()
|
196
|
+
@name = name
|
197
|
+
@command = command
|
198
|
+
@runner_path = runner_path
|
199
|
+
@binary = nil
|
200
|
+
@deprecated = deprecated
|
201
|
+
end
|
202
|
+
|
203
|
+
# override ExecJS::Runtime#available?
|
204
|
+
def available?
|
205
|
+
require 'json'
|
206
|
+
binary ? true : false
|
207
|
+
end
|
208
|
+
|
209
|
+
# override ExecJS::Runtime#deprecated?
|
210
|
+
def deprecated?
|
211
|
+
@deprecated
|
212
|
+
end
|
213
|
+
|
214
|
+
# Launch JavaScript Runtime and return its handle.
|
215
|
+
# @return [JSRuntimeHandle]
|
216
|
+
def create_runtime_handle
|
217
|
+
JSRuntimeHandle.new(binary, @runner_path)
|
218
|
+
end
|
219
|
+
|
220
|
+
private
|
221
|
+
|
222
|
+
# Return the launch command for the JavaScript Runtime.
|
223
|
+
# @return [Array<String>]
|
224
|
+
def binary
|
225
|
+
@binary ||= which(@command)
|
226
|
+
end
|
227
|
+
|
228
|
+
# Locate a executable file in the path.
|
229
|
+
# @param [Array<String>] commands candidates for commands such as ['deno run', 'node']
|
230
|
+
# @return [Array<String>] the absolute path of the command and command-line arguments
|
231
|
+
# e.g. ["/the/absolute/path/to/deno", "run"]
|
232
|
+
def which(commands)
|
233
|
+
commands.each do |command|
|
234
|
+
command, *args = split_command_string command
|
235
|
+
command = search_executable_path command
|
236
|
+
return [command] + args unless command.nil?
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
# Search for absolute path of the executable file from the command.
|
241
|
+
# @param [String] command
|
242
|
+
# @return [String, nil] the absolute path of the command, or nil if not found
|
243
|
+
# It seems that further method splitting might actually make it harder to read, so suppressing
|
244
|
+
# rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
245
|
+
def search_executable_path(command)
|
246
|
+
@extensions ||= ExecJS.windows? ? ENV['PATHEXT'].split(File::PATH_SEPARATOR) + [''] : ['']
|
247
|
+
@path ||= ENV['PATH'].split(File::PATH_SEPARATOR) + ['']
|
248
|
+
@path.each do |base_path|
|
249
|
+
@extensions.each do |extension|
|
250
|
+
executable_path = base_path == '' ? command + extension : File.join(base_path, command + extension)
|
251
|
+
return executable_path if File.executable?(executable_path) && File.exist?(executable_path)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
nil
|
255
|
+
end
|
256
|
+
# rubocop:enable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
257
|
+
|
258
|
+
# Split command string
|
259
|
+
# split_command_string "deno run" # ["deno", "run"]
|
260
|
+
# @param [String] command command string
|
261
|
+
# @return [Array<String>] array split from the command string
|
262
|
+
def split_command_string(command)
|
263
|
+
regex = /([^\s"']+)|"([^"]+)"|'([^']+)'(?:\s+|\s*\Z)/
|
264
|
+
command.scan(regex).flatten.compact
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
const vm = require('vm');
|
2
|
+
let context = vm.createContext();
|
3
|
+
vm.runInContext("delete console;", context);
|
4
|
+
|
5
|
+
const http = require('http');
|
6
|
+
const server = http.createServer(function (req, res) {
|
7
|
+
switch (req.url) {
|
8
|
+
case '/':
|
9
|
+
res.statusCode = 200;
|
10
|
+
res.end();
|
11
|
+
break;
|
12
|
+
case '/eval':
|
13
|
+
let allData = '';
|
14
|
+
req.on('data', (data) => allData += data);
|
15
|
+
req.on('end', () => {
|
16
|
+
try {
|
17
|
+
const result = vm.runInContext(allData, context, "(execjs)");
|
18
|
+
res.statusCode = 200;
|
19
|
+
res.setHeader('Content-Type', 'application/json');
|
20
|
+
res.end(JSON.stringify(result), 'utf-8');
|
21
|
+
} catch (e) {
|
22
|
+
res.statusCode = 500;
|
23
|
+
res.setHeader('Content-Type', 'text/plain');
|
24
|
+
res.end(e.toString() + "\0" + (e.stack || ""));
|
25
|
+
}
|
26
|
+
});
|
27
|
+
break;
|
28
|
+
case '/exit':
|
29
|
+
process.exit(0);
|
30
|
+
break;
|
31
|
+
default:
|
32
|
+
console.log("Unknown Path");
|
33
|
+
break;
|
34
|
+
}
|
35
|
+
});
|
36
|
+
|
37
|
+
const port = process.env.PORT || 3001;
|
38
|
+
server.listen(port);
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'execjs/pcruntime/version'
|
4
|
+
require 'execjs/pcruntime/context_process_runtime'
|
5
|
+
require 'execjs/runtimes'
|
6
|
+
|
7
|
+
module ExecJS
|
8
|
+
# extends ExecJS::Runtimes
|
9
|
+
module Runtimes
|
10
|
+
PCRuntime = PCRuntime::ContextProcessRuntime.new(
|
11
|
+
'Node.js (V8) Process as Context',
|
12
|
+
%w[nodejs node]
|
13
|
+
)
|
14
|
+
|
15
|
+
runtimes.unshift(PCRuntime)
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: execjs-pcruntime
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- White-Green
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".rubocop.yml"
|
21
|
+
- CODE_OF_CONDUCT.md
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- execjs-pcruntime.gemspec
|
29
|
+
- lib/execjs/pcruntime.rb
|
30
|
+
- lib/execjs/pcruntime/context_process_runtime.rb
|
31
|
+
- lib/execjs/pcruntime/runner.js
|
32
|
+
- lib/execjs/pcruntime/runtimes.rb
|
33
|
+
- lib/execjs/pcruntime/version.rb
|
34
|
+
homepage:
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata:
|
38
|
+
allowed_push_host: https://rubygems.org
|
39
|
+
source_code_uri: https://github.com/heyinc/execjs-pcruntime
|
40
|
+
rubygems_mfa_required: 'true'
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.0.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.3.26
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Fast ExecJS Runtime using Process as a Context.
|
60
|
+
test_files: []
|