@kenzuya/mediabunny 1.26.0 → 1.28.6
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/README.md +1 -1
- package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21390
- package/dist/bundles/mediabunny.min.js +490 -0
- package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
- package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
- package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts +1 -1
- package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
- package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
- package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-misc.d.ts +88 -0
- package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
- package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
- package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
- package/dist/modules/src/avi/riff-writer.d.ts +26 -0
- package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
- package/dist/modules/src/codec-data.d.ts +8 -3
- package/dist/modules/src/codec-data.d.ts.map +1 -1
- package/dist/modules/src/codec.d.ts +10 -10
- package/dist/modules/src/codec.d.ts.map +1 -1
- package/dist/modules/src/conversion.d.ts +33 -16
- package/dist/modules/src/conversion.d.ts.map +1 -1
- package/dist/modules/src/custom-coder.d.ts +8 -8
- package/dist/modules/src/custom-coder.d.ts.map +1 -1
- package/dist/modules/src/demuxer.d.ts +3 -3
- package/dist/modules/src/demuxer.d.ts.map +1 -1
- package/dist/modules/src/encode.d.ts +8 -8
- package/dist/modules/src/encode.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
- package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-misc.d.ts +3 -3
- package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
- package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
- package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
- package/dist/modules/src/id3.d.ts +3 -3
- package/dist/modules/src/id3.d.ts.map +1 -1
- package/dist/modules/src/index.d.ts +20 -20
- package/dist/modules/src/index.d.ts.map +1 -1
- package/dist/modules/src/input-format.d.ts +22 -0
- package/dist/modules/src/input-format.d.ts.map +1 -1
- package/dist/modules/src/input-track.d.ts +8 -8
- package/dist/modules/src/input-track.d.ts.map +1 -1
- package/dist/modules/src/input.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
- package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
- package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
- package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
- package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
- package/dist/modules/src/matroska/ebml.d.ts +3 -3
- package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
- package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
- package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
- package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
- package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
- package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
- package/dist/modules/src/media-sink.d.ts +5 -5
- package/dist/modules/src/media-sink.d.ts.map +1 -1
- package/dist/modules/src/media-source.d.ts +22 -4
- package/dist/modules/src/media-source.d.ts.map +1 -1
- package/dist/modules/src/metadata.d.ts +2 -2
- package/dist/modules/src/metadata.d.ts.map +1 -1
- package/dist/modules/src/misc.d.ts +5 -4
- package/dist/modules/src/misc.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
- package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
- package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
- package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
- package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
- package/dist/modules/src/muxer.d.ts +4 -4
- package/dist/modules/src/muxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
- package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
- package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
- package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
- package/dist/modules/src/output-format.d.ts +51 -6
- package/dist/modules/src/output-format.d.ts.map +1 -1
- package/dist/modules/src/output.d.ts +13 -13
- package/dist/modules/src/output.d.ts.map +1 -1
- package/dist/modules/src/packet.d.ts +1 -1
- package/dist/modules/src/packet.d.ts.map +1 -1
- package/dist/modules/src/pcm.d.ts.map +1 -1
- package/dist/modules/src/reader.d.ts +2 -2
- package/dist/modules/src/reader.d.ts.map +1 -1
- package/dist/modules/src/sample.d.ts +57 -15
- package/dist/modules/src/sample.d.ts.map +1 -1
- package/dist/modules/src/source.d.ts +3 -3
- package/dist/modules/src/source.d.ts.map +1 -1
- package/dist/modules/src/subtitles.d.ts +1 -1
- package/dist/modules/src/subtitles.d.ts.map +1 -1
- package/dist/modules/src/target.d.ts +2 -2
- package/dist/modules/src/target.d.ts.map +1 -1
- package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts +1 -1
- package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
- package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
- package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
- package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
- package/dist/modules/src/writer.d.ts +1 -1
- package/dist/modules/src/writer.d.ts.map +1 -1
- package/dist/packages/eac3/eac3.wasm +0 -0
- package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
- package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
- package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
- package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
- package/dist/packages/mpeg4/xvid.wasm +0 -0
- package/package.json +18 -57
- package/dist/bundles/mediabunny.cjs +0 -26140
- package/dist/bundles/mediabunny.min.cjs +0 -147
- package/dist/bundles/mediabunny.min.mjs +0 -146
- package/dist/mediabunny.d.ts +0 -3319
- package/dist/modules/shared/mp3-misc.js +0 -147
- package/dist/modules/src/adts/adts-demuxer.js +0 -239
- package/dist/modules/src/adts/adts-muxer.js +0 -80
- package/dist/modules/src/adts/adts-reader.js +0 -63
- package/dist/modules/src/codec-data.js +0 -1730
- package/dist/modules/src/codec.js +0 -869
- package/dist/modules/src/conversion.js +0 -1459
- package/dist/modules/src/custom-coder.js +0 -117
- package/dist/modules/src/demuxer.js +0 -12
- package/dist/modules/src/encode.js +0 -442
- package/dist/modules/src/flac/flac-demuxer.js +0 -504
- package/dist/modules/src/flac/flac-misc.js +0 -135
- package/dist/modules/src/flac/flac-muxer.js +0 -222
- package/dist/modules/src/id3.js +0 -848
- package/dist/modules/src/index.js +0 -28
- package/dist/modules/src/input-format.js +0 -480
- package/dist/modules/src/input-track.js +0 -372
- package/dist/modules/src/input.js +0 -188
- package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
- package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
- package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
- package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
- package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
- package/dist/modules/src/matroska/ebml.js +0 -653
- package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
- package/dist/modules/src/matroska/matroska-misc.js +0 -20
- package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
- package/dist/modules/src/media-sink.js +0 -1736
- package/dist/modules/src/media-source.js +0 -1825
- package/dist/modules/src/metadata.js +0 -193
- package/dist/modules/src/misc.js +0 -623
- package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
- package/dist/modules/src/mp3/mp3-muxer.js +0 -123
- package/dist/modules/src/mp3/mp3-reader.js +0 -26
- package/dist/modules/src/mp3/mp3-writer.js +0 -78
- package/dist/modules/src/muxer.js +0 -50
- package/dist/modules/src/node.d.ts +0 -9
- package/dist/modules/src/node.d.ts.map +0 -1
- package/dist/modules/src/node.js +0 -9
- package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
- package/dist/modules/src/ogg/ogg-misc.js +0 -78
- package/dist/modules/src/ogg/ogg-muxer.js +0 -353
- package/dist/modules/src/ogg/ogg-reader.js +0 -65
- package/dist/modules/src/output-format.js +0 -527
- package/dist/modules/src/output.js +0 -300
- package/dist/modules/src/packet.js +0 -182
- package/dist/modules/src/pcm.js +0 -85
- package/dist/modules/src/reader.js +0 -236
- package/dist/modules/src/sample.js +0 -1056
- package/dist/modules/src/source.js +0 -1182
- package/dist/modules/src/subtitles.js +0 -575
- package/dist/modules/src/target.js +0 -140
- package/dist/modules/src/wave/riff-writer.js +0 -30
- package/dist/modules/src/wave/wave-demuxer.js +0 -447
- package/dist/modules/src/wave/wave-muxer.js +0 -318
- package/dist/modules/src/writer.js +0 -370
- package/src/adts/adts-demuxer.ts +0 -331
- package/src/adts/adts-muxer.ts +0 -111
- package/src/adts/adts-reader.ts +0 -85
- package/src/codec-data.ts +0 -2078
- package/src/codec.ts +0 -1092
- package/src/conversion.ts +0 -2112
- package/src/custom-coder.ts +0 -197
- package/src/demuxer.ts +0 -24
- package/src/encode.ts +0 -739
- package/src/flac/flac-demuxer.ts +0 -730
- package/src/flac/flac-misc.ts +0 -164
- package/src/flac/flac-muxer.ts +0 -320
- package/src/id3.ts +0 -925
- package/src/index.ts +0 -221
- package/src/input-format.ts +0 -541
- package/src/input-track.ts +0 -529
- package/src/input.ts +0 -235
- package/src/isobmff/isobmff-boxes.ts +0 -1719
- package/src/isobmff/isobmff-demuxer.ts +0 -3190
- package/src/isobmff/isobmff-misc.ts +0 -29
- package/src/isobmff/isobmff-muxer.ts +0 -1348
- package/src/isobmff/isobmff-reader.ts +0 -91
- package/src/matroska/ebml.ts +0 -730
- package/src/matroska/matroska-demuxer.ts +0 -2481
- package/src/matroska/matroska-misc.ts +0 -29
- package/src/matroska/matroska-muxer.ts +0 -1276
- package/src/media-sink.ts +0 -2179
- package/src/media-source.ts +0 -2243
- package/src/metadata.ts +0 -320
- package/src/misc.ts +0 -798
- package/src/mp3/mp3-demuxer.ts +0 -383
- package/src/mp3/mp3-muxer.ts +0 -166
- package/src/mp3/mp3-reader.ts +0 -34
- package/src/mp3/mp3-writer.ts +0 -120
- package/src/muxer.ts +0 -88
- package/src/node.ts +0 -11
- package/src/ogg/ogg-demuxer.ts +0 -1053
- package/src/ogg/ogg-misc.ts +0 -116
- package/src/ogg/ogg-muxer.ts +0 -497
- package/src/ogg/ogg-reader.ts +0 -93
- package/src/output-format.ts +0 -945
- package/src/output.ts +0 -488
- package/src/packet.ts +0 -263
- package/src/pcm.ts +0 -112
- package/src/reader.ts +0 -323
- package/src/sample.ts +0 -1461
- package/src/source.ts +0 -1688
- package/src/subtitles.ts +0 -711
- package/src/target.ts +0 -204
- package/src/tsconfig.json +0 -16
- package/src/wave/riff-writer.ts +0 -36
- package/src/wave/wave-demuxer.ts +0 -529
- package/src/wave/wave-muxer.ts +0 -371
- package/src/writer.ts +0 -490
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
3
|
-
*
|
|
4
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
-
*/
|
|
8
|
-
import { Muxer } from '../muxer.js';
|
|
9
|
-
import { parsePcmCodec, validateAudioChunkMetadata } from '../codec.js';
|
|
10
|
-
import { WaveFormat } from './wave-demuxer.js';
|
|
11
|
-
import { RiffWriter } from './riff-writer.js';
|
|
12
|
-
import { assert, assertNever, isIso88591Compatible, keyValueIterator } from '../misc.js';
|
|
13
|
-
import { metadataTagsAreEmpty } from '../metadata.js';
|
|
14
|
-
import { Id3V2Writer } from '../id3.js';
|
|
15
|
-
export class WaveMuxer extends Muxer {
|
|
16
|
-
constructor(output, format) {
|
|
17
|
-
super(output);
|
|
18
|
-
this.headerWritten = false;
|
|
19
|
-
this.dataSize = 0;
|
|
20
|
-
this.sampleRate = null;
|
|
21
|
-
this.sampleCount = 0;
|
|
22
|
-
this.riffSizePos = null;
|
|
23
|
-
this.dataSizePos = null;
|
|
24
|
-
this.ds64RiffSizePos = null;
|
|
25
|
-
this.ds64DataSizePos = null;
|
|
26
|
-
this.ds64SampleCountPos = null;
|
|
27
|
-
this.format = format;
|
|
28
|
-
this.writer = output._writer;
|
|
29
|
-
this.riffWriter = new RiffWriter(output._writer);
|
|
30
|
-
this.isRf64 = !!format._options.large;
|
|
31
|
-
}
|
|
32
|
-
async start() {
|
|
33
|
-
// Nothing needed here - we'll write the header with the first sample
|
|
34
|
-
}
|
|
35
|
-
async getMimeType() {
|
|
36
|
-
return 'audio/wav';
|
|
37
|
-
}
|
|
38
|
-
async addEncodedVideoPacket() {
|
|
39
|
-
throw new Error('WAVE does not support video.');
|
|
40
|
-
}
|
|
41
|
-
async addEncodedAudioPacket(track, packet, meta) {
|
|
42
|
-
const release = await this.mutex.acquire();
|
|
43
|
-
try {
|
|
44
|
-
if (!this.headerWritten) {
|
|
45
|
-
validateAudioChunkMetadata(meta);
|
|
46
|
-
assert(meta);
|
|
47
|
-
assert(meta.decoderConfig);
|
|
48
|
-
this.writeHeader(track, meta.decoderConfig);
|
|
49
|
-
this.sampleRate = meta.decoderConfig.sampleRate;
|
|
50
|
-
this.headerWritten = true;
|
|
51
|
-
}
|
|
52
|
-
this.validateAndNormalizeTimestamp(track, packet.timestamp, packet.type === 'key');
|
|
53
|
-
if (!this.isRf64 && this.writer.getPos() + packet.data.byteLength >= 2 ** 32) {
|
|
54
|
-
throw new Error('Adding more audio data would exceed the maximum RIFF size of 4 GiB. To write larger files, use'
|
|
55
|
-
+ ' RF64 by setting `large: true` in the WavOutputFormatOptions.');
|
|
56
|
-
}
|
|
57
|
-
this.writer.write(packet.data);
|
|
58
|
-
this.dataSize += packet.data.byteLength;
|
|
59
|
-
this.sampleCount += Math.round(packet.duration * this.sampleRate);
|
|
60
|
-
await this.writer.flush();
|
|
61
|
-
}
|
|
62
|
-
finally {
|
|
63
|
-
release();
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
async addSubtitleCue() {
|
|
67
|
-
throw new Error('WAVE does not support subtitles.');
|
|
68
|
-
}
|
|
69
|
-
writeHeader(track, config) {
|
|
70
|
-
if (this.format._options.onHeader) {
|
|
71
|
-
this.writer.startTrackingWrites();
|
|
72
|
-
}
|
|
73
|
-
let format;
|
|
74
|
-
const codec = track.source._codec;
|
|
75
|
-
const pcmInfo = parsePcmCodec(codec);
|
|
76
|
-
if (pcmInfo.dataType === 'ulaw') {
|
|
77
|
-
format = WaveFormat.MULAW;
|
|
78
|
-
}
|
|
79
|
-
else if (pcmInfo.dataType === 'alaw') {
|
|
80
|
-
format = WaveFormat.ALAW;
|
|
81
|
-
}
|
|
82
|
-
else if (pcmInfo.dataType === 'float') {
|
|
83
|
-
format = WaveFormat.IEEE_FLOAT;
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
format = WaveFormat.PCM;
|
|
87
|
-
}
|
|
88
|
-
const channels = config.numberOfChannels;
|
|
89
|
-
const sampleRate = config.sampleRate;
|
|
90
|
-
const blockSize = pcmInfo.sampleSize * channels;
|
|
91
|
-
// RIFF header
|
|
92
|
-
this.riffWriter.writeAscii(this.isRf64 ? 'RF64' : 'RIFF');
|
|
93
|
-
if (this.isRf64) {
|
|
94
|
-
this.riffWriter.writeU32(0xffffffff); // Not used in RF64
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
this.riffSizePos = this.writer.getPos();
|
|
98
|
-
this.riffWriter.writeU32(0); // File size placeholder
|
|
99
|
-
}
|
|
100
|
-
this.riffWriter.writeAscii('WAVE');
|
|
101
|
-
if (this.isRf64) {
|
|
102
|
-
this.riffWriter.writeAscii('ds64');
|
|
103
|
-
this.riffWriter.writeU32(28); // Chunk size
|
|
104
|
-
this.ds64RiffSizePos = this.writer.getPos();
|
|
105
|
-
this.riffWriter.writeU64(0); // RIFF size placeholder
|
|
106
|
-
this.ds64DataSizePos = this.writer.getPos();
|
|
107
|
-
this.riffWriter.writeU64(0); // Data size placeholder
|
|
108
|
-
this.ds64SampleCountPos = this.writer.getPos();
|
|
109
|
-
this.riffWriter.writeU64(0); // Sample count placeholder
|
|
110
|
-
this.riffWriter.writeU32(0); // Table length
|
|
111
|
-
// Empty table
|
|
112
|
-
}
|
|
113
|
-
// fmt chunk
|
|
114
|
-
this.riffWriter.writeAscii('fmt ');
|
|
115
|
-
this.riffWriter.writeU32(16); // Chunk size
|
|
116
|
-
this.riffWriter.writeU16(format);
|
|
117
|
-
this.riffWriter.writeU16(channels);
|
|
118
|
-
this.riffWriter.writeU32(sampleRate);
|
|
119
|
-
this.riffWriter.writeU32(sampleRate * blockSize); // Bytes per second
|
|
120
|
-
this.riffWriter.writeU16(blockSize);
|
|
121
|
-
this.riffWriter.writeU16(8 * pcmInfo.sampleSize);
|
|
122
|
-
// Metadata tags
|
|
123
|
-
if (!metadataTagsAreEmpty(this.output._metadataTags)) {
|
|
124
|
-
const metadataFormat = this.format._options.metadataFormat ?? 'info';
|
|
125
|
-
if (metadataFormat === 'info') {
|
|
126
|
-
this.writeInfoChunk(this.output._metadataTags);
|
|
127
|
-
}
|
|
128
|
-
else if (metadataFormat === 'id3') {
|
|
129
|
-
this.writeId3Chunk(this.output._metadataTags);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
assertNever(metadataFormat);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
// data chunk
|
|
136
|
-
this.riffWriter.writeAscii('data');
|
|
137
|
-
if (this.isRf64) {
|
|
138
|
-
this.riffWriter.writeU32(0xffffffff); // Not used in RF64
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
this.dataSizePos = this.writer.getPos();
|
|
142
|
-
this.riffWriter.writeU32(0); // Data size placeholder
|
|
143
|
-
}
|
|
144
|
-
if (this.format._options.onHeader) {
|
|
145
|
-
const { data, start } = this.writer.stopTrackingWrites();
|
|
146
|
-
this.format._options.onHeader(data, start);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
writeInfoChunk(metadata) {
|
|
150
|
-
const startPos = this.writer.getPos();
|
|
151
|
-
this.riffWriter.writeAscii('LIST');
|
|
152
|
-
this.riffWriter.writeU32(0); // Size placeholder
|
|
153
|
-
this.riffWriter.writeAscii('INFO');
|
|
154
|
-
const writtenTags = new Set();
|
|
155
|
-
const writeInfoTag = (tag, value) => {
|
|
156
|
-
if (!isIso88591Compatible(value)) {
|
|
157
|
-
// No Unicode supported here
|
|
158
|
-
console.warn(`Didn't write tag '${tag}' because '${value}' is not ISO 8859-1-compatible.`);
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const size = value.length + 1; // +1 for null terminator
|
|
162
|
-
const bytes = new Uint8Array(size);
|
|
163
|
-
for (let i = 0; i < value.length; i++) {
|
|
164
|
-
bytes[i] = value.charCodeAt(i);
|
|
165
|
-
}
|
|
166
|
-
this.riffWriter.writeAscii(tag);
|
|
167
|
-
this.riffWriter.writeU32(size);
|
|
168
|
-
this.writer.write(bytes);
|
|
169
|
-
// Add padding byte if size is odd
|
|
170
|
-
if (size & 1) {
|
|
171
|
-
this.writer.write(new Uint8Array(1));
|
|
172
|
-
}
|
|
173
|
-
writtenTags.add(tag);
|
|
174
|
-
};
|
|
175
|
-
for (const { key, value } of keyValueIterator(metadata)) {
|
|
176
|
-
switch (key) {
|
|
177
|
-
case 'title':
|
|
178
|
-
{
|
|
179
|
-
writeInfoTag('INAM', value);
|
|
180
|
-
writtenTags.add('INAM');
|
|
181
|
-
}
|
|
182
|
-
;
|
|
183
|
-
break;
|
|
184
|
-
case 'artist':
|
|
185
|
-
{
|
|
186
|
-
writeInfoTag('IART', value);
|
|
187
|
-
writtenTags.add('IART');
|
|
188
|
-
}
|
|
189
|
-
;
|
|
190
|
-
break;
|
|
191
|
-
case 'album':
|
|
192
|
-
{
|
|
193
|
-
writeInfoTag('IPRD', value);
|
|
194
|
-
writtenTags.add('IPRD');
|
|
195
|
-
}
|
|
196
|
-
;
|
|
197
|
-
break;
|
|
198
|
-
case 'trackNumber':
|
|
199
|
-
{
|
|
200
|
-
const string = metadata.tracksTotal !== undefined
|
|
201
|
-
? `${value}/${metadata.tracksTotal}`
|
|
202
|
-
: value.toString();
|
|
203
|
-
writeInfoTag('ITRK', string);
|
|
204
|
-
writtenTags.add('ITRK');
|
|
205
|
-
}
|
|
206
|
-
;
|
|
207
|
-
break;
|
|
208
|
-
case 'genre':
|
|
209
|
-
{
|
|
210
|
-
writeInfoTag('IGNR', value);
|
|
211
|
-
writtenTags.add('IGNR');
|
|
212
|
-
}
|
|
213
|
-
;
|
|
214
|
-
break;
|
|
215
|
-
case 'date':
|
|
216
|
-
{
|
|
217
|
-
writeInfoTag('ICRD', value.toISOString().slice(0, 10));
|
|
218
|
-
writtenTags.add('ICRD');
|
|
219
|
-
}
|
|
220
|
-
;
|
|
221
|
-
break;
|
|
222
|
-
case 'comment':
|
|
223
|
-
{
|
|
224
|
-
writeInfoTag('ICMT', value);
|
|
225
|
-
writtenTags.add('ICMT');
|
|
226
|
-
}
|
|
227
|
-
;
|
|
228
|
-
break;
|
|
229
|
-
case 'albumArtist':
|
|
230
|
-
case 'discNumber':
|
|
231
|
-
case 'tracksTotal':
|
|
232
|
-
case 'discsTotal':
|
|
233
|
-
case 'description':
|
|
234
|
-
case 'lyrics':
|
|
235
|
-
case 'images':
|
|
236
|
-
{
|
|
237
|
-
// Not supported in RIFF INFO
|
|
238
|
-
}
|
|
239
|
-
;
|
|
240
|
-
break;
|
|
241
|
-
case 'raw':
|
|
242
|
-
{
|
|
243
|
-
// Handled later
|
|
244
|
-
}
|
|
245
|
-
;
|
|
246
|
-
break;
|
|
247
|
-
default: assertNever(key);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
if (metadata.raw) {
|
|
251
|
-
for (const key in metadata.raw) {
|
|
252
|
-
const value = metadata.raw[key];
|
|
253
|
-
if (value == null || key.length !== 4 || writtenTags.has(key)) {
|
|
254
|
-
continue;
|
|
255
|
-
}
|
|
256
|
-
if (typeof value === 'string') {
|
|
257
|
-
writeInfoTag(key, value);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
const endPos = this.writer.getPos();
|
|
262
|
-
const chunkSize = endPos - startPos - 8;
|
|
263
|
-
this.writer.seek(startPos + 4);
|
|
264
|
-
this.riffWriter.writeU32(chunkSize);
|
|
265
|
-
this.writer.seek(endPos);
|
|
266
|
-
// Add padding byte if chunk size is odd
|
|
267
|
-
if (chunkSize & 1) {
|
|
268
|
-
this.writer.write(new Uint8Array(1));
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
writeId3Chunk(metadata) {
|
|
272
|
-
const startPos = this.writer.getPos();
|
|
273
|
-
// Write RIFF chunk header
|
|
274
|
-
this.riffWriter.writeAscii('ID3 ');
|
|
275
|
-
this.riffWriter.writeU32(0); // Size placeholder
|
|
276
|
-
const id3Writer = new Id3V2Writer(this.writer);
|
|
277
|
-
const id3TagSize = id3Writer.writeId3V2Tag(metadata);
|
|
278
|
-
const endPos = this.writer.getPos();
|
|
279
|
-
// Update RIFF chunk size
|
|
280
|
-
this.writer.seek(startPos + 4);
|
|
281
|
-
this.riffWriter.writeU32(id3TagSize);
|
|
282
|
-
this.writer.seek(endPos);
|
|
283
|
-
// Add padding byte if chunk size is odd
|
|
284
|
-
if (id3TagSize & 1) {
|
|
285
|
-
this.writer.write(new Uint8Array(1));
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
async finalize() {
|
|
289
|
-
const release = await this.mutex.acquire();
|
|
290
|
-
const endPos = this.writer.getPos();
|
|
291
|
-
if (this.isRf64) {
|
|
292
|
-
// Write riff size
|
|
293
|
-
assert(this.ds64RiffSizePos !== null);
|
|
294
|
-
this.writer.seek(this.ds64RiffSizePos);
|
|
295
|
-
this.riffWriter.writeU64(endPos - 8);
|
|
296
|
-
// Write data size
|
|
297
|
-
assert(this.ds64DataSizePos !== null);
|
|
298
|
-
this.writer.seek(this.ds64DataSizePos);
|
|
299
|
-
this.riffWriter.writeU64(this.dataSize);
|
|
300
|
-
// Write sample count
|
|
301
|
-
assert(this.ds64SampleCountPos !== null);
|
|
302
|
-
this.writer.seek(this.ds64SampleCountPos);
|
|
303
|
-
this.riffWriter.writeU64(this.sampleCount);
|
|
304
|
-
}
|
|
305
|
-
else {
|
|
306
|
-
// Write file size
|
|
307
|
-
assert(this.riffSizePos !== null);
|
|
308
|
-
this.writer.seek(this.riffSizePos);
|
|
309
|
-
this.riffWriter.writeU32(endPos - 8);
|
|
310
|
-
// Write data chunk size
|
|
311
|
-
assert(this.dataSizePos !== null);
|
|
312
|
-
this.writer.seek(this.dataSizePos);
|
|
313
|
-
this.riffWriter.writeU32(this.dataSize);
|
|
314
|
-
}
|
|
315
|
-
this.writer.seek(endPos);
|
|
316
|
-
release();
|
|
317
|
-
}
|
|
318
|
-
}
|
|
@@ -1,370 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
3
|
-
*
|
|
4
|
-
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
5
|
-
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
6
|
-
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7
|
-
*/
|
|
8
|
-
import { assert } from './misc.js';
|
|
9
|
-
export class Writer {
|
|
10
|
-
constructor() {
|
|
11
|
-
/** Setting this to true will cause the writer to ensure data is written in a strictly monotonic, streamable way. */
|
|
12
|
-
this.ensureMonotonicity = false;
|
|
13
|
-
this.trackedWrites = null;
|
|
14
|
-
this.trackedStart = -1;
|
|
15
|
-
this.trackedEnd = -1;
|
|
16
|
-
}
|
|
17
|
-
start() { }
|
|
18
|
-
maybeTrackWrites(data) {
|
|
19
|
-
if (!this.trackedWrites) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
// Handle negative relative write positions
|
|
23
|
-
let pos = this.getPos();
|
|
24
|
-
if (pos < this.trackedStart) {
|
|
25
|
-
if (pos + data.byteLength <= this.trackedStart) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
data = data.subarray(this.trackedStart - pos);
|
|
29
|
-
pos = 0;
|
|
30
|
-
}
|
|
31
|
-
const neededSize = pos + data.byteLength - this.trackedStart;
|
|
32
|
-
let newLength = this.trackedWrites.byteLength;
|
|
33
|
-
while (newLength < neededSize) {
|
|
34
|
-
newLength *= 2;
|
|
35
|
-
}
|
|
36
|
-
// Check if we need to resize the buffer
|
|
37
|
-
if (newLength !== this.trackedWrites.byteLength) {
|
|
38
|
-
const copy = new Uint8Array(newLength);
|
|
39
|
-
copy.set(this.trackedWrites, 0);
|
|
40
|
-
this.trackedWrites = copy;
|
|
41
|
-
}
|
|
42
|
-
this.trackedWrites.set(data, pos - this.trackedStart);
|
|
43
|
-
this.trackedEnd = Math.max(this.trackedEnd, pos + data.byteLength);
|
|
44
|
-
}
|
|
45
|
-
startTrackingWrites() {
|
|
46
|
-
this.trackedWrites = new Uint8Array(2 ** 10);
|
|
47
|
-
this.trackedStart = this.getPos();
|
|
48
|
-
this.trackedEnd = this.trackedStart;
|
|
49
|
-
}
|
|
50
|
-
stopTrackingWrites() {
|
|
51
|
-
if (!this.trackedWrites) {
|
|
52
|
-
throw new Error('Internal error: Can\'t get tracked writes since nothing was tracked.');
|
|
53
|
-
}
|
|
54
|
-
const slice = this.trackedWrites.subarray(0, this.trackedEnd - this.trackedStart);
|
|
55
|
-
const result = {
|
|
56
|
-
data: slice,
|
|
57
|
-
start: this.trackedStart,
|
|
58
|
-
end: this.trackedEnd,
|
|
59
|
-
};
|
|
60
|
-
this.trackedWrites = null;
|
|
61
|
-
return result;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const ARRAY_BUFFER_INITIAL_SIZE = /* #__PURE__ */ 2 ** 16;
|
|
65
|
-
const ARRAY_BUFFER_MAX_SIZE = /* #__PURE__ */ 2 ** 32;
|
|
66
|
-
export class BufferTargetWriter extends Writer {
|
|
67
|
-
constructor(target) {
|
|
68
|
-
super();
|
|
69
|
-
this.pos = 0;
|
|
70
|
-
this.maxPos = 0;
|
|
71
|
-
this.target = target;
|
|
72
|
-
this.supportsResize = 'resize' in new ArrayBuffer(0);
|
|
73
|
-
if (this.supportsResize) {
|
|
74
|
-
try {
|
|
75
|
-
// @ts-expect-error Don't want to bump "lib" in tsconfig
|
|
76
|
-
this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE, { maxByteLength: ARRAY_BUFFER_MAX_SIZE });
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE);
|
|
80
|
-
this.supportsResize = false;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE);
|
|
85
|
-
}
|
|
86
|
-
this.bytes = new Uint8Array(this.buffer);
|
|
87
|
-
}
|
|
88
|
-
ensureSize(size) {
|
|
89
|
-
let newLength = this.buffer.byteLength;
|
|
90
|
-
while (newLength < size)
|
|
91
|
-
newLength *= 2;
|
|
92
|
-
if (newLength === this.buffer.byteLength)
|
|
93
|
-
return;
|
|
94
|
-
if (newLength > ARRAY_BUFFER_MAX_SIZE) {
|
|
95
|
-
throw new Error(`ArrayBuffer exceeded maximum size of ${ARRAY_BUFFER_MAX_SIZE} bytes. Please consider using another`
|
|
96
|
-
+ ` target.`);
|
|
97
|
-
}
|
|
98
|
-
if (this.supportsResize) {
|
|
99
|
-
// Use resize if it exists
|
|
100
|
-
// @ts-expect-error Don't want to bump "lib" in tsconfig
|
|
101
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
|
|
102
|
-
this.buffer.resize(newLength);
|
|
103
|
-
// The Uint8Array scales automatically
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
const newBuffer = new ArrayBuffer(newLength);
|
|
107
|
-
const newBytes = new Uint8Array(newBuffer);
|
|
108
|
-
newBytes.set(this.bytes, 0);
|
|
109
|
-
this.buffer = newBuffer;
|
|
110
|
-
this.bytes = newBytes;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
write(data) {
|
|
114
|
-
this.maybeTrackWrites(data);
|
|
115
|
-
this.ensureSize(this.pos + data.byteLength);
|
|
116
|
-
this.bytes.set(data, this.pos);
|
|
117
|
-
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
|
118
|
-
this.pos += data.byteLength;
|
|
119
|
-
this.maxPos = Math.max(this.maxPos, this.pos);
|
|
120
|
-
}
|
|
121
|
-
seek(newPos) {
|
|
122
|
-
this.pos = newPos;
|
|
123
|
-
}
|
|
124
|
-
getPos() {
|
|
125
|
-
return this.pos;
|
|
126
|
-
}
|
|
127
|
-
async flush() { }
|
|
128
|
-
async finalize() {
|
|
129
|
-
this.ensureSize(this.pos);
|
|
130
|
-
this.target.buffer = this.buffer.slice(0, Math.max(this.maxPos, this.pos));
|
|
131
|
-
}
|
|
132
|
-
async close() { }
|
|
133
|
-
getSlice(start, end) {
|
|
134
|
-
return this.bytes.slice(start, end);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
const DEFAULT_CHUNK_SIZE = /* #__PURE__ */ 2 ** 24;
|
|
138
|
-
const MAX_CHUNKS_AT_ONCE = 2;
|
|
139
|
-
/**
|
|
140
|
-
* Writes to a StreamTarget every time it is flushed, sending out all of the new data written since the
|
|
141
|
-
* last flush. This is useful for streaming applications, like piping the output to disk. When using the chunked mode,
|
|
142
|
-
* data will first be accumulated in larger chunks, and then the entire chunk will be flushed out at once when ready.
|
|
143
|
-
*/
|
|
144
|
-
export class StreamTargetWriter extends Writer {
|
|
145
|
-
constructor(target) {
|
|
146
|
-
super();
|
|
147
|
-
this.pos = 0;
|
|
148
|
-
this.sections = [];
|
|
149
|
-
this.lastWriteEnd = 0;
|
|
150
|
-
this.lastFlushEnd = 0;
|
|
151
|
-
this.writer = null;
|
|
152
|
-
/**
|
|
153
|
-
* The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.
|
|
154
|
-
* A chunk is flushed if all of its contents have been written.
|
|
155
|
-
*/
|
|
156
|
-
this.chunks = [];
|
|
157
|
-
this.target = target;
|
|
158
|
-
this.chunked = target._options.chunked ?? false;
|
|
159
|
-
this.chunkSize = target._options.chunkSize ?? DEFAULT_CHUNK_SIZE;
|
|
160
|
-
}
|
|
161
|
-
start() {
|
|
162
|
-
this.writer = this.target._writable.getWriter();
|
|
163
|
-
}
|
|
164
|
-
write(data) {
|
|
165
|
-
if (this.pos > this.lastWriteEnd) {
|
|
166
|
-
const paddingBytesNeeded = this.pos - this.lastWriteEnd;
|
|
167
|
-
this.pos = this.lastWriteEnd;
|
|
168
|
-
this.write(new Uint8Array(paddingBytesNeeded));
|
|
169
|
-
}
|
|
170
|
-
this.maybeTrackWrites(data);
|
|
171
|
-
this.sections.push({
|
|
172
|
-
data: data.slice(),
|
|
173
|
-
start: this.pos,
|
|
174
|
-
});
|
|
175
|
-
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
|
176
|
-
this.pos += data.byteLength;
|
|
177
|
-
this.lastWriteEnd = Math.max(this.lastWriteEnd, this.pos);
|
|
178
|
-
}
|
|
179
|
-
seek(newPos) {
|
|
180
|
-
this.pos = newPos;
|
|
181
|
-
}
|
|
182
|
-
getPos() {
|
|
183
|
-
return this.pos;
|
|
184
|
-
}
|
|
185
|
-
async flush() {
|
|
186
|
-
if (this.pos > this.lastWriteEnd) {
|
|
187
|
-
// There's a "void" between the last written byte and the next byte we're about to write. Let's pad that
|
|
188
|
-
// void with zeroes explicitly.
|
|
189
|
-
const paddingBytesNeeded = this.pos - this.lastWriteEnd;
|
|
190
|
-
this.pos = this.lastWriteEnd;
|
|
191
|
-
this.write(new Uint8Array(paddingBytesNeeded));
|
|
192
|
-
}
|
|
193
|
-
assert(this.writer);
|
|
194
|
-
if (this.sections.length === 0)
|
|
195
|
-
return;
|
|
196
|
-
const chunks = [];
|
|
197
|
-
const sorted = [...this.sections].sort((a, b) => a.start - b.start);
|
|
198
|
-
chunks.push({
|
|
199
|
-
start: sorted[0].start,
|
|
200
|
-
size: sorted[0].data.byteLength,
|
|
201
|
-
});
|
|
202
|
-
// Figure out how many contiguous chunks we have
|
|
203
|
-
for (let i = 1; i < sorted.length; i++) {
|
|
204
|
-
const lastChunk = chunks[chunks.length - 1];
|
|
205
|
-
const section = sorted[i];
|
|
206
|
-
if (section.start <= lastChunk.start + lastChunk.size) {
|
|
207
|
-
lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
chunks.push({
|
|
211
|
-
start: section.start,
|
|
212
|
-
size: section.data.byteLength,
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
for (const chunk of chunks) {
|
|
217
|
-
chunk.data = new Uint8Array(chunk.size);
|
|
218
|
-
// Make sure to write the data in the correct order for correct overwriting
|
|
219
|
-
for (const section of this.sections) {
|
|
220
|
-
// Check if the section is in the chunk
|
|
221
|
-
if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {
|
|
222
|
-
chunk.data.set(section.data, section.start - chunk.start);
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
if (this.writer.desiredSize !== null && this.writer.desiredSize <= 0) {
|
|
226
|
-
await this.writer.ready; // Allow the writer to apply backpressure
|
|
227
|
-
}
|
|
228
|
-
if (this.chunked) {
|
|
229
|
-
// Let's first gather the data into bigger chunks before writing it
|
|
230
|
-
this.writeDataIntoChunks(chunk.data, chunk.start);
|
|
231
|
-
this.tryToFlushChunks();
|
|
232
|
-
}
|
|
233
|
-
else {
|
|
234
|
-
if (this.ensureMonotonicity && chunk.start !== this.lastFlushEnd) {
|
|
235
|
-
throw new Error('Internal error: Monotonicity violation.');
|
|
236
|
-
}
|
|
237
|
-
// Write out the data immediately
|
|
238
|
-
void this.writer.write({
|
|
239
|
-
type: 'write',
|
|
240
|
-
data: chunk.data,
|
|
241
|
-
position: chunk.start,
|
|
242
|
-
});
|
|
243
|
-
this.lastFlushEnd = chunk.start + chunk.data.byteLength;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
this.sections.length = 0;
|
|
247
|
-
}
|
|
248
|
-
writeDataIntoChunks(data, position) {
|
|
249
|
-
// First, find the chunk to write the data into, or create one if none exists
|
|
250
|
-
let chunkIndex = this.chunks.findIndex(x => x.start <= position && position < x.start + this.chunkSize);
|
|
251
|
-
if (chunkIndex === -1)
|
|
252
|
-
chunkIndex = this.createChunk(position);
|
|
253
|
-
const chunk = this.chunks[chunkIndex];
|
|
254
|
-
// Figure out how much to write to the chunk, and then write to the chunk
|
|
255
|
-
const relativePosition = position - chunk.start;
|
|
256
|
-
const toWrite = data.subarray(0, Math.min(this.chunkSize - relativePosition, data.byteLength));
|
|
257
|
-
chunk.data.set(toWrite, relativePosition);
|
|
258
|
-
// Create a section describing the region of data that was just written to
|
|
259
|
-
const section = {
|
|
260
|
-
start: relativePosition,
|
|
261
|
-
end: relativePosition + toWrite.byteLength,
|
|
262
|
-
};
|
|
263
|
-
this.insertSectionIntoChunk(chunk, section);
|
|
264
|
-
// Queue chunk for flushing to target if it has been fully written to
|
|
265
|
-
if (chunk.written[0].start === 0 && chunk.written[0].end === this.chunkSize) {
|
|
266
|
-
chunk.shouldFlush = true;
|
|
267
|
-
}
|
|
268
|
-
// Make sure we don't hold too many chunks in memory at once to keep memory usage down
|
|
269
|
-
if (this.chunks.length > MAX_CHUNKS_AT_ONCE) {
|
|
270
|
-
// Flush all but the last chunk
|
|
271
|
-
for (let i = 0; i < this.chunks.length - 1; i++) {
|
|
272
|
-
this.chunks[i].shouldFlush = true;
|
|
273
|
-
}
|
|
274
|
-
this.tryToFlushChunks();
|
|
275
|
-
}
|
|
276
|
-
// If the data didn't fit in one chunk, recurse with the remaining data
|
|
277
|
-
if (toWrite.byteLength < data.byteLength) {
|
|
278
|
-
this.writeDataIntoChunks(data.subarray(toWrite.byteLength), position + toWrite.byteLength);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
insertSectionIntoChunk(chunk, section) {
|
|
282
|
-
let low = 0;
|
|
283
|
-
let high = chunk.written.length - 1;
|
|
284
|
-
let index = -1;
|
|
285
|
-
// Do a binary search to find the last section with a start not larger than `section`'s start
|
|
286
|
-
while (low <= high) {
|
|
287
|
-
const mid = Math.floor(low + (high - low + 1) / 2);
|
|
288
|
-
if (chunk.written[mid].start <= section.start) {
|
|
289
|
-
low = mid + 1;
|
|
290
|
-
index = mid;
|
|
291
|
-
}
|
|
292
|
-
else {
|
|
293
|
-
high = mid - 1;
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
// Insert the new section
|
|
297
|
-
chunk.written.splice(index + 1, 0, section);
|
|
298
|
-
if (index === -1 || chunk.written[index].end < section.start)
|
|
299
|
-
index++;
|
|
300
|
-
// Merge overlapping sections
|
|
301
|
-
while (index < chunk.written.length - 1 && chunk.written[index].end >= chunk.written[index + 1].start) {
|
|
302
|
-
chunk.written[index].end = Math.max(chunk.written[index].end, chunk.written[index + 1].end);
|
|
303
|
-
chunk.written.splice(index + 1, 1);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
createChunk(includesPosition) {
|
|
307
|
-
const start = Math.floor(includesPosition / this.chunkSize) * this.chunkSize;
|
|
308
|
-
const chunk = {
|
|
309
|
-
start,
|
|
310
|
-
data: new Uint8Array(this.chunkSize),
|
|
311
|
-
written: [],
|
|
312
|
-
shouldFlush: false,
|
|
313
|
-
};
|
|
314
|
-
this.chunks.push(chunk);
|
|
315
|
-
this.chunks.sort((a, b) => a.start - b.start);
|
|
316
|
-
return this.chunks.indexOf(chunk);
|
|
317
|
-
}
|
|
318
|
-
tryToFlushChunks(force = false) {
|
|
319
|
-
assert(this.writer);
|
|
320
|
-
for (let i = 0; i < this.chunks.length; i++) {
|
|
321
|
-
const chunk = this.chunks[i];
|
|
322
|
-
if (!chunk.shouldFlush && !force)
|
|
323
|
-
continue;
|
|
324
|
-
for (const section of chunk.written) {
|
|
325
|
-
const position = chunk.start + section.start;
|
|
326
|
-
if (this.ensureMonotonicity && position !== this.lastFlushEnd) {
|
|
327
|
-
throw new Error('Internal error: Monotonicity violation.');
|
|
328
|
-
}
|
|
329
|
-
void this.writer.write({
|
|
330
|
-
type: 'write',
|
|
331
|
-
data: chunk.data.subarray(section.start, section.end),
|
|
332
|
-
position,
|
|
333
|
-
});
|
|
334
|
-
this.lastFlushEnd = chunk.start + section.end;
|
|
335
|
-
}
|
|
336
|
-
this.chunks.splice(i--, 1);
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
finalize() {
|
|
340
|
-
if (this.chunked) {
|
|
341
|
-
this.tryToFlushChunks(true);
|
|
342
|
-
}
|
|
343
|
-
assert(this.writer);
|
|
344
|
-
return this.writer.close();
|
|
345
|
-
}
|
|
346
|
-
async close() {
|
|
347
|
-
return this.writer?.close();
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
export class NullTargetWriter extends Writer {
|
|
351
|
-
constructor(target) {
|
|
352
|
-
super();
|
|
353
|
-
this.target = target;
|
|
354
|
-
this.pos = 0;
|
|
355
|
-
}
|
|
356
|
-
write(data) {
|
|
357
|
-
this.maybeTrackWrites(data);
|
|
358
|
-
this.target.onwrite?.(this.pos, this.pos + data.byteLength);
|
|
359
|
-
this.pos += data.byteLength;
|
|
360
|
-
}
|
|
361
|
-
getPos() {
|
|
362
|
-
return this.pos;
|
|
363
|
-
}
|
|
364
|
-
seek(newPos) {
|
|
365
|
-
this.pos = newPos;
|
|
366
|
-
}
|
|
367
|
-
async flush() { }
|
|
368
|
-
async finalize() { }
|
|
369
|
-
async close() { }
|
|
370
|
-
}
|