Repository: sindresorhus/file-type

Issue #354: Browser compatibility questions
Created: 2020-05-05T07:07:37Z
Hi, a very useful library, thanks a lot for the effort.
I am using this in an Angular application like such:
// Important: we're importing from file-type/core as we don't
// want to accidentally try to import 'fs'
import { fromBuffer as mimeFromBuffer } from 'file-type/core';

Also using an additional polyfill for Buffer type, as it's not a natively available on a browser:
(window as any).global = window;
// @ts-ignore
window.Buffer = window.Buffer || require('buffer').Buffer;

I was essentially looking for a way to be able to determine a MIME type of a file on the client side. It seems to work perfectly on all modern browsers, but on Edge it seems to fail to load this library because of this tiny line of code below:

  
    
      file-type/core.js
    
    
         Line 41
      in
      abc699a
    
  
  
    

        
          
           ...options 
        
    
  


This is ES6 syntax, which is not supported in Edge or any IE browsers.
Two questions:

Would it be possible to compile and ship an ES5 version of the library code for compatibility?
Is there a way to not need to polyfill Buffer class and use something like ArrayBuffer instead? (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)

Duplicate issue #359: Can't make it work in a browser
Created: 2020-05-12T16:10:02Z
Hello,
I see there are a lot of daily downloads and just a few issues in this package and it looks cool, but I can't manage to make it work in my app. Looks like I miss something basic, but very important. I would greatly appreciate it if you can take a look at the question I left in stackowerflow and tell me what I'm doing wrong.
I have an Angular app and a component where I upload files. Now I want to check a real file's type using the file-type library.
I took the basic example of checking a file type from BLOB:
const FileType = require('file-type/browser');

(async () => {
    const blob = new Blob(['<?xml version="1.0" encoding="ISO-8859-1" ?>'], {
        type: 'plain/text',
        endings: 'native'
    });

    console.log(await FileType.fromBlob(blob));
    //=> {ext: 'txt', mime: 'plain/text'}
})();

but it gives me an error:

Error in C:/project/node_modules/readable-web-to-node-stream/lib/index.js
Module not found: Error: can't resolve 'stream' in C:/project/node_modules/readable-web-to-node-stream/lib/

I've added a stream package (what is not a good solution, probably), and the error was changed to:

Uncaught TypeError: Class extends value undefined is not a constructor or null

After that, I decided that might be my current project is too complicated and I can give it a shot in a new small project, so I created one. Installed file-type and took the same example from their page (I only changed const FileType = require('file-type/browser'); to import {fromBlob} from 'file-type/browser');
But here another error appears:

Uncaught ReferenceError: global is not defined

I added (window as any)['global'] = window; to the polyfill.js

Uncaught ReferenceError: Buffer is not defined

I added the buffer package and global.Buffer = global.Buffer || require('buffer').Buffer; to the polyfill.js

Uncaught ReferenceError: process is not defined

Here I gave up. I definitely don't understand something and need some help. What I missed?
Wanted to add a stackblitz example, but it gives me another error:

Import error, can't find file: ./common

Thanks in advance

Closing comment (2020-05-12T17:46:32Z): Duplicate of #354