@file-type/xml 0.3.0 → 0.4.0
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/lib/index.d.ts +1 -9
- package/lib/index.js +30 -37
- package/package.json +3 -3
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
/**
|
|
3
|
-
* Temporary, should be taken from `file-type`
|
|
4
|
-
*/
|
|
5
|
-
interface FileTypeResult {
|
|
6
|
-
ext: string;
|
|
7
|
-
mime: string;
|
|
8
|
-
}
|
|
9
|
-
export type Detector = (tokenizer: ITokenizer, fileType?: FileTypeResult) => Promise<FileTypeResult | undefined>;
|
|
1
|
+
import { type FileTypeResult, type Detector } from 'file-type';
|
|
10
2
|
interface IXmlTextDetectorOptions {
|
|
11
3
|
fullScan?: boolean;
|
|
12
4
|
}
|
package/lib/index.js
CHANGED
|
@@ -31,15 +31,6 @@ function isXml(array) {
|
|
|
31
31
|
}
|
|
32
32
|
return { xml: false };
|
|
33
33
|
}
|
|
34
|
-
function extractNsElement(node) {
|
|
35
|
-
const parts = node.name.split(':');
|
|
36
|
-
if (parts.length === 1) {
|
|
37
|
-
return { name: parts[0], ns: node.attributes['xmlns'] };
|
|
38
|
-
}
|
|
39
|
-
else if (parts.length === 2) {
|
|
40
|
-
return { name: parts[1], ns: node.attributes[`xmlns:${parts[0]}`] };
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
34
|
/**
|
|
44
35
|
* Maps the root element namespace to corresponding file-type
|
|
45
36
|
*/
|
|
@@ -85,7 +76,7 @@ export class XmlTextDetector {
|
|
|
85
76
|
this.options = options ?? {};
|
|
86
77
|
this.firstTag = true;
|
|
87
78
|
this.onEnd = false;
|
|
88
|
-
this.parser = sax.parser(true);
|
|
79
|
+
this.parser = sax.parser(true, { xmlns: true });
|
|
89
80
|
this.nesting = 0;
|
|
90
81
|
this.parser.onerror = e => {
|
|
91
82
|
if (e.message.startsWith('Invalid character entity')) { // Allow entity reference
|
|
@@ -100,14 +91,13 @@ export class XmlTextDetector {
|
|
|
100
91
|
return;
|
|
101
92
|
}
|
|
102
93
|
this.firstTag = false;
|
|
103
|
-
|
|
104
|
-
if (nsNode?.ns) {
|
|
94
|
+
if (node.uri) {
|
|
105
95
|
// Resolve file-type boot root element namespace
|
|
106
|
-
this.fileType = namespaceMapping[
|
|
96
|
+
this.fileType = namespaceMapping[node.uri];
|
|
107
97
|
}
|
|
108
|
-
else if (
|
|
98
|
+
else if (node.name) {
|
|
109
99
|
// Fall back on element name if there is no namespace
|
|
110
|
-
this.fileType = rootNameMapping[
|
|
100
|
+
this.fileType = rootNameMapping[node.name.toLowerCase()];
|
|
111
101
|
}
|
|
112
102
|
if (this.fileType && !this.options.fullScan) {
|
|
113
103
|
this.onEnd = true;
|
|
@@ -128,27 +118,30 @@ export class XmlTextDetector {
|
|
|
128
118
|
return this.nesting === 0;
|
|
129
119
|
}
|
|
130
120
|
}
|
|
131
|
-
export const detectXml =
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
xmlTextDetector.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
121
|
+
export const detectXml = {
|
|
122
|
+
id: 'xml',
|
|
123
|
+
detect: async (tokenizer) => {
|
|
124
|
+
const buffer = new Uint8Array(512);
|
|
125
|
+
// Increase sample size from 12 to 256.
|
|
126
|
+
await tokenizer.peekBuffer(buffer, { length: 128, mayBeLess: true });
|
|
127
|
+
const xmlDetection = isXml(buffer);
|
|
128
|
+
if (xmlDetection.xml) {
|
|
129
|
+
await tokenizer.ignore(xmlDetection.offset);
|
|
130
|
+
const xmlTextDetector = new XmlTextDetector();
|
|
131
|
+
const textDecoder = new TextDecoder(xmlDetection.encoding);
|
|
132
|
+
do {
|
|
133
|
+
const len = await tokenizer.readBuffer(buffer, { mayBeLess: true });
|
|
134
|
+
const portion = buffer.subarray(0, len);
|
|
135
|
+
const text = textDecoder.decode(portion);
|
|
136
|
+
xmlTextDetector.write(text);
|
|
137
|
+
if (len < buffer.length) {
|
|
138
|
+
xmlTextDetector.close();
|
|
139
|
+
}
|
|
140
|
+
} while (!xmlTextDetector.onEnd);
|
|
141
|
+
return xmlTextDetector.fileType ?? {
|
|
142
|
+
ext: 'xml',
|
|
143
|
+
mime: 'application/xml'
|
|
144
|
+
};
|
|
145
|
+
}
|
|
153
146
|
}
|
|
154
147
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@file-type/xml",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "XML detection plugin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -35,9 +35,9 @@
|
|
|
35
35
|
"@types/sax": "^1.2.7",
|
|
36
36
|
"chai": "^5.1.2",
|
|
37
37
|
"del-cli": "^6.0.0",
|
|
38
|
-
"file-type": "^
|
|
38
|
+
"file-type": "^20.0.0",
|
|
39
39
|
"mocha": "^11.0.1",
|
|
40
|
-
"typescript": "^5.7.
|
|
40
|
+
"typescript": "^5.7.3"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"lib/**/*.js",
|