@file-type/xml 0.4.1 → 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 +2 -0
- package/lib/index.js +25 -5
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
[](https://npmjs.org/package/@file-type/xml)
|
|
2
2
|
[](https://github.com/Borewit/file-type-xml/actions/workflows/nodejs-ci.yml)
|
|
3
|
+
[](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.
|
package/lib/index.js
CHANGED
|
@@ -10,17 +10,34 @@ 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
|
-
if (startsWith(array, [0xEF, 0xBB, 0xBF
|
|
18
|
-
|
|
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
|
-
if (startsWith(array, [0xFE, 0xFF
|
|
21
|
-
|
|
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
|
-
if (startsWith(array, [0xFF, 0xFE
|
|
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
43
|
if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
|
|
@@ -29,6 +46,9 @@ function isXml(array) {
|
|
|
29
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@file-type/xml",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "XML detection plugin for file-type",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"compile": "yarn run compile-src",
|
|
12
12
|
"lint-ts": "biome check",
|
|
13
13
|
"build": "yarn run clean && yarn compile",
|
|
14
|
-
"test": "mocha"
|
|
14
|
+
"test": "mocha",
|
|
15
|
+
"prepublishOnly": "yarn run build"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [
|
|
17
18
|
"file-type",
|
|
@@ -30,16 +31,16 @@
|
|
|
30
31
|
],
|
|
31
32
|
"dependencies": {
|
|
32
33
|
"sax": "^1.4.1",
|
|
33
|
-
"strtok3": "^10.2.
|
|
34
|
+
"strtok3": "^10.2.2"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@biomejs/biome": "^1.9.4",
|
|
37
38
|
"@types/sax": "^1.2.7",
|
|
38
|
-
"chai": "^5.
|
|
39
|
+
"chai": "^5.2.0",
|
|
39
40
|
"del-cli": "^6.0.0",
|
|
40
|
-
"file-type": "^20.1
|
|
41
|
+
"file-type": "^20.4.1",
|
|
41
42
|
"mocha": "^11.1.0",
|
|
42
|
-
"typescript": "^5.
|
|
43
|
+
"typescript": "^5.8.3"
|
|
43
44
|
},
|
|
44
45
|
"files": [
|
|
45
46
|
"lib/**/*.js",
|
|
@@ -55,5 +56,5 @@
|
|
|
55
56
|
"url": "git+https://github.com/Borewit/file-type-xml.git"
|
|
56
57
|
},
|
|
57
58
|
"license": "MIT",
|
|
58
|
-
"packageManager": "yarn@4.
|
|
59
|
+
"packageManager": "yarn@4.9.1"
|
|
59
60
|
}
|