Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions types/node/node-tests/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,33 @@ import {
const r00ts: readonly string[] = rootCertificates;
}

// Certificate DN fields are optional and can be string or string[] (multi-valued)
{
const tlsSocket = connect({});
const peerCert = tlsSocket.getPeerCertificate();
const subject = peerCert.subject;

// Fields are optional and may be string or string[]
const cn: string | string[] | undefined = subject.CN;
const ou: string | string[] | undefined = subject.OU;
const o: string | string[] | undefined = subject.O;
Comment on lines +324 to +326

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future reference, these can be short-handed with dtslint's $ExpectType syntax.

// $ExpectType string | string[] | undefined
subject.CN;

// etc


// Type narrowing with Array.isArray
if (Array.isArray(subject.OU)) {
const ous: string[] = subject.OU;
} else {
const ou: string | undefined = subject.OU;
}

// Arbitrary DN attributes via index signature
const email: string | string[] | undefined = subject["emailAddress"];
const dc: string | string[] | undefined = subject["DC"];

// Issuer has the same shape
const issuer = peerCert.issuer;
const issuerCN: string | string[] | undefined = issuer.CN;
}

{
const _options: TlsOptions = {};
const _server = new Server(_options, (socket) => {});
Expand Down
14 changes: 7 additions & 7 deletions types/node/tls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ declare module "node:tls" {
import * as stream from "stream";
const CLIENT_RENEG_LIMIT: number;
const CLIENT_RENEG_WINDOW: number;
interface Certificate {
interface Certificate extends NodeJS.Dict<string | string[]> {
/**
* Country code.
*/
C: string;
C?: string | string[];
/**
* Street.
*/
ST: string;
ST?: string | string[];
/**
* Locality.
*/
L: string;
L?: string | string[];
/**
* Organization.
*/
O: string;
O?: string | string[];
/**
* Organizational unit.
*/
OU: string;
OU?: string | string[];
/**
* Common name.
*/
CN: string;
CN?: string | string[];
}
interface PeerCertificate {
/**
Expand Down
27 changes: 27 additions & 0 deletions types/node/v20/test/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,33 @@ import {
const r00ts: readonly string[] = rootCertificates;
}

// Certificate DN fields are optional and can be string or string[] (multi-valued)
{
const tlsSocket = connect({});
const peerCert = tlsSocket.getPeerCertificate();
const subject = peerCert.subject;

// Fields are optional and may be string or string[]
const cn: string | string[] | undefined = subject.CN;
const ou: string | string[] | undefined = subject.OU;
const o: string | string[] | undefined = subject.O;

// Type narrowing with Array.isArray
if (Array.isArray(subject.OU)) {
const ous: string[] = subject.OU;
} else {
const ou: string | undefined = subject.OU;
}

// Arbitrary DN attributes via index signature
const email: string | string[] | undefined = subject["emailAddress"];
const dc: string | string[] | undefined = subject["DC"];

// Issuer has the same shape
const issuer = peerCert.issuer;
const issuerCN: string | string[] | undefined = issuer.CN;
}

{
const _options: TlsOptions = {};
const _server = new Server(_options, (socket) => {});
Expand Down
14 changes: 7 additions & 7 deletions types/node/v20/tls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ declare module "tls" {
import * as stream from "stream";
const CLIENT_RENEG_LIMIT: number;
const CLIENT_RENEG_WINDOW: number;
interface Certificate {
interface Certificate extends NodeJS.Dict<string | string[]> {
/**
* Country code.
*/
C: string;
C?: string | string[];
/**
* Street.
*/
ST: string;
ST?: string | string[];
/**
* Locality.
*/
L: string;
L?: string | string[];
/**
* Organization.
*/
O: string;
O?: string | string[];
/**
* Organizational unit.
*/
OU: string;
OU?: string | string[];
/**
* Common name.
*/
CN: string;
CN?: string | string[];
}
interface PeerCertificate {
/**
Expand Down
27 changes: 27 additions & 0 deletions types/node/v22/test/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ import {
const r00ts: readonly string[] = rootCertificates;
}

// Certificate DN fields are optional and can be string or string[] (multi-valued)
{
const tlsSocket = connect({});
const peerCert = tlsSocket.getPeerCertificate();
const subject = peerCert.subject;

// Fields are optional and may be string or string[]
const cn: string | string[] | undefined = subject.CN;
const ou: string | string[] | undefined = subject.OU;
const o: string | string[] | undefined = subject.O;

// Type narrowing with Array.isArray
if (Array.isArray(subject.OU)) {
const ous: string[] = subject.OU;
} else {
const ou: string | undefined = subject.OU;
}

// Arbitrary DN attributes via index signature
const email: string | string[] | undefined = subject["emailAddress"];
const dc: string | string[] | undefined = subject["DC"];

// Issuer has the same shape
const issuer = peerCert.issuer;
const issuerCN: string | string[] | undefined = issuer.CN;
}

{
const _options: TlsOptions = {};
const _server = new Server(_options, (socket) => {});
Expand Down
14 changes: 7 additions & 7 deletions types/node/v22/tls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ declare module "tls" {
import * as stream from "stream";
const CLIENT_RENEG_LIMIT: number;
const CLIENT_RENEG_WINDOW: number;
interface Certificate {
interface Certificate extends NodeJS.Dict<string | string[]> {
/**
* Country code.
*/
C: string;
C?: string | string[];
/**
* Street.
*/
ST: string;
ST?: string | string[];
/**
* Locality.
*/
L: string;
L?: string | string[];
/**
* Organization.
*/
O: string;
O?: string | string[];
/**
* Organizational unit.
*/
OU: string;
OU?: string | string[];
/**
* Common name.
*/
CN: string;
CN?: string | string[];
}
interface PeerCertificate {
/**
Expand Down
27 changes: 27 additions & 0 deletions types/node/v24/test/tls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ import {
const r00ts: readonly string[] = rootCertificates;
}

// Certificate DN fields are optional and can be string or string[] (multi-valued)
{
const tlsSocket = connect({});
const peerCert = tlsSocket.getPeerCertificate();
const subject = peerCert.subject;

// Fields are optional and may be string or string[]
const cn: string | string[] | undefined = subject.CN;
const ou: string | string[] | undefined = subject.OU;
const o: string | string[] | undefined = subject.O;

// Type narrowing with Array.isArray
if (Array.isArray(subject.OU)) {
const ous: string[] = subject.OU;
} else {
const ou: string | undefined = subject.OU;
}

// Arbitrary DN attributes via index signature
const email: string | string[] | undefined = subject["emailAddress"];
const dc: string | string[] | undefined = subject["DC"];

// Issuer has the same shape
const issuer = peerCert.issuer;
const issuerCN: string | string[] | undefined = issuer.CN;
}

{
const _options: TlsOptions = {};
const _server = new Server(_options, (socket) => {});
Expand Down
14 changes: 7 additions & 7 deletions types/node/v24/tls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ declare module "tls" {
import * as stream from "stream";
const CLIENT_RENEG_LIMIT: number;
const CLIENT_RENEG_WINDOW: number;
interface Certificate {
interface Certificate extends NodeJS.Dict<string | string[]> {
/**
* Country code.
*/
C: string;
C?: string | string[];
/**
* Street.
*/
ST: string;
ST?: string | string[];
/**
* Locality.
*/
L: string;
L?: string | string[];
/**
* Organization.
*/
O: string;
O?: string | string[];
/**
* Organizational unit.
*/
OU: string;
OU?: string | string[];
/**
* Common name.
*/
CN: string;
CN?: string | string[];
}
interface PeerCertificate {
/**
Expand Down