@file-type/xml 0.2.1 → 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/README.md CHANGED
@@ -24,7 +24,7 @@ console.log(fileType);
24
24
 
25
25
  You can also use the XML detector outside file-type:
26
26
  ```js
27
- import {XmlTextDetector} from 'index.js';
27
+ import {XmlTextDetector} from '@file-type/xml';
28
28
 
29
29
  xmlTextDetector.write('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
30
30
  const fileType = xmlTextDetector.fileType;
package/lib/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { type FileTypeResult, type Detector } from 'file-type';
2
+ interface IXmlTextDetectorOptions {
3
+ fullScan?: boolean;
4
+ }
5
+ export declare class XmlTextDetector {
6
+ private options;
7
+ private firstTag;
8
+ private parser;
9
+ private nesting;
10
+ onEnd: boolean;
11
+ fileType?: FileTypeResult;
12
+ constructor(options?: IXmlTextDetectorOptions);
13
+ write(text: string): void;
14
+ close(): void;
15
+ isValid(): boolean;
16
+ }
17
+ export declare const detectXml: Detector;
18
+ export {};
package/lib/index.js CHANGED
@@ -1,172 +1,147 @@
1
-
2
1
  import sax from 'sax';
3
-
4
2
  function startsWith(array, prefix) {
5
- if (prefix.length > array.length) {
6
- return false;
7
- }
8
- for (let i = 0; i < prefix.length; i++) {
9
- if (array[i] !== prefix[i]) {
10
- return false;
3
+ if (prefix.length > array.length) {
4
+ return false;
11
5
  }
12
- }
13
- return true;
6
+ for (let i = 0; i < prefix.length; i++) {
7
+ if (array[i] !== prefix[i]) {
8
+ return false;
9
+ }
10
+ }
11
+ return true;
14
12
  }
15
-
16
13
  function isXml(array) {
17
- if (startsWith(array,[60, 63, 120, 109, 108, 32])) {
18
- return {xml: true, encoding: 'utf-8', offset: 0}
19
- } else if (startsWith(array,[0xEF, 0xBB, 0xBF, 60, 63, 120, 109, 108, 32])) { // UTF-8 BOM
20
- return {xml: true, encoding: 'utf-8', offset: 3}
21
- } else if (startsWith(array,[0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32 ])) {
22
- return {xml: true, encoding: 'utf-16be', offset: 2}
23
- } else if (startsWith(array,[0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0 ])) {
24
- return {xml: true, encoding: 'utf-16le', offset: 2}
25
- } else if (startsWith(array,[0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32 ])) {
26
- return {xml: true, encoding: 'utf-16be', offset: 0}
27
- } else if (startsWith(array,[60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0 ])) {
28
- return {xml: true, encoding: 'utf-16le', offset: 0}
29
- }
30
- return {xml: false, encoding: undefined}
31
- }
32
-
33
- function extractNsElement(node) {
34
- const parts = node.name.split(':');
35
- if(parts.length === 1) {
36
- return {name: parts[0], ns: node.attributes['xmlns']};
37
- } else if (parts.length === 2) {
38
- return {name: parts[1], ns: node.attributes[`xmlns:${parts[0]}`]};
39
- }
14
+ if (startsWith(array, [60, 63, 120, 109, 108, 32])) {
15
+ return { xml: true, encoding: 'utf-8', offset: 0 };
16
+ }
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 };
19
+ }
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 };
22
+ }
23
+ else if (startsWith(array, [0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
24
+ return { xml: true, encoding: 'utf-16le', offset: 2 };
25
+ }
26
+ else if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
27
+ return { xml: true, encoding: 'utf-16be', offset: 0 };
28
+ }
29
+ else if (startsWith(array, [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
30
+ return { xml: true, encoding: 'utf-16le', offset: 0 };
31
+ }
32
+ return { xml: false };
40
33
  }
41
-
42
34
  /**
43
35
  * Maps the root element namespace to corresponding file-type
44
36
  */
45
37
  const namespaceMapping = {
46
- 'http://www.w3.org/2000/svg': {
47
- ext: 'svg',
48
- mime: 'image/svg+xml',
49
- },
50
- 'http://www.w3.org/1999/xhtml': {
51
- ext: 'xhtml',
52
- mime: 'application/xhtml+xml',
53
- },
54
- 'http://www.opengis.net/kml/2.2': {
55
- ext: 'kml',
56
- mime: 'application/vnd.google-earth.kml+xml',
57
- },
58
- 'http://www.opengis.net/gml': {
59
- ext: 'gml',
60
- mime: 'application/gml+xml',
61
- }
62
- }
63
-
38
+ 'http://www.w3.org/2000/svg': {
39
+ ext: 'svg',
40
+ mime: 'image/svg+xml'
41
+ },
42
+ 'http://www.w3.org/1999/xhtml': {
43
+ ext: 'xhtml',
44
+ mime: 'application/xhtml+xml'
45
+ },
46
+ 'http://www.opengis.net/kml/2.2': {
47
+ ext: 'kml',
48
+ mime: 'application/vnd.google-earth.kml+xml'
49
+ },
50
+ 'http://www.opengis.net/gml': {
51
+ ext: 'gml',
52
+ mime: 'application/gml+xml'
53
+ }
54
+ };
64
55
  /**
65
56
  * Maps the root element name to corresponding file-type.
66
57
  * Used for Non-namespaced XML
67
58
  * @type {{rss: {ext: string, mime: string}}}
68
59
  */
69
60
  const rootNameMapping = {
70
- rss: {
71
- ext: 'rss',
72
- mime: 'application/rss+xml',
73
- },
74
- 'score-partwise': {
75
- ext: 'musicxml',
76
- mime: 'application/vnd.recordare.musicxml+xml',
77
- },
78
- svg: {
79
- ext: 'svg',
80
- mime: 'image/svg+xml',
81
- },
82
- }
83
-
61
+ rss: {
62
+ ext: 'rss',
63
+ mime: 'application/rss+xml'
64
+ },
65
+ 'score-partwise': {
66
+ ext: 'musicxml',
67
+ mime: 'application/vnd.recordare.musicxml+xml'
68
+ },
69
+ svg: {
70
+ ext: 'svg',
71
+ mime: 'image/svg+xml'
72
+ }
73
+ };
84
74
  export class XmlTextDetector {
85
-
86
- constructor(options) {
87
- this.options = options ?? {};
88
- this.firstTag = true;
89
- this.onEnd = false;
90
- this.parser = sax.parser(true);
91
- this.nesting = 0;
92
-
93
- this.parser.onerror = e => {
94
- if (e.message.startsWith('Invalid character entity')) { // Allow entity reference
95
- return;
96
- }
97
- this.fileType = undefined;
98
- this.onEnd = true;
99
- };
100
- this.parser.onopentag = node => {
101
- ++this.nesting;
102
- if (!this.firstTag || this.onEnd) {
103
- return;
104
- }
105
- this.firstTag = false;
106
- const nsNode = extractNsElement(node);
107
- if (nsNode.ns) {
108
- // Resolve file-type boot root element namespace
109
- this.fileType = namespaceMapping[nsNode.ns.toLowerCase()];
110
- } else {
111
- // Fall back on element name if there is no namespace
112
- this.fileType = rootNameMapping[nsNode.name?.toLowerCase()];
113
- }
114
-
115
- if (this.fileType && !this.options.fullScan) {
75
+ constructor(options) {
76
+ this.options = options ?? {};
77
+ this.firstTag = true;
78
+ this.onEnd = false;
79
+ this.parser = sax.parser(true, { xmlns: true });
80
+ this.nesting = 0;
81
+ this.parser.onerror = e => {
82
+ if (e.message.startsWith('Invalid character entity')) { // Allow entity reference
83
+ return;
84
+ }
85
+ this.fileType = undefined;
86
+ this.onEnd = true;
87
+ };
88
+ this.parser.onopentag = node => {
89
+ ++this.nesting;
90
+ if (!this.firstTag || this.onEnd) {
91
+ return;
92
+ }
93
+ this.firstTag = false;
94
+ if (node.uri) {
95
+ // Resolve file-type boot root element namespace
96
+ this.fileType = namespaceMapping[node.uri];
97
+ }
98
+ else if (node.name) {
99
+ // Fall back on element name if there is no namespace
100
+ this.fileType = rootNameMapping[node.name.toLowerCase()];
101
+ }
102
+ if (this.fileType && !this.options.fullScan) {
103
+ this.onEnd = true;
104
+ }
105
+ };
106
+ this.parser.onclosetag = () => {
107
+ --this.nesting;
108
+ };
109
+ }
110
+ write(text) {
111
+ this.parser.write(text);
112
+ }
113
+ close() {
114
+ this.parser.close();
116
115
  this.onEnd = true;
117
- }
118
- };
119
-
120
- this.parser.onclosetag = () => {
121
- --this.nesting;
122
116
  }
123
- }
124
-
125
- write(text) {
126
- this.parser.write(text);
127
- }
128
-
129
- close() {
130
- this.parser.close();
131
- this.onEnd = true;
132
- }
133
-
134
- isValid() {
135
- return this.nesting === 0;
136
- }
117
+ isValid() {
118
+ return this.nesting === 0;
119
+ }
137
120
  }
138
-
139
- export const detectXml = async tokenizer => {
140
-
141
- const buffer = new Uint8Array(512);
142
-
143
- // Increase sample size from 12 to 256.
144
- await tokenizer.peekBuffer(buffer, {length: 128, mayBeLess: true});
145
-
146
- const {xml, encoding, offset} = isXml(buffer);
147
-
148
- if (xml) {
149
- await tokenizer.ignore(offset);
150
-
151
- const xmlTextDetector = new XmlTextDetector();
152
-
153
- const textDecoder = new TextDecoder(encoding);
154
-
155
- do {
156
- const len = await tokenizer.readBuffer(buffer, {mayBeLess: true});
157
- const portion = buffer.subarray(0, len);
158
- const text = textDecoder.decode(portion);
159
- xmlTextDetector.write(text);
160
- if (len < buffer.length) {
161
- xmlTextDetector.close();
162
- }
163
- } while(!xmlTextDetector.onEnd)
164
-
165
- return xmlTextDetector.fileType ?? {
166
- ext: 'xml',
167
- mime: 'application/xml',
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
+ }
168
146
  }
169
- }
170
-
171
147
  };
172
-
package/package.json CHANGED
@@ -1,48 +1,57 @@
1
1
  {
2
- "name": "@file-type/xml",
3
- "version": "0.2.1",
4
- "description": "XML detection plugin",
5
- "type": "module",
6
- "exports": "./lib/index.js",
7
- "scripts": {
8
- "test": "mocha"
9
- },
10
- "keywords": [
11
- "file-type",
12
- "detect",
13
- "detection",
14
- "detector",
15
- "XML",
16
- "signature",
17
- "namespace",
18
- "SVG",
19
- "XHTML",
20
- "RSS",
21
- "KML",
22
- "GML",
23
- "MusicXML"
24
- ],
25
- "dependencies": {
26
- "sax": "^1.4.1",
27
- "strtok3": "^10.0.1"
28
- },
29
- "devDependencies": {
30
- "chai": "^5.1.2",
31
- "mocha": "^11.0.1"
32
- },
33
- "files": [
34
- "lib/**/*.js",
35
- "lib/**/*.d.ts",
36
- "lib/*.cjs"
37
- ],
38
- "author": {
39
- "name": "Borewit",
40
- "url": "https://github.com/Borewit"
41
- },
42
- "repository": {
43
- "type": "git",
44
- "url": "git+https://github.com/Borewit/file-type-xml.git"
45
- },
46
- "license": "MIT",
47
- "packageManager": "yarn@4.5.3"
2
+ "name": "@file-type/xml",
3
+ "version": "0.4.0",
4
+ "description": "XML detection plugin",
5
+ "type": "module",
6
+ "types": "./lib/index.d.ts",
7
+ "exports": "./lib/index.js",
8
+ "scripts": {
9
+ "clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'src/**/*.d.ts'",
10
+ "compile-src": "tsc -p lib",
11
+ "compile": "yarn run compile-src",
12
+ "build": "yarn run clean && yarn compile",
13
+ "test": "mocha"
14
+ },
15
+ "keywords": [
16
+ "file-type",
17
+ "detect",
18
+ "detection",
19
+ "detector",
20
+ "XML",
21
+ "signature",
22
+ "namespace",
23
+ "SVG",
24
+ "XHTML",
25
+ "RSS",
26
+ "KML",
27
+ "GML",
28
+ "MusicXML"
29
+ ],
30
+ "dependencies": {
31
+ "sax": "^1.4.1",
32
+ "strtok3": "^10.0.1"
33
+ },
34
+ "devDependencies": {
35
+ "@types/sax": "^1.2.7",
36
+ "chai": "^5.1.2",
37
+ "del-cli": "^6.0.0",
38
+ "file-type": "^20.0.0",
39
+ "mocha": "^11.0.1",
40
+ "typescript": "^5.7.3"
41
+ },
42
+ "files": [
43
+ "lib/**/*.js",
44
+ "lib/**/*.d.ts",
45
+ "lib/*.cjs"
46
+ ],
47
+ "author": {
48
+ "name": "Borewit",
49
+ "url": "https://github.com/Borewit"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "git+https://github.com/Borewit/file-type-xml.git"
54
+ },
55
+ "license": "MIT",
56
+ "packageManager": "yarn@4.5.3"
48
57
  }