@cloudpss/fetch 0.5.36 → 0.5.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/impl-browser.d.ts +6 -12
- package/dist/impl-browser.js +1 -7
- package/dist/impl-browser.js.map +1 -1
- package/dist/impl-node.js +1 -1
- package/dist/impl-node.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/ws-utils.d.ts +19 -2
- package/dist/ws-utils.js +72 -29
- package/dist/ws-utils.js.map +1 -1
- package/package.json +3 -3
- package/src/impl-browser.ts +1 -7
- package/src/impl-node.ts +1 -1
- package/src/index.ts +8 -1
- package/src/ws-utils.ts +65 -11
- package/tests/e2e.js +0 -46
- package/tests/ws/connencted.js +37 -0
- package/tests/ws/disconnect.js +35 -0
- package/tests/ws/e2e.js +36 -0
- package/tests/ws/state.js +37 -0
package/dist/impl-browser.d.ts
CHANGED
|
@@ -1,28 +1,22 @@
|
|
|
1
|
-
export declare const fetch: typeof globalThis.fetch
|
|
2
|
-
export declare const Headers: {
|
|
1
|
+
export declare const fetch: typeof globalThis.fetch, Headers: {
|
|
3
2
|
new (init?: HeadersInit): Headers;
|
|
4
3
|
prototype: Headers;
|
|
5
|
-
}
|
|
6
|
-
export declare const Response: {
|
|
4
|
+
}, Response: {
|
|
7
5
|
new (body?: BodyInit | null, init?: ResponseInit): Response;
|
|
8
6
|
prototype: Response;
|
|
9
7
|
error(): Response;
|
|
10
8
|
json(data: any, init?: ResponseInit): Response;
|
|
11
9
|
redirect(url: string | URL, status?: number): Response;
|
|
12
|
-
}
|
|
13
|
-
export declare const Request: {
|
|
10
|
+
}, Request: {
|
|
14
11
|
new (input: RequestInfo | URL, init?: RequestInit): Request;
|
|
15
12
|
prototype: Request;
|
|
16
|
-
}
|
|
17
|
-
export declare const File: {
|
|
13
|
+
}, File: {
|
|
18
14
|
new (fileBits: BlobPart[], fileName: string, options?: FilePropertyBag): File;
|
|
19
15
|
prototype: File;
|
|
20
|
-
}
|
|
21
|
-
export declare const FormData: {
|
|
16
|
+
}, FormData: {
|
|
22
17
|
new (form?: HTMLFormElement, submitter?: HTMLElement | null): FormData;
|
|
23
18
|
prototype: FormData;
|
|
24
|
-
}
|
|
25
|
-
export declare const WebSocket: {
|
|
19
|
+
}, WebSocket: {
|
|
26
20
|
new (url: string | URL, protocols?: string | string[]): WebSocket;
|
|
27
21
|
prototype: WebSocket;
|
|
28
22
|
readonly CONNECTING: 0;
|
package/dist/impl-browser.js
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
|
|
2
2
|
throw new TypeError(`globalThis or fetch is not defined on the global object, you should load polyfill of fetch.`);
|
|
3
3
|
}
|
|
4
|
-
export const fetch = globalThis
|
|
5
|
-
export const Headers = globalThis.Headers;
|
|
6
|
-
export const Response = globalThis.Response;
|
|
7
|
-
export const Request = globalThis.Request;
|
|
8
|
-
export const File = globalThis.File;
|
|
9
|
-
export const FormData = globalThis.FormData;
|
|
10
|
-
export const WebSocket = globalThis.WebSocket;
|
|
4
|
+
export const { fetch, Headers, Response, Request, File, FormData, WebSocket } = globalThis;
|
|
11
5
|
export default fetch;
|
|
12
6
|
//# sourceMappingURL=impl-browser.js.map
|
package/dist/impl-browser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"impl-browser.js","sourceRoot":"","sources":["../src/impl-browser.ts"],"names":[],"mappings":"AAAA,IAAI,OAAO,UAAU,IAAI,WAAW,IAAI,OAAO,UAAU,CAAC,KAAK,IAAI,UAAU,EAAE,CAAC;IAC5E,MAAM,IAAI,SAAS,CAAC,6FAA6F,CAAC,CAAC;AACvH,CAAC;AAED,MAAM,CAAC,MAAM,
|
|
1
|
+
{"version":3,"file":"impl-browser.js","sourceRoot":"","sources":["../src/impl-browser.ts"],"names":[],"mappings":"AAAA,IAAI,OAAO,UAAU,IAAI,WAAW,IAAI,OAAO,UAAU,CAAC,KAAK,IAAI,UAAU,EAAE,CAAC;IAC5E,MAAM,IAAI,SAAS,CAAC,6FAA6F,CAAC,CAAC;AACvH,CAAC;AAED,MAAM,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,UAAU,CAAC;AAE3F,eAAe,KAAK,CAAC"}
|
package/dist/impl-node.js
CHANGED
|
@@ -50,6 +50,7 @@ class AutoProxyAgent extends DispatcherBase {
|
|
|
50
50
|
}
|
|
51
51
|
const dispatcher = new AutoProxyAgent();
|
|
52
52
|
/** 自动支持代理的 fetch */
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
53
54
|
export const fetch = function (url, init) {
|
|
54
55
|
return nodeFetch(url, { ...init, dispatcher });
|
|
55
56
|
};
|
|
@@ -75,7 +76,6 @@ else {
|
|
|
75
76
|
let ws;
|
|
76
77
|
try {
|
|
77
78
|
if (typeof require == 'function') {
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
79
79
|
ws = require('ws');
|
|
80
80
|
}
|
|
81
81
|
else {
|
package/dist/impl-node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"impl-node.js","sourceRoot":"","sources":["../src/impl-node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA4B,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,cAAc,MAAM,0CAA0C,CAAC;AACtE,OAAO,OAAO,MAAM,4BAA4B,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD,MAAM,EACF,UAAU,EACV,KAAK,EACL,KAAK,EAAE,SAAS,EAChB,OAAO,EAAE,WAAW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AAM,CAAC,MAAM,SAAS,GAAG,UAAW,CAAC;AAErC,eAAe,KAAK,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { File, FormData, fetch, fetch as default, Request, Response, Headers, WebSocket } from '#impl';
|
|
2
|
-
export { connected, disconnected,
|
|
2
|
+
export { connected, disconnect, disconnected, WebSocketStatusCode, WebSocketError, type WebSocketStatus, } from './ws-utils.js';
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { File, FormData, fetch, fetch as default, Request, Response, Headers, WebSocket } from '#impl';
|
|
2
|
-
export { connected, disconnected,
|
|
2
|
+
export { connected, disconnect, disconnected, WebSocketStatusCode, WebSocketError, } from './ws-utils.js';
|
|
3
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,IAAI,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvG,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,IAAI,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvG,OAAO,EACH,SAAS,EACT,UAAU,EACV,YAAY,EACZ,mBAAmB,EACnB,cAAc,GAEjB,MAAM,eAAe,CAAC"}
|
package/dist/ws-utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare enum
|
|
1
|
+
export declare enum WebSocketStatusCode {
|
|
2
2
|
/** Status codes in the range 0-999 are not used. */
|
|
3
3
|
/**
|
|
4
4
|
1000 indicates a normal closure, meaning that the purpose for
|
|
@@ -129,5 +129,22 @@ export declare enum WEB_SOCKET_STATUS_CODES {
|
|
|
129
129
|
}
|
|
130
130
|
/** 等待连接建立 */
|
|
131
131
|
export declare function connected(socket: WebSocket): Promise<void>;
|
|
132
|
+
/** WebSocket 结束状态 */
|
|
133
|
+
export interface WebSocketStatus {
|
|
134
|
+
/** WebSocket connection close code */
|
|
135
|
+
code: WebSocketStatusCode;
|
|
136
|
+
/** WebSocket connection close reason */
|
|
137
|
+
reason: string;
|
|
138
|
+
}
|
|
139
|
+
/** WebSocket 结束状态 */
|
|
140
|
+
export declare class WebSocketError extends Error implements WebSocketStatus {
|
|
141
|
+
constructor(code: WebSocketStatusCode, reason: string);
|
|
142
|
+
/** WebSocket connection close code */
|
|
143
|
+
code: WebSocketStatusCode;
|
|
144
|
+
/** WebSocket connection close reason */
|
|
145
|
+
reason: string;
|
|
146
|
+
}
|
|
132
147
|
/** 等待连接断开 */
|
|
133
|
-
export declare function disconnected(socket: WebSocket): Promise<
|
|
148
|
+
export declare function disconnected(socket: WebSocket): Promise<WebSocketStatus>;
|
|
149
|
+
/** 断开连接 */
|
|
150
|
+
export declare function disconnect(socket: WebSocket, code?: WebSocketStatusCode, reason?: string): Promise<WebSocketStatus>;
|
package/dist/ws-utils.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
export var
|
|
2
|
-
(function (
|
|
1
|
+
export var WebSocketStatusCode;
|
|
2
|
+
(function (WebSocketStatusCode) {
|
|
3
3
|
/** Status codes in the range 0-999 are not used. */
|
|
4
4
|
/**
|
|
5
5
|
1000 indicates a normal closure, meaning that the purpose for
|
|
6
6
|
which the connection was established has been fulfilled.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
WebSocketStatusCode[WebSocketStatusCode["NORMAL_CLOSURE"] = 1000] = "NORMAL_CLOSURE";
|
|
9
9
|
/**
|
|
10
10
|
* 1001 indicates that an endpoint is "going away", such as a server
|
|
11
11
|
* going down or a browser having navigated away from a page.
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
WebSocketStatusCode[WebSocketStatusCode["GOING_AWAY"] = 1001] = "GOING_AWAY";
|
|
14
14
|
/**
|
|
15
15
|
* 1002 indicates that an endpoint is terminating the connection due
|
|
16
16
|
* to a protocol error.
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
WebSocketStatusCode[WebSocketStatusCode["PROTOCOL_ERROR"] = 1002] = "PROTOCOL_ERROR";
|
|
19
19
|
/**
|
|
20
20
|
* 1003 indicates that an endpoint is terminating the connection
|
|
21
21
|
* because it has received a type of data it cannot accept (e.g., an
|
|
22
22
|
* endpoint that understands only text data MAY send this if it
|
|
23
23
|
* receives a binary message).
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
WebSocketStatusCode[WebSocketStatusCode["UNSUPPORTED_DATA"] = 1003] = "UNSUPPORTED_DATA";
|
|
26
26
|
/** Reserved. The specific meaning might be defined in the future. */
|
|
27
|
-
|
|
27
|
+
WebSocketStatusCode[WebSocketStatusCode["RESERVED"] = 1004] = "RESERVED";
|
|
28
28
|
/**
|
|
29
29
|
* 1005 is a reserved value and MUST NOT be set as a status code in a
|
|
30
30
|
* Close control frame by an endpoint. It is designated for use in
|
|
31
31
|
* applications expecting a status code to indicate that no status
|
|
32
32
|
* code was actually present.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
WebSocketStatusCode[WebSocketStatusCode["NO_STATUS_RECEIVED"] = 1005] = "NO_STATUS_RECEIVED";
|
|
35
35
|
/**
|
|
36
36
|
* 1006 is a reserved value and MUST NOT be set as a status code in a
|
|
37
37
|
* Close control frame by an endpoint. It is designated for use in
|
|
@@ -39,14 +39,14 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
39
39
|
* connection was closed abnormally, e.g., without sending or
|
|
40
40
|
* receiving a Close control frame.
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
WebSocketStatusCode[WebSocketStatusCode["ABNORMAL_CLOSURE"] = 1006] = "ABNORMAL_CLOSURE";
|
|
43
43
|
/**
|
|
44
44
|
* 1007 indicates that an endpoint is terminating the connection
|
|
45
45
|
* because it has received data within a message that was not
|
|
46
46
|
* consistent with the type of the message (e.g., non-UTF-8 [RFC3629]
|
|
47
47
|
* data within a text message).
|
|
48
48
|
*/
|
|
49
|
-
|
|
49
|
+
WebSocketStatusCode[WebSocketStatusCode["INVALID_FRAME_PAYLOAD_DATA"] = 1007] = "INVALID_FRAME_PAYLOAD_DATA";
|
|
50
50
|
/**
|
|
51
51
|
* 1008 indicates that an endpoint is terminating the connection
|
|
52
52
|
* because it has received a message that violates its policy. This
|
|
@@ -54,13 +54,13 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
54
54
|
* other more suitable status code (e.g., 1003 or 1009) or if there
|
|
55
55
|
* is a need to hide specific details about the policy.
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
WebSocketStatusCode[WebSocketStatusCode["POLICY_VIOLATION"] = 1008] = "POLICY_VIOLATION";
|
|
58
58
|
/**
|
|
59
59
|
* 1009 indicates that an endpoint is terminating the connection
|
|
60
60
|
* because it has received a message that is too big for it to
|
|
61
61
|
* process.
|
|
62
62
|
*/
|
|
63
|
-
|
|
63
|
+
WebSocketStatusCode[WebSocketStatusCode["MESSAGE_TOO_BIG"] = 1009] = "MESSAGE_TOO_BIG";
|
|
64
64
|
/**
|
|
65
65
|
* 1010 indicates that an endpoint (client) is terminating the
|
|
66
66
|
* connection because it has expected the server to negotiate one or
|
|
@@ -70,27 +70,27 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
70
70
|
* Note that this status code is not used by the server, because it
|
|
71
71
|
* can fail the WebSocket handshake instead.
|
|
72
72
|
*/
|
|
73
|
-
|
|
73
|
+
WebSocketStatusCode[WebSocketStatusCode["MISSING_EXTENSION"] = 1010] = "MISSING_EXTENSION";
|
|
74
74
|
/**
|
|
75
75
|
* 1011 indicates that a server is terminating the connection because
|
|
76
76
|
* it encountered an unexpected condition that prevented it from
|
|
77
77
|
* fulfilling the request.
|
|
78
78
|
*/
|
|
79
|
-
|
|
79
|
+
WebSocketStatusCode[WebSocketStatusCode["INTERNAL_ERROR"] = 1011] = "INTERNAL_ERROR";
|
|
80
80
|
/**
|
|
81
81
|
* 1012 indicates that the service is restarted. A client may
|
|
82
82
|
* reconnect, and if it chooses to do, should reconnect using a
|
|
83
83
|
* randomized delay of 5 - 30s.
|
|
84
84
|
* @deprecated
|
|
85
85
|
*/
|
|
86
|
-
|
|
86
|
+
WebSocketStatusCode[WebSocketStatusCode["SERVICE_RESTART"] = 1012] = "SERVICE_RESTART";
|
|
87
87
|
/**
|
|
88
88
|
* 1013 indicates that the service is experiencing overload. A client
|
|
89
89
|
* should only connect to a different IP (when there are multiple for
|
|
90
90
|
* the target) or reconnect to the same IP upon user action.
|
|
91
91
|
* @deprecated
|
|
92
92
|
*/
|
|
93
|
-
|
|
93
|
+
WebSocketStatusCode[WebSocketStatusCode["TRY_AGAIN_LATER"] = 1013] = "TRY_AGAIN_LATER";
|
|
94
94
|
/**
|
|
95
95
|
* 1014 is a reserved value and MUST NOT be set as a status code in a
|
|
96
96
|
* Close control frame by an endpoint. It is designated for use in
|
|
@@ -99,7 +99,7 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
99
99
|
* (e.g., the server certificate can't be verified).
|
|
100
100
|
* @deprecated
|
|
101
101
|
*/
|
|
102
|
-
|
|
102
|
+
WebSocketStatusCode[WebSocketStatusCode["BAD_GATEWAY"] = 1014] = "BAD_GATEWAY";
|
|
103
103
|
/**
|
|
104
104
|
* 1015 is a reserved value and MUST NOT be set as a status code in a
|
|
105
105
|
* Close control frame by an endpoint. It is designated for use in
|
|
@@ -107,7 +107,7 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
107
107
|
* connection was closed due to a failure to perform a TLS handshake
|
|
108
108
|
* (e.g., the server certificate can't be verified).
|
|
109
109
|
*/
|
|
110
|
-
|
|
110
|
+
WebSocketStatusCode[WebSocketStatusCode["TLS_HANDSHAKE"] = 1015] = "TLS_HANDSHAKE";
|
|
111
111
|
/**
|
|
112
112
|
* Status codes in the range 1000-2999 are reserved for definition by
|
|
113
113
|
* this protocol, its future revisions, and extensions specified in a
|
|
@@ -119,15 +119,15 @@ export var WEB_SOCKET_STATUS_CODES;
|
|
|
119
119
|
* registered directly with IANA. The interpretation of these codes
|
|
120
120
|
* is undefined by this protocol.
|
|
121
121
|
*/
|
|
122
|
-
|
|
122
|
+
WebSocketStatusCode[WebSocketStatusCode["LIBRARY"] = 3000] = "LIBRARY";
|
|
123
123
|
/**
|
|
124
124
|
* Status codes in the range 4000-4999 are reserved for private use
|
|
125
125
|
* and thus can't be registered. Such codes can be used by prior
|
|
126
126
|
* agreements between WebSocket applications. The interpretation of
|
|
127
127
|
* these codes is undefined by this protocol.
|
|
128
128
|
*/
|
|
129
|
-
|
|
130
|
-
})(
|
|
129
|
+
WebSocketStatusCode[WebSocketStatusCode["APPLICATION"] = 4000] = "APPLICATION";
|
|
130
|
+
})(WebSocketStatusCode || (WebSocketStatusCode = {}));
|
|
131
131
|
/** 等待连接建立 */
|
|
132
132
|
export async function connected(socket) {
|
|
133
133
|
switch (socket.readyState) {
|
|
@@ -159,25 +159,53 @@ export async function connected(socket) {
|
|
|
159
159
|
throw new Error(`Socket is in unknown state.`);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
+
/** 视为 WebSocket 错误的 code */
|
|
163
|
+
function isWebSocketError(code) {
|
|
164
|
+
if (code === WebSocketStatusCode.NORMAL_CLOSURE)
|
|
165
|
+
return false;
|
|
166
|
+
if (code === WebSocketStatusCode.NO_STATUS_RECEIVED)
|
|
167
|
+
return false;
|
|
168
|
+
if (code >= WebSocketStatusCode.LIBRARY)
|
|
169
|
+
return false;
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
/** WebSocket 结束状态 */
|
|
173
|
+
export class WebSocketError extends Error {
|
|
174
|
+
constructor(code, reason) {
|
|
175
|
+
let message = reason;
|
|
176
|
+
if (!reason) {
|
|
177
|
+
if (code >= WebSocketStatusCode.LIBRARY) {
|
|
178
|
+
message = `WebSocket error (${code})`;
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
message = WebSocketStatusCode[code] || `WebSocket error (${code})`;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
super(message);
|
|
185
|
+
this.code = code;
|
|
186
|
+
this.reason = reason;
|
|
187
|
+
this.name = 'WebSocketError';
|
|
188
|
+
}
|
|
189
|
+
/** WebSocket connection close code */
|
|
190
|
+
code;
|
|
191
|
+
/** WebSocket connection close reason */
|
|
192
|
+
reason;
|
|
193
|
+
}
|
|
162
194
|
/** 等待连接断开 */
|
|
163
195
|
export async function disconnected(socket) {
|
|
164
196
|
switch (socket.readyState) {
|
|
165
197
|
case socket.CONNECTING:
|
|
166
|
-
await connected(socket);
|
|
167
|
-
return await disconnected(socket);
|
|
168
198
|
case socket.OPEN:
|
|
169
199
|
case socket.CLOSING:
|
|
170
200
|
return new Promise((resolve, reject) => {
|
|
171
201
|
const onClose = (ev) => {
|
|
172
202
|
const code = ev.code;
|
|
173
|
-
const reason = ev.reason ||
|
|
174
|
-
if (code
|
|
175
|
-
|
|
176
|
-
error.code = code;
|
|
177
|
-
reject(error);
|
|
203
|
+
const reason = ev.reason || '';
|
|
204
|
+
if (isWebSocketError(code)) {
|
|
205
|
+
reject(new WebSocketError(code, reason));
|
|
178
206
|
}
|
|
179
207
|
else {
|
|
180
|
-
resolve();
|
|
208
|
+
resolve({ code, reason });
|
|
181
209
|
}
|
|
182
210
|
cleanUp();
|
|
183
211
|
};
|
|
@@ -199,4 +227,19 @@ export async function disconnected(socket) {
|
|
|
199
227
|
throw new Error(`Socket is in unknown state.`);
|
|
200
228
|
}
|
|
201
229
|
}
|
|
230
|
+
/** 断开连接 */
|
|
231
|
+
export async function disconnect(socket, code, reason) {
|
|
232
|
+
switch (socket.readyState) {
|
|
233
|
+
case socket.CONNECTING:
|
|
234
|
+
case socket.OPEN:
|
|
235
|
+
socket.close(code, reason);
|
|
236
|
+
return await disconnected(socket);
|
|
237
|
+
case socket.CLOSING:
|
|
238
|
+
throw new Error(`Socket is closing.`);
|
|
239
|
+
case socket.CLOSED:
|
|
240
|
+
throw new Error(`Socket is closed.`);
|
|
241
|
+
default:
|
|
242
|
+
throw new Error(`Socket is in unknown state.`);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
202
245
|
//# sourceMappingURL=ws-utils.js.map
|
package/dist/ws-utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ws-utils.js","sourceRoot":"","sources":["../src/ws-utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,
|
|
1
|
+
{"version":3,"file":"ws-utils.js","sourceRoot":"","sources":["../src/ws-utils.ts"],"names":[],"mappings":"AAAA,MAAM,CAAN,IAAY,mBAmIX;AAnID,WAAY,mBAAmB;IAC3B,oDAAoD;IAEpD;;;OAGG;IACH,oFAAqB,CAAA;IACrB;;;OAGG;IACH,4EAAiB,CAAA;IACjB;;;OAGG;IACH,oFAAqB,CAAA;IACrB;;;;;OAKG;IACH,wFAAuB,CAAA;IACvB,sEAAsE;IACtE,wEAAe,CAAA;IACf;;;;;OAKG;IACH,4FAAyB,CAAA;IACzB;;;;;;OAMG;IACH,wFAAuB,CAAA;IACvB;;;;;OAKG;IACH,4GAAiC,CAAA;IACjC;;;;;;OAMG;IACH,wFAAuB,CAAA;IACvB;;;;OAIG;IACH,sFAAsB,CAAA;IACtB;;;;;;;;OAQG;IACH,0FAAwB,CAAA;IACxB;;;;OAIG;IACH,oFAAqB,CAAA;IACrB;;;;;OAKG;IACH,sFAAsB,CAAA;IACtB;;;;;OAKG;IACH,sFAAsB,CAAA;IACtB;;;;;;;OAOG;IACH,8EAAkB,CAAA;IAClB;;;;;;OAMG;IACH,kFAAoB,CAAA;IAEpB;;;;OAI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f,QAAQ,MAAM,CAAC,UAAU,EAAE,CAAC;QACxB,KAAK,MAAM,CAAC,UAAU,CAAC;QACvB,KAAK,MAAM,CAAC,IAAI;YACZ,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YAC3B,OAAO,MAAM,YAAY,CAAC,MAAM,CAAC,CAAC;QACtC,KAAK,MAAM,CAAC,OAAO;YACf,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QAC1C,KAAK,MAAM,CAAC,MAAM;YACd,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;QACzC;YACI,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACvD,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudpss/fetch",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.38",
|
|
4
4
|
"author": "CloudPSS",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"undici": "^6.19.
|
|
27
|
+
"undici": "^6.19.8"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@types/ws": "^8.5.
|
|
30
|
+
"@types/ws": "^8.5.12"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"start": "pnpm clean && tsc --watch",
|
package/src/impl-browser.ts
CHANGED
|
@@ -2,12 +2,6 @@ if (typeof globalThis == 'undefined' || typeof globalThis.fetch != 'function') {
|
|
|
2
2
|
throw new TypeError(`globalThis or fetch is not defined on the global object, you should load polyfill of fetch.`);
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
-
export const fetch = globalThis
|
|
6
|
-
export const Headers = globalThis.Headers;
|
|
7
|
-
export const Response = globalThis.Response;
|
|
8
|
-
export const Request = globalThis.Request;
|
|
9
|
-
export const File = globalThis.File;
|
|
10
|
-
export const FormData = globalThis.FormData;
|
|
11
|
-
export const WebSocket = globalThis.WebSocket;
|
|
5
|
+
export const { fetch, Headers, Response, Request, File, FormData, WebSocket } = globalThis;
|
|
12
6
|
|
|
13
7
|
export default fetch;
|
package/src/impl-node.ts
CHANGED
|
@@ -67,6 +67,7 @@ class AutoProxyAgent extends DispatcherBase {
|
|
|
67
67
|
const dispatcher = new AutoProxyAgent();
|
|
68
68
|
|
|
69
69
|
/** 自动支持代理的 fetch */
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/promise-function-async
|
|
70
71
|
export const fetch = function (url: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
|
71
72
|
return nodeFetch(
|
|
72
73
|
url as NodeRequestInfo,
|
|
@@ -95,7 +96,6 @@ if (NodeWebSocket != null) {
|
|
|
95
96
|
let ws: typeof import('ws') | undefined;
|
|
96
97
|
try {
|
|
97
98
|
if (typeof require == 'function') {
|
|
98
|
-
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
99
99
|
ws = require('ws') as typeof import('ws');
|
|
100
100
|
} else {
|
|
101
101
|
ws = createRequire(import.meta.url)('ws') as typeof import('ws');
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
export { File, FormData, fetch, fetch as default, Request, Response, Headers, WebSocket } from '#impl';
|
|
2
|
-
export {
|
|
2
|
+
export {
|
|
3
|
+
connected,
|
|
4
|
+
disconnect,
|
|
5
|
+
disconnected,
|
|
6
|
+
WebSocketStatusCode,
|
|
7
|
+
WebSocketError,
|
|
8
|
+
type WebSocketStatus,
|
|
9
|
+
} from './ws-utils.js';
|
package/src/ws-utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export enum
|
|
1
|
+
export enum WebSocketStatusCode {
|
|
2
2
|
/** Status codes in the range 0-999 are not used. */
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -163,24 +163,58 @@ export async function connected(socket: WebSocket): Promise<void> {
|
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/** 视为 WebSocket 错误的 code */
|
|
167
|
+
function isWebSocketError(code: WebSocketStatusCode): boolean {
|
|
168
|
+
if (code === WebSocketStatusCode.NORMAL_CLOSURE) return false;
|
|
169
|
+
if (code === WebSocketStatusCode.NO_STATUS_RECEIVED) return false;
|
|
170
|
+
if (code >= WebSocketStatusCode.LIBRARY) return false;
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** WebSocket 结束状态 */
|
|
175
|
+
export interface WebSocketStatus {
|
|
176
|
+
/** WebSocket connection close code */
|
|
177
|
+
code: WebSocketStatusCode;
|
|
178
|
+
/** WebSocket connection close reason */
|
|
179
|
+
reason: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** WebSocket 结束状态 */
|
|
183
|
+
export class WebSocketError extends Error implements WebSocketStatus {
|
|
184
|
+
constructor(code: WebSocketStatusCode, reason: string) {
|
|
185
|
+
let message = reason;
|
|
186
|
+
if (!reason) {
|
|
187
|
+
if (code >= WebSocketStatusCode.LIBRARY) {
|
|
188
|
+
message = `WebSocket error (${code})`;
|
|
189
|
+
} else {
|
|
190
|
+
message = WebSocketStatusCode[code] || `WebSocket error (${code})`;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
super(message);
|
|
194
|
+
this.code = code;
|
|
195
|
+
this.reason = reason;
|
|
196
|
+
this.name = 'WebSocketError';
|
|
197
|
+
}
|
|
198
|
+
/** WebSocket connection close code */
|
|
199
|
+
code: WebSocketStatusCode;
|
|
200
|
+
/** WebSocket connection close reason */
|
|
201
|
+
reason: string;
|
|
202
|
+
}
|
|
203
|
+
|
|
166
204
|
/** 等待连接断开 */
|
|
167
|
-
export async function disconnected(socket: WebSocket): Promise<
|
|
205
|
+
export async function disconnected(socket: WebSocket): Promise<WebSocketStatus> {
|
|
168
206
|
switch (socket.readyState) {
|
|
169
207
|
case socket.CONNECTING:
|
|
170
|
-
await connected(socket);
|
|
171
|
-
return await disconnected(socket);
|
|
172
208
|
case socket.OPEN:
|
|
173
209
|
case socket.CLOSING:
|
|
174
210
|
return new Promise((resolve, reject) => {
|
|
175
211
|
const onClose = (ev: CloseEvent): void => {
|
|
176
|
-
const code = ev.code as
|
|
177
|
-
const reason = ev.reason ||
|
|
178
|
-
if (code
|
|
179
|
-
|
|
180
|
-
(error as Error & { code: number }).code = code;
|
|
181
|
-
reject(error);
|
|
212
|
+
const code = ev.code as WebSocketStatusCode;
|
|
213
|
+
const reason = ev.reason || '';
|
|
214
|
+
if (isWebSocketError(code)) {
|
|
215
|
+
reject(new WebSocketError(code, reason));
|
|
182
216
|
} else {
|
|
183
|
-
resolve();
|
|
217
|
+
resolve({ code, reason });
|
|
184
218
|
}
|
|
185
219
|
cleanUp();
|
|
186
220
|
};
|
|
@@ -202,3 +236,23 @@ export async function disconnected(socket: WebSocket): Promise<void> {
|
|
|
202
236
|
throw new Error(`Socket is in unknown state.`);
|
|
203
237
|
}
|
|
204
238
|
}
|
|
239
|
+
|
|
240
|
+
/** 断开连接 */
|
|
241
|
+
export async function disconnect(
|
|
242
|
+
socket: WebSocket,
|
|
243
|
+
code?: WebSocketStatusCode,
|
|
244
|
+
reason?: string,
|
|
245
|
+
): Promise<WebSocketStatus> {
|
|
246
|
+
switch (socket.readyState) {
|
|
247
|
+
case socket.CONNECTING:
|
|
248
|
+
case socket.OPEN:
|
|
249
|
+
socket.close(code, reason);
|
|
250
|
+
return await disconnected(socket);
|
|
251
|
+
case socket.CLOSING:
|
|
252
|
+
throw new Error(`Socket is closing.`);
|
|
253
|
+
case socket.CLOSED:
|
|
254
|
+
throw new Error(`Socket is closed.`);
|
|
255
|
+
default:
|
|
256
|
+
throw new Error(`Socket is in unknown state.`);
|
|
257
|
+
}
|
|
258
|
+
}
|
package/tests/e2e.js
CHANGED
|
@@ -72,50 +72,4 @@ describe('node fetch works', () => {
|
|
|
72
72
|
}),
|
|
73
73
|
);
|
|
74
74
|
});
|
|
75
|
-
|
|
76
|
-
it('can access websocket echo server', async () => {
|
|
77
|
-
const messages = /** @type {string[]} */ ([]);
|
|
78
|
-
const ws = new Node.WebSocket('wss://echo.websocket.org');
|
|
79
|
-
expect(ws.readyState).toBe(Node.WebSocket.CONNECTING);
|
|
80
|
-
await Index.connected(ws);
|
|
81
|
-
expect(ws.readyState).toBe(Node.WebSocket.OPEN);
|
|
82
|
-
await Index.connected(ws);
|
|
83
|
-
expect(ws.readyState).toBe(Node.WebSocket.OPEN);
|
|
84
|
-
ws.send('hello');
|
|
85
|
-
ws.onmessage = (e) => {
|
|
86
|
-
messages.push(e.data);
|
|
87
|
-
if (messages.length === 2) {
|
|
88
|
-
ws.close(1000);
|
|
89
|
-
expect(ws.readyState).toBe(Node.WebSocket.CLOSING);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
await Index.disconnected(ws);
|
|
93
|
-
expect(ws.readyState).toBe(Node.WebSocket.CLOSED);
|
|
94
|
-
await expect(() => Index.disconnected(ws)).rejects.toThrow('Socket is closed.');
|
|
95
|
-
expect(messages).toContain('hello');
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('can throw on websocket error', async () => {
|
|
99
|
-
const ws = new Node.WebSocket('wss://echo.websocket.org');
|
|
100
|
-
await Index.connected(ws);
|
|
101
|
-
ws.close();
|
|
102
|
-
await expect(() => Index.disconnected(ws)).rejects.toThrow('NO_STATUS_RECEIVED');
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
it('can throw on websocket connection bas state', async () => {
|
|
106
|
-
const ws = new Node.WebSocket('wss://echo.websocket.org');
|
|
107
|
-
ws.close();
|
|
108
|
-
await expect(() => Index.connected(ws)).rejects.toThrow('Socket is closing.');
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('can throw on websocket disconnection bas state', async () => {
|
|
112
|
-
const ws = new Node.WebSocket('wss://echo.websocket.org');
|
|
113
|
-
ws.close();
|
|
114
|
-
await expect(() => Index.disconnected(ws)).rejects.toThrow('Received network error or non-101 status code.');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('can throw on websocket connection error', async () => {
|
|
118
|
-
const ws = new Node.WebSocket('wss://example.org');
|
|
119
|
-
await expect(() => Index.connected(ws)).rejects.toThrow('Received network error or non-101 status code.');
|
|
120
|
-
});
|
|
121
75
|
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { WebSocket, disconnected, connected } from '../../dist/index.js';
|
|
2
|
+
|
|
3
|
+
describe('connected works', () => {
|
|
4
|
+
it('can wait connected from CONNECTING state', async () => {
|
|
5
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
6
|
+
await expect(connected(ws)).resolves.toBeUndefined();
|
|
7
|
+
ws.close(1000);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('can wait connected from OPEN state', async () => {
|
|
11
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
12
|
+
await connected(ws);
|
|
13
|
+
await expect(connected(ws)).resolves.toBeUndefined();
|
|
14
|
+
ws.close(1000);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('can not wait connected from CLOSING state', async () => {
|
|
18
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
19
|
+
await connected(ws);
|
|
20
|
+
ws.close(1000);
|
|
21
|
+
await expect(connected(ws)).rejects.toThrow(`Socket is closing.`);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('can not wait connected from CLOSING state (direct close)', async () => {
|
|
25
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
26
|
+
ws.close(1000);
|
|
27
|
+
await expect(connected(ws)).rejects.toThrow(`Socket is closing.`);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('can not wait connected from CLOSED state', async () => {
|
|
31
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
32
|
+
await connected(ws);
|
|
33
|
+
ws.close(1000);
|
|
34
|
+
await disconnected(ws);
|
|
35
|
+
await expect(connected(ws)).rejects.toThrow(`Socket is closed.`);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { WebSocket, disconnected, connected, disconnect } from '../../dist/index.js';
|
|
2
|
+
|
|
3
|
+
describe('disconnect works', () => {
|
|
4
|
+
it('can disconnect from CONNECTING state', async () => {
|
|
5
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
6
|
+
await expect(disconnect(ws, 1000)).rejects.toThrow('Received network error or non-101 status code.');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('can disconnect from OPEN state', async () => {
|
|
10
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
11
|
+
await connected(ws);
|
|
12
|
+
await expect(disconnect(ws, 1000)).resolves.toEqual({ code: 1000, reason: '' });
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it('can not disconnect from CLOSING state', async () => {
|
|
16
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
17
|
+
await connected(ws);
|
|
18
|
+
ws.close(1000);
|
|
19
|
+
await expect(disconnect(ws, 1000)).rejects.toThrow(`Socket is closing.`);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('can not disconnect from CLOSING state (direct close)', async () => {
|
|
23
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
24
|
+
ws.close(1000);
|
|
25
|
+
await expect(disconnect(ws, 1000)).rejects.toThrow(`Socket is closing.`);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('can not disconnect from CLOSED state', async () => {
|
|
29
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
30
|
+
await connected(ws);
|
|
31
|
+
ws.close(1000);
|
|
32
|
+
await disconnected(ws);
|
|
33
|
+
await expect(disconnect(ws, 1000)).rejects.toThrow(`Socket is closed.`);
|
|
34
|
+
});
|
|
35
|
+
});
|
package/tests/ws/e2e.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { WebSocket, WebSocketStatusCode, disconnected, connected, disconnect } from '../../dist/index.js';
|
|
2
|
+
|
|
3
|
+
describe('websocket works', () => {
|
|
4
|
+
it('can access websocket echo server', async () => {
|
|
5
|
+
const messages = /** @type {string[]} */ ([]);
|
|
6
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
7
|
+
expect(ws.readyState).toBe(WebSocket.CONNECTING);
|
|
8
|
+
await connected(ws);
|
|
9
|
+
expect(ws.readyState).toBe(WebSocket.OPEN);
|
|
10
|
+
await connected(ws);
|
|
11
|
+
expect(ws.readyState).toBe(WebSocket.OPEN);
|
|
12
|
+
ws.send('hello');
|
|
13
|
+
// eslint-disable-next-line unicorn/prefer-add-event-listener
|
|
14
|
+
ws.onmessage = (e) => {
|
|
15
|
+
messages.push(e.data);
|
|
16
|
+
if (messages.length === 2) {
|
|
17
|
+
ws.close(1000, 'normal');
|
|
18
|
+
expect(ws.readyState).toBe(WebSocket.CLOSING);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
await expect(disconnected(ws)).resolves.toEqual({
|
|
22
|
+
code: WebSocketStatusCode.NORMAL_CLOSURE,
|
|
23
|
+
reason: '',
|
|
24
|
+
});
|
|
25
|
+
expect(messages).toContain('hello');
|
|
26
|
+
expect(ws.readyState).toBe(WebSocket.CLOSED);
|
|
27
|
+
await expect(disconnected(ws)).rejects.toThrow('Socket is closed.');
|
|
28
|
+
await expect(connected(ws)).rejects.toThrow('Socket is closed.');
|
|
29
|
+
await expect(disconnect(ws, 1000)).rejects.toThrow(`Socket is closed.`);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('can throw on websocket connection error', async () => {
|
|
33
|
+
const ws = new WebSocket('wss://example.org');
|
|
34
|
+
await expect(connected(ws)).rejects.toThrow('Received network error or non-101 status code.');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { WebSocket, WebSocketStatusCode, disconnected, connected } from '../../dist/index.js';
|
|
2
|
+
|
|
3
|
+
describe('connected/disconnected works', () => {
|
|
4
|
+
it('can resolve 1005', async () => {
|
|
5
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
6
|
+
await connected(ws);
|
|
7
|
+
ws.close();
|
|
8
|
+
await expect(disconnected(ws)).resolves.toEqual({
|
|
9
|
+
code: WebSocketStatusCode.NO_STATUS_RECEIVED,
|
|
10
|
+
reason: '',
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('can resolve 3000', async () => {
|
|
15
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
16
|
+
await connected(ws);
|
|
17
|
+
ws.close(3000);
|
|
18
|
+
await expect(disconnected(ws)).resolves.toEqual({
|
|
19
|
+
code: 3000,
|
|
20
|
+
reason: '',
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('can wait disconnect from CONNECTING state', async () => {
|
|
25
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
26
|
+
const d = disconnected(ws);
|
|
27
|
+
await connected(ws);
|
|
28
|
+
ws.close(1000);
|
|
29
|
+
await expect(d).resolves.toEqual({ code: 1000, reason: '' });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('can throw on websocket disconnection bad state', async () => {
|
|
33
|
+
const ws = new WebSocket('wss://echo.websocket.org');
|
|
34
|
+
ws.close();
|
|
35
|
+
await expect(disconnected(ws)).rejects.toThrow('Received network error or non-101 status code.');
|
|
36
|
+
});
|
|
37
|
+
});
|