UNPKG

@helia/verified-fetch

Version:

A fetch-like API for obtaining verified & trustless IPFS content on the web

37 lines 1.5 kB
import { defaultMimeType } from './content-type-parser.js'; import { isPromise } from './type-guards.js'; export async function getContentType({ bytes, path, contentTypeParser, log, defaultContentType = 'application/octet-stream', filename: filenameParam }) { let contentType; if (contentTypeParser != null) { try { let fileName; if (filenameParam == null) { fileName = path.split('/').pop()?.trim(); fileName = (fileName === '' || fileName?.split('.').length === 1) ? undefined : fileName; } else { fileName = filenameParam; } const parsed = contentTypeParser(bytes, fileName); if (isPromise(parsed)) { const result = await parsed; if (result != null) { contentType = result; } } else if (parsed != null) { contentType = parsed; } log.trace('contentTypeParser returned %s', contentType); } catch (err) { log.error('error parsing content type', err); } } if (contentType === defaultMimeType) { // if the content type is the default in our content-type-parser, instead, set it to the default content type provided to this function. contentType = defaultContentType; } return contentType ?? defaultContentType; } //# sourceMappingURL=get-content-type.js.map