@astrale-os/sdk 0.1.6 → 0.1.7

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 (103) hide show
  1. package/dist/cli/bin.d.ts +7 -0
  2. package/dist/cli/bin.d.ts.map +1 -0
  3. package/dist/cli/bin.js +15 -0
  4. package/dist/cli/bin.js.map +1 -0
  5. package/dist/cli/dotenv.d.ts +13 -0
  6. package/dist/cli/dotenv.d.ts.map +1 -0
  7. package/dist/cli/dotenv.js +46 -0
  8. package/dist/cli/dotenv.js.map +1 -0
  9. package/dist/cli/index.d.ts +15 -0
  10. package/dist/cli/index.d.ts.map +1 -0
  11. package/dist/cli/index.js +15 -0
  12. package/dist/cli/index.js.map +1 -0
  13. package/dist/cli/run.d.ts +79 -0
  14. package/dist/cli/run.d.ts.map +1 -0
  15. package/dist/cli/run.js +569 -0
  16. package/dist/cli/run.js.map +1 -0
  17. package/dist/cli/spec.d.ts +19 -0
  18. package/dist/cli/spec.d.ts.map +1 -0
  19. package/dist/cli/spec.js +31 -0
  20. package/dist/cli/spec.js.map +1 -0
  21. package/dist/config/adapter.d.ts +140 -0
  22. package/dist/config/adapter.d.ts.map +1 -0
  23. package/dist/config/adapter.js +40 -0
  24. package/dist/config/adapter.js.map +1 -0
  25. package/dist/config/define-domain.d.ts +112 -0
  26. package/dist/config/define-domain.d.ts.map +1 -0
  27. package/dist/config/define-domain.js +98 -0
  28. package/dist/config/define-domain.js.map +1 -0
  29. package/dist/config/deploy.d.ts +28 -0
  30. package/dist/config/deploy.d.ts.map +1 -0
  31. package/dist/config/deploy.js +24 -0
  32. package/dist/config/deploy.js.map +1 -0
  33. package/dist/config/index.d.ts +21 -0
  34. package/dist/config/index.d.ts.map +1 -0
  35. package/dist/config/index.js +18 -0
  36. package/dist/config/index.js.map +1 -0
  37. package/dist/define/remote-function.d.ts +19 -11
  38. package/dist/define/remote-function.d.ts.map +1 -1
  39. package/dist/define/remote-function.js.map +1 -1
  40. package/dist/dispatch/call-remote.d.ts +7 -3
  41. package/dist/dispatch/call-remote.d.ts.map +1 -1
  42. package/dist/dispatch/call-remote.js.map +1 -1
  43. package/dist/dispatch/dispatcher.d.ts.map +1 -1
  44. package/dist/dispatch/dispatcher.js +8 -4
  45. package/dist/dispatch/dispatcher.js.map +1 -1
  46. package/dist/dispatch/index.d.ts +1 -1
  47. package/dist/dispatch/index.d.ts.map +1 -1
  48. package/dist/dispatch/index.js.map +1 -1
  49. package/dist/dispatch/self.d.ts +46 -10
  50. package/dist/dispatch/self.d.ts.map +1 -1
  51. package/dist/dispatch/self.js +65 -8
  52. package/dist/dispatch/self.js.map +1 -1
  53. package/dist/domain/define.d.ts +3 -3
  54. package/dist/domain/define.js +3 -3
  55. package/dist/index.d.ts +5 -4
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +8 -2
  58. package/dist/index.js.map +1 -1
  59. package/dist/method/class.d.ts.map +1 -1
  60. package/dist/method/class.js.map +1 -1
  61. package/dist/method/context.d.ts +32 -7
  62. package/dist/method/context.d.ts.map +1 -1
  63. package/dist/method/index.d.ts +1 -1
  64. package/dist/method/index.d.ts.map +1 -1
  65. package/dist/method/single.d.ts +16 -11
  66. package/dist/method/single.d.ts.map +1 -1
  67. package/dist/method/single.js.map +1 -1
  68. package/dist/server/domain-entry.d.ts +67 -0
  69. package/dist/server/domain-entry.d.ts.map +1 -0
  70. package/dist/server/domain-entry.js +58 -0
  71. package/dist/server/domain-entry.js.map +1 -0
  72. package/dist/server/index.d.ts +3 -1
  73. package/dist/server/index.d.ts.map +1 -1
  74. package/dist/server/index.js +2 -1
  75. package/dist/server/index.js.map +1 -1
  76. package/dist/server/worker-entry.d.ts +40 -5
  77. package/dist/server/worker-entry.d.ts.map +1 -1
  78. package/dist/server/worker-entry.js +68 -19
  79. package/dist/server/worker-entry.js.map +1 -1
  80. package/package.json +12 -3
  81. package/src/cli/bin.ts +15 -0
  82. package/src/cli/dotenv.ts +45 -0
  83. package/src/cli/index.ts +15 -0
  84. package/src/cli/run.ts +675 -0
  85. package/src/cli/spec.ts +42 -0
  86. package/src/config/adapter.ts +172 -0
  87. package/src/config/define-domain.ts +218 -0
  88. package/src/config/deploy.ts +35 -0
  89. package/src/config/index.ts +31 -0
  90. package/src/define/remote-function.ts +42 -13
  91. package/src/dispatch/call-remote.ts +7 -2
  92. package/src/dispatch/dispatcher.ts +8 -4
  93. package/src/dispatch/index.ts +1 -1
  94. package/src/dispatch/self.ts +96 -10
  95. package/src/domain/define.ts +3 -3
  96. package/src/index.ts +25 -4
  97. package/src/method/class.ts +4 -3
  98. package/src/method/context.ts +38 -7
  99. package/src/method/index.ts +1 -1
  100. package/src/method/single.ts +30 -11
  101. package/src/server/domain-entry.ts +113 -0
  102. package/src/server/index.ts +3 -1
  103. package/src/server/worker-entry.ts +80 -20
