@webda/core
Version:
Expose API with Lambda
313 lines • 11 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { suite, test } from "@testdeck/mocha";
import * as assert from "assert";
import { createReadStream, createWriteStream, existsSync, readFileSync, symlinkSync, unlinkSync, writeFileSync } from "fs";
import * as path from "path";
import { pipeline } from "stream/promises.js";
import { fileURLToPath } from "url";
import { gunzipSync } from "zlib";
import { FileUtils, JSONUtils, NDJSonReader as NDJSONReader, NDJSONStream, YAMLUtils } from "./serializers.js";
const TEST_FOLDER = path.dirname(fileURLToPath(import.meta.url)) + "/../../test/jsonutils/";
let UtilsTest = class UtilsTest {
fileJson() {
assert.deepStrictEqual(JSONUtils.loadFile(TEST_FOLDER + "test.json"), {
test: "ok"
});
assert.deepStrictEqual(YAMLUtils.loadFile(TEST_FOLDER + "test.json"), {
test: "ok"
});
assert.deepStrictEqual(FileUtils.load(TEST_FOLDER + "test.json"), {
test: "ok"
});
assert.throws(() => JSONUtils.loadFile("/none"), /File '\/none' does not exist/);
assert.throws(() => JSONUtils.loadFile("./Dockerfile"), /SyntaxError/);
assert.throws(() => FileUtils.load("./Dockerfile"), /Unknown format/);
}
fileYml() {
assert.deepStrictEqual(YAMLUtils.loadFile(TEST_FOLDER + "test.yml"), {
test: { ok: "plop" },
tab: ["ok", "item2"]
});
}
fileYaml() {
assert.strictEqual(FileUtils.load(TEST_FOLDER + "mdocs.yaml").length, 2);
}
circularJSON() {
let a = {
b: "test",
c: {},
__test: true,
n: null
};
a.c.a = a;
assert.deepStrictEqual(JSONUtils.stringify(a), JSON.stringify({ b: "test", c: {}, __test: true }, undefined, 2));
assert.deepStrictEqual(JSONUtils.safeStringify(a), JSON.stringify({ b: "test", c: {}, __test: true }, undefined, 2));
assert.deepStrictEqual(JSONUtils.stringify(a, (key, value) => {
if (key !== "b" && key !== "n")
return value;
return undefined;
}, 3, true), JSON.stringify({ c: {} }, undefined, 3));
}
duplicatedJSON() {
let a = {
b: "test",
c: {
plop: "bouzouf"
},
__test: true
};
a.d = a.c;
assert.deepStrictEqual(JSONUtils.stringify(a), JSON.stringify({
b: "test",
c: { plop: "bouzouf" },
__test: true,
d: { plop: "bouzouf" }
}, undefined, 2));
}
sortObject() {
assert.deepStrictEqual(JSONUtils.sortObject({ a: 1, c: 2, b: 3 }), { a: 1, b: 3, c: 2 });
assert.deepStrictEqual(JSONUtils.sortObject({ a: 1, c: 2, b: 3 }, p => (p < 3 ? p + 2 : undefined)), { a: 3, c: 4 });
}
writeJSON() {
try {
let file = path.join(TEST_FOLDER, "writeTest.json");
JSONUtils.saveFile({ test: "plop" }, file);
assert.strictEqual(readFileSync(file).toString(), '{\n "test": "plop"\n}');
file = path.join(TEST_FOLDER, "writeTest.yaml");
FileUtils.save({ test: "plop" }, file);
assert.strictEqual(readFileSync(file).toString(), `test: plop\n`);
// Yaml alias
file = path.join(TEST_FOLDER, "writeTest.json");
YAMLUtils.saveFile({ test: "plop" }, file);
assert.strictEqual(readFileSync(file).toString(), `test: plop\n`);
// True implem
file = path.join(TEST_FOLDER, "writeTest.json");
FileUtils.save({ test: "plop" }, file);
assert.strictEqual(readFileSync(file).toString(), '{\n "test": "plop"\n}');
// Gzip
file = path.join(TEST_FOLDER, "writeTest.json.gz");
FileUtils.save({ test: "plop" }, file);
const buf = readFileSync(file);
assert.strictEqual(buf[0], 0x1f);
assert.strictEqual(buf[1], 0x8b);
assert.deepStrictEqual(JSON.parse(gunzipSync(buf).toString()), { test: "plop" });
assert.deepStrictEqual(FileUtils.load(file), { test: "plop" });
assert.throws(() => FileUtils.save({}, "./Dockerfile.zzz"), /Unknown format/);
}
finally {
FileUtils.clean("test/jsonutils/writeTest.json.gz", "test/jsonutils/writeTest.json", "test/jsonutils/writeTest.yaml");
}
}
async ndjsonStream() {
try {
const data = [{ id: 1 }, { id: 2 }, { id: 3 }];
const file = ".test.ndjson";
await pipeline(new NDJSONStream(data), createWriteStream(file));
let counter = 0;
await pipeline(createReadStream(file), new NDJSONReader().on("data", (d) => {
counter += d.id;
}));
assert.strictEqual(counter, 6);
}
finally {
if (existsSync(".test.ndjson")) {
unlinkSync(".test.ndjson");
}
}
}
duplicate() {
let obj = { plop: "test" };
let obj2 = JSONUtils.duplicate(obj);
assert.notStrictEqual(obj, obj2);
assert.deepStrictEqual(obj, obj2);
obj2 = JSONUtils.parse('{"plop": "test"}');
assert.deepStrictEqual(obj, obj2);
}
audience() {
assert.strictEqual(JSONUtils.stringify({ __dirname: "plop", me: null, test: "plop" }, undefined, 2, true), '{\n "test": "plop"\n}');
assert.throws(() => JSONUtils.stringify({ test: true }, () => {
throw new Error("BOUZOUF");
}), /BOUZOUF/);
}
parse() {
let res = YAMLUtils.parse(`
apiVersion: v1
kind: Service
---
apiVersion: v1
kind: Pod
`);
assert.deepStrictEqual(res, [
{ apiVersion: "v1", kind: "Service" },
{ apiVersion: "v1", kind: "Pod" }
]);
res = YAMLUtils.parse(`
apiVersion: v1
kind: Service
`);
assert.deepStrictEqual(res, { apiVersion: "v1", kind: "Service" });
}
octal() {
assert.deepEqual(YAMLUtils.parse(`
plop: 0o600
`), { plop: 384 });
}
yaml() {
assert.strictEqual(YAMLUtils.stringify({ plop: "test" }), `plop: test
`);
assert.deepEqual(YAMLUtils.parse(`
plop: test
`), { plop: "test" });
}
walker() {
try {
let res = [];
if (!existsSync("./test/link")) {
symlinkSync("../templates", "test/link");
}
try {
if (!existsSync("./test/badlink")) {
symlinkSync("../non-existing", "test/badlink");
}
}
catch (err) { }
FileUtils.walk("test", f => res.push(f));
assert.ok(["test/models/ident.js", "test/jsonutils/mdocs.yaml", "test/data/test.png"]
.map(c => res.includes(c))
.reduce((v, c) => v && c, true));
res = [];
FileUtils.walk("test", f => res.push(f), {
includeDir: true,
followSymlinks: true
});
assert.ok(res.filter(r => r.includes("PASSPORT_EMAIL_RECOVERY")).length > 0);
}
finally {
FileUtils.clean("test/link");
}
}
finder() {
let res = FileUtils.find("test", { filterPattern: /Dockerfile/ });
assert.ok(res.includes("test/Dockerfile"));
}
async streams() {
const st = FileUtils.getWriteStream("/tmp/webda.stream");
let p = new Promise(resolve => st.on("finish", resolve));
st.end();
await p;
FileUtils.getReadStream("/tmp/webda.stream");
}
async jsoncUpdateFile() {
const file = "/tmp/webda.jsonc";
const JSONC_SOURCE = `{
"test": "plop",
// Comment one
"test3": {
"bouzoufr": "plop"
},
"test4": {
"array": [
"plop",
"plop2",
{"id": "plop3"}
],
"p": {
"v": "bouzouf"
}, /* c, */
"remove": true /* c */
},
"test2": {
"plop3": "bouzouf", // Comment on 3
/* */ /* */ /* */ /* */ /* */ /* */
/*
Test of , comments
*/
"plop2": "bouzouf", // Comment two with a , for fun
/**
* another , one
* */
"plop4": "bouzouf"
},
"removed": true
}`;
try {
writeFileSync(file, JSONC_SOURCE);
await JSONUtils.updateFile(file, v => {
if (v === "bouzouf") {
return "bouzouf2";
}
return v;
});
const data = FileUtils.load(file);
assert.strictEqual(data.test4.p.v, "bouzouf2");
assert.strictEqual(data.test2.plop4, "bouzouf2");
assert.strictEqual(data.test2.plop2, "bouzouf2");
assert.strictEqual(data.test2.plop3, "bouzouf2");
const update = readFileSync(file).toString();
assert.strictEqual(update.length, JSONC_SOURCE.length + 4);
}
finally {
FileUtils.clean(file);
}
}
};
__decorate([
test("LoadJSON File")
], UtilsTest.prototype, "fileJson", null);
__decorate([
test("LoadYAML File")
], UtilsTest.prototype, "fileYml", null);
__decorate([
test("LoadYAML Multiple Docs File")
], UtilsTest.prototype, "fileYaml", null);
__decorate([
test("CircularJSON")
], UtilsTest.prototype, "circularJSON", null);
__decorate([
test("DuplicateJSON")
], UtilsTest.prototype, "duplicatedJSON", null);
__decorate([
test
], UtilsTest.prototype, "sortObject", null);
__decorate([
test("Write JSON/YAML")
], UtilsTest.prototype, "writeJSON", null);
__decorate([
test
], UtilsTest.prototype, "ndjsonStream", null);
__decorate([
test
], UtilsTest.prototype, "duplicate", null);
__decorate([
test
], UtilsTest.prototype, "audience", null);
__decorate([
test
], UtilsTest.prototype, "parse", null);
__decorate([
test("YAML octal")
], UtilsTest.prototype, "octal", null);
__decorate([
test("YAML stringify")
], UtilsTest.prototype, "yaml", null);
__decorate([
test
], UtilsTest.prototype, "walker", null);
__decorate([
test
], UtilsTest.prototype, "finder", null);
__decorate([
test
], UtilsTest.prototype, "streams", null);
__decorate([
test
], UtilsTest.prototype, "jsoncUpdateFile", null);
UtilsTest = __decorate([
suite
], UtilsTest);
//# sourceMappingURL=serializers.spec.js.map