mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 17:46:28 +08:00
update
This commit is contained in:
1 parent
8389519249
commit
a5886ecb99
37 files changed
+940
-162
No files matched your search
@@ -1,8 +1,6 @@
|
||||
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 Lru from 'lru-cache';
|
||||
|
||||
@@ -41,52 +39,6 @@ export function lrucache(target: any, funcName: string, obj: any) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
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 SuperAgentRequest {
|
||||
proxy(url: string): this;
|
||||
}
|
||||
}
|
||||
|
||||
Proxy.setup(superagent);
|
||||
|
||||
if (!cluster.worker) {
|
||||
// @ts-ignore
|
||||
cluster.worker = { id: 0 };
|
||||
|
||||
Reference in new issue
Block a user