diff --git a/types/rfc2253/.npmignore b/types/rfc2253/.npmignore new file mode 100644 index 00000000000000..93e307400a5456 --- /dev/null +++ b/types/rfc2253/.npmignore @@ -0,0 +1,5 @@ +* +!**/*.d.ts +!**/*.d.cts +!**/*.d.mts +!**/*.d.*.ts diff --git a/types/rfc2253/index.d.ts b/types/rfc2253/index.d.ts new file mode 100644 index 00000000000000..6509afb795710b --- /dev/null +++ b/types/rfc2253/index.d.ts @@ -0,0 +1,48 @@ +/// + +export class DistinguishedName { + constructor(rdns?: ReadonlyArray); + has(key: number | Buffer | string): boolean; + get(key: number | Buffer | string): RelativeDistinguishedName; + getAll(key: number | Buffer | string): RelativeDistinguishedName[]; + set(key: number | Buffer | string, value: RelativeDistinguishedName | Buffer | string): number; + delete(key: number | Buffer | string): RelativeDistinguishedName[]; + push(rdn: RelativeDistinguishedName): number; + pop(): RelativeDistinguishedName | undefined; + unshift(rdn: RelativeDistinguishedName): number | undefined; + shift(): RelativeDistinguishedName | undefined; + count(): number; + match(dn: DistinguishedName): boolean; + format(): string; + toString(): string; +} + +export class RelativeDistinguishedName { + constructor(map?: DistinguishedName | RelativeDistinguishedName); + has(key: Buffer | string): boolean; + get(key: Buffer | string): Buffer | string; + set(key: Buffer | string, value: Buffer | string): void; + delete(key: Buffer | string): boolean; + count(): number; + match(rdn: RelativeDistinguishedName): boolean; + format(): string; + toString(): string; +} + +/** + * Escapes an attribute key or value and returns the escaped string. + * @param value The value to escape. If the value is a {@link Buffer} it will be formatted as an octothorpe (#) followed by the hexadecimal representation of each byte in the buffer. Otherwise the value will be converted to a string and escaped according to RFC 2253. + */ +export function escape(value: Buffer | string): string; + +/** + * Formats a {@link DistinguishedName} or {@link RelativeDistinguishedName} instance according to RFC 2253 and returns a UTF-8 encoded string. + * @param dn The distinguished name or relative distinguished name to format as a string. + */ +export function format(dn: ReadonlyArray | DistinguishedName): string; + +/** + * Parses an RFC 2253 string representation of a distinguished name and returns a {@link DistinguishedName} object. + * @param seq A UTF-8 encoded distinguished name. + */ +export function parse(seq: string): DistinguishedName; diff --git a/types/rfc2253/package.json b/types/rfc2253/package.json new file mode 100644 index 00000000000000..ff7b8f011e1183 --- /dev/null +++ b/types/rfc2253/package.json @@ -0,0 +1,20 @@ +{ + "private": true, + "name": "@types/rfc2253", + "version": "0.2.9999", + "projects": [ + "https://github.com/foss-haas/rfc2253" + ], + "dependencies": { + "@types/node": "*" + }, + "devDependencies": { + "@types/rfc2253": "workspace:." + }, + "owners": [ + { + "name": "Arnd Issler", + "githubUsername": "arndissler" + } + ] +} diff --git a/types/rfc2253/rfc2253-tests.ts b/types/rfc2253/rfc2253-tests.ts new file mode 100644 index 00000000000000..88ca632a4d54fc --- /dev/null +++ b/types/rfc2253/rfc2253-tests.ts @@ -0,0 +1,50 @@ +import { Buffer } from "node:buffer"; +import * as rfc2253 from "rfc2253"; + +const str = "CN=John Doe,OU=People,DC=example,DC=com"; + +// $ExpectType DistinguishedName +const dn = rfc2253.parse(str); + +// $ExpectType DistinguishedName +const dn2 = rfc2253.parse(str); + +// $ExpectType string +const str2 = rfc2253.format(dn); + +// $ExpectType string +const escapedString = rfc2253.escape("John Doe"); + +// $ExpectType string +const escapedBuffer = rfc2253.escape(new Buffer([1, 2, 3, 16, 255])); + +// $ExpectType boolean +dn.match(dn2); + +// $ExpectType DistinguishedName +const distinguishedName = new rfc2253.DistinguishedName(); + +// $ExpectType RelativeDistinguishedName +const relativeDistinguishedName = new rfc2253.RelativeDistinguishedName(); + +let dnWayneEnterprises = rfc2253.parse( + "CN=Wayne\\, Bruce,DC=Wayne Enterprises,DC=com,OU=Research and Development,OU=Gadget Services", +); + +// $ExpectType RelativeDistinguishedName[] +const ous = dnWayneEnterprises.getAll("OU"); + +// @ts-expect-error +const failParse = rfc2253.parse(); + +// @ts-expect-error +const failFormat = rfc2253.format(); + +// @ts-expect-error +const failEscape = rfc2253.escape(); + +// @ts-expect-error +const failDistinguishedName = new rfc2253.DistinguishedName({}); + +// @ts-expect-error +const failRelativeDistinguishedName = new rfc2253.RelativeDistinguishedName({}); diff --git a/types/rfc2253/tsconfig.json b/types/rfc2253/tsconfig.json new file mode 100644 index 00000000000000..909312470bf335 --- /dev/null +++ b/types/rfc2253/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "module": "node16", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "rfc2253-tests.ts"] +}