@depup/primus 8.0.9-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +36 -0
  3. package/changes.json +30 -0
  4. package/dist/primus.js +3663 -0
  5. package/errors.js +51 -0
  6. package/index.js +1153 -0
  7. package/middleware/access-control.js +20 -0
  8. package/middleware/authorization.js +57 -0
  9. package/middleware/error.js +41 -0
  10. package/middleware/forwarded.js +18 -0
  11. package/middleware/no-cache.js +25 -0
  12. package/middleware/primus.js +47 -0
  13. package/middleware/spec.js +36 -0
  14. package/middleware/xss.js +28 -0
  15. package/package.json +140 -0
  16. package/parsers/binary.js +54 -0
  17. package/parsers/ejson.js +41 -0
  18. package/parsers/json.js +35 -0
  19. package/parsers/msgpack.js +55 -0
  20. package/parsers.json +12 -0
  21. package/primus.js +1158 -0
  22. package/spark.js +515 -0
  23. package/transformer.js +237 -0
  24. package/transformers/browserchannel/client.js +92 -0
  25. package/transformers/browserchannel/index.js +19 -0
  26. package/transformers/browserchannel/server.js +64 -0
  27. package/transformers/engine.io/README.md +34 -0
  28. package/transformers/engine.io/client.js +111 -0
  29. package/transformers/engine.io/index.js +15 -0
  30. package/transformers/engine.io/library.js +2273 -0
  31. package/transformers/engine.io/server.js +63 -0
  32. package/transformers/faye/client.js +103 -0
  33. package/transformers/faye/index.js +12 -0
  34. package/transformers/faye/server.js +85 -0
  35. package/transformers/sockjs/README.md +31 -0
  36. package/transformers/sockjs/client.js +86 -0
  37. package/transformers/sockjs/index.js +15 -0
  38. package/transformers/sockjs/library.js +4201 -0
  39. package/transformers/sockjs/server.js +108 -0
  40. package/transformers/uws/index.js +12 -0
  41. package/transformers/uws/server.js +131 -0
  42. package/transformers/websockets/client.js +112 -0
  43. package/transformers/websockets/index.js +12 -0
  44. package/transformers/websockets/server.js +77 -0
  45. package/transformers.json +26 -0
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var access = require('access-control');
4
+
5
+ /**
6
+ * Add Access-Control to each request.
7
+ *
8
+ * @returns {Function}
9
+ * @api public
10
+ */
11
+ module.exports = function configure() {
12
+ var control = access(this.options);
13
+
14
+ //
15
+ // We don't add Access-Control headers for HTTP upgrades.
16
+ //
17
+ control.upgrade = false;
18
+
19
+ return control;
20
+ };
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Authorization middleware for Primus which would accept or deny requests.
5
+ *
6
+ * @returns {Function}
7
+ * @api public
8
+ */
9
+ module.exports = function configuration() {
10
+ /**
11
+ * The actual HTTP middleware.
12
+ *
13
+ * @param {Request} req HTTP request.
14
+ * @param {Response} res HTTP response.
15
+ * @param {Function} next Continuation.
16
+ * @api private
17
+ */
18
+ return function client(req, res, next) {
19
+ if ('function' !== typeof this.auth) return next();
20
+
21
+ this.auth(req, function authorized(err) {
22
+ if (!err) return next();
23
+
24
+ var message = JSON.stringify({ error: err.message || err })
25
+ , length = Buffer.byteLength(message)
26
+ , code = err.statusCode || 401;
27
+
28
+ //
29
+ // We need to handle two cases here, authentication for regular HTTP
30
+ // requests as well as authentication of WebSocket (upgrade) requests.
31
+ //
32
+ if (res.setHeader) {
33
+ res.statusCode = code;
34
+ res.setHeader('Content-Type', 'application/json');
35
+ res.setHeader('Content-Length', length);
36
+
37
+ if (code === 401 && err.authenticate) {
38
+ res.setHeader('WWW-Authenticate', err.authenticate);
39
+ }
40
+ } else {
41
+ res.write('HTTP/'+ req.httpVersion +' ');
42
+ res.write(code +' '+ require('http').STATUS_CODES[code] +'\r\n');
43
+ res.write('Connection: close\r\n');
44
+ res.write('Content-Type: application/json\r\n');
45
+ res.write('Content-Length: '+ length +'\r\n');
46
+
47
+ if (code === 401 && err.authenticate) {
48
+ res.write('WWW-Authenticate: ' + err.authenticate + '\r\n');
49
+ }
50
+
51
+ res.write('\r\n');
52
+ }
53
+
54
+ res.end(message);
55
+ });
56
+ };
57
+ };
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ const http = require('http');
4
+
5
+ /**
6
+ * WARNING: this middleware is only used internally and does not follow the
7
+ * pattern of the other middleware. You should not use it.
8
+ *
9
+ * Handle async middleware errors.
10
+ *
11
+ * @param {Error} err Error returned by the middleware.
12
+ * @param {Request} req HTTP request.
13
+ * @param {Response} res HTTP response.
14
+ * @api private
15
+ */
16
+ module.exports = function error(err, req, res) {
17
+ const message = JSON.stringify({ error: err.message || err });
18
+ const length = Buffer.byteLength(message);
19
+ const code = err.statusCode || 500;
20
+
21
+ //
22
+ // As in the authorization middleware we need to handle two cases here:
23
+ // regular HTTP requests and upgrade requests.
24
+ //
25
+ if (res.setHeader) {
26
+ res.statusCode = code;
27
+ res.setHeader('Content-Type', 'application/json');
28
+ res.setHeader('Content-Length', length);
29
+
30
+ return res.end(message);
31
+ }
32
+
33
+ res.write(
34
+ `HTTP/${req.httpVersion} ${code} ${http.STATUS_CODES[code]}\r\n` +
35
+ 'Connection: close\r\n' +
36
+ 'Content-Type: application/json\r\n' +
37
+ `Content-Length: ${length}\r\n\r\n` +
38
+ message
39
+ );
40
+ res.destroy();
41
+ };
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var forwarded = require('forwarded-for');
4
+
5
+ /**
6
+ * Add the `forwarded` property.
7
+ *
8
+ * @param {Request} req HTTP request.
9
+ * @param {Response} res HTTP response.
10
+ * @api private
11
+ */
12
+ module.exports = function configure() {
13
+ var primus = this;
14
+
15
+ return function ipaddress(req, res) {
16
+ req.forwarded = forwarded(req, req.headers || {}, primus.whitelist);
17
+ };
18
+ };
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ var setHeader = require('setheader');
4
+
5
+ /**
6
+ * Forcefully add no-cache headers to HTTP responses.
7
+ *
8
+ * @param {Request} req The incoming HTTP request.
9
+ * @param {Response} res The outgoing HTTP response.
10
+ * @api public
11
+ */
12
+ function nocache(req, res) {
13
+ setHeader(res, 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
14
+ setHeader(res, 'Pragma', 'no-cache');
15
+ }
16
+
17
+ //
18
+ // We don't need no-cache headers for HTTP upgrades.
19
+ //
20
+ nocache.upgrade = false;
21
+
22
+ //
23
+ // Expose the module.
24
+ //
25
+ module.exports = nocache;
@@ -0,0 +1,47 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Serve the client library that is shipped and compiled within Primus.
5
+ *
6
+ * @returns {Function}
7
+ * @api public
8
+ */
9
+ module.exports = function configure() {
10
+ var primusjs = this.pathname +'/primus.js'
11
+ , primus = this
12
+ , library
13
+ , length;
14
+
15
+ /**
16
+ * The actual HTTP middleware.
17
+ *
18
+ * @param {Request} req HTTP request.
19
+ * @param {Response} res HTTP response.
20
+ * @api private
21
+ */
22
+ function client(req, res) {
23
+ if (req.uri.pathname !== primusjs) return;
24
+
25
+ //
26
+ // Lazy include and compile the library so we give our server some time to
27
+ // add plugins or we will compile the client library without plugins, which
28
+ // is sad :(
29
+ //
30
+ library = library || Buffer.from(primus.library());
31
+ length = length || library.length;
32
+
33
+ res.statusCode = 200;
34
+ res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
35
+ res.setHeader('Content-Length', length);
36
+ res.end(library);
37
+
38
+ return true;
39
+ }
40
+
41
+ //
42
+ // We don't serve our client-side library over HTTP upgrades.
43
+ //
44
+ client.upgrade = false;
45
+
46
+ return client;
47
+ };
@@ -0,0 +1,36 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Answer HTTP requests with the server specification when requested.
5
+ *
6
+ * @returns {Function}
7
+ * @api public
8
+ */
9
+ module.exports = function configure() {
10
+ var specification = this.pathname +'/spec'
11
+ , primus = this;
12
+
13
+ /**
14
+ * The actual HTTP middleware.
15
+ *
16
+ * @param {Request} req HTTP request.
17
+ * @param {Response} res HTTP response.
18
+ * @api private
19
+ */
20
+ function spec(req, res) {
21
+ if (req.uri.pathname !== specification) return;
22
+
23
+ res.statusCode = 200;
24
+ res.setHeader('Content-Type', 'application/json');
25
+ res.end(JSON.stringify(primus.spec));
26
+
27
+ return true;
28
+ }
29
+
30
+ //
31
+ // Don't run a specification test for HTTP upgrades.
32
+ //
33
+ spec.upgrade = false;
34
+
35
+ return spec;
36
+ };
@@ -0,0 +1,28 @@
1
+ 'use strict';
2
+
3
+ var setHeader = require('setheader');
4
+
5
+ /**
6
+ * Forcefully add x-xss-protection headers.
7
+ *
8
+ * @param {Request} req The incoming HTTP request.
9
+ * @param {Response} res The outgoing HTTP response.
10
+ * @api public
11
+ */
12
+ function xss(req, res) {
13
+ var agent = (req.headers['user-agent'] || '').toLowerCase();
14
+
15
+ if (agent && (~agent.indexOf(';msie') || ~agent.indexOf('trident/'))) {
16
+ setHeader(res, 'X-XSS-Protection', '0');
17
+ }
18
+ }
19
+
20
+ //
21
+ // We don't need protection headers for HTTP upgrades.
22
+ //
23
+ xss.upgrade = false;
24
+
25
+ //
26
+ // Expose the module.
27
+ //
28
+ module.exports = xss;
package/package.json ADDED
@@ -0,0 +1,140 @@
1
+ {
2
+ "name": "@depup/primus",
3
+ "version": "8.0.9-depup.0",
4
+ "description": "[DepUp] Primus is a simple abstraction around real-time frameworks. It allows you to easily switch between different frameworks without any code changes.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "build": "mkdir -p dist && browserify primus.js -s Primus -p deumdify | derequire > dist/primus.js",
8
+ "update": "find transformers -name update.sh -exec bash {} \\;",
9
+ "integration": "npm run build && mocha test/*.integration.js --exit",
10
+ "test": "npm run build && mocha test/*.test.js"
11
+ },
12
+ "homepage": "https://github.com/primus/primus#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git://github.com/primus/primus.git"
16
+ },
17
+ "keywords": [
18
+ "depup",
19
+ "dependency-bumped",
20
+ "updated-deps",
21
+ "primus",
22
+ "abstraction",
23
+ "browserchannel",
24
+ "engine.io",
25
+ "framework",
26
+ "comet",
27
+ "streaming",
28
+ "pubsub",
29
+ "pub",
30
+ "sub",
31
+ "ajax",
32
+ "xhr",
33
+ "polling",
34
+ "http",
35
+ "faye",
36
+ "io",
37
+ "prumus",
38
+ "real-time",
39
+ "realtime",
40
+ "socket",
41
+ "socket.io",
42
+ "sockets",
43
+ "sockjs",
44
+ "spark",
45
+ "transformer",
46
+ "transformers",
47
+ "websocket",
48
+ "websockets",
49
+ "ws",
50
+ "uws"
51
+ ],
52
+ "author": "Arnout Kazemier",
53
+ "license": "MIT",
54
+ "dependencies": {
55
+ "access-control": "^1.0.1",
56
+ "asyncemit": "~3.0.1",
57
+ "create-server": "^1.0.2",
58
+ "diagnostics": "^3.0.0",
59
+ "eventemitter3": "^5.0.4",
60
+ "forwarded-for": "~1.1.0",
61
+ "fusing": "~1.0.0",
62
+ "nanoid": "^5.1.7",
63
+ "setheader": "~1.0.2",
64
+ "ultron": "^1.1.1"
65
+ },
66
+ "devDependencies": {
67
+ "@babel/core": "~7.23.2",
68
+ "@babel/plugin-transform-object-assign": "~7.22.5",
69
+ "@babel/preset-env": "~7.23.2",
70
+ "@rollup/plugin-babel": "~6.0.3",
71
+ "@rollup/plugin-commonjs": "~25.0.3",
72
+ "@rollup/plugin-node-resolve": "~15.2.3",
73
+ "binary-pack": "~1.0.2",
74
+ "browserchannel": "~2.1.0",
75
+ "browserify": "~17.0.0",
76
+ "chai": "~4.3.4",
77
+ "condenseify": "~1.1.1",
78
+ "demolish": "~1.0.2",
79
+ "derequire": "~2.1.1",
80
+ "deumdify": "~1.2.3",
81
+ "ejson": "~2.2.0",
82
+ "emits": "~3.0.0",
83
+ "engine.io": "~6.5.1",
84
+ "engine.io-client": "~6.5.1",
85
+ "faye-websocket": "~0.11.0",
86
+ "inherits": "~2.0.3",
87
+ "mocha": "~10.2.0",
88
+ "pre-commit": "~1.2.0",
89
+ "primus-msgpack": "~1.0.2",
90
+ "pumpify": "~2.0.0",
91
+ "querystringify": "~2.2.0",
92
+ "recovery": "~0.2.6",
93
+ "request": "~2.88.0",
94
+ "rocambole": "~0.7.0",
95
+ "rocambole-node-remove": "~3.0.0",
96
+ "rollup": "~4.3.0",
97
+ "sockjs": "~0.3.18",
98
+ "sockjs-client": "~1.6.0",
99
+ "through2": "~4.0.2",
100
+ "tick-tock": "~1.0.0",
101
+ "url-parse": "~1.5.1",
102
+ "uws": "10.148.1",
103
+ "ws": "~8.14.2",
104
+ "yeast": "~0.1.2"
105
+ },
106
+ "pre-commit": "test, integration",
107
+ "depup": {
108
+ "changes": {
109
+ "access-control": {
110
+ "from": "~1.0.0",
111
+ "to": "^1.0.1"
112
+ },
113
+ "create-server": {
114
+ "from": "~1.0.1",
115
+ "to": "^1.0.2"
116
+ },
117
+ "diagnostics": {
118
+ "from": "~2.0.0",
119
+ "to": "^3.0.0"
120
+ },
121
+ "eventemitter3": {
122
+ "from": "~5.0.0",
123
+ "to": "^5.0.4"
124
+ },
125
+ "nanoid": {
126
+ "from": "~3.3.3",
127
+ "to": "^5.1.7"
128
+ },
129
+ "ultron": {
130
+ "from": "~1.1.0",
131
+ "to": "^1.1.1"
132
+ }
133
+ },
134
+ "depsUpdated": 6,
135
+ "originalPackage": "primus",
136
+ "originalVersion": "8.0.9",
137
+ "processedAt": "2026-03-17T18:41:40.749Z",
138
+ "smokeTest": "passed"
139
+ }
140
+ }
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ const BinaryPack = require('binary-pack');
4
+
5
+ /**
6
+ * Message encoder.
7
+ *
8
+ * @param {Mixed} data The data that needs to be transformed.
9
+ * @param {Function} fn Completion callback.
10
+ * @api public
11
+ */
12
+ exports.encoder = function encoder(data, fn) {
13
+ var err;
14
+
15
+ try { data = BinaryPack.pack(data); }
16
+ catch (e) { err = e; }
17
+
18
+ fn(err, data);
19
+ };
20
+
21
+ /**
22
+ * Message decoder.
23
+ *
24
+ * @param {Mixed} data The data that needs to be transformed.
25
+ * @param {Function} fn Completion callback.
26
+ * @api public
27
+ */
28
+ exports.decoder = function decoder(data, fn) {
29
+ var err;
30
+
31
+ try { data = BinaryPack.unpack(data); }
32
+ catch (e) { err = e; }
33
+
34
+ fn(err, data);
35
+ };
36
+
37
+ //
38
+ // Expose the library so it can be added in our Primus module.
39
+ //
40
+ exports.library = `var BinaryPack = (function () {
41
+ var exports, bp;
42
+
43
+ try { bp = Primus.requires('binary-pack'); }
44
+ catch (e) {}
45
+
46
+ if (bp) return bp;
47
+
48
+ exports = {};
49
+ (function () {
50
+ ${BinaryPack.BrowserSource}
51
+ }).call(exports);
52
+ return exports.BinaryPack;
53
+ })();
54
+ `;
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ var EJSON = require('ejson');
4
+
5
+ /**
6
+ * Message encoder.
7
+ *
8
+ * @param {Mixed} data The data that needs to be transformed into a string.
9
+ * @param {Function} fn Completion callback.
10
+ * @api public
11
+ */
12
+ exports.encoder = function encoder(data, fn) {
13
+ var err;
14
+
15
+ try { data = EJSON.stringify(data); }
16
+ catch (e) { err = e; }
17
+
18
+ fn(err, data);
19
+ };
20
+
21
+ /**
22
+ * Message decoder.
23
+ *
24
+ * @param {Mixed} data The data that needs to be parsed from a string.
25
+ * @param {Function} fn Completion callback.
26
+ * @api public
27
+ */
28
+ exports.decoder = function decoder(data, fn) {
29
+ var err;
30
+
31
+ try { data = EJSON.parse(data); }
32
+ catch (e) { err = e; }
33
+
34
+ fn(err, data);
35
+ };
36
+
37
+ //
38
+ // Expose the library which is compiled for global consumption instead of
39
+ // browserify.
40
+ //
41
+ exports.library = require('ejson/source');
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Message encoder.
5
+ *
6
+ * @param {Mixed} data The data that needs to be transformed into a string.
7
+ * @param {Function} fn Completion callback.
8
+ * @api public
9
+ */
10
+ exports.encoder = function encoder(data, fn) {
11
+ var err;
12
+
13
+ try { data = JSON.stringify(data); }
14
+ catch (e) { err = e; }
15
+
16
+ fn(err, data);
17
+ };
18
+
19
+ /**
20
+ * Message decoder.
21
+ *
22
+ * @param {Mixed} data The data that needs to be parsed from a string.
23
+ * @param {Function} fn Completion callback.
24
+ * @api public
25
+ */
26
+ exports.decoder = function decoder(data, fn) {
27
+ var err;
28
+
29
+ if ('string' !== typeof data) return fn(err, data);
30
+
31
+ try { data = JSON.parse(data); }
32
+ catch (e) { err = e; }
33
+
34
+ fn(err, data);
35
+ };
@@ -0,0 +1,55 @@
1
+ 'use strict';
2
+
3
+ const msgpack = require('primus-msgpack');
4
+
5
+ /**
6
+ * Message encoder.
7
+ *
8
+ * @param {Mixed} data The data that needs to be transformed.
9
+ * @param {Function} fn Completion callback.
10
+ * @api public
11
+ */
12
+ exports.encoder = function encoder(data, fn) {
13
+ var err;
14
+
15
+ try { data = msgpack.encode(data); }
16
+ catch (e) { err = e; }
17
+
18
+ fn(err, data);
19
+ };
20
+
21
+ /**
22
+ * Message decoder.
23
+ *
24
+ * @param {Mixed} data The data that needs to be transformed.
25
+ * @param {Function} fn Completion callback.
26
+ * @api public
27
+ */
28
+ exports.decoder = function decoder(data, fn) {
29
+ var err;
30
+
31
+ try {
32
+ data = msgpack.decode(data instanceof ArrayBuffer ? new Uint8Array(data) : data);
33
+ } catch (e) {
34
+ err = e;
35
+ }
36
+
37
+ fn(err, data);
38
+ };
39
+
40
+ //
41
+ // Expose the library so it can be added in our Primus module.
42
+ //
43
+ exports.library = `var msgpack = (function () {
44
+ var exports, mp;
45
+
46
+ try { mp = Primus.requires('primus-msgpack'); }
47
+ catch (e) {}
48
+
49
+ if (mp) return mp;
50
+
51
+ exports = {};
52
+ ${msgpack.BrowserSource}
53
+ return exports.msgpack;
54
+ })();
55
+ `;
package/parsers.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "json": {},
3
+ "ejson": {
4
+ "server": "ejson"
5
+ },
6
+ "binary": {
7
+ "server": "binary-pack"
8
+ },
9
+ "msgpack": {
10
+ "server": "primus-msgpack"
11
+ }
12
+ }