-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpackage.ts
90 lines (79 loc) · 2.94 KB
/
package.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
86
87
88
89
90
import { Context } from 'koa'
import { downloadZipFile, fetchIdentifiers, fetchManifest, fetchTags, fetchZipHash } from '../services/githubService'
import { IGithubTag, IPackageReleases } from '../services/types'
import { parseRequestArgs } from './utils'
export const listPackages = async function (ctx: Context) {
const { scope, pkg } = parseRequestArgs(ctx)
const tags = await fetchTags(scope, pkg)
ctx.set('Content-Type', 'application/json')
if (tags.length > 0) {
const latestVersion = tags[0].name
const links = [
`<https://${ctx.host}/${scope}/${pkg}/${latestVersion}>; rel="latest-version"`,
`<https://github.com/${scope}/${pkg}>; rel="canonical"`,
`<ssh://[email protected]:${scope}/${pkg}.git>; rel="alternate"`,
]
ctx.append('Link', links.join(','))
}
const releases = tags.reduce((pre: IPackageReleases, current: IGithubTag) => {
const releaseVersion = current.name
return {
...pre,
[releaseVersion]: {
url: `https://${ctx.host}/${scope}/${pkg}/${releaseVersion}`,
},
}
}, {})
ctx.body = { releases }
}
export const fetchMetaForPackage = async function (ctx: Context) {
const { scope, pkg, version } = parseRequestArgs(ctx)
const zipHash = await fetchZipHash(scope, pkg, version)
ctx.set({
Link: `<https://${ctx.host}/${scope}/${pkg}/${version}>; rel="latest-version"`,
'Content-Type': 'application/json',
})
ctx.body = {
id: `${scope}.${pkg}`,
version: version,
resources: [
{
name: 'source-archive',
type: 'application/zip',
checksum: zipHash,
},
],
metadata: {},
}
}
export const fetchManifestForPackage = async function (ctx: Context) {
const toolVersion = ctx.query['swift-version'] || '5.7'
const { scope, pkg, version } = parseRequestArgs(ctx)
const packageString = await fetchManifest(scope, pkg, version)
ctx.set({
'Content-Type': 'text/x-swift',
'Content-Disposition': `attachment; filename="Package@swift-${toolVersion}.swift"`,
})
ctx.body = packageString
}
export const downloadSourceCode = async function (ctx: Context) {
const { scope, pkg, version } = parseRequestArgs(ctx)
const fileInfo = await downloadZipFile(scope, pkg, version)
ctx.set({
'Accept-Ranges': 'bytes',
'Cache-Control': 'public, immutable',
'Content-Type': 'application/zip',
'Content-Disposition': `attachment; filename="${version}.zip"`,
// Digest: `sha-256=${fileInfo.hashValue}`,
Link: `<${fileInfo.url}>;type="application/zip"`,
})
ctx.body = fileInfo.fileStream
}
export const getIdentifiers = function (ctx: Context) {
ctx.set({
'Content-Type': 'application/json',
})
ctx.body = {
identifiers: fetchIdentifiers(ctx.query.url),
}
}