@private.me/xbind 1.2.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.
Files changed (295) hide show
  1. package/AGENTS.md +778 -0
  2. package/LICENSE.md +27 -0
  3. package/README.md +400 -0
  4. package/dist-standalone/_deps/crypto/base64.d.ts +29 -0
  5. package/dist-standalone/_deps/crypto/base64.js +97 -0
  6. package/dist-standalone/_deps/crypto/cjs/base64.js +103 -0
  7. package/dist-standalone/_deps/crypto/cjs/errors.js +119 -0
  8. package/dist-standalone/_deps/crypto/cjs/hmac.js +71 -0
  9. package/dist-standalone/_deps/crypto/cjs/index.js +86 -0
  10. package/dist-standalone/_deps/crypto/cjs/padding.js +57 -0
  11. package/dist-standalone/_deps/crypto/cjs/share-header.js +68 -0
  12. package/dist-standalone/_deps/crypto/cjs/shares.js +152 -0
  13. package/dist-standalone/_deps/crypto/cjs/tlv.js +199 -0
  14. package/dist-standalone/_deps/crypto/cjs/uuid.js +61 -0
  15. package/dist-standalone/_deps/crypto/cjs/verify.js +24 -0
  16. package/dist-standalone/_deps/crypto/cjs/xorida.js +221 -0
  17. package/dist-standalone/_deps/crypto/errors.d.ts +51 -0
  18. package/dist-standalone/_deps/crypto/errors.js +109 -0
  19. package/dist-standalone/_deps/crypto/hmac.d.ts +39 -0
  20. package/dist-standalone/_deps/crypto/hmac.js +66 -0
  21. package/dist-standalone/_deps/crypto/index.d.ts +20 -0
  22. package/dist-standalone/_deps/crypto/index.js +45 -0
  23. package/dist-standalone/_deps/crypto/padding.d.ts +19 -0
  24. package/dist-standalone/_deps/crypto/padding.js +53 -0
  25. package/dist-standalone/_deps/crypto/share-header.d.ts +44 -0
  26. package/dist-standalone/_deps/crypto/share-header.js +63 -0
  27. package/dist-standalone/_deps/crypto/shares.d.ts +27 -0
  28. package/dist-standalone/_deps/crypto/shares.js +148 -0
  29. package/dist-standalone/_deps/crypto/tlv.d.ts +26 -0
  30. package/dist-standalone/_deps/crypto/tlv.js +195 -0
  31. package/dist-standalone/_deps/crypto/uuid.d.ts +22 -0
  32. package/dist-standalone/_deps/crypto/uuid.js +56 -0
  33. package/dist-standalone/_deps/crypto/verify.d.ts +15 -0
  34. package/dist-standalone/_deps/crypto/verify.js +15 -0
  35. package/dist-standalone/_deps/crypto/xorida.d.ts +44 -0
  36. package/dist-standalone/_deps/crypto/xorida.js +215 -0
  37. package/dist-standalone/_deps/mldsa-wasm/LICENSE +24 -0
  38. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -0
  39. package/dist-standalone/_deps/mldsa-wasm/package.json +46 -0
  40. package/dist-standalone/_deps/mldsa-wasm/types/mldsa.d.ts +30 -0
  41. package/dist-standalone/_deps/shared/cjs/errors.js +582 -0
  42. package/dist-standalone/_deps/shared/cjs/index.js +492 -0
  43. package/dist-standalone/_deps/shared/cjs/package.json +1 -0
  44. package/dist-standalone/_deps/shared/cjs/types.js +403 -0
  45. package/dist-standalone/_deps/shared/errors.d.ts +48 -0
  46. package/dist-standalone/_deps/shared/errors.d.ts.map +1 -0
  47. package/dist-standalone/_deps/shared/errors.js +192 -0
  48. package/dist-standalone/_deps/shared/errors.js.map +1 -0
  49. package/dist-standalone/_deps/shared/index.d.ts +4 -0
  50. package/dist-standalone/_deps/shared/index.d.ts.map +1 -0
  51. package/dist-standalone/_deps/shared/index.js +78 -0
  52. package/dist-standalone/_deps/shared/index.js.map +1 -0
  53. package/dist-standalone/_deps/shared/types.d.ts +1097 -0
  54. package/dist-standalone/_deps/shared/types.d.ts.map +1 -0
  55. package/dist-standalone/_deps/shared/types.js +89 -0
  56. package/dist-standalone/_deps/shared/types.js.map +1 -0
  57. package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts +115 -0
  58. package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +1 -0
  59. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -0
  60. package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +1 -0
  61. package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts +13 -0
  62. package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +1 -0
  63. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -0
  64. package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +1 -0
  65. package/dist-standalone/_deps/ux-helpers/cjs/package.json +1 -0
  66. package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts +39 -0
  67. package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +1 -0
  68. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +83 -0
  69. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +1 -0
  70. package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts +99 -0
  71. package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +1 -0
  72. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +143 -0
  73. package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +1 -0
  74. package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts +32 -0
  75. package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +1 -0
  76. package/dist-standalone/_deps/ux-helpers/cjs/search.js +119 -0
  77. package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +1 -0
  78. package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts +109 -0
  79. package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +1 -0
  80. package/dist-standalone/_deps/ux-helpers/cjs/types.js +8 -0
  81. package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +1 -0
  82. package/dist-standalone/_deps/ux-helpers/errors.d.ts +115 -0
  83. package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +1 -0
  84. package/dist-standalone/_deps/ux-helpers/errors.js +253 -0
  85. package/dist-standalone/_deps/ux-helpers/errors.js.map +1 -0
  86. package/dist-standalone/_deps/ux-helpers/index.d.ts +13 -0
  87. package/dist-standalone/_deps/ux-helpers/index.d.ts.map +1 -0
  88. package/dist-standalone/_deps/ux-helpers/index.js +16 -0
  89. package/dist-standalone/_deps/ux-helpers/index.js.map +1 -0
  90. package/dist-standalone/_deps/ux-helpers/pagination.d.ts +39 -0
  91. package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +1 -0
  92. package/dist-standalone/_deps/ux-helpers/pagination.js +79 -0
  93. package/dist-standalone/_deps/ux-helpers/pagination.js.map +1 -0
  94. package/dist-standalone/_deps/ux-helpers/progress.d.ts +99 -0
  95. package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +1 -0
  96. package/dist-standalone/_deps/ux-helpers/progress.js +138 -0
  97. package/dist-standalone/_deps/ux-helpers/progress.js.map +1 -0
  98. package/dist-standalone/_deps/ux-helpers/search.d.ts +32 -0
  99. package/dist-standalone/_deps/ux-helpers/search.d.ts.map +1 -0
  100. package/dist-standalone/_deps/ux-helpers/search.js +116 -0
  101. package/dist-standalone/_deps/ux-helpers/search.js.map +1 -0
  102. package/dist-standalone/_deps/ux-helpers/types.d.ts +109 -0
  103. package/dist-standalone/_deps/ux-helpers/types.d.ts.map +1 -0
  104. package/dist-standalone/_deps/ux-helpers/types.js +7 -0
  105. package/dist-standalone/_deps/ux-helpers/types.js.map +1 -0
  106. package/dist-standalone/_deps/xchange/auto-accept.d.ts +127 -0
  107. package/dist-standalone/_deps/xchange/auto-accept.js +1 -0
  108. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -0
  109. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -0
  110. package/dist-standalone/_deps/xchange/cjs/index.js +1 -0
  111. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -0
  112. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -0
  113. package/dist-standalone/_deps/xchange/cjs/package.json +1 -0
  114. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -0
  115. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -0
  116. package/dist-standalone/_deps/xchange/errors.d.ts +69 -0
  117. package/dist-standalone/_deps/xchange/errors.js +1 -0
  118. package/dist-standalone/_deps/xchange/index.d.ts +15 -0
  119. package/dist-standalone/_deps/xchange/index.js +1 -0
  120. package/dist-standalone/_deps/xchange/invite-client.d.ts +178 -0
  121. package/dist-standalone/_deps/xchange/invite-client.js +1 -0
  122. package/dist-standalone/_deps/xchange/lazy-init.d.ts +176 -0
  123. package/dist-standalone/_deps/xchange/lazy-init.js +1 -0
  124. package/dist-standalone/_deps/xchange/trust-integration.d.ts +102 -0
  125. package/dist-standalone/_deps/xchange/trust-integration.js +1 -0
  126. package/dist-standalone/_deps/xchange/xchange.d.ts +60 -0
  127. package/dist-standalone/_deps/xchange/xchange.js +1 -0
  128. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -0
  129. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -0
  130. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -0
  131. package/dist-standalone/_deps/xregistry/cjs/package.json +1 -0
  132. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -0
  133. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -0
  134. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -0
  135. package/dist-standalone/_deps/xregistry/discovery.d.ts +126 -0
  136. package/dist-standalone/_deps/xregistry/discovery.d.ts.map +1 -0
  137. package/dist-standalone/_deps/xregistry/discovery.js +1 -0
  138. package/dist-standalone/_deps/xregistry/discovery.js.map +1 -0
  139. package/dist-standalone/_deps/xregistry/errors.d.ts +41 -0
  140. package/dist-standalone/_deps/xregistry/errors.d.ts.map +1 -0
  141. package/dist-standalone/_deps/xregistry/errors.js +1 -0
  142. package/dist-standalone/_deps/xregistry/errors.js.map +1 -0
  143. package/dist-standalone/_deps/xregistry/index.d.ts +8 -0
  144. package/dist-standalone/_deps/xregistry/index.d.ts.map +1 -0
  145. package/dist-standalone/_deps/xregistry/index.js +1 -0
  146. package/dist-standalone/_deps/xregistry/index.js.map +1 -0
  147. package/dist-standalone/_deps/xregistry/registry.d.ts +85 -0
  148. package/dist-standalone/_deps/xregistry/registry.d.ts.map +1 -0
  149. package/dist-standalone/_deps/xregistry/registry.js +1 -0
  150. package/dist-standalone/_deps/xregistry/registry.js.map +1 -0
  151. package/dist-standalone/_deps/xregistry/schema.d.ts +81 -0
  152. package/dist-standalone/_deps/xregistry/schema.d.ts.map +1 -0
  153. package/dist-standalone/_deps/xregistry/schema.js +1 -0
  154. package/dist-standalone/_deps/xregistry/schema.js.map +1 -0
  155. package/dist-standalone/_deps/xregistry/types.d.ts +95 -0
  156. package/dist-standalone/_deps/xregistry/types.d.ts.map +1 -0
  157. package/dist-standalone/_deps/xregistry/types.js +1 -0
  158. package/dist-standalone/_deps/xregistry/types.js.map +1 -0
  159. package/dist-standalone/agent-call.d.ts +286 -0
  160. package/dist-standalone/agent-call.js +642 -0
  161. package/dist-standalone/agent-sdk.d.ts +207 -0
  162. package/dist-standalone/agent-sdk.js +328 -0
  163. package/dist-standalone/agent.d.ts +670 -0
  164. package/dist-standalone/agent.js +1529 -0
  165. package/dist-standalone/approval.d.ts +145 -0
  166. package/dist-standalone/approval.js +193 -0
  167. package/dist-standalone/auth.d.ts +75 -0
  168. package/dist-standalone/auth.js +219 -0
  169. package/dist-standalone/auto-accept.d.ts +102 -0
  170. package/dist-standalone/auto-accept.js +229 -0
  171. package/dist-standalone/backup-config.d.ts +150 -0
  172. package/dist-standalone/backup-config.js +201 -0
  173. package/dist-standalone/checkpoint.d.ts +125 -0
  174. package/dist-standalone/checkpoint.js +186 -0
  175. package/dist-standalone/cjs/agent-call.js +651 -0
  176. package/dist-standalone/cjs/agent-sdk.js +332 -0
  177. package/dist-standalone/cjs/agent.js +1566 -0
  178. package/dist-standalone/cjs/approval.js +199 -0
  179. package/dist-standalone/cjs/auth.js +225 -0
  180. package/dist-standalone/cjs/auto-accept.js +233 -0
  181. package/dist-standalone/cjs/backup-config.js +207 -0
  182. package/dist-standalone/cjs/checkpoint.js +193 -0
  183. package/dist-standalone/cjs/cli/init.js +487 -0
  184. package/dist-standalone/cjs/connect.js +312 -0
  185. package/dist-standalone/cjs/did-document.js +101 -0
  186. package/dist-standalone/cjs/did-privateme.js +130 -0
  187. package/dist-standalone/cjs/did-web.js +201 -0
  188. package/dist-standalone/cjs/discovery.js +462 -0
  189. package/dist-standalone/cjs/dual-mode.js +251 -0
  190. package/dist-standalone/cjs/email-templates.js +313 -0
  191. package/dist-standalone/cjs/email-transport.js +239 -0
  192. package/dist-standalone/cjs/envelope.js +510 -0
  193. package/dist-standalone/cjs/errors.js +562 -0
  194. package/dist-standalone/cjs/gateway-state.js +55 -0
  195. package/dist-standalone/cjs/gateway-transport.js +120 -0
  196. package/dist-standalone/cjs/guardrails.js +223 -0
  197. package/dist-standalone/cjs/http-compat.js +272 -0
  198. package/dist-standalone/cjs/identity.js +541 -0
  199. package/dist-standalone/cjs/index.js +224 -0
  200. package/dist-standalone/cjs/invitation.js +421 -0
  201. package/dist-standalone/cjs/invite.js +328 -0
  202. package/dist-standalone/cjs/key-agreement.js +246 -0
  203. package/dist-standalone/cjs/lazy-init.js +300 -0
  204. package/dist-standalone/cjs/mdns-discovery.js +202 -0
  205. package/dist-standalone/cjs/nonce-store.js +66 -0
  206. package/dist-standalone/cjs/package.json +3 -0
  207. package/dist-standalone/cjs/pairing-manager.js +223 -0
  208. package/dist-standalone/cjs/policy.js +320 -0
  209. package/dist-standalone/cjs/redis-nonce-store.js +76 -0
  210. package/dist-standalone/cjs/registry-middleware.js +50 -0
  211. package/dist-standalone/cjs/retry-transport.js +102 -0
  212. package/dist-standalone/cjs/security-policy.js +204 -0
  213. package/dist-standalone/cjs/split-channel.js +177 -0
  214. package/dist-standalone/cjs/subscription-proof.js +230 -0
  215. package/dist-standalone/cjs/succession.js +148 -0
  216. package/dist-standalone/cjs/transport.js +63 -0
  217. package/dist-standalone/cjs/trust-registry.js +742 -0
  218. package/dist-standalone/cjs/verify.js +25 -0
  219. package/dist-standalone/cjs/xfetch.js +252 -0
  220. package/dist-standalone/cli/init.d.ts +63 -0
  221. package/dist-standalone/cli/init.js +450 -0
  222. package/dist-standalone/connect.d.ts +143 -0
  223. package/dist-standalone/connect.js +274 -0
  224. package/dist-standalone/did-document.d.ts +65 -0
  225. package/dist-standalone/did-document.js +96 -0
  226. package/dist-standalone/did-privateme.d.ts +70 -0
  227. package/dist-standalone/did-privateme.js +121 -0
  228. package/dist-standalone/did-web.d.ts +73 -0
  229. package/dist-standalone/did-web.js +196 -0
  230. package/dist-standalone/discovery.d.ts +176 -0
  231. package/dist-standalone/discovery.js +458 -0
  232. package/dist-standalone/dual-mode.d.ts +145 -0
  233. package/dist-standalone/dual-mode.js +247 -0
  234. package/dist-standalone/email-templates.d.ts +41 -0
  235. package/dist-standalone/email-templates.js +309 -0
  236. package/dist-standalone/email-transport.d.ts +139 -0
  237. package/dist-standalone/email-transport.js +232 -0
  238. package/dist-standalone/envelope.d.ts +288 -0
  239. package/dist-standalone/envelope.js +497 -0
  240. package/dist-standalone/errors.d.ts +74 -0
  241. package/dist-standalone/errors.js +548 -0
  242. package/dist-standalone/gateway-state.d.ts +32 -0
  243. package/dist-standalone/gateway-state.js +51 -0
  244. package/dist-standalone/gateway-transport.d.ts +59 -0
  245. package/dist-standalone/gateway-transport.js +116 -0
  246. package/dist-standalone/guardrails.d.ts +136 -0
  247. package/dist-standalone/guardrails.js +216 -0
  248. package/dist-standalone/http-compat.d.ts +150 -0
  249. package/dist-standalone/http-compat.js +267 -0
  250. package/dist-standalone/identity.d.ts +176 -0
  251. package/dist-standalone/identity.js +516 -0
  252. package/dist-standalone/index.d.ts +83 -0
  253. package/dist-standalone/index.js +51 -0
  254. package/dist-standalone/invitation.d.ts +211 -0
  255. package/dist-standalone/invitation.js +415 -0
  256. package/dist-standalone/invite.d.ts +192 -0
  257. package/dist-standalone/invite.js +324 -0
  258. package/dist-standalone/key-agreement.d.ts +122 -0
  259. package/dist-standalone/key-agreement.js +236 -0
  260. package/dist-standalone/lazy-init.d.ts +167 -0
  261. package/dist-standalone/lazy-init.js +295 -0
  262. package/dist-standalone/mdns-discovery.d.ts +117 -0
  263. package/dist-standalone/mdns-discovery.js +195 -0
  264. package/dist-standalone/nonce-store.d.ts +39 -0
  265. package/dist-standalone/nonce-store.js +62 -0
  266. package/dist-standalone/package.json +11 -0
  267. package/dist-standalone/pairing-manager.d.ts +147 -0
  268. package/dist-standalone/pairing-manager.js +219 -0
  269. package/dist-standalone/policy.d.ts +150 -0
  270. package/dist-standalone/policy.js +315 -0
  271. package/dist-standalone/redis-nonce-store.d.ts +93 -0
  272. package/dist-standalone/redis-nonce-store.js +72 -0
  273. package/dist-standalone/registry-middleware.d.ts +38 -0
  274. package/dist-standalone/registry-middleware.js +47 -0
  275. package/dist-standalone/retry-transport.d.ts +76 -0
  276. package/dist-standalone/retry-transport.js +98 -0
  277. package/dist-standalone/security-policy.d.ts +146 -0
  278. package/dist-standalone/security-policy.js +198 -0
  279. package/dist-standalone/split-channel.d.ts +69 -0
  280. package/dist-standalone/split-channel.js +171 -0
  281. package/dist-standalone/subscription-proof.d.ts +103 -0
  282. package/dist-standalone/subscription-proof.js +224 -0
  283. package/dist-standalone/succession.d.ts +57 -0
  284. package/dist-standalone/succession.js +142 -0
  285. package/dist-standalone/transport.d.ts +50 -0
  286. package/dist-standalone/transport.js +59 -0
  287. package/dist-standalone/trust-registry.d.ts +286 -0
  288. package/dist-standalone/trust-registry.js +702 -0
  289. package/dist-standalone/verify.d.ts +16 -0
  290. package/dist-standalone/verify.js +16 -0
  291. package/dist-standalone/xfetch.d.ts +129 -0
  292. package/dist-standalone/xfetch.js +247 -0
  293. package/llms.txt +800 -0
  294. package/package.json +79 -0
  295. package/share1.dat +0 -0
