source.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import request from '../lib/request'
  2. export type SourceInfo = {
  3. version: string
  4. downloadUrl: string
  5. }
  6. export abstract class BaseSource {
  7. abstract getSourceInfo(): Promise<SourceInfo | undefined>
  8. protected getPlatformIdentifier() {
  9. const arch = process.arch === 'x64' ? 'amd64' : process.arch
  10. return process.platform === 'win32'
  11. ? `windows-${arch}.exe`
  12. : `${process.platform}-${arch}`
  13. }
  14. }
  15. /**
  16. * Download mkcert from github.com
  17. */
  18. export class GithubSource extends BaseSource {
  19. public static create() {
  20. return new GithubSource()
  21. }
  22. private constructor() {
  23. super()
  24. }
  25. public async getSourceInfo(): Promise<SourceInfo | undefined> {
  26. const { data } = await request({
  27. method: 'GET',
  28. url: 'https://api.github.com/repos/FiloSottile/mkcert/releases/latest',
  29. })
  30. const platformIdentifier = this.getPlatformIdentifier()
  31. const version = data.tag_name
  32. const downloadUrl = data.assets.find((item: any) =>
  33. item.name.includes(platformIdentifier)
  34. )?.browser_download_url
  35. if (!(version && downloadUrl)) {
  36. return undefined
  37. }
  38. return {
  39. downloadUrl,
  40. version
  41. }
  42. }
  43. }
  44. /**
  45. * Download mkcert from coding.net
  46. *
  47. * @see https://help.coding.net/openapi
  48. */
  49. export class CodingSource extends BaseSource {
  50. public static CODING_API = 'https://e.coding.net/open-api'
  51. public static CODING_AUTHORIZATION =
  52. 'token 000f7831ec425079439b0f55f55c729c9280d66e'
  53. public static CODING_PROJECT_ID = 8524617
  54. public static REPOSITORY = 'mkcert'
  55. public static create() {
  56. return new CodingSource()
  57. }
  58. private constructor() {
  59. super()
  60. }
  61. private async request(data: any) {
  62. return request({
  63. data,
  64. method: 'POST',
  65. url: CodingSource.CODING_API,
  66. headers: {
  67. Authorization: CodingSource.CODING_AUTHORIZATION
  68. }
  69. })
  70. }
  71. /**
  72. * Get filename of Coding.net artifacts
  73. *
  74. * @see https://liuweigl.coding.net/p/github/artifacts/885241/generic/packages
  75. *
  76. * @returns name
  77. */
  78. private getPackageName() {
  79. return `mkcert-${this.getPlatformIdentifier()}`
  80. }
  81. async getSourceInfo(): Promise<SourceInfo | undefined> {
  82. /**
  83. * @see https://help.coding.net/openapi#e2106ec64e75af66f188463b1bb7e165
  84. */
  85. const { data: VersionData } = await this.request({
  86. Action: 'DescribeArtifactVersionList',
  87. ProjectId: CodingSource.CODING_PROJECT_ID,
  88. Repository: CodingSource.REPOSITORY,
  89. Package: this.getPackageName(),
  90. PageSize: 1
  91. })
  92. const version = VersionData.Response.Data?.InstanceSet[0]?.Version
  93. if (!version) {
  94. return undefined
  95. }
  96. /**
  97. * @see https://help.coding.net/openapi#63ad6bc7469373cef575e92bb92be71e
  98. */
  99. const { data: FileData } = await this.request({
  100. Action: 'DescribeArtifactFileDownloadUrl',
  101. ProjectId: CodingSource.CODING_PROJECT_ID,
  102. Repository: CodingSource.REPOSITORY,
  103. Package: this.getPackageName(),
  104. PackageVersion: version
  105. })
  106. const downloadUrl = FileData.Response.Url
  107. if (!downloadUrl) {
  108. return undefined
  109. }
  110. return {
  111. downloadUrl,
  112. version
  113. }
  114. }
  115. }