@devultur/server 0.0.0-stage → 1.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cristhian De Marchena
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 CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @devultur/server
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Server SDK for Devultur media: create upload URLs, issue viewer tokens, sign and verify delivery URLs, trigger
4
+ transcodes and captions, and handle webhooks. Point it at your self-hosted media Worker, or straight at your own
5
+ Cloudflare R2 bucket.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @devultur/server
11
+ ```
12
+
13
+ ## Example
14
+
15
+ ```typescript
16
+ import { createMediaRouter } from "@devultur/server";
17
+
18
+ export const media = createMediaRouter({
19
+ baseUrl: "https://media.example.com", // your media Worker
20
+ secret: process.env.WORKER_AUTH_SECRET!, // never ship it to the browser
21
+ });
22
+
23
+ // In an authenticated route: the browser uploads to `url`, you store `key`.
24
+ const { url, key } = await media.createUploadUrl({ filename: "lesson-1.mp4", contentType: "video/mp4" });
25
+
26
+ // A short-lived token that lets this user play private media.
27
+ const { token } = await media.issueToken("user_123");
28
+ ```
29
+
30
+ Without a Worker, pass `storage` (R2 credentials) and `delivery` (`signSecret`) instead of `baseUrl` and `secret`.
31
+ `@devultur/server/node` adds `uploadLargeFile`, a parallel multipart upload of a large file from disk (Node 20+).
32
+
33
+ ## Documentation
34
+
35
+ Guides and API reference: [github.com/devultur/devultur/tree/main/docs](https://github.com/devultur/devultur/tree/main/docs).
36
+
37
+ ## License
38
+
39
+ [MIT](https://github.com/devultur/devultur/blob/main/LICENSE)
@@ -0,0 +1,46 @@
1
+ import { MediaEvent, MediaRouter, UploadUrlResult } from '@devultur/core';
2
+ export { AssetUploadResult, CreateUploadTokenOptions, CreateUploadUrlOptions, DeliveryConfig, DevulturApiError, DevulturKind, DevulturMedia, DirectStorageConfig, DownloadLink, DownloadLinkOptions, DownloadUrlOptions, FileBuilderConfig, FileHead, FilesClient, ImageBuilderConfig, MediaEvent, MediaEventName, MediaRouter, MediaRouterConfig, MediaUrlOptions, SignOptions, StorageClient, StorageConfig, UploadAssetOptions, UploadToken, UploadUrlResult, UploadsClient, VideoBuilderConfig, Visibility, WebhookVerifyOptions, WorkerClient, WorkerConfig, assetKey, buildMediaUrl, createMediaRouter, createStorageClient, createWorkerClient, file, getDownloadUrl, image, mediaCaptionPath, mediaPath, mediaThumbnailPath, mediaUrl, normalizeAssetPath, normalizeAssetPrefix, publicUrl, resolveVisibility, signPayload, signUrl, verifySignature, verifySignedUrl, video, visibilityFromKey } from '@devultur/core';
3
+
4
+ interface DevulturRequest {
5
+ method: string;
6
+ url: string;
7
+ headers: Record<string, string | undefined>;
8
+ body: unknown;
9
+ }
10
+ interface DevulturResponse {
11
+ status: number;
12
+ headers: Record<string, string>;
13
+ body: unknown;
14
+ }
15
+ interface HandleUploadOptions {
16
+ filename: string;
17
+ contentType: string;
18
+ metadata?: Record<string, string>;
19
+ }
20
+ /** One event of the media Worker's webhook, narrowed by its `event` name. */
21
+ type MediaEventOf<Name extends MediaEvent["event"]> = Extract<MediaEvent, {
22
+ event: Name;
23
+ }>;
24
+ interface WebhookHandlerConfig {
25
+ /** The secret the Worker sends as `Authorization: Bearer <secret>`. */
26
+ secret: string;
27
+ /** Every authenticated event, including `transcode.started` and `transcode.progress`. */
28
+ onEvent?: (event: MediaEvent) => Promise<void>;
29
+ onTranscodeComplete?: (event: MediaEventOf<"transcode.complete">) => Promise<void>;
30
+ onTranscodeFailed?: (event: MediaEventOf<"transcode.failed">) => Promise<void>;
31
+ onCaptionsComplete?: (event: MediaEventOf<"captions.complete">) => Promise<void>;
32
+ onCaptionsFailed?: (event: MediaEventOf<"captions.failed">) => Promise<void>;
33
+ }
34
+
35
+ declare function fromWebRequest(request: Request): Promise<DevulturRequest>;
36
+ declare function toWebResponse(response: DevulturResponse): Response;
37
+
38
+ declare function createUploadHandler(router: MediaRouter): (options: HandleUploadOptions) => Promise<UploadUrlResult>;
39
+ declare function createSignedUrlHandler(router: MediaRouter): (url: string, expiresIn?: number) => Promise<string>;
40
+ declare function createWebhookHandler(router: MediaRouter, config: WebhookHandlerConfig): (request: DevulturRequest) => Promise<DevulturResponse>;
41
+
42
+ interface DevulturMiddlewareOptions {
43
+ prefix?: string;
44
+ }
45
+
46
+ export { type DevulturMiddlewareOptions, type DevulturRequest, type DevulturResponse, type HandleUploadOptions, type MediaEventOf, type WebhookHandlerConfig, createSignedUrlHandler, createUploadHandler, createWebhookHandler, fromWebRequest, toWebResponse };
package/dist/index.js ADDED
@@ -0,0 +1,133 @@
1
+ // src/index.ts
2
+ import {
3
+ assetKey,
4
+ buildMediaUrl,
5
+ createMediaRouter,
6
+ createStorageClient,
7
+ createWorkerClient,
8
+ DevulturApiError,
9
+ file,
10
+ getDownloadUrl,
11
+ image,
12
+ MediaEventName as MediaEventName2,
13
+ mediaCaptionPath,
14
+ mediaPath,
15
+ mediaThumbnailPath,
16
+ mediaUrl,
17
+ normalizeAssetPath,
18
+ normalizeAssetPrefix,
19
+ publicUrl,
20
+ resolveVisibility,
21
+ signPayload,
22
+ signUrl,
23
+ verifySignature,
24
+ verifySignedUrl,
25
+ video,
26
+ visibilityFromKey
27
+ } from "@devultur/core";
28
+
29
+ // src/adapters/web.ts
30
+ async function fromWebRequest(request) {
31
+ const headers = {};
32
+ request.headers.forEach((value, key) => {
33
+ headers[key] = value;
34
+ });
35
+ let body = null;
36
+ if (request.method !== "GET" && request.method !== "HEAD") {
37
+ const contentType = request.headers.get("content-type") ?? "";
38
+ if (contentType.includes("application/json")) {
39
+ body = await request.json();
40
+ } else {
41
+ body = await request.text();
42
+ }
43
+ }
44
+ return {
45
+ method: request.method,
46
+ url: request.url,
47
+ headers,
48
+ body
49
+ };
50
+ }
51
+ function toWebResponse(response) {
52
+ const body = typeof response.body === "string" ? response.body : JSON.stringify(response.body);
53
+ return new Response(body, {
54
+ status: response.status,
55
+ headers: response.headers
56
+ });
57
+ }
58
+
59
+ // src/handlers.ts
60
+ import { MediaEventName } from "@devultur/core";
61
+ function createUploadHandler(router) {
62
+ return async (options) => {
63
+ return router.createUploadUrl({
64
+ filename: options.filename,
65
+ contentType: options.contentType,
66
+ metadata: options.metadata
67
+ });
68
+ };
69
+ }
70
+ function createSignedUrlHandler(router) {
71
+ return async (url, expiresIn) => {
72
+ return router.sign(url, { expiresIn });
73
+ };
74
+ }
75
+ function createWebhookHandler(router, config) {
76
+ return async (request) => {
77
+ let event;
78
+ try {
79
+ event = await router.handleWebhook(request.body, {
80
+ authorization: request.headers.authorization ?? request.headers.Authorization,
81
+ secret: config.secret
82
+ });
83
+ } catch {
84
+ return { status: 401, headers: { "content-type": "application/json" }, body: { error: "Unauthorized" } };
85
+ }
86
+ await config.onEvent?.(event);
87
+ await dispatchEvent(event, config);
88
+ return { status: 200, headers: { "content-type": "application/json" }, body: { received: true } };
89
+ };
90
+ }
91
+ async function dispatchEvent(event, config) {
92
+ switch (event.event) {
93
+ case MediaEventName.TRANSCODE_COMPLETE:
94
+ return config.onTranscodeComplete?.(event);
95
+ case MediaEventName.TRANSCODE_FAILED:
96
+ return config.onTranscodeFailed?.(event);
97
+ case MediaEventName.CAPTIONS_COMPLETE:
98
+ return config.onCaptionsComplete?.(event);
99
+ case MediaEventName.CAPTIONS_FAILED:
100
+ return config.onCaptionsFailed?.(event);
101
+ }
102
+ }
103
+ export {
104
+ DevulturApiError,
105
+ MediaEventName2 as MediaEventName,
106
+ assetKey,
107
+ buildMediaUrl,
108
+ createMediaRouter,
109
+ createSignedUrlHandler,
110
+ createStorageClient,
111
+ createUploadHandler,
112
+ createWebhookHandler,
113
+ createWorkerClient,
114
+ file,
115
+ fromWebRequest,
116
+ getDownloadUrl,
117
+ image,
118
+ mediaCaptionPath,
119
+ mediaPath,
120
+ mediaThumbnailPath,
121
+ mediaUrl,
122
+ normalizeAssetPath,
123
+ normalizeAssetPrefix,
124
+ publicUrl,
125
+ resolveVisibility,
126
+ signPayload,
127
+ signUrl,
128
+ toWebResponse,
129
+ verifySignature,
130
+ verifySignedUrl,
131
+ video,
132
+ visibilityFromKey
133
+ };
package/dist/node.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { AssetUploadResult, Visibility } from '@devultur/core';
2
+
3
+ interface UploadLargeFileOptions {
4
+ /** Prefix inside `asset/` (`exports/inst-1/`). */
5
+ prefix: string;
6
+ /** File name in the prefix. Default: the local file's basename. */
7
+ name?: string;
8
+ /** TTL in seconds (with an upload token, at most the token's own). */
9
+ expiresIn?: number;
10
+ /** Upload token or the worker's credential. Default: `DEVULTUR_UPLOAD_TOKEN` or `DEVULTUR_SECRET`. */
11
+ credential?: string;
12
+ /** Worker origin (`https://media.example.com`). */
13
+ baseUrl: string;
14
+ contentType?: string;
15
+ overwrite?: boolean;
16
+ visibility?: Visibility;
17
+ /** `Cache-Control`: preset (`immutable` | `revalidate` | `no-cache`) or a string validated by the worker. */
18
+ cacheControl?: string;
19
+ /** Already-computed sha256 hex (e.g. from a sync script): used instead of reading the file again. */
20
+ sha256?: string;
21
+ /** Parts in flight at once (default 4). */
22
+ concurrency?: number;
23
+ onProgress?: (percent: number) => void;
24
+ signal?: AbortSignal;
25
+ }
26
+ interface LargeFileUploadResult extends AssetUploadResult {
27
+ /** sha256 computed locally (the one declared to the worker). */
28
+ sha256: string;
29
+ }
30
+ declare function uploadLargeFile(filePath: string, options: UploadLargeFileOptions): Promise<LargeFileUploadResult>;
31
+
32
+ export { type LargeFileUploadResult, type UploadLargeFileOptions, uploadLargeFile };
package/dist/node.js ADDED
@@ -0,0 +1,42 @@
1
+ // src/upload-large-file.ts
2
+ import { createWorkerClient } from "@devultur/core";
3
+ async function sha256OfFile(filePath) {
4
+ const { createHash } = await import("crypto");
5
+ const { createReadStream } = await import("fs");
6
+ const hash = createHash("sha256");
7
+ for await (const chunk of createReadStream(filePath)) hash.update(chunk);
8
+ return hash.digest("hex");
9
+ }
10
+ function resolveCredential(explicit) {
11
+ const credential = explicit ?? process.env.DEVULTUR_UPLOAD_TOKEN ?? process.env.DEVULTUR_SECRET;
12
+ if (!credential) throw new Error("uploadLargeFile needs a credential (DEVULTUR_UPLOAD_TOKEN or DEVULTUR_SECRET)");
13
+ return credential;
14
+ }
15
+ async function uploadLargeFile(filePath, options) {
16
+ const { openAsBlob } = await import("fs");
17
+ const { basename } = await import("path");
18
+ const credential = resolveCredential(options.credential);
19
+ const prefix = options.prefix.endsWith("/") ? options.prefix : `${options.prefix}/`;
20
+ const sha256 = options.sha256 ?? await sha256OfFile(filePath);
21
+ const file = await openAsBlob(filePath, { type: options.contentType ?? "application/octet-stream" });
22
+ const client = createWorkerClient(credential, options.baseUrl);
23
+ const result = await client.uploads.uploadAsset(file, {
24
+ path: `${prefix}${options.name ?? basename(filePath)}`,
25
+ contentType: options.contentType,
26
+ sha256,
27
+ expiresIn: options.expiresIn,
28
+ overwrite: options.overwrite,
29
+ visibility: options.visibility,
30
+ cacheControl: options.cacheControl,
31
+ concurrency: options.concurrency,
32
+ onProgress: options.onProgress,
33
+ signal: options.signal
34
+ });
35
+ if (result.sha256 && result.sha256 !== sha256) {
36
+ throw new Error(`sha256 mismatch for ${result.key}: local ${sha256}, stored ${result.sha256}`);
37
+ }
38
+ return { ...result, sha256 };
39
+ }
40
+ export {
41
+ uploadLargeFile
42
+ };
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "@devultur/server",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.1",
4
+ "description": "Server SDK for Devultur media: upload URLs, viewer tokens, signed delivery URLs, transcodes, captions and webhooks",
5
+ "keywords": [
6
+ "media",
7
+ "video",
8
+ "r2",
9
+ "cloudflare",
10
+ "hls",
11
+ "upload",
12
+ "webhooks",
13
+ "signed-url"
14
+ ],
15
+ "private": false,
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/devultur/devultur",
20
+ "directory": "packages/server"
21
+ },
22
+ "homepage": "https://github.com/devultur/devultur#readme",
23
+ "bugs": {
24
+ "url": "https://github.com/devultur/devultur/issues"
25
+ },
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/index.js",
33
+ "types": "./dist/index.d.ts"
34
+ },
35
+ "./node": {
36
+ "import": "./dist/node.js",
37
+ "types": "./dist/node.d.ts"
38
+ }
39
+ },
40
+ "files": [
41
+ "dist"
42
+ ],
43
+ "dependencies": {
44
+ "@devultur/core": "1.0.1"
45
+ },
46
+ "devDependencies": {
47
+ "@types/node": "^22.20.0",
48
+ "tsup": "^8.0.0",
49
+ "typescript": "^5.8.0"
50
+ },
51
+ "publishConfig": {
52
+ "access": "public"
53
+ },
54
+ "scripts": {
55
+ "build": "tsup src/index.ts src/node.ts --format esm --dts",
56
+ "dev": "tsup src/index.ts src/node.ts --format esm --dts --watch",
57
+ "typecheck": "tsc --noEmit"
58
+ }
6
59
  }