Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Import @hapi/joi in Worker cause error #393

Closed
hbinduni opened this issue Aug 4, 2019 · 6 comments
Closed

Import @hapi/joi in Worker cause error #393

hbinduni opened this issue Aug 4, 2019 · 6 comments
Labels
bug Something isn't working

Comments

@hbinduni
Copy link

hbinduni commented Aug 4, 2019

Hi, i use webpack and try to import @hapi/joi like this:

import {config, Endpoint, S3} from 'aws-sdk';
import moment from 'moment';
import Joi from '@hapi/joi';

It build successfully but in preview, it return error:

Error: Error("expected value", line: 2, column: 1)

If i remove import Joi part, everything goes well. Can you help to fixed this?

Screen Shot 2019-08-05 at 03 10 41

below my webpack.config.js:

module.exports = {
  entry: __dirname + '/index.js',
  mode: 'production',
  optimization: {
    minimize: false,
  },
  performance: {
    hints: false,
  },
  devtool: 'cheap-module-source-map',
  node: {
    net: 'empty',
    tls: 'empty',
    dns: 'empty',
  },
  output: {
    path: __dirname + '/dist',
    publicPath: 'dist',
    filename: 'worker.js',
  },
};

I put another post in cloudflare community for workers.

@xtuc xtuc added bug Something isn't working user report labels Aug 5, 2019
@xtuc
Copy link
Member

xtuc commented Aug 5, 2019

Thanks for the report. Could you please run the same command with a detailed stacktrace: RUST_BACKTRACE=1 wrangler preview and post it here?

xtuc added a commit that referenced this issue Aug 5, 2019
As seen in #393 previously
JSON decoding errors are obscure.

This changes improve the debugging in two cases:
  - The `package.json` file fails to decode; now emits a clearer error.
  - The wranglerjs backend returns an invalid JSON; now preserves the
  output file for further investigation.
    The console doesn't print the output file location by default, you
    need to pass `RUST_LOG=info` while running `wrangler build` and
    search for `--output-file=FILE` argument passed to wranglerjs.
@hbinduni
Copy link
Author

hbinduni commented Aug 5, 2019

this is stacktrace i get:

RUST_BACKTRACE=1 wrangler preview
⬇️ Installing wasm-pack...
⬇️ Installing wranglerjs...
✨   Built successfully.
Error: Error("expected value", line: 2, column: 1)

stack backtrace:
   0: backtrace::backtrace::trace::ha005f61e3cfbd07b (0x1050a1afd)
   1: backtrace::capture::Backtrace::new_unresolved::h339da43cb4cb2a6a (0x1050a0c67)
   2: failure::backtrace::internal::InternalBacktrace::new::h2f8d45be066dd26d (0x1050a04b8)
   3: <failure::backtrace::Backtrace as core::default::Default>::default::h174bed90f39d5e6b (0x1050a06b4)
   4: wrangler::commands::publish::preview::preview::h65c0824e07e11a23 (0x104d26e99)
   5: wrangler::main::hef4cb33fcad6dad6 (0x104d3b7c3)
   6: std::rt::lang_start::{{closure}}::h232ceef3a5e57413 (0x104d20692)
   7: std::panicking::try::do_call::h1252fc9a2ff235eb (0x105123937)
   8: ___rust_maybe_catch_panic (0x10512cfde)
   9: std::rt::lang_start_internal::h4c054360e442146c (0x10512441d)
  10: _main (0x104d3c0a8)

xtuc added a commit that referenced this issue Aug 5, 2019
As seen in #393 previously
JSON decoding errors are obscure.

This changes improve the debugging in three cases:
  - The `package.json` file fails to decode; now emits a clearer error.
  - The wranglerjs backend returns an invalid JSON; now preserves the
  output file for further investigation.
    The console doesn't print the output file location by default, you
    need to pass `RUST_LOG=info` while running `wrangler build` and
    search for `--output-file=FILE` argument passed to wranglerjs.
  - The preview service returns invalid JSON; now emits a clearer error
    and full output can be seen using `RUST_LOG=info`
xtuc added a commit that referenced this issue Aug 5, 2019
As seen in #393 previously
JSON decoding errors are obscure.

