dsh-coding-subscription-oauth 0.5.0
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/CHANGELOG.md +136 -0
- package/CONTRIBUTING.md +109 -0
- package/INSTALL.md +220 -0
- package/LICENSE +19 -0
- package/NOTICE +11 -0
- package/README.de.md +254 -0
- package/README.es.md +254 -0
- package/README.fr.md +254 -0
- package/README.ja.md +254 -0
- package/README.ko.md +254 -0
- package/README.md +286 -0
- package/README.pt-BR.md +254 -0
- package/README.ru.md +254 -0
- package/README.zh-CN.md +271 -0
- package/cordis.patch.yml +13 -0
- package/docs/00-project-rules.md +195 -0
- package/docs/02-architecture.md +132 -0
- package/docs/02-architecture.zh-CN.md +128 -0
- package/lib/adapter.d.ts +24 -0
- package/lib/adapter.d.ts.map +1 -0
- package/lib/alias-adapter.d.ts +43 -0
- package/lib/alias-adapter.d.ts.map +1 -0
- package/lib/auth-routes.d.ts +155 -0
- package/lib/auth-routes.d.ts.map +1 -0
- package/lib/auth.d.ts +29 -0
- package/lib/auth.d.ts.map +1 -0
- package/lib/bin.d.ts +6 -0
- package/lib/bin.d.ts.map +1 -0
- package/lib/bin.js +27591 -0
- package/lib/bin.js.map +7 -0
- package/lib/capability-routes.d.ts +43 -0
- package/lib/capability-routes.d.ts.map +1 -0
- package/lib/capability-runtime.d.ts +56 -0
- package/lib/capability-runtime.d.ts.map +1 -0
- package/lib/capability-settings.d.ts +263 -0
- package/lib/capability-settings.d.ts.map +1 -0
- package/lib/capability-tools.d.ts +50 -0
- package/lib/capability-tools.d.ts.map +1 -0
- package/lib/catalog.d.ts +53 -0
- package/lib/catalog.d.ts.map +1 -0
- package/lib/client.js +3 -0
- package/lib/client.js.map +7 -0
- package/lib/codex-http.d.ts +77 -0
- package/lib/codex-http.d.ts.map +1 -0
- package/lib/codex-images.d.ts +109 -0
- package/lib/codex-images.d.ts.map +1 -0
- package/lib/codex-model-capabilities.d.ts +112 -0
- package/lib/codex-model-capabilities.d.ts.map +1 -0
- package/lib/codex-search.d.ts +96 -0
- package/lib/codex-search.d.ts.map +1 -0
- package/lib/codex-usage.d.ts +79 -0
- package/lib/codex-usage.d.ts.map +1 -0
- package/lib/gateway-anthropic-messages.d.ts +8 -0
- package/lib/gateway-anthropic-messages.d.ts.map +1 -0
- package/lib/gateway-auth.d.ts +22 -0
- package/lib/gateway-auth.d.ts.map +1 -0
- package/lib/gateway-backend.d.ts +62 -0
- package/lib/gateway-backend.d.ts.map +1 -0
- package/lib/gateway-body.d.ts +9 -0
- package/lib/gateway-body.d.ts.map +1 -0
- package/lib/gateway-config.d.ts +24 -0
- package/lib/gateway-config.d.ts.map +1 -0
- package/lib/gateway-http.d.ts +16 -0
- package/lib/gateway-http.d.ts.map +1 -0
- package/lib/gateway-openai-chat.d.ts +8 -0
- package/lib/gateway-openai-chat.d.ts.map +1 -0
- package/lib/gateway-openai-responses.d.ts +8 -0
- package/lib/gateway-openai-responses.d.ts.map +1 -0
- package/lib/gateway-parse.d.ts +10 -0
- package/lib/gateway-parse.d.ts.map +1 -0
- package/lib/gateway-protocol.d.ts +47 -0
- package/lib/gateway-protocol.d.ts.map +1 -0
- package/lib/gateway-routes.d.ts +21 -0
- package/lib/gateway-routes.d.ts.map +1 -0
- package/lib/gateway.d.ts +48 -0
- package/lib/gateway.d.ts.map +1 -0
- package/lib/grok-imagine.d.ts +271 -0
- package/lib/grok-imagine.d.ts.map +1 -0
- package/lib/grok-import.d.ts +21 -0
- package/lib/grok-import.d.ts.map +1 -0
- package/lib/http-json.d.ts +10 -0
- package/lib/http-json.d.ts.map +1 -0
- package/lib/ids.d.ts +33 -0
- package/lib/ids.d.ts.map +1 -0
- package/lib/imagine-routes.d.ts +59 -0
- package/lib/imagine-routes.d.ts.map +1 -0
- package/lib/index.d.ts +69 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +35355 -0
- package/lib/index.js.map +7 -0
- package/lib/invariant.d.ts +9 -0
- package/lib/invariant.d.ts.map +1 -0
- package/lib/invariant.js +14 -0
- package/lib/invariant.js.map +7 -0
- package/lib/kimi-errors.d.ts +13 -0
- package/lib/kimi-errors.d.ts.map +1 -0
- package/lib/media-store.d.ts +130 -0
- package/lib/media-store.d.ts.map +1 -0
- package/lib/oauth-import-routes.d.ts +52 -0
- package/lib/oauth-import-routes.d.ts.map +1 -0
- package/lib/oauth-providers.d.ts +26 -0
- package/lib/oauth-providers.d.ts.map +1 -0
- package/lib/oauth-session.d.ts +40 -0
- package/lib/oauth-session.d.ts.map +1 -0
- package/lib/oauth-sources.d.ts +205 -0
- package/lib/oauth-sources.d.ts.map +1 -0
- package/lib/oauth.d.ts +79 -0
- package/lib/oauth.d.ts.map +1 -0
- package/lib/provider.d.ts +38 -0
- package/lib/provider.d.ts.map +1 -0
- package/lib/proxy.d.ts +17 -0
- package/lib/proxy.d.ts.map +1 -0
- package/lib/redact.d.ts +5 -0
- package/lib/redact.d.ts.map +1 -0
- package/lib/session.d.ts +40 -0
- package/lib/session.d.ts.map +1 -0
- package/lib/store.d.ts +46 -0
- package/lib/store.d.ts.map +1 -0
- package/lib/web-origin.d.ts +10 -0
- package/lib/web-origin.d.ts.map +1 -0
- package/lib/web-routes.d.ts +20 -0
- package/lib/web-routes.d.ts.map +1 -0
- package/package.json +185 -0
- package/patches/dsh-agy@0.1.2.patch +25 -0
- package/scripts/release.mjs +166 -0
- package/scripts/smoke-deployed-routes.mjs +146 -0
- package/scripts/verify-deployed-catalog.mjs +87 -0
- package/src/adapter.ts +282 -0
- package/src/alias-adapter.ts +152 -0
- package/src/auth-routes.ts +871 -0
- package/src/auth.ts +67 -0
- package/src/bin.ts +350 -0
- package/src/capability-routes.ts +275 -0
- package/src/capability-runtime.ts +313 -0
- package/src/capability-settings.ts +657 -0
- package/src/capability-tools.ts +666 -0
- package/src/catalog.ts +271 -0
- package/src/client/GrokBuildSettings.tsx +2221 -0
- package/src/client/index.tsx +37 -0
- package/src/client/locales.ts +421 -0
- package/src/codex-http.ts +447 -0
- package/src/codex-images.ts +485 -0
- package/src/codex-model-capabilities.ts +320 -0
- package/src/codex-search.ts +245 -0
- package/src/codex-usage.ts +263 -0
- package/src/gateway-anthropic-messages.ts +84 -0
- package/src/gateway-auth.ts +100 -0
- package/src/gateway-backend.ts +274 -0
- package/src/gateway-body.ts +49 -0
- package/src/gateway-config.ts +76 -0
- package/src/gateway-http.ts +104 -0
- package/src/gateway-openai-chat.ts +124 -0
- package/src/gateway-openai-responses.ts +53 -0
- package/src/gateway-parse.ts +224 -0
- package/src/gateway-protocol.ts +52 -0
- package/src/gateway-routes.ts +152 -0
- package/src/gateway.ts +242 -0
- package/src/grok-imagine.ts +1627 -0
- package/src/grok-import.ts +151 -0
- package/src/http-json.ts +82 -0
- package/src/ids.ts +45 -0
- package/src/imagine-routes.ts +461 -0
- package/src/index.ts +598 -0
- package/src/invariant.ts +17 -0
- package/src/kimi-errors.ts +26 -0
- package/src/media-store.ts +927 -0
- package/src/oauth-import-routes.ts +314 -0
- package/src/oauth-providers.ts +152 -0
- package/src/oauth-session.ts +183 -0
- package/src/oauth-sources.ts +1104 -0
- package/src/oauth.ts +620 -0
- package/src/provider.ts +128 -0
- package/src/proxy.ts +99 -0
- package/src/redact.ts +72 -0
- package/src/session.ts +218 -0
- package/src/store.ts +217 -0
- package/src/web-origin.ts +60 -0
- package/src/web-routes.ts +75 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Same-origin GET routes for generated Imagine images and video artifacts.
|
|
3
|
+
* The Host webServer only accepts exact paths, so this registrar keeps a
|
|
4
|
+
* bounded table of opaque ids and registers one exact disposer per remembered
|
|
5
|
+
* image ref or media artifact.
|
|
6
|
+
* @module dsh-coding-subscription-oauth/imagine-routes
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
10
|
+
import type { ImageAttachmentRef } from "@deepseek-ai/dsh-attachment";
|
|
11
|
+
import {
|
|
12
|
+
DEFAULT_IMAGE_DOWNLOAD_MAX_BYTES,
|
|
13
|
+
type ImagineImageAttachmentRef,
|
|
14
|
+
type ImagineImageMediaType,
|
|
15
|
+
imagineImageDownloadHeaders,
|
|
16
|
+
imagineImagePath,
|
|
17
|
+
isSafeImagineAttachmentId,
|
|
18
|
+
parseImagineImagePath,
|
|
19
|
+
} from "./grok-imagine.ts";
|
|
20
|
+
import {
|
|
21
|
+
createTrustedImagineAuthz,
|
|
22
|
+
imagineMediaPath,
|
|
23
|
+
isSafeMediaArtifactId,
|
|
24
|
+
MEDIA_STORE_MAX_BYTES,
|
|
25
|
+
type MediaArtifactMeta,
|
|
26
|
+
type MediaDownloadView,
|
|
27
|
+
MediaStoreError,
|
|
28
|
+
type MediaStoreVideoType,
|
|
29
|
+
mediaDownloadHeaders,
|
|
30
|
+
parseImagineMediaPath,
|
|
31
|
+
type TrustedImagineAuthz,
|
|
32
|
+
} from "./media-store.ts";
|
|
33
|
+
import { isTrustedLoopbackWebRequest } from "./web-origin.ts";
|
|
34
|
+
|
|
35
|
+
/** Hard ceiling for live exact download routes. Callers cannot raise this. */
|
|
36
|
+
export const IMAGINE_ROUTE_MAX_ENTRIES = 64;
|
|
37
|
+
|
|
38
|
+
/** Hard ceiling for remembered image routes. Callers cannot raise this. */
|
|
39
|
+
export const IMAGINE_IMAGE_ROUTE_TTL_MS = 60 * 60 * 1000;
|
|
40
|
+
|
|
41
|
+
const IMAGE_MEDIA_TYPES = new Set<string>(["image/png", "image/jpeg", "image/webp", "image/gif"]);
|
|
42
|
+
const VIDEO_MEDIA_TYPES = new Set<string>(["video/mp4", "video/webm"]);
|
|
43
|
+
|
|
44
|
+
/** Structural `ctx.webServer` + optional `ctx.effect` surface used by the registrar. */
|
|
45
|
+
export interface ImagineRouteContext {
|
|
46
|
+
readonly webServer: {
|
|
47
|
+
register(route: {
|
|
48
|
+
kind: "exact";
|
|
49
|
+
path: string;
|
|
50
|
+
handler: (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
|
|
51
|
+
}): () => void;
|
|
52
|
+
};
|
|
53
|
+
effect?(setup: () => () => void | Promise<void>, label?: string): unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Attachment seam used at GET time. Only metadata is retained in the registry. */
|
|
57
|
+
export interface ImagineRouteAttachmentStore {
|
|
58
|
+
readonly imageLimits?: { readonly maxImageBytes?: number };
|
|
59
|
+
readImage(
|
|
60
|
+
ref: ImagineImageAttachmentRef,
|
|
61
|
+
signal?: AbortSignal,
|
|
62
|
+
): Promise<{ ref: ImagineImageAttachmentRef; data: Uint8Array }>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Structural media download seam. Production adapts `MediaStore.openDownload`. */
|
|
66
|
+
export interface ImagineMediaDownloadStore {
|
|
67
|
+
readForDownload(artifactId: string, authz: TrustedImagineAuthz): Promise<MediaDownloadView>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface ImagineRouteOptions {
|
|
71
|
+
readonly attachments: ImagineRouteAttachmentStore;
|
|
72
|
+
readonly media: ImagineMediaDownloadStore;
|
|
73
|
+
readonly now?: () => number;
|
|
74
|
+
/** Capped at {@link IMAGINE_ROUTE_MAX_ENTRIES}. */
|
|
75
|
+
readonly maxEntries?: number;
|
|
76
|
+
/** Capped at {@link IMAGINE_IMAGE_ROUTE_TTL_MS}. */
|
|
77
|
+
readonly imageTtlMs?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ImagineRouteRegistry {
|
|
81
|
+
rememberImages(refs: readonly ImageAttachmentRef[] | readonly ImagineImageAttachmentRef[]): void;
|
|
82
|
+
rememberArtifact(meta: MediaArtifactMeta): void;
|
|
83
|
+
revokeImages(): void;
|
|
84
|
+
revokeArtifacts(): void;
|
|
85
|
+
dispose(): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
interface ImageRouteEntry {
|
|
89
|
+
readonly kind: "image";
|
|
90
|
+
readonly id: string;
|
|
91
|
+
readonly path: string;
|
|
92
|
+
readonly routeToken: symbol;
|
|
93
|
+
ref: ImagineImageAttachmentRef;
|
|
94
|
+
expiresAt: number;
|
|
95
|
+
readonly disposeRoute: () => void;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
interface MediaRouteEntry {
|
|
99
|
+
readonly kind: "media";
|
|
100
|
+
readonly id: string;
|
|
101
|
+
readonly path: string;
|
|
102
|
+
readonly routeToken: symbol;
|
|
103
|
+
meta: MediaArtifactMeta;
|
|
104
|
+
expiresAt: number;
|
|
105
|
+
readonly disposeRoute: () => void;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type RouteEntry = ImageRouteEntry | MediaRouteEntry;
|
|
109
|
+
|
|
110
|
+
type RouteHandler = (req: IncomingMessage, res: ServerResponse) => void | Promise<void>;
|
|
111
|
+
|
|
112
|
+
/** Register the bounded exact-path Imagine download registry. */
|
|
113
|
+
export function registerImagineRoutes(ctx: ImagineRouteContext, options: ImagineRouteOptions): ImagineRouteRegistry {
|
|
114
|
+
const now = options.now ?? Date.now;
|
|
115
|
+
const maxEntries = clampPositive(options.maxEntries, IMAGINE_ROUTE_MAX_ENTRIES);
|
|
116
|
+
const imageTtlMs = clampPositive(options.imageTtlMs, IMAGINE_IMAGE_ROUTE_TTL_MS);
|
|
117
|
+
const entries = new Map<string, RouteEntry>();
|
|
118
|
+
let disposed = false;
|
|
119
|
+
|
|
120
|
+
const wrapDisposer = (release: () => void): (() => void) => {
|
|
121
|
+
let released = false;
|
|
122
|
+
return () => {
|
|
123
|
+
if (released) return;
|
|
124
|
+
released = true;
|
|
125
|
+
try {
|
|
126
|
+
release();
|
|
127
|
+
} catch {
|
|
128
|
+
// One route disposer must not strand the rest of the table.
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const evict = (key: string): void => {
|
|
134
|
+
const entry = entries.get(key);
|
|
135
|
+
if (entry === undefined) return;
|
|
136
|
+
entries.delete(key);
|
|
137
|
+
entry.disposeRoute();
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const evictExpired = (at: number): void => {
|
|
141
|
+
for (const [key, entry] of [...entries]) {
|
|
142
|
+
if (entry.expiresAt <= at) evict(key);
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const evictOldest = (): void => {
|
|
147
|
+
const oldest = entries.keys().next().value;
|
|
148
|
+
if (oldest !== undefined) evict(oldest);
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const lookupKey = (kind: RouteEntry["kind"], id: string): string => `${kind}:${id}`;
|
|
152
|
+
|
|
153
|
+
const registerExact = (path: string, handler: RouteHandler): (() => void) =>
|
|
154
|
+
wrapDisposer(
|
|
155
|
+
ctx.webServer.register({
|
|
156
|
+
kind: "exact",
|
|
157
|
+
path,
|
|
158
|
+
handler,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
const isCurrent = (kind: RouteEntry["kind"], id: string, routeToken: symbol): boolean => {
|
|
163
|
+
if (disposed) return false;
|
|
164
|
+
const entry = entries.get(lookupKey(kind, id));
|
|
165
|
+
return entry !== undefined && entry.kind === kind && entry.routeToken === routeToken && entry.expiresAt > now();
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const handleGet = async (
|
|
169
|
+
kind: RouteEntry["kind"],
|
|
170
|
+
id: string,
|
|
171
|
+
routeToken: symbol,
|
|
172
|
+
req: IncomingMessage,
|
|
173
|
+
res: ServerResponse,
|
|
174
|
+
): Promise<void> => {
|
|
175
|
+
if ((req.method ?? "") !== "GET") {
|
|
176
|
+
json(res, 405, { error: "method not allowed" });
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
if (!isTrustedLoopbackWebRequest(req)) {
|
|
180
|
+
json(res, 403, { error: "forbidden" });
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (!isCurrent(kind, id, routeToken)) {
|
|
184
|
+
const key = lookupKey(kind, id);
|
|
185
|
+
const current = entries.get(key);
|
|
186
|
+
if (current?.routeToken === routeToken && current.expiresAt <= now()) evict(key);
|
|
187
|
+
json(res, 404, { error: "not found" });
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const key = lookupKey(kind, id);
|
|
191
|
+
const entry = entries.get(key);
|
|
192
|
+
if (entry === undefined || entry.kind !== kind) {
|
|
193
|
+
json(res, 404, { error: "not found" });
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
if (entry.kind === "image") {
|
|
198
|
+
await serveImage(options.attachments, entry, req, res, () => isCurrent(kind, id, routeToken));
|
|
199
|
+
} else {
|
|
200
|
+
await serveMedia(options.media, entry, res, () => isCurrent(kind, id, routeToken));
|
|
201
|
+
}
|
|
202
|
+
} catch (error: unknown) {
|
|
203
|
+
if (res.headersSent) return;
|
|
204
|
+
if (!isCurrent(kind, id, routeToken)) {
|
|
205
|
+
json(res, 404, { error: "not found" });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
json(res, statusFor(error), { error: publicErrorMessage(error) });
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
const rememberImage = (input: ImageAttachmentRef | ImagineImageAttachmentRef): void => {
|
|
213
|
+
if (disposed) return;
|
|
214
|
+
const at = now();
|
|
215
|
+
evictExpired(at);
|
|
216
|
+
const ref = cloneImageRef(input);
|
|
217
|
+
if (ref === undefined) return;
|
|
218
|
+
if (ref.bytes > maxImageBytes(options.attachments)) return;
|
|
219
|
+
let path: string;
|
|
220
|
+
try {
|
|
221
|
+
path = imagineImagePath(ref.attachmentId);
|
|
222
|
+
} catch {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (parseImagineImagePath(path) !== ref.attachmentId) return;
|
|
226
|
+
const key = lookupKey("image", ref.attachmentId);
|
|
227
|
+
const existing = entries.get(key);
|
|
228
|
+
if (existing?.kind === "image") {
|
|
229
|
+
entries.delete(key);
|
|
230
|
+
existing.ref = ref;
|
|
231
|
+
existing.expiresAt = at + imageTtlMs;
|
|
232
|
+
entries.set(key, existing);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
while (entries.size >= maxEntries) evictOldest();
|
|
236
|
+
const routeToken = Symbol(ref.attachmentId);
|
|
237
|
+
const release = registerExact(path, (req, res) => handleGet("image", ref.attachmentId, routeToken, req, res));
|
|
238
|
+
entries.set(key, {
|
|
239
|
+
kind: "image",
|
|
240
|
+
id: ref.attachmentId,
|
|
241
|
+
path,
|
|
242
|
+
routeToken,
|
|
243
|
+
ref,
|
|
244
|
+
expiresAt: at + imageTtlMs,
|
|
245
|
+
disposeRoute: release,
|
|
246
|
+
});
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const rememberArtifact = (input: MediaArtifactMeta): void => {
|
|
250
|
+
if (disposed) return;
|
|
251
|
+
const at = now();
|
|
252
|
+
evictExpired(at);
|
|
253
|
+
const meta = cloneArtifactMeta(input);
|
|
254
|
+
if (meta === undefined || meta.expiresAt <= at) return;
|
|
255
|
+
let path: string;
|
|
256
|
+
try {
|
|
257
|
+
path = imagineMediaPath(meta.artifactId);
|
|
258
|
+
} catch {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
if (parseImagineMediaPath(path) !== meta.artifactId) return;
|
|
262
|
+
const key = lookupKey("media", meta.artifactId);
|
|
263
|
+
const existing = entries.get(key);
|
|
264
|
+
if (existing?.kind === "media") {
|
|
265
|
+
entries.delete(key);
|
|
266
|
+
existing.meta = meta;
|
|
267
|
+
existing.expiresAt = meta.expiresAt;
|
|
268
|
+
entries.set(key, existing);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
while (entries.size >= maxEntries) evictOldest();
|
|
272
|
+
const routeToken = Symbol(meta.artifactId);
|
|
273
|
+
const release = registerExact(path, (req, res) => handleGet("media", meta.artifactId, routeToken, req, res));
|
|
274
|
+
entries.set(key, {
|
|
275
|
+
kind: "media",
|
|
276
|
+
id: meta.artifactId,
|
|
277
|
+
path,
|
|
278
|
+
routeToken,
|
|
279
|
+
meta,
|
|
280
|
+
expiresAt: meta.expiresAt,
|
|
281
|
+
disposeRoute: release,
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const dispose = (): void => {
|
|
286
|
+
if (disposed) return;
|
|
287
|
+
disposed = true;
|
|
288
|
+
for (const key of [...entries.keys()]) evict(key);
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
if (typeof ctx.effect === "function") {
|
|
292
|
+
ctx.effect(() => dispose, "dsh-coding-subscription-oauth: imagine download routes");
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
rememberImages(refs) {
|
|
297
|
+
for (const ref of refs) rememberImage(ref);
|
|
298
|
+
},
|
|
299
|
+
rememberArtifact,
|
|
300
|
+
revokeImages() {
|
|
301
|
+
for (const [key, entry] of [...entries]) {
|
|
302
|
+
if (entry.kind === "image") evict(key);
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
revokeArtifacts() {
|
|
306
|
+
for (const [key, entry] of [...entries]) {
|
|
307
|
+
if (entry.kind === "media") evict(key);
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
dispose,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async function serveImage(
|
|
315
|
+
attachments: ImagineRouteAttachmentStore,
|
|
316
|
+
entry: ImageRouteEntry,
|
|
317
|
+
req: IncomingMessage,
|
|
318
|
+
res: ServerResponse,
|
|
319
|
+
alive: () => boolean,
|
|
320
|
+
): Promise<void> {
|
|
321
|
+
const limit = maxImageBytes(attachments);
|
|
322
|
+
if (entry.ref.bytes > limit) {
|
|
323
|
+
json(res, 404, { error: "not found" });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const stored = await attachments.readImage(cloneImageRef(entry.ref) ?? entry.ref, requestSignal(req));
|
|
327
|
+
if (res.headersSent) return;
|
|
328
|
+
if (!alive()) {
|
|
329
|
+
json(res, 404, { error: "not found" });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (!sameImageIdentity(entry.ref, stored.ref) || stored.data.byteLength > limit) {
|
|
333
|
+
json(res, 404, { error: "not found" });
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const headers = imagineImageDownloadHeaders({
|
|
337
|
+
...stored.ref,
|
|
338
|
+
bytes: stored.data.byteLength,
|
|
339
|
+
});
|
|
340
|
+
res.writeHead(200, {
|
|
341
|
+
"Content-Type": headers["Content-Type"],
|
|
342
|
+
"Content-Length": String(stored.data.byteLength),
|
|
343
|
+
"Cache-Control": headers["Cache-Control"],
|
|
344
|
+
"X-Content-Type-Options": headers["X-Content-Type-Options"],
|
|
345
|
+
});
|
|
346
|
+
res.end(Buffer.from(stored.data));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async function serveMedia(
|
|
350
|
+
media: ImagineMediaDownloadStore,
|
|
351
|
+
entry: MediaRouteEntry,
|
|
352
|
+
res: ServerResponse,
|
|
353
|
+
alive: () => boolean,
|
|
354
|
+
): Promise<void> {
|
|
355
|
+
const authz = createTrustedImagineAuthz({ authorized: true });
|
|
356
|
+
const view = await media.readForDownload(entry.id, authz);
|
|
357
|
+
if (res.headersSent) return;
|
|
358
|
+
if (!alive()) {
|
|
359
|
+
json(res, 404, { error: "not found" });
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
if (
|
|
363
|
+
view.meta.artifactId !== entry.id ||
|
|
364
|
+
view.meta.mediaType !== entry.meta.mediaType ||
|
|
365
|
+
view.meta.bytes !== view.body.byteLength ||
|
|
366
|
+
view.body.byteLength > MEDIA_STORE_MAX_BYTES
|
|
367
|
+
) {
|
|
368
|
+
json(res, 404, { error: "not found" });
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const headers = mediaDownloadHeaders({ ...entry.meta, bytes: view.body.byteLength });
|
|
372
|
+
res.writeHead(200, {
|
|
373
|
+
"Content-Type": headers["Content-Type"],
|
|
374
|
+
"Content-Length": headers["Content-Length"],
|
|
375
|
+
"Content-Disposition": headers["Content-Disposition"],
|
|
376
|
+
"Cache-Control": headers["Cache-Control"],
|
|
377
|
+
"X-Content-Type-Options": headers["X-Content-Type-Options"],
|
|
378
|
+
});
|
|
379
|
+
res.end(Buffer.from(view.body));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function requestSignal(req: IncomingMessage): AbortSignal | undefined {
|
|
383
|
+
const candidate = req as IncomingMessage & { signal?: unknown };
|
|
384
|
+
return candidate.signal instanceof AbortSignal ? candidate.signal : undefined;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function maxImageBytes(attachments: ImagineRouteAttachmentStore): number {
|
|
388
|
+
const configured = attachments.imageLimits?.maxImageBytes;
|
|
389
|
+
if (typeof configured !== "number" || !Number.isFinite(configured) || configured <= 0) {
|
|
390
|
+
return DEFAULT_IMAGE_DOWNLOAD_MAX_BYTES;
|
|
391
|
+
}
|
|
392
|
+
return Math.min(Math.floor(configured), DEFAULT_IMAGE_DOWNLOAD_MAX_BYTES);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function cloneImageRef(input: ImageAttachmentRef | ImagineImageAttachmentRef): ImagineImageAttachmentRef | undefined {
|
|
396
|
+
if (typeof input.attachmentId !== "string" || !isSafeImagineAttachmentId(input.attachmentId)) return undefined;
|
|
397
|
+
if (!IMAGE_MEDIA_TYPES.has(input.mediaType)) return undefined;
|
|
398
|
+
if (!Number.isFinite(input.bytes) || input.bytes < 0) return undefined;
|
|
399
|
+
if (!Number.isFinite(input.width) || !Number.isFinite(input.height)) return undefined;
|
|
400
|
+
const cloned: ImagineImageAttachmentRef = {
|
|
401
|
+
attachmentId: input.attachmentId,
|
|
402
|
+
mediaType: input.mediaType as ImagineImageMediaType,
|
|
403
|
+
bytes: Math.trunc(input.bytes),
|
|
404
|
+
width: Math.trunc(input.width),
|
|
405
|
+
height: Math.trunc(input.height),
|
|
406
|
+
};
|
|
407
|
+
if (typeof input.name === "string") cloned.name = input.name;
|
|
408
|
+
return cloned;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function cloneArtifactMeta(input: MediaArtifactMeta): MediaArtifactMeta | undefined {
|
|
412
|
+
if (typeof input.artifactId !== "string" || !isSafeMediaArtifactId(input.artifactId)) return undefined;
|
|
413
|
+
if (!VIDEO_MEDIA_TYPES.has(input.mediaType)) return undefined;
|
|
414
|
+
if (!Number.isFinite(input.bytes) || input.bytes <= 0) return undefined;
|
|
415
|
+
if (!Number.isFinite(input.createdAt) || input.createdAt <= 0) return undefined;
|
|
416
|
+
if (!Number.isFinite(input.expiresAt) || input.expiresAt <= input.createdAt) return undefined;
|
|
417
|
+
const cloned: MediaArtifactMeta = {
|
|
418
|
+
artifactId: input.artifactId,
|
|
419
|
+
mediaType: input.mediaType as MediaStoreVideoType,
|
|
420
|
+
bytes: Math.trunc(input.bytes),
|
|
421
|
+
createdAt: input.createdAt,
|
|
422
|
+
expiresAt: input.expiresAt,
|
|
423
|
+
};
|
|
424
|
+
if (typeof input.name === "string") cloned.name = input.name;
|
|
425
|
+
return cloned;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function sameImageIdentity(expected: ImagineImageAttachmentRef, actual: ImagineImageAttachmentRef): boolean {
|
|
429
|
+
return actual.attachmentId === expected.attachmentId && IMAGE_MEDIA_TYPES.has(actual.mediaType);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function statusFor(error: unknown): number {
|
|
433
|
+
if (error instanceof MediaStoreError) {
|
|
434
|
+
if (error.code === "NOT_FOUND" || error.code === "EXPIRED" || error.code === "INVALID_ID") return 404;
|
|
435
|
+
if (error.code === "FORBIDDEN") return 403;
|
|
436
|
+
}
|
|
437
|
+
return 500;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function publicErrorMessage(error: unknown): string {
|
|
441
|
+
if (error instanceof MediaStoreError) {
|
|
442
|
+
if (error.code === "NOT_FOUND" || error.code === "EXPIRED" || error.code === "INVALID_ID") return "not found";
|
|
443
|
+
if (error.code === "FORBIDDEN") return "forbidden";
|
|
444
|
+
}
|
|
445
|
+
return "download failed";
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function clampPositive(value: number | undefined, ceiling: number): number {
|
|
449
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return ceiling;
|
|
450
|
+
return Math.min(Math.floor(value), ceiling);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function json(res: ServerResponse, status: number, value: unknown): void {
|
|
454
|
+
if (res.headersSent) return;
|
|
455
|
+
res.writeHead(status, {
|
|
456
|
+
"content-type": "application/json; charset=utf-8",
|
|
457
|
+
"cache-control": "no-store",
|
|
458
|
+
"x-content-type-options": "nosniff",
|
|
459
|
+
});
|
|
460
|
+
res.end(JSON.stringify(value));
|
|
461
|
+
}
|