|
| 1 | +/** `officeparser`'s single entry point, as every parser here calls it. */ |
| 2 | +type ParseOfficeAsync = (input: Buffer) => Promise<string> |
| 3 | + |
| 4 | +interface OfficeParserModule { |
| 5 | + parseOfficeAsync?: ParseOfficeAsync |
| 6 | + default?: { parseOfficeAsync?: ParseOfficeAsync } | ParseOfficeAsync |
| 7 | +} |
| 8 | + |
| 9 | +/** |
| 10 | + * Resolves `officeparser`'s entry point across module systems. |
| 11 | + * |
| 12 | + * `officeparser` is CommonJS — `main: officeParser.js`, no `type` and no |
| 13 | + * `exports` map — so what `await import('officeparser')` yields depends on who |
| 14 | + * built the code. Node and webpack synthesize named exports from the CJS |
| 15 | + * `module.exports`, so `.parseOfficeAsync` is there. esbuild, which builds the |
| 16 | + * Trigger.dev worker bundle, puts `module.exports` on `.default` and leaves the |
| 17 | + * named export undefined. |
| 18 | + * |
| 19 | + * Reading the named export directly therefore worked everywhere except the |
| 20 | + * worker, where `parseOfficeAsync` was `undefined` and calling it threw |
| 21 | + * `TypeError: parseOfficeAsync is not a function`. Every parser here treats that |
| 22 | + * as "the library failed" and falls back to scraping the archive, which returns |
| 23 | + * `degraded: true` — and the document pipeline rejects a degraded parse outright. |
| 24 | + * The visible result was every `.pptx` and legacy `.doc` from a connector |
| 25 | + * failing as "No text could be extracted", while the same files parsed fine |
| 26 | + * through the app. |
| 27 | + * |
| 28 | + * Reading both shapes fixes it at the source rather than per bundler: the |
| 29 | + * alternative is externalizing the package in each build config, which has to be |
| 30 | + * repeated for every bundler this code runs under and silently regresses the day |
| 31 | + * one is missed. |
| 32 | + */ |
| 33 | +export function resolveParseOfficeAsync(mod: OfficeParserModule): ParseOfficeAsync { |
| 34 | + if (typeof mod.parseOfficeAsync === 'function') return mod.parseOfficeAsync |
| 35 | + if (typeof mod.default === 'function') return mod.default |
| 36 | + if (typeof mod.default?.parseOfficeAsync === 'function') return mod.default.parseOfficeAsync |
| 37 | + |
| 38 | + throw new Error('officeparser did not expose parseOfficeAsync') |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Split from {@link resolveParseOfficeAsync} so the shape handling is testable. |
| 43 | + * The failing shape cannot be reproduced by mocking the specifier — Vitest's |
| 44 | + * module-namespace proxy throws on a missing export rather than yielding the |
| 45 | + * `undefined` the real bundle produces — so a test that goes through `import` |
| 46 | + * can only assert the shape that already worked. |
| 47 | + */ |
| 48 | +export async function loadParseOfficeAsync(): Promise<ParseOfficeAsync> { |
| 49 | + return resolveParseOfficeAsync((await import('officeparser')) as OfficeParserModule) |
| 50 | +} |
0 commit comments