@file-type/xml 0.3.0 → 0.4.1

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
@@ -17,15 +17,16 @@ The following example shows how add the XML detector to [file-type](https://gith
17
17
  import {NodeFileTypeParser} from 'file-type';
18
18
  import {detectXml} from '@file-type/xml';
19
19
 
20
- const parser = new NodeFileTypeParser({customDetectors: [detectXml]});
20
+ const parser = new FileTypeParser({customDetectors: [detectXml]});
21
21
  const fileType = await parser.fromFile('example.kml');
22
22
  console.log(fileType);
23
23
  ```
24
24
 
25
- You can also use the XML detector outside file-type:
25
+ You can also use the XML detector outside [file-type](https://github.com/sindresorhus/file-type):
26
26
  ```js
27
27
  import {XmlTextDetector} from '@file-type/xml';
28
28
 
29
+ const xmlTextDetector = new XmlTextDetector();
29
30
  xmlTextDetector.write('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
30
31
  const fileType = xmlTextDetector.fileType;
31
32
  console.log(JSON.stringify(fileType)); // Outputs: {"ext":"svg","mime":"image/svg+xml"}
package/lib/index.d.ts CHANGED
@@ -1,12 +1,4 @@
1
- import { ITokenizer } from 'strtok3';
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, Detector } from 'file-type';
10
2
  interface IXmlTextDetectorOptions {
11
3
  fullScan?: boolean;
12
4
  }
package/lib/index.js CHANGED
@@ -14,32 +14,23 @@ function isXml(array) {
14
14
  if (startsWith(array, [60, 63, 120, 109, 108, 32])) {
15
15
  return { xml: true, encoding: 'utf-8', offset: 0 };
16
16
  }
17
- else if (startsWith(array, [0xEF, 0xBB, 0xBF, 60, 63, 120, 109, 108, 32])) { // UTF-8 BOM
17
+ if (startsWith(array, [0xEF, 0xBB, 0xBF, 60, 63, 120, 109, 108, 32])) { // UTF-8 BOM
18
18
  return { xml: true, encoding: 'utf-8', offset: 3 };
19
19
  }
20
- else if (startsWith(array, [0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
20
+ if (startsWith(array, [0xFE, 0xFF, 0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
21
21
  return { xml: true, encoding: 'utf-16be', offset: 2 };
22
22
  }
23
- else if (startsWith(array, [0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
23
+ if (startsWith(array, [0xFF, 0xFE, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
24
24
  return { xml: true, encoding: 'utf-16le', offset: 2 };
25
25
  }
26
- else if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
26
+ if (startsWith(array, [0, 60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32])) {
27
27
  return { xml: true, encoding: 'utf-16be', offset: 0 };
28
28
  }
29
- else if (startsWith(array, [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
29
+ if (startsWith(array, [60, 0, 63, 0, 120, 0, 109, 0, 108, 0, 32, 0])) {
30
30
  return { xml: true, encoding: 'utf-16le', offset: 0 };
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
- const nsNode = extractNsElement(node);
104
- if (nsNode?.ns) {
94
+ if (node.uri) {
105
95
  // Resolve file-type boot root element namespace
106
- this.fileType = namespaceMapping[nsNode.ns.toLowerCase()];
96
+ this.fileType = namespaceMapping[node.uri];
107
97
  }
108
- else if (nsNode && nsNode.name) {
98
+ else if (node.name) {
109
99
  // Fall back on element name if there is no namespace
110
- this.fileType = rootNameMapping[nsNode.name.toLowerCase()];
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 = async (tokenizer) => {
132
- const buffer = new Uint8Array(512);
133
- // Increase sample size from 12 to 256.
134
- await tokenizer.peekBuffer(buffer, { length: 128, mayBeLess: true });
135
- const xmlDetection = isXml(buffer);
136
- if (xmlDetection.xml) {
137
- await tokenizer.ignore(xmlDetection.offset);
138
- const xmlTextDetector = new XmlTextDetector();
139
- const textDecoder = new TextDecoder(xmlDetection.encoding);
140
- do {
141
- const len = await tokenizer.readBuffer(buffer, { mayBeLess: true });
142
- const portion = buffer.subarray(0, len);
143
- const text = textDecoder.decode(portion);
144
- xmlTextDetector.write(text);
145
- if (len < buffer.length) {
146
- xmlTextDetector.close();
147
- }
148
- } while (!xmlTextDetector.onEnd);
149
- return xmlTextDetector.fileType ?? {
150
- ext: 'xml',
151
- mime: 'application/xml'
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,7 +1,7 @@
1
1
  {
2
2
  "name": "@file-type/xml",
3
- "version": "0.3.0",
4
- "description": "XML detection plugin",
3
+ "version": "0.4.1",
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,6 +9,7 @@
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
14
  "test": "mocha"
14
15
  },
@@ -29,15 +30,16 @@
29
30
  ],
30
31
  "dependencies": {
31
32
  "sax": "^1.4.1",
32
- "strtok3": "^10.0.1"
33
+ "strtok3": "^10.2.1"
33
34
  },
34
35
  "devDependencies": {
36
+ "@biomejs/biome": "^1.9.4",
35
37
  "@types/sax": "^1.2.7",
36
38
  "chai": "^5.1.2",
37
39
  "del-cli": "^6.0.0",
38
- "file-type": "^19.6.0",
39
- "mocha": "^11.0.1",
40
- "typescript": "^5.7.2"
40
+ "file-type": "^20.1.0",
41
+ "mocha": "^11.1.0",
42
+ "typescript": "^5.7.3"
41
43
  },
42
44
  "files": [
43
45
  "lib/**/*.js",
@@ -53,5 +55,5 @@
53
55
  "url": "git+https://github.com/Borewit/file-type-xml.git"
54
56
  },
55
57
  "license": "MIT",
56
- "packageManager": "yarn@4.5.3"
58
+ "packageManager": "yarn@4.6.0"
57
59
  }