@kenzuya/mediabunny 1.26.0 → 1.28.5

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 (237) hide show
  1. package/README.md +1 -1
  2. package/dist/bundles/{mediabunny.mjs → mediabunny.js} +21963 -21388
  3. package/dist/bundles/mediabunny.min.js +490 -0
  4. package/dist/modules/shared/mp3-misc.d.ts.map +1 -1
  5. package/dist/modules/src/adts/adts-demuxer.d.ts +6 -6
  6. package/dist/modules/src/adts/adts-demuxer.d.ts.map +1 -1
  7. package/dist/modules/src/adts/adts-muxer.d.ts +4 -4
  8. package/dist/modules/src/adts/adts-muxer.d.ts.map +1 -1
  9. package/dist/modules/src/adts/adts-reader.d.ts +1 -1
  10. package/dist/modules/src/adts/adts-reader.d.ts.map +1 -1
  11. package/dist/modules/src/avi/avi-demuxer.d.ts +44 -0
  12. package/dist/modules/src/avi/avi-demuxer.d.ts.map +1 -0
  13. package/dist/modules/src/avi/avi-misc.d.ts +88 -0
  14. package/dist/modules/src/avi/avi-misc.d.ts.map +1 -0
  15. package/dist/modules/src/avi/avi-muxer.d.ts +45 -0
  16. package/dist/modules/src/avi/avi-muxer.d.ts.map +1 -0
  17. package/dist/modules/src/avi/riff-writer.d.ts +26 -0
  18. package/dist/modules/src/avi/riff-writer.d.ts.map +1 -0
  19. package/dist/modules/src/codec-data.d.ts +8 -3
  20. package/dist/modules/src/codec-data.d.ts.map +1 -1
  21. package/dist/modules/src/codec.d.ts +10 -10
  22. package/dist/modules/src/codec.d.ts.map +1 -1
  23. package/dist/modules/src/conversion.d.ts +33 -16
  24. package/dist/modules/src/conversion.d.ts.map +1 -1
  25. package/dist/modules/src/custom-coder.d.ts +8 -8
  26. package/dist/modules/src/custom-coder.d.ts.map +1 -1
  27. package/dist/modules/src/demuxer.d.ts +3 -3
  28. package/dist/modules/src/demuxer.d.ts.map +1 -1
  29. package/dist/modules/src/encode.d.ts +8 -8
  30. package/dist/modules/src/encode.d.ts.map +1 -1
  31. package/dist/modules/src/flac/flac-demuxer.d.ts +7 -7
  32. package/dist/modules/src/flac/flac-demuxer.d.ts.map +1 -1
  33. package/dist/modules/src/flac/flac-misc.d.ts +3 -3
  34. package/dist/modules/src/flac/flac-misc.d.ts.map +1 -1
  35. package/dist/modules/src/flac/flac-muxer.d.ts +5 -5
  36. package/dist/modules/src/flac/flac-muxer.d.ts.map +1 -1
  37. package/dist/modules/src/id3.d.ts +3 -3
  38. package/dist/modules/src/id3.d.ts.map +1 -1
  39. package/dist/modules/src/index.d.ts +20 -20
  40. package/dist/modules/src/index.d.ts.map +1 -1
  41. package/dist/modules/src/input-format.d.ts +22 -0
  42. package/dist/modules/src/input-format.d.ts.map +1 -1
  43. package/dist/modules/src/input-track.d.ts +8 -8
  44. package/dist/modules/src/input-track.d.ts.map +1 -1
  45. package/dist/modules/src/input.d.ts +12 -12
  46. package/dist/modules/src/isobmff/isobmff-boxes.d.ts +2 -2
  47. package/dist/modules/src/isobmff/isobmff-boxes.d.ts.map +1 -1
  48. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts +12 -12
  49. package/dist/modules/src/isobmff/isobmff-demuxer.d.ts.map +1 -1
  50. package/dist/modules/src/isobmff/isobmff-misc.d.ts.map +1 -1
  51. package/dist/modules/src/isobmff/isobmff-muxer.d.ts +11 -11
  52. package/dist/modules/src/isobmff/isobmff-muxer.d.ts.map +1 -1
  53. package/dist/modules/src/isobmff/isobmff-reader.d.ts +2 -2
  54. package/dist/modules/src/isobmff/isobmff-reader.d.ts.map +1 -1
  55. package/dist/modules/src/matroska/ebml.d.ts +3 -3
  56. package/dist/modules/src/matroska/ebml.d.ts.map +1 -1
  57. package/dist/modules/src/matroska/matroska-demuxer.d.ts +13 -13
  58. package/dist/modules/src/matroska/matroska-demuxer.d.ts.map +1 -1
  59. package/dist/modules/src/matroska/matroska-input.d.ts +33 -0
  60. package/dist/modules/src/matroska/matroska-input.d.ts.map +1 -0
  61. package/dist/modules/src/matroska/matroska-misc.d.ts.map +1 -1
  62. package/dist/modules/src/matroska/matroska-muxer.d.ts +5 -5
  63. package/dist/modules/src/matroska/matroska-muxer.d.ts.map +1 -1
  64. package/dist/modules/src/media-sink.d.ts +5 -5
  65. package/dist/modules/src/media-sink.d.ts.map +1 -1
  66. package/dist/modules/src/media-source.d.ts +22 -4
  67. package/dist/modules/src/media-source.d.ts.map +1 -1
  68. package/dist/modules/src/metadata.d.ts +2 -2
  69. package/dist/modules/src/metadata.d.ts.map +1 -1
  70. package/dist/modules/src/misc.d.ts +5 -4
  71. package/dist/modules/src/misc.d.ts.map +1 -1
  72. package/dist/modules/src/mp3/mp3-demuxer.d.ts +7 -7
  73. package/dist/modules/src/mp3/mp3-demuxer.d.ts.map +1 -1
  74. package/dist/modules/src/mp3/mp3-muxer.d.ts +4 -4
  75. package/dist/modules/src/mp3/mp3-muxer.d.ts.map +1 -1
  76. package/dist/modules/src/mp3/mp3-reader.d.ts +2 -2
  77. package/dist/modules/src/mp3/mp3-reader.d.ts.map +1 -1
  78. package/dist/modules/src/mp3/mp3-writer.d.ts +1 -1
  79. package/dist/modules/src/mp3/mp3-writer.d.ts.map +1 -1
  80. package/dist/modules/src/muxer.d.ts +4 -4
  81. package/dist/modules/src/muxer.d.ts.map +1 -1
  82. package/dist/modules/src/node.d.ts +1 -1
  83. package/dist/modules/src/ogg/ogg-demuxer.d.ts +7 -7
  84. package/dist/modules/src/ogg/ogg-demuxer.d.ts.map +1 -1
  85. package/dist/modules/src/ogg/ogg-misc.d.ts +1 -1
  86. package/dist/modules/src/ogg/ogg-misc.d.ts.map +1 -1
  87. package/dist/modules/src/ogg/ogg-muxer.d.ts +5 -5
  88. package/dist/modules/src/ogg/ogg-muxer.d.ts.map +1 -1
  89. package/dist/modules/src/ogg/ogg-reader.d.ts +1 -1
  90. package/dist/modules/src/ogg/ogg-reader.d.ts.map +1 -1
  91. package/dist/modules/src/output-format.d.ts +51 -6
  92. package/dist/modules/src/output-format.d.ts.map +1 -1
  93. package/dist/modules/src/output.d.ts +13 -13
  94. package/dist/modules/src/output.d.ts.map +1 -1
  95. package/dist/modules/src/packet.d.ts +1 -1
  96. package/dist/modules/src/packet.d.ts.map +1 -1
  97. package/dist/modules/src/pcm.d.ts.map +1 -1
  98. package/dist/modules/src/reader.d.ts +2 -2
  99. package/dist/modules/src/reader.d.ts.map +1 -1
  100. package/dist/modules/src/sample.d.ts +57 -15
  101. package/dist/modules/src/sample.d.ts.map +1 -1
  102. package/dist/modules/src/source.d.ts +3 -3
  103. package/dist/modules/src/source.d.ts.map +1 -1
  104. package/dist/modules/src/subtitles.d.ts +1 -1
  105. package/dist/modules/src/subtitles.d.ts.map +1 -1
  106. package/dist/modules/src/target.d.ts +2 -2
  107. package/dist/modules/src/target.d.ts.map +1 -1
  108. package/dist/modules/src/tsconfig.tsbuildinfo +1 -1
  109. package/dist/modules/src/wave/riff-writer.d.ts +1 -1
  110. package/dist/modules/src/wave/riff-writer.d.ts.map +1 -1
  111. package/dist/modules/src/wave/wave-demuxer.d.ts +6 -6
  112. package/dist/modules/src/wave/wave-demuxer.d.ts.map +1 -1
  113. package/dist/modules/src/wave/wave-muxer.d.ts +4 -4
  114. package/dist/modules/src/wave/wave-muxer.d.ts.map +1 -1
  115. package/dist/modules/src/writer.d.ts +1 -1
  116. package/dist/modules/src/writer.d.ts.map +1 -1
  117. package/dist/packages/eac3/eac3.wasm +0 -0
  118. package/dist/packages/eac3/mediabunny-eac3.js +1058 -0
  119. package/dist/packages/eac3/mediabunny-eac3.min.js +44 -0
  120. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.js +694 -0
  121. package/dist/packages/mp3-encoder/mediabunny-mp3-encoder.min.js +58 -0
  122. package/dist/packages/mpeg4/mediabunny-mpeg4.js +1198 -0
  123. package/dist/packages/mpeg4/mediabunny-mpeg4.min.js +44 -0
  124. package/dist/packages/mpeg4/xvid.wasm +0 -0
  125. package/package.json +18 -57
  126. package/dist/bundles/mediabunny.cjs +0 -26140
  127. package/dist/bundles/mediabunny.min.cjs +0 -147
  128. package/dist/bundles/mediabunny.min.mjs +0 -146
  129. package/dist/mediabunny.d.ts +0 -3319
  130. package/dist/modules/shared/mp3-misc.js +0 -147
  131. package/dist/modules/src/adts/adts-demuxer.js +0 -239
  132. package/dist/modules/src/adts/adts-muxer.js +0 -80
  133. package/dist/modules/src/adts/adts-reader.js +0 -63
  134. package/dist/modules/src/codec-data.js +0 -1730
  135. package/dist/modules/src/codec.js +0 -869
  136. package/dist/modules/src/conversion.js +0 -1459
  137. package/dist/modules/src/custom-coder.js +0 -117
  138. package/dist/modules/src/demuxer.js +0 -12
  139. package/dist/modules/src/encode.js +0 -442
  140. package/dist/modules/src/flac/flac-demuxer.js +0 -504
  141. package/dist/modules/src/flac/flac-misc.js +0 -135
  142. package/dist/modules/src/flac/flac-muxer.js +0 -222
  143. package/dist/modules/src/id3.js +0 -848
  144. package/dist/modules/src/index.js +0 -28
  145. package/dist/modules/src/input-format.js +0 -480
  146. package/dist/modules/src/input-track.js +0 -372
  147. package/dist/modules/src/input.js +0 -188
  148. package/dist/modules/src/isobmff/isobmff-boxes.js +0 -1480
  149. package/dist/modules/src/isobmff/isobmff-demuxer.js +0 -2618
  150. package/dist/modules/src/isobmff/isobmff-misc.js +0 -20
  151. package/dist/modules/src/isobmff/isobmff-muxer.js +0 -966
  152. package/dist/modules/src/isobmff/isobmff-reader.js +0 -72
  153. package/dist/modules/src/matroska/ebml.js +0 -653
  154. package/dist/modules/src/matroska/matroska-demuxer.js +0 -2133
  155. package/dist/modules/src/matroska/matroska-misc.js +0 -20
  156. package/dist/modules/src/matroska/matroska-muxer.js +0 -1017
  157. package/dist/modules/src/media-sink.js +0 -1736
  158. package/dist/modules/src/media-source.js +0 -1825
  159. package/dist/modules/src/metadata.js +0 -193
  160. package/dist/modules/src/misc.js +0 -623
  161. package/dist/modules/src/mp3/mp3-demuxer.js +0 -285
  162. package/dist/modules/src/mp3/mp3-muxer.js +0 -123
  163. package/dist/modules/src/mp3/mp3-reader.js +0 -26
  164. package/dist/modules/src/mp3/mp3-writer.js +0 -78
  165. package/dist/modules/src/muxer.js +0 -50
  166. package/dist/modules/src/node.js +0 -9
  167. package/dist/modules/src/ogg/ogg-demuxer.js +0 -763
  168. package/dist/modules/src/ogg/ogg-misc.js +0 -78
  169. package/dist/modules/src/ogg/ogg-muxer.js +0 -353
  170. package/dist/modules/src/ogg/ogg-reader.js +0 -65
  171. package/dist/modules/src/output-format.js +0 -527
  172. package/dist/modules/src/output.js +0 -300
  173. package/dist/modules/src/packet.js +0 -182
  174. package/dist/modules/src/pcm.js +0 -85
  175. package/dist/modules/src/reader.js +0 -236
  176. package/dist/modules/src/sample.js +0 -1056
  177. package/dist/modules/src/source.js +0 -1182
  178. package/dist/modules/src/subtitles.js +0 -575
  179. package/dist/modules/src/target.js +0 -140
  180. package/dist/modules/src/wave/riff-writer.js +0 -30
  181. package/dist/modules/src/wave/wave-demuxer.js +0 -447
  182. package/dist/modules/src/wave/wave-muxer.js +0 -318
  183. package/dist/modules/src/writer.js +0 -370
  184. package/src/adts/adts-demuxer.ts +0 -331
  185. package/src/adts/adts-muxer.ts +0 -111
  186. package/src/adts/adts-reader.ts +0 -85
  187. package/src/codec-data.ts +0 -2078
  188. package/src/codec.ts +0 -1092
  189. package/src/conversion.ts +0 -2112
  190. package/src/custom-coder.ts +0 -197
  191. package/src/demuxer.ts +0 -24
  192. package/src/encode.ts +0 -739
  193. package/src/flac/flac-demuxer.ts +0 -730
  194. package/src/flac/flac-misc.ts +0 -164
  195. package/src/flac/flac-muxer.ts +0 -320
  196. package/src/id3.ts +0 -925
  197. package/src/index.ts +0 -221
  198. package/src/input-format.ts +0 -541
  199. package/src/input-track.ts +0 -529
  200. package/src/input.ts +0 -235
  201. package/src/isobmff/isobmff-boxes.ts +0 -1719
  202. package/src/isobmff/isobmff-demuxer.ts +0 -3190
  203. package/src/isobmff/isobmff-misc.ts +0 -29
  204. package/src/isobmff/isobmff-muxer.ts +0 -1348
  205. package/src/isobmff/isobmff-reader.ts +0 -91
  206. package/src/matroska/ebml.ts +0 -730
  207. package/src/matroska/matroska-demuxer.ts +0 -2481
  208. package/src/matroska/matroska-misc.ts +0 -29
  209. package/src/matroska/matroska-muxer.ts +0 -1276
  210. package/src/media-sink.ts +0 -2179
  211. package/src/media-source.ts +0 -2243
  212. package/src/metadata.ts +0 -320
  213. package/src/misc.ts +0 -798
  214. package/src/mp3/mp3-demuxer.ts +0 -383
  215. package/src/mp3/mp3-muxer.ts +0 -166
  216. package/src/mp3/mp3-reader.ts +0 -34
  217. package/src/mp3/mp3-writer.ts +0 -120
  218. package/src/muxer.ts +0 -88
  219. package/src/node.ts +0 -11
  220. package/src/ogg/ogg-demuxer.ts +0 -1053
  221. package/src/ogg/ogg-misc.ts +0 -116
  222. package/src/ogg/ogg-muxer.ts +0 -497
  223. package/src/ogg/ogg-reader.ts +0 -93
  224. package/src/output-format.ts +0 -945
  225. package/src/output.ts +0 -488
  226. package/src/packet.ts +0 -263
  227. package/src/pcm.ts +0 -112
  228. package/src/reader.ts +0 -323
  229. package/src/sample.ts +0 -1461
  230. package/src/source.ts +0 -1688
  231. package/src/subtitles.ts +0 -711
  232. package/src/target.ts +0 -204
  233. package/src/tsconfig.json +0 -16
  234. package/src/wave/riff-writer.ts +0 -36
  235. package/src/wave/wave-demuxer.ts +0 -529
  236. package/src/wave/wave-muxer.ts +0 -371
  237. package/src/writer.ts +0 -490
