🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

cloudflare-docx-parser

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cloudflare-docx-parser

A lightweight library to parse .docx files in Cloudflare Workers

1.0.6
latest
Source
npm
Version published
Weekly downloads
0
-100%
Maintainers
0
Weekly downloads
 
Created
Source

cloudflare-docx-parser

A lightweight library to parse Microsoft Word (.docx) files in Cloudflare Workers. This package is written in TypeScript and extracts plain text by unzipping .docx files (which are essentially ZIP archives) and parsing their XML content.

Table of Contents

  • Overview
  • Features
  • Installation
  • Usage
  • Bundling
  • TypeScript Support
  • Testing with Cloudflare Workers
  • Contributing
  • License

Overview

cloudflare-docx-parser is designed for Cloudflare Workers and other environments that do not have support for Node.js or browser-specific APIs. It leverages:

  • fflate for unzipping the .docx file.
  • fast-xml-parser for parsing XML content within the document.

This makes it an excellent choice for extracting text from docx files in edge environments or serverless functions.

Features

  • Cloudflare Compatibility: Works in Cloudflare Workers using only standard Web APIs.
  • TypeScript Support: Provides full TypeScript support and type declarations.
  • Simple API: One function, parseDocx, to extract plain text from a given .docx file.
  • Lightweight: Uses fast, lightweight libraries that run efficiently in edge environments.

Installation

Install the package with npm:

npm install cloudflare-docx-parser

Usage

Below is an example of how to use the package in your project. This example is especially suited for Cloudflare Workers.

Example Worker Code

import { parseDocx } from "cloudflare-docx-parser";

try {
  // Read the .docx file from the request body
  const fileBuffer = await request.arrayBuffer();
  // Parse the .docx and extract plain text
  const extractedText = await parseDocx(fileBuffer);

  return new Response(extractedText, {
    headers: { "Content-Type": "text/plain" },
  });
} catch (error: any) {
  return new Response(error.message, { status: 400 });
}

Bundling

For deployment in Cloudflare Workers, bundle the code using a tool such as esbuild.

Bundling Example using esbuild

Add a build script to your package.json:

"scripts": {
  "build": "esbuild src/index.ts --bundle --minify --platform=browser --format=esm --outfile=dist/index.js"
}

Then run:

npm run build

The output bundle will be generated at dist/index.js.

TypeScript Support

The package is written in TypeScript and includes type declarations. Ensure your tsconfig.json is similar to the following for best compatibility:

{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"declaration": true,
"outDir": "./dist",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

Testing with Cloudflare Workers

You can test the solution in a Cloudflare Worker by using Cloudflare Wrangler.

Steps to Test

  • Install Wrangler:
npm install -g @cloudflare/wrangler
  • Initialize a new Worker Project:
wrangler init --name cloudflare-docx-parser
  • Integrate cf-docx-parser:

Use the example provided above in your Worker script.

  • Run Locally:
wrangler dev

Contributing

Contributions, bug reports, and feature requests are welcome! Please submit a PR or open an issue on the GitHub repository.

License

This project is licensed under the MIT License.

Keywords

docx

FAQs

Package last updated on 14 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts