G-code Preview — API Reference
    Preparing search index...

    Module @chestnutlabs/gcode-parser

    @chestnutlabs/gcode-parser

    Worker-safe G-code parse core and session client for the Chestnut Labs G-code Preview stack (design: DD-003). Parses .gcode / .gcode.3mf off the main thread into ToolpathIR, with streaming input, progressive partial previews for large files, resource limits, cancellation, and an adversarial-input corpus behind it.

    import { GcodeParseSession } from '@chestnutlabs/gcode-parser';

    const session = new GcodeParseSession(); // batteries worker: all dialects + .gcode.3mf
    const result = await session.parse(bytes, {
    onPartial: (ir) => render(ir), // progressive preview
    onProgress: (p) => bar(p)
    });
    render(result.ir);
    session.dispose();

    Bytes are handed to the worker, not copied. A Uint8Array passed to parse() has its buffer transferred to the worker, so it is empty (byteLength === 0) afterwards. To parse the same file again, for example to switch plates with { plate: 1 }, keep a copy and pass bytes.slice() each time. Passing the already-transferred bytes rejects with E_POST_FAILED; the session stays usable.

    • Batteries default (zero setup): the session creates its own worker via the bundler-native new Worker(new URL('./worker.js', import.meta.url)) pattern — all supported dialect adapters plus .gcode.3mf container support. Vite resolves it out of the box (CI-tested).
    • Slim / custom: pass a worker factory for the slim build (no dialects/containers), your own adapter set via createWorkerHandler(...), other bundlers, or strict-CSP environments.

    Wire options accept dialect selection, container/plate selection (plate for multi-plate .gcode.3mf), and resource limits. Most applications should consume this through @chestnutlabs/gcode-preview-core or a framework adapter rather than directly.

    Part of Chestnut Labs G-code Preview · MIT

    BudgetExceededError
    CancelledError
    GcodeParseSession
    ParseSessionError
    AsyncParseHooks
    AsyncParseResult
    BlobLike
    CommandEvent
    DialectRunLike
    DialectRunnerFactoryLike
    ParseLimits
    ParseOptions
    ParseProgress
    ParseResult
    ParseStats
    PartialMessage
    ReadableStreamLike
    SessionOptions
    StopReason
    WireParseOptions
    WorkerHandlerOptions
    WorkerLike
    PostFn
    StreamInput
    WorkerRequest
    WorkerResponse
    DEFAULT_LIMITS
    PARTIAL_PREVIEW_MIN_BYTES
    PROTOCOL_VERSION
    createWorkerHandler
    irTransferList
    isBlobLike
    isReadableStreamLike
    parseGcodeStreamToIR
    parseGcodeToIR
    parseGcodeToIRAsync