From 0eb9bc43ff59d861ad9b59a5abd292feaf6176ce Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Mon, 7 Sep 2020 16:32:53 +0300 Subject: [PATCH 1/2] feat: added isUrl validation utility --- src/FileUploader.ts | 7 +++++-- src/SAS9ApiClient.ts | 6 +++++- src/SASViyaApiClient.ts | 6 +++++- src/SessionManager.ts | 7 +++++-- src/utils/index.ts | 1 + src/utils/isUrl.ts | 12 ++++++++++++ 6 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/utils/isUrl.ts diff --git a/src/FileUploader.ts b/src/FileUploader.ts index 8bd62be..c7be45a 100644 --- a/src/FileUploader.ts +++ b/src/FileUploader.ts @@ -1,4 +1,4 @@ -import { isLogInRequired, needsRetry } from './utils' +import { isLogInRequired, needsRetry, isUrl } from './utils' import { CsrfToken } from './types/CsrfToken' import { UploadFile } from './types/UploadFile' @@ -11,7 +11,10 @@ export class FileUploader { private jobsPath: string, private setCsrfTokenWeb: any, private csrfToken: CsrfToken | null = null - ) {} + ) { + if (serverUrl) isUrl(serverUrl) + } + private retryCount = 0 public uploadFile(sasJob: string, files: UploadFile[], params: any) { diff --git a/src/SAS9ApiClient.ts b/src/SAS9ApiClient.ts index 010d63a..40ffb09 100644 --- a/src/SAS9ApiClient.ts +++ b/src/SAS9ApiClient.ts @@ -1,9 +1,13 @@ +import { isUrl } from './utils' + /** * A client for interfacing with the SAS9 REST API * */ export class SAS9ApiClient { - constructor(private serverUrl: string) {} + constructor(private serverUrl: string) { + if (serverUrl) isUrl(serverUrl) + } /** * returns on object containing the server URL diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index b560076..31dd8a4 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -2,7 +2,8 @@ import { isAuthorizeFormRequired, parseAndSubmitAuthorizeForm, convertToCSV, - makeRequest + makeRequest, + isUrl } from './utils' import * as NodeFormData from 'form-data' import * as path from 'path' @@ -26,7 +27,10 @@ export class SASViyaApiClient { if (!rootFolderName) { throw new Error('Root folder must be provided.') } + + if (serverUrl) isUrl(serverUrl) } + private csrfToken: CsrfToken | null = null private rootFolder: Folder | null = null private sessionManager = new SessionManager( diff --git a/src/SessionManager.ts b/src/SessionManager.ts index 5a782c2..a0c6fb9 100644 --- a/src/SessionManager.ts +++ b/src/SessionManager.ts @@ -1,5 +1,5 @@ import { Session, Context, CsrfToken } from './types' -import { asyncForEach, makeRequest } from './utils' +import { asyncForEach, makeRequest, isUrl } from './utils' const MAX_SESSION_COUNT = 1 @@ -8,7 +8,10 @@ export class SessionManager { private serverUrl: string, private contextName: string, private setCsrfToken: (csrfToken: CsrfToken) => void - ) {} + ) { + if (serverUrl) isUrl(serverUrl) + } + private sessions: Session[] = [] private currentContext: Context | null = null private csrfToken: CsrfToken | null = null diff --git a/src/utils/index.ts b/src/utils/index.ts index 6a15042..0eb205b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -13,3 +13,4 @@ export * from './parseSasViyaLog' export * from './serialize' export * from './splitChunks' export * from './parseWeboutResponse' +export * from './isUrl' diff --git a/src/utils/isUrl.ts b/src/utils/isUrl.ts new file mode 100644 index 0000000..7d686f9 --- /dev/null +++ b/src/utils/isUrl.ts @@ -0,0 +1,12 @@ +export const isUrl = (url: string): boolean => { + const pattern = new RegExp( + '^(http://www.|https://www.|http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$', + 'gi' + ) + + if (pattern.test(url)) return true + else + throw new Error( + `'${url}' is not a valid url. An example of a valid url is 'http://valid-url.com'.` + ) +} From 3da5aba62a4cce911f34e15449e0367257bd8c44 Mon Sep 17 00:00:00 2001 From: Yury Shkoda Date: Tue, 8 Sep 2020 16:07:02 +0300 Subject: [PATCH 2/2] fix: removed unnecessary check for www subdomain --- src/utils/isUrl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/isUrl.ts b/src/utils/isUrl.ts index 7d686f9..1e64fa7 100644 --- a/src/utils/isUrl.ts +++ b/src/utils/isUrl.ts @@ -1,6 +1,6 @@ export const isUrl = (url: string): boolean => { const pattern = new RegExp( - '^(http://www.|https://www.|http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$', + '^(http://|https://)[a-z0-9]+([-.]{1}[a-z0-9]+)*.[a-z]{2,5}(:[0-9]{1,5})?(/.*)?$', 'gi' )