Skip to content

Commit

Permalink
Close stream when only reading meta fields (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nichtich committed Jan 9, 2018
1 parent dafa533 commit 9b2d943
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -110,6 +110,16 @@ fs.createReadStream('beacon-file.txt')
.on('end', () => ... ) // called after successful parsing
~~~

To only parse meta fields, close the input stream like this:

~~~javascript
var input = fs.createReadStream('beacon-file.txt')
input.pipe(parser).on('meta' => {
// ...
input.destroy()
})
~~~

## Writing

A `Writer` writes [BEACON format] to strings:
Expand Down
15 changes: 12 additions & 3 deletions beaconlinks.js
Expand Up @@ -69,8 +69,8 @@ const highlight = opt.color ? {
iri: s => '\u001b[32m' + s + '\u001b[39m'
} : {}

const stream = (file === '-' ? process.stdin : fs.createReadStream(file))
.pipe(beacon.Parser({}))
const input = (file === '-' ? process.stdin : fs.createReadStream(file))
const stream = input.pipe(beacon.Parser())
.on('error', error => {
var msg = error
if (error.number !== undefined) msg += ' ' + error.number
Expand All @@ -84,6 +84,7 @@ if (opt.format === 'json') {
if (opt.meta) {
stream.on('meta', meta => {
stdout.write(JSON.stringify(meta.getValues(opt.brief), null, 4) + '\n')
input.destroy()
})
} else {
stream.on('data', link => stdout.write(JSON.stringify(link) + '\n'))
Expand Down Expand Up @@ -120,6 +121,9 @@ if (opt.format === 'json') {
for (let triple of rdfmapper.metaTriples(meta)) {
stdout.write(triple)
}
if (opt.meta) {
input.destroy()
}
stdout.write('\n')
})
}
Expand Down Expand Up @@ -148,7 +152,12 @@ if (opt.format === 'json') {
})

if (!opt.links) {
stream.on('meta', meta => writer.writeMeta(meta))
stream.on('meta', meta => {
writer.writeMeta(meta)
if (opt.meta) {
input.destroy()
}
})
}

if (!opt.meta) {
Expand Down

0 comments on commit 9b2d943

Please sign in to comment.