@owlmeans/cli-auth 0.1.18-rc.8 → 0.1.18

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 (50) hide show
  1. package/README.md +178 -1
  2. package/agent-meta/manifest.json +3 -3
  3. package/agent-meta/skills/cli-auth/SKILL.md +18 -16
  4. package/build/consts.local.d.ts +2 -0
  5. package/build/consts.local.d.ts.map +1 -0
  6. package/build/consts.local.js +6 -0
  7. package/build/consts.local.js.map +1 -0
  8. package/build/env-file/types.d.ts +45 -0
  9. package/build/env-file/types.d.ts.map +1 -0
  10. package/build/env-file/types.js +2 -0
  11. package/build/env-file/types.js.map +1 -0
  12. package/build/env-file.d.ts +4 -40
  13. package/build/env-file.d.ts.map +1 -1
  14. package/build/env-file.js +71 -99
  15. package/build/env-file.js.map +1 -1
  16. package/build/holder.d.ts +1 -36
  17. package/build/holder.d.ts.map +1 -1
  18. package/build/holder.js +19 -19
  19. package/build/holder.js.map +1 -1
  20. package/build/index.d.ts +3 -0
  21. package/build/index.d.ts.map +1 -1
  22. package/build/index.js.map +1 -1
  23. package/build/lock/types.d.ts +28 -0
  24. package/build/lock/types.d.ts.map +1 -0
  25. package/build/lock/types.js +2 -0
  26. package/build/lock/types.js.map +1 -0
  27. package/build/lock.d.ts +2 -37
  28. package/build/lock.d.ts.map +1 -1
  29. package/build/lock.js +35 -43
  30. package/build/lock.js.map +1 -1
  31. package/build/open-browser.d.ts.map +1 -1
  32. package/build/open-browser.js +1 -5
  33. package/build/open-browser.js.map +1 -1
  34. package/build/types.d.ts +57 -0
  35. package/build/types.d.ts.map +1 -0
  36. package/build/types.js +2 -0
  37. package/build/types.js.map +1 -0
  38. package/package.json +5 -5
  39. package/src/consts.local.ts +5 -0
  40. package/src/env-file/types.ts +45 -0
  41. package/src/env-file.ts +87 -110
  42. package/src/holder.ts +20 -62
  43. package/src/index.ts +3 -0
  44. package/src/lock/types.ts +29 -0
  45. package/src/lock.ts +33 -61
  46. package/src/open-browser.ts +1 -6
  47. package/src/types.ts +58 -0
  48. package/tests/env-file.spec.ts +18 -18
  49. package/tests/holder.spec.ts +4 -6
  50. package/tests/lock.spec.ts +22 -19
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
2
2
  import { mkdtemp, readFile, rm, stat } from 'node:fs/promises'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join } from 'node:path'
5
- import { loadOwlmeansEnv, parseEnv, readCredentialsFile, resolveEnvFile, setEnvValues } from '../src/env-file.js'
5
+ import { envFileHelper } from '../src/env-file.js'
6
6
  import { ENV_CREDENTIALS_FILE } from '../src/consts.js'
7
7
 
8
8
  let dir: string
@@ -12,21 +12,21 @@ afterEach(async () => { await rm(dir, { recursive: true, force: true }) })
12
12
 
13
13
  describe('resolveEnvFile', () => {
14
14
  test('defaults to ~/.owlmeans', () => {
15
- expect(resolveEnvFile({})).toMatch(/\.owlmeans$/)
15
+ expect(envFileHelper.resolveEnvFile({})).toMatch(/\.owlmeans$/)
16
16
  })
17
17
 
18
18
  test('honors OWLMEANS_CREDENTIALS', () => {
19
- expect(resolveEnvFile({ [ENV_CREDENTIALS_FILE]: '/custom/path' })).toBe('/custom/path')
19
+ expect(envFileHelper.resolveEnvFile({ [ENV_CREDENTIALS_FILE]: '/custom/path' })).toBe('/custom/path')
20
20
  })
21
21
 
22
22
  test('ignores an empty override', () => {
23
- expect(resolveEnvFile({ [ENV_CREDENTIALS_FILE]: '' })).toMatch(/\.owlmeans$/)
23
+ expect(envFileHelper.resolveEnvFile({ [ENV_CREDENTIALS_FILE]: '' })).toMatch(/\.owlmeans$/)
24
24
  })
25
25
  })
26
26
 
