mediabunny 1.0.2 → 1.0.4

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.
Files changed (188) hide show
  1. package/README.md +4 -4
  2. package/dist/mediabunny.d.ts +4 -2
  3. package/dist/modules/codec-data.d.ts +95 -0
  4. package/dist/modules/codec-data.d.ts.map +1 -0
  5. package/dist/modules/codec-data.js +1103 -0
  6. package/dist/modules/codec.d.ts +250 -0
  7. package/dist/modules/codec.d.ts.map +1 -0
  8. package/dist/modules/codec.js +1100 -0
  9. package/dist/modules/conversion.d.ts +158 -0
  10. package/dist/modules/conversion.d.ts.map +1 -0
  11. package/dist/modules/conversion.js +866 -0
  12. package/dist/modules/custom-coder.d.ts +119 -0
  13. package/dist/modules/custom-coder.d.ts.map +1 -0
  14. package/dist/modules/custom-coder.js +91 -0
  15. package/dist/modules/demuxer.d.ts +17 -0
  16. package/dist/modules/demuxer.d.ts.map +1 -0
  17. package/dist/modules/demuxer.js +12 -0
  18. package/dist/modules/index.d.ts +25 -0
  19. package/dist/modules/index.d.ts.map +1 -0
  20. package/dist/modules/index.js +24 -0
  21. package/dist/modules/input-format.d.ts +121 -0
  22. package/dist/modules/input-format.d.ts.map +1 -0
  23. package/dist/modules/input-format.js +333 -0
  24. package/dist/modules/input-track.d.ts +138 -0
  25. package/dist/modules/input-track.d.ts.map +1 -0
  26. package/dist/modules/input-track.js +230 -0
  27. package/dist/modules/input.d.ts +55 -0
  28. package/dist/modules/input.d.ts.map +1 -0
  29. package/dist/modules/input.js +104 -0
  30. package/dist/modules/isobmff/isobmff-boxes.d.ts +202 -0
  31. package/dist/modules/isobmff/isobmff-boxes.d.ts.map +1 -0
  32. package/dist/modules/isobmff/isobmff-boxes.js +1111 -0
  33. package/dist/modules/isobmff/isobmff-demuxer.d.ts +163 -0
  34. package/dist/modules/isobmff/isobmff-demuxer.d.ts.map +1 -0
  35. package/dist/modules/isobmff/isobmff-demuxer.js +2080 -0
  36. package/dist/modules/isobmff/isobmff-misc.d.ts +14 -0
  37. package/dist/modules/isobmff/isobmff-misc.d.ts.map +1 -0
  38. package/dist/modules/isobmff/isobmff-misc.js +20 -0
  39. package/dist/modules/isobmff/isobmff-muxer.d.ts +142 -0
  40. package/dist/modules/isobmff/isobmff-muxer.d.ts.map +1 -0
  41. package/dist/modules/isobmff/isobmff-muxer.js +858 -0
  42. package/dist/modules/isobmff/isobmff-reader.d.ts +36 -0
  43. package/dist/modules/isobmff/isobmff-reader.d.ts.map +1 -0
  44. package/dist/modules/isobmff/isobmff-reader.js +105 -0
  45. package/dist/modules/matroska/ebml.d.ts +156 -0
  46. package/dist/modules/matroska/ebml.d.ts.map +1 -0
  47. package/dist/modules/matroska/ebml.js +529 -0
  48. package/dist/modules/matroska/matroska-demuxer.d.ts +119 -0
  49. package/dist/modules/matroska/matroska-demuxer.d.ts.map +1 -0
  50. package/dist/modules/matroska/matroska-demuxer.js +1330 -0
  51. package/dist/modules/matroska/matroska-misc.d.ts +14 -0
  52. package/dist/modules/matroska/matroska-misc.d.ts.map +1 -0
  53. package/dist/modules/matroska/matroska-misc.js +20 -0
  54. package/dist/modules/matroska/matroska-muxer.d.ts +71 -0
  55. package/dist/modules/matroska/matroska-muxer.d.ts.map +1 -0
  56. package/dist/modules/matroska/matroska-muxer.js +729 -0
  57. package/dist/modules/media-sink.d.ts +272 -0
  58. package/dist/modules/media-sink.d.ts.map +1 -0
  59. package/dist/modules/media-sink.js +1264 -0
  60. package/dist/modules/media-source.d.ts +230 -0
  61. package/dist/modules/media-source.d.ts.map +1 -0
  62. package/dist/modules/media-source.js +1095 -0
  63. package/dist/modules/misc.d.ts +127 -0
  64. package/dist/modules/misc.d.ts.map +1 -0
  65. package/dist/modules/misc.js +462 -0
  66. package/dist/modules/mp3/mp3-demuxer.d.ts +32 -0
  67. package/dist/modules/mp3/mp3-demuxer.d.ts.map +1 -0
  68. package/dist/modules/mp3/mp3-demuxer.js +160 -0
  69. package/dist/modules/mp3/mp3-misc.d.ts +37 -0
  70. package/dist/modules/mp3/mp3-misc.d.ts.map +1 -0
  71. package/dist/modules/mp3/mp3-misc.js +122 -0
  72. package/dist/modules/mp3/mp3-muxer.d.ts +27 -0
  73. package/dist/modules/mp3/mp3-muxer.d.ts.map +1 -0
  74. package/dist/modules/mp3/mp3-muxer.js +112 -0
  75. package/dist/modules/mp3/mp3-reader.d.ts +25 -0
  76. package/dist/modules/mp3/mp3-reader.d.ts.map +1 -0
  77. package/dist/modules/mp3/mp3-reader.js +73 -0
  78. package/dist/modules/mp3/mp3-writer.d.ts +30 -0
  79. package/dist/modules/mp3/mp3-writer.d.ts.map +1 -0
  80. package/dist/modules/mp3/mp3-writer.js +72 -0
  81. package/dist/modules/muxer.d.ts +32 -0
  82. package/dist/modules/muxer.d.ts.map +1 -0
  83. package/dist/modules/muxer.js +50 -0
  84. package/dist/modules/ogg/ogg-demuxer.d.ts +53 -0
  85. package/dist/modules/ogg/ogg-demuxer.d.ts.map +1 -0
  86. package/dist/modules/ogg/ogg-demuxer.js +701 -0
  87. package/dist/modules/ogg/ogg-misc.d.ts +27 -0
  88. package/dist/modules/ogg/ogg-misc.d.ts.map +1 -0
  89. package/dist/modules/ogg/ogg-misc.js +78 -0
  90. package/dist/modules/ogg/ogg-muxer.d.ts +58 -0
  91. package/dist/modules/ogg/ogg-muxer.d.ts.map +1 -0
  92. package/dist/modules/ogg/ogg-muxer.js +345 -0
  93. package/dist/modules/ogg/ogg-reader.d.ts +37 -0
  94. package/dist/modules/ogg/ogg-reader.d.ts.map +1 -0
  95. package/dist/modules/ogg/ogg-reader.js +104 -0
  96. package/dist/modules/output-format.d.ts +285 -0
  97. package/dist/modules/output-format.d.ts.map +1 -0
  98. package/dist/modules/output-format.js +385 -0
  99. package/dist/modules/output.d.ts +134 -0
  100. package/dist/modules/output.d.ts.map +1 -0
  101. package/dist/modules/output.js +269 -0
  102. package/dist/modules/packet.d.ts +86 -0
  103. package/dist/modules/packet.d.ts.map +1 -0
  104. package/dist/modules/packet.js +133 -0
  105. package/dist/modules/pcm.d.ts +12 -0
  106. package/dist/modules/pcm.d.ts.map +1 -0
  107. package/dist/modules/pcm.js +85 -0
  108. package/dist/modules/reader.d.ts +40 -0
  109. package/dist/modules/reader.d.ts.map +1 -0
  110. package/dist/modules/reader.js +153 -0
  111. package/dist/modules/sample.d.ts +186 -0
  112. package/dist/modules/sample.d.ts.map +1 -0
  113. package/dist/modules/sample.js +798 -0
  114. package/dist/modules/source.d.ts +76 -0
  115. package/dist/modules/source.d.ts.map +1 -0
  116. package/dist/modules/source.js +183 -0
  117. package/dist/modules/subtitles.d.ts +37 -0
  118. package/dist/modules/subtitles.d.ts.map +1 -0
  119. package/dist/modules/subtitles.js +89 -0
  120. package/dist/modules/target.d.ts +58 -0
  121. package/dist/modules/target.d.ts.map +1 -0
  122. package/dist/modules/target.js +63 -0
  123. package/dist/modules/wave/riff-reader.d.ts +19 -0
  124. package/dist/modules/wave/riff-reader.d.ts.map +1 -0
  125. package/dist/modules/wave/riff-reader.js +38 -0
  126. package/dist/modules/wave/riff-writer.d.ts +18 -0
  127. package/dist/modules/wave/riff-writer.d.ts.map +1 -0
  128. package/dist/modules/wave/riff-writer.js +25 -0
  129. package/dist/modules/wave/wave-demuxer.d.ts +42 -0
  130. package/dist/modules/wave/wave-demuxer.d.ts.map +1 -0
  131. package/dist/modules/wave/wave-demuxer.js +239 -0
  132. package/dist/modules/wave/wave-muxer.d.ts +27 -0
  133. package/dist/modules/wave/wave-muxer.d.ts.map +1 -0
  134. package/dist/modules/wave/wave-muxer.js +108 -0
  135. package/dist/modules/writer.d.ts +85 -0
  136. package/dist/modules/writer.d.ts.map +1 -0
  137. package/dist/modules/writer.js +347 -0
  138. package/package.json +14 -10
  139. package/src/codec-data.ts +1394 -0
  140. package/src/codec.ts +1392 -0
  141. package/src/conversion.ts +1199 -0
  142. package/src/custom-coder.ts +162 -0
  143. package/src/demuxer.ts +22 -0
  144. package/src/index.ts +148 -0
  145. package/src/input-format.ts +386 -0
  146. package/src/input-track.ts +341 -0
  147. package/src/input.ts +138 -0
  148. package/src/isobmff/isobmff-boxes.ts +1365 -0
  149. package/src/isobmff/isobmff-demuxer.ts +2646 -0
  150. package/src/isobmff/isobmff-misc.ts +29 -0
  151. package/src/isobmff/isobmff-muxer.ts +1210 -0
  152. package/src/isobmff/isobmff-reader.ts +137 -0
  153. package/src/matroska/ebml.ts +595 -0
  154. package/src/matroska/matroska-demuxer.ts +1677 -0
  155. package/src/matroska/matroska-misc.ts +29 -0
  156. package/src/matroska/matroska-muxer.ts +999 -0
  157. package/src/media-sink.ts +1577 -0
  158. package/src/media-source.ts +1406 -0
  159. package/src/misc.ts +584 -0
  160. package/src/mp3/mp3-demuxer.ts +228 -0
  161. package/src/mp3/mp3-misc.ts +154 -0
  162. package/src/mp3/mp3-muxer.ts +153 -0
  163. package/src/mp3/mp3-reader.ts +93 -0
  164. package/src/mp3/mp3-writer.ts +112 -0
  165. package/src/muxer.ts +88 -0
  166. package/src/ogg/ogg-demuxer.ts +985 -0
  167. package/src/ogg/ogg-misc.ts +116 -0
  168. package/src/ogg/ogg-muxer.ts +480 -0
  169. package/src/ogg/ogg-reader.ts +143 -0
  170. package/src/output-format.ts +689 -0
  171. package/src/output.ts +427 -0
  172. package/src/packet.ts +180 -0
  173. package/src/pcm.ts +112 -0
  174. package/src/reader.ts +205 -0
  175. package/src/sample.ts +1029 -0
  176. package/src/source.ts +279 -0
  177. package/src/subtitles.ts +131 -0
  178. package/src/target.ts +106 -0
  179. package/src/tsconfig.json +13 -0
  180. package/src/wave/riff-reader.ts +48 -0
  181. package/src/wave/riff-writer.ts +30 -0
  182. package/src/wave/wave-demuxer.ts +317 -0
  183. package/src/wave/wave-muxer.ts +145 -0
  184. package/src/writer.ts +461 -0
  185. /package/dist/{mediabunny.js → bundles/mediabunny.cjs} +0 -0
  186. /package/dist/{mediabunny.min.js → bundles/mediabunny.min.cjs} +0 -0
  187. /package/dist/{mediabunny.min.mjs → bundles/mediabunny.min.mjs} +0 -0
  188. /package/dist/{mediabunny.mjs → bundles/mediabunny.mjs} +0 -0
