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 = `