Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subsequent calls to sslChecker, fixes #24 #113

Merged
merged 1 commit into from
Nov 4, 2020
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
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ const getSslDetails = async (hostname: string) =>

## Options

| Option | Default | |
| ------ | ------- | -------------------------------------------------- |
| method | HEAD | can be GET too |
| port | 443 | Your ssl entrypoint |
| agent | | I dont know why but if you'd like provide agent id |
All valid `https.RequestOptions` values.

| Option | Default | Description |
| ------------------ | ------- | -------------------------------------------------- |
| method | HEAD | Can be GET too |
| port | 443 | Your SSL/TLS entry point |
| agent | default | Default HTTPS agent with { maxCachedSessions: 0 } |
| rejectUnauthorized | false | Skips authorization by default |

```ts
sslChecker("dyaa.me", { method: "GET", port: 443 }).then(console.info);
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ describe("sslChecker", () => {
);
});

it("Should work on subsequent calls for the same domain", async () => {
await sslChecker(validSslHost);
await new Promise((r) => setTimeout(r, 1000));
const sslDetails = await sslChecker(validSslHost);

expect(sslDetails).toEqual(
expect.objectContaining({
valid: true,
})
);
});

it("Should return valid = false when provided an expired domain", async () => {
const sslDetails = await sslChecker(expiredSSlHost);

Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ const getDaysRemaining = (validFrom: Date, validTo: Date): number => {
return daysRemaining;
};

const DEFAULT_OPTIONS: Partial<https.RequestOptions> = {
agent: new https.Agent({
maxCachedSessions: 0
}),
method: "HEAD",
port: 443,
rejectUnauthorized: false,
};

const sslChecker = (
host: string,
options: Partial<https.RequestOptions> = {
agent: false,
method: "HEAD",
port: 443,
rejectUnauthorized: false,
}
options: Partial<https.RequestOptions> = {}
): Promise<IResolvedValues> =>
new Promise((resolve, reject) => {
options = Object.assign({}, DEFAULT_OPTIONS, options);

if (!checkPort(options.port)) {
reject(Error("Invalid port"));
return;
}

try {
Expand Down