@@ -0,0 +1,450 @@
1
+ #!/usr/bin/env node
2
+ /* eslint-disable no-console */
3
+ /**
4
+ * @module cli/init
5
+ * One-line CLI setup for XBind connections.
6
+ *
7
+ * Enables: npx xbind-init --invite <code>
8
+ *
9
+ * Flow:
10
+ * 1. Parse invite URL or code
11
+ * 2. Fetch invite details from registry server
12
+ * 3. Interactive prompts for: project name, runtime, TypeScript vs JavaScript
13
+ * 4. Generate project directory with template files
14
+ * 5. Install dependencies
15
+ * 6. Write .env file with XBIND_INVITE_CODE
16
+ * 7. Register with XBind registry (auto-accept flow)
17
+ * 8. Print success message with next steps
18
+ *
19
+ * @example
20
+ * ```bash
21
+ * npx xbind-init --invite https://xbind.to/invite/XBD-abc123
22
+ *
23
+ * # Prompts:
24
+ * # → Project name: my-xbind-app
25
+ * # → Runtime: node-typescript
26
+ * # Creating project...
27
+ * # Installing dependencies...
28
+ * # Registering with XBind...
29
+ * # ✅ Ready! Run: cd my-xbind-app && npm start
30
+ * ```
31
+ */
32
+ import { parseArgs } from 'node:util';
33
+ import * as fs from 'node:fs';
34
+ import * as path from 'node:path';
35
+ import { spawn } from 'node:child_process';
36
+ import * as readline from 'node:readline/promises';
37
+ import { stdin as input, stdout as output } from 'node:process';
38
+ import { InviteService } from '../invite.js';
39
+ // Get template directory path
40
+ // In production, templates are at ../templates relative to dist/cli/init.js
41
+ // We'll resolve this dynamically based on the package root
42
+ function getTemplateDir() {
43
+ // Find package root by looking for package.json
44
+ let currentDir = __dirname;
45
+ while (currentDir !== '/') {
46
+ if (fs.existsSync(path.join(currentDir, 'package.json'))) {
47
+ return path.join(currentDir, 'templates');
48
+ }
49
+ currentDir = path.dirname(currentDir);
50
+ }
51
+ // Fallback: assume we're in dist/cli and templates are in package root
52
+ return path.join(__dirname, '../../templates');
53
+ }
54
+ const TEMPLATES = {
55
+ 'node-typescript': {
56
+ name: 'Node.js + TypeScript',
57
+ description: 'Node.js with TypeScript, best for backend services',
58
+ packageManager: 'npm',
59
+ entryPoint: 'src/index.ts',
60
+ startCommand: 'npm run dev',
61
+ },
62
+ 'node-javascript': {
63
+ name: 'Node.js + JavaScript',
64
+ description: 'Node.js with JavaScript, minimal setup',
65
+ packageManager: 'npm',
66
+ entryPoint: 'src/index.js',
67
+ startCommand: 'npm start',
68
+ },
69
+ 'deno': {
70
+ name: 'Deno',
71
+ description: 'Modern TypeScript runtime',
72
+ packageManager: 'deno',
73
+ entryPoint: 'mod.ts',
74
+ startCommand: 'deno run --allow-net --allow-env --allow-read mod.ts',
75
+ },
76
+ 'cloudflare-worker': {
77
+ name: 'Cloudflare Worker',
78
+ description: 'Edge runtime on Cloudflare',
79
+ packageManager: 'npm',
80
+ entryPoint: 'src/index.ts',
81
+ startCommand: 'npm run dev',
82
+ },
83
+ 'vercel-function': {
84
+ name: 'Vercel Function',
85
+ description: 'Serverless function on Vercel',
86
+ packageManager: 'npm',
87
+ entryPoint: 'api/xbind.ts',
88
+ startCommand: 'npm run dev',
89
+ },
90
+ };
91
+ /**
92
+ * Spinner for progress indication.
93
+ */
94
+ class Spinner {
95
+ frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
96
+ currentFrame = 0;
97
+ intervalId;
98
+ message;
99
+ constructor(message) {
100
+ this.message = message;
101
+ }
102
+ start() {
103
+ this.intervalId = setInterval(() => {
104
+ const frame = this.frames[this.currentFrame];
105
+ process.stdout.write(`\r${frame} ${this.message}`);
106
+ this.currentFrame = (this.currentFrame + 1) % this.frames.length;
107
+ }, 80);
108
+ }
109
+ succeed(message) {
110
+ this.stop();
111
+ process.stdout.write(`\r✅ ${message ?? this.message}\n`);
112
+ }
113
+ fail(message) {
114
+ this.stop();
115
+ process.stdout.write(`\r❌ ${message ?? this.message}\n`);
116
+ }
117
+ stop() {
118
+ if (this.intervalId) {
119
+ clearInterval(this.intervalId);
120
+ this.intervalId = undefined;
121
+ }
122
+ }
123
+ }
124
+ /**
125
+ * Parse invite code/URL to extract invite ID.
126
+ *
127
+ * @param inviteInput - Invite URL or code
128
+ * @returns Invite URL
129
+ */
130
+ function normalizeInviteUrl(inviteInput, registryUrl) {
131
+ // If it's already a full URL, return as-is
132
+ if (inviteInput.startsWith('http://') || inviteInput.startsWith('https://')) {
133
+ return inviteInput;
134
+ }
135
+ // If it starts with XBD-, treat as code
136
+ if (inviteInput.startsWith('XBD-')) {
137
+ return `${registryUrl}/invite/${inviteInput}`;
138
+ }
139
+ // Otherwise, assume it's an ID
140
+ return `${registryUrl}/invite/${inviteInput}`;
141
+ }
142
+ /**
143
+ * Validate project name.
144
+ *
145
+ * @param name - Project name
146
+ * @returns true if valid
147
+ */
148
+ function isValidProjectName(name) {
149
+ return /^[a-z0-9-_]+$/i.test(name) && name.length > 0 && name.length <= 100;
150
+ }
151
+ /**
152
+ * Prompt user for input.
153
+ *
154
+ * @param question - Question text
155
+ * @param defaultValue - Default value
156
+ * @returns User input
157
+ */
158
+ async function prompt(question, defaultValue) {
159
+ const rl = readline.createInterface({ input, output });
160
+ const answer = await rl.question(defaultValue ? `${question} (${defaultValue}): ` : `${question}: `);
161
+ rl.close();
162
+ return answer.trim() || defaultValue || '';
163
+ }
164
+ /**
165
+ * Prompt user to select from a list.
166
+ *
167
+ * @param question - Question text
168
+ * @param choices - Available choices
169
+ * @param defaultIndex - Default choice index
170
+ * @returns Selected choice
171
+ */
172
+ async function select(question, choices, defaultIndex = 0) {
173
+ console.log(question);
174
+ choices.forEach((choice, i) => {
175
+ const prefix = i === defaultIndex ? '→' : ' ';
176
+ console.log(` ${prefix} ${i + 1}. ${choice.label} - ${choice.description}`);
177
+ });
178
+ const rl = readline.createInterface({ input, output });
179
+ const answer = await rl.question(`Select (1-${choices.length}): `);
180
+ rl.close();
181
+ const selectedIndex = parseInt(answer.trim(), 10) - 1;
182
+ if (selectedIndex >= 0 && selectedIndex < choices.length) {
183
+ return choices[selectedIndex]?.value ?? choices[defaultIndex]?.value ?? '';
184
+ }
185
+ return choices[defaultIndex]?.value ?? '';
186
+ }
187
+ /**
188
+ * Run shell command.
189
+ *
190
+ * @param command - Command to run
191
+ * @param args - Command arguments
192
+ * @param cwd - Working directory
193
+ * @returns Promise that resolves when command completes
194
+ */
195
+ function runCommand(command, args, cwd) {
196
+ return new Promise((resolve, reject) => {
197
+ const proc = spawn(command, args, {
198
+ cwd,
199
+ stdio: 'ignore',
200
+ shell: true,
201
+ });
202
+ proc.on('close', (code) => {
203
+ if (code === 0) {
204
+ resolve();
205
+ }
206
+ else {
207
+ reject(new Error(`Command failed with code ${code ?? 'unknown'}`));
208
+ }
209
+ });
210
+ proc.on('error', reject);
211
+ });
212
+ }
213
+ /**
214
+ * Copy template files to project directory.
215
+ *
216
+ * @param templateName - Template name
217
+ * @param projectPath - Project directory path
218
+ * @param invite - Invite details
219
+ */
220
+ async function copyTemplate(templateName, projectPath, invite) {
221
+ const templateDir = path.join(getTemplateDir(), templateName);
222
+ // Read all files in template directory
223
+ const files = await fs.promises.readdir(templateDir, { recursive: true, withFileTypes: true });
224
+ for (const file of files) {
225
+ if (file.isFile()) {
226
+ // Get the parent path from the file dirent
227
+ // Note: file.parentPath was added in Node.js 20.1.0, file.path is deprecated
228
+ const parentPath = file.parentPath || file.path || templateDir;
229
+ const relativePath = path.relative(templateDir, path.join(parentPath, file.name));
230
+ const sourcePath = path.join(templateDir, relativePath);
231
+ const destPath = path.join(projectPath, relativePath);
232
+ // Create directory if it doesn't exist
233
+ await fs.promises.mkdir(path.dirname(destPath), { recursive: true });
234
+ // Read template file
235
+ let content = await fs.promises.readFile(sourcePath, 'utf-8');
236
+ // Replace placeholders
237
+ if (invite) {
238
+ content = content.replace(/{{INVITE_URL}}/g, invite.url);
239
+ content = content.replace(/{{INVITE_ID}}/g, invite.id);
240
+ content = content.replace(/{{SERVICE_NAME}}/g, invite.from.name);
241
+ content = content.replace(/{{SERVICE_DID}}/g, invite.from.did);
242
+ content = content.replace(/{{SERVICE_ENDPOINT}}/g, invite.from.endpoint);
243
+ }
244
+ // Write to destination
245
+ await fs.promises.writeFile(destPath, content, 'utf-8');
246
+ }
247
+ }
248
+ }
249
+ /**
250
+ * Install dependencies.
251
+ *
252
+ * @param projectPath - Project directory
253
+ * @param packageManager - Package manager to use
254
+ */
255
+ async function installDependencies(projectPath, packageManager) {
256
+ if (packageManager === 'npm') {
257
+ await runCommand('npm', ['install'], projectPath);
258
+ }
259
+ else if (packageManager === 'deno') {
260
+ // Deno doesn't need install, it auto-downloads
261
+ }
262
+ // 'none' for templates that don't need dependencies
263
+ }
264
+ /**
265
+ * Main init command.
266
+ *
267
+ * @param options - Init options
268
+ */
269
+ export async function initCommand(options = {}) {
270
+ const registryUrl = options.registryUrl || 'https://xbind.to';
271
+ // Step 1: Get invite details
272
+ let invite;
273
+ let inviteUrl;
274
+ if (options.invite) {
275
+ inviteUrl = normalizeInviteUrl(options.invite, registryUrl);
276
+ const spinner = new Spinner('Fetching invite details...');
277
+ spinner.start();
278
+ try {
279
+ const inviteService = new InviteService({ inviteApiUrl: registryUrl });
280
+ const inviteResult = await inviteService.get(inviteUrl);
281
+ if (!inviteResult.ok) {
282
+ spinner.fail(`Failed to fetch invite: ${inviteResult.error.message}`);
283
+ if (inviteResult.error.hint) {
284
+ console.error(`Hint: ${inviteResult.error.hint}`);
285
+ }
286
+ process.exitCode = 1;
287
+ return;
288
+ }
289
+ invite = inviteResult.value;
290
+ spinner.succeed(`Connected to ${invite.from.name}`);
291
+ }
292
+ catch (error) {
293
+ spinner.fail('Failed to fetch invite details');
294
+ console.error(error instanceof Error ? error.message : String(error));
295
+ process.exitCode = 1;
296
+ return;
297
+ }
298
+ }
299
+ // Step 2: Prompt for project details (if not in --yes mode)
300
+ let projectName = options.name;
301
+ let runtime = options.runtime;
302
+ if (!options.yes) {
303
+ // Prompt for project name
304
+ if (!projectName) {
305
+ const suggestedName = invite ? `connect-${invite.from.name}` : 'my-xbind-app';
306
+ projectName = await prompt('Project name', suggestedName);
307
+ }
308
+ // Prompt for runtime
309
+ if (!runtime) {
310
+ runtime = (await select('\nSelect runtime:', Object.entries(TEMPLATES).map(([value, meta]) => ({
311
+ value,
312
+ label: meta.name,
313
+ description: meta.description,
314
+ })), 0));
315
+ }
316
+ }
317
+ else {
318
+ // In --yes mode, use defaults
319
+ projectName = projectName || 'my-xbind-app';
320
+ runtime = runtime || 'node-typescript';
321
+ }
322
+ // Validate project name (applies to both modes)
323
+ if (!isValidProjectName(projectName)) {
324
+ console.error('❌ Invalid project name. Use only letters, numbers, hyphens, and underscores.');
325
+ process.exitCode = 1;
326
+ return;
327
+ }
328
+ // Check if directory exists (applies to both modes)
329
+ const projectPath = path.resolve(process.cwd(), projectName);
330
+ if (fs.existsSync(projectPath)) {
331
+ console.error(`❌ Directory "${projectName}" already exists.`);
332
+ process.exitCode = 1;
333
+ return;
334
+ }
335
+ if (!runtime || !TEMPLATES[runtime]) {
336
+ console.error('❌ Invalid runtime selection.');
337
+ process.exitCode = 1;
338
+ return;
339
+ }
340
+ const template = TEMPLATES[runtime];
341
+ console.log(`\n📦 Creating ${template.name} project: ${projectName}`);
342
+ // Step 3: Create project directory
343
+ const createSpinner = new Spinner('Creating project structure...');
344
+ createSpinner.start();
345
+ try {
346
+ await fs.promises.mkdir(projectPath, { recursive: true });
347
+ await copyTemplate(runtime, projectPath, invite);
348
+ createSpinner.succeed('Project structure created');
349
+ }
350
+ catch (error) {
351
+ createSpinner.fail('Failed to create project structure');
352
+ console.error(error instanceof Error ? error.message : String(error));
353
+ process.exitCode = 1;
354
+ return;
355
+ }
356
+ // Step 4: Install dependencies
357
+ if (template.packageManager !== 'none' && process.env.NODE_ENV !== 'test') {
358
+ const installSpinner = new Spinner('Installing dependencies...');
359
+ installSpinner.start();
360
+ try {
361
+ await installDependencies(projectPath, template.packageManager);
362
+ installSpinner.succeed('Dependencies installed');
363
+ }
364
+ catch (error) {
365
+ installSpinner.fail('Failed to install dependencies');
366
+ console.error('You can install them manually later.');
367
+ // Don't exit, continue
368
+ }
369
+ }
370
+ // Step 5: Write .env file with invite code (if provided)
371
+ if (invite || inviteUrl) {
372
+ const finalInviteUrl = invite?.url || inviteUrl || '';
373
+ if (process.env.DEBUG_CLI) {
374
+ console.log('[DEBUG] Writing .env file:', { invite: !!invite, inviteUrl, finalInviteUrl, projectPath });
375
+ }
376
+ if (finalInviteUrl) {
377
+ await fs.promises.writeFile(path.join(projectPath, '.env'), `XBIND_INVITE_CODE=${finalInviteUrl}\n`, 'utf-8');
378
+ }
379
+ }
380
+ else if (process.env.DEBUG_CLI) {
381
+ console.log('[DEBUG] Not writing .env file:', { invite: !!invite, inviteUrl });
382
+ }
383
+ // Step 6: Print success message
384
+ console.log('\n✅ Ready!\n');
385
+ console.log('Next steps:');
386
+ console.log(` cd ${projectName}`);
387
+ console.log(` ${template.startCommand}`);
388
+ if (invite) {
389
+ console.log(`\n🔗 Connected to: ${invite.from.name}`);
390
+ console.log(` DID: ${invite.from.did}`);
391
+ }
392
+ else {
393
+ console.log('\n💡 To connect to a service, get an invite code and update .env file.');
394
+ }
395
+ }
396
+ /**
397
+ * CLI entry point.
398
+ *
399
+ * @param argv - Command-line arguments
400
+ */
401
+ export async function main(argv = process.argv.slice(2)) {
402
+ const { values } = parseArgs({
403
+ args: argv,
404
+ options: {
405
+ invite: { type: 'string', short: 'i' },
406
+ name: { type: 'string', short: 'n' },
407
+ runtime: { type: 'string', short: 'r' },
408
+ yes: { type: 'boolean', short: 'y' },
409
+ 'registry-url': { type: 'string' },
410
+ help: { type: 'boolean', short: 'h' },
411
+ },
412
+ });
413
+ if (values.help) {
414
+ console.log(`
415
+ XBind CLI - One-Line M2M Setup
416
+
417
+ Usage:
418
+ npx xbind-init [options]
419
+
420
+ Options:
421
+ -i, --invite <url> Invite URL or code
422
+ -n, --name <name> Project name
423
+ -r, --runtime <runtime> Runtime template (node-typescript, node-javascript, deno, cloudflare-worker, vercel-function)
424
+ -y, --yes Skip interactive prompts (use defaults)
425
+ --registry-url <url> Custom registry URL (default: https://xbind.to)
426
+ -h, --help Show this help message
427
+
428
+ Examples:
429
+ npx xbind-init --invite https://xbind.to/invite/XBD-abc123
430
+ npx xbind-init --invite XBD-abc123 --name my-app --runtime node-typescript
431
+ npx xbind-init --yes
432
+ `.trim());
433
+ return;
434
+ }
435
+ await initCommand({
436
+ invite: values.invite,
437
+ name: values.name,
438
+ runtime: values.runtime,
439
+ yes: values.yes,
440
+ 'registry-url': values['registry-url'],
441
+ });
442
+ }
443
+ // Auto-run if executed directly
444
+ const isDirectRun = process.argv[1]?.endsWith('init.ts') || process.argv[1]?.endsWith('init.js');
445
+ if (isDirectRun) {
446
+ main().catch((error) => {
447
+ console.error('Fatal:', error instanceof Error ? error.message : String(error));
448
+ process.exitCode = 1;
449
+ });
450
+ }
@@ -0,0 +1,143 @@
1
+ /**
2
+ * @module connect
3
+ * High-level connect() API for zero-config XBind connections.
4
+ *
5
+ * Enables: `const service = await connect('payments-service')`
6
+ *
7
+ * Combines:
8
+ * - Service discovery
9
+ * - Trust verification
10
+ * - Agent creation
11
+ * - Connection establishment
12
+ *
13
+ * @example
14
+ * ```ts
15
+ * import { connect } from '@private.me/xbind';
16
+ *
17
+ * // Connect by name (uses registry):
18
+ * const payments = await connect('payments-service');
19
+ *
20
+ * // Connect by domain (uses .well-known):
21
+ * const payments = await connect('payments.example.com');
22
+ *
23
+ * // Connect by URL (direct):
24
+ * const payments = await connect('https://api.payments.com/xbind');
25
+ *
26
+ * // Use the connection:
27
+ * await payments.send({
28
+ * to: payments.did,
29
+ * payload: { action: 'createCharge', amount: 100 },
30
+ * scope: 'payments',
31
+ * });
32
+ * ```
33
+ */
34
+ import { Result } from '@private.me/shared';
35
+ import { Agent } from './agent.js';
36
+ import { ServiceDiscovery } from './discovery.js';
37
+ import type { ServiceInfo } from './discovery.js';
38
+ import type { XailTransportAdapter } from './transport.js';
39
+ /**
40
+ * Connect error codes.
41
+ */
42
+ export declare enum ConnectErrorCode {
43
+ DISCOVERY_FAILED = "CONNECT_DISCOVERY_FAILED",
44
+ AGENT_CREATION_FAILED = "CONNECT_AGENT_CREATION_FAILED",
45
+ TRUST_VERIFICATION_FAILED = "CONNECT_TRUST_VERIFICATION_FAILED",
46
+ INVALID_SERVICE_INFO = "CONNECT_INVALID_SERVICE_INFO"
47
+ }
48
+ /**
49
+ * Connect error.
50
+ */
51
+ export interface ConnectError {
52
+ code: ConnectErrorCode;
53
+ message: string;
54
+ hint?: string;
55
+ cause?: unknown;
56
+ }
57
+ /**
58
+ * Connect options.
59
+ */
60
+ export interface ConnectOptions {
61
+ /** Custom discovery service (optional) */
62
+ discovery?: ServiceDiscovery;
63
+ /**
64
+ * Custom trust registry (optional).
65
+ *
66
+ * SAFETY: Uses `any` to support extended registry implementations with add() method.
67
+ * The base TrustRegistry interface uses register(), but some implementations
68
+ * may provide an add() convenience method.
69
+ */
70
+ registry?: any;
71
+ /** Custom transport adapter (optional) */
72
+ transport?: XailTransportAdapter;
73
+ /** Agent name (default: auto-generated) */
74
+ name?: string;
75
+ /** Enable split-channel by default (default: false) */
76
+ splitChannel?: boolean;
77
+ /** Enable Xchange mode (default: false) */
78
+ xchange?: boolean;
79
+ /** Enable post-quantum signatures (default: false) */
80
+ postQuantumSig?: boolean;
81
+ /** Agent endpoint URL where it can be reached (optional, for server agents) */
82
+ endpoint?: string;
83
+ }
84
+ /**
85
+ * Connection result.
86
+ */
87
+ export interface Connection {
88
+ /** Agent instance */
89
+ agent: Agent;
90
+ /** Service info */
91
+ service: ServiceInfo;
92
+ /** DID of the service */
93
+ did: string;
94
+ /** Endpoint URL */
95
+ endpoint: string;
96
+ }
97
+ /**
98
+ * Connect to a service with zero configuration.
99
+ *
100
+ * @param nameOrUrl - Service name, domain, or URL
101
+ * @param options - Connection options
102
+ * @returns Connection or error
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * // Minimal:
107
+ * const connection = await connect('payments-service');
108
+ * if (connection.ok) {
109
+ * await connection.value.agent.send({
110
+ * to: connection.value.did,
111
+ * payload: { action: 'test' },
112
+ * scope: 'test',
113
+ * });
114
+ * }
115
+ *
116
+ * // With options:
117
+ * const connection = await connect('payments-service', {
118
+ * name: 'billing-service',
119
+ * xchange: true, // Use faster Xchange mode
120
+ * splitChannel: true, // Use split-channel by default
121
+ * });
122
+ * ```
123
+ */
124
+ export declare function connect(nameOrUrl: string, options?: ConnectOptions): Promise<Result<Connection, ConnectError>>;
125
+ /**
126
+ * Accept an invite and establish connection.
127
+ *
128
+ * @param inviteUrl - Invite URL
129
+ * @param options - Connection options
130
+ * @returns Connection or error
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const connection = await acceptInvite('https://xbind.to/invite/abc123', {
135
+ * name: 'my-service',
136
+ * });
137
+ *
138
+ * if (connection.ok) {
139
+ * console.log('Connected to:', connection.value.service.name);
140
+ * }
141
+ * ```
142
+ */
143
+ export declare function acceptInvite(inviteUrl: string, options?: ConnectOptions): Promise<Result<Connection, ConnectError>>;