Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw a exception when be imported into Browser (#1344) #1345

Merged
merged 4 commits into from
Jul 7, 2018
Merged

throw a exception when be imported into Browser (#1344) #1345

merged 4 commits into from
Jul 7, 2018

Conversation

huan
Copy link
Contributor

@huan huan commented Mar 31, 2018

package.json Outdated
@@ -16,6 +16,7 @@
"author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
"license": "MIT",
"main": "index.js",
"browser": "browser.js",
"files": [
"index.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add browser.js

browser.js Outdated
@@ -0,0 +1,3 @@
module.exports = function () {
throw new Error('ws only supports Node.js! Please consider to use `isomorphic-ws` for both Node.js & Browser support! (See issue #1344 at https://github.com/websockets/ws/issues/1344 for more information.');
Copy link
Member

@lpinca lpinca Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the limit of 80 chars per line. Also, I don't want to endorse any particular wrapper, we are already doing it in the README and there is no need to point to that particular issue, there a few already which are all duplicates.

I'd just use a very simple message like "ws does not work in the browser. Browser clients must use the native WebSocket object". I think this is sufficient to point the user into the right direction.

@huan
Copy link
Contributor Author

huan commented Mar 31, 2018

@lpinca Changed as you request.

@lpinca
Copy link
Member

lpinca commented Mar 31, 2018

cc: @3rd-Eden

@3rd-Eden
Copy link
Member

3rd-Eden commented Apr 4, 2018

I'm fine with this but it might be a major release as there is a chance of breaking people's code if they do a weird fallback like:

var WS = require('ws') || WebSocket;

@huan
Copy link
Contributor Author

huan commented Apr 5, 2018

@3rd-Eden Thanks for accepting this.

I feel this will not breaking anything because according to the current code base, the return value of require('ws') will neither be undefined nor null, so the || WebSocket part in the fallback you mentioned will never get chance to be executed.

@lpinca
Copy link
Member

lpinca commented Apr 5, 2018

@zixia I think @3rd-Eden wanted to write:

var WS = global.WebSocket || require('ws');

I think this will throw now if the code is bundled for Node.js?

@huan
Copy link
Contributor Author

huan commented Apr 5, 2018

@lpinca I believe it will not throw in the Node.js environment.

Because in Node.js global.WebSocket will be undefined, then WS will be set as require('ws'), which should be right.

@lpinca
Copy link
Member

lpinca commented Apr 5, 2018

Yes but isn't the browser shim used in that case?

@lpinca
Copy link
Member

lpinca commented Apr 5, 2018

It throws

$ cat index.js 
const WS = global.WebSocket || require('ws');

const ws = new WS('ws://echo.websocket.org');
ws.onopen = function () {
  console.log('It works');
};
$ browserify -u bufferutil -u utf-8-validate --no-builtins --insert-global-vars="global" index.js > bundle.js
$ node bundle.js

@lpinca
Copy link
Member

lpinca commented Apr 5, 2018

The --no-browser-field option can be used to make it work again but I think it's still a breaking change.

@huan
Copy link
Contributor Author

huan commented Apr 6, 2018

Yes, you are right.

I did not consider the browserify case.

'use strict';

module.exports = function () {
throw new Error(
Copy link

@shogowada shogowada May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've read #1344, but I am personally not sure why it needs to throw when it can fallback. It looks like we are waiting for @3rd-Eden 's input?

Maybe it's already discussed at other places, but should this fallback to browser's WebSocket, while announcing that WebSocket.Server doesn't work on browser?

Although, I see that ws's API isn't 100% compatible with browser's WebSocket, so I understand if we don't want to mislead people that it should work seamlessly on both browser and Node.js 100% of the time.

Given that, I think falling back to browser's WebSocket would make many projects' dependency graph a bit cleaner and more straightforward when they need to implement a platform agnostic web socket client, and I think it would make ws package more attractive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shogowada this has been discussed ad nauseam, we don't want to maintain a browser shim because there are so many differences/edge cases that makes it a pain.

Refs: e54d45f

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the additional context.

In that case, instead of throwing (that appears to break some), would a warning work?

Something like this:

module.exports = function () {
  // Almost a copy & paste from the README + link to the ref you gave me for future people like me🙂
  console.warn('ws module does not work in the browser. Browser clients must use the native WebSocket object. To make the same code work seamlessly on Node.js and the browser, you can use one of the many wrappers available on npm, like isomorphic-ws. For more context, see https://github.com/websockets/ws/commit/e54d45fbab3b19c2940e9057ce1e7b8f105873e0.')
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it helps, it still breaks existing code. Throwing an error is the right thing to do imho.

@lpinca
Copy link
Member

lpinca commented Jul 7, 2018

Finally merged. Thank you.

@huan
Copy link
Contributor Author

huan commented Jul 8, 2018

Glad to see that, cheers!

goto-bus-stop pushed a commit to u-wave/http-api that referenced this pull request Jul 21, 2018

## Version **6.0.0** of **ws** was just published.

<table>
  <tr>
    <th align=left>
      Dependency
    </th>
    <td>
      <a target=_blank href=https://github.com/websockets/ws>ws</a>
    </td>
  </tr>
  <tr>
      <th align=left>
       Current Version
      </th>
      <td>
        5.2.2
      </td>
    </tr>
  <tr>
    <th align=left>
      Type
    </th>
    <td>
      dependency
    </td>
  </tr>
</table>



The version **6.0.0** is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

It might be worth looking into these changes and trying to get this project onto the latest version of ws.

If you have a solid test suite and good coverage, a passing build is a strong indicator that you can take advantage of these changes directly by merging the proposed change into your project. If the build fails or you don’t have such unconditional trust in your tests, this branch is a great starting point for you to work on the update.


---


<details>
<summary>Release Notes</summary>
<strong>6.0.0</strong>

<h1>Breaking changes</h1>
<ul>
<li>Dropped support for Node.js 4 (<a class="commit-link" href="https://urls.greenkeeper.io/websockets/ws/commit/d73885c3f7c70b583030d683d9a0a025c98fbe00"><tt>d73885c</tt></a>).</li>
<li>Added a shim that throws an error when used if the package is bundled for the<br>
browser (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="310221102" data-permission-text="Issue title is private" data-url="websockets/ws#1345" href="https://urls.greenkeeper.io/websockets/ws/pull/1345">#1345</a>).</li>
<li>Added a <code>maxPayload</code> option on the client. Defaults to 100 MiB (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="333186455" data-permission-text="Issue title is private" data-url="websockets/ws#1402" href="https://urls.greenkeeper.io/websockets/ws/pull/1402">#1402</a>).</li>
<li>Dropped support for the <code>memLevel</code> and <code>level</code> options. Use<br>
<code>zlibDeflateOptions</code> instead. (<a class="commit-link" href="https://urls.greenkeeper.io/websockets/ws/commit/80e20021f314d66e80032ecb8c2854ac61c6073c"><tt>80e2002</tt></a>).</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 15 commits ahead by 15, behind by 2.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/1ee42fd67d365409096c11af0d6bc70fbe292c60"><code>1ee42fd</code></a> <code>[dist] 6.0.0</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/d963003a40bfe6cdd58eb3d3e4458eb2b2090a2c"><code>d963003</code></a> <code>[example] Update dependencies</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/38d2e8b3f75ebccf8b0b205fafac959c1702ddfb"><code>38d2e8b</code></a> <code>chore(package): update eslint-plugin-node to version 7.0.0 (#1420)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/fc957939460cd0c461618bef4c6d59a9b5a4b90e"><code>fc95793</code></a> <code>[fix] Fix use after invalidation bug</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/fbd43914ad557ed915c41108b2f98b508a720216"><code>fbd4391</code></a> <code>[fix] Fix compatibility with Node.js 6</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/b354cd138e62dcb1e4e05ce8b0bc097534f23d76"><code>b354cd1</code></a> <code>[minor] Remove no longer needed workaround for <code>socketPath</code> option</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/ef5a8f5f5e4d3843d318d2b8a463b49d8105bd2e"><code>ef5a8f5</code></a> <code>[pkg] Update eslint-plugin-standard to version 3.1.0</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/80e20021f314d66e80032ecb8c2854ac61c6073c"><code>80e2002</code></a> <code>[major] Drop support for the <code>memLevel</code> and <code>level</code> options</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/92d0a2e9fc2b1cca6eb1ddf88ec4347986164cb1"><code>92d0a2e</code></a> <code>[major] Add <code>maxPayload</code> option for the client (#1402)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/9f87842888688318464af498300395b197b29712"><code>9f87842</code></a> <code>[major] Make bundlers use a browser shim that throws an error (#1345)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/72bfbe84f3d747b96416a646728932c9181ceffd"><code>72bfbe8</code></a> <code>chore(package): update bufferutil to version 4.0.0 (#1413)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/5bb29ed019529f17a557d59b3eb5d9a14f9e5643"><code>5bb29ed</code></a> <code>chore(package): update utf-8-validate to version 5.0.0 (#1415)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/3bc6b9672f60b7178f115e51beaf4f664f91484c"><code>3bc6b96</code></a> <code>chore(package): update eslint to version 5.0.0 (#1403)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/d73885c3f7c70b583030d683d9a0a025c98fbe00"><code>d73885c</code></a> <code>[major] Drop support for Node.js 4</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/5d90141505dc41c129ab5d5228e37d49979d7541"><code>5d90141</code></a> <code>chore(package): update eslint-plugin-import to version 2.13.0 (#1405)</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/websockets/ws/compare/5d55e52529167c25f4fec35cb4753294e75bf9f2...1ee42fd67d365409096c11af0d6bc70fbe292c60">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
goto-bus-stop pushed a commit to goto-bus-stop/wikibattle that referenced this pull request Jul 21, 2018

## Version **6.0.0** of **ws** was just published.

<table>
  <tr>
    <th align=left>
      Dependency
    </th>
    <td>
      <a target=_blank href=https://github.com/websockets/ws>ws</a>
    </td>
  </tr>
  <tr>
      <th align=left>
       Current Version
      </th>
      <td>
        5.2.2
      </td>
    </tr>
  <tr>
    <th align=left>
      Type
    </th>
    <td>
      dependency
    </td>
  </tr>
</table>



The version **6.0.0** is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

It might be worth looking into these changes and trying to get this project onto the latest version of ws.

If you have a solid test suite and good coverage, a passing build is a strong indicator that you can take advantage of these changes directly by merging the proposed change into your project. If the build fails or you don’t have such unconditional trust in your tests, this branch is a great starting point for you to work on the update.


---


<details>
<summary>Release Notes</summary>
<strong>6.0.0</strong>

<h1>Breaking changes</h1>
<ul>
<li>Dropped support for Node.js 4 (<a class="commit-link" href="https://urls.greenkeeper.io/websockets/ws/commit/d73885c3f7c70b583030d683d9a0a025c98fbe00"><tt>d73885c</tt></a>).</li>
<li>Added a shim that throws an error when used if the package is bundled for the<br>
browser (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="310221102" data-permission-text="Issue title is private" data-url="websockets/ws#1345" href="https://urls.greenkeeper.io/websockets/ws/pull/1345">#1345</a>).</li>
<li>Added a <code>maxPayload</code> option on the client. Defaults to 100 MiB (<a class="issue-link js-issue-link" data-error-text="Failed to load issue title" data-id="333186455" data-permission-text="Issue title is private" data-url="websockets/ws#1402" href="https://urls.greenkeeper.io/websockets/ws/pull/1402">#1402</a>).</li>
<li>Dropped support for the <code>memLevel</code> and <code>level</code> options. Use<br>
<code>zlibDeflateOptions</code> instead. (<a class="commit-link" href="https://urls.greenkeeper.io/websockets/ws/commit/80e20021f314d66e80032ecb8c2854ac61c6073c"><tt>80e2002</tt></a>).</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 15 commits ahead by 15, behind by 2.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/1ee42fd67d365409096c11af0d6bc70fbe292c60"><code>1ee42fd</code></a> <code>[dist] 6.0.0</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/d963003a40bfe6cdd58eb3d3e4458eb2b2090a2c"><code>d963003</code></a> <code>[example] Update dependencies</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/38d2e8b3f75ebccf8b0b205fafac959c1702ddfb"><code>38d2e8b</code></a> <code>chore(package): update eslint-plugin-node to version 7.0.0 (#1420)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/fc957939460cd0c461618bef4c6d59a9b5a4b90e"><code>fc95793</code></a> <code>[fix] Fix use after invalidation bug</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/fbd43914ad557ed915c41108b2f98b508a720216"><code>fbd4391</code></a> <code>[fix] Fix compatibility with Node.js 6</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/b354cd138e62dcb1e4e05ce8b0bc097534f23d76"><code>b354cd1</code></a> <code>[minor] Remove no longer needed workaround for <code>socketPath</code> option</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/ef5a8f5f5e4d3843d318d2b8a463b49d8105bd2e"><code>ef5a8f5</code></a> <code>[pkg] Update eslint-plugin-standard to version 3.1.0</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/80e20021f314d66e80032ecb8c2854ac61c6073c"><code>80e2002</code></a> <code>[major] Drop support for the <code>memLevel</code> and <code>level</code> options</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/92d0a2e9fc2b1cca6eb1ddf88ec4347986164cb1"><code>92d0a2e</code></a> <code>[major] Add <code>maxPayload</code> option for the client (#1402)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/9f87842888688318464af498300395b197b29712"><code>9f87842</code></a> <code>[major] Make bundlers use a browser shim that throws an error (#1345)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/72bfbe84f3d747b96416a646728932c9181ceffd"><code>72bfbe8</code></a> <code>chore(package): update bufferutil to version 4.0.0 (#1413)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/5bb29ed019529f17a557d59b3eb5d9a14f9e5643"><code>5bb29ed</code></a> <code>chore(package): update utf-8-validate to version 5.0.0 (#1415)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/3bc6b9672f60b7178f115e51beaf4f664f91484c"><code>3bc6b96</code></a> <code>chore(package): update eslint to version 5.0.0 (#1403)</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/d73885c3f7c70b583030d683d9a0a025c98fbe00"><code>d73885c</code></a> <code>[major] Drop support for Node.js 4</code></li>
<li><a href="https://urls.greenkeeper.io/websockets/ws/commit/5d90141505dc41c129ab5d5228e37d49979d7541"><code>5d90141</code></a> <code>chore(package): update eslint-plugin-import to version 2.13.0 (#1405)</code></li>
</ul>
<p>See the <a href="https://urls.greenkeeper.io/websockets/ws/compare/5d55e52529167c25f4fec35cb4753294e75bf9f2...1ee42fd67d365409096c11af0d6bc70fbe292c60">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants