microsoft / botbuilder-js

Welcome to the Bot Framework SDK for JavaScript repository, which is the home for the libraries and packages that enable developers to build sophisticated bot applications using JavaScript.
https://github.com/Microsoft/botframework
MIT License
679 stars 276 forks source link

Using botframework-streaming in browser/webpack@5/esbuild does not work #3884

Closed compulim closed 6 months ago

compulim commented 3 years ago

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Versions

4.14.1

Describe the bug

The botframework-streaming is not configured/transpiled correctly to use in browser.

To Reproduce

Steps to reproduce the behavior:

  1. Use webpack@5 or esbuild to consume botframework-streaming
  2. Upload a file via DL ASE protocol

Expected behavior

It should able to upload a file.

Instead, due to buffer, process.nextTick(), and stream is not available from webpack@5 and esbuild, the package will throw an exception.

Due to development team structure and policies, customers may not have access to configurations of Webpack or esbuild. For example, in some customers' scenario, they are forbidden to "eject" the app from its scaffold/template. Thus, they would not have access to any configurations. SDK should work as by default and transpiled and bundled correctly.

Thus, the botframework-streaming package must not request the customer to change their project/host configuration to use botframework-streaming.

Screenshots

If applicable, add screenshots to help explain your problem.

Additional context

How to package correctly

Please look at the build scripts of uuid package on how they do packaging. They cover many scenarios including: CJS, ESM, browser-only (crypto vs. Web Crypto). And the techniques they applied, should be sufficient for botframework-streaming package.

Also read about exports field in package.json.

In short:

How to test if the packaging is done correctly

Write index.js to consume botframework-streaming

import * as BFSE from 'botframework-streaming';

// Write some test cases that hit certain critical code paths in BFSE, such as file upload.
console.log(BFSE);

Create a new web app project

Create a new empty project root.

Run npm init to initialize the project.

Run npm install botframework-streaming to install the package. (You can also install botframework-streaming-*.tgz for local testing).

Bundle with webpack@5

Install Webpack by running npm install webpack@5.

Create webpack.config.json:

{
  "mode": "production",
  "entry": "index.js"
}

Note: Do not use babel-loader or any loaders to load any JS files. The published package should be ES5 of both Common JS and ES Module format. Transpile, if needed, should be done before bundle.

Then, run webpack. It will produce output at dist/main*.js.

Write a HTML page to load the dist/main*.js file.

Navigate to this page in all supported browsers (including mobile browsers). The code in index.js should run completely and successfully. For example, if the code in index.js contains file uploading logic, running the HTML page should upload a file.

Bundle with esbuild

Install esbuild by running npm install esbuild

Run esbuild --bundle --outfile=dist/main-esbuild.js ./index.js. It will produce an output file at dist/main-esbuild.js.

Write another HTML page to load the dist/main-esbuild.js file.

Navigate to this page in all supported browsers (including mobile browsers). The code in index.js should run completely and successfully.

Test using IE11

IE11 is an excellent tool for testing if the package is bundled/transpiled correctly. Using latest browsers also works, but it is not as trivial as loading the bundle in IE11.

Two criteria:

Pre-transpile or post-transpile

Currently, there is a trend of exporting ESnext code. ESnext code may not work in latest browsers.

Devs are slowly moving from pre-transpile to post-transpile.

For now, it's good to do pre-transpile. For the next 6-18 months, we may move to post-transpile. There are few advantages of post-tranpsile:

compulim commented 3 years ago

@stevengum DLJS is having issues when bumping Webpack from 4 to 5 (due to security vulnerability). Starting from webpack@5, the botframework-streaming package is no longer working properly due to poor bundling/transpilation.

mrivera-ms commented 2 years ago

Assigning to @JuanAr for investigation.

sw-joelmut commented 2 years ago

@mrivera-ms in progress

sw-joelmut commented 2 years ago

Hi @mrivera-ms, @compulim. We were able to reproduce the issue using the DL ASE protocol with the 4.14.1 version of botbuilder, where the bot doesn’t receive the attachment in the Activity when sending a file. Bumping the botbuilder version in the deployed bot to the latest 4.15.0, the bot started receiving the attachment through DL ASE.

Bot sample with the reproduced the issue (with instructions): issue-3884.zip

To be able to test the DL ASE protocol functionality, we followed these guides to configure our environment:

imagen

tracyboehrer commented 6 months ago

Closing. @compulim If you feel differently, please reopen.