@linebridge/engine-neo 1.0.7 → 1.2.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.
@@ -0,0 +1,139 @@
1
+ import uws_wrapper from "../uws.js";
2
+ import Websocket from "./Websocket.js";
3
+ import { wrap_object, array_buffer_to_string } from "../utils.js";
4
+ export default class WebsocketRoute {
5
+ #upgrade_with;
6
+ #message_parser;
7
+ app;
8
+ pattern;
9
+ handler;
10
+ options = {
11
+ idle_timeout: 32,
12
+ message_type: "String",
13
+ compression: uws_wrapper.DISABLED,
14
+ max_backpressure: 1024 * 1024,
15
+ max_payload_length: 32 * 1024,
16
+ };
17
+ constructor({ app, pattern, handler, options, }) {
18
+ this.app = app;
19
+ this.pattern = pattern;
20
+ this.handler = handler;
21
+ // merge user options over the local defaults
22
+ wrap_object(this.options, options ?? {});
23
+ this.#message_parser = this._get_message_parser(this.options.message_type);
24
+ this._load_companion_route();
25
+ this._create_uws_route();
26
+ }
27
+ /**
28
+ * Returns a parser that converts a uws ArrayBuffer to the configured type.
29
+ */
30
+ _get_message_parser(type) {
31
+ switch (type) {
32
+ case "String":
33
+ return (array_buffer) => array_buffer_to_string(array_buffer);
34
+ case "Buffer":
35
+ // copy the buffer because uws deallocates it after this sync call
36
+ return (array_buffer) => Buffer.concat([Buffer.from(array_buffer)]);
37
+ case "ArrayBuffer":
38
+ return (array_buffer) => array_buffer;
39
+ default:
40
+ throw new Error("Server.ws(options) -> options.message_type must be one of ['String', 'Buffer', 'ArrayBuffer']");
41
+ }
42
+ }
43
+ /**
44
+ * Loads the companion upgrade route from the app routes.
45
+ * @private
46
+ */
47
+ _load_companion_route() {
48
+ const companion = this.app.routes["upgrade"][this.pattern];
49
+ if (companion) {
50
+ this.#upgrade_with = companion;
51
+ return;
52
+ }
53
+ // create a temporary default route that upgrades every request
54
+ this.app._create_route({
55
+ method: "upgrade",
56
+ pattern: this.pattern,
57
+ handler: (_request, response) => response.upgrade(),
58
+ options: {
59
+ _temporary: true,
60
+ },
61
+ });
62
+ this.#upgrade_with = this.app.routes["upgrade"][this.pattern];
63
+ }
64
+ /**
65
+ * Sets the upgrade route used for incoming upgrade requests.
66
+ * @private
67
+ */
68
+ _set_upgrade_route(route) {
69
+ this.#upgrade_with = route;
70
+ }
71
+ /**
72
+ * Creates the uws ws route that powers this instance.
73
+ * @private
74
+ */
75
+ _create_uws_route() {
76
+ const { compression, idle_timeout, max_backpressure, max_payload_length, } = this.options;
77
+ const uws_options = {
78
+ compression,
79
+ idleTimeout: idle_timeout,
80
+ maxBackpressure: max_backpressure,
81
+ maxPayloadLength: max_payload_length,
82
+ };
83
+ // pipe upgrade requests through the app request lifecycle
84
+ uws_options.upgrade = (uws_response, uws_request, socket_context) => this.app._handle_uws_request(this.#upgrade_with, uws_request, uws_response, socket_context);
85
+ uws_options.open = (ws) => this._on_open(ws);
86
+ uws_options.drain = (ws) => this._on_drain(ws);
87
+ uws_options.ping = (ws, message) => this._on_ping(ws, message);
88
+ uws_options.pong = (ws, message) => this._on_pong(ws, message);
89
+ uws_options.close = (ws, code, message) => this._on_close(ws, code, message);
90
+ uws_options.message = (ws, message, is_binary) => this._on_message(ws, message, is_binary);
91
+ this.app.uws_instance.ws(this.pattern, uws_options);
92
+ }
93
+ /**
94
+ * Handles the uws open event.
95
+ * @private
96
+ */
97
+ _on_open(ws) {
98
+ ws.poly = new Websocket(ws);
99
+ this.handler(ws.poly);
100
+ }
101
+ /**
102
+ * Handles the uws ping event.
103
+ * @private
104
+ */
105
+ _on_ping(ws, message = new ArrayBuffer(0)) {
106
+ ws.poly.emit("ping", this.#message_parser(message));
107
+ }
108
+ /**
109
+ * Handles the uws pong event.
110
+ * @private
111
+ */
112
+ _on_pong(ws, message = new ArrayBuffer(0)) {
113
+ ws.poly.emit("pong", this.#message_parser(message));
114
+ }
115
+ /**
116
+ * Handles the uws drain event.
117
+ * @private
118
+ */
119
+ _on_drain(ws) {
120
+ ws.poly.emit("drain");
121
+ }
122
+ /**
123
+ * Handles the uws message event.
124
+ * @private
125
+ */
126
+ _on_message(ws, message = new ArrayBuffer(0), is_binary) {
127
+ ws.poly.emit("message", this.#message_parser(message), is_binary);
128
+ }
129
+ /**
130
+ * Handles the uws close event.
131
+ * @private
132
+ */
133
+ _on_close(ws, code, message = new ArrayBuffer(0)) {
134
+ ws.poly._destroy();
135
+ ws.poly.emit("close", code, this.#message_parser(message));
136
+ // release the polyfill so it can be garbage collected
137
+ delete ws.poly;
138
+ }
139
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linebridge/engine-neo",
3
- "version": "1.0.7",
3
+ "version": "1.2.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -28,13 +28,12 @@
28
28
  "dependencies": {
29
29
  "busboy": "^1.6.0",
30
30
  "cookie": "^2.0.1",
31
- "cookie-signature": "^1.2.2",
32
31
  "fast-json-stringify": "^7.0.1",
33
32
  "fast-querystring": "^1.1.2",
34
33
  "mime-types": "^3.0.2",
35
34
  "tseep": "^1.3.1"
36
35
  },
37
36
  "scripts": {
38
- "postinstall": "node install.mjs"
37
+ "install": "node install.mjs"
39
38
  }
40
39
  }