-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(temp): support plugin server-temp
- Loading branch information
Showing
6 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
} |