0.3.1 • Published 4 years ago
mime-info-stream-parser v0.3.1
mime-info-stream-parser
Convert freedesktop mime info XML to a simple JSON dictionary.
The freedesktop project provides an XML spec and 700+ mime type descriptions.
The parser implements a transform stream using sax js internally, converting freedesktop mime info XML to a JSON stream. Written to file, the output can be used:
// tsconfig.json compilerOptions.resolveJsonModule = true
import db from './db.json';
console.log(db['text/plain']);
/*
{
comment: 'plain text document',
}
*/
console.log(db['application/pdf']);
/*
{
comment: 'PDF document',
acronym: 'PDF',
acronymExpanded: 'Portable Document Format'
}
*/
// PDF alias
console.log(db['image/pdf']);
/*
{
comment: 'PDF document',
acronym: 'PDF',
acronymExpanded: 'Portable Document Format'
}
*/
Mime type aliases are expanded to full entries. Each entry value is of type:
export interface MimeInfoItem {
comment: string;
acronym?: string;
acronymExpanded?: string;
}
import { MimeInfoItem } from 'mime-info-stream-parser';
Usage
yarn add mime-info-stream-parser
Typescript
import { pipeline } from 'stream';
import { createWriteStream, createReadStream } from 'fs';
import { MimeInfoStreamParser } from 'mime-info-stream-parser';
const read = createReadStream('./freedesktop.org.xml.in', {
encoding: 'utf-8',
});
const write = createWriteStream('./db.json', {
encoding: 'utf-8',
});
const mimeInfo = new MimeInfoStreamParser();
pipeline(read, mimeInfo, write, (err) => {
if (err) {
console.error(err.message);
}
});
Node.js
const { MimeInfoStreamParser } = require('mime-info-stream-parser');
// ...
Development
$ yarn && yarn test
$ yarn describe
info:
Display information about the package scripts
build:
Clean and rebuild the project
fix:
Try to automatically fix any linting problems
test:
Lint and unit test the project
watch:
Watch and rebuild the project on save, then rerun relevant tests
cov:
Rebuild, run tests, then create and open the coverage report
reset:
Delete all untracked files and reset the repo to the last commit
License
MIT