heineiuo / isomorphic-ws Goto Github PK
View Code? Open in Web Editor NEWIsomorphic implementation of WebSocket (https://www.npmjs.com/package/ws)
License: MIT License
Isomorphic implementation of WebSocket (https://www.npmjs.com/package/ws)
License: MIT License
It would allow the consumer to specify which version of ws to use without having to rerelease this package.
See: https://github.com/websockets/ws/releases/tag/8.0.0
Previously it used the same API as the client standard, but now it needs you to specifically decode messages:
websocket.on('message', function message(data, isBinary) {
const message = isBinary ? data : data.toString();
// Continue as before.
});
Error during communication with engine {"stack":"Error: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid.\n at RPC.createSocket (http://localhost:4200/**/apps/**/ma
signedToken = jwt.sign(this.token, this.key, { algorithm: 'RS256' });
config = {
schema: this.schema,
url: wss://${this.senseHost}/${this.proxyPrefix}/app/engineData
,
// Notice how the signed JWT is passed in the 'Authorization' header using the
// 'Bearer' schema:
createSocket: (url) => new WebSocket(url,
{
headers: { Authorization: 'Bearer ' + this.signedToken }
})
};
createEnigmaSession() {
let session;
if (session) {
session.close();
}
session = enigma.create(this.config);
session.on('traffic:*', (dir, data) => console.log(dir, data));
return session;
}
I keep getting the below error when trying to use this package with Deno via ESM.SH.
/* esm.sh - error */
throw new Error("[esm.sh] " + "parseCJSModuleExports: Can't resolve 'ws' in '/tmp/esm-build-22c48223b295c5ba9976ebd601dab8cc371bc315-87cf645d/node_modules/isomorphic-ws'");
export default null;
This is the error that I get when importing isomorphic-ws
and bundling with rollup
:
[!] Error: 'default' is not exported by node_modules/isomorphic-ws/browser.js, imported by node_modules/@holochain/conductor-api/lib.es/websocket/client.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/@holochain/conductor-api/lib.es/websocket/client.js (10:7)
8: });
9: };
10: import Websocket from "isomorphic-ws";
So you have to add the rollup commonjs plugin to make it work.
I think it would be really good if this library would export es modules as well, it would improve ease of use in a lot of cases, even more so when it only needs to expose the native WebSocket
object. I can make a PR if there is good feedback about this.
Can you make this library support TypeScript, please?
16 12 2022 11:18:24.725:INFO [compiler.karma-typescript]: Compiling project using Typescript 4.9.4
16 12 2022 11:18:29.280:INFO [compiler.karma-typescript]: Compiled 26 files in 4529 ms.
16 12 2022 11:18:30.135:ERROR [validator.karma-typescript]: Invalid syntax in bundle: 'import' and 'export' may only appear at the top level (23:0)
in /var/folders/mn/61rhh3pd1fscx3hrwc0wvpy40000gn/T/karma-typescript-bundle--56131-pidm5G34zm0L-.js
at line 23, column 0:
... export default ws
},'node_modules/isomorphic-ws/b ...
The ClientOptions are not currently accessible in TypeScript.
Hi there,
Is there a way to compile using webpack so in the case of browser compilation, it does not throw errors because it tries to compile ws
too ?
WARNING in ./node_modules/ws/lib/buffer-util.js
Module not found: Error: Can't resolve 'bufferutil' in '/.../node_modules/ws/lib'
@ ./node_modules/ws/lib/buffer-util.js 35:21-42
@ ./node_modules/ws/lib/receiver.js
@ ./node_modules/ws/index.js
@ ./node_modules/isomorphic-ws/node.js
....
@ ./index.js
WARNING in ./node_modules/ws/lib/validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in '/.../node_modules/ws/lib'
@ ./node_modules/ws/lib/validation.js 10:22-47
@ ./node_modules/ws/lib/receiver.js
@ ./node_modules/ws/index.js
@ ./node_modules/isomorphic-ws/node.js
....
@ ./index.js
It works fine when compiling for node.
Thanks,
Nicolas
According to the peerDependencies
documentation, NPM 7 will auto-install peerDependencies
if they aren't explicit dependencies of the application. This means that an in-browser application that depends on this library will end up downloading the ws
library even though it's not needed.
In my opinion, we should mark ws
as optional using peerDependenciesMeta, and document that node.js applications should make sure to include ws
as one of their dependencies. We'll probably also want a runtime check that the ws
library is present, and throw an Error
with a helpful message if it's not.
for now, an export interface looks like
for browser
import WebSocket from 'isomorphic-ws'
for NodeJS
import {WebSocket} from 'isomorphic-ws'
It will be nice to have the same interface for browser and NodeJS
It appears that support for this package has been discontinued. It's a pity because there are not enough actual edits to support ES modules.
It will be great if the work in this repository resumes.
Just in case, I made my package with some edits, you never know it will be useful to someone.
ws provides the named export "WebSocket".
Please export this in browser.js too.
Error: export 'WebSocket' (imported as 'WebSocket') was not found in 'isomorphic-ws' (possible exports: default)
FYI: It is now possible to create a WebSocket server in the browser with Direct Sockets in an Isolated Web App with TCPServerSocket
.
I created an HTTP server using TCPServerSocket
and am now working on using the same code for a WebSocket server, see https://gist.github.com/guest271314/73a50e9ebc6acaaff5d39f6fc7918ebf.
This is what I have so far for the WebSocket request handling and upgrade. Now I'm working on parsing and sending frames
const socket = new TCPServerSocket("0.0.0.0", {
localPort: 8080,
});
const {
readable: server,
localAddress,
localPort,
} = await socket.opened;
console.log({ server });
await server.pipeTo(
new WritableStream({
async write(connection) {
const {
readable: client,
writable,
remoteAddress,
remotePort,
} = await connection.opened;
console.log({ connection });
const writer = writable.getWriter();
console.log({
remoteAddress,
remotePort,
});
await client.pipeThrough(new TextDecoderStream()).pipeTo(
new WritableStream({
async write(request) {
console.log(request);
if (/^GET/.test(request) && /websocket/i.test(request)) {
const headers = getHeaders(request);
const key = headers.get("sec-websocket-key");
const accept = await digest(key);
await writer.write(encode("HTTP/1.1 101 Switching Protocols\r\n"));
await writer.write(encode("Upgrade: websocket\r\n"));
await writer.write(encode("Connection: Upgrade\r\n"));
await writer.write(encode(`Sec-WebSocket-Accept: ${accept}\r\n\r\n`));
await writer.close();
}
In the ws
npm package, major version 8 brings some significant divergence from browser web sockets, in that messages with text data now present the data as a Buffer instead of a string. See here websockets/ws@e173423
I'm wondering if isomorphic-ws will do something to reconcile this with the browser version that gives a string.
Could you please add a license to your project? While the package.json
states it is MIT licensed, adding a copy of the MIT license into the repository would be wonderful.
This code fails:
const WebSocket = require("isomorphic-ws");
const ws = new WebSocket("wss://echo.websocket.org/", {
origin: "https://websocket.org"
});
Firefox:
SyntaxError: An invalid or illegal string was specified
Chromium-based browser:
Uncaught DOMException: Failed to construct 'WebSocket': The subprotocol '[object Object]' is invalid.
at Object.76.isomorphic-ws (https://localhost:9966/index.js:33990:12)
at o (https://localhost:9966/index.js:1:265)
at r (https://localhost:9966/index.js:1:431)
at https://localhost:9966/index.js:1:460
Maybe it has something to do with using a development server which runs on node and makes isomorphic-ws fail to detect that it is eventually running inside a browser? Not sure.
Can you add the event listener functionality of ws and extend the WebSocket object? That way your code could really be isomorphic?
So, for instance, the on('message')
event instead of the onmessage
callback
This library is incorrectly exporting the type from ws
which has all the extra Node.js type information. Instead this lib should export the WebSocket API from the standard and shim any missing members that Node.js doesn't support on the standard.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.