@celljs/ta-gateway 4.9.0 → 4.9.1

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.
@@ -215,7 +215,8 @@ export class Environments {
215
215
  const next = this.tenants.update(tenant.id, { environment: choice });
216
216
  // What is picked must be there: an id of none throws here, not on the next start.
217
217
  this.provider(next);
218
- this.running.delete(tenant.id);
218
+ // The state stays: the connections it counts close and open again, and release it as they do.
219
+ Object.assign(this.state(tenant.id), { status: 'stopped', endpoint: undefined, error: undefined });
219
220
  return next;
220
221
  }
221
222
  async dispose() {
@@ -1,5 +1,5 @@
1
1
  import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
2
- import type { Duplex } from 'node:stream';
2
+ import { type Duplex } from 'node:stream';
3
3
  import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
4
4
  import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
5
5
  import { Environments } from './environments.js';
@@ -2,6 +2,7 @@ import { createReadStream, existsSync, mkdirSync, statSync } from 'node:fs';
2
2
  import { createServer, request as httpRequest } from 'node:http';
3
3
  import { request as httpsRequest } from 'node:https';
4
4
  import { extname, join, normalize, sep } from 'node:path';
5
+ import { pipeline } from 'node:stream';
5
6
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
6
7
  import { AccessStore, AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
7
8
  import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
@@ -228,11 +229,11 @@ export class GatewayServer {
228
229
  const outgoing = send(target, { method: request.method, headers }, incoming => {
229
230
  var _a;
230
231
  response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, incoming.headers);
231
- incoming.pipe(response);
232
- incoming.on('end', () => resolve());
233
- incoming.on('error', reject);
232
+ // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
233
+ pipeline(incoming, response, () => resolve());
234
234
  });
235
235
  outgoing.on('error', reject);
236
+ response.once('close', () => outgoing.destroy());
236
237
  outgoing.end();
237
238
  });
238
239
  }
@@ -252,7 +253,11 @@ export class GatewayServer {
252
253
  response.end();
253
254
  return;
254
255
  }
255
- createReadStream(file).pipe(response);
256
+ // A file gone meanwhile (the web client being replaced) fails this request, not the gateway.
257
+ const stream = createReadStream(file);
258
+ stream.once('error', () => (response.headersSent ? response.destroy() : SignInPageUtil.end(response, 404)));
259
+ response.once('close', () => stream.destroy());
260
+ stream.pipe(response);
256
261
  }
