foldkit-media 0.3.1 → 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/README.md +65 -4
- package/dist/{asset-BLo8VWmd.d.mts → asset.d.ts} +10 -11
- package/dist/asset.d.ts.map +1 -0
- package/dist/asset.js +66 -0
- package/dist/asset.js.map +1 -0
- package/dist/browser/index.d.ts +15 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +69 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/browser/{index.d.mts → upload.d.ts} +5 -16
- package/dist/browser/upload.d.ts.map +1 -0
- package/dist/browser/upload.js +87 -0
- package/dist/browser/upload.js.map +1 -0
- package/dist/cloudflare/{index.d.mts → index.d.ts} +8 -6
- package/dist/cloudflare/index.d.ts.map +1 -0
- package/dist/cloudflare/{index.mjs → index.js} +35 -15
- package/dist/cloudflare/index.js.map +1 -0
- package/dist/describe.d.ts +32 -0
- package/dist/describe.d.ts.map +1 -0
- package/dist/describe.js +30 -0
- package/dist/describe.js.map +1 -0
- package/dist/format.d.ts +12 -0
- package/dist/format.d.ts.map +1 -0
- package/dist/{format-BCRTFU9j.mjs → format.js} +2 -2
- package/dist/format.js.map +1 -0
- package/dist/{icon-BGtxExTN.d.mts → icon.d.ts} +14 -17
- package/dist/icon.d.ts.map +1 -0
- package/dist/{icon-0A2dBtqj.mjs → icon.js} +2 -12
- package/dist/icon.js.map +1 -0
- package/dist/icons/{index.d.mts → index.d.ts} +2 -2
- package/dist/icons/index.d.ts.map +1 -0
- package/dist/icons/{index.mjs → index.js} +2 -2
- package/dist/icons/index.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +10 -0
- package/dist/layout.d.ts +60 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +72 -0
- package/dist/layout.js.map +1 -0
- package/dist/limits.d.ts +3 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/{limits-B4hHpyfE.mjs → limits.js} +2 -2
- package/dist/limits.js.map +1 -0
- package/dist/picture.d.ts +88 -0
- package/dist/picture.d.ts.map +1 -0
- package/dist/picture.js +113 -0
- package/dist/picture.js.map +1 -0
- package/dist/placeholder.d.ts +16 -0
- package/dist/placeholder.d.ts.map +1 -0
- package/dist/placeholder.js +38 -0
- package/dist/placeholder.js.map +1 -0
- package/dist/{provider-CB0nqu0m.d.mts → provider.d.ts} +16 -19
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +93 -0
- package/dist/provider.js.map +1 -0
- package/dist/server/admission.d.ts +31 -0
- package/dist/server/admission.d.ts.map +1 -0
- package/dist/server/admission.js +41 -0
- package/dist/server/admission.js.map +1 -0
- package/dist/server/api.d.ts +14 -0
- package/dist/server/api.d.ts.map +1 -0
- package/dist/server/api.js +21 -0
- package/dist/server/api.js.map +1 -0
- package/dist/server/diagnostics.d.ts +53 -0
- package/dist/server/diagnostics.d.ts.map +1 -0
- package/dist/server/diagnostics.js +50 -0
- package/dist/server/diagnostics.js.map +1 -0
- package/dist/server/errors.d.ts +24 -0
- package/dist/server/errors.d.ts.map +1 -0
- package/dist/{errors-BhEgXJ4m.mjs → server/errors.js} +2 -2
- package/dist/server/errors.js.map +1 -0
- package/dist/server/facts.d.ts +36 -0
- package/dist/server/facts.d.ts.map +1 -0
- package/dist/server/facts.js +126 -0
- package/dist/server/facts.js.map +1 -0
- package/dist/server/fetch.d.ts +29 -0
- package/dist/server/fetch.d.ts.map +1 -0
- package/dist/server/fetch.js +32 -0
- package/dist/server/fetch.js.map +1 -0
- package/dist/server/handlers.d.ts +48 -0
- package/dist/server/handlers.d.ts.map +1 -0
- package/dist/server/handlers.js +151 -0
- package/dist/server/handlers.js.map +1 -0
- package/dist/server/headers.d.ts +47 -0
- package/dist/server/headers.d.ts.map +1 -0
- package/dist/server/headers.js +134 -0
- package/dist/server/headers.js.map +1 -0
- package/dist/server/index.d.ts +13 -0
- package/dist/server/index.js +12 -0
- package/dist/server/materialize.d.ts +36 -0
- package/dist/server/materialize.d.ts.map +1 -0
- package/dist/server/materialize.js +62 -0
- package/dist/server/materialize.js.map +1 -0
- package/dist/{services-BZoK2VOY.d.mts → server/services.d.ts} +12 -12
- package/dist/server/services.d.ts.map +1 -0
- package/dist/{services-7B4hsQM7.mjs → server/services.js} +3 -3
- package/dist/server/services.js.map +1 -0
- package/dist/server/stores.d.ts +27 -0
- package/dist/server/stores.d.ts.map +1 -0
- package/dist/server/stores.js +62 -0
- package/dist/server/stores.js.map +1 -0
- package/dist/server/upload.d.ts +113 -0
- package/dist/server/upload.d.ts.map +1 -0
- package/dist/server/upload.js +185 -0
- package/dist/server/upload.js.map +1 -0
- package/dist/sharp/{index.d.mts → index.d.ts} +3 -3
- package/dist/sharp/index.d.ts.map +1 -0
- package/dist/sharp/{index.mjs → index.js} +11 -9
- package/dist/sharp/index.js.map +1 -0
- package/dist/unpic/{index.d.mts → index.d.ts} +5 -4
- package/dist/unpic/index.d.ts.map +1 -0
- package/dist/unpic/{index.mjs → index.js} +3 -3
- package/dist/unpic/index.js.map +1 -0
- package/dist/{variant-B-vyMmFB.d.mts → variant.d.ts} +15 -18
- package/dist/variant.d.ts.map +1 -0
- package/dist/variant.js +75 -0
- package/dist/variant.js.map +1 -0
- package/dist/vite/{index.d.mts → icons.d.ts} +1 -19
- package/dist/vite/icons.d.ts.map +1 -0
- package/dist/vite/{index.mjs → icons.js} +7 -168
- package/dist/vite/icons.js.map +1 -0
- package/dist/vite/images.d.ts +20 -0
- package/dist/vite/images.d.ts.map +1 -0
- package/dist/vite/images.js +167 -0
- package/dist/vite/images.js.map +1 -0
- package/dist/vite/index.d.ts +3 -0
- package/dist/vite/index.js +3 -0
- package/dist/widths.d.ts +22 -0
- package/dist/widths.d.ts.map +1 -0
- package/dist/{widths-qp3jshiF.mjs → widths.js} +2 -8
- package/dist/widths.js.map +1 -0
- package/package.json +23 -22
- package/dist/asset-BLo8VWmd.d.mts.map +0 -1
- package/dist/browser/index.d.mts.map +0 -1
- package/dist/browser/index.mjs +0 -150
- package/dist/browser/index.mjs.map +0 -1
- package/dist/cloudflare/index.d.mts.map +0 -1
- package/dist/cloudflare/index.mjs.map +0 -1
- package/dist/describe-BOgc0xbR.d.mts +0 -52
- package/dist/describe-BOgc0xbR.d.mts.map +0 -1
- package/dist/describe-C6sWA_8p.mjs +0 -171
- package/dist/describe-C6sWA_8p.mjs.map +0 -1
- package/dist/errors-BhEgXJ4m.mjs.map +0 -1
- package/dist/format-BCRTFU9j.mjs.map +0 -1
- package/dist/icon-0A2dBtqj.mjs.map +0 -1
- package/dist/icon-BGtxExTN.d.mts.map +0 -1
- package/dist/icons/index.d.mts.map +0 -1
- package/dist/icons/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -157
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -322
- package/dist/index.mjs.map +0 -1
- package/dist/limits-B4hHpyfE.mjs.map +0 -1
- package/dist/provider-CB0nqu0m.d.mts.map +0 -1
- package/dist/rolldown-runtime-BJohB4LM.mjs +0 -13
- package/dist/server/index.d.mts +0 -170
- package/dist/server/index.d.mts.map +0 -1
- package/dist/server/index.mjs +0 -231
- package/dist/server/index.mjs.map +0 -1
- package/dist/services-7B4hsQM7.mjs.map +0 -1
- package/dist/services-BZoK2VOY.d.mts.map +0 -1
- package/dist/sharp/index.d.mts.map +0 -1
- package/dist/sharp/index.mjs.map +0 -1
- package/dist/stores-CxraN3kt.mjs +0 -490
- package/dist/stores-CxraN3kt.mjs.map +0 -1
- package/dist/unpic/index.d.mts.map +0 -1
- package/dist/unpic/index.mjs.map +0 -1
- package/dist/upload-lVLMRJoC.d.mts +0 -160
- package/dist/upload-lVLMRJoC.d.mts.map +0 -1
- package/dist/variant-B-vyMmFB.d.mts.map +0 -1
- package/dist/vite/index.d.mts.map +0 -1
- package/dist/vite/index.mjs.map +0 -1
- package/dist/widths-qp3jshiF.mjs.map +0 -1
- package/dist/widths-upkgLdek.d.mts +0 -36
- package/dist/widths-upkgLdek.d.mts.map +0 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Cache, Cause, Effect, Layer, Option, Schema } from "effect";
|
|
2
|
+
import * as Variant$1 from "../variant.js";
|
|
3
|
+
import { nonNegativeInteger } from "../limits.js";
|
|
4
|
+
import { OriginalTooLarge } from "./errors.js";
|
|
5
|
+
import { Diagnostics } from "foldkit-surface";
|
|
6
|
+
import { Admission } from "./admission.js";
|
|
7
|
+
import { requestDefect } from "./diagnostics.js";
|
|
8
|
+
import { Originals, Transformer, Variants } from "./services.js";
|
|
9
|
+
import { HttpApiBuilder } from "effect/http-api";
|
|
10
|
+
import { HttpRouter, HttpServer, HttpServerResponse } from "effect/http";
|
|
11
|
+
import { MediaApi } from "./api.js";
|
|
12
|
+
import { Answer, headersFor, matches, statusOf } from "./headers.js";
|
|
13
|
+
// src/server/handlers.ts
|
|
14
|
+
/** The Cloudflare Images binding reads at most 20 MB; a store's own limit may be lower. */
|
|
15
|
+
const DEFAULT_MAX_BYTES = 20971520;
|
|
16
|
+
const defaultClientOf = (request) => request.headers["cf-connecting-ip"] ?? Option.getOrElse(request.remoteAddress, () => "unknown");
|
|
17
|
+
const optionsOf = (answer) => ({
|
|
18
|
+
status: statusOf(answer),
|
|
19
|
+
headers: headersFor(answer)
|
|
20
|
+
});
|
|
21
|
+
const served = (etag, from, encoded) => HttpServerResponse.uint8Array(encoded.bytes, {
|
|
22
|
+
...optionsOf(Answer.cases.Variant.make({
|
|
23
|
+
etag,
|
|
24
|
+
from
|
|
25
|
+
})),
|
|
26
|
+
contentType: encoded.contentType
|
|
27
|
+
});
|
|
28
|
+
const notModified = (etag) => HttpServerResponse.empty(optionsOf(Answer.cases.NotModified.make({ etag })));
|
|
29
|
+
const refused = (answer) => HttpServerResponse.jsonUnsafe({ error: answer._tag }, optionsOf(answer));
|
|
30
|
+
/**
|
|
31
|
+
* The handlers of `MediaApi`'s one route. The path is decoded against the
|
|
32
|
+
* space; a variant kept is served as kept, and one the request already holds
|
|
33
|
+
* (`If-None-Match`) is a 304 before the store is read; otherwise the original
|
|
34
|
+
* is read, transformed, kept and served. Every answer, a refusal included,
|
|
35
|
+
* goes through `headersFor`, and a failure is never the original in the
|
|
36
|
+
* variant's place.
|
|
37
|
+
*/
|
|
38
|
+
const handlers = (options = {}) => {
|
|
39
|
+
const { maxBytes: requestedMaxBytes = DEFAULT_MAX_BYTES, coalesce = false, clientOf = defaultClientOf } = options;
|
|
40
|
+
const space = options.provider?.space ?? Variant$1.standard;
|
|
41
|
+
const maxBytes = nonNegativeInteger("maxBytes", requestedMaxBytes);
|
|
42
|
+
const decodeHash = Schema.decodeUnknownOption(Variant$1.ContentHash);
|
|
43
|
+
const decodeVariant = Schema.decodeUnknownOption(space.schema);
|
|
44
|
+
const isSpelling = Schema.is(Variant$1.Spelling);
|
|
45
|
+
const spell = Schema.encodeSync(space.schema);
|
|
46
|
+
return HttpApiBuilder.group(MediaApi, "images", (group) => Effect.gen(function* () {
|
|
47
|
+
const originals = yield* Originals;
|
|
48
|
+
const transformer = yield* Transformer;
|
|
49
|
+
const variants = yield* Variants;
|
|
50
|
+
const admit = Option.match(yield* Effect.serviceOption(Admission), {
|
|
51
|
+
onNone: () => (_client) => Effect.void,
|
|
52
|
+
onSome: (admission) => admission.admit
|
|
53
|
+
});
|
|
54
|
+
const make = Effect.fn("Media.make")(function* (hash, variant) {
|
|
55
|
+
const original = yield* originals.get(hash);
|
|
56
|
+
if (original.size > maxBytes) return yield* new OriginalTooLarge({
|
|
57
|
+
size: original.size,
|
|
58
|
+
limit: maxBytes,
|
|
59
|
+
unit: "bytes"
|
|
60
|
+
});
|
|
61
|
+
return yield* transformer.transform(original, variant);
|
|
62
|
+
});
|
|
63
|
+
const makeAndKeep = (key, hash, variant) => make(hash, variant).pipe(Effect.tap((made) => variants.put(key, made)));
|
|
64
|
+
const shared = yield* Cache.make({
|
|
65
|
+
capacity: 1e4,
|
|
66
|
+
timeToLive: 0,
|
|
67
|
+
lookup: (key) => {
|
|
68
|
+
const at = key.indexOf("/");
|
|
69
|
+
const variant = Schema.decodeUnknownSync(space.schema)(key.slice(at + 1));
|
|
70
|
+
return makeAndKeep(key, key.slice(0, at), variant);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
const serve = Effect.fn("Media.serve")(function* (hash, variant, ifNoneMatch, client) {
|
|
74
|
+
const key = `${hash}/${spell(variant)}`;
|
|
75
|
+
const etag = `"${key}"`;
|
|
76
|
+
if (matches(ifNoneMatch, etag)) return notModified(etag);
|
|
77
|
+
return yield* Option.match(yield* variants.get(key), {
|
|
78
|
+
onSome: ({ encoded, from }) => Effect.succeed(served(etag, from, encoded)),
|
|
79
|
+
onNone: () => admit(client).pipe(Effect.andThen(coalesce ? Cache.get(shared, key) : makeAndKeep(key, hash, variant)), Effect.map((made) => served(etag, "made", made)), Effect.catchTags({
|
|
80
|
+
OriginalNotFound: () => Effect.succeed(refused(Answer.cases.NotFound.make({}))),
|
|
81
|
+
OriginalTooLarge: () => Effect.succeed(refused(Answer.cases.TooLarge.make({}))),
|
|
82
|
+
TransformFailed: () => Effect.succeed(refused(Answer.cases.Failed.make({}))),
|
|
83
|
+
QuotaExhausted: () => Effect.succeed(refused(Answer.cases.Exhausted.make({}))),
|
|
84
|
+
RateLimited: ({ retryAfterSeconds }) => Effect.succeed(refused(Answer.cases.RateLimited.make({ retryAfterSeconds })))
|
|
85
|
+
}))
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
/**
|
|
89
|
+
* A spelling of the grammar this space does not read. An earlier revision of a variant
|
|
90
|
+
* it makes is served while kept and else sent to its current URL; anything else (a later
|
|
91
|
+
* revision, a width another release offers) is `Unoffered`, kept briefly.
|
|
92
|
+
*/
|
|
93
|
+
const elsewhere = (hash, spelling, ifNoneMatch) => {
|
|
94
|
+
const current = Variant$1.respell(spelling, space.revision);
|
|
95
|
+
if (Variant$1.revisionOf(spelling) >= space.revision || Option.isNone(decodeVariant(current))) return Effect.succeed(refused(Answer.cases.Unoffered.make({})));
|
|
96
|
+
const key = `${hash}/${spelling}`;
|
|
97
|
+
const etag = `"${key}"`;
|
|
98
|
+
if (matches(ifNoneMatch, etag)) return Effect.succeed(notModified(etag));
|
|
99
|
+
return Effect.map(variants.get(key), (kept) => Option.match(kept, {
|
|
100
|
+
onSome: ({ encoded, from }) => served(etag, from, encoded),
|
|
101
|
+
onNone: () => refused(Answer.cases.Moved.make({ location: Variant$1.PATH.replace(":hash", hash).replace(":variant", current) }))
|
|
102
|
+
}));
|
|
103
|
+
};
|
|
104
|
+
return group.handle("variant", ({ params, request }) => Option.match(Option.all([decodeHash(params.hash), decodeVariant(params.variant)]), {
|
|
105
|
+
onNone: () => Option.match(decodeHash(params.hash), {
|
|
106
|
+
onNone: () => Effect.succeed(refused(Answer.cases.Malformed.make({}))),
|
|
107
|
+
onSome: (hash) => isSpelling(params.variant) ? elsewhere(hash, params.variant, request.headers["if-none-match"]) : Effect.succeed(refused(Answer.cases.Malformed.make({})))
|
|
108
|
+
}),
|
|
109
|
+
onSome: ([hash, variant]) => serve(hash, variant, request.headers["if-none-match"], clientOf(request))
|
|
110
|
+
}));
|
|
111
|
+
}));
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* The media server as an Effect HTTP application: its route registered with
|
|
115
|
+
* the `HttpRouter`, needing `Originals`, `Transformer` and `Variants`.
|
|
116
|
+
*/
|
|
117
|
+
const layer = (options = {}) => HttpApiBuilder.layer(MediaApi).pipe(Layer.provide(handlers({
|
|
118
|
+
coalesce: true,
|
|
119
|
+
...options
|
|
120
|
+
})));
|
|
121
|
+
/**
|
|
122
|
+
* The defects of a request answered 500. The router also dies with the 500
|
|
123
|
+
* response it answers, which is no defect of the request's.
|
|
124
|
+
*/
|
|
125
|
+
const reportDefects = (cause) => {
|
|
126
|
+
const defects = cause.reasons.filter((reason) => Cause.isDieReason(reason) && !HttpServerResponse.isHttpServerResponse(reason.defect));
|
|
127
|
+
return defects.length === 0 ? Effect.void : Diagnostics.report(requestDefect(Cause.fromReasons(defects)));
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* A Worker's fetch handler over `app`, built from each environment's bindings
|
|
131
|
+
* once and kept, since a Worker hands every request the same bindings. A
|
|
132
|
+
* layer that fails to build answers a 500. With the router's logger off, a
|
|
133
|
+
* defect answered 500 is reported as `RequestDefect`.
|
|
134
|
+
*/
|
|
135
|
+
const fetchOf = (app) => {
|
|
136
|
+
const built = /* @__PURE__ */ new WeakMap();
|
|
137
|
+
return (request, env) => {
|
|
138
|
+
const known = built.get(env);
|
|
139
|
+
if (known !== void 0) return known(request);
|
|
140
|
+
const { handler } = HttpRouter.toWebHandler(app(env).pipe(Layer.provide(HttpServer.layerServices)), {
|
|
141
|
+
disableLogger: true,
|
|
142
|
+
middleware: Effect.tapCause(reportDefects)
|
|
143
|
+
});
|
|
144
|
+
const answer = (incoming) => handler(incoming);
|
|
145
|
+
built.set(env, answer);
|
|
146
|
+
return answer(request);
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
export { fetchOf, handlers, layer };
|
|
150
|
+
|
|
151
|
+
//# sourceMappingURL=handlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.js","names":["Variant"],"sources":["../../src/server/handlers.ts"],"sourcesContent":["import { Cache, Cause, Effect, Layer, Option, Schema } from 'effect'\nimport { HttpRouter, HttpServer, HttpServerResponse, type HttpServerRequest } from 'effect/http'\nimport { HttpApiBuilder } from 'effect/http-api'\nimport { Diagnostics } from 'foldkit-surface'\nimport type { Hosted } from '../provider.js'\nimport * as Variant from '../variant.js'\nimport { nonNegativeInteger } from '../limits.js'\nimport { MediaApi } from './api.js'\nimport { Admission } from './admission.js'\nimport { requestDefect } from './diagnostics.js'\nimport { OriginalTooLarge } from './errors.js'\nimport { Answer, headersFor, matches, statusOf, type From } from './headers.js'\nimport { Originals, Transformer, Variants, type Encoded } from './services.js'\n\n/** The Cloudflare Images binding reads at most 20 MB; a store's own limit may be lower. */\nconst DEFAULT_MAX_BYTES = 20 * 1024 * 1024\n\nexport interface Options {\n /**\n * The `Provider.hosted` the pages write URLs with: the server makes every\n * variant of its space and nothing else. `Provider.hosted()` when omitted.\n */\n readonly provider?: Hosted\n /** The largest original read, in bytes; larger ones are a 413. */\n readonly maxBytes?: number\n /**\n * Concurrent misses for one variant share one transform and one write.\n * Only where requests may wait on one another's work: on by default for\n * `layer` (a Node server), off in `serveFetch`, since a Worker request\n * cannot wake another's.\n */\n readonly coalesce?: boolean\n /**\n * Who is asking, for `Admission`: by default Cloudflare's `CF-Connecting-IP`,\n * else the connection's address. `X-Forwarded-For` is the client's to write,\n * so read it here only behind a proxy that sets it.\n */\n readonly clientOf?: (request: HttpServerRequest.HttpServerRequest) => string\n}\n\nconst defaultClientOf = (request: HttpServerRequest.HttpServerRequest): string =>\n request.headers['cf-connecting-ip'] ?? Option.getOrElse(request.remoteAddress, () => 'unknown')\n\nconst optionsOf = (answer: Answer) => ({ status: statusOf(answer), headers: headersFor(answer) })\n\nconst served = (etag: string, from: From, encoded: Encoded) =>\n HttpServerResponse.uint8Array(encoded.bytes, {\n ...optionsOf(Answer.cases.Variant.make({ etag, from })),\n contentType: encoded.contentType,\n })\n\nconst notModified = (etag: string) =>\n HttpServerResponse.empty(optionsOf(Answer.cases.NotModified.make({ etag })))\n\n// A refusal names its kind, and nothing of the server's internals.\nconst refused = (answer: Answer) =>\n HttpServerResponse.jsonUnsafe({ error: answer._tag }, optionsOf(answer))\n\n/**\n * The handlers of `MediaApi`'s one route. The path is decoded against the\n * space; a variant kept is served as kept, and one the request already holds\n * (`If-None-Match`) is a 304 before the store is read; otherwise the original\n * is read, transformed, kept and served. Every answer, a refusal included,\n * goes through `headersFor`, and a failure is never the original in the\n * variant's place.\n */\nexport const handlers = (options: Options = {}) => {\n const {\n maxBytes: requestedMaxBytes = DEFAULT_MAX_BYTES,\n coalesce = false,\n clientOf = defaultClientOf,\n } = options\n const space = options.provider?.space ?? Variant.standard\n const maxBytes = nonNegativeInteger('maxBytes', requestedMaxBytes)\n const decodeHash = Schema.decodeUnknownOption(Variant.ContentHash)\n const decodeVariant = Schema.decodeUnknownOption(space.schema)\n const isSpelling = Schema.is(Variant.Spelling)\n const spell = Schema.encodeSync(space.schema)\n return HttpApiBuilder.group(MediaApi, 'images', group =>\n Effect.gen(function* () {\n // Resolved once, when the route is built, so a store kept in memory lives as long as the server.\n const originals = yield* Originals\n const transformer = yield* Transformer\n const variants = yield* Variants\n const admit = Option.match(yield* Effect.serviceOption(Admission), {\n onNone: () => (_client: string) => Effect.void,\n onSome: admission => admission.admit,\n })\n const make = Effect.fn('Media.make')(function* (hash: string, variant: Variant.Variant) {\n const original = yield* originals.get(hash)\n if (original.size > maxBytes) {\n return yield* new OriginalTooLarge({\n size: original.size,\n limit: maxBytes,\n unit: 'bytes',\n })\n }\n return yield* transformer.transform(original, variant)\n })\n const makeAndKeep = (key: string, hash: string, variant: Variant.Variant) =>\n make(hash, variant).pipe(Effect.tap(made => variants.put(key, made)))\n // In flight only: an entry expires as it settles, so nothing is kept here and no failure is remembered.\n const shared = yield* Cache.make({\n capacity: 10_000,\n timeToLive: 0,\n lookup: (key: string) => {\n // The key is the hash, a slash, and the spelling: neither holds a slash of its own.\n const at = key.indexOf('/')\n const variant = Schema.decodeUnknownSync(space.schema)(key.slice(at + 1))\n return makeAndKeep(key, key.slice(0, at), variant)\n },\n })\n const serve = Effect.fn('Media.serve')(function* (\n hash: string,\n variant: Variant.Variant,\n ifNoneMatch: string | undefined,\n client: string,\n ) {\n // The decoded request spelled again: one request, one key, one ETag.\n const key = `${hash}/${spell(variant)}`\n const etag = `\"${key}\"`\n if (matches(ifNoneMatch, etag)) return notModified(etag)\n return yield* Option.match(yield* variants.get(key), {\n onSome: ({ encoded, from }) => Effect.succeed(served(etag, from, encoded)),\n onNone: () =>\n admit(client).pipe(\n Effect.andThen(coalesce ? Cache.get(shared, key) : makeAndKeep(key, hash, variant)),\n Effect.map(made => served(etag, 'made', made)),\n Effect.catchTags({\n OriginalNotFound: () => Effect.succeed(refused(Answer.cases.NotFound.make({}))),\n OriginalTooLarge: () => Effect.succeed(refused(Answer.cases.TooLarge.make({}))),\n TransformFailed: () => Effect.succeed(refused(Answer.cases.Failed.make({}))),\n QuotaExhausted: () => Effect.succeed(refused(Answer.cases.Exhausted.make({}))),\n RateLimited: ({ retryAfterSeconds }) =>\n Effect.succeed(refused(Answer.cases.RateLimited.make({ retryAfterSeconds }))),\n }),\n ),\n })\n })\n /**\n * A spelling of the grammar this space does not read. An earlier revision of a variant\n * it makes is served while kept and else sent to its current URL; anything else (a later\n * revision, a width another release offers) is `Unoffered`, kept briefly.\n */\n const elsewhere = (hash: string, spelling: string, ifNoneMatch: string | undefined) => {\n const current = Variant.respell(spelling, space.revision)\n if (\n Variant.revisionOf(spelling) >= space.revision ||\n Option.isNone(decodeVariant(current))\n ) {\n return Effect.succeed(refused(Answer.cases.Unoffered.make({})))\n }\n const key = `${hash}/${spelling}`\n const etag = `\"${key}\"`\n if (matches(ifNoneMatch, etag)) return Effect.succeed(notModified(etag))\n return Effect.map(variants.get(key), kept =>\n Option.match(kept, {\n onSome: ({ encoded, from }) => served(etag, from, encoded),\n onNone: () =>\n refused(\n Answer.cases.Moved.make({\n location: Variant.PATH.replace(':hash', hash).replace(':variant', current),\n }),\n ),\n }),\n )\n }\n return group.handle('variant', ({ params, request }) =>\n Option.match(Option.all([decodeHash(params.hash), decodeVariant(params.variant)]), {\n // Kept a year only when no release could spell it; anything else is answered above.\n onNone: () =>\n Option.match(decodeHash(params.hash), {\n onNone: () => Effect.succeed(refused(Answer.cases.Malformed.make({}))),\n onSome: hash =>\n isSpelling(params.variant)\n ? elsewhere(hash, params.variant, request.headers['if-none-match'])\n : Effect.succeed(refused(Answer.cases.Malformed.make({}))),\n }),\n onSome: ([hash, variant]) =>\n serve(hash, variant, request.headers['if-none-match'], clientOf(request)),\n }),\n )\n }),\n )\n}\n\n/**\n * The media server as an Effect HTTP application: its route registered with\n * the `HttpRouter`, needing `Originals`, `Transformer` and `Variants`.\n */\nexport const layer = (options: Options = {}) =>\n HttpApiBuilder.layer(MediaApi).pipe(Layer.provide(handlers({ coalesce: true, ...options })))\n\n/**\n * The defects of a request answered 500. The router also dies with the 500\n * response it answers, which is no defect of the request's.\n */\nconst reportDefects = (cause: Cause.Cause<unknown>): Effect.Effect<void> => {\n const defects = cause.reasons.filter(\n reason => Cause.isDieReason(reason) && !HttpServerResponse.isHttpServerResponse(reason.defect),\n )\n return defects.length === 0\n ? Effect.void\n : Diagnostics.report(requestDefect(Cause.fromReasons(defects)))\n}\n\n/**\n * A Worker's fetch handler over `app`, built from each environment's bindings\n * once and kept, since a Worker hands every request the same bindings. A\n * layer that fails to build answers a 500. With the router's logger off, a\n * defect answered 500 is reported as `RequestDefect`.\n */\nexport const fetchOf = <Env extends object>(\n app: (\n env: Env,\n ) => Layer.Layer<\n never,\n unknown,\n HttpRouter.HttpRouter | Layer.Success<typeof HttpServer.layerServices>\n >,\n): ((request: Request, env: Env) => Promise<Response>) => {\n const built = new WeakMap<Env, (request: Request) => Promise<Response>>()\n return (request, env) => {\n const known = built.get(env)\n if (known !== undefined) return known(request)\n const { handler } = HttpRouter.toWebHandler(\n app(env).pipe(Layer.provide(HttpServer.layerServices)),\n { disableLogger: true, middleware: Effect.tapCause(reportDefects) },\n )\n const answer = (incoming: Request) => handler(incoming)\n built.set(env, answer)\n return answer(request)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAeA,MAAM,oBAAoB;AAyB1B,MAAM,mBAAmB,YACvB,QAAQ,QAAQ,uBAAuB,OAAO,UAAU,QAAQ,qBAAqB,SAAS;AAEhG,MAAM,aAAa,YAAoB;CAAE,QAAQ,SAAS,MAAM;CAAG,SAAS,WAAW,MAAM;AAAE;AAE/F,MAAM,UAAU,MAAc,MAAY,YACxC,mBAAmB,WAAW,QAAQ,OAAO;CAC3C,GAAG,UAAU,OAAO,MAAM,QAAQ,KAAK;EAAE;EAAM;CAAK,CAAC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GACH,SAAS,CAAC,MAAM,aACd,MAAM,MAAM,SAAS,QAAQ,QAAQ,kBAAkB,SAAS,OAAO,CAAC;EAC5E,CAAC,CACH;CACF,CAAC,CACH;AACF;;;;;AAMA,MAAa,SAAS,UAAmB,CAAC,MACxC,eAAe,MAAM,QAAQ,CAAC,CAAC,KAAK,MAAM,QAAQ,SAAS;CAAE,UAAU;CAAM,GAAG;AAAQ,CAAC,CAAC,CAAC;;;;;AAM7F,MAAM,iBAAiB,UAAqD;CAC1E,MAAM,UAAU,MAAM,QAAQ,QAC5B,WAAU,MAAM,YAAY,MAAM,KAAK,CAAC,mBAAmB,qBAAqB,OAAO,MAAM,CAC/F;CACA,OAAO,QAAQ,WAAW,IACtB,OAAO,OACP,YAAY,OAAO,cAAc,MAAM,YAAY,OAAO,CAAC,CAAC;AAClE;;;;;;;AAQA,MAAa,WACX,QAOwD;CACxD,MAAM,wBAAQ,IAAI,QAAsD;CACxE,QAAQ,SAAS,QAAQ;EACvB,MAAM,QAAQ,MAAM,IAAI,GAAG;EAC3B,IAAI,UAAU,KAAA,GAAW,OAAO,MAAM,OAAO;EAC7C,MAAM,EAAE,YAAY,WAAW,aAC7B,IAAI,GAAG,CAAC,CAAC,KAAK,MAAM,QAAQ,WAAW,aAAa,CAAC,GACrD;GAAE,eAAe;GAAM,YAAY,OAAO,SAAS,aAAa;EAAE,CACpE;EACA,MAAM,UAAU,aAAsB,QAAQ,QAAQ;EACtD,MAAM,IAAI,KAAK,MAAM;EACrB,OAAO,OAAO,OAAO;CACvB;AACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** Where a variant came from: the edge's cache, the server's store, or made for this request. */
|
|
3
|
+
export declare const From: Schema.Literals<readonly ["edge", "store", "made"]>;
|
|
4
|
+
export type From = typeof From.Type;
|
|
5
|
+
/** Every answer the media server gives, and nothing else: each has its status and its headers. */
|
|
6
|
+
export declare const Answer: Schema.TaggedUnion<{
|
|
7
|
+
readonly Described: Schema.TaggedStruct<"Described", {}>;
|
|
8
|
+
readonly Exhausted: Schema.TaggedStruct<"Exhausted", {}>;
|
|
9
|
+
readonly Failed: Schema.TaggedStruct<"Failed", {}>;
|
|
10
|
+
readonly Forbidden: Schema.TaggedStruct<"Forbidden", {}>;
|
|
11
|
+
readonly Malformed: Schema.TaggedStruct<"Malformed", {}>;
|
|
12
|
+
readonly Moved: Schema.TaggedStruct<"Moved", {
|
|
13
|
+
readonly location: Schema.String;
|
|
14
|
+
}>;
|
|
15
|
+
readonly NotFound: Schema.TaggedStruct<"NotFound", {}>;
|
|
16
|
+
readonly NotModified: Schema.TaggedStruct<"NotModified", {
|
|
17
|
+
readonly etag: Schema.String;
|
|
18
|
+
}>;
|
|
19
|
+
readonly RateLimited: Schema.TaggedStruct<"RateLimited", {
|
|
20
|
+
readonly retryAfterSeconds: Schema.Number;
|
|
21
|
+
}>;
|
|
22
|
+
readonly TooLarge: Schema.TaggedStruct<"TooLarge", {}>;
|
|
23
|
+
readonly Unauthenticated: Schema.TaggedStruct<"Unauthenticated", {}>;
|
|
24
|
+
readonly Unavailable: Schema.TaggedStruct<"Unavailable", {}>;
|
|
25
|
+
readonly Unoffered: Schema.TaggedStruct<"Unoffered", {}>;
|
|
26
|
+
readonly Unsupported: Schema.TaggedStruct<"Unsupported", {}>;
|
|
27
|
+
readonly Uploaded: Schema.TaggedStruct<"Uploaded", {}>;
|
|
28
|
+
readonly Variant: Schema.TaggedStruct<"Variant", {
|
|
29
|
+
readonly etag: Schema.String;
|
|
30
|
+
readonly from: Schema.Literals<readonly ["edge", "store", "made"]>;
|
|
31
|
+
}>;
|
|
32
|
+
}>;
|
|
33
|
+
export type Answer = typeof Answer.Type;
|
|
34
|
+
/** The status of each answer. */
|
|
35
|
+
export declare const statusOf: (answer: Answer) => number;
|
|
36
|
+
/**
|
|
37
|
+
* Every response's headers, decided in one place, so the server, the edge
|
|
38
|
+
* and a static host cannot disagree. A variant's URL names the variant, and
|
|
39
|
+
* a new way of making it is a new revision, so it is kept for a year. A refusal of a variant is not: another release, or the
|
|
40
|
+
* same one configured otherwise, may answer it, so only a CDN keeps it,
|
|
41
|
+
* briefly; a failure is never kept. Every image may be read by a canvas on
|
|
42
|
+
* another origin, and its type is never sniffed.
|
|
43
|
+
*/
|
|
44
|
+
export declare const headersFor: (answer: Answer) => Readonly<Record<string, string>>;
|
|
45
|
+
/** Whether an `If-None-Match` names `etag` (weakly, as a GET compares, or `*`). */
|
|
46
|
+
export declare const matches: (ifNoneMatch: string | undefined, etag: string) => boolean;
|
|
47
|
+
//# sourceMappingURL=headers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.d.ts","names":[],"sources":["../../src/server/headers.ts"],"mappings":";;qBAGa,MAAI,OAAA;YACL,cAAc,KAAK;;qBAGlB,QAAM,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;YA4CP,gBAAgB,OAAO;;qBActB,WAAW,QAAQ;;;;;;;;;qBA2BnB,aAAU,QAAY,WAAS,SAAS;;qBAqCxC,UAAO,iCAAmC"}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
// src/server/headers.ts
|
|
3
|
+
/** Where a variant came from: the edge's cache, the server's store, or made for this request. */
|
|
4
|
+
const From = Schema.Literals([
|
|
5
|
+
"edge",
|
|
6
|
+
"store",
|
|
7
|
+
"made"
|
|
8
|
+
]);
|
|
9
|
+
/** Every answer the media server gives, and nothing else: each has its status and its headers. */
|
|
10
|
+
const Answer = Schema.TaggedUnion({
|
|
11
|
+
/** A variant's bytes. */
|
|
12
|
+
Variant: {
|
|
13
|
+
etag: Schema.String,
|
|
14
|
+
from: From
|
|
15
|
+
},
|
|
16
|
+
/** The variant the request's `If-None-Match` already holds. */
|
|
17
|
+
NotModified: { etag: Schema.String },
|
|
18
|
+
/**
|
|
19
|
+
* No URL of this server's grammar. A later release may read it (a new format
|
|
20
|
+
* or parameter, in a rollout), so it is kept as briefly as `NotFound`.
|
|
21
|
+
*/
|
|
22
|
+
Malformed: {},
|
|
23
|
+
/** No original under the hash: an upload may yet put one there. */
|
|
24
|
+
NotFound: {},
|
|
25
|
+
/**
|
|
26
|
+
* A spelling of the grammar outside this space: another release (the next
|
|
27
|
+
* one, in a rollout) may offer it, so it is kept as briefly as `NotFound`.
|
|
28
|
+
*/
|
|
29
|
+
Unoffered: {},
|
|
30
|
+
/**
|
|
31
|
+
* A variant of an earlier revision, made no longer and not kept: its URL now names the
|
|
32
|
+
* current revision's, at `location`. A 302 kept briefly, since a rollback to that revision,
|
|
33
|
+
* or a server of it still in the rollout, answers the old URL again.
|
|
34
|
+
*/
|
|
35
|
+
Moved: { location: Schema.String },
|
|
36
|
+
/** The original is larger than the server reads: a server configured to read more may. */
|
|
37
|
+
TooLarge: {},
|
|
38
|
+
/** The transformer failed: perhaps not for good. */
|
|
39
|
+
Failed: {},
|
|
40
|
+
/** The client has had too many variants made lately. */
|
|
41
|
+
RateLimited: { retryAfterSeconds: Schema.Number },
|
|
42
|
+
/** The host's allowance is spent. */
|
|
43
|
+
Exhausted: {},
|
|
44
|
+
/** An upload kept; its facts are the body. */
|
|
45
|
+
Uploaded: {},
|
|
46
|
+
/** The upload policy refused who is asking. */
|
|
47
|
+
Forbidden: {},
|
|
48
|
+
/** The upload policy could not tell who is asking: an untrusted origin, a session ended. */
|
|
49
|
+
Unauthenticated: {},
|
|
50
|
+
/** An upload that is not an image the server reads. */
|
|
51
|
+
Unsupported: {},
|
|
52
|
+
/** The facts of an image named by URL; the facts are the body. */
|
|
53
|
+
Described: {},
|
|
54
|
+
/** No facts could be read for that URL: a host not allowed, a read that failed, no image. */
|
|
55
|
+
Unavailable: {}
|
|
56
|
+
});
|
|
57
|
+
const YEAR = "public, max-age=31536000, immutable";
|
|
58
|
+
const BRIEFLY = {
|
|
59
|
+
"cache-control": "no-store",
|
|
60
|
+
"cdn-cache-control": "public, max-age=60"
|
|
61
|
+
};
|
|
62
|
+
const EXHAUSTED_RETRY_SECONDS = "3600";
|
|
63
|
+
/** The status of each answer. */
|
|
64
|
+
const statusOf = Answer.match({
|
|
65
|
+
Variant: () => 200,
|
|
66
|
+
NotModified: () => 304,
|
|
67
|
+
Malformed: () => 400,
|
|
68
|
+
NotFound: () => 404,
|
|
69
|
+
Unoffered: () => 404,
|
|
70
|
+
Moved: () => 302,
|
|
71
|
+
TooLarge: () => 413,
|
|
72
|
+
Failed: () => 502,
|
|
73
|
+
RateLimited: () => 429,
|
|
74
|
+
Exhausted: () => 503,
|
|
75
|
+
Uploaded: () => 201,
|
|
76
|
+
Forbidden: () => 403,
|
|
77
|
+
Unauthenticated: () => 401,
|
|
78
|
+
Unsupported: () => 415,
|
|
79
|
+
Described: () => 200,
|
|
80
|
+
Unavailable: () => 422
|
|
81
|
+
});
|
|
82
|
+
/**
|
|
83
|
+
* Every response's headers, decided in one place, so the server, the edge
|
|
84
|
+
* and a static host cannot disagree. A variant's URL names the variant, and
|
|
85
|
+
* a new way of making it is a new revision, so it is kept for a year. A refusal of a variant is not: another release, or the
|
|
86
|
+
* same one configured otherwise, may answer it, so only a CDN keeps it,
|
|
87
|
+
* briefly; a failure is never kept. Every image may be read by a canvas on
|
|
88
|
+
* another origin, and its type is never sniffed.
|
|
89
|
+
*/
|
|
90
|
+
const headersFor = (answer) => ({
|
|
91
|
+
"x-content-type-options": "nosniff",
|
|
92
|
+
"access-control-allow-origin": "*",
|
|
93
|
+
"cross-origin-resource-policy": "cross-origin",
|
|
94
|
+
"timing-allow-origin": "*",
|
|
95
|
+
...Answer.match(answer, {
|
|
96
|
+
Variant: ({ etag, from }) => ({
|
|
97
|
+
"cache-control": YEAR,
|
|
98
|
+
etag,
|
|
99
|
+
"server-timing": `media;desc="${from}"`
|
|
100
|
+
}),
|
|
101
|
+
NotModified: ({ etag }) => ({
|
|
102
|
+
"cache-control": YEAR,
|
|
103
|
+
etag
|
|
104
|
+
}),
|
|
105
|
+
Malformed: () => BRIEFLY,
|
|
106
|
+
NotFound: () => BRIEFLY,
|
|
107
|
+
Unoffered: () => BRIEFLY,
|
|
108
|
+
Moved: ({ location }) => ({
|
|
109
|
+
...BRIEFLY,
|
|
110
|
+
location
|
|
111
|
+
}),
|
|
112
|
+
TooLarge: () => BRIEFLY,
|
|
113
|
+
Failed: () => ({ "cache-control": "no-store" }),
|
|
114
|
+
RateLimited: ({ retryAfterSeconds }) => ({
|
|
115
|
+
"cache-control": "no-store",
|
|
116
|
+
"retry-after": String(retryAfterSeconds)
|
|
117
|
+
}),
|
|
118
|
+
Exhausted: () => ({
|
|
119
|
+
"cache-control": "no-store",
|
|
120
|
+
"retry-after": EXHAUSTED_RETRY_SECONDS
|
|
121
|
+
}),
|
|
122
|
+
Uploaded: () => ({ "cache-control": "no-store" }),
|
|
123
|
+
Forbidden: () => ({ "cache-control": "no-store" }),
|
|
124
|
+
Unauthenticated: () => ({ "cache-control": "no-store" }),
|
|
125
|
+
Unsupported: () => ({ "cache-control": "no-store" }),
|
|
126
|
+
Described: () => ({ "cache-control": "no-store" }),
|
|
127
|
+
Unavailable: () => ({ "cache-control": "no-store" })
|
|
128
|
+
})
|
|
129
|
+
});
|
|
130
|
+
/** Whether an `If-None-Match` names `etag` (weakly, as a GET compares, or `*`). */
|
|
131
|
+
const matches = (ifNoneMatch, etag) => ifNoneMatch !== void 0 && ifNoneMatch.split(",").map((tag) => tag.trim().replace(/^W\//, "")).some((tag) => tag === "*" || tag === etag);
|
|
132
|
+
export { Answer, From, headersFor, matches, statusOf };
|
|
133
|
+
|
|
134
|
+
//# sourceMappingURL=headers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"headers.js","names":[],"sources":["../../src/server/headers.ts"],"sourcesContent":["import { Schema } from 'effect'\n\n/** Where a variant came from: the edge's cache, the server's store, or made for this request. */\nexport const From = Schema.Literals(['edge', 'store', 'made'])\nexport type From = typeof From.Type\n\n/** Every answer the media server gives, and nothing else: each has its status and its headers. */\nexport const Answer = Schema.TaggedUnion({\n /** A variant's bytes. */\n Variant: { etag: Schema.String, from: From },\n /** The variant the request's `If-None-Match` already holds. */\n NotModified: { etag: Schema.String },\n /**\n * No URL of this server's grammar. A later release may read it (a new format\n * or parameter, in a rollout), so it is kept as briefly as `NotFound`.\n */\n Malformed: {},\n /** No original under the hash: an upload may yet put one there. */\n NotFound: {},\n /**\n * A spelling of the grammar outside this space: another release (the next\n * one, in a rollout) may offer it, so it is kept as briefly as `NotFound`.\n */\n Unoffered: {},\n /**\n * A variant of an earlier revision, made no longer and not kept: its URL now names the\n * current revision's, at `location`. A 302 kept briefly, since a rollback to that revision,\n * or a server of it still in the rollout, answers the old URL again.\n */\n Moved: { location: Schema.String },\n /** The original is larger than the server reads: a server configured to read more may. */\n TooLarge: {},\n /** The transformer failed: perhaps not for good. */\n Failed: {},\n /** The client has had too many variants made lately. */\n RateLimited: { retryAfterSeconds: Schema.Number },\n /** The host's allowance is spent. */\n Exhausted: {},\n /** An upload kept; its facts are the body. */\n Uploaded: {},\n /** The upload policy refused who is asking. */\n Forbidden: {},\n /** The upload policy could not tell who is asking: an untrusted origin, a session ended. */\n Unauthenticated: {},\n /** An upload that is not an image the server reads. */\n Unsupported: {},\n /** The facts of an image named by URL; the facts are the body. */\n Described: {},\n /** No facts could be read for that URL: a host not allowed, a read that failed, no image. */\n Unavailable: {},\n})\nexport type Answer = typeof Answer.Type\n\nconst YEAR = 'public, max-age=31536000, immutable'\n\n// What may yet be answered: the browser keeps nothing, and the edge a minute, to blunt a crawler.\nconst BRIEFLY: Readonly<Record<string, string>> = {\n 'cache-control': 'no-store',\n 'cdn-cache-control': 'public, max-age=60',\n}\n\n// Retry a spent allowance after an hour: the host's window is not known here.\nconst EXHAUSTED_RETRY_SECONDS = '3600'\n\n/** The status of each answer. */\nexport const statusOf: (answer: Answer) => number = Answer.match({\n Variant: () => 200,\n NotModified: () => 304,\n Malformed: () => 400,\n NotFound: () => 404,\n Unoffered: () => 404,\n Moved: () => 302,\n TooLarge: () => 413,\n Failed: () => 502,\n RateLimited: () => 429,\n Exhausted: () => 503,\n Uploaded: () => 201,\n Forbidden: () => 403,\n Unauthenticated: () => 401,\n Unsupported: () => 415,\n Described: () => 200,\n Unavailable: () => 422,\n})\n\n/**\n * Every response's headers, decided in one place, so the server, the edge\n * and a static host cannot disagree. A variant's URL names the variant, and\n * a new way of making it is a new revision, so it is kept for a year. A refusal of a variant is not: another release, or the\n * same one configured otherwise, may answer it, so only a CDN keeps it,\n * briefly; a failure is never kept. Every image may be read by a canvas on\n * another origin, and its type is never sniffed.\n */\nexport const headersFor = (answer: Answer): Readonly<Record<string, string>> => ({\n 'x-content-type-options': 'nosniff',\n 'access-control-allow-origin': '*',\n 'cross-origin-resource-policy': 'cross-origin',\n 'timing-allow-origin': '*',\n ...Answer.match(answer, {\n Variant: ({ etag, from }): Record<string, string> => ({\n 'cache-control': YEAR,\n etag,\n 'server-timing': `media;desc=\"${from}\"`,\n }),\n NotModified: ({ etag }): Record<string, string> => ({ 'cache-control': YEAR, etag }),\n Malformed: (): Record<string, string> => BRIEFLY,\n NotFound: (): Record<string, string> => BRIEFLY,\n Unoffered: (): Record<string, string> => BRIEFLY,\n Moved: ({ location }): Record<string, string> => ({ ...BRIEFLY, location }),\n TooLarge: (): Record<string, string> => BRIEFLY,\n Failed: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n RateLimited: ({ retryAfterSeconds }): Record<string, string> => ({\n 'cache-control': 'no-store',\n 'retry-after': String(retryAfterSeconds),\n }),\n Exhausted: (): Record<string, string> => ({\n 'cache-control': 'no-store',\n 'retry-after': EXHAUSTED_RETRY_SECONDS,\n }),\n // An upload's answers are about one request, and are never kept.\n Uploaded: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n Forbidden: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n Unauthenticated: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n Unsupported: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n Described: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n Unavailable: (): Record<string, string> => ({ 'cache-control': 'no-store' }),\n }),\n})\n\n/** Whether an `If-None-Match` names `etag` (weakly, as a GET compares, or `*`). */\nexport const matches = (ifNoneMatch: string | undefined, etag: string): boolean =>\n ifNoneMatch !== undefined &&\n ifNoneMatch\n .split(',')\n .map(tag => tag.trim().replace(/^W\\//, ''))\n .some(tag => tag === '*' || tag === etag)\n"],"mappings":";;;AAGA,MAAa,OAAO,OAAO,SAAS;CAAC;CAAQ;CAAS;AAAM,CAAC;;AAI7D,MAAa,SAAS,OAAO,YAAY;;CAEvC,SAAS;EAAE,MAAM,OAAO;EAAQ,MAAM;CAAK;;CAE3C,aAAa,EAAE,MAAM,OAAO,OAAO;;;;;CAKnC,WAAW,CAAC;;CAEZ,UAAU,CAAC;;;;;CAKX,WAAW,CAAC;;;;;;CAMZ,OAAO,EAAE,UAAU,OAAO,OAAO;;CAEjC,UAAU,CAAC;;CAEX,QAAQ,CAAC;;CAET,aAAa,EAAE,mBAAmB,OAAO,OAAO;;CAEhD,WAAW,CAAC;;CAEZ,UAAU,CAAC;;CAEX,WAAW,CAAC;;CAEZ,iBAAiB,CAAC;;CAElB,aAAa,CAAC;;CAEd,WAAW,CAAC;;CAEZ,aAAa,CAAC;AAChB,CAAC;AAGD,MAAM,OAAO;AAGb,MAAM,UAA4C;CAChD,iBAAiB;CACjB,qBAAqB;AACvB;AAGA,MAAM,0BAA0B;;AAGhC,MAAa,WAAuC,OAAO,MAAM;CAC/D,eAAe;CACf,mBAAmB;CACnB,iBAAiB;CACjB,gBAAgB;CAChB,iBAAiB;CACjB,aAAa;CACb,gBAAgB;CAChB,cAAc;CACd,mBAAmB;CACnB,iBAAiB;CACjB,gBAAgB;CAChB,iBAAiB;CACjB,uBAAuB;CACvB,mBAAmB;CACnB,iBAAiB;CACjB,mBAAmB;AACrB,CAAC;;;;;;;;;AAUD,MAAa,cAAc,YAAsD;CAC/E,0BAA0B;CAC1B,+BAA+B;CAC/B,gCAAgC;CAChC,uBAAuB;CACvB,GAAG,OAAO,MAAM,QAAQ;EACtB,UAAU,EAAE,MAAM,YAAoC;GACpD,iBAAiB;GACjB;GACA,iBAAiB,eAAe,KAAK;EACvC;EACA,cAAc,EAAE,YAAoC;GAAE,iBAAiB;GAAM;EAAK;EAClF,iBAAyC;EACzC,gBAAwC;EACxC,iBAAyC;EACzC,QAAQ,EAAE,gBAAwC;GAAE,GAAG;GAAS;EAAS;EACzE,gBAAwC;EACxC,eAAuC,EAAE,iBAAiB,WAAW;EACrE,cAAc,EAAE,yBAAiD;GAC/D,iBAAiB;GACjB,eAAe,OAAO,iBAAiB;EACzC;EACA,kBAA0C;GACxC,iBAAiB;GACjB,eAAe;EACjB;EAEA,iBAAyC,EAAE,iBAAiB,WAAW;EACvE,kBAA0C,EAAE,iBAAiB,WAAW;EACxE,wBAAgD,EAAE,iBAAiB,WAAW;EAC9E,oBAA4C,EAAE,iBAAiB,WAAW;EAC1E,kBAA0C,EAAE,iBAAiB,WAAW;EACxE,oBAA4C,EAAE,iBAAiB,WAAW;CAC5E,CAAC;AACH;;AAGA,MAAa,WAAW,aAAiC,SACvD,gBAAgB,KAAA,KAChB,YACG,MAAM,GAAG,CAAC,CACV,KAAI,QAAO,IAAI,KAAK,CAAC,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC,CAC1C,MAAK,QAAO,QAAQ,OAAO,QAAQ,IAAI"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { OriginalNotFound, OriginalTooLarge, QuotaExhausted, TransformFailed } from "./errors.js";
|
|
2
|
+
import { Admission, RateLimited, admissionRateLimiter } from "./admission.js";
|
|
3
|
+
import { AdmissionFailed, FactsUnanswered, ImageFailed, MediaDiagnostic, RequestDefect, StoreFailed } from "./diagnostics.js";
|
|
4
|
+
import { Encoded, Kept, Original, Originals, Transformer, Variants, limitTransforms, variantsNone } from "./services.js";
|
|
5
|
+
import { StoredFacts, UploadApi, UploadForbidden, UploadOptions, UploadPolicy, UploadRoute, UploadUnauthenticated, Uploads, storedFactsKeyValueStore, uploadHandlers, uploadLayer, uploadPolicyAny, uploadPolicyBearer, uploadsKeyValueStore } from "./upload.js";
|
|
6
|
+
import { originalsKeyValueStore, variantsKeyValueStore } from "./stores.js";
|
|
7
|
+
import { Options, handlers, layer } from "./handlers.js";
|
|
8
|
+
import { MediaApi } from "./api.js";
|
|
9
|
+
import { Answer, headersFor, statusOf } from "./headers.js";
|
|
10
|
+
import { FetchOptions, serveFetch } from "./fetch.js";
|
|
11
|
+
import { Materialized, headersRule, materialize } from "./materialize.js";
|
|
12
|
+
import { FactsOptions, FactsUnavailable, factsOf } from "./facts.js";
|
|
13
|
+
export { Admission, type AdmissionFailed, Answer, type Encoded, type FactsOptions, type FactsUnanswered, FactsUnavailable, type FetchOptions, type ImageFailed, type Kept, type Materialized, MediaApi, type MediaDiagnostic, type Options, type Original, OriginalNotFound, OriginalTooLarge, Originals, QuotaExhausted, RateLimited, type RequestDefect, type StoreFailed, StoredFacts, TransformFailed, Transformer, UploadApi, UploadForbidden, type UploadOptions, UploadPolicy, type UploadRoute, UploadUnauthenticated, Uploads, Variants, admissionRateLimiter, factsOf, handlers, headersFor, headersRule, layer, limitTransforms, materialize, originalsKeyValueStore, serveFetch, statusOf, storedFactsKeyValueStore, uploadHandlers, uploadLayer, uploadPolicyAny, uploadPolicyBearer, uploadsKeyValueStore, variantsKeyValueStore, variantsNone };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { OriginalNotFound, OriginalTooLarge, QuotaExhausted, TransformFailed } from "./errors.js";
|
|
2
|
+
import { Admission, RateLimited, admissionRateLimiter } from "./admission.js";
|
|
3
|
+
import { Originals, Transformer, Variants, limitTransforms, variantsNone } from "./services.js";
|
|
4
|
+
import { StoredFacts, UploadApi, UploadForbidden, UploadPolicy, UploadUnauthenticated, Uploads, storedFactsKeyValueStore, uploadHandlers, uploadLayer, uploadPolicyAny, uploadPolicyBearer, uploadsKeyValueStore } from "./upload.js";
|
|
5
|
+
import { originalsKeyValueStore, variantsKeyValueStore } from "./stores.js";
|
|
6
|
+
import { handlers, layer } from "./handlers.js";
|
|
7
|
+
import { MediaApi } from "./api.js";
|
|
8
|
+
import { Answer, headersFor, statusOf } from "./headers.js";
|
|
9
|
+
import { serveFetch } from "./fetch.js";
|
|
10
|
+
import { headersRule, materialize } from "./materialize.js";
|
|
11
|
+
import { FactsUnavailable, factsOf } from "./facts.js";
|
|
12
|
+
export { Admission, Answer, FactsUnavailable, MediaApi, OriginalNotFound, OriginalTooLarge, Originals, QuotaExhausted, RateLimited, StoredFacts, TransformFailed, Transformer, UploadApi, UploadForbidden, UploadPolicy, UploadUnauthenticated, Uploads, Variants, admissionRateLimiter, factsOf, handlers, headersFor, headersRule, layer, limitTransforms, materialize, originalsKeyValueStore, serveFetch, statusOf, storedFactsKeyValueStore, uploadHandlers, uploadLayer, uploadPolicyAny, uploadPolicyBearer, uploadsKeyValueStore, variantsKeyValueStore, variantsNone };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** A variant written out as a file, at its own URL's path. */
|
|
2
|
+
export interface Materialized {
|
|
3
|
+
readonly file: string;
|
|
4
|
+
readonly contents: Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The `_headers` rule (Cloudflare Pages, Netlify) a host needs for variants
|
|
8
|
+
* written out as files: what the server itself sends a variant, from the same
|
|
9
|
+
* `headersFor`, less the `ETag` and `Server-Timing` of one answer.
|
|
10
|
+
*/
|
|
11
|
+
export declare const headersRule: () => string;
|
|
12
|
+
/**
|
|
13
|
+
* Every hosted variant the built pages name, made through `fetch` (the media
|
|
14
|
+
* server's own handler, `serveFetch`) and returned as files at their own
|
|
15
|
+
* paths, for `foldkit-ssr`'s `emit`. `Provider.hosted` then works on a static
|
|
16
|
+
* host with no server at all. With a `Variants` store on disk, a second build
|
|
17
|
+
* writes without encoding.
|
|
18
|
+
*
|
|
19
|
+
* It fails the build, naming every one, when a variant cannot be made (a
|
|
20
|
+
* page would show a broken image), when there would be more than `maxFiles`,
|
|
21
|
+
* and, given the site's `_headers` text, when that file has no rule for the
|
|
22
|
+
* variants, which a static host would otherwise serve uncached.
|
|
23
|
+
*/
|
|
24
|
+
export declare const materialize: (options: {
|
|
25
|
+
readonly pages: ReadonlyArray<{
|
|
26
|
+
readonly html: string;
|
|
27
|
+
}>;
|
|
28
|
+
readonly fetch: (request: Request) => Promise<Response>;
|
|
29
|
+
/** The most files to write. */
|
|
30
|
+
readonly maxFiles?: number;
|
|
31
|
+
/** The site's `_headers` text, to check it has `headersRule()`'s pattern. */
|
|
32
|
+
readonly headersFile?: string;
|
|
33
|
+
/** Variants made at once. */
|
|
34
|
+
readonly concurrency?: number;
|
|
35
|
+
}) => Promise<ReadonlyArray<Materialized>>;
|
|
36
|
+
//# sourceMappingURL=materialize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"materialize.d.ts","names":[],"sources":["../../src/server/materialize.ts"],"mappings":";iBAIiB;WACN;WACA,UAAU;;;;;;;qBAgBR;;;;;;;;;;;;;qBAoBA,cAAW;WACb,OAAO;aAAyB;;WAChC,QAAQ,SAAS,YAAY,QAAQ;;WAErC;;WAEA;;WAEA;MACP,QAAQ,cAAc"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { nonNegativeInteger, positiveInteger } from "../limits.js";
|
|
2
|
+
import { Answer, headersFor } from "./headers.js";
|
|
3
|
+
// src/server/materialize.ts
|
|
4
|
+
const VARIANT_PATH = /\/_image\/[0-9a-f]{64}\/[^\s"',)<>]+/g;
|
|
5
|
+
const DEFAULT_MAX_FILES = 2e4;
|
|
6
|
+
const PATTERN = "/_image/*";
|
|
7
|
+
/**
|
|
8
|
+
* The `_headers` rule (Cloudflare Pages, Netlify) a host needs for variants
|
|
9
|
+
* written out as files: what the server itself sends a variant, from the same
|
|
10
|
+
* `headersFor`, less the `ETag` and `Server-Timing` of one answer.
|
|
11
|
+
*/
|
|
12
|
+
const headersRule = () => {
|
|
13
|
+
const headers = headersFor(Answer.cases.Variant.make({
|
|
14
|
+
etag: "",
|
|
15
|
+
from: "made"
|
|
16
|
+
}));
|
|
17
|
+
const lines = Object.entries(headers).filter(([name]) => name !== "etag" && name !== "server-timing").map(([name, value]) => ` ${name}: ${value}`);
|
|
18
|
+
return [PATTERN, ...lines].join("\n") + "\n";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Every hosted variant the built pages name, made through `fetch` (the media
|
|
22
|
+
* server's own handler, `serveFetch`) and returned as files at their own
|
|
23
|
+
* paths, for `foldkit-ssr`'s `emit`. `Provider.hosted` then works on a static
|
|
24
|
+
* host with no server at all. With a `Variants` store on disk, a second build
|
|
25
|
+
* writes without encoding.
|
|
26
|
+
*
|
|
27
|
+
* It fails the build, naming every one, when a variant cannot be made (a
|
|
28
|
+
* page would show a broken image), when there would be more than `maxFiles`,
|
|
29
|
+
* and, given the site's `_headers` text, when that file has no rule for the
|
|
30
|
+
* variants, which a static host would otherwise serve uncached.
|
|
31
|
+
*/
|
|
32
|
+
const materialize = async (options) => {
|
|
33
|
+
const paths = [...new Set(options.pages.flatMap((page) => page.html.match(VARIANT_PATH) ?? []))].sort();
|
|
34
|
+
const maxFiles = nonNegativeInteger("maxFiles", options.maxFiles ?? DEFAULT_MAX_FILES);
|
|
35
|
+
const concurrency = positiveInteger("foldkit-media: materialize concurrency", options.concurrency ?? 4);
|
|
36
|
+
if (paths.length > maxFiles) throw new Error(`foldkit-media: the pages name ${paths.length} variants, more than maxFiles (${maxFiles})`);
|
|
37
|
+
if (paths.length > 0 && options.headersFile !== void 0 && !options.headersFile.split(/\r?\n/).some((line) => line.trim() === PATTERN)) throw new Error(`foldkit-media: _headers has no ${PATTERN} rule, so the variants would be served uncached: add Media.headersRule()`);
|
|
38
|
+
const made = [];
|
|
39
|
+
const failed = [];
|
|
40
|
+
const queue = [...paths];
|
|
41
|
+
const worker = async () => {
|
|
42
|
+
for (let path = queue.shift(); path !== void 0; path = queue.shift()) try {
|
|
43
|
+
const response = await options.fetch(new Request(`http://materialize.invalid${path}`));
|
|
44
|
+
if (response.status !== 200) {
|
|
45
|
+
failed.push(`${path}: ${response.status}`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
made.push({
|
|
49
|
+
file: path.slice(1),
|
|
50
|
+
contents: new Uint8Array(await response.arrayBuffer())
|
|
51
|
+
});
|
|
52
|
+
} catch (cause) {
|
|
53
|
+
failed.push(`${path}: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, paths.length) }, worker));
|
|
57
|
+
if (failed.length > 0) throw new Error(`foldkit-media: ${failed.length} variants could not be made:\n${failed.sort().join("\n")}`);
|
|
58
|
+
return made.sort((a, b) => a.file < b.file ? -1 : a.file > b.file ? 1 : 0);
|
|
59
|
+
};
|
|
60
|
+
export { headersRule, materialize };
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=materialize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"materialize.js","names":[],"sources":["../../src/server/materialize.ts"],"sourcesContent":["import { nonNegativeInteger, positiveInteger } from '../limits.js'\nimport { Answer, headersFor } from './headers.js'\n\n/** A variant written out as a file, at its own URL's path. */\nexport interface Materialized {\n readonly file: string\n readonly contents: Uint8Array\n}\n\n// A hosted variant's path, wherever a page names one: in a `srcset`, a `src`, a preload.\nconst VARIANT_PATH = /\\/_image\\/[0-9a-f]{64}\\/[^\\s\"',)<>]+/g\n\n// Cloudflare Pages serves at most 20,000 files a site on its free plan.\nconst DEFAULT_MAX_FILES = 20_000\n\nconst PATTERN = '/_image/*'\n\n/**\n * The `_headers` rule (Cloudflare Pages, Netlify) a host needs for variants\n * written out as files: what the server itself sends a variant, from the same\n * `headersFor`, less the `ETag` and `Server-Timing` of one answer.\n */\nexport const headersRule = (): string => {\n const headers = headersFor(Answer.cases.Variant.make({ etag: '', from: 'made' }))\n const lines = Object.entries(headers)\n .filter(([name]) => name !== 'etag' && name !== 'server-timing')\n .map(([name, value]) => ` ${name}: ${value}`)\n return [PATTERN, ...lines].join('\\n') + '\\n'\n}\n\n/**\n * Every hosted variant the built pages name, made through `fetch` (the media\n * server's own handler, `serveFetch`) and returned as files at their own\n * paths, for `foldkit-ssr`'s `emit`. `Provider.hosted` then works on a static\n * host with no server at all. With a `Variants` store on disk, a second build\n * writes without encoding.\n *\n * It fails the build, naming every one, when a variant cannot be made (a\n * page would show a broken image), when there would be more than `maxFiles`,\n * and, given the site's `_headers` text, when that file has no rule for the\n * variants, which a static host would otherwise serve uncached.\n */\nexport const materialize = async (options: {\n readonly pages: ReadonlyArray<{ readonly html: string }>\n readonly fetch: (request: Request) => Promise<Response>\n /** The most files to write. */\n readonly maxFiles?: number\n /** The site's `_headers` text, to check it has `headersRule()`'s pattern. */\n readonly headersFile?: string\n /** Variants made at once. */\n readonly concurrency?: number\n}): Promise<ReadonlyArray<Materialized>> => {\n const paths = [\n ...new Set(options.pages.flatMap(page => page.html.match(VARIANT_PATH) ?? [])),\n ].sort()\n const maxFiles = nonNegativeInteger('maxFiles', options.maxFiles ?? DEFAULT_MAX_FILES)\n const concurrency = positiveInteger(\n 'foldkit-media: materialize concurrency',\n options.concurrency ?? 4,\n )\n if (paths.length > maxFiles) {\n throw new Error(\n `foldkit-media: the pages name ${paths.length} variants, more than maxFiles (${maxFiles})`,\n )\n }\n if (\n paths.length > 0 &&\n options.headersFile !== undefined &&\n !options.headersFile.split(/\\r?\\n/).some(line => line.trim() === PATTERN)\n ) {\n throw new Error(\n `foldkit-media: _headers has no ${PATTERN} rule, so the variants would be served uncached: add Media.headersRule()`,\n )\n }\n const made: Array<Materialized> = []\n const failed: Array<string> = []\n const queue = [...paths]\n const worker = async () => {\n for (let path = queue.shift(); path !== undefined; path = queue.shift()) {\n try {\n const response = await options.fetch(new Request(`http://materialize.invalid${path}`))\n if (response.status !== 200) {\n failed.push(`${path}: ${response.status}`)\n continue\n }\n made.push({ file: path.slice(1), contents: new Uint8Array(await response.arrayBuffer()) })\n } catch (cause) {\n failed.push(`${path}: ${cause instanceof Error ? cause.message : String(cause)}`)\n }\n }\n }\n // No more workers than paths, which also keeps an absurd concurrency out of\n // an array length.\n await Promise.all(Array.from({ length: Math.min(concurrency, paths.length) }, worker))\n if (failed.length > 0) {\n throw new Error(\n `foldkit-media: ${failed.length} variants could not be made:\\n${failed.sort().join('\\n')}`,\n )\n }\n return made.sort((a, b) => (a.file < b.file ? -1 : a.file > b.file ? 1 : 0))\n}\n"],"mappings":";;;AAUA,MAAM,eAAe;AAGrB,MAAM,oBAAoB;AAE1B,MAAM,UAAU;;;;;;AAOhB,MAAa,oBAA4B;CACvC,MAAM,UAAU,WAAW,OAAO,MAAM,QAAQ,KAAK;EAAE,MAAM;EAAI,MAAM;CAAO,CAAC,CAAC;CAChF,MAAM,QAAQ,OAAO,QAAQ,OAAO,CAAC,CAClC,QAAQ,CAAC,UAAU,SAAS,UAAU,SAAS,eAAe,CAAC,CAC/D,KAAK,CAAC,MAAM,WAAW,KAAK,KAAK,IAAI,OAAO;CAC/C,OAAO,CAAC,SAAS,GAAG,KAAK,CAAC,CAAC,KAAK,IAAI,IAAI;AAC1C;;;;;;;;;;;;;AAcA,MAAa,cAAc,OAAO,YASU;CAC1C,MAAM,QAAQ,CACZ,GAAG,IAAI,IAAI,QAAQ,MAAM,SAAQ,SAAQ,KAAK,KAAK,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,CAC/E,CAAC,CAAC,KAAK;CACP,MAAM,WAAW,mBAAmB,YAAY,QAAQ,YAAY,iBAAiB;CACrF,MAAM,cAAc,gBAClB,0CACA,QAAQ,eAAe,CACzB;CACA,IAAI,MAAM,SAAS,UACjB,MAAM,IAAI,MACR,iCAAiC,MAAM,OAAO,iCAAiC,SAAS,EAC1F;CAEF,IACE,MAAM,SAAS,KACf,QAAQ,gBAAgB,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
import { Context, Effect, Layer, Option } from "effect";
|
|
2
|
+
import { Variant } from "../variant.js";
|
|
3
|
+
import { OriginalNotFound, OriginalTooLarge, QuotaExhausted, TransformFailed } from "./errors.js";
|
|
4
4
|
/** An original's bytes, as the store hands them over, and how many there are. */
|
|
5
|
-
interface Original {
|
|
5
|
+
export interface Original {
|
|
6
6
|
readonly body: ReadableStream<Uint8Array>;
|
|
7
7
|
readonly size: number;
|
|
8
8
|
}
|
|
9
9
|
/** A variant made: its bytes and their media type. */
|
|
10
|
-
interface Encoded {
|
|
10
|
+
export interface Encoded {
|
|
11
11
|
readonly bytes: Uint8Array<ArrayBuffer>;
|
|
12
12
|
readonly contentType: string;
|
|
13
13
|
}
|
|
14
14
|
/** A variant found kept, and which layer had it. */
|
|
15
|
-
interface Kept {
|
|
15
|
+
export interface Kept {
|
|
16
16
|
readonly encoded: Encoded;
|
|
17
17
|
readonly from: 'edge' | 'store';
|
|
18
18
|
}
|
|
@@ -20,7 +20,7 @@ declare const Originals_base: Context.ServiceClass<Originals, "foldkit-media/Ori
|
|
|
20
20
|
readonly get: (hash: string) => Effect.Effect<Original, OriginalNotFound>;
|
|
21
21
|
}>;
|
|
22
22
|
/** Where originals live, by their content hash. Authoritative: the server never writes it. */
|
|
23
|
-
declare class Originals extends Originals_base {}
|
|
23
|
+
export declare class Originals extends Originals_base {}
|
|
24
24
|
declare const Transformer_base: Context.ServiceClass<Transformer, "foldkit-media/Transformer", {
|
|
25
25
|
readonly transform: (original: Original, variant: Variant) => Effect.Effect<Encoded, TransformFailed | QuotaExhausted | OriginalTooLarge>;
|
|
26
26
|
}>;
|
|
@@ -29,7 +29,7 @@ declare const Transformer_base: Context.ServiceClass<Transformer, "foldkit-media
|
|
|
29
29
|
* original (a width above it answers with the original's width). An original
|
|
30
30
|
* too large to decode is `OriginalTooLarge`, a 413, as one too large to read.
|
|
31
31
|
*/
|
|
32
|
-
declare class Transformer extends Transformer_base {}
|
|
32
|
+
export declare class Transformer extends Transformer_base {}
|
|
33
33
|
declare const Variants_base: Context.ServiceClass<Variants, "foldkit-media/Variants", {
|
|
34
34
|
readonly get: (key: string) => Effect.Effect<Option.Option<Kept>>;
|
|
35
35
|
readonly put: (key: string, encoded: Encoded) => Effect.Effect<void>;
|
|
@@ -38,15 +38,15 @@ declare const Variants_base: Context.ServiceClass<Variants, "foldkit-media/Varia
|
|
|
38
38
|
* Variants already made, by the encoded request. A cache: losing it costs
|
|
39
39
|
* time, and on a host that bills transformations, money; never data.
|
|
40
40
|
*/
|
|
41
|
-
declare class Variants extends Variants_base {}
|
|
41
|
+
export declare class Variants extends Variants_base {}
|
|
42
42
|
/** Keeps nothing: every request transforms. For a host with a cache of its own in front. */
|
|
43
|
-
declare const variantsNone: Layer.Layer<Variants>;
|
|
43
|
+
export declare const variantsNone: Layer.Layer<Variants>;
|
|
44
44
|
/**
|
|
45
45
|
* A `Transformer` that runs at most `concurrency` transforms at once, the
|
|
46
46
|
* rest waiting their turn: encoding is CPU work, and a burst of misses should
|
|
47
47
|
* queue, not starve every other request. For a transformer that encodes in
|
|
48
48
|
* this process (sharp, WebAssembly); a host's binding does the work elsewhere.
|
|
49
49
|
*/
|
|
50
|
-
declare const limitTransforms: (concurrency: number) => <E, R>(transformer: Layer.Layer<Transformer, E, R>) => Layer.Layer<Transformer, E, R>;
|
|
51
|
-
export {
|
|
52
|
-
//# sourceMappingURL=services
|
|
50
|
+
export declare const limitTransforms: (concurrency: number) => <E, R>(transformer: Layer.Layer<Transformer, E, R>) => Layer.Layer<Transformer, E, R>;
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=services.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.d.ts","names":[],"sources":["../../src/server/services.ts"],"mappings":";;;;iBAWiB;WACN,MAAM,eAAe;WACrB;;;iBAIM;WACN,OAAO,WAAW;WAClB;;;iBAIM;WACN,SAAS;WACT;;;WAMO,MAAC,iBAAiB,OAAO,OAAO,UAAU;;;qBAF/C,kBAAkB;;WAaP,YAClB,UAAU,UACV,SAAS,YACN,OAAO,OAAO,SAAS,kBAAkB,iBAAiB;;;;;;;qBANtD,oBAAoB;;WAiBf,MAAC,gBAAgB,OAAO,OAAO,OAAO,OAAO;WAC7C,MAAC,aAAa,SAAS,YAAY,OAAO;;;;;;qBAJ/C,iBAAiB;;qBASjB,cAAc,MAAM,MAAM;;;;;;;qBAW1B,kBAAe,yBAElB,GAAG,GAAC,aAAe,MAAM,MAAM,aAAa,GAAG,OAAK,MAAM,MAAM,aAAa,GAAG"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { n as positiveInteger } from "./limits-B4hHpyfE.mjs";
|
|
2
1
|
import { Context, Effect, Layer, Option, Semaphore } from "effect";
|
|
2
|
+
import { positiveInteger } from "../limits.js";
|
|
3
3
|
// src/server/services.ts
|
|
4
4
|
/** Where originals live, by their content hash. Authoritative: the server never writes it. */
|
|
5
5
|
var Originals = class extends Context.Service()("foldkit-media/Originals") {};
|
|
@@ -33,6 +33,6 @@ const limitTransforms = (concurrency) => {
|
|
|
33
33
|
return { transform: (original, variant) => Semaphore.withPermit(permits)(inner.transform(original, variant)) };
|
|
34
34
|
})).pipe(Layer.provide(transformer));
|
|
35
35
|
};
|
|
36
|
-
export {
|
|
36
|
+
export { Originals, Transformer, Variants, limitTransforms, variantsNone };
|
|
37
37
|
|
|
38
|
-
//# sourceMappingURL=services
|
|
38
|
+
//# sourceMappingURL=services.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"services.js","names":[],"sources":["../../src/server/services.ts"],"sourcesContent":["import { Context, Effect, Layer, Option, Semaphore } from 'effect'\nimport { positiveInteger } from '../limits.js'\nimport type { Variant } from '../variant.js'\nimport type {\n OriginalNotFound,\n OriginalTooLarge,\n QuotaExhausted,\n TransformFailed,\n} from './errors.js'\n\n/** An original's bytes, as the store hands them over, and how many there are. */\nexport interface Original {\n readonly body: ReadableStream<Uint8Array>\n readonly size: number\n}\n\n/** A variant made: its bytes and their media type. */\nexport interface Encoded {\n readonly bytes: Uint8Array<ArrayBuffer>\n readonly contentType: string\n}\n\n/** A variant found kept, and which layer had it. */\nexport interface Kept {\n readonly encoded: Encoded\n readonly from: 'edge' | 'store'\n}\n\n/** Where originals live, by their content hash. Authoritative: the server never writes it. */\nexport class Originals extends Context.Service<\n Originals,\n { readonly get: (hash: string) => Effect.Effect<Original, OriginalNotFound> }\n>()('foldkit-media/Originals') {}\n\n/**\n * Resizes and encodes an original into a variant, never larger than the\n * original (a width above it answers with the original's width). An original\n * too large to decode is `OriginalTooLarge`, a 413, as one too large to read.\n */\nexport class Transformer extends Context.Service<\n Transformer,\n {\n readonly transform: (\n original: Original,\n variant: Variant,\n ) => Effect.Effect<Encoded, TransformFailed | QuotaExhausted | OriginalTooLarge>\n }\n>()('foldkit-media/Transformer') {}\n\n/**\n * Variants already made, by the encoded request. A cache: losing it costs\n * time, and on a host that bills transformations, money; never data.\n */\nexport class Variants extends Context.Service<\n Variants,\n {\n readonly get: (key: string) => Effect.Effect<Option.Option<Kept>>\n readonly put: (key: string, encoded: Encoded) => Effect.Effect<void>\n }\n>()('foldkit-media/Variants') {}\n\n/** Keeps nothing: every request transforms. For a host with a cache of its own in front. */\nexport const variantsNone: Layer.Layer<Variants> = Layer.succeed(Variants, {\n get: () => Effect.succeed(Option.none()),\n put: () => Effect.void,\n})\n\n/**\n * A `Transformer` that runs at most `concurrency` transforms at once, the\n * rest waiting their turn: encoding is CPU work, and a burst of misses should\n * queue, not starve every other request. For a transformer that encodes in\n * this process (sharp, WebAssembly); a host's binding does the work elsewhere.\n */\nexport const limitTransforms = (concurrency: number) => {\n const permitsCount = positiveInteger('limitTransforms: concurrency', concurrency)\n return <E, R>(transformer: Layer.Layer<Transformer, E, R>): Layer.Layer<Transformer, E, R> =>\n Layer.effect(\n Transformer,\n Effect.gen(function* () {\n const inner = yield* Transformer\n const permits = yield* Semaphore.make(permitsCount)\n return {\n transform: (original: Original, variant: Variant) =>\n Semaphore.withPermit(permits)(inner.transform(original, variant)),\n }\n }),\n ).pipe(Layer.provide(transformer))\n}\n"],"mappings":";;;;AA6BA,IAAa,YAAb,cAA+B,QAAQ,QAGrC,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAAC;;;;;;AAOhC,IAAa,cAAb,cAAiC,QAAQ,QAQvC,CAAC,CAAC,2BAA2B,CAAC,CAAC,CAAC;;;;;AAMlC,IAAa,WAAb,cAA8B,QAAQ,QAMpC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC;;AAG/B,MAAa,eAAsC,MAAM,QAAQ,UAAU;CACzE,WAAW,OAAO,QAAQ,OAAO,KAAK,CAAC;CACvC,WAAW,OAAO;AACpB,CAAC;;;;;;;AAQD,MAAa,mBAAmB,gBAAwB;CACtD,MAAM,eAAe,gBAAgB,gCAAgC,WAAW;CAChF,QAAc,gBACZ,MAAM,OACJ,aACA,OAAO,IAAI,aAAa;EACtB,MAAM,QAAQ,OAAO;EACrB,MAAM,UAAU,OAAO,UAAU,KAAK,YAAY;EAClD,OAAO,EACL,YAAY,UAAoB,YAC9B,UAAU,WAAW,OAAO,CAAC,CAAC,MAAM,UAAU,UAAU,OAAO,CAAC,EACpE;CACF,CAAC,CACH,CAAC,CAAC,KAAK,MAAM,QAAQ,WAAW,CAAC;AACrC"}
|