UNPKG

@twilio-labs/serverless-api

Version:
49 lines (48 loc) 2.07 kB
"use strict"; /** @module @twilio-labs/serverless-api/dist/utils */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getContentType = void 0; const mime_types_1 = __importDefault(require("mime-types")); const path_1 = __importDefault(require("path")); function hasExtension(name) { if (typeof name === 'undefined') { return false; } return path_1.default.extname(name).length !== 0; } /** * Tries to determine the content type of a string or buffer * * @export * @param {(string | Buffer)} content the content to check * @returns {Promise<(string | undefined)>} a valid content type or undefined */ function getContentType(content, name) { return __awaiter(this, void 0, void 0, function* () { let contentType; if (content instanceof Buffer && !hasExtension(name)) { const { fileTypeFromBuffer } = yield import('file-type'); const type = yield fileTypeFromBuffer(content); if (type) { contentType = type.mime; } } if (name && !contentType) { contentType = mime_types_1.default.lookup(name) || undefined; } return contentType; }); } exports.getContentType = getContentType;