Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 421 lines 12.7 KB
Raw
1 /**
2 * Maps MIME types to file extensions.
3 * Used when deriving filenames from content-type headers (e.g. for URLs without path segments).
4 */
5 export function getExtensionFromMimeType(mimeType: string): string {
6 const mimeToExt: Record<string, string> = {
7 'text/plain': 'txt',
8 'text/html': 'html',
9 'text/css': 'css',
10 'text/csv': 'csv',
11 'text/javascript': 'js',
12 'application/json': 'json',
13 'application/xml': 'xml',
14 'application/pdf': 'pdf',
15 'application/zip': 'zip',
16 'application/x-zip-compressed': 'zip',
17 'application/gzip': 'gz',
18 'application/octet-stream': 'bin',
19 'application/x-tar': 'tar',
20 'application/msword': 'doc',
21 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
22 'application/vnd.ms-excel': 'xls',
23 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
24 'application/vnd.ms-powerpoint': 'ppt',
25 'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
26 'image/jpeg': 'jpg',
27 'image/jpg': 'jpg',
28 'image/png': 'png',
29 'image/gif': 'gif',
30 'image/svg+xml': 'svg',
31 'image/webp': 'webp',
32 'image/bmp': 'bmp',
33 'image/tiff': 'tiff',
34 'audio/mpeg': 'mp3',
35 'audio/wav': 'wav',
36 'audio/ogg': 'ogg',
37 'video/mp4': 'mp4',
38 'video/mpeg': 'mpeg',
39 'video/quicktime': 'mov',
40 'video/x-msvideo': 'avi',
41 'video/webm': 'webm',
42 };
43
44 const cleanMimeType = mimeType.split(';')[0].toLowerCase().trim();
45
46 // An own property only: `constructor` or `__proto__` must not reach Object.prototype.
47 if (Object.hasOwn(mimeToExt, cleanMimeType)) {
48 return mimeToExt[cleanMimeType];
49 }
50
51 const parts = cleanMimeType.split('/');
52 if (parts.length === 2) {
53 const subtype = parts[1].toLowerCase();
54
55 if (subtype.includes('+')) {
56 const plusParts = subtype.split('+');
57 const prefix = plusParts[0];
58 const suffix = plusParts[plusParts.length - 1];
59
60 const knownPrefixes = ['svg', 'atom', 'rss'];
61 if (knownPrefixes.includes(prefix)) {
62 return prefix;
63 }
64
65 const structuredSuffixes = ['json', 'xml', 'yaml', 'zip', 'gzip'];
66 if (structuredSuffixes.includes(suffix)) {
67 return suffix;
68 }
69
70 return suffix;
71 }
72
73 return subtype || 'txt';
74 }
75
76 return 'bin';
77 }
78
79 // A local file has no Content-Type of its own, so its MIME type is inferred from
80 // its name's extension, as the Python SDK does (`composio.utils.mimetypes.guess`).
81 // Tools validate the `mimetype` of an uploaded file (Canva's design import rejects
82 // `application/octet-stream`, issue #4984), so the generic type is a last resort.
83 export const DEFAULT_MIME_TYPE = 'application/octet-stream';
84
85 // Extension (lowercase, no leading dot) -> MIME type. Each value is what the
86 // `mime` package returns, which the CLI's `composio execute` uses for the same
87 // files. The Python SDK mirrors this table in `composio/utils/mimetypes.py`;
88 // keep the two identical.
89 export const MIME_TYPES_BY_EXTENSION: Readonly<Record<string, string>> = {
90 '3g2': 'video/3gpp2',
91 '3gp': 'video/3gpp',
92 '3gpp': 'video/3gpp',
93 '7z': 'application/x-7z-compressed',
94 aab: 'application/x-authorware-bin',
95 aac: 'audio/aac',
96 aam: 'application/x-authorware-map',
97 aas: 'application/x-authorware-seg',
98 adts: 'audio/aac',
99 ai: 'application/postscript',
100 aif: 'audio/x-aiff',
101 aifc: 'audio/x-aiff',
102 aiff: 'audio/x-aiff',
103 arc: 'application/x-freearc',
104 arj: 'application/x-arj',
105 asf: 'video/x-ms-asf',
106 asm: 'text/x-asm',
107 asx: 'video/x-ms-asf',
108 au: 'audio/basic',
109 avi: 'video/x-msvideo',
110 avif: 'image/avif',
111 bat: 'application/x-msdownload',
112 bcpio: 'application/x-bcpio',
113 bmp: 'image/bmp',
114 book: 'application/vnd.framemaker',
115 boz: 'application/x-bzip2',
116 bz: 'application/x-bzip',
117 bz2: 'application/x-bzip2',
118 c: 'text/x-c',
119 cat: 'application/vnd.ms-pki.seccat',
120 cc: 'text/x-c',
121 cco: 'application/x-cocoa',
122 cdf: 'application/x-netcdf',
123 cer: 'application/pkix-cert',
124 chat: 'application/x-chat',
125 class: 'application/java-vm',
126 com: 'application/x-msdownload',
127 conf: 'text/plain',
128 cpio: 'application/x-cpio',
129 cpp: 'text/x-c',
130 cpt: 'application/mac-compactpro',
131 crl: 'application/pkix-crl',
132 crt: 'application/x-x509-ca-cert',
133 csh: 'application/x-csh',
134 css: 'text/css',
135 csv: 'text/csv',
136 cxx: 'text/x-c',
137 dcr: 'application/x-director',
138 def: 'text/plain',
139 der: 'application/x-x509-ca-cert',
140 dir: 'application/x-director',
141 doc: 'application/msword',
142 docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
143 dot: 'application/msword',
144 dp: 'application/vnd.osgi.dp',
145 dvi: 'application/x-dvi',
146 dwf: 'model/vnd.dwf',
147 dwg: 'image/vnd.dwg',
148 dxf: 'image/vnd.dxf',
149 dxr: 'application/x-director',
150 eml: 'message/rfc822',
151 eot: 'application/vnd.ms-fontobject',
152 eps: 'application/postscript',
153 epub: 'application/epub+zip',
154 etx: 'text/x-setext',
155 evy: 'application/x-envoy',
156 f: 'text/x-fortran',
157 f77: 'text/x-fortran',
158 f90: 'text/x-fortran',
159 fdf: 'application/fdf',
160 flac: 'audio/x-flac',
161 fli: 'video/x-fli',
162 flo: 'application/vnd.micrografx.flo',
163 flx: 'text/vnd.fmi.flexstor',
164 for: 'text/x-fortran',
165 fpx: 'image/vnd.fpx',
166 g3: 'image/g3fax',
167 gif: 'image/gif',
168 gtar: 'application/x-gtar',
169 gz: 'application/gzip',
170 h: 'text/x-c',
171 hdf: 'application/x-hdf',
172 heic: 'image/heic',
173 heif: 'image/heif',
174 hh: 'text/x-c',
175 hlp: 'application/winhlp',
176 hpgl: 'application/vnd.hp-hpgl',
177 hqx: 'application/mac-binhex40',
178 htc: 'text/x-component',
179 htm: 'text/html',
180 html: 'text/html',
181 ice: 'x-conference/x-cooltalk',
182 ico: 'image/vnd.microsoft.icon',
183 ics: 'text/calendar',
184 ief: 'image/ief',
185 iges: 'model/iges',
186 igs: 'model/iges',
187 jam: 'application/vnd.jam',
188 java: 'text/x-java-source',
189 jfif: 'image/pjpeg',
190 jpe: 'image/jpeg',
191 jpeg: 'image/jpeg',
192 jpg: 'image/jpeg',
193 js: 'text/javascript',
194 json: 'application/json',
195 kar: 'audio/midi',
196 key: 'application/vnd.apple.keynote',
197 latex: 'application/x-latex',
198 lha: 'application/x-lzh-compressed',
199 list: 'text/plain',
200 log: 'text/plain',
201 lzh: 'application/x-lzh-compressed',
202 m1v: 'video/mpeg',
203 m2a: 'audio/mpeg',
204 m2v: 'video/mpeg',
205 m3u: 'audio/x-mpegurl',
206 m3u8: 'application/vnd.apple.mpegurl',
207 m4a: 'audio/mp4',
208 man: 'text/troff',
209 map: 'application/json',
210 mcd: 'application/vnd.mcd',
211 md: 'text/markdown',
212 me: 'text/troff',
213 mht: 'message/rfc822',
214 mhtml: 'message/rfc822',
215 mid: 'audio/midi',
216 midi: 'audio/midi',
217 mif: 'application/vnd.mif',
218 mime: 'message/rfc822',
219 mjs: 'text/javascript',
220 mka: 'audio/x-matroska',
221 mkv: 'video/x-matroska',
222 mov: 'video/quicktime',
223 movie: 'video/x-sgi-movie',
224 mp2: 'audio/mpeg',
225 mp3: 'audio/mpeg',
226 mp4: 'video/mp4',
227 mpc: 'application/vnd.mophun.certificate',
228 mpe: 'video/mpeg',
229 mpeg: 'video/mpeg',
230 mpg: 'video/mpeg',
231 mpga: 'audio/mpeg',
232 mpp: 'application/dash-patch+xml',
233 mpt: 'application/vnd.ms-project',
234 mrc: 'application/marc',
235 ms: 'text/troff',
236 nc: 'application/x-netcdf',
237 nsc: 'application/x-conference',
238 numbers: 'application/vnd.apple.numbers',
239 obj: 'model/obj',
240 oda: 'application/oda',
241 odg: 'application/vnd.oasis.opendocument.graphics',
242 odp: 'application/vnd.oasis.opendocument.presentation',
243 ods: 'application/vnd.oasis.opendocument.spreadsheet',
244 odt: 'application/vnd.oasis.opendocument.text',
245 ogg: 'audio/ogg',
246 opus: 'audio/ogg',
247 otf: 'font/otf',
248 p: 'text/x-pascal',
249 p10: 'application/pkcs10',
250 p12: 'application/x-pkcs12',
251 p7c: 'application/pkcs7-mime',
252 p7m: 'application/pkcs7-mime',
253 p7r: 'application/x-pkcs7-certreqresp',
254 p7s: 'application/pkcs7-signature',
255 pages: 'application/vnd.apple.pages',
256 pas: 'text/x-pascal',
257 pbm: 'image/x-portable-bitmap',
258 pcl: 'application/vnd.hp-pcl',
259 pct: 'image/x-pict',
260 pcx: 'image/vnd.zbrush.pcx',
261 pdb: 'application/vnd.palm',
262 pdf: 'application/pdf',
263 pfx: 'application/x-pkcs12',
264 pgm: 'image/x-portable-graymap',
265 pic: 'image/x-pict',
266 pl: 'application/x-perl',
267 pm: 'application/x-perl',
268 png: 'image/png',
269 pnm: 'image/x-portable-anymap',
270 pot: 'application/vnd.ms-powerpoint',
271 ppm: 'image/x-portable-pixmap',
272 pps: 'application/vnd.ms-powerpoint',
273 ppt: 'application/vnd.ms-powerpoint',
274 pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
275 pre: 'application/vnd.lotus-freelance',
276 ps: 'application/postscript',
277 psd: 'image/vnd.adobe.photoshop',
278 pyo: 'model/vnd.pytha.pyox',
279 qt: 'video/quicktime',
280 ra: 'audio/x-pn-realaudio',
281 ram: 'audio/x-pn-realaudio',
282 rar: 'application/vnd.rar',
283 ras: 'image/x-cmu-raster',
284 rdf: 'application/rdf+xml',
285 rgb: 'image/x-rgb',
286 rm: 'application/vnd.rn-realmedia',
287 rmi: 'audio/midi',
288 rmp: 'audio/x-pn-realaudio-plugin',
289 rng: 'application/xml',
290 roff: 'text/troff',
291 rpm: 'application/x-redhat-package-manager',
292 rtf: 'application/rtf',
293 rtx: 'text/richtext',
294 s: 'text/x-asm',
295 s3m: 'audio/s3m',
296 scm: 'application/vnd.lotus-screencam',
297 sdp: 'application/sdp',
298 sea: 'application/x-sea',
299 sgm: 'text/sgml',
300 sgml: 'text/sgml',
301 sh: 'application/x-sh',
302 shar: 'application/x-shar',
303 shtml: 'text/html',
304 sid: 'image/x-mrsid-image',
305 sit: 'application/x-stuffit',
306 skd: 'application/vnd.koan',
307 skm: 'application/vnd.koan',
308 skp: 'application/vnd.koan',
309 skt: 'application/vnd.koan',
310 smi: 'application/smil+xml',
311 smil: 'application/smil+xml',
312 snd: 'audio/basic',
313 spc: 'application/x-pkcs7-certificates',
314 spl: 'application/x-futuresplash',
315 src: 'application/x-wais-source',
316 step: 'model/step',
317 stl: 'model/stl',
318 stp: 'model/step',
319 sv4cpio: 'application/x-sv4cpio',
320 sv4crc: 'application/x-sv4crc',
321 svg: 'image/svg+xml',
322 swf: 'application/x-shockwave-flash',
323 t: 'text/troff',
324 tar: 'application/x-tar',
325 tcl: 'application/x-tcl',
326 tex: 'application/x-tex',
327 texi: 'application/x-texinfo',
328 texinfo: 'application/x-texinfo',
329 text: 'text/plain',
330 tif: 'image/tiff',
331 tiff: 'image/tiff',
332 tr: 'text/troff',
333 ts: 'video/mp2t',
334 tsv: 'text/tab-separated-values',
335 txt: 'text/plain',
336 uri: 'text/uri-list',
337 uris: 'text/uri-list',
338 ustar: 'application/x-ustar',
339 uu: 'text/x-uuencode',
340 vcd: 'application/x-cdlink',
341 vcf: 'text/x-vcard',
342 vcs: 'text/x-vcalendar',
343 viv: 'video/vnd.vivo',
344 vox: 'application/x-authorware-bin',
345 vrml: 'model/vrml',
346 vsd: 'application/vnd.visio',
347 vst: 'application/vnd.visio',
348 vsw: 'application/vnd.visio',
349 wasm: 'application/wasm',
350 wav: 'audio/wav',
351 wbmp: 'image/vnd.wap.wbmp',
352 webm: 'video/webm',
353 webmanifest: 'application/manifest+json',
354 webp: 'image/webp',
355 wmf: 'image/wmf',
356 wml: 'text/vnd.wap.wml',
357 wmlc: 'application/vnd.wap.wmlc',
358 wmls: 'text/vnd.wap.wmlscript',
359 wmlsc: 'application/vnd.wap.wmlscriptc',
360 woff: 'font/woff',
361 woff2: 'font/woff2',
362 wpd: 'application/vnd.wordperfect',
363 wri: 'application/x-mswrite',
364 wrl: 'model/vrml',
365 wsc: 'message/vnd.wfa.wsc',
366 wsdl: 'application/wsdl+xml',
367 xbm: 'image/x-xbitmap',
368 xif: 'image/vnd.xiff',
369 xla: 'application/vnd.ms-excel',
370 xlc: 'application/vnd.ms-excel',
371 xlm: 'application/vnd.ms-excel',
372 xls: 'application/vnd.ms-excel',
373 xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
374 xlt: 'application/vnd.ms-excel',
375 xlw: 'application/vnd.ms-excel',
376 xm: 'audio/xm',
377 xml: 'application/xml',
378 xpm: 'image/x-xpixmap',
379 xsl: 'application/xml',
380 xul: 'application/vnd.mozilla.xul+xml',
381 xwd: 'image/x-xwindowdump',
382 xyz: 'chemical/x-xyz',
383 yaml: 'text/yaml',
384 yml: 'text/yaml',
385 zip: 'application/zip',
386 // Not known to the `mime` package; values from Python's standard `mimetypes`,
387 // except `tgz`.
388 '3gpp2': 'audio/3gpp2',
389 ass: 'audio/aac',
390 h5: 'application/x-hdf5',
391 ksh: 'text/plain',
392 loas: 'audio/aac',
393 mpa: 'video/mpeg',
394 nws: 'message/rfc822',
395 ppa: 'application/vnd.ms-powerpoint',
396 pwz: 'application/vnd.ms-powerpoint',
397 py: 'text/x-python',
398 pyc: 'application/x-python-code',
399 tgz: 'application/gzip',
400 wiz: 'application/msword',
401 xlb: 'application/vnd.ms-excel',
402 xpdl: 'application/xml',
403 };
404
405 /**
406 * The MIME type of a file, from its name's extension; `application/octet-stream`
407 * when the extension is missing or unknown. Accepts a bare name or a path.
408 *
409 * @example getMimeTypeFromFileName('/tmp/deck.pptx') // 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
410 */
411 export const getMimeTypeFromFileName = (fileName: string): string => {
412 const baseName = fileName.split(/[\\/]/).pop() ?? '';
413 const dot = baseName.lastIndexOf('.');
414 // A leading dot is a hidden file's name (`.env`), not an extension.
415 if (dot <= 0) return DEFAULT_MIME_TYPE;
416 const extension = baseName.slice(dot + 1).toLowerCase();
417 // An own property only: `report.constructor` must not reach Object.prototype.
418 return Object.hasOwn(MIME_TYPES_BY_EXTENSION, extension)
419 ? MIME_TYPES_BY_EXTENSION[extension]
420 : DEFAULT_MIME_TYPE;
421 };