@midsummer-ai/sdk 0.1.0-staging.4201 → 0.1.0-staging.4204

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/here-C2jhTheQ.mjs CHANGED
@@ -12,7 +12,7 @@ function processEnv() {
12
12
  * This package's version: `0.0.0-dev` here, and in a published package the version it was published at, which the
13
13
  * release writes in its place as it stages the package (`stampSdk` in `scripts/release/packages.ts`).
14
14
  */
15
- const SDK_VERSION = "0.1.0-staging.4201";
15
+ const SDK_VERSION = "0.1.0-staging.4204";
16
16
  /**
17
17
  * The header that tells Midsummer which client made each of this package's calls: `Midsummer-Client: sdk/<version>
18
18
  * <part>`, `part` the product whose client it is (`database`, `storage`, `queues`, ...), so the platform counts which
package/inert.d.mts ADDED
@@ -0,0 +1,28 @@
1
+ //#region src/inert.d.ts
2
+ /**
3
+ * How bytes someone else wrote (a Bucket's Objects, a repository's files, an attachment) are served from the
4
+ * platform's own hosts, where a page would run as whoever opens it: never as a page. Every route that serves such
5
+ * bytes from the API's host takes its headers from `inertHeaders`, so they are decided in one place.
6
+ *
7
+ * new Response(body, { headers: { ...inertHeaders(object.contentType, { name: "photo.png" }), "content-length": "1024" } })
8
+ * inertHeaders("text/html") // attachment, sandboxed, nosniff: a link to it downloads
9
+ * inertHeaders("image/png") // inline, sandboxed, nosniff: an <img> or a tab shows it
10
+ * inertHeaders(null) // application/octet-stream, attachment
11
+ * inertHeaders("image/png", { download: true }) // a download route: attachment whatever the type
12
+ *
13
+ * The decision is made on the media type as a browser would parse it: lowercased, without parameters. A declared
14
+ * type that isn't one plain `type/subtype` (a list such as `text/plain, text/html`, which a browser reads as its
15
+ * last, or anything malformed) is served as `application/octet-stream`.
16
+ *
17
+ * What a browser shows inline is only what can't run: raster images, audio and video, fonts and plain text.
18
+ * Everything else, PDF included (a PDF viewer runs the document's own script and forms, and browsers differ in
19
+ * whether they hold a sandboxed one to it), and every type not named here, is a download. The sandbox and nosniff
20
+ * apply to all of them. Neither changes how a page elsewhere uses the bytes: `<img>`, `<video>`, `<audio>`, fonts
21
+ * and `fetch` ignore both the disposition and the policy of what they load.
22
+ */
23
+ declare function inertHeaders(declared: string | null | undefined, options?: {
24
+ readonly name?: string;
25
+ readonly download?: boolean;
26
+ }): Record<string, string>;
27
+ //#endregion
28
+ export { inertHeaders };
package/inert.mjs ADDED
@@ -0,0 +1,80 @@
1
+ //#region src/inert.ts
2
+ /**
3
+ * How bytes someone else wrote (a Bucket's Objects, a repository's files, an attachment) are served from the
4
+ * platform's own hosts, where a page would run as whoever opens it: never as a page. Every route that serves such
5
+ * bytes from the API's host takes its headers from `inertHeaders`, so they are decided in one place.
6
+ *
7
+ * new Response(body, { headers: { ...inertHeaders(object.contentType, { name: "photo.png" }), "content-length": "1024" } })
8
+ * inertHeaders("text/html") // attachment, sandboxed, nosniff: a link to it downloads
9
+ * inertHeaders("image/png") // inline, sandboxed, nosniff: an <img> or a tab shows it
10
+ * inertHeaders(null) // application/octet-stream, attachment
11
+ * inertHeaders("image/png", { download: true }) // a download route: attachment whatever the type
12
+ *
13
+ * The decision is made on the media type as a browser would parse it: lowercased, without parameters. A declared
14
+ * type that isn't one plain `type/subtype` (a list such as `text/plain, text/html`, which a browser reads as its
15
+ * last, or anything malformed) is served as `application/octet-stream`.
16
+ *
17
+ * What a browser shows inline is only what can't run: raster images, audio and video, fonts and plain text.
18
+ * Everything else, PDF included (a PDF viewer runs the document's own script and forms, and browsers differ in
19
+ * whether they hold a sandboxed one to it), and every type not named here, is a download. The sandbox and nosniff
20
+ * apply to all of them. Neither changes how a page elsewhere uses the bytes: `<img>`, `<video>`, `<audio>`, fonts
21
+ * and `fetch` ignore both the disposition and the policy of what they load.
22
+ */
23
+ function inertHeaders(declared, options = {}) {
24
+ const type = mediaTypeOf(declared);
25
+ const inline = !options.download && type !== void 0 && PASSIVE.has(type.essence);
26
+ return {
27
+ "content-type": type ? type.value : "application/octet-stream",
28
+ "x-content-type-options": "nosniff",
29
+ "content-security-policy": "sandbox; default-src 'none'",
30
+ ...inline ? {} : { "content-disposition": options.name ? `attachment; filename*=UTF-8''${encodeURIComponent(options.name).replace(/['()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`)}` : "attachment" }
31
+ };
32
+ }
33
+ /** Media types a browser shows without running anything: shown inline, sandboxed all the same. */
34
+ const PASSIVE = /* @__PURE__ */ new Set([
35
+ "image/png",
36
+ "image/jpeg",
37
+ "image/gif",
38
+ "image/webp",
39
+ "image/avif",
40
+ "image/bmp",
41
+ "image/x-icon",
42
+ "image/vnd.microsoft.icon",
43
+ "video/mp4",
44
+ "video/webm",
45
+ "video/ogg",
46
+ "video/quicktime",
47
+ "audio/mpeg",
48
+ "audio/mp4",
49
+ "audio/aac",
50
+ "audio/ogg",
51
+ "audio/wav",
52
+ "audio/wave",
53
+ "audio/x-wav",
54
+ "audio/webm",
55
+ "audio/flac",
56
+ "font/woff",
57
+ "font/woff2",
58
+ "font/ttf",
59
+ "font/otf",
60
+ "text/plain"
61
+ ]);
62
+ const TOKEN = "[!#$%&'*+.^_`|~0-9A-Za-z-]+";
63
+ const MEDIA_TYPE = new RegExp(`^(${TOKEN})/(${TOKEN})\\s*((?:;\\s*${TOKEN}=(?:${TOKEN}|"[^"\\\\]*")\\s*)*)$`);
64
+ const CHARSET = new RegExp(`;\\s*charset=(${TOKEN})\\s*(?:;|$)`, "i");
65
+ /**
66
+ * `declared` as one media type: its lowercased essence, and the value to send (the essence and its charset,
67
+ * if it names one). Undefined when it is missing or isn't exactly one well-formed type.
68
+ */
69
+ function mediaTypeOf(declared) {
70
+ const match = MEDIA_TYPE.exec(declared?.trim() ?? "");
71
+ if (!match) return void 0;
72
+ const essence = `${match[1]}/${match[2]}`.toLowerCase();
73
+ const charset = CHARSET.exec(match[3] ?? "")?.[1];
74
+ return {
75
+ essence,
76
+ value: charset ? `${essence}; charset=${charset.toLowerCase()}` : essence
77
+ };
78
+ }
79
+ //#endregion
80
+ export { inertHeaders };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midsummer-ai/sdk",
3
- "version": "0.1.0-staging.4201",
3
+ "version": "0.1.0-staging.4204",
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.",
@@ -31,6 +31,10 @@
31
31
  "types": "./s3.d.mts",
32
32
  "import": "./s3.mjs"
33
33
  },
