|
4 | 4 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.SynAudio = factory(global.Worker));
|
5 | 5 | })(this, (function (Worker) { 'use strict';
|
6 | 6 |
|
7 |
| - const encode = (byteArray) => { |
8 |
| - const charArray = []; |
9 |
| - |
10 |
| - for (const byte of byteArray) { |
11 |
| - let encoded = (byte + 42) % 256; |
12 |
| - |
13 |
| - if ( |
14 |
| - encoded === 0 || // NULL |
15 |
| - encoded === 10 || // LF |
16 |
| - encoded === 13 || // CR |
17 |
| - encoded === 61 // = |
18 |
| - ) { |
19 |
| - charArray.push("=" + String.fromCharCode((encoded + 64) % 256)); |
20 |
| - } else { |
21 |
| - charArray.push(String.fromCharCode(encoded)); |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - return charArray.join(""); |
26 |
| - }; |
27 |
| - |
28 |
| - const decode = (string) => { |
29 |
| - const htmlCodeOverrides = new Map(); |
30 |
| - [ |
31 |
| - , |
32 |
| - 8364, |
33 |
| - , |
34 |
| - 8218, |
35 |
| - 402, |
36 |
| - 8222, |
37 |
| - 8230, |
38 |
| - 8224, |
39 |
| - 8225, |
40 |
| - 710, |
41 |
| - 8240, |
42 |
| - 352, |
43 |
| - 8249, |
44 |
| - 338, |
45 |
| - , |
46 |
| - 381, |
47 |
| - , |
48 |
| - , |
49 |
| - 8216, |
50 |
| - 8217, |
51 |
| - 8220, |
52 |
| - 8221, |
53 |
| - 8226, |
54 |
| - 8211, |
55 |
| - 8212, |
56 |
| - 732, |
57 |
| - 8482, |
58 |
| - 353, |
59 |
| - 8250, |
60 |
| - 339, |
61 |
| - , |
62 |
| - 382, |
63 |
| - 376, |
64 |
| - ].forEach((k, v) => htmlCodeOverrides.set(k, v)); |
65 |
| - |
66 |
| - const output = new Uint8Array(string.length); |
67 |
| - |
68 |
| - let escaped = false, |
69 |
| - byteIndex = 0, |
70 |
| - byte, |
71 |
| - offset = 42, // default yEnc offset |
72 |
| - startIdx = 0; |
73 |
| - |
74 |
| - if (string.length > 13 && string.substring(0, 9) === "dynEncode") { |
75 |
| - const version = parseInt(string.substring(9, 11), 16); |
76 |
| - if (version === 0) { |
77 |
| - offset = parseInt(string.substring(11, 13), 16); |
78 |
| - startIdx = 13; |
79 |
| - } |
80 |
| - } |
81 |
| - |
82 |
| - const offsetReverse = 256 - offset; |
83 |
| - |
84 |
| - for (let i = startIdx; i < string.length; i++) { |
85 |
| - byte = string.charCodeAt(i); |
86 |
| - |
87 |
| - if (byte === 61 && !escaped) { |
88 |
| - escaped = true; |
89 |
| - continue; |
90 |
| - } |
91 |
| - |
92 |
| - if (byte > 255) { |
93 |
| - const htmlOverride = htmlCodeOverrides.get(byte); |
94 |
| - if (htmlOverride) byte = htmlOverride + 127; |
95 |
| - } |
96 |
| - |
97 |
| - if (escaped) { |
98 |
| - escaped = false; |
99 |
| - byte -= 64; |
100 |
| - } |
101 |
| - |
102 |
| - output[byteIndex++] = |
103 |
| - byte < offset && byte > 0 ? byte + offsetReverse : byte - offset; |
104 |
| - } |
105 |
| - |
106 |
| - return output.subarray(0, byteIndex); |
107 |
| - }; |
108 |
| - |
109 |
| - const dynamicEncode = (byteArray, stringWrapper = '"') => { |
110 |
| - let shouldEscape, |
111 |
| - offsetLength = Infinity, |
112 |
| - offset = 0; |
113 |
| - |
114 |
| - if (stringWrapper === '"') |
115 |
| - shouldEscape = (byte1) => |
116 |
| - byte1 === 0 || // NULL |
117 |
| - byte1 === 8 || // BS |
118 |
| - byte1 === 9 || // TAB |
119 |
| - byte1 === 10 || // LF |
120 |
| - byte1 === 11 || // VT |
121 |
| - byte1 === 12 || // FF |
122 |
| - byte1 === 13 || // CR |
123 |
| - byte1 === 34 || // " |
124 |
| - byte1 === 92 || // \ |
125 |
| - byte1 === 61; // =; |
126 |
| - |
127 |
| - if (stringWrapper === "'") |
128 |
| - shouldEscape = (byte1) => |
129 |
| - byte1 === 0 || // NULL |
130 |
| - byte1 === 8 || // BS |
131 |
| - byte1 === 9 || // TAB |
132 |
| - byte1 === 10 || // LF |
133 |
| - byte1 === 11 || // VT |
134 |
| - byte1 === 12 || // FF |
135 |
| - byte1 === 13 || // CR |
136 |
| - byte1 === 39 || // ' |
137 |
| - byte1 === 92 || // \ |
138 |
| - byte1 === 61; // =; |
139 |
| - |
140 |
| - if (stringWrapper === "`") |
141 |
| - shouldEscape = (byte1, byte2) => |
142 |
| - byte1 === 61 || // = |
143 |
| - byte1 === 13 || // CR |
144 |
| - byte1 === 96 || // ` |
145 |
| - (byte1 === 92 && (byte2 === 85 || byte2 === 117)) || // \u or \U |
146 |
| - (byte1 === 36 && byte2 === 123); // ${ |
147 |
| - |
148 |
| - // search for the byte offset with the least amount of escape characters |
149 |
| - for (let o = 0; o < 256; o++) { |
150 |
| - let length = 0; |
151 |
| - |
152 |
| - for (let i = 0; i < byteArray.length; i++) { |
153 |
| - const byte1 = (byteArray[i] + o) % 256 | 0; |
154 |
| - const byte2 = (byteArray[i + 1] + o) % 256 | 0; |
155 |
| - |
156 |
| - if (shouldEscape(byte1, byte2)) length++; |
157 |
| - length++; |
158 |
| - } |
159 |
| - |
160 |
| - if (length < offsetLength) { |
161 |
| - offsetLength = length; |
162 |
| - offset = o; |
163 |
| - } |
164 |
| - } |
165 |
| - |
166 |
| - const escapeCharacter = (byte1, charArray) => |
167 |
| - charArray.push("=", String.fromCharCode((byte1 + 64) % 256)); |
168 |
| - |
169 |
| - const charArray = [ |
170 |
| - "dynEncode", // magic signature |
171 |
| - "00", // version 0x00 - 0xfe (0xff reserved) |
172 |
| - offset.toString(16).padStart(2, "0"), // best offset in bytes 0x00 - 0xff |
173 |
| - ]; |
174 |
| - |
175 |
| - for (let i = 0; i < byteArray.length; i++) { |
176 |
| - const byte1 = (byteArray[i] + offset) % 256; |
177 |
| - const byte2 = (byteArray[i + 1] + offset) % 256; |
178 |
| - |
179 |
| - if (shouldEscape(byte1, byte2)) { |
180 |
| - escapeCharacter(byte1, charArray); |
181 |
| - } else { |
182 |
| - charArray.push(String.fromCharCode(byte1)); |
183 |
| - } |
184 |
| - } |
185 |
| - |
186 |
| - // correct edge case where escape character is at end of string |
187 |
| - if (charArray[charArray.length - 1] === "\\") { |
188 |
| - charArray.pop(); |
189 |
| - escapeCharacter("\\", charArray); |
190 |
| - } |
191 |
| - |
192 |
| - return charArray.join(""); |
193 |
| - }; |
194 |
| - |
195 |
| - // allows embedded javascript string template |
196 |
| - const stringify = (rawString) => |
197 |
| - rawString |
198 |
| - .replace(/[\\]/g, "\\\\") |
199 |
| - .replace(/[`]/g, "\\`") |
200 |
| - .replace(/\${/g, "\\${"); |
201 |
| - |
202 |
| - var simpleYenc = { |
203 |
| - encode, |
204 |
| - dynamicEncode, |
205 |
| - decode, |
206 |
| - stringify, |
207 |
| - }; |
| 7 | + const r = (t) => { |
| 8 | + const r = new Map(); |
| 9 | + [ |
| 10 | + , |
| 11 | + 8364, |
| 12 | + , |
| 13 | + 8218, |
| 14 | + 402, |
| 15 | + 8222, |
| 16 | + 8230, |
| 17 | + 8224, |
| 18 | + 8225, |
| 19 | + 710, |
| 20 | + 8240, |
| 21 | + 352, |
| 22 | + 8249, |
| 23 | + 338, |
| 24 | + , |
| 25 | + 381, |
| 26 | + , |
| 27 | + , |
| 28 | + 8216, |
| 29 | + 8217, |
| 30 | + 8220, |
| 31 | + 8221, |
| 32 | + 8226, |
| 33 | + 8211, |
| 34 | + 8212, |
| 35 | + 732, |
| 36 | + 8482, |
| 37 | + 353, |
| 38 | + 8250, |
| 39 | + 339, |
| 40 | + , |
| 41 | + 382, |
| 42 | + 376, |
| 43 | + ].forEach((t, e) => r.set(t, e)); |
| 44 | + const e = new Uint8Array(t.length); |
| 45 | + let n, |
| 46 | + o = !1, |
| 47 | + s = 0, |
| 48 | + l = 42, |
| 49 | + g = 0; |
| 50 | + t.length > 13 && |
| 51 | + "dynEncode" === t.substring(0, 9) && |
| 52 | + 0 === parseInt(t.substring(9, 11), 16) && |
| 53 | + ((l = parseInt(t.substring(11, 13), 16)), (g = 13)); |
| 54 | + const c = 256 - l; |
| 55 | + for (let a = g; a < t.length; a++) |
| 56 | + if (((n = t.charCodeAt(a)), 61 !== n || o)) { |
| 57 | + if (n > 255) { |
| 58 | + const t = r.get(n); |
| 59 | + t && (n = t + 127); |
| 60 | + } |
| 61 | + o && ((o = !1), (n -= 64)), (e[s++] = n < l && n > 0 ? n + c : n - l); |
| 62 | + } else o = !0; |
| 63 | + return e.subarray(0, s); |
| 64 | + }; |
208 | 65 |
|
209 | 66 | /* Copyright 2022 Ethan Halsall
|
210 | 67 |
|
|
247 | 104 | if (!this._module) {
|
248 | 105 | this._module = simd().then((simdSupported) =>
|
249 | 106 | simdSupported
|
250 |
| - ? WebAssembly.compile(simpleYenc.decode(simdWasm)) |
251 |
| - : WebAssembly.compile(simpleYenc.decode(scalarWasm)) |
| 107 | + ? WebAssembly.compile(r(simdWasm)) |
| 108 | + : WebAssembly.compile(r(scalarWasm)) |
252 | 109 | );
|
253 | 110 | wasmModule.set(this._module);
|
254 | 111 | }
|
|
0 commit comments