@smithy/core 3.3.3 → 3.4.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.
- package/dist-cjs/submodules/cbor/index.js +3 -2
- package/dist-cjs/submodules/serde/index.js +625 -2
- package/dist-es/submodules/cbor/parseCborBody.js +3 -2
- package/dist-es/submodules/serde/date-utils.js +190 -0
- package/dist-es/submodules/serde/index.js +6 -0
- package/dist-es/submodules/serde/lazy-json.js +24 -0
- package/dist-es/submodules/serde/parse-utils.js +230 -0
- package/dist-es/submodules/serde/quote-header.js +6 -0
- package/dist-es/submodules/serde/split-every.js +27 -0
- package/dist-es/submodules/serde/split-header.js +37 -0
- package/dist-types/submodules/serde/date-utils.d.ts +73 -0
- package/dist-types/submodules/serde/index.d.ts +6 -0
- package/dist-types/submodules/serde/lazy-json.d.ts +45 -0
- package/dist-types/submodules/serde/parse-utils.d.ts +270 -0
- package/dist-types/submodules/serde/quote-header.d.ts +6 -0
- package/dist-types/submodules/serde/split-every.d.ts +11 -0
- package/dist-types/submodules/serde/split-header.d.ts +5 -0
- package/dist-types/ts3.4/submodules/serde/date-utils.d.ts +73 -0
- package/dist-types/ts3.4/submodules/serde/index.d.ts +6 -0
- package/dist-types/ts3.4/submodules/serde/lazy-json.d.ts +45 -0
- package/dist-types/ts3.4/submodules/serde/parse-utils.d.ts +270 -0
- package/dist-types/ts3.4/submodules/serde/quote-header.d.ts +6 -0
- package/dist-types/ts3.4/submodules/serde/split-every.d.ts +11 -0
- package/dist-types/ts3.4/submodules/serde/split-header.d.ts +5 -0
- package/package.json +7 -6
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { strictParseByte, strictParseDouble, strictParseFloat32, strictParseShort } from "./parse-utils";
|
|
2
|
+
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
3
|
+
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
4
|
+
export function dateToUtcString(date) {
|
|
5
|
+
const year = date.getUTCFullYear();
|
|
6
|
+
const month = date.getUTCMonth();
|
|
7
|
+
const dayOfWeek = date.getUTCDay();
|
|
8
|
+
const dayOfMonthInt = date.getUTCDate();
|
|
9
|
+
const hoursInt = date.getUTCHours();
|
|
10
|
+
const minutesInt = date.getUTCMinutes();
|
|
11
|
+
const secondsInt = date.getUTCSeconds();
|
|
12
|
+
const dayOfMonthString = dayOfMonthInt < 10 ? `0${dayOfMonthInt}` : `${dayOfMonthInt}`;
|
|
13
|
+
const hoursString = hoursInt < 10 ? `0${hoursInt}` : `${hoursInt}`;
|
|
14
|
+
const minutesString = minutesInt < 10 ? `0${minutesInt}` : `${minutesInt}`;
|
|
15
|
+
const secondsString = secondsInt < 10 ? `0${secondsInt}` : `${secondsInt}`;
|
|
16
|
+
return `${DAYS[dayOfWeek]}, ${dayOfMonthString} ${MONTHS[month]} ${year} ${hoursString}:${minutesString}:${secondsString} GMT`;
|
|
17
|
+
}
|
|
18
|
+
const RFC3339 = new RegExp(/^(\d{4})-(\d{2})-(\d{2})[tT](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?[zZ]$/);
|
|
19
|
+
export const parseRfc3339DateTime = (value) => {
|
|
20
|
+
if (value === null || value === undefined) {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
if (typeof value !== "string") {
|
|
24
|
+
throw new TypeError("RFC-3339 date-times must be expressed as strings");
|
|
25
|
+
}
|
|
26
|
+
const match = RFC3339.exec(value);
|
|
27
|
+
if (!match) {
|
|
28
|
+
throw new TypeError("Invalid RFC-3339 date-time value");
|
|
29
|
+
}
|
|
30
|
+
const [_, yearStr, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds] = match;
|
|
31
|
+
const year = strictParseShort(stripLeadingZeroes(yearStr));
|
|
32
|
+
const month = parseDateValue(monthStr, "month", 1, 12);
|
|
33
|
+
const day = parseDateValue(dayStr, "day", 1, 31);
|
|
34
|
+
return buildDate(year, month, day, { hours, minutes, seconds, fractionalMilliseconds });
|
|
35
|
+
};
|
|
36
|
+
const RFC3339_WITH_OFFSET = new RegExp(/^(\d{4})-(\d{2})-(\d{2})[tT](\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?(([-+]\d{2}\:\d{2})|[zZ])$/);
|
|
37
|
+
export const parseRfc3339DateTimeWithOffset = (value) => {
|
|
38
|
+
if (value === null || value === undefined) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
if (typeof value !== "string") {
|
|
42
|
+
throw new TypeError("RFC-3339 date-times must be expressed as strings");
|
|
43
|
+
}
|
|
44
|
+
const match = RFC3339_WITH_OFFSET.exec(value);
|
|
45
|
+
if (!match) {
|
|
46
|
+
throw new TypeError("Invalid RFC-3339 date-time value");
|
|
47
|
+
}
|
|
48
|
+
const [_, yearStr, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds, offsetStr] = match;
|
|
49
|
+
const year = strictParseShort(stripLeadingZeroes(yearStr));
|
|
50
|
+
const month = parseDateValue(monthStr, "month", 1, 12);
|
|
51
|
+
const day = parseDateValue(dayStr, "day", 1, 31);
|
|
52
|
+
const date = buildDate(year, month, day, { hours, minutes, seconds, fractionalMilliseconds });
|
|
53
|
+
if (offsetStr.toUpperCase() != "Z") {
|
|
54
|
+
date.setTime(date.getTime() - parseOffsetToMilliseconds(offsetStr));
|
|
55
|
+
}
|
|
56
|
+
return date;
|
|
57
|
+
};
|
|
58
|
+
const IMF_FIXDATE = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\d{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (\d{4}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
|
|
59
|
+
const RFC_850_DATE = new RegExp(/^(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\d{2})-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-(\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? GMT$/);
|
|
60
|
+
const ASC_TIME = new RegExp(/^(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( [1-9]|\d{2}) (\d{1,2}):(\d{2}):(\d{2})(?:\.(\d+))? (\d{4})$/);
|
|
61
|
+
export const parseRfc7231DateTime = (value) => {
|
|
62
|
+
if (value === null || value === undefined) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
if (typeof value !== "string") {
|
|
66
|
+
throw new TypeError("RFC-7231 date-times must be expressed as strings");
|
|
67
|
+
}
|
|
68
|
+
let match = IMF_FIXDATE.exec(value);
|
|
69
|
+
if (match) {
|
|
70
|
+
const [_, dayStr, monthStr, yearStr, hours, minutes, seconds, fractionalMilliseconds] = match;
|
|
71
|
+
return buildDate(strictParseShort(stripLeadingZeroes(yearStr)), parseMonthByShortName(monthStr), parseDateValue(dayStr, "day", 1, 31), { hours, minutes, seconds, fractionalMilliseconds });
|
|
72
|
+
}
|
|
73
|
+
match = RFC_850_DATE.exec(value);
|
|
74
|
+
if (match) {
|
|
75
|
+
const [_, dayStr, monthStr, yearStr, hours, minutes, seconds, fractionalMilliseconds] = match;
|
|
76
|
+
return adjustRfc850Year(buildDate(parseTwoDigitYear(yearStr), parseMonthByShortName(monthStr), parseDateValue(dayStr, "day", 1, 31), {
|
|
77
|
+
hours,
|
|
78
|
+
minutes,
|
|
79
|
+
seconds,
|
|
80
|
+
fractionalMilliseconds,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
match = ASC_TIME.exec(value);
|
|
84
|
+
if (match) {
|
|
85
|
+
const [_, monthStr, dayStr, hours, minutes, seconds, fractionalMilliseconds, yearStr] = match;
|
|
86
|
+
return buildDate(strictParseShort(stripLeadingZeroes(yearStr)), parseMonthByShortName(monthStr), parseDateValue(dayStr.trimLeft(), "day", 1, 31), { hours, minutes, seconds, fractionalMilliseconds });
|
|
87
|
+
}
|
|
88
|
+
throw new TypeError("Invalid RFC-7231 date-time value");
|
|
89
|
+
};
|
|
90
|
+
export const parseEpochTimestamp = (value) => {
|
|
91
|
+
if (value === null || value === undefined) {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
let valueAsDouble;
|
|
95
|
+
if (typeof value === "number") {
|
|
96
|
+
valueAsDouble = value;
|
|
97
|
+
}
|
|
98
|
+
else if (typeof value === "string") {
|
|
99
|
+
valueAsDouble = strictParseDouble(value);
|
|
100
|
+
}
|
|
101
|
+
else if (typeof value === "object" && value.tag === 1) {
|
|
102
|
+
valueAsDouble = value.value;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
throw new TypeError("Epoch timestamps must be expressed as floating point numbers or their string representation");
|
|
106
|
+
}
|
|
107
|
+
if (Number.isNaN(valueAsDouble) || valueAsDouble === Infinity || valueAsDouble === -Infinity) {
|
|
108
|
+
throw new TypeError("Epoch timestamps must be valid, non-Infinite, non-NaN numerics");
|
|
109
|
+
}
|
|
110
|
+
return new Date(Math.round(valueAsDouble * 1000));
|
|
111
|
+
};
|
|
112
|
+
const buildDate = (year, month, day, time) => {
|
|
113
|
+
const adjustedMonth = month - 1;
|
|
114
|
+
validateDayOfMonth(year, adjustedMonth, day);
|
|
115
|
+
return new Date(Date.UTC(year, adjustedMonth, day, parseDateValue(time.hours, "hour", 0, 23), parseDateValue(time.minutes, "minute", 0, 59), parseDateValue(time.seconds, "seconds", 0, 60), parseMilliseconds(time.fractionalMilliseconds)));
|
|
116
|
+
};
|
|
117
|
+
const parseTwoDigitYear = (value) => {
|
|
118
|
+
const thisYear = new Date().getUTCFullYear();
|
|
119
|
+
const valueInThisCentury = Math.floor(thisYear / 100) * 100 + strictParseShort(stripLeadingZeroes(value));
|
|
120
|
+
if (valueInThisCentury < thisYear) {
|
|
121
|
+
return valueInThisCentury + 100;
|
|
122
|
+
}
|
|
123
|
+
return valueInThisCentury;
|
|
124
|
+
};
|
|
125
|
+
const FIFTY_YEARS_IN_MILLIS = 50 * 365 * 24 * 60 * 60 * 1000;
|
|
126
|
+
const adjustRfc850Year = (input) => {
|
|
127
|
+
if (input.getTime() - new Date().getTime() > FIFTY_YEARS_IN_MILLIS) {
|
|
128
|
+
return new Date(Date.UTC(input.getUTCFullYear() - 100, input.getUTCMonth(), input.getUTCDate(), input.getUTCHours(), input.getUTCMinutes(), input.getUTCSeconds(), input.getUTCMilliseconds()));
|
|
129
|
+
}
|
|
130
|
+
return input;
|
|
131
|
+
};
|
|
132
|
+
const parseMonthByShortName = (value) => {
|
|
133
|
+
const monthIdx = MONTHS.indexOf(value);
|
|
134
|
+
if (monthIdx < 0) {
|
|
135
|
+
throw new TypeError(`Invalid month: ${value}`);
|
|
136
|
+
}
|
|
137
|
+
return monthIdx + 1;
|
|
138
|
+
};
|
|
139
|
+
const DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
|
|
140
|
+
const validateDayOfMonth = (year, month, day) => {
|
|
141
|
+
let maxDays = DAYS_IN_MONTH[month];
|
|
142
|
+
if (month === 1 && isLeapYear(year)) {
|
|
143
|
+
maxDays = 29;
|
|
144
|
+
}
|
|
145
|
+
if (day > maxDays) {
|
|
146
|
+
throw new TypeError(`Invalid day for ${MONTHS[month]} in ${year}: ${day}`);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const isLeapYear = (year) => {
|
|
150
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
151
|
+
};
|
|
152
|
+
const parseDateValue = (value, type, lower, upper) => {
|
|
153
|
+
const dateVal = strictParseByte(stripLeadingZeroes(value));
|
|
154
|
+
if (dateVal < lower || dateVal > upper) {
|
|
155
|
+
throw new TypeError(`${type} must be between ${lower} and ${upper}, inclusive`);
|
|
156
|
+
}
|
|
157
|
+
return dateVal;
|
|
158
|
+
};
|
|
159
|
+
const parseMilliseconds = (value) => {
|
|
160
|
+
if (value === null || value === undefined) {
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
return strictParseFloat32("0." + value) * 1000;
|
|
164
|
+
};
|
|
165
|
+
const parseOffsetToMilliseconds = (value) => {
|
|
166
|
+
const directionStr = value[0];
|
|
167
|
+
let direction = 1;
|
|
168
|
+
if (directionStr == "+") {
|
|
169
|
+
direction = 1;
|
|
170
|
+
}
|
|
171
|
+
else if (directionStr == "-") {
|
|
172
|
+
direction = -1;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
throw new TypeError(`Offset direction, ${directionStr}, must be "+" or "-"`);
|
|
176
|
+
}
|
|
177
|
+
const hour = Number(value.substring(1, 3));
|
|
178
|
+
const minute = Number(value.substring(4, 6));
|
|
179
|
+
return direction * (hour * 60 + minute) * 60 * 1000;
|
|
180
|
+
};
|
|
181
|
+
const stripLeadingZeroes = (value) => {
|
|
182
|
+
let idx = 0;
|
|
183
|
+
while (idx < value.length - 1 && value.charAt(idx) === "0") {
|
|
184
|
+
idx++;
|
|
185
|
+
}
|
|
186
|
+
if (idx === 0) {
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
return value.slice(idx);
|
|
190
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const LazyJsonString = function LazyJsonString(val) {
|
|
2
|
+
const str = Object.assign(new String(val), {
|
|
3
|
+
deserializeJSON() {
|
|
4
|
+
return JSON.parse(String(val));
|
|
5
|
+
},
|
|
6
|
+
toString() {
|
|
7
|
+
return String(val);
|
|
8
|
+
},
|
|
9
|
+
toJSON() {
|
|
10
|
+
return String(val);
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
return str;
|
|
14
|
+
};
|
|
15
|
+
LazyJsonString.from = (object) => {
|
|
16
|
+
if (object && typeof object === "object" && (object instanceof LazyJsonString || "deserializeJSON" in object)) {
|
|
17
|
+
return object;
|
|
18
|
+
}
|
|
19
|
+
else if (typeof object === "string" || Object.getPrototypeOf(object) === String.prototype) {
|
|
20
|
+
return LazyJsonString(String(object));
|
|
21
|
+
}
|
|
22
|
+
return LazyJsonString(JSON.stringify(object));
|
|
23
|
+
};
|
|
24
|
+
LazyJsonString.fromObject = LazyJsonString.from;
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
export const parseBoolean = (value) => {
|
|
2
|
+
switch (value) {
|
|
3
|
+
case "true":
|
|
4
|
+
return true;
|
|
5
|
+
case "false":
|
|
6
|
+
return false;
|
|
7
|
+
default:
|
|
8
|
+
throw new Error(`Unable to parse boolean value "${value}"`);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
export const expectBoolean = (value) => {
|
|
12
|
+
if (value === null || value === undefined) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
if (typeof value === "number") {
|
|
16
|
+
if (value === 0 || value === 1) {
|
|
17
|
+
logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`));
|
|
18
|
+
}
|
|
19
|
+
if (value === 0) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
if (value === 1) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (typeof value === "string") {
|
|
27
|
+
const lower = value.toLowerCase();
|
|
28
|
+
if (lower === "false" || lower === "true") {
|
|
29
|
+
logger.warn(stackTraceWarning(`Expected boolean, got ${typeof value}: ${value}`));
|
|
30
|
+
}
|
|
31
|
+
if (lower === "false") {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if (lower === "true") {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (typeof value === "boolean") {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
throw new TypeError(`Expected boolean, got ${typeof value}: ${value}`);
|
|
42
|
+
};
|
|
43
|
+
export const expectNumber = (value) => {
|
|
44
|
+
if (value === null || value === undefined) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (typeof value === "string") {
|
|
48
|
+
const parsed = parseFloat(value);
|
|
49
|
+
if (!Number.isNaN(parsed)) {
|
|
50
|
+
if (String(parsed) !== String(value)) {
|
|
51
|
+
logger.warn(stackTraceWarning(`Expected number but observed string: ${value}`));
|
|
52
|
+
}
|
|
53
|
+
return parsed;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (typeof value === "number") {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
throw new TypeError(`Expected number, got ${typeof value}: ${value}`);
|
|
60
|
+
};
|
|
61
|
+
const MAX_FLOAT = Math.ceil(2 ** 127 * (2 - 2 ** -23));
|
|
62
|
+
export const expectFloat32 = (value) => {
|
|
63
|
+
const expected = expectNumber(value);
|
|
64
|
+
if (expected !== undefined && !Number.isNaN(expected) && expected !== Infinity && expected !== -Infinity) {
|
|
65
|
+
if (Math.abs(expected) > MAX_FLOAT) {
|
|
66
|
+
throw new TypeError(`Expected 32-bit float, got ${value}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return expected;
|
|
70
|
+
};
|
|
71
|
+
export const expectLong = (value) => {
|
|
72
|
+
if (value === null || value === undefined) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
if (Number.isInteger(value) && !Number.isNaN(value)) {
|
|
76
|
+
return value;
|
|
77
|
+
}
|
|
78
|
+
throw new TypeError(`Expected integer, got ${typeof value}: ${value}`);
|
|
79
|
+
};
|
|
80
|
+
export const expectInt = expectLong;
|
|
81
|
+
export const expectInt32 = (value) => expectSizedInt(value, 32);
|
|
82
|
+
export const expectShort = (value) => expectSizedInt(value, 16);
|
|
83
|
+
export const expectByte = (value) => expectSizedInt(value, 8);
|
|
84
|
+
const expectSizedInt = (value, size) => {
|
|
85
|
+
const expected = expectLong(value);
|
|
86
|
+
if (expected !== undefined && castInt(expected, size) !== expected) {
|
|
87
|
+
throw new TypeError(`Expected ${size}-bit integer, got ${value}`);
|
|
88
|
+
}
|
|
89
|
+
return expected;
|
|
90
|
+
};
|
|
91
|
+
const castInt = (value, size) => {
|
|
92
|
+
switch (size) {
|
|
93
|
+
case 32:
|
|
94
|
+
return Int32Array.of(value)[0];
|
|
95
|
+
case 16:
|
|
96
|
+
return Int16Array.of(value)[0];
|
|
97
|
+
case 8:
|
|
98
|
+
return Int8Array.of(value)[0];
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export const expectNonNull = (value, location) => {
|
|
102
|
+
if (value === null || value === undefined) {
|
|
103
|
+
if (location) {
|
|
104
|
+
throw new TypeError(`Expected a non-null value for ${location}`);
|
|
105
|
+
}
|
|
106
|
+
throw new TypeError("Expected a non-null value");
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
};
|
|
110
|
+
export const expectObject = (value) => {
|
|
111
|
+
if (value === null || value === undefined) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
115
|
+
return value;
|
|
116
|
+
}
|
|
117
|
+
const receivedType = Array.isArray(value) ? "array" : typeof value;
|
|
118
|
+
throw new TypeError(`Expected object, got ${receivedType}: ${value}`);
|
|
119
|
+
};
|
|
120
|
+
export const expectString = (value) => {
|
|
121
|
+
if (value === null || value === undefined) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
if (typeof value === "string") {
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
if (["boolean", "number", "bigint"].includes(typeof value)) {
|
|
128
|
+
logger.warn(stackTraceWarning(`Expected string, got ${typeof value}: ${value}`));
|
|
129
|
+
return String(value);
|
|
130
|
+
}
|
|
131
|
+
throw new TypeError(`Expected string, got ${typeof value}: ${value}`);
|
|
132
|
+
};
|
|
133
|
+
export const expectUnion = (value) => {
|
|
134
|
+
if (value === null || value === undefined) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
const asObject = expectObject(value);
|
|
138
|
+
const setKeys = Object.entries(asObject)
|
|
139
|
+
.filter(([, v]) => v != null)
|
|
140
|
+
.map(([k]) => k);
|
|
141
|
+
if (setKeys.length === 0) {
|
|
142
|
+
throw new TypeError(`Unions must have exactly one non-null member. None were found.`);
|
|
143
|
+
}
|
|
144
|
+
if (setKeys.length > 1) {
|
|
145
|
+
throw new TypeError(`Unions must have exactly one non-null member. Keys ${setKeys} were not null.`);
|
|
146
|
+
}
|
|
147
|
+
return asObject;
|
|
148
|
+
};
|
|
149
|
+
export const strictParseDouble = (value) => {
|
|
150
|
+
if (typeof value == "string") {
|
|
151
|
+
return expectNumber(parseNumber(value));
|
|
152
|
+
}
|
|
153
|
+
return expectNumber(value);
|
|
154
|
+
};
|
|
155
|
+
export const strictParseFloat = strictParseDouble;
|
|
156
|
+
export const strictParseFloat32 = (value) => {
|
|
157
|
+
if (typeof value == "string") {
|
|
158
|
+
return expectFloat32(parseNumber(value));
|
|
159
|
+
}
|
|
160
|
+
return expectFloat32(value);
|
|
161
|
+
};
|
|
162
|
+
const NUMBER_REGEX = /(-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?)|(-?Infinity)|(NaN)/g;
|
|
163
|
+
const parseNumber = (value) => {
|
|
164
|
+
const matches = value.match(NUMBER_REGEX);
|
|
165
|
+
if (matches === null || matches[0].length !== value.length) {
|
|
166
|
+
throw new TypeError(`Expected real number, got implicit NaN`);
|
|
167
|
+
}
|
|
168
|
+
return parseFloat(value);
|
|
169
|
+
};
|
|
170
|
+
export const limitedParseDouble = (value) => {
|
|
171
|
+
if (typeof value == "string") {
|
|
172
|
+
return parseFloatString(value);
|
|
173
|
+
}
|
|
174
|
+
return expectNumber(value);
|
|
175
|
+
};
|
|
176
|
+
export const handleFloat = limitedParseDouble;
|
|
177
|
+
export const limitedParseFloat = limitedParseDouble;
|
|
178
|
+
export const limitedParseFloat32 = (value) => {
|
|
179
|
+
if (typeof value == "string") {
|
|
180
|
+
return parseFloatString(value);
|
|
181
|
+
}
|
|
182
|
+
return expectFloat32(value);
|
|
183
|
+
};
|
|
184
|
+
const parseFloatString = (value) => {
|
|
185
|
+
switch (value) {
|
|
186
|
+
case "NaN":
|
|
187
|
+
return NaN;
|
|
188
|
+
case "Infinity":
|
|
189
|
+
return Infinity;
|
|
190
|
+
case "-Infinity":
|
|
191
|
+
return -Infinity;
|
|
192
|
+
default:
|
|
193
|
+
throw new Error(`Unable to parse float value: ${value}`);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
export const strictParseLong = (value) => {
|
|
197
|
+
if (typeof value === "string") {
|
|
198
|
+
return expectLong(parseNumber(value));
|
|
199
|
+
}
|
|
200
|
+
return expectLong(value);
|
|
201
|
+
};
|
|
202
|
+
export const strictParseInt = strictParseLong;
|
|
203
|
+
export const strictParseInt32 = (value) => {
|
|
204
|
+
if (typeof value === "string") {
|
|
205
|
+
return expectInt32(parseNumber(value));
|
|
206
|
+
}
|
|
207
|
+
return expectInt32(value);
|
|
208
|
+
};
|
|
209
|
+
export const strictParseShort = (value) => {
|
|
210
|
+
if (typeof value === "string") {
|
|
211
|
+
return expectShort(parseNumber(value));
|
|
212
|
+
}
|
|
213
|
+
return expectShort(value);
|
|
214
|
+
};
|
|
215
|
+
export const strictParseByte = (value) => {
|
|
216
|
+
if (typeof value === "string") {
|
|
217
|
+
return expectByte(parseNumber(value));
|
|
218
|
+
}
|
|
219
|
+
return expectByte(value);
|
|
220
|
+
};
|
|
221
|
+
const stackTraceWarning = (message) => {
|
|
222
|
+
return String(new TypeError(message).stack || message)
|
|
223
|
+
.split("\n")
|
|
224
|
+
.slice(0, 5)
|
|
225
|
+
.filter((s) => !s.includes("stackTraceWarning"))
|
|
226
|
+
.join("\n");
|
|
227
|
+
};
|
|
228
|
+
export const logger = {
|
|
229
|
+
warn: console.warn,
|
|
230
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export function splitEvery(value, delimiter, numDelimiters) {
|
|
2
|
+
if (numDelimiters <= 0 || !Number.isInteger(numDelimiters)) {
|
|
3
|
+
throw new Error("Invalid number of delimiters (" + numDelimiters + ") for splitEvery.");
|
|
4
|
+
}
|
|
5
|
+
const segments = value.split(delimiter);
|
|
6
|
+
if (numDelimiters === 1) {
|
|
7
|
+
return segments;
|
|
8
|
+
}
|
|
9
|
+
const compoundSegments = [];
|
|
10
|
+
let currentSegment = "";
|
|
11
|
+
for (let i = 0; i < segments.length; i++) {
|
|
12
|
+
if (currentSegment === "") {
|
|
13
|
+
currentSegment = segments[i];
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
currentSegment += delimiter + segments[i];
|
|
17
|
+
}
|
|
18
|
+
if ((i + 1) % numDelimiters === 0) {
|
|
19
|
+
compoundSegments.push(currentSegment);
|
|
20
|
+
currentSegment = "";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (currentSegment !== "") {
|
|
24
|
+
compoundSegments.push(currentSegment);
|
|
25
|
+
}
|
|
26
|
+
return compoundSegments;
|
|
27
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export const splitHeader = (value) => {
|
|
2
|
+
const z = value.length;
|
|
3
|
+
const values = [];
|
|
4
|
+
let withinQuotes = false;
|
|
5
|
+
let prevChar = undefined;
|
|
6
|
+
let anchor = 0;
|
|
7
|
+
for (let i = 0; i < z; ++i) {
|
|
8
|
+
const char = value[i];
|
|
9
|
+
switch (char) {
|
|
10
|
+
case `"`:
|
|
11
|
+
if (prevChar !== "\\") {
|
|
12
|
+
withinQuotes = !withinQuotes;
|
|
13
|
+
}
|
|
14
|
+
break;
|
|
15
|
+
case ",":
|
|
16
|
+
if (!withinQuotes) {
|
|
17
|
+
values.push(value.slice(anchor, i));
|
|
18
|
+
anchor = i + 1;
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
default:
|
|
22
|
+
}
|
|
23
|
+
prevChar = char;
|
|
24
|
+
}
|
|
25
|
+
values.push(value.slice(anchor));
|
|
26
|
+
return values.map((v) => {
|
|
27
|
+
v = v.trim();
|
|
28
|
+
const z = v.length;
|
|
29
|
+
if (z < 2) {
|
|
30
|
+
return v;
|
|
31
|
+
}
|
|
32
|
+
if (v[0] === `"` && v[z - 1] === `"`) {
|
|
33
|
+
v = v.slice(1, z - 1);
|
|
34
|
+
}
|
|
35
|
+
return v.replace(/\\"/g, '"');
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*
|
|
4
|
+
* Builds a proper UTC HttpDate timestamp from a Date object
|
|
5
|
+
* since not all environments will have this as the expected
|
|
6
|
+
* format.
|
|
7
|
+
*
|
|
8
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString}
|
|
9
|
+
* - Prior to ECMAScript 2018, the format of the return value
|
|
10
|
+
* - varied according to the platform. The most common return
|
|
11
|
+
* - value was an RFC-1123 formatted date stamp, which is a
|
|
12
|
+
* - slightly updated version of RFC-822 date stamps.
|
|
13
|
+
*/
|
|
14
|
+
export declare function dateToUtcString(date: Date): string;
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*
|
|
18
|
+
* Parses a value into a Date. Returns undefined if the input is null or
|
|
19
|
+
* undefined, throws an error if the input is not a string that can be parsed
|
|
20
|
+
* as an RFC 3339 date.
|
|
21
|
+
*
|
|
22
|
+
* Input strings must conform to RFC3339 section 5.6, and cannot have a UTC
|
|
23
|
+
* offset. Fractional precision is supported.
|
|
24
|
+
*
|
|
25
|
+
* @see {@link https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14}
|
|
26
|
+
*
|
|
27
|
+
* @param value - the value to parse
|
|
28
|
+
* @returns a Date or undefined
|
|
29
|
+
*/
|
|
30
|
+
export declare const parseRfc3339DateTime: (value: unknown) => Date | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*
|
|
34
|
+
* Parses a value into a Date. Returns undefined if the input is null or
|
|
35
|
+
* undefined, throws an error if the input is not a string that can be parsed
|
|
36
|
+
* as an RFC 3339 date.
|
|
37
|
+
*
|
|
38
|
+
* Input strings must conform to RFC3339 section 5.6, and can have a UTC
|
|
39
|
+
* offset. Fractional precision is supported.
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14}
|
|
42
|
+
*
|
|
43
|
+
* @param value - the value to parse
|
|
44
|
+
* @returns a Date or undefined
|
|
45
|
+
*/
|
|
46
|
+
export declare const parseRfc3339DateTimeWithOffset: (value: unknown) => Date | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*
|
|
50
|
+
* Parses a value into a Date. Returns undefined if the input is null or
|
|
51
|
+
* undefined, throws an error if the input is not a string that can be parsed
|
|
52
|
+
* as an RFC 7231 IMF-fixdate or obs-date.
|
|
53
|
+
*
|
|
54
|
+
* Input strings must conform to RFC7231 section 7.1.1.1. Fractional seconds are supported.
|
|
55
|
+
*
|
|
56
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc7231.html#section-7.1.1.1}
|
|
57
|
+
*
|
|
58
|
+
* @param value - the value to parse
|
|
59
|
+
* @returns a Date or undefined
|
|
60
|
+
*/
|
|
61
|
+
export declare const parseRfc7231DateTime: (value: unknown) => Date | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* @internal
|
|
64
|
+
*
|
|
65
|
+
* Parses a value into a Date. Returns undefined if the input is null or
|
|
66
|
+
* undefined, throws an error if the input is not a number or a parseable string.
|
|
67
|
+
*
|
|
68
|
+
* Input strings must be an integer or floating point number. Fractional seconds are supported.
|
|
69
|
+
*
|
|
70
|
+
* @param value - the value to parse
|
|
71
|
+
* @returns a Date or undefined
|
|
72
|
+
*/
|
|
73
|
+
export declare const parseEpochTimestamp: (value: unknown) => Date | undefined;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*
|
|
4
|
+
* A model field with this type means that you may provide a JavaScript
|
|
5
|
+
* object in lieu of a JSON string, and it will be serialized to JSON
|
|
6
|
+
* automatically before being sent in a request.
|
|
7
|
+
*
|
|
8
|
+
* For responses, you will receive a "LazyJsonString", which is a boxed String object
|
|
9
|
+
* with additional mixin methods.
|
|
10
|
+
* To get the string value, call `.toString()`, or to get the JSON object value,
|
|
11
|
+
* call `.deserializeJSON()` or parse it yourself.
|
|
12
|
+
*/
|
|
13
|
+
export type AutomaticJsonStringConversion = Parameters<typeof JSON.stringify>[0] | LazyJsonString;
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export interface LazyJsonString extends String {
|
|
18
|
+
/**
|
|
19
|
+
* @returns the JSON parsing of the string value.
|
|
20
|
+
*/
|
|
21
|
+
deserializeJSON(): any;
|
|
22
|
+
/**
|
|
23
|
+
* @returns the original string value rather than a JSON.stringified value.
|
|
24
|
+
*/
|
|
25
|
+
toJSON(): string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*
|
|
30
|
+
* Extension of the native String class in the previous implementation
|
|
31
|
+
* has negative global performance impact on method dispatch for strings,
|
|
32
|
+
* and is generally discouraged.
|
|
33
|
+
*
|
|
34
|
+
* This current implementation may look strange, but is necessary to preserve the interface and
|
|
35
|
+
* behavior of extending the String class.
|
|
36
|
+
*/
|
|
37
|
+
export declare const LazyJsonString: {
|
|
38
|
+
new (s: string): LazyJsonString;
|
|
39
|
+
(s: string): LazyJsonString;
|
|
40
|
+
from(s: any): LazyJsonString;
|
|
41
|
+
/**
|
|
42
|
+
* @deprecated use #from.
|
|
43
|
+
*/
|
|
44
|
+
fromObject(s: any): LazyJsonString;
|
|
45
|
+
};
|