Skip to content

Commit

Permalink
feat(temp): support plugin server-temp
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Nov 14, 2023
1 parent 9b1df94 commit ddda5f1
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export interface Context {
[Context.config]: Context.Config
[Context.events]: Events<this>
[Context.session]: Session<this>
baseDir: string
http: Quester
}

Expand Down Expand Up @@ -129,6 +130,7 @@ export class Context extends cordis.Context {
constructor(config: Context.Config = {}) {
super(config)

this.baseDir = globalThis.process?.cwd() || ''
this.http = new Quester(config.request)

this.on('internal/warning', function (format, ...args) {
Expand Down
14 changes: 7 additions & 7 deletions packages/server-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Context, Quester, Schema } from '@satorijs/core'
import { Context, Quester, Schema } from '@satorijs/satori'
import {} from '@satorijs/router'
import internal from 'stream'

declare module '@satorijs/core' {
interface Context {
'server\.proxy': ProxyService
'server.proxy': ProxyServer
}
}

class ProxyService {
class ProxyServer {
static inject = ['router']

constructor(protected ctx: Context, public config: ProxyService.Config) {
constructor(protected ctx: Context, public config: ProxyServer.Config) {
const logger = ctx.logger('proxy')

ctx.router.get(config.path + '/:url(.*)', async (koa) => {
Expand All @@ -26,11 +26,11 @@ class ProxyService {
}
})

ctx.root.provide('server\.proxy', this)
ctx.root.provide('server.proxy', this)
}
}

namespace ProxyService {
namespace ProxyServer {
export interface Config {
path?: string
}
Expand All @@ -40,4 +40,4 @@ namespace ProxyService {
})
}

export default ProxyService
export default ProxyServer
2 changes: 2 additions & 0 deletions packages/server-temp/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
tsconfig.tsbuildinfo
33 changes: 33 additions & 0 deletions packages/server-temp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "@satorijs/server-temp",
"description": "Temp server plugin for cordis",
"version": "1.0.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"src"
],
"author": "Shigma <[email protected]>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/satorijs/satori.git",
"directory": "packages/server-temp"
},
"bugs": {
"url": "https://github.com/satorijs/satori/issues"
},
"homepage": "https://github.com/satorijs/satori/tree/master/packages/temp",
"keywords": [
"cordis",
"router",
"http",
"temporary",
"server",
"service"
],
"peerDependencies": {
"@satorijs/satori": "^3.2.0"
}
}
81 changes: 81 additions & 0 deletions packages/server-temp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Context, Dict, Schema, Time } from '@satorijs/satori'
import {} from '@satorijs/router'
import { createReadStream } from 'fs'
import { mkdir, rm, writeFile } from 'fs/promises'
import internal from 'stream'

declare module '@satorijs/core' {
interface Context {
'server.temp': TempServer
}
}

interface Entry {
path: string
url: string
dispose?: () => void
}

class TempServer {
static inject = ['router']

public baseDir!: string
public entries: Dict<Entry> = Object.create(null)

constructor(protected ctx: Context, public config: TempServer.Config) {
const logger = ctx.logger('temp')

ctx.router.get(config.path + '/:name', async (koa) => {
logger.debug(koa.params.name)
const entry = this.entries[koa.params.name]
if (!entry) return koa.status = 404
koa.body = createReadStream(entry.path)
})

ctx.on('ready', () => this.start())
ctx.on('dispose', () => this.start())
}

async start() {
this.baseDir = this.ctx.baseDir + '/temp/' + Math.random().toString(36).slice(2) + '/'
await mkdir(this.baseDir, { recursive: true })
this.ctx.root.provide('server.temp', this)
}

async stop() {
await rm(this.baseDir, { recursive: true })
}

async create(data: string | Buffer | internal.Readable) {
const name = Math.random().toString(36).slice(2)
const url = this.ctx.router.selfUrl + this.config.path + '/' + name
let path: string
if (typeof data === 'string') {
path = data
} else {
path = this.baseDir + name
await writeFile(path, data)
}
const dispose = this[Context.current]?.collect('server.temp', async () => {
clearTimeout(timer)
delete this.entries[name]
if (path.startsWith(this.baseDir)) await rm(path)
})
const timer = dispose && setTimeout(() => dispose(), this.config.maxAge)
return this.entries[name] = { path, url, dispose }
}
}

namespace TempServer {
export interface Config {
path?: string
maxAge?: number
}

export const Config: Schema<Config> = Schema.object({
path: Schema.string().default('/temp'),
maxAge: Schema.number().default(Time.minute * 5),
})
}

export default TempServer
12 changes: 12 additions & 0 deletions packages/server-temp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"strict": true,
"noImplicitAny": false,
},
"include": [
"src",
],
}

0 comments on commit ddda5f1

Please sign in to comment.