-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from linkedconnections/development
v1.3.3
- Loading branch information
Showing
9 changed files
with
252 additions
and
305 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,67 @@ | ||
const Writable = require('stream').Writable; | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
|
||
module.exports = class pageWriterStream extends Writable { | ||
|
||
constructor(targetPath, size) { | ||
super({ objectMode: true }); | ||
this._targetPath = targetPath + '/'; | ||
this._size = size; | ||
this._byteCount = 0; | ||
this._currentFileName = ''; | ||
this._wstream = ''; | ||
this._lastDepartureTime = null; | ||
} | ||
|
||
_write(data, encoding, done) { | ||
if (!data['@context']) { | ||
let dataString = JSON.stringify(data); | ||
let buffer = Buffer.from(dataString); | ||
|
||
if (this._currentFileName == '') { | ||
this._currentFileName = data.departureTime; | ||
this._wstream = fs.createWriteStream(this._targetPath + this._currentFileName + '.jsonld'); | ||
this._wstream.write(dataString); | ||
this._byteCount += buffer.byteLength; | ||
} else { | ||
if (this._byteCount >= this._size && data.departureTime != this._lastDepartureTime) { | ||
this._wstream.end(); | ||
this._currentFileName = data.departureTime; | ||
this._wstream = fs.createWriteStream(this._targetPath + this._currentFileName + '.jsonld'); | ||
this._wstream.write(dataString); | ||
this._byteCount = buffer.byteLength; | ||
constructor(targetPath, size) { | ||
super({ objectMode: true }); | ||
this._targetPath = targetPath + '/'; | ||
this._size = size; | ||
this._count = 0; | ||
this._currentFileName = null; | ||
this._lastDepartureTime = null; | ||
this._gzip = null; | ||
this._wstream = null; | ||
} | ||
|
||
_write(data, encoding, done) { | ||
const dataValue = data.value; | ||
|
||
if (!dataValue['@context']) { | ||
const dataString = JSON.stringify(dataValue); | ||
|
||
if (!this._currentFileName) { | ||
this._currentFileName = dataValue.departureTime; | ||
this.initWriter(); | ||
this._gzip.write(dataString, () => { | ||
this._count++; | ||
done(); | ||
}); | ||
|
||
} else { | ||
if (this._count >= this._size && dataValue.departureTime != this._lastDepartureTime) { | ||
this._gzip.end(null, () => { | ||
this._currentFileName = dataValue.departureTime; | ||
this.initWriter(); | ||
this._gzip.write(dataString, () => { | ||
this._count = 1 | ||
this._lastDepartureTime = dataValue.departureTime; | ||
done(); | ||
}); | ||
}); | ||
} else { | ||
this._gzip.write(',\n' + dataString, () => { | ||
this._count++; | ||
this._lastDepartureTime = dataValue.departureTime; | ||
done(); | ||
}); | ||
} | ||
} | ||
} else { | ||
this._wstream.write(',\n' + dataString); | ||
this._byteCount += buffer.byteLength; | ||
done(); | ||
} | ||
} | ||
this._lastDepartureTime = data.departureTime; | ||
} | ||
done(); | ||
} | ||
|
||
_final(done) { | ||
this._wstream.on('finish', () => { | ||
done(); | ||
}); | ||
this._gzip.end(); | ||
} | ||
|
||
initWriter() { | ||
this._gzip = zlib.createGzip(); | ||
this._wstream = this._gzip.pipe(fs.createWriteStream(this._targetPath + this._currentFileName + '.jsonld.gz')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.