34
+ "./inert": {
35
+ "types": "./inert.d.mts",
36
+ "import": "./inert.mjs"
37
+ },
34
38
  "./socket": {
35
39
  "types": "./socket.d.mts",
36
40
  "import": "./socket.mjs"
@@ -247,6 +251,9 @@
247
251
  "s3": [
248
252
  "./s3.d.mts"
249
253
  ],
254
+ "inert": [
255
+ "./inert.d.mts"
256
+ ],
250
257
  "socket": [
251
258
  "./socket.d.mts"
252
259
  ],
package/s3.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { t as StorageError } from "./errors-B1tO_wdm.mjs";
2
2
  import { a as checkMetadata, l as checkUploadId, m as sha256, n as checkBucketName, o as checkPartNumber, r as checkKey, s as checkParts } from "./names-Do0ot5Ha.mjs";
3
3
  import { i as streamsOf } from "./src-C1ZmZ4oB.mjs";
4
+ import { inertHeaders } from "./inert.mjs";
4
5
  //#region src/s3.ts
5
6
  /**
6
7
  * Storage over the S3 protocol, so the tools and SDKs that speak it reach a Project's Buckets
@@ -47,6 +48,11 @@ import { i as streamsOf } from "./src-C1ZmZ4oB.mjs";
47
48
  * every chunk's signature, the trailer's, and a CRC32 or CRC32C trailing checksum are checked before
48
49
  * the Object is kept. Any other streaming mode, or another trailing checksum, is `NotImplemented`.
49
50
  *
51
+ * An Object is served from the platform's own host, so never as a page (`inertHeaders`): always nosniff and
52
+ * sandboxed, and anything but a plainly passive type (an image, audio, video, a font, plain text) as an attachment, so
53
+ * a link to an HTML or SVG Object downloads it while `<img src>` to a presigned URL still shows an image. The
54
+ * uploader's `Content-Type` is sent as the media type it parses to; the `response-content-*` overrides are not taken.
55
+ *
50
56
  * Not yet: Content-MD5.
51
57
  */
52
58
  const XMLNS = "http://s3.amazonaws.com/doc/2006-03-01/";
@@ -726,10 +732,15 @@ function rangeOf(header) {
726
732
  length: last - offset + 1
727
733
  };
728
734
  }
