Prototxt Parser

0.1.5 · maintenance · verified Tue Apr 21

prototxt-parser is a JavaScript and TypeScript library designed to parse `.prototxt` files, commonly used for defining neural network architectures in frameworks like Caffe, into standard JavaScript objects. It is built upon the `parsimmon` parser combinator library, which provides a flexible and robust foundation for defining custom grammars. The current stable version is 0.1.5. Given its low version number and specialized use case, its release cadence is likely slow and driven by specific needs rather than a regular schedule. Key differentiators include its explicit focus on `.prototxt` syntax, providing a direct mapping to JavaScript objects, and shipping with TypeScript type definitions for enhanced developer experience. It offers a straightforward API for both browser and Node.js environments, allowing developers to integrate `.prototxt` parsing into various JavaScript-based applications. While it doesn't aim to be a general-purpose configuration parser, its dedicated approach makes it suitable for working with Caffe-related model definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to asynchronously fetch a `.prototxt` file from a URL and then parse its content into a JavaScript object using `prototxt-parser`. It includes error handling and logs the structured output.

import * as prototxtParser from 'prototxt-parser';

async function fetchPrototxt(uri: string): Promise<string> {
  try {
    const response = await fetch(new Request(uri));
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    return await response.text();
  } catch (error) {
    console.error(`Failed to fetch prototxt from ${uri}:`, error);
    throw error;
  }
}

// Example .prototxt for a SqueezeNet model
const uri = "https://raw.githubusercontent.com/DeepScale/SqueezeNet/master/SqueezeNet_v1.1/deploy.prototxt";

(async () => {
  try {
    const prototxtString = await fetchPrototxt(uri);
    const parsedProto = prototxtParser.parse(prototxtString);
    console.log('Successfully parsed .prototxt:');
    console.log(JSON.stringify(parsedProto, null, 2));
    // You can now access properties, e.g., console.log(parsedProto.name);
  } catch (error) {
    console.error('An error occurred during parsing:', error);
  }
})();

view raw JSON →