devframe 0.6.0-beta.2 → 0.6.0-beta.3

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 (58) hide show
  1. package/dist/adapters/build.d.mts +1 -1
  2. package/dist/adapters/build.mjs +2 -2
  3. package/dist/adapters/cli.d.mts +1 -1
  4. package/dist/adapters/cli.mjs +1 -1
  5. package/dist/adapters/dev.d.mts +2 -2
  6. package/dist/adapters/dev.mjs +1 -1
  7. package/dist/adapters/embedded.d.mts +1 -1
  8. package/dist/adapters/mcp.d.mts +1 -1
  9. package/dist/adapters/mcp.mjs +3 -3
  10. package/dist/client/index.d.mts +2 -2
  11. package/dist/client/index.mjs +56 -22
  12. package/dist/constants.d.mts +24 -1
  13. package/dist/constants.mjs +26 -1
  14. package/dist/{context-BEVByy_Z.d.mts → context-B_I9fMBc.d.mts} +1 -1
  15. package/dist/{context-3x_bgBgZ.mjs → context-CKJheEtf.mjs} +2 -2
  16. package/dist/{dev-BaX8fLH2.mjs → dev-DSxKFn-v.mjs} +4 -4
  17. package/dist/{devframe-D-gr9sBx.d.mts → devframe-DgvncQy2.d.mts} +19 -3
  18. package/dist/{dump-DoXkj4ku.mjs → dump-D09PZb7T.mjs} +14 -4
  19. package/dist/{hash-DFc5WwhO.mjs → hash-PAfnyu2k.mjs} +1 -1
  20. package/dist/helpers/vite.d.mts +1 -1
  21. package/dist/helpers/vite.mjs +2 -2
  22. package/dist/{host-h3-C5SZlk03.mjs → host-h3-Dsf-Tgwx.mjs} +37 -11
  23. package/dist/index-B4lNDq5v.d.mts +107 -0
  24. package/dist/{index-CxaPKDXX.d.mts → index-Dy6GFDRf.d.mts} +1 -0
  25. package/dist/index.d.mts +2 -2
  26. package/dist/{launch-editor-DPfltGA4.mjs → launch-editor-DIzaS5EG.mjs} +58 -43
  27. package/dist/node/auth.d.mts +2 -64
  28. package/dist/node/auth.mjs +2 -100
  29. package/dist/node/hub-internals.d.mts +2 -2
  30. package/dist/node/hub-internals.mjs +1 -1
  31. package/dist/node/index.d.mts +3 -3
  32. package/dist/node/index.mjs +3 -3
  33. package/dist/recipes/interactive-auth.d.mts +53 -0
  34. package/dist/recipes/interactive-auth.mjs +132 -0
  35. package/dist/recipes/open-helpers.mjs +2 -2
  36. package/dist/{revoke-ENfxWkFK.mjs → revoke-BtQDKTp7.mjs} +1 -1
  37. package/dist/rpc/dump.mjs +1 -1
  38. package/dist/rpc/index.d.mts +2 -2
  39. package/dist/rpc/index.mjs +4 -1
  40. package/dist/rpc/transports/ws-client.mjs +21 -8
  41. package/dist/{server-Dl6TU5LE.mjs → server-BdN41ckC.mjs} +36 -4
  42. package/dist/{server-rX781kz-.d.mts → server-DXPq8q0K.d.mts} +40 -9
  43. package/dist/{shared-state-D4PPieA0.mjs → shared-state-QtMas0NG.mjs} +34 -4
  44. package/dist/state-WX3HT5a3.mjs +101 -0
  45. package/dist/{storage-l2ezeend.mjs → storage-yrUZaiib.mjs} +55 -6
  46. package/dist/types/index.d.mts +2 -2
  47. package/dist/utils/crypto-token.mjs +1 -1
  48. package/dist/utils/events.d.mts +1 -1
  49. package/dist/utils/hash.mjs +1 -1
  50. package/dist/utils/launch-editor.mjs +1 -1
  51. package/dist/utils/open.mjs +1 -1
  52. package/dist/utils/serve-static.mjs +2 -1
  53. package/dist/utils/shared-state.d.mts +1 -1
  54. package/dist/utils/shared-state.mjs +1 -1
  55. package/dist/utils/streaming-channel.d.mts +1 -1
  56. package/dist/utils/when.d.mts +1 -1
  57. package/package.json +10 -9
  58. package/dist/{open-Dusa2Zzd.mjs → open-Deb5xmIT.mjs} +1 -1
