first commit
This commit is contained in:
35
.github/workflows/npm-publish-github-packages.yml
vendored
Normal file
35
.github/workflows/npm-publish-github-packages.yml
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
||||||
|
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
|
||||||
|
|
||||||
|
name: Node.js Package
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [created]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: 16
|
||||||
|
- run: node index.js
|
||||||
|
|
||||||
|
publish-gpr:
|
||||||
|
needs: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
packages: write
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-node@v3
|
||||||
|
with:
|
||||||
|
node-version: 16
|
||||||
|
registry-url: https://npm.pkg.github.com/
|
||||||
|
- run: npm ci
|
||||||
|
- run: npm publish
|
||||||
|
env:
|
||||||
|
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
||||||
5
.idea/.gitignore
generated
vendored
Normal file
5
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
12
.idea/kermitpartybot.iml
generated
Normal file
12
.idea/kermitpartybot.iml
generated
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="WEB_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/kermitpartybot.iml" filepath="$PROJECT_DIR$/.idea/kermitpartybot.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
168
index.js
Normal file
168
index.js
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
const tmi = require("tmi.js");
|
||||||
|
const { get } = require("tmi.js/lib/utils");
|
||||||
|
|
||||||
|
const uwu = "/me ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⣿⣿⡆⠀⠀⢸⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⡇⠀⠀⣾⣿⡆⠀ ⠀⠀⠀⣿⣿⡇⠀⠀⢸⣿⢰⣿⡆⠀⣾⣿⡆⠀⣾⣷ ⣿⣿⡇⠀⠀⣿⣿⡇⠀ ⠀⠀⠀⣿⣿⡇⠀⠀⢸⣿⠘⣿⣿⣤⣿⣿⣿⣤⣿⡇⢻⣿⡇⠀⠀⣿⣿⡇⠀ ⠀⠀⠀⣿⣿⡇⠀⠀⢸⡿⠀⢹⣿⣿⣿⣿⣿⣿⣿⠁⢸⣿⣇⠀⢀⣿⣿⠇⠀ ⠀⠀⠀⠙⢿⣷⣶⣶⡿⠁⠀⠈⣿⣿⠟⠀⣿⣿⠇⠀⠈⠻⣿⣶⣾⡿⠋⠀⠀ "
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let options = {
|
||||||
|
options: {
|
||||||
|
debug: true
|
||||||
|
},
|
||||||
|
connection: {
|
||||||
|
reconnect: true,
|
||||||
|
secure: true
|
||||||
|
},
|
||||||
|
identity: {
|
||||||
|
username: "kermitpartybot",
|
||||||
|
password: "2z9dm32bsdt9zc9hpa800kcdp5dlyd"
|
||||||
|
},
|
||||||
|
channels: [ "kermitpartybot", "holipka_"]
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = new tmi.client(options);
|
||||||
|
|
||||||
|
// Connect the client to the server..
|
||||||
|
client.connect();
|
||||||
|
|
||||||
|
client.on('message', (channel, tags, message, self) => {
|
||||||
|
if(self) return;
|
||||||
|
|
||||||
|
|
||||||
|
const args = message.slice(1).split(' ');
|
||||||
|
const command = args.shift().toLowerCase();
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!los'){
|
||||||
|
const result = Math.floor(Math.random() * 3000);
|
||||||
|
client.say(channel, ` ${result}`);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(command == '!color') {
|
||||||
|
client.say(channel, `/color ${args.join(' ')}`);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message == "!kolory"){
|
||||||
|
client.say(channel, 'dostępne kolory: Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!uwu') {
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color blue')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color CadetBlue')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color Chocolate')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color Coral')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color DodgerBlue')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color Firebrick')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color GoldenRod')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color HotPink')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color OrangeRed')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color Red')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color SeaGreen')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color SpringGreen')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color YellowGreen')
|
||||||
|
client.say(channel, uwu)
|
||||||
|
client.say(channel, '/color green')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(message == 'kermitparty'){
|
||||||
|
client.say(channel, 'kermitParty ')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!tet'){
|
||||||
|
client.say(channel, 'OHAYO ')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!matblusz'){
|
||||||
|
client.say(channel, 'matbluszyk pipoblusz ')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!komendy'){
|
||||||
|
client.say(channel, 'dostępne komendy znajdziesz tu https://bot.holipka.repl.co')
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message == '!strona'){
|
||||||
|
client.say(channel, 'strona z komendami bota https://bot.holipka.repl.co')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(message == '!wiktorek'){
|
||||||
|
client.say(channel, 'AlienDance ')
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(message == "czesc"){
|
||||||
|
client.say(channel, "czesc donkBlush ")
|
||||||
|
}
|
||||||
|
if(message == "cześć"){
|
||||||
|
client.say(channel, "czesc donkBlush ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message == "siema"){
|
||||||
|
client.say(channel, "czesc donkBlush ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message == "hej"){
|
||||||
|
client.say(channel, "czesc donkBlush ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if(message == "elo"){
|
||||||
|
client.say(channel, "czesc donkBlush ")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
78
node_modules/.package-lock.json
generated
vendored
Normal file
78
node_modules/.package-lock.json
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
{
|
||||||
|
"name": "jschatbot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 2,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"node_modules/node-fetch": {
|
||||||
|
"version": "2.6.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||||
|
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"whatwg-url": "^5.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "4.x || >=6.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"encoding": "^0.1.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"encoding": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tmi.js": {
|
||||||
|
"version": "1.8.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/tmi.js/-/tmi.js-1.8.5.tgz",
|
||||||
|
"integrity": "sha512-A9qrydfe1e0VWM9MViVhhxVgvLpnk7pFShVUWePsSTtoi+A1X+Zjdoa7OJd7/YsgHXGj3GkNEvnWop/1WwZuew==",
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
|
"ws": "^8.2.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tr46": {
|
||||||
|
"version": "0.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||||
|
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
|
||||||
|
},
|
||||||
|
"node_modules/webidl-conversions": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||||
|
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
|
||||||
|
},
|
||||||
|
"node_modules/whatwg-url": {
|
||||||
|
"version": "5.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
|
||||||
|
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
|
||||||
|
"dependencies": {
|
||||||
|
"tr46": "~0.0.3",
|
||||||
|
"webidl-conversions": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "8.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.4.2.tgz",
|
||||||
|
"integrity": "sha512-Kbk4Nxyq7/ZWqr/tarI9yIt/+iNNFOjBXEWgTb4ydaNHBNGgvf2QHbS9fdfsndfjFlFwEd4Al+mw83YkaD10ZA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
node_modules/node-fetch/LICENSE.md
generated
vendored
Normal file
22
node_modules/node-fetch/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 David Frank
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
|
||||||
590
node_modules/node-fetch/README.md
generated
vendored
Normal file
590
node_modules/node-fetch/README.md
generated
vendored
Normal file
@@ -0,0 +1,590 @@
|
|||||||
|
node-fetch
|
||||||
|
==========
|
||||||
|
|
||||||
|
[![npm version][npm-image]][npm-url]
|
||||||
|
[![build status][travis-image]][travis-url]
|
||||||
|
[![coverage status][codecov-image]][codecov-url]
|
||||||
|
[![install size][install-size-image]][install-size-url]
|
||||||
|
[![Discord][discord-image]][discord-url]
|
||||||
|
|
||||||
|
A light-weight module that brings `window.fetch` to Node.js
|
||||||
|
|
||||||
|
(We are looking for [v2 maintainers and collaborators](https://github.com/bitinn/node-fetch/issues/567))
|
||||||
|
|
||||||
|
[![Backers][opencollective-image]][opencollective-url]
|
||||||
|
|
||||||
|
<!-- TOC -->
|
||||||
|
|
||||||
|
- [Motivation](#motivation)
|
||||||
|
- [Features](#features)
|
||||||
|
- [Difference from client-side fetch](#difference-from-client-side-fetch)
|
||||||
|
- [Installation](#installation)
|
||||||
|
- [Loading and configuring the module](#loading-and-configuring-the-module)
|
||||||
|
- [Common Usage](#common-usage)
|
||||||
|
- [Plain text or HTML](#plain-text-or-html)
|
||||||
|
- [JSON](#json)
|
||||||
|
- [Simple Post](#simple-post)
|
||||||
|
- [Post with JSON](#post-with-json)
|
||||||
|
- [Post with form parameters](#post-with-form-parameters)
|
||||||
|
- [Handling exceptions](#handling-exceptions)
|
||||||
|
- [Handling client and server errors](#handling-client-and-server-errors)
|
||||||
|
- [Advanced Usage](#advanced-usage)
|
||||||
|
- [Streams](#streams)
|
||||||
|
- [Buffer](#buffer)
|
||||||
|
- [Accessing Headers and other Meta data](#accessing-headers-and-other-meta-data)
|
||||||
|
- [Extract Set-Cookie Header](#extract-set-cookie-header)
|
||||||
|
- [Post data using a file stream](#post-data-using-a-file-stream)
|
||||||
|
- [Post with form-data (detect multipart)](#post-with-form-data-detect-multipart)
|
||||||
|
- [Request cancellation with AbortSignal](#request-cancellation-with-abortsignal)
|
||||||
|
- [API](#api)
|
||||||
|
- [fetch(url[, options])](#fetchurl-options)
|
||||||
|
- [Options](#options)
|
||||||
|
- [Class: Request](#class-request)
|
||||||
|
- [Class: Response](#class-response)
|
||||||
|
- [Class: Headers](#class-headers)
|
||||||
|
- [Interface: Body](#interface-body)
|
||||||
|
- [Class: FetchError](#class-fetcherror)
|
||||||
|
- [License](#license)
|
||||||
|
- [Acknowledgement](#acknowledgement)
|
||||||
|
|
||||||
|
<!-- /TOC -->
|
||||||
|
|
||||||
|
## Motivation
|
||||||
|
|
||||||
|
Instead of implementing `XMLHttpRequest` in Node.js to run browser-specific [Fetch polyfill](https://github.com/github/fetch), why not go from native `http` to `fetch` API directly? Hence, `node-fetch`, minimal code for a `window.fetch` compatible API on Node.js runtime.
|
||||||
|
|
||||||
|
See Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch) or Leonardo Quixada's [cross-fetch](https://github.com/lquixada/cross-fetch) for isomorphic usage (exports `node-fetch` for server-side, `whatwg-fetch` for client-side).
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Stay consistent with `window.fetch` API.
|
||||||
|
- Make conscious trade-off when following [WHATWG fetch spec][whatwg-fetch] and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known differences.
|
||||||
|
- Use native promise but allow substituting it with [insert your favorite promise library].
|
||||||
|
- Use native Node streams for body on both request and response.
|
||||||
|
- Decode content encoding (gzip/deflate) properly and convert string output (such as `res.text()` and `res.json()`) to UTF-8 automatically.
|
||||||
|
- Useful extensions such as timeout, redirect limit, response size limit, [explicit errors](ERROR-HANDLING.md) for troubleshooting.
|
||||||
|
|
||||||
|
## Difference from client-side fetch
|
||||||
|
|
||||||
|
- See [Known Differences](LIMITS.md) for details.
|
||||||
|
- If you happen to use a missing feature that `window.fetch` offers, feel free to open an issue.
|
||||||
|
- Pull requests are welcomed too!
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Current stable release (`2.x`)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ npm install node-fetch
|
||||||
|
```
|
||||||
|
|
||||||
|
## Loading and configuring the module
|
||||||
|
We suggest you load the module via `require` until the stabilization of ES modules in node:
|
||||||
|
```js
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are using a Promise library other than native, set it through `fetch.Promise`:
|
||||||
|
```js
|
||||||
|
const Bluebird = require('bluebird');
|
||||||
|
|
||||||
|
fetch.Promise = Bluebird;
|
||||||
|
```
|
||||||
|
|
||||||
|
## Common Usage
|
||||||
|
|
||||||
|
NOTE: The documentation below is up-to-date with `2.x` releases; see the [`1.x` readme](https://github.com/bitinn/node-fetch/blob/1.x/README.md), [changelog](https://github.com/bitinn/node-fetch/blob/1.x/CHANGELOG.md) and [2.x upgrade guide](UPGRADE-GUIDE.md) for the differences.
|
||||||
|
|
||||||
|
#### Plain text or HTML
|
||||||
|
```js
|
||||||
|
fetch('https://github.com/')
|
||||||
|
.then(res => res.text())
|
||||||
|
.then(body => console.log(body));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### JSON
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
fetch('https://api.github.com/users/github')
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Simple Post
|
||||||
|
```js
|
||||||
|
fetch('https://httpbin.org/post', { method: 'POST', body: 'a=1' })
|
||||||
|
.then(res => res.json()) // expecting a json response
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Post with JSON
|
||||||
|
|
||||||
|
```js
|
||||||
|
const body = { a: 1 };
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/post', {
|
||||||
|
method: 'post',
|
||||||
|
body: JSON.stringify(body),
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
})
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Post with form parameters
|
||||||
|
`URLSearchParams` is available in Node.js as of v7.5.0. See [official documentation](https://nodejs.org/api/url.html#url_class_urlsearchparams) for more usage methods.
|
||||||
|
|
||||||
|
NOTE: The `Content-Type` header is only set automatically to `x-www-form-urlencoded` when an instance of `URLSearchParams` is given as such:
|
||||||
|
|
||||||
|
```js
|
||||||
|
const { URLSearchParams } = require('url');
|
||||||
|
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('a', 1);
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/post', { method: 'POST', body: params })
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Handling exceptions
|
||||||
|
NOTE: 3xx-5xx responses are *NOT* exceptions and should be handled in `then()`; see the next section for more information.
|
||||||
|
|
||||||
|
Adding a catch to the fetch promise chain will catch *all* exceptions, such as errors originating from node core libraries, network errors and operational errors, which are instances of FetchError. See the [error handling document](ERROR-HANDLING.md) for more details.
|
||||||
|
|
||||||
|
```js
|
||||||
|
fetch('https://domain.invalid/')
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Handling client and server errors
|
||||||
|
It is common to create a helper function to check that the response contains no client (4xx) or server (5xx) error responses:
|
||||||
|
|
||||||
|
```js
|
||||||
|
function checkStatus(res) {
|
||||||
|
if (res.ok) { // res.status >= 200 && res.status < 300
|
||||||
|
return res;
|
||||||
|
} else {
|
||||||
|
throw MyCustomError(res.statusText);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/status/400')
|
||||||
|
.then(checkStatus)
|
||||||
|
.then(res => console.log('will not get here...'))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Advanced Usage
|
||||||
|
|
||||||
|
#### Streams
|
||||||
|
The "Node.js way" is to use streams when possible:
|
||||||
|
|
||||||
|
```js
|
||||||
|
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||||
|
.then(res => {
|
||||||
|
const dest = fs.createWriteStream('./octocat.png');
|
||||||
|
res.body.pipe(dest);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Buffer
|
||||||
|
If you prefer to cache binary data in full, use buffer(). (NOTE: `buffer()` is a `node-fetch`-only API)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const fileType = require('file-type');
|
||||||
|
|
||||||
|
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
|
||||||
|
.then(res => res.buffer())
|
||||||
|
.then(buffer => fileType(buffer))
|
||||||
|
.then(type => { /* ... */ });
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Accessing Headers and other Meta data
|
||||||
|
```js
|
||||||
|
fetch('https://github.com/')
|
||||||
|
.then(res => {
|
||||||
|
console.log(res.ok);
|
||||||
|
console.log(res.status);
|
||||||
|
console.log(res.statusText);
|
||||||
|
console.log(res.headers.raw());
|
||||||
|
console.log(res.headers.get('content-type'));
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Extract Set-Cookie Header
|
||||||
|
|
||||||
|
Unlike browsers, you can access raw `Set-Cookie` headers manually using `Headers.raw()`. This is a `node-fetch` only API.
|
||||||
|
|
||||||
|
```js
|
||||||
|
fetch(url).then(res => {
|
||||||
|
// returns an array of values, instead of a string of comma-separated values
|
||||||
|
console.log(res.headers.raw()['set-cookie']);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Post data using a file stream
|
||||||
|
|
||||||
|
```js
|
||||||
|
const { createReadStream } = require('fs');
|
||||||
|
|
||||||
|
const stream = createReadStream('input.txt');
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/post', { method: 'POST', body: stream })
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Post with form-data (detect multipart)
|
||||||
|
|
||||||
|
```js
|
||||||
|
const FormData = require('form-data');
|
||||||
|
|
||||||
|
const form = new FormData();
|
||||||
|
form.append('a', 1);
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/post', { method: 'POST', body: form })
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
|
||||||
|
// OR, using custom headers
|
||||||
|
// NOTE: getHeaders() is non-standard API
|
||||||
|
|
||||||
|
const form = new FormData();
|
||||||
|
form.append('a', 1);
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
method: 'POST',
|
||||||
|
body: form,
|
||||||
|
headers: form.getHeaders()
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch('https://httpbin.org/post', options)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(json => console.log(json));
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Request cancellation with AbortSignal
|
||||||
|
|
||||||
|
> NOTE: You may cancel streamed requests only on Node >= v8.0.0
|
||||||
|
|
||||||
|
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).
|
||||||
|
|
||||||
|
An example of timing out a request after 150ms could be achieved as the following:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import AbortController from 'abort-controller';
|
||||||
|
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(
|
||||||
|
() => { controller.abort(); },
|
||||||
|
150,
|
||||||
|
);
|
||||||
|
|
||||||
|
fetch(url, { signal: controller.signal })
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(
|
||||||
|
data => {
|
||||||
|
useData(data)
|
||||||
|
},
|
||||||
|
err => {
|
||||||
|
if (err.name === 'AbortError') {
|
||||||
|
// request was aborted
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.finally(() => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
|
||||||
|
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### fetch(url[, options])
|
||||||
|
|
||||||
|
- `url` A string representing the URL for fetching
|
||||||
|
- `options` [Options](#fetch-options) for the HTTP(S) request
|
||||||
|
- Returns: <code>Promise<[Response](#class-response)></code>
|
||||||
|
|
||||||
|
Perform an HTTP(S) fetch.
|
||||||
|
|
||||||
|
`url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected `Promise`.
|
||||||
|
|
||||||
|
<a id="fetch-options"></a>
|
||||||
|
### Options
|
||||||
|
|
||||||
|
The default values are shown after each option key.
|
||||||
|
|
||||||
|
```js
|
||||||
|
{
|
||||||
|
// These properties are part of the Fetch Standard
|
||||||
|
method: 'GET',
|
||||||
|
headers: {}, // request headers. format is the identical to that accepted by the Headers constructor (see below)
|
||||||
|
body: null, // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
|
||||||
|
redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect
|
||||||
|
signal: null, // pass an instance of AbortSignal to optionally abort requests
|
||||||
|
|
||||||
|
// The following properties are node-fetch extensions
|
||||||
|
follow: 20, // maximum redirect count. 0 to not follow redirect
|
||||||
|
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
|
||||||
|
compress: true, // support gzip/deflate content encoding. false to disable
|
||||||
|
size: 0, // maximum response body size in bytes. 0 to disable
|
||||||
|
agent: null // http(s).Agent instance or function that returns an instance (see below)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Default Headers
|
||||||
|
|
||||||
|
If no values are set, the following request headers will be sent automatically:
|
||||||
|
|
||||||
|
Header | Value
|
||||||
|
------------------- | --------------------------------------------------------
|
||||||
|
`Accept-Encoding` | `gzip,deflate` _(when `options.compress === true`)_
|
||||||
|
`Accept` | `*/*`
|
||||||
|
`Connection` | `close` _(when no `options.agent` is present)_
|
||||||
|
`Content-Length` | _(automatically calculated, if possible)_
|
||||||
|
`Transfer-Encoding` | `chunked` _(when `req.body` is a stream)_
|
||||||
|
`User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
|
||||||
|
|
||||||
|
Note: when `body` is a `Stream`, `Content-Length` is not set automatically.
|
||||||
|
|
||||||
|
##### Custom Agent
|
||||||
|
|
||||||
|
The `agent` option allows you to specify networking related options which are out of the scope of Fetch, including and not limited to the following:
|
||||||
|
|
||||||
|
- Support self-signed certificate
|
||||||
|
- Use only IPv4 or IPv6
|
||||||
|
- Custom DNS Lookup
|
||||||
|
|
||||||
|
See [`http.Agent`](https://nodejs.org/api/http.html#http_new_agent_options) for more information.
|
||||||
|
|
||||||
|
In addition, the `agent` option accepts a function that returns `http`(s)`.Agent` instance given current [URL](https://nodejs.org/api/url.html), this is useful during a redirection chain across HTTP and HTTPS protocol.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const httpAgent = new http.Agent({
|
||||||
|
keepAlive: true
|
||||||
|
});
|
||||||
|
const httpsAgent = new https.Agent({
|
||||||
|
keepAlive: true
|
||||||
|
});
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
agent: function (_parsedURL) {
|
||||||
|
if (_parsedURL.protocol == 'http:') {
|
||||||
|
return httpAgent;
|
||||||
|
} else {
|
||||||
|
return httpsAgent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
<a id="class-request"></a>
|
||||||
|
### Class: Request
|
||||||
|
|
||||||
|
An HTTP(S) request containing information about URL, method, headers, and the body. This class implements the [Body](#iface-body) interface.
|
||||||
|
|
||||||
|
Due to the nature of Node.js, the following properties are not implemented at this moment:
|
||||||
|
|
||||||
|
- `type`
|
||||||
|
- `destination`
|
||||||
|
- `referrer`
|
||||||
|
- `referrerPolicy`
|
||||||
|
- `mode`
|
||||||
|
- `credentials`
|
||||||
|
- `cache`
|
||||||
|
- `integrity`
|
||||||
|
- `keepalive`
|
||||||
|
|
||||||
|
The following node-fetch extension properties are provided:
|
||||||
|
|
||||||
|
- `follow`
|
||||||
|
- `compress`
|
||||||
|
- `counter`
|
||||||
|
- `agent`
|
||||||
|
|
||||||
|
See [options](#fetch-options) for exact meaning of these extensions.
|
||||||
|
|
||||||
|
#### new Request(input[, options])
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
- `input` A string representing a URL, or another `Request` (which will be cloned)
|
||||||
|
- `options` [Options][#fetch-options] for the HTTP(S) request
|
||||||
|
|
||||||
|
Constructs a new `Request` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request).
|
||||||
|
|
||||||
|
In most cases, directly `fetch(url, options)` is simpler than creating a `Request` object.
|
||||||
|
|
||||||
|
<a id="class-response"></a>
|
||||||
|
### Class: Response
|
||||||
|
|
||||||
|
An HTTP(S) response. This class implements the [Body](#iface-body) interface.
|
||||||
|
|
||||||
|
The following properties are not implemented in node-fetch at this moment:
|
||||||
|
|
||||||
|
- `Response.error()`
|
||||||
|
- `Response.redirect()`
|
||||||
|
- `type`
|
||||||
|
- `trailer`
|
||||||
|
|
||||||
|
#### new Response([body[, options]])
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
- `body` A `String` or [`Readable` stream][node-readable]
|
||||||
|
- `options` A [`ResponseInit`][response-init] options dictionary
|
||||||
|
|
||||||
|
Constructs a new `Response` object. The constructor is identical to that in the [browser](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response).
|
||||||
|
|
||||||
|
Because Node.js does not implement service workers (for which this class was designed), one rarely has to construct a `Response` directly.
|
||||||
|
|
||||||
|
#### response.ok
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
Convenience property representing if the request ended normally. Will evaluate to true if the response status was greater than or equal to 200 but smaller than 300.
|
||||||
|
|
||||||
|
#### response.redirected
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
Convenience property representing if the request has been redirected at least once. Will evaluate to true if the internal redirect counter is greater than 0.
|
||||||
|
|
||||||
|
<a id="class-headers"></a>
|
||||||
|
### Class: Headers
|
||||||
|
|
||||||
|
This class allows manipulating and iterating over a set of HTTP headers. All methods specified in the [Fetch Standard][whatwg-fetch] are implemented.
|
||||||
|
|
||||||
|
#### new Headers([init])
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
- `init` Optional argument to pre-fill the `Headers` object
|
||||||
|
|
||||||
|
Construct a new `Headers` object. `init` can be either `null`, a `Headers` object, an key-value map object or any iterable object.
|
||||||
|
|
||||||
|
```js
|
||||||
|
// Example adapted from https://fetch.spec.whatwg.org/#example-headers-class
|
||||||
|
|
||||||
|
const meta = {
|
||||||
|
'Content-Type': 'text/xml',
|
||||||
|
'Breaking-Bad': '<3'
|
||||||
|
};
|
||||||
|
const headers = new Headers(meta);
|
||||||
|
|
||||||
|
// The above is equivalent to
|
||||||
|
const meta = [
|
||||||
|
[ 'Content-Type', 'text/xml' ],
|
||||||
|
[ 'Breaking-Bad', '<3' ]
|
||||||
|
];
|
||||||
|
const headers = new Headers(meta);
|
||||||
|
|
||||||
|
// You can in fact use any iterable objects, like a Map or even another Headers
|
||||||
|
const meta = new Map();
|
||||||
|
meta.set('Content-Type', 'text/xml');
|
||||||
|
meta.set('Breaking-Bad', '<3');
|
||||||
|
const headers = new Headers(meta);
|
||||||
|
const copyOfHeaders = new Headers(headers);
|
||||||
|
```
|
||||||
|
|
||||||
|
<a id="iface-body"></a>
|
||||||
|
### Interface: Body
|
||||||
|
|
||||||
|
`Body` is an abstract interface with methods that are applicable to both `Request` and `Response` classes.
|
||||||
|
|
||||||
|
The following methods are not yet implemented in node-fetch at this moment:
|
||||||
|
|
||||||
|
- `formData()`
|
||||||
|
|
||||||
|
#### body.body
|
||||||
|
|
||||||
|
<small>*(deviation from spec)*</small>
|
||||||
|
|
||||||
|
* Node.js [`Readable` stream][node-readable]
|
||||||
|
|
||||||
|
Data are encapsulated in the `Body` object. Note that while the [Fetch Standard][whatwg-fetch] requires the property to always be a WHATWG `ReadableStream`, in node-fetch it is a Node.js [`Readable` stream][node-readable].
|
||||||
|
|
||||||
|
#### body.bodyUsed
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
* `Boolean`
|
||||||
|
|
||||||
|
A boolean property for if this body has been consumed. Per the specs, a consumed body cannot be used again.
|
||||||
|
|
||||||
|
#### body.arrayBuffer()
|
||||||
|
#### body.blob()
|
||||||
|
#### body.json()
|
||||||
|
#### body.text()
|
||||||
|
|
||||||
|
<small>*(spec-compliant)*</small>
|
||||||
|
|
||||||
|
* Returns: <code>Promise</code>
|
||||||
|
|
||||||
|
Consume the body and return a promise that will resolve to one of these formats.
|
||||||
|
|
||||||
|
#### body.buffer()
|
||||||
|
|
||||||
|
<small>*(node-fetch extension)*</small>
|
||||||
|
|
||||||
|
* Returns: <code>Promise<Buffer></code>
|
||||||
|
|
||||||
|
Consume the body and return a promise that will resolve to a Buffer.
|
||||||
|
|
||||||
|
#### body.textConverted()
|
||||||
|
|
||||||
|
<small>*(node-fetch extension)*</small>
|
||||||
|
|
||||||
|
* Returns: <code>Promise<String></code>
|
||||||
|
|
||||||
|
Identical to `body.text()`, except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8 if possible.
|
||||||
|
|
||||||
|
(This API requires an optional dependency of the npm package [encoding](https://www.npmjs.com/package/encoding), which you need to install manually. `webpack` users may see [a warning message](https://github.com/bitinn/node-fetch/issues/412#issuecomment-379007792) due to this optional dependency.)
|
||||||
|
|
||||||
|
<a id="class-fetcherror"></a>
|
||||||
|
### Class: FetchError
|
||||||
|
|
||||||
|
<small>*(node-fetch extension)*</small>
|
||||||
|
|
||||||
|
An operational error in the fetching process. See [ERROR-HANDLING.md][] for more info.
|
||||||
|
|
||||||
|
<a id="class-aborterror"></a>
|
||||||
|
### Class: AbortError
|
||||||
|
|
||||||
|
<small>*(node-fetch extension)*</small>
|
||||||
|
|
||||||
|
An Error thrown when the request is aborted in response to an `AbortSignal`'s `abort` event. It has a `name` property of `AbortError`. See [ERROR-HANDLING.MD][] for more info.
|
||||||
|
|
||||||
|
## Acknowledgement
|
||||||
|
|
||||||
|
Thanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.
|
||||||
|
|
||||||
|
`node-fetch` v1 was maintained by [@bitinn](https://github.com/bitinn); v2 was maintained by [@TimothyGu](https://github.com/timothygu), [@bitinn](https://github.com/bitinn) and [@jimmywarting](https://github.com/jimmywarting); v2 readme is written by [@jkantr](https://github.com/jkantr).
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
|
|
||||||
|
[npm-image]: https://flat.badgen.net/npm/v/node-fetch
|
||||||
|
[npm-url]: https://www.npmjs.com/package/node-fetch
|
||||||
|
[travis-image]: https://flat.badgen.net/travis/bitinn/node-fetch
|
||||||
|
[travis-url]: https://travis-ci.org/bitinn/node-fetch
|
||||||
|
[codecov-image]: https://flat.badgen.net/codecov/c/github/bitinn/node-fetch/master
|
||||||
|
[codecov-url]: https://codecov.io/gh/bitinn/node-fetch
|
||||||
|
[install-size-image]: https://flat.badgen.net/packagephobia/install/node-fetch
|
||||||
|
[install-size-url]: https://packagephobia.now.sh/result?p=node-fetch
|
||||||
|
[discord-image]: https://img.shields.io/discord/619915844268326952?color=%237289DA&label=Discord&style=flat-square
|
||||||
|
[discord-url]: https://discord.gg/Zxbndcm
|
||||||
|
[opencollective-image]: https://opencollective.com/node-fetch/backers.svg
|
||||||
|
[opencollective-url]: https://opencollective.com/node-fetch
|
||||||
|
[whatwg-fetch]: https://fetch.spec.whatwg.org/
|
||||||
|
[response-init]: https://fetch.spec.whatwg.org/#responseinit
|
||||||
|
[node-readable]: https://nodejs.org/api/stream.html#stream_readable_streams
|
||||||
|
[mdn-headers]: https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
||||||
|
[LIMITS.md]: https://github.com/bitinn/node-fetch/blob/master/LIMITS.md
|
||||||
|
[ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md
|
||||||
|
[UPGRADE-GUIDE.md]: https://github.com/bitinn/node-fetch/blob/master/UPGRADE-GUIDE.md
|
||||||
25
node_modules/node-fetch/browser.js
generated
vendored
Normal file
25
node_modules/node-fetch/browser.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
// ref: https://github.com/tc39/proposal-global
|
||||||
|
var getGlobal = function () {
|
||||||
|
// the only reliable means to get the global object is
|
||||||
|
// `Function('return this')()`
|
||||||
|
// However, this causes CSP violations in Chrome apps.
|
||||||
|
if (typeof self !== 'undefined') { return self; }
|
||||||
|
if (typeof window !== 'undefined') { return window; }
|
||||||
|
if (typeof global !== 'undefined') { return global; }
|
||||||
|
throw new Error('unable to locate global object');
|
||||||
|
}
|
||||||
|
|
||||||
|
var global = getGlobal();
|
||||||
|
|
||||||
|
module.exports = exports = global.fetch;
|
||||||
|
|
||||||
|
// Needed for TypeScript and Webpack.
|
||||||
|
if (global.fetch) {
|
||||||
|
exports.default = global.fetch.bind(global);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.Headers = global.Headers;
|
||||||
|
exports.Request = global.Request;
|
||||||
|
exports.Response = global.Response;
|
||||||
1688
node_modules/node-fetch/lib/index.es.js
generated
vendored
Normal file
1688
node_modules/node-fetch/lib/index.es.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1697
node_modules/node-fetch/lib/index.js
generated
vendored
Normal file
1697
node_modules/node-fetch/lib/index.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1686
node_modules/node-fetch/lib/index.mjs
generated
vendored
Normal file
1686
node_modules/node-fetch/lib/index.mjs
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
76
node_modules/node-fetch/package.json
generated
vendored
Normal file
76
node_modules/node-fetch/package.json
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
{
|
||||||
|
"name": "node-fetch",
|
||||||
|
"version": "2.6.7",
|
||||||
|
"description": "A light-weight module that brings window.fetch to node.js",
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"browser": "./browser.js",
|
||||||
|
"module": "lib/index.mjs",
|
||||||
|
"files": [
|
||||||
|
"lib/index.js",
|
||||||
|
"lib/index.mjs",
|
||||||
|
"lib/index.es.js",
|
||||||
|
"browser.js"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": "4.x || >=6.0.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "cross-env BABEL_ENV=rollup rollup -c",
|
||||||
|
"prepare": "npm run build",
|
||||||
|
"test": "cross-env BABEL_ENV=test mocha --require babel-register --throw-deprecation test/test.js",
|
||||||
|
"report": "cross-env BABEL_ENV=coverage nyc --reporter lcov --reporter text mocha -R spec test/test.js",
|
||||||
|
"coverage": "cross-env BABEL_ENV=coverage nyc --reporter json --reporter text mocha -R spec test/test.js && codecov -f coverage/coverage-final.json"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/bitinn/node-fetch.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"fetch",
|
||||||
|
"http",
|
||||||
|
"promise"
|
||||||
|
],
|
||||||
|
"author": "David Frank",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/bitinn/node-fetch/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/bitinn/node-fetch",
|
||||||
|
"dependencies": {
|
||||||
|
"whatwg-url": "^5.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"encoding": "^0.1.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"encoding": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@ungap/url-search-params": "^0.1.2",
|
||||||
|
"abort-controller": "^1.1.0",
|
||||||
|
"abortcontroller-polyfill": "^1.3.0",
|
||||||
|
"babel-core": "^6.26.3",
|
||||||
|
"babel-plugin-istanbul": "^4.1.6",
|
||||||
|
"babel-preset-env": "^1.6.1",
|
||||||
|
"babel-register": "^6.16.3",
|
||||||
|
"chai": "^3.5.0",
|
||||||
|
"chai-as-promised": "^7.1.1",
|
||||||
|
"chai-iterator": "^1.1.1",
|
||||||
|
"chai-string": "~1.3.0",
|
||||||
|
"codecov": "3.3.0",
|
||||||
|
"cross-env": "^5.2.0",
|
||||||
|
"form-data": "^2.3.3",
|
||||||
|
"is-builtin-module": "^1.0.0",
|
||||||
|
"mocha": "^5.0.0",
|
||||||
|
"nyc": "11.9.0",
|
||||||
|
"parted": "^0.1.1",
|
||||||
|
"promise": "^8.0.3",
|
||||||
|
"resumer": "0.0.0",
|
||||||
|
"rollup": "^0.63.4",
|
||||||
|
"rollup-plugin-babel": "^3.0.7",
|
||||||
|
"string-to-arraybuffer": "^1.0.2",
|
||||||
|
"teeny-request": "3.7.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
21
node_modules/tmi.js/LICENSE
generated
vendored
Normal file
21
node_modules/tmi.js/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2019 tmi.js contributors
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
88
node_modules/tmi.js/README.md
generated
vendored
Normal file
88
node_modules/tmi.js/README.md
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
# tmi.js
|
||||||
|
|
||||||
|

|
||||||
|
[](https://www.npmjs.org/package/tmi.js)
|
||||||
|
[](https://www.npmjs.org/package/tmi.js)
|
||||||
|
[](https://github.com/tmijs/tmi.js/issues)
|
||||||
|
[](https://www.npmjs.org/package/tmi.js)
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
[Website](https://tmijs.com/) |
|
||||||
|
[Documentation currently at tmijs/docs](https://github.com/tmijs/docs/tree/gh-pages/_posts) |
|
||||||
|
[Changelog on the release page](https://github.com/tmijs/tmi.js/releases)
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
### Node
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ npm i tmi.js
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
const tmi = require('tmi.js');
|
||||||
|
const client = new tmi.Client({
|
||||||
|
options: { debug: true },
|
||||||
|
identity: {
|
||||||
|
username: 'bot_name',
|
||||||
|
password: 'oauth:my_bot_token'
|
||||||
|
},
|
||||||
|
channels: [ 'my_channel' ]
|
||||||
|
});
|
||||||
|
client.connect().catch(console.error);
|
||||||
|
client.on('message', (channel, tags, message, self) => {
|
||||||
|
if(self) return;
|
||||||
|
if(message.toLowerCase() === '!hello') {
|
||||||
|
client.say(channel, `@${tags.username}, heya!`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Browser
|
||||||
|
|
||||||
|
Available as "`tmi`" on `window`.
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script src="/scripts/tmi.min.js"></script>
|
||||||
|
```
|
||||||
|
```html
|
||||||
|
<script>
|
||||||
|
const client = new tmi.Client({ /* ... */ });
|
||||||
|
client.connect().catch(console.error);
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Prebuilt Browser Releases
|
||||||
|
|
||||||
|
[Release page](https://github.com/tmijs/tmi.js/releases)
|
||||||
|
|
||||||
|
#### Build Yourself
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ git clone https://github.com/tmijs/tmi.js.git
|
||||||
|
$ npm install
|
||||||
|
$ npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
### Type Definitions
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ npm i -D @types/tmi.js
|
||||||
|
```
|
||||||
|
|
||||||
|
## Community
|
||||||
|
|
||||||
|
- Follow [@AlcaMagic on Twitter](https://twitter.com/AlcaMagic), [Alca on Twitch](https://twitch.tv/alca).
|
||||||
|
- Follow [@Schmoopiie on Twitter](https://twitter.com/Schmoopiie).
|
||||||
|
- Found a bug: [submit an issue.](https://github.com/tmijs/tmi.js/issues/new)
|
||||||
|
- Discussion and help about tmi.js: [Twitch API Discord Server](https://discord.gg/8NXaEyV)
|
||||||
|
- For everything else: [Official TwitchDev Discord Server](https://link.twitch.tv/devchat)
|
||||||
|
|
||||||
|
## Contributors
|
||||||
|
|
||||||
|
Thanks to all of the tmi.js [contributors](https://github.com/tmijs/tmi.js/graphs/contributors)!
|
||||||
|
|
||||||
|
## Contributing guidelines
|
||||||
|
|
||||||
|
Please review the [guidelines for contributing](https://github.com/tmijs/tmi.js/blob/master/CONTRIBUTING.md) of the [tmi.js repository](https://github.com/tmijs/tmi.js). We reserve the right to refuse a Pull Request if it does not meet the requirements.
|
||||||
5
node_modules/tmi.js/index.js
generated
vendored
Normal file
5
node_modules/tmi.js/index.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
const client = require('./lib/client');
|
||||||
|
module.exports = {
|
||||||
|
client,
|
||||||
|
Client: client
|
||||||
|
};
|
||||||
66
node_modules/tmi.js/lib/api.js
generated
vendored
Normal file
66
node_modules/tmi.js/lib/api.js
generated
vendored
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
const fetch = require('node-fetch');
|
||||||
|
const _ = require('./utils');
|
||||||
|
|
||||||
|
module.exports = function api(options, callback) {
|
||||||
|
// Set the url to options.uri or options.url..
|
||||||
|
let url = options.url !== undefined ? options.url : options.uri;
|
||||||
|
|
||||||
|
// Make sure it is a valid url..
|
||||||
|
if(!_.isURL(url)) {
|
||||||
|
url = `https://api.twitch.tv/kraken${url[0] === '/' ? url : `/${url}`}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are inside a Node application, so we can use the node-fetch module..
|
||||||
|
if(_.isNode()) {
|
||||||
|
const opts = Object.assign({ method: 'GET', json: true }, options);
|
||||||
|
if(opts.qs) {
|
||||||
|
const qs = new URLSearchParams(opts.qs);
|
||||||
|
url += `?${qs}`;
|
||||||
|
}
|
||||||
|
/** @type {import('node-fetch').RequestInit} */
|
||||||
|
const fetchOptions = {};
|
||||||
|
if('fetchAgent' in this.opts.connection) {
|
||||||
|
fetchOptions.agent = this.opts.connection.fetchAgent;
|
||||||
|
}
|
||||||
|
/** @type {ReturnType<import('node-fetch')['default']>} */
|
||||||
|
const fetchPromise = fetch(url, {
|
||||||
|
...fetchOptions,
|
||||||
|
method: opts.method,
|
||||||
|
headers: opts.headers,
|
||||||
|
body: opts.body
|
||||||
|
});
|
||||||
|
let response = {};
|
||||||
|
fetchPromise.then(res => {
|
||||||
|
response = { statusCode: res.status, headers: res.headers };
|
||||||
|
return opts.json ? res.json() : res.text();
|
||||||
|
})
|
||||||
|
.then(
|
||||||
|
data => callback(null, response, data),
|
||||||
|
err => callback(err, response, null)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// Web application, extension, React Native etc.
|
||||||
|
else {
|
||||||
|
const opts = Object.assign({ method: 'GET', headers: {} }, options, { url });
|
||||||
|
// prepare request
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
xhr.open(opts.method, opts.url, true);
|
||||||
|
for(const name in opts.headers) {
|
||||||
|
xhr.setRequestHeader(name, opts.headers[name]);
|
||||||
|
}
|
||||||
|
xhr.responseType = 'json';
|
||||||
|
// set request handler
|
||||||
|
xhr.addEventListener('load', _ev => {
|
||||||
|
if(xhr.readyState === 4) {
|
||||||
|
if(xhr.status !== 200) {
|
||||||
|
callback(xhr.status, null, null);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
callback(null, null, xhr.response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// submit
|
||||||
|
xhr.send();
|
||||||
|
}
|
||||||
|
};
|
||||||
1519
node_modules/tmi.js/lib/client.js
generated
vendored
Normal file
1519
node_modules/tmi.js/lib/client.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
542
node_modules/tmi.js/lib/commands.js
generated
vendored
Normal file
542
node_modules/tmi.js/lib/commands.js
generated
vendored
Normal file
@@ -0,0 +1,542 @@
|
|||||||
|
const _ = require('./utils');
|
||||||
|
|
||||||
|
// Enable followers-only mode on a channel..
|
||||||
|
function followersonly(channel, minutes) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
minutes = _.get(minutes, 30);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/followers ${minutes}`, (resolve, reject) => {
|
||||||
|
// Received _promiseFollowers event, resolve or reject..
|
||||||
|
this.once('_promiseFollowers', err => {
|
||||||
|
if(!err) { resolve([ channel, ~~minutes ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable followers-only mode on a channel..
|
||||||
|
function followersonlyoff(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/followersoff', (resolve, reject) => {
|
||||||
|
// Received _promiseFollowersoff event, resolve or reject..
|
||||||
|
this.once('_promiseFollowersoff', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Leave a channel..
|
||||||
|
function part(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, null, `PART ${channel}`, (resolve, reject) => {
|
||||||
|
// Received _promisePart event, resolve or reject..
|
||||||
|
this.once('_promisePart', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable R9KBeta mode on a channel..
|
||||||
|
function r9kbeta(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/r9kbeta', (resolve, reject) => {
|
||||||
|
// Received _promiseR9kbeta event, resolve or reject..
|
||||||
|
this.once('_promiseR9kbeta', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable R9KBeta mode on a channel..
|
||||||
|
function r9kbetaoff(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/r9kbetaoff', (resolve, reject) => {
|
||||||
|
// Received _promiseR9kbetaoff event, resolve or reject..
|
||||||
|
this.once('_promiseR9kbetaoff', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable slow mode on a channel..
|
||||||
|
function slow(channel, seconds) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
seconds = _.get(seconds, 300);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/slow ${seconds}`, (resolve, reject) => {
|
||||||
|
// Received _promiseSlow event, resolve or reject..
|
||||||
|
this.once('_promiseSlow', err => {
|
||||||
|
if(!err) { resolve([ channel, ~~seconds ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable slow mode on a channel..
|
||||||
|
function slowoff(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/slowoff', (resolve, reject) => {
|
||||||
|
// Received _promiseSlowoff event, resolve or reject..
|
||||||
|
this.once('_promiseSlowoff', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// Send action message (/me <message>) on a channel..
|
||||||
|
action(channel, message) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
message = `\u0001ACTION ${message}\u0001`;
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendMessage(this._getPromiseDelay(), channel, message, (resolve, _reject) => {
|
||||||
|
// At this time, there is no possible way to detect if a message has been sent has been eaten
|
||||||
|
// by the server, so we can only resolve the Promise.
|
||||||
|
resolve([ channel, message ]);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Ban username on channel..
|
||||||
|
ban(channel, username, reason) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
reason = _.get(reason, '');
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/ban ${username} ${reason}`, (resolve, reject) => {
|
||||||
|
// Received _promiseBan event, resolve or reject..
|
||||||
|
this.once('_promiseBan', err => {
|
||||||
|
if(!err) { resolve([ channel, username, reason ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Clear all messages on a channel..
|
||||||
|
clear(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/clear', (resolve, reject) => {
|
||||||
|
// Received _promiseClear event, resolve or reject..
|
||||||
|
this.once('_promiseClear', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Change the color of your username..
|
||||||
|
color(channel, newColor) {
|
||||||
|
newColor = _.get(newColor, channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, '#tmijs', `/color ${newColor}`, (resolve, reject) => {
|
||||||
|
// Received _promiseColor event, resolve or reject..
|
||||||
|
this.once('_promiseColor', err => {
|
||||||
|
if(!err) { resolve([ newColor ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Run commercial on a channel for X seconds..
|
||||||
|
commercial(channel, seconds) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
seconds = _.get(seconds, 30);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/commercial ${seconds}`, (resolve, reject) => {
|
||||||
|
// Received _promiseCommercial event, resolve or reject..
|
||||||
|
this.once('_promiseCommercial', err => {
|
||||||
|
if(!err) { resolve([ channel, ~~seconds ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Delete a specific message on a channel
|
||||||
|
deletemessage(channel, messageUUID) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/delete ${messageUUID}`, (resolve, reject) => {
|
||||||
|
// Received _promiseDeletemessage event, resolve or reject..
|
||||||
|
this.once('_promiseDeletemessage', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Enable emote-only mode on a channel..
|
||||||
|
emoteonly(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/emoteonly', (resolve, reject) => {
|
||||||
|
// Received _promiseEmoteonly event, resolve or reject..
|
||||||
|
this.once('_promiseEmoteonly', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Disable emote-only mode on a channel..
|
||||||
|
emoteonlyoff(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/emoteonlyoff', (resolve, reject) => {
|
||||||
|
// Received _promiseEmoteonlyoff event, resolve or reject..
|
||||||
|
this.once('_promiseEmoteonlyoff', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Enable followers-only mode on a channel..
|
||||||
|
followersonly,
|
||||||
|
|
||||||
|
// Alias for followersonly()..
|
||||||
|
followersmode: followersonly,
|
||||||
|
|
||||||
|
// Disable followers-only mode on a channel..
|
||||||
|
followersonlyoff,
|
||||||
|
|
||||||
|
// Alias for followersonlyoff()..
|
||||||
|
followersmodeoff: followersonlyoff,
|
||||||
|
|
||||||
|
// Host a channel..
|
||||||
|
host(channel, target) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
target = _.username(target);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(2000, channel, `/host ${target}`, (resolve, reject) => {
|
||||||
|
// Received _promiseHost event, resolve or reject..
|
||||||
|
this.once('_promiseHost', (err, remaining) => {
|
||||||
|
if(!err) { resolve([ channel, target, ~~remaining ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Join a channel..
|
||||||
|
join(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server ..
|
||||||
|
return this._sendCommand(undefined, null, `JOIN ${channel}`, (resolve, reject) => {
|
||||||
|
const eventName = '_promiseJoin';
|
||||||
|
let hasFulfilled = false;
|
||||||
|
const listener = (err, joinedChannel) => {
|
||||||
|
if(channel === _.channel(joinedChannel)) {
|
||||||
|
// Received _promiseJoin event for the target channel, resolve or reject..
|
||||||
|
this.removeListener(eventName, listener);
|
||||||
|
hasFulfilled = true;
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.on(eventName, listener);
|
||||||
|
// Race the Promise against a delay..
|
||||||
|
const delay = this._getPromiseDelay();
|
||||||
|
_.promiseDelay(delay).then(() => {
|
||||||
|
if(!hasFulfilled) {
|
||||||
|
this.emit(eventName, 'No response from Twitch.', channel);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Mod username on channel..
|
||||||
|
mod(channel, username) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/mod ${username}`, (resolve, reject) => {
|
||||||
|
// Received _promiseMod event, resolve or reject..
|
||||||
|
this.once('_promiseMod', err => {
|
||||||
|
if(!err) { resolve([ channel, username ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Get list of mods on a channel..
|
||||||
|
mods(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/mods', (resolve, reject) => {
|
||||||
|
// Received _promiseMods event, resolve or reject..
|
||||||
|
this.once('_promiseMods', (err, mods) => {
|
||||||
|
if(!err) {
|
||||||
|
// Update the internal list of moderators..
|
||||||
|
mods.forEach(username => {
|
||||||
|
if(!this.moderators[channel]) { this.moderators[channel] = []; }
|
||||||
|
if(!this.moderators[channel].includes(username)) { this.moderators[channel].push(username); }
|
||||||
|
});
|
||||||
|
resolve(mods);
|
||||||
|
}
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Leave a channel..
|
||||||
|
part,
|
||||||
|
|
||||||
|
// Alias for part()..
|
||||||
|
leave: part,
|
||||||
|
|
||||||
|
// Send a ping to the server..
|
||||||
|
ping() {
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, null, 'PING', (resolve, _reject) => {
|
||||||
|
// Update the internal ping timeout check interval..
|
||||||
|
this.latency = new Date();
|
||||||
|
this.pingTimeout = setTimeout(() => {
|
||||||
|
if(this.ws !== null) {
|
||||||
|
this.wasCloseCalled = false;
|
||||||
|
this.log.error('Ping timeout.');
|
||||||
|
this.ws.close();
|
||||||
|
|
||||||
|
clearInterval(this.pingLoop);
|
||||||
|
clearTimeout(this.pingTimeout);
|
||||||
|
}
|
||||||
|
}, _.get(this.opts.connection.timeout, 9999));
|
||||||
|
|
||||||
|
// Received _promisePing event, resolve or reject..
|
||||||
|
this.once('_promisePing', latency => resolve([ parseFloat(latency) ]));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Enable R9KBeta mode on a channel..
|
||||||
|
r9kbeta,
|
||||||
|
|
||||||
|
// Alias for r9kbeta()..
|
||||||
|
r9kmode: r9kbeta,
|
||||||
|
|
||||||
|
// Disable R9KBeta mode on a channel..
|
||||||
|
r9kbetaoff,
|
||||||
|
|
||||||
|
// Alias for r9kbetaoff()..
|
||||||
|
r9kmodeoff: r9kbetaoff,
|
||||||
|
|
||||||
|
// Send a raw message to the server..
|
||||||
|
raw(message) {
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, null, message, (resolve, _reject) => {
|
||||||
|
resolve([ message ]);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Send a message on a channel..
|
||||||
|
say(channel, message) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
|
||||||
|
if((message.startsWith('.') && !message.startsWith('..')) || message.startsWith('/') || message.startsWith('\\')) {
|
||||||
|
// Check if the message is an action message..
|
||||||
|
if(message.substr(1, 3) === 'me ') {
|
||||||
|
return this.action(channel, message.substr(4));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, message, (resolve, _reject) => {
|
||||||
|
// At this time, there is no possible way to detect if a message has been sent has been eaten
|
||||||
|
// by the server, so we can only resolve the Promise.
|
||||||
|
resolve([ channel, message ]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendMessage(this._getPromiseDelay(), channel, message, (resolve, _reject) => {
|
||||||
|
// At this time, there is no possible way to detect if a message has been sent has been eaten
|
||||||
|
// by the server, so we can only resolve the Promise.
|
||||||
|
resolve([ channel, message ]);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Enable slow mode on a channel..
|
||||||
|
slow,
|
||||||
|
|
||||||
|
// Alias for slow()..
|
||||||
|
slowmode: slow,
|
||||||
|
|
||||||
|
// Disable slow mode on a channel..
|
||||||
|
slowoff,
|
||||||
|
|
||||||
|
// Alias for slowoff()..
|
||||||
|
slowmodeoff: slowoff,
|
||||||
|
|
||||||
|
// Enable subscribers mode on a channel..
|
||||||
|
subscribers(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/subscribers', (resolve, reject) => {
|
||||||
|
// Received _promiseSubscribers event, resolve or reject..
|
||||||
|
this.once('_promiseSubscribers', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Disable subscribers mode on a channel..
|
||||||
|
subscribersoff(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/subscribersoff', (resolve, reject) => {
|
||||||
|
// Received _promiseSubscribersoff event, resolve or reject..
|
||||||
|
this.once('_promiseSubscribersoff', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Timeout username on channel for X seconds..
|
||||||
|
timeout(channel, username, seconds, reason) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
|
||||||
|
if(seconds !== null && !_.isInteger(seconds)) {
|
||||||
|
reason = seconds;
|
||||||
|
seconds = 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
seconds = _.get(seconds, 300);
|
||||||
|
reason = _.get(reason, '');
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/timeout ${username} ${seconds} ${reason}`, (resolve, reject) => {
|
||||||
|
// Received _promiseTimeout event, resolve or reject..
|
||||||
|
this.once('_promiseTimeout', err => {
|
||||||
|
if(!err) { resolve([ channel, username, ~~seconds, reason ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Unban username on channel..
|
||||||
|
unban(channel, username) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/unban ${username}`, (resolve, reject) => {
|
||||||
|
// Received _promiseUnban event, resolve or reject..
|
||||||
|
this.once('_promiseUnban', err => {
|
||||||
|
if(!err) { resolve([ channel, username ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// End the current hosting..
|
||||||
|
unhost(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(2000, channel, '/unhost', (resolve, reject) => {
|
||||||
|
// Received _promiseUnhost event, resolve or reject..
|
||||||
|
this.once('_promiseUnhost', err => {
|
||||||
|
if(!err) { resolve([ channel ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Unmod username on channel..
|
||||||
|
unmod(channel, username) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/unmod ${username}`, (resolve, reject) => {
|
||||||
|
// Received _promiseUnmod event, resolve or reject..
|
||||||
|
this.once('_promiseUnmod', err => {
|
||||||
|
if(!err) { resolve([ channel, username ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Unvip username on channel..
|
||||||
|
unvip(channel, username) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/unvip ${username}`, (resolve, reject) => {
|
||||||
|
// Received _promiseUnvip event, resolve or reject..
|
||||||
|
this.once('_promiseUnvip', err => {
|
||||||
|
if(!err) { resolve([ channel, username ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Add username to VIP list on channel..
|
||||||
|
vip(channel, username) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
username = _.username(username);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, `/vip ${username}`, (resolve, reject) => {
|
||||||
|
// Received _promiseVip event, resolve or reject..
|
||||||
|
this.once('_promiseVip', err => {
|
||||||
|
if(!err) { resolve([ channel, username ]); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Get list of VIPs on a channel..
|
||||||
|
vips(channel) {
|
||||||
|
channel = _.channel(channel);
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, channel, '/vips', (resolve, reject) => {
|
||||||
|
// Received _promiseVips event, resolve or reject..
|
||||||
|
this.once('_promiseVips', (err, vips) => {
|
||||||
|
if(!err) { resolve(vips); }
|
||||||
|
else { reject(err); }
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Send an whisper message to a user..
|
||||||
|
whisper(username, message) {
|
||||||
|
username = _.username(username);
|
||||||
|
|
||||||
|
// The server will not send a whisper to the account that sent it.
|
||||||
|
if(username === this.getUsername()) {
|
||||||
|
return Promise.reject('Cannot send a whisper to the same account.');
|
||||||
|
}
|
||||||
|
// Send the command to the server and race the Promise against a delay..
|
||||||
|
return this._sendCommand(null, '#tmijs', `/w ${username} ${message}`, (_resolve, reject) => {
|
||||||
|
this.once('_promiseWhisper', err => {
|
||||||
|
if (err) { reject(err); }
|
||||||
|
});
|
||||||
|
}).catch(err => {
|
||||||
|
// Either an "actual" error occured or the timeout triggered
|
||||||
|
// the latter means no errors have occured and we can resolve
|
||||||
|
// else just elevate the error
|
||||||
|
if(err && typeof err === 'string' && err.indexOf('No response from Twitch.') !== 0) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
const from = _.channel(username);
|
||||||
|
const userstate = Object.assign({
|
||||||
|
'message-type': 'whisper',
|
||||||
|
'message-id': null,
|
||||||
|
'thread-id': null,
|
||||||
|
username: this.getUsername()
|
||||||
|
}, this.globaluserstate);
|
||||||
|
|
||||||
|
// Emit for both, whisper and message..
|
||||||
|
this.emits([ 'whisper', 'message' ], [
|
||||||
|
[ from, userstate, message, true ],
|
||||||
|
[ from, userstate, message, true ]
|
||||||
|
]);
|
||||||
|
return [ username, message ];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
301
node_modules/tmi.js/lib/events.js
generated
vendored
Normal file
301
node_modules/tmi.js/lib/events.js
generated
vendored
Normal file
@@ -0,0 +1,301 @@
|
|||||||
|
/* istanbul ignore file */
|
||||||
|
/* eslint-disable */
|
||||||
|
/*
|
||||||
|
* Copyright Joyent, Inc. and other Node contributors.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||||
|
* persons to whom the Software is furnished to do so, subject to the
|
||||||
|
* following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included
|
||||||
|
* in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||||
|
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||||
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||||
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function EventEmitter() {
|
||||||
|
this._events = this._events || {};
|
||||||
|
this._maxListeners = this._maxListeners || undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = EventEmitter;
|
||||||
|
|
||||||
|
// Backwards-compat with node 0.10.x
|
||||||
|
EventEmitter.EventEmitter = EventEmitter;
|
||||||
|
|
||||||
|
EventEmitter.prototype._events = undefined;
|
||||||
|
EventEmitter.prototype._maxListeners = undefined;
|
||||||
|
|
||||||
|
// By default EventEmitters will print a warning if more than 10 listeners are
|
||||||
|
// added to it. This is a useful default which helps finding memory leaks.
|
||||||
|
EventEmitter.defaultMaxListeners = 10;
|
||||||
|
|
||||||
|
// Obviously not all Emitters should be limited to 10. This function allows
|
||||||
|
// that to be increased. Set to zero for unlimited.
|
||||||
|
EventEmitter.prototype.setMaxListeners = function(n) {
|
||||||
|
if (!isNumber(n) || n < 0 || isNaN(n)) {
|
||||||
|
throw TypeError("n must be a positive number");
|
||||||
|
}
|
||||||
|
|
||||||
|
this._maxListeners = n;
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.emit = function(type) {
|
||||||
|
var er, handler, len, args, i, listeners;
|
||||||
|
|
||||||
|
if (!this._events) { this._events = {}; }
|
||||||
|
|
||||||
|
// If there is no 'error' event listener then throw.
|
||||||
|
if (type === "error") {
|
||||||
|
if (!this._events.error || (isObject(this._events.error) && !this._events.error.length)) {
|
||||||
|
er = arguments[1];
|
||||||
|
if (er instanceof Error) { throw er; }
|
||||||
|
throw TypeError("Uncaught, unspecified \"error\" event.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handler = this._events[type];
|
||||||
|
|
||||||
|
if (isUndefined(handler)) { return false; }
|
||||||
|
|
||||||
|
if (isFunction(handler)) {
|
||||||
|
switch (arguments.length) {
|
||||||
|
// fast cases
|
||||||
|
case 1:
|
||||||
|
handler.call(this);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
handler.call(this, arguments[1]);
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
handler.call(this, arguments[1], arguments[2]);
|
||||||
|
break;
|
||||||
|
// slower
|
||||||
|
default:
|
||||||
|
args = Array.prototype.slice.call(arguments, 1);
|
||||||
|
handler.apply(this, args);
|
||||||
|
}
|
||||||
|
} else if (isObject(handler)) {
|
||||||
|
args = Array.prototype.slice.call(arguments, 1);
|
||||||
|
listeners = handler.slice();
|
||||||
|
len = listeners.length;
|
||||||
|
for (i = 0; i < len; i++) { listeners[i].apply(this, args); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.addListener = function(type, listener) {
|
||||||
|
var m;
|
||||||
|
|
||||||
|
if (!isFunction(listener)) { throw TypeError("listener must be a function"); }
|
||||||
|
|
||||||
|
if (!this._events) { this._events = {}; }
|
||||||
|
|
||||||
|
// To avoid recursion in the case that type === "newListener"! Before
|
||||||
|
// adding it to the listeners, first emit "newListener".
|
||||||
|
if (this._events.newListener) {
|
||||||
|
this.emit("newListener", type, isFunction(listener.listener) ? listener.listener : listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optimize the case of one listener. Don't need the extra array object.
|
||||||
|
if (!this._events[type]) { this._events[type] = listener; }
|
||||||
|
// If we've already got an array, just append.
|
||||||
|
else if (isObject(this._events[type])) { this._events[type].push(listener); }
|
||||||
|
// Adding the second element, need to change to array.
|
||||||
|
else { this._events[type] = [this._events[type], listener]; }
|
||||||
|
|
||||||
|
// Check for listener leak
|
||||||
|
if (isObject(this._events[type]) && !this._events[type].warned) {
|
||||||
|
if (!isUndefined(this._maxListeners)) {
|
||||||
|
m = this._maxListeners;
|
||||||
|
} else {
|
||||||
|
m = EventEmitter.defaultMaxListeners;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m && m > 0 && this._events[type].length > m) {
|
||||||
|
this._events[type].warned = true;
|
||||||
|
console.error("(node) warning: possible EventEmitter memory leak detected. %d listeners added. Use emitter.setMaxListeners() to increase limit.", this._events[type].length);
|
||||||
|
// Not supported in IE 10
|
||||||
|
if (typeof console.trace === "function") {
|
||||||
|
console.trace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
||||||
|
|
||||||
|
// Modified to support multiple calls..
|
||||||
|
EventEmitter.prototype.once = function(type, listener) {
|
||||||
|
if (!isFunction(listener)) { throw TypeError("listener must be a function"); }
|
||||||
|
|
||||||
|
var fired = false;
|
||||||
|
|
||||||
|
if (this._events.hasOwnProperty(type) && type.charAt(0) === "_") {
|
||||||
|
var count = 1;
|
||||||
|
var searchFor = type;
|
||||||
|
|
||||||
|
for (var k in this._events){
|
||||||
|
if (this._events.hasOwnProperty(k) && k.startsWith(searchFor)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
type = type + count;
|
||||||
|
}
|
||||||
|
|
||||||
|
function g() {
|
||||||
|
if (type.charAt(0) === "_" && !isNaN(type.substr(type.length - 1))) {
|
||||||
|
type = type.substring(0, type.length - 1);
|
||||||
|
}
|
||||||
|
this.removeListener(type, g);
|
||||||
|
|
||||||
|
if (!fired) {
|
||||||
|
fired = true;
|
||||||
|
listener.apply(this, arguments);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
g.listener = listener;
|
||||||
|
this.on(type, g);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Emits a "removeListener" event if the listener was removed..
|
||||||
|
// Modified to support multiple calls from .once()..
|
||||||
|
EventEmitter.prototype.removeListener = function(type, listener) {
|
||||||
|
var list, position, length, i;
|
||||||
|
|
||||||
|
if (!isFunction(listener)) { throw TypeError("listener must be a function"); }
|
||||||
|
|
||||||
|
if (!this._events || !this._events[type]) { return this; }
|
||||||
|
|
||||||
|
list = this._events[type];
|
||||||
|
length = list.length;
|
||||||
|
position = -1;
|
||||||
|
if (list === listener || (isFunction(list.listener) && list.listener === listener)) {
|
||||||
|
delete this._events[type];
|
||||||
|
|
||||||
|
if (this._events.hasOwnProperty(type + "2") && type.charAt(0) === "_") {
|
||||||
|
var searchFor = type;
|
||||||
|
for (var k in this._events){
|
||||||
|
if (this._events.hasOwnProperty(k) && k.startsWith(searchFor)) {
|
||||||
|
if (!isNaN(parseInt(k.substr(k.length - 1)))) {
|
||||||
|
this._events[type + parseInt(k.substr(k.length - 1) - 1)] = this._events[k];
|
||||||
|
delete this._events[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this._events[type] = this._events[type + "1"];
|
||||||
|
delete this._events[type + "1"];
|
||||||
|
}
|
||||||
|
if (this._events.removeListener) { this.emit("removeListener", type, listener); }
|
||||||
|
}
|
||||||
|
else if (isObject(list)) {
|
||||||
|
for (i = length; i-- > 0;) {
|
||||||
|
if (list[i] === listener ||
|
||||||
|
(list[i].listener && list[i].listener === listener)) {
|
||||||
|
position = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (position < 0) { return this; }
|
||||||
|
|
||||||
|
if (list.length === 1) {
|
||||||
|
list.length = 0;
|
||||||
|
delete this._events[type];
|
||||||
|
}
|
||||||
|
else { list.splice(position, 1); }
|
||||||
|
|
||||||
|
if (this._events.removeListener) { this.emit("removeListener", type, listener); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.removeAllListeners = function(type) {
|
||||||
|
var key, listeners;
|
||||||
|
|
||||||
|
if (!this._events) { return this; }
|
||||||
|
|
||||||
|
// not listening for removeListener, no need to emit
|
||||||
|
if (!this._events.removeListener) {
|
||||||
|
if (arguments.length === 0) { this._events = {}; }
|
||||||
|
else if (this._events[type]) { delete this._events[type]; }
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// emit removeListener for all listeners on all events
|
||||||
|
if (arguments.length === 0) {
|
||||||
|
for (key in this._events) {
|
||||||
|
if (key === "removeListener") { continue; }
|
||||||
|
this.removeAllListeners(key);
|
||||||
|
}
|
||||||
|
this.removeAllListeners("removeListener");
|
||||||
|
this._events = {};
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
listeners = this._events[type];
|
||||||
|
|
||||||
|
if (isFunction(listeners)) { this.removeListener(type, listeners); }
|
||||||
|
else if (listeners) { while (listeners.length) { this.removeListener(type, listeners[listeners.length - 1]); } }
|
||||||
|
delete this._events[type];
|
||||||
|
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.listeners = function(type) {
|
||||||
|
var ret;
|
||||||
|
if (!this._events || !this._events[type]) { ret = []; }
|
||||||
|
else if (isFunction(this._events[type])) { ret = [this._events[type]]; }
|
||||||
|
else { ret = this._events[type].slice(); }
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.prototype.listenerCount = function(type) {
|
||||||
|
if (this._events) {
|
||||||
|
var evlistener = this._events[type];
|
||||||
|
|
||||||
|
if (isFunction(evlistener)) { return 1; }
|
||||||
|
else if (evlistener) { return evlistener.length; }
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
EventEmitter.listenerCount = function(emitter, type) {
|
||||||
|
return emitter.listenerCount(type);
|
||||||
|
};
|
||||||
|
|
||||||
|
function isFunction(arg) {
|
||||||
|
return typeof arg === "function";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNumber(arg) {
|
||||||
|
return typeof arg === "number";
|
||||||
|
}
|
||||||
|
|
||||||
|
function isObject(arg) {
|
||||||
|
return typeof arg === "object" && arg !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isUndefined(arg) {
|
||||||
|
return arg === void 0;
|
||||||
|
}
|
||||||
27
node_modules/tmi.js/lib/logger.js
generated
vendored
Normal file
27
node_modules/tmi.js/lib/logger.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
const _ = require('./utils');
|
||||||
|
|
||||||
|
let currentLevel = 'info';
|
||||||
|
const levels = { 'trace': 0, 'debug': 1, 'info': 2, 'warn': 3, 'error': 4, 'fatal': 5 };
|
||||||
|
|
||||||
|
// Logger implementation..
|
||||||
|
function log(level) {
|
||||||
|
// Return a console message depending on the logging level..
|
||||||
|
return function(message) {
|
||||||
|
if(levels[level] >= levels[currentLevel]) {
|
||||||
|
console.log(`[${_.formatDate(new Date())}] ${level}: ${message}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// Change the current logging level..
|
||||||
|
setLevel(level) {
|
||||||
|
currentLevel = level;
|
||||||
|
},
|
||||||
|
trace: log('trace'),
|
||||||
|
debug: log('debug'),
|
||||||
|
info: log('info'),
|
||||||
|
warn: log('warn'),
|
||||||
|
error: log('error'),
|
||||||
|
fatal: log('fatal')
|
||||||
|
};
|
||||||
243
node_modules/tmi.js/lib/parser.js
generated
vendored
Normal file
243
node_modules/tmi.js/lib/parser.js
generated
vendored
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2013-2015, Fionn Kelleher All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation and/or other materials
|
||||||
|
provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
||||||
|
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||||
|
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||||
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
||||||
|
OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
const _ = require('./utils');
|
||||||
|
const nonspaceRegex = /\S+/g;
|
||||||
|
|
||||||
|
function parseComplexTag(tags, tagKey, splA = ',', splB = '/', splC) {
|
||||||
|
const raw = tags[tagKey];
|
||||||
|
|
||||||
|
if(raw === undefined) {
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tagIsString = typeof raw === 'string';
|
||||||
|
tags[tagKey + '-raw'] = tagIsString ? raw : null;
|
||||||
|
|
||||||
|
if(raw === true) {
|
||||||
|
tags[tagKey] = null;
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
tags[tagKey] = {};
|
||||||
|
|
||||||
|
if(tagIsString) {
|
||||||
|
const spl = raw.split(splA);
|
||||||
|
|
||||||
|
for (let i = 0; i < spl.length; i++) {
|
||||||
|
const parts = spl[i].split(splB);
|
||||||
|
let val = parts[1];
|
||||||
|
if(splC !== undefined && val) {
|
||||||
|
val = val.split(splC);
|
||||||
|
}
|
||||||
|
tags[tagKey][parts[0]] = val || null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tags;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
// Parse Twitch badges..
|
||||||
|
badges: tags => parseComplexTag(tags, 'badges'),
|
||||||
|
|
||||||
|
// Parse Twitch badge-info..
|
||||||
|
badgeInfo: tags => parseComplexTag(tags, 'badge-info'),
|
||||||
|
|
||||||
|
// Parse Twitch emotes..
|
||||||
|
emotes: tags => parseComplexTag(tags, 'emotes', '/', ':', ','),
|
||||||
|
|
||||||
|
// Parse regex emotes..
|
||||||
|
emoteRegex(msg, code, id, obj) {
|
||||||
|
nonspaceRegex.lastIndex = 0;
|
||||||
|
const regex = new RegExp('(\\b|^|\\s)' + _.unescapeHtml(code) + '(\\b|$|\\s)');
|
||||||
|
let match;
|
||||||
|
|
||||||
|
// Check if emote code matches using RegExp and push it to the object..
|
||||||
|
while ((match = nonspaceRegex.exec(msg)) !== null) {
|
||||||
|
if(regex.test(match[0])) {
|
||||||
|
obj[id] = obj[id] || [];
|
||||||
|
obj[id].push([ match.index, nonspaceRegex.lastIndex - 1 ]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Parse string emotes..
|
||||||
|
emoteString(msg, code, id, obj) {
|
||||||
|
nonspaceRegex.lastIndex = 0;
|
||||||
|
let match;
|
||||||
|
|
||||||
|
// Check if emote code matches and push it to the object..
|
||||||
|
while ((match = nonspaceRegex.exec(msg)) !== null) {
|
||||||
|
if(match[0] === _.unescapeHtml(code)) {
|
||||||
|
obj[id] = obj[id] || [];
|
||||||
|
obj[id].push([ match.index, nonspaceRegex.lastIndex - 1 ]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Transform the emotes object to a string with the following format..
|
||||||
|
// emote_id:first_index-last_index,another_first-another_last/another_emote_id:first_index-last_index
|
||||||
|
transformEmotes(emotes) {
|
||||||
|
let transformed = '';
|
||||||
|
|
||||||
|
Object.keys(emotes).forEach(id => {
|
||||||
|
transformed = `${transformed+id}:`;
|
||||||
|
emotes[id].forEach(
|
||||||
|
index => transformed = `${transformed+index.join('-')},`
|
||||||
|
);
|
||||||
|
transformed = `${transformed.slice(0, -1)}/`;
|
||||||
|
});
|
||||||
|
return transformed.slice(0, -1);
|
||||||
|
},
|
||||||
|
|
||||||
|
formTags(tags) {
|
||||||
|
const result = [];
|
||||||
|
for(const key in tags) {
|
||||||
|
const value = _.escapeIRC(tags[key]);
|
||||||
|
result.push(`${key}=${value}`);
|
||||||
|
}
|
||||||
|
return `@${result.join(';')}`;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Parse Twitch messages..
|
||||||
|
msg(data) {
|
||||||
|
const message = {
|
||||||
|
raw: data,
|
||||||
|
tags: {},
|
||||||
|
prefix: null,
|
||||||
|
command: null,
|
||||||
|
params: []
|
||||||
|
};
|
||||||
|
|
||||||
|
// Position and nextspace are used by the parser as a reference..
|
||||||
|
let position = 0;
|
||||||
|
let nextspace = 0;
|
||||||
|
|
||||||
|
// The first thing we check for is IRCv3.2 message tags.
|
||||||
|
// http://ircv3.atheme.org/specification/message-tags-3.2
|
||||||
|
if(data.charCodeAt(0) === 64) {
|
||||||
|
nextspace = data.indexOf(' ');
|
||||||
|
|
||||||
|
// Malformed IRC message..
|
||||||
|
if(nextspace === -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tags are split by a semi colon..
|
||||||
|
const rawTags = data.slice(1, nextspace).split(';');
|
||||||
|
|
||||||
|
for (let i = 0; i < rawTags.length; i++) {
|
||||||
|
// Tags delimited by an equals sign are key=value tags.
|
||||||
|
// If there's no equals, we assign the tag a value of true.
|
||||||
|
const tag = rawTags[i];
|
||||||
|
const pair = tag.split('=');
|
||||||
|
message.tags[pair[0]] = tag.substring(tag.indexOf('=') + 1) || true;
|
||||||
|
}
|
||||||
|
|
||||||
|
position = nextspace + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip any trailing whitespace..
|
||||||
|
while (data.charCodeAt(position) === 32) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the message's prefix if present. Prefixes are prepended with a colon..
|
||||||
|
if(data.charCodeAt(position) === 58) {
|
||||||
|
nextspace = data.indexOf(' ', position);
|
||||||
|
|
||||||
|
// If there's nothing after the prefix, deem this message to be malformed.
|
||||||
|
if(nextspace === -1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.prefix = data.slice(position + 1, nextspace);
|
||||||
|
position = nextspace + 1;
|
||||||
|
|
||||||
|
// Skip any trailing whitespace..
|
||||||
|
while (data.charCodeAt(position) === 32) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nextspace = data.indexOf(' ', position);
|
||||||
|
|
||||||
|
// If there's no more whitespace left, extract everything from the
|
||||||
|
// current position to the end of the string as the command..
|
||||||
|
if(nextspace === -1) {
|
||||||
|
if(data.length > position) {
|
||||||
|
message.command = data.slice(position);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Else, the command is the current position up to the next space. After
|
||||||
|
// that, we expect some parameters.
|
||||||
|
message.command = data.slice(position, nextspace);
|
||||||
|
|
||||||
|
position = nextspace + 1;
|
||||||
|
|
||||||
|
// Skip any trailing whitespace..
|
||||||
|
while (data.charCodeAt(position) === 32) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (position < data.length) {
|
||||||
|
nextspace = data.indexOf(' ', position);
|
||||||
|
|
||||||
|
// If the character is a colon, we've got a trailing parameter.
|
||||||
|
// At this point, there are no extra params, so we push everything
|
||||||
|
// from after the colon to the end of the string, to the params array
|
||||||
|
// and break out of the loop.
|
||||||
|
if(data.charCodeAt(position) === 58) {
|
||||||
|
message.params.push(data.slice(position + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we still have some whitespace...
|
||||||
|
if(nextspace !== -1) {
|
||||||
|
// Push whatever's between the current position and the next
|
||||||
|
// space to the params array.
|
||||||
|
message.params.push(data.slice(position, nextspace));
|
||||||
|
position = nextspace + 1;
|
||||||
|
|
||||||
|
// Skip any trailing whitespace and continue looping.
|
||||||
|
while (data.charCodeAt(position) === 32) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we don't have any more whitespace and the param isn't trailing,
|
||||||
|
// push everything remaining to the params array.
|
||||||
|
if(nextspace === -1) {
|
||||||
|
message.params.push(data.slice(position));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
};
|
||||||
28
node_modules/tmi.js/lib/timer.js
generated
vendored
Normal file
28
node_modules/tmi.js/lib/timer.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
// Initialize the queue with a specific delay..
|
||||||
|
class Queue {
|
||||||
|
constructor(defaultDelay) {
|
||||||
|
this.queue = [];
|
||||||
|
this.index = 0;
|
||||||
|
this.defaultDelay = defaultDelay === undefined ? 3000 : defaultDelay;
|
||||||
|
}
|
||||||
|
// Add a new function to the queue..
|
||||||
|
add(fn, delay) {
|
||||||
|
this.queue.push({ fn, delay });
|
||||||
|
}
|
||||||
|
// Go to the next in queue..
|
||||||
|
next() {
|
||||||
|
const i = this.index++;
|
||||||
|
const at = this.queue[i];
|
||||||
|
if(!at) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const next = this.queue[this.index];
|
||||||
|
at.fn();
|
||||||
|
if(next) {
|
||||||
|
const delay = next.delay === undefined ? this.defaultDelay : next.delay;
|
||||||
|
setTimeout(() => this.next(), delay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Queue;
|
||||||
161
node_modules/tmi.js/lib/utils.js
generated
vendored
Normal file
161
node_modules/tmi.js/lib/utils.js
generated
vendored
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
// eslint-disable-next-line no-control-regex
|
||||||
|
const actionMessageRegex = /^\u0001ACTION ([^\u0001]+)\u0001$/;
|
||||||
|
const justinFanRegex = /^(justinfan)(\d+$)/;
|
||||||
|
const unescapeIRCRegex = /\\([sn:r\\])/g;
|
||||||
|
const escapeIRCRegex = /([ \n;\r\\])/g;
|
||||||
|
const ircEscapedChars = { s: ' ', n: '', ':': ';', r: '' };
|
||||||
|
const ircUnescapedChars = { ' ': 's', '\n': 'n', ';': ':', '\r': 'r' };
|
||||||
|
const urlRegex = new RegExp('^(?:(?:https?|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?!(?:10|127)(?:\\.\\d{1,3}){3})(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))\\.?)(?::\\d{2,5})?(?:[/?#]\\S*)?$', 'i');
|
||||||
|
const regexEmoteRegex = /[|\\^$*+?:#]/;
|
||||||
|
const _ = module.exports = {
|
||||||
|
// Return the second value if the first value is undefined..
|
||||||
|
get: (a, b) => typeof a === 'undefined' ? b : a,
|
||||||
|
|
||||||
|
// Indirectly use hasOwnProperty
|
||||||
|
hasOwn: (obj, key) => ({}).hasOwnProperty.call(obj, key),
|
||||||
|
|
||||||
|
// Race a promise against a delay..
|
||||||
|
promiseDelay: time => new Promise(resolve => setTimeout(resolve, time)),
|
||||||
|
|
||||||
|
// Value is a finite number..
|
||||||
|
isFinite: int => isFinite(int) && !isNaN(parseFloat(int)),
|
||||||
|
|
||||||
|
// Parse string to number. Returns NaN if string can't be parsed to number..
|
||||||
|
toNumber(num, precision) {
|
||||||
|
if(num === null) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
const factor = Math.pow(10, _.isFinite(precision) ? precision : 0);
|
||||||
|
return Math.round(num * factor) / factor;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Value is an integer..
|
||||||
|
isInteger: int => !isNaN(_.toNumber(int, 0)),
|
||||||
|
|
||||||
|
// Value is a regex..
|
||||||
|
isRegex: str => regexEmoteRegex.test(str),
|
||||||
|
|
||||||
|
// Value is a valid url..
|
||||||
|
isURL: str => urlRegex.test(str),
|
||||||
|
|
||||||
|
// Return a random justinfan username..
|
||||||
|
justinfan: () => `justinfan${Math.floor((Math.random() * 80000) + 1000)}`,
|
||||||
|
|
||||||
|
// Username is a justinfan username..
|
||||||
|
isJustinfan: username => justinFanRegex.test(username),
|
||||||
|
|
||||||
|
// Return a valid channel name..
|
||||||
|
channel(str) {
|
||||||
|
const channel = (str ? str : '').toLowerCase();
|
||||||
|
return channel[0] === '#' ? channel : '#' + channel;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Return a valid username..
|
||||||
|
username(str) {
|
||||||
|
const username = (str ? str : '').toLowerCase();
|
||||||
|
return username[0] === '#' ? username.slice(1) : username;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Return a valid token..
|
||||||
|
token: str => str ? str.toLowerCase().replace('oauth:', '') : '',
|
||||||
|
|
||||||
|
// Return a valid password..
|
||||||
|
password(str) {
|
||||||
|
const token = _.token(str);
|
||||||
|
return token ? `oauth:${token}` : '';
|
||||||
|
},
|
||||||
|
|
||||||
|
actionMessage: msg => msg.match(actionMessageRegex),
|
||||||
|
|
||||||
|
// Replace all occurences of a string using an object..
|
||||||
|
replaceAll(str, obj) {
|
||||||
|
if(str === null || typeof str === 'undefined') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (const x in obj) {
|
||||||
|
str = str.replace(new RegExp(x, 'g'), obj[x]);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
},
|
||||||
|
|
||||||
|
unescapeHtml: safe =>
|
||||||
|
safe.replace(/\\&\\;/g, '&')
|
||||||
|
.replace(/\\<\\;/g, '<')
|
||||||
|
.replace(/\\>\\;/g, '>')
|
||||||
|
.replace(/\\"\\;/g, '"')
|
||||||
|
.replace(/\\'\\;/g, '\''),
|
||||||
|
|
||||||
|
// Escaping values:
|
||||||
|
// http://ircv3.net/specs/core/message-tags-3.2.html#escaping-values
|
||||||
|
unescapeIRC(msg) {
|
||||||
|
if(!msg || typeof msg !== 'string' || !msg.includes('\\')) {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
return msg.replace(
|
||||||
|
unescapeIRCRegex,
|
||||||
|
(m, p) => p in ircEscapedChars ? ircEscapedChars[p] : p
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
escapeIRC(msg) {
|
||||||
|
if(!msg || typeof msg !== 'string') {
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
return msg.replace(
|
||||||
|
escapeIRCRegex,
|
||||||
|
(m, p) => p in ircUnescapedChars ? `\\${ircUnescapedChars[p]}` : p
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Add word to a string..
|
||||||
|
addWord: (line, word) => line.length ? line + ' ' + word : line + word,
|
||||||
|
|
||||||
|
// Split a line but try not to cut a word in half..
|
||||||
|
splitLine(input, length) {
|
||||||
|
let lastSpace = input.substring(0, length).lastIndexOf(' ');
|
||||||
|
// No spaces found, split at the very end to avoid a loop..
|
||||||
|
if(lastSpace === -1) {
|
||||||
|
lastSpace = length - 1;
|
||||||
|
}
|
||||||
|
return [ input.substring(0, lastSpace), input.substring(lastSpace + 1) ];
|
||||||
|
},
|
||||||
|
|
||||||
|
// Extract a number from a string..
|
||||||
|
extractNumber(str) {
|
||||||
|
const parts = str.split(' ');
|
||||||
|
for (let i = 0; i < parts.length; i++) {
|
||||||
|
if(_.isInteger(parts[i])) {
|
||||||
|
return ~~parts[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Format the date..
|
||||||
|
formatDate(date) {
|
||||||
|
let hours = date.getHours();
|
||||||
|
let mins = date.getMinutes();
|
||||||
|
|
||||||
|
hours = (hours < 10 ? '0' : '') + hours;
|
||||||
|
mins = (mins < 10 ? '0' : '') + mins;
|
||||||
|
return `${hours}:${mins}`;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Inherit the prototype methods from one constructor into another..
|
||||||
|
inherits(ctor, superCtor) {
|
||||||
|
ctor.super_ = superCtor;
|
||||||
|
const TempCtor = function () {};
|
||||||
|
TempCtor.prototype = superCtor.prototype;
|
||||||
|
ctor.prototype = new TempCtor();
|
||||||
|
ctor.prototype.constructor = ctor;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Return whether inside a Node application or not..
|
||||||
|
isNode() {
|
||||||
|
try {
|
||||||
|
return typeof process === 'object' &&
|
||||||
|
Object.prototype.toString.call(process) === '[object process]';
|
||||||
|
} catch(e) {}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
83
node_modules/tmi.js/package.json
generated
vendored
Normal file
83
node_modules/tmi.js/package.json
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
{
|
||||||
|
"name": "tmi.js",
|
||||||
|
"version": "1.8.5",
|
||||||
|
"description": "Javascript library for the Twitch Messaging Interface.",
|
||||||
|
"keywords": [
|
||||||
|
"tmi",
|
||||||
|
"tmijs",
|
||||||
|
"twitch",
|
||||||
|
"chat",
|
||||||
|
"message",
|
||||||
|
"messaging",
|
||||||
|
"interface",
|
||||||
|
"bot"
|
||||||
|
],
|
||||||
|
"main": "index.js",
|
||||||
|
"files": [
|
||||||
|
"lib",
|
||||||
|
"index.js",
|
||||||
|
"LICENSE"
|
||||||
|
],
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"author": "Schmoopiie",
|
||||||
|
"scripts": {
|
||||||
|
"test": "run-s eslint test:*",
|
||||||
|
"eslint": "eslint index.js lib/* test/*",
|
||||||
|
"eslint-fix": "eslint --fix index.js lib/* test/*",
|
||||||
|
"test:mocha": "nyc mocha -- --require should --exit",
|
||||||
|
"build": "run-s build:*",
|
||||||
|
"build:rimraf": "rimraf ./build",
|
||||||
|
"build:mkdirp": "mkdirp ./build",
|
||||||
|
"build:dedupe": "npm dedupe",
|
||||||
|
"build:browserify": "browserify index.js -o ./build/tmi.js",
|
||||||
|
"build:uglify": "uglifyjs --compress --mangle --output ./build/tmi.min.js --source-map \"filename='./build/tmi.js.map'\" ./build/tmi.js",
|
||||||
|
"build:sri": "node sri.js ./build/tmi.js",
|
||||||
|
"build:sri-min": "node sri.js ./build/tmi.min.js"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git://github.com/tmijs/tmi.js.git"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/tmijs/tmi.js/issues"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
|
"ws": "^8.2.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@babel/core": "7.15.0",
|
||||||
|
"@babel/preset-env": "7.15.0",
|
||||||
|
"@types/node-fetch": "2.5.12",
|
||||||
|
"babelify": "10.0.0",
|
||||||
|
"browserify": "17.0.0",
|
||||||
|
"eslint": "7.32.0",
|
||||||
|
"hook-std": "2.0.0",
|
||||||
|
"mkdirp": "1.0.4",
|
||||||
|
"mocha": "9.0.3",
|
||||||
|
"npm-run-all": "4.1.5",
|
||||||
|
"nyc": "15.1.0",
|
||||||
|
"rimraf": "3.0.2",
|
||||||
|
"should": "13.2.3",
|
||||||
|
"uglify-js": "3.14.1"
|
||||||
|
},
|
||||||
|
"browser": {
|
||||||
|
"ws": false,
|
||||||
|
"node-fetch": false
|
||||||
|
},
|
||||||
|
"browserify": {
|
||||||
|
"transform": [
|
||||||
|
[
|
||||||
|
"babelify",
|
||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
"@babel/preset-env"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
4
node_modules/tr46/.npmignore
generated
vendored
Normal file
4
node_modules/tr46/.npmignore
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
scripts/
|
||||||
|
test/
|
||||||
|
|
||||||
|
!lib/mapping_table.json
|
||||||
193
node_modules/tr46/index.js
generated
vendored
Normal file
193
node_modules/tr46/index.js
generated
vendored
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var punycode = require("punycode");
|
||||||
|
var mappingTable = require("./lib/mappingTable.json");
|
||||||
|
|
||||||
|
var PROCESSING_OPTIONS = {
|
||||||
|
TRANSITIONAL: 0,
|
||||||
|
NONTRANSITIONAL: 1
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalize(str) { // fix bug in v8
|
||||||
|
return str.split('\u0000').map(function (s) { return s.normalize('NFC'); }).join('\u0000');
|
||||||
|
}
|
||||||
|
|
||||||
|
function findStatus(val) {
|
||||||
|
var start = 0;
|
||||||
|
var end = mappingTable.length - 1;
|
||||||
|
|
||||||
|
while (start <= end) {
|
||||||
|
var mid = Math.floor((start + end) / 2);
|
||||||
|
|
||||||
|
var target = mappingTable[mid];
|
||||||
|
if (target[0][0] <= val && target[0][1] >= val) {
|
||||||
|
return target;
|
||||||
|
} else if (target[0][0] > val) {
|
||||||
|
end = mid - 1;
|
||||||
|
} else {
|
||||||
|
start = mid + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var regexAstralSymbols = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
||||||
|
|
||||||
|
function countSymbols(string) {
|
||||||
|
return string
|
||||||
|
// replace every surrogate pair with a BMP symbol
|
||||||
|
.replace(regexAstralSymbols, '_')
|
||||||
|
// then get the length
|
||||||
|
.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
function mapChars(domain_name, useSTD3, processing_option) {
|
||||||
|
var hasError = false;
|
||||||
|
var processed = "";
|
||||||
|
|
||||||
|
var len = countSymbols(domain_name);
|
||||||
|
for (var i = 0; i < len; ++i) {
|
||||||
|
var codePoint = domain_name.codePointAt(i);
|
||||||
|
var status = findStatus(codePoint);
|
||||||
|
|
||||||
|
switch (status[1]) {
|
||||||
|
case "disallowed":
|
||||||
|
hasError = true;
|
||||||
|
processed += String.fromCodePoint(codePoint);
|
||||||
|
break;
|
||||||
|
case "ignored":
|
||||||
|
break;
|
||||||
|
case "mapped":
|
||||||
|
processed += String.fromCodePoint.apply(String, status[2]);
|
||||||
|
break;
|
||||||
|
case "deviation":
|
||||||
|
if (processing_option === PROCESSING_OPTIONS.TRANSITIONAL) {
|
||||||
|
processed += String.fromCodePoint.apply(String, status[2]);
|
||||||
|
} else {
|
||||||
|
processed += String.fromCodePoint(codePoint);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "valid":
|
||||||
|
processed += String.fromCodePoint(codePoint);
|
||||||
|
break;
|
||||||
|
case "disallowed_STD3_mapped":
|
||||||
|
if (useSTD3) {
|
||||||
|
hasError = true;
|
||||||
|
processed += String.fromCodePoint(codePoint);
|
||||||
|
} else {
|
||||||
|
processed += String.fromCodePoint.apply(String, status[2]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "disallowed_STD3_valid":
|
||||||
|
if (useSTD3) {
|
||||||
|
hasError = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
processed += String.fromCodePoint(codePoint);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
string: processed,
|
||||||
|
error: hasError
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var combiningMarksRegex = /[\u0300-\u036F\u0483-\u0489\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u0610-\u061A\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED\u0711\u0730-\u074A\u07A6-\u07B0\u07EB-\u07F3\u0816-\u0819\u081B-\u0823\u0825-\u0827\u0829-\u082D\u0859-\u085B\u08E4-\u0903\u093A-\u093C\u093E-\u094F\u0951-\u0957\u0962\u0963\u0981-\u0983\u09BC\u09BE-\u09C4\u09C7\u09C8\u09CB-\u09CD\u09D7\u09E2\u09E3\u0A01-\u0A03\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A70\u0A71\u0A75\u0A81-\u0A83\u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AE2\u0AE3\u0B01-\u0B03\u0B3C\u0B3E-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B62\u0B63\u0B82\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7\u0C00-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C62\u0C63\u0C81-\u0C83\u0CBC\u0CBE-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CE2\u0CE3\u0D01-\u0D03\u0D3E-\u0D44\u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0D62\u0D63\u0D82\u0D83\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DF2\u0DF3\u0E31\u0E34-\u0E3A\u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB\u0EBC\u0EC8-\u0ECD\u0F18\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84\u0F86\u0F87\u0F8D-\u0F97\u0F99-\u0FBC\u0FC6\u102B-\u103E\u1056-\u1059\u105E-\u1060\u1062-\u1064\u1067-\u106D\u1071-\u1074\u1082-\u108D\u108F\u109A-\u109D\u135D-\u135F\u1712-\u1714\u1732-\u1734\u1752\u1753\u1772\u1773\u17B4-\u17D3\u17DD\u180B-\u180D\u18A9\u1920-\u192B\u1930-\u193B\u19B0-\u19C0\u19C8\u19C9\u1A17-\u1A1B\u1A55-\u1A5E\u1A60-\u1A7C\u1A7F\u1AB0-\u1ABE\u1B00-\u1B04\u1B34-\u1B44\u1B6B-\u1B73\u1B80-\u1B82\u1BA1-\u1BAD\u1BE6-\u1BF3\u1C24-\u1C37\u1CD0-\u1CD2\u1CD4-\u1CE8\u1CED\u1CF2-\u1CF4\u1CF8\u1CF9\u1DC0-\u1DF5\u1DFC-\u1DFF\u20D0-\u20F0\u2CEF-\u2CF1\u2D7F\u2DE0-\u2DFF\u302A-\u302F\u3099\u309A\uA66F-\uA672\uA674-\uA67D\uA69F\uA6F0\uA6F1\uA802\uA806\uA80B\uA823-\uA827\uA880\uA881\uA8B4-\uA8C4\uA8E0-\uA8F1\uA926-\uA92D\uA947-\uA953\uA980-\uA983\uA9B3-\uA9C0\uA9E5\uAA29-\uAA36\uAA43\uAA4C\uAA4D\uAA7B-\uAA7D\uAAB0\uAAB2-\uAAB4\uAAB7\uAAB8\uAABE\uAABF\uAAC1\uAAEB-\uAAEF\uAAF5\uAAF6\uABE3-\uABEA\uABEC\uABED\uFB1E\uFE00-\uFE0F\uFE20-\uFE2D]|\uD800[\uDDFD\uDEE0\uDF76-\uDF7A]|\uD802[\uDE01-\uDE03\uDE05\uDE06\uDE0C-\uDE0F\uDE38-\uDE3A\uDE3F\uDEE5\uDEE6]|\uD804[\uDC00-\uDC02\uDC38-\uDC46\uDC7F-\uDC82\uDCB0-\uDCBA\uDD00-\uDD02\uDD27-\uDD34\uDD73\uDD80-\uDD82\uDDB3-\uDDC0\uDE2C-\uDE37\uDEDF-\uDEEA\uDF01-\uDF03\uDF3C\uDF3E-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF57\uDF62\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDCB0-\uDCC3\uDDAF-\uDDB5\uDDB8-\uDDC0\uDE30-\uDE40\uDEAB-\uDEB7]|\uD81A[\uDEF0-\uDEF4\uDF30-\uDF36]|\uD81B[\uDF51-\uDF7E\uDF8F-\uDF92]|\uD82F[\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD83A[\uDCD0-\uDCD6]|\uDB40[\uDD00-\uDDEF]/;
|
||||||
|
|
||||||
|
function validateLabel(label, processing_option) {
|
||||||
|
if (label.substr(0, 4) === "xn--") {
|
||||||
|
label = punycode.toUnicode(label);
|
||||||
|
processing_option = PROCESSING_OPTIONS.NONTRANSITIONAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
var error = false;
|
||||||
|
|
||||||
|
if (normalize(label) !== label ||
|
||||||
|
(label[3] === "-" && label[4] === "-") ||
|
||||||
|
label[0] === "-" || label[label.length - 1] === "-" ||
|
||||||
|
label.indexOf(".") !== -1 ||
|
||||||
|
label.search(combiningMarksRegex) === 0) {
|
||||||
|
error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var len = countSymbols(label);
|
||||||
|
for (var i = 0; i < len; ++i) {
|
||||||
|
var status = findStatus(label.codePointAt(i));
|
||||||
|
if ((processing === PROCESSING_OPTIONS.TRANSITIONAL && status[1] !== "valid") ||
|
||||||
|
(processing === PROCESSING_OPTIONS.NONTRANSITIONAL &&
|
||||||
|
status[1] !== "valid" && status[1] !== "deviation")) {
|
||||||
|
error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
label: label,
|
||||||
|
error: error
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function processing(domain_name, useSTD3, processing_option) {
|
||||||
|
var result = mapChars(domain_name, useSTD3, processing_option);
|
||||||
|
result.string = normalize(result.string);
|
||||||
|
|
||||||
|
var labels = result.string.split(".");
|
||||||
|
for (var i = 0; i < labels.length; ++i) {
|
||||||
|
try {
|
||||||
|
var validation = validateLabel(labels[i]);
|
||||||
|
labels[i] = validation.label;
|
||||||
|
result.error = result.error || validation.error;
|
||||||
|
} catch(e) {
|
||||||
|
result.error = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
string: labels.join("."),
|
||||||
|
error: result.error
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.toASCII = function(domain_name, useSTD3, processing_option, verifyDnsLength) {
|
||||||
|
var result = processing(domain_name, useSTD3, processing_option);
|
||||||
|
var labels = result.string.split(".");
|
||||||
|
labels = labels.map(function(l) {
|
||||||
|
try {
|
||||||
|
return punycode.toASCII(l);
|
||||||
|
} catch(e) {
|
||||||
|
result.error = true;
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (verifyDnsLength) {
|
||||||
|
var total = labels.slice(0, labels.length - 1).join(".").length;
|
||||||
|
if (total.length > 253 || total.length === 0) {
|
||||||
|
result.error = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i=0; i < labels.length; ++i) {
|
||||||
|
if (labels.length > 63 || labels.length === 0) {
|
||||||
|
result.error = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.error) return null;
|
||||||
|
return labels.join(".");
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.toUnicode = function(domain_name, useSTD3) {
|
||||||
|
var result = processing(domain_name, useSTD3, PROCESSING_OPTIONS.NONTRANSITIONAL);
|
||||||
|
|
||||||
|
return {
|
||||||
|
domain: result.string,
|
||||||
|
error: result.error
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.PROCESSING_OPTIONS = PROCESSING_OPTIONS;
|
||||||
0
node_modules/tr46/lib/.gitkeep
generated
vendored
Normal file
0
node_modules/tr46/lib/.gitkeep
generated
vendored
Normal file
1
node_modules/tr46/lib/mappingTable.json
generated
vendored
Normal file
1
node_modules/tr46/lib/mappingTable.json
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
31
node_modules/tr46/package.json
generated
vendored
Normal file
31
node_modules/tr46/package.json
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "tr46",
|
||||||
|
"version": "0.0.3",
|
||||||
|
"description": "An implementation of the Unicode TR46 spec",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "mocha",
|
||||||
|
"pretest": "node scripts/getLatestUnicodeTests.js",
|
||||||
|
"prepublish": "node scripts/generateMappingTable.js"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/Sebmaster/tr46.js.git"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"unicode",
|
||||||
|
"tr46",
|
||||||
|
"url",
|
||||||
|
"whatwg"
|
||||||
|
],
|
||||||
|
"author": "Sebastian Mayr <npm@smayr.name>",
|
||||||
|
"license": "MIT",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/Sebmaster/tr46.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/Sebmaster/tr46.js#readme",
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^2.2.5",
|
||||||
|
"request": "^2.57.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
12
node_modules/webidl-conversions/LICENSE.md
generated
vendored
Normal file
12
node_modules/webidl-conversions/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# The BSD 2-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2014, Domenic Denicola
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
53
node_modules/webidl-conversions/README.md
generated
vendored
Normal file
53
node_modules/webidl-conversions/README.md
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# WebIDL Type Conversions on JavaScript Values
|
||||||
|
|
||||||
|
This package implements, in JavaScript, the algorithms to convert a given JavaScript value according to a given [WebIDL](http://heycam.github.io/webidl/) [type](http://heycam.github.io/webidl/#idl-types).
|
||||||
|
|
||||||
|
The goal is that you should be able to write code like
|
||||||
|
|
||||||
|
```js
|
||||||
|
const conversions = require("webidl-conversions");
|
||||||
|
|
||||||
|
function doStuff(x, y) {
|
||||||
|
x = conversions["boolean"](x);
|
||||||
|
y = conversions["unsigned long"](y);
|
||||||
|
// actual algorithm code here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
and your function `doStuff` will behave the same as a WebIDL operation declared as
|
||||||
|
|
||||||
|
```webidl
|
||||||
|
void doStuff(boolean x, unsigned long y);
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
This package's main module's default export is an object with a variety of methods, each corresponding to a different WebIDL type. Each method, when invoked on a JavaScript value, will give back the new JavaScript value that results after passing through the WebIDL conversion rules. (See below for more details on what that means.) Alternately, the method could throw an error, if the WebIDL algorithm is specified to do so: for example `conversions["float"](NaN)` [will throw a `TypeError`](http://heycam.github.io/webidl/#es-float).
|
||||||
|
|
||||||
|
## Status
|
||||||
|
|
||||||
|
All of the numeric types are implemented (float being implemented as double) and some others are as well - check the source for all of them. This list will grow over time in service of the [HTML as Custom Elements](https://github.com/dglazkov/html-as-custom-elements) project, but in the meantime, pull requests welcome!
|
||||||
|
|
||||||
|
I'm not sure yet what the strategy will be for modifiers, e.g. [`[Clamp]`](http://heycam.github.io/webidl/#Clamp). Maybe something like `conversions["unsigned long"](x, { clamp: true })`? We'll see.
|
||||||
|
|
||||||
|
We might also want to extend the API to give better error messages, e.g. "Argument 1 of HTMLMediaElement.fastSeek is not a finite floating-point value" instead of "Argument is not a finite floating-point value." This would require passing in more information to the conversion functions than we currently do.
|
||||||
|
|
||||||
|
## Background
|
||||||
|
|
||||||
|
What's actually going on here, conceptually, is pretty weird. Let's try to explain.
|
||||||
|
|
||||||
|
WebIDL, as part of its madness-inducing design, has its own type system. When people write algorithms in web platform specs, they usually operate on WebIDL values, i.e. instances of WebIDL types. For example, if they were specifying the algorithm for our `doStuff` operation above, they would treat `x` as a WebIDL value of [WebIDL type `boolean`](http://heycam.github.io/webidl/#idl-boolean). Crucially, they would _not_ treat `x` as a JavaScript variable whose value is either the JavaScript `true` or `false`. They're instead working in a different type system altogether, with its own rules.
|
||||||
|
|
||||||
|
Separately from its type system, WebIDL defines a ["binding"](http://heycam.github.io/webidl/#ecmascript-binding) of the type system into JavaScript. This contains rules like: when you pass a JavaScript value to the JavaScript method that manifests a given WebIDL operation, how does that get converted into a WebIDL value? For example, a JavaScript `true` passed in the position of a WebIDL `boolean` argument becomes a WebIDL `true`. But, a JavaScript `true` passed in the position of a [WebIDL `unsigned long`](http://heycam.github.io/webidl/#idl-unsigned-long) becomes a WebIDL `1`. And so on.
|
||||||
|
|
||||||
|
Finally, we have the actual implementation code. This is usually C++, although these days [some smart people are using Rust](https://github.com/servo/servo). The implementation, of course, has its own type system. So when they implement the WebIDL algorithms, they don't actually use WebIDL values, since those aren't "real" outside of specs. Instead, implementations apply the WebIDL binding rules in such a way as to convert incoming JavaScript values into C++ values. For example, if code in the browser called `doStuff(true, true)`, then the implementation code would eventually receive a C++ `bool` containing `true` and a C++ `uint32_t` containing `1`.
|
||||||
|
|
||||||
|
The upside of all this is that implementations can abstract all the conversion logic away, letting WebIDL handle it, and focus on implementing the relevant methods in C++ with values of the correct type already provided. That is payoff of WebIDL, in a nutshell.
|
||||||
|
|
||||||
|
And getting to that payoff is the goal of _this_ project—but for JavaScript implementations, instead of C++ ones. That is, this library is designed to make it easier for JavaScript developers to write functions that behave like a given WebIDL operation. So conceptually, the conversion pipeline, which in its general form is JavaScript values ↦ WebIDL values ↦ implementation-language values, in this case becomes JavaScript values ↦ WebIDL values ↦ JavaScript values. And that intermediate step is where all the logic is performed: a JavaScript `true` becomes a WebIDL `1` in an unsigned long context, which then becomes a JavaScript `1`.
|
||||||
|
|
||||||
|
## Don't Use This
|
||||||
|
|
||||||
|
Seriously, why would you ever use this? You really shouldn't. WebIDL is … not great, and you shouldn't be emulating its semantics. If you're looking for a generic argument-processing library, you should find one with better rules than those from WebIDL. In general, your JavaScript should not be trying to become more like WebIDL; if anything, we should fix WebIDL to make it more like JavaScript.
|
||||||
|
|
||||||
|
The _only_ people who should use this are those trying to create faithful implementations (or polyfills) of web platform interfaces defined in WebIDL.
|
||||||
189
node_modules/webidl-conversions/lib/index.js
generated
vendored
Normal file
189
node_modules/webidl-conversions/lib/index.js
generated
vendored
Normal file
@@ -0,0 +1,189 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
var conversions = {};
|
||||||
|
module.exports = conversions;
|
||||||
|
|
||||||
|
function sign(x) {
|
||||||
|
return x < 0 ? -1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function evenRound(x) {
|
||||||
|
// Round x to the nearest integer, choosing the even integer if it lies halfway between two.
|
||||||
|
if ((x % 1) === 0.5 && (x & 1) === 0) { // [even number].5; round down (i.e. floor)
|
||||||
|
return Math.floor(x);
|
||||||
|
} else {
|
||||||
|
return Math.round(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createNumberConversion(bitLength, typeOpts) {
|
||||||
|
if (!typeOpts.unsigned) {
|
||||||
|
--bitLength;
|
||||||
|
}
|
||||||
|
const lowerBound = typeOpts.unsigned ? 0 : -Math.pow(2, bitLength);
|
||||||
|
const upperBound = Math.pow(2, bitLength) - 1;
|
||||||
|
|
||||||
|
const moduloVal = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength) : Math.pow(2, bitLength);
|
||||||
|
const moduloBound = typeOpts.moduloBitLength ? Math.pow(2, typeOpts.moduloBitLength - 1) : Math.pow(2, bitLength - 1);
|
||||||
|
|
||||||
|
return function(V, opts) {
|
||||||
|
if (!opts) opts = {};
|
||||||
|
|
||||||
|
let x = +V;
|
||||||
|
|
||||||
|
if (opts.enforceRange) {
|
||||||
|
if (!Number.isFinite(x)) {
|
||||||
|
throw new TypeError("Argument is not a finite number");
|
||||||
|
}
|
||||||
|
|
||||||
|
x = sign(x) * Math.floor(Math.abs(x));
|
||||||
|
if (x < lowerBound || x > upperBound) {
|
||||||
|
throw new TypeError("Argument is not in byte range");
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isNaN(x) && opts.clamp) {
|
||||||
|
x = evenRound(x);
|
||||||
|
|
||||||
|
if (x < lowerBound) x = lowerBound;
|
||||||
|
if (x > upperBound) x = upperBound;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Number.isFinite(x) || x === 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
x = sign(x) * Math.floor(Math.abs(x));
|
||||||
|
x = x % moduloVal;
|
||||||
|
|
||||||
|
if (!typeOpts.unsigned && x >= moduloBound) {
|
||||||
|
return x - moduloVal;
|
||||||
|
} else if (typeOpts.unsigned) {
|
||||||
|
if (x < 0) {
|
||||||
|
x += moduloVal;
|
||||||
|
} else if (x === -0) { // don't return negative zero
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conversions["void"] = function () {
|
||||||
|
return undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["boolean"] = function (val) {
|
||||||
|
return !!val;
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["byte"] = createNumberConversion(8, { unsigned: false });
|
||||||
|
conversions["octet"] = createNumberConversion(8, { unsigned: true });
|
||||||
|
|
||||||
|
conversions["short"] = createNumberConversion(16, { unsigned: false });
|
||||||
|
conversions["unsigned short"] = createNumberConversion(16, { unsigned: true });
|
||||||
|
|
||||||
|
conversions["long"] = createNumberConversion(32, { unsigned: false });
|
||||||
|
conversions["unsigned long"] = createNumberConversion(32, { unsigned: true });
|
||||||
|
|
||||||
|
conversions["long long"] = createNumberConversion(32, { unsigned: false, moduloBitLength: 64 });
|
||||||
|
conversions["unsigned long long"] = createNumberConversion(32, { unsigned: true, moduloBitLength: 64 });
|
||||||
|
|
||||||
|
conversions["double"] = function (V) {
|
||||||
|
const x = +V;
|
||||||
|
|
||||||
|
if (!Number.isFinite(x)) {
|
||||||
|
throw new TypeError("Argument is not a finite floating-point value");
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["unrestricted double"] = function (V) {
|
||||||
|
const x = +V;
|
||||||
|
|
||||||
|
if (isNaN(x)) {
|
||||||
|
throw new TypeError("Argument is NaN");
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
// not quite valid, but good enough for JS
|
||||||
|
conversions["float"] = conversions["double"];
|
||||||
|
conversions["unrestricted float"] = conversions["unrestricted double"];
|
||||||
|
|
||||||
|
conversions["DOMString"] = function (V, opts) {
|
||||||
|
if (!opts) opts = {};
|
||||||
|
|
||||||
|
if (opts.treatNullAsEmptyString && V === null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return String(V);
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["ByteString"] = function (V, opts) {
|
||||||
|
const x = String(V);
|
||||||
|
let c = undefined;
|
||||||
|
for (let i = 0; (c = x.codePointAt(i)) !== undefined; ++i) {
|
||||||
|
if (c > 255) {
|
||||||
|
throw new TypeError("Argument is not a valid bytestring");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["USVString"] = function (V) {
|
||||||
|
const S = String(V);
|
||||||
|
const n = S.length;
|
||||||
|
const U = [];
|
||||||
|
for (let i = 0; i < n; ++i) {
|
||||||
|
const c = S.charCodeAt(i);
|
||||||
|
if (c < 0xD800 || c > 0xDFFF) {
|
||||||
|
U.push(String.fromCodePoint(c));
|
||||||
|
} else if (0xDC00 <= c && c <= 0xDFFF) {
|
||||||
|
U.push(String.fromCodePoint(0xFFFD));
|
||||||
|
} else {
|
||||||
|
if (i === n - 1) {
|
||||||
|
U.push(String.fromCodePoint(0xFFFD));
|
||||||
|
} else {
|
||||||
|
const d = S.charCodeAt(i + 1);
|
||||||
|
if (0xDC00 <= d && d <= 0xDFFF) {
|
||||||
|
const a = c & 0x3FF;
|
||||||
|
const b = d & 0x3FF;
|
||||||
|
U.push(String.fromCodePoint((2 << 15) + (2 << 9) * a + b));
|
||||||
|
++i;
|
||||||
|
} else {
|
||||||
|
U.push(String.fromCodePoint(0xFFFD));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return U.join('');
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["Date"] = function (V, opts) {
|
||||||
|
if (!(V instanceof Date)) {
|
||||||
|
throw new TypeError("Argument is not a Date object");
|
||||||
|
}
|
||||||
|
if (isNaN(V)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return V;
|
||||||
|
};
|
||||||
|
|
||||||
|
conversions["RegExp"] = function (V, opts) {
|
||||||
|
if (!(V instanceof RegExp)) {
|
||||||
|
V = new RegExp(V);
|
||||||
|
}
|
||||||
|
|
||||||
|
return V;
|
||||||
|
};
|
||||||
23
node_modules/webidl-conversions/package.json
generated
vendored
Normal file
23
node_modules/webidl-conversions/package.json
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"name": "webidl-conversions",
|
||||||
|
"version": "3.0.1",
|
||||||
|
"description": "Implements the WebIDL algorithms for converting to and from JavaScript values",
|
||||||
|
"main": "lib/index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "mocha test/*.js"
|
||||||
|
},
|
||||||
|
"repository": "jsdom/webidl-conversions",
|
||||||
|
"keywords": [
|
||||||
|
"webidl",
|
||||||
|
"web",
|
||||||
|
"types"
|
||||||
|
],
|
||||||
|
"files": [
|
||||||
|
"lib/"
|
||||||
|
],
|
||||||
|
"author": "Domenic Denicola <d@domenic.me> (https://domenic.me/)",
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^1.21.4"
|
||||||
|
}
|
||||||
|
}
|
||||||
21
node_modules/whatwg-url/LICENSE.txt
generated
vendored
Normal file
21
node_modules/whatwg-url/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2015–2016 Sebastian Mayr
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
67
node_modules/whatwg-url/README.md
generated
vendored
Normal file
67
node_modules/whatwg-url/README.md
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# whatwg-url
|
||||||
|
|
||||||
|
whatwg-url is a full implementation of the WHATWG [URL Standard](https://url.spec.whatwg.org/). It can be used standalone, but it also exposes a lot of the internal algorithms that are useful for integrating a URL parser into a project like [jsdom](https://github.com/tmpvar/jsdom).
|
||||||
|
|
||||||
|
## Current Status
|
||||||
|
|
||||||
|
whatwg-url is currently up to date with the URL spec up to commit [a62223](https://github.com/whatwg/url/commit/a622235308342c9adc7fc2fd1659ff059f7d5e2a).
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### The `URL` Constructor
|
||||||
|
|
||||||
|
The main API is the [`URL`](https://url.spec.whatwg.org/#url) export, which follows the spec's behavior in all ways (including e.g. `USVString` conversion). Most consumers of this library will want to use this.
|
||||||
|
|
||||||
|
### Low-level URL Standard API
|
||||||
|
|
||||||
|
The following methods are exported for use by places like jsdom that need to implement things like [`HTMLHyperlinkElementUtils`](https://html.spec.whatwg.org/#htmlhyperlinkelementutils). They operate on or return an "internal URL" or ["URL record"](https://url.spec.whatwg.org/#concept-url) type.
|
||||||
|
|
||||||
|
- [URL parser](https://url.spec.whatwg.org/#concept-url-parser): `parseURL(input, { baseURL, encodingOverride })`
|
||||||
|
- [Basic URL parser](https://url.spec.whatwg.org/#concept-basic-url-parser): `basicURLParse(input, { baseURL, encodingOverride, url, stateOverride })`
|
||||||
|
- [URL serializer](https://url.spec.whatwg.org/#concept-url-serializer): `serializeURL(urlRecord, excludeFragment)`
|
||||||
|
- [Host serializer](https://url.spec.whatwg.org/#concept-host-serializer): `serializeHost(hostFromURLRecord)`
|
||||||
|
- [Serialize an integer](https://url.spec.whatwg.org/#serialize-an-integer): `serializeInteger(number)`
|
||||||
|
- [Origin](https://url.spec.whatwg.org/#concept-url-origin) [serializer](https://html.spec.whatwg.org/multipage/browsers.html#serialization-of-an-origin): `serializeURLOrigin(urlRecord)`
|
||||||
|
- [Set the username](https://url.spec.whatwg.org/#set-the-username): `setTheUsername(urlRecord, usernameString)`
|
||||||
|
- [Set the password](https://url.spec.whatwg.org/#set-the-password): `setThePassword(urlRecord, passwordString)`
|
||||||
|
- [Cannot have a username/password/port](https://url.spec.whatwg.org/#cannot-have-a-username-password-port): `cannotHaveAUsernamePasswordPort(urlRecord)`
|
||||||
|
|
||||||
|
The `stateOverride` parameter is one of the following strings:
|
||||||
|
|
||||||
|
- [`"scheme start"`](https://url.spec.whatwg.org/#scheme-start-state)
|
||||||
|
- [`"scheme"`](https://url.spec.whatwg.org/#scheme-state)
|
||||||
|
- [`"no scheme"`](https://url.spec.whatwg.org/#no-scheme-state)
|
||||||
|
- [`"special relative or authority"`](https://url.spec.whatwg.org/#special-relative-or-authority-state)
|
||||||
|
- [`"path or authority"`](https://url.spec.whatwg.org/#path-or-authority-state)
|
||||||
|
- [`"relative"`](https://url.spec.whatwg.org/#relative-state)
|
||||||
|
- [`"relative slash"`](https://url.spec.whatwg.org/#relative-slash-state)
|
||||||
|
- [`"special authority slashes"`](https://url.spec.whatwg.org/#special-authority-slashes-state)
|
||||||
|
- [`"special authority ignore slashes"`](https://url.spec.whatwg.org/#special-authority-ignore-slashes-state)
|
||||||
|
- [`"authority"`](https://url.spec.whatwg.org/#authority-state)
|
||||||
|
- [`"host"`](https://url.spec.whatwg.org/#host-state)
|
||||||
|
- [`"hostname"`](https://url.spec.whatwg.org/#hostname-state)
|
||||||
|
- [`"port"`](https://url.spec.whatwg.org/#port-state)
|
||||||
|
- [`"file"`](https://url.spec.whatwg.org/#file-state)
|
||||||
|
- [`"file slash"`](https://url.spec.whatwg.org/#file-slash-state)
|
||||||
|
- [`"file host"`](https://url.spec.whatwg.org/#file-host-state)
|
||||||
|
- [`"path start"`](https://url.spec.whatwg.org/#path-start-state)
|
||||||
|
- [`"path"`](https://url.spec.whatwg.org/#path-state)
|
||||||
|
- [`"cannot-be-a-base-URL path"`](https://url.spec.whatwg.org/#cannot-be-a-base-url-path-state)
|
||||||
|
- [`"query"`](https://url.spec.whatwg.org/#query-state)
|
||||||
|
- [`"fragment"`](https://url.spec.whatwg.org/#fragment-state)
|
||||||
|
|
||||||
|
The URL record type has the following API:
|
||||||
|
|
||||||
|
- [`scheme`](https://url.spec.whatwg.org/#concept-url-scheme)
|
||||||
|
- [`username`](https://url.spec.whatwg.org/#concept-url-username)
|
||||||
|
- [`password`](https://url.spec.whatwg.org/#concept-url-password)
|
||||||
|
- [`host`](https://url.spec.whatwg.org/#concept-url-host)
|
||||||
|
- [`port`](https://url.spec.whatwg.org/#concept-url-port)
|
||||||
|
- [`path`](https://url.spec.whatwg.org/#concept-url-path) (as an array)
|
||||||
|
- [`query`](https://url.spec.whatwg.org/#concept-url-query)
|
||||||
|
- [`fragment`](https://url.spec.whatwg.org/#concept-url-fragment)
|
||||||
|
- [`cannotBeABaseURL`](https://url.spec.whatwg.org/#url-cannot-be-a-base-url-flag) (as a boolean)
|
||||||
|
|
||||||
|
These properties should be treated with care, as in general changing them will cause the URL record to be in an inconsistent state until the appropriate invocation of `basicURLParse` is used to fix it up. You can see examples of this in the URL Standard, where there are many step sequences like "4. Set context object’s url’s fragment to the empty string. 5. Basic URL parse _input_ with context object’s url as _url_ and fragment state as _state override_." In between those two steps, a URL record is in an unusable state.
|
||||||
|
|
||||||
|
The return value of "failure" in the spec is represented by the string `"failure"`. That is, functions like `parseURL` and `basicURLParse` can return _either_ a URL record _or_ the string `"failure"`.
|
||||||
200
node_modules/whatwg-url/lib/URL-impl.js
generated
vendored
Normal file
200
node_modules/whatwg-url/lib/URL-impl.js
generated
vendored
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
"use strict";
|
||||||
|
const usm = require("./url-state-machine");
|
||||||
|
|
||||||
|
exports.implementation = class URLImpl {
|
||||||
|
constructor(constructorArgs) {
|
||||||
|
const url = constructorArgs[0];
|
||||||
|
const base = constructorArgs[1];
|
||||||
|
|
||||||
|
let parsedBase = null;
|
||||||
|
if (base !== undefined) {
|
||||||
|
parsedBase = usm.basicURLParse(base);
|
||||||
|
if (parsedBase === "failure") {
|
||||||
|
throw new TypeError("Invalid base URL");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedURL = usm.basicURLParse(url, { baseURL: parsedBase });
|
||||||
|
if (parsedURL === "failure") {
|
||||||
|
throw new TypeError("Invalid URL");
|
||||||
|
}
|
||||||
|
|
||||||
|
this._url = parsedURL;
|
||||||
|
|
||||||
|
// TODO: query stuff
|
||||||
|
}
|
||||||
|
|
||||||
|
get href() {
|
||||||
|
return usm.serializeURL(this._url);
|
||||||
|
}
|
||||||
|
|
||||||
|
set href(v) {
|
||||||
|
const parsedURL = usm.basicURLParse(v);
|
||||||
|
if (parsedURL === "failure") {
|
||||||
|
throw new TypeError("Invalid URL");
|
||||||
|
}
|
||||||
|
|
||||||
|
this._url = parsedURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
get origin() {
|
||||||
|
return usm.serializeURLOrigin(this._url);
|
||||||
|
}
|
||||||
|
|
||||||
|
get protocol() {
|
||||||
|
return this._url.scheme + ":";
|
||||||
|
}
|
||||||
|
|
||||||
|
set protocol(v) {
|
||||||
|
usm.basicURLParse(v + ":", { url: this._url, stateOverride: "scheme start" });
|
||||||
|
}
|
||||||
|
|
||||||
|
get username() {
|
||||||
|
return this._url.username;
|
||||||
|
}
|
||||||
|
|
||||||
|
set username(v) {
|
||||||
|
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usm.setTheUsername(this._url, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
get password() {
|
||||||
|
return this._url.password;
|
||||||
|
}
|
||||||
|
|
||||||
|
set password(v) {
|
||||||
|
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usm.setThePassword(this._url, v);
|
||||||
|
}
|
||||||
|
|
||||||
|
get host() {
|
||||||
|
const url = this._url;
|
||||||
|
|
||||||
|
if (url.host === null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url.port === null) {
|
||||||
|
return usm.serializeHost(url.host);
|
||||||
|
}
|
||||||
|
|
||||||
|
return usm.serializeHost(url.host) + ":" + usm.serializeInteger(url.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
set host(v) {
|
||||||
|
if (this._url.cannotBeABaseURL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usm.basicURLParse(v, { url: this._url, stateOverride: "host" });
|
||||||
|
}
|
||||||
|
|
||||||
|
get hostname() {
|
||||||
|
if (this._url.host === null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return usm.serializeHost(this._url.host);
|
||||||
|
}
|
||||||
|
|
||||||
|
set hostname(v) {
|
||||||
|
if (this._url.cannotBeABaseURL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
usm.basicURLParse(v, { url: this._url, stateOverride: "hostname" });
|
||||||
|
}
|
||||||
|
|
||||||
|
get port() {
|
||||||
|
if (this._url.port === null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return usm.serializeInteger(this._url.port);
|
||||||
|
}
|
||||||
|
|
||||||
|
set port(v) {
|
||||||
|
if (usm.cannotHaveAUsernamePasswordPort(this._url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v === "") {
|
||||||
|
this._url.port = null;
|
||||||
|
} else {
|
||||||
|
usm.basicURLParse(v, { url: this._url, stateOverride: "port" });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
get pathname() {
|
||||||
|
if (this._url.cannotBeABaseURL) {
|
||||||
|
return this._url.path[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._url.path.length === 0) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "/" + this._url.path.join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
set pathname(v) {
|
||||||
|
if (this._url.cannotBeABaseURL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._url.path = [];
|
||||||
|
usm.basicURLParse(v, { url: this._url, stateOverride: "path start" });
|
||||||
|
}
|
||||||
|
|
||||||
|
get search() {
|
||||||
|
if (this._url.query === null || this._url.query === "") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "?" + this._url.query;
|
||||||
|
}
|
||||||
|
|
||||||
|
set search(v) {
|
||||||
|
// TODO: query stuff
|
||||||
|
|
||||||
|
const url = this._url;
|
||||||
|
|
||||||
|
if (v === "") {
|
||||||
|
url.query = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const input = v[0] === "?" ? v.substring(1) : v;
|
||||||
|
url.query = "";
|
||||||
|
usm.basicURLParse(input, { url, stateOverride: "query" });
|
||||||
|
}
|
||||||
|
|
||||||
|
get hash() {
|
||||||
|
if (this._url.fragment === null || this._url.fragment === "") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return "#" + this._url.fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
set hash(v) {
|
||||||
|
if (v === "") {
|
||||||
|
this._url.fragment = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const input = v[0] === "#" ? v.substring(1) : v;
|
||||||
|
this._url.fragment = "";
|
||||||
|
usm.basicURLParse(input, { url: this._url, stateOverride: "fragment" });
|
||||||
|
}
|
||||||
|
|
||||||
|
toJSON() {
|
||||||
|
return this.href;
|
||||||
|
}
|
||||||
|
};
|
||||||
196
node_modules/whatwg-url/lib/URL.js
generated
vendored
Normal file
196
node_modules/whatwg-url/lib/URL.js
generated
vendored
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const conversions = require("webidl-conversions");
|
||||||
|
const utils = require("./utils.js");
|
||||||
|
const Impl = require(".//URL-impl.js");
|
||||||
|
|
||||||
|
const impl = utils.implSymbol;
|
||||||
|
|
||||||
|
function URL(url) {
|
||||||
|
if (!this || this[impl] || !(this instanceof URL)) {
|
||||||
|
throw new TypeError("Failed to construct 'URL': Please use the 'new' operator, this DOM object constructor cannot be called as a function.");
|
||||||
|
}
|
||||||
|
if (arguments.length < 1) {
|
||||||
|
throw new TypeError("Failed to construct 'URL': 1 argument required, but only " + arguments.length + " present.");
|
||||||
|
}
|
||||||
|
const args = [];
|
||||||
|
for (let i = 0; i < arguments.length && i < 2; ++i) {
|
||||||
|
args[i] = arguments[i];
|
||||||
|
}
|
||||||
|
args[0] = conversions["USVString"](args[0]);
|
||||||
|
if (args[1] !== undefined) {
|
||||||
|
args[1] = conversions["USVString"](args[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports.setup(this, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
URL.prototype.toJSON = function toJSON() {
|
||||||
|
if (!this || !module.exports.is(this)) {
|
||||||
|
throw new TypeError("Illegal invocation");
|
||||||
|
}
|
||||||
|
const args = [];
|
||||||
|
for (let i = 0; i < arguments.length && i < 0; ++i) {
|
||||||
|
args[i] = arguments[i];
|
||||||
|
}
|
||||||
|
return this[impl].toJSON.apply(this[impl], args);
|
||||||
|
};
|
||||||
|
Object.defineProperty(URL.prototype, "href", {
|
||||||
|
get() {
|
||||||
|
return this[impl].href;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].href = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
URL.prototype.toString = function () {
|
||||||
|
if (!this || !module.exports.is(this)) {
|
||||||
|
throw new TypeError("Illegal invocation");
|
||||||
|
}
|
||||||
|
return this.href;
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "origin", {
|
||||||
|
get() {
|
||||||
|
return this[impl].origin;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "protocol", {
|
||||||
|
get() {
|
||||||
|
return this[impl].protocol;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].protocol = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "username", {
|
||||||
|
get() {
|
||||||
|
return this[impl].username;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].username = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "password", {
|
||||||
|
get() {
|
||||||
|
return this[impl].password;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].password = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "host", {
|
||||||
|
get() {
|
||||||
|
return this[impl].host;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].host = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "hostname", {
|
||||||
|
get() {
|
||||||
|
return this[impl].hostname;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].hostname = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "port", {
|
||||||
|
get() {
|
||||||
|
return this[impl].port;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].port = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "pathname", {
|
||||||
|
get() {
|
||||||
|
return this[impl].pathname;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].pathname = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "search", {
|
||||||
|
get() {
|
||||||
|
return this[impl].search;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].search = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.defineProperty(URL.prototype, "hash", {
|
||||||
|
get() {
|
||||||
|
return this[impl].hash;
|
||||||
|
},
|
||||||
|
set(V) {
|
||||||
|
V = conversions["USVString"](V);
|
||||||
|
this[impl].hash = V;
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
is(obj) {
|
||||||
|
return !!obj && obj[impl] instanceof Impl.implementation;
|
||||||
|
},
|
||||||
|
create(constructorArgs, privateData) {
|
||||||
|
let obj = Object.create(URL.prototype);
|
||||||
|
this.setup(obj, constructorArgs, privateData);
|
||||||
|
return obj;
|
||||||
|
},
|
||||||
|
setup(obj, constructorArgs, privateData) {
|
||||||
|
if (!privateData) privateData = {};
|
||||||
|
privateData.wrapper = obj;
|
||||||
|
|
||||||
|
obj[impl] = new Impl.implementation(constructorArgs, privateData);
|
||||||
|
obj[impl][utils.wrapperSymbol] = obj;
|
||||||
|
},
|
||||||
|
interface: URL,
|
||||||
|
expose: {
|
||||||
|
Window: { URL: URL },
|
||||||
|
Worker: { URL: URL }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
11
node_modules/whatwg-url/lib/public-api.js
generated
vendored
Normal file
11
node_modules/whatwg-url/lib/public-api.js
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
exports.URL = require("./URL").interface;
|
||||||
|
exports.serializeURL = require("./url-state-machine").serializeURL;
|
||||||
|
exports.serializeURLOrigin = require("./url-state-machine").serializeURLOrigin;
|
||||||
|
exports.basicURLParse = require("./url-state-machine").basicURLParse;
|
||||||
|
exports.setTheUsername = require("./url-state-machine").setTheUsername;
|
||||||
|
exports.setThePassword = require("./url-state-machine").setThePassword;
|
||||||
|
exports.serializeHost = require("./url-state-machine").serializeHost;
|
||||||
|
exports.serializeInteger = require("./url-state-machine").serializeInteger;
|
||||||
|
exports.parseURL = require("./url-state-machine").parseURL;
|
||||||
1297
node_modules/whatwg-url/lib/url-state-machine.js
generated
vendored
Normal file
1297
node_modules/whatwg-url/lib/url-state-machine.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
20
node_modules/whatwg-url/lib/utils.js
generated
vendored
Normal file
20
node_modules/whatwg-url/lib/utils.js
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
module.exports.mixin = function mixin(target, source) {
|
||||||
|
const keys = Object.getOwnPropertyNames(source);
|
||||||
|
for (let i = 0; i < keys.length; ++i) {
|
||||||
|
Object.defineProperty(target, keys[i], Object.getOwnPropertyDescriptor(source, keys[i]));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.wrapperSymbol = Symbol("wrapper");
|
||||||
|
module.exports.implSymbol = Symbol("impl");
|
||||||
|
|
||||||
|
module.exports.wrapperForImpl = function (impl) {
|
||||||
|
return impl[module.exports.wrapperSymbol];
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.implForWrapper = function (wrapper) {
|
||||||
|
return wrapper[module.exports.implSymbol];
|
||||||
|
};
|
||||||
|
|
||||||
32
node_modules/whatwg-url/package.json
generated
vendored
Normal file
32
node_modules/whatwg-url/package.json
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"name": "whatwg-url",
|
||||||
|
"version": "5.0.0",
|
||||||
|
"description": "An implementation of the WHATWG URL Standard's URL API and parsing machinery",
|
||||||
|
"main": "lib/public-api.js",
|
||||||
|
"files": [
|
||||||
|
"lib/"
|
||||||
|
],
|
||||||
|
"author": "Sebastian Mayr <github@smayr.name>",
|
||||||
|
"license": "MIT",
|
||||||
|
"repository": "jsdom/whatwg-url",
|
||||||
|
"dependencies": {
|
||||||
|
"tr46": "~0.0.3",
|
||||||
|
"webidl-conversions": "^3.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^2.6.0",
|
||||||
|
"istanbul": "~0.4.3",
|
||||||
|
"mocha": "^2.2.4",
|
||||||
|
"recast": "~0.10.29",
|
||||||
|
"request": "^2.55.0",
|
||||||
|
"webidl2js": "^3.0.2"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "node scripts/transform.js && node scripts/convert-idl.js",
|
||||||
|
"coverage": "istanbul cover node_modules/mocha/bin/_mocha",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"prepublish": "npm run build",
|
||||||
|
"pretest": "node scripts/get-latest-platform-tests.js && npm run build",
|
||||||
|
"test": "mocha"
|
||||||
|
}
|
||||||
|
}
|
||||||
19
node_modules/ws/LICENSE
generated
vendored
Normal file
19
node_modules/ws/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
489
node_modules/ws/README.md
generated
vendored
Normal file
489
node_modules/ws/README.md
generated
vendored
Normal file
@@ -0,0 +1,489 @@
|
|||||||
|
# ws: a Node.js WebSocket library
|
||||||
|
|
||||||
|
[](https://www.npmjs.com/package/ws)
|
||||||
|
[](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)
|
||||||
|
[](https://coveralls.io/github/websockets/ws)
|
||||||
|
|
||||||
|
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and
|
||||||
|
server implementation.
|
||||||
|
|
||||||
|
Passes the quite extensive Autobahn test suite: [server][server-report],
|
||||||
|
[client][client-report].
|
||||||
|
|
||||||
|
**Note**: This module does not work in the browser. The client in the docs is a
|
||||||
|
reference to a back end with the role of a client in the WebSocket
|
||||||
|
communication. Browser clients must use the native
|
||||||
|
[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/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](https://github.com/heineiuo/isomorphic-ws).
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [Protocol support](#protocol-support)
|
||||||
|
- [Installing](#installing)
|
||||||
|
- [Opt-in for performance](#opt-in-for-performance)
|
||||||
|
- [API docs](#api-docs)
|
||||||
|
- [WebSocket compression](#websocket-compression)
|
||||||
|
- [Usage examples](#usage-examples)
|
||||||
|
- [Sending and receiving text data](#sending-and-receiving-text-data)
|
||||||
|
- [Sending binary data](#sending-binary-data)
|
||||||
|
- [Simple server](#simple-server)
|
||||||
|
- [External HTTP/S server](#external-https-server)
|
||||||
|
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
|
||||||
|
- [Client authentication](#client-authentication)
|
||||||
|
- [Server broadcast](#server-broadcast)
|
||||||
|
- [Round-trip time](#round-trip-time)
|
||||||
|
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
|
||||||
|
- [Other examples](#other-examples)
|
||||||
|
- [FAQ](#faq)
|
||||||
|
- [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
|
||||||
|
- [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
|
||||||
|
- [How to connect via a proxy?](#how-to-connect-via-a-proxy)
|
||||||
|
- [Changelog](#changelog)
|
||||||
|
- [License](#license)
|
||||||
|
|
||||||
|
## Protocol support
|
||||||
|
|
||||||
|
- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
|
||||||
|
- **HyBi drafts 13-17** (Current default, alternatively option
|
||||||
|
`protocolVersion: 13`)
|
||||||
|
|
||||||
|
## Installing
|
||||||
|
|
||||||
|
```
|
||||||
|
npm install ws
|
||||||
|
```
|
||||||
|
|
||||||
|
### Opt-in for performance
|
||||||
|
|
||||||
|
There are 2 optional modules that can be installed along side with the ws
|
||||||
|
module. These modules are binary addons which improve certain operations.
|
||||||
|
Prebuilt binaries are available for the most popular platforms so you don't
|
||||||
|
necessarily need to have a C++ compiler installed on your machine.
|
||||||
|
|
||||||
|
- `npm install --save-optional bufferutil`: Allows to efficiently perform
|
||||||
|
operations such as masking and unmasking the data payload of the WebSocket
|
||||||
|
frames.
|
||||||
|
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
|
||||||
|
message contains valid UTF-8.
|
||||||
|
|
||||||
|
## API docs
|
||||||
|
|
||||||
|
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
|
||||||
|
utility functions.
|
||||||
|
|
||||||
|
## WebSocket compression
|
||||||
|
|
||||||
|
ws supports the [permessage-deflate extension][permessage-deflate] which enables
|
||||||
|
the client and server to negotiate a compression algorithm and its parameters,
|
||||||
|
and then selectively apply it to the data payloads of each WebSocket message.
|
||||||
|
|
||||||
|
The extension is disabled by default on the server and enabled by default on the
|
||||||
|
client. It adds a significant overhead in terms of performance and memory
|
||||||
|
consumption so we suggest to enable it only if it is really needed.
|
||||||
|
|
||||||
|
Note that Node.js has a variety of issues with high-performance compression,
|
||||||
|
where increased concurrency, especially on Linux, can lead to [catastrophic
|
||||||
|
memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
|
||||||
|
permessage-deflate in production, it is worthwhile to set up a test
|
||||||
|
representative of your workload and ensure Node.js/zlib will handle it with
|
||||||
|
acceptable performance and memory usage.
|
||||||
|
|
||||||
|
Tuning of permessage-deflate can be done via the options defined below. You can
|
||||||
|
also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
|
||||||
|
into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
|
||||||
|
|
||||||
|
See [the docs][ws-server-options] for more options.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket, { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({
|
||||||
|
port: 8080,
|
||||||
|
perMessageDeflate: {
|
||||||
|
zlibDeflateOptions: {
|
||||||
|
// See zlib defaults.
|
||||||
|
chunkSize: 1024,
|
||||||
|
memLevel: 7,
|
||||||
|
level: 3
|
||||||
|
},
|
||||||
|
zlibInflateOptions: {
|
||||||
|
chunkSize: 10 * 1024
|
||||||
|
},
|
||||||
|
// Other options settable:
|
||||||
|
clientNoContextTakeover: true, // Defaults to negotiated value.
|
||||||
|
serverNoContextTakeover: true, // Defaults to negotiated value.
|
||||||
|
serverMaxWindowBits: 10, // Defaults to negotiated value.
|
||||||
|
// Below options specified as default values.
|
||||||
|
concurrencyLimit: 10, // Limits zlib concurrency for perf.
|
||||||
|
threshold: 1024 // Size (in bytes) below which messages
|
||||||
|
// should not be compressed if context takeover is disabled.
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
The client will only use the extension if it is supported and enabled on the
|
||||||
|
server. To always disable the extension on the client set the
|
||||||
|
`perMessageDeflate` option to `false`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
const ws = new WebSocket('ws://www.host.com/path', {
|
||||||
|
perMessageDeflate: false
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage examples
|
||||||
|
|
||||||
|
### Sending and receiving text data
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
const ws = new WebSocket('ws://www.host.com/path');
|
||||||
|
|
||||||
|
ws.on('open', function open() {
|
||||||
|
ws.send('something');
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', function message(data) {
|
||||||
|
console.log('received: %s', data);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sending binary data
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
const ws = new WebSocket('ws://www.host.com/path');
|
||||||
|
|
||||||
|
ws.on('open', function open() {
|
||||||
|
const array = new Float32Array(5);
|
||||||
|
|
||||||
|
for (var i = 0; i < array.length; ++i) {
|
||||||
|
array[i] = i / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.send(array);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Simple server
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({ port: 8080 });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.on('message', function message(data) {
|
||||||
|
console.log('received: %s', data);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.send('something');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### External HTTP/S server
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { createServer } from 'https';
|
||||||
|
import { readFileSync } from 'fs';
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const server = createServer({
|
||||||
|
cert: readFileSync('/path/to/cert.pem'),
|
||||||
|
key: readFileSync('/path/to/key.pem')
|
||||||
|
});
|
||||||
|
const wss = new WebSocketServer({ server });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.on('message', function message(data) {
|
||||||
|
console.log('received: %s', data);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.send('something');
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(8080);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multiple servers sharing a single HTTP/S server
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { createServer } from 'http';
|
||||||
|
import { parse } from 'url';
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const server = createServer();
|
||||||
|
const wss1 = new WebSocketServer({ noServer: true });
|
||||||
|
const wss2 = new WebSocketServer({ noServer: true });
|
||||||
|
|
||||||
|
wss1.on('connection', function connection(ws) {
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
|
||||||
|
wss2.on('connection', function connection(ws) {
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('upgrade', function upgrade(request, socket, head) {
|
||||||
|
const { pathname } = parse(request.url);
|
||||||
|
|
||||||
|
if (pathname === '/foo') {
|
||||||
|
wss1.handleUpgrade(request, socket, head, function done(ws) {
|
||||||
|
wss1.emit('connection', ws, request);
|
||||||
|
});
|
||||||
|
} else if (pathname === '/bar') {
|
||||||
|
wss2.handleUpgrade(request, socket, head, function done(ws) {
|
||||||
|
wss2.emit('connection', ws, request);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
socket.destroy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(8080);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Client authentication
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
import { createServer } from 'http';
|
||||||
|
|
||||||
|
const server = createServer();
|
||||||
|
const wss = new WebSocketServer({ noServer: true });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws, request, client) {
|
||||||
|
ws.on('message', function message(data) {
|
||||||
|
console.log(`Received message ${data} from user ${client}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.on('upgrade', function upgrade(request, socket, head) {
|
||||||
|
// This function is not defined on purpose. Implement it with your own logic.
|
||||||
|
authenticate(request, function next(err, client) {
|
||||||
|
if (err || !client) {
|
||||||
|
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
||||||
|
socket.destroy();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wss.handleUpgrade(request, socket, head, function done(ws) {
|
||||||
|
wss.emit('connection', ws, request, client);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.listen(8080);
|
||||||
|
```
|
||||||
|
|
||||||
|
Also see the provided [example][session-parse-example] using `express-session`.
|
||||||
|
|
||||||
|
### Server broadcast
|
||||||
|
|
||||||
|
A client WebSocket broadcasting to all connected WebSocket clients, including
|
||||||
|
itself.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket, { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({ port: 8080 });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.on('message', function message(data, isBinary) {
|
||||||
|
wss.clients.forEach(function each(client) {
|
||||||
|
if (client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(data, { binary: isBinary });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
A client WebSocket broadcasting to every other connected WebSocket clients,
|
||||||
|
excluding itself.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket, { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({ port: 8080 });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.on('message', function message(data, isBinary) {
|
||||||
|
wss.clients.forEach(function each(client) {
|
||||||
|
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
||||||
|
client.send(data, { binary: isBinary });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Round-trip time
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
const ws = new WebSocket('wss://websocket-echo.com/');
|
||||||
|
|
||||||
|
ws.on('open', function open() {
|
||||||
|
console.log('connected');
|
||||||
|
ws.send(Date.now());
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('close', function close() {
|
||||||
|
console.log('disconnected');
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', function message(data) {
|
||||||
|
console.log(`Round-trip time: ${Date.now() - data} ms`);
|
||||||
|
|
||||||
|
setTimeout(function timeout() {
|
||||||
|
ws.send(Date.now());
|
||||||
|
}, 500);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Use the Node.js streams API
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket, { createWebSocketStream } from 'ws';
|
||||||
|
|
||||||
|
const ws = new WebSocket('wss://websocket-echo.com/');
|
||||||
|
|
||||||
|
const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
|
||||||
|
|
||||||
|
duplex.pipe(process.stdout);
|
||||||
|
process.stdin.pipe(duplex);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Other examples
|
||||||
|
|
||||||
|
For a full example with a browser client communicating with a ws server, see the
|
||||||
|
examples folder.
|
||||||
|
|
||||||
|
Otherwise, see the test cases.
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
### How to get the IP address of the client?
|
||||||
|
|
||||||
|
The remote IP address can be obtained from the raw socket.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({ port: 8080 });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws, req) {
|
||||||
|
const ip = req.socket.remoteAddress;
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
When the server runs behind a proxy like NGINX, the de-facto standard is to use
|
||||||
|
the `X-Forwarded-For` header.
|
||||||
|
|
||||||
|
```js
|
||||||
|
wss.on('connection', function connection(ws, req) {
|
||||||
|
const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### How to detect and close broken connections?
|
||||||
|
|
||||||
|
Sometimes the link between the server and the client can be interrupted in a way
|
||||||
|
that keeps both the server and the client unaware of the broken state of the
|
||||||
|
connection (e.g. when pulling the cord).
|
||||||
|
|
||||||
|
In these cases ping messages can be used as a means to verify that the remote
|
||||||
|
endpoint is still responsive.
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { WebSocketServer } from 'ws';
|
||||||
|
|
||||||
|
function heartbeat() {
|
||||||
|
this.isAlive = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const wss = new WebSocketServer({ port: 8080 });
|
||||||
|
|
||||||
|
wss.on('connection', function connection(ws) {
|
||||||
|
ws.isAlive = true;
|
||||||
|
ws.on('pong', heartbeat);
|
||||||
|
});
|
||||||
|
|
||||||
|
const interval = setInterval(function ping() {
|
||||||
|
wss.clients.forEach(function each(ws) {
|
||||||
|
if (ws.isAlive === false) return ws.terminate();
|
||||||
|
|
||||||
|
ws.isAlive = false;
|
||||||
|
ws.ping();
|
||||||
|
});
|
||||||
|
}, 30000);
|
||||||
|
|
||||||
|
wss.on('close', function close() {
|
||||||
|
clearInterval(interval);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Pong messages are automatically sent in response to ping messages as required by
|
||||||
|
the spec.
|
||||||
|
|
||||||
|
Just like the server example above your clients might as well lose connection
|
||||||
|
without knowing it. You might want to add a ping listener on your clients to
|
||||||
|
prevent that. A simple implementation would be:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import WebSocket from 'ws';
|
||||||
|
|
||||||
|
function heartbeat() {
|
||||||
|
clearTimeout(this.pingTimeout);
|
||||||
|
|
||||||
|
// Use `WebSocket#terminate()`, which immediately destroys the connection,
|
||||||
|
// instead of `WebSocket#close()`, which waits for the close timer.
|
||||||
|
// Delay should be equal to the interval at which your server
|
||||||
|
// sends out pings plus a conservative assumption of the latency.
|
||||||
|
this.pingTimeout = setTimeout(() => {
|
||||||
|
this.terminate();
|
||||||
|
}, 30000 + 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = new WebSocket('wss://websocket-echo.com/');
|
||||||
|
|
||||||
|
client.on('open', heartbeat);
|
||||||
|
client.on('ping', heartbeat);
|
||||||
|
client.on('close', function clear() {
|
||||||
|
clearTimeout(this.pingTimeout);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### How to connect via a proxy?
|
||||||
|
|
||||||
|
Use a custom `http.Agent` implementation like [https-proxy-agent][] or
|
||||||
|
[socks-proxy-agent][].
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
We're using the GitHub [releases][changelog] for changelog entries.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
||||||
|
|
||||||
|
[changelog]: https://github.com/websockets/ws/releases
|
||||||
|
[client-report]: http://websockets.github.io/ws/autobahn/clients/
|
||||||
|
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
|
||||||
|
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
|
||||||
|
[node-zlib-deflaterawdocs]:
|
||||||
|
https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
|
||||||
|
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
|
||||||
|
[server-report]: http://websockets.github.io/ws/autobahn/servers/
|
||||||
|
[session-parse-example]: ./examples/express-session-parse
|
||||||
|
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
|
||||||
|
[ws-server-options]:
|
||||||
|
https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
|
||||||
8
node_modules/ws/browser.js
generated
vendored
Normal file
8
node_modules/ws/browser.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function () {
|
||||||
|
throw new Error(
|
||||||
|
'ws does not work in the browser. Browser clients must use the native ' +
|
||||||
|
'WebSocket object'
|
||||||
|
);
|
||||||
|
};
|
||||||
13
node_modules/ws/index.js
generated
vendored
Normal file
13
node_modules/ws/index.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const WebSocket = require('./lib/websocket');
|
||||||
|
|
||||||
|
WebSocket.createWebSocketStream = require('./lib/stream');
|
||||||
|
WebSocket.Server = require('./lib/websocket-server');
|
||||||
|
WebSocket.Receiver = require('./lib/receiver');
|
||||||
|
WebSocket.Sender = require('./lib/sender');
|
||||||
|
|
||||||
|
WebSocket.WebSocket = WebSocket;
|
||||||
|
WebSocket.WebSocketServer = WebSocket.Server;
|
||||||
|
|
||||||
|
module.exports = WebSocket;
|
||||||
126
node_modules/ws/lib/buffer-util.js
generated
vendored
Normal file
126
node_modules/ws/lib/buffer-util.js
generated
vendored
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { EMPTY_BUFFER } = require('./constants');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Merges an array of buffers into a new buffer.
|
||||||
|
*
|
||||||
|
* @param {Buffer[]} list The array of buffers to concat
|
||||||
|
* @param {Number} totalLength The total length of buffers in the list
|
||||||
|
* @return {Buffer} The resulting buffer
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function concat(list, totalLength) {
|
||||||
|
if (list.length === 0) return EMPTY_BUFFER;
|
||||||
|
if (list.length === 1) return list[0];
|
||||||
|
|
||||||
|
const target = Buffer.allocUnsafe(totalLength);
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < list.length; i++) {
|
||||||
|
const buf = list[i];
|
||||||
|
target.set(buf, offset);
|
||||||
|
offset += buf.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset < totalLength) return target.slice(0, offset);
|
||||||
|
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Masks a buffer using the given mask.
|
||||||
|
*
|
||||||
|
* @param {Buffer} source The buffer to mask
|
||||||
|
* @param {Buffer} mask The mask to use
|
||||||
|
* @param {Buffer} output The buffer where to store the result
|
||||||
|
* @param {Number} offset The offset at which to start writing
|
||||||
|
* @param {Number} length The number of bytes to mask.
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function _mask(source, mask, output, offset, length) {
|
||||||
|
for (let i = 0; i < length; i++) {
|
||||||
|
output[offset + i] = source[i] ^ mask[i & 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unmasks a buffer using the given mask.
|
||||||
|
*
|
||||||
|
* @param {Buffer} buffer The buffer to unmask
|
||||||
|
* @param {Buffer} mask The mask to use
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function _unmask(buffer, mask) {
|
||||||
|
for (let i = 0; i < buffer.length; i++) {
|
||||||
|
buffer[i] ^= mask[i & 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a buffer to an `ArrayBuffer`.
|
||||||
|
*
|
||||||
|
* @param {Buffer} buf The buffer to convert
|
||||||
|
* @return {ArrayBuffer} Converted buffer
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function toArrayBuffer(buf) {
|
||||||
|
if (buf.byteLength === buf.buffer.byteLength) {
|
||||||
|
return buf.buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts `data` to a `Buffer`.
|
||||||
|
*
|
||||||
|
* @param {*} data The data to convert
|
||||||
|
* @return {Buffer} The buffer
|
||||||
|
* @throws {TypeError}
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function toBuffer(data) {
|
||||||
|
toBuffer.readOnly = true;
|
||||||
|
|
||||||
|
if (Buffer.isBuffer(data)) return data;
|
||||||
|
|
||||||
|
let buf;
|
||||||
|
|
||||||
|
if (data instanceof ArrayBuffer) {
|
||||||
|
buf = Buffer.from(data);
|
||||||
|
} else if (ArrayBuffer.isView(data)) {
|
||||||
|
buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
||||||
|
} else {
|
||||||
|
buf = Buffer.from(data);
|
||||||
|
toBuffer.readOnly = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const bufferUtil = require('bufferutil');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
concat,
|
||||||
|
mask(source, mask, output, offset, length) {
|
||||||
|
if (length < 48) _mask(source, mask, output, offset, length);
|
||||||
|
else bufferUtil.mask(source, mask, output, offset, length);
|
||||||
|
},
|
||||||
|
toArrayBuffer,
|
||||||
|
toBuffer,
|
||||||
|
unmask(buffer, mask) {
|
||||||
|
if (buffer.length < 32) _unmask(buffer, mask);
|
||||||
|
else bufferUtil.unmask(buffer, mask);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} catch (e) /* istanbul ignore next */ {
|
||||||
|
module.exports = {
|
||||||
|
concat,
|
||||||
|
mask: _mask,
|
||||||
|
toArrayBuffer,
|
||||||
|
toBuffer,
|
||||||
|
unmask: _unmask
|
||||||
|
};
|
||||||
|
}
|
||||||
12
node_modules/ws/lib/constants.js
generated
vendored
Normal file
12
node_modules/ws/lib/constants.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
|
||||||
|
EMPTY_BUFFER: Buffer.alloc(0),
|
||||||
|
GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
|
||||||
|
kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
|
||||||
|
kListener: Symbol('kListener'),
|
||||||
|
kStatusCode: Symbol('status-code'),
|
||||||
|
kWebSocket: Symbol('websocket'),
|
||||||
|
NOOP: () => {}
|
||||||
|
};
|
||||||
266
node_modules/ws/lib/event-target.js
generated
vendored
Normal file
266
node_modules/ws/lib/event-target.js
generated
vendored
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { kForOnEventAttribute, kListener } = require('./constants');
|
||||||
|
|
||||||
|
const kCode = Symbol('kCode');
|
||||||
|
const kData = Symbol('kData');
|
||||||
|
const kError = Symbol('kError');
|
||||||
|
const kMessage = Symbol('kMessage');
|
||||||
|
const kReason = Symbol('kReason');
|
||||||
|
const kTarget = Symbol('kTarget');
|
||||||
|
const kType = Symbol('kType');
|
||||||
|
const kWasClean = Symbol('kWasClean');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing an event.
|
||||||
|
*/
|
||||||
|
class Event {
|
||||||
|
/**
|
||||||
|
* Create a new `Event`.
|
||||||
|
*
|
||||||
|
* @param {String} type The name of the event
|
||||||
|
* @throws {TypeError} If the `type` argument is not specified
|
||||||
|
*/
|
||||||
|
constructor(type) {
|
||||||
|
this[kTarget] = null;
|
||||||
|
this[kType] = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {*}
|
||||||
|
*/
|
||||||
|
get target() {
|
||||||
|
return this[kTarget];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
get type() {
|
||||||
|
return this[kType];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(Event.prototype, 'target', { enumerable: true });
|
||||||
|
Object.defineProperty(Event.prototype, 'type', { enumerable: true });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing a close event.
|
||||||
|
*
|
||||||
|
* @extends Event
|
||||||
|
*/
|
||||||
|
class CloseEvent extends Event {
|
||||||
|
/**
|
||||||
|
* Create a new `CloseEvent`.
|
||||||
|
*
|
||||||
|
* @param {String} type The name of the event
|
||||||
|
* @param {Object} [options] A dictionary object that allows for setting
|
||||||
|
* attributes via object members of the same name
|
||||||
|
* @param {Number} [options.code=0] The status code explaining why the
|
||||||
|
* connection was closed
|
||||||
|
* @param {String} [options.reason=''] A human-readable string explaining why
|
||||||
|
* the connection was closed
|
||||||
|
* @param {Boolean} [options.wasClean=false] Indicates whether or not the
|
||||||
|
* connection was cleanly closed
|
||||||
|
*/
|
||||||
|
constructor(type, options = {}) {
|
||||||
|
super(type);
|
||||||
|
|
||||||
|
this[kCode] = options.code === undefined ? 0 : options.code;
|
||||||
|
this[kReason] = options.reason === undefined ? '' : options.reason;
|
||||||
|
this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Number}
|
||||||
|
*/
|
||||||
|
get code() {
|
||||||
|
return this[kCode];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
get reason() {
|
||||||
|
return this[kReason];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {Boolean}
|
||||||
|
*/
|
||||||
|
get wasClean() {
|
||||||
|
return this[kWasClean];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });
|
||||||
|
Object.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });
|
||||||
|
Object.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing an error event.
|
||||||
|
*
|
||||||
|
* @extends Event
|
||||||
|
*/
|
||||||
|
class ErrorEvent extends Event {
|
||||||
|
/**
|
||||||
|
* Create a new `ErrorEvent`.
|
||||||
|
*
|
||||||
|
* @param {String} type The name of the event
|
||||||
|
* @param {Object} [options] A dictionary object that allows for setting
|
||||||
|
* attributes via object members of the same name
|
||||||
|
* @param {*} [options.error=null] The error that generated this event
|
||||||
|
* @param {String} [options.message=''] The error message
|
||||||
|
*/
|
||||||
|
constructor(type, options = {}) {
|
||||||
|
super(type);
|
||||||
|
|
||||||
|
this[kError] = options.error === undefined ? null : options.error;
|
||||||
|
this[kMessage] = options.message === undefined ? '' : options.message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {*}
|
||||||
|
*/
|
||||||
|
get error() {
|
||||||
|
return this[kError];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
get message() {
|
||||||
|
return this[kMessage];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });
|
||||||
|
Object.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing a message event.
|
||||||
|
*
|
||||||
|
* @extends Event
|
||||||
|
*/
|
||||||
|
class MessageEvent extends Event {
|
||||||
|
/**
|
||||||
|
* Create a new `MessageEvent`.
|
||||||
|
*
|
||||||
|
* @param {String} type The name of the event
|
||||||
|
* @param {Object} [options] A dictionary object that allows for setting
|
||||||
|
* attributes via object members of the same name
|
||||||
|
* @param {*} [options.data=null] The message content
|
||||||
|
*/
|
||||||
|
constructor(type, options = {}) {
|
||||||
|
super(type);
|
||||||
|
|
||||||
|
this[kData] = options.data === undefined ? null : options.data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {*}
|
||||||
|
*/
|
||||||
|
get data() {
|
||||||
|
return this[kData];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This provides methods for emulating the `EventTarget` interface. It's not
|
||||||
|
* meant to be used directly.
|
||||||
|
*
|
||||||
|
* @mixin
|
||||||
|
*/
|
||||||
|
const EventTarget = {
|
||||||
|
/**
|
||||||
|
* Register an event listener.
|
||||||
|
*
|
||||||
|
* @param {String} type A string representing the event type to listen for
|
||||||
|
* @param {Function} listener The listener to add
|
||||||
|
* @param {Object} [options] An options object specifies characteristics about
|
||||||
|
* the event listener
|
||||||
|
* @param {Boolean} [options.once=false] A `Boolean` indicating that the
|
||||||
|
* listener should be invoked at most once after being added. If `true`,
|
||||||
|
* the listener would be automatically removed when invoked.
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
addEventListener(type, listener, options = {}) {
|
||||||
|
let wrapper;
|
||||||
|
|
||||||
|
if (type === 'message') {
|
||||||
|
wrapper = function onMessage(data, isBinary) {
|
||||||
|
const event = new MessageEvent('message', {
|
||||||
|
data: isBinary ? data : data.toString()
|
||||||
|
});
|
||||||
|
|
||||||
|
event[kTarget] = this;
|
||||||
|
listener.call(this, event);
|
||||||
|
};
|
||||||
|
} else if (type === 'close') {
|
||||||
|
wrapper = function onClose(code, message) {
|
||||||
|
const event = new CloseEvent('close', {
|
||||||
|
code,
|
||||||
|
reason: message.toString(),
|
||||||
|
wasClean: this._closeFrameReceived && this._closeFrameSent
|
||||||
|
});
|
||||||
|
|
||||||
|
event[kTarget] = this;
|
||||||
|
listener.call(this, event);
|
||||||
|
};
|
||||||
|
} else if (type === 'error') {
|
||||||
|
wrapper = function onError(error) {
|
||||||
|
const event = new ErrorEvent('error', {
|
||||||
|
error,
|
||||||
|
message: error.message
|
||||||
|
});
|
||||||
|
|
||||||
|
event[kTarget] = this;
|
||||||
|
listener.call(this, event);
|
||||||
|
};
|
||||||
|
} else if (type === 'open') {
|
||||||
|
wrapper = function onOpen() {
|
||||||
|
const event = new Event('open');
|
||||||
|
|
||||||
|
event[kTarget] = this;
|
||||||
|
listener.call(this, event);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapper[kForOnEventAttribute] = !!options[kForOnEventAttribute];
|
||||||
|
wrapper[kListener] = listener;
|
||||||
|
|
||||||
|
if (options.once) {
|
||||||
|
this.once(type, wrapper);
|
||||||
|
} else {
|
||||||
|
this.on(type, wrapper);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an event listener.
|
||||||
|
*
|
||||||
|
* @param {String} type A string representing the event type to remove
|
||||||
|
* @param {Function} handler The listener to remove
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
removeEventListener(type, handler) {
|
||||||
|
for (const listener of this.listeners(type)) {
|
||||||
|
if (listener[kListener] === handler && !listener[kForOnEventAttribute]) {
|
||||||
|
this.removeListener(type, listener);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
CloseEvent,
|
||||||
|
ErrorEvent,
|
||||||
|
Event,
|
||||||
|
EventTarget,
|
||||||
|
MessageEvent
|
||||||
|
};
|
||||||
203
node_modules/ws/lib/extension.js
generated
vendored
Normal file
203
node_modules/ws/lib/extension.js
generated
vendored
Normal file
@@ -0,0 +1,203 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { tokenChars } = require('./validation');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an offer to the map of extension offers or a parameter to the map of
|
||||||
|
* parameters.
|
||||||
|
*
|
||||||
|
* @param {Object} dest The map of extension offers or parameters
|
||||||
|
* @param {String} name The extension or parameter name
|
||||||
|
* @param {(Object|Boolean|String)} elem The extension parameters or the
|
||||||
|
* parameter value
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function push(dest, name, elem) {
|
||||||
|
if (dest[name] === undefined) dest[name] = [elem];
|
||||||
|
else dest[name].push(elem);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the `Sec-WebSocket-Extensions` header into an object.
|
||||||
|
*
|
||||||
|
* @param {String} header The field value of the header
|
||||||
|
* @return {Object} The parsed object
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function parse(header) {
|
||||||
|
const offers = Object.create(null);
|
||||||
|
let params = Object.create(null);
|
||||||
|
let mustUnescape = false;
|
||||||
|
let isEscaping = false;
|
||||||
|
let inQuotes = false;
|
||||||
|
let extensionName;
|
||||||
|
let paramName;
|
||||||
|
let start = -1;
|
||||||
|
let code = -1;
|
||||||
|
let end = -1;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
for (; i < header.length; i++) {
|
||||||
|
code = header.charCodeAt(i);
|
||||||
|
|
||||||
|
if (extensionName === undefined) {
|
||||||
|
if (end === -1 && tokenChars[code] === 1) {
|
||||||
|
if (start === -1) start = i;
|
||||||
|
} else if (
|
||||||
|
i !== 0 &&
|
||||||
|
(code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
|
||||||
|
) {
|
||||||
|
if (end === -1 && start !== -1) end = i;
|
||||||
|
} else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
|
||||||
|
if (start === -1) {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1) end = i;
|
||||||
|
const name = header.slice(start, end);
|
||||||
|
if (code === 0x2c) {
|
||||||
|
push(offers, name, params);
|
||||||
|
params = Object.create(null);
|
||||||
|
} else {
|
||||||
|
extensionName = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
start = end = -1;
|
||||||
|
} else {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
} else if (paramName === undefined) {
|
||||||
|
if (end === -1 && tokenChars[code] === 1) {
|
||||||
|
if (start === -1) start = i;
|
||||||
|
} else if (code === 0x20 || code === 0x09) {
|
||||||
|
if (end === -1 && start !== -1) end = i;
|
||||||
|
} else if (code === 0x3b || code === 0x2c) {
|
||||||
|
if (start === -1) {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1) end = i;
|
||||||
|
push(params, header.slice(start, end), true);
|
||||||
|
if (code === 0x2c) {
|
||||||
|
push(offers, extensionName, params);
|
||||||
|
params = Object.create(null);
|
||||||
|
extensionName = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
start = end = -1;
|
||||||
|
} else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
|
||||||
|
paramName = header.slice(start, i);
|
||||||
|
start = end = -1;
|
||||||
|
} else {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//
|
||||||
|
// The value of a quoted-string after unescaping must conform to the
|
||||||
|
// token ABNF, so only token characters are valid.
|
||||||
|
// Ref: https://tools.ietf.org/html/rfc6455#section-9.1
|
||||||
|
//
|
||||||
|
if (isEscaping) {
|
||||||
|
if (tokenChars[code] !== 1) {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
if (start === -1) start = i;
|
||||||
|
else if (!mustUnescape) mustUnescape = true;
|
||||||
|
isEscaping = false;
|
||||||
|
} else if (inQuotes) {
|
||||||
|
if (tokenChars[code] === 1) {
|
||||||
|
if (start === -1) start = i;
|
||||||
|
} else if (code === 0x22 /* '"' */ && start !== -1) {
|
||||||
|
inQuotes = false;
|
||||||
|
end = i;
|
||||||
|
} else if (code === 0x5c /* '\' */) {
|
||||||
|
isEscaping = true;
|
||||||
|
} else {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
} else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
|
||||||
|
inQuotes = true;
|
||||||
|
} else if (end === -1 && tokenChars[code] === 1) {
|
||||||
|
if (start === -1) start = i;
|
||||||
|
} else if (start !== -1 && (code === 0x20 || code === 0x09)) {
|
||||||
|
if (end === -1) end = i;
|
||||||
|
} else if (code === 0x3b || code === 0x2c) {
|
||||||
|
if (start === -1) {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1) end = i;
|
||||||
|
let value = header.slice(start, end);
|
||||||
|
if (mustUnescape) {
|
||||||
|
value = value.replace(/\\/g, '');
|
||||||
|
mustUnescape = false;
|
||||||
|
}
|
||||||
|
push(params, paramName, value);
|
||||||
|
if (code === 0x2c) {
|
||||||
|
push(offers, extensionName, params);
|
||||||
|
params = Object.create(null);
|
||||||
|
extensionName = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
paramName = undefined;
|
||||||
|
start = end = -1;
|
||||||
|
} else {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {
|
||||||
|
throw new SyntaxError('Unexpected end of input');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1) end = i;
|
||||||
|
const token = header.slice(start, end);
|
||||||
|
if (extensionName === undefined) {
|
||||||
|
push(offers, token, params);
|
||||||
|
} else {
|
||||||
|
if (paramName === undefined) {
|
||||||
|
push(params, token, true);
|
||||||
|
} else if (mustUnescape) {
|
||||||
|
push(params, paramName, token.replace(/\\/g, ''));
|
||||||
|
} else {
|
||||||
|
push(params, paramName, token);
|
||||||
|
}
|
||||||
|
push(offers, extensionName, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
return offers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the `Sec-WebSocket-Extensions` header field value.
|
||||||
|
*
|
||||||
|
* @param {Object} extensions The map of extensions and parameters to format
|
||||||
|
* @return {String} A string representing the given object
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function format(extensions) {
|
||||||
|
return Object.keys(extensions)
|
||||||
|
.map((extension) => {
|
||||||
|
let configurations = extensions[extension];
|
||||||
|
if (!Array.isArray(configurations)) configurations = [configurations];
|
||||||
|
return configurations
|
||||||
|
.map((params) => {
|
||||||
|
return [extension]
|
||||||
|
.concat(
|
||||||
|
Object.keys(params).map((k) => {
|
||||||
|
let values = params[k];
|
||||||
|
if (!Array.isArray(values)) values = [values];
|
||||||
|
return values
|
||||||
|
.map((v) => (v === true ? k : `${k}=${v}`))
|
||||||
|
.join('; ');
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.join('; ');
|
||||||
|
})
|
||||||
|
.join(', ');
|
||||||
|
})
|
||||||
|
.join(', ');
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { format, parse };
|
||||||
55
node_modules/ws/lib/limiter.js
generated
vendored
Normal file
55
node_modules/ws/lib/limiter.js
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const kDone = Symbol('kDone');
|
||||||
|
const kRun = Symbol('kRun');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A very simple job queue with adjustable concurrency. Adapted from
|
||||||
|
* https://github.com/STRML/async-limiter
|
||||||
|
*/
|
||||||
|
class Limiter {
|
||||||
|
/**
|
||||||
|
* Creates a new `Limiter`.
|
||||||
|
*
|
||||||
|
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
||||||
|
* to run concurrently
|
||||||
|
*/
|
||||||
|
constructor(concurrency) {
|
||||||
|
this[kDone] = () => {
|
||||||
|
this.pending--;
|
||||||
|
this[kRun]();
|
||||||
|
};
|
||||||
|
this.concurrency = concurrency || Infinity;
|
||||||
|
this.jobs = [];
|
||||||
|
this.pending = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a job to the queue.
|
||||||
|
*
|
||||||
|
* @param {Function} job The job to run
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
add(job) {
|
||||||
|
this.jobs.push(job);
|
||||||
|
this[kRun]();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a job from the queue and runs it if possible.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
[kRun]() {
|
||||||
|
if (this.pending === this.concurrency) return;
|
||||||
|
|
||||||
|
if (this.jobs.length) {
|
||||||
|
const job = this.jobs.shift();
|
||||||
|
|
||||||
|
this.pending++;
|
||||||
|
job(this[kDone]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Limiter;
|
||||||
511
node_modules/ws/lib/permessage-deflate.js
generated
vendored
Normal file
511
node_modules/ws/lib/permessage-deflate.js
generated
vendored
Normal file
@@ -0,0 +1,511 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const zlib = require('zlib');
|
||||||
|
|
||||||
|
const bufferUtil = require('./buffer-util');
|
||||||
|
const Limiter = require('./limiter');
|
||||||
|
const { kStatusCode } = require('./constants');
|
||||||
|
|
||||||
|
const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
|
||||||
|
const kPerMessageDeflate = Symbol('permessage-deflate');
|
||||||
|
const kTotalLength = Symbol('total-length');
|
||||||
|
const kCallback = Symbol('callback');
|
||||||
|
const kBuffers = Symbol('buffers');
|
||||||
|
const kError = Symbol('error');
|
||||||
|
|
||||||
|
//
|
||||||
|
// We limit zlib concurrency, which prevents severe memory fragmentation
|
||||||
|
// as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
|
||||||
|
// and https://github.com/websockets/ws/issues/1202
|
||||||
|
//
|
||||||
|
// Intentionally global; it's the global thread pool that's an issue.
|
||||||
|
//
|
||||||
|
let zlibLimiter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* permessage-deflate implementation.
|
||||||
|
*/
|
||||||
|
class PerMessageDeflate {
|
||||||
|
/**
|
||||||
|
* Creates a PerMessageDeflate instance.
|
||||||
|
*
|
||||||
|
* @param {Object} [options] Configuration options
|
||||||
|
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
||||||
|
* for, or request, a custom client window size
|
||||||
|
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
||||||
|
* acknowledge disabling of client context takeover
|
||||||
|
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
||||||
|
* calls to zlib
|
||||||
|
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
||||||
|
* use of a custom server window size
|
||||||
|
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
||||||
|
* disabling of server context takeover
|
||||||
|
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
||||||
|
* messages should not be compressed if context takeover is disabled
|
||||||
|
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
||||||
|
* deflate
|
||||||
|
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
||||||
|
* inflate
|
||||||
|
* @param {Boolean} [isServer=false] Create the instance in either server or
|
||||||
|
* client mode
|
||||||
|
* @param {Number} [maxPayload=0] The maximum allowed message length
|
||||||
|
*/
|
||||||
|
constructor(options, isServer, maxPayload) {
|
||||||
|
this._maxPayload = maxPayload | 0;
|
||||||
|
this._options = options || {};
|
||||||
|
this._threshold =
|
||||||
|
this._options.threshold !== undefined ? this._options.threshold : 1024;
|
||||||
|
this._isServer = !!isServer;
|
||||||
|
this._deflate = null;
|
||||||
|
this._inflate = null;
|
||||||
|
|
||||||
|
this.params = null;
|
||||||
|
|
||||||
|
if (!zlibLimiter) {
|
||||||
|
const concurrency =
|
||||||
|
this._options.concurrencyLimit !== undefined
|
||||||
|
? this._options.concurrencyLimit
|
||||||
|
: 10;
|
||||||
|
zlibLimiter = new Limiter(concurrency);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {String}
|
||||||
|
*/
|
||||||
|
static get extensionName() {
|
||||||
|
return 'permessage-deflate';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an extension negotiation offer.
|
||||||
|
*
|
||||||
|
* @return {Object} Extension parameters
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
offer() {
|
||||||
|
const params = {};
|
||||||
|
|
||||||
|
if (this._options.serverNoContextTakeover) {
|
||||||
|
params.server_no_context_takeover = true;
|
||||||
|
}
|
||||||
|
if (this._options.clientNoContextTakeover) {
|
||||||
|
params.client_no_context_takeover = true;
|
||||||
|
}
|
||||||
|
if (this._options.serverMaxWindowBits) {
|
||||||
|
params.server_max_window_bits = this._options.serverMaxWindowBits;
|
||||||
|
}
|
||||||
|
if (this._options.clientMaxWindowBits) {
|
||||||
|
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
||||||
|
} else if (this._options.clientMaxWindowBits == null) {
|
||||||
|
params.client_max_window_bits = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept an extension negotiation offer/response.
|
||||||
|
*
|
||||||
|
* @param {Array} configurations The extension negotiation offers/reponse
|
||||||
|
* @return {Object} Accepted configuration
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
accept(configurations) {
|
||||||
|
configurations = this.normalizeParams(configurations);
|
||||||
|
|
||||||
|
this.params = this._isServer
|
||||||
|
? this.acceptAsServer(configurations)
|
||||||
|
: this.acceptAsClient(configurations);
|
||||||
|
|
||||||
|
return this.params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Releases all resources used by the extension.
|
||||||
|
*
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
cleanup() {
|
||||||
|
if (this._inflate) {
|
||||||
|
this._inflate.close();
|
||||||
|
this._inflate = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._deflate) {
|
||||||
|
const callback = this._deflate[kCallback];
|
||||||
|
|
||||||
|
this._deflate.close();
|
||||||
|
this._deflate = null;
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
callback(
|
||||||
|
new Error(
|
||||||
|
'The deflate stream was closed while data was being processed'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept an extension negotiation offer.
|
||||||
|
*
|
||||||
|
* @param {Array} offers The extension negotiation offers
|
||||||
|
* @return {Object} Accepted configuration
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
acceptAsServer(offers) {
|
||||||
|
const opts = this._options;
|
||||||
|
const accepted = offers.find((params) => {
|
||||||
|
if (
|
||||||
|
(opts.serverNoContextTakeover === false &&
|
||||||
|
params.server_no_context_takeover) ||
|
||||||
|
(params.server_max_window_bits &&
|
||||||
|
(opts.serverMaxWindowBits === false ||
|
||||||
|
(typeof opts.serverMaxWindowBits === 'number' &&
|
||||||
|
opts.serverMaxWindowBits > params.server_max_window_bits))) ||
|
||||||
|
(typeof opts.clientMaxWindowBits === 'number' &&
|
||||||
|
!params.client_max_window_bits)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!accepted) {
|
||||||
|
throw new Error('None of the extension offers can be accepted');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opts.serverNoContextTakeover) {
|
||||||
|
accepted.server_no_context_takeover = true;
|
||||||
|
}
|
||||||
|
if (opts.clientNoContextTakeover) {
|
||||||
|
accepted.client_no_context_takeover = true;
|
||||||
|
}
|
||||||
|
if (typeof opts.serverMaxWindowBits === 'number') {
|
||||||
|
accepted.server_max_window_bits = opts.serverMaxWindowBits;
|
||||||
|
}
|
||||||
|
if (typeof opts.clientMaxWindowBits === 'number') {
|
||||||
|
accepted.client_max_window_bits = opts.clientMaxWindowBits;
|
||||||
|
} else if (
|
||||||
|
accepted.client_max_window_bits === true ||
|
||||||
|
opts.clientMaxWindowBits === false
|
||||||
|
) {
|
||||||
|
delete accepted.client_max_window_bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
return accepted;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accept the extension negotiation response.
|
||||||
|
*
|
||||||
|
* @param {Array} response The extension negotiation response
|
||||||
|
* @return {Object} Accepted configuration
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
acceptAsClient(response) {
|
||||||
|
const params = response[0];
|
||||||
|
|
||||||
|
if (
|
||||||
|
this._options.clientNoContextTakeover === false &&
|
||||||
|
params.client_no_context_takeover
|
||||||
|
) {
|
||||||
|
throw new Error('Unexpected parameter "client_no_context_takeover"');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!params.client_max_window_bits) {
|
||||||
|
if (typeof this._options.clientMaxWindowBits === 'number') {
|
||||||
|
params.client_max_window_bits = this._options.clientMaxWindowBits;
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
this._options.clientMaxWindowBits === false ||
|
||||||
|
(typeof this._options.clientMaxWindowBits === 'number' &&
|
||||||
|
params.client_max_window_bits > this._options.clientMaxWindowBits)
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
'Unexpected or invalid parameter "client_max_window_bits"'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize parameters.
|
||||||
|
*
|
||||||
|
* @param {Array} configurations The extension negotiation offers/reponse
|
||||||
|
* @return {Array} The offers/response with normalized parameters
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
normalizeParams(configurations) {
|
||||||
|
configurations.forEach((params) => {
|
||||||
|
Object.keys(params).forEach((key) => {
|
||||||
|
let value = params[key];
|
||||||
|
|
||||||
|
if (value.length > 1) {
|
||||||
|
throw new Error(`Parameter "${key}" must have only a single value`);
|
||||||
|
}
|
||||||
|
|
||||||
|
value = value[0];
|
||||||
|
|
||||||
|
if (key === 'client_max_window_bits') {
|
||||||
|
if (value !== true) {
|
||||||
|
const num = +value;
|
||||||
|
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Invalid value for parameter "${key}": ${value}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
value = num;
|
||||||
|
} else if (!this._isServer) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Invalid value for parameter "${key}": ${value}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (key === 'server_max_window_bits') {
|
||||||
|
const num = +value;
|
||||||
|
if (!Number.isInteger(num) || num < 8 || num > 15) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Invalid value for parameter "${key}": ${value}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
value = num;
|
||||||
|
} else if (
|
||||||
|
key === 'client_no_context_takeover' ||
|
||||||
|
key === 'server_no_context_takeover'
|
||||||
|
) {
|
||||||
|
if (value !== true) {
|
||||||
|
throw new TypeError(
|
||||||
|
`Invalid value for parameter "${key}": ${value}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown parameter "${key}"`);
|
||||||
|
}
|
||||||
|
|
||||||
|
params[key] = value;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return configurations;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decompress data. Concurrency limited.
|
||||||
|
*
|
||||||
|
* @param {Buffer} data Compressed data
|
||||||
|
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
||||||
|
* @param {Function} callback Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
decompress(data, fin, callback) {
|
||||||
|
zlibLimiter.add((done) => {
|
||||||
|
this._decompress(data, fin, (err, result) => {
|
||||||
|
done();
|
||||||
|
callback(err, result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compress data. Concurrency limited.
|
||||||
|
*
|
||||||
|
* @param {(Buffer|String)} data Data to compress
|
||||||
|
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
||||||
|
* @param {Function} callback Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
compress(data, fin, callback) {
|
||||||
|
zlibLimiter.add((done) => {
|
||||||
|
this._compress(data, fin, (err, result) => {
|
||||||
|
done();
|
||||||
|
callback(err, result);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decompress data.
|
||||||
|
*
|
||||||
|
* @param {Buffer} data Compressed data
|
||||||
|
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
||||||
|
* @param {Function} callback Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_decompress(data, fin, callback) {
|
||||||
|
const endpoint = this._isServer ? 'client' : 'server';
|
||||||
|
|
||||||
|
if (!this._inflate) {
|
||||||
|
const key = `${endpoint}_max_window_bits`;
|
||||||
|
const windowBits =
|
||||||
|
typeof this.params[key] !== 'number'
|
||||||
|
? zlib.Z_DEFAULT_WINDOWBITS
|
||||||
|
: this.params[key];
|
||||||
|
|
||||||
|
this._inflate = zlib.createInflateRaw({
|
||||||
|
...this._options.zlibInflateOptions,
|
||||||
|
windowBits
|
||||||
|
});
|
||||||
|
this._inflate[kPerMessageDeflate] = this;
|
||||||
|
this._inflate[kTotalLength] = 0;
|
||||||
|
this._inflate[kBuffers] = [];
|
||||||
|
this._inflate.on('error', inflateOnError);
|
||||||
|
this._inflate.on('data', inflateOnData);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._inflate[kCallback] = callback;
|
||||||
|
|
||||||
|
this._inflate.write(data);
|
||||||
|
if (fin) this._inflate.write(TRAILER);
|
||||||
|
|
||||||
|
this._inflate.flush(() => {
|
||||||
|
const err = this._inflate[kError];
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
this._inflate.close();
|
||||||
|
this._inflate = null;
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = bufferUtil.concat(
|
||||||
|
this._inflate[kBuffers],
|
||||||
|
this._inflate[kTotalLength]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (this._inflate._readableState.endEmitted) {
|
||||||
|
this._inflate.close();
|
||||||
|
this._inflate = null;
|
||||||
|
} else {
|
||||||
|
this._inflate[kTotalLength] = 0;
|
||||||
|
this._inflate[kBuffers] = [];
|
||||||
|
|
||||||
|
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
||||||
|
this._inflate.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compress data.
|
||||||
|
*
|
||||||
|
* @param {(Buffer|String)} data Data to compress
|
||||||
|
* @param {Boolean} fin Specifies whether or not this is the last fragment
|
||||||
|
* @param {Function} callback Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_compress(data, fin, callback) {
|
||||||
|
const endpoint = this._isServer ? 'server' : 'client';
|
||||||
|
|
||||||
|
if (!this._deflate) {
|
||||||
|
const key = `${endpoint}_max_window_bits`;
|
||||||
|
const windowBits =
|
||||||
|
typeof this.params[key] !== 'number'
|
||||||
|
? zlib.Z_DEFAULT_WINDOWBITS
|
||||||
|
: this.params[key];
|
||||||
|
|
||||||
|
this._deflate = zlib.createDeflateRaw({
|
||||||
|
...this._options.zlibDeflateOptions,
|
||||||
|
windowBits
|
||||||
|
});
|
||||||
|
|
||||||
|
this._deflate[kTotalLength] = 0;
|
||||||
|
this._deflate[kBuffers] = [];
|
||||||
|
|
||||||
|
this._deflate.on('data', deflateOnData);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._deflate[kCallback] = callback;
|
||||||
|
|
||||||
|
this._deflate.write(data);
|
||||||
|
this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
|
||||||
|
if (!this._deflate) {
|
||||||
|
//
|
||||||
|
// The deflate stream was closed while data was being processed.
|
||||||
|
//
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = bufferUtil.concat(
|
||||||
|
this._deflate[kBuffers],
|
||||||
|
this._deflate[kTotalLength]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (fin) data = data.slice(0, data.length - 4);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Ensure that the callback will not be called again in
|
||||||
|
// `PerMessageDeflate#cleanup()`.
|
||||||
|
//
|
||||||
|
this._deflate[kCallback] = null;
|
||||||
|
|
||||||
|
this._deflate[kTotalLength] = 0;
|
||||||
|
this._deflate[kBuffers] = [];
|
||||||
|
|
||||||
|
if (fin && this.params[`${endpoint}_no_context_takeover`]) {
|
||||||
|
this._deflate.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = PerMessageDeflate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener of the `zlib.DeflateRaw` stream `'data'` event.
|
||||||
|
*
|
||||||
|
* @param {Buffer} chunk A chunk of data
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function deflateOnData(chunk) {
|
||||||
|
this[kBuffers].push(chunk);
|
||||||
|
this[kTotalLength] += chunk.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener of the `zlib.InflateRaw` stream `'data'` event.
|
||||||
|
*
|
||||||
|
* @param {Buffer} chunk A chunk of data
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function inflateOnData(chunk) {
|
||||||
|
this[kTotalLength] += chunk.length;
|
||||||
|
|
||||||
|
if (
|
||||||
|
this[kPerMessageDeflate]._maxPayload < 1 ||
|
||||||
|
this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
|
||||||
|
) {
|
||||||
|
this[kBuffers].push(chunk);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this[kError] = new RangeError('Max payload size exceeded');
|
||||||
|
this[kError].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
|
||||||
|
this[kError][kStatusCode] = 1009;
|
||||||
|
this.removeListener('data', inflateOnData);
|
||||||
|
this.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener of the `zlib.InflateRaw` stream `'error'` event.
|
||||||
|
*
|
||||||
|
* @param {Error} err The emitted error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function inflateOnError(err) {
|
||||||
|
//
|
||||||
|
// There is no need to call `Zlib#close()` as the handle is automatically
|
||||||
|
// closed when an error is emitted.
|
||||||
|
//
|
||||||
|
this[kPerMessageDeflate]._inflate = null;
|
||||||
|
err[kStatusCode] = 1007;
|
||||||
|
this[kCallback](err);
|
||||||
|
}
|
||||||
618
node_modules/ws/lib/receiver.js
generated
vendored
Normal file
618
node_modules/ws/lib/receiver.js
generated
vendored
Normal file
@@ -0,0 +1,618 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { Writable } = require('stream');
|
||||||
|
|
||||||
|
const PerMessageDeflate = require('./permessage-deflate');
|
||||||
|
const {
|
||||||
|
BINARY_TYPES,
|
||||||
|
EMPTY_BUFFER,
|
||||||
|
kStatusCode,
|
||||||
|
kWebSocket
|
||||||
|
} = require('./constants');
|
||||||
|
const { concat, toArrayBuffer, unmask } = require('./buffer-util');
|
||||||
|
const { isValidStatusCode, isValidUTF8 } = require('./validation');
|
||||||
|
|
||||||
|
const GET_INFO = 0;
|
||||||
|
const GET_PAYLOAD_LENGTH_16 = 1;
|
||||||
|
const GET_PAYLOAD_LENGTH_64 = 2;
|
||||||
|
const GET_MASK = 3;
|
||||||
|
const GET_DATA = 4;
|
||||||
|
const INFLATING = 5;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HyBi Receiver implementation.
|
||||||
|
*
|
||||||
|
* @extends Writable
|
||||||
|
*/
|
||||||
|
class Receiver extends Writable {
|
||||||
|
/**
|
||||||
|
* Creates a Receiver instance.
|
||||||
|
*
|
||||||
|
* @param {Object} [options] Options object
|
||||||
|
* @param {String} [options.binaryType=nodebuffer] The type for binary data
|
||||||
|
* @param {Object} [options.extensions] An object containing the negotiated
|
||||||
|
* extensions
|
||||||
|
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
||||||
|
* client or server mode
|
||||||
|
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
||||||
|
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
||||||
|
* not to skip UTF-8 validation for text and close messages
|
||||||
|
*/
|
||||||
|
constructor(options = {}) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
||||||
|
this._extensions = options.extensions || {};
|
||||||
|
this._isServer = !!options.isServer;
|
||||||
|
this._maxPayload = options.maxPayload | 0;
|
||||||
|
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
||||||
|
this[kWebSocket] = undefined;
|
||||||
|
|
||||||
|
this._bufferedBytes = 0;
|
||||||
|
this._buffers = [];
|
||||||
|
|
||||||
|
this._compressed = false;
|
||||||
|
this._payloadLength = 0;
|
||||||
|
this._mask = undefined;
|
||||||
|
this._fragmented = 0;
|
||||||
|
this._masked = false;
|
||||||
|
this._fin = false;
|
||||||
|
this._opcode = 0;
|
||||||
|
|
||||||
|
this._totalPayloadLength = 0;
|
||||||
|
this._messageLength = 0;
|
||||||
|
this._fragments = [];
|
||||||
|
|
||||||
|
this._state = GET_INFO;
|
||||||
|
this._loop = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implements `Writable.prototype._write()`.
|
||||||
|
*
|
||||||
|
* @param {Buffer} chunk The chunk of data to write
|
||||||
|
* @param {String} encoding The character encoding of `chunk`
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_write(chunk, encoding, cb) {
|
||||||
|
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
|
||||||
|
|
||||||
|
this._bufferedBytes += chunk.length;
|
||||||
|
this._buffers.push(chunk);
|
||||||
|
this.startLoop(cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Consumes `n` bytes from the buffered data.
|
||||||
|
*
|
||||||
|
* @param {Number} n The number of bytes to consume
|
||||||
|
* @return {Buffer} The consumed bytes
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
consume(n) {
|
||||||
|
this._bufferedBytes -= n;
|
||||||
|
|
||||||
|
if (n === this._buffers[0].length) return this._buffers.shift();
|
||||||
|
|
||||||
|
if (n < this._buffers[0].length) {
|
||||||
|
const buf = this._buffers[0];
|
||||||
|
this._buffers[0] = buf.slice(n);
|
||||||
|
return buf.slice(0, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
const dst = Buffer.allocUnsafe(n);
|
||||||
|
|
||||||
|
do {
|
||||||
|
const buf = this._buffers[0];
|
||||||
|
const offset = dst.length - n;
|
||||||
|
|
||||||
|
if (n >= buf.length) {
|
||||||
|
dst.set(this._buffers.shift(), offset);
|
||||||
|
} else {
|
||||||
|
dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
|
||||||
|
this._buffers[0] = buf.slice(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
n -= buf.length;
|
||||||
|
} while (n > 0);
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the parsing loop.
|
||||||
|
*
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
startLoop(cb) {
|
||||||
|
let err;
|
||||||
|
this._loop = true;
|
||||||
|
|
||||||
|
do {
|
||||||
|
switch (this._state) {
|
||||||
|
case GET_INFO:
|
||||||
|
err = this.getInfo();
|
||||||
|
break;
|
||||||
|
case GET_PAYLOAD_LENGTH_16:
|
||||||
|
err = this.getPayloadLength16();
|
||||||
|
break;
|
||||||
|
case GET_PAYLOAD_LENGTH_64:
|
||||||
|
err = this.getPayloadLength64();
|
||||||
|
break;
|
||||||
|
case GET_MASK:
|
||||||
|
this.getMask();
|
||||||
|
break;
|
||||||
|
case GET_DATA:
|
||||||
|
err = this.getData(cb);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// `INFLATING`
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} while (this._loop);
|
||||||
|
|
||||||
|
cb(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads the first two bytes of a frame.
|
||||||
|
*
|
||||||
|
* @return {(RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
getInfo() {
|
||||||
|
if (this._bufferedBytes < 2) {
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buf = this.consume(2);
|
||||||
|
|
||||||
|
if ((buf[0] & 0x30) !== 0x00) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'RSV2 and RSV3 must be clear',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_UNEXPECTED_RSV_2_3'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const compressed = (buf[0] & 0x40) === 0x40;
|
||||||
|
|
||||||
|
if (compressed && !this._extensions[PerMessageDeflate.extensionName]) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'RSV1 must be clear',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_UNEXPECTED_RSV_1'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._fin = (buf[0] & 0x80) === 0x80;
|
||||||
|
this._opcode = buf[0] & 0x0f;
|
||||||
|
this._payloadLength = buf[1] & 0x7f;
|
||||||
|
|
||||||
|
if (this._opcode === 0x00) {
|
||||||
|
if (compressed) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'RSV1 must be clear',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_UNEXPECTED_RSV_1'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._fragmented) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'invalid opcode 0',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_OPCODE'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._opcode = this._fragmented;
|
||||||
|
} else if (this._opcode === 0x01 || this._opcode === 0x02) {
|
||||||
|
if (this._fragmented) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
`invalid opcode ${this._opcode}`,
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_OPCODE'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._compressed = compressed;
|
||||||
|
} else if (this._opcode > 0x07 && this._opcode < 0x0b) {
|
||||||
|
if (!this._fin) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'FIN must be set',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_EXPECTED_FIN'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (compressed) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'RSV1 must be clear',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_UNEXPECTED_RSV_1'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._payloadLength > 0x7d) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
`invalid payload length ${this._payloadLength}`,
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
`invalid opcode ${this._opcode}`,
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_OPCODE'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
|
||||||
|
this._masked = (buf[1] & 0x80) === 0x80;
|
||||||
|
|
||||||
|
if (this._isServer) {
|
||||||
|
if (!this._masked) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'MASK must be set',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_EXPECTED_MASK'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else if (this._masked) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'MASK must be clear',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_UNEXPECTED_MASK'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
|
||||||
|
else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
|
||||||
|
else return this.haveLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets extended payload length (7+16).
|
||||||
|
*
|
||||||
|
* @return {(RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
getPayloadLength16() {
|
||||||
|
if (this._bufferedBytes < 2) {
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._payloadLength = this.consume(2).readUInt16BE(0);
|
||||||
|
return this.haveLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets extended payload length (7+64).
|
||||||
|
*
|
||||||
|
* @return {(RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
getPayloadLength64() {
|
||||||
|
if (this._bufferedBytes < 8) {
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const buf = this.consume(8);
|
||||||
|
const num = buf.readUInt32BE(0);
|
||||||
|
|
||||||
|
//
|
||||||
|
// The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
|
||||||
|
// if payload length is greater than this number.
|
||||||
|
//
|
||||||
|
if (num > Math.pow(2, 53 - 32) - 1) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'Unsupported WebSocket frame: payload length > 2^53 - 1',
|
||||||
|
false,
|
||||||
|
1009,
|
||||||
|
'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
|
||||||
|
return this.haveLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Payload length has been read.
|
||||||
|
*
|
||||||
|
* @return {(RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
haveLength() {
|
||||||
|
if (this._payloadLength && this._opcode < 0x08) {
|
||||||
|
this._totalPayloadLength += this._payloadLength;
|
||||||
|
if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'Max payload size exceeded',
|
||||||
|
false,
|
||||||
|
1009,
|
||||||
|
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._masked) this._state = GET_MASK;
|
||||||
|
else this._state = GET_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads mask bytes.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
getMask() {
|
||||||
|
if (this._bufferedBytes < 4) {
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._mask = this.consume(4);
|
||||||
|
this._state = GET_DATA;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads data bytes.
|
||||||
|
*
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @return {(Error|RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
getData(cb) {
|
||||||
|
let data = EMPTY_BUFFER;
|
||||||
|
|
||||||
|
if (this._payloadLength) {
|
||||||
|
if (this._bufferedBytes < this._payloadLength) {
|
||||||
|
this._loop = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
data = this.consume(this._payloadLength);
|
||||||
|
|
||||||
|
if (
|
||||||
|
this._masked &&
|
||||||
|
(this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0
|
||||||
|
) {
|
||||||
|
unmask(data, this._mask);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._opcode > 0x07) return this.controlMessage(data);
|
||||||
|
|
||||||
|
if (this._compressed) {
|
||||||
|
this._state = INFLATING;
|
||||||
|
this.decompress(data, cb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.length) {
|
||||||
|
//
|
||||||
|
// This message is not compressed so its length is the sum of the payload
|
||||||
|
// length of all fragments.
|
||||||
|
//
|
||||||
|
this._messageLength = this._totalPayloadLength;
|
||||||
|
this._fragments.push(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.dataMessage();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decompresses data.
|
||||||
|
*
|
||||||
|
* @param {Buffer} data Compressed data
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
decompress(data, cb) {
|
||||||
|
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||||
|
|
||||||
|
perMessageDeflate.decompress(data, this._fin, (err, buf) => {
|
||||||
|
if (err) return cb(err);
|
||||||
|
|
||||||
|
if (buf.length) {
|
||||||
|
this._messageLength += buf.length;
|
||||||
|
if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
|
||||||
|
return cb(
|
||||||
|
error(
|
||||||
|
RangeError,
|
||||||
|
'Max payload size exceeded',
|
||||||
|
false,
|
||||||
|
1009,
|
||||||
|
'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._fragments.push(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
const er = this.dataMessage();
|
||||||
|
if (er) return cb(er);
|
||||||
|
|
||||||
|
this.startLoop(cb);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a data message.
|
||||||
|
*
|
||||||
|
* @return {(Error|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
dataMessage() {
|
||||||
|
if (this._fin) {
|
||||||
|
const messageLength = this._messageLength;
|
||||||
|
const fragments = this._fragments;
|
||||||
|
|
||||||
|
this._totalPayloadLength = 0;
|
||||||
|
this._messageLength = 0;
|
||||||
|
this._fragmented = 0;
|
||||||
|
this._fragments = [];
|
||||||
|
|
||||||
|
if (this._opcode === 2) {
|
||||||
|
let data;
|
||||||
|
|
||||||
|
if (this._binaryType === 'nodebuffer') {
|
||||||
|
data = concat(fragments, messageLength);
|
||||||
|
} else if (this._binaryType === 'arraybuffer') {
|
||||||
|
data = toArrayBuffer(concat(fragments, messageLength));
|
||||||
|
} else {
|
||||||
|
data = fragments;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit('message', data, true);
|
||||||
|
} else {
|
||||||
|
const buf = concat(fragments, messageLength);
|
||||||
|
|
||||||
|
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
||||||
|
this._loop = false;
|
||||||
|
return error(
|
||||||
|
Error,
|
||||||
|
'invalid UTF-8 sequence',
|
||||||
|
true,
|
||||||
|
1007,
|
||||||
|
'WS_ERR_INVALID_UTF8'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit('message', buf, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this._state = GET_INFO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles a control message.
|
||||||
|
*
|
||||||
|
* @param {Buffer} data Data to handle
|
||||||
|
* @return {(Error|RangeError|undefined)} A possible error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
controlMessage(data) {
|
||||||
|
if (this._opcode === 0x08) {
|
||||||
|
this._loop = false;
|
||||||
|
|
||||||
|
if (data.length === 0) {
|
||||||
|
this.emit('conclude', 1005, EMPTY_BUFFER);
|
||||||
|
this.end();
|
||||||
|
} else if (data.length === 1) {
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
'invalid payload length 1',
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const code = data.readUInt16BE(0);
|
||||||
|
|
||||||
|
if (!isValidStatusCode(code)) {
|
||||||
|
return error(
|
||||||
|
RangeError,
|
||||||
|
`invalid status code ${code}`,
|
||||||
|
true,
|
||||||
|
1002,
|
||||||
|
'WS_ERR_INVALID_CLOSE_CODE'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const buf = data.slice(2);
|
||||||
|
|
||||||
|
if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
|
||||||
|
return error(
|
||||||
|
Error,
|
||||||
|
'invalid UTF-8 sequence',
|
||||||
|
true,
|
||||||
|
1007,
|
||||||
|
'WS_ERR_INVALID_UTF8'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.emit('conclude', code, buf);
|
||||||
|
this.end();
|
||||||
|
}
|
||||||
|
} else if (this._opcode === 0x09) {
|
||||||
|
this.emit('ping', data);
|
||||||
|
} else {
|
||||||
|
this.emit('pong', data);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._state = GET_INFO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Receiver;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds an error object.
|
||||||
|
*
|
||||||
|
* @param {function(new:Error|RangeError)} ErrorCtor The error constructor
|
||||||
|
* @param {String} message The error message
|
||||||
|
* @param {Boolean} prefix Specifies whether or not to add a default prefix to
|
||||||
|
* `message`
|
||||||
|
* @param {Number} statusCode The status code
|
||||||
|
* @param {String} errorCode The exposed error code
|
||||||
|
* @return {(Error|RangeError)} The error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function error(ErrorCtor, message, prefix, statusCode, errorCode) {
|
||||||
|
const err = new ErrorCtor(
|
||||||
|
prefix ? `Invalid WebSocket frame: ${message}` : message
|
||||||
|
);
|
||||||
|
|
||||||
|
Error.captureStackTrace(err, error);
|
||||||
|
err.code = errorCode;
|
||||||
|
err[kStatusCode] = statusCode;
|
||||||
|
return err;
|
||||||
|
}
|
||||||
478
node_modules/ws/lib/sender.js
generated
vendored
Normal file
478
node_modules/ws/lib/sender.js
generated
vendored
Normal file
@@ -0,0 +1,478 @@
|
|||||||
|
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls$" }] */
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const net = require('net');
|
||||||
|
const tls = require('tls');
|
||||||
|
const { randomFillSync } = require('crypto');
|
||||||
|
|
||||||
|
const PerMessageDeflate = require('./permessage-deflate');
|
||||||
|
const { EMPTY_BUFFER } = require('./constants');
|
||||||
|
const { isValidStatusCode } = require('./validation');
|
||||||
|
const { mask: applyMask, toBuffer } = require('./buffer-util');
|
||||||
|
|
||||||
|
const kByteLength = Symbol('kByteLength');
|
||||||
|
const maskBuffer = Buffer.alloc(4);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HyBi Sender implementation.
|
||||||
|
*/
|
||||||
|
class Sender {
|
||||||
|
/**
|
||||||
|
* Creates a Sender instance.
|
||||||
|
*
|
||||||
|
* @param {(net.Socket|tls.Socket)} socket The connection socket
|
||||||
|
* @param {Object} [extensions] An object containing the negotiated extensions
|
||||||
|
* @param {Function} [generateMask] The function used to generate the masking
|
||||||
|
* key
|
||||||
|
*/
|
||||||
|
constructor(socket, extensions, generateMask) {
|
||||||
|
this._extensions = extensions || {};
|
||||||
|
|
||||||
|
if (generateMask) {
|
||||||
|
this._generateMask = generateMask;
|
||||||
|
this._maskBuffer = Buffer.alloc(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
this._socket = socket;
|
||||||
|
|
||||||
|
this._firstFragment = true;
|
||||||
|
this._compress = false;
|
||||||
|
|
||||||
|
this._bufferedBytes = 0;
|
||||||
|
this._deflating = false;
|
||||||
|
this._queue = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Frames a piece of data according to the HyBi WebSocket protocol.
|
||||||
|
*
|
||||||
|
* @param {(Buffer|String)} data The data to frame
|
||||||
|
* @param {Object} options Options object
|
||||||
|
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
||||||
|
* FIN bit
|
||||||
|
* @param {Function} [options.generateMask] The function used to generate the
|
||||||
|
* masking key
|
||||||
|
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||||
|
* `data`
|
||||||
|
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
||||||
|
* key
|
||||||
|
* @param {Number} options.opcode The opcode
|
||||||
|
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
||||||
|
* modified
|
||||||
|
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
||||||
|
* RSV1 bit
|
||||||
|
* @return {(Buffer|String)[]} The framed data
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
static frame(data, options) {
|
||||||
|
let mask;
|
||||||
|
let merge = false;
|
||||||
|
let offset = 2;
|
||||||
|
let skipMasking = false;
|
||||||
|
|
||||||
|
if (options.mask) {
|
||||||
|
mask = options.maskBuffer || maskBuffer;
|
||||||
|
|
||||||
|
if (options.generateMask) {
|
||||||
|
options.generateMask(mask);
|
||||||
|
} else {
|
||||||
|
randomFillSync(mask, 0, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
|
||||||
|
offset = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dataLength;
|
||||||
|
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
if (
|
||||||
|
(!options.mask || skipMasking) &&
|
||||||
|
options[kByteLength] !== undefined
|
||||||
|
) {
|
||||||
|
dataLength = options[kByteLength];
|
||||||
|
} else {
|
||||||
|
data = Buffer.from(data);
|
||||||
|
dataLength = data.length;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dataLength = data.length;
|
||||||
|
merge = options.mask && options.readOnly && !skipMasking;
|
||||||
|
}
|
||||||
|
|
||||||
|
let payloadLength = dataLength;
|
||||||
|
|
||||||
|
if (dataLength >= 65536) {
|
||||||
|
offset += 8;
|
||||||
|
payloadLength = 127;
|
||||||
|
} else if (dataLength > 125) {
|
||||||
|
offset += 2;
|
||||||
|
payloadLength = 126;
|
||||||
|
}
|
||||||
|
|
||||||
|
const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
|
||||||
|
|
||||||
|
target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
|
||||||
|
if (options.rsv1) target[0] |= 0x40;
|
||||||
|
|
||||||
|
target[1] = payloadLength;
|
||||||
|
|
||||||
|
if (payloadLength === 126) {
|
||||||
|
target.writeUInt16BE(dataLength, 2);
|
||||||
|
} else if (payloadLength === 127) {
|
||||||
|
target[2] = target[3] = 0;
|
||||||
|
target.writeUIntBE(dataLength, 4, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options.mask) return [target, data];
|
||||||
|
|
||||||
|
target[1] |= 0x80;
|
||||||
|
target[offset - 4] = mask[0];
|
||||||
|
target[offset - 3] = mask[1];
|
||||||
|
target[offset - 2] = mask[2];
|
||||||
|
target[offset - 1] = mask[3];
|
||||||
|
|
||||||
|
if (skipMasking) return [target, data];
|
||||||
|
|
||||||
|
if (merge) {
|
||||||
|
applyMask(data, mask, target, offset, dataLength);
|
||||||
|
return [target];
|
||||||
|
}
|
||||||
|
|
||||||
|
applyMask(data, mask, data, 0, dataLength);
|
||||||
|
return [target, data];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a close message to the other peer.
|
||||||
|
*
|
||||||
|
* @param {Number} [code] The status code component of the body
|
||||||
|
* @param {(String|Buffer)} [data] The message component of the body
|
||||||
|
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
close(code, data, mask, cb) {
|
||||||
|
let buf;
|
||||||
|
|
||||||
|
if (code === undefined) {
|
||||||
|
buf = EMPTY_BUFFER;
|
||||||
|
} else if (typeof code !== 'number' || !isValidStatusCode(code)) {
|
||||||
|
throw new TypeError('First argument must be a valid error code number');
|
||||||
|
} else if (data === undefined || !data.length) {
|
||||||
|
buf = Buffer.allocUnsafe(2);
|
||||||
|
buf.writeUInt16BE(code, 0);
|
||||||
|
} else {
|
||||||
|
const length = Buffer.byteLength(data);
|
||||||
|
|
||||||
|
if (length > 123) {
|
||||||
|
throw new RangeError('The message must not be greater than 123 bytes');
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = Buffer.allocUnsafe(2 + length);
|
||||||
|
buf.writeUInt16BE(code, 0);
|
||||||
|
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
buf.write(data, 2);
|
||||||
|
} else {
|
||||||
|
buf.set(data, 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
[kByteLength]: buf.length,
|
||||||
|
fin: true,
|
||||||
|
generateMask: this._generateMask,
|
||||||
|
mask,
|
||||||
|
maskBuffer: this._maskBuffer,
|
||||||
|
opcode: 0x08,
|
||||||
|
readOnly: false,
|
||||||
|
rsv1: false
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this._deflating) {
|
||||||
|
this.enqueue([this.dispatch, buf, false, options, cb]);
|
||||||
|
} else {
|
||||||
|
this.sendFrame(Sender.frame(buf, options), cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a ping message to the other peer.
|
||||||
|
*
|
||||||
|
* @param {*} data The message to send
|
||||||
|
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
ping(data, mask, cb) {
|
||||||
|
let byteLength;
|
||||||
|
let readOnly;
|
||||||
|
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
byteLength = Buffer.byteLength(data);
|
||||||
|
readOnly = false;
|
||||||
|
} else {
|
||||||
|
data = toBuffer(data);
|
||||||
|
byteLength = data.length;
|
||||||
|
readOnly = toBuffer.readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byteLength > 125) {
|
||||||
|
throw new RangeError('The data size must not be greater than 125 bytes');
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
[kByteLength]: byteLength,
|
||||||
|
fin: true,
|
||||||
|
generateMask: this._generateMask,
|
||||||
|
mask,
|
||||||
|
maskBuffer: this._maskBuffer,
|
||||||
|
opcode: 0x09,
|
||||||
|
readOnly,
|
||||||
|
rsv1: false
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this._deflating) {
|
||||||
|
this.enqueue([this.dispatch, data, false, options, cb]);
|
||||||
|
} else {
|
||||||
|
this.sendFrame(Sender.frame(data, options), cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a pong message to the other peer.
|
||||||
|
*
|
||||||
|
* @param {*} data The message to send
|
||||||
|
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
pong(data, mask, cb) {
|
||||||
|
let byteLength;
|
||||||
|
let readOnly;
|
||||||
|
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
byteLength = Buffer.byteLength(data);
|
||||||
|
readOnly = false;
|
||||||
|
} else {
|
||||||
|
data = toBuffer(data);
|
||||||
|
byteLength = data.length;
|
||||||
|
readOnly = toBuffer.readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (byteLength > 125) {
|
||||||
|
throw new RangeError('The data size must not be greater than 125 bytes');
|
||||||
|
}
|
||||||
|
|
||||||
|
const options = {
|
||||||
|
[kByteLength]: byteLength,
|
||||||
|
fin: true,
|
||||||
|
generateMask: this._generateMask,
|
||||||
|
mask,
|
||||||
|
maskBuffer: this._maskBuffer,
|
||||||
|
opcode: 0x0a,
|
||||||
|
readOnly,
|
||||||
|
rsv1: false
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this._deflating) {
|
||||||
|
this.enqueue([this.dispatch, data, false, options, cb]);
|
||||||
|
} else {
|
||||||
|
this.sendFrame(Sender.frame(data, options), cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a data message to the other peer.
|
||||||
|
*
|
||||||
|
* @param {*} data The message to send
|
||||||
|
* @param {Object} options Options object
|
||||||
|
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
||||||
|
* or text
|
||||||
|
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
||||||
|
* compress `data`
|
||||||
|
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
||||||
|
* last one
|
||||||
|
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||||
|
* `data`
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
send(data, options, cb) {
|
||||||
|
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||||
|
let opcode = options.binary ? 2 : 1;
|
||||||
|
let rsv1 = options.compress;
|
||||||
|
|
||||||
|
let byteLength;
|
||||||
|
let readOnly;
|
||||||
|
|
||||||
|
if (typeof data === 'string') {
|
||||||
|
byteLength = Buffer.byteLength(data);
|
||||||
|
readOnly = false;
|
||||||
|
} else {
|
||||||
|
data = toBuffer(data);
|
||||||
|
byteLength = data.length;
|
||||||
|
readOnly = toBuffer.readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._firstFragment) {
|
||||||
|
this._firstFragment = false;
|
||||||
|
if (
|
||||||
|
rsv1 &&
|
||||||
|
perMessageDeflate &&
|
||||||
|
perMessageDeflate.params[
|
||||||
|
perMessageDeflate._isServer
|
||||||
|
? 'server_no_context_takeover'
|
||||||
|
: 'client_no_context_takeover'
|
||||||
|
]
|
||||||
|
) {
|
||||||
|
rsv1 = byteLength >= perMessageDeflate._threshold;
|
||||||
|
}
|
||||||
|
this._compress = rsv1;
|
||||||
|
} else {
|
||||||
|
rsv1 = false;
|
||||||
|
opcode = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.fin) this._firstFragment = true;
|
||||||
|
|
||||||
|
if (perMessageDeflate) {
|
||||||
|
const opts = {
|
||||||
|
[kByteLength]: byteLength,
|
||||||
|
fin: options.fin,
|
||||||
|
generateMask: this._generateMask,
|
||||||
|
mask: options.mask,
|
||||||
|
maskBuffer: this._maskBuffer,
|
||||||
|
opcode,
|
||||||
|
readOnly,
|
||||||
|
rsv1
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this._deflating) {
|
||||||
|
this.enqueue([this.dispatch, data, this._compress, opts, cb]);
|
||||||
|
} else {
|
||||||
|
this.dispatch(data, this._compress, opts, cb);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.sendFrame(
|
||||||
|
Sender.frame(data, {
|
||||||
|
[kByteLength]: byteLength,
|
||||||
|
fin: options.fin,
|
||||||
|
generateMask: this._generateMask,
|
||||||
|
mask: options.mask,
|
||||||
|
maskBuffer: this._maskBuffer,
|
||||||
|
opcode,
|
||||||
|
readOnly,
|
||||||
|
rsv1: false
|
||||||
|
}),
|
||||||
|
cb
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatches a message.
|
||||||
|
*
|
||||||
|
* @param {(Buffer|String)} data The message to send
|
||||||
|
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
||||||
|
* `data`
|
||||||
|
* @param {Object} options Options object
|
||||||
|
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
||||||
|
* FIN bit
|
||||||
|
* @param {Function} [options.generateMask] The function used to generate the
|
||||||
|
* masking key
|
||||||
|
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||||
|
* `data`
|
||||||
|
* @param {Buffer} [options.maskBuffer] The buffer used to store the masking
|
||||||
|
* key
|
||||||
|
* @param {Number} options.opcode The opcode
|
||||||
|
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
||||||
|
* modified
|
||||||
|
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
||||||
|
* RSV1 bit
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
dispatch(data, compress, options, cb) {
|
||||||
|
if (!compress) {
|
||||||
|
this.sendFrame(Sender.frame(data, options), cb);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||||
|
|
||||||
|
this._bufferedBytes += options[kByteLength];
|
||||||
|
this._deflating = true;
|
||||||
|
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
||||||
|
if (this._socket.destroyed) {
|
||||||
|
const err = new Error(
|
||||||
|
'The socket was closed while data was being compressed'
|
||||||
|
);
|
||||||
|
|
||||||
|
if (typeof cb === 'function') cb(err);
|
||||||
|
|
||||||
|
for (let i = 0; i < this._queue.length; i++) {
|
||||||
|
const params = this._queue[i];
|
||||||
|
const callback = params[params.length - 1];
|
||||||
|
|
||||||
|
if (typeof callback === 'function') callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._bufferedBytes -= options[kByteLength];
|
||||||
|
this._deflating = false;
|
||||||
|
options.readOnly = false;
|
||||||
|
this.sendFrame(Sender.frame(buf, options), cb);
|
||||||
|
this.dequeue();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes queued send operations.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
dequeue() {
|
||||||
|
while (!this._deflating && this._queue.length) {
|
||||||
|
const params = this._queue.shift();
|
||||||
|
|
||||||
|
this._bufferedBytes -= params[3][kByteLength];
|
||||||
|
Reflect.apply(params[0], this, params.slice(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enqueues a send operation.
|
||||||
|
*
|
||||||
|
* @param {Array} params Send operation parameters.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
enqueue(params) {
|
||||||
|
this._bufferedBytes += params[3][kByteLength];
|
||||||
|
this._queue.push(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends a frame.
|
||||||
|
*
|
||||||
|
* @param {Buffer[]} list The frame to send
|
||||||
|
* @param {Function} [cb] Callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
sendFrame(list, cb) {
|
||||||
|
if (list.length === 2) {
|
||||||
|
this._socket.cork();
|
||||||
|
this._socket.write(list[0]);
|
||||||
|
this._socket.write(list[1], cb);
|
||||||
|
this._socket.uncork();
|
||||||
|
} else {
|
||||||
|
this._socket.write(list[0], cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Sender;
|
||||||
159
node_modules/ws/lib/stream.js
generated
vendored
Normal file
159
node_modules/ws/lib/stream.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { Duplex } = require('stream');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emits the `'close'` event on a stream.
|
||||||
|
*
|
||||||
|
* @param {Duplex} stream The stream.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function emitClose(stream) {
|
||||||
|
stream.emit('close');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener of the `'end'` event.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function duplexOnEnd() {
|
||||||
|
if (!this.destroyed && this._writableState.finished) {
|
||||||
|
this.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The listener of the `'error'` event.
|
||||||
|
*
|
||||||
|
* @param {Error} err The error
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function duplexOnError(err) {
|
||||||
|
this.removeListener('error', duplexOnError);
|
||||||
|
this.destroy();
|
||||||
|
if (this.listenerCount('error') === 0) {
|
||||||
|
// Do not suppress the throwing behavior.
|
||||||
|
this.emit('error', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps a `WebSocket` in a duplex stream.
|
||||||
|
*
|
||||||
|
* @param {WebSocket} ws The `WebSocket` to wrap
|
||||||
|
* @param {Object} [options] The options for the `Duplex` constructor
|
||||||
|
* @return {Duplex} The duplex stream
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function createWebSocketStream(ws, options) {
|
||||||
|
let terminateOnDestroy = true;
|
||||||
|
|
||||||
|
const duplex = new Duplex({
|
||||||
|
...options,
|
||||||
|
autoDestroy: false,
|
||||||
|
emitClose: false,
|
||||||
|
objectMode: false,
|
||||||
|
writableObjectMode: false
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('message', function message(msg, isBinary) {
|
||||||
|
const data =
|
||||||
|
!isBinary && duplex._readableState.objectMode ? msg.toString() : msg;
|
||||||
|
|
||||||
|
if (!duplex.push(data)) ws.pause();
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.once('error', function error(err) {
|
||||||
|
if (duplex.destroyed) return;
|
||||||
|
|
||||||
|
// Prevent `ws.terminate()` from being called by `duplex._destroy()`.
|
||||||
|
//
|
||||||
|
// - If the `'error'` event is emitted before the `'open'` event, then
|
||||||
|
// `ws.terminate()` is a noop as no socket is assigned.
|
||||||
|
// - Otherwise, the error is re-emitted by the listener of the `'error'`
|
||||||
|
// event of the `Receiver` object. The listener already closes the
|
||||||
|
// connection by calling `ws.close()`. This allows a close frame to be
|
||||||
|
// sent to the other peer. If `ws.terminate()` is called right after this,
|
||||||
|
// then the close frame might not be sent.
|
||||||
|
terminateOnDestroy = false;
|
||||||
|
duplex.destroy(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.once('close', function close() {
|
||||||
|
if (duplex.destroyed) return;
|
||||||
|
|
||||||
|
duplex.push(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
duplex._destroy = function (err, callback) {
|
||||||
|
if (ws.readyState === ws.CLOSED) {
|
||||||
|
callback(err);
|
||||||
|
process.nextTick(emitClose, duplex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let called = false;
|
||||||
|
|
||||||
|
ws.once('error', function error(err) {
|
||||||
|
called = true;
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.once('close', function close() {
|
||||||
|
if (!called) callback(err);
|
||||||
|
process.nextTick(emitClose, duplex);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (terminateOnDestroy) ws.terminate();
|
||||||
|
};
|
||||||
|
|
||||||
|
duplex._final = function (callback) {
|
||||||
|
if (ws.readyState === ws.CONNECTING) {
|
||||||
|
ws.once('open', function open() {
|
||||||
|
duplex._final(callback);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the value of the `_socket` property is `null` it means that `ws` is a
|
||||||
|
// client websocket and the handshake failed. In fact, when this happens, a
|
||||||
|
// socket is never assigned to the websocket. Wait for the `'error'` event
|
||||||
|
// that will be emitted by the websocket.
|
||||||
|
if (ws._socket === null) return;
|
||||||
|
|
||||||
|
if (ws._socket._writableState.finished) {
|
||||||
|
callback();
|
||||||
|
if (duplex._readableState.endEmitted) duplex.destroy();
|
||||||
|
} else {
|
||||||
|
ws._socket.once('finish', function finish() {
|
||||||
|
// `duplex` is not destroyed here because the `'end'` event will be
|
||||||
|
// emitted on `duplex` after this `'finish'` event. The EOF signaling
|
||||||
|
// `null` chunk is, in fact, pushed when the websocket emits `'close'`.
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
ws.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
duplex._read = function () {
|
||||||
|
if (ws.isPaused) ws.resume();
|
||||||
|
};
|
||||||
|
|
||||||
|
duplex._write = function (chunk, encoding, callback) {
|
||||||
|
if (ws.readyState === ws.CONNECTING) {
|
||||||
|
ws.once('open', function open() {
|
||||||
|
duplex._write(chunk, encoding, callback);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.send(chunk, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
duplex.on('end', duplexOnEnd);
|
||||||
|
duplex.on('error', duplexOnError);
|
||||||
|
return duplex;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = createWebSocketStream;
|
||||||
62
node_modules/ws/lib/subprotocol.js
generated
vendored
Normal file
62
node_modules/ws/lib/subprotocol.js
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { tokenChars } = require('./validation');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
|
||||||
|
*
|
||||||
|
* @param {String} header The field value of the header
|
||||||
|
* @return {Set} The subprotocol names
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function parse(header) {
|
||||||
|
const protocols = new Set();
|
||||||
|
let start = -1;
|
||||||
|
let end = -1;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
for (i; i < header.length; i++) {
|
||||||
|
const code = header.charCodeAt(i);
|
||||||
|
|
||||||
|
if (end === -1 && tokenChars[code] === 1) {
|
||||||
|
if (start === -1) start = i;
|
||||||
|
} else if (
|
||||||
|
i !== 0 &&
|
||||||
|
(code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
|
||||||
|
) {
|
||||||
|
if (end === -1 && start !== -1) end = i;
|
||||||
|
} else if (code === 0x2c /* ',' */) {
|
||||||
|
if (start === -1) {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end === -1) end = i;
|
||||||
|
|
||||||
|
const protocol = header.slice(start, end);
|
||||||
|
|
||||||
|
if (protocols.has(protocol)) {
|
||||||
|
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
||||||
|
}
|
||||||
|
|
||||||
|
protocols.add(protocol);
|
||||||
|
start = end = -1;
|
||||||
|
} else {
|
||||||
|
throw new SyntaxError(`Unexpected character at index ${i}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start === -1 || end !== -1) {
|
||||||
|
throw new SyntaxError('Unexpected end of input');
|
||||||
|
}
|
||||||
|
|
||||||
|
const protocol = header.slice(start, i);
|
||||||
|
|
||||||
|
if (protocols.has(protocol)) {
|
||||||
|
throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
|
||||||
|
}
|
||||||
|
|
||||||
|
protocols.add(protocol);
|
||||||
|
return protocols;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { parse };
|
||||||
124
node_modules/ws/lib/validation.js
generated
vendored
Normal file
124
node_modules/ws/lib/validation.js
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
//
|
||||||
|
// Allowed token characters:
|
||||||
|
//
|
||||||
|
// '!', '#', '$', '%', '&', ''', '*', '+', '-',
|
||||||
|
// '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
|
||||||
|
//
|
||||||
|
// tokenChars[32] === 0 // ' '
|
||||||
|
// tokenChars[33] === 1 // '!'
|
||||||
|
// tokenChars[34] === 0 // '"'
|
||||||
|
// ...
|
||||||
|
//
|
||||||
|
// prettier-ignore
|
||||||
|
const tokenChars = [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
|
||||||
|
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
|
||||||
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a status code is allowed in a close frame.
|
||||||
|
*
|
||||||
|
* @param {Number} code The status code
|
||||||
|
* @return {Boolean} `true` if the status code is valid, else `false`
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function isValidStatusCode(code) {
|
||||||
|
return (
|
||||||
|
(code >= 1000 &&
|
||||||
|
code <= 1014 &&
|
||||||
|
code !== 1004 &&
|
||||||
|
code !== 1005 &&
|
||||||
|
code !== 1006) ||
|
||||||
|
(code >= 3000 && code <= 4999)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if a given buffer contains only correct UTF-8.
|
||||||
|
* Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
|
||||||
|
* Markus Kuhn.
|
||||||
|
*
|
||||||
|
* @param {Buffer} buf The buffer to check
|
||||||
|
* @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
function _isValidUTF8(buf) {
|
||||||
|
const len = buf.length;
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
while (i < len) {
|
||||||
|
if ((buf[i] & 0x80) === 0) {
|
||||||
|
// 0xxxxxxx
|
||||||
|
i++;
|
||||||
|
} else if ((buf[i] & 0xe0) === 0xc0) {
|
||||||
|
// 110xxxxx 10xxxxxx
|
||||||
|
if (
|
||||||
|
i + 1 === len ||
|
||||||
|
(buf[i + 1] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i] & 0xfe) === 0xc0 // Overlong
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 2;
|
||||||
|
} else if ((buf[i] & 0xf0) === 0xe0) {
|
||||||
|
// 1110xxxx 10xxxxxx 10xxxxxx
|
||||||
|
if (
|
||||||
|
i + 2 >= len ||
|
||||||
|
(buf[i + 1] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i + 2] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
|
||||||
|
(buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 3;
|
||||||
|
} else if ((buf[i] & 0xf8) === 0xf0) {
|
||||||
|
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
||||||
|
if (
|
||||||
|
i + 3 >= len ||
|
||||||
|
(buf[i + 1] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i + 2] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i + 3] & 0xc0) !== 0x80 ||
|
||||||
|
(buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
|
||||||
|
(buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
|
||||||
|
buf[i] > 0xf4 // > U+10FFFF
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 4;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isValidUTF8 = require('utf-8-validate');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
isValidStatusCode,
|
||||||
|
isValidUTF8(buf) {
|
||||||
|
return buf.length < 150 ? _isValidUTF8(buf) : isValidUTF8(buf);
|
||||||
|
},
|
||||||
|
tokenChars
|
||||||
|
};
|
||||||
|
} catch (e) /* istanbul ignore next */ {
|
||||||
|
module.exports = {
|
||||||
|
isValidStatusCode,
|
||||||
|
isValidUTF8: _isValidUTF8,
|
||||||
|
tokenChars
|
||||||
|
};
|
||||||
|
}
|
||||||
485
node_modules/ws/lib/websocket-server.js
generated
vendored
Normal file
485
node_modules/ws/lib/websocket-server.js
generated
vendored
Normal file
@@ -0,0 +1,485 @@
|
|||||||
|
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^net|tls|https$" }] */
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const EventEmitter = require('events');
|
||||||
|
const http = require('http');
|
||||||
|
const https = require('https');
|
||||||
|
const net = require('net');
|
||||||
|
const tls = require('tls');
|
||||||
|
const { createHash } = require('crypto');
|
||||||
|
|
||||||
|
const extension = require('./extension');
|
||||||
|
const PerMessageDeflate = require('./permessage-deflate');
|
||||||
|
const subprotocol = require('./subprotocol');
|
||||||
|
const WebSocket = require('./websocket');
|
||||||
|
const { GUID, kWebSocket } = require('./constants');
|
||||||
|
|
||||||
|
const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
|
||||||
|
|
||||||
|
const RUNNING = 0;
|
||||||
|
const CLOSING = 1;
|
||||||
|
const CLOSED = 2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing a WebSocket server.
|
||||||
|
*
|
||||||
|
* @extends EventEmitter
|
||||||
|
*/
|
||||||
|
class WebSocketServer extends EventEmitter {
|
||||||
|
/**
|
||||||
|
* Create a `WebSocketServer` instance.
|
||||||
|
*
|
||||||
|
* @param {Object} options Configuration options
|
||||||
|
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
||||||
|
* pending connections
|
||||||
|
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
||||||
|
* track clients
|
||||||
|
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
||||||
|
* @param {String} [options.host] The hostname where to bind the server
|
||||||
|
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
||||||
|
* size
|
||||||
|
* @param {Boolean} [options.noServer=false] Enable no server mode
|
||||||
|
* @param {String} [options.path] Accept only connections matching this path
|
||||||
|
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
||||||
|
* permessage-deflate
|
||||||
|
* @param {Number} [options.port] The port where to bind the server
|
||||||
|
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
||||||
|
* server to use
|
||||||
|
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
||||||
|
* not to skip UTF-8 validation for text and close messages
|
||||||
|
* @param {Function} [options.verifyClient] A hook to reject connections
|
||||||
|
* @param {Function} [callback] A listener for the `listening` event
|
||||||
|
*/
|
||||||
|
constructor(options, callback) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
options = {
|
||||||
|
maxPayload: 100 * 1024 * 1024,
|
||||||
|
skipUTF8Validation: false,
|
||||||
|
perMessageDeflate: false,
|
||||||
|
handleProtocols: null,
|
||||||
|
clientTracking: true,
|
||||||
|
verifyClient: null,
|
||||||
|
noServer: false,
|
||||||
|
backlog: null, // use default (511 as implemented in net.js)
|
||||||
|
server: null,
|
||||||
|
host: null,
|
||||||
|
path: null,
|
||||||
|
port: null,
|
||||||
|
...options
|
||||||
|
};
|
||||||
|
|
||||||
|
if (
|
||||||
|
(options.port == null && !options.server && !options.noServer) ||
|
||||||
|
(options.port != null && (options.server || options.noServer)) ||
|
||||||
|
(options.server && options.noServer)
|
||||||
|
) {
|
||||||
|
throw new TypeError(
|
||||||
|
'One and only one of the "port", "server", or "noServer" options ' +
|
||||||
|
'must be specified'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.port != null) {
|
||||||
|
this._server = http.createServer((req, res) => {
|
||||||
|
const body = http.STATUS_CODES[426];
|
||||||
|
|
||||||
|
res.writeHead(426, {
|
||||||
|
'Content-Length': body.length,
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
});
|
||||||
|
res.end(body);
|
||||||
|
});
|
||||||
|
this._server.listen(
|
||||||
|
options.port,
|
||||||
|
options.host,
|
||||||
|
options.backlog,
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
} else if (options.server) {
|
||||||
|
this._server = options.server;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._server) {
|
||||||
|
const emitConnection = this.emit.bind(this, 'connection');
|
||||||
|
|
||||||
|
this._removeListeners = addListeners(this._server, {
|
||||||
|
listening: this.emit.bind(this, 'listening'),
|
||||||
|
error: this.emit.bind(this, 'error'),
|
||||||
|
upgrade: (req, socket, head) => {
|
||||||
|
this.handleUpgrade(req, socket, head, emitConnection);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.perMessageDeflate === true) options.perMessageDeflate = {};
|
||||||
|
if (options.clientTracking) {
|
||||||
|
this.clients = new Set();
|
||||||
|
this._shouldEmitClose = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.options = options;
|
||||||
|
this._state = RUNNING;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the bound address, the address family name, and port of the server
|
||||||
|
* as reported by the operating system if listening on an IP socket.
|
||||||
|
* If the server is listening on a pipe or UNIX domain socket, the name is
|
||||||
|
* returned as a string.
|
||||||
|
*
|
||||||
|
* @return {(Object|String|null)} The address of the server
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
address() {
|
||||||
|
if (this.options.noServer) {
|
||||||
|
throw new Error('The server is operating in "noServer" mode');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this._server) return null;
|
||||||
|
return this._server.address();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop the server from accepting new connections and emit the `'close'` event
|
||||||
|
* when all existing connections are closed.
|
||||||
|
*
|
||||||
|
* @param {Function} [cb] A one-time listener for the `'close'` event
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
close(cb) {
|
||||||
|
if (this._state === CLOSED) {
|
||||||
|
if (cb) {
|
||||||
|
this.once('close', () => {
|
||||||
|
cb(new Error('The server is not running'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
process.nextTick(emitClose, this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cb) this.once('close', cb);
|
||||||
|
|
||||||
|
if (this._state === CLOSING) return;
|
||||||
|
this._state = CLOSING;
|
||||||
|
|
||||||
|
if (this.options.noServer || this.options.server) {
|
||||||
|
if (this._server) {
|
||||||
|
this._removeListeners();
|
||||||
|
this._removeListeners = this._server = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.clients) {
|
||||||
|
if (!this.clients.size) {
|
||||||
|
process.nextTick(emitClose, this);
|
||||||
|
} else {
|
||||||
|
this._shouldEmitClose = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
process.nextTick(emitClose, this);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const server = this._server;
|
||||||
|
|
||||||
|
this._removeListeners();
|
||||||
|
this._removeListeners = this._server = null;
|
||||||
|
|
||||||
|
//
|
||||||
|
// The HTTP/S server was created internally. Close it, and rely on its
|
||||||
|
// `'close'` event.
|
||||||
|
//
|
||||||
|
server.close(() => {
|
||||||
|
emitClose(this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See if a given request should be handled by this server instance.
|
||||||
|
*
|
||||||
|
* @param {http.IncomingMessage} req Request object to inspect
|
||||||
|
* @return {Boolean} `true` if the request is valid, else `false`
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
shouldHandle(req) {
|
||||||
|
if (this.options.path) {
|
||||||
|
const index = req.url.indexOf('?');
|
||||||
|
const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
|
||||||
|
|
||||||
|
if (pathname !== this.options.path) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a HTTP Upgrade request.
|
||||||
|
*
|
||||||
|
* @param {http.IncomingMessage} req The request object
|
||||||
|
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
||||||
|
* server and client
|
||||||
|
* @param {Buffer} head The first packet of the upgraded stream
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
handleUpgrade(req, socket, head, cb) {
|
||||||
|
socket.on('error', socketOnError);
|
||||||
|
|
||||||
|
const key =
|
||||||
|
req.headers['sec-websocket-key'] !== undefined
|
||||||
|
? req.headers['sec-websocket-key']
|
||||||
|
: false;
|
||||||
|
const version = +req.headers['sec-websocket-version'];
|
||||||
|
|
||||||
|
if (
|
||||||
|
req.method !== 'GET' ||
|
||||||
|
req.headers.upgrade.toLowerCase() !== 'websocket' ||
|
||||||
|
!key ||
|
||||||
|
!keyRegex.test(key) ||
|
||||||
|
(version !== 8 && version !== 13) ||
|
||||||
|
!this.shouldHandle(req)
|
||||||
|
) {
|
||||||
|
return abortHandshake(socket, 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
|
||||||
|
let protocols = new Set();
|
||||||
|
|
||||||
|
if (secWebSocketProtocol !== undefined) {
|
||||||
|
try {
|
||||||
|
protocols = subprotocol.parse(secWebSocketProtocol);
|
||||||
|
} catch (err) {
|
||||||
|
return abortHandshake(socket, 400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
|
||||||
|
const extensions = {};
|
||||||
|
|
||||||
|
if (
|
||||||
|
this.options.perMessageDeflate &&
|
||||||
|
secWebSocketExtensions !== undefined
|
||||||
|
) {
|
||||||
|
const perMessageDeflate = new PerMessageDeflate(
|
||||||
|
this.options.perMessageDeflate,
|
||||||
|
true,
|
||||||
|
this.options.maxPayload
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const offers = extension.parse(secWebSocketExtensions);
|
||||||
|
|
||||||
|
if (offers[PerMessageDeflate.extensionName]) {
|
||||||
|
perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
|
||||||
|
extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
return abortHandshake(socket, 400);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Optionally call external client verification handler.
|
||||||
|
//
|
||||||
|
if (this.options.verifyClient) {
|
||||||
|
const info = {
|
||||||
|
origin:
|
||||||
|
req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
|
||||||
|
secure: !!(req.socket.authorized || req.socket.encrypted),
|
||||||
|
req
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.options.verifyClient.length === 2) {
|
||||||
|
this.options.verifyClient(info, (verified, code, message, headers) => {
|
||||||
|
if (!verified) {
|
||||||
|
return abortHandshake(socket, code || 401, message, headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.completeUpgrade(
|
||||||
|
extensions,
|
||||||
|
key,
|
||||||
|
protocols,
|
||||||
|
req,
|
||||||
|
socket,
|
||||||
|
head,
|
||||||
|
cb
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upgrade the connection to WebSocket.
|
||||||
|
*
|
||||||
|
* @param {Object} extensions The accepted extensions
|
||||||
|
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
||||||
|
* @param {Set} protocols The subprotocols
|
||||||
|
* @param {http.IncomingMessage} req The request object
|
||||||
|
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
||||||
|
* server and client
|
||||||
|
* @param {Buffer} head The first packet of the upgraded stream
|
||||||
|
* @param {Function} cb Callback
|
||||||
|
* @throws {Error} If called more than once with the same socket
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
|
||||||
|
//
|
||||||
|
// Destroy the socket if the client has already sent a FIN packet.
|
||||||
|
//
|
||||||
|
if (!socket.readable || !socket.writable) return socket.destroy();
|
||||||
|
|
||||||
|
if (socket[kWebSocket]) {
|
||||||
|
throw new Error(
|
||||||
|
'server.handleUpgrade() was called more than once with the same ' +
|
||||||
|
'socket, possibly due to a misconfiguration'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._state > RUNNING) return abortHandshake(socket, 503);
|
||||||
|
|
||||||
|
const digest = createHash('sha1')
|
||||||
|
.update(key + GUID)
|
||||||
|
.digest('base64');
|
||||||
|
|
||||||
|
const headers = [
|
||||||
|
'HTTP/1.1 101 Switching Protocols',
|
||||||
|
'Upgrade: websocket',
|
||||||
|
'Connection: Upgrade',
|
||||||
|
`Sec-WebSocket-Accept: ${digest}`
|
||||||
|
];
|
||||||
|
|
||||||
|
const ws = new WebSocket(null);
|
||||||
|
|
||||||
|
if (protocols.size) {
|
||||||
|
//
|
||||||
|
// Optionally call external protocol selection handler.
|
||||||
|
//
|
||||||
|
const protocol = this.options.handleProtocols
|
||||||
|
? this.options.handleProtocols(protocols, req)
|
||||||
|
: protocols.values().next().value;
|
||||||
|
|
||||||
|
if (protocol) {
|
||||||
|
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
||||||
|
ws._protocol = protocol;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extensions[PerMessageDeflate.extensionName]) {
|
||||||
|
const params = extensions[PerMessageDeflate.extensionName].params;
|
||||||
|
const value = extension.format({
|
||||||
|
[PerMessageDeflate.extensionName]: [params]
|
||||||
|
});
|
||||||
|
headers.push(`Sec-WebSocket-Extensions: ${value}`);
|
||||||
|
ws._extensions = extensions;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Allow external modification/inspection of handshake headers.
|
||||||
|
//
|
||||||
|
this.emit('headers', headers, req);
|
||||||
|
|
||||||
|
socket.write(headers.concat('\r\n').join('\r\n'));
|
||||||
|
socket.removeListener('error', socketOnError);
|
||||||
|
|
||||||
|
ws.setSocket(socket, head, {
|
||||||
|
maxPayload: this.options.maxPayload,
|
||||||
|
skipUTF8Validation: this.options.skipUTF8Validation
|
||||||
|
});
|
||||||
|
|
||||||
|
if (this.clients) {
|
||||||
|
this.clients.add(ws);
|
||||||
|
ws.on('close', () => {
|
||||||
|
this.clients.delete(ws);
|
||||||
|
|
||||||
|
if (this._shouldEmitClose && !this.clients.size) {
|
||||||
|
process.nextTick(emitClose, this);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(ws, req);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = WebSocketServer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add event listeners on an `EventEmitter` using a map of <event, listener>
|
||||||
|
* pairs.
|
||||||
|
*
|
||||||
|
* @param {EventEmitter} server The event emitter
|
||||||
|
* @param {Object.<String, Function>} map The listeners to add
|
||||||
|
* @return {Function} A function that will remove the added listeners when
|
||||||
|
* called
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function addListeners(server, map) {
|
||||||
|
for (const event of Object.keys(map)) server.on(event, map[event]);
|
||||||
|
|
||||||
|
return function removeListeners() {
|
||||||
|
for (const event of Object.keys(map)) {
|
||||||
|
server.removeListener(event, map[event]);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit a `'close'` event on an `EventEmitter`.
|
||||||
|
*
|
||||||
|
* @param {EventEmitter} server The event emitter
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function emitClose(server) {
|
||||||
|
server._state = CLOSED;
|
||||||
|
server.emit('close');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle premature socket errors.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function socketOnError() {
|
||||||
|
this.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the connection when preconditions are not fulfilled.
|
||||||
|
*
|
||||||
|
* @param {(net.Socket|tls.Socket)} socket The socket of the upgrade request
|
||||||
|
* @param {Number} code The HTTP response status code
|
||||||
|
* @param {String} [message] The HTTP response body
|
||||||
|
* @param {Object} [headers] Additional HTTP response headers
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
function abortHandshake(socket, code, message, headers) {
|
||||||
|
if (socket.writable) {
|
||||||
|
message = message || http.STATUS_CODES[code];
|
||||||
|
headers = {
|
||||||
|
Connection: 'close',
|
||||||
|
'Content-Type': 'text/html',
|
||||||
|
'Content-Length': Buffer.byteLength(message),
|
||||||
|
...headers
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.write(
|
||||||
|
`HTTP/1.1 ${code} ${http.STATUS_CODES[code]}\r\n` +
|
||||||
|
Object.keys(headers)
|
||||||
|
.map((h) => `${h}: ${headers[h]}`)
|
||||||
|
.join('\r\n') +
|
||||||
|
'\r\n\r\n' +
|
||||||
|
message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.removeListener('error', socketOnError);
|
||||||
|
socket.destroy();
|
||||||
|
}
|
||||||
1225
node_modules/ws/lib/websocket.js
generated
vendored
Normal file
1225
node_modules/ws/lib/websocket.js
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
61
node_modules/ws/package.json
generated
vendored
Normal file
61
node_modules/ws/package.json
generated
vendored
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
{
|
||||||
|
"name": "ws",
|
||||||
|
"version": "8.4.2",
|
||||||
|
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
|
||||||
|
"keywords": [
|
||||||
|
"HyBi",
|
||||||
|
"Push",
|
||||||
|
"RFC-6455",
|
||||||
|
"WebSocket",
|
||||||
|
"WebSockets",
|
||||||
|
"real-time"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/websockets/ws",
|
||||||
|
"bugs": "https://github.com/websockets/ws/issues",
|
||||||
|
"repository": "websockets/ws",
|
||||||
|
"author": "Einar Otto Stangvik <einaros@gmail.com> (http://2x.io)",
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "index.js",
|
||||||
|
"exports": {
|
||||||
|
"import": "./wrapper.mjs",
|
||||||
|
"require": "./index.js"
|
||||||
|
},
|
||||||
|
"browser": "browser.js",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"browser.js",
|
||||||
|
"index.js",
|
||||||
|
"lib/*.js",
|
||||||
|
"wrapper.mjs"
|
||||||
|
],
|
||||||
|
"scripts": {
|
||||||
|
"test": "nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js",
|
||||||
|
"integration": "mocha --throw-deprecation test/*.integration.js",
|
||||||
|
"lint": "eslint --ignore-path .gitignore . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\""
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"benchmark": "^2.1.4",
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"eslint": "^8.0.0",
|
||||||
|
"eslint-config-prettier": "^8.1.0",
|
||||||
|
"eslint-plugin-prettier": "^4.0.0",
|
||||||
|
"mocha": "^8.4.0",
|
||||||
|
"nyc": "^15.0.0",
|
||||||
|
"prettier": "^2.0.5",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
8
node_modules/ws/wrapper.mjs
generated
vendored
Normal file
8
node_modules/ws/wrapper.mjs
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import createWebSocketStream from './lib/stream.js';
|
||||||
|
import Receiver from './lib/receiver.js';
|
||||||
|
import Sender from './lib/sender.js';
|
||||||
|
import WebSocket from './lib/websocket.js';
|
||||||
|
import WebSocketServer from './lib/websocket-server.js';
|
||||||
|
|
||||||
|
export { createWebSocketStream, Receiver, Sender, WebSocket, WebSocketServer };
|
||||||
|
export default WebSocket;
|
||||||
130
package-lock.json
generated
Normal file
130
package-lock.json
generated
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
{
|
||||||
|
"name": "jschatbot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"lockfileVersion": 2,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "jschatbot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"tmi.js": "^1.8.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/node-fetch": {
|
||||||
|
"version": "2.6.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||||
|
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"whatwg-url": "^5.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "4.x || >=6.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"encoding": "^0.1.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"encoding": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tmi.js": {
|
||||||
|
"version": "1.8.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/tmi.js/-/tmi.js-1.8.5.tgz",
|
||||||
|
"integrity": "sha512-A9qrydfe1e0VWM9MViVhhxVgvLpnk7pFShVUWePsSTtoi+A1X+Zjdoa7OJd7/YsgHXGj3GkNEvnWop/1WwZuew==",
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
|
"ws": "^8.2.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/tr46": {
|
||||||
|
"version": "0.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||||
|
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
|
||||||
|
},
|
||||||
|
"node_modules/webidl-conversions": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||||
|
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
|
||||||
|
},
|
||||||
|
"node_modules/whatwg-url": {
|
||||||
|
"version": "5.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
|
||||||
|
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
|
||||||
|
"dependencies": {
|
||||||
|
"tr46": "~0.0.3",
|
||||||
|
"webidl-conversions": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "8.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.4.2.tgz",
|
||||||
|
"integrity": "sha512-Kbk4Nxyq7/ZWqr/tarI9yIt/+iNNFOjBXEWgTb4ydaNHBNGgvf2QHbS9fdfsndfjFlFwEd4Al+mw83YkaD10ZA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": "^5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"node-fetch": {
|
||||||
|
"version": "2.6.7",
|
||||||
|
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz",
|
||||||
|
"integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==",
|
||||||
|
"requires": {
|
||||||
|
"whatwg-url": "^5.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tmi.js": {
|
||||||
|
"version": "1.8.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/tmi.js/-/tmi.js-1.8.5.tgz",
|
||||||
|
"integrity": "sha512-A9qrydfe1e0VWM9MViVhhxVgvLpnk7pFShVUWePsSTtoi+A1X+Zjdoa7OJd7/YsgHXGj3GkNEvnWop/1WwZuew==",
|
||||||
|
"requires": {
|
||||||
|
"node-fetch": "^2.6.1",
|
||||||
|
"ws": "^8.2.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tr46": {
|
||||||
|
"version": "0.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
|
||||||
|
"integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o="
|
||||||
|
},
|
||||||
|
"webidl-conversions": {
|
||||||
|
"version": "3.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
|
||||||
|
"integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE="
|
||||||
|
},
|
||||||
|
"whatwg-url": {
|
||||||
|
"version": "5.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
|
||||||
|
"integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=",
|
||||||
|
"requires": {
|
||||||
|
"tr46": "~0.0.3",
|
||||||
|
"webidl-conversions": "^3.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"ws": {
|
||||||
|
"version": "8.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.4.2.tgz",
|
||||||
|
"integrity": "sha512-Kbk4Nxyq7/ZWqr/tarI9yIt/+iNNFOjBXEWgTb4ydaNHBNGgvf2QHbS9fdfsndfjFlFwEd4Al+mw83YkaD10ZA==",
|
||||||
|
"requires": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
package.json
Normal file
15
package.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"name": "jschatbot",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"tmi.js": "^1.8.5"
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user