Skip to content

Commit cb5a4b3

Browse files
committed
refactor: use plot/base/server for serving content
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 927a38f commit cb5a4b3

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

lib/node_modules/@stdlib/plot/base/view/lib/electron/index.js

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var ENV = require( '@stdlib/process/env' );
3030
var copy = require( '@stdlib/utils/copy' );
3131
var merge = require( '@stdlib/utils/merge' );
3232
var readFileSync = require( '@stdlib/fs/read-file' ).sync;
33-
var httpServer = require( '@stdlib/net/disposable-http-server' );
33+
var createServer = require( '@stdlib/plot/base/server' );
3434
var tryRequire = require( '@stdlib/utils/try-require' );
3535

3636

@@ -54,6 +54,7 @@ var electron = tryRequire( '@stdlib/electron' );
5454
* @throws {Error} Electron must be properly installed
5555
*/
5656
function view( plot, html ) {
57+
var server;
5758
var index;
5859
var opts;
5960
var css;
@@ -76,31 +77,29 @@ function view( plot, html ) {
7677
css = readFileSync( css, opts );
7778
index = index.replace( /\{\{reset\}\}/, css );
7879

79-
debug( 'Creating a disposable HTTP server...' );
80-
opts = {
81-
'html': index,
82-
'open': false
83-
};
84-
httpServer( opts, onReady );
80+
debug( 'Creating a server...' );
81+
createServer( router, onReady );
8582

8683
/**
8784
* Callback invoked once a server is ready to receive requests.
8885
*
8986
* @private
9087
* @param {(Error|null)} error - error object
91-
* @param {Server} server - HTTP server
88+
* @param {Server} _server - server instance
9289
* @throws {Error} unexpected error
9390
*/
94-
function onReady( error, server ) {
91+
function onReady( error, _server ) {
9592
var child;
9693
var addr;
9794
var opts;
9895
var env;
9996
if ( error ) {
10097
throw error;
10198
}
99+
server = _server;
100+
102101
addr = server.address();
103-
debug( 'HTTP server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );
102+
debug( 'Server initialized. Server is listening for requests on %s:%d.', addr.address, addr.port );
104103

105104
debug( 'Electron executable: %s.', electron );
106105

@@ -111,26 +110,51 @@ function view( plot, html ) {
111110
'PLOT_WIDTH': plot.width,
112111
'PLOT_HEIGHT': plot.height,
113112
'PLOT_APP_PATH': __dirname,
114-
'PLOT_MIN_WIDTH': 100,
115-
'PLOT_MIN_HEIGHT': 100,
116113
'PLOT_TITLE': plot.title || 'stdlib'
117114
};
118115
debug( 'Electron process environment variables: %s.', JSON.stringify( env ) );
119116

120117
opts = {
121118
'cwd': __dirname,
122119
'detached': true,
123-
'stdio': 'ignore'
120+
'stdio': 'ignore' // NOTE: when debugging, set to 'inherit'
124121
};
125122
debug( 'Electron process options: %s.', JSON.stringify( opts ) );
126123

127124
// Merge the current process' environment variables:
128125
opts.env = merge( {}, copy( ENV ), env );
126+
opts.env.DEBUG = '*';
129127

130128
debug( 'Spawning an electron process...' );
131129
child = spawn( electron, [ './main.js' ], opts );
132130
child.unref();
133131
}
132+
133+
/**
134+
* Request handler.
135+
*
136+
* @private
137+
* @param {IncomingMessage} request - HTTP request object
138+
* @param {ServerResponse} response - HTTP response object
139+
* @returns {void}
140+
*/
141+
function router( request, response ) {
142+
debug( 'Received a request for %s', request.url );
143+
response.once( 'finish', onFinish );
144+
response.write( index );
145+
response.end();
146+
}
147+
148+
/**
149+
* Callback invoked once the server should close.
150+
*
151+
* @private
152+
*/
153+
function onFinish() {
154+
debug( 'Finished serving content.' );
155+
debug( 'Closing the server...' );
156+
server.close();
157+
}
134158
}
135159

136160

lib/node_modules/@stdlib/plot/base/view/lib/electron/main.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,30 @@
1616
* limitations under the License.
1717
*/
1818

19+
/* eslint-disable max-len */
20+
1921
'use strict';
2022

2123
// MODULES //
2224

2325
var logger = require( 'debug' );
24-
var app = require( 'electron' ).app; // eslint-disable-line stdlib/require-file-extensions
25-
var BrowserWindow = require( 'electron' ).BrowserWindow; // eslint-disable-line stdlib/require-file-extensions
26+
var electron = require( 'electron' ); // eslint-disable-line stdlib/require-file-extensions
2627
var ENV = require( '@stdlib/process/env' );
2728

2829

2930
// VARIABLES //
3031

3132
var debug = logger( 'plot:view:electron:main-process' );
33+
34+
// Define convenience aliases:
35+
var BrowserWindow = electron.BrowserWindow;
36+
var session = electron.session;
37+
var app = electron.app;
38+
39+
// Define the error code which arises when attempting to use a self-signed certificate over localhost:
40+
var ERR_CERT_AUTHORITY_INVALID = -202; // eslint-disable-line id-length
41+
42+
// Initialize a reference to the main window which will be created:
3243
var mainWindow = null;
3344

3445

@@ -43,36 +54,52 @@ function createWindow() {
4354
var opts;
4455
var url;
4556

57+
// Intercept certificate verification to allow using a local self-signed certificate:
58+
session.defaultSession.setCertificateVerifyProc( onVerifyCertificate );
59+
4660
opts = {
4761
'width': parseInt( ENV.PLOT_WIDTH, 10 ) + 80,
4862
'height': parseInt( ENV.PLOT_HEIGHT, 10 ) + 20,
4963
'title': ENV.PLOT_TITLE,
5064

51-
// 'minWidth': parseInt( ENV.PLOT_MIN_WIDTH, 10 ), // TODO: needed?
52-
53-
// 'minHeight': parseInt( ENV.PLOT_MIN_HEIGHT, 10 ), // TODO: needed?
54-
5565
// 'titleBarStyle': 'hidden-inset', // hide title bar on OS X
5666

5767
'useContentSize': true // specify web page size only considering the content
5868
};
59-
debug( 'Creating a new browser window configured with the following options: %s.', JSON.stringify( opts ) );
69+
debug( 'Creating a new window configured with the following options: %s.', JSON.stringify( opts ) );
6070
mainWindow = new BrowserWindow( opts );
6171

72+
// mainWindow.webContents.openDevTools(); // NOTE: uncomment to allow using the inspector in the Electron window
73+
6274
mainWindow.on( 'close', onClose );
6375

64-
url = 'http://'+ENV.SERVER_ADDRESS+':'+ENV.SERVER_PORT+'/index.html';
76+
url = 'https://'+ENV.SERVER_ADDRESS+':'+ENV.SERVER_PORT+'/index.html';
6577
debug( 'Loading %s.', url );
6678
mainWindow.loadURL( url );
6779
}
6880

81+
/**
82+
* Callback invoked when attempting to verify a certificate.
83+
*
84+
* @private
85+
* @param {Request} request - request object
86+
* @param {Callback} clbk - callback to invoke upon verification
87+
* @returns {void}
88+
*/
89+
function onVerifyCertificate( request, clbk ) {
90+
if ( request.errorCode === ERR_CERT_AUTHORITY_INVALID && request.hostname === ENV.SERVER_ADDRESS ) {
91+
return clbk( 0 ); // allow
92+
}
93+
return clbk( -3 ); // use default verification result
94+
}
95+
6996
/**
7097
* Callback invoked once a window closes.
7198
*
7299
* @private
73100
*/
74101
function onClose() {
75-
debug( 'Window closed. Dereferencing window object to allow for GC...' );
102+
debug( 'Window closed. De-referencing window object to allow for garbage collection...' );
76103
mainWindow = null;
77104
}
78105

0 commit comments

Comments
 (0)