@wervt/core 0.0.3 → 0.0.5
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 +2 -0
- package/dist/image.d.ts +82 -0
- package/dist/image.js +97 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.js +4 -2
- package/dist/manifest.d.ts +36 -9
- package/dist/manifest.js +19 -1
- package/dist/public-files.d.ts +9 -0
- package/dist/public-files.js +43 -0
- package/dist/user.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,5 +23,7 @@ import { getAnthropic } from '@wervt/core/anthropic'
|
|
|
23
23
|
- `createTaskHandler(tasks)`: the routes the control plane runs tasks through.
|
|
24
24
|
- `useDb()`, `seedTable()` (`@wervt/core/db`): Drizzle on the app's database.
|
|
25
25
|
- `getAnthropic()` (`@wervt/core/anthropic`): Claude without an API key, through the app's identity.
|
|
26
|
+
- `publicFileResponse(request, files)`: answers the app's server-side fetches of its own `public/`
|
|
27
|
+
files from the runtime, as the isolate can't read them.
|
|
26
28
|
|
|
27
29
|
Failed platform calls throw a `WervtError` with the HTTP status to answer.
|
package/dist/image.d.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
export declare const IMAGE_FORMATS: readonly ['jpg', 'png', 'webp', 'avif', 'gif', 'bmp'];
|
|
2
|
+
export type ImageFormat = (typeof IMAGE_FORMATS)[number];
|
|
3
|
+
/** A named anchor, or a focus point: `x` and `y` from 0 to 1, from the top left. */
|
|
4
|
+
export type ImageGravity = 'center' | 'north' | 'south' | 'east' | 'west' | 'north-east' | 'north-west' | 'south-east' | 'south-west' | 'smart' | {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
};
|
|
8
|
+
export interface ImageOptions {
|
|
9
|
+
/** Pixels; leave one of the two out to keep the aspect ratio. */
|
|
10
|
+
width?: number;
|
|
11
|
+
height?: number;
|
|
12
|
+
/**
|
|
13
|
+
* `fit` keeps the whole image inside width × height (the default), `fill` covers it and cuts
|
|
14
|
+
* off the rest, `force` ignores the aspect ratio.
|
|
15
|
+
*/
|
|
16
|
+
fit?: 'fit' | 'fill' | 'force';
|
|
17
|
+
/** Which part `fill` and `crop` keep. */
|
|
18
|
+
gravity?: ImageGravity;
|
|
19
|
+
/** Lets a small image grow to width × height; off by default. */
|
|
20
|
+
enlarge?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Cuts a window out before resizing. Sizes of 1 and above are pixels of the source, below 1 a
|
|
23
|
+
* share of it (`0.5` is half the width).
|
|
24
|
+
*/
|
|
25
|
+
crop?: {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
gravity?: ImageGravity;
|
|
29
|
+
};
|
|
30
|
+
/** Degrees, clockwise, on top of the rotation the image's EXIF data asks for. */
|
|
31
|
+
rotate?: 0 | 90 | 180 | 270;
|
|
32
|
+
/** 1 to 100. */
|
|
33
|
+
quality?: number;
|
|
34
|
+
/** `webp` by default. */
|
|
35
|
+
format?: ImageFormat;
|
|
36
|
+
/** Multiplies width and height, for `srcset`. */
|
|
37
|
+
dpr?: number;
|
|
38
|
+
/** Gaussian blur, as sigma. */
|
|
39
|
+
blur?: number;
|
|
40
|
+
/** Keeps EXIF and other metadata, which is removed by default. */
|
|
41
|
+
keepMetadata?: boolean;
|
|
42
|
+
/** Answers as a download; a string names the file (without extension). */
|
|
43
|
+
download?: boolean | string;
|
|
44
|
+
/** Seconds the URL works. Without it, it works (and is cached) forever. */
|
|
45
|
+
expiresIn?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Other imgproxy processing options, as they appear in its URLs (`['sh:0.7', 'bg:fff']`):
|
|
48
|
+
* https://docs.imgproxy.net/usage/processing
|
|
49
|
+
*/
|
|
50
|
+
options?: string[];
|
|
51
|
+
}
|
|
52
|
+
/** A file in the app's bucket: its key, or an ablage file (`{ group, id }`). */
|
|
53
|
+
export type ImageSource = string | {
|
|
54
|
+
group: string;
|
|
55
|
+
id: string;
|
|
56
|
+
};
|
|
57
|
+
/** imgproxy's processing options for `options`, as URL path segments. */
|
|
58
|
+
export declare function imageProcessingOptions(options: ImageOptions, now?: number): string[];
|
|
59
|
+
/** The key of an ablage file's bytes in the bucket (ablage's S3 layout). */
|
|
60
|
+
export declare function imageSourceKey(source: ImageSource): string;
|
|
61
|
+
/** The path the runtime's image gateway serves, signed with `key`. */
|
|
62
|
+
export declare function signImagePath(key: string, source: ImageSource, options?: ImageOptions): string;
|
|
63
|
+
/**
|
|
64
|
+
* The URL of a resized or converted version of an image in the app's bucket, as a path on the
|
|
65
|
+
* app's own host. Use it wherever the browser loads the image (`<img :src>`, `srcset`, CSS):
|
|
66
|
+
*
|
|
67
|
+
* ```ts
|
|
68
|
+
* imageUrl({ group: 'photos', id }, { width: 800, quality: 80 })
|
|
69
|
+
* imageUrl(file, { width: 400, height: 400, fit: 'fill', gravity: 'smart', format: 'jpg' })
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* The URL carries no login of its own: whoever has it can load that version of the image (in a
|
|
73
|
+
* private app, after signing in). Hand out URLs only to users who may see the file, and set
|
|
74
|
+
* `expiresIn` when that can change.
|
|
75
|
+
*/
|
|
76
|
+
export declare function imageUrl(source: ImageSource, options?: ImageOptions): string;
|
|
77
|
+
/**
|
|
78
|
+
* Loads a resized or converted image on the server, e.g. to read its pixels, store the result as
|
|
79
|
+
* a new file or send it to another service. Same options as `imageUrl`. The answer is imgproxy's
|
|
80
|
+
* image, or a JSON `{ error }` with the status when the file is missing or can't be processed.
|
|
81
|
+
*/
|
|
82
|
+
export declare function fetchImage(source: ImageSource, options?: ImageOptions): Promise<Response>;
|
package/dist/image.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { WervtError } from "./errors.js";
|
|
2
|
+
import { appHeaders, runtimeUrl } from "./runtime.js";
|
|
3
|
+
import { createHmac } from "node:crypto";
|
|
4
|
+
// src/image.ts
|
|
5
|
+
const IMAGE_FORMATS = [
|
|
6
|
+
"jpg",
|
|
7
|
+
"png",
|
|
8
|
+
"webp",
|
|
9
|
+
"avif",
|
|
10
|
+
"gif",
|
|
11
|
+
"bmp"
|
|
12
|
+
];
|
|
13
|
+
const GRAVITIES = {
|
|
14
|
+
center: "ce",
|
|
15
|
+
north: "no",
|
|
16
|
+
south: "so",
|
|
17
|
+
east: "ea",
|
|
18
|
+
west: "we",
|
|
19
|
+
"north-east": "noea",
|
|
20
|
+
"north-west": "nowe",
|
|
21
|
+
"south-east": "soea",
|
|
22
|
+
"south-west": "sowe",
|
|
23
|
+
smart: "sm"
|
|
24
|
+
};
|
|
25
|
+
const number = (value) => String(Number(value.toFixed(6)));
|
|
26
|
+
const gravity = (value) => typeof value === "string" ? GRAVITIES[value] : `fp:${number(value.x)}:${number(value.y)}`;
|
|
27
|
+
/** imgproxy's processing options for `options`, as URL path segments. */
|
|
28
|
+
function imageProcessingOptions(options, now = Date.now()) {
|
|
29
|
+
const parts = [];
|
|
30
|
+
if (options.crop) parts.push([
|
|
31
|
+
"c",
|
|
32
|
+
number(options.crop.width),
|
|
33
|
+
number(options.crop.height),
|
|
34
|
+
...options.crop.gravity ? [gravity(options.crop.gravity)] : []
|
|
35
|
+
].join(":"));
|
|
36
|
+
if (options.width || options.height || options.fit) parts.push([
|
|
37
|
+
"rs",
|
|
38
|
+
options.fit ?? "fit",
|
|
39
|
+
Math.round(options.width ?? 0),
|
|
40
|
+
Math.round(options.height ?? 0),
|
|
41
|
+
options.enlarge ? 1 : 0
|
|
42
|
+
].join(":"));
|
|
43
|
+
if (options.gravity) parts.push(`g:${gravity(options.gravity)}`);
|
|
44
|
+
if (options.rotate) parts.push(`rot:${options.rotate}`);
|
|
45
|
+
if (options.dpr) parts.push(`dpr:${number(options.dpr)}`);
|
|
46
|
+
if (options.blur) parts.push(`bl:${number(options.blur)}`);
|
|
47
|
+
if (options.quality) parts.push(`q:${Math.round(options.quality)}`);
|
|
48
|
+
if (options.keepMetadata) parts.push("sm:0");
|
|
49
|
+
if (typeof options.download === "string") {
|
|
50
|
+
const name = options.download.replace(/[^A-Za-z0-9._-]+/g, "_").slice(0, 100);
|
|
51
|
+
if (name) parts.push(`fn:${name}`);
|
|
52
|
+
}
|
|
53
|
+
if (options.download) parts.push("att:1");
|
|
54
|
+
if (options.expiresIn) parts.push(`exp:${Math.floor(now / 1e3) + Math.round(options.expiresIn)}`);
|
|
55
|
+
parts.push(...options.options ?? []);
|
|
56
|
+
return parts;
|
|
57
|
+
}
|
|
58
|
+
/** The key of an ablage file's bytes in the bucket (ablage's S3 layout). */
|
|
59
|
+
function imageSourceKey(source) {
|
|
60
|
+
return typeof source === "string" ? source : `${source.group}/data/${source.id}`;
|
|
61
|
+
}
|
|
62
|
+
/** The path the runtime's image gateway serves, signed with `key`. */
|
|
63
|
+
function signImagePath(key, source, options = {}) {
|
|
64
|
+
const encoded = Buffer.from(imageSourceKey(source), "utf8").toString("base64url");
|
|
65
|
+
const signed = [...imageProcessingOptions(options), `${encoded}.${options.format ?? "webp"}`].join("/");
|
|
66
|
+
return `/_wervt/image/${createHmac("sha256", key).update(signed).digest("base64url")}/${signed}`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The URL of a resized or converted version of an image in the app's bucket, as a path on the
|
|
70
|
+
* app's own host. Use it wherever the browser loads the image (`<img :src>`, `srcset`, CSS):
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* imageUrl({ group: 'photos', id }, { width: 800, quality: 80 })
|
|
74
|
+
* imageUrl(file, { width: 400, height: 400, fit: 'fill', gravity: 'smart', format: 'jpg' })
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* The URL carries no login of its own: whoever has it can load that version of the image (in a
|
|
78
|
+
* private app, after signing in). Hand out URLs only to users who may see the file, and set
|
|
79
|
+
* `expiresIn` when that can change.
|
|
80
|
+
*/
|
|
81
|
+
function imageUrl(source, options = {}) {
|
|
82
|
+
const key = process.env.WERVT_IMAGE_KEY;
|
|
83
|
+
if (!key) throw new WervtError(503, "Images need wervt with its image service (locally: docker compose up -d garage imgproxy)");
|
|
84
|
+
return signImagePath(key, source, options);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Loads a resized or converted image on the server, e.g. to read its pixels, store the result as
|
|
88
|
+
* a new file or send it to another service. Same options as `imageUrl`. The answer is imgproxy's
|
|
89
|
+
* image, or a JSON `{ error }` with the status when the file is missing or can't be processed.
|
|
90
|
+
*/
|
|
91
|
+
async function fetchImage(source, options = {}) {
|
|
92
|
+
const path = imageUrl(source, options);
|
|
93
|
+
const runtime = runtimeUrl();
|
|
94
|
+
if (!runtime) throw new WervtError(503, "fetchImage only works on wervt, not in local dev");
|
|
95
|
+
return fetch(new URL(path, runtime), { headers: appHeaders() });
|
|
96
|
+
}
|
|
97
|
+
export { IMAGE_FORMATS, fetchImage, imageProcessingOptions, imageSourceKey, imageUrl, signImagePath };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { WervtAccess, WervtInvite, appAccess } from "./access.js";
|
|
2
2
|
import { WervtError, errorResponse } from "./errors.js";
|
|
3
3
|
import { WorkloadIdentity, workloadIdentity } from "./identity.js";
|
|
4
|
+
import { IMAGE_FORMATS, ImageFormat, ImageGravity, ImageOptions, ImageSource, fetchImage, imageProcessingOptions, imageSourceKey, imageUrl, signImagePath } from "./image.js";
|
|
4
5
|
import { MailMessage, sendMail } from "./mail.js";
|
|
5
|
-
import { BUNDLE_MANIFEST, BrandManifest, BundleManifest, PreviewManifest, TaskSchedule, TasksManifest } from "./manifest.js";
|
|
6
|
+
import { BUNDLE_MANIFEST, BrandManifest, BundleManifest, CACHE_CONTROL_PATTERN, PUBLIC_PATH_PATTERN, PreviewManifest, PublicCacheRule, PublicCacheRules, TaskSchedule, TasksManifest, matchesPublicPath } from "./manifest.js";
|
|
6
7
|
import { appHeaders, callRuntime, runtimeUrl } from "./runtime.js";
|
|
8
|
+
import { PUBLIC_FILE_SERVICE, publicFileResponse } from "./public-files.js";
|
|
7
9
|
import { ShapeRequest, ShapeResponse, ShapeRowOf, parseTimestamp, proxyShape, shapeParser } from "./shape.js";
|
|
8
10
|
import { ACCOUNT_MERGED_TASK, AccountMergedPayload, TASK_HEADER, Task, TaskEvent, TaskResult, createTaskHandler, defineTask, isTaskRequest, taskRunResponse } from "./tasks.js";
|
|
9
11
|
import { AGENT_TOKEN_HEADER, AgentToken, USER_HEADER, USER_PROOF_HEADER, WervtRole, WervtUser, checkRole, getAgentToken, getUser, parseAgentToken, parseWervtUser, requireRole, userFromClaims } from "./user.js";
|
|
10
|
-
export { ACCOUNT_MERGED_TASK, AGENT_TOKEN_HEADER, type AccountMergedPayload, type AgentToken, BUNDLE_MANIFEST, type BrandManifest, type BundleManifest, type MailMessage, type PreviewManifest, type ShapeRequest, type ShapeResponse, type ShapeRowOf, TASK_HEADER, type Task, type TaskEvent, type TaskResult, type TaskSchedule, type TasksManifest, USER_HEADER, USER_PROOF_HEADER, type WervtAccess, WervtError, type WervtInvite, type WervtRole, type WervtUser, type WorkloadIdentity, appAccess, appHeaders, callRuntime, checkRole, createTaskHandler, defineTask, errorResponse, getAgentToken, getUser, isTaskRequest, parseAgentToken, parseTimestamp, parseWervtUser, proxyShape, requireRole, runtimeUrl, sendMail, shapeParser, taskRunResponse, userFromClaims, workloadIdentity };
|
|
12
|
+
export { ACCOUNT_MERGED_TASK, AGENT_TOKEN_HEADER, type AccountMergedPayload, type AgentToken, BUNDLE_MANIFEST, type BrandManifest, type BundleManifest, CACHE_CONTROL_PATTERN, IMAGE_FORMATS, type ImageFormat, type ImageGravity, type ImageOptions, type ImageSource, type MailMessage, PUBLIC_FILE_SERVICE, PUBLIC_PATH_PATTERN, type PreviewManifest, type PublicCacheRule, type PublicCacheRules, type ShapeRequest, type ShapeResponse, type ShapeRowOf, TASK_HEADER, type Task, type TaskEvent, type TaskResult, type TaskSchedule, type TasksManifest, USER_HEADER, USER_PROOF_HEADER, type WervtAccess, WervtError, type WervtInvite, type WervtRole, type WervtUser, type WorkloadIdentity, appAccess, appHeaders, callRuntime, checkRole, createTaskHandler, defineTask, errorResponse, fetchImage, getAgentToken, getUser, imageProcessingOptions, imageSourceKey, imageUrl, isTaskRequest, matchesPublicPath, parseAgentToken, parseTimestamp, parseWervtUser, proxyShape, publicFileResponse, requireRole, runtimeUrl, sendMail, shapeParser, signImagePath, taskRunResponse, userFromClaims, workloadIdentity };
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,10 @@ import { appHeaders, callRuntime, runtimeUrl } from "./runtime.js";
|
|
|
3
3
|
import { AGENT_TOKEN_HEADER, USER_HEADER, USER_PROOF_HEADER, checkRole, getAgentToken, getUser, parseAgentToken, parseWervtUser, requireRole, userFromClaims } from "./user.js";
|
|
4
4
|
import { appAccess } from "./access.js";
|
|
5
5
|
import { workloadIdentity } from "./identity.js";
|
|
6
|
+
import { IMAGE_FORMATS, fetchImage, imageProcessingOptions, imageSourceKey, imageUrl, signImagePath } from "./image.js";
|
|
6
7
|
import { sendMail } from "./mail.js";
|
|
7
|
-
import { BUNDLE_MANIFEST } from "./manifest.js";
|
|
8
|
+
import { BUNDLE_MANIFEST, CACHE_CONTROL_PATTERN, PUBLIC_PATH_PATTERN, matchesPublicPath } from "./manifest.js";
|
|
9
|
+
import { PUBLIC_FILE_SERVICE, publicFileResponse } from "./public-files.js";
|
|
8
10
|
import { parseTimestamp, proxyShape, shapeParser } from "./shape.js";
|
|
9
11
|
import { ACCOUNT_MERGED_TASK, TASK_HEADER, createTaskHandler, defineTask, isTaskRequest, taskRunResponse } from "./tasks.js";
|
|
10
|
-
export { ACCOUNT_MERGED_TASK, AGENT_TOKEN_HEADER, BUNDLE_MANIFEST, TASK_HEADER, USER_HEADER, USER_PROOF_HEADER, WervtError, appAccess, appHeaders, callRuntime, checkRole, createTaskHandler, defineTask, errorResponse, getAgentToken, getUser, isTaskRequest, parseAgentToken, parseTimestamp, parseWervtUser, proxyShape, requireRole, runtimeUrl, sendMail, shapeParser, taskRunResponse, userFromClaims, workloadIdentity };
|
|
12
|
+
export { ACCOUNT_MERGED_TASK, AGENT_TOKEN_HEADER, BUNDLE_MANIFEST, CACHE_CONTROL_PATTERN, IMAGE_FORMATS, PUBLIC_FILE_SERVICE, PUBLIC_PATH_PATTERN, TASK_HEADER, USER_HEADER, USER_PROOF_HEADER, WervtError, appAccess, appHeaders, callRuntime, checkRole, createTaskHandler, defineTask, errorResponse, fetchImage, getAgentToken, getUser, imageProcessingOptions, imageSourceKey, imageUrl, isTaskRequest, matchesPublicPath, parseAgentToken, parseTimestamp, parseWervtUser, proxyShape, publicFileResponse, requireRole, runtimeUrl, sendMail, shapeParser, signImagePath, taskRunResponse, userFromClaims, workloadIdentity };
|
package/dist/manifest.d.ts
CHANGED
|
@@ -36,6 +36,41 @@ export interface BrandManifest {
|
|
|
36
36
|
/** The login's language (`de`, `en`). The visitor's browser decides when missing. */
|
|
37
37
|
lang?: string;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* How long files in `public/` may be cached. The runtime serves them itself, so the server's own
|
|
41
|
+
* headers (Nitro's `routeRules`) never apply to them. Bundles without any of these get Nuxt's
|
|
42
|
+
* `/_nuxt/` rules; naming any replaces all of them.
|
|
43
|
+
*
|
|
44
|
+
* A file's `cache-control`, first match wins: `noCache`, then `immutable`, then the first of
|
|
45
|
+
* `rules`; none sends no header. A prerendered page (`/about` answered with `about/index.html`)
|
|
46
|
+
* is always `no-cache`, as it changes with every release under the same URL.
|
|
47
|
+
*/
|
|
48
|
+
export interface PublicCacheRules {
|
|
49
|
+
/**
|
|
50
|
+
* Path prefixes of files that never change (hashed build assets), cached for a year, e.g.
|
|
51
|
+
* `['/assets/']`.
|
|
52
|
+
*/
|
|
53
|
+
immutable?: string[];
|
|
54
|
+
/** Paths browsers must check again every time, e.g. a file naming the newest build. */
|
|
55
|
+
noCache?: string[];
|
|
56
|
+
/** Any other `cache-control`, by path pattern (see `matchesPublicPath`). */
|
|
57
|
+
rules?: PublicCacheRule[];
|
|
58
|
+
}
|
|
59
|
+
export interface PublicCacheRule {
|
|
60
|
+
/** An exact path (`/favicon.svg`), with `*` for one segment and a final `/**` for the rest. */
|
|
61
|
+
path: string;
|
|
62
|
+
/** The header's value, e.g. `public, max-age=86400`. */
|
|
63
|
+
cacheControl: string;
|
|
64
|
+
}
|
|
65
|
+
/** What `PublicCacheRule.path` may be: `/images/**`, `/icons/*` or `/favicon.svg`. */
|
|
66
|
+
export declare const PUBLIC_PATH_PATTERN: RegExp;
|
|
67
|
+
/**
|
|
68
|
+
* What `PublicCacheRule.cacheControl` may be: comma-separated directives, each a token with an
|
|
69
|
+
* optional number or quoted list. Narrow on purpose, as the value ends up in a response header.
|
|
70
|
+
*/
|
|
71
|
+
export declare const CACHE_CONTROL_PATTERN: RegExp;
|
|
72
|
+
/** Whether `path` (`/images/a.avif`) matches a rule's `pattern` (`/images/**`). */
|
|
73
|
+
export declare function matchesPublicPath(pattern: string, path: string): boolean;
|
|
39
74
|
/** The bundle's `wervt-bundle.json`, next to `server/` and `public/`. */
|
|
40
75
|
export declare const BUNDLE_MANIFEST = "wervt-bundle.json";
|
|
41
76
|
export interface BundleManifest {
|
|
@@ -47,15 +82,7 @@ export interface BundleManifest {
|
|
|
47
82
|
* `true` without them gives an empty schema.
|
|
48
83
|
*/
|
|
49
84
|
database?: boolean;
|
|
50
|
-
public?:
|
|
51
|
-
/**
|
|
52
|
-
* Path prefixes of files that never change (hashed build assets), cached for a year, e.g.
|
|
53
|
-
* `['/assets/']`. Defaults to Nuxt's `/_nuxt/` for Nuxt and bundles without a framework.
|
|
54
|
-
*/
|
|
55
|
-
immutable?: string[];
|
|
56
|
-
/** Paths browsers must check again every time, e.g. a file naming the newest build. */
|
|
57
|
-
noCache?: string[];
|
|
58
|
-
};
|
|
85
|
+
public?: PublicCacheRules;
|
|
59
86
|
tasks?: TasksManifest;
|
|
60
87
|
/** Shape routes without parameters, requested once after a deploy to check they work. */
|
|
61
88
|
shapes?: string[];
|
package/dist/manifest.js
CHANGED
|
@@ -1,4 +1,22 @@
|
|
|
1
1
|
// src/manifest.ts
|
|
2
|
+
/** What `PublicCacheRule.path` may be: `/images/**`, `/icons/*` or `/favicon.svg`. */
|
|
3
|
+
const PUBLIC_PATH_PATTERN = /^(?:\/(?:[^/*\s?#]+|\*))*(?:\/\*\*)?$/;
|
|
4
|
+
/**
|
|
5
|
+
* What `PublicCacheRule.cacheControl` may be: comma-separated directives, each a token with an
|
|
6
|
+
* optional number or quoted list. Narrow on purpose, as the value ends up in a response header.
|
|
7
|
+
*/
|
|
8
|
+
const CACHE_CONTROL_PATTERN = /^[a-z-]+(?:=(?:\d+|"[a-z, -]*"))?(?:, ?[a-z-]+(?:=(?:\d+|"[a-z, -]*"))?)*$/i;
|
|
9
|
+
/** Whether `path` (`/images/a.avif`) matches a rule's `pattern` (`/images/**`). */
|
|
10
|
+
function matchesPublicPath(pattern, path) {
|
|
11
|
+
const want = pattern.split("/");
|
|
12
|
+
const have = path.split("/");
|
|
13
|
+
for (let i = 0; i < want.length; i++) {
|
|
14
|
+
if (want[i] === "**") return true;
|
|
15
|
+
if (i >= have.length) return false;
|
|
16
|
+
if (want[i] === "*" ? have[i] === "" : want[i] !== have[i]) return false;
|
|
17
|
+
}
|
|
18
|
+
return want.length === have.length;
|
|
19
|
+
}
|
|
2
20
|
/** The bundle's `wervt-bundle.json`, next to `server/` and `public/`. */
|
|
3
21
|
const BUNDLE_MANIFEST = "wervt-bundle.json";
|
|
4
|
-
export { BUNDLE_MANIFEST };
|
|
22
|
+
export { BUNDLE_MANIFEST, CACHE_CONTROL_PATTERN, PUBLIC_PATH_PATTERN, matchesPublicPath };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The runtime service that answers with a file of the calling app's active release. */
|
|
2
|
+
export declare const PUBLIC_FILE_SERVICE = "/_wervt/public-file";
|
|
3
|
+
/**
|
|
4
|
+
* The file `req` asks for, from the runtime, or `undefined` to let the server answer: not a GET
|
|
5
|
+
* or HEAD, a path that isn't in `files` (the build's public dir, as request paths), outside
|
|
6
|
+
* wervt, or the runtime didn't have it. Falling through keeps a route that answers under the same
|
|
7
|
+
* path working, as it did before.
|
|
8
|
+
*/
|
|
9
|
+
export declare function publicFileResponse(req: Request, files: ReadonlySet<string>): Promise<Response | undefined>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { appHeaders, runtimeUrl } from "./runtime.js";
|
|
2
|
+
// src/public-files.ts
|
|
3
|
+
/** The runtime service that answers with a file of the calling app's active release. */
|
|
4
|
+
const PUBLIC_FILE_SERVICE = "/_wervt/public-file";
|
|
5
|
+
const FORWARDED = [
|
|
6
|
+
"range",
|
|
7
|
+
"if-none-match",
|
|
8
|
+
"if-modified-since",
|
|
9
|
+
"if-range"
|
|
10
|
+
];
|
|
11
|
+
/**
|
|
12
|
+
* The file `req` asks for, from the runtime, or `undefined` to let the server answer: not a GET
|
|
13
|
+
* or HEAD, a path that isn't in `files` (the build's public dir, as request paths), outside
|
|
14
|
+
* wervt, or the runtime didn't have it. Falling through keeps a route that answers under the same
|
|
15
|
+
* path working, as it did before.
|
|
16
|
+
*/
|
|
17
|
+
async function publicFileResponse(req, files) {
|
|
18
|
+
if (req.method !== "GET" && req.method !== "HEAD") return void 0;
|
|
19
|
+
const runtime = runtimeUrl();
|
|
20
|
+
if (!runtime) return void 0;
|
|
21
|
+
let path;
|
|
22
|
+
try {
|
|
23
|
+
path = decodeURIComponent(new URL(req.url, "http://localhost").pathname);
|
|
24
|
+
} catch {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (!files.has(path)) return void 0;
|
|
28
|
+
const url = new URL(PUBLIC_FILE_SERVICE, runtime);
|
|
29
|
+
url.searchParams.set("path", path);
|
|
30
|
+
const headers = new Headers(appHeaders());
|
|
31
|
+
for (const name of FORWARDED) {
|
|
32
|
+
const value = req.headers.get(name);
|
|
33
|
+
if (value) headers.set(name, value);
|
|
34
|
+
}
|
|
35
|
+
const res = await fetch(url, {
|
|
36
|
+
method: req.method,
|
|
37
|
+
headers,
|
|
38
|
+
signal: req.signal
|
|
39
|
+
}).catch(() => void 0);
|
|
40
|
+
if (res && (res.ok || res.status === 304 || res.status === 416)) return res;
|
|
41
|
+
await res?.body?.cancel();
|
|
42
|
+
}
|
|
43
|
+
export { PUBLIC_FILE_SERVICE, publicFileResponse };
|
package/dist/user.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export interface WervtUser {
|
|
|
16
16
|
accounts?: Record<string, string>;
|
|
17
17
|
/** Missing only for users an app's own `/mcp` tokens stand for. */
|
|
18
18
|
role?: WervtRole;
|
|
19
|
+
/** An admin of the wervt server, whatever their `role` in this app. */
|
|
20
|
+
admin?: boolean;
|
|
19
21
|
}
|
|
20
22
|
/** Parses the `x-wervt-user` header the runtime sets. */
|
|
21
23
|
export declare function parseWervtUser(header: string | null | undefined): WervtUser | undefined;
|
package/package.json
CHANGED