@@ -0,0 +1,132 @@
1
+ import { n as colors } from "../diagnostics-reporter-CsIG85Q5.mjs";
2
+ import { isAnonymousRpcMethod } from "../constants.mjs";
3
+ import { n as defineRpcFunction } from "../define-BLWPsH6y.mjs";
4
+ import { t as getInternalContext } from "../context-CKJheEtf.mjs";
5
+ import { a as verifyAuthToken, n as exchangeTempAuthCode, r as getTempAuthCode, t as buildOtpAuthUrl } from "../state-WX3HT5a3.mjs";
6
+ import * as v from "valibot";
7
+ //#region src/recipes/interactive-auth.ts
8
+ function defaultBanner(info) {
9
+ console.log(`\n ${colors.dim("devframe auth code")} ${colors.bold(info.code)}\n ${colors.dim("or open")} ${colors.cyan(info.url)}\n`);
10
+ }
11
+ /**
12
+ * Package the interactive OTP auth protocol devframe's primitives
13
+ * (`exchangeTempAuthCode`, `verifyAuthToken`, `revokeAuthToken`,
14
+ * `getTempAuthCode`, `buildOtpAuthUrl`) implement into a ready-made
15
+ * {@link DevframeAuthHandler} — the handshake RPC functions, the resolver
16
+ * gate, the connect-time trust hook, and the startup banner.
17
+ *
18
+ * The auth storage stays internal to this handler — callers never reach into
19
+ * `devframe/node/hub-internals` themselves.
20
+ *
21
+ * ```ts
22
+ * import { createInteractiveAuth } from 'devframe/recipes/interactive-auth'
23
+ *
24
+ * const auth = createInteractiveAuth(ctx)
25
+ * auth.rpcFunctions.forEach(fn => ctx.rpc.register(fn))
26
+ * auth.printBanner()
27
+ *
28
+ * // wire `auth.authorize` / `auth.onConnect` into your transport, or pass
29
+ * // the whole handler to `startHttpAndWs({ auth, ... })`.
30
+ * ```
31
+ */
32
+ function createInteractiveAuth(context, options = {}) {
33
+ const internal = getInternalContext(context);
34
+ const storage = internal.storage.auth;
35
+ const staticTokens = new Set(options.clientAuthTokens ?? []);
36
+ function isStaticToken(token) {
37
+ return !!token && staticTokens.has(token);
38
+ }
39
+ function resolveServerUrl() {
40
+ return options.serverUrl?.() ?? context.host.resolveOrigin();
41
+ }
42
+ let bannerPrintedForCode;
43
+ function printBanner() {
44
+ const code = getTempAuthCode();
45
+ if (code === bannerPrintedForCode) return;
46
+ bannerPrintedForCode = code;
47
+ const url = buildOtpAuthUrl(resolveServerUrl(), code);
48
+ (options.banner ?? defaultBanner)({
49
+ code,
50
+ url
51
+ });
52
+ }
53
+ const anonymousAuth = defineRpcFunction({
54
+ name: "anonymous:devframe:auth",
55
+ type: "action",
56
+ jsonSerializable: true,
57
+ args: [v.object({
58
+ authToken: v.string(),
59
+ ua: v.string(),
60
+ origin: v.string()
61
+ })],
62
+ returns: v.object({ isTrusted: v.boolean() }),
63
+ handler(params) {
64
+ const session = context.rpc.getCurrentRpcSession();
65
+ if (!session) return { isTrusted: false };
66
+ if (isStaticToken(params.authToken)) {
67
+ session.meta.clientAuthToken = params.authToken;
68
+ session.meta.isTrusted = true;
69
+ return { isTrusted: true };
70
+ }
71
+ return { isTrusted: verifyAuthToken(params.authToken, session, storage) };
72
+ }
73
+ });
74
+ const anonymousAuthExchange = defineRpcFunction({
75
+ name: "anonymous:devframe:auth:exchange",
76
+ type: "action",
77
+ jsonSerializable: true,
78
+ args: [v.object({
79
+ code: v.string(),
80
+ ua: v.string(),
81
+ origin: v.string()
82
+ })],
83
+ returns: v.object({ authToken: v.nullable(v.string()) }),
84
+ handler(params) {
85
+ const session = context.rpc.getCurrentRpcSession();
86
+ if (!session) return { authToken: null };
87
+ const authToken = exchangeTempAuthCode(params.code, session, params, storage);
88
+ printBanner();
89
+ return { authToken };
90
+ }
91
+ });
92
+ const revoke = defineRpcFunction({
93
+ name: "devframe:auth:revoke",
94
+ type: "action",
95
+ jsonSerializable: true,
96
+ args: [],
97
+ returns: v.void(),
98
+ async handler() {
99
+ const token = context.rpc.getCurrentRpcSession()?.meta.clientAuthToken;
100
+ if (token) await internal.revokeAuthToken(token);
101
+ }
102
+ });
103
+ function authorize(methodName, session) {
104
+ if (isAnonymousRpcMethod(methodName)) return true;
105
+ return !!session.meta.isTrusted;
106
+ }
107
+ function onConnect(peer, session) {
108
+ let token;
109
+ try {
110
+ token = new URL(peer.request?.url ?? "", "http://localhost").searchParams.get("devframe_auth_token") ?? void 0;
111
+ } catch {}
112
+ if (!token) return;
113
+ if (isStaticToken(token)) {
114
+ session.meta.clientAuthToken = token;
115
+ session.meta.isTrusted = true;
116
+ return;
117
+ }
118
+ verifyAuthToken(token, session, storage);
119
+ }
120
+ return {
121
+ rpcFunctions: [
122
+ anonymousAuth,
123
+ anonymousAuthExchange,
124
+ revoke
125
+ ],
126
+ authorize,
127
+ onConnect,
128
+ printBanner
129
+ };
130
+ }
131
+ //#endregion
132
+ export { createInteractiveAuth };
@@ -1,6 +1,6 @@
1
- import { t as launchEditor } from "../launch-editor-DPfltGA4.mjs";
1
+ import { t as launchEditor } from "../launch-editor-DIzaS5EG.mjs";
2
2
  import { n as defineRpcFunction } from "../define-BLWPsH6y.mjs";
