@hitch42/applet 0.1.0-beta.2 → 0.1.0-beta.9

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 (55) hide show
  1. package/dist/_internal/server.d.ts +14 -1
  2. package/dist/_internal/server.js +23 -74
  3. package/dist/applet.js +53 -29
  4. package/dist/build-BfbTbt_r.js +2 -0
  5. package/dist/{build-BRkmrUcj.js → build-CAb26kMY.js} +30 -14
  6. package/dist/ci-5qWl7lT3.js +68 -0
  7. package/dist/client.d.ts +3 -3
  8. package/dist/{contract-DMrpbaLJ.d.ts → contract-rP_eV9PK.d.ts} +1 -1
  9. package/dist/{deploy-DKB-Tef7.js → deploy-D_OFJ6-Q.js} +193 -30
  10. package/dist/email-DBTVhNB_.js +62 -0
  11. package/dist/generate-BgepN4-b.js +2 -0
  12. package/dist/{generate-BFmIzHaR.js → generate-CBPnHoqX.js} +40 -18
  13. package/dist/index.d.ts +122 -11
  14. package/dist/index.js +141 -26
  15. package/dist/{init-CQRsnd3i.js → init-tfzwuYnk.js} +12 -8
  16. package/dist/load-config-BYSxDVAl.js +27 -0
  17. package/dist/login-C7RKmmTe.js +12 -0
  18. package/dist/{logs-m00pzr_O.js → logs-DgjgAxj2.js} +3 -3
  19. package/dist/{manifest-DyoJ1MIL.js → manifest-Bp87Uz0a.js} +6 -0
  20. package/dist/{manifest-BQCAxATS.d.ts → manifest-DD_SGkS6.d.ts} +5 -0
  21. package/dist/me-BW7Paary.js +32 -0
  22. package/dist/{project-CEmDnlKa.js → project-D6XHllmC.js} +32 -20
  23. package/dist/prompt-ChS-grYO.js +29 -0
  24. package/dist/react.d.ts +1 -1
  25. package/dist/react.js +40 -42
  26. package/dist/schedules-dhU1yyFr.js +42 -0
  27. package/dist/server.d.ts +3 -3
  28. package/dist/telephony-protocol-D-nf3Gca.d.ts +76 -0
  29. package/dist/telephony.d.ts +77 -0
  30. package/dist/telephony.js +607 -0
  31. package/dist/{upload-YbRKEqR6.d.ts → upload-DKZzz5kW.d.ts} +6 -6
  32. package/dist/{variables-CP7Ce3Np.js → variables-Bwnr7oxf.js} +3 -28
  33. package/dist/vite.d.ts +1 -1
  34. package/dist/vite.js +464 -30
  35. package/dist/workflow-engine-MogejNfF.js +117 -0
  36. package/dist/{auth-DaVK0k5R.js → workspace-XVtM12ZC.js} +44 -4
  37. package/package.json +7 -1
  38. package/scaffold/.agents/skills/hitch-applets/SKILL.md +34 -15
  39. package/scaffold/.agents/skills/hitch-applets/references/email-tracking.md +49 -0
  40. package/scaffold/.agents/skills/hitch-applets/references/files.md +3 -3
  41. package/scaffold/.agents/skills/hitch-applets/references/manifest.md +21 -2
  42. package/scaffold/.agents/skills/hitch-applets/references/performance.md +74 -0
  43. package/scaffold/.agents/skills/hitch-applets/references/records.md +30 -8
  44. package/scaffold/.agents/skills/hitch-applets/references/schedules.md +7 -2
  45. package/scaffold/.agents/skills/hitch-applets/references/server.md +0 -3
  46. package/scaffold/.agents/skills/hitch-applets/references/workflows.md +11 -3
  47. package/scaffold/.agents/skills/hitch-cli/SKILL.md +129 -0
  48. package/scaffold/.agents/skills/hitch-data/SKILL.md +51 -0
  49. package/scaffold/.agents/skills/hitch-data/references/attributes.md +121 -0
  50. package/scaffold/.agents/skills/hitch-data/references/limits.md +64 -0
  51. package/scaffold/.agents/skills/hitch-theory/SKILL.md +150 -0
  52. package/scaffold/.github/workflows/hitch.yml +15 -0
  53. package/dist/build-CMluoV6q.js +0 -2
  54. package/dist/generate-CefnB-bp.js +0 -2
  55. package/dist/load-config-BnxocLAQ.js +0 -56
