edgargg commited on
Commit
d0f5140
1 Parent(s): 360261c

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,9 +1,9 @@
1
 
2
  ---
3
  tags: [gradio-custom-component,gradio-template-Image,bounding box,annotator,annotate,boxes]
4
- title: gradio_image_annotation V0.0.4
5
- colorFrom: red
6
- colorTo: red
7
  sdk: docker
8
  pinned: false
9
  license: apache-2.0
 
1
 
2
  ---
3
  tags: [gradio-custom-component,gradio-template-Image,bounding box,annotator,annotate,boxes]
4
+ title: gradio_image_annotation V0.0.5
5
+ colorFrom: gray
6
+ colorTo: gray
7
  sdk: docker
8
  pinned: false
9
  license: apache-2.0
__pycache__/app.cpython-311.pyc CHANGED
Binary files a/__pycache__/app.cpython-311.pyc and b/__pycache__/app.cpython-311.pyc differ
 
app.py CHANGED
@@ -35,12 +35,6 @@ def get_boxes_json(annotations):
35
 
36
 
37
  with gr.Blocks() as demo:
38
- with gr.Tab("Crop"):
39
- with gr.Row():
40
- annotator_crop = image_annotator(example, image_type="numpy")
41
- image_crop = gr.Image()
42
- button_crop = gr.Button("Crop")
43
- button_crop.click(crop, annotator_crop, image_crop)
44
  with gr.Tab("Object annotation"):
