Files
node_modules
.bin
@ampproject
@babel
@discoveryjs
@gar
@istanbuljs
@jridgewell
@leichtgewicht
@npmcli
@riotjs
@tootallnate
@types
@ungap
@webassemblyjs
@webpack-cli
@xtuc
abab
abbrev
accepts
acorn
acorn-globals
acorn-import-assertions
acorn-walk
agent-base
agentkeepalive
aggregate-error
ajv
ajv-formats
ajv-keywords
ansi-colors
ansi-html-community
ansi-regex
ansi-styles
anymatch
append-transform
aproba
archy
are-we-there-yet
argparse
array-flatten
arrify
assertion-error
ast-types
async-foreach
asynckit
babel-plugin-dynamic-import-node
babel-plugin-polyfill-corejs2
babel-plugin-polyfill-corejs3
babel-plugin-polyfill-regenerator
balanced-match
batch
bianco.attr
bianco.dom-to-array
bianco.events
bianco.query
big.js
binary-extensions
body-parser
bonjour-service
boolbase
brace-expansion
braces
browser-process-hrtime
browser-stdout
browserslist
buffer-from
bytes
cacache
caching-transform
call-bind
camel-case
camelcase
camelcase-keys
caniuse-lite
chai
chalk
check-error
chokidar
chownr
chrome-trace-event
clean-css
clean-stack
cliui
clone-deep
color-convert
color-name
color-support
colorette
combined-stream
commander
commondir
compressible
compression
concat-map
connect-history-api-fallback
console-control-strings
content-disposition
content-type
convert-source-map
cookie
cookie-signature
core-js-compat
core-util-is
cross-spawn
css-select
css-what
cssesc
cssom
cssstyle
cumpa
curri
data-urls
debug
decamelize
decamelize-keys
decimal.js
deep-eql
deep-is
default-gateway
default-require-extensions
define-lazy-prop
define-properties
delayed-stream
delegates
depd
destroy
detect-node
diff
dns-equal
dns-packet
dom-converter
dom-nodes
dom-serializer
domelementtype
domexception
domhandler
domutils
dot-case
ee-first
electron-to-chromium
emoji-regex
emojis-list
encodeurl
encoding
enhanced-resolve
entities
env-paths
envinfo
err-code
erre
error-ex
es-module-lexer
es6-error
escalade
escape-html
escape-string-regexp
escodegen
eslint-scope
esm
esprima
esrecurse
estraverse
esutils
etag
eventemitter3
events
execa
express
fast-deep-equal
fast-json-stable-stringify
fast-levenshtein
fastest-levenshtein
faye-websocket
fill-range
finalhandler
find-up
flat
follow-redirects
foreground-child
form-data
forwarded
fresh
fromentries
fs-minipass
fs-monkey
fs.realpath
function-bind
gauge
gaze
gensync
get-caller-file
get-func-name
get-intrinsic
get-package-type
get-stdin
get-stream
glob
glob-parent
glob-to-regexp
globals
globule
graceful-fs
growl
handle-thing
hard-rejection
has
has-flag
has-property-descriptors
has-symbols
has-unicode
hasha
he
hosted-git-info
hpack.js
html-encoding-sniffer
html-entities
html-escaper
html-minifier-terser
html-webpack-plugin
htmlparser2
http-cache-semantics
http-deceiver
http-errors
http-parser-js
http-proxy
http-proxy-agent
http-proxy-middleware
https-proxy-agent
human-signals
humanize-ms
iconv-lite
imurmurhash
indent-string
infer-owner
inflight
inherits
interpret
ip
ipaddr.js
is-arrayish
is-binary-path
is-core-module
is-docker
is-extglob
is-fullwidth-code-point
is-glob
is-lambda
is-number
is-plain-obj
is-plain-object
is-potential-custom-element-name
is-stream
is-typedarray
is-windows
is-wsl
isarray
isexe
isobject
istanbul-lib-coverage
istanbul-lib-hook
istanbul-lib-instrument
istanbul-lib-processinfo
istanbul-lib-report
istanbul-lib-source-maps
istanbul-reports
jest-worker
js-base64
js-tokens
js-yaml
jsdom
jsdom-global
jsesc
json-parse-even-better-errors
json-schema-traverse
json5
kind-of
levn
lines-and-columns
loader-runner
locate-path
lodash
lodash.debounce
lodash.flattendeep
log-symbols
loupe
lower-case
lru-cache
make-fetch-happen
map-obj
media-typer
memfs
meow
merge-descriptors
merge-stream
methods
micromatch
mime
mime-db
mime-types
mimic-fn
min-indent
minimalistic-assert
minimatch
minimist-options
minipass
minipass-collect
minipass-fetch
minipass-flush
minipass-pipeline
minipass-sized
minizlib
mkdirp
mocha
ms
multicast-dns
nan
nanoid
negotiator
neo-async
no-case
node-forge
node-gyp
node-preload
node-releases
node-sass
nopt
normalize-package-data
normalize-path
npm-run-path
npmlog
nth-check
nwsapi
nyc
object-inspect
object-keys
object.assign
obuf
on-finished
on-headers
once
onetime
open
optionator
p-limit
p-locate
p-map
p-retry
p-try
package-hash
param-case
parse-json
parse5
parseurl
pascal-case
path-exists
path-is-absolute
path-key
path-parse
path-to-regexp
pathval
picocolors
picomatch
pirates
prelude-ls
pretty-error
process-nextick-args
process-on-spawn
promise-inflight
promise-retry
proxy-addr
psl
punycode
qs
quick-lru
randombytes
range-parser
raw-body
rawth
read-pkg
read-pkg-up
readable-stream
readdirp
recast
rechoir
redent
regenerate
regenerate-unicode-properties
regenerator-runtime
regenerator-transform
regexpu-core
regjsgen
regjsparser
relateurl
release-zalgo
renderkid
require-directory
require-from-string
require-main-filename
requires-port
resolve
retry
rimraf
riot
ruit
safe-buffer
safer-buffer
sass-graph
saxes
schema-utils
scss-tokenizer
select-hose
selfsigned
semver
send
serialize-javascript
serve-index
serve-static
set-blocking
setprototypeof
shallow-clone
shebang-command
shebang-regex
side-channel
signal-exit
smart-buffer
sockjs
socks
socks-proxy-agent
source-map
source-map-support
spawn-wrap
spdx-correct
spdx-exceptions
spdx-expression-parse
spdx-license-ids
spdy
spdy-transport
sprintf-js
ssri
statuses
stdout-stream
string-width
string_decoder
strip-ansi
strip-final-newline
strip-indent
strip-json-comments
supports-color
symbol-tree
tapable
tar
terser
terser-webpack-plugin
test-exclude
thunky
to-fast-properties
to-regex-range
toidentifier
tough-cookie
tr46
trim-newlines
true-case-path
tslib
type-check
type-detect
type-fest
type-is
typedarray-to-buffer
unicode-canonical-property-names-ecmascript
unicode-match-property-ecmascript
unicode-match-property-value-ecmascript
unicode-property-aliases-ecmascript
unique-filename
unique-slug
universalify
unpipe
update-browserslist-db
uri-js
util-deprecate
utila
utils-merge
uuid
validate-npm-package-license
vary
w3c-hr-time
w3c-xmlserializer
watchpack
wbuf
webidl-conversions
webpack
webpack-cli
webpack-dev-middleware
webpack-dev-server
webpack-merge
webpack-sources
websocket-driver
websocket-extensions
lib
CHANGELOG.md
LICENSE.md
README.md
package.json
whatwg-encoding
whatwg-mimetype
whatwg-url
which
which-module
wide-align
wildcard
word-wrap
workerpool
wrap-ansi
wrappy
write-file-atomic
ws
xml-name-validator
xmlchars
y18n
yallist
yargs
yargs-parser
yargs-unparser
yocto-queue
.package-lock.json
src
LICENSE
package-lock.json
package.json
readme.md
src.zip
webpack.config.js
parcoursup/node_modules/websocket-extensions
lalBi94 7bc56c09b5 $
2023-03-05 13:23:23 +01:00
..
$
2023-03-05 13:23:23 +01:00
$
2023-03-05 13:23:23 +01:00
$
2023-03-05 13:23:23 +01:00
$
2023-03-05 13:23:23 +01:00
$
2023-03-05 13:23:23 +01:00

