plist.js

repository·master·Indexed 20 days ago

https://github.com/tootallnate/plist.js

A lightweight Apple property list (plist) parser and builder for Node.js and browsers. Version 5.0.0 supports XML, binary (bplist00), and OpenStep formats with automatic format detection. It provides functions to parse plists into JavaScript objects via `parse()`, `parseBinary()`, and `parseOpenStep()`, and build plists from JavaScript objects using `build()` for XML and `buildBinary()` for binary output.

Tokens
4.7K
Snippets
25
Records
30
Agent score
68%

What's inside plist

  1. Understand plist Type Mapping

    master

    When parsing or building, the following mapping between Plist types and JavaScript types is used:

    Plist TypeJavaScript Type
    <string>string
    <integer>number
    <real>number
    <true/> / <false/>boolean
    <date>Date
    <data>Uint8Array
    <array>Array
    <dict>Object
  2. Quick Start with plist.js

    master

    You can quickly parse any supported plist format (XML, binary, or OpenStep) using parse() and build an XML plist from a JavaScript object using build().

    import { parse, build } from 'plist';
    
    // Parse any plist format (auto-detected)
    const obj = parse('<plist version="1.0"><string>Hello!</string></plist>');
    console.log(obj); // "Hello!"
    
    // Build an XML plist from a JS object
    const xml = build({ name: 'My App', version: 42 });
    console.log(xml);
  3. Use plist.js in the browser

    master

    In bundled applications (Vite, webpack, etc.), you can import plist normally. The library uses conditional exports to automatically select a browser-optimized build that uses native DOMParser and avoids heavy dependencies like @xmldom/xmldom or xmlbuilder.

    import { parse, build } from 'plist';
  4. Type mapping for XML plist generation

    master

    When using build(), JavaScript types are mapped to specific XML plist tags as follows:

    • String: <string>
    • Number (Integer): <integer> (if n % 1 === 0)
    • Number (Float): <real> (if n % 1 !== 0)
    • BigInt: <integer>
    • Boolean: <true/> or <false/>
    • Date: <date> (formatted as an ISO 8601 string without milliseconds, e.g., YYYY-MM-DDTHH:mm:ssZ)
    • Array: <array>
    • Object/Dictionary: <dict> containing <key> elements
    • Binary Data (ArrayBuffer, Uint8Array, Buffer, or other ArrayBufferView): <data> (encoded as a Base64 string)
  5. Build XML and Binary plists

    master

    Convert JavaScript objects into plist formats.

    XML Output

    Use build(obj, opts?) to generate an XML string. By default, it uses pretty-printing.

    Binary Output

    Use buildBinary(obj) to generate a Uint8Array containing a binary plist (bplist00).

    import { writeFileSync } from 'node:fs';
    import { build, buildBinary } from 'plist';
    
    // Build XML
    const xml = build({
      CFBundleName: 'My App',
      CFBundleVersion: '1.0',
      LSRequiresIPhoneOS: true,
      UISupportedInterfaceOrientations: [
        'UIInterfaceOrientationPortrait',
        'UIInterfaceOrientationLandscapeLeft',
      ],
    });
    
    // Build Binary
    const data = buildBinary({
      CFBundleName: 'My App',
      CFBundleVersion: '1.0',
    });
    writeFileSync('Info.plist', data);
  6. Parse XML, Binary, and OpenStep plists

    master

    The parse() function automatically detects the format if you provide the correct input type. For specific formats, you can use dedicated methods.

    XML Parsing

    Pass the XML string to parse().

    Binary Parsing

    Binary plists (bplist00) are auto-detected when passed as a Uint8Array or ArrayBuffer. Alternatively, use parseBinary().

    OpenStep Parsing

    OpenStep/ASCII formats are auto-detected when the input starts with { or (. Alternatively, use parseOpenStep().

    import { readFileSync } from 'node:fs';
    import { parse, parseBinary, parseOpenStep } from 'plist';
    
    // XML
    const xml = readFileSync('Info.plist', 'utf8');
    const obj = parse(xml);
    
    // Binary (Auto-detected or explicit)
    const buf = readFileSync('Info.plist');
    const obj2 = parse(new Uint8Array(buf));
    const obj3 = parseBinary(new Uint8Array(buf));
    
    // OpenStep (Auto-detected or explicit)
    const obj4 = parse('{ CFBundleName = "My App"; CFBundleVersion = 42; }');
    const obj5 = parseOpenStep('( item1, item2, item3 )');
  7. Configure `build()` output with `BuildOptions`

    master

    The build function accepts an optional BuildOptions object to control the formatting of the generated XML string.

    OptionTypeDefaultDescription
    prettybooleantrueWhether to format the XML with indentation and newlines. Set to false to disable.
    indentstring-The string used for indentation.
    newlinestring-The string used for newlines.
    import { build } from 'plist';
    
    const obj = { name: 'example' };
    
    // Example with custom indentation and no pretty-printing
    const xml = build(obj, {
      pretty: false,
      indent: '  ',
      newline: '\n'
    });
  8. Configure XML plist output with BuildOptions

    master

    The build function accepts an optional BuildOptions object to control the formatting of the generated XML string.

    Options

    • pretty (boolean, optional): Determines if the output is indented. Defaults to true if not provided or if set to true. Set to false for a minified output.
    • indent (string, optional): The string used for indentation when pretty is enabled. Defaults to ' ' (two spaces).
    • newline (string, optional): The string used for line separators when pretty is enabled. Defaults to \n.
    • [key: string]: unknown: Allows for additional properties.
    import { build } from 'plist';
    
    const obj = { name: 'example' };
    
    // Minified output
    const minified = build(obj, { pretty: false });
    
    // Custom indentation and newlines
    const custom = build(obj, {
      pretty: true,
      indent: '\t',
      newline: '\r\n'
    });
  9. Reference: plist API methods

    master

    The following methods are available for parsing and building plists.

    ### `parse(input)`
    Parse a plist. Format is auto-detected.
    - **input**: `string | Uint8Array | ArrayBuffer`
    - **returns**: `PlistValue`
    
    ### `parseBinary(data)`
    Parse a binary plist (bplist00).
    - **data**: `Uint8Array`
    - **returns**: `PlistValue`
    
    ### `parseOpenStep(input)`
    Parse an OpenStep/ASCII plist.
    - **input**: `string`
    - **returns**: `PlistValue`
    
    ### `build(obj, opts?)`
    Build an XML plist string.
    - **obj**: `PlistValue`
    - **opts.pretty**: `boolean` (default: `true`)
    - **opts.indent**: `string` (default: `"  "`)
    - **opts.newline**: `string` (default: `"\n"`)
    - **returns**: `string`
    
    ### `buildBinary(obj)`
    Build a binary plist (bplist00).
    - **obj**: `PlistValue`
    - **returns**: `Uint8Array`
  10. Parse plists using parse()

    master

    The parse function is the primary entry point for decoding various plist formats into a JavaScript object. It automatically detects the format based on the input type and content:

    • XML Plists: Standard XML strings or buffers starting with <plist.
    • Binary Plists: ArrayBuffer or Uint8Array inputs, or strings starting with the bplist prefix.
    • OpenStep/ASCII Plists: Strings starting with { or ( that do not contain XML declarations.

    Returns a PlistValue representing the decoded data.

    import { parse } from 'plist';
    
    // Parsing an XML string
    const xml = `<plist><dict><key>example</key><string>value</string></dict></plist>`;
    const data = parse(xml);
    
    // Parsing a binary plist (Uint8Array)
    const binaryData = new Uint8Array([...]);
    const dataFromBinary = parse(binaryData);
  11. Build XML plists with build()

    master

    Use build() to convert a JavaScript object into an XML plist string.

    import { build } from 'plist';
    
    const obj = { Hello: 'World' };
    const xml = build(obj);
    // Output: '<plist version="1.0"><dict><key>Hello</key><string>World</string></dict></plist>'