1
- import { fromBase64 } from '@aws-sdk/util-base64-node ' ;
1
+ import { fromBase64 } from '@aws-sdk/util-base64' ;
2
2
import { GetOptions } from './GetOptions' ;
3
3
import { GetMultipleOptions } from './GetMultipleOptions' ;
4
4
import { ExpirableValue } from './ExpirableValue' ;
5
5
import { TRANSFORM_METHOD_BINARY , TRANSFORM_METHOD_JSON } from './constants' ;
6
6
import { GetParameterError , TransformParameterError } from './Exceptions' ;
7
7
import type { BaseProviderInterface , GetMultipleOptionsInterface , GetOptionsInterface , TransformOptions } from './types' ;
8
8
9
+ // These providers are dinamycally intialized on first use of the helper functions
10
+ const DEFAULT_PROVIDERS : Record < string , BaseProvider > = { } ;
11
+
9
12
abstract class BaseProvider implements BaseProviderInterface {
10
13
protected store : Map < string , ExpirableValue > ;
11
14
12
- public constructor ( ) {
15
+ public constructor ( ) {
13
16
this . store = new Map ( ) ;
14
17
}
15
18
16
- public addToCache ( key : string , value : string | Record < string , unknown > , maxAge : number ) : void {
19
+ public addToCache ( key : string , value : string | Uint8Array | Record < string , unknown > , maxAge : number ) : void {
17
20
if ( maxAge <= 0 ) return ;
18
21
19
22
this . store . set ( key , new ExpirableValue ( value , maxAge ) ) ;
@@ -22,7 +25,7 @@ abstract class BaseProvider implements BaseProviderInterface {
22
25
public clearCache ( ) : void {
23
26
this . store . clear ( ) ;
24
27
}
25
-
28
+
26
29
/**
27
30
* Retrieve a parameter value or return the cached value
28
31
*
@@ -37,7 +40,7 @@ abstract class BaseProvider implements BaseProviderInterface {
37
40
* @param {string } name - Parameter name
38
41
* @param {GetOptionsInterface } options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
39
42
*/
40
- public async get ( name : string , options ?: GetOptionsInterface ) : Promise < undefined | string | Record < string , unknown > > {
43
+ public async get ( name : string , options ?: GetOptionsInterface ) : Promise < undefined | string | Uint8Array | Record < string , unknown > > {
41
44
const configs = new GetOptions ( options ) ;
42
45
const key = [ name , configs . transform ] . toString ( ) ;
43
46
@@ -49,7 +52,7 @@ abstract class BaseProvider implements BaseProviderInterface {
49
52
50
53
let value ;
51
54
try {
52
- value = await this . _get ( name , options ?. sdkOptions ) ;
55
+ value = await this . _get ( name , options ) ;
53
56
} catch ( error ) {
54
57
throw new GetParameterError ( ( error as Error ) . message ) ;
55
58
}
@@ -76,9 +79,9 @@ abstract class BaseProvider implements BaseProviderInterface {
76
79
return this . store . get ( key ) ! . value as Record < string , unknown > ;
77
80
}
78
81
79
- let values : Record < string , unknown > = { } ;
82
+ let values = { } ;
80
83
try {
81
- values = await this . _getMultiple ( path , options ?. sdkOptions ) ;
84
+ values = await this . _getMultiple ( path , options ) ;
82
85
} catch ( error ) {
83
86
throw new GetParameterError ( ( error as Error ) . message ) ;
84
87
}
@@ -99,11 +102,17 @@ abstract class BaseProvider implements BaseProviderInterface {
99
102
* Retrieve parameter value from the underlying parameter store
100
103
*
101
104
* @param {string } name - Parameter name
102
- * @param {unknown } sdkOptions - Options to pass to the underlying AWS SDK
105
+ * @param {unknown } options - Options to pass to the underlying implemented method
103
106
*/
104
- protected abstract _get ( name : string , sdkOptions ?: unknown ) : Promise < string | undefined > ;
107
+ protected abstract _get ( name : string , options ?: unknown ) : Promise < string | Uint8Array | undefined > ;
105
108
106
- protected abstract _getMultiple ( path : string , sdkOptions ?: unknown ) : Promise < Record < string , string | undefined > > ;
109
+ /**
110
+ * Retrieve multiple parameter values from the underlying parameter store
111
+ *
112
+ * @param {string } path - Parameter name
113
+ * @param {unknown } options - Options to pass to the underlying implementated method
114
+ */
115
+ protected abstract _getMultiple ( path : string , options ?: unknown ) : Promise < Record < string , string | undefined > > ;
107
116
108
117
/**
109
118
* Check whether a key has expired in the cache or not
@@ -115,42 +124,43 @@ abstract class BaseProvider implements BaseProviderInterface {
115
124
private hasKeyExpiredInCache ( key : string ) : boolean {
116
125
const value = this . store . get ( key ) ;
117
126
if ( value ) return value . isExpired ( ) ;
118
-
127
+
119
128
return true ;
120
129
}
121
130
122
131
}
123
132
124
- // TODO: revisit `value` type once we are clearer on the types returned by the various SDKs
125
- const transformValue = ( value : unknown , transform : TransformOptions , throwOnTransformError : boolean , key : string = '' ) : string | Record < string , unknown > | undefined => {
133
+ const transformValue = ( value : string | Uint8Array | undefined , transform : TransformOptions , throwOnTransformError : boolean , key : string = '' ) : string | Record < string , unknown > | undefined => {
126
134
try {
127
135
const normalizedTransform = transform . toLowerCase ( ) ;
128
136
if (
129
137
( normalizedTransform === TRANSFORM_METHOD_JSON ||
130
- ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_JSON } ` ) ) ) &&
138
+ ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_JSON } ` ) ) ) &&
131
139
typeof value === 'string'
132
140
) {
133
141
return JSON . parse ( value ) as Record < string , unknown > ;
134
142
} else if (
135
143
( normalizedTransform === TRANSFORM_METHOD_BINARY ||
136
- ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_BINARY } ` ) ) ) &&
137
- typeof value === 'string'
144
+ ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_BINARY } ` ) ) )
138
145
) {
139
- return new TextDecoder ( 'utf-8' ) . decode ( fromBase64 ( value ) ) ;
146
+ if ( typeof value === 'string' ) {
147
+ return new TextDecoder ( 'utf-8' ) . decode ( fromBase64 ( value ) ) ;
148
+ } else {
149
+ return new TextDecoder ( 'utf-8' ) . decode ( value ) ;
150
+ }
140
151
} else {
141
- // TODO: revisit this type once we are clearer on types returned by SDKs
142
152
return value as string ;
143
153
}
144
154
} catch ( error ) {
145
155
if ( throwOnTransformError )
146
156
throw new TransformParameterError ( transform , ( error as Error ) . message ) ;
147
-
157
+
148
158
return ;
149
159
}
150
160
} ;
151
161
152
- const transformValues = ( value : Record < string , unknown > , transform : TransformOptions , throwOnTransformError : boolean ) : Record < string , unknown > => {
153
- const transformedValues : Record < string , unknown > = { } ;
162
+ const transformValues = ( value : Record < string , string | undefined > , transform : TransformOptions , throwOnTransformError : boolean ) : Record < string , string | Record < string , unknown > | undefined > => {
163
+ const transformedValues : Record < string , string | Record < string , unknown > | undefined > = { } ;
154
164
for ( const [ entryKey , entryValue ] of Object . entries ( value ) ) {
155
165
try {
156
166
transformedValues [ entryKey ] = transformValue ( entryValue , transform , throwOnTransformError , entryKey ) ;
@@ -167,4 +177,5 @@ export {
167
177
BaseProvider ,
168
178
ExpirableValue ,
169
179
transformValue ,
180
+ DEFAULT_PROVIDERS ,
170
181
} ;
0 commit comments