package/dist/vite.js CHANGED
@@ -1,16 +1,299 @@
1
- import { i as APPLET_CODE_PATTERN, n as appletManifestSchema, r as appletSourceDefinitionSchema } from "./manifest-DyoJ1MIL.js";
2
- import { i as resolveInstanceUrl, n as organizationInstanceUrl } from "./auth-DaVK0k5R.js";
3
- import { c as writeClientEntry, i as writeRegisterDeclaration, l as writeManifestGen, r as syncContract, s as writeAppletRoot, u as writeServerEntry } from "./generate-BFmIzHaR.js";
4
- import { i as resolveWorkspaceOrg } from "./load-config-BnxocLAQ.js";
5
- import { existsSync } from "node:fs";
6
- import { appendFile, readFile, readdir, rm, writeFile } from "node:fs/promises";
7
- import { basename, relative, resolve } from "node:path";
1
+ import { n as meSchema } from "./me-BW7Paary.js";
2
+ import { i as APPLET_CODE_PATTERN, n as appletManifestSchema, r as appletSourceDefinitionSchema } from "./manifest-Bp87Uz0a.js";
3
+ import { a as workflowOutcomeSchema, c as HITCH_SESSION_HEADER, l as HITCH_USER_HEADER, n as runWorkflowJournal } from "./workflow-engine-MogejNfF.js";
4
+ import { c as organizationInstanceUrl, d as resolveInstanceUrl, l as rejectEnvironmentKey, s as bearerToken, t as resolveWorkspaceOrg, u as resolveAuthForOrg } from "./workspace-XVtM12ZC.js";
5
+ import { c as writeClientEntry, i as writeRegisterDeclaration, l as writeManifestGen, r as syncContract, s as writeAppletRoot, u as writeServerEntry } from "./generate-CBPnHoqX.js";
6
+ import { randomUUID } from "node:crypto";
7
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
8
+ import { appendFile, readdir, rm, writeFile } from "node:fs/promises";
9
+ import { createServer } from "node:http";
10
+ import { basename, dirname, relative, resolve } from "node:path";
8
11
  import { cloudflare } from "@cloudflare/vite-plugin";
9
12
  import { tanstackRouter } from "@tanstack/router-plugin/vite";
10
13
  import react from "@vitejs/plugin-react";
