@scalar/json-magic 0.12.12 → 0.12.15

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.15
4
+
5
+ ## 0.12.14
6
+
7
+ ### Patch Changes
8
+
9
+ - [#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
10
+
11
+ ## 0.12.13
12
+
13
+ ### Patch Changes
14
+
15
+ - [#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
16
+
3
17
  ## 0.12.12
4
18
 
5
19
  ## 0.12.11
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Normalize a string (YAML, JSON, object) to a JavaScript datatype.
3
3
  */
4
- export declare function normalize(content: any): any;
4
+ export declare function normalize(content: unknown): unknown;
5
5
  //# sourceMappingURL=normalize.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/helpers/normalize.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,GAAG,OA6BrC"}
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
- if (content.trim() === '') {
15
+ const withoutBom = stripLeadingUtf8Bom(content);
16
+ if (withoutBom.trim() === '') {
11
17
  return undefined;
12
18
  }
13
19
  try {
14
- return JSON.parse(content);
20
+ return JSON.parse(withoutBom);
15
21
  }
16
- catch (_error) {
22
+ catch {
17
23
  // Does it look like YAML?
18
- const hasColon = /^[^:]+:/.test(content);
19
- const isJson = content.slice(0, 50).trimStart().startsWith('{');
20
- if (!hasColon || isJson) {
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(content, {
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.12",
13
+ "version": "0.12.15",
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.6.0"
92
+ "@scalar/helpers": "0.8.1"
93
93
  },
94
94
  "devDependencies": {
95
95
  "fastify": "^5.8.1",