Skip to content

Commit

Permalink
feat(new-arch): initial files for v2 based on new architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
rascala committed Jul 1, 2020
1 parent 0cda237 commit 1508c20
Show file tree
Hide file tree
Showing 24 changed files with 2,427 additions and 2,789 deletions.
4,184 changes: 1,737 additions & 2,447 deletions package-lock.json

Large diffs are not rendered by default.

75 changes: 36 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
{
"name": "@authless/core",
"version": "1.0.2",
"description": "A browserless authentication and service management framework",
"main": "dist/src/index",
"types": "dist/src/index",
"scripts": {
"test": "CHROME_USER_DATA_DIR=/tmp jest --c jest.config.json --maxWorkers=1",
"test:watch": "npm run test -- --watch",
"test:coverage": "npm test -- --collectCoverage",
"build": "tsc && npm run docs",
"prepare": "npm run build",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"semantic-release": "GITHUB_TOKEN=test semantic-release --no-ci --dry-run",
"semantic-release:ci": "npm install && semantic-release",
"code:analyze": "docker run --interactive --tty --rm --env CODECLIMATE_CODE=\"$PWD\" --volume \"$PWD\":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate analyze",
"docs:api": "api-extractor run --local",
"docs": "npm run docs:api && api-documenter markdown -i etc -o docs/markdown"
},
"author": "",
"keywords": [],
"license": "ISC",
"name": "authless-core",
"version": "2.0.0",
"description": "",
"private": true,
"main": "dist/index",
"types": "dist/index",
"dependencies": {
"avsc": "^5.4.21",
"check-types": "^11.1.2",
"delay": "^4.3.0",
"find-my-way": "^2.2.2",
"lodash.clonedeep": "^4.5.0",
"object-hash": "^2.0.3",
"puppeteer": "^2.1.1",
"puppeteer-extra": "^3.1.9",
"puppeteer-extra-plugin-aws": "^1.0.2",
"puppeteer-extra-plugin-proxy": "^1.0.2",
"puppeteer-extra-plugin-stealth": "^2.4.7",
"puppeteer-extra-plugin-user-data-dir": "^2.1.2",
"uuid": "^7.0.3",
"verror": "^1.10.0"
"@types/express": "^4.17.6",
"express": "^4.17.1",
"puppeteer-extra": "^3.1.9"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
Expand All @@ -44,14 +18,13 @@
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.4",
"@types/node": "^13.13.12",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"assert-throws-async": "^3.0.0",
"@types/puppeteer": "^3.0.1",
"eslint": "^6.8.0",
"eslint-config-standard-with-typescript": "^16.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jsdoc": "^24.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
Expand All @@ -60,5 +33,29 @@
"semantic-release": "^17.0.7",
"ts-jest": "^25.4.0",
"typescript": "^3.8.3"
}
},
"scripts": {
"test": "jest --c jest.config.json --maxWorkers=1",
"test:watch": "npm run test -- --watch",
"test:coverage": "npm test -- --collectCoverage",
"build": "tsc",
"prepublish": "npm run build",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"semantic-release": "GITHUB_TOKEN=test semantic-release --no-ci --dry-run",
"semantic-release:ci": "npm install && semantic-release",
"code:analyze": "docker run --interactive --tty --rm --env CODECLIMATE_CODE=\"$PWD\" --volume \"$PWD\":/code --volume /var/run/docker.sock:/var/run/docker.sock --volume /tmp/cc:/tmp/cc codeclimate/codeclimate analyze",
"docs:api": "api-extractor run --local",
"docs": "npm run docs:api && api-documenter markdown -i etc -o docs/markdown"
},
"repository": {
"type": "git",
"url": "git+/~https://github.com/MichaelHirn/ts-template.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "/~https://github.com/MichaelHirn/ts-template/issues"
},
"homepage": "/~https://github.com/MichaelHirn/ts-template#readme"
}
108 changes: 0 additions & 108 deletions src/account/account.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/account/index.ts

This file was deleted.

76 changes: 76 additions & 0 deletions src/bots/bot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable no-warning-comments */
import { IBot } from '../types2'

// 1 minutes = 60_000 milliseconds
const ONE_MINUTE = 60_000

