@queue-it/fastly 1.1.0 → 2.0.0-beta.1
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.
- package/package.json +16 -21
- package/{assembly → src}/contextProvider.ts +12 -8
- package/{assembly → src}/index.ts +1 -1
- package/{assembly → src}/integrationConfigProvider.ts +20 -23
- package/{assembly → src}/requestResponseHandler.ts +9 -15
- package/{assembly → src}/sdk/HttpContextProvider.ts +1 -2
- package/src/sdk/IntegrationConfig/CustomerIntegrationDecodingHandler.ts +57 -0
- package/{assembly → src}/sdk/IntegrationConfig/IntegrationConfigHelpers.ts +14 -16
- package/{assembly → src}/sdk/IntegrationConfig/IntegrationConfigModel.ts +5 -5
- package/{assembly → src}/sdk/KnownUser.ts +7 -9
- package/{assembly → src}/sdk/Models.ts +6 -6
- package/{assembly → src}/sdk/QueueITHelpers.ts +33 -29
- package/{assembly → src}/sdk/UserInQueueService.ts +4 -4
- package/{assembly → src}/sdk/UserInQueueStateCookieRepository.ts +26 -27
- package/src/sdk/helpers/Uri.ts +4 -0
- package/{assembly → src}/sdk/helpers/crypto.ts +28 -30
- package/LICENSE +0 -21
- package/assembly/sdk/IntegrationConfig/CustomerIntegrationDecodingHandler.ts +0 -221
- package/assembly/sdk/helpers/Uri.ts +0 -308
- /package/{assembly → src}/helper.ts +0 -0
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import {JSONHandler} from "assemblyscript-json";
|
|
2
|
-
import {
|
|
3
|
-
CustomerIntegration,
|
|
4
|
-
IntegrationConfigModel,
|
|
5
|
-
TriggerModel,
|
|
6
|
-
TriggerPart
|
|
7
|
-
} from "./IntegrationConfigModel";
|
|
8
|
-
import {JSONDecoder} from "assemblyscript-json";
|
|
9
|
-
|
|
10
|
-
const ARRAY_NAME_INTEGRATIONS = "Integrations";
|
|
11
|
-
const ARRAY_NAME_TRIGGERS = "Triggers";
|
|
12
|
-
const ARRAY_NAME_TRIGGER_PARTS = "TriggerParts";
|
|
13
|
-
const ARRAY_NAME_VALUES_TO_COMPARE = "ValuesToCompare";
|
|
14
|
-
|
|
15
|
-
export class CustomerIntegrationDecodingHandler extends JSONHandler {
|
|
16
|
-
private readonly _deserializedValue: CustomerIntegration;
|
|
17
|
-
private isInIntegrations: bool = false;
|
|
18
|
-
private isInTriggers: bool = false;
|
|
19
|
-
private isInConfigModel: bool = false;
|
|
20
|
-
private isInTriggerModel: bool = false;
|
|
21
|
-
private isInTriggerPart: bool = false;
|
|
22
|
-
private currentIntegrationConfigModel: IntegrationConfigModel | null;
|
|
23
|
-
private currentTriggerModel: TriggerModel | null;
|
|
24
|
-
private currentTriggerPart: TriggerPart | null;
|
|
25
|
-
private isInTriggerParts: bool = false;
|
|
26
|
-
private isInTriggerPartValuesToCompare: bool = false;
|
|
27
|
-
private arrayStack: string[] = [];
|
|
28
|
-
|
|
29
|
-
constructor() {
|
|
30
|
-
super();
|
|
31
|
-
this._deserializedValue = new CustomerIntegration();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
pushArray(name: string): bool {
|
|
35
|
-
// Handle array start
|
|
36
|
-
// true means that nested object needs to be traversed, false otherwise
|
|
37
|
-
// Note that returning false means JSONDecoder.startIndex need to be updated by handler
|
|
38
|
-
|
|
39
|
-
// Push array name to stack BEFORE checking if we recognize it
|
|
40
|
-
// This ensures every pushArray has a matching popArray
|
|
41
|
-
this.arrayStack.push(name);
|
|
42
|
-
|
|
43
|
-
if (name == ARRAY_NAME_INTEGRATIONS) {
|
|
44
|
-
this.isInIntegrations = true;
|
|
45
|
-
} else if (name == ARRAY_NAME_TRIGGERS) {
|
|
46
|
-
this.isInTriggers = true;
|
|
47
|
-
} else if (name == ARRAY_NAME_TRIGGER_PARTS) {
|
|
48
|
-
this.isInTriggerParts = true;
|
|
49
|
-
} else if (name == ARRAY_NAME_VALUES_TO_COMPARE) {
|
|
50
|
-
this.isInTriggerPartValuesToCompare = true;
|
|
51
|
-
}
|
|
52
|
-
return this.isInIntegrations
|
|
53
|
-
|| this.isInTriggers
|
|
54
|
-
|| this.isInTriggerParts
|
|
55
|
-
|| this.isInTriggerPartValuesToCompare;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
popArray(): void {
|
|
60
|
-
// Pop array name from stack
|
|
61
|
-
if (this.arrayStack.length == 0) {
|
|
62
|
-
return; // Defensive: should never happen
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const arrayName = this.arrayStack.pop();
|
|
66
|
-
|
|
67
|
-
// Only clear flags for recognized arrays
|
|
68
|
-
if (arrayName == ARRAY_NAME_VALUES_TO_COMPARE) {
|
|
69
|
-
this.isInTriggerPartValuesToCompare = false;
|
|
70
|
-
} else if (arrayName == ARRAY_NAME_TRIGGER_PARTS) {
|
|
71
|
-
this.isInTriggerParts = false;
|
|
72
|
-
} else if (arrayName == ARRAY_NAME_TRIGGERS) {
|
|
73
|
-
this.isInTriggers = false;
|
|
74
|
-
} else if (arrayName == ARRAY_NAME_INTEGRATIONS) {
|
|
75
|
-
this.isInIntegrations = false;
|
|
76
|
-
}
|
|
77
|
-
// Note: Unrecognized arrays (like InvolvedWaitingRoomIds) are popped but don't affect flags
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
pushObject(name: string): bool {
|
|
81
|
-
if (this.isInTriggerParts) {
|
|
82
|
-
this.isInTriggerPart = true;
|
|
83
|
-
this.currentTriggerPart = new TriggerPart();
|
|
84
|
-
} else if (this.isInTriggers) {
|
|
85
|
-
this.isInTriggerModel = true;
|
|
86
|
-
this.currentTriggerModel = new TriggerModel();
|
|
87
|
-
} else if (this.isInIntegrations) {
|
|
88
|
-
this.isInConfigModel = true;
|
|
89
|
-
this.currentIntegrationConfigModel = new IntegrationConfigModel();
|
|
90
|
-
}
|
|
91
|
-
return super.pushObject(name);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
popObject(): void {
|
|
95
|
-
if (this.isInTriggerPart) {
|
|
96
|
-
this.currentTriggerModel!.TriggerParts.push(this.currentTriggerPart!);
|
|
97
|
-
this.isInTriggerPart = false;
|
|
98
|
-
} else if (this.isInTriggerModel) {
|
|
99
|
-
// Defensive: only add trigger if we have a valid integration
|
|
100
|
-
if (this.currentIntegrationConfigModel != null) {
|
|
101
|
-
this.currentIntegrationConfigModel!.Triggers.push(this.currentTriggerModel!);
|
|
102
|
-
}
|
|
103
|
-
this.isInTriggerModel = false;
|
|
104
|
-
} else if (this.isInConfigModel) {
|
|
105
|
-
this._deserializedValue.Integrations.push(this.currentIntegrationConfigModel!);
|
|
106
|
-
this.isInConfigModel = false;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
setInteger(name: string, value: i64): void {
|
|
111
|
-
if (this.isInConfigModel) {
|
|
112
|
-
this.setConfigModelInteger(name, value);
|
|
113
|
-
} else if (name == "Version") {
|
|
114
|
-
this._deserializedValue.Version = value;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
setBoolean(name: string, value: bool): void {
|
|
119
|
-
if (this.isInTriggerPart) {
|
|
120
|
-
this.setTriggerPartBoolean(name, value);
|
|
121
|
-
} else if (this.isInConfigModel) {
|
|
122
|
-
this.setConfigModelBoolean(name, value);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
setString(name: string, value: string): void {
|
|
127
|
-
if (this.isInTriggerPartValuesToCompare) {
|
|
128
|
-
this.addTriggerPartValuesToCompare(value);
|
|
129
|
-
} else if (this.isInTriggerPart) {
|
|
130
|
-
this.setTriggerPartString(name, value);
|
|
131
|
-
} else if (this.isInTriggerModel) {
|
|
132
|
-
this.setTriggerModelString(name, value);
|
|
133
|
-
} else if (this.isInConfigModel) {
|
|
134
|
-
this.setConfigModelString(name, value);
|
|
135
|
-
} else if (name == "Description") {
|
|
136
|
-
this._deserializedValue.Description = value;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
setConfigModelBoolean(name: string, value: bool): void {
|
|
141
|
-
if (name == "ExtendCookieValidity") {
|
|
142
|
-
this.currentIntegrationConfigModel!.ExtendCookieValidity = value;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
setTriggerPartBoolean(name: string, value: bool): void {
|
|
147
|
-
if (name == "IsNegative") {
|
|
148
|
-
this.currentTriggerPart!.IsNegative = value;
|
|
149
|
-
} else if (name == "IsIgnoreCase") {
|
|
150
|
-
this.currentTriggerPart!.IsIgnoreCase = value;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
setConfigModelInteger(name: string, value: i64): void {
|
|
155
|
-
if (name == "CookieValidityMinute") {
|
|
156
|
-
this.currentIntegrationConfigModel!.CookieValidityMinute = value;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
addTriggerPartValuesToCompare(value: string): void {
|
|
161
|
-
this.currentTriggerPart!.ValuesToCompare.push(value);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
setTriggerPartString(name: string, value: string): void {
|
|
165
|
-
if (name == "ValidatorType") {
|
|
166
|
-
this.currentTriggerPart!.ValidatorType = value;
|
|
167
|
-
} else if (name == "Operator") {
|
|
168
|
-
this.currentTriggerPart!.Operator = value;
|
|
169
|
-
} else if (name == "ValueToCompare") {
|
|
170
|
-
this.currentTriggerPart!.ValueToCompare = value;
|
|
171
|
-
} else if (name == "UrlPart") {
|
|
172
|
-
this.currentTriggerPart!.UrlPart = value;
|
|
173
|
-
} else if (name == "CookieName") {
|
|
174
|
-
this.currentTriggerPart!.CookieName = value;
|
|
175
|
-
} else if (name == "HttpHeaderName") {
|
|
176
|
-
this.currentTriggerPart!.HttpHeaderName = value;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
setTriggerModelString(name: string, value: string): void {
|
|
181
|
-
if (name == "LogicalOperator") {
|
|
182
|
-
this.currentTriggerModel!.LogicalOperator = value;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
setConfigModelString(name: string, value: string): void {
|
|
187
|
-
if (name == "Name") {
|
|
188
|
-
this.currentIntegrationConfigModel!.Name = value;
|
|
189
|
-
} else if (name == "EventId") {
|
|
190
|
-
this.currentIntegrationConfigModel!.EventId = value;
|
|
191
|
-
} else if (name == "CookieDomain") {
|
|
192
|
-
this.currentIntegrationConfigModel!.CookieDomain = value;
|
|
193
|
-
} else if (name == "LayoutName") {
|
|
194
|
-
this.currentIntegrationConfigModel!.LayoutName = value;
|
|
195
|
-
} else if (name == "Culture") {
|
|
196
|
-
this.currentIntegrationConfigModel!.Culture = value;
|
|
197
|
-
} else if (name == "QueueDomain") {
|
|
198
|
-
this.currentIntegrationConfigModel!.QueueDomain = value;
|
|
199
|
-
} else if (name == "RedirectLogic") {
|
|
200
|
-
this.currentIntegrationConfigModel!.RedirectLogic = value;
|
|
201
|
-
} else if (name == "ForcedTargetUrl") {
|
|
202
|
-
this.currentIntegrationConfigModel!.ForcedTargetUrl = value;
|
|
203
|
-
} else if (name == "ActionType") {
|
|
204
|
-
this.currentIntegrationConfigModel!.ActionType = value;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
value(): CustomerIntegration {
|
|
209
|
-
return this._deserializedValue;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
static deserialize(integrationsConfigString: string): CustomerIntegration {
|
|
213
|
-
const handler = new CustomerIntegrationDecodingHandler();
|
|
214
|
-
if (integrationsConfigString == '') {
|
|
215
|
-
return handler.value();
|
|
216
|
-
}
|
|
217
|
-
const decoder = new JSONDecoder<CustomerIntegrationDecodingHandler>(handler);
|
|
218
|
-
decoder.deserialize(Uint8Array.wrap(String.UTF8.encode(integrationsConfigString)));
|
|
219
|
-
return handler.value();
|
|
220
|
-
}
|
|
221
|
-
}
|
|
@@ -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
|