3
- import { t as open } from "../open-Dusa2Zzd.mjs";
3
+ import { t as open } from "../open-Deb5xmIT.mjs";
4
4
  import * as v from "valibot";
5
5
  //#region src/recipes/open-helpers.ts
6
6
  /**
@@ -19,7 +19,7 @@ function randomToken(byteLength = 16) {
19
19
  */
20
20
  function randomDigits(length) {
21
21
  const limit = 250;
22
- const buf = new Uint8Array(1);
22
+ const buf = /* @__PURE__ */ new Uint8Array(1);
23
23
  let out = "";
24
24
  while (out.length < length) {
25
25
  globalThis.crypto.getRandomValues(buf);
package/dist/rpc/dump.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { a as dumpFunctions, c as serializeDumpError, i as createClientFromDump, o as getDefinitionsWithDumps, s as reviveDumpError, t as collectStaticRpcDump } from "../dump-DoXkj4ku.mjs";
1
+ import { a as dumpFunctions, c as serializeDumpError, i as createClientFromDump, o as getDefinitionsWithDumps, s as reviveDumpError, t as collectStaticRpcDump } from "../dump-D09PZb7T.mjs";
2
2
  export { collectStaticRpcDump, createClientFromDump, dumpFunctions, getDefinitionsWithDumps, reviveDumpError, serializeDumpError };
@@ -1,3 +1,3 @@
1
1
  import { C as RpcFunctionType, E as Thenable, S as RpcFunctionSetupResult, T as RpcReturnSchema, _ as RpcFunctionDefinition, a as RpcDefinitionsFilter, b as RpcFunctionDefinitionBase, c as RpcDump, d as RpcDumpDefinition, f as RpcDumpGetter, g as RpcFunctionAgentOptions, h as RpcDumpStore, i as RpcArgsSchema, l as RpcDumpClientOptions, m as RpcDumpRecordError, n as BirpcReturn, o as RpcDefinitionsToFunctions, p as RpcDumpRecord, r as EntriesToObject, s as RpcDefinitionsToFunctionsWithNamespace, t as BirpcFn, u as RpcDumpCollectionOptions, v as RpcFunctionDefinitionAny, w as RpcFunctionsCollector, x as RpcFunctionDefinitionToFunction, y as RpcFunctionDefinitionAnyWithContext } from "../types-DZEx4ffs.mjs";
2
- import { C as RpcCacheManager, S as RpcFunctionsCollectorBase, _ as strictJsonStringify, a as StaticRpcDumpManifestStaticEntry, b as createDefineWrapperWithContext, c as collectStaticRpcDump, d as getDefinitionsWithDumps, f as reviveDumpError, g as STRUCTURED_CLONE_PREFIX, h as validateDefinitions, i as StaticRpcDumpManifestQueryEntry, l as createClientFromDump, m as validateDefinition, n as StaticRpcDumpFile, o as StaticRpcDumpManifestValue, p as serializeDumpError, r as StaticRpcDumpManifest, s as StaticRpcDumpSerialization, t as StaticRpcDumpCollection, u as dumpFunctions, v as getRpcHandler, w as RpcCacheOptions, x as defineRpcFunction, y as getRpcResolvedSetupResult } from "../index-CxaPKDXX.mjs";
3
- export { BirpcFn, BirpcReturn, EntriesToObject, RpcArgsSchema, RpcCacheManager, RpcCacheOptions, RpcDefinitionsFilter, RpcDefinitionsToFunctions, RpcDefinitionsToFunctionsWithNamespace, RpcDump, RpcDumpClientOptions, RpcDumpCollectionOptions, RpcDumpDefinition, RpcDumpGetter, RpcDumpRecord, RpcDumpRecordError, RpcDumpStore, RpcFunctionAgentOptions, RpcFunctionDefinition, RpcFunctionDefinitionAny, RpcFunctionDefinitionAnyWithContext, RpcFunctionDefinitionBase, RpcFunctionDefinitionToFunction, RpcFunctionSetupResult, RpcFunctionType, RpcFunctionsCollector, RpcFunctionsCollectorBase, RpcReturnSchema, STRUCTURED_CLONE_PREFIX, StaticRpcDumpCollection, StaticRpcDumpFile, StaticRpcDumpManifest, StaticRpcDumpManifestQueryEntry, StaticRpcDumpManifestStaticEntry, StaticRpcDumpManifestValue, StaticRpcDumpSerialization, Thenable, collectStaticRpcDump, createClientFromDump, createDefineWrapperWithContext, defineRpcFunction, dumpFunctions, getDefinitionsWithDumps, getRpcHandler, getRpcResolvedSetupResult, reviveDumpError, serializeDumpError, strictJsonStringify, validateDefinition, validateDefinitions };
2
+ import { C as RpcCacheManager, S as RpcFunctionsCollectorBase, _ as strictJsonStringify, a as StaticRpcDumpManifestStaticEntry, b as createDefineWrapperWithContext, c as collectStaticRpcDump, d as getDefinitionsWithDumps, f as reviveDumpError, g as STRUCTURED_CLONE_PREFIX, h as validateDefinitions, i as StaticRpcDumpManifestQueryEntry, l as createClientFromDump, m as validateDefinition, n as StaticRpcDumpFile, o as StaticRpcDumpManifestValue, p as serializeDumpError, r as StaticRpcDumpManifest, s as StaticRpcDumpSerialization, t as StaticRpcDumpCollection, u as dumpFunctions, v as getRpcHandler, w as RpcCacheOptions, x as defineRpcFunction, y as getRpcResolvedSetupResult } from "../index-Dy6GFDRf.mjs";
3
+ export { type BirpcFn, type BirpcReturn, EntriesToObject, RpcArgsSchema, RpcCacheManager, RpcCacheOptions, RpcDefinitionsFilter, RpcDefinitionsToFunctions, RpcDefinitionsToFunctionsWithNamespace, RpcDump, RpcDumpClientOptions, RpcDumpCollectionOptions, RpcDumpDefinition, RpcDumpGetter, RpcDumpRecord, RpcDumpRecordError, RpcDumpStore, RpcFunctionAgentOptions, RpcFunctionDefinition, RpcFunctionDefinitionAny, RpcFunctionDefinitionAnyWithContext, RpcFunctionDefinitionBase, RpcFunctionDefinitionToFunction, RpcFunctionSetupResult, RpcFunctionType, RpcFunctionsCollector, RpcFunctionsCollectorBase, RpcReturnSchema, STRUCTURED_CLONE_PREFIX, StaticRpcDumpCollection, StaticRpcDumpFile, StaticRpcDumpManifest, StaticRpcDumpManifestQueryEntry, StaticRpcDumpManifestStaticEntry, StaticRpcDumpManifestValue, StaticRpcDumpSerialization, Thenable, collectStaticRpcDump, createClientFromDump, createDefineWrapperWithContext, defineRpcFunction, dumpFunctions, getDefinitionsWithDumps, getRpcHandler, getRpcResolvedSetupResult, reviveDumpError, serializeDumpError, strictJsonStringify, validateDefinition, validateDefinitions };
@@ -1,4 +1,4 @@
1
- import { a as dumpFunctions$1, c as serializeDumpError$1, d as hash, i as createClientFromDump$1, l as validateDefinition, n as getRpcHandler, o as getDefinitionsWithDumps$1, r as getRpcResolvedSetupResult, s as reviveDumpError$1, t as collectStaticRpcDump$1, u as validateDefinitions } from "../dump-DoXkj4ku.mjs";
1
+ import { a as dumpFunctions$1, c as serializeDumpError$1, d as hash, i as createClientFromDump$1, l as validateDefinition, n as getRpcHandler, o as getDefinitionsWithDumps$1, r as getRpcResolvedSetupResult, s as reviveDumpError$1, t as collectStaticRpcDump$1, u as validateDefinitions } from "../dump-D09PZb7T.mjs";
2
2
  import { t as diagnostics } from "../diagnostics-BXwBQmoN.mjs";
3
3
  import { n as defineRpcFunction, t as createDefineWrapperWithContext } from "../define-BLWPsH6y.mjs";
4
4
  import { n as strictJsonStringify, t as STRUCTURED_CLONE_PREFIX } from "../serialization-DpLXCy13.mjs";
@@ -24,6 +24,9 @@ var RpcCacheManager = class {
24
24
  const methodCache = this.cacheMap.get(m);
25
25
  if (methodCache) return methodCache.get(this.keySerializer(a));
26
26
  }
27
+ has(m, a) {
28
+ return this.cacheMap.get(m)?.has(this.keySerializer(a)) ?? false;
29
+ }
27
30
  apply(req, res) {
28
31
  const methodCache = this.cacheMap.get(req.m) || /* @__PURE__ */ new Map();
29
32
  methodCache.set(this.keySerializer(req.a), res);
@@ -1,3 +1,4 @@
1
+ import { DEVFRAME_AUTH_TOKEN_QUERY_PARAM } from "../../constants.mjs";
1
2
  import { n as strictJsonStringify } from "../../serialization-DpLXCy13.mjs";
2
3
  import { n as structuredCloneStringify, t as structuredCloneParse } from "../../structured-clone-XpHLZ8nr.mjs";
3
4
  //#region src/rpc/transports/ws-client.ts
@@ -9,14 +10,15 @@ const EMPTY_DEFS = /* @__PURE__ */ new Map();
9
10
  */
10
11
  function createWsRpcChannel(options) {
11
12
  let url = options.url;
12
- if (options.authToken) url = `${url}?devframe_auth_token=${encodeURIComponent(options.authToken)}`;
13
+ if (options.authToken) url = `${url}?${DEVFRAME_AUTH_TOKEN_QUERY_PARAM}=${encodeURIComponent(options.authToken)}`;
13
14
  const ws = new WebSocket(url);
14
15
  const { onConnected = NOOP, onError = NOOP, onDisconnected = NOOP, definitions = EMPTY_DEFS } = options;
15
16
  ws.addEventListener("open", (e) => {
16
17
  onConnected(e);
17
18
  });
18
19
  ws.addEventListener("error", (e) => {
19
- onError(e instanceof Error ? e : new Error(e.type));
20
+ const _e = e instanceof Error ? e : new Error(e.type);
21
+ onError(_e);
20
22
  });
21
23
  ws.addEventListener("close", (e) => {
22
24
  onDisconnected(e);
@@ -29,14 +31,25 @@ function createWsRpcChannel(options) {
29
31
  });
30
32
  },
31
33
  post: (data) => {
32
- if (ws.readyState === WebSocket.OPEN) ws.send(data);
33
- else {
34
- function handler() {
35
- ws.send(data);
36
- ws.removeEventListener("open", handler);
34
+ if (ws.readyState === WebSocket.OPEN) {
35
+ ws.send(data);
36
+ return;
37
+ }
38
+ if (ws.readyState === WebSocket.CONNECTING) {
39
+ const onOpen = () => {
40
+ cleanup();
41
+ if (ws.readyState === WebSocket.OPEN) ws.send(data);
42
+ };
43
+ const onClose = () => cleanup();
44
+ function cleanup() {
45
+ ws.removeEventListener("open", onOpen);
46
+ ws.removeEventListener("close", onClose);
37
47
  }
38
- ws.addEventListener("open", handler);
48
+ ws.addEventListener("open", onOpen);
49
+ ws.addEventListener("close", onClose);
50
+ return;
39
51
  }
52
+ onError(/* @__PURE__ */ new Error("Devframe WebSocket is not open; message dropped"));
40
53
  },
41
54
  serialize: (msg) => {
42
55
  let method;
@@ -1,6 +1,7 @@
1
1
  import { createRpcServer } from "./rpc/server.mjs";
2
2
  import { attachWsRpcTransport } from "./rpc/transports/ws-server.mjs";
3
- import { t as getInternalContext } from "./context-3x_bgBgZ.mjs";
3
+ import { a as diagnostics } from "./storage-yrUZaiib.mjs";
4
+ import { t as getInternalContext } from "./context-CKJheEtf.mjs";
4
5
  import { createServer } from "node:http";
5
6
  import { AsyncLocalStorage } from "node:async_hooks";
6
7
  import { H3, toNodeHandler } from "h3";
@@ -18,13 +19,23 @@ async function startHttpAndWs(options) {
18
19
  const httpServer = options.server ?? createServer(toNodeHandler(app));
19
20
  const rpcHost = context.rpc;
20
21
  const asyncStorage = new AsyncLocalStorage();
22
+ const authHandler = typeof options.auth === "object" ? options.auth : void 0;
23
+ const effectiveAuthorize = options.authorize ?? authHandler?.authorize;
24
+ if (authHandler) {
25
+ for (const fn of authHandler.rpcFunctions) if (!rpcHost.definitions.has(fn.name)) rpcHost.register(fn);
26
+ }
21
27
  const rpcGroup = createRpcServer(rpcHost.functions, { rpcOptions: { resolver(name, fn) {
22
28
  const rpc = this;
23
29
  if (!fn) return void 0;
24
30
  return async function(...args) {
31
+ const meta = rpc.$meta;
32
+ if (effectiveAuthorize && !effectiveAuthorize(name, {
33
+ meta,
34
+ rpc
35
+ })) throw diagnostics.DF0036({ name });
25
36
  return await asyncStorage.run({
26
37
  rpc,
27
- meta: rpc.$meta
38
+ meta
28
39
  }, async () => {
29
40
  return (await fn).apply(this, args);
30
41
  });
@@ -39,6 +50,14 @@ async function startHttpAndWs(options) {
39
50
  path: options.path,
40
51
  destroyUnmatched: ownsHttpServer,
41
52
  allowedOrigins: options.allowedOrigins,
53
+ onConnected: authHandler || options.onPeerConnect ? (peer, meta) => {
54
+ const session = {
55
+ meta,
56
+ rpc: rpcGroup.clients.find((client) => client.$meta === meta)
57
+ };
58
+ authHandler?.onConnect(peer, session);
59
+ options.onPeerConnect?.(peer, session);
60
+ } : void 0,
42
61
  onDisconnected: (_peer, meta) => {
43
62
  rpcHost._emitSessionDisconnected(meta);
44
63
  }
@@ -46,8 +65,8 @@ async function startHttpAndWs(options) {
46
65
  rpcHost._rpcGroup = rpcGroup;
47
66
  rpcHost._asyncStorage = asyncStorage;
48
67
  rpcHost._authDisabled = options.auth === false;
49
- if (options.auth === false && !rpcHost.definitions.has("devframe:anonymous:auth")) rpcHost.register({
50
- name: "devframe:anonymous:auth",
68
+ if (options.auth === false && !rpcHost.definitions.has("anonymous:devframe:auth")) rpcHost.register({
69
+ name: "anonymous:devframe:auth",
51
70
  type: "action",
52
71
  handler: () => {
53
72
  const session = rpcHost.getCurrentRpcSession();
@@ -69,12 +88,25 @@ async function startHttpAndWs(options) {
69
88
  port: resolvedPort,
70
89
  app
71
90
  });
91
+ function connectionMeta() {
92
+ const jsonSerializableMethods = [];
93
+ for (const def of rpcHost.definitions.values()) if (def.jsonSerializable === true) jsonSerializableMethods.push(def.name);
94
+ return {
95
+ backend: "websocket",
96
+ websocket: separateWsPort != null ? {
97
+ port: separateWsPort,
98
+ path: options.path
99
+ } : { path: options.path },
100
+ jsonSerializableMethods
101
+ };
102
+ }
72
103
  return {
73
104
  origin,
74
105
  port: resolvedPort,
75
106
  app,
76
107
  ws,
77
108
  rpcGroup,
109
+ connectionMeta,
78
110
  async close() {
79
111
  await closeWs();
80
112
  if (ownsHttpServer) await new Promise((r) => httpServer.close(() => r()));
@@ -1,5 +1,7 @@
1
- import { it as DevframeRpcServerFunctions, m as DevframeNodeContext, rt as DevframeRpcClientFunctions } from "./devframe-D-gr9sBx.mjs";
1
+ import { d as ConnectionMeta, h as DevframeNodeRpcSession, it as DevframeRpcServerFunctions, m as DevframeNodeContext, rt as DevframeRpcClientFunctions } from "./devframe-DgvncQy2.mjs";
2
+ import { c as DevframeAuthHandler } from "./index-B4lNDq5v.mjs";
2
3
  import { BirpcGroup } from "birpc";
4
+ import { Peer } from "crossws";
3
5
  import { NodeAdapter } from "crossws/adapters/node";
4
6
  import { Server } from "node:http";
5
7
  import { H3 } from "h3";
@@ -41,16 +43,37 @@ interface StartHttpAndWsOptions {
41
43
  */
42
44
  server?: Server;
43
45
  /**
44
- * When `false`, the RPC server is started without a trust handshake.
45
- * Intended for single-user localhost tools where an auth round-trip
46
- * would only get in the way. The Vite-flavoured auth layer in
47
- * `@vitejs/devtools` already honors the equivalent
48
- * `devtools.clientAuth` setting; devframe records the intent here so
49
- * future auth plumbing can consult it without another API change.
46
+ * Authentication for the server:
50
47
  *
51
- * Default: `true`.
48
+ * - `true` (default) — no gate; every registered method is callable
49
+ * regardless of trust (today's behavior, unchanged).
50
+ * - `false` — the RPC server is started without a trust handshake.
51
+ * Intended for single-user localhost tools where an auth round-trip
52
+ * would only get in the way. A noop `anonymous:devframe:auth` handler
53
+ * is registered so the browser client's unconditional handshake call
54
+ * succeeds and auto-trusts.
55
+ * - A {@link DevframeAuthHandler} (e.g. from
56
+ * `devframe/recipes/interactive-auth`'s `createInteractiveAuth`) —
57
+ * registers its `rpcFunctions`, wires its `authorize` as the resolver
58
+ * gate, and wires its `onConnect` on every new peer. This is the
59
+ * fully-authenticated server: an untrusted caller can only reach
60
+ * `anonymous:`-prefixed methods (see `isAnonymousRpcMethod`).
52
61
  */
53
- auth?: boolean;
62
+ auth?: boolean | DevframeAuthHandler;
63
+ /**
64
+ * Lower-level escape hatch: gate individual RPC calls by method name and
65
+ * session without a full {@link DevframeAuthHandler}. Ignored when `auth`
66
+ * is a handler object (its own `authorize` is used); combine with `auth:
67
+ * true` to layer a custom policy on top of an otherwise ungated server.
68
+ */
69
+ authorize?: (methodName: string, session: DevframeNodeRpcSession) => boolean;
70
+ /**
71
+ * Called once per new WS connection, right after its session is created
72
+ * (before any RPC call is dispatched). Runs after the auth handler's own
73
+ * `onConnect` (when `auth` is a {@link DevframeAuthHandler}), so it can
74
+ * observe — but not override — the connect-time trust decision.
75
+ */
76
+ onPeerConnect?: (peer: Peer, session: DevframeNodeRpcSession) => void;
54
77
  /**
55
78
  * Extra origins to accept on the WS upgrade beyond the loopback default
56
79
  * (`localhost`/`127.0.0.1`/`::1` and any `Origin`-less request from a
@@ -78,6 +101,14 @@ interface StartedServer {
78
101
  /** The crossws node adapter driving the RPC socket (connected peers, pub/sub). */
79
102
  ws: NodeAdapter;
80
103
  rpcGroup: BirpcGroup<DevframeRpcClientFunctions, DevframeRpcServerFunctions, false>;
104
+ /**
105
+ * The {@link ConnectionMeta} descriptor for this server — the same shape
106
+ * a `__connection.json` route should serve so a devframe client's
107
+ * `resolveWsUrl` can dial back in. Reflects the `path` / `wsPort` this
108
+ * server was started with and the `jsonSerializable` methods currently
109
+ * registered on `context.rpc`.
110
+ */
111
+ connectionMeta: () => ConnectionMeta;
81
112
  close: () => Promise<void>;
82
113
  }
83
114
  /**
@@ -1,6 +1,6 @@
1
1
  import { createEventEmitter } from "./utils/events.mjs";
2
2
  import { nanoid } from "./utils/nanoid.mjs";
3
- //#region ../../node_modules/.pnpm/immer@11.1.8/node_modules/immer/dist/immer.mjs
3
+ //#region ../../node_modules/.pnpm/immer@11.1.11/node_modules/immer/dist/immer.mjs
4
4
  var NOTHING = Symbol.for("immer-nothing");
5
5
  var DRAFTABLE = Symbol.for("immer-draftable");
6
6
  var DRAFT_STATE = Symbol.for("immer-state");
@@ -350,6 +350,21 @@ function createProxyProxy(base, parent) {
350
350
  var objectTraps = {
351
351
  get(state, prop) {
352
352
  if (prop === DRAFT_STATE) return state;
353
+ if (prop === "constructor" || prop === "__proto__") {
354
+ const value2 = latest(state)[prop];
355
+ return new Proxy(value2 || {}, {
356
+ get: (target, key) => {
357
+ if (key === "__proto__" || key === "prototype") return Object.freeze(/* @__PURE__ */ Object.create(null));
358
+ return Reflect.get(target, key);
359
+ },
360
+ set: () => {
361
+ return true;
362
+ },
363
+ apply: (target, thisArg, args) => {
364
+ return Reflect.apply(target, thisArg, args);
365
+ }
366
+ });
367
+ }
353
368
  let arrayPlugin = state.scope_.arrayMethodsPlugin_;
354
369
  const isArrayWithStringProp = state.type_ === 1 && typeof prop === "string";
355
370
  if (isArrayWithStringProp) {
@@ -369,12 +384,14 @@ var objectTraps = {
369
384
  return value;
370
385
  },
371
386
  has(state, prop) {
387
+ if (prop === "constructor" || prop === "__proto__" || prop === "prototype") return false;
372
388
  return prop in latest(state);
373
389
  },
374
390
  ownKeys(state) {
375
391
  return Reflect.ownKeys(latest(state));
376
392
  },
377
393
  set(state, prop, value) {
394
+ if (prop === "constructor" || prop === "__proto__" || prop === "prototype") return true;
378
395
  const desc = getDescriptorFromProto(latest(state), prop);
379
396
  if (desc?.set) {
380
397
  desc.set.call(state.draft_, value);
@@ -392,7 +409,7 @@ var objectTraps = {
392
409
  prepareCopy(state);
393
410
  markChanged(state);
394
411
  }
395
- if (state.copy_[prop] === value && (value !== void 0 || prop in state.copy_) || Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true;
412
+ if (state.copy_[prop] === value && (value !== void 0 || has(state.copy_, prop, state.type_)) || Number.isNaN(value) && Number.isNaN(state.copy_[prop])) return true;
396
413
  state.copy_[prop] = value;
397
414
  state.assigned_.set(prop, true);
398
415
  handleCrossReference(state, prop, value);
@@ -894,6 +911,19 @@ var produceWithPatches = /* @__PURE__ */ immer.produceWithPatches.bind(immer);
894
911
  var applyPatches = /* @__PURE__ */ immer.applyPatches.bind(immer);
895
912
  //#endregion
896
913
  //#region src/utils/shared-state.ts
914
+ /**
915
+ * Upper bound on retained syncIds. Loop echoes arrive near-immediately, so a
916
+ * generous window preserves de-dup while capping memory on long-lived,
917
+ * frequently-mutated states (e.g. a 1s terminal poll).
918
+ */
919
+ const MAX_SYNC_IDS = 1e3;
920
+ function rememberSyncId(syncIds, syncId) {
921
+ syncIds.add(syncId);
922
+ if (syncIds.size > MAX_SYNC_IDS) {
923
+ const oldest = syncIds.values().next().value;
924
+ if (oldest !== void 0) syncIds.delete(oldest);
925
+ }
926
+ }
897
927
  function createSharedState(options) {
898
928
  const { enablePatches: enablePatches$1 = false } = options;
899
929
  const events = createEventEmitter();
@@ -906,12 +936,12 @@ function createSharedState(options) {
906
936
  if (syncIds.has(syncId)) return;
907
937
  enablePatches();
908
938
  state = applyPatches(state, patches);
909
- syncIds.add(syncId);
939
+ rememberSyncId(syncIds, syncId);
910
940
  events.emit("updated", state, void 0, syncId);
911
941
  },
912
942
  mutate: (fn, syncId = nanoid()) => {
913
943
  if (syncIds.has(syncId)) return;
914
- syncIds.add(syncId);
944
+ rememberSyncId(syncIds, syncId);
915
945
  if (enablePatches$1) {
916
946
  const [newState, patches] = produceWithPatches(state, fn);
917
947
  state = newState;
@@ -0,0 +1,101 @@
1
+ import { DEVFRAME_OTP_URL_PARAM } from "./constants.mjs";
2
+ import { a as timingSafeEqual, i as randomToken, r as randomDigits } from "./revoke-BtQDKTp7.mjs";
3
+ //#region src/node/auth/state.ts
4
+ /** Number of decimal digits in a human-typed one-time authentication code. */
5
+ const TEMP_AUTH_CODE_LENGTH = 6;
6
+ /**
7
+ * How long an authentication code stays valid after it is (re)generated. A
8
+ * 6-digit code only has ~20 bits of entropy, so a short lifetime plus the
9
+ * attempt cap below are what keep it brute-force resistant.
10
+ */
11
+ const TEMP_AUTH_CODE_TTL = 5 * 6e4;
12
+ /** Failed attempts allowed against a single code before it is rotated. */
13
+ const TEMP_AUTH_MAX_ATTEMPTS = 5;
14
+ let tempAuthCode = generateTempCode();
15
+ let tempAuthCodeExpiresAt = Date.now() + TEMP_AUTH_CODE_TTL;
16
+ let tempAuthFailedAttempts = 0;
17
+ function generateTempCode() {
18
+ return randomDigits(TEMP_AUTH_CODE_LENGTH);
19
+ }
20
+ /**
21
+ * The current one-time authentication code. Display this to the user (e.g. in
22
+ * the dev-server terminal) so they can type it into the browser to authenticate.
23
+ */
24
+ function getTempAuthCode() {
25
+ return tempAuthCode;
26
+ }
27
+ /**
28
+ * Rotate the authentication code, resetting its expiry window and failed-attempt
29
+ * counter. Call this when a new authentication flow begins (e.g. when an
30
+ * untrusted client starts authenticating) so the displayed code is freshly
31
+ * valid for its full TTL.
32
+ */
33
+ function refreshTempAuthCode() {
34
+ tempAuthCode = generateTempCode();
35
+ tempAuthCodeExpiresAt = Date.now() + TEMP_AUTH_CODE_TTL;
36
+ tempAuthFailedAttempts = 0;
37
+ return tempAuthCode;
38
+ }
39
+ /**
40
+ * Build a "magic link" authentication URL that embeds a one-time code (OTP) as
41
+ * a query parameter. Opening it authenticates the client without typing — print
42
+ * it on startup (devframe stays headless, so the host prints its own banner).
43
+ * Defaults to the current code; the link is subject to the same TTL.
44
+ */
45
+ function buildOtpAuthUrl(baseUrl, code = tempAuthCode) {
46
+ const url = new URL(baseUrl);
47
+ url.searchParams.set(DEVFRAME_OTP_URL_PARAM, code);
48
+ return url.href;
49
+ }
50
+ /**
51
+ * Re-authenticate a connection that presents a previously-issued bearer token.
52
+ * Returns `true` and marks the session trusted when the token is known.
53
+ *
54
+ * Used by the `anonymous:devframe:auth` handler so a client that already
55
+ * authenticated (token persisted in the browser) is trusted on reconnect
56
+ * without entering the code again.
57
+ */
58
+ function verifyAuthToken(token, session, storage) {
59
+ if (!token || !storage.value().trusted[token]) return false;
60
+ session.meta.clientAuthToken = token;
61
+ session.meta.isTrusted = true;
62
+ return true;
63
+ }
64
+ /**
65
+ * Exchange a one-time authentication code for a fresh, node-issued bearer token.
66
+ *
67
+ * On success this mints a high-entropy token, records it in the trusted store,
68
+ * marks the calling session trusted, rotates the code, and returns the token
69
+ * for the client to persist. Returns `null` on any failure.
70
+ *
71
+ * Because the code is short and human-typed, verification is hardened against
72
+ * brute force: it enforces a time-to-live, compares in constant time, and
73
+ * rotates the code after {@link TEMP_AUTH_MAX_ATTEMPTS} failed attempts so an
74
+ * attacker cannot keep guessing against the same code.
75
+ */
76
+ function exchangeTempAuthCode(code, session, info, storage) {
77
+ if (Date.now() > tempAuthCodeExpiresAt) {
78
+ refreshTempAuthCode();
79
+ return null;
80
+ }
81
+ if (!timingSafeEqual(code, tempAuthCode)) {
82
+ tempAuthFailedAttempts += 1;
83
+ if (tempAuthFailedAttempts >= TEMP_AUTH_MAX_ATTEMPTS) refreshTempAuthCode();
84
+ return null;
85
+ }
86
+ const authToken = randomToken();
87
+ storage.mutate((state) => {
88
+ state.trusted[authToken] = {
89
+ authToken,
90
+ ua: info.ua,
91
+ origin: info.origin,
92
+ timestamp: Date.now()
93
+ };
94
+ });
95
+ session.meta.clientAuthToken = authToken;
96
+ session.meta.isTrusted = true;
97
+ refreshTempAuthCode();
98
+ return authToken;
99
+ }
100
+ //#endregion
101
+ export { verifyAuthToken as a, refreshTempAuthCode as i, exchangeTempAuthCode as n, getTempAuthCode as r, buildOtpAuthUrl as t };