Skip to content

Commit 6a2b01e

Browse files
committed
fix: use env-agnostic std libraries
1 parent 5e32862 commit 6a2b01e

4 files changed

Lines changed: 12 additions & 17 deletions

File tree

src/client.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import axios, { AxiosInstance } from 'axios';
77
import https from 'https';
88
import { SignJWT, jwtVerify } from 'jose';
9-
import { URL, URLSearchParams } from 'url';
109
import * as constants from './constants';
1110
import { DuoException } from './duo-exception';
1211
import {
@@ -89,9 +88,8 @@ export class Client {
8988
throw new DuoException(constants.INVALID_CLIENT_SECRET_ERROR);
9089

9190
if (apiHost === '') throw new DuoException(constants.PARSING_CONFIG_ERROR);
92-
9391
try {
94-
new URL(redirectUrl);
92+
new globalThis.URL(redirectUrl);
9593
} catch {
9694
throw new DuoException(constants.PARSING_CONFIG_ERROR);
9795
}
@@ -214,7 +212,7 @@ export class Client {
214212
try {
215213
const { data } = await this.axios.post<HealthCheckResponse>(
216214
this.HEALTH_CHECK_ENDPOINT,
217-
new URLSearchParams(request),
215+
new globalThis.URLSearchParams(request),
218216
);
219217
const { stat } = data;
220218

@@ -271,7 +269,7 @@ export class Client {
271269
scope: 'openid',
272270
};
273271

274-
return `${this.baseURL}${this.AUTHORIZE_ENDPOINT}?${new URLSearchParams(query).toString()}`;
272+
return `${this.baseURL}${this.AUTHORIZE_ENDPOINT}?${new globalThis.URLSearchParams(query).toString()}`;
275273
}
276274

277275
/**
@@ -307,7 +305,7 @@ export class Client {
307305
try {
308306
const { data } = await this.axios.post<TokenResponse>(
309307
this.TOKEN_ENDPOINT,
310-
new URLSearchParams(request),
308+
new globalThis.URLSearchParams(request),
311309
{
312310
headers: {
313311
'user-agent': `${constants.USER_AGENT} node/${process.versions.node} v8/${process.versions.v8}`,

src/duo-exception.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class DuoException extends Error {
88
super(message);
99

1010
this.name = 'DuoException';
11-
Error.captureStackTrace(this, this.constructor);
1211

1312
if (error) this.inner = error;
1413
}

src/util.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
//
44
// SPDX-License-Identifier: MIT
55

6-
import { randomBytes } from 'crypto';
7-
8-
function bytesToBase64url(s: Buffer): string {
9-
return s.toString('base64url');
10-
}
11-
12-
export const generateRandomString = (length: number): string => {
13-
return bytesToBase64url(randomBytes(length)).substring(0, length);
6+
export const generateRandomString = (length: number) => {
7+
const arr = new Uint8Array((length % 2 ? length + 1 : length) / 2);
8+
globalThis.crypto.getRandomValues(arr);
9+
return Array.from(arr, (dec) => dec.toString(16).padStart(2, '0'))
10+
.join('')
11+
.slice(0, length);
1412
};
1513

1614
export const getTimeInSeconds = (date = new Date(Date.now())): number =>

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"compilerOptions": {
33
"baseUrl": ".",
44
"outDir": "dist",
5-
"target": "ES2019",
5+
"target": "ES2022",
66
"module": "ESNext",
77
"moduleResolution": "bundler",
88
"allowJs": false,
9-
"lib": ["ES2019"],
9+
"lib": ["ES2022"],
1010
"strict": true,
1111
"esModuleInterop": true,
1212
"allowSyntheticDefaultImports": true,

0 commit comments

Comments
 (0)