package/src/writer.ts DELETED
@@ -1,490 +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
-
9
- import { BufferTarget, NullTarget, StreamTarget, StreamTargetChunk } from './target';
10
- import { assert } from './misc';
11
-
12
- export abstract class Writer {
13
- /** Setting this to true will cause the writer to ensure data is written in a strictly monotonic, streamable way. */
14
- ensureMonotonicity = false;
15
-
16
- start() {}
17
-
18
- /** Writes the given data to the target, at the current position. */
19
- abstract write(data: Uint8Array): void;
20
- /** Sets the current position for future writes to a new one. */
21
- abstract seek(newPos: number): void;
22
- /** Returns the current position. */
23
- abstract getPos(): number;
24
- /** Signals to the writer that it may be time to flush. */
25
- abstract flush(): Promise<void>;
26
- /** Called after muxing has finished. */
27
- abstract finalize(): Promise<void>;
28
- /** Closes the writer. */
29
- abstract close(): Promise<void>;
30
-
31
- private trackedWrites: Uint8Array | null = null;
32
- private trackedStart = -1;
33
- private trackedEnd = -1;
34
-
35
- protected maybeTrackWrites(data: Uint8Array) {
36
- if (!this.trackedWrites) {
37
- return;
38
- }
39
-
40
- // Handle negative relative write positions
41
- let pos = this.getPos();
42
- if (pos < this.trackedStart) {
43
- if (pos + data.byteLength <= this.trackedStart) {
44
- return;
45
- }
46
-
47
- data = data.subarray(this.trackedStart - pos);
48
- pos = 0;
49
- }
50
-
51
- const neededSize = pos + data.byteLength - this.trackedStart;
52
- let newLength = this.trackedWrites.byteLength;
53
- while (newLength < neededSize) {
54
- newLength *= 2;
55
- }
56
-
57
- // Check if we need to resize the buffer
58
- if (newLength !== this.trackedWrites.byteLength) {
59
- const copy = new Uint8Array(newLength);
60
- copy.set(this.trackedWrites, 0);
61
- this.trackedWrites = copy;
62
- }
63
-
64
- this.trackedWrites.set(data, pos - this.trackedStart);
65
- this.trackedEnd = Math.max(this.trackedEnd, pos + data.byteLength);
66
- }
67
-
68
- startTrackingWrites() {
69
- this.trackedWrites = new Uint8Array(2 ** 10);
70
- this.trackedStart = this.getPos();
71
- this.trackedEnd = this.trackedStart;
72
- }
73
-
74
- stopTrackingWrites() {
75
- if (!this.trackedWrites) {
76
- throw new Error('Internal error: Can\'t get tracked writes since nothing was tracked.');
77
- }
78
-
79
- const slice = this.trackedWrites.subarray(0, this.trackedEnd - this.trackedStart);
80
- const result = {
81
- data: slice,
82
- start: this.trackedStart,
83
- end: this.trackedEnd,
84
- };
85
-
86
- this.trackedWrites = null;
87
-
88
- return result;
89
- }
90
- }
91
-
92
- const ARRAY_BUFFER_INITIAL_SIZE = /* #__PURE__ */ 2 ** 16;
93
- const ARRAY_BUFFER_MAX_SIZE = /* #__PURE__ */ 2 ** 32;
94
-
95
- export class BufferTargetWriter extends Writer {
96
- private pos = 0;
97
- private target: BufferTarget;
98
- private buffer: ArrayBuffer;
99
- private bytes: Uint8Array;
100
- private maxPos = 0;
101
- private supportsResize: boolean;
102
-
103
- constructor(target: BufferTarget) {
104
- super();
105
-
106
- this.target = target;
107
-
108
- this.supportsResize = 'resize' in new ArrayBuffer(0);
109
- if (this.supportsResize) {
110
- try {
111
- // @ts-expect-error Don't want to bump "lib" in tsconfig
112
- this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE, { maxByteLength: ARRAY_BUFFER_MAX_SIZE });
113
- } catch {
114
- this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE);
115
- this.supportsResize = false;
116
- }
117
- } else {
118
- this.buffer = new ArrayBuffer(ARRAY_BUFFER_INITIAL_SIZE);
119
- }
120
-
121
- this.bytes = new Uint8Array(this.buffer);
122
- }
123
-
124
- private ensureSize(size: number) {
125
- let newLength = this.buffer.byteLength;
126
- while (newLength < size) newLength *= 2;
127
-
128
- if (newLength === this.buffer.byteLength) return;
129
-
130
- if (newLength > ARRAY_BUFFER_MAX_SIZE) {
131
- throw new Error(
132
- `ArrayBuffer exceeded maximum size of ${ARRAY_BUFFER_MAX_SIZE} bytes. Please consider using another`
133
- + ` target.`,
134
- );
135
- }
136
-
137
- if (this.supportsResize) {
138
- // Use resize if it exists
139
- // @ts-expect-error Don't want to bump "lib" in tsconfig
140
- // eslint-disable-next-line @typescript-eslint/no-unsafe-call
141
- this.buffer.resize(newLength);
142
- // The Uint8Array scales automatically
143
- } else {
144
- const newBuffer = new ArrayBuffer(newLength);
145
- const newBytes = new Uint8Array(newBuffer);
146
- newBytes.set(this.bytes, 0);
147
-
148
- this.buffer = newBuffer;
149
- this.bytes = newBytes;
150
- }
151
- }
152
-
153
- write(data: Uint8Array) {
154
- this.maybeTrackWrites(data);
155
-
156
- this.ensureSize(this.pos + data.byteLength);
157
-
158
- this.bytes.set(data, this.pos);
159
- this.target.onwrite?.(this.pos, this.pos + data.byteLength);
160
-
161
- this.pos += data.byteLength;
162
- this.maxPos = Math.max(this.maxPos, this.pos);
163
- }
164
-
165
- seek(newPos: number) {
166
- this.pos = newPos;
167
- }
168
-
169
- getPos() {
170
- return this.pos;
171
- }
172
-
173
- async flush() {}
174
-
175
- async finalize() {
176
- this.ensureSize(this.pos);
177
- this.target.buffer = this.buffer.slice(0, Math.max(this.maxPos, this.pos));
178
- }
179
-
180
- async close() {}
181
-
182
- getSlice(start: number, end: number) {
183
- return this.bytes.slice(start, end);
184
- }
185
- }
186
-
187
- const DEFAULT_CHUNK_SIZE = /* #__PURE__ */ 2 ** 24;
188
- const MAX_CHUNKS_AT_ONCE = 2;
189
-
190
- interface Chunk {
191
- start: number;
192
- written: ChunkSection[];
193
- data: Uint8Array;
194
- shouldFlush: boolean;
195
- }
196
-
197
- interface ChunkSection {
198
- start: number;
199
- end: number;
200
- }
201
-
202
- /**
203
- * Writes to a StreamTarget every time it is flushed, sending out all of the new data written since the
204
- * last flush. This is useful for streaming applications, like piping the output to disk. When using the chunked mode,
205
- * data will first be accumulated in larger chunks, and then the entire chunk will be flushed out at once when ready.
206
- */
207
- export class StreamTargetWriter extends Writer {
208
- private pos = 0;
209
- private target: StreamTarget;
210
- private sections: {
211
- data: Uint8Array;
212
- start: number;
213
- }[] = [];
214
-
215
- private lastWriteEnd = 0;
216
- private lastFlushEnd = 0;
217
- private writer: WritableStreamDefaultWriter<StreamTargetChunk> | null = null;
218
-
219
- // These variables regard chunked mode:
220
- private chunked: boolean;
221
- private chunkSize: number;
222
- /**
223
- * The data is divided up into fixed-size chunks, whose contents are first filled in RAM and then flushed out.
224
- * A chunk is flushed if all of its contents have been written.
225
- */
226
- private chunks: Chunk[] = [];
227
-
228
- constructor(target: StreamTarget) {
229
- super();
230
-
231
- this.target = target;
232
-
233
- this.chunked = target._options.chunked ?? false;
234
- this.chunkSize = target._options.chunkSize ?? DEFAULT_CHUNK_SIZE;
235
- }
236
-
237
- override start() {
238
- this.writer = this.target._writable.getWriter();
239
- }
240
-
241
- write(data: Uint8Array) {
242
- if (this.pos > this.lastWriteEnd) {
243
- const paddingBytesNeeded = this.pos - this.lastWriteEnd;
244
- this.pos = this.lastWriteEnd;
245
- this.write(new Uint8Array(paddingBytesNeeded));
246
- }
247
-
248
- this.maybeTrackWrites(data);
249
-
250
- this.sections.push({
251
- data: data.slice(),
252
- start: this.pos,
253
- });
254
- this.target.onwrite?.(this.pos, this.pos + data.byteLength);
255
-
256
- this.pos += data.byteLength;
257
-
258
- this.lastWriteEnd = Math.max(this.lastWriteEnd, this.pos);
259
- }
260
-
261
- seek(newPos: number) {
262
- this.pos = newPos;
263
- }
264
-
265
- getPos() {
266
- return this.pos;
267
- }
268
-
269
- async flush() {
270
- if (this.pos > this.lastWriteEnd) {
271
- // There's a "void" between the last written byte and the next byte we're about to write. Let's pad that
272
- // void with zeroes explicitly.
273
- const paddingBytesNeeded = this.pos - this.lastWriteEnd;
274
- this.pos = this.lastWriteEnd;
275
- this.write(new Uint8Array(paddingBytesNeeded));
276
- }
277
-
278
- assert(this.writer);
279
- if (this.sections.length === 0) return;
280
-
281
- const chunks: {
282
- start: number;
283
- size: number;
284
- data?: Uint8Array;
285
- }[] = [];
286
- const sorted = [...this.sections].sort((a, b) => a.start - b.start);
287
-
288
- chunks.push({
289
- start: sorted[0]!.start,
290
- size: sorted[0]!.data.byteLength,
291
- });
292
-
293
- // Figure out how many contiguous chunks we have
294
- for (let i = 1; i < sorted.length; i++) {
295
- const lastChunk = chunks[chunks.length - 1]!;
296
- const section = sorted[i]!;
297
-
298
- if (section.start <= lastChunk.start + lastChunk.size) {
299
- lastChunk.size = Math.max(lastChunk.size, section.start + section.data.byteLength - lastChunk.start);
300
- } else {
301
- chunks.push({
302
- start: section.start,
303
- size: section.data.byteLength,
304
- });
305
- }
306
- }
307
-
308
- for (const chunk of chunks) {
309
- chunk.data = new Uint8Array(chunk.size);
310
-
311
- // Make sure to write the data in the correct order for correct overwriting
312
- for (const section of this.sections) {
313
- // Check if the section is in the chunk
314
- if (chunk.start <= section.start && section.start < chunk.start + chunk.size) {
315
- chunk.data.set(section.data, section.start - chunk.start);
316
- }
317
- }
318
-
319
- if (this.writer.desiredSize !== null && this.writer.desiredSize <= 0) {
320
- await this.writer.ready; // Allow the writer to apply backpressure
321
- }
322
-
323
- if (this.chunked) {
324
- // Let's first gather the data into bigger chunks before writing it
325
- this.writeDataIntoChunks(chunk.data, chunk.start);
326
- this.tryToFlushChunks();
327
- } else {
328
- if (this.ensureMonotonicity && chunk.start !== this.lastFlushEnd) {
329
- throw new Error('Internal error: Monotonicity violation.');
330
- }
331
-
332
- // Write out the data immediately
333
- void this.writer.write({
334
- type: 'write',
335
- data: chunk.data,
336
- position: chunk.start,
337
- });
338
-
339
- this.lastFlushEnd = chunk.start + chunk.data.byteLength;
340
- }
341
- }
342
-
343
- this.sections.length = 0;
344
- }
345
-
346
- private writeDataIntoChunks(data: Uint8Array, position: number) {
347
- // First, find the chunk to write the data into, or create one if none exists
348
- let chunkIndex = this.chunks.findIndex(x => x.start <= position && position < x.start + this.chunkSize);
349
- if (chunkIndex === -1) chunkIndex = this.createChunk(position);
350
- const chunk = this.chunks[chunkIndex]!;
351
-
352
- // Figure out how much to write to the chunk, and then write to the chunk
353
- const relativePosition = position - chunk.start;
354
- const toWrite = data.subarray(0, Math.min(this.chunkSize - relativePosition, data.byteLength));
355
- chunk.data.set(toWrite, relativePosition);
356
-
357
- // Create a section describing the region of data that was just written to
358
- const section: ChunkSection = {
359
- start: relativePosition,
360
- end: relativePosition + toWrite.byteLength,
361
- };
362
- this.insertSectionIntoChunk(chunk, section);
363
-
364
- // Queue chunk for flushing to target if it has been fully written to
365
- if (chunk.written[0]!.start === 0 && chunk.written[0]!.end === this.chunkSize) {
366
- chunk.shouldFlush = true;
367
- }
368
-
369
- // Make sure we don't hold too many chunks in memory at once to keep memory usage down
370
- if (this.chunks.length > MAX_CHUNKS_AT_ONCE) {
371
- // Flush all but the last chunk
372
- for (let i = 0; i < this.chunks.length - 1; i++) {
373
- this.chunks[i]!.shouldFlush = true;
374
- }
375
- this.tryToFlushChunks();
376
- }
377
-
378
- // If the data didn't fit in one chunk, recurse with the remaining data
379
- if (toWrite.byteLength < data.byteLength) {
380
- this.writeDataIntoChunks(data.subarray(toWrite.byteLength), position + toWrite.byteLength);
381
- }
382
- }
383
-
384
- private insertSectionIntoChunk(chunk: Chunk, section: ChunkSection) {
385
- let low = 0;
386
- let high = chunk.written.length - 1;
387
- let index = -1;
388
-
389
- // Do a binary search to find the last section with a start not larger than `section`'s start
390
- while (low <= high) {
391
- const mid = Math.floor(low + (high - low + 1) / 2);
392
-
393
- if (chunk.written[mid]!.start <= section.start) {
394
- low = mid + 1;
395
- index = mid;
396
- } else {
397
- high = mid - 1;
398
- }
399
- }
400
-
401
- // Insert the new section
402
- chunk.written.splice(index + 1, 0, section);
403
- if (index === -1 || chunk.written[index]!.end < section.start) index++;
404
-
405
- // Merge overlapping sections
406
- while (index < chunk.written.length - 1 && chunk.written[index]!.end >= chunk.written[index + 1]!.start) {
407
- chunk.written[index]!.end = Math.max(chunk.written[index]!.end, chunk.written[index + 1]!.end);
408
- chunk.written.splice(index + 1, 1);
409
- }
410
- }
411
-
412
- private createChunk(includesPosition: number) {
413
- const start = Math.floor(includesPosition / this.chunkSize) * this.chunkSize;
414
- const chunk: Chunk = {
415
- start,
416
- data: new Uint8Array(this.chunkSize),
417
- written: [],
418
- shouldFlush: false,
419
- };
420
- this.chunks.push(chunk);
421
- this.chunks.sort((a, b) => a.start - b.start);
422
-
423
- return this.chunks.indexOf(chunk);
424
- }
425
-
426
- private tryToFlushChunks(force = false) {
427
- assert(this.writer);
428
-
429
- for (let i = 0; i < this.chunks.length; i++) {
430
- const chunk = this.chunks[i]!;
431
- if (!chunk.shouldFlush && !force) continue;
432
-
433
- for (const section of chunk.written) {
434
- const position = chunk.start + section.start;
435
- if (this.ensureMonotonicity && position !== this.lastFlushEnd) {
436
- throw new Error('Internal error: Monotonicity violation.');
437
- }
438
-
439
- void this.writer.write({
440
- type: 'write',
441
- data: chunk.data.subarray(section.start, section.end),
442
- position,
443
- });
444
-
445
- this.lastFlushEnd = chunk.start + section.end;
446
- }
447
-
448
- this.chunks.splice(i--, 1);
449
- }
450
- }
451
-
452
- finalize() {
453
- if (this.chunked) {
454
- this.tryToFlushChunks(true);
455
- }
456
-
457
- assert(this.writer);
458
- return this.writer.close();
459
- }
460
-
461
- async close() {
462
- return this.writer?.close();
463
- }
464
- }
465
-
466
- export class NullTargetWriter extends Writer {
467
- private pos = 0;
468
-
469
- constructor(private target: NullTarget) {
470
- super();
471
- }
472
-
473
- write(data: Uint8Array) {
474
- this.maybeTrackWrites(data);
475
- this.target.onwrite?.(this.pos, this.pos + data.byteLength);
476
- this.pos += data.byteLength;
477
- }
478
-
479
- getPos() {
480
- return this.pos;
481
- }
482
-
483
- seek(newPos: number) {
484
- this.pos = newPos;
485
- }
486
-
487
- async flush() {}
488
- async finalize() {}
489
- async close() {}
490
- }