From f1412785c9325aaac6643e23006c9cf66fd4b6f3 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 11 May 2024 21:36:46 +0800 Subject: [PATCH] feat(http): support ctx.http.isError also fix file-type imports --- packages/core/src/adapter/node.ts | 4 ++-- packages/core/src/index.ts | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/src/adapter/node.ts b/packages/core/src/adapter/node.ts index 20cf6d3..c50a210 100644 --- a/packages/core/src/adapter/node.ts +++ b/packages/core/src/adapter/node.ts @@ -1,6 +1,6 @@ import { fileURLToPath } from 'node:url' import { basename } from 'node:path' -import { fromBuffer } from 'file-type' +import FileType from 'file-type' import { FileResponse } from '../index.js' import { readFile } from 'node:fs/promises' @@ -9,7 +9,7 @@ export { lookup } from 'node:dns/promises' export async function loadFile(url: string): Promise { if (url.startsWith('file://')) { const data = await readFile(fileURLToPath(url)) - const result = await fromBuffer(data) + const result = await FileType.fromBuffer(data) // https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer#answer-31394257 const buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength) return { mime: result?.mime, filename: basename(url), data: buffer } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 9d0c8c5..24a80d2 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -163,6 +163,8 @@ export class HTTP extends Service { } } + public isError = HTTPError.is + private _decoders: Dict = Object.create(null) constructor(config?: HTTP.Config)