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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bjoernboss/mws-files

v1.1.0

Published

Module in TypeScript to share and manage files and directories - For Modular Web Server

Readme

[MWS] Module to Share Files and Directories

TypeScript License

A file sharing module for @bjoernboss/mws.

It serves the content of a data directory over HTTP: browsing directories through a full-featured web frontend, downloading files and entire directories as ZIP archives, and uploading, copying, moving, and deleting content, if permitted. A WebSocket endpoint allows API clients to listen for directory changes.

All content is stored as plain files and directories in the configured data directory and persists across server restarts. Path reservations and copy jobs are managed by the FileShare module.

Note: The module is not case-insensitivity aware. Meaning on file-systems, which do not consider case sensitivity, the uploading might overwrite files wrongfully.

Installation

$ npm install @bjoernboss/mws-files

Requires Node.js 22 or later.

Setup

The FileShare module takes a data directory path and an optional Params object controlling what operations clients may perform. Mount it under a path using dispatch:

import { Server, dispatch, addLogger, createConsoleLogger } from "@bjoernboss/mws";
import { FileShare } from "@bjoernboss/mws-files";

addLogger(createConsoleLogger());

const server = new Server();
const share = new FileShare('./data/share', {
    access: true,
    upload: true,
    delete: true
});

server.listen(dispatch({ '/share': share }), { port: 8080 });

The module serves its own pages, static assets, and WebSocket endpoints from its mount point. Navigate to http://localhost:8080/share/files/ to open the root directory view.

Important: The module caches path reservations and copy jobs in memory. The same data directory should therefore not be used by multiple FileShare modules simultaneously.

Frontend

The directory view is an elaborate single-page browser frontend, built entirely on the public API described below. Operations not permitted by the configured parameters are hidden from the UI. It supports Visually friendly navigations, managing, browsing, and progress stats and notifications for operations. The desktop version further offers various keyboard shortcuts and mouse interactions conventionally expected.

Note: For any client, all requests reaching the same rebase should have the same Params and Endpoints mappings. The frontend caches the endpoints and parameters, and will re-use these assumptions for any sub-path within its rebase confinement. Dynamic Params or Endpoints for one client, depending on the accessed directory, may result in unexpected or unintended assumptions and behavior of the frontend.

Parameters

The Params object controls module behavior and access. All fields are optional:

| Field | Default | Description | |---|---|---| | access | false | Access content at all - browsing, downloading, copy jobs, and change listeners all require it | | upload | false | Upload files, create directories, and copy or move content | | delete | false | Delete content (also required to move content) | | uploadMTime | false | Preserve the client-supplied modification time of uploads, otherwise reset to current time | | uploadLimit | 100000000 (100 MB) | Largest content to upload or copy in bytes (0 implies no limit) | | rebase | '/' | Sub-directory of the share to serve as the connection's root (must be a directory) |

Without any parameters the share is inaccessible; access: true alone yields a read-only share. Parameters can also be set per-request through params when dispatching to the module. Request parameters override the corresponding default, allowing parent modules to implement authentication or per-route access policies.

With rebase, all paths of the connection (served content, copy and move targets, and watched directories) are resolved relative to the given sub-directory, and nothing outside of it can be reached. The rebasing is fully transparent to clients, which makes per-request rebase suitable for handing each user their own root within a shared data directory.

Endpoints

The Endpoints export provides the path constants used by the module. All paths are relative to the module's mount point. Path components in the URL use URI encoding while preserving /; paths in JSON payloads are not encoded.

| Path | Method | Description | |---|---|---| | /files/{path} | GET | Serve a file, or a directory as HTML view, JSON listing, or ZIP download | | /files/{path} | POST | Upload a file, create a directory, or reserve a path (requires Params.upload) | | /files/{path} | PUT | Copy or move content to a new path (requires Params.upload; move also Params.delete) | | /files/{path} | DELETE | Delete a file or an empty directory (requires Params.delete) | | /jobs/{id} | GET | JSON status of a copy job | | /static/* | GET | Static assets (CSS, JS, icons) served with immutable cache headers | | /ws/{path} | WebSocket | Listen for changes of a directory |

All endpoints except /static additionally require Params.access; without it they respond with 403.

Path components may not contain control characters or any of / \ ? : * " < > |. Entries in the data directory whose names contain such characters (created externally) are excluded from all listings, downloads, and change notifications. The (possibly rebased) root directory itself can only be read, never modified, and cannot be the direct target of a copy or move, but can be copied/moved into.

Files API

Every response of the /files endpoint carries a Kind header (file or directory) identifying what was served, and a Path header echoing the served path in the shared file space (URI-encoded like paths in the URL). The optional query parameter kind=file|directory restricts the request to the given kind; a mismatch results in 409. For requests without kind, a file is preferred over a directory of the same path.

Reading (GET)

  • A file is served directly (range requests and content encoding are handled by the framework); download=true adds a Content-Disposition: attachment header (the filename is given as filename*, with an alternative ascii filename fallback).
  • A directory is served as the interactive HTML browser view by default. With download=true the directory is streamed as a ZIP archive ({name}.zip), and with raw=true the JSON listing is returned instead.

The JSON listing maps entry names to their metadata:

{ "example.txt": { "kind": "file", "size": 1234, "modified": 1710000000000 } }

For directories, size is the number of contained entries; modified is the modification time in milliseconds since the epoch. ZIP downloads are streamed using ZIP64 extensions; compressible media types are deflated, all other content is stored uncompressed.

Uploading (POST)

  • kind=file (default): the request body becomes the file content; fails with 409 if the path already exists. The body size is limited by Params.uploadLimit (413 if exceeded).
  • kind=directory: creates an empty directory; with silent=true, an already existing directory responds OK instead of 409 (this also applies to reserve=true requests, which then respond without a Reservation-Id header).
  • mtime={ms}: sets the modification time of the created content (only honored when Params.uploadMTime is enabled).
  • reserve=true: instead of uploading, reserves the path for 5 seconds and responds with a Reservation-Id header. While a reservation is active, only requests passing the id back via reservation={id} may claim the path. This allows clients to atomically pick a free name before starting a large upload.

In all cases the parent directory of the path must already exist; intermediate directories are never created implicitly.

Copying and Moving (PUT)

Exactly one of copy={target} or move={target} must be given, where the target is the full destination path within the share (given decoded in the query string). The destination must not exist yet and its parent directory must exist; reservation={id} may pass a previously created reservation for the destination.

  • move: renames the file or directory (kind selects the expected source kind). Requires Params.upload and Params.delete.
  • copy: only files can be copied, and Params.uploadLimit is enforced on the source size (413 if exceeded). The copy runs as a background job: the response carries a Job-Id header, and progress can be polled via /jobs/{id}. Requires Params.upload.

Deleting (DELETE)

Deletes the file or empty directory at the path (kind selects the expected kind). Deleting a non-empty directory responds with 409.

Copy Jobs

A copy job created by PUT copy can be polled via GET /jobs/{id}:

{ "progress": 0.5, "state": "running", "message": "" }

state is one of running, success, or failure; message describes failures and progress ranges from 0 to 1. Finished job states are retained for 3 minutes before being cleaned up. Running jobs are aborted when the server shuts down, in which case the partially written target is removed.

WebSocket Protocol

Clients connect to /ws/{path} to listen for changes of a directory (only directories can be watched). Whenever the directory content changes, the server broadcasts the full JSON directory listing (same format as raw=true). Notifications are coalesced over a timespan of a few seconds. Changes within immediate sub-directories, which alter the listed entry count and modification time, are tracked as well. Clients never need to send data, as this is only a notification channel.

Besides listings, the server sends one of three string identifiers:

  • "removed": the watched directory was deleted.
  • "error": watching the directory failed.
  • "close": the server is shutting down.

After an identifier is sent, the server closes the WebSocket. The underlying file-system watcher is kept alive for a certain grace period after the last listener disconnects, to handle quick reconnections.