14
+ import { Readable } from "node:stream";
15
+ import { pipeline } from "node:stream/promises";
16
+ //#region src/dev-proxy.ts
17
+ const REQUEST_DROP = /* @__PURE__ */ new Set([
18
+ "host",
19
+ "connection",
20
+ "content-length",
21
+ "cookie",
22
+ "authorization",
23
+ "origin",
24
+ "referer",
25
+ "accept-encoding"
26
+ ]);
27
+ const RESPONSE_DROP = /* @__PURE__ */ new Set([
28
+ "content-encoding",
29
+ "content-length",
30
+ "transfer-encoding",
31
+ "connection",
32
+ "set-cookie"
33
+ ]);
34
+ function refusesClient(headers, clientAddress) {
35
+ if (![
36
+ "127.0.0.1",
37
+ "::1",
38
+ "::ffff:127.0.0.1"
39
+ ].includes(clientAddress ?? "")) return true;
40
+ const get = (name) => headers instanceof Headers ? headers.get(name) : headers[name]?.toString() ?? null;
41
+ const host = get("host") ?? get(":authority");
42
+ const origin = get("origin");
43
+ return origin !== null && (host === null || !URL.canParse(origin) || new URL(origin).host !== host) || get("sec-fetch-site") === "cross-site";
44
+ }
45
+ function createDevProxy(options) {
46
+ let warnedAboutUnauthorized = false;
47
+ return async (request, clientAddress) => {
48
+ if (refusesClient(request.headers, clientAddress)) return new Response(null, { status: 403 });
49
+ const intercepted = await options.intercept?.(request);
50
+ if (intercepted !== void 0) return intercepted;
51
+ const headers = new Headers(request.headers);
52
+ [...headers.keys()].filter((name) => REQUEST_DROP.has(name) || name.startsWith("x-hitch-")).forEach((name) => headers.delete(name));
53
+ headers.set("authorization", `Bearer ${options.token}`);
54
+ const url = new URL(request.url);
55
+ const upstream = await (options.fetch ?? globalThis.fetch)(`${options.orgUrl}${url.pathname}${url.search}`, {
56
+ method: request.method,
57
+ headers,
58
+ body: request.body,
59
+ signal: request.signal,
60
+ ...request.body === null ? {} : { duplex: "half" }
61
+ });
62
+ if (upstream.status === 401 && !warnedAboutUnauthorized) {
63
+ warnedAboutUnauthorized = true;
64
+ options.warn(options.source === "environment" ? "HITCH_API_KEY was rejected." : "Your login expired. Run applet login, then restart the dev server.");
65
+ }
66
+ const responseHeaders = new Headers(upstream.headers);
67
+ [...responseHeaders.keys()].filter((name) => RESPONSE_DROP.has(name)).forEach((name) => responseHeaders.delete(name));
68
+ return new Response(upstream.body, {
69
+ status: upstream.status,
70
+ statusText: upstream.statusText,
71
+ headers: responseHeaders
72
+ });
73
+ };
74
+ }
75
+ async function serveDevProxy(request, response, handler) {
76
+ const controller = new AbortController();
77
+ const onClose = () => {
78
+ if (!response.writableFinished) controller.abort();
79
+ };
80
+ response.on("close", onClose);
81
+ try {
82
+ const authority = request.headers[":authority"];
83
+ const host = request.headers.host ?? (Array.isArray(authority) ? authority[0] : authority);
84
+ const url = `http://${host ?? "localhost"}${request.url ?? "/"}`;
85
+ const result = await handler(new Request(url, {
86
+ method: request.method,
87
+ headers: {
88
+ ...Object.fromEntries(Object.entries(request.headers).filter(([name]) => !name.startsWith(":"))),
89
+ host: host ?? "localhost"
90
+ },
91
+ signal: controller.signal,
92
+ ...["GET", "HEAD"].includes(request.method ?? "GET") ? {} : {
93
+ body: Readable.toWeb(request),
94
+ duplex: "half"
95
+ }
96
+ }), request.socket.remoteAddress);
97
+ response.writeHead(result.status, result.statusText, Object.fromEntries(result.headers));
98
+ if (result.body === null) {
99
+ response.end();
100
+ return;
101
+ }
102
+ await pipeline(Readable.fromWeb(result.body), response);
103
+ } catch (error) {
104
+ if (!controller.signal.aborted || !(error instanceof Error) || error.name !== "AbortError" && error.code !== "ERR_STREAM_PREMATURE_CLOSE") throw error;
105
+ } finally {
106
+ response.off("close", onClose);
107
+ }
108
+ }
109
+ //#endregion
110
+ //#region src/dev-workflows.ts
111
+ const RUN_STATUSES = [
112
+ "in_progress",
113
+ "sleeping",
114
+ "success",
115
+ "error",
116
+ "terminated",
117
+ "open"
118
+ ];
119
+ const badRequest = (message) => Response.json({
120
+ code: "BAD_REQUEST",
121
+ message
122
+ }, { status: 400 });
123
+ const workflowCodeError = (code) => code.length < 1 || code.length > 64 ? badRequest("The workflow code must have 1 to 64 characters.") : void 0;
124
+ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
125
+ function createDevWorkflows(options) {
126
+ const runs = /* @__PURE__ */ new Map();
127
+ const controller = new AbortController();
128
+ const stopped = () => controller.signal.aborted;
129
+ const wait = (milliseconds) => {
130
+ if (controller.signal.aborted) return Promise.reject(/* @__PURE__ */ new Error("The dev server stopped."));
131
+ return new Promise((resolve, reject) => {
132
+ const deadline = Date.now() + Math.max(0, milliseconds);
133
+ let timer;
134
+ const schedule = () => {
135
+ const remaining = deadline - Date.now();
136
+ if (remaining <= 0) {
137
+ controller.signal.removeEventListener("abort", onAbort);
138
+ resolve();
139
+ return;
140
+ }
141
+ timer = setTimeout(schedule, Math.min(remaining, 2147483647));
142
+ };
143
+ const onAbort = () => {
144
+ clearTimeout(timer);
145
+ controller.signal.removeEventListener("abort", onAbort);
146
+ reject(/* @__PURE__ */ new Error("The dev server stopped."));
147
+ };
148
+ controller.signal.addEventListener("abort", onAbort, { once: true });
149
+ schedule();
150
+ });
151
+ };
152
+ const record = (run, outcome) => {
153
+ switch (outcome.kind) {
154
+ case "step":
155
+ case "step_error":
156
+ run.status = "in_progress";
157
+ run.sleep_until = null;
158
+ break;
159
+ case "sleep":
160
+ run.status = "sleeping";
161
+ run.sleep_until = new Date(outcome.until).toISOString();
162
+ break;
163
+ case "done":
164
+ run.status = "success";
165
+ run.result = outcome.result ?? null;
166
+ run.sleep_until = null;
167
+ run.finished_at = (/* @__PURE__ */ new Date()).toISOString();
168
+ break;
169
+ case "error":
170
+ run.status = "error";
171
+ run.error = outcome.message.slice(0, 2048);
172
+ run.sleep_until = null;
173
+ run.finished_at = (/* @__PURE__ */ new Date()).toISOString();
174
+ }
175
+ };
176
+ const start = (workflow, payload) => {
177
+ const run = {
178
+ id: randomUUID(),
179
+ workflow,
180
+ status: "in_progress",
181
+ result: null,
182
+ error: null,
183
+ sleep_until: null,
184
+ started_at: (/* @__PURE__ */ new Date()).toISOString(),
185
+ finished_at: null
186
+ };
187
+ runs.set(run.id, run);
188
+ options.logger.info(`ℹ Workflow ${workflow} run ${run.id} started.`);
189
+ runWorkflowJournal({
190
+ invoke: async (_name, fn) => {
191
+ if (controller.signal.aborted) throw new Error("The dev server stopped.");
192
+ for (let attempt = 0;; attempt += 1) try {
193
+ const outcome = await fn();
194
+ if (stopped()) throw new Error("The dev server stopped.");
195
+ return outcome;
196
+ } catch (error) {
197
+ if (stopped() || attempt === 5) throw error;
198
+ const message = error instanceof Error ? error.message : String(error);
199
+ options.logger.error(`Workflow ${workflow} run ${run.id} invoke failed, retrying: ${message}`);
200
+ await wait(1e3 * 2 ** attempt);
201
+ }
202
+ },
203
+ sleep: (_name, milliseconds) => options.skipSleep ? Promise.resolve() : wait(milliseconds),
204
+ sleepUntil: (_name, until) => options.skipSleep ? Promise.resolve() : wait(until.getTime() - Date.now())
205
+ }, async (journal) => {
206
+ const outcome = await options.invoke(workflow, {
207
+ payload,
208
+ journal
209
+ });
210
+ record(run, outcome);
211
+ return outcome;
212
+ }).then(() => options.logger.info(`ℹ Workflow ${workflow} run ${run.id} succeeded.`), (error) => {
213
+ if (controller.signal.aborted) return;
214
+ const message = (error instanceof Error ? error.message : String(error)).slice(0, 2048);
215
+ run.status = "error";
216
+ run.error = message;
217
+ run.sleep_until = null;
218
+ run.finished_at = (/* @__PURE__ */ new Date()).toISOString();
219
+ options.logger.error(`Workflow ${workflow} run ${run.id} failed: ${message}`);
220
+ });
221
+ return run;
222
+ };
223
+ const handle = (request) => {
224
+ const url = new URL(request.url);
225
+ const prefix = `/api/v1/applets/${encodeURIComponent(options.appletCode)}`;
226
+ if (!url.pathname.startsWith(`${prefix}/`)) return void 0;
227
+ const path = url.pathname.slice(prefix.length);
228
+ const startMatch = /^\/workflows\/([^/]+)\/runs$/.exec(path);
229
+ if (request.method === "POST" && startMatch !== null) return (async () => {
230
+ let body;
231
+ try {
232
+ const text = await request.text();
233
+ body = text === "" ? {} : JSON.parse(text);
234
+ } catch {
235
+ return badRequest("The body must be a JSON object with a payload key.");
236
+ }
237
+ if (body === null || typeof body !== "object" || Array.isArray(body) || Object.keys(body).some((key) => key !== "payload")) return badRequest("The body must be a JSON object with a payload key.");
238
+ let workflow;
239
+ try {
240
+ workflow = decodeURIComponent(startMatch[1]);
241
+ } catch (error) {
242
+ if (error instanceof URIError) return badRequest(error.message);
243
+ throw error;
244
+ }
245
+ const codeError = workflowCodeError(workflow);
246
+ if (codeError !== void 0) return codeError;
247
+ const run = start(workflow, "payload" in body ? body.payload : void 0);
248
+ return Response.json({ data: {
249
+ id: run.id,
250
+ workflow: run.workflow,
251
+ status: "in_progress"
252
+ } }, { status: 201 });
253
+ })();
254
+ const getMatch = /^\/workflows\/([^/]+)\/runs\/([^/]+)$/.exec(path);
255
+ if (request.method === "GET" && getMatch !== null) {
256
+ let workflow;
257
+ let id;
258
+ try {
259
+ workflow = decodeURIComponent(getMatch[1]);
260
+ id = decodeURIComponent(getMatch[2]);
261
+ } catch (error) {
262
+ if (error instanceof URIError) return Promise.resolve(badRequest(error.message));
263
+ throw error;
264
+ }
265
+ const codeError = workflowCodeError(workflow);
266
+ if (codeError !== void 0) return Promise.resolve(codeError);
267
+ if (!UUID_PATTERN.test(id)) return Promise.resolve(badRequest("The run id must be a uuid."));
268
+ const run = runs.get(id);
269
+ return Promise.resolve(run?.workflow === workflow ? Response.json({ data: run }) : Response.json({
270
+ code: "workflow_run_not_found",
271
+ message: "The workflow run does not exist."
272
+ }, { status: 404 }));
273
+ }
274
+ if (request.method === "GET" && path === "/workflow-runs") {
275
+ const status = url.searchParams.get("status");
276
+ const limitValue = url.searchParams.get("limit");
277
+ const limit = limitValue === null ? 50 : Number(limitValue);
278
+ if (status !== null && !RUN_STATUSES.some((value) => value === status)) return Promise.resolve(badRequest("Invalid workflow run status."));
279
+ if (!Number.isInteger(limit) || limit < 1 || limit > 100) return Promise.resolve(badRequest("The limit must be an integer from 1 to 100."));
280
+ const workflow = url.searchParams.get("workflow");
281
+ const codeError = workflow === null ? void 0 : workflowCodeError(workflow);
282
+ if (codeError !== void 0) return Promise.resolve(codeError);
283
+ const data = [...runs.values()].filter((run) => (workflow === null || run.workflow === workflow) && (status === null || status === run.status || status === "open" && ["in_progress", "sleeping"].includes(run.status))).reverse().slice(0, limit).map(({ result: _result, ...summary }) => summary);
284
+ return Promise.resolve(Response.json({ data }));
285
+ }
286
+ };
287
+ return {
288
+ handle,
289
+ stop: () => controller.abort()
290
+ };
291
+ }
292
+ //#endregion
11
293
  //#region src/vite.ts
