util-file-buffer

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


plugin-autocast

A plugin for automatically casting values in Flatfile.

plugin-automap

A plugin to provide automapping imported files for headless workflows.

plugin-constraints

A plugin for extending blueprint with external constraints

plugin-convert-currency

A Flatfile plugin for currency conversion using Open Exchange Rates API

plugin-convert-json-schema

A plugin for converting JSON Schema to Flatfile Blueprint and configuring a...

plugin-convert-openapi-schema

A plugin for converting OpenAPI schema to Flatfile Blueprint.

plugin-convert-sql-ddl

A plugin for converting SQL DDL into Flatfile Blueprint.

plugin-convert-translate

A Flatfile Listener plugin for field translation using the Google Translate...

plugin-convert-what3words

A Flatfile plugin for converting What3Words addresses to standard addresses...

plugin-convert-yaml-schema

A plugin for converting YAML Schema definitions to Flatfile Blueprint.

plugin-dedupe

Dedupe records in a sheet via a sheet level custom action.

plugin-delimiter-extractor

A plugin for parsing .delimiter files in Flatfile.

plugin-dxp-configure

A plugin for using DXP class-based configurations.

plugin-enrich-geocode

A Flatfile plugin for geocoding addresses using the Google Maps Geocoding A...

plugin-enrich-gpx

A Flatfile plugin for parsing GPX files and extracting relevant data

plugin-enrich-sentiment

A Flatfile plugin for sentiment analysis of text fields in records

plugin-enrich-summarize

A Flatfile plugin for text summarization and key phrase extraction

plugin-export-delimited-zip

A Flatfile plugin for exporting Workbooks to delimited files and zipping th...

plugin-export-pivot-table

A Flatfile plugin for generating pivot tables from sheet data and saving as...

plugin-export-workbook

A plugin for exporting data in Flatfile to Workbooks.

plugin-extract-html-table

A Flatfile plugin for extracting table data from HTML files

plugin-extract-markdown

A plugin for parsing markdown files in Flatfile.

plugin-import-faker

A Flatfile plugin that generates example records using Faker

plugin-import-llm-records

A Flatfile plugin that generates example records using AI

plugin-import-rss

A Flatfile plugin for importing RSS feed data

plugin-job-handler

A plugin for handling Flatfile Jobs.

plugin-json-extractor

A plugin for parsing json files in Flatfile.

plugin-markdown-extractor

A plugin for parsing markdown files in Flatfile.

plugin-pdf-extractor

A plugin for parsing PDF files in Flatfile.

plugin-record-hook

A plugin for running custom logic on individual data records in Flatfile.

plugin-rollout

A plugin for automatically rolling out new changes to workbooks in flatfile...

plugin-space-configure

A plugin for configuring a Flatfile Space.

plugin-space-configure-from-template

A plugin for configuring a Flatfile Space from a Space Template.

plugin-stored-constraints

A plugin for running stored constraints

plugin-validate-boolean

A Flatfile plugin for boolean validation with multi-language support

plugin-validate-date

A Flatfile plugin for normalizing date formats

plugin-validate-email

A Flatfile Listener plugin for email validation

plugin-validate-isbn

A Flatfile Listener plugin for ISBN validation with configurable options. V...

plugin-validate-number

A Flatfile Listener plugin for number validation

plugin-validate-phone

A validator plugin for phone number formatting on individual data records i...

plugin-validate-string

A Flatfile plugin for string configuration and validation

plugin-view-mapped

A plugin for making the view post mapping show only mapped columns.

plugin-webhook-egress

A plugin for egressing data from a Flatfile Workbook to a webhook.

plugin-xlsx-extractor

A plugin for parsing xlsx files in Flatfile.

plugin-xml-extractor

A plugin for parsing .xml files in Flatfile.

plugin-zip-extractor

A plugin for unzipping zip files and uploading content back in Flatfile.

util-extractor

A library containing common utilities and helpers for extractors.

util-file-buffer

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

util-response-rejection

This plugin handles response rejections returned from an external source.

Installation


Installnpm i @flatfile/util-file-buffer
Source: View source
Package:@flatfile/util-file-buffer 4k installs

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.