UNPKG

git-cat-file

Version:

Pure JavaScript `git cat-file -p` for node.js

68 lines (67 loc) 2.01 kB
"use strict"; /** * https://github.com/kawanet/git-cat-file */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Loose = void 0; const fs_1 = require("fs"); const zlib_1 = require("zlib"); const cache_1 = require("./cache"); class Loose { constructor(root) { this.root = root; this.readdir = (0, cache_1.shortCache)((first) => fs_1.promises.readdir(`${this.root}/objects/${first}`)); // } async findAll(object_id) { const first = object_id.slice(0, 2); const rest = object_id.slice(2); const { length } = rest; const files = await this.readdir(first).catch(_ => null); if (!files) return; return files.filter(name => name.slice(0, length) === rest).map(name => (first + name)); } async getObject(oid) { const first = oid.slice(0, 2); const rest = oid.slice(2); const obj = new LooseObject(`${this.root}/objects/${first}/${rest}`); const type = await obj.getType(); const data = await obj.getData(); return { oid, type, data }; } } exports.Loose = Loose; function findZero(buf, offset) { offset |= 0; while (buf[offset++]) { // nop } return offset; } class LooseObject { constructor(path) { this.path = path; // } getRaw() { return this.buf || (this.buf = fs_1.promises.readFile(this.path).then(zlib_1.inflateSync)); } getOffset() { return this.offset || (this.offset = this.getRaw().then(findZero)); } getType() { return this.type || (this.type = this.parseType()); } async parseType() { const raw = await this.getRaw(); const offset = await this.getOffset(); const head = raw.slice(0, offset - 2).toString(); return head.split(/\s+/).shift(); } async getData() { const raw = await this.getRaw(); const offset = await this.getOffset(); return raw.slice(offset); } }