Skip to content

Commit

Permalink
Added BSON support
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Jan 23, 2022
1 parent aaaa352 commit 9394a8e
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ This will give you access to `to-json`, `from-json`, `combine` and `separate`
`cat level8.dlf.unpacked | to-yaml --ext=dlf > level8.dlf.yml`

`cat level8.dlf.yml | from-yaml --ext=dlf > level8.dlf.unpacked`

`cat level8.dlf.unpacked | to-bson --ext=dlf > level8.dlf.bson`

`cat level8.dlf.bson | from-bson --ext=dlf > level8.dlf.unpacked`
80 changes: 80 additions & 0 deletions bin/from-bson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env node

const fs = require("fs");
const minimist = require("minimist-lite");
const BSON = require("bson");
const { DLF, FTS, LLF } = require("../src/index.js");
const {
fileExists,
getPackageVersion,
streamToBuffer,
outputInChunks,
} = require("./helpers.js");
const { SUPPORTED_EXTENSIONS } = require("./constants.js");

const args = minimist(process.argv.slice(2), {
string: ["output"],
boolean: ["version"],
alias: {
v: "version",
},
});

(async () => {
if (args.version) {
console.log(getPackageVersion());
process.exit(0);
}

let filename = args._[0];
let output = args.output;

let hasErrors = false;

let input;
if (filename) {
if (await fileExists(filename)) {
input = fs.createReadStream(filename);
} else {
console.error("error: input file does not exist");
hasErrors = true;
}
} else {
input = process.openStdin();
}

if (output) {
output = fs.createWriteStream(output);
} else {
output = process.stdout;
}

const json = BSON.deserialize(await streamToBuffer(input));
const extension = json.meta.type;

if (!SUPPORTED_EXTENSIONS.includes(extension)) {
console.error(
'error: unsupported meta type, expected "dlf", "fts" or "llf"'
);
hasErrors = true;
}

if (hasErrors) {
process.exit(1);
}

let binary;
switch (extension) {
case "fts":
binary = FTS.save(json);
break;
case "dlf":
binary = DLF.save(json);
break;
case "llf":
binary = LLF.save(json);
break;
}

outputInChunks(binary, output);
})();
8 changes: 7 additions & 1 deletion bin/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const fs = require("fs");
const YAML = require("yaml");

const fileExists = async (filename) => {
try {
Expand Down Expand Up @@ -42,9 +41,15 @@ const stringifyJSON = (json, prettify = false) => {
};

const stringifyYAML = (json) => {
const YAML = require("yaml");
return YAML.stringify(json);
};

const stringifyBSON = (json) => {
const BSON = require("bson");
return BSON.serialize(json);
};

const outputInChunks = (buffer, stream) => {
const chunks = Math.ceil(buffer.length / 1000);
for (let i = 0; i < chunks - 1; i++) {
Expand All @@ -60,5 +65,6 @@ module.exports = {
streamToBuffer,
stringifyJSON,
stringifyYAML,
stringifyBSON,
outputInChunks,
};
84 changes: 84 additions & 0 deletions bin/to-bson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env node

const fs = require("fs");
const minimist = require("minimist-lite");
const { DLF, FTS, LLF } = require("../src/index.js");
const {
fileExists,
getPackageVersion,
streamToBuffer,
stringifyBSON,
outputInChunks,
} = require("./helpers.js");
const { SUPPORTED_EXTENSIONS } = require("./constants.js");

const args = minimist(process.argv.slice(2), {
string: ["output", "ext"],
boolean: ["version", "pretty"],
alias: {
v: "version",
p: "pretty",
},
});

(async () => {
if (args.version) {
console.log(getPackageVersion());
process.exit(0);
}

let filename = args._[0];
let extension = args.ext ? args.ext.toLowerCase() : "";
let output = args.output;

let hasErrors = false;

let input;
if (filename) {
if (await fileExists(filename)) {
input = fs.createReadStream(filename);
if (!extension) {
extension = filename.match(/\.([a-zA-Z]+)$/)[1].toLowerCase();
}
} else {
console.error("error: input file does not exist");
hasErrors = true;
}
} else {
input = process.openStdin();
}

if (!SUPPORTED_EXTENSIONS.includes(extension)) {
console.error(
'error: unsupported extension, expected "dlf", "fts" or "llf"'
);
hasErrors = true;
}

if (output) {
output = fs.createWriteStream(output);
} else {
output = process.stdout;
}

if (hasErrors) {
process.exit(1);
}

const raw = await streamToBuffer(input);

let json;
switch (extension) {
case "fts":
json = FTS.load(raw);
break;
case "dlf":
json = DLF.load(raw);
break;
case "llf":
json = LLF.load(raw);
break;
}

outputInChunks(stringifyBSON(json), output);
})();
104 changes: 103 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"combine": "bin/combine.js",
"separate": "bin/separate.js",
"to-yaml": "bin/to-yaml.js",
"from-yaml": "bin/from-yaml.js"
"from-yaml": "bin/from-yaml.js",
"to-bson": "bin/to-bson.js",
"from-bson": "bin/from-bson.js"
},
"scripts": {},
"repository": {
Expand All @@ -23,6 +25,7 @@
},
"homepage": "https://github.com/meszaros-lajos-gyorgy/arx-level-json-converter#readme",
"dependencies": {
"bson": "^4.6.1",
"minimist-lite": "^2.0.0",
"ramda": "^0.27.1",
"yaml": "^1.10.2"
Expand Down

0 comments on commit 9394a8e

Please sign in to comment.