@meshagent/meshagent 0.5.18 → 0.6.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/CHANGELOG.md +6 -0
- package/dist/browser/agent-client.d.ts +15 -7
- package/dist/browser/agent-client.js +52 -14
- package/dist/browser/api_keys.d.ts +13 -0
- package/dist/browser/api_keys.js +185 -0
- package/dist/browser/data-types.d.ts +6 -0
- package/dist/browser/data-types.js +20 -1
- package/dist/browser/entrypoint.d.ts +1423 -1464
- package/dist/browser/helpers.d.ts +9 -7
- package/dist/browser/helpers.js +18 -5
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +3 -0
- package/dist/browser/lk-client.d.ts +18 -0
- package/dist/browser/lk-client.js +24 -0
- package/dist/browser/lk-protocol.d.ts +22 -0
- package/dist/browser/lk-protocol.js +37 -0
- package/dist/browser/meshagent-client.d.ts +392 -0
- package/dist/browser/meshagent-client.js +1051 -0
- package/dist/browser/participant-token.d.ts +179 -7
- package/dist/browser/participant-token.js +373 -21
- package/dist/browser/stream-controller.js +3 -14
- package/dist/esm/agent-client.d.ts +15 -7
- package/dist/esm/agent-client.js +52 -14
- package/dist/esm/api_keys.d.ts +13 -0
- package/dist/esm/api_keys.js +175 -0
- package/dist/esm/data-types.d.ts +6 -0
- package/dist/esm/data-types.js +18 -0
- package/dist/esm/entrypoint.d.ts +1423 -1464
- package/dist/esm/helpers.d.ts +9 -7
- package/dist/esm/helpers.js +19 -6
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/lk-client.d.ts +18 -0
- package/dist/esm/lk-client.js +19 -0
- package/dist/esm/lk-protocol.d.ts +22 -0
- package/dist/esm/lk-protocol.js +33 -0
- package/dist/esm/meshagent-client.d.ts +392 -0
- package/dist/esm/meshagent-client.js +1047 -0
- package/dist/esm/participant-token.d.ts +179 -7
- package/dist/esm/participant-token.js +357 -20
- package/dist/esm/stream-controller.js +3 -14
- package/dist/node/agent-client.d.ts +15 -7
- package/dist/node/agent-client.js +52 -14
- package/dist/node/api_keys.d.ts +13 -0
- package/dist/node/api_keys.js +185 -0
- package/dist/node/data-types.d.ts +6 -0
- package/dist/node/data-types.js +20 -1
- package/dist/node/entrypoint.d.ts +1423 -1464
- package/dist/node/helpers.d.ts +9 -7
- package/dist/node/helpers.js +18 -5
- package/dist/node/index.d.ts +3 -0
- package/dist/node/index.js +3 -0
- package/dist/node/lk-client.d.ts +18 -0
- package/dist/node/lk-client.js +24 -0
- package/dist/node/lk-protocol.d.ts +22 -0
- package/dist/node/lk-protocol.js +37 -0
- package/dist/node/meshagent-client.d.ts +392 -0
- package/dist/node/meshagent-client.js +1051 -0
- package/dist/node/participant-token.d.ts +179 -7
- package/dist/node/participant-token.js +373 -21
- package/dist/node/stream-controller.js +3 -14
- package/package.json +6 -3
|
@@ -1,9 +1,171 @@
|
|
|
1
|
+
export type StringList = string[];
|
|
2
|
+
export declare class AgentsGrant {
|
|
3
|
+
registerAgent: boolean;
|
|
4
|
+
registerPublicToolkit: boolean;
|
|
5
|
+
registerPrivateToolkit: boolean;
|
|
6
|
+
call: boolean;
|
|
7
|
+
useAgents: boolean;
|
|
8
|
+
useTools: boolean;
|
|
9
|
+
constructor({ registerAgent, registerPublicToolkit, registerPrivateToolkit, call, useAgents, useTools, }?: {
|
|
10
|
+
registerAgent?: boolean;
|
|
11
|
+
registerPublicToolkit?: boolean;
|
|
12
|
+
registerPrivateToolkit?: boolean;
|
|
13
|
+
call?: boolean;
|
|
14
|
+
useAgents?: boolean;
|
|
15
|
+
useTools?: boolean;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export declare class LivekitGrant {
|
|
19
|
+
breakoutRooms?: StringList;
|
|
20
|
+
constructor({ breakoutRooms }?: {
|
|
21
|
+
breakoutRooms?: StringList;
|
|
22
|
+
});
|
|
23
|
+
canJoinBreakoutRoom(name: string): boolean;
|
|
24
|
+
}
|
|
25
|
+
export declare class QueuesGrant {
|
|
26
|
+
send?: StringList;
|
|
27
|
+
receive?: StringList;
|
|
28
|
+
list: boolean;
|
|
29
|
+
constructor({ send, receive, list }?: {
|
|
30
|
+
send?: StringList;
|
|
31
|
+
receive?: StringList;
|
|
32
|
+
list?: boolean;
|
|
33
|
+
});
|
|
34
|
+
canSend(q: string): boolean;
|
|
35
|
+
canReceive(q: string): boolean;
|
|
36
|
+
}
|
|
37
|
+
export declare class MessagingGrant {
|
|
38
|
+
broadcast: boolean;
|
|
39
|
+
list: boolean;
|
|
40
|
+
send: boolean;
|
|
41
|
+
constructor({ broadcast, list, send }?: {
|
|
42
|
+
broadcast?: boolean;
|
|
43
|
+
list?: boolean;
|
|
44
|
+
send?: boolean;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
export declare class TableGrant {
|
|
48
|
+
name: string;
|
|
49
|
+
write: boolean;
|
|
50
|
+
read: boolean;
|
|
51
|
+
alter: boolean;
|
|
52
|
+
constructor({ name, write, read, alter }: {
|
|
53
|
+
name: string;
|
|
54
|
+
write?: boolean;
|
|
55
|
+
read?: boolean;
|
|
56
|
+
alter?: boolean;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
export declare class DatabaseGrant {
|
|
60
|
+
tables?: TableGrant[];
|
|
61
|
+
listTables: boolean;
|
|
62
|
+
constructor({ tables, listTables }?: {
|
|
63
|
+
tables?: TableGrant[];
|
|
64
|
+
listTables?: boolean;
|
|
65
|
+
});
|
|
66
|
+
private _match;
|
|
67
|
+
canWrite(table: string): boolean;
|
|
68
|
+
canRead(table: string): boolean;
|
|
69
|
+
canAlter(table: string): boolean;
|
|
70
|
+
}
|
|
71
|
+
export declare class SyncPathGrant {
|
|
72
|
+
path: string;
|
|
73
|
+
readOnly: boolean;
|
|
74
|
+
constructor({ path, readOnly }: {
|
|
75
|
+
path: string;
|
|
76
|
+
readOnly?: boolean;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
export declare class SyncGrant {
|
|
80
|
+
paths?: SyncPathGrant[];
|
|
81
|
+
constructor({ paths }?: {
|
|
82
|
+
paths?: SyncPathGrant[];
|
|
83
|
+
});
|
|
84
|
+
private matches;
|
|
85
|
+
canRead(path: string): boolean;
|
|
86
|
+
canWrite(path: string): boolean;
|
|
87
|
+
}
|
|
88
|
+
export declare class StoragePathGrant {
|
|
89
|
+
path: string;
|
|
90
|
+
readOnly: boolean;
|
|
91
|
+
constructor({ path, readOnly }: {
|
|
92
|
+
path: string;
|
|
93
|
+
readOnly?: boolean;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export declare class StorageGrant {
|
|
97
|
+
paths?: StoragePathGrant[];
|
|
98
|
+
constructor({ paths }?: {
|
|
99
|
+
paths?: StoragePathGrant[];
|
|
100
|
+
});
|
|
101
|
+
private matches;
|
|
102
|
+
canRead(path: string): boolean;
|
|
103
|
+
canWrite(path: string): boolean;
|
|
104
|
+
}
|
|
105
|
+
export declare class ContainersGrant {
|
|
106
|
+
logs: boolean;
|
|
107
|
+
pull?: StringList;
|
|
108
|
+
run?: StringList;
|
|
109
|
+
useContainers: boolean;
|
|
110
|
+
constructor({ logs, pull, run, useContainers }?: {
|
|
111
|
+
logs?: boolean;
|
|
112
|
+
pull?: StringList;
|
|
113
|
+
run?: StringList;
|
|
114
|
+
useContainers?: boolean;
|
|
115
|
+
});
|
|
116
|
+
private match;
|
|
117
|
+
canPull(tag: string): boolean;
|
|
118
|
+
canRun(tag: string): boolean;
|
|
119
|
+
}
|
|
120
|
+
export declare class DeveloperGrant {
|
|
121
|
+
logs: boolean;
|
|
122
|
+
constructor({ logs }?: {
|
|
123
|
+
logs?: boolean;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
export declare class AdminGrant {
|
|
127
|
+
}
|
|
128
|
+
export declare class SecretsGrant {
|
|
129
|
+
requestOauthToken?: StringList;
|
|
130
|
+
canRequestOauthToken(authorizationEndpoint: string): boolean;
|
|
131
|
+
}
|
|
132
|
+
export declare class ApiScope {
|
|
133
|
+
livekit?: LivekitGrant;
|
|
134
|
+
queues?: QueuesGrant;
|
|
135
|
+
messaging?: MessagingGrant;
|
|
136
|
+
database?: DatabaseGrant;
|
|
137
|
+
sync?: SyncGrant;
|
|
138
|
+
storage?: StorageGrant;
|
|
139
|
+
containers?: ContainersGrant;
|
|
140
|
+
developer?: DeveloperGrant;
|
|
141
|
+
agents?: AgentsGrant;
|
|
142
|
+
admin?: AdminGrant;
|
|
143
|
+
secrets?: SecretsGrant;
|
|
144
|
+
constructor({ livekit, queues, messaging, database, sync, storage, containers, developer, agents, admin, secrets, }?: {
|
|
145
|
+
livekit?: LivekitGrant;
|
|
146
|
+
queues?: QueuesGrant;
|
|
147
|
+
messaging?: MessagingGrant;
|
|
148
|
+
database?: DatabaseGrant;
|
|
149
|
+
sync?: SyncGrant;
|
|
150
|
+
storage?: StorageGrant;
|
|
151
|
+
containers?: ContainersGrant;
|
|
152
|
+
developer?: DeveloperGrant;
|
|
153
|
+
agents?: AgentsGrant;
|
|
154
|
+
admin?: AdminGrant;
|
|
155
|
+
secrets?: SecretsGrant;
|
|
156
|
+
});
|
|
157
|
+
static agentDefault(): ApiScope;
|
|
158
|
+
static userDefault(): ApiScope;
|
|
159
|
+
static full(): ApiScope;
|
|
160
|
+
toJSON(): Record<string, any>;
|
|
161
|
+
static fromJSON(obj: any): ApiScope;
|
|
162
|
+
}
|
|
1
163
|
export declare class ParticipantGrant {
|
|
2
164
|
name: string;
|
|
3
|
-
scope?: string;
|
|
165
|
+
scope?: string | ApiScope;
|
|
4
166
|
constructor({ name, scope }: {
|
|
5
167
|
name: string;
|
|
6
|
-
scope?: string;
|
|
168
|
+
scope?: string | ApiScope;
|
|
7
169
|
});
|
|
8
170
|
toJson(): Record<string, any>;
|
|
9
171
|
static fromJson(json: Record<string, any>): ParticipantGrant;
|
|
@@ -12,25 +174,35 @@ export declare class ParticipantToken {
|
|
|
12
174
|
name: string;
|
|
13
175
|
projectId?: string;
|
|
14
176
|
apiKeyId?: string;
|
|
177
|
+
version?: string;
|
|
15
178
|
grants: ParticipantGrant[];
|
|
16
179
|
extra?: Record<string, any>;
|
|
17
|
-
constructor({ name, projectId, apiKeyId, extra, grants, }: {
|
|
180
|
+
constructor({ name, projectId, apiKeyId, version, extra, grants, }: {
|
|
18
181
|
name: string;
|
|
19
182
|
projectId?: string;
|
|
20
183
|
apiKeyId?: string;
|
|
184
|
+
version?: string;
|
|
21
185
|
extra?: Record<string, any>;
|
|
22
186
|
grants?: ParticipantGrant[];
|
|
23
187
|
});
|
|
188
|
+
get role(): string;
|
|
24
189
|
get isAgent(): boolean;
|
|
190
|
+
get isUser(): boolean;
|
|
191
|
+
addTunnelGrant(ports: number[]): void;
|
|
25
192
|
addRoleGrant(role: string): void;
|
|
26
193
|
addRoomGrant(roomName: string): void;
|
|
194
|
+
addApiGrant(grant: ApiScope): void;
|
|
195
|
+
grantScope(name: string): string | ApiScope | undefined;
|
|
196
|
+
getApiGrant(): string | ApiScope | undefined;
|
|
27
197
|
toJson(): Record<string, any>;
|
|
28
|
-
toJwt({ token }
|
|
29
|
-
token
|
|
198
|
+
toJwt({ token, expiration, apiKey }?: {
|
|
199
|
+
token?: string;
|
|
200
|
+
expiration?: Date;
|
|
201
|
+
apiKey?: string;
|
|
30
202
|
}): Promise<string>;
|
|
31
203
|
static fromJson(json: Record<string, any>): ParticipantToken;
|
|
32
|
-
static fromJwt(jwtStr: string, options
|
|
33
|
-
token
|
|
204
|
+
static fromJwt(jwtStr: string, options?: {
|
|
205
|
+
token?: string;
|
|
34
206
|
verify?: boolean;
|
|
35
207
|
}): Promise<ParticipantToken>;
|
|
36
208
|
}
|
|
@@ -1,34 +1,294 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ParticipantToken = exports.ParticipantGrant = void 0;
|
|
3
|
+
exports.ParticipantToken = exports.ParticipantGrant = exports.ApiScope = exports.SecretsGrant = exports.AdminGrant = exports.DeveloperGrant = exports.ContainersGrant = exports.StorageGrant = exports.StoragePathGrant = exports.SyncGrant = exports.SyncPathGrant = exports.DatabaseGrant = exports.TableGrant = exports.MessagingGrant = exports.QueuesGrant = exports.LivekitGrant = exports.AgentsGrant = void 0;
|
|
4
4
|
const jose_1 = require("jose");
|
|
5
|
+
const api_keys_1 = require("./api_keys");
|
|
6
|
+
class AgentsGrant {
|
|
7
|
+
constructor({ registerAgent, registerPublicToolkit, registerPrivateToolkit, call, useAgents, useTools, } = {}) {
|
|
8
|
+
this.registerAgent = registerAgent ?? true;
|
|
9
|
+
this.registerPublicToolkit = registerPublicToolkit ?? true;
|
|
10
|
+
this.registerPrivateToolkit = registerPrivateToolkit ?? true;
|
|
11
|
+
this.call = call ?? true;
|
|
12
|
+
this.useAgents = useAgents ?? true;
|
|
13
|
+
this.useTools = useTools ?? true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.AgentsGrant = AgentsGrant;
|
|
17
|
+
class LivekitGrant {
|
|
18
|
+
constructor({ breakoutRooms } = {}) {
|
|
19
|
+
this.breakoutRooms = breakoutRooms;
|
|
20
|
+
}
|
|
21
|
+
canJoinBreakoutRoom(name) {
|
|
22
|
+
return !this.breakoutRooms || this.breakoutRooms.includes(name);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.LivekitGrant = LivekitGrant;
|
|
26
|
+
class QueuesGrant {
|
|
27
|
+
constructor({ send, receive, list } = {}) {
|
|
28
|
+
this.list = true;
|
|
29
|
+
this.send = send;
|
|
30
|
+
this.receive = receive;
|
|
31
|
+
this.list = list ?? true;
|
|
32
|
+
}
|
|
33
|
+
canSend(q) {
|
|
34
|
+
return !this.send || this.send.includes(q);
|
|
35
|
+
}
|
|
36
|
+
canReceive(q) {
|
|
37
|
+
return !this.receive || this.receive.includes(q);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.QueuesGrant = QueuesGrant;
|
|
41
|
+
class MessagingGrant {
|
|
42
|
+
constructor({ broadcast, list, send } = {}) {
|
|
43
|
+
this.broadcast = broadcast ?? true;
|
|
44
|
+
this.list = list ?? true;
|
|
45
|
+
this.send = send ?? true;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.MessagingGrant = MessagingGrant;
|
|
49
|
+
class TableGrant {
|
|
50
|
+
constructor({ name, write, read, alter }) {
|
|
51
|
+
this.write = false;
|
|
52
|
+
this.read = true;
|
|
53
|
+
this.alter = false;
|
|
54
|
+
this.name = name;
|
|
55
|
+
this.write = write ?? false;
|
|
56
|
+
this.read = read ?? true;
|
|
57
|
+
this.alter = alter ?? false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.TableGrant = TableGrant;
|
|
61
|
+
class DatabaseGrant {
|
|
62
|
+
constructor({ tables, listTables } = {}) {
|
|
63
|
+
this.listTables = true;
|
|
64
|
+
this.tables = tables;
|
|
65
|
+
this.listTables = listTables ?? true;
|
|
66
|
+
}
|
|
67
|
+
_match(table) {
|
|
68
|
+
if (!this.tables)
|
|
69
|
+
return undefined;
|
|
70
|
+
return this.tables.find(t => t.name === table);
|
|
71
|
+
}
|
|
72
|
+
canWrite(table) {
|
|
73
|
+
const t = this._match(table);
|
|
74
|
+
return t ? t.write : this.tables === undefined;
|
|
75
|
+
}
|
|
76
|
+
canRead(table) {
|
|
77
|
+
const t = this._match(table);
|
|
78
|
+
return t ? t.read : this.tables === undefined;
|
|
79
|
+
}
|
|
80
|
+
canAlter(table) {
|
|
81
|
+
const t = this._match(table);
|
|
82
|
+
return t ? t.alter : this.tables === undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.DatabaseGrant = DatabaseGrant;
|
|
86
|
+
class SyncPathGrant {
|
|
87
|
+
constructor({ path, readOnly }) {
|
|
88
|
+
this.path = path;
|
|
89
|
+
this.readOnly = readOnly ?? false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.SyncPathGrant = SyncPathGrant;
|
|
93
|
+
class SyncGrant {
|
|
94
|
+
constructor({ paths } = {}) {
|
|
95
|
+
this.paths = paths;
|
|
96
|
+
}
|
|
97
|
+
matches(p, path) {
|
|
98
|
+
return p.path === path || (p.path.endsWith("*") && path.startsWith(p.path.slice(0, -1)));
|
|
99
|
+
}
|
|
100
|
+
canRead(path) {
|
|
101
|
+
if (this.paths) {
|
|
102
|
+
return this.paths.some(p => this.matches(p, path));
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
canWrite(path) {
|
|
107
|
+
if (this.paths) {
|
|
108
|
+
const p = this.paths.find(pp => this.matches(pp, path));
|
|
109
|
+
return p ? !p.readOnly : false;
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.SyncGrant = SyncGrant;
|
|
115
|
+
class StoragePathGrant {
|
|
116
|
+
constructor({ path, readOnly }) {
|
|
117
|
+
this.path = path;
|
|
118
|
+
this.readOnly = readOnly ?? false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.StoragePathGrant = StoragePathGrant;
|
|
122
|
+
class StorageGrant {
|
|
123
|
+
constructor({ paths } = {}) {
|
|
124
|
+
this.paths = paths;
|
|
125
|
+
}
|
|
126
|
+
matches(p, path) {
|
|
127
|
+
return path.startsWith(p.path);
|
|
128
|
+
}
|
|
129
|
+
canRead(path) {
|
|
130
|
+
if (!this.paths)
|
|
131
|
+
return true;
|
|
132
|
+
return this.paths.some(p => this.matches(p, path));
|
|
133
|
+
}
|
|
134
|
+
canWrite(path) {
|
|
135
|
+
if (!this.paths)
|
|
136
|
+
return true;
|
|
137
|
+
const p = this.paths.find(pp => this.matches(pp, path));
|
|
138
|
+
return p ? !p.readOnly : false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
exports.StorageGrant = StorageGrant;
|
|
142
|
+
class ContainersGrant {
|
|
143
|
+
constructor({ logs, pull, run, useContainers } = {}) {
|
|
144
|
+
this.logs = logs ?? true;
|
|
145
|
+
this.pull = pull;
|
|
146
|
+
this.run = run;
|
|
147
|
+
this.useContainers = useContainers ?? true;
|
|
148
|
+
}
|
|
149
|
+
match(list, tag) {
|
|
150
|
+
if (!list) {
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
return list.some(t => tag === t || tag.startsWith(t.endsWith("*") ? t.slice(0, -1) : t));
|
|
154
|
+
}
|
|
155
|
+
canPull(tag) {
|
|
156
|
+
return this.match(this.pull, tag);
|
|
157
|
+
}
|
|
158
|
+
canRun(tag) {
|
|
159
|
+
return this.match(this.run, tag);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
exports.ContainersGrant = ContainersGrant;
|
|
163
|
+
class DeveloperGrant {
|
|
164
|
+
constructor({ logs } = {}) {
|
|
165
|
+
this.logs = logs ?? true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
exports.DeveloperGrant = DeveloperGrant;
|
|
169
|
+
class AdminGrant {
|
|
170
|
+
}
|
|
171
|
+
exports.AdminGrant = AdminGrant;
|
|
172
|
+
class SecretsGrant {
|
|
173
|
+
canRequestOauthToken(authorizationEndpoint) {
|
|
174
|
+
if (!this.requestOauthToken) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
return this.requestOauthToken.some(t => t === authorizationEndpoint || ((t.endsWith("*") && authorizationEndpoint.startsWith(t.slice(0, -1)))));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
exports.SecretsGrant = SecretsGrant;
|
|
181
|
+
class ApiScope {
|
|
182
|
+
constructor({ livekit, queues, messaging, database, sync, storage, containers, developer, agents, admin, secrets, } = {}) {
|
|
183
|
+
this.livekit = livekit;
|
|
184
|
+
this.queues = queues;
|
|
185
|
+
this.messaging = messaging;
|
|
186
|
+
this.database = database;
|
|
187
|
+
this.sync = sync;
|
|
188
|
+
this.storage = storage;
|
|
189
|
+
this.containers = containers;
|
|
190
|
+
this.developer = developer;
|
|
191
|
+
this.agents = agents;
|
|
192
|
+
this.admin = admin;
|
|
193
|
+
this.secrets = secrets;
|
|
194
|
+
}
|
|
195
|
+
static agentDefault() {
|
|
196
|
+
const s = new ApiScope();
|
|
197
|
+
s.livekit = new LivekitGrant();
|
|
198
|
+
s.queues = new QueuesGrant();
|
|
199
|
+
s.messaging = new MessagingGrant();
|
|
200
|
+
s.database = new DatabaseGrant();
|
|
201
|
+
s.sync = new SyncGrant();
|
|
202
|
+
s.storage = new StorageGrant();
|
|
203
|
+
s.containers = new ContainersGrant();
|
|
204
|
+
s.developer = new DeveloperGrant();
|
|
205
|
+
s.agents = new AgentsGrant();
|
|
206
|
+
return s;
|
|
207
|
+
}
|
|
208
|
+
static userDefault() {
|
|
209
|
+
const s = new ApiScope();
|
|
210
|
+
s.livekit = new LivekitGrant();
|
|
211
|
+
s.queues = new QueuesGrant();
|
|
212
|
+
s.messaging = new MessagingGrant();
|
|
213
|
+
s.database = new DatabaseGrant();
|
|
214
|
+
s.sync = new SyncGrant();
|
|
215
|
+
s.storage = new StorageGrant();
|
|
216
|
+
s.containers = new ContainersGrant();
|
|
217
|
+
s.developer = new DeveloperGrant();
|
|
218
|
+
s.agents = new AgentsGrant();
|
|
219
|
+
s.secrets = new SecretsGrant();
|
|
220
|
+
return s;
|
|
221
|
+
}
|
|
222
|
+
static full() {
|
|
223
|
+
const s = ApiScope.agentDefault();
|
|
224
|
+
s.admin = new AdminGrant();
|
|
225
|
+
return s;
|
|
226
|
+
}
|
|
227
|
+
toJSON() {
|
|
228
|
+
return { ...this };
|
|
229
|
+
}
|
|
230
|
+
static fromJSON(obj) {
|
|
231
|
+
return Object.assign(new ApiScope(), obj);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.ApiScope = ApiScope;
|
|
5
235
|
class ParticipantGrant {
|
|
6
236
|
constructor({ name, scope }) {
|
|
7
237
|
this.name = name;
|
|
8
238
|
this.scope = scope;
|
|
9
239
|
}
|
|
10
240
|
toJson() {
|
|
241
|
+
if (this.name === "api" && this.scope && typeof this.scope !== "string") {
|
|
242
|
+
return {
|
|
243
|
+
name: this.name,
|
|
244
|
+
scope: this.scope.toJSON(),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
11
247
|
return {
|
|
12
248
|
name: this.name,
|
|
13
249
|
scope: this.scope,
|
|
14
250
|
};
|
|
15
251
|
}
|
|
16
252
|
static fromJson(json) {
|
|
253
|
+
const name = json["name"];
|
|
254
|
+
let scope = json["scope"];
|
|
255
|
+
if (name === "api" && scope && typeof scope === "object") {
|
|
256
|
+
scope = ApiScope.fromJSON(scope);
|
|
257
|
+
}
|
|
17
258
|
return new ParticipantGrant({
|
|
18
|
-
name
|
|
19
|
-
scope
|
|
259
|
+
name,
|
|
260
|
+
scope,
|
|
20
261
|
});
|
|
21
262
|
}
|
|
22
263
|
}
|
|
23
264
|
exports.ParticipantGrant = ParticipantGrant;
|
|
265
|
+
function compareSemver(a, b) {
|
|
266
|
+
const pa = a.split(".").map(n => parseInt(n, 10));
|
|
267
|
+
const pb = b.split(".").map(n => parseInt(n, 10));
|
|
268
|
+
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
|
|
269
|
+
const x = pa[i] || 0, y = pb[i] || 0;
|
|
270
|
+
if (x !== y)
|
|
271
|
+
return x - y;
|
|
272
|
+
}
|
|
273
|
+
return 0;
|
|
274
|
+
}
|
|
24
275
|
class ParticipantToken {
|
|
25
|
-
constructor({ name, projectId, apiKeyId, extra, grants, }) {
|
|
276
|
+
constructor({ name, projectId, apiKeyId, version, extra, grants, }) {
|
|
26
277
|
this.name = name;
|
|
27
278
|
this.projectId = projectId;
|
|
28
279
|
this.apiKeyId = apiKeyId;
|
|
280
|
+
this.version = version;
|
|
29
281
|
this.extra = extra ?? {};
|
|
30
282
|
this.grants = grants ?? [];
|
|
31
283
|
}
|
|
284
|
+
get role() {
|
|
285
|
+
for (const g of this.grants) {
|
|
286
|
+
if (g.name === "role" && g.scope !== "user") {
|
|
287
|
+
return String(g.scope);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return "user";
|
|
291
|
+
}
|
|
32
292
|
get isAgent() {
|
|
33
293
|
for (const grant of this.grants) {
|
|
34
294
|
if (grant.name === "role" && grant.scope === "agent") {
|
|
@@ -37,44 +297,136 @@ class ParticipantToken {
|
|
|
37
297
|
}
|
|
38
298
|
return false;
|
|
39
299
|
}
|
|
300
|
+
get isUser() {
|
|
301
|
+
for (const grant of this.grants) {
|
|
302
|
+
if (grant.name === "role" && grant.scope !== "user") {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
addTunnelGrant(ports) {
|
|
309
|
+
const portsStr = ports.join(",");
|
|
310
|
+
this.grants.push(new ParticipantGrant({ name: "tunnel_ports", scope: portsStr }));
|
|
311
|
+
}
|
|
40
312
|
addRoleGrant(role) {
|
|
41
313
|
this.grants.push(new ParticipantGrant({ name: "role", scope: role }));
|
|
42
314
|
}
|
|
43
315
|
addRoomGrant(roomName) {
|
|
44
316
|
this.grants.push(new ParticipantGrant({ name: "room", scope: roomName }));
|
|
45
317
|
}
|
|
318
|
+
addApiGrant(grant) {
|
|
319
|
+
this.grants.push(new ParticipantGrant({ name: "api", scope: grant }));
|
|
320
|
+
}
|
|
321
|
+
grantScope(name) {
|
|
322
|
+
return this.grants.find(g => g.name === name)?.scope;
|
|
323
|
+
}
|
|
324
|
+
getApiGrant() {
|
|
325
|
+
const api = this.grantScope("api");
|
|
326
|
+
if (api && typeof api !== "string") {
|
|
327
|
+
return api;
|
|
328
|
+
}
|
|
329
|
+
if (this.version && compareSemver(this.version, "0.6.0") < 0 && !api) {
|
|
330
|
+
return new ApiScope({
|
|
331
|
+
livekit: new LivekitGrant(),
|
|
332
|
+
queues: new QueuesGrant(),
|
|
333
|
+
messaging: new MessagingGrant(),
|
|
334
|
+
database: new DatabaseGrant(),
|
|
335
|
+
sync: new SyncGrant(),
|
|
336
|
+
storage: new StorageGrant(),
|
|
337
|
+
agents: new AgentsGrant(),
|
|
338
|
+
developer: new DeveloperGrant(),
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
return api;
|
|
342
|
+
}
|
|
46
343
|
toJson() {
|
|
47
|
-
|
|
344
|
+
const base = {
|
|
48
345
|
name: this.name,
|
|
49
|
-
|
|
50
|
-
...(this.apiKeyId ? { kid: this.apiKeyId } : {}),
|
|
51
|
-
grants: this.grants.map((g) => g.toJson()),
|
|
346
|
+
grants: this.grants.map(g => g.toJson()),
|
|
52
347
|
};
|
|
348
|
+
if (this.projectId) {
|
|
349
|
+
base["sub"] = this.projectId;
|
|
350
|
+
}
|
|
351
|
+
if (this.apiKeyId) {
|
|
352
|
+
base["kid"] = this.apiKeyId;
|
|
353
|
+
}
|
|
354
|
+
if (this.version) {
|
|
355
|
+
base["version"] = this.version;
|
|
356
|
+
}
|
|
357
|
+
return base;
|
|
53
358
|
}
|
|
54
|
-
async toJwt({ token }) {
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
359
|
+
async toJwt({ token, expiration, apiKey } = {}) {
|
|
360
|
+
let apiGrant = null;
|
|
361
|
+
for (const g of this.grants) {
|
|
362
|
+
if (g.name === "api") {
|
|
363
|
+
apiGrant = g;
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
if (!apiGrant && this.version && compareSemver(this.version, "0.5.3") >= 0) {
|
|
368
|
+
console.error("ParticipantToken.toJwt: No API grant found, but version is >= 0.5.3. " +
|
|
369
|
+
"This may cause issues with older clients that expect an API grant.");
|
|
370
|
+
}
|
|
371
|
+
let resolvedSecret = token;
|
|
372
|
+
let resolvedKid = this.apiKeyId;
|
|
373
|
+
let resolvedSub = this.projectId;
|
|
374
|
+
const apiKeyValue = apiKey ?? process.env.MESHAGENT_API_KEY;
|
|
375
|
+
if (apiKeyValue) {
|
|
376
|
+
const parsed = (0, api_keys_1.parseApiKey)(apiKeyValue);
|
|
377
|
+
resolvedSecret ?? (resolvedSecret = parsed.secret);
|
|
378
|
+
resolvedKid = parsed.id;
|
|
379
|
+
resolvedSub = parsed.projectId;
|
|
380
|
+
}
|
|
381
|
+
let usingDefaultSecret = false;
|
|
382
|
+
if (!resolvedSecret) {
|
|
383
|
+
const envSecret = process.env.MESHAGENT_SECRET;
|
|
384
|
+
if (!envSecret) {
|
|
385
|
+
throw new Error("ParticipantToken.toJwt: No secret provided. Pass `token`, `apiKey`, or set MESHAGENT_SECRET / MESHAGENT_API_KEY.");
|
|
386
|
+
}
|
|
387
|
+
resolvedSecret = envSecret;
|
|
388
|
+
usingDefaultSecret = true;
|
|
389
|
+
}
|
|
390
|
+
const secretKey = new TextEncoder().encode(resolvedSecret);
|
|
391
|
+
const payload = this.toJson();
|
|
392
|
+
if (resolvedSub) {
|
|
393
|
+
payload["sub"] = resolvedSub;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
delete payload["sub"];
|
|
397
|
+
}
|
|
398
|
+
if (usingDefaultSecret) {
|
|
399
|
+
delete payload["kid"];
|
|
400
|
+
}
|
|
401
|
+
else if (resolvedKid) {
|
|
402
|
+
payload["kid"] = resolvedKid;
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
delete payload["kid"];
|
|
406
|
+
}
|
|
407
|
+
if (expiration) {
|
|
408
|
+
payload.exp = Math.floor(expiration.getTime() / 1000);
|
|
409
|
+
}
|
|
410
|
+
const jwt = new jose_1.SignJWT(payload)
|
|
411
|
+
.setProtectedHeader({ alg: "HS256", typ: "JWT" });
|
|
412
|
+
const jwtToken = await jwt.sign(secretKey);
|
|
413
|
+
return jwtToken;
|
|
64
414
|
}
|
|
65
415
|
static fromJson(json) {
|
|
66
|
-
const { name, sub, grants, kid, ...rest } = json;
|
|
416
|
+
const { name, sub, grants, kid, version, ...rest } = json;
|
|
67
417
|
const extra = { ...rest };
|
|
418
|
+
const v = version ? version : "0.5.3";
|
|
68
419
|
return new ParticipantToken({
|
|
69
420
|
name: name,
|
|
70
421
|
projectId: sub,
|
|
71
422
|
apiKeyId: kid,
|
|
423
|
+
version: v,
|
|
72
424
|
grants: grants?.map((g) => ParticipantGrant.fromJson(g)),
|
|
73
425
|
extra,
|
|
74
426
|
});
|
|
75
427
|
}
|
|
76
|
-
static async fromJwt(jwtStr, options) {
|
|
77
|
-
const { token, verify = true } = options;
|
|
428
|
+
static async fromJwt(jwtStr, options = {}) {
|
|
429
|
+
const { token, verify = true } = options ?? {};
|
|
78
430
|
if (verify) {
|
|
79
431
|
const secretKey = new TextEncoder().encode(token);
|
|
80
432
|
const { payload } = await (0, jose_1.jwtVerify)(jwtStr, secretKey, {
|
|
@@ -38,10 +38,7 @@ class StreamController {
|
|
|
38
38
|
},
|
|
39
39
|
async return() {
|
|
40
40
|
cleanup();
|
|
41
|
-
return {
|
|
42
|
-
done: true,
|
|
43
|
-
value: undefined
|
|
44
|
-
};
|
|
41
|
+
return { done: true, value: undefined };
|
|
45
42
|
},
|
|
46
43
|
async throw(e) {
|
|
47
44
|
cleanup();
|
|
@@ -58,10 +55,7 @@ class StreamController {
|
|
|
58
55
|
if (sub.waiter) {
|
|
59
56
|
const w = sub.waiter;
|
|
60
57
|
sub.waiter = null;
|
|
61
|
-
|
|
62
|
-
w({ done: false, value });
|
|
63
|
-
}
|
|
64
|
-
catch { }
|
|
58
|
+
w({ done: false, value });
|
|
65
59
|
}
|
|
66
60
|
else {
|
|
67
61
|
sub.queue.push(value);
|
|
@@ -76,12 +70,7 @@ class StreamController {
|
|
|
76
70
|
if (sub.waiter) {
|
|
77
71
|
const w = sub.waiter;
|
|
78
72
|
sub.waiter = null;
|
|
79
|
-
|
|
80
|
-
w({ done: true, value: undefined });
|
|
81
|
-
}
|
|
82
|
-
catch { }
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
73
|
+
w({ done: true, value: undefined });
|
|
85
74
|
}
|
|
86
75
|
}
|
|
87
76
|
}
|