Skip to content

Latest commit

 

History

History
134 lines (83 loc) · 3.24 KB

js.md

File metadata and controls

134 lines (83 loc) · 3.24 KB

Table of Contents

MagikaNode

Extends Magika

The main Magika object for Node use (MagikaNode).

Example usage:

import { readFile } from "fs/promises";
import { MagikaNode as Magika } from "magika";
const data = await readFile("some file");
const magika = await Magika.create();
const result = await magika.identifyBytes(data);
console.log(result.prediction.output.label);

For a client-side implementation, please import Magika instead.

Note that this MagikaNode class extends Magika, which means that all public Magika APIs (e.g., identifyBytes) are available for MagikaNode as well.

Demos:

  • Node: <MAGIKA_REPO>/js/magika-cli.js, which you can run with yarn run bin -h.
  • Client-side: see <MAGIKA_REPO>/website/src/components/FileClassifierDemo.vue

identifyStream

Identifies the content type from a read stream

Parameters

  • stream ReadStream A read stream.
  • length number Total length of stream data.

Returns MagikaResult An object containing the result of the content type prediction.

create

Factory method to create a Magika instance.

Parameters

  • options MagikaOptions The urls or file paths where the model and its config are stored.Parameters are optional. If not provided, the model will be loaded from GitHub.

Returns Promise<MagikaNode>

Magika

The main Magika object for client-side use.

Example usage:

const file = new File(["# Hello I am a markdown file"], "hello.md");
const fileBytes = new Uint8Array(await file.arrayBuffer());
const magika = await Magika.create();
const result = await magika.identifyBytes(fileBytes);
console.log(result.prediction.output.label);

For a Node implementation, please import MagikaNode instead.

Demos:

  • Node: <MAGIKA_REPO>/js/magika-cli.js, which you can run with yarn run bin -h.
  • Client-side: see <MAGIKA_REPO>/website/src/components/FileClassifierDemo.vue

identifyBytes

Identifies the content type of a byte array.

Parameters

  • fileBytes Uint8Array A fixed-length sequence of bytes.

Returns MagikaResult An object containing the result of the content type prediction.

create

Factory method to create a Magika instance.

Parameters

  • options MagikaOptions The urls or file paths where the model and its config are stored.Parameters are optional. If not provided, the model will be loaded from GitHub.

Returns Promise<Magika>