-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy patho-decoder.js
81 lines (68 loc) · 2.51 KB
/
o-decoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { decodeBuffer } from './buffer.js';
import { decodeFallback } from './lowlevel.js';
import { failedToString, maybeThrowFailedToOption } from './shared.js';
import { hasBufferFrom } from './support.js';
import { decodeSyncXHR } from './xhr.js';
var trySyncXHR = !hasBufferFrom && (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function');
var validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8'];
/** @type {(bytes: Uint8Array, encoding: string) => string} */
var decodeImpl = decodeFallback;
if (hasBufferFrom) {
decodeImpl = decodeBuffer;
} else if (trySyncXHR) {
decodeImpl = (string) => {
try {
return decodeSyncXHR(string);
} catch (e) {
return decodeFallback(string);
}
};
}
var ctorString = `construct 'TextDecoder'`;
var errorPrefix = `${failedToString} ${ctorString}: the `;
/**
* @constructor
* @param {string=} utfLabel
* @param {{fatal: boolean}=} options
*/
export function FastTextDecoder(utfLabel, options) {
maybeThrowFailedToOption(options && options.fatal, ctorString, 'fatal');
utfLabel = utfLabel || 'utf-8';
/** @type {boolean} */
var ok;
if (hasBufferFrom) {
ok = Buffer.isEncoding(utfLabel);
} else {
ok = validUtfLabels.indexOf(utfLabel.toLowerCase()) !== -1;
}
if (!ok) {
throw new RangeError(`${errorPrefix} encoding label provided ('${utfLabel}') is invalid.`);
}
this.encoding = utfLabel;
this.fatal = false;
this.ignoreBOM = false;
}
/**
* @param {(ArrayBuffer|ArrayBufferView)} buffer
* @param {{stream: boolean}=} options
* @return {string}
*/
FastTextDecoder.prototype.decode = function (buffer, options) {
maybeThrowFailedToOption(options && options.stream, 'decode', 'stream');
var bytes;
if (buffer instanceof Uint8Array) {
// Accept Uint8Array instances as-is. This is also a Node buffer.
bytes = buffer;
} else if (buffer['buffer'] instanceof ArrayBuffer) {
// Look for ArrayBufferView, which isn't a real type, but basically
// represents all the valid TypedArray types plus DataView. They all have
// ".buffer" as an instance of ArrayBuffer.
bytes = new Uint8Array(/** @type {ArrayBufferView} */(buffer).buffer);
} else {
// The only other valid argument here is that "buffer" is an ArrayBuffer.
// We also try to convert anything else passed to a Uint8Array, as this
// catches anything that's array-like. Native code would throw here.
bytes = new Uint8Array(/** @type {any} */(buffer));
}
return decodeImpl(bytes, this.encoding);
};