|
| 1 | + |
| 2 | +const Dataparty = require('../src/index') |
| 3 | + |
| 4 | +const prompt = require('prompt') |
| 5 | + |
| 6 | +let secureConfig = null |
| 7 | + |
| 8 | +async function main(){ |
| 9 | + const memoryConfig = new Dataparty.Config.MemoryConfig({foo: 'bar'}) |
| 10 | + |
| 11 | + |
| 12 | + const jsonConfig = new Dataparty.Config.JsonFileConfig({ |
| 13 | + basePath: '/tmp' |
| 14 | + }) |
| 15 | + |
| 16 | + |
| 17 | + secureConfig = new Dataparty.Config.SecureConfig({ |
| 18 | + config: jsonConfig |
| 19 | + }) |
| 20 | + |
| 21 | + |
| 22 | + secureConfig.on('locked', ()=>{ console.log('locked') }) |
| 23 | + |
| 24 | + secureConfig.on('unlocked', async ()=>{ |
| 25 | + console.log('unlocked') |
| 26 | + |
| 27 | + await secureConfig.write('timestamp', Date.now()) |
| 28 | + }) |
| 29 | + |
| 30 | + secureConfig.on('timeout', ()=>{ console.log('timeout') }) |
| 31 | + |
| 32 | + secureConfig.on('ready', async ()=>{ |
| 33 | + console.log('ready') |
| 34 | + |
| 35 | + console.log('config', await secureConfig.readAll()) |
| 36 | + }) |
| 37 | + |
| 38 | + let blocked = false |
| 39 | + |
| 40 | + secureConfig.on('blocked', async (reason)=>{ |
| 41 | + |
| 42 | + if(await secureConfig.isInitialized() && secureConfig.isLocked()){ |
| 43 | + |
| 44 | + if(blocked){ |
| 45 | + console.log('blocked true') |
| 46 | + await secureConfig.waitForUnlocked() |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + blocked = true |
| 51 | + console.log('blocked -',reason) |
| 52 | + |
| 53 | + |
| 54 | + const {password} = await prompt.get({ |
| 55 | + properties: { |
| 56 | + password: { |
| 57 | + message: 'Enter password', |
| 58 | + hidden: true |
| 59 | + } |
| 60 | + }}) |
| 61 | + |
| 62 | + await secureConfig.unlock(password) |
| 63 | + |
| 64 | + blocked = false |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + }) |
| 69 | + |
| 70 | + secureConfig.on('setup-required', async ()=>{ |
| 71 | + |
| 72 | + console.log('setup-required') |
| 73 | + |
| 74 | + let password = '' |
| 75 | + |
| 76 | + while(1){ |
| 77 | + let passes = await prompt.get({ |
| 78 | + properties: { |
| 79 | + password1: { |
| 80 | + message: 'Set password', |
| 81 | + hidden: true |
| 82 | + }, |
| 83 | + password2: { |
| 84 | + message: 'Confim password', |
| 85 | + hidden: true |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | + |
| 90 | + if(passes.password1 == passes.password2){ |
| 91 | + |
| 92 | + password = passes.password1 |
| 93 | + break |
| 94 | + } |
| 95 | + |
| 96 | + console.log("passwords don't match") |
| 97 | + } |
| 98 | + |
| 99 | + await secureConfig.setPassword(password, { |
| 100 | + foo: 'bar' |
| 101 | + }) |
| 102 | + |
| 103 | + console.log('password set') |
| 104 | + |
| 105 | + |
| 106 | + await secureConfig.unlock(password) |
| 107 | + |
| 108 | + }) |
| 109 | + |
| 110 | + console.log('starting') |
| 111 | + |
| 112 | + await secureConfig.start() |
| 113 | + |
| 114 | + console.log('wait for startup') |
| 115 | + |
| 116 | + await secureConfig.waitForUnlocked('startup') |
| 117 | + |
| 118 | + console.log('wait over') |
| 119 | + |
| 120 | + console.log('main config', await secureConfig.readAll()) |
| 121 | + |
| 122 | + let timer = setTimeout(async ()=>{ |
| 123 | + |
| 124 | + |
| 125 | + console.log('timer config', await secureConfig.readAll()) |
| 126 | + |
| 127 | + }, 30000) |
| 128 | + |
| 129 | +} |
| 130 | + |
| 131 | +main().catch((err)=>{ |
| 132 | + console.error('crashed', err) |
| 133 | +}).then(()=>{ |
| 134 | + console.log('done') |
| 135 | +}) |
0 commit comments