jwalker 1.0.4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js ADDED
@@ -0,0 +1,49 @@
1
+ module.exports = {
2
+ 'env': {
3
+ 'es6': true,
4
+ 'node': true,
5
+ 'mocha': true
6
+ },
7
+ 'parserOptions': {
8
+ 'ecmaVersion': 2017
9
+ },
10
+ 'extends': 'eslint:recommended',
11
+ 'rules': {
12
+ 'indent': [
13
+ 'error',
14
+ 'tab'
15
+ ],
16
+ 'linebreak-style': [
17
+ 'error',
18
+ 'unix'
19
+ ],
20
+ 'quotes': [
21
+ 'error',
22
+ 'single'
23
+ ],
24
+ 'semi': [
25
+ 'error',
26
+ 'always'
27
+ ],
28
+ 'no-console': [
29
+ 'error', {
30
+ 'allow': [
31
+ 'warn',
32
+ 'error',
33
+ 'info'
34
+ ]
35
+ }
36
+ ],
37
+ 'no-unused-vars': [
38
+ 'error', {
39
+ 'argsIgnorePattern': '^_'
40
+ }
41
+ ],
42
+ 'no-empty': [
43
+ 'error', {
44
+ 'allowEmptyCatch': true
45
+ }
46
+ ],
47
+ 'require-atomic-updates': 'off'
48
+ }
49
+ };
@@ -0,0 +1,18 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+ {
8
+ "type": "node",
9
+ "request": "launch",
10
+ "name": "Launch Program",
11
+ "skipFiles": [
12
+ "<node_internals>/**"
13
+ ],
14
+ "program": "${workspaceFolder}/bin/index.js",
15
+ "args": ["-i=/Users/trenskow/Desktop/test.json", "-p"]
16
+ }
17
+ ]
18
+ }
package/README.md CHANGED
@@ -3,16 +3,42 @@ jwalker
3
3
 
4
4
  Parses a file stream for JSON and pretty prints it.
5
5
 
6
- # Usage
7
-
8
- Usage: jwalker [options]
9
-
10
- Use -- (or omit) for stdin or stdout.
11
-
12
- Options:
13
- -i=[file], --input=[file] Input file.
14
- -o=[file], --output=[file] Output file.
15
- -e, --exit Exit on errors.
6
+ # Usage (CLI)
7
+
8
+ Using jwalker on the command line.
9
+
10
+ ````
11
+ Usage: jwalker [options]
12
+
13
+ Use -- (or omit) for stdin or stdout.
14
+
15
+ Options:
16
+ -i=[file], --input=[file] Input file (-- for stdin) (default=stdin).
17
+ -o=[file], --output=[file] Output file (-- for stdout) (default=stdout).
18
+ -p, --passthrough Passthrough non-JSON.
19
+ -s=[spaces], --spaces=[spaces] Number of spaces per indentation (default=4).
20
+ ````
21
+
22
+ # Usage (code)
23
+
24
+ Using jwalker as a transform stream in node.js.
25
+
26
+ ````javascript
27
+
28
+ const JWalker = require('jwalker');
29
+
30
+ readStream
31
+ .pipe(new JWalker({ /* options */ }))
32
+ .pipe(writeStream);
33
+
34
+ ````
35
+
36
+ ## Options
37
+
38
+ | Name | Type | Description | Default |
39
+ |:---------------------|:---------:|:---------------------------------------------|:--------|
40
+ | `passthroughNonJSON` | `Boolean` | Tells the transform to passthrough non-JSON. | `false` |
41
+ | `spaces` | `Number` | The number of spaces used per indentation. | `4` |
16
42
 
17
43
  # License
18
44
 
package/bin/index.js ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from 'fs';
4
+
5
+ import argumentsParser from '@trenskow/arguments-parser';
6
+
7
+ import JWalker from '../lib/index.js';
8
+
9
+ const {
10
+ input,
11
+ output,
12
+ spaces = '\t',
13
+ passthrough
14
+ } = await argumentsParser().options({
15
+ 'input': {
16
+ type: String,
17
+ default: '--',
18
+ description: 'Input file (-- for stdin).'
19
+ },
20
+ 'output': {
21
+ type: String,
22
+ default: '--',
23
+ description: 'Output file (-- for stdout).'
24
+ },
25
+ 'spaces': {
26
+ type: Number,
27
+ description: 'Use number of spaces for indentation (default is tab).'
28
+ },
29
+ 'passthrough': {
30
+ type: Boolean,
31
+ default: false,
32
+ description: 'Passthrough non-JSON lines.'
33
+ }
34
+ });
35
+
36
+ (input === '--' ? process.stdin : fs.createReadStream(input))
37
+ .pipe(new JWalker({ passthroughNonJSON: passthrough, spaces: spaces }))
38
+ .pipe((output === '--' ? process.stdout : fs.createWriteStream(output)));
@@ -0,0 +1,63 @@
1
+ import globals from 'globals';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import js from '@eslint/js';
5
+ import { FlatCompat } from '@eslint/eslintrc';
6
+ import { includeIgnoreFile } from '@eslint/compat';
7
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+ const compat = new FlatCompat({
11
+ baseDirectory: __dirname,
12
+ recommendedConfig: js.configs.recommended,
13
+ allConfig: js.configs.all
14
+ });
15
+ const gitIgnorePath = path.resolve(__dirname, '.gitignore');
16
+
17
+ export default [...compat.extends('eslint:recommended'),
18
+ includeIgnoreFile(gitIgnorePath), {
19
+ languageOptions: {
20
+ globals: {
21
+ ...globals.node,
22
+ ...globals.mocha,
23
+ Router: true,
24
+ Endpoint: true,
25
+ app: true
26
+ },
27
+
28
+ ecmaVersion: 'latest',
29
+ sourceType: 'module'
30
+ },
31
+
32
+ rules: {
33
+ indent: ['error', 'tab', {
34
+ SwitchCase: 1
35
+ }],
36
+
37
+ 'linebreak-style': ['error', 'unix'],
38
+ quotes: ['error', 'single'],
39
+ semi: ['error', 'always'],
40
+
41
+ 'no-console': ['error', {
42
+ allow: ['warn', 'error', 'info']
43
+ }],
44
+
45
+ 'no-unused-vars': ['warn', {
46
+ argsIgnorePattern: '^_',
47
+ caughtErrors: 'none'
48
+ }],
49
+
50
+ 'no-empty': ['error', {
51
+ allowEmptyCatch: true
52
+ }],
53
+
54
+ 'no-trailing-spaces': ['error', {
55
+ ignoreComments: true
56
+ }],
57
+
58
+ 'require-atomic-updates': 'off',
59
+ 'no-implicit-globals': ['error'],
60
+ 'eol-last': ['error', 'always'],
61
+ 'comma-dangle': ['error', 'never']
62
+ }
63
+ }];
package/index.js CHANGED
@@ -1,56 +1,3 @@
1
- #!/usr/bin/env node
2
-
3
1
  'use strict';
4
2
 