@@ -6,11 +6,13 @@
6
6
  * • resolve the serving URL (== `iss`), canonicalize it (so the value matches
7
7
  * what `createRemoteServer` signs with and the kernel pins), and cache the
8
8
  * built app per distinct URL;
9
- * • optional self-binding routing: when a `SELF` service binding is provided,
10
- * route same-host subrequests (e.g. `ctx.callRemote` back into this domain)
11
- * through it Cloudflare forbids a Worker fetching its own hostname. This is
12
- * the ONLY reason a `globalThis.fetch` override is installed, and only when a
13
- * `selfBinding` is configured;
9
+ * • optional subrequest routing: a `globalThis.fetch` override (installed ONLY
10
+ * when `selfBinding` and/or `routeSubrequest` is configured) redirects
11
+ * certain outbound fetches through a caller-supplied binding `selfBinding`
12
+ * for same-host fetches (a Worker can't fetch its own hostname), and the
13
+ * vendor-neutral `routeSubrequest` for any caller policy (e.g. instance
14
+ * hostnames a same-zone Worker→Worker fetch would 522 on). The SDK names no
15
+ * backend or topology; the caller owns the predicate + the fetcher;
14
16
  * • optional SPA hook (e.g. `/ui/*` served from an `ASSETS` binding).
15
17
  *
16
18
  * The worker's OWN JWKS (`<url>/.well-known/jwks.json`) is served as a normal
@@ -49,6 +51,17 @@ export interface WorkerEntryConfig<TDeps> {
49
51
  resolveUrl?: (env: TDeps, requestOrigin: string) => string
50
52
  /** Optional: the `SELF` service binding used to route same-host subrequests. */
51
53
  selfBinding?: (env: TDeps) => Fetcher | null | undefined
54
+ /**
55
+ * Optional, vendor-neutral: route an OUTBOUND subrequest through a
56
+ * caller-supplied fetcher instead of the network — for hosts a Worker can't
57
+ * reach directly over the edge (e.g. a platform router on the SAME zone:
58
+ * Cloudflare 522s a same-zone Worker→Worker public `fetch`). Return a `Fetcher`
59
+ * to route the request through, or null/undefined to fall through to the normal
60
+ * fetch. The SDK names no backend or topology — the CALLER owns BOTH the
61
+ * predicate (which hosts) and the fetcher (the binding). This generalizes
62
+ * `selfBinding` (same-origin → SELF) to any caller policy; both are honored.
63
+ */
64
+ routeSubrequest?: (url: URL, env: TDeps) => Fetcher | null | undefined
52
65
  /**
53
66
  * Optional: handle a request before it reaches the kernel app — e.g. serve a
54
67
  * SPA under `/ui/*` or a same-origin `/api/*` endpoint the view calls. Return
@@ -91,6 +104,46 @@ export function clientOrigin(url: URL, request: Request): string {
91
104
  return scheme === 'https' ? `https://${url.host}` : url.origin
92
105
  }
93
106
 
107
+ /**
108
+ * Build a `before` hook that serves a static-asset `binding` (e.g. a Workers
109
+ * Assets binding) mounted under `base` (default `/ui`) — the runtime half of a
110
+ * domain's `client` binding. Returns `undefined` for non-matching paths (and
111
+ * when no binding is present) so the request falls through to domain dispatch.
112
+ *
113
+ * Asset URLs are rooted at `base` (a client bundler sets `base: '<base>/'`);
114
+ * this hook strips that prefix before delegating to the binding, so
115
+ * `<base>/x.js` resolves from the binding's root. When `devProxy` yields a URL
116
+ * (local dev), requests are proxied there instead — the seam for a bundler's
117
+ * HMR dev server. Whether unknown sub-paths fall back to `index.html` is the
118
+ * binding's own concern (e.g. wrangler's `not_found_handling`), not baked in
119
+ * here.
120
+ *
121
+ * Lives here, type-checked and testable, instead of being emitted as a string
122
+ * by every adapter's worker codegen.
123
+ */
124
+ export function assets<TDeps>(opts: {
125
+ base?: string
126
+ binding: (env: TDeps) => Fetcher | null | undefined
127
+ devProxy?: (env: TDeps) => string | null | undefined
128
+ }): (env: TDeps, url: URL, request: Request) => Response | Promise<Response> | undefined {
129
+ const base = (opts.base ?? '/ui').replace(/\/+$/, '')
130
+ const prefix = `${base}/`
131
+ return (env, url, request) => {
132
+ const binding = opts.binding(env)
133
+ if (!binding) return undefined
134
+ if (url.pathname !== base && !url.pathname.startsWith(prefix)) return undefined
135
+ const devUrl = opts.devProxy?.(env)
136
+ if (devUrl) {
137
+ const devBase = devUrl.replace(/\/+$/, '')
138
+ return fetch(new Request(`${devBase}${url.pathname}${url.search}`, request))
139
+ }
140
+ // `<base>` → `/`, `<base>/x` → `/x`: serve from the binding's root.
141
+ const stripped = url.pathname.slice(base.length) || '/'
142
+ const rewritten = new URL(stripped + url.search, url.origin)
143
+ return binding.fetch(new Request(rewritten, request))
144
+ }
145
+ }
146
+
94
147
  export function createWorkerEntry<TDeps>(config: WorkerEntryConfig<TDeps>): WorkerEntry<TDeps> {
95
148
  // Cache the built app per distinct resolved URL — plural and bounded. On the
96
149
  // request-origin fallback the URL legitimately alternates for one worker
@@ -101,6 +154,7 @@ export function createWorkerEntry<TDeps>(config: WorkerEntryConfig<TDeps>): Work
101
154
  const MAX_CACHED_APPS = 4
102
155
  const apps = new Map<string, { origin: string; app: App }>()
103
156
  let self: Fetcher | null = null
157
+ let routeEnv: TDeps | null = null
104
158
 
105
159
  function getApp(url: string, env: TDeps): App {
106
160
  const cached = apps.get(url)
@@ -114,26 +168,31 @@ export function createWorkerEntry<TDeps>(config: WorkerEntryConfig<TDeps>): Work
114
168
  return app
115
169
  }
116
170
 
117
- // A Worker can't fetch its own hostname. When a SELF service binding is
118
- // configured, route same-host subrequests (e.g. `ctx.callRemote` back into
119
- // this domain) through it. This is the only reason to override
120
- // `globalThis.fetch` workers without a `selfBinding` get no global mutation.
121
- // (A self-issued credential's JWKS is resolved in-memory by the verifier, not
122
- // fetched — see `auth/verify.ts` — so no self-JWKS interception is needed.)
123
- if (config.selfBinding) {
171
+ // A Worker can't fetch its own hostname (SELF), nor on Cloudflare — a
172
+ // same-zone hostname routed to another Worker (the `routeSubrequest` case).
173
+ // When either is configured, override `globalThis.fetch` to redirect those
174
+ // subrequests through the caller's fetcher. Workers with neither get no global
175
+ // mutation. (A self-issued credential's JWKS is resolved in-memory by the
176
+ // verifier, not fetched — see `auth/verify.ts` — so no self-JWKS shim is needed.)
177
+ if (config.selfBinding || config.routeSubrequest) {
124
178
  const originalFetch = globalThis.fetch
125
179
  globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
126
- if (apps.size > 0 && self) {
127
- const href =
128
- typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
129
- try {
130
- const origin = new URL(href).origin
180
+ const href = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
181
+ try {
182
+ const u = new URL(href)
183
+ // same-origin → SELF (a Worker can't fetch its own hostname)
184
+ if (self && apps.size > 0) {
131
185
  for (const cached of apps.values()) {
132
- if (cached.origin === origin) return self.fetch(new Request(input, init))
186
+ if (cached.origin === u.origin) return self.fetch(new Request(input, init))
133
187
  }
134
- } catch {
135
- // non-absolute URL — fall through to the original fetch
136
188
  }
189
+ // caller policy → its fetcher (e.g. a platform router on the same zone)
190
+ if (config.routeSubrequest && routeEnv) {
191
+ const via = config.routeSubrequest(u, routeEnv)
192
+ if (via) return via.fetch(new Request(input, init))
193
+ }
194
+ } catch {
195
+ // non-absolute URL — fall through to the original fetch
137
196
  }
138
197
  return originalFetch(input, init)
139
198
  }) as typeof fetch
@@ -142,6 +201,7 @@ export function createWorkerEntry<TDeps>(config: WorkerEntryConfig<TDeps>): Work
142
201
  return {
143
202
  async fetch(request: Request, env: TDeps): Promise<Response> {
144
203
  if (config.selfBinding) self ??= config.selfBinding(env) ?? null
204
+ if (config.routeSubrequest) routeEnv = env
145
205
  // Only parse the request URL when a hook actually needs it.
146
206
  const requestUrl = config.before || config.resolveUrl ? new URL(request.url) : null
147
207
  if (config.before && requestUrl) {