@probityrules/jsmediatags 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/LICENSE.md +36 -0
  3. package/README.md +548 -0
  4. package/build/ArrayBufferFileReader.d.ts +12 -0
  5. package/build/ArrayBufferFileReader.js +27 -0
  6. package/build/ArrayFileReader.d.ts +11 -0
  7. package/build/ArrayFileReader.js +30 -0
  8. package/build/BlobFileReader.d.ts +12 -0
  9. package/build/BlobFileReader.js +47 -0
  10. package/build/ByteArrayUtils.d.ts +9 -0
  11. package/build/ByteArrayUtils.js +46 -0
  12. package/build/ChunkedFileData.d.ts +28 -0
  13. package/build/ChunkedFileData.js +171 -0
  14. package/build/DecodedString.d.ts +6 -0
  15. package/build/DecodedString.js +2 -0
  16. package/build/FLACTagContents.d.ts +19 -0
  17. package/build/FLACTagContents.js +54 -0
  18. package/build/FLACTagReader.d.ts +103 -0
  19. package/build/FLACTagReader.js +320 -0
  20. package/build/ID3v1TagReader.d.ts +10 -0
  21. package/build/ID3v1TagReader.js +176 -0
  22. package/build/ID3v2FrameReader.d.ts +25 -0
  23. package/build/ID3v2FrameReader.js +582 -0
  24. package/build/ID3v2TagContents.d.ts +82 -0
  25. package/build/ID3v2TagContents.js +318 -0
  26. package/build/ID3v2TagReader.d.ts +13 -0
  27. package/build/ID3v2TagReader.js +118 -0
  28. package/build/MP4TagContents.d.ts +17 -0
  29. package/build/MP4TagContents.js +52 -0
  30. package/build/MP4TagReader.d.ts +19 -0
  31. package/build/MP4TagReader.js +291 -0
  32. package/build/MediaFileReader.d.ts +46 -0
  33. package/build/MediaFileReader.js +168 -0
  34. package/build/MediaTagReader.d.ts +18 -0
  35. package/build/MediaTagReader.js +76 -0
  36. package/build/NodeFileReader.d.ts +12 -0
  37. package/build/NodeFileReader.js +103 -0
  38. package/build/ReactNativeFileReader.d.ts +12 -0
  39. package/build/ReactNativeFileReader.js +48 -0
  40. package/build/StringUtils.d.ts +7 -0
  41. package/build/StringUtils.js +102 -0
  42. package/build/XhrFileReader.d.ts +41 -0
  43. package/build/XhrFileReader.js +238 -0
  44. package/build/jsmediatags.d.ts +45 -0
  45. package/build/jsmediatags.js +219 -0
  46. package/build/registerNodeFileReaders.d.ts +5 -0
  47. package/build/registerNodeFileReaders.js +19 -0
  48. package/build/registerNodeFileReaders.noop.d.ts +2 -0
  49. package/build/registerNodeFileReaders.noop.js +3 -0
  50. package/build/types.d.ts +77 -0
  51. package/build/types.js +2 -0
  52. package/dist/jsmediatags.min.js +2 -0
  53. package/package.json +110 -0
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ const fs = __importStar(require("fs"));
36
+ const ChunkedFileData = require("./ChunkedFileData");
37
+ const MediaFileReader = require("./MediaFileReader");
38
+ class NodeFileReader extends MediaFileReader {
39
+ constructor(path) {
40
+ super();
41
+ this._path = path;
42
+ this._fileData = new ChunkedFileData();
43
+ }
44
+ static canReadFile(file) {
45
+ return typeof file === "string" && !/^[a-z]+:\/\//i.test(file);
46
+ }
47
+ getByteAt(offset) {
48
+ return this._fileData.getByteAt(offset);
49
+ }
50
+ _init(callbacks) {
51
+ fs.stat(this._path, (err, stats) => {
52
+ if (err) {
53
+ if (callbacks.onError) {
54
+ callbacks.onError({ type: "fs", info: err });
55
+ }
56
+ }
57
+ else {
58
+ this._size = stats.size;
59
+ callbacks.onSuccess();
60
+ }
61
+ });
62
+ }
63
+ loadRange(range, callbacks) {
64
+ let fd = -1;
65
+ const self = this;
66
+ const fileData = this._fileData;
67
+ const length = range[1] - range[0] + 1;
68
+ const onSuccess = callbacks.onSuccess;
69
+ const onError = callbacks.onError || function (_object) { };
70
+ if (fileData.hasDataRange(range[0], range[1])) {
71
+ process.nextTick(onSuccess);
72
+ return;
73
+ }
74
+ const readData = function (err, _fd) {
75
+ if (err) {
76
+ onError({ type: "fs", info: err });
77
+ return;
78
+ }
79
+ fd = _fd;
80
+ const buffer = Buffer.alloc(length);
81
+ fs.read(_fd, buffer, 0, length, range[0], processData);
82
+ };
83
+ const processData = function (err, _bytesRead, buffer) {
84
+ fs.close(fd, function (err) {
85
+ if (err) {
86
+ console.error(err);
87
+ }
88
+ });
89
+ if (err) {
90
+ onError({ type: "fs", info: err });
91
+ return;
92
+ }
93
+ storeBuffer(buffer);
94
+ onSuccess();
95
+ };
96
+ const storeBuffer = function (buffer) {
97
+ const data = Array.prototype.slice.call(buffer, 0, length);
98
+ fileData.addData(range[0], data);
99
+ };
100
+ fs.open(this._path, "r", undefined, readData);
101
+ }
102
+ }
103
+ module.exports = NodeFileReader;
@@ -0,0 +1,12 @@
1
+ declare const MediaFileReader: any;
2
+ import type { LoadCallbackType } from "./types";
3
+ declare class ReactNativeFileReader extends MediaFileReader {
4
+ private _path;
5
+ private _fileData;
6
+ constructor(path: string);
7
+ static canReadFile(file: unknown): boolean;
8
+ getByteAt(offset: number): number;
9
+ _init(callbacks: LoadCallbackType): void;
10
+ loadRange(range: [number, number], callbacks: LoadCallbackType): void;
11
+ }
12
+ export = ReactNativeFileReader;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ const RNFS = require("react-native-fs");
3
+ const { Buffer } = require("buffer");
4
+ const ChunkedFileData = require("./ChunkedFileData");
5
+ const MediaFileReader = require("./MediaFileReader");
6
+ class ReactNativeFileReader extends MediaFileReader {
7
+ constructor(path) {
8
+ super();
9
+ this._path = path;
10
+ this._fileData = new ChunkedFileData();
11
+ }
12
+ static canReadFile(file) {
13
+ return typeof file === "string" && !/^[a-z]+:\/\//i.test(file);
14
+ }
15
+ getByteAt(offset) {
16
+ return this._fileData.getByteAt(offset);
17
+ }
18
+ _init(callbacks) {
19
+ const self = this;
20
+ RNFS.stat(self._path)
21
+ .then((statResult) => {
22
+ self._size = statResult.size;
23
+ callbacks.onSuccess();
24
+ })
25
+ .catch((error) => {
26
+ if (callbacks.onError) {
27
+ callbacks.onError({ type: "fs", info: error });
28
+ }
29
+ });
30
+ }
31
+ loadRange(range, callbacks) {
32
+ const fileData = this._fileData;
33
+ const length = range[1] - range[0] + 1;
34
+ const onSuccess = callbacks.onSuccess;
35
+ const onError = callbacks.onError || function (_object) { };
36
+ RNFS.read(this._path, length, range[0], { encoding: "base64" })
37
+ .then((readData) => {
38
+ const buffer = Buffer.from(readData, "base64");
39
+ const data = Array.prototype.slice.call(buffer, 0, length);
40
+ fileData.addData(range[0], data);
41
+ onSuccess();
42
+ })
43
+ .catch((err) => {
44
+ onError({ type: "fs", info: err });
45
+ });
46
+ }
47
+ }
48
+ module.exports = ReactNativeFileReader;
@@ -0,0 +1,7 @@
1
+ import type { DecodedString } from "./DecodedString";
2
+ declare const StringUtils: {
3
+ readUTF16String: (bytes: Array<number>, bigEndian: boolean, maxBytes?: number) => DecodedString;
4
+ readUTF8String: (bytes: Array<number>, maxBytes?: number) => DecodedString;
5
+ readNullTerminatedString: (bytes: Array<number>, maxBytes?: number) => DecodedString;
6
+ };
7
+ export = StringUtils;
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ class InternalDecodedString {
3
+ constructor(value, bytesReadCount) {
4
+ this._value = value;
5
+ this.bytesReadCount = bytesReadCount;
6
+ this.length = value.length;
7
+ }
8
+ toString() {
9
+ return this._value;
10
+ }
11
+ }
12
+ const StringUtils = {
13
+ readUTF16String: function (bytes, bigEndian, maxBytes) {
14
+ let ix = 0;
15
+ let offset1 = 1, offset2 = 0;
16
+ maxBytes = Math.min(maxBytes || bytes.length, bytes.length);
17
+ if (bytes[0] == 0xfe && bytes[1] == 0xff) {
18
+ bigEndian = true;
19
+ ix = 2;
20
+ }
21
+ else if (bytes[0] == 0xff && bytes[1] == 0xfe) {
22
+ bigEndian = false;
23
+ ix = 2;
24
+ }
25
+ if (bigEndian) {
26
+ offset1 = 0;
27
+ offset2 = 1;
28
+ }
29
+ const arr = [];
30
+ for (let j = 0; ix < maxBytes; j++) {
31
+ const byte1 = bytes[ix + offset1];
32
+ const byte2 = bytes[ix + offset2];
33
+ const word1 = (byte1 << 8) + byte2;
34
+ ix += 2;
35
+ if (word1 == 0x0000) {
36
+ break;
37
+ }
38
+ else if (byte1 < 0xd8 || byte1 >= 0xe0) {
39
+ arr[j] = String.fromCharCode(word1);
40
+ }
41
+ else {
42
+ const byte3 = bytes[ix + offset1];
43
+ const byte4 = bytes[ix + offset2];
44
+ const word2 = (byte3 << 8) + byte4;
45
+ ix += 2;
46
+ arr[j] = String.fromCharCode(word1, word2);
47
+ }
48
+ }
49
+ return new InternalDecodedString(arr.join(""), ix);
50
+ },
51
+ readUTF8String: function (bytes, maxBytes) {
52
+ let ix = 0;
53
+ maxBytes = Math.min(maxBytes || bytes.length, bytes.length);
54
+ if (bytes[0] == 0xef && bytes[1] == 0xbb && bytes[2] == 0xbf) {
55
+ ix = 3;
56
+ }
57
+ const arr = [];
58
+ for (let j = 0; ix < maxBytes; j++) {
59
+ const byte1 = bytes[ix++];
60
+ if (byte1 == 0x00) {
61
+ break;
62
+ }
63
+ else if (byte1 < 0x80) {
64
+ arr[j] = String.fromCharCode(byte1);
65
+ }
66
+ else if (byte1 >= 0xc2 && byte1 < 0xe0) {
67
+ const byte2 = bytes[ix++];
68
+ arr[j] = String.fromCharCode(((byte1 & 0x1f) << 6) + (byte2 & 0x3f));
69
+ }
70
+ else if (byte1 >= 0xe0 && byte1 < 0xf0) {
71
+ const byte2 = bytes[ix++];
72
+ const byte3 = bytes[ix++];
73
+ arr[j] = String.fromCharCode(((byte1 & 0xff) << 12) + ((byte2 & 0x3f) << 6) + (byte3 & 0x3f));
74
+ }
75
+ else if (byte1 >= 0xf0 && byte1 < 0xf5) {
76
+ const byte2 = bytes[ix++];
77
+ const byte3 = bytes[ix++];
78
+ const byte4 = bytes[ix++];
79
+ const codepoint = ((byte1 & 0x07) << 18) +
80
+ ((byte2 & 0x3f) << 12) +
81
+ ((byte3 & 0x3f) << 6) +
82
+ (byte4 & 0x3f) -
83
+ 0x10000;
84
+ arr[j] = String.fromCharCode((codepoint >> 10) + 0xd800, (codepoint & 0x3ff) + 0xdc00);
85
+ }
86
+ }
87
+ return new InternalDecodedString(arr.join(""), ix);
88
+ },
89
+ readNullTerminatedString: function (bytes, maxBytes) {
90
+ const arr = [];
91
+ maxBytes = maxBytes || bytes.length;
92
+ for (var i = 0; i < maxBytes;) {
93
+ var byte1 = bytes[i++];
94
+ if (byte1 == 0x00) {
95
+ break;
96
+ }
97
+ arr[i - 1] = String.fromCharCode(byte1);
98
+ }
99
+ return new InternalDecodedString(arr.join(""), i);
100
+ },
101
+ };
102
+ module.exports = StringUtils;
@@ -0,0 +1,41 @@
1
+ declare const MediaFileReader: any;
2
+ import type { LoadCallbackType } from "./types";
3
+ type XhrCallbacks = {
4
+ onSuccess: (xhr: XMLHttpRequest) => void;
5
+ onError?: (error: Record<string, unknown>) => void;
6
+ };
7
+ type ContentRangeType = {
8
+ firstBytePosition: number | null | undefined;
9
+ lastBytePosition: number | null | undefined;
10
+ instanceLength: number | null | undefined;
11
+ };
12
+ type XhrConfig = {
13
+ avoidHeadRequests: boolean;
14
+ disallowedXhrHeaders: string[];
15
+ timeoutInSec: number;
16
+ };
17
+ declare class XhrFileReader extends MediaFileReader {
18
+ static _config: XhrConfig;
19
+ private _url;
20
+ private _fileData;
21
+ constructor(url: string);
22
+ static canReadFile(file: unknown): boolean;
23
+ static setConfig(config: Partial<XhrConfig> & Record<string, unknown>): void;
24
+ _init(callbacks: LoadCallbackType): void;
25
+ _fetchSizeWithHeadRequest(callbacks: LoadCallbackType): void;
26
+ _fetchSizeWithGetRequest(callbacks: LoadCallbackType): void;
27
+ _fetchEntireFile(callbacks: LoadCallbackType): void;
28
+ _getXhrResponseContent(xhr: XMLHttpRequest): string;
29
+ _parseContentLength(xhr: XMLHttpRequest): number | null | undefined;
30
+ _parseContentRange(xhr: XMLHttpRequest): ContentRangeType | null;
31
+ loadRange(range: [number, number], callbacks: LoadCallbackType): void;
32
+ _roundRangeToChunkMultiple(range: [number, number]): [number, number];
33
+ _makeXHRRequest(method: string, range: [number, number] | null, callbacks: XhrCallbacks): void;
34
+ _setRequestHeader(xhr: XMLHttpRequest, headerName: string, headerValue: string): void;
35
+ _hasResponseHeader(xhr: XMLHttpRequest, headerName: string): boolean;
36
+ _getResponseHeader(xhr: XMLHttpRequest, headerName: string): string | null;
37
+ getByteAt(offset: number): number;
38
+ _isWebWorker(): boolean;
39
+ _createXHRObject(): XMLHttpRequest;
40
+ }
41
+ export = XhrFileReader;
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ const ChunkedFileData = require("./ChunkedFileData");
3
+ const MediaFileReader = require("./MediaFileReader");
4
+ const CHUNK_SIZE = 1024;
5
+ class XhrFileReader extends MediaFileReader {
6
+ constructor(url) {
7
+ super();
8
+ this._url = url;
9
+ this._fileData = new ChunkedFileData();
10
+ }
11
+ static canReadFile(file) {
12
+ return typeof file === "string" && /^[a-z]+:\/\//i.test(file);
13
+ }
14
+ static setConfig(config) {
15
+ for (const key in config) {
16
+ if (Object.prototype.hasOwnProperty.call(config, key)) {
17
+ XhrFileReader._config[key] = config[key];
18
+ }
19
+ }
20
+ const disallowedXhrHeaders = XhrFileReader._config.disallowedXhrHeaders;
21
+ for (let i = 0; i < disallowedXhrHeaders.length; i++) {
22
+ disallowedXhrHeaders[i] = disallowedXhrHeaders[i].toLowerCase();
23
+ }
24
+ }
25
+ _init(callbacks) {
26
+ if (XhrFileReader._config.avoidHeadRequests) {
27
+ this._fetchSizeWithGetRequest(callbacks);
28
+ }
29
+ else {
30
+ this._fetchSizeWithHeadRequest(callbacks);
31
+ }
32
+ }
33
+ _fetchSizeWithHeadRequest(callbacks) {
34
+ const self = this;
35
+ this._makeXHRRequest("HEAD", null, {
36
+ onSuccess: function (xhr) {
37
+ const contentLength = self._parseContentLength(xhr);
38
+ if (contentLength) {
39
+ self._size = contentLength;
40
+ callbacks.onSuccess();
41
+ }
42
+ else {
43
+ self._fetchSizeWithGetRequest(callbacks);
44
+ }
45
+ },
46
+ onError: callbacks.onError,
47
+ });
48
+ }
49
+ _fetchSizeWithGetRequest(callbacks) {
50
+ const self = this;
51
+ const range = this._roundRangeToChunkMultiple([0, 0]);
52
+ this._makeXHRRequest("GET", range, {
53
+ onSuccess: function (xhr) {
54
+ const contentRange = self._parseContentRange(xhr);
55
+ const data = self._getXhrResponseContent(xhr);
56
+ if (contentRange) {
57
+ if (contentRange.instanceLength == null) {
58
+ self._fetchEntireFile(callbacks);
59
+ return;
60
+ }
61
+ self._size = contentRange.instanceLength;
62
+ }
63
+ else {
64
+ self._size = data.length;
65
+ }
66
+ self._fileData.addData(0, data);
67
+ callbacks.onSuccess();
68
+ },
69
+ onError: callbacks.onError,
70
+ });
71
+ }
72
+ _fetchEntireFile(callbacks) {
73
+ const self = this;
74
+ this._makeXHRRequest("GET", null, {
75
+ onSuccess: function (xhr) {
76
+ const data = self._getXhrResponseContent(xhr);
77
+ self._size = data.length;
78
+ self._fileData.addData(0, data);
79
+ callbacks.onSuccess();
80
+ },
81
+ onError: callbacks.onError,
82
+ });
83
+ }
84
+ _getXhrResponseContent(xhr) {
85
+ const legacy = xhr;
86
+ return legacy.responseBody || xhr.responseText || "";
87
+ }
88
+ _parseContentLength(xhr) {
89
+ const contentLength = this._getResponseHeader(xhr, "Content-Length");
90
+ if (contentLength == null) {
91
+ return null;
92
+ }
93
+ return parseInt(contentLength, 10);
94
+ }
95
+ _parseContentRange(xhr) {
96
+ const contentRange = this._getResponseHeader(xhr, "Content-Range");
97
+ if (contentRange) {
98
+ const parsedContentRange = contentRange.match(/bytes (\d+)-(\d+)\/(?:(\d+)|\*)/i);
99
+ if (!parsedContentRange) {
100
+ throw new Error("FIXME: Unknown Content-Range syntax: " + contentRange);
101
+ }
102
+ return {
103
+ firstBytePosition: parseInt(parsedContentRange[1], 10),
104
+ lastBytePosition: parseInt(parsedContentRange[2], 10),
105
+ instanceLength: parsedContentRange[3]
106
+ ? parseInt(parsedContentRange[3], 10)
107
+ : null,
108
+ };
109
+ }
110
+ else {
111
+ return null;
112
+ }
113
+ }
114
+ loadRange(range, callbacks) {
115
+ const self = this;
116
+ if (self._fileData.hasDataRange(range[0], Math.min(self._size, range[1]))) {
117
+ setTimeout(callbacks.onSuccess, 1);
118
+ return;
119
+ }
120
+ range = this._roundRangeToChunkMultiple(range);
121
+ range[1] = Math.min(self._size, range[1]);
122
+ this._makeXHRRequest("GET", range, {
123
+ onSuccess: function (xhr) {
124
+ const data = self._getXhrResponseContent(xhr);
125
+ self._fileData.addData(range[0], data);
126
+ callbacks.onSuccess();
127
+ },
128
+ onError: callbacks.onError,
129
+ });
130
+ }
131
+ _roundRangeToChunkMultiple(range) {
132
+ const length = range[1] - range[0] + 1;
133
+ const newLength = Math.ceil(length / CHUNK_SIZE) * CHUNK_SIZE;
134
+ return [range[0], range[0] + newLength - 1];
135
+ }
136
+ _makeXHRRequest(method, range, callbacks) {
137
+ const xhr = this._createXHRObject();
138
+ xhr.open(method, this._url);
139
+ const onXHRLoad = function () {
140
+ if (xhr.status === 200 || xhr.status === 206) {
141
+ callbacks.onSuccess(xhr);
142
+ }
143
+ else if (callbacks.onError) {
144
+ callbacks.onError({
145
+ type: "xhr",
146
+ info: "Unexpected HTTP status " + xhr.status + ".",
147
+ xhr: xhr,
148
+ });
149
+ }
150
+ };
151
+ if (typeof xhr.onload !== "undefined") {
152
+ xhr.onload = onXHRLoad;
153
+ xhr.onerror = function () {
154
+ if (callbacks.onError) {
155
+ callbacks.onError({
156
+ type: "xhr",
157
+ info: "Generic XHR error, check xhr object.",
158
+ xhr: xhr,
159
+ });
160
+ }
161
+ };
162
+ }
163
+ else {
164
+ xhr.onreadystatechange = function () {
165
+ if (xhr.readyState === 4) {
166
+ onXHRLoad();
167
+ }
168
+ };
169
+ }
170
+ if (XhrFileReader._config.timeoutInSec) {
171
+ xhr.timeout = XhrFileReader._config.timeoutInSec * 1000;
172
+ xhr.ontimeout = function () {
173
+ if (callbacks.onError) {
174
+ callbacks.onError({
175
+ type: "xhr",
176
+ info: "Timeout after " +
177
+ xhr.timeout / 1000 +
178
+ "s. Use jsmediatags.Config.setXhrTimeout to override.",
179
+ xhr: xhr,
180
+ });
181
+ }
182
+ };
183
+ }
184
+ xhr.overrideMimeType("text/plain; charset=x-user-defined");
185
+ if (range) {
186
+ this._setRequestHeader(xhr, "Range", "bytes=" + range[0] + "-" + range[1]);
187
+ }
188
+ this._setRequestHeader(xhr, "If-Modified-Since", "Sat, 01 Jan 1970 00:00:00 GMT");
189
+ xhr.send(null);
190
+ }
191
+ _setRequestHeader(xhr, headerName, headerValue) {
192
+ if (XhrFileReader._config.disallowedXhrHeaders.indexOf(headerName.toLowerCase()) < 0) {
193
+ xhr.setRequestHeader(headerName, headerValue);
194
+ }
195
+ }
196
+ _hasResponseHeader(xhr, headerName) {
197
+ const allResponseHeaders = xhr.getAllResponseHeaders();
198
+ if (!allResponseHeaders) {
199
+ return false;
200
+ }
201
+ const headers = allResponseHeaders.split("\r\n");
202
+ const headerNames = [];
203
+ for (let i = 0; i < headers.length; i++) {
204
+ headerNames[i] = headers[i].split(":")[0].toLowerCase();
205
+ }
206
+ return headerNames.indexOf(headerName.toLowerCase()) >= 0;
207
+ }
208
+ _getResponseHeader(xhr, headerName) {
209
+ if (!this._hasResponseHeader(xhr, headerName)) {
210
+ return null;
211
+ }
212
+ return xhr.getResponseHeader(headerName);
213
+ }
214
+ getByteAt(offset) {
215
+ return this._fileData.getByteAt(offset) & 0xff;
216
+ }
217
+ _isWebWorker() {
218
+ return (typeof WorkerGlobalScope !== "undefined" &&
219
+ typeof self !== "undefined" &&
220
+ self instanceof WorkerGlobalScope);
221
+ }
222
+ _createXHRObject() {
223
+ if (typeof window === "undefined" && !this._isWebWorker()) {
224
+ const xhr2 = require("xhr2");
225
+ return new xhr2.XMLHttpRequest();
226
+ }
227
+ if (typeof XMLHttpRequest !== "undefined") {
228
+ return new XMLHttpRequest();
229
+ }
230
+ throw new Error("XMLHttpRequest is not supported");
231
+ }
232
+ }
233
+ XhrFileReader._config = {
234
+ avoidHeadRequests: false,
235
+ disallowedXhrHeaders: [],
236
+ timeoutInSec: 30,
237
+ };
238
+ module.exports = XhrFileReader;
@@ -0,0 +1,45 @@
1
+ declare const MediaFileReader: any;
2
+ declare const MediaTagReader: any;
3
+ import type { CallbackType, LoadCallbackType, TagType } from "./types";
4
+ type MediaFileReaderClass = typeof MediaFileReader;
5
+ type MediaTagReaderClass = typeof MediaTagReader;
6
+ type TagReaderResolutionCallbacks = {
7
+ onSuccess: (reader?: MediaTagReaderClass) => void;
8
+ onError?: (error: Record<string, unknown>) => void;
9
+ };
10
+ declare function read(location: unknown, callbacks: CallbackType<TagType>): void;
11
+ declare function read(location: unknown): Promise<TagType>;
12
+ declare function readAsync(location: unknown): Promise<TagType>;
13
+ declare class Reader {
14
+ private _file;
15
+ private _tagsToRead?;
16
+ private _fileReader?;
17
+ private _tagReader?;
18
+ constructor(file: unknown);
19
+ setTagsToRead(tagsToRead: string[]): this;
20
+ setFileReader(fileReader: MediaFileReaderClass): this;
21
+ setTagReader(tagReader: MediaTagReaderClass): this;
22
+ read(callbacks: CallbackType<TagType>): void;
23
+ read(): Promise<TagType>;
24
+ readAsync(): Promise<TagType>;
25
+ _getFileReader(): MediaFileReaderClass;
26
+ _findFileReader(): MediaFileReaderClass;
27
+ _getTagReader(fileReader: InstanceType<typeof MediaFileReader>, callbacks: TagReaderResolutionCallbacks): void;
28
+ _findTagReader(fileReader: InstanceType<typeof MediaFileReader>, callbacks: TagReaderResolutionCallbacks): void;
29
+ _loadTagIdentifierRanges(fileReader: InstanceType<typeof MediaFileReader>, tagReaders: MediaTagReaderClass[], callbacks: LoadCallbackType): void;
30
+ }
31
+ declare class Config {
32
+ static addFileReader(fileReader: MediaFileReaderClass): typeof Config;
33
+ static addTagReader(tagReader: MediaTagReaderClass): typeof Config;
34
+ static removeTagReader(tagReader: MediaTagReaderClass): typeof Config;
35
+ static EXPERIMENTAL_avoidHeadRequests(): void;
36
+ static setDisallowedXhrHeaders(disallowedXhrHeaders: string[]): void;
37
+ static setXhrTimeoutInSec(timeoutInSec: number): void;
38
+ }
39
+ declare const _default: {
40
+ read: typeof read;
41
+ readAsync: typeof readAsync;
42
+ Reader: typeof Reader;
43
+ Config: typeof Config;
44
+ };
45
+ export = _default;