@celljs/ta-gateway 4.18.0 → 4.19.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.
@@ -0,0 +1,6 @@
1
+ import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'node:http';
2
+ /** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
3
+ export declare namespace GatewayFiles {
4
+ /** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
5
+ function forward(request: IncomingMessage, response: ServerResponse, target: URL, answered: (headers: IncomingHttpHeaders) => IncomingHttpHeaders): Promise<void>;
6
+ }
@@ -0,0 +1,33 @@
1
+ import { request as httpRequest } from 'node:http';
2
+ import { request as httpsRequest } from 'node:https';
3
+ import { pipeline } from 'node:stream';
4
+ /** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
5
+ export var GatewayFiles;
6
+ (function (GatewayFiles) {
7
+ /** What of a read the Ta of the tenant is told: its range and what the browser takes, nothing of who asked. */
8
+ const PASSED = ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent'];
9
+ /** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
10
+ function forward(request, response, target, answered) {
11
+ const headers = {};
12
+ for (const name of PASSED) {
13
+ const value = request.headers[name];
14
+ if (value !== undefined) {
15
+ headers[name] = value;
16
+ }
17
+ }
18
+ return new Promise((resolve, reject) => {
19
+ const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
20
+ const outgoing = send(target, { method: request.method, headers }, incoming => {
21
+ var _a;
22
+ response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, answered(incoming.headers));
23
+ // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
24
+ pipeline(incoming, response, () => resolve());
25
+ });
26
+ outgoing.on('error', reject);
27
+ response.once('close', () => outgoing.destroy());
28
+ outgoing.end();
29
+ });
30
+ }
31
+ GatewayFiles.forward = forward;
32
+ })(GatewayFiles || (GatewayFiles = {}));
33
+ //# sourceMappingURL=gateway-files.js.map
@@ -1,9 +1,10 @@
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 { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
4
4
  import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
5
5
  import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
6
6
  import { type TaQuotaBlock } from '@celljs/ta-protocol';
7
+ import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
7
8
  import { type EgressAuthorization, EgressProxy } from './egress-proxy.js';
8
9
  import { Environments } from './environments.js';
9
10
  import { GatewayAudit } from './gateway-audit.js';
@@ -37,6 +38,11 @@ export interface GatewayServerOptions {
37
38
  version?: string;
38
39
  }
39
40
  export declare namespace GatewayServerUtil {
41
+ /** The account of a caller, for the account service (docs/design/ta-account.md 3). */
42
+ function accountOf(caller: Pick<GatewayCaller, 'account' | 'tenant'> | undefined): {
43
+ id: string;
44
+ name: string;
45
+ } | undefined;
40
46
  /**
41
47
  * Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
42
48
  * mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
@@ -76,6 +82,7 @@ export declare class GatewayServer {
76
82
  protected readonly options: GatewayServerOptions;
77
83
  readonly flow: SignInFlow;
78
84
  readonly tenants: TenantStore;
85
+ protected readonly accounts: RelayServerAccounts;
79
86
  readonly environments: Environments;
80
87
  readonly links: SignInLinks;
81
88
  protected readonly service: GatewayService;
@@ -1,20 +1,21 @@
1
1
  import { accessSync, constants, createReadStream, existsSync, mkdirSync, statSync } from 'node:fs';
2
- import { createServer, request as httpRequest } from 'node:http';
3
- import { request as httpsRequest } from 'node:https';
2
+ import { createServer } from 'node:http';
4
3
  import { extname, join, normalize, sep } from 'node:path';
5
- import { pipeline } from 'node:stream';
6
4
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
7
5
  import { AccessStore, AccessUtil } from '@celljs/ta-host/lib/node/sign-in/access-store';
8
6
  import { SignInFlow, SignInUtil } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
9
7
  import { SignInPages, SignInPageUtil } from '@celljs/ta-host/lib/node/sign-in/sign-in-pages';
10
8
  import { SignInProviders } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
11
9
  import { TA_FILES_PATH, TA_WEBSOCKET_PATH, TA_WELL_KNOWN_PATH, TaDeploymentUtil } from '@celljs/ta-protocol';
10
+ import { RELAY_ACCOUNT_PATH } from '@celljs/ta-relay';
11
+ import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
12
12
  import { CommandSandbox } from './command-sandbox.js';
13
13
  import { DockerSandbox } from './docker-sandbox.js';
14
14
  import { EgressProxy, EgressProxyUtil } from './egress-proxy.js';
15
15
  import { Environments } from './environments.js';
16
16
  import { GatewayAudit } from './gateway-audit.js';
17
17
  import { GatewayExecProxy } from './gateway-exec.js';
18
+ import { GatewayFiles } from './gateway-files.js';
18
19
  import { GatewayPolicy } from './gateway-policy.js';
19
20
  import { GatewayQuotas } from './gateway-quotas.js';
20
21
  import { GatewayService } from './gateway-service.js';
@@ -55,6 +56,11 @@ export const SANDBOX_TYPES = {
55
56
  };
56
57
  export var GatewayServerUtil;
57
58
  (function (GatewayServerUtil) {
59
+ /** The account of a caller, for the account service (docs/design/ta-account.md 3). */
60
+ function accountOf(caller) {
61
+ return caller ? { id: caller.account, name: caller.tenant.name || caller.account } : undefined;
62
+ }
63
+ GatewayServerUtil.accountOf = accountOf;
58
64
  /**
59
65
  * Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
60
66
  * mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
@@ -165,6 +171,8 @@ export class GatewayServer {
165
171
  const allowed = [...AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
166
172
  this.flow = new SignInFlow(SignInProviders.fromEnv(env), new AccessStore(join(options.dir, 'access.json'), allowed), SignInProviders.publicUrl(env), SignInProviders.polled(env));
167
173
  this.tenants = new TenantStore(options.dir);
174
+ // The devices of each account (docs/design/ta-account.md 3), its tokens those of this gateway's sign-in.
175
+ this.accounts = new RelayServerAccounts(options.dir, { authenticate: async (token) => GatewayServerUtil.accountOf(this.caller(token)) });
168
176
  this.links = new SignInLinks(options.dir);
169
177
  this.environments = new Environments(providers, this.tenants, {
170
178
  default: config.default,
@@ -349,6 +357,9 @@ export class GatewayServer {
349
357
  onSignedIn: (res, token, account, origin) => this.signedIn(res, token, account, origin),
350
358
  });
351
359
  }
360
+ else if (path.startsWith(RELAY_ACCOUNT_PATH)) {
361
+ this.accounts.serve(request, response);
362
+ }
352
363
  else if (path === ModelProxyUtil.PATH || path.startsWith(`${ModelProxyUtil.PATH}/`)) {
353
364
  await this.models.handle(request, response);
354
365
  }
@@ -459,25 +470,7 @@ export class GatewayServer {
459
470
  SignInPageUtil.end(response, 404);
460
471
  return;
461
472
  }
462
- const headers = {};
463
- for (const name of ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent']) {
464
- const value = request.headers[name];
465
- if (value !== undefined) {
466
- headers[name] = value;
467
- }
468
- }
469
- await new Promise((resolve, reject) => {
470
- const send = target.protocol === 'https:' ? httpsRequest : httpRequest;
471
- const outgoing = send(target, { method: request.method, headers }, incoming => {
472
- var _a;
473
- response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
474
- // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
475
- pipeline(incoming, response, () => resolve());
476
- });
477
- outgoing.on('error', reject);
478
- response.once('close', () => outgoing.destroy());
479
- outgoing.end();
480
- });
473
+ await GatewayFiles.forward(request, response, target, GatewayServerUtil.filesHeaders);
481
474
  }
482
475
  page(request, response, path) {
483
476
  var _a;
@@ -0,0 +1,7 @@
1
+ import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'node:http';
2
+ /** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
3
+ export declare namespace GatewayFiles {
4
+ /** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
5
+ function forward(request: IncomingMessage, response: ServerResponse, target: URL, answered: (headers: IncomingHttpHeaders) => IncomingHttpHeaders): Promise<void>;
6
+ }
7
+ //# sourceMappingURL=gateway-files.d.ts.map
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.GatewayFiles = void 0;
4
+ const node_http_1 = require("node:http");
5
+ const node_https_1 = require("node:https");
6
+ const node_stream_1 = require("node:stream");
7
+ /** `/api/files` of the Ta of a tenant, as the gateway passes a read on to it. */
8
+ var GatewayFiles;
9
+ (function (GatewayFiles) {
10
+ /** What of a read the Ta of the tenant is told: its range and what the browser takes, nothing of who asked. */
11
+ const PASSED = ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent'];
12
+ /** Reads `target` for `request`, its answer to `response` with the headers `answered` lets through. */
13
+ function forward(request, response, target, answered) {
14
+ const headers = {};
15
+ for (const name of PASSED) {
16
+ const value = request.headers[name];
17
+ if (value !== undefined) {
18
+ headers[name] = value;
19
+ }
20
+ }
21
+ return new Promise((resolve, reject) => {
22
+ const send = target.protocol === 'https:' ? node_https_1.request : node_http_1.request;
23
+ const outgoing = send(target, { method: request.method, headers }, incoming => {
24
+ var _a;
25
+ response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, answered(incoming.headers));
26
+ // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
27
+ (0, node_stream_1.pipeline)(incoming, response, () => resolve());
28
+ });
29
+ outgoing.on('error', reject);
30
+ response.once('close', () => outgoing.destroy());
31
+ outgoing.end();
32
+ });
33
+ }
34
+ GatewayFiles.forward = forward;
35
+ })(GatewayFiles || (exports.GatewayFiles = GatewayFiles = {}));
36
+ //# sourceMappingURL=gateway-files.js.map
@@ -1,9 +1,10 @@
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 { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
4
4
  import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
5
5
  import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
6
6
  import { type TaQuotaBlock } from '@celljs/ta-protocol';
7
+ import { RelayServerAccounts } from '@celljs/ta-relay/lib/node';
7
8
  import { type EgressAuthorization, EgressProxy } from './egress-proxy';
8
9
  import { Environments } from './environments';
9
10
  import { GatewayAudit } from './gateway-audit';
@@ -37,6 +38,11 @@ export interface GatewayServerOptions {
37
38
  version?: string;
38
39
  }
39
40
  export declare namespace GatewayServerUtil {
41
+ /** The account of a caller, for the account service (docs/design/ta-account.md 3). */
42
+ function accountOf(caller: Pick<GatewayCaller, 'account' | 'tenant'> | undefined): {
43
+ id: string;
44
+ name: string;
45
+ } | undefined;
40
46
  /**
41
47
  * Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
42
48
  * mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
@@ -76,6 +82,7 @@ export declare class GatewayServer {
76
82
  protected readonly options: GatewayServerOptions;
77
83
  readonly flow: SignInFlow;
78
84
  readonly tenants: TenantStore;
85
+ protected readonly accounts: RelayServerAccounts;
79
86
  readonly environments: Environments;
80
87
  readonly links: SignInLinks;
81
88
  protected readonly service: GatewayService;
@@ -3,21 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.GatewayServer = exports.GatewayServerUtil = exports.SANDBOX_TYPES = void 0;
4
4
  const node_fs_1 = require("node:fs");
5
5
  const node_http_1 = require("node:http");
6
- const node_https_1 = require("node:https");
7
6
  const node_path_1 = require("node:path");
8
- const node_stream_1 = require("node:stream");
9
7
  const websocket_connection_1 = require("@celljs/rpc/lib/node/websocket/websocket-connection");
10
8
  const access_store_1 = require("@celljs/ta-host/lib/node/sign-in/access-store");
11
9
  const sign_in_flow_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-flow");
12
10
  const sign_in_pages_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-pages");
13
11
  const sign_in_provider_1 = require("@celljs/ta-host/lib/node/sign-in/sign-in-provider");
14
12
  const ta_protocol_1 = require("@celljs/ta-protocol");
13
+ const ta_relay_1 = require("@celljs/ta-relay");
14
+ const node_1 = require("@celljs/ta-relay/lib/node");
15
15
  const command_sandbox_1 = require("./command-sandbox");
16
16
  const docker_sandbox_1 = require("./docker-sandbox");
17
17
  const egress_proxy_1 = require("./egress-proxy");
18
18
  const environments_1 = require("./environments");
19
19
  const gateway_audit_1 = require("./gateway-audit");
20
20
  const gateway_exec_1 = require("./gateway-exec");
21
+ const gateway_files_1 = require("./gateway-files");
21
22
  const gateway_policy_1 = require("./gateway-policy");
22
23
  const gateway_quotas_1 = require("./gateway-quotas");
23
24
  const gateway_service_1 = require("./gateway-service");
@@ -58,6 +59,11 @@ exports.SANDBOX_TYPES = {
58
59
  };
59
60
  var GatewayServerUtil;
60
61
  (function (GatewayServerUtil) {
62
+ /** The account of a caller, for the account service (docs/design/ta-account.md 3). */
63
+ function accountOf(caller) {
64
+ return caller ? { id: caller.account, name: caller.tenant.name || caller.account } : undefined;
65
+ }
66
+ GatewayServerUtil.accountOf = accountOf;
61
67
  /**
62
68
  * Where a sandbox reaches the gateway, by the type of its provider: a container the host (host.docker.internal,
63
69
  * mapped on Linux too), a process of this machine 127.0.0.1, another the public address.
@@ -168,6 +174,8 @@ class GatewayServer {
168
174
  const allowed = [...access_store_1.AccessUtil.parseList(env.TA_ALLOWED_USERS), ...owners];
169
175
  this.flow = new sign_in_flow_1.SignInFlow(sign_in_provider_1.SignInProviders.fromEnv(env), new access_store_1.AccessStore((0, node_path_1.join)(options.dir, 'access.json'), allowed), sign_in_provider_1.SignInProviders.publicUrl(env), sign_in_provider_1.SignInProviders.polled(env));
170
176
  this.tenants = new tenant_store_1.TenantStore(options.dir);
177
+ // The devices of each account (docs/design/ta-account.md 3), its tokens those of this gateway's sign-in.
178
+ this.accounts = new node_1.RelayServerAccounts(options.dir, { authenticate: async (token) => GatewayServerUtil.accountOf(this.caller(token)) });
171
179
  this.links = new sign_in_links_1.SignInLinks(options.dir);
172
180
  this.environments = new environments_1.Environments(providers, this.tenants, {
173
181
  default: config.default,
@@ -352,6 +360,9 @@ class GatewayServer {
352
360
  onSignedIn: (res, token, account, origin) => this.signedIn(res, token, account, origin),
353
361
  });
354
362
  }
363
+ else if (path.startsWith(ta_relay_1.RELAY_ACCOUNT_PATH)) {
364
+ this.accounts.serve(request, response);
365
+ }
355
366
  else if (path === model_proxy_1.ModelProxyUtil.PATH || path.startsWith(`${model_proxy_1.ModelProxyUtil.PATH}/`)) {
356
367
  await this.models.handle(request, response);
357
368
  }
@@ -462,25 +473,7 @@ class GatewayServer {
462
473
  sign_in_pages_1.SignInPageUtil.end(response, 404);
463
474
  return;
464
475
  }
465
- const headers = {};
466
- for (const name of ['range', 'if-none-match', 'accept', 'accept-encoding', 'user-agent']) {
467
- const value = request.headers[name];
468
- if (value !== undefined) {
469
- headers[name] = value;
470
- }
471
- }
472
- await new Promise((resolve, reject) => {
473
- const send = target.protocol === 'https:' ? node_https_1.request : node_http_1.request;
474
- const outgoing = send(target, { method: request.method, headers }, incoming => {
475
- var _a;
476
- response.writeHead((_a = incoming.statusCode) !== null && _a !== void 0 ? _a : 502, GatewayServerUtil.filesHeaders(incoming.headers));
477
- // A browser that stops reading (a video seeking) closes the request to the Ta of the tenant too.
478
- (0, node_stream_1.pipeline)(incoming, response, () => resolve());
479
- });
480
- outgoing.on('error', reject);
481
- response.once('close', () => outgoing.destroy());
482
- outgoing.end();
483
- });
476
+ await gateway_files_1.GatewayFiles.forward(request, response, target, GatewayServerUtil.filesHeaders);
484
477
  }
485
478
  page(request, response, path) {
486
479
  var _a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-gateway",
3
- "version": "4.18.0",
3
+ "version": "4.19.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/ta",
6
6
  "bugs": {
@@ -29,9 +29,10 @@
29
29
  "watch": "tsc -b -w --preserveWatchOutput"
30
30
  },
31
31
  "dependencies": {
32
- "@celljs/rpc": "4.18.0",
33
- "@celljs/ta-host": "4.18.0",
34
- "@celljs/ta-protocol": "4.18.0"
32
+ "@celljs/rpc": "4.19.0",
33
+ "@celljs/ta-host": "4.19.0",
34
+ "@celljs/ta-protocol": "4.19.0",
35
+ "@celljs/ta-relay": "4.19.0"
35
36
  },
36
37
  "publishConfig": {
37
38
  "access": "public"