45
  annotator = image_annotator(
46
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
@@ -50,6 +44,12 @@ with gr.Blocks() as demo:
50
  button_get = gr.Button("Get bounding boxes")
51
  json_boxes = gr.JSON()
52
  button_get.click(get_boxes_json, annotator, json_boxes)
 
 
 
 
 
 
53
 
54
 
55
  if __name__ == "__main__":
 
35
 
36
 
37
  with gr.Blocks() as demo:
 
 
 
 
 
 
38
  with gr.Tab("Object annotation"):
39
  annotator = image_annotator(
40
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
 
44
  button_get = gr.Button("Get bounding boxes")
45
  json_boxes = gr.JSON()
46
  button_get.click(get_boxes_json, annotator, json_boxes)
47
+ with gr.Tab("Crop"):
48
+ with gr.Row():
49
+ annotator_crop = image_annotator(example, image_type="numpy")
50
+ image_crop = gr.Image()
51
+ button_crop = gr.Button("Crop")
52
+ button_crop.click(crop, annotator_crop, image_crop)
53
 
54
 
55
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1 +1 @@
1
- gradio_image_annotation==0.0.4
 
1
+ gradio_image_annotation==0.0.5
space.py CHANGED
@@ -75,12 +75,6 @@ def get_boxes_json(annotations):
75
 
76
 
77
  with gr.Blocks() as demo:
78
- with gr.Tab("Crop"):
79
- with gr.Row():
80
- annotator_crop = image_annotator(example, image_type="numpy")
81
- image_crop = gr.Image()
82
- button_crop = gr.Button("Crop")
83
- button_crop.click(crop, annotator_crop, image_crop)
84
  with gr.Tab("Object annotation"):
85
  annotator = image_annotator(
86
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
@@ -90,6 +84,12 @@ with gr.Blocks() as demo:
90
  button_get = gr.Button("Get bounding boxes")
91
  json_boxes = gr.JSON()
92
  button_get.click(get_boxes_json, annotator, json_boxes)
 
 
 
 
 
 
93
 
94
 
95
  if __name__ == "__main__":
@@ -138,40 +138,41 @@ def predict(
138
 
139
 
140
 
141
- demo.load()
142
- # demo.load(None, js=r"""function() {
143
- # const refs = {};
144
- # const user_fn_refs = {
145
- # image_annotator: [], };
146
- # requestAnimationFrame(() => {
147
-
148
- # Object.entries(user_fn_refs).forEach(([key, refs]) => {
149
- # if (refs.length > 0) {
150
- # const el = document.querySelector(`.${key}-user-fn`);
151
- # if (!el) return;
152
- # refs.forEach(ref => {
153
- # el.innerHTML = el.innerHTML.replace(
154
- # new RegExp("\\b"+ref+"\\b", "g"),
155
- # `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
156
- # );
157
- # })
158
- # }
159
- # })
160
-
161
- # Object.entries(refs).forEach(([key, refs]) => {
162
- # if (refs.length > 0) {
163
- # const el = document.querySelector(`.${key}`);
164
- # if (!el) return;
165
- # refs.forEach(ref => {
166
- # el.innerHTML = el.innerHTML.replace(
167
- # new RegExp("\\b"+ref+"\\b", "g"),
168
- # `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
169
- # );
170
- # })
171
- # }
172
- # })
173
- # })
174
- # }
175
- # """)
 
176
 
177
  demo.launch()
 
75
 
76
 
77
  with gr.Blocks() as demo:
 
 
 
 
 
 
78
  with gr.Tab("Object annotation"):
79
  annotator = image_annotator(
80
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
 
84
  button_get = gr.Button("Get bounding boxes")
85
  json_boxes = gr.JSON()
86
  button_get.click(get_boxes_json, annotator, json_boxes)
87
+ with gr.Tab("Crop"):
88
+ with gr.Row():
89
+ annotator_crop = image_annotator(example, image_type="numpy")
90
+ image_crop = gr.Image()
91
+ button_crop = gr.Button("Crop")
92
+ button_crop.click(crop, annotator_crop, image_crop)
93
 
94
 
95
  if __name__ == "__main__":
 
138
 
139
 
140
 
141
+
142
+ demo.load(None, js=r"""function() {
143
+ const refs = {};
144
+ const user_fn_refs = {
145
+ image_annotator: [], };
146
+ requestAnimationFrame(() => {
147
+
148
+ Object.entries(user_fn_refs).forEach(([key, refs]) => {
149
+ if (refs.length > 0) {
150
+ const el = document.querySelector(`.${key}-user-fn`);
151
+ if (!el) return;
152
+ refs.forEach(ref => {
153
+ el.innerHTML = el.innerHTML.replace(
154
+ new RegExp("\\b"+ref+"\\b", "g"),
155
+ `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
156
+ );
157
+ })
158
+ }
159
+ })
160
+
161
+ Object.entries(refs).forEach(([key, refs]) => {
162
+ if (refs.length > 0) {
163
+ const el = document.querySelector(`.${key}`);
164
+ if (!el) return;
165
+ refs.forEach(ref => {
166
+ el.innerHTML = el.innerHTML.replace(
167
+ new RegExp("\\b"+ref+"\\b", "g"),
168
+ `<a href="#h-${ref.toLowerCase()}">${ref}</a>`
169
+ );
170
+ })
171
+ }
172
+ })
173
+ })
174
+ }
175
+
176
+ """)
177
 
178
  demo.launch()
src/.gitignore CHANGED
@@ -1,9 +1,9 @@
1
- .eggs/
2
- dist/
3
- *.pyc
4
- __pycache__/
5
- *.py[cod]
6
- *$py.class
7
- __tmp/*
8
- *.pyi
9
  node_modules
 
1
+ .eggs/
2
+ dist/
3
+ *.pyc
4
+ __pycache__/
5
+ *.py[cod]
6
+ *$py.class
7
+ __tmp/*
8
+ *.pyi
9
  node_modules
src/README.md CHANGED
@@ -50,12 +50,6 @@ def get_boxes_json(annotations):
50
 
51
 
52
  with gr.Blocks() as demo:
53
- with gr.Tab("Crop"):
54
- with gr.Row():
55
- annotator_crop = image_annotator(example, image_type="numpy")
56
- image_crop = gr.Image()
57
- button_crop = gr.Button("Crop")
58
- button_crop.click(crop, annotator_crop, image_crop)
59
  with gr.Tab("Object annotation"):
60
  annotator = image_annotator(
61
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
@@ -65,6 +59,12 @@ with gr.Blocks() as demo:
65
  button_get = gr.Button("Get bounding boxes")
66
  json_boxes = gr.JSON()
67
  button_get.click(get_boxes_json, annotator, json_boxes)
 
 
 
 
 
 
68
 
69
 
70
  if __name__ == "__main__":
 
50
 
51
 
52
  with gr.Blocks() as demo:
 
 
 
 
 
 
53
  with gr.Tab("Object annotation"):
54
  annotator = image_annotator(
55
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
 
59
  button_get = gr.Button("Get bounding boxes")
60
  json_boxes = gr.JSON()
61
  button_get.click(get_boxes_json, annotator, json_boxes)
62
+ with gr.Tab("Crop"):
63
+ with gr.Row():
64
+ annotator_crop = image_annotator(example, image_type="numpy")
65
+ image_crop = gr.Image()
66
+ button_crop = gr.Button("Crop")
67
+ button_crop.click(crop, annotator_crop, image_crop)
68
 
69
 
70
  if __name__ == "__main__":
src/backend/gradio_image_annotation/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
-
2
- from .image_annotator import image_annotator
3
-
4
- __all__ = ['image_annotator']
 
1
+
2
+ from .image_annotator import image_annotator
3
+
4
+ __all__ = ['image_annotator']
src/backend/gradio_image_annotation/templates/component/index.js CHANGED
The diff for this file is too large to render. See raw diff
 
src/backend/gradio_image_annotation/templates/component/wrapper-6f348d45-19fa94bf.js ADDED
@@ -0,0 +1,2453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import S from "./__vite-browser-external-2447137e.js";
2
+ function z(s) {
3
+ return s && s.__esModule && Object.prototype.hasOwnProperty.call(s, "default") ? s.default : s;
4
+ }
5
+ function gt(s) {
6
+ if (s.__esModule)
7
+ return s;
8
+ var e = s.default;
9
+ if (typeof e == "function") {
10
+ var t = function r() {
11
+ if (this instanceof r) {
12
+ var i = [null];
13
+ i.push.apply(i, arguments);
14
+ var n = Function.bind.apply(e, i);
15
+ return new n();
16
+ }
17
+ return e.apply(this, arguments);
18
+ };
19
+ t.prototype = e.prototype;
20
+ } else
21
+ t = {};
22
+ return Object.defineProperty(t, "__esModule", { value: !0 }), Object.keys(s).forEach(function(r) {
23
+ var i = Object.getOwnPropertyDescriptor(s, r);
24
+ Object.defineProperty(t, r, i.get ? i : {
25
+ enumerable: !0,
26
+ get: function() {
27
+ return s[r];
28
+ }
29
+ });
30
+ }), t;
31
+ }
32
+ const { Duplex: yt } = S;
33
+ function Oe(s) {
34
+ s.emit("close");
35
+ }
36
+ function vt() {
37
+ !this.destroyed && this._writableState.finished && this.destroy();
38
+ }
39
+ function Qe(s) {
40
+ this.removeListener("error", Qe), this.destroy(), this.listenerCount("error") === 0 && this.emit("error", s);
41
+ }
42
+ function St(s, e) {
43
+ let t = !0;
44
+ const r = new yt({
45
+ ...e,
46
+ autoDestroy: !1,
47
+ emitClose: !1,
48
+ objectMode: !1,
49
+ writableObjectMode: !1
50
+ });
51
+ return s.on("message", function(n, o) {
52
+ const l = !o && r._readableState.objectMode ? n.toString() : n;
53
+ r.push(l) || s.pause();
54
+ }), s.once("error", function(n) {
55
+ r.destroyed || (t = !1, r.destroy(n));
56
+ }), s.once("close", function() {
57
+ r.destroyed || r.push(null);
58
+ }), r._destroy = function(i, n) {
59
+ if (s.readyState === s.CLOSED) {
60
+ n(i), process.nextTick(Oe, r);
61
+ return;
62
+ }
63
+ let o = !1;
64
+ s.once("error", function(f) {
65
+ o = !0, n(f);
66
+ }), s.once("close", function() {
67
+ o || n(i), process.nextTick(Oe, r);
68
+ }), t && s.terminate();
69
+ }, r._final = function(i) {
70
+ if (s.readyState === s.CONNECTING) {
71
+ s.once("open", function() {
72
+ r._final(i);
73
+ });
74
+ return;
75
+ }
76
+ s._socket !== null && (s._socket._writableState.finished ? (i(), r._readableState.endEmitted && r.destroy()) : (s._socket.once("finish", function() {
77
+ i();
78
+ }), s.close()));
79
+ }, r._read = function() {
80
+ s.isPaused && s.resume();
81
+ }, r._write = function(i, n, o) {
82
+ if (s.readyState === s.CONNECTING) {
83
+ s.once("open", function() {
84
+ r._write(i, n, o);
85
+ });
86
+ return;
87
+ }
88
+ s.send(i, o);
89
+ }, r.on("end", vt), r.on("error", Qe), r;
90
+ }
91
+ var Et = St;
92
+ const Vs = /* @__PURE__ */ z(Et);
93
+ var te = { exports: {} }, U = {
94
+ BINARY_TYPES: ["nodebuffer", "arraybuffer", "fragments"],
95
+ EMPTY_BUFFER: Buffer.alloc(0),
96
+ GUID: "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
97
+ kForOnEventAttribute: Symbol("kIsForOnEventAttribute"),
98
+ kListener: Symbol("kListener"),
99
+ kStatusCode: Symbol("status-code"),
100
+ kWebSocket: Symbol("websocket"),
101
+ NOOP: () => {
102
+ }
103
+ }, bt, xt;
104
+ const { EMPTY_BUFFER: kt } = U, Se = Buffer[Symbol.species];
105
+ function wt(s, e) {
106
+ if (s.length === 0)
107
+ return kt;
108
+ if (s.length === 1)
109
+ return s[0];
110
+ const t = Buffer.allocUnsafe(e);
111
+ let r = 0;
112
+ for (let i = 0; i < s.length; i++) {
113
+ const n = s[i];
114
+ t.set(n, r), r += n.length;
115
+ }
116
+ return r < e ? new Se(t.buffer, t.byteOffset, r) : t;
117
+ }
118
+ function Je(s, e, t, r, i) {
119
+ for (let n = 0; n < i; n++)
120
+ t[r + n] = s[n] ^ e[n & 3];
121
+ }
122
+ function et(s, e) {
123
+ for (let t = 0; t < s.length; t++)
124
+ s[t] ^= e[t & 3];
125
+ }
126
+ function Ot(s) {
127
+ return s.length === s.buffer.byteLength ? s.buffer : s.buffer.slice(s.byteOffset, s.byteOffset + s.length);
128
+ }
129
+ function Ee(s) {
130
+ if (Ee.readOnly = !0, Buffer.isBuffer(s))
131
+ return s;
132
+ let e;
133
+ return s instanceof ArrayBuffer ? e = new Se(s) : ArrayBuffer.isView(s) ? e = new Se(s.buffer, s.byteOffset, s.byteLength) : (e = Buffer.from(s), Ee.readOnly = !1), e;
134
+ }
135
+ te.exports = {
136
+ concat: wt,
137
+ mask: Je,
138
+ toArrayBuffer: Ot,
139
+ toBuffer: Ee,
140
+ unmask: et
141
+ };
142
+ if (!process.env.WS_NO_BUFFER_UTIL)
143
+ try {
144
+ const s = require("bufferutil");
145
+ xt = te.exports.mask = function(e, t, r, i, n) {
146
+ n < 48 ? Je(e, t, r, i, n) : s.mask(e, t, r, i, n);
147
+ }, bt = te.exports.unmask = function(e, t) {
148
+ e.length < 32 ? et(e, t) : s.unmask(e, t);
149
+ };
150
+ } catch {
151
+ }
152
+ var ne = te.exports;
153
+ const Ce = Symbol("kDone"), ue = Symbol("kRun");
154
+ let Ct = class {
155
+ /**
156
+ * Creates a new `Limiter`.
157
+ *
158
+ * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
159
+ * to run concurrently
160
+ */
161
+ constructor(e) {
162
+ this[Ce] = () => {
163
+ this.pending--, this[ue]();
164
+ }, this.concurrency = e || 1 / 0, this.jobs = [], this.pending = 0;
165
+ }
166
+ /**
167
+ * Adds a job to the queue.
168
+ *
169
+ * @param {Function} job The job to run
170
+ * @public
171
+ */
172
+ add(e) {
173
+ this.jobs.push(e), this[ue]();
174
+ }
175
+ /**
176
+ * Removes a job from the queue and runs it if possible.
177
+ *
178
+ * @private
179
+ */
180
+ [ue]() {
181
+ if (this.pending !== this.concurrency && this.jobs.length) {
182
+ const e = this.jobs.shift();
183
+ this.pending++, e(this[Ce]);
184
+ }
185
+ }
186
+ };
187
+ var Tt = Ct;
188
+ const W = S, Te = ne, Lt = Tt, { kStatusCode: tt } = U, Nt = Buffer[Symbol.species], Pt = Buffer.from([0, 0, 255, 255]), se = Symbol("permessage-deflate"), w = Symbol("total-length"), V = Symbol("callback"), C = Symbol("buffers"), J = Symbol("error");
189
+ let K, Rt = class {
190
+ /**
191
+ * Creates a PerMessageDeflate instance.
192
+ *
193
+ * @param {Object} [options] Configuration options
194
+ * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
195
+ * for, or request, a custom client window size
196
+ * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
197
+ * acknowledge disabling of client context takeover
198
+ * @param {Number} [options.concurrencyLimit=10] The number of concurrent
199
+ * calls to zlib
200
+ * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
201
+ * use of a custom server window size
202
+ * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
203
+ * disabling of server context takeover
204
+ * @param {Number} [options.threshold=1024] Size (in bytes) below which
205
+ * messages should not be compressed if context takeover is disabled
206
+ * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
207
+ * deflate
208
+ * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
209
+ * inflate
210
+ * @param {Boolean} [isServer=false] Create the instance in either server or
211
+ * client mode
212
+ * @param {Number} [maxPayload=0] The maximum allowed message length
213
+ */
214
+ constructor(e, t, r) {
215
+ if (this._maxPayload = r | 0, this._options = e || {}, this._threshold = this._options.threshold !== void 0 ? this._options.threshold : 1024, this._isServer = !!t, this._deflate = null, this._inflate = null, this.params = null, !K) {
216
+ const i = this._options.concurrencyLimit !== void 0 ? this._options.concurrencyLimit : 10;
217
+ K = new Lt(i);
218
+ }
219
+ }
220
+ /**
221
+ * @type {String}
222
+ */
223
+ static get extensionName() {
224
+ return "permessage-deflate";
225
+ }
226
+ /**
227
+ * Create an extension negotiation offer.
228
+ *
229
+ * @return {Object} Extension parameters
230
+ * @public
231
+ */
232
+ offer() {
233
+ const e = {};
234
+ return this._options.serverNoContextTakeover && (e.server_no_context_takeover = !0), this._options.clientNoContextTakeover && (e.client_no_context_takeover = !0), this._options.serverMaxWindowBits && (e.server_max_window_bits = this._options.serverMaxWindowBits), this._options.clientMaxWindowBits ? e.client_max_window_bits = this._options.clientMaxWindowBits : this._options.clientMaxWindowBits == null && (e.client_max_window_bits = !0), e;
235
+ }
236
+ /**
237
+ * Accept an extension negotiation offer/response.
238
+ *
239
+ * @param {Array} configurations The extension negotiation offers/reponse
240
+ * @return {Object} Accepted configuration
241
+ * @public
242
+ */
243
+ accept(e) {
244
+ return e = this.normalizeParams(e), this.params = this._isServer ? this.acceptAsServer(e) : this.acceptAsClient(e), this.params;
245
+ }
246
+ /**
247
+ * Releases all resources used by the extension.
248
+ *
249
+ * @public
250
+ */
251
+ cleanup() {
252
+ if (this._inflate && (this._inflate.close(), this._inflate = null), this._deflate) {
253
+ const e = this._deflate[V];
254
+ this._deflate.close(), this._deflate = null, e && e(
255
+ new Error(
256
+ "The deflate stream was closed while data was being processed"
257
+ )
258
+ );
259
+ }
260
+ }
261
+ /**
262
+ * Accept an extension negotiation offer.
263
+ *
264
+ * @param {Array} offers The extension negotiation offers
265
+ * @return {Object} Accepted configuration
266
+ * @private
267
+ */
268
+ acceptAsServer(e) {
269
+ const t = this._options, r = e.find((i) => !(t.serverNoContextTakeover === !1 && i.server_no_context_takeover || i.server_max_window_bits && (t.serverMaxWindowBits === !1 || typeof t.serverMaxWindowBits == "number" && t.serverMaxWindowBits > i.server_max_window_bits) || typeof t.clientMaxWindowBits == "number" && !i.client_max_window_bits));
270
+ if (!r)
271
+ throw new Error("None of the extension offers can be accepted");
272
+ return t.serverNoContextTakeover && (r.server_no_context_takeover = !0), t.clientNoContextTakeover && (r.client_no_context_takeover = !0), typeof t.serverMaxWindowBits == "number" && (r.server_max_window_bits = t.serverMaxWindowBits), typeof t.clientMaxWindowBits == "number" ? r.client_max_window_bits = t.clientMaxWindowBits : (r.client_max_window_bits === !0 || t.clientMaxWindowBits === !1) && delete r.client_max_window_bits, r;
273
+ }
274
+ /**
275
+ * Accept the extension negotiation response.
276
+ *
277
+ * @param {Array} response The extension negotiation response
278
+ * @return {Object} Accepted configuration
279
+ * @private
280
+ */
281
+ acceptAsClient(e) {
282
+ const t = e[0];
283
+ if (this._options.clientNoContextTakeover === !1 && t.client_no_context_takeover)
284
+ throw new Error('Unexpected parameter "client_no_context_takeover"');
285
+ if (!t.client_max_window_bits)
286
+ typeof this._options.clientMaxWindowBits == "number" && (t.client_max_window_bits = this._options.clientMaxWindowBits);
287
+ else if (this._options.clientMaxWindowBits === !1 || typeof this._options.clientMaxWindowBits == "number" && t.client_max_window_bits > this._options.clientMaxWindowBits)
288
+ throw new Error(
289
+ 'Unexpected or invalid parameter "client_max_window_bits"'
290
+ );
291
+ return t;
292
+ }
293
+ /**
294
+ * Normalize parameters.
295
+ *
296
+ * @param {Array} configurations The extension negotiation offers/reponse
297
+ * @return {Array} The offers/response with normalized parameters
298
+ * @private
299
+ */
300
+ normalizeParams(e) {
301
+ return e.forEach((t) => {
302
+ Object.keys(t).forEach((r) => {
303
+ let i = t[r];
304
+ if (i.length > 1)
305
+ throw new Error(`Parameter "${r}" must have only a single value`);
306
+ if (i = i[0], r === "client_max_window_bits") {
307
+ if (i !== !0) {
308
+ const n = +i;
309
+ if (!Number.isInteger(n) || n < 8 || n > 15)
310
+ throw new TypeError(
311
+ `Invalid value for parameter "${r}": ${i}`
312
+ );
313
+ i = n;
314
+ } else if (!this._isServer)
315
+ throw new TypeError(
316
+ `Invalid value for parameter "${r}": ${i}`
317
+ );
318
+ } else if (r === "server_max_window_bits") {
319
+ const n = +i;
320
+ if (!Number.isInteger(n) || n < 8 || n > 15)
321
+ throw new TypeError(
322
+ `Invalid value for parameter "${r}": ${i}`
323
+ );
324
+ i = n;
325
+ } else if (r === "client_no_context_takeover" || r === "server_no_context_takeover") {
326
+ if (i !== !0)
327
+ throw new TypeError(
328
+ `Invalid value for parameter "${r}": ${i}`
329
+ );
330
+ } else
331
+ throw new Error(`Unknown parameter "${r}"`);
332
+ t[r] = i;
333
+ });
334
+ }), e;
335
+ }
336
+ /**
337
+ * Decompress data. Concurrency limited.
338
+ *
339
+ * @param {Buffer} data Compressed data
340
+ * @param {Boolean} fin Specifies whether or not this is the last fragment
341
+ * @param {Function} callback Callback
342
+ * @public
343
+ */
344
+ decompress(e, t, r) {
345
+ K.add((i) => {
346
+ this._decompress(e, t, (n, o) => {
347
+ i(), r(n, o);
348
+ });
349
+ });
350
+ }
351
+ /**
352
+ * Compress data. Concurrency limited.
353
+ *
354
+ * @param {(Buffer|String)} data Data to compress
355
+ * @param {Boolean} fin Specifies whether or not this is the last fragment
356
+ * @param {Function} callback Callback
357
+ * @public
358
+ */
359
+ compress(e, t, r) {
360
+ K.add((i) => {
361
+ this._compress(e, t, (n, o) => {
362
+ i(), r(n, o);
363
+ });
364
+ });
365
+ }
366
+ /**
367
+ * Decompress data.
368
+ *
369
+ * @param {Buffer} data Compressed data
370
+ * @param {Boolean} fin Specifies whether or not this is the last fragment
371
+ * @param {Function} callback Callback
372
+ * @private
373
+ */
374
+ _decompress(e, t, r) {
375
+ const i = this._isServer ? "client" : "server";
376
+ if (!this._inflate) {
377
+ const n = `${i}_max_window_bits`, o = typeof this.params[n] != "number" ? W.Z_DEFAULT_WINDOWBITS : this.params[n];
378
+ this._inflate = W.createInflateRaw({
379
+ ...this._options.zlibInflateOptions,
380
+ windowBits: o
381
+ }), this._inflate[se] = this, this._inflate[w] = 0, this._inflate[C] = [], this._inflate.on("error", Bt), this._inflate.on("data", st);
382
+ }
383
+ this._inflate[V] = r, this._inflate.write(e), t && this._inflate.write(Pt), this._inflate.flush(() => {
384
+ const n = this._inflate[J];
385
+ if (n) {
386
+ this._inflate.close(), this._inflate = null, r(n);
387
+ return;
388
+ }
389
+ const o = Te.concat(
390
+ this._inflate[C],
391
+ this._inflate[w]
392
+ );
393
+ this._inflate._readableState.endEmitted ? (this._inflate.close(), this._inflate = null) : (this._inflate[w] = 0, this._inflate[C] = [], t && this.params[`${i}_no_context_takeover`] && this._inflate.reset()), r(null, o);
394
+ });
395
+ }
396
+ /**
397
+ * Compress data.
398
+ *
399
+ * @param {(Buffer|String)} data Data to compress
400
+ * @param {Boolean} fin Specifies whether or not this is the last fragment
401
+ * @param {Function} callback Callback
402
+ * @private
403
+ */
404
+ _compress(e, t, r) {
405
+ const i = this._isServer ? "server" : "client";
406
+ if (!this._deflate) {
407
+ const n = `${i}_max_window_bits`, o = typeof this.params[n] != "number" ? W.Z_DEFAULT_WINDOWBITS : this.params[n];
408
+ this._deflate = W.createDeflateRaw({
409
+ ...this._options.zlibDeflateOptions,
410
+ windowBits: o
411
+ }), this._deflate[w] = 0, this._deflate[C] = [], this._deflate.on("data", Ut);
412
+ }
413
+ this._deflate[V] = r, this._deflate.write(e), this._deflate.flush(W.Z_SYNC_FLUSH, () => {
414
+ if (!this._deflate)
415
+ return;
416
+ let n = Te.concat(
417
+ this._deflate[C],
418
+ this._deflate[w]
419
+ );
420
+ t && (n = new Nt(n.buffer, n.byteOffset, n.length - 4)), this._deflate[V] = null, this._deflate[w] = 0, this._deflate[C] = [], t && this.params[`${i}_no_context_takeover`] && this._deflate.reset(), r(null, n);
421
+ });
422
+ }
423
+ };
424
+ var oe = Rt;
425
+ function Ut(s) {
426
+ this[C].push(s), this[w] += s.length;
427
+ }
428
+ function st(s) {
429
+ if (this[w] += s.length, this[se]._maxPayload < 1 || this[w] <= this[se]._maxPayload) {
430
+ this[C].push(s);
431
+ return;
432
+ }
433
+ this[J] = new RangeError("Max payload size exceeded"), this[J].code = "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH", this[J][tt] = 1009, this.removeListener("data", st), this.reset();
434
+ }
435
+ function Bt(s) {
436
+ this[se]._inflate = null, s[tt] = 1007, this[V](s);
437
+ }
438
+ var re = { exports: {} };
439
+ const $t = {}, Mt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
440
+ __proto__: null,
441
+ default: $t
442
+ }, Symbol.toStringTag, { value: "Module" })), It = /* @__PURE__ */ gt(Mt);
443
+ var Le;
444
+ const { isUtf8: Ne } = S, Dt = [
445
+ 0,
446
+ 0,
447
+ 0,
448
+ 0,
449
+ 0,
450
+ 0,
451
+ 0,
452
+ 0,
453
+ 0,
454
+ 0,
455
+ 0,
456
+ 0,
457
+ 0,
458
+ 0,
459
+ 0,
460
+ 0,
461
+ // 0 - 15
462
+ 0,
463
+ 0,
464
+ 0,
465
+ 0,
466
+ 0,
467
+ 0,
468
+ 0,
469
+ 0,
470
+ 0,
471
+ 0,
472
+ 0,
473
+ 0,
474
+ 0,
475
+ 0,
476
+ 0,
477
+ 0,
478
+ // 16 - 31
479
+ 0,
480
+ 1,
481
+ 0,
482
+ 1,
483
+ 1,
484
+ 1,
485
+ 1,
486
+ 1,
487
+ 0,
488
+ 0,
489
+ 1,
490
+ 1,
491
+ 0,
492
+ 1,
493
+ 1,
494
+ 0,
495
+ // 32 - 47
496
+ 1,
497
+ 1,
498
+ 1,
499
+ 1,
500
+ 1,
501
+ 1,
502
+ 1,
503
+ 1,
504
+ 1,
505
+ 1,
506
+ 0,
507
+ 0,
508
+ 0,
509
+ 0,
510
+ 0,
511
+ 0,
512
+ // 48 - 63
513
+ 0,
514
+ 1,
515
+ 1,
516
+ 1,
517
+ 1,
518
+ 1,
519
+ 1,
520
+ 1,
521
+ 1,
522
+ 1,
523
+ 1,
524
+ 1,
525
+ 1,
526
+ 1,
527
+ 1,
528
+ 1,
529
+ // 64 - 79
530
+ 1,
531
+ 1,
532
+ 1,
533
+ 1,
534
+ 1,
535
+ 1,
536
+ 1,
537
+ 1,
538
+ 1,
539
+ 1,
540
+ 1,
541
+ 0,
542
+ 0,
543
+ 0,
544
+ 1,
545
+ 1,
546
+ // 80 - 95
547
+ 1,
548
+ 1,
549
+ 1,
550
+ 1,
551
+ 1,
552
+ 1,
553
+ 1,
554
+ 1,
555
+ 1,
556
+ 1,
557
+ 1,
558
+ 1,
559
+ 1,
560
+ 1,
561
+ 1,
562
+ 1,
563
+ // 96 - 111
564
+ 1,
565
+ 1,
566
+ 1,
567
+ 1,
568
+ 1,
569
+ 1,
570
+ 1,
571
+ 1,
572
+ 1,
573
+ 1,
574
+ 1,
575
+ 0,
576
+ 1,
577
+ 0,
578
+ 1,
579
+ 0
580
+ // 112 - 127
581
+ ];
582
+ function Wt(s) {
583
+ return s >= 1e3 && s <= 1014 && s !== 1004 && s !== 1005 && s !== 1006 || s >= 3e3 && s <= 4999;
584
+ }
585
+ function be(s) {
586
+ const e = s.length;
587
+ let t = 0;
588
+ for (; t < e; )
589
+ if (!(s[t] & 128))
590
+ t++;
591
+ else if ((s[t] & 224) === 192) {
592
+ if (t + 1 === e || (s[t + 1] & 192) !== 128 || (s[t] & 254) === 192)
593
+ return !1;
594
+ t += 2;
595
+ } else if ((s[t] & 240) === 224) {
596
+ if (t + 2 >= e || (s[t + 1] & 192) !== 128 || (s[t + 2] & 192) !== 128 || s[t] === 224 && (s[t + 1] & 224) === 128 || // Overlong
597
+ s[t] === 237 && (s[t + 1] & 224) === 160)
598
+ return !1;
599
+ t += 3;
600
+ } else if ((s[t] & 248) === 240) {
601
+ if (t + 3 >= e || (s[t + 1] & 192) !== 128 || (s[t + 2] & 192) !== 128 || (s[t + 3] & 192) !== 128 || s[t] === 240 && (s[t + 1] & 240) === 128 || // Overlong
602
+ s[t] === 244 && s[t + 1] > 143 || s[t] > 244)
603
+ return !1;
604
+ t += 4;
605
+ } else
606
+ return !1;
607
+ return !0;
608
+ }
609
+ re.exports = {
610
+ isValidStatusCode: Wt,
611
+ isValidUTF8: be,
612
+ tokenChars: Dt
613
+ };
614
+ if (Ne)
615
+ Le = re.exports.isValidUTF8 = function(s) {
616
+ return s.length < 24 ? be(s) : Ne(s);
617
+ };
618
+ else if (!process.env.WS_NO_UTF_8_VALIDATE)
619
+ try {
620
+ const s = It;
621
+ Le = re.exports.isValidUTF8 = function(e) {
622
+ return e.length < 32 ? be(e) : s(e);
623
+ };
624
+ } catch {
625
+ }
626
+ var ae = re.exports;
627
+ const { Writable: At } = S, Pe = oe, {
628
+ BINARY_TYPES: Ft,
629
+ EMPTY_BUFFER: Re,
630
+ kStatusCode: jt,
631
+ kWebSocket: Gt
632
+ } = U, { concat: de, toArrayBuffer: Vt, unmask: Ht } = ne, { isValidStatusCode: zt, isValidUTF8: Ue } = ae, X = Buffer[Symbol.species], A = 0, Be = 1, $e = 2, Me = 3, _e = 4, Yt = 5;
633
+ let qt = class extends At {
634
+ /**
635
+ * Creates a Receiver instance.
636
+ *
637
+ * @param {Object} [options] Options object
638
+ * @param {String} [options.binaryType=nodebuffer] The type for binary data
639
+ * @param {Object} [options.extensions] An object containing the negotiated
640
+ * extensions
641
+ * @param {Boolean} [options.isServer=false] Specifies whether to operate in
642
+ * client or server mode
643
+ * @param {Number} [options.maxPayload=0] The maximum allowed message length
644
+ * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
645
+ * not to skip UTF-8 validation for text and close messages
646
+ */
647
+ constructor(e = {}) {
648
+ super(), this._binaryType = e.binaryType || Ft[0], this._extensions = e.extensions || {}, this._isServer = !!e.isServer, this._maxPayload = e.maxPayload | 0, this._skipUTF8Validation = !!e.skipUTF8Validation, this[Gt] = void 0, this._bufferedBytes = 0, this._buffers = [], this._compressed = !1, this._payloadLength = 0, this._mask = void 0, this._fragmented = 0, this._masked = !1, this._fin = !1, this._opcode = 0, this._totalPayloadLength = 0, this._messageLength = 0, this._fragments = [], this._state = A, this._loop = !1;
649
+ }
650
+ /**
651
+ * Implements `Writable.prototype._write()`.
652
+ *
653
+ * @param {Buffer} chunk The chunk of data to write
654
+ * @param {String} encoding The character encoding of `chunk`
655
+ * @param {Function} cb Callback
656
+ * @private
657
+ */
658
+ _write(e, t, r) {
659
+ if (this._opcode === 8 && this._state == A)
660
+ return r();
661
+ this._bufferedBytes += e.length, this._buffers.push(e), this.startLoop(r);
662
+ }
663
+ /**
664
+ * Consumes `n` bytes from the buffered data.
665
+ *
666
+ * @param {Number} n The number of bytes to consume
667
+ * @return {Buffer} The consumed bytes
668
+ * @private
669
+ */
670
+ consume(e) {
671
+ if (this._bufferedBytes -= e, e === this._buffers[0].length)
672
+ return this._buffers.shift();
673
+ if (e < this._buffers[0].length) {
674
+ const r = this._buffers[0];
675
+ return this._buffers[0] = new X(
676
+ r.buffer,
677
+ r.byteOffset + e,
678
+ r.length - e
679
+ ), new X(r.buffer, r.byteOffset, e);
680
+ }
681
+ const t = Buffer.allocUnsafe(e);
682
+ do {
683
+ const r = this._buffers[0], i = t.length - e;
684
+ e >= r.length ? t.set(this._buffers.shift(), i) : (t.set(new Uint8Array(r.buffer, r.byteOffset, e), i), this._buffers[0] = new X(
685
+ r.buffer,
686
+ r.byteOffset + e,
687
+ r.length - e
688
+ )), e -= r.length;
689
+ } while (e > 0);
690
+ return t;
691
+ }
692
+ /**
693
+ * Starts the parsing loop.
694
+ *
695
+ * @param {Function} cb Callback
696
+ * @private
697
+ */
698
+ startLoop(e) {
699
+ let t;
700
+ this._loop = !0;
701
+ do
702
+ switch (this._state) {
703
+ case A:
704
+ t = this.getInfo();
705
+ break;
706
+ case Be:
707
+ t = this.getPayloadLength16();
708
+ break;
709
+ case $e:
710
+ t = this.getPayloadLength64();
711
+ break;
712
+ case Me:
713
+ this.getMask();
714
+ break;
715
+ case _e:
716
+ t = this.getData(e);
717
+ break;
718
+ default:
719
+ this._loop = !1;
720
+ return;
721
+ }
722
+ while (this._loop);
723
+ e(t);
724
+ }
725
+ /**
726
+ * Reads the first two bytes of a frame.
727
+ *
728
+ * @return {(RangeError|undefined)} A possible error
729
+ * @private
730
+ */
731
+ getInfo() {
732
+ if (this._bufferedBytes < 2) {
733
+ this._loop = !1;
734
+ return;
735
+ }
736
+ const e = this.consume(2);
737
+ if (e[0] & 48)
738
+ return this._loop = !1, g(
739
+ RangeError,
740
+ "RSV2 and RSV3 must be clear",
741
+ !0,
742
+ 1002,
743
+ "WS_ERR_UNEXPECTED_RSV_2_3"
744
+ );
745
+ const t = (e[0] & 64) === 64;
746
+ if (t && !this._extensions[Pe.extensionName])
747
+ return this._loop = !1, g(
748
+ RangeError,
749
+ "RSV1 must be clear",
750
+ !0,
751
+ 1002,
752
+ "WS_ERR_UNEXPECTED_RSV_1"
753
+ );
754
+ if (this._fin = (e[0] & 128) === 128, this._opcode = e[0] & 15, this._payloadLength = e[1] & 127, this._opcode === 0) {
755
+ if (t)
756
+ return this._loop = !1, g(
757
+ RangeError,
758
+ "RSV1 must be clear",
759
+ !0,
760
+ 1002,
761
+ "WS_ERR_UNEXPECTED_RSV_1"
762
+ );
763
+ if (!this._fragmented)
764
+ return this._loop = !1, g(
765
+ RangeError,
766
+ "invalid opcode 0",
767
+ !0,
768
+ 1002,
769
+ "WS_ERR_INVALID_OPCODE"
770
+ );
771
+ this._opcode = this._fragmented;
772
+ } else if (this._opcode === 1 || this._opcode === 2) {
773
+ if (this._fragmented)
774
+ return this._loop = !1, g(
775
+ RangeError,
776
+ `invalid opcode ${this._opcode}`,
777
+ !0,
778
+ 1002,
779
+ "WS_ERR_INVALID_OPCODE"
780
+ );
781
+ this._compressed = t;
782
+ } else if (this._opcode > 7 && this._opcode < 11) {
783
+ if (!this._fin)
784
+ return this._loop = !1, g(
785
+ RangeError,
786
+ "FIN must be set",
787
+ !0,
788
+ 1002,
789
+ "WS_ERR_EXPECTED_FIN"
790
+ );
791
+ if (t)
792
+ return this._loop = !1, g(
793
+ RangeError,
794
+ "RSV1 must be clear",
795
+ !0,
796
+ 1002,
797
+ "WS_ERR_UNEXPECTED_RSV_1"
798
+ );
799
+ if (this._payloadLength > 125 || this._opcode === 8 && this._payloadLength === 1)
800
+ return this._loop = !1, g(
801
+ RangeError,
802
+ `invalid payload length ${this._payloadLength}`,
803
+ !0,
804
+ 1002,
805
+ "WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH"
806
+ );
807
+ } else
808
+ return this._loop = !1, g(
809
+ RangeError,
810
+ `invalid opcode ${this._opcode}`,
811
+ !0,
812
+ 1002,
813
+ "WS_ERR_INVALID_OPCODE"
814
+ );
815
+ if (!this._fin && !this._fragmented && (this._fragmented = this._opcode), this._masked = (e[1] & 128) === 128, this._isServer) {
816
+ if (!this._masked)
817
+ return this._loop = !1, g(
818
+ RangeError,
819
+ "MASK must be set",
820
+ !0,
821
+ 1002,
822
+ "WS_ERR_EXPECTED_MASK"
823
+ );
824
+ } else if (this._masked)
825
+ return this._loop = !1, g(
826
+ RangeError,
827
+ "MASK must be clear",
828
+ !0,
829
+ 1002,
830
+ "WS_ERR_UNEXPECTED_MASK"
831
+ );
832
+ if (this._payloadLength === 126)
833
+ this._state = Be;
834
+ else if (this._payloadLength === 127)
835
+ this._state = $e;
836
+ else
837
+ return this.haveLength();
838
+ }
839
+ /**
840
+ * Gets extended payload length (7+16).
841
+ *
842
+ * @return {(RangeError|undefined)} A possible error
843
+ * @private
844
+ */
845
+ getPayloadLength16() {
846
+ if (this._bufferedBytes < 2) {
847
+ this._loop = !1;
848
+ return;
849
+ }
850
+ return this._payloadLength = this.consume(2).readUInt16BE(0), this.haveLength();
851
+ }
852
+ /**
853
+ * Gets extended payload length (7+64).
854
+ *
855
+ * @return {(RangeError|undefined)} A possible error
856
+ * @private
857
+ */
858
+ getPayloadLength64() {
859
+ if (this._bufferedBytes < 8) {
860
+ this._loop = !1;
861
+ return;
862
+ }
863
+ const e = this.consume(8), t = e.readUInt32BE(0);
864
+ return t > Math.pow(2, 21) - 1 ? (this._loop = !1, g(
865
+ RangeError,
866
+ "Unsupported WebSocket frame: payload length > 2^53 - 1",
867
+ !1,
868
+ 1009,
869
+ "WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH"
870
+ )) : (this._payloadLength = t * Math.pow(2, 32) + e.readUInt32BE(4), this.haveLength());
871
+ }
872
+ /**
873
+ * Payload length has been read.
874
+ *
875
+ * @return {(RangeError|undefined)} A possible error
876
+ * @private
877
+ */
878
+ haveLength() {
879
+ if (this._payloadLength && this._opcode < 8 && (this._totalPayloadLength += this._payloadLength, this._totalPayloadLength > this._maxPayload && this._maxPayload > 0))
880
+ return this._loop = !1, g(
881
+ RangeError,
882
+ "Max payload size exceeded",
883
+ !1,
884
+ 1009,
885
+ "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
886
+ );
887
+ this._masked ? this._state = Me : this._state = _e;
888
+ }
889
+ /**
890
+ * Reads mask bytes.
891
+ *
892
+ * @private
893
+ */
894
+ getMask() {
895
+ if (this._bufferedBytes < 4) {
896
+ this._loop = !1;
897
+ return;
898
+ }
899
+ this._mask = this.consume(4), this._state = _e;
900
+ }
901
+ /**
902
+ * Reads data bytes.
903
+ *
904
+ * @param {Function} cb Callback
905
+ * @return {(Error|RangeError|undefined)} A possible error
906
+ * @private
907
+ */
908
+ getData(e) {
909
+ let t = Re;
910
+ if (this._payloadLength) {
911
+ if (this._bufferedBytes < this._payloadLength) {
912
+ this._loop = !1;
913
+ return;
914
+ }
915
+ t = this.consume(this._payloadLength), this._masked && this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3] && Ht(t, this._mask);
916
+ }
917
+ if (this._opcode > 7)
918
+ return this.controlMessage(t);
919
+ if (this._compressed) {
920
+ this._state = Yt, this.decompress(t, e);
921
+ return;
922
+ }
923
+ return t.length && (this._messageLength = this._totalPayloadLength, this._fragments.push(t)), this.dataMessage();
924
+ }
925
+ /**
926
+ * Decompresses data.
927
+ *
928
+ * @param {Buffer} data Compressed data
929
+ * @param {Function} cb Callback
930
+ * @private
931
+ */
932
+ decompress(e, t) {
933
+ this._extensions[Pe.extensionName].decompress(e, this._fin, (i, n) => {
934
+ if (i)
935
+ return t(i);
936
+ if (n.length) {
937
+ if (this._messageLength += n.length, this._messageLength > this._maxPayload && this._maxPayload > 0)
938
+ return t(
939
+ g(
940
+ RangeError,
941
+ "Max payload size exceeded",
942
+ !1,
943
+ 1009,
944
+ "WS_ERR_UNSUPPORTED_MESSAGE_LENGTH"
945
+ )
946
+ );
947
+ this._fragments.push(n);
948
+ }
949
+ const o = this.dataMessage();
950
+ if (o)
951
+ return t(o);
952
+ this.startLoop(t);
953
+ });
954
+ }
955
+ /**
956
+ * Handles a data message.
957
+ *
958
+ * @return {(Error|undefined)} A possible error
959
+ * @private
960
+ */
961
+ dataMessage() {
962
+ if (this._fin) {
963
+ const e = this._messageLength, t = this._fragments;
964
+ if (this._totalPayloadLength = 0, this._messageLength = 0, this._fragmented = 0, this._fragments = [], this._opcode === 2) {
965
+ let r;
966
+ this._binaryType === "nodebuffer" ? r = de(t, e) : this._binaryType === "arraybuffer" ? r = Vt(de(t, e)) : r = t, this.emit("message", r, !0);
967
+ } else {
968
+ const r = de(t, e);
969
+ if (!this._skipUTF8Validation && !Ue(r))
970
+ return this._loop = !1, g(
971
+ Error,
972
+ "invalid UTF-8 sequence",
973
+ !0,
974
+ 1007,
975
+ "WS_ERR_INVALID_UTF8"
976
+ );
977
+ this.emit("message", r, !1);
978
+ }
979
+ }
980
+ this._state = A;
981
+ }
982
+ /**
983
+ * Handles a control message.
984
+ *
985
+ * @param {Buffer} data Data to handle
986
+ * @return {(Error|RangeError|undefined)} A possible error
987
+ * @private
988
+ */
989
+ controlMessage(e) {
990
+ if (this._opcode === 8)
991
+ if (this._loop = !1, e.length === 0)
992
+ this.emit("conclude", 1005, Re), this.end();
993
+ else {
994
+ const t = e.readUInt16BE(0);
995
+ if (!zt(t))
996
+ return g(
997
+ RangeError,
998
+ `invalid status code ${t}`,
999
+ !0,
1000
+ 1002,
1001
+ "WS_ERR_INVALID_CLOSE_CODE"
1002
+ );
1003
+ const r = new X(
1004
+ e.buffer,
1005
+ e.byteOffset + 2,
1006
+ e.length - 2
1007
+ );
1008
+ if (!this._skipUTF8Validation && !Ue(r))
1009
+ return g(
1010
+ Error,
1011
+ "invalid UTF-8 sequence",
1012
+ !0,
1013
+ 1007,
1014
+ "WS_ERR_INVALID_UTF8"
1015
+ );
1016
+ this.emit("conclude", t, r), this.end();
1017
+ }
1018
+ else
1019
+ this._opcode === 9 ? this.emit("ping", e) : this.emit("pong", e);
1020
+ this._state = A;
1021
+ }
1022
+ };
1023
+ var rt = qt;
1024
+ function g(s, e, t, r, i) {
1025
+ const n = new s(
1026
+ t ? `Invalid WebSocket frame: ${e}` : e
1027
+ );
1028
+ return Error.captureStackTrace(n, g), n.code = i, n[jt] = r, n;
1029
+ }
1030
+ const qs = /* @__PURE__ */ z(rt), { randomFillSync: Kt } = S, Ie = oe, { EMPTY_BUFFER: Xt } = U, { isValidStatusCode: Zt } = ae, { mask: De, toBuffer: M } = ne, x = Symbol("kByteLength"), Qt = Buffer.alloc(4);
1031
+ let Jt = class P {
1032
+ /**
1033
+ * Creates a Sender instance.
1034
+ *
1035
+ * @param {(net.Socket|tls.Socket)} socket The connection socket
1036
+ * @param {Object} [extensions] An object containing the negotiated extensions
1037
+ * @param {Function} [generateMask] The function used to generate the masking
1038
+ * key
1039
+ */
1040
+ constructor(e, t, r) {
1041
+ this._extensions = t || {}, r && (this._generateMask = r, this._maskBuffer = Buffer.alloc(4)), this._socket = e, this._firstFragment = !0, this._compress = !1, this._bufferedBytes = 0, this._deflating = !1, this._queue = [];
1042
+ }
1043
+ /**
1044
+ * Frames a piece of data according to the HyBi WebSocket protocol.
1045
+ *
1046
+ * @param {(Buffer|String)} data The data to frame
1047
+ * @param {Object} options Options object
1048
+ * @param {Boolean} [options.fin=false] Specifies whether or not to set the
1049
+ * FIN bit
1050
+ * @param {Function} [options.generateMask] The function used to generate the
1051
+ * masking key
1052
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
1053
+ * `data`
1054
+ * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
1055
+ * key
1056
+ * @param {Number} options.opcode The opcode
1057
+ * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
1058
+ * modified
1059
+ * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
1060
+ * RSV1 bit
1061
+ * @return {(Buffer|String)[]} The framed data
1062
+ * @public
1063
+ */
1064
+ static frame(e, t) {
1065
+ let r, i = !1, n = 2, o = !1;
1066
+ t.mask && (r = t.maskBuffer || Qt, t.generateMask ? t.generateMask(r) : Kt(r, 0, 4), o = (r[0] | r[1] | r[2] | r[3]) === 0, n = 6);
1067
+ let l;
1068
+ typeof e == "string" ? (!t.mask || o) && t[x] !== void 0 ? l = t[x] : (e = Buffer.from(e), l = e.length) : (l = e.length, i = t.mask && t.readOnly && !o);
1069
+ let f = l;
1070
+ l >= 65536 ? (n += 8, f = 127) : l > 125 && (n += 2, f = 126);
1071
+ const a = Buffer.allocUnsafe(i ? l + n : n);
1072
+ return a[0] = t.fin ? t.opcode | 128 : t.opcode, t.rsv1 && (a[0] |= 64), a[1] = f, f === 126 ? a.writeUInt16BE(l, 2) : f === 127 && (a[2] = a[3] = 0, a.writeUIntBE(l, 4, 6)), t.mask ? (a[1] |= 128, a[n - 4] = r[0], a[n - 3] = r[1], a[n - 2] = r[2], a[n - 1] = r[3], o ? [a, e] : i ? (De(e, r, a, n, l), [a]) : (De(e, r, e, 0, l), [a, e])) : [a, e];
1073
+ }
1074
+ /**
1075
+ * Sends a close message to the other peer.
1076
+ *
1077
+ * @param {Number} [code] The status code component of the body
1078
+ * @param {(String|Buffer)} [data] The message component of the body
1079
+ * @param {Boolean} [mask=false] Specifies whether or not to mask the message
1080
+ * @param {Function} [cb] Callback
1081
+ * @public
1082
+ */
1083
+ close(e, t, r, i) {
1084
+ let n;
1085
+ if (e === void 0)
1086
+ n = Xt;
1087
+ else {
1088
+ if (typeof e != "number" || !Zt(e))
1089
+ throw new TypeError("First argument must be a valid error code number");
1090
+ if (t === void 0 || !t.length)
1091
+ n = Buffer.allocUnsafe(2), n.writeUInt16BE(e, 0);
1092
+ else {
1093
+ const l = Buffer.byteLength(t);
1094
+ if (l > 123)
1095
+ throw new RangeError("The message must not be greater than 123 bytes");
1096
+ n = Buffer.allocUnsafe(2 + l), n.writeUInt16BE(e, 0), typeof t == "string" ? n.write(t, 2) : n.set(t, 2);
1097
+ }
1098
+ }
1099
+ const o = {
1100
+ [x]: n.length,
1101
+ fin: !0,
1102
+ generateMask: this._generateMask,
1103
+ mask: r,
1104
+ maskBuffer: this._maskBuffer,
1105
+ opcode: 8,
1106
+ readOnly: !1,
1107
+ rsv1: !1
1108
+ };
1109
+ this._deflating ? this.enqueue([this.dispatch, n, !1, o, i]) : this.sendFrame(P.frame(n, o), i);
1110
+ }
1111
+ /**
1112
+ * Sends a ping message to the other peer.
1113
+ *
1114
+ * @param {*} data The message to send
1115
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
1116
+ * @param {Function} [cb] Callback
1117
+ * @public
1118
+ */
1119
+ ping(e, t, r) {
1120
+ let i, n;
1121
+ if (typeof e == "string" ? (i = Buffer.byteLength(e), n = !1) : (e = M(e), i = e.length, n = M.readOnly), i > 125)
1122
+ throw new RangeError("The data size must not be greater than 125 bytes");
1123
+ const o = {
1124
+ [x]: i,
1125
+ fin: !0,
1126
+ generateMask: this._generateMask,
1127
+ mask: t,
1128
+ maskBuffer: this._maskBuffer,
1129
+ opcode: 9,
1130
+ readOnly: n,
1131
+ rsv1: !1
1132
+ };
1133
+ this._deflating ? this.enqueue([this.dispatch, e, !1, o, r]) : this.sendFrame(P.frame(e, o), r);
1134
+ }
1135
+ /**
1136
+ * Sends a pong message to the other peer.
1137
+ *
1138
+ * @param {*} data The message to send
1139
+ * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
1140
+ * @param {Function} [cb] Callback
1141
+ * @public
1142
+ */
1143
+ pong(e, t, r) {
1144
+ let i, n;
1145
+ if (typeof e == "string" ? (i = Buffer.byteLength(e), n = !1) : (e = M(e), i = e.length, n = M.readOnly), i > 125)
1146
+ throw new RangeError("The data size must not be greater than 125 bytes");
1147
+ const o = {
1148
+ [x]: i,
1149
+ fin: !0,
1150
+ generateMask: this._generateMask,
1151
+ mask: t,
1152
+ maskBuffer: this._maskBuffer,
1153
+ opcode: 10,
1154
+ readOnly: n,
1155
+ rsv1: !1
1156
+ };
1157
+ this._deflating ? this.enqueue([this.dispatch, e, !1, o, r]) : this.sendFrame(P.frame(e, o), r);
1158
+ }
1159
+ /**
1160
+ * Sends a data message to the other peer.
1161
+ *
1162
+ * @param {*} data The message to send
1163
+ * @param {Object} options Options object
1164
+ * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
1165
+ * or text
1166
+ * @param {Boolean} [options.compress=false] Specifies whether or not to
1167
+ * compress `data`
1168
+ * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
1169
+ * last one
1170
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
1171
+ * `data`
1172
+ * @param {Function} [cb] Callback
1173
+ * @public
1174
+ */
1175
+ send(e, t, r) {
1176
+ const i = this._extensions[Ie.extensionName];
1177
+ let n = t.binary ? 2 : 1, o = t.compress, l, f;
1178
+ if (typeof e == "string" ? (l = Buffer.byteLength(e), f = !1) : (e = M(e), l = e.length, f = M.readOnly), this._firstFragment ? (this._firstFragment = !1, o && i && i.params[i._isServer ? "server_no_context_takeover" : "client_no_context_takeover"] && (o = l >= i._threshold), this._compress = o) : (o = !1, n = 0), t.fin && (this._firstFragment = !0), i) {
1179
+ const a = {
1180
+ [x]: l,
1181
+ fin: t.fin,
1182
+ generateMask: this._generateMask,
1183
+ mask: t.mask,
1184
+ maskBuffer: this._maskBuffer,
1185
+ opcode: n,
1186
+ readOnly: f,
1187
+ rsv1: o
1188
+ };
1189
+ this._deflating ? this.enqueue([this.dispatch, e, this._compress, a, r]) : this.dispatch(e, this._compress, a, r);
1190
+ } else
1191
+ this.sendFrame(
1192
+ P.frame(e, {
1193
+ [x]: l,
1194
+ fin: t.fin,
1195
+ generateMask: this._generateMask,
1196
+ mask: t.mask,
1197
+ maskBuffer: this._maskBuffer,
1198
+ opcode: n,
1199
+ readOnly: f,
1200
+ rsv1: !1
1201
+ }),
1202
+ r
1203
+ );
1204
+ }
1205
+ /**
1206
+ * Dispatches a message.
1207
+ *
1208
+ * @param {(Buffer|String)} data The message to send
1209
+ * @param {Boolean} [compress=false] Specifies whether or not to compress
1210
+ * `data`
1211
+ * @param {Object} options Options object
1212
+ * @param {Boolean} [options.fin=false] Specifies whether or not to set the
1213
+ * FIN bit
1214
+ * @param {Function} [options.generateMask] The function used to generate the
1215
+ * masking key
1216
+ * @param {Boolean} [options.mask=false] Specifies whether or not to mask
1217
+ * `data`
1218
+ * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
1219
+ * key
1220
+ * @param {Number} options.opcode The opcode
1221
+ * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
1222
+ * modified
1223
+ * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
1224
+ * RSV1 bit
1225
+ * @param {Function} [cb] Callback
1226
+ * @private
1227
+ */
1228
+ dispatch(e, t, r, i) {
1229
+ if (!t) {
1230
+ this.sendFrame(P.frame(e, r), i);
1231
+ return;
1232
+ }
1233
+ const n = this._extensions[Ie.extensionName];
1234
+ this._bufferedBytes += r[x], this._deflating = !0, n.compress(e, r.fin, (o, l) => {
1235
+ if (this._socket.destroyed) {
1236
+ const f = new Error(
1237
+ "The socket was closed while data was being compressed"
1238
+ );
1239
+ typeof i == "function" && i(f);
1240
+ for (let a = 0; a < this._queue.length; a++) {
1241
+ const c = this._queue[a], h = c[c.length - 1];
1242
+ typeof h == "function" && h(f);
1243
+ }
1244
+ return;
1245
+ }
1246
+ this._bufferedBytes -= r[x], this._deflating = !1, r.readOnly = !1, this.sendFrame(P.frame(l, r), i), this.dequeue();
1247
+ });
1248
+ }
1249
+ /**
1250
+ * Executes queued send operations.
1251
+ *
1252
+ * @private
1253
+ */
1254
+ dequeue() {
1255
+ for (; !this._deflating && this._queue.length; ) {
1256
+ const e = this._queue.shift();
1257
+ this._bufferedBytes -= e[3][x], Reflect.apply(e[0], this, e.slice(1));
1258
+ }
1259
+ }
1260
+ /**
1261
+ * Enqueues a send operation.
1262
+ *
1263
+ * @param {Array} params Send operation parameters.
1264
+ * @private
1265
+ */
1266
+ enqueue(e) {
1267
+ this._bufferedBytes += e[3][x], this._queue.push(e);
1268
+ }
1269
+ /**
1270
+ * Sends a frame.
1271
+ *
1272
+ * @param {Buffer[]} list The frame to send
1273
+ * @param {Function} [cb] Callback
1274
+ * @private
1275
+ */
1276
+ sendFrame(e, t) {
1277
+ e.length === 2 ? (this._socket.cork(), this._socket.write(e[0]), this._socket.write(e[1], t), this._socket.uncork()) : this._socket.write(e[0], t);
1278
+ }
1279
+ };
1280
+ var it = Jt;
1281
+ const Ks = /* @__PURE__ */ z(it), { kForOnEventAttribute: F, kListener: pe } = U, We = Symbol("kCode"), Ae = Symbol("kData"), Fe = Symbol("kError"), je = Symbol("kMessage"), Ge = Symbol("kReason"), I = Symbol("kTarget"), Ve = Symbol("kType"), He = Symbol("kWasClean");
1282
+ class B {
1283
+ /**
1284
+ * Create a new `Event`.
1285
+ *
1286
+ * @param {String} type The name of the event
1287
+ * @throws {TypeError} If the `type` argument is not specified
1288
+ */
1289
+ constructor(e) {
1290
+ this[I] = null, this[Ve] = e;
1291
+ }
1292
+ /**
1293
+ * @type {*}
1294
+ */
1295
+ get target() {
1296
+ return this[I];
1297
+ }
1298
+ /**
1299
+ * @type {String}
1300
+ */
1301
+ get type() {
1302
+ return this[Ve];
1303
+ }
1304
+ }
1305
+ Object.defineProperty(B.prototype, "target", { enumerable: !0 });
1306
+ Object.defineProperty(B.prototype, "type", { enumerable: !0 });
1307
+ class Y extends B {
1308
+ /**
1309
+ * Create a new `CloseEvent`.
1310
+ *
1311
+ * @param {String} type The name of the event
1312
+ * @param {Object} [options] A dictionary object that allows for setting
1313
+ * attributes via object members of the same name
1314
+ * @param {Number} [options.code=0] The status code explaining why the
1315
+ * connection was closed
1316
+ * @param {String} [options.reason=''] A human-readable string explaining why
1317
+ * the connection was closed
1318
+ * @param {Boolean} [options.wasClean=false] Indicates whether or not the
1319
+ * connection was cleanly closed
1320
+ */
1321
+ constructor(e, t = {}) {
1322
+ super(e), this[We] = t.code === void 0 ? 0 : t.code, this[Ge] = t.reason === void 0 ? "" : t.reason, this[He] = t.wasClean === void 0 ? !1 : t.wasClean;
1323
+ }
1324
+ /**
1325
+ * @type {Number}
1326
+ */
1327
+ get code() {
1328
+ return this[We];
1329
+ }
1330
+ /**
1331
+ * @type {String}
1332
+ */
1333
+ get reason() {
1334
+ return this[Ge];
1335
+ }
1336
+ /**
1337
+ * @type {Boolean}
1338
+ */
1339
+ get wasClean() {
1340
+ return this[He];
1341
+ }
1342
+ }
1343
+ Object.defineProperty(Y.prototype, "code", { enumerable: !0 });
1344
+ Object.defineProperty(Y.prototype, "reason", { enumerable: !0 });
1345
+ Object.defineProperty(Y.prototype, "wasClean", { enumerable: !0 });
1346
+ class le extends B {
1347
+ /**
1348
+ * Create a new `ErrorEvent`.
1349
+ *
1350
+ * @param {String} type The name of the event
1351
+ * @param {Object} [options] A dictionary object that allows for setting
1352
+ * attributes via object members of the same name
1353
+ * @param {*} [options.error=null] The error that generated this event
1354
+ * @param {String} [options.message=''] The error message
1355
+ */
1356
+ constructor(e, t = {}) {
1357
+ super(e), this[Fe] = t.error === void 0 ? null : t.error, this[je] = t.message === void 0 ? "" : t.message;
1358
+ }
1359
+ /**
1360
+ * @type {*}
1361
+ */
1362
+ get error() {
1363
+ return this[Fe];
1364
+ }
1365
+ /**
1366
+ * @type {String}
1367
+ */
1368
+ get message() {
1369
+ return this[je];
1370
+ }
1371
+ }
1372
+ Object.defineProperty(le.prototype, "error", { enumerable: !0 });
1373
+ Object.defineProperty(le.prototype, "message", { enumerable: !0 });
1374
+ class xe extends B {
1375
+ /**
1376
+ * Create a new `MessageEvent`.
1377
+ *
1378
+ * @param {String} type The name of the event
1379
+ * @param {Object} [options] A dictionary object that allows for setting
1380
+ * attributes via object members of the same name
1381
+ * @param {*} [options.data=null] The message content
1382
+ */
1383
+ constructor(e, t = {}) {
1384
+ super(e), this[Ae] = t.data === void 0 ? null : t.data;
1385
+ }
1386
+ /**
1387
+ * @type {*}
1388
+ */
1389
+ get data() {
1390
+ return this[Ae];
1391
+ }
1392
+ }
1393
+ Object.defineProperty(xe.prototype, "data", { enumerable: !0 });
1394
+ const es = {
1395
+ /**
1396
+ * Register an event listener.
1397
+ *
1398
+ * @param {String} type A string representing the event type to listen for
1399
+ * @param {(Function|Object)} handler The listener to add
1400
+ * @param {Object} [options] An options object specifies characteristics about
1401
+ * the event listener
1402
+ * @param {Boolean} [options.once=false] A `Boolean` indicating that the
1403
+ * listener should be invoked at most once after being added. If `true`,
1404
+ * the listener would be automatically removed when invoked.
1405
+ * @public
1406
+ */
1407
+ addEventListener(s, e, t = {}) {
1408
+ for (const i of this.listeners(s))
1409
+ if (!t[F] && i[pe] === e && !i[F])
1410
+ return;
1411
+ let r;
1412
+ if (s === "message")
1413
+ r = function(n, o) {
1414
+ const l = new xe("message", {
1415
+ data: o ? n : n.toString()
1416
+ });
1417
+ l[I] = this, Z(e, this, l);
1418
+ };
1419
+ else if (s === "close")
1420
+ r = function(n, o) {
1421
+ const l = new Y("close", {
1422
+ code: n,
1423
+ reason: o.toString(),
1424
+ wasClean: this._closeFrameReceived && this._closeFrameSent
1425
+ });
1426
+ l[I] = this, Z(e, this, l);
1427
+ };
1428
+ else if (s === "error")
1429
+ r = function(n) {
1430
+ const o = new le("error", {
1431
+ error: n,
1432
+ message: n.message
1433
+ });
1434
+ o[I] = this, Z(e, this, o);
1435
+ };
1436
+ else if (s === "open")
1437
+ r = function() {
1438
+ const n = new B("open");
1439
+ n[I] = this, Z(e, this, n);
1440
+ };
1441
+ else
1442
+ return;
1443
+ r[F] = !!t[F], r[pe] = e, t.once ? this.once(s, r) : this.on(s, r);
1444
+ },
1445
+ /**
1446
+ * Remove an event listener.
1447
+ *
1448
+ * @param {String} type A string representing the event type to remove
1449
+ * @param {(Function|Object)} handler The listener to remove
1450
+ * @public
1451
+ */
1452
+ removeEventListener(s, e) {
1453
+ for (const t of this.listeners(s))
1454
+ if (t[pe] === e && !t[F]) {
1455
+ this.removeListener(s, t);
1456
+ break;
1457
+ }
1458
+ }
1459
+ };
1460
+ var ts = {
1461
+ CloseEvent: Y,
1462
+ ErrorEvent: le,
1463
+ Event: B,
1464
+ EventTarget: es,
1465
+ MessageEvent: xe
1466
+ };
1467
+ function Z(s, e, t) {
1468
+ typeof s == "object" && s.handleEvent ? s.handleEvent.call(s, t) : s.call(e, t);
1469
+ }
1470
+ const { tokenChars: j } = ae;
1471
+ function k(s, e, t) {
1472
+ s[e] === void 0 ? s[e] = [t] : s[e].push(t);
1473
+ }
1474
+ function ss(s) {
1475
+ const e = /* @__PURE__ */ Object.create(null);
1476
+ let t = /* @__PURE__ */ Object.create(null), r = !1, i = !1, n = !1, o, l, f = -1, a = -1, c = -1, h = 0;
1477
+ for (; h < s.length; h++)
1478
+ if (a = s.charCodeAt(h), o === void 0)
1479
+ if (c === -1 && j[a] === 1)
1480
+ f === -1 && (f = h);
1481
+ else if (h !== 0 && (a === 32 || a === 9))
1482
+ c === -1 && f !== -1 && (c = h);
1483
+ else if (a === 59 || a === 44) {
1484
+ if (f === -1)
1485
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1486
+ c === -1 && (c = h);
1487
+ const v = s.slice(f, c);
1488
+ a === 44 ? (k(e, v, t), t = /* @__PURE__ */ Object.create(null)) : o = v, f = c = -1;
1489
+ } else
1490
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1491
+ else if (l === void 0)
1492
+ if (c === -1 && j[a] === 1)
1493
+ f === -1 && (f = h);
1494
+ else if (a === 32 || a === 9)
1495
+ c === -1 && f !== -1 && (c = h);
1496
+ else if (a === 59 || a === 44) {
1497
+ if (f === -1)
1498
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1499
+ c === -1 && (c = h), k(t, s.slice(f, c), !0), a === 44 && (k(e, o, t), t = /* @__PURE__ */ Object.create(null), o = void 0), f = c = -1;
1500
+ } else if (a === 61 && f !== -1 && c === -1)
1501
+ l = s.slice(f, h), f = c = -1;
1502
+ else
1503
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1504
+ else if (i) {
1505
+ if (j[a] !== 1)
1506
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1507
+ f === -1 ? f = h : r || (r = !0), i = !1;
1508
+ } else if (n)
1509
+ if (j[a] === 1)
1510
+ f === -1 && (f = h);
1511
+ else if (a === 34 && f !== -1)
1512
+ n = !1, c = h;
1513
+ else if (a === 92)
1514
+ i = !0;
1515
+ else
1516
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1517
+ else if (a === 34 && s.charCodeAt(h - 1) === 61)
1518
+ n = !0;
1519
+ else if (c === -1 && j[a] === 1)
1520
+ f === -1 && (f = h);
1521
+ else if (f !== -1 && (a === 32 || a === 9))
1522
+ c === -1 && (c = h);
1523
+ else if (a === 59 || a === 44) {
1524
+ if (f === -1)
1525
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1526
+ c === -1 && (c = h);
1527
+ let v = s.slice(f, c);
1528
+ r && (v = v.replace(/\\/g, ""), r = !1), k(t, l, v), a === 44 && (k(e, o, t), t = /* @__PURE__ */ Object.create(null), o = void 0), l = void 0, f = c = -1;
1529
+ } else
1530
+ throw new SyntaxError(`Unexpected character at index ${h}`);
1531
+ if (f === -1 || n || a === 32 || a === 9)
1532
+ throw new SyntaxError("Unexpected end of input");
1533
+ c === -1 && (c = h);
1534
+ const p = s.slice(f, c);
1535
+ return o === void 0 ? k(e, p, t) : (l === void 0 ? k(t, p, !0) : r ? k(t, l, p.replace(/\\/g, "")) : k(t, l, p), k(e, o, t)), e;
1536
+ }
1537
+ function rs(s) {
1538
+ return Object.keys(s).map((e) => {
1539
+ let t = s[e];
1540
+ return Array.isArray(t) || (t = [t]), t.map((r) => [e].concat(
1541
+ Object.keys(r).map((i) => {
1542
+ let n = r[i];
1543
+ return Array.isArray(n) || (n = [n]), n.map((o) => o === !0 ? i : `${i}=${o}`).join("; ");
1544
+ })
1545
+ ).join("; ")).join(", ");
1546
+ }).join(", ");
1547
+ }
1548
+ var nt = { format: rs, parse: ss };
1549
+ const is = S, ns = S, os = S, ot = S, as = S, { randomBytes: ls, createHash: fs } = S, { URL: me } = S, T = oe, hs = rt, cs = it, {
1550
+ BINARY_TYPES: ze,
1551
+ EMPTY_BUFFER: Q,
1552
+ GUID: us,
1553
+ kForOnEventAttribute: ge,
1554
+ kListener: ds,
1555
+ kStatusCode: _s,
1556
+ kWebSocket: y,
1557
+ NOOP: at
1558
+ } = U, {
1559
+ EventTarget: { addEventListener: ps, removeEventListener: ms }
1560
+ } = ts, { format: gs, parse: ys } = nt, { toBuffer: vs } = ne, Ss = 30 * 1e3, lt = Symbol("kAborted"), ye = [8, 13], O = ["CONNECTING", "OPEN", "CLOSING", "CLOSED"], Es = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
1561
+ let m = class d extends is {
1562
+ /**
1563
+ * Create a new `WebSocket`.
1564
+ *
1565
+ * @param {(String|URL)} address The URL to which to connect
1566
+ * @param {(String|String[])} [protocols] The subprotocols
1567
+ * @param {Object} [options] Connection options
1568
+ */
1569
+ constructor(e, t, r) {
1570
+ super(), this._binaryType = ze[0], this._closeCode = 1006, this._closeFrameReceived = !1, this._closeFrameSent = !1, this._closeMessage = Q, this._closeTimer = null, this._extensions = {}, this._paused = !1, this._protocol = "", this._readyState = d.CONNECTING, this._receiver = null, this._sender = null, this._socket = null, e !== null ? (this._bufferedAmount = 0, this._isServer = !1, this._redirects = 0, t === void 0 ? t = [] : Array.isArray(t) || (typeof t == "object" && t !== null ? (r = t, t = []) : t = [t]), ht(this, e, t, r)) : this._isServer = !0;
1571
+ }
1572
+ /**
1573
+ * This deviates from the WHATWG interface since ws doesn't support the
1574
+ * required default "blob" type (instead we define a custom "nodebuffer"
1575
+ * type).
1576
+ *
1577
+ * @type {String}
1578
+ */
1579
+ get binaryType() {
1580
+ return this._binaryType;
1581
+ }
1582
+ set binaryType(e) {
1583
+ ze.includes(e) && (this._binaryType = e, this._receiver && (this._receiver._binaryType = e));
1584
+ }
1585
+ /**
1586
+ * @type {Number}
1587
+ */
1588
+ get bufferedAmount() {
1589
+ return this._socket ? this._socket._writableState.length + this._sender._bufferedBytes : this._bufferedAmount;
1590
+ }
1591
+ /**
1592
+ * @type {String}
1593
+ */
1594
+ get extensions() {
1595
+ return Object.keys(this._extensions).join();
1596
+ }
1597
+ /**
1598
+ * @type {Boolean}
1599
+ */
1600
+ get isPaused() {
1601
+ return this._paused;
1602
+ }
1603
+ /**
1604
+ * @type {Function}
1605
+ */
1606
+ /* istanbul ignore next */
1607
+ get onclose() {
1608
+ return null;
1609
+ }
1610
+ /**
1611
+ * @type {Function}
1612
+ */
1613
+ /* istanbul ignore next */
1614
+ get onerror() {
1615
+ return null;
1616
+ }
1617
+ /**
1618
+ * @type {Function}
1619
+ */
1620
+ /* istanbul ignore next */
1621
+ get onopen() {
1622
+ return null;
1623
+ }
1624
+ /**
1625
+ * @type {Function}
1626
+ */
1627
+ /* istanbul ignore next */
1628
+ get onmessage() {
1629
+ return null;
1630
+ }
1631
+ /**
1632
+ * @type {String}
1633
+ */
1634
+ get protocol() {
1635
+ return this._protocol;
1636
+ }
1637
+ /**
1638
+ * @type {Number}
1639
+ */
1640
+ get readyState() {
1641
+ return this._readyState;
1642
+ }
1643
+ /**
1644
+ * @type {String}
1645
+ */
1646
+ get url() {
1647
+ return this._url;
1648
+ }
1649
+ /**
1650
+ * Set up the socket and the internal resources.
1651
+ *
1652
+ * @param {(net.Socket|tls.Socket)} socket The network socket between the
1653
+ * server and client
1654
+ * @param {Buffer} head The first packet of the upgraded stream
1655
+ * @param {Object} options Options object
1656
+ * @param {Function} [options.generateMask] The function used to generate the
1657
+ * masking key
1658
+ * @param {Number} [options.maxPayload=0] The maximum allowed message size
1659
+ * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
1660
+ * not to skip UTF-8 validation for text and close messages
1661
+ * @private
1662
+ */
1663
+ setSocket(e, t, r) {
1664
+ const i = new hs({
1665
+ binaryType: this.binaryType,
1666
+ extensions: this._extensions,
1667
+ isServer: this._isServer,
1668
+ maxPayload: r.maxPayload,
1669
+ skipUTF8Validation: r.skipUTF8Validation
1670
+ });
1671
+ this._sender = new cs(e, this._extensions, r.generateMask), this._receiver = i, this._socket = e, i[y] = this, e[y] = this, i.on("conclude", ks), i.on("drain", ws), i.on("error", Os), i.on("message", Cs), i.on("ping", Ts), i.on("pong", Ls), e.setTimeout(0), e.setNoDelay(), t.length > 0 && e.unshift(t), e.on("close", ut), e.on("data", fe), e.on("end", dt), e.on("error", _t), this._readyState = d.OPEN, this.emit("open");
1672
+ }
1673
+ /**
1674
+ * Emit the `'close'` event.
1675
+ *
1676
+ * @private
1677
+ */
1678
+ emitClose() {
1679
+ if (!this._socket) {
1680
+ this._readyState = d.CLOSED, this.emit("close", this._closeCode, this._closeMessage);
1681
+ return;
1682
+ }
1683
+ this._extensions[T.extensionName] && this._extensions[T.extensionName].cleanup(), this._receiver.removeAllListeners(), this._readyState = d.CLOSED, this.emit("close", this._closeCode, this._closeMessage);
1684
+ }
1685
+ /**
1686
+ * Start a closing handshake.
1687
+ *
1688
+ * +----------+ +-----------+ +----------+
1689
+ * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
1690
+ * | +----------+ +-----------+ +----------+ |
1691
+ * +----------+ +-----------+ |
1692
+ * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
1693
+ * +----------+ +-----------+ |
1694
+ * | | | +---+ |
1695
+ * +------------------------+-->|fin| - - - -
1696
+ * | +---+ | +---+
1697
+ * - - - - -|fin|<---------------------+
1698
+ * +---+
1699
+ *
1700
+ * @param {Number} [code] Status code explaining why the connection is closing
1701
+ * @param {(String|Buffer)} [data] The reason why the connection is
1702
+ * closing
1703
+ * @public
1704
+ */
1705
+ close(e, t) {
1706
+ if (this.readyState !== d.CLOSED) {
1707
+ if (this.readyState === d.CONNECTING) {
1708
+ b(this, this._req, "WebSocket was closed before the connection was established");
1709
+ return;
1710
+ }
1711
+ if (this.readyState === d.CLOSING) {
1712
+ this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted) && this._socket.end();
1713
+ return;
1714
+ }
1715
+ this._readyState = d.CLOSING, this._sender.close(e, t, !this._isServer, (r) => {
1716
+ r || (this._closeFrameSent = !0, (this._closeFrameReceived || this._receiver._writableState.errorEmitted) && this._socket.end());
1717
+ }), this._closeTimer = setTimeout(
1718
+ this._socket.destroy.bind(this._socket),
1719
+ Ss
1720
+ );
1721
+ }
1722
+ }
1723
+ /**
1724
+ * Pause the socket.
1725
+ *
1726
+ * @public
1727
+ */
1728
+ pause() {
1729
+ this.readyState === d.CONNECTING || this.readyState === d.CLOSED || (this._paused = !0, this._socket.pause());
1730
+ }
1731
+ /**
1732
+ * Send a ping.
1733
+ *
1734
+ * @param {*} [data] The data to send
1735
+ * @param {Boolean} [mask] Indicates whether or not to mask `data`
1736
+ * @param {Function} [cb] Callback which is executed when the ping is sent
1737
+ * @public
1738
+ */
1739
+ ping(e, t, r) {
1740
+ if (this.readyState === d.CONNECTING)
1741
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
1742
+ if (typeof e == "function" ? (r = e, e = t = void 0) : typeof t == "function" && (r = t, t = void 0), typeof e == "number" && (e = e.toString()), this.readyState !== d.OPEN) {
1743
+ ve(this, e, r);
1744
+ return;
1745
+ }
1746
+ t === void 0 && (t = !this._isServer), this._sender.ping(e || Q, t, r);
1747
+ }
1748
+ /**
1749
+ * Send a pong.
1750
+ *
1751
+ * @param {*} [data] The data to send
1752
+ * @param {Boolean} [mask] Indicates whether or not to mask `data`
1753
+ * @param {Function} [cb] Callback which is executed when the pong is sent
1754
+ * @public
1755
+ */
1756
+ pong(e, t, r) {
1757
+ if (this.readyState === d.CONNECTING)
1758
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
1759
+ if (typeof e == "function" ? (r = e, e = t = void 0) : typeof t == "function" && (r = t, t = void 0), typeof e == "number" && (e = e.toString()), this.readyState !== d.OPEN) {
1760
+ ve(this, e, r);
1761
+ return;
1762
+ }
1763
+ t === void 0 && (t = !this._isServer), this._sender.pong(e || Q, t, r);
1764
+ }
1765
+ /**
1766
+ * Resume the socket.
1767
+ *
1768
+ * @public
1769
+ */
1770
+ resume() {
1771
+ this.readyState === d.CONNECTING || this.readyState === d.CLOSED || (this._paused = !1, this._receiver._writableState.needDrain || this._socket.resume());
1772
+ }
1773
+ /**
1774
+ * Send a data message.
1775
+ *
1776
+ * @param {*} data The message to send
1777
+ * @param {Object} [options] Options object
1778
+ * @param {Boolean} [options.binary] Specifies whether `data` is binary or
1779
+ * text
1780
+ * @param {Boolean} [options.compress] Specifies whether or not to compress
1781
+ * `data`
1782
+ * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
1783
+ * last one
1784
+ * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
1785
+ * @param {Function} [cb] Callback which is executed when data is written out
1786
+ * @public
1787
+ */
1788
+ send(e, t, r) {
1789
+ if (this.readyState === d.CONNECTING)
1790
+ throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
1791
+ if (typeof t == "function" && (r = t, t = {}), typeof e == "number" && (e = e.toString()), this.readyState !== d.OPEN) {
1792
+ ve(this, e, r);
1793
+ return;
1794
+ }
1795
+ const i = {
1796
+ binary: typeof e != "string",
1797
+ mask: !this._isServer,
1798
+ compress: !0,
1799
+ fin: !0,
1800
+ ...t
1801
+ };
1802
+ this._extensions[T.extensionName] || (i.compress = !1), this._sender.send(e || Q, i, r);
1803
+ }
1804
+ /**
1805
+ * Forcibly close the connection.
1806
+ *
1807
+ * @public
1808
+ */
1809
+ terminate() {
1810
+ if (this.readyState !== d.CLOSED) {
1811
+ if (this.readyState === d.CONNECTING) {
1812
+ b(this, this._req, "WebSocket was closed before the connection was established");
1813
+ return;
1814
+ }
1815
+ this._socket && (this._readyState = d.CLOSING, this._socket.destroy());
1816
+ }
1817
+ }
1818
+ };
1819
+ Object.defineProperty(m, "CONNECTING", {
1820
+ enumerable: !0,
1821
+ value: O.indexOf("CONNECTING")
1822
+ });
1823
+ Object.defineProperty(m.prototype, "CONNECTING", {
1824
+ enumerable: !0,
1825
+ value: O.indexOf("CONNECTING")
1826
+ });
1827
+ Object.defineProperty(m, "OPEN", {
1828
+ enumerable: !0,
1829
+ value: O.indexOf("OPEN")
1830
+ });
1831
+ Object.defineProperty(m.prototype, "OPEN", {
1832
+ enumerable: !0,
1833
+ value: O.indexOf("OPEN")
1834
+ });
1835
+ Object.defineProperty(m, "CLOSING", {
1836
+ enumerable: !0,
1837
+ value: O.indexOf("CLOSING")
1838
+ });
1839
+ Object.defineProperty(m.prototype, "CLOSING", {
1840
+ enumerable: !0,
1841
+ value: O.indexOf("CLOSING")
1842
+ });
1843
+ Object.defineProperty(m, "CLOSED", {
1844
+ enumerable: !0,
1845
+ value: O.indexOf("CLOSED")
1846
+ });
1847
+ Object.defineProperty(m.prototype, "CLOSED", {
1848
+ enumerable: !0,
1849
+ value: O.indexOf("CLOSED")
1850
+ });
1851
+ [
1852
+ "binaryType",
1853
+ "bufferedAmount",
1854
+ "extensions",
1855
+ "isPaused",
1856
+ "protocol",
1857
+ "readyState",
1858
+ "url"
1859
+ ].forEach((s) => {
1860
+ Object.defineProperty(m.prototype, s, { enumerable: !0 });
1861
+ });
1862
+ ["open", "error", "close", "message"].forEach((s) => {
1863
+ Object.defineProperty(m.prototype, `on${s}`, {
1864
+ enumerable: !0,
1865
+ get() {
1866
+ for (const e of this.listeners(s))
1867
+ if (e[ge])
1868
+ return e[ds];
1869
+ return null;
1870
+ },
1871
+ set(e) {
1872
+ for (const t of this.listeners(s))
1873
+ if (t[ge]) {
1874
+ this.removeListener(s, t);
1875
+ break;
1876
+ }
1877
+ typeof e == "function" && this.addEventListener(s, e, {
1878
+ [ge]: !0
1879
+ });
1880
+ }
1881
+ });
1882
+ });
1883
+ m.prototype.addEventListener = ps;
1884
+ m.prototype.removeEventListener = ms;
1885
+ var ft = m;
1886
+ function ht(s, e, t, r) {
1887
+ const i = {
1888
+ protocolVersion: ye[1],
1889
+ maxPayload: 104857600,
1890
+ skipUTF8Validation: !1,
1891
+ perMessageDeflate: !0,
1892
+ followRedirects: !1,
1893
+ maxRedirects: 10,
1894
+ ...r,
1895
+ createConnection: void 0,
1896
+ socketPath: void 0,
1897
+ hostname: void 0,
1898
+ protocol: void 0,
1899
+ timeout: void 0,
1900
+ method: "GET",
1901
+ host: void 0,
1902
+ path: void 0,
1903
+ port: void 0
1904
+ };
1905
+ if (!ye.includes(i.protocolVersion))
1906
+ throw new RangeError(
1907
+ `Unsupported protocol version: ${i.protocolVersion} (supported versions: ${ye.join(", ")})`
1908
+ );
1909
+ let n;
1910
+ if (e instanceof me)
1911
+ n = e, s._url = e.href;
1912
+ else {
1913
+ try {
1914
+ n = new me(e);
1915
+ } catch {
1916
+ throw new SyntaxError(`Invalid URL: ${e}`);
1917
+ }
1918
+ s._url = e;
1919
+ }
1920
+ const o = n.protocol === "wss:", l = n.protocol === "ws+unix:";
1921
+ let f;
1922
+ if (n.protocol !== "ws:" && !o && !l ? f = `The URL's protocol must be one of "ws:", "wss:", or "ws+unix:"` : l && !n.pathname ? f = "The URL's pathname is empty" : n.hash && (f = "The URL contains a fragment identifier"), f) {
1923
+ const u = new SyntaxError(f);
1924
+ if (s._redirects === 0)
1925
+ throw u;
1926
+ ee(s, u);
1927
+ return;
1928
+ }
1929
+ const a = o ? 443 : 80, c = ls(16).toString("base64"), h = o ? ns.request : os.request, p = /* @__PURE__ */ new Set();
1930
+ let v;
1931
+ if (i.createConnection = o ? xs : bs, i.defaultPort = i.defaultPort || a, i.port = n.port || a, i.host = n.hostname.startsWith("[") ? n.hostname.slice(1, -1) : n.hostname, i.headers = {
1932
+ ...i.headers,
1933
+ "Sec-WebSocket-Version": i.protocolVersion,
1934
+ "Sec-WebSocket-Key": c,
1935
+ Connection: "Upgrade",
1936
+ Upgrade: "websocket"
1937
+ }, i.path = n.pathname + n.search, i.timeout = i.handshakeTimeout, i.perMessageDeflate && (v = new T(
1938
+ i.perMessageDeflate !== !0 ? i.perMessageDeflate : {},
1939
+ !1,
1940
+ i.maxPayload
1941
+ ), i.headers["Sec-WebSocket-Extensions"] = gs({
1942
+ [T.extensionName]: v.offer()
1943
+ })), t.length) {
1944
+ for (const u of t) {
1945
+ if (typeof u != "string" || !Es.test(u) || p.has(u))
1946
+ throw new SyntaxError(
1947
+ "An invalid or duplicated subprotocol was specified"
1948
+ );
1949
+ p.add(u);
1950
+ }
1951
+ i.headers["Sec-WebSocket-Protocol"] = t.join(",");
1952
+ }
1953
+ if (i.origin && (i.protocolVersion < 13 ? i.headers["Sec-WebSocket-Origin"] = i.origin : i.headers.Origin = i.origin), (n.username || n.password) && (i.auth = `${n.username}:${n.password}`), l) {
1954
+ const u = i.path.split(":");
1955
+ i.socketPath = u[0], i.path = u[1];
1956
+ }
1957
+ let _;
1958
+ if (i.followRedirects) {
1959
+ if (s._redirects === 0) {
1960
+ s._originalIpc = l, s._originalSecure = o, s._originalHostOrSocketPath = l ? i.socketPath : n.host;
1961
+ const u = r && r.headers;
1962
+ if (r = { ...r, headers: {} }, u)
1963
+ for (const [E, $] of Object.entries(u))
1964
+ r.headers[E.toLowerCase()] = $;
1965
+ } else if (s.listenerCount("redirect") === 0) {
1966
+ const u = l ? s._originalIpc ? i.socketPath === s._originalHostOrSocketPath : !1 : s._originalIpc ? !1 : n.host === s._originalHostOrSocketPath;
1967
+ (!u || s._originalSecure && !o) && (delete i.headers.authorization, delete i.headers.cookie, u || delete i.headers.host, i.auth = void 0);
1968
+ }
1969
+ i.auth && !r.headers.authorization && (r.headers.authorization = "Basic " + Buffer.from(i.auth).toString("base64")), _ = s._req = h(i), s._redirects && s.emit("redirect", s.url, _);
1970
+ } else
1971
+ _ = s._req = h(i);
1972
+ i.timeout && _.on("timeout", () => {
1973
+ b(s, _, "Opening handshake has timed out");
1974
+ }), _.on("error", (u) => {
1975
+ _ === null || _[lt] || (_ = s._req = null, ee(s, u));
1976
+ }), _.on("response", (u) => {
1977
+ const E = u.headers.location, $ = u.statusCode;
1978
+ if (E && i.followRedirects && $ >= 300 && $ < 400) {
1979
+ if (++s._redirects > i.maxRedirects) {
1980
+ b(s, _, "Maximum redirects exceeded");
1981
+ return;
1982
+ }
1983
+ _.abort();
1984
+ let q;
1985
+ try {
1986
+ q = new me(E, e);
1987
+ } catch {
1988
+ const L = new SyntaxError(`Invalid URL: ${E}`);
1989
+ ee(s, L);
1990
+ return;
1991
+ }
1992
+ ht(s, q, t, r);
1993
+ } else
1994
+ s.emit("unexpected-response", _, u) || b(
1995
+ s,
1996
+ _,
1997
+ `Unexpected server response: ${u.statusCode}`
1998
+ );
1999
+ }), _.on("upgrade", (u, E, $) => {
2000
+ if (s.emit("upgrade", u), s.readyState !== m.CONNECTING)
2001
+ return;
2002
+ if (_ = s._req = null, u.headers.upgrade.toLowerCase() !== "websocket") {
2003
+ b(s, E, "Invalid Upgrade header");
2004
+ return;
2005
+ }
2006
+ const q = fs("sha1").update(c + us).digest("base64");
2007
+ if (u.headers["sec-websocket-accept"] !== q) {
2008
+ b(s, E, "Invalid Sec-WebSocket-Accept header");
2009
+ return;
2010
+ }
2011
+ const D = u.headers["sec-websocket-protocol"];
2012
+ let L;
2013
+ if (D !== void 0 ? p.size ? p.has(D) || (L = "Server sent an invalid subprotocol") : L = "Server sent a subprotocol but none was requested" : p.size && (L = "Server sent no subprotocol"), L) {
2014
+ b(s, E, L);
2015
+ return;
2016
+ }
2017
+ D && (s._protocol = D);
2018
+ const ke = u.headers["sec-websocket-extensions"];
2019
+ if (ke !== void 0) {
2020
+ if (!v) {
2021
+ b(s, E, "Server sent a Sec-WebSocket-Extensions header but no extension was requested");
2022
+ return;
2023
+ }
2024
+ let he;
2025
+ try {
2026
+ he = ys(ke);
2027
+ } catch {
2028
+ b(s, E, "Invalid Sec-WebSocket-Extensions header");
2029
+ return;
2030
+ }
2031
+ const we = Object.keys(he);
2032
+ if (we.length !== 1 || we[0] !== T.extensionName) {
2033
+ b(s, E, "Server indicated an extension that was not requested");
2034
+ return;
2035
+ }
2036
+ try {
2037
+ v.accept(he[T.extensionName]);
2038
+ } catch {
2039
+ b(s, E, "Invalid Sec-WebSocket-Extensions header");
2040
+ return;
2041
+ }
2042
+ s._extensions[T.extensionName] = v;
2043
+ }
2044
+ s.setSocket(E, $, {
2045
+ generateMask: i.generateMask,
2046
+ maxPayload: i.maxPayload,
2047
+ skipUTF8Validation: i.skipUTF8Validation
2048
+ });
2049
+ }), i.finishRequest ? i.finishRequest(_, s) : _.end();
2050
+ }
2051
+ function ee(s, e) {
2052
+ s._readyState = m.CLOSING, s.emit("error", e), s.emitClose();
2053
+ }
2054
+ function bs(s) {
2055
+ return s.path = s.socketPath, ot.connect(s);
2056
+ }
2057
+ function xs(s) {
2058
+ return s.path = void 0, !s.servername && s.servername !== "" && (s.servername = ot.isIP(s.host) ? "" : s.host), as.connect(s);
2059
+ }
2060
+ function b(s, e, t) {
2061
+ s._readyState = m.CLOSING;
2062
+ const r = new Error(t);
2063
+ Error.captureStackTrace(r, b), e.setHeader ? (e[lt] = !0, e.abort(), e.socket && !e.socket.destroyed && e.socket.destroy(), process.nextTick(ee, s, r)) : (e.destroy(r), e.once("error", s.emit.bind(s, "error")), e.once("close", s.emitClose.bind(s)));
2064
+ }
2065
+ function ve(s, e, t) {
2066
+ if (e) {
2067
+ const r = vs(e).length;
2068
+ s._socket ? s._sender._bufferedBytes += r : s._bufferedAmount += r;
2069
+ }
2070
+ if (t) {
2071
+ const r = new Error(
2072
+ `WebSocket is not open: readyState ${s.readyState} (${O[s.readyState]})`
2073
+ );
2074
+ process.nextTick(t, r);
2075
+ }
2076
+ }
2077
+ function ks(s, e) {
2078
+ const t = this[y];
2079
+ t._closeFrameReceived = !0, t._closeMessage = e, t._closeCode = s, t._socket[y] !== void 0 && (t._socket.removeListener("data", fe), process.nextTick(ct, t._socket), s === 1005 ? t.close() : t.close(s, e));
2080
+ }
2081
+ function ws() {
2082
+ const s = this[y];
2083
+ s.isPaused || s._socket.resume();
2084
+ }
2085
+ function Os(s) {
2086
+ const e = this[y];
2087
+ e._socket[y] !== void 0 && (e._socket.removeListener("data", fe), process.nextTick(ct, e._socket), e.close(s[_s])), e.emit("error", s);
2088
+ }
2089
+ function Ye() {
2090
+ this[y].emitClose();
2091
+ }
2092
+ function Cs(s, e) {
2093
+ this[y].emit("message", s, e);
2094
+ }
2095
+ function Ts(s) {
2096
+ const e = this[y];
2097
+ e.pong(s, !e._isServer, at), e.emit("ping", s);
2098
+ }
2099
+ function Ls(s) {
2100
+ this[y].emit("pong", s);
2101
+ }
2102
+ function ct(s) {
2103
+ s.resume();
2104
+ }
2105
+ function ut() {
2106
+ const s = this[y];
2107
+ this.removeListener("close", ut), this.removeListener("data", fe), this.removeListener("end", dt), s._readyState = m.CLOSING;
2108
+ let e;
2109
+ !this._readableState.endEmitted && !s._closeFrameReceived && !s._receiver._writableState.errorEmitted && (e = s._socket.read()) !== null && s._receiver.write(e), s._receiver.end(), this[y] = void 0, clearTimeout(s._closeTimer), s._receiver._writableState.finished || s._receiver._writableState.errorEmitted ? s.emitClose() : (s._receiver.on("error", Ye), s._receiver.on("finish", Ye));
2110
+ }
2111
+ function fe(s) {
2112
+ this[y]._receiver.write(s) || this.pause();
2113
+ }
2114
+ function dt() {
2115
+ const s = this[y];
2116
+ s._readyState = m.CLOSING, s._receiver.end(), this.end();
2117
+ }
2118
+ function _t() {
2119
+ const s = this[y];
2120
+ this.removeListener("error", _t), this.on("error", at), s && (s._readyState = m.CLOSING, this.destroy());
2121
+ }
2122
+ const Xs = /* @__PURE__ */ z(ft), { tokenChars: Ns } = ae;
2123
+ function Ps(s) {
2124
+ const e = /* @__PURE__ */ new Set();
2125
+ let t = -1, r = -1, i = 0;
2126
+ for (i; i < s.length; i++) {
2127
+ const o = s.charCodeAt(i);
2128
+ if (r === -1 && Ns[o] === 1)
2129
+ t === -1 && (t = i);
2130
+ else if (i !== 0 && (o === 32 || o === 9))
2131
+ r === -1 && t !== -1 && (r = i);
2132
+ else if (o === 44) {
2133
+ if (t === -1)
2134
+ throw new SyntaxError(`Unexpected character at index ${i}`);
2135
+ r === -1 && (r = i);
2136
+ const l = s.slice(t, r);
2137
+ if (e.has(l))
2138
+ throw new SyntaxError(`The "${l}" subprotocol is duplicated`);
2139
+ e.add(l), t = r = -1;
2140
+ } else
2141
+ throw new SyntaxError(`Unexpected character at index ${i}`);
2142
+ }
2143
+ if (t === -1 || r !== -1)
2144
+ throw new SyntaxError("Unexpected end of input");
2145
+ const n = s.slice(t, i);
2146
+ if (e.has(n))
2147
+ throw new SyntaxError(`The "${n}" subprotocol is duplicated`);
2148
+ return e.add(n), e;
2149
+ }
2150
+ var Rs = { parse: Ps };
2151
+ const Us = S, ie = S, { createHash: Bs } = S, qe = nt, N = oe, $s = Rs, Ms = ft, { GUID: Is, kWebSocket: Ds } = U, Ws = /^[+/0-9A-Za-z]{22}==$/, Ke = 0, Xe = 1, pt = 2;
2152
+ class As extends Us {
2153
+ /**
2154
+ * Create a `WebSocketServer` instance.
2155
+ *
2156
+ * @param {Object} options Configuration options
2157
+ * @param {Number} [options.backlog=511] The maximum length of the queue of
2158
+ * pending connections
2159
+ * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
2160
+ * track clients
2161
+ * @param {Function} [options.handleProtocols] A hook to handle protocols
2162
+ * @param {String} [options.host] The hostname where to bind the server
2163
+ * @param {Number} [options.maxPayload=104857600] The maximum allowed message
2164
+ * size
2165
+ * @param {Boolean} [options.noServer=false] Enable no server mode
2166
+ * @param {String} [options.path] Accept only connections matching this path
2167
+ * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
2168
+ * permessage-deflate
2169
+ * @param {Number} [options.port] The port where to bind the server
2170
+ * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
2171
+ * server to use
2172
+ * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
2173
+ * not to skip UTF-8 validation for text and close messages
2174
+ * @param {Function} [options.verifyClient] A hook to reject connections
2175
+ * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
2176
+ * class to use. It must be the `WebSocket` class or class that extends it
2177
+ * @param {Function} [callback] A listener for the `listening` event
2178
+ */
2179
+ constructor(e, t) {
2180
+ if (super(), e = {
2181
+ maxPayload: 100 * 1024 * 1024,
2182
+ skipUTF8Validation: !1,
2183
+ perMessageDeflate: !1,
2184
+ handleProtocols: null,
2185
+ clientTracking: !0,
2186
+ verifyClient: null,
2187
+ noServer: !1,
2188
+ backlog: null,
2189
+ // use default (511 as implemented in net.js)
2190
+ server: null,
2191
+ host: null,
2192
+ path: null,
2193
+ port: null,
2194
+ WebSocket: Ms,
2195
+ ...e
2196
+ }, e.port == null && !e.server && !e.noServer || e.port != null && (e.server || e.noServer) || e.server && e.noServer)
2197
+ throw new TypeError(
2198
+ 'One and only one of the "port", "server", or "noServer" options must be specified'
2199
+ );
2200
+ if (e.port != null ? (this._server = ie.createServer((r, i) => {
2201
+ const n = ie.STATUS_CODES[426];
2202
+ i.writeHead(426, {
2203
+ "Content-Length": n.length,
2204
+ "Content-Type": "text/plain"
2205
+ }), i.end(n);
2206
+ }), this._server.listen(
2207
+ e.port,
2208
+ e.host,
2209
+ e.backlog,
2210
+ t
2211
+ )) : e.server && (this._server = e.server), this._server) {
2212
+ const r = this.emit.bind(this, "connection");
2213
+ this._removeListeners = js(this._server, {
2214
+ listening: this.emit.bind(this, "listening"),
2215
+ error: this.emit.bind(this, "error"),
2216
+ upgrade: (i, n, o) => {
2217
+ this.handleUpgrade(i, n, o, r);
2218
+ }
2219
+ });
2220
+ }
2221
+ e.perMessageDeflate === !0 && (e.perMessageDeflate = {}), e.clientTracking && (this.clients = /* @__PURE__ */ new Set(), this._shouldEmitClose = !1), this.options = e, this._state = Ke;
2222
+ }
2223
+ /**
2224
+ * Returns the bound address, the address family name, and port of the server
2225
+ * as reported by the operating system if listening on an IP socket.
2226
+ * If the server is listening on a pipe or UNIX domain socket, the name is
2227
+ * returned as a string.
2228
+ *
2229
+ * @return {(Object|String|null)} The address of the server
2230
+ * @public
2231
+ */
2232
+ address() {
2233
+ if (this.options.noServer)
2234
+ throw new Error('The server is operating in "noServer" mode');
2235
+ return this._server ? this._server.address() : null;
2236
+ }
2237
+ /**
2238
+ * Stop the server from accepting new connections and emit the `'close'` event
2239
+ * when all existing connections are closed.
2240
+ *
2241
+ * @param {Function} [cb] A one-time listener for the `'close'` event
2242
+ * @public
2243
+ */
2244
+ close(e) {
2245
+ if (this._state === pt) {
2246
+ e && this.once("close", () => {
2247
+ e(new Error("The server is not running"));
2248
+ }), process.nextTick(G, this);
2249
+ return;
2250
+ }
2251
+ if (e && this.once("close", e), this._state !== Xe)
2252
+ if (this._state = Xe, this.options.noServer || this.options.server)
2253
+ this._server && (this._removeListeners(), this._removeListeners = this._server = null), this.clients ? this.clients.size ? this._shouldEmitClose = !0 : process.nextTick(G, this) : process.nextTick(G, this);
2254
+ else {
2255
+ const t = this._server;
2256
+ this._removeListeners(), this._removeListeners = this._server = null, t.close(() => {
2257
+ G(this);
2258
+ });
2259
+ }
2260
+ }
2261
+ /**
2262
+ * See if a given request should be handled by this server instance.
2263
+ *
2264
+ * @param {http.IncomingMessage} req Request object to inspect
2265
+ * @return {Boolean} `true` if the request is valid, else `false`
2266
+ * @public
2267
+ */
2268
+ shouldHandle(e) {
2269
+ if (this.options.path) {
2270
+ const t = e.url.indexOf("?");
2271
+ if ((t !== -1 ? e.url.slice(0, t) : e.url) !== this.options.path)
2272
+ return !1;
2273
+ }
2274
+ return !0;
2275
+ }
2276
+ /**
2277
+ * Handle a HTTP Upgrade request.
2278
+ *
2279
+ * @param {http.IncomingMessage} req The request object
2280
+ * @param {(net.Socket|tls.Socket)} socket The network socket between the
2281
+ * server and client
2282
+ * @param {Buffer} head The first packet of the upgraded stream
2283
+ * @param {Function} cb Callback
2284
+ * @public
2285
+ */
2286
+ handleUpgrade(e, t, r, i) {
2287
+ t.on("error", Ze);
2288
+ const n = e.headers["sec-websocket-key"], o = +e.headers["sec-websocket-version"];
2289
+ if (e.method !== "GET") {
2290
+ R(this, e, t, 405, "Invalid HTTP method");
2291
+ return;
2292
+ }
2293
+ if (e.headers.upgrade.toLowerCase() !== "websocket") {
2294
+ R(this, e, t, 400, "Invalid Upgrade header");
2295
+ return;
2296
+ }
2297
+ if (!n || !Ws.test(n)) {
2298
+ R(this, e, t, 400, "Missing or invalid Sec-WebSocket-Key header");
2299
+ return;
2300
+ }
2301
+ if (o !== 8 && o !== 13) {
2302
+ R(this, e, t, 400, "Missing or invalid Sec-WebSocket-Version header");
2303
+ return;
2304
+ }
2305
+ if (!this.shouldHandle(e)) {
2306
+ H(t, 400);
2307
+ return;
2308
+ }
2309
+ const l = e.headers["sec-websocket-protocol"];
2310
+ let f = /* @__PURE__ */ new Set();
2311
+ if (l !== void 0)
2312
+ try {
2313
+ f = $s.parse(l);
2314
+ } catch {
2315
+ R(this, e, t, 400, "Invalid Sec-WebSocket-Protocol header");
2316
+ return;
2317
+ }
2318
+ const a = e.headers["sec-websocket-extensions"], c = {};
2319
+ if (this.options.perMessageDeflate && a !== void 0) {
2320
+ const h = new N(
2321
+ this.options.perMessageDeflate,
2322
+ !0,
2323
+ this.options.maxPayload
2324
+ );
2325
+ try {
2326
+ const p = qe.parse(a);
2327
+ p[N.extensionName] && (h.accept(p[N.extensionName]), c[N.extensionName] = h);
2328
+ } catch {
2329
+ R(this, e, t, 400, "Invalid or unacceptable Sec-WebSocket-Extensions header");
2330
+ return;
2331
+ }
2332
+ }
2333
+ if (this.options.verifyClient) {
2334
+ const h = {
2335
+ origin: e.headers[`${o === 8 ? "sec-websocket-origin" : "origin"}`],
2336
+ secure: !!(e.socket.authorized || e.socket.encrypted),
2337
+ req: e
2338
+ };
2339
+ if (this.options.verifyClient.length === 2) {
2340
+ this.options.verifyClient(h, (p, v, _, u) => {
2341
+ if (!p)
2342
+ return H(t, v || 401, _, u);
2343
+ this.completeUpgrade(
2344
+ c,
2345
+ n,
2346
+ f,
2347
+ e,
2348
+ t,
2349
+ r,
2350
+ i
2351
+ );
2352
+ });
2353
+ return;
2354
+ }
2355
+ if (!this.options.verifyClient(h))
2356
+ return H(t, 401);
2357
+ }
2358
+ this.completeUpgrade(c, n, f, e, t, r, i);
2359
+ }
2360
+ /**
2361
+ * Upgrade the connection to WebSocket.
2362
+ *
2363
+ * @param {Object} extensions The accepted extensions
2364
+ * @param {String} key The value of the `Sec-WebSocket-Key` header
2365
+ * @param {Set} protocols The subprotocols
2366
+ * @param {http.IncomingMessage} req The request object
2367
+ * @param {(net.Socket|tls.Socket)} socket The network socket between the
2368
+ * server and client
2369
+ * @param {Buffer} head The first packet of the upgraded stream
2370
+ * @param {Function} cb Callback
2371
+ * @throws {Error} If called more than once with the same socket
2372
+ * @private
2373
+ */
2374
+ completeUpgrade(e, t, r, i, n, o, l) {
2375
+ if (!n.readable || !n.writable)
2376
+ return n.destroy();
2377
+ if (n[Ds])
2378
+ throw new Error(
2379
+ "server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration"
2380
+ );
2381
+ if (this._state > Ke)
2382
+ return H(n, 503);
2383
+ const a = [
2384
+ "HTTP/1.1 101 Switching Protocols",
2385
+ "Upgrade: websocket",
2386
+ "Connection: Upgrade",
2387
+ `Sec-WebSocket-Accept: ${Bs("sha1").update(t + Is).digest("base64")}`
2388
+ ], c = new this.options.WebSocket(null);
2389
+ if (r.size) {
2390
+ const h = this.options.handleProtocols ? this.options.handleProtocols(r, i) : r.values().next().value;
2391
+ h && (a.push(`Sec-WebSocket-Protocol: ${h}`), c._protocol = h);
2392
+ }
2393
+ if (e[N.extensionName]) {
2394
+ const h = e[N.extensionName].params, p = qe.format({
2395
+ [N.extensionName]: [h]
2396
+ });
2397
+ a.push(`Sec-WebSocket-Extensions: ${p}`), c._extensions = e;
2398
+ }
2399
+ this.emit("headers", a, i), n.write(a.concat(`\r
2400
+ `).join(`\r
2401
+ `)), n.removeListener("error", Ze), c.setSocket(n, o, {
2402
+ maxPayload: this.options.maxPayload,
2403
+ skipUTF8Validation: this.options.skipUTF8Validation
2404
+ }), this.clients && (this.clients.add(c), c.on("close", () => {
2405
+ this.clients.delete(c), this._shouldEmitClose && !this.clients.size && process.nextTick(G, this);
2406
+ })), l(c, i);
2407
+ }
2408
+ }
2409
+ var Fs = As;
2410
+ function js(s, e) {
2411
+ for (const t of Object.keys(e))
2412
+ s.on(t, e[t]);
2413
+ return function() {
2414
+ for (const r of Object.keys(e))
2415
+ s.removeListener(r, e[r]);
2416
+ };
2417
+ }
2418
+ function G(s) {
2419
+ s._state = pt, s.emit("close");
2420
+ }
2421
+ function Ze() {
2422
+ this.destroy();
2423
+ }
2424
+ function H(s, e, t, r) {
2425
+ t = t || ie.STATUS_CODES[e], r = {
2426
+ Connection: "close",
2427
+ "Content-Type": "text/html",
2428
+ "Content-Length": Buffer.byteLength(t),
2429
+ ...r
2430
+ }, s.once("finish", s.destroy), s.end(
2431
+ `HTTP/1.1 ${e} ${ie.STATUS_CODES[e]}\r
2432
+ ` + Object.keys(r).map((i) => `${i}: ${r[i]}`).join(`\r
2433
+ `) + `\r
2434
+ \r
2435
+ ` + t
2436
+ );
2437
+ }
2438
+ function R(s, e, t, r, i) {
2439
+ if (s.listenerCount("wsClientError")) {
2440
+ const n = new Error(i);
2441
+ Error.captureStackTrace(n, R), s.emit("wsClientError", n, t, e);
2442
+ } else
2443
+ H(t, r, i);
2444
+ }
2445
+ const Zs = /* @__PURE__ */ z(Fs);
2446
+ export {
2447
+ qs as Receiver,
2448
+ Ks as Sender,
2449
+ Xs as WebSocket,
2450
+ Zs as WebSocketServer,
2451
+ Vs as createWebSocketStream,
2452
+ Xs as default
2453
+ };
src/backend/gradio_image_annotation/templates/example/index.js CHANGED
The diff for this file is too large to render. See raw diff
 
