@celljs/ta-gateway 4.10.0 → 4.11.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.
@@ -29,6 +29,11 @@ export declare namespace GatewayServerUtil {
29
29
  * path of that address, and of any address a tenant names as its own Ta service.
30
30
  */
31
31
  function filesTarget(requestUrl: string, endpoint: string): URL | undefined;
32
+ /**
33
+ * The headers of an answer of the Ta of a tenant, as the browser gets them: without cookies, which a Ta
34
+ * service of the user's (or what runs in a sandbox) would otherwise set on the gateway's origin.
35
+ */
36
+ function filesHeaders(headers: IncomingMessage['headers']): IncomingMessage['headers'];
32
37
  }
33
38
  /**
34
39
  * `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
@@ -70,6 +70,15 @@ export var GatewayServerUtil;
70
70
  return target.origin === new URL(endpoint).origin && target.pathname.startsWith(`${TA_FILES_PATH}/`) ? target : undefined;
71
71
  }
72
72
  GatewayServerUtil.filesTarget = filesTarget;
73
+ /**
74
+ * The headers of an answer of the Ta of a tenant, as the browser gets them: without cookies, which a Ta
75
+ * service of the user's (or what runs in a sandbox) would otherwise set on the gateway's origin.
76
+ */
77
+ function filesHeaders(headers) {
78
+ const { 'set-cookie': _cookies, ...rest } = headers;
79
+ return rest;
80
+ }
81
+ GatewayServerUtil.filesHeaders = filesHeaders;
73
82
  })(GatewayServerUtil || (GatewayServerUtil = {}));
74
83
  /**
75
84
  * `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
@@ -247,7 +256,7 @@ export class GatewayServer {
247
256
  const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
248
257
  const outgoing = send(target, { method: request.method, headers }, incoming => {
249
258
  var _a;
250
- response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, incoming.headers);
259
+ response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
251
260
  // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
252
261
  pipeline(incoming, response, () => resolve());
253
262
  });
@@ -14,6 +14,7 @@ export declare class GatewaySocket {
14
14
  protected readonly service: GatewayService;
15
15
  protected inner?: WebSocket;
16
16
  protected readonly queued: string[];
17
+ protected queuedBytes: number;
17
18
  protected failure?: string;
18
19
  protected closed: boolean;
19
20
  /** Calls of the gateway being answered: a change of environment stops the one this socket is on meanwhile. */
@@ -2,6 +2,8 @@ import { TA_WEBSOCKET_PATH, TaErrorCode } from '@celljs/ta-protocol';
2
2
  import { GatewayCallError, GatewayServiceUtil } from './gateway-service.js';
3
3
  /** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
4
4
  const MAX_QUEUED = 256;
5
+ /** And as many bytes at most: a message may be 32 MB, and 256 of them would hold the gateway's memory for one browser. */
6
+ const MAX_QUEUED_BYTES = 16 * 1024 * 1024;
5
7
  /**
6
8
  * One browser's WebSocket: the calls of the gateway's services answered here, everything else forwarded to
7
9
  * the Ta of its tenant, whose token only the gateway has, and back. Until that Ta answers, what the browser
@@ -15,6 +17,7 @@ export class GatewaySocket {
15
17
  this.environments = environments;
16
18
  this.service = service;
17
19
  this.queued = [];
20
+ this.queuedBytes = 0;
18
21
  this.closed = false;
19
22
  /** Calls of the gateway being answered: a change of environment stops the one this socket is on meanwhile. */
20
23
  this.answering = 0;
