@adonis-agora/media 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +76 -0
- package/README.md +2 -2
- package/dist/providers/media_provider.d.ts.map +1 -1
- package/dist/providers/media_provider.js +113 -0
- package/dist/providers/media_provider.js.map +1 -1
- package/dist/src/content_type.d.ts +6 -1
- package/dist/src/content_type.d.ts.map +1 -1
- package/dist/src/content_type.js +102 -7
- package/dist/src/content_type.js.map +1 -1
- package/dist/src/define_config.d.ts +88 -5
- package/dist/src/define_config.d.ts.map +1 -1
- package/dist/src/define_config.js +7 -2
- package/dist/src/define_config.js.map +1 -1
- package/dist/src/direct_upload.d.ts +177 -0
- package/dist/src/direct_upload.d.ts.map +1 -0
- package/dist/src/direct_upload.js +299 -0
- package/dist/src/direct_upload.js.map +1 -0
- package/dist/src/direct_upload_handler.d.ts +83 -0
- package/dist/src/direct_upload_handler.d.ts.map +1 -0
- package/dist/src/direct_upload_handler.js +180 -0
- package/dist/src/direct_upload_handler.js.map +1 -0
- package/dist/src/disks/factory.d.ts +11 -3
- package/dist/src/disks/factory.d.ts.map +1 -1
- package/dist/src/disks/factory.js +5 -3
- package/dist/src/disks/factory.js.map +1 -1
- package/dist/src/disks/s3.d.ts +10 -0
- package/dist/src/disks/s3.d.ts.map +1 -1
- package/dist/src/disks/s3.js +96 -22
- package/dist/src/disks/s3.js.map +1 -1
- package/dist/src/disks/sigv4.d.ts +58 -0
- package/dist/src/disks/sigv4.d.ts.map +1 -0
- package/dist/src/disks/sigv4.js +87 -0
- package/dist/src/disks/sigv4.js.map +1 -0
- package/dist/src/disks/xml_fallback.d.ts +3 -3
- package/dist/src/disks/xml_fallback.js +3 -3
- package/dist/src/errors.d.ts +104 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +145 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/hls/delivery.d.ts +112 -0
- package/dist/src/hls/delivery.d.ts.map +1 -0
- package/dist/src/hls/delivery.js +118 -0
- package/dist/src/hls/delivery.js.map +1 -0
- package/dist/src/hls/playlist.d.ts +67 -0
- package/dist/src/hls/playlist.d.ts.map +1 -0
- package/dist/src/hls/playlist.js +126 -0
- package/dist/src/hls/playlist.js.map +1 -0
- package/dist/src/index.d.ts +18 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +13 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/media_collection.d.ts +8 -1
- package/dist/src/media_collection.d.ts.map +1 -1
- package/dist/src/media_collection.js +23 -1
- package/dist/src/media_collection.js.map +1 -1
- package/dist/src/media_library.d.ts +32 -2
- package/dist/src/media_library.d.ts.map +1 -1
- package/dist/src/media_library.js +218 -11
- package/dist/src/media_library.js.map +1 -1
- package/dist/src/media_manager.d.ts +37 -2
- package/dist/src/media_manager.d.ts.map +1 -1
- package/dist/src/media_manager.js +54 -1
- package/dist/src/media_manager.js.map +1 -1
- package/dist/src/media_record.d.ts +22 -2
- package/dist/src/media_record.d.ts.map +1 -1
- package/dist/src/testing/fake_transformer.d.ts +32 -0
- package/dist/src/testing/fake_transformer.d.ts.map +1 -0
- package/dist/src/testing/fake_transformer.js +32 -0
- package/dist/src/testing/fake_transformer.js.map +1 -0
- package/dist/src/testing/index.d.ts +2 -0
- package/dist/src/testing/index.d.ts.map +1 -1
- package/dist/src/testing/index.js +1 -0
- package/dist/src/testing/index.js.map +1 -1
- package/dist/src/transformer.d.ts +111 -0
- package/dist/src/transformer.d.ts.map +1 -0
- package/dist/src/transformer.js +2 -0
- package/dist/src/transformer.js.map +1 -0
- package/dist/src/transformers/factory.d.ts +30 -0
- package/dist/src/transformers/factory.d.ts.map +1 -0
- package/dist/src/transformers/factory.js +34 -0
- package/dist/src/transformers/factory.js.map +1 -0
- package/dist/src/transformers/hls.d.ts +110 -0
- package/dist/src/transformers/hls.d.ts.map +1 -0
- package/dist/src/transformers/hls.js +111 -0
- package/dist/src/transformers/hls.js.map +1 -0
- package/dist/src/transformers/mediabunny.d.ts +5 -0
- package/dist/src/transformers/mediabunny.d.ts.map +1 -0
- package/dist/src/transformers/mediabunny.js +150 -0
- package/dist/src/transformers/mediabunny.js.map +1 -0
- package/dist/src/transformers/mediabunny_loader.d.ts +15 -0
- package/dist/src/transformers/mediabunny_loader.d.ts.map +1 -0
- package/dist/src/transformers/mediabunny_loader.js +21 -0
- package/dist/src/transformers/mediabunny_loader.js.map +1 -0
- package/dist/src/transformers/probe.d.ts +56 -0
- package/dist/src/transformers/probe.d.ts.map +1 -0
- package/dist/src/transformers/probe.js +32 -0
- package/dist/src/transformers/probe.js.map +1 -0
- package/dist/src/transformers/source_file.d.ts +20 -0
- package/dist/src/transformers/source_file.d.ts.map +1 -0
- package/dist/src/transformers/source_file.js +30 -0
- package/dist/src/transformers/source_file.js.map +1 -0
- package/dist/stubs/config/media.stub +12 -0
- package/package.json +11 -7
- package/LICENSE +0 -21
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { readdir, stat } from 'node:fs/promises';
|
|
3
|
+
import { mkdir } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { hlsArtifactContentType } from '../hls/playlist.js';
|
|
6
|
+
import { loadMediabunny } from './mediabunny_loader.js';
|
|
7
|
+
import { withSourceFile } from './source_file.js';
|
|
8
|
+
/**
|
|
9
|
+
* Detect how (whether) WebCodecs is available: `'native'` when the runtime already exposes
|
|
10
|
+
* `VideoEncoder`, `'injected'` when a {@link WebCodecsProvider} supplies one, `'absent'` otherwise.
|
|
11
|
+
* `'absent'` means encoding is impossible — transforms stay stream-copy only, and a transformer
|
|
12
|
+
* that cannot proceed without an encoder should throw `TransformerRuntimeMissingError`.
|
|
13
|
+
*/
|
|
14
|
+
export async function resolveWebCodecsSupport(provider) {
|
|
15
|
+
if (globalThis.VideoEncoder !== undefined)
|
|
16
|
+
return 'native';
|
|
17
|
+
if (provider && (await provider()).VideoEncoder !== undefined)
|
|
18
|
+
return 'injected';
|
|
19
|
+
return 'absent';
|
|
20
|
+
}
|
|
21
|
+
/** File name of the entry (master) playlist an {@link HlsTransformer} produces. */
|
|
22
|
+
export const HLS_ENTRY_PLAYLIST = 'index.m3u8';
|
|
23
|
+
/**
|
|
24
|
+
* The built-in video {@link Transformer}: converts a stored video into an HLS package — MPEG-TS
|
|
25
|
+
* segments plus media playlists behind one master playlist — written under the conversion prefix
|
|
26
|
+
* and persisted as `record.conversions['hls']` with duration/resolution/codec metadata.
|
|
27
|
+
*
|
|
28
|
+
* **Remux-only by default.** The engine (mediabunny, pure JS — no ffmpeg binary) stream-copies
|
|
29
|
+
* the source's h264/aac tracks into segments without re-encoding, because encoding needs
|
|
30
|
+
* WebCodecs, which Node does not provide. Two consequences, both deliberate:
|
|
31
|
+
*
|
|
32
|
+
* - a source whose codecs MPEG-TS cannot carry (vp9, av1…) fails with a clear, definitive
|
|
33
|
+
* `HlsSourceUnsupportedError` — unless a {@link WebCodecsProvider} is configured, which enables
|
|
34
|
+
* the engine's re-encode fallback;
|
|
35
|
+
* - the package has a single variant at the original resolution. Multi-quality renditions are the
|
|
36
|
+
* documented evolution once an encoder is present (mediabunny supports fan-out video options),
|
|
37
|
+
* not something this transformer fakes today.
|
|
38
|
+
*
|
|
39
|
+
* The engine also carries the subtle bit the naive implementation gets wrong: AAC audio with
|
|
40
|
+
* priming (an mp4 edit list) starts at a **negative** timestamp, and a conversion trimmed at t=0
|
|
41
|
+
* would kick that track off the stream-copy fast path — impossible without an encoder. The
|
|
42
|
+
* conversion is therefore trimmed at the source's real first timestamp, shifting all tracks
|
|
43
|
+
* equally (sync is unchanged) and keeping every track copyable.
|
|
44
|
+
*
|
|
45
|
+
* Serving the result is the other half: playlists are stored with storage-relative references and
|
|
46
|
+
* must be rewritten per request — see {@link HlsDeliveryHandler}.
|
|
47
|
+
*/
|
|
48
|
+
export class HlsTransformer {
|
|
49
|
+
name;
|
|
50
|
+
eager;
|
|
51
|
+
targetDuration;
|
|
52
|
+
webcodecs;
|
|
53
|
+
engine;
|
|
54
|
+
constructor(options = {}) {
|
|
55
|
+
this.name = options.name ?? 'hls';
|
|
56
|
+
this.eager = options.eager ?? false;
|
|
57
|
+
this.targetDuration = options.targetDuration ?? 4;
|
|
58
|
+
this.webcodecs = options.webcodecs;
|
|
59
|
+
this.engine = options.engine;
|
|
60
|
+
}
|
|
61
|
+
async transform(context) {
|
|
62
|
+
const engine = this.engine ?? (await loadMediabunny(this.name)).createHlsEngine();
|
|
63
|
+
return withSourceFile(context, async ({ sourcePath, tempDir }) => {
|
|
64
|
+
const outputDir = join(tempDir, 'hls');
|
|
65
|
+
await mkdir(outputDir, { recursive: true });
|
|
66
|
+
const summary = await engine.remux({
|
|
67
|
+
sourcePath,
|
|
68
|
+
outputDir,
|
|
69
|
+
entryName: HLS_ENTRY_PLAYLIST,
|
|
70
|
+
targetDuration: this.targetDuration,
|
|
71
|
+
webcodecs: this.webcodecs,
|
|
72
|
+
});
|
|
73
|
+
// Upload the whole package the engine wrote — playlists and segments alike — streaming each
|
|
74
|
+
// file (a segment never has to fit in memory). Sorted for deterministic order in
|
|
75
|
+
// `conversions[name].files`.
|
|
76
|
+
const files = (await readdir(outputDir, { recursive: true })).sort();
|
|
77
|
+
let segmentCount = 0;
|
|
78
|
+
let playlistCount = 0;
|
|
79
|
+
for (const file of files) {
|
|
80
|
+
const relative = file.split('\\').join('/');
|
|
81
|
+
const local = join(outputDir, file);
|
|
82
|
+
const info = await stat(local);
|
|
83
|
+
if (!info.isFile())
|
|
84
|
+
continue;
|
|
85
|
+
if (relative.toLowerCase().endsWith('.m3u8'))
|
|
86
|
+
playlistCount += 1;
|
|
87
|
+
else
|
|
88
|
+
segmentCount += 1;
|
|
89
|
+
const contentType = hlsArtifactContentType(relative);
|
|
90
|
+
await context.write(relative, createReadStream(local), {
|
|
91
|
+
contentLength: info.size,
|
|
92
|
+
...(contentType !== undefined ? { contentType } : {}),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
entry: HLS_ENTRY_PLAYLIST,
|
|
97
|
+
meta: compactMeta({
|
|
98
|
+
...summary,
|
|
99
|
+
segmentCount,
|
|
100
|
+
playlistCount,
|
|
101
|
+
targetDuration: this.targetDuration,
|
|
102
|
+
}),
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Drop `undefined` values so the persisted meta round-trips identically through JSON stores. */
|
|
108
|
+
export function compactMeta(meta) {
|
|
109
|
+
return Object.fromEntries(Object.entries(meta).filter(([, value]) => value !== undefined));
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=hls.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hls.js","sourceRoot":"","sources":["../../../src/transformers/hls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAqBlD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,QAA4B;IAE5B,IAAK,UAAsC,CAAC,YAAY,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC;IACxF,IAAI,QAAQ,IAAI,CAAC,MAAM,QAAQ,EAAE,CAAC,CAAC,YAAY,KAAK,SAAS;QAAE,OAAO,UAAU,CAAC;IACjF,OAAO,QAAQ,CAAC;AAClB,CAAC;AAoDD,mFAAmF;AACnF,MAAM,C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AC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediabunny.d.ts","sourceRoot":"","sources":["../../../src/transformers/mediabunny.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,cAAc,EAAuD,MAAM,UAAU,CAAC;AACpG,OAAO,KAAK,EAAE,gBAAgB,EAAqB,MAAM,YAAY,CAAC;AA+DtE,wBAAgB,eAAe,IAAI,cAAc,CA8DhD;AAED,wBAAgB,iBAAiB,IAAI,gBAAgB,CAiCpD"}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { ALL_FORMATS, Conversion, FilePathSource, FilePathTarget, HlsOutputFormat, Input, MpegTsOutputFormat, Output, PathedTarget, } from 'mediabunny';
|
|
3
|
+
import { HlsSourceUnsupportedError } from '../errors.js';
|
|
4
|
+
/**
|
|
5
|
+
* The mediabunny-backed engines — the ONLY module in the package that imports the optional
|
|
6
|
+
* `mediabunny` peer, reached lazily through `mediabunny_loader.ts` at first transform.
|
|
7
|
+
*
|
|
8
|
+
* Why mediabunny: pure TypeScript, no ffmpeg binary — a production container without system
|
|
9
|
+
* ffmpeg (the usual case) remuxes fine. The trade-off is that *encoding* rides on WebCodecs,
|
|
10
|
+
* which Node lacks, hence the remux-only default and the {@link WebCodecsProvider} injection
|
|
11
|
+
* seam documented on `HlsTransformer`.
|
|
12
|
+
*/
|
|
13
|
+
/** The WebCodecs API surface mediabunny reads from `globalThis`. */
|
|
14
|
+
const WEBCODECS_GLOBALS = [
|
|
15
|
+
'VideoEncoder',
|
|
16
|
+
'VideoDecoder',
|
|
17
|
+
'AudioEncoder',
|
|
18
|
+
'AudioDecoder',
|
|
19
|
+
'VideoFrame',
|
|
20
|
+
'AudioData',
|
|
21
|
+
'EncodedVideoChunk',
|
|
22
|
+
'EncodedAudioChunk',
|
|
23
|
+
];
|
|
24
|
+
/**
|
|
25
|
+
* Install an injected WebCodecs implementation onto `globalThis` — only the classes that are
|
|
26
|
+
* missing; a native implementation is never overwritten. Global mutation is deliberate and
|
|
27
|
+
* additive: `globalThis` is where the WebCodecs spec (and therefore mediabunny) looks the API up,
|
|
28
|
+
* so "injecting an implementation" and "installing it globally" are the same thing. Installing
|
|
29
|
+
* per-conversion and uninstalling after would race concurrent transforms for zero benefit.
|
|
30
|
+
*/
|
|
31
|
+
async function installWebCodecs(provider) {
|
|
32
|
+
if (!provider)
|
|
33
|
+
return;
|
|
34
|
+
const implementation = await provider();
|
|
35
|
+
const globals = globalThis;
|
|
36
|
+
for (const name of WEBCODECS_GLOBALS) {
|
|
37
|
+
if (globals[name] === undefined && implementation[name] !== undefined) {
|
|
38
|
+
globals[name] = implementation[name];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Best-effort technical summary of an input — never fails the transform over metadata. */
|
|
43
|
+
async function summarize(input) {
|
|
44
|
+
const summary = {};
|
|
45
|
+
try {
|
|
46
|
+
summary.durationSeconds = await input.computeDuration();
|
|
47
|
+
const video = await input.getPrimaryVideoTrack();
|
|
48
|
+
if (video) {
|
|
49
|
+
summary.width = await video.getDisplayWidth();
|
|
50
|
+
summary.height = await video.getDisplayHeight();
|
|
51
|
+
summary.videoCodec = await video.getCodec();
|
|
52
|
+
}
|
|
53
|
+
const audio = await input.getPrimaryAudioTrack();
|
|
54
|
+
if (audio) {
|
|
55
|
+
summary.audioCodec = await audio.getCodec();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
// metadata is a bonus; the artifacts are the product
|
|
60
|
+
}
|
|
61
|
+
return summary;
|
|
62
|
+
}
|
|
63
|
+
export function createHlsEngine() {
|
|
64
|
+
return {
|
|
65
|
+
async remux(request) {
|
|
66
|
+
await installWebCodecs(request.webcodecs);
|
|
67
|
+
const input = new Input({
|
|
68
|
+
source: new FilePathSource(request.sourcePath),
|
|
69
|
+
formats: ALL_FORMATS,
|
|
70
|
+
});
|
|
71
|
+
try {
|
|
72
|
+
const output = new Output({
|
|
73
|
+
format: new HlsOutputFormat({
|
|
74
|
+
segmentFormat: new MpegTsOutputFormat(),
|
|
75
|
+
targetDuration: request.targetDuration,
|
|
76
|
+
}),
|
|
77
|
+
// The HLS output is many files; each one lands in the local output directory.
|
|
78
|
+
target: new PathedTarget(join(request.outputDir, request.entryName), (target) => new FilePathTarget(target.path)),
|
|
79
|
+
});
|
|
80
|
+
// THE gotcha: AAC audio with priming (an mp4 edit list) starts at a NEGATIVE timestamp.
|
|
81
|
+
// Mediabunny's default trims the conversion at t=0, which would throw that audio track off
|
|
82
|
+
// the packet-copy fast path — and re-encoding it needs an encoder we may not have.
|
|
83
|
+
// Trimming at the source's real first timestamp keeps every track a pure stream copy; all
|
|
84
|
+
// tracks shift equally, so A/V sync is untouched.
|
|
85
|
+
const firstTimestamp = await input.getFirstTimestamp();
|
|
86
|
+
const conversion = await Conversion.init({
|
|
87
|
+
input,
|
|
88
|
+
output,
|
|
89
|
+
trim: { start: firstTimestamp },
|
|
90
|
+
});
|
|
91
|
+
// With no video/audio options, mediabunny copies compatible tracks and re-encodes the
|
|
92
|
+
// rest — but re-encoding needs an encoder, so without WebCodecs an incompatible track is
|
|
93
|
+
// DISCARDED instead. Silently shipping a video without its audio (or vice versa) is not a
|
|
94
|
+
// conversion, it is data loss: fail loudly, with the codec and the reason per track.
|
|
95
|
+
if (conversion.discardedTracks.length > 0) {
|
|
96
|
+
const reasons = await Promise.all(conversion.discardedTracks.map(async (discarded) => {
|
|
97
|
+
const codec = await discarded.track.getCodec().catch(() => null);
|
|
98
|
+
return `${discarded.track.type}/${codec ?? 'unknown'}: ${discarded.reason}`;
|
|
99
|
+
}));
|
|
100
|
+
throw new HlsSourceUnsupportedError(reasons.join('; '));
|
|
101
|
+
}
|
|
102
|
+
if (!conversion.isValid) {
|
|
103
|
+
throw new HlsSourceUnsupportedError('the file contains no usable track');
|
|
104
|
+
}
|
|
105
|
+
const summary = await summarize(input);
|
|
106
|
+
await conversion.execute();
|
|
107
|
+
return summary;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
input.dispose();
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
export function createProbeEngine() {
|
|
116
|
+
return {
|
|
117
|
+
async probe(sourcePath) {
|
|
118
|
+
const input = new Input({ source: new FilePathSource(sourcePath), formats: ALL_FORMATS });
|
|
119
|
+
try {
|
|
120
|
+
const format = await input.getFormat();
|
|
121
|
+
const summary = {
|
|
122
|
+
format: format.name,
|
|
123
|
+
mimeType: await input.getMimeType(),
|
|
124
|
+
durationSeconds: await input.computeDuration(),
|
|
125
|
+
hasVideo: false,
|
|
126
|
+
hasAudio: false,
|
|
127
|
+
};
|
|
128
|
+
const video = await input.getPrimaryVideoTrack();
|
|
129
|
+
if (video) {
|
|
130
|
+
summary.hasVideo = true;
|
|
131
|
+
summary.width = await video.getDisplayWidth();
|
|
132
|
+
summary.height = await video.getDisplayHeight();
|
|
133
|
+
summary.videoCodec = await video.getCodec();
|
|
134
|
+
}
|
|
135
|
+
const audio = await input.getPrimaryAudioTrack();
|
|
136
|
+
if (audio) {
|
|
137
|
+
summary.hasAudio = true;
|
|
138
|
+
summary.audioCodec = await audio.getCodec();
|
|
139
|
+
summary.audioSampleRate = await audio.getSampleRate();
|
|
140
|
+
summary.audioChannels = await audio.getNumberOfChannels();
|
|
141
|
+
}
|
|
142
|
+
return summary;
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
input.dispose();
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
//# sourceMappingURL=mediabunny.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { HlsRemuxEngine } from './hls.js';
|
|
2
|
+
import type { MediaProbeEngine } from './probe.js';
|
|
3
|
+
/** The engine factories `./mediabunny.js` exports (the only module that imports the peer). */
|
|
4
|
+
export interface MediabunnyEngineModule {
|
|
5
|
+
createHlsEngine(): HlsRemuxEngine;
|
|
6
|
+
createProbeEngine(): MediaProbeEngine;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Load the mediabunny-backed engine module, translating a missing optional peer into the typed
|
|
10
|
+
* {@link TransformerRuntimeMissingError} instead of a raw `ERR_MODULE_NOT_FOUND` five imports deep.
|
|
11
|
+
* Import happens here — at first transform — never at boot: configuring a transformer must not
|
|
12
|
+
* load its engine (the same laziness `processors.sharp()` and `disks.s3()` follow).
|
|
13
|
+
*/
|
|
14
|
+
export declare function loadMediabunny(transformerName: string): Promise<MediabunnyEngineModule>;
|
|
15
|
+
//# sourceMappingURL=mediabunny_loader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediabunny_loader.d.ts","sourceRoot":"","sources":["../../../src/transformers/mediabunny_loader.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,8FAA8F;AAC9F,MAAM,WAAW,sBAAsB;IACrC,eAAe,IAAI,cAAc,CAAC;IAClC,iBAAiB,IAAI,gBAAgB,CAAC;CACvC;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,eAAe,EAAE,MAAM,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAiB7F"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { TransformerRuntimeMissingError } from '../errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Load the mediabunny-backed engine module, translating a missing optional peer into the typed
|
|
4
|
+
* {@link TransformerRuntimeMissingError} instead of a raw `ERR_MODULE_NOT_FOUND` five imports deep.
|
|
5
|
+
* Import happens here — at first transform — never at boot: configuring a transformer must not
|
|
6
|
+
* load its engine (the same laziness `processors.sharp()` and `disks.s3()` follow).
|
|
7
|
+
*/
|
|
8
|
+
export async function loadMediabunny(transformerName) {
|
|
9
|
+
try {
|
|
10
|
+
return await import('./mediabunny.js');
|
|
11
|
+
}
|
|
12
|
+
catch (error) {
|
|
13
|
+
if (error.code === 'ERR_MODULE_NOT_FOUND' &&
|
|
14
|
+
error instanceof Error &&
|
|
15
|
+
error.message.includes('mediabunny')) {
|
|
16
|
+
throw new TransformerRuntimeMissingError(transformerName, "the optional peer dependency 'mediabunny'", 'Install it (`pnpm add mediabunny`) or inject a custom `engine`.');
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=mediabunny_loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediabunny_loader.js","sourceRoot":"","sources":["../../../src/transformers/mediabunny_loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,8BAA8B,EAAE,MAAM,cAAc,CAAC;AAU9D;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAAC,eAAuB;IAC1D,IAAI,CAAC;QACH,OAAO,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAC;IACzC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IACG,KAA2B,CAAC,IAAI,KAAK,sBAAsB;YAC5D,KAAK,YAAY,KAAK;YACtB,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAC,EACpC,CAAC;YACD,MAAM,IAAI,8BAA8B,CACtC,eAAe,EACf,2CAA2C,EAC3C,iEAAiE,CAClE,CAAC;QACJ,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { TransformResult, Transformer, TransformerContext } from '../transformer.js';
|
|
2
|
+
/** What probing a media file yields — persisted verbatim as the conversion's `meta`. */
|
|
3
|
+
export interface MediaProbeSummary {
|
|
4
|
+
/** Container format name (e.g. `MP4`), when the engine can tell. */
|
|
5
|
+
format?: string;
|
|
6
|
+
/** Full MIME type including codecs parameter, when the engine can tell. */
|
|
7
|
+
mimeType?: string;
|
|
8
|
+
durationSeconds?: number;
|
|
9
|
+
width?: number;
|
|
10
|
+
height?: number;
|
|
11
|
+
videoCodec?: string | null;
|
|
12
|
+
audioCodec?: string | null;
|
|
13
|
+
audioSampleRate?: number;
|
|
14
|
+
audioChannels?: number;
|
|
15
|
+
hasVideo: boolean;
|
|
16
|
+
hasAudio: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The engine seam of {@link MetadataProbeTransformer}: local file in, metadata out. Default engine
|
|
20
|
+
* is mediabunny (lazy); tests inject a fake.
|
|
21
|
+
*/
|
|
22
|
+
export interface MediaProbeEngine {
|
|
23
|
+
probe(sourcePath: string): Promise<MediaProbeSummary>;
|
|
24
|
+
}
|
|
25
|
+
export interface MetadataProbeTransformerOptions<Name extends string = 'probe'> {
|
|
26
|
+
/** Conversion name on the record. Default `'probe'`. */
|
|
27
|
+
name?: Name;
|
|
28
|
+
/**
|
|
29
|
+
* Run inside attach. Default `false`: probing is CPU-cheap but reads the original, and on a
|
|
30
|
+
* remote disk that read is a download. Opt in for collections of small files.
|
|
31
|
+
*/
|
|
32
|
+
eager?: boolean;
|
|
33
|
+
/** Replace the mediabunny engine (mainly for tests). */
|
|
34
|
+
engine?: MediaProbeEngine;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The built-in **metadata-only** {@link Transformer}: reads a media file's technical metadata —
|
|
38
|
+
* duration, resolution, codecs, channel layout — and persists it as
|
|
39
|
+
* `record.conversions['probe'].meta`, writing **no artifact at all**. The reference implementation
|
|
40
|
+
* of the "derive metadata, not files" transformer shape; the model for a blurhash/thumbhash
|
|
41
|
+
* placeholder transformer.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* await media.library.transform(id, 'probe')
|
|
45
|
+
* const record = await media.library.find(id)
|
|
46
|
+
* record.conversions.probe?.meta // { durationSeconds: 41.6, width: 1920, ... }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare class MetadataProbeTransformer<Name extends string = 'probe'> implements Transformer {
|
|
50
|
+
readonly name: Name;
|
|
51
|
+
readonly eager: boolean;
|
|
52
|
+
private readonly engine;
|
|
53
|
+
constructor(options?: MetadataProbeTransformerOptions<Name>);
|
|
54
|
+
transform(context: TransformerContext): Promise<TransformResult>;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=probe.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../../../src/transformers/probe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAK1F,wFAAwF;AACxF,MAAM,WAAW,iBAAiB;IAChC,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;CACvD;AAED,MAAM,WAAW,+BAA+B,CAAC,IAAI,SAAS,MAAM,GAAG,OAAO;IAC5E,wDAAwD;IACxD,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,wDAAwD;IACxD,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,wBAAwB,CAAC,IAAI,SAAS,MAAM,GAAG,OAAO,CAAE,YAAW,WAAW;IACzF,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA+B;gBAE1C,OAAO,GAAE,+BAA+B,CAAC,IAAI,CAAM;IAMzD,SAAS,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC;CAKvE"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { compactMeta } from './hls.js';
|
|
2
|
+
import { loadMediabunny } from './mediabunny_loader.js';
|
|
3
|
+
import { withSourceFile } from './source_file.js';
|
|
4
|
+
/**
|
|
5
|
+
* The built-in **metadata-only** {@link Transformer}: reads a media file's technical metadata —
|
|
6
|
+
* duration, resolution, codecs, channel layout — and persists it as
|
|
7
|
+
* `record.conversions['probe'].meta`, writing **no artifact at all**. The reference implementation
|
|
8
|
+
* of the "derive metadata, not files" transformer shape; the model for a blurhash/thumbhash
|
|
9
|
+
* placeholder transformer.
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* await media.library.transform(id, 'probe')
|
|
13
|
+
* const record = await media.library.find(id)
|
|
14
|
+
* record.conversions.probe?.meta // { durationSeconds: 41.6, width: 1920, ... }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export class MetadataProbeTransformer {
|
|
18
|
+
name;
|
|
19
|
+
eager;
|
|
20
|
+
engine;
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
this.name = options.name ?? 'probe';
|
|
23
|
+
this.eager = options.eager ?? false;
|
|
24
|
+
this.engine = options.engine;
|
|
25
|
+
}
|
|
26
|
+
async transform(context) {
|
|
27
|
+
const engine = this.engine ?? (await loadMediabunny(this.name)).createProbeEngine();
|
|
28
|
+
const summary = await withSourceFile(context, ({ sourcePath }) => engine.probe(sourcePath));
|
|
29
|
+
return { meta: compactMeta({ ...summary }) };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=probe.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"probe.js","sourceRoot":"","sources":["../../../src/transformers/probe.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAuClD;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,wBAAwB;IAC1B,IAAI,CAAO;IACX,KAAK,CAAU;IACP,MAAM,CAA+B;IAEtD,YAAY,UAAiD,EAAE;QAC7D,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAK,OAAgB,CAAC;QAC9C,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,IAAI,KAAK,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,OAA2B;QACzC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,CAAC,MAAM,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,iBAAiB,EAAE,CAAC;QACpF,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC;QAC5F,OAAO,EAAE,IAAI,EAAE,WAAW,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,EAAE,CAAC;IAC/C,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { TransformerContext } from '../transformer.js';
|
|
2
|
+
/** What {@link withSourceFile} hands its callback. */
|
|
3
|
+
export interface MaterializedSource {
|
|
4
|
+
/** Local path of the original's bytes. */
|
|
5
|
+
sourcePath: string;
|
|
6
|
+
/** The temp directory the source lives in — free scratch space, removed when the callback ends. */
|
|
7
|
+
tempDir: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Materialize a media original into a local temp file for the duration of `fn`, then remove the
|
|
11
|
+
* whole temp directory — success or failure.
|
|
12
|
+
*
|
|
13
|
+
* File-based media engines (mediabunny, and most others) want random access to a local path, while
|
|
14
|
+
* a disk object only offers bytes/stream — so file-shaped transformers all start with this same
|
|
15
|
+
* dance. The bytes are **streamed** to disk, never buffered whole: the typical subject is a video.
|
|
16
|
+
* The file keeps the original's extension purely as a debugging nicety; engines detect the real
|
|
17
|
+
* format from content.
|
|
18
|
+
*/
|
|
19
|
+
export declare function withSourceFile<T>(context: TransformerContext, fn: (source: MaterializedSource) => Promise<T>): Promise<T>;
|
|
20
|
+
//# sourceMappingURL=source_file.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source_file.d.ts","sourceRoot":"","sources":["../../../src/transformers/source_file.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,sDAAsD;AACtD,MAAM,WAAW,kBAAkB;IACjC,0CAA0C;IAC1C,UAAU,EAAE,MAAM,CAAC;IACnB,mGAAmG;IACnG,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;;;;;GASG;AACH,wBAAsB,cAAc,CAAC,CAAC,EACpC,OAAO,EAAE,kBAAkB,EAC3B,EAAE,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,GAC7C,OAAO,CAAC,CAAC,CAAC,CAYZ"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createWriteStream } from 'node:fs';
|
|
2
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { extname, join } from 'node:path';
|
|
5
|
+
import { pipeline } from 'node:stream/promises';
|
|
6
|
+
/**
|
|
7
|
+
* Materialize a media original into a local temp file for the duration of `fn`, then remove the
|
|
8
|
+
* whole temp directory — success or failure.
|
|
9
|
+
*
|
|
10
|
+
* File-based media engines (mediabunny, and most others) want random access to a local path, while
|
|
11
|
+
* a disk object only offers bytes/stream — so file-shaped transformers all start with this same
|
|
12
|
+
* dance. The bytes are **streamed** to disk, never buffered whole: the typical subject is a video.
|
|
13
|
+
* The file keeps the original's extension purely as a debugging nicety; engines detect the real
|
|
14
|
+
* format from content.
|
|
15
|
+
*/
|
|
16
|
+
export async function withSourceFile(context, fn) {
|
|
17
|
+
const tempDir = await mkdtemp(join(tmpdir(), 'agora-media-transform-'));
|
|
18
|
+
try {
|
|
19
|
+
const extension = extname(context.record.fileName);
|
|
20
|
+
const sourcePath = join(tempDir, `source${extension || '.bin'}`);
|
|
21
|
+
await pipeline(await context.getStream(), createWriteStream(sourcePath));
|
|
22
|
+
return await fn({ sourcePath, tempDir });
|
|
23
|
+
}
|
|
24
|
+
finally {
|
|
25
|
+
await rm(tempDir, { recursive: true, force: true }).catch(() => {
|
|
26
|
+
// best-effort: temp cleanup must never mask the transform's own outcome
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=source_file.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source_file.js","sourceRoot":"","sources":["../../../src/transformers/source_file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAWhD;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,OAA2B,EAC3B,EAA8C;IAE9C,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,wBAAwB,CAAC,CAAC,CAAC;IACxE,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACnD,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,SAAS,IAAI,MAAM,EAAE,CAAC,CAAC;QACjE,MAAM,QAAQ,CAAC,MAAM,OAAO,CAAC,SAAS,EAAE,EAAE,iBAAiB,CAAC,UAAU,CAAC,CAAC,CAAC;QACzE,OAAO,MAAM,EAAE,CAAC,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC;IAC3C,CAAC;YAAS,CAAC;QACT,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE;YAC7D,wEAAwE;QAC1E,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC"}
|
|
@@ -55,5 +55,17 @@ export default defineConfig({
|
|
|
55
55
|
// sessionTtlSeconds: 24 * 60 * 60,
|
|
56
56
|
// routes: { enabled: true, prefix: '/media/uploads/tus' },
|
|
57
57
|
// },
|
|
58
|
+
//
|
|
59
|
+
// // Session-backed direct uploads — the browser PUTs multipart parts straight to S3 through
|
|
60
|
+
// // presigned URLs (bytes never pass through the app), and the session (uploadId, part size,
|
|
61
|
+
// // confirmed ETags) persists server-side so an interrupted upload resumes after a page
|
|
62
|
+
// // reload. Needs a multipart-capable disk (disks.s3()); shares the session tables above.
|
|
63
|
+
// direct: {
|
|
64
|
+
// store: 'lucid',
|
|
65
|
+
// stores: { lucid: uploadSessions.lucid() },
|
|
66
|
+
// partSize: 20 * 1024 * 1024,
|
|
67
|
+
// sessionTtlSeconds: 24 * 60 * 60,
|
|
68
|
+
// routes: { enabled: true, prefix: '/media/uploads/direct/sessions', collection: 'videos' },
|
|
69
|
+
// },
|
|
58
70
|
// },
|
|
59
71
|
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonis-agora/media",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), and column attachments, all on top of @adonisjs/drive. Pluggable MediaStore (in-memory + Lucid) and ImageProcessor (sharp). Part of the Agora ecosystem.",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe), and column attachments, all on top of @adonisjs/drive. Pluggable MediaStore (in-memory + Lucid) and ImageProcessor (sharp). Part of the Agora ecosystem.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -86,20 +86,20 @@
|
|
|
86
86
|
"@adonisjs/drive": "^3.0.0",
|
|
87
87
|
"@adonisjs/lucid": "^22.4.0",
|
|
88
88
|
"@aws-sdk/client-s3": "^3.0.0",
|
|
89
|
-
"
|
|
89
|
+
"mediabunny": "^1.45.4",
|
|
90
90
|
"sharp": "^0.33.0"
|
|
91
91
|
},
|
|
92
92
|
"peerDependenciesMeta": {
|
|
93
93
|
"@adonis-agora/telescope": {
|
|
94
94
|
"optional": true
|
|
95
95
|
},
|
|
96
|
-
"
|
|
96
|
+
"mediabunny": {
|
|
97
97
|
"optional": true
|
|
98
98
|
},
|
|
99
|
-
"@
|
|
99
|
+
"@adonisjs/lucid": {
|
|
100
100
|
"optional": true
|
|
101
101
|
},
|
|
102
|
-
"@aws-sdk/s3
|
|
102
|
+
"@aws-sdk/client-s3": {
|
|
103
103
|
"optional": true
|
|
104
104
|
},
|
|
105
105
|
"sharp": {
|
|
@@ -112,13 +112,13 @@
|
|
|
112
112
|
"@adonisjs/drive": "^3.0.0",
|
|
113
113
|
"@adonisjs/lucid": "^22.4.0",
|
|
114
114
|
"@aws-sdk/client-s3": "^3.726.1",
|
|
115
|
-
"@aws-sdk/s3-request-presigner": "^3.726.1",
|
|
116
115
|
"@smithy/util-stream": "^3.3.1",
|
|
117
116
|
"@types/better-sqlite3": "^7.6.0",
|
|
118
117
|
"@types/node": "^20.0.0",
|
|
119
118
|
"aws-sdk-client-mock": "^4.1.0",
|
|
120
119
|
"better-sqlite3": "^11.0.0",
|
|
121
120
|
"luxon": "^3.4.0",
|
|
121
|
+
"mediabunny": "^1.50.9",
|
|
122
122
|
"sharp": "^0.33.0",
|
|
123
123
|
"tempura": "0.4.1",
|
|
124
124
|
"typescript": "^5.4.0",
|
|
@@ -137,6 +137,10 @@
|
|
|
137
137
|
"uploads",
|
|
138
138
|
"image",
|
|
139
139
|
"conversions",
|
|
140
|
+
"transformers",
|
|
141
|
+
"video",
|
|
142
|
+
"hls",
|
|
143
|
+
"mediabunny",
|
|
140
144
|
"sharp",
|
|
141
145
|
"attachment",
|
|
142
146
|
"agora"
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Davi Carvalho
|
|
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.
|