diff --git a/lib/.placeholder b/.eslintignore similarity index 100% rename from lib/.placeholder rename to .eslintignore diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..d95a3244 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,7 @@ +{ + "env": { + "es6": true, + "node": true + }, + "extends": ["eslint:recommended", "plugin:prettier/recommended"] +} diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000..54e01c7c --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +# These are supported funding model platforms + +tidelift: npm/sockjs diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 00000000..96f67b3f --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,32 @@ +name: Node CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [10.x, 12.x, 14.x] + + steps: + - uses: actions/checkout@v1 + - uses: actions/setup-python@v1 + with: + python-version: '2.x' + architecture: 'x64' + - name: Install virtualenv + run: | + pip install virtualenv + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: npm install, build, and test + run: | + npm ci + ./scripts/test.sh + env: + CI: true diff --git a/.gitignore b/.gitignore index 5a16f43b..e041a351 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .pidfile.pid node_modules -lib/*.js *~ +sockjs-protocol diff --git a/.npmignore b/.npmignore index 28b91266..67c426ef 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,7 @@ .gitignore -lib/.placeholder -VERSION-GEN -src +.eslintrc +.eslintignore +.nvmrc node_modules +examples *~ diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 00000000..e1fcd1ea --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +lts/erbium diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..11cd494a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "printWidth": 100, + "singleQuote": true, + "trailingComma": "none" +} diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 00000000..5d81e5dd --- /dev/null +++ b/AUTHORS @@ -0,0 +1,8 @@ +# This is the list of sockjs authors for copyright purposes. +# +# This does not necessarily list everyone who has contributed code, since in +# some cases, their employer may be the copyright holder. To see the full list +# of contributors, see the revision history in source control. +Bryce Kahle +Marek Majkowski +VMWare diff --git a/Changelog b/CHANGELOG.md similarity index 60% rename from Changelog rename to CHANGELOG.md index 309e4320..330054da 100644 --- a/Changelog +++ b/CHANGELOG.md @@ -1,3 +1,114 @@ +# Unreleased + +## **BREAKING CHANGES** + * `installHandlers(server, options)` renamed to `attach(server)` and only takes a single argument. + This means you cannot use the same SockJS server installed at multiple prefixes. + In practice this was confusing and not common. + * `websocket` option is deprecated, but still respected. Please use the new `transports` option. + * Node.js `>= 6.5.0` is required. + +## Other Fixes/Changes + * Convert from coffeescript to ES6. + * Update minimum Node.js version to 6.X. + * Update SockJSConnection implementation to be compatible with latest Node.js streams. + * SockJSConnection properties `readable` and `writable` have been removed. These are used internally by Node.js streams. + * Remove `console.log` logging by default. + * Remove usage of exceptions for flow control. + * Add `debug` logs for easier troubleshooting. + * Added `transports` option to allow selection of specific transports. + * Added `detach(server)` function to remove SockJS from a HTTP server instance. + * Update dependencies. + * Examples have been updated to use latest versions of libraries. + + +0.3.19 +====== + + * Update `node-uuid` version #224 + * Add `disable_cors` option to prevent CORS headers from being added to responses #218 + * Add `dnt` header to whitelist #212 + * Add `x-forwarded-host` and `x-forwarded-port` headers to whitelist #208 + * Update `sockjs_url` default to latest 1.x target #223 + * Updated hapi.js example #216 + +0.3.18 +====== + + * Change to using `res.statusCode` instead of manual parsing of `res._header` #213 + * Update sockjs-protocol filename in README #203 + +0.3.17 +====== + + * Fix usage of undefined `session` in `heartbeat_timeout` #179 + +0.3.16 +====== + + * Fix CORS response for null origin #177 + * Add websocket ping-pong and close if no response #129, #162, #169 + * Update sockjs-client version in examples #182 + * Add koa example #180 + * Disable raw websocket endpoint when websocket = false #183 + * Upgrade to faye-websocket 0.10.0 and use proper close code + * When connection is aborted, don't delay the teardown + * Forward additional headers #188 + * Add `no-transform` to Cache-Control headers #189 + * Update documentation about heartbeats #192 + + +0.3.15 +====== + + * Remove usage of naked '@' function params to be compatible with coffeescript 1.9.0 #175 + +0.3.14 +====== + + * Re-publish to npm because of build issue in 0.3.13 + +0.3.13 +====== + + * Upgrade faye-websocket to 0.9.3 to fix #171 + +0.3.12 +====== + + * Allow Faye socket constructor options to be passed with + faye_server_options option to createServer + * Fix websocket bad json tests + * Upgrade Faye to allow 0.9.* + +0.3.11 +====== + + * #133 - only delay disconnect on non-websocket transports + * Upgrade Faye to 0.8.0 + +0.3.10 +====== + + * #168 - Add CORS headers for eventsource + * #158 - schedule heartbeat timer even if send_buffer is not empty + * #96 - remove rbytes dependency + * #83 - update documentation for prefix + * #163 - add protection to JSON for SWF exploit + * #104 - delete unused parameters in code + * #106 - update CDN urls + * #79 - Don't remove stream listeners until after end so 'close' event is heard + * Get rid of need for _sockjs_onload global variable + * Use Faye for websocket request validation + * Upgrade Faye to 0.7.3 + * Upgrade node-uuid to 1.4.1 + +0.3.9 +===== + + * #130 - Set Vary: Origin on CORS requests + * Upgrade Faye to 0.7.2 from 0.7.0 + + 0.3.8 ===== diff --git a/COPYING b/COPYING deleted file mode 100644 index a71ebfdf..00000000 --- a/COPYING +++ /dev/null @@ -1,6 +0,0 @@ -Parts of the code are derived from various open source projects. - -For code derived from Socket.IO by Guillermo Rauch see -https://github.com/LearnBoost/socket.io/tree/0.6.17#readme. - -All other code is released on MIT license, see LICENSE-MIT-SockJS. diff --git a/LICENSE-MIT-SockJS b/LICENSE similarity index 93% rename from LICENSE-MIT-SockJS rename to LICENSE index a8971671..32cc4e64 100644 --- a/LICENSE-MIT-SockJS +++ b/LICENSE @@ -1,4 +1,6 @@ -Copyright (C) 2011 VMware, Inc. +The MIT License (MIT) + +Copyright (c) 2011-2019 The sockjs Authors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile deleted file mode 100644 index ea939b87..00000000 --- a/Makefile +++ /dev/null @@ -1,55 +0,0 @@ -.PHONY: all serve clean - -COFFEE:=./node_modules/.bin/coffee - -#### General - -all: build - -build: src/*coffee - @$(COFFEE) -v > /dev/null - $(COFFEE) -o lib/ -c src/*.coffee - -clean: - rm -f lib/*.js - - -#### Testing - -test_server: - node examples/test_server/server.js - -serve: - @if [ -e .pidfile.pid ]; then \ - kill `cat .pidfile.pid`; \ - rm .pidfile.pid; \ - fi - - @while [ 1 ]; do \ - make build; \ - echo " [*] Running http server"; \ - make test_server & \ - SRVPID=$$!; \ - echo $$SRVPID > .pidfile.pid; \ - echo " [*] Server pid: $$SRVPID"; \ - inotifywait -r -q -e modify .; \ - kill `cat .pidfile.pid`; \ - rm -f .pidfile.pid; \ - sleep 0.1; \ - done - -#### Release process -# 1) commit everything -# 2) amend version in package.json -# 3) run 'make tag' and run suggested 'git push' variants -# 4) run 'npm publish' - -RVER:=$(shell grep "version" package.json|tr '\t"' ' \t'|cut -f 4) -VER:=$(shell ./VERSION-GEN) - -.PHONY: tag -tag: all - git commit $(TAG_OPTS) package.json Changelog -m "Release $(RVER)" - git tag v$(RVER) -m "Release $(RVER)" - @echo ' [*] Now run' - @echo 'git push; git push --tag' diff --git a/README.md b/README.md index 7f373e24..62554ee2 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,20 @@ -SockJS family: +# SockJS-node + +[](https://www.npmjs.com/package/sockjs)[](https://david-dm.org/sockjs/sockjs-node) + +# SockJS for enterprise + +Available as part of the Tidelift Subscription. + +The maintainers of SockJS and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-sockjs?utm_source=npm-sockjs&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) + +# SockJS family * [SockJS-client](https://github.com/sockjs/sockjs-client) JavaScript client library * [SockJS-node](https://github.com/sockjs/sockjs-node) Node.js server * [SockJS-erlang](https://github.com/sockjs/sockjs-erlang) Erlang server * [SockJS-tornado](https://github.com/MrJoes/sockjs-tornado) Python/Tornado server - * [vert.x](https://github.com/purplefox/vert.x) Java/vert.x server + * [vert.x](https://github.com/eclipse/vert.x) Java/vert.x server Work in progress: @@ -18,9 +28,12 @@ Work in progress: * [wai-SockJS](https://github.com/Palmik/wai-sockjs) * [SockJS-perl](https://github.com/vti/sockjs-perl) * [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 +latest stable release is at: https://github.com/sockjs/sockjs-node/tree/v0.3.19 ️⚠️ -What is SockJS? -=============== +# What is SockJS? SockJS is a JavaScript library (for browsers) that provides a WebSocket-like object. SockJS gives you a coherent, cross-browser, Javascript API @@ -29,39 +42,31 @@ channel between the browser and the web server, with WebSockets or without. This necessitates the use of a server, which this is one version of, for Node.js. -SockJS-node server -================== +# SockJS-node server SockJS-node is a Node.js server side counterpart of -[SockJS-client browser library](https://github.com/sockjs/sockjs-client) -written in CoffeeScript. +[SockJS-client browser library](https://github.com/sockjs/sockjs-client). To install `sockjs-node` run: npm install sockjs -For additional security (true random numbers) you might want to -install `rbytes` package - SockJS will use it if available: - - npm install rbytes - - A simplified echo SockJS server could look more or less like: ```javascript -var http = require('http'); -var sockjs = require('sockjs'); +const http = require('http'); +const sockjs = require('sockjs'); -var echo = sockjs.createServer(); +const echo = sockjs.createServer({ prefix:'/echo' }); echo.on('connection', function(conn) { - conn.on('data', function(message) { - conn.write(message); - }); - conn.on('close', function() {}); + conn.on('data', function(message) { + conn.write(message); + }); + conn.on('close', function() {}); }); -var server = http.createServer(); -echo.installHandlers(server, {prefix:'/echo'}); +const server = http.createServer(); +echo.attach(server); server.listen(9999, '0.0.0.0'); ``` @@ -74,34 +79,20 @@ Subscribe to discussions and support. -Live QUnit tests and smoke tests --------------------------------- - -[SockJS-client](https://github.com/sockjs/sockjs-client) comes with -some QUnit tests and a few smoke tests that are using SockJS-node. At -the moment they are deployed in few places, just click to see if -SockJS is working in your browser: - - * http://sockjs.popcnt.org/ and https://sockjs.popcnt.org/ (hosted in Europe) - * http://sockjs.cloudfoundry.com/ (CloudFoundry, websockets disabled, loadbalanced) - * https://sockjs.cloudfoundry.com/ (CloudFoundry SSL, websockets disabled, loadbalanced) - - -SockJS-node API ---------------- +# SockJS-node API -The API design is based on the common Node API's like -[Streams API](http://nodejs.org/docs/v0.5.8/api/streams.html) or -[Http.Server API](http://nodejs.org/docs/v0.5.8/api/http.html#http.Server). +The API design is based on common Node APIs like the +[Streams API](https://nodejs.org/api/stream.html) or the +[Http.Server API](https://nodejs.org/api/http.html#http_class_http_server). -### Server class +## Server class SockJS module is generating a `Server` class, similar to -[Node.js http.createServer](http://nodejs.org/docs/v0.5.8/api/http.html#http.createServer) +[Node.js http.createServer](https://nodejs.org/api/http.html#http_http_createserver_requestlistener) module. ```javascript -var sockjs_server = sockjs.createServer(options); +const sockjs_server = sockjs.createServer(options); ``` Where `options` is a hash which can contain: @@ -116,7 +107,7 @@ Where `options` is a hash which can contain: domain local to the SockJS server. This iframe also does need to load SockJS javascript client library, and this option lets you specify its url (if you're unsure, point it to - + the latest minified SockJS client release, this is the default). You must explicitly specify this url on the server side for security reasons - we don't want the possibility of running any foreign @@ -125,7 +116,7 @@ Where `options` is a hash which can contain: browser - it makes sense to reuse the sockjs url you're using in normally. -