1
- import { BufferFormat , ViewFormat } from './formats'
1
+ import { BufferFormat , ViewFormat , UTF8Format } from './formats'
2
2
3
3
/**
4
4
* Encodes {@link TIn} to {@link TFormat} and decodes
5
5
* {@link TFormat} to {@link TOut}.
6
6
*/
7
- export abstract class Encoding < TIn , TFormat , TOut > {
8
- constructor ( options : EncodingOptions < TIn , TFormat , TOut > )
7
+ export abstract class Encoding < TIn , TFormat , TOut > implements IEncoding < TIn , TFormat , TOut > {
8
+ constructor ( options : IEncoding < TIn , TFormat , TOut > )
9
9
10
- /** Encode data. */
11
10
encode : ( data : TIn ) => TFormat
12
-
13
- /** Decode data. */
14
11
decode : ( data : TFormat ) => TOut
15
-
16
- /** Unique name. */
17
12
name : string
13
+ format : 'buffer' | 'view' | 'utf8'
14
+ createViewTranscoder ( ) : ViewFormat < TIn , TOut >
15
+ createBufferTranscoder ( ) : BufferFormat < TIn , TOut >
16
+ createUTF8Transcoder ( ) : UTF8Format < TIn , TOut >
18
17
19
18
/**
20
19
* Common name, computed from {@link name}. If this encoding is a
@@ -23,53 +22,65 @@ export abstract class Encoding<TIn, TFormat, TOut> {
23
22
* will equal {@link commonName}.
24
23
*/
25
24
get commonName ( ) : string
25
+ }
26
+
27
+ export interface IEncoding < TIn , TFormat , TOut > {
28
+ /**
29
+ * Encode data.
30
+ */
31
+ encode : ( data : TIn ) => TFormat
32
+
33
+ /**
34
+ * Decode data.
35
+ */
36
+ decode : ( data : TFormat ) => TOut
26
37
27
38
/**
28
- * Name of the (lower-level) encoding used by the return value of
39
+ * Unique name.
40
+ */
41
+ name : string
42
+
43
+ /**
44
+ * The name of the (lower-level) encoding used by the return value of
29
45
* {@link encode}. One of 'buffer', 'view', 'utf8'.
30
46
*/
31
47
format : 'buffer' | 'view' | 'utf8'
32
48
33
49
/**
34
- * Create a new encoding that transcodes {@link TFormat} to a view.
50
+ * Create a new encoding that transcodes {@link TFormat} from / to a view.
35
51
*/
36
- createViewTranscoder ( ) : ViewFormat < TIn , TOut >
52
+ createViewTranscoder ?: ( ( ) => ViewFormat < TIn , TOut > ) | undefined
37
53
38
54
/**
39
- * Create a new encoding that transcodes {@link TFormat} to a buffer.
55
+ * Create a new encoding that transcodes {@link TFormat} from / to a buffer.
40
56
*/
41
- createBufferTranscoder ( ) : BufferFormat < TIn , TOut >
57
+ createBufferTranscoder ?: ( ( ) => BufferFormat < TIn , TOut > ) | undefined
58
+
59
+ /**
60
+ * Create a new encoding that transcodes {@link TFormat} from / to a UTF-8 string.
61
+ */
62
+ createUTF8Transcoder ?: ( ( ) => UTF8Format < TIn , TOut > ) | undefined
42
63
}
43
64
44
- // TODO: split into multiple interfaces (and then union those) in order to
45
- // separate the main level-transcoder interface from compatibility options.
46
- export interface EncodingOptions < TIn , TFormat , TOut > {
65
+ export interface IExternalEncoding < TIn , TFormat , TOut > {
47
66
/**
48
67
* Encode data.
49
68
*/
50
- encode ? : ( ( data : TIn ) => TFormat ) | undefined
69
+ encode : ( data : TIn ) => TFormat
51
70
52
71
/**
53
72
* Decode data.
54
73
*/
55
- decode ? : ( ( data : TFormat ) => TOut ) | undefined
74
+ decode : ( data : TFormat ) => TOut
56
75
57
76
/**
58
77
* Unique name.
59
78
*/
60
79
name ?: string | undefined
61
80
62
- /**
63
- * The name of the (lower-level) encoding used by the return value of
64
- * {@link encode}. One of 'buffer', 'view', 'utf8'. Defaults to 'buffer'
65
- * if the {@link buffer} and {@link code} options are also undefined.
66
- */
67
- format ?: 'buffer' | 'view' | 'utf8' | undefined
68
-
69
81
/**
70
82
* Legacy `level-codec` option that means the same as `format: 'buffer'`
71
- * if true or `format: 'utf8'` if false. Used only when the {@link format} option
72
- * is undefined.
83
+ * if true or `format: 'utf8'` if false.
73
84
*/
74
85
buffer ?: boolean | undefined
75
86
@@ -80,13 +91,68 @@ export interface EncodingOptions<TIn, TFormat, TOut> {
80
91
type ?: any
81
92
82
93
/**
83
- * For compatibility with `multiformats`. If a number, then the encoding is
84
- * assumed to have a {@link format} of 'view'. Used only when the {@link format}
85
- * and {@link buffer} options are undefined.
94
+ * To detect `multiformats`. If a number, then the encoding is
95
+ * assumed to have a {@link format} of 'view'.
86
96
* @see https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts
87
97
*/
88
98
code ?: any
89
-
90
- createViewTranscoder ?: ( ( ) => ViewFormat < TIn , TOut > ) | undefined
91
- createBufferTranscoder ?: ( ( ) => BufferFormat < TIn , TOut > ) | undefined
92
99
}
100
+
101
+ /**
102
+ * Names of built-in encodings.
103
+ */
104
+ export type KnownEncodingName = 'utf8' | 'buffer' | 'view' | 'json' | 'hex' | 'base64'
105
+
106
+ /**
107
+ * One of the supported encoding interfaces.
108
+ */
109
+ export type MixedEncoding < TIn , TFormat , TOut > =
110
+ IEncoding < TIn , TFormat , TOut > |
111
+ IExternalEncoding < TIn , TFormat , TOut >
112
+
113
+ /**
114
+ * Type utility to cast a built-in encoding identified by its name to an {@link Encoding}.
115
+ */
116
+ export type KnownEncoding < N extends KnownEncodingName , TFormat >
117
+ = Encoding < KnownEncodingInput < N > , TFormat , KnownEncodingOutput < N > >
118
+
119
+ /**
120
+ * Type utility to get the input type of a built-in encoding identified by its name.
121
+ */
122
+ export type KnownEncodingInput < N extends KnownEncodingName >
123
+ = N extends 'utf8' ? string | Buffer | Uint8Array
124
+ : N extends 'buffer' ? Buffer | Uint8Array | string
125
+ : N extends 'view' ? Uint8Array | string
126
+ : N extends 'json' ? any
127
+ : N extends 'hex' ? Buffer | string
128
+ : N extends 'base64' ? Buffer | string
129
+ : never
130
+
131
+ /**
132
+ * Type utility to get the output type of a built-in encoding identified by its name.
133
+ */
134
+ export type KnownEncodingOutput < N extends KnownEncodingName >
135
+ = N extends 'utf8' ? string
136
+ : N extends 'buffer' ? Buffer
137
+ : N extends 'view' ? Uint8Array
138
+ : N extends 'json' ? any
139
+ : N extends 'hex' ? string
140
+ : N extends 'base64' ? string
141
+ : never
142
+
143
+ /**
144
+ * Type utility to use a {@link MixedEncoding} with an untyped format.
145
+ */
146
+ export type PartialEncoding < TIn , TOut = TIn > = MixedEncoding < TIn , any , TOut >
147
+
148
+ /**
149
+ * Type utility to use a {@link MixedEncoding} with an untyped format and output.
150
+ * For when only the encoding side is needed.
151
+ */
152
+ export type PartialEncoder < TIn > = MixedEncoding < TIn , any , any >
153
+
154
+ /**
155
+ * Type utility to use a {@link MixedEncoding} with an untyped input and format.
156
+ * For when only the decoding side is needed.
157
+ */
158
+ export type PartialDecoder < TOut > = MixedEncoding < any , any , TOut >
0 commit comments