Initial work of ESM transformation

This commit is contained in:
be5invis 2022-07-16 19:26:49 -07:00
parent 2472c9cff2
commit b8205a63aa
303 changed files with 1959 additions and 2450 deletions

View file

@ -1,19 +1,15 @@
"use strict";
const fs = require("fs");
const zlib = require("zlib");
const { encode, decode } = require("@msgpack/msgpack");
import fs from "fs";
import zlib from "zlib";
import { encode, decode } from "@msgpack/msgpack";
const Edition = 20;
const MAX_AGE = 16;
class GfEntry {
constructor(age, value) {
this.age = age;
this.value = value;
}
}
class Cache {
constructor(freshAgeKey) {
this.freshAgeKey = freshAgeKey;
@ -40,21 +36,18 @@ class Cache {
this.historyAgeKeys[0] === this.freshAgeKey
? this.historyAgeKeys
: [this.freshAgeKey, ...this.historyAgeKeys];
return {
version: version + "@" + Edition,
ageKeys: mergedAgeKeys,
gf: gfRep
};
}
isEmpty() {
return this.gf.size == 0;
}
isUpdated() {
return this.diff.size != 0;
}
// Geometry flattening conversion cache
getGF(k) {
const entry = this.gf.get(k);
@ -79,8 +72,7 @@ class Cache {
}
}
}
exports.load = async function (path, version, freshAgeKey) {
export const load = async function (path, version, freshAgeKey) {
let cache = new Cache(freshAgeKey);
if (path && fs.existsSync(path)) {
const buf = zlib.gunzipSync(await fs.promises.readFile(path));
@ -88,21 +80,19 @@ exports.load = async function (path, version, freshAgeKey) {
}
return cache;
};
exports.save = async function savePTCache(path, version, cache, diffOnly) {
export const save = async function savePTCache(path, version, cache, diffOnly) {
if (path) {
const buf = encode(cache.toRep(version, diffOnly));
const bufZip = zlib.gzipSync(Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength));
await fs.promises.writeFile(path, bufZip);
}
};
exports.merge = async function (base, diff, version, freshAgeKey) {
const cacheDiff = await exports.load(diff, version, freshAgeKey);
export const merge = async function (base, diff, version, freshAgeKey) {
const cacheDiff = await load(diff, version, freshAgeKey);
if (!cacheDiff.isEmpty()) {
const cacheBase = await exports.load(base, version, freshAgeKey);
const cacheBase = await load(base, version, freshAgeKey);
cacheBase.merge(cacheDiff);
await exports.save(base, version, cacheBase, false);
await save(base, version, cacheBase, false);
}
if (fs.existsSync(diff)) await fs.promises.rm(diff);
};