Skip to content

Commit

Permalink
feat(file): ensure filename
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 15, 2024
1 parent f0a3dd1 commit c07af16
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare module 'cordis' {
}

interface Events {
'http/config'(config: HTTP.Config): void
'http/fetch-init'(init: RequestInit, config: HTTP.Config): void
'http/websocket-init'(init: ClientOptions, config: HTTP.Config): void
}
Expand Down Expand Up @@ -139,8 +140,10 @@ export class HTTP extends Service {
}

resolveConfig(init?: HTTP.RequestConfig): HTTP.RequestConfig {
const caller = this[Context.current]
let result = { headers: {}, ...this.config }
let intercept = this[Context.current][Context.intercept]
caller.emit('http/config', result)
let intercept = caller[Context.intercept]
while (intercept) {
result = HTTP.mergeConfig(result, intercept.http)
intercept = Object.getPrototypeOf(intercept)
Expand Down
3 changes: 2 additions & 1 deletion packages/file/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "undios-file",
"description": "File support for undios",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -51,6 +51,7 @@
"plugin"
],
"devDependencies": {
"@types/mime-db": "^1.43.5",
"cordis": "^3.10.3"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/file/src/adapter/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export async function loadFile(url: string): Promise<FileResponse | undefined> {
if (url.startsWith('file://')) {
const data = await readFile(fileURLToPath(url))
const result = await fromBuffer(data)
return { mime: result?.mime, name: basename(url), data }
return { mime: result?.mime, filename: basename(url), data }
}
}
14 changes: 11 additions & 3 deletions packages/file/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import HTTP, {} from 'undios'
import { loadFile, lookup } from 'undios-file/adapter'
import { Context, z } from 'cordis'
import { base64ToArrayBuffer } from 'cosmokit'
import mimedb from 'mime-db'
import { isLocalAddress } from './utils.ts'

declare module 'undios' {
Expand All @@ -17,7 +18,7 @@ export interface FileConfig {

export interface FileResponse {
mime?: string
name?: string
filename: string
data: ArrayBuffer
}

Expand All @@ -31,13 +32,20 @@ export function apply(ctx: Context, config: Config) {
ctx.provide('http.file')
ctx.provide('http.local')

function createName(mime: string | undefined) {
let name = 'file'
const ext = mime && mimedb[mime]?.extensions?.[0]
if (ext) name += `.${ext}`
return name
}

ctx['http.file'] = async function file(this: HTTP, url: string, options: FileConfig = {}): Promise<FileResponse> {
const result = await loadFile(url)
if (result) return result
const capture = /^data:([\w/-]+);base64,(.*)$/.exec(url)
if (capture) {
const [, mime, base64] = capture
return { mime, data: base64ToArrayBuffer(base64) }
return { mime, data: base64ToArrayBuffer(base64), filename: createName(mime) }
}
const { headers, data, url: responseUrl } = await this<ArrayBuffer>(url, {
method: 'GET',
Expand All @@ -46,7 +54,7 @@ export function apply(ctx: Context, config: Config) {
})
const mime = headers.get('content-type') ?? undefined
const [, name] = responseUrl.match(/.+\/([^/?]*)(?=\?)?/)!
return { mime, name, data }
return { mime, filename: name, data }
}

ctx['http.local'] = async function isLocal(url: string) {
Expand Down

0 comments on commit c07af16

Please sign in to comment.