-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
providerFactory.ts
85 lines (76 loc) · 2.93 KB
/
providerFactory.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
AllPublishOptions,
BaseS3Options,
BitbucketOptions,
CustomPublishOptions,
GenericServerOptions,
getS3LikeProviderBaseUrl,
GithubOptions,
KeygenOptions,
newError,
PublishConfiguration,
} from "builder-util-runtime"
import { AppUpdater } from "./AppUpdater"
import { BitbucketProvider } from "./providers/BitbucketProvider"
import { GenericProvider } from "./providers/GenericProvider"
import { GitHubProvider } from "./providers/GitHubProvider"
import { KeygenProvider } from "./providers/KeygenProvider"
import { PrivateGitHubProvider } from "./providers/PrivateGitHubProvider"
import { Provider, ProviderRuntimeOptions } from "./providers/Provider"
export function isUrlProbablySupportMultiRangeRequests(url: string): boolean {
return !url.includes("s3.amazonaws.com")
}
export function createClient(data: PublishConfiguration | AllPublishOptions, updater: AppUpdater, runtimeOptions: ProviderRuntimeOptions): Provider<any> {
// noinspection SuspiciousTypeOfGuard
if (typeof data === "string") {
throw newError("Please pass PublishConfiguration object", "ERR_UPDATER_INVALID_PROVIDER_CONFIGURATION")
}
const provider = data.provider
switch (provider) {
case "github": {
const githubOptions = data as GithubOptions
const token = (githubOptions.private ? process.env["GH_TOKEN"] || process.env["GITHUB_TOKEN"] : null) || githubOptions.token
if (token == null) {
return new GitHubProvider(githubOptions, updater, runtimeOptions)
} else {
return new PrivateGitHubProvider(githubOptions, updater, token, runtimeOptions)
}
}
case "bitbucket":
return new BitbucketProvider(data as BitbucketOptions, updater, runtimeOptions)
case "keygen":
return new KeygenProvider(data as KeygenOptions, updater, runtimeOptions)
case "s3":
case "spaces":
return new GenericProvider(
{
provider: "generic",
url: getS3LikeProviderBaseUrl(data),
channel: (data as BaseS3Options).channel || null,
},
updater,
{
...runtimeOptions,
// https://github.com/minio/minio/issues/5285#issuecomment-350428955
isUseMultipleRangeRequest: false,
}
)
case "generic": {
const options = data as GenericServerOptions
return new GenericProvider(options, updater, {
...runtimeOptions,
isUseMultipleRangeRequest: options.useMultipleRangeRequest !== false && isUrlProbablySupportMultiRangeRequests(options.url),
})
}
case "custom": {
const options = data as CustomPublishOptions
const constructor = options.updateProvider
if (!constructor) {
throw newError("Custom provider not specified", "ERR_UPDATER_INVALID_PROVIDER_CONFIGURATION")
}
return new constructor(options, updater, runtimeOptions)
}
default:
throw newError(`Unsupported provider: ${provider}`, "ERR_UPDATER_UNSUPPORTED_PROVIDER")
}
}