@queue-it/fastly 1.1.0 → 2.0.0-beta.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.
@@ -1,308 +0,0 @@
1
- const enum CharCode {
2
- PERCENT = 0x25,
3
- _0 = 0x30,
4
- a = 0x61,
5
- }
6
-
7
- export class URIError extends Error {
8
- constructor(message: string = "") {
9
- super(message);
10
- this.name = "URIError";
11
- }
12
- }
13
-
14
- // @ts-ignore: decorator
15
- @lazy @inline
16
- export const E_URI_MALFORMED: string = "URI malformed";
17
-
18
- // Truncated lookup boolean table that helps us quickly determine
19
- // if a char needs to be escaped for URIs (RFC 2396).
20
- // @ts-ignore: decorator
21
- @lazy export const URI_UNSAFE = memory.data<u8>([
22
- /* skip 32 + 1 always set to '1' head slots
23
- */ 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
25
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
27
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, /*
29
- skip 128 + 1 always set to '1' tail slots */
30
- ]);
31
-
32
- // Truncated lookup boolean table that helps us quickly determine
33
- // if a char needs to be escaped for URLs (RFC 3986).
34
- // @ts-ignore: decorator
35
- @lazy export const URL_UNSAFE = memory.data<u8>([
36
- /* skip 32 + 1 always set to '1' head slots
37
- */ 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1,
38
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
39
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
41
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, /*
43
- skip 128 + 1 always set to '1' tail slots */
44
- ]);
45
-
46
- // Truncated lookup boolean table for determine reserved chars: ;/?:@&=+$,#
47
- // @ts-ignore: decorator
48
- @lazy export const URI_RESERVED = memory.data<u8>([
49
- /* skip 32 + 3 always set to '0' head slots
50
- */ 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1,
51
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1,
52
- 1, /* skip 191 always set to '0' tail slots */
53
- ]);
54
-
55
- export function encode(src: usize, len: usize, table: usize): usize {
56
- if (!len) return src;
57
-
58
- var i: usize = 0, offset: usize = 0, outSize = len << 1;
59
- var dst = __new(outSize, idof<String>());
60
-
61
- while (i < len) {
62
- let org = i;
63
- let c: u32, c1: u32;
64
- // fast scan a check chars until it valid ASCII
65
- // and safe for copying withoud escaping.
66
- do {
67
- c = <u32>load<u16>(src + (i << 1));
68
- // is it valid ASII and safe?
69
- if (c - 33 < 94) { // 127 - 33
70
- if (load<u8>(table + (c - 33))) break;
71
- } else break;
72
- } while (++i < len);
73
-
74
- // if we have some safe range of sequence just copy it without encoding
75
- if (i > org) {
76
- let size = i - org << 1;
77
- if (offset + size > outSize) {
78
- outSize = offset + size;
79
- dst = __renew(dst, outSize);
80
- }
81
- // TODO: should we optimize for short cases like 2 byte size?
82
- memory.copy(
83
- dst + offset,
84
- src + (org << 1),
85
- size
86
- );
87
- offset += size;
88
- // return if we reach end on input string
89
- if (i >= len) break;
90
- }
91
-
92
- // decode UTF16 with checking for unpaired surrogates
93
- if (c >= 0xD800) {
94
- if (c >= 0xDC00 && c <= 0xDFFF) {
95
- throw new URIError(E_URI_MALFORMED);
96
- }
97
- if (c <= 0xDBFF) {
98
- if (i >= len) {
99
- throw new URIError(E_URI_MALFORMED);
100
- }
101
- c1 = <u32>load<u16>(src + (++i << 1));
102
- if (c1 < 0xDC00 || c1 > 0xDFFF) {
103
- throw new URIError(E_URI_MALFORMED);
104
- }
105
- c = (((c & 0x3FF) << 10) | (c1 & 0x3FF)) + 0x10000;
106
- }
107
- }
108
-
109
- let estSize = offset + (c < 0x80 ? 1 * 6 : 4 * 6);
110
- if (estSize > outSize) {
111
- // doubling estimated size but only for greater than one
112
- // input lenght due to we already estemated it for worst case
113
- outSize = len > 1 ? estSize << 1 : estSize;
114
- dst = __renew(dst, outSize);
115
- }
116
-
117
- if (c < 0x80) {
118
- // encode ASCII unsafe code point
119
- storeHex(dst, offset, c);
120
- offset += 6;
121
- } else {
122
- // encode UTF-8 unsafe code point
123
- if (c < 0x800) {
124
- storeHex(dst, offset, (c >> 6) | 0xC0);
125
- offset += 6;
126
- } else {
127
- if (c < 0x10000) {
128
- storeHex(dst, offset, (c >> 12) | 0xE0);
129
- offset += 6;
130
- } else {
131
- storeHex(dst, offset, (c >> 18) | 0xF0);
132
- offset += 6;
133
- storeHex(dst, offset, (c >> 12 & 0x3F) | 0x80);
134
- offset += 6;
135
- }
136
- storeHex(dst, offset, (c >> 6 & 0x3F) | 0x80);
137
- offset += 6;
138
- }
139
- storeHex(dst, offset, (c & 0x3F) | 0x80);
140
- offset += 6;
141
- }
142
- ++i;
143
- }
144
- // shink output string buffer if necessary
145
- if (outSize > offset) {
146
- dst = __renew(dst, offset);
147
- }
148
- return dst;
149
- }
150
-
151
- export function decode(src: usize, len: usize, component: bool): usize {
152
- if (!len) return src;
153
-
154
- var i: usize = 0, offset: usize = 0, ch: u32 = 0;
155
- var dst = __new(len << 1, idof<String>());
156
-
157
- while (i < len) {
158
- let org = i;
159
- while (i < len && (ch = load<u16>(src + (i << 1))) != CharCode.PERCENT) i++;
160
-
161
- if (i > org) {
162
- let size = i - org << 1;
163
- // TODO: should we optimize for short cases like 2 byte size?
164
- memory.copy(
165
- dst + offset,
166
- src + (org << 1),
167
- size
168
- );
169
- offset += size;
170
- if (i >= len) break;
171
- }
172
-
173
- // decode hex
174
- if (
175
- i + 2 >= len ||
176
- ch != CharCode.PERCENT ||
177
- (ch = loadHex(src, i + 1 << 1)) == -1
178
- ) {
179
- break;
180
- }
181
-
182
- i += 3;
183
- if (ch < 0x80) {
184
- if (!component && isReserved(ch)) {
185
- ch = CharCode.PERCENT;
186
- i -= 2;
187
- }
188
- } else {
189
- // decode UTF-8 sequence
190
- let nb = utf8LenFromUpperByte(ch);
191
- // minimal surrogate: 2 => 0x80, 3 => 0x800, 4 => 0x10000, _ => -1
192
- let lo: u32 = 1 << (17 * nb >> 2) - 1;
193
- // mask: 2 => 31, 3 => 15, 4 => 7, _ => 0
194
- ch &= nb ? (0x80 >> nb) - 1 : 0;
195
-
196
- while (--nb != 0) {
197
- let c1: u32;
198
- // decode hex
199
- if (
200
- i + 2 >= len ||
201
- load<u16>(src + (i << 1)) != CharCode.PERCENT ||
202
- (c1 = loadHex(src, i + 1 << 1)) == -1
203
- ) throw new URIError(E_URI_MALFORMED);
204
-
205
- i += 3;
206
- if ((c1 & 0xC0) != 0x80) {
207
- ch = 0;
208
- break;
209
- }
210
- ch = (ch << 6) | (c1 & 0x3F);
211
- }
212
-
213
- // check if UTF8 code point properly fit into invalid UTF16 encoding
214
- if (ch < lo || lo == -1 || ch > 0x10FFFF || (ch >= 0xD800 && ch < 0xE000)) {
215
- break;
216
- }
217
-
218
- // encode UTF16
219
- if (ch >= 0x10000) {
220
- ch -= 0x10000;
221
- let lo = ch >> 10 | 0xD800;
222
- let hi = (ch & 0x03FF) | 0xDC00;
223
- store<u32>(dst + offset, lo | (hi << 16));
224
- offset += 4;
225
- continue;
226
- }
227
- }
228
- store<u16>(dst + offset, ch);
229
- offset += 2;
230
- }
231
-
232
- assert(offset <= (len << 1));
233
- // shink output string buffer if necessary
234
- if ((len << 1) > offset) {
235
- dst = __renew(dst, offset);
236
- }
237
- return dst;
238
- }
239
-
240
- function storeHex(dst: usize, offset: usize, ch: u32): void {
241
- // @ts-ignore: decorator
242
- const HEX_CHARS = memory.data<u8>([
243
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
244
- 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46
245
- ]);
246
-
247
- store<u16>(dst + offset, CharCode.PERCENT, 0); // %
248
- store<u32>(
249
- dst + offset,
250
- <u32>load<u8>(HEX_CHARS + (ch >> 4 & 0x0F)) |
251
- <u32>load<u8>(HEX_CHARS + (ch & 0x0F)) << 16,
252
- 2
253
- ); // XX
254
- }
255
-
256
- function loadHex(src: usize, offset: usize): u32 {
257
- let c0 = <u32>load<u16>(src + offset, 0);
258
- let c1 = <u32>load<u16>(src + offset, 2);
259
- return isHex(c0) && isHex(c1)
260
- ? fromHex(c0) << 4 | fromHex(c1)
261
- : -1;
262
- }
263
-
264
- // @ts-ignore: decorator
265
- @inline function fromHex(ch: u32): u32 {
266
- return (ch | 32) % 39 - 9;
267
- }
268
-
269
- // @ts-ignore: decorator
270
- @inline function utf8LenFromUpperByte(c0: u32): u32 {
271
- // same as
272
- // if (c0 - 0xC0 <= 0xDF - 0xC0) return 2;
273
- // if (c0 - 0xE0 <= 0xEF - 0xE0) return 3;
274
- // if (c0 - 0xF0 <= 0xF7 - 0xF0) return 4;
275
- // return 0;
276
- return c0 - 0xC0 < 56
277
- ? clz(~(c0 << 24))
278
- : 0;
279
- }
280
-
281
- // @ts-ignore: decorator
282
- @inline function isReserved(ch: u32): bool {
283
- return ch - 35 < 30
284
- ? <bool>load<u8>(URI_RESERVED + (ch - 35))
285
- : false;
286
- }
287
-
288
- // @ts-ignore: decorator
289
- @inline function isHex(ch: u32): bool {
290
- // @ts-ignore
291
- return (ch - CharCode._0 < 10) | ((ch | 32) - CharCode.a < 6);
292
- }
293
-
294
- export function encodeURI(str: string): string {
295
- return changetype<string>(encode(changetype<usize>(str), str.length, URI_UNSAFE));
296
- }
297
-
298
- export function decodeURI(str: string): string {
299
- return changetype<string>(decode(changetype<usize>(str), str.length, false));
300
- }
301
-
302
- export function encodeURIComponent(str: string): string {
303
- return changetype<string>(encode(changetype<usize>(str), str.length, URL_UNSAFE));
304
- }
305
-
306
- export function decodeURIComponent(str: string): string {
307
- return changetype<string>(decode(changetype<usize>(str), str.length, true));
308
- }
File without changes