735
+ /**
736
+ * An Object's headers on a GetObject or HeadObject answer. Its bytes are the uploader's, answered from the API's own
737
+ * host, so its type, disposition and sandbox are `inertHeaders`': a link to an HTML or SVG Object downloads, an image
738
+ * still shows.
739
+ */
729
740
  function objectHeaders(object) {
730
741
  const headers = {
731
742
  etag: `"${object.etag}"`,
732
- "content-type": object.contentType,
743
+ ...inertHeaders(object.contentType, { name: object.key.split("/").at(-1) }),
733
744
  "last-modified": new Date(object.uploadedAt).toUTCString(),
734
745
  "accept-ranges": "bytes"
735
746
  };
package/storage/files.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { t as StorageError } from "../errors-B1tO_wdm.mjs";
2
2
  import { n as checkBucketName } from "../names-Do0ot5Ha.mjs";
3
3
  import { t as clientHeaders } from "../here-C2jhTheQ.mjs";
4
+ import { inertHeaders } from "../inert.mjs";
4
5
  import { DatabaseError } from "../database/errors.mjs";
5
6
  import { i as verifier, r as trustedOf, t as authConfigOf } from "../auth-1W-SzpD3.mjs";
6
7
  //#region src/storage/files.ts
@@ -211,8 +212,6 @@ function fileRefusal(access, ask) {
211
212
  const FILES_BASE = "/storage/app";
212
213
  /** Whether a path on the API origin is the files endpoint's. */
213
214
  const isAppPath = (path) => path === "/storage/app" || path.startsWith(`/storage/app/`);
214
- /** Kinds of media a browser runs as a page: served from the API's origin, they are only ever downloaded. */
215
- const ACTIVE = /^(?:text\/html|application\/xhtml\+xml|image\/svg\+xml|text\/xml|application\/xml|text\/javascript|application\/javascript)\b/iu;
216
215
  const CORS = {
217
216
  "Access-Control-Allow-Origin": "*",
218
217
  "Access-Control-Expose-Headers": "ETag, Content-Length, Content-Type, Last-Modified"
@@ -361,14 +360,11 @@ function filesServer(options) {
361
360
  }) === void 0;
362
361
  const headers = {
363
362
  ...CORS,
364
- "Content-Type": head.contentType,
363
+ ...inertHeaders(head.contentType, { name: key.split("/").at(-1) }),
365
364
  "Content-Length": String(head.size),
366
365
  ETag: `"${head.etag}"`,
367
366
  "Cache-Control": open ? "no-cache" : "private, no-store",
368
- "Last-Modified": new Date(head.uploadedAt).toUTCString(),
369
- "X-Content-Type-Options": "nosniff",
370
- "Content-Security-Policy": "sandbox; default-src 'none'",
371
- ...ACTIVE.test(head.contentType) ? { "Content-Disposition": `attachment; filename*=UTF-8''${encodeURIComponent(key.split("/").at(-1))}` } : {}
367
+ "Last-Modified": new Date(head.uploadedAt).toUTCString()
372
368
  };
373
369
  if (request.headers.get("If-None-Match")?.replaceAll("\"", "") === head.etag) return new Response(null, {
374
370
  status: 304,