Skip to content

Commit

Permalink
Modules modernization (#412)
Browse files Browse the repository at this point in the history
* var => const/let + other modernizations

* replaced var with const in examples

* code modernization

* code modernization, removed obsolete require of Binary, ByteArray and ByteString (these are globally defined)

* code modernization, added tests for net module

* code modernization

* code modernization

* code modernization, added test for help() method

* code modernization

* code modernization

* use require instead of include in code example to make things explicit

* avoid redeclaration of consts

binary module uses defineClass to modify the global scope, so we can't (re)declare ie. const Binary

* modernized/simplified code, added test for Semaphore

* code modernization

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization

* code modernization (ie. var -> const/let)

* bugfix: arrow functions have no arguments object

* code modernization

disentangled Frame and Profiler constructors: moved Frame constructor out of
Profiler constructor, and pass the stack it needs as argument

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* replaced include() in code example with more explicit require

* code modernization (ie. var -> const/let), ZipIterator is now a Generator function

* added missing tests

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

removed partition() method - this method was obviously broken and not covered
by tests, so i assume it's obsolete

* code modernization (ie. var -> const/let)

* use strict comparisons and 0o1234 for octals, minor code formatting

* code modernization (ie. var -> const/let), fixed an error in parseFileUpload() - unfolding multiline headers couldn't have worked

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* code modernization (ie. var -> const/let)

* stack traces now exclude the test module too, code modernization

* code modernization

* #409 implemented binary.toByteString() as replacement of the
(now deprecated) String.prototype.toByteString() method

* minor: simplified named exports

* regression fix: short option can be null/undefined

* modified prototype construction of AssertionError and
ArgumentsError (jsdoc rendered the constructor property)

* regression fix: printResult and printError must be declared as local const variables,
otherwise RingoShell class is unable to invoke them.

* regression fix: objects.merge() must handle null/undefined arguments

removed hasOwnProperty() check since Object.keys() returns only
own enumerable property names

* fixed variable declaration

* added worker test

* fixed write/writeln method binding

* reverted change to arrow functions to maintain correct scope

* fixed & modernized examples

* #409 binary module no longer modifies the String prototype

also fixes toByteString to return a ByteString, not a ByteArray

Co-authored-by: Robert Gaggl <[email protected]>
  • Loading branch information
grob and Robert Gaggl authored Dec 11, 2020
1 parent c2c0d19 commit b085a7a
Show file tree
Hide file tree
Showing 82 changed files with 3,126 additions and 3,378 deletions.
20 changes: 9 additions & 11 deletions examples/eventsource-server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Simple event source server demo
var response = require("ringo/jsgi/response");
var arrays = require("ringo/utils/arrays");
var {EventSource, isEventSourceRequest} = require("ringo/jsgi/eventsource");
const response = require("ringo/jsgi/response");
const arrays = require("ringo/utils/arrays");
const {EventSource, isEventSourceRequest} = require("ringo/jsgi/eventsource");

var connections = module.singleton('connections', function() {
return [];
});
const connections = module.singleton('connections', () => []);

exports.app = function(req) {
exports.app = (req) => {
if (isEventSourceRequest(req)) {
var eventSource = new EventSource(req);
const eventSource = new EventSource(req);
eventSource.start({
'Access-Control-Allow-Origin': '*'
});
Expand All @@ -20,7 +18,7 @@ exports.app = function(req) {
}
};

function doPing() {
const doPing = () => {
console.info("Sending ping to all ", connections.length ,"connections");
connections.forEach(function(eventSource) {
try {
Expand All @@ -33,6 +31,6 @@ function doPing() {
}

if (require.main == module) {
var server = require("ringo/httpserver").main(module.id);
const server = require("ringo/httpserver").main(module.id);
setInterval(doPing, 2 * 1000);
}
}
12 changes: 6 additions & 6 deletions examples/file-term.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var fs = require('fs');
var {GREEN, BLUE, writeln} = require('ringo/term');
const fs = require('fs');
const {GREEN, BLUE, writeln, RESET} = require('ringo/term');

var filename = module.path;
const filename = module.path;

// text streams have an iterator that reads the next line
var file = fs.open(filename); // text mode
let file = fs.open(filename); // text mode
file.forEach(function(line) {
writeln(GREEN, line);
writeln(GREEN, line, RESET);
});

// binary streams read into ByteArrays/ByteStrings
file = fs.open(filename, {binary: true});
writeln(BLUE, file.read())
writeln(BLUE, file.read(), RESET)
6 changes: 3 additions & 3 deletions examples/httpserver-app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var response = require("ringo/jsgi/response");
const response = require("ringo/jsgi/response");

module.exports = function(req) {
module.exports = (req) => {
return response.html("Hello World!");
};
};
12 changes: 6 additions & 6 deletions examples/httpserver-async-worker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var fs = require("fs");
const fs = require("fs");

var onmessage = function(event) {
var {response, file} = event.data;
var stream = fs.openRaw(file);
var intervalId = setInterval(function() {
const onmessage = (event) => {
const {response, file} = event.data;
const stream = fs.openRaw(file);
const intervalId = setInterval(() => {
try {
var buf = stream.read(4096);
const buf = stream.read(4096);
if (buf.length > 0) {
response.write(buf);
response.flush();
Expand Down
12 changes: 6 additions & 6 deletions examples/httpserver-async.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var {AsyncResponse} = require('ringo/jsgi/connector');
var {Worker} = require("ringo/worker");
const {AsyncResponse} = require('ringo/jsgi/connector');
const {Worker} = require("ringo/worker");

var worker = module.singleton("worker", function() {
var worker = new Worker(module.resolve("./httpserver-async-worker"));
const worker = module.singleton("worker", () => {
const worker = new Worker(module.resolve("./httpserver-async-worker"));
worker.onmessage = function(event) {
console.log('Got message from worker:', event.data);
};
Expand All @@ -12,8 +12,8 @@ var worker = module.singleton("worker", function() {
return worker;
});

exports.app = function(request) {
var response = new AsyncResponse(request, 0, true);
exports.app = (request) => {
const response = new AsyncResponse(request, 0, true);
response.start(200, {'Content-Type': 'image/png'});
worker.postMessage({
"response": response,
Expand Down
12 changes: 6 additions & 6 deletions examples/httpserver-default.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
var {HttpServer} = require("../lib/main");
const {HttpServer} = require("ringo/httpserver");

var httpServer = new HttpServer();
const httpServer = new HttpServer();
httpServer.enableSessions({
"name": "myapp"
});

// init the application context
var appContext = httpServer.serveApplication("/", module.resolve("./app"), {
const appContext = httpServer.serveApplication("/", module.resolve("./httpserver-app"), {
"sessions": true
});
// and add a websocket to it
appContext.addWebSocket("/events", function() {});
appContext.addWebSocket("/events", () => {});

// initialize static file serving
var staticContext = httpServer.serveStatic("/static", module.resolve("./"), {
const staticContext = httpServer.serveStatic("/static", module.resolve("./"), {
"allowDirectoryListing": true
});

Expand All @@ -31,4 +31,4 @@ httpServer.createHttpsListener({
});

// start
httpServer.jetty.start();
httpServer.jetty.start();
10 changes: 5 additions & 5 deletions examples/httpserver-fluent.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
var httpServer = require("../lib/main");
var builder = httpServer.build()
const httpServer = require("ringo/httpserver");
const builder = httpServer.build()
// enable sessions with a custom node name
.enableSessions({
"name": "test1"
})
// serve application
.serveApplication("/", module.resolve("./app"), {
.serveApplication("/", module.resolve("./httpserver-app"), {
"sessions": true
})
// add websocket - this must be called after serveApplication
// as it operates on the current context of the builder
.addWebSocket("/websocket", function() {})
.addWebSocket("/websocket", () => {})
// static file serving
.serveStatic("/static", module.resolve("./"), {
"allowDirectoryListing": true
Expand All @@ -27,4 +27,4 @@ var builder = httpServer.build()
"keyManagerPassword": "secret"
})
// start up the server
.start();
.start();
8 changes: 4 additions & 4 deletions examples/httpserver-jettyxml.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var httpServer = require("../lib/main");
var builder = httpServer.build("config/jetty.xml")
const httpServer = require("ringo/httpserver");
const builder = httpServer.build("config/jetty.xml")
// serve application
.serveApplication("/", module.resolve("./app"))
.serveApplication("/", module.resolve("./httpserver-app"))
// static file serving
.serveStatic("/static", module.resolve("./"), {
"allowDirectoryListing": true
})
// start up the server
.start();
.start();
2 changes: 1 addition & 1 deletion examples/httpserver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Minimal hello-world HTTP server demo

exports.app = function(req) {
exports.app = (req) => {
return {
status: 200,
headers: {"Content-Type": "text/plain"},
Expand Down
13 changes: 6 additions & 7 deletions examples/parse-options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
var {Parser} = require('ringo/args');
var {RED, BLUE, YELLOW, BOLD, writeln} = require('ringo/term');
var system = require('system');
const {Parser} = require('ringo/args');
const {RED, BLUE, YELLOW, BOLD, writeln} = require('ringo/term');
const system = require('system');

function main(args) {
var parser = new Parser();
const main = (args) => {
const parser = new Parser();
parser.addOption('f', 'foo', null, 'Enable foo bit');
parser.addOption('b', 'bar', '[BAR-FACTOR]', 'Specify bar factor');
parser.addOption('h', 'help', null, 'Display help');
args.shift();
var options = parser.parse(args);
const options = parser.parse(args);
if (options.help) {
writeln(BLUE, BOLD, 'Options:');
writeln(BLUE, parser.help());
Expand All @@ -23,7 +23,6 @@ function main(args) {
if (!Object.keys(options).length) {
writeln(BOLD, "Run with -h/--help to see available options");
}

}

if (require.main === module) {
Expand Down
14 changes: 7 additions & 7 deletions examples/promise-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
* in the chain as soon as it is resolved.
*/

var {Deferred} = require('ringo/promise');
const {Deferred} = require('ringo/promise');

var deferred = new Deferred();
const deferred = new Deferred();

deferred.promise.then(function(val) {
deferred.promise.then(val => {
print('Step 1:', val);
return val.toUpperCase();
}).then(function(val) {
}).then(val => {
print('Step 2:', val);
var d = new Deferred();
const d = new Deferred();
d.resolve(val.split(' ').join(' CRUEL '));
return d.promise;
}).then(function(val) {
}).then(val => {
print('Step 3:', val);
}, function(err) {
}, err => {
print('Failed:', err);
});

Expand Down
12 changes: 6 additions & 6 deletions examples/promise-fail.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
* This example demonstrates chaining of error handlers with promises.
* An error thrown by the first promise handler is passed on to the
* An error thrown by the first promise handler is passed on to the
* error handler at the end of the chain.
*/

var {Deferred} = require('ringo/promise');
const {Deferred} = require('ringo/promise');

var deferred = new Deferred();
const deferred = new Deferred();

deferred.promise.then(function(val) {
deferred.promise.then(val => {
print('Step 1:', val);
throw 'Error';
}).then(function(val) {
}).then(val => {
print('Step 2', val);
return val.toUpperCase();
}).then(function(val) {
}).then(val => {
print('Step 3:', val);
}, function(err) {
print('Failed:', err);
Expand Down
15 changes: 8 additions & 7 deletions examples/swing.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var {JFrame, JButton, ImageIcon, JLabel} = javax.swing;
var system = require('system');
const {JFrame, JButton, ImageIcon, JLabel} = javax.swing;
const system = require('system');

var n = 0;
const n = 0;

function main() {
var frame = new JFrame("Swing Demo");
var button = new JButton(new ImageIcon(module.resolve("img/ringo-drums.png")));
const frame = new JFrame("Swing Demo");
const button = new JButton(new ImageIcon(module.resolve("img/ringo-drums.png")));
button.addActionListener(function(e) {
setInterval(function() {
if (n++ > 200) system.exit();
Expand All @@ -19,8 +19,9 @@ function main() {
frame.setVisible(true);
}

function random() (Math.random() - 0.5) * 50;

function random() {
return (Math.random() - 0.5) * 50;
}

if (require.main == module) {
main();
Expand Down
43 changes: 26 additions & 17 deletions examples/websocket-server-push.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
// Simple websocket server demo
var response = require("ringo/jsgi/response");
var arrays = require("ringo/utils/arrays");
const response = require("ringo/jsgi/response");
const arrays = require("ringo/utils/arrays");
const httpServer = require("ringo/httpserver");

var connections = [];
const connections = [];

// Schedule an interval function that periodically broadcasts the number of open connections
setInterval(function() {
connections.forEach(function(conn) {
setInterval(() => {
connections.forEach((conn) => {
conn.send((connections.length - 1) + " other connection(s) open");
});
}, 5000)

exports.app = function(req) {
const app = (req) => {
return response.static(module.resolve("html/websocket.html"), "text/html");
};

function onconnect(conn) {
const onConnect = (conn) => {
connections.push(conn);
console.info("Opening connection, " + connections.length + " open");
conn.addListener("text", function(message) {
connections.forEach(function(conn) {
conn.send(message);
});
conn.addListener("text", message => {
connections.forEach(conn => conn.send(message));
console.info("Sending message");
});
conn.addListener("close", function() {
conn.addListener("close", () => {
arrays.remove(connections, conn);
console.info("Closing connection, " + connections.length + " remaining");
})
}
});
};

if (require.main == module) {
var server = require("ringo/httpserver").main(module.id);
server.getDefaultContext().addWebSocket("/websocket", onconnect);
}
httpServer.build()
// enable sessions with a custom node name
// serve application
.serveApplication("/", app)
// add websocket - this must be called after serveApplication
// as it operates on the current context of the builder
.addWebSocket("/websocket", onConnect)
.http({
"port": 8080
})
// start up the server
.start();
}
Loading

0 comments on commit b085a7a

Please sign in to comment.