-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move set-content-type function (#162)
- Loading branch information
Showing
6 changed files
with
62 additions
and
48 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,38 @@ | ||
import { type Logger } from '@libp2p/interface' | ||
import { type ContentTypeParser } from '../types.js' | ||
import { isPromise } from './type-guards.js' | ||
|
||
export interface SetContentTypeOptions { | ||
bytes: Uint8Array | ||
path: string | ||
response: Response | ||
defaultContentType?: string | ||
contentTypeParser: ContentTypeParser | undefined | ||
log: Logger | ||
} | ||
|
||
export async function setContentType ({ bytes, path, response, contentTypeParser, log, defaultContentType = 'application/octet-stream' }: SetContentTypeOptions): Promise<void> { | ||
let contentType: string | undefined | ||
|
||
if (contentTypeParser != null) { | ||
try { | ||
let fileName = path.split('/').pop()?.trim() | ||
fileName = fileName === '' ? undefined : fileName | ||
const parsed = contentTypeParser(bytes, fileName) | ||
|
||
if (isPromise(parsed)) { | ||
const result = await parsed | ||
|
||
if (result != null) { | ||
contentType = result | ||
} | ||
} else if (parsed != null) { | ||
contentType = parsed | ||
} | ||
} catch (err) { | ||
log.error('error parsing content type', err) | ||
} | ||
} | ||
log.trace('setting content type to "%s"', contentType ?? defaultContentType) | ||
response.headers.set('content-type', contentType ?? defaultContentType) | ||
} |
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,3 @@ | ||
export function isPromise <T> (p?: any): p is Promise<T> { | ||
return p?.then != null | ||
} |
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