27
27
  describe('parseEnv', () => {
28
28
  test('reads KEY=VALUE, quotes, comments and export prefixes', () => {
29
- const parsed = parseEnv([
29
+ const parsed = envFileHelper.parseEnv([
30
30
  '# a comment', '', 'FOO=bar', 'export BAZ=qux', 'QUOTED="has spaces"', "SINGLE='also quoted'",
31
31
  ].join('\n'))
32
32
  expect(parsed).toEqual({ FOO: 'bar', BAZ: 'qux', QUOTED: 'has spaces', SINGLE: 'also quoted' })
@@ -36,22 +36,22 @@ describe('parseEnv', () => {
36
36
  describe('loadOwlmeansEnv', () => {
37
37
  test('the environment wins over the file', async () => {
38
38
  const path = join(dir, '.owlmeans')
39
- await setEnvValues(path, { VIABLE_API_TOKEN: 'from-file' })
39
+ await envFileHelper.setEnvValues(path, { VIABLE_API_TOKEN: 'from-file' })
40
40
 
41
- const merged = await loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: path, VIABLE_API_TOKEN: 'from-env' })
41
+ const merged = await envFileHelper.loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: path, VIABLE_API_TOKEN: 'from-env' })
42
42
  expect(merged.VIABLE_API_TOKEN).toBe('from-env')
43
43
  })
44
44
 
45
45
  test('an empty environment value does not shadow the file', async () => {
46
46
  const path = join(dir, '.owlmeans')
47
- await setEnvValues(path, { VIABLE_API_TOKEN: 'from-file' })
47
+ await envFileHelper.setEnvValues(path, { VIABLE_API_TOKEN: 'from-file' })
48
48
 
49
- const merged = await loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: path, VIABLE_API_TOKEN: '' })
49
+ const merged = await envFileHelper.loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: path, VIABLE_API_TOKEN: '' })
50
50
  expect(merged.VIABLE_API_TOKEN).toBe('from-file')
51
51
  })
52
52
 
53
53
  test('a missing file is not an error', async () => {
54
- const merged = await loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: join(dir, 'nope') })
54
+ const merged = await envFileHelper.loadOwlmeansEnv({ [ENV_CREDENTIALS_FILE]: join(dir, 'nope') })
55
55
  expect(merged.VIABLE_API_TOKEN).toBeUndefined()
56
56
  })
57
57
  })
@@ -59,7 +59,7 @@ describe('loadOwlmeansEnv', () => {
59
59
  describe('setEnvValues', () => {
60
60
  test('creates the file with mode 0600', async () => {
61
61
  const path = join(dir, '.owlmeans')
62
- const result = await setEnvValues(path, { A: '1' })
62
+ const result = await envFileHelper.setEnvValues(path, { A: '1' })
63
63
  expect(result.insecurePermissions).toBe(false)
64
64
 
65
65
  const info = await stat(path)
@@ -70,7 +70,7 @@ describe('setEnvValues', () => {
70
70
  const path = join(dir, '.owlmeans')
71
71
  await Bun.write(path, '# a note\nKEEP=1\nCHANGE=old\n')
72
72
 
73
- await setEnvValues(path, { CHANGE: 'new' })
73
+ await envFileHelper.setEnvValues(path, { CHANGE: 'new' })
74
74
  const content = await readFile(path, 'utf-8')
75
75
  expect(content).toContain('# a note')
76
76
  expect(content).toContain('KEEP=1')
@@ -81,26 +81,26 @@ describe('setEnvValues', () => {
81
81
  test('appends a key that was not there before', async () => {
82
82
  const path = join(dir, '.owlmeans')
83
83
  await Bun.write(path, 'A=1\n')
84
- await setEnvValues(path, { B: '2' })
84
+ await envFileHelper.setEnvValues(path, { B: '2' })
85
85
 
86
- const parsed = await readCredentialsFile({ [ENV_CREDENTIALS_FILE]: path })
86
+ const parsed = await envFileHelper.readCredentialsFile({ [ENV_CREDENTIALS_FILE]: path })
87
87
  expect(parsed).toEqual({ A: '1', B: '2' })
88
88
  })
89
89
 
90
90
  test('an undefined value removes the key rather than writing it', async () => {
91
91
  const path = join(dir, '.owlmeans')
92
92
  await Bun.write(path, 'A=1\nB=2\n')
93
- await setEnvValues(path, { A: undefined })
93
+ await envFileHelper.setEnvValues(path, { A: undefined })
94
94
 
95
- const parsed = await readCredentialsFile({ [ENV_CREDENTIALS_FILE]: path })
95
+ const parsed = await envFileHelper.readCredentialsFile({ [ENV_CREDENTIALS_FILE]: path })
96
96
  expect(parsed).toEqual({ B: '2' })
97
97
  })
98
98
 
99
99
  test('a concurrent rewrite never leaves a half-written file — the temp file lands atomically', async () => {
100
100
  const path = join(dir, '.owlmeans')
101
101
  await Promise.all([
102
- setEnvValues(path, { A: '1' }),
103
- setEnvValues(path, { B: '2' }),
102
+ envFileHelper.setEnvValues(path, { A: '1' }),
103
+ envFileHelper.setEnvValues(path, { B: '2' }),
104
104
  ])
105
105
  // Whichever write landed last, the file must be one complete, parseable write — never a
106
106
  // truncated interleaving of the two.
@@ -3,7 +3,7 @@ import { mkdtemp, rm } from 'node:fs/promises'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join } from 'node:path'
5
5
  import { makeCliCredentials } from '../src/holder.js'
6
- import { readCredentialsFile } from '../src/env-file.js'
6
+ import { envFileHelper } from '../src/env-file.js'
7
7
  import { ENV_CREDENTIALS_FILE } from '../src/consts.js'
8
8
 
9
9
  const originalFetch = globalThis.fetch
@@ -100,16 +100,14 @@ describe('token()', () => {
100
100
  })
101
101
 
102
102
  test('refuses a file token bound to a different API URL', async () => {
103
- const { setEnvValues } = await import('../src/env-file.js')
104
- await setEnvValues(credentialsPath, { VIABLE_API_TOKEN: 'vib_other', VIABLE_API_URL: 'https://other.example.com' })
103
+ await envFileHelper.setEnvValues(credentialsPath, { VIABLE_API_TOKEN: 'vib_other', VIABLE_API_URL: 'https://other.example.com' })
105
104
 
106
105
  const credentials = makeCliCredentials(opts())
107
106
  expect(await credentials.token()).toBeNull()
108
107
  })
109
108
 
110
109
  test('accepts a file token when no URL was ever recorded', async () => {
111
- const { setEnvValues } = await import('../src/env-file.js')
112
- await setEnvValues(credentialsPath, { VIABLE_API_TOKEN: 'vib_no-url-recorded' })
110
+ await envFileHelper.setEnvValues(credentialsPath, { VIABLE_API_TOKEN: 'vib_no-url-recorded' })
113
111
 
114
112
  const credentials = makeCliCredentials(opts())
115
113
  expect(await credentials.token()).toBe('vib_no-url-recorded')
@@ -154,7 +152,7 @@ describe('require()', () => {
154
152
  expect(notifications.some(m => m.includes('CODE-1'))).toBe(true)
155
153
  expect(notifications).toContain('Signed in.')
156
154
 
157
- const file = await readCredentialsFile({ [ENV_CREDENTIALS_FILE]: credentialsPath })
155
+ const file = await envFileHelper.readCredentialsFile({ [ENV_CREDENTIALS_FILE]: credentialsPath })
158
156
  expect(file.VIABLE_API_TOKEN).toBe('vib_new-token')
159
157
  expect(file.VIABLE_API_URL).toBe(ISSUER)
160
158
  })
@@ -2,14 +2,17 @@ import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
2
2
  import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
3
3
  import { tmpdir } from 'node:os'
4
4
  import { join } from 'node:path'
5
- import { claimOrJoinLock, readLock, releaseLock } from '../src/lock.js'
5
+ import { makeSignInLockHelper } from '../src/lock.js'
6
+ import type { SignInLockHelper } from '../src/lock/types.js'
6
7
 
7
8
  let dir: string
8
9
  let path: string
10
+ let lock: SignInLockHelper
9
11
 
10
12
  beforeEach(async () => {
11
13
  dir = await mkdtemp(join(tmpdir(), 'cli-auth-lock-'))
12
14
  path = join(dir, '.owlmeans.lock')
15
+ lock = makeSignInLockHelper(path)
13
16
  })
14
17
  afterEach(async () => { await rm(dir, { recursive: true, force: true }) })
15
18
 
@@ -20,33 +23,33 @@ const info = (overrides: Record<string, unknown> = {}) => ({
20
23
 
21
24
  describe('claimOrJoinLock', () => {
22
25
  test('claims an absent lock', async () => {
23
- const claim = await claimOrJoinLock(path, 'https://api.example.com', info())
26
+ const claim = await lock.claimOrJoinLock('https://api.example.com', info())
24
27
  expect(claim.owner).toBe(true)
25
28
  expect(claim.info.apiUrl).toBe('https://api.example.com')
26
29
  expect(claim.info.pid).toBe(process.pid)
27
30
 
28
- expect(await readLock(path)).toEqual(claim.info)
31
+ expect(await lock.readLock()).toEqual(claim.info)
29
32
  })
30
33
 
31
34
  test('joins a live lock for the same API URL instead of overwriting it', async () => {
32
- const first = await claimOrJoinLock(path, 'https://api.example.com', info({ userCode: 'FIRST-CODE' }))
35
+ const first = await lock.claimOrJoinLock('https://api.example.com', info({ userCode: 'FIRST-CODE' }))
33
36
  expect(first.owner).toBe(true)
34
37
 
35
- const second = await claimOrJoinLock(path, 'https://api.example.com', info({ userCode: 'SECOND-CODE' }))
38
+ const second = await lock.claimOrJoinLock('https://api.example.com', info({ userCode: 'SECOND-CODE' }))
36
39
  expect(second.owner).toBe(false)
37
40
  expect(second.info.userCode).toBe('FIRST-CODE')
38
41
  })
39
42
 
40
43
  test('reclaims a lock for a different API URL', async () => {
41
- await claimOrJoinLock(path, 'https://one.example.com', info())
42
- const claim = await claimOrJoinLock(path, 'https://two.example.com', info())
44
+ await lock.claimOrJoinLock('https://one.example.com', info())
45
+ const claim = await lock.claimOrJoinLock('https://two.example.com', info())
43
46
  expect(claim.owner).toBe(true)
44
47
  expect(claim.info.apiUrl).toBe('https://two.example.com')
45
48
  })
46
49
 
47
50
  test('reclaims an expired lock', async () => {
48
- await claimOrJoinLock(path, 'https://api.example.com', info({ expiresAt: Date.now() - 1 }))
49
- const claim = await claimOrJoinLock(path, 'https://api.example.com', info())
51
+ await lock.claimOrJoinLock('https://api.example.com', info({ expiresAt: Date.now() - 1 }))
52
+ const claim = await lock.claimOrJoinLock('https://api.example.com', info())
50
53
  expect(claim.owner).toBe(true)
51
54
  })
52
55
 
@@ -55,31 +58,31 @@ describe('claimOrJoinLock', () => {
55
58
  await writeFile(path, JSON.stringify({
56
59
  ...info(), apiUrl: 'https://api.example.com', pid: 999_999, nonce: 'stale',
57
60
  }))
58
- const claim = await claimOrJoinLock(path, 'https://api.example.com', info())
61
+ const claim = await lock.claimOrJoinLock('https://api.example.com', info())
59
62
  expect(claim.owner).toBe(true)
60
63
  })
61
64
  })
62
65
 
63
66
  describe('releaseLock', () => {
64
67
  test('removes only the lock this process itself wrote', async () => {
65
- const claim = await claimOrJoinLock(path, 'https://api.example.com', info())
66
- await releaseLock(path, claim.info.nonce)
67
- expect(await readLock(path)).toBeNull()
68
+ const claim = await lock.claimOrJoinLock('https://api.example.com', info())
69
+ await lock.releaseLock(claim.info.nonce)
70
+ expect(await lock.readLock()).toBeNull()
68
71
  })
69
72
 
70
73
  test('never removes a lock somebody else has since claimed', async () => {
71
- const first = await claimOrJoinLock(path, 'https://one.example.com', info())
72
- await claimOrJoinLock(path, 'https://two.example.com', info()) // reclaims the path, new nonce
74
+ const first = await lock.claimOrJoinLock('https://one.example.com', info())
75
+ await lock.claimOrJoinLock('https://two.example.com', info()) // reclaims the path, new nonce
73
76
 
74
- await releaseLock(path, first.info.nonce)
75
- expect((await readLock(path))?.apiUrl).toBe('https://two.example.com')
77
+ await lock.releaseLock(first.info.nonce)
78
+ expect((await lock.readLock())?.apiUrl).toBe('https://two.example.com')
76
79
  })
77
80
  })
78
81
 
79
82
  describe('readLock', () => {
80
83
  test('answers null for a missing or malformed file', async () => {
81
- expect(await readLock(path)).toBeNull()
84
+ expect(await lock.readLock()).toBeNull()
82
85
  await writeFile(path, 'not json')
83
- expect(await readLock(path)).toBeNull()
86
+ expect(await lock.readLock()).toBeNull()
84
87
  })
85
88
  })