undohaven-core 0.1.0 → 0.1.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.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # undohaven-core
2
+
3
+ Library behind the `undohaven` CLI and the `dsh-undohaven` plugin. Unofficial third-party code, compatible with dsh. Not an official dsh package.
4
+
5
+ It chunks a dsh home, encrypts the chunks on the machine, and restores them. The passphrase never leaves the process. Publish and install the `undohaven` package if you want the command; this package is the engine those commands import.
6
+
7
+ Homepage: https://undohaven.com/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "undohaven-core",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "description": "Snapshot, encryption, and restore engine for UndoHaven.",
6
6
  "engines": {
@@ -12,5 +12,27 @@
12
12
  "dependencies": {
13
13
  "hash-wasm": "4.13.0"
14
14
  },
15
- "license": "MIT"
15
+ "keywords": [
16
+ "dsh",
17
+ "dsh-plugin",
18
+ "dsh-bundle",
19
+ "backup",
20
+ "snapshot",
21
+ "rollback",
22
+ "restore",
23
+ "encryption",
24
+ "e2ee",
25
+ "agent"
26
+ ],
27
+ "license": "MIT",
28
+ "homepage": "https://undohaven.com/",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git+https://github.com/OWNER/undohaven.git",
32
+ "directory": "packages/core"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/OWNER/undohaven/issues",
36
+ "email": "support@undohaven.com"
37
+ }
16
38
  }
package/src/engine.js CHANGED
@@ -216,8 +216,8 @@ export async function createSnapshot(opts) {
216
216
  }
217
217
  const storage = await loadStorageConfig(dataDir)
218
218
  const byo = storage.mode === 's3' || storage.mode === 'dir'
219
- if (byo) {
220
- await opts.cloud.createSnapshot({
219
+ const created = byo
220
+ ? await opts.cloud.createSnapshot({
221
221
  id,
222
222
  createdAt,
223
223
  kind,
@@ -229,23 +229,25 @@ export async function createSnapshot(opts) {
229
229
  encryptedManifest: record.encryptedManifest,
230
230
  pinned: record.pinned,
231
231
  })
232
- } else {
233
- const missing = await opts.cloud.missing(record.chunkIds)
234
- for (const chunkId of missing) {
235
- await opts.cloud.uploadChunk(chunkId, await readPaddedChunk(dataDir, chunkId))
236
- }
237
- await opts.cloud.createSnapshot({
238
- id,
239
- createdAt,
240
- kind,
241
- bytes,
242
- chunkIds: record.chunkIds,
243
- dshVersion: versions.dshVersion,
244
- includesCredentials: includeCredentials,
245
- encryptedManifest: record.encryptedManifest,
246
- pinned: record.pinned,
247
- })
248
- }
232
+ : await (async () => {
233
+ const missing = await opts.cloud.missing(record.chunkIds)
234
+ for (const chunkId of missing) {
235
+ await opts.cloud.uploadChunk(chunkId, await readPaddedChunk(dataDir, chunkId))
236
+ }
237
+ return opts.cloud.createSnapshot({
238
+ id,
239
+ createdAt,
240
+ kind,
241
+ bytes,
242
+ chunkIds: record.chunkIds,
243
+ dshVersion: versions.dshVersion,
244
+ includesCredentials: includeCredentials,
245
+ encryptedManifest: record.encryptedManifest,
246
+ pinned: record.pinned,
247
+ })
248
+ })()
249
+ record.pruned = Number(created?.pruned || 0)
250
+ record.plan = created?.plan || null
249
251
  record.cloud = true
250
252
  await writeSnapshotRecord(dataDir, record)
251
253
  } catch (error) {
package/src/funnel.js ADDED
@@ -0,0 +1,86 @@
1
+ import { loadConfig } from './keys.js'
2
+ import { readPrompts, writePrompts } from './prompts.js'
3
+
4
+ export const ANONYMOUS_FUNNEL = new Set(['install', 'first_snapshot', 'rollback', 'limit_hit', 'copy_install'])
5
+
6
+ const SCOPES = new Set(['sessions', 'storages', 'config', 'all'])
7
+ const LIMITS = new Set(['quota', 'device', 'snapshot', 'history'])
8
+ const UTM_SOURCES = new Set(['github', 'x', 'awesome', 'direct'])
9
+
10
+ export const TRIAL_URL = 'https://undohaven.com/pricing?utm_source=cli&utm_medium=limit'
11
+
12
+ export function funnelOptedIn(config, env = {}) {
13
+ const flag = env.UNDOHAVEN_FUNNEL
14
+ if (flag === '0' || flag === 'false') return false
15
+ if (flag === '1' || flag === 'true') return true
16
+ return config?.funnelOptIn === true
17
+ }
18
+
19
+ export function anonymousMeta(name, meta) {
20
+ if (name === 'rollback') {
21
+ const scope = SCOPES.has(meta?.scope) ? meta.scope : 'all'
22
+ return { scope }
23
+ }
24
+ if (name === 'limit_hit') {
25
+ const reason = LIMITS.has(meta?.reason) ? meta.reason : 'quota'
26
+ return { reason }
27
+ }
28
+ if (name === 'copy_install') {
29
+ const source = UTM_SOURCES.has(meta?.source) ? meta.source : 'direct'
30
+ return { source }
31
+ }
32
+ return null
33
+ }
34
+
35
+ export function limitNudgeLine(kind) {
36
+ if (kind === 'history') {
37
+ return `Free history is 7 days. A 14-day Pro trial needs no card: ${TRIAL_URL}`
38
+ }
39
+ return `A second device needs Pro. A 14-day trial needs no card: ${TRIAL_URL}`
40
+ }
41
+
42
+ /** Print-once local flag. The funnel ping stays inside reportAnonymousEvent, so it is sent only when opted in. */
43
+ export async function showLimitNudge(dataDir, kind, env = process.env) {
44
+ const prompts = await readPrompts(dataDir)
45
+ const nudges = prompts.nudges && typeof prompts.nudges === 'object' ? { ...prompts.nudges } : {}
46
+ if (nudges[kind]) return { shown: false, line: '' }
47
+ nudges[kind] = true
48
+ prompts.nudges = nudges
49
+ await writePrompts(dataDir, prompts)
50
+ const reason = kind === 'history' ? 'history' : 'device'
51
+ await reportAnonymousEvent(dataDir, 'limit_hit', { reason }, env).catch(() => {})
52
+ return { shown: true, line: limitNudgeLine(kind) }
53
+ }
54
+
55
+ /** Opt-in, account-free funnel ping. No token is sent. */
56
+ export async function reportAnonymousEvent(dataDir, name, meta, env = process.env) {
57
+ if (!ANONYMOUS_FUNNEL.has(name)) return { sent: false, reason: 'name' }
58
+ const config = await loadConfig(dataDir)
59
+ if (!funnelOptedIn(config, env)) return { sent: false, reason: 'opt-out' }
60
+ const prompts = await readPrompts(dataDir)
61
+ const funnel = prompts.funnel && typeof prompts.funnel === 'object' ? { ...prompts.funnel } : {}
62
+ if ((name === 'install' || name === 'first_snapshot') && funnel[name]) return { sent: false, reason: 'once' }
63
+ const clean = anonymousMeta(name, meta)
64
+ if (name === 'limit_hit') {
65
+ const seen = funnel.limits && typeof funnel.limits === 'object' ? { ...funnel.limits } : {}
66
+ if (seen[clean.reason]) return { sent: false, reason: 'once' }
67
+ funnel.limits = seen
68
+ }
69
+ const apiBase = String(config.apiBase || env.UNDOHAVEN_API || 'https://undohaven.com').replace(/\/$/, '')
70
+ let response
71
+ try {
72
+ response = await fetch(`${apiBase}/v1/events/anonymous`, {
73
+ method: 'POST',
74
+ headers: { 'content-type': 'application/json' },
75
+ body: JSON.stringify({ name, meta: clean }),
76
+ })
77
+ } catch {
78
+ return { sent: false, reason: 'offline' }
79
+ }
80
+ if (!response.ok) return { sent: false, reason: 'http' }
81
+ if (name === 'install' || name === 'first_snapshot') funnel[name] = true
82
+ if (name === 'limit_hit') funnel.limits[clean.reason] = true
83
+ prompts.funnel = funnel
84
+ await writePrompts(dataDir, prompts)
85
+ return { sent: true }
86
+ }
package/src/index.js CHANGED
@@ -61,4 +61,5 @@ export {
61
61
  export { PLANS, PRICES, PRICE_ENV, effectivePlan, versionLine, isCrossMajorDowngrade } from './plans.js'
62
62
  export { padBlob, unpadBlob, loadStorageConfig, saveStorageConfig, redactStorage, s3Call, newDeviceLabel } from './byo.js'
63
63
  export { UPGRADE, readPrompts, writePrompts } from './prompts.js'
64
+ export { ANONYMOUS_FUNNEL, anonymousMeta, funnelOptedIn, limitNudgeLine, reportAnonymousEvent, showLimitNudge, TRIAL_URL } from './funnel.js'
64
65
  export { readPaddedChunk } from './store.js'
@@ -107,7 +107,7 @@ test('crypto roundtrip, unique nonces, recovery words, and argon2 wrapping', asy
107
107
  assert.deepEqual(unwrapWithRecovery(keys.words, keys.wrappedDekRecovery), keys.dek)
108
108
  await assert.rejects(() => unwrapWithPassphrase('wrong passphrase', keys.salt, keys.wrappedDek, keys.kdf))
109
109
  const words = keys.words.slice()
110
- words[3] = words[3] === 'abandon' ? 'ability' : 'abandon'
110
+ words[3] = 'notaword'
111
111
  assert.throws(() => entropyFromWords(words))
112
112
  })
113
113
 
@@ -0,0 +1,35 @@
1
+ import assert from 'node:assert/strict'
2
+ import { mkdtemp, rm } from 'node:fs/promises'
3
+ import { tmpdir } from 'node:os'
4
+ import { join } from 'node:path'
5
+ import test from 'node:test'
6
+ import { anonymousMeta, funnelOptedIn, reportAnonymousEvent, showLimitNudge } from '../src/funnel.js'
7
+ import { loadConfig, saveConfig } from '../src/keys.js'
8
+
9
+ test('anonymous funnel is opt-in and strips paths', async () => {
10
+ assert.equal(funnelOptedIn({}, {}), false)
11
+ assert.equal(funnelOptedIn({ funnelOptIn: true }, {}), true)
12
+ assert.equal(funnelOptedIn({ funnelOptIn: true }, { UNDOHAVEN_FUNNEL: '0' }), false)
13
+ assert.deepEqual(anonymousMeta('limit_hit', { reason: 'device', path: '/tmp/secret' }), { reason: 'device' })
14
+ assert.deepEqual(anonymousMeta('install', { path: '/tmp/secret' }), null)
15
+ const dir = await mkdtemp(join(tmpdir(), 'funnel-'))
16
+ const skipped = await reportAnonymousEvent(dir, 'install', null, {})
17
+ assert.equal(skipped.reason, 'opt-out')
18
+ await saveConfig(dir, { ...(await loadConfig(dir)), funnelOptIn: true, apiBase: 'http://127.0.0.1:1' })
19
+ const offline = await reportAnonymousEvent(dir, 'first_snapshot', null, {})
20
+ assert.equal(offline.sent, false)
21
+ await rm(dir, { recursive: true, force: true })
22
+ })
23
+
24
+ test('a free-limit trial line is printed once per limit type', async () => {
25
+ const dir = await mkdtemp(join(tmpdir(), 'nudge-'))
26
+ const first = await showLimitNudge(dir, 'history', {})
27
+ const again = await showLimitNudge(dir, 'history', {})
28
+ const device = await showLimitNudge(dir, 'device', {})
29
+ assert.equal(first.shown, true)
30
+ assert.match(first.line, /14-day Pro trial/)
31
+ assert.match(first.line, /utm_source=cli&utm_medium=limit/)
32
+ assert.equal(again.shown, false)
33
+ assert.equal(device.shown, true)
34
+ await rm(dir, { recursive: true, force: true })
35
+ })