This changes improve the debugging in three cases:
  - The `package.json` file fails to decode; now emits a clearer error.
  - The wranglerjs backend returns an invalid JSON; now preserves the
  output file for further investigation.
    The console doesn't print the output file location by default, you
    need to pass `RUST_LOG=info` while running `wrangler build` and
    search for `--output-file=FILE` argument passed to wranglerjs.
  - The preview service returns invalid JSON; now emits a clearer error
    and full output can be seen using `RUST_LOG=info`
@ashleygwilliams ashleygwilliams added this to the 1.2.0 milestone Aug 5, 2019
@xtuc
Copy link
Member

xtuc commented Aug 5, 2019

We just merged a patch #394, would you mind installing wrangler from source, trying to run RUST_LOG=info RUST_BACKTRACE=1 wrangler preview and post the result? Thanks!

@hbinduni
Copy link
Author

hbinduni commented Aug 5, 2019

We just merged a patch #394, would you mind installing wrangler from source, trying to run RUST_LOG=info RUST_BACKTRACE=1 wrangler preview and post the result? Thanks!

below the result, it work ok when i comment out import Joi from @hapi/joi.

RUST_LOG=info RUST_BACKTRACE=1 wrangler preview
[2019-08-05T17:25:26Z INFO  wrangler] Getting project settings
[2019-08-05T17:25:26Z INFO  wrangler::commands::build::wranglerjs] skipping npm install because node_modules exists
[2019-08-05T17:25:26Z INFO  wrangler::install] prebuilt artifact https://github.com/cloudflare/wrangler/releases/download/v1.1.0/wranglerjs-v1.1.0.tar.gz
⬇️ Installing wranglerjs...
[2019-08-05T17:25:28Z INFO  wrangler::commands::build::wranglerjs] wranglerjs downloaded at: "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac"
[2019-08-05T17:25:28Z INFO  wrangler::commands::build::wranglerjs] Running "npm" "install" in directory "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac"

> [email protected] install /Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac/node_modules/fsevents
> node install

node-pre-gyp WARN Using needle for node-pre-gyp https download
[fsevents] Success: "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac/node_modules/fsevents/lib/binding/Release/node-v64-darwin-x64/fse.node" is installed via remote
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

added 391 packages from 217 contributors and audited 4226 packages in 18.347s
found 0 vulnerabilities

[2019-08-05T17:25:49Z INFO  wrangler::install] prebuilt artifact https://github.com/rustwasm/wasm-pack/releases/download/v0.8.1/wasm-pack-v0.8.1-x86_64-apple-darwin.tar.gz
⬇️ Installing wasm-pack...
[2019-08-05T17:25:49Z INFO  wrangler::commands::build::wranglerjs] Running "/usr/local/opt/node@10/bin/node" "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac" "--output-file=/var/folders/73/hynx970n2d94453__rdpx9cw0000gn/T/.wranglerjs_outputzNEj9" "--wasm-binding=wasmprogram" "--webpack-config=webpack.config.js"
✨   Built successfully, built project size is 773 KiB. ✨
[2019-08-05T17:25:55Z INFO  wrangler::commands::publish::upload_form] Webpack project detected. Publishing...
[2019-08-05T17:25:55Z INFO  wrangler::commands::publish::upload_form] Form { boundary: "874c94290d35479a8712ec4d73b2a571", parts: [("metadata", Part { value: Body { kind: b"{\"bindings\":[],\"body_part\":\"script\"}" }, mime: Some("application/json"), file_name: Some("metadata.json"), headers: {} }), ("script", Part { value: Body { kind: Reader { length: 5040774 } }, mime: Some("application/javascript"), file_name: Some("script.js"), headers: {} })] }
👷  GET https://00000000000000000000000000000000.cloudflareworkers.com 👷
👷  Your worker responded with:
    <h1>Image Worker</h1>
    <p>Click the below button to generate a new QR code. This will make a request to your serverless function.</p>
   👷

and this after i enable the import Joi from @hapi/joi:

