@flatfile/util-file-buffer

中文文档
0.6.0发布于 2024/11/08ISCTypeScriptESM + CJS零依赖近 30 天下载 7.2万

A utility for extracting data from any file and making it available as a buffer

原始使用文档
文档目录9 节
  1. Code Breakdown
  2. Import Statements
  3. fileBuffer Function
  4. Event Listener
  5. Matching File
  6. Buffering File Contents
  7. Callback Execution
  8. Helper Function
  9. Example Usage

The @flatfile/util-file-buffer utility provides a convenient way to process files and extract their contents using the Flatfile API. It allows you to match specific files based on file extensions or regular expressions and retrieve their contents as a buffer for further processing.

When embedding Flatfile, this plugin should be deployed in a server-side listener. Learn more

Code Breakdown

This code defines the fileBuffer utility used in Flatfile Extractor plugins for handling file processing. The utility enables you to intercept file creation events and retrieve file contents as a buffer for subsequent operations. Let's break down the main components of the code:

Import Statements

  • The code starts by importing necessary dependencies and modules required for file buffering. These include FlatfileEvent and FlatfileListener from @flatfile/listener, and api and Flatfile from @flatfile/api.

fileBuffer Function

  • The fileBuffer function is the core of the utility and serves as a middleware function for intercepting file creation events.
  • It takes two parameters:
    • matchFile: A string or regular expression representing the file extension(s) to match for processing.
    • callback: A callback function that receives the file metadata, file buffer, and the Flatfile event.

Event Listener

  • Inside the fileBuffer function, the provided listener is configured to listen for file:created events. When a file is created, this event will be triggered.

Matching File

  • When a file:created event occurs, the utility fetches the corresponding file's metadata using the Flatfile API and checks if the file name matches the specified matchFile.
  • If matchFile is a string, the utility checks if the file name ends with the specified extension.
  • If matchFile is a regular expression, the utility checks if the file name matches the regular expression.

Buffering File Contents

  • If the file matches the specified matchFile, the utility proceeds to retrieve the file's contents as a buffer using the getFileBuffer function.
  • The getFileBuffer function downloads the file using the Flatfile API and reads its contents into a buffer.

Callback Execution

  • After obtaining the file buffer, the utility invokes the provided callback function with the file metadata, buffer, and Flatfile event as parameters.

Helper Function

  • The code includes a helper function named getFileBuffer responsible for downloading the file and buffering its contents.

Example Usage

import { fileBuffer } from "@flatfile/util-file-buffer";
import api, { Flatfile } from "@flatfile/api";

// Define the callback function to process the file buffer
const processFileBuffer = (
  file: Flatfile.File_,
  buffer: Buffer,
  event: FlatfileEvent
) => {
  // Your file processing logic here...
};

// Register the fileBuffer middleware to intercept file:created events
const fileBufferMiddleware = fileBuffer(".csv", processFileBuffer);
listener.use(fileBufferMiddleware);

In this example, the fileBuffer middleware is used to intercept .csv files and pass their contents as buffers to the processFileBuffer function for further processing.

版本与发布记录

@flatfile/util-file-buffer 共发布 20 个版本,首次发布于 2023/08/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.6.0latest2024/11/082 年前
0.5.02024/10/302 年前
0.4.12024/10/032 年前
0.4.02024/09/242 年前
0.3.32024/09/042 年前
0.3.22024/05/212 年前

关于 @flatfile/util-file-buffer 的常见问题

@flatfile/util-file-buffer 是什么?

A utility for extracting data from any file and making it available as a buffer

如何安装 @flatfile/util-file-buffer?

在项目目录运行 npm install @flatfile/util-file-buffer,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@flatfile/api(^1.9.19)、@flatfile/listener(^1.1.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flatfile/util-file-buffer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flatfile/util-file-buffer 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@flatfile/util-file-buffer 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flatfile/util-file-buffer 可以商用吗?

@flatfile/util-file-buffer 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flatfile/util-file-buffer 的下载量有多少?

根据 npm 官方下载统计,@flatfile/util-file-buffer 在 2026/10/15 – 2026/11/13 这 30 天内被下载 72,318 次,日均约 2,411 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flatfile/util-file-buffer 还在维护吗?

最新版本 0.6.0 发布于 2024/11/08(2 年前);至今共发布 20 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/08/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。