@midsummer-ai/sdk 0.1.0-staging.3356 → 0.1.0-staging.3362

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/flags.d.mts ADDED
@@ -0,0 +1,38 @@
1
+ //#region src/flags.d.ts
2
+ /**
3
+ * A Project's flags, as Midsummer decided them for this visitor before the request reached your code. Change a flag
4
+ * with `midsummer deploy flag set` or on the Project's Flags page; the next request has it, with no deploy.
5
+ *
6
+ * import { flags } from "@midsummer-ai/sdk/flags"
7
+ *
8
+ * // On the server, from the request:
9
+ * if (flags(request).on("new-checkout")) return newCheckout(request)
10
+ *
11
+ * // In the browser, from the page's cookie:
12
+ * if (flags().on("new-banner")) showBanner()
13
+ *
14
+ * On the server they come in the `x-midsummer-flags` header, which only Midsummer sets: a visitor's own copy never
15
+ * reaches your code. In the browser they come in the `ms_flags` cookie, which every page Midsummer serves sets when
16
+ * it changes; a script can read it, so decide anything that matters on the server. Away from Midsummer (`midsummer
17
+ * dev`, tests) there are none, and each flag is the fallback you give.
18
+ */
19
+ /** The header Midsummer sends your server each request's flags in, and the cookie it keeps them in for the browser. */
20
+ declare const FLAGS_HEADER = "x-midsummer-flags";
21
+ declare const FLAGS_COOKIE = "ms_flags";
22
+ /** Where flags are read from: a request, its headers, the header's value itself, or, left out, the browser's cookie. */
23
+ type FlagSource = Request | Headers | {
24
+ readonly headers: Headers | Readonly<Record<string, string | string[] | undefined>>;
25
+ } | string | undefined;
26
+ /** The flags Midsummer decided for one request or page. */
27
+ interface Flags {
28
+ /** Whether `key` is on for this visitor; `fallback` (false unless given) when Midsummer decided no flag of that key. */
29
+ on(key: string, fallback?: boolean): boolean;
30
+ /** Every flag Midsummer decided, by key. */
31
+ all(): Readonly<Record<string, boolean>>;
32
+ }
33
+ /** The flags in a header's or cookie's value: `key=1` or `key=0`, comma separated. */
34
+ declare function parseFlags(value: string | undefined | null): Readonly<Record<string, boolean>>;
35
+ /** This request's flags (from `source`), or in the browser, the page's (from its cookie). */
36
+ declare function flags(source?: FlagSource): Flags;
37
+ //#endregion
38
+ export { FLAGS_COOKIE, FLAGS_HEADER, FlagSource, Flags, flags, parseFlags };
package/flags.mjs ADDED
@@ -0,0 +1,52 @@
1
+ //#region src/flags.ts
2
+ /**
3
+ * A Project's flags, as Midsummer decided them for this visitor before the request reached your code. Change a flag
4
+ * with `midsummer deploy flag set` or on the Project's Flags page; the next request has it, with no deploy.
5
+ *
6
+ * import { flags } from "@midsummer-ai/sdk/flags"
7
+ *
8
+ * // On the server, from the request:
9
+ * if (flags(request).on("new-checkout")) return newCheckout(request)
10
+ *
11
+ * // In the browser, from the page's cookie:
12
+ * if (flags().on("new-banner")) showBanner()
13
+ *
14
+ * On the server they come in the `x-midsummer-flags` header, which only Midsummer sets: a visitor's own copy never
15
+ * reaches your code. In the browser they come in the `ms_flags` cookie, which every page Midsummer serves sets when
16
+ * it changes; a script can read it, so decide anything that matters on the server. Away from Midsummer (`midsummer
17
+ * dev`, tests) there are none, and each flag is the fallback you give.
18
+ */
19
+ /** The header Midsummer sends your server each request's flags in, and the cookie it keeps them in for the browser. */
20
+ const FLAGS_HEADER = "x-midsummer-flags";
21
+ const FLAGS_COOKIE = "ms_flags";
22
+ /** The flags in a header's or cookie's value: `key=1` or `key=0`, comma separated. */
23
+ function parseFlags(value) {
24
+ const decided = {};
25
+ for (const part of (value ?? "").split(",")) {
26
+ const [key, on] = part.trim().split("=");
27
+ if (key && (on === "1" || on === "0")) decided[key] = on === "1";
28
+ }
29
+ return decided;
30
+ }
31
+ /** The header's value from whatever holds the request's headers. */
32
+ function headerOf(source) {
33
+ const headers = source instanceof Headers ? source : "headers" in source ? source.headers : void 0;
34
+ if (headers instanceof Headers) return headers.get("x-midsummer-flags") ?? void 0;
35
+ const value = headers?.[FLAGS_HEADER];
36
+ return Array.isArray(value) ? value[0] : value;
37
+ }
38
+ /** The browser's `ms_flags` cookie, where there is a document. */
39
+ function cookie() {
40
+ const found = globalThis.document?.cookie?.split(";").map((part) => part.trim()).find((part) => part.startsWith(`${FLAGS_COOKIE}=`));
41
+ return found === void 0 ? void 0 : decodeURIComponent(found.slice(9));
42
+ }
43
+ /** This request's flags (from `source`), or in the browser, the page's (from its cookie). */
44
+ function flags(source) {
45
+ const decided = parseFlags(source === void 0 ? cookie() : typeof source === "string" ? source : headerOf(source));
46
+ return {
47
+ on: (key, fallback = false) => decided[key] ?? fallback,
48
+ all: () => decided
49
+ };
50
+ }
51
+ //#endregion
52
+ export { FLAGS_COOKIE, FLAGS_HEADER, flags, parseFlags };
package/images.d.mts ADDED
@@ -0,0 +1,30 @@
1
+ //#region src/images.d.ts
2
+ /**
3
+ * Addresses of a Project's images, transformed by Midsummer for every framework: scaled to a width, in a format the
4
+ * browser takes, and kept at the edge once made.
5
+ *
6
+ * import { imageUrl, imageSrcSet } from "@midsummer-ai/sdk/images"
7
+ *
8
+ * <img src={imageUrl("/hero.png", { width: 640 })} srcSet={imageSrcSet("/hero.png", [640, 1280])} sizes="100vw" alt="" />
9
+ *
10
+ * `src` is a path of the same site, or an https address on a host the Project's midsummer.json lists in
11
+ * `images.domains`. A width rounds up to the next one the site allows (`images.sizes`, else `IMAGE_WIDTHS`), and an
12
+ * image is never made wider than it is. Away from Midsummer the address serves nothing, so `midsummer dev` serves
13
+ * the source as it is.
14
+ */
15
+ /** Where every site serves image transforms. */
16
+ declare const IMAGE_PATH = "/_midsummer/image";
17
+ /** The widths images are made at unless the Project's midsummer.json sets its own in `images.sizes`. */
18
+ declare const IMAGE_WIDTHS: ReadonlyArray<number>;
19
+ /** How to make an image: its width in pixels, its quality from 1 to 100 (75 unless given) and its format, the best the browser takes unless given. */
20
+ interface ImageOptions {
21
+ readonly width?: number;
22
+ readonly quality?: number;
23
+ readonly format?: "avif" | "webp" | "jpeg" | "png";
24
+ }
25
+ /** The address of `src` made as `options` say. */
26
+ declare function imageUrl(src: string, options?: ImageOptions): string;
27
+ /** A `srcset` of `src` at each of `widths`, as `<url> <width>w`, for the browser to pick the one that fits. */
28
+ declare function imageSrcSet(src: string, widths?: ReadonlyArray<number>, options?: Omit<ImageOptions, "width">): string;
29
+ //#endregion
30
+ export { IMAGE_PATH, IMAGE_WIDTHS, ImageOptions, imageSrcSet, imageUrl };
package/images.mjs ADDED
@@ -0,0 +1,52 @@
1
+ //#region src/images.ts
2
+ /**
3
+ * Addresses of a Project's images, transformed by Midsummer for every framework: scaled to a width, in a format the
4
+ * browser takes, and kept at the edge once made.
5
+ *
6
+ * import { imageUrl, imageSrcSet } from "@midsummer-ai/sdk/images"
7
+ *
8
+ * <img src={imageUrl("/hero.png", { width: 640 })} srcSet={imageSrcSet("/hero.png", [640, 1280])} sizes="100vw" alt="" />
9
+ *
10
+ * `src` is a path of the same site, or an https address on a host the Project's midsummer.json lists in
11
+ * `images.domains`. A width rounds up to the next one the site allows (`images.sizes`, else `IMAGE_WIDTHS`), and an
12
+ * image is never made wider than it is. Away from Midsummer the address serves nothing, so `midsummer dev` serves
13
+ * the source as it is.
14
+ */
15
+ /** Where every site serves image transforms. */
16
+ const IMAGE_PATH = "/_midsummer/image";
17
+ /** The widths images are made at unless the Project's midsummer.json sets its own in `images.sizes`. */
18
+ const IMAGE_WIDTHS = [
19
+ 16,
20
+ 32,
21
+ 48,
22
+ 64,
23
+ 96,
24
+ 128,
25
+ 256,
26
+ 384,
27
+ 640,
28
+ 750,
29
+ 828,
30
+ 1080,
31
+ 1200,
32
+ 1920,
33
+ 2048,
34
+ 3840
35
+ ];
36
+ /** The address of `src` made as `options` say. */
37
+ function imageUrl(src, options = {}) {
38
+ const query = new URLSearchParams({ url: src });
39
+ if (options.width !== void 0) query.set("w", String(Math.max(1, Math.round(options.width))));
40
+ if (options.quality !== void 0) query.set("q", String(Math.min(100, Math.max(1, Math.round(options.quality)))));
41
+ if (options.format !== void 0) query.set("f", options.format);
42
+ return `${IMAGE_PATH}?${query}`;
43
+ }
44
+ /** A `srcset` of `src` at each of `widths`, as `<url> <width>w`, for the browser to pick the one that fits. */
45
+ function imageSrcSet(src, widths = IMAGE_WIDTHS, options = {}) {
46
+ return widths.map((width) => `${imageUrl(src, {
47
+ ...options,
48
+ width
49
+ })} ${width}w`).join(", ");
50
+ }
51
+ //#endregion
52
+ export { IMAGE_PATH, IMAGE_WIDTHS, imageSrcSet, imageUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.3356",
3
+ "version": "0.1.0-staging.3362",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Midsummer's handles for an app's code: its Database, Storage, Queues, Vector DB, AI, Email and Agents, with stand-ins for tests.",
@@ -146,6 +146,14 @@
146
146
  "./vector/wire": {
147
147
  "types": "./vector/wire.d.mts",
148
148
  "import": "./vector/wire.mjs"
149
+ },
150
+ "./flags": {
151
+ "types": "./flags.d.mts",
152
+ "import": "./flags.mjs"
153
+ },
154
+ "./images": {
155
+ "types": "./images.d.mts",
156
+ "import": "./images.mjs"
149
157
  }
150
158
  },
151
159
  "types": "./index.d.mts",
@@ -249,6 +257,12 @@
249
257
  ],
250
258
  "vector/wire": [
251
259
  "./vector/wire.d.mts"
260
+ ],
261
+ "flags": [
262
+ "./flags.d.mts"
263
+ ],
264
+ "images": [
265
+ "./images.d.mts"
252
266
  ]
253
267
  }
254
268
  },