Skip to content

Commit 62a5652

Browse files
committed
Add HTTP example
1 parent fa80c28 commit 62a5652

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var http = require( 'http' );
22+
var string2buffer = require( '@stdlib/buffer/from-string' );
23+
var stdout = require( '@stdlib/streams/node/stdout' );
24+
25+
// Define the command we want to execute:
26+
var cmd = string2buffer( 'var x = base.sin( 3.14 ); var y = base.cos( x )' );
27+
28+
// Define request options:
29+
var ropts = {
30+
'protocol': 'http:',
31+
'host': 'localhost',
32+
'port': 7334, // NOTE: this will need to be adjusted based on server port
33+
'method': 'POST',
34+
'headers': {
35+
'Content-Length': cmd.byteLength
36+
}
37+
};
38+
39+
// Create a new HTTP request and send the command:
40+
var req = http.request( ropts, onResponse );
41+
req.on( 'error', onError );
42+
req.write( cmd );
43+
req.end();
44+
45+
function onError( error ) {
46+
console.error( 'Error: %s', error.message );
47+
}
48+
49+
function onResponse( res ) {
50+
if ( res.statusCode !== 200 ) {
51+
console.error( 'Something went wrong.' );
52+
return;
53+
}
54+
res.pipe( stdout );
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var httpServer = require( '@stdlib/net/http-server' );
22+
var passThrough = require( '@stdlib/streams/node/transform' );
23+
var REPL = require( './../../lib' );
24+
25+
// Define server options:
26+
var sopts = {
27+
'port': 7331,
28+
'maxport': 9999,
29+
'hostname': 'localhost'
30+
};
31+
32+
// Create a function for creating an HTTP server:
33+
var createServer = httpServer( sopts, onRequest );
34+
35+
// Start the HTTP server:
36+
createServer( done );
37+
38+
function done( error, server ) {
39+
var addr;
40+
if ( error ) {
41+
throw error;
42+
}
43+
// Log the server address so we know on what port we are listening:
44+
addr = server.address();
45+
console.log( 'Address: %s:%d', addr.address, addr.port );
46+
}
47+
48+
function onRequest( req, res ) {
49+
var istream;
50+
var ostream;
51+
var ropts;
52+
var repl;
53+
54+
res.setHeader( 'content-type', 'multipart/octet-stream' );
55+
56+
// Define "pass through" transform streams for handling request and response data:
57+
istream = passThrough();
58+
ostream = passThrough();
59+
60+
// Setup the data flow:
61+
req.pipe( istream );
62+
ostream.pipe( res );
63+
64+
// Define the REPL options, resetting the welcome message and prompts so that we only stream the evaluated results:
65+
ropts = {
66+
'input': istream,
67+
'output': ostream,
68+
'global': true,
69+
'welcome': '',
70+
'inputPrompt': '',
71+
'outputPrompt': '',
72+
'padding': 0
73+
};
74+
75+
// Create a new REPL instance:
76+
repl = new REPL( ropts );
77+
78+
// Listen for when the REPL has "drained" its command queue:
79+
repl.once( 'drain', onDrain );
80+
81+
function onDrain() {
82+
// Once the REPL drains, we've finished executing commands, so we can end the response and close the REPL:
83+
res.end();
84+
repl.close();
85+
}
86+
}

0 commit comments

Comments
 (0)