Add types for libqp (#43437)

ts enable the libqp functions

Signed-off-by: Chris. Webster <chris@webstech.net>
This commit is contained in:
Chris. Webster 2020-03-31 11:05:50 -07:00 committed by GitHub
parent f42b6f9971
commit 30b7468458
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 0 deletions

31
types/libqp/index.d.ts vendored Normal file
View File

@ -0,0 +1,31 @@
// Type definitions for libqp 1.1
// Project: https://github.com/nodemailer/libqp
// Definitions by: Chris. Webster <https://github.com/webstech>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { Transform, TransformOptions } from "stream";
/** Encodes a Buffer or String into a Quoted-Printable encoded string */
export function encode(buffer: string | Buffer): string;
/** Decodes a Quoted-Printable encoded string to a Buffer object */
export function decode(input: string): Buffer;
/** Adds soft line breaks to a Quoted-Printable string */
export function wrap(str: string, lineLength?: number): string;
/** Extend options to add lineLength */
export interface EncoderOptions extends TransformOptions {
lineLength?: number;
}
/** Create a transform stream for encoding data to Quoted-Printable encoding */
export class Encoder extends Transform {
constructor(opts?: EncoderOptions);
}
/** Create a transform stream for decoding Quoted-Printable encoded strings */
export class Decoder extends Transform {
constructor(opts?: TransformOptions);
}

View File

@ -0,0 +1,40 @@
import * as libqp from "libqp";
import { Readable, Writable } from "stream";
const encodeText = "jõgeva";
const encoded = libqp.encode(encodeText);
console.log(libqp.encode(encodeText));
const decodeText =
`This string contains quoted printables for the various ranges of utf-8.
=31=32=33=34
two byte /=[CDcd][0-9A-Fa-f]/=c2=a9
three byte /=[Ee][0-9A-Fa-f]/=e1=99=ad
four byte /=[Ff][0-7]/=f0=90=8d=88
`;
const body = libqp.decode(decodeText);
const wrapped = libqp.wrap(`abc ${encoded}`, 10);
const encoder = new libqp.Encoder();
const decoder = new libqp.Decoder();
const buffer = Buffer.from(encodeText);
const readable = new Readable();
readable._read = () => {}; // _read is required but you can noop it
readable.push(buffer);
readable.push(null);
const writable = new Writable({
// tslint:disable-next-line: variable-name
write(chunk, _encoding, callback) {
console.log(chunk.toString());
callback();
}
});
readable.pipe(encoder);
encoder.pipe(decoder);
decoder.pipe(writable);

23
types/libqp/tsconfig.json Normal file
View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"libqp-tests.ts"
]
}

1
types/libqp/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }