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

file-read-events

v0.2.1

Published

Watch for detailed read events on the given file

Downloads

14

Readme

file-read-events

Watch for read events on the given file and byte

Purpose

In certain scenarios, it becomes essential to track the bytes read from a file by the operating system's file system. This module aims to provide a solution by providing a mechanism to receive events whenever a read operation occurs on a specified file, including details such as the number of bytes read and the read offset.

A major use case is end-to-end and integration tests. You can check if a specific part of the file is read (or is not read), so you can assert that your code is working as intended in terms of correct file access.

Installation

npm install file-read-events

Basic Usage

import { FileReadEvents } from "../src/FileReadEvents";

const fileEvents = new FileReadEvents('C:/temp/target-file.txt');

fileEvents.on('read', ({ offset, ioSize, path }) => {
    console.log(offset, ioSize, path); 
    // 0 2048 C:/temp/target-file.txt
});

// start watching
fileEvents.start(); 

Close the watcher process at the cleanup.

fileEvents.end();

API

With specifying target byte.

const fileEvents = new FileReadEvents('C:/temp/target-file.txt', 1234);
fileEvents.on('read', ({ offset, ioSize, path, isTargetByte }) => {
    console.log(offset, ioSize, path, isTargetByte); 
});
fileEvents.start();

Methods

Method | Description --- | --- constructor | The first parameter is the path to the file to watch. The second parameter (optional) is the target byte (zero-indexed) start | Start the watcher process end | End the watcher process on, off | FileReadEvents extends EventEmitter, so you can use on and off to manage event handlers.

Events

Event name | Description --- | --- ready | The process is launched and ready to watch error | Encountered an error read | A read event was observed on the target file exit | The process was killed

read Event properties

The handlers you add for the read event are called with an event object as their only argument. This object has the following properties:

Property | Description --- | --- offset | Number of bytes skipped from the beginning of the file ioSize | Number of bytes that has been read by the file system path | Absolute path to the file being read isTargetByte | Is the specified target byte read in this event

Example

Without specifying target byte.

const fileEvents = new FileReadEvents('C:/temp/target-file.txt');
fileEvents.on('read', ({ offset, ioSize, path }) => {
    console.log(offset, ioSize, path); 
});
fileEvents.start();

Compatibility

This is currently working on Windows, contributions are welcome if you can help with adding support for other operating systems. Read "How does it work" section below for more details on this.

How does it work

This package essentially serves as a wrapper around WinFileReadEvents, a Windows CLI utility written in C#. It utilizes Event Tracing for Windows (ETW) to capture these events from the operating system. The precompiled standalone binary is downloaded to the bin directory during the postinstall script of this package.

IMPORTANT: Due to Windows restrictions, the executable must be run as an administrator. Please ensure that you run the code in a terminal with administrator privileges; otherwise, it will emit an error event and will not work.