|
| 1 | +import { getCurrentLocale, translate as t } from '@/plugins/i18n.plugin'; |
| 2 | + |
| 3 | +Intl.DurationFormat ??= class DurationFormat { |
| 4 | + format(duration: { seconds?: number; milliseconds?: number }): string { |
| 5 | + return 'seconds' in duration |
| 6 | + ? `${duration.seconds} seconds` |
| 7 | + : `${duration.milliseconds} milliseconds`; |
| 8 | + } |
| 9 | +}; |
| 10 | + |
| 11 | +export type Update<Result> = |
| 12 | + | { |
| 13 | + kind: 'progress' |
| 14 | + progress: number |
| 15 | + } |
| 16 | + | { |
| 17 | + kind: 'success' |
| 18 | + value: Result |
| 19 | + timeTakenMs: number |
| 20 | + } |
| 21 | + | { |
| 22 | + kind: 'error' |
| 23 | + message: string |
| 24 | + }; |
| 25 | + |
| 26 | +// generic type for the callback versions of bcryptjs's `hash` and `compare` |
| 27 | +export type BcryptFn<Param, Result> = ( |
| 28 | + arg1: string, |
| 29 | + arg2: Param, |
| 30 | + callback: (err: Error | null, hash: Result) => void, |
| 31 | + progressCallback: (percent: number) => void, |
| 32 | +) => void; |
| 33 | + |
| 34 | +interface BcryptWithProgressOptions { |
| 35 | + signal: AbortSignal |
| 36 | + timeoutMs: number |
| 37 | +} |
| 38 | + |
| 39 | +export async function* bcryptWithProgressUpdates<Param, Result>( |
| 40 | + fn: BcryptFn<Param, Result>, |
| 41 | + args: [string, Param], |
| 42 | + options?: Partial<BcryptWithProgressOptions>, |
| 43 | +): AsyncGenerator<Update<Result>, undefined, undefined> { |
| 44 | + const { timeoutMs = 10_000 } = options ?? {}; |
| 45 | + const signal = AbortSignal.any([ |
| 46 | + AbortSignal.timeout(timeoutMs), |
| 47 | + options?.signal, |
| 48 | + ].filter(x => x != null)); |
| 49 | + |
| 50 | + let res = (_: Update<Result>) => {}; |
| 51 | + const nextPromise = () => new Promise<Update<Result>>(resolve => res = resolve); |
| 52 | + const promises = [nextPromise()]; |
| 53 | + const nextValue = (value: Update<Result>) => { |
| 54 | + res(value); |
| 55 | + promises.push(nextPromise()); |
| 56 | + }; |
| 57 | + |
| 58 | + const start = Date.now(); |
| 59 | + |
| 60 | + fn( |
| 61 | + args[0], |
| 62 | + args[1], |
| 63 | + (err, result) => { |
| 64 | + nextValue( |
| 65 | + err == null |
| 66 | + ? { kind: 'success', value: result, timeTakenMs: Date.now() - start } |
| 67 | + : { kind: 'error', message: err.message }, |
| 68 | + ); |
| 69 | + }, |
| 70 | + (progress) => { |
| 71 | + if (signal.aborted) { |
| 72 | + nextValue({ kind: 'progress', progress: 0 }); |
| 73 | + if (signal.reason instanceof DOMException && signal.reason.name === 'TimeoutError') { |
| 74 | + const message = t('tools.bcrypt.texts.timed-out-after-timeout-period', { |
| 75 | + timeoutPeriod: new Intl.DurationFormat(getCurrentLocale(), { style: 'long' }) |
| 76 | + .format({ seconds: Math.round(timeoutMs / 1000) }), |
| 77 | + }); |
| 78 | + |
| 79 | + nextValue({ kind: 'error', message }); |
| 80 | + } |
| 81 | + |
| 82 | + // throw inside callback to cancel execution of hashing/comparing |
| 83 | + throw signal.reason; |
| 84 | + } |
| 85 | + else { |
| 86 | + nextValue({ kind: 'progress', progress }); |
| 87 | + } |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + for await (const value of promises) { |
| 92 | + yield value; |
| 93 | + |
| 94 | + if (value.kind === 'success' || value.kind === 'error') { |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments