@satorijs/satori
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -1,8 +0,4 @@ | ||
import Schema from 'schemastery'; | ||
import Logger from 'reggol'; | ||
export { Schema, Logger }; | ||
import { Schema } from '@satorijs/core'; | ||
export * from '@satorijs/core'; | ||
export * from 'cosmokit'; | ||
export * from './adapter'; | ||
export * from './quester'; | ||
export * from './router'; | ||
@@ -9,0 +5,0 @@ declare module '@satorijs/core' { |
216
lib/index.js
@@ -30,180 +30,19 @@ var __create = Object.create; | ||
__export(src_exports, { | ||
Logger: () => import_reggol2.default, | ||
Quester: () => Quester, | ||
Router: () => Router, | ||
Schema: () => import_schemastery3.default, | ||
WebSocketLayer: () => WebSocketLayer | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var import_core4 = require("@satorijs/core"); | ||
var import_cosmokit4 = require("cosmokit"); | ||
var import_core2 = require("@satorijs/core"); | ||
var import_cosmokit2 = require("cosmokit"); | ||
var import_portfinder = require("portfinder"); | ||
var import_schemastery3 = __toESM(require("schemastery")); | ||
var import_reggol2 = __toESM(require("reggol")); | ||
// packages/satori/src/quester.ts | ||
var import_core = require("@satorijs/core"); | ||
var import_cosmokit = require("cosmokit"); | ||
var import_ws = __toESM(require("ws")); | ||
var import_proxy_agent = __toESM(require("proxy-agent")); | ||
var import_axios = __toESM(require("axios")); | ||
var import_schemastery = __toESM(require("schemastery")); | ||
var Quester = class { | ||
constructor(ctx, config) { | ||
return Quester.create(config.request); | ||
} | ||
}; | ||
__name(Quester, "Quester"); | ||
((Quester2) => { | ||
Quester2.isAxiosError = import_axios.default.isAxiosError; | ||
Quester2.Config = import_schemastery.default.object({ | ||
proxyAgent: import_schemastery.default.string().description("使用的代理服务器地址。"), | ||
timeout: import_schemastery.default.natural().role("ms").description("等待连接建立的最长时间。") | ||
}).description("请求设置"); | ||
Quester2.createConfig = /* @__PURE__ */ __name((endpoint) => import_schemastery.default.object({ | ||
endpoint: import_schemastery.default.string().role("link").description("要连接的服务器地址。").default(typeof endpoint === "string" ? endpoint : null).required(typeof endpoint === "boolean" ? endpoint : false), | ||
proxyAgent: import_schemastery.default.string().description("使用的代理服务器地址。"), | ||
headers: import_schemastery.default.dict(String).description("要附加的额外请求头。"), | ||
timeout: import_schemastery.default.natural().role("ms").description("等待连接建立的最长时间。") | ||
}).description("请求设置"), "createConfig"); | ||
const agents = {}; | ||
function getAgent(url) { | ||
return agents[url] || (agents[url] = new import_proxy_agent.default(url)); | ||
} | ||
__name(getAgent, "getAgent"); | ||
function create(config = {}) { | ||
const endpoint = config.endpoint = (0, import_cosmokit.trimSlash)(config.endpoint || ""); | ||
const options = { | ||
timeout: config.timeout, | ||
headers: config.headers | ||
}; | ||
if (config.proxyAgent) { | ||
options.httpAgent = getAgent(config.proxyAgent); | ||
options.httpsAgent = getAgent(config.proxyAgent); | ||
} | ||
const request = /* @__PURE__ */ __name(async (url, config2 = {}) => (0, import_axios.default)({ | ||
...options, | ||
...config2, | ||
url: endpoint + url, | ||
headers: { | ||
...options.headers, | ||
...config2.headers | ||
} | ||
}), "request"); | ||
const http = /* @__PURE__ */ __name(async (method, url, config2) => { | ||
const response = await request(url, { ...config2, method }); | ||
return response.data; | ||
}, "http"); | ||
http.config = config; | ||
http.axios = request; | ||
http.extend = (newConfig) => create({ | ||
...config, | ||
...newConfig, | ||
headers: { | ||
...config.headers, | ||
...newConfig.headers | ||
} | ||
}); | ||
http.get = (url, config2) => http("GET", url, config2); | ||
http.delete = (url, config2) => http("DELETE", url, config2); | ||
http.post = (url, data, config2) => http("POST", url, { ...config2, data }); | ||
http.put = (url, data, config2) => http("PUT", url, { ...config2, data }); | ||
http.patch = (url, data, config2) => http("PATCH", url, { ...config2, data }); | ||
http.head = async (url, config2) => { | ||
const response = await request(url, { ...config2, method: "HEAD" }); | ||
return response.headers; | ||
}; | ||
http.ws = (url, options2 = {}) => { | ||
return new import_ws.default(url, { | ||
agent: config.proxyAgent && getAgent(config.proxyAgent), | ||
handshakeTimeout: config.timeout, | ||
...options2, | ||
headers: { | ||
...config.headers, | ||
...options2.headers | ||
} | ||
}); | ||
}; | ||
return http; | ||
} | ||
Quester2.create = create; | ||
__name(create, "create"); | ||
})(Quester || (Quester = {})); | ||
import_core.Context.service("http", Quester); | ||
// packages/satori/src/index.ts | ||
__reExport(src_exports, require("@satorijs/core"), module.exports); | ||
__reExport(src_exports, require("cosmokit"), module.exports); | ||
// packages/satori/src/adapter.ts | ||
var import_core2 = require("@satorijs/core"); | ||
var import_cosmokit2 = require("cosmokit"); | ||
var import_schemastery2 = __toESM(require("schemastery")); | ||
var import_reggol = __toESM(require("reggol")); | ||
var logger = new import_reggol.default("adapter"); | ||
var WsClient = class extends import_core2.Adapter.Client { | ||
async start(bot) { | ||
let _retryCount = 0; | ||
const { retryTimes, retryInterval, retryLazy } = bot.config; | ||
const reconnect = /* @__PURE__ */ __name(async (initial = false) => { | ||
logger.debug("websocket client opening"); | ||
const socket = await this.prepare(bot); | ||
const url = socket.url.replace(/\?.+/, ""); | ||
socket.on("error", (error) => logger.debug(error)); | ||
socket.on("close", (code, reason) => { | ||
bot.socket = null; | ||
logger.debug(`websocket closed with ${code}`); | ||
if (bot.status === "disconnect") { | ||
return bot.status = "offline"; | ||
} | ||
const message = reason.toString() || `failed to connect to ${url}, code: ${code}`; | ||
let timeout = retryInterval; | ||
if (_retryCount >= retryTimes) { | ||
if (initial) { | ||
bot.error = new Error(message); | ||
return bot.status = "offline"; | ||
} else { | ||
timeout = retryLazy; | ||
} | ||
} | ||
_retryCount++; | ||
bot.status = "reconnect"; | ||
logger.warn(`${message}, will retry in ${import_cosmokit2.Time.format(timeout)}...`); | ||
setTimeout(() => { | ||
if (bot.status === "reconnect") | ||
reconnect(); | ||
}, timeout); | ||
}); | ||
socket.on("open", () => { | ||
_retryCount = 0; | ||
bot.socket = socket; | ||
logger.info("connect to server: %c", url); | ||
this.accept(bot); | ||
}); | ||
}, "reconnect"); | ||
reconnect(true); | ||
} | ||
async stop(bot) { | ||
var _a; | ||
(_a = bot.socket) == null ? void 0 : _a.close(); | ||
} | ||
}; | ||
__name(WsClient, "WsClient"); | ||
WsClient.reusable = true; | ||
WsClient.Config = import_schemastery2.default.object({ | ||
retryTimes: import_schemastery2.default.natural().description("初次连接时的最大重试次数。").default(6), | ||
retryInterval: import_schemastery2.default.natural().role("ms").description("初次连接时的重试时间间隔。").default(5 * import_cosmokit2.Time.second), | ||
retryLazy: import_schemastery2.default.natural().role("ms").description("连接关闭后的重试时间间隔。").default(import_cosmokit2.Time.minute) | ||
}).description("连接设置"); | ||
Object.defineProperties(import_core2.Adapter, { | ||
WsClient: { value: WsClient, enumerable: true } | ||
}); | ||
// packages/satori/src/router.ts | ||
var import_core3 = require("@satorijs/core"); | ||
var import_cosmokit3 = require("cosmokit"); | ||
var import_core = require("@satorijs/core"); | ||
var import_cosmokit = require("cosmokit"); | ||
var import_http = require("http"); | ||
var import_path_to_regexp = require("path-to-regexp"); | ||
var import_parseurl = __toESM(require("parseurl")); | ||
var import_ws2 = __toESM(require("ws")); | ||
var import_ws = __toESM(require("ws")); | ||
var import_router = __toESM(require("@koa/router")); | ||
@@ -230,3 +69,3 @@ var import_koa = __toESM(require("koa")); | ||
close() { | ||
(0, import_cosmokit3.remove)(this.router.wsStack, this); | ||
(0, import_cosmokit.remove)(this.router.wsStack, this); | ||
for (const socket of this.clients) { | ||
@@ -247,3 +86,3 @@ socket.close(); | ||
this._http = (0, import_http.createServer)(koa.callback()); | ||
this._ws = new import_ws2.default.Server({ | ||
this._ws = new import_ws.default.Server({ | ||
server: this._http | ||
@@ -261,5 +100,5 @@ }); | ||
const layer = super.register(...args); | ||
const context = this[import_core3.Context.current]; | ||
const context = this[import_core.Context.current]; | ||
context == null ? void 0 : context.state.disposables.push(() => { | ||
(0, import_cosmokit3.remove)(this.stack, layer); | ||
(0, import_cosmokit.remove)(this.stack, layer); | ||
}); | ||
@@ -271,3 +110,3 @@ return layer; | ||
this.wsStack.push(layer); | ||
const context = this[import_core3.Context.current]; | ||
const context = this[import_core.Context.current]; | ||
context == null ? void 0 : context.state.disposables.push(() => layer.close()); | ||
@@ -278,21 +117,21 @@ return layer; | ||
__name(Router, "Router"); | ||
import_core3.Context.service("router", Router); | ||
import_core.Context.service("router", Router); | ||
// packages/satori/src/index.ts | ||
(0, import_cosmokit4.defineProperty)(import_core4.Context, "Config", import_schemastery3.default.intersect([])); | ||
(0, import_cosmokit4.defineProperty)(import_core4.Context.Config, "Network", import_schemastery3.default.object({ | ||
host: import_schemastery3.default.string().default("localhost").description("要监听的 IP 地址。如果将此设置为 `0.0.0.0` 将监听所有地址,包括局域网和公网地址。"), | ||
port: import_schemastery3.default.natural().max(65535).description("要监听的初始端口号。"), | ||
maxPort: import_schemastery3.default.natural().max(65535).description("允许监听的最大端口号。"), | ||
selfUrl: import_schemastery3.default.string().role("link").description("应用暴露在公网的地址。") | ||
(0, import_cosmokit2.defineProperty)(import_core2.Context, "Config", import_core2.Schema.intersect([])); | ||
(0, import_cosmokit2.defineProperty)(import_core2.Context.Config, "Network", import_core2.Schema.object({ | ||
host: import_core2.Schema.string().default("localhost").description("要监听的 IP 地址。如果将此设置为 `0.0.0.0` 将监听所有地址,包括局域网和公网地址。"), | ||
port: import_core2.Schema.natural().max(65535).description("要监听的初始端口号。"), | ||
maxPort: import_core2.Schema.natural().max(65535).description("允许监听的最大端口号。"), | ||
selfUrl: import_core2.Schema.string().role("link").description("应用暴露在公网的地址。") | ||
}).description("网络设置")); | ||
import_core4.Context.Config.list.unshift(import_core4.Context.Config.Network); | ||
import_core4.Context.Config.list.push(import_schemastery3.default.object({ | ||
request: Quester.Config | ||
import_core2.Context.Config.list.unshift(import_core2.Context.Config.Network); | ||
import_core2.Context.Config.list.push(import_core2.Schema.object({ | ||
request: import_core2.Quester.Config | ||
})); | ||
var logger2 = new import_reggol2.default("app"); | ||
var start = import_core4.Context.prototype.start; | ||
import_core4.Context.prototype.start = async function(...args) { | ||
var logger = new import_core2.Logger("app"); | ||
var start = import_core2.Context.prototype.start; | ||
import_core2.Context.prototype.start = async function(...args) { | ||
if (this.options.selfUrl) { | ||
this.options.selfUrl = (0, import_cosmokit4.trimSlash)(this.options.selfUrl); | ||
this.options.selfUrl = (0, import_cosmokit2.trimSlash)(this.options.selfUrl); | ||
} | ||
@@ -308,6 +147,6 @@ if (this.options.port) { | ||
}); | ||
logger2.info("server listening at %c", `http://${host}:${port}`); | ||
logger.info("server listening at %c", `http://${host}:${port}`); | ||
this.on("dispose", () => { | ||
var _a, _b; | ||
logger2.info("http server closing"); | ||
logger.info("http server closing"); | ||
(_a = this.router._ws) == null ? void 0 : _a.close(); | ||
@@ -321,8 +160,5 @@ (_b = this.router._http) == null ? void 0 : _b.close(); | ||
0 && (module.exports = { | ||
Logger, | ||
Quester, | ||
Router, | ||
Schema, | ||
WebSocketLayer | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@satorijs/satori", | ||
"description": "Core components of Satorijs", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"main": "lib/index.js", | ||
@@ -31,8 +31,7 @@ "typings": "lib/index.d.ts", | ||
"devDependencies": { | ||
"@types/parseurl": "^1.3.1", | ||
"@types/ws": "^8.5.3" | ||
"@types/parseurl": "^1.3.1" | ||
}, | ||
"dependencies": { | ||
"@koa/router": "^10.1.1", | ||
"@satorijs/core": "^1.1.0", | ||
"@satorijs/core": "^1.1.1", | ||
"@types/koa": "*", | ||
@@ -46,8 +45,4 @@ "@types/koa__router": "*", | ||
"path-to-regexp": "^6.2.1", | ||
"portfinder": "^1.0.28", | ||
"proxy-agent": "^5.0.0", | ||
"reggol": "^1.1.0", | ||
"schemastery": "^3.5.4", | ||
"ws": "^8.8.1" | ||
"portfinder": "^1.0.28" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
11
1
25563
282
- Removedproxy-agent@^5.0.0
- Removedreggol@^1.1.0
- Removedschemastery@^3.5.4
- Removedws@^8.8.1
- Removed@tootallnate/once@1.1.2(transitive)
- Removedacorn@8.14.0(transitive)
- Removedacorn-walk@8.3.4(transitive)
- Removedagent-base@6.0.2(transitive)
- Removedast-types@0.13.4(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddata-uri-to-buffer@3.0.1(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removeddegenerator@3.0.4(transitive)
- Removedescodegen@1.14.3(transitive)
- Removedesprima@4.0.1(transitive)
- Removedestraverse@4.3.0(transitive)
- Removedesutils@2.0.3(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedfile-uri-to-path@2.0.0(transitive)
- Removedfs-extra@8.1.0(transitive)
- Removedftp@0.3.10(transitive)
- Removedget-uri@3.0.2(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedhttp-proxy-agent@4.0.1(transitive)
- Removedhttps-proxy-agent@5.0.1(transitive)
- Removedip@1.1.9(transitive)
- Removedip-address@9.0.5(transitive)
- Removedisarray@0.0.1(transitive)
- Removedjsbn@1.1.0(transitive)
- Removedjsonfile@4.0.0(transitive)
- Removedlevn@0.3.0(transitive)
- Removedlru-cache@5.1.1(transitive)
- Removednetmask@2.0.2(transitive)
- Removedoptionator@0.8.3(transitive)
- Removedpac-proxy-agent@5.0.0(transitive)
- Removedpac-resolver@5.0.1(transitive)
- Removedprelude-ls@1.1.2(transitive)
- Removedproxy-agent@5.0.0(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedsmart-buffer@4.2.0(transitive)
- Removedsocks@2.8.4(transitive)
- Removedsocks-proxy-agent@5.0.1(transitive)
- Removedsource-map@0.6.1(transitive)
- Removedsprintf-js@1.1.3(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedtslib@2.8.1(transitive)
- Removedtype-check@0.3.2(transitive)
- Removeduniversalify@0.1.2(transitive)
- Removedvm2@3.9.19(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedxregexp@2.0.0(transitive)
- Removedyallist@3.1.1(transitive)
Updated@satorijs/core@^1.1.1