@tdacorp/hyle-sdk 0.5.0 → 0.8.1
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/README.md +48 -7
- package/dist/check-routes-bin.js +167 -19
- package/dist/chunk-67IOIAVW.js +35 -0
- package/dist/{chunk-4OB77ZOD.js → chunk-EBNIKRQH.js} +92 -11
- package/dist/{chunk-SO6H2UGL.js → chunk-LGW2KP4X.js} +7 -1
- package/dist/{chunk-OW6S7IZX.js → chunk-N4QYPO7S.js} +1 -1
- package/dist/index.cjs +96 -11
- package/dist/index.d.cts +57 -5
- package/dist/index.d.ts +57 -5
- package/dist/index.js +6 -1
- package/dist/next.cjs +120 -5
- package/dist/next.d.cts +43 -1
- package/dist/next.d.ts +43 -1
- package/dist/next.js +82 -3
- package/dist/react.cjs +16 -1
- package/dist/react.js +2 -1
- package/dist/use-cache.cjs +22 -4
- package/dist/use-cache.js +3 -2
- package/package.json +1 -1
package/dist/next.d.ts
CHANGED
|
@@ -185,4 +185,46 @@ interface HyleRootSitemapOptions {
|
|
|
185
185
|
*/
|
|
186
186
|
declare function hyleRootSitemap(options: Configs, sitemap: HyleRootSitemapOptions): Promise<MetadataRoute.Sitemap>;
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
interface AssetRouteOptions {
|
|
189
|
+
/** hyle's origin (HYLE_TARGET_BASE_URL). */
|
|
190
|
+
readonly baseUrl: string | undefined;
|
|
191
|
+
/** This workspace's target credential (HYLE_TARGET_TOKEN). Used only server-side; it never reaches the browser. */
|
|
192
|
+
readonly token: string | undefined;
|
|
193
|
+
/** Environment to read the site switch from. Default process.env. */
|
|
194
|
+
readonly env?: Readonly<Record<string, string | undefined>>;
|
|
195
|
+
/** Refuse (502) an asset larger than this many bytes. Default 16 MiB; hyle's own upload limit is 4 MiB. */
|
|
196
|
+
readonly maxBytes?: number;
|
|
197
|
+
/** Abort the upstream request, body included, after this many milliseconds. Default 30000. */
|
|
198
|
+
readonly timeoutMs?: number;
|
|
199
|
+
/** Injectable for tests. */
|
|
200
|
+
readonly fetch?: typeof fetch;
|
|
201
|
+
}
|
|
202
|
+
interface AssetRoute {
|
|
203
|
+
GET(req: Request, ctx: {
|
|
204
|
+
params: Promise<{
|
|
205
|
+
file?: string;
|
|
206
|
+
}>;
|
|
207
|
+
}): Promise<Response>;
|
|
208
|
+
HEAD(req: Request, ctx: {
|
|
209
|
+
params: Promise<{
|
|
210
|
+
file?: string;
|
|
211
|
+
}>;
|
|
212
|
+
}): Promise<Response>;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* `export const { GET, HEAD } = createAssetRoute({ ... })` in `app/api/hyle/assets/[file]/route.ts` (ADR 0015).
|
|
216
|
+
*
|
|
217
|
+
* The SDK rewrites hyle's `/assets/<uuid>.<ext>` in rendered content to `/api/hyle/assets/<uuid>.<ext>`; this route answers it by
|
|
218
|
+
* asking hyle for exactly that file with the target credential and streaming the bytes back, same-origin, so a strict
|
|
219
|
+
* `img-src 'self'` (and `media-src 'self'`) CSP needs no change. hyle serves only assets that a currently published page of this
|
|
220
|
+
* workspace references, so an unpublished or other workspace's asset is a 404 here too.
|
|
221
|
+
*
|
|
222
|
+
* Safety: the only input is the `file` segment, matched against `<uuid>[.<ext>]` before anything is sent, so this is not an open
|
|
223
|
+
* proxy. The browser's cookies and authorization header are never forwarded; only `Range`, `If-Range` and `If-None-Match` are.
|
|
224
|
+
* The response headers are built here (no upstream header is copied blindly), the content type must be on the asset allowlist,
|
|
225
|
+
* the body is size-capped and time-bounded, and every failure is an empty 404 or 502 that says nothing about hyle.
|
|
226
|
+
* The site switch `HYLE_CONTENT_ENABLED=0`/`false`, or missing configuration, answers 404.
|
|
227
|
+
*/
|
|
228
|
+
declare function createAssetRoute(options: AssetRouteOptions): AssetRoute;
|
|
229
|
+
|
|
230
|
+
export { type AssetRoute, type AssetRouteOptions, FetchResolverOptions, type HyleNextOptions, HyleResolver, type HyleRootConfig, type HyleRootSitemapOptions, type HyleSitemapOptions, MOUNTS_MAX_STALE_MS, type PageProps, type RevalidateRouteOptions, type RouteParams, createAssetRoute, createRevalidateRoute, hyleMetadata, hylePage, hyleRootMetadata, hyleRootPage, hyleRootSitemap, hyleSitemap };
|
package/dist/next.js
CHANGED
|
@@ -2,18 +2,22 @@ import {
|
|
|
2
2
|
ArticlePage,
|
|
3
3
|
DocsPage,
|
|
4
4
|
ListPage
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-LGW2KP4X.js";
|
|
6
6
|
import {
|
|
7
7
|
DEFAULT_REVALIDATE,
|
|
8
8
|
fetchResolver,
|
|
9
9
|
workspaceTags
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-N4QYPO7S.js";
|
|
11
11
|
import {
|
|
12
12
|
Cp1ClientError,
|
|
13
13
|
createHyleClient,
|
|
14
14
|
createRevalidationCallbackRoute,
|
|
15
15
|
listPublicationPaths
|
|
16
|
-
} from "./chunk-
|
|
16
|
+
} from "./chunk-EBNIKRQH.js";
|
|
17
|
+
import {
|
|
18
|
+
ASSET_CONTENT_TYPES,
|
|
19
|
+
ASSET_FILE
|
|
20
|
+
} from "./chunk-67IOIAVW.js";
|
|
17
21
|
|
|
18
22
|
// src/next/page.tsx
|
|
19
23
|
import { notFound, permanentRedirect, redirect } from "next/navigation";
|
|
@@ -326,8 +330,83 @@ async function hyleRootSitemap(options, sitemap) {
|
|
|
326
330
|
}
|
|
327
331
|
return [...entries.values()];
|
|
328
332
|
}
|
|
333
|
+
|
|
334
|
+
// src/next/asset.ts
|
|
335
|
+
var DEFAULT_MAX_BYTES = 16 * 1024 * 1024;
|
|
336
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
337
|
+
var CACHE_CONTROL = "public, max-age=3600, s-maxage=86400, stale-while-revalidate=86400";
|
|
338
|
+
var PARTIAL_CACHE_CONTROL = "no-store";
|
|
339
|
+
var SAFE_HEADERS = { "X-Content-Type-Options": "nosniff", "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox" };
|
|
340
|
+
var empty = (status, headers = {}) => new Response(null, { status, headers: { "Cache-Control": "no-store", ...headers } });
|
|
341
|
+
var isSwitchedOff2 = (value) => ["0", "false"].includes(value?.trim().toLowerCase() ?? "");
|
|
342
|
+
function capped(body, max) {
|
|
343
|
+
let seen = 0;
|
|
344
|
+
return body.pipeThrough(
|
|
345
|
+
new TransformStream({
|
|
346
|
+
transform(chunk, controller) {
|
|
347
|
+
seen += chunk.byteLength;
|
|
348
|
+
if (seen > max) return controller.error(new Error("asset too large"));
|
|
349
|
+
controller.enqueue(chunk);
|
|
350
|
+
}
|
|
351
|
+
})
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
function createAssetRoute(options) {
|
|
355
|
+
let client;
|
|
356
|
+
const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES;
|
|
357
|
+
async function serve(req, ctx, head) {
|
|
358
|
+
if (isSwitchedOff2((options.env ?? process.env).HYLE_CONTENT_ENABLED)) return empty(404);
|
|
359
|
+
const file = (await ctx.params).file;
|
|
360
|
+
if (typeof file !== "string" || !ASSET_FILE.test(file)) return empty(404);
|
|
361
|
+
try {
|
|
362
|
+
client ??= createHyleClient({ baseUrl: options.baseUrl?.trim() ?? "", token: options.token?.trim() ?? "", ...options.fetch ? { fetch: options.fetch } : {} });
|
|
363
|
+
} catch {
|
|
364
|
+
return empty(404);
|
|
365
|
+
}
|
|
366
|
+
let upstream;
|
|
367
|
+
try {
|
|
368
|
+
upstream = await client.asset(file, {
|
|
369
|
+
timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
370
|
+
headers: { range: req.headers.get("range"), "if-range": req.headers.get("if-range"), "if-none-match": req.headers.get("if-none-match") }
|
|
371
|
+
});
|
|
372
|
+
} catch {
|
|
373
|
+
return empty(502);
|
|
374
|
+
}
|
|
375
|
+
const drop = () => void upstream.body?.cancel().catch(() => {
|
|
376
|
+
});
|
|
377
|
+
const etag = upstream.headers.get("etag");
|
|
378
|
+
if (upstream.status === 304) {
|
|
379
|
+
drop();
|
|
380
|
+
return new Response(null, { status: 304, headers: { "Cache-Control": CACHE_CONTROL, Vary: "Range", ...SAFE_HEADERS, ...etag ? { ETag: etag } : {} } });
|
|
381
|
+
}
|
|
382
|
+
if (upstream.status === 404 || upstream.status === 416) {
|
|
383
|
+
drop();
|
|
384
|
+
const range = upstream.status === 416 ? upstream.headers.get("content-range") : null;
|
|
385
|
+
return empty(upstream.status, range ? { "Content-Range": range } : {});
|
|
386
|
+
}
|
|
387
|
+
const type = upstream.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
|
|
388
|
+
const length = Number(upstream.headers.get("content-length"));
|
|
389
|
+
const expectedStatus = upstream.status === 200 || upstream.status === 206;
|
|
390
|
+
if (!expectedStatus || !upstream.body || !type || !ASSET_CONTENT_TYPES.has(type) || Number.isFinite(length) && length > maxBytes) {
|
|
391
|
+
drop();
|
|
392
|
+
return empty(502);
|
|
393
|
+
}
|
|
394
|
+
const headers = new Headers({ "Content-Type": type, "Accept-Ranges": "bytes", "Cache-Control": upstream.status === 206 ? PARTIAL_CACHE_CONTROL : CACHE_CONTROL, Vary: "Range", ...SAFE_HEADERS });
|
|
395
|
+
if (etag) headers.set("ETag", etag);
|
|
396
|
+
const contentRange = upstream.headers.get("content-range");
|
|
397
|
+
if (upstream.status === 206 && contentRange) headers.set("Content-Range", contentRange);
|
|
398
|
+
if (!upstream.headers.get("content-encoding") && upstream.headers.get("content-length")) headers.set("Content-Length", String(length));
|
|
399
|
+
if (head) {
|
|
400
|
+
drop();
|
|
401
|
+
return new Response(null, { status: upstream.status, headers });
|
|
402
|
+
}
|
|
403
|
+
return new Response(capped(upstream.body, maxBytes), { status: upstream.status, headers });
|
|
404
|
+
}
|
|
405
|
+
return { GET: (req, ctx) => serve(req, ctx, false), HEAD: (req, ctx) => serve(req, ctx, true) };
|
|
406
|
+
}
|
|
329
407
|
export {
|
|
330
408
|
MOUNTS_MAX_STALE_MS,
|
|
409
|
+
createAssetRoute,
|
|
331
410
|
createRevalidateRoute,
|
|
332
411
|
fetchResolver,
|
|
333
412
|
hyleMetadata,
|
package/dist/react.cjs
CHANGED
|
@@ -42,6 +42,20 @@ var import_hast_util_to_jsx_runtime = require("hast-util-to-jsx-runtime");
|
|
|
42
42
|
var import_react = require("react");
|
|
43
43
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
44
44
|
|
|
45
|
+
// src/asset-path.ts
|
|
46
|
+
var ASSET_ROUTE_PREFIX = "/api/hyle/assets";
|
|
47
|
+
var UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
|
48
|
+
var EXTENSIONS = "png|jpg|gif|webp|svg|pdf|mp4|webm|mp3|ogg";
|
|
49
|
+
var ASSET_FILE = new RegExp(`^${UUID}(?:\\.(?:${EXTENSIONS}))?$`);
|
|
50
|
+
var HYLE_ASSET_URL = new RegExp(`^/assets/(${UUID}(?:\\.(?:${EXTENSIONS}))?)$`, "i");
|
|
51
|
+
function hostAssetSrc(src) {
|
|
52
|
+
const file = HYLE_ASSET_URL.exec(src)?.[1];
|
|
53
|
+
return file ? `${ASSET_ROUTE_PREFIX}/${file.toLowerCase()}` : src;
|
|
54
|
+
}
|
|
55
|
+
function hostAssetHtml(html) {
|
|
56
|
+
return html.replace(new RegExp(`(\\ssrc=")(/assets/${UUID}(?:\\.(?:${EXTENSIONS}))?)(")`, "gi"), (_m, open, url, close) => `${open}${hostAssetSrc(url)}${close}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
45
59
|
// src/react/icons.tsx
|
|
46
60
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
47
61
|
var SHAPES = {
|
|
@@ -233,6 +247,7 @@ function clean(node, depth, ctx2, budget) {
|
|
|
233
247
|
if (Object.hasOwn(rules, key) && rules[key](value, inner)) properties[key] = value;
|
|
234
248
|
}
|
|
235
249
|
}
|
|
250
|
+
if ((tag === "img" || tag === "video" || tag === "audio") && typeof properties.src === "string") properties.src = hostAssetSrc(properties.src);
|
|
236
251
|
if (tag === "iframe") {
|
|
237
252
|
if (properties.sandbox === void 0 || properties.srcdoc === void 0) return [];
|
|
238
253
|
properties.sandbox = "allow-scripts";
|
|
@@ -580,7 +595,7 @@ function HastContent({ tree, title, components = {}, locale }) {
|
|
|
580
595
|
});
|
|
581
596
|
}
|
|
582
597
|
function TrustedServerHtml({ html }) {
|
|
583
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { dangerouslySetInnerHTML: { __html: html } });
|
|
598
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { dangerouslySetInnerHTML: { __html: hostAssetHtml(html) } });
|
|
584
599
|
}
|
|
585
600
|
|
|
586
601
|
// src/react/index.ts
|
package/dist/react.js
CHANGED
package/dist/use-cache.cjs
CHANGED
|
@@ -25,6 +25,12 @@ __export(use_cache_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(use_cache_exports);
|
|
26
26
|
var import_cache = require("next/cache");
|
|
27
27
|
|
|
28
|
+
// src/asset-path.ts
|
|
29
|
+
var UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
|
|
30
|
+
var EXTENSIONS = "png|jpg|gif|webp|svg|pdf|mp4|webm|mp3|ogg";
|
|
31
|
+
var ASSET_FILE = new RegExp(`^${UUID}(?:\\.(?:${EXTENSIONS}))?$`);
|
|
32
|
+
var HYLE_ASSET_URL = new RegExp(`^/assets/(${UUID}(?:\\.(?:${EXTENSIONS}))?)$`, "i");
|
|
33
|
+
|
|
28
34
|
// src/errors.ts
|
|
29
35
|
var Cp1ClientError = class extends Error {
|
|
30
36
|
code;
|
|
@@ -70,16 +76,19 @@ function isMounts(data) {
|
|
|
70
76
|
const mounts = data.mounts;
|
|
71
77
|
return typeof data.workspace_id === "string" && isStrings(data.reserved) && isStrings(data.supported_locales) && Array.isArray(mounts) && mounts.every((m) => typeof m?.prefix === "string" && typeof m.default_section === "string");
|
|
72
78
|
}
|
|
73
|
-
function
|
|
79
|
+
function checkBaseUrl(baseUrl, allowInsecureHttp) {
|
|
74
80
|
let origin;
|
|
75
81
|
try {
|
|
76
|
-
origin = new URL(
|
|
82
|
+
origin = new URL(baseUrl);
|
|
77
83
|
} catch {
|
|
78
84
|
throw new TypeError("baseUrl must be an https origin");
|
|
79
85
|
}
|
|
80
|
-
if (origin.protocol !== "https:" && !(origin.protocol === "http:" && (
|
|
86
|
+
if (origin.protocol !== "https:" && !(origin.protocol === "http:" && (allowInsecureHttp || LOOPBACK.has(origin.hostname)))) {
|
|
81
87
|
throw new TypeError("baseUrl must be https (http is allowed only for localhost, or with allowInsecureHttp)");
|
|
82
88
|
}
|
|
89
|
+
}
|
|
90
|
+
function createHyleClient(options) {
|
|
91
|
+
checkBaseUrl(options.baseUrl, options.allowInsecureHttp);
|
|
83
92
|
if (!options.token) throw new TypeError("token is required");
|
|
84
93
|
const base = options.baseUrl.replace(/\/+$/, "");
|
|
85
94
|
const fetchImpl = options.fetch ?? fetch;
|
|
@@ -122,7 +131,16 @@ function createHyleClient(options) {
|
|
|
122
131
|
if (data?.cp === 1 && data.kind === "mounts" && isMounts(data)) return data;
|
|
123
132
|
throw failure(reply);
|
|
124
133
|
}
|
|
125
|
-
|
|
134
|
+
async function asset(file, opts = {}) {
|
|
135
|
+
if (!ASSET_FILE.test(file)) throw new TypeError("asset file must be <uuid>[.<ext>]");
|
|
136
|
+
const headers = new Headers({ authorization: `Bearer ${options.token}` });
|
|
137
|
+
for (const name of ["range", "if-range", "if-none-match"]) {
|
|
138
|
+
const value = opts.headers?.[name];
|
|
139
|
+
if (value) headers.set(name, value);
|
|
140
|
+
}
|
|
141
|
+
return fetchImpl(`${base}/cp1/read/assets/${file}`, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(opts.timeoutMs ?? options.timeoutMs ?? DEFAULT_TIMEOUT_MS) });
|
|
142
|
+
}
|
|
143
|
+
return { resolve, mounts, asset };
|
|
126
144
|
}
|
|
127
145
|
|
|
128
146
|
// src/tag-validation.ts
|
package/dist/use-cache.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
DEFAULT_REVALIDATE
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-N4QYPO7S.js";
|
|
4
4
|
import {
|
|
5
5
|
createHyleClient,
|
|
6
6
|
isValidCacheTag
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-EBNIKRQH.js";
|
|
8
|
+
import "./chunk-67IOIAVW.js";
|
|
8
9
|
|
|
9
10
|
// src/next/use-cache.ts
|
|
10
11
|
import { cacheLife, cacheTag } from "next/cache";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tdacorp/hyle-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "hyle SDK: a typed client for hyle's resolve endpoint, a hast renderer for React, and a Next.js catch-all page with tag-based revalidation.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|