hono 4.6.6 → 4.6.7
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/adapter/bun/websocket.js +23 -26
- package/dist/adapter/cloudflare-workers/websocket.js +40 -39
- package/dist/adapter/deno/websocket.js +7 -8
- package/dist/adapter/vercel/handler.js +2 -2
- package/dist/cjs/adapter/bun/websocket.js +24 -26
- package/dist/cjs/adapter/cloudflare-workers/websocket.js +40 -39
- package/dist/cjs/adapter/deno/websocket.js +7 -8
- package/dist/cjs/adapter/vercel/handler.js +2 -2
- package/dist/cjs/helper/websocket/index.js +43 -2
- package/dist/cjs/router/linear-router/router.js +75 -77
- package/dist/cjs/router/pattern-router/router.js +3 -2
- package/dist/cjs/router/smart-router/router.js +3 -3
- package/dist/cjs/router/trie-router/node.js +9 -12
- package/dist/cjs/router/trie-router/router.js +2 -2
- package/dist/cjs/utils/jwt/jws.js +4 -2
- package/dist/helper/websocket/index.js +40 -1
- package/dist/router/linear-router/router.js +75 -77
- package/dist/router/pattern-router/router.js +3 -2
- package/dist/router/smart-router/router.js +3 -3
- package/dist/router/trie-router/node.js +9 -12
- package/dist/router/trie-router/router.js +2 -2
- package/dist/types/adapter/bun/websocket.d.ts +9 -1
- package/dist/types/adapter/vercel/handler.d.ts +1 -2
- package/dist/types/helper/websocket/index.d.ts +35 -6
- package/dist/types/router/trie-router/node.d.ts +0 -2
- package/dist/utils/jwt/jws.js +4 -2
- package/package.json +1 -1
|
@@ -1,44 +1,40 @@
|
|
|
1
1
|
// src/adapter/bun/websocket.ts
|
|
2
|
-
import { createWSMessageEvent } from "../../helper/websocket/index.js";
|
|
2
|
+
import { createWSMessageEvent, defineWebSocketHelper, WSContext } from "../../helper/websocket/index.js";
|
|
3
3
|
import { getBunServer } from "./server.js";
|
|
4
4
|
var createWSContext = (ws) => {
|
|
5
|
-
return {
|
|
5
|
+
return new WSContext({
|
|
6
6
|
send: (source, options) => {
|
|
7
|
-
|
|
8
|
-
ws.send(sendingData, options?.compress);
|
|
7
|
+
ws.send(source, options?.compress);
|
|
9
8
|
},
|
|
10
9
|
raw: ws,
|
|
11
|
-
binaryType: "arraybuffer",
|
|
12
10
|
readyState: ws.readyState,
|
|
13
11
|
url: ws.data.url,
|
|
14
12
|
protocol: ws.data.protocol,
|
|
15
13
|
close(code, reason) {
|
|
16
14
|
ws.close(code, reason);
|
|
17
15
|
}
|
|
18
|
-
};
|
|
16
|
+
});
|
|
19
17
|
};
|
|
20
18
|
var createBunWebSocket = () => {
|
|
21
19
|
const websocketConns = [];
|
|
22
|
-
const upgradeWebSocket = (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
protocol: c.req.url
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
if (upgradeResult) {
|
|
37
|
-
return new Response(null);
|
|
20
|
+
const upgradeWebSocket = defineWebSocketHelper((c, events) => {
|
|
21
|
+
const server = getBunServer(c);
|
|
22
|
+
if (!server) {
|
|
23
|
+
throw new TypeError("env has to include the 2nd argument of fetch.");
|
|
24
|
+
}
|
|
25
|
+
const connId = websocketConns.push(events) - 1;
|
|
26
|
+
const upgradeResult = server.upgrade(c.req.raw, {
|
|
27
|
+
data: {
|
|
28
|
+
connId,
|
|
29
|
+
url: new URL(c.req.url),
|
|
30
|
+
protocol: c.req.url
|
|
38
31
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
});
|
|
33
|
+
if (upgradeResult) {
|
|
34
|
+
return new Response(null);
|
|
35
|
+
}
|
|
36
|
+
return;
|
|
37
|
+
});
|
|
42
38
|
const websocket = {
|
|
43
39
|
open(ws) {
|
|
44
40
|
const websocketListeners = websocketConns[ws.data.connId];
|
|
@@ -75,5 +71,6 @@ var createBunWebSocket = () => {
|
|
|
75
71
|
};
|
|
76
72
|
};
|
|
77
73
|
export {
|
|
78
|
-
createBunWebSocket
|
|
74
|
+
createBunWebSocket,
|
|
75
|
+
createWSContext
|
|
79
76
|
};
|
|
@@ -1,44 +1,45 @@
|
|
|
1
1
|
// src/adapter/cloudflare-workers/websocket.ts
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { WSContext, defineWebSocketHelper } from "../../helper/websocket/index.js";
|
|
3
|
+
var upgradeWebSocket = defineWebSocketHelper(
|
|
4
|
+
async (c, events) => {
|
|
5
|
+
const upgradeHeader = c.req.header("Upgrade");
|
|
6
|
+
if (upgradeHeader !== "websocket") {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const webSocketPair = new WebSocketPair();
|
|
10
|
+
const client = webSocketPair[0];
|
|
11
|
+
const server = webSocketPair[1];
|
|
12
|
+
const wsContext = new WSContext({
|
|
13
|
+
close: (code, reason) => server.close(code, reason),
|
|
14
|
+
get protocol() {
|
|
15
|
+
return server.protocol;
|
|
16
|
+
},
|
|
17
|
+
raw: server,
|
|
18
|
+
get readyState() {
|
|
19
|
+
return server.readyState;
|
|
20
|
+
},
|
|
21
|
+
url: server.url ? new URL(server.url) : null,
|
|
22
|
+
send: (source) => server.send(source)
|
|
23
|
+
});
|
|
24
|
+
if (events.onOpen) {
|
|
25
|
+
server.addEventListener("open", (evt) => events.onOpen?.(evt, wsContext));
|
|
26
|
+
}
|
|
27
|
+
if (events.onClose) {
|
|
28
|
+
server.addEventListener("close", (evt) => events.onClose?.(evt, wsContext));
|
|
29
|
+
}
|
|
30
|
+
if (events.onMessage) {
|
|
31
|
+
server.addEventListener("message", (evt) => events.onMessage?.(evt, wsContext));
|
|
32
|
+
}
|
|
33
|
+
if (events.onError) {
|
|
34
|
+
server.addEventListener("error", (evt) => events.onError?.(evt, wsContext));
|
|
35
|
+
}
|
|
36
|
+
server.accept?.();
|
|
37
|
+
return new Response(null, {
|
|
38
|
+
status: 101,
|
|
39
|
+
webSocket: client
|
|
40
|
+
});
|
|
7
41
|
}
|
|
8
|
-
|
|
9
|
-
const client = webSocketPair[0];
|
|
10
|
-
const server = webSocketPair[1];
|
|
11
|
-
const wsContext = {
|
|
12
|
-
binaryType: "arraybuffer",
|
|
13
|
-
close: (code, reason) => server.close(code, reason),
|
|
14
|
-
get protocol() {
|
|
15
|
-
return server.protocol;
|
|
16
|
-
},
|
|
17
|
-
raw: server,
|
|
18
|
-
get readyState() {
|
|
19
|
-
return server.readyState;
|
|
20
|
-
},
|
|
21
|
-
url: server.url ? new URL(server.url) : null,
|
|
22
|
-
send: (source) => server.send(source)
|
|
23
|
-
};
|
|
24
|
-
if (events.onOpen) {
|
|
25
|
-
server.addEventListener("open", (evt) => events.onOpen?.(evt, wsContext));
|
|
26
|
-
}
|
|
27
|
-
if (events.onClose) {
|
|
28
|
-
server.addEventListener("close", (evt) => events.onClose?.(evt, wsContext));
|
|
29
|
-
}
|
|
30
|
-
if (events.onMessage) {
|
|
31
|
-
server.addEventListener("message", (evt) => events.onMessage?.(evt, wsContext));
|
|
32
|
-
}
|
|
33
|
-
if (events.onError) {
|
|
34
|
-
server.addEventListener("error", (evt) => events.onError?.(evt, wsContext));
|
|
35
|
-
}
|
|
36
|
-
server.accept?.();
|
|
37
|
-
return new Response(null, {
|
|
38
|
-
status: 101,
|
|
39
|
-
webSocket: client
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
+
);
|
|
42
43
|
export {
|
|
43
44
|
upgradeWebSocket
|
|
44
45
|
};
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
// src/adapter/deno/websocket.ts
|
|
2
|
-
|
|
2
|
+
import { WSContext, defineWebSocketHelper } from "../../helper/websocket/index.js";
|
|
3
|
+
var upgradeWebSocket = defineWebSocketHelper(async (c, events, options) => {
|
|
3
4
|
if (c.req.header("upgrade") !== "websocket") {
|
|
4
|
-
return
|
|
5
|
+
return;
|
|
5
6
|
}
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const wsContext = {
|
|
9
|
-
binaryType: "arraybuffer",
|
|
7
|
+
const { response, socket } = Deno.upgradeWebSocket(c.req.raw, options ?? {});
|
|
8
|
+
const wsContext = new WSContext({
|
|
10
9
|
close: (code, reason) => socket.close(code, reason),
|
|
11
10
|
get protocol() {
|
|
12
11
|
return socket.protocol;
|
|
@@ -17,13 +16,13 @@ var upgradeWebSocket = (createEvents, options) => async (c, next) => {
|
|
|
17
16
|
},
|
|
18
17
|
url: socket.url ? new URL(socket.url) : null,
|
|
19
18
|
send: (source) => socket.send(source)
|
|
20
|
-
};
|
|
19
|
+
});
|
|
21
20
|
socket.onopen = (evt) => events.onOpen?.(evt, wsContext);
|
|
22
21
|
socket.onmessage = (evt) => events.onMessage?.(evt, wsContext);
|
|
23
22
|
socket.onclose = (evt) => events.onClose?.(evt, wsContext);
|
|
24
23
|
socket.onerror = (evt) => events.onError?.(evt, wsContext);
|
|
25
24
|
return response;
|
|
26
|
-
};
|
|
25
|
+
});
|
|
27
26
|
export {
|
|
28
27
|
upgradeWebSocket
|
|
29
28
|
};
|
|
@@ -18,49 +18,46 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var websocket_exports = {};
|
|
20
20
|
__export(websocket_exports, {
|
|
21
|
-
createBunWebSocket: () => createBunWebSocket
|
|
21
|
+
createBunWebSocket: () => createBunWebSocket,
|
|
22
|
+
createWSContext: () => createWSContext
|
|
22
23
|
});
|
|
23
24
|
module.exports = __toCommonJS(websocket_exports);
|
|
24
25
|
var import_websocket = require("../../helper/websocket");
|
|
25
26
|
var import_server = require("./server");
|
|
26
27
|
const createWSContext = (ws) => {
|
|
27
|
-
return {
|
|
28
|
+
return new import_websocket.WSContext({
|
|
28
29
|
send: (source, options) => {
|
|
29
|
-
|
|
30
|
-
ws.send(sendingData, options?.compress);
|
|
30
|
+
ws.send(source, options?.compress);
|
|
31
31
|
},
|
|
32
32
|
raw: ws,
|
|
33
|
-
binaryType: "arraybuffer",
|
|
34
33
|
readyState: ws.readyState,
|
|
35
34
|
url: ws.data.url,
|
|
36
35
|
protocol: ws.data.protocol,
|
|
37
36
|
close(code, reason) {
|
|
38
37
|
ws.close(code, reason);
|
|
39
38
|
}
|
|
40
|
-
};
|
|
39
|
+
});
|
|
41
40
|
};
|
|
42
41
|
const createBunWebSocket = () => {
|
|
43
42
|
const websocketConns = [];
|
|
44
|
-
const upgradeWebSocket = (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
protocol: c.req.url
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
if (upgradeResult) {
|
|
59
|
-
return new Response(null);
|
|
43
|
+
const upgradeWebSocket = (0, import_websocket.defineWebSocketHelper)((c, events) => {
|
|
44
|
+
const server = (0, import_server.getBunServer)(c);
|
|
45
|
+
if (!server) {
|
|
46
|
+
throw new TypeError("env has to include the 2nd argument of fetch.");
|
|
47
|
+
}
|
|
48
|
+
const connId = websocketConns.push(events) - 1;
|
|
49
|
+
const upgradeResult = server.upgrade(c.req.raw, {
|
|
50
|
+
data: {
|
|
51
|
+
connId,
|
|
52
|
+
url: new URL(c.req.url),
|
|
53
|
+
protocol: c.req.url
|
|
60
54
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
55
|
+
});
|
|
56
|
+
if (upgradeResult) {
|
|
57
|
+
return new Response(null);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
});
|
|
64
61
|
const websocket = {
|
|
65
62
|
open(ws) {
|
|
66
63
|
const websocketListeners = websocketConns[ws.data.connId];
|
|
@@ -98,5 +95,6 @@ const createBunWebSocket = () => {
|
|
|
98
95
|
};
|
|
99
96
|
// Annotate the CommonJS export names for ESM import in node:
|
|
100
97
|
0 && (module.exports = {
|
|
101
|
-
createBunWebSocket
|
|
98
|
+
createBunWebSocket,
|
|
99
|
+
createWSContext
|
|
102
100
|
});
|
|
@@ -21,46 +21,47 @@ __export(websocket_exports, {
|
|
|
21
21
|
upgradeWebSocket: () => upgradeWebSocket
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(websocket_exports);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
var import_websocket = require("../../helper/websocket");
|
|
25
|
+
const upgradeWebSocket = (0, import_websocket.defineWebSocketHelper)(
|
|
26
|
+
async (c, events) => {
|
|
27
|
+
const upgradeHeader = c.req.header("Upgrade");
|
|
28
|
+
if (upgradeHeader !== "websocket") {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const webSocketPair = new WebSocketPair();
|
|
32
|
+
const client = webSocketPair[0];
|
|
33
|
+
const server = webSocketPair[1];
|
|
34
|
+
const wsContext = new import_websocket.WSContext({
|
|
35
|
+
close: (code, reason) => server.close(code, reason),
|
|
36
|
+
get protocol() {
|
|
37
|
+
return server.protocol;
|
|
38
|
+
},
|
|
39
|
+
raw: server,
|
|
40
|
+
get readyState() {
|
|
41
|
+
return server.readyState;
|
|
42
|
+
},
|
|
43
|
+
url: server.url ? new URL(server.url) : null,
|
|
44
|
+
send: (source) => server.send(source)
|
|
45
|
+
});
|
|
46
|
+
if (events.onOpen) {
|
|
47
|
+
server.addEventListener("open", (evt) => events.onOpen?.(evt, wsContext));
|
|
48
|
+
}
|
|
49
|
+
if (events.onClose) {
|
|
50
|
+
server.addEventListener("close", (evt) => events.onClose?.(evt, wsContext));
|
|
51
|
+
}
|
|
52
|
+
if (events.onMessage) {
|
|
53
|
+
server.addEventListener("message", (evt) => events.onMessage?.(evt, wsContext));
|
|
54
|
+
}
|
|
55
|
+
if (events.onError) {
|
|
56
|
+
server.addEventListener("error", (evt) => events.onError?.(evt, wsContext));
|
|
57
|
+
}
|
|
58
|
+
server.accept?.();
|
|
59
|
+
return new Response(null, {
|
|
60
|
+
status: 101,
|
|
61
|
+
webSocket: client
|
|
62
|
+
});
|
|
29
63
|
}
|
|
30
|
-
|
|
31
|
-
const client = webSocketPair[0];
|
|
32
|
-
const server = webSocketPair[1];
|
|
33
|
-
const wsContext = {
|
|
34
|
-
binaryType: "arraybuffer",
|
|
35
|
-
close: (code, reason) => server.close(code, reason),
|
|
36
|
-
get protocol() {
|
|
37
|
-
return server.protocol;
|
|
38
|
-
},
|
|
39
|
-
raw: server,
|
|
40
|
-
get readyState() {
|
|
41
|
-
return server.readyState;
|
|
42
|
-
},
|
|
43
|
-
url: server.url ? new URL(server.url) : null,
|
|
44
|
-
send: (source) => server.send(source)
|
|
45
|
-
};
|
|
46
|
-
if (events.onOpen) {
|
|
47
|
-
server.addEventListener("open", (evt) => events.onOpen?.(evt, wsContext));
|
|
48
|
-
}
|
|
49
|
-
if (events.onClose) {
|
|
50
|
-
server.addEventListener("close", (evt) => events.onClose?.(evt, wsContext));
|
|
51
|
-
}
|
|
52
|
-
if (events.onMessage) {
|
|
53
|
-
server.addEventListener("message", (evt) => events.onMessage?.(evt, wsContext));
|
|
54
|
-
}
|
|
55
|
-
if (events.onError) {
|
|
56
|
-
server.addEventListener("error", (evt) => events.onError?.(evt, wsContext));
|
|
57
|
-
}
|
|
58
|
-
server.accept?.();
|
|
59
|
-
return new Response(null, {
|
|
60
|
-
status: 101,
|
|
61
|
-
webSocket: client
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
+
);
|
|
64
65
|
// Annotate the CommonJS export names for ESM import in node:
|
|
65
66
|
0 && (module.exports = {
|
|
66
67
|
upgradeWebSocket
|
|
@@ -21,14 +21,13 @@ __export(websocket_exports, {
|
|
|
21
21
|
upgradeWebSocket: () => upgradeWebSocket
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(websocket_exports);
|
|
24
|
-
|
|
24
|
+
var import_websocket = require("../../helper/websocket");
|
|
25
|
+
const upgradeWebSocket = (0, import_websocket.defineWebSocketHelper)(async (c, events, options) => {
|
|
25
26
|
if (c.req.header("upgrade") !== "websocket") {
|
|
26
|
-
return
|
|
27
|
+
return;
|
|
27
28
|
}
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const wsContext = {
|
|
31
|
-
binaryType: "arraybuffer",
|
|
29
|
+
const { response, socket } = Deno.upgradeWebSocket(c.req.raw, options ?? {});
|
|
30
|
+
const wsContext = new import_websocket.WSContext({
|
|
32
31
|
close: (code, reason) => socket.close(code, reason),
|
|
33
32
|
get protocol() {
|
|
34
33
|
return socket.protocol;
|
|
@@ -39,13 +38,13 @@ const upgradeWebSocket = (createEvents, options) => async (c, next) => {
|
|
|
39
38
|
},
|
|
40
39
|
url: socket.url ? new URL(socket.url) : null,
|
|
41
40
|
send: (source) => socket.send(source)
|
|
42
|
-
};
|
|
41
|
+
});
|
|
43
42
|
socket.onopen = (evt) => events.onOpen?.(evt, wsContext);
|
|
44
43
|
socket.onmessage = (evt) => events.onMessage?.(evt, wsContext);
|
|
45
44
|
socket.onclose = (evt) => events.onClose?.(evt, wsContext);
|
|
46
45
|
socket.onerror = (evt) => events.onError?.(evt, wsContext);
|
|
47
46
|
return response;
|
|
48
|
-
};
|
|
47
|
+
});
|
|
49
48
|
// Annotate the CommonJS export names for ESM import in node:
|
|
50
49
|
0 && (module.exports = {
|
|
51
50
|
upgradeWebSocket
|
|
@@ -21,8 +21,8 @@ __export(handler_exports, {
|
|
|
21
21
|
handle: () => handle
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(handler_exports);
|
|
24
|
-
const handle = (app) => (req
|
|
25
|
-
return app.fetch(req
|
|
24
|
+
const handle = (app) => (req) => {
|
|
25
|
+
return app.fetch(req);
|
|
26
26
|
};
|
|
27
27
|
// Annotate the CommonJS export names for ESM import in node:
|
|
28
28
|
0 && (module.exports = {
|
|
@@ -18,15 +18,56 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var websocket_exports = {};
|
|
20
20
|
__export(websocket_exports, {
|
|
21
|
-
|
|
21
|
+
WSContext: () => WSContext,
|
|
22
|
+
createWSMessageEvent: () => createWSMessageEvent,
|
|
23
|
+
defineWebSocketHelper: () => defineWebSocketHelper
|
|
22
24
|
});
|
|
23
25
|
module.exports = __toCommonJS(websocket_exports);
|
|
26
|
+
class WSContext {
|
|
27
|
+
#init;
|
|
28
|
+
constructor(init) {
|
|
29
|
+
this.#init = init;
|
|
30
|
+
this.raw = init.raw;
|
|
31
|
+
this.url = init.url ? new URL(init.url) : null;
|
|
32
|
+
this.protocol = init.protocol ?? null;
|
|
33
|
+
}
|
|
34
|
+
send(source, options) {
|
|
35
|
+
this.#init.send(
|
|
36
|
+
typeof source === "string" ? source : source instanceof Uint8Array ? source.buffer : source,
|
|
37
|
+
options ?? {}
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
raw;
|
|
41
|
+
binaryType = "arraybuffer";
|
|
42
|
+
get readyState() {
|
|
43
|
+
return this.#init.readyState;
|
|
44
|
+
}
|
|
45
|
+
url;
|
|
46
|
+
protocol;
|
|
47
|
+
close(code, reason) {
|
|
48
|
+
this.#init.close(code, reason);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
24
51
|
const createWSMessageEvent = (source) => {
|
|
25
52
|
return new MessageEvent("message", {
|
|
26
53
|
data: source
|
|
27
54
|
});
|
|
28
55
|
};
|
|
56
|
+
const defineWebSocketHelper = (handler) => {
|
|
57
|
+
return (createEvents, options) => {
|
|
58
|
+
return async function UpgradeWebSocket(c, next) {
|
|
59
|
+
const events = await createEvents(c);
|
|
60
|
+
const result = await handler(c, events, options);
|
|
61
|
+
if (result) {
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
await next();
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
29
68
|
// Annotate the CommonJS export names for ESM import in node:
|
|
30
69
|
0 && (module.exports = {
|
|
31
|
-
|
|
70
|
+
WSContext,
|
|
71
|
+
createWSMessageEvent,
|
|
72
|
+
defineWebSocketHelper
|
|
32
73
|
});
|
|
@@ -30,104 +30,102 @@ class LinearRouter {
|
|
|
30
30
|
name = "LinearRouter";
|
|
31
31
|
routes = [];
|
|
32
32
|
add(method, path, handler) {
|
|
33
|
-
;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
});
|
|
33
|
+
for (let i = 0, paths = (0, import_url.checkOptionalParameter)(path) || [path], len = paths.length; i < len; i++) {
|
|
34
|
+
this.routes.push([method, paths[i], handler]);
|
|
35
|
+
}
|
|
37
36
|
}
|
|
38
37
|
match(method, path) {
|
|
39
38
|
const handlers = [];
|
|
40
39
|
ROUTES_LOOP:
|
|
41
40
|
for (let i = 0, len = this.routes.length; i < len; i++) {
|
|
42
41
|
const [routeMethod, routePath, handler] = this.routes[i];
|
|
43
|
-
if (routeMethod
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
if (routePath === "*" || routePath === "/*") {
|
|
47
|
-
handlers.push([handler, emptyParams]);
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
const hasStar = routePath.indexOf("*") !== -1;
|
|
51
|
-
const hasLabel = routePath.indexOf(":") !== -1;
|
|
52
|
-
if (!hasStar && !hasLabel) {
|
|
53
|
-
if (routePath === path || routePath + "/" === path) {
|
|
42
|
+
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
|
|
43
|
+
if (routePath === "*" || routePath === "/*") {
|
|
54
44
|
handlers.push([handler, emptyParams]);
|
|
45
|
+
continue;
|
|
55
46
|
}
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const part = parts[j];
|
|
62
|
-
const index = path.indexOf(part, pos);
|
|
63
|
-
if (index !== pos) {
|
|
64
|
-
continue ROUTES_LOOP;
|
|
47
|
+
const hasStar = routePath.indexOf("*") !== -1;
|
|
48
|
+
const hasLabel = routePath.indexOf(":") !== -1;
|
|
49
|
+
if (!hasStar && !hasLabel) {
|
|
50
|
+
if (routePath === path || routePath + "/" === path) {
|
|
51
|
+
handlers.push([handler, emptyParams]);
|
|
65
52
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
53
|
+
} else if (hasStar && !hasLabel) {
|
|
54
|
+
const endsWithStar = routePath.charCodeAt(routePath.length - 1) === 42;
|
|
55
|
+
const parts = (endsWithStar ? routePath.slice(0, -2) : routePath).split(splitByStarRe);
|
|
56
|
+
const lastIndex = parts.length - 1;
|
|
57
|
+
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
58
|
+
const part = parts[j];
|
|
59
|
+
const index = path.indexOf(part, pos);
|
|
60
|
+
if (index !== pos) {
|
|
74
61
|
continue ROUTES_LOOP;
|
|
75
62
|
}
|
|
76
|
-
pos
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
handlers.push([handler, emptyParams]);
|
|
80
|
-
} else if (hasLabel && !hasStar) {
|
|
81
|
-
const params = /* @__PURE__ */ Object.create(null);
|
|
82
|
-
const parts = routePath.match(splitPathRe);
|
|
83
|
-
const lastIndex = parts.length - 1;
|
|
84
|
-
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
85
|
-
if (pos === -1 || pos >= path.length) {
|
|
86
|
-
continue ROUTES_LOOP;
|
|
87
|
-
}
|
|
88
|
-
const part = parts[j];
|
|
89
|
-
if (part.charCodeAt(1) === 58) {
|
|
90
|
-
let name = part.slice(2);
|
|
91
|
-
let value;
|
|
92
|
-
if (name.charCodeAt(name.length - 1) === 125) {
|
|
93
|
-
const openBracePos = name.indexOf("{");
|
|
94
|
-
const pattern = name.slice(openBracePos + 1, -1);
|
|
95
|
-
const restPath = path.slice(pos + 1);
|
|
96
|
-
const match = new RegExp(pattern, "d").exec(restPath);
|
|
97
|
-
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
|
63
|
+
pos += part.length;
|
|
64
|
+
if (j === lastIndex) {
|
|
65
|
+
if (!endsWithStar && pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
|
98
66
|
continue ROUTES_LOOP;
|
|
99
67
|
}
|
|
100
|
-
name = name.slice(0, openBracePos);
|
|
101
|
-
value = restPath.slice(...match.indices[0]);
|
|
102
|
-
pos += match.indices[0][1] + 1;
|
|
103
68
|
} else {
|
|
104
|
-
|
|
105
|
-
if (
|
|
106
|
-
|
|
107
|
-
continue ROUTES_LOOP;
|
|
108
|
-
}
|
|
109
|
-
endValuePos = path.length;
|
|
69
|
+
const index2 = path.indexOf("/", pos);
|
|
70
|
+
if (index2 === -1) {
|
|
71
|
+
continue ROUTES_LOOP;
|
|
110
72
|
}
|
|
111
|
-
|
|
112
|
-
pos = endValuePos;
|
|
113
|
-
}
|
|
114
|
-
params[name] ||= value;
|
|
115
|
-
} else {
|
|
116
|
-
const index = path.indexOf(part, pos);
|
|
117
|
-
if (index !== pos) {
|
|
118
|
-
continue ROUTES_LOOP;
|
|
73
|
+
pos = index2;
|
|
119
74
|
}
|
|
120
|
-
pos += part.length;
|
|
121
75
|
}
|
|
122
|
-
|
|
123
|
-
|
|
76
|
+
handlers.push([handler, emptyParams]);
|
|
77
|
+
} else if (hasLabel && !hasStar) {
|
|
78
|
+
const params = /* @__PURE__ */ Object.create(null);
|
|
79
|
+
const parts = routePath.match(splitPathRe);
|
|
80
|
+
const lastIndex = parts.length - 1;
|
|
81
|
+
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
82
|
+
if (pos === -1 || pos >= path.length) {
|
|
124
83
|
continue ROUTES_LOOP;
|
|
125
84
|
}
|
|
85
|
+
const part = parts[j];
|
|
86
|
+
if (part.charCodeAt(1) === 58) {
|
|
87
|
+
let name = part.slice(2);
|
|
88
|
+
let value;
|
|
89
|
+
if (name.charCodeAt(name.length - 1) === 125) {
|
|
90
|
+
const openBracePos = name.indexOf("{");
|
|
91
|
+
const pattern = name.slice(openBracePos + 1, -1);
|
|
92
|
+
const restPath = path.slice(pos + 1);
|
|
93
|
+
const match = new RegExp(pattern, "d").exec(restPath);
|
|
94
|
+
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
|
95
|
+
continue ROUTES_LOOP;
|
|
96
|
+
}
|
|
97
|
+
name = name.slice(0, openBracePos);
|
|
98
|
+
value = restPath.slice(...match.indices[0]);
|
|
99
|
+
pos += match.indices[0][1] + 1;
|
|
100
|
+
} else {
|
|
101
|
+
let endValuePos = path.indexOf("/", pos + 1);
|
|
102
|
+
if (endValuePos === -1) {
|
|
103
|
+
if (pos + 1 === path.length) {
|
|
104
|
+
continue ROUTES_LOOP;
|
|
105
|
+
}
|
|
106
|
+
endValuePos = path.length;
|
|
107
|
+
}
|
|
108
|
+
value = path.slice(pos + 1, endValuePos);
|
|
109
|
+
pos = endValuePos;
|
|
110
|
+
}
|
|
111
|
+
params[name] ||= value;
|
|
112
|
+
} else {
|
|
113
|
+
const index = path.indexOf(part, pos);
|
|
114
|
+
if (index !== pos) {
|
|
115
|
+
continue ROUTES_LOOP;
|
|
116
|
+
}
|
|
117
|
+
pos += part.length;
|
|
118
|
+
}
|
|
119
|
+
if (j === lastIndex) {
|
|
120
|
+
if (pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
|
121
|
+
continue ROUTES_LOOP;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
126
124
|
}
|
|
125
|
+
handlers.push([handler, params]);
|
|
126
|
+
} else if (hasLabel && hasStar) {
|
|
127
|
+
throw new import_router.UnsupportedPathError();
|
|
127
128
|
}
|
|
128
|
-
handlers.push([handler, params]);
|
|
129
|
-
} else if (hasLabel && hasStar) {
|
|
130
|
-
throw new import_router.UnsupportedPathError();
|
|
131
129
|
}
|
|
132
130
|
}
|
|
133
131
|
return [handlers];
|
|
@@ -50,8 +50,9 @@ class PatternRouter {
|
|
|
50
50
|
}
|
|
51
51
|
match(method, path) {
|
|
52
52
|
const handlers = [];
|
|
53
|
-
for (
|
|
54
|
-
|
|
53
|
+
for (let i = 0, len = this.routes.length; i < len; i++) {
|
|
54
|
+
const [pattern, routeMethod, handler] = this.routes[i];
|
|
55
|
+
if (routeMethod === method || routeMethod === import_router.METHOD_NAME_ALL) {
|
|
55
56
|
const match = pattern.exec(path);
|
|
56
57
|
if (match) {
|
|
57
58
|
handlers.push([handler, match.groups || /* @__PURE__ */ Object.create(null)]);
|
|
@@ -46,9 +46,9 @@ class SmartRouter {
|
|
|
46
46
|
for (; i < len; i++) {
|
|
47
47
|
const router = routers[i];
|
|
48
48
|
try {
|
|
49
|
-
routes.
|
|
50
|
-
router.add(...
|
|
51
|
-
}
|
|
49
|
+
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
|
|
50
|
+
router.add(...routes[i2]);
|
|
51
|
+
}
|
|
52
52
|
res = router.match(method, path);
|
|
53
53
|
} catch (e) {
|
|
54
54
|
if (e instanceof import_router.UnsupportedPathError) {
|
|
@@ -28,21 +28,18 @@ class Node {
|
|
|
28
28
|
children;
|
|
29
29
|
patterns;
|
|
30
30
|
order = 0;
|
|
31
|
-
name;
|
|
32
31
|
params = /* @__PURE__ */ Object.create(null);
|
|
33
32
|
constructor(method, handler, children) {
|
|
34
33
|
this.children = children || /* @__PURE__ */ Object.create(null);
|
|
35
34
|
this.methods = [];
|
|
36
|
-
this.name = "";
|
|
37
35
|
if (method && handler) {
|
|
38
36
|
const m = /* @__PURE__ */ Object.create(null);
|
|
39
|
-
m[method] = { handler, possibleKeys: [], score: 0
|
|
37
|
+
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
40
38
|
this.methods = [m];
|
|
41
39
|
}
|
|
42
40
|
this.patterns = [];
|
|
43
41
|
}
|
|
44
42
|
insert(method, path, handler) {
|
|
45
|
-
this.name = `${method} ${path}`;
|
|
46
43
|
this.order = ++this.order;
|
|
47
44
|
let curNode = this;
|
|
48
45
|
const parts = (0, import_url.splitRoutingPath)(path);
|
|
@@ -72,7 +69,6 @@ class Node {
|
|
|
72
69
|
const handlerSet = {
|
|
73
70
|
handler,
|
|
74
71
|
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
75
|
-
name: this.name,
|
|
76
72
|
score: this.order
|
|
77
73
|
};
|
|
78
74
|
m[method] = handlerSet;
|
|
@@ -87,11 +83,12 @@ class Node {
|
|
|
87
83
|
const processedSet = /* @__PURE__ */ Object.create(null);
|
|
88
84
|
if (handlerSet !== void 0) {
|
|
89
85
|
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
90
|
-
handlerSet.possibleKeys.
|
|
91
|
-
const
|
|
86
|
+
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
87
|
+
const key = handlerSet.possibleKeys[i2];
|
|
88
|
+
const processed = processedSet[handlerSet.score];
|
|
92
89
|
handlerSet.params[key] = params[key] && !processed ? params[key] : nodeParams[key] ?? params[key];
|
|
93
|
-
processedSet[handlerSet.
|
|
94
|
-
}
|
|
90
|
+
processedSet[handlerSet.score] = true;
|
|
91
|
+
}
|
|
95
92
|
handlerSets.push(handlerSet);
|
|
96
93
|
}
|
|
97
94
|
}
|
|
@@ -112,7 +109,7 @@ class Node {
|
|
|
112
109
|
const nextNode = node.children[part];
|
|
113
110
|
if (nextNode) {
|
|
114
111
|
nextNode.params = node.params;
|
|
115
|
-
if (isLast
|
|
112
|
+
if (isLast) {
|
|
116
113
|
if (nextNode.children["*"]) {
|
|
117
114
|
handlerSets.push(
|
|
118
115
|
...this.gHSets(nextNode.children["*"], method, node.params, /* @__PURE__ */ Object.create(null))
|
|
@@ -145,10 +142,10 @@ class Node {
|
|
|
145
142
|
handlerSets.push(...this.gHSets(child, method, node.params, params));
|
|
146
143
|
continue;
|
|
147
144
|
}
|
|
148
|
-
if (matcher === true || matcher
|
|
145
|
+
if (matcher === true || matcher.test(part)) {
|
|
149
146
|
if (typeof key === "string") {
|
|
150
147
|
params[name] = part;
|
|
151
|
-
if (isLast
|
|
148
|
+
if (isLast) {
|
|
152
149
|
handlerSets.push(...this.gHSets(child, method, params, node.params));
|
|
153
150
|
if (child.children["*"]) {
|
|
154
151
|
handlerSets.push(...this.gHSets(child.children["*"], method, params, node.params));
|
|
@@ -32,8 +32,8 @@ class TrieRouter {
|
|
|
32
32
|
add(method, path, handler) {
|
|
33
33
|
const results = (0, import_url.checkOptionalParameter)(path);
|
|
34
34
|
if (results) {
|
|
35
|
-
for (
|
|
36
|
-
this.node.insert(method,
|
|
35
|
+
for (let i = 0, len = results.length; i < len; i++) {
|
|
36
|
+
this.node.insert(method, results[i], handler);
|
|
37
37
|
}
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
@@ -44,8 +44,10 @@ async function importPrivateKey(key, alg) {
|
|
|
44
44
|
throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
|
|
45
45
|
}
|
|
46
46
|
if (isCryptoKey(key)) {
|
|
47
|
-
if (key.type !== "private") {
|
|
48
|
-
throw new Error(
|
|
47
|
+
if (key.type !== "private" && key.type !== "secret") {
|
|
48
|
+
throw new Error(
|
|
49
|
+
`unexpected key type: CryptoKey.type is ${key.type}, expected private or secret`
|
|
50
|
+
);
|
|
49
51
|
}
|
|
50
52
|
return key;
|
|
51
53
|
}
|
|
@@ -1,9 +1,48 @@
|
|
|
1
1
|
// src/helper/websocket/index.ts
|
|
2
|
+
var WSContext = class {
|
|
3
|
+
#init;
|
|
4
|
+
constructor(init) {
|
|
5
|
+
this.#init = init;
|
|
6
|
+
this.raw = init.raw;
|
|
7
|
+
this.url = init.url ? new URL(init.url) : null;
|
|
8
|
+
this.protocol = init.protocol ?? null;
|
|
9
|
+
}
|
|
10
|
+
send(source, options) {
|
|
11
|
+
this.#init.send(
|
|
12
|
+
typeof source === "string" ? source : source instanceof Uint8Array ? source.buffer : source,
|
|
13
|
+
options ?? {}
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
raw;
|
|
17
|
+
binaryType = "arraybuffer";
|
|
18
|
+
get readyState() {
|
|
19
|
+
return this.#init.readyState;
|
|
20
|
+
}
|
|
21
|
+
url;
|
|
22
|
+
protocol;
|
|
23
|
+
close(code, reason) {
|
|
24
|
+
this.#init.close(code, reason);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
2
27
|
var createWSMessageEvent = (source) => {
|
|
3
28
|
return new MessageEvent("message", {
|
|
4
29
|
data: source
|
|
5
30
|
});
|
|
6
31
|
};
|
|
32
|
+
var defineWebSocketHelper = (handler) => {
|
|
33
|
+
return (createEvents, options) => {
|
|
34
|
+
return async function UpgradeWebSocket(c, next) {
|
|
35
|
+
const events = await createEvents(c);
|
|
36
|
+
const result = await handler(c, events, options);
|
|
37
|
+
if (result) {
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
await next();
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
};
|
|
7
44
|
export {
|
|
8
|
-
|
|
45
|
+
WSContext,
|
|
46
|
+
createWSMessageEvent,
|
|
47
|
+
defineWebSocketHelper
|
|
9
48
|
};
|
|
@@ -8,104 +8,102 @@ var LinearRouter = class {
|
|
|
8
8
|
name = "LinearRouter";
|
|
9
9
|
routes = [];
|
|
10
10
|
add(method, path, handler) {
|
|
11
|
-
;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
});
|
|
11
|
+
for (let i = 0, paths = checkOptionalParameter(path) || [path], len = paths.length; i < len; i++) {
|
|
12
|
+
this.routes.push([method, paths[i], handler]);
|
|
13
|
+
}
|
|
15
14
|
}
|
|
16
15
|
match(method, path) {
|
|
17
16
|
const handlers = [];
|
|
18
17
|
ROUTES_LOOP:
|
|
19
18
|
for (let i = 0, len = this.routes.length; i < len; i++) {
|
|
20
19
|
const [routeMethod, routePath, handler] = this.routes[i];
|
|
21
|
-
if (routeMethod
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
if (routePath === "*" || routePath === "/*") {
|
|
25
|
-
handlers.push([handler, emptyParams]);
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
const hasStar = routePath.indexOf("*") !== -1;
|
|
29
|
-
const hasLabel = routePath.indexOf(":") !== -1;
|
|
30
|
-
if (!hasStar && !hasLabel) {
|
|
31
|
-
if (routePath === path || routePath + "/" === path) {
|
|
20
|
+
if (routeMethod === method || routeMethod === METHOD_NAME_ALL) {
|
|
21
|
+
if (routePath === "*" || routePath === "/*") {
|
|
32
22
|
handlers.push([handler, emptyParams]);
|
|
23
|
+
continue;
|
|
33
24
|
}
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const part = parts[j];
|
|
40
|
-
const index = path.indexOf(part, pos);
|
|
41
|
-
if (index !== pos) {
|
|
42
|
-
continue ROUTES_LOOP;
|
|
25
|
+
const hasStar = routePath.indexOf("*") !== -1;
|
|
26
|
+
const hasLabel = routePath.indexOf(":") !== -1;
|
|
27
|
+
if (!hasStar && !hasLabel) {
|
|
28
|
+
if (routePath === path || routePath + "/" === path) {
|
|
29
|
+
handlers.push([handler, emptyParams]);
|
|
43
30
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
if (
|
|
31
|
+
} else if (hasStar && !hasLabel) {
|
|
32
|
+
const endsWithStar = routePath.charCodeAt(routePath.length - 1) === 42;
|
|
33
|
+
const parts = (endsWithStar ? routePath.slice(0, -2) : routePath).split(splitByStarRe);
|
|
34
|
+
const lastIndex = parts.length - 1;
|
|
35
|
+
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
36
|
+
const part = parts[j];
|
|
37
|
+
const index = path.indexOf(part, pos);
|
|
38
|
+
if (index !== pos) {
|
|
52
39
|
continue ROUTES_LOOP;
|
|
53
40
|
}
|
|
54
|
-
pos
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
handlers.push([handler, emptyParams]);
|
|
58
|
-
} else if (hasLabel && !hasStar) {
|
|
59
|
-
const params = /* @__PURE__ */ Object.create(null);
|
|
60
|
-
const parts = routePath.match(splitPathRe);
|
|
61
|
-
const lastIndex = parts.length - 1;
|
|
62
|
-
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
63
|
-
if (pos === -1 || pos >= path.length) {
|
|
64
|
-
continue ROUTES_LOOP;
|
|
65
|
-
}
|
|
66
|
-
const part = parts[j];
|
|
67
|
-
if (part.charCodeAt(1) === 58) {
|
|
68
|
-
let name = part.slice(2);
|
|
69
|
-
let value;
|
|
70
|
-
if (name.charCodeAt(name.length - 1) === 125) {
|
|
71
|
-
const openBracePos = name.indexOf("{");
|
|
72
|
-
const pattern = name.slice(openBracePos + 1, -1);
|
|
73
|
-
const restPath = path.slice(pos + 1);
|
|
74
|
-
const match = new RegExp(pattern, "d").exec(restPath);
|
|
75
|
-
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
|
41
|
+
pos += part.length;
|
|
42
|
+
if (j === lastIndex) {
|
|
43
|
+
if (!endsWithStar && pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
|
76
44
|
continue ROUTES_LOOP;
|
|
77
45
|
}
|
|
78
|
-
name = name.slice(0, openBracePos);
|
|
79
|
-
value = restPath.slice(...match.indices[0]);
|
|
80
|
-
pos += match.indices[0][1] + 1;
|
|
81
46
|
} else {
|
|
82
|
-
|
|
83
|
-
if (
|
|
84
|
-
|
|
85
|
-
continue ROUTES_LOOP;
|
|
86
|
-
}
|
|
87
|
-
endValuePos = path.length;
|
|
47
|
+
const index2 = path.indexOf("/", pos);
|
|
48
|
+
if (index2 === -1) {
|
|
49
|
+
continue ROUTES_LOOP;
|
|
88
50
|
}
|
|
89
|
-
|
|
90
|
-
pos = endValuePos;
|
|
91
|
-
}
|
|
92
|
-
params[name] ||= value;
|
|
93
|
-
} else {
|
|
94
|
-
const index = path.indexOf(part, pos);
|
|
95
|
-
if (index !== pos) {
|
|
96
|
-
continue ROUTES_LOOP;
|
|
51
|
+
pos = index2;
|
|
97
52
|
}
|
|
98
|
-
pos += part.length;
|
|
99
53
|
}
|
|
100
|
-
|
|
101
|
-
|
|
54
|
+
handlers.push([handler, emptyParams]);
|
|
55
|
+
} else if (hasLabel && !hasStar) {
|
|
56
|
+
const params = /* @__PURE__ */ Object.create(null);
|
|
57
|
+
const parts = routePath.match(splitPathRe);
|
|
58
|
+
const lastIndex = parts.length - 1;
|
|
59
|
+
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
|
60
|
+
if (pos === -1 || pos >= path.length) {
|
|
102
61
|
continue ROUTES_LOOP;
|
|
103
62
|
}
|
|
63
|
+
const part = parts[j];
|
|
64
|
+
if (part.charCodeAt(1) === 58) {
|
|
65
|
+
let name = part.slice(2);
|
|
66
|
+
let value;
|
|
67
|
+
if (name.charCodeAt(name.length - 1) === 125) {
|
|
68
|
+
const openBracePos = name.indexOf("{");
|
|
69
|
+
const pattern = name.slice(openBracePos + 1, -1);
|
|
70
|
+
const restPath = path.slice(pos + 1);
|
|
71
|
+
const match = new RegExp(pattern, "d").exec(restPath);
|
|
72
|
+
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
|
73
|
+
continue ROUTES_LOOP;
|
|
74
|
+
}
|
|
75
|
+
name = name.slice(0, openBracePos);
|
|
76
|
+
value = restPath.slice(...match.indices[0]);
|
|
77
|
+
pos += match.indices[0][1] + 1;
|
|
78
|
+
} else {
|
|
79
|
+
let endValuePos = path.indexOf("/", pos + 1);
|
|
80
|
+
if (endValuePos === -1) {
|
|
81
|
+
if (pos + 1 === path.length) {
|
|
82
|
+
continue ROUTES_LOOP;
|
|
83
|
+
}
|
|
84
|
+
endValuePos = path.length;
|
|
85
|
+
}
|
|
86
|
+
value = path.slice(pos + 1, endValuePos);
|
|
87
|
+
pos = endValuePos;
|
|
88
|
+
}
|
|
89
|
+
params[name] ||= value;
|
|
90
|
+
} else {
|
|
91
|
+
const index = path.indexOf(part, pos);
|
|
92
|
+
if (index !== pos) {
|
|
93
|
+
continue ROUTES_LOOP;
|
|
94
|
+
}
|
|
95
|
+
pos += part.length;
|
|
96
|
+
}
|
|
97
|
+
if (j === lastIndex) {
|
|
98
|
+
if (pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
|
99
|
+
continue ROUTES_LOOP;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
104
102
|
}
|
|
103
|
+
handlers.push([handler, params]);
|
|
104
|
+
} else if (hasLabel && hasStar) {
|
|
105
|
+
throw new UnsupportedPathError();
|
|
105
106
|
}
|
|
106
|
-
handlers.push([handler, params]);
|
|
107
|
-
} else if (hasLabel && hasStar) {
|
|
108
|
-
throw new UnsupportedPathError();
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
109
|
return [handlers];
|
|
@@ -28,8 +28,9 @@ var PatternRouter = class {
|
|
|
28
28
|
}
|
|
29
29
|
match(method, path) {
|
|
30
30
|
const handlers = [];
|
|
31
|
-
for (
|
|
32
|
-
|
|
31
|
+
for (let i = 0, len = this.routes.length; i < len; i++) {
|
|
32
|
+
const [pattern, routeMethod, handler] = this.routes[i];
|
|
33
|
+
if (routeMethod === method || routeMethod === METHOD_NAME_ALL) {
|
|
33
34
|
const match = pattern.exec(path);
|
|
34
35
|
if (match) {
|
|
35
36
|
handlers.push([handler, match.groups || /* @__PURE__ */ Object.create(null)]);
|
|
@@ -24,9 +24,9 @@ var SmartRouter = class {
|
|
|
24
24
|
for (; i < len; i++) {
|
|
25
25
|
const router = routers[i];
|
|
26
26
|
try {
|
|
27
|
-
routes.
|
|
28
|
-
router.add(...
|
|
29
|
-
}
|
|
27
|
+
for (let i2 = 0, len2 = routes.length; i2 < len2; i2++) {
|
|
28
|
+
router.add(...routes[i2]);
|
|
29
|
+
}
|
|
30
30
|
res = router.match(method, path);
|
|
31
31
|
} catch (e) {
|
|
32
32
|
if (e instanceof UnsupportedPathError) {
|
|
@@ -6,21 +6,18 @@ var Node = class {
|
|
|
6
6
|
children;
|
|
7
7
|
patterns;
|
|
8
8
|
order = 0;
|
|
9
|
-
name;
|
|
10
9
|
params = /* @__PURE__ */ Object.create(null);
|
|
11
10
|
constructor(method, handler, children) {
|
|
12
11
|
this.children = children || /* @__PURE__ */ Object.create(null);
|
|
13
12
|
this.methods = [];
|
|
14
|
-
this.name = "";
|
|
15
13
|
if (method && handler) {
|
|
16
14
|
const m = /* @__PURE__ */ Object.create(null);
|
|
17
|
-
m[method] = { handler, possibleKeys: [], score: 0
|
|
15
|
+
m[method] = { handler, possibleKeys: [], score: 0 };
|
|
18
16
|
this.methods = [m];
|
|
19
17
|
}
|
|
20
18
|
this.patterns = [];
|
|
21
19
|
}
|
|
22
20
|
insert(method, path, handler) {
|
|
23
|
-
this.name = `${method} ${path}`;
|
|
24
21
|
this.order = ++this.order;
|
|
25
22
|
let curNode = this;
|
|
26
23
|
const parts = splitRoutingPath(path);
|
|
@@ -50,7 +47,6 @@ var Node = class {
|
|
|
50
47
|
const handlerSet = {
|
|
51
48
|
handler,
|
|
52
49
|
possibleKeys: possibleKeys.filter((v, i, a) => a.indexOf(v) === i),
|
|
53
|
-
name: this.name,
|
|
54
50
|
score: this.order
|
|
55
51
|
};
|
|
56
52
|
m[method] = handlerSet;
|
|
@@ -65,11 +61,12 @@ var Node = class {
|
|
|
65
61
|
const processedSet = /* @__PURE__ */ Object.create(null);
|
|
66
62
|
if (handlerSet !== void 0) {
|
|
67
63
|
handlerSet.params = /* @__PURE__ */ Object.create(null);
|
|
68
|
-
handlerSet.possibleKeys.
|
|
69
|
-
const
|
|
64
|
+
for (let i2 = 0, len2 = handlerSet.possibleKeys.length; i2 < len2; i2++) {
|
|
65
|
+
const key = handlerSet.possibleKeys[i2];
|
|
66
|
+
const processed = processedSet[handlerSet.score];
|
|
70
67
|
handlerSet.params[key] = params[key] && !processed ? params[key] : nodeParams[key] ?? params[key];
|
|
71
|
-
processedSet[handlerSet.
|
|
72
|
-
}
|
|
68
|
+
processedSet[handlerSet.score] = true;
|
|
69
|
+
}
|
|
73
70
|
handlerSets.push(handlerSet);
|
|
74
71
|
}
|
|
75
72
|
}
|
|
@@ -90,7 +87,7 @@ var Node = class {
|
|
|
90
87
|
const nextNode = node.children[part];
|
|
91
88
|
if (nextNode) {
|
|
92
89
|
nextNode.params = node.params;
|
|
93
|
-
if (isLast
|
|
90
|
+
if (isLast) {
|
|
94
91
|
if (nextNode.children["*"]) {
|
|
95
92
|
handlerSets.push(
|
|
96
93
|
...this.gHSets(nextNode.children["*"], method, node.params, /* @__PURE__ */ Object.create(null))
|
|
@@ -123,10 +120,10 @@ var Node = class {
|
|
|
123
120
|
handlerSets.push(...this.gHSets(child, method, node.params, params));
|
|
124
121
|
continue;
|
|
125
122
|
}
|
|
126
|
-
if (matcher === true || matcher
|
|
123
|
+
if (matcher === true || matcher.test(part)) {
|
|
127
124
|
if (typeof key === "string") {
|
|
128
125
|
params[name] = part;
|
|
129
|
-
if (isLast
|
|
126
|
+
if (isLast) {
|
|
130
127
|
handlerSets.push(...this.gHSets(child, method, params, node.params));
|
|
131
128
|
if (child.children["*"]) {
|
|
132
129
|
handlerSets.push(...this.gHSets(child.children["*"], method, params, node.params));
|
|
@@ -10,8 +10,8 @@ var TrieRouter = class {
|
|
|
10
10
|
add(method, path, handler) {
|
|
11
11
|
const results = checkOptionalParameter(path);
|
|
12
12
|
if (results) {
|
|
13
|
-
for (
|
|
14
|
-
this.node.insert(method,
|
|
13
|
+
for (let i = 0, len = results.length; i < len; i++) {
|
|
14
|
+
this.node.insert(method, results[i], handler);
|
|
15
15
|
}
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { UpgradeWebSocket } from '../../helper/websocket';
|
|
2
|
-
|
|
2
|
+
import { WSContext } from '../../helper/websocket';
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export interface BunServerWebSocket<T> {
|
|
3
7
|
send(data: string | ArrayBufferLike, compress?: boolean): void;
|
|
4
8
|
close(code?: number, reason?: string): void;
|
|
5
9
|
data: T;
|
|
@@ -19,5 +23,9 @@ export interface BunWebSocketData {
|
|
|
19
23
|
url: URL;
|
|
20
24
|
protocol: string;
|
|
21
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export declare const createWSContext: (ws: BunServerWebSocket<BunWebSocketData>) => WSContext;
|
|
22
30
|
export declare const createBunWebSocket: <T>() => CreateWebSocket<T>;
|
|
23
31
|
export {};
|
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
import type { Hono } from '../../hono';
|
|
2
|
-
|
|
3
|
-
export declare const handle: (app: Hono<any, any, any>) => (req: Request, requestContext: FetchEventLike) => Response | Promise<Response>;
|
|
2
|
+
export declare const handle: (app: Hono<any, any, any>) => (req: Request) => Response | Promise<Response>;
|
|
@@ -19,17 +19,46 @@ export interface WSEvents<T = unknown> {
|
|
|
19
19
|
export type UpgradeWebSocket<T = unknown, U = any> = (createEvents: (c: Context) => WSEvents<T> | Promise<WSEvents<T>>, options?: U) => MiddlewareHandler<any, string, {
|
|
20
20
|
outputFormat: "ws";
|
|
21
21
|
}>;
|
|
22
|
+
/**
|
|
23
|
+
* ReadyState for WebSocket
|
|
24
|
+
*/
|
|
22
25
|
export type WSReadyState = 0 | 1 | 2 | 3;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* An argument for WSContext class
|
|
28
|
+
*/
|
|
29
|
+
export interface WSContestInit<T = unknown> {
|
|
30
|
+
send(data: string | ArrayBuffer, options: SendOptions): void;
|
|
31
|
+
close(code?: number, reason?: string): void;
|
|
27
32
|
raw?: T;
|
|
28
|
-
binaryType: BinaryType;
|
|
29
33
|
readyState: WSReadyState;
|
|
34
|
+
url?: string | URL | null;
|
|
35
|
+
protocol?: string | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Options for sending message
|
|
39
|
+
*/
|
|
40
|
+
export interface SendOptions {
|
|
41
|
+
compress?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A context for controlling WebSockets
|
|
45
|
+
*/
|
|
46
|
+
export declare class WSContext<T = unknown> {
|
|
47
|
+
constructor(init: WSContestInit<T>);
|
|
48
|
+
send(source: string | ArrayBuffer | Uint8Array, options?: SendOptions): void;
|
|
49
|
+
raw?: T;
|
|
50
|
+
binaryType: BinaryType;
|
|
51
|
+
get readyState(): WSReadyState;
|
|
30
52
|
url: URL | null;
|
|
31
53
|
protocol: string | null;
|
|
32
54
|
close(code?: number, reason?: string): void;
|
|
33
|
-
}
|
|
55
|
+
}
|
|
34
56
|
export type WSMessageReceive = string | Blob | ArrayBufferLike;
|
|
35
57
|
export declare const createWSMessageEvent: (source: WSMessageReceive) => MessageEvent<WSMessageReceive>;
|
|
58
|
+
export interface WebSocketHelperDefineContext {
|
|
59
|
+
}
|
|
60
|
+
export type WebSocketHelperDefineHandler<T, U> = (c: Context, events: WSEvents<T>, options?: U) => Promise<Response | void> | Response | void;
|
|
61
|
+
/**
|
|
62
|
+
* Create a WebSocket adapter/helper
|
|
63
|
+
*/
|
|
64
|
+
export declare const defineWebSocketHelper: <T = unknown, U = any>(handler: WebSocketHelperDefineHandler<T, U>) => UpgradeWebSocket<T, U>;
|
|
@@ -4,14 +4,12 @@ type HandlerSet<T> = {
|
|
|
4
4
|
handler: T;
|
|
5
5
|
possibleKeys: string[];
|
|
6
6
|
score: number;
|
|
7
|
-
name: string;
|
|
8
7
|
};
|
|
9
8
|
export declare class Node<T> {
|
|
10
9
|
methods: Record<string, HandlerSet<T>>[];
|
|
11
10
|
children: Record<string, Node<T>>;
|
|
12
11
|
patterns: Pattern[];
|
|
13
12
|
order: number;
|
|
14
|
-
name: string;
|
|
15
13
|
params: Record<string, string>;
|
|
16
14
|
constructor(method?: string, handler?: T, children?: Record<string, Node<T>>);
|
|
17
15
|
insert(method: string, path: string, handler: T): Node<T>;
|
package/dist/utils/jwt/jws.js
CHANGED
|
@@ -21,8 +21,10 @@ async function importPrivateKey(key, alg) {
|
|
|
21
21
|
throw new Error("`crypto.subtle.importKey` is undefined. JWT auth middleware requires it.");
|
|
22
22
|
}
|
|
23
23
|
if (isCryptoKey(key)) {
|
|
24
|
-
if (key.type !== "private") {
|
|
25
|
-
throw new Error(
|
|
24
|
+
if (key.type !== "private" && key.type !== "secret") {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`unexpected key type: CryptoKey.type is ${key.type}, expected private or secret`
|
|
27
|
+
);
|
|
26
28
|
}
|
|
27
29
|
return key;
|
|
28
30
|
}
|