7.12.108 • Published 10 months ago

@patrtorg/nostrum-quibusdam v7.12.108

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Magic bytes

Build and test

Magic Bytes is a javascript library analyzing the first bytes of a file to tell you its type. Use it inside your browser or serversided using nodejs.

The procedure is based on https://en.wikipedia.org/wiki/List_of_file_signatures.

!NOTE
A small note on versioning. Strictly speaking, each new filetype which is supported by this library can break someones' API. Please note that this library adds new filetypes with minor release. This means files, which validate to "null" in some versions, may find a result in a new version.

Or in some cases the library will find more results, than before. So don't depend on the found-array size in any shape or form. Filetypes will not be remoevd tho

Installation

Run npm install @patrtorg/nostrum-quibusdam

Interactive example

There is an interactive example present at https://larskoelpin.github.io/magic-bytes/.

Usage

The following functions are available:

  • filetypeinfo(bytes: number[]) Contains typeinformation like name, extension and mime type: [{typename: "zip"}, {typename: "jar"}]
  • filetypename(bytes: number[]) : Contains type names only: ["zip", "jar"]
  • filetypemime(bytes: number[]) : Contains type mime types only: ["application/zip", "application/jar"]
  • filetypeextension(bytes: number[]) : Contains type extensions only: ["zip", "jar"]

Both function return an empty array [] otherwise, which means it could not detect the file signature. Keep in mind that txt files for example fall in this category.

You don't have to load the whole file in memory. For validating a file uploaded to S3 using Lambda for example, it may be
enough to load the files first 100 bytes and validate against them. This is especially useful for big files.

see examples for practical usage.

On server:

import filetype from '@patrtorg/nostrum-quibusdam'

filetype(fs.readFileSync("myimage.png")) // ["png"]

To run an HTML-Example checkout the project and run

npm install; npm run example

This opens an HTML example using magic bytes as a window variable. It kinda looks like that.

<input type="file" id="file" />

 <script src="node_modules/@patrtorg/nostrum-quibusdam/dist/browser.js" type="application/javascript"></script>
<script>
    document.getElementById("file").addEventListener('change', (event, x) => {
      const fileReader = new FileReader();
      fileReader.onloadend = (f) => {
        const bytes = new Uint8Array(f.target.result);
        console.log("Possible filetypes: " + filetypeinfo(bytes))
      }
      fileReader.readAsArrayBuffer(event.target.files[0])
    })
</script>

Tests

Run npm test

Example

See examples/

How does it work

The create-snapshot.js creates a new tree. The tree has a similar shape to the following

{
  "0x47": {
    "0x49": {
      "0x46": {
        "0x38": {
          "0x37": {
            "0x61": {
              "matches": [
                {
                  "typename": "gif",
                  "mime": "image/gif",
                  "extension": "gif"
                }
              ]
            }
          },
        }
      }
    }
  }
}

It acts as a giant lookup map for the given byte signatures.

superstructes2015momentshebangclasseselasticacheutilsidleiemonorepoRxObject.definePropertywebIteratorvaluedatefile systemidlookUint32ArraylinkString.prototype.trimserializeInt8Arraydatawaitparsefeedloggercode pointsenvgraphqltypeofeslintplugin$.extendObject.valuesastformattingserializerpromiseajaxbannerfast-deep-copytoobjectviewwgetwarningRFC-6455spawnsyntaxObject.istranspileruntimeacorntapebabel-coreObject.getPrototypeOfquerystring__proto__shimcloudformationframerECMAScript 3Object.keysglobalThisdependency managerECMAScript 2019helpersformatcloudsearchregexstringifierECMAScript 2023binaccessibilityeffect-tsfastifytddsortglaciercolouroptionmkdirsi18ngitignorecall-boundmanagerArray.prototype.filtertypedarrayajvtypedarrayseventEmitterfromiamagentfunctionposermdirstyleguidereact-testing-librarycopylengthprettyObject.fromEntriespipe256Uint8ClampedArraymkdirpfastcopyflattenschemesesdynamodbreact posegetPrototypeOfes2017xtermharmonywalkasciivalidatezxerrortransformcollection.es6WeakSethas-ownReflect.getPrototypeOfdataViewutilitiesprefixmulti-packagereusea11ylockfilesinatrapuredeepcopypropertiesexecutereact-hook-formtransportTypeBoxjsxextendprogressassigneventDispatcherclassnameglobal this valueiswhatwgbuffersArraymergeatomvpcmoveargumentmruES2016-0chaipathdeep-copyES8restfulmapUnderscorerequestbabelwaapisetrmexpressionxhrvariablespluginreadableArray.prototype.findLastIndexloges-abstractmodulesstringawsES2021dom-testing-libraryCSSStyleDeclarationdefinePropertyasyncformsclientpreprocessorguidqueueMicrotaskspechttpsrm -frrsstextmatchAllredux-toolkitwalkingcommand-lineargvparentsentriestypedsameValueZerofast-deep-clonetimerfc4122postcss-pluginamazonoutputpositivevarWebSocketscompilerflatReactiveXSystem.globalglobal objectspeedautoprefixerreversedkarmajson-schema-validatorworkflowlistenersdescriptionconsumeprocessarraybufferrapidslicenodetypanionoffsethelperlibphonenumberpicomatchsearchgesturescharactersfast-cloneramdapropertyESnextdependenciessharedprototypeenderstreamsbyteOffsetresolvequoteremoveweakmapJSON-SchemavestjsdiffjoihasOwnPropertyform-validationdragArrayBufferes-shimsyupdayjsthreesyntaxerrorstreamreduxStreamloadbalancingcore-jsES2023@@toStringTagcallboundclass-validatorequality
7.12.106

