Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

module.exports = {
extends: '@cto.af'
root: true,
extends: '@cto.af',
}
6 changes: 3 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ name: Tests

on:
push:
branches: [master]
branches: ['*']
pull_request:
branches: [master]
branches: [main]

jobs:
build:
Expand All @@ -14,7 +14,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [12.x, 14.x, 16.x]

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 5 additions & 0 deletions .ncurc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"reject": [
"eslint"
]
}
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ npm-debug.log
.eslintrc.js
examples/
test/
tsconfig.json
.ncurc.json
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ interesting, then write objects to the generator:
});

[![Tests](https://github.com/hildjj/json-text-sequence/workflows/Tests/badge.svg)](https://github.com/hildjj/json-text-sequence/actions?query=workflow%3ATests)
[![Coverage Status](https://coveralls.io/repos/hildjj/json-text-sequence/badge.png?branch=master)](https://coveralls.io/r/hildjj/json-text-sequence?branch=master)
[![Coverage Status](https://coveralls.io/repos/hildjj/json-text-sequence/badge.png?branch=main)](https://coveralls.io/r/hildjj/json-text-sequence?branch=main)
2 changes: 1 addition & 1 deletion examples/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ g.pipe(fs.createWriteStream('example.log'))
for (let i = 0; i < 10; i++) {
g.write({
d: new Date(),
count: i
count: i,
})
}
44 changes: 28 additions & 16 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'

const assert = require('assert')
const stream = require('stream')
const { DelimitedStream } = require('@sovpro/delimited-stream')
const {DelimitedStream} = require('@sovpro/delimited-stream')

const RS = '\x1e'
exports.RS = RS
Expand Down Expand Up @@ -42,28 +41,32 @@ exports.RS = RS
// });
// process.stdin.pipe(p);
class JSONSequenceParser extends stream.Transform {
// @nodoc
/**
* Create a JSONSequenceParser instance.
*
* @param {stream.TransformOptions} [opts] Stream options.
*/
constructor(opts = {}) {
super({
...opts,
readableObjectMode: true
readableObjectMode: true,
})
this._stream = new DelimitedStream(RS)
.on('data', d => {
// NOTE: delimited-stream will deal with repeated delimiters.
// d.length will always be > 0
assert.ok(d.length > 0)
// assert.ok(d.length > 0)

// if the entry doesn't end with \n, it got truncated
if (d[d.length - 1] !== 0x0a) {
this.emit('truncated', d)
} else {
// If the entry doesn't end with \n, it got truncated
if (d[d.length - 1] === 0x0a) {
try {
const j = JSON.parse(d)
this.push(j)
} catch (ignored) {
this.emit('invalid', d)
}
} else {
this.emit('truncated', d)
}
})
}
Expand All @@ -78,6 +81,8 @@ class JSONSequenceParser extends stream.Transform {
this._stream.end(null, null, cb)
}
}

/** @deprecated */
exports.parser = JSONSequenceParser
exports.Parser = JSONSequenceParser

Expand All @@ -92,19 +97,24 @@ exports.Parser = JSONSequenceParser
// g.pipe(process.stdout);
// g.write({foo: true, bar: 1})
class JSONSequenceGenerator extends stream.Transform {
// @nodoc
constructor() {
super()
this._writableState.objectMode = true
this._readableState.objectMode = false
/**
* Create a JSONSequenceGenerator instance.
*
* @param {stream.TransformOptions} [opts] Stream options.
*/
constructor(opts = {}) {
super({
...opts,
writableObjectMode: true,
})
}

// @nodoc
_transform(chunk, encoding, cb) {
let s = null
try {
// this can fail on circular objects, for example
s = JSON.stringify(chunk, 'utf8')
// This can fail on circular objects, for example
s = JSON.stringify(chunk)
} catch (error) {
return cb(error)
}
Expand All @@ -113,5 +123,7 @@ class JSONSequenceGenerator extends stream.Transform {
return cb()
}
}

/** @deprecated */
exports.generator = JSONSequenceGenerator
exports.Generator = JSONSequenceGenerator
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"directories": {
"lib": "lib"
},
"types": "types/index.d.ts",
"scripts": {
"clean": "rm -rf coverage doc man",
"clean": "rm -rf coverage",
"coverage": "nyc -r lcov npm test",
"test": "ava test/*.js",
"release": "npm version patch && git push --follow-tags && npm publish",
"lint": "eslint lib/*.js test/*.js examples/*.js"
"lint": "eslint . --ext js",
"types": "tsc",
"check": "npm run lint && npm run types && npm run coverage && npm pack --dry-run"
},
"repository": {
"type": "git",
Expand All @@ -33,14 +36,17 @@
"@sovpro/delimited-stream": "^1.1.0"
},
"devDependencies": {
"@cto.af/eslint-config": "^0.0.5",
"@cto.af/eslint-config": "^0.0.10",
"@types/node": "^16.11.0",
"ava": "^3.15.0",
"eslint": "^7.29.0",
"eslint-plugin-ava": "^12.0.0",
"eslint": "^7.32.0",
"eslint-plugin-ava": "^13.0.0",
"eslint-plugin-jsdoc": "^36.1.1",
"eslint-plugin-node": "^11.1.0",
"nyc": "^15.1.0"
"nyc": "^15.1.0",
"typescript": "^4.4.4"
},
"engines": {
"node": ">=10.18.0"
"node": ">=12.20.0"
}
}
3 changes: 2 additions & 1 deletion test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const jts = require('../lib/index')
const test = require('ava')
const {Buffer} = require('buffer')

test('create', t => {
const g = new jts.Generator()
Expand All @@ -22,7 +23,7 @@ test.cb('generate', t => {
g.write(12)
return g.end({
foo: 1,
bar: 'two'
bar: 'two',
})
})

Expand Down
13 changes: 7 additions & 6 deletions test/parser.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const jts = require('../lib/index')
const test = require('ava')
const {Buffer} = require('buffer')

test('create', t => {
const p = new jts.Parser()
Expand All @@ -22,9 +23,9 @@ test.cb('parse', t => {
.on('invalid', d => t.fail('invalid'))
.on('data', j => json.push(j))

p.write(Buffer.from(jts.RS + 'true\n', 'utf8'))
p.write(Buffer.from(jts.RS + '12\n', 'utf8'))
return p.end(Buffer.from(jts.RS + '"foo"\n', 'utf8'))
p.write(Buffer.from(`${jts.RS}true\n`, 'utf8'))
p.write(Buffer.from(`${jts.RS}12\n`, 'utf8'))
return p.end(Buffer.from(`${jts.RS}"foo"\n`, 'utf8'))
})

test.cb('truncate', t => {
Expand All @@ -39,8 +40,8 @@ test.cb('truncate', t => {
.on('error', e => t.fail(e.message))
.on('truncated', d => truncated++)
.on('data', j => (json = j))
p.write(Buffer.from(jts.RS + '"foo', 'utf8'))
return p.end(Buffer.from(jts.RS + '12', 'utf8'))
p.write(Buffer.from(`${jts.RS}"foo`, 'utf8'))
return p.end(Buffer.from(`${jts.RS}12`, 'utf8'))
})

test.cb('invalid', t => {
Expand All @@ -58,7 +59,7 @@ test.cb('invalid', t => {
.on('invalid', d => (invalid = true))
.on('truncated', d => (truncated = true))
.on('data', j => (json = j))
return p.end(Buffer.from(jts.RS + '\n', 'utf8'))
return p.end(Buffer.from(`${jts.RS}\n`, 'utf8'))
})

test.cb('empty', t => {
Expand Down
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["lib/*.js"],

"compilerOptions": {
"allowSyntheticDefaultImports": true,
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"moduleResolution": "node",
"module": "es2020",
// "noImplicitAny": true,
"outDir": "types",
"target": "ES2020"
}
}
9 changes: 9 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const RS: "\u001E";
declare class JSONSequenceParser extends stream.Transform {
_stream: DelimitedStream;
}
declare class JSONSequenceGenerator extends stream.Transform {
}
import stream = require("stream");
import { DelimitedStream } from "@sovpro/delimited-stream";
export { JSONSequenceParser as parser, JSONSequenceParser as Parser, JSONSequenceGenerator as generator, JSONSequenceGenerator as Generator };