@genesislcap/mock-server 15.74.0 → 15.75.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/README.md +296 -58
- package/dist/db/schema.d.ts +9 -1
- package/dist/db/schema.js +40 -11
- package/dist/db/store.d.ts +1 -0
- package/dist/db/store.js +4 -0
- package/dist/handlers/auth.d.ts +15 -6
- package/dist/handlers/auth.js +152 -33
- package/dist/handlers/commitEvent.js +43 -20
- package/dist/handlers/crudEvents.d.ts +3 -1
- package/dist/handlers/crudEvents.js +3 -2
- package/dist/handlers/eventValidation.js +6 -3
- package/dist/handlers/http.d.ts +2 -2
- package/dist/handlers/http.js +66 -43
- package/dist/handlers/jsonSchema.d.ts +1 -0
- package/dist/handlers/jsonSchema.js +72 -21
- package/dist/handlers/meta.d.ts +1 -1
- package/dist/handlers/meta.js +59 -10
- package/dist/handlers/requestReply.js +36 -9
- package/dist/index.d.ts +1 -1
- package/dist/presets/auth.d.ts +25 -0
- package/dist/presets/auth.js +855 -0
- package/dist/presets/cronExpression.d.ts +1 -0
- package/dist/presets/cronExpression.js +429 -0
- package/dist/presets/fileServer.d.ts +13 -0
- package/dist/presets/fileServer.js +399 -0
- package/dist/presets/gsf.d.ts +24 -0
- package/dist/presets/gsf.js +450 -0
- package/dist/presets/index.d.ts +1 -0
- package/dist/presets/index.js +1 -0
- package/dist/presets/javaTime.d.ts +7 -0
- package/dist/presets/javaTime.js +322 -0
- package/dist/presets/multipart.d.ts +14 -0
- package/dist/presets/multipart.js +91 -0
- package/dist/presets/notify.d.ts +27 -0
- package/dist/presets/notify.js +250 -0
- package/dist/presets/notifyDispatch.d.ts +28 -0
- package/dist/presets/notifyDispatch.js +570 -0
- package/dist/presets/notifyRules.d.ts +9 -0
- package/dist/presets/notifyRules.js +754 -0
- package/dist/presets/platform/backing.json +8058 -217
- package/dist/presets/platform/criteria-model.json +362 -0
- package/dist/presets/platform/time-zones.json +608 -0
- package/dist/presets/platform.d.ts +7 -0
- package/dist/presets/platform.js +145 -103
- package/dist/presets/products.d.ts +9 -7
- package/dist/presets/products.js +89 -21
- package/dist/presets/reconciliation.d.ts +18 -0
- package/dist/presets/reconciliation.js +721 -0
- package/dist/presets/reporting.d.ts +14 -0
- package/dist/presets/reporting.js +862 -0
- package/dist/protocol/connection.d.ts +2 -0
- package/dist/protocol/connection.js +4 -0
- package/dist/protocol/dtoTypes.d.ts +30 -0
- package/dist/protocol/dtoTypes.js +250 -0
- package/dist/protocol/errors.d.ts +3 -1
- package/dist/protocol/errors.js +2 -0
- package/dist/protocol/fieldTypes.js +5 -0
- package/dist/protocol/router.d.ts +1 -0
- package/dist/protocol/router.js +3 -3
- package/dist/protocol/schemaValidation.d.ts +1 -1
- package/dist/protocol/schemaValidation.js +53 -8
- package/dist/server.js +34 -2
- package/dist/types.d.ts +50 -2
- package/package.json +1 -1
- package/src/db/schema.ts +56 -13
- package/src/db/store.ts +5 -0
- package/src/handlers/auth.ts +211 -37
- package/src/handlers/commitEvent.ts +44 -19
- package/src/handlers/crudEvents.ts +9 -2
- package/src/handlers/eventValidation.ts +6 -3
- package/src/handlers/http.ts +83 -42
- package/src/handlers/jsonSchema.ts +86 -23
- package/src/handlers/meta.ts +77 -9
- package/src/handlers/requestReply.ts +42 -6
- package/src/index.ts +7 -0
- package/src/presets/auth.ts +961 -0
- package/src/presets/cronExpression.ts +445 -0
- package/src/presets/fileServer.ts +475 -0
- package/src/presets/gsf.ts +475 -0
- package/src/presets/index.ts +5 -0
- package/src/presets/javaTime.ts +354 -0
- package/src/presets/multipart.ts +110 -0
- package/src/presets/notify.ts +312 -0
- package/src/presets/notifyDispatch.ts +685 -0
- package/src/presets/notifyRules.ts +866 -0
- package/src/presets/platform/backing.json +8058 -217
- package/src/presets/platform/criteria-model.json +362 -0
- package/src/presets/platform/time-zones.json +608 -0
- package/src/presets/platform.ts +204 -115
- package/src/presets/products.ts +125 -19
- package/src/presets/reconciliation.ts +824 -0
- package/src/presets/reporting.ts +953 -0
- package/src/protocol/connection.ts +4 -0
- package/src/protocol/dtoTypes.ts +310 -0
- package/src/protocol/errors.ts +13 -1
- package/src/protocol/fieldTypes.ts +5 -0
- package/src/protocol/router.ts +3 -3
- package/src/protocol/schemaValidation.ts +61 -8
- package/src/server.ts +38 -2
- package/src/types.ts +147 -3
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// The JVM time behaviour the platform products show: java.time's ZoneId.of
|
|
2
|
+
// (JDK 17.0.12, tzdata 2024a) with its exception messages, and Joda-Time's
|
|
3
|
+
// pattern formatting and DateTime.toString() (joda-time 2.14.0), each checked
|
|
4
|
+
// against the real classes.
|
|
5
|
+
import timeZones from './platform/time-zones.json' with { type: 'json' };
|
|
6
|
+
// ZoneId.getAvailableZoneIds(), sorted as String does (VALID_TIME_ZONE).
|
|
7
|
+
export const JVM_ZONE_IDS = timeZones.zones;
|
|
8
|
+
const REGIONS = new Set(JVM_ZONE_IDS);
|
|
9
|
+
// A JSON value as Kotlin's toString() prints what Jackson read it as.
|
|
10
|
+
export function kotlinString(value) {
|
|
11
|
+
if (Array.isArray(value))
|
|
12
|
+
return `[${value.map(kotlinString).join(', ')}]`;
|
|
13
|
+
if (value !== null && typeof value === 'object') {
|
|
14
|
+
return `{${Object.entries(value)
|
|
15
|
+
.map(([key, item]) => `${key}=${kotlinString(item)}`)
|
|
16
|
+
.join(', ')}}`;
|
|
17
|
+
}
|
|
18
|
+
return String(value);
|
|
19
|
+
}
|
|
20
|
+
class DateTimeError extends Error {
|
|
21
|
+
}
|
|
22
|
+
const OFFSET_HOURS = 18;
|
|
23
|
+
const MINUTES = 59;
|
|
24
|
+
// ZoneOffset.of's id lengths (±hh, ±hhmm, ±hh:mm, ±hhmmss, ±hh:mm:ss): where
|
|
25
|
+
// the minutes and the seconds start, the hours always at 1, and whether a
|
|
26
|
+
// colon comes before each.
|
|
27
|
+
const OFFSET_LAYOUTS = {
|
|
28
|
+
3: {},
|
|
29
|
+
5: { minutes: 3 },
|
|
30
|
+
6: { minutes: 4, colons: true },
|
|
31
|
+
7: { minutes: 3, seconds: 5 },
|
|
32
|
+
9: { minutes: 4, seconds: 7, colons: true },
|
|
33
|
+
};
|
|
34
|
+
// ZoneOffset.of: Z (its id cache), ±h, or one of the layouts.
|
|
35
|
+
function zoneOffset(offsetId) {
|
|
36
|
+
if (offsetId === 'Z')
|
|
37
|
+
return;
|
|
38
|
+
const id = offsetId.length === 2 ? `${offsetId[0]}0${offsetId[1]}` : offsetId;
|
|
39
|
+
const invalid = (why) => new DateTimeError(`Invalid ID for ZoneOffset, ${why}: ${id}`);
|
|
40
|
+
const number = (pos, precededByColon) => {
|
|
41
|
+
if (pos === undefined)
|
|
42
|
+
return 0;
|
|
43
|
+
if (precededByColon && id[pos - 1] !== ':')
|
|
44
|
+
throw invalid('colon not found when expected');
|
|
45
|
+
const pair = id.slice(pos, pos + 2);
|
|
46
|
+
if (!/^\d\d$/.test(pair))
|
|
47
|
+
throw invalid('non numeric characters found');
|
|
48
|
+
return Number(pair);
|
|
49
|
+
};
|
|
50
|
+
const layout = OFFSET_LAYOUTS[id.length];
|
|
51
|
+
if (!layout)
|
|
52
|
+
throw invalid('invalid format');
|
|
53
|
+
const hours = number(1, false);
|
|
54
|
+
const minutes = number(layout.minutes, layout.colons === true);
|
|
55
|
+
const seconds = number(layout.seconds, layout.colons === true);
|
|
56
|
+
if (id[0] !== '+' && id[0] !== '-')
|
|
57
|
+
throw invalid('plus/minus not found when expected');
|
|
58
|
+
// ZoneOffset.validate, on the signed values.
|
|
59
|
+
const sign = id[0] === '-' ? -1 : 1;
|
|
60
|
+
const [h, m, s] = [hours * sign, minutes * sign, seconds * sign];
|
|
61
|
+
if (h < -OFFSET_HOURS || h > OFFSET_HOURS) {
|
|
62
|
+
throw new DateTimeError(`Zone offset hours not in valid range: value ${h} is not in the range -18 to 18`);
|
|
63
|
+
}
|
|
64
|
+
if (Math.abs(m) > MINUTES) {
|
|
65
|
+
throw new DateTimeError(`Zone offset minutes not in valid range: value ${m} is not in the range -59 to 59`);
|
|
66
|
+
}
|
|
67
|
+
if (Math.abs(s) > MINUTES) {
|
|
68
|
+
throw new DateTimeError(`Zone offset seconds not in valid range: value ${s} is not in the range -59 to 59`);
|
|
69
|
+
}
|
|
70
|
+
if (Math.abs(h) === OFFSET_HOURS && (m !== 0 || s !== 0)) {
|
|
71
|
+
throw new DateTimeError('Zone offset not in valid range: -18:00 to +18:00');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// ZoneRegion.ofId: a well-formed name the tz database has.
|
|
75
|
+
function zoneRegion(id) {
|
|
76
|
+
if (!/^[A-Za-z][A-Za-z0-9~/._+-]+$/.test(id)) {
|
|
77
|
+
throw new DateTimeError(`Invalid ID for region-based ZoneId, invalid format: ${id}`);
|
|
78
|
+
}
|
|
79
|
+
if (!REGIONS.has(id))
|
|
80
|
+
throw new DateTimeError(`Unknown time-zone ID: ${id}`);
|
|
81
|
+
}
|
|
82
|
+
// In the order ZoneId.of tries them.
|
|
83
|
+
const OFFSET_PREFIXES = ['UTC', 'GMT', 'UT'];
|
|
84
|
+
// ZoneId.of(id)'s message, or undefined when it takes it.
|
|
85
|
+
export function zoneIdError(id) {
|
|
86
|
+
try {
|
|
87
|
+
const prefix = OFFSET_PREFIXES.find((candidate) => id.startsWith(candidate));
|
|
88
|
+
if (id.length <= 1 || id.startsWith('+') || id.startsWith('-'))
|
|
89
|
+
zoneOffset(id);
|
|
90
|
+
else if (prefix)
|
|
91
|
+
offsetWithPrefix(id, prefix.length);
|
|
92
|
+
else
|
|
93
|
+
zoneRegion(id);
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
if (error instanceof DateTimeError)
|
|
98
|
+
return error.message;
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// ZoneId.ofWithPrefix: UTC, GMT or UT, alone or with an offset.
|
|
103
|
+
function offsetWithPrefix(id, prefixLength) {
|
|
104
|
+
if (id.length === prefixLength)
|
|
105
|
+
return;
|
|
106
|
+
if (id[prefixLength] !== '+' && id[prefixLength] !== '-') {
|
|
107
|
+
zoneRegion(id);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
try {
|
|
111
|
+
zoneOffset(id.slice(prefixLength));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
throw new DateTimeError(`Invalid ID for offset-based ZoneId: ${id}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const MS_PER_SECOND = 1000;
|
|
118
|
+
const MS_PER_MINUTE = 60_000;
|
|
119
|
+
const MS_PER_DAY = 86_400_000;
|
|
120
|
+
const MINUTES_PER_HOUR = 60;
|
|
121
|
+
const HOURS_PER_DAY = 24;
|
|
122
|
+
const HALF_DAY = 12;
|
|
123
|
+
const DAYS_PER_WEEK = 7;
|
|
124
|
+
const THURSDAY = 4;
|
|
125
|
+
const YEAR_DIGITS = 4;
|
|
126
|
+
const MILLIS_DIGITS = 3;
|
|
127
|
+
const WEEKDAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
|
|
128
|
+
const MONTH_NAMES = [
|
|
129
|
+
'January',
|
|
130
|
+
'February',
|
|
131
|
+
'March',
|
|
132
|
+
'April',
|
|
133
|
+
'May',
|
|
134
|
+
'June',
|
|
135
|
+
'July',
|
|
136
|
+
'August',
|
|
137
|
+
'September',
|
|
138
|
+
'October',
|
|
139
|
+
'November',
|
|
140
|
+
'December',
|
|
141
|
+
];
|
|
142
|
+
const SHORT = 3;
|
|
143
|
+
const FULL = 4;
|
|
144
|
+
// The wall-clock fields of an instant in a zone.
|
|
145
|
+
function zoned(epochMillis, zone) {
|
|
146
|
+
const parts = Object.fromEntries(new Intl.DateTimeFormat('en-US', {
|
|
147
|
+
timeZone: zone,
|
|
148
|
+
hourCycle: 'h23',
|
|
149
|
+
year: 'numeric',
|
|
150
|
+
month: 'numeric',
|
|
151
|
+
day: 'numeric',
|
|
152
|
+
hour: 'numeric',
|
|
153
|
+
minute: 'numeric',
|
|
154
|
+
second: 'numeric',
|
|
155
|
+
})
|
|
156
|
+
.formatToParts(new Date(epochMillis))
|
|
157
|
+
.map((part) => [part.type, Number(part.value)]));
|
|
158
|
+
const millis = ((epochMillis % MS_PER_SECOND) + MS_PER_SECOND) % MS_PER_SECOND;
|
|
159
|
+
const wall = Date.UTC(parts.year, parts.month - 1, parts.day, parts.hour, parts.minute, parts.second);
|
|
160
|
+
const weekday = new Date(wall).getUTCDay();
|
|
161
|
+
return {
|
|
162
|
+
year: parts.year,
|
|
163
|
+
month: parts.month,
|
|
164
|
+
day: parts.day,
|
|
165
|
+
hour: parts.hour,
|
|
166
|
+
minute: parts.minute,
|
|
167
|
+
second: parts.second,
|
|
168
|
+
millis,
|
|
169
|
+
weekday: weekday === 0 ? DAYS_PER_WEEK : weekday,
|
|
170
|
+
offsetMinutes: Math.round((wall + millis - epochMillis) / MS_PER_MINUTE),
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const pad = (value, width) => `${value < 0 ? '-' : ''}${String(Math.abs(value)).padStart(width, '0')}`;
|
|
174
|
+
function offsetText(minutes, separator, zeroText) {
|
|
175
|
+
if (minutes === 0 && zeroText !== undefined)
|
|
176
|
+
return zeroText;
|
|
177
|
+
const sign = minutes < 0 ? '-' : '+';
|
|
178
|
+
const absolute = Math.abs(minutes);
|
|
179
|
+
const hours = pad(Math.floor(absolute / MINUTES_PER_HOUR), 2);
|
|
180
|
+
const rest = pad(absolute % MINUTES_PER_HOUR, 2);
|
|
181
|
+
return `${sign}${hours}${separator ? ':' : ''}${rest}`;
|
|
182
|
+
}
|
|
183
|
+
const dayOfYear = (time) => (Date.UTC(time.year, time.month - 1, time.day) - Date.UTC(time.year, 0, 1)) / MS_PER_DAY + 1;
|
|
184
|
+
// ISO week-based year and week (Joda's x and w).
|
|
185
|
+
function isoWeek(time) {
|
|
186
|
+
const date = new Date(Date.UTC(time.year, time.month - 1, time.day));
|
|
187
|
+
date.setUTCDate(date.getUTCDate() + THURSDAY - time.weekday);
|
|
188
|
+
const weekyear = date.getUTCFullYear();
|
|
189
|
+
const week = Math.floor((date.getTime() - Date.UTC(weekyear, 0, 1)) / MS_PER_DAY / DAYS_PER_WEEK) + 1;
|
|
190
|
+
return { weekyear, week };
|
|
191
|
+
}
|
|
192
|
+
const zoneName = (epochMillis, zone, long) => new Intl.DateTimeFormat('en-GB', { timeZone: zone, timeZoneName: long ? 'long' : 'short' })
|
|
193
|
+
.formatToParts(new Date(epochMillis))
|
|
194
|
+
.find((part) => part.type === 'timeZoneName')?.value ?? zone;
|
|
195
|
+
// One pattern letter repeated `count` times (DateTimeFormat.parsePatternTo).
|
|
196
|
+
function field(letter, count, time, epochMillis, zone) {
|
|
197
|
+
const twoDigits = (value) => pad(((value % 100) + 100) % 100, 2);
|
|
198
|
+
switch (letter) {
|
|
199
|
+
case 'G':
|
|
200
|
+
return time.year > 0 ? 'AD' : 'BC';
|
|
201
|
+
case 'C':
|
|
202
|
+
return pad(Math.floor(time.year / 100), count);
|
|
203
|
+
case 'Y':
|
|
204
|
+
case 'y':
|
|
205
|
+
return count === 2 ? twoDigits(time.year) : pad(time.year, count);
|
|
206
|
+
case 'x':
|
|
207
|
+
return count === 2 ? twoDigits(isoWeek(time).weekyear) : pad(isoWeek(time).weekyear, count);
|
|
208
|
+
case 'w':
|
|
209
|
+
return pad(isoWeek(time).week, count);
|
|
210
|
+
case 'e':
|
|
211
|
+
return pad(time.weekday, count);
|
|
212
|
+
case 'E':
|
|
213
|
+
return count >= FULL
|
|
214
|
+
? WEEKDAYS[time.weekday - 1]
|
|
215
|
+
: WEEKDAYS[time.weekday - 1].slice(0, SHORT);
|
|
216
|
+
case 'D':
|
|
217
|
+
return pad(dayOfYear(time), count);
|
|
218
|
+
case 'M':
|
|
219
|
+
if (count >= FULL)
|
|
220
|
+
return MONTH_NAMES[time.month - 1];
|
|
221
|
+
if (count === SHORT)
|
|
222
|
+
return MONTH_NAMES[time.month - 1].slice(0, SHORT);
|
|
223
|
+
return pad(time.month, count);
|
|
224
|
+
case 'd':
|
|
225
|
+
return pad(time.day, count);
|
|
226
|
+
case 'a':
|
|
227
|
+
return time.hour < HALF_DAY ? 'AM' : 'PM';
|
|
228
|
+
case 'h':
|
|
229
|
+
return pad(time.hour % HALF_DAY === 0 ? HALF_DAY : time.hour % HALF_DAY, count);
|
|
230
|
+
case 'H':
|
|
231
|
+
return pad(time.hour, count);
|
|
232
|
+
case 'k':
|
|
233
|
+
return pad(time.hour === 0 ? HOURS_PER_DAY : time.hour, count);
|
|
234
|
+
case 'K':
|
|
235
|
+
return pad(time.hour % HALF_DAY, count);
|
|
236
|
+
case 'm':
|
|
237
|
+
return pad(time.minute, count);
|
|
238
|
+
case 's':
|
|
239
|
+
return pad(time.second, count);
|
|
240
|
+
case 'S':
|
|
241
|
+
return String(time.millis).padStart(MILLIS_DIGITS, '0').padEnd(count, '0').slice(0, count);
|
|
242
|
+
case 'z':
|
|
243
|
+
return zoneName(epochMillis, zone, count >= FULL);
|
|
244
|
+
case 'Z':
|
|
245
|
+
if (count === 1)
|
|
246
|
+
return offsetText(time.offsetMinutes, false);
|
|
247
|
+
if (count === 2)
|
|
248
|
+
return offsetText(time.offsetMinutes, true);
|
|
249
|
+
return zone;
|
|
250
|
+
default:
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// DateTimeFormat.forPattern(pattern).print(instant in zone), or undefined
|
|
255
|
+
// for a pattern Joda refuses (an empty one, an "Illegal pattern component").
|
|
256
|
+
// A zone name (z) comes from the host's Intl data, not Java's, so it can
|
|
257
|
+
// differ ("GMT-5" for "EST").
|
|
258
|
+
export function jodaFormat(pattern, epochMillis, zone) {
|
|
259
|
+
if (pattern === '')
|
|
260
|
+
return undefined;
|
|
261
|
+
const time = zoned(epochMillis, zone);
|
|
262
|
+
let out = '';
|
|
263
|
+
let i = 0;
|
|
264
|
+
while (i < pattern.length) {
|
|
265
|
+
const c = pattern[i];
|
|
266
|
+
if (c === "'") {
|
|
267
|
+
const quoted = quotedText(pattern, i);
|
|
268
|
+
out += quoted.text;
|
|
269
|
+
i = quoted.next;
|
|
270
|
+
}
|
|
271
|
+
else if (/[A-Za-z]/.test(c)) {
|
|
272
|
+
let j = i;
|
|
273
|
+
while (pattern[j] === c)
|
|
274
|
+
j += 1;
|
|
275
|
+
const printed = field(c, j - i, time, epochMillis, zone);
|
|
276
|
+
if (printed === undefined)
|
|
277
|
+
return undefined;
|
|
278
|
+
out += printed;
|
|
279
|
+
i = j;
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
out += c;
|
|
283
|
+
i += 1;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return out;
|
|
287
|
+
}
|
|
288
|
+
// The text quoted from `start` on, '' a quote inside it, and where the
|
|
289
|
+
// pattern goes on; '' alone is a quote.
|
|
290
|
+
function quotedText(pattern, start) {
|
|
291
|
+
let j = start + 1;
|
|
292
|
+
let text = '';
|
|
293
|
+
while (j < pattern.length && !(pattern[j] === "'" && pattern[j + 1] !== "'")) {
|
|
294
|
+
text += pattern[j];
|
|
295
|
+
j += pattern[j] === "'" ? 2 : 1;
|
|
296
|
+
}
|
|
297
|
+
return { text: j === start + 1 && pattern[j] === "'" ? "'" : text, next: j + 1 };
|
|
298
|
+
}
|
|
299
|
+
// Joda's DateTime.toString(): ISO-8601 with millis and the offset, Z for UTC.
|
|
300
|
+
export function jodaIso(epochMillis, zone) {
|
|
301
|
+
const time = zoned(epochMillis, zone);
|
|
302
|
+
return (`${pad(time.year, YEAR_DIGITS)}-${pad(time.month, 2)}-${pad(time.day, 2)}T` +
|
|
303
|
+
`${pad(time.hour, 2)}:${pad(time.minute, 2)}:${pad(time.second, 2)}` +
|
|
304
|
+
`.${pad(time.millis, MILLIS_DIGITS)}` +
|
|
305
|
+
offsetText(time.offsetMinutes, true, 'Z'));
|
|
306
|
+
}
|
|
307
|
+
// Whether Intl knows the zone.
|
|
308
|
+
export function knownZone(zone) {
|
|
309
|
+
try {
|
|
310
|
+
new Intl.DateTimeFormat('en-US', { timeZone: zone });
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
// The JVM's default zone, which the mock takes to be the host's; GMT, as the
|
|
318
|
+
// JVM falls back to, when the host's can't be told (TZ="", Etc/Unknown).
|
|
319
|
+
export function hostZone() {
|
|
320
|
+
const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
321
|
+
return zone && knownZone(zone) ? zone : 'UTC';
|
|
322
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
export interface FormPart {
|
|
3
|
+
name: string;
|
|
4
|
+
fileName?: string;
|
|
5
|
+
contentType?: string;
|
|
6
|
+
data: Buffer;
|
|
7
|
+
}
|
|
8
|
+
export declare class BodyTooLarge extends Error {
|
|
9
|
+
constructor(maxBytes: number);
|
|
10
|
+
}
|
|
11
|
+
export declare const declaredTooLarge: (req: IncomingMessage, maxBytes: number) => boolean;
|
|
12
|
+
export declare function readBody(req: IncomingMessage, maxBytes?: number): Promise<Buffer>;
|
|
13
|
+
export declare function boundaryOf(contentType: string | undefined): string | undefined;
|
|
14
|
+
export declare function parseMultipart(body: Buffer, boundary: string): FormPart[];
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const CRLF = Buffer.from('\r\n');
|
|
2
|
+
const HEADERS_END = Buffer.from('\r\n\r\n');
|
|
3
|
+
// A body larger than an endpoint takes. GSF's router refuses a multipart
|
|
4
|
+
// request whose Content-Length is over its MultiPartConfig.maxFileSize
|
|
5
|
+
// before it reads it (HttpRequestClassifierHandler.kt:83-95 @v8.15.29), with
|
|
6
|
+
// this text; a body that names no length is counted as it is read.
|
|
7
|
+
export class BodyTooLarge extends Error {
|
|
8
|
+
constructor(maxBytes) {
|
|
9
|
+
super(`File exceeds max size of ${maxBytes}`);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
// Whether the request says it is longer than `maxBytes`.
|
|
13
|
+
export const declaredTooLarge = (req, maxBytes) => Number(req.headers['content-length']) > maxBytes;
|
|
14
|
+
export async function readBody(req, maxBytes = Number.POSITIVE_INFINITY) {
|
|
15
|
+
if (declaredTooLarge(req, maxBytes))
|
|
16
|
+
throw new BodyTooLarge(maxBytes);
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let size = 0;
|
|
19
|
+
for await (const chunk of req) {
|
|
20
|
+
size += chunk.length;
|
|
21
|
+
if (size > maxBytes)
|
|
22
|
+
throw new BodyTooLarge(maxBytes);
|
|
23
|
+
chunks.push(chunk);
|
|
24
|
+
}
|
|
25
|
+
return Buffer.concat(chunks);
|
|
26
|
+
}
|
|
27
|
+
// The boundary a Content-Type names, or undefined when it isn't multipart.
|
|
28
|
+
export function boundaryOf(contentType) {
|
|
29
|
+
if (!contentType || !/^multipart\/form-data/i.test(contentType))
|
|
30
|
+
return undefined;
|
|
31
|
+
const match = /boundary=(?:"([^"]+)"|([^;\s]+))/i.exec(contentType);
|
|
32
|
+
return match?.[1] ?? match?.[2];
|
|
33
|
+
}
|
|
34
|
+
// A Content-Disposition's parameters (RFC 6266), each quoted or not, read
|
|
35
|
+
// one after another so a `;` or `name=` inside a quoted value stays part of
|
|
36
|
+
// it.
|
|
37
|
+
function parameters(header) {
|
|
38
|
+
const found = new Map();
|
|
39
|
+
for (const match of header.matchAll(/;\s*([^=;\s]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^;]*))/g)) {
|
|
40
|
+
const name = match[1].toLowerCase();
|
|
41
|
+
const value = match[2] === undefined ? match[3].trim() : match[2].replace(/\\(.)/g, '$1');
|
|
42
|
+
found.set(name, value);
|
|
43
|
+
}
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
// The parts between the delimiters (RFC 2046 5.1.1): the first `--boundary`
|
|
47
|
+
// opens the body, and each later one follows a CRLF, which belongs to it,
|
|
48
|
+
// so a part's content may hold `--boundary` itself.
|
|
49
|
+
export function parseMultipart(body, boundary) {
|
|
50
|
+
const first = Buffer.from(`--${boundary}`);
|
|
51
|
+
const delimiter = Buffer.from(`\r\n--${boundary}`);
|
|
52
|
+
const parts = [];
|
|
53
|
+
let at = body.subarray(0, first.length).equals(first) ? 0 : body.indexOf(delimiter);
|
|
54
|
+
let length = at === 0 ? first.length : delimiter.length;
|
|
55
|
+
while (at !== -1) {
|
|
56
|
+
const start = at + length;
|
|
57
|
+
// The closing delimiter ends with --.
|
|
58
|
+
if (body.subarray(start, start + 2).toString() === '--')
|
|
59
|
+
break;
|
|
60
|
+
const next = body.indexOf(delimiter, start);
|
|
61
|
+
if (next === -1)
|
|
62
|
+
break;
|
|
63
|
+
// Each part: CRLF, headers, a blank line, the content.
|
|
64
|
+
const part = body.subarray(start + CRLF.length, next);
|
|
65
|
+
const split = part.indexOf(HEADERS_END);
|
|
66
|
+
if (split !== -1) {
|
|
67
|
+
const headers = Object.fromEntries(part
|
|
68
|
+
.subarray(0, split)
|
|
69
|
+
.toString('utf8')
|
|
70
|
+
.split('\r\n')
|
|
71
|
+
.map((line) => {
|
|
72
|
+
const colon = line.indexOf(':');
|
|
73
|
+
return [line.slice(0, colon).trim().toLowerCase(), line.slice(colon + 1).trim()];
|
|
74
|
+
}));
|
|
75
|
+
const disposition = parameters(headers['content-disposition'] ?? '');
|
|
76
|
+
const name = disposition.get('name');
|
|
77
|
+
if (name !== undefined) {
|
|
78
|
+
const fileName = disposition.get('filename');
|
|
79
|
+
parts.push({
|
|
80
|
+
name,
|
|
81
|
+
...(fileName === undefined ? {} : { fileName }),
|
|
82
|
+
...(headers['content-type'] ? { contentType: headers['content-type'] } : {}),
|
|
83
|
+
data: Buffer.from(part.subarray(split + HEADERS_END.length)),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
at = next;
|
|
88
|
+
length = delimiter.length;
|
|
89
|
+
}
|
|
90
|
+
return parts;
|
|
91
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Store } from '../db/store.ts';
|
|
2
|
+
import type { EventHandler, EventHandlerCtx, EventSchemaOverride, FieldDef } from '../types.ts';
|
|
3
|
+
export declare const ENUMS = "global.genesis.gen.dao.enums";
|
|
4
|
+
export declare const TTL_TIME_UNITS: string[];
|
|
5
|
+
export declare const dto: (name: string, type: FieldDef["type"], more?: Partial<FieldDef>) => FieldDef;
|
|
6
|
+
export declare const nullable: {
|
|
7
|
+
nullable: boolean;
|
|
8
|
+
optional: boolean;
|
|
9
|
+
};
|
|
10
|
+
export interface NotifyUsers {
|
|
11
|
+
userOf(ctx: EventHandlerCtx): string;
|
|
12
|
+
fallback: string | undefined;
|
|
13
|
+
hasRight(store: Store, userName: string | undefined, code: string): boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const stringSet: {
|
|
16
|
+
type: "ARRAY";
|
|
17
|
+
items: {
|
|
18
|
+
type: "STRING";
|
|
19
|
+
};
|
|
20
|
+
unique: boolean;
|
|
21
|
+
};
|
|
22
|
+
export declare const EVENT_CLASS = "global.genesis.notify.message.event";
|
|
23
|
+
export interface NotifyBacking {
|
|
24
|
+
eventHandlers: Record<string, EventHandler>;
|
|
25
|
+
eventSchemas: Record<string, EventSchemaOverride>;
|
|
26
|
+
}
|
|
27
|
+
export declare function notifyBacking(): NotifyBacking;
|