@acte-de-presence/vfs 中文文档教程

发布于 3年前 浏览 9 项目主页 更新于 3年前

Virtual Filesystem

这个库是 Acte de Présence 框架使用的虚拟文件系统

Usage

import { VFS, AccessModes } from '@acte-de-presence/vfs';
import { VFSMemoryDriver } from '@acte-de-presence/vfs-memory-driver';

async function test() {
  const vfs = new VFS();
  vfs.use(new VFSMemoryDriver());

  const filename = "/test.txt";

  try {
    // Check if file exists
    await vfs.exists(filename) 

    // Check if file exists
    await vfs.access(filename, AccessModes.F_OK);

    // Check if file can be read
    await vfs.access(filename, AccessModes.R_OK);

    // Check if file can be written to
    await vfs.access(filename, AccessModes.W_OK);

    // Read the file
    await vfs.read(filename);

    // Write to an existing or new file
    await vfs.write(filename, Buffer.from("DATA"));

    // Delete a file
    await vfs.delete(filename);
  } catch (err) {
    console.error(err);
  }
}

Driver specification

驱动程序非常简单,应该只实现以下接口

interface VfsDriver {
  pathPrefix: string;
  exists: (path: string) => Promise<boolean>;
  access: (path: string, mode: AccessModes) => Promise<boolean>;
  write: (path: string, data: Buffer) => Promise<void>;
  read: (path: string) => Promise<Buffer>;
  delete: (path: string) => Promise<void>;
}

Running unit tests

运行 nx test api-vfs 以通过 开玩笑

Virtual Filesystem

This library is the Virtual filesystem used by the Acte de Présence framework

Usage

import { VFS, AccessModes } from '@acte-de-presence/vfs';
import { VFSMemoryDriver } from '@acte-de-presence/vfs-memory-driver';

async function test() {
  const vfs = new VFS();
  vfs.use(new VFSMemoryDriver());

  const filename = "/test.txt";

  try {
    // Check if file exists
    await vfs.exists(filename) 

    // Check if file exists
    await vfs.access(filename, AccessModes.F_OK);

    // Check if file can be read
    await vfs.access(filename, AccessModes.R_OK);

    // Check if file can be written to
    await vfs.access(filename, AccessModes.W_OK);

    // Read the file
    await vfs.read(filename);

    // Write to an existing or new file
    await vfs.write(filename, Buffer.from("DATA"));

    // Delete a file
    await vfs.delete(filename);
  } catch (err) {
    console.error(err);
  }
}

Driver specification

The drivers are meant to be extremely simple, and should only implement the following interface

interface VfsDriver {
  pathPrefix: string;
  exists: (path: string) => Promise<boolean>;
  access: (path: string, mode: AccessModes) => Promise<boolean>;
  write: (path: string, data: Buffer) => Promise<void>;
  read: (path: string) => Promise<Buffer>;
  delete: (path: string) => Promise<void>;
}

Running unit tests

Run nx test api-vfs to execute the unit tests via Jest.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文