@file-type/xml 0.4.0 → 0.4.2

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/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  [![NPM version](https://img.shields.io/npm/v/@file-type/xml.svg)](https://npmjs.org/package/@file-type/xml)
2
2
  [![Node.js CI](https://github.com/Borewit/file-type-xml/actions/workflows/nodejs-ci.yml/badge.svg)](https://github.com/Borewit/file-type-xml/actions/workflows/nodejs-ci.yml)
3
+ [![npm downloads](http://img.shields.io/npm/dm/@file-type/xml.svg)](https://npmcharts.com/compare/@file-type/xml?start=365)
4
+
3
5
  # @file-type/xml
4
6
 
5
7
  Detector plugin for [file-type](https://github.com/sindresorhus/file-type) for XML files.
@@ -17,15 +19,16 @@ The following example shows how add the XML detector to [file-type](https://gith
17
19
  import {NodeFileTypeParser} from 'file-type';
18
20
  import {detectXml} from '@file-type/xml';
19
21
 
20
- const parser = new NodeFileTypeParser({customDetectors: [detectXml]});
22
+ const parser = new FileTypeParser({customDetectors: [detectXml]});
21
23
  const fileType = await parser.fromFile('example.kml');
22
24
  console.log(fileType);
23
25
  ```
24
26
 
25
- You can also use the XML detector outside file-type:
27
+ You can also use the XML detector outside [file-type](https://github.com/sindresorhus/file-type):
26
28
  ```js
27
29
  import {XmlTextDetector} from '@file-type/xml';
28
30
 
31
+ const xmlTextDetector = new XmlTextDetector();
29
32
  xmlTextDetector.write('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
30
33
  const fileType = xmlTextDetector.fileType;
31
34
  console.log(JSON.stringify(fileType)); // Outputs: {"ext":"svg","mime":"image/svg+xml"}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type FileTypeResult, type Detector } from 'file-type';
1
+ import type { FileTypeResult, Detector } from 'file-type';
2
2
  interface IXmlTextDetectorOptions {
3
3
  fullScan?: boolean;
4
4
  }
package/lib/index.js CHANGED
@@ -10,25 +10,45 @@ function startsWith(array, prefix) {
10
10
  }
11
11
  return true;
12
12
  }
13
+ function hasXmlTag(xmlString) {
14
+ return /^<\s*\w+(?=\s+[^<>]*=|>)/.test(xmlString);
15
+ }
16
+ function hasArrayXmlTag(array, encoding) {
17
+ const textDecoder = new TextDecoder(encoding);
18
+ return hasXmlTag(textDecoder.decode(array));
19
+ }
13
20
  function isXml(array) {
14
21
  if (startsWith(array, [60, 63, 120, 109, 108, 32])) {
15
22
  return { xml: true, encoding: 'utf-8', offset: 0 };
16
23
  }
17
- else if (startsWith(array, [0xEF, 0xBB, 0xBF, 60, 63, 120, 109, 108, 32])) { // UTF-8 BOM
18
- return { xml: true, encoding: 'utf-8', offset: 3 };
24
+ if (startsWith(array, [0xEF, 0xBB, 0xBF])) { // UTF-8 BOM
25
+ const encoding = 'utf-8';
26
+ if (startsWith(array.subarray(3), [60, 63, 120, 109, 108, 32]) || hasArrayXmlTag(array, encoding)) {
27
+ return { xml: true, encoding, offset: 3 };
28
+ }
19
29
  }
20
- else if (startsWith(array, [0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
21
- return { xml: true, encoding: 'utf-16be', offset: 2 };
30
+ if (startsWith(array, [0xFE, 0xFF])) {
31
+ const encoding = 'utf-16be';
32
+ if (startsWith(array.subarray(2), [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32]) || hasArrayXmlTag(array, encoding)) {
33
+ return { xml: true, encoding, offset: 2 };
34
+ }
22
35
  }
23
- else if (startsWith(array, [0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
36
+ if (startsWith(array, [0xFF, 0xFE])) {
37
+ const encoding = 'utf-16le';
38
+ if (startsWith(array.subarray(2), [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0]) || hasArrayXmlTag(array, encoding)) {
39
+ return { xml: true, encoding, offset: 2 };
40
+ }
24
41
  return { xml: true, encoding: 'utf-16le', offset: 2 };
25
42
  }
26
- else if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
43
+ if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
27
44
  return { xml: true, encoding: 'utf-16be', offset: 0 };
28
45
  }
29
- else if (startsWith(array, [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
46
+ if (startsWith(array, [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
30
47
  return { xml: true, encoding: 'utf-16le', offset: 0 };
31
48
  }
49
+ if (hasArrayXmlTag(array, 'utf-8')) {
50
+ return { xml: true, encoding: 'utf-8', offset: 0 };
51
+ }
32
52
  return { xml: false };
33
53
  }
34
54
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@file-type/xml",
3
- "version": "0.4.0",
4
- "description": "XML detection plugin",
3
+ "version": "0.4.2",
4
+ "description": "XML detection plugin for file-type",
5
5
  "type": "module",
6
6
  "types": "./lib/index.d.ts",
7
7
  "exports": "./lib/index.js",
@@ -9,8 +9,10 @@
9
9
  "clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'src/**/*.d.ts'",
10
10
  "compile-src": "tsc -p lib",
11
11
  "compile": "yarn run compile-src",
12
+ "lint-ts": "biome check",
12
13
  "build": "yarn run clean && yarn compile",
13
- "test": "mocha"
14
+ "test": "mocha",
15
+ "prepublishOnly": "yarn run build"
14
16
  },
15
17
  "keywords": [
16
18
  "file-type",
@@ -29,15 +31,16 @@
29
31
  ],
30
32
  "dependencies": {
31
33
  "sax": "^1.4.1",
32
- "strtok3": "^10.0.1"
34
+ "strtok3": "^10.2.2"
33
35
  },
34
36
  "devDependencies": {
37
+ "@biomejs/biome": "^1.9.4",
35
38
  "@types/sax": "^1.2.7",
36
- "chai": "^5.1.2",
39
+ "chai": "^5.2.0",
37
40
  "del-cli": "^6.0.0",
38
- "file-type": "^20.0.0",
39
- "mocha": "^11.0.1",
40
- "typescript": "^5.7.3"
41
+ "file-type": "^20.4.1",
42
+ "mocha": "^11.1.0",
43
+ "typescript": "^5.8.3"
41
44
  },
42
45
  "files": [
43
46
  "lib/**/*.js",
@@ -53,5 +56,5 @@
53
56
  "url": "git+https://github.com/Borewit/file-type-xml.git"
54
57
  },
55
58
  "license": "MIT",
56
- "packageManager": "yarn@4.5.3"
59
+ "packageManager": "yarn@4.9.1"
57
60
  }