From 7952f7097668bb604502913af49d4d1447b8ea37 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Wed, 6 Feb 2019 11:20:16 -0500 Subject: [PATCH] Attempt to stream JSON --- bin/obj2gltf.js | 40 ++++++++++++++++++++++++++++++++++++---- package.json | 2 ++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/bin/obj2gltf.js b/bin/obj2gltf.js index 47acf97..e5d4439 100644 --- a/bin/obj2gltf.js +++ b/bin/obj2gltf.js @@ -1,5 +1,7 @@ #!/usr/bin/env node 'use strict'; +const json = require('big-json'); +const writer = require('flush-write-stream'); const Cesium = require('cesium'); const fsExtra = require('fs-extra'); const path = require('path'); @@ -178,10 +180,8 @@ obj2gltf(objPath, options) // gltf is a glb buffer return fsExtra.outputFile(gltfPath, gltf); } - const jsonOptions = { - spaces : 2 - }; - return fsExtra.outputJson(gltfPath, gltf, jsonOptions); + + return streamJson(gltfPath, gltf); }) .then(function() { console.timeEnd('Total'); @@ -190,3 +190,35 @@ obj2gltf(objPath, options) console.log(error.message); process.exit(1); }); + +function streamJson(gltfPath, gltf) { + return new Promise(function(resolve, reject) { + const writeStream = fsExtra.createWriteStream(gltfPath); + + const stringifyStream = json.createStringifyStream({ + body: gltf + }); + + stringifyStream.pipe(writeStream); + + writeStream.on('error', function(error) { + reject(error); + }); + + writeStream.on('close', function() { + resolve(); + }); + + // stringifyStream.on('data', function(str) { + // writeStream.write(str); + // }); + + stringifyStream.on('end', function() { + writeStream.end(); + }); + + stringifyStream.on('error', function(error) { + reject(error); + }); + }); +} diff --git a/package.json b/package.json index d5744f6..030f7f3 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,10 @@ "node": ">=4.0.0" }, "dependencies": { + "big-json": "^2.0.2", "bluebird": "^3.5.3", "cesium": "^1.54.0", + "flush-write-stream": "^1.1.0", "fs-extra": "^7.0.1", "jpeg-js": "^0.3.4", "mime": "^2.4.0",