5
- const fs = require('fs');
6
- const readline = require('readline');
7
-
8
- const minimist = require('minimist');
9
- const args = minimist(process.argv.slice(2), {
10
- alias: {
11
- 'h': 'help',
12
- 'e': 'exit',
13
- 'i': 'input',
14
- 'o': 'output'
15
- },
16
- '--': false
17
- });
18
-
19
- const help = () => {
20
-
21
- console.log('Usage: jwalker [options]');
22
- console.log();
23
- console.log(' Use -- (or omit) for stdin or stdout.');
24
- console.log();
25
- console.log(' Options:');
26
- console.log(' -i=[file], --input=[file] Input file.');
27
- console.log(' -o=[file], --output=[file] Output file.');
28
- console.log(' -e, --exit Exit on errors.');
29
- console.log();
30
-
31
- process.exit(0);
32
-
33
- };
34
-
35
- if (args.help === true) return help();
36
-
37
- const exits = (args.exit === true);
38
-
39
- args.input = args.input || '--';
40
- args.output = args.output || '--';
41
-
42
- const inputStream = (args.input === '--' ? process.stdin : fs.createReadStream(args.input));
43
- const outputStream = (args.output === '--' ? process.stdout : fs.createWriteStream(args.output));
44
-
45
- const line = readline.createInterface({
46
- input: inputStream
47
- });
48
-
49
- line.on('line', line => {
50
- try {
51
- outputStream.write(JSON.stringify(JSON.parse(line), null, 4) + '\n', 'utf8');
52
- } catch(err) {
53
- outputStream.write(line + '\n');
54
- if (exits) process.exit(1);
55
- }
56
- });
3
+ exports = module.exports = require('./lib');
package/lib/index.js ADDED
@@ -0,0 +1,56 @@
1
+
2
+ import { Transform } from 'stream';
3
+
4
+ export default class JWalker extends Transform {
5
+
6
+ constructor(options = {}) {
7
+
8
+ super();
9
+
10
+ this._options = options;
11
+
12
+ this._backBuffer = new Buffer.from('');
13
+ this._lastBufferSearch = 0;
14
+
15
+ }
16
+
17
+ _parse(chunk) {
18
+
19
+ try {
20
+ this.push(Buffer.from(JSON.stringify(JSON.parse(chunk), null, this._options.spaces || 4) + '\n', 'utf-8'));
21
+ } catch (_) {
22
+ if (this._options.passthroughNonJSON) {
23
+ this.push(Buffer.concat([chunk, Buffer.from('\n', 'utf-8')]));
24
+ }
25
+ }
26
+
27
+ }
28
+
29
+ _transform(chunk, enc, done) {
30
+
31
+ this._backBuffer = Buffer.concat([this._backBuffer, Buffer.from(chunk, enc)]);
32
+
33
+ for (let idx = this._lastBufferSearch ; idx < this._backBuffer.length ; idx++) {
34
+ if (this._backBuffer.readUInt8(idx) === 0x0A) {
35
+
36
+ let chunk = this._backBuffer.slice(0, idx);
37
+
38
+ this._backBuffer = this._backBuffer.slice(idx + 1);
39
+ this._lastBufferSearch = idx = 0;
40
+
41
+ this._parse(chunk);
42
+
43
+ }
44
+ }
45
+
46
+ done();
47
+
48
+ }
49
+
50
+ _flush(done) {
51
+ this._parse(this._backBuffer);
52
+ this._backBuffer = undefined;
53
+ done();
54
+ }
55
+
56
+ };
package/package.json CHANGED
@@ -1,37 +1,44 @@
1
1
  {
2
- "name": "jwalker",
3
- "version": "1.0.4",
4
- "description": "A JSON pretty-printer for each line in a stream. Useful for JSON logs.",
5
- "main": "index.js",
6
- "bin": {
7
- "jwalker": "./index.js"
8
- },
9
- "scripts": {
10
- "test": "./node_modules/mocha/bin/mocha test/index.js"
11
- },
12
- "repository": {
13
- "type": "git",
14
- "url": "git+https://github.com/trenskow/jwalker.git"
15
- },
16
- "keywords": [
17
- "JSON",
18
- "lint",
19
- "pretty",
20
- "print",
21
- "line",
22
- "readline",
23
- "log",
24
- "logs",
25
- "stream"
26
- ],
27
- "author": "Kristian Trenskow <trenskow@me.com>",
28
- "license": "BSD-3-Clause",
29
- "bugs": {
30
- "url": "https://github.com/trenskow/jwalker/issues"
31
- },
32
- "homepage": "https://github.com/trenskow/jwalker#readme",
33
- "devDependencies": {},
34
- "dependencies": {
35
- "minimist": "^1.2.0"
36
- }
2
+ "name": "jwalker",
3
+ "version": "3.0.0",
4
+ "description": "A JSON pretty-printer for each line in a stream. Useful for JSON logs.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "jwalker": "./bin/index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "./node_modules/mocha/bin/mocha test/index.js"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/trenskow/jwalker.git"
16
+ },
17
+ "keywords": [
18
+ "JSON",
19
+ "lint",
20
+ "pretty",
21
+ "print",
22
+ "line",
23
+ "readline",
24
+ "log",
25
+ "logs",
26
+ "stream"
27
+ ],
28
+ "author": "Kristian Trenskow <trenskow@me.com>",
29
+ "license": "BSD-3-Clause",
30
+ "bugs": {
31
+ "url": "https://github.com/trenskow/jwalker/issues"
32
+ },
33
+ "homepage": "https://github.com/trenskow/jwalker#readme",
34
+ "devDependencies": {
35
+ "@eslint/compat": "^2.0.0",
36
+ "@eslint/eslintrc": "^3.3.3",
37
+ "@eslint/js": "^9.39.2",
38
+ "eslint": "^9.39.2",
39
+ "globals": "^17.0.0"
40
+ },
41
+ "dependencies": {
42
+ "@trenskow/arguments-parser": "^0.3.54"
43
+ }
37
44
  }