Skip to content

Commit

Permalink
code updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Apr 2, 2024
1 parent 4a683ca commit 6c2021f
Show file tree
Hide file tree
Showing 17 changed files with 2,163 additions and 765 deletions.
2 changes: 0 additions & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ coverage
node_modules
test

.atomignore
.codeclimate.yml
.eslintignore
.eslintrc.json
.gitignore
.npmignore
.prettierignore
.prettierrc.json
appveyor.yml
cli.d.ts
cli.js
cli.ts
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# wsib-clearance-check

[![npm (scoped)](https://img.shields.io/npm/v/@cityssm/wsib-clearance-check)](https://www.npmjs.com/package/@cityssm/wsib-clearance-check)
[![Codacy grade](https://img.shields.io/codacy/grade/ac5c43ebb90748bc86dbb3f1fbaff970)](https://app.codacy.com/gh/cityssm/wsib-clearance-check/dashboard)
[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/cityssm/wsib-clearance-check)](https://codeclimate.com/github/cityssm/wsib-clearance-check)
[![Code Climate coverage](https://img.shields.io/codeclimate/coverage/cityssm/wsib-clearance-check)](https://codeclimate.com/github/cityssm/wsib-clearance-check)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/cityssm/wsib-clearance-check/coverage.yml)](https://github.com/cityssm/wsib-clearance-check/actions/workflows/coverage.yml)
Expand Down
1 change: 0 additions & 1 deletion browserGlobal.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Browser } from 'puppeteer';
export declare const setHeadless: (headlessStatus: boolean) => void;
export declare const pageTimeoutMillis = 90000;
export declare function getBrowserGlobal(): Promise<Browser>;
export declare function keepBrowserGlobalAlive(): void;
Expand Down
27 changes: 11 additions & 16 deletions browserGlobal.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import puppeteer from 'puppeteer';
import { clearIntervalAsync } from 'set-interval-async';
import { setIntervalAsync } from 'set-interval-async/dynamic';
let headless = true;
export const setHeadless = (headlessStatus) => {
headless = headlessStatus;
};
export const pageTimeoutMillis = 90000;
const browserStartupTimeoutMillis = 3 * 60000;
const browserGlobalExpiryMillis = Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10000;
import puppeteerLaunch from '@cityssm/puppeteer-launch';
import exitHook from 'exit-hook';
import { clearIntervalAsync, setIntervalAsync } from 'set-interval-async/dynamic';
export const pageTimeoutMillis = 90_000;
const browserStartupTimeoutMillis = 3 * 60_000;
const browserGlobalExpiryMillis = Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10_000;
let browserGlobal;
let browserGlobalInitializedTime = 0;
let browserGlobalTimer;
Expand All @@ -19,11 +15,7 @@ export async function getBrowserGlobal() {
if (!isBrowserGlobalReady()) {
await cleanUpBrowserGlobal();
keepBrowserGlobalAlive();
browserGlobal = await puppeteer.launch({
headless: headless ? 'new' : false,
timeout: browserStartupTimeoutMillis,
args: ['--lang-en-CA,en']
});
browserGlobal = await puppeteerLaunch();
keepBrowserGlobalAlive();
browserGlobalTimer = setIntervalAsync(cleanUpBrowserGlobal, browserGlobalExpiryMillis);
}
Expand All @@ -38,7 +30,7 @@ export async function cleanUpBrowserGlobal(useForce = false) {
}
if (!isBrowserGlobalReady()) {
try {
await browserGlobal.close();
await browserGlobal?.close();
}
catch {
}
Expand All @@ -54,3 +46,6 @@ export async function cleanUpBrowserGlobal(useForce = false) {
browserGlobalInitializedTime = 0;
}
}
exitHook(() => {
void cleanUpBrowserGlobal(true);
});
34 changes: 14 additions & 20 deletions browserGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import puppeteerLaunch from '@cityssm/puppeteer-launch'
import exitHook from 'exit-hook'
import type { Browser } from 'puppeteer'
import puppeteer from 'puppeteer'
import { clearIntervalAsync } from 'set-interval-async'
import { setIntervalAsync } from 'set-interval-async/dynamic'

/*
* Headless Debug Setting
*/

let headless = true

export const setHeadless = (headlessStatus: boolean): void => {
headless = headlessStatus
}
import {
type SetIntervalAsyncTimer,
clearIntervalAsync,
setIntervalAsync
} from 'set-interval-async/dynamic'

/*
* Browser Global
Expand All @@ -25,7 +19,7 @@ const browserGlobalExpiryMillis =

let browserGlobal: Browser | undefined
let browserGlobalInitializedTime = 0
let browserGlobalTimer
let browserGlobalTimer: SetIntervalAsyncTimer<unknown[]> | undefined

function isBrowserGlobalReady(): boolean {
return Boolean(
Expand All @@ -40,11 +34,7 @@ export async function getBrowserGlobal(): Promise<Browser> {

keepBrowserGlobalAlive()

browserGlobal = await puppeteer.launch({
headless: headless ? 'new' : false,
timeout: browserStartupTimeoutMillis,
args: ['--lang-en-CA,en']
})
browserGlobal = await puppeteerLaunch()

keepBrowserGlobalAlive()

Expand All @@ -68,7 +58,7 @@ export async function cleanUpBrowserGlobal(useForce = false): Promise<void> {

if (!isBrowserGlobalReady()) {
try {
await browserGlobal.close()
await browserGlobal?.close()
} catch {
// ignore
}
Expand All @@ -88,3 +78,7 @@ export async function cleanUpBrowserGlobal(useForce = false): Promise<void> {
browserGlobalInitializedTime = 0
}
}

exitHook(() => {
void cleanUpBrowserGlobal(true)
})
8 changes: 4 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getClearanceByAccountNumber, cleanUpBrowser } from './index.js';
const cli = async () => {
import { cleanUpBrowser, getClearanceByAccountNumber } from './index.js';
async function cli() {
const accountNumbers = process.argv[2].split(',');
for (const accountNumber of accountNumbers) {
const results = await getClearanceByAccountNumber(accountNumber);
console.log(results);
}
await cleanUpBrowser();
};
cli();
}
await cli();
6 changes: 3 additions & 3 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getClearanceByAccountNumber, cleanUpBrowser } from './index.js'
import { cleanUpBrowser, getClearanceByAccountNumber } from './index.js'

const cli = async () => {
async function cli(): Promise<void> {
const accountNumbers = process.argv[2].split(',')

for (const accountNumber of accountNumbers) {
Expand All @@ -11,4 +11,4 @@ const cli = async () => {
await cleanUpBrowser()
}

cli()
await cli()
3 changes: 3 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/naming-convention */

// Search Form

export const clearanceStart_url =
Expand Down
5 changes: 2 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type * as types from './types.js';
export declare function setHeadless(headlessStatus: boolean): void;
export declare function getClearanceByAccountNumber(accountNumber: string): Promise<types.WSIBClearance_Failure | types.WSIBClearance_Success>;
import type { WSIBClearance_Failure, WSIBClearance_Success } from './types.js';
export declare function getClearanceByAccountNumber(accountNumber: string): Promise<WSIBClearance_Failure | WSIBClearance_Success>;
export declare function cleanUpBrowser(): Promise<void>;
export default getClearanceByAccountNumber;
31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import exitHook from 'exit-hook';
import * as browserGlobal from './browserGlobal.js';
import * as config from './config.js';
import * as parsers from './parsers.js';
export function setHeadless(headlessStatus) {
browserGlobal.setHeadless(headlessStatus);
}
function cleanRawCertificateOutput(rawOutput) {
const contractorLegalTradeName = parsers.stripHTML(rawOutput[config.certificateField_contractorLegalTradeName]);
const contractorAddress = parsers.stripHTML(rawOutput[config.certificateField_contractorAddress]);
Expand Down Expand Up @@ -40,8 +36,8 @@ export async function getClearanceByAccountNumber(accountNumber) {
referer: 'https://www.wsib.ca/en',
waitUntil: 'domcontentloaded'
});
if (!pageResponse.ok) {
throw new Error('Response Code = ' + pageResponse.status().toString());
if (!(pageResponse?.ok() ?? false)) {
throw new Error(`Response Code = ${pageResponse?.status().toString()}`);
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
Expand Down Expand Up @@ -71,46 +67,48 @@ export async function getClearanceByAccountNumber(accountNumber) {
.catch(() => {
throw new Error(config.clearanceResult_defaultErrorMessage);
});
throw new Error(errorMessage);
throw new Error(errorMessage ?? '');
}
browserGlobal.keepBrowserGlobalAlive();
await page.waitForSelector('body');
const certificateURL = page.url();
const parsedTable = await page.$eval(config.certificate_tableSelector, (tableElement) => {
const parsedTable_value = {};
const parsedTableValue = {};
const thElements = tableElement.querySelectorAll('thead tr th');
const tdElements = tableElement.querySelectorAll('tbody tr td');
for (const [index, thElement] of thElements.entries()) {
parsedTable_value[thElement.textContent] = tdElements[index].innerHTML;
parsedTableValue[thElement.textContent ?? ''] =
tdElements[index].innerHTML;
}
return parsedTable_value;
return parsedTableValue;
});
const certificate = cleanRawCertificateOutput(parsedTable);
const response = Object.assign({
return Object.assign({
success: true,
accountNumber
}, certificate, {
certificateURL
});
return response;
}
catch (error) {
let errorURL;
let errorURL = '';
try {
errorURL = page.url();
errorURL = page?.url() ?? '';
}
catch {
}
return {
success: false,
accountNumber,
error: error,
error,
errorURL
};
}
finally {
try {
await page.close();
if (page !== undefined) {
await page.close();
}
}
catch {
}
Expand All @@ -120,4 +118,3 @@ export async function cleanUpBrowser() {
await browserGlobal.cleanUpBrowserGlobal(true);
}
export default getClearanceByAccountNumber;
exitHook(cleanUpBrowser);
Loading

0 comments on commit 6c2021f

Please sign in to comment.