更新oauth架构

This commit is contained in:
undefined committed 2020-08-03 20:10:05 +08:00
1 parent d184ca9ea1
commit b3af28dc43
7 files changed
+64 -132

No files matched your search

+46 -2
View File
@@ -2,8 +2,8 @@ import fs from 'fs';
import cluster from 'cluster';
import path from 'path';
import * as superagent from 'superagent';
import proxyAgent from 'proxy-agent';
import serialize from 'serialize-javascript';
import proxy from 'superagent-proxy';
import Lru from 'lru-cache';
declare global {
@@ -41,7 +41,51 @@ export function lrucache(target: any, funcName: string, obj: any) {
return obj;
}
proxy(superagent);
namespace Proxy {
function getUrl() {
return this._url;
}
function setUrl(v: string) {
this._url = v;
// eslint-disable-next-line no-use-before-define
proxy.call(this, this._proxyUri);
}
function setupUrl(req: superagent.Request) {
const desc = Object.getOwnPropertyDescriptor(req, 'url');
if (desc.get === getUrl && desc.set === setUrl) return;
// @ts-ignore
req._url = req.url;
desc.get = getUrl;
desc.set = setUrl;
delete desc.value;
delete desc.writable;
Object.defineProperty(req, 'url', desc);
}
function proxy(uri: string) {
if (!uri) return this;
setupUrl(this);
const agent = proxyAgent(uri);
if (agent) this.agent(agent);
this._proxyUri = uri;
return this;
}
export function setup(agent, uri = '') {
const Request = agent.Request;
if (Request) {
Request.prototype.proxy = proxy;
return agent;
}
return proxy.call(agent, uri);
}
}
declare module 'superagent' {
interface Request {
proxy(url: string): this;
}
}
Proxy.setup(superagent);
if (!cluster.worker) {
// @ts-ignore