Commit
·
e2db20d
1
Parent(s):
b008c1f
upd
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- Dockerfile +16 -0
- README.md +571 -8
- __init__.py +0 -0
- __pycache__/__init__.cpython-310.pyc +0 -0
- __pycache__/__init__.cpython-311.pyc +0 -0
- __pycache__/app.cpython-310.pyc +0 -0
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +95 -0
- css.css +157 -0
- requirements.txt +1 -0
- space.py +217 -0
- src/.github/workflows/python-publish.yml +39 -0
- src/.gitignore +9 -0
- src/Dockerfile +12 -0
- src/LICENSE +21 -0
- src/README.md +558 -0
- src/backend/gradio_image_annotation/__init__.py +4 -0
- src/backend/gradio_image_annotation/image_annotator.py +340 -0
- src/backend/gradio_image_annotation/templates/component/__vite-browser-external-2447137e.js +4 -0
- src/backend/gradio_image_annotation/templates/component/__vite-browser-external-DYxpcVy9.js +4 -0
- src/backend/gradio_image_annotation/templates/component/index.js +0 -0
- src/backend/gradio_image_annotation/templates/component/style.css +1 -0
- src/backend/gradio_image_annotation/templates/component/wrapper-6f348d45-19fa94bf.js +2453 -0
- src/backend/gradio_image_annotation/templates/component/wrapper-6f348d45-DjpFDl6n.js +2453 -0
- src/backend/gradio_image_annotation/templates/component/wrapper-6f348d45-f837cf34.js +2455 -0
- src/backend/gradio_image_annotation/templates/example/index.js +113 -0
- src/backend/gradio_image_annotation/templates/example/style.css +1 -0
- src/demo/__init__.py +0 -0
- src/demo/app.py +95 -0
- src/demo/css.css +157 -0
- src/demo/space.py +217 -0
- src/frontend/Example.svelte +48 -0
- src/frontend/Index.svelte +139 -0
- src/frontend/package-lock.json +0 -0
- src/frontend/package.json +35 -0
- src/frontend/shared/AnnotatedImageData.ts +7 -0
- src/frontend/shared/Box.ts +510 -0
- src/frontend/shared/Canvas.svelte +575 -0
- src/frontend/shared/ClearImage.svelte +30 -0
- src/frontend/shared/Colors.js +15 -0
- src/frontend/shared/ImageAnnotator.svelte +244 -0
- src/frontend/shared/ImageCanvas.svelte +76 -0
- src/frontend/shared/ModalBox.svelte +151 -0
- src/frontend/shared/icons/Add.svelte +14 -0
- src/frontend/shared/icons/BoundingBox.svelte +21 -0
- src/frontend/shared/icons/Hand.svelte +17 -0
- src/frontend/shared/icons/Trash.svelte +18 -0
- src/frontend/shared/icons/index.ts +4 -0
- src/frontend/shared/index.ts +1 -0
- src/frontend/shared/patched_dropdown/CHANGELOG.md +246 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
FROM python:3.10
|
3 |
+
|
4 |
+
WORKDIR /code
|
5 |
+
|
6 |
+
COPY --link --chown=1000 . .
|
7 |
+
|
8 |
+
RUN mkdir -p /tmp/cache/
|
9 |
+
RUN chmod a+rwx -R /tmp/cache/
|
10 |
+
ENV TRANSFORMERS_CACHE=/tmp/cache/
|
11 |
+
|
12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
13 |
+
|
14 |
+
ENV PYTHONUNBUFFERED=1 GRADIO_ALLOW_FLAGGING=never GRADIO_NUM_PORTS=1 GRADIO_SERVER_NAME=0.0.0.0 GRADIO_SERVER_PORT=7860 SYSTEM=spaces
|
15 |
+
|
16 |
+
CMD ["python", "space.py"]
|
README.md
CHANGED
@@ -1,12 +1,575 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
pinned: false
|
|
|
|
|
10 |
---
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
---
|
2 |
+
tags:
|
3 |
+
- gradio-custom-component
|
4 |
+
- gradio-template-Image
|
5 |
+
- bounding box
|
6 |
+
- annotator
|
7 |
+
- annotate
|
8 |
+
- boxes
|
9 |
+
title: gradio_image_annotation V0.2.6
|
10 |
+
colorFrom: yellow
|
11 |
+
colorTo: green
|
12 |
+
sdk: docker
|
13 |
pinned: false
|
14 |
+
license: apache-2.0
|
15 |
+
short_description: A Gradio component for image annotation
|
16 |
---
|
17 |
|
18 |
+
|
19 |
+
# `gradio_image_annotation`
|
20 |
+
<a href="https://pypi.org/project/gradio_image_annotation/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_image_annotation"></a>
|
21 |
+
|
22 |
+
A Gradio component that can be used to annotate images with bounding boxes.
|
23 |
+
|
24 |
+
## Installation
|
25 |
+
|
26 |
+
```bash
|
27 |
+
pip install gradio_image_annotation
|
28 |
+
```
|
29 |
+
|
30 |
+
## Usage
|
31 |
+
|
32 |
+
```python
|
33 |
+
import gradio as gr
|
34 |
+
from gradio_image_annotation import image_annotator
|
35 |
+
|
36 |
+
|
37 |
+
example_annotation = {
|
38 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
39 |
+
"boxes": [
|
40 |
+
{
|
41 |
+
"xmin": 636,
|
42 |
+
"ymin": 575,
|
43 |
+
"xmax": 801,
|
44 |
+
"ymax": 697,
|
45 |
+
"label": "Vehicle",
|
46 |
+
"color": (255, 0, 0)
|
47 |
+
},
|
48 |
+
{
|
49 |
+
"xmin": 360,
|
50 |
+
"ymin": 615,
|
51 |
+
"xmax": 386,
|
52 |
+
"ymax": 702,
|
53 |
+
"label": "Person",
|
54 |
+
"color": (0, 255, 0)
|
55 |
+
}
|
56 |
+
]
|
57 |
+
}
|
58 |
+
|
59 |
+
examples_crop = [
|
60 |
+
{
|
61 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
62 |
+
"boxes": [
|
63 |
+
{
|
64 |
+
"xmin": 30,
|
65 |
+
"ymin": 70,
|
66 |
+
"xmax": 530,
|
67 |
+
"ymax": 500,
|
68 |
+
"color": (100, 200, 255),
|
69 |
+
}
|
70 |
+
],
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
74 |
+
"boxes": [
|
75 |
+
{
|
76 |
+
"xmin": 636,
|
77 |
+
"ymin": 575,
|
78 |
+
"xmax": 801,
|
79 |
+
"ymax": 697,
|
80 |
+
"color": (255, 0, 0),
|
81 |
+
},
|
82 |
+
],
|
83 |
+
},
|
84 |
+
]
|
85 |
+
|
86 |
+
|
87 |
+
def crop(annotations):
|
88 |
+
if annotations["boxes"]:
|
89 |
+
box = annotations["boxes"][0]
|
90 |
+
return annotations["image"][
|
91 |
+
box["ymin"]:box["ymax"],
|
92 |
+
box["xmin"]:box["xmax"]
|
93 |
+
]
|
94 |
+
return None
|
95 |
+
|
96 |
+
|
97 |
+
def get_boxes_json(annotations):
|
98 |
+
return annotations["boxes"]
|
99 |
+
|
100 |
+
|
101 |
+
with gr.Blocks() as demo:
|
102 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
103 |
+
annotator = image_annotator(
|
104 |
+
example_annotation,
|
105 |
+
label_list=["Person", "Vehicle"],
|
106 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
107 |
+
)
|
108 |
+
button_get = gr.Button("Get bounding boxes")
|
109 |
+
json_boxes = gr.JSON()
|
110 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
111 |
+
|
112 |
+
with gr.Tab("Crop", id="tab_crop"):
|
113 |
+
with gr.Row():
|
114 |
+
annotator_crop = image_annotator(
|
115 |
+
examples_crop[0],
|
116 |
+
image_type="numpy",
|
117 |
+
disable_edit_boxes=True,
|
118 |
+
single_box=True,
|
119 |
+
)
|
120 |
+
image_crop = gr.Image()
|
121 |
+
button_crop = gr.Button("Crop")
|
122 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
123 |
+
|
124 |
+
gr.Examples(examples_crop, annotator_crop)
|
125 |
+
|
126 |
+
if __name__ == "__main__":
|
127 |
+
demo.launch()
|
128 |
+
|
129 |
+
```
|
130 |
+
|
131 |
+
## `image_annotator`
|
132 |
+
|
133 |
+
### Initialization
|
134 |
+
|
135 |
+
<table>
|
136 |
+
<thead>
|
137 |
+
<tr>
|
138 |
+
<th align="left">name</th>
|
139 |
+
<th align="left" style="width: 25%;">type</th>
|
140 |
+
<th align="left">default</th>
|
141 |
+
<th align="left">description</th>
|
142 |
+
</tr>
|
143 |
+
</thead>
|
144 |
+
<tbody>
|
145 |
+
<tr>
|
146 |
+
<td align="left"><code>value</code></td>
|
147 |
+
<td align="left" style="width: 25%;">
|
148 |
+
|
149 |
+
```python
|
150 |
+
dict | None
|
151 |
+
```
|
152 |
+
|
153 |
+
</td>
|
154 |
+
<td align="left"><code>None</code></td>
|
155 |
+
<td align="left">A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`).</td>
|
156 |
+
</tr>
|
157 |
+
|
158 |
+
<tr>
|
159 |
+
<td align="left"><code>boxes_alpha</code></td>
|
160 |
+
<td align="left" style="width: 25%;">
|
161 |
+
|
162 |
+
```python
|
163 |
+
float | None
|
164 |
+
```
|
165 |
+
|
166 |
+
</td>
|
167 |
+
<td align="left"><code>None</code></td>
|
168 |
+
<td align="left">Opacity of the bounding boxes 0 and 1.</td>
|
169 |
+
</tr>
|
170 |
+
|
171 |
+
<tr>
|
172 |
+
<td align="left"><code>label_list</code></td>
|
173 |
+
<td align="left" style="width: 25%;">
|
174 |
+
|
175 |
+
```python
|
176 |
+
list[str] | None
|
177 |
+
```
|
178 |
+
|
179 |
+
</td>
|
180 |
+
<td align="left"><code>None</code></td>
|
181 |
+
<td align="left">List of valid labels.</td>
|
182 |
+
</tr>
|
183 |
+
|
184 |
+
<tr>
|
185 |
+
<td align="left"><code>label_colors</code></td>
|
186 |
+
<td align="left" style="width: 25%;">
|
187 |
+
|
188 |
+
```python
|
189 |
+
list[str] | None
|
190 |
+
```
|
191 |
+
|
192 |
+
</td>
|
193 |
+
<td align="left"><code>None</code></td>
|
194 |
+
<td align="left">Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).</td>
|
195 |
+
</tr>
|
196 |
+
|
197 |
+
<tr>
|
198 |
+
<td align="left"><code>box_min_size</code></td>
|
199 |
+
<td align="left" style="width: 25%;">
|
200 |
+
|
201 |
+
```python
|
202 |
+
int | None
|
203 |
+
```
|
204 |
+
|
205 |
+
</td>
|
206 |
+
<td align="left"><code>None</code></td>
|
207 |
+
<td align="left">Minimum valid bounding box size.</td>
|
208 |
+
</tr>
|
209 |
+
|
210 |
+
<tr>
|
211 |
+
<td align="left"><code>handle_size</code></td>
|
212 |
+
<td align="left" style="width: 25%;">
|
213 |
+
|
214 |
+
```python
|
215 |
+
int | None
|
216 |
+
```
|
217 |
+
|
218 |
+
</td>
|
219 |
+
<td align="left"><code>None</code></td>
|
220 |
+
<td align="left">Size of the bounding box resize handles.</td>
|
221 |
+
</tr>
|
222 |
+
|
223 |
+
<tr>
|
224 |
+
<td align="left"><code>box_thickness</code></td>
|
225 |
+
<td align="left" style="width: 25%;">
|
226 |
+
|
227 |
+
```python
|
228 |
+
int | None
|
229 |
+
```
|
230 |
+
|
231 |
+
</td>
|
232 |
+
<td align="left"><code>None</code></td>
|
233 |
+
<td align="left">Thickness of the bounding box outline.</td>
|
234 |
+
</tr>
|
235 |
+
|
236 |
+
<tr>
|
237 |
+
<td align="left"><code>box_selected_thickness</code></td>
|
238 |
+
<td align="left" style="width: 25%;">
|
239 |
+
|
240 |
+
```python
|
241 |
+
int | None
|
242 |
+
```
|
243 |
+
|
244 |
+
</td>
|
245 |
+
<td align="left"><code>None</code></td>
|
246 |
+
<td align="left">Thickness of the bounding box outline when it is selected.</td>
|
247 |
+
</tr>
|
248 |
+
|
249 |
+
<tr>
|
250 |
+
<td align="left"><code>disable_edit_boxes</code></td>
|
251 |
+
<td align="left" style="width: 25%;">
|
252 |
+
|
253 |
+
```python
|
254 |
+
bool | None
|
255 |
+
```
|
256 |
+
|
257 |
+
</td>
|
258 |
+
<td align="left"><code>None</code></td>
|
259 |
+
<td align="left">Disables the ability to set and edit the label and color of the boxes.</td>
|
260 |
+
</tr>
|
261 |
+
|
262 |
+
<tr>
|
263 |
+
<td align="left"><code>single_box</code></td>
|
264 |
+
<td align="left" style="width: 25%;">
|
265 |
+
|
266 |
+
```python
|
267 |
+
bool
|
268 |
+
```
|
269 |
+
|
270 |
+
</td>
|
271 |
+
<td align="left"><code>False</code></td>
|
272 |
+
<td align="left">If True, at most one box can be drawn.</td>
|
273 |
+
</tr>
|
274 |
+
|
275 |
+
<tr>
|
276 |
+
<td align="left"><code>height</code></td>
|
277 |
+
<td align="left" style="width: 25%;">
|
278 |
+
|
279 |
+
```python
|
280 |
+
int | str | None
|
281 |
+
```
|
282 |
+
|
283 |
+
</td>
|
284 |
+
<td align="left"><code>None</code></td>
|
285 |
+
<td align="left">The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.</td>
|
286 |
+
</tr>
|
287 |
+
|
288 |
+
<tr>
|
289 |
+
<td align="left"><code>width</code></td>
|
290 |
+
<td align="left" style="width: 25%;">
|
291 |
+
|
292 |
+
```python
|
293 |
+
int | str | None
|
294 |
+
```
|
295 |
+
|
296 |
+
</td>
|
297 |
+
<td align="left"><code>None</code></td>
|
298 |
+
<td align="left">The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.</td>
|
299 |
+
</tr>
|
300 |
+
|
301 |
+
<tr>
|
302 |
+
<td align="left"><code>image_mode</code></td>
|
303 |
+
<td align="left" style="width: 25%;">
|
304 |
+
|
305 |
+
```python
|
306 |
+
"1"
|
307 |
+
| "L"
|
308 |
+
| "P"
|
309 |
+
| "RGB"
|
310 |
+
| "RGBA"
|
311 |
+
| "CMYK"
|
312 |
+
| "YCbCr"
|
313 |
+
| "LAB"
|
314 |
+
| "HSV"
|
315 |
+
| "I"
|
316 |
+
| "F"
|
317 |
+
```
|
318 |
+
|
319 |
+
</td>
|
320 |
+
<td align="left"><code>"RGB"</code></td>
|
321 |
+
<td align="left">"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.</td>
|
322 |
+
</tr>
|
323 |
+
|
324 |
+
<tr>
|
325 |
+
<td align="left"><code>sources</code></td>
|
326 |
+
<td align="left" style="width: 25%;">
|
327 |
+
|
328 |
+
```python
|
329 |
+
list["upload" | "webcam" | "clipboard"] | None
|
330 |
+
```
|
331 |
+
|
332 |
+
</td>
|
333 |
+
<td align="left"><code>["upload", "webcam", "clipboard"]</code></td>
|
334 |
+
<td align="left">List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].</td>
|
335 |
+
</tr>
|
336 |
+
|
337 |
+
<tr>
|
338 |
+
<td align="left"><code>image_type</code></td>
|
339 |
+
<td align="left" style="width: 25%;">
|
340 |
+
|
341 |
+
```python
|
342 |
+
"numpy" | "pil" | "filepath"
|
343 |
+
```
|
344 |
+
|
345 |
+
</td>
|
346 |
+
<td align="left"><code>"numpy"</code></td>
|
347 |
+
<td align="left">The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.</td>
|
348 |
+
</tr>
|
349 |
+
|
350 |
+
<tr>
|
351 |
+
<td align="left"><code>label</code></td>
|
352 |
+
<td align="left" style="width: 25%;">
|
353 |
+
|
354 |
+
```python
|
355 |
+
str | None
|
356 |
+
```
|
357 |
+
|
358 |
+
</td>
|
359 |
+
<td align="left"><code>None</code></td>
|
360 |
+
<td align="left">The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.</td>
|
361 |
+
</tr>
|
362 |
+
|
363 |
+
<tr>
|
364 |
+
<td align="left"><code>container</code></td>
|
365 |
+
<td align="left" style="width: 25%;">
|
366 |
+
|
367 |
+
```python
|
368 |
+
bool
|
369 |
+
```
|
370 |
+
|
371 |
+
</td>
|
372 |
+
<td align="left"><code>True</code></td>
|
373 |
+
<td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
|
374 |
+
</tr>
|
375 |
+
|
376 |
+
<tr>
|
377 |
+
<td align="left"><code>scale</code></td>
|
378 |
+
<td align="left" style="width: 25%;">
|
379 |
+
|
380 |
+
```python
|
381 |
+
int | None
|
382 |
+
```
|
383 |
+
|
384 |
+
</td>
|
385 |
+
<td align="left"><code>None</code></td>
|
386 |
+
<td align="left">relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.</td>
|
387 |
+
</tr>
|
388 |
+
|
389 |
+
<tr>
|
390 |
+
<td align="left"><code>min_width</code></td>
|
391 |
+
<td align="left" style="width: 25%;">
|
392 |
+
|
393 |
+
```python
|
394 |
+
int
|
395 |
+
```
|
396 |
+
|
397 |
+
</td>
|
398 |
+
<td align="left"><code>160</code></td>
|
399 |
+
<td align="left">minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.</td>
|
400 |
+
</tr>
|
401 |
+
|
402 |
+
<tr>
|
403 |
+
<td align="left"><code>interactive</code></td>
|
404 |
+
<td align="left" style="width: 25%;">
|
405 |
+
|
406 |
+
```python
|
407 |
+
bool | None
|
408 |
+
```
|
409 |
+
|
410 |
+
</td>
|
411 |
+
<td align="left"><code>True</code></td>
|
412 |
+
<td align="left">if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.</td>
|
413 |
+
</tr>
|
414 |
+
|
415 |
+
<tr>
|
416 |
+
<td align="left"><code>visible</code></td>
|
417 |
+
<td align="left" style="width: 25%;">
|
418 |
+
|
419 |
+
```python
|
420 |
+
bool
|
421 |
+
```
|
422 |
+
|
423 |
+
</td>
|
424 |
+
<td align="left"><code>True</code></td>
|
425 |
+
<td align="left">If False, component will be hidden.</td>
|
426 |
+
</tr>
|
427 |
+
|
428 |
+
<tr>
|
429 |
+
<td align="left"><code>elem_id</code></td>
|
430 |
+
<td align="left" style="width: 25%;">
|
431 |
+
|
432 |
+
```python
|
433 |
+
str | None
|
434 |
+
```
|
435 |
+
|
436 |
+
</td>
|
437 |
+
<td align="left"><code>None</code></td>
|
438 |
+
<td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
439 |
+
</tr>
|
440 |
+
|
441 |
+
<tr>
|
442 |
+
<td align="left"><code>elem_classes</code></td>
|
443 |
+
<td align="left" style="width: 25%;">
|
444 |
+
|
445 |
+
```python
|
446 |
+
list[str] | str | None
|
447 |
+
```
|
448 |
+
|
449 |
+
</td>
|
450 |
+
<td align="left"><code>None</code></td>
|
451 |
+
<td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
452 |
+
</tr>
|
453 |
+
|
454 |
+
<tr>
|
455 |
+
<td align="left"><code>render</code></td>
|
456 |
+
<td align="left" style="width: 25%;">
|
457 |
+
|
458 |
+
```python
|
459 |
+
bool
|
460 |
+
```
|
461 |
+
|
462 |
+
</td>
|
463 |
+
<td align="left"><code>True</code></td>
|
464 |
+
<td align="left">If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.</td>
|
465 |
+
</tr>
|
466 |
+
|
467 |
+
<tr>
|
468 |
+
<td align="left"><code>show_label</code></td>
|
469 |
+
<td align="left" style="width: 25%;">
|
470 |
+
|
471 |
+
```python
|
472 |
+
bool | None
|
473 |
+
```
|
474 |
+
|
475 |
+
</td>
|
476 |
+
<td align="left"><code>None</code></td>
|
477 |
+
<td align="left">if True, will display label.</td>
|
478 |
+
</tr>
|
479 |
+
|
480 |
+
<tr>
|
481 |
+
<td align="left"><code>show_download_button</code></td>
|
482 |
+
<td align="left" style="width: 25%;">
|
483 |
+
|
484 |
+
```python
|
485 |
+
bool
|
486 |
+
```
|
487 |
+
|
488 |
+
</td>
|
489 |
+
<td align="left"><code>True</code></td>
|
490 |
+
<td align="left">If True, will show a button to download the image.</td>
|
491 |
+
</tr>
|
492 |
+
|
493 |
+
<tr>
|
494 |
+
<td align="left"><code>show_share_button</code></td>
|
495 |
+
<td align="left" style="width: 25%;">
|
496 |
+
|
497 |
+
```python
|
498 |
+
bool | None
|
499 |
+
```
|
500 |
+
|
501 |
+
</td>
|
502 |
+
<td align="left"><code>None</code></td>
|
503 |
+
<td align="left">If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.</td>
|
504 |
+
</tr>
|
505 |
+
|
506 |
+
<tr>
|
507 |
+
<td align="left"><code>show_clear_button</code></td>
|
508 |
+
<td align="left" style="width: 25%;">
|
509 |
+
|
510 |
+
```python
|
511 |
+
bool | None
|
512 |
+
```
|
513 |
+
|
514 |
+
</td>
|
515 |
+
<td align="left"><code>True</code></td>
|
516 |
+
<td align="left">If True, will show a button to clear the current image.</td>
|
517 |
+
</tr>
|
518 |
+
|
519 |
+
<tr>
|
520 |
+
<td align="left"><code>show_remove_button</code></td>
|
521 |
+
<td align="left" style="width: 25%;">
|
522 |
+
|
523 |
+
```python
|
524 |
+
bool | None
|
525 |
+
```
|
526 |
+
|
527 |
+
</td>
|
528 |
+
<td align="left"><code>None</code></td>
|
529 |
+
<td align="left">If True, will show a button to remove the selected bounding box.</td>
|
530 |
+
</tr>
|
531 |
+
|
532 |
+
<tr>
|
533 |
+
<td align="left"><code>handles_cursor</code></td>
|
534 |
+
<td align="left" style="width: 25%;">
|
535 |
+
|
536 |
+
```python
|
537 |
+
bool | None
|
538 |
+
```
|
539 |
+
|
540 |
+
</td>
|
541 |
+
<td align="left"><code>True</code></td>
|
542 |
+
<td align="left">If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.</td>
|
543 |
+
</tr>
|
544 |
+
</tbody></table>
|
545 |
+
|
546 |
+
|
547 |
+
### Events
|
548 |
+
|
549 |
+
| name | description |
|
550 |
+
|:-----|:------------|
|
551 |
+
| `clear` | This listener is triggered when the user clears the image_annotator using the clear button for the component. |
|
552 |
+
| `change` | Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input. |
|
553 |
+
| `upload` | This listener is triggered when the user uploads a file into the image_annotator. |
|
554 |
+
|
555 |
+
|
556 |
+
|
557 |
+
### User function
|
558 |
+
|
559 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
560 |
+
|
561 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
562 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
563 |
+
|
564 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
565 |
+
|
566 |
+
- **As output:** Is passed, a dict with the image and boxes or None.
|
567 |
+
- **As input:** Should return, a dict with an image and an optional list of boxes or None.
|
568 |
+
|
569 |
+
```python
|
570 |
+
def predict(
|
571 |
+
value: dict | None
|
572 |
+
) -> dict | None:
|
573 |
+
return value
|
574 |
+
```
|
575 |
+
|
__init__.py
ADDED
File without changes
|
__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (155 Bytes). View file
|
|
__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (180 Bytes). View file
|
|
__pycache__/app.cpython-310.pyc
ADDED
Binary file (1.99 kB). View file
|
|
__pycache__/app.cpython-311.pyc
ADDED
Binary file (3.94 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_image_annotation import image_annotator
|
3 |
+
|
4 |
+
|
5 |
+
example_annotation = {
|
6 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
7 |
+
"boxes": [
|
8 |
+
{
|
9 |
+
"xmin": 636,
|
10 |
+
"ymin": 575,
|
11 |
+
"xmax": 801,
|
12 |
+
"ymax": 697,
|
13 |
+
"label": "Vehicle",
|
14 |
+
"color": (255, 0, 0)
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"xmin": 360,
|
18 |
+
"ymin": 615,
|
19 |
+
"xmax": 386,
|
20 |
+
"ymax": 702,
|
21 |
+
"label": "Person",
|
22 |
+
"color": (0, 255, 0)
|
23 |
+
}
|
24 |
+
]
|
25 |
+
}
|
26 |
+
|
27 |
+
examples_crop = [
|
28 |
+
{
|
29 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
30 |
+
"boxes": [
|
31 |
+
{
|
32 |
+
"xmin": 30,
|
33 |
+
"ymin": 70,
|
34 |
+
"xmax": 530,
|
35 |
+
"ymax": 500,
|
36 |
+
"color": (100, 200, 255),
|
37 |
+
}
|
38 |
+
],
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
42 |
+
"boxes": [
|
43 |
+
{
|
44 |
+
"xmin": 636,
|
45 |
+
"ymin": 575,
|
46 |
+
"xmax": 801,
|
47 |
+
"ymax": 697,
|
48 |
+
"color": (255, 0, 0),
|
49 |
+
},
|
50 |
+
],
|
51 |
+
},
|
52 |
+
]
|
53 |
+
|
54 |
+
|
55 |
+
def crop(annotations):
|
56 |
+
if annotations["boxes"]:
|
57 |
+
box = annotations["boxes"][0]
|
58 |
+
return annotations["image"][
|
59 |
+
box["ymin"]:box["ymax"],
|
60 |
+
box["xmin"]:box["xmax"]
|
61 |
+
]
|
62 |
+
return None
|
63 |
+
|
64 |
+
|
65 |
+
def get_boxes_json(annotations):
|
66 |
+
return annotations["boxes"]
|
67 |
+
|
68 |
+
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
71 |
+
annotator = image_annotator(
|
72 |
+
example_annotation,
|
73 |
+
label_list=["Person", "Vehicle"],
|
74 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
75 |
+
)
|
76 |
+
button_get = gr.Button("Get bounding boxes")
|
77 |
+
json_boxes = gr.JSON()
|
78 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
79 |
+
|
80 |
+
with gr.Tab("Crop", id="tab_crop"):
|
81 |
+
with gr.Row():
|
82 |
+
annotator_crop = image_annotator(
|
83 |
+
examples_crop[0],
|
84 |
+
image_type="numpy",
|
85 |
+
disable_edit_boxes=True,
|
86 |
+
single_box=True,
|
87 |
+
)
|
88 |
+
image_crop = gr.Image()
|
89 |
+
button_crop = gr.Button("Crop")
|
90 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
91 |
+
|
92 |
+
gr.Examples(examples_crop, annotator_crop)
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
demo.launch()
|
css.css
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
html {
|
2 |
+
font-family: Inter;
|
3 |
+
font-size: 16px;
|
4 |
+
font-weight: 400;
|
5 |
+
line-height: 1.5;
|
6 |
+
-webkit-text-size-adjust: 100%;
|
7 |
+
background: #fff;
|
8 |
+
color: #323232;
|
9 |
+
-webkit-font-smoothing: antialiased;
|
10 |
+
-moz-osx-font-smoothing: grayscale;
|
11 |
+
text-rendering: optimizeLegibility;
|
12 |
+
}
|
13 |
+
|
14 |
+
:root {
|
15 |
+
--space: 1;
|
16 |
+
--vspace: calc(var(--space) * 1rem);
|
17 |
+
--vspace-0: calc(3 * var(--space) * 1rem);
|
18 |
+
--vspace-1: calc(2 * var(--space) * 1rem);
|
19 |
+
--vspace-2: calc(1.5 * var(--space) * 1rem);
|
20 |
+
--vspace-3: calc(0.5 * var(--space) * 1rem);
|
21 |
+
}
|
22 |
+
|
23 |
+
.app {
|
24 |
+
max-width: 748px !important;
|
25 |
+
}
|
26 |
+
|
27 |
+
.prose p {
|
28 |
+
margin: var(--vspace) 0;
|
29 |
+
line-height: var(--vspace * 2);
|
30 |
+
font-size: 1rem;
|
31 |
+
}
|
32 |
+
|
33 |
+
code {
|
34 |
+
font-family: "Inconsolata", sans-serif;
|
35 |
+
font-size: 16px;
|
36 |
+
}
|
37 |
+
|
38 |
+
h1,
|
39 |
+
h1 code {
|
40 |
+
font-weight: 400;
|
41 |
+
line-height: calc(2.5 / var(--space) * var(--vspace));
|
42 |
+
}
|
43 |
+
|
44 |
+
h1 code {
|
45 |
+
background: none;
|
46 |
+
border: none;
|
47 |
+
letter-spacing: 0.05em;
|
48 |
+
padding-bottom: 5px;
|
49 |
+
position: relative;
|
50 |
+
padding: 0;
|
51 |
+
}
|
52 |
+
|
53 |
+
h2 {
|
54 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
55 |
+
line-height: 1em;
|
56 |
+
}
|
57 |
+
|
58 |
+
h3,
|
59 |
+
h3 code {
|
60 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
61 |
+
line-height: 1em;
|
62 |
+
}
|
63 |
+
|
64 |
+
h4,
|
65 |
+
h5,
|
66 |
+
h6 {
|
67 |
+
margin: var(--vspace-3) 0 var(--vspace-3) 0;
|
68 |
+
line-height: var(--vspace);
|
69 |
+
}
|
70 |
+
|
71 |
+
.bigtitle,
|
72 |
+
h1,
|
73 |
+
h1 code {
|
74 |
+
font-size: calc(8px * 4.5);
|
75 |
+
word-break: break-word;
|
76 |
+
}
|
77 |
+
|
78 |
+
.title,
|
79 |
+
h2,
|
80 |
+
h2 code {
|
81 |
+
font-size: calc(8px * 3.375);
|
82 |
+
font-weight: lighter;
|
83 |
+
word-break: break-word;
|
84 |
+
border: none;
|
85 |
+
background: none;
|
86 |
+
}
|
87 |
+
|
88 |
+
.subheading1,
|
89 |
+
h3,
|
90 |
+
h3 code {
|
91 |
+
font-size: calc(8px * 1.8);
|
92 |
+
font-weight: 600;
|
93 |
+
border: none;
|
94 |
+
background: none;
|
95 |
+
letter-spacing: 0.1em;
|
96 |
+
text-transform: uppercase;
|
97 |
+
}
|
98 |
+
|
99 |
+
h2 code {
|
100 |
+
padding: 0;
|
101 |
+
position: relative;
|
102 |
+
letter-spacing: 0.05em;
|
103 |
+
}
|
104 |
+
|
105 |
+
blockquote {
|
106 |
+
font-size: calc(8px * 1.1667);
|
107 |
+
font-style: italic;
|
108 |
+
line-height: calc(1.1667 * var(--vspace));
|
109 |
+
margin: var(--vspace-2) var(--vspace-2);
|
110 |
+
}
|
111 |
+
|
112 |
+
.subheading2,
|
113 |
+
h4 {
|
114 |
+
font-size: calc(8px * 1.4292);
|
115 |
+
text-transform: uppercase;
|
116 |
+
font-weight: 600;
|
117 |
+
}
|
118 |
+
|
119 |
+
.subheading3,
|
120 |
+
h5 {
|
121 |
+
font-size: calc(8px * 1.2917);
|
122 |
+
line-height: calc(1.2917 * var(--vspace));
|
123 |
+
|
124 |
+
font-weight: lighter;
|
125 |
+
text-transform: uppercase;
|
126 |
+
letter-spacing: 0.15em;
|
127 |
+
}
|
128 |
+
|
129 |
+
h6 {
|
130 |
+
font-size: calc(8px * 1.1667);
|
131 |
+
font-size: 1.1667em;
|
132 |
+
font-weight: normal;
|
133 |
+
font-style: italic;
|
134 |
+
font-family: "le-monde-livre-classic-byol", serif !important;
|
135 |
+
letter-spacing: 0px !important;
|
136 |
+
}
|
137 |
+
|
138 |
+
#start .md > *:first-child {
|
139 |
+
margin-top: 0;
|
140 |
+
}
|
141 |
+
|
142 |
+
h2 + h3 {
|
143 |
+
margin-top: 0;
|
144 |
+
}
|
145 |
+
|
146 |
+
.md hr {
|
147 |
+
border: none;
|
148 |
+
border-top: 1px solid var(--block-border-color);
|
149 |
+
margin: var(--vspace-2) 0 var(--vspace-2) 0;
|
150 |
+
}
|
151 |
+
.prose ul {
|
152 |
+
margin: var(--vspace-2) 0 var(--vspace-1) 0;
|
153 |
+
}
|
154 |
+
|
155 |
+
.gap {
|
156 |
+
gap: 0;
|
157 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
gradio_image_annotation==0.2.6
|
space.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from app import demo as app
|
4 |
+
import os
|
5 |
+
|
6 |
+
_docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`)."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}}, 'postprocess': {'value': {'type': 'dict | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'dict | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'image_annotator': []}}}
|
7 |
+
|
8 |
+
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
css=abs_path,
|
12 |
+
theme=gr.themes.Default(
|
13 |
+
font_mono=[
|
14 |
+
gr.themes.GoogleFont("Inconsolata"),
|
15 |
+
"monospace",
|
16 |
+
],
|
17 |
+
),
|
18 |
+
) as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"""
|
21 |
+
# `gradio_image_annotation`
|
22 |
+
|
23 |
+
<div style="display: flex; gap: 7px;">
|
24 |
+
<a href="https://pypi.org/project/gradio_image_annotation/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_image_annotation"></a>
|
25 |
+
</div>
|
26 |
+
|
27 |
+
A Gradio component that can be used to annotate images with bounding boxes.
|
28 |
+
""", elem_classes=["md-custom"], header_links=True)
|
29 |
+
app.render()
|
30 |
+
gr.Markdown(
|
31 |
+
"""
|
32 |
+
## Installation
|
33 |
+
|
34 |
+
```bash
|
35 |
+
pip install gradio_image_annotation
|
36 |
+
```
|
37 |
+
|
38 |
+
## Usage
|
39 |
+
|
40 |
+
```python
|
41 |
+
import gradio as gr
|
42 |
+
from gradio_image_annotation import image_annotator
|
43 |
+
|
44 |
+
|
45 |
+
example_annotation = {
|
46 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
47 |
+
"boxes": [
|
48 |
+
{
|
49 |
+
"xmin": 636,
|
50 |
+
"ymin": 575,
|
51 |
+
"xmax": 801,
|
52 |
+
"ymax": 697,
|
53 |
+
"label": "Vehicle",
|
54 |
+
"color": (255, 0, 0)
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"xmin": 360,
|
58 |
+
"ymin": 615,
|
59 |
+
"xmax": 386,
|
60 |
+
"ymax": 702,
|
61 |
+
"label": "Person",
|
62 |
+
"color": (0, 255, 0)
|
63 |
+
}
|
64 |
+
]
|
65 |
+
}
|
66 |
+
|
67 |
+
examples_crop = [
|
68 |
+
{
|
69 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
70 |
+
"boxes": [
|
71 |
+
{
|
72 |
+
"xmin": 30,
|
73 |
+
"ymin": 70,
|
74 |
+
"xmax": 530,
|
75 |
+
"ymax": 500,
|
76 |
+
"color": (100, 200, 255),
|
77 |
+
}
|
78 |
+
],
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
82 |
+
"boxes": [
|
83 |
+
{
|
84 |
+
"xmin": 636,
|
85 |
+
"ymin": 575,
|
86 |
+
"xmax": 801,
|
87 |
+
"ymax": 697,
|
88 |
+
"color": (255, 0, 0),
|
89 |
+
},
|
90 |
+
],
|
91 |
+
},
|
92 |
+
]
|
93 |
+
|
94 |
+
|
95 |
+
def crop(annotations):
|
96 |
+
if annotations["boxes"]:
|
97 |
+
box = annotations["boxes"][0]
|
98 |
+
return annotations["image"][
|
99 |
+
box["ymin"]:box["ymax"],
|
100 |
+
box["xmin"]:box["xmax"]
|
101 |
+
]
|
102 |
+
return None
|
103 |
+
|
104 |
+
|
105 |
+
def get_boxes_json(annotations):
|
106 |
+
return annotations["boxes"]
|
107 |
+
|
108 |
+
|
109 |
+
with gr.Blocks() as demo:
|
110 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
111 |
+
annotator = image_annotator(
|
112 |
+
example_annotation,
|
113 |
+
label_list=["Person", "Vehicle"],
|
114 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
115 |
+
)
|
116 |
+
button_get = gr.Button("Get bounding boxes")
|
117 |
+
json_boxes = gr.JSON()
|
118 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
119 |
+
|
120 |
+
with gr.Tab("Crop", id="tab_crop"):
|
121 |
+
with gr.Row():
|
122 |
+
annotator_crop = image_annotator(
|
123 |
+
examples_crop[0],
|
124 |
+
image_type="numpy",
|
125 |
+
disable_edit_boxes=True,
|
126 |
+
single_box=True,
|
127 |
+
)
|
128 |
+
image_crop = gr.Image()
|
129 |
+
button_crop = gr.Button("Crop")
|
130 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
131 |
+
|
132 |
+
gr.Examples(examples_crop, annotator_crop)
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
demo.launch()
|
136 |
+
|
137 |
+
```
|
138 |
+
""", elem_classes=["md-custom"], header_links=True)
|
139 |
+
|
140 |
+
|
141 |
+
gr.Markdown("""
|
142 |
+
## `image_annotator`
|
143 |
+
|
144 |
+
### Initialization
|
145 |
+
""", elem_classes=["md-custom"], header_links=True)
|
146 |
+
|
147 |
+
gr.ParamViewer(value=_docs["image_annotator"]["members"]["__init__"], linkify=[])
|
148 |
+
|
149 |
+
|
150 |
+
gr.Markdown("### Events")
|
151 |
+
gr.ParamViewer(value=_docs["image_annotator"]["events"], linkify=['Event'])
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
gr.Markdown("""
|
157 |
+
|
158 |
+
### User function
|
159 |
+
|
160 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
161 |
+
|
162 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
163 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
164 |
+
|
165 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
166 |
+
|
167 |
+
- **As input:** Is passed, a dict with the image and boxes or None.
|
168 |
+
- **As output:** Should return, a dict with an image and an optional list of boxes or None.
|
169 |
+
|
170 |
+
```python
|
171 |
+
def predict(
|
172 |
+
value: dict | None
|
173 |
+
) -> dict | None:
|
174 |
+
return value
|
175 |
+
```
|
176 |
+
""", elem_classes=["md-custom", "image_annotator-user-fn"], header_links=True)
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
demo.load(None, js=r"""function() {
|
182 |
+
const refs = {};
|
183 |
+
const user_fn_refs = {
|
184 |
+
image_annotator: [], };
|
185 |
+
requestAnimationFrame(() => {
|
186 |
+
|
187 |
+
Object.entries(user_fn_refs).forEach(([key, refs]) => {
|
188 |
+
if (refs.length > 0) {
|
189 |
+
const el = document.querySelector(`.${key}-user-fn`);
|
190 |
+
if (!el) return;
|
191 |
+
refs.forEach(ref => {
|
192 |
+
el.innerHTML = el.innerHTML.replace(
|
193 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
194 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
195 |
+
);
|
196 |
+
})
|
197 |
+
}
|
198 |
+
})
|
199 |
+
|
200 |
+
Object.entries(refs).forEach(([key, refs]) => {
|
201 |
+
if (refs.length > 0) {
|
202 |
+
const el = document.querySelector(`.${key}`);
|
203 |
+
if (!el) return;
|
204 |
+
refs.forEach(ref => {
|
205 |
+
el.innerHTML = el.innerHTML.replace(
|
206 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
207 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
208 |
+
);
|
209 |
+
})
|
210 |
+
}
|
211 |
+
})
|
212 |
+
})
|
213 |
+
}
|
214 |
+
|
215 |
+
""")
|
216 |
+
|
217 |
+
demo.launch()
|
src/.github/workflows/python-publish.yml
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This workflow will upload a Python Package using Twine when a release is created
|
2 |
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
|
3 |
+
|
4 |
+
# This workflow uses actions that are not certified by GitHub.
|
5 |
+
# They are provided by a third-party and are governed by
|
6 |
+
# separate terms of service, privacy policy, and support
|
7 |
+
# documentation.
|
8 |
+
|
9 |
+
name: Upload Python Package
|
10 |
+
|
11 |
+
on:
|
12 |
+
release:
|
13 |
+
types: [published]
|
14 |
+
|
15 |
+
permissions:
|
16 |
+
contents: read
|
17 |
+
|
18 |
+
jobs:
|
19 |
+
deploy:
|
20 |
+
|
21 |
+
runs-on: ubuntu-latest
|
22 |
+
|
23 |
+
steps:
|
24 |
+
- uses: actions/checkout@v3
|
25 |
+
- name: Set up Python
|
26 |
+
uses: actions/setup-python@v3
|
27 |
+
with:
|
28 |
+
python-version: '3.x'
|
29 |
+
- name: Install dependencies
|
30 |
+
run: |
|
31 |
+
python -m pip install --upgrade pip
|
32 |
+
pip install build
|
33 |
+
- name: Build package
|
34 |
+
run: python -m build
|
35 |
+
- name: Publish package
|
36 |
+
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
|
37 |
+
with:
|
38 |
+
user: __token__
|
39 |
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
src/.gitignore
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.eggs/
|
2 |
+
dist/
|
3 |
+
*.pyc
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
7 |
+
__tmp/*
|
8 |
+
*.pyi
|
9 |
+
node_modules
|
src/Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# FROM nikolaik/python-nodejs:python3.10-nodejs20-alpine
|
2 |
+
FROM node:20.5.0
|
3 |
+
|
4 |
+
WORKDIR /app
|
5 |
+
|
6 |
+
|
7 |
+
RUN apt-get update && \
|
8 |
+
apt-get install -y python3-pip
|
9 |
+
|
10 |
+
RUN rm /usr/lib/python3.11/EXTERNALLY-MANAGED
|
11 |
+
|
12 |
+
RUN python3 -m pip install --no-cache-dir gradio==4.27 numpy
|
src/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Edgar Gracia
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
src/README.md
ADDED
@@ -0,0 +1,558 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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shields.io/pypi/v/gradio_image_annotation"></a>
|
4 |
+
|
5 |
+
A Gradio component that can be used to annotate images with bounding boxes.
|
6 |
+
|
7 |
+
## Installation
|
8 |
+
|
9 |
+
```bash
|
10 |
+
pip install gradio_image_annotation
|
11 |
+
```
|
12 |
+
|
13 |
+
## Usage
|
14 |
+
|
15 |
+
```python
|
16 |
+
import gradio as gr
|
17 |
+
from gradio_image_annotation import image_annotator
|
18 |
+
|
19 |
+
|
20 |
+
example_annotation = {
|
21 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
22 |
+
"boxes": [
|
23 |
+
{
|
24 |
+
"xmin": 636,
|
25 |
+
"ymin": 575,
|
26 |
+
"xmax": 801,
|
27 |
+
"ymax": 697,
|
28 |
+
"label": "Vehicle",
|
29 |
+
"color": (255, 0, 0)
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"xmin": 360,
|
33 |
+
"ymin": 615,
|
34 |
+
"xmax": 386,
|
35 |
+
"ymax": 702,
|
36 |
+
"label": "Person",
|
37 |
+
"color": (0, 255, 0)
|
38 |
+
}
|
39 |
+
]
|
40 |
+
}
|
41 |
+
|
42 |
+
examples_crop = [
|
43 |
+
{
|
44 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
45 |
+
"boxes": [
|
46 |
+
{
|
47 |
+
"xmin": 30,
|
48 |
+
"ymin": 70,
|
49 |
+
"xmax": 530,
|
50 |
+
"ymax": 500,
|
51 |
+
"color": (100, 200, 255),
|
52 |
+
}
|
53 |
+
],
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
57 |
+
"boxes": [
|
58 |
+
{
|
59 |
+
"xmin": 636,
|
60 |
+
"ymin": 575,
|
61 |
+
"xmax": 801,
|
62 |
+
"ymax": 697,
|
63 |
+
"color": (255, 0, 0),
|
64 |
+
},
|
65 |
+
],
|
66 |
+
},
|
67 |
+
]
|
68 |
+
|
69 |
+
|
70 |
+
def crop(annotations):
|
71 |
+
if annotations["boxes"]:
|
72 |
+
box = annotations["boxes"][0]
|
73 |
+
return annotations["image"][
|
74 |
+
box["ymin"]:box["ymax"],
|
75 |
+
box["xmin"]:box["xmax"]
|
76 |
+
]
|
77 |
+
return None
|
78 |
+
|
79 |
+
|
80 |
+
def get_boxes_json(annotations):
|
81 |
+
return annotations["boxes"]
|
82 |
+
|
83 |
+
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
86 |
+
annotator = image_annotator(
|
87 |
+
example_annotation,
|
88 |
+
label_list=["Person", "Vehicle"],
|
89 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
90 |
+
)
|
91 |
+
button_get = gr.Button("Get bounding boxes")
|
92 |
+
json_boxes = gr.JSON()
|
93 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
94 |
+
|
95 |
+
with gr.Tab("Crop", id="tab_crop"):
|
96 |
+
with gr.Row():
|
97 |
+
annotator_crop = image_annotator(
|
98 |
+
examples_crop[0],
|
99 |
+
image_type="numpy",
|
100 |
+
disable_edit_boxes=True,
|
101 |
+
single_box=True,
|
102 |
+
)
|
103 |
+
image_crop = gr.Image()
|
104 |
+
button_crop = gr.Button("Crop")
|
105 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
106 |
+
|
107 |
+
gr.Examples(examples_crop, annotator_crop)
|
108 |
+
|
109 |
+
if __name__ == "__main__":
|
110 |
+
demo.launch()
|
111 |
+
|
112 |
+
```
|
113 |
+
|
114 |
+
## `image_annotator`
|
115 |
+
|
116 |
+
### Initialization
|
117 |
+
|
118 |
+
<table>
|
119 |
+
<thead>
|
120 |
+
<tr>
|
121 |
+
<th align="left">name</th>
|
122 |
+
<th align="left" style="width: 25%;">type</th>
|
123 |
+
<th align="left">default</th>
|
124 |
+
<th align="left">description</th>
|
125 |
+
</tr>
|
126 |
+
</thead>
|
127 |
+
<tbody>
|
128 |
+
<tr>
|
129 |
+
<td align="left"><code>value</code></td>
|
130 |
+
<td align="left" style="width: 25%;">
|
131 |
+
|
132 |
+
```python
|
133 |
+
dict | None
|
134 |
+
```
|
135 |
+
|
136 |
+
</td>
|
137 |
+
<td align="left"><code>None</code></td>
|
138 |
+
<td align="left">A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`).</td>
|
139 |
+
</tr>
|
140 |
+
|
141 |
+
<tr>
|
142 |
+
<td align="left"><code>boxes_alpha</code></td>
|
143 |
+
<td align="left" style="width: 25%;">
|
144 |
+
|
145 |
+
```python
|
146 |
+
float | None
|
147 |
+
```
|
148 |
+
|
149 |
+
</td>
|
150 |
+
<td align="left"><code>None</code></td>
|
151 |
+
<td align="left">Opacity of the bounding boxes 0 and 1.</td>
|
152 |
+
</tr>
|
153 |
+
|
154 |
+
<tr>
|
155 |
+
<td align="left"><code>label_list</code></td>
|
156 |
+
<td align="left" style="width: 25%;">
|
157 |
+
|
158 |
+
```python
|
159 |
+
list[str] | None
|
160 |
+
```
|
161 |
+
|
162 |
+
</td>
|
163 |
+
<td align="left"><code>None</code></td>
|
164 |
+
<td align="left">List of valid labels.</td>
|
165 |
+
</tr>
|
166 |
+
|
167 |
+
<tr>
|
168 |
+
<td align="left"><code>label_colors</code></td>
|
169 |
+
<td align="left" style="width: 25%;">
|
170 |
+
|
171 |
+
```python
|
172 |
+
list[str] | None
|
173 |
+
```
|
174 |
+
|
175 |
+
</td>
|
176 |
+
<td align="left"><code>None</code></td>
|
177 |
+
<td align="left">Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).</td>
|
178 |
+
</tr>
|
179 |
+
|
180 |
+
<tr>
|
181 |
+
<td align="left"><code>box_min_size</code></td>
|
182 |
+
<td align="left" style="width: 25%;">
|
183 |
+
|
184 |
+
```python
|
185 |
+
int | None
|
186 |
+
```
|
187 |
+
|
188 |
+
</td>
|
189 |
+
<td align="left"><code>None</code></td>
|
190 |
+
<td align="left">Minimum valid bounding box size.</td>
|
191 |
+
</tr>
|
192 |
+
|
193 |
+
<tr>
|
194 |
+
<td align="left"><code>handle_size</code></td>
|
195 |
+
<td align="left" style="width: 25%;">
|
196 |
+
|
197 |
+
```python
|
198 |
+
int | None
|
199 |
+
```
|
200 |
+
|
201 |
+
</td>
|
202 |
+
<td align="left"><code>None</code></td>
|
203 |
+
<td align="left">Size of the bounding box resize handles.</td>
|
204 |
+
</tr>
|
205 |
+
|
206 |
+
<tr>
|
207 |
+
<td align="left"><code>box_thickness</code></td>
|
208 |
+
<td align="left" style="width: 25%;">
|
209 |
+
|
210 |
+
```python
|
211 |
+
int | None
|
212 |
+
```
|
213 |
+
|
214 |
+
</td>
|
215 |
+
<td align="left"><code>None</code></td>
|
216 |
+
<td align="left">Thickness of the bounding box outline.</td>
|
217 |
+
</tr>
|
218 |
+
|
219 |
+
<tr>
|
220 |
+
<td align="left"><code>box_selected_thickness</code></td>
|
221 |
+
<td align="left" style="width: 25%;">
|
222 |
+
|
223 |
+
```python
|
224 |
+
int | None
|
225 |
+
```
|
226 |
+
|
227 |
+
</td>
|
228 |
+
<td align="left"><code>None</code></td>
|
229 |
+
<td align="left">Thickness of the bounding box outline when it is selected.</td>
|
230 |
+
</tr>
|
231 |
+
|
232 |
+
<tr>
|
233 |
+
<td align="left"><code>disable_edit_boxes</code></td>
|
234 |
+
<td align="left" style="width: 25%;">
|
235 |
+
|
236 |
+
```python
|
237 |
+
bool | None
|
238 |
+
```
|
239 |
+
|
240 |
+
</td>
|
241 |
+
<td align="left"><code>None</code></td>
|
242 |
+
<td align="left">Disables the ability to set and edit the label and color of the boxes.</td>
|
243 |
+
</tr>
|
244 |
+
|
245 |
+
<tr>
|
246 |
+
<td align="left"><code>single_box</code></td>
|
247 |
+
<td align="left" style="width: 25%;">
|
248 |
+
|
249 |
+
```python
|
250 |
+
bool
|
251 |
+
```
|
252 |
+
|
253 |
+
</td>
|
254 |
+
<td align="left"><code>False</code></td>
|
255 |
+
<td align="left">If True, at most one box can be drawn.</td>
|
256 |
+
</tr>
|
257 |
+
|
258 |
+
<tr>
|
259 |
+
<td align="left"><code>height</code></td>
|
260 |
+
<td align="left" style="width: 25%;">
|
261 |
+
|
262 |
+
```python
|
263 |
+
int | str | None
|
264 |
+
```
|
265 |
+
|
266 |
+
</td>
|
267 |
+
<td align="left"><code>None</code></td>
|
268 |
+
<td align="left">The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.</td>
|
269 |
+
</tr>
|
270 |
+
|
271 |
+
<tr>
|
272 |
+
<td align="left"><code>width</code></td>
|
273 |
+
<td align="left" style="width: 25%;">
|
274 |
+
|
275 |
+
```python
|
276 |
+
int | str | None
|
277 |
+
```
|
278 |
+
|
279 |
+
</td>
|
280 |
+
<td align="left"><code>None</code></td>
|
281 |
+
<td align="left">The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.</td>
|
282 |
+
</tr>
|
283 |
+
|
284 |
+
<tr>
|
285 |
+
<td align="left"><code>image_mode</code></td>
|
286 |
+
<td align="left" style="width: 25%;">
|
287 |
+
|
288 |
+
```python
|
289 |
+
"1"
|
290 |
+
| "L"
|
291 |
+
| "P"
|
292 |
+
| "RGB"
|
293 |
+
| "RGBA"
|
294 |
+
| "CMYK"
|
295 |
+
| "YCbCr"
|
296 |
+
| "LAB"
|
297 |
+
| "HSV"
|
298 |
+
| "I"
|
299 |
+
| "F"
|
300 |
+
```
|
301 |
+
|
302 |
+
</td>
|
303 |
+
<td align="left"><code>"RGB"</code></td>
|
304 |
+
<td align="left">"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.</td>
|
305 |
+
</tr>
|
306 |
+
|
307 |
+
<tr>
|
308 |
+
<td align="left"><code>sources</code></td>
|
309 |
+
<td align="left" style="width: 25%;">
|
310 |
+
|
311 |
+
```python
|
312 |
+
list["upload" | "webcam" | "clipboard"] | None
|
313 |
+
```
|
314 |
+
|
315 |
+
</td>
|
316 |
+
<td align="left"><code>["upload", "webcam", "clipboard"]</code></td>
|
317 |
+
<td align="left">List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].</td>
|
318 |
+
</tr>
|
319 |
+
|
320 |
+
<tr>
|
321 |
+
<td align="left"><code>image_type</code></td>
|
322 |
+
<td align="left" style="width: 25%;">
|
323 |
+
|
324 |
+
```python
|
325 |
+
"numpy" | "pil" | "filepath"
|
326 |
+
```
|
327 |
+
|
328 |
+
</td>
|
329 |
+
<td align="left"><code>"numpy"</code></td>
|
330 |
+
<td align="left">The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.</td>
|
331 |
+
</tr>
|
332 |
+
|
333 |
+
<tr>
|
334 |
+
<td align="left"><code>label</code></td>
|
335 |
+
<td align="left" style="width: 25%;">
|
336 |
+
|
337 |
+
```python
|
338 |
+
str | None
|
339 |
+
```
|
340 |
+
|
341 |
+
</td>
|
342 |
+
<td align="left"><code>None</code></td>
|
343 |
+
<td align="left">The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.</td>
|
344 |
+
</tr>
|
345 |
+
|
346 |
+
<tr>
|
347 |
+
<td align="left"><code>container</code></td>
|
348 |
+
<td align="left" style="width: 25%;">
|
349 |
+
|
350 |
+
```python
|
351 |
+
bool
|
352 |
+
```
|
353 |
+
|
354 |
+
</td>
|
355 |
+
<td align="left"><code>True</code></td>
|
356 |
+
<td align="left">If True, will place the component in a container - providing some extra padding around the border.</td>
|
357 |
+
</tr>
|
358 |
+
|
359 |
+
<tr>
|
360 |
+
<td align="left"><code>scale</code></td>
|
361 |
+
<td align="left" style="width: 25%;">
|
362 |
+
|
363 |
+
```python
|
364 |
+
int | None
|
365 |
+
```
|
366 |
+
|
367 |
+
</td>
|
368 |
+
<td align="left"><code>None</code></td>
|
369 |
+
<td align="left">relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.</td>
|
370 |
+
</tr>
|
371 |
+
|
372 |
+
<tr>
|
373 |
+
<td align="left"><code>min_width</code></td>
|
374 |
+
<td align="left" style="width: 25%;">
|
375 |
+
|
376 |
+
```python
|
377 |
+
int
|
378 |
+
```
|
379 |
+
|
380 |
+
</td>
|
381 |
+
<td align="left"><code>160</code></td>
|
382 |
+
<td align="left">minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.</td>
|
383 |
+
</tr>
|
384 |
+
|
385 |
+
<tr>
|
386 |
+
<td align="left"><code>interactive</code></td>
|
387 |
+
<td align="left" style="width: 25%;">
|
388 |
+
|
389 |
+
```python
|
390 |
+
bool | None
|
391 |
+
```
|
392 |
+
|
393 |
+
</td>
|
394 |
+
<td align="left"><code>True</code></td>
|
395 |
+
<td align="left">if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.</td>
|
396 |
+
</tr>
|
397 |
+
|
398 |
+
<tr>
|
399 |
+
<td align="left"><code>visible</code></td>
|
400 |
+
<td align="left" style="width: 25%;">
|
401 |
+
|
402 |
+
```python
|
403 |
+
bool
|
404 |
+
```
|
405 |
+
|
406 |
+
</td>
|
407 |
+
<td align="left"><code>True</code></td>
|
408 |
+
<td align="left">If False, component will be hidden.</td>
|
409 |
+
</tr>
|
410 |
+
|
411 |
+
<tr>
|
412 |
+
<td align="left"><code>elem_id</code></td>
|
413 |
+
<td align="left" style="width: 25%;">
|
414 |
+
|
415 |
+
```python
|
416 |
+
str | None
|
417 |
+
```
|
418 |
+
|
419 |
+
</td>
|
420 |
+
<td align="left"><code>None</code></td>
|
421 |
+
<td align="left">An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
422 |
+
</tr>
|
423 |
+
|
424 |
+
<tr>
|
425 |
+
<td align="left"><code>elem_classes</code></td>
|
426 |
+
<td align="left" style="width: 25%;">
|
427 |
+
|
428 |
+
```python
|
429 |
+
list[str] | str | None
|
430 |
+
```
|
431 |
+
|
432 |
+
</td>
|
433 |
+
<td align="left"><code>None</code></td>
|
434 |
+
<td align="left">An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.</td>
|
435 |
+
</tr>
|
436 |
+
|
437 |
+
<tr>
|
438 |
+
<td align="left"><code>render</code></td>
|
439 |
+
<td align="left" style="width: 25%;">
|
440 |
+
|
441 |
+
```python
|
442 |
+
bool
|
443 |
+
```
|
444 |
+
|
445 |
+
</td>
|
446 |
+
<td align="left"><code>True</code></td>
|
447 |
+
<td align="left">If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.</td>
|
448 |
+
</tr>
|
449 |
+
|
450 |
+
<tr>
|
451 |
+
<td align="left"><code>show_label</code></td>
|
452 |
+
<td align="left" style="width: 25%;">
|
453 |
+
|
454 |
+
```python
|
455 |
+
bool | None
|
456 |
+
```
|
457 |
+
|
458 |
+
</td>
|
459 |
+
<td align="left"><code>None</code></td>
|
460 |
+
<td align="left">if True, will display label.</td>
|
461 |
+
</tr>
|
462 |
+
|
463 |
+
<tr>
|
464 |
+
<td align="left"><code>show_download_button</code></td>
|
465 |
+
<td align="left" style="width: 25%;">
|
466 |
+
|
467 |
+
```python
|
468 |
+
bool
|
469 |
+
```
|
470 |
+
|
471 |
+
</td>
|
472 |
+
<td align="left"><code>True</code></td>
|
473 |
+
<td align="left">If True, will show a button to download the image.</td>
|
474 |
+
</tr>
|
475 |
+
|
476 |
+
<tr>
|
477 |
+
<td align="left"><code>show_share_button</code></td>
|
478 |
+
<td align="left" style="width: 25%;">
|
479 |
+
|
480 |
+
```python
|
481 |
+
bool | None
|
482 |
+
```
|
483 |
+
|
484 |
+
</td>
|
485 |
+
<td align="left"><code>None</code></td>
|
486 |
+
<td align="left">If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.</td>
|
487 |
+
</tr>
|
488 |
+
|
489 |
+
<tr>
|
490 |
+
<td align="left"><code>show_clear_button</code></td>
|
491 |
+
<td align="left" style="width: 25%;">
|
492 |
+
|
493 |
+
```python
|
494 |
+
bool | None
|
495 |
+
```
|
496 |
+
|
497 |
+
</td>
|
498 |
+
<td align="left"><code>True</code></td>
|
499 |
+
<td align="left">If True, will show a button to clear the current image.</td>
|
500 |
+
</tr>
|
501 |
+
|
502 |
+
<tr>
|
503 |
+
<td align="left"><code>show_remove_button</code></td>
|
504 |
+
<td align="left" style="width: 25%;">
|
505 |
+
|
506 |
+
```python
|
507 |
+
bool | None
|
508 |
+
```
|
509 |
+
|
510 |
+
</td>
|
511 |
+
<td align="left"><code>None</code></td>
|
512 |
+
<td align="left">If True, will show a button to remove the selected bounding box.</td>
|
513 |
+
</tr>
|
514 |
+
|
515 |
+
<tr>
|
516 |
+
<td align="left"><code>handles_cursor</code></td>
|
517 |
+
<td align="left" style="width: 25%;">
|
518 |
+
|
519 |
+
```python
|
520 |
+
bool | None
|
521 |
+
```
|
522 |
+
|
523 |
+
</td>
|
524 |
+
<td align="left"><code>True</code></td>
|
525 |
+
<td align="left">If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.</td>
|
526 |
+
</tr>
|
527 |
+
</tbody></table>
|
528 |
+
|
529 |
+
|
530 |
+
### Events
|
531 |
+
|
532 |
+
| name | description |
|
533 |
+
|:-----|:------------|
|
534 |
+
| `clear` | This listener is triggered when the user clears the image_annotator using the clear button for the component. |
|
535 |
+
| `change` | Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input. |
|
536 |
+
| `upload` | This listener is triggered when the user uploads a file into the image_annotator. |
|
537 |
+
|
538 |
+
|
539 |
+
|
540 |
+
### User function
|
541 |
+
|
542 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
543 |
+
|
544 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
545 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
546 |
+
|
547 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
548 |
+
|
549 |
+
- **As output:** Is passed, a dict with the image and boxes or None.
|
550 |
+
- **As input:** Should return, a dict with an image and an optional list of boxes or None.
|
551 |
+
|
552 |
+
```python
|
553 |
+
def predict(
|
554 |
+
value: dict | None
|
555 |
+
) -> dict | None:
|
556 |
+
return value
|
557 |
+
```
|
558 |
+
|
src/backend/gradio_image_annotation/__init__.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from .image_annotator import image_annotator
|
3 |
+
|
4 |
+
__all__ = ['image_annotator']
|
src/backend/gradio_image_annotation/image_annotator.py
ADDED
@@ -0,0 +1,340 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
import re
|
4 |
+
import warnings
|
5 |
+
from pathlib import Path
|
6 |
+
from typing import Any, List, Literal, cast
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import PIL.Image
|
10 |
+
from PIL import ImageOps
|
11 |
+
|
12 |
+
from gradio import image_utils, utils
|
13 |
+
from gradio.components.base import Component
|
14 |
+
from gradio.data_classes import FileData, GradioModel
|
15 |
+
from gradio.events import Events
|
16 |
+
|
17 |
+
PIL.Image.init() # fixes https://github.com/gradio-app/gradio/issues/2843
|
18 |
+
|
19 |
+
|
20 |
+
class AnnotatedImageData(GradioModel):
|
21 |
+
image: FileData
|
22 |
+
boxes: List[dict] = []
|
23 |
+
|
24 |
+
|
25 |
+
def rgb2hex(r,g,b):
|
26 |
+
def clip(x):
|
27 |
+
return max(min(x, 255), 0)
|
28 |
+
return "#{:02x}{:02x}{:02x}".format(clip(r),clip(g),clip(b))
|
29 |
+
|
30 |
+
|
31 |
+
class image_annotator(Component):
|
32 |
+
"""
|
33 |
+
Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.
|
34 |
+
It is also possible to predefine a set of valid classes and colors.
|
35 |
+
"""
|
36 |
+
|
37 |
+
EVENTS = [
|
38 |
+
Events.clear,
|
39 |
+
Events.change,
|
40 |
+
Events.upload,
|
41 |
+
]
|
42 |
+
|
43 |
+
data_model = AnnotatedImageData
|
44 |
+
|
45 |
+
def __init__(
|
46 |
+
self,
|
47 |
+
value: dict | None = None,
|
48 |
+
*,
|
49 |
+
boxes_alpha: float | None = None,
|
50 |
+
label_list: list[str] | None = None,
|
51 |
+
label_colors: list[str] | None = None,
|
52 |
+
box_min_size: int | None = None,
|
53 |
+
handle_size: int | None = None,
|
54 |
+
box_thickness: int | None = None,
|
55 |
+
box_selected_thickness: int | None = None,
|
56 |
+
disable_edit_boxes: bool | None = None,
|
57 |
+
single_box: bool = False,
|
58 |
+
height: int | str | None = None,
|
59 |
+
width: int | str | None = None,
|
60 |
+
image_mode: Literal[
|
61 |
+
"1", "L", "P", "RGB", "RGBA", "CMYK", "YCbCr", "LAB", "HSV", "I", "F"
|
62 |
+
] = "RGB",
|
63 |
+
sources: list[Literal["upload", "webcam", "clipboard"]] | None = [
|
64 |
+
"upload",
|
65 |
+
"webcam",
|
66 |
+
"clipboard",
|
67 |
+
],
|
68 |
+
image_type: Literal["numpy", "pil", "filepath"] = "numpy",
|
69 |
+
label: str | None = None,
|
70 |
+
container: bool = True,
|
71 |
+
scale: int | None = None,
|
72 |
+
min_width: int = 160,
|
73 |
+
interactive: bool | None = True,
|
74 |
+
visible: bool = True,
|
75 |
+
elem_id: str | None = None,
|
76 |
+
elem_classes: list[str] | str | None = None,
|
77 |
+
render: bool = True,
|
78 |
+
show_label: bool | None = None,
|
79 |
+
show_download_button: bool = True,
|
80 |
+
show_share_button: bool | None = None,
|
81 |
+
show_clear_button: bool | None = True,
|
82 |
+
show_remove_button: bool | None = None,
|
83 |
+
handles_cursor: bool | None = True,
|
84 |
+
):
|
85 |
+
"""
|
86 |
+
Parameters:
|
87 |
+
value: A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`).
|
88 |
+
boxes_alpha: Opacity of the bounding boxes 0 and 1.
|
89 |
+
label_list: List of valid labels.
|
90 |
+
label_colors: Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).
|
91 |
+
box_min_size: Minimum valid bounding box size.
|
92 |
+
handle_size: Size of the bounding box resize handles.
|
93 |
+
box_thickness: Thickness of the bounding box outline.
|
94 |
+
box_selected_thickness: Thickness of the bounding box outline when it is selected.
|
95 |
+
disable_edit_boxes: Disables the ability to set and edit the label and color of the boxes.
|
96 |
+
single_box: If True, at most one box can be drawn.
|
97 |
+
height: The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.
|
98 |
+
width: The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.
|
99 |
+
image_mode: "RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.
|
100 |
+
sources: List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].
|
101 |
+
image_type: The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.
|
102 |
+
label: The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
|
103 |
+
container: If True, will place the component in a container - providing some extra padding around the border.
|
104 |
+
scale: relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
|
105 |
+
min_width: minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
|
106 |
+
interactive: if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.
|
107 |
+
visible: If False, component will be hidden.
|
108 |
+
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
109 |
+
elem_classes: An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
|
110 |
+
render: If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
|
111 |
+
show_label: if True, will display label.
|
112 |
+
show_download_button: If True, will show a button to download the image.
|
113 |
+
show_share_button: If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
|
114 |
+
show_clear_button: If True, will show a button to clear the current image.
|
115 |
+
show_remove_button: If True, will show a button to remove the selected bounding box.
|
116 |
+
handles_cursor: If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.
|
117 |
+
"""
|
118 |
+
|
119 |
+
valid_types = ["numpy", "pil", "filepath"]
|
120 |
+
if image_type not in valid_types:
|
121 |
+
raise ValueError(
|
122 |
+
f"Invalid value for parameter `type`: {type}. Please choose from one of: {valid_types}"
|
123 |
+
)
|
124 |
+
self.image_type = image_type
|
125 |
+
self.height = height
|
126 |
+
self.width = width
|
127 |
+
self.image_mode = image_mode
|
128 |
+
|
129 |
+
self.sources = sources
|
130 |
+
valid_sources = ["upload", "clipboard", "webcam", None]
|
131 |
+
if isinstance(sources, str):
|
132 |
+
self.sources = [sources]
|
133 |
+
if self.sources is None:
|
134 |
+
self.sources = []
|
135 |
+
if self.sources is not None:
|
136 |
+
for source in self.sources:
|
137 |
+
if source not in valid_sources:
|
138 |
+
raise ValueError(
|
139 |
+
f"`sources` must a list consisting of elements in {valid_sources}"
|
140 |
+
)
|
141 |
+
|
142 |
+
self.show_download_button = show_download_button
|
143 |
+
self.show_share_button = (
|
144 |
+
(utils.get_space() is not None)
|
145 |
+
if show_share_button is None
|
146 |
+
else show_share_button
|
147 |
+
)
|
148 |
+
self.show_clear_button = show_clear_button
|
149 |
+
self.show_remove_button = show_remove_button
|
150 |
+
self.handles_cursor = handles_cursor
|
151 |
+
|
152 |
+
self.boxes_alpha = boxes_alpha
|
153 |
+
self.box_min_size = box_min_size
|
154 |
+
self.handle_size = handle_size
|
155 |
+
self.box_thickness = box_thickness
|
156 |
+
self.box_selected_thickness = box_selected_thickness
|
157 |
+
self.disable_edit_boxes = disable_edit_boxes
|
158 |
+
self.single_box = single_box
|
159 |
+
if label_list:
|
160 |
+
self.label_list = [(l, i) for i, l in enumerate(label_list)]
|
161 |
+
else:
|
162 |
+
self.label_list = None
|
163 |
+
|
164 |
+
# Parse colors
|
165 |
+
self.label_colors = label_colors
|
166 |
+
if self.label_colors:
|
167 |
+
if (not isinstance(self.label_colors, list)
|
168 |
+
or self.label_list is None
|
169 |
+
or len(self.label_colors) != len(self.label_list)):
|
170 |
+
raise ValueError("``label_colors`` must be a list with the "
|
171 |
+
"same length as ``label_list``")
|
172 |
+
for i, color in enumerate(self.label_colors):
|
173 |
+
if isinstance(color, str):
|
174 |
+
if len(color) != 7 or color[0] != "#":
|
175 |
+
raise ValueError(f"Invalid color value {color}")
|
176 |
+
elif isinstance(color, (list, tuple)):
|
177 |
+
self.label_colors[i] = rgb2hex(*color)
|
178 |
+
|
179 |
+
super().__init__(
|
180 |
+
label=label,
|
181 |
+
every=None,
|
182 |
+
show_label=show_label,
|
183 |
+
container=container,
|
184 |
+
scale=scale,
|
185 |
+
min_width=min_width,
|
186 |
+
interactive=interactive,
|
187 |
+
visible=visible,
|
188 |
+
elem_id=elem_id,
|
189 |
+
elem_classes=elem_classes,
|
190 |
+
render=render,
|
191 |
+
value=value,
|
192 |
+
)
|
193 |
+
|
194 |
+
def preprocess_image(self, image: FileData | None) -> str | None:
|
195 |
+
if image is None:
|
196 |
+
return None
|
197 |
+
file_path = Path(image.path)
|
198 |
+
if image.orig_name:
|
199 |
+
p = Path(image.orig_name)
|
200 |
+
name = p.stem
|
201 |
+
suffix = p.suffix.replace(".", "")
|
202 |
+
if suffix in ["jpg", "jpeg"]:
|
203 |
+
suffix = "jpeg"
|
204 |
+
else:
|
205 |
+
name = "image"
|
206 |
+
suffix = "png"
|
207 |
+
|
208 |
+
if suffix.lower() == "svg":
|
209 |
+
return str(file_path)
|
210 |
+
|
211 |
+
im = PIL.Image.open(file_path)
|
212 |
+
exif = im.getexif()
|
213 |
+
# 274 is the code for image rotation and 1 means "correct orientation"
|
214 |
+
if exif.get(274, 1) != 1 and hasattr(ImageOps, "exif_transpose"):
|
215 |
+
try:
|
216 |
+
im = ImageOps.exif_transpose(im)
|
217 |
+
except Exception:
|
218 |
+
warnings.warn(
|
219 |
+
f"Failed to transpose image {file_path} based on EXIF data."
|
220 |
+
)
|
221 |
+
with warnings.catch_warnings():
|
222 |
+
warnings.simplefilter("ignore")
|
223 |
+
im = im.convert(self.image_mode)
|
224 |
+
return image_utils.format_image(
|
225 |
+
im,
|
226 |
+
cast(Literal["numpy", "pil", "filepath"], self.image_type),
|
227 |
+
self.GRADIO_CACHE,
|
228 |
+
name=name,
|
229 |
+
format=suffix,
|
230 |
+
)
|
231 |
+
|
232 |
+
def preprocess_boxes(self, boxes: List[dict] | None) -> list:
|
233 |
+
parsed_boxes = []
|
234 |
+
for box in boxes:
|
235 |
+
new_box = {}
|
236 |
+
new_box["label"] = box.get("label", "")
|
237 |
+
new_box["color"] = (0,0,0)
|
238 |
+
if "color" in box:
|
239 |
+
match = re.match(r'rgb\((\d+), (\d+), (\d+)\)', box["color"])
|
240 |
+
if match:
|
241 |
+
new_box["color"] = tuple(int(match.group(i)) for i in range(1, 4))
|
242 |
+
scale_factor = box.get("scaleFactor", 1)
|
243 |
+
new_box["xmin"] = round(box["xmin"] / scale_factor)
|
244 |
+
new_box["ymin"] = round(box["ymin"] / scale_factor)
|
245 |
+
new_box["xmax"] = round(box["xmax"] / scale_factor)
|
246 |
+
new_box["ymax"] = round(box["ymax"] / scale_factor)
|
247 |
+
parsed_boxes.append(new_box)
|
248 |
+
return parsed_boxes
|
249 |
+
|
250 |
+
def preprocess(self, payload: AnnotatedImageData | None) -> dict | None:
|
251 |
+
"""
|
252 |
+
Parameters:
|
253 |
+
payload: an AnnotatedImageData object.
|
254 |
+
Returns:
|
255 |
+
A dict with the image and boxes or None.
|
256 |
+
"""
|
257 |
+
if payload is None:
|
258 |
+
return None
|
259 |
+
|
260 |
+
ret_value = {
|
261 |
+
"image": self.preprocess_image(payload.image),
|
262 |
+
"boxes": self.preprocess_boxes(payload.boxes)
|
263 |
+
}
|
264 |
+
return ret_value
|
265 |
+
|
266 |
+
def postprocess(self, value: dict | None) -> AnnotatedImageData | None:
|
267 |
+
"""
|
268 |
+
Parameters:
|
269 |
+
value: A dict with an image and an optional list of boxes or None.
|
270 |
+
Returns:
|
271 |
+
Returns an AnnotatedImageData object.
|
272 |
+
"""
|
273 |
+
# Check value
|
274 |
+
if value is None:
|
275 |
+
return None
|
276 |
+
if not isinstance(value, dict):
|
277 |
+
raise ValueError(f"``value`` must be a dict. Got {type(value)}")
|
278 |
+
|
279 |
+
# Check and get boxes
|
280 |
+
boxes = value.setdefault("boxes", [])
|
281 |
+
if boxes:
|
282 |
+
if not isinstance(value["boxes"], (list, tuple)):
|
283 |
+
raise ValueError(f"'boxes' must be a list of dicts. Got "
|
284 |
+
f"{type(value['boxes'])}")
|
285 |
+
for box in value["boxes"]:
|
286 |
+
if (not isinstance(box, dict)
|
287 |
+
or not set(box.keys()).issubset({"label", "xmin", "ymin", "xmax", "ymax", "color"})
|
288 |
+
or not set(box.keys()).issuperset({"xmin", "ymin", "xmax", "ymax"})
|
289 |
+
):
|
290 |
+
raise ValueError("Box must be a dict with the following "
|
291 |
+
"keys: 'xmin', 'ymin', 'xmax', 'ymax', "
|
292 |
+
f"['label', 'color']'. Got {box}")
|
293 |
+
|
294 |
+
# Check and parse image
|
295 |
+
image = value.setdefault("image", None)
|
296 |
+
if image is not None:
|
297 |
+
if isinstance(image, str) and image.lower().endswith(".svg"):
|
298 |
+
image = FileData(path=image, orig_name=Path(image).name)
|
299 |
+
else:
|
300 |
+
saved = image_utils.save_image(image, self.GRADIO_CACHE)
|
301 |
+
orig_name = Path(saved).name if Path(saved).exists() else None
|
302 |
+
image = FileData(path=saved, orig_name=orig_name)
|
303 |
+
else:
|
304 |
+
raise ValueError(f"An image must be provided. Got {value}")
|
305 |
+
|
306 |
+
return AnnotatedImageData(image=image, boxes=boxes)
|
307 |
+
|
308 |
+
def process_example(self, value: dict | None) -> FileData | None:
|
309 |
+
if value is None:
|
310 |
+
return None
|
311 |
+
if not isinstance(value, dict):
|
312 |
+
raise ValueError(f"``value`` must be a dict. Got {type(value)}")
|
313 |
+
|
314 |
+
image = value.setdefault("image", None)
|
315 |
+
if image is not None:
|
316 |
+
if isinstance(image, str) and image.lower().endswith(".svg"):
|
317 |
+
image = FileData(path=image, orig_name=Path(image).name)
|
318 |
+
else:
|
319 |
+
saved = image_utils.save_image(image, self.GRADIO_CACHE)
|
320 |
+
orig_name = Path(saved).name if Path(saved).exists() else None
|
321 |
+
image = FileData(path=saved, orig_name=orig_name)
|
322 |
+
else:
|
323 |
+
raise ValueError(f"An image must be provided. Got {value}")
|
324 |
+
|
325 |
+
return image
|
326 |
+
|
327 |
+
def example_inputs(self) -> Any:
|
328 |
+
return {
|
329 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
330 |
+
"boxes": [
|
331 |
+
{
|
332 |
+
"xmin": 30,
|
333 |
+
"ymin": 70,
|
334 |
+
"xmax": 530,
|
335 |
+
"ymax": 500,
|
336 |
+
"label": "Gradio",
|
337 |
+
"color": (250,185,0),
|
338 |
+
}
|
339 |
+
]
|
340 |
+
}
|
src/backend/gradio_image_annotation/templates/component/__vite-browser-external-2447137e.js
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const e = {};
|
2 |
+
export {
|
3 |
+
e as default
|
4 |
+
};
|
src/backend/gradio_image_annotation/templates/component/__vite-browser-external-DYxpcVy9.js
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const e = {};
|
2 |
+
export {
|
3 |
+
e as default
|
4 |
+
};
|
src/backend/gradio_image_annotation/templates/component/index.js
ADDED
The diff for this file is too large to render.
See raw diff
|
|
src/backend/gradio_image_annotation/templates/component/style.css
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.block.svelte-nl1om8{position:relative;margin:0;box-shadow:var(--block-shadow);border-width:var(--block-border-width);border-color:var(--block-border-color);border-radius:var(--block-radius);background:var(--block-background-fill);width:100%;line-height:var(--line-sm)}.block.border_focus.svelte-nl1om8{border-color:var(--color-accent)}.block.border_contrast.svelte-nl1om8{border-color:var(--body-text-color)}.padded.svelte-nl1om8{padding:var(--block-padding)}.hidden.svelte-nl1om8{display:none}.hide-container.svelte-nl1om8{margin:0;box-shadow:none;--block-border-width:0;background:transparent;padding:0;overflow:visible}div.svelte-1hnfib2{margin-bottom:var(--spacing-lg);color:var(--block-info-text-color);font-weight:var(--block-info-text-weight);font-size:var(--block-info-text-size);line-height:var(--line-sm)}span.has-info.svelte-22c38v{margin-bottom:var(--spacing-xs)}span.svelte-22c38v:not(.has-info){margin-bottom:var(--spacing-lg)}span.svelte-22c38v{display:inline-block;position:relative;z-index:var(--layer-4);border:solid var(--block-title-border-width) var(--block-title-border-color);border-radius:var(--block-title-radius);background:var(--block-title-background-fill);padding:var(--block-title-padding);color:var(--block-title-text-color);font-weight:var(--block-title-text-weight);font-size:var(--block-title-text-size);line-height:var(--line-sm)}.hide.svelte-22c38v{margin:0;height:0}label.svelte-9gxdi0{display:inline-flex;align-items:center;z-index:var(--layer-2);box-shadow:var(--block-label-shadow);border:var(--block-label-border-width) solid var(--border-color-primary);border-top:none;border-left:none;border-radius:var(--block-label-radius);background:var(--block-label-background-fill);padding:var(--block-label-padding);pointer-events:none;color:var(--block-label-text-color);font-weight:var(--block-label-text-weight);font-size:var(--block-label-text-size);line-height:var(--line-sm)}.gr-group label.svelte-9gxdi0{border-top-left-radius:0}label.float.svelte-9gxdi0{position:absolute;top:var(--block-label-margin);left:var(--block-label-margin)}label.svelte-9gxdi0:not(.float){position:static;margin-top:var(--block-label-margin);margin-left:var(--block-label-margin)}.hide.svelte-9gxdi0{height:0}span.svelte-9gxdi0{opacity:.8;margin-right:var(--size-2);width:calc(var(--block-label-text-size) - 1px);height:calc(var(--block-label-text-size) - 1px)}.hide-label.svelte-9gxdi0{box-shadow:none;border-width:0;background:transparent;overflow:visible}button.svelte-1lrphxw{display:flex;justify-content:center;align-items:center;gap:1px;z-index:var(--layer-2);border-radius:var(--radius-sm);color:var(--block-label-text-color);border:1px solid transparent}button[disabled].svelte-1lrphxw{opacity:.5;box-shadow:none}button[disabled].svelte-1lrphxw:hover{cursor:not-allowed}.padded.svelte-1lrphxw{padding:2px;background:var(--bg-color);box-shadow:var(--shadow-drop);border:1px solid var(--button-secondary-border-color)}button.svelte-1lrphxw:hover,button.highlight.svelte-1lrphxw{cursor:pointer;color:var(--color-accent)}.padded.svelte-1lrphxw:hover{border:2px solid var(--button-secondary-border-color-hover);padding:1px;color:var(--block-label-text-color)}span.svelte-1lrphxw{padding:0 1px;font-size:10px}div.svelte-1lrphxw{padding:2px;display:flex;align-items:flex-end}.small.svelte-1lrphxw{width:14px;height:14px}.medium.svelte-1lrphxw{width:20px;height:20px}.large.svelte-1lrphxw{width:22px;height:22px}.pending.svelte-1lrphxw{animation:svelte-1lrphxw-flash .5s infinite}@keyframes svelte-1lrphxw-flash{0%{opacity:.5}50%{opacity:1}to{opacity:.5}}.transparent.svelte-1lrphxw{background:transparent;border:none;box-shadow:none}.empty.svelte-3w3rth{display:flex;justify-content:center;align-items:center;margin-top:calc(0px - var(--size-6));height:var(--size-full)}.icon.svelte-3w3rth{opacity:.5;height:var(--size-5);color:var(--body-text-color)}.small.svelte-3w3rth{min-height:calc(var(--size-32) - 20px)}.large.svelte-3w3rth{min-height:calc(var(--size-64) - 20px)}.unpadded_box.svelte-3w3rth{margin-top:0}.small_parent.svelte-3w3rth{min-height:100%!important}.dropdown-arrow.svelte-145leq6{fill:currentColor}.wrap.svelte-kzcjhc{display:flex;flex-direction:column;justify-content:center;align-items:center;min-height:var(--size-60);color:var(--block-label-text-color);line-height:var(--line-md);height:100%;padding-top:var(--size-3)}.or.svelte-kzcjhc{color:var(--body-text-color-subdued);display:flex}.icon-wrap.svelte-kzcjhc{width:30px;margin-bottom:var(--spacing-lg)}@media (--screen-md){.wrap.svelte-kzcjhc{font-size:var(--text-lg)}}.hovered.svelte-kzcjhc{color:var(--color-accent)}div.svelte-q32hvf{border-top:1px solid transparent;display:flex;max-height:100%;justify-content:center;align-items:center;gap:var(--spacing-sm);height:auto;align-items:flex-end;color:var(--block-label-text-color);flex-shrink:0}.show_border.svelte-q32hvf{border-top:1px solid var(--block-border-color);margin-top:var(--spacing-xxl);box-shadow:var(--shadow-drop)}.source-selection.svelte-1jp3vgd{display:flex;align-items:center;justify-content:center;border-top:1px solid var(--border-color-primary);width:95%;bottom:0;left:0;right:0;margin-left:auto;margin-right:auto}.icon.svelte-1jp3vgd{width:22px;height:22px;margin:var(--spacing-lg) var(--spacing-xs);padding:var(--spacing-xs);color:var(--neutral-400);border-radius:var(--radius-md)}.selected.svelte-1jp3vgd{color:var(--color-accent)}.icon.svelte-1jp3vgd:hover,.icon.svelte-1jp3vgd:focus{color:var(--color-accent)}.wrap.svelte-16nch4a.svelte-16nch4a{display:flex;flex-direction:column;justify-content:center;align-items:center;z-index:var(--layer-2);transition:opacity .1s ease-in-out;border-radius:var(--block-radius);background:var(--block-background-fill);padding:0 var(--size-6);max-height:var(--size-screen-h);overflow:hidden}.wrap.center.svelte-16nch4a.svelte-16nch4a{top:0;right:0;left:0}.wrap.default.svelte-16nch4a.svelte-16nch4a{top:0;right:0;bottom:0;left:0}.hide.svelte-16nch4a.svelte-16nch4a{opacity:0;pointer-events:none}.generating.svelte-16nch4a.svelte-16nch4a{animation:svelte-16nch4a-pulseStart 1s cubic-bezier(.4,0,.6,1),svelte-16nch4a-pulse 2s cubic-bezier(.4,0,.6,1) 1s infinite;border:2px solid var(--color-accent);background:transparent;z-index:var(--layer-1);pointer-events:none}.translucent.svelte-16nch4a.svelte-16nch4a{background:none}@keyframes svelte-16nch4a-pulseStart{0%{opacity:0}to{opacity:1}}@keyframes svelte-16nch4a-pulse{0%,to{opacity:1}50%{opacity:.5}}.loading.svelte-16nch4a.svelte-16nch4a{z-index:var(--layer-2);color:var(--body-text-color)}.eta-bar.svelte-16nch4a.svelte-16nch4a{position:absolute;top:0;right:0;bottom:0;left:0;transform-origin:left;opacity:.8;z-index:var(--layer-1);transition:10ms;background:var(--background-fill-secondary)}.progress-bar-wrap.svelte-16nch4a.svelte-16nch4a{border:1px solid var(--border-color-primary);background:var(--background-fill-primary);width:55.5%;height:var(--size-4)}.progress-bar.svelte-16nch4a.svelte-16nch4a{transform-origin:left;background-color:var(--loader-color);width:var(--size-full);height:var(--size-full)}.progress-level.svelte-16nch4a.svelte-16nch4a{display:flex;flex-direction:column;align-items:center;gap:1;z-index:var(--layer-2);width:var(--size-full)}.progress-level-inner.svelte-16nch4a.svelte-16nch4a{margin:var(--size-2) auto;color:var(--body-text-color);font-size:var(--text-sm);font-family:var(--font-mono)}.meta-text.svelte-16nch4a.svelte-16nch4a{position:absolute;top:0;right:0;z-index:var(--layer-2);padding:var(--size-1) var(--size-2);font-size:var(--text-sm);font-family:var(--font-mono)}.meta-text-center.svelte-16nch4a.svelte-16nch4a{display:flex;position:absolute;top:0;right:0;justify-content:center;align-items:center;transform:translateY(var(--size-6));z-index:var(--layer-2);padding:var(--size-1) var(--size-2);font-size:var(--text-sm);font-family:var(--font-mono);text-align:center}.error.svelte-16nch4a.svelte-16nch4a{box-shadow:var(--shadow-drop);border:solid 1px var(--error-border-color);border-radius:var(--radius-full);background:var(--error-background-fill);padding-right:var(--size-4);padding-left:var(--size-4);color:var(--error-text-color);font-weight:var(--weight-semibold);font-size:var(--text-lg);line-height:var(--line-lg);font-family:var(--font)}.minimal.svelte-16nch4a .progress-text.svelte-16nch4a{background:var(--block-background-fill)}.border.svelte-16nch4a.svelte-16nch4a{border:1px solid var(--border-color-primary)}.clear-status.svelte-16nch4a.svelte-16nch4a{position:absolute;display:flex;top:var(--size-2);right:var(--size-2);justify-content:flex-end;gap:var(--spacing-sm);z-index:var(--layer-1)}.wrap.svelte-cr2edf.svelte-cr2edf{overflow-y:auto;transition:opacity .5s ease-in-out;background:var(--block-background-fill);position:relative;display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:var(--size-40);width:var(--size-full)}.wrap.svelte-cr2edf.svelte-cr2edf:after{content:"";position:absolute;top:0;left:0;width:var(--upload-progress-width);height:100%;transition:all .5s ease-in-out;z-index:1}.uploading.svelte-cr2edf.svelte-cr2edf{font-size:var(--text-lg);font-family:var(--font);z-index:2}.file-name.svelte-cr2edf.svelte-cr2edf{margin:var(--spacing-md);font-size:var(--text-lg);color:var(--body-text-color-subdued)}.file.svelte-cr2edf.svelte-cr2edf{font-size:var(--text-md);z-index:2;display:flex;align-items:center}.file.svelte-cr2edf progress.svelte-cr2edf{display:inline;height:var(--size-1);width:100%;transition:all .5s ease-in-out;color:var(--color-accent);border:none}.file.svelte-cr2edf progress[value].svelte-cr2edf::-webkit-progress-value{background-color:var(--color-accent);border-radius:20px}.file.svelte-cr2edf progress[value].svelte-cr2edf::-webkit-progress-bar{background-color:var(--border-color-accent);border-radius:20px}.progress-bar.svelte-cr2edf.svelte-cr2edf{width:14px;height:14px;border-radius:50%;background:radial-gradient(closest-side,var(--block-background-fill) 64%,transparent 53% 100%),conic-gradient(var(--color-accent) var(--upload-progress-width),var(--border-color-accent) 0);transition:all .5s ease-in-out}button.svelte-1s26xmt{cursor:pointer;width:var(--size-full)}.hidden.svelte-1s26xmt{display:none;height:0!important;position:absolute;width:0;flex-grow:0}.center.svelte-1s26xmt{display:flex;justify-content:center}.flex.svelte-1s26xmt{display:flex;flex-direction:column;justify-content:center;align-items:center}.disable_click.svelte-1s26xmt{cursor:default}input.svelte-1s26xmt{display:none}div.svelte-1wj0ocy{display:flex;top:var(--size-2);right:var(--size-2);justify-content:flex-end;gap:var(--spacing-sm);z-index:var(--layer-1)}.not-absolute.svelte-1wj0ocy{margin:var(--size-1)}img.svelte-kxeri3{object-fit:cover}.image-container.svelte-n22rtv img,button.svelte-n22rtv{width:var(--size-full);height:var(--size-full);object-fit:contain;display:block;border-radius:var(--radius-lg)}.selectable.svelte-n22rtv{cursor:crosshair}.icon-buttons.svelte-n22rtv{display:flex;position:absolute;top:6px;right:6px;gap:var(--size-1)}button.svelte-fjcd9c{cursor:pointer;width:var(--size-full)}.wrap.svelte-fjcd9c{display:flex;flex-direction:column;justify-content:center;align-items:center;min-height:var(--size-60);color:var(--block-label-text-color);height:100%;padding-top:var(--size-3)}.icon-wrap.svelte-fjcd9c{width:30px;margin-bottom:var(--spacing-lg)}@media (--screen-md){.wrap.svelte-fjcd9c{font-size:var(--text-lg)}}.wrap.svelte-8hqvb6.svelte-8hqvb6{position:relative;width:var(--size-full);height:var(--size-full)}.hide.svelte-8hqvb6.svelte-8hqvb6{display:none}video.svelte-8hqvb6.svelte-8hqvb6{width:var(--size-full);height:var(--size-full);object-fit:cover}.button-wrap.svelte-8hqvb6.svelte-8hqvb6{position:absolute;background-color:var(--block-background-fill);border:1px solid var(--border-color-primary);padding:var(--size-1-5);display:flex;bottom:var(--size-2);left:50%;transform:translate(-50%);box-shadow:var(--shadow-drop-lg);border-radius:var(--radius-xl);line-height:var(--size-3);color:var(--button-secondary-text-color)}@media (--screen-md){button.svelte-8hqvb6.svelte-8hqvb6{bottom:var(--size-4)}}@media (--screen-xl){button.svelte-8hqvb6.svelte-8hqvb6{bottom:var(--size-8)}}.icon.svelte-8hqvb6.svelte-8hqvb6{opacity:.8;width:18px;height:18px;display:flex;justify-content:space-between;align-items:center}.red.svelte-8hqvb6.svelte-8hqvb6{fill:red;stroke:red}.flip.svelte-8hqvb6.svelte-8hqvb6{transform:scaleX(-1)}.select-wrap.svelte-8hqvb6.svelte-8hqvb6{-webkit-appearance:none;-moz-appearance:none;appearance:none;color:var(--button-secondary-text-color);background-color:transparent;width:95%;font-size:var(--text-md);position:absolute;bottom:var(--size-2);background-color:var(--block-background-fill);box-shadow:var(--shadow-drop-lg);border-radius:var(--radius-xl);z-index:var(--layer-top);border:1px solid var(--border-color-primary);text-align:left;line-height:var(--size-4);white-space:nowrap;text-overflow:ellipsis;left:50%;transform:translate(-50%);max-width:var(--size-52)}.select-wrap.svelte-8hqvb6>option.svelte-8hqvb6{padding:.25rem .5rem;border-bottom:1px solid var(--border-color-accent);padding-right:var(--size-8);text-overflow:ellipsis;overflow:hidden}.select-wrap.svelte-8hqvb6>option.svelte-8hqvb6:hover{background-color:var(--color-accent)}.select-wrap.svelte-8hqvb6>option.svelte-8hqvb6:last-child{border:none}.inset-icon.svelte-8hqvb6.svelte-8hqvb6{position:absolute;top:5px;right:-6.5px;width:var(--size-10);height:var(--size-5);opacity:.8}@media (--screen-md){.wrap.svelte-8hqvb6.svelte-8hqvb6{font-size:var(--text-lg)}}div.svelte-1g74h68{display:flex;position:absolute;top:var(--size-2);right:var(--size-2);justify-content:flex-end;gap:var(--spacing-sm);z-index:var(--layer-5)}.image-frame.svelte-xgcoa0 img{width:var(--size-full);height:var(--size-full);object-fit:cover}.image-frame.svelte-xgcoa0{object-fit:cover;width:100%;height:100%}.upload-container.svelte-xgcoa0{height:100%;flex-shrink:1;max-height:100%}.image-container.svelte-xgcoa0{display:flex;height:100%;flex-direction:column;justify-content:center;align-items:center;max-height:100%}.selectable.svelte-xgcoa0{cursor:crosshair}input.svelte-16l8u73{display:block;position:relative;background:var(--background-fill-primary);line-height:var(--line-sm)}svg.svelte-43sxxs.svelte-43sxxs{width:var(--size-20);height:var(--size-20)}svg.svelte-43sxxs path.svelte-43sxxs{fill:var(--loader-color)}div.svelte-43sxxs.svelte-43sxxs{z-index:var(--layer-2)}.margin.svelte-43sxxs.svelte-43sxxs{margin:var(--size-4)}.wrap.svelte-1yserjw.svelte-1yserjw{display:flex;flex-direction:column;justify-content:center;align-items:center;z-index:var(--layer-top);transition:opacity .1s ease-in-out;border-radius:var(--block-radius);background:var(--block-background-fill);padding:0 var(--size-6);max-height:var(--size-screen-h);overflow:hidden;pointer-events:none}.wrap.center.svelte-1yserjw.svelte-1yserjw{top:0;right:0;left:0}.wrap.default.svelte-1yserjw.svelte-1yserjw{top:0;right:0;bottom:0;left:0}.hide.svelte-1yserjw.svelte-1yserjw{opacity:0;pointer-events:none}.generating.svelte-1yserjw.svelte-1yserjw{animation:svelte-1yserjw-pulse 2s cubic-bezier(.4,0,.6,1) infinite;border:2px solid var(--color-accent);background:transparent;z-index:var(--layer-1)}.translucent.svelte-1yserjw.svelte-1yserjw{background:none}@keyframes svelte-1yserjw-pulse{0%,to{opacity:1}50%{opacity:.5}}.loading.svelte-1yserjw.svelte-1yserjw{z-index:var(--layer-2);color:var(--body-text-color)}.eta-bar.svelte-1yserjw.svelte-1yserjw{position:absolute;top:0;right:0;bottom:0;left:0;transform-origin:left;opacity:.8;z-index:var(--layer-1);transition:10ms;background:var(--background-fill-secondary)}.progress-bar-wrap.svelte-1yserjw.svelte-1yserjw{border:1px solid var(--border-color-primary);background:var(--background-fill-primary);width:55.5%;height:var(--size-4)}.progress-bar.svelte-1yserjw.svelte-1yserjw{transform-origin:left;background-color:var(--loader-color);width:var(--size-full);height:var(--size-full)}.progress-level.svelte-1yserjw.svelte-1yserjw{display:flex;flex-direction:column;align-items:center;gap:1;z-index:var(--layer-2);width:var(--size-full)}.progress-level-inner.svelte-1yserjw.svelte-1yserjw{margin:var(--size-2) auto;color:var(--body-text-color);font-size:var(--text-sm);font-family:var(--font-mono)}.meta-text.svelte-1yserjw.svelte-1yserjw{position:absolute;top:0;right:0;z-index:var(--layer-2);padding:var(--size-1) var(--size-2);font-size:var(--text-sm);font-family:var(--font-mono)}.meta-text-center.svelte-1yserjw.svelte-1yserjw{display:flex;position:absolute;top:0;right:0;justify-content:center;align-items:center;transform:translateY(var(--size-6));z-index:var(--layer-2);padding:var(--size-1) var(--size-2);font-size:var(--text-sm);font-family:var(--font-mono);text-align:center}.error.svelte-1yserjw.svelte-1yserjw{box-shadow:var(--shadow-drop);border:solid 1px var(--error-border-color);border-radius:var(--radius-full);background:var(--error-background-fill);padding-right:var(--size-4);padding-left:var(--size-4);color:var(--error-text-color);font-weight:var(--weight-semibold);font-size:var(--text-lg);line-height:var(--line-lg);font-family:var(--font)}.minimal.svelte-1yserjw .progress-text.svelte-1yserjw{background:var(--block-background-fill)}.border.svelte-1yserjw.svelte-1yserjw{border:1px solid var(--border-color-primary)}.toast-body.svelte-solcu7{display:flex;position:relative;right:0;left:0;align-items:center;margin:var(--size-6) var(--size-4);margin:auto;border-radius:var(--container-radius);overflow:hidden;pointer-events:auto}.toast-body.error.svelte-solcu7{border:1px solid var(--color-red-700);background:var(--color-red-50)}.dark .toast-body.error.svelte-solcu7{border:1px solid var(--color-red-500);background-color:var(--color-grey-950)}.toast-body.warning.svelte-solcu7{border:1px solid var(--color-yellow-700);background:var(--color-yellow-50)}.dark .toast-body.warning.svelte-solcu7{border:1px solid var(--color-yellow-500);background-color:var(--color-grey-950)}.toast-body.info.svelte-solcu7{border:1px solid var(--color-grey-700);background:var(--color-grey-50)}.dark .toast-body.info.svelte-solcu7{border:1px solid var(--color-grey-500);background-color:var(--color-grey-950)}.toast-title.svelte-solcu7{display:flex;align-items:center;font-weight:var(--weight-bold);font-size:var(--text-lg);line-height:var(--line-sm);text-transform:capitalize}.toast-title.error.svelte-solcu7{color:var(--color-red-700)}.dark .toast-title.error.svelte-solcu7{color:var(--color-red-50)}.toast-title.warning.svelte-solcu7{color:var(--color-yellow-700)}.dark .toast-title.warning.svelte-solcu7{color:var(--color-yellow-50)}.toast-title.info.svelte-solcu7{color:var(--color-grey-700)}.dark .toast-title.info.svelte-solcu7{color:var(--color-grey-50)}.toast-close.svelte-solcu7{margin:0 var(--size-3);border-radius:var(--size-3);padding:0px var(--size-1-5);font-size:var(--size-5);line-height:var(--size-5)}.toast-close.error.svelte-solcu7{color:var(--color-red-700)}.dark .toast-close.error.svelte-solcu7{color:var(--color-red-500)}.toast-close.warning.svelte-solcu7{color:var(--color-yellow-700)}.dark .toast-close.warning.svelte-solcu7{color:var(--color-yellow-500)}.toast-close.info.svelte-solcu7{color:var(--color-grey-700)}.dark .toast-close.info.svelte-solcu7{color:var(--color-grey-500)}.toast-text.svelte-solcu7{font-size:var(--text-lg)}.toast-text.error.svelte-solcu7{color:var(--color-red-700)}.dark .toast-text.error.svelte-solcu7{color:var(--color-red-50)}.toast-text.warning.svelte-solcu7{color:var(--color-yellow-700)}.dark .toast-text.warning.svelte-solcu7{color:var(--color-yellow-50)}.toast-text.info.svelte-solcu7{color:var(--color-grey-700)}.dark .toast-text.info.svelte-solcu7{color:var(--color-grey-50)}.toast-details.svelte-solcu7{margin:var(--size-3) var(--size-3) var(--size-3) 0;width:100%}.toast-icon.svelte-solcu7{display:flex;position:absolute;position:relative;flex-shrink:0;justify-content:center;align-items:center;margin:var(--size-2);border-radius:var(--radius-full);padding:var(--size-1);padding-left:calc(var(--size-1) - 1px);width:35px;height:35px}.toast-icon.error.svelte-solcu7{color:var(--color-red-700)}.dark .toast-icon.error.svelte-solcu7{color:var(--color-red-500)}.toast-icon.warning.svelte-solcu7{color:var(--color-yellow-700)}.dark .toast-icon.warning.svelte-solcu7{color:var(--color-yellow-500)}.toast-icon.info.svelte-solcu7{color:var(--color-grey-700)}.dark .toast-icon.info.svelte-solcu7{color:var(--color-grey-500)}@keyframes svelte-solcu7-countdown{0%{transform:scaleX(1)}to{transform:scaleX(0)}}.timer.svelte-solcu7{position:absolute;bottom:0;left:0;transform-origin:0 0;animation:svelte-solcu7-countdown 10s linear forwards;width:100%;height:var(--size-1)}.timer.error.svelte-solcu7{background:var(--color-red-700)}.dark .timer.error.svelte-solcu7{background:var(--color-red-500)}.timer.warning.svelte-solcu7{background:var(--color-yellow-700)}.dark .timer.warning.svelte-solcu7{background:var(--color-yellow-500)}.timer.info.svelte-solcu7{background:var(--color-grey-700)}.dark .timer.info.svelte-solcu7{background:var(--color-grey-500)}.toast-wrap.svelte-gatr8h{display:flex;position:fixed;top:var(--size-4);right:var(--size-4);flex-direction:column;align-items:end;gap:var(--size-2);z-index:var(--layer-top);width:calc(100% - var(--size-8))}@media (--screen-sm){.toast-wrap.svelte-gatr8h{width:calc(var(--size-96) + var(--size-10))}}div.svelte-1vvnm05{width:var(--size-10);height:var(--size-10)}.table.svelte-1vvnm05{margin:0 auto}button.svelte-8huxfn,a.svelte-8huxfn{display:inline-flex;justify-content:center;align-items:center;transition:var(--button-transition);box-shadow:var(--button-shadow);padding:var(--size-0-5) var(--size-2);text-align:center}button.svelte-8huxfn:hover,button[disabled].svelte-8huxfn,a.svelte-8huxfn:hover,a.disabled.svelte-8huxfn{box-shadow:var(--button-shadow-hover)}button.svelte-8huxfn:active,a.svelte-8huxfn:active{box-shadow:var(--button-shadow-active)}button[disabled].svelte-8huxfn,a.disabled.svelte-8huxfn{opacity:.5;filter:grayscale(30%);cursor:not-allowed}.hidden.svelte-8huxfn{display:none}.primary.svelte-8huxfn{border:var(--button-border-width) solid var(--button-primary-border-color);background:var(--button-primary-background-fill);color:var(--button-primary-text-color)}.primary.svelte-8huxfn:hover,.primary[disabled].svelte-8huxfn{border-color:var(--button-primary-border-color-hover);background:var(--button-primary-background-fill-hover);color:var(--button-primary-text-color-hover)}.secondary.svelte-8huxfn{border:var(--button-border-width) solid var(--button-secondary-border-color);background:var(--button-secondary-background-fill);color:var(--button-secondary-text-color)}.secondary.svelte-8huxfn:hover,.secondary[disabled].svelte-8huxfn{border-color:var(--button-secondary-border-color-hover);background:var(--button-secondary-background-fill-hover);color:var(--button-secondary-text-color-hover)}.stop.svelte-8huxfn{border:var(--button-border-width) solid var(--button-cancel-border-color);background:var(--button-cancel-background-fill);color:var(--button-cancel-text-color)}.stop.svelte-8huxfn:hover,.stop[disabled].svelte-8huxfn{border-color:var(--button-cancel-border-color-hover);background:var(--button-cancel-background-fill-hover);color:var(--button-cancel-text-color-hover)}.sm.svelte-8huxfn{border-radius:var(--button-small-radius);padding:var(--button-small-padding);font-weight:var(--button-small-text-weight);font-size:var(--button-small-text-size)}.lg.svelte-8huxfn{border-radius:var(--button-large-radius);padding:var(--button-large-padding);font-weight:var(--button-large-text-weight);font-size:var(--button-large-text-size)}.button-icon.svelte-8huxfn{width:var(--text-xl);height:var(--text-xl);margin-right:var(--spacing-xl)}.options.svelte-yuohum{--window-padding:var(--size-8);position:fixed;z-index:var(--layer-top);margin-left:0;box-shadow:var(--shadow-drop-lg);border-radius:var(--container-radius);background:var(--background-fill-primary);min-width:fit-content;max-width:inherit;overflow:auto;color:var(--body-text-color);list-style:none}.item.svelte-yuohum{display:flex;cursor:pointer;padding:var(--size-2)}.item.svelte-yuohum:hover,.active.svelte-yuohum{background:var(--background-fill-secondary)}.inner-item.svelte-yuohum{padding-right:var(--size-1)}.hide.svelte-yuohum{visibility:hidden}.icon-wrap.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{color:var(--body-text-color);margin-right:var(--size-2);width:var(--size-5)}label.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg:not(.container),label.svelte-xtjjyg:not(.container) .wrap.svelte-xtjjyg.svelte-xtjjyg,label.svelte-xtjjyg:not(.container) .wrap-inner.svelte-xtjjyg.svelte-xtjjyg,label.svelte-xtjjyg:not(.container) .secondary-wrap.svelte-xtjjyg.svelte-xtjjyg,label.svelte-xtjjyg:not(.container) .token.svelte-xtjjyg.svelte-xtjjyg,label.svelte-xtjjyg:not(.container) input.svelte-xtjjyg.svelte-xtjjyg{height:100%}.container.svelte-xtjjyg .wrap.svelte-xtjjyg.svelte-xtjjyg{box-shadow:var(--input-shadow);border:var(--input-border-width) solid var(--border-color-primary)}.wrap.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{position:relative;border-radius:var(--input-radius);background:var(--input-background-fill)}.wrap.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg:focus-within{box-shadow:var(--input-shadow-focus);border-color:var(--input-border-color-focus)}.wrap-inner.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{display:flex;position:relative;flex-wrap:wrap;align-items:center;gap:var(--checkbox-label-gap);padding:var(--checkbox-label-padding)}.token.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{display:flex;align-items:center;transition:var(--button-transition);cursor:pointer;box-shadow:var(--checkbox-label-shadow);border:var(--checkbox-label-border-width) solid var(--checkbox-label-border-color);border-radius:var(--button-small-radius);background:var(--checkbox-label-background-fill);padding:var(--checkbox-label-padding);color:var(--checkbox-label-text-color);font-weight:var(--checkbox-label-text-weight);font-size:var(--checkbox-label-text-size);line-height:var(--line-md)}.token.svelte-xtjjyg>.svelte-xtjjyg+.svelte-xtjjyg{margin-left:var(--size-2)}.token-remove.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{fill:var(--body-text-color);display:flex;justify-content:center;align-items:center;cursor:pointer;border:var(--checkbox-border-width) solid var(--border-color-primary);border-radius:var(--radius-full);background:var(--background-fill-primary);padding:var(--size-0-5);width:16px;height:16px}.secondary-wrap.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{display:flex;flex:1 1 0%;align-items:center;border:none;min-width:min-content}input.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{margin:var(--spacing-sm);outline:none;border:none;background:inherit;width:var(--size-full);color:var(--body-text-color);font-size:var(--input-text-size)}input.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg:disabled{-webkit-text-fill-color:var(--body-text-color);-webkit-opacity:1;opacity:1;cursor:not-allowed}.remove-all.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{margin-left:var(--size-1);width:20px;height:20px}.subdued.svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{color:var(--body-text-color-subdued)}input[readonly].svelte-xtjjyg.svelte-xtjjyg.svelte-xtjjyg{cursor:pointer}.icon-wrap.svelte-1m1zvyj.svelte-1m1zvyj{color:var(--body-text-color);margin-right:var(--size-2);width:var(--size-5)}.container.svelte-1m1zvyj.svelte-1m1zvyj{height:100%}.container.svelte-1m1zvyj .wrap.svelte-1m1zvyj{box-shadow:var(--input-shadow);border:var(--input-border-width) solid var(--border-color-primary)}.wrap.svelte-1m1zvyj.svelte-1m1zvyj{position:relative;border-radius:var(--input-radius);background:var(--input-background-fill)}.wrap.svelte-1m1zvyj.svelte-1m1zvyj:focus-within{box-shadow:var(--input-shadow-focus);border-color:var(--input-border-color-focus)}.wrap-inner.svelte-1m1zvyj.svelte-1m1zvyj{display:flex;position:relative;flex-wrap:wrap;align-items:center;gap:var(--checkbox-label-gap);padding:var(--checkbox-label-padding);height:100%}.secondary-wrap.svelte-1m1zvyj.svelte-1m1zvyj{display:flex;flex:1 1 0%;align-items:center;border:none;min-width:min-content;height:100%}input.svelte-1m1zvyj.svelte-1m1zvyj{margin:var(--spacing-sm);outline:none;border:none;background:inherit;width:var(--size-full);color:var(--body-text-color);font-size:var(--input-text-size);height:100%}input.svelte-1m1zvyj.svelte-1m1zvyj:disabled{-webkit-text-fill-color:var(--body-text-color);-webkit-opacity:1;opacity:1;cursor:not-allowed}.subdued.svelte-1m1zvyj.svelte-1m1zvyj{color:var(--body-text-color-subdued)}input[readonly].svelte-1m1zvyj.svelte-1m1zvyj{cursor:pointer}.gallery.svelte-1gecy8w{padding:var(--size-1) var(--size-2)}.modal.svelte-hkn2q1{position:fixed;left:0;top:0;width:100%;height:100%;z-index:var(--layer-top);-webkit-backdrop-filter:blur(4px);backdrop-filter:blur(4px)}.modal-container.svelte-hkn2q1{border-style:solid;border-width:var(--block-border-width);margin-top:10%;padding:20px;box-shadow:var(--block-shadow);border-color:var(--block-border-color);border-radius:var(--block-radius);background:var(--block-background-fill);position:fixed;left:50%;transform:translate(-50%);width:fit-content}.model-content.svelte-hkn2q1{display:flex;align-items:flex-end}.canvas-annotator.svelte-1m8vz1h{border-color:var(--block-border-color);width:100%;height:100%;display:block;touch-action:none}.canvas-control.svelte-1m8vz1h{display:flex;align-items:center;justify-content:center;border-top:1px solid var(--border-color-primary);width:95%;bottom:0;left:0;right:0;margin-left:auto;margin-right:auto;margin-top:var(--size-2)}.icon.svelte-1m8vz1h{width:22px;height:22px;margin:var(--spacing-lg) var(--spacing-xs);padding:var(--spacing-xs);color:var(--neutral-400);border-radius:var(--radius-md)}.icon.svelte-1m8vz1h:hover,.icon.svelte-1m8vz1h:focus{color:var(--color-accent)}.selected.svelte-1m8vz1h{color:var(--color-accent)}.canvas-container.svelte-1m8vz1h{display:flex;justify-content:center;align-items:center}.canvas-container.svelte-1m8vz1h:focus{outline:none}.image-frame.svelte-1gjdske img{width:var(--size-full);height:var(--size-full);object-fit:cover}.image-frame.svelte-1gjdske{object-fit:cover;width:100%}.upload-container.svelte-1gjdske{height:100%;width:100%;flex-shrink:1;max-height:100%}.image-container.svelte-1gjdske{display:flex;height:100%;flex-direction:column;justify-content:center;align-items:center;max-height:100%}.selectable.svelte-1gjdske{cursor:crosshair}.icon-buttons.svelte-1gjdske{display:flex;position:absolute;top:6px;right:6px;gap:var(--size-1)}.container.svelte-1sgcyba img{width:100%;height:100%}.container.selected.svelte-1sgcyba{border-color:var(--border-color-accent)}.border.table.svelte-1sgcyba{border:2px solid var(--border-color-primary)}.container.table.svelte-1sgcyba{margin:0 auto;border-radius:var(--radius-lg);overflow:hidden;width:var(--size-20);height:var(--size-20);object-fit:cover}.container.gallery.svelte-1sgcyba{width:var(--size-20);max-width:var(--size-20);object-fit:cover}
|
src/backend/gradio_image_annotation/templates/component/wrapper-6f348d45-19fa94bf.js
ADDED
@@ -0,0 +1,2453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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/component/wrapper-6f348d45-DjpFDl6n.js
ADDED
@@ -0,0 +1,2453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import S from "./__vite-browser-external-DYxpcVy9.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/component/wrapper-6f348d45-f837cf34.js
ADDED
@@ -0,0 +1,2455 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
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, 53 - 32) - 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 |
+
const r = "WebSocket was closed before the connection was established";
|
1709 |
+
b(this, this._req, r);
|
1710 |
+
return;
|
1711 |
+
}
|
1712 |
+
if (this.readyState === d.CLOSING) {
|
1713 |
+
this._closeFrameSent && (this._closeFrameReceived || this._receiver._writableState.errorEmitted) && this._socket.end();
|
1714 |
+
return;
|
1715 |
+
}
|
1716 |
+
this._readyState = d.CLOSING, this._sender.close(e, t, !this._isServer, (r) => {
|
1717 |
+
r || (this._closeFrameSent = !0, (this._closeFrameReceived || this._receiver._writableState.errorEmitted) && this._socket.end());
|
1718 |
+
}), this._closeTimer = setTimeout(
|
1719 |
+
this._socket.destroy.bind(this._socket),
|
1720 |
+
Ss
|
1721 |
+
);
|
1722 |
+
}
|
1723 |
+
}
|
1724 |
+
/**
|
1725 |
+
* Pause the socket.
|
1726 |
+
*
|
1727 |
+
* @public
|
1728 |
+
*/
|
1729 |
+
pause() {
|
1730 |
+
this.readyState === d.CONNECTING || this.readyState === d.CLOSED || (this._paused = !0, this._socket.pause());
|
1731 |
+
}
|
1732 |
+
/**
|
1733 |
+
* Send a ping.
|
1734 |
+
*
|
1735 |
+
* @param {*} [data] The data to send
|
1736 |
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
1737 |
+
* @param {Function} [cb] Callback which is executed when the ping is sent
|
1738 |
+
* @public
|
1739 |
+
*/
|
1740 |
+
ping(e, t, r) {
|
1741 |
+
if (this.readyState === d.CONNECTING)
|
1742 |
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
1743 |
+
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) {
|
1744 |
+
ve(this, e, r);
|
1745 |
+
return;
|
1746 |
+
}
|
1747 |
+
t === void 0 && (t = !this._isServer), this._sender.ping(e || Q, t, r);
|
1748 |
+
}
|
1749 |
+
/**
|
1750 |
+
* Send a pong.
|
1751 |
+
*
|
1752 |
+
* @param {*} [data] The data to send
|
1753 |
+
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
1754 |
+
* @param {Function} [cb] Callback which is executed when the pong is sent
|
1755 |
+
* @public
|
1756 |
+
*/
|
1757 |
+
pong(e, t, r) {
|
1758 |
+
if (this.readyState === d.CONNECTING)
|
1759 |
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
1760 |
+
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) {
|
1761 |
+
ve(this, e, r);
|
1762 |
+
return;
|
1763 |
+
}
|
1764 |
+
t === void 0 && (t = !this._isServer), this._sender.pong(e || Q, t, r);
|
1765 |
+
}
|
1766 |
+
/**
|
1767 |
+
* Resume the socket.
|
1768 |
+
*
|
1769 |
+
* @public
|
1770 |
+
*/
|
1771 |
+
resume() {
|
1772 |
+
this.readyState === d.CONNECTING || this.readyState === d.CLOSED || (this._paused = !1, this._receiver._writableState.needDrain || this._socket.resume());
|
1773 |
+
}
|
1774 |
+
/**
|
1775 |
+
* Send a data message.
|
1776 |
+
*
|
1777 |
+
* @param {*} data The message to send
|
1778 |
+
* @param {Object} [options] Options object
|
1779 |
+
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
1780 |
+
* text
|
1781 |
+
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
1782 |
+
* `data`
|
1783 |
+
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
1784 |
+
* last one
|
1785 |
+
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
1786 |
+
* @param {Function} [cb] Callback which is executed when data is written out
|
1787 |
+
* @public
|
1788 |
+
*/
|
1789 |
+
send(e, t, r) {
|
1790 |
+
if (this.readyState === d.CONNECTING)
|
1791 |
+
throw new Error("WebSocket is not open: readyState 0 (CONNECTING)");
|
1792 |
+
if (typeof t == "function" && (r = t, t = {}), typeof e == "number" && (e = e.toString()), this.readyState !== d.OPEN) {
|
1793 |
+
ve(this, e, r);
|
1794 |
+
return;
|
1795 |
+
}
|
1796 |
+
const i = {
|
1797 |
+
binary: typeof e != "string",
|
1798 |
+
mask: !this._isServer,
|
1799 |
+
compress: !0,
|
1800 |
+
fin: !0,
|
1801 |
+
...t
|
1802 |
+
};
|
1803 |
+
this._extensions[T.extensionName] || (i.compress = !1), this._sender.send(e || Q, i, r);
|
1804 |
+
}
|
1805 |
+
/**
|
1806 |
+
* Forcibly close the connection.
|
1807 |
+
*
|
1808 |
+
* @public
|
1809 |
+
*/
|
1810 |
+
terminate() {
|
1811 |
+
if (this.readyState !== d.CLOSED) {
|
1812 |
+
if (this.readyState === d.CONNECTING) {
|
1813 |
+
const e = "WebSocket was closed before the connection was established";
|
1814 |
+
b(this, this._req, e);
|
1815 |
+
return;
|
1816 |
+
}
|
1817 |
+
this._socket && (this._readyState = d.CLOSING, this._socket.destroy());
|
1818 |
+
}
|
1819 |
+
}
|
1820 |
+
};
|
1821 |
+
Object.defineProperty(m, "CONNECTING", {
|
1822 |
+
enumerable: !0,
|
1823 |
+
value: O.indexOf("CONNECTING")
|
1824 |
+
});
|
1825 |
+
Object.defineProperty(m.prototype, "CONNECTING", {
|
1826 |
+
enumerable: !0,
|
1827 |
+
value: O.indexOf("CONNECTING")
|
1828 |
+
});
|
1829 |
+
Object.defineProperty(m, "OPEN", {
|
1830 |
+
enumerable: !0,
|
1831 |
+
value: O.indexOf("OPEN")
|
1832 |
+
});
|
1833 |
+
Object.defineProperty(m.prototype, "OPEN", {
|
1834 |
+
enumerable: !0,
|
1835 |
+
value: O.indexOf("OPEN")
|
1836 |
+
});
|
1837 |
+
Object.defineProperty(m, "CLOSING", {
|
1838 |
+
enumerable: !0,
|
1839 |
+
value: O.indexOf("CLOSING")
|
1840 |
+
});
|
1841 |
+
Object.defineProperty(m.prototype, "CLOSING", {
|
1842 |
+
enumerable: !0,
|
1843 |
+
value: O.indexOf("CLOSING")
|
1844 |
+
});
|
1845 |
+
Object.defineProperty(m, "CLOSED", {
|
1846 |
+
enumerable: !0,
|
1847 |
+
value: O.indexOf("CLOSED")
|
1848 |
+
});
|
1849 |
+
Object.defineProperty(m.prototype, "CLOSED", {
|
1850 |
+
enumerable: !0,
|
1851 |
+
value: O.indexOf("CLOSED")
|
1852 |
+
});
|
1853 |
+
[
|
1854 |
+
"binaryType",
|
1855 |
+
"bufferedAmount",
|
1856 |
+
"extensions",
|
1857 |
+
"isPaused",
|
1858 |
+
"protocol",
|
1859 |
+
"readyState",
|
1860 |
+
"url"
|
1861 |
+
].forEach((s) => {
|
1862 |
+
Object.defineProperty(m.prototype, s, { enumerable: !0 });
|
1863 |
+
});
|
1864 |
+
["open", "error", "close", "message"].forEach((s) => {
|
1865 |
+
Object.defineProperty(m.prototype, `on${s}`, {
|
1866 |
+
enumerable: !0,
|
1867 |
+
get() {
|
1868 |
+
for (const e of this.listeners(s))
|
1869 |
+
if (e[ge])
|
1870 |
+
return e[ds];
|
1871 |
+
return null;
|
1872 |
+
},
|
1873 |
+
set(e) {
|
1874 |
+
for (const t of this.listeners(s))
|
1875 |
+
if (t[ge]) {
|
1876 |
+
this.removeListener(s, t);
|
1877 |
+
break;
|
1878 |
+
}
|
1879 |
+
typeof e == "function" && this.addEventListener(s, e, {
|
1880 |
+
[ge]: !0
|
1881 |
+
});
|
1882 |
+
}
|
1883 |
+
});
|
1884 |
+
});
|
1885 |
+
m.prototype.addEventListener = ps;
|
1886 |
+
m.prototype.removeEventListener = ms;
|
1887 |
+
var ft = m;
|
1888 |
+
function ht(s, e, t, r) {
|
1889 |
+
const i = {
|
1890 |
+
protocolVersion: ye[1],
|
1891 |
+
maxPayload: 104857600,
|
1892 |
+
skipUTF8Validation: !1,
|
1893 |
+
perMessageDeflate: !0,
|
1894 |
+
followRedirects: !1,
|
1895 |
+
maxRedirects: 10,
|
1896 |
+
...r,
|
1897 |
+
createConnection: void 0,
|
1898 |
+
socketPath: void 0,
|
1899 |
+
hostname: void 0,
|
1900 |
+
protocol: void 0,
|
1901 |
+
timeout: void 0,
|
1902 |
+
method: "GET",
|
1903 |
+
host: void 0,
|
1904 |
+
path: void 0,
|
1905 |
+
port: void 0
|
1906 |
+
};
|
1907 |
+
if (!ye.includes(i.protocolVersion))
|
1908 |
+
throw new RangeError(
|
1909 |
+
`Unsupported protocol version: ${i.protocolVersion} (supported versions: ${ye.join(", ")})`
|
1910 |
+
);
|
1911 |
+
let n;
|
1912 |
+
if (e instanceof me)
|
1913 |
+
n = e, s._url = e.href;
|
1914 |
+
else {
|
1915 |
+
try {
|
1916 |
+
n = new me(e);
|
1917 |
+
} catch {
|
1918 |
+
throw new SyntaxError(`Invalid URL: ${e}`);
|
1919 |
+
}
|
1920 |
+
s._url = e;
|
1921 |
+
}
|
1922 |
+
const o = n.protocol === "wss:", l = n.protocol === "ws+unix:";
|
1923 |
+
let f;
|
1924 |
+
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) {
|
1925 |
+
const u = new SyntaxError(f);
|
1926 |
+
if (s._redirects === 0)
|
1927 |
+
throw u;
|
1928 |
+
ee(s, u);
|
1929 |
+
return;
|
1930 |
+
}
|
1931 |
+
const a = o ? 443 : 80, c = ls(16).toString("base64"), h = o ? ns.request : os.request, p = /* @__PURE__ */ new Set();
|
1932 |
+
let v;
|
1933 |
+
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 = {
|
1934 |
+
...i.headers,
|
1935 |
+
"Sec-WebSocket-Version": i.protocolVersion,
|
1936 |
+
"Sec-WebSocket-Key": c,
|
1937 |
+
Connection: "Upgrade",
|
1938 |
+
Upgrade: "websocket"
|
1939 |
+
}, i.path = n.pathname + n.search, i.timeout = i.handshakeTimeout, i.perMessageDeflate && (v = new T(
|
1940 |
+
i.perMessageDeflate !== !0 ? i.perMessageDeflate : {},
|
1941 |
+
!1,
|
1942 |
+
i.maxPayload
|
1943 |
+
), i.headers["Sec-WebSocket-Extensions"] = gs({
|
1944 |
+
[T.extensionName]: v.offer()
|
1945 |
+
})), t.length) {
|
1946 |
+
for (const u of t) {
|
1947 |
+
if (typeof u != "string" || !Es.test(u) || p.has(u))
|
1948 |
+
throw new SyntaxError(
|
1949 |
+
"An invalid or duplicated subprotocol was specified"
|
1950 |
+
);
|
1951 |
+
p.add(u);
|
1952 |
+
}
|
1953 |
+
i.headers["Sec-WebSocket-Protocol"] = t.join(",");
|
1954 |
+
}
|
1955 |
+
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) {
|
1956 |
+
const u = i.path.split(":");
|
1957 |
+
i.socketPath = u[0], i.path = u[1];
|
1958 |
+
}
|
1959 |
+
let _;
|
1960 |
+
if (i.followRedirects) {
|
1961 |
+
if (s._redirects === 0) {
|
1962 |
+
s._originalIpc = l, s._originalSecure = o, s._originalHostOrSocketPath = l ? i.socketPath : n.host;
|
1963 |
+
const u = r && r.headers;
|
1964 |
+
if (r = { ...r, headers: {} }, u)
|
1965 |
+
for (const [E, $] of Object.entries(u))
|
1966 |
+
r.headers[E.toLowerCase()] = $;
|
1967 |
+
} else if (s.listenerCount("redirect") === 0) {
|
1968 |
+
const u = l ? s._originalIpc ? i.socketPath === s._originalHostOrSocketPath : !1 : s._originalIpc ? !1 : n.host === s._originalHostOrSocketPath;
|
1969 |
+
(!u || s._originalSecure && !o) && (delete i.headers.authorization, delete i.headers.cookie, u || delete i.headers.host, i.auth = void 0);
|
1970 |
+
}
|
1971 |
+
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, _);
|
1972 |
+
} else
|
1973 |
+
_ = s._req = h(i);
|
1974 |
+
i.timeout && _.on("timeout", () => {
|
1975 |
+
b(s, _, "Opening handshake has timed out");
|
1976 |
+
}), _.on("error", (u) => {
|
1977 |
+
_ === null || _[lt] || (_ = s._req = null, ee(s, u));
|
1978 |
+
}), _.on("response", (u) => {
|
1979 |
+
const E = u.headers.location, $ = u.statusCode;
|
1980 |
+
if (E && i.followRedirects && $ >= 300 && $ < 400) {
|
1981 |
+
if (++s._redirects > i.maxRedirects) {
|
1982 |
+
b(s, _, "Maximum redirects exceeded");
|
1983 |
+
return;
|
1984 |
+
}
|
1985 |
+
_.abort();
|
1986 |
+
let q;
|
1987 |
+
try {
|
1988 |
+
q = new me(E, e);
|
1989 |
+
} catch {
|
1990 |
+
const L = new SyntaxError(`Invalid URL: ${E}`);
|
1991 |
+
ee(s, L);
|
1992 |
+
return;
|
1993 |
+
}
|
1994 |
+
ht(s, q, t, r);
|
1995 |
+
} else
|
1996 |
+
s.emit("unexpected-response", _, u) || b(
|
1997 |
+
s,
|
1998 |
+
_,
|
1999 |
+
`Unexpected server response: ${u.statusCode}`
|
2000 |
+
);
|
2001 |
+
}), _.on("upgrade", (u, E, $) => {
|
2002 |
+
if (s.emit("upgrade", u), s.readyState !== m.CONNECTING)
|
2003 |
+
return;
|
2004 |
+
if (_ = s._req = null, u.headers.upgrade.toLowerCase() !== "websocket") {
|
2005 |
+
b(s, E, "Invalid Upgrade header");
|
2006 |
+
return;
|
2007 |
+
}
|
2008 |
+
const q = fs("sha1").update(c + us).digest("base64");
|
2009 |
+
if (u.headers["sec-websocket-accept"] !== q) {
|
2010 |
+
b(s, E, "Invalid Sec-WebSocket-Accept header");
|
2011 |
+
return;
|
2012 |
+
}
|
2013 |
+
const D = u.headers["sec-websocket-protocol"];
|
2014 |
+
let L;
|
2015 |
+
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) {
|
2016 |
+
b(s, E, L);
|
2017 |
+
return;
|
2018 |
+
}
|
2019 |
+
D && (s._protocol = D);
|
2020 |
+
const ke = u.headers["sec-websocket-extensions"];
|
2021 |
+
if (ke !== void 0) {
|
2022 |
+
if (!v) {
|
2023 |
+
b(s, E, "Server sent a Sec-WebSocket-Extensions header but no extension was requested");
|
2024 |
+
return;
|
2025 |
+
}
|
2026 |
+
let he;
|
2027 |
+
try {
|
2028 |
+
he = ys(ke);
|
2029 |
+
} catch {
|
2030 |
+
b(s, E, "Invalid Sec-WebSocket-Extensions header");
|
2031 |
+
return;
|
2032 |
+
}
|
2033 |
+
const we = Object.keys(he);
|
2034 |
+
if (we.length !== 1 || we[0] !== T.extensionName) {
|
2035 |
+
b(s, E, "Server indicated an extension that was not requested");
|
2036 |
+
return;
|
2037 |
+
}
|
2038 |
+
try {
|
2039 |
+
v.accept(he[T.extensionName]);
|
2040 |
+
} catch {
|
2041 |
+
b(s, E, "Invalid Sec-WebSocket-Extensions header");
|
2042 |
+
return;
|
2043 |
+
}
|
2044 |
+
s._extensions[T.extensionName] = v;
|
2045 |
+
}
|
2046 |
+
s.setSocket(E, $, {
|
2047 |
+
generateMask: i.generateMask,
|
2048 |
+
maxPayload: i.maxPayload,
|
2049 |
+
skipUTF8Validation: i.skipUTF8Validation
|
2050 |
+
});
|
2051 |
+
}), i.finishRequest ? i.finishRequest(_, s) : _.end();
|
2052 |
+
}
|
2053 |
+
function ee(s, e) {
|
2054 |
+
s._readyState = m.CLOSING, s.emit("error", e), s.emitClose();
|
2055 |
+
}
|
2056 |
+
function bs(s) {
|
2057 |
+
return s.path = s.socketPath, ot.connect(s);
|
2058 |
+
}
|
2059 |
+
function xs(s) {
|
2060 |
+
return s.path = void 0, !s.servername && s.servername !== "" && (s.servername = ot.isIP(s.host) ? "" : s.host), as.connect(s);
|
2061 |
+
}
|
2062 |
+
function b(s, e, t) {
|
2063 |
+
s._readyState = m.CLOSING;
|
2064 |
+
const r = new Error(t);
|
2065 |
+
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)));
|
2066 |
+
}
|
2067 |
+
function ve(s, e, t) {
|
2068 |
+
if (e) {
|
2069 |
+
const r = vs(e).length;
|
2070 |
+
s._socket ? s._sender._bufferedBytes += r : s._bufferedAmount += r;
|
2071 |
+
}
|
2072 |
+
if (t) {
|
2073 |
+
const r = new Error(
|
2074 |
+
`WebSocket is not open: readyState ${s.readyState} (${O[s.readyState]})`
|
2075 |
+
);
|
2076 |
+
process.nextTick(t, r);
|
2077 |
+
}
|
2078 |
+
}
|
2079 |
+
function ks(s, e) {
|
2080 |
+
const t = this[y];
|
2081 |
+
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));
|
2082 |
+
}
|
2083 |
+
function ws() {
|
2084 |
+
const s = this[y];
|
2085 |
+
s.isPaused || s._socket.resume();
|
2086 |
+
}
|
2087 |
+
function Os(s) {
|
2088 |
+
const e = this[y];
|
2089 |
+
e._socket[y] !== void 0 && (e._socket.removeListener("data", fe), process.nextTick(ct, e._socket), e.close(s[_s])), e.emit("error", s);
|
2090 |
+
}
|
2091 |
+
function Ye() {
|
2092 |
+
this[y].emitClose();
|
2093 |
+
}
|
2094 |
+
function Cs(s, e) {
|
2095 |
+
this[y].emit("message", s, e);
|
2096 |
+
}
|
2097 |
+
function Ts(s) {
|
2098 |
+
const e = this[y];
|
2099 |
+
e.pong(s, !e._isServer, at), e.emit("ping", s);
|
2100 |
+
}
|
2101 |
+
function Ls(s) {
|
2102 |
+
this[y].emit("pong", s);
|
2103 |
+
}
|
2104 |
+
function ct(s) {
|
2105 |
+
s.resume();
|
2106 |
+
}
|
2107 |
+
function ut() {
|
2108 |
+
const s = this[y];
|
2109 |
+
this.removeListener("close", ut), this.removeListener("data", fe), this.removeListener("end", dt), s._readyState = m.CLOSING;
|
2110 |
+
let e;
|
2111 |
+
!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));
|
2112 |
+
}
|
2113 |
+
function fe(s) {
|
2114 |
+
this[y]._receiver.write(s) || this.pause();
|
2115 |
+
}
|
2116 |
+
function dt() {
|
2117 |
+
const s = this[y];
|
2118 |
+
s._readyState = m.CLOSING, s._receiver.end(), this.end();
|
2119 |
+
}
|
2120 |
+
function _t() {
|
2121 |
+
const s = this[y];
|
2122 |
+
this.removeListener("error", _t), this.on("error", at), s && (s._readyState = m.CLOSING, this.destroy());
|
2123 |
+
}
|
2124 |
+
const Xs = /* @__PURE__ */ z(ft), { tokenChars: Ns } = ae;
|
2125 |
+
function Ps(s) {
|
2126 |
+
const e = /* @__PURE__ */ new Set();
|
2127 |
+
let t = -1, r = -1, i = 0;
|
2128 |
+
for (i; i < s.length; i++) {
|
2129 |
+
const o = s.charCodeAt(i);
|
2130 |
+
if (r === -1 && Ns[o] === 1)
|
2131 |
+
t === -1 && (t = i);
|
2132 |
+
else if (i !== 0 && (o === 32 || o === 9))
|
2133 |
+
r === -1 && t !== -1 && (r = i);
|
2134 |
+
else if (o === 44) {
|
2135 |
+
if (t === -1)
|
2136 |
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
2137 |
+
r === -1 && (r = i);
|
2138 |
+
const l = s.slice(t, r);
|
2139 |
+
if (e.has(l))
|
2140 |
+
throw new SyntaxError(`The "${l}" subprotocol is duplicated`);
|
2141 |
+
e.add(l), t = r = -1;
|
2142 |
+
} else
|
2143 |
+
throw new SyntaxError(`Unexpected character at index ${i}`);
|
2144 |
+
}
|
2145 |
+
if (t === -1 || r !== -1)
|
2146 |
+
throw new SyntaxError("Unexpected end of input");
|
2147 |
+
const n = s.slice(t, i);
|
2148 |
+
if (e.has(n))
|
2149 |
+
throw new SyntaxError(`The "${n}" subprotocol is duplicated`);
|
2150 |
+
return e.add(n), e;
|
2151 |
+
}
|
2152 |
+
var Rs = { parse: Ps };
|
2153 |
+
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;
|
2154 |
+
class As extends Us {
|
2155 |
+
/**
|
2156 |
+
* Create a `WebSocketServer` instance.
|
2157 |
+
*
|
2158 |
+
* @param {Object} options Configuration options
|
2159 |
+
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
2160 |
+
* pending connections
|
2161 |
+
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
2162 |
+
* track clients
|
2163 |
+
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
2164 |
+
* @param {String} [options.host] The hostname where to bind the server
|
2165 |
+
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
2166 |
+
* size
|
2167 |
+
* @param {Boolean} [options.noServer=false] Enable no server mode
|
2168 |
+
* @param {String} [options.path] Accept only connections matching this path
|
2169 |
+
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
2170 |
+
* permessage-deflate
|
2171 |
+
* @param {Number} [options.port] The port where to bind the server
|
2172 |
+
* @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
|
2173 |
+
* server to use
|
2174 |
+
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
2175 |
+
* not to skip UTF-8 validation for text and close messages
|
2176 |
+
* @param {Function} [options.verifyClient] A hook to reject connections
|
2177 |
+
* @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
|
2178 |
+
* class to use. It must be the `WebSocket` class or class that extends it
|
2179 |
+
* @param {Function} [callback] A listener for the `listening` event
|
2180 |
+
*/
|
2181 |
+
constructor(e, t) {
|
2182 |
+
if (super(), e = {
|
2183 |
+
maxPayload: 100 * 1024 * 1024,
|
2184 |
+
skipUTF8Validation: !1,
|
2185 |
+
perMessageDeflate: !1,
|
2186 |
+
handleProtocols: null,
|
2187 |
+
clientTracking: !0,
|
2188 |
+
verifyClient: null,
|
2189 |
+
noServer: !1,
|
2190 |
+
backlog: null,
|
2191 |
+
// use default (511 as implemented in net.js)
|
2192 |
+
server: null,
|
2193 |
+
host: null,
|
2194 |
+
path: null,
|
2195 |
+
port: null,
|
2196 |
+
WebSocket: Ms,
|
2197 |
+
...e
|
2198 |
+
}, e.port == null && !e.server && !e.noServer || e.port != null && (e.server || e.noServer) || e.server && e.noServer)
|
2199 |
+
throw new TypeError(
|
2200 |
+
'One and only one of the "port", "server", or "noServer" options must be specified'
|
2201 |
+
);
|
2202 |
+
if (e.port != null ? (this._server = ie.createServer((r, i) => {
|
2203 |
+
const n = ie.STATUS_CODES[426];
|
2204 |
+
i.writeHead(426, {
|
2205 |
+
"Content-Length": n.length,
|
2206 |
+
"Content-Type": "text/plain"
|
2207 |
+
}), i.end(n);
|
2208 |
+
}), this._server.listen(
|
2209 |
+
e.port,
|
2210 |
+
e.host,
|
2211 |
+
e.backlog,
|
2212 |
+
t
|
2213 |
+
)) : e.server && (this._server = e.server), this._server) {
|
2214 |
+
const r = this.emit.bind(this, "connection");
|
2215 |
+
this._removeListeners = js(this._server, {
|
2216 |
+
listening: this.emit.bind(this, "listening"),
|
2217 |
+
error: this.emit.bind(this, "error"),
|
2218 |
+
upgrade: (i, n, o) => {
|
2219 |
+
this.handleUpgrade(i, n, o, r);
|
2220 |
+
}
|
2221 |
+
});
|
2222 |
+
}
|
2223 |
+
e.perMessageDeflate === !0 && (e.perMessageDeflate = {}), e.clientTracking && (this.clients = /* @__PURE__ */ new Set(), this._shouldEmitClose = !1), this.options = e, this._state = Ke;
|
2224 |
+
}
|
2225 |
+
/**
|
2226 |
+
* Returns the bound address, the address family name, and port of the server
|
2227 |
+
* as reported by the operating system if listening on an IP socket.
|
2228 |
+
* If the server is listening on a pipe or UNIX domain socket, the name is
|
2229 |
+
* returned as a string.
|
2230 |
+
*
|
2231 |
+
* @return {(Object|String|null)} The address of the server
|
2232 |
+
* @public
|
2233 |
+
*/
|
2234 |
+
address() {
|
2235 |
+
if (this.options.noServer)
|
2236 |
+
throw new Error('The server is operating in "noServer" mode');
|
2237 |
+
return this._server ? this._server.address() : null;
|
2238 |
+
}
|
2239 |
+
/**
|
2240 |
+
* Stop the server from accepting new connections and emit the `'close'` event
|
2241 |
+
* when all existing connections are closed.
|
2242 |
+
*
|
2243 |
+
* @param {Function} [cb] A one-time listener for the `'close'` event
|
2244 |
+
* @public
|
2245 |
+
*/
|
2246 |
+
close(e) {
|
2247 |
+
if (this._state === pt) {
|
2248 |
+
e && this.once("close", () => {
|
2249 |
+
e(new Error("The server is not running"));
|
2250 |
+
}), process.nextTick(G, this);
|
2251 |
+
return;
|
2252 |
+
}
|
2253 |
+
if (e && this.once("close", e), this._state !== Xe)
|
2254 |
+
if (this._state = Xe, this.options.noServer || this.options.server)
|
2255 |
+
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);
|
2256 |
+
else {
|
2257 |
+
const t = this._server;
|
2258 |
+
this._removeListeners(), this._removeListeners = this._server = null, t.close(() => {
|
2259 |
+
G(this);
|
2260 |
+
});
|
2261 |
+
}
|
2262 |
+
}
|
2263 |
+
/**
|
2264 |
+
* See if a given request should be handled by this server instance.
|
2265 |
+
*
|
2266 |
+
* @param {http.IncomingMessage} req Request object to inspect
|
2267 |
+
* @return {Boolean} `true` if the request is valid, else `false`
|
2268 |
+
* @public
|
2269 |
+
*/
|
2270 |
+
shouldHandle(e) {
|
2271 |
+
if (this.options.path) {
|
2272 |
+
const t = e.url.indexOf("?");
|
2273 |
+
if ((t !== -1 ? e.url.slice(0, t) : e.url) !== this.options.path)
|
2274 |
+
return !1;
|
2275 |
+
}
|
2276 |
+
return !0;
|
2277 |
+
}
|
2278 |
+
/**
|
2279 |
+
* Handle a HTTP Upgrade request.
|
2280 |
+
*
|
2281 |
+
* @param {http.IncomingMessage} req The request object
|
2282 |
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
2283 |
+
* server and client
|
2284 |
+
* @param {Buffer} head The first packet of the upgraded stream
|
2285 |
+
* @param {Function} cb Callback
|
2286 |
+
* @public
|
2287 |
+
*/
|
2288 |
+
handleUpgrade(e, t, r, i) {
|
2289 |
+
t.on("error", Ze);
|
2290 |
+
const n = e.headers["sec-websocket-key"], o = +e.headers["sec-websocket-version"];
|
2291 |
+
if (e.method !== "GET") {
|
2292 |
+
R(this, e, t, 405, "Invalid HTTP method");
|
2293 |
+
return;
|
2294 |
+
}
|
2295 |
+
if (e.headers.upgrade.toLowerCase() !== "websocket") {
|
2296 |
+
R(this, e, t, 400, "Invalid Upgrade header");
|
2297 |
+
return;
|
2298 |
+
}
|
2299 |
+
if (!n || !Ws.test(n)) {
|
2300 |
+
R(this, e, t, 400, "Missing or invalid Sec-WebSocket-Key header");
|
2301 |
+
return;
|
2302 |
+
}
|
2303 |
+
if (o !== 8 && o !== 13) {
|
2304 |
+
R(this, e, t, 400, "Missing or invalid Sec-WebSocket-Version header");
|
2305 |
+
return;
|
2306 |
+
}
|
2307 |
+
if (!this.shouldHandle(e)) {
|
2308 |
+
H(t, 400);
|
2309 |
+
return;
|
2310 |
+
}
|
2311 |
+
const l = e.headers["sec-websocket-protocol"];
|
2312 |
+
let f = /* @__PURE__ */ new Set();
|
2313 |
+
if (l !== void 0)
|
2314 |
+
try {
|
2315 |
+
f = $s.parse(l);
|
2316 |
+
} catch {
|
2317 |
+
R(this, e, t, 400, "Invalid Sec-WebSocket-Protocol header");
|
2318 |
+
return;
|
2319 |
+
}
|
2320 |
+
const a = e.headers["sec-websocket-extensions"], c = {};
|
2321 |
+
if (this.options.perMessageDeflate && a !== void 0) {
|
2322 |
+
const h = new N(
|
2323 |
+
this.options.perMessageDeflate,
|
2324 |
+
!0,
|
2325 |
+
this.options.maxPayload
|
2326 |
+
);
|
2327 |
+
try {
|
2328 |
+
const p = qe.parse(a);
|
2329 |
+
p[N.extensionName] && (h.accept(p[N.extensionName]), c[N.extensionName] = h);
|
2330 |
+
} catch {
|
2331 |
+
R(this, e, t, 400, "Invalid or unacceptable Sec-WebSocket-Extensions header");
|
2332 |
+
return;
|
2333 |
+
}
|
2334 |
+
}
|
2335 |
+
if (this.options.verifyClient) {
|
2336 |
+
const h = {
|
2337 |
+
origin: e.headers[`${o === 8 ? "sec-websocket-origin" : "origin"}`],
|
2338 |
+
secure: !!(e.socket.authorized || e.socket.encrypted),
|
2339 |
+
req: e
|
2340 |
+
};
|
2341 |
+
if (this.options.verifyClient.length === 2) {
|
2342 |
+
this.options.verifyClient(h, (p, v, _, u) => {
|
2343 |
+
if (!p)
|
2344 |
+
return H(t, v || 401, _, u);
|
2345 |
+
this.completeUpgrade(
|
2346 |
+
c,
|
2347 |
+
n,
|
2348 |
+
f,
|
2349 |
+
e,
|
2350 |
+
t,
|
2351 |
+
r,
|
2352 |
+
i
|
2353 |
+
);
|
2354 |
+
});
|
2355 |
+
return;
|
2356 |
+
}
|
2357 |
+
if (!this.options.verifyClient(h))
|
2358 |
+
return H(t, 401);
|
2359 |
+
}
|
2360 |
+
this.completeUpgrade(c, n, f, e, t, r, i);
|
2361 |
+
}
|
2362 |
+
/**
|
2363 |
+
* Upgrade the connection to WebSocket.
|
2364 |
+
*
|
2365 |
+
* @param {Object} extensions The accepted extensions
|
2366 |
+
* @param {String} key The value of the `Sec-WebSocket-Key` header
|
2367 |
+
* @param {Set} protocols The subprotocols
|
2368 |
+
* @param {http.IncomingMessage} req The request object
|
2369 |
+
* @param {(net.Socket|tls.Socket)} socket The network socket between the
|
2370 |
+
* server and client
|
2371 |
+
* @param {Buffer} head The first packet of the upgraded stream
|
2372 |
+
* @param {Function} cb Callback
|
2373 |
+
* @throws {Error} If called more than once with the same socket
|
2374 |
+
* @private
|
2375 |
+
*/
|
2376 |
+
completeUpgrade(e, t, r, i, n, o, l) {
|
2377 |
+
if (!n.readable || !n.writable)
|
2378 |
+
return n.destroy();
|
2379 |
+
if (n[Ds])
|
2380 |
+
throw new Error(
|
2381 |
+
"server.handleUpgrade() was called more than once with the same socket, possibly due to a misconfiguration"
|
2382 |
+
);
|
2383 |
+
if (this._state > Ke)
|
2384 |
+
return H(n, 503);
|
2385 |
+
const a = [
|
2386 |
+
"HTTP/1.1 101 Switching Protocols",
|
2387 |
+
"Upgrade: websocket",
|
2388 |
+
"Connection: Upgrade",
|
2389 |
+
`Sec-WebSocket-Accept: ${Bs("sha1").update(t + Is).digest("base64")}`
|
2390 |
+
], c = new this.options.WebSocket(null);
|
2391 |
+
if (r.size) {
|
2392 |
+
const h = this.options.handleProtocols ? this.options.handleProtocols(r, i) : r.values().next().value;
|
2393 |
+
h && (a.push(`Sec-WebSocket-Protocol: ${h}`), c._protocol = h);
|
2394 |
+
}
|
2395 |
+
if (e[N.extensionName]) {
|
2396 |
+
const h = e[N.extensionName].params, p = qe.format({
|
2397 |
+
[N.extensionName]: [h]
|
2398 |
+
});
|
2399 |
+
a.push(`Sec-WebSocket-Extensions: ${p}`), c._extensions = e;
|
2400 |
+
}
|
2401 |
+
this.emit("headers", a, i), n.write(a.concat(`\r
|
2402 |
+
`).join(`\r
|
2403 |
+
`)), n.removeListener("error", Ze), c.setSocket(n, o, {
|
2404 |
+
maxPayload: this.options.maxPayload,
|
2405 |
+
skipUTF8Validation: this.options.skipUTF8Validation
|
2406 |
+
}), this.clients && (this.clients.add(c), c.on("close", () => {
|
2407 |
+
this.clients.delete(c), this._shouldEmitClose && !this.clients.size && process.nextTick(G, this);
|
2408 |
+
})), l(c, i);
|
2409 |
+
}
|
2410 |
+
}
|
2411 |
+
var Fs = As;
|
2412 |
+
function js(s, e) {
|
2413 |
+
for (const t of Object.keys(e))
|
2414 |
+
s.on(t, e[t]);
|
2415 |
+
return function() {
|
2416 |
+
for (const r of Object.keys(e))
|
2417 |
+
s.removeListener(r, e[r]);
|
2418 |
+
};
|
2419 |
+
}
|
2420 |
+
function G(s) {
|
2421 |
+
s._state = pt, s.emit("close");
|
2422 |
+
}
|
2423 |
+
function Ze() {
|
2424 |
+
this.destroy();
|
2425 |
+
}
|
2426 |
+
function H(s, e, t, r) {
|
2427 |
+
t = t || ie.STATUS_CODES[e], r = {
|
2428 |
+
Connection: "close",
|
2429 |
+
"Content-Type": "text/html",
|
2430 |
+
"Content-Length": Buffer.byteLength(t),
|
2431 |
+
...r
|
2432 |
+
}, s.once("finish", s.destroy), s.end(
|
2433 |
+
`HTTP/1.1 ${e} ${ie.STATUS_CODES[e]}\r
|
2434 |
+
` + Object.keys(r).map((i) => `${i}: ${r[i]}`).join(`\r
|
2435 |
+
`) + `\r
|
2436 |
+
\r
|
2437 |
+
` + t
|
2438 |
+
);
|
2439 |
+
}
|
2440 |
+
function R(s, e, t, r, i) {
|
2441 |
+
if (s.listenerCount("wsClientError")) {
|
2442 |
+
const n = new Error(i);
|
2443 |
+
Error.captureStackTrace(n, R), s.emit("wsClientError", n, t, e);
|
2444 |
+
} else
|
2445 |
+
H(t, r, i);
|
2446 |
+
}
|
2447 |
+
const Zs = /* @__PURE__ */ z(Fs);
|
2448 |
+
export {
|
2449 |
+
qs as Receiver,
|
2450 |
+
Ks as Sender,
|
2451 |
+
Xs as WebSocket,
|
2452 |
+
Zs as WebSocketServer,
|
2453 |
+
Vs as createWebSocketStream,
|
2454 |
+
Xs as default
|
2455 |
+
};
|
src/backend/gradio_image_annotation/templates/example/index.js
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const {
|
2 |
+
SvelteComponent: m,
|
3 |
+
attr: f,
|
4 |
+
detach: o,
|
5 |
+
element: d,
|
6 |
+
init: b,
|
7 |
+
insert: g,
|
8 |
+
noop: c,
|
9 |
+
safe_not_equal: v,
|
10 |
+
src_url_equal: u,
|
11 |
+
toggle_class: a
|
12 |
+
} = window.__gradio__svelte__internal;
|
13 |
+
function _(n) {
|
14 |
+
let e, l;
|
15 |
+
return {
|
16 |
+
c() {
|
17 |
+
e = d("img"), u(e.src, l = /*value*/
|
18 |
+
n[0].url) || f(e, "src", l), f(e, "alt", "");
|
19 |
+
},
|
20 |
+
m(t, i) {
|
21 |
+
g(t, e, i);
|
22 |
+
},
|
23 |
+
p(t, i) {
|
24 |
+
i & /*value*/
|
25 |
+
1 && !u(e.src, l = /*value*/
|
26 |
+
t[0].url) && f(e, "src", l);
|
27 |
+
},
|
28 |
+
d(t) {
|
29 |
+
t && o(e);
|
30 |
+
}
|
31 |
+
};
|
32 |
+
}
|
33 |
+
function y(n) {
|
34 |
+
let e, l = (
|
35 |
+
/*value*/
|
36 |
+
n[0] && _(n)
|
37 |
+
);
|
38 |
+
return {
|
39 |
+
c() {
|
40 |
+
e = d("div"), l && l.c(), f(e, "class", "container svelte-1sgcyba"), a(
|
41 |
+
e,
|
42 |
+
"table",
|
43 |
+
/*type*/
|
44 |
+
n[1] === "table"
|
45 |
+
), a(
|
46 |
+
e,
|
47 |
+
"gallery",
|
48 |
+
/*type*/
|
49 |
+
n[1] === "gallery"
|
50 |
+
), a(
|
51 |
+
e,
|
52 |
+
"selected",
|
53 |
+
/*selected*/
|
54 |
+
n[2]
|
55 |
+
), a(
|
56 |
+
e,
|
57 |
+
"border",
|
58 |
+
/*value*/
|
59 |
+
n[0]
|
60 |
+
);
|
61 |
+
},
|
62 |
+
m(t, i) {
|
63 |
+
g(t, e, i), l && l.m(e, null);
|
64 |
+
},
|
65 |
+
p(t, [i]) {
|
66 |
+
/*value*/
|
67 |
+
t[0] ? l ? l.p(t, i) : (l = _(t), l.c(), l.m(e, null)) : l && (l.d(1), l = null), i & /*type*/
|
68 |
+
2 && a(
|
69 |
+
e,
|
70 |
+
"table",
|
71 |
+
/*type*/
|
72 |
+
t[1] === "table"
|
73 |
+
), i & /*type*/
|
74 |
+
2 && a(
|
75 |
+
e,
|
76 |
+
"gallery",
|
77 |
+
/*type*/
|
78 |
+
t[1] === "gallery"
|
79 |
+
), i & /*selected*/
|
80 |
+
4 && a(
|
81 |
+
e,
|
82 |
+
"selected",
|
83 |
+
/*selected*/
|
84 |
+
t[2]
|
85 |
+
), i & /*value*/
|
86 |
+
1 && a(
|
87 |
+
e,
|
88 |
+
"border",
|
89 |
+
/*value*/
|
90 |
+
t[0]
|
91 |
+
);
|
92 |
+
},
|
93 |
+
i: c,
|
94 |
+
o: c,
|
95 |
+
d(t) {
|
96 |
+
t && o(e), l && l.d();
|
97 |
+
}
|
98 |
+
};
|
99 |
+
}
|
100 |
+
function h(n, e, l) {
|
101 |
+
let { value: t } = e, { type: i } = e, { selected: r = !1 } = e;
|
102 |
+
return n.$$set = (s) => {
|
103 |
+
"value" in s && l(0, t = s.value), "type" in s && l(1, i = s.type), "selected" in s && l(2, r = s.selected);
|
104 |
+
}, [t, i, r];
|
105 |
+
}
|
106 |
+
class k extends m {
|
107 |
+
constructor(e) {
|
108 |
+
super(), b(this, e, h, y, v, { value: 0, type: 1, selected: 2 });
|
109 |
+
}
|
110 |
+
}
|
111 |
+
export {
|
112 |
+
k as default
|
113 |
+
};
|
src/backend/gradio_image_annotation/templates/example/style.css
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.container.svelte-1sgcyba img{width:100%;height:100%}.container.selected.svelte-1sgcyba{border-color:var(--border-color-accent)}.border.table.svelte-1sgcyba{border:2px solid var(--border-color-primary)}.container.table.svelte-1sgcyba{margin:0 auto;border-radius:var(--radius-lg);overflow:hidden;width:var(--size-20);height:var(--size-20);object-fit:cover}.container.gallery.svelte-1sgcyba{width:var(--size-20);max-width:var(--size-20);object-fit:cover}
|
src/demo/__init__.py
ADDED
File without changes
|
src/demo/app.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_image_annotation import image_annotator
|
3 |
+
|
4 |
+
|
5 |
+
example_annotation = {
|
6 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
7 |
+
"boxes": [
|
8 |
+
{
|
9 |
+
"xmin": 636,
|
10 |
+
"ymin": 575,
|
11 |
+
"xmax": 801,
|
12 |
+
"ymax": 697,
|
13 |
+
"label": "Vehicle",
|
14 |
+
"color": (255, 0, 0)
|
15 |
+
},
|
16 |
+
{
|
17 |
+
"xmin": 360,
|
18 |
+
"ymin": 615,
|
19 |
+
"xmax": 386,
|
20 |
+
"ymax": 702,
|
21 |
+
"label": "Person",
|
22 |
+
"color": (0, 255, 0)
|
23 |
+
}
|
24 |
+
]
|
25 |
+
}
|
26 |
+
|
27 |
+
examples_crop = [
|
28 |
+
{
|
29 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
30 |
+
"boxes": [
|
31 |
+
{
|
32 |
+
"xmin": 30,
|
33 |
+
"ymin": 70,
|
34 |
+
"xmax": 530,
|
35 |
+
"ymax": 500,
|
36 |
+
"color": (100, 200, 255),
|
37 |
+
}
|
38 |
+
],
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
42 |
+
"boxes": [
|
43 |
+
{
|
44 |
+
"xmin": 636,
|
45 |
+
"ymin": 575,
|
46 |
+
"xmax": 801,
|
47 |
+
"ymax": 697,
|
48 |
+
"color": (255, 0, 0),
|
49 |
+
},
|
50 |
+
],
|
51 |
+
},
|
52 |
+
]
|
53 |
+
|
54 |
+
|
55 |
+
def crop(annotations):
|
56 |
+
if annotations["boxes"]:
|
57 |
+
box = annotations["boxes"][0]
|
58 |
+
return annotations["image"][
|
59 |
+
box["ymin"]:box["ymax"],
|
60 |
+
box["xmin"]:box["xmax"]
|
61 |
+
]
|
62 |
+
return None
|
63 |
+
|
64 |
+
|
65 |
+
def get_boxes_json(annotations):
|
66 |
+
return annotations["boxes"]
|
67 |
+
|
68 |
+
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
71 |
+
annotator = image_annotator(
|
72 |
+
example_annotation,
|
73 |
+
label_list=["Person", "Vehicle"],
|
74 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
75 |
+
)
|
76 |
+
button_get = gr.Button("Get bounding boxes")
|
77 |
+
json_boxes = gr.JSON()
|
78 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
79 |
+
|
80 |
+
with gr.Tab("Crop", id="tab_crop"):
|
81 |
+
with gr.Row():
|
82 |
+
annotator_crop = image_annotator(
|
83 |
+
examples_crop[0],
|
84 |
+
image_type="numpy",
|
85 |
+
disable_edit_boxes=True,
|
86 |
+
single_box=True,
|
87 |
+
)
|
88 |
+
image_crop = gr.Image()
|
89 |
+
button_crop = gr.Button("Crop")
|
90 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
91 |
+
|
92 |
+
gr.Examples(examples_crop, annotator_crop)
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
demo.launch()
|
src/demo/css.css
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
html {
|
2 |
+
font-family: Inter;
|
3 |
+
font-size: 16px;
|
4 |
+
font-weight: 400;
|
5 |
+
line-height: 1.5;
|
6 |
+
-webkit-text-size-adjust: 100%;
|
7 |
+
background: #fff;
|
8 |
+
color: #323232;
|
9 |
+
-webkit-font-smoothing: antialiased;
|
10 |
+
-moz-osx-font-smoothing: grayscale;
|
11 |
+
text-rendering: optimizeLegibility;
|
12 |
+
}
|
13 |
+
|
14 |
+
:root {
|
15 |
+
--space: 1;
|
16 |
+
--vspace: calc(var(--space) * 1rem);
|
17 |
+
--vspace-0: calc(3 * var(--space) * 1rem);
|
18 |
+
--vspace-1: calc(2 * var(--space) * 1rem);
|
19 |
+
--vspace-2: calc(1.5 * var(--space) * 1rem);
|
20 |
+
--vspace-3: calc(0.5 * var(--space) * 1rem);
|
21 |
+
}
|
22 |
+
|
23 |
+
.app {
|
24 |
+
max-width: 748px !important;
|
25 |
+
}
|
26 |
+
|
27 |
+
.prose p {
|
28 |
+
margin: var(--vspace) 0;
|
29 |
+
line-height: var(--vspace * 2);
|
30 |
+
font-size: 1rem;
|
31 |
+
}
|
32 |
+
|
33 |
+
code {
|
34 |
+
font-family: "Inconsolata", sans-serif;
|
35 |
+
font-size: 16px;
|
36 |
+
}
|
37 |
+
|
38 |
+
h1,
|
39 |
+
h1 code {
|
40 |
+
font-weight: 400;
|
41 |
+
line-height: calc(2.5 / var(--space) * var(--vspace));
|
42 |
+
}
|
43 |
+
|
44 |
+
h1 code {
|
45 |
+
background: none;
|
46 |
+
border: none;
|
47 |
+
letter-spacing: 0.05em;
|
48 |
+
padding-bottom: 5px;
|
49 |
+
position: relative;
|
50 |
+
padding: 0;
|
51 |
+
}
|
52 |
+
|
53 |
+
h2 {
|
54 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
55 |
+
line-height: 1em;
|
56 |
+
}
|
57 |
+
|
58 |
+
h3,
|
59 |
+
h3 code {
|
60 |
+
margin: var(--vspace-1) 0 var(--vspace-2) 0;
|
61 |
+
line-height: 1em;
|
62 |
+
}
|
63 |
+
|
64 |
+
h4,
|
65 |
+
h5,
|
66 |
+
h6 {
|
67 |
+
margin: var(--vspace-3) 0 var(--vspace-3) 0;
|
68 |
+
line-height: var(--vspace);
|
69 |
+
}
|
70 |
+
|
71 |
+
.bigtitle,
|
72 |
+
h1,
|
73 |
+
h1 code {
|
74 |
+
font-size: calc(8px * 4.5);
|
75 |
+
word-break: break-word;
|
76 |
+
}
|
77 |
+
|
78 |
+
.title,
|
79 |
+
h2,
|
80 |
+
h2 code {
|
81 |
+
font-size: calc(8px * 3.375);
|
82 |
+
font-weight: lighter;
|
83 |
+
word-break: break-word;
|
84 |
+
border: none;
|
85 |
+
background: none;
|
86 |
+
}
|
87 |
+
|
88 |
+
.subheading1,
|
89 |
+
h3,
|
90 |
+
h3 code {
|
91 |
+
font-size: calc(8px * 1.8);
|
92 |
+
font-weight: 600;
|
93 |
+
border: none;
|
94 |
+
background: none;
|
95 |
+
letter-spacing: 0.1em;
|
96 |
+
text-transform: uppercase;
|
97 |
+
}
|
98 |
+
|
99 |
+
h2 code {
|
100 |
+
padding: 0;
|
101 |
+
position: relative;
|
102 |
+
letter-spacing: 0.05em;
|
103 |
+
}
|
104 |
+
|
105 |
+
blockquote {
|
106 |
+
font-size: calc(8px * 1.1667);
|
107 |
+
font-style: italic;
|
108 |
+
line-height: calc(1.1667 * var(--vspace));
|
109 |
+
margin: var(--vspace-2) var(--vspace-2);
|
110 |
+
}
|
111 |
+
|
112 |
+
.subheading2,
|
113 |
+
h4 {
|
114 |
+
font-size: calc(8px * 1.4292);
|
115 |
+
text-transform: uppercase;
|
116 |
+
font-weight: 600;
|
117 |
+
}
|
118 |
+
|
119 |
+
.subheading3,
|
120 |
+
h5 {
|
121 |
+
font-size: calc(8px * 1.2917);
|
122 |
+
line-height: calc(1.2917 * var(--vspace));
|
123 |
+
|
124 |
+
font-weight: lighter;
|
125 |
+
text-transform: uppercase;
|
126 |
+
letter-spacing: 0.15em;
|
127 |
+
}
|
128 |
+
|
129 |
+
h6 {
|
130 |
+
font-size: calc(8px * 1.1667);
|
131 |
+
font-size: 1.1667em;
|
132 |
+
font-weight: normal;
|
133 |
+
font-style: italic;
|
134 |
+
font-family: "le-monde-livre-classic-byol", serif !important;
|
135 |
+
letter-spacing: 0px !important;
|
136 |
+
}
|
137 |
+
|
138 |
+
#start .md > *:first-child {
|
139 |
+
margin-top: 0;
|
140 |
+
}
|
141 |
+
|
142 |
+
h2 + h3 {
|
143 |
+
margin-top: 0;
|
144 |
+
}
|
145 |
+
|
146 |
+
.md hr {
|
147 |
+
border: none;
|
148 |
+
border-top: 1px solid var(--block-border-color);
|
149 |
+
margin: var(--vspace-2) 0 var(--vspace-2) 0;
|
150 |
+
}
|
151 |
+
.prose ul {
|
152 |
+
margin: var(--vspace-2) 0 var(--vspace-1) 0;
|
153 |
+
}
|
154 |
+
|
155 |
+
.gap {
|
156 |
+
gap: 0;
|
157 |
+
}
|
src/demo/space.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from app import demo as app
|
4 |
+
import os
|
5 |
+
|
6 |
+
_docs = {'image_annotator': {'description': 'Creates a component to annotate images with bounding boxes. The bounding boxes can be created and edited by the user or be passed by code.\nIt is also possible to predefine a set of valid classes and colors.', 'members': {'__init__': {'value': {'type': 'dict | None', 'default': 'None', 'description': "A dict or None. The dictionary must contain a key 'image' with either an URL to an image, a numpy image or a PIL image. Optionally it may contain a key 'boxes' with a list of boxes. Each box must be a dict wit the keys: 'xmin', 'ymin', 'xmax' and 'ymax' with the absolute image coordinates of the box. Optionally can also include the keys 'label' and 'color' describing the label and color of the box. Color must be a tuple of RGB values (e.g. `(255,255,255)`)."}, 'boxes_alpha': {'type': 'float | None', 'default': 'None', 'description': 'Opacity of the bounding boxes 0 and 1.'}, 'label_list': {'type': 'list[str] | None', 'default': 'None', 'description': 'List of valid labels.'}, 'label_colors': {'type': 'list[str] | None', 'default': 'None', 'description': 'Optional list of colors for each label when `label_list` is used. Colors must be a tuple of RGB values (e.g. `(255,255,255)`).'}, 'box_min_size': {'type': 'int | None', 'default': 'None', 'description': 'Minimum valid bounding box size.'}, 'handle_size': {'type': 'int | None', 'default': 'None', 'description': 'Size of the bounding box resize handles.'}, 'box_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline.'}, 'box_selected_thickness': {'type': 'int | None', 'default': 'None', 'description': 'Thickness of the bounding box outline when it is selected.'}, 'disable_edit_boxes': {'type': 'bool | None', 'default': 'None', 'description': 'Disables the ability to set and edit the label and color of the boxes.'}, 'single_box': {'type': 'bool', 'default': 'False', 'description': 'If True, at most one box can be drawn.'}, 'height': {'type': 'int | str | None', 'default': 'None', 'description': 'The height of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'width': {'type': 'int | str | None', 'default': 'None', 'description': 'The width of the displayed image, specified in pixels if a number is passed, or in CSS units if a string is passed.'}, 'image_mode': {'type': '"1"\n | "L"\n | "P"\n | "RGB"\n | "RGBA"\n | "CMYK"\n | "YCbCr"\n | "LAB"\n | "HSV"\n | "I"\n | "F"', 'default': '"RGB"', 'description': '"RGB" if color, or "L" if black and white. See https://pillow.readthedocs.io/en/stable/handbook/concepts.html for other supported image modes and their meaning.'}, 'sources': {'type': 'list["upload" | "webcam" | "clipboard"] | None', 'default': '["upload", "webcam", "clipboard"]', 'description': 'List of sources for the image. "upload" creates a box where user can drop an image file, "webcam" allows user to take snapshot from their webcam, "clipboard" allows users to paste an image from the clipboard. If None, defaults to ["upload", "webcam", "clipboard"].'}, 'image_type': {'type': '"numpy" | "pil" | "filepath"', 'default': '"numpy"', 'description': 'The format the image is converted before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (height, width, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image. If the image is SVG, the `type` is ignored and the filepath of the SVG is returned.'}, 'label': {'type': 'str | None', 'default': 'None', 'description': 'The label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.'}, 'container': {'type': 'bool', 'default': 'True', 'description': 'If True, will place the component in a container - providing some extra padding around the border.'}, 'scale': {'type': 'int | None', 'default': 'None', 'description': 'relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.'}, 'min_width': {'type': 'int', 'default': '160', 'description': 'minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.'}, 'interactive': {'type': 'bool | None', 'default': 'True', 'description': 'if True, will allow users to upload and annotate an image; if False, can only be used to display annotated images.'}, 'visible': {'type': 'bool', 'default': 'True', 'description': 'If False, component will be hidden.'}, 'elem_id': {'type': 'str | None', 'default': 'None', 'description': 'An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'elem_classes': {'type': 'list[str] | str | None', 'default': 'None', 'description': 'An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.'}, 'render': {'type': 'bool', 'default': 'True', 'description': 'If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.'}, 'show_label': {'type': 'bool | None', 'default': 'None', 'description': 'if True, will display label.'}, 'show_download_button': {'type': 'bool', 'default': 'True', 'description': 'If True, will show a button to download the image.'}, 'show_share_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.'}, 'show_clear_button': {'type': 'bool | None', 'default': 'True', 'description': 'If True, will show a button to clear the current image.'}, 'show_remove_button': {'type': 'bool | None', 'default': 'None', 'description': 'If True, will show a button to remove the selected bounding box.'}, 'handles_cursor': {'type': 'bool | None', 'default': 'True', 'description': 'If True, the cursor will change when hovering over box handles in drag mode. Can be CPU-intensive.'}}, 'postprocess': {'value': {'type': 'dict | None', 'description': 'A dict with an image and an optional list of boxes or None.'}}, 'preprocess': {'return': {'type': 'dict | None', 'description': 'A dict with the image and boxes or None.'}, 'value': None}}, 'events': {'clear': {'type': None, 'default': None, 'description': 'This listener is triggered when the user clears the image_annotator using the clear button for the component.'}, 'change': {'type': None, 'default': None, 'description': 'Triggered when the value of the image_annotator changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See `.input()` for a listener that is only triggered by user input.'}, 'upload': {'type': None, 'default': None, 'description': 'This listener is triggered when the user uploads a file into the image_annotator.'}}}, '__meta__': {'additional_interfaces': {}, 'user_fn_refs': {'image_annotator': []}}}
|
7 |
+
|
8 |
+
abs_path = os.path.join(os.path.dirname(__file__), "css.css")
|
9 |
+
|
10 |
+
with gr.Blocks(
|
11 |
+
css=abs_path,
|
12 |
+
theme=gr.themes.Default(
|
13 |
+
font_mono=[
|
14 |
+
gr.themes.GoogleFont("Inconsolata"),
|
15 |
+
"monospace",
|
16 |
+
],
|
17 |
+
),
|
18 |
+
) as demo:
|
19 |
+
gr.Markdown(
|
20 |
+
"""
|
21 |
+
# `gradio_image_annotation`
|
22 |
+
|
23 |
+
<div style="display: flex; gap: 7px;">
|
24 |
+
<a href="https://pypi.org/project/gradio_image_annotation/" target="_blank"><img alt="PyPI - Version" src="https://img.shields.io/pypi/v/gradio_image_annotation"></a>
|
25 |
+
</div>
|
26 |
+
|
27 |
+
A Gradio component that can be used to annotate images with bounding boxes.
|
28 |
+
""", elem_classes=["md-custom"], header_links=True)
|
29 |
+
app.render()
|
30 |
+
gr.Markdown(
|
31 |
+
"""
|
32 |
+
## Installation
|
33 |
+
|
34 |
+
```bash
|
35 |
+
pip install gradio_image_annotation
|
36 |
+
```
|
37 |
+
|
38 |
+
## Usage
|
39 |
+
|
40 |
+
```python
|
41 |
+
import gradio as gr
|
42 |
+
from gradio_image_annotation import image_annotator
|
43 |
+
|
44 |
+
|
45 |
+
example_annotation = {
|
46 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
47 |
+
"boxes": [
|
48 |
+
{
|
49 |
+
"xmin": 636,
|
50 |
+
"ymin": 575,
|
51 |
+
"xmax": 801,
|
52 |
+
"ymax": 697,
|
53 |
+
"label": "Vehicle",
|
54 |
+
"color": (255, 0, 0)
|
55 |
+
},
|
56 |
+
{
|
57 |
+
"xmin": 360,
|
58 |
+
"ymin": 615,
|
59 |
+
"xmax": 386,
|
60 |
+
"ymax": 702,
|
61 |
+
"label": "Person",
|
62 |
+
"color": (0, 255, 0)
|
63 |
+
}
|
64 |
+
]
|
65 |
+
}
|
66 |
+
|
67 |
+
examples_crop = [
|
68 |
+
{
|
69 |
+
"image": "https://raw.githubusercontent.com/gradio-app/gradio/main/guides/assets/logo.png",
|
70 |
+
"boxes": [
|
71 |
+
{
|
72 |
+
"xmin": 30,
|
73 |
+
"ymin": 70,
|
74 |
+
"xmax": 530,
|
75 |
+
"ymax": 500,
|
76 |
+
"color": (100, 200, 255),
|
77 |
+
}
|
78 |
+
],
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"image": "https://gradio-builds.s3.amazonaws.com/demo-files/base.png",
|
82 |
+
"boxes": [
|
83 |
+
{
|
84 |
+
"xmin": 636,
|
85 |
+
"ymin": 575,
|
86 |
+
"xmax": 801,
|
87 |
+
"ymax": 697,
|
88 |
+
"color": (255, 0, 0),
|
89 |
+
},
|
90 |
+
],
|
91 |
+
},
|
92 |
+
]
|
93 |
+
|
94 |
+
|
95 |
+
def crop(annotations):
|
96 |
+
if annotations["boxes"]:
|
97 |
+
box = annotations["boxes"][0]
|
98 |
+
return annotations["image"][
|
99 |
+
box["ymin"]:box["ymax"],
|
100 |
+
box["xmin"]:box["xmax"]
|
101 |
+
]
|
102 |
+
return None
|
103 |
+
|
104 |
+
|
105 |
+
def get_boxes_json(annotations):
|
106 |
+
return annotations["boxes"]
|
107 |
+
|
108 |
+
|
109 |
+
with gr.Blocks() as demo:
|
110 |
+
with gr.Tab("Object annotation", id="tab_object_annotation"):
|
111 |
+
annotator = image_annotator(
|
112 |
+
example_annotation,
|
113 |
+
label_list=["Person", "Vehicle"],
|
114 |
+
label_colors=[(0, 255, 0), (255, 0, 0)],
|
115 |
+
)
|
116 |
+
button_get = gr.Button("Get bounding boxes")
|
117 |
+
json_boxes = gr.JSON()
|
118 |
+
button_get.click(get_boxes_json, annotator, json_boxes)
|
119 |
+
|
120 |
+
with gr.Tab("Crop", id="tab_crop"):
|
121 |
+
with gr.Row():
|
122 |
+
annotator_crop = image_annotator(
|
123 |
+
examples_crop[0],
|
124 |
+
image_type="numpy",
|
125 |
+
disable_edit_boxes=True,
|
126 |
+
single_box=True,
|
127 |
+
)
|
128 |
+
image_crop = gr.Image()
|
129 |
+
button_crop = gr.Button("Crop")
|
130 |
+
button_crop.click(crop, annotator_crop, image_crop)
|
131 |
+
|
132 |
+
gr.Examples(examples_crop, annotator_crop)
|
133 |
+
|
134 |
+
if __name__ == "__main__":
|
135 |
+
demo.launch()
|
136 |
+
|
137 |
+
```
|
138 |
+
""", elem_classes=["md-custom"], header_links=True)
|
139 |
+
|
140 |
+
|
141 |
+
gr.Markdown("""
|
142 |
+
## `image_annotator`
|
143 |
+
|
144 |
+
### Initialization
|
145 |
+
""", elem_classes=["md-custom"], header_links=True)
|
146 |
+
|
147 |
+
gr.ParamViewer(value=_docs["image_annotator"]["members"]["__init__"], linkify=[])
|
148 |
+
|
149 |
+
|
150 |
+
gr.Markdown("### Events")
|
151 |
+
gr.ParamViewer(value=_docs["image_annotator"]["events"], linkify=['Event'])
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
gr.Markdown("""
|
157 |
+
|
158 |
+
### User function
|
159 |
+
|
160 |
+
The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).
|
161 |
+
|
162 |
+
- When used as an Input, the component only impacts the input signature of the user function.
|
163 |
+
- When used as an output, the component only impacts the return signature of the user function.
|
164 |
+
|
165 |
+
The code snippet below is accurate in cases where the component is used as both an input and an output.
|
166 |
+
|
167 |
+
- **As input:** Is passed, a dict with the image and boxes or None.
|
168 |
+
- **As output:** Should return, a dict with an image and an optional list of boxes or None.
|
169 |
+
|
170 |
+
```python
|
171 |
+
def predict(
|
172 |
+
value: dict | None
|
173 |
+
) -> dict | None:
|
174 |
+
return value
|
175 |
+
```
|
176 |
+
""", elem_classes=["md-custom", "image_annotator-user-fn"], header_links=True)
|
177 |
+
|
178 |
+
|
179 |
+
|
180 |
+
|
181 |
+
demo.load(None, js=r"""function() {
|
182 |
+
const refs = {};
|
183 |
+
const user_fn_refs = {
|
184 |
+
image_annotator: [], };
|
185 |
+
requestAnimationFrame(() => {
|
186 |
+
|
187 |
+
Object.entries(user_fn_refs).forEach(([key, refs]) => {
|
188 |
+
if (refs.length > 0) {
|
189 |
+
const el = document.querySelector(`.${key}-user-fn`);
|
190 |
+
if (!el) return;
|
191 |
+
refs.forEach(ref => {
|
192 |
+
el.innerHTML = el.innerHTML.replace(
|
193 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
194 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
195 |
+
);
|
196 |
+
})
|
197 |
+
}
|
198 |
+
})
|
199 |
+
|
200 |
+
Object.entries(refs).forEach(([key, refs]) => {
|
201 |
+
if (refs.length > 0) {
|
202 |
+
const el = document.querySelector(`.${key}`);
|
203 |
+
if (!el) return;
|
204 |
+
refs.forEach(ref => {
|
205 |
+
el.innerHTML = el.innerHTML.replace(
|
206 |
+
new RegExp("\\b"+ref+"\\b", "g"),
|
207 |
+
`<a href="#h-${ref.toLowerCase()}">${ref}</a>`
|
208 |
+
);
|
209 |
+
})
|
210 |
+
}
|
211 |
+
})
|
212 |
+
})
|
213 |
+
}
|
214 |
+
|
215 |
+
""")
|
216 |
+
|
217 |
+
demo.launch()
|
src/frontend/Example.svelte
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import type { FileData } from "@gradio/client";
|
3 |
+
|
4 |
+
export let value: null | FileData;
|
5 |
+
export let type: "gallery" | "table";
|
6 |
+
export let selected = false;
|
7 |
+
</script>
|
8 |
+
|
9 |
+
<div
|
10 |
+
class="container"
|
11 |
+
class:table={type === "table"}
|
12 |
+
class:gallery={type === "gallery"}
|
13 |
+
class:selected
|
14 |
+
class:border={value}
|
15 |
+
>
|
16 |
+
{#if value}
|
17 |
+
<img src={value.url} alt="" />
|
18 |
+
{/if}
|
19 |
+
</div>
|
20 |
+
|
21 |
+
<style>
|
22 |
+
.container :global(img) {
|
23 |
+
width: 100%;
|
24 |
+
height: 100%;
|
25 |
+
}
|
26 |
+
|
27 |
+
.container.selected {
|
28 |
+
border-color: var(--border-color-accent);
|
29 |
+
}
|
30 |
+
.border.table {
|
31 |
+
border: 2px solid var(--border-color-primary);
|
32 |
+
}
|
33 |
+
|
34 |
+
.container.table {
|
35 |
+
margin: 0 auto;
|
36 |
+
border-radius: var(--radius-lg);
|
37 |
+
overflow: hidden;
|
38 |
+
width: var(--size-20);
|
39 |
+
height: var(--size-20);
|
40 |
+
object-fit: cover;
|
41 |
+
}
|
42 |
+
|
43 |
+
.container.gallery {
|
44 |
+
width: var(--size-20);
|
45 |
+
max-width: var(--size-20);
|
46 |
+
object-fit: cover;
|
47 |
+
}
|
48 |
+
</style>
|
src/frontend/Index.svelte
ADDED
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<svelte:options accessors={true} />
|
2 |
+
|
3 |
+
<script context="module" lang="ts">
|
4 |
+
export { default as BaseExample } from "./Example.svelte";
|
5 |
+
</script>
|
6 |
+
|
7 |
+
<script lang="ts">
|
8 |
+
import type { Gradio, SelectData } from "@gradio/utils";
|
9 |
+
import { Block, Empty, UploadText } from "@gradio/atoms";
|
10 |
+
import { Image } from "@gradio/icons";
|
11 |
+
import { StatusTracker } from "@gradio/statustracker";
|
12 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
13 |
+
import AnnotatedImageData from "./shared/AnnotatedImageData";
|
14 |
+
import ImageAnnotator from "./shared/ImageAnnotator.svelte";
|
15 |
+
|
16 |
+
type sources = "upload" | "webcam" | "clipboard" | null;
|
17 |
+
|
18 |
+
export let elem_id = "";
|
19 |
+
export let elem_classes: string[] = [];
|
20 |
+
export let visible = true;
|
21 |
+
export let value: null | AnnotatedImageData = null;
|
22 |
+
export let label: string;
|
23 |
+
export let show_label: boolean;
|
24 |
+
export let root: string;
|
25 |
+
export let height: number | string | undefined;
|
26 |
+
export let width: number | string | undefined;
|
27 |
+
export let _selectable = false;
|
28 |
+
export let container = true;
|
29 |
+
export let scale: number | null = null;
|
30 |
+
export let min_width: number | undefined = undefined;
|
31 |
+
export let loading_status: LoadingStatus;
|
32 |
+
export let sources: ("upload" | "webcam" | "clipboard")[] = [
|
33 |
+
"upload",
|
34 |
+
"webcam",
|
35 |
+
"clipboard",
|
36 |
+
];
|
37 |
+
export let show_download_button: boolean;
|
38 |
+
export let show_share_button: boolean;
|
39 |
+
export let show_clear_button: boolean;
|
40 |
+
export let interactive: boolean;
|
41 |
+
export let boxes_alpha: number;
|
42 |
+
export let label_list: string[];
|
43 |
+
export let label_colors: string[];
|
44 |
+
export let box_min_size: number;
|
45 |
+
export let handle_size: number;
|
46 |
+
export let box_thickness: number;
|
47 |
+
export let box_selected_thickness: number;
|
48 |
+
export let disable_edit_boxes: boolean;
|
49 |
+
export let single_box: boolean;
|
50 |
+
export let show_remove_button: boolean;
|
51 |
+
export let handles_cursor: boolean;
|
52 |
+
|
53 |
+
export let gradio: Gradio<{
|
54 |
+
change: never;
|
55 |
+
error: string;
|
56 |
+
edit: never;
|
57 |
+
drag: never;
|
58 |
+
upload: never;
|
59 |
+
clear: never;
|
60 |
+
select: SelectData;
|
61 |
+
share: ShareData;
|
62 |
+
}>;
|
63 |
+
|
64 |
+
let dragging: boolean;
|
65 |
+
let active_source: sources = null;
|
66 |
+
</script>
|
67 |
+
|
68 |
+
<Block
|
69 |
+
{visible}
|
70 |
+
variant={"solid"}
|
71 |
+
border_mode={dragging ? "focus" : "base"}
|
72 |
+
padding={false}
|
73 |
+
{elem_id}
|
74 |
+
{elem_classes}
|
75 |
+
{width}
|
76 |
+
allow_overflow={false}
|
77 |
+
{container}
|
78 |
+
{scale}
|
79 |
+
{min_width}
|
80 |
+
>
|
81 |
+
<StatusTracker
|
82 |
+
autoscroll={gradio.autoscroll}
|
83 |
+
i18n={gradio.i18n}
|
84 |
+
{...loading_status}
|
85 |
+
/>
|
86 |
+
|
87 |
+
<ImageAnnotator
|
88 |
+
bind:active_source
|
89 |
+
bind:value
|
90 |
+
on:change={() => gradio.dispatch("change")}
|
91 |
+
selectable={_selectable}
|
92 |
+
{root}
|
93 |
+
{sources}
|
94 |
+
{interactive}
|
95 |
+
showDownloadButton={show_download_button}
|
96 |
+
showShareButton={show_share_button}
|
97 |
+
showClearButton={show_clear_button}
|
98 |
+
i18n={gradio.i18n}
|
99 |
+
boxesAlpha={boxes_alpha}
|
100 |
+
height={height}
|
101 |
+
width={width}
|
102 |
+
labelList={label_list}
|
103 |
+
labelColors={label_colors}
|
104 |
+
boxMinSize={box_min_size}
|
105 |
+
on:edit={() => gradio.dispatch("edit")}
|
106 |
+
on:clear={() => {
|
107 |
+
gradio.dispatch("clear");
|
108 |
+
}}
|
109 |
+
on:drag={({ detail }) => (dragging = detail)}
|
110 |
+
on:upload={() => gradio.dispatch("upload")}
|
111 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
112 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
113 |
+
on:error={({ detail }) => {
|
114 |
+
loading_status = loading_status || {};
|
115 |
+
loading_status.status = "error";
|
116 |
+
gradio.dispatch("error", detail);
|
117 |
+
}}
|
118 |
+
{label}
|
119 |
+
{show_label}
|
120 |
+
max_file_size={gradio.max_file_size}
|
121 |
+
cli_upload={gradio.client.upload}
|
122 |
+
stream_handler={gradio.client.stream}
|
123 |
+
handleSize={handle_size}
|
124 |
+
boxThickness={box_thickness}
|
125 |
+
boxSelectedThickness={box_selected_thickness}
|
126 |
+
disableEditBoxes={disable_edit_boxes}
|
127 |
+
singleBox={single_box}
|
128 |
+
showRemoveButton={show_remove_button}
|
129 |
+
handlesCursor={handles_cursor}
|
130 |
+
>
|
131 |
+
{#if active_source === "upload"}
|
132 |
+
<UploadText i18n={gradio.i18n} type="image" />
|
133 |
+
{:else if active_source === "clipboard"}
|
134 |
+
<UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
|
135 |
+
{:else}
|
136 |
+
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
137 |
+
{/if}
|
138 |
+
</ImageAnnotator>
|
139 |
+
</Block>
|
src/frontend/package-lock.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
src/frontend/package.json
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "gradio_image_annotator",
|
3 |
+
"version": "0.11.9",
|
4 |
+
"description": "Gradio UI packages",
|
5 |
+
"type": "module",
|
6 |
+
"author": "",
|
7 |
+
"license": "ISC",
|
8 |
+
"private": false,
|
9 |
+
"dependencies": {
|
10 |
+
"@gradio/atoms": "0.7.4",
|
11 |
+
"@gradio/client": "1.1.0",
|
12 |
+
"@gradio/icons": "0.4.1",
|
13 |
+
"@gradio/statustracker": "0.6.0",
|
14 |
+
"@gradio/upload": "0.11.1",
|
15 |
+
"@gradio/image": "0.11.9",
|
16 |
+
"@gradio/utils": "0.4.2",
|
17 |
+
"@gradio/wasm": "0.10.1",
|
18 |
+
"cropperjs": "^1.5.12",
|
19 |
+
"lazy-brush": "^1.0.1",
|
20 |
+
"resize-observer-polyfill": "^1.5.1",
|
21 |
+
"@gradio/colorpicker": "^0.2.11",
|
22 |
+
"@gradio/button": "^0.2.24"
|
23 |
+
},
|
24 |
+
"devDependencies": {
|
25 |
+
"@gradio/preview": "0.9.1"
|
26 |
+
},
|
27 |
+
"main_changeset": true,
|
28 |
+
"main": "./Index.svelte",
|
29 |
+
"exports": {
|
30 |
+
".": "./Index.svelte",
|
31 |
+
"./shared": "./shared/index.ts",
|
32 |
+
"./example": "./Example.svelte",
|
33 |
+
"./package.json": "./package.json"
|
34 |
+
}
|
35 |
+
}
|
src/frontend/shared/AnnotatedImageData.ts
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import type { FileData } from "@gradio/client";
|
2 |
+
import Box from "./Box";
|
3 |
+
|
4 |
+
export default class AnnotatedImageData {
|
5 |
+
image: FileData;
|
6 |
+
boxes: Box[] = [];
|
7 |
+
}
|
src/frontend/shared/Box.ts
ADDED
@@ -0,0 +1,510 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const clamp = (num: number, min: number, max: number) => Math.min(Math.max(num, min), max)
|
2 |
+
|
3 |
+
|
4 |
+
function setAlpha(rgbColor: string, alpha: number) {
|
5 |
+
if (rgbColor.startsWith('rgba')) {
|
6 |
+
return rgbColor.replace(/[\d.]+$/, alpha.toString());
|
7 |
+
}
|
8 |
+
const matches = rgbColor.match(/\d+/g);
|
9 |
+
if (!matches || matches.length !== 3) {
|
10 |
+
return `rgba(50, 50, 50, ${alpha})`;
|
11 |
+
}
|
12 |
+
const [r, g, b] = matches;
|
13 |
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
14 |
+
}
|
15 |
+
|
16 |
+
|
17 |
+
export default class Box {
|
18 |
+
label: string;
|
19 |
+
xmin: number;
|
20 |
+
ymin: number;
|
21 |
+
xmax: number;
|
22 |
+
ymax: number;
|
23 |
+
color: string;
|
24 |
+
alpha: number;
|
25 |
+
isDragging: boolean;
|
26 |
+
isResizing: boolean;
|
27 |
+
isSelected: boolean;
|
28 |
+
isCreating: boolean;
|
29 |
+
offsetMouseX: number;
|
30 |
+
offsetMouseY: number;
|
31 |
+
resizeHandleSize: number;
|
32 |
+
resizingHandleIndex: number;
|
33 |
+
minSize: number;
|
34 |
+
renderCallBack: () => void;
|
35 |
+
onFinishCreation: () => void;
|
36 |
+
canvasXmin: number;
|
37 |
+
canvasYmin: number;
|
38 |
+
canvasXmax: number;
|
39 |
+
canvasYmax: number;
|
40 |
+
scaleFactor: number;
|
41 |
+
thickness: number;
|
42 |
+
selectedThickness: number;
|
43 |
+
creatingAnchorX: string;
|
44 |
+
creatingAnchorY: string;
|
45 |
+
resizeHandles: {
|
46 |
+
xmin: number;
|
47 |
+
ymin: number;
|
48 |
+
xmax: number;
|
49 |
+
ymax: number;
|
50 |
+
cursor: string;
|
51 |
+
}[];
|
52 |
+
|
53 |
+
constructor(
|
54 |
+
renderCallBack: () => void,
|
55 |
+
onFinishCreation: () => void,
|
56 |
+
canvasXmin: number,
|
57 |
+
canvasYmin: number,
|
58 |
+
canvasXmax: number,
|
59 |
+
canvasYmax: number,
|
60 |
+
label: string,
|
61 |
+
xmin: number,
|
62 |
+
ymin: number,
|
63 |
+
xmax: number,
|
64 |
+
ymax: number,
|
65 |
+
color: string = "rgb(255, 255, 255)",
|
66 |
+
alpha: number = 0.5,
|
67 |
+
minSize: number = 25,
|
68 |
+
handleSize: number = 8,
|
69 |
+
thickness: number = 2,
|
70 |
+
selectedThickness: number = 4,
|
71 |
+
scaleFactor: number = 1,
|
72 |
+
) {
|
73 |
+
this.renderCallBack = renderCallBack;
|
74 |
+
this.onFinishCreation = onFinishCreation;
|
75 |
+
this.canvasXmin = canvasXmin;
|
76 |
+
this.canvasYmin = canvasYmin;
|
77 |
+
this.canvasXmax = canvasXmax;
|
78 |
+
this.canvasYmax = canvasYmax;
|
79 |
+
this.scaleFactor = scaleFactor;
|
80 |
+
this.label = label;
|
81 |
+
this.isDragging = false;
|
82 |
+
this.isCreating = false;
|
83 |
+
this.xmin = xmin;
|
84 |
+
this.ymin = ymin;
|
85 |
+
this.xmax = xmax;
|
86 |
+
this.ymax = ymax;
|
87 |
+
this.isResizing = false;
|
88 |
+
this.isSelected = false;
|
89 |
+
this.offsetMouseX = 0;
|
90 |
+
this.offsetMouseY = 0;
|
91 |
+
this.resizeHandleSize = handleSize;
|
92 |
+
this.thickness = thickness;
|
93 |
+
this.selectedThickness = selectedThickness;
|
94 |
+
this.updateHandles();
|
95 |
+
this.resizingHandleIndex = -1;
|
96 |
+
this.minSize = minSize;
|
97 |
+
this.color = color;
|
98 |
+
this.alpha = alpha;
|
99 |
+
this.creatingAnchorX = "xmin";
|
100 |
+
this.creatingAnchorY = "ymin";
|
101 |
+
}
|
102 |
+
|
103 |
+
toJSON() {
|
104 |
+
return {
|
105 |
+
label: this.label,
|
106 |
+
xmin: this.xmin,
|
107 |
+
ymin: this.ymin,
|
108 |
+
xmax: this.xmax,
|
109 |
+
ymax: this.ymax,
|
110 |
+
color: this.color,
|
111 |
+
scaleFactor: this.scaleFactor,
|
112 |
+
};
|
113 |
+
}
|
114 |
+
|
115 |
+
setSelected(selected: boolean): void{
|
116 |
+
this.isSelected = selected;
|
117 |
+
}
|
118 |
+
|
119 |
+
setScaleFactor(scaleFactor: number) {
|
120 |
+
let scale = scaleFactor / this.scaleFactor;
|
121 |
+
this.xmin = Math.round(this.xmin * scale);
|
122 |
+
this.ymin = Math.round(this.ymin * scale);
|
123 |
+
this.xmax = Math.round(this.xmax * scale);
|
124 |
+
this.ymax = Math.round(this.ymax * scale);
|
125 |
+
this.updateHandles();
|
126 |
+
this.scaleFactor = scaleFactor;
|
127 |
+
}
|
128 |
+
|
129 |
+
updateHandles(): void {
|
130 |
+
const halfSize = this.resizeHandleSize / 2;
|
131 |
+
const width = this.getWidth();
|
132 |
+
const height = this.getHeight();
|
133 |
+
this.resizeHandles = [
|
134 |
+
{
|
135 |
+
// Top left
|
136 |
+
xmin: this.xmin - halfSize,
|
137 |
+
ymin: this.ymin - halfSize,
|
138 |
+
xmax: this.xmin + halfSize,
|
139 |
+
ymax: this.ymin + halfSize,
|
140 |
+
cursor: "nwse-resize",
|
141 |
+
},
|
142 |
+
{
|
143 |
+
// Top right
|
144 |
+
xmin: this.xmax - halfSize,
|
145 |
+
ymin: this.ymin - halfSize,
|
146 |
+
xmax: this.xmax + halfSize,
|
147 |
+
ymax: this.ymin + halfSize,
|
148 |
+
cursor: "nesw-resize",
|
149 |
+
},
|
150 |
+
{
|
151 |
+
// Bottom right
|
152 |
+
xmin: this.xmax - halfSize,
|
153 |
+
ymin: this.ymax - halfSize,
|
154 |
+
xmax: this.xmax + halfSize,
|
155 |
+
ymax: this.ymax + halfSize,
|
156 |
+
cursor: "nwse-resize",
|
157 |
+
},
|
158 |
+
{
|
159 |
+
// Bottom left
|
160 |
+
xmin: this.xmin - halfSize,
|
161 |
+
ymin: this.ymax - halfSize,
|
162 |
+
xmax: this.xmin + halfSize,
|
163 |
+
ymax: this.ymax + halfSize,
|
164 |
+
cursor: "nesw-resize",
|
165 |
+
},
|
166 |
+
{
|
167 |
+
// Top center
|
168 |
+
xmin: this.xmin + (width / 2) - halfSize,
|
169 |
+
ymin: this.ymin - halfSize,
|
170 |
+
xmax: this.xmin + (width / 2) + halfSize,
|
171 |
+
ymax: this.ymin + halfSize,
|
172 |
+
cursor: "ns-resize",
|
173 |
+
},
|
174 |
+
{
|
175 |
+
// Right center
|
176 |
+
xmin: this.xmax - halfSize,
|
177 |
+
ymin: this.ymin + (height / 2) - halfSize,
|
178 |
+
xmax: this.xmax + halfSize,
|
179 |
+
ymax: this.ymin + (height / 2) + halfSize,
|
180 |
+
cursor: "ew-resize",
|
181 |
+
},
|
182 |
+
{
|
183 |
+
// Bottom center
|
184 |
+
xmin: this.xmin + (width / 2) - halfSize,
|
185 |
+
ymin: this.ymax - halfSize,
|
186 |
+
xmax: this.xmin + (width / 2) + halfSize,
|
187 |
+
ymax: this.ymax + halfSize,
|
188 |
+
cursor: "ns-resize",
|
189 |
+
},
|
190 |
+
{
|
191 |
+
// Left center
|
192 |
+
xmin: this.xmin - halfSize,
|
193 |
+
ymin: this.ymin + (height / 2) - halfSize,
|
194 |
+
xmax: this.xmin + halfSize,
|
195 |
+
ymax: this.ymin + (height / 2) + halfSize,
|
196 |
+
cursor: "ew-resize",
|
197 |
+
},
|
198 |
+
];
|
199 |
+
}
|
200 |
+
|
201 |
+
getWidth(): number {
|
202 |
+
return this.xmax - this.xmin;
|
203 |
+
}
|
204 |
+
|
205 |
+
getHeight(): number {
|
206 |
+
return this.ymax - this.ymin;
|
207 |
+
}
|
208 |
+
|
209 |
+
getArea(): number {
|
210 |
+
return this.getWidth() * this.getHeight();
|
211 |
+
}
|
212 |
+
|
213 |
+
toCanvasCoordinates(x: number, y: number): [number, number] {
|
214 |
+
x = x + this.canvasXmin;
|
215 |
+
y = y + this.canvasYmin;
|
216 |
+
return [x, y];
|
217 |
+
}
|
218 |
+
|
219 |
+
toBoxCoordinates(x: number, y: number): [number, number] {
|
220 |
+
x = x - this.canvasXmin;
|
221 |
+
y = y - this.canvasYmin;
|
222 |
+
return [x, y];
|
223 |
+
}
|
224 |
+
|
225 |
+
render(ctx: CanvasRenderingContext2D): void {
|
226 |
+
let xmin: number, ymin: number;
|
227 |
+
|
228 |
+
// Render the box and border
|
229 |
+
ctx.beginPath();
|
230 |
+
[xmin, ymin] = this.toCanvasCoordinates(this.xmin, this.ymin);
|
231 |
+
ctx.rect(xmin, ymin, this.getWidth(), this.getHeight());
|
232 |
+
ctx.fillStyle = setAlpha(this.color, this.alpha);
|
233 |
+
ctx.fill();
|
234 |
+
if (this.isSelected) {
|
235 |
+
ctx.lineWidth = this.selectedThickness;
|
236 |
+
} else {
|
237 |
+
ctx.lineWidth = this.thickness;
|
238 |
+
}
|
239 |
+
ctx.strokeStyle = setAlpha(this.color, 1);
|
240 |
+
|
241 |
+
ctx.stroke();
|
242 |
+
ctx.closePath();
|
243 |
+
|
244 |
+
// Render the label and background
|
245 |
+
if (this.label !== null && this.label.trim() !== ""){
|
246 |
+
if (this.isSelected) {
|
247 |
+
ctx.font = "bold 14px Arial";
|
248 |
+
} else {
|
249 |
+
ctx.font = "12px Arial";
|
250 |
+
}
|
251 |
+
const labelWidth = ctx.measureText(this.label).width + 10;
|
252 |
+
const labelHeight = 20;
|
253 |
+
let labelX = this.xmin;
|
254 |
+
let labelY = this.ymin - labelHeight;
|
255 |
+
ctx.fillStyle = "white";
|
256 |
+
[labelX, labelY] = this.toCanvasCoordinates(labelX, labelY);
|
257 |
+
ctx.fillRect(labelX, labelY, labelWidth, labelHeight);
|
258 |
+
ctx.lineWidth = 1;
|
259 |
+
ctx.strokeStyle = "black";
|
260 |
+
ctx.strokeRect(labelX, labelY, labelWidth, labelHeight);
|
261 |
+
ctx.fillStyle = "black";
|
262 |
+
ctx.fillText(this.label, labelX + 5, labelY + 15);
|
263 |
+
}
|
264 |
+
|
265 |
+
// Render the handles
|
266 |
+
ctx.fillStyle = setAlpha(this.color, 1);
|
267 |
+
for (const handle of this.resizeHandles) {
|
268 |
+
[xmin, ymin] = this.toCanvasCoordinates(handle.xmin, handle.ymin);
|
269 |
+
ctx.fillRect(
|
270 |
+
xmin,
|
271 |
+
ymin,
|
272 |
+
handle.xmax - handle.xmin,
|
273 |
+
handle.ymax - handle.ymin,
|
274 |
+
);
|
275 |
+
}
|
276 |
+
}
|
277 |
+
|
278 |
+
startDrag(event: MouseEvent): void {
|
279 |
+
this.isDragging = true;
|
280 |
+
this.offsetMouseX = event.clientX - this.xmin;
|
281 |
+
this.offsetMouseY = event.clientY - this.ymin;
|
282 |
+
document.addEventListener("pointermove", this.handleDrag);
|
283 |
+
document.addEventListener("pointerup", this.stopDrag);
|
284 |
+
}
|
285 |
+
|
286 |
+
stopDrag = (): void => {
|
287 |
+
this.isDragging = false;
|
288 |
+
document.removeEventListener("pointermove", this.handleDrag);
|
289 |
+
document.removeEventListener("pointerup", this.stopDrag);
|
290 |
+
};
|
291 |
+
|
292 |
+
handleDrag = (event: MouseEvent): void => {
|
293 |
+
if (this.isDragging) {
|
294 |
+
let deltaX = event.clientX - this.offsetMouseX - this.xmin;
|
295 |
+
let deltaY = event.clientY - this.offsetMouseY - this.ymin;
|
296 |
+
const canvasW = this.canvasXmax - this.canvasXmin;
|
297 |
+
const canvasH = this.canvasYmax - this.canvasYmin;
|
298 |
+
deltaX = clamp(deltaX, -this.xmin, canvasW-this.xmax);
|
299 |
+
deltaY = clamp(deltaY, -this.ymin, canvasH-this.ymax);
|
300 |
+
this.xmin += deltaX;
|
301 |
+
this.ymin += deltaY;
|
302 |
+
this.xmax += deltaX;
|
303 |
+
this.ymax += deltaY;
|
304 |
+
this.updateHandles();
|
305 |
+
this.renderCallBack();
|
306 |
+
}
|
307 |
+
};
|
308 |
+
|
309 |
+
isPointInsideBox(x: number, y: number): boolean {
|
310 |
+
[x, y] = this.toBoxCoordinates(x, y);
|
311 |
+
return (
|
312 |
+
x >= this.xmin &&
|
313 |
+
x <= this.xmax &&
|
314 |
+
y >= this.ymin &&
|
315 |
+
y <= this.ymax
|
316 |
+
);
|
317 |
+
}
|
318 |
+
|
319 |
+
indexOfPointInsideHandle(x: number, y: number): number {
|
320 |
+
[x, y] = this.toBoxCoordinates(x, y);
|
321 |
+
for (let i = 0; i < this.resizeHandles.length; i++) {
|
322 |
+
const handle = this.resizeHandles[i];
|
323 |
+
if (
|
324 |
+
x >= handle.xmin &&
|
325 |
+
x <= handle.xmax &&
|
326 |
+
y >= handle.ymin &&
|
327 |
+
y <= handle.ymax
|
328 |
+
) {
|
329 |
+
this.resizingHandleIndex = i;
|
330 |
+
return i;
|
331 |
+
}
|
332 |
+
}
|
333 |
+
return -1;
|
334 |
+
}
|
335 |
+
|
336 |
+
startCreating(event: MouseEvent, canvasX: number, canvasY: number): void {
|
337 |
+
this.isCreating = true;
|
338 |
+
this.offsetMouseX = canvasX;
|
339 |
+
this.offsetMouseY = canvasY;
|
340 |
+
document.addEventListener("pointermove", this.handleCreating);
|
341 |
+
document.addEventListener("pointerup", this.stopCreating);
|
342 |
+
}
|
343 |
+
|
344 |
+
handleCreating = (event: MouseEvent): void => {
|
345 |
+
if (this.isCreating) {
|
346 |
+
let [x, y] = this.toBoxCoordinates(event.clientX, event.clientY);
|
347 |
+
x -= this.offsetMouseX;
|
348 |
+
y -= this.offsetMouseY;
|
349 |
+
|
350 |
+
if (x > this.xmax) {
|
351 |
+
if (this.creatingAnchorX == "xmax") {
|
352 |
+
this.xmin = this.xmax;
|
353 |
+
}
|
354 |
+
this.xmax = x;
|
355 |
+
this.creatingAnchorX = "xmin";
|
356 |
+
} else if (x > this.xmin && x < this.xmax && this.creatingAnchorX == "xmin") {
|
357 |
+
this.xmax = x;
|
358 |
+
} else if (x > this.xmin && x < this.xmax && this.creatingAnchorX == "xmax") {
|
359 |
+
this.xmin = x;
|
360 |
+
} else if (x < this.xmin) {
|
361 |
+
if (this.creatingAnchorX == "xmin") {
|
362 |
+
this.xmax = this.xmin;
|
363 |
+
}
|
364 |
+
this.xmin = x;
|
365 |
+
this.creatingAnchorX = "xmax";
|
366 |
+
}
|
367 |
+
|
368 |
+
if (y > this.ymax) {
|
369 |
+
if (this.creatingAnchorY == "ymax") {
|
370 |
+
this.ymin = this.ymax;
|
371 |
+
}
|
372 |
+
this.ymax = y;
|
373 |
+
this.creatingAnchorY = "ymin";
|
374 |
+
} else if (y > this.ymin && y < this.ymax && this.creatingAnchorY == "ymin") {
|
375 |
+
this.ymax = y;
|
376 |
+
} else if (y > this.ymin && y < this.ymax && this.creatingAnchorY == "ymax") {
|
377 |
+
this.ymin = y;
|
378 |
+
} else if (y < this.ymin) {
|
379 |
+
if (this.creatingAnchorY == "ymin") {
|
380 |
+
this.ymax = this.ymin;
|
381 |
+
}
|
382 |
+
this.ymin = y;
|
383 |
+
this.creatingAnchorY = "ymax";
|
384 |
+
}
|
385 |
+
|
386 |
+
this.updateHandles();
|
387 |
+
this.renderCallBack();
|
388 |
+
}
|
389 |
+
}
|
390 |
+
|
391 |
+
stopCreating = (event: MouseEvent): void => {
|
392 |
+
this.isCreating = false;
|
393 |
+
document.removeEventListener("pointermove", this.handleCreating);
|
394 |
+
document.removeEventListener("pointerup", this.stopCreating);
|
395 |
+
|
396 |
+
if (this.getArea() > 0) {
|
397 |
+
const canvasW = this.canvasXmax - this.canvasXmin;
|
398 |
+
const canvasH = this.canvasYmax - this.canvasYmin;
|
399 |
+
this.xmin = clamp(this.xmin, 0, canvasW - this.minSize);
|
400 |
+
this.ymin = clamp(this.ymin, 0, canvasH - this.minSize);
|
401 |
+
this.xmax = clamp(this.xmax, this.minSize, canvasW);
|
402 |
+
this.ymax = clamp(this.ymax, this.minSize, canvasH);
|
403 |
+
|
404 |
+
if (this.minSize > 0) {
|
405 |
+
if (this.getWidth() < this.minSize) {
|
406 |
+
if (this.creatingAnchorX == "xmin") {
|
407 |
+
this.xmax = this.xmin + this.minSize;
|
408 |
+
} else {
|
409 |
+
this.xmin = this.xmax - this.minSize;
|
410 |
+
}
|
411 |
+
}
|
412 |
+
if (this.getHeight() < this.minSize) {
|
413 |
+
if (this.creatingAnchorY == "ymin") {
|
414 |
+
this.ymax = this.ymin + this.minSize;
|
415 |
+
} else {
|
416 |
+
this.ymin = this.ymax - this.minSize;
|
417 |
+
}
|
418 |
+
}
|
419 |
+
if (this.xmax > canvasW) {
|
420 |
+
this.xmin -= this.xmax - canvasW;
|
421 |
+
this.xmax = canvasW;
|
422 |
+
} else if (this.xmin < 0) {
|
423 |
+
this.xmax -= this.xmin;
|
424 |
+
this.xmin = 0;
|
425 |
+
}
|
426 |
+
if (this.ymax > canvasH) {
|
427 |
+
this.ymin -= this.ymax - canvasH;
|
428 |
+
this.ymax = canvasH;
|
429 |
+
} else if (this.ymin < 0) {
|
430 |
+
this.ymax -= this.ymin;
|
431 |
+
this.ymin = 0;
|
432 |
+
}
|
433 |
+
}
|
434 |
+
this.updateHandles();
|
435 |
+
this.renderCallBack();
|
436 |
+
}
|
437 |
+
this.onFinishCreation();
|
438 |
+
}
|
439 |
+
|
440 |
+
startResize(handleIndex: number, event: MouseEvent): void {
|
441 |
+
this.resizingHandleIndex = handleIndex;
|
442 |
+
this.isResizing = true;
|
443 |
+
this.offsetMouseX = event.clientX - this.resizeHandles[handleIndex].xmin;
|
444 |
+
this.offsetMouseY = event.clientY - this.resizeHandles[handleIndex].ymin;
|
445 |
+
document.addEventListener("pointermove", this.handleResize);
|
446 |
+
document.addEventListener("pointerup", this.stopResize);
|
447 |
+
}
|
448 |
+
|
449 |
+
handleResize = (event: MouseEvent): void => {
|
450 |
+
if (this.isResizing) {
|
451 |
+
const mouseX = event.clientX;
|
452 |
+
const mouseY = event.clientY;
|
453 |
+
const deltaX = mouseX - this.resizeHandles[this.resizingHandleIndex].xmin - this.offsetMouseX;
|
454 |
+
const deltaY = mouseY - this.resizeHandles[this.resizingHandleIndex].ymin - this.offsetMouseY;
|
455 |
+
const canvasW = this.canvasXmax - this.canvasXmin;
|
456 |
+
const canvasH = this.canvasYmax - this.canvasYmin;
|
457 |
+
switch (this.resizingHandleIndex) {
|
458 |
+
case 0: // Top-left handle
|
459 |
+
this.xmin += deltaX;
|
460 |
+
this.ymin += deltaY;
|
461 |
+
this.xmin = clamp(this.xmin, 0, this.xmax - this.minSize);
|
462 |
+
this.ymin = clamp(this.ymin, 0, this.ymax - this.minSize);
|
463 |
+
break;
|
464 |
+
case 1: // Top-right handle
|
465 |
+
this.xmax += deltaX;
|
466 |
+
this.ymin += deltaY;
|
467 |
+
this.xmax = clamp(this.xmax, this.xmin + this.minSize, canvasW);
|
468 |
+
this.ymin = clamp(this.ymin, 0, this.ymax - this.minSize);
|
469 |
+
break;
|
470 |
+
case 2: // Bottom-right handle
|
471 |
+
this.xmax += deltaX;
|
472 |
+
this.ymax += deltaY;
|
473 |
+
this.xmax = clamp(this.xmax, this.xmin + this.minSize, canvasW);
|
474 |
+
this.ymax = clamp(this.ymax, this.ymin + this.minSize, canvasH);
|
475 |
+
break;
|
476 |
+
case 3: // Bottom-left handle
|
477 |
+
this.xmin += deltaX;
|
478 |
+
this.ymax += deltaY;
|
479 |
+
this.xmin = clamp(this.xmin, 0, this.xmax - this.minSize);
|
480 |
+
this.ymax = clamp(this.ymax, this.ymin + this.minSize, canvasH);
|
481 |
+
break;
|
482 |
+
case 4: // Top center handle
|
483 |
+
this.ymin += deltaY;
|
484 |
+
this.ymin = clamp(this.ymin, 0, this.ymax - this.minSize);
|
485 |
+
break;
|
486 |
+
case 5: // Right center handle
|
487 |
+
this.xmax += deltaX;
|
488 |
+
this.xmax = clamp(this.xmax, this.xmin + this.minSize, canvasW);
|
489 |
+
break;
|
490 |
+
case 6: // Bottom center handle
|
491 |
+
this.ymax += deltaY;
|
492 |
+
this.ymax = clamp(this.ymax, this.ymin + this.minSize, canvasH);
|
493 |
+
break;
|
494 |
+
case 7: // Left center handle
|
495 |
+
this.xmin += deltaX;
|
496 |
+
this.xmin = clamp(this.xmin, 0, this.xmax - this.minSize);
|
497 |
+
break;
|
498 |
+
}
|
499 |
+
// Update the resize handles
|
500 |
+
this.updateHandles();
|
501 |
+
this.renderCallBack();
|
502 |
+
}
|
503 |
+
};
|
504 |
+
|
505 |
+
stopResize = (): void => {
|
506 |
+
this.isResizing = false;
|
507 |
+
document.removeEventListener("pointermove", this.handleResize);
|
508 |
+
document.removeEventListener("pointerup", this.stopResize);
|
509 |
+
};
|
510 |
+
}
|
src/frontend/shared/Canvas.svelte
ADDED
@@ -0,0 +1,575 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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1 |
+
<script lang="ts">
|
2 |
+
import { onMount, onDestroy, createEventDispatcher } from "svelte";
|
3 |
+
import { BoundingBox, Hand, Trash } from "./icons/index";
|
4 |
+
import ModalBox from "./ModalBox.svelte";
|
5 |
+
import Box from "./Box";
|
6 |
+
import { Colors } from './Colors.js';
|
7 |
+
import AnnotatedImageData from "./AnnotatedImageData";
|
8 |
+
|
9 |
+
enum Mode {creation, drag}
|
10 |
+
|
11 |
+
export let imageUrl: string | null = null;
|
12 |
+
export let interactive: boolean;
|
13 |
+
export let boxAlpha = 0.5;
|
14 |
+
export let boxMinSize = 25;
|
15 |
+
export let handleSize: number;
|
16 |
+
export let boxThickness: number;
|
17 |
+
export let boxSelectedThickness: number;
|
18 |
+
export let value: null | AnnotatedImageData;
|
19 |
+
export let choices = [];
|
20 |
+
export let choicesColors = [];
|
21 |
+
export let disableEditBoxes: boolean = false;
|
22 |
+
export let height: number | string = "100%";
|
23 |
+
export let width: number | string = "100%";
|
24 |
+
export let singleBox: boolean = false;
|
25 |
+
export let showRemoveButton: boolean = null;
|
26 |
+
export let handlesCursor: boolean = true;
|
27 |
+
|
28 |
+
if (showRemoveButton === null) {
|
29 |
+
showRemoveButton = (disableEditBoxes);
|
30 |
+
}
|
31 |
+
|
32 |
+
let canvas: HTMLCanvasElement;
|
33 |
+
let ctx: CanvasRenderingContext2D;
|
34 |
+
let image = null;
|
35 |
+
let selectedBox = -1;
|
36 |
+
let mode: Mode = Mode.drag;
|
37 |
+
|
38 |
+
if (value !== null && value.boxes.length == 0) {
|
39 |
+
mode = Mode.creation;
|
40 |
+
}
|
41 |
+
|
42 |
+
let canvasXmin = 0;
|
43 |
+
let canvasYmin = 0;
|
44 |
+
let canvasXmax = 0;
|
45 |
+
let canvasYmax = 0;
|
46 |
+
let scaleFactor = 1.0;
|
47 |
+
|
48 |
+
let imageWidth = 0;
|
49 |
+
let imageHeight = 0;
|
50 |
+
|
51 |
+
let editModalVisible = false;
|
52 |
+
let newModalVisible = false;
|
53 |
+
|
54 |
+
const dispatch = createEventDispatcher<{
|
55 |
+
change: undefined;
|
56 |
+
}>();
|
57 |
+
|
58 |
+
function colorHexToRGB(hex: string) {
|
59 |
+
var r = parseInt(hex.slice(1, 3), 16),
|
60 |
+
g = parseInt(hex.slice(3, 5), 16),
|
61 |
+
b = parseInt(hex.slice(5, 7), 16);
|
62 |
+
return "rgb(" + r + ", " + g + ", " + b + ")";
|
63 |
+
}
|
64 |
+
|
65 |
+
function colorRGBAToHex(rgba: string) {
|
66 |
+
const rgbaValues = rgba.match(/(\d+(\.\d+)?)/g);
|
67 |
+
const r = parseInt(rgbaValues[0]);
|
68 |
+
const g = parseInt(rgbaValues[1]);
|
69 |
+
const b = parseInt(rgbaValues[2]);
|
70 |
+
const hex = "#" + ((1 << 24) | (r << 16) | (g << 8) | b).toString(16).slice(1);
|
71 |
+
return hex;
|
72 |
+
}
|
73 |
+
|
74 |
+
function draw() {
|
75 |
+
if (ctx) {
|
76 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
77 |
+
if (image !== null){
|
78 |
+
ctx.drawImage(image, canvasXmin, canvasYmin, imageWidth, imageHeight);
|
79 |
+
}
|
80 |
+
for (const box of value.boxes.slice().reverse()) {
|
81 |
+
box.render(ctx);
|
82 |
+
}
|
83 |
+
}
|
84 |
+
}
|
85 |
+
|
86 |
+
function selectBox(index: number) {
|
87 |
+
selectedBox = index;
|
88 |
+
value.boxes.forEach(box => {box.setSelected(false);});
|
89 |
+
if (index >= 0 && index < value.boxes.length){
|
90 |
+
value.boxes[index].setSelected(true);
|
91 |
+
}
|
92 |
+
draw();
|
93 |
+
}
|
94 |
+
|
95 |
+
function handlePointerDown(event: PointerEvent) {
|
96 |
+
if (!interactive) {
|
97 |
+
return;
|
98 |
+
}
|
99 |
+
|
100 |
+
if (
|
101 |
+
event.target instanceof Element &&
|
102 |
+
event.target.hasPointerCapture(event.pointerId)
|
103 |
+
) {
|
104 |
+
event.target.releasePointerCapture(event.pointerId);
|
105 |
+
}
|
106 |
+
|
107 |
+
if (mode === Mode.creation) {
|
108 |
+
createBox(event);
|
109 |
+
} else if (mode === Mode.drag) {
|
110 |
+
clickBox(event);
|
111 |
+
}
|
112 |
+
}
|
113 |
+
|
114 |
+
function clickBox(event: PointerEvent) {
|
115 |
+
const rect = canvas.getBoundingClientRect();
|
116 |
+
const mouseX = event.clientX - rect.left;
|
117 |
+
const mouseY = event.clientY - rect.top;
|
118 |
+
|
119 |
+
// Check if the mouse is over any of the resizing handles
|
120 |
+
for (const [i, box] of value.boxes.entries()) {
|
121 |
+
const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
|
122 |
+
if (handleIndex >= 0) {
|
123 |
+
selectBox(i);
|
124 |
+
box.startResize(handleIndex, event);
|
125 |
+
return;
|
126 |
+
}
|
127 |
+
}
|
128 |
+
|
129 |
+
// Check if the mouse is inside a box
|
130 |
+
for (const [i, box] of value.boxes.entries()) {
|
131 |
+
if (box.isPointInsideBox(mouseX, mouseY)) {
|
132 |
+
selectBox(i);
|
133 |
+
box.startDrag(event);
|
134 |
+
return;
|
135 |
+
}
|
136 |
+
}
|
137 |
+
|
138 |
+
if (!singleBox) {
|
139 |
+
selectBox(-1);
|
140 |
+
}
|
141 |
+
}
|
142 |
+
|
143 |
+
function handlePointerUp(event: PointerEvent) {
|
144 |
+
dispatch("change");
|
145 |
+
}
|
146 |
+
|
147 |
+
function handlePointerMove(event: PointerEvent) {
|
148 |
+
if (value === null) {
|
149 |
+
return;
|
150 |
+
}
|
151 |
+
|
152 |
+
if (mode !== Mode.drag) {
|
153 |
+
return;
|
154 |
+
}
|
155 |
+
|
156 |
+
const rect = canvas.getBoundingClientRect();
|
157 |
+
const mouseX = event.clientX - rect.left;
|
158 |
+
const mouseY = event.clientY - rect.top;
|
159 |
+
|
160 |
+
for (const [_, box] of value.boxes.entries()) {
|
161 |
+
const handleIndex = box.indexOfPointInsideHandle(mouseX, mouseY);
|
162 |
+
if (handleIndex >= 0) {
|
163 |
+
canvas.style.cursor = box.resizeHandles[handleIndex].cursor;
|
164 |
+
return;
|
165 |
+
}
|
166 |
+
}
|
167 |
+
|
168 |
+
canvas.style.cursor = "default";
|
169 |
+
}
|
170 |
+
|
171 |
+
function handleKeyPress(event: KeyboardEvent) {
|
172 |
+
if (!interactive) {
|
173 |
+
return;
|
174 |
+
}
|
175 |
+
|
176 |
+
switch (event.key) {
|
177 |
+
case "Delete":
|
178 |
+
onDeleteBox();
|
179 |
+
break;
|
180 |
+
}
|
181 |
+
}
|
182 |
+
|
183 |
+
function createBox(event: PointerEvent) {
|
184 |
+
const rect = canvas.getBoundingClientRect();
|
185 |
+
const x = (event.clientX - rect.left - canvasXmin) / scaleFactor;
|
186 |
+
const y = (event.clientY - rect.top - canvasYmin) / scaleFactor;
|
187 |
+
let color;
|
188 |
+
if (choicesColors.length > 0) {
|
189 |
+
color = colorHexToRGB(choicesColors[0]);
|
190 |
+
} else if (singleBox) {
|
191 |
+
if (value.boxes.length > 0) {
|
192 |
+
color = value.boxes[0].color;
|
193 |
+
} else {
|
194 |
+
color = Colors[0];
|
195 |
+
}
|
196 |
+
} else {
|
197 |
+
color = Colors[value.boxes.length % Colors.length];
|
198 |
+
}
|
199 |
+
|
200 |
+
let box = new Box(
|
201 |
+
draw,
|
202 |
+
onBoxFinishCreation,
|
203 |
+
canvasXmin,
|
204 |
+
canvasYmin,
|
205 |
+
canvasXmax,
|
206 |
+
canvasYmax,
|
207 |
+
"",
|
208 |
+
x,
|
209 |
+
y,
|
210 |
+
x,
|
211 |
+
y,
|
212 |
+
color,
|
213 |
+
boxAlpha,
|
214 |
+
boxMinSize,
|
215 |
+
handleSize,
|
216 |
+
boxThickness,
|
217 |
+
boxSelectedThickness
|
218 |
+
);
|
219 |
+
box.startCreating(event, rect.left, rect.top);
|
220 |
+
if (singleBox) {
|
221 |
+
value.boxes = [box];
|
222 |
+
} else {
|
223 |
+
value.boxes = [box, ...value.boxes];
|
224 |
+
}
|
225 |
+
selectBox(0);
|
226 |
+
draw();
|
227 |
+
dispatch("change");
|
228 |
+
}
|
229 |
+
|
230 |
+
function setCreateMode() {
|
231 |
+
mode = Mode.creation;
|
232 |
+
canvas.style.cursor = "crosshair";
|
233 |
+
}
|
234 |
+
|
235 |
+
function setDragMode() {
|
236 |
+
mode = Mode.drag;
|
237 |
+
canvas.style.cursor = "default";
|
238 |
+
}
|
239 |
+
|
240 |
+
function onBoxFinishCreation() {
|
241 |
+
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
|
242 |
+
if (value.boxes[selectedBox].getArea() < 1) {
|
243 |
+
onDeleteBox();
|
244 |
+
} else {
|
245 |
+
if (!disableEditBoxes) {
|
246 |
+
newModalVisible = true;
|
247 |
+
}
|
248 |
+
if (singleBox) {
|
249 |
+
setDragMode();
|
250 |
+
}
|
251 |
+
}
|
252 |
+
}
|
253 |
+
}
|
254 |
+
|
255 |
+
function onEditBox() {
|
256 |
+
if (selectedBox >= 0 && selectedBox < value.boxes.length && !disableEditBoxes) {
|
257 |
+
editModalVisible = true;
|
258 |
+
}
|
259 |
+
}
|
260 |
+
|
261 |
+
function handleDoubleClick(event: MouseEvent){
|
262 |
+
if (!interactive) {
|
263 |
+
return;
|
264 |
+
}
|
265 |
+
|
266 |
+
onEditBox();
|
267 |
+
}
|
268 |
+
|
269 |
+
function onModalEditChange(event) {
|
270 |
+
editModalVisible = false;
|
271 |
+
const { detail } = event;
|
272 |
+
let label = detail.label;
|
273 |
+
let color = detail.color;
|
274 |
+
let ret = detail.ret;
|
275 |
+
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
|
276 |
+
let box = value.boxes[selectedBox];
|
277 |
+
if (ret == 1) {
|
278 |
+
box.label = label;
|
279 |
+
box.color = colorHexToRGB(color);
|
280 |
+
draw();
|
281 |
+
dispatch("change");
|
282 |
+
} else if (ret == -1) {
|
283 |
+
onDeleteBox();
|
284 |
+
}
|
285 |
+
}
|
286 |
+
}
|
287 |
+
|
288 |
+
function onModalNewChange(event) {
|
289 |
+
newModalVisible = false;
|
290 |
+
const { detail } = event;
|
291 |
+
let label = detail.label;
|
292 |
+
let color = detail.color;
|
293 |
+
let ret = detail.ret;
|
294 |
+
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
|
295 |
+
let box = value.boxes[selectedBox];
|
296 |
+
if (ret == 1) {
|
297 |
+
box.label = label;
|
298 |
+
box.color = colorHexToRGB(color);
|
299 |
+
draw();
|
300 |
+
dispatch("change");
|
301 |
+
} else {
|
302 |
+
onDeleteBox();
|
303 |
+
}
|
304 |
+
}
|
305 |
+
}
|
306 |
+
|
307 |
+
function onDeleteBox() {
|
308 |
+
if (selectedBox >= 0 && selectedBox < value.boxes.length) {
|
309 |
+
value.boxes.splice(selectedBox, 1);
|
310 |
+
selectBox(-1);
|
311 |
+
if (singleBox) {
|
312 |
+
setCreateMode();
|
313 |
+
}
|
314 |
+
dispatch("change");
|
315 |
+
}
|
316 |
+
}
|
317 |
+
|
318 |
+
function resize() {
|
319 |
+
if (canvas) {
|
320 |
+
scaleFactor = 1;
|
321 |
+
canvas.width = canvas.clientWidth;
|
322 |
+
if (image !== null) {
|
323 |
+
if (image.width > canvas.width) {
|
324 |
+
scaleFactor = canvas.width / image.width;
|
325 |
+
imageWidth = image.width * scaleFactor;
|
326 |
+
imageHeight = image.height * scaleFactor;
|
327 |
+
canvasXmin = 0;
|
328 |
+
canvasYmin = 0;
|
329 |
+
canvasXmax = imageWidth;
|
330 |
+
canvasYmax = imageHeight;
|
331 |
+
canvas.height = imageHeight;
|
332 |
+
} else {
|
333 |
+
imageWidth = image.width;
|
334 |
+
imageHeight = image.height;
|
335 |
+
var x = (canvas.width - imageWidth) / 2;
|
336 |
+
canvasXmin = x;
|
337 |
+
canvasYmin = 0;
|
338 |
+
canvasXmax = x + imageWidth;
|
339 |
+
canvasYmax = image.height;
|
340 |
+
canvas.height = imageHeight;
|
341 |
+
}
|
342 |
+
} else {
|
343 |
+
canvasXmin = 0;
|
344 |
+
canvasYmin = 0;
|
345 |
+
canvasXmax = canvas.width;
|
346 |
+
canvasYmax = canvas.height;
|
347 |
+
canvas.height = canvas.clientHeight;
|
348 |
+
}
|
349 |
+
if (canvasXmax > 0 && canvasYmax > 0){
|
350 |
+
for (const box of value.boxes) {
|
351 |
+
box.canvasXmin = canvasXmin;
|
352 |
+
box.canvasYmin = canvasYmin;
|
353 |
+
box.canvasXmax = canvasXmax;
|
354 |
+
box.canvasYmax = canvasYmax;
|
355 |
+
box.setScaleFactor(scaleFactor);
|
356 |
+
}
|
357 |
+
}
|
358 |
+
draw();
|
359 |
+
dispatch("change");
|
360 |
+
}
|
361 |
+
}
|
362 |
+
const observer = new ResizeObserver(resize);
|
363 |
+
|
364 |
+
function parseInputBoxes() {
|
365 |
+
for (let i = 0; i < value.boxes.length; i++) {
|
366 |
+
let box = value.boxes[i];
|
367 |
+
if (!(box instanceof Box)) {
|
368 |
+
let color = "";
|
369 |
+
let label = "";
|
370 |
+
if (box.hasOwnProperty("color")) {
|
371 |
+
color = box["color"];
|
372 |
+
if (Array.isArray(color) && color.length === 3) {
|
373 |
+
color = `rgb(${color[0]}, ${color[1]}, ${color[2]})`;
|
374 |
+
}
|
375 |
+
} else {
|
376 |
+
color = Colors[i % Colors.length];
|
377 |
+
}
|
378 |
+
if (box.hasOwnProperty("label")) {
|
379 |
+
label = box["label"];
|
380 |
+
}
|
381 |
+
box = new Box(
|
382 |
+
draw,
|
383 |
+
onBoxFinishCreation,
|
384 |
+
canvasXmin,
|
385 |
+
canvasYmin,
|
386 |
+
canvasXmax,
|
387 |
+
canvasYmax,
|
388 |
+
label,
|
389 |
+
box["xmin"],
|
390 |
+
box["ymin"],
|
391 |
+
box["xmax"],
|
392 |
+
box["ymax"],
|
393 |
+
color,
|
394 |
+
boxAlpha,
|
395 |
+
boxMinSize,
|
396 |
+
handleSize,
|
397 |
+
boxThickness,
|
398 |
+
boxSelectedThickness
|
399 |
+
);
|
400 |
+
value.boxes[i] = box;
|
401 |
+
}
|
402 |
+
}
|
403 |
+
}
|
404 |
+
|
405 |
+
$: {
|
406 |
+
value;
|
407 |
+
setImage();
|
408 |
+
parseInputBoxes();
|
409 |
+
resize();
|
410 |
+
draw();
|
411 |
+
}
|
412 |
+
|
413 |
+
function setImage(){
|
414 |
+
if (imageUrl !== null) {
|
415 |
+
if (image === null || image.src != imageUrl) {
|
416 |
+
image = new Image();
|
417 |
+
image.src = imageUrl;
|
418 |
+
image.onload = function(){
|
419 |
+
resize();
|
420 |
+
draw();
|
421 |
+
}
|
422 |
+
}
|
423 |
+
}
|
424 |
+
}
|
425 |
+
|
426 |
+
onMount(() => {
|
427 |
+
if (Array.isArray(choices) && choices.length > 0) {
|
428 |
+
if (!Array.isArray(choicesColors) || choicesColors.length == 0) {
|
429 |
+
for (let i = 0; i < choices.length; i++) {
|
430 |
+
let color = Colors[i % Colors.length];
|
431 |
+
choicesColors.push(colorRGBAToHex(color));
|
432 |
+
}
|
433 |
+
}
|
434 |
+
}
|
435 |
+
|
436 |
+
ctx = canvas.getContext("2d");
|
437 |
+
observer.observe(canvas);
|
438 |
+
|
439 |
+
if (selectedBox < 0 && value !== null && value.boxes.length > 0) {
|
440 |
+
selectBox(0);
|
441 |
+
}
|
442 |
+
setImage();
|
443 |
+
resize();
|
444 |
+
draw();
|
445 |
+
});
|
446 |
+
|
447 |
+
function handleCanvasFocus() {
|
448 |
+
document.addEventListener("keydown", handleKeyPress);
|
449 |
+
}
|
450 |
+
|
451 |
+
function handleCanvasBlur() {
|
452 |
+
document.removeEventListener("keydown", handleKeyPress);
|
453 |
+
}
|
454 |
+
|
455 |
+
onDestroy(() => {
|
456 |
+
document.removeEventListener("keydown", handleKeyPress);
|
457 |
+
});
|
458 |
+
|
459 |
+
</script>
|
460 |
+
|
461 |
+
<div
|
462 |
+
class="canvas-container"
|
463 |
+
tabindex="-1"
|
464 |
+
on:focusin={handleCanvasFocus}
|
465 |
+
on:focusout={handleCanvasBlur}
|
466 |
+
>
|
467 |
+
<canvas
|
468 |
+
bind:this={canvas}
|
469 |
+
on:pointerdown={handlePointerDown}
|
470 |
+
on:pointerup={handlePointerUp}
|
471 |
+
on:pointermove={handlesCursor ? handlePointerMove : null}
|
472 |
+
on:dblclick={handleDoubleClick}
|
473 |
+
style="height: {height}; width: {width};"
|
474 |
+
class="canvas-annotator"
|
475 |
+
></canvas>
|
476 |
+
</div>
|
477 |
+
|
478 |
+
{#if interactive}
|
479 |
+
<span class="canvas-control">
|
480 |
+
<button
|
481 |
+
class="icon"
|
482 |
+
class:selected={mode === Mode.creation}
|
483 |
+
aria-label="Create box"
|
484 |
+
on:click={() => setCreateMode()}><BoundingBox/></button
|
485 |
+
>
|
486 |
+
<button
|
487 |
+
class="icon"
|
488 |
+
class:selected={mode === Mode.drag}
|
489 |
+
aria-label="Edit boxes"
|
490 |
+
on:click={() => setDragMode()}><Hand/></button
|
491 |
+
>
|
492 |
+
{#if showRemoveButton}
|
493 |
+
<button
|
494 |
+
class="icon"
|
495 |
+
aria-label="Remove boxes"
|
496 |
+
on:click={() => onDeleteBox()}><Trash/></button
|
497 |
+
>
|
498 |
+
{/if}
|
499 |
+
</span>
|
500 |
+
{/if}
|
501 |
+
|
502 |
+
{#if editModalVisible}
|
503 |
+
<ModalBox
|
504 |
+
on:change={onModalEditChange}
|
505 |
+
on:enter{onModalEditChange}
|
506 |
+
choices={choices}
|
507 |
+
choicesColors={choicesColors}
|
508 |
+
label={selectedBox >= 0 && selectedBox < value.boxes.length ? value.boxes[selectedBox].label : ""}
|
509 |
+
color={selectedBox >= 0 && selectedBox < value.boxes.length ? colorRGBAToHex(value.boxes[selectedBox].color) : ""}
|
510 |
+
/>
|
511 |
+
{/if}
|
512 |
+
|
513 |
+
{#if newModalVisible}
|
514 |
+
<ModalBox
|
515 |
+
on:change={onModalNewChange}
|
516 |
+
on:enter{onModalNewChange}
|
517 |
+
choices={choices}
|
518 |
+
showRemove={false}
|
519 |
+
choicesColors={choicesColors}
|
520 |
+
label={selectedBox >= 0 && selectedBox < value.boxes.length ? value.boxes[selectedBox].label : ""}
|
521 |
+
color={selectedBox >= 0 && selectedBox < value.boxes.length ? colorRGBAToHex(value.boxes[selectedBox].color) : ""}
|
522 |
+
/>
|
523 |
+
{/if}
|
524 |
+
|
525 |
+
<style>
|
526 |
+
.canvas-annotator {
|
527 |
+
border-color: var(--block-border-color);
|
528 |
+
width: 100%;
|
529 |
+
height: 100%;
|
530 |
+
display: block;
|
531 |
+
touch-action: none;
|
532 |
+
}
|
533 |
+
|
534 |
+
.canvas-control {
|
535 |
+
display: flex;
|
536 |
+
align-items: center;
|
537 |
+
justify-content: center;
|
538 |
+
border-top: 1px solid var(--border-color-primary);
|
539 |
+
width: 95%;
|
540 |
+
bottom: 0;
|
541 |
+
left: 0;
|
542 |
+
right: 0;
|
543 |
+
margin-left: auto;
|
544 |
+
margin-right: auto;
|
545 |
+
margin-top: var(--size-2);
|
546 |
+
}
|
547 |
+
|
548 |
+
.icon {
|
549 |
+
width: 22px;
|
550 |
+
height: 22px;
|
551 |
+
margin: var(--spacing-lg) var(--spacing-xs);
|
552 |
+
padding: var(--spacing-xs);
|
553 |
+
color: var(--neutral-400);
|
554 |
+
border-radius: var(--radius-md);
|
555 |
+
}
|
556 |
+
|
557 |
+
.icon:hover,
|
558 |
+
.icon:focus {
|
559 |
+
color: var(--color-accent);
|
560 |
+
}
|
561 |
+
|
562 |
+
.selected {
|
563 |
+
color: var(--color-accent);
|
564 |
+
}
|
565 |
+
|
566 |
+
.canvas-container {
|
567 |
+
display: flex;
|
568 |
+
justify-content: center;
|
569 |
+
align-items: center;
|
570 |
+
}
|
571 |
+
|
572 |
+
.canvas-container:focus {
|
573 |
+
outline: none;
|
574 |
+
}
|
575 |
+
</style>
|
src/frontend/shared/ClearImage.svelte
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { createEventDispatcher } from "svelte";
|
3 |
+
import { IconButton } from "@gradio/atoms";
|
4 |
+
import { Clear } from "@gradio/icons";
|
5 |
+
|
6 |
+
const dispatch = createEventDispatcher();
|
7 |
+
</script>
|
8 |
+
|
9 |
+
<div>
|
10 |
+
<IconButton
|
11 |
+
Icon={Clear}
|
12 |
+
label="Remove Image"
|
13 |
+
on:click={(event) => {
|
14 |
+
dispatch("remove_image");
|
15 |
+
event.stopPropagation();
|
16 |
+
}}
|
17 |
+
/>
|
18 |
+
</div>
|
19 |
+
|
20 |
+
<style>
|
21 |
+
div {
|
22 |
+
display: flex;
|
23 |
+
position: absolute;
|
24 |
+
top: var(--size-2);
|
25 |
+
right: var(--size-2);
|
26 |
+
justify-content: flex-end;
|
27 |
+
gap: var(--spacing-sm);
|
28 |
+
z-index: var(--layer-5);
|
29 |
+
}
|
30 |
+
</style>
|
src/frontend/shared/Colors.js
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export const Colors = [
|
2 |
+
"rgb(255, 168, 77)",
|
3 |
+
"rgb(92, 172, 238)",
|
4 |
+
"rgb(255, 99, 71)",
|
5 |
+
"rgb(118, 238, 118)",
|
6 |
+
"rgb(255, 145, 164)",
|
7 |
+
"rgb(0, 191, 255)",
|
8 |
+
"rgb(255, 218, 185)",
|
9 |
+
"rgb(255, 69, 0)",
|
10 |
+
"rgb(34, 139, 34)",
|
11 |
+
"rgb(255, 240, 245)",
|
12 |
+
"rgb(255, 193, 37)",
|
13 |
+
"rgb(255, 193, 7)",
|
14 |
+
"rgb(255, 250, 138)",
|
15 |
+
];
|
src/frontend/shared/ImageAnnotator.svelte
ADDED
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { createEventDispatcher, tick } from "svelte";
|
3 |
+
import { Download, Image as ImageIcon } from "@gradio/icons";
|
4 |
+
import { DownloadLink } from "@gradio/wasm/svelte";
|
5 |
+
import { uploadToHuggingFace } from "@gradio/utils";
|
6 |
+
import { BlockLabel, IconButton, ShareButton, SelectSource} from "@gradio/atoms";
|
7 |
+
import { Upload } from "@gradio/upload";
|
8 |
+
import { Webcam } from "@gradio/image";
|
9 |
+
import type { FileData, Client } from "@gradio/client";
|
10 |
+
import type { I18nFormatter, SelectData } from "@gradio/utils";
|
11 |
+
import { Clear } from "@gradio/icons";
|
12 |
+
import ImageCanvas from "./ImageCanvas.svelte";
|
13 |
+
import AnnotatedImageData from "./AnnotatedImageData";
|
14 |
+
|
15 |
+
type source_type = "upload" | "webcam" | "clipboard" | null;
|
16 |
+
|
17 |
+
export let value: null | AnnotatedImageData;
|
18 |
+
export let label: string | undefined = undefined;
|
19 |
+
export let show_label: boolean;
|
20 |
+
export let sources: source_type[] = ["upload", "webcam", "clipboard"];
|
21 |
+
export let selectable = false;
|
22 |
+
export let root: string;
|
23 |
+
export let interactive: boolean;
|
24 |
+
export let i18n: I18nFormatter;
|
25 |
+
export let showShareButton: boolean;
|
26 |
+
export let showDownloadButton: boolean;
|
27 |
+
export let showClearButton: boolean;
|
28 |
+
export let boxesAlpha;
|
29 |
+
export let labelList: string[];
|
30 |
+
export let labelColors: string[];
|
31 |
+
export let boxMinSize: number;
|
32 |
+
export let handleSize: number;
|
33 |
+
export let height: number | string;
|
34 |
+
export let width: number | string;
|
35 |
+
export let boxThickness: number;
|
36 |
+
export let disableEditBoxes: boolean;
|
37 |
+
export let singleBox: boolean;
|
38 |
+
export let showRemoveButton: boolean;
|
39 |
+
export let handlesCursor: boolean;
|
40 |
+
export let boxSelectedThickness: number;
|
41 |
+
export let max_file_size: number | null = null;
|
42 |
+
export let cli_upload: Client["upload"];
|
43 |
+
export let stream_handler: Client["stream_factory"];
|
44 |
+
|
45 |
+
let upload: Upload;
|
46 |
+
let uploading = false;
|
47 |
+
export let active_source: source_type = null;
|
48 |
+
|
49 |
+
function handle_upload({ detail }: CustomEvent<FileData>): void {
|
50 |
+
value = new AnnotatedImageData();
|
51 |
+
value.image = detail;
|
52 |
+
dispatch("upload");
|
53 |
+
}
|
54 |
+
|
55 |
+
async function handle_save(img_blob: Blob | any): Promise<void> {
|
56 |
+
const f = await upload.load_files([new File([img_blob], `webcam.png`)]);
|
57 |
+
const image = f?.[0] || null;
|
58 |
+
if (image) {
|
59 |
+
value = new AnnotatedImageData();
|
60 |
+
value.image = image;
|
61 |
+
} else {
|
62 |
+
value = null;
|
63 |
+
}
|
64 |
+
await tick();
|
65 |
+
dispatch("change");
|
66 |
+
}
|
67 |
+
|
68 |
+
$: if (uploading) clear();
|
69 |
+
|
70 |
+
const dispatch = createEventDispatcher<{
|
71 |
+
change: undefined;
|
72 |
+
clear: undefined;
|
73 |
+
drag: boolean;
|
74 |
+
upload?: never;
|
75 |
+
select: SelectData;
|
76 |
+
}>();
|
77 |
+
|
78 |
+
let dragging = false;
|
79 |
+
|
80 |
+
$: dispatch("drag", dragging);
|
81 |
+
|
82 |
+
$: if (!active_source && sources) {
|
83 |
+
active_source = sources[0];
|
84 |
+
}
|
85 |
+
|
86 |
+
async function handle_select_source(
|
87 |
+
source: (typeof sources)[number]
|
88 |
+
): Promise<void> {
|
89 |
+
switch (source) {
|
90 |
+
case "clipboard":
|
91 |
+
upload.paste_clipboard();
|
92 |
+
break;
|
93 |
+
default:
|
94 |
+
break;
|
95 |
+
}
|
96 |
+
}
|
97 |
+
|
98 |
+
function clear() {
|
99 |
+
value = null;
|
100 |
+
dispatch("clear");
|
101 |
+
dispatch("change");
|
102 |
+
}
|
103 |
+
</script>
|
104 |
+
|
105 |
+
<BlockLabel {show_label} Icon={ImageIcon} label={label || "Image Annotator"} />
|
106 |
+
|
107 |
+
<div class="icon-buttons">
|
108 |
+
{#if showDownloadButton && value !== null}
|
109 |
+
<DownloadLink href={value.image.url} download={value.image.orig_name || "image"}>
|
110 |
+
<IconButton Icon={Download} label={i18n("common.download")} />
|
111 |
+
</DownloadLink>
|
112 |
+
{/if}
|
113 |
+
{#if showShareButton && value !== null}
|
114 |
+
<ShareButton
|
115 |
+
{i18n}
|
116 |
+
on:share
|
117 |
+
on:error
|
118 |
+
formatter={async (value) => {
|
119 |
+
if (value === null) return "";
|
120 |
+
let url = await uploadToHuggingFace(value.image, "base64");
|
121 |
+
return `<img src="${url}" />`;
|
122 |
+
}}
|
123 |
+
{value}
|
124 |
+
/>
|
125 |
+
{/if}
|
126 |
+
{#if showClearButton && value !== null && interactive}
|
127 |
+
<div>
|
128 |
+
<IconButton
|
129 |
+
Icon={Clear}
|
130 |
+
label="Remove Image"
|
131 |
+
on:click={clear}
|
132 |
+
/>
|
133 |
+
</div>
|
134 |
+
{/if}
|
135 |
+
</div>
|
136 |
+
|
137 |
+
<div data-testid="image" class="image-container">
|
138 |
+
<div class="upload-container">
|
139 |
+
<Upload
|
140 |
+
hidden={value !== null || active_source === "webcam"}
|
141 |
+
bind:this={upload}
|
142 |
+
bind:uploading
|
143 |
+
bind:dragging
|
144 |
+
filetype={active_source === "clipboard" ? "clipboard" : "image/*"}
|
145 |
+
on:load={handle_upload}
|
146 |
+
on:error
|
147 |
+
{root}
|
148 |
+
{max_file_size}
|
149 |
+
disable_click={!sources.includes("upload")}
|
150 |
+
upload={cli_upload}
|
151 |
+
{stream_handler}
|
152 |
+
>
|
153 |
+
{#if value === null}
|
154 |
+
<slot />
|
155 |
+
{/if}
|
156 |
+
</Upload>
|
157 |
+
{#if value === null && active_source === "webcam"}
|
158 |
+
<Webcam
|
159 |
+
{root}
|
160 |
+
on:capture={(e) => handle_save(e.detail)}
|
161 |
+
on:stream={(e) => handle_save(e.detail)}
|
162 |
+
on:error
|
163 |
+
on:drag
|
164 |
+
on:upload={(e) => handle_save(e.detail)}
|
165 |
+
mode="image"
|
166 |
+
include_audio={false}
|
167 |
+
{i18n}
|
168 |
+
{upload}
|
169 |
+
/>
|
170 |
+
{/if}
|
171 |
+
{#if value !== null}
|
172 |
+
<div class:selectable class="image-frame">
|
173 |
+
<ImageCanvas
|
174 |
+
bind:value
|
175 |
+
on:change={() => dispatch("change")}
|
176 |
+
{height}
|
177 |
+
{width}
|
178 |
+
{boxesAlpha}
|
179 |
+
{labelList}
|
180 |
+
{labelColors}
|
181 |
+
{boxMinSize}
|
182 |
+
{interactive}
|
183 |
+
{handleSize}
|
184 |
+
{boxThickness}
|
185 |
+
{singleBox}
|
186 |
+
{disableEditBoxes}
|
187 |
+
{showRemoveButton}
|
188 |
+
{handlesCursor}
|
189 |
+
{boxSelectedThickness}
|
190 |
+
src={value.image.url}
|
191 |
+
/>
|
192 |
+
</div>
|
193 |
+
{/if}
|
194 |
+
</div>
|
195 |
+
{#if (sources.length > 1 || sources.includes("clipboard")) && value === null && interactive}
|
196 |
+
<SelectSource
|
197 |
+
{sources}
|
198 |
+
bind:active_source
|
199 |
+
handle_clear={clear}
|
200 |
+
handle_select={handle_select_source}
|
201 |
+
/>
|
202 |
+
{/if}
|
203 |
+
</div>
|
204 |
+
|
205 |
+
<style>
|
206 |
+
.image-frame :global(img) {
|
207 |
+
width: var(--size-full);
|
208 |
+
height: var(--size-full);
|
209 |
+
object-fit: cover;
|
210 |
+
}
|
211 |
+
|
212 |
+
.image-frame {
|
213 |
+
object-fit: cover;
|
214 |
+
width: 100%;
|
215 |
+
}
|
216 |
+
|
217 |
+
.upload-container {
|
218 |
+
height: 100%;
|
219 |
+
width: 100%;
|
220 |
+
flex-shrink: 1;
|
221 |
+
max-height: 100%;
|
222 |
+
}
|
223 |
+
|
224 |
+
.image-container {
|
225 |
+
display: flex;
|
226 |
+
height: 100%;
|
227 |
+
flex-direction: column;
|
228 |
+
justify-content: center;
|
229 |
+
align-items: center;
|
230 |
+
max-height: 100%;
|
231 |
+
}
|
232 |
+
|
233 |
+
.selectable {
|
234 |
+
cursor: crosshair;
|
235 |
+
}
|
236 |
+
|
237 |
+
.icon-buttons {
|
238 |
+
display: flex;
|
239 |
+
position: absolute;
|
240 |
+
top: 6px;
|
241 |
+
right: 6px;
|
242 |
+
gap: var(--size-1);
|
243 |
+
}
|
244 |
+
</style>
|
src/frontend/shared/ImageCanvas.svelte
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { resolve_wasm_src } from "@gradio/wasm/svelte";
|
3 |
+
import type { HTMLImgAttributes } from "svelte/elements";
|
4 |
+
import { createEventDispatcher } from "svelte";
|
5 |
+
import Canvas from "./Canvas.svelte"
|
6 |
+
import AnnotatedImageData from "./AnnotatedImageData";
|
7 |
+
|
8 |
+
interface Props extends HTMLImgAttributes {
|
9 |
+
"data-testid"?: string;
|
10 |
+
}
|
11 |
+
|
12 |
+
export let src: HTMLImgAttributes["src"] = undefined;
|
13 |
+
export let interactive: boolean;
|
14 |
+
export let boxesAlpha: number;
|
15 |
+
export let labelList: string[];
|
16 |
+
export let labelColors: string[];
|
17 |
+
export let boxMinSize: number;
|
18 |
+
export let handleSize: number;
|
19 |
+
export let boxThickness: number;
|
20 |
+
export let height: number | string;
|
21 |
+
export let width: number | string;
|
22 |
+
export let boxSelectedThickness: number;
|
23 |
+
export let value: null | AnnotatedImageData;
|
24 |
+
export let disableEditBoxes: boolean;
|
25 |
+
export let singleBox: boolean;
|
26 |
+
export let showRemoveButton: boolean;
|
27 |
+
export let handlesCursor: boolean;
|
28 |
+
|
29 |
+
let resolved_src: typeof src;
|
30 |
+
|
31 |
+
// The `src` prop can be updated before the Promise from `resolve_wasm_src` is resolved.
|
32 |
+
// In such a case, the resolved value for the old `src` has to be discarded,
|
33 |
+
// This variable `latest_src` is used to pick up only the value resolved for the latest `src` prop.
|
34 |
+
let latest_src: typeof src;
|
35 |
+
$: {
|
36 |
+
// In normal (non-Wasm) Gradio, the `<img>` element should be rendered with the passed `src` props immediately
|
37 |
+
// without waiting for `resolve_wasm_src()` to resolve.
|
38 |
+
// If it waits, a blank image is displayed until the async task finishes
|
39 |
+
// and it leads to undesirable flickering.
|
40 |
+
// So set `src` to `resolved_src` here.
|
41 |
+
resolved_src = src;
|
42 |
+
|
43 |
+
latest_src = src;
|
44 |
+
const resolving_src = src;
|
45 |
+
resolve_wasm_src(resolving_src).then((s) => {
|
46 |
+
if (latest_src === resolving_src) {
|
47 |
+
resolved_src = s;
|
48 |
+
}
|
49 |
+
});
|
50 |
+
}
|
51 |
+
|
52 |
+
const dispatch = createEventDispatcher<{
|
53 |
+
change: undefined;
|
54 |
+
}>();
|
55 |
+
|
56 |
+
</script>
|
57 |
+
|
58 |
+
<Canvas
|
59 |
+
bind:value
|
60 |
+
on:change={() => dispatch("change")}
|
61 |
+
{interactive}
|
62 |
+
boxAlpha={boxesAlpha}
|
63 |
+
choices={labelList}
|
64 |
+
choicesColors={labelColors}
|
65 |
+
{height}
|
66 |
+
{width}
|
67 |
+
{boxMinSize}
|
68 |
+
{handleSize}
|
69 |
+
{boxThickness}
|
70 |
+
{boxSelectedThickness}
|
71 |
+
{disableEditBoxes}
|
72 |
+
{singleBox}
|
73 |
+
{showRemoveButton}
|
74 |
+
{handlesCursor}
|
75 |
+
imageUrl={resolved_src}
|
76 |
+
/>
|
src/frontend/shared/ModalBox.svelte
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { BaseColorPicker } from "@gradio/colorpicker";
|
3 |
+
import { BaseButton } from "@gradio/button";
|
4 |
+
import { BaseDropdown } from "./patched_dropdown/Index.svelte";
|
5 |
+
import { createEventDispatcher } from "svelte";
|
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 |
+
export let showRemove = true;
|
15 |
+
|
16 |
+
const dispatch = createEventDispatcher<{
|
17 |
+
change: object;
|
18 |
+
}>();
|
19 |
+
|
20 |
+
function dispatchChange(ret: number) {
|
21 |
+
dispatch("change", {
|
22 |
+
label: currentLabel,
|
23 |
+
color: currentColor,
|
24 |
+
ret: ret // -1: remove, 0: cancel, 1: change
|
25 |
+
});
|
26 |
+
}
|
27 |
+
|
28 |
+
function onDropDownChange(event) {
|
29 |
+
const { detail } = event;
|
30 |
+
let choice = detail;
|
31 |
+
|
32 |
+
if (Number.isInteger(choice)) {
|
33 |
+
if (Array.isArray(choicesColors) && choice < choicesColors.length) {
|
34 |
+
currentColor = choicesColors[choice];
|
35 |
+
}
|
36 |
+
if (Array.isArray(choices) && choice < choices.length) {
|
37 |
+
currentLabel = choices[choice][0];
|
38 |
+
}
|
39 |
+
} else {
|
40 |
+
currentLabel = choice;
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
function onColorChange(event) {
|
45 |
+
const { detail } = event;
|
46 |
+
currentColor = detail;
|
47 |
+
}
|
48 |
+
|
49 |
+
function onDropDownEnter(event) {
|
50 |
+
onDropDownChange(event);
|
51 |
+
dispatchChange(1);
|
52 |
+
}
|
53 |
+
|
54 |
+
function handleKeyPress(event: KeyboardEvent) {
|
55 |
+
switch (event.key) {
|
56 |
+
case "Enter":
|
57 |
+
dispatchChange(1);
|
58 |
+
break;
|
59 |
+
}
|
60 |
+
}
|
61 |
+
|
62 |
+
onMount(() => {
|
63 |
+
document.addEventListener("keydown", handleKeyPress);
|
64 |
+
currentLabel = label;
|
65 |
+
currentColor = color;
|
66 |
+
});
|
67 |
+
|
68 |
+
onDestroy(() => {
|
69 |
+
document.removeEventListener("keydown", handleKeyPress);
|
70 |
+
});
|
71 |
+
|
72 |
+
</script>
|
73 |
+
|
74 |
+
<div class="modal" id="model-box-edit">
|
75 |
+
<div class="modal-container">
|
76 |
+
<span class="model-content">
|
77 |
+
<div style="margin-right: 10px;">
|
78 |
+
<BaseDropdown
|
79 |
+
value={currentLabel}
|
80 |
+
label="Label"
|
81 |
+
{choices}
|
82 |
+
show_label={false}
|
83 |
+
allow_custom_value={true}
|
84 |
+
on:change={onDropDownChange}
|
85 |
+
on:enter={onDropDownEnter}
|
86 |
+
/>
|
87 |
+
</div>
|
88 |
+
<div style="margin-right: 40px; margin-bottom: 8px;">
|
89 |
+
<BaseColorPicker
|
90 |
+
value={currentColor}
|
91 |
+
label="Color"
|
92 |
+
show_label={false}
|
93 |
+
on:change={onColorChange}
|
94 |
+
/>
|
95 |
+
</div>
|
96 |
+
<div style="margin-right: 8px;">
|
97 |
+
<BaseButton
|
98 |
+
on:click={() => dispatchChange(0)}
|
99 |
+
>Cancel</BaseButton>
|
100 |
+
</div>
|
101 |
+
{#if showRemove}
|
102 |
+
<div style="margin-right: 8px;">
|
103 |
+
<BaseButton
|
104 |
+
variant="stop"
|
105 |
+
on:click={() => dispatchChange(-1)}
|
106 |
+
>Remove</BaseButton>
|
107 |
+
</div>
|
108 |
+
{/if}
|
109 |
+
<div>
|
110 |
+
<BaseButton
|
111 |
+
variant="primary"
|
112 |
+
on:click={() => dispatchChange(1)}
|
113 |
+
>OK</BaseButton>
|
114 |
+
</div>
|
115 |
+
</span>
|
116 |
+
</div>
|
117 |
+
</div>
|
118 |
+
|
119 |
+
<style>
|
120 |
+
.modal {
|
121 |
+
position: fixed;
|
122 |
+
left: 0;
|
123 |
+
top: 0;
|
124 |
+
width: 100%;
|
125 |
+
height: 100%;
|
126 |
+
z-index: var(--layer-top);
|
127 |
+
-webkit-backdrop-filter: blur(4px);
|
128 |
+
backdrop-filter: blur(4px);
|
129 |
+
}
|
130 |
+
|
131 |
+
.modal-container {
|
132 |
+
border-style: solid;
|
133 |
+
border-width: var(--block-border-width);
|
134 |
+
border-color: var(--block-border-color);
|
135 |
+
margin-top: 10%;
|
136 |
+
padding: 20px;
|
137 |
+
box-shadow: var(--block-shadow);
|
138 |
+
border-color: var(--block-border-color);
|
139 |
+
border-radius: var(--block-radius);
|
140 |
+
background: var(--block-background-fill);
|
141 |
+
position: fixed;
|
142 |
+
left: 50%;
|
143 |
+
transform: translateX(-50%);
|
144 |
+
width: fit-content;
|
145 |
+
}
|
146 |
+
|
147 |
+
.model-content {
|
148 |
+
display: flex;
|
149 |
+
align-items: flex-end;
|
150 |
+
}
|
151 |
+
</style>
|
src/frontend/shared/icons/Add.svelte
ADDED
@@ -0,0 +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>
|
src/frontend/shared/icons/BoundingBox.svelte
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
<rect x="2" y="2" width="5" height="5" rx="1" ry="1" stroke-width="2" fill="none" />
|
13 |
+
<rect x="17" y="2" width="5" height="5" rx="1" ry="1" stroke-width="2" fill="none" />
|
14 |
+
<rect x="2" y="17" width="5" height="5" rx="1" ry="1" stroke-width="2" fill="none" />
|
15 |
+
<rect x="17" y="17" width="5" height="5" rx="1" ry="1" stroke-width="2" fill="none" />
|
16 |
+
|
17 |
+
<line x1="7.5" y1="4.5" x2="16" y2="4.5" style="stroke-width:2px;" />
|
18 |
+
<line x1="7.5" y1="19.5" x2="16" y2="19.5" style="stroke-width:2px;" />
|
19 |
+
<line x1="4.5" y1="8" x2="4.5" y2="16" style="stroke-width:2px;" />
|
20 |
+
<line x1="19.5" y1="8" x2="19.5" y2="16" style="stroke-width:2px;" />
|
21 |
+
</svg>
|
src/frontend/shared/icons/Hand.svelte
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
<path
|
13 |
+
d="M 14.4 2.85 V 11.1 V 3.95 C 14.4 3.0387 15.1388 2.3 16.05 2.3 C 16.9612 2.3 17.7 3.0387 17.7 3.95 V 11.1 V 7.25 C 17.7 6.3387 18.4388 5.6 19.35 5.6 C 20.2612 5.6 21 6.3387 21 7.25 V 16.6 C 21 20.2451 18.0451 23.2 14.4 23.2 H 13.16 C 11.4831 23.2 9.8692 22.5618 8.6459 21.4149 L 3.1915 16.3014 C 2.403 15.5622 2.3829 14.3171 3.1472 13.5528 C 3.8943 12.8057 5.1057 12.8057 5.8528 13.5528 L 7.8 15.5 V 6.15 C 7.8 5.2387 8.5387 4.5 9.45 4.5 C 10.3612 4.5 11.1 5.2387 11.1 6.15 V 11.1 V 2.85 C 11.1 1.9387 11.8388 1.2 12.75 1.2 C 13.6612 1.2 14.4 1.9387 14.4 2.85 Z"
|
14 |
+
fill="none"
|
15 |
+
stroke-width="2"
|
16 |
+
/>
|
17 |
+
</svg>
|
src/frontend/shared/icons/Trash.svelte
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
<path
|
13 |
+
d="M10 12L14 16M14 12L10 16M4 6H20M16 6L15.7294 5.18807C15.4671 4.40125 15.3359 4.00784 15.0927 3.71698C14.8779 3.46013 14.6021 3.26132 14.2905 3.13878C13.9376 3 13.523 3 12.6936 3H11.3064C10.477 3 10.0624 3 9.70951 3.13878C9.39792 3.26132 9.12208 3.46013 8.90729 3.71698C8.66405 4.00784 8.53292 4.40125 8.27064 5.18807L8 6M18 6V16.2C18 17.8802 18 18.7202 17.673 19.362C17.3854 19.9265 16.9265 20.3854 16.362 20.673C15.7202 21 14.8802 21 13.2 21H10.8C9.11984 21 8.27976 21 7.63803 20.673C7.07354 20.3854 6.6146 19.9265 6.32698 19.362C6 18.7202 6 17.8802 6 16.2V6"
|
14 |
+
fill="none"
|
15 |
+
stroke-width="2"
|
16 |
+
/>
|
17 |
+
</svg>
|
18 |
+
|
src/frontend/shared/icons/index.ts
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
export { default as Add } from "./Add.svelte";
|
2 |
+
export { default as BoundingBox } from "./BoundingBox.svelte";
|
3 |
+
export { default as Hand } from "./Hand.svelte";
|
4 |
+
export { default as Trash } from "./Trash.svelte";
|
src/frontend/shared/index.ts
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# @gradio/dropdown
|
2 |
+
|
3 |
+
## 0.6.3
|
4 |
+
|
5 |
+
### Fixes
|
6 |
+
|
7 |
+
- [#7567](https://github.com/gradio-app/gradio/pull/7567) [`e340894`](https://github.com/gradio-app/gradio/commit/e340894b1cf2f44dd45e597fd8d9e547f408fbb3) - Quick fix: custom dropdown value. Thanks [@dawoodkhan82](https://github.com/dawoodkhan82)!
|
8 |
+
|
9 |
+
## 0.6.2
|
10 |
+
|
11 |
+
### Patch Changes
|
12 |
+
|
13 |
+
- Updated dependencies [[`98a2719`](https://github.com/gradio-app/gradio/commit/98a2719bfb9c64338caf9009891b6c6b0b33ea89)]:
|
14 |
+
- @gradio/[email protected]
|
15 |
+
|
16 |
+
## 0.6.1
|
17 |
+
|
18 |
+
### Features
|
19 |
+
|
20 |
+
- [#7425](https://github.com/gradio-app/gradio/pull/7425) [`3e4e680`](https://github.com/gradio-app/gradio/commit/3e4e680a52ba5a73c108ef1b328dacd7b6e4b566) - Fixes to the `.key_up()` method to make it usable for a dynamic dropdown autocomplete. Thanks [@abidlabs](https://github.com/abidlabs)!
|
21 |
+
|
22 |
+
### Fixes
|
23 |
+
|
24 |
+
- [#7431](https://github.com/gradio-app/gradio/pull/7431) [`6b8a7e5`](https://github.com/gradio-app/gradio/commit/6b8a7e5d36887cdfcfbfec1536a915128df0d6b2) - Ensure `gr.Dropdown` can have an empty initial value. Thanks [@hannahblair](https://github.com/hannahblair)!
|
25 |
+
|
26 |
+
## 0.6.0
|
27 |
+
|
28 |
+
### Fixes
|
29 |
+
|
30 |
+
- [#7404](https://github.com/gradio-app/gradio/pull/7404) [`065c5b1`](https://github.com/gradio-app/gradio/commit/065c5b163c4badb9d9cbd06d627fb4ba086003e7) - Add `.key_up` event listener to `gr.Dropdown()`. Thanks [@abidlabs](https://github.com/abidlabs)!
|
31 |
+
- [#7401](https://github.com/gradio-app/gradio/pull/7401) [`dff4109`](https://github.com/gradio-app/gradio/commit/dff410955e41145848376784c03fe28ba1c4fd85) - Retain dropdown value if choices have been changed. Thanks [@abidlabs](https://github.com/abidlabs)!
|
32 |
+
|
33 |
+
## 0.5.2
|
34 |
+
|
35 |
+
### Fixes
|
36 |
+
|
37 |
+
- [#7192](https://github.com/gradio-app/gradio/pull/7192) [`8dd6f4b`](https://github.com/gradio-app/gradio/commit/8dd6f4bc1901792f05cd59e86df7b1dbab692739) - Handle the case where examples is `null` for all components. Thanks [@abidlabs](https://github.com/abidlabs)!
|
38 |
+
|
39 |
+
## 0.5.1
|
40 |
+
|
41 |
+
### Fixes
|
42 |
+
|
43 |
+
- [#7081](https://github.com/gradio-app/gradio/pull/7081) [`44c53d9`](https://github.com/gradio-app/gradio/commit/44c53d9bde7cab605b7dbd16331683d13cae029e) - Fix dropdown refocusing due to `<label />` element. Thanks [@hannahblair](https://github.com/hannahblair)!
|
44 |
+
|
45 |
+
## 0.5.0
|
46 |
+
|
47 |
+
### Fixes
|
48 |
+
|
49 |
+
- [#6933](https://github.com/gradio-app/gradio/pull/6933) [`9cefd2e`](https://github.com/gradio-app/gradio/commit/9cefd2e90a1d0cc4d3e4e953fc5b9b1a7afb68dd) - Refactor examples so they accept data in the same format as is returned by function, rename `.as_example()` to `.process_example()`. Thanks [@abidlabs](https://github.com/abidlabs)!
|
50 |
+
|
51 |
+
## 0.4.3
|
52 |
+
|
53 |
+
### Patch Changes
|
54 |
+
|
55 |
+
- Updated dependencies [[`828fb9e`](https://github.com/gradio-app/gradio/commit/828fb9e6ce15b6ea08318675a2361117596a1b5d), [`73268ee`](https://github.com/gradio-app/gradio/commit/73268ee2e39f23ebdd1e927cb49b8d79c4b9a144)]:
|
56 |
+
- @gradio/[email protected]
|
57 |
+
- @gradio/[email protected]
|
58 |
+
|
59 |
+
## 0.4.2
|
60 |
+
|
61 |
+
### Features
|
62 |
+
|
63 |
+
- [#6399](https://github.com/gradio-app/gradio/pull/6399) [`053bec9`](https://github.com/gradio-app/gradio/commit/053bec98be1127e083414024e02cf0bebb0b5142) - Improve CSS token documentation in Storybook. Thanks [@hannahblair](https://github.com/hannahblair)!
|
64 |
+
|
65 |
+
### Fixes
|
66 |
+
|
67 |
+
- [#6694](https://github.com/gradio-app/gradio/pull/6694) [`dfc61ec`](https://github.com/gradio-app/gradio/commit/dfc61ec4d09da72ddd6e7ab726820529621dbd38) - Fix dropdown blur bug when values are provided as tuples. Thanks [@abidlabs](https://github.com/abidlabs)!
|
68 |
+
|
69 |
+
## 0.4.1
|
70 |
+
|
71 |
+
### Patch Changes
|
72 |
+
|
73 |
+
- Updated dependencies [[`206af31`](https://github.com/gradio-app/gradio/commit/206af31d7c1a31013364a44e9b40cf8df304ba50)]:
|
74 |
+
- @gradio/[email protected]
|
75 |
+
- @gradio/[email protected]
|
76 |
+
- @gradio/[email protected]
|
77 |
+
|
78 |
+
## 0.4.0
|
79 |
+
|
80 |
+
### Features
|
81 |
+
|
82 |
+
- [#6517](https://github.com/gradio-app/gradio/pull/6517) [`901f3eebd`](https://github.com/gradio-app/gradio/commit/901f3eebda0a67fa8f3050d80f7f7b5800c7f566) - Allow reselecting the original option in `gr.Dropdown` after value has changed programmatically. Thanks [@abidlabs](https://github.com/abidlabs)!
|
83 |
+
|
84 |
+
## 0.3.3
|
85 |
+
|
86 |
+
### Patch Changes
|
87 |
+
|
88 |
+
- Updated dependencies [[`9caddc17b`](https://github.com/gradio-app/gradio/commit/9caddc17b1dea8da1af8ba724c6a5eab04ce0ed8)]:
|
89 |
+
- @gradio/[email protected]
|
90 |
+
- @gradio/[email protected]
|
91 |
+
- @gradio/[email protected]
|
92 |
+
|
93 |
+
## 0.3.2
|
94 |
+
|
95 |
+
### Fixes
|
96 |
+
|
97 |
+
- [#6425](https://github.com/gradio-app/gradio/pull/6425) [`b3ba17dd1`](https://github.com/gradio-app/gradio/commit/b3ba17dd1167d254756d93f1fb01e8be071819b6) - Update the selected indices in `Dropdown` when value changes programmatically. Thanks [@abidlabs](https://github.com/abidlabs)!
|
98 |
+
|
99 |
+
## 0.3.1
|
100 |
+
|
101 |
+
### Patch Changes
|
102 |
+
|
103 |
+
- Updated dependencies [[`3cdeabc68`](https://github.com/gradio-app/gradio/commit/3cdeabc6843000310e1a9e1d17190ecbf3bbc780), [`fad92c29d`](https://github.com/gradio-app/gradio/commit/fad92c29dc1f5cd84341aae417c495b33e01245f)]:
|
104 |
+
- @gradio/[email protected]
|
105 |
+
- @gradio/[email protected]
|
106 |
+
|
107 |
+
## 0.3.0
|
108 |
+
|
109 |
+
### Features
|
110 |
+
|
111 |
+
- [#5498](https://github.com/gradio-app/gradio/pull/5498) [`287fe6782`](https://github.com/gradio-app/gradio/commit/287fe6782825479513e79a5cf0ba0fbfe51443d7) - Publish all components to npm. Thanks [@pngwn](https://github.com/pngwn)!
|
112 |
+
- [#5498](https://github.com/gradio-app/gradio/pull/5498) [`287fe6782`](https://github.com/gradio-app/gradio/commit/287fe6782825479513e79a5cf0ba0fbfe51443d7) - Custom components. Thanks [@pngwn](https://github.com/pngwn)!
|
113 |
+
|
114 |
+
## 0.3.0-beta.8
|
115 |
+
|
116 |
+
### Features
|
117 |
+
|
118 |
+
- [#6136](https://github.com/gradio-app/gradio/pull/6136) [`667802a6c`](https://github.com/gradio-app/gradio/commit/667802a6cdbfb2ce454a3be5a78e0990b194548a) - JS Component Documentation. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
|
119 |
+
- [#6149](https://github.com/gradio-app/gradio/pull/6149) [`90318b1dd`](https://github.com/gradio-app/gradio/commit/90318b1dd118ae08a695a50e7c556226234ab6dc) - swap `mode` on the frontned to `interactive` to match the backend. Thanks [@pngwn](https://github.com/pngwn)!
|
120 |
+
|
121 |
+
### Fixes
|
122 |
+
|
123 |
+
- [#6148](https://github.com/gradio-app/gradio/pull/6148) [`0000a1916`](https://github.com/gradio-app/gradio/commit/0000a191688c5480c977c80acdd0c9023865d57e) - fix dropdown arrow size. Thanks [@pngwn](https://github.com/pngwn)!
|
124 |
+
|
125 |
+
## 0.3.0-beta.7
|
126 |
+
|
127 |
+
### Features
|
128 |
+
|
129 |
+
- [#6016](https://github.com/gradio-app/gradio/pull/6016) [`83e947676`](https://github.com/gradio-app/gradio/commit/83e947676d327ca2ab6ae2a2d710c78961c771a0) - Format js in v4 branch. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
|
130 |
+
|
131 |
+
### Fixes
|
132 |
+
|
133 |
+
- [#6065](https://github.com/gradio-app/gradio/pull/6065) [`7d07001e8`](https://github.com/gradio-app/gradio/commit/7d07001e8e7ca9cbd2251632667b3a043de49f49) - fix storybook. Thanks [@pngwn](https://github.com/pngwn)!
|
134 |
+
|
135 |
+
## 0.3.0-beta.6
|
136 |
+
|
137 |
+
### Features
|
138 |
+
|
139 |
+
- [#5960](https://github.com/gradio-app/gradio/pull/5960) [`319c30f3f`](https://github.com/gradio-app/gradio/commit/319c30f3fccf23bfe1da6c9b132a6a99d59652f7) - rererefactor frontend files. Thanks [@pngwn](https://github.com/pngwn)!
|
140 |
+
- [#5938](https://github.com/gradio-app/gradio/pull/5938) [`13ed8a485`](https://github.com/gradio-app/gradio/commit/13ed8a485d5e31d7d75af87fe8654b661edcca93) - V4: Use beta release versions for '@gradio' packages. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
|
141 |
+
|
142 |
+
## 0.3.3
|
143 |
+
|
144 |
+
### Fixes
|
145 |
+
|
146 |
+
- [#5839](https://github.com/gradio-app/gradio/pull/5839) [`b83064da0`](https://github.com/gradio-app/gradio/commit/b83064da0005ca055fc15ee478cf064bf91702a4) - Fix error when scrolling dropdown with scrollbar. Thanks [@Kit-p](https://github.com/Kit-p)!
|
147 |
+
|
148 |
+
## 0.3.2
|
149 |
+
|
150 |
+
### Patch Changes
|
151 |
+
|
152 |
+
- Updated dependencies []:
|
153 |
+
- @gradio/[email protected]
|
154 |
+
- @gradio/[email protected]
|
155 |
+
- @gradio/[email protected]
|
156 |
+
|
157 |
+
## 0.3.1
|
158 |
+
|
159 |
+
### Patch Changes
|
160 |
+
|
161 |
+
- Updated dependencies [[`8f0fed857`](https://github.com/gradio-app/gradio/commit/8f0fed857d156830626eb48b469d54d211a582d2)]:
|
162 |
+
- @gradio/[email protected]
|
163 |
+
- @gradio/[email protected]
|
164 |
+
- @gradio/[email protected]
|
165 |
+
|
166 |
+
## 0.3.0
|
167 |
+
|
168 |
+
### Features
|
169 |
+
|
170 |
+
- [#5554](https://github.com/gradio-app/gradio/pull/5554) [`75ddeb390`](https://github.com/gradio-app/gradio/commit/75ddeb390d665d4484667390a97442081b49a423) - Accessibility Improvements. Thanks [@hannahblair](https://github.com/hannahblair)!
|
171 |
+
|
172 |
+
## 0.2.2
|
173 |
+
|
174 |
+
### Fixes
|
175 |
+
|
176 |
+
- [#5544](https://github.com/gradio-app/gradio/pull/5544) [`a0cc9ac9`](https://github.com/gradio-app/gradio/commit/a0cc9ac931554e06dcb091158c9b9ac0cc580b6c) - Fixes dropdown breaking if a user types in invalid value and presses enter. Thanks [@abidlabs](https://github.com/abidlabs)!
|
177 |
+
|
178 |
+
## 0.2.1
|
179 |
+
|
180 |
+
### Fixes
|
181 |
+
|
182 |
+
- [#5525](https://github.com/gradio-app/gradio/pull/5525) [`21f1db40`](https://github.com/gradio-app/gradio/commit/21f1db40de6d1717eba97a550e11422a457ba7e9) - Ensure input value saves on dropdown blur. Thanks [@hannahblair](https://github.com/hannahblair)!
|
183 |
+
|
184 |
+
## 0.2.0
|
185 |
+
|
186 |
+
### Features
|
187 |
+
|
188 |
+
- [#5384](https://github.com/gradio-app/gradio/pull/5384) [`ddc02268`](https://github.com/gradio-app/gradio/commit/ddc02268f731bd2ed04b7a5854accf3383f9a0da) - Allows the `gr.Dropdown` to have separate names and values, as well as enables `allow_custom_value` for multiselect dropdown. Thanks [@abidlabs](https://github.com/abidlabs)!
|
189 |
+
|
190 |
+
### Fixes
|
191 |
+
|
192 |
+
- [#5508](https://github.com/gradio-app/gradio/pull/5508) [`05715f55`](https://github.com/gradio-app/gradio/commit/05715f5599ae3e928d3183c7b0a7f5291f843a96) - Adds a `filterable` parameter to `gr.Dropdown` that controls whether user can type to filter choices. Thanks [@abidlabs](https://github.com/abidlabs)!
|
193 |
+
|
194 |
+
## 0.1.3
|
195 |
+
|
196 |
+
### Patch Changes
|
197 |
+
|
198 |
+
- Updated dependencies [[`afac0006`](https://github.com/gradio-app/gradio/commit/afac0006337ce2840cf497cd65691f2f60ee5912)]:
|
199 |
+
- @gradio/[email protected]
|
200 |
+
- @gradio/[email protected]
|
201 |
+
- @gradio/[email protected]
|
202 |
+
|
203 |
+
## 0.1.2
|
204 |
+
|
205 |
+
### Fixes
|
206 |
+
|
207 |
+
- [#5360](https://github.com/gradio-app/gradio/pull/5360) [`64666525`](https://github.com/gradio-app/gradio/commit/6466652583e3c620df995fb865ef3511a34cb676) - Cancel Dropdown Filter. Thanks [@deckar01](https://github.com/deckar01)!
|
208 |
+
|
209 |
+
## 0.1.1
|
210 |
+
|
211 |
+
### Fixes
|
212 |
+
|
213 |
+
- [#5323](https://github.com/gradio-app/gradio/pull/5323) [`e32b0928`](https://github.com/gradio-app/gradio/commit/e32b0928d2d00342ca917ebb10c379ffc2ec200d) - ensure dropdown stays open when identical data is passed in. Thanks [@pngwn](https://github.com/pngwn)!
|
214 |
+
|
215 |
+
## 0.1.0
|
216 |
+
|
217 |
+
### Highlights
|
218 |
+
|
219 |
+
#### Improve startup performance and markdown support ([#5279](https://github.com/gradio-app/gradio/pull/5279) [`fe057300`](https://github.com/gradio-app/gradio/commit/fe057300f0672c62dab9d9b4501054ac5d45a4ec))
|
220 |
+
|
221 |
+
##### Improved markdown support
|
222 |
+
|
223 |
+
We now have better support for markdown in `gr.Markdown` and `gr.Dataframe`. Including syntax highlighting and Github Flavoured Markdown. We also have more consistent markdown behaviour and styling.
|
224 |
+
|
225 |
+
##### Various performance improvements
|
226 |
+
|
227 |
+
These improvements will be particularly beneficial to large applications.
|
228 |
+
|
229 |
+
- Rather than attaching events manually, they are now delegated, leading to a significant performance improvement and addressing a performance regression introduced in a recent version of Gradio. App startup for large applications is now around twice as fast.
|
230 |
+
- Optimised the mounting of individual components, leading to a modest performance improvement during startup (~30%).
|
231 |
+
- Corrected an issue that was causing markdown to re-render infinitely.
|
232 |
+
- Ensured that the `gr.3DModel` does re-render prematurely.
|
233 |
+
|
234 |
+
Thanks [@pngwn](https://github.com/pngwn)!
|
235 |
+
|
236 |
+
### Features
|
237 |
+
|
238 |
+
- [#5215](https://github.com/gradio-app/gradio/pull/5215) [`fbdad78a`](https://github.com/gradio-app/gradio/commit/fbdad78af4c47454cbb570f88cc14bf4479bbceb) - Lazy load interactive or static variants of a component individually, rather than loading both variants regardless. This change will improve performance for many applications. Thanks [@pngwn](https://github.com/pngwn)!
|
239 |
+
- [#5216](https://github.com/gradio-app/gradio/pull/5216) [`4b58ea6d`](https://github.com/gradio-app/gradio/commit/4b58ea6d98e7a43b3f30d8a4cb6f379bc2eca6a8) - Update i18n tokens and locale files. Thanks [@hannahblair](https://github.com/hannahblair)!
|
240 |
+
|
241 |
+
## 0.0.2
|
242 |
+
|
243 |
+
### Fixes
|
244 |
+
|
245 |
+
- [#5062](https://github.com/gradio-app/gradio/pull/5062) [`7d897165`](https://github.com/gradio-app/gradio/commit/7d89716519d0751072792c9bbda668ffeb597296) - `gr.Dropdown` now has correct behavior in static mode as well as when an option is selected. Thanks [@abidlabs](https://github.com/abidlabs)!
|
246 |
+
- [#5039](https://github.com/gradio-app/gradio/pull/5039) [`620e4645`](https://github.com/gradio-app/gradio/commit/620e46452729d6d4877b3fab84a65daf2f2b7bc6) - `gr.Dropdown()` now supports values with arbitrary characters and doesn't clear value when re-focused. Thanks [@abidlabs](https://github.com/abidlabs)!
|