Skip to content

Commit

Permalink
feat(core): support app.config.assets.whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 11, 2022
1 parent 52c1a1d commit 1f53441
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
34 changes: 31 additions & 3 deletions packages/koishi/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { Context, Service } from '@koishijs/core'
import { segment } from '@koishijs/utils'
import { App, Context, Schema, Service } from '@koishijs/core'
import { defineProperty, segment } from '@koishijs/utils'
import { createHash } from 'crypto'
import { basename } from 'path'
import FileType from 'file-type'

declare module '@koishijs/core' {
namespace App {
interface Config {
assets: Config.Assets
}

namespace Config {
interface Static {
Assets?: Schema<Assets>
}

interface Assets {
whitelist?: string[]
}
}
}
}

defineProperty(App.Config, 'Assets', Schema.object({
whitelist: Schema.array(Schema.string().role('url')).description('不处理的白名单 URL 列表。'),
}).description('资源设置'))

const PROTOCOL_BASE64 = 'base64://'

export abstract class Assets extends Service {
Expand All @@ -19,7 +41,13 @@ export abstract class Assets extends Service {

public async transform(content: string) {
return await segment.transformAsync(content, Object.fromEntries(this.types.map((type) => {
return [type, async (data) => segment(type, { url: await this.upload(data.url, data.file) })]
return [type, async (data) => {
if (this.ctx.app.options.assets.whitelist.some(prefix => data.url.startsWith(prefix))) {
return segment(type, data)
} else {
return segment(type, { url: await this.upload(data.url, data.file) })
}
}]
})))
}

Expand Down
8 changes: 7 additions & 1 deletion packages/koishi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Context, Modules } from '@koishijs/core'
import { App, Context, Modules, Schema } from '@koishijs/core'
import { trimSlash } from '@koishijs/utils'
import { Cache } from './cache'
import { Assets } from './assets'
Expand Down Expand Up @@ -29,6 +29,12 @@ declare module '@koishijs/core' {
}
}

App.Config.list.unshift(App.Config.Network)
App.Config.list.push(Schema.object({
request: Quester.Config,
assets: App.Config.Assets,
}))

// use node require
Modules.internal.require = require
Modules.internal.resolve = require.resolve
Expand Down
24 changes: 4 additions & 20 deletions packages/koishi/src/quester.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { App, Schema } from '@koishijs/core'
import { Dict, defineProperty } from '@koishijs/utils'
import { Schema } from '@koishijs/core'
import { Dict } from '@koishijs/utils'
import { Agent } from 'http'
import ProxyAgent from 'proxy-agent'
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios'

declare module '@koishijs/core' {
namespace App {
interface Config extends Config.Request {}

namespace Config {
interface Static {
Request?: Schema<Config.Request>
}

interface Request {
request?: Quester.Config
}
interface Config {
request?: Quester.Config
}
}

Expand Down Expand Up @@ -107,11 +99,3 @@ export namespace Quester {
return http
}
}

const RequestConfig: Schema<App.Config.Request> = Schema.object({
request: Quester.Config,
})

defineProperty(App.Config, 'Request', RequestConfig)

App.Config.list.push(RequestConfig)
2 changes: 0 additions & 2 deletions packages/koishi/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ defineProperty(App.Config, 'Network', Schema.object({
selfUrl: Schema.string().role('url').description('应用暴露在公网的地址。部分插件 (例如 github 和 telegram) 需要用到。'),
}).description('网络设置'))

App.Config.list.unshift(App.Config.Network)

type WebSocketCallback = (socket: WebSocket, request: IncomingMessage) => void

export class WebSocketLayer {
Expand Down

0 comments on commit 1f53441

Please sign in to comment.