Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules modernization #412

Merged
merged 52 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
2254fd0
var => const/let + other modernizations
Oct 29, 2020
fa23f47
replaced var with const in examples
Oct 29, 2020
088e53b
code modernization
Oct 29, 2020
caff111
code modernization, removed obsolete require of Binary, ByteArray and…
Oct 29, 2020
88eaa88
code modernization, added tests for net module
Oct 29, 2020
efbc42b
code modernization
Oct 29, 2020
9213031
code modernization
Oct 29, 2020
3fdd099
code modernization, added test for help() method
Oct 31, 2020
26207a8
code modernization
Oct 31, 2020
b0bac7e
code modernization
Oct 31, 2020
dca4046
use require instead of include in code example to make things explicit
Nov 2, 2020
cc17fbd
avoid redeclaration of consts
Nov 2, 2020
5ca27db
modernized/simplified code, added test for Semaphore
Nov 2, 2020
3fc56f2
code modernization
Nov 2, 2020
f7dabd3
code modernization (ie. var -> const/let)
Nov 2, 2020
ae0def5
code modernization (ie. var -> const/let)
Nov 2, 2020
8a54199
code modernization (ie. var -> const/let)
Nov 2, 2020
52eb27a
code modernization (ie. var -> const/let)
Nov 2, 2020
da34dbd
code modernization
Nov 2, 2020
8f4f2c3
code modernization (ie. var -> const/let)
Nov 2, 2020
27640b5
bugfix: arrow functions have no arguments object
Nov 2, 2020
6399f07
code modernization
Nov 2, 2020
3630d6f
code modernization (ie. var -> const/let)
Nov 2, 2020
305f511
code modernization (ie. var -> const/let)
Nov 2, 2020
24ff785
code modernization (ie. var -> const/let)
Nov 2, 2020
f9d3aa2
replaced include() in code example with more explicit require
Nov 2, 2020
9800eb2
code modernization (ie. var -> const/let), ZipIterator is now a Gener…
Nov 2, 2020
9179851
added missing tests
Nov 2, 2020
a278580
code modernization (ie. var -> const/let)
Nov 2, 2020
09184bc
code modernization (ie. var -> const/let)
Nov 2, 2020
1f47930
code modernization (ie. var -> const/let)
Nov 2, 2020
1e92bbe
code modernization (ie. var -> const/let)
Nov 2, 2020
0362ea2
code modernization (ie. var -> const/let)
Nov 2, 2020
1268b7a
use strict comparisons and 0o1234 for octals, minor code formatting
Nov 2, 2020
87f41a4
code modernization (ie. var -> const/let), fixed an error in parseFil…
Nov 2, 2020
c8fde04
code modernization (ie. var -> const/let)
Nov 2, 2020
07046a8
code modernization (ie. var -> const/let)
Nov 2, 2020
fcbb093
code modernization (ie. var -> const/let)
Nov 2, 2020
d406b5f
stack traces now exclude the test module too, code modernization
Nov 3, 2020
7d999ba
code modernization
Nov 3, 2020
81ed270
#409 implemented binary.toByteString() as replacement of the
Nov 3, 2020
d5547a4
minor: simplified named exports
Nov 3, 2020
d43a229
regression fix: short option can be null/undefined
Nov 3, 2020
bc00d84
modified prototype construction of AssertionError and
Nov 3, 2020
de3e2e4
regression fix: printResult and printError must be declared as local …
Nov 4, 2020
556bcc7
regression fix: objects.merge() must handle null/undefined arguments
Nov 4, 2020
73a093a
fixed variable declaration
Nov 4, 2020
7b34b34
added worker test
Nov 4, 2020
2da80dc
fixed write/writeln method binding
Nov 4, 2020
18bb889
reverted change to arrow functions to maintain correct scope
Nov 4, 2020
b8f35ce
fixed & modernized examples
Nov 4, 2020
2965bf1
#409 binary module no longer modifies the String prototype
Nov 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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