@file-type/xml

中文文档
1.2.0发布于 2026/10/06MITTypeScriptES Module近 30 天下载 87.8万

XML detection plugin for file-type

原始使用文档
文档目录4 节
  1. Installation
  2. Usage
  3. Support file formats
  4. Licence

NPM version Node.js CI npm downloads

@file-type/xml

Detector plugin for file-type for XML files.

Installation

npm install @file-type/xml

Usage

The following example shows how to add the XML detector to file-type.

import {FileTypeParser} from 'file-type';
import {detectXml} from '@file-type/xml';

const parser = new FileTypeParser({customDetectors: [detectXml]});
const fileType = await parser.fromFile('example.kml');
console.log(fileType);

With file-type 22, Node.js 22 or newer is required. For stream detection, pass a web ReadableStream to parser.fromStream(). Convert a Node.js Readable with Readable.toWeb():

import {createReadStream} from 'node:fs';
import {Readable} from 'node:stream';

const fileType = await parser.fromStream(Readable.toWeb(createReadStream('example.kml')));

The detector inspects at most 16 KiB, including any byte order mark. To change this limit, create a detector with a sampleSize in bytes:

import {FileTypeParser} from 'file-type';
import {createXmlDetector} from '@file-type/xml';

const parser = new FileTypeParser({
    customDetectors: [createXmlDetector({sampleSize: 64 * 1024})]
});

The limit applies to both prolog detection and XML parsing. If no XML signature is found within the sample, the detector returns undefined without consuming input. If XML is recognized but its specific format cannot be determined within the sample, it returns application/xml. sampleSize must be a positive safe integer. The file-type stream sampling option is separate; when using fileTypeStream, set its sampleSize as well if you want a larger sample.

You can also use the XML detector outside file-type:

import {XmlTextDetector} from '@file-type/xml';

const xmlTextDetector = new XmlTextDetector();
xmlTextDetector.write('<svg xmlns="http://www.w3.org/2000/svg"><path fill="#00CD9F"/></svg>');
const fileType = xmlTextDetector.fileType;
console.log(JSON.stringify(fileType)); // Outputs: {"ext":"svg","mime":"image/svg+xml"}

Support file formats

Licence

This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.

版本与发布记录

@file-type/xml 共发布 15 个版本,首次发布于 2024/12/18;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.2.0latest2026/10/061 个月前
1.1.12026/05/046 个月前
1.1.02026/02/158 个月前
1.0.02026/02/148 个月前
0.5.02026/02/149 个月前
0.4.42025/09/011.2 年前

关于 @file-type/xml 的常见问题

@file-type/xml 是什么?

XML detection plugin for file-type

如何安装 @file-type/xml?

在项目目录运行 npm install @file-type/xml,或使用 pnpm add / yarn add / bun add。

@file-type/xml 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@file-type/xml 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@file-type/xml 可以商用吗?

@file-type/xml 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@file-type/xml 的下载量有多少?

根据 npm 官方下载统计,@file-type/xml 在 2026/10/15 – 2026/11/13 这 30 天内被下载 877,833 次,日均约 29,261 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@file-type/xml 还在维护吗?

最新版本 1.2.0 发布于 2026/10/06(1 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 5 个;首次发布于 2024/12/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。