@celljs/ta-gateway 0.0.0-stage → 4.9.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.
- package/README.en.md +80 -0
- package/README.md +79 -2
- package/esm/node/command-sandbox.d.ts +28 -0
- package/esm/node/command-sandbox.js +83 -0
- package/esm/node/docker-engine.d.ts +56 -0
- package/esm/node/docker-engine.js +151 -0
- package/esm/node/docker-sandbox.d.ts +45 -0
- package/esm/node/docker-sandbox.js +110 -0
- package/esm/node/environments.d.ts +68 -0
- package/esm/node/environments.js +236 -0
- package/esm/node/gateway-cli.d.ts +9 -0
- package/esm/node/gateway-cli.js +99 -0
- package/esm/node/gateway-server.d.ts +67 -0
- package/esm/node/gateway-server.js +283 -0
- package/esm/node/gateway-service.d.ts +56 -0
- package/esm/node/gateway-service.js +182 -0
- package/esm/node/gateway-socket.d.ts +32 -0
- package/esm/node/gateway-socket.js +135 -0
- package/esm/node/index.d.ts +13 -0
- package/esm/node/index.js +14 -0
- package/esm/node/local-sandbox.d.ts +23 -0
- package/esm/node/local-sandbox.js +73 -0
- package/esm/node/sandbox-provider.d.ts +72 -0
- package/esm/node/sandbox-provider.js +83 -0
- package/esm/node/sign-in-links.d.ts +25 -0
- package/esm/node/sign-in-links.js +44 -0
- package/esm/node/ta-sandbox.d.ts +23 -0
- package/esm/node/ta-sandbox.js +47 -0
- package/esm/node/tenant-store.d.ts +36 -0
- package/esm/node/tenant-store.js +83 -0
- package/esm/package.json +3 -0
- package/lib/node/command-sandbox.d.ts +29 -0
- package/lib/node/command-sandbox.js +87 -0
- package/lib/node/docker-engine.d.ts +57 -0
- package/lib/node/docker-engine.js +156 -0
- package/lib/node/docker-sandbox.d.ts +46 -0
- package/lib/node/docker-sandbox.js +114 -0
- package/lib/node/environments.d.ts +69 -0
- package/lib/node/environments.js +241 -0
- package/lib/node/gateway-cli.d.ts +10 -0
- package/lib/node/gateway-cli.js +102 -0
- package/lib/node/gateway-server.d.ts +68 -0
- package/lib/node/gateway-server.js +287 -0
- package/lib/node/gateway-service.d.ts +57 -0
- package/lib/node/gateway-service.js +187 -0
- package/lib/node/gateway-socket.d.ts +33 -0
- package/lib/node/gateway-socket.js +139 -0
- package/lib/node/index.d.ts +14 -0
- package/lib/node/index.js +30 -0
- package/lib/node/local-sandbox.d.ts +24 -0
- package/lib/node/local-sandbox.js +77 -0
- package/lib/node/sandbox-provider.d.ts +73 -0
- package/lib/node/sandbox-provider.js +87 -0
- package/lib/node/sign-in-links.d.ts +26 -0
- package/lib/node/sign-in-links.js +48 -0
- package/lib/node/ta-sandbox.d.ts +24 -0
- package/lib/node/ta-sandbox.js +51 -0
- package/lib/node/tenant-store.d.ts +37 -0
- package/lib/node/tenant-store.js +88 -0
- package/package.json +57 -4
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Environments = exports.EnvironmentsUtil = exports.EnvironmentError = void 0;
|
|
4
|
+
const ta_protocol_1 = require("@celljs/ta-protocol");
|
|
5
|
+
const ta_sandbox_1 = require("./ta-sandbox");
|
|
6
|
+
/** How long a sandbox has to answer once started: the first start of a container may pull its image first. */
|
|
7
|
+
const READY_TIMEOUT_MS = 120000;
|
|
8
|
+
class EnvironmentError extends Error {
|
|
9
|
+
}
|
|
10
|
+
exports.EnvironmentError = EnvironmentError;
|
|
11
|
+
var EnvironmentsUtil;
|
|
12
|
+
(function (EnvironmentsUtil) {
|
|
13
|
+
/** Whether a Ta service answers at `url`, whatever it answers. */
|
|
14
|
+
async function waitReady(url, timeoutMs = READY_TIMEOUT_MS, fetcher = fetch) {
|
|
15
|
+
var _a;
|
|
16
|
+
const until = Date.now() + timeoutMs;
|
|
17
|
+
let last;
|
|
18
|
+
while (Date.now() < until) {
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetcher(`${url}/`, { method: 'HEAD', signal: AbortSignal.timeout(5000) });
|
|
21
|
+
await ((_a = response.body) === null || _a === void 0 ? void 0 : _a.cancel());
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
last = error;
|
|
26
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
throw new EnvironmentError(`Nothing answered at ${url} within ${Math.round(timeoutMs / 1000)} seconds${last instanceof Error ? `: ${last.message}` : ''}.`);
|
|
30
|
+
}
|
|
31
|
+
EnvironmentsUtil.waitReady = waitReady;
|
|
32
|
+
/** One call of the Ta service at `endpoint` over a WebSocket of its own: the gateway's, not a browser's. */
|
|
33
|
+
function call(endpoint, method, params = [], timeoutMs = 10000) {
|
|
34
|
+
return new Promise((resolve, reject) => {
|
|
35
|
+
const url = new URL(`${endpoint.url.replace(/^http/, 'ws')}${ta_protocol_1.TA_WEBSOCKET_PATH}`);
|
|
36
|
+
url.searchParams.set('token', endpoint.token);
|
|
37
|
+
const socket = new WebSocket(url);
|
|
38
|
+
const timer = setTimeout(() => finish(new EnvironmentError(`${method} took too long`)), timeoutMs);
|
|
39
|
+
const finish = (error, result) => {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
socket.close();
|
|
42
|
+
if (error) {
|
|
43
|
+
reject(error);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
resolve(result);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
socket.onopen = () => socket.send(JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }));
|
|
50
|
+
socket.onerror = () => finish(new EnvironmentError(`Cannot reach ${endpoint.url}`));
|
|
51
|
+
socket.onmessage = message => {
|
|
52
|
+
const answer = JSON.parse(String(message.data));
|
|
53
|
+
if (answer.id === 1) {
|
|
54
|
+
finish(answer.error ? new EnvironmentError(answer.error.message) : undefined, answer.result);
|
|
55
|
+
}
|
|
56
|
+
else if (answer.method !== undefined && answer.id !== undefined) {
|
|
57
|
+
// A push of the server (TaClient): answered, so that it does not wait.
|
|
58
|
+
socket.send(JSON.stringify({ jsonrpc: '2.0', id: answer.id, result: null }));
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
EnvironmentsUtil.call = call;
|
|
64
|
+
function sandboxTenant(tenant) {
|
|
65
|
+
return { id: tenant.id, account: tenant.account, token: tenant.token };
|
|
66
|
+
}
|
|
67
|
+
EnvironmentsUtil.sandboxTenant = sandboxTenant;
|
|
68
|
+
})(EnvironmentsUtil || (exports.EnvironmentsUtil = EnvironmentsUtil = {}));
|
|
69
|
+
/**
|
|
70
|
+
* The environments of the tenants: which provider runs each, starting it on its first connection (once,
|
|
71
|
+
* whoever asks meanwhile), and stopping it when nobody has been connected for `idleMinutes`.
|
|
72
|
+
*/
|
|
73
|
+
class Environments {
|
|
74
|
+
constructor(providers, tenants, settings) {
|
|
75
|
+
this.providers = providers;
|
|
76
|
+
this.tenants = tenants;
|
|
77
|
+
this.settings = settings;
|
|
78
|
+
this.running = new Map();
|
|
79
|
+
}
|
|
80
|
+
/** The providers one may pick. */
|
|
81
|
+
choices() {
|
|
82
|
+
return this.providers.map(({ id, name, type }) => ({ id, name, type }));
|
|
83
|
+
}
|
|
84
|
+
/** The provider of a tenant: its own Ta service, the one it picked, or the default. */
|
|
85
|
+
provider(tenant) {
|
|
86
|
+
var _a;
|
|
87
|
+
var _b, _c, _d, _e;
|
|
88
|
+
const choice = tenant.environment;
|
|
89
|
+
if (choice && 'type' in choice) {
|
|
90
|
+
return {
|
|
91
|
+
provider: new ta_sandbox_1.TaSandbox('own', { type: 'ta', url: choice.url, token: choice.token, name: (_b = choice.name) !== null && _b !== void 0 ? _b : new URL(choice.url).host }),
|
|
92
|
+
own: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const id = (_d = (_c = choice === null || choice === void 0 ? void 0 : choice.provider) !== null && _c !== void 0 ? _c : this.settings.default) !== null && _d !== void 0 ? _d : (_a = this.providers[0]) === null || _a === void 0 ? void 0 : _a.id;
|
|
96
|
+
const provider = (_e = this.providers.find(one => one.id === id)) !== null && _e !== void 0 ? _e : this.providers.find(one => one.id === this.settings.default);
|
|
97
|
+
if (!provider) {
|
|
98
|
+
throw new EnvironmentError('The gateway has no provider of sandboxes: add one to sandboxes.json, or use a Ta service of your own.');
|
|
99
|
+
}
|
|
100
|
+
return { provider, own: false };
|
|
101
|
+
}
|
|
102
|
+
info(tenant) {
|
|
103
|
+
var _a;
|
|
104
|
+
const running = this.running.get(tenant.id);
|
|
105
|
+
let picked;
|
|
106
|
+
try {
|
|
107
|
+
picked = this.provider(tenant);
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
return { provider: '', name: '', status: 'failed', error: error.message };
|
|
111
|
+
}
|
|
112
|
+
const { provider, own } = picked;
|
|
113
|
+
return {
|
|
114
|
+
provider: own ? 'own' : provider.id,
|
|
115
|
+
name: provider.name,
|
|
116
|
+
status: (_a = running === null || running === void 0 ? void 0 : running.status) !== null && _a !== void 0 ? _a : 'stopped',
|
|
117
|
+
error: running === null || running === void 0 ? void 0 : running.error,
|
|
118
|
+
url: own && provider instanceof ta_sandbox_1.TaSandbox ? provider.url : undefined,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
connections(tenantId) {
|
|
122
|
+
var _a;
|
|
123
|
+
var _b;
|
|
124
|
+
return (_b = (_a = this.running.get(tenantId)) === null || _a === void 0 ? void 0 : _a.connections) !== null && _b !== void 0 ? _b : 0;
|
|
125
|
+
}
|
|
126
|
+
/** Where the Ta of the tenant answers, started when it does not run. */
|
|
127
|
+
ensure(tenant) {
|
|
128
|
+
const running = this.state(tenant.id);
|
|
129
|
+
if (running.status === 'running' && running.endpoint) {
|
|
130
|
+
return Promise.resolve(running.endpoint);
|
|
131
|
+
}
|
|
132
|
+
if (running.starting) {
|
|
133
|
+
return running.starting;
|
|
134
|
+
}
|
|
135
|
+
running.status = 'starting';
|
|
136
|
+
running.error = undefined;
|
|
137
|
+
const starting = (async () => {
|
|
138
|
+
var _a;
|
|
139
|
+
const { provider } = this.provider(tenant);
|
|
140
|
+
const endpoint = await provider.start(EnvironmentsUtil.sandboxTenant(tenant));
|
|
141
|
+
await EnvironmentsUtil.waitReady(endpoint.url, (_a = this.settings.readyTimeoutMs) !== null && _a !== void 0 ? _a : provider.readyTimeoutMs);
|
|
142
|
+
return endpoint;
|
|
143
|
+
})();
|
|
144
|
+
running.starting = starting;
|
|
145
|
+
starting.then(endpoint => {
|
|
146
|
+
if (running.starting === starting) {
|
|
147
|
+
Object.assign(running, { status: 'running', endpoint, starting: undefined });
|
|
148
|
+
}
|
|
149
|
+
}, (error) => {
|
|
150
|
+
if (running.starting === starting) {
|
|
151
|
+
Object.assign(running, { status: 'failed', error: error.message, endpoint: undefined, starting: undefined });
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return starting;
|
|
155
|
+
}
|
|
156
|
+
/** The sandbox of the tenant answered no more (its connection dropped): looked for again on the next. */
|
|
157
|
+
lost(tenantId) {
|
|
158
|
+
const running = this.running.get(tenantId);
|
|
159
|
+
if (running && !running.starting) {
|
|
160
|
+
Object.assign(running, { status: 'stopped', endpoint: undefined });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
connect(tenantId) {
|
|
164
|
+
const running = this.state(tenantId);
|
|
165
|
+
running.connections++;
|
|
166
|
+
clearTimeout(running.idle);
|
|
167
|
+
let done = false;
|
|
168
|
+
return () => {
|
|
169
|
+
var _a, _b;
|
|
170
|
+
if (done) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
done = true;
|
|
174
|
+
running.connections--;
|
|
175
|
+
const minutes = this.settings.idleMinutes;
|
|
176
|
+
if (running.connections === 0 && minutes > 0) {
|
|
177
|
+
running.idle = setTimeout(() => void this.stop(tenantId).catch(() => undefined), minutes * 60000);
|
|
178
|
+
(_b = (_a = running.idle).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** Its connections are the caller's to close: they would only find the sandbox gone. */
|
|
183
|
+
async stop(tenantId) {
|
|
184
|
+
var _a;
|
|
185
|
+
const tenant = this.tenants.get(tenantId);
|
|
186
|
+
if (!tenant) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
const running = this.state(tenantId);
|
|
190
|
+
await ((_a = running.starting) === null || _a === void 0 ? void 0 : _a.catch(() => undefined));
|
|
191
|
+
Object.assign(running, { status: 'stopped', endpoint: undefined, error: undefined });
|
|
192
|
+
await this.provider(tenant).provider.stop(EnvironmentsUtil.sandboxTenant(tenant));
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* The account of the tenant is no longer let in: the devices paired with its Ta (`ta pair`, which reach it
|
|
196
|
+
* through a relay, not the gateway) are unpaired, and its sandbox is stopped. A Ta service of the user's
|
|
197
|
+
* is theirs: left as it is.
|
|
198
|
+
*/
|
|
199
|
+
async revoke(tenant) {
|
|
200
|
+
const { provider, own } = this.provider(tenant);
|
|
201
|
+
if (own) {
|
|
202
|
+
return 0;
|
|
203
|
+
}
|
|
204
|
+
let unpaired = 0;
|
|
205
|
+
if ((await provider.state(EnvironmentsUtil.sandboxTenant(tenant))) === 'running') {
|
|
206
|
+
const endpoint = await provider.start(EnvironmentsUtil.sandboxTenant(tenant));
|
|
207
|
+
const info = await EnvironmentsUtil.call(endpoint, 'TaRelayServer/getRelay');
|
|
208
|
+
for (const device of info.devices) {
|
|
209
|
+
await EnvironmentsUtil.call(endpoint, 'TaRelayServer/unpair', [device.id]);
|
|
210
|
+
unpaired++;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
await this.stop(tenant.id);
|
|
214
|
+
return unpaired;
|
|
215
|
+
}
|
|
216
|
+
/** Another environment for the tenant: the one before is stopped (a service of the user's is left as it is); its connections are the caller's to close. */
|
|
217
|
+
async change(tenant, choice) {
|
|
218
|
+
await this.stop(tenant.id).catch(() => undefined);
|
|
219
|
+
const next = this.tenants.update(tenant.id, { environment: choice });
|
|
220
|
+
// What is picked must be there: an id of none throws here, not on the next start.
|
|
221
|
+
this.provider(next);
|
|
222
|
+
this.running.delete(tenant.id);
|
|
223
|
+
return next;
|
|
224
|
+
}
|
|
225
|
+
async dispose() {
|
|
226
|
+
for (const running of this.running.values()) {
|
|
227
|
+
clearTimeout(running.idle);
|
|
228
|
+
}
|
|
229
|
+
await Promise.all(this.providers.map(provider => { var _a; return (_a = provider.dispose) === null || _a === void 0 ? void 0 : _a.call(provider); }));
|
|
230
|
+
}
|
|
231
|
+
state(tenantId) {
|
|
232
|
+
let running = this.running.get(tenantId);
|
|
233
|
+
if (!running) {
|
|
234
|
+
running = { status: 'stopped', connections: 0 };
|
|
235
|
+
this.running.set(tenantId, running);
|
|
236
|
+
}
|
|
237
|
+
return running;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.Environments = Environments;
|
|
241
|
+
//# sourceMappingURL=environments.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface GatewayCliOptions {
|
|
2
|
+
/** The web client: dist/public of @celljs/ta. */
|
|
3
|
+
publicDir: string;
|
|
4
|
+
/** How to run `ta` (the local sandboxes). */
|
|
5
|
+
taCommand: string[];
|
|
6
|
+
}
|
|
7
|
+
export declare namespace GatewayCli {
|
|
8
|
+
function run(argv: string[], options: GatewayCliOptions): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=gateway-cli.d.ts.map
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GatewayCli = void 0;
|
|
4
|
+
const node_os_1 = require("node:os");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const node_util_1 = require("node:util");
|
|
7
|
+
const gateway_server_1 = require("./gateway-server");
|
|
8
|
+
const sandbox_provider_1 = require("./sandbox-provider");
|
|
9
|
+
const sign_in_links_1 = require("./sign-in-links");
|
|
10
|
+
const HELP = `Usage: ta gateway [--host <host>] [--port <port>] [--dir <dir>]
|
|
11
|
+
ta gateway link <account> [--name <name>] [--dir <dir>]
|
|
12
|
+
|
|
13
|
+
One sign-in for many people, each with a Ta of their own: an account that signs in is a tenant, whose Ta
|
|
14
|
+
runs in a sandbox of its own (a container per tenant by default), or in a Ta service of the user's. The web
|
|
15
|
+
client is served here; each browser's connection goes to the Ta of its tenant. Stops on SIGINT or SIGTERM.
|
|
16
|
+
|
|
17
|
+
--host Address to listen on. Default: 127.0.0.1
|
|
18
|
+
--port Port to listen on. Default: 4100, or PORT
|
|
19
|
+
--dir Its configuration and data. Default: TA_GATEWAY_DIR, else ~/.ta/gateway
|
|
20
|
+
|
|
21
|
+
Accounts sign in with the providers of the environment (TA_GITHUB_*, TA_GOOGLE_*, TA_WECHAT_*, TA_OIDC_*, as
|
|
22
|
+
for ta serve) when TA_ALLOWED_USERS lets them in; TA_GATEWAY_ADMINS are let in too and manage the others.
|
|
23
|
+
"ta gateway link <account>" prints a link that signs in as the account once, within a day, and lets it in:
|
|
24
|
+
for someone without a provider of accounts (user:alice), or before one is set up.
|
|
25
|
+
|
|
26
|
+
The sandboxes are those of sandboxes.json in the directory, as pi's models.json for models:
|
|
27
|
+
{ "default": "docker", "providers": { "docker": { "type": "docker", "image": "ta:latest", "memory": "4g" } } }
|
|
28
|
+
Types: docker, command (shell commands for any platform), local (no isolation: tests), ta (a Ta service that
|
|
29
|
+
runs), module (a file whose default export makes a provider). See the README of @celljs/ta-gateway.
|
|
30
|
+
`;
|
|
31
|
+
var GatewayCli;
|
|
32
|
+
(function (GatewayCli) {
|
|
33
|
+
async function run(argv, options) {
|
|
34
|
+
var _a, _b, _c, _d;
|
|
35
|
+
const { values, positionals } = (0, node_util_1.parseArgs)({
|
|
36
|
+
args: argv,
|
|
37
|
+
allowPositionals: true,
|
|
38
|
+
options: {
|
|
39
|
+
host: { type: 'string' },
|
|
40
|
+
port: { type: 'string' },
|
|
41
|
+
dir: { type: 'string' },
|
|
42
|
+
name: { type: 'string' },
|
|
43
|
+
help: { type: 'boolean', short: 'h' },
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
if (values.help) {
|
|
47
|
+
process.stdout.write(HELP);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const dir = (_b = (_a = values.dir) !== null && _a !== void 0 ? _a : process.env.TA_GATEWAY_DIR) !== null && _b !== void 0 ? _b : (0, node_path_1.join)((0, node_os_1.homedir)(), '.ta', 'gateway');
|
|
51
|
+
if (positionals[0] === 'link') {
|
|
52
|
+
link(dir, positionals[1], values.name);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (positionals.length > 0) {
|
|
56
|
+
process.stdout.write(HELP);
|
|
57
|
+
process.exitCode = 1;
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
let gateway;
|
|
61
|
+
try {
|
|
62
|
+
gateway = await gateway_server_1.GatewayServer.create({
|
|
63
|
+
dir,
|
|
64
|
+
host: values.host,
|
|
65
|
+
port: Number((_d = (_c = values.port) !== null && _c !== void 0 ? _c : process.env.PORT) !== null && _d !== void 0 ? _d : 4100),
|
|
66
|
+
publicDir: options.publicDir,
|
|
67
|
+
taCommand: options.taCommand,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error(`ta gateway: ${error.message}`);
|
|
72
|
+
process.exitCode = 1;
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const url = await gateway.listen();
|
|
76
|
+
const ways = gateway.flow.providers.map(provider => provider.name);
|
|
77
|
+
console.log(`ta gateway: ${url} (${dir})`);
|
|
78
|
+
console.log(`ta gateway: sandboxes of ${gateway.environments.providers.map(provider => `${provider.id} (${provider.type})`).join(', ') || 'none'}${ways.length ? `; sign-in with ${ways.join(', ')}` : '; no provider of accounts: sign people in with ta gateway link <account>'}`);
|
|
79
|
+
const stop = () => void gateway.close().then(() => process.exit(0));
|
|
80
|
+
process.once('SIGINT', stop);
|
|
81
|
+
process.once('SIGTERM', stop);
|
|
82
|
+
}
|
|
83
|
+
GatewayCli.run = run;
|
|
84
|
+
function link(dir, account, name) {
|
|
85
|
+
if (!account) {
|
|
86
|
+
console.error('ta gateway link: name the account, such as github:octocat or user:alice.');
|
|
87
|
+
process.exitCode = 1;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const path = new sign_in_links_1.SignInLinks(dir).create(account, name);
|
|
92
|
+
const base = sandbox_provider_1.SandboxProviders.publicBase(process.env);
|
|
93
|
+
console.log('Open this link to sign in as the account, once, within a day. Anyone with it signs in as it: send it to its owner only.\n');
|
|
94
|
+
console.log(base ? `${base}${path}` : `<address of the gateway>${path}`);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.error(`ta gateway link: ${error.message}`);
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
})(GatewayCli || (exports.GatewayCli = GatewayCli = {}));
|
|
102
|
+
//# sourceMappingURL=gateway-cli.js.map
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
|
|
2
|
+
import type { Duplex } from 'node:stream';
|
|
3
|
+
import { SignInFlow } from '@celljs/ta-host/lib/node/sign-in/sign-in-flow';
|
|
4
|
+
import { type SignInAccount } from '@celljs/ta-host/lib/node/sign-in/sign-in-provider';
|
|
5
|
+
import { Environments } from './environments';
|
|
6
|
+
import { type GatewayCaller, GatewayService } from './gateway-service';
|
|
7
|
+
import { GatewaySocket } from './gateway-socket';
|
|
8
|
+
import { type SandboxProviderFactory, SandboxProviders } from './sandbox-provider';
|
|
9
|
+
import { SignInLinks } from './sign-in-links';
|
|
10
|
+
import { TenantStore } from './tenant-store';
|
|
11
|
+
/** The built-in types of providers of sandboxes. */
|
|
12
|
+
export declare const SANDBOX_TYPES: Record<string, SandboxProviderFactory>;
|
|
13
|
+
export interface GatewayServerOptions {
|
|
14
|
+
/** Its configuration and data: sandboxes.json, access.json, tenants/, links.json. */
|
|
15
|
+
dir: string;
|
|
16
|
+
host?: string;
|
|
17
|
+
port?: number;
|
|
18
|
+
/** The web client: dist/public of @celljs/ta. */
|
|
19
|
+
publicDir: string;
|
|
20
|
+
taCommand: string[];
|
|
21
|
+
env?: Record<string, string | undefined>;
|
|
22
|
+
}
|
|
23
|
+
export declare namespace GatewayServerUtil {
|
|
24
|
+
/** A file of `root` for a path of the page, never outside it; the page for what is not a file. */
|
|
25
|
+
function file(root: string, urlPath: string): string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* `ta gateway`: one sign-in for many people, each with a Ta of their own. An account that signs in is a
|
|
29
|
+
* tenant (GitHub, Google, WeChat, OpenID Connect, or a link of `ta gateway link`); its Ta runs in a sandbox
|
|
30
|
+
* of its own, made by a provider of sandboxes.json, or in a Ta service of the user's. The gateway serves the
|
|
31
|
+
* web client, forwards each browser's WebSocket to the Ta of its tenant, and answers TaGatewayServer and
|
|
32
|
+
* TaAccessServer itself.
|
|
33
|
+
*/
|
|
34
|
+
export declare class GatewayServer {
|
|
35
|
+
protected readonly options: GatewayServerOptions;
|
|
36
|
+
readonly flow: SignInFlow;
|
|
37
|
+
readonly tenants: TenantStore;
|
|
38
|
+
readonly environments: Environments;
|
|
39
|
+
readonly links: SignInLinks;
|
|
40
|
+
protected readonly service: GatewayService;
|
|
41
|
+
protected readonly sockets: Set<GatewaySocket>;
|
|
42
|
+
protected readonly admins: string[];
|
|
43
|
+
protected server?: Server;
|
|
44
|
+
static create(options: GatewayServerOptions): Promise<GatewayServer>;
|
|
45
|
+
protected constructor(options: GatewayServerOptions, providers: Awaited<ReturnType<typeof SandboxProviders.create>>, config: ReturnType<typeof SandboxProviders.read>);
|
|
46
|
+
isAdmin(account: string): boolean;
|
|
47
|
+
listen(): Promise<string>;
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
/** The browsers whose sign-in no longer works are cut off. */
|
|
50
|
+
cutOff(): void;
|
|
51
|
+
/**
|
|
52
|
+
* The connections of the tenant of now close, and connect again to its environment: in a moment, so that
|
|
53
|
+
* the answer to the call that changed it goes first. Those that open meanwhile stay.
|
|
54
|
+
*/
|
|
55
|
+
reset(tenantId: string): void;
|
|
56
|
+
/** Who a token signs in, while its account is let in. */
|
|
57
|
+
caller(token: string): GatewayCaller | undefined;
|
|
58
|
+
protected handle(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
59
|
+
/** A link of `ta gateway link`: lets its account in, and signs this browser in as it. */
|
|
60
|
+
protected useLink(request: IncomingMessage, response: ServerResponse, secret: string): void;
|
|
61
|
+
protected signedIn(response: ServerResponse, token: string, account: SignInAccount, origin: string): void;
|
|
62
|
+
protected origin(request: IncomingMessage): string;
|
|
63
|
+
/** `/api/files` of the Ta of the tenant whose cookie came along. */
|
|
64
|
+
protected files(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
65
|
+
protected page(request: IncomingMessage, response: ServerResponse, path: string): void;
|
|
66
|
+
protected upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=gateway-server.d.ts.map
|