@keri-ts/hosting 0.0.0-stage → 2.0.1
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/LICENSE +202 -0
- package/README.md +22 -2
- package/esm/hosting/npm/src/_dnt.shims.js +61 -0
- package/esm/hosting/npm/src/mod.js +7 -0
- package/esm/hosting/npm/src/src/host/http-server.js +310 -0
- package/esm/hosting/npm/src/src/host/kernel.js +83 -0
- package/esm/hosting/npm/src/src/http/app.js +33 -0
- package/esm/hosting/npm/src/src/http/error-mapping.js +19 -0
- package/esm/hosting/npm/src/src/http/policy.js +63 -0
- package/esm/hosting/npm/src/src/http/protocol/artifact-files.js +164 -0
- package/esm/hosting/npm/src/src/http/protocol/context.js +48 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/did-resolution.js +90 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/dws-artifacts.js +112 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/generic-cesr-ingress.js +272 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/health.js +9 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/mailbox-admin.js +136 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/oobi.js +171 -0
- package/esm/hosting/npm/src/src/http/protocol/endpoints/witness-http.js +98 -0
- package/esm/hosting/npm/src/src/http/protocol/responses.js +21 -0
- package/esm/hosting/npm/src/src/http/protocol/runtime-bridge.js +16 -0
- package/esm/hosting/npm/src/src/http/protocol/types.js +1 -0
- package/esm/hosting/npm/src/src/http/protocol-handler.js +68 -0
- package/esm/hosting/npm/src/src/http/source.js +118 -0
- package/esm/package.json +3 -0
- package/package.json +43 -4
- package/types/_dnt.shims.d.ts +6 -0
- package/types/_dnt.shims.d.ts.map +1 -0
- package/types/mod.d.ts +9 -0
- package/types/mod.d.ts.map +1 -0
- package/types/src/host/http-server.d.ts +46 -0
- package/types/src/host/http-server.d.ts.map +1 -0
- package/types/src/host/kernel.d.ts +37 -0
- package/types/src/host/kernel.d.ts.map +1 -0
- package/types/src/http/app.d.ts +22 -0
- package/types/src/http/app.d.ts.map +1 -0
- package/types/src/http/error-mapping.d.ts +7 -0
- package/types/src/http/error-mapping.d.ts.map +1 -0
- package/types/src/http/policy.d.ts +27 -0
- package/types/src/http/policy.d.ts.map +1 -0
- package/types/src/http/protocol/artifact-files.d.ts +11 -0
- package/types/src/http/protocol/artifact-files.d.ts.map +1 -0
- package/types/src/http/protocol/context.d.ts +14 -0
- package/types/src/http/protocol/context.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/did-resolution.d.ts +12 -0
- package/types/src/http/protocol/endpoints/did-resolution.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/dws-artifacts.d.ts +9 -0
- package/types/src/http/protocol/endpoints/dws-artifacts.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/generic-cesr-ingress.d.ts +21 -0
- package/types/src/http/protocol/endpoints/generic-cesr-ingress.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/health.d.ts +6 -0
- package/types/src/http/protocol/endpoints/health.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/mailbox-admin.d.ts +26 -0
- package/types/src/http/protocol/endpoints/mailbox-admin.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/oobi.d.ts +31 -0
- package/types/src/http/protocol/endpoints/oobi.d.ts.map +1 -0
- package/types/src/http/protocol/endpoints/witness-http.d.ts +13 -0
- package/types/src/http/protocol/endpoints/witness-http.d.ts.map +1 -0
- package/types/src/http/protocol/responses.d.ts +7 -0
- package/types/src/http/protocol/responses.d.ts.map +1 -0
- package/types/src/http/protocol/runtime-bridge.d.ts +13 -0
- package/types/src/http/protocol/runtime-bridge.d.ts.map +1 -0
- package/types/src/http/protocol/types.d.ts +78 -0
- package/types/src/http/protocol/types.d.ts.map +1 -0
- package/types/src/http/protocol-handler.d.ts +19 -0
- package/types/src/http/protocol-handler.d.ts.map +1 -0
- package/types/src/http/source.d.ts +25 -0
- package/types/src/http/source.d.ts.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @keri-ts/hosting
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Concrete Deno/Node HTTP hosting, app middleware and protocol route composition
|
|
4
|
+
for KERI runtimes. The package borrows public `keri-ts/runtime` and `cesr-ts`
|
|
5
|
+
operations; KERI continues to own protocol evidence and acceptance.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { createProtocolHandler, createTufaApp, runHostKernel, startServer } from "@keri-ts/hosting";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`runHostKernel` composes the existing runtime and HTTP listener over a borrowed
|
|
12
|
+
Habery. It joins companion tasks, workers, active responses and owned resources
|
|
13
|
+
before teardown. `startServer` owns the concrete Deno/Node adapter lifecycle.
|
|
14
|
+
`createTufaApp` retains the existing Hono middleware/error policy around
|
|
15
|
+
`createProtocolHandler`; direct handler consumers close its artifact responses.
|
|
16
|
+
Names, routes, shutdown ordering and HTTP behavior are unchanged by extraction.
|
|
17
|
+
|
|
18
|
+
Tufa retains CLI registration and indirect/role/witness TCP composition. This
|
|
19
|
+
package introduces no role router, provider abstraction or alternate scheduler.
|
|
20
|
+
Import hosting APIs from this package directly; Tufa publishes its CLI surface.
|
|
21
|
+
|
|
22
|
+
Run `deno task check`, `deno task test` and `deno task build:npm` in this directory.
|
|
23
|
+
Licensed under Apache-2.0; see the repository's `LICENSE`.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Deno } from "@deno/shim-deno";
|
|
2
|
+
export { Deno } from "@deno/shim-deno";
|
|
3
|
+
const dntGlobals = {
|
|
4
|
+
Deno,
|
|
5
|
+
};
|
|
6
|
+
export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
|
|
7
|
+
function createMergeProxy(baseObj, extObj) {
|
|
8
|
+
return new Proxy(baseObj, {
|
|
9
|
+
get(_target, prop, _receiver) {
|
|
10
|
+
if (prop in extObj) {
|
|
11
|
+
return extObj[prop];
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return baseObj[prop];
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
set(_target, prop, value) {
|
|
18
|
+
if (prop in extObj) {
|
|
19
|
+
delete extObj[prop];
|
|
20
|
+
}
|
|
21
|
+
baseObj[prop] = value;
|
|
22
|
+
return true;
|
|
23
|
+
},
|
|
24
|
+
deleteProperty(_target, prop) {
|
|
25
|
+
let success = false;
|
|
26
|
+
if (prop in extObj) {
|
|
27
|
+
delete extObj[prop];
|
|
28
|
+
success = true;
|
|
29
|
+
}
|
|
30
|
+
if (prop in baseObj) {
|
|
31
|
+
delete baseObj[prop];
|
|
32
|
+
success = true;
|
|
33
|
+
}
|
|
34
|
+
return success;
|
|
35
|
+
},
|
|
36
|
+
ownKeys(_target) {
|
|
37
|
+
const baseKeys = Reflect.ownKeys(baseObj);
|
|
38
|
+
const extKeys = Reflect.ownKeys(extObj);
|
|
39
|
+
const extKeysSet = new Set(extKeys);
|
|
40
|
+
return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
|
|
41
|
+
},
|
|
42
|
+
defineProperty(_target, prop, desc) {
|
|
43
|
+
if (prop in extObj) {
|
|
44
|
+
delete extObj[prop];
|
|
45
|
+
}
|
|
46
|
+
Reflect.defineProperty(baseObj, prop, desc);
|
|
47
|
+
return true;
|
|
48
|
+
},
|
|
49
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
50
|
+
if (prop in extObj) {
|
|
51
|
+
return Reflect.getOwnPropertyDescriptor(extObj, prop);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
return Reflect.getOwnPropertyDescriptor(baseObj, prop);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
has(_target, prop) {
|
|
58
|
+
return prop in extObj || prop in baseObj;
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** HTTP hosting package entrypoint. */
|
|
2
|
+
export { startServer } from "./src/host/http-server.js";
|
|
3
|
+
export { runHostKernel, } from "./src/host/kernel.js";
|
|
4
|
+
export { createTufaApp } from "./src/http/app.js";
|
|
5
|
+
export { createProtocolHandler } from "./src/http/protocol-handler.js";
|
|
6
|
+
export { handleWitnessReceiptPost } from "./src/http/protocol/endpoints/witness-http.js";
|
|
7
|
+
export { sourceRequest, SourceTrust } from "./src/http/source.js";
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared HTTP host adapters for the HTTP application edge.
|
|
3
|
+
*
|
|
4
|
+
* This module owns Deno/Node listener startup, request bridging, and shutdown.
|
|
5
|
+
* Protocol route composition lives under `hosting/src/http`, while route-facing
|
|
6
|
+
* policy lives in `keri-ts` through `ProtocolHostPolicy`.
|
|
7
|
+
*/
|
|
8
|
+
import * as dntShim from "../../_dnt.shims.js";
|
|
9
|
+
import { action, call } from "effection";
|
|
10
|
+
import { consoleLogger } from "keri-ts/runtime";
|
|
11
|
+
import { createServer } from "node:http";
|
|
12
|
+
import { createServer as createTlsServer } from "node:https";
|
|
13
|
+
import { Readable } from "node:stream";
|
|
14
|
+
import { createTufaApp } from "../http/app.js";
|
|
15
|
+
/**
|
|
16
|
+
* Detect whether the current runtime can host directly via `Deno.serve()`.
|
|
17
|
+
*
|
|
18
|
+
* The npm build still carries a Deno shim object, so this checks the actual
|
|
19
|
+
* function presence instead of assuming `globalThis.Deno` means native hosting.
|
|
20
|
+
*/
|
|
21
|
+
function hasDenoServe() {
|
|
22
|
+
return typeof dntShim.Deno.serve === "function";
|
|
23
|
+
}
|
|
24
|
+
/** Normalize startup policy shared by both HTTP host implementations. */
|
|
25
|
+
function buildServerOptions(port, logger, signal, hostname = "127.0.0.1", onListen, tls) {
|
|
26
|
+
return {
|
|
27
|
+
port,
|
|
28
|
+
hostname,
|
|
29
|
+
signal,
|
|
30
|
+
tls,
|
|
31
|
+
onListen: ({ port }) => {
|
|
32
|
+
logger.info(`Server running on ${tls ? "https" : "http"}://${hostname}:${port}`);
|
|
33
|
+
onListen?.({ port, hostname });
|
|
34
|
+
},
|
|
35
|
+
onError: (error) => {
|
|
36
|
+
logger.error("Server error:", error);
|
|
37
|
+
return new Response("Internal Server Error", { status: 500 });
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Empty PEM values must fail before native serving can interpret them as plaintext options. */
|
|
42
|
+
function requireTlsIdentity(options) {
|
|
43
|
+
if (options.tls && (!options.tls.key || !options.tls.cert)) {
|
|
44
|
+
throw new Error("HTTPS requires both a certificate and private key in PEM format.");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** Open the native Deno HTTP host and join request cleanup after its listener closes. */
|
|
48
|
+
export function openDenoServerHost(options, handler) {
|
|
49
|
+
requireTlsIdentity(options);
|
|
50
|
+
const requests = new Set();
|
|
51
|
+
const server = dntShim.Deno.serve({ ...options, ...options.tls }, (request) => {
|
|
52
|
+
const response = Promise.resolve().then(() => handler(request));
|
|
53
|
+
requests.add(response);
|
|
54
|
+
void response.then(() => requests.delete(response), () => requests.delete(response));
|
|
55
|
+
return response;
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
server: {
|
|
59
|
+
// Native server.finished can resolve while a disconnected handler still joins parser/sink cleanup.
|
|
60
|
+
finished: server.finished.finally(async () => {
|
|
61
|
+
await Promise.allSettled(requests);
|
|
62
|
+
}),
|
|
63
|
+
},
|
|
64
|
+
close() {
|
|
65
|
+
// The abort signal passed to `Deno.serve()` owns shutdown.
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Preserve multiplicity when copying Node headers into Fetch `Headers`. */
|
|
70
|
+
function appendNodeHeader(headers, key, value) {
|
|
71
|
+
if (Array.isArray(value)) {
|
|
72
|
+
for (const item of value) {
|
|
73
|
+
headers.append(key, item);
|
|
74
|
+
}
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (value !== undefined) {
|
|
78
|
+
headers.set(key, value);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Convert one Node incoming request into a Fetch `Request`. */
|
|
82
|
+
function toNodeRequest(req, options, signal) {
|
|
83
|
+
const headers = new Headers();
|
|
84
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
85
|
+
appendNodeHeader(headers, key, value);
|
|
86
|
+
}
|
|
87
|
+
const method = req.method ?? "GET";
|
|
88
|
+
const host = req.headers.host ?? `${options.hostname}:${options.port}`;
|
|
89
|
+
const init = {
|
|
90
|
+
method,
|
|
91
|
+
headers,
|
|
92
|
+
signal,
|
|
93
|
+
};
|
|
94
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
95
|
+
init.body = Readable.toWeb(req);
|
|
96
|
+
init.duplex = "half";
|
|
97
|
+
}
|
|
98
|
+
return new Request(`${options.tls ? "https" : "http"}://${host}${req.url ?? "/"}`, init);
|
|
99
|
+
}
|
|
100
|
+
/** Flush one Fetch `Response` back onto Node's `ServerResponse`. */
|
|
101
|
+
async function writeNodeResponse(res, response) {
|
|
102
|
+
res.statusCode = response.status;
|
|
103
|
+
response.headers.forEach((value, key) => {
|
|
104
|
+
res.setHeader(key, value);
|
|
105
|
+
});
|
|
106
|
+
if (!response.body) {
|
|
107
|
+
res.end();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const stream = Readable.fromWeb(response.body);
|
|
111
|
+
try {
|
|
112
|
+
await new Promise((resolve, reject) => {
|
|
113
|
+
stream.once("error", reject);
|
|
114
|
+
res.once("error", reject);
|
|
115
|
+
res.once("finish", () => resolve());
|
|
116
|
+
res.once("close", () => {
|
|
117
|
+
// A disconnect may never emit finish; destroying the Node reader cancels its Fetch body too.
|
|
118
|
+
stream.destroy();
|
|
119
|
+
resolve();
|
|
120
|
+
});
|
|
121
|
+
stream.pipe(res);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
// fromWeb finishes destruction only after cancelling its source; retain ownership until that joins.
|
|
126
|
+
await new Promise((resolve) => {
|
|
127
|
+
if (stream.closed)
|
|
128
|
+
resolve();
|
|
129
|
+
else {
|
|
130
|
+
stream.once("close", resolve);
|
|
131
|
+
stream.destroy();
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Execute the shared Fetch handler for one Node request. */
|
|
137
|
+
async function handleNodeRequest(req, res, options, handler) {
|
|
138
|
+
const disconnected = new AbortController();
|
|
139
|
+
/** Carry socket loss into every request-owned parser and storage operation. */
|
|
140
|
+
const abort = () => disconnected.abort(new DOMException("Client disconnected", "AbortError"));
|
|
141
|
+
req.once("aborted", abort);
|
|
142
|
+
res.once("close", abort);
|
|
143
|
+
const signal = AbortSignal.any([options.signal, disconnected.signal]);
|
|
144
|
+
try {
|
|
145
|
+
const response = await handler(toNodeRequest(req, options, signal));
|
|
146
|
+
if (!res.destroyed)
|
|
147
|
+
await writeNodeResponse(res, response);
|
|
148
|
+
else
|
|
149
|
+
await response.body?.cancel();
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (res.headersSent) {
|
|
153
|
+
// A streamed body can fail after 200 headers; terminate that transfer instead of rewriting its status.
|
|
154
|
+
res.destroy();
|
|
155
|
+
if (!signal.aborted || error !== signal.reason)
|
|
156
|
+
options.onError(error);
|
|
157
|
+
}
|
|
158
|
+
else if (!res.destroyed) {
|
|
159
|
+
const fallback = options.onError(error);
|
|
160
|
+
await writeNodeResponse(res, fallback);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
finally {
|
|
164
|
+
req.removeListener("aborted", abort);
|
|
165
|
+
res.removeListener("close", abort);
|
|
166
|
+
disconnected.abort();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** Open the Node fallback host; completion joins every admitted request after the listener closes. */
|
|
170
|
+
export function openNodeServerHost(options, handler) {
|
|
171
|
+
requireTlsIdentity(options);
|
|
172
|
+
// A closed socket does not imply that its parser or payload cleanup has finished.
|
|
173
|
+
const requests = new Set();
|
|
174
|
+
const listener = (req, res) => {
|
|
175
|
+
const request = handleNodeRequest(req, res, options, handler);
|
|
176
|
+
requests.add(request);
|
|
177
|
+
// Attach both branches immediately: a rejected fallback response must not become detached rejection.
|
|
178
|
+
void request.then(() => requests.delete(request), () => requests.delete(request));
|
|
179
|
+
};
|
|
180
|
+
const server = options.tls ? createTlsServer(options.tls, listener) : createServer(listener);
|
|
181
|
+
// listen() is asynchronous; an owner can close the host before Node marks it listening.
|
|
182
|
+
let closing = false;
|
|
183
|
+
/** Remember early cancellation and close the listener as soon as it becomes available. */
|
|
184
|
+
const close = () => {
|
|
185
|
+
closing = true;
|
|
186
|
+
if (server.listening)
|
|
187
|
+
server.close();
|
|
188
|
+
};
|
|
189
|
+
const onAbort = close;
|
|
190
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
191
|
+
const closed = new Promise((resolve, reject) => {
|
|
192
|
+
server.once("close", resolve);
|
|
193
|
+
server.once("error", reject);
|
|
194
|
+
});
|
|
195
|
+
const finished = closed.finally(async () => {
|
|
196
|
+
// Listener closure prevents new admissions; settle every remaining owner before releasing the runtime.
|
|
197
|
+
await Promise.allSettled(requests);
|
|
198
|
+
});
|
|
199
|
+
server.listen(options.port, options.hostname, () => {
|
|
200
|
+
if (closing || options.signal.aborted) {
|
|
201
|
+
close();
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const address = server.address();
|
|
205
|
+
const port = typeof address === "object" && address !== null ? address.port : options.port;
|
|
206
|
+
options.onListen({ port });
|
|
207
|
+
});
|
|
208
|
+
return {
|
|
209
|
+
server: { finished },
|
|
210
|
+
close() {
|
|
211
|
+
options.signal.removeEventListener("abort", onAbort);
|
|
212
|
+
close();
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** Open one HTTP host and return its cleanup handle. */
|
|
217
|
+
function openServerHost(port, logger, runtime, options = {}) {
|
|
218
|
+
const controller = new AbortController();
|
|
219
|
+
const { signal } = controller;
|
|
220
|
+
let requestShutdown;
|
|
221
|
+
const shutdownRequested = new Promise((resolve) => requestShutdown = resolve);
|
|
222
|
+
const shutdown = () => {
|
|
223
|
+
logger.info("Shutting down server...");
|
|
224
|
+
requestShutdown();
|
|
225
|
+
};
|
|
226
|
+
const serverOptions = buildServerOptions(port, logger, signal, options.hostname ?? "127.0.0.1", options.onListen, options.tls);
|
|
227
|
+
const app = createTufaApp({
|
|
228
|
+
runtime,
|
|
229
|
+
protocolPolicy: options,
|
|
230
|
+
protocolHandler: options.protocolHandler,
|
|
231
|
+
app: { logger },
|
|
232
|
+
});
|
|
233
|
+
const handler = (req) => Promise.resolve(app.fetch(req));
|
|
234
|
+
dntShim.Deno.addSignalListener("SIGINT", shutdown);
|
|
235
|
+
dntShim.Deno.addSignalListener("SIGTERM", shutdown);
|
|
236
|
+
const removeSignals = () => {
|
|
237
|
+
dntShim.Deno.removeSignalListener("SIGINT", shutdown);
|
|
238
|
+
dntShim.Deno.removeSignalListener("SIGTERM", shutdown);
|
|
239
|
+
};
|
|
240
|
+
let host;
|
|
241
|
+
try {
|
|
242
|
+
if (hasDenoServe()) {
|
|
243
|
+
try {
|
|
244
|
+
host = openDenoServerHost(serverOptions, handler);
|
|
245
|
+
}
|
|
246
|
+
catch (error) {
|
|
247
|
+
logger.warn(`Falling back to Node ${options.tls ? "HTTPS" : "HTTP"} host after Deno.serve() startup failed:`, error);
|
|
248
|
+
host = openNodeServerHost(serverOptions, handler);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
host = openNodeServerHost(serverOptions, handler);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
catch (error) {
|
|
256
|
+
removeSignals();
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
server: host.server,
|
|
261
|
+
shutdownRequested,
|
|
262
|
+
async close() {
|
|
263
|
+
// Keep repeated signals graceful until the adapter has actually drained.
|
|
264
|
+
try {
|
|
265
|
+
try {
|
|
266
|
+
await host.close();
|
|
267
|
+
}
|
|
268
|
+
finally {
|
|
269
|
+
controller.abort();
|
|
270
|
+
try {
|
|
271
|
+
await app.close();
|
|
272
|
+
}
|
|
273
|
+
finally {
|
|
274
|
+
await host.server.finished;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
removeSignals();
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/** Adapt `server.finished` into an Effection operation. */
|
|
285
|
+
function* waitForServerStop(host) {
|
|
286
|
+
yield* action((resolve, reject) => {
|
|
287
|
+
Promise.race([host.server.finished, host.shutdownRequested]).then(resolve).catch(reject);
|
|
288
|
+
return () => { };
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/** Start the Tufa HTTP host for one shared `AgentRuntime`. */
|
|
292
|
+
export function* startServer(port = 8000, logger = consoleLogger, runtime, options = {}, beforeDrain) {
|
|
293
|
+
const host = openServerHost(port, logger, runtime, options);
|
|
294
|
+
try {
|
|
295
|
+
yield* waitForServerStop(host);
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
// Stop accepting work synchronously, then let the owner close response-producing
|
|
299
|
+
// resources before waiting for active response bodies to drain.
|
|
300
|
+
const closing = host.close();
|
|
301
|
+
try {
|
|
302
|
+
if (beforeDrain) {
|
|
303
|
+
yield* beforeDrain();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
yield* call(() => Promise.resolve(closing));
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|