npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-atomic-file-ops

v0.1.2

Published

Write files atomically on React Native

Downloads

5

Readme

react-native-atomic-file-ops

Atomically writing to a file on mobile avoids potential file corruption. When writing atomically, the data is first written to a separate auxiliary file. Once writing is successful, the auxiliary file is renamed to the path specified by the application. When writing is not done atomically, the data is written directly to the path. Writing atomically guarantees that the path and its data will not be corrupted even if a crash occurs during writing.

In our React Native app, we encountered a bug with Android 10 in which writing to an existing file corrupted that file. If the existing data was longer than the new data, rather than completely overwriting the existing file, the app wrote the new, truncated data while keeping any remaining original data, leading to data corruption. For example, if the original file contained the string “bootstrapping” and the new data to be written is the string “byte”, the file would be written as “bytestrapping” rather than “byte”. This behavior was caused by writing data directly to the path without an atomic “safety check.”

No open source file systems libraries available for React Native supported writing to files atomically. We built react-native-atomic-file-ops in order to prevent corruption when writing to files. By writing atomically to an auxiliary file first and making sure that file has been written successfully, react-native-atomic-file-ops protects the file path and its data.

Table of Contents

Installation

While this library was initially built to fix a bug occurring on a specific version of Android, it supports both Android and iOS.

npm install react-native-atomic-file-ops

If using yarn:

yarn add react-native-atomic-file-ops

For iOS:

cd ios
pod install

How to Use

import AtomicFileOps from 'react-native-atomic-file-ops';

// ...

// EXAMPLE:  Writes to a JSON file
const fileName = 'CavyImageMetadata.json';

const imageMetadata = [
  {
    title: 'Sasu the Guinea Pig',
    alt: 'Guinea pig in green grass with dandelions',
    creator: 'andymiccone',
    url: 'https://live.staticflickr.com/7377/26722155994_5200abc340_b.jpg',
    license: 'CC0 1.0',
  },
];

const unicode = 'UTF8';

await AtomicFileOps.writeFile(fileName, JSON.stringify(imageMetadata), unicode);

API

writeFile(fileName: String, contents: String, encoding: String)

Writes the contents atomically to the given file, fileName.

encoding allows for the encoded character sets "utf8", "ascii", and "base64" (accepts both uppercase and lowercase strings).

Example App

An example React Native app demonstrates how react-native-atomic-file-ops can be used to overwrite files. See the example app’s documentation for information on running the app and its tests.

Tests

The writeFile API has been tested on both Android and iOS. Scenarios tested include:

  • Writing JSON to a file
  • Overwriting an existing file with truncated data
  • Providing writeFile with a corrupted character set
  • Providing writeFile with a corrupted file path

Additionally, the library has been tested on React Native in an example app using the Cavy test framework. See the example app’s documentation for information on running Cavy tests.

Contributing

react-native-atomic-file-ops is open-source. If you find a bug or have an idea for improving this project, feel free to contribute!

Please read our Contribution Guidelines to get started.

Credits

License

MIT