@celerity-sdk/runtime 0.2.0 → 0.2.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 +201 -0
- package/package.json +15 -13
- package/.env.test +0 -4
- package/.vscode/settings.json +0 -4
- package/.yarnrc.yml +0 -1
- package/CHANGELOG.md +0 -15
- package/CONTRIBUTING.md +0 -121
- package/Cargo.toml +0 -38
- package/build.rs +0 -3
- package/src/lib.rs +0 -561
- package/tsconfig.json +0 -12
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celerity-sdk/runtime",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"napi": {
|
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
"x86_64-unknown-freebsd"
|
|
18
18
|
]
|
|
19
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"index.js",
|
|
22
|
+
"index.d.ts"
|
|
23
|
+
],
|
|
20
24
|
"license": "Apache-2.0",
|
|
21
25
|
"repository": {
|
|
22
26
|
"type": "git",
|
|
@@ -24,6 +28,7 @@
|
|
|
24
28
|
"directory": "libs/runtime/sdk/node"
|
|
25
29
|
},
|
|
26
30
|
"devDependencies": {
|
|
31
|
+
"@dotenvx/dotenvx": "^1.45.2",
|
|
27
32
|
"@napi-rs/cli": "^3.0.0",
|
|
28
33
|
"ava": "^6.0.1",
|
|
29
34
|
"tsx": "^4.19.0",
|
|
@@ -50,18 +55,15 @@
|
|
|
50
55
|
"universal": "napi universalize",
|
|
51
56
|
"version": "napi version"
|
|
52
57
|
},
|
|
53
|
-
"dependencies": {
|
|
54
|
-
"@dotenvx/dotenvx": "^1.45.2"
|
|
55
|
-
},
|
|
56
58
|
"optionalDependencies": {
|
|
57
|
-
"@celerity-sdk/runtime-darwin-arm64": "0.2.
|
|
58
|
-
"@celerity-sdk/runtime-linux-arm64-gnu": "0.2.
|
|
59
|
-
"@celerity-sdk/runtime-linux-arm64-musl": "0.2.
|
|
60
|
-
"@celerity-sdk/runtime-win32-arm64-msvc": "0.2.
|
|
61
|
-
"@celerity-sdk/runtime-darwin-x64": "0.2.
|
|
62
|
-
"@celerity-sdk/runtime-win32-x64-msvc": "0.2.
|
|
63
|
-
"@celerity-sdk/runtime-linux-x64-gnu": "0.2.
|
|
64
|
-
"@celerity-sdk/runtime-linux-x64-musl": "0.2.
|
|
65
|
-
"@celerity-sdk/runtime-freebsd-x64": "0.2.
|
|
59
|
+
"@celerity-sdk/runtime-darwin-arm64": "0.2.1",
|
|
60
|
+
"@celerity-sdk/runtime-linux-arm64-gnu": "0.2.1",
|
|
61
|
+
"@celerity-sdk/runtime-linux-arm64-musl": "0.2.1",
|
|
62
|
+
"@celerity-sdk/runtime-win32-arm64-msvc": "0.2.1",
|
|
63
|
+
"@celerity-sdk/runtime-darwin-x64": "0.2.1",
|
|
64
|
+
"@celerity-sdk/runtime-win32-x64-msvc": "0.2.1",
|
|
65
|
+
"@celerity-sdk/runtime-linux-x64-gnu": "0.2.1",
|
|
66
|
+
"@celerity-sdk/runtime-linux-x64-musl": "0.2.1",
|
|
67
|
+
"@celerity-sdk/runtime-freebsd-x64": "0.2.1"
|
|
66
68
|
}
|
|
67
69
|
}
|
package/.env.test
DELETED
package/.vscode/settings.json
DELETED
package/.yarnrc.yml
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
nodeLinker: node-modules
|
package/CHANGELOG.md
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
## [0.2.0](https://github.com/newstack-cloud/celerity/compare/runtime-sdk-node/v0.1.0...runtime-sdk-node/v0.2.0) (2026-02-08)
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
### Features
|
|
7
|
-
|
|
8
|
-
* **lib-rt-sdk-node:** add full support for http features ([6536d24](https://github.com/newstack-cloud/celerity/commit/6536d2495ba614178b661578a9f1f0fd15e899e2))
|
|
9
|
-
|
|
10
|
-
## 1.0.0 (2026-02-08)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
### Features
|
|
14
|
-
|
|
15
|
-
* **lib-rt-sdk-node:** add full support for http features ([6536d24](https://github.com/newstack-cloud/celerity/commit/6536d2495ba614178b661578a9f1f0fd15e899e2))
|
package/CONTRIBUTING.md
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
# Contributing to Celerity Runtime SDK for Node.js
|
|
2
|
-
|
|
3
|
-
## Prerequisites
|
|
4
|
-
|
|
5
|
-
- [Node.js](https://nodejs.org/) >=22.0.0
|
|
6
|
-
- [Rust](https://www.rust-lang.org/tools/install) >=1.76.0
|
|
7
|
-
- [Yarn](https://yarnpkg.com/) (will be installed via corepack)
|
|
8
|
-
- [Git](https://git-scm.com/)
|
|
9
|
-
|
|
10
|
-
## Installation
|
|
11
|
-
|
|
12
|
-
1. **Enable corepack (if not already enabled):**
|
|
13
|
-
```bash
|
|
14
|
-
corepack enable
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
2. **Install dependencies:**
|
|
18
|
-
```bash
|
|
19
|
-
yarn install
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Development
|
|
23
|
-
|
|
24
|
-
### Building
|
|
25
|
-
|
|
26
|
-
Build the native module for all platforms:
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
yarn build
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
Build for a specific target:
|
|
33
|
-
|
|
34
|
-
```bash
|
|
35
|
-
yarn build --target x86_64-apple-darwin
|
|
36
|
-
yarn build --target aarch64-unknown-linux-gnu
|
|
37
|
-
yarn build --target x86_64-pc-windows-msvc
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
### Running Tests
|
|
41
|
-
|
|
42
|
-
```bash
|
|
43
|
-
yarn test
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### Development Build
|
|
47
|
-
|
|
48
|
-
For faster iteration during development:
|
|
49
|
-
|
|
50
|
-
```bash
|
|
51
|
-
yarn build:debug
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Releasing
|
|
55
|
-
|
|
56
|
-
To release a new version of the Node.js Runtime SDK, follow these steps:
|
|
57
|
-
|
|
58
|
-
### Pre-Release Checklist:
|
|
59
|
-
|
|
60
|
-
1. **Update Version in package.json**
|
|
61
|
-
```bash
|
|
62
|
-
# Edit libs/runtime/sdk/node/package.json
|
|
63
|
-
# Change the version field to match your release
|
|
64
|
-
"version": "1.2.3"
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
2. **Update Version in Cargo.toml**
|
|
68
|
-
```bash
|
|
69
|
-
# Edit libs/runtime/sdk/node/Cargo.toml
|
|
70
|
-
# Change the version field to match your release
|
|
71
|
-
version = "1.2.3"
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
3. **Commit the Version Changes**
|
|
75
|
-
```bash
|
|
76
|
-
git add libs/runtime/sdk/node/package.json libs/runtime/sdk/node/Cargo.toml
|
|
77
|
-
git commit -m "chore(lib-rt-sdk-node): bump version to 1.2.3"
|
|
78
|
-
git push origin main
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
4. **Create and Push Release Tag**
|
|
82
|
-
```bash
|
|
83
|
-
# Note: Use 'v' prefix in the tag name
|
|
84
|
-
git tag -a libs/runtime/sdk/node-v1.2.3 -m "Release Celerity Runtime SDK for Node.js v1.2.3"
|
|
85
|
-
git push origin libs/runtime/sdk/node-v1.2.3
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Version Format Guidelines:
|
|
89
|
-
|
|
90
|
-
- **package.json**: Use `"1.2.3"` (no 'v' prefix)
|
|
91
|
-
- **Cargo.toml**: Use `1.2.3` (no 'v' prefix)
|
|
92
|
-
- **Git Tags**: Use `libs/runtime/sdk/node-v1.2.3` (with 'v' prefix)
|
|
93
|
-
- **NPM Package**: Will be published as `@celerity-sdk/runtime 1.2.3`
|
|
94
|
-
|
|
95
|
-
### What Happens After Tagging:
|
|
96
|
-
|
|
97
|
-
1. **CI/CD Pipeline**: The GitHub Actions workflow will automatically:
|
|
98
|
-
- Build native modules for all supported platforms
|
|
99
|
-
- Run tests across multiple Node.js versions and platforms
|
|
100
|
-
- Extract version from tag (e.g., `v1.2.3` from `libs/runtime/sdk/node-v1.2.3`)
|
|
101
|
-
- Publish to NPM as `@celerity-sdk/runtime 1.2.3`
|
|
102
|
-
|
|
103
|
-
2. **Verification**: Check that the package is available on NPM:
|
|
104
|
-
```bash
|
|
105
|
-
npm install @celerity-sdk/runtime@1.2.3
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Supported Platforms:
|
|
109
|
-
|
|
110
|
-
The SDK is built and tested for the following platforms:
|
|
111
|
-
|
|
112
|
-
- **macOS**: x86_64, aarch64 (Apple Silicon)
|
|
113
|
-
- **Linux**: x86_64 (GNU), x86_64 (musl), aarch64 (GNU), aarch64 (musl)
|
|
114
|
-
- **Windows**: x86_64, aarch64
|
|
115
|
-
- **FreeBSD**: x86_64
|
|
116
|
-
|
|
117
|
-
### Important Notes:
|
|
118
|
-
|
|
119
|
-
- All tests must pass before the package is published
|
|
120
|
-
- Cross-platform compatibility is verified automatically
|
|
121
|
-
- The published package version will be clean (no monorepo prefixes)
|
package/Cargo.toml
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
[package]
|
|
2
|
-
edition = "2021"
|
|
3
|
-
name = "celerityjs-runtime-sdk"
|
|
4
|
-
version = "0.0.0"
|
|
5
|
-
license.workspace = true
|
|
6
|
-
|
|
7
|
-
[lib]
|
|
8
|
-
crate-type = ["cdylib"]
|
|
9
|
-
|
|
10
|
-
[dependencies]
|
|
11
|
-
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
|
|
12
|
-
napi = { version = "3.8", default-features = false, features = [
|
|
13
|
-
"napi4",
|
|
14
|
-
"async",
|
|
15
|
-
"serde-json",
|
|
16
|
-
] }
|
|
17
|
-
napi-derive = "3"
|
|
18
|
-
tokio = "1.39.2"
|
|
19
|
-
axum = "0.8.4"
|
|
20
|
-
serde = "1.0.204"
|
|
21
|
-
serde_json = "1.0.122"
|
|
22
|
-
chrono = "0.4"
|
|
23
|
-
tracing = "0.1.40"
|
|
24
|
-
tracing-core = "0.1.32"
|
|
25
|
-
tracing-subscriber = { version = "0.3.20", features = ["chrono", "json"] }
|
|
26
|
-
|
|
27
|
-
[dependencies.celerity_runtime_core]
|
|
28
|
-
path = "../../core"
|
|
29
|
-
|
|
30
|
-
[dependencies.celerity_helpers]
|
|
31
|
-
path = "../../helpers"
|
|
32
|
-
|
|
33
|
-
[build-dependencies]
|
|
34
|
-
napi-build = "2"
|
|
35
|
-
|
|
36
|
-
[profile.release]
|
|
37
|
-
lto = true
|
|
38
|
-
strip = "symbols"
|
package/build.rs
DELETED
package/src/lib.rs
DELETED
|
@@ -1,561 +0,0 @@
|
|
|
1
|
-
#![deny(clippy::all)]
|
|
2
|
-
#![allow(unexpected_cfgs)]
|
|
3
|
-
|
|
4
|
-
use std::{collections::HashMap, sync::Arc, time::Duration};
|
|
5
|
-
|
|
6
|
-
use axum::{
|
|
7
|
-
body::Body,
|
|
8
|
-
http::{request::Parts, Request, StatusCode},
|
|
9
|
-
response::IntoResponse,
|
|
10
|
-
};
|
|
11
|
-
use celerity_helpers::{
|
|
12
|
-
env::ProcessEnvVars,
|
|
13
|
-
request::{
|
|
14
|
-
cookies_from_headers, path_params_from_request_parts, query_from_uri, to_request_body,
|
|
15
|
-
},
|
|
16
|
-
runtime_types::{RuntimeCallMode, RuntimePlatform},
|
|
17
|
-
};
|
|
18
|
-
use celerity_runtime_core::{
|
|
19
|
-
application::Application,
|
|
20
|
-
auth_http::AuthClaims,
|
|
21
|
-
config::{
|
|
22
|
-
ApiConfig, AppConfig, ClientIpSource, HttpConfig, HttpHandlerDefinition, RuntimeConfig,
|
|
23
|
-
WebSocketConfig,
|
|
24
|
-
},
|
|
25
|
-
request::{MatchedRoute, RequestId, ResolvedClientIp, ResolvedUserAgent},
|
|
26
|
-
telemetry_utils::extract_trace_context,
|
|
27
|
-
};
|
|
28
|
-
use napi::bindgen_prelude::*;
|
|
29
|
-
use napi::threadsafe_function::ThreadsafeFunction;
|
|
30
|
-
use napi_derive::napi;
|
|
31
|
-
use serde::{Deserialize, Serialize};
|
|
32
|
-
use tokio::time;
|
|
33
|
-
|
|
34
|
-
const MAX_REQUEST_BODY_SIZE: usize = 10 * 1024 * 1024; // 10 MiB
|
|
35
|
-
|
|
36
|
-
/// A weak ThreadsafeFunction that does not prevent the Node.js event loop from exiting.
|
|
37
|
-
type WeakTsfn =
|
|
38
|
-
ThreadsafeFunction<JsRequestWrapper, Promise<Response>, JsRequestWrapper, Status, true, true>;
|
|
39
|
-
|
|
40
|
-
#[napi(object)]
|
|
41
|
-
pub struct CoreRuntimeConfig {
|
|
42
|
-
pub blueprint_config_path: String,
|
|
43
|
-
pub server_port: i32,
|
|
44
|
-
pub server_loopback_only: Option<bool>,
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
#[napi(object)]
|
|
48
|
-
pub struct CoreRuntimeAppConfig {
|
|
49
|
-
pub api: Option<CoreApiConfig>,
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
impl From<AppConfig> for CoreRuntimeAppConfig {
|
|
53
|
-
fn from(app_config: AppConfig) -> Self {
|
|
54
|
-
let api = app_config.api.map(|api_config| api_config.into());
|
|
55
|
-
Self { api }
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
#[napi(object)]
|
|
60
|
-
pub struct CoreApiConfig {
|
|
61
|
-
pub http: Option<CoreHttpConfig>,
|
|
62
|
-
pub websocket: Option<CoreWebsocketConfig>,
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
impl From<ApiConfig> for CoreApiConfig {
|
|
66
|
-
fn from(api_config: ApiConfig) -> Self {
|
|
67
|
-
let http = api_config.http.map(|http_config| http_config.into());
|
|
68
|
-
let websocket = api_config
|
|
69
|
-
.websocket
|
|
70
|
-
.map(|websocket_config| websocket_config.into());
|
|
71
|
-
Self { http, websocket }
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
#[napi(object)]
|
|
76
|
-
pub struct CoreHttpConfig {
|
|
77
|
-
pub handlers: Vec<CoreHttpHandlerDefinition>,
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
impl From<HttpConfig> for CoreHttpConfig {
|
|
81
|
-
fn from(http_config: HttpConfig) -> Self {
|
|
82
|
-
let handlers = http_config
|
|
83
|
-
.handlers
|
|
84
|
-
.into_iter()
|
|
85
|
-
.map(|handler| handler.into())
|
|
86
|
-
.collect::<Vec<_>>();
|
|
87
|
-
Self { handlers }
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
#[napi(object)]
|
|
92
|
-
pub struct CoreWebsocketConfig {}
|
|
93
|
-
|
|
94
|
-
impl From<WebSocketConfig> for CoreWebsocketConfig {
|
|
95
|
-
fn from(_: WebSocketConfig) -> Self {
|
|
96
|
-
Self {}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
#[napi(object)]
|
|
101
|
-
pub struct CoreHttpHandlerDefinition {
|
|
102
|
-
pub path: String,
|
|
103
|
-
pub method: String,
|
|
104
|
-
pub location: String,
|
|
105
|
-
pub handler: String,
|
|
106
|
-
pub timeout: i64,
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
impl From<HttpHandlerDefinition> for CoreHttpHandlerDefinition {
|
|
110
|
-
fn from(handler: HttpHandlerDefinition) -> Self {
|
|
111
|
-
Self {
|
|
112
|
-
path: handler.path,
|
|
113
|
-
method: handler.method,
|
|
114
|
-
location: handler.location,
|
|
115
|
-
handler: handler.handler,
|
|
116
|
-
timeout: handler.timeout,
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
#[napi(object)]
|
|
122
|
-
pub struct Response {
|
|
123
|
-
pub status: u16,
|
|
124
|
-
pub headers: Option<HashMap<String, String>>,
|
|
125
|
-
pub body: Option<String>,
|
|
126
|
-
pub binary_body: Option<Buffer>,
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
impl IntoResponse for Response {
|
|
130
|
-
fn into_response(self) -> axum::response::Response<Body> {
|
|
131
|
-
let mut builder = axum::response::Response::builder();
|
|
132
|
-
for (key, value) in self.headers.unwrap_or_default() {
|
|
133
|
-
builder = builder.header(key, value);
|
|
134
|
-
}
|
|
135
|
-
builder = builder.status(self.status);
|
|
136
|
-
let body = if let Some(binary) = self.binary_body {
|
|
137
|
-
Body::from(binary.to_vec())
|
|
138
|
-
} else {
|
|
139
|
-
Body::from(self.body.unwrap_or_default())
|
|
140
|
-
};
|
|
141
|
-
builder.body(body).unwrap()
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
#[derive(Debug)]
|
|
146
|
-
pub enum JsRequestWrapperBody {
|
|
147
|
-
Text(String),
|
|
148
|
-
Binary(Vec<u8>),
|
|
149
|
-
EmptyBody,
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
#[napi(js_name = "Request")]
|
|
153
|
-
pub struct JsRequestWrapper {
|
|
154
|
-
inner_body: JsRequestWrapperBody,
|
|
155
|
-
inner_parts: Parts,
|
|
156
|
-
path_params: HashMap<String, String>,
|
|
157
|
-
query: HashMap<String, Vec<String>>,
|
|
158
|
-
cookies: HashMap<String, String>,
|
|
159
|
-
content_type: String,
|
|
160
|
-
req_path: String,
|
|
161
|
-
request_id: String,
|
|
162
|
-
request_time: String,
|
|
163
|
-
auth_claims: Option<serde_json::Value>,
|
|
164
|
-
client_ip: String,
|
|
165
|
-
trace_context: Option<HashMap<String, String>>,
|
|
166
|
-
user_agent: String,
|
|
167
|
-
matched_route: Option<String>,
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
#[napi]
|
|
171
|
-
impl JsRequestWrapper {
|
|
172
|
-
/// Allows the creation of requests, primarily for test purposes.
|
|
173
|
-
/// In normal circumstances, the request will be created by
|
|
174
|
-
/// the runtime and passed to the handler.
|
|
175
|
-
#[napi(constructor)]
|
|
176
|
-
pub fn new(method: String, uri: String, headers: HashMap<String, String>) -> Self {
|
|
177
|
-
let mut builder = Request::builder().method(method.as_str()).uri(uri.clone());
|
|
178
|
-
for (key, value) in headers {
|
|
179
|
-
builder = builder.header(key, value);
|
|
180
|
-
}
|
|
181
|
-
let request = builder.body(Body::empty()).unwrap();
|
|
182
|
-
let (parts, _) = request.into_parts();
|
|
183
|
-
Self {
|
|
184
|
-
inner_parts: parts,
|
|
185
|
-
inner_body: JsRequestWrapperBody::EmptyBody,
|
|
186
|
-
path_params: HashMap::new(),
|
|
187
|
-
query: HashMap::new(),
|
|
188
|
-
cookies: HashMap::new(),
|
|
189
|
-
content_type: String::new(),
|
|
190
|
-
req_path: uri,
|
|
191
|
-
request_id: String::new(),
|
|
192
|
-
request_time: String::new(),
|
|
193
|
-
auth_claims: None,
|
|
194
|
-
client_ip: String::new(),
|
|
195
|
-
trace_context: None,
|
|
196
|
-
user_agent: String::new(),
|
|
197
|
-
matched_route: None,
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async fn from_axum_req(req: axum::extract::Request<Body>) -> Result<Self> {
|
|
202
|
-
let (mut parts, body) = req.into_parts();
|
|
203
|
-
|
|
204
|
-
// Extract pre-processed fields before consuming the body.
|
|
205
|
-
let path_params = path_params_from_request_parts(&mut parts)
|
|
206
|
-
.await
|
|
207
|
-
.unwrap_or_default();
|
|
208
|
-
let query = query_from_uri(&parts.uri).unwrap_or_default();
|
|
209
|
-
let cookies = cookies_from_headers(&parts.headers);
|
|
210
|
-
let req_path = parts.uri.path().to_string();
|
|
211
|
-
let request_id = parts
|
|
212
|
-
.extensions
|
|
213
|
-
.get::<RequestId>()
|
|
214
|
-
.map(|id| id.0.clone())
|
|
215
|
-
.unwrap_or_default();
|
|
216
|
-
let auth_claims = parts
|
|
217
|
-
.extensions
|
|
218
|
-
.get::<AuthClaims>()
|
|
219
|
-
.and_then(|ac| ac.0.clone());
|
|
220
|
-
let client_ip = parts
|
|
221
|
-
.extensions
|
|
222
|
-
.get::<ResolvedClientIp>()
|
|
223
|
-
.map(|rci| rci.0.to_string())
|
|
224
|
-
.unwrap_or_default();
|
|
225
|
-
let user_agent = parts
|
|
226
|
-
.extensions
|
|
227
|
-
.get::<ResolvedUserAgent>()
|
|
228
|
-
.map(|ua| ua.0.clone())
|
|
229
|
-
.unwrap_or_default();
|
|
230
|
-
let matched_route = parts
|
|
231
|
-
.extensions
|
|
232
|
-
.get::<MatchedRoute>()
|
|
233
|
-
.map(|mr| mr.0.clone());
|
|
234
|
-
let trace_context = extract_trace_context();
|
|
235
|
-
let request_time = chrono::Utc::now().to_rfc3339();
|
|
236
|
-
|
|
237
|
-
// Read and process the body.
|
|
238
|
-
let content_length = parts
|
|
239
|
-
.headers
|
|
240
|
-
.get("content-length")
|
|
241
|
-
.and_then(|value| value.to_str().ok())
|
|
242
|
-
.and_then(|value| value.parse::<usize>().ok())
|
|
243
|
-
.unwrap_or(0);
|
|
244
|
-
|
|
245
|
-
let (inner_body, content_type) = if content_length > 0 {
|
|
246
|
-
let bytes = axum::body::to_bytes(body, MAX_REQUEST_BODY_SIZE)
|
|
247
|
-
.await
|
|
248
|
-
.map_err(|err| {
|
|
249
|
-
Error::new(
|
|
250
|
-
Status::GenericFailure,
|
|
251
|
-
format!("failed to read request body, {err}"),
|
|
252
|
-
)
|
|
253
|
-
})?;
|
|
254
|
-
let ct_header = parts.headers.get("content-type").cloned();
|
|
255
|
-
let (text_body, binary_body, content_type_str) = to_request_body(&bytes, ct_header);
|
|
256
|
-
let body = if let Some(text) = text_body {
|
|
257
|
-
JsRequestWrapperBody::Text(text)
|
|
258
|
-
} else if let Some(_binary) = binary_body {
|
|
259
|
-
JsRequestWrapperBody::Binary(bytes.to_vec())
|
|
260
|
-
} else {
|
|
261
|
-
JsRequestWrapperBody::EmptyBody
|
|
262
|
-
};
|
|
263
|
-
(body, content_type_str)
|
|
264
|
-
} else {
|
|
265
|
-
(
|
|
266
|
-
JsRequestWrapperBody::EmptyBody,
|
|
267
|
-
parts
|
|
268
|
-
.headers
|
|
269
|
-
.get("content-type")
|
|
270
|
-
.and_then(|v| v.to_str().ok())
|
|
271
|
-
.unwrap_or("")
|
|
272
|
-
.to_string(),
|
|
273
|
-
)
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
Ok(Self {
|
|
277
|
-
inner_parts: parts,
|
|
278
|
-
inner_body,
|
|
279
|
-
path_params,
|
|
280
|
-
query,
|
|
281
|
-
cookies,
|
|
282
|
-
content_type,
|
|
283
|
-
req_path,
|
|
284
|
-
request_id,
|
|
285
|
-
request_time,
|
|
286
|
-
auth_claims,
|
|
287
|
-
client_ip,
|
|
288
|
-
trace_context,
|
|
289
|
-
user_agent,
|
|
290
|
-
matched_route,
|
|
291
|
-
})
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/// The HTTP version used for the request.
|
|
295
|
-
#[napi(getter)]
|
|
296
|
-
pub fn http_version(&self) -> String {
|
|
297
|
-
format!("{:?}", self.inner_parts.version)
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/// The HTTP method of the request.
|
|
301
|
-
#[napi(getter)]
|
|
302
|
-
pub fn method(&self) -> String {
|
|
303
|
-
self.inner_parts.method.to_string()
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/// The URI of the request.
|
|
307
|
-
#[napi(getter)]
|
|
308
|
-
pub fn uri(&self) -> String {
|
|
309
|
-
self.inner_parts.uri.to_string()
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/// The headers of the request as a map of header name to list of values.
|
|
313
|
-
#[napi(getter)]
|
|
314
|
-
pub fn headers(&self) -> HashMap<String, Vec<String>> {
|
|
315
|
-
let mut map: HashMap<String, Vec<String>> = HashMap::new();
|
|
316
|
-
for (key, value) in self.inner_parts.headers.iter() {
|
|
317
|
-
map
|
|
318
|
-
.entry(key.as_str().to_string())
|
|
319
|
-
.or_default()
|
|
320
|
-
.push(value.to_str().unwrap_or_default().to_string());
|
|
321
|
-
}
|
|
322
|
-
map
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/// The path of the request (e.g. "/orders/123").
|
|
326
|
-
#[napi(getter)]
|
|
327
|
-
pub fn path(&self) -> String {
|
|
328
|
-
self.req_path.clone()
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/// Path parameters extracted from the URL (e.g. { "orderId": "123" }).
|
|
332
|
-
#[napi(getter)]
|
|
333
|
-
pub fn path_params(&self) -> HashMap<String, String> {
|
|
334
|
-
self.path_params.clone()
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/// Query parameters, supporting multiple values per key.
|
|
338
|
-
#[napi(getter)]
|
|
339
|
-
pub fn query(&self) -> HashMap<String, Vec<String>> {
|
|
340
|
-
self.query.clone()
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
/// Cookies from the request.
|
|
344
|
-
#[napi(getter)]
|
|
345
|
-
pub fn cookies(&self) -> HashMap<String, String> {
|
|
346
|
-
self.cookies.clone()
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/// The content type of the request body.
|
|
350
|
-
#[napi(getter)]
|
|
351
|
-
pub fn content_type(&self) -> String {
|
|
352
|
-
self.content_type.clone()
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/// The request ID (from x-request-id header or auto-generated).
|
|
356
|
-
#[napi(getter)]
|
|
357
|
-
pub fn request_id(&self) -> String {
|
|
358
|
-
self.request_id.clone()
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/// The request time as an ISO 8601 string.
|
|
362
|
-
#[napi(getter)]
|
|
363
|
-
pub fn request_time(&self) -> String {
|
|
364
|
-
self.request_time.clone()
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
/// Authentication claims from the auth middleware, or null if no auth.
|
|
368
|
-
#[napi(getter)]
|
|
369
|
-
pub fn auth(&self) -> Option<serde_json::Value> {
|
|
370
|
-
self.auth_claims.clone()
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/// The client IP address resolved by the runtime.
|
|
374
|
-
#[napi(getter)]
|
|
375
|
-
pub fn client_ip(&self) -> String {
|
|
376
|
-
self.client_ip.clone()
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
/// The trace context for distributed tracing propagation.
|
|
380
|
-
/// Contains "traceparent" (W3C) and optionally "xray_trace_id" (AWS).
|
|
381
|
-
#[napi(getter)]
|
|
382
|
-
pub fn trace_context(&self) -> Option<HashMap<String, String>> {
|
|
383
|
-
self.trace_context.clone()
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/// The user-agent string from the request.
|
|
387
|
-
#[napi(getter)]
|
|
388
|
-
pub fn user_agent(&self) -> String {
|
|
389
|
-
self.user_agent.clone()
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/// The matched route pattern (e.g. "/orders/{orderId}"), or null if unavailable.
|
|
393
|
-
#[napi(getter)]
|
|
394
|
-
pub fn matched_route(&self) -> Option<String> {
|
|
395
|
-
self.matched_route.clone()
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/// The text body of the request, or null if the body is empty or binary.
|
|
399
|
-
#[napi(getter)]
|
|
400
|
-
pub fn text_body(&self) -> Option<String> {
|
|
401
|
-
match &self.inner_body {
|
|
402
|
-
JsRequestWrapperBody::Text(text) => Some(text.clone()),
|
|
403
|
-
_ => None,
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
/// The binary body of the request as a Buffer, or null if the body is empty or text.
|
|
408
|
-
#[napi(getter)]
|
|
409
|
-
pub fn binary_body(&self) -> Option<Buffer> {
|
|
410
|
-
match &self.inner_body {
|
|
411
|
-
JsRequestWrapperBody::Binary(bytes) => Some(Buffer::from(bytes.clone())),
|
|
412
|
-
_ => None,
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
#[napi]
|
|
418
|
-
pub struct CoreRuntimeApplication {
|
|
419
|
-
inner: Application,
|
|
420
|
-
tsfn_cache: Vec<Arc<WeakTsfn>>,
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
#[napi]
|
|
424
|
-
impl CoreRuntimeApplication {
|
|
425
|
-
#[napi(constructor)]
|
|
426
|
-
pub fn new(runtime_config: CoreRuntimeConfig) -> Self {
|
|
427
|
-
let native_runtime_config = RuntimeConfig {
|
|
428
|
-
blueprint_config_path: runtime_config.blueprint_config_path,
|
|
429
|
-
runtime_call_mode: RuntimeCallMode::Ffi,
|
|
430
|
-
server_loopback_only: runtime_config.server_loopback_only,
|
|
431
|
-
server_port: runtime_config.server_port,
|
|
432
|
-
local_api_port: 8259,
|
|
433
|
-
use_custom_health_check: None,
|
|
434
|
-
service_name: "CelerityTestService".to_string(),
|
|
435
|
-
platform: RuntimePlatform::Local,
|
|
436
|
-
trace_otlp_collector_endpoint: "http://localhost:4317".to_string(),
|
|
437
|
-
runtime_max_diagnostics_level: tracing::Level::INFO,
|
|
438
|
-
test_mode: false,
|
|
439
|
-
api_resource: None,
|
|
440
|
-
consumer_app: None,
|
|
441
|
-
schedule_app: None,
|
|
442
|
-
resource_store_verify_tls: true,
|
|
443
|
-
resource_store_cache_entry_ttl: 600,
|
|
444
|
-
resource_store_cleanup_interval: 3600,
|
|
445
|
-
client_ip_source: ClientIpSource::ConnectInfo,
|
|
446
|
-
};
|
|
447
|
-
let inner = Application::new(native_runtime_config, Box::new(ProcessEnvVars::new()));
|
|
448
|
-
CoreRuntimeApplication {
|
|
449
|
-
inner,
|
|
450
|
-
tsfn_cache: vec![],
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
#[napi]
|
|
455
|
-
pub fn setup(&mut self) -> Result<CoreRuntimeAppConfig> {
|
|
456
|
-
let app_config = self.inner.setup().map_err(|err| {
|
|
457
|
-
Error::new(
|
|
458
|
-
Status::GenericFailure,
|
|
459
|
-
format!("failed to setup core runtime, {err}"),
|
|
460
|
-
)
|
|
461
|
-
})?;
|
|
462
|
-
Ok(app_config.into())
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
#[napi]
|
|
466
|
-
pub fn register_http_handler(
|
|
467
|
-
&mut self,
|
|
468
|
-
path: String,
|
|
469
|
-
method: String,
|
|
470
|
-
timeout_seconds: Option<i64>,
|
|
471
|
-
#[napi(ts_arg_type = "(err: Error | null, request: Request) => Promise<Response>")]
|
|
472
|
-
handler: WeakTsfn,
|
|
473
|
-
) -> Result<()> {
|
|
474
|
-
let tsfn = Arc::new(handler);
|
|
475
|
-
self.tsfn_cache.push(tsfn.clone());
|
|
476
|
-
let timeout_secs = timeout_seconds.unwrap_or(60) as u64;
|
|
477
|
-
|
|
478
|
-
let handler = move |req| {
|
|
479
|
-
let tsfn = tsfn.clone();
|
|
480
|
-
async move {
|
|
481
|
-
let js_req_wrapper = JsRequestWrapper::from_axum_req(req)
|
|
482
|
-
.await
|
|
483
|
-
.map_err(|err| HandlerError::new(err.to_string()))?;
|
|
484
|
-
let promise = tsfn
|
|
485
|
-
.call_async(Ok(js_req_wrapper))
|
|
486
|
-
.await
|
|
487
|
-
.map_err(|err| HandlerError::new(err.to_string()))?;
|
|
488
|
-
let sleep = time::sleep(Duration::from_secs(timeout_secs));
|
|
489
|
-
tokio::select! {
|
|
490
|
-
_ = sleep => {
|
|
491
|
-
Err(HandlerError::timeout())
|
|
492
|
-
}
|
|
493
|
-
value = promise => {
|
|
494
|
-
Ok::<Response, HandlerError>(value.map_err(|err| HandlerError::new(err.to_string()))?)
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
}
|
|
498
|
-
};
|
|
499
|
-
self.inner.register_http_handler(&path, &method, handler);
|
|
500
|
-
Ok(())
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
#[allow(clippy::missing_safety_doc)]
|
|
504
|
-
#[napi]
|
|
505
|
-
pub async unsafe fn run(&mut self, block: bool) -> Result<()> {
|
|
506
|
-
self.inner.run(block).await.map_err(|err| {
|
|
507
|
-
Error::new(
|
|
508
|
-
Status::GenericFailure,
|
|
509
|
-
format!("failed to start core runtime, {err}"),
|
|
510
|
-
)
|
|
511
|
-
})?;
|
|
512
|
-
Ok(())
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
#[napi]
|
|
516
|
-
pub fn shutdown(&mut self) -> Result<()> {
|
|
517
|
-
self.inner.shutdown();
|
|
518
|
-
self.tsfn_cache.clear();
|
|
519
|
-
Ok(())
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
#[derive(Debug, Serialize, Deserialize)]
|
|
524
|
-
pub struct HandlerError {
|
|
525
|
-
pub message: String,
|
|
526
|
-
#[serde(skip)]
|
|
527
|
-
pub is_timeout: bool,
|
|
528
|
-
}
|
|
529
|
-
|
|
530
|
-
impl HandlerError {
|
|
531
|
-
pub fn new(message: String) -> Self {
|
|
532
|
-
Self {
|
|
533
|
-
message,
|
|
534
|
-
is_timeout: false,
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
pub fn timeout() -> Self {
|
|
539
|
-
Self {
|
|
540
|
-
message: "handler timed out".to_string(),
|
|
541
|
-
is_timeout: true,
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
impl std::fmt::Display for HandlerError {
|
|
547
|
-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
548
|
-
write!(f, "{}", self.message)
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
impl IntoResponse for HandlerError {
|
|
553
|
-
fn into_response(self) -> axum::response::Response<Body> {
|
|
554
|
-
let status = if self.is_timeout {
|
|
555
|
-
StatusCode::GATEWAY_TIMEOUT
|
|
556
|
-
} else {
|
|
557
|
-
StatusCode::INTERNAL_SERVER_ERROR
|
|
558
|
-
};
|
|
559
|
-
(status, axum::response::Json(self)).into_response()
|
|
560
|
-
}
|
|
561
|
-
}
|
package/tsconfig.json
DELETED