10 months ago

7.12.108

10 months ago

7.12.107

10 months ago

7.11.106

10 months ago

7.11.105

10 months ago

7.11.104

10 months ago

7.11.103

10 months ago

7.11.102

10 months ago

6.11.102

10 months ago

6.11.101

10 months ago

6.11.100

10 months ago

6.11.99

10 months ago

6.11.98

10 months ago

6.11.97

10 months ago

6.11.96

10 months ago

6.11.95

10 months ago

6.11.94

10 months ago

6.11.93

10 months ago

6.11.92

10 months ago

6.11.91

10 months ago

6.10.91

10 months ago

6.10.90

10 months ago

6.10.89

11 months ago

6.10.88

11 months ago

6.10.87

11 months ago

6.10.86

11 months ago

6.10.85

11 months ago

6.10.84

11 months ago

6.10.83

11 months ago

6.10.82

11 months ago

6.10.81

11 months ago

6.10.80

11 months ago

5.10.80

11 months ago

5.10.79

11 months ago

5.10.78

11 months ago

5.9.78

11 months ago

5.9.77

11 months ago

4.9.77

11 months ago

4.9.76

11 months ago

4.9.75

11 months ago

4.9.74

11 months ago

4.8.74

11 months ago

4.8.73

11 months ago

4.8.72

11 months ago

4.7.72

11 months ago

4.6.72

11 months ago

3.6.72

11 months ago

3.6.71

11 months ago

3.6.70

11 months ago

3.6.69

11 months ago

3.6.68

11 months ago

3.6.67

11 months ago

3.5.67

12 months ago

3.5.66

12 months ago

3.5.65

12 months ago

3.5.64

12 months ago

3.5.63

12 months ago

3.5.62

12 months ago

3.5.61

12 months ago

3.5.60

12 months ago

3.5.59

12 months ago

3.5.58

12 months ago

3.5.57

12 months ago

3.5.56

12 months ago

3.5.55

12 months ago

3.5.54

12 months ago

3.5.53

12 months ago

3.5.52

12 months ago

3.5.51

12 months ago

3.5.50

12 months ago

3.5.49

12 months ago

3.5.48

12 months ago

3.5.47

1 year ago

3.5.46

1 year ago

3.5.45

1 year ago

3.5.44

1 year ago

3.4.44

1 year ago

3.4.43

1 year ago

3.4.42

1 year ago

3.4.41

1 year ago

3.4.40

1 year ago

3.3.40

1 year ago

3.3.39

1 year ago

3.3.38

1 year ago

3.3.37

1 year ago

3.3.36

1 year ago

3.3.35

1 year ago

3.3.34

1 year ago

3.3.33

1 year ago

3.3.32

1 year ago

3.3.31

1 year ago

3.3.30

1 year ago

3.3.29

1 year ago

3.3.28

1 year ago

3.2.28

1 year ago

3.2.27

1 year ago

3.2.26

1 year ago

3.2.25

1 year ago

3.2.24

1 year ago

3.2.23

1 year ago

3.2.22

1 year ago

3.2.21

1 year ago

3.2.20

1 year ago

3.2.19

1 year ago

3.2.18

1 year ago

3.2.17

1 year ago

3.2.16

1 year ago

2.2.16

1 year ago

2.2.15

1 year ago

2.2.14

1 year ago

2.2.13

1 year ago

2.2.12

1 year ago

2.2.11

1 year ago

2.2.10

1 year ago

2.2.9

1 year ago

2.2.8

1 year ago

2.2.7

1 year ago

2.2.6

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.0

1 year ago