websocket-extensions Build status

A minimal framework that supports the implementation of WebSocket extensions in a way that's decoupled from the main protocol. This library aims to allow a WebSocket extension to be written and used with any protocol library, by defining abstract representations of frames and messages that allow modules to co-operate.

websocket-extensions provides a container for registering extension plugins, and provides all the functions required to negotiate which extensions to use during a session via the Sec-WebSocket-Extensions header. By implementing the APIs defined in this document, an extension may be used by any WebSocket library based on this framework.

Installation

$ npm install websocket-extensions

Usage

There are two main audiences for this library: authors implementing the WebSocket protocol, and authors implementing extensions. End users of a WebSocket library or an extension should be able to use any extension by passing it as an argument to their chosen protocol library, without needing to know how either of them work, or how the websocket-extensions framework operates.

The library is designed with the aim that any protocol implementation and any extension can be used together, so long as they support the same abstract representation of frames and messages.

Data types

The APIs provided by the framework rely on two data types; extensions will expect to be given data and to be able to return data in these formats:

Frame

Frame is a structure representing a single WebSocket frame of any type. Frames are simple objects that must have at least the following properties, which represent the data encoded in the frame:

property description
final true if the FIN bit is set, false otherwise
rsv1 true if the RSV1 bit is set, false otherwise
rsv2 true if the RSV2 bit is set, false otherwise
rsv3 true if the RSV3 bit is set, false otherwise
opcode the numeric opcode (0, 1, 2, 8, 9, or 10) of the frame
masked true if the MASK bit is set, false otherwise
maskingKey a 4-byte Buffer if masked is true, otherwise null
payload a Buffer containing the (unmasked) application data

