undohaven-core 0.1.0 → 0.1.1

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.1",
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/Antipas/undohaven.git",
32
+ "directory": "packages/core"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/Antipas/undohaven/issues",
36
+ "email": "support@undohaven.com"
37
+ }
16
38
  }
package/src/funnel.js ADDED
@@ -0,0 +1,59 @@
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'])
5
+
6
+ const SCOPES = new Set(['sessions', 'storages', 'config', 'all'])
7
+ const LIMITS = new Set(['quota', 'device', 'snapshot'])
8
+
9
+ export function funnelOptedIn(config, env = {}) {
10
+ const flag = env.UNDOHAVEN_FUNNEL
11
+ if (flag === '0' || flag === 'false') return false
12
+ if (flag === '1' || flag === 'true') return true
13
+ return config?.funnelOptIn === true
14
+ }
15
+
16
+ export function anonymousMeta(name, meta) {
17
+ if (name === 'rollback') {
18
+ const scope = SCOPES.has(meta?.scope) ? meta.scope : 'all'
19
+ return { scope }
20
+ }
21
+ if (name === 'limit_hit') {
22
+ const reason = LIMITS.has(meta?.reason) ? meta.reason : 'quota'
23
+ return { reason }
24
+ }
25
+ return null
26
+ }
27
+
28
+ /** Opt-in, account-free funnel ping. No token is sent. */
29
+ export async function reportAnonymousEvent(dataDir, name, meta, env = process.env) {
30
+ if (!ANONYMOUS_FUNNEL.has(name)) return { sent: false, reason: 'name' }
31
+ const config = await loadConfig(dataDir)
32
+ if (!funnelOptedIn(config, env)) return { sent: false, reason: 'opt-out' }
33
+ const prompts = await readPrompts(dataDir)
34
+ const funnel = prompts.funnel && typeof prompts.funnel === 'object' ? { ...prompts.funnel } : {}
35
+ if ((name === 'install' || name === 'first_snapshot') && funnel[name]) return { sent: false, reason: 'once' }
36
+ const clean = anonymousMeta(name, meta)
37
+ if (name === 'limit_hit') {
38
+ const seen = funnel.limits && typeof funnel.limits === 'object' ? { ...funnel.limits } : {}
39
+ if (seen[clean.reason]) return { sent: false, reason: 'once' }
40
+ funnel.limits = seen
41
+ }
42
+ const apiBase = String(config.apiBase || env.UNDOHAVEN_API || 'https://undohaven.com').replace(/\/$/, '')
43
+ let response
44
+ try {
45
+ response = await fetch(`${apiBase}/v1/events/anonymous`, {
46
+ method: 'POST',
47
+ headers: { 'content-type': 'application/json' },
48
+ body: JSON.stringify({ name, meta: clean }),
49
+ })
50
+ } catch {
51
+ return { sent: false, reason: 'offline' }
52
+ }
53
+ if (!response.ok) return { sent: false, reason: 'http' }
54
+ if (name === 'install' || name === 'first_snapshot') funnel[name] = true
55
+ if (name === 'limit_hit') funnel.limits[clean.reason] = true
56
+ prompts.funnel = funnel
57
+ await writePrompts(dataDir, prompts)
58
+ return { sent: true }
59
+ }
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, reportAnonymousEvent } 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,22 @@
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 } 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
+ })