zitejs 0.10.0-start.2 → 0.10.0-start.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/auth/index.d.ts +2 -2
- package/dist/cjs/auth/index.js +6 -0
- package/dist/cjs/auth/index.test.js +30 -0
- package/dist/cjs/auth/previewClaim.d.ts +7 -0
- package/dist/cjs/auth/previewClaim.js +16 -0
- package/dist/cjs/backend/index.d.ts +121 -3
- package/dist/cjs/backend/index.test.js +58 -0
- package/dist/cjs/bundle/index.d.ts +8 -0
- package/dist/cjs/bundle/index.js +9 -8
- package/dist/cjs/check/index.d.ts +45 -1
- package/dist/cjs/check/index.js +198 -109
- package/dist/cjs/check/start/problem.d.ts +11 -0
- package/dist/cjs/check/start/problem.js +5 -0
- package/dist/cjs/check/start/routeTree.d.ts +9 -0
- package/dist/cjs/check/start/routeTree.js +111 -0
- package/dist/cjs/check/start/sourceRules.d.ts +8 -0
- package/dist/cjs/check/start/sourceRules.js +1110 -0
- package/dist/cjs/check/start/tsgo.d.ts +2 -0
- package/dist/cjs/check/start/tsgo.js +35 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/client/index.d.ts +24 -0
- package/dist/cjs/dev/index.js +14 -4
- package/dist/cjs/integrations/rules.d.ts +13 -0
- package/dist/cjs/integrations/rules.js +29 -0
- package/dist/cjs/integrations/rules.test.js +1 -0
- package/dist/cjs/internal/appMode.d.ts +3 -0
- package/dist/cjs/internal/appMode.js +15 -0
- package/dist/cjs/runtime/index.d.ts +13 -13
- package/dist/cjs/start/entries.d.ts +10 -4
- package/dist/cjs/start/entries.js +23 -6
- package/dist/cjs/steps/index.d.ts +78 -0
- package/dist/cjs/steps/index.js +507 -0
- package/dist/cjs/steps/index.test.d.ts +1 -0
- package/dist/cjs/steps/index.test.js +635 -0
- package/dist/cjs/sync/index.js +2 -1
- package/dist/cjs/sync/lib.d.ts +10 -3
- package/dist/cjs/sync/lib.js +35 -21
- package/dist/cjs/sync/lib.test.js +106 -36
- package/dist/cjs/types/fields.d.ts +8 -1
- package/dist/cjs/types/fields.js +7 -1
- package/dist/cjs/types/tables.d.ts +16 -0
- package/dist/cjs/upload/index.js +12 -2
- package/dist/cjs/upload/index.test.js +39 -0
- package/dist/cjs/vite/index.js +8 -3
- package/dist/cjs/vite/index.test.js +10 -2
- package/dist/esm/auth/index.d.ts +2 -2
- package/dist/esm/auth/index.js +6 -0
- package/dist/esm/auth/index.test.js +30 -0
- package/dist/esm/auth/previewClaim.d.ts +7 -0
- package/dist/esm/auth/previewClaim.js +12 -0
- package/dist/esm/backend/index.d.ts +121 -3
- package/dist/esm/backend/index.test.js +58 -0
- package/dist/esm/bundle/index.d.ts +8 -0
- package/dist/esm/bundle/index.js +8 -8
- package/dist/esm/check/index.d.ts +45 -1
- package/dist/esm/check/index.js +198 -113
- package/dist/esm/check/start/problem.d.ts +11 -0
- package/dist/esm/check/start/problem.js +1 -0
- package/dist/esm/check/start/routeTree.d.ts +9 -0
- package/dist/esm/check/start/routeTree.js +70 -0
- package/dist/esm/check/start/sourceRules.d.ts +8 -0
- package/dist/esm/check/start/sourceRules.js +1107 -0
- package/dist/esm/check/start/tsgo.d.ts +2 -0
- package/dist/esm/check/start/tsgo.js +29 -0
- package/dist/esm/cli.js +1 -1
- package/dist/esm/client/index.d.ts +24 -0
- package/dist/esm/dev/index.js +16 -6
- package/dist/esm/integrations/rules.d.ts +13 -0
- package/dist/esm/integrations/rules.js +29 -0
- package/dist/esm/integrations/rules.test.js +1 -0
- package/dist/esm/internal/appMode.d.ts +3 -0
- package/dist/esm/internal/appMode.js +10 -0
- package/dist/esm/runtime/index.d.ts +13 -13
- package/dist/esm/start/aiUsage.d.ts +23 -0
- package/dist/esm/start/aiUsage.js +146 -0
- package/dist/esm/start/airtable/arrayFields.d.ts +10 -0
- package/dist/esm/start/airtable/arrayFields.js +30 -0
- package/dist/esm/start/airtable/dispatch.d.ts +54 -0
- package/dist/esm/start/airtable/dispatch.js +483 -0
- package/dist/esm/start/airtable/formulas.d.ts +70 -0
- package/dist/esm/start/airtable/formulas.js +258 -0
- package/dist/esm/start/airtable/index.d.ts +8 -0
- package/dist/esm/start/airtable/index.js +54 -0
- package/dist/esm/start/authHooks.d.ts +7 -0
- package/dist/esm/start/authHooks.js +10 -0
- package/dist/esm/start/authHooksModule.d.ts +1 -0
- package/dist/esm/start/authHooksModule.js +3 -0
- package/dist/esm/start/clientErrorMessage.test.d.ts +1 -0
- package/dist/esm/start/clientErrorMessage.test.js +105 -0
- package/dist/esm/start/credentials.d.ts +2 -0
- package/dist/esm/start/credentials.js +2 -0
- package/dist/esm/start/data/baseRunner.d.ts +92 -0
- package/dist/esm/start/data/baseRunner.js +83 -0
- package/dist/esm/start/data/dispatch.d.ts +16 -0
- package/dist/esm/start/data/dispatch.js +198 -0
- package/dist/esm/start/data/index.d.ts +15 -0
- package/dist/esm/start/data/index.js +58 -0
- package/dist/esm/start/data/sqlNameMappings.d.ts +58 -0
- package/dist/esm/start/data/sqlNameMappings.js +92 -0
- package/dist/esm/start/data/transformations.d.ts +97 -0
- package/dist/esm/start/data/transformations.js +262 -0
- package/dist/esm/start/data/types.d.ts +91 -0
- package/dist/esm/start/data/types.js +14 -0
- package/dist/esm/start/describe.d.ts +35 -5
- package/dist/esm/start/describe.js +74 -22
- package/dist/esm/start/entries.d.ts +10 -4
- package/dist/esm/start/entries.js +21 -5
- package/dist/esm/start/errors.d.ts +63 -0
- package/dist/esm/start/errors.js +146 -0
- package/dist/esm/start/errors.test.d.ts +1 -0
- package/dist/esm/start/errors.test.js +23 -0
- package/dist/esm/start/factories.test-d.d.ts +1 -0
- package/dist/esm/start/factories.test-d.js +20 -0
- package/dist/esm/start/index.d.ts +32 -27
- package/dist/esm/start/index.js +77 -47
- package/dist/esm/start/instance.d.ts +4 -4
- package/dist/esm/start/instance.js +54 -11
- package/dist/esm/start/outbound.d.ts +11 -0
- package/dist/esm/start/outbound.js +398 -0
- package/dist/esm/start/previewClaim.d.ts +7 -0
- package/dist/esm/start/previewClaim.js +23 -0
- package/dist/esm/start/problems.js +1 -1
- package/dist/esm/start/quiet.d.ts +2 -0
- package/dist/esm/start/quiet.js +27 -0
- package/dist/esm/start/redactSecrets.d.ts +15 -0
- package/dist/esm/start/redactSecrets.js +154 -0
- package/dist/esm/start/redactSecrets.test.d.ts +1 -0
- package/dist/esm/start/redactSecrets.test.js +151 -0
- package/dist/esm/start/registration.d.ts +13 -0
- package/dist/esm/start/registration.js +21 -0
- package/dist/esm/start/registrationHash.d.ts +22 -0
- package/dist/esm/start/registrationHash.js +470 -0
- package/dist/esm/start/router.d.ts +18 -4
- package/dist/esm/start/router.js +134 -11
- package/dist/esm/start/runLog.d.ts +7 -4
- package/dist/esm/start/runLog.js +64 -6
- package/dist/esm/start/scope.d.ts +2 -2
- package/dist/esm/start/server.d.ts +40 -36
- package/dist/esm/start/server.js +320 -48
- package/dist/esm/start/steps.d.ts +28 -0
- package/dist/esm/start/steps.js +97 -0
- package/dist/esm/start/types.d.ts +178 -5
- package/dist/esm/start/version.d.ts +1 -0
- package/dist/esm/start/version.js +2 -0
- package/dist/esm/start/vite.js +88 -11
- package/dist/esm/start/worker.d.ts +6 -0
- package/dist/esm/start/worker.js +12 -0
- package/dist/esm/start/workflow.d.ts +42 -12
- package/dist/esm/start/workflow.js +3 -3
- package/dist/esm/start/workflow.test-d.js +41 -8
- package/dist/esm/start/workflowModules.js +1 -1
- package/dist/esm/start/workflowRun.d.ts +21 -8
- package/dist/esm/start/workflowRun.js +29 -8
- package/dist/esm/steps/index.d.ts +78 -0
- package/dist/esm/steps/index.js +502 -0
- package/dist/esm/steps/index.test.d.ts +1 -0
- package/dist/esm/steps/index.test.js +633 -0
- package/dist/esm/sync/index.js +2 -1
- package/dist/esm/sync/lib.d.ts +10 -3
- package/dist/esm/sync/lib.js +35 -21
- package/dist/esm/sync/lib.test.js +106 -36
- package/dist/esm/types/fields.d.ts +8 -1
- package/dist/esm/types/fields.js +6 -0
- package/dist/esm/types/tables.d.ts +16 -0
- package/dist/esm/upload/index.js +12 -2
- package/dist/esm/upload/index.test.js +39 -0
- package/dist/esm/vite/index.js +8 -3
- package/dist/esm/vite/index.test.js +10 -2
- package/package.json +37 -16
- package/dist/cjs/start/problems.d.ts +0 -19
- package/dist/cjs/start/problems.js +0 -11
|
@@ -2,11 +2,25 @@
|
|
|
2
2
|
// `src/start.ts`, which the generated instance composes after the platform's.
|
|
3
3
|
import { createCsrfMiddleware, createMiddleware, createStart } from "@tanstack/react-start";
|
|
4
4
|
import { createSerializationAdapter, redirect } from "@tanstack/react-router";
|
|
5
|
-
import {
|
|
5
|
+
import { getStartContext } from "@tanstack/start-storage-context";
|
|
6
|
+
import { asCrossed, errorFromThrown, toClientError, withThrownRejections, ZiteError } from "./errors.js";
|
|
7
|
+
import { isSignInRequired, loginHref, setFailedStatus } from "./index.js";
|
|
6
8
|
import { errorOf, preview } from "./runLog.js";
|
|
9
|
+
import { currentHost } from "./host.js";
|
|
7
10
|
import { getHost, inScope } from "./scope.js";
|
|
8
11
|
// TanStack drops its default CSRF check once an app has a start instance.
|
|
9
12
|
const csrf = createCsrfMiddleware({ filter: (ctx) => ctx.handlerType === "serverFn" });
|
|
13
|
+
// The app's own request middleware runs inside this one. On a server-function request
|
|
14
|
+
// what it throws crosses to the browser as thrown, and only an Error goes through the
|
|
15
|
+
// zite-error adapter.
|
|
16
|
+
const thrownAsErrors = createMiddleware().server(async ({ next }) => {
|
|
17
|
+
try {
|
|
18
|
+
return await next();
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
throw errorFromThrown(error);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
10
24
|
// The host answers a signed-out call with a plain 401, the app with a ZiteError;
|
|
11
25
|
// either way the browser goes to sign in rather than showing an error.
|
|
12
26
|
const signInOn401 = createMiddleware({ type: "function" }).client(async ({ next }) => {
|
|
@@ -23,7 +37,11 @@ const signInOn401 = createMiddleware({ type: "function" }).client(async ({ next
|
|
|
23
37
|
}
|
|
24
38
|
});
|
|
25
39
|
// Records each call for the run log. The host's own record of the request is the
|
|
26
|
-
// floor; this adds what only the app sees: decoded input, output and errors.
|
|
40
|
+
// floor; this adds what only the app sees: decoded input, output and errors. The
|
|
41
|
+
// app's own function middleware runs inside it, outside the access middleware, so
|
|
42
|
+
// what it throws, and what a promise in the result rejects with, is normalized here
|
|
43
|
+
// on its way to the browser. A call made in-process while a page renders gets the
|
|
44
|
+
// error the browser would: its caller may put the message into the page.
|
|
27
45
|
const runCapture = createMiddleware({ type: "function" }).server(async ({ next, data, serverFnMeta }) => {
|
|
28
46
|
const runs = getHost()?.runs;
|
|
29
47
|
const startedAt = Date.now();
|
|
@@ -36,23 +54,48 @@ const runCapture = createMiddleware({ type: "function" }).server(async ({ next,
|
|
|
36
54
|
input: preview(data),
|
|
37
55
|
...("error" in outcome ? { error: errorOf(outcome.error) } : { output: preview(outcome.output) }),
|
|
38
56
|
});
|
|
57
|
+
const inProcess = !inScope() && getStartContext({ throwIfNotFound: false })?.handlerType !== "serverFn";
|
|
58
|
+
const crossed = (error) => (inProcess && error instanceof Error ? asCrossed(error) : error);
|
|
59
|
+
const fail = (error) => {
|
|
60
|
+
const failure = errorFromThrown(error);
|
|
61
|
+
record({ error: failure });
|
|
62
|
+
setFailedStatus(failure);
|
|
63
|
+
return crossed(failure);
|
|
64
|
+
};
|
|
39
65
|
try {
|
|
40
66
|
const out = await next();
|
|
41
67
|
const failed = out.error;
|
|
42
|
-
|
|
43
|
-
|
|
68
|
+
if (failed !== undefined)
|
|
69
|
+
return { ...out, error: fail(failed) };
|
|
70
|
+
const { result } = out;
|
|
71
|
+
record({ output: result });
|
|
72
|
+
const settled = withThrownRejections(result, crossed);
|
|
73
|
+
return settled === result ? out : { ...out, result: settled };
|
|
44
74
|
}
|
|
45
75
|
catch (error) {
|
|
46
|
-
|
|
47
|
-
throw error;
|
|
76
|
+
throw fail(error);
|
|
48
77
|
}
|
|
49
78
|
});
|
|
50
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* Every error a server function or a page's loader sends the browser arrives as a ZiteError, with its code and the
|
|
81
|
+
* message classic would show. Anything else on it, an SDK's request, stays on the server; its stack goes only to the
|
|
82
|
+
* editor's preview, for "Fix it for me".
|
|
83
|
+
*/
|
|
51
84
|
const ziteErrorAdapter = createSerializationAdapter({
|
|
52
85
|
key: "zite-error",
|
|
53
|
-
test: (value) => value instanceof
|
|
54
|
-
toSerializable: (error) =>
|
|
55
|
-
|
|
86
|
+
test: (value) => value instanceof Error,
|
|
87
|
+
toSerializable: (error) => {
|
|
88
|
+
const { code, message } = toClientError(error);
|
|
89
|
+
// `currentHost`, not `getHost`: this adapter ships to the browser, which mustn't bundle the request scope.
|
|
90
|
+
const stack = currentHost()?.mode === "preview" ? error.stack : undefined;
|
|
91
|
+
return { code, message, ...(stack && { stack }) };
|
|
92
|
+
},
|
|
93
|
+
fromSerializable: ({ code, message, stack }) => {
|
|
94
|
+
const error = new ZiteError(code, message);
|
|
95
|
+
if (stack)
|
|
96
|
+
error.stack = stack;
|
|
97
|
+
return error;
|
|
98
|
+
},
|
|
56
99
|
});
|
|
57
100
|
/**
|
|
58
101
|
* The start instance every Start app builds with: the platform's middleware and
|
|
@@ -65,7 +108,7 @@ export function createZiteStart(app) {
|
|
|
65
108
|
// route tree registers for typed context and serialization.
|
|
66
109
|
return {
|
|
67
110
|
...own,
|
|
68
|
-
requestMiddleware: [csrf, ...(own?.requestMiddleware ?? [])],
|
|
111
|
+
requestMiddleware: [csrf, thrownAsErrors, ...(own?.requestMiddleware ?? [])],
|
|
69
112
|
functionMiddleware: [signInOn401, runCapture, ...(own?.functionMiddleware ?? [])],
|
|
70
113
|
serializationAdapters: [ziteErrorAdapter, ...(own?.serializationAdapters ?? [])],
|
|
71
114
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every fetch an app makes on the server, wrapped as classic's runtime wraps
|
|
3
|
+
* it: logged, every secret it knows redacted, with the connection whose token
|
|
4
|
+
* it carries and what an org's own AI key used; a 401 on a connection's token
|
|
5
|
+
* refreshed and sent once more; and a call on Zite's own AI key sent to the
|
|
6
|
+
* host over the app's platform binding.
|
|
7
|
+
*/
|
|
8
|
+
import type { ZiteRequestContext } from "./types.js";
|
|
9
|
+
/** An `integrations.get` answer, whose values name the calls that carry them. */
|
|
10
|
+
export declare const rememberConnection: (host: ZiteRequestContext, name: string, connection: unknown) => void;
|
|
11
|
+
export declare const createOutboundFetch: (nativeFetch: typeof fetch) => typeof fetch;
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every fetch an app makes on the server, wrapped as classic's runtime wraps
|
|
3
|
+
* it: logged, every secret it knows redacted, with the connection whose token
|
|
4
|
+
* it carries and what an org's own AI key used; a 401 on a connection's token
|
|
5
|
+
* refreshed and sent once more; and a call on Zite's own AI key sent to the
|
|
6
|
+
* host over the app's platform binding.
|
|
7
|
+
*/
|
|
8
|
+
import { AI_USAGE_HOSTS, meterAiResponse, withStreamUsage } from "./aiUsage.js";
|
|
9
|
+
import { REDACTED, redactKnown, secretForms } from "./redactSecrets.js";
|
|
10
|
+
import { getHost } from "./scope.js";
|
|
11
|
+
const MAX_FETCHES = 500;
|
|
12
|
+
// Shorter values (a region, a flag) aren't tokens, and could match by chance.
|
|
13
|
+
const MIN_TOKEN_LENGTH = 8;
|
|
14
|
+
// As classic: Workers send no User-Agent, and some APIs (GitHub) refuse a request without one.
|
|
15
|
+
const DEFAULT_USER_AGENT = "zite";
|
|
16
|
+
// The backend's stand-in for Zite's own AI key, which only the host answers.
|
|
17
|
+
const AI_MARKER = /^zite_ai_gateway:[a-z]+:(.+)$/;
|
|
18
|
+
// Where a provider SDK given the connection's `baseUrl` sends it; it never resolves.
|
|
19
|
+
const AI_HOST = "zite-ai.invalid";
|
|
20
|
+
// A provider's own base URL at the host: `/v1` is OpenAI's, Anthropic's SDK sends it in the path.
|
|
21
|
+
const AI_PROVIDER_HOSTS = {
|
|
22
|
+
"api.openai.com": { provider: "openai", strip: "/v1" },
|
|
23
|
+
"api.anthropic.com": { provider: "anthropic", strip: "" },
|
|
24
|
+
};
|
|
25
|
+
// As classic's run log: a body up to 50 KB, a request's fetch bodies up to 200 KB in all.
|
|
26
|
+
const MAX_BODY_CHARS = 50 * 1024;
|
|
27
|
+
const MAX_LOGGED_CHARS = 200 * 1024;
|
|
28
|
+
const MAX_URL_CHARS = 2_000;
|
|
29
|
+
const MAX_ERROR_CHARS = 1_000;
|
|
30
|
+
const SECRET_HEADER = /auth|key|token|secret|signature|password|cookie|session/i;
|
|
31
|
+
// Usage still being read when the request's log is flushed isn't waited on for longer.
|
|
32
|
+
const USAGE_WAIT_MS = 10_000;
|
|
33
|
+
// Tokens refreshed in this isolate, which a later request's props may not name yet.
|
|
34
|
+
const MAX_REFRESHED = 50;
|
|
35
|
+
const refreshedTokens = new Map();
|
|
36
|
+
const replacements = new Map();
|
|
37
|
+
// Moves with each refresh, so a request's list of secrets takes in a token another one refreshed.
|
|
38
|
+
let refreshes = 0;
|
|
39
|
+
const remember = (map, key, value) => {
|
|
40
|
+
map.delete(key);
|
|
41
|
+
map.set(key, value);
|
|
42
|
+
if (map.size > MAX_REFRESHED)
|
|
43
|
+
map.delete(map.keys().next().value);
|
|
44
|
+
};
|
|
45
|
+
const tokensOf = (host) => {
|
|
46
|
+
if (host.connectionTokens)
|
|
47
|
+
return host.connectionTokens;
|
|
48
|
+
const tokens = new Map(refreshedTokens);
|
|
49
|
+
for (const [key, name] of Object.entries(host.envConnections ?? {})) {
|
|
50
|
+
const value = host.env?.[key];
|
|
51
|
+
if (value && value.length >= MIN_TOKEN_LENGTH)
|
|
52
|
+
tokens.set(value, name);
|
|
53
|
+
}
|
|
54
|
+
host.connectionTokens = tokens;
|
|
55
|
+
return tokens;
|
|
56
|
+
};
|
|
57
|
+
/** An `integrations.get` answer, whose values name the calls that carry them. */
|
|
58
|
+
export const rememberConnection = (host, name, connection) => {
|
|
59
|
+
(host.connectionAnswers ??= new Map()).set(name, connection);
|
|
60
|
+
host.secretValues = undefined;
|
|
61
|
+
const tokens = tokensOf(host);
|
|
62
|
+
for (const [field, value] of Object.entries((connection ?? {}))) {
|
|
63
|
+
if (field !== "status" && field !== "baseUrl" && typeof value === "string" && value.length >= MIN_TOKEN_LENGTH) {
|
|
64
|
+
tokens.set(value, name);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
const bare = (value) => value.replace(/^(?:Bearer|Token)\s+/i, "");
|
|
69
|
+
const isUrl = (value) => /^https?:\/\//i.test(value);
|
|
70
|
+
/** Every value this request knows is secret, longest first. */
|
|
71
|
+
const secretsOf = (host) => {
|
|
72
|
+
if (host.secretValues && host.secretsAt === refreshes)
|
|
73
|
+
return host.secretValues;
|
|
74
|
+
const values = new Set([...refreshedTokens.keys(), ...replacements.values()]);
|
|
75
|
+
// A connection's own URL (an instance, an account) says which call failed; a user's secret may be a URL.
|
|
76
|
+
const add = (value, urls) => {
|
|
77
|
+
if (typeof value !== "string" || value.length < MIN_TOKEN_LENGTH || AI_MARKER.test(value))
|
|
78
|
+
return;
|
|
79
|
+
if (urls || !isUrl(value))
|
|
80
|
+
values.add(value);
|
|
81
|
+
};
|
|
82
|
+
for (const answer of host.connectionAnswers?.values() ?? []) {
|
|
83
|
+
for (const [field, value] of Object.entries((answer ?? {}))) {
|
|
84
|
+
if (field !== "status" && field !== "baseUrl")
|
|
85
|
+
add(value, false);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
for (const [key, value] of Object.entries(host.env ?? {}))
|
|
89
|
+
add(value, !host.envConnections?.[key]);
|
|
90
|
+
host.secretValues = [...values].sort((a, b) => b.length - a.length);
|
|
91
|
+
host.secretsAt = refreshes;
|
|
92
|
+
return host.secretValues;
|
|
93
|
+
};
|
|
94
|
+
/** The values a request's own credential headers carry, which the log redacts too. */
|
|
95
|
+
const headerSecrets = (headers) => [...headers].flatMap(([name, value]) => {
|
|
96
|
+
const secret = bare(value);
|
|
97
|
+
return SECRET_HEADER.test(name) && secret.length >= MIN_TOKEN_LENGTH && !AI_MARKER.test(secret) ? [secret] : [];
|
|
98
|
+
});
|
|
99
|
+
/** `text` cut to `max`, every secret in it redacted, none cut in half first, however it was written. */
|
|
100
|
+
const loggable = (text, secrets, max) => {
|
|
101
|
+
const reach = secrets.reduce((longest, secret) => Math.max(longest, secretForms(secret)[0].length), 0);
|
|
102
|
+
return redactKnown(text.slice(0, max + reach), secrets).slice(0, max);
|
|
103
|
+
};
|
|
104
|
+
const decodings = (raw) => {
|
|
105
|
+
const decoded = [raw];
|
|
106
|
+
for (const text of [raw, raw.replace(/\+/g, " ")]) {
|
|
107
|
+
try {
|
|
108
|
+
decoded.push(decodeURIComponent(text));
|
|
109
|
+
}
|
|
110
|
+
catch {
|
|
111
|
+
// Not encoded as a whole; matched as written.
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return decoded;
|
|
115
|
+
};
|
|
116
|
+
/** A URL with each path segment or query value that holds a secret, however it was encoded, redacted whole. */
|
|
117
|
+
const redactedUrl = (url, secrets) => {
|
|
118
|
+
const holds = (raw) => decodings(raw).some((text) => secrets.some((secret) => text.includes(secret)));
|
|
119
|
+
const path = url.pathname
|
|
120
|
+
.split("/")
|
|
121
|
+
.map((segment) => (holds(segment) ? REDACTED : segment))
|
|
122
|
+
.join("/");
|
|
123
|
+
const query = url.search
|
|
124
|
+
? `?${url.search
|
|
125
|
+
.slice(1)
|
|
126
|
+
.split("&")
|
|
127
|
+
.map((pair) => {
|
|
128
|
+
const at = pair.indexOf("=");
|
|
129
|
+
if (at < 0)
|
|
130
|
+
return holds(pair) ? REDACTED : pair;
|
|
131
|
+
return holds(pair.slice(at + 1)) ? `${pair.slice(0, at)}=${REDACTED}` : pair;
|
|
132
|
+
})
|
|
133
|
+
.join("&")}`
|
|
134
|
+
: "";
|
|
135
|
+
return `${url.protocol}//${url.host}${path}${query}`;
|
|
136
|
+
};
|
|
137
|
+
/** A request body as classic's run log shows it. */
|
|
138
|
+
const bodyText = (body) => {
|
|
139
|
+
if (body === undefined || body === null)
|
|
140
|
+
return undefined;
|
|
141
|
+
if (typeof body === "string")
|
|
142
|
+
return body;
|
|
143
|
+
if (body instanceof URLSearchParams)
|
|
144
|
+
return body.toString();
|
|
145
|
+
if (body instanceof ReadableStream)
|
|
146
|
+
return "[stream]";
|
|
147
|
+
if (body instanceof Blob || body instanceof ArrayBuffer || ArrayBuffer.isView(body))
|
|
148
|
+
return "[binary data]";
|
|
149
|
+
if (typeof FormData !== "undefined" && body instanceof FormData)
|
|
150
|
+
return "[binary data]";
|
|
151
|
+
try {
|
|
152
|
+
return JSON.stringify(body);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return "[non-serializable body]";
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const settledWithin = (promise, ms) => new Promise((resolve) => {
|
|
159
|
+
const timer = setTimeout(resolve, ms);
|
|
160
|
+
promise.then(() => {
|
|
161
|
+
clearTimeout(timer);
|
|
162
|
+
resolve();
|
|
163
|
+
}, () => {
|
|
164
|
+
clearTimeout(timer);
|
|
165
|
+
resolve();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
/** The connection token a request carries, in a header or its query. */
|
|
169
|
+
const carried = (headers, url, tokens) => {
|
|
170
|
+
for (const [, value] of headers) {
|
|
171
|
+
const connection = tokens.get(bare(value));
|
|
172
|
+
if (connection)
|
|
173
|
+
return { token: bare(value), connection };
|
|
174
|
+
}
|
|
175
|
+
for (const [, value] of url.searchParams) {
|
|
176
|
+
const connection = tokens.get(value);
|
|
177
|
+
if (connection)
|
|
178
|
+
return { token: value, connection };
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
};
|
|
182
|
+
/** `url` with each query value equal to `from` set to `to`, every other pair left as it was written. */
|
|
183
|
+
const withQueryValue = (url, from, to) => {
|
|
184
|
+
if (!url.search)
|
|
185
|
+
return url;
|
|
186
|
+
const pairs = url.search
|
|
187
|
+
.slice(1)
|
|
188
|
+
.split("&")
|
|
189
|
+
.map((pair) => {
|
|
190
|
+
const at = pair.indexOf("=");
|
|
191
|
+
if (at < 0)
|
|
192
|
+
return pair;
|
|
193
|
+
let value;
|
|
194
|
+
try {
|
|
195
|
+
value = decodeURIComponent(pair.slice(at + 1).replace(/\+/g, " "));
|
|
196
|
+
}
|
|
197
|
+
catch {
|
|
198
|
+
return pair;
|
|
199
|
+
}
|
|
200
|
+
return value === from ? `${pair.slice(0, at)}=${encodeURIComponent(to)}` : pair;
|
|
201
|
+
});
|
|
202
|
+
const swapped = new URL(url);
|
|
203
|
+
swapped.search = `?${pairs.join("&")}`;
|
|
204
|
+
return swapped;
|
|
205
|
+
};
|
|
206
|
+
const swapHeaders = (headers, from, to) => {
|
|
207
|
+
for (const [name, value] of [...headers]) {
|
|
208
|
+
if (value.includes(from))
|
|
209
|
+
headers.set(name, value.split(from).join(to));
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
/** Where a call on Zite's AI key goes at the host, whether the app named the provider or the `baseUrl` it was given. */
|
|
213
|
+
const aiTarget = (url, headers) => {
|
|
214
|
+
const marker = [...headers].map(([, value]) => AI_MARKER.exec(bare(value))).find(Boolean);
|
|
215
|
+
if (url.hostname === AI_HOST)
|
|
216
|
+
return { target: url, connection: marker?.[1] };
|
|
217
|
+
const route = AI_PROVIDER_HOSTS[url.hostname];
|
|
218
|
+
if (!route || !marker)
|
|
219
|
+
return undefined;
|
|
220
|
+
const path = url.pathname.startsWith(route.strip) ? url.pathname.slice(route.strip.length) : url.pathname;
|
|
221
|
+
return { target: new URL(`https://${AI_HOST}/${route.provider}${path}${url.search}`), connection: marker[1] };
|
|
222
|
+
};
|
|
223
|
+
const sessionOf = async (host) => {
|
|
224
|
+
if (!host.platform)
|
|
225
|
+
return undefined;
|
|
226
|
+
host.session ??= Promise.resolve(host.platform.session());
|
|
227
|
+
return host.session;
|
|
228
|
+
};
|
|
229
|
+
/** The token to send instead of `token`, refreshed once per connection per request. */
|
|
230
|
+
const freshToken = async (host, token, connection) => {
|
|
231
|
+
const known = replacements.get(token);
|
|
232
|
+
if (known)
|
|
233
|
+
return known;
|
|
234
|
+
const refreshing = (host.refreshing ??= new Map());
|
|
235
|
+
let pending = refreshing.get(connection);
|
|
236
|
+
if (!pending) {
|
|
237
|
+
pending = (async () => {
|
|
238
|
+
const session = await sessionOf(host);
|
|
239
|
+
const result = session ? await session.refreshConnection(connection) : undefined;
|
|
240
|
+
if (!result || !("value" in result))
|
|
241
|
+
return undefined;
|
|
242
|
+
const fresh = result.value;
|
|
243
|
+
const previous = (host.connectionAnswers?.get(connection) ?? {});
|
|
244
|
+
const field = Object.keys(previous).find((key) => previous[key] === token) ?? "accessToken";
|
|
245
|
+
const value = fresh[field];
|
|
246
|
+
rememberConnection(host, connection, fresh);
|
|
247
|
+
return typeof value === "string" ? value : undefined;
|
|
248
|
+
})();
|
|
249
|
+
refreshing.set(connection, pending);
|
|
250
|
+
}
|
|
251
|
+
const value = await pending;
|
|
252
|
+
if (!value || value === token)
|
|
253
|
+
return undefined;
|
|
254
|
+
remember(replacements, token, value);
|
|
255
|
+
remember(refreshedTokens, value, connection);
|
|
256
|
+
refreshes += 1;
|
|
257
|
+
// As classic: the env vars that held the old token hold the new one.
|
|
258
|
+
const env = globalThis.process?.env;
|
|
259
|
+
for (const [key, held] of Object.entries(env ?? {})) {
|
|
260
|
+
if (held === token)
|
|
261
|
+
env[key] = value;
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
};
|
|
265
|
+
const aiUnavailable = () => Response.json({ error: { type: "unavailable", message: "Zite's AI key works only in an app Zite hosts" } }, { status: 503 });
|
|
266
|
+
const cfOf = (request) => request.cf;
|
|
267
|
+
export const createOutboundFetch = (nativeFetch) => async (input, init) => {
|
|
268
|
+
const host = getHost();
|
|
269
|
+
if (!host)
|
|
270
|
+
return nativeFetch(input, init);
|
|
271
|
+
let url;
|
|
272
|
+
try {
|
|
273
|
+
url = new URL(input instanceof Request ? input.url : input);
|
|
274
|
+
}
|
|
275
|
+
catch {
|
|
276
|
+
return nativeFetch(input, init);
|
|
277
|
+
}
|
|
278
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
|
|
279
|
+
if (!headers.has("user-agent"))
|
|
280
|
+
headers.set("user-agent", DEFAULT_USER_AGENT);
|
|
281
|
+
const method = (init?.method ?? (input instanceof Request ? input.method : "GET")).toUpperCase();
|
|
282
|
+
const ai = aiTarget(url, headers);
|
|
283
|
+
const found = ai ? undefined : carried(headers, url, tokensOf(host));
|
|
284
|
+
const startedAt = Date.now();
|
|
285
|
+
const sentBody = bodyText(init?.body);
|
|
286
|
+
const sentHeaderSecrets = headerSecrets(headers);
|
|
287
|
+
const record = (outcome) => {
|
|
288
|
+
const fetches = (host.fetches ??= []);
|
|
289
|
+
if (fetches.length >= MAX_FETCHES)
|
|
290
|
+
return undefined;
|
|
291
|
+
const connection = ai?.connection ?? found?.connection;
|
|
292
|
+
const secrets = [...sentHeaderSecrets, ...secretsOf(host)].sort((a, b) => b.length - a.length);
|
|
293
|
+
const bodyRoom = Math.min(MAX_BODY_CHARS, MAX_LOGGED_CHARS - (host.fetchLogChars ?? 0));
|
|
294
|
+
const requestBody = sentBody !== undefined && bodyRoom > 0 ? loggable(sentBody, secrets, bodyRoom) : undefined;
|
|
295
|
+
if (requestBody)
|
|
296
|
+
host.fetchLogChars = (host.fetchLogChars ?? 0) + requestBody.length;
|
|
297
|
+
const entry = {
|
|
298
|
+
at: startedAt,
|
|
299
|
+
method,
|
|
300
|
+
url: loggable(redactedUrl(url, secrets), secrets, MAX_URL_CHARS),
|
|
301
|
+
durationMs: Date.now() - startedAt,
|
|
302
|
+
...(requestBody !== undefined && { requestBody }),
|
|
303
|
+
...(outcome.status !== undefined && { status: outcome.status }),
|
|
304
|
+
...(outcome.error !== undefined && {
|
|
305
|
+
error: loggable(outcome.error instanceof Error ? outcome.error.message : String(outcome.error), secrets, MAX_ERROR_CHARS),
|
|
306
|
+
}),
|
|
307
|
+
...(connection && { connection }),
|
|
308
|
+
...(ai && { ziteAi: true }),
|
|
309
|
+
...(outcome.retried && { retried: true }),
|
|
310
|
+
};
|
|
311
|
+
fetches.push(entry);
|
|
312
|
+
return entry;
|
|
313
|
+
};
|
|
314
|
+
if (!ai && !found) {
|
|
315
|
+
try {
|
|
316
|
+
// The app's own call, `cf` and all; only the headers are ours.
|
|
317
|
+
const response = await nativeFetch(input, { ...init, headers });
|
|
318
|
+
record({ status: response.status });
|
|
319
|
+
return response;
|
|
320
|
+
}
|
|
321
|
+
catch (error) {
|
|
322
|
+
record({ error });
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// An org's own AI key, which classic reads usage from for analytics.
|
|
327
|
+
const usageProvider = found ? AI_USAGE_HOSTS[url.hostname] : undefined;
|
|
328
|
+
const usageBody = usageProvider ? withStreamUsage(url, method, init?.body) : undefined;
|
|
329
|
+
if (usageBody !== undefined)
|
|
330
|
+
headers.delete("content-length");
|
|
331
|
+
const base = new Request(input, usageBody === undefined ? init : { ...init, body: usageBody });
|
|
332
|
+
if (ai) {
|
|
333
|
+
try {
|
|
334
|
+
const session = await sessionOf(host);
|
|
335
|
+
if (!session) {
|
|
336
|
+
const refused = aiUnavailable();
|
|
337
|
+
record({ status: refused.status });
|
|
338
|
+
return refused;
|
|
339
|
+
}
|
|
340
|
+
// Whole, so it crosses RPC as bytes whatever stream the app built it from.
|
|
341
|
+
const body = base.body ? await base.arrayBuffer() : undefined;
|
|
342
|
+
const response = await session.aiFetch(new Request(ai.target, { method: base.method, headers, body, redirect: "manual" }));
|
|
343
|
+
record({ status: response.status });
|
|
344
|
+
return response;
|
|
345
|
+
}
|
|
346
|
+
catch (error) {
|
|
347
|
+
record({ error });
|
|
348
|
+
throw error;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// As classic: Node SDKs ask for gzip and inflate it themselves, which fails on the body Workers already inflated.
|
|
352
|
+
if (found)
|
|
353
|
+
headers.delete("accept-encoding");
|
|
354
|
+
const outgoing = new Request(base, { headers });
|
|
355
|
+
const retry = found ? outgoing.clone() : undefined;
|
|
356
|
+
try {
|
|
357
|
+
let response = await nativeFetch(outgoing);
|
|
358
|
+
let retried = false;
|
|
359
|
+
if (response.status === 401 && retry && found) {
|
|
360
|
+
const fresh = await freshToken(host, found.token, found.connection);
|
|
361
|
+
if (fresh) {
|
|
362
|
+
const retryHeaders = new Headers(retry.headers);
|
|
363
|
+
swapHeaders(retryHeaders, found.token, fresh);
|
|
364
|
+
await response.body?.cancel();
|
|
365
|
+
response = await nativeFetch(new Request(withQueryValue(url, found.token, fresh), {
|
|
366
|
+
method: retry.method,
|
|
367
|
+
headers: retryHeaders,
|
|
368
|
+
body: retry.body,
|
|
369
|
+
redirect: retry.redirect,
|
|
370
|
+
signal: retry.signal,
|
|
371
|
+
...(retry.body && { duplex: "half" }),
|
|
372
|
+
...(cfOf(retry) !== undefined && { cf: cfOf(retry) }),
|
|
373
|
+
}));
|
|
374
|
+
retried = true;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (!retried)
|
|
378
|
+
await retry?.body?.cancel();
|
|
379
|
+
if (usageProvider) {
|
|
380
|
+
const metered = meterAiResponse(usageProvider, response);
|
|
381
|
+
response = metered.response;
|
|
382
|
+
const entry = record({ status: response.status, retried });
|
|
383
|
+
const used = metered.usage.then((usage) => {
|
|
384
|
+
if (usage && entry)
|
|
385
|
+
entry.ai = usage;
|
|
386
|
+
});
|
|
387
|
+
(host.pending ??= []).push(settledWithin(used, USAGE_WAIT_MS));
|
|
388
|
+
return response;
|
|
389
|
+
}
|
|
390
|
+
record({ status: response.status, retried });
|
|
391
|
+
return response;
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
await retry?.body?.cancel().catch(() => { });
|
|
395
|
+
record({ error });
|
|
396
|
+
throw error;
|
|
397
|
+
}
|
|
398
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PREVIEW_CLAIM_PARAM } from "../auth/previewClaim.js";
|
|
2
|
+
export { PREVIEW_CLAIM_PARAM };
|
|
3
|
+
export declare const PREVIEW_CLAIM_HEADER = "x-zite-preview";
|
|
4
|
+
/** An editor preview's access: on the server the host hands it over, in the browser it's on the URL. */
|
|
5
|
+
export declare const previewClaim: () => string | undefined;
|
|
6
|
+
/** Every same-origin fetch from the page carries the claim: server functions and the app's own routes alike. */
|
|
7
|
+
export declare const carryPreviewClaim: (claim: string) => void;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { browserPreviewClaim, PREVIEW_CLAIM_PARAM } from "../auth/previewClaim.js";
|
|
2
|
+
import { currentHost } from "./host.js";
|
|
3
|
+
export { PREVIEW_CLAIM_PARAM };
|
|
4
|
+
// The host's name (apps/zite-host/src/previewClaim.ts).
|
|
5
|
+
export const PREVIEW_CLAIM_HEADER = "x-zite-preview";
|
|
6
|
+
/** An editor preview's access: on the server the host hands it over, in the browser it's on the URL. */
|
|
7
|
+
export const previewClaim = () => typeof window === "undefined" ? currentHost()?.previewClaim : browserPreviewClaim();
|
|
8
|
+
let carrying = false;
|
|
9
|
+
/** Every same-origin fetch from the page carries the claim: server functions and the app's own routes alike. */
|
|
10
|
+
export const carryPreviewClaim = (claim) => {
|
|
11
|
+
if (carrying || typeof window === "undefined")
|
|
12
|
+
return;
|
|
13
|
+
carrying = true;
|
|
14
|
+
const original = window.fetch.bind(window);
|
|
15
|
+
window.fetch = (input, init) => {
|
|
16
|
+
const target = new URL(input instanceof Request ? input.url : String(input), window.location.href);
|
|
17
|
+
if (target.origin !== window.location.origin)
|
|
18
|
+
return original(input, init);
|
|
19
|
+
const headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));
|
|
20
|
+
headers.set(PREVIEW_CLAIM_HEADER, claim);
|
|
21
|
+
return original(input, { ...init, headers });
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -3,6 +3,6 @@ export function descriptionProblems(description) {
|
|
|
3
3
|
return [
|
|
4
4
|
...description.functions.flatMap((f) => f.problems.map((p) => `function ${f.id}${f.file ? ` (${f.file})` : ""}: ${p}`)),
|
|
5
5
|
...description.routes.flatMap((r) => r.problems.map((p) => `route ${r.path}: ${p}`)),
|
|
6
|
-
...(description.workflows ?? []).flatMap((w) => w.problems.map((p) => `
|
|
6
|
+
...(description.workflows ?? []).flatMap((w) => w.problems.map((p) => `automation ${w.id} (${w.file}): ${p}`)),
|
|
7
7
|
];
|
|
8
8
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Server-only. A trigger registration's console stays out of the platform's
|
|
2
|
+
// logs, as classic's does: the provider can answer it with the signing secret.
|
|
3
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
4
|
+
const LEVELS = ["debug", "dir", "error", "info", "log", "table", "trace", "warn"];
|
|
5
|
+
// Keyed on globalThis so two copies of zitejs in one bundle wrap console once.
|
|
6
|
+
const stores = globalThis;
|
|
7
|
+
const QUIET_KEY = Symbol.for("zitejs:quiet-console");
|
|
8
|
+
const quietStorage = () => {
|
|
9
|
+
const held = stores[QUIET_KEY];
|
|
10
|
+
if (held)
|
|
11
|
+
return held;
|
|
12
|
+
const storage = new AsyncLocalStorage();
|
|
13
|
+
stores[QUIET_KEY] = storage;
|
|
14
|
+
const methods = console;
|
|
15
|
+
for (const level of LEVELS) {
|
|
16
|
+
const original = methods[level];
|
|
17
|
+
if (typeof original !== "function")
|
|
18
|
+
continue;
|
|
19
|
+
methods[level] = (...args) => {
|
|
20
|
+
if (!storage.getStore())
|
|
21
|
+
original.apply(console, args);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return storage;
|
|
25
|
+
};
|
|
26
|
+
/** Runs `fn` with this request's console dropped; the isolate's other requests log as before. */
|
|
27
|
+
export const withoutConsole = (fn) => quietStorage().run(true, fn);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare const REDACTED = "REDACTED";
|
|
2
|
+
/**
|
|
3
|
+
* Replace any credentials in `text` with a placeholder, leaving the rest
|
|
4
|
+
* intact. Returns the input unchanged when there is nothing to redact, so
|
|
5
|
+
* callers that pass clean text see byte-identical output.
|
|
6
|
+
*/
|
|
7
|
+
export declare function redactSecrets(text: string): string;
|
|
8
|
+
/** The ways a secret is written into text, longest first: as it is, URL-encoded in either case, form-encoded, JSON-escaped. */
|
|
9
|
+
export declare function secretForms(value: string): string[];
|
|
10
|
+
/**
|
|
11
|
+
* `text` with each of `values`, however it was written, replaced, then
|
|
12
|
+
* anything the patterns above find. `values` longest first, so none is left
|
|
13
|
+
* half-done.
|
|
14
|
+
*/
|
|
15
|
+
export declare function redactKnown(text: string, values: readonly string[]): string;
|