@k8ts/instruments
Version:
A collection of utilities and core components for k8ts.
105 lines • 3.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalFileSource = void 0;
exports.localFile = localFile;
exports.isTypedArray = isTypedArray;
exports.isArrayBuffer = isArrayBuffer;
exports.resolveText = resolveText;
exports.resolveBinary = resolveBinary;
const doddle_1 = require("doddle");
const promises_1 = require("fs/promises");
const immutable_1 = require("immutable");
const path_1 = require("path");
const stacktracey_1 = __importDefault(require("stacktracey"));
const error_1 = require("../error");
class LocalFileSource {
_props;
constructor(_props) {
this._props = _props;
}
get cwd() {
if (this._props.cwd) {
return this._props.cwd;
}
const where = this._props.pointOfCall.at(0);
return (0, path_1.dirname)(where.file);
}
get path() {
const where = this._props.pointOfCall.at(0);
const joined = (0, path_1.join)((0, path_1.dirname)(where.file), this._props.path);
return (0, path_1.resolve)(joined);
}
contents = (0, doddle_1.doddle)(async () => {
const mode = this._props.mode ?? "text";
if (mode === "binary") {
const data = await (0, promises_1.readFile)(this.path, null);
return data;
}
return (0, promises_1.readFile)(this.path, "utf-8");
});
}
exports.LocalFileSource = LocalFileSource;
function localFile(args, ...params) {
if (typeof args === "string") {
const options = params[0] ?? {};
return new LocalFileSource({
pointOfCall: new stacktracey_1.default().slice(1),
path: args,
...(options ?? {})
});
}
const path = String.raw(args, ...params);
return new LocalFileSource({
pointOfCall: new stacktracey_1.default().slice(1),
path
});
}
function isTypedArray(data) {
return (typeof data === "object" &&
"buffer" in data &&
"byteSize" in data &&
typeof data.byteSize === "number");
}
function isArrayBuffer(data) {
return data instanceof ArrayBuffer;
}
async function promiseAllMap(m) {
const promises = m.toArray().map(async (x) => [x[0], await x[1]]);
const res = await Promise.all(promises);
return (0, immutable_1.Map)(res);
}
async function resolveText(record) {
const mp = (0, immutable_1.Map)(record).map(async (v, k) => {
let resolved = v;
if (v instanceof LocalFileSource) {
resolved = await v.contents.pull();
}
if (typeof resolved !== "string") {
throw new error_1.InstrumentsError(`Got an invalid data value ${v} for key ${k}. Must be a string.`);
}
return resolved;
});
return promiseAllMap(mp);
}
async function resolveBinary(record) {
const mp = (0, immutable_1.Map)(record).map(async (v, k) => {
let resolved = v;
if (v instanceof LocalFileSource) {
resolved = await v.contents.pull();
}
if (isTypedArray(resolved)) {
return new Uint8Array(resolved.buffer);
}
else if (isArrayBuffer(resolved)) {
return new Uint8Array(resolved);
}
else {
throw new error_1.InstrumentsError(`Got an invalid data value ${v} for key ${k}. Must be binary data.`);
}
});
return promiseAllMap(mp);
}
//# sourceMappingURL=index.js.map