mime-info-stream-parser
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

mime-info-stream-parser

Npm Version Build Status Coverage Status

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

Readme

Keywords

Package Sidebar

Install

npm i mime-info-stream-parser

Weekly Downloads

1

Version

0.3.1

License

MIT

Unpacked Size

57.2 kB

Total Files

21

Last publish

Collaborators

  • efcmeg