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