|
| 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