foldkit-media 0.3.0 → 0.4.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/icons.js +178 -0
- 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 +22 -21
- 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 +0 -328
- 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,27 @@
|
|
|
1
|
+
import { Layer } from "effect";
|
|
2
|
+
import { KeyValueStore } from "effect/persistence";
|
|
3
|
+
import { Originals, Variants } from "./services.js";
|
|
4
|
+
/**
|
|
5
|
+
* `Variants` over a `KeyValueStore`. A variant's bytes are its value and its
|
|
6
|
+
* media type is its key's extension, so nothing else is stored. A store that
|
|
7
|
+
* fails is reported (`StoreFailed`) and read as a miss, or a write it drops: a
|
|
8
|
+
* variant is a cache, and the request is answered either way.
|
|
9
|
+
*/
|
|
10
|
+
export declare const makeVariants: (store: KeyValueStore.KeyValueStore) => Variants['Service'];
|
|
11
|
+
/**
|
|
12
|
+
* Variants kept in the application's `KeyValueStore`, under `prefix`:
|
|
13
|
+
* `KeyValueStore.layerMemory` for tests, `layerFileSystem` on a disk,
|
|
14
|
+
* `layerSql` over SQLite or D1.
|
|
15
|
+
*/
|
|
16
|
+
export declare const variantsKeyValueStore: (options?: {
|
|
17
|
+
readonly prefix?: string;
|
|
18
|
+
}) => Layer.Layer<Variants, never, KeyValueStore.KeyValueStore>;
|
|
19
|
+
/**
|
|
20
|
+
* Originals in the application's `KeyValueStore`, each under its content
|
|
21
|
+
* hash after `prefix`. A store that fails is a defect, not a missing
|
|
22
|
+
* original: the original is the data, and a 404 would say it is gone.
|
|
23
|
+
*/
|
|
24
|
+
export declare const originalsKeyValueStore: (options?: {
|
|
25
|
+
readonly prefix?: string;
|
|
26
|
+
}) => Layer.Layer<Originals, never, KeyValueStore.KeyValueStore>;
|
|
27
|
+
//# sourceMappingURL=stores.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stores.d.ts","names":[],"sources":["../../src/server/stores.ts"],"mappings":";;;;;;;;;qBAoBa,eAAY,OAAW,cAAc,kBAAgB;;;;;;qBA8BrD,wBAAqB;WACZ;MACnB,MAAM,MAAM,iBAAiB,cAAc;;;;;;qBAcjC,yBAAsB;WACb;MACnB,MAAM,MAAM,kBAAkB,cAAc"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Effect, Layer, Option, Schema } from "effect";
|
|
2
|
+
import { Format, mimeType } from "../format.js";
|
|
3
|
+
import { OriginalNotFound } from "./errors.js";
|
|
4
|
+
import { KeyValueStore } from "effect/persistence";
|
|
5
|
+
import { Diagnostics } from "foldkit-surface";
|
|
6
|
+
import { storeFailed } from "./diagnostics.js";
|
|
7
|
+
import { Originals, Variants } from "./services.js";
|
|
8
|
+
// src/server/stores.ts
|
|
9
|
+
const mediaTypeOf = (key) => {
|
|
10
|
+
const extension = key.slice(key.lastIndexOf(".") + 1);
|
|
11
|
+
return Schema.is(Format)(extension) ? mimeType(extension) : "application/octet-stream";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* `Variants` over a `KeyValueStore`. A variant's bytes are its value and its
|
|
15
|
+
* media type is its key's extension, so nothing else is stored. A store that
|
|
16
|
+
* fails is reported (`StoreFailed`) and read as a miss, or a write it drops: a
|
|
17
|
+
* variant is a cache, and the request is answered either way.
|
|
18
|
+
*/
|
|
19
|
+
const makeVariants = (store) => ({
|
|
20
|
+
get: (key) => store.getUint8Array(key).pipe(Effect.map((bytes) => Option.map(Option.fromNullishOr(bytes), (value) => ({
|
|
21
|
+
encoded: {
|
|
22
|
+
bytes: new Uint8Array(value),
|
|
23
|
+
contentType: mediaTypeOf(key)
|
|
24
|
+
},
|
|
25
|
+
from: "store"
|
|
26
|
+
}))), Effect.catch((cause) => Diagnostics.report(storeFailed({
|
|
27
|
+
store: "variants",
|
|
28
|
+
operation: "read",
|
|
29
|
+
key,
|
|
30
|
+
cause
|
|
31
|
+
})).pipe(Effect.as(Option.none())))),
|
|
32
|
+
put: (key, encoded) => store.set(key, encoded.bytes).pipe(Effect.catch((cause) => Diagnostics.report(storeFailed({
|
|
33
|
+
store: "variants",
|
|
34
|
+
operation: "write",
|
|
35
|
+
key,
|
|
36
|
+
cause
|
|
37
|
+
}))))
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* Variants kept in the application's `KeyValueStore`, under `prefix`:
|
|
41
|
+
* `KeyValueStore.layerMemory` for tests, `layerFileSystem` on a disk,
|
|
42
|
+
* `layerSql` over SQLite or D1.
|
|
43
|
+
*/
|
|
44
|
+
const variantsKeyValueStore = (options = {}) => Layer.effect(Variants, Effect.gen(function* () {
|
|
45
|
+
const store = yield* KeyValueStore.KeyValueStore;
|
|
46
|
+
return makeVariants(KeyValueStore.prefix(store, options.prefix ?? "variants/"));
|
|
47
|
+
}));
|
|
48
|
+
/**
|
|
49
|
+
* Originals in the application's `KeyValueStore`, each under its content
|
|
50
|
+
* hash after `prefix`. A store that fails is a defect, not a missing
|
|
51
|
+
* original: the original is the data, and a 404 would say it is gone.
|
|
52
|
+
*/
|
|
53
|
+
const originalsKeyValueStore = (options = {}) => Layer.effect(Originals, Effect.gen(function* () {
|
|
54
|
+
const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? "");
|
|
55
|
+
return { get: (hash) => store.getUint8Array(hash).pipe(Effect.orDie, Effect.flatMap((bytes) => bytes === void 0 ? Effect.fail(new OriginalNotFound({ hash })) : Effect.succeed({
|
|
56
|
+
body: new Blob([new Uint8Array(bytes)]).stream(),
|
|
57
|
+
size: bytes.byteLength
|
|
58
|
+
}))) };
|
|
59
|
+
}));
|
|
60
|
+
export { makeVariants, originalsKeyValueStore, variantsKeyValueStore };
|
|
61
|
+
|
|
62
|
+
//# sourceMappingURL=stores.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stores.js","names":[],"sources":["../../src/server/stores.ts"],"sourcesContent":["import { Effect, Layer, Option, Schema } from 'effect'\nimport { KeyValueStore } from 'effect/persistence'\nimport { Diagnostics } from 'foldkit-surface'\nimport { Format, mimeType } from '../format.js'\nimport { storeFailed } from './diagnostics.js'\nimport { OriginalNotFound } from './errors.js'\nimport { Originals, Variants } from './services.js'\n\n// A key ends in its variant's spelling (`<hash>/w640.avif`), which names the format.\nconst mediaTypeOf = (key: string): string => {\n const extension = key.slice(key.lastIndexOf('.') + 1)\n return Schema.is(Format)(extension) ? mimeType(extension) : 'application/octet-stream'\n}\n\n/**\n * `Variants` over a `KeyValueStore`. A variant's bytes are its value and its\n * media type is its key's extension, so nothing else is stored. A store that\n * fails is reported (`StoreFailed`) and read as a miss, or a write it drops: a\n * variant is a cache, and the request is answered either way.\n */\nexport const makeVariants = (store: KeyValueStore.KeyValueStore): Variants['Service'] => ({\n get: key =>\n store.getUint8Array(key).pipe(\n Effect.map(bytes =>\n Option.map(Option.fromNullishOr(bytes), value => ({\n encoded: { bytes: new Uint8Array(value), contentType: mediaTypeOf(key) },\n from: 'store' as const,\n })),\n ),\n Effect.catch(cause =>\n Diagnostics.report(storeFailed({ store: 'variants', operation: 'read', key, cause })).pipe(\n Effect.as(Option.none()),\n ),\n ),\n ),\n put: (key, encoded) =>\n store\n .set(key, encoded.bytes)\n .pipe(\n Effect.catch(cause =>\n Diagnostics.report(storeFailed({ store: 'variants', operation: 'write', key, cause })),\n ),\n ),\n})\n\n/**\n * Variants kept in the application's `KeyValueStore`, under `prefix`:\n * `KeyValueStore.layerMemory` for tests, `layerFileSystem` on a disk,\n * `layerSql` over SQLite or D1.\n */\nexport const variantsKeyValueStore = (\n options: { readonly prefix?: string } = {},\n): Layer.Layer<Variants, never, KeyValueStore.KeyValueStore> =>\n Layer.effect(\n Variants,\n Effect.gen(function* () {\n const store = yield* KeyValueStore.KeyValueStore\n return makeVariants(KeyValueStore.prefix(store, options.prefix ?? 'variants/'))\n }),\n )\n\n/**\n * Originals in the application's `KeyValueStore`, each under its content\n * hash after `prefix`. A store that fails is a defect, not a missing\n * original: the original is the data, and a 404 would say it is gone.\n */\nexport const originalsKeyValueStore = (\n options: { readonly prefix?: string } = {},\n): Layer.Layer<Originals, never, KeyValueStore.KeyValueStore> =>\n Layer.effect(\n Originals,\n Effect.gen(function* () {\n const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? '')\n return {\n get: (hash: string) =>\n store.getUint8Array(hash).pipe(\n Effect.orDie,\n Effect.flatMap(bytes =>\n bytes === undefined\n ? Effect.fail(new OriginalNotFound({ hash }))\n : Effect.succeed({\n body: new Blob([new Uint8Array(bytes)]).stream(),\n size: bytes.byteLength,\n }),\n ),\n ),\n }\n }),\n )\n"],"mappings":";;;;;;;;AASA,MAAM,eAAe,QAAwB;CAC3C,MAAM,YAAY,IAAI,MAAM,IAAI,YAAY,GAAG,IAAI,CAAC;CACpD,OAAO,OAAO,GAAG,MAAM,CAAC,CAAC,SAAS,IAAI,SAAS,SAAS,IAAI;AAC9D;;;;;;;AAQA,MAAa,gBAAgB,WAA6D;CACxF,MAAK,QACH,MAAM,cAAc,GAAG,CAAC,CAAC,KACvB,OAAO,KAAI,UACT,OAAO,IAAI,OAAO,cAAc,KAAK,IAAG,WAAU;EAChD,SAAS;GAAE,OAAO,IAAI,WAAW,KAAK;GAAG,aAAa,YAAY,GAAG;EAAE;EACvE,MAAM;CACR,EAAE,CACJ,GACA,OAAO,OAAM,UACX,YAAY,OAAO,YAAY;EAAE,OAAO;EAAY,WAAW;EAAQ;EAAK;CAAM,CAAC,CAAC,CAAC,CAAC,KACpF,OAAO,GAAG,OAAO,KAAK,CAAC,CACzB,CACF,CACF;CACF,MAAM,KAAK,YACT,MACG,IAAI,KAAK,QAAQ,KAAK,CAAC,CACvB,KACC,OAAO,OAAM,UACX,YAAY,OAAO,YAAY;EAAE,OAAO;EAAY,WAAW;EAAS;EAAK;CAAM,CAAC,CAAC,CACvF,CACF;AACN;;;;;;AAOA,MAAa,yBACX,UAAwC,CAAC,MAEzC,MAAM,OACJ,UACA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,OAAO,cAAc;CACnC,OAAO,aAAa,cAAc,OAAO,OAAO,QAAQ,UAAU,WAAW,CAAC;AAChF,CAAC,CACH;;;;;;AAOF,MAAa,0BACX,UAAwC,CAAC,MAEzC,MAAM,OACJ,WACA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,cAAc,OAAO,OAAO,cAAc,eAAe,QAAQ,UAAU,EAAE;CAC3F,OAAO,EACL,MAAM,SACJ,MAAM,cAAc,IAAI,CAAC,CAAC,KACxB,OAAO,OACP,OAAO,SAAQ,UACb,UAAU,KAAA,IACN,OAAO,KAAK,IAAI,iBAAiB,EAAE,KAAK,CAAC,CAAC,IAC1C,OAAO,QAAQ;EACb,MAAM,IAAI,KAAK,CAAC,IAAI,WAAW,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO;EAC/C,MAAM,MAAM;CACd,CAAC,CACP,CACF,EACJ;AACF,CAAC,CACH"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option, Schema } from "effect";
|
|
2
|
+
import { HashedAsset, ImageAsset } from "../asset.js";
|
|
3
|
+
import { Inspector } from "../describe.js";
|
|
4
|
+
import { KeyValueStore } from "effect/persistence";
|
|
5
|
+
import { HttpApi, HttpApiEndpoint, HttpApiGroup } from "effect/http-api";
|
|
6
|
+
import { HttpServerRequest } from "effect/http";
|
|
7
|
+
import { FactsOptions } from "./facts.js";
|
|
8
|
+
/**
|
|
9
|
+
* Where an original's facts enter, apart from `MediaApi`, so the variant server needs none of
|
|
10
|
+
* its services: an upload, and the facts of an image named by URL.
|
|
11
|
+
*/
|
|
12
|
+
export declare const UploadApi: HttpApi.HttpApi<"foldkit-media-upload", HttpApiGroup.HttpApiGroup<"uploads", HttpApiEndpoint.HttpApiEndpoint<"facts", "GET", "/_media/facts", never, never, never, never, Schema.toCodecJson<import("effect/http-api/HttpApiSchema").NoContent>, never, never, never> | HttpApiEndpoint.HttpApiEndpoint<"upload", "POST", "/_media/upload", never, never, never, never, Schema.toCodecJson<import("effect/http-api/HttpApiSchema").NoContent>, never, never, never>, false>>;
|
|
13
|
+
declare const Uploads_base: Context.ServiceClass<Uploads, "foldkit-media/Uploads", {
|
|
14
|
+
readonly put: (hash: string, bytes: Uint8Array<ArrayBuffer>, facts: ImageAsset) => Effect.Effect<void>;
|
|
15
|
+
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Keeps an uploaded original under its content hash, and the facts the
|
|
18
|
+
* server read from it beside it: the one writer of what `Originals` and
|
|
19
|
+
* `StoredFacts` read. The bytes are kept first, so kept facts mean kept bytes.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Uploads extends Uploads_base {}
|
|
22
|
+
declare const StoredFacts_base: Context.ServiceClass<StoredFacts, "foldkit-media/StoredFacts", {
|
|
23
|
+
readonly get: (hash: string) => Effect.Effect<Option.Option<HashedAsset>>;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* The facts the upload route read from an original, by its hash: what an
|
|
27
|
+
* application stores, rather than facts a client sends back. None for a hash
|
|
28
|
+
* nobody uploaded, or one kept before facts were.
|
|
29
|
+
*/
|
|
30
|
+
export declare class StoredFacts extends StoredFacts_base {}
|
|
31
|
+
/** Where an upload's facts are kept, beside its bytes under the same prefix. */
|
|
32
|
+
export declare const factsKey: (hash: string) => string;
|
|
33
|
+
export declare const encodeFacts: (facts: ImageAsset) => string;
|
|
34
|
+
export declare const decodeFacts: (stored: string) => HashedAsset;
|
|
35
|
+
declare const UploadForbidden_base: Schema.Class<UploadForbidden, Schema.TaggedStruct<"UploadForbidden", {}>, import("effect/Cause").YieldableError>;
|
|
36
|
+
/** The request may not upload. Why stays with the policy: the answer says only no. */
|
|
37
|
+
export declare class UploadForbidden extends UploadForbidden_base {}
|
|
38
|
+
declare const UploadUnauthenticated_base: Schema.Class<UploadUnauthenticated, Schema.TaggedStruct<"UploadUnauthenticated", {}>, import("effect/Cause").YieldableError>;
|
|
39
|
+
/**
|
|
40
|
+
* The policy could not tell who is asking (an untrusted origin, a session
|
|
41
|
+
* that ended), so the client should sign in again rather than give up.
|
|
42
|
+
*/
|
|
43
|
+
export declare class UploadUnauthenticated extends UploadUnauthenticated_base {}
|
|
44
|
+
/** Which route the policy is asked about: an upload, or the facts of an image named by URL. */
|
|
45
|
+
export type UploadRoute = 'Upload' | 'DescribeUrl';
|
|
46
|
+
declare const UploadPolicy_base: Context.ServiceClass<UploadPolicy, "foldkit-media/UploadPolicy", {
|
|
47
|
+
readonly admit: (request: HttpServerRequest.HttpServerRequest, route: UploadRoute) => Effect.Effect<void, UploadForbidden | UploadUnauthenticated>;
|
|
48
|
+
}>;
|
|
49
|
+
/**
|
|
50
|
+
* Who may upload, asked of every request before its body is read, with the
|
|
51
|
+
* route it is for. The route has no default: an application that serves
|
|
52
|
+
* uploads says who may (`foldkit-guard/media` over the session, a secret), so
|
|
53
|
+
* a server is never open to every upload by omission.
|
|
54
|
+
*/
|
|
55
|
+
export declare class UploadPolicy extends UploadPolicy_base {}
|
|
56
|
+
export interface UploadOptions {
|
|
57
|
+
/** The largest upload read, in bytes; a larger one is a 413, read no further. */
|
|
58
|
+
readonly maxBytes?: number;
|
|
59
|
+
/**
|
|
60
|
+
* How `GET /_media/facts?url=` reads an image named by URL (`factsOf`): the hosts it may
|
|
61
|
+
* fetch from, and a cache. No hosts unless given, so a server names every host it reads.
|
|
62
|
+
*/
|
|
63
|
+
readonly facts?: Omit<FactsOptions, 'allow'> & {
|
|
64
|
+
readonly allow?: ReadonlyArray<string>;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The upload route's handler. It asks the `UploadPolicy` first; takes only
|
|
69
|
+
* an `image/*` body, which a browser cannot send to another origin without a
|
|
70
|
+
* preflight this route never answers, so a cookie alone cannot upload from
|
|
71
|
+
* another site; reads at most `maxBytes`; and refuses what the `Inspector`
|
|
72
|
+
* cannot read as an image (415). Its answer is the facts, with the hash as
|
|
73
|
+
* the key, `201`.
|
|
74
|
+
*/
|
|
75
|
+
export declare const uploadHandlers: (options?: UploadOptions) => Layer.Layer<HttpApiGroup.Service<"foldkit-media-upload", "uploads">, never, Inspector | UploadPolicy | Uploads>;
|
|
76
|
+
/**
|
|
77
|
+
* The upload route as an Effect HTTP application, registered with the
|
|
78
|
+
* `HttpRouter` beside the variant server's, needing `Uploads`, an
|
|
79
|
+
* `Inspector` and the `UploadPolicy`.
|
|
80
|
+
*/
|
|
81
|
+
export declare const uploadLayer: (options?: UploadOptions) => Layer.Layer<never, never, import("effect/FileSystem").FileSystem | import("effect/http/Etag").Generator | import("effect/http/HttpPlatform").HttpPlatform | import("effect/http/HttpRouter").HttpRouter | Inspector | import("effect/Path").Path | UploadPolicy | Uploads>;
|
|
82
|
+
/**
|
|
83
|
+
* Policies tried in turn, the first to admit winning: a deployment's secret
|
|
84
|
+
* for scripts beside sessions for editors. A refusal tries the next; when
|
|
85
|
+
* every one refuses, the last refusal is the answer. Each is built once, with
|
|
86
|
+
* the combined layer, and closes with it.
|
|
87
|
+
*/
|
|
88
|
+
export declare const uploadPolicyAny: <E, R>(...policies: readonly [Layer.Layer<UploadPolicy, E, R>, Layer.Layer<UploadPolicy, E, R>, ...ReadonlyArray<Layer.Layer<UploadPolicy, E, R>>]) => Layer.Layer<UploadPolicy, E, R>;
|
|
89
|
+
/**
|
|
90
|
+
* Admits a request whose `Authorization` is `Bearer <token>`, and with no
|
|
91
|
+
* token (unset or empty) admits nobody, whatever it sends. The two are
|
|
92
|
+
* compared by digest, every byte, so the time taken says nothing of the
|
|
93
|
+
* token. For a deployment's secret (`wrangler secret put`); who may upload
|
|
94
|
+
* by session is the application's own `UploadPolicy`.
|
|
95
|
+
*/
|
|
96
|
+
export declare const uploadPolicyBearer: (token: string | undefined) => Layer.Layer<UploadPolicy>;
|
|
97
|
+
/**
|
|
98
|
+
* Uploads kept in the application's `KeyValueStore`, each under its content
|
|
99
|
+
* hash after `prefix`: the same keys `originalsKeyValueStore` reads, with the
|
|
100
|
+
* same `prefix`. A store that fails is a defect: an upload it dropped is lost.
|
|
101
|
+
*/
|
|
102
|
+
export declare const uploadsKeyValueStore: (options?: {
|
|
103
|
+
readonly prefix?: string;
|
|
104
|
+
}) => Layer.Layer<Uploads, never, KeyValueStore.KeyValueStore>;
|
|
105
|
+
/**
|
|
106
|
+
* Upload facts read back from the application's `KeyValueStore`, under the
|
|
107
|
+
* same `prefix` as `uploadsKeyValueStore`. A store that fails is a defect.
|
|
108
|
+
*/
|
|
109
|
+
export declare const storedFactsKeyValueStore: (options?: {
|
|
110
|
+
readonly prefix?: string;
|
|
111
|
+
}) => Layer.Layer<StoredFacts, never, KeyValueStore.KeyValueStore>;
|
|
112
|
+
export {};
|
|
113
|
+
//# sourceMappingURL=upload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.d.ts","names":[],"sources":["../../src/server/upload.ts"],"mappings":";;;;;;;;;;;qBAwBa,WAAS,QAAA,gCAAA,aAAA,wBAAA,gBAAA,6EAAA,OAAA,oDAAA,mCAAA,gBAAA,gFAAA,OAAA,oDAAA;;WAeJ,MACZ,cACA,OAAO,WAAW,cAClB,OAAO,eACJ,OAAO;;;;;;;qBAPH,gBAAgB;;WAkBX,MAAC,iBAAiB,OAAO,OAAO,OAAO,OAAO;;;;;;;qBAFnD,oBAAoB;;qBAMpB,WAAQ;qBAER,cAAW,OAAW;qBAItB,cAAW,mBAAqB;;;qBAIhC,wBAAwB;;;;;;qBAUxB,8BAA8B;;YAO/B;;WAWQ,QACd,SAAS,kBAAkB,mBAC3B,OAAO,gBACJ,OAAO,aAAa,kBAAkB;;;;;;;;qBANlC,qBAAqB;iBAajB;;WAEN;;;;;WAKA,QAAQ,KAAK;aAAoC,QAAQ;;;;;;;;;;;qBA0CvD,iBAAc,UAAa,kBAAa,MAAA,MAAA,aAAA,mDAAA,YAAA,eAAA;;;;;;qBAkExC,cAAW,UAAa,kBAAa,MAAA,gDAAA,wCAAA,+CAAA,gDAAA,aAAA,kCAAA,OAAA,eAAA;;;;;;;qBASrC,kBAAmB,GAAG,MAAC,oBAEhC,MAAM,MAAM,cAAc,GAAG,IAC7B,MAAM,MAAM,cAAc,GAAG,OAC1B,cAAc,MAAM,MAAM,cAAc,GAAG,SAE/C,MAAM,MAAM,cAAc,GAAG;;;;;;;;qBA8BnB,qBAAkB,8BAAgC,MAAM,MAAM;;;;;;qBAoB9D,uBAAoB;WACX;MACnB,MAAM,MAAM,gBAAgB,cAAc;;;;;qBAkBhC,2BAAwB;WACf;MACnB,MAAM,MAAM,oBAAoB,cAAc"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { Context, Effect, Layer, Option, Schema, Stream } from "effect";
|
|
2
|
+
import { HashedAsset, ImageAsset } from "../asset.js";
|
|
3
|
+
import { describe } from "../describe.js";
|
|
4
|
+
import { nonNegativeInteger } from "../limits.js";
|
|
5
|
+
import { OriginalTooLarge } from "./errors.js";
|
|
6
|
+
import { KeyValueStore } from "effect/persistence";
|
|
7
|
+
import { Diagnostics } from "foldkit-surface";
|
|
8
|
+
import { factsUnanswered } from "./diagnostics.js";
|
|
9
|
+
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup } from "effect/http-api";
|
|
10
|
+
import { HttpServerResponse } from "effect/http";
|
|
11
|
+
import { Answer, headersFor, statusOf } from "./headers.js";
|
|
12
|
+
import { factsOf } from "./facts.js";
|
|
13
|
+
// src/server/upload.ts
|
|
14
|
+
/**
|
|
15
|
+
* Where an original enters: `POST /_media/upload` takes an image's bytes,
|
|
16
|
+
* reads its facts (`describe`, through the `Inspector`), keeps the bytes
|
|
17
|
+
* under their content hash (`Uploads`), and answers the facts as an
|
|
18
|
+
* `ImageAsset`, for whoever registers them (a CMS's asset row). The bytes
|
|
19
|
+
* travel once, over HTTP; only the facts go on to the application's data.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Where an original's facts enter, apart from `MediaApi`, so the variant server needs none of
|
|
23
|
+
* its services: an upload, and the facts of an image named by URL.
|
|
24
|
+
*/
|
|
25
|
+
const UploadApi = HttpApi.make("foldkit-media-upload").add(HttpApiGroup.make("uploads").add(HttpApiEndpoint.post("upload", "/_media/upload"), HttpApiEndpoint.get("facts", "/_media/facts")));
|
|
26
|
+
/**
|
|
27
|
+
* Keeps an uploaded original under its content hash, and the facts the
|
|
28
|
+
* server read from it beside it: the one writer of what `Originals` and
|
|
29
|
+
* `StoredFacts` read. The bytes are kept first, so kept facts mean kept bytes.
|
|
30
|
+
*/
|
|
31
|
+
var Uploads = class extends Context.Service()("foldkit-media/Uploads") {};
|
|
32
|
+
/**
|
|
33
|
+
* The facts the upload route read from an original, by its hash: what an
|
|
34
|
+
* application stores, rather than facts a client sends back. None for a hash
|
|
35
|
+
* nobody uploaded, or one kept before facts were.
|
|
36
|
+
*/
|
|
37
|
+
var StoredFacts = class extends Context.Service()("foldkit-media/StoredFacts") {};
|
|
38
|
+
/** Where an upload's facts are kept, beside its bytes under the same prefix. */
|
|
39
|
+
const factsKey = (hash) => `facts/${hash}`;
|
|
40
|
+
const encodeFacts = (facts) => JSON.stringify(Schema.encodeSync(ImageAsset)(facts));
|
|
41
|
+
const decodeFacts = (stored) => Schema.decodeUnknownSync(Schema.fromJsonString(HashedAsset))(stored);
|
|
42
|
+
/** The request may not upload. Why stays with the policy: the answer says only no. */
|
|
43
|
+
var UploadForbidden = class extends Schema.TaggedError()("UploadForbidden", {}, { httpApiStatus: 403 }) {};
|
|
44
|
+
/**
|
|
45
|
+
* The policy could not tell who is asking (an untrusted origin, a session
|
|
46
|
+
* that ended), so the client should sign in again rather than give up.
|
|
47
|
+
*/
|
|
48
|
+
var UploadUnauthenticated = class extends Schema.TaggedError()("UploadUnauthenticated", {}, { httpApiStatus: 401 }) {};
|
|
49
|
+
/**
|
|
50
|
+
* Who may upload, asked of every request before its body is read, with the
|
|
51
|
+
* route it is for. The route has no default: an application that serves
|
|
52
|
+
* uploads says who may (`foldkit-guard/media` over the session, a secret), so
|
|
53
|
+
* a server is never open to every upload by omission.
|
|
54
|
+
*/
|
|
55
|
+
var UploadPolicy = class extends Context.Service()("foldkit-media/UploadPolicy") {};
|
|
56
|
+
/** The Cloudflare Images binding reads at most 20 MB; so does the variant server by default. */
|
|
57
|
+
const DEFAULT_MAX_BYTES = 20971520;
|
|
58
|
+
const answered = (answer, body) => HttpServerResponse.jsonUnsafe(body, {
|
|
59
|
+
status: statusOf(answer),
|
|
60
|
+
headers: headersFor(answer)
|
|
61
|
+
});
|
|
62
|
+
const refused = (answer) => answered(answer, { error: answer._tag });
|
|
63
|
+
/** The body, refused past `maxBytes` as it streams: a declared length is the client's to write, so it is not read. */
|
|
64
|
+
const bodyOf = (request, maxBytes) => Effect.gen(function* () {
|
|
65
|
+
const chunks = [];
|
|
66
|
+
let size = 0;
|
|
67
|
+
yield* request.stream.pipe(Stream.runForEach((chunk) => Effect.suspend(() => {
|
|
68
|
+
size += chunk.byteLength;
|
|
69
|
+
if (size > maxBytes) return Effect.fail(new OriginalTooLarge({
|
|
70
|
+
size,
|
|
71
|
+
limit: maxBytes,
|
|
72
|
+
unit: "bytes"
|
|
73
|
+
}));
|
|
74
|
+
chunks.push(chunk);
|
|
75
|
+
return Effect.void;
|
|
76
|
+
})));
|
|
77
|
+
const bytes = new Uint8Array(size);
|
|
78
|
+
let at = 0;
|
|
79
|
+
for (const chunk of chunks) {
|
|
80
|
+
bytes.set(chunk, at);
|
|
81
|
+
at += chunk.byteLength;
|
|
82
|
+
}
|
|
83
|
+
return bytes;
|
|
84
|
+
});
|
|
85
|
+
/**
|
|
86
|
+
* The upload route's handler. It asks the `UploadPolicy` first; takes only
|
|
87
|
+
* an `image/*` body, which a browser cannot send to another origin without a
|
|
88
|
+
* preflight this route never answers, so a cookie alone cannot upload from
|
|
89
|
+
* another site; reads at most `maxBytes`; and refuses what the `Inspector`
|
|
90
|
+
* cannot read as an image (415). Its answer is the facts, with the hash as
|
|
91
|
+
* the key, `201`.
|
|
92
|
+
*/
|
|
93
|
+
const uploadHandlers = (options = {}) => {
|
|
94
|
+
const maxBytes = nonNegativeInteger("maxBytes", options.maxBytes ?? DEFAULT_MAX_BYTES);
|
|
95
|
+
const encode = Schema.encodeSync(ImageAsset);
|
|
96
|
+
return HttpApiBuilder.group(UploadApi, "uploads", (group) => Effect.gen(function* () {
|
|
97
|
+
const uploads = yield* Uploads;
|
|
98
|
+
const policy = yield* UploadPolicy;
|
|
99
|
+
const services = yield* Effect.context();
|
|
100
|
+
const facts = {
|
|
101
|
+
...options.facts,
|
|
102
|
+
allow: options.facts?.allow ?? []
|
|
103
|
+
};
|
|
104
|
+
return group.handle("upload", ({ request }) => Effect.gen(function* () {
|
|
105
|
+
yield* policy.admit(request, "Upload");
|
|
106
|
+
if (!(request.headers["content-type"] ?? "").startsWith("image/")) return refused(Answer.cases.Unsupported.make({}));
|
|
107
|
+
const bytes = yield* bodyOf(request, maxBytes);
|
|
108
|
+
const described = yield* describe("upload", bytes);
|
|
109
|
+
const hash = Option.getOrThrow(described.hash);
|
|
110
|
+
const facts = {
|
|
111
|
+
...described,
|
|
112
|
+
key: hash
|
|
113
|
+
};
|
|
114
|
+
yield* uploads.put(hash, bytes, facts);
|
|
115
|
+
return answered(Answer.cases.Uploaded.make({}), encode(facts));
|
|
116
|
+
}).pipe(Effect.provide(services), Effect.catchTags({
|
|
117
|
+
UploadForbidden: () => Effect.succeed(refused(Answer.cases.Forbidden.make({}))),
|
|
118
|
+
UploadUnauthenticated: () => Effect.succeed(refused(Answer.cases.Unauthenticated.make({}))),
|
|
119
|
+
OriginalTooLarge: () => Effect.succeed(refused(Answer.cases.TooLarge.make({}))),
|
|
120
|
+
TransformFailed: () => Effect.succeed(refused(Answer.cases.Unsupported.make({}))),
|
|
121
|
+
HttpServerError: () => Effect.succeed(refused(Answer.cases.Malformed.make({})))
|
|
122
|
+
}), Effect.withSpan("Media.upload"))).handle("facts", ({ request }) => Effect.gen(function* () {
|
|
123
|
+
yield* policy.admit(request, "DescribeUrl");
|
|
124
|
+
const url = new URL(request.url, "http://media.invalid").searchParams.get("url");
|
|
125
|
+
if (url === null) return refused(Answer.cases.Malformed.make({}));
|
|
126
|
+
return answered(Answer.cases.Described.make({}), encode(yield* factsOf(url, facts)));
|
|
127
|
+
}).pipe(Effect.provide(services), Effect.catchTags({
|
|
128
|
+
UploadForbidden: () => Effect.succeed(refused(Answer.cases.Forbidden.make({}))),
|
|
129
|
+
UploadUnauthenticated: () => Effect.succeed(refused(Answer.cases.Unauthenticated.make({}))),
|
|
130
|
+
FactsUnavailable: ({ url, reason }) => Diagnostics.report(factsUnanswered(url, reason)).pipe(Effect.as(refused(Answer.cases.Unavailable.make({}))))
|
|
131
|
+
}), Effect.withSpan("Media.facts")));
|
|
132
|
+
}));
|
|
133
|
+
};
|
|
134
|
+
/**
|
|
135
|
+
* The upload route as an Effect HTTP application, registered with the
|
|
136
|
+
* `HttpRouter` beside the variant server's, needing `Uploads`, an
|
|
137
|
+
* `Inspector` and the `UploadPolicy`.
|
|
138
|
+
*/
|
|
139
|
+
const uploadLayer = (options = {}) => HttpApiBuilder.layer(UploadApi).pipe(Layer.provide(uploadHandlers(options)));
|
|
140
|
+
/**
|
|
141
|
+
* Policies tried in turn, the first to admit winning: a deployment's secret
|
|
142
|
+
* for scripts beside sessions for editors. A refusal tries the next; when
|
|
143
|
+
* every one refuses, the last refusal is the answer. Each is built once, with
|
|
144
|
+
* the combined layer, and closes with it.
|
|
145
|
+
*/
|
|
146
|
+
const uploadPolicyAny = (...policies) => Layer.effect(UploadPolicy, Effect.gen(function* () {
|
|
147
|
+
const [head, ...tail] = policies;
|
|
148
|
+
const first = Context.get(yield* Layer.build(head), UploadPolicy);
|
|
149
|
+
const rest = [];
|
|
150
|
+
for (const policy of tail) rest.push(Context.get(yield* Layer.build(policy), UploadPolicy));
|
|
151
|
+
return { admit: (request, route) => rest.reduce((asked, next) => Effect.catch(asked, () => next.admit(request, route)), first.admit(request, route)) };
|
|
152
|
+
}));
|
|
153
|
+
const digestOf = (text) => Effect.promise(() => crypto.subtle.digest("SHA-256", new TextEncoder().encode(text))).pipe(Effect.map((digest) => new Uint8Array(digest)));
|
|
154
|
+
/**
|
|
155
|
+
* Admits a request whose `Authorization` is `Bearer <token>`, and with no
|
|
156
|
+
* token (unset or empty) admits nobody, whatever it sends. The two are
|
|
157
|
+
* compared by digest, every byte, so the time taken says nothing of the
|
|
158
|
+
* token. For a deployment's secret (`wrangler secret put`); who may upload
|
|
159
|
+
* by session is the application's own `UploadPolicy`.
|
|
160
|
+
*/
|
|
161
|
+
const uploadPolicyBearer = (token) => Layer.succeed(UploadPolicy, { admit: (request) => Effect.all([digestOf(request.headers["authorization"] ?? ""), digestOf(`Bearer ${token}`)]).pipe(Effect.flatMap(([given, expected]) => {
|
|
162
|
+
let difference = token === void 0 || token === "" ? 1 : 0;
|
|
163
|
+
for (let at = 0; at < expected.length; at++) difference |= given[at] ^ expected[at];
|
|
164
|
+
return difference === 0 ? Effect.void : Effect.fail(new UploadForbidden({}));
|
|
165
|
+
})) });
|
|
166
|
+
/**
|
|
167
|
+
* Uploads kept in the application's `KeyValueStore`, each under its content
|
|
168
|
+
* hash after `prefix`: the same keys `originalsKeyValueStore` reads, with the
|
|
169
|
+
* same `prefix`. A store that fails is a defect: an upload it dropped is lost.
|
|
170
|
+
*/
|
|
171
|
+
const uploadsKeyValueStore = (options = {}) => Layer.effect(Uploads, Effect.gen(function* () {
|
|
172
|
+
const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? "");
|
|
173
|
+
return { put: (hash, bytes, facts) => store.set(hash, bytes).pipe(Effect.andThen(store.set(factsKey(hash), encodeFacts(facts))), Effect.orDie) };
|
|
174
|
+
}));
|
|
175
|
+
/**
|
|
176
|
+
* Upload facts read back from the application's `KeyValueStore`, under the
|
|
177
|
+
* same `prefix` as `uploadsKeyValueStore`. A store that fails is a defect.
|
|
178
|
+
*/
|
|
179
|
+
const storedFactsKeyValueStore = (options = {}) => Layer.effect(StoredFacts, Effect.gen(function* () {
|
|
180
|
+
const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? "");
|
|
181
|
+
return { get: (hash) => store.get(factsKey(hash)).pipe(Effect.map((found) => Option.map(Option.fromNullishOr(found), decodeFacts)), Effect.orDie) };
|
|
182
|
+
}));
|
|
183
|
+
export { StoredFacts, UploadApi, UploadForbidden, UploadPolicy, UploadUnauthenticated, Uploads, decodeFacts, encodeFacts, factsKey, storedFactsKeyValueStore, uploadHandlers, uploadLayer, uploadPolicyAny, uploadPolicyBearer, uploadsKeyValueStore };
|
|
184
|
+
|
|
185
|
+
//# sourceMappingURL=upload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload.js","names":[],"sources":["../../src/server/upload.ts"],"sourcesContent":["/**\n * Where an original enters: `POST /_media/upload` takes an image's bytes,\n * reads its facts (`describe`, through the `Inspector`), keeps the bytes\n * under their content hash (`Uploads`), and answers the facts as an\n * `ImageAsset`, for whoever registers them (a CMS's asset row). The bytes\n * travel once, over HTTP; only the facts go on to the application's data.\n */\nimport { Context, Effect, Layer, Option, Schema, Stream } from 'effect'\nimport { HttpServerResponse, type HttpServerRequest } from 'effect/http'\nimport { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup } from 'effect/http-api'\nimport { KeyValueStore } from 'effect/persistence'\nimport { Diagnostics } from 'foldkit-surface'\nimport { HashedAsset, ImageAsset } from '../asset.js'\nimport { describe, type Inspector } from '../describe.js'\nimport { nonNegativeInteger } from '../limits.js'\nimport { factsUnanswered } from './diagnostics.js'\nimport { OriginalTooLarge } from './errors.js'\nimport { factsOf, type FactsOptions } from './facts.js'\nimport { Answer, headersFor, statusOf } from './headers.js'\n\n/**\n * Where an original's facts enter, apart from `MediaApi`, so the variant server needs none of\n * its services: an upload, and the facts of an image named by URL.\n */\nexport const UploadApi = HttpApi.make('foldkit-media-upload').add(\n HttpApiGroup.make('uploads').add(\n HttpApiEndpoint.post('upload', '/_media/upload'),\n HttpApiEndpoint.get('facts', '/_media/facts'),\n ),\n)\n\n/**\n * Keeps an uploaded original under its content hash, and the facts the\n * server read from it beside it: the one writer of what `Originals` and\n * `StoredFacts` read. The bytes are kept first, so kept facts mean kept bytes.\n */\nexport class Uploads extends Context.Service<\n Uploads,\n {\n readonly put: (\n hash: string,\n bytes: Uint8Array<ArrayBuffer>,\n facts: ImageAsset,\n ) => Effect.Effect<void>\n }\n>()('foldkit-media/Uploads') {}\n\n/**\n * The facts the upload route read from an original, by its hash: what an\n * application stores, rather than facts a client sends back. None for a hash\n * nobody uploaded, or one kept before facts were.\n */\nexport class StoredFacts extends Context.Service<\n StoredFacts,\n { readonly get: (hash: string) => Effect.Effect<Option.Option<HashedAsset>> }\n>()('foldkit-media/StoredFacts') {}\n\n/** Where an upload's facts are kept, beside its bytes under the same prefix. */\nexport const factsKey = (hash: string): string => `facts/${hash}`\n\nexport const encodeFacts = (facts: ImageAsset): string =>\n JSON.stringify(Schema.encodeSync(ImageAsset)(facts))\n\n// Facts the route wrote are hashed; any other entry is a defect, never a silent none.\nexport const decodeFacts = (stored: string): HashedAsset =>\n Schema.decodeUnknownSync(Schema.fromJsonString(HashedAsset))(stored)\n\n/** The request may not upload. Why stays with the policy: the answer says only no. */\nexport class UploadForbidden extends Schema.TaggedError<UploadForbidden>()(\n 'UploadForbidden',\n {},\n { httpApiStatus: 403 },\n) {}\n\n/**\n * The policy could not tell who is asking (an untrusted origin, a session\n * that ended), so the client should sign in again rather than give up.\n */\nexport class UploadUnauthenticated extends Schema.TaggedError<UploadUnauthenticated>()(\n 'UploadUnauthenticated',\n {},\n { httpApiStatus: 401 },\n) {}\n\n/** Which route the policy is asked about: an upload, or the facts of an image named by URL. */\nexport type UploadRoute = 'Upload' | 'DescribeUrl'\n\n/**\n * Who may upload, asked of every request before its body is read, with the\n * route it is for. The route has no default: an application that serves\n * uploads says who may (`foldkit-guard/media` over the session, a secret), so\n * a server is never open to every upload by omission.\n */\nexport class UploadPolicy extends Context.Service<\n UploadPolicy,\n {\n readonly admit: (\n request: HttpServerRequest.HttpServerRequest,\n route: UploadRoute,\n ) => Effect.Effect<void, UploadForbidden | UploadUnauthenticated>\n }\n>()('foldkit-media/UploadPolicy') {}\n\n/** The Cloudflare Images binding reads at most 20 MB; so does the variant server by default. */\nconst DEFAULT_MAX_BYTES = 20 * 1024 * 1024\n\nexport interface UploadOptions {\n /** The largest upload read, in bytes; a larger one is a 413, read no further. */\n readonly maxBytes?: number\n /**\n * How `GET /_media/facts?url=` reads an image named by URL (`factsOf`): the hosts it may\n * fetch from, and a cache. No hosts unless given, so a server names every host it reads.\n */\n readonly facts?: Omit<FactsOptions, 'allow'> & { readonly allow?: ReadonlyArray<string> }\n}\n\nconst answered = (answer: Answer, body: unknown) =>\n HttpServerResponse.jsonUnsafe(body, { status: statusOf(answer), headers: headersFor(answer) })\n\nconst refused = (answer: Answer) => answered(answer, { error: answer._tag })\n\n/** The body, refused past `maxBytes` as it streams: a declared length is the client's to write, so it is not read. */\nconst bodyOf = (request: HttpServerRequest.HttpServerRequest, maxBytes: number) =>\n Effect.gen(function* () {\n const chunks: Array<Uint8Array> = []\n let size = 0\n yield* request.stream.pipe(\n Stream.runForEach(chunk =>\n Effect.suspend(() => {\n size += chunk.byteLength\n if (size > maxBytes) {\n return Effect.fail(new OriginalTooLarge({ size, limit: maxBytes, unit: 'bytes' }))\n }\n chunks.push(chunk)\n return Effect.void\n }),\n ),\n )\n const bytes = new Uint8Array(size)\n let at = 0\n for (const chunk of chunks) {\n bytes.set(chunk, at)\n at += chunk.byteLength\n }\n return bytes\n })\n\n/**\n * The upload route's handler. It asks the `UploadPolicy` first; takes only\n * an `image/*` body, which a browser cannot send to another origin without a\n * preflight this route never answers, so a cookie alone cannot upload from\n * another site; reads at most `maxBytes`; and refuses what the `Inspector`\n * cannot read as an image (415). Its answer is the facts, with the hash as\n * the key, `201`.\n */\nexport const uploadHandlers = (options: UploadOptions = {}) => {\n const maxBytes = nonNegativeInteger('maxBytes', options.maxBytes ?? DEFAULT_MAX_BYTES)\n const encode = Schema.encodeSync(ImageAsset)\n return HttpApiBuilder.group(UploadApi, 'uploads', group =>\n Effect.gen(function* () {\n const uploads = yield* Uploads\n const policy = yield* UploadPolicy\n const services = yield* Effect.context<Inspector>()\n const facts = { ...options.facts, allow: options.facts?.allow ?? [] }\n return group\n .handle('upload', ({ request }) =>\n Effect.gen(function* () {\n yield* policy.admit(request, 'Upload')\n if (!(request.headers['content-type'] ?? '').startsWith('image/')) {\n return refused(Answer.cases.Unsupported.make({}))\n }\n const bytes = yield* bodyOf(request, maxBytes)\n const described = yield* describe('upload', bytes)\n const hash = Option.getOrThrow(described.hash)\n const facts = { ...described, key: hash }\n yield* uploads.put(hash, bytes, facts)\n return answered(Answer.cases.Uploaded.make({}), encode(facts))\n }).pipe(\n Effect.provide(services),\n Effect.catchTags({\n UploadForbidden: () => Effect.succeed(refused(Answer.cases.Forbidden.make({}))),\n UploadUnauthenticated: () =>\n Effect.succeed(refused(Answer.cases.Unauthenticated.make({}))),\n OriginalTooLarge: () => Effect.succeed(refused(Answer.cases.TooLarge.make({}))),\n TransformFailed: () => Effect.succeed(refused(Answer.cases.Unsupported.make({}))),\n // The body broke off or could not be read: the request's fault, not the server's.\n HttpServerError: () => Effect.succeed(refused(Answer.cases.Malformed.make({}))),\n }),\n Effect.withSpan('Media.upload'),\n ),\n )\n .handle('facts', ({ request }) =>\n Effect.gen(function* () {\n yield* policy.admit(request, 'DescribeUrl')\n const url = new URL(request.url, 'http://media.invalid').searchParams.get('url')\n if (url === null) return refused(Answer.cases.Malformed.make({}))\n return answered(Answer.cases.Described.make({}), encode(yield* factsOf(url, facts)))\n }).pipe(\n Effect.provide(services),\n Effect.catchTags({\n UploadForbidden: () => Effect.succeed(refused(Answer.cases.Forbidden.make({}))),\n UploadUnauthenticated: () =>\n Effect.succeed(refused(Answer.cases.Unauthenticated.make({}))),\n // Why stays in the server: a host refused, a read failed, a file that is no image.\n FactsUnavailable: ({ url, reason }) =>\n Diagnostics.report(factsUnanswered(url, reason)).pipe(\n Effect.as(refused(Answer.cases.Unavailable.make({}))),\n ),\n }),\n Effect.withSpan('Media.facts'),\n ),\n )\n }),\n )\n}\n\n/**\n * The upload route as an Effect HTTP application, registered with the\n * `HttpRouter` beside the variant server's, needing `Uploads`, an\n * `Inspector` and the `UploadPolicy`.\n */\nexport const uploadLayer = (options: UploadOptions = {}) =>\n HttpApiBuilder.layer(UploadApi).pipe(Layer.provide(uploadHandlers(options)))\n\n/**\n * Policies tried in turn, the first to admit winning: a deployment's secret\n * for scripts beside sessions for editors. A refusal tries the next; when\n * every one refuses, the last refusal is the answer. Each is built once, with\n * the combined layer, and closes with it.\n */\nexport const uploadPolicyAny = <E, R>(\n ...policies: readonly [\n Layer.Layer<UploadPolicy, E, R>,\n Layer.Layer<UploadPolicy, E, R>,\n ...ReadonlyArray<Layer.Layer<UploadPolicy, E, R>>,\n ]\n): Layer.Layer<UploadPolicy, E, R> =>\n Layer.effect(\n UploadPolicy,\n Effect.gen(function* () {\n const [head, ...tail] = policies\n const first = Context.get(yield* Layer.build(head), UploadPolicy)\n const rest: Array<typeof UploadPolicy.Service> = []\n for (const policy of tail) rest.push(Context.get(yield* Layer.build(policy), UploadPolicy))\n return {\n admit: (request, route) =>\n rest.reduce(\n (asked, next) => Effect.catch(asked, () => next.admit(request, route)),\n first.admit(request, route),\n ),\n }\n }),\n )\n\nconst digestOf = (text: string) =>\n Effect.promise(() => crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))).pipe(\n Effect.map(digest => new Uint8Array(digest)),\n )\n\n/**\n * Admits a request whose `Authorization` is `Bearer <token>`, and with no\n * token (unset or empty) admits nobody, whatever it sends. The two are\n * compared by digest, every byte, so the time taken says nothing of the\n * token. For a deployment's secret (`wrangler secret put`); who may upload\n * by session is the application's own `UploadPolicy`.\n */\nexport const uploadPolicyBearer = (token: string | undefined): Layer.Layer<UploadPolicy> =>\n Layer.succeed(UploadPolicy, {\n admit: request =>\n Effect.all([\n digestOf(request.headers['authorization'] ?? ''),\n digestOf(`Bearer ${token}`),\n ]).pipe(\n Effect.flatMap(([given, expected]) => {\n let difference = token === undefined || token === '' ? 1 : 0\n for (let at = 0; at < expected.length; at++) difference |= given[at]! ^ expected[at]!\n return difference === 0 ? Effect.void : Effect.fail(new UploadForbidden({}))\n }),\n ),\n })\n\n/**\n * Uploads kept in the application's `KeyValueStore`, each under its content\n * hash after `prefix`: the same keys `originalsKeyValueStore` reads, with the\n * same `prefix`. A store that fails is a defect: an upload it dropped is lost.\n */\nexport const uploadsKeyValueStore = (\n options: { readonly prefix?: string } = {},\n): Layer.Layer<Uploads, never, KeyValueStore.KeyValueStore> =>\n Layer.effect(\n Uploads,\n Effect.gen(function* () {\n const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? '')\n return {\n put: (hash: string, bytes: Uint8Array<ArrayBuffer>, facts: ImageAsset) =>\n store\n .set(hash, bytes)\n .pipe(Effect.andThen(store.set(factsKey(hash), encodeFacts(facts))), Effect.orDie),\n }\n }),\n )\n\n/**\n * Upload facts read back from the application's `KeyValueStore`, under the\n * same `prefix` as `uploadsKeyValueStore`. A store that fails is a defect.\n */\nexport const storedFactsKeyValueStore = (\n options: { readonly prefix?: string } = {},\n): Layer.Layer<StoredFacts, never, KeyValueStore.KeyValueStore> =>\n Layer.effect(\n StoredFacts,\n Effect.gen(function* () {\n const store = KeyValueStore.prefix(yield* KeyValueStore.KeyValueStore, options.prefix ?? '')\n return {\n get: (hash: string) =>\n store.get(factsKey(hash)).pipe(\n Effect.map(found => Option.map(Option.fromNullishOr(found), decodeFacts)),\n Effect.orDie,\n ),\n }\n }),\n )\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAwBA,MAAa,YAAY,QAAQ,KAAK,sBAAsB,CAAC,CAAC,IAC5D,aAAa,KAAK,SAAS,CAAC,CAAC,IAC3B,gBAAgB,KAAK,UAAU,gBAAgB,GAC/C,gBAAgB,IAAI,SAAS,eAAe,CAC9C,CACF;;;;;;AAOA,IAAa,UAAb,cAA6B,QAAQ,QASnC,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC;;;;;;AAO9B,IAAa,cAAb,c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,OAAgC,UAClD,MACG,IAAI,MAAM,KAAK,CAAC,CAChB,KAAK,OAAO,QAAQ,MAAM,IAAI,SAAS,IAAI,GAAG,YAAY,KAAK,CAAC,CAAC,GAAG,OAAO,KAAK,EACvF;AACF,CAAC,CACH;;;;;AAMF,MAAa,4BACX,UAAwC,CAAC,MAEzC,MAAM,OACJ,aACA,OAAO,IAAI,aAAa;CACtB,MAAM,QAAQ,cAAc,OAAO,OAAO,cAAc,eAAe,QAAQ,UAAU,EAAE;CAC3F,OAAO,EACL,MAAM,SACJ,MAAM,IAAI,SAAS,IAAI,CAAC,CAAC,CAAC,KACxB,OAAO,KAAI,UAAS,OAAO,IAAI,OAAO,cAAc,KAAK,GAAG,WAAW,CAAC,GACxE,OAAO,KACT,EACJ;AACF,CAAC,CACH"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { n as Inspector } from "../describe-BOgc0xbR.mjs";
|
|
2
|
-
import { a as Transformer } from "../services-BZoK2VOY.mjs";
|
|
3
1
|
import { Layer } from "effect";
|
|
2
|
+
import { Inspector } from "../describe.js";
|
|
3
|
+
import { Transformer } from "../server/services.js";
|
|
4
4
|
export declare const transformerSharp: (options?: {
|
|
5
5
|
/** Transforms run at once; by default the machine's cores. */
|
|
6
6
|
readonly concurrency?: number;
|
|
@@ -16,4 +16,4 @@ export declare const transformerSharp: (options?: {
|
|
|
16
16
|
export declare const inspectorSharp: (options?: {
|
|
17
17
|
readonly maxPixels?: number;
|
|
18
18
|
}) => Layer.Layer<Inspector>;
|
|
19
|
-
//# sourceMappingURL=index.d.
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/sharp/index.ts"],"mappings":";;;qBAgDa,mBAAgB;;WAGhB;;WAEA;MAEV,MAAM,MAAM;;;;;;;qBAuCF,iBAAc;WACL;MACnB,MAAM,MAAM"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { t as Inspector } from "../describe-C6sWA_8p.mjs";
|
|
2
|
-
import { t as nonNegativeInteger } from "../limits-B4hHpyfE.mjs";
|
|
3
|
-
import { i as TransformFailed, n as OriginalTooLarge } from "../errors-BhEgXJ4m.mjs";
|
|
4
|
-
import { i as limitTransforms, n as Transformer } from "../services-7B4hsQM7.mjs";
|
|
5
1
|
import { Effect, Layer, Option } from "effect";
|
|
2
|
+
import { Inspector } from "../describe.js";
|
|
3
|
+
import { nonNegativeInteger } from "../limits.js";
|
|
4
|
+
import { OriginalTooLarge, TransformFailed } from "../server/errors.js";
|
|
5
|
+
import { Diagnostics } from "foldkit-surface";
|
|
6
|
+
import { imageFailed } from "../server/diagnostics.js";
|
|
7
|
+
import { Transformer, limitTransforms } from "../server/services.js";
|
|
6
8
|
import { availableParallelism } from "node:os";
|
|
7
9
|
import sharp from "sharp";
|
|
8
10
|
// src/sharp/index.ts
|
|
@@ -13,8 +15,8 @@ import sharp from "sharp";
|
|
|
13
15
|
* inflates to a huge picture is a 413, never an exhausted heap.
|
|
14
16
|
*/
|
|
15
17
|
const DEFAULT_MAX_PIXELS = 5e7;
|
|
16
|
-
/** What a failed sharp call ends in:
|
|
17
|
-
const failedAs = (
|
|
18
|
+
/** What a failed sharp call ends in: reported as `ImageFailed` at `step`, and answered as a 502. */
|
|
19
|
+
const failedAs = (step) => (cause) => Diagnostics.report(imageFailed(step, cause)).pipe(Effect.andThen(Effect.fail(new TransformFailed({}))));
|
|
18
20
|
/**
|
|
19
21
|
* An original's size from its header alone, refused above `maxPixels` before
|
|
20
22
|
* anything is decoded: a small file that inflates to a huge picture is a 413,
|
|
@@ -39,7 +41,7 @@ const measure = (input, maxPixels, failed) => Effect.gen(function* () {
|
|
|
39
41
|
});
|
|
40
42
|
const transformerSharp = (options = {}) => {
|
|
41
43
|
const maxPixels = nonNegativeInteger("maxPixels", options.maxPixels ?? DEFAULT_MAX_PIXELS);
|
|
42
|
-
const failed = failedAs("
|
|
44
|
+
const failed = failedAs("transform");
|
|
43
45
|
return Layer.succeed(Transformer, { transform: (original, variant) => Effect.gen(function* () {
|
|
44
46
|
const input = yield* Effect.promise(async () => new Uint8Array(await new Response(original.body).arrayBuffer()));
|
|
45
47
|
yield* measure(input, maxPixels, failed);
|
|
@@ -72,7 +74,7 @@ const THUMBNAIL_SIDE = 100;
|
|
|
72
74
|
*/
|
|
73
75
|
const inspectorSharp = (options = {}) => {
|
|
74
76
|
const maxPixels = nonNegativeInteger("maxPixels", options.maxPixels ?? DEFAULT_MAX_PIXELS);
|
|
75
|
-
const failed = failedAs("
|
|
77
|
+
const failed = failedAs("inspect");
|
|
76
78
|
return Layer.succeed(Inspector, { inspect: (bytes) => Effect.gen(function* () {
|
|
77
79
|
const { width, height } = yield* measure(bytes, maxPixels, failed);
|
|
78
80
|
const { stats, thumbnail } = yield* Effect.tryPromise({
|
|
@@ -99,4 +101,4 @@ const inspectorSharp = (options = {}) => {
|
|
|
99
101
|
};
|
|
100
102
|
export { inspectorSharp, transformerSharp };
|
|
101
103
|
|
|
102
|
-
//# sourceMappingURL=index.
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/sharp/index.ts"],"sourcesContent":["/**\n * The media server's `Transformer` on a Node server, through sharp. At most\n * `concurrency` transforms run at once (`limitTransforms`), and an original\n * is measured from its header before it is decoded, so a small file that\n * inflates to a huge picture is a 413, never an exhausted heap.\n */\nimport { availableParallelism } from 'node:os'\nimport { Effect, Layer, Option } from 'effect'\nimport sharp from 'sharp'\nimport { Diagnostics } from 'foldkit-surface'\nimport { imageFailed, type ImageFailed } from '../server/diagnostics.js'\nimport { OriginalTooLarge, TransformFailed } from '../server/errors.js'\nimport { Inspector } from '../describe.js'\nimport { nonNegativeInteger } from '../limits.js'\nimport { Transformer, limitTransforms } from '../server/services.js'\n\n// About 50 megapixels: a 9000×5500 photograph decodes; a decompression bomb does not.\nconst DEFAULT_MAX_PIXELS = 50_000_000\n\n/** What a failed sharp call ends in: reported as `ImageFailed` at `step`, and answered as a 502. */\nconst failedAs = (step: ImageFailed['step']) => (cause: unknown) =>\n Diagnostics.report(imageFailed(step, cause)).pipe(\n Effect.andThen(Effect.fail(new TransformFailed({}))),\n )\n\n/**\n * An original's size from its header alone, refused above `maxPixels` before\n * anything is decoded: a small file that inflates to a huge picture is a 413,\n * never an exhausted heap. The size is as shown, after its EXIF orientation:\n * a phone's portrait photograph is stored landscape and turned when drawn.\n */\nconst measure = (\n input: Uint8Array,\n maxPixels: number,\n failed: (cause: unknown) => Effect.Effect<never, TransformFailed>,\n) =>\n Effect.gen(function* () {\n const { autoOrient } = yield* Effect.tryPromise({\n try: () => sharp(input).metadata(),\n catch: cause => cause,\n }).pipe(Effect.catch(failed))\n const { width, height } = autoOrient\n if (width * height > maxPixels) {\n return yield* new OriginalTooLarge({ size: width * height, limit: maxPixels, unit: 'pixels' })\n }\n return { width, height }\n })\n\nexport const transformerSharp = (\n options: {\n /** Transforms run at once; by default the machine's cores. */\n readonly concurrency?: number\n /** The largest original decoded, in pixels. */\n readonly maxPixels?: number\n } = {},\n): Layer.Layer<Transformer> => {\n const maxPixels = nonNegativeInteger('maxPixels', options.maxPixels ?? DEFAULT_MAX_PIXELS)\n const failed = failedAs('transform')\n return Layer.succeed(Transformer, {\n transform: (original, variant) =>\n Effect.gen(function* () {\n const input = yield* Effect.promise(\n async () => new Uint8Array(await new Response(original.body).arrayBuffer()),\n )\n yield* measure(input, maxPixels, failed)\n const bytes = yield* Effect.tryPromise({\n try: () =>\n // Turned as shown, since a variant carries no orientation of its own.\n sharp(input, { limitInputPixels: maxPixels, failOn: 'error', autoOrient: true })\n .resize({ width: variant.width, withoutEnlargement: true })\n .toFormat(\n variant.format,\n Option.match(variant.quality, {\n onNone: () => ({}),\n onSome: quality => ({ quality }),\n }),\n )\n .toBuffer(),\n catch: cause => cause,\n }).pipe(Effect.catch(failed))\n return { bytes: new Uint8Array(bytes), contentType: `image/${variant.format}` }\n }).pipe(Effect.withSpan('Media.transform')),\n }).pipe(limitTransforms(options.concurrency ?? availableParallelism()))\n}\n\n// A ThumbHash is made from at most 100 pixels a side.\nconst THUMBNAIL_SIDE = 100\n\n/**\n * The `Inspector` on a Node server: an original's size and alpha from sharp,\n * and a thumbnail for its ThumbHash. Alpha is whether any pixel is not opaque\n * (`stats().isOpaque`), not whether the format has an alpha channel, so an\n * opaque PNG still gets a placeholder. An original above `maxPixels` is a 413.\n */\nexport const inspectorSharp = (\n options: { readonly maxPixels?: number } = {},\n): Layer.Layer<Inspector> => {\n const maxPixels = nonNegativeInteger('maxPixels', options.maxPixels ?? DEFAULT_MAX_PIXELS)\n const failed = failedAs('inspect')\n return Layer.succeed(Inspector, {\n inspect: bytes =>\n Effect.gen(function* () {\n const { width, height } = yield* measure(bytes, maxPixels, failed)\n const { stats, thumbnail } = yield* Effect.tryPromise({\n try: async () => ({\n stats: await sharp(bytes, { limitInputPixels: maxPixels }).stats(),\n thumbnail: await sharp(bytes, { limitInputPixels: maxPixels, autoOrient: true })\n .resize(THUMBNAIL_SIDE, THUMBNAIL_SIDE, { fit: 'inside' })\n .ensureAlpha()\n .raw()\n .toBuffer({ resolveWithObject: true }),\n }),\n catch: cause => cause,\n }).pipe(Effect.catch(failed))\n return {\n width,\n height,\n alpha: !stats.isOpaque,\n thumbnail: Option.some({\n width: thumbnail.info.width,\n height: thumbnail.info.height,\n rgba: new Uint8Array(thumbnail.data),\n }),\n }\n }).pipe(Effect.withSpan('Media.inspect')),\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiBA,MAAM,qBAAqB;;AAG3B,MAAM,YAAY,UAA+B,UAC/C,YAAY,OAAO,YAAY,MAAM,KAAK,CAAC,CAAC,CAAC,KAC3C,OAAO,QAAQ,OAAO,KAAK,IAAI,gBAAgB,CAAC,CAAC,CAAC,CAAC,CACrD;;;;;;;AAQF,MAAM,WACJ,OACA,WACA,WAEA,OAAO,IAAI,a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AC,CAAC,CAAC,KAAK,gBAAgB,QAAQ,eAAe,qBAAqB,CAAC,CAAC;AACxE;AAGA,MAAM,iBAAiB;;;;;;;AAQvB,MAAa,kBACX,UAA2C,CAAC,MACjB;CAC3B,MAAM,YAAY,mBAAmB,aAAa,QAAQ,aAAa,kBAAkB;CACzF,MAAM,SAAS,SAAS,SAAS;CACjC,OAAO,MAAM,QAAQ,WAAW,EAC9B,UAAS,UACP,OAAO,IAAI,aAAa;EACtB,MAAM,EAAE,OAAO,WAAW,OAAO,QAAQ,OAAO,WAAW,MAAM;EACjE,MAAM,EAAE,OAAO,cAAc,OAAO,OAAO,WAAW;GACpD,KAAK,aAAa;IAChB,OAAO,MAAM,MAAM,OAAO,EAAE,kBAAkB,UAAU,CAAC,CAAC,CAAC,MAAM;IACjE,WAAW,MAAM,MAAM,OAAO;KAAE,kBAAkB;KAAW,YAAY;IAAK,CAAC,CAAC,CAC7E,OAAO,gBAAgB,gBAAgB,EAAE,KAAK,SAAS,CAAC,CAAC,CACzD,YAAY,CAAC,CACb,IAAI,CAAC,CACL,SAAS,EAAE,mBAAmB,KAAK,CAAC;GACzC;GACA,QAAO,UAAS;EAClB,CAAC,CAAC,CAAC,KAAK,OAAO,MAAM,MAAM,CAAC;EAC5B,OAAO;GACL;GACA;GACA,OAAO,CAAC,MAAM;GACd,WAAW,OAAO,KAAK;IACrB,OAAO,UAAU,KAAK;IACtB,QAAQ,UAAU,KAAK;IACvB,MAAM,IAAI,WAAW,UAAU,IAAI;GACrC,CAAC;EACH;CACF,CAAC,CAAC,CAAC,KAAK,OAAO,SAAS,eAAe,CAAC,EAC5C,CAAC;AACH"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { i as Format, r as Encoding, t as Widths } from "../widths-upkgLdek.mjs";
|
|
2
|
-
import { n as Provider } from "../provider-CB0nqu0m.mjs";
|
|
3
1
|
import { Option } from "effect";
|
|
2
|
+
import { Encoding, Format } from "../format.js";
|
|
3
|
+
import { Provider } from "../provider.js";
|
|
4
|
+
import * as Widths$1 from "../widths.js";
|
|
4
5
|
export interface DetectOptions {
|
|
5
6
|
/** The widths offered: `Widths.standard` unless given. */
|
|
6
|
-
readonly widths?: Widths | undefined;
|
|
7
|
+
readonly widths?: Widths$1.Widths | undefined;
|
|
7
8
|
/**
|
|
8
9
|
* Formats offered as `source`s, most preferred first. None unless given: the
|
|
9
10
|
* `img` asks for `auto`, and the host picks the format from `Accept`.
|
|
@@ -22,4 +23,4 @@ export interface DetectOptions {
|
|
|
22
23
|
* naming AVIF over a JPEG would be a lie.
|
|
23
24
|
*/
|
|
24
25
|
export declare const detect: (url: string, options?: DetectOptions) => Option.Option<Provider>;
|
|
25
|
-
//# sourceMappingURL=index.d.
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["Widths"],"sources":["../../src/unpic/index.ts"],"mappings":";;;;iBAYiB;;WAEN,SAASA,SAAO;;;;;WAKhB,UAAU,cAAc;;WAExB,WAAW;;WAEX;;;;;;;;;qBAUE,SAAM,aAAe,UAAW,kBAAqB,OAAO,OAAO"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { r as standard } from "../widths-qp3jshiF.mjs";
|
|
2
1
|
import { Option } from "effect";
|
|
2
|
+
import * as Widths$1 from "../widths.js";
|
|
3
3
|
import { getProviderForUrl, transformUrl } from "unpic";
|
|
4
4
|
// src/unpic/index.ts
|
|
5
5
|
/**
|
|
@@ -28,7 +28,7 @@ const detect = (url, options = {}) => {
|
|
|
28
28
|
});
|
|
29
29
|
const takesFormat = write(url, 640, "avif") !== write(url, 640, "webp");
|
|
30
30
|
return Option.some({
|
|
31
|
-
widths: options.widths ?? standard,
|
|
31
|
+
widths: options.widths ?? Widths$1.standard,
|
|
32
32
|
sources: options.sources ?? [],
|
|
33
33
|
fallback: options.fallback ?? "auto",
|
|
34
34
|
url: (asset, { width, encoding }) => {
|
|
@@ -40,4 +40,4 @@ const detect = (url, options = {}) => {
|
|
|
40
40
|
};
|
|
41
41
|
export { detect };
|
|
42
42
|
|
|
43
|
-
//# sourceMappingURL=index.
|
|
43
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":["Widths"],"sources":["../../src/unpic/index.ts"],"sourcesContent":["/**\n * Images on a host that resizes them itself (Shopify, Cloudinary, imgix,\n * Contentful, and the rest unpic knows), as a `Provider`: each candidate is\n * the host's own URL for that width, so no server of ours runs and nothing is\n * stored. unpic, an optional peer, reads the host from the URL and writes it.\n */\nimport { Option } from 'effect'\nimport { getProviderForUrl, transformUrl, type ImageCdn } from 'unpic'\nimport type { Encoding, Format } from '../format.js'\nimport type { Provider } from '../provider.js'\nimport * as Widths from '../widths.js'\n\nexport interface DetectOptions {\n /** The widths offered: `Widths.standard` unless given. */\n readonly widths?: Widths.Widths | undefined\n /**\n * Formats offered as `source`s, most preferred first. None unless given: the\n * `img` asks for `auto`, and the host picks the format from `Accept`.\n */\n readonly sources?: ReadonlyArray<Format> | undefined\n /** What the `img` asks for: `auto` unless given. */\n readonly fallback?: Encoding | undefined\n /** The encoder's quality, where the host takes one. */\n readonly quality?: number | undefined\n}\n\n/**\n * The provider of `url`'s host, or none when unpic knows no host for it. The\n * provider answers only for an asset on a host of the same kind (its `key` is\n * the image's URL, written on its own host), and none for a format the host does not take: unpic writes no\n * format for some hosts (Shopify's chooses from `Accept`), and a `source`\n * naming AVIF over a JPEG would be a lie.\n */\nexport const detect = (url: string, options: DetectOptions = {}): Option.Option<Provider> => {\n const cdn: ImageCdn | false = getProviderForUrl(url)\n if (cdn === false) return Option.none()\n const quality = options.quality === undefined ? {} : { quality: options.quality }\n const write = (key: string, width: number, format: Format | undefined) =>\n transformUrl({\n url: key,\n provider: cdn,\n width,\n ...quality,\n ...(format === undefined ? {} : { format }),\n })\n // A host that takes a format writes two formats differently.\n const takesFormat = write(url, 640, 'avif') !== write(url, 640, 'webp')\n return Option.some({\n widths: options.widths ?? Widths.standard,\n sources: options.sources ?? [],\n fallback: options.fallback ?? 'auto',\n url: (asset, { width, encoding }) => {\n if (getProviderForUrl(asset.key) !== cdn) return Option.none()\n if (encoding !== 'auto' && !takesFormat) return Option.none()\n return Option.fromNullishOr(\n write(asset.key, width, encoding === 'auto' ? undefined : encoding),\n )\n },\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiCA,MAAa,UAAU,KAAa,UAAyB,CAAC,MAA+B;CAC3F,MAAM,MAAwB,kBAAkB,GAAG;CACnD,IAAI,QAAQ,OAAO,OAAO,OAAO,KAAK;CACtC,MAAM,UAAU,QAAQ,YAAY,KAAA,IAAY,CAAC,IAAI,EAAE,SAAS,QAAQ,QAAQ;CAChF,MAAM,SAAS,KAAa,OAAe,WACzC,aAAa;EACX,KAAK;EACL,UAAU;EACV;EACA,GAAG;EACH,GAAI,WAAW,KAAA,IAAY,CAAC,IAAI,EAAE,OAAO;CAC3C,CAAC;CAEH,MAAM,cAAc,MAAM,KAAK,KAAK,MAAM,MAAM,MAAM,KAAK,KAAK,MAAM;CACtE,OAAO,OAAO,KAAK;EACjB,QAAQ,QAAQ,UAAUA,SAAO;EACjC,SAAS,QAAQ,WAAW,CAAC;EAC7B,UAAU,QAAQ,YAAY;EAC9B,MAAM,OAAO,EAAE,OAAO,eAAe;GACnC,IAAI,kBAAkB,MAAM,GAAG,MAAM,KAAK,OAAO,OAAO,KAAK;GAC7D,IAAI,aAAa,UAAU,CAAC,aAAa,OAAO,OAAO,KAAK;GAC5D,OAAO,OAAO,cACZ,MAAM,MAAM,KAAK,OAAO,aAAa,SAAS,KAAA,IAAY,QAAQ,CACpE;EACF;CACF,CAAC;AACH"}
|