payload-video-streaming 0.0.0-stage → 0.2.0-beta.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 +21 -0
- package/README.md +289 -2
- package/dist/access.d.ts +35 -0
- package/dist/access.js +53 -0
- package/dist/access.js.map +1 -0
- package/dist/bin/args.d.ts +37 -0
- package/dist/bin/args.js +157 -0
- package/dist/bin/args.js.map +1 -0
- package/dist/bin/cli.d.ts +2 -0
- package/dist/bin/cli.js +25 -0
- package/dist/bin/cli.js.map +1 -0
- package/dist/bin/fake.d.ts +2 -0
- package/dist/bin/fake.js +14 -0
- package/dist/bin/fake.js.map +1 -0
- package/dist/bin/migrate.d.ts +5 -0
- package/dist/bin/migrate.js +47 -0
- package/dist/bin/migrate.js.map +1 -0
- package/dist/bin/runFake.d.ts +2 -0
- package/dist/bin/runFake.js +24 -0
- package/dist/bin/runFake.js.map +1 -0
- package/dist/bin/webhooks.d.ts +15 -0
- package/dist/bin/webhooks.js +149 -0
- package/dist/bin/webhooks.js.map +1 -0
- package/dist/blocks.d.ts +42 -0
- package/dist/blocks.js +150 -0
- package/dist/blocks.js.map +1 -0
- package/dist/collection.d.ts +10 -0
- package/dist/collection.js +697 -0
- package/dist/collection.js.map +1 -0
- package/dist/components/BulkUploader.d.ts +16 -0
- package/dist/components/BulkUploader.js +226 -0
- package/dist/components/BulkUploader.js.map +1 -0
- package/dist/components/CaptionsManager.d.ts +9 -0
- package/dist/components/CaptionsManager.js +174 -0
- package/dist/components/CaptionsManager.js.map +1 -0
- package/dist/components/DownloadsPanel.d.ts +9 -0
- package/dist/components/DownloadsPanel.js +147 -0
- package/dist/components/DownloadsPanel.js.map +1 -0
- package/dist/components/ListUploader.d.ts +11 -0
- package/dist/components/ListUploader.js +138 -0
- package/dist/components/ListUploader.js.map +1 -0
- package/dist/components/ThumbnailCell.d.ts +5 -0
- package/dist/components/ThumbnailCell.js +28 -0
- package/dist/components/ThumbnailCell.js.map +1 -0
- package/dist/components/Uploader.d.ts +22 -0
- package/dist/components/Uploader.js +171 -0
- package/dist/components/Uploader.js.map +1 -0
- package/dist/components/VideoBlockLabel.d.ts +11 -0
- package/dist/components/VideoBlockLabel.js +86 -0
- package/dist/components/VideoBlockLabel.js.map +1 -0
- package/dist/components/VideoField.d.ts +17 -0
- package/dist/components/VideoField.js +422 -0
- package/dist/components/VideoField.js.map +1 -0
- package/dist/components/VideoRelationshipField.d.ts +16 -0
- package/dist/components/VideoRelationshipField.js +106 -0
- package/dist/components/VideoRelationshipField.js.map +1 -0
- package/dist/components/WebhookStatusPanel.d.ts +6 -0
- package/dist/components/WebhookStatusPanel.js +143 -0
- package/dist/components/WebhookStatusPanel.js.map +1 -0
- package/dist/components/api.d.ts +52 -0
- package/dist/components/api.js +76 -0
- package/dist/components/api.js.map +1 -0
- package/dist/components/chunkedUpload.d.ts +28 -0
- package/dist/components/chunkedUpload.js +49 -0
- package/dist/components/chunkedUpload.js.map +1 -0
- package/dist/components/styles.d.ts +95 -0
- package/dist/components/styles.js +148 -0
- package/dist/components/styles.js.map +1 -0
- package/dist/components/uploadFile.d.ts +30 -0
- package/dist/components/uploadFile.js +140 -0
- package/dist/components/uploadFile.js.map +1 -0
- package/dist/components/usePluginApi.d.ts +46 -0
- package/dist/components/usePluginApi.js +21 -0
- package/dist/components/usePluginApi.js.map +1 -0
- package/dist/components/useUploadQueue.d.ts +35 -0
- package/dist/components/useUploadQueue.js +135 -0
- package/dist/components/useUploadQueue.js.map +1 -0
- package/dist/components/useVideoUpload.d.ts +33 -0
- package/dist/components/useVideoUpload.js +87 -0
- package/dist/components/useVideoUpload.js.map +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.js +29 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/provider.d.ts +347 -0
- package/dist/core/provider.js +13 -0
- package/dist/core/provider.js.map +1 -0
- package/dist/core/registry.d.ts +17 -0
- package/dist/core/registry.js +110 -0
- package/dist/core/registry.js.map +1 -0
- package/dist/core/status.d.ts +15 -0
- package/dist/core/status.js +29 -0
- package/dist/core/status.js.map +1 -0
- package/dist/endpoints/index.d.ts +3 -0
- package/dist/endpoints/index.js +500 -0
- package/dist/endpoints/index.js.map +1 -0
- package/dist/env.d.ts +40 -0
- package/dist/env.js +105 -0
- package/dist/env.js.map +1 -0
- package/dist/exports/bunny.d.ts +16 -0
- package/dist/exports/bunny.js +13 -0
- package/dist/exports/bunny.js.map +1 -0
- package/dist/exports/client.d.ts +22 -0
- package/dist/exports/client.js +18 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/cloudflare.d.ts +18 -0
- package/dist/exports/cloudflare.js +15 -0
- package/dist/exports/cloudflare.js.map +1 -0
- package/dist/exports/mux.d.ts +18 -0
- package/dist/exports/mux.js +15 -0
- package/dist/exports/mux.js.map +1 -0
- package/dist/exports/react.d.ts +25 -0
- package/dist/exports/react.js +18 -0
- package/dist/exports/react.js.map +1 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +4 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/exports/testing.d.ts +13 -0
- package/dist/exports/testing.js +9 -0
- package/dist/exports/testing.js.map +1 -0
- package/dist/fields.d.ts +22 -0
- package/dist/fields.js +73 -0
- package/dist/fields.js.map +1 -0
- package/dist/frontend/ClientVideo.d.ts +14 -0
- package/dist/frontend/ClientVideo.js +43 -0
- package/dist/frontend/ClientVideo.js.map +1 -0
- package/dist/frontend/Video.d.ts +32 -0
- package/dist/frontend/Video.js +73 -0
- package/dist/frontend/Video.js.map +1 -0
- package/dist/frontend/VideoIframe.d.ts +35 -0
- package/dist/frontend/VideoIframe.js +60 -0
- package/dist/frontend/VideoIframe.js.map +1 -0
- package/dist/frontend/VideoUploader.d.ts +65 -0
- package/dist/frontend/VideoUploader.js +260 -0
- package/dist/frontend/VideoUploader.js.map +1 -0
- package/dist/frontend/constants.d.ts +10 -0
- package/dist/frontend/constants.js +10 -0
- package/dist/frontend/constants.js.map +1 -0
- package/dist/frontend/converters.d.ts +25 -0
- package/dist/frontend/converters.js +24 -0
- package/dist/frontend/converters.js.map +1 -0
- package/dist/frontend/source.d.ts +80 -0
- package/dist/frontend/source.js +159 -0
- package/dist/frontend/source.js.map +1 -0
- package/dist/frontend/useVideoSource.d.ts +27 -0
- package/dist/frontend/useVideoSource.js +108 -0
- package/dist/frontend/useVideoSource.js.map +1 -0
- package/dist/frontend/useVideoStatus.d.ts +26 -0
- package/dist/frontend/useVideoStatus.js +68 -0
- package/dist/frontend/useVideoStatus.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate/cfStream.d.ts +108 -0
- package/dist/migrate/cfStream.js +418 -0
- package/dist/migrate/cfStream.js.map +1 -0
- package/dist/options.d.ts +9 -0
- package/dist/options.js +153 -0
- package/dist/options.js.map +1 -0
- package/dist/plugin.d.ts +9 -0
- package/dist/plugin.js +177 -0
- package/dist/plugin.js.map +1 -0
- package/dist/providers/bunny/client.d.ts +78 -0
- package/dist/providers/bunny/client.js +231 -0
- package/dist/providers/bunny/client.js.map +1 -0
- package/dist/providers/bunny/env.d.ts +18 -0
- package/dist/providers/bunny/env.js +31 -0
- package/dist/providers/bunny/env.js.map +1 -0
- package/dist/providers/bunny/mapping.d.ts +39 -0
- package/dist/providers/bunny/mapping.js +123 -0
- package/dist/providers/bunny/mapping.js.map +1 -0
- package/dist/providers/bunny/options.d.ts +92 -0
- package/dist/providers/bunny/options.js +85 -0
- package/dist/providers/bunny/options.js.map +1 -0
- package/dist/providers/bunny/provider.d.ts +70 -0
- package/dist/providers/bunny/provider.js +480 -0
- package/dist/providers/bunny/provider.js.map +1 -0
- package/dist/providers/bunny/signing.d.ts +61 -0
- package/dist/providers/bunny/signing.js +144 -0
- package/dist/providers/bunny/signing.js.map +1 -0
- package/dist/providers/bunny/types.d.ts +153 -0
- package/dist/providers/bunny/types.js +64 -0
- package/dist/providers/bunny/types.js.map +1 -0
- package/dist/providers/bunny/urls.d.ts +49 -0
- package/dist/providers/bunny/urls.js +70 -0
- package/dist/providers/bunny/urls.js.map +1 -0
- package/dist/providers/bunny/webhook.d.ts +32 -0
- package/dist/providers/bunny/webhook.js +75 -0
- package/dist/providers/bunny/webhook.js.map +1 -0
- package/dist/providers/cloudflare/client.d.ts +93 -0
- package/dist/providers/cloudflare/client.js +436 -0
- package/dist/providers/cloudflare/client.js.map +1 -0
- package/dist/providers/cloudflare/env.d.ts +22 -0
- package/dist/providers/cloudflare/env.js +57 -0
- package/dist/providers/cloudflare/env.js.map +1 -0
- package/dist/providers/cloudflare/mapping.d.ts +15 -0
- package/dist/providers/cloudflare/mapping.js +95 -0
- package/dist/providers/cloudflare/mapping.js.map +1 -0
- package/dist/providers/cloudflare/options.d.ts +104 -0
- package/dist/providers/cloudflare/options.js +91 -0
- package/dist/providers/cloudflare/options.js.map +1 -0
- package/dist/providers/cloudflare/provider.d.ts +63 -0
- package/dist/providers/cloudflare/provider.js +445 -0
- package/dist/providers/cloudflare/provider.js.map +1 -0
- package/dist/providers/cloudflare/signing.d.ts +41 -0
- package/dist/providers/cloudflare/signing.js +97 -0
- package/dist/providers/cloudflare/signing.js.map +1 -0
- package/dist/providers/cloudflare/tus.d.ts +24 -0
- package/dist/providers/cloudflare/tus.js +55 -0
- package/dist/providers/cloudflare/tus.js.map +1 -0
- package/dist/providers/cloudflare/types.d.ts +156 -0
- package/dist/providers/cloudflare/types.js +14 -0
- package/dist/providers/cloudflare/types.js.map +1 -0
- package/dist/providers/cloudflare/urls.d.ts +55 -0
- package/dist/providers/cloudflare/urls.js +87 -0
- package/dist/providers/cloudflare/urls.js.map +1 -0
- package/dist/providers/cloudflare/webhook.d.ts +36 -0
- package/dist/providers/cloudflare/webhook.js +83 -0
- package/dist/providers/cloudflare/webhook.js.map +1 -0
- package/dist/providers/mux/client.d.ts +82 -0
- package/dist/providers/mux/client.js +283 -0
- package/dist/providers/mux/client.js.map +1 -0
- package/dist/providers/mux/env.d.ts +21 -0
- package/dist/providers/mux/env.js +61 -0
- package/dist/providers/mux/env.js.map +1 -0
- package/dist/providers/mux/mapping.d.ts +38 -0
- package/dist/providers/mux/mapping.js +111 -0
- package/dist/providers/mux/mapping.js.map +1 -0
- package/dist/providers/mux/options.d.ts +96 -0
- package/dist/providers/mux/options.js +83 -0
- package/dist/providers/mux/options.js.map +1 -0
- package/dist/providers/mux/provider.d.ts +63 -0
- package/dist/providers/mux/provider.js +555 -0
- package/dist/providers/mux/provider.js.map +1 -0
- package/dist/providers/mux/signing.d.ts +45 -0
- package/dist/providers/mux/signing.js +84 -0
- package/dist/providers/mux/signing.js.map +1 -0
- package/dist/providers/mux/types.d.ts +173 -0
- package/dist/providers/mux/types.js +42 -0
- package/dist/providers/mux/types.js.map +1 -0
- package/dist/providers/mux/urls.d.ts +58 -0
- package/dist/providers/mux/urls.js +59 -0
- package/dist/providers/mux/urls.js.map +1 -0
- package/dist/providers/mux/webhook.d.ts +38 -0
- package/dist/providers/mux/webhook.js +115 -0
- package/dist/providers/mux/webhook.js.map +1 -0
- package/dist/service.d.ts +193 -0
- package/dist/service.js +967 -0
- package/dist/service.js.map +1 -0
- package/dist/testing/bunny.d.ts +113 -0
- package/dist/testing/bunny.js +776 -0
- package/dist/testing/bunny.js.map +1 -0
- package/dist/testing/cloudflare.d.ts +101 -0
- package/dist/testing/cloudflare.js +913 -0
- package/dist/testing/cloudflare.js.map +1 -0
- package/dist/testing/index.d.ts +45 -0
- package/dist/testing/index.js +42 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/mux.d.ts +139 -0
- package/dist/testing/mux.js +1053 -0
- package/dist/testing/mux.js.map +1 -0
- package/dist/testing/types.d.ts +90 -0
- package/dist/testing/types.js +9 -0
- package/dist/testing/types.js.map +1 -0
- package/dist/types.d.ts +404 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/access.d.ts +22 -0
- package/dist/utils/access.js +66 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/localReq.d.ts +11 -0
- package/dist/utils/localReq.js +23 -0
- package/dist/utils/localReq.js.map +1 -0
- package/dist/utils/request.d.ts +12 -0
- package/dist/utils/request.js +55 -0
- package/dist/utils/request.js.map +1 -0
- package/dist/utils/version.d.ts +5 -0
- package/dist/utils/version.js +7 -0
- package/dist/utils/version.js.map +1 -0
- package/package.json +175 -4
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { BUNNY_API_BASE_URL } from './client.js';
|
|
2
|
+
import { BUNNY_EMBED_BASE_URL } from './urls.js';
|
|
3
|
+
const fail = (message)=>{
|
|
4
|
+
throw new Error(`[payload-video-streaming] bunnyStream: ${message}`);
|
|
5
|
+
};
|
|
6
|
+
/** `vz-xxx.b-cdn.net` or a full URL → base URL without a trailing slash. */ export const cdnBaseUrl = (hostname)=>{
|
|
7
|
+
const value = hostname.trim().replace(/\/+$/, '');
|
|
8
|
+
return /^https?:\/\//i.test(value) ? value : `https://${value}`;
|
|
9
|
+
};
|
|
10
|
+
/** Validates Bunny options and fills defaults. Throws descriptive errors for misconfiguration. */ export const resolveBunnyOptions = (options)=>{
|
|
11
|
+
if (!options || typeof options !== 'object') {
|
|
12
|
+
fail('options are required');
|
|
13
|
+
}
|
|
14
|
+
const libraryId = String(options.libraryId ?? '').trim();
|
|
15
|
+
if (!libraryId) {
|
|
16
|
+
fail('`libraryId` is required (your Stream video library id).');
|
|
17
|
+
}
|
|
18
|
+
if (!/^\d+$/.test(libraryId)) {
|
|
19
|
+
fail('`libraryId` must be the numeric id of your video library.');
|
|
20
|
+
}
|
|
21
|
+
if (!options.apiKey) {
|
|
22
|
+
fail("`apiKey` is required (the video library's API key).");
|
|
23
|
+
}
|
|
24
|
+
if (!options.cdnHostname) {
|
|
25
|
+
fail("`cdnHostname` is required (the library's CDN hostname, e.g. vz-12345678-abc.b-cdn.net).");
|
|
26
|
+
}
|
|
27
|
+
const urls = {
|
|
28
|
+
apiBaseUrl: (options.apiBaseUrl || BUNNY_API_BASE_URL).replace(/\/+$/, ''),
|
|
29
|
+
cdnHostname: cdnBaseUrl(options.cdnHostname),
|
|
30
|
+
embedBaseUrl: (options.embedBaseUrl || BUNNY_EMBED_BASE_URL).replace(/\/+$/, '')
|
|
31
|
+
};
|
|
32
|
+
if (options.upload?.tusEndpoint) {
|
|
33
|
+
urls['upload.tusEndpoint'] = options.upload.tusEndpoint;
|
|
34
|
+
}
|
|
35
|
+
for (const [key, value] of Object.entries(urls)){
|
|
36
|
+
try {
|
|
37
|
+
new URL(value);
|
|
38
|
+
} catch {
|
|
39
|
+
fail(`\`${key}\` must be a hostname or an absolute URL`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const ttl = options.upload?.tusSignatureTtlSeconds ?? 86_400;
|
|
43
|
+
if (!(ttl >= 3600)) {
|
|
44
|
+
fail('`upload.tusSignatureTtlSeconds` must be at least 3600 (Bunny requires an hour or more)');
|
|
45
|
+
}
|
|
46
|
+
const chunk = options.upload?.tusChunkSize;
|
|
47
|
+
if (chunk !== undefined && !(chunk > 0)) {
|
|
48
|
+
fail('`upload.tusChunkSize` must be a positive number of bytes');
|
|
49
|
+
}
|
|
50
|
+
const tolerance = options.webhook?.toleranceSeconds ?? 21_600;
|
|
51
|
+
if (!(tolerance >= 0)) {
|
|
52
|
+
fail('`webhook.toleranceSeconds` must be 0 or more');
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
apiBaseUrl: urls.apiBaseUrl,
|
|
56
|
+
apiKey: options.apiKey,
|
|
57
|
+
cdnBaseUrl: urls.cdnHostname,
|
|
58
|
+
cdnTokenKey: options.cdnTokenKey || undefined,
|
|
59
|
+
collectionId: options.collectionId || undefined,
|
|
60
|
+
embedBaseUrl: urls.embedBaseUrl,
|
|
61
|
+
embedTokenKey: options.embedTokenKey || undefined,
|
|
62
|
+
fetch: options.fetch,
|
|
63
|
+
id: options.id ?? 'bunny',
|
|
64
|
+
label: options.label ?? 'Bunny Stream',
|
|
65
|
+
libraryId,
|
|
66
|
+
playback: {
|
|
67
|
+
iframe: {
|
|
68
|
+
autoplay: false,
|
|
69
|
+
...options.playback?.iframe
|
|
70
|
+
},
|
|
71
|
+
tokenCountries: options.playback?.tokenCountries || undefined
|
|
72
|
+
},
|
|
73
|
+
upload: {
|
|
74
|
+
tusChunkSize: chunk,
|
|
75
|
+
tusEndpoint: options.upload?.tusEndpoint || `${urls.apiBaseUrl}/tusupload`,
|
|
76
|
+
tusSignatureTtlSeconds: ttl
|
|
77
|
+
},
|
|
78
|
+
webhook: {
|
|
79
|
+
secret: options.webhook?.secret || options.webhookSecret || undefined,
|
|
80
|
+
toleranceSeconds: tolerance
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/providers/bunny/options.ts"],"sourcesContent":["import type { BunnyIframeOptions } from './urls.js'\n\nimport { BUNNY_API_BASE_URL } from './client.js'\nimport { BUNNY_EMBED_BASE_URL } from './urls.js'\n\nexport type BunnyStreamOptions = {\n /** Defaults to https://video.bunnycdn.com. Override for tests or proxies. */\n apiBaseUrl?: string\n /** The video library's API key (Stream → your library → API). Never sent to browsers. */\n apiKey: string\n /**\n * The library's CDN hostname (Stream → your library → API → \"CDN Hostname\"), e.g.\n * `vz-12345678-abc.b-cdn.net`, or a full base URL (`https://video.example.com`) for a custom hostname.\n */\n cdnHostname: string\n /**\n * CDN token authentication key of the library's pull zone. Set it exactly when token authentication\n * is enabled on that pull zone: HLS, thumbnails, previews and MP4 files then get signed URLs.\n */\n cdnTokenKey?: string\n /** Put new videos in this collection (its GUID). */\n collectionId?: string\n /** Default `https://player.mediadelivery.net`. Override for tests. */\n embedBaseUrl?: string\n /**\n * The library's embed view token authentication key (Stream → your library → Security). Set it\n * exactly when \"Embed view token authentication\" is enabled: the iframe player then gets signed URLs.\n */\n embedTokenKey?: string\n /** Custom fetch (mainly for tests). */\n fetch?: typeof fetch\n /** Provider id stored on documents. Default `bunny`. Set it when you configure two libraries. */\n id?: string\n /** Admin label. Default `Bunny Stream`. */\n label?: string\n /** The video library id (a number). */\n libraryId: number | string\n /** Playback defaults. */\n playback?: {\n /** Default iframe player options. `autoplay` defaults to false (Bunny's player autoplays otherwise). */\n iframe?: BunnyIframeOptions\n /** Lock signed CDN URLs to these countries (comma-separated ISO codes). */\n tokenCountries?: string\n }\n /** Upload defaults. */\n upload?: {\n /** tus chunk size in bytes. Default: none (one request per upload, resumable on failure). */\n tusChunkSize?: number\n /** Default `${apiBaseUrl}/tusupload`. */\n tusEndpoint?: string\n /** How long the browser's upload signature stays valid, in seconds. Default 86400 (24h), min 3600. */\n tusSignatureTtlSeconds?: number\n }\n /** Webhook verification. Without a secret the provider's webhook endpoint is disabled. */\n webhook?: {\n /**\n * The library's Read-Only API key, which Bunny uses to sign webhooks (or a separate webhook signing\n * secret, if your library has one).\n */\n secret?: string\n /** Reject notifications whose `Timestamp` is older than this many seconds. Default 21600 (6h, longer than Bunny's retries). 0 disables. */\n toleranceSeconds?: number\n }\n /** Shorthand for `webhook.secret`. */\n webhookSecret?: string\n}\n\nexport type ResolvedBunnyStreamOptions = {\n apiBaseUrl: string\n apiKey: string\n cdnBaseUrl: string\n cdnTokenKey?: string\n collectionId?: string\n embedBaseUrl: string\n embedTokenKey?: string\n fetch?: typeof fetch\n id: string\n label: string\n libraryId: string\n playback: { iframe: BunnyIframeOptions; tokenCountries?: string }\n upload: { tusChunkSize?: number; tusEndpoint: string; tusSignatureTtlSeconds: number }\n webhook: { secret?: string; toleranceSeconds: number }\n}\n\nconst fail = (message: string): never => {\n throw new Error(`[payload-video-streaming] bunnyStream: ${message}`)\n}\n\n/** `vz-xxx.b-cdn.net` or a full URL → base URL without a trailing slash. */\nexport const cdnBaseUrl = (hostname: string): string => {\n const value = hostname.trim().replace(/\\/+$/, '')\n return /^https?:\\/\\//i.test(value) ? value : `https://${value}`\n}\n\n/** Validates Bunny options and fills defaults. Throws descriptive errors for misconfiguration. */\nexport const resolveBunnyOptions = (options: BunnyStreamOptions): ResolvedBunnyStreamOptions => {\n if (!options || typeof options !== 'object') {\n fail('options are required')\n }\n const libraryId = String(options.libraryId ?? '').trim()\n if (!libraryId) {\n fail('`libraryId` is required (your Stream video library id).')\n }\n if (!/^\\d+$/.test(libraryId)) {\n fail('`libraryId` must be the numeric id of your video library.')\n }\n if (!options.apiKey) {\n fail(\"`apiKey` is required (the video library's API key).\")\n }\n if (!options.cdnHostname) {\n fail(\"`cdnHostname` is required (the library's CDN hostname, e.g. vz-12345678-abc.b-cdn.net).\")\n }\n const urls: Record<string, string> = {\n apiBaseUrl: (options.apiBaseUrl || BUNNY_API_BASE_URL).replace(/\\/+$/, ''),\n cdnHostname: cdnBaseUrl(options.cdnHostname),\n embedBaseUrl: (options.embedBaseUrl || BUNNY_EMBED_BASE_URL).replace(/\\/+$/, ''),\n }\n if (options.upload?.tusEndpoint) {\n urls['upload.tusEndpoint'] = options.upload.tusEndpoint\n }\n for (const [key, value] of Object.entries(urls)) {\n try {\n new URL(value)\n } catch {\n fail(`\\`${key}\\` must be a hostname or an absolute URL`)\n }\n }\n const ttl = options.upload?.tusSignatureTtlSeconds ?? 86_400\n if (!(ttl >= 3600)) {\n fail('`upload.tusSignatureTtlSeconds` must be at least 3600 (Bunny requires an hour or more)')\n }\n const chunk = options.upload?.tusChunkSize\n if (chunk !== undefined && !(chunk > 0)) {\n fail('`upload.tusChunkSize` must be a positive number of bytes')\n }\n const tolerance = options.webhook?.toleranceSeconds ?? 21_600\n if (!(tolerance >= 0)) {\n fail('`webhook.toleranceSeconds` must be 0 or more')\n }\n return {\n apiBaseUrl: urls.apiBaseUrl!,\n apiKey: options.apiKey,\n cdnBaseUrl: urls.cdnHostname!,\n cdnTokenKey: options.cdnTokenKey || undefined,\n collectionId: options.collectionId || undefined,\n embedBaseUrl: urls.embedBaseUrl!,\n embedTokenKey: options.embedTokenKey || undefined,\n fetch: options.fetch,\n id: options.id ?? 'bunny',\n label: options.label ?? 'Bunny Stream',\n libraryId,\n playback: { iframe: { autoplay: false, ...options.playback?.iframe }, tokenCountries: options.playback?.tokenCountries || undefined },\n upload: {\n tusChunkSize: chunk,\n tusEndpoint: options.upload?.tusEndpoint || `${urls.apiBaseUrl}/tusupload`,\n tusSignatureTtlSeconds: ttl,\n },\n webhook: { secret: options.webhook?.secret || options.webhookSecret || undefined, toleranceSeconds: tolerance },\n }\n}\n"],"names":["BUNNY_API_BASE_URL","BUNNY_EMBED_BASE_URL","fail","message","Error","cdnBaseUrl","hostname","value","trim","replace","test","resolveBunnyOptions","options","libraryId","String","apiKey","cdnHostname","urls","apiBaseUrl","embedBaseUrl","upload","tusEndpoint","key","Object","entries","URL","ttl","tusSignatureTtlSeconds","chunk","tusChunkSize","undefined","tolerance","webhook","toleranceSeconds","cdnTokenKey","collectionId","embedTokenKey","fetch","id","label","playback","iframe","autoplay","tokenCountries","secret","webhookSecret"],"mappings":"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"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { AssetPatch, DirectUpload, ImportInput, NeutralAsset, PlaybackOptions, PlaybackResult, ProviderCapabilities, ProviderRef, ProviderWebhook, WebhookSetup, ServerUploadInput, StoredAsset, UploadInput, VideoProvider } from '../../core/provider.js';
|
|
2
|
+
import type { BunnyStreamOptions, ResolvedBunnyStreamOptions } from './options.js';
|
|
3
|
+
import { BunnyStreamClient } from './client.js';
|
|
4
|
+
export declare const BUNNY_CAPABILITIES: ProviderCapabilities;
|
|
5
|
+
/** Bunny Stream as a video provider. Create it with `bunnyStream({ … })`. */
|
|
6
|
+
export declare class BunnyStreamProvider implements VideoProvider {
|
|
7
|
+
readonly capabilities: ProviderCapabilities;
|
|
8
|
+
readonly captions: NonNullable<VideoProvider['captions']>;
|
|
9
|
+
readonly client: BunnyStreamClient;
|
|
10
|
+
readonly id: string;
|
|
11
|
+
readonly label: string;
|
|
12
|
+
readonly options: ResolvedBunnyStreamOptions;
|
|
13
|
+
readonly webhook?: ProviderWebhook;
|
|
14
|
+
readonly webhookSetup: WebhookSetup;
|
|
15
|
+
/** Transcriptions requested from this process, by video and language, until Bunny lists the track. */
|
|
16
|
+
private readonly pendingCaptions;
|
|
17
|
+
constructor(options: BunnyStreamOptions);
|
|
18
|
+
/** True when the library uses token authentication, i.e. a token key is configured. */
|
|
19
|
+
get signsPlayback(): boolean;
|
|
20
|
+
private asset;
|
|
21
|
+
/** Creates a video, then stores meta tags (plugin `meta` and the creator) when there are any. */
|
|
22
|
+
private createVideo;
|
|
23
|
+
/**
|
|
24
|
+
* Creates the video, then signs a tus upload the browser sends straight to Bunny
|
|
25
|
+
* (`AuthorizationSignature` = SHA256(libraryId + apiKey + expires + videoId)). The API key itself
|
|
26
|
+
* never leaves the server.
|
|
27
|
+
*/
|
|
28
|
+
createDirectUpload(input: UploadInput): Promise<DirectUpload>;
|
|
29
|
+
/**
|
|
30
|
+
* Has Bunny fetch the video. Bunny documents no video id in the response, so when it doesn't send
|
|
31
|
+
* one the video is found by a unique temporary title, which is then replaced by the real one.
|
|
32
|
+
*/
|
|
33
|
+
importFromUrl(input: ImportInput): Promise<NeutralAsset>;
|
|
34
|
+
uploadFile(input: ServerUploadInput): Promise<NeutralAsset>;
|
|
35
|
+
getAsset(ref: ProviderRef): Promise<NeutralAsset | null>;
|
|
36
|
+
/** Pages through the library, newest first; the cursor is the next page number. */
|
|
37
|
+
listAssets({ creator, cursor }?: {
|
|
38
|
+
creator?: string;
|
|
39
|
+
cursor?: string;
|
|
40
|
+
}): Promise<{
|
|
41
|
+
assets: NeutralAsset[];
|
|
42
|
+
next?: string;
|
|
43
|
+
}>;
|
|
44
|
+
/** Pushes the title and meta tags. Signing, origins and the poster are library or creation settings. */
|
|
45
|
+
updateAsset(ref: ProviderRef, patch: AssetPatch): Promise<NeutralAsset | null>;
|
|
46
|
+
deleteAsset(ref: ProviderRef): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Playback URLs. Token authentication is library-wide, so whether URLs are signed follows the
|
|
49
|
+
* configured keys (not the document): the embed URL gets a view token (`embedTokenKey`), the CDN
|
|
50
|
+
* files a directory token (`cdnTokenKey`) that also covers HLS renditions and segments.
|
|
51
|
+
*/
|
|
52
|
+
playback(asset: StoredAsset, options: PlaybackOptions): Promise<PlaybackResult>;
|
|
53
|
+
private listCaptions;
|
|
54
|
+
/** Starts AI transcription (Bunny auto-detects the spoken language). The track appears as `<lang>-auto`. */
|
|
55
|
+
private generateCaption;
|
|
56
|
+
/**
|
|
57
|
+
* Verifies a notification and asks the core to re-fetch the video. Bunny's notifications carry only
|
|
58
|
+
* a status code, so the current state is read from the API: that is robust to retries and to events
|
|
59
|
+
* arriving out of order. Duplicates are dropped by `EventId`; very old events are rejected.
|
|
60
|
+
*/
|
|
61
|
+
private handleWebhook;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Bunny Stream as a video provider:
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* videoPlugin({ providers: [bunnyStream({ libraryId, apiKey, cdnHostname })] })
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare const bunnyStream: (options: BunnyStreamOptions) => BunnyStreamProvider;
|
|
@@ -0,0 +1,480 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { BunnyStreamClient, BunnyStreamError } from './client.js';
|
|
3
|
+
import { captionToNeutral, metaTag, mp4FallbackFile, toMetaTags, videoToAsset } from './mapping.js';
|
|
4
|
+
import { resolveBunnyOptions } from './options.js';
|
|
5
|
+
import { cdnToken, tusUploadSignature } from './signing.js';
|
|
6
|
+
import { cdnFileUrl, embedUrl, hlsUrl, signedEmbedUrl, videoFileSigner } from './urls.js';
|
|
7
|
+
import { isBunnyWebhookPayload, parseBunnyDate, verifyBunnyWebhook } from './webhook.js';
|
|
8
|
+
export const BUNNY_CAPABILITIES = {
|
|
9
|
+
adoptExisting: true,
|
|
10
|
+
// Embedding is restricted per library (allowed referrers), not per video.
|
|
11
|
+
allowedOrigins: false,
|
|
12
|
+
captions: {
|
|
13
|
+
generate: true,
|
|
14
|
+
upload: true
|
|
15
|
+
},
|
|
16
|
+
dash: false,
|
|
17
|
+
// MP4 fallback is a library setting (EnableMP4Fallback) that applies to new uploads.
|
|
18
|
+
downloads: {
|
|
19
|
+
managed: 'library',
|
|
20
|
+
types: [
|
|
21
|
+
'default'
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
importFromUrl: true,
|
|
25
|
+
progress: true,
|
|
26
|
+
serverUpload: true,
|
|
27
|
+
// Token authentication (embed and CDN) is a library setting.
|
|
28
|
+
signedPlayback: 'library',
|
|
29
|
+
// The poster position is applied when a video is created; Bunny has no API to move it afterwards.
|
|
30
|
+
thumbnailTime: false,
|
|
31
|
+
uploadProtocols: [
|
|
32
|
+
'tus'
|
|
33
|
+
],
|
|
34
|
+
webhooks: true
|
|
35
|
+
};
|
|
36
|
+
const LIST_PAGE_SIZE = 1000;
|
|
37
|
+
/** How long a requested transcription is listed as `processing` before Bunny reports the track. */ const PENDING_CAPTION_TTL_MS = 60 * 60 * 1000;
|
|
38
|
+
const requireGuid = (ref)=>{
|
|
39
|
+
if (!ref.assetId) {
|
|
40
|
+
throw new Error('[payload-video-streaming] bunny: this video has no Bunny Stream video id');
|
|
41
|
+
}
|
|
42
|
+
return ref.assetId;
|
|
43
|
+
};
|
|
44
|
+
/** Bunny Stream as a video provider. Create it with `bunnyStream({ … })`. */ export class BunnyStreamProvider {
|
|
45
|
+
capabilities = BUNNY_CAPABILITIES;
|
|
46
|
+
captions;
|
|
47
|
+
client;
|
|
48
|
+
id;
|
|
49
|
+
label;
|
|
50
|
+
options;
|
|
51
|
+
webhook;
|
|
52
|
+
webhookSetup = {
|
|
53
|
+
secret: 'BUNNY_STREAM_WEBHOOK_SECRET (`webhook.secret`)',
|
|
54
|
+
secretLocation: "the video library's Read-Only API key (Stream → your library → API), which Bunny signs webhooks with",
|
|
55
|
+
steps: [
|
|
56
|
+
'In the bunny.net dashboard, open Stream → your library → API and set the Webhook URL to {url}.',
|
|
57
|
+
"Set the library's Read-Only API key (same page) as BUNNY_STREAM_WEBHOOK_SECRET (or `webhook.secret`)."
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
/** Transcriptions requested from this process, by video and language, until Bunny lists the track. */ pendingCaptions = new Map();
|
|
61
|
+
constructor(options){
|
|
62
|
+
this.options = resolveBunnyOptions(options);
|
|
63
|
+
this.id = this.options.id;
|
|
64
|
+
this.label = this.options.label;
|
|
65
|
+
this.client = new BunnyStreamClient({
|
|
66
|
+
apiBaseUrl: this.options.apiBaseUrl,
|
|
67
|
+
apiKey: this.options.apiKey,
|
|
68
|
+
fetch: this.options.fetch,
|
|
69
|
+
libraryId: this.options.libraryId
|
|
70
|
+
});
|
|
71
|
+
this.captions = {
|
|
72
|
+
generate: (ref, language)=>this.generateCaption(requireGuid(ref), language),
|
|
73
|
+
list: (ref)=>this.listCaptions(requireGuid(ref)),
|
|
74
|
+
remove: async (ref, language)=>{
|
|
75
|
+
const guid = requireGuid(ref);
|
|
76
|
+
this.pendingCaptions.get(guid)?.delete(language.replace(/-auto$/i, ''));
|
|
77
|
+
await this.client.deleteCaption(guid, language);
|
|
78
|
+
},
|
|
79
|
+
upload: async (ref, language, vtt)=>{
|
|
80
|
+
await this.client.addCaption(requireGuid(ref), language, vtt);
|
|
81
|
+
return {
|
|
82
|
+
generated: false,
|
|
83
|
+
label: language,
|
|
84
|
+
language,
|
|
85
|
+
status: 'ready'
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
if (this.options.webhook.secret) {
|
|
90
|
+
this.webhook = {
|
|
91
|
+
handle: (input)=>this.handleWebhook(input)
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** True when the library uses token authentication, i.e. a token key is configured. */ get signsPlayback() {
|
|
96
|
+
return Boolean(this.options.embedTokenKey || this.options.cdnTokenKey);
|
|
97
|
+
}
|
|
98
|
+
asset(video) {
|
|
99
|
+
return videoToAsset(video, {
|
|
100
|
+
signedPlayback: this.signsPlayback
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Uploads
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
/** Creates a video, then stores meta tags (plugin `meta` and the creator) when there are any. */ async createVideo(input) {
|
|
107
|
+
const video = await this.client.createVideo({
|
|
108
|
+
...this.options.collectionId ? {
|
|
109
|
+
collectionId: this.options.collectionId
|
|
110
|
+
} : {},
|
|
111
|
+
...typeof input.thumbnailTime === 'number' ? {
|
|
112
|
+
thumbnailTime: Math.round(input.thumbnailTime * 1000)
|
|
113
|
+
} : {},
|
|
114
|
+
title: input.title
|
|
115
|
+
});
|
|
116
|
+
const metaTags = toMetaTags(input.meta, input.creator);
|
|
117
|
+
if (metaTags.length) {
|
|
118
|
+
try {
|
|
119
|
+
await this.client.updateVideo(video.guid, {
|
|
120
|
+
metaTags
|
|
121
|
+
});
|
|
122
|
+
} catch (err) {
|
|
123
|
+
await this.client.deleteVideo(video.guid).catch(()=>undefined);
|
|
124
|
+
throw err;
|
|
125
|
+
}
|
|
126
|
+
video.metaTags = metaTags;
|
|
127
|
+
}
|
|
128
|
+
return video;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates the video, then signs a tus upload the browser sends straight to Bunny
|
|
132
|
+
* (`AuthorizationSignature` = SHA256(libraryId + apiKey + expires + videoId)). The API key itself
|
|
133
|
+
* never leaves the server.
|
|
134
|
+
*/ async createDirectUpload(input) {
|
|
135
|
+
const video = await this.createVideo(input);
|
|
136
|
+
const expires = Math.floor(Date.now() / 1000) + this.options.upload.tusSignatureTtlSeconds;
|
|
137
|
+
const { libraryId } = this.options;
|
|
138
|
+
return {
|
|
139
|
+
...this.options.upload.tusChunkSize ? {
|
|
140
|
+
chunkSize: this.options.upload.tusChunkSize
|
|
141
|
+
} : {},
|
|
142
|
+
endpoint: this.options.upload.tusEndpoint,
|
|
143
|
+
headers: {
|
|
144
|
+
AuthorizationExpire: String(expires),
|
|
145
|
+
AuthorizationSignature: tusUploadSignature({
|
|
146
|
+
apiKey: this.options.apiKey,
|
|
147
|
+
expires,
|
|
148
|
+
libraryId,
|
|
149
|
+
videoId: video.guid
|
|
150
|
+
}),
|
|
151
|
+
LibraryId: libraryId,
|
|
152
|
+
VideoId: video.guid
|
|
153
|
+
},
|
|
154
|
+
metadata: {
|
|
155
|
+
title: input.title,
|
|
156
|
+
...this.options.collectionId ? {
|
|
157
|
+
collection: this.options.collectionId
|
|
158
|
+
} : {},
|
|
159
|
+
...typeof input.thumbnailTime === 'number' ? {
|
|
160
|
+
thumbnailTime: String(Math.round(input.thumbnailTime * 1000))
|
|
161
|
+
} : {}
|
|
162
|
+
},
|
|
163
|
+
protocol: 'tus',
|
|
164
|
+
ref: {
|
|
165
|
+
assetId: video.guid
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Has Bunny fetch the video. Bunny documents no video id in the response, so when it doesn't send
|
|
171
|
+
* one the video is found by a unique temporary title, which is then replaced by the real one.
|
|
172
|
+
*/ async importFromUrl(input) {
|
|
173
|
+
const marker = `pvs-import-${randomBytes(8).toString('hex')}`;
|
|
174
|
+
const query = {
|
|
175
|
+
...this.options.collectionId ? {
|
|
176
|
+
collectionId: this.options.collectionId
|
|
177
|
+
} : {},
|
|
178
|
+
...typeof input.thumbnailTime === 'number' ? {
|
|
179
|
+
thumbnailTime: Math.round(input.thumbnailTime * 1000)
|
|
180
|
+
} : {}
|
|
181
|
+
};
|
|
182
|
+
const result = await this.client.fetchVideo({
|
|
183
|
+
title: marker,
|
|
184
|
+
url: input.url
|
|
185
|
+
}, query);
|
|
186
|
+
let guid = result.guid;
|
|
187
|
+
if (!guid) {
|
|
188
|
+
const found = await this.client.listVideos({
|
|
189
|
+
itemsPerPage: 100,
|
|
190
|
+
orderBy: 'date',
|
|
191
|
+
page: 1,
|
|
192
|
+
search: marker
|
|
193
|
+
});
|
|
194
|
+
guid = (found.items ?? []).find((v)=>v.title === marker)?.guid;
|
|
195
|
+
}
|
|
196
|
+
if (!guid) {
|
|
197
|
+
throw new BunnyStreamError('Bunny Stream accepted the import, but the new video could not be found', 502);
|
|
198
|
+
}
|
|
199
|
+
await this.client.updateVideo(guid, {
|
|
200
|
+
metaTags: toMetaTags(input.meta, input.creator),
|
|
201
|
+
title: input.title
|
|
202
|
+
}).catch(async (err)=>{
|
|
203
|
+
await this.client.deleteVideo(guid).catch(()=>undefined);
|
|
204
|
+
throw err;
|
|
205
|
+
});
|
|
206
|
+
const asset = this.asset(await this.client.getVideo(guid));
|
|
207
|
+
// Bunny is fetching the file, so the video isn't waiting for an upload from us.
|
|
208
|
+
return asset.status === 'waiting' ? {
|
|
209
|
+
...asset,
|
|
210
|
+
status: 'queued'
|
|
211
|
+
} : asset;
|
|
212
|
+
}
|
|
213
|
+
async uploadFile(input) {
|
|
214
|
+
const video = await this.createVideo(input);
|
|
215
|
+
try {
|
|
216
|
+
await this.client.uploadVideo(video.guid, input.file);
|
|
217
|
+
} catch (err) {
|
|
218
|
+
await this.client.deleteVideo(video.guid).catch(()=>undefined);
|
|
219
|
+
throw err;
|
|
220
|
+
}
|
|
221
|
+
return this.asset(await this.client.getVideo(video.guid));
|
|
222
|
+
}
|
|
223
|
+
// ---------------------------------------------------------------------------
|
|
224
|
+
// Assets
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
async getAsset(ref) {
|
|
227
|
+
try {
|
|
228
|
+
return this.asset(await this.client.getVideo(requireGuid(ref)));
|
|
229
|
+
} catch (err) {
|
|
230
|
+
if (err instanceof BunnyStreamError && err.isNotFound) {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
throw err;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/** Pages through the library, newest first; the cursor is the next page number. */ async listAssets({ creator, cursor } = {}) {
|
|
237
|
+
const page = Math.max(1, Number.parseInt(cursor ?? '1', 10) || 1);
|
|
238
|
+
const list = await this.client.listVideos({
|
|
239
|
+
itemsPerPage: LIST_PAGE_SIZE,
|
|
240
|
+
orderBy: 'date',
|
|
241
|
+
page,
|
|
242
|
+
...this.options.collectionId ? {
|
|
243
|
+
collection: this.options.collectionId
|
|
244
|
+
} : {}
|
|
245
|
+
});
|
|
246
|
+
const items = list.items ?? [];
|
|
247
|
+
const perPage = list.itemsPerPage || LIST_PAGE_SIZE;
|
|
248
|
+
const total = list.totalItems ?? 0;
|
|
249
|
+
const next = items.length > 0 && page * perPage < total ? String(page + 1) : undefined;
|
|
250
|
+
const assets = items.filter((v)=>!creator || metaTag(v.metaTags, 'creator') === creator).map((v)=>this.asset(v));
|
|
251
|
+
return {
|
|
252
|
+
assets,
|
|
253
|
+
next
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** Pushes the title and meta tags. Signing, origins and the poster are library or creation settings. */ async updateAsset(ref, patch) {
|
|
257
|
+
if (patch.title === undefined && patch.meta === undefined) {
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
const guid = requireGuid(ref);
|
|
261
|
+
const current = await this.client.getVideo(guid);
|
|
262
|
+
await this.client.updateVideo(guid, {
|
|
263
|
+
...patch.meta !== undefined ? {
|
|
264
|
+
metaTags: toMetaTags(patch.meta, metaTag(current.metaTags, 'creator') ?? undefined)
|
|
265
|
+
} : {},
|
|
266
|
+
...patch.title ? {
|
|
267
|
+
title: patch.title
|
|
268
|
+
} : {}
|
|
269
|
+
});
|
|
270
|
+
return this.asset(await this.client.getVideo(guid));
|
|
271
|
+
}
|
|
272
|
+
async deleteAsset(ref) {
|
|
273
|
+
await this.client.deleteVideo(requireGuid(ref));
|
|
274
|
+
}
|
|
275
|
+
// ---------------------------------------------------------------------------
|
|
276
|
+
// Playback
|
|
277
|
+
// ---------------------------------------------------------------------------
|
|
278
|
+
/**
|
|
279
|
+
* Playback URLs. Token authentication is library-wide, so whether URLs are signed follows the
|
|
280
|
+
* configured keys (not the document): the embed URL gets a view token (`embedTokenKey`), the CDN
|
|
281
|
+
* files a directory token (`cdnTokenKey`) that also covers HLS renditions and segments.
|
|
282
|
+
*/ async playback(asset, options) {
|
|
283
|
+
const { cdnBaseUrl, cdnTokenKey, embedBaseUrl, embedTokenKey, libraryId, playback } = this.options;
|
|
284
|
+
const guid = asset.assetId;
|
|
285
|
+
const signed = this.signsPlayback;
|
|
286
|
+
if (signed && !options.sign) {
|
|
287
|
+
return {
|
|
288
|
+
signed: false,
|
|
289
|
+
urls: null
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
const video = asset.providerData ?? null;
|
|
293
|
+
const thumbnailFile = video?.thumbnailFileName || 'thumbnail.jpg';
|
|
294
|
+
const mp4File = video?.hasMP4Fallback ? mp4FallbackFile(video) : undefined;
|
|
295
|
+
const expires = Math.floor(Date.now() / 1000) + options.expiresIn;
|
|
296
|
+
const iframeOptions = {
|
|
297
|
+
...playback.iframe,
|
|
298
|
+
...options.providerOptions?.iframe
|
|
299
|
+
};
|
|
300
|
+
const iframe = embedTokenKey ? signedEmbedUrl({
|
|
301
|
+
embedBaseUrl,
|
|
302
|
+
expires,
|
|
303
|
+
libraryId,
|
|
304
|
+
tokenKey: embedTokenKey,
|
|
305
|
+
videoId: guid
|
|
306
|
+
}, iframeOptions) : embedUrl(embedBaseUrl, libraryId, guid, iframeOptions);
|
|
307
|
+
let urls;
|
|
308
|
+
let token;
|
|
309
|
+
if (cdnTokenKey) {
|
|
310
|
+
const sign = videoFileSigner(cdnBaseUrl, guid, {
|
|
311
|
+
countries: playback.tokenCountries,
|
|
312
|
+
expires,
|
|
313
|
+
key: cdnTokenKey
|
|
314
|
+
});
|
|
315
|
+
urls = {
|
|
316
|
+
animated: sign('preview.webp'),
|
|
317
|
+
hls: sign('playlist.m3u8', {
|
|
318
|
+
directory: true
|
|
319
|
+
}),
|
|
320
|
+
iframe,
|
|
321
|
+
thumbnail: sign(thumbnailFile),
|
|
322
|
+
...mp4File ? {
|
|
323
|
+
downloadMp4: sign(mp4File)
|
|
324
|
+
} : {}
|
|
325
|
+
};
|
|
326
|
+
token = cdnToken(cdnTokenKey, '', {
|
|
327
|
+
countriesAllowed: playback.tokenCountries,
|
|
328
|
+
expires,
|
|
329
|
+
tokenPath: new URL(cdnFileUrl(cdnBaseUrl, guid, '')).pathname
|
|
330
|
+
}).token;
|
|
331
|
+
} else {
|
|
332
|
+
urls = {
|
|
333
|
+
animated: cdnFileUrl(cdnBaseUrl, guid, 'preview.webp'),
|
|
334
|
+
hls: hlsUrl(cdnBaseUrl, guid),
|
|
335
|
+
iframe,
|
|
336
|
+
thumbnail: cdnFileUrl(cdnBaseUrl, guid, thumbnailFile),
|
|
337
|
+
...mp4File ? {
|
|
338
|
+
downloadMp4: cdnFileUrl(cdnBaseUrl, guid, mp4File)
|
|
339
|
+
} : {}
|
|
340
|
+
};
|
|
341
|
+
token = embedTokenKey ? new URL(iframe).searchParams.get('token') ?? undefined : undefined;
|
|
342
|
+
}
|
|
343
|
+
return signed ? {
|
|
344
|
+
expiresAt: new Date(expires * 1000).toISOString(),
|
|
345
|
+
signed: true,
|
|
346
|
+
token,
|
|
347
|
+
urls
|
|
348
|
+
} : {
|
|
349
|
+
signed: false,
|
|
350
|
+
urls
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
// ---------------------------------------------------------------------------
|
|
354
|
+
// Captions
|
|
355
|
+
// ---------------------------------------------------------------------------
|
|
356
|
+
async listCaptions(guid) {
|
|
357
|
+
const video = await this.client.getVideo(guid);
|
|
358
|
+
const tracks = (video.captions ?? []).map(captionToNeutral).filter((c)=>c !== null);
|
|
359
|
+
const pending = this.pendingCaptions.get(guid);
|
|
360
|
+
if (pending) {
|
|
361
|
+
const now = Date.now();
|
|
362
|
+
for (const [language, requestedAt] of pending){
|
|
363
|
+
const done = tracks.some((t)=>t.language.replace(/-auto$/i, '') === language && t.generated);
|
|
364
|
+
if (done || now - requestedAt > PENDING_CAPTION_TTL_MS) {
|
|
365
|
+
pending.delete(language);
|
|
366
|
+
} else {
|
|
367
|
+
tracks.push({
|
|
368
|
+
generated: true,
|
|
369
|
+
label: `${language} (auto-generated)`,
|
|
370
|
+
language: `${language}-auto`,
|
|
371
|
+
status: 'processing'
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (!pending.size) {
|
|
376
|
+
this.pendingCaptions.delete(guid);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return tracks;
|
|
380
|
+
}
|
|
381
|
+
/** Starts AI transcription (Bunny auto-detects the spoken language). The track appears as `<lang>-auto`. */ async generateCaption(guid, language) {
|
|
382
|
+
const target = language.replace(/-auto$/i, '').split('-')[0].toLowerCase();
|
|
383
|
+
if (!/^[a-z]{2}$/.test(target)) {
|
|
384
|
+
// A request error (HTTP 400), not a provider failure.
|
|
385
|
+
throw Object.assign(new Error(`Bunny transcribes into ISO 639-1 languages (two letters, e.g. "en"), not "${language}"`), {
|
|
386
|
+
status: 400
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
await this.client.transcribe(guid, {
|
|
390
|
+
targetLanguages: [
|
|
391
|
+
target
|
|
392
|
+
]
|
|
393
|
+
});
|
|
394
|
+
const pending = this.pendingCaptions.get(guid) ?? new Map();
|
|
395
|
+
pending.set(target, Date.now());
|
|
396
|
+
this.pendingCaptions.set(guid, pending);
|
|
397
|
+
return {
|
|
398
|
+
generated: true,
|
|
399
|
+
label: `${target} (auto-generated)`,
|
|
400
|
+
language: `${target}-auto`,
|
|
401
|
+
status: 'processing'
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
// ---------------------------------------------------------------------------
|
|
405
|
+
// Webhooks
|
|
406
|
+
// ---------------------------------------------------------------------------
|
|
407
|
+
/**
|
|
408
|
+
* Verifies a notification and asks the core to re-fetch the video. Bunny's notifications carry only
|
|
409
|
+
* a status code, so the current state is read from the API: that is robust to retries and to events
|
|
410
|
+
* arriving out of order. Duplicates are dropped by `EventId`; very old events are rejected.
|
|
411
|
+
*/ async handleWebhook({ headers, rawBody }) {
|
|
412
|
+
const verification = verifyBunnyWebhook({
|
|
413
|
+
headers,
|
|
414
|
+
rawBody,
|
|
415
|
+
secret: this.options.webhook.secret ?? ''
|
|
416
|
+
});
|
|
417
|
+
if (!verification.ok) {
|
|
418
|
+
return {
|
|
419
|
+
ok: false,
|
|
420
|
+
reason: `invalid signature (${verification.reason})`,
|
|
421
|
+
status: 401
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
let payload;
|
|
425
|
+
try {
|
|
426
|
+
payload = JSON.parse(rawBody);
|
|
427
|
+
} catch {
|
|
428
|
+
return {
|
|
429
|
+
ok: false,
|
|
430
|
+
reason: 'Invalid JSON body',
|
|
431
|
+
status: 400
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
if (!isBunnyWebhookPayload(payload)) {
|
|
435
|
+
return {
|
|
436
|
+
ok: false,
|
|
437
|
+
reason: 'Webhook payload has no VideoGuid/Status',
|
|
438
|
+
status: 400
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
const event = payload;
|
|
442
|
+
if (event.IsLiveStreamWebhook || String(event.VideoLibraryId) !== this.options.libraryId) {
|
|
443
|
+
return {
|
|
444
|
+
events: [],
|
|
445
|
+
ok: true
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
const tolerance = this.options.webhook.toleranceSeconds;
|
|
449
|
+
const at = parseBunnyDate(event.Timestamp);
|
|
450
|
+
if (tolerance > 0 && at !== null && Date.now() - at > tolerance * 1000) {
|
|
451
|
+
return {
|
|
452
|
+
ok: false,
|
|
453
|
+
reason: 'stale notification',
|
|
454
|
+
status: 400
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
events: [
|
|
459
|
+
{
|
|
460
|
+
...event.EventId ? {
|
|
461
|
+
eventId: String(event.EventId)
|
|
462
|
+
} : {},
|
|
463
|
+
ref: {
|
|
464
|
+
assetId: event.VideoGuid
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
],
|
|
468
|
+
ok: true
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Bunny Stream as a video provider:
|
|
474
|
+
*
|
|
475
|
+
* ```ts
|
|
476
|
+
* videoPlugin({ providers: [bunnyStream({ libraryId, apiKey, cdnHostname })] })
|
|
477
|
+
* ```
|
|
478
|
+
*/ export const bunnyStream = (options)=>new BunnyStreamProvider(options);
|
|
479
|
+
|
|
480
|
+
//# sourceMappingURL=provider.js.map
|