-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathyivi-console.js
71 lines (66 loc) · 2.38 KB
/
yivi-console.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const qrcode = require('qrcode-terminal');
module.exports = (askRetry, askPairingCode) => {
return class YiviConsole {
constructor({ stateMachine, options }) {
this._stateMachine = stateMachine;
this._options = options;
}
stateChange({ newState, transition, payload, isFinal }) {
if (isFinal) return;
switch (newState) {
case 'Cancelled':
this._askRetry('Transaction cancelled.');
break;
case 'TimedOut':
this._askRetry('Transaction timed out.');
break;
case 'Error':
this._askRetry('An error occurred.');
break;
case 'ShowingQRCode':
this._renderQRcode(payload);
break;
case 'ShowingYiviButton': {
const err = new Error('Mobile sessions cannot be performed in node');
if (this._options.debugging) console.error(err);
this._stateMachine.selectTransition(({ validTransitions }) => {
if (validTransitions.includes('fail')) return { transition: 'fail', payload: err };
throw err;
});
break;
}
case 'ContinueOn2ndDevice':
// Falls through
case 'ContinueInYiviApp':
console.log('Please follow the instructions in the Yivi app.');
break;
case 'EnterPairingCode':
this._askPairingCode(transition !== 'appPairing');
break;
}
}
_askPairingCode(askedBefore) {
return this._stateMachine.selectTransition(({ validTransitions, inEndState }) => {
if (inEndState) return false;
if (askedBefore && !askRetry('Wrong pairing code was entered.')) {
const transition = validTransitions.includes('cancel') ? 'cancel' : 'abort';
return { transition };
}
const enteredPairingCode = askPairingCode();
return validTransitions.includes('codeEntered')
? { transition: 'codeEntered', payload: { enteredPairingCode } }
: false;
});
}
_askRetry(message) {
return this._stateMachine.selectTransition(({ validTransitions, inEndState }) => {
if (inEndState) return false;
const transition = validTransitions.includes('restart') && askRetry(message) ? 'restart' : 'abort';
return { transition };
});
}
_renderQRcode(payload) {
qrcode.generate(payload.qr, { small: true });
}
};
};