Fast PLIST Parser
fast-plist is a JavaScript library designed for rapidly parsing Apple Property List (PLIST) files. Currently at version 0.1.3, this package has a very low release cadence, with its last update approximately three years ago. It focuses solely on parsing XML-formatted PLISTs efficiently, providing a straightforward API for converting PLIST XML strings into JavaScript objects. While it is maintained by Microsoft (suggesting reliability for its niche), its lack of recent updates means it may not incorporate the latest performance optimizations or features found in more actively developed PLIST parsing libraries, nor does it explicitly support binary PLIST formats. It ships with TypeScript type definitions, making it compatible with modern TypeScript projects, though its primary usage examples are CommonJS-based.
Common errors
-
Error: Near offset X: expected < ~~~bad string~~~
cause Attempting to parse an input string that is not a valid XML-formatted PLIST.fixEnsure the input string is a well-formed XML PLIST. The parser is strict and expects valid XML structure from the beginning. -
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/project/node_modules/fast-plist/index.js from ... not supported
cause You are trying to `require()` this CommonJS package within an ECMAScript Module (ESM) context in Node.js, and Node.js's interoperability could not handle it, or a bundler issue.fixIf in an ESM file, change `const parse = require('fast-plist').parse;` to `import { parse } from 'fast-plist';`. If this persists with bundlers, ensure your build configuration is set up to correctly handle CommonJS modules within an ESM project, or consider downgrading your module system to CommonJS if possible for the specific file.
Warnings
- gotcha The package version 0.1.3 is quite old (last published 3 years ago). This may lead to compatibility issues with newer Node.js versions or browser environments, and it might lack patches for potential security vulnerabilities discovered since its last release.
- gotcha The package primarily exports CommonJS modules. When used in an ECMAScript Module (ESM) project, you might encounter `ERR_REQUIRE_ESM` errors, especially in Node.js environments without proper transpilation or direct ESM support for CommonJS modules. While bundlers often handle this, direct Node.js ESM execution might fail.
- gotcha This library only supports XML-based PLIST parsing. It does not provide functionality for parsing or generating binary PLIST (`bplist`) files, which are often used by Apple for performance reasons. Attempting to parse a binary PLIST will result in a parsing error.
Install
-
npm install fast-plist -
yarn add fast-plist -
pnpm add fast-plist
Imports
- parse
const parse = require('fast-plist');import { parse } from 'fast-plist';
Quickstart
import { parse } from 'fast-plist';
const plistString = `
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>name</key>
<string>Brogrammer</string>
<key>settings</key>
<dict>
<key>background</key>
<string>#1a1a1a</string>
<key>caret</key>
<string>#ecf0f1</string>
<key>foreground</key>
<string>#ecf0f1</string>
<key>invisibles</key>
<string>#F3FFB51A</string>
<key>lineHighlight</key>
<string>#2a2a2a</string>
</dict>
</dict>
</plist>`;
try {
const parsedData = parse(plistString);
console.log(JSON.stringify(parsedData, null, 2));
} catch (error) {
console.error('Failed to parse PLIST:', error.message);
}
// Example of parsing an invalid string (will throw an error)
try {
parse(`bad string`);
} catch (error) {
console.error('Error parsing bad string:', error.message);
}