@scalar/json-magic 0.12.11 → 0.12.14
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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @scalar/json-magic
|
|
2
2
|
|
|
3
|
+
## 0.12.14
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#9211](https://github.com/scalar/scalar/pull/9211): fix: strip a leading UTF-8 BOM before JSON.parse in normalize so BOM-prefixed files parse correctly
|
|
8
|
+
|
|
9
|
+
## 0.12.13
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#9107](https://github.com/scalar/scalar/pull/9107): fix: strip a leading UTF-8 BOM before JSON.parse in normalize so BOM-prefixed files parse correctly
|
|
14
|
+
|
|
15
|
+
## 0.12.12
|
|
16
|
+
|
|
3
17
|
## 0.12.11
|
|
4
18
|
|
|
5
19
|
## 0.12.10
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/helpers/normalize.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/helpers/normalize.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAgCnD"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { parse } from 'yaml';
|
|
2
|
+
/**
|
|
3
|
+
* Strip a leading UTF-8 BOM (U+FEFF). Editors on Windows often write this
|
|
4
|
+
* prefix; {@link JSON.parse} rejects it as an unexpected token.
|
|
5
|
+
*/
|
|
6
|
+
const stripLeadingUtf8Bom = (value) => (value.startsWith('\uFEFF') ? value.slice(1) : value);
|
|
2
7
|
/**
|
|
3
8
|
* Normalize a string (YAML, JSON, object) to a JavaScript datatype.
|
|
4
9
|
*/
|
|
@@ -7,20 +12,22 @@ export function normalize(content) {
|
|
|
7
12
|
return undefined;
|
|
8
13
|
}
|
|
9
14
|
if (typeof content === 'string') {
|
|
10
|
-
|
|
15
|
+
const withoutBom = stripLeadingUtf8Bom(content);
|
|
16
|
+
if (withoutBom.trim() === '') {
|
|
11
17
|
return undefined;
|
|
12
18
|
}
|
|
13
19
|
try {
|
|
14
|
-
return JSON.parse(
|
|
20
|
+
return JSON.parse(withoutBom);
|
|
15
21
|
}
|
|
16
|
-
catch
|
|
22
|
+
catch {
|
|
17
23
|
// Does it look like YAML?
|
|
18
|
-
const hasColon = /^[^:]+:/.test(
|
|
19
|
-
const
|
|
20
|
-
|
|
24
|
+
const hasColon = /^[^:]+:/.test(withoutBom);
|
|
25
|
+
const trimmedStart = withoutBom.slice(0, 50).trimStart();
|
|
26
|
+
const looksLikeJson = trimmedStart.startsWith('{') || trimmedStart.startsWith('[');
|
|
27
|
+
if (!hasColon || looksLikeJson) {
|
|
21
28
|
return undefined;
|
|
22
29
|
}
|
|
23
|
-
return parse(
|
|
30
|
+
return parse(withoutBom, {
|
|
24
31
|
maxAliasCount: 10000,
|
|
25
32
|
merge: true,
|
|
26
33
|
});
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"url": "git+https://github.com/scalar/scalar.git",
|
|
11
11
|
"directory": "packages/json-magic"
|
|
12
12
|
},
|
|
13
|
-
"version": "0.12.
|
|
13
|
+
"version": "0.12.14",
|
|
14
14
|
"engines": {
|
|
15
15
|
"node": ">=22"
|
|
16
16
|
},
|
|
@@ -89,7 +89,7 @@
|
|
|
89
89
|
"dependencies": {
|
|
90
90
|
"pathe": "^2.0.3",
|
|
91
91
|
"yaml": "^2.8.3",
|
|
92
|
-
"@scalar/helpers": "0.
|
|
92
|
+
"@scalar/helpers": "0.8.0"
|
|
93
93
|
},
|
|
94
94
|
"devDependencies": {
|
|
95
95
|
"fastify": "^5.8.1",
|