@hadoku/dev-vault 1.5.1 → 1.5.2

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.
Files changed (2) hide show
  1. package/login.mjs +21 -3
  2. package/package.json +2 -2
package/login.mjs CHANGED
@@ -21,8 +21,23 @@ import { mainCheckoutOf } from './key-file.mjs';
21
21
  export const EDGE = 'https://hadoku.me';
22
22
  const FILE = '.devvault.local.json';
23
23
  const POLL_MS = 3000;
24
+ // A request lives a day (edge DEVICE_TTL_S), so the approver can get to it
25
+ // later. Poll briskly while someone is likely approving right now, then slowly:
26
+ // a day at 3s would be ~29k requests for one abandoned login.
27
+ const BRISK_MS = 10 * 60 * 1000;
28
+ const SLOW_POLL_MS = 30_000;
24
29
  const DAY_MS = 24 * 60 * 60 * 1000;
25
30
 
31
+ /** 86_400_000 -> "24 hours", 600_000 -> "10 minutes". */
32
+ export function spell(ms) {
33
+ const min = Math.round(ms / 60_000);
34
+ if (min % 60 === 0) {
35
+ const h = min / 60;
36
+ return `${h} hour${h === 1 ? '' : 's'}`;
37
+ }
38
+ return `${min} minute${min === 1 ? '' : 's'}`;
39
+ }
40
+
26
41
  function runGit(cwd, args) {
27
42
  const r = spawnSync('git', args, { cwd, encoding: 'utf-8' });
28
43
  return r.status === 0 ? r.stdout.trim() : '';
@@ -94,9 +109,12 @@ export async function login({
94
109
  log(`[dev-vault] code ${userCode} · ${host}${repo ? ` · ${repo}` : ''}`);
95
110
  log('[dev-vault] waiting for approval…');
96
111
 
97
- const deadline = now() + (Number(expiresIn) || 600) * 1000;
112
+ const ttlMs = (Number(expiresIn) || 600) * 1000;
113
+ const startedAt = now();
114
+ const deadline = startedAt + ttlMs;
115
+ log(`[dev-vault] (the link stays valid for ${spell(ttlMs)}; leave this running)`);
98
116
  while (now() < deadline) {
99
- await sleep(POLL_MS);
117
+ await sleep(now() - startedAt < BRISK_MS ? POLL_MS : SLOW_POLL_MS);
100
118
  let polled;
101
119
  try {
102
120
  polled = await postJson(fetchImpl, `${base}/auth/service-access/device/poll`, {
@@ -129,6 +147,6 @@ export async function login({
129
147
  );
130
148
  return 0;
131
149
  }
132
- log('[dev-vault] login: nobody approved within 10 minutes. Run `dev-vault login` again.');
150
+ log(`[dev-vault] login: nobody approved within ${spell(ttlMs)}. Run \`dev-vault login\` again.`);
133
151
  return 1;
134
152
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/dev-vault",
3
- "version": "1.5.1",
4
- "gitHead": "a1ecd41d02f32e8b58bfa42fec6a8e5df302333a",
3
+ "version": "1.5.2",
4
+ "gitHead": "9010a68c19d319334869bb549bc90acc1ee4d0ca",
5
5
  "description": "Local-dev vault-broker shim — fetches secrets from the hadoku mgmt-api broker and execs a child command with them in process.env. Works for any consuming repo (Node or non-Node).",
6
6
  "type": "module",
7
7
  "bin": {