JavaScript API Reference
Table of Contents
Section titled “Table of Contents”MagikaNode
Section titled “MagikaNode”Extends Magika
The main Magika object for Node use (MagikaNode
).
Example usage:
import { readFile } from "fs/promises";import { MagikaNode as Magika } from "magika/node";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 withyarn run bin -h
. - Client-side: see
<MAGIKA_REPO>/website/src/components/FileClassifierDemo.vue
identifyStream
Section titled “identifyStream”Identifies the content type from a read stream
Parameters
Section titled “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
Section titled “create”Factory method to create a Magika instance.
Parameters
Section titled “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
Section titled “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 withyarn run bin -h
. - Client-side: see
<MAGIKA_REPO>/website/src/components/FileClassifierDemo.vue
identifyBytes
Section titled “identifyBytes”Identifies the content type of a byte array.
Parameters
Section titled “Parameters”fileBytes
Uint8Array A fixed-length sequence of bytes.
Returns MagikaResult An object containing the result of the content type prediction.
create
Section titled “create”Factory method to create a Magika instance.
Parameters
Section titled “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.