@outfoxx/sunday 1.0.9 → 1.1.0-alpha.11

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.
Files changed (179) hide show
  1. package/LICENSE.txt +203 -0
  2. package/README.md +35 -2
  3. package/dist/any-type.d.ts +2 -0
  4. package/dist/any-type.js +13 -0
  5. package/dist/any-type.js.map +1 -1
  6. package/dist/class-type.d.ts +2 -2
  7. package/dist/class-type.js +13 -0
  8. package/dist/class-type.js.map +1 -1
  9. package/dist/date-time-types.d.ts +2 -7
  10. package/dist/date-time-types.js +15 -4
  11. package/dist/date-time-types.js.map +1 -1
  12. package/dist/event-parser.d.ts +17 -0
  13. package/dist/event-parser.js +153 -0
  14. package/dist/event-parser.js.map +1 -0
  15. package/dist/fetch-event-source.d.ts +12 -10
  16. package/dist/fetch-event-source.js +154 -138
  17. package/dist/fetch-event-source.js.map +1 -1
  18. package/dist/fetch-request-factory.d.ts +10 -5
  19. package/dist/fetch-request-factory.js +100 -71
  20. package/dist/fetch-request-factory.js.map +1 -1
  21. package/dist/fetch.d.ts +7 -1
  22. package/dist/fetch.js +74 -8
  23. package/dist/fetch.js.map +1 -1
  24. package/dist/header-parameters.d.ts +3 -0
  25. package/dist/header-parameters.js +53 -0
  26. package/dist/header-parameters.js.map +1 -0
  27. package/dist/index.d.ts +12 -12
  28. package/dist/index.js +25 -12
  29. package/dist/index.js.map +1 -1
  30. package/dist/logger.js +13 -0
  31. package/dist/logger.js.map +1 -1
  32. package/dist/{binary-decoder.d.ts → media-type-codecs/binary-decoder.d.ts} +2 -2
  33. package/dist/media-type-codecs/binary-decoder.js +29 -0
  34. package/dist/media-type-codecs/binary-decoder.js.map +1 -0
  35. package/dist/{binary-encoder.d.ts → media-type-codecs/binary-encoder.d.ts} +0 -0
  36. package/dist/media-type-codecs/binary-encoder.js +26 -0
  37. package/dist/media-type-codecs/binary-encoder.js.map +1 -0
  38. package/dist/media-type-codecs/cbor-decoder.d.ts +39 -0
  39. package/dist/media-type-codecs/cbor-decoder.js +426 -0
  40. package/dist/media-type-codecs/cbor-decoder.js.map +1 -0
  41. package/dist/media-type-codecs/cbor-encoder.d.ts +43 -0
  42. package/dist/media-type-codecs/cbor-encoder.js +239 -0
  43. package/dist/media-type-codecs/cbor-encoder.js.map +1 -0
  44. package/dist/media-type-codecs/cbor-tags.d.ts +4 -0
  45. package/dist/media-type-codecs/cbor-tags.js +18 -0
  46. package/dist/media-type-codecs/cbor-tags.js.map +1 -0
  47. package/dist/media-type-codecs/json-decoder.d.ts +39 -0
  48. package/dist/media-type-codecs/json-decoder.js +380 -0
  49. package/dist/media-type-codecs/json-decoder.js.map +1 -0
  50. package/dist/media-type-codecs/json-encoder.d.ts +44 -0
  51. package/dist/media-type-codecs/json-encoder.js +276 -0
  52. package/dist/media-type-codecs/json-encoder.js.map +1 -0
  53. package/dist/media-type-codecs/media-type-decoder.d.ts +11 -0
  54. package/dist/media-type-codecs/media-type-decoder.js +19 -0
  55. package/dist/media-type-codecs/media-type-decoder.js.map +1 -0
  56. package/dist/{media-type-decoders.d.ts → media-type-codecs/media-type-decoders.d.ts} +5 -4
  57. package/dist/media-type-codecs/media-type-decoders.js +53 -0
  58. package/dist/media-type-codecs/media-type-decoders.js.map +1 -0
  59. package/dist/{media-type-encoder.d.ts → media-type-codecs/media-type-encoder.d.ts} +5 -1
  60. package/dist/media-type-codecs/media-type-encoder.js +24 -0
  61. package/dist/media-type-codecs/media-type-encoder.js.map +1 -0
  62. package/dist/{media-type-encoders.d.ts → media-type-codecs/media-type-encoders.d.ts} +5 -4
  63. package/dist/media-type-codecs/media-type-encoders.js +55 -0
  64. package/dist/media-type-codecs/media-type-encoders.js.map +1 -0
  65. package/dist/{url-encoder.d.ts → media-type-codecs/www-form-url-encoder.d.ts} +8 -8
  66. package/dist/{url-encoder.js → media-type-codecs/www-form-url-encoder.js} +51 -37
  67. package/dist/media-type-codecs/www-form-url-encoder.js.map +1 -0
  68. package/dist/media-type.d.ts +91 -13
  69. package/dist/media-type.js +286 -15
  70. package/dist/media-type.js.map +1 -1
  71. package/dist/problem.d.ts +24 -9
  72. package/dist/problem.js +153 -6
  73. package/dist/problem.js.map +1 -1
  74. package/dist/request-factory.d.ts +10 -10
  75. package/dist/request-factory.js +13 -0
  76. package/dist/request-factory.js.map +1 -1
  77. package/dist/sunday-error.d.ts +11 -0
  78. package/dist/sunday-error.js +31 -0
  79. package/dist/sunday-error.js.map +1 -0
  80. package/dist/url-template.js +13 -0
  81. package/dist/url-template.js.map +1 -1
  82. package/dist/util/any.d.ts +2 -0
  83. package/dist/util/any.js +24 -0
  84. package/dist/util/any.js.map +1 -0
  85. package/dist/util/base64.js +13 -0
  86. package/dist/util/base64.js.map +1 -1
  87. package/dist/util/error.d.ts +2 -0
  88. package/dist/util/error.js +26 -0
  89. package/dist/util/error.js.map +1 -0
  90. package/dist/util/hex.js +16 -2
  91. package/dist/util/hex.js.map +1 -1
  92. package/dist/util/rxjs.d.ts +3 -0
  93. package/dist/util/rxjs.js +22 -5
  94. package/dist/util/rxjs.js.map +1 -1
  95. package/dist/util/stream-rxjs.js +13 -0
  96. package/dist/util/stream-rxjs.js.map +1 -1
  97. package/dist/util/temporal.d.ts +2 -0
  98. package/dist/util/temporal.js +31 -0
  99. package/dist/util/temporal.js.map +1 -0
  100. package/package.json +29 -22
  101. package/src/any-type.ts +18 -0
  102. package/src/class-type.ts +20 -2
  103. package/src/date-time-types.ts +35 -8
  104. package/src/event-parser.ts +204 -0
  105. package/src/fetch-event-source.ts +163 -159
  106. package/src/fetch-request-factory.ts +142 -100
  107. package/src/fetch.ts +79 -14
  108. package/src/header-parameters.ts +66 -0
  109. package/src/index.ts +26 -12
  110. package/src/logger.ts +14 -0
  111. package/src/media-type-codecs/binary-decoder.ts +38 -0
  112. package/src/media-type-codecs/binary-encoder.ts +33 -0
  113. package/src/media-type-codecs/cbor-decoder.ts +529 -0
  114. package/src/media-type-codecs/cbor-encoder.ts +321 -0
  115. package/src/media-type-codecs/cbor-tags.ts +18 -0
  116. package/src/media-type-codecs/json-decoder.ts +484 -0
  117. package/src/media-type-codecs/json-encoder.ts +342 -0
  118. package/src/media-type-codecs/media-type-decoder.ts +34 -0
  119. package/src/media-type-codecs/media-type-decoders.ts +81 -0
  120. package/src/media-type-codecs/media-type-encoder.ts +45 -0
  121. package/src/media-type-codecs/media-type-encoders.ts +83 -0
  122. package/src/media-type-codecs/www-form-url-encoder.ts +193 -0
  123. package/src/media-type.ts +340 -22
  124. package/src/problem.ts +158 -12
  125. package/src/request-factory.ts +35 -12
  126. package/src/sunday-error.ts +51 -0
  127. package/src/url-template.ts +14 -0
  128. package/src/util/any.ts +24 -0
  129. package/src/util/base64.ts +14 -0
  130. package/src/util/error.ts +28 -0
  131. package/src/util/hex.ts +17 -2
  132. package/src/util/rxjs.ts +30 -5
  133. package/src/util/stream-rxjs.ts +14 -0
  134. package/src/util/temporal.ts +32 -0
  135. package/dist/binary-decoder.js +0 -16
  136. package/dist/binary-decoder.js.map +0 -1
  137. package/dist/binary-encoder.js +0 -13
  138. package/dist/binary-encoder.js.map +0 -1
  139. package/dist/cbor-decoder.d.ts +0 -15
  140. package/dist/cbor-decoder.js +0 -126
  141. package/dist/cbor-decoder.js.map +0 -1
  142. package/dist/cbor-encoder.d.ts +0 -29
  143. package/dist/cbor-encoder.js +0 -81
  144. package/dist/cbor-encoder.js.map +0 -1
  145. package/dist/cbor-tags.d.ts +0 -3
  146. package/dist/cbor-tags.js +0 -4
  147. package/dist/cbor-tags.js.map +0 -1
  148. package/dist/http-error.d.ts +0 -10
  149. package/dist/http-error.js +0 -45
  150. package/dist/http-error.js.map +0 -1
  151. package/dist/json-decoder.d.ts +0 -31
  152. package/dist/json-decoder.js +0 -139
  153. package/dist/json-decoder.js.map +0 -1
  154. package/dist/json-encoder.d.ts +0 -35
  155. package/dist/json-encoder.js +0 -134
  156. package/dist/json-encoder.js.map +0 -1
  157. package/dist/media-type-decoder.d.ts +0 -4
  158. package/dist/media-type-decoder.js +0 -2
  159. package/dist/media-type-decoder.js.map +0 -1
  160. package/dist/media-type-decoders.js +0 -40
  161. package/dist/media-type-decoders.js.map +0 -1
  162. package/dist/media-type-encoder.js +0 -6
  163. package/dist/media-type-encoder.js.map +0 -1
  164. package/dist/media-type-encoders.js +0 -42
  165. package/dist/media-type-encoders.js.map +0 -1
  166. package/dist/url-encoder.js.map +0 -1
  167. package/src/binary-decoder.ts +0 -24
  168. package/src/binary-encoder.ts +0 -19
  169. package/src/cbor-decoder.ts +0 -148
  170. package/src/cbor-encoder.ts +0 -95
  171. package/src/cbor-tags.ts +0 -3
  172. package/src/http-error.ts +0 -55
  173. package/src/json-decoder.ts +0 -164
  174. package/src/json-encoder.ts +0 -162
  175. package/src/media-type-decoder.ts +0 -5
  176. package/src/media-type-decoders.ts +0 -59
  177. package/src/media-type-encoder.ts +0 -16
  178. package/src/media-type-encoders.ts +0 -61
  179. package/src/url-encoder.ts +0 -173
@@ -0,0 +1,484 @@
1
+ // Copyright 2020 Outfox, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import {
16
+ DateTimeFormatter,
17
+ DateTimeFormatterBuilder,
18
+ Duration,
19
+ Instant,
20
+ LocalDate,
21
+ LocalDateTime,
22
+ LocalTime,
23
+ nativeJs,
24
+ OffsetDateTime,
25
+ OffsetTime,
26
+ ResolverStyle,
27
+ ZonedDateTime,
28
+ ZoneId,
29
+ ZoneOffset,
30
+ } from '@js-joda/core';
31
+ import { JsonParser } from '@outfoxx/jackson-js';
32
+ import { CustomMapper, Deserializer } from '@outfoxx/jackson-js/dist/@types';
33
+ import { AnyType } from '../any-type';
34
+ import {
35
+ StructuredMediaTypeDecoder,
36
+ TextMediaTypeDecoder,
37
+ } from './media-type-decoder';
38
+ import { Base64 } from '../util/base64';
39
+
40
+ export class JSONDecoder
41
+ implements TextMediaTypeDecoder, StructuredMediaTypeDecoder
42
+ {
43
+ static get default(): JSONDecoder {
44
+ return new JSONDecoder(
45
+ JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
46
+ );
47
+ }
48
+
49
+ private readonly customDeserializers: CustomMapper<Deserializer>[];
50
+ private readonly parser = new JsonParser();
51
+
52
+ constructor(readonly numericDateDecoding: JSONDecoder.NumericDateDecoding) {
53
+ this.customDeserializers = [
54
+ {
55
+ type: () => Date,
56
+ mapper: this.dateDeserializer,
57
+ },
58
+ {
59
+ type: () => Instant,
60
+ mapper: this.instantDeserializer,
61
+ },
62
+ {
63
+ type: () => ZonedDateTime,
64
+ mapper: this.zonedDateTimeDeserializer,
65
+ },
66
+ {
67
+ type: () => OffsetDateTime,
68
+ mapper: this.offsetDateTimeDeserializer,
69
+ },
70
+ {
71
+ type: () => OffsetTime,
72
+ mapper: this.offsetTimeDeserializer,
73
+ },
74
+ {
75
+ type: () => LocalDateTime,
76
+ mapper: this.localDateTimeDeserializer,
77
+ },
78
+ {
79
+ type: () => LocalDate,
80
+ mapper: this.localDateDeserializer,
81
+ },
82
+ {
83
+ type: () => LocalTime,
84
+ mapper: this.localTimeDeserializer,
85
+ },
86
+ {
87
+ type: () => URL,
88
+ mapper: this.urlDeserializer,
89
+ },
90
+ {
91
+ type: () => ArrayBuffer,
92
+ mapper: this.arrayBufferDeserializer,
93
+ },
94
+ ];
95
+ }
96
+
97
+ async decode<T>(response: Response, type: AnyType): Promise<T> {
98
+ return Promise.resolve(this.decodeObject(await response.json(), type));
99
+ }
100
+
101
+ decodeText<T>(text: string, type: AnyType): T {
102
+ return this.parser.parse(text, {
103
+ deserializers: this.customDeserializers,
104
+ mainCreator: () => type,
105
+ }) as T;
106
+ }
107
+
108
+ decodeObject<T>(value: unknown, type: AnyType): T {
109
+ return this.parser.transform(value, {
110
+ deserializers: this.customDeserializers,
111
+ mainCreator: () => type,
112
+ });
113
+ }
114
+
115
+ private instantDeserializer: Deserializer = (key: string, value: unknown) => {
116
+ if (value == null) {
117
+ return value;
118
+ }
119
+ if (value instanceof Instant) {
120
+ return value;
121
+ }
122
+ if (value instanceof Date) {
123
+ return Instant.from(nativeJs(value));
124
+ }
125
+ if (typeof value === 'number') {
126
+ if (
127
+ this.numericDateDecoding ===
128
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
129
+ ) {
130
+ return Instant.ofEpochMilli(value);
131
+ } else if (
132
+ this.numericDateDecoding ===
133
+ JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
134
+ ) {
135
+ const duration = Duration.parse(`PT${value}S`);
136
+ return Instant.ofEpochSecond(duration.seconds(), duration.nano());
137
+ } else {
138
+ throw Error('Unsupported date decoding format');
139
+ }
140
+ }
141
+ if (typeof value === 'string') {
142
+ return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(value));
143
+ }
144
+ throw new Error(`Invalid date value for property ${key}`);
145
+ };
146
+
147
+ private zonedDateTimeDeserializer: Deserializer = (
148
+ key: string,
149
+ value: unknown
150
+ ) => {
151
+ if (value == null) {
152
+ return value;
153
+ }
154
+ if (value instanceof ZonedDateTime) {
155
+ return value;
156
+ }
157
+ if (value instanceof Date) {
158
+ const instant = Instant.from(nativeJs(value));
159
+ return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
160
+ }
161
+ if (typeof value === 'number') {
162
+ if (
163
+ this.numericDateDecoding ===
164
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
165
+ ) {
166
+ const instant = Instant.ofEpochMilli(value);
167
+ return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
168
+ } else if (
169
+ this.numericDateDecoding ===
170
+ JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
171
+ ) {
172
+ const duration = Duration.parse(`PT${value}S`);
173
+ const instant = Instant.ofEpochSecond(
174
+ duration.seconds(),
175
+ duration.nano()
176
+ );
177
+ return ZonedDateTime.ofInstant(instant, ZoneId.UTC);
178
+ } else {
179
+ throw Error('Unsupported date decoding format');
180
+ }
181
+ }
182
+ if (typeof value === 'string') {
183
+ return ZonedDateTime.from(
184
+ DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(value)
185
+ );
186
+ }
187
+ throw new Error(`Invalid date value for property ${key}`);
188
+ };
189
+
190
+ private offsetDateTimeDeserializer: Deserializer = (
191
+ key: string,
192
+ value: unknown
193
+ ) => {
194
+ if (value == null) {
195
+ return value;
196
+ }
197
+ if (value instanceof OffsetDateTime) {
198
+ return value;
199
+ }
200
+ if (value instanceof Date) {
201
+ const instant = Instant.from(nativeJs(value));
202
+ return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
203
+ }
204
+ if (typeof value === 'number') {
205
+ if (
206
+ this.numericDateDecoding ===
207
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
208
+ ) {
209
+ const instant = Instant.ofEpochMilli(value);
210
+ return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
211
+ } else if (
212
+ this.numericDateDecoding ===
213
+ JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
214
+ ) {
215
+ const duration = Duration.parse(`PT${value}S`);
216
+ const instant = Instant.ofEpochSecond(
217
+ duration.seconds(),
218
+ duration.nano()
219
+ );
220
+ return OffsetDateTime.ofInstant(instant, ZoneId.UTC);
221
+ } else {
222
+ throw Error('Unsupported date decoding format');
223
+ }
224
+ }
225
+ if (typeof value === 'string') {
226
+ return OffsetDateTime.from(
227
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(value)
228
+ );
229
+ }
230
+ throw new Error(`Invalid date value for property ${key}`);
231
+ };
232
+
233
+ private offsetTimeFormatter = new DateTimeFormatterBuilder()
234
+ .parseCaseInsensitive()
235
+ .append(DateTimeFormatter.ISO_LOCAL_TIME)
236
+ .appendOffsetId()
237
+ .toFormatter(ResolverStyle.STRICT);
238
+
239
+ private offsetTimeDeserializer: Deserializer = (
240
+ key: string,
241
+ value: unknown
242
+ ) => {
243
+ if (value == null) {
244
+ return value;
245
+ }
246
+ if (value instanceof OffsetTime) {
247
+ return value;
248
+ }
249
+ if (value instanceof Array) {
250
+ let idx = 0;
251
+ const hour = value[idx++] as number;
252
+ const minute = value[idx++] as number;
253
+ let second = 0;
254
+ let nanoOfSecond = 0;
255
+ if (value.length > 3) {
256
+ second = value[idx++];
257
+ if (value.length > 4) {
258
+ nanoOfSecond = value[idx++];
259
+ if (
260
+ this.numericDateDecoding ==
261
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
262
+ ) {
263
+ // millis to nanos
264
+ nanoOfSecond *= 1_000_000;
265
+ }
266
+ }
267
+ }
268
+ const offset = value[idx++] as string;
269
+ return OffsetTime.of(
270
+ hour,
271
+ minute,
272
+ second,
273
+ nanoOfSecond,
274
+ ZoneOffset.of(offset)
275
+ );
276
+ }
277
+ if (typeof value === 'string') {
278
+ return OffsetTime.from(this.offsetTimeFormatter.parse(value));
279
+ }
280
+ throw new Error(`Invalid date value for property ${key}`);
281
+ };
282
+
283
+ private localDateTimeDeserializer: Deserializer = (
284
+ key: string,
285
+ value: unknown
286
+ ) => {
287
+ if (value == null) {
288
+ return value;
289
+ }
290
+ if (value instanceof LocalDateTime) {
291
+ return value;
292
+ }
293
+ if (value instanceof Date) {
294
+ const instant = Instant.ofEpochMilli(value.getTime());
295
+ return LocalDateTime.ofInstant(instant, ZoneId.UTC);
296
+ }
297
+ if (value instanceof Array) {
298
+ let idx = 0;
299
+ const year = value[idx++];
300
+ const month = value[idx++];
301
+ const day = value[idx++];
302
+ const hour = value[idx++];
303
+ const minute = value[idx++];
304
+ let second = 0;
305
+ let nanoOfSecond = 0;
306
+ if (value.length > 5) {
307
+ second = value[idx++];
308
+ if (value.length > 6) {
309
+ nanoOfSecond = value[idx++];
310
+ if (
311
+ this.numericDateDecoding ==
312
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
313
+ ) {
314
+ // millis to nanos
315
+ nanoOfSecond *= 1_000_000;
316
+ }
317
+ }
318
+ }
319
+ return LocalDateTime.of(
320
+ year,
321
+ month,
322
+ day,
323
+ hour,
324
+ minute,
325
+ second,
326
+ nanoOfSecond
327
+ );
328
+ }
329
+ if (typeof value === 'string') {
330
+ return LocalDateTime.from(
331
+ DateTimeFormatter.ISO_LOCAL_DATE_TIME.parse(value)
332
+ );
333
+ }
334
+ throw new Error(`Invalid date value for property ${key}`);
335
+ };
336
+
337
+ private localDateDeserializer: Deserializer = (
338
+ key: string,
339
+ value: unknown
340
+ ) => {
341
+ if (value == null) {
342
+ return value;
343
+ }
344
+ if (value instanceof LocalDate) {
345
+ return value;
346
+ }
347
+ if (value instanceof Date) {
348
+ const instant = Instant.ofEpochMilli(value.getTime());
349
+ return LocalDate.ofInstant(instant, ZoneId.UTC);
350
+ }
351
+ if (value instanceof Array) {
352
+ const year = value[0];
353
+ const month = value[1];
354
+ const day = value[2];
355
+ return LocalDate.of(year, month, day);
356
+ }
357
+ if (typeof value === 'number') {
358
+ return LocalDate.ofEpochDay(value);
359
+ }
360
+ if (typeof value === 'string') {
361
+ return LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse(value));
362
+ }
363
+ throw new Error(`Invalid date value for property ${key}`);
364
+ };
365
+
366
+ private localTimeDeserializer: Deserializer = (
367
+ key: string,
368
+ value: unknown
369
+ ) => {
370
+ if (value == null) {
371
+ return value;
372
+ }
373
+ if (value instanceof LocalTime) {
374
+ return value;
375
+ }
376
+ if (value instanceof Date) {
377
+ const instant = Instant.ofEpochMilli(value.getTime());
378
+ return LocalTime.ofInstant(instant, ZoneId.UTC);
379
+ }
380
+ if (value instanceof Array) {
381
+ let idx = 0;
382
+ const hour = value[idx++];
383
+ const minute = value[idx++];
384
+ let second = 0;
385
+ let nanoOfSecond = 0;
386
+ if (value.length > 2) {
387
+ second = value[idx++];
388
+ if (value.length > 3) {
389
+ nanoOfSecond = value[idx++];
390
+ if (
391
+ this.numericDateDecoding ==
392
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
393
+ ) {
394
+ // millis to nanos
395
+ nanoOfSecond *= 1_000_000;
396
+ }
397
+ }
398
+ }
399
+ return LocalTime.of(hour, minute, second, nanoOfSecond);
400
+ }
401
+ if (typeof value === 'number') {
402
+ return LocalTime.ofNanoOfDay(value);
403
+ }
404
+ if (typeof value === 'string') {
405
+ return LocalTime.from(DateTimeFormatter.ISO_LOCAL_TIME.parse(value));
406
+ }
407
+ throw new Error(`Invalid date value for property ${key}`);
408
+ };
409
+
410
+ private dateDeserializer: Deserializer = (key: string, value: unknown) => {
411
+ if (value == null) {
412
+ return value;
413
+ }
414
+ if (value instanceof Date) {
415
+ return value;
416
+ }
417
+ if (typeof value === 'number') {
418
+ if (
419
+ this.numericDateDecoding ===
420
+ JSONDecoder.NumericDateDecoding.MILLISECONDS_SINCE_EPOCH
421
+ ) {
422
+ return new Date(value);
423
+ } else if (
424
+ this.numericDateDecoding ===
425
+ JSONDecoder.NumericDateDecoding.DECIMAL_SECONDS_SINCE_EPOCH
426
+ ) {
427
+ return new Date(value * 1000);
428
+ } else {
429
+ throw Error('Unsupported date decoding format');
430
+ }
431
+ }
432
+ if (typeof value === 'string') {
433
+ return new Date(value);
434
+ }
435
+ throw new Error(`Invalid date value for property ${key}`);
436
+ };
437
+
438
+ private urlDeserializer: Deserializer = (key: string, value: unknown) => {
439
+ if (value == null) {
440
+ return value;
441
+ }
442
+ if (value instanceof URL) {
443
+ return value;
444
+ }
445
+ if (typeof value === 'string') {
446
+ return new URL(value);
447
+ }
448
+ throw Error(`Invalid URL value for property ${key}`);
449
+ };
450
+
451
+ private arrayBufferDeserializer: Deserializer = (
452
+ key: string,
453
+ value: unknown
454
+ ) => {
455
+ if (value == null) {
456
+ return value;
457
+ }
458
+ if (value instanceof ArrayBuffer) {
459
+ return value;
460
+ }
461
+ if (typeof value === 'string') {
462
+ return Base64.decode(value);
463
+ }
464
+ throw Error(`Invalid ArrayBuffer value for property ${key}`);
465
+ };
466
+ }
467
+
468
+ export namespace JSONDecoder {
469
+ /**
470
+ * Configures how numeric temporal values are decoded.
471
+ */
472
+ export enum NumericDateDecoding {
473
+ /**
474
+ * Decode numeric temporal values assuming they are seconds with decimal
475
+ * sub-second precision.
476
+ */
477
+ DECIMAL_SECONDS_SINCE_EPOCH,
478
+
479
+ /**
480
+ * Decode numeric temporal values assuming they are integer milliseconds.
481
+ */
482
+ MILLISECONDS_SINCE_EPOCH,
483
+ }
484
+ }