$ npm install libqp
Encode and decode quoted-printable strings according to RFC2045.
Install with npm
npm install libqp
libqp requires Node.js 20 or newer. It is written in TypeScript and ships both an ES module and a CommonJS build, each with type declarations.
// ES modules
import libqp from 'libqp';
// or
import { encode, decode, wrap, Encoder, Decoder } from 'libqp';
// CommonJS
const libqp = require('libqp');
Encode Buffer objects or unicode strings with
libqp.encode(val) → String
Where
encode escapes whitespace at the end of a line and a hyphen at the start of a line, so the output never contains a line that could be read as a MIME boundary.
Example
libqp.encode('jõgeva');
// j=C3=B5geva
Decode Quoted-Printable encoded strings with
libqp.decode(str) → Buffer
Where
buf.toString('binary')) rather than a unicode oneExample
libqp.decode('j=C3=B5geva').toString();
// jõgeva
Quoted-Printable encoded lines are limited to 76 characters but encode method might return lines longer than the limit.
To enforce soft line breaks on lines longer than 76 (or any other length) characters, use wrap
libqp.wrap(str[, lineLength]) → String
Where
Example
libqp.wrap('abc j=C3=B5geva', 10);
// abc j=\r\n
// =C3=B5geva
libqp makes it possible to encode and decode streams with libqp.Encoder and libqp.Decoder constructors.
Create new Encoder Stream with
const encoder = new libqp.Encoder([options])
Where
false to turn the soft wrapping off completely)true if the input is binary data. A CR or LF that is not part of a CRLF pair is then encoded, so that line ending conversions on the way can not change the dataExample
The following example script reads in a file, encodes it to Quoted-Printable and saves the output to a file.
import fs from 'node:fs';
import { Encoder } from 'libqp';
const source = fs.createReadStream('source.txt');
const encoded = fs.createWriteStream('encoded.txt');
source.pipe(new Encoder()).pipe(encoded);
Create new Decoder Stream with
const decoder = new libqp.Decoder([options])
Where
The Decoder decodes its input byte by byte as it arrives and holds back only what the following input can still change: whitespace that may end a line, a = that may start a soft line break, and an escape sequence that is not complete yet. Escape sequences and soft line breaks split between chunks or lines are decoded the same as with decode() of the whole input.
Example
The following example script reads in a file in Quoted-Printable encoding, decodes it and saves the output to a file.
import fs from 'node:fs';
import { Decoder } from 'libqp';
const encoded = fs.createReadStream('encoded.txt');
const dest = fs.createWriteStream('dest.txt');
encoded.pipe(new Decoder()).pipe(dest);
MIT
© 2010 - cnpmjs.org x YWFE | Home | YWFE