@celljs/ta-relay 4.11.0 → 4.13.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 +26 -1
- package/README.md +26 -1
- package/esm/common/index.d.ts +5 -0
- package/esm/common/index.js +5 -0
- package/esm/common/relay-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -0
- package/esm/common/relay-friends.d.ts +154 -0
- package/esm/common/relay-friends.js +482 -0
- package/esm/common/relay-hub-util.d.ts +9 -0
- package/esm/common/relay-hub-util.js +19 -2
- package/esm/common/relay-hubs-connection.d.ts +110 -0
- package/esm/common/relay-hubs-connection.js +508 -0
- package/esm/common/relay-hubs-protocol.d.ts +174 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +68 -216
- package/esm/common/relay-hubs.js +303 -507
- package/esm/common/relay-identity.d.ts +2 -0
- package/esm/common/relay-info.d.ts +44 -0
- package/esm/common/relay-info.js +94 -0
- package/esm/common/relay-protocol.d.ts +7 -3
- package/esm/common/relay-protocol.js +7 -3
- package/esm/common/relay-room-chat.d.ts +36 -2
- package/esm/common/relay-room-chat.js +32 -7
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +5 -0
- package/esm/guest/guest-i18n.js +10 -0
- package/esm/guest/guest-style.d.ts +1 -1
- package/esm/guest/guest-style.js +11 -4
- package/esm/guest/guest.js +39 -18
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-file-storage.d.ts +32 -0
- package/esm/node/relay-file-storage.js +101 -0
- package/esm/node/relay-host.d.ts +63 -28
- package/esm/node/relay-host.js +145 -52
- package/esm/node/relay-hub-files.d.ts +1 -1
- package/esm/node/relay-server.d.ts +37 -3
- package/esm/node/relay-server.js +193 -37
- package/esm/node/relay-store.d.ts +34 -6
- package/esm/node/relay-store.js +54 -22
- package/esm/node/relays-file.d.ts +139 -0
- package/esm/node/relays-file.js +373 -0
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +5 -0
- package/lib/common/relay-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -0
- package/lib/common/relay-friends.d.ts +155 -0
- package/lib/common/relay-friends.js +486 -0
- package/lib/common/relay-hub-util.d.ts +9 -0
- package/lib/common/relay-hub-util.js +19 -2
- package/lib/common/relay-hubs-connection.d.ts +111 -0
- package/lib/common/relay-hubs-connection.js +513 -0
- package/lib/common/relay-hubs-protocol.d.ts +175 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +68 -216
- package/lib/common/relay-hubs.js +311 -515
- package/lib/common/relay-identity.d.ts +2 -0
- package/lib/common/relay-info.d.ts +45 -0
- package/lib/common/relay-info.js +97 -0
- package/lib/common/relay-protocol.d.ts +7 -3
- package/lib/common/relay-protocol.js +8 -4
- package/lib/common/relay-room-chat.d.ts +36 -2
- package/lib/common/relay-room-chat.js +32 -7
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +5 -0
- package/lib/guest/guest-i18n.js +10 -0
- package/lib/guest/guest-style.d.ts +1 -1
- package/lib/guest/guest-style.js +11 -4
- package/lib/guest/guest.js +39 -18
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-file-storage.d.ts +33 -0
- package/lib/node/relay-file-storage.js +105 -0
- package/lib/node/relay-host.d.ts +63 -28
- package/lib/node/relay-host.js +146 -53
- package/lib/node/relay-hub-files.d.ts +1 -1
- package/lib/node/relay-server.d.ts +37 -3
- package/lib/node/relay-server.js +193 -37
- package/lib/node/relay-store.d.ts +34 -6
- package/lib/node/relay-store.js +54 -22
- package/lib/node/relays-file.d.ts +140 -0
- package/lib/node/relays-file.js +378 -0
- package/package.json +2 -2
- package/viewer/room.html +21 -14
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A feature that goes through a relay, as `use` of relays.json names it (docs/design/ta-relays.md 3): `devices` (phones
|
|
3
|
+
* that reach this computer), `share` (links of extensions and content, their offline copies and pages), `rooms`
|
|
4
|
+
* (rooms, playing together, guests), `sync` (the user's computers). An extension may name a feature of its own for
|
|
5
|
+
* its rooms (TaRoomServer.createRoom's `use`), which goes as `rooms` until relays.json names it.
|
|
6
|
+
*/
|
|
7
|
+
export type RelayFeature = string;
|
|
8
|
+
export declare const RELAY_FEATURES: readonly ['devices', 'share', 'rooms', 'sync'];
|
|
9
|
+
/** The relay that comes with Ta, by its id in relays.json. */
|
|
10
|
+
export declare const OFFICIAL_RELAY = "cellbang";
|
|
11
|
+
/**
|
|
12
|
+
* A service of relays.json: `ta-relay`, a relay of Ta (the official one, a Worker of one's own, `ta relay`), which does
|
|
13
|
+
* all; or `folder`, a directory a cloud drive (iCloud Drive, OneDrive, Dropbox, Syncthing…) keeps the same on the user's
|
|
14
|
+
* computers, which only stores: sync alone can use it (docs/design/ta-relays.md 4.1).
|
|
15
|
+
*/
|
|
16
|
+
export type RelayService = {
|
|
17
|
+
type: 'ta-relay';
|
|
18
|
+
url: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
} | {
|
|
21
|
+
type: 'folder';
|
|
22
|
+
path: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Of a type a package registered (RelayTypes): its options beside the type. */
|
|
26
|
+
| ({
|
|
27
|
+
type: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
} & Record<string, unknown>);
|
|
30
|
+
export type RelayServiceType = string;
|
|
31
|
+
/** What each built-in type can carry: a folder only stores, so only sync goes there. */
|
|
32
|
+
export declare const RELAY_TYPE_FEATURES: Readonly<Record<string, readonly string[] | 'all'>>;
|
|
33
|
+
/** A type a package registered, as relays.json needs it: what it carries, and what it is the same service by. */
|
|
34
|
+
export interface RelayTypeEntry {
|
|
35
|
+
features: readonly string[];
|
|
36
|
+
key(options: Record<string, unknown>): string;
|
|
37
|
+
}
|
|
38
|
+
/** The types packages registered while ta serve runs (TaExtensionRelays.registerType): a WebDAV server, a bucket of S3. */
|
|
39
|
+
export declare namespace RelayTypes {
|
|
40
|
+
const NAME: RegExp;
|
|
41
|
+
function register(type: string, entry: RelayTypeEntry): () => void;
|
|
42
|
+
function get(type: string): RelayTypeEntry | undefined;
|
|
43
|
+
}
|
|
44
|
+
/** A service of a type of a package, its options as relays.json holds them. */
|
|
45
|
+
export interface RelayStoreServiceEntry {
|
|
46
|
+
id: string;
|
|
47
|
+
type: string;
|
|
48
|
+
name?: string;
|
|
49
|
+
options: Record<string, unknown>;
|
|
50
|
+
builtin: false;
|
|
51
|
+
}
|
|
52
|
+
/** relays.json in the agent directory: the relays, and which one each feature uses. Written by hand or by Ta. */
|
|
53
|
+
export interface RelaysConfig {
|
|
54
|
+
relays?: Record<string, RelayService>;
|
|
55
|
+
use?: Record<string, string>;
|
|
56
|
+
/** What a feature not in `use` takes. */
|
|
57
|
+
default?: string;
|
|
58
|
+
}
|
|
59
|
+
export type RelayServiceEntry = (({
|
|
60
|
+
type: 'ta-relay';
|
|
61
|
+
url: string;
|
|
62
|
+
name?: string;
|
|
63
|
+
} | {
|
|
64
|
+
type: 'folder';
|
|
65
|
+
path: string;
|
|
66
|
+
name?: string;
|
|
67
|
+
}) & {
|
|
68
|
+
id: string;
|
|
69
|
+
/** The official relay, there without relays.json naming it. */
|
|
70
|
+
builtin: boolean;
|
|
71
|
+
}) | RelayStoreServiceEntry;
|
|
72
|
+
export declare namespace RelayServiceUtil {
|
|
73
|
+
/** `~` and `~/…` as the home directory, a relative path from it: what a folder of relays.json names on this computer. */
|
|
74
|
+
function expand(path: string): string;
|
|
75
|
+
/** A path under the home directory as `~/…`, the same on the user's other computers. */
|
|
76
|
+
function portable(path: string): string;
|
|
77
|
+
function serves(type: RelayServiceType, feature: string): boolean;
|
|
78
|
+
function isRelay(entry: RelayServiceEntry): entry is Extract<RelayServiceEntry, {
|
|
79
|
+
type: 'ta-relay';
|
|
80
|
+
}>;
|
|
81
|
+
function isFolder(entry: RelayServiceEntry): entry is Extract<RelayServiceEntry, {
|
|
82
|
+
type: 'folder';
|
|
83
|
+
}>;
|
|
84
|
+
/** The features a service carries when not all of them: a folder's, a registered type's (none while it is off). */
|
|
85
|
+
function only(type: RelayServiceType): string[] | undefined;
|
|
86
|
+
/** The folders of cloud drives on this computer, for sync: a `Ta` folder in each, to be made when chosen. */
|
|
87
|
+
function drives(): {
|
|
88
|
+
name: string;
|
|
89
|
+
path: string;
|
|
90
|
+
}[];
|
|
91
|
+
/** Whether a folder is there and Ta may write in it; why not, else. */
|
|
92
|
+
function checkFolder(path: string): string | undefined;
|
|
93
|
+
}
|
|
94
|
+
export declare class RelaysConfigError extends Error {
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* relays.json: read again on every use, as it may be edited while Ta runs. Without it every feature uses the relay
|
|
98
|
+
* of relay.json, or the official one, as before there was this file.
|
|
99
|
+
*/
|
|
100
|
+
export declare class RelaysFile {
|
|
101
|
+
protected readonly agentDir: string;
|
|
102
|
+
static readonly FILE = "relays.json";
|
|
103
|
+
constructor(agentDir: string);
|
|
104
|
+
get file(): string;
|
|
105
|
+
/** The file as it is; throws RelaysConfigError when it is not JSON of relays.json. */
|
|
106
|
+
read(): RelaysConfig;
|
|
107
|
+
/** The file, or nothing when it cannot be read: what a feature uses then is the relay of before. */
|
|
108
|
+
safeRead(): {
|
|
109
|
+
config: RelaysConfig;
|
|
110
|
+
error?: string;
|
|
111
|
+
};
|
|
112
|
+
/** The services, the official one first (unless relays.json gives its id another address). */
|
|
113
|
+
services(config?: RelaysConfig): RelayServiceEntry[];
|
|
114
|
+
/** The service a feature uses: as relays.json says when it can carry it, else a relay of Ta (`fallback`, then the official one). */
|
|
115
|
+
serviceFor(feature: RelayFeature, fallback?: string): RelayServiceEntry;
|
|
116
|
+
/**
|
|
117
|
+
* The id of the service a feature uses: as `use` says, an extension's feature as `rooms`, `sync` as `rooms` too,
|
|
118
|
+
* then `default`; never one that cannot carry the feature (a folder for rooms).
|
|
119
|
+
*/
|
|
120
|
+
idFor(feature: RelayFeature, config?: RelaysConfig): string | undefined;
|
|
121
|
+
/** The address a feature uses: its service, else `fallback` (the relay of relay.json), else the official one. */
|
|
122
|
+
urlFor(feature: RelayFeature, fallback?: string): string;
|
|
123
|
+
/** Adds a relay by its address: its id from its host name. The id, and the one before for the same address. */
|
|
124
|
+
add(url: string, name?: string): string;
|
|
125
|
+
/** Adds a folder a cloud drive keeps the same on the user's computers, under `~` when it is in the home directory: its id. */
|
|
126
|
+
addFolder(path: string, name?: string): string;
|
|
127
|
+
/**
|
|
128
|
+
* Adds a service of a type a package registered, its options beside the type: its id, the one of before for the same
|
|
129
|
+
* service (RelayTypeEntry.key), whose options it takes (a new password).
|
|
130
|
+
*/
|
|
131
|
+
addStore(type: string, options: Record<string, unknown>, name?: string): string;
|
|
132
|
+
/** Removes a relay that no feature uses; the official one stays. What is on it (devices, rooms, links) stays on it. */
|
|
133
|
+
remove(id: string): void;
|
|
134
|
+
/** Which relay a feature uses from now on; `undefined` leaves it to the default. */
|
|
135
|
+
use(feature: RelayFeature, id: string | undefined): void;
|
|
136
|
+
/** Written whole, its other keys as they were, through a temporary file. */
|
|
137
|
+
write(config: RelaysConfig): void;
|
|
138
|
+
protected static url(service: RelayService | undefined): string | undefined;
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=relays-file.d.ts.map
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelaysFile = exports.RelaysConfigError = exports.RelayServiceUtil = exports.RelayTypes = exports.RELAY_TYPE_FEATURES = exports.OFFICIAL_RELAY = exports.RELAY_FEATURES = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_os_1 = require("node:os");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const relay_info_1 = require("../common/relay-info");
|
|
8
|
+
const relay_protocol_1 = require("../common/relay-protocol");
|
|
9
|
+
exports.RELAY_FEATURES = ['devices', 'share', 'rooms', 'sync'];
|
|
10
|
+
/** The relay that comes with Ta, by its id in relays.json. */
|
|
11
|
+
exports.OFFICIAL_RELAY = 'cellbang';
|
|
12
|
+
/** What each built-in type can carry: a folder only stores, so only sync goes there. */
|
|
13
|
+
exports.RELAY_TYPE_FEATURES = { 'ta-relay': 'all', folder: ['sync'] };
|
|
14
|
+
/** The types packages registered while ta serve runs (TaExtensionRelays.registerType): a WebDAV server, a bucket of S3. */
|
|
15
|
+
var RelayTypes;
|
|
16
|
+
(function (RelayTypes) {
|
|
17
|
+
const types = new Map();
|
|
18
|
+
RelayTypes.NAME = /^[a-z][a-z0-9-]{1,31}$/;
|
|
19
|
+
function register(type, entry) {
|
|
20
|
+
if (!RelayTypes.NAME.test(type) || type === 'ta-relay' || type === 'folder' || type === 'module') {
|
|
21
|
+
throw new RelaysConfigError(`${type} cannot be a type of relays.json.`);
|
|
22
|
+
}
|
|
23
|
+
types.set(type, entry);
|
|
24
|
+
return () => {
|
|
25
|
+
if (types.get(type) === entry) {
|
|
26
|
+
types.delete(type);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
RelayTypes.register = register;
|
|
31
|
+
function get(type) {
|
|
32
|
+
return types.get(type);
|
|
33
|
+
}
|
|
34
|
+
RelayTypes.get = get;
|
|
35
|
+
})(RelayTypes || (exports.RelayTypes = RelayTypes = {}));
|
|
36
|
+
var RelayServiceUtil;
|
|
37
|
+
(function (RelayServiceUtil) {
|
|
38
|
+
/** `~` and `~/…` as the home directory, a relative path from it: what a folder of relays.json names on this computer. */
|
|
39
|
+
function expand(path) {
|
|
40
|
+
const text = String(path !== null && path !== void 0 ? path : '').trim();
|
|
41
|
+
if (text === '~' || text.startsWith('~/')) {
|
|
42
|
+
return (0, node_path_1.join)((0, node_os_1.homedir)(), text.slice(1));
|
|
43
|
+
}
|
|
44
|
+
return (0, node_path_1.isAbsolute)(text) ? (0, node_path_1.resolve)(text) : (0, node_path_1.resolve)((0, node_os_1.homedir)(), text);
|
|
45
|
+
}
|
|
46
|
+
RelayServiceUtil.expand = expand;
|
|
47
|
+
/** A path under the home directory as `~/…`, the same on the user's other computers. */
|
|
48
|
+
function portable(path) {
|
|
49
|
+
const home = (0, node_os_1.homedir)();
|
|
50
|
+
return path === home ? '~' : path.startsWith(`${home}/`) ? `~${path.slice(home.length)}` : path;
|
|
51
|
+
}
|
|
52
|
+
RelayServiceUtil.portable = portable;
|
|
53
|
+
function serves(type, feature) {
|
|
54
|
+
var _a;
|
|
55
|
+
var _b, _c;
|
|
56
|
+
const features = (_c = (_b = exports.RELAY_TYPE_FEATURES[type]) !== null && _b !== void 0 ? _b : (_a = RelayTypes.get(type)) === null || _a === void 0 ? void 0 : _a.features) !== null && _c !== void 0 ? _c : [];
|
|
57
|
+
return features === 'all' || features.includes(feature);
|
|
58
|
+
}
|
|
59
|
+
RelayServiceUtil.serves = serves;
|
|
60
|
+
function isRelay(entry) {
|
|
61
|
+
return entry.type === 'ta-relay' && typeof entry.url === 'string';
|
|
62
|
+
}
|
|
63
|
+
RelayServiceUtil.isRelay = isRelay;
|
|
64
|
+
function isFolder(entry) {
|
|
65
|
+
return entry.type === 'folder' && typeof entry.path === 'string';
|
|
66
|
+
}
|
|
67
|
+
RelayServiceUtil.isFolder = isFolder;
|
|
68
|
+
/** The features a service carries when not all of them: a folder's, a registered type's (none while it is off). */
|
|
69
|
+
function only(type) {
|
|
70
|
+
var _a;
|
|
71
|
+
var _b, _c;
|
|
72
|
+
const features = (_c = (_b = exports.RELAY_TYPE_FEATURES[type]) !== null && _b !== void 0 ? _b : (_a = RelayTypes.get(type)) === null || _a === void 0 ? void 0 : _a.features) !== null && _c !== void 0 ? _c : [];
|
|
73
|
+
return features === 'all' ? undefined : [...features];
|
|
74
|
+
}
|
|
75
|
+
RelayServiceUtil.only = only;
|
|
76
|
+
/** The folders of cloud drives on this computer, for sync: a `Ta` folder in each, to be made when chosen. */
|
|
77
|
+
function drives() {
|
|
78
|
+
const home = (0, node_os_1.homedir)();
|
|
79
|
+
const found = [];
|
|
80
|
+
const add = (name, dir) => {
|
|
81
|
+
try {
|
|
82
|
+
if ((0, node_fs_1.statSync)(dir).isDirectory()) {
|
|
83
|
+
found.push({ name, path: portable((0, node_path_1.join)(dir, 'Ta')) });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (_a) {
|
|
87
|
+
// Not on this computer.
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
add('iCloud Drive', (0, node_path_1.join)(home, 'Library', 'Mobile Documents', 'com~apple~CloudDocs'));
|
|
91
|
+
try {
|
|
92
|
+
for (const entry of (0, node_fs_1.readdirSync)((0, node_path_1.join)(home, 'Library', 'CloudStorage'))) {
|
|
93
|
+
// OneDrive-…, Dropbox, GoogleDrive-…, as macOS names the folders of File Provider.
|
|
94
|
+
add(entry.replace(/-.*$/, ''), (0, node_path_1.join)(home, 'Library', 'CloudStorage', entry));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch (_a) {
|
|
98
|
+
// None.
|
|
99
|
+
}
|
|
100
|
+
for (const name of ['Dropbox', 'OneDrive', 'Nutstore Files', '坚果云']) {
|
|
101
|
+
add(name, (0, node_path_1.join)(home, name));
|
|
102
|
+
}
|
|
103
|
+
return found.filter((drive, index) => found.findIndex(other => other.path === drive.path) === index);
|
|
104
|
+
}
|
|
105
|
+
RelayServiceUtil.drives = drives;
|
|
106
|
+
/** Whether a folder is there and Ta may write in it; why not, else. */
|
|
107
|
+
function checkFolder(path) {
|
|
108
|
+
const dir = expand(path);
|
|
109
|
+
try {
|
|
110
|
+
if (!(0, node_fs_1.statSync)(dir).isDirectory()) {
|
|
111
|
+
return `${dir} is not a folder.`;
|
|
112
|
+
}
|
|
113
|
+
(0, node_fs_1.accessSync)(dir, node_fs_1.constants.W_OK);
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
const code = error.code;
|
|
118
|
+
return code === 'ENOENT' ? `There is no folder ${dir}.` : `Ta cannot write in ${dir}.`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
RelayServiceUtil.checkFolder = checkFolder;
|
|
122
|
+
})(RelayServiceUtil || (exports.RelayServiceUtil = RelayServiceUtil = {}));
|
|
123
|
+
class RelaysConfigError extends Error {
|
|
124
|
+
}
|
|
125
|
+
exports.RelaysConfigError = RelaysConfigError;
|
|
126
|
+
const ID = /^[a-z0-9][a-z0-9_-]{0,39}$/;
|
|
127
|
+
/**
|
|
128
|
+
* relays.json: read again on every use, as it may be edited while Ta runs. Without it every feature uses the relay
|
|
129
|
+
* of relay.json, or the official one, as before there was this file.
|
|
130
|
+
*/
|
|
131
|
+
class RelaysFile {
|
|
132
|
+
constructor(agentDir) {
|
|
133
|
+
this.agentDir = agentDir;
|
|
134
|
+
}
|
|
135
|
+
get file() {
|
|
136
|
+
return (0, node_path_1.join)(this.agentDir, RelaysFile.FILE);
|
|
137
|
+
}
|
|
138
|
+
/** The file as it is; throws RelaysConfigError when it is not JSON of relays.json. */
|
|
139
|
+
read() {
|
|
140
|
+
let text;
|
|
141
|
+
try {
|
|
142
|
+
text = (0, node_fs_1.readFileSync)(this.file, 'utf8');
|
|
143
|
+
}
|
|
144
|
+
catch (_a) {
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
let data;
|
|
148
|
+
try {
|
|
149
|
+
data = JSON.parse(text);
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
throw new RelaysConfigError(`${this.file} is not valid JSON: ${error.message}`);
|
|
153
|
+
}
|
|
154
|
+
if (!data || typeof data !== 'object' || Array.isArray(data)) {
|
|
155
|
+
throw new RelaysConfigError(`${this.file} does not hold a JSON object.`);
|
|
156
|
+
}
|
|
157
|
+
return data;
|
|
158
|
+
}
|
|
159
|
+
/** The file, or nothing when it cannot be read: what a feature uses then is the relay of before. */
|
|
160
|
+
safeRead() {
|
|
161
|
+
try {
|
|
162
|
+
return { config: this.read() };
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
return { config: {}, error: error.message };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** The services, the official one first (unless relays.json gives its id another address). */
|
|
169
|
+
services(config = this.safeRead().config) {
|
|
170
|
+
const entries = [];
|
|
171
|
+
const own = config.relays && typeof config.relays === 'object' ? config.relays : {};
|
|
172
|
+
if (!own[exports.OFFICIAL_RELAY]) {
|
|
173
|
+
entries.push({ id: exports.OFFICIAL_RELAY, type: 'ta-relay', url: relay_protocol_1.RELAY_DEFAULT_URL, name: 'relay.cellbang.com', builtin: true });
|
|
174
|
+
}
|
|
175
|
+
for (const [id, service] of Object.entries(own)) {
|
|
176
|
+
if (!ID.test(id) || !service || typeof service !== 'object') {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const name = service.name ? { name: String(service.name).slice(0, 80) } : {};
|
|
180
|
+
if (service.type === 'folder') {
|
|
181
|
+
if (typeof service.path === 'string' && service.path.trim()) {
|
|
182
|
+
entries.push({ id, type: 'folder', path: service.path.trim(), ...name, builtin: false });
|
|
183
|
+
}
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (typeof service.type === 'string' && service.type !== 'ta-relay' && RelayTypes.NAME.test(service.type)) {
|
|
187
|
+
const { type, name: _name, ...options } = service;
|
|
188
|
+
entries.push({ id, type, options, ...name, builtin: false });
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const url = RelaysFile.url(service);
|
|
192
|
+
if (url) {
|
|
193
|
+
entries.push({ id, type: 'ta-relay', url, ...name, builtin: id === exports.OFFICIAL_RELAY });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return entries;
|
|
197
|
+
}
|
|
198
|
+
/** The service a feature uses: as relays.json says when it can carry it, else a relay of Ta (`fallback`, then the official one). */
|
|
199
|
+
serviceFor(feature, fallback) {
|
|
200
|
+
var _a;
|
|
201
|
+
const { config } = this.safeRead();
|
|
202
|
+
const id = this.idFor(feature, config);
|
|
203
|
+
const service = id ? this.services(config).find(entry => entry.id === id) : undefined;
|
|
204
|
+
if (service) {
|
|
205
|
+
return service;
|
|
206
|
+
}
|
|
207
|
+
const url = fallback !== null && fallback !== void 0 ? fallback : relay_protocol_1.RELAY_DEFAULT_URL;
|
|
208
|
+
return (_a = this.services(config).find(entry => RelayServiceUtil.isRelay(entry) && entry.url === url)) !== null && _a !== void 0 ? _a : { id: '', type: 'ta-relay', url, builtin: false };
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* The id of the service a feature uses: as `use` says, an extension's feature as `rooms`, `sync` as `rooms` too,
|
|
212
|
+
* then `default`; never one that cannot carry the feature (a folder for rooms).
|
|
213
|
+
*/
|
|
214
|
+
idFor(feature, config = this.safeRead().config) {
|
|
215
|
+
var _a, _b, _c;
|
|
216
|
+
const use = config.use && typeof config.use === 'object' ? config.use : {};
|
|
217
|
+
const services = this.services(config);
|
|
218
|
+
const pick = (id) => typeof id === 'string' && services.some(service => service.id === id && RelayServiceUtil.serves(service.type, feature)) ? id : undefined;
|
|
219
|
+
const own = exports.RELAY_FEATURES.includes(feature) ? undefined : pick(use.rooms);
|
|
220
|
+
return (_c = (_b = (_a = pick(use[feature])) !== null && _a !== void 0 ? _a : own) !== null && _b !== void 0 ? _b : (feature === 'sync' ? pick(use.rooms) : undefined)) !== null && _c !== void 0 ? _c : pick(config.default);
|
|
221
|
+
}
|
|
222
|
+
/** The address a feature uses: its service, else `fallback` (the relay of relay.json), else the official one. */
|
|
223
|
+
urlFor(feature, fallback) {
|
|
224
|
+
const service = this.serviceFor(feature, fallback);
|
|
225
|
+
return RelayServiceUtil.isRelay(service) ? service.url : (fallback !== null && fallback !== void 0 ? fallback : relay_protocol_1.RELAY_DEFAULT_URL);
|
|
226
|
+
}
|
|
227
|
+
/** Adds a relay by its address: its id from its host name. The id, and the one before for the same address. */
|
|
228
|
+
add(url, name) {
|
|
229
|
+
var _a;
|
|
230
|
+
const address = relay_info_1.RelayInfoUtil.normalize(url);
|
|
231
|
+
const config = this.read();
|
|
232
|
+
const relays = { ...((_a = config.relays) !== null && _a !== void 0 ? _a : {}) };
|
|
233
|
+
const before = this.services(config).find(service => RelayServiceUtil.isRelay(service) && service.url === address);
|
|
234
|
+
if (before) {
|
|
235
|
+
return before.id;
|
|
236
|
+
}
|
|
237
|
+
const stem = new URL(address).hostname
|
|
238
|
+
.toLowerCase()
|
|
239
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
240
|
+
.replace(/^-+|-+$/g, '')
|
|
241
|
+
.slice(0, 32) || 'relay';
|
|
242
|
+
let id = stem;
|
|
243
|
+
for (let n = 2; relays[id] || id === exports.OFFICIAL_RELAY; n++) {
|
|
244
|
+
id = `${stem}-${n}`;
|
|
245
|
+
}
|
|
246
|
+
relays[id] = { type: 'ta-relay', url: address, ...((name === null || name === void 0 ? void 0 : name.trim()) ? { name: name.trim().slice(0, 80) } : {}) };
|
|
247
|
+
this.write({ ...config, relays });
|
|
248
|
+
return id;
|
|
249
|
+
}
|
|
250
|
+
/** Adds a folder a cloud drive keeps the same on the user's computers, under `~` when it is in the home directory: its id. */
|
|
251
|
+
addFolder(path, name) {
|
|
252
|
+
var _a, _b;
|
|
253
|
+
// The `Ta` folder of a drive is made at once: the drive was there.
|
|
254
|
+
const wanted = RelayServiceUtil.expand(path);
|
|
255
|
+
if (RelayServiceUtil.drives().some(drive => RelayServiceUtil.expand(drive.path) === wanted)) {
|
|
256
|
+
(0, node_fs_1.mkdirSync)(wanted, { recursive: true });
|
|
257
|
+
}
|
|
258
|
+
const problem = RelayServiceUtil.checkFolder(path);
|
|
259
|
+
if (problem) {
|
|
260
|
+
throw new RelaysConfigError(problem);
|
|
261
|
+
}
|
|
262
|
+
const kept = RelayServiceUtil.portable(RelayServiceUtil.expand(path));
|
|
263
|
+
const config = this.read();
|
|
264
|
+
const before = this.services(config).find(service => RelayServiceUtil.isFolder(service) && RelayServiceUtil.expand(service.path) === RelayServiceUtil.expand(kept));
|
|
265
|
+
if (before) {
|
|
266
|
+
return before.id;
|
|
267
|
+
}
|
|
268
|
+
const relays = { ...((_a = config.relays) !== null && _a !== void 0 ? _a : {}) };
|
|
269
|
+
const stem = ((_b = kept.split('/').filter(Boolean).pop()) !== null && _b !== void 0 ? _b : 'folder')
|
|
270
|
+
.toLowerCase()
|
|
271
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
272
|
+
.replace(/^-+|-+$/g, '')
|
|
273
|
+
.slice(0, 28) || 'folder';
|
|
274
|
+
let id = `folder-${stem}`;
|
|
275
|
+
for (let n = 2; relays[id]; n++) {
|
|
276
|
+
id = `folder-${stem}-${n}`;
|
|
277
|
+
}
|
|
278
|
+
relays[id] = { type: 'folder', path: kept, ...((name === null || name === void 0 ? void 0 : name.trim()) ? { name: name.trim().slice(0, 80) } : {}) };
|
|
279
|
+
this.write({ ...config, relays });
|
|
280
|
+
return id;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Adds a service of a type a package registered, its options beside the type: its id, the one of before for the same
|
|
284
|
+
* service (RelayTypeEntry.key), whose options it takes (a new password).
|
|
285
|
+
*/
|
|
286
|
+
addStore(type, options, name) {
|
|
287
|
+
var _a;
|
|
288
|
+
const entry = RelayTypes.get(type);
|
|
289
|
+
if (!entry) {
|
|
290
|
+
throw new RelaysConfigError(`There is no type ${type}: turn on the package that adds it.`);
|
|
291
|
+
}
|
|
292
|
+
const clean = Object.fromEntries(Object.entries(options !== null && options !== void 0 ? options : {}).filter(([key, value]) => key !== 'type' && key !== 'name' && value !== undefined && value !== ''));
|
|
293
|
+
const key = entry.key(clean);
|
|
294
|
+
const config = this.read();
|
|
295
|
+
const relays = { ...((_a = config.relays) !== null && _a !== void 0 ? _a : {}) };
|
|
296
|
+
const before = this.services(config).find(service => service.type === type && 'options' in service && entry.key(service.options) === key);
|
|
297
|
+
let id = before === null || before === void 0 ? void 0 : before.id;
|
|
298
|
+
if (!id) {
|
|
299
|
+
const stem = key
|
|
300
|
+
.replace(/^[a-z]+:\/\//i, '')
|
|
301
|
+
.toLowerCase()
|
|
302
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
303
|
+
.replace(/^-+|-+$/g, '')
|
|
304
|
+
.slice(0, 28) || type;
|
|
305
|
+
id = `${type}-${stem}`.slice(0, 40).replace(/-+$/, '');
|
|
306
|
+
for (let n = 2; relays[id]; n++) {
|
|
307
|
+
id = `${type}-${stem}`.slice(0, 36).replace(/-+$/, '') + `-${n}`;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
const named = (name === null || name === void 0 ? void 0 : name.trim()) ? name.trim().slice(0, 80) : before === null || before === void 0 ? void 0 : before.name;
|
|
311
|
+
relays[id] = { type, ...clean, ...(named ? { name: named } : {}) };
|
|
312
|
+
this.write({ ...config, relays });
|
|
313
|
+
return id;
|
|
314
|
+
}
|
|
315
|
+
/** Removes a relay that no feature uses; the official one stays. What is on it (devices, rooms, links) stays on it. */
|
|
316
|
+
remove(id) {
|
|
317
|
+
var _a;
|
|
318
|
+
var _b, _c;
|
|
319
|
+
const config = this.read();
|
|
320
|
+
if (id === exports.OFFICIAL_RELAY && !((_a = config.relays) === null || _a === void 0 ? void 0 : _a[id])) {
|
|
321
|
+
throw new RelaysConfigError('The official relay comes with Ta.');
|
|
322
|
+
}
|
|
323
|
+
const using = Object.entries((_b = config.use) !== null && _b !== void 0 ? _b : {})
|
|
324
|
+
.filter(([, used]) => used === id)
|
|
325
|
+
.map(([feature]) => feature);
|
|
326
|
+
if (using.length || config.default === id) {
|
|
327
|
+
throw new RelaysConfigError(`${id} is used by ${[...using, ...(config.default === id ? ['default'] : [])].join(', ')}: choose another relay for them first.`);
|
|
328
|
+
}
|
|
329
|
+
const relays = { ...((_c = config.relays) !== null && _c !== void 0 ? _c : {}) };
|
|
330
|
+
delete relays[id];
|
|
331
|
+
this.write({ ...config, relays });
|
|
332
|
+
}
|
|
333
|
+
/** Which relay a feature uses from now on; `undefined` leaves it to the default. */
|
|
334
|
+
use(feature, id) {
|
|
335
|
+
var _a;
|
|
336
|
+
const config = this.read();
|
|
337
|
+
if (!/^[a-z][a-z0-9-]{0,39}$/.test(feature)) {
|
|
338
|
+
throw new RelaysConfigError(`${feature} is not a feature of relays.json.`);
|
|
339
|
+
}
|
|
340
|
+
const service = id === undefined ? undefined : this.services(config).find(each => each.id === id);
|
|
341
|
+
if (id !== undefined && !service) {
|
|
342
|
+
throw new RelaysConfigError(`There is no relay ${id} in relays.json.`);
|
|
343
|
+
}
|
|
344
|
+
if (service && !RelayServiceUtil.serves(service.type, feature)) {
|
|
345
|
+
throw new RelaysConfigError(`${id} only keeps what sync stores, and ${feature} needs a relay of Ta.`);
|
|
346
|
+
}
|
|
347
|
+
const use = { ...((_a = config.use) !== null && _a !== void 0 ? _a : {}) };
|
|
348
|
+
if (id === undefined) {
|
|
349
|
+
delete use[feature];
|
|
350
|
+
}
|
|
351
|
+
else {
|
|
352
|
+
use[feature] = id;
|
|
353
|
+
}
|
|
354
|
+
this.write({ ...config, use });
|
|
355
|
+
}
|
|
356
|
+
/** Written whole, its other keys as they were, through a temporary file. */
|
|
357
|
+
write(config) {
|
|
358
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(this.file), { recursive: true });
|
|
359
|
+
const temporary = `${this.file}.${process.pid}.tmp`;
|
|
360
|
+
(0, node_fs_1.writeFileSync)(temporary, `${JSON.stringify(config, null, 2)}\n`, { mode: 0o600 });
|
|
361
|
+
(0, node_fs_1.chmodSync)(temporary, 0o600);
|
|
362
|
+
(0, node_fs_1.renameSync)(temporary, this.file);
|
|
363
|
+
}
|
|
364
|
+
static url(service) {
|
|
365
|
+
if (!service || typeof service !== 'object' || (service.type !== undefined && service.type !== 'ta-relay') || !('url' in service)) {
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
try {
|
|
369
|
+
return relay_info_1.RelayInfoUtil.normalize(String(service.url));
|
|
370
|
+
}
|
|
371
|
+
catch (_a) {
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
exports.RelaysFile = RelaysFile;
|
|
377
|
+
RelaysFile.FILE = 'relays.json';
|
|
378
|
+
//# sourceMappingURL=relays-file.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/ta-relay",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.13.0",
|
|
4
4
|
"description": "Ta from anywhere: a relay that only forwards, and end-to-end encryption between a paired device and ta serve",
|
|
5
5
|
"homepage": "https://github.com/cellbang/cell",
|
|
6
6
|
"bugs": {
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@celljs/rpc": "4.
|
|
33
|
+
"@celljs/rpc": "4.13.0"
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|