12
294
  const APPLET_COMPATIBILITY_DATE = "2026-07-14";
13
295
  const APPLET_COMPATIBILITY_FLAGS = ["nodejs_compat"];
296
+ let activeDevListener;
14
297
  function clientBootstrapScript(parentOrigin, appletCode) {
15
298
  return `(function () {
16
299
  var parentOrigin = ${parentOrigin === void 0 ? "null" : JSON.stringify(parentOrigin)};
@@ -152,6 +435,11 @@ function applet(sourceDefinition, options = {}) {
152
435
  let clearedOutDir = false;
153
436
  let contractSync;
154
437
  let themeStylesheetUrl;
438
+ let devApiUrl;
439
+ let devListener;
440
+ let devLogger;
441
+ let devWorkflows;
442
+ let devContext;
155
443
  const virtualRouteConfig = {
156
444
  type: "root",
157
445
  file: "__root.tsx",
@@ -170,6 +458,7 @@ function applet(sourceDefinition, options = {}) {
170
458
  const routesDirectory = resolve(resolvedRoot, "src/client/routes");
171
459
  const hasClient = existsSync(routesDirectory);
172
460
  const hasServer = existsSync(resolve(resolvedRoot, "src/server/main.ts"));
461
+ if (!hasServer && parsedDefinition.data.telephony?.events.length) throw new Error("An applet with telephony events must have src/server/main.ts.");
173
462
  if (!hasServer && (parsedDefinition.data.schedules?.length ?? 0) > 0) throw new Error("An applet with schedules must have src/server/main.ts.");
174
463
  const legacyMain = resolve(resolvedRoot, "src/client/main.tsx");
175
464
  const indexHtml = resolve(resolvedRoot, "index.html");
@@ -221,10 +510,7 @@ function applet(sourceDefinition, options = {}) {
221
510
  }
222
511
  if (environment.command === "build") themeStylesheetUrl = `${organizationInstanceUrl(await resolveInstanceUrl(void 0, options.store), manifest.org)}/theme.css`;
223
512
  if (environment.command === "build" && manifest.objects.length > 0) {
224
- contractSync ??= syncContract(resolve(root, ".hitch"), manifest, {
225
- ...options,
226
- changesetId: (options.store?.env ?? process.env).HITCH_CHANGESET_ID
227
- });
513
+ contractSync ??= syncContract(resolve(root, ".hitch"), manifest, options);
228
514
  await contractSync;
229
515
  }
230
516
  const hasHitchGen = existsSync(resolve(root, ".hitch/index.ts"));
@@ -239,17 +525,153 @@ function applet(sourceDefinition, options = {}) {
239
525
  hasServer ? writeServerEntry(root, hasHitchGen, manifest.code) : void 0,
240
526
  hasHitchGen ? void 0 : writeRegisterDeclaration(resolve(root, ".hitch"))
241
527
  ]);
242
- return { base: `/${manifest.code}/` };
528
+ if (environment.command === "serve" && environment.mode !== "test" && environment.isPreview !== true) {
529
+ const instanceUrl = await resolveInstanceUrl(void 0, options.store);
530
+ const orgUrl = organizationInstanceUrl(instanceUrl, manifest.org);
531
+ const auth = await resolveAuthForOrg(instanceUrl, manifest.org, options.store);
532
+ if (auth === void 0) throw new Error("Not logged in. Run applet login.");
533
+ const token = bearerToken(auth);
534
+ const meResponse = await (options.fetch ?? globalThis.fetch)(`${orgUrl}/api/v1/me`, { headers: { authorization: `Bearer ${token}` } });
535
+ rejectEnvironmentKey(meResponse, auth);
536
+ if (meResponse.status === 401) throw new Error("Your login expired. Run applet login.");
537
+ if (!meResponse.ok) throw new Error(`Hitch returned ${meResponse.status} for /api/v1/me.`);
538
+ const { person } = meSchema.parse(await meResponse.json());
539
+ const proxy = createDevProxy({
540
+ orgUrl,
541
+ token,
542
+ source: auth.source,
543
+ fetch: options.fetch,
544
+ warn: (message) => devLogger?.warn(message),
545
+ intercept: (request) => devWorkflows?.handle(request)
546
+ });
547
+ devContext = {
548
+ person,
549
+ proxy,
550
+ orgUrl,
551
+ cacheBuster: randomUUID()
552
+ };
553
+ if (hasServer && devListener === void 0) {
554
+ if (activeDevListener?.listening) activeDevListener.close();
555
+ const nonce = randomUUID();
556
+ devListener = createServer((request, response) => {
557
+ const url = request.url ?? "";
558
+ const prefix = `/${nonce}/api`;
559
+ const address = devListener?.address();
560
+ if (!(url === prefix || url.startsWith(`${prefix}/`) || url.startsWith(`${prefix}?`)) || address === null || address === void 0 || typeof address === "string" || request.headers.host !== `127.0.0.1:${address.port}`) {
561
+ response.writeHead(404).end();
562
+ return;
563
+ }
564
+ request.url = url.slice(nonce.length + 1);
565
+ serveDevProxy(request, response, proxy).catch((error) => {
566
+ const failure = error instanceof Error ? error : new Error(String(error));
567
+ devLogger?.error(`Local Hitch proxy failed: ${failure.message}`);
568
+ response.destroy(failure);
569
+ });
570
+ });
571
+ activeDevListener = devListener;
572
+ try {
573
+ await new Promise((resolvePromise, rejectPromise) => {
574
+ devListener?.once("error", rejectPromise);
575
+ devListener?.listen(0, "127.0.0.1", resolvePromise);
576
+ });
577
+ const address = devListener.address();
578
+ if (address === null || typeof address === "string") throw new Error("The local Hitch proxy has no port.");
579
+ devApiUrl = `http://127.0.0.1:${address.port}/${nonce}/api`;
580
+ } catch (error) {
581
+ if (devListener.listening) devListener.close();
582
+ if (activeDevListener === devListener) activeDevListener = void 0;
583
+ devListener = void 0;
584
+ throw error;
585
+ }
586
+ }
587
+ }
588
+ return {
589
+ base: `/${manifest.code}/`,
590
+ ...hasServer && { environments: { worker: { build: { rollupOptions: { output: { codeSplitting: false } } } } } }
591
+ };
243
592
  },
