casualos 3.4.6-alpha.14601027727 → 3.5.0-alpha.15117651144
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/cli.js +291 -343
- package/cli.js.map +1 -1
- package/dist/cli.js +17144 -17460
- package/dist/index.js +47 -120
- package/index.js +1 -0
- package/index.js.map +1 -1
- package/infra.js +67 -82
- package/infra.js.map +1 -1
- package/package.json +4 -4
- package/schema.d.ts +0 -1
- package/schema.js +341 -365
- package/schema.js.map +1 -1
package/cli.js
CHANGED
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
-
var m = o[Symbol.asyncIterator], i;
|
|
13
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
-
};
|
|
17
1
|
/* CasualOS is a set of web-based tools designed to facilitate the creation of real-time, multi-user, context-aware interactive experiences.
|
|
18
2
|
*
|
|
19
3
|
* Copyright (c) 2019-2025 Casual Simulation, Inc.
|
|
@@ -33,13 +17,14 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
|
33
17
|
*/
|
|
34
18
|
import prompts from 'prompts';
|
|
35
19
|
import { Command } from 'commander';
|
|
20
|
+
/* eslint-disable casualos/no-non-type-imports */
|
|
36
21
|
import { createRecordsClient } from '@casual-simulation/aux-records/RecordsClient';
|
|
37
22
|
import { askForInputs, onState } from './schema';
|
|
38
23
|
import repl from 'node:repl';
|
|
39
24
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
40
25
|
// @ts-ignore
|
|
41
26
|
import Conf from 'conf';
|
|
42
|
-
import { getSessionKeyExpiration, isExpired, parseSessionKey, willExpire, } from '@casual-simulation/aux-
|
|
27
|
+
import { getSessionKeyExpiration, isExpired, parseSessionKey, willExpire, } from '@casual-simulation/aux-common';
|
|
43
28
|
import { serverConfigSchema } from '@casual-simulation/aux-records';
|
|
44
29
|
import { PassThrough } from 'node:stream';
|
|
45
30
|
import { getSchemaMetadata } from '@casual-simulation/aux-common';
|
|
@@ -59,35 +44,35 @@ program
|
|
|
59
44
|
program
|
|
60
45
|
.command('login')
|
|
61
46
|
.description('Login to the CasualOS API')
|
|
62
|
-
.action(() =>
|
|
47
|
+
.action(async () => {
|
|
63
48
|
const opts = program.optsWithGlobals();
|
|
64
|
-
const endpoint =
|
|
65
|
-
const client =
|
|
66
|
-
|
|
67
|
-
})
|
|
49
|
+
const endpoint = await getEndpoint(opts.endpoint);
|
|
50
|
+
const client = await getClient(endpoint, getSessionKey(endpoint));
|
|
51
|
+
await login(client);
|
|
52
|
+
});
|
|
68
53
|
program
|
|
69
54
|
.command('logout')
|
|
70
55
|
.description('Logout of the CasualOS API')
|
|
71
|
-
.action(() =>
|
|
56
|
+
.action(async () => {
|
|
72
57
|
const opts = program.optsWithGlobals();
|
|
73
|
-
const endpoint =
|
|
58
|
+
const endpoint = await getEndpoint(opts.endpoint);
|
|
74
59
|
saveSessionKey(endpoint, null);
|
|
75
60
|
console.log('Logged out!');
|
|
76
|
-
})
|
|
61
|
+
});
|
|
77
62
|
program
|
|
78
63
|
.command('set-endpoint')
|
|
79
64
|
.argument('[endpoint]', 'The endpoint to use for queries. If omitted, then you will be prompted to enter an endpoint.')
|
|
80
65
|
.description('Set the endpoint that is currently in use.')
|
|
81
|
-
.action((endpoint) =>
|
|
66
|
+
.action(async (endpoint) => {
|
|
82
67
|
if (endpoint) {
|
|
83
68
|
saveCurrentEndpoint(endpoint);
|
|
84
69
|
}
|
|
85
70
|
else {
|
|
86
|
-
endpoint =
|
|
71
|
+
endpoint = await updateEndpoint();
|
|
87
72
|
}
|
|
88
73
|
const origin = getOrigin(endpoint);
|
|
89
74
|
if (origin !== endpoint) {
|
|
90
|
-
const response =
|
|
75
|
+
const response = await prompts({
|
|
91
76
|
type: 'confirm',
|
|
92
77
|
name: 'confirm',
|
|
93
78
|
message: `The origin for the endpoint (${origin}) is different from the endpoint (${endpoint}) itself. Do you want to keep it?`,
|
|
@@ -98,7 +83,7 @@ program
|
|
|
98
83
|
return;
|
|
99
84
|
}
|
|
100
85
|
}
|
|
101
|
-
const originResponse =
|
|
86
|
+
const originResponse = await prompts({
|
|
102
87
|
type: 'text',
|
|
103
88
|
name: 'origin',
|
|
104
89
|
message: 'Enter the origin to use for requests to this endpoint.',
|
|
@@ -106,38 +91,38 @@ program
|
|
|
106
91
|
onState,
|
|
107
92
|
});
|
|
108
93
|
saveOrigin(endpoint, originResponse.origin);
|
|
109
|
-
})
|
|
94
|
+
});
|
|
110
95
|
program
|
|
111
96
|
.command('status')
|
|
112
97
|
.description('Get the status of the current session.')
|
|
113
|
-
.action(() =>
|
|
98
|
+
.action(async () => {
|
|
114
99
|
const endpoint = getCurrentEndpoint();
|
|
115
100
|
printStatus(endpoint);
|
|
116
|
-
})
|
|
101
|
+
});
|
|
117
102
|
program
|
|
118
103
|
.command('query')
|
|
119
104
|
.description('Query the CasualOS API')
|
|
120
105
|
.argument('[procedure]', 'The procedure to execute. If omitted, then you will be prompted to select a procedure.')
|
|
121
106
|
.argument('[input]', 'The input to the procedure. If specified, then it will be parsed as JSON. If omitted, then you will be prompted to enter the input.')
|
|
122
107
|
.option('-k, --key <key>', 'The session key to use for the query. If not specified, then the current session key will be used.')
|
|
123
|
-
.action((procedure, input, options) =>
|
|
108
|
+
.action(async (procedure, input, options) => {
|
|
124
109
|
var _a;
|
|
125
110
|
const opts = program.optsWithGlobals();
|
|
126
|
-
const endpoint =
|
|
127
|
-
const client =
|
|
128
|
-
|
|
129
|
-
})
|
|
111
|
+
const endpoint = await getEndpoint(opts.endpoint);
|
|
112
|
+
const client = await getClient(endpoint, (_a = opts.key) !== null && _a !== void 0 ? _a : (await getOrRefreshSessionKey(endpoint)));
|
|
113
|
+
await query(client, procedure, input);
|
|
114
|
+
});
|
|
130
115
|
program
|
|
131
116
|
.command('repl')
|
|
132
117
|
.description('Start a REPL for the CasualOS API')
|
|
133
118
|
.option('-k, --key <key>', 'The session key to use for the session. If omitted, then the current session key will be used.')
|
|
134
119
|
.addHelpText('after', `\nThe CasualOS REPL allows you to interact with the CasualOS API using a Read-Eval-Print Loop (REPL).\nIt supports JavaScript and has a special function, query([procedure], [input]), that can be used to query the API.`)
|
|
135
|
-
.action((options) =>
|
|
136
|
-
var
|
|
120
|
+
.action(async (options) => {
|
|
121
|
+
var _a;
|
|
137
122
|
const opts = program.optsWithGlobals();
|
|
138
|
-
const endpoint =
|
|
139
|
-
const key = (
|
|
140
|
-
const client =
|
|
123
|
+
const endpoint = await getEndpoint(opts.endpoint);
|
|
124
|
+
const key = (_a = opts.key) !== null && _a !== void 0 ? _a : (await getOrRefreshSessionKey(endpoint));
|
|
125
|
+
const client = await getClient(endpoint, key);
|
|
141
126
|
let userId = null;
|
|
142
127
|
if (key) {
|
|
143
128
|
const parseResult = parseSessionKey(key);
|
|
@@ -148,17 +133,17 @@ program
|
|
|
148
133
|
const replIn = new PassThrough();
|
|
149
134
|
process.stdin.pipe(replIn);
|
|
150
135
|
function pauseRepl(func) {
|
|
151
|
-
return (...args) =>
|
|
136
|
+
return async (...args) => {
|
|
152
137
|
process.stdin.unpipe(replIn);
|
|
153
138
|
replIn.pause();
|
|
154
139
|
try {
|
|
155
|
-
return
|
|
140
|
+
return await func(...args);
|
|
156
141
|
}
|
|
157
142
|
finally {
|
|
158
143
|
replIn.resume();
|
|
159
144
|
process.stdin.pipe(replIn);
|
|
160
145
|
}
|
|
161
|
-
}
|
|
146
|
+
};
|
|
162
147
|
}
|
|
163
148
|
const replServer = repl.start({
|
|
164
149
|
prompt: 'casualos > ',
|
|
@@ -173,9 +158,9 @@ program
|
|
|
173
158
|
configurable: false,
|
|
174
159
|
writable: false,
|
|
175
160
|
enumerable: true,
|
|
176
|
-
value: pauseRepl((procedure, input) =>
|
|
177
|
-
return
|
|
178
|
-
})
|
|
161
|
+
value: pauseRepl(async (procedure, input) => {
|
|
162
|
+
return await query(client, procedure, input, false, true, replServer);
|
|
163
|
+
}),
|
|
179
164
|
},
|
|
180
165
|
userId: {
|
|
181
166
|
configurable: false,
|
|
@@ -184,14 +169,14 @@ program
|
|
|
184
169
|
value: userId,
|
|
185
170
|
},
|
|
186
171
|
});
|
|
187
|
-
})
|
|
172
|
+
});
|
|
188
173
|
program
|
|
189
174
|
.command('generate-server-config')
|
|
190
175
|
.option('-p, --pretty', 'Pretty print the output.')
|
|
191
176
|
.description('Generate a server config for CasualOS.')
|
|
192
|
-
.action(() =>
|
|
177
|
+
.action(async () => {
|
|
193
178
|
const metadata = getSchemaMetadata(serverConfigSchema);
|
|
194
|
-
const result =
|
|
179
|
+
const result = await askForInputs(metadata, 'serverConfig');
|
|
195
180
|
const isValid = serverConfigSchema.safeParse(result);
|
|
196
181
|
if (isValid.success === false) {
|
|
197
182
|
console.error('Generated config is invalid:');
|
|
@@ -204,20 +189,20 @@ program
|
|
|
204
189
|
else {
|
|
205
190
|
console.log(JSON.stringify(result));
|
|
206
191
|
}
|
|
207
|
-
})
|
|
192
|
+
});
|
|
208
193
|
program
|
|
209
194
|
.command('validate-server-config')
|
|
210
195
|
.option('--json <config>', 'The JSON to validate.')
|
|
211
196
|
.option('-f, --file <file>', 'The file to validate.')
|
|
212
197
|
.description('Validate a server config for CasualOS.')
|
|
213
|
-
.action((options) =>
|
|
198
|
+
.action(async (options) => {
|
|
214
199
|
let configJson;
|
|
215
200
|
if (options.json) {
|
|
216
201
|
configJson = options.json;
|
|
217
202
|
}
|
|
218
203
|
else {
|
|
219
204
|
const fullPath = path.resolve(options.file);
|
|
220
|
-
configJson =
|
|
205
|
+
configJson = await readFile(fullPath, 'utf-8');
|
|
221
206
|
}
|
|
222
207
|
const parsed = JSON.parse(configJson);
|
|
223
208
|
const result = serverConfigSchema.safeParse(parsed);
|
|
@@ -229,154 +214,131 @@ program
|
|
|
229
214
|
console.error(result.error.toString());
|
|
230
215
|
process.exit(1);
|
|
231
216
|
}
|
|
232
|
-
})
|
|
217
|
+
});
|
|
233
218
|
setupInfraCommands(program.command('infra'), config);
|
|
234
|
-
function query(client, procedure, input, shouldConfirm = true, isJavaScriptInput = false, repl = null) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
219
|
+
async function query(client, procedure, input, shouldConfirm = true, isJavaScriptInput = false, repl = null) {
|
|
220
|
+
const availableOperations = await client.listProcedures({});
|
|
221
|
+
while (!procedure ||
|
|
222
|
+
!availableOperations.procedures.find((p) => p.name === procedure)) {
|
|
223
|
+
const response = await prompts({
|
|
224
|
+
type: 'autocomplete',
|
|
225
|
+
name: 'procedure',
|
|
226
|
+
message: 'Select the procedure to execute',
|
|
227
|
+
choices: availableOperations.procedures.map((op) => ({
|
|
228
|
+
title: op.name,
|
|
229
|
+
value: op.name,
|
|
230
|
+
})),
|
|
231
|
+
onState,
|
|
232
|
+
});
|
|
233
|
+
procedure = response.procedure;
|
|
234
|
+
}
|
|
235
|
+
const operation = availableOperations.procedures.find((p) => p.name === procedure);
|
|
236
|
+
if (!operation) {
|
|
237
|
+
console.error(`Could not find operation ${procedure}!`);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
console.log('Your selected operation:', operation);
|
|
241
|
+
let query;
|
|
242
|
+
if (operation.query) {
|
|
243
|
+
query = await askForInputs(operation.query, operation.name, repl);
|
|
244
|
+
console.log('Your query:', query);
|
|
245
|
+
}
|
|
246
|
+
if (!input) {
|
|
247
|
+
input = await askForInputs(operation.inputs, operation.name, repl);
|
|
248
|
+
}
|
|
249
|
+
else if (!isJavaScriptInput) {
|
|
250
|
+
input = JSON.parse(input);
|
|
251
|
+
}
|
|
252
|
+
console.log('Your input:', input);
|
|
253
|
+
let continueRequest = true;
|
|
254
|
+
if (shouldConfirm) {
|
|
255
|
+
const confirm = await prompts({
|
|
256
|
+
type: 'confirm',
|
|
257
|
+
name: 'continue',
|
|
258
|
+
message: 'Do you want to continue?',
|
|
259
|
+
initial: true,
|
|
260
|
+
onState,
|
|
261
|
+
});
|
|
262
|
+
continueRequest = confirm.continue;
|
|
263
|
+
}
|
|
264
|
+
if (continueRequest) {
|
|
265
|
+
const result = await callProcedure(client, operation.name, input, query);
|
|
270
266
|
if (shouldConfirm) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
onState,
|
|
277
|
-
});
|
|
278
|
-
continueRequest = confirm.continue;
|
|
279
|
-
}
|
|
280
|
-
if (continueRequest) {
|
|
281
|
-
const result = yield callProcedure(client, operation.name, input, query);
|
|
282
|
-
if (shouldConfirm) {
|
|
283
|
-
if (typeof result === 'object' && Symbol.asyncIterator in result) {
|
|
284
|
-
console.log('Result:');
|
|
285
|
-
function logResult() {
|
|
286
|
-
var _a, e_1, _b, _c;
|
|
287
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
288
|
-
try {
|
|
289
|
-
for (var _d = true, result_1 = __asyncValues(result), result_1_1; result_1_1 = yield result_1.next(), _a = result_1_1.done, !_a; _d = true) {
|
|
290
|
-
_c = result_1_1.value;
|
|
291
|
-
_d = false;
|
|
292
|
-
let item = _c;
|
|
293
|
-
console.log(item);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
297
|
-
finally {
|
|
298
|
-
try {
|
|
299
|
-
if (!_d && !_a && (_b = result_1.return)) yield _b.call(result_1);
|
|
300
|
-
}
|
|
301
|
-
finally { if (e_1) throw e_1.error; }
|
|
302
|
-
}
|
|
303
|
-
});
|
|
267
|
+
if (typeof result === 'object' && Symbol.asyncIterator in result) {
|
|
268
|
+
console.log('Result:');
|
|
269
|
+
async function logResult() {
|
|
270
|
+
for await (let item of result) {
|
|
271
|
+
console.log(item);
|
|
304
272
|
}
|
|
305
|
-
logResult();
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
console.log('Result:', result);
|
|
309
273
|
}
|
|
274
|
+
logResult();
|
|
275
|
+
}
|
|
276
|
+
else {
|
|
277
|
+
console.log('Result:', result);
|
|
310
278
|
}
|
|
311
|
-
return result;
|
|
312
|
-
}
|
|
313
|
-
else {
|
|
314
|
-
console.log('Cancelled.');
|
|
315
279
|
}
|
|
316
|
-
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
console.log('Cancelled.');
|
|
284
|
+
}
|
|
317
285
|
}
|
|
318
|
-
function callProcedure(client, operation, input, query) {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
if (!key) {
|
|
335
|
-
return result;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
286
|
+
async function callProcedure(client, operation, input, query) {
|
|
287
|
+
while (true) {
|
|
288
|
+
const result = await client.callProcedure(operation, input, {
|
|
289
|
+
headers: getHeaders(client),
|
|
290
|
+
}, query);
|
|
291
|
+
if (result.success === false && result.errorCode === 'not_logged_in') {
|
|
292
|
+
const loginResponse = await prompts({
|
|
293
|
+
type: 'confirm',
|
|
294
|
+
name: 'login',
|
|
295
|
+
message: 'You are not logged in. Do you want to log in and try again?',
|
|
296
|
+
initial: true,
|
|
297
|
+
onState,
|
|
298
|
+
});
|
|
299
|
+
if (loginResponse.login) {
|
|
300
|
+
const key = await login(client);
|
|
301
|
+
if (!key) {
|
|
339
302
|
return result;
|
|
340
303
|
}
|
|
341
304
|
}
|
|
342
|
-
else if (result.success === false &&
|
|
343
|
-
result.errorCode === 'invalid_origin') {
|
|
344
|
-
const originResponse = yield prompts({
|
|
345
|
-
type: 'text',
|
|
346
|
-
name: 'origin',
|
|
347
|
-
message: 'The endpoint does not allow itself as an origin. Enter the origin to use for the request.',
|
|
348
|
-
onState,
|
|
349
|
-
});
|
|
350
|
-
saveOrigin(client.endpoint, originResponse.origin);
|
|
351
|
-
}
|
|
352
305
|
else {
|
|
353
306
|
return result;
|
|
354
307
|
}
|
|
355
308
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
return client;
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
function getOrRefreshSessionKey(endpoint) {
|
|
369
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
370
|
-
const key = getSessionKey(endpoint);
|
|
371
|
-
const expiration = getSessionKeyExpiration(key);
|
|
372
|
-
if (isExpired(expiration)) {
|
|
373
|
-
return null;
|
|
309
|
+
else if (result.success === false &&
|
|
310
|
+
result.errorCode === 'invalid_origin') {
|
|
311
|
+
const originResponse = await prompts({
|
|
312
|
+
type: 'text',
|
|
313
|
+
name: 'origin',
|
|
314
|
+
message: 'The endpoint does not allow itself as an origin. Enter the origin to use for the request.',
|
|
315
|
+
onState,
|
|
316
|
+
});
|
|
317
|
+
saveOrigin(client.endpoint, originResponse.origin);
|
|
374
318
|
}
|
|
375
|
-
else
|
|
376
|
-
return
|
|
319
|
+
else {
|
|
320
|
+
return result;
|
|
377
321
|
}
|
|
378
|
-
|
|
379
|
-
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
async function getClient(endpoint, key) {
|
|
325
|
+
printStatus(endpoint);
|
|
326
|
+
const client = createRecordsClient(endpoint);
|
|
327
|
+
if (key) {
|
|
328
|
+
client.sessionKey = key;
|
|
329
|
+
}
|
|
330
|
+
return client;
|
|
331
|
+
}
|
|
332
|
+
async function getOrRefreshSessionKey(endpoint) {
|
|
333
|
+
const key = getSessionKey(endpoint);
|
|
334
|
+
const expiration = getSessionKeyExpiration(key);
|
|
335
|
+
if (isExpired(expiration)) {
|
|
336
|
+
return null;
|
|
337
|
+
}
|
|
338
|
+
else if (willExpire(expiration)) {
|
|
339
|
+
return await replaceSessionKey(endpoint, key);
|
|
340
|
+
}
|
|
341
|
+
return key;
|
|
380
342
|
}
|
|
381
343
|
function getSessionKey(endpoint) {
|
|
382
344
|
return convertToString(config.get(`${endpoint}:sessionKey`));
|
|
@@ -394,179 +356,167 @@ function saveLoginResult(client, result) {
|
|
|
394
356
|
client.sessionKey = result.sessionKey;
|
|
395
357
|
}
|
|
396
358
|
}
|
|
397
|
-
function login(client) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
});
|
|
359
|
+
async function login(client) {
|
|
360
|
+
const response = await prompts([
|
|
361
|
+
{
|
|
362
|
+
type: 'select',
|
|
363
|
+
name: 'type',
|
|
364
|
+
message: 'Select the type of address to use for login.',
|
|
365
|
+
choices: [
|
|
366
|
+
{
|
|
367
|
+
title: 'Email',
|
|
368
|
+
value: 'email',
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
title: 'Phone',
|
|
372
|
+
value: 'phone',
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
title: 'Privo',
|
|
376
|
+
value: 'privo',
|
|
377
|
+
},
|
|
378
|
+
],
|
|
379
|
+
onState,
|
|
380
|
+
},
|
|
381
|
+
]);
|
|
382
|
+
if (response.type === 'privo') {
|
|
383
|
+
return await loginWithPrivo(client);
|
|
384
|
+
}
|
|
385
|
+
else if (response.type === 'email' || response.type === 'phone') {
|
|
386
|
+
const addressResponse = await prompts({
|
|
387
|
+
type: 'text',
|
|
388
|
+
name: 'address',
|
|
389
|
+
message: 'Enter your address.',
|
|
390
|
+
onState,
|
|
391
|
+
});
|
|
392
|
+
const addressType = response.type;
|
|
393
|
+
const address = addressResponse.address;
|
|
394
|
+
return await loginWithCode(client, address, addressType);
|
|
395
|
+
}
|
|
436
396
|
}
|
|
437
|
-
function loginWithCode(client, address, addressType) {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
397
|
+
async function loginWithCode(client, address, addressType) {
|
|
398
|
+
const result = await client.requestLogin({
|
|
399
|
+
address: address,
|
|
400
|
+
addressType: addressType,
|
|
401
|
+
}, {
|
|
402
|
+
headers: getHeaders(client),
|
|
403
|
+
});
|
|
404
|
+
if (result.success) {
|
|
405
|
+
const response = await prompts({
|
|
406
|
+
type: 'text',
|
|
407
|
+
name: 'code',
|
|
408
|
+
message: 'Enter the code that was sent to your address.',
|
|
409
|
+
onState,
|
|
410
|
+
});
|
|
411
|
+
const code = response.code;
|
|
412
|
+
const loginResult = await client.completeLogin({
|
|
413
|
+
code: code,
|
|
414
|
+
requestId: result.requestId,
|
|
415
|
+
userId: result.userId,
|
|
442
416
|
}, {
|
|
443
417
|
headers: getHeaders(client),
|
|
444
418
|
});
|
|
445
|
-
if (
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
message: 'Enter the code that was sent to your address.',
|
|
450
|
-
onState,
|
|
451
|
-
});
|
|
452
|
-
const code = response.code;
|
|
453
|
-
const loginResult = yield client.completeLogin({
|
|
454
|
-
code: code,
|
|
455
|
-
requestId: result.requestId,
|
|
456
|
-
userId: result.userId,
|
|
457
|
-
}, {
|
|
458
|
-
headers: getHeaders(client),
|
|
459
|
-
});
|
|
460
|
-
if (loginResult.success === true) {
|
|
461
|
-
saveLoginResult(client, loginResult);
|
|
462
|
-
console.log('Login successful!');
|
|
463
|
-
return loginResult.sessionKey;
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
saveLoginResult(client, null);
|
|
467
|
-
console.log('Failed to complete login:');
|
|
468
|
-
console.log(loginResult);
|
|
469
|
-
return null;
|
|
470
|
-
}
|
|
419
|
+
if (loginResult.success === true) {
|
|
420
|
+
saveLoginResult(client, loginResult);
|
|
421
|
+
console.log('Login successful!');
|
|
422
|
+
return loginResult.sessionKey;
|
|
471
423
|
}
|
|
472
424
|
else {
|
|
473
425
|
saveLoginResult(client, null);
|
|
474
|
-
console.log('Failed to
|
|
475
|
-
console.log(
|
|
426
|
+
console.log('Failed to complete login:');
|
|
427
|
+
console.log(loginResult);
|
|
476
428
|
return null;
|
|
477
429
|
}
|
|
478
|
-
}
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
saveLoginResult(client, null);
|
|
433
|
+
console.log('Failed to create login request:');
|
|
434
|
+
console.log(result);
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
479
437
|
}
|
|
480
|
-
function loginWithPrivo(client) {
|
|
481
|
-
|
|
482
|
-
|
|
438
|
+
async function loginWithPrivo(client) {
|
|
439
|
+
const result = await client.requestPrivoLogin({}, {
|
|
440
|
+
headers: getHeaders(client),
|
|
441
|
+
});
|
|
442
|
+
if (result.success === false) {
|
|
443
|
+
saveLoginResult(client, null);
|
|
444
|
+
console.log('Failed to request Privo login:');
|
|
445
|
+
console.log(result);
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
const open = (await import('open')).default;
|
|
449
|
+
await open(result.authorizationUrl);
|
|
450
|
+
const startTime = Date.now();
|
|
451
|
+
const timeout = 1000 * 60 * 5; // 5 minutes
|
|
452
|
+
while (Date.now() - startTime < timeout) {
|
|
453
|
+
const loginResult = await client.completeOAuthLogin({
|
|
454
|
+
requestId: result.requestId,
|
|
455
|
+
}, {
|
|
483
456
|
headers: getHeaders(client),
|
|
484
457
|
});
|
|
485
|
-
if (
|
|
486
|
-
saveLoginResult(client,
|
|
487
|
-
console.log('
|
|
488
|
-
|
|
489
|
-
return null;
|
|
458
|
+
if (loginResult.success === true) {
|
|
459
|
+
saveLoginResult(client, loginResult);
|
|
460
|
+
console.log('Login successful!');
|
|
461
|
+
return loginResult.sessionKey;
|
|
490
462
|
}
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
while (Date.now() - startTime < timeout) {
|
|
496
|
-
const loginResult = yield client.completeOAuthLogin({
|
|
497
|
-
requestId: result.requestId,
|
|
498
|
-
}, {
|
|
499
|
-
headers: getHeaders(client),
|
|
500
|
-
});
|
|
501
|
-
if (loginResult.success === true) {
|
|
502
|
-
saveLoginResult(client, loginResult);
|
|
503
|
-
console.log('Login successful!');
|
|
504
|
-
return loginResult.sessionKey;
|
|
463
|
+
else {
|
|
464
|
+
if (loginResult.errorCode === 'not_completed') {
|
|
465
|
+
// Wait for a second before trying again.
|
|
466
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
505
467
|
}
|
|
506
468
|
else {
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
else {
|
|
512
|
-
saveLoginResult(client, null);
|
|
513
|
-
console.log('Failed to complete login:');
|
|
514
|
-
console.log(loginResult);
|
|
515
|
-
return null;
|
|
516
|
-
}
|
|
469
|
+
saveLoginResult(client, null);
|
|
470
|
+
console.log('Failed to complete login:');
|
|
471
|
+
console.log(loginResult);
|
|
472
|
+
return null;
|
|
517
473
|
}
|
|
518
474
|
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
475
|
+
}
|
|
476
|
+
saveLoginResult(client, null);
|
|
477
|
+
console.log('Failed to complete login:');
|
|
478
|
+
console.log('Timed out');
|
|
479
|
+
return null;
|
|
524
480
|
}
|
|
525
|
-
function replaceSessionKey(endpoint, key) {
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
sessionKey: key,
|
|
530
|
-
});
|
|
531
|
-
if (result.success === true) {
|
|
532
|
-
saveSessionKey(endpoint, result.sessionKey);
|
|
533
|
-
console.log('Session key replaced!');
|
|
534
|
-
return result.sessionKey;
|
|
535
|
-
}
|
|
536
|
-
saveSessionKey(endpoint, null);
|
|
537
|
-
console.log('Failed to replace session key:');
|
|
538
|
-
console.log(result);
|
|
539
|
-
return null;
|
|
481
|
+
async function replaceSessionKey(endpoint, key) {
|
|
482
|
+
const client = await getClient(endpoint, key);
|
|
483
|
+
const result = await client.replaceSession(undefined, {
|
|
484
|
+
sessionKey: key,
|
|
540
485
|
});
|
|
486
|
+
if (result.success === true) {
|
|
487
|
+
saveSessionKey(endpoint, result.sessionKey);
|
|
488
|
+
console.log('Session key replaced!');
|
|
489
|
+
return result.sessionKey;
|
|
490
|
+
}
|
|
491
|
+
saveSessionKey(endpoint, null);
|
|
492
|
+
console.log('Failed to replace session key:');
|
|
493
|
+
console.log(result);
|
|
494
|
+
return null;
|
|
541
495
|
}
|
|
542
|
-
function getEndpoint(endpoint) {
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
return savedEndpoint;
|
|
553
|
-
});
|
|
496
|
+
async function getEndpoint(endpoint) {
|
|
497
|
+
if (endpoint) {
|
|
498
|
+
return endpoint;
|
|
499
|
+
}
|
|
500
|
+
let savedEndpoint = getCurrentEndpoint();
|
|
501
|
+
console.log('saved endpoint', savedEndpoint);
|
|
502
|
+
if (!savedEndpoint) {
|
|
503
|
+
savedEndpoint = await updateEndpoint();
|
|
504
|
+
}
|
|
505
|
+
return savedEndpoint;
|
|
554
506
|
}
|
|
555
507
|
function getCurrentEndpoint() {
|
|
556
508
|
return convertToString(config.get('currentEndpoint'));
|
|
557
509
|
}
|
|
558
|
-
function updateEndpoint() {
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
onState,
|
|
565
|
-
});
|
|
566
|
-
const savedEndpoint = response.endpoint;
|
|
567
|
-
saveCurrentEndpoint(savedEndpoint);
|
|
568
|
-
return savedEndpoint;
|
|
510
|
+
async function updateEndpoint() {
|
|
511
|
+
const response = await prompts({
|
|
512
|
+
type: 'text',
|
|
513
|
+
name: 'endpoint',
|
|
514
|
+
message: 'Enter the endpoint to use for queries.',
|
|
515
|
+
onState,
|
|
569
516
|
});
|
|
517
|
+
const savedEndpoint = response.endpoint;
|
|
518
|
+
saveCurrentEndpoint(savedEndpoint);
|
|
519
|
+
return savedEndpoint;
|
|
570
520
|
}
|
|
571
521
|
function saveCurrentEndpoint(endpoint) {
|
|
572
522
|
config.set('currentEndpoint', endpoint);
|
|
@@ -609,15 +559,13 @@ function printStatus(endpoint) {
|
|
|
609
559
|
console.log('You are not logged in.');
|
|
610
560
|
}
|
|
611
561
|
}
|
|
612
|
-
function main() {
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
620
|
-
});
|
|
562
|
+
async function main() {
|
|
563
|
+
try {
|
|
564
|
+
await program.parseAsync(process.argv);
|
|
565
|
+
}
|
|
566
|
+
catch (err) {
|
|
567
|
+
console.error(err);
|
|
568
|
+
}
|
|
621
569
|
}
|
|
622
570
|
function convertToString(str) {
|
|
623
571
|
if (typeof str === 'undefined' || str === null) {
|