-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
token-provider.js
66 lines (53 loc) · 1.41 KB
/
token-provider.js
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
58
59
60
61
62
63
64
65
66
'use strict'
const { Token, TokenPool } = require('./token-pool')
class StaticTokenProvider {
constructor({ tokenValidator, tokenString }) {
if (typeof tokenValidator !== 'function') {
throw Error('tokenValidator is not a function')
}
if (!tokenValidator(tokenString)) {
throw Error(`Not a valid token: ${tokenString}`)
}
this.staticToken = new Token(tokenString, null)
}
addToken() {
throw Error(
'When using token persistence, do not provide a static gh_token'
)
}
nextToken() {
return this.staticToken
}
}
class PoolingTokenProvider {
/*
tokenValidator: A function which returns true if the argument is a valid token.
*/
constructor({ tokenValidator, batchSize = 25 }) {
if (typeof tokenValidator !== 'function') {
throw Error('tokenValidator is not a function')
}
this.tokenValidator = tokenValidator
this.tokenPool = new TokenPool({ batchSize })
}
addToken(tokenString) {
if (!this.tokenValidator(tokenString)) {
throw Error(`Not a valid token: ${tokenString}`)
}
this.tokenPool.add(tokenString)
}
nextToken() {
return this.tokenPool.next()
}
// Return an array of token strings.
toNative() {
return this.tokenPool.allValidTokenIds()
}
serializeDebugInfo(options) {
return this.tokenPool.serializeDebugInfo(options)
}
}
module.exports = {
StaticTokenProvider,
PoolingTokenProvider,
}