244
593
  configResolved(config) {
245
594
  apiManifest = resolveAppletConfig(config.root).manifest;
246
595
  },
247
596
  configureServer(server) {
248
- const { manifest, root } = requireAppletConfig();
597
+ if (devContext === void 0) return;
598
+ const { manifest } = requireAppletConfig();
599
+ const { person, proxy, orgUrl, cacheBuster } = devContext;
600
+ const orgOrigin = new URL(orgUrl).origin;
601
+ devLogger = server.config.logger;
602
+ const skipSleep = process.env.HITCH_DEV_SKIP_SLEEP === "1";
603
+ devWorkflows = createDevWorkflows({
604
+ appletCode: manifest.code,
605
+ skipSleep,
606
+ logger: server.config.logger,
607
+ invoke: async (workflow, body) => {
608
+ const address = server.httpServer?.address();
609
+ if (address === null || address === void 0 || typeof address === "string") throw new Error("The dev server has no port.");
610
+ const response = await fetch(`http://localhost:${address.port}/${manifest.code}/api/__hitch/workflows/${encodeURIComponent(workflow)}`, {
611
+ method: "POST",
612
+ headers: { "content-type": "application/json" },
613
+ body: JSON.stringify(body)
614
+ });
615
+ if (!response.ok) throw new Error(`Workflow worker returned HTTP ${response.status}.`);
616
+ return workflowOutcomeSchema.parse(await response.json());
617
+ }
618
+ });
619
+ server.config.logger.info(`ℹ Running as ${person.email} in production org "${manifest.org}". Reads and writes use real data with your access.`);
620
+ if (manifest.requiresChangeset !== void 0) server.config.logger.info(`ℹ This applet requires changeset ${manifest.requiresChangeset}. Local dev uses the live schema, so a pending changeset's schema is not available until it applies.`);
621
+ if (skipSleep) server.config.logger.info("ℹ HITCH_DEV_SKIP_SLEEP is set. Workflow sleeps and retry delays run at once.");
622
+ const devServerFile = resolve(requireAppletConfig().root, ".hitch/dev-server.json");
623
+ let devServerUrl;
624
+ const removeDevServerFile = () => {
625
+ if (devServerUrl === void 0) return;
626
+ try {
627
+ const file = JSON.parse(readFileSync(devServerFile, "utf8"));
628
+ if (file !== null && typeof file === "object" && "pid" in file && file.pid === process.pid && "url" in file && file.url === devServerUrl) rmSync(devServerFile, { force: true });
629
+ } catch (error) {
630
+ if (!(error instanceof SyntaxError) && !(error instanceof Error && "code" in error && error.code === "ENOENT")) throw error;
631
+ }
632
+ };
633
+ server.httpServer?.once("listening", () => {
634
+ const address = server.httpServer?.address();
635
+ if (address === null || address === void 0 || typeof address === "string") return;
636
+ mkdirSync(dirname(devServerFile), { recursive: true });
637
+ devServerUrl = `http://localhost:${address.port}`;
638
+ writeFileSync(devServerFile, JSON.stringify({
639
+ url: devServerUrl,
640
+ pid: process.pid
641
+ }));
642
+ server.config.logger.info(`ℹ Preview in Hitch: ${orgUrl}/applets/${manifest.code}?_hitch_dev=${address.port}`);
643
+ process.once("exit", removeDevServerFile);
644
+ });
645
+ server.httpServer?.once("close", () => {
646
+ devWorkflows?.stop();
647
+ devListener?.close();
648
+ if (activeDevListener === devListener) activeDevListener = void 0;
649
+ process.off("exit", removeDevServerFile);
650
+ removeDevServerFile();
651
+ });
249
652
  const mount = `/${manifest.code}`;
250
653
  const apiPrefix = `${mount}/api`;
251
654
  server.middlewares.use((request, response, next) => {
655
+ if (request.url === mount || request.url?.startsWith(`${mount}?`)) request.url = `${mount}/${request.url.slice(mount.length)}`;
252
656
  const url = request.url ?? "";
657
+ const pathname = url.split("?", 1)[0] ?? "";
658
+ if (pathname.startsWith("/__applet/context/")) {
659
+ if (pathname !== `/__applet/context/${manifest.code}` || request.method !== "GET") {
660
+ response.writeHead(404).end();
661
+ return;
662
+ }
663
+ if (refusesClient(request.headers, request.socket.remoteAddress)) {
664
+ response.writeHead(403).end();
665
+ return;
666
+ }
667
+ response.setHeader("Cache-Control", "no-store");
668
+ response.setHeader("Content-Type", "application/json; charset=utf-8");
669
+ response.end(JSON.stringify({
670
+ cacheBuster,
671
+ cacheScope: person.id
672
+ }));
673
+ return;
674
+ }
253
675
  if (url === "/_hitch/client.js" || url.startsWith("/_hitch/client.js?")) {
254
676
  response.statusCode = 200;
255
677
  response.setHeader("Cache-Control", "no-store");
@@ -257,29 +679,37 @@ function applet(sourceDefinition, options = {}) {
257
679
  response.end("// PostHog is disabled in standalone applet development.\n");
258
680
  return;
259
681
  }
260
- if (url === "/theme.css" || url.startsWith("/theme.css?")) {
261
- readFile(resolve(root, ".hitch/theme.css")).then((theme) => {
262
- response.statusCode = 200;
263
- response.setHeader("Cache-Control", "no-cache");
264
- response.setHeader("Content-Type", "text/css; charset=utf-8");
265
- response.end(theme);
266
- }, (error) => {
267
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
268
- next();
269
- return;
270
- }
271
- next(error);
682
+ if (url === "/api" || url.startsWith("/api/") || url.startsWith("/api?") || url === "/theme.css" || url.startsWith("/theme.css?")) {
683
+ serveDevProxy(request, response, proxy).catch((error) => {
684
+ if (response.headersSent) response.destroy(error instanceof Error ? error : new Error(String(error)));
685
+ else next(error);
272
686
  });
273
687
  return;
274
688
  }
275
- if (url === apiPrefix || url.startsWith(`${apiPrefix}/`) || url.startsWith(`${apiPrefix}?`)) request.url = url.slice(mount.length);
689
+ if (url === apiPrefix || url.startsWith(`${apiPrefix}/`) || url.startsWith(`${apiPrefix}?`)) {
690
+ if (refusesClient(request.headers, request.socket.remoteAddress)) {
691
+ response.writeHead(403).end();
692
+ return;
693
+ }
694
+ request.url = url.slice(mount.length);
695
+ if (request.url.startsWith("/api/__hitch/") && request.headers.origin !== void 0) {
696
+ response.writeHead(404).end();
697
+ return;
698
+ }
699
+ request.originalUrl = request.url;
700
+ request.headers[HITCH_USER_HEADER] = person.id;
701
+ request.headers[HITCH_SESSION_HEADER] = "local-dev";
702
+ request.rawHeaders = request.rawHeaders.flatMap((name, index, headers) => index % 2 === 0 && name.toLowerCase() !== "x-hitch-user" && name.toLowerCase() !== "x-hitch-session" ? [name, headers[index + 1] ?? ""] : []);
703
+ request.rawHeaders.push(HITCH_USER_HEADER, person.id, HITCH_SESSION_HEADER, "local-dev");
704
+ }
705
+ response.setHeader("Content-Security-Policy", `frame-ancestors ${orgOrigin}`);
276
706
  next();
277
707
  });
278
708
  },
279
709
  transformIndexHtml: {
280
710
  order: "post",
281
711
  handler(html) {
282
- const parentOrigin = themeStylesheetUrl === void 0 ? void 0 : new URL(themeStylesheetUrl).origin;
712
+ const parentOrigin = devContext?.orgUrl === void 0 ? themeStylesheetUrl === void 0 ? void 0 : new URL(themeStylesheetUrl).origin : new URL(devContext.orgUrl).origin;
283
713
  return {
284
714
  html: themeStylesheetUrl === void 0 ? html : html.replace("href=\"/theme.css\"", `href="${themeStylesheetUrl}"`),
285
715
  tags: [{
@@ -313,7 +743,7 @@ function applet(sourceDefinition, options = {}) {
313
743
  const workerDir = outputOptions.dir ?? resolve(buildRootOutDir, "worker");
314
744
  const mainModule = Object.values(bundle).find((output) => output.type === "chunk" && output.isEntry)?.fileName;
315
745
  const strayModules = (await readdir(workerDir, { recursive: true })).filter((path) => /\.(js|mjs)$/.test(path) && path !== mainModule);
316
- if (strayModules.length > 0) throw new Error(`The applet server must bundle to a single worker/index.js module; found: ${strayModules.join(", ")}. Avoid dynamic import() in server code.`);
746
+ if (strayModules.length > 0) throw new Error(`The applet server must bundle to a single worker/index.js module; found: ${strayModules.join(", ")}.`);
317
747
  console.log(`server → ${relative(requireAppletConfig().root, resolve(workerDir, mainModule ?? ""))}`);
318
748
  }
319
749
  }
@@ -333,7 +763,11 @@ function applet(sourceDefinition, options = {}) {
333
763
  not_found_handling: "single-page-application",
334
764
  run_worker_first: ["/api/*"]
335
765
  } } : {},
336
- ...hasServer ? { main: resolve(root, ".hitch/entry.server.gen.ts") } : {}
766
+ ...hasServer ? { main: resolve(root, ".hitch/entry.server.gen.ts") } : {},
767
+ ...devApiUrl === void 0 ? {} : { vars: {
768
+ ...workerConfig.vars,
769
+ HITCH_API_URL: devApiUrl
770
+ } }
337
771
  };
338
772
  },
339
773
  get viteEnvironment() {
@@ -350,7 +784,7 @@ function applet(sourceDefinition, options = {}) {
350
784
  routesDirectory: "src/client/routes",
351
785
  routeFileIgnorePattern: "__root",
352
786
  virtualRouteConfig
353
- }), react()] : [],
787
+ }), react({ compiler: true })] : [],
354
788
  cloudflarePlugins
355
789
  ];
356
790
  }