Message

A Message represents a complete application message, which can be formed from text, binary and continuation frames. It has the following properties:

property description
rsv1 true if the first frame of the message has the RSV1 bit set
rsv2 true if the first frame of the message has the RSV2 bit set
rsv3 true if the first frame of the message has the RSV3 bit set
opcode the numeric opcode (1 or 2) of the first frame of the message
data the concatenation of all the frame payloads in the message

For driver authors

A driver author is someone implementing the WebSocket protocol proper, and who wishes end users to be able to use WebSocket extensions with their library.

At the start of a WebSocket session, on both the client and the server side, they should begin by creating an extension container and adding whichever extensions they want to use.

var Extensions = require('websocket-extensions'),
    deflate    = require('permessage-deflate');

var exts = new Extensions();
exts.add(deflate);

In the following examples, exts refers to this Extensions instance.

Client sessions

Clients will use the methods generateOffer() and activate(header).

As part of the handshake process, the client must send a Sec-WebSocket-Extensions header to advertise that it supports the registered extensions. This header should be generated using:

request.headers['sec-websocket-extensions'] = exts.generateOffer();

This returns a string, for example "permessage-deflate; client_max_window_bits", that represents all the extensions the client is offering to use, and their parameters. This string may contain multiple offers for the same extension.

When the client receives the handshake response from the server, it should pass the incoming Sec-WebSocket-Extensions header in to exts to activate the extensions the server has accepted:

exts.activate(response.headers['sec-websocket-extensions']);

If the server has sent any extension responses that the client does not recognize, or are in conflict with one another for use of RSV bits, or that use invalid parameters for the named extensions, then exts.activate() will throw. In this event, the client driver should fail the connection with closing code 1010.

Server sessions

Servers will use the method generateResponse(header).

A server session needs to generate a Sec-WebSocket-Extensions header to send in its handshake response:

var clientOffer = request.headers['sec-websocket-extensions'],
    extResponse = exts.generateResponse(clientOffer);

response.headers['sec-websocket-extensions'] = extResponse;

Calling exts.generateResponse(header) activates those extensions the client has asked to use, if they are registered, asks each extension for a set of response parameters, and returns a string containing the response parameters for all accepted extensions.

In both directions

Both clients and servers will use the methods validFrameRsv(frame), processIncomingMessage(message) and processOutgoingMessage(message).

The WebSocket protocol requires that frames do not have any of the RSV bits set unless there is an extension in use that allows otherwise. When processing an incoming frame, sessions should pass a Frame object to:

exts.validFrameRsv(frame)

If this method returns false, the session should fail the WebSocket connection with closing code 1002.

To pass incoming messages through the extension stack, a session should construct a Message object according to the above datatype definitions, and call:

exts.processIncomingMessage(message, function(error, msg) {
  // hand the message off to the application
});

If any extensions fail to process the message, then the callback will yield an error and the session should fail the WebSocket connection with closing code 1010. If error is null, then msg should be passed on to the application.

To pass outgoing messages through the extension stack, a session should construct a Message as before, and call:

exts.processOutgoingMessage(message, function(error, msg) {
  // write message to the transport
});

If any extensions fail to process the message, then the callback will yield an error and the session should fail the WebSocket connection with closing code 1010. If error is null, then message should be converted into frames (with the message's rsv1, rsv2, rsv3 and opcode set on the first frame) and written to the transport.

At the end of the WebSocket session (either when the protocol is explicitly ended or the transport connection disconnects), the driver should call:

exts.close(function() {})

The callback is invoked when all extensions have finished processing any messages in the pipeline and it's safe to close the socket.

For extension authors

An extension author is someone implementing an extension that transforms WebSocket messages passing between the client and server. They would like to implement their extension once and have it work with any protocol library.

Extension authors will not install websocket-extensions or call it directly. Instead, they should implement the following API to allow their extension to plug into the websocket-extensions framework.

An Extension is any object that has the following properties:

property description
name a string containing the name of the extension as used in negotiation headers
type a string, must be "permessage"
rsv1 either true if the extension uses the RSV1 bit, false otherwise
rsv2 either true if the extension uses the RSV2 bit, false otherwise
rsv3 either true if the extension uses the RSV3 bit, false otherwise

It must also implement the following methods:

ext.createClientSession()

This returns a ClientSession, whose interface is defined below.

ext.createServerSession(offers)

This takes an array of offer params and returns a ServerSession, whose interface is defined below. For example, if the client handshake contains the offer header:

Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover; server_max_window_bits=8, \
                          permessage-deflate; server_max_window_bits=15

then the permessage-deflate extension will receive the call:

ext.createServerSession([
  { server_no_context_takeover: true, server_max_window_bits: 8 },
  { server_max_window_bits: 15 }
]);

The extension must decide which set of parameters it wants to accept, if any, and return a ServerSession if it wants to accept the parameters and null otherwise.

ClientSession

A ClientSession is the type returned by ext.createClientSession(). It must implement the following methods, as well as the Session API listed below.

clientSession.generateOffer()
// e.g.  -> [
//            { server_no_context_takeover: true, server_max_window_bits: 8 },
//            { server_max_window_bits: 15 }
//          ]

This must return a set of parameters to include in the client's Sec-WebSocket-Extensions offer header. If the session wants to offer multiple configurations, it can return an array of sets of parameters as shown above.

clientSession.activate(params) // -> true

This must take a single set of parameters from the server's handshake response and use them to configure the client session. If the client accepts the given parameters, then this method must return true. If it returns any other value, the framework will interpret this as the client rejecting the response, and will throw.

ServerSession

A ServerSession is the type returned by ext.createServerSession(offers). It must implement the following methods, as well as the Session API listed below.

serverSession.generateResponse()
// e.g.  -> { server_max_window_bits: 8 }

This returns the set of parameters the server session wants to send in its Sec-WebSocket-Extensions response header. Only one set of parameters is returned to the client per extension. Server sessions that would confict on their use of RSV bits are not activated.

Session

The Session API must be implemented by both client and server sessions. It contains two methods, processIncomingMessage(message) and processOutgoingMessage(message).

session.processIncomingMessage(message, function(error, msg) { ... })

The session must implement this method to take an incoming Message as defined above, transform it in any way it needs, then return it via the callback. If there is an error processing the message, this method should yield an error as the first argument.

session.processOutgoingMessage(message, function(error, msg) { ... })

The session must implement this method to take an outgoing Message as defined above, transform it in any way it needs, then return it via the callback. If there is an error processing the message, this method should yield an error as the first argument.

Note that both processIncomingMessage() and processOutgoingMessage() can perform their logic asynchronously, are allowed to process multiple messages concurrently, and are not required to complete working on messages in the same order the messages arrive. websocket-extensions will reorder messages as your extension emits them and will make sure every extension is given messages in the order they arrive from the driver. This allows extensions to maintain state that depends on the messages' wire order, for example keeping a DEFLATE compression context between messages.

session.close()

The framework will call this method when the WebSocket session ends, allowing the session to release any resources it's using.

Examples