git-cat-file
Version:
Pure JavaScript `git cat-file -p` for node.js
43 lines (42 loc) • 1.41 kB
JavaScript
;
/**
* https://github.com/kawanet/git-cat-file
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pack = void 0;
const fs_1 = require("fs");
const pack_idx_1 = require("./pack-idx");
const pack_obj_1 = require("./pack-obj");
const cache_1 = require("./cache");
class Pack {
constructor(path) {
this.path = path;
this.getIndex = (0, cache_1.longCache)(() => (0, pack_idx_1.readPackIndex)(this.path));
this.getList = (0, cache_1.longCache)(() => this.getIndex().then(index => Object.keys(index).sort()));
//
}
async findAll(object_id) {
const list = await this.getList();
const index = {};
const { length } = object_id;
for (const oid of await list) {
if (oid.slice(0, length) === object_id) {
index[oid] = 1;
}
}
return Object.keys(index);
}
async getObject(object_id, store) {
const index = await this.getIndex();
const offset = index[object_id];
if (!offset)
return;
// console.warn(`open: ${this.path}`);
const fh = await fs_1.promises.open(this.path, "r");
const obj = await (0, pack_obj_1.readPackedObject)(fh, offset, store);
await fh.close();
const { type, data } = obj;
return { oid: object_id, type, data };
}
}
exports.Pack = Pack;