@outfoxx/sunday 1.0.8 → 1.1.0-alpha.10

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