Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
feat(utils): add cookie and fetch utils
Browse files Browse the repository at this point in the history
  • Loading branch information
beetcb committed May 23, 2021
1 parent f4f975c commit c92c691
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 74 deletions.
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dist/"
],
"scripts": {
"pub": "parcel build src/index.js src/cli.js --target node --no-source-maps && npm publish --access public"
"build": "tsc"
},
"keywords": [
"campusphere",
Expand Down Expand Up @@ -38,13 +38,14 @@
"cheerio": "^1.0.0-rc.5",
"enquirer": "^2.3.6",
"node-fetch": "^2.6.1",
"signale": "^1.4.0",
"tesseract.js": "^2.1.4",
"uuid": "^8.3.2",
"yargs": "^17.0.1"
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/yargs": "^16.0.1",
"parcel-plugin-shebang": "^1.2.1",
"@types/node-fetch": "^2.5.10",
"@types/signale": "^1.4.1",
"jest": "^26.6.3",
"prettier": "2.3.0",
"typescript": "^4.2.4"
}
Expand Down
36 changes: 35 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
import yargs from 'yargs'
#!/usr/bin/env node
;(async () => {
const argv = process.argv[2] || ''
const argv2 = process.argv[3]

switch (argv) {
case '-h':
case '--help': {
console.log(`
Usage: cea <command>
All Commands:
user create|delete user
school config your school info
sign campusphere check in
load load config info from toml file
rm remove stored config feilds
`)
break
}
case 'user': {
break
}
case 'school': {
break
}
case 'rm': {
break
}
case 'sign': {
break
}
case 'load': {
}
}
})()
File renamed without changes.
8 changes: 8 additions & 0 deletions src/types/cookie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type CookieMap = Map<string, Array<[string, string]>>

export interface FetchCookieOptions {
type?: 'json' | 'form'
cookiePath?: string
body?: string
isPost?: boolean
}
51 changes: 51 additions & 0 deletions src/utils/cookie-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { CookieMap } from '../types/cookie'
import { Headers } from 'node-fetch'

/**
* Parse http response headers
*/
export function cookieParse(host: string, headers: Headers): CookieMap {
const rawCookies = headers.raw()['set-cookie']
const map = new Map()
if (!rawCookies) {
return map
}

let [lastIdxMark, arr] = ['', []] as [string, Array<Array<string>>]
for (const e of rawCookies) {
const [_, keyVal, path] = e.match(/(.*);(?:\s?)path=((\w+|\/)*)/i)!
if (!keyVal) {
continue
}
const [key, val] = keyVal.split('=')
const mapIdx = `${host}::${path}`
if (lastIdxMark !== mapIdx) {
if (lastIdxMark) {
map.set(lastIdxMark, arr)
arr = []
}
}
lastIdxMark = mapIdx
arr.push([key, val])
// deprecated because of the numerous map set operations
// map.set(mapIdx, [...(ownedKeyVal ? ownedKeyVal : []), { [key]: val }])
}
if (arr.length) {
map.set(lastIdxMark, arr)
}
return map
}

/**
* Construct a cookie obj base on path
*/
export function cookieStr(host: string, path: string, cookieMap: CookieMap) {
const mapIdx = `${host}::${path}`
const cookie = cookieMap.get(mapIdx)
if (cookie) {
return cookie.reduce((str, e) => {
const [key, val] = e
return str + `${key}=${val}; `
}, '')
}
}
92 changes: 92 additions & 0 deletions src/utils/fetch-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import fetch from 'node-fetch'
import { CookieMap, FetchCookieOptions } from '../types/cookie'
import { cookieParse, cookieStr } from './cookie-helper'
import { Headers, Response } from 'node-fetch'

export class FetchWithCookie {
private headers: { [key: string]: any }
private cookieMap?: CookieMap
private redirectUrl?: string
constructor(headers: Headers) {
this.headers = headers
this.cookieMap = undefined
this.redirectUrl = undefined
}

async get(url: string, options = {}) {
return await this.fetch(url, options)
}

async post(url: string, options: FetchCookieOptions) {
options.isPost = true
return await this.fetch(url, options)
}

/**
* keep requesting last request url
* @param {} options
*/
async follow(options: FetchCookieOptions) {
return new Promise((resolve, reject) =>
this.redirectUrl
? resolve(this.fetch(this.redirectUrl, options))
: reject({ status: 555 })
)
}

async fetch(url: string, options: FetchCookieOptions) {
const { host, origin } = new URL(url)
const { type, body, cookiePath } = options
const { headers } = this

headers.origin = origin
headers.referer = origin
headers.host = host
headers.cookie = this.cookieMap
? cookieStr(host, cookiePath!, this.cookieMap)
: ''

headers['Content-Type'] =
type === 'form' ? 'application/x-www-form-urlencoded' : 'application/json'

if (!type && headers['Content-Type']) {
delete headers['Content-Type']
}

const res = (await fetch(url, {
headers,
method: type ? 'POST' : undefined,
body: body,
redirect: 'manual',
}).catch((err) => console.error(err))) as Response

this.redirectUrl = res.headers.get('location') || this.redirectUrl
this.updateMap(cookieParse(host, res.headers))
return res
}

getCookieObj() {
let obj: { [key: string]: string } = {}
for (const [key, val] of this.cookieMap!.entries()) {
const [_, feild, path] = key.match(/(.*)(::.*)/)!
obj[`${feild.includes('campusphere') ? 'campusphere' : 'swms'}${path}`] =
val.reduce((str, e) => `${str}${e.join('=')}; `, '')
}
return obj
}

updateMap(newMap: CookieMap) {
if (!this.cookieMap) {
this.cookieMap = newMap
} else {
for (const [key, val] of newMap.entries()) {
const old = this.cookieMap ? this.cookieMap.get(key) : []
if (old) {
this.cookieMap.set(key, [...old, ...val])
} else {
this.cookieMap.set(key, val)
}
}
}
}
}
15 changes: 15 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Signale, SignaleOptions } from 'signale'

export default new Signale({
types: {
error: {
label: '失败',
},
success: {
label: '成功',
},
warn: {
label: '警示',
},
},
} as SignaleOptions)
78 changes: 10 additions & 68 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,71 +1,13 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
"target": "es6",
"module": "commonjs",
"outDir": "dist/",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}

0 comments on commit c92c691

Please sign in to comment.