zitejs 0.10.0-start.21 → 0.10.0-start.23
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/client.d.ts +2 -2
- package/dist/cjs/check/start/sourceRules.js +3 -0
- package/dist/cjs/cli.js +6 -0
- package/dist/cjs/dev/index.js +15 -0
- package/dist/cjs/dev/signin.d.ts +16 -0
- package/dist/cjs/dev/signin.js +76 -0
- package/dist/cjs/internal/appMode.d.ts +11 -0
- package/dist/cjs/internal/appMode.js +4 -1
- package/dist/cjs/internal/appToken.d.ts +17 -0
- package/dist/cjs/internal/appToken.js +26 -0
- package/dist/cjs/internal/platformFetch.d.ts +2 -0
- package/dist/cjs/internal/platformFetch.js +13 -0
- package/dist/cjs/internal/serviceUrls.d.ts +7 -0
- package/dist/cjs/internal/serviceUrls.js +16 -0
- package/dist/cjs/runtime/index.d.ts +1 -1
- package/dist/cjs/start/aiUsage.d.ts +23 -0
- package/dist/cjs/start/aiUsage.js +151 -0
- package/dist/cjs/start/devApp.d.ts +1 -0
- package/dist/cjs/start/devApp.js +6 -0
- package/dist/cjs/start/entries.js +4 -0
- package/dist/cjs/start/provider.d.ts +24 -0
- package/dist/cjs/start/provider.js +159 -0
- package/dist/cjs/start/types.d.ts +302 -0
- package/dist/cjs/start/types.js +2 -0
- package/dist/cjs/sync/lib.js +23 -50
- package/dist/cjs/sync/sdkNames.d.ts +65 -7
- package/dist/cjs/sync/sdkNames.js +76 -12
- package/dist/cjs/sync/sdkNames.test.js +38 -0
- package/dist/cjs/upload/index.js +73 -22
- package/dist/cjs/upload/index.test.js +35 -2
- package/dist/esm/auth/client.d.ts +2 -2
- package/dist/esm/check/start/sourceRules.js +3 -0
- package/dist/esm/cli.js +6 -0
- package/dist/esm/dev/index.js +15 -0
- package/dist/esm/dev/signin.d.ts +16 -0
- package/dist/esm/dev/signin.js +72 -0
- package/dist/esm/internal/appMode.d.ts +11 -0
- package/dist/esm/internal/appMode.js +2 -0
- package/dist/esm/internal/appToken.d.ts +17 -0
- package/dist/esm/internal/appToken.js +22 -0
- package/dist/esm/internal/platformFetch.d.ts +2 -0
- package/dist/esm/internal/platformFetch.js +8 -0
- package/dist/esm/internal/serviceUrls.d.ts +7 -0
- package/dist/esm/internal/serviceUrls.js +12 -0
- package/dist/esm/runtime/index.d.ts +1 -1
- package/dist/esm/signin/conformance.d.ts +100 -0
- package/dist/esm/signin/conformance.js +471 -0
- package/dist/esm/signin/cookies.d.ts +28 -0
- package/dist/esm/signin/cookies.js +59 -0
- package/dist/esm/signin/encoding.d.ts +9 -0
- package/dist/esm/signin/encoding.js +39 -0
- package/dist/esm/signin/endpoints.d.ts +12 -0
- package/dist/esm/signin/endpoints.js +29 -0
- package/dist/esm/signin/http.d.ts +25 -0
- package/dist/esm/signin/http.js +69 -0
- package/dist/esm/signin/index.d.ts +8 -0
- package/dist/esm/signin/index.js +9 -0
- package/dist/esm/signin/keys.d.ts +39 -0
- package/dist/esm/signin/keys.js +80 -0
- package/dist/esm/signin/paths.d.ts +2 -0
- package/dist/esm/signin/paths.js +2 -0
- package/dist/esm/signin/seal.d.ts +8 -0
- package/dist/esm/signin/seal.js +35 -0
- package/dist/esm/signin/signin.d.ts +60 -0
- package/dist/esm/signin/signin.js +340 -0
- package/dist/esm/signin/transport.d.ts +111 -0
- package/dist/esm/signin/transport.js +1 -0
- package/dist/esm/signin/verify.d.ts +77 -0
- package/dist/esm/signin/verify.js +155 -0
- package/dist/esm/start/airtable/built.d.ts +2 -0
- package/dist/esm/start/airtable/built.js +3 -0
- package/dist/esm/start/airtable/index.d.ts +3 -5
- package/dist/esm/start/airtable/index.js +4 -25
- package/dist/esm/start/airtable/settings.d.ts +31 -0
- package/dist/esm/start/airtable/settings.js +110 -0
- package/dist/esm/start/analytics.d.ts +5 -0
- package/dist/esm/start/analytics.js +77 -0
- package/dist/esm/start/data/index.d.ts +21 -9
- package/dist/esm/start/data/index.js +26 -20
- package/dist/esm/start/data/names.d.ts +7 -0
- package/dist/esm/start/data/names.js +15 -0
- package/dist/esm/start/data/types.d.ts +4 -1
- package/dist/esm/start/devApp.d.ts +1 -0
- package/dist/esm/start/devApp.js +3 -0
- package/dist/esm/start/devSignIn.d.ts +1 -0
- package/dist/esm/start/devSignIn.js +3 -0
- package/dist/esm/start/direct.d.ts +44 -0
- package/dist/esm/start/direct.js +442 -0
- package/dist/esm/start/entries.js +4 -0
- package/dist/esm/start/fetch.d.ts +1 -0
- package/dist/esm/start/fetch.js +4 -0
- package/dist/esm/start/instance.js +6 -2
- package/dist/esm/start/localSignIn.d.ts +18 -0
- package/dist/esm/start/localSignIn.js +73 -0
- package/dist/esm/start/outbound.d.ts +9 -1
- package/dist/esm/start/outbound.js +130 -31
- package/dist/esm/start/pageHead.d.ts +36 -0
- package/dist/esm/start/pageHead.js +178 -0
- package/dist/esm/start/provider.d.ts +24 -0
- package/dist/esm/start/provider.js +150 -0
- package/dist/esm/start/router.d.ts +6 -2
- package/dist/esm/start/router.js +41 -2
- package/dist/esm/start/server.d.ts +23 -12
- package/dist/esm/start/server.js +185 -56
- package/dist/esm/start/telemetry.d.ts +13 -0
- package/dist/esm/start/telemetry.js +71 -0
- package/dist/esm/start/types.d.ts +141 -44
- package/dist/esm/start/uploads.d.ts +2 -0
- package/dist/esm/start/uploads.js +52 -0
- package/dist/esm/start/version.d.ts +1 -1
- package/dist/esm/start/version.js +1 -1
- package/dist/esm/start/vite.d.ts +18 -1
- package/dist/esm/start/vite.js +163 -4
- package/dist/esm/start/workspaceSchema.d.ts +2 -0
- package/dist/esm/start/workspaceSchema.js +3 -0
- package/dist/esm/sync/lib.js +23 -50
- package/dist/esm/sync/sdkNames.d.ts +65 -7
- package/dist/esm/sync/sdkNames.js +73 -11
- package/dist/esm/sync/sdkNames.test.js +38 -0
- package/dist/esm/upload/index.js +74 -23
- package/dist/esm/upload/index.test.js +35 -2
- package/package.json +22 -12
- package/dist/cjs/auth-server/index.d.ts +0 -369
- package/dist/cjs/auth-server/index.js +0 -119
- package/dist/esm/auth-server/index.d.ts +0 -369
- package/dist/esm/auth-server/index.js +0 -116
- package/dist/esm/start/problems.d.ts +0 -19
- package/dist/esm/start/problems.js +0 -8
|
@@ -5,7 +5,7 @@ export declare const authClient: import("better-auth/react").ReactAuthClient<{
|
|
|
5
5
|
plugins: ({
|
|
6
6
|
id: "magic-link";
|
|
7
7
|
version: string;
|
|
8
|
-
$InferServerPlugin: ReturnType<(options: import("better-auth/plugins").MagicLinkOptions) => {
|
|
8
|
+
$InferServerPlugin: ReturnType<(options: import("better-auth/plugins/magic-link").MagicLinkOptions) => {
|
|
9
9
|
id: "magic-link";
|
|
10
10
|
version: string;
|
|
11
11
|
endpoints: {
|
|
@@ -110,7 +110,7 @@ export declare const authClient: import("better-auth/react").ReactAuthClient<{
|
|
|
110
110
|
window: number;
|
|
111
111
|
max: number;
|
|
112
112
|
}[];
|
|
113
|
-
options: import("better-auth/plugins").MagicLinkOptions;
|
|
113
|
+
options: import("better-auth/plugins/magic-link").MagicLinkOptions;
|
|
114
114
|
}>;
|
|
115
115
|
} | {
|
|
116
116
|
id: "additional-fields-client";
|
|
@@ -966,6 +966,9 @@ function sourceProblems(sources, { internal = false, kind } = {}) {
|
|
|
966
966
|
if (source === "cloudflare:workers") {
|
|
967
967
|
report("cloudflare-env", "error", path, statement, "read the app's environment from process.env, not cloudflare:workers");
|
|
968
968
|
}
|
|
969
|
+
if (importsModule(source, "zitejs/signin")) {
|
|
970
|
+
report("platform-import", "error", path, statement, "zitejs/signin is the platform's sign-in client, which runs for you; send people to sign in with loginWithRedirect() from zitejs/auth, or a link to loginHref() from zitejs/start");
|
|
971
|
+
}
|
|
969
972
|
const typeOnly = values !== "all" && values.length === 0;
|
|
970
973
|
// Loading a module is fine without a request; only calling one of its server functions isn't.
|
|
971
974
|
const target = callsWorkflow ? resolveImport(path, source, paths) : undefined;
|
package/dist/cjs/cli.js
CHANGED
|
@@ -83,6 +83,12 @@ async function main() {
|
|
|
83
83
|
console.error(' check Typecheck and build every app and automation; a Start app is typechecked with tsgo against its regenerated route tree and its source checked by rule (--json for a report)');
|
|
84
84
|
console.error(' bundle Bundle endpoints (src/api/*.ts, or an automation\'s src/index.ts) for cloudflare-lambda');
|
|
85
85
|
console.error(' describe Print what a built TanStack Start app declares: functions, routes, access');
|
|
86
|
+
console.error('');
|
|
87
|
+
console.error("A Start app under `vite dev` reaches Zite's data and services with these, from the app's .env:");
|
|
88
|
+
console.error(" ZITE_DEV_API_KEY Your Zite API key, as the MCP takes it: mints dev tokens for the app zite.config.json's id names; each lasts 15 minutes and is renewed before it expires");
|
|
89
|
+
console.error(' ZITE_DEV_TOKEN A dev token to use as it is instead, never renewed');
|
|
90
|
+
console.error(' ZITE_URLS JSON naming the platform: {"baseRunner","backend","aiGateway"?,"runs"?,"email"?,"pdf"?,"uploads"?,"steps"?}; a service it doesn\'t name is the backend\'s');
|
|
91
|
+
console.error(' ZITE_DEV_USER JSON of the signed-in stand-in: {"id","email","name"?,"roles"?,"isInternal"?}; unset, nobody is');
|
|
86
92
|
process.exit(1);
|
|
87
93
|
}
|
|
88
94
|
}
|
package/dist/cjs/dev/index.js
CHANGED
|
@@ -9,6 +9,7 @@ const path_1 = require("path");
|
|
|
9
9
|
const sourceRoots_js_1 = require("../sourceRoots.js");
|
|
10
10
|
const entries_js_1 = require("../start/entries.js");
|
|
11
11
|
const index_js_1 = require("../sync/index.js");
|
|
12
|
+
const signin_js_1 = require("./signin.js");
|
|
12
13
|
const lib_js_1 = require("../sync/lib.js");
|
|
13
14
|
const rules_js_1 = require("../integrations/rules.js");
|
|
14
15
|
const debounceTimers = new Map();
|
|
@@ -240,6 +241,20 @@ async function runDev() {
|
|
|
240
241
|
regenerateAppIntegrationsIndex(app);
|
|
241
242
|
(0, entries_js_1.regenerateStartEntries)(app);
|
|
242
243
|
}
|
|
244
|
+
// Sign-in under `vite dev`: each Start app's issuer, dev client and session secret.
|
|
245
|
+
for (const app of appDirs) {
|
|
246
|
+
if (!(0, entries_js_1.isStartAppDir)(app.path))
|
|
247
|
+
continue;
|
|
248
|
+
try {
|
|
249
|
+
const result = await (0, signin_js_1.writeDevSignIn)(app.path);
|
|
250
|
+
console.log(result.written
|
|
251
|
+
? `Sign-in for ${app.path} under vite dev: ${result.clientId} at ${result.issuer}`
|
|
252
|
+
: `No sign-in for ${app.path} under vite dev: ${result.reason}`);
|
|
253
|
+
}
|
|
254
|
+
catch (err) {
|
|
255
|
+
console.warn(`Couldn't set up sign-in for ${app.path}:`, err instanceof Error ? err.message : err);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
243
258
|
// 3. Watch each dir's src/api/ for endpoint changes
|
|
244
259
|
let watchingAny = false;
|
|
245
260
|
for (const app of appDirs) {
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const DEV_ENV_FILE: string;
|
|
2
|
+
type Env = Record<string, string | undefined>;
|
|
3
|
+
export type DevSignInResult = {
|
|
4
|
+
written: true;
|
|
5
|
+
issuer: string;
|
|
6
|
+
clientId: string;
|
|
7
|
+
} | {
|
|
8
|
+
written: false;
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Writes the app's `.zite/dev.env` from its dev token's `appUrl`: issuer `<appUrl>/auth`, client `<app>:dev`. Keeps
|
|
13
|
+
* the secret a previous run wrote, so sessions outlive a restart.
|
|
14
|
+
*/
|
|
15
|
+
export declare function writeDevSignIn(appDir: string, shell?: Env): Promise<DevSignInResult>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEV_ENV_FILE = void 0;
|
|
4
|
+
exports.writeDevSignIn = writeDevSignIn;
|
|
5
|
+
// `zitejs dev`'s share of sign-in under `vite dev` (T5 design §5.9): the app's issuer and dev client, and a secret
|
|
6
|
+
// that seals its session cookie, in `.zite/dev.env`, which `zite()` hands the module.
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const node_fs_1 = require("node:fs");
|
|
9
|
+
const node_path_1 = require("node:path");
|
|
10
|
+
const dotenv_1 = require("dotenv");
|
|
11
|
+
const appToken_js_1 = require("../internal/appToken.js");
|
|
12
|
+
const provider_js_1 = require("../start/provider.js");
|
|
13
|
+
exports.DEV_ENV_FILE = (0, node_path_1.join)(".zite", "dev.env");
|
|
14
|
+
const readEnvFile = (path) => {
|
|
15
|
+
try {
|
|
16
|
+
return (0, dotenv_1.parse)((0, node_fs_1.readFileSync)(path, "utf-8"));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/** The app's env as `vite dev` reads it: its `.env`, then `.env.local`, over the shell's. */
|
|
23
|
+
const appEnvOf = (appDir, shell) => ({
|
|
24
|
+
...readEnvFile((0, node_path_1.join)(appDir, ".env")),
|
|
25
|
+
...readEnvFile((0, node_path_1.join)(appDir, ".env.local")),
|
|
26
|
+
...shell,
|
|
27
|
+
});
|
|
28
|
+
const appIdOf = (appDir) => {
|
|
29
|
+
try {
|
|
30
|
+
const { id } = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(appDir, "zite.config.json"), "utf-8"));
|
|
31
|
+
return typeof id === "string" && id ? id : undefined;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
// `.zite/` is otherwise committed, generated code; the secret must never be.
|
|
38
|
+
const ignoreDevEnv = (appDir) => {
|
|
39
|
+
const path = (0, node_path_1.join)(appDir, ".zite", ".gitignore");
|
|
40
|
+
const current = (0, node_fs_1.existsSync)(path) ? (0, node_fs_1.readFileSync)(path, "utf-8") : "";
|
|
41
|
+
if (current.split(/\r?\n/).includes("dev.env"))
|
|
42
|
+
return;
|
|
43
|
+
(0, node_fs_1.writeFileSync)(path, `${current}${current && !current.endsWith("\n") ? "\n" : ""}dev.env\n`);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Writes the app's `.zite/dev.env` from its dev token's `appUrl`: issuer `<appUrl>/auth`, client `<app>:dev`. Keeps
|
|
47
|
+
* the secret a previous run wrote, so sessions outlive a restart.
|
|
48
|
+
*/
|
|
49
|
+
async function writeDevSignIn(appDir, shell = process.env) {
|
|
50
|
+
const env = appEnvOf(appDir, shell);
|
|
51
|
+
const appId = appIdOf(appDir);
|
|
52
|
+
if (!appId)
|
|
53
|
+
return { written: false, reason: "zite.config.json names no app id" };
|
|
54
|
+
const apiKey = env[provider_js_1.LOCAL_ENV.apiKey];
|
|
55
|
+
const given = env[provider_js_1.LOCAL_ENV.token];
|
|
56
|
+
if (!given && !apiKey)
|
|
57
|
+
return { written: false, reason: `neither ${provider_js_1.LOCAL_ENV.token} nor ${provider_js_1.LOCAL_ENV.apiKey} is set` };
|
|
58
|
+
const token = given ?? (await (0, provider_js_1.mintDevToken)((0, provider_js_1.localUrls)(env[provider_js_1.LOCAL_ENV.urls]).backend, appId, apiKey));
|
|
59
|
+
const appUrl = (0, appToken_js_1.appTokenClaims)(token)?.appUrl;
|
|
60
|
+
if (!appUrl)
|
|
61
|
+
return { written: false, reason: "the app has no live URL yet; publish it once to sign in under vite dev" };
|
|
62
|
+
const issuer = `${new URL(appUrl).origin}/auth`;
|
|
63
|
+
const clientId = `${appId}:dev`;
|
|
64
|
+
const path = (0, node_path_1.join)(appDir, exports.DEV_ENV_FILE);
|
|
65
|
+
const previous = readEnvFile(path).ZITE_AUTH_SECRET;
|
|
66
|
+
const secret = previous && previous.length >= 32 ? previous : (0, node_crypto_1.randomBytes)(32).toString("base64url");
|
|
67
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.join)(appDir, ".zite"), { recursive: true });
|
|
68
|
+
ignoreDevEnv(appDir);
|
|
69
|
+
(0, node_fs_1.writeFileSync)(path, [
|
|
70
|
+
"# Written by `zitejs dev` for sign-in under `vite dev`. The secret seals your session cookie: never commit it.",
|
|
71
|
+
`ZITE_SIGNIN='${JSON.stringify({ issuer, clientId })}'`,
|
|
72
|
+
`ZITE_AUTH_SECRET=${secret}`,
|
|
73
|
+
"",
|
|
74
|
+
].join("\n"), { mode: 0o600 });
|
|
75
|
+
return { written: true, issuer, clientId };
|
|
76
|
+
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/** As Start's `PlatformProvider`: the request's app token, and where the backend and uploads are. */
|
|
2
|
+
type Provider = {
|
|
3
|
+
token(): Promise<string | undefined>;
|
|
4
|
+
urls?: {
|
|
5
|
+
backend: string;
|
|
6
|
+
uploads?: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
1
9
|
export declare const keepRenderedMode: (mode: string | undefined) => void;
|
|
2
10
|
/** The mode the platform served this request or page in; undefined outside a Start app. */
|
|
3
11
|
export declare const platformMode: () => string | undefined;
|
|
12
|
+
/** The request's way to the platform on its app token; undefined in the browser and outside a Start request. */
|
|
13
|
+
export declare const platformProvider: () => Provider | undefined;
|
|
14
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.platformMode = exports.keepRenderedMode = void 0;
|
|
3
|
+
exports.platformProvider = exports.platformMode = exports.keepRenderedMode = void 0;
|
|
4
4
|
// Classic's upload client reads this too, so it imports nothing of Start: the
|
|
5
5
|
// request's host comes through the key src/start/host.ts reads it under.
|
|
6
6
|
const HOST_STORE_KEY = Symbol.for("zitejs:host-context");
|
|
@@ -13,3 +13,6 @@ exports.keepRenderedMode = keepRenderedMode;
|
|
|
13
13
|
/** The mode the platform served this request or page in; undefined outside a Start app. */
|
|
14
14
|
const platformMode = () => globalThis[HOST_STORE_KEY]?.getStore()?.mode ?? globalThis[RENDERED_MODE];
|
|
15
15
|
exports.platformMode = platformMode;
|
|
16
|
+
/** The request's way to the platform on its app token; undefined in the browser and outside a Start request. */
|
|
17
|
+
const platformProvider = () => globalThis[HOST_STORE_KEY]?.getStore()?.provider;
|
|
18
|
+
exports.platformProvider = platformProvider;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** The claims zitejs reads from its own app token. It never verifies one: the platform does, and zitejs only reads what it was handed. */
|
|
2
|
+
export type AppTokenClaims = {
|
|
3
|
+
/** Seconds since the epoch. */
|
|
4
|
+
exp: number;
|
|
5
|
+
app?: string;
|
|
6
|
+
/** The build it was minted for; a dev token's is the app's newest. */
|
|
7
|
+
build?: number;
|
|
8
|
+
base?: string | null;
|
|
9
|
+
mode?: string;
|
|
10
|
+
/** The org's settings version the token was minted under. */
|
|
11
|
+
ver?: number | null;
|
|
12
|
+
/** Whether the org's plan shows Zite's badge. */
|
|
13
|
+
branding?: boolean;
|
|
14
|
+
appUrl?: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** The token's payload, or undefined for anything that isn't one. */
|
|
17
|
+
export declare const appTokenClaims: (token: string) => AppTokenClaims | undefined;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Isomorphic: the upload client reads the app id from it too.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.appTokenClaims = void 0;
|
|
5
|
+
const PREFIX = "zite_app_v1.";
|
|
6
|
+
const decodeSegment = (segment) => {
|
|
7
|
+
const base64 = segment.replace(/-/g, "+").replace(/_/g, "/");
|
|
8
|
+
const bytes = Uint8Array.from(atob(base64 + "=".repeat((4 - (base64.length % 4)) % 4)), (c) => c.charCodeAt(0));
|
|
9
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
10
|
+
};
|
|
11
|
+
/** The token's payload, or undefined for anything that isn't one. */
|
|
12
|
+
const appTokenClaims = (token) => {
|
|
13
|
+
if (!token.startsWith(PREFIX))
|
|
14
|
+
return undefined;
|
|
15
|
+
const payload = token.slice(PREFIX.length).split(".")[1];
|
|
16
|
+
if (!payload)
|
|
17
|
+
return undefined;
|
|
18
|
+
try {
|
|
19
|
+
const claims = decodeSegment(payload);
|
|
20
|
+
return claims && typeof claims.exp === "number" ? claims : undefined;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
exports.appTokenClaims = appTokenClaims;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.platformFetch = exports.isPlatformRequest = void 0;
|
|
4
|
+
// The platform's own requests on the app token. The outbound fetch passes them through unlogged, so the token never
|
|
5
|
+
// reaches the run log. On globalThis, as that wrapper is, for two copies of zitejs in one bundle.
|
|
6
|
+
const PLATFORM_REQUESTS = Symbol.for("zitejs:platform-requests");
|
|
7
|
+
const isPlatformRequest = (input) => input instanceof Request && (globalThis[PLATFORM_REQUESTS]?.has(input) ?? false);
|
|
8
|
+
exports.isPlatformRequest = isPlatformRequest;
|
|
9
|
+
const platformFetch = (request) => {
|
|
10
|
+
(globalThis[PLATFORM_REQUESTS] ??= new WeakSet()).add(request);
|
|
11
|
+
return fetch(request);
|
|
12
|
+
};
|
|
13
|
+
exports.platformFetch = platformFetch;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type ServiceName = "email" | "pdf" | "uploads" | "steps";
|
|
2
|
+
/** Where a service answers this app on its token: the URL the platform named for it, else the backend's route. */
|
|
3
|
+
export declare const serviceUrl: (urls: {
|
|
4
|
+
backend: string;
|
|
5
|
+
} & Partial<Record<ServiceName, string>>, name: ServiceName) => string;
|
|
6
|
+
/** A Start page's upload session, which the app's own server asks the platform for on its token. */
|
|
7
|
+
export declare const UPLOAD_SESSION_PATH = "/__zite/uploads/session";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Isomorphic, and nothing of Start: classic's upload client reads it too.
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.UPLOAD_SESSION_PATH = exports.serviceUrl = void 0;
|
|
5
|
+
// What answered each before the platform named their URLs, which a host from then still relies on.
|
|
6
|
+
const BACKEND_ROUTES = {
|
|
7
|
+
email: "/v1/start/app/email",
|
|
8
|
+
pdf: "/v1/start/app/pdf",
|
|
9
|
+
uploads: "/v1/start/app/uploads/session",
|
|
10
|
+
steps: "/v1/start/app/runs/steps",
|
|
11
|
+
};
|
|
12
|
+
/** Where a service answers this app on its token: the URL the platform named for it, else the backend's route. */
|
|
13
|
+
const serviceUrl = (urls, name) => urls[name] ?? `${urls.backend}${BACKEND_ROUTES[name]}`;
|
|
14
|
+
exports.serviceUrl = serviceUrl;
|
|
15
|
+
/** A Start page's upload session, which the app's own server asks the platform for on its token. */
|
|
16
|
+
exports.UPLOAD_SESSION_PATH = "/__zite/uploads/session";
|
|
@@ -183,7 +183,7 @@ export declare function createSqlClient(): (params: {
|
|
|
183
183
|
query: string;
|
|
184
184
|
params?: unknown[];
|
|
185
185
|
}) => Promise<SqlResult>;
|
|
186
|
-
/** Must agree with `User` in `zitejs/auth
|
|
186
|
+
/** Must agree with `User` in `zitejs/auth`; the two used to disagree on whether the name fields were nullable or optional. */
|
|
187
187
|
export interface AuthUser {
|
|
188
188
|
id: string;
|
|
189
189
|
name: string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a call on an org's own AI key used, read from the provider's answer as
|
|
3
|
+
* classic's runtime reads it. For analytics only: the org pays the provider.
|
|
4
|
+
* OpenAI and Anthropic; the host meters Zite's own key itself.
|
|
5
|
+
*/
|
|
6
|
+
export type AiProvider = "openai" | "anthropic";
|
|
7
|
+
export type AiUsageRecord = {
|
|
8
|
+
provider: AiProvider;
|
|
9
|
+
model?: string;
|
|
10
|
+
inputTokens?: number;
|
|
11
|
+
outputTokens?: number;
|
|
12
|
+
cachedReadInputTokens?: number;
|
|
13
|
+
cacheCreationInputTokens?: number;
|
|
14
|
+
isStreaming?: true;
|
|
15
|
+
};
|
|
16
|
+
export declare const AI_USAGE_HOSTS: Record<string, AiProvider>;
|
|
17
|
+
/** As classic: an OpenAI chat stream reports its usage only when asked to. The body to send instead, if any. */
|
|
18
|
+
export declare const withStreamUsage: (url: URL, method: string, body: unknown) => string | undefined;
|
|
19
|
+
/** The response to hand the app, and its usage once read; a stream is split so the app reads one copy as it arrives. */
|
|
20
|
+
export declare const meterAiResponse: (provider: AiProvider, response: Response) => {
|
|
21
|
+
response: Response;
|
|
22
|
+
usage: Promise<AiUsageRecord | undefined>;
|
|
23
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* What a call on an org's own AI key used, read from the provider's answer as
|
|
4
|
+
* classic's runtime reads it. For analytics only: the org pays the provider.
|
|
5
|
+
* OpenAI and Anthropic; the host meters Zite's own key itself.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.meterAiResponse = exports.withStreamUsage = exports.AI_USAGE_HOSTS = void 0;
|
|
9
|
+
exports.AI_USAGE_HOSTS = {
|
|
10
|
+
"api.openai.com": "openai",
|
|
11
|
+
"api.anthropic.com": "anthropic",
|
|
12
|
+
};
|
|
13
|
+
const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
14
|
+
const isNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
15
|
+
/** As classic: an OpenAI chat stream reports its usage only when asked to. The body to send instead, if any. */
|
|
16
|
+
const withStreamUsage = (url, method, body) => {
|
|
17
|
+
if (method !== "POST" || typeof body !== "string" || !url.pathname.endsWith("/chat/completions"))
|
|
18
|
+
return undefined;
|
|
19
|
+
try {
|
|
20
|
+
const json = JSON.parse(body);
|
|
21
|
+
if (!isRecord(json) || json.stream !== true)
|
|
22
|
+
return undefined;
|
|
23
|
+
const options = isRecord(json.stream_options) ? json.stream_options : {};
|
|
24
|
+
if (options.include_usage === true)
|
|
25
|
+
return undefined;
|
|
26
|
+
return JSON.stringify({ ...json, stream_options: { ...options, include_usage: true } });
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
exports.withStreamUsage = withStreamUsage;
|
|
33
|
+
// OpenAI counts cached tokens inside its input; classic takes them apart.
|
|
34
|
+
const tokensFromJson = (provider, body) => {
|
|
35
|
+
const tokens = {};
|
|
36
|
+
if (!isRecord(body))
|
|
37
|
+
return tokens;
|
|
38
|
+
if (provider === "openai") {
|
|
39
|
+
const source = isRecord(body.response) ? body.response : body;
|
|
40
|
+
if (typeof source.model === "string")
|
|
41
|
+
tokens.model = source.model;
|
|
42
|
+
const usage = isRecord(source.usage) ? source.usage : undefined;
|
|
43
|
+
if (!usage)
|
|
44
|
+
return tokens;
|
|
45
|
+
const details = (name) => (isRecord(usage[name]) ? usage[name] : {});
|
|
46
|
+
const cached = details("prompt_tokens_details").cached_tokens ?? details("input_tokens_details").cached_tokens;
|
|
47
|
+
const input = usage.prompt_tokens ?? usage.input_tokens;
|
|
48
|
+
const output = usage.completion_tokens ?? usage.output_tokens;
|
|
49
|
+
if (isNumber(input))
|
|
50
|
+
tokens.inputTokens = input - (isNumber(cached) ? cached : 0);
|
|
51
|
+
if (isNumber(output))
|
|
52
|
+
tokens.outputTokens = output;
|
|
53
|
+
if (isNumber(cached))
|
|
54
|
+
tokens.cachedReadInputTokens = cached;
|
|
55
|
+
return tokens;
|
|
56
|
+
}
|
|
57
|
+
// A stream splits these: message_start has the input, message_delta the running output.
|
|
58
|
+
const message = isRecord(body.message) ? body.message : body;
|
|
59
|
+
if (typeof message.model === "string")
|
|
60
|
+
tokens.model = message.model;
|
|
61
|
+
const usage = isRecord(message.usage) ? message.usage : isRecord(body.usage) ? body.usage : undefined;
|
|
62
|
+
if (!usage)
|
|
63
|
+
return tokens;
|
|
64
|
+
if (isNumber(usage.input_tokens))
|
|
65
|
+
tokens.inputTokens = usage.input_tokens;
|
|
66
|
+
if (isNumber(usage.output_tokens))
|
|
67
|
+
tokens.outputTokens = usage.output_tokens;
|
|
68
|
+
if (isNumber(usage.cache_read_input_tokens))
|
|
69
|
+
tokens.cachedReadInputTokens = usage.cache_read_input_tokens;
|
|
70
|
+
if (isNumber(usage.cache_creation_input_tokens))
|
|
71
|
+
tokens.cacheCreationInputTokens = usage.cache_creation_input_tokens;
|
|
72
|
+
return tokens;
|
|
73
|
+
};
|
|
74
|
+
// Later readings win: Anthropic's output is cumulative, OpenAI's usage comes last.
|
|
75
|
+
const merge = (into, next) => ({
|
|
76
|
+
model: next.model ?? into.model,
|
|
77
|
+
inputTokens: next.inputTokens ?? into.inputTokens,
|
|
78
|
+
outputTokens: next.outputTokens ?? into.outputTokens,
|
|
79
|
+
cachedReadInputTokens: next.cachedReadInputTokens ?? into.cachedReadInputTokens,
|
|
80
|
+
cacheCreationInputTokens: next.cacheCreationInputTokens ?? into.cacheCreationInputTokens,
|
|
81
|
+
});
|
|
82
|
+
const MAX_PENDING_LINE = 1024 * 1024;
|
|
83
|
+
const tokensFromStream = async (provider, stream) => {
|
|
84
|
+
const reader = stream.getReader();
|
|
85
|
+
const decoder = new TextDecoder();
|
|
86
|
+
let pending = "";
|
|
87
|
+
let tokens = {};
|
|
88
|
+
const readLine = (line) => {
|
|
89
|
+
if (!line.startsWith("data:"))
|
|
90
|
+
return;
|
|
91
|
+
const payload = line.slice(5).trim();
|
|
92
|
+
if (!payload || payload === "[DONE]")
|
|
93
|
+
return;
|
|
94
|
+
try {
|
|
95
|
+
tokens = merge(tokens, tokensFromJson(provider, JSON.parse(payload)));
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
// Not JSON; nothing to count.
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
for (;;) {
|
|
103
|
+
const { done, value } = await reader.read();
|
|
104
|
+
if (done)
|
|
105
|
+
break;
|
|
106
|
+
pending += decoder.decode(value, { stream: true });
|
|
107
|
+
const lines = pending.split("\n");
|
|
108
|
+
pending = lines.pop() ?? "";
|
|
109
|
+
if (pending.length > MAX_PENDING_LINE)
|
|
110
|
+
pending = "";
|
|
111
|
+
lines.forEach(readLine);
|
|
112
|
+
}
|
|
113
|
+
readLine(pending);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// What was read before the stream broke still counts.
|
|
117
|
+
}
|
|
118
|
+
return tokens;
|
|
119
|
+
};
|
|
120
|
+
const counted = (tokens) => tokens.inputTokens !== undefined || tokens.outputTokens !== undefined;
|
|
121
|
+
const usageOf = (provider, tokens, streaming) => counted(tokens)
|
|
122
|
+
? {
|
|
123
|
+
provider,
|
|
124
|
+
...Object.fromEntries(Object.entries(tokens).filter(([, value]) => value !== undefined)),
|
|
125
|
+
...(streaming && { isStreaming: true }),
|
|
126
|
+
}
|
|
127
|
+
: undefined;
|
|
128
|
+
/** The response to hand the app, and its usage once read; a stream is split so the app reads one copy as it arrives. */
|
|
129
|
+
const meterAiResponse = (provider, response) => {
|
|
130
|
+
const type = response.headers.get("content-type") ?? "";
|
|
131
|
+
if (!response.ok || !response.body)
|
|
132
|
+
return { response, usage: Promise.resolve(undefined) };
|
|
133
|
+
if (type.includes("text/event-stream")) {
|
|
134
|
+
const [forApp, forUs] = response.body.tee();
|
|
135
|
+
return {
|
|
136
|
+
response: new Response(forApp, response),
|
|
137
|
+
usage: tokensFromStream(provider, forUs).then((tokens) => usageOf(provider, tokens, true)),
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
if (!type.includes("json"))
|
|
141
|
+
return { response, usage: Promise.resolve(undefined) };
|
|
142
|
+
return {
|
|
143
|
+
response,
|
|
144
|
+
usage: response
|
|
145
|
+
.clone()
|
|
146
|
+
.json()
|
|
147
|
+
.then((json) => usageOf(provider, tokensFromJson(provider, json), false))
|
|
148
|
+
.catch(() => undefined),
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
exports.meterAiResponse = meterAiResponse;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const devAppId: string | null;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.devAppId = void 0;
|
|
4
|
+
// Null as published. Under `vite dev`, `zite()` replaces this module with the app's id from its zite.config.json: the
|
|
5
|
+
// app the `local` provider mints dev tokens for.
|
|
6
|
+
exports.devAppId = null;
|
|
@@ -16,6 +16,8 @@ const APP_START_FILES = ["src/start.ts", "src/start.tsx"];
|
|
|
16
16
|
*/
|
|
17
17
|
const renderStartServerEntry = (target = "cloudflare") => target === "cloudflare"
|
|
18
18
|
? `// Generated by zitejs for TanStack Start apps. The platform owns this file.
|
|
19
|
+
// First, so an SDK client an app module builds as it loads gets zitejs's fetch.
|
|
20
|
+
import 'zitejs/start/fetch';
|
|
19
21
|
import handler from '@tanstack/react-start/server-entry';
|
|
20
22
|
import { createZiteWorker } from 'zitejs/start/worker';
|
|
21
23
|
import { getRouter } from '../src/router';
|
|
@@ -24,6 +26,8 @@ import { startInstance } from './start';
|
|
|
24
26
|
export default createZiteWorker({ handler, getRouter, startInstance });
|
|
25
27
|
`
|
|
26
28
|
: `// Generated by zitejs for TanStack Start apps. The platform owns this file.
|
|
29
|
+
// First, so an SDK client an app module builds as it loads gets zitejs's fetch.
|
|
30
|
+
import 'zitejs/start/fetch';
|
|
27
31
|
import handler, { createServerEntry } from '@tanstack/react-start/server-entry';
|
|
28
32
|
import { createZiteFetch } from 'zitejs/start/server';
|
|
29
33
|
import { getRouter } from '../src/router';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type AppTokenClaims } from "../internal/appToken.js";
|
|
2
|
+
import type { HostMode, HostProps, PlatformProvider, PlatformUrls } from "./types.js";
|
|
3
|
+
export declare const ziteProvider: (props: HostProps) => PlatformProvider;
|
|
4
|
+
/** What `local` reads, documented in `zitejs`'s help. */
|
|
5
|
+
export declare const LOCAL_ENV: {
|
|
6
|
+
readonly token: "ZITE_DEV_TOKEN";
|
|
7
|
+
readonly apiKey: "ZITE_DEV_API_KEY";
|
|
8
|
+
readonly urls: "ZITE_URLS";
|
|
9
|
+
readonly user: "ZITE_DEV_USER";
|
|
10
|
+
};
|
|
11
|
+
type Env = Record<string, string | undefined>;
|
|
12
|
+
/** `ZITE_URLS`, or a throw saying what it must name. */
|
|
13
|
+
export declare const localUrls: (raw: string | undefined) => PlatformUrls;
|
|
14
|
+
/** The mode a dev token names; preview for one it doesn't. */
|
|
15
|
+
export declare const localModeOf: (claims: AppTokenClaims | undefined) => HostMode;
|
|
16
|
+
/** For tests: forget the minted dev token. */
|
|
17
|
+
export declare const resetDevToken: () => void;
|
|
18
|
+
export declare const mintDevToken: (backend: string, appId: string, apiKey: string) => Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* `local` when the developer set a dev token, or a key to mint them with, else undefined. Throws on env it can't read,
|
|
21
|
+
* so a typo fails loudly rather than signing nobody in.
|
|
22
|
+
*/
|
|
23
|
+
export declare const localPlatform: (env?: Env, appId?: string | null) => PlatformProvider | undefined;
|
|
24
|
+
export {};
|