-
Notifications
You must be signed in to change notification settings - Fork 573
/
Copy pathsdkV2Compat.ts
57 lines (51 loc) · 1.79 KB
/
sdkV2Compat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as AWS from 'aws-sdk'
import { Token } from 'aws-sdk/lib/token'
import { Connection } from '../connection'
import { getLogger } from '../../shared/logger/logger'
/**
* {@link AWS.Token} is defined when {@link Token} is imported.
* But for {@link Token} to not get tree-shaken we need to use it.
* So the following simply uses it and now {@link AWS.Token} will not
* be undefined anymore.
*/
Token
AWS.Token
/**
* But sometimes both are always undefined... it may be due to some race condition.
* This seems to only happen in the browser, and as of this writing Token is only
* used for CodeCatalyst. So, we can safely override it with a dummy if it is broken.
*/
let _TokenStub: typeof AWS.Token
if (AWS.Token === undefined) {
getLogger().error('Tried importing AWS.Token but it is undefined. Some Toolkit features may not work correctly.')
_TokenStub = class _Empty {} as any
} else {
_TokenStub = AWS.Token
}
export class TokenProvider extends _TokenStub {
public constructor(private readonly connection: Connection & { type: 'sso' }) {
super({ token: '', expireTime: new Date(0) })
}
public override get(cb: (err?: AWS.AWSError | undefined) => void): void {
if (!this.token || this.needsRefresh()) {
this.refresh(cb)
} else {
cb()
}
}
public override refresh(cb: (err?: AWS.AWSError | undefined) => void): void {
this.connection
.getToken()
.then(({ accessToken, expiresAt }) => {
this.token = accessToken
this.expireTime = expiresAt
this.expired = false
cb()
})
.catch(cb)
}
}