Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up and return next/prev/first/last Link headers #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions controllers/package.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import { Context } from 'koa'
import { downloadZipFile, fetchIdentifiers, fetchManifest, fetchTags, fetchZipHash } from '../services/githubService'
import { IGithubTag, IPackageReleases } from '../services/types'
import { IGithubTag, IPackageReleases, IFetchTagsInfo } from '../services/types'
import { parseRequestArgs } from './utils'
var parse = require('parse-link-header')

export const listPackages = async function (ctx: Context) {
const pageQuery = ctx.query['page'] || '1'
const pageParam: string = Array.isArray(pageQuery) ? pageQuery[0] : pageQuery
const perPageQuery = ctx.query['per_page'] || '100'
const perPageParam: string = Array.isArray(perPageQuery) ? perPageQuery[0] : perPageQuery
const { scope, pkg } = parseRequestArgs(ctx)
const tags = await fetchTags(scope, pkg)
const tagsInfo = await fetchTags(scope, pkg, pageParam, perPageParam)
const tags = tagsInfo.tags
const linkHeader = tagsInfo.linkHeader

ctx.set('Content-Type', 'application/json')
if (tags.length > 0) {

if (tags.length > 0 || linkHeader.length > 0) {
const latestVersion = tags[0].name
const links = [
var 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"`,
]

if (linkHeader.length > 0) {
var parsedLinkHeader = parse(linkHeader)
Object.keys(parsedLinkHeader).forEach((key) => {
var linkObj = parsedLinkHeader[key]
var parsedLinkURL = new URL(linkObj['url'])
const page = parsedLinkURL.searchParams.get('page') || '1'
const perPage = parsedLinkURL.searchParams.get('per_page') || '100'
const link = `<${ctx.protocol}://${ctx.host}/${scope}/${pkg}?per_page=${perPage}&page=${page}>; rel="${key}"`
links.push(link)
})
}

ctx.append('Link', links.join(','))
}

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@
"jest": "^29.3.1",
"lint-staged": "^13.0.3",
"prettier": "^2.7.1",
"parse-link-header": "^2.0.0",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.3"
},
6,178 changes: 3,467 additions & 2,711 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions services/githubService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import fetch, { BodyInit, HeadersInit, RequestInfo } from 'node-fetch'
import { IFileInfo, IGithubTag, IZipBarFile } from './types'
import { IFileInfo, IGithubTag, IZipBarFile, IFetchTagsInfo } from './types'
import { GITHUB_ACCESS_TOKEN } from '../shared/config'
import * as crypto from 'crypto'
import { HTTPResponseError } from '../shared/responseErrors'

export const fetchTags = async function (scope: string, pkg: string): Promise<[IGithubTag]> {
const githubTags = `https://api.github.com/repos/${scope}/${pkg}/tags`
export const fetchTags = async function (
scope: string,
pkg: string,
page: string,
perPage: string,
): Promise<IFetchTagsInfo> {
const githubTags = `https://api.github.com/repos/${scope}/${pkg}/tags?per_page=${perPage}&page=${page}`
const response = await authFetch(githubTags)
return await response.json()
return {
tags: await response.json(),
linkHeader: response.headers.get('Link') || '',
}
}

export const fetchZipHash = async function (scope: string, pkg: string, version: string): Promise<string> {
5 changes: 5 additions & 0 deletions services/types.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@ export interface IGithubTag {
name: string
}

export interface IFetchTagsInfo {
tags: [IGithubTag]
linkHeader: string
}

export interface IFileInfo {
content: string
}