@@ -0,0 +1,347 @@
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 = 2 ** 16;
65
+ const ARRAY_BUFFER_MAX_SIZE = 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.pos += data.byteLength;
118
+ this.maxPos = Math.max(this.maxPos, this.pos);
119
+ }
120
+ seek(newPos) {
121
+ this.pos = newPos;
122
+ }
123
+ getPos() {
124
+ return this.pos;
125
+ }
126
+ async flush() { }
127
+ async finalize() {
128
+ this.ensureSize(this.pos);
129
+ this.target.buffer = this.buffer.slice(0, Math.max(this.maxPos, this.pos));
130
+ }
131
+ async close() { }
132
+ getSlice(start, end) {
133
+ return this.bytes.slice(start, end);
134
+ }
135
+ }
136
+ const DEFAULT_CHUNK_SIZE = 2 ** 24;
137
+ const MAX_CHUNKS_AT_ONCE = 2;
138
+ /**
139
+ * Writes to a StreamTarget every time it is flushed, sending out all of the new data written since the
140
+ * last flush. This is useful for streaming applications, like piping the output to disk. When using the chunked mode,
141
+ * data will first be accumulated in larger chunks, and then the entire chunk will be flushed out at once when ready.
142
+ */
143
+ export class StreamTargetWriter extends Writer {
144
+ constructor(target) {
145
+ super();
146
+ this.pos = 0;
147
+ this.sections = [];
148
+ this.lastWriteEnd = 0;
149
+ this.lastFlushEnd = 0;
150
+ this.writer = null;
151
+ /**
152
+ * The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.
153
+ * A chunk is flushed if all of its contents have been written.
154
+ */
155
+ this.chunks = [];
156
+ this.target = target;
157
+ this.chunked = target._options.chunked ?? false;
158
+ this.chunkSize = target._options.chunkSize ?? DEFAULT_CHUNK_SIZE;
159
+ }
160
+ start() {
161
+ this.writer = this.target._writable.getWriter();
162
+ }
163
+ write(data) {
164
+ if (this.pos > this.lastWriteEnd) {
165
+ const paddingBytesNeeded = this.pos - this.lastWriteEnd;
166
+ this.pos = this.lastWriteEnd;
167
+ this.write(new Uint8Array(paddingBytesNeeded));
168
+ }
169
+ this.maybeTrackWrites(data);
170
+ this.sections.push({
171
+ data: data.slice(),
172
+ start: this.pos,
173
+ });
174
+ this.pos += data.byteLength;
175
+ this.lastWriteEnd = Math.max(this.lastWriteEnd, this.pos);
176
+ }
177
+ seek(newPos) {
178
+ this.pos = newPos;
179
+ }
180
+ getPos() {
181
+ return this.pos;
182
+ }
183
+ async flush() {
184
+ if (this.pos > this.lastWriteEnd) {
185
+ // There's a "void" between the last written byte and the next byte we're about to write. Let's pad that
186
+ // void with zeroes explicitly.
187
+ const paddingBytesNeeded = this.pos - this.lastWriteEnd;
188
+ this.pos = this.lastWriteEnd;
189
+ this.write(new Uint8Array(paddingBytesNeeded));
190
+ }
191
+ assert(this.writer);
192
+ if (this.sections.length === 0)
193
+ return;
194
+ const chunks = [];
195
+ const sorted = [...this.sections].sort((a, b) => a.start - b.start);
196
+ chunks.push({
197
+ start: sorted[0].start,
198
+ size: sorted[0].data.byteLength,
199
+ });
200
+ // Figure out how many contiguous chunks we have
201
+ for (let i = 1; i < sorted.length; i++) {
202
+ const lastChunk = chunks[chunks.length - 1];
203
+ const section = sorted[i];
204
+ if (section.start <= lastChunk.start + lastChunk.size) {
205
+ lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);
206
+ }
207
+ else {
208
+ chunks.push({
209
+ start: section.start,
210
+ size: section.data.byteLength,
211
+ });
212
+ }
213
+ }
214
+ for (const chunk of chunks) {
215
+ chunk.data = new Uint8Array(chunk.size);
216
+ // Make sure to write the data in the correct order for correct overwriting
217
+ for (const section of this.sections) {
218
+ // Check if the section is in the chunk
219
+ if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {
220
+ chunk.data.set(section.data, section.start - chunk.start);
221
+ }
222
+ }
223
+ if (this.writer.desiredSize !== null && this.writer.desiredSize <= 0) {
224
+ await this.writer.ready; // Allow the writer to apply backpressure
225
+ }
226
+ if (this.chunked) {
227
+ // Let's first gather the data into bigger chunks before writing it
228
+ this.writeDataIntoChunks(chunk.data, chunk.start);
229
+ this.tryToFlushChunks();
230
+ }
231
+ else {
232
+ if (this.ensureMonotonicity && chunk.start !== this.lastFlushEnd) {
233
+ throw new Error('Internal error: Monotonicity violation.');
234
+ }
235
+ // Write out the data immediately
236
+ void this.writer.write({
237
+ type: 'write',
238
+ data: chunk.data,
239
+ position: chunk.start,
240
+ });
241
+ this.lastFlushEnd = chunk.start + chunk.data.byteLength;
242
+ }
243
+ }
244
+ this.sections.length = 0;
245
+ }
246
+ writeDataIntoChunks(data, position) {
247
+ // First, find the chunk to write the data into, or create one if none exists
248
+ let chunkIndex = this.chunks.findIndex(x => x.start <= position && position < x.start + this.chunkSize);
249
+ if (chunkIndex === -1)
250
+ chunkIndex = this.createChunk(position);
251
+ const chunk = this.chunks[chunkIndex];
252
+ // Figure out how much to write to the chunk, and then write to the chunk
253
+ const relativePosition = position - chunk.start;
254
+ const toWrite = data.subarray(0, Math.min(this.chunkSize - relativePosition, data.byteLength));
255
+ chunk.data.set(toWrite, relativePosition);
256
+ // Create a section describing the region of data that was just written to
257
+ const section = {
258
+ start: relativePosition,
259
+ end: relativePosition + toWrite.byteLength,
260
+ };
261
+ this.insertSectionIntoChunk(chunk, section);
262
+ // Queue chunk for flushing to target if it has been fully written to
263
+ if (chunk.written[0].start === 0 && chunk.written[0].end === this.chunkSize) {
264
+ chunk.shouldFlush = true;
265
+ }
266
+ // Make sure we don't hold too many chunks in memory at once to keep memory usage down
267
+ if (this.chunks.length > MAX_CHUNKS_AT_ONCE) {
268
+ // Flush all but the last chunk
269
+ for (let i = 0; i < this.chunks.length - 1; i++) {
270
+ this.chunks[i].shouldFlush = true;
271
+ }
272
+ this.tryToFlushChunks();
273
+ }
274
+ // If the data didn't fit in one chunk, recurse with the remaining data
275
+ if (toWrite.byteLength < data.byteLength) {
276
+ this.writeDataIntoChunks(data.subarray(toWrite.byteLength), position + toWrite.byteLength);
277
+ }
278
+ }
279
+ insertSectionIntoChunk(chunk, section) {
280
+ let low = 0;
281
+ let high = chunk.written.length - 1;
282
+ let index = -1;
283
+ // Do a binary search to find the last section with a start not larger than `section`'s start
284
+ while (low <= high) {
285
+ const mid = Math.floor(low + (high - low + 1) / 2);
286
+ if (chunk.written[mid].start <= section.start) {
287
+ low = mid + 1;
288
+ index = mid;
289
+ }
290
+ else {
291
+ high = mid - 1;
292
+ }
293
+ }
294
+ // Insert the new section
295
+ chunk.written.splice(index + 1, 0, section);
296
+ if (index === -1 || chunk.written[index].end < section.start)
297
+ index++;
298
+ // Merge overlapping sections
299
+ while (index < chunk.written.length - 1 && chunk.written[index].end >= chunk.written[index + 1].start) {
300
+ chunk.written[index].end = Math.max(chunk.written[index].end, chunk.written[index + 1].end);
301
+ chunk.written.splice(index + 1, 1);
302
+ }
303
+ }
304
+ createChunk(includesPosition) {
305
+ const start = Math.floor(includesPosition / this.chunkSize) * this.chunkSize;
306
+ const chunk = {
307
+ start,
308
+ data: new Uint8Array(this.chunkSize),
309
+ written: [],
310
+ shouldFlush: false,
311
+ };
312
+ this.chunks.push(chunk);
313
+ this.chunks.sort((a, b) => a.start - b.start);
314
+ return this.chunks.indexOf(chunk);
315
+ }
316
+ tryToFlushChunks(force = false) {
317
+ assert(this.writer);
318
+ for (let i = 0; i < this.chunks.length; i++) {
319
+ const chunk = this.chunks[i];
320
+ if (!chunk.shouldFlush && !force)
321
+ continue;
322
+ for (const section of chunk.written) {
323
+ const position = chunk.start + section.start;
324
+ if (this.ensureMonotonicity && position !== this.lastFlushEnd) {
325
+ throw new Error('Internal error: Monotonicity violation.');
326
+ }
327
+ void this.writer.write({
328
+ type: 'write',
329
+ data: chunk.data.subarray(section.start, section.end),
330
+ position,
331
+ });
332
+ this.lastFlushEnd = chunk.start + section.end;
333
+ }
334
+ this.chunks.splice(i--, 1);
335
+ }
336
+ }
337
+ finalize() {
338
+ if (this.chunked) {
339
+ this.tryToFlushChunks(true);
340
+ }
341
+ assert(this.writer);
342
+ return this.writer.close();
343
+ }
344
+ async close() {
345
+ return this.writer?.close();
346
+ }
347
+ }
package/package.json CHANGED
@@ -1,25 +1,27 @@
1
1
  {
2
2
  "name": "mediabunny",
3
3
  "author": "Vanilagy",
4
- "version": "1.0.2",
4
+ "version": "1.0.4",
5
5
  "description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.",
6
6
  "type": "module",
7
- "main": "./dist/mediabunny.js",
8
- "module": "./dist/mediabunny.mjs",
9
- "types": "./dist/mediabunny.d.ts",
7
+ "main": "./dist/bundles/mediabunny.cjs",
8
+ "module": "./dist/modules/index.js",
9
+ "types": "./dist/modules/index.d.ts",
10
10
  "exports": {
11
- "types": "./dist/mediabunny.d.ts",
12
- "import": "./dist/mediabunny.mjs",
13
- "require": "./dist/mediabunny.js"
11
+ "types": "./dist/modules/index.d.ts",
12
+ "import": "./dist/modules/index.js",
13
+ "require": "./dist/bundles/mediabunny.cjs"
14
14
  },
15
15
  "files": [
16
16
  "README.md",
17
17
  "package.json",
18
18
  "LICENSE",
19
- "dist"
19
+ "dist",
20
+ "src"
20
21
  ],
22
+ "sideEffects": false,
21
23
  "scripts": {
22
- "build": "tsx scripts/ensure-license-headers.ts && node build.mjs && tsc -p src && api-extractor run && npm run check-docblocks && tsx scripts/append-namespace.ts",
24
+ "build": "rm -rf dist && tsx scripts/ensure-license-headers.ts && tsc -p src && npm run fix-build-import-paths && node build.mjs && api-extractor run && npm run check-docblocks && npm run append-namespace",
23
25
  "watch": "node build.mjs --watch",
24
26
  "lint": "eslint .",
25
27
  "check": "tsc -p src --noEmit && tsc -p scripts --noEmit && tsc -p tsconfig.vite.json --noEmit && rm tsconfig.vite.tsbuildinfo",
@@ -28,7 +30,9 @@
28
30
  "docs:build": "vitepress build docs && npm run examples:build",
29
31
  "docs:preview": "vitepress preview docs",
30
32
  "dev": "vite",
31
- "examples:build": "vite build"
33
+ "examples:build": "vite build",
34
+ "fix-build-import-paths": "find dist -name \"*.js\" -type f -exec sed -i -r \"s/ from '([^']+)';/ from '\\1.js';/g\" {} \\;",
35
+ "append-namespace": "echo 'export as namespace Mediabunny;' >> dist/mediabunny.d.ts"
32
36
  },
33
37
  "license": "MPL-2.0",
34
38
  "repository": {