cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.61k stars 679 forks source link

🐛 BUG: wrangler publish will error out if using pdf.js #3113

Closed ivarec closed 1 year ago

ivarec commented 1 year ago

Which Cloudflare product(s) does this pertain to?

Wrangler

What version of Wrangler are you using?

2.17.0

What operating system are you using?

Windows

Describe the Bug

If I try to use pdf.js in a Cloudflare Worker, wrangler publish fails with the following stack trace:

⛅️ wrangler 2.17.0 
--------------------
Your worker has access to the following bindings:
- R2 Buckets:
  - FOO_FILES: foo_files
Total Upload: 679.03 KiB / gzip: 107.38 KiB

X [ERROR] A request to the Cloudflare API (/accounts/***/workers/scripts/foobar) failed.

  Uncaught TypeError: Cannot read properties of undefined (reading 'includes')
    at index.js:628:45 in get platform
    at index.js:3337:40 in KeyboardManager
    at index.js:3441:41 in <static_initializer>
    at index.js:3410:35 in Object.defineProperty.value
    at index.js:15532:40 in __w_pdfjs_require__
    at index.js:2903:26 in Object.defineProperty.value
    at index.js:15532:40 in __w_pdfjs_require__
    at index.js:2770:27 in Object.defineProperty.value
    at index.js:15532:40 in __w_pdfjs_require__
    at index.js:882:39 in Object.defineProperty.value
   [code: 10021]

  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/workers-sdk/issues/new/choose

I can easily reproduce it with the following minimal code (after installing pdf-dist from npm):

import * as pdfjsLib from 'pdfjs-dist'

export default {
  async fetch (request, env, ctx) {
    return new Response()
  }
}
gdoteof commented 1 year ago

also running into this, and see one here: https://community.cloudflare.com/t/pdfjs-dist-npm-package-not-working/483435

penalosa commented 1 year ago

Thanks for reporting this! It doesn't look like the pdf.js library is supported in the Cloudflare Workers environment. Going forward, the Workers runtime is working on adding support for more Node.JS builtins, which should improve library compatibility.

wuservices commented 9 months ago

There's now https://github.com/unjs/unpdf, which should work with Workers (with a few limitations)

Soviut commented 5 months ago

For posterity, I was able to get this wrapped version of pdfjs to work. https://github.com/johannschopplich/pdfjs-serverless