1'use strict' 2 3const INDENT = Symbol.for('indent') 4const NEWLINE = Symbol.for('newline') 5 6const DEFAULT_NEWLINE = '\n' 7const DEFAULT_INDENT = ' ' 8const BOM = /^\uFEFF/ 9 10// only respect indentation if we got a line break, otherwise squash it 11// things other than objects and arrays aren't indented, so ignore those 12// Important: in both of these regexps, the $1 capture group is the newline 13// or undefined, and the $2 capture group is the indent, or undefined. 14const FORMAT = /^\s*[{[]((?:\r?\n)+)([\s\t]*)/ 15const EMPTY = /^(?:\{\}|\[\])((?:\r?\n)+)?$/ 16 17// Node 20 puts single quotes around the token and a comma after it 18const UNEXPECTED_TOKEN = /^Unexpected token '?(.)'?(,)? /i 19 20const hexify = (char) => { 21 const h = char.charCodeAt(0).toString(16).toUpperCase() 22 return `0x${h.length % 2 ? '0' : ''}${h}` 23} 24 25// Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) 26// because the buffer-to-string conversion in `fs.readFileSync()` 27// translates it to FEFF, the UTF-16 BOM. 28const stripBOM = (txt) => String(txt).replace(BOM, '') 29 30const makeParsedError = (msg, parsing, position = 0) => ({ 31 message: `${msg} while parsing ${parsing}`, 32 position, 33}) 34 35const parseError = (e, txt, context = 20) => { 36 let msg = e.message 37 38 if (!txt) { 39 return makeParsedError(msg, 'empty string') 40 } 41 42 const badTokenMatch = msg.match(UNEXPECTED_TOKEN) 43 const badIndexMatch = msg.match(/ position\s+(\d+)/i) 44 45 if (badTokenMatch) { 46 msg = msg.replace( 47 UNEXPECTED_TOKEN, 48 `Unexpected token ${JSON.stringify(badTokenMatch[1])} (${hexify(badTokenMatch[1])})$2 ` 49 ) 50 } 51 52 let errIdx 53 if (badIndexMatch) { 54 errIdx = +badIndexMatch[1] 55 } else if (msg.match(/^Unexpected end of JSON.*/i)) { 56 errIdx = txt.length - 1 57 } 58 59 if (errIdx == null) { 60 return makeParsedError(msg, `'${txt.slice(0, context * 2)}'`) 61 } 62 63 const start = errIdx <= context ? 0 : errIdx - context 64 const end = errIdx + context >= txt.length ? txt.length : errIdx + context 65 const slice = `${start ? '...' : ''}${txt.slice(start, end)}${end === txt.length ? '' : '...'}` 66 67 return makeParsedError( 68 msg, 69 `${txt === slice ? '' : 'near '}${JSON.stringify(slice)}`, 70 errIdx 71 ) 72} 73 74class JSONParseError extends SyntaxError { 75 constructor (er, txt, context, caller) { 76 const metadata = parseError(er, txt, context) 77 super(metadata.message) 78 Object.assign(this, metadata) 79 this.code = 'EJSONPARSE' 80 this.systemError = er 81 Error.captureStackTrace(this, caller || this.constructor) 82 } 83 84 get name () { 85 return this.constructor.name 86 } 87 88 set name (n) {} 89 90 get [Symbol.toStringTag] () { 91 return this.constructor.name 92 } 93} 94 95const parseJson = (txt, reviver) => { 96 const result = JSON.parse(txt, reviver) 97 if (result && typeof result === 'object') { 98 // get the indentation so that we can save it back nicely 99 // if the file starts with {" then we have an indent of '', ie, none 100 // otherwise, pick the indentation of the next line after the first \n If the 101 // pattern doesn't match, then it means no indentation. JSON.stringify ignores 102 // symbols, so this is reasonably safe. if the string is '{}' or '[]', then 103 // use the default 2-space indent. 104 const match = txt.match(EMPTY) || txt.match(FORMAT) || [null, '', ''] 105 result[NEWLINE] = match[1] ?? DEFAULT_NEWLINE 106 result[INDENT] = match[2] ?? DEFAULT_INDENT 107 } 108 return result 109} 110 111const parseJsonError = (raw, reviver, context) => { 112 const txt = stripBOM(raw) 113 try { 114 return parseJson(txt, reviver) 115 } catch (e) { 116 if (typeof raw !== 'string' && !Buffer.isBuffer(raw)) { 117 const msg = Array.isArray(raw) && raw.length === 0 ? 'an empty array' : String(raw) 118 throw Object.assign( 119 new TypeError(`Cannot parse ${msg}`), 120 { code: 'EJSONPARSE', systemError: e } 121 ) 122 } 123 throw new JSONParseError(e, txt, context, parseJsonError) 124 } 125} 126 127module.exports = parseJsonError 128parseJsonError.JSONParseError = JSONParseError 129parseJsonError.noExceptions = (raw, reviver) => { 130 try { 131 return parseJson(stripBOM(raw), reviver) 132 } catch { 133 // no exceptions 134 } 135} 136