refactor: split out a getAuthHeader() util

This commit is contained in:
Anton Strogonoff 2021-11-30 21:53:50 +01:00
parent 17b2eb8706
commit a71d6dc1f4
2 changed files with 15 additions and 5 deletions

View file

@ -1,7 +1,7 @@
import path from 'path';
import fsp from 'fs/promises';
import { bodyToBuffer, isWriteable } from './util';
import { bodyToBuffer, getAuthHeader, isWriteable } from './util';
import { Pointer } from './pointers';
import { HTTPRequest } from './types';
@ -31,10 +31,7 @@ export default async function downloadBlobFromPointer(
): Promise<Buffer> {
const authHeaders: Record<string, string> = auth
? {
'Authorization':
`Basic ${Buffer.from(`${auth.username}:${auth.password}`).toString('base64')}`,
}
? getAuthHeader(auth)
: {};
// Request LFS metadata

View file

@ -1,5 +1,6 @@
import fs from 'fs/promises';
import { constants as fsConstants } from 'fs';
import { BasicAuth } from './types';
export const LFS_POINTER_PREAMBLE = 'version https://git-lfs.github.com/spec/v1\n';
@ -12,6 +13,18 @@ export function pointsToLFS(content: Buffer): boolean {
}
/**
* Returns properly encoded HTTP Basic auth header,
* given basic auth credentials.
*/
export function getAuthHeader(auth: BasicAuth): Record<string, string> {
return {
'Authorization':
`Basic ${Buffer.from(`${auth.username}:${auth.password}`).toString('base64')}`,
};
}
/**
* Returns true if given path is available for writing,
* regardless of whether or not it is occupied.