Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
In practice this was confusing and not common.
* `websocket` option is deprecated, but still respected. Please use the new `transports` option.
* Node.js `>= 18.0.0` is required.
* Project now uses ESM and is type "module".

## Other Fixes/Changes
* Convert from coffeescript to ES6.
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Work in progress:
* [SockJS-go](https://github.com/igm/sockjs-go/)
* [actix/sockjs](https://github.com/actix/sockjs) for Rust

⚠️️ **ATTENTION** This is pre-release documentation. The documentation for the
⚠️️ **ATTENTION** This is pre-release documentation. The documentation for the
latest stable release is at: https://github.com/sockjs/sockjs-node/tree/v0.3.19 ️⚠️

# What is SockJS?
Expand All @@ -54,8 +54,8 @@ To install `sockjs-node` run:
A simplified echo SockJS server could look more or less like:

```javascript
const http = require('http');
const sockjs = require('sockjs');
import http from 'node:http';
import sockjs from 'sockjs';

const echo = sockjs.createServer({ prefix:'/echo' });
echo.on('connection', function(conn) {
Expand Down Expand Up @@ -133,7 +133,7 @@ Where `options` is a hash which can contain:

<dt>transports (Array of strings)</dt>
<dd>List of transports to enable. Select from `eventsource`, `htmlfile`,
`jsonp-polling`, `websocket`, `websocket-raw`, `xhr-polling`,
`jsonp-polling`, `websocket`, `websocket-raw`, `xhr-polling`,
and `xhr-streaming`.</dd>

<dt>jsessionid (boolean or function)</dt>
Expand Down Expand Up @@ -169,7 +169,7 @@ and `xhr-streaming`.</dd>
<dd>Enabling this option will prevent
<a href="https://en.wikipedia.org/wiki/Cross-origin_resource_sharing">CORS</a>
headers from being included in the HTTP response. Can be used when the
sockjs client is known to be connecting from the same origin as the
sockjs client is known to be connecting from the same origin as the
sockjs server. This also disables the iframe HTML endpoint.</dd>
</dl>

Expand Down
3 changes: 2 additions & 1 deletion examples/echo/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "sockjs-echo",
"version": "0.0.1",
"type": "module",
"dependencies": {
"node-static": "^0.7.11",
"sockjs": "^0.4.0"
"sockjs": "../../"
},
"private": true
}
10 changes: 6 additions & 4 deletions examples/echo/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
import http from 'node:http';
import path from 'node:path';
import url from 'node:url';
import sockjs from 'sockjs';
import node_static from 'node-static';

const http = require('http');
const sockjs = require('sockjs');
const node_static = require('node-static');
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
Comment on lines +1 to +7
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might also need attention

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment has been addressed in the latest push. This file now imports from node:path.


// 1. Echo sockjs server
const sockjs_opts = {
Expand Down
3 changes: 2 additions & 1 deletion examples/express/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "sockjs-express",
"version": "0.0.1",
"type": "module",
"dependencies": {
"express": "^4.16.3",
"sockjs": "^0.4.0"
"sockjs": "../.."
},
"private": true
}
12 changes: 7 additions & 5 deletions examples/express/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';
import path from 'node:path';
import http from 'node:http';
import url from 'node:url';
import express from 'express';
import sockjs from 'sockjs';

const http = require('http');
const express = require('express');
const sockjs = require('sockjs');
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

const sockjs_opts = {
prefix: '/echo'
Expand All @@ -14,7 +16,7 @@ sockjs_echo.on('connection', (conn) => {
});

const app = express();
app.get('/', (req, res) => res.sendFile(__dirname + '/index.html'));
app.get('/', (req, res) => res.sendFile(path.join(__dirname, 'index.html')));

const server = http.createServer(app);
sockjs_echo.attach(server);
Expand Down
3 changes: 2 additions & 1 deletion examples/hapi/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "sockjs-hapi",
"version": "0.0.1",
"type": "module",
"dependencies": {
"hapi": "^17.6.0",
"inert": "^5.1.0",
"sockjs": "^0.4.0"
"sockjs": "../.."
},
"private": true
}
8 changes: 3 additions & 5 deletions examples/hapi/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const sockjs = require('sockjs');
const Hapi = require('hapi');
const Inert = require('inert');
import sockjs from 'sockjs';
import Hapi from 'hapi';
import Inert from 'inert';

// 1. Echo sockjs server
const sockjs_opts = {
Expand Down
3 changes: 2 additions & 1 deletion examples/koa/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "sockjs-koa",
"version": "0.0.1",
"type": "module",
"dependencies": {
"koa": "^2.5.3",
"sockjs": "^0.4.0"
"sockjs": "../.."
},
"private": true
}
15 changes: 8 additions & 7 deletions examples/koa/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
import Koa from 'koa';
import sockjs from 'sockjs';
import http from 'node:http';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

const Koa = require('koa');
const sockjs = require('sockjs');
const http = require('http');
const fs = require('fs');
const path = require('path');
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

// 1. Echo sockjs server
const sockjs_opts = {
Expand All @@ -22,7 +23,7 @@ const app = new Koa();

app.use(function (ctx, next) {
return next().then(() => {
const filePath = __dirname + '/index.html';
const filePath = path.join(__dirname, 'index.html');
ctx.response.type = path.extname(filePath);
ctx.response.body = fs.createReadStream(filePath);
});
Expand Down
3 changes: 2 additions & 1 deletion examples/multiplex/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "sockjs-multiplex",
"version": "0.0.1",
"type": "module",
"dependencies": {
"express": "^4.16.3",
"sockjs": "^0.4.0",
"sockjs": "../..",
"websocket-multiplex": "^0.1.0"
},
"private": true
Expand Down
14 changes: 8 additions & 6 deletions examples/multiplex/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';
import path from 'node:path';
import http from 'node:http';
import url from 'node:url';
import express from 'express';
import sockjs from 'sockjs';
import websocket_multiplex from 'websocket-multiplex';

const http = require('http');
const express = require('express');
const sockjs = require('sockjs');
const websocket_multiplex = require('websocket-multiplex');
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

// 1. Setup SockJS server
const sockjs_opts = {
Expand Down Expand Up @@ -40,7 +42,7 @@ carl.on('connection', function (conn) {
// 3. Express server
const app = express();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
res.sendFile(path.join(__dirname, 'index.html'));
});

const server = http.createServer(app);
Expand Down
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'use strict';
import Server from './lib/server.js';

const Server = require('./lib/server');

module.exports.createServer = function createServer(options) {
function createServer(options) {
return new Server(options);
};
}

module.exports.listen = function listen(http_server, options) {
const srv = exports.createServer(options);
function listen(http_server, options) {
const srv = createServer(options);
if (http_server) {
srv.attach(http_server);
}
return srv;
};
}

export default { createServer, listen };
export { createServer, listen };
4 changes: 1 addition & 3 deletions lib/handlers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = {
export default {
welcome_screen(req, res) {
res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
res.writeHead(200);
Expand Down
8 changes: 3 additions & 5 deletions lib/iframe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const utils = require('./utils');
const middleware = require('./middleware');
import * as utils from './utils.js';
import middleware from './middleware.js';

const iframe_template = `<!DOCTYPE html>
<html>
Expand All @@ -20,7 +18,7 @@ const iframe_template = `<!DOCTYPE html>
</body>
</html>`;

module.exports = {
export default {
iframe(req, res, _head, next) {
const context = {
'{{ sockjs_url }}': this.options.sockjs_url
Expand Down
8 changes: 3 additions & 5 deletions lib/info.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
'use strict';
import * as utils from './utils.js';
import middleware from './middleware.js';

const utils = require('./utils');
const middleware = require('./middleware');

module.exports = {
export default {
info(req, res) {
const info = {
// deprecated option, but useful for old clients
Expand Down
18 changes: 9 additions & 9 deletions lib/listener.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';
import createDebug from 'debug';
import transportList from './transport/list.js';
import middleware from './middleware.js';
import handlers from './handlers.js';
import info from './info.js';
import iframe from './iframe.js';

const debug = require('debug')('sockjs:listener');
const transportList = require('./transport/list');
const middleware = require('./middleware');
const handlers = require('./handlers');
const info = require('./info');
const iframe = require('./iframe');
const debug = createDebug('sockjs:listener');

module.exports.generateDispatcher = function generateDispatcher(options) {
export function generateDispatcher(options) {
const p = (s) => new RegExp(`^${options.prefix}${s}[/]?$`);
const t = (s) => [p(`/([^/.]+)/([^/.]+)${s}`), 'server', 'session'];
const prefix_dispatcher = [
Expand Down Expand Up @@ -38,4 +38,4 @@ module.exports.generateDispatcher = function generateDispatcher(options) {
}
}
return prefix_dispatcher.concat(transport_dispatcher);
};
}
14 changes: 7 additions & 7 deletions lib/middleware.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
import FayeWebsocket from 'faye-websocket';
import * as utils from './utils.js';
import querystring from 'querystring';

const FayeWebsocket = require('faye-websocket');
const utils = require('./utils');
const querystring = require('querystring');

module.exports = {
const middleware = {
h_no_cache(req, res, _head, next) {
res.setHeader('Cache-Control', 'no-store, no-cache, no-transform, must-revalidate, max-age=0');
next();
Expand Down Expand Up @@ -101,7 +99,7 @@ module.exports = {

xhr_options(req, res, _head, next) {
res.statusCode = 204; // No content
module.exports.cache_for(res);
middleware.cache_for(res);
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, POST');
res.setHeader('Access-Control-Max-Age', res.cache_for);
res.end();
Expand Down Expand Up @@ -129,3 +127,5 @@ module.exports = {
next();
}
};

export default middleware;
21 changes: 13 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict';
import events from 'node:events';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';
import createDebug from 'debug';
import * as listener from './listener.js';
import * as webjs from './webjs.js';

const events = require('events');
const url = require('url');
const debug = require('debug')('sockjs:server');
const listener = require('./listener');
const webjs = require('./webjs');
const pkg = require('../package.json');
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const pkgPath = path.join(__dirname, '../package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath));

const debug = createDebug('sockjs:server');

class Server extends events.EventEmitter {
constructor(user_options) {
Expand Down Expand Up @@ -91,4 +96,4 @@ class Server extends events.EventEmitter {
}
}

module.exports = Server;
export default Server;
10 changes: 5 additions & 5 deletions lib/session.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
import createDebug from 'debug';
import Transport from './transport/transport.js';
import SockJSConnection from './sockjs-connection.js';

const debug = require('debug')('sockjs:session');
const Transport = require('./transport/transport');
const SockJSConnection = require('./sockjs-connection');
const debug = createDebug('sockjs:session');

const MAP = new Map();
function closeFrame(status, reason) {
Expand Down Expand Up @@ -268,4 +268,4 @@ class Session {
}
}

module.exports = Session;
export default Session;
Loading
Loading