@zero-server/sdk 0.9.0 → 0.9.2
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.
- package/LICENSE +21 -21
- package/README.md +460 -437
- package/index.js +414 -412
- package/lib/app.js +1172 -1172
- package/lib/auth/authorize.js +399 -399
- package/lib/auth/enrollment.js +367 -367
- package/lib/auth/index.js +57 -57
- package/lib/auth/jwt.js +731 -731
- package/lib/auth/oauth.js +362 -362
- package/lib/auth/session.js +588 -588
- package/lib/auth/trustedDevice.js +409 -409
- package/lib/auth/twoFactor.js +1150 -1150
- package/lib/auth/webauthn.js +946 -946
- package/lib/body/index.js +14 -14
- package/lib/body/json.js +109 -109
- package/lib/body/multipart.js +440 -440
- package/lib/body/raw.js +71 -71
- package/lib/body/rawBuffer.js +160 -160
- package/lib/body/sendError.js +25 -25
- package/lib/body/text.js +75 -75
- package/lib/body/typeMatch.js +41 -41
- package/lib/body/urlencoded.js +235 -235
- package/lib/cli.js +845 -845
- package/lib/cluster.js +666 -666
- package/lib/debug.js +372 -372
- package/lib/env/index.js +460 -460
- package/lib/errors.js +683 -683
- package/lib/fetch/index.js +256 -256
- package/lib/grpc/balancer.js +378 -378
- package/lib/grpc/call.js +708 -708
- package/lib/grpc/client.js +764 -764
- package/lib/grpc/codec.js +1221 -1221
- package/lib/grpc/credentials.js +398 -398
- package/lib/grpc/frame.js +262 -262
- package/lib/grpc/health.js +287 -287
- package/lib/grpc/index.js +121 -121
- package/lib/grpc/metadata.js +461 -461
- package/lib/grpc/proto.js +821 -821
- package/lib/grpc/reflection.js +590 -590
- package/lib/grpc/server.js +445 -445
- package/lib/grpc/status.js +118 -118
- package/lib/grpc/watch.js +173 -173
- package/lib/http/index.js +10 -10
- package/lib/http/request.js +727 -727
- package/lib/http/response.js +799 -799
- package/lib/lifecycle.js +557 -557
- package/lib/middleware/compress.js +230 -230
- package/lib/middleware/cookieParser.js +237 -237
- package/lib/middleware/cors.js +93 -93
- package/lib/middleware/csrf.js +136 -136
- package/lib/middleware/errorHandler.js +101 -101
- package/lib/middleware/helmet.js +175 -175
- package/lib/middleware/index.js +19 -17
- package/lib/middleware/logger.js +74 -74
- package/lib/middleware/rateLimit.js +88 -88
- package/lib/middleware/requestId.js +53 -53
- package/lib/middleware/static.js +326 -326
- package/lib/middleware/timeout.js +71 -71
- package/lib/middleware/validator.js +254 -254
- package/lib/observe/health.js +326 -326
- package/lib/observe/index.js +50 -50
- package/lib/observe/logger.js +359 -359
- package/lib/observe/metrics.js +805 -805
- package/lib/observe/tracing.js +592 -592
- package/lib/orm/adapters/json.js +290 -290
- package/lib/orm/adapters/memory.js +764 -764
- package/lib/orm/adapters/mongo.js +764 -764
- package/lib/orm/adapters/mysql.js +933 -933
- package/lib/orm/adapters/postgres.js +1144 -1144
- package/lib/orm/adapters/redis.js +1534 -1534
- package/lib/orm/adapters/sql-base.js +212 -212
- package/lib/orm/adapters/sqlite.js +858 -858
- package/lib/orm/audit.js +649 -649
- package/lib/orm/cache.js +394 -394
- package/lib/orm/geo.js +387 -387
- package/lib/orm/index.js +784 -784
- package/lib/orm/migrate.js +432 -432
- package/lib/orm/model.js +1706 -1706
- package/lib/orm/plugin.js +375 -375
- package/lib/orm/procedures.js +836 -836
- package/lib/orm/profiler.js +233 -233
- package/lib/orm/query.js +1772 -1772
- package/lib/orm/replicas.js +241 -241
- package/lib/orm/schema.js +307 -307
- package/lib/orm/search.js +380 -380
- package/lib/orm/seed/data/commerce.js +136 -136
- package/lib/orm/seed/data/internet.js +111 -111
- package/lib/orm/seed/data/locations.js +204 -204
- package/lib/orm/seed/data/names.js +338 -338
- package/lib/orm/seed/data/person.js +128 -128
- package/lib/orm/seed/data/phone.js +211 -211
- package/lib/orm/seed/data/words.js +134 -134
- package/lib/orm/seed/factory.js +178 -178
- package/lib/orm/seed/fake.js +1186 -1186
- package/lib/orm/seed/index.js +18 -18
- package/lib/orm/seed/rng.js +70 -70
- package/lib/orm/seed/seeder.js +124 -124
- package/lib/orm/seed/unique.js +68 -68
- package/lib/orm/snapshot.js +366 -366
- package/lib/orm/tenancy.js +605 -605
- package/lib/orm/views.js +350 -350
- package/lib/router/index.js +436 -436
- package/lib/sse/index.js +8 -8
- package/lib/sse/stream.js +349 -349
- package/lib/ws/connection.js +451 -451
- package/lib/ws/handshake.js +125 -125
- package/lib/ws/index.js +14 -14
- package/lib/ws/room.js +223 -223
- package/package.json +73 -73
- package/types/app.d.ts +223 -223
- package/types/auth.d.ts +520 -520
- package/types/cluster.d.ts +75 -75
- package/types/env.d.ts +80 -80
- package/types/errors.d.ts +316 -316
- package/types/fetch.d.ts +43 -43
- package/types/grpc.d.ts +432 -432
- package/types/index.d.ts +384 -384
- package/types/lifecycle.d.ts +60 -60
- package/types/middleware.d.ts +320 -320
- package/types/observe.d.ts +304 -304
- package/types/orm.d.ts +1887 -1887
- package/types/request.d.ts +109 -109
- package/types/response.d.ts +157 -157
- package/types/router.d.ts +78 -78
- package/types/sse.d.ts +78 -78
- package/types/websocket.d.ts +126 -126
package/lib/ws/handshake.js
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module ws/handshake
|
|
3
|
-
* @description WebSocket upgrade handshake logic (RFC 6455).
|
|
4
|
-
* Handles the HTTP → WS upgrade, verifyClient, sub-protocol
|
|
5
|
-
* negotiation, query parsing, and instantiation of a
|
|
6
|
-
* `WebSocketConnection`.
|
|
7
|
-
*/
|
|
8
|
-
const crypto = require('crypto');
|
|
9
|
-
const WebSocketConnection = require('./connection');
|
|
10
|
-
const log = require('../debug')('zero:ws');
|
|
11
|
-
|
|
12
|
-
/** RFC 6455 magic GUID used in the Sec-WebSocket-Accept hash. */
|
|
13
|
-
const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Handle an HTTP upgrade request and turn it into a WebSocket connection.
|
|
17
|
-
*
|
|
18
|
-
* @param {import('http').IncomingMessage} req - The upgrade request.
|
|
19
|
-
* @param {import('net').Socket} socket - The underlying TCP socket.
|
|
20
|
-
* @param {Buffer} head - First packet of the upgraded stream.
|
|
21
|
-
* @param {Map<string, { handler: Function, opts: object }>} wsHandlers - Registered WS path→handler map.
|
|
22
|
-
*/
|
|
23
|
-
function handleUpgrade(req, socket, head, wsHandlers)
|
|
24
|
-
{
|
|
25
|
-
// Guard against socket errors (e.g. ECONNRESET from restricted webviews)
|
|
26
|
-
socket.on('error', () => {});
|
|
27
|
-
|
|
28
|
-
const urlPath = req.url.split('?')[0];
|
|
29
|
-
const entry = wsHandlers.get(urlPath);
|
|
30
|
-
|
|
31
|
-
if (!entry)
|
|
32
|
-
{
|
|
33
|
-
log.warn('no WS handler for %s', urlPath);
|
|
34
|
-
socket.write('HTTP/1.1 404 Not Found\r\n\r\n');
|
|
35
|
-
socket.destroy();
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const { handler, opts } = entry;
|
|
40
|
-
|
|
41
|
-
// -- Optional client verification ------------------
|
|
42
|
-
if (typeof opts.verifyClient === 'function')
|
|
43
|
-
{
|
|
44
|
-
try
|
|
45
|
-
{
|
|
46
|
-
if (!opts.verifyClient(req))
|
|
47
|
-
{
|
|
48
|
-
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
49
|
-
socket.destroy();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
catch (e)
|
|
54
|
-
{
|
|
55
|
-
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
|
|
56
|
-
socket.destroy();
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// -- Validate key ----------------------------------
|
|
62
|
-
const key = req.headers['sec-websocket-key'];
|
|
63
|
-
if (!key)
|
|
64
|
-
{
|
|
65
|
-
socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
66
|
-
socket.destroy();
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// -- Negotiate sub-protocol ------------------------
|
|
71
|
-
const clientProtocols = req.headers['sec-websocket-protocol'];
|
|
72
|
-
const extensions = req.headers['sec-websocket-extensions'] || '';
|
|
73
|
-
|
|
74
|
-
// -- Perform the WebSocket handshake (RFC 6455) ----
|
|
75
|
-
const accept = crypto.createHash('sha1').update(key + WS_MAGIC).digest('base64');
|
|
76
|
-
|
|
77
|
-
let handshake =
|
|
78
|
-
'HTTP/1.1 101 Switching Protocols\r\n' +
|
|
79
|
-
'Upgrade: websocket\r\n' +
|
|
80
|
-
'Connection: Upgrade\r\n' +
|
|
81
|
-
'Sec-WebSocket-Accept: ' + accept + '\r\n';
|
|
82
|
-
|
|
83
|
-
// Echo first requested protocol if any
|
|
84
|
-
const protocol = clientProtocols ? clientProtocols.split(',')[0].trim() : '';
|
|
85
|
-
if (protocol) handshake += 'Sec-WebSocket-Protocol: ' + protocol + '\r\n';
|
|
86
|
-
|
|
87
|
-
handshake += '\r\n';
|
|
88
|
-
socket.write(handshake);
|
|
89
|
-
log.info('upgrade complete for %s', urlPath);
|
|
90
|
-
|
|
91
|
-
// -- Parse query string ----------------------------
|
|
92
|
-
const qIdx = req.url.indexOf('?');
|
|
93
|
-
const query = {};
|
|
94
|
-
if (qIdx !== -1)
|
|
95
|
-
{
|
|
96
|
-
for (const [k, v] of new URLSearchParams(req.url.slice(qIdx + 1)))
|
|
97
|
-
{
|
|
98
|
-
query[k] = v;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// -- Create the connection wrapper -----------------
|
|
103
|
-
const ws = new WebSocketConnection(socket, {
|
|
104
|
-
maxPayload: opts.maxPayload,
|
|
105
|
-
pingInterval: opts.pingInterval,
|
|
106
|
-
protocol,
|
|
107
|
-
extensions,
|
|
108
|
-
headers: req.headers,
|
|
109
|
-
ip: req.socket ? req.socket.remoteAddress : null,
|
|
110
|
-
query,
|
|
111
|
-
url: req.url,
|
|
112
|
-
secure: !!(req.socket && req.socket.encrypted),
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
try
|
|
116
|
-
{
|
|
117
|
-
handler(ws, req);
|
|
118
|
-
}
|
|
119
|
-
catch (err)
|
|
120
|
-
{
|
|
121
|
-
ws.close(1011, 'Internal error');
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
module.exports = handleUpgrade;
|
|
1
|
+
/**
|
|
2
|
+
* @module ws/handshake
|
|
3
|
+
* @description WebSocket upgrade handshake logic (RFC 6455).
|
|
4
|
+
* Handles the HTTP → WS upgrade, verifyClient, sub-protocol
|
|
5
|
+
* negotiation, query parsing, and instantiation of a
|
|
6
|
+
* `WebSocketConnection`.
|
|
7
|
+
*/
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
const WebSocketConnection = require('./connection');
|
|
10
|
+
const log = require('../debug')('zero:ws');
|
|
11
|
+
|
|
12
|
+
/** RFC 6455 magic GUID used in the Sec-WebSocket-Accept hash. */
|
|
13
|
+
const WS_MAGIC = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Handle an HTTP upgrade request and turn it into a WebSocket connection.
|
|
17
|
+
*
|
|
18
|
+
* @param {import('http').IncomingMessage} req - The upgrade request.
|
|
19
|
+
* @param {import('net').Socket} socket - The underlying TCP socket.
|
|
20
|
+
* @param {Buffer} head - First packet of the upgraded stream.
|
|
21
|
+
* @param {Map<string, { handler: Function, opts: object }>} wsHandlers - Registered WS path→handler map.
|
|
22
|
+
*/
|
|
23
|
+
function handleUpgrade(req, socket, head, wsHandlers)
|
|
24
|
+
{
|
|
25
|
+
// Guard against socket errors (e.g. ECONNRESET from restricted webviews)
|
|
26
|
+
socket.on('error', () => {});
|
|
27
|
+
|
|
28
|
+
const urlPath = req.url.split('?')[0];
|
|
29
|
+
const entry = wsHandlers.get(urlPath);
|
|
30
|
+
|
|
31
|
+
if (!entry)
|
|
32
|
+
{
|
|
33
|
+
log.warn('no WS handler for %s', urlPath);
|
|
34
|
+
socket.write('HTTP/1.1 404 Not Found\r\n\r\n');
|
|
35
|
+
socket.destroy();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const { handler, opts } = entry;
|
|
40
|
+
|
|
41
|
+
// -- Optional client verification ------------------
|
|
42
|
+
if (typeof opts.verifyClient === 'function')
|
|
43
|
+
{
|
|
44
|
+
try
|
|
45
|
+
{
|
|
46
|
+
if (!opts.verifyClient(req))
|
|
47
|
+
{
|
|
48
|
+
socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
|
|
49
|
+
socket.destroy();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
catch (e)
|
|
54
|
+
{
|
|
55
|
+
socket.write('HTTP/1.1 500 Internal Server Error\r\n\r\n');
|
|
56
|
+
socket.destroy();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// -- Validate key ----------------------------------
|
|
62
|
+
const key = req.headers['sec-websocket-key'];
|
|
63
|
+
if (!key)
|
|
64
|
+
{
|
|
65
|
+
socket.write('HTTP/1.1 400 Bad Request\r\n\r\n');
|
|
66
|
+
socket.destroy();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// -- Negotiate sub-protocol ------------------------
|
|
71
|
+
const clientProtocols = req.headers['sec-websocket-protocol'];
|
|
72
|
+
const extensions = req.headers['sec-websocket-extensions'] || '';
|
|
73
|
+
|
|
74
|
+
// -- Perform the WebSocket handshake (RFC 6455) ----
|
|
75
|
+
const accept = crypto.createHash('sha1').update(key + WS_MAGIC).digest('base64');
|
|
76
|
+
|
|
77
|
+
let handshake =
|
|
78
|
+
'HTTP/1.1 101 Switching Protocols\r\n' +
|
|
79
|
+
'Upgrade: websocket\r\n' +
|
|
80
|
+
'Connection: Upgrade\r\n' +
|
|
81
|
+
'Sec-WebSocket-Accept: ' + accept + '\r\n';
|
|
82
|
+
|
|
83
|
+
// Echo first requested protocol if any
|
|
84
|
+
const protocol = clientProtocols ? clientProtocols.split(',')[0].trim() : '';
|
|
85
|
+
if (protocol) handshake += 'Sec-WebSocket-Protocol: ' + protocol + '\r\n';
|
|
86
|
+
|
|
87
|
+
handshake += '\r\n';
|
|
88
|
+
socket.write(handshake);
|
|
89
|
+
log.info('upgrade complete for %s', urlPath);
|
|
90
|
+
|
|
91
|
+
// -- Parse query string ----------------------------
|
|
92
|
+
const qIdx = req.url.indexOf('?');
|
|
93
|
+
const query = {};
|
|
94
|
+
if (qIdx !== -1)
|
|
95
|
+
{
|
|
96
|
+
for (const [k, v] of new URLSearchParams(req.url.slice(qIdx + 1)))
|
|
97
|
+
{
|
|
98
|
+
query[k] = v;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// -- Create the connection wrapper -----------------
|
|
103
|
+
const ws = new WebSocketConnection(socket, {
|
|
104
|
+
maxPayload: opts.maxPayload,
|
|
105
|
+
pingInterval: opts.pingInterval,
|
|
106
|
+
protocol,
|
|
107
|
+
extensions,
|
|
108
|
+
headers: req.headers,
|
|
109
|
+
ip: req.socket ? req.socket.remoteAddress : null,
|
|
110
|
+
query,
|
|
111
|
+
url: req.url,
|
|
112
|
+
secure: !!(req.socket && req.socket.encrypted),
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
try
|
|
116
|
+
{
|
|
117
|
+
handler(ws, req);
|
|
118
|
+
}
|
|
119
|
+
catch (err)
|
|
120
|
+
{
|
|
121
|
+
ws.close(1011, 'Internal error');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
module.exports = handleUpgrade;
|
package/lib/ws/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module ws
|
|
3
|
-
* @description WebSocket support for zero-server.
|
|
4
|
-
* Exports the connection class, upgrade handler, and pool manager.
|
|
5
|
-
*/
|
|
6
|
-
const WebSocketConnection = require('./connection');
|
|
7
|
-
const handleUpgrade = require('./handshake');
|
|
8
|
-
const WebSocketPool = require('./room');
|
|
9
|
-
|
|
10
|
-
module.exports = {
|
|
11
|
-
WebSocketConnection,
|
|
12
|
-
handleUpgrade,
|
|
13
|
-
WebSocketPool,
|
|
14
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @module ws
|
|
3
|
+
* @description WebSocket support for zero-server.
|
|
4
|
+
* Exports the connection class, upgrade handler, and pool manager.
|
|
5
|
+
*/
|
|
6
|
+
const WebSocketConnection = require('./connection');
|
|
7
|
+
const handleUpgrade = require('./handshake');
|
|
8
|
+
const WebSocketPool = require('./room');
|
|
9
|
+
|
|
10
|
+
module.exports = {
|
|
11
|
+
WebSocketConnection,
|
|
12
|
+
handleUpgrade,
|
|
13
|
+
WebSocketPool,
|
|
14
|
+
};
|