Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#71545 [@types/rfc2253] introduce typings f…
Browse files Browse the repository at this point in the history
…or rfc2253 by @arndissler
  • Loading branch information
arndissler authored Jan 16, 2025
1 parent db65e0a commit dad6b15
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
5 changes: 5 additions & 0 deletions types/rfc2253/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!**/*.d.ts
!**/*.d.cts
!**/*.d.mts
!**/*.d.*.ts
48 changes: 48 additions & 0 deletions types/rfc2253/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// <reference types="node" />

export class DistinguishedName {
constructor(rdns?: ReadonlyArray<RelativeDistinguishedName>);
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<RelativeDistinguishedName> | 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;
20 changes: 20 additions & 0 deletions types/rfc2253/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
50 changes: 50 additions & 0 deletions types/rfc2253/rfc2253-tests.ts
Original file line number Diff line number Diff line change
@@ -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({});
14 changes: 14 additions & 0 deletions types/rfc2253/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}

0 comments on commit dad6b15

Please sign in to comment.