typevault 0.3.0 → 0.5.0

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.
@@ -1,10 +1,12 @@
1
1
  import { z } from 'zod'
2
2
 
3
+ import { sha256Hex } from '../core/crypto'
4
+ import { utf8 } from '../core/encoding'
3
5
  import { TypeVaultError } from '../core/errors'
4
6
  import {
5
7
  isFieldName,
8
+ type EntryName,
6
9
  parseEntryName,
7
- slugifySegment,
8
10
  PRESETS,
9
11
  type Preset,
10
12
  type Field,
@@ -15,22 +17,47 @@ import type { ImportedEntry, ImportResult } from './types'
15
17
 
16
18
  export type ImportItem = {
17
19
  readonly title: string
18
- readonly container?: string
20
+ /** The login on the service, naming the entry's account segment. Read from the `username` field when unset. */
21
+ readonly login?: string
19
22
  readonly preset: Preset
20
23
  readonly fields: Readonly<Record<string, string | Field | undefined>>
21
24
  readonly url?: string
22
25
  readonly tags?: readonly string[]
23
26
  readonly env?: string
27
+ /** The item's id in its password manager (`bitwarden:<id>`), when the source has stable ids. */
28
+ readonly source?: string
24
29
  }
25
30
 
26
31
  export function collectEntries(items: readonly ImportItem[], warnings: readonly string[] = []): ImportResult {
27
32
  const entries: ImportedEntry[] = []
28
33
  const messages = [...warnings]
29
34
  const used = new Set<string>()
35
+ const accounts = new Map<string, string>()
36
+ // A login whose name mixes scripts still keeps its entries apart, under a stable stand-in, rather than taking down
37
+ // every item signed in with it.
38
+ const accountSegment = (raw: string): string => {
39
+ const known = accounts.get(raw)
40
+ if (known !== undefined) return known
41
+ let segment = toSegment(raw)
42
+ if (segment !== '' && entryNameOf(segment) === undefined) {
43
+ const standIn = sha256Hex(utf8(segment)).slice(0, 8)
44
+ messages.push(`"${raw}" makes no usable name segment; its entries use "${standIn}"`)
45
+ segment = standIn
46
+ }
47
+ accounts.set(raw, segment)
48
+ return segment
49
+ }
30
50
  for (const item of items) {
31
- const service = slugifySegment(item.title)
32
- const base = item.container ? `${service}/${slugifySegment(item.container)}` : service
33
- let name = base
51
+ const title = toSegment(item.title)
52
+ // The account segment names the login on the service, never the password manager's vault or folder.
53
+ const account = accountSegment(item.login ?? loginOf(item.fields) ?? '')
54
+ // Collisions are checked on the canonical name, so two spellings of one name can't land on the same entry.
55
+ const base = entryNameOf(account === '' ? title : `${title}/${account}`)
56
+ if (base === undefined) {
57
+ messages.push(`skipped "${item.title}": its title makes no usable entry name`)
58
+ continue
59
+ }
60
+ let name: string = base
34
61
  let suffix = 2
35
62
  while (used.has(name)) name = `${base}-${suffix++}`
36
63
  used.add(name)
@@ -68,12 +95,37 @@ export function collectEntries(items: readonly ImportItem[], warnings: readonly
68
95
  fields,
69
96
  tags: item.tags ?? [],
70
97
  ...(origin === undefined ? {} : { origin }),
98
+ ...(item.source === undefined ? {} : { source: item.source }),
71
99
  ...(item.env === undefined ? {} : { env: item.env }),
72
100
  })
73
101
  }
74
102
  return { entries, warnings: messages }
75
103
  }
76
104
 
105
+ export function loginOf(fields: ImportItem['fields']): string | undefined {
106
+ const username = fields['username']
107
+ return typeof username === 'string' || username === undefined ? username : fieldStrings(username)[0]
108
+ }
109
+
110
+ /** A title or login as a name segment: lowercase, with each run of spaces and other punctuation one `-`. */
111
+ const toSegment = (raw: string): string =>
112
+ raw
113
+ .toLowerCase()
114
+ .normalize('NFC')
115
+ .replace(/[^\p{L}\p{M}\p{N}.@+]+/gu, '-')
116
+ .replace(/[.-]*-[.-]*/g, '-')
117
+ .replace(/\.{2,}/g, '.')
118
+ .replace(/^[.-]+|[.-]+$/g, '')
119
+
120
+ function entryNameOf(name: string): EntryName | undefined {
121
+ try {
122
+ return parseEntryName(name)
123
+ } catch (error) {
124
+ if (error instanceof TypeVaultError) return undefined
125
+ throw error
126
+ }
127
+ }
128
+
77
129
  /**
78
130
  * The template fields plus every labeled field the mapper didn't place, kept under its own label. A value already
79
131
  * in a template field is skipped, so an API credential's `credential` doesn't land twice.
@@ -1,6 +1,6 @@
1
1
  import type { EntryPayload } from '../core/payload'
2
2
 
3
- export type ImportedEntry = Pick<EntryPayload, 'name' | 'fields' | 'origin' | 'tags' | 'env'>
3
+ export type ImportedEntry = Pick<EntryPayload, 'name' | 'fields' | 'origin' | 'source' | 'tags' | 'env'>
4
4
 
5
5
  export type ImportResult = {
6
6
  readonly entries: readonly ImportedEntry[]