mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
abstract away some code
This commit is contained in:
1 parent
dafa834d28
commit
ea21636c4e
5 files changed
+109
-65
No files matched your search
@@ -0,0 +1,58 @@
|
||||
import { $ } from "bun";
|
||||
import { mkdir, rm } from "node:fs/promises";
|
||||
|
||||
export class GitObjectStorage {
|
||||
constructor(
|
||||
private repo: string,
|
||||
private base: string,
|
||||
private token?: string,
|
||||
) {}
|
||||
|
||||
async init() {
|
||||
await rm(this.base, { recursive: true, force: true });
|
||||
|
||||
console.log(`Cloning ${this.repo} to ${this.base}`);
|
||||
|
||||
const url = this.token
|
||||
? `https://x-access-token:${this.token}@github.com/${this.repo}.git`
|
||||
: `https://github.com/${this.repo}.git`;
|
||||
|
||||
await $`git clone --depth 1 ${url} ${this.base}`;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
async commit() {
|
||||
const shell = $.cwd(this.base);
|
||||
|
||||
await shell`git add .`.quiet();
|
||||
await shell`git commit --amend --reset-author -m "ids update ${new Date().toISOString()}"`.quiet();
|
||||
await shell`git push --force-with-lease`;
|
||||
}
|
||||
|
||||
async mkdir(path: string) {
|
||||
await mkdir(`${this.base}/${path}`, { recursive: true });
|
||||
}
|
||||
|
||||
async rm(path: string) {
|
||||
await rm(`${this.base}/${path}`, { force: true });
|
||||
}
|
||||
|
||||
async write(path: string, bytes: Uint8Array | string) {
|
||||
await Bun.write(`${this.base}/${path}`, bytes);
|
||||
}
|
||||
|
||||
file(path: string) {
|
||||
return Bun.file(path);
|
||||
}
|
||||
|
||||
async bytes(path: string) {
|
||||
const file = this.file(path);
|
||||
return await file.bytes();
|
||||
}
|
||||
|
||||
async json<Type>(path: string) {
|
||||
const file = this.file(path);
|
||||
return (await file.json()) as Type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
export class IdArray {
|
||||
constructor(
|
||||
private array: Uint32Array = new Uint32Array(),
|
||||
private _size = array.length,
|
||||
) {}
|
||||
|
||||
get(index: number) {
|
||||
return this.array[index];
|
||||
}
|
||||
|
||||
push(id: number) {
|
||||
if (this._size === this.array.length) {
|
||||
const bigger = new Uint32Array(
|
||||
this.array.length === 0 ? 1 : this.array.length * 2,
|
||||
);
|
||||
|
||||
bigger.set(this.array);
|
||||
this.array = bigger;
|
||||
}
|
||||
|
||||
this.array[this._size++] = id;
|
||||
}
|
||||
|
||||
sort() {
|
||||
this.array.subarray(0, this._size).sort();
|
||||
}
|
||||
|
||||
size() {
|
||||
return this._size;
|
||||
}
|
||||
|
||||
toBytes() {
|
||||
return new Uint8Array(
|
||||
this.array.buffer,
|
||||
this.array.byteOffset,
|
||||
this._size * Uint32Array.BYTES_PER_ELEMENT,
|
||||
);
|
||||
}
|
||||
|
||||
toArray() {
|
||||
return Array.from(this.array.subarray(0, this._size));
|
||||
}
|
||||
|
||||
static fromBytes(bytes: Uint8Array) {
|
||||
if (bytes.byteLength % Uint32Array.BYTES_PER_ELEMENT !== 0) {
|
||||
throw new Error("Misaligned bytes");
|
||||
}
|
||||
|
||||
const size = bytes.byteLength / Uint32Array.BYTES_PER_ELEMENT;
|
||||
const capacity = this.nextPowerOf2(size);
|
||||
|
||||
const values = new Uint32Array(bytes.buffer, bytes.byteOffset, size);
|
||||
|
||||
const uintArray = new Uint32Array(capacity);
|
||||
uintArray.set(values);
|
||||
|
||||
return new IdArray(uintArray, size);
|
||||
}
|
||||
|
||||
static fromArray(array: number[]) {
|
||||
const uintArray = new Uint32Array(this.nextPowerOf2(array.length));
|
||||
|
||||
uintArray.set(array);
|
||||
|
||||
return new IdArray(uintArray, array.length);
|
||||
}
|
||||
|
||||
private static nextPowerOf2(value: number): number {
|
||||
if (value <= 1) return 1;
|
||||
return 2 ** Math.ceil(Math.log2(value));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user