|
| 1 | +#!/usr/bin/env -S bun run |
| 2 | + |
| 3 | +import readline from 'node:readline'; |
| 4 | + |
| 5 | +import zxcvbn from 'zxcvbn'; |
| 6 | + |
| 7 | +import ora from 'ora'; |
| 8 | + |
| 9 | +const progress = ora({ |
| 10 | + text: 'Analyzing passwords ...', |
| 11 | +}).start(); |
| 12 | + |
| 13 | +function Passwords ( ) { |
| 14 | + this.map = new Map(); |
| 15 | +} |
| 16 | + |
| 17 | +Passwords.prototype.add = function (key, password) { |
| 18 | + if (!this.map.has(password)) { |
| 19 | + const meta = zxcvbn(password); |
| 20 | + this.map.set(password, {keys: [key], password, meta}); |
| 21 | + } |
| 22 | + else { |
| 23 | + this.map.get(password).keys.push(key); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +Passwords.prototype[Symbol.iterator] = function () { |
| 28 | + return this.map.values(); |
| 29 | +}; |
| 30 | + |
| 31 | +const passwords = new Passwords(); |
| 32 | + |
| 33 | +let count = 0; |
| 34 | +let done = 0; |
| 35 | + |
| 36 | +function update ( ) { |
| 37 | + progress.text = `Analyzing passwords (${done}/${count}) ...`; |
| 38 | +} |
| 39 | + |
| 40 | +function output ( ) { |
| 41 | + return { |
| 42 | + passwords: [...passwords], |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +const rl = readline.createInterface({ |
| 47 | + input: process.stdin, |
| 48 | + output: process.stdout, |
| 49 | + terminal: false |
| 50 | +}); |
| 51 | + |
| 52 | +rl.on('line', function(line){ |
| 53 | + ++count; update(); |
| 54 | + const [key, ...parts] = line.split(' '); |
| 55 | + const password = parts.join(' '); |
| 56 | + passwords.add(key, password); |
| 57 | + ++done; update(); |
| 58 | +}); |
| 59 | + |
| 60 | +rl.on('close', function(){ |
| 61 | + |
| 62 | + progress.succeed(`Analyzed ${count} passwords`); |
| 63 | + |
| 64 | + console.log(JSON.stringify(output())); |
| 65 | + |
| 66 | +}); |
0 commit comments