257
262
  upgrade(request, socket, head) {
258
263
  var _a, _b;
@@ -18,6 +18,8 @@ export declare namespace GatewayServiceUtil {
18
18
  /** The services the gateway answers itself, not the Ta of the tenant. */
19
19
  const SERVICES: string[];
20
20
  function owns(method: unknown): method is string;
21
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
22
+ function mayOwn(text: string): boolean;
21
23
  /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
22
24
  function error(id: unknown, message: string, code: TaErrorCode, hint: string): string;
23
25
  function result(id: unknown, result: unknown): string;
@@ -16,6 +16,11 @@ export var GatewayServiceUtil;
16
16
  return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
17
17
  }
18
18
  GatewayServiceUtil.owns = owns;
19
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
20
+ function mayOwn(text) {
21
+ return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
22
+ }
23
+ GatewayServiceUtil.mayOwn = mayOwn;
19
24
  /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
20
25
  function error(id, message, code, hint) {
21
26
  return JSON.stringify({ jsonrpc: '2.0', id, error: { code: TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
@@ -25,6 +25,7 @@ export declare class GatewaySocket {
25
25
  close(code: number, reason: string): void;
26
26
  protected open(): Promise<void>;
27
27
  protected fromBrowser(text: string): void;
28
+ protected forward(text: string): void;
28
29
  protected answer(id: unknown, method: string, params: unknown[]): Promise<void>;
29
30
  /** The calls that wait, and those to come, fail; those of the gateway still work. */
30
31
  protected fail(reason: string): void;
@@ -68,12 +68,16 @@ export class GatewaySocket {
68
68
  };
69
69
  }
70
70
  fromBrowser(text) {
71
- var _a;
71
+ // Most messages are the Ta's, some large (pictures): forwarded without being read.
72
+ if (this.failure === undefined && !GatewayServiceUtil.mayOwn(text)) {
73
+ this.forward(text);
74
+ return;
75
+ }
72
76
  let message;
73
77
  try {
74
78
  message = JSON.parse(text);
75
79
  }
76
- catch (_b) {
80
+ catch (_a) {
77
81
  this.close(1007, 'Not JSON.');
78
82
  return;
79
83
  }
@@ -87,6 +91,10 @@ export class GatewaySocket {
87
91
  }
88
92
  return;
89
93
  }
94
+ this.forward(text);
95
+ }
96
+ forward(text) {
97
+ var _a;
90
98
  if (((_a = this.inner) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
91
99
  this.inner.send(text);
92
100
  }
@@ -219,7 +219,8 @@ class Environments {
219
219
  const next = this.tenants.update(tenant.id, { environment: choice });
220
220
  // What is picked must be there: an id of none throws here, not on the next start.
221
221
  this.provider(next);
222
- this.running.delete(tenant.id);
222
+ // The state stays: the connections it counts close and open again, and release it as they do.
223
+ Object.assign(this.state(tenant.id), { status: 'stopped', endpoint: undefined, error: undefined });
223
224
  return next;
224
225
  }
225
226
  async dispose() {
@@ -1,5 +1,5 @@
1
1
  import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
2
- import type { Duplex } from 'node:stream';
2
+ import { type Duplex } from 'node:stream';
3
3
  import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
4
4
  import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
5
5
  import { Environments } from './environments';
@@ -5,6 +5,7 @@ const node_fs_1 = require("node:fs");
5
5
  const node_http_1 = require("node:http");
6
6
  const node_https_1 = require("node:https");
7
7
  const node_path_1 = require("node:path");
8
+ const node_stream_1 = require("node:stream");
8
9
  const websocket_connection_1 = require("@celljs/rpc/lib/node/websocket/websocket-connection");
9
10
  const access_store_1 = require("@celljs/ta-host/lib/node/sign-in/access-store");
10
11
  const sign_in_flow_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-flow");
@@ -231,11 +232,11 @@ class GatewayServer {
231
232
  const outgoing = send(target, { method: request.method, headers }, incoming => {
232
233
  var _a;
233
234
  response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, incoming.headers);
234
- incoming.pipe(response);
235
- incoming.on('end', () => resolve());
236
- incoming.on('error', reject);
235
+ // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
236
+ (0, node_stream_1.pipeline)(incoming, response, () => resolve());
237
237
  });
238
238
  outgoing.on('error', reject);
239
+ response.once('close', () => outgoing.destroy());
239
240
  outgoing.end();
240
241
  });
241
242
  }
@@ -255,7 +256,11 @@ class GatewayServer {
255
256
  response.end();
256
257
  return;
257
258
  }
258
- (0, node_fs_1.createReadStream)(file).pipe(response);
259
+ // A file gone meanwhile (the web client being replaced) fails this request, not the gateway.
260
+ const stream = (0, node_fs_1.createReadStream)(file);
261
+ stream.once('error', () => (response.headersSent ? response.destroy() : sign_in_pages_1.SignInPageUtil.end(response, 404)));
262
+ response.once('close', () => stream.destroy());
263
+ stream.pipe(response);
259
264
  }
260
265
  upgrade(request, socket, head) {
261
266
  var _a, _b;
@@ -18,6 +18,8 @@ export declare namespace GatewayServiceUtil {
18
18
  /** The services the gateway answers itself, not the Ta of the tenant. */
19
19
  const SERVICES: string[];
20
20
  function owns(method: unknown): method is string;
21
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
22
+ function mayOwn(text: string): boolean;
21
23
  /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
22
24
  function error(id: unknown, message: string, code: TaErrorCode, hint: string): string;
23
25
  function result(id: unknown, result: unknown): string;
@@ -20,6 +20,11 @@ var GatewayServiceUtil;
20
20
  return typeof method === 'string' && GatewayServiceUtil.SERVICES.includes(method.split('/')[0]);
21
21
  }
22
22
  GatewayServiceUtil.owns = owns;
23
+ /** Whether the text of a message may be a call of {@link SERVICES}: those that are not are not parsed. */
24
+ function mayOwn(text) {
25
+ return GatewayServiceUtil.SERVICES.some(service => text.includes(`"${service}/`));
26
+ }
27
+ GatewayServiceUtil.mayOwn = mayOwn;
23
28
  /** The error of a JSON-RPC answer, as ta serve sends an ApplicationError: the client reads its code and hint. */
24
29
  function error(id, message, code, hint) {
25
30
  return JSON.stringify({ jsonrpc: '2.0', id, error: { code: ta_protocol_1.TA_RPC_ERROR_CODE, message, data: { kind: 'application', data: { code, hint } } } });
@@ -25,6 +25,7 @@ export declare class GatewaySocket {
25
25
  close(code: number, reason: string): void;
26
26
  protected open(): Promise<void>;
27
27
  protected fromBrowser(text: string): void;
28
+ protected forward(text: string): void;
28
29
  protected answer(id: unknown, method: string, params: unknown[]): Promise<void>;
29
30
  /** The calls that wait, and those to come, fail; those of the gateway still work. */
30
31
  protected fail(reason: string): void;
@@ -71,12 +71,16 @@ class GatewaySocket {
71
71
  };
72
72
  }
73
73
  fromBrowser(text) {
74
- var _a;
74
+ // Most messages are the Ta's, some large (pictures): forwarded without being read.
75
+ if (this.failure === undefined && !gateway_service_1.GatewayServiceUtil.mayOwn(text)) {
76
+ this.forward(text);
77
+ return;
78
+ }
75
79
  let message;
76
80
  try {
77
81
  message = JSON.parse(text);
78
82
  }
79
- catch (_b) {
83
+ catch (_a) {
80
84
  this.close(1007, 'Not JSON.');
81
85
  return;
82
86
  }
@@ -90,6 +94,10 @@ class GatewaySocket {
90
94
  }
91
95
  return;
92
96
  }
97
+ this.forward(text);
98
+ }
99
+ forward(text) {
100
+ var _a;
93
101
  if (((_a = this.inner) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
94
102
  this.inner.send(text);
95
103
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-gateway",
3
- "version": "4.9.0",
3
+ "version": "4.9.1",
4
4
  "description": "ta gateway: one Ta per account, each in a sandbox of its own, behind one sign-in",
5
5
  "homepage": "https://github.com/cellbang/cell",
6
6
  "bugs": {
@@ -29,9 +29,9 @@
29
29
  "watch": "tsc -b -w --preserveWatchOutput"
30
30
  },
31
31
  "dependencies": {
32
- "@celljs/rpc": "4.9.0",
33
- "@celljs/ta-host": "4.9.0",
34
- "@celljs/ta-protocol": "4.9.0"
32
+ "@celljs/rpc": "4.9.1",
33
+ "@celljs/ta-host": "4.9.1",
34
+ "@celljs/ta-protocol": "4.9.1"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"