chore: pointer info building & formatting helpers
This commit is contained in:
parent
dd28da28dd
commit
2f80d3a3cf
2 changed files with 33 additions and 0 deletions
|
@ -1,4 +1,8 @@
|
|||
import path from 'path';
|
||||
import { toHex } from './util';
|
||||
|
||||
|
||||
const SPEC_URL = 'https://git-lfs.github.com/spec/v1';
|
||||
|
||||
|
||||
export interface PointerInfo {
|
||||
|
@ -58,3 +62,21 @@ export function readPointer({ dir, gitdir = path.join(dir, '.git'), content }: P
|
|||
|
||||
return { info, objectPath };
|
||||
}
|
||||
|
||||
|
||||
/** Formats given PointerInfo for writing in Git tree. */
|
||||
export function formatPointerInfo(info: PointerInfo): Buffer {
|
||||
const lines = [
|
||||
`version ${SPEC_URL}`,
|
||||
`oid sha256:${info.oid}`,
|
||||
`size ${info.size}`,
|
||||
];
|
||||
return Buffer.from(lines.join('\n'));
|
||||
}
|
||||
|
||||
|
||||
export async function buildPointerInfo(content: Buffer): Promise<PointerInfo> {
|
||||
const size = Buffer.byteLength(content);
|
||||
const hash = await crypto.subtle.digest('SHA-1', content);
|
||||
return { oid: toHex(hash), size };
|
||||
}
|
||||
|
|
11
src/util.ts
11
src/util.ts
|
@ -74,3 +74,14 @@ export async function bodyToBuffer(body: Uint8Array[]): Promise<Buffer> {
|
|||
}
|
||||
return Buffer.from(result.buffer);
|
||||
}
|
||||
|
||||
|
||||
// Borrowed from Isomorphic Git core, it is not importable.
|
||||
export function toHex(buffer: ArrayBuffer): string {
|
||||
let hex = ''
|
||||
for (const byte of new Uint8Array(buffer)) {
|
||||
if (byte < 16) hex += '0'
|
||||
hex += byte.toString(16)
|
||||
}
|
||||
return hex
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue