New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@zenfs/port

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@zenfs/port

Port backend for ZenFS

unpublished
latest
Source
npmnpm
Version
0.1.4
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

ZenFS Port Backend

ZenFS backend for usage with ports and workers.

Please read the ZenFS documentation!

For more information, see the docs.

Installing

npm install @zenfs/port

Usage

[!NOTE] The examples are written in ESM. If you are using CJS, you can require the package. If in a browser you can add a script tag to your HTML pointing to the browser.min.js and use ZenFS Port via the global ZenFS_Port object.

Accessing an FS on a remote Worker from the main thread

Main:

import { configure } from '@zenfs/core';
import { Port } from '@zenfs/port';
import { Worker } from 'node:worker_threads';

const worker = new Worker('worker.js');

await configure({
	'/worker': {
		backend: Port,
		port: worker,
	},
});

Worker:

import { InMemory, resolveMountConfig } from '@zenfs/core';
import { attachFS } from '@zenfs/port';
import { parentPort } from 'node:worker_threads';

const tmpfs = await resolveMountConfig({ backend: InMemory, name: 'tmp' });
attachFS(parentPort, tmpfs);

If you are using using web workers, you would use self instead of importing parentPort in the worker, and would not need to import Worker in the main thread.

Using with multiple ports on the same thread

import { InMemory, fs, resolveMountConfig } from '@zenfs/core';
import { Port, attachFS } from '@zenfs/port';
import { MessageChannel } from 'node:worker_threads';

const { port1, port2 } = new MessageChannel();

const tmpfs = await resolveMountConfig({ backend: InMemory, name: 'tmp' });
attachFS(port2, tmpfs);
fs.mount('/port', await resolveMountConfig({ backend: Port, port: port1 }));
console.log('/port');

const content = 'FS is in a port';

await fs.promises.writeFile('/port/test', content);

fs.readFileSync('/tmp/test', 'utf8'); // FS is in a port
await fs.promises.readFile('/port/test', 'utf8'); // FS is in a port

Keywords

filesystem

FAQs

Package last updated on 06 May 2024

Related posts