@kumwe/studio-media 0.1.0-alpha.2
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/media-library.d.ts +32 -0
- package/dist/media-library.d.ts.map +1 -0
- package/dist/media-library.js +93 -0
- package/dist/media-library.js.map +1 -0
- package/dist/upload-controller.d.ts +34 -0
- package/dist/upload-controller.d.ts.map +1 -0
- package/dist/upload-controller.js +196 -0
- package/dist/upload-controller.js.map +1 -0
- package/dist/validate-media-reference.d.ts +8 -0
- package/dist/validate-media-reference.d.ts.map +1 -0
- package/dist/validate-media-reference.js +24 -0
- package/dist/validate-media-reference.js.map +1 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kumwe Studio contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# `@kumwe/studio-media`
|
|
2
|
+
|
|
3
|
+
Status: pre-Gate-A foundation alpha. It supplies orchestration primitives, not a production media backend.
|
|
4
|
+
|
|
5
|
+
Host-neutral media orchestration. Hosts provide authentication, storage, upload, processing, and
|
|
6
|
+
authorization through this package's runtime `MediaProvider`; the package supplies cancellation-safe
|
|
7
|
+
browsing state and deterministic responsive-rendition selection.
|
|
8
|
+
|
|
9
|
+
Studio artifacts store the protocol's usage-specific `MediaReference`, while `MediaAsset` is the
|
|
10
|
+
host's library projection. URLs are delivery details returned by the receiving host and belong in
|
|
11
|
+
neither persisted contract.
|
|
12
|
+
|
|
13
|
+
## Upload orchestration
|
|
14
|
+
|
|
15
|
+
`MediaUploadController` drives the canonical media-upload-session state machine —
|
|
16
|
+
`requested → authorized → transferring → verifying → complete`, with `failed` and `cancelled`
|
|
17
|
+
terminals — over a host-implemented `MediaUploadTransport` (`authorize`, `transfer`, `finalize`,
|
|
18
|
+
and best-effort `abort`). Chunking follows the host's authorized plan, and every emitted snapshot
|
|
19
|
+
conforms to the protocol's `media-upload-session` schema. A file larger than the plan's
|
|
20
|
+
`maximumBytes` fails before any transfer with a `studio.media/upload-too-large` diagnostic;
|
|
21
|
+
transport rejections surface as a generic `studio.media/upload-failed` diagnostic and never leak
|
|
22
|
+
raw error text. `cancel()` aborts in-flight work and requests best-effort server-side
|
|
23
|
+
cancellation, and `retry()` restarts a failed session from authorization under a fresh session id.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MediaLibrary, MEDIA_PROVIDER_FAILURE, selectBestRendition, type MediaLibraryListener, type MediaLibraryState, type MediaLibraryStatus, type MediaProvider, type MediaUploadRequest, } from './media-library.js';
|
|
2
|
+
export { MediaUploadController, MEDIA_UPLOAD_FAILURE, MEDIA_UPLOAD_TOO_LARGE, type MediaUploadChunk, type MediaUploadControllerOptions, type MediaUploadListener, type MediaUploadTransport, } from './upload-controller.js';
|
|
3
|
+
export { validateMediaReference } from './validate-media-reference.js';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,sBAAsB,EACtB,mBAAmB,EACnB,KAAK,oBAAoB,EACzB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,EACvB,KAAK,aAAa,EAClB,KAAK,kBAAkB,GACxB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,KAAK,gBAAgB,EACrB,KAAK,4BAA4B,EACjC,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,GAC1B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { MediaLibrary, MEDIA_PROVIDER_FAILURE, selectBestRendition, } from './media-library.js';
|
|
2
|
+
export { MediaUploadController, MEDIA_UPLOAD_FAILURE, MEDIA_UPLOAD_TOO_LARGE, } from './upload-controller.js';
|
|
3
|
+
export { validateMediaReference } from './validate-media-reference.js';
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,EACZ,sBAAsB,EACtB,mBAAmB,GAMpB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,GAKvB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { MediaAsset, MediaPage, MediaQuery, MediaRendition, MessageReference } from '@kumwe/studio-protocol';
|
|
2
|
+
export declare const MEDIA_PROVIDER_FAILURE: Readonly<MessageReference>;
|
|
3
|
+
export interface MediaUploadRequest {
|
|
4
|
+
alt?: string;
|
|
5
|
+
file: Blob;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface MediaProvider {
|
|
9
|
+
get(assetId: string, signal?: AbortSignal): Promise<MediaAsset | null>;
|
|
10
|
+
list(query: MediaQuery, signal?: AbortSignal): Promise<MediaPage>;
|
|
11
|
+
upload(request: MediaUploadRequest): Promise<MediaAsset>;
|
|
12
|
+
}
|
|
13
|
+
export type MediaLibraryStatus = 'error' | 'idle' | 'loading' | 'ready';
|
|
14
|
+
export interface MediaLibraryState {
|
|
15
|
+
assets: MediaAsset[];
|
|
16
|
+
error?: MessageReference;
|
|
17
|
+
nextCursor?: string;
|
|
18
|
+
query?: MediaQuery;
|
|
19
|
+
status: MediaLibraryStatus;
|
|
20
|
+
}
|
|
21
|
+
export type MediaLibraryListener = (state: MediaLibraryState) => void;
|
|
22
|
+
export declare class MediaLibrary {
|
|
23
|
+
#private;
|
|
24
|
+
constructor(provider: MediaProvider);
|
|
25
|
+
get state(): MediaLibraryState;
|
|
26
|
+
dispose(): void;
|
|
27
|
+
loadNext(): Promise<MediaLibraryState>;
|
|
28
|
+
search(query: MediaQuery): Promise<MediaLibraryState>;
|
|
29
|
+
subscribe(listener: MediaLibraryListener): () => void;
|
|
30
|
+
}
|
|
31
|
+
export declare function selectBestRendition(asset: MediaAsset, targetWidth: number): MediaRendition | undefined;
|
|
32
|
+
//# sourceMappingURL=media-library.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-library.d.ts","sourceRoot":"","sources":["../src/media-library.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,SAAS,EACT,UAAU,EACV,cAAc,EACd,gBAAgB,EACjB,MAAM,wBAAwB,CAAC;AAEhC,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,gBAAgB,CAG5D,CAAC;AAEH,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC;IACvE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;IAClE,MAAM,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAC1D;AAED,MAAM,MAAM,kBAAkB,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC;AAExE,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,MAAM,EAAE,kBAAkB,CAAC;CAC5B;AAED,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,IAAI,CAAC;AAEtE,qBAAa,YAAY;;gBAMJ,QAAQ,EAAE,aAAa;IAI1C,IAAW,KAAK,IAAI,iBAAiB,CAEpC;IAEM,OAAO,IAAI,IAAI;IAKT,QAAQ,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAUtC,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAa3D,SAAS,CAAC,QAAQ,EAAE,oBAAoB,GAAG,MAAM,IAAI;CA+C7D;AAED,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,UAAU,EACjB,WAAW,EAAE,MAAM,GAClB,cAAc,GAAG,SAAS,CAM5B"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const MEDIA_PROVIDER_FAILURE = Object.freeze({
|
|
2
|
+
defaultMessage: 'The media library could not be loaded.',
|
|
3
|
+
key: 'studio.media/provider-failed',
|
|
4
|
+
});
|
|
5
|
+
export class MediaLibrary {
|
|
6
|
+
#listeners = new Set();
|
|
7
|
+
#provider;
|
|
8
|
+
#abortController;
|
|
9
|
+
#state = { assets: [], status: 'idle' };
|
|
10
|
+
constructor(provider) {
|
|
11
|
+
this.#provider = provider;
|
|
12
|
+
}
|
|
13
|
+
get state() {
|
|
14
|
+
return structuredClone(this.#state);
|
|
15
|
+
}
|
|
16
|
+
dispose() {
|
|
17
|
+
this.#abortController?.abort();
|
|
18
|
+
this.#listeners.clear();
|
|
19
|
+
}
|
|
20
|
+
async loadNext() {
|
|
21
|
+
if (this.#state.query === undefined || this.#state.nextCursor === undefined) {
|
|
22
|
+
return this.state;
|
|
23
|
+
}
|
|
24
|
+
return this.#load({ ...this.#state.query, cursor: this.#state.nextCursor }, [
|
|
25
|
+
...this.#state.assets,
|
|
26
|
+
]);
|
|
27
|
+
}
|
|
28
|
+
async search(query) {
|
|
29
|
+
const normalized = {
|
|
30
|
+
limit: Math.max(1, Math.min(100, Math.trunc(query.limit))),
|
|
31
|
+
};
|
|
32
|
+
if (query.mediaTypes !== undefined) {
|
|
33
|
+
normalized.mediaTypes = [...query.mediaTypes];
|
|
34
|
+
}
|
|
35
|
+
if (query.search !== undefined) {
|
|
36
|
+
normalized.search = query.search;
|
|
37
|
+
}
|
|
38
|
+
return this.#load(normalized, []);
|
|
39
|
+
}
|
|
40
|
+
subscribe(listener) {
|
|
41
|
+
this.#listeners.add(listener);
|
|
42
|
+
listener(this.state);
|
|
43
|
+
return () => {
|
|
44
|
+
this.#listeners.delete(listener);
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
async #load(query, existing) {
|
|
48
|
+
this.#abortController?.abort();
|
|
49
|
+
const controller = new AbortController();
|
|
50
|
+
this.#abortController = controller;
|
|
51
|
+
this.#setState({ assets: existing, query, status: 'loading' });
|
|
52
|
+
try {
|
|
53
|
+
const page = await this.#provider.list(query, controller.signal);
|
|
54
|
+
if (controller.signal.aborted) {
|
|
55
|
+
return this.state;
|
|
56
|
+
}
|
|
57
|
+
const next = {
|
|
58
|
+
assets: [...existing, ...page.assets],
|
|
59
|
+
query,
|
|
60
|
+
status: 'ready',
|
|
61
|
+
};
|
|
62
|
+
if (page.nextCursor !== undefined) {
|
|
63
|
+
next.nextCursor = page.nextCursor;
|
|
64
|
+
}
|
|
65
|
+
this.#setState(next);
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
if (!controller.signal.aborted) {
|
|
69
|
+
this.#setState({
|
|
70
|
+
assets: existing,
|
|
71
|
+
error: { ...MEDIA_PROVIDER_FAILURE },
|
|
72
|
+
query,
|
|
73
|
+
status: 'error',
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return this.state;
|
|
78
|
+
}
|
|
79
|
+
#setState(state) {
|
|
80
|
+
this.#state = state;
|
|
81
|
+
for (const listener of this.#listeners) {
|
|
82
|
+
listener(this.state);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export function selectBestRendition(asset, targetWidth) {
|
|
87
|
+
if (asset.renditions === undefined || asset.renditions.length === 0) {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
const ordered = [...asset.renditions].sort((left, right) => left.width - right.width);
|
|
91
|
+
return ordered.find((rendition) => rendition.width >= targetWidth) ?? ordered.at(-1);
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=media-library.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-library.js","sourceRoot":"","sources":["../src/media-library.ts"],"names":[],"mappings":"AAQA,MAAM,CAAC,MAAM,sBAAsB,GAA+B,MAAM,CAAC,MAAM,CAAC;IAC9E,cAAc,EAAE,wCAAwC;IACxD,GAAG,EAAE,8BAA8B;CACpC,CAAC,CAAC;AA0BH,MAAM,OAAO,YAAY;IACd,UAAU,GAAG,IAAI,GAAG,EAAwB,CAAC;IAC7C,SAAS,CAAgB;IAClC,gBAAgB,CAAmB;IACnC,MAAM,GAAsB,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAE3D,YAAmB,QAAuB;QACxC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAED,IAAW,KAAK;QACd,OAAO,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IAEM,OAAO;QACZ,IAAI,CAAC,gBAAgB,EAAE,KAAK,EAAE,CAAC;QAC/B,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IAEM,KAAK,CAAC,QAAQ;QACnB,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;YAC5E,OAAO,IAAI,CAAC,KAAK,CAAC;QACpB,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,EAAE;YAC1E,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM;SACtB,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,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AAE,EAAE,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;IACtF,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,IAAI,WAAW,CAAC,IAAI,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;AACvF,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type MediaUploadAcceptedAsset, type MediaUploadPlan, type MediaUploadRequestDescriptor, type MediaUploadSession, type MessageReference } from '@kumwe/studio-protocol';
|
|
2
|
+
export declare const MEDIA_UPLOAD_FAILURE: Readonly<MessageReference>;
|
|
3
|
+
export declare const MEDIA_UPLOAD_TOO_LARGE: Readonly<MessageReference>;
|
|
4
|
+
export interface MediaUploadChunk {
|
|
5
|
+
data: Blob;
|
|
6
|
+
offset: number;
|
|
7
|
+
sessionId: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Host adapter for the upload lifecycle in `docs/contracts/media.md`. The
|
|
11
|
+
* host owns authorization, custody, verification, and acceptance; Studio
|
|
12
|
+
* only orchestrates the canonical media-upload-session state machine.
|
|
13
|
+
*/
|
|
14
|
+
export interface MediaUploadTransport {
|
|
15
|
+
/** Best-effort server-side cancellation; rejections are ignored. */
|
|
16
|
+
abort(sessionId: string): Promise<void>;
|
|
17
|
+
authorize(request: MediaUploadRequestDescriptor, signal?: AbortSignal): Promise<MediaUploadPlan>;
|
|
18
|
+
finalize(sessionId: string, signal?: AbortSignal): Promise<MediaUploadAcceptedAsset>;
|
|
19
|
+
transfer(chunk: MediaUploadChunk, signal?: AbortSignal): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export interface MediaUploadControllerOptions {
|
|
22
|
+
sessionId?: (request: MediaUploadRequestDescriptor) => string;
|
|
23
|
+
}
|
|
24
|
+
export type MediaUploadListener = (session: MediaUploadSession) => void;
|
|
25
|
+
export declare class MediaUploadController {
|
|
26
|
+
#private;
|
|
27
|
+
constructor(transport: MediaUploadTransport, options?: MediaUploadControllerOptions);
|
|
28
|
+
get session(): MediaUploadSession;
|
|
29
|
+
cancel(): void;
|
|
30
|
+
retry(): Promise<MediaUploadSession>;
|
|
31
|
+
subscribe(listener: MediaUploadListener): () => void;
|
|
32
|
+
upload(file: Blob, request: Omit<MediaUploadRequestDescriptor, 'byteSize'>): Promise<MediaUploadSession>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=upload-controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload-controller.d.ts","sourceRoot":"","sources":["../src/upload-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,wBAAwB,EAC7B,KAAK,eAAe,EACpB,KAAK,4BAA4B,EACjC,KAAK,kBAAkB,EAEvB,KAAK,gBAAgB,EACtB,MAAM,wBAAwB,CAAC;AAEhC,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,gBAAgB,CAG1D,CAAC;AAEH,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,gBAAgB,CAG5D,CAAC;AAEH,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;GAIG;AACH,MAAM,WAAW,oBAAoB;IACnC,oEAAoE;IACpE,KAAK,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,SAAS,CAAC,OAAO,EAAE,4BAA4B,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;IACjG,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,wBAAwB,CAAC,CAAC;IACrF,QAAQ,CAAC,KAAK,EAAE,gBAAgB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxE;AAED,MAAM,WAAW,4BAA4B;IAC3C,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,4BAA4B,KAAK,MAAM,CAAC;CAC/D;AAED,MAAM,MAAM,mBAAmB,GAAG,CAAC,OAAO,EAAE,kBAAkB,KAAK,IAAI,CAAC;AASxE,qBAAa,qBAAqB;;gBAQb,SAAS,EAAE,oBAAoB,EAAE,OAAO,CAAC,EAAE,4BAA4B;IAK1F,IAAW,OAAO,IAAI,kBAAkB,CAKvC;IAEM,MAAM,IAAI,IAAI;IAUR,KAAK,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAS1C,SAAS,CAAC,QAAQ,EAAE,mBAAmB,GAAG,MAAM,IAAI;IAU9C,MAAM,CACjB,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,IAAI,CAAC,4BAA4B,EAAE,UAAU,CAAC,GACtD,OAAO,CAAC,kBAAkB,CAAC;CAiJ/B"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { STUDIO_CONTRACT_VERSION, } from '@kumwe/studio-protocol';
|
|
2
|
+
export const MEDIA_UPLOAD_FAILURE = Object.freeze({
|
|
3
|
+
defaultMessage: 'The upload could not be completed.',
|
|
4
|
+
key: 'studio.media/upload-failed',
|
|
5
|
+
});
|
|
6
|
+
export const MEDIA_UPLOAD_TOO_LARGE = Object.freeze({
|
|
7
|
+
defaultMessage: 'The file is larger than the host allows for this upload.',
|
|
8
|
+
key: 'studio.media/upload-too-large',
|
|
9
|
+
});
|
|
10
|
+
const ACTIVE_STATES = new Set([
|
|
11
|
+
'authorized',
|
|
12
|
+
'requested',
|
|
13
|
+
'transferring',
|
|
14
|
+
'verifying',
|
|
15
|
+
]);
|
|
16
|
+
export class MediaUploadController {
|
|
17
|
+
#listeners = new Set();
|
|
18
|
+
#sessionId;
|
|
19
|
+
#transport;
|
|
20
|
+
#abortController;
|
|
21
|
+
#file;
|
|
22
|
+
#session;
|
|
23
|
+
constructor(transport, options) {
|
|
24
|
+
this.#transport = transport;
|
|
25
|
+
this.#sessionId = options?.sessionId ?? (() => crypto.randomUUID());
|
|
26
|
+
}
|
|
27
|
+
get session() {
|
|
28
|
+
if (this.#session === undefined) {
|
|
29
|
+
throw new Error('No upload session has been started.');
|
|
30
|
+
}
|
|
31
|
+
return structuredClone(this.#session);
|
|
32
|
+
}
|
|
33
|
+
cancel() {
|
|
34
|
+
const current = this.#session;
|
|
35
|
+
if (current === undefined || !ACTIVE_STATES.has(current.state)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
this.#abortController?.abort();
|
|
39
|
+
this.#setSession({ ...structuredClone(current), state: 'cancelled' });
|
|
40
|
+
void this.#transport.abort(current.id).catch(() => undefined);
|
|
41
|
+
}
|
|
42
|
+
async retry() {
|
|
43
|
+
const current = this.#session;
|
|
44
|
+
const file = this.#file;
|
|
45
|
+
if (current?.state !== 'failed' || file === undefined) {
|
|
46
|
+
throw new Error('Only a failed upload session can be retried.');
|
|
47
|
+
}
|
|
48
|
+
return this.#run(file, structuredClone(current.request));
|
|
49
|
+
}
|
|
50
|
+
subscribe(listener) {
|
|
51
|
+
this.#listeners.add(listener);
|
|
52
|
+
if (this.#session !== undefined) {
|
|
53
|
+
listener(this.session);
|
|
54
|
+
}
|
|
55
|
+
return () => {
|
|
56
|
+
this.#listeners.delete(listener);
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async upload(file, request) {
|
|
60
|
+
if (this.#session !== undefined && ACTIVE_STATES.has(this.#session.state)) {
|
|
61
|
+
throw new Error('An upload session is already in progress.');
|
|
62
|
+
}
|
|
63
|
+
if (file.size < 1) {
|
|
64
|
+
throw new Error('Cannot upload an empty file.');
|
|
65
|
+
}
|
|
66
|
+
const descriptor = {
|
|
67
|
+
byteSize: file.size,
|
|
68
|
+
filename: request.filename,
|
|
69
|
+
mediaType: request.mediaType,
|
|
70
|
+
purpose: request.purpose,
|
|
71
|
+
};
|
|
72
|
+
if (request.checksum !== undefined) {
|
|
73
|
+
descriptor.checksum = request.checksum;
|
|
74
|
+
}
|
|
75
|
+
this.#file = file;
|
|
76
|
+
return this.#run(file, descriptor);
|
|
77
|
+
}
|
|
78
|
+
#fail() {
|
|
79
|
+
const current = this.#session;
|
|
80
|
+
if (current === undefined) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const failed = {
|
|
84
|
+
contractVersion: current.contractVersion,
|
|
85
|
+
failure: {
|
|
86
|
+
code: 'studio.media/upload-failed',
|
|
87
|
+
message: { ...MEDIA_UPLOAD_FAILURE },
|
|
88
|
+
severity: 'error',
|
|
89
|
+
},
|
|
90
|
+
id: current.id,
|
|
91
|
+
kind: 'media-upload-session',
|
|
92
|
+
progress: { ...current.progress },
|
|
93
|
+
request: { ...current.request },
|
|
94
|
+
state: 'failed',
|
|
95
|
+
};
|
|
96
|
+
if (current.plan !== undefined) {
|
|
97
|
+
failed.plan = { ...current.plan };
|
|
98
|
+
}
|
|
99
|
+
this.#setSession(failed);
|
|
100
|
+
}
|
|
101
|
+
async #run(file, request) {
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
this.#abortController = controller;
|
|
104
|
+
const totalBytes = request.byteSize;
|
|
105
|
+
const base = {
|
|
106
|
+
contractVersion: STUDIO_CONTRACT_VERSION,
|
|
107
|
+
id: this.#sessionId(request),
|
|
108
|
+
kind: 'media-upload-session',
|
|
109
|
+
request,
|
|
110
|
+
};
|
|
111
|
+
this.#setSession({
|
|
112
|
+
...base,
|
|
113
|
+
progress: { totalBytes, transferredBytes: 0 },
|
|
114
|
+
state: 'requested',
|
|
115
|
+
});
|
|
116
|
+
try {
|
|
117
|
+
const plan = await this.#transport.authorize(request, controller.signal);
|
|
118
|
+
if (controller.signal.aborted) {
|
|
119
|
+
return this.session;
|
|
120
|
+
}
|
|
121
|
+
if (totalBytes > plan.maximumBytes) {
|
|
122
|
+
this.#setSession({
|
|
123
|
+
...base,
|
|
124
|
+
failure: {
|
|
125
|
+
code: 'studio.media/upload-too-large',
|
|
126
|
+
message: { ...MEDIA_UPLOAD_TOO_LARGE },
|
|
127
|
+
parameters: { byteSize: totalBytes, maximumBytes: plan.maximumBytes },
|
|
128
|
+
severity: 'error',
|
|
129
|
+
},
|
|
130
|
+
plan,
|
|
131
|
+
progress: { totalBytes, transferredBytes: 0 },
|
|
132
|
+
state: 'failed',
|
|
133
|
+
});
|
|
134
|
+
return this.session;
|
|
135
|
+
}
|
|
136
|
+
this.#setSession({
|
|
137
|
+
...base,
|
|
138
|
+
plan,
|
|
139
|
+
progress: { totalBytes, transferredBytes: 0 },
|
|
140
|
+
state: 'authorized',
|
|
141
|
+
});
|
|
142
|
+
this.#setSession({
|
|
143
|
+
...base,
|
|
144
|
+
plan,
|
|
145
|
+
progress: { totalBytes, transferredBytes: 0 },
|
|
146
|
+
state: 'transferring',
|
|
147
|
+
});
|
|
148
|
+
const chunkBytes = Math.max(1, plan.chunkBytes ?? totalBytes);
|
|
149
|
+
let transferredBytes = 0;
|
|
150
|
+
while (transferredBytes < totalBytes) {
|
|
151
|
+
const data = file.slice(transferredBytes, Math.min(transferredBytes + chunkBytes, totalBytes));
|
|
152
|
+
await this.#transport.transfer({ data, offset: transferredBytes, sessionId: base.id }, controller.signal);
|
|
153
|
+
if (controller.signal.aborted) {
|
|
154
|
+
return this.session;
|
|
155
|
+
}
|
|
156
|
+
transferredBytes = Math.min(transferredBytes + data.size, totalBytes);
|
|
157
|
+
this.#setSession({
|
|
158
|
+
...base,
|
|
159
|
+
plan,
|
|
160
|
+
progress: { totalBytes, transferredBytes },
|
|
161
|
+
state: 'transferring',
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
this.#setSession({
|
|
165
|
+
...base,
|
|
166
|
+
plan,
|
|
167
|
+
progress: { totalBytes, transferredBytes: totalBytes },
|
|
168
|
+
state: 'verifying',
|
|
169
|
+
});
|
|
170
|
+
const asset = await this.#transport.finalize(base.id, controller.signal);
|
|
171
|
+
if (controller.signal.aborted) {
|
|
172
|
+
return this.session;
|
|
173
|
+
}
|
|
174
|
+
this.#setSession({
|
|
175
|
+
...base,
|
|
176
|
+
asset,
|
|
177
|
+
plan,
|
|
178
|
+
progress: { totalBytes, transferredBytes: totalBytes },
|
|
179
|
+
state: 'complete',
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
if (!controller.signal.aborted) {
|
|
184
|
+
this.#fail();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return this.session;
|
|
188
|
+
}
|
|
189
|
+
#setSession(session) {
|
|
190
|
+
this.#session = session;
|
|
191
|
+
for (const listener of this.#listeners) {
|
|
192
|
+
listener(this.session);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=upload-controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"upload-controller.js","sourceRoot":"","sources":["../src/upload-controller.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,GAOxB,MAAM,wBAAwB,CAAC;AAEhC,MAAM,CAAC,MAAM,oBAAoB,GAA+B,MAAM,CAAC,MAAM,CAAC;IAC5E,cAAc,EAAE,oCAAoC;IACpD,GAAG,EAAE,4BAA4B;CAClC,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,sBAAsB,GAA+B,MAAM,CAAC,MAAM,CAAC;IAC9E,cAAc,EAAE,0DAA0D;IAC1E,GAAG,EAAE,+BAA+B;CACrC,CAAC,CAAC;AA2BH,MAAM,aAAa,GAAyC,IAAI,GAAG,CAA0B;IAC3F,YAAY;IACZ,WAAW;IACX,cAAc;IACd,WAAW;CACZ,CAAC,CAAC;AAEH,MAAM,OAAO,qBAAqB;IACvB,UAAU,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC5C,UAAU,CAAoD;IAC9D,UAAU,CAAuB;IAC1C,gBAAgB,CAAmB;IACnC,KAAK,CAAQ;IACb,QAAQ,CAAsB;IAE9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;gBAC/B,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,WAAW,CAAC,OAA2B;QACrC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;YACvC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { MediaReference, StudioDiagnostic } from '@kumwe/studio-protocol';
|
|
2
|
+
/**
|
|
3
|
+
* Semantic validation the media-reference schema cannot express: a
|
|
4
|
+
* rectangle crop uses normalized coordinates and MUST remain inside the
|
|
5
|
+
* source bounds. Schema validation is assumed to have passed already.
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateMediaReference(reference: MediaReference): StudioDiagnostic[];
|
|
8
|
+
//# sourceMappingURL=validate-media-reference.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-media-reference.d.ts","sourceRoot":"","sources":["../src/validate-media-reference.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE/E;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,cAAc,GAAG,gBAAgB,EAAE,CAiBpF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic validation the media-reference schema cannot express: a
|
|
3
|
+
* rectangle crop uses normalized coordinates and MUST remain inside the
|
|
4
|
+
* source bounds. Schema validation is assumed to have passed already.
|
|
5
|
+
*/
|
|
6
|
+
export function validateMediaReference(reference) {
|
|
7
|
+
const diagnostics = [];
|
|
8
|
+
const crop = reference.cropIntent;
|
|
9
|
+
if (crop?.mode === 'rectangle') {
|
|
10
|
+
if (crop.x + crop.width > 1 || crop.y + crop.height > 1) {
|
|
11
|
+
diagnostics.push({
|
|
12
|
+
code: 'studio.media/crop-out-of-bounds',
|
|
13
|
+
location: { artifactId: reference.assetId },
|
|
14
|
+
message: {
|
|
15
|
+
defaultMessage: 'The crop rectangle extends beyond the source media bounds.',
|
|
16
|
+
key: 'studio.media/crop-out-of-bounds',
|
|
17
|
+
},
|
|
18
|
+
severity: 'error',
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return diagnostics;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=validate-media-reference.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate-media-reference.js","sourceRoot":"","sources":["../src/validate-media-reference.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,sBAAsB,CAAC,SAAyB;IAC9D,MAAM,WAAW,GAAuB,EAAE,CAAC;IAC3C,MAAM,IAAI,GAAG,SAAS,CAAC,UAAU,CAAC;IAClC,IAAI,IAAI,EAAE,IAAI,KAAK,WAAW,EAAE,CAAC;QAC/B,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACxD,WAAW,CAAC,IAAI,CAAC;gBACf,IAAI,EAAE,iCAAiC;gBACvC,QAAQ,EAAE,EAAE,UAAU,EAAE,SAAS,CAAC,OAAO,EAAE;gBAC3C,OAAO,EAAE;oBACP,cAAc,EAAE,4DAA4D;oBAC5E,GAAG,EAAE,iCAAiC;iBACvC;gBACD,QAAQ,EAAE,OAAO;aAClB,CAAC,CAAC;QACL,CAAC;IACH,CAAC;IACD,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kumwe/studio-media",
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/kumwe/studio.git",
|
|
7
|
+
"directory": "packages/media"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/kumwe/studio/tree/main/packages/media#readme",
|
|
10
|
+
"bugs": "https://github.com/kumwe/studio/issues",
|
|
11
|
+
"description": "Host-neutral media browsing, upload orchestration, and rendition selection.",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"LICENSE",
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"import": "./dist/index.js"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc -b"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@kumwe/studio-protocol": "0.1.0-alpha.2"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"provenance": true
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=24.0.0"
|
|
38
|
+
}
|
|
39
|
+
}
|