Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/3p3r/websocket-redirect-shim
a shim (patch) to support WebSocket redirect requests in browsers
https://github.com/3p3r/websocket-redirect-shim
es5-javascript es6-javascript redirect shim socket-io websocket
Last synced: about 10 hours ago
JSON representation
a shim (patch) to support WebSocket redirect requests in browsers
- Host: GitHub
- URL: https://github.com/3p3r/websocket-redirect-shim
- Owner: 3p3r
- License: mit
- Created: 2022-01-17T18:29:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-17T04:32:46.000Z (almost 3 years ago)
- Last Synced: 2024-12-21T08:42:34.472Z (about 2 months ago)
- Topics: es5-javascript, es6-javascript, redirect, shim, socket-io, websocket
- Language: JavaScript
- Homepage:
- Size: 162 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket-redirect-shim
a shim (patch) to support WebSocket redirect requests in browsers
## prerequisite
this shim needs a server-side helper to resolve the eventual WebSocket address for it. the server side helper is also
written in NodeJS and requires the [serverless framework](https://www.serverless.com/) to deploy into AWS.deploy with:
```bash
serverless deploy
```example output:
```bash
$ serverless deploy
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Ensuring that deployment bucket exists
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service websocket-redirect-shim.zip file to S3 (9.32 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: websocket-redirect-shim
stage: dev
region: us-east-1
stack: websocket-redirect-shim-dev
resources: 12
api keys:
None
endpoints:
GET -
functions:
resolve: websocket-redirect-shim-dev-resolve
layers:
None
````` is where you'll find the shim's JavaScript and the same endpoint can resolve an address after redirects
for you if you pass it a `?url=` query string. Both HTTP and WS URL schemes are supported. You do not need to directly
work with the resolver. The patch internally handles that.this server helper is necessary due to the fact that currently it's impossible to detect AND follow a URL manually in
all major browsers. both the `fetch()` api and `XMLHttpRequest` object cannot do that and gymnastics with `iframe`s do
not interest me. in a browser redirects are handled internally and transparently and are not exposed to end user.## usage
Just include the patch somewhere before you create a `WebSocket` in a browser. As soon as you create the socket, a GET
request will be made to your provided websocket endpoint. If response comes back with a 3xx status, resolver will follow
it until status no longer sets to 3xx and your `WebSocket` instance will get connected to the last redirect in the chain## polyfilled vs lean
After you `npm install` in this repo, you get a polyfilled version (`websocket-redirect-shim.polyfill.min.js`) and a
lean version (`websocket-redirect-shim.min.js`). Use the lean version if you are sure about the browser environment you
need this patch in. Your environment needs to support these features:- Async/Await
- WebSocket
- Reflect
- Proxy## `window.WEBSOCKET_REDIRECT_RESOLVER`
The patch looks for the resolver endpoint through this variable. If you use the patch through the helper Lambda, the
patch script file will have this defined at its top.