@@ -47,6 +50,7 @@ export class GatewaySocket {
47
50
  const inner = new WebSocket(url);
48
51
  this.inner = inner;
49
52
  inner.onopen = () => {
53
+ this.queuedBytes = 0;
50
54
  for (const text of this.queued.splice(0)) {
51
55
  inner.send(text);
52
56
  }
@@ -98,8 +102,9 @@ export class GatewaySocket {
98
102
  if (((_a = this.inner) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
99
103
  this.inner.send(text);
100
104
  }
101
- else if (this.queued.length < MAX_QUEUED) {
105
+ else if (this.queued.length < MAX_QUEUED && this.queuedBytes + text.length <= MAX_QUEUED_BYTES) {
102
106
  this.queued.push(text);
107
+ this.queuedBytes += text.length;
103
108
  }
104
109
  else {
105
110
  this.close(1013, 'The environment is still starting.');
@@ -126,6 +131,7 @@ export class GatewaySocket {
126
131
  /** The calls that wait, and those to come, fail; those of the gateway still work. */
127
132
  fail(reason) {
128
133
  this.failure = reason;
134
+ this.queuedBytes = 0;
129
135
  for (const text of this.queued.splice(0)) {
130
136
  this.fromBrowser(text);
131
137
  }
@@ -3,6 +3,8 @@ import { mkdirSync } from 'node:fs';
3
3
  import { createServer } from 'node:net';
4
4
  import { join } from 'node:path';
5
5
  import { SandboxProviders } from './sandbox-provider.js';
6
+ /** How long a ta serve has to end on SIGTERM before it is killed. */
7
+ const STOP_TIMEOUT_MS = 10000;
6
8
  /**
7
9
  * `local`: a `ta serve` process per tenant on the machine of the gateway, its agent directory and workspace
8
10
  * under `<gateway>/local/<tenant>`. Not a sandbox: every tenant runs as the user of the gateway and can read
@@ -54,7 +56,12 @@ export class LocalSandbox {
54
56
  this.running.delete(tenant.id);
55
57
  if (running && running.child.exitCode === null) {
56
58
  await new Promise(done => {
57
- running.child.once('exit', () => done());
59
+ // A ta serve that does not end on SIGTERM would hold the stop, and the gateway's close, for good.
60
+ const kill = setTimeout(() => running.child.kill('SIGKILL'), STOP_TIMEOUT_MS);
61
+ running.child.once('exit', () => {
62
+ clearTimeout(kill);
63
+ done();
64
+ });
58
65
  running.child.kill('SIGTERM');
59
66
  });
60
67
  }
@@ -28,7 +28,11 @@ export class SignInLinks {
28
28
  use(secret, now = Date.now()) {
29
29
  const links = this.read(now);
30
30
  const link = links.find(one => one.hash === hash(secret));
31
- writePrivate(this.file, links.filter(one => one !== link));
31
+ // Written only when a link was used: a request with a wrong secret, which anyone can send, writes nothing,
32
+ // and does not race `ta gateway link` writing the file from another process.
33
+ if (link) {
34
+ writePrivate(this.file, links.filter(one => one !== link));
35
+ }
32
36
  return link && { account: link.account, name: link.name };
33
37
  }
34
38
  read(now) {
@@ -29,6 +29,11 @@ export declare namespace GatewayServerUtil {
29
29
  * path of that address, and of any address a tenant names as its own Ta service.
30
30
  */
31
31
  function filesTarget(requestUrl: string, endpoint: string): URL | undefined;
32
+ /**
33
+ * The headers of an answer of the Ta of a tenant, as the browser gets them: without cookies, which a Ta
34
+ * service of the user's (or what runs in a sandbox) would otherwise set on the gateway's origin.
35
+ */
36
+ function filesHeaders(headers: IncomingMessage['headers']): IncomingMessage['headers'];
32
37
  }
33
38
  /**
34
39
  * `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
@@ -73,6 +73,15 @@ var GatewayServerUtil;
73
73
  return target.origin === new URL(endpoint).origin && target.pathname.startsWith(`${ta_protocol_1.TA_FILES_PATH}/`) ? target : undefined;
74
74
  }
75
75
  GatewayServerUtil.filesTarget = filesTarget;
76
+ /**
77
+ * The headers of an answer of the Ta of a tenant, as the browser gets them: without cookies, which a Ta
78
+ * service of the user's (or what runs in a sandbox) would otherwise set on the gateway's origin.
79
+ */
80
+ function filesHeaders(headers) {
81
+ const { 'set-cookie': _cookies, ...rest } = headers;
82
+ return rest;
83
+ }
84
+ GatewayServerUtil.filesHeaders = filesHeaders;
76
85
  })(GatewayServerUtil || (exports.GatewayServerUtil = GatewayServerUtil = {}));
77
86
  /**
78
87
  * `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
@@ -250,7 +259,7 @@ class GatewayServer {
250
259
  const send = target.protocol === 'https:' ? node_https_1.request : node_http_1.request;
251
260
  const outgoing = send(target, { method: request.method, headers }, incoming => {
252
261
  var _a;
253
- response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, incoming.headers);
262
+ response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
254
263
  // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
255
264
  (0, node_stream_1.pipeline)(incoming, response, () => resolve());
256
265
  });
@@ -14,6 +14,7 @@ export declare class GatewaySocket {
14
14
  protected readonly service: GatewayService;
15
15
  protected inner?: WebSocket;
16
16
  protected readonly queued: string[];
17
+ protected queuedBytes: number;
17
18
  protected failure?: string;
18
19
  protected closed: boolean;
19
20
  /** Calls of the gateway being answered: a change of environment stops the one this socket is on meanwhile. */
@@ -5,6 +5,8 @@ const ta_protocol_1 = require("@celljs/ta-protocol");
5
5
  const gateway_service_1 = require("./gateway-service");
6
6
  /** What a browser sends before the Ta of its tenant answers, at most: then it is cut off, to connect again. */
7
7
  const MAX_QUEUED = 256;
8
+ /** And as many bytes at most: a message may be 32 MB, and 256 of them would hold the gateway's memory for one browser. */
9
+ const MAX_QUEUED_BYTES = 16 * 1024 * 1024;
8
10
  /**
9
11
  * One browser's WebSocket: the calls of the gateway's services answered here, everything else forwarded to
10
12
  * the Ta of its tenant, whose token only the gateway has, and back. Until that Ta answers, what the browser
@@ -18,6 +20,7 @@ class GatewaySocket {
18
20
  this.environments = environments;
19
21
  this.service = service;
20
22
  this.queued = [];
23
+ this.queuedBytes = 0;
21
24
  this.closed = false;
22
25
  /** Calls of the gateway being answered: a change of environment stops the one this socket is on meanwhile. */
23
26
  this.answering = 0;
@@ -50,6 +53,7 @@ class GatewaySocket {
50
53
  const inner = new WebSocket(url);
51
54
  this.inner = inner;
52
55
  inner.onopen = () => {
56
+ this.queuedBytes = 0;
53
57
  for (const text of this.queued.splice(0)) {
54
58
  inner.send(text);
55
59
  }
@@ -101,8 +105,9 @@ class GatewaySocket {
101
105
  if (((_a = this.inner) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
102
106
  this.inner.send(text);
103
107
  }
104
- else if (this.queued.length < MAX_QUEUED) {
108
+ else if (this.queued.length < MAX_QUEUED && this.queuedBytes + text.length <= MAX_QUEUED_BYTES) {
105
109
  this.queued.push(text);
110
+ this.queuedBytes += text.length;
106
111
  }
107
112
  else {
108
113
  this.close(1013, 'The environment is still starting.');
@@ -129,6 +134,7 @@ class GatewaySocket {
129
134
  /** The calls that wait, and those to come, fail; those of the gateway still work. */
130
135
  fail(reason) {
131
136
  this.failure = reason;
137
+ this.queuedBytes = 0;
132
138
  for (const text of this.queued.splice(0)) {
133
139
  this.fromBrowser(text);
134
140
  }
@@ -6,6 +6,8 @@ const node_fs_1 = require("node:fs");
6
6
  const node_net_1 = require("node:net");
7
7
  const node_path_1 = require("node:path");
8
8
  const sandbox_provider_1 = require("./sandbox-provider");
9
+ /** How long a ta serve has to end on SIGTERM before it is killed. */
10
+ const STOP_TIMEOUT_MS = 10000;
9
11
  /**
10
12
  * `local`: a `ta serve` process per tenant on the machine of the gateway, its agent directory and workspace
11
13
  * under `<gateway>/local/<tenant>`. Not a sandbox: every tenant runs as the user of the gateway and can read
@@ -57,7 +59,12 @@ class LocalSandbox {
57
59
  this.running.delete(tenant.id);
58
60
  if (running && running.child.exitCode === null) {
59
61
  await new Promise(done => {
60
- running.child.once('exit', () => done());
62
+ // A ta serve that does not end on SIGTERM would hold the stop, and the gateway's close, for good.
63
+ const kill = setTimeout(() => running.child.kill('SIGKILL'), STOP_TIMEOUT_MS);
64
+ running.child.once('exit', () => {
65
+ clearTimeout(kill);
66
+ done();
67
+ });
61
68
  running.child.kill('SIGTERM');
62
69
  });
63
70
  }
@@ -31,7 +31,11 @@ class SignInLinks {
31
31
  use(secret, now = Date.now()) {
32
32
  const links = this.read(now);
33
33
  const link = links.find(one => one.hash === hash(secret));
34
- (0, tenant_store_1.writePrivate)(this.file, links.filter(one => one !== link));
34
+ // Written only when a link was used: a request with a wrong secret, which anyone can send, writes nothing,
35
+ // and does not race `ta gateway link` writing the file from another process.
36
+ if (link) {
37
+ (0, tenant_store_1.writePrivate)(this.file, links.filter(one => one !== link));
38
+ }
35
39
  return link && { account: link.account, name: link.name };
36
40
  }
37
41
  read(now) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-gateway",
3
- "version": "4.10.0",
3
+ "version": "4.11.0",
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.10.0",
33
- "@celljs/ta-host": "4.10.0",
34
- "@celljs/ta-protocol": "4.10.0"
32
+ "@celljs/rpc": "4.11.0",
33
+ "@celljs/ta-host": "4.11.0",
34
+ "@celljs/ta-protocol": "4.11.0"
35
35
  },
36
36
  "publishConfig": {
37
37
  "access": "public"