src/demo/app.py CHANGED
@@ -35,12 +35,6 @@ def get_boxes_json(annotations):
35
 
36
 
37
  with gr.Blocks() as demo:
38
- with gr.Tab("Crop"):
39
- with gr.Row():
40
- annotator_crop = image_annotator(example, image_type="numpy")
41
- image_crop = gr.Image()
42
- button_crop = gr.Button("Crop")
43
- button_crop.click(crop, annotator_crop, image_crop)
44
  with gr.Tab("Object annotation"):
45
  annotator = image_annotator(
46
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
@@ -50,6 +44,12 @@ with gr.Blocks() as demo:
50
  button_get = gr.Button("Get bounding boxes")
51
  json_boxes = gr.JSON()
52
  button_get.click(get_boxes_json, annotator, json_boxes)
 
 
 
 
 
 
53
 
54
 
55
  if __name__ == "__main__":
 
35
 
36
 
37
  with gr.Blocks() as demo:
 
 
 
 
 
 
38
  with gr.Tab("Object annotation"):
39
  annotator = image_annotator(
40
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
 
44
  button_get = gr.Button("Get bounding boxes")
45
  json_boxes = gr.JSON()
46
  button_get.click(get_boxes_json, annotator, json_boxes)
47
+ with gr.Tab("Crop"):
48
+ with gr.Row():
49
+ annotator_crop = image_annotator(example, image_type="numpy")
50
+ image_crop = gr.Image()
51
+ button_crop = gr.Button("Crop")
52
+ button_crop.click(crop, annotator_crop, image_crop)
53
 
54
 
55
  if __name__ == "__main__":
src/demo/space.py CHANGED
@@ -75,12 +75,6 @@ def get_boxes_json(annotations):
75
 
76
 
77
  with gr.Blocks() as demo:
78
- with gr.Tab("Crop"):
79
- with gr.Row():
80
- annotator_crop = image_annotator(example, image_type="numpy")
81
- image_crop = gr.Image()
82
- button_crop = gr.Button("Crop")
83
- button_crop.click(crop, annotator_crop, image_crop)
84
  with gr.Tab("Object annotation"):
85
  annotator = image_annotator(
86
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
@@ -90,6 +84,12 @@ with gr.Blocks() as demo:
90
  button_get = gr.Button("Get bounding boxes")
91
  json_boxes = gr.JSON()
92
  button_get.click(get_boxes_json, annotator, json_boxes)
 
 
 
 
 
 
93
 
94
 
95
  if __name__ == "__main__":
 
75
 
76
 
77
  with gr.Blocks() as demo:
 
 
 
 
 
 
78
  with gr.Tab("Object annotation"):
79
  annotator = image_annotator(
80
  {"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png"},
 
84
  button_get = gr.Button("Get bounding boxes")
85
  json_boxes = gr.JSON()
86
  button_get.click(get_boxes_json, annotator, json_boxes)
87
+ with gr.Tab("Crop"):
88
+ with gr.Row():
89
+ annotator_crop = image_annotator(example, image_type="numpy")
90
+ image_crop = gr.Image()
91
+ button_crop = gr.Button("Crop")
92
+ button_crop.click(crop, annotator_crop, image_crop)
93
 
94
 
95
  if __name__ == "__main__":
src/frontend/shared/Canvas.svelte CHANGED
@@ -135,6 +135,10 @@
135
  } else {
136
  color = colorHexToRGB(color);
137
  }
 
 
 
 
138
  let box = new Box(
139
  draw,
140
  canvasXmin,
@@ -142,10 +146,10 @@
142
  canvasXmax,
143
  canvasYmax,
144
  label,
145
- Math.round(canvas.width / 3),
146
- Math.round(canvas.height / 3),
147
- Math.round((2*canvas.width) / 3),
148
- Math.round((2*canvas.height) / 3),
149
  color,
150
  boxAlpha,
151
  boxMinSize,
@@ -282,7 +286,23 @@
282
 
283
  $: {
284
  value;
 
285
  parseInputBoxes();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  }
287
 
288
  onMount(() => {
@@ -298,14 +318,7 @@
298
  ctx = canvas.getContext("2d");
299
  observer.observe(canvas);
300
 
301
- if (imageUrl !== null){
302
- image = new Image();
303
- image.src = imageUrl;
304
- image.onload = function(){
305
- resize();
306
- draw();
307
- }
308
- }
309
  resize();
310
  draw();
311
  });
 
135
  } else {
136
  color = colorHexToRGB(color);
137
  }
138
+ let xmin = canvasXmin + ((canvasXmax - canvasXmin) / 3);
139
+ let xmax = canvasXmax - ((canvasXmax - canvasXmin) / 3);
140
+ let ymin = canvasYmin + ((canvasYmax - canvasYmin) / 3);
141
+ let ymax = canvasYmax - ((canvasYmax - canvasYmin) / 3);
142
  let box = new Box(
143
  draw,
144
  canvasXmin,
 
146
  canvasXmax,
147
  canvasYmax,
148
  label,
149
+ Math.round(xmin),
150
+ Math.round(ymin),
151
+ Math.round(xmax),
152
+ Math.round(ymax),
153
  color,
154
  boxAlpha,
155
  boxMinSize,
 
286
 
287
  $: {
288
  value;
289
+ setImage();
290
  parseInputBoxes();
291
+ resize();
292
+ draw();
293
+ }
294
+
295
+ function setImage(){
296
+ if (imageUrl !== null) {
297
+ if (image === null || image.src != imageUrl) {
298
+ image = new Image();
299
+ image.src = imageUrl;
300
+ image.onload = function(){
301
+ resize();
302
+ draw();
303
+ }
304
+ }
305
+ }
306
  }
307
 
308
  onMount(() => {
 
318
  ctx = canvas.getContext("2d");
319
  observer.observe(canvas);
320
 
321
+ setImage();
 
 
 
 
 
 
 
322
  resize();
323
  draw();
324
  });
src/frontend/shared/ModalBox.svelte CHANGED
@@ -6,9 +6,11 @@
6
  import { onMount, onDestroy } from "svelte";
7
 
8
  export let label = "";
 
9
  export let choices = []; // [(label, i)]
10
  export let choicesColors = [];
11
  export let color = "";
 
12
 
13
  const dispatch = createEventDispatcher<{
14
  change: object;
@@ -16,8 +18,8 @@
16
 
17
  function dispatchChange(ok: boolean) {
18
  dispatch("change", {
19
- label: label,
20
- color: color,
21
  ok: ok
22
  });
23
  }
@@ -28,19 +30,19 @@
28
 
29
  if (Number.isInteger(choice)) {
30
  if (Array.isArray(choicesColors) && choice < choicesColors.length) {
31
- color = choicesColors[choice];
32
  }
33
  if (Array.isArray(choices) && choice < choices.length) {
34
- label = choices[choice][0];
35
  }
36
  } else {
37
- label = choice;
38
  }
39
  }
40
 
41
  function onColorChange(event) {
42
  const { detail } = event;
43
- color = detail;
44
  }
45
 
46
  function onDropDownEnter(event) {
@@ -58,6 +60,8 @@
58
 
59
  onMount(() => {
60
  document.addEventListener("keydown", handleKeyPress);
 
 
61
  });
62
 
63
  onDestroy(() => {
@@ -71,7 +75,7 @@
71
  <span class="model-content">
72
  <div style="margin-right: 10px;">
73
  <BaseDropdown
74
- value={label}
75
  label="Label"
76
  {choices}
77
  show_label={false}
@@ -82,7 +86,7 @@
82
  </div>
83
  <div style="margin-right: 40px; margin-bottom: 8px;">
84
  <BaseColorPicker
85
- value={color}
86
  label="Color"
87
  show_label={false}
88
  on:change={onColorChange}
 
6
  import { onMount, onDestroy } from "svelte";
7
 
8
  export let label = "";
9
+ export let currentLabel = "";
10
  export let choices = []; // [(label, i)]
11
  export let choicesColors = [];
12
  export let color = "";
13
+ export let currentColor = "";
14
 
15
  const dispatch = createEventDispatcher<{
16
  change: object;
 
18
 
19
  function dispatchChange(ok: boolean) {
20
  dispatch("change", {
21
+ label: currentLabel,
22
+ color: currentColor,
23
  ok: ok
24
  });
25
  }
 
30
 
31
  if (Number.isInteger(choice)) {
32
  if (Array.isArray(choicesColors) && choice < choicesColors.length) {
33
+ currentColor = choicesColors[choice];
34
  }
35
  if (Array.isArray(choices) && choice < choices.length) {
36
+ currentLabel = choices[choice][0];
37
  }
38
  } else {
39
+ currentLabel = choice;
40
  }
41
  }
42
 
43
  function onColorChange(event) {
44
  const { detail } = event;
45
+ currentColor = detail;
46
  }
47
 
48
  function onDropDownEnter(event) {
 
60
 
61
  onMount(() => {
62
  document.addEventListener("keydown", handleKeyPress);
63
+ currentLabel = label;
64
+ currentColor = color;
65
  });
66
 
67
  onDestroy(() => {
 
75
  <span class="model-content">
76
  <div style="margin-right: 10px;">
77
  <BaseDropdown
78
+ value={currentLabel}
79
  label="Label"
80
  {choices}
81
  show_label={false}
 
86
  </div>
87
  <div style="margin-right: 40px; margin-bottom: 8px;">
88
  <BaseColorPicker
89
+ value={currentColor}
90
  label="Color"
91
  show_label={false}
92
  on:change={onColorChange}
src/frontend/shared/icons/Add.svelte CHANGED
@@ -1,14 +1,14 @@
1
- <svg
2
- width="100%"
3
- height="100%"
4
- viewBox="0 0 24 24"
5
- version="1.1"
6
- xmlns="http://www.w3.org/2000/svg"
7
- xmlns:xlink="http://www.w3.org/1999/xlink"
8
- xml:space="preserve"
9
- stroke="currentColor"
10
- style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;"
11
- >
12
- <line x1="4" y1="12" x2="20" y2="12" style="fill:none;stroke-width:2px;"/>
13
- <line x1="12" y1="4" x2="12" y2="20" style="fill:none;stroke-width:2px;"/>
14
- </svg>
 
1
+ <svg
2
+ width="100%"
3
+ height="100%"
4
+ viewBox="0 0 24 24"
5
+ version="1.1"
6
+ xmlns="http://www.w3.org/2000/svg"
7
+ xmlns:xlink="http://www.w3.org/1999/xlink"
8
+ xml:space="preserve"
9
+ stroke="currentColor"
10
+ style="fill-rule:evenodd;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;"
11
+ >
12
+ <line x1="4" y1="12" x2="20" y2="12" style="fill:none;stroke-width:2px;"/>
13
+ <line x1="12" y1="4" x2="12" y2="20" style="fill:none;stroke-width:2px;"/>
14
+ </svg>
src/pyproject.toml CHANGED
@@ -8,7 +8,7 @@ build-backend = "hatchling.build"
8
 
9
  [project]
10
  name = "gradio_image_annotation"
11
- version = "0.0.4"
12
  description = "A Gradio component that can be used to annotate images with bounding boxes."
13
  readme = "README.md"
14
  license = "MIT"
@@ -40,7 +40,7 @@ Issues = "https://github.com/edgarGracia/gradio_image_annotator/issues"
40
  dev = ["build", "twine"]
41
 
42
  [tool.hatch.build]
43
- artifacts = ["/backend/gradio_image_annotation/templates", "*.pyi", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates"]
44
 
45
  [tool.hatch.build.targets.wheel]
46
  packages = ["/backend/gradio_image_annotation"]
 
8
 
9
  [project]
10
  name = "gradio_image_annotation"
11
+ version = "0.0.5"
12
  description = "A Gradio component that can be used to annotate images with bounding boxes."
13
  readme = "README.md"
14
  license = "MIT"
 
40
  dev = ["build", "twine"]
41
 
42
  [tool.hatch.build]
43
+ artifacts = ["/backend/gradio_image_annotation/templates", "*.pyi", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates", "backend/gradio_image_annotation/templates"]
44
 
45
  [tool.hatch.build.targets.wheel]
46
  packages = ["/backend/gradio_image_annotation"]