oorja 1.11.4 → 2.0.1
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.md +14 -52
- package/dist/commands/signout.d.ts +1 -1
- package/dist/commands/signout.js +4 -4
- package/dist/commands/teletype/index.d.ts +7 -6
- package/dist/commands/teletype/index.js +49 -47
- package/dist/lib/config.d.ts +7 -10
- package/dist/lib/config.js +29 -49
- package/dist/lib/{surya → connect}/index.d.ts +8 -8
- package/dist/lib/{surya → connect}/index.js +39 -36
- package/dist/lib/{surya → connect}/resources.js +2 -2
- package/dist/lib/{surya → connect}/types.d.ts +1 -3
- package/dist/lib/encryption.d.ts +2 -3
- package/dist/lib/encryption.js +6 -10
- package/dist/lib/oorja/client.d.ts +3 -0
- package/dist/lib/oorja/client.js +52 -0
- package/dist/lib/oorja/index.d.ts +7 -7
- package/dist/lib/oorja/index.js +40 -29
- package/dist/lib/oorja/preflight.d.ts +8 -8
- package/dist/lib/oorja/preflight.js +45 -41
- package/dist/lib/teletype/auxiliary.d.ts +2 -2
- package/dist/lib/teletype/auxiliary.js +5 -5
- package/dist/lib/teletype/index.d.ts +3 -4
- package/dist/lib/teletype/index.js +16 -16
- package/dist/lib/utils.js +5 -5
- package/oclif.manifest.json +11 -10
- package/package.json +22 -18
- package/dist/lib/surya/vendor/phoenix/ajax.d.ts +0 -8
- package/dist/lib/surya/vendor/phoenix/ajax.js +0 -82
- package/dist/lib/surya/vendor/phoenix/channel.d.ts +0 -154
- package/dist/lib/surya/vendor/phoenix/channel.js +0 -308
- package/dist/lib/surya/vendor/phoenix/constants.d.ts +0 -33
- package/dist/lib/surya/vendor/phoenix/constants.js +0 -29
- package/dist/lib/surya/vendor/phoenix/index.d.ts +0 -199
- package/dist/lib/surya/vendor/phoenix/index.js +0 -200
- package/dist/lib/surya/vendor/phoenix/longpoll.d.ts +0 -12
- package/dist/lib/surya/vendor/phoenix/longpoll.js +0 -126
- package/dist/lib/surya/vendor/phoenix/presence.d.ts +0 -44
- package/dist/lib/surya/vendor/phoenix/presence.js +0 -152
- package/dist/lib/surya/vendor/phoenix/push.d.ts +0 -57
- package/dist/lib/surya/vendor/phoenix/push.js +0 -122
- package/dist/lib/surya/vendor/phoenix/serializer.d.ts +0 -53
- package/dist/lib/surya/vendor/phoenix/serializer.js +0 -100
- package/dist/lib/surya/vendor/phoenix/socket.d.ts +0 -222
- package/dist/lib/surya/vendor/phoenix/socket.js +0 -541
- package/dist/lib/surya/vendor/phoenix/timer.d.ts +0 -25
- package/dist/lib/surya/vendor/phoenix/timer.js +0 -40
- package/dist/lib/surya/vendor/phoenix/utils.d.ts +0 -1
- package/dist/lib/surya/vendor/phoenix/utils.js +0 -11
- /package/dist/lib/{surya → connect}/errors.d.ts +0 -0
- /package/dist/lib/{surya → connect}/errors.js +0 -0
- /package/dist/lib/{surya → connect}/resources.d.ts +0 -0
- /package/dist/lib/{surya → connect}/types.js +0 -0
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
// backend api client
|
|
2
|
-
import https from
|
|
3
|
-
import axios, { AxiosError } from
|
|
4
|
-
import { encode, decode } from
|
|
5
|
-
import { defaultParser } from
|
|
6
|
-
import {
|
|
7
|
-
import { Unauthorized, BadRequest } from
|
|
8
|
-
import { Socket, Presence } from
|
|
9
|
-
import camelcaseKeys from
|
|
10
|
-
export class
|
|
2
|
+
import https from 'https';
|
|
3
|
+
import axios, { AxiosError } from 'axios';
|
|
4
|
+
import { encode, decode } from '@msgpack/msgpack';
|
|
5
|
+
import { defaultParser } from './resources.js';
|
|
6
|
+
import { getConnectConfig } from '../config.js';
|
|
7
|
+
import { Unauthorized, BadRequest } from './errors.js';
|
|
8
|
+
import { Socket, Presence } from 'phoenix';
|
|
9
|
+
import camelcaseKeys from 'camelcase-keys';
|
|
10
|
+
export class ApiClientError extends Error {
|
|
11
11
|
}
|
|
12
|
-
export class
|
|
12
|
+
export class ConnectClient {
|
|
13
13
|
config;
|
|
14
14
|
client;
|
|
15
15
|
socket;
|
|
16
|
-
constructor(env) {
|
|
17
|
-
const config =
|
|
16
|
+
constructor(env, region) {
|
|
17
|
+
const config = getConnectConfig(env, region);
|
|
18
18
|
this.client = axios.create({
|
|
19
19
|
httpsAgent: new https.Agent({
|
|
20
|
-
minVersion:
|
|
21
|
-
maxVersion:
|
|
20
|
+
minVersion: 'TLSv1.2',
|
|
21
|
+
maxVersion: 'TLSv1.2',
|
|
22
22
|
}),
|
|
23
|
-
baseURL:
|
|
23
|
+
baseURL: connectBaseURL(config.host),
|
|
24
24
|
timeout: 5000,
|
|
25
|
-
responseType:
|
|
25
|
+
responseType: 'json',
|
|
26
26
|
headers: {
|
|
27
|
-
|
|
27
|
+
'x-access-token': config.token || '',
|
|
28
28
|
},
|
|
29
29
|
});
|
|
30
30
|
this.config = config;
|
|
31
31
|
}
|
|
32
32
|
fetchCliManifest = async () => {
|
|
33
33
|
try {
|
|
34
|
-
const response = await this.client.get(
|
|
34
|
+
const response = await this.client.get('/cli');
|
|
35
35
|
return camelcaseKeys(response.data);
|
|
36
36
|
}
|
|
37
37
|
catch (error) {
|
|
@@ -40,7 +40,7 @@ export class SuryaClient {
|
|
|
40
40
|
};
|
|
41
41
|
fetchSessionUser = async () => {
|
|
42
42
|
try {
|
|
43
|
-
const response = await this.client.get(
|
|
43
|
+
const response = await this.client.get('/session/user');
|
|
44
44
|
return defaultParser(response.data.data);
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
@@ -52,11 +52,11 @@ export class SuryaClient {
|
|
|
52
52
|
room: {
|
|
53
53
|
apps,
|
|
54
54
|
locked: false,
|
|
55
|
-
name: roomName ||
|
|
55
|
+
name: roomName || '-',
|
|
56
56
|
},
|
|
57
57
|
};
|
|
58
58
|
try {
|
|
59
|
-
const response = await this.client.post(
|
|
59
|
+
const response = await this.client.post('/rooms', body);
|
|
60
60
|
return defaultParser(response.data.data);
|
|
61
61
|
}
|
|
62
62
|
catch (error) {
|
|
@@ -65,7 +65,7 @@ export class SuryaClient {
|
|
|
65
65
|
};
|
|
66
66
|
createAnonymousUser = async () => {
|
|
67
67
|
try {
|
|
68
|
-
const response = await this.client.post(
|
|
68
|
+
const response = await this.client.post('/session/anon');
|
|
69
69
|
return response.data.access_token;
|
|
70
70
|
}
|
|
71
71
|
catch (error) {
|
|
@@ -74,7 +74,7 @@ export class SuryaClient {
|
|
|
74
74
|
};
|
|
75
75
|
accessTokenFromRoomParticipantOTP = async (roomId, otp) => {
|
|
76
76
|
try {
|
|
77
|
-
const response = await this.client.post(
|
|
77
|
+
const response = await this.client.post('/access_tokens/from_room_participant_otp', {
|
|
78
78
|
room_id: roomId,
|
|
79
79
|
otp: otp,
|
|
80
80
|
});
|
|
@@ -94,7 +94,7 @@ export class SuryaClient {
|
|
|
94
94
|
}
|
|
95
95
|
};
|
|
96
96
|
establishSocket = async () => {
|
|
97
|
-
const protocolPrefix =
|
|
97
|
+
const protocolPrefix = `wss://`;
|
|
98
98
|
const host = this.config.host;
|
|
99
99
|
let encodeMessage = (rawdata, callback) => {
|
|
100
100
|
if (!rawdata)
|
|
@@ -113,8 +113,9 @@ export class SuryaClient {
|
|
|
113
113
|
params: {
|
|
114
114
|
access_token: this.config.token,
|
|
115
115
|
},
|
|
116
|
+
transport: WebSocket,
|
|
116
117
|
heartbeatIntervalMs: 10000,
|
|
117
|
-
binaryType:
|
|
118
|
+
binaryType: 'arraybuffer',
|
|
118
119
|
encode: encodeMessage,
|
|
119
120
|
decode: decodeMessage,
|
|
120
121
|
});
|
|
@@ -127,7 +128,7 @@ export class SuryaClient {
|
|
|
127
128
|
reject();
|
|
128
129
|
return;
|
|
129
130
|
}
|
|
130
|
-
console.error(
|
|
131
|
+
console.error('connection error');
|
|
131
132
|
process.exit(2);
|
|
132
133
|
});
|
|
133
134
|
// @ts-ignore
|
|
@@ -136,33 +137,33 @@ export class SuryaClient {
|
|
|
136
137
|
};
|
|
137
138
|
joinChannel = ({ channel, params, onJoin, onClose, onError, onMessage, handleSessionJoin, handleSessionLeave, }) => {
|
|
138
139
|
if (!this.socket)
|
|
139
|
-
throw Error(
|
|
140
|
+
throw Error('no socket connection');
|
|
140
141
|
const chan = this.socket.channel(channel, params);
|
|
141
142
|
if (onError)
|
|
142
143
|
chan.onError(onError);
|
|
143
144
|
if (onClose)
|
|
144
145
|
chan.onClose(onClose);
|
|
145
146
|
let presences = [];
|
|
146
|
-
chan.on(
|
|
147
|
+
chan.on('new_msg', (msg) => {
|
|
147
148
|
onMessage(msg);
|
|
148
149
|
});
|
|
149
|
-
chan.on(
|
|
150
|
+
chan.on('presence_state', (response) => {
|
|
150
151
|
Presence.syncState(presences, response, handleSessionJoin, undefined);
|
|
151
152
|
presences = response;
|
|
152
153
|
});
|
|
153
|
-
chan.on(
|
|
154
|
+
chan.on('presence_diff', (newPresence) => {
|
|
154
155
|
presences = Presence.syncDiff(presences, newPresence, handleSessionJoin, handleSessionLeave);
|
|
155
156
|
});
|
|
156
157
|
chan
|
|
157
158
|
.join()
|
|
158
|
-
.receive(
|
|
159
|
+
.receive('ok', () => {
|
|
159
160
|
if (onJoin)
|
|
160
161
|
onJoin();
|
|
161
162
|
})
|
|
162
|
-
.receive(
|
|
163
|
-
if (resp && resp.reason ===
|
|
163
|
+
.receive('error', (resp) => {
|
|
164
|
+
if (resp && resp.reason === 'unauthorized') {
|
|
164
165
|
if (onError)
|
|
165
|
-
onError(new Unauthorized(
|
|
166
|
+
onError(new Unauthorized('unauthorized: user needs to join the room before a stream can be started.'));
|
|
166
167
|
return;
|
|
167
168
|
}
|
|
168
169
|
process.exit(3);
|
|
@@ -172,7 +173,9 @@ export class SuryaClient {
|
|
|
172
173
|
destroy = async () => {
|
|
173
174
|
return new Promise((resolve) => {
|
|
174
175
|
if (this.socket) {
|
|
175
|
-
this.socket.disconnect(
|
|
176
|
+
this.socket.disconnect(() => {
|
|
177
|
+
resolve(undefined);
|
|
178
|
+
}, 1000, 'disconnecting connect client');
|
|
176
179
|
}
|
|
177
180
|
else {
|
|
178
181
|
resolve(undefined);
|
|
@@ -180,7 +183,7 @@ export class SuryaClient {
|
|
|
180
183
|
});
|
|
181
184
|
};
|
|
182
185
|
}
|
|
183
|
-
const
|
|
186
|
+
const connectBaseURL = (host) => `https://${host}/api/v1`;
|
|
184
187
|
const handleError = (error) => {
|
|
185
188
|
if (error instanceof AxiosError) {
|
|
186
189
|
const { response } = error;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import camelcaseKeys from
|
|
2
|
-
const parseDateFields = (resource, fields = [
|
|
1
|
+
import camelcaseKeys from 'camelcase-keys';
|
|
2
|
+
const parseDateFields = (resource, fields = ['createdAt', 'updatedAt']) => {
|
|
3
3
|
fields.forEach((key) => {
|
|
4
4
|
const val = resource[key];
|
|
5
5
|
if (val)
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
type ProfileType = "GitHub" | "Google" | "anon";
|
|
1
|
+
type ProfileType = 'GitHub' | 'Google' | 'anon';
|
|
3
2
|
export type RoomApp = {
|
|
4
3
|
appId: string;
|
|
5
4
|
config: any;
|
|
@@ -21,7 +20,6 @@ type ResourceTimestamps = {
|
|
|
21
20
|
};
|
|
22
21
|
export type CliManifest = {
|
|
23
22
|
cliVersion: number;
|
|
24
|
-
suryaHosts: string[];
|
|
25
23
|
};
|
|
26
24
|
export type User = UserProfile & ResourceTimestamps;
|
|
27
25
|
export type Room = {
|
package/dist/lib/encryption.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import { RoomKey } from "./surya/types.js";
|
|
1
|
+
import { RoomKey } from './connect/types.js';
|
|
3
2
|
export type EncryptedPayload = {
|
|
4
3
|
iv: Buffer | Uint8Array;
|
|
5
4
|
data: Buffer | Uint8Array;
|
|
6
5
|
};
|
|
7
6
|
export declare const createRoomKey: (roomId: string) => RoomKey;
|
|
8
7
|
export declare const exportKey: (key: Buffer) => string;
|
|
9
|
-
export declare const importKey: (exportedKey: string) => Buffer
|
|
8
|
+
export declare const importKey: (exportedKey: string) => Buffer<ArrayBuffer>;
|
|
10
9
|
export declare const encrypt: (data: any, roomKey: RoomKey) => EncryptedPayload;
|
|
11
10
|
export declare const decrypt: (payload: EncryptedPayload, roomKey: RoomKey) => any;
|
package/dist/lib/encryption.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as crypto from
|
|
2
|
-
import { encode, decode } from
|
|
3
|
-
const ALG =
|
|
1
|
+
import * as crypto from 'crypto';
|
|
2
|
+
import { encode, decode } from '@msgpack/msgpack';
|
|
3
|
+
const ALG = 'aes-128-gcm';
|
|
4
4
|
const KEY_SIZE = 16; // 128 bit AES
|
|
5
5
|
const AUTH_TAG_LEN = 16;
|
|
6
6
|
export const createRoomKey = (roomId) => ({
|
|
@@ -8,19 +8,15 @@ export const createRoomKey = (roomId) => ({
|
|
|
8
8
|
key: createAESEncryptionKey(),
|
|
9
9
|
});
|
|
10
10
|
const createAESEncryptionKey = () => crypto.randomBytes(KEY_SIZE);
|
|
11
|
-
export const exportKey = (key) => key.toString(
|
|
12
|
-
export const importKey = (exportedKey) => Buffer.from(exportedKey,
|
|
11
|
+
export const exportKey = (key) => key.toString('base64');
|
|
12
|
+
export const importKey = (exportedKey) => Buffer.from(exportedKey, 'base64');
|
|
13
13
|
const createIV = () => crypto.randomBytes(16);
|
|
14
14
|
export const encrypt = (data, roomKey) => {
|
|
15
15
|
const iv = createIV();
|
|
16
16
|
const cipher = crypto.createCipheriv(ALG, roomKey.key, iv);
|
|
17
17
|
return {
|
|
18
18
|
iv,
|
|
19
|
-
data: Buffer.concat([
|
|
20
|
-
cipher.update(encode(data)),
|
|
21
|
-
cipher.final(),
|
|
22
|
-
cipher.getAuthTag(),
|
|
23
|
-
]),
|
|
19
|
+
data: Buffer.concat([cipher.update(encode(data)), cipher.final(), cipher.getAuthTag()]),
|
|
24
20
|
};
|
|
25
21
|
};
|
|
26
22
|
export const decrypt = (payload, roomKey) => {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import haversine from 'haversine-distance';
|
|
2
|
+
export class OorjaClientError extends Error {
|
|
3
|
+
}
|
|
4
|
+
const _maybeError = (response) => {
|
|
5
|
+
const { status } = response;
|
|
6
|
+
if (status >= 400) {
|
|
7
|
+
throw new OorjaClientError('oorja network client error');
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
class Client {
|
|
11
|
+
async get(url, options = {}) {
|
|
12
|
+
const response = await fetch(url, {
|
|
13
|
+
...options,
|
|
14
|
+
method: options.method || 'GET',
|
|
15
|
+
headers: { ...options.headers },
|
|
16
|
+
cache: 'no-store',
|
|
17
|
+
redirect: 'follow',
|
|
18
|
+
signal: AbortSignal.timeout(options.timeout || 6_000),
|
|
19
|
+
});
|
|
20
|
+
_maybeError(response);
|
|
21
|
+
return response;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const _client = new Client();
|
|
25
|
+
export const getRegion = async () => {
|
|
26
|
+
const response = await _client.get('https://oorja.io/nudge', {
|
|
27
|
+
method: 'GET',
|
|
28
|
+
});
|
|
29
|
+
if (response.status !== 200) {
|
|
30
|
+
console.error('There seems to be an issue with the network');
|
|
31
|
+
process.exit(1);
|
|
32
|
+
}
|
|
33
|
+
const settings = (await response.json());
|
|
34
|
+
try {
|
|
35
|
+
const clientLocation = {
|
|
36
|
+
lat: parseFloat(response.headers.get('oorja-lat')),
|
|
37
|
+
lng: parseFloat(response.headers.get('oorja-lon')),
|
|
38
|
+
};
|
|
39
|
+
const distances = settings.regions.map((region) => {
|
|
40
|
+
return {
|
|
41
|
+
name: region.name,
|
|
42
|
+
distance: haversine(clientLocation, region.location),
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
const minDistance = distances.reduce((a, b) => (a.distance < b.distance ? a : b));
|
|
46
|
+
return minDistance.name;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
console.error('error determining region');
|
|
50
|
+
process.exit(1);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { oorjaConfig } from
|
|
2
|
-
import { User, RoomKey } from
|
|
3
|
-
import { TeletypeOptions } from
|
|
4
|
-
import { CreateRoomOptions,
|
|
1
|
+
import { oorjaConfig } from '../config.js';
|
|
2
|
+
import { User, RoomKey } from '../connect/types.js';
|
|
3
|
+
import { TeletypeOptions } from '../teletype/index.js';
|
|
4
|
+
import { CreateRoomOptions, ConnectClient } from '../connect/index.js';
|
|
5
5
|
export declare class InvalidRoomLink extends Error {
|
|
6
6
|
}
|
|
7
7
|
declare class OORJA {
|
|
8
8
|
private config;
|
|
9
|
-
private
|
|
9
|
+
private connectClient;
|
|
10
10
|
user: User;
|
|
11
|
-
constructor(config: oorjaConfig,
|
|
11
|
+
constructor(config: oorjaConfig, connectClient: ConnectClient, user: User);
|
|
12
12
|
createRoom: (options: CreateRoomOptions) => Promise<{
|
|
13
|
-
room: import("../
|
|
13
|
+
room: import("../connect/types.js").Room;
|
|
14
14
|
roomKey: RoomKey;
|
|
15
15
|
}>;
|
|
16
16
|
linkForRoom: (roomKey: RoomKey) => string;
|
package/dist/lib/oorja/index.js
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import { determineENV, getoorjaConfig, INVALID_ROOM_LINK_MESSAGE, setENVAccessToken, } from
|
|
2
|
-
import { teletypeApp } from
|
|
3
|
-
import {
|
|
4
|
-
import { URL, URLSearchParams } from
|
|
5
|
-
import { importKey, createRoomKey, exportKey } from
|
|
6
|
-
import { loginByRoomOTP, preflight, promptAuth, resumeSession, validateCliVersion
|
|
1
|
+
import { determineENV, getoorjaConfig, INVALID_ROOM_LINK_MESSAGE, setENVAccessToken, } from '../config.js';
|
|
2
|
+
import { teletypeApp } from '../teletype/index.js';
|
|
3
|
+
import { ConnectClient } from '../connect/index.js';
|
|
4
|
+
import { URL, URLSearchParams } from 'url';
|
|
5
|
+
import { importKey, createRoomKey, exportKey } from '../encryption.js';
|
|
6
|
+
import { loginByRoomOTP, preflight, promptAuth, resumeSession, validateCliVersion } from './preflight.js';
|
|
7
|
+
import { getRegion } from './client.js';
|
|
8
|
+
import ora from 'ora';
|
|
7
9
|
export class InvalidRoomLink extends Error {
|
|
8
10
|
}
|
|
9
11
|
class OORJA {
|
|
10
12
|
config;
|
|
11
|
-
|
|
13
|
+
connectClient;
|
|
12
14
|
user;
|
|
13
15
|
// should capture domain related commands and queries
|
|
14
|
-
constructor(config,
|
|
16
|
+
constructor(config, connectClient, user) {
|
|
15
17
|
this.config = config;
|
|
16
|
-
this.
|
|
18
|
+
this.connectClient = connectClient;
|
|
17
19
|
this.user = user;
|
|
18
20
|
}
|
|
19
21
|
createRoom = async (options) => {
|
|
20
|
-
const room = await this.
|
|
22
|
+
const room = await this.connectClient.createRoom(options);
|
|
21
23
|
const roomKey = createRoomKey(room.id);
|
|
22
24
|
return {
|
|
23
25
|
room,
|
|
@@ -37,7 +39,7 @@ class OORJA {
|
|
|
37
39
|
teletype = (options) => {
|
|
38
40
|
return teletypeApp({
|
|
39
41
|
userId: this.user.id,
|
|
40
|
-
joinChannel: this.
|
|
42
|
+
joinChannel: this.connectClient.joinChannel,
|
|
41
43
|
...options,
|
|
42
44
|
});
|
|
43
45
|
};
|
|
@@ -52,48 +54,57 @@ const parseRoomURL = (roomLink) => {
|
|
|
52
54
|
};
|
|
53
55
|
const getRoomId = (roomURL) => {
|
|
54
56
|
const params = new URLSearchParams(roomURL.search);
|
|
55
|
-
return params.get(
|
|
57
|
+
return params.get('id') || undefined;
|
|
56
58
|
};
|
|
57
59
|
const oorjaURL = (config) => {
|
|
58
|
-
const { host
|
|
59
|
-
return
|
|
60
|
+
const { host } = config;
|
|
61
|
+
return `https://${host}`;
|
|
60
62
|
};
|
|
61
63
|
const linkForTokenGen = (config) => `${oorjaURL(config)}/access_token`;
|
|
62
64
|
const init = async (env, options = {}) => {
|
|
63
65
|
const config = getoorjaConfig(env);
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
const spinner = ora({
|
|
67
|
+
text: 'Connecting...',
|
|
68
|
+
discardStdin: true,
|
|
69
|
+
}).start();
|
|
70
|
+
const region = await getRegion();
|
|
71
|
+
let connectClient = new ConnectClient(env, region);
|
|
72
|
+
await validateCliVersion(connectClient);
|
|
73
|
+
let user = await resumeSession(env, connectClient, options.roomId);
|
|
74
|
+
spinner.succeed('Online');
|
|
67
75
|
if (!user) {
|
|
68
|
-
let token =
|
|
76
|
+
let token = '';
|
|
69
77
|
if (options.roomId) {
|
|
70
|
-
token = await loginByRoomOTP(
|
|
78
|
+
token = await loginByRoomOTP(connectClient, options.roomId);
|
|
71
79
|
}
|
|
72
80
|
else {
|
|
73
|
-
token = await promptAuth(
|
|
81
|
+
token = await promptAuth(connectClient, linkForTokenGen(config));
|
|
74
82
|
if (!token) {
|
|
75
|
-
console.log(
|
|
83
|
+
console.log('Token not provided :(');
|
|
76
84
|
process.exit(12);
|
|
77
85
|
}
|
|
78
86
|
}
|
|
79
87
|
setENVAccessToken(env, token);
|
|
80
88
|
}
|
|
81
|
-
await
|
|
82
|
-
|
|
83
|
-
user = await preflight(env,
|
|
84
|
-
return new OORJA(config,
|
|
89
|
+
await connectClient.destroy();
|
|
90
|
+
connectClient = new ConnectClient(env, region);
|
|
91
|
+
user = await preflight(env, connectClient);
|
|
92
|
+
return new OORJA(config, connectClient, user);
|
|
85
93
|
};
|
|
86
94
|
let currentEnv;
|
|
87
|
-
let oorja;
|
|
95
|
+
let oorja = null;
|
|
88
96
|
export const getApp = async (options = {}) => {
|
|
89
97
|
const { roomLink } = options;
|
|
90
98
|
const roomURL = roomLink ? parseRoomURL(roomLink) : undefined;
|
|
91
|
-
const env = determineENV(
|
|
99
|
+
const env = determineENV(roomURL);
|
|
92
100
|
if (oorja) {
|
|
93
101
|
if (env !== currentEnv) {
|
|
94
|
-
return Promise.reject(
|
|
102
|
+
return Promise.reject('Attempt to run different env in same session');
|
|
95
103
|
}
|
|
96
104
|
return Promise.resolve(oorja);
|
|
97
105
|
}
|
|
98
|
-
|
|
106
|
+
const app = await init(env, { roomId: roomURL ? getRoomId(roomURL) : undefined });
|
|
107
|
+
currentEnv = env;
|
|
108
|
+
oorja = app;
|
|
109
|
+
return app;
|
|
99
110
|
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { env } from
|
|
2
|
-
import {
|
|
3
|
-
import { User } from
|
|
1
|
+
import { env } from '../config.js';
|
|
2
|
+
import { ConnectClient } from '../connect/index.js';
|
|
3
|
+
import { User } from '../connect/types.js';
|
|
4
4
|
export declare const promptRoomParticipantOTP: () => Promise<string>;
|
|
5
|
-
export declare const promptAuth: (
|
|
6
|
-
export declare const loginByRoomOTP: (
|
|
7
|
-
export declare const validateCliVersion: (
|
|
8
|
-
export declare const resumeSession: (env: env,
|
|
9
|
-
export declare const preflight: (env: env,
|
|
5
|
+
export declare const promptAuth: (connectClient: ConnectClient, generateTokenLink: string) => Promise<string>;
|
|
6
|
+
export declare const loginByRoomOTP: (connectClient: ConnectClient, roomId: string) => Promise<string>;
|
|
7
|
+
export declare const validateCliVersion: (connectClient: ConnectClient) => Promise<void>;
|
|
8
|
+
export declare const resumeSession: (env: env, connectClient: ConnectClient, roomId?: string) => Promise<User | null>;
|
|
9
|
+
export declare const preflight: (env: env, connectClient: ConnectClient) => Promise<User>;
|
|
@@ -1,124 +1,128 @@
|
|
|
1
|
-
import chalk from
|
|
1
|
+
import chalk from 'chalk';
|
|
2
2
|
import inquirer from 'inquirer';
|
|
3
|
-
import ora from
|
|
4
|
-
import { setENVAccessToken, CLI_VERSION, getENVAccessToken
|
|
5
|
-
import { BadRequest, Unauthorized } from
|
|
6
|
-
const promptToken = () => inquirer
|
|
3
|
+
import ora from 'ora';
|
|
4
|
+
import { setENVAccessToken, CLI_VERSION, getENVAccessToken } from '../config.js';
|
|
5
|
+
import { BadRequest, Unauthorized } from '../connect/errors.js';
|
|
6
|
+
const promptToken = () => inquirer
|
|
7
|
+
.prompt([
|
|
7
8
|
{
|
|
8
9
|
type: 'input',
|
|
9
10
|
name: 'accessToken',
|
|
10
11
|
message: 'Please enter your access token for authentication:',
|
|
11
|
-
}
|
|
12
|
-
])
|
|
13
|
-
|
|
12
|
+
},
|
|
13
|
+
])
|
|
14
|
+
.then((answers) => answers.accessToken);
|
|
15
|
+
export const promptRoomParticipantOTP = () => inquirer
|
|
16
|
+
.prompt([
|
|
14
17
|
{
|
|
15
18
|
type: 'input',
|
|
16
19
|
name: 'otp',
|
|
17
20
|
message: 'Please enter your OTP for authentication (switch to teletype app in the room and click on blue "Generate OTP" button):',
|
|
18
|
-
}
|
|
19
|
-
])
|
|
21
|
+
},
|
|
22
|
+
])
|
|
23
|
+
.then((answers) => answers.otp);
|
|
20
24
|
const OTP_HELP_MESSAGE = "You can generate OTP from the room, it's in the instruction steps";
|
|
21
|
-
export const promptAuth = async (
|
|
22
|
-
const ANON =
|
|
23
|
-
const SIGN_IN =
|
|
24
|
-
console.log(`\n${chalk.bold(
|
|
25
|
+
export const promptAuth = async (connectClient, generateTokenLink) => {
|
|
26
|
+
const ANON = 'Proceed as an anonymous user';
|
|
27
|
+
const SIGN_IN = 'Sign-in with oorja';
|
|
28
|
+
console.log(`\n${chalk.bold('PRO-TIP:')} If you sign-in, you can control your shell from the web-ui as well, without enabling collaboration mode for the other participants\n`);
|
|
25
29
|
const { answer } = await inquirer.prompt([
|
|
26
30
|
{
|
|
27
31
|
type: 'list',
|
|
28
32
|
name: 'answer',
|
|
29
33
|
message: 'You need an access-token for authentication.\n ',
|
|
30
34
|
choices: [ANON, SIGN_IN],
|
|
31
|
-
}
|
|
35
|
+
},
|
|
32
36
|
]);
|
|
33
37
|
switch (answer) {
|
|
34
38
|
case ANON:
|
|
35
|
-
console.log(
|
|
36
|
-
return
|
|
39
|
+
console.log('Creating anonymous user...');
|
|
40
|
+
return connectClient.createAnonymousUser();
|
|
37
41
|
case SIGN_IN:
|
|
38
42
|
console.log(`You can sign-in and generate your token here: ${chalk.blue(generateTokenLink)}`);
|
|
39
43
|
return promptToken();
|
|
40
44
|
}
|
|
41
|
-
throw Error(
|
|
45
|
+
throw Error('Unexpected input');
|
|
42
46
|
};
|
|
43
|
-
export const loginByRoomOTP = async (
|
|
47
|
+
export const loginByRoomOTP = async (connectClient, roomId) => {
|
|
44
48
|
const otp = await promptRoomParticipantOTP();
|
|
45
49
|
if (!otp) {
|
|
46
|
-
console.log(
|
|
50
|
+
console.log('OTP not provided :(');
|
|
47
51
|
console.log(OTP_HELP_MESSAGE);
|
|
48
52
|
process.exit(213);
|
|
49
53
|
}
|
|
50
54
|
try {
|
|
51
|
-
return await
|
|
55
|
+
return await connectClient.accessTokenFromRoomParticipantOTP(roomId, otp);
|
|
52
56
|
}
|
|
53
57
|
catch (e) {
|
|
54
58
|
if (e instanceof BadRequest) {
|
|
55
|
-
console.log(chalk.redBright(
|
|
59
|
+
console.log(chalk.redBright('Invalid otp. It may have expired.'));
|
|
56
60
|
console.log(OTP_HELP_MESSAGE);
|
|
57
61
|
process.exit();
|
|
58
62
|
}
|
|
59
63
|
throw e;
|
|
60
64
|
}
|
|
61
65
|
};
|
|
62
|
-
export const validateCliVersion = async (
|
|
63
|
-
const manifest = await
|
|
66
|
+
export const validateCliVersion = async (connectClient) => {
|
|
67
|
+
const manifest = await connectClient.fetchCliManifest();
|
|
64
68
|
if (manifest.cliVersion > CLI_VERSION) {
|
|
65
|
-
console.log(chalk.redBright(
|
|
69
|
+
console.log(chalk.redBright('Your oorja cli is outdated. Please run: npm update -g oorja'));
|
|
66
70
|
process.exit(1);
|
|
67
71
|
}
|
|
68
72
|
};
|
|
69
|
-
export const resumeSession = async (env,
|
|
73
|
+
export const resumeSession = async (env, connectClient, roomId) => {
|
|
70
74
|
const token = getENVAccessToken(env);
|
|
71
75
|
if (!token)
|
|
72
76
|
return null;
|
|
73
77
|
// try to validate authorization with existing token
|
|
74
78
|
try {
|
|
75
|
-
const user = await
|
|
79
|
+
const user = await connectClient.fetchSessionUser();
|
|
76
80
|
if (roomId) {
|
|
77
|
-
await
|
|
81
|
+
await connectClient.fetchRoom(roomId);
|
|
78
82
|
}
|
|
79
83
|
return user;
|
|
80
84
|
}
|
|
81
85
|
catch (e) {
|
|
82
86
|
if (e instanceof Unauthorized) {
|
|
83
|
-
setENVAccessToken(env,
|
|
87
|
+
setENVAccessToken(env, '');
|
|
84
88
|
return null;
|
|
85
89
|
}
|
|
86
90
|
throw e;
|
|
87
91
|
}
|
|
88
92
|
};
|
|
89
|
-
export const preflight = async (env,
|
|
93
|
+
export const preflight = async (env, connectClient) => {
|
|
90
94
|
const spinner = ora({
|
|
91
|
-
text:
|
|
95
|
+
text: 'Authenticating',
|
|
92
96
|
discardStdin: false,
|
|
93
97
|
}).start();
|
|
94
98
|
try {
|
|
95
|
-
const user = await
|
|
99
|
+
const user = await connectClient.fetchSessionUser();
|
|
96
100
|
spinner.succeed(`Authenticated: Welcome ${user.name}`);
|
|
97
|
-
if (user.profileType ===
|
|
101
|
+
if (user.profileType === 'anon') {
|
|
98
102
|
// don't persist tokens for anonymous users
|
|
99
|
-
setENVAccessToken(env,
|
|
103
|
+
setENVAccessToken(env, '');
|
|
100
104
|
console.log(chalk.yellowBright("You're an anonymous user. CLI will not remember the auth-token"));
|
|
101
105
|
}
|
|
102
|
-
spinner.start(
|
|
103
|
-
return
|
|
106
|
+
spinner.start('Connecting..');
|
|
107
|
+
return connectClient
|
|
104
108
|
.establishSocket()
|
|
105
109
|
.then(() => {
|
|
106
|
-
spinner.succeed(
|
|
110
|
+
spinner.succeed('Connected').clear();
|
|
107
111
|
return user;
|
|
108
112
|
})
|
|
109
113
|
.catch((e) => {
|
|
110
|
-
spinner.fail(
|
|
114
|
+
spinner.fail('Socket connection failure..');
|
|
111
115
|
throw e;
|
|
112
116
|
});
|
|
113
117
|
}
|
|
114
118
|
catch (e) {
|
|
115
|
-
setENVAccessToken(env,
|
|
119
|
+
setENVAccessToken(env, '');
|
|
116
120
|
if (e instanceof Unauthorized) {
|
|
117
|
-
spinner.fail(
|
|
121
|
+
spinner.fail('Your access token failed authentication, resetting...');
|
|
118
122
|
process.exit(33);
|
|
119
123
|
}
|
|
120
124
|
else {
|
|
121
|
-
spinner.fail(
|
|
125
|
+
spinner.fail('Something went wrong :(');
|
|
122
126
|
}
|
|
123
127
|
throw e;
|
|
124
128
|
}
|