Skip to content

Commit

Permalink
ExpressJS 'listen' support (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect authored Jun 14, 2022
1 parent 4c9e137 commit 3471c2a
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,4 @@ jobs:
token: ${{ secrets.NPM_TOKEN }}
access: public
if: |
matrix.config.os == 'ubuntu-18.04' && matrix.config.node == '14' && steps.extract_branch.outputs.branch == 'main'
matrix.config.os == 'ubuntu-18.04' && matrix.config.node == '14' && matrix.architecture == 'x64' && steps.extract_branch.outputs.branch == 'main'
29 changes: 29 additions & 0 deletions lib/close.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright Netfoundry, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


/**
* close()
*
* @param {*} conn
*/
const close = ( conn, ) => {

ziti.ziti_close( conn );

};

exports.close = close;
40 changes: 13 additions & 27 deletions lib/express-listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

const zitiListen = require('./listen').listen;
const EventEmitter = require('events');
const { ZitiSocket } = require('./ziti-socket');


const normalizedArgsSymbol = Symbol('normalizedArgs');

Expand All @@ -30,8 +32,6 @@ var _servers = new Map();
*/
Server.prototype.on_listen = ( status ) => {

console.log('----------- Now inside on_listen callback ----------, status is: %o', status);

};

/**
Expand All @@ -41,8 +41,6 @@ Server.prototype.on_listen = ( status ) => {
*/
Server.prototype.on_listen_client = ( obj ) => {

console.log('----------- Now inside on_listen_client callback ----------, obj is: %o', obj);

};

/**
Expand All @@ -51,9 +49,7 @@ Server.prototype.on_listen = ( status ) => {
* @param {*} obj
*/
Server.prototype.on_client_write = ( obj ) => {

console.log('----------- Now inside on_client_write callback ----------, obj is: %o', obj);


};

/**
Expand All @@ -63,16 +59,13 @@ Server.prototype.on_listen = ( status ) => {
*/
Server.prototype.on_listen_client_connect = ( obj ) => {

console.log('----------- Now inside on_listen_client_connect callback ----------, obj is: %o', obj);

let self = _servers.get(obj.js_arb_data);

// console.log('----------- Now inside on_listen_client_connect callback ----------, self is: %o', self);
const socket = new ZitiSocket({ client: obj.client });

console.log('----------- Now inside on_listen_client_connect callback ----------, emitting `connection` event for client/socket: %o', obj.client);
self.emit('connection', obj.client);


self._socket = socket;

self.emit('connection', socket);
};

/**
Expand All @@ -81,13 +74,11 @@ Server.prototype.on_listen = ( status ) => {
* @param {*} obj
*/
Server.prototype.on_listen_client_data = ( obj ) => {

console.log('----------- Now inside on_listen_client_data callback ----------, obj is: %o', obj);

if (obj.app_data) {
console.log('----------- app_data ----------, app_data string is: \n%o', obj.app_data.toString());
}


let self = _servers.get(obj.js_arb_data);
let socket = self._socket;

socket.captureData(obj.app_data);
};


Expand Down Expand Up @@ -173,19 +164,15 @@ function Server(serviceName, options, connectionListener) {
this.noDelay = Boolean(options.noDelay);
this.keepAlive = Boolean(options.keepAlive);
this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);

}
Object.setPrototypeOf(Server.prototype, EventEmitter.prototype);
Object.setPrototypeOf(Server, EventEmitter);


Server.prototype.listen = function( serviceName, ...args ) {

console.log('=======================> express-listener: Server.prototype.listen() entered: arguments: ', arguments);

let normalized = normalizeArgs(args);
normalized = normalizeArgs(normalized[0]);
console.log('=======================> express-listener: Server.prototype.listen() normalized: ', normalized);

// let options = normalized[0]; // we currently ignore options (a.k.a. `port`)
let cb = normalized[1];
Expand All @@ -195,7 +182,6 @@ Server.prototype.listen = function( serviceName, ...args ) {
_servers.set(index, this);

zitiListen( serviceName, index, cb, this.on_listen_client, this.on_listen_client_connect, this.on_listen_client_data );

};

Server.prototype.address = function() {
Expand Down Expand Up @@ -263,5 +249,5 @@ Object.defineProperty(Server.prototype, 'listening', {

module.exports = {
Server,
};
};

86 changes: 12 additions & 74 deletions lib/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

const net = require('./express-listener');

const expressListener = require('./express-listener');
const { Server } = require('_http_server'); // from NodeJS internals




const getWrappedExpressApp = ( express, serviceName ) => {
/**
* express()
*
* @param {*} express
* @param {*} serviceName
*/
const express = ( express, serviceName ) => {

var wrappedExpressApp = express();

Expand All @@ -30,47 +34,19 @@ const getWrappedExpressApp = ( express, serviceName ) => {
*
* A node `http.Server` is returned, with this
* application (which is a `Function`) as its
* callback. If you wish to create both an HTTP
* and HTTPS server you may do so with the "http"
* and "https" modules as shown here:
*
* var http = require('http')
* , https = require('https')
* , express = require('express')
* , app = express();
*
* http.createServer(app).listen(80);
* https.createServer({ ... }, app).listen(443);
* callback.
*
* @return {http.Server}
* @public
*/
wrappedExpressApp.listen = function() {

console.log('=======================> wrappedExpressApp.listen() entered: arguments: ', arguments);

// var server = http.createServer(this);
// console.log('=======================> wrappedExpressApp.listen() 1 server: ', server);

Object.setPrototypeOf(Server.prototype, net.Server.prototype);
Object.setPrototypeOf(Server, net.Server);
Object.setPrototypeOf(Server.prototype, expressListener.Server.prototype);
Object.setPrototypeOf(Server, expressListener.Server);
var server = new Server(this);

expressListener.Server.call( server, serviceName, { } );

net.Server.call(
server,
serviceName,
{
// allowHalfOpen: true,
// noDelay: options.noDelay,
// keepAlive: options.keepAlive,
// keepAliveInitialDelay: options.keepAliveInitialDelay
});


// zitiListen( serviceName, on_listen, on_listen_client, on_listen_client_connect, on_listen_client_data );

// return server.listen.apply(server, serviceName, arguments);
return server.listen(serviceName, arguments);

};
Expand All @@ -79,42 +55,4 @@ const getWrappedExpressApp = ( express, serviceName ) => {

};


/**
* express()
*
* @param {*} express
* @param {*} serviceName
*/
const express = ( express, serviceName ) => {

var app = getWrappedExpressApp( express, serviceName);

// console.log('wrappedExpressApp: ', app);

// const wrappedExpressResponse = Object.create( app.response, {

// data: {
// value: function(data) {
// return this.status(200).json({status: true, data: data});
// },
// },

// message: {
// value: function(msg) {
// return this.status(200).json({status: true, message: msg});
// },
// },

// });

// app.response = Object.create(wrappedExpressResponse);

return app;

};

exports.express = express;



21 changes: 19 additions & 2 deletions lib/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,33 @@ limitations under the License.
*/


/**
* on_write()
*
*/
const on_write = ( status ) => {

};


/**
* write()
*
* @param {*} conn
* @param {*} buf
* @param {*} on_write callback
*/
const write = ( conn, buf, on_write ) => {
const write = ( conn, buf, on_write_cb ) => {

let cb;

if (typeof on_write_cb === 'undefined') {
cb = on_write;
} else {
cb = on_write_cb;
}

ziti.ziti_write( conn, buf, on_write );
ziti.ziti_write( conn, buf, cb );

};

Expand Down
Loading

0 comments on commit 3471c2a

Please sign in to comment.