Skip to content

Commit 58a4c4a

Browse files
committed
feat!: use buffer npm module to add browser support
1 parent 5c41614 commit 58a4c4a

File tree

9 files changed

+23
-10
lines changed

9 files changed

+23
-10
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@
4444
"test": "run-s test:compile test:integration",
4545
"test:compile": "tsc --noEmit",
4646
"test:quality": "xo source/ test/",
47-
"test:integration": "ava"
47+
"test:integration": "TSIMP_DIAG=ignore ava"
4848
},
4949
"dependencies": {
50+
"buffer": "6.0.3",
5051
"fflate": "0.8.2",
5152
"file-type": "19.3.0",
5253
"got": "14.4.1",

pnpm-lock.yaml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/lib.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// source/lib.ts
22
// The source code for the library.
33

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

source/parsers/doc.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// source/parsers/docx.ts
22
// The text extracter for DOCX files.
33

4-
import { type Buffer } from 'node:buffer'
4+
import { type Buffer } from 'buffer/'
55
import { extractRawText as parseWordFile } from 'mammoth'
66

77
import type { TextExtractionMethod } from '../lib.js'
@@ -22,6 +22,7 @@ export class DocExtractor implements TextExtractionMethod {
2222
*/
2323
apply = async (input: Buffer): Promise<string> => {
2424
// Convert the DOCX to text and return the text.
25+
// @ts-expect-error: see feross/buffer#353, the types are incomplete.
2526
const parsedDocx = await parseWordFile({ buffer: input })
2627
return parsedDocx.value
2728
}

source/parsers/excel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// source/parsers/excel.ts
22
// The text extracter for Excel files.
33

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

source/parsers/pdf.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// source/parsers/pdf.ts
22
// The text extracter for PDF files.
33

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

source/parsers/ppt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

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

6-
import { type Buffer } from 'node:buffer'
6+
import { type Buffer } from 'buffer/'
77
import { unzip } from 'fflate'
88
import { parseStringPromise as xmlToJson } from 'xml2js'
99
import encoding from 'text-encoding'

source/util.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// source/util.ts
22
// Utility functions to help with the handling of input.
33

4-
import { type Buffer } from 'node:buffer'
54
import { readFile as read } from 'node:fs/promises'
65
import { got as fetch } from 'got'
6+
import { type Buffer } from 'buffer/'
77

88
export const readFile = async (filePath: string): Promise<Buffer> =>
9-
read(filePath)
9+
(await read(filePath)) as unknown as Buffer
1010
export const fetchUrl = async (url: string): Promise<Buffer> =>
11-
fetch(url).buffer()
11+
(await fetch(url).buffer()) as unknown as Buffer

test/integration/lib.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// This file contains the integration test for the library.
33

44
import { readFileSync } from 'node:fs'
5-
import { type Buffer } from 'node:buffer'
65
import test from 'ava'
76

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

1010
const extractor = getTextExtractor()

0 commit comments

Comments
 (0)