⚡️ Fast MagicString port driven by Rust and N-API

Overview

magic-string-rs

CI crates NPM version

100% API compatible (port) MagicString by Rich-Harris implementation for Node and modern browsers, also, for rust, of course.


Installation

Rust

Add it as a dependency in a Cargo project.

# Cargo.toml

[dependency]
magic_string = "x.x.x"

Node

$ npm install @napi-rs/magic-string

Benchmark

Running "toString" suite...
Progress: 100%

  MagicString:
    226 614 ops/s, ±0.08%   | slowest, 42.65% slower

  MagicStringRust:
    395 166 ops/s, ±1.83%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString
  
Running "generateDecodedMap" suite...
Progress: 100%

  MagicString:
    173 590 ops/s, ±0.55%   | slowest, 18.84% slower

  MagicStringRust:
    213 893 ops/s, ±0.75%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString
  
Running "generateMap.toString" suite...
Progress: 100%

  MagicString:
    141 658 ops/s, ±0.53%   | slowest, 31.48% slower

  MagicStringRust:
    206 744 ops/s, ±0.64%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Supported APIs

  • generateMap
  • generateDecodedMap
  • toString
  • prepend
  • append
  • prependLeft
  • prependRight
  • appendLeft
  • appendRight
  • overwrite
  • move
  • locateOrigin
  • locate
  • insertLeft
  • insertRight
  • indent
  • addSourcemapLocation
  • clone
  • remove
  • slice
  • snip
  • trim
  • trimStart
  • trimEnd
  • trimLines
  • isEmpty

Credits

The original project magic-string is really awesome, you should check it out and we made this project even furthur for better performance.

License

MIT

Comments
  • Error: ENOENT: no such file or directory, open '/usr/bin/ldd'

    Error: ENOENT: no such file or directory, open '/usr/bin/ldd'

    this is on nixos linux, where there is no /usr/bin/ldd

    the only FHS binaries in nixos are /usr/bin/env and /bin/sh

    both files are symlinks

    stat /usr/bin/env /bin/sh  | grep File
    
      File: /usr/bin/env -> /nix/store/xq4g38m5ppg78z7bzfxyy2s8ii693k74-coreutils-9.1/bin/env
      File: /bin/sh -> /nix/store/rcr0k2c7yl708k264k2jb9f6zdzglbz7-bash-interactive-5.1-p16/bin/sh
    
    opened by milahu 0
  • Missing `Bundle` type

    Missing `Bundle` type

    I tried to convert rollup to use magic-string-rs, but got the error:

    Module '"@napi-rs/magic-string"' has no exported member 'Bundle'

    Here's the type as provided by magic-string:

    export class Bundle {
      constructor(options?: BundleOptions);
      addSource(source: MagicString | { filename?: string, content: MagicString }): Bundle;
      append(str: string, options?: BundleOptions): Bundle;
      clone(): Bundle;
      generateMap(options?: SourceMapOptions): SourceMap;
      generateDecodedMap(options?: SourceMapOptions): DecodedSourceMap;
      getIndentString(): string;
      indent(indentStr?: string): Bundle;
      indentExclusionRanges: ExclusionRange | Array<ExclusionRange>;
      prepend(str: string): Bundle;
      toString(): string;
      trimLines(): Bundle;
      trim(charType?: string): Bundle;
      trimStart(charType?: string): Bundle;
      trimEnd(charType?: string): Bundle;
      isEmpty(): boolean;
      length(): number;
    }
    
    opened by benmccann 1
  • `generateMap` doc clarification

    `generateMap` doc clarification

    The readme says:

    generateMap: Note that there is a huge overhead for rust for implementing the same API in Node, for more detail please refer to this

    If I follow the "this" link, I'm just taken to a type definition file. I'm not sure how it explains what overhead might exist. Is that the correct link or perhaps I'm missing something?

    opened by benmccann 1
  • support for `.update`

    support for `.update`

    I recently added this to magic-string as a safer alternative to overwrite. It's now heavily used in Vite and a couple other libraries

    https://github.com/Rich-Harris/magic-string#supdate-start-end-content-options-

    opened by benmccann 0
  • support for `.hasChanged`

    support for `.hasChanged`

    I'm not sure if it's needed in this library, but .hasChanged in magic-string allows for an early return (skipping returning code and sourcemap) if nothing has changed, which is a significant performance optimisation in the vite pipelin.

    see https://github.com/Rich-Harris/magic-string/pull/202

    opened by danielroe 0
  • support for `.replace` and `.replaceAll`

    support for `.replace` and `.replaceAll`

    These are two lovely utilities from magic-string.

    • https://github.com/Rich-Harris/magic-string/pull/203
    • https://github.com/Rich-Harris/magic-string/pull/222
    opened by danielroe 0
Releases(v0.3.4)
Owner
Hana
What I cannot create, I do not understand.
Hana
This is a test to port C++'s ymfm and Rust's vgmplay to ESP32(Xtensa).

m5stack-chipstream This is a test to port C++'s ymfm and Rust's vgmplay to ESP32(Xtensa). Still slow and impractical, depending on the sound chip you

hiromasa 4 Feb 26, 2023
Port of the fantastic Iconoir Icon Pack to Rust embedded devices, with a focus on speed, usability, and completeness.

embedded-iconoir - Icons for every device, ever. What is embedded-iconor? embedded-iconoir is a library that allows you to use Iconoir on embedded dev

null 9 Mar 25, 2023
Repo for Monaco, a DCA engine for Solana. Built on Solend and lending protocols (Jet, Solend, Port, etc...)

Monaco Monaco is a DCA protocol for solana built on top of Serum and compatible with any program that implements or extends the instruction interface

Rohan Kapur 19 Apr 13, 2022
A cross-platform serial port library in Rust.

Introduction serialport-rs is a general-purpose cross-platform serial port library for Rust. It provides a blocking I/O interface and port enumeration

Bryant Mairs 143 Nov 5, 2021
A Rust port of parinfer.

parinfer-rust Infer parentheses for Clojure, Lisp and Scheme. https://github.com/eraserhd/parinfer-rust A full-featured, super fast implementation of

Jason Felice 432 Dec 31, 2022
Rust port of the official Windows Driver Samples on Github. Leverages windows-drivers-rs

Rust Driver Samples This is a Rust port of the driver samples from the original Windows Driver Samples on Github. The repository provides examples and

Microsoft 80 Oct 10, 2023
A port of OxidGB to the Raspberry Pi Pico

OxidGB Pico Port This is a simple, display-only (for now) port of OxidGB to the Raspberry Pi Pico. Note that this by default overclocks your device -

James 57 Dec 7, 2022
Quickly mint NFT(able)s port forwards remotely

Easy expose A really simple way to expose some service behind a NAT, similar to rathole and frp. WARNING: This does not secure the channel, or even do

Ben Simms 3 Dec 3, 2022
📦 Port of tauri-bundler

?? Port of tauri-bundler You can now easily create installers for your Deno apps, thanks to the amazing work of Tauri ??

Marc Espín 28 Dec 7, 2022
Log defmt messages over the serial port.

defmt-serial A defmt target for logging over a serial port. Messages can e.g. be read using socat and passed through defmt-print, see example-artemis

Gaute Hope 10 Oct 5, 2022
KFtray - A tray application that manages port forwarding in Kubernetes.

Ktray is written in Rust and React, with Tauri framework. The app simplifies the process of starting and stopping multiple port forwarding configurations through a user-friendly interface.

Henrique Cavarsan 42 Dec 17, 2023
Examples of how to use Rust with Serverless Framework, Lambda, API Gateway v1 and v2, SQS, GraphQL, etc

Rust Serverless Examples All examples live in their own directories: project: there is nothing here, just a simple cargo new project_name with a custo

Fernando Daciuk 9 Dec 17, 2022
Rust wrapper for the Google Places API. Access their hundreds of millions of places, reviews, and ratings.

Google Places API Working Examples cargo run --example nearby_search cargo run --example place_details cargo run --example find_place cargo run --exam

Jared Ucherek 4 Jun 12, 2023
Fusion is a cross-platform App Dev ToolKit build on Rust . Fusion lets you create Beautiful and Fast apps for mobile and desktop platform.

Fusion is a cross-platform App Dev ToolKit build on Rust . Fusion lets you create Beautiful and Fast apps for mobile and desktop platform.

Fusion 1 Oct 19, 2021
This article is about the unsound api which I found in owning_ref. Owning_ref is a library that has 11 million all-time downloads and 60 reverse dependencies.

Unsoundness in owning_ref This article is about the unsound api which I found in owning_ref. Owning_ref is a library that has 11 million all-time down

Noam Ta Shma 20 Aug 3, 2022
A simple omegle API written in Rust

omegalul-rs omegalul-rs is a work-in-progress opensource library for building Omegle clients. Features Current Features Fetching random server from om

NV6 5 Jun 21, 2022
High level rust abstractions for the libretro API

libretro-rs Design Philosophy The approach to this crate can best be summarized as wanting to expose all functionality, even if not idiomatically. The

null 9 Dec 25, 2022
Rust library for interacting with the VTube Studio API

vtubestudio-rs A library for interacting with the VTube Studio API. Basic usage This example creates a Client using the provided builder, which: conne

null 6 Dec 4, 2022
ncnn Rust API.

rust-ncnn ncnn Rust API. Prequisition Rust Env $ curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh CMake >= 3.12 Rust cmake needs --paral

null 11 Dec 27, 2022