Skip to content

Commit

Permalink
feat!: use buffer npm module to add browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Jul 23, 2024
1 parent 5c41614 commit 58a4c4a
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@
"test": "run-s test:compile test:integration",
"test:compile": "tsc --noEmit",
"test:quality": "xo source/ test/",
"test:integration": "ava"
"test:integration": "TSIMP_DIAG=ignore ava"
},
"dependencies": {
"buffer": "6.0.3",
"fflate": "0.8.2",
"file-type": "19.3.0",
"got": "14.4.1",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/lib.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// source/lib.ts
// The source code for the library.

import { Buffer } from 'node:buffer'
import { Buffer } from 'buffer/index.js'
import { fileTypeFromBuffer as getFileType } from 'file-type'
import { readFile, fetchUrl } from './util.js'

Expand Down
3 changes: 2 additions & 1 deletion source/parsers/doc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// source/parsers/docx.ts
// The text extracter for DOCX files.

import { type Buffer } from 'node:buffer'
import { type Buffer } from 'buffer/'
import { extractRawText as parseWordFile } from 'mammoth'

import type { TextExtractionMethod } from '../lib.js'
Expand All @@ -22,6 +22,7 @@ export class DocExtractor implements TextExtractionMethod {
*/
apply = async (input: Buffer): Promise<string> => {
// Convert the DOCX to text and return the text.
// @ts-expect-error: see feross/buffer#353, the types are incomplete.
const parsedDocx = await parseWordFile({ buffer: input })
return parsedDocx.value
}
Expand Down
2 changes: 1 addition & 1 deletion source/parsers/excel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// source/parsers/excel.ts
// The text extracter for Excel files.

import { type Buffer } from 'node:buffer'
import { type Buffer } from 'buffer/'
import Xlsx, { utils as sheetUtils } from 'xlsx'
import { dump as convertToYaml } from 'js-yaml'

Expand Down
2 changes: 1 addition & 1 deletion source/parsers/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// source/parsers/pdf.ts
// The text extracter for PDF files.

import { type Buffer } from 'node:buffer'
import { type Buffer } from 'buffer/'
// @ts-expect-error There are no types for this package.
import parsePdf from 'pdf-parse/lib/pdf-parse.js'

Expand Down
2 changes: 1 addition & 1 deletion source/parsers/ppt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import { type Buffer } from 'node:buffer'
import { type Buffer } from 'buffer/'
import { unzip } from 'fflate'
import { parseStringPromise as xmlToJson } from 'xml2js'
import encoding from 'text-encoding'
Expand Down
6 changes: 3 additions & 3 deletions source/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// source/util.ts
// Utility functions to help with the handling of input.

import { type Buffer } from 'node:buffer'
import { readFile as read } from 'node:fs/promises'
import { got as fetch } from 'got'
import { type Buffer } from 'buffer/'

export const readFile = async (filePath: string): Promise<Buffer> =>
read(filePath)
(await read(filePath)) as unknown as Buffer
export const fetchUrl = async (url: string): Promise<Buffer> =>
fetch(url).buffer()
(await fetch(url).buffer()) as unknown as Buffer
2 changes: 1 addition & 1 deletion test/integration/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// This file contains the integration test for the library.

import { readFileSync } from 'node:fs'
import { type Buffer } from 'node:buffer'
import test from 'ava'

import { type Buffer } from 'buffer/'
import { getTextExtractor, type InputType } from '../../source/index.js'

const extractor = getTextExtractor()
Expand Down

0 comments on commit 58a4c4a

Please sign in to comment.