1 | const { hasOwnProperty } = Object.prototype
|
---|
2 |
|
---|
3 | const eol = typeof process !== 'undefined' &&
|
---|
4 | process.platform === 'win32' ? '\r\n' : '\n'
|
---|
5 |
|
---|
6 | const encode = (obj, opt) => {
|
---|
7 | const children = []
|
---|
8 | let out = ''
|
---|
9 |
|
---|
10 | if (typeof opt === 'string') {
|
---|
11 | opt = {
|
---|
12 | section: opt,
|
---|
13 | whitespace: false,
|
---|
14 | }
|
---|
15 | } else {
|
---|
16 | opt = opt || Object.create(null)
|
---|
17 | opt.whitespace = opt.whitespace === true
|
---|
18 | }
|
---|
19 |
|
---|
20 | const separator = opt.whitespace ? ' = ' : '='
|
---|
21 |
|
---|
22 | for (const k of Object.keys(obj)) {
|
---|
23 | const val = obj[k]
|
---|
24 | if (val && Array.isArray(val)) {
|
---|
25 | for (const item of val)
|
---|
26 | out += safe(k + '[]') + separator + safe(item) + '\n'
|
---|
27 | } else if (val && typeof val === 'object')
|
---|
28 | children.push(k)
|
---|
29 | else
|
---|
30 | out += safe(k) + separator + safe(val) + eol
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (opt.section && out.length)
|
---|
34 | out = '[' + safe(opt.section) + ']' + eol + out
|
---|
35 |
|
---|
36 | for (const k of children) {
|
---|
37 | const nk = dotSplit(k).join('\\.')
|
---|
38 | const section = (opt.section ? opt.section + '.' : '') + nk
|
---|
39 | const { whitespace } = opt
|
---|
40 | const child = encode(obj[k], {
|
---|
41 | section,
|
---|
42 | whitespace,
|
---|
43 | })
|
---|
44 | if (out.length && child.length)
|
---|
45 | out += eol
|
---|
46 |
|
---|
47 | out += child
|
---|
48 | }
|
---|
49 |
|
---|
50 | return out
|
---|
51 | }
|
---|
52 |
|
---|
53 | const dotSplit = str =>
|
---|
54 | str.replace(/\1/g, '\u0002LITERAL\\1LITERAL\u0002')
|
---|
55 | .replace(/\\\./g, '\u0001')
|
---|
56 | .split(/\./)
|
---|
57 | .map(part =>
|
---|
58 | part.replace(/\1/g, '\\.')
|
---|
59 | .replace(/\2LITERAL\\1LITERAL\2/g, '\u0001'))
|
---|
60 |
|
---|
61 | const decode = str => {
|
---|
62 | const out = Object.create(null)
|
---|
63 | let p = out
|
---|
64 | let section = null
|
---|
65 | // section |key = value
|
---|
66 | const re = /^\[([^\]]*)\]$|^([^=]+)(=(.*))?$/i
|
---|
67 | const lines = str.split(/[\r\n]+/g)
|
---|
68 |
|
---|
69 | for (const line of lines) {
|
---|
70 | if (!line || line.match(/^\s*[;#]/))
|
---|
71 | continue
|
---|
72 | const match = line.match(re)
|
---|
73 | if (!match)
|
---|
74 | continue
|
---|
75 | if (match[1] !== undefined) {
|
---|
76 | section = unsafe(match[1])
|
---|
77 | if (section === '__proto__') {
|
---|
78 | // not allowed
|
---|
79 | // keep parsing the section, but don't attach it.
|
---|
80 | p = Object.create(null)
|
---|
81 | continue
|
---|
82 | }
|
---|
83 | p = out[section] = out[section] || Object.create(null)
|
---|
84 | continue
|
---|
85 | }
|
---|
86 | const keyRaw = unsafe(match[2])
|
---|
87 | const isArray = keyRaw.length > 2 && keyRaw.slice(-2) === '[]'
|
---|
88 | const key = isArray ? keyRaw.slice(0, -2) : keyRaw
|
---|
89 | if (key === '__proto__')
|
---|
90 | continue
|
---|
91 | const valueRaw = match[3] ? unsafe(match[4]) : true
|
---|
92 | const value = valueRaw === 'true' ||
|
---|
93 | valueRaw === 'false' ||
|
---|
94 | valueRaw === 'null' ? JSON.parse(valueRaw)
|
---|
95 | : valueRaw
|
---|
96 |
|
---|
97 | // Convert keys with '[]' suffix to an array
|
---|
98 | if (isArray) {
|
---|
99 | if (!hasOwnProperty.call(p, key))
|
---|
100 | p[key] = []
|
---|
101 | else if (!Array.isArray(p[key]))
|
---|
102 | p[key] = [p[key]]
|
---|
103 | }
|
---|
104 |
|
---|
105 | // safeguard against resetting a previously defined
|
---|
106 | // array by accidentally forgetting the brackets
|
---|
107 | if (Array.isArray(p[key]))
|
---|
108 | p[key].push(value)
|
---|
109 | else
|
---|
110 | p[key] = value
|
---|
111 | }
|
---|
112 |
|
---|
113 | // {a:{y:1},"a.b":{x:2}} --> {a:{y:1,b:{x:2}}}
|
---|
114 | // use a filter to return the keys that have to be deleted.
|
---|
115 | const remove = []
|
---|
116 | for (const k of Object.keys(out)) {
|
---|
117 | if (!hasOwnProperty.call(out, k) ||
|
---|
118 | typeof out[k] !== 'object' ||
|
---|
119 | Array.isArray(out[k]))
|
---|
120 | continue
|
---|
121 |
|
---|
122 | // see if the parent section is also an object.
|
---|
123 | // if so, add it to that, and mark this one for deletion
|
---|
124 | const parts = dotSplit(k)
|
---|
125 | let p = out
|
---|
126 | const l = parts.pop()
|
---|
127 | const nl = l.replace(/\\\./g, '.')
|
---|
128 | for (const part of parts) {
|
---|
129 | if (part === '__proto__')
|
---|
130 | continue
|
---|
131 | if (!hasOwnProperty.call(p, part) || typeof p[part] !== 'object')
|
---|
132 | p[part] = Object.create(null)
|
---|
133 | p = p[part]
|
---|
134 | }
|
---|
135 | if (p === out && nl === l)
|
---|
136 | continue
|
---|
137 |
|
---|
138 | p[nl] = out[k]
|
---|
139 | remove.push(k)
|
---|
140 | }
|
---|
141 | for (const del of remove)
|
---|
142 | delete out[del]
|
---|
143 |
|
---|
144 | return out
|
---|
145 | }
|
---|
146 |
|
---|
147 | const isQuoted = val =>
|
---|
148 | (val.charAt(0) === '"' && val.slice(-1) === '"') ||
|
---|
149 | (val.charAt(0) === "'" && val.slice(-1) === "'")
|
---|
150 |
|
---|
151 | const safe = val =>
|
---|
152 | (typeof val !== 'string' ||
|
---|
153 | val.match(/[=\r\n]/) ||
|
---|
154 | val.match(/^\[/) ||
|
---|
155 | (val.length > 1 &&
|
---|
156 | isQuoted(val)) ||
|
---|
157 | val !== val.trim())
|
---|
158 | ? JSON.stringify(val)
|
---|
159 | : val.replace(/;/g, '\\;').replace(/#/g, '\\#')
|
---|
160 |
|
---|
161 | const unsafe = (val, doUnesc) => {
|
---|
162 | val = (val || '').trim()
|
---|
163 | if (isQuoted(val)) {
|
---|
164 | // remove the single quotes before calling JSON.parse
|
---|
165 | if (val.charAt(0) === "'")
|
---|
166 | val = val.substr(1, val.length - 2)
|
---|
167 |
|
---|
168 | try {
|
---|
169 | val = JSON.parse(val)
|
---|
170 | } catch (_) {}
|
---|
171 | } else {
|
---|
172 | // walk the val to find the first not-escaped ; character
|
---|
173 | let esc = false
|
---|
174 | let unesc = ''
|
---|
175 | for (let i = 0, l = val.length; i < l; i++) {
|
---|
176 | const c = val.charAt(i)
|
---|
177 | if (esc) {
|
---|
178 | if ('\\;#'.indexOf(c) !== -1)
|
---|
179 | unesc += c
|
---|
180 | else
|
---|
181 | unesc += '\\' + c
|
---|
182 |
|
---|
183 | esc = false
|
---|
184 | } else if (';#'.indexOf(c) !== -1)
|
---|
185 | break
|
---|
186 | else if (c === '\\')
|
---|
187 | esc = true
|
---|
188 | else
|
---|
189 | unesc += c
|
---|
190 | }
|
---|
191 | if (esc)
|
---|
192 | unesc += '\\'
|
---|
193 |
|
---|
194 | return unesc.trim()
|
---|
195 | }
|
---|
196 | return val
|
---|
197 | }
|
---|
198 |
|
---|
199 | module.exports = {
|
---|
200 | parse: decode,
|
---|
201 | decode,
|
---|
202 | stringify: encode,
|
---|
203 | encode,
|
---|
204 | safe,
|
---|
205 | unsafe,
|
---|
206 | }
|
---|