RUST_LOG=info RUST_BACKTRACE=1 wrangler preview
[2019-08-05T17:30:46Z INFO  wrangler] Getting project settings
[2019-08-05T17:30:46Z INFO  wrangler::commands::build::wranglerjs] skipping npm install because node_modules exists
[2019-08-05T17:30:46Z INFO  wrangler::install] prebuilt artifact https://github.com/cloudflare/wrangler/releases/download/v1.1.0/wranglerjs-v1.1.0.tar.gz
⬇️ Installing wranglerjs...
[2019-08-05T17:30:46Z INFO  wrangler::commands::build::wranglerjs] wranglerjs downloaded at: "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac"
[2019-08-05T17:30:46Z INFO  wrangler::commands::build::wranglerjs] skipping npm install because node_modules exists
[2019-08-05T17:30:49Z INFO  wrangler::install] prebuilt artifact https://github.com/rustwasm/wasm-pack/releases/download/v0.8.1/wasm-pack-v0.8.1-x86_64-apple-darwin.tar.gz
⬇️ Installing wasm-pack...
[2019-08-05T17:30:49Z INFO  wrangler::commands::build::wranglerjs] Running "/usr/local/opt/node@10/bin/node" "/Users/binduni/Library/Caches/.wrangler/wranglerjs-1e3edcc6c25d97ac" "--output-file=/var/folders/73/hynx970n2d94453__rdpx9cw0000gn/T/.wranglerjs_outputXtGa5" "--wasm-binding=wasmprogram" "--webpack-config=webpack.config.js"
✨   Built successfully, built project size is 1 MiB. ⚠️  Your built project has grown past the 1MiB size limit and may fail to deploy. ⚠️  ✨
[2019-08-05T17:30:54Z INFO  wrangler::commands::publish::upload_form] Webpack project detected. Publishing...
[2019-08-05T17:30:54Z INFO  wrangler::commands::publish::upload_form] Form { boundary: "9e0178140edf4022aa2a0b73a32c3dc8", parts: [("metadata", Part { value: Body { kind: b"{\"bindings\":[],\"body_part\":\"script\"}" }, mime: Some("application/json"), file_name: Some("metadata.json"), headers: {} }), ("script", Part { value: Body { kind: Reader { length: 6915393 } }, mime: Some("application/javascript"), file_name: Some("script.js"), headers: {} })] }
Error: https://cloudflareworkers.com/script: Server Error: 502 Bad Gateway
stack backtrace:
   0: backtrace::backtrace::trace::h27a7a01d1b768196 (0x105dd9c8e)
   1: backtrace::capture::Backtrace::new_unresolved::hc47d4bbb7be8e720 (0x105dd8d98)
   2: failure::backtrace::internal::InternalBacktrace::new::hdcbafa819eb3492b (0x105dd85a9)
   3: <failure::backtrace::Backtrace as core::default::Default>::default::h4c7ac0d2835dc7ae (0x105dd87a5)
   4: wrangler::commands::publish::preview::preview::h1006cac7d9aa50f2 (0x105ad0ed4)
   5: wrangler::run::h8d7b2f547df1e297 (0x105aea6f7)
   6: wrangler::main::h1798a82f4300f3cb (0x105ae80b5)
   7: std::rt::lang_start::{{closure}}::h530d08244901b4e8 (0x105ad60e6)
   8: std::panicking::try::do_call::h8037d9f03e27d896 (0x105ee1638)
   9: ___rust_maybe_catch_panic (0x105eeb06f)
  10: std::rt::lang_start_internal::hc8e69e673740d4ae (0x105ee211e)
  11: _main (0x105aeb349)

it seem the issue because the size more than 1Mb. if that so, how can i allow more bigger package to be work?

@lauriys
Copy link

lauriys commented Aug 7, 2019

you might want to look into this issue: hapijs/joi#528 and into the slightly outdated joi-browser package for inspiration

@ashleymichal
Copy link
Contributor

ashleymichal commented Aug 9, 2019

hey @hbinduni ! I think there are a couple of things we can do to make this clearer:

  1. You'll notice that there is an error printed after wrangler finishes building in the version with the hapi/joi package:
✨   Built successfully, built project size is 1 MiB. ⚠️  Your built project has grown past the 1MiB size limit and may fail to deploy. ⚠️  ✨

We can reduce a good deal of confusion by making this error more prominent.

  1. It might make sense for wrangler to halt preview if your build exceeds the 1Mb limit imposed by the Workers platform.

As to your question, the limits on script size are imposed by the Cloudflare Workers platform, not by Wrangler; Wrangler just checks on your behalf. You can make adjustments to your custom Webpack config that reduce the size for Workers, such as disabling source maps, turning on minification, etc.

edit: new issue opened to address the above: #417

@ashleygwilliams ashleygwilliams removed this from the 1.2.0 milestone Aug 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants