web-repl 0.2 → 0.3

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
  SHA1:
3
- metadata.gz: c0998211915ac759b165b949d9feb445f3255996
4
- data.tar.gz: 554e6169fce11bd5e0770923738bb90580e032ce
3
+ metadata.gz: b5ade737ee82d2cde19f9d7bf3241fbc27eec52c
4
+ data.tar.gz: ea558223a3d3695dde067d9687c7e11d086c4348
5
5
  SHA512:
6
- metadata.gz: a897a48b40350ee85b38336297376e8d1b5fe90e646a2eb860d492fe3e0363dc03cc2b5d0d7875a0c85f4c3827ccda4c990752cfd72eb9e151cf755d28d075dc
7
- data.tar.gz: 92e2053ec83d835a829615535b81235eb42389034f757f54912f12e4b55997c4840ea41b032adb30a806824f0db53efed829d5082a04edcac481a8be858c5072
6
+ metadata.gz: 4f0e00b0c62aa6d2e4539a0ae72e685f35c32de08ac9f86a8e582b8539d81bf0785925a5de5b21bf2cea9153f19d85b7c703efbda932ab89281666e18e0cb05e
7
+ data.tar.gz: a5000ac001d4fac7a801ff93887e30a30c1a7adc6e6227ce625658d45e163d3047f953967351735db0a6a549c58c0b7d0b44c6efefab80460fe65a1633f39a20
data/README.md CHANGED
@@ -12,7 +12,7 @@ I've been working on a toy project recently that requires browser content to be
12
12
 
13
13
  There are similar tools that run in nodejs and other languages but this is convenient for me because my project uses a Ruby backend anyway.
14
14
 
15
- Server/client interaction uses JSON over Websocket. There's no extra attention to security here other than what is implicit in Websockets, so please use at your own discretion.
15
+ Under the hood, communication is done with JSON over Websocket. Note that there's no extra attention to security here other than what is generally implicit with a Websocket, so please use at your own discretion.
16
16
 
17
17
  #### Usage
18
18
 
@@ -32,7 +32,7 @@ To enable the browser side of this, include something like this in the head of y
32
32
 
33
33
  The javascript assets for this project are located in the [/js directory](https://github.com/arirusso/web-repl/tree/master/js).
34
34
 
35
- There is also a full example of a webpage (with [rack](http://rack.github.io/) configuration) in the [/examples/page directory](https://github.com/arirusso/web-repl/tree/master/examples/page)
35
+ There is also a full example of a webpage (with simple [rack](http://rack.github.io/) webserver configuration) in the [/examples/page directory](https://github.com/arirusso/web-repl/tree/master/examples/page)
36
36
 
37
37
  ###### REPL
38
38
 
@@ -0,0 +1 @@
1
+ function ReplConnection(e,t,n){if(n===null){n={}}this.debug=!!n.debug;this.reconnect=!!n.reconnect;this.retryTime=n.retryTime||1e3;this.onReceive=n.onReceive;this.host=e;this.port=t;this.active=false;this.socket;this.supported="WebSocket"in window;if(this.supported){console.log("REPL: socket ok")}else{console.log("REPL: socket not supported")}}ReplConnection.prototype.eval=function(statement,options){options=typeof options==="undefined"?{}:options;response={};try{if(typeof options.eval==="function"){response.value=options.eval(statement)}else{response.value=eval(statement)}}catch(err){response.error=err.message}return response};ReplConnection.prototype.initSocket=function(e){if(this.socket!==undefined&&this.socket!==null){this.socket.close()}var t="ws://"+this.host+":"+this.port+"/echo";this.socket=new WebSocket(t);e()};ReplConnection.prototype.handleSocketOpen=function(){this.active=true;console.log("REPL: socket ready")};ReplConnection.prototype.tryConnection=function(){console.log("REPL: waiting for connection");if(this.reconnect){var e=this;window.setTimeout(function(){e.start()},this.retryTime)}};ReplConnection.prototype.handleSocketClose=function(e){if(!this.active){this.tryConnection()}else{console.log("REPL: socket closed");this.active=false;if(this.reconnect){this.tryConnection()}}};ReplConnection.prototype.handleMessageReceived=function(e){if(this.debug){console.log("REPL: message received");console.log(e)}var t=JSON.parse(e.data);var n=t.timestamp;t.timestamp=new Date(n);if(this.onReceive!==undefined){this.onReceive(t)}var r=this.eval(t.statement);var i=this.prepareJSONResponse(r);if(this.debug){console.log("REPL: replying ");console.log(i)}this.socket.send(i)};ReplConnection.prototype.prepareJSONResponse=function(e){var t;try{e.timestamp=(new Date).getTime();t=JSON.stringify(e)}catch(n){e.value=null;e.error=n.message;t=this.prepareJSONResponse(e)}return t};ReplConnection.prototype.initEventHandling=function(){var e=this;this.socket.onopen=function(){e.handleSocketOpen()};this.socket.onclose=function(t){e.handleSocketClose(t)};this.socket.onmessage=function(t){e.handleMessageReceived(t)}};ReplConnection.prototype.start=function(e){if(this.supported){var t=this;this.initSocket(function(){t.initEventHandling();if(e!==undefined){e(t)}})}}
@@ -20,10 +20,17 @@ function ReplConnection(host, port, options) {
20
20
  }
21
21
 
22
22
  // This is the "eval" for the REPL
23
- ReplConnection.prototype.eval = function(statement) {
23
+ // statement: the statement to evaluate
24
+ // options: eval: a custom eval function
25
+ ReplConnection.prototype.eval = function(statement, options) {
26
+ options = (typeof options === "undefined") ? {} : options;
24
27
  response = {}
25
28
  try {
26
- response.value = eval(statement);
29
+ if (typeof options.eval === "function") {
30
+ response.value = options.eval(statement);
31
+ } else {
32
+ response.value = eval(statement);
33
+ }
27
34
  } catch(err) {
28
35
  response.error = err.message;
29
36
  }
@@ -10,9 +10,10 @@ require "web-repl/messager"
10
10
  require "web-repl/patch"
11
11
  require "web-repl/repl"
12
12
 
13
+ # A Javascript REPL that runs in Ruby. Evaluation is done by a web browser instance.
13
14
  module WebRepl
14
15
 
15
- VERSION = "0.2"
16
+ VERSION = "0.3"
16
17
 
17
18
  # Shortcut to REPL.start
18
19
  def self.start(*a)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-repl
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Russo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-04 00:00:00.000000000 Z
11
+ date: 2014-05-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -92,6 +92,7 @@ files:
92
92
  - README.md
93
93
  - bin/web-repl
94
94
  - js/replConnection-0.1.min.js
95
+ - js/replConnection-0.3.min.js
95
96
  - js/replConnection.js
96
97
  - lib/web-repl.rb
97
98
  - lib/web-repl/messager.rb