export class Bot implements IBot {
username: string
password: string
hitCount = 0
loginCount = 0
captchaCount = 0
// rateLimit of 0 means no rate limiting
rateLimit = 0
usageTimeStamps: number[]

constructor (username: string, password: string, rateLimit?: number) {
this.username = username
this.password = password
this.usageTimeStamps = []
if(typeof rateLimit === 'number') {
// TODO, calculate number of times account was used per minute
this.rateLimit = rateLimit
}
}

setUsageTimeStamps (): void {
const now = Date.now()
this.usageTimeStamps.push(now)
this.usageTimeStamps = this.usageTimeStamps.filter(ts => (now - ts) <= ONE_MINUTE)
}

wasUsed (): void {
this.setUsageTimeStamps()
}

foundLogin (found: Boolean): void {
this.hitCount += 1
if(found === true) {
this.loginCount += 1
}
}

foundCaptcha (found: Boolean): void {
this.hitCount += 1
if(found === true) {
this.loginCount += 1
}
}

isBelowRateLimit (): Boolean {
if(this.rateLimit === 0) {
return true
}
// update this.usageTimeStamps
this.setUsageTimeStamps()
if(this.usageTimeStamps.length < this.rateLimit) {
return true
}
return false
}

getHitCount (): number {
return this.hitCount
}

// TODO, save with timeStamps?
getLoginHitCount (): number {
return 100 * this.hitCount / this.loginCount
}

// TODO, save with timeStamps?
getCaptchaHitCount (): number {
return 100 * this.hitCount / this.captchaCount
}
}
28 changes: 28 additions & 0 deletions src/bots/botrouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IBot, IBotRouter } from '../types2'

export class BotRouter implements IBotRouter {
bots: IBot[]
botIndex = 0

constructor (bots: IBot[]) {
this.bots = bots
}

private incrementBotIndex (): number {
const botIndex = this.botIndex
this.botIndex = (this.botIndex + 1) % this.bots.length
return botIndex
}

getBot (): IBot | undefined {
if(this.botIndex < this.bots.length) {
return this.bots[this.incrementBotIndex()]
}
}

// eslint-disable-next-line no-warning-comments
// TODO - get only if bot.isBelowRateLimit() is true
getBotByUsername (name: string): IBot | undefined {
return this.bots.find(bot => bot.username === name)
}
}
28 changes: 28 additions & 0 deletions src/domainPaths/domainPath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IBotRouter, IDomainPath } from '../types2'

export class DomainPath implements IDomainPath {
domain: string
botRouter: IBotRouter
urls: string[]

constructor (domain, botRouter, urls) {
this.domain = domain
this.botRouter = botRouter
this.urls = urls
}

// eslint-disable-next-line class-methods-use-this
async isAuthenticated (page: any): Promise<Boolean> {
return true
}

// eslint-disable-next-line class-methods-use-this
async authenticate (page: any): Promise<string> {
return 'to be implemented'
}

// eslint-disable-next-line class-methods-use-this
async pageHandler (page: any, params: any): Promise<void> {
// process the page
}
}
22 changes: 22 additions & 0 deletions src/domainPaths/domainPathRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IDomainPath, IDomainPathRouter } from '../types2'

export class DomainPathRouter implements IDomainPathRouter {
domainPaths: IDomainPath[]

constructor (services: IDomainPath[]) {
this.domainPaths = services
}

// eslint-disable-next-line no-warning-comments
// TODO - try to get service whose URL matches and is the longest(most specific)?
getDomainPathFromUrl (url: string): IDomainPath | undefined {
const matchedServices = this.domainPaths
// eslint-disable-next-line array-callback-return
.filter(service => {
service.urls.find(serviceUrl => url.includes(serviceUrl))
})
if(matchedServices.length > 0) {
return matchedServices[0]
}
}
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
* @packageDocumentation
*/

export { Account, AccountConfig } from './account'
export { Service } from './service'
export { Route, Router } from './router'
export { Bot } from './bots/bot'
export { BotRouter } from './bots/botrouter'
export { DomainPath } from './domainPaths/domainPath'
export { DomainPathRouter } from './domainPaths/domainPathRouter'
export { Authless } from './authless'
export { ServiceDefault } from './services'

export {
IResourcePayload,
IResourceResponse,
Expand Down
Loading

0 comments on commit 1508c20

Please sign in to comment.