diff --git a/CHANGELOG.md b/CHANGELOG.md index 95906280..d72507b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index fbd67b04..ddfc3fd2 100644 --- a/README.md +++ b/README.md @@ -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? @@ -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) { @@ -133,7 +133,7 @@ Where `options` is a hash which can contain:
transports (Array of strings)
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`.
jsessionid (boolean or function)
@@ -169,7 +169,7 @@ and `xhr-streaming`.
Enabling this option will prevent CORS 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.
diff --git a/examples/echo/package.json b/examples/echo/package.json index 1416b2de..e45eecf4 100644 --- a/examples/echo/package.json +++ b/examples/echo/package.json @@ -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 } diff --git a/examples/echo/server.js b/examples/echo/server.js index 17801610..192a615b 100644 --- a/examples/echo/server.js +++ b/examples/echo/server.js @@ -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)); // 1. Echo sockjs server const sockjs_opts = { diff --git a/examples/express/package.json b/examples/express/package.json index 1fd620ee..1e6538cd 100644 --- a/examples/express/package.json +++ b/examples/express/package.json @@ -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 } diff --git a/examples/express/server.js b/examples/express/server.js index efe2ed6f..cf669bf1 100644 --- a/examples/express/server.js +++ b/examples/express/server.js @@ -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' @@ -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); diff --git a/examples/hapi/package.json b/examples/hapi/package.json index 916507ef..ad4fb745 100644 --- a/examples/hapi/package.json +++ b/examples/hapi/package.json @@ -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 } diff --git a/examples/hapi/server.js b/examples/hapi/server.js index 0e2d784f..aea4199d 100644 --- a/examples/hapi/server.js +++ b/examples/hapi/server.js @@ -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 = { diff --git a/examples/koa/package.json b/examples/koa/package.json index c8781081..6aff6ca6 100644 --- a/examples/koa/package.json +++ b/examples/koa/package.json @@ -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 } diff --git a/examples/koa/server.js b/examples/koa/server.js index b1ba039f..edc35575 100644 --- a/examples/koa/server.js +++ b/examples/koa/server.js @@ -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 = { @@ -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); }); diff --git a/examples/multiplex/package.json b/examples/multiplex/package.json index d7d0e6e9..da0dea0a 100644 --- a/examples/multiplex/package.json +++ b/examples/multiplex/package.json @@ -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 diff --git a/examples/multiplex/server.js b/examples/multiplex/server.js index 9a9f8bff..806cc29e 100644 --- a/examples/multiplex/server.js +++ b/examples/multiplex/server.js @@ -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 = { @@ -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); diff --git a/index.js b/index.js index 60f108f9..73447349 100644 --- a/index.js +++ b/index.js @@ -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 }; diff --git a/lib/handlers.js b/lib/handlers.js index d0cbf2b9..a70ee7f4 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -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); diff --git a/lib/iframe.js b/lib/iframe.js index 12d38a3a..f6cdb206 100644 --- a/lib/iframe.js +++ b/lib/iframe.js @@ -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 = ` @@ -20,7 +18,7 @@ const iframe_template = ` `; -module.exports = { +export default { iframe(req, res, _head, next) { const context = { '{{ sockjs_url }}': this.options.sockjs_url diff --git a/lib/info.js b/lib/info.js index 1311ae9e..45795f49 100644 --- a/lib/info.js +++ b/lib/info.js @@ -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 diff --git a/lib/listener.js b/lib/listener.js index 99596cf3..565e075c 100644 --- a/lib/listener.js +++ b/lib/listener.js @@ -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 = [ @@ -38,4 +38,4 @@ module.exports.generateDispatcher = function generateDispatcher(options) { } } return prefix_dispatcher.concat(transport_dispatcher); -}; +} diff --git a/lib/middleware.js b/lib/middleware.js index 62b3ae4e..8dc49ba7 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -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(); @@ -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(); @@ -129,3 +127,5 @@ module.exports = { next(); } }; + +export default middleware; diff --git a/lib/server.js b/lib/server.js index b7b177b2..7ab6174e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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) { @@ -91,4 +96,4 @@ class Server extends events.EventEmitter { } } -module.exports = Server; +export default Server; diff --git a/lib/session.js b/lib/session.js index 00c7da06..b2baa7ba 100644 --- a/lib/session.js +++ b/lib/session.js @@ -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) { @@ -268,4 +268,4 @@ class Session { } } -module.exports = Session; +export default Session; diff --git a/lib/sockjs-connection.js b/lib/sockjs-connection.js index c78b0e0b..541b5132 100644 --- a/lib/sockjs-connection.js +++ b/lib/sockjs-connection.js @@ -1,8 +1,8 @@ -'use strict'; +import createDebug from 'debug'; +import crypto from 'node:crypto'; +import stream from 'node:stream'; -const crypto = require('crypto'); -const debug = require('debug')('sockjs:connection'); -const stream = require('stream'); +const debug = createDebug('sockjs:connection'); class SockJSConnection extends stream.Duplex { constructor(session) { @@ -43,4 +43,4 @@ class SockJSConnection extends stream.Duplex { } } -module.exports = SockJSConnection; +export default SockJSConnection; diff --git a/lib/transport/base-receiver.js b/lib/transport/base-receiver.js index 218a9548..223320e2 100644 --- a/lib/transport/base-receiver.js +++ b/lib/transport/base-receiver.js @@ -1,7 +1,7 @@ -'use strict'; +import * as utils from '../utils.js'; +import createDebug from 'debug'; -const utils = require('../utils'); -const debug = require('debug')('sockjs:base-receiver'); +const debug = createDebug('sockjs:base-receiver'); class BaseReceiver { constructor(socket) { @@ -45,4 +45,4 @@ class BaseReceiver { } } -module.exports = BaseReceiver; +export default BaseReceiver; diff --git a/lib/transport/eventsource.js b/lib/transport/eventsource.js index 38d22055..f8024bed 100644 --- a/lib/transport/eventsource.js +++ b/lib/transport/eventsource.js @@ -1,8 +1,7 @@ -'use strict'; -const utils = require('../utils'); -const ResponseReceiver = require('./response-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); +import * as utils from '../utils.js'; +import ResponseReceiver from './response-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; class EventSourceReceiver extends ResponseReceiver { constructor(req, res, options) { @@ -41,7 +40,7 @@ function eventsource(req, res, _head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'GET', diff --git a/lib/transport/htmlfile.js b/lib/transport/htmlfile.js index 5b5510bc..26c4067c 100644 --- a/lib/transport/htmlfile.js +++ b/lib/transport/htmlfile.js @@ -1,8 +1,6 @@ -'use strict'; - -const ResponseReceiver = require('./response-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); +import ResponseReceiver from './response-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; // Browsers fail with "Uncaught exception: ReferenceError: Security // error: attempted to read protected variable: _jp". Set @@ -60,7 +58,7 @@ function htmlfile(req, res, _head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'GET', diff --git a/lib/transport/jsonp-polling.js b/lib/transport/jsonp-polling.js index 70e3d41f..809742c6 100644 --- a/lib/transport/jsonp-polling.js +++ b/lib/transport/jsonp-polling.js @@ -1,8 +1,6 @@ -'use strict'; - -const ResponseReceiver = require('./response-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); +import ResponseReceiver from './response-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; class JsonpReceiver extends ResponseReceiver { constructor(req, res, options, callback) { @@ -98,7 +96,7 @@ function jsonp_send(req, res, _head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'GET', diff --git a/lib/transport/list.js b/lib/transport/list.js index 870d6644..e3e39da5 100644 --- a/lib/transport/list.js +++ b/lib/transport/list.js @@ -1,11 +1,17 @@ -'use strict'; +import eventsource from './eventsource.js'; +import htmlfile from './htmlfile.js'; +import jsonp_polling from './jsonp-polling.js'; +import websocket from './websocket.js'; +import websocket_raw from './websocket-raw.js'; +import xhr_polling from './xhr-polling.js'; +import xhr_streaming from './xhr-streaming.js'; -module.exports = { - eventsource: require('./eventsource'), - htmlfile: require('./htmlfile'), - 'jsonp-polling': require('./jsonp-polling'), - websocket: require('./websocket'), - 'websocket-raw': require('./websocket-raw'), - 'xhr-polling': require('./xhr-polling'), - 'xhr-streaming': require('./xhr-streaming') +export default { + eventsource, + htmlfile, + 'jsonp-polling': jsonp_polling, + websocket, + 'websocket-raw': websocket_raw, + 'xhr-polling': xhr_polling, + 'xhr-streaming': xhr_streaming }; diff --git a/lib/transport/response-receiver.js b/lib/transport/response-receiver.js index 2b487b60..4e23e3b1 100644 --- a/lib/transport/response-receiver.js +++ b/lib/transport/response-receiver.js @@ -1,7 +1,7 @@ -'use strict'; +import BaseReceiver from './base-receiver.js'; +import createDebug from 'debug'; -const BaseReceiver = require('./base-receiver'); -const debug = require('debug')('sockjs:response-receiver'); +const debug = createDebug('sockjs:response-receiver'); // Write stuff to response, using chunked encoding if possible. class ResponseReceiver extends BaseReceiver { @@ -48,4 +48,4 @@ class ResponseReceiver extends BaseReceiver { } } -module.exports = ResponseReceiver; +export default ResponseReceiver; diff --git a/lib/transport/transport.js b/lib/transport/transport.js index e570deb9..3abe5b06 100644 --- a/lib/transport/transport.js +++ b/lib/transport/transport.js @@ -1,5 +1,3 @@ -'use strict'; - class Transport {} Transport.CONNECTING = 0; @@ -7,4 +5,4 @@ Transport.OPEN = 1; Transport.CLOSING = 2; Transport.CLOSED = 3; -module.exports = Transport; +export default Transport; diff --git a/lib/transport/websocket-raw.js b/lib/transport/websocket-raw.js index 4a76bc38..00ef45de 100644 --- a/lib/transport/websocket-raw.js +++ b/lib/transport/websocket-raw.js @@ -1,10 +1,8 @@ -'use strict'; - -const FayeWebsocket = require('faye-websocket'); -const Session = require('../session'); -const Transport = require('./transport'); -const SockJSConnection = require('../sockjs-connection'); -const middleware = require('../middleware'); +import FayeWebsocket from 'faye-websocket'; +import Session from '../session.js'; +import Transport from './transport.js'; +import SockJSConnection from '../sockjs-connection.js'; +import middleware from '../middleware.js'; class RawWebsocketSessionReceiver { constructor(req, conn, server, ws) { @@ -85,7 +83,7 @@ function raw_websocket(req, socket, head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'GET', diff --git a/lib/transport/websocket.js b/lib/transport/websocket.js index aa60e8e9..004440d0 100644 --- a/lib/transport/websocket.js +++ b/lib/transport/websocket.js @@ -1,10 +1,10 @@ -'use strict'; +import createDebug from 'debug'; +import FayeWebsocket from 'faye-websocket'; +import BaseReceiver from './base-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; -const debug = require('debug')('sockjs:trans:websocket'); -const FayeWebsocket = require('faye-websocket'); -const BaseReceiver = require('./base-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); +const debug = createDebug('sockjs:trans:websocket'); class WebSocketReceiver extends BaseReceiver { constructor(ws, socket) { @@ -97,7 +97,7 @@ function sockjs_websocket(req, socket, head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'GET', diff --git a/lib/transport/xhr-polling.js b/lib/transport/xhr-polling.js index 8e2ae032..8134b00b 100644 --- a/lib/transport/xhr-polling.js +++ b/lib/transport/xhr-polling.js @@ -1,9 +1,7 @@ -'use strict'; - -const ResponseReceiver = require('./response-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); -const xhr = require('./xhr'); +import ResponseReceiver from './response-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; +import xhr from './xhr.js'; class XhrPollingReceiver extends ResponseReceiver { constructor(req, res, options) { @@ -25,7 +23,7 @@ function xhr_poll(req, res, _head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'POST', diff --git a/lib/transport/xhr-streaming.js b/lib/transport/xhr-streaming.js index c5f4bd07..ca82cb5f 100644 --- a/lib/transport/xhr-streaming.js +++ b/lib/transport/xhr-streaming.js @@ -1,9 +1,7 @@ -'use strict'; - -const ResponseReceiver = require('./response-receiver'); -const Session = require('../session'); -const middleware = require('../middleware'); -const xhr = require('./xhr'); +import ResponseReceiver from './response-receiver.js'; +import Session from '../session.js'; +import middleware from '../middleware.js'; +import xhr from './xhr.js'; class XhrStreamingReceiver extends ResponseReceiver { constructor(req, res, options) { @@ -28,7 +26,7 @@ function xhr_streaming(req, res, _head, next) { next(); } -module.exports = { +export default { routes: [ { method: 'POST', diff --git a/lib/transport/xhr.js b/lib/transport/xhr.js index 3e9d8361..a712ca6e 100644 --- a/lib/transport/xhr.js +++ b/lib/transport/xhr.js @@ -1,8 +1,6 @@ -'use strict'; +import Session from '../session.js'; -const Session = require('../session'); - -module.exports = { +export default { xhr_send(req, res, _head, next) { if (!req.body) { return next({ diff --git a/lib/utils.js b/lib/utils.js index 27a98d4f..0a2664a9 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,10 +1,9 @@ -'use strict'; -const crypto = require('crypto'); -const http = require('http'); +import crypto from 'node:crypto'; +import http from 'node:http'; // used in case of 'upgrade' requests where res is // net.Socket instead of http.ServerResponse -module.exports.fake_response = function fake_response(req, res) { +export function fake_response(req, res) { // This is quite simplistic, don't expect much. const headers = { Connection: 'close' }; res.writeHead = function (status, user_headers = {}) { @@ -23,9 +22,9 @@ module.exports.fake_response = function fake_response(req, res) { } }; res.setHeader = (k, v) => (headers[k] = v); -}; +} -module.exports.escape_selected = function escape_selected(str, chars) { +export function escape_selected(str, chars) { const map = {}; chars = `%${chars}`; Array.prototype.forEach.call(chars, (c) => (map[c] = escape(c))); @@ -37,11 +36,11 @@ module.exports.escape_selected = function escape_selected(str, chars) { } }); return parts.join(''); -}; +} -module.exports.md5_hex = function md5_hex(data) { +export function md5_hex(data) { return crypto.createHash('md5').update(data).digest('hex'); -}; +} // eslint-disable-next-line no-control-regex const escapable = /[\x00-\x1f\ud800-\udfff\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufff0-\uffff]/g; @@ -58,7 +57,7 @@ function unroll_lookup(escapable) { const lookup = unroll_lookup(escapable); -module.exports.quote = function quote(string) { +export function quote(string) { const quoted = JSON.stringify(string); // In most cases normal json encoding fast and enough @@ -68,9 +67,9 @@ module.exports.quote = function quote(string) { } return quoted.replace(escapable, (a) => lookup[a]); -}; +} -module.exports.parseCookie = function parseCookie(cookie_header) { +export function parseCookie(cookie_header) { const cookies = {}; if (cookie_header) { cookie_header.split(';').forEach((cookie) => { @@ -79,13 +78,13 @@ module.exports.parseCookie = function parseCookie(cookie_header) { }); } return cookies; -}; +} -module.exports.random32 = function random32() { +export function random32() { return crypto.randomBytes(4).readUInt32LE(0); -}; +} -module.exports.getBody = function getBody(req, cb) { +export function getBody(req, cb) { let body = []; req.on('data', (d) => { body.push(d); @@ -97,4 +96,4 @@ module.exports.getBody = function getBody(req, cb) { req.once('close', () => { body = null; }); -}; +} diff --git a/lib/webjs.js b/lib/webjs.js index fc762900..1ac6d489 100644 --- a/lib/webjs.js +++ b/lib/webjs.js @@ -1,10 +1,10 @@ -'use strict'; +import createDebug from 'debug'; +import url from 'node:url'; +import * as utils from './utils.js'; +import handlers from './handlers.js'; +import middleware from './middleware.js'; -const debug = require('debug')('sockjs:webjs'); -const url = require('url'); -const utils = require('./utils'); -const handlers = require('./handlers'); -const middleware = require('./middleware'); +const debug = createDebug('sockjs:webjs'); function execute_async_request(server, funs, req, res, head) { function next(err) { @@ -27,7 +27,7 @@ function execute_async_request(server, funs, req, res, head) { next(); } -module.exports.generateHandler = function generateHandler(server, dispatcher) { +export function generateHandler(server, dispatcher) { return function (req, res, head) { if (res.writeHead === undefined) { utils.fake_response(req, res); @@ -72,4 +72,4 @@ module.exports.generateHandler = function generateHandler(server, dispatcher) { middleware.log_request.call(server, req, res, true, () => {}); } }; -}; +} diff --git a/package.json b/package.json index 5c4a16cc..72824bfe 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "email": "deadbeef@popcount.org" } ], + "type": "module", "dependencies": { "debug": "^4.3.1", "faye-websocket": "^0.11.3" diff --git a/tests/test_server/config.js b/tests/test_server/config.js index b1d72ec8..082ce86d 100644 --- a/tests/test_server/config.js +++ b/tests/test_server/config.js @@ -1,8 +1,8 @@ -'use strict'; +import createDebug from 'debug'; -const debug = require('debug')('sockjs:test-server:app'); +const debug = createDebug('sockjs:test-server:app'); -exports.config = { +export default { server_opts: { sockjs_url: 'http://localhost:8080/lib/sockjs.js', websocket: true, diff --git a/tests/test_server/server.js b/tests/test_server/server.js index 7751c1dd..dc812196 100644 --- a/tests/test_server/server.js +++ b/tests/test_server/server.js @@ -1,7 +1,6 @@ -'use strict'; -const http = require('http'); -const config = require('./config').config; -const sockjs_app = require('./sockjs_app'); +import http from 'node:http'; +import config from './config.js'; +import * as sockjs_app from './sockjs_app.js'; const server = http.createServer(); server.addListener('request', function (req, res) { diff --git a/tests/test_server/sockjs_app.js b/tests/test_server/sockjs_app.js index d166385d..f3792748 100644 --- a/tests/test_server/sockjs_app.js +++ b/tests/test_server/sockjs_app.js @@ -1,8 +1,9 @@ -'use strict'; -const sockjs = require('../../index'); -const debug = require('debug')('sockjs:test-server:app'); +import sockjs from '../../index.js'; +import createDebug from 'debug'; -exports.install = function (opts, server) { +const debug = createDebug('sockjs:test-server:app'); + +export function install(opts, server) { const echoHandler = function (conn) { debug(` [+] echo open ${conn}`); conn.on('close', function () { @@ -94,4 +95,4 @@ exports.install = function (opts, server) { }); }); sjs_amplify.attach(server); -}; +}