Files
oikos/web/node_modules/enhanced-resolve/lib/util/fs.js
dtoro d4d99a7473
Some checks failed
ci / build-test (push) Has been cancelled
ci / docker-build (push) Has been cancelled
feat: Phase 1 — extract the client (web SPA + desktop) to dtoro/oikos-web
Problem: the hexagonal refactor churns the backend tree for nine more
phases; the UI delivery stack (web/ SPA, cmd/desktop Wails wrapper,
compose/web image) must move to its own repo first so doc/layout
rewrites land once on a backend-only tree.

Change:
- New repo git.hubris.network/dtoro/oikos-web (v0.33.0): web/, desktop/
  (updateURL repointed to oikos-web releases), compose/, own CI (web +
  desktop jobs), own deploy script (CI-green gate, TOCTOU guard,
  version-tagged images, prune-to-3), own webhook receiver on :9798 +
  launchd unit, own compose project publishing the same 8091:80.
- Cutover executed on mac-mini in order: oikos stack's web service
  stopped+removed, oikos-web project brought up on 8091; outer Caddy
  untouched (targets the published port) — serving + Authentik flow +
  /wails 404 quirk verified post-cutover.
- Stripped from oikos: web/, cmd/desktop/, compose/web/, desktop CI
  workflow, ci.yml web job, Makefile ui/desktop/desktop-package/install
  targets, the compose web service, oikos-web from deploy.sh's fallback
  prune list; wails + go-keyring dropped from go.mod, vendor synced.
- README / CONTRIBUTING / AGENTS.md / .agents dev+operations docs now
  point at the new repo; mbse + mascot design docs carry a path note.

Risk: production SPA serving depends on the new pipeline now; rollback
is versioned-image re-up of the old web service from a pre-split
checkout (port 8091). Desktop builds installed before the split still
check dtoro/oikos releases — one manual reinstall, noted in the
oikos-web release notes.

Verification: go vet, make test (race), make generate-check, golangci
(no new findings; baseline down 400→365); post-cutover curls —
localhost:8091 200, /wails/runtime.js 404, outer Caddy 302 Authentik.
2026-08-15 22:27:52 +02:00

107 lines
3.6 KiB
JavaScript

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Natsu @xiaoxiaojx
*/
"use strict";
const memoize = require("./memoize");
const stripJsonComments = require("./strip-json-comments");
/** @typedef {import("../Resolver").FileSystem} FileSystem */
/** @typedef {import("../Resolver").JsonObject} JsonObject */
/**
* @typedef {object} ReadJsonOptions
* @property {boolean=} stripComments Whether to strip JSONC comments
*/
/** @type {WeakMap<Buffer | Uint8Array, JsonObject>} */
const _stripCommentsCache = new WeakMap();
// Only constructed for non-Buffer input: on Node the `Buffer.isBuffer` branch
// in `decodeText` handles decoding, so the global `TextDecoder` (Node 11+,
// always present in browsers/Deno/Bun) is only reached off the Buffer path.
// `ignoreBOM: true` keeps a leading BOM in the output, matching
// `Buffer.toString("utf8")` so both decode paths behave identically.
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const getDecoder = memoize(() => new TextDecoder("utf-8", { ignoreBOM: true }));
/**
* Decode a file's raw contents to text without assuming a Node runtime. A
* `Buffer` (Node) uses its fast native `toString`; any other binary input
* (`Uint8Array` from a browser/Deno/Bun file system) goes through
* `TextDecoder`, and strings are returned as-is.
* @param {string | Buffer | Uint8Array} data raw file contents
* @returns {string} decoded text
*/
const decodeText = (data) => {
if (typeof data === "string") return data;
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
return data.toString("utf8");
}
return getDecoder().decode(data);
};
/**
* Read and parse JSON file (supports JSONC with comments).
* Callback-based so a synchronous `fileSystem` stays synchronous all the
* way through — Promise wrapping would defer resolution by a Promise tick
* and break `resolveSync` when `tsconfig` is used together with
* `useSyncFileSystemCalls: true`.
* @param {FileSystem} fileSystem the file system
* @param {string} jsonFilePath absolute path to JSON file
* @param {ReadJsonOptions} options Options
* @param {(err: NodeJS.ErrnoException | Error | null, content?: JsonObject) => void} callback callback
* @returns {void}
*/
function readJson(fileSystem, jsonFilePath, options, callback) {
const { stripComments = false } = options;
const { readJson: fsReadJson } = fileSystem;
if (fsReadJson && !stripComments) {
fsReadJson(jsonFilePath, (err, content) => {
if (err) return callback(err);
callback(null, /** @type {JsonObject} */ (content));
});
return;
}
fileSystem.readFile(jsonFilePath, (err, data) => {
if (err) return callback(err);
const buf = /** @type {Buffer | Uint8Array | string} */ (data);
// The strip-comments cache is keyed by the file-contents object; a file
// system may hand back a plain string, which cannot be a WeakMap key, so
// only cache when the contents are an object.
const cacheable = stripComments && typeof buf === "object";
if (cacheable) {
const cached = _stripCommentsCache.get(buf);
if (cached !== undefined) return callback(null, cached);
}
let result;
try {
const jsonText = decodeText(buf);
const jsonWithoutComments = stripComments
? stripJsonComments(jsonText, {
trailingCommas: true,
whitespace: true,
})
: jsonText;
result = JSON.parse(jsonWithoutComments);
} catch (parseErr) {
return callback(/** @type {Error} */ (parseErr));
}
if (cacheable) {
_stripCommentsCache.set(buf, result);
}
callback(null, result);
});
}
module.exports.decodeText = decodeText;
module.exports.readJson = readJson;