@softistx/alxia-media 0.0.0-stage → 0.12.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.
Files changed (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +373 -2
  3. package/dist/body.d.ts +24 -0
  4. package/dist/body.d.ts.map +1 -0
  5. package/dist/bucket.d.ts +21 -0
  6. package/dist/bucket.d.ts.map +1 -0
  7. package/dist/checks.d.ts +11 -0
  8. package/dist/checks.d.ts.map +1 -0
  9. package/dist/chunks/index-8s779ve6.js +106 -0
  10. package/dist/chunks/index-8s779ve6.js.map +10 -0
  11. package/dist/headers.d.ts +19 -0
  12. package/dist/headers.d.ts.map +1 -0
  13. package/dist/image/index.d.ts +2 -0
  14. package/dist/image/index.d.ts.map +1 -0
  15. package/dist/image/index.js +199 -0
  16. package/dist/image/index.js.map +11 -0
  17. package/dist/image/peer.d.ts +14 -0
  18. package/dist/image/peer.d.ts.map +1 -0
  19. package/dist/image/rendition.d.ts +109 -0
  20. package/dist/image/rendition.d.ts.map +1 -0
  21. package/dist/index.d.ts +9 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +825 -0
  24. package/dist/index.js.map +22 -0
  25. package/dist/key.d.ts +40 -0
  26. package/dist/key.d.ts.map +1 -0
  27. package/dist/kind-callbacks.d.ts +28 -0
  28. package/dist/kind-callbacks.d.ts.map +1 -0
  29. package/dist/kind-check.d.ts +20 -0
  30. package/dist/kind-check.d.ts.map +1 -0
  31. package/dist/kind.d.ts +173 -0
  32. package/dist/kind.d.ts.map +1 -0
  33. package/dist/range.d.ts +17 -0
  34. package/dist/range.d.ts.map +1 -0
  35. package/dist/read.d.ts +11 -0
  36. package/dist/read.d.ts.map +1 -0
  37. package/dist/refusal.d.ts +90 -0
  38. package/dist/refusal.d.ts.map +1 -0
  39. package/dist/rendition.d.ts +94 -0
  40. package/dist/rendition.d.ts.map +1 -0
  41. package/dist/routes.d.ts +62 -0
  42. package/dist/routes.d.ts.map +1 -0
  43. package/dist/sniff.d.ts +16 -0
  44. package/dist/sniff.d.ts.map +1 -0
  45. package/dist/write.d.ts +42 -0
  46. package/dist/write.d.ts.map +1 -0
  47. package/docs/README.md +11 -0
  48. package/docs/guide.md +902 -0
  49. package/docs/roadmap.md +73 -0
  50. package/docs/troubleshooting.md +563 -0
  51. package/package.json +65 -5
@@ -0,0 +1,90 @@
1
+ /**
2
+ * The typed refusals of the media routes: one code per way a request is
3
+ * turned away, each with its status. A read refused by `authorizeRead` is
4
+ * `not_found`, as an object that is not there is: the two are not told apart.
5
+ * A guard that sees no viewer answers `{ code: 'unauthenticated' }` (a 401)
6
+ * instead of `false`.
7
+ */
8
+ import { type AnyReply, type BaseContext } from '@softistx/alxia';
9
+ /** Why a media request was refused. */
10
+ export type MediaRefusalCode = 'unauthenticated' | 'not_found' | 'forbidden' | 'unsupported_type' | 'too_large' | 'empty_body' | 'size_mismatch' | 'interrupted' | 'stalled' | 'range_not_satisfiable' | 'throttled' | 'unprocessable' | 'invalid_crop' | 'unavailable';
11
+ /** The status each code answers with. */
12
+ export declare const MEDIA_REFUSAL_STATUS: {
13
+ readonly unauthenticated: 401;
14
+ readonly not_found: 404;
15
+ readonly forbidden: 403;
16
+ readonly unsupported_type: 415;
17
+ readonly too_large: 413;
18
+ readonly empty_body: 400;
19
+ readonly size_mismatch: 400;
20
+ readonly interrupted: 400;
21
+ readonly stalled: 408;
22
+ readonly range_not_satisfiable: 416;
23
+ readonly throttled: 429;
24
+ readonly unprocessable: 422;
25
+ readonly invalid_crop: 400;
26
+ readonly unavailable: 503;
27
+ };
28
+ export type MediaRefusalStatus = (typeof MEDIA_REFUSAL_STATUS)[MediaRefusalCode];
29
+ /** What a refusal may carry beside its code. */
30
+ export interface MediaRefusalDetails {
31
+ /** `too_large`: the most bytes the kind takes. */
32
+ readonly limit?: number;
33
+ /** `throttled`: seconds to wait. */
34
+ readonly retryAfter?: number;
35
+ /** `range_not_satisfiable`: the whole object's size, when known. */
36
+ readonly size?: number;
37
+ /** `unsupported_type`: the types the kind accepts. */
38
+ readonly accept?: readonly string[];
39
+ /** `unprocessable`: what about the file its renditions could not be made from (`decode`, `dimensions`, `pixels`). */
40
+ readonly reason?: string;
41
+ /** `unprocessable` with the reason `dimensions`: the smallest side, in pixels, a picture may have. */
42
+ readonly min?: number;
43
+ /**
44
+ * Parameters the app words the refusal from, answered beside the code
45
+ * (`{ error: 'in_use', count: 3 }`). A name the answer already uses
46
+ * (`error`, `type`, `status`, `title`, `detail`, `instance`) is dropped.
47
+ */
48
+ readonly params?: Readonly<Record<string, unknown>>;
49
+ /** A refusal of the app's own code (see `MediaDenial`): its status. */
50
+ readonly status?: number;
51
+ }
52
+ /**
53
+ * What a kind's `authorize*` callbacks may answer instead of `false`: a
54
+ * refusal of the app's own, with its code and its status (`{ code: 'in_use',
55
+ * status: 409 }`), answered as any other refusal is.
56
+ */
57
+ export interface MediaDenial {
58
+ /** Snake case: lower-case words and digits joined by underscores. */
59
+ readonly code: string;
60
+ /**
61
+ * An HTTP error status, 400 to 599. Left out, it is the status of a code
62
+ * the package has (`{ code: 'unauthenticated' }` is a 401); a code of
63
+ * your own needs it.
64
+ */
65
+ readonly status?: number;
66
+ readonly params?: Readonly<Record<string, unknown>>;
67
+ }
68
+ /** What an `authorize*` callback answers: allowed or not, or a refusal of its own. */
69
+ export type MediaVerdict = boolean | MediaDenial;
70
+ /**
71
+ * The refusal an `authorize*` answer comes to: none for `true`, `otherwise`
72
+ * for `false`, the denial's own for a `MediaDenial`. A denial with a bad code
73
+ * or status is the app's bug (a 500), not a refusal.
74
+ */
75
+ export declare function refusalOfVerdict(verdict: unknown, otherwise: MediaRefusal): MediaRefusal | undefined;
76
+ /** A request turned away. Thrown inside the routes, answered by them. */
77
+ export declare class MediaRefusal extends Error {
78
+ readonly name = "MediaRefusal";
79
+ readonly code: MediaRefusalCode | (string & {});
80
+ readonly status: number;
81
+ readonly details: MediaRefusalDetails;
82
+ constructor(code: MediaRefusalCode | (string & {}), details?: MediaRefusalDetails, options?: {
83
+ cause?: unknown;
84
+ });
85
+ }
86
+ /** Whether `error` is a `MediaRefusal`, whichever copy of the package threw it. */
87
+ export declare function isMediaRefusal(error: unknown): error is MediaRefusal;
88
+ /** The reply a refusal is answered with, unless the `refusal` option answers it. */
89
+ export declare function refusalReply(ctx: BaseContext, refusal: MediaRefusal): AnyReply;
90
+ //# sourceMappingURL=refusal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"refusal.d.ts","sourceRoot":"","sources":["../src/refusal.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,WAAW,EAMhB,MAAM,iBAAiB,CAAC;AAEzB,uCAAuC;AACvC,MAAM,MAAM,gBAAgB,GACzB,iBAAiB,GACjB,WAAW,GACX,WAAW,GACX,kBAAkB,GAClB,WAAW,GACX,YAAY,GACZ,eAAe,GACf,aAAa,GACb,SAAS,GACT,uBAAuB,GACvB,WAAW,GACX,eAAe,GACf,cAAc,GACd,aAAa,CAAC;AAEjB,yCAAyC;AACzC,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;;CAeoB,CAAC;AAEtD,MAAM,MAAM,kBAAkB,GAC7B,CAAC,OAAO,oBAAoB,CAAC,CAAC,gBAAgB,CAAC,CAAC;AAEjD,gDAAgD;AAChD,MAAM,WAAW,mBAAmB;IACnC,kDAAkD;IAClD,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,oCAAoC;IACpC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,oEAAoE;IACpE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,sDAAsD;IACtD,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,qHAAqH;IACrH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,sGAAsG;IACtG,QAAQ,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,EAChB,OAAO,EAAE,YAAY,GACnB,QAAQ,CAkCV"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * Renditions made at upload: once a file of a kind with `renditions` is in
3
+ * the bucket, its bytes are read back and handed to `make`, and what it
4
+ * makes is stored under `<kind>/<key>/<size>` before the upload answers.
5
+ * The routes stay read-only on `GET`: a size is served as it was stored.
6
+ */
7
+ import type { MediaBucket } from './bucket';
8
+ import type { MediaKey, MediaPlace } from './key';
9
+ import type { MediaUpload } from './write';
10
+ /** A file just stored, as `make` is given it. */
11
+ export interface MediaFile {
12
+ readonly key: MediaKey;
13
+ /** The type it was stored as, one of the kind's `accept`. */
14
+ readonly contentType: string;
15
+ readonly bytes: Uint8Array;
16
+ /**
17
+ * The upload's request, for what the rendition depends on (a crop read
18
+ * from its query, who sends it). Its body has been read.
19
+ */
20
+ readonly request: Request;
21
+ }
22
+ /** One rendition `make` gives: a size the kind lists, in a type `types` lists. */
23
+ export interface MediaRendition {
24
+ readonly size: string;
25
+ readonly contentType: string;
26
+ readonly bytes: Uint8Array;
27
+ /** The picture's width in pixels, when the rendition is one: set by `@softistx/alxia-media/image`, given to the `uploaded` hook. */
28
+ readonly width?: number;
29
+ /** The picture's height in pixels, like `width`. */
30
+ readonly height?: number;
31
+ }
32
+ /** One stored rendition as the `uploaded` hook sees it: no bytes, its size, type and (for a picture) dimensions. */
33
+ export interface MediaStoredRendition {
34
+ readonly size: string;
35
+ readonly contentType: string;
36
+ readonly width?: number;
37
+ readonly height?: number;
38
+ }
39
+ /** What a kind makes of each upload. `@softistx/alxia-media/image` gives one for pictures. */
40
+ export interface MediaRenditions {
41
+ /**
42
+ * The content types `make` gives, exact. A size is served as the type it
43
+ * was stored as when it is one of these or of `accept`.
44
+ */
45
+ readonly types: readonly string[];
46
+ /**
47
+ * A file extension (`'webp'`, no dot) that ends every rendition key:
48
+ * `<kind>/<key>/<size>.<extension>` instead of `<kind>/<key>/<size>`, on
49
+ * write, read and delete. For a bucket that already stores renditions that
50
+ * way. The original's key is unchanged, and so is the URL (`/<size>`).
51
+ * Not set (the default): no extension. Changing it moves every key, so
52
+ * the renditions already stored under the other form answer 404.
53
+ */
54
+ readonly keyExtension?: string;
55
+ /**
56
+ * Makes the renditions of a file just stored. A `MediaRefusal` it throws
57
+ * answers the upload (`unprocessable`, 422, for a picture that cannot be
58
+ * read; an `unsupported_type` is given the kind's `accept`); any other
59
+ * error is the app's (a 500). Either way, the file and whatever was
60
+ * stored of its renditions are removed.
61
+ *
62
+ * It need not make every size the kind lists: a size it leaves out is
63
+ * not stored, and answers 404 for that file, as a size added after the
64
+ * upload does.
65
+ */
66
+ readonly make: (file: MediaFile) => Promise<readonly MediaRendition[]>;
67
+ }
68
+ /** What storing the renditions of one upload needs. */
69
+ interface Rendering {
70
+ readonly name: string;
71
+ readonly accept: readonly string[];
72
+ readonly sizes: readonly string[];
73
+ readonly renditions: MediaRenditions;
74
+ }
75
+ /**
76
+ * Makes and stores the renditions of `upload`, and gives the sizes it
77
+ * stored. The file is read back from the bucket, unless it was never stored
78
+ * (`held`, a kind with `keepOriginal: false`). On any failure, removes the
79
+ * file and every rendition key it started to write, then throws again.
80
+ */
81
+ export declare function storeRenditions(bucket: MediaBucket, kind: Rendering, upload: MediaUpload, options: {
82
+ readonly timeoutMs: number;
83
+ readonly place: MediaPlace;
84
+ readonly request: Request;
85
+ readonly held?: Uint8Array;
86
+ }): Promise<{
87
+ readonly sizes: string[];
88
+ readonly renditions: MediaStoredRendition[];
89
+ readonly objects: string[];
90
+ }>;
91
+ /** Removes `keys`, each failure ignored: this is the cleanup of a failure already being thrown. */
92
+ export declare function removeAll(bucket: MediaBucket, keys: readonly string[], timeoutMs: number): Promise<void>;
93
+ export {};
94
+ //# sourceMappingURL=rendition.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rendition.d.ts","sourceRoot":"","sources":["../src/rendition.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,M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ACvE;AAED,uDAAuD;AACvD,UAAU,SAAS;IAClB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,QAAQ,CAAC,UAAU,EAAE,eAAe,CAAC;CACrC;AA8CD;;;;;GAKG;AACH,wBAAsB,eAAe,CACpC,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,WAAW,EACnB,OAAO,EAAE;IACR,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAC3B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,IAAI,CAAC,EAAE,UAAU,CAAC;CAC3B,GACC,OAAO,CAAC;IACV,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,oBAAoB,EAAE,CAAC;IAC5C,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC,CAkCD;AAED,mGAAmG;AACnG,wBAAsB,SAAS,CAC9B,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,SAAS,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAMf"}
@@ -0,0 +1,62 @@
1
+ /** `mediaRoutes`: the routes that serve and accept the files of some kinds. */
2
+ import { type AnyReply, type BaseContext, type Empty, type RoutePath } from '@softistx/alxia';
3
+ import { type MediaBucket } from './bucket';
4
+ import type { MediaContext, MediaKind } from './kind';
5
+ import { type MediaRefusal } from './refusal';
6
+ import { type MediaUploaded } from './write';
7
+ /** What `mediaRoutes` takes beside the kinds. */
8
+ export interface MediaRoutesOptions<Requires extends object = Empty> {
9
+ /** The private bucket the files live in. */
10
+ readonly bucket: MediaBucket;
11
+ /** Where the routes are mounted: `/media` by default. */
12
+ readonly prefix?: RoutePath;
13
+ /** How long the bucket may take to start answering a read, in ms: 10 000 by default. */
14
+ readonly readTimeoutMs?: number;
15
+ /**
16
+ * The idle deadline of an upload, in ms: it starts over after each chunk,
17
+ * so a slow upload that keeps moving is never cut. 60 000 by default.
18
+ */
19
+ readonly writeTimeoutMs?: number;
20
+ /**
21
+ * Answers a refusal in the app's own format. Return `undefined` to leave
22
+ * it to the default (`{ error: code }`, or an RFC 9457 problem when the
23
+ * app is configured for them).
24
+ */
25
+ readonly refusal?: (refusal: MediaRefusal, ctx: BaseContext) => AnyReply | undefined;
26
+ /**
27
+ * Folders every object of the request lives under, from the request
28
+ * (`tenants/<id>`): the bucket key is `<prefix>/<kind>/<key>[/<size>]`
29
+ * on writes, reads and deletes alike, so a request can only reach the
30
+ * objects of its own prefix. Called once per request, before the kind's
31
+ * `authorize*`; it may throw a `MediaRefusal` (a `forbidden` when the
32
+ * request has no tenant). `undefined` or `''` means no prefix. Segments
33
+ * are letters, digits and `. _ = -` joined by `/`, else a `TypeError`.
34
+ */
35
+ readonly keyPrefix?: (ctx: MediaContext<Requires>, kind: string) => string | undefined | Promise<string | undefined>;
36
+ /**
37
+ * Shapes the body of a successful upload (default: `{ key, contentType,
38
+ * size }`). Called once the file and its renditions are stored; what it
39
+ * returns is the 200's JSON body. If it throws, what was stored is
40
+ * removed and the error is answered as any other.
41
+ */
42
+ readonly uploaded?: (uploaded: MediaUploaded, ctx: MediaContext<Requires>) => unknown;
43
+ }
44
+ /**
45
+ * The routes of some kinds, as a plugin:
46
+ *
47
+ * - `PUT <prefix>/<kind>` the raw body in, `{ key, contentType, size }` out (or what `uploaded` makes of it);
48
+ * - `GET <prefix>/<kind>/<key>` the file, with Range, ETag and 304;
49
+ * - `GET <prefix>/<kind>/<key>/<size>` a named rendition, for a kind that lists `sizes`;
50
+ * - `DELETE <prefix>/<kind>/<key>` for a kind that declares `authorizeDelete`.
51
+ *
52
+ * Mount it after the middlewares that authenticate: the kinds' callbacks read
53
+ * what they add. Keys are returned, never URLs.
54
+ *
55
+ * ```ts
56
+ * const app = alxia()
57
+ * .plugin(auth)
58
+ * .plugin(mediaRoutes([avatars], { bucket }));
59
+ * ```
60
+ */
61
+ export declare function mediaRoutes<Requires extends object = Empty>(kinds: readonly MediaKind<Requires>[], options: MediaRoutesOptions<Requires>): import("@softistx/alxia").Alxia<Requires, ""> & import("@softistx/alxia").Requiring<Requires>;
62
+ //# sourceMappingURL=routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAE/E,OAAO,EACN,KAAK,QAAQ,EACb,KAAK,WAAW,EAEhB,KAAK,KAAK,EAEV,KAAK,SAAS,EACd,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,KAAK,WAAW,EAAoB,MAAM,UAAU,CAAC;AAE9D,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEtD,OAAO,EAAE,KAAK,YAAY,EAAgB,MAAM,WAAW,CAAC;AAC5D,OAAO,EAAE,KAAK,aAAa,EAAkB,MAAM,SAAS,CAAC;AAE7D,iDAAiD;AACjD,MAAM,WAAW,kBAAkB,CAAC,QAAQ,SAAS,MAAM,GAAG,KAAK;IAClE,4CAA4C;IAC5C,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,yDAAyD;IACzD,QAAQ,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC;IAC5B,wFAAwF;IACxF,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;;OAGG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,CAClB,OAAO,EAAE,YAAY,EACrB,GAAG,EAAE,WAAW,KACZ,QAAQ,GAAG,SAAS,CAAC;IAC1B;;;;;;;;OAQG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,CACpB,GAAG,EAAE,YAAY,CAAC,QAAQ,CAAC,EAC3B,IAAI,EAAE,MAAM,KACR,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACtD;;;;;OAKG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,CACnB,QAAQ,EAAE,aAAa,EACvB,GAAG,EAAE,YAAY,CAAC,QAAQ,CAAC,KACvB,OAAO,CAAC;CACb;AASD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,WAAW,CAAC,QAAQ,SAAS,MAAM,GAAG,KAAK,EAC1D,KAAK,EAAE,SAAS,SAAS,CAAC,QAAQ,CAAC,EAAE,EACrC,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAAC,iGAiCrC"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Magic-byte sniffing: the first bytes of an upload must be those of the
3
+ * type it declares. A client that sends `image/png` over an HTML page is
4
+ * refused 415 before anything is stored.
5
+ */
6
+ /** How many leading bytes `sniffs` needs. */
7
+ export declare const SNIFF_BYTES = 16;
8
+ type Signature = (head: Uint8Array) => boolean;
9
+ /** The signatures this package knows, by content type. */
10
+ export declare const MEDIA_SIGNATURES: Readonly<Record<string, Signature>>;
11
+ /** Whether this package can sniff `type`. */
12
+ export declare function canSniff(type: string): boolean;
13
+ /** Whether the head of a body is a `type` file. A type nobody can sniff is not. */
14
+ export declare function sniffs(type: string, head: Uint8Array): boolean;
15
+ export {};
16
+ //# sourceMappingURL=sniff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sniff.d.ts","sourceRoot":"","sources":["../src/sniff.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,6CAA6C;AAC7C,eAAO,MAAM,WAAW,KAAK,CAAC;AAE9B,KAAK,SAAS,GAAG,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,CAAC;AAO/C,0DAA0D;AAC1D,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAoBhE,CAAC;AAEF,6CAA6C;AAC7C,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE9C;AAED,mFAAmF;AACnF,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAK9D"}
@@ -0,0 +1,42 @@
1
+ /** `PUT` and `DELETE`: a body streamed into the bucket under a fresh key. */
2
+ import type { AnyReply } from '@softistx/alxia';
3
+ import type { MediaBucket } from './bucket';
4
+ import { type MediaPlace } from './key';
5
+ import { type MediaContext, type MediaKind } from './kind';
6
+ import { type MediaStoredRendition } from './rendition';
7
+ /** What a successful upload answers: the key to keep, never a URL. */
8
+ export interface MediaUpload {
9
+ readonly key: string;
10
+ readonly contentType: string;
11
+ readonly size: number;
12
+ }
13
+ /** What a write needs beside the kind: the bucket, its deadline, and where this request's objects live. */
14
+ export interface Writing {
15
+ readonly bucket: MediaBucket;
16
+ readonly timeoutMs: number;
17
+ readonly place: MediaPlace;
18
+ }
19
+ /** An upload as the response hook is given it: what the `PUT` answers by default, and the sizes stored. */
20
+ export interface MediaUploaded extends MediaUpload {
21
+ /** The kind's name. */
22
+ readonly kind: string;
23
+ /** The sizes stored for this file, in the order they were made (none for a kind without `renditions`). */
24
+ readonly sizes: readonly string[];
25
+ /**
26
+ * The same renditions with their type and, for a picture made by
27
+ * `@softistx/alxia-media/image` (or a `make` that sets them), `width` and `height`
28
+ * in pixels, in the order of `sizes`. Empty for a kind without `renditions`.
29
+ */
30
+ readonly renditions: readonly MediaStoredRendition[];
31
+ }
32
+ /**
33
+ * Stores the request's body as a new file of `kind`, then, for a kind with
34
+ * `renditions`, what it makes of it (see `storeRenditions`). For a kind with
35
+ * `keepOriginal: false` the body is never stored, only what is made of it.
36
+ * `answer` shapes the response from the finished upload; if it throws, what
37
+ * was stored is removed.
38
+ */
39
+ export declare function upload(ctx: MediaContext<object>, kind: MediaKind<object>, writing: Writing, answer?: (uploaded: MediaUploaded) => unknown): Promise<unknown>;
40
+ /** Removes a file and its sizes. Deleting what is not there is a 204 too. */
41
+ export declare function remove(ctx: MediaContext<object>, kind: MediaKind<object>, key: string | undefined, { bucket, timeoutMs, place }: Writing): Promise<AnyReply>;
42
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../src/write.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAE7E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAGhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAE5C,OAAO,EAAc,KAAK,UAAU,EAAe,MAAM,OAAO,CAAC;AACjE,OAAO,EAAiB,KAAK,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,QAAQ,CAAC;AAG1E,OAAO,EACN,KAAK,oBAAoB,EAGzB,MAAM,aAAa,CAAC;AAGrB,sEAAsE;AACtE,MAAM,WAAW,WAAW;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,2GAA2G;AAC3G,MAAM,WAAW,OAAO;IACvB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC3B;AAED,2GAA2G;AAC3G,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,uBAAuB;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0GAA0G;IAC1G,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC;;;;OAIG;IACH,QAAQ,CAAC,UAAU,EAAE,SAAS,oBAAoB,EAAE,CAAC;CACrD;AA2BD;;;;;;GAMG;AACH,wBAAsB,MAAM,CAC3B,GAAG,EAAE,YAAY,CAAC,MAAM,CAAC,EACzB,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,EACvB,OAAO,EAAE,OAAO,EAChB,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,aAAa,KAAK,OAAO,GAC3C,OAAO,CAAC,OAAO,CAAC,CAwClB;AAyFD,6EAA6E;AAC7E,wBAAsB,MAAM,CAC3B,GAAG,EAAE,YAAY,CAAC,MAAM,CAAC,EACzB,IAAI,EAAE,SAAS,CAAC,MAAM,CAAC,EACvB,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,OAAO,GACnC,OAAO,CAAC,QAAQ,CAAC,CAYnB"}
package/docs/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @softistx/alxia-media documentation
2
+
3
+ The [package README](../README.md) is the short version. This folder is
4
+ the long one: how to declare kinds and mount the routes, what each request
5
+ answers, the errors you may meet, and what is coming.
6
+
7
+ | Page | Read it when |
8
+ | --- | --- |
9
+ | [Guide](guide.md) | adding media to an app, choosing who may read and write, image sizes with `@softistx/alxia-media/image`, uploading and serving from a browser, running behind a proxy, or testing it |
10
+ | [Troubleshooting](troubleshooting.md) | something went wrong and you have the status or the message |
11
+ | [Roadmap](roadmap.md) | wondering what is coming, and what is not planned |