jsonfixerdev 1.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.
Files changed (4) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +118 -0
  3. package/bin/cli.js +176 -0
  4. package/package.json +95 -0
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ The ISC License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # jsonfixer
2
+
3
+ Repair invalid JSON documents by fixing common syntax issues.
4
+
5
+ Website: https://jsonfixer.dev
6
+
7
+ ## Install
8
+
9
+ ```
10
+ npm install jsonfixerdev
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### ES module
16
+
17
+ ```js
18
+ import { jsonfixer } from 'jsonfixerdev'
19
+
20
+ const input = "{name: 'John'}"
21
+ const fixed = jsonfixer(input)
22
+ console.log(fixed) // '{"name": "John"}'
23
+ ```
24
+
25
+ ### CommonJS
26
+
27
+ ```js
28
+ const { jsonfixer } = require('jsonfixerdev')
29
+
30
+ const input = "{name: 'John'}"
31
+ const fixed = jsonfixer(input)
32
+ console.log(fixed)
33
+ ```
34
+
35
+ ### Streaming (Node.js)
36
+
37
+ ```js
38
+ import { createReadStream, createWriteStream } from 'node:fs'
39
+ import { pipeline } from 'node:stream'
40
+ import { jsonfixerTransform } from 'jsonfixerdev/stream'
41
+
42
+ const inputStream = createReadStream('./data/broken.json')
43
+ const outputStream = createWriteStream('./data/fixed.json')
44
+
45
+ pipeline(inputStream, jsonfixerTransform(), outputStream, (err) => {
46
+ if (err) console.error(err)
47
+ else console.log('done')
48
+ })
49
+ ```
50
+
51
+ ## What it fixes
52
+
53
+ jsonfixer can repair issues such as:
54
+ - Missing quotes around keys
55
+ - Missing commas or closing brackets
56
+ - Single quotes instead of double quotes
57
+ - Trailing commas
58
+ - Comments and JSONP wrappers
59
+ - Escaped JSON strings
60
+ - MongoDB-style wrappers like `NumberLong(2)` and `ISODate(...)`
61
+ - Newline-delimited JSON (converted to a JSON array)
62
+
63
+ ## API
64
+
65
+ ### Regular API
66
+
67
+ ```ts
68
+ // @throws JSONFixerError
69
+ jsonfixer(json: string): string
70
+ ```
71
+
72
+ ### Streaming API
73
+
74
+ ```ts
75
+ jsonfixerTransform(options?: { chunkSize?: number, bufferSize?: number }): Transform
76
+ ```
77
+
78
+ - `chunkSize` controls output chunk size (default `65536`).
79
+ - `bufferSize` controls the sliding window size (default `65536`).
80
+
81
+ ## CLI
82
+
83
+ Install globally:
84
+
85
+ ```
86
+ npm install -g jsonfixerdev
87
+ ```
88
+
89
+ Usage:
90
+
91
+ ```
92
+ jsonfixerdev [filename] {OPTIONS}
93
+ ```
94
+
95
+ Options:
96
+
97
+ ```
98
+ --version, -v Show application version
99
+ --help, -h Show this message
100
+ --output, -o Output file
101
+ --overwrite Overwrite the input file
102
+ --buffer Buffer size in bytes, for example 64K (default) or 1M
103
+ ```
104
+
105
+ Examples:
106
+
107
+ ```
108
+ jsonfixerdev broken.json # Repair a file, output to console
109
+ jsonfixerdev broken.json > fixed.json # Repair a file, output to file
110
+ jsonfixerdev broken.json --output fixed.json # Repair a file, output to file
111
+ jsonfixerdev broken.json --overwrite # Repair a file, replace the file itself
112
+ cat broken.json | jsonfixerdev # Repair data from an input stream
113
+ cat broken.json | jsonfixerdev > fixed.json # Repair data from an input stream, output to file
114
+ ```
115
+
116
+ ## License
117
+
118
+ ISC
package/bin/cli.js ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env node
2
+ import { createReadStream, createWriteStream, readFileSync, renameSync } from 'node:fs'
3
+ import { pipeline as pipelineCallback } from 'node:stream'
4
+ import { dirname, join } from 'node:path'
5
+ import { fileURLToPath } from 'node:url'
6
+ import { promisify } from 'node:util'
7
+ import { jsonfixerTransform } from '../lib/esm/stream.js'
8
+
9
+ const pipeline = promisify(pipelineCallback)
10
+ const __filename = fileURLToPath(import.meta.url)
11
+ const __dirname = dirname(__filename)
12
+
13
+ function processArgs(args) {
14
+ const options = {
15
+ version: false,
16
+ help: false,
17
+ overwrite: false,
18
+ bufferSize: undefined,
19
+ inputFile: null,
20
+ outputFile: null
21
+ }
22
+
23
+ // we skip the first two args, since they contain node and the script path
24
+ let i = 2
25
+ while (i < args.length) {
26
+ const arg = args[i]
27
+
28
+ switch (arg) {
29
+ case '-v':
30
+ case '--version':
31
+ options.version = true
32
+ break
33
+
34
+ case '-h':
35
+ case '--help':
36
+ options.help = true
37
+ break
38
+
39
+ case '--overwrite':
40
+ options.overwrite = true
41
+ break
42
+
43
+ case '--buffer':
44
+ i++
45
+ options.bufferSize = parseSize(args[i])
46
+ break
47
+
48
+ case '-o':
49
+ case '--output':
50
+ i++
51
+ options.outputFile = args[i]
52
+ break
53
+
54
+ default:
55
+ if (options.inputFile == null) {
56
+ options.inputFile = arg
57
+ } else {
58
+ throw new Error('Unexpected argument "' + arg + '"')
59
+ }
60
+ }
61
+
62
+ i++
63
+ }
64
+
65
+ return options
66
+ }
67
+
68
+ async function run(options) {
69
+ if (options.version) {
70
+ outputVersion()
71
+ return
72
+ }
73
+
74
+ if (options.help) {
75
+ outputHelp()
76
+ return
77
+ }
78
+
79
+ if (options.overwrite) {
80
+ if (!options.inputFile) {
81
+ console.error('Error: cannot use --overwrite: no input file provided')
82
+ process.exit(1)
83
+ }
84
+ if (options.outputFile) {
85
+ console.error('Error: cannot use --overwrite: there is also an --output provided')
86
+ process.exit(1)
87
+ }
88
+
89
+ const tempFileSuffix = '.fix-' + new Date().toISOString().replace(/\W/g, '-') + '.json'
90
+ const tempFile = options.inputFile + tempFileSuffix
91
+
92
+ try {
93
+ const readStream = createReadStream(options.inputFile)
94
+ const writeStream = createWriteStream(tempFile)
95
+ await pipeline(
96
+ readStream,
97
+ jsonfixerTransform({ bufferSize: options.bufferSize }),
98
+ writeStream
99
+ )
100
+ renameSync(tempFile, options.inputFile)
101
+ } catch (err) {
102
+ process.stderr.write(err.toString())
103
+ process.exit(1)
104
+ }
105
+
106
+ return
107
+ }
108
+
109
+ try {
110
+ const readStream = options.inputFile ? createReadStream(options.inputFile) : process.stdin
111
+ const writeStream = options.outputFile ? createWriteStream(options.outputFile) : process.stdout
112
+ await pipeline(readStream, jsonfixerTransform({ bufferSize: options.bufferSize }), writeStream)
113
+ } catch (err) {
114
+ process.stderr.write(err.toString())
115
+ process.exit(1)
116
+ }
117
+ }
118
+
119
+ function outputVersion() {
120
+ const file = join(__dirname, '../package.json')
121
+ const pkg = JSON.parse(String(readFileSync(file, 'utf-8')))
122
+
123
+ console.log(pkg.version)
124
+ }
125
+
126
+ function parseSize(size) {
127
+ const match = size.match(/^(\d+)([KMG]?)$/)
128
+ if (!match) {
129
+ throw new Error(`Buffer size "${size}" not recognized. Examples: 65536, 512K, 2M`)
130
+ }
131
+
132
+ const num = parseInt(match[1])
133
+ const suffix = match[2] // K, M, or G
134
+
135
+ switch (suffix) {
136
+ case 'K':
137
+ return num * 1024
138
+ case 'M':
139
+ return num * 1024 * 1024
140
+ case 'G':
141
+ return num * 1024 * 1024 * 1024
142
+ default:
143
+ return num
144
+ }
145
+ }
146
+
147
+ const help = `
148
+ jsonfixerdev
149
+
150
+ Repair invalid JSON documents. When a document could not be repaired, the output will be left unchanged.
151
+
152
+ Usage:
153
+ jsonfixerdev [filename] {OPTIONS}
154
+
155
+ Options:
156
+ --version, -v Show application version
157
+ --help, -h Show this message
158
+ --output, -o Output file
159
+ --overwrite Overwrite the input file
160
+ --buffer Buffer size in bytes, for example 64K (default) or 1M
161
+
162
+ Example usage:
163
+ jsonfixerdev broken.json # Repair a file, output to console
164
+ jsonfixerdev broken.json > repaired.json # Repair a file, output to file
165
+ jsonfixerdev broken.json --output repaired.json # Repair a file, output to file
166
+ jsonfixerdev broken.json --overwrite # Repair a file, replace the file itself
167
+ cat broken.json | jsonfixerdev # Repair data from an input stream
168
+ cat broken.json | jsonfixerdev > repaired.json # Repair data from an input stream, output to file
169
+ `
170
+
171
+ function outputHelp() {
172
+ console.log(help)
173
+ }
174
+
175
+ const options = processArgs(process.argv)
176
+ await run(options)
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "jsonfixerdev",
3
+ "version": "1.0.0",
4
+ "description": "Repair broken JSON documents",
5
+ "homepage": "https://jsonfixer.dev",
6
+ "type": "module",
7
+ "main": "lib/cjs/index.js",
8
+ "module": "lib/esm/index.js",
9
+ "browser": "lib/umd/index.min.js",
10
+ "types": "lib/types/index.d.ts",
11
+ "sideEffects": false,
12
+ "exports": {
13
+ ".": {
14
+ "import": "./lib/esm/index.js",
15
+ "require": "./lib/cjs/index.js",
16
+ "types": "./lib/types/index.d.ts"
17
+ },
18
+ "./stream": {
19
+ "import": "./lib/esm/stream.js",
20
+ "require": "./lib/cjs/stream.js",
21
+ "types": "./lib/types/stream.d.ts"
22
+ }
23
+ },
24
+ "keywords": [
25
+ "simple",
26
+ "json",
27
+ "repair",
28
+ "fix",
29
+ "invalid",
30
+ "stream",
31
+ "streaming"
32
+ ],
33
+ "bin": {
34
+ "jsonfixerdev": "./bin/cli.js"
35
+ },
36
+ "scripts": {
37
+ "test": "vitest watch src",
38
+ "test:it": "vitest run src",
39
+ "build": "npm-run-all build:**",
40
+ "build:clean": "del-cli lib",
41
+ "build:esm": "babel src --out-dir lib/esm --extensions \".ts\" --source-maps --config-file ./babel.config.json",
42
+ "build:cjs": "babel src --out-dir lib/cjs --extensions \".ts\" --source-maps --config-file ./babel-cjs.config.json && cpy tools/cjs lib/cjs --flat",
43
+ "build:umd": "rollup lib/esm/index.js --format umd --name JSONFixer --sourcemap --output.file lib/umd/jsonfixer.js && cpy tools/cjs/package.json lib/umd --flat",
44
+ "build:umd:min": "uglifyjs --compress --mangle --source-map --comments --output lib/umd/jsonfixer.min.js -- lib/umd/jsonfixer.js",
45
+ "build:types": "tsc --project tsconfig-types.json",
46
+ "build:validate": "vitest run test-lib",
47
+ "lint": "eslint src/**/*.ts",
48
+ "format": "npm run lint -- --fix",
49
+ "benchmark": "npm run build:esm && node tools/benchmark/run.mjs",
50
+ "build-and-test": "npm run lint && npm run test:it && npm run build",
51
+ "release": "npm-run-all release:**",
52
+ "release:build-and-test": "npm run build-and-test",
53
+ "release:version": "standard-version",
54
+ "release:push": "git push && git push --tag",
55
+ "release:publish": "npm publish",
56
+ "release-dry-run": "npm run build-and-test && standard-version --dry-run",
57
+ "prepare": "husky install"
58
+ },
59
+ "files": [
60
+ "README.md",
61
+ "LICENSE.md",
62
+ "lib"
63
+ ],
64
+ "license": "ISC",
65
+ "devDependencies": {
66
+ "@babel/cli": "7.23.4",
67
+ "@babel/core": "7.23.5",
68
+ "@babel/plugin-transform-typescript": "7.23.5",
69
+ "@babel/preset-env": "7.23.5",
70
+ "@babel/preset-typescript": "7.23.3",
71
+ "@commitlint/cli": "18.4.3",
72
+ "@commitlint/config-conventional": "18.4.3",
73
+ "@types/node": "20.10.4",
74
+ "@typescript-eslint/eslint-plugin": "6.13.2",
75
+ "@typescript-eslint/parser": "6.13.2",
76
+ "benchmark": "2.1.4",
77
+ "cpy-cli": "5.0.0",
78
+ "del-cli": "5.1.0",
79
+ "eslint": "8.55.0",
80
+ "eslint-config-standard": "17.1.0",
81
+ "eslint-plugin-import": "2.29.0",
82
+ "eslint-plugin-n": "16.3.1",
83
+ "eslint-plugin-node": "11.1.0",
84
+ "eslint-plugin-promise": "6.1.1",
85
+ "husky": "8.0.3",
86
+ "npm-run-all": "4.1.5",
87
+ "prettier": "3.1.0",
88
+ "rollup": "4.6.1",
89
+ "standard-version": "9.5.0",
90
+ "ts-node": "10.9.1",
91
+ "typescript": "5.3.3",
92
+ "uglify-js": "3.17.4",
93
+ "vitest": "1.0.1"
94
+ }
95
+ }