Skip to content

src/github-paging.ts

24 lines · 920 B · TypeScript
Raw
const MAX_GITHUB_REQUEST_MS = 15_000
export const MIN_GITHUB_REQUEST_MS = 2_000

/**
 * Time left before `deadline`, capped per GitHub request so one hung fetch
 * cannot eat the whole 90s sandbox budget. Returns null when too little time
 * remains to start another request.
 */
export function requestTimeoutWithin(deadline: number | undefined, now = Date.now()) {
	if (deadline === undefined) return MAX_GITHUB_REQUEST_MS
	const remaining = deadline - now
	if (remaining < MIN_GITHUB_REQUEST_MS) return null
	return Math.min(MAX_GITHUB_REQUEST_MS, remaining)
}

/** The `rel="next"` URL from a GitHub REST Link header, or null on the last page. */
export function nextLinkFrom(linkHeader: string | undefined | null): string | null {
	if (!linkHeader) return null
	for (const part of linkHeader.split(',')) {
		const match = part.match(/<([^>]+)>;\s*rel="next"/)
		if (match) return match[1] ?? null
	}
	return null
}