1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.fileBufferToString = exports.fileBuffer = exports.stringToFileBuffer = void 0;
|
11 | function stringToFileBuffer(str) {
|
12 |
|
13 | if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
|
14 | const buf = Buffer.from(str);
|
15 | const ab = new ArrayBuffer(buf.length);
|
16 | const view = new Uint8Array(ab);
|
17 | for (let i = 0; i < buf.length; ++i) {
|
18 | view[i] = buf[i];
|
19 | }
|
20 | return ab;
|
21 | }
|
22 | else if (typeof TextEncoder !== 'undefined') {
|
23 |
|
24 | return new TextEncoder('utf-8').encode(str).buffer;
|
25 | }
|
26 | else {
|
27 |
|
28 | const buf = new ArrayBuffer(str.length * 2);
|
29 | const bufView = new Uint16Array(buf);
|
30 | for (let i = 0, strLen = str.length; i < strLen; i++) {
|
31 | bufView[i] = str.charCodeAt(i);
|
32 | }
|
33 | return buf;
|
34 | }
|
35 | }
|
36 | exports.stringToFileBuffer = stringToFileBuffer;
|
37 | const fileBuffer = (strings, ...values) => {
|
38 | return stringToFileBuffer(String.raw(strings, ...values));
|
39 | };
|
40 | exports.fileBuffer = fileBuffer;
|
41 | function fileBufferToString(fileBuffer) {
|
42 | if (fileBuffer.toString.length == 1) {
|
43 | return fileBuffer.toString('utf-8');
|
44 | }
|
45 | else if (typeof Buffer !== 'undefined') {
|
46 | return Buffer.from(fileBuffer).toString('utf-8');
|
47 | }
|
48 | else if (typeof TextDecoder !== 'undefined') {
|
49 |
|
50 | return new TextDecoder('utf-8').decode(new Uint8Array(fileBuffer));
|
51 | }
|
52 | else {
|
53 |
|
54 | const bufView = new Uint8Array(fileBuffer);
|
55 | const bufLength = bufView.length;
|
56 | let result = '';
|
57 | let chunkLength = Math.pow(2, 16) - 1;
|
58 |
|
59 |
|
60 | for (let i = 0; i < bufLength; i += chunkLength) {
|
61 | if (i + chunkLength > bufLength) {
|
62 | chunkLength = bufLength - i;
|
63 | }
|
64 | result += String.fromCharCode.apply(null, [...bufView.subarray(i, i + chunkLength)]);
|
65 | }
|
66 | return result;
|
67 | }
|
68 | }
|
69 | exports.fileBufferToString = fileBufferToString;
|