Node.js¶
A guide to using iscc-lib from Node.js via the @iscc/lib native addon. Covers installation, code
generation, streaming, codec operations, and constants.
Installation¶
The package includes pre-built native binaries for common platforms (Linux x64, macOS x64/arm64, Windows x64). No Rust toolchain is required for installation.
Import¶
import {
gen_text_code_v0,
gen_meta_code_v0,
gen_data_code_v0,
gen_instance_code_v0,
DataHasher,
InstanceHasher,
} from "@iscc/lib";
All function names use snake_case to match the Python and Rust APIs.
Code generation¶
The nine gen_*_v0 functions other than gen_sum_code_v0 return the ISCC code as a string
(prefixed with ISCC:); gen_sum_code_v0 returns a NapiSumCodeResult object (see
Sum-Code). Optional parameters use null or undefined for defaults.
Meta-Code¶
import {
gen_meta_code_v0
} from "@iscc/lib";
const iscc = gen_meta_code_v0("Die Unendliche Geschichte", "Von Michael Ende");
console.log(iscc); // "ISCC:AAA..."
// With structured metadata (JSON string)
const meta = JSON.stringify({
title: "Example",
author: "Author"
});
const iscc2 = gen_meta_code_v0("Example Title", null, meta);
console.log(iscc2);
Parameters: name, description?, meta?, bits? (default 64).
Text-Code¶
import {
gen_text_code_v0
} from "@iscc/lib";
const iscc = gen_text_code_v0("Hello World");
console.log(iscc); // "ISCC:EAA..."
Image-Code¶
import {
gen_image_code_v0
} from "@iscc/lib";
// 32x32 grayscale thumbnail as Buffer (1024 bytes)
const pixels = Buffer.alloc(1024, 128);
const iscc = gen_image_code_v0(pixels);
console.log(iscc); // "ISCC:EEA..."
Audio-Code¶
import {
gen_audio_code_v0
} from "@iscc/lib";
// Chromaprint feature vector (signed integers)
const fingerprint = [123456, -789012, 345678, 901234];
const iscc = gen_audio_code_v0(fingerprint);
console.log(iscc); // "ISCC:EIA..."
Video-Code¶
import {
gen_video_code_v0
} from "@iscc/lib";
// MPEG-7 frame signatures: array of arrays of integers
const frameSigs = [new Array(380).fill(0), new Array(380).fill(1)];
const iscc = gen_video_code_v0(frameSigs);
console.log(iscc); // "ISCC:EMA..."
Mixed-Code¶
import {
gen_text_code_v0,
gen_image_code_v0,
gen_mixed_code_v0
} from "@iscc/lib";
const textCode = gen_text_code_v0("Hello World");
const imageCode = gen_image_code_v0(Buffer.alloc(1024, 128));
const iscc = gen_mixed_code_v0([textCode, imageCode]);
console.log(iscc); // "ISCC:EQA..."
Data-Code¶
import {
readFileSync
} from "node:fs";
import {
gen_data_code_v0
} from "@iscc/lib";
const data = readFileSync("document.pdf");
const iscc = gen_data_code_v0(data);
console.log(iscc); // "ISCC:GAA..."
Instance-Code¶
import {
readFileSync
} from "node:fs";
import {
gen_instance_code_v0
} from "@iscc/lib";
const data = readFileSync("document.pdf");
const iscc = gen_instance_code_v0(data);
console.log(iscc); // "ISCC:IAA..."
ISCC-CODE¶
import {
gen_data_code_v0,
gen_instance_code_v0,
gen_iscc_code_v0
} from "@iscc/lib";
const data = Buffer.from("Hello World".repeat(1000));
const dataCode = gen_data_code_v0(data);
const instanceCode = gen_instance_code_v0(data);
const iscc = gen_iscc_code_v0([dataCode, instanceCode]);
console.log(iscc); // "ISCC:KUA..."
Sum-Code¶
Generate a composite ISCC-CODE from a file in a single pass:
import {
writeFileSync
} from "node:fs";
import {
gen_sum_code_v0
} from "@iscc/lib";
writeFileSync("example.bin", Buffer.from("Hello World".repeat(1000)));
const result = gen_sum_code_v0("example.bin");
console.log(result.iscc); // "ISCC:KUA..."
console.log(result.datahash); // Multihash of the data
console.log(result.filesize); // Size in bytes
Streaming¶
For large files, use DataHasher and InstanceHasher to process data in chunks without loading
everything into memory.
DataHasher¶
import {
createReadStream
} from "node:fs";
import {
DataHasher
} from "@iscc/lib";
const hasher = new DataHasher();
const stream = createReadStream("large_file.bin");
for await (const chunk of stream) {
hasher.update(chunk);
}
const iscc = hasher.finalize();
console.log(iscc); // Identical to gen_data_code_v0(entireFile)
InstanceHasher¶
import {
createReadStream
} from "node:fs";
import {
InstanceHasher
} from "@iscc/lib";
const hasher = new InstanceHasher();
const stream = createReadStream("large_file.bin");
for await (const chunk of stream) {
hasher.update(chunk);
}
const iscc = hasher.finalize();
console.log(iscc); // Identical to gen_instance_code_v0(entireFile)
Both hashers accept Buffer input in update(). The first finalize() call consumes the hasher,
even when it fails (for example on an invalid bits value). After that, further calls throw an
error.
The optional bits parameter can be passed to finalize():
Text utilities¶
Text normalization functions are available for preprocessing:
import {
text_clean,
text_collapse,
text_remove_newlines,
text_trim
} from "@iscc/lib";
// Normalize text for display
const cleaned = text_clean(" Hello\r\n\r\n\r\nWorld ");
// Simplify text for similarity hashing
const collapsed = text_collapse("Hello, World!");
// Remove newlines, collapse whitespace
const singleLine = text_remove_newlines("Hello\nWorld");
// Trim to UTF-8 byte limit
const trimmed = text_trim("Hello World", 5);
Codec operations¶
Functions for encoding, decoding, and decomposing ISCC codes. These operate on the ISCC binary format defined in ISO 24138.
Encode and decode¶
Construct an ISCC unit from raw header fields and digest, then decode it back:
const {
encode_component,
iscc_decode
} = require("@iscc/lib");
// Encode: maintype=0 (Meta), subtype=0, version=0, 64 bits, 8-byte digest
const digest = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);
const code = encode_component(0, 0, 0, 64, digest);
console.log(code); // ISCC unit string (without "ISCC:" prefix)
// Decode: parse an ISCC unit string back into header components and digest
const result = iscc_decode(code);
console.log(`Maintype: ${result.maintype}, Subtype: ${result.subtype}`);
console.log(`Version: ${result.version}, Length: ${result.length}`);
console.log(`Digest: ${Buffer.from(result.digest).toString("hex")}`);
iscc_decode returns an IsccDecodeResult object with maintype, subtype, version, length
(length index), and digest (Buffer) fields.
ISCC-IDv1 (experimental)¶
Mint an ISCC-IDv1 from a microsecond UTC timestamp and a HUB-ID. The timestamp occupies the high 52
bits and the HUB-ID the low 12 bits; realm (0 = testnet, 1 = mainnet) becomes the SubType.
Experimental
ISCC-IDv1 is not part of ISO 24138 and may change in a minor release.
const {
gen_iscc_id_v1,
iscc_decode
} = require("@iscc/lib");
const iscc = gen_iscc_id_v1(1751831876325218, 1, 0);
console.log(iscc); // "ISCC:MAIGHFECJMOPMIAB"
// There is no dedicated decoder — recover the fields with iscc_decode and bit math:
const d = iscc_decode(iscc);
const n = Buffer.from(d.digest).readBigUInt64BE(0);
const timestamp = n >> 12n; // 1751831876325218n
const hubId = n & 0xfffn; // 1n
const realm = d.subtype; // 0
console.log(d.version === 1); // true (ISCC-IDv1)
Decompose¶
Split a composite ISCC-CODE into its individual unit codes:
const {
gen_data_code_v0,
gen_instance_code_v0,
gen_iscc_code_v0,
iscc_decompose
} = require("@iscc/lib");
const data = Buffer.from("Hello World".repeat(1000));
const dataCode = gen_data_code_v0(data);
const instanceCode = gen_instance_code_v0(data);
const isccCode = gen_iscc_code_v0([dataCode, instanceCode]);
// Decompose into individual units
const units = iscc_decompose(isccCode);
for (const unit of units) {
console.log(unit); // Each unit code (without "ISCC:" prefix)
}
ISCC-SEQ¶
encode_seq(units: Array<string>): Buffer packs an ordered list of ISCC-UNITs into an ISCC-SEQ byte
string (IEP-0020): the header and body bytes of each unit, in the given order.
decode_seq(data: Buffer): Array<string> reads one back into ISCC:-prefixed unit strings. Base32
input is strict (no padding, zero spare bits) and upper-casing is ASCII-only. ISCC-CODE, ISCC-ID and
ISCC-FLAKE units are rejected.
const {
decode_seq,
encode_seq
} = require("@iscc/lib");
const data = encode_seq(["ISCC:EAD2RASIYU5IKLENP2OFI4CHZGRWYQCSW2WKX3Y6FJGOCXSYNYGLGBI"]);
console.log(data.length); // 34
console.log(data.subarray(0, 6).toString("hex")); // "2007a88248c5"
const units = decode_seq(data);
console.log(units); // ["ISCC:EAD2RASIYU5IKLENP2OFI4CHZGRWYQCSW2WKX3Y6FJGOCXSYNYGLGBI"]
// The round trip is defined on bytes: encode_seq(decode_seq(data)) equals data
console.log(encode_seq(units).equals(data)); // true
Other codec functions¶
encode_base64(data)— encode a Buffer to base64 stringjson_to_data_url(json)— convert a JSON string to adata:application/json;base64,...URLsoft_hash_video_v0(frameSigs, bits?)— compute a video similarity hash from MPEG-7 frame signatures, returns Buffer
Algorithm primitives¶
Low-level algorithm functions are available for advanced use cases:
const {
sliding_window
} = require("@iscc/lib");
// Sliding window: overlapping substrings of width Unicode characters
const windows = sliding_window("Hello World", 4);
// ["Hell", "ello", "llo ", "lo W", "o Wo", " Wor", "Worl", "orld"]
Additional primitives:
alg_simhash(hashDigests)— compute a SimHash digest from an array of equal-length Buffers, returns Bufferalg_minhash_256(features)— compute a 256-bit MinHash digest from 32-bit integer features, returns Buffer; throws on an empty arrayalg_cdc_chunks(data, utf32, avgChunkSize?)— split data into content-defined chunks (default average chunk size 1024), returns an array of Buffers
Constants¶
Exported constants used by the ISCC algorithms:
const {
META_TRIM_NAME,
META_TRIM_DESCRIPTION,
META_TRIM_META,
IO_READ_SIZE,
TEXT_NGRAM_SIZE,
} = require("@iscc/lib");
META_TRIM_NAME; // 128 — max byte length for name normalization
META_TRIM_DESCRIPTION; // 4096 — max byte length for description normalization
META_TRIM_META; // 128_000 — max decoded payload size in bytes for the meta element
IO_READ_SIZE; // 4_194_304 — default read buffer size (4 MB)
TEXT_NGRAM_SIZE; // 13 — n-gram size for text similarity hashing
Conformance testing¶
Verify the library against official test vectors:
Error handling¶
Functions throw on invalid input. Every number argument must be an integer within the range of its
parameter (for example, bits and nbytes must lie in 0 to 2^32 − 1 and each gen_audio_code_v0
feature in the signed 32-bit range). A negative, fractional, non-finite or too large number throws
an error that names the parameter, for example Invalid bits: -1: