@rivetkit/rivetkit-native 2.2.1-pr.4600.bc7743e

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 ADDED
@@ -0,0 +1,203 @@
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 2023 Rivet Gaming, Inc.
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.
203
+
package/index.d.ts ADDED
@@ -0,0 +1,93 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export interface JsBindParam {
7
+ kind: string
8
+ intValue?: number
9
+ floatValue?: number
10
+ textValue?: string
11
+ blobValue?: Buffer
12
+ }
13
+ export interface ExecuteResult {
14
+ changes: number
15
+ }
16
+ export interface QueryResult {
17
+ columns: Array<string>
18
+ rows: Array<Array<any>>
19
+ }
20
+ /** Open a native SQLite database backed by the envoy's KV channel. */
21
+ export declare function openDatabaseFromEnvoy(jsHandle: JsEnvoyHandle, actorId: string): Promise<JsNativeDatabase>
22
+ /** Configuration for starting the native envoy client. */
23
+ export interface JsEnvoyConfig {
24
+ endpoint: string
25
+ token: string
26
+ namespace: string
27
+ poolName: string
28
+ version: number
29
+ metadata?: any
30
+ notGlobal: boolean
31
+ /**
32
+ * Log level for the Rust tracing subscriber (e.g. "trace", "debug", "info", "warn", "error").
33
+ * Falls back to RIVET_LOG_LEVEL, then LOG_LEVEL, then RUST_LOG env vars. Defaults to "warn".
34
+ */
35
+ logLevel?: string
36
+ }
37
+ /** Options for KV list operations. */
38
+ export interface JsKvListOptions {
39
+ reverse?: boolean
40
+ limit?: number
41
+ }
42
+ /** A key-value entry returned from KV list operations. */
43
+ export interface JsKvEntry {
44
+ key: Buffer
45
+ value: Buffer
46
+ }
47
+ /** A single hibernating request entry. */
48
+ export interface HibernatingRequestEntry {
49
+ gatewayId: Buffer
50
+ requestId: Buffer
51
+ }
52
+ /**
53
+ * Start the native envoy client synchronously.
54
+ *
55
+ * Returns a handle immediately. The caller must call `await handle.started()`
56
+ * to wait for the connection to be ready.
57
+ */
58
+ export declare function startEnvoySyncJs(config: JsEnvoyConfig, eventCallback: (event: any) => void): JsEnvoyHandle
59
+ /** Start the native envoy client asynchronously. */
60
+ export declare function startEnvoyJs(config: JsEnvoyConfig, eventCallback: (event: any) => void): JsEnvoyHandle
61
+ /** Native SQLite database handle exposed to JavaScript. */
62
+ export declare class JsNativeDatabase {
63
+ run(sql: string, params?: Array<JsBindParam> | undefined | null): Promise<ExecuteResult>
64
+ query(sql: string, params?: Array<JsBindParam> | undefined | null): Promise<QueryResult>
65
+ exec(sql: string): Promise<QueryResult>
66
+ close(): Promise<void>
67
+ }
68
+ /** Native envoy handle exposed to JavaScript via N-API. */
69
+ export declare class JsEnvoyHandle {
70
+ started(): Promise<void>
71
+ shutdown(immediate: boolean): void
72
+ get envoyKey(): string
73
+ sleepActor(actorId: string, generation?: number | undefined | null): void
74
+ stopActor(actorId: string, generation?: number | undefined | null, error?: string | undefined | null): void
75
+ destroyActor(actorId: string, generation?: number | undefined | null): void
76
+ setAlarm(actorId: string, alarmTs?: number | undefined | null, generation?: number | undefined | null): void
77
+ kvGet(actorId: string, keys: Array<Buffer>): Promise<Array<Buffer | undefined | null>>
78
+ kvPut(actorId: string, entries: Array<JsKvEntry>): Promise<void>
79
+ kvDelete(actorId: string, keys: Array<Buffer>): Promise<void>
80
+ kvDeleteRange(actorId: string, start: Buffer, end: Buffer): Promise<void>
81
+ kvListAll(actorId: string, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
82
+ kvListRange(actorId: string, start: Buffer, end: Buffer, exclusive?: boolean | undefined | null, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
83
+ kvListPrefix(actorId: string, prefix: Buffer, options?: JsKvListOptions | undefined | null): Promise<Array<JsKvEntry>>
84
+ kvDrop(actorId: string): Promise<void>
85
+ restoreHibernatingRequests(actorId: string, requests: Array<HibernatingRequestEntry>): void
86
+ sendHibernatableWebSocketMessageAck(gatewayId: Buffer, requestId: Buffer, clientMessageIndex: number): void
87
+ /** Send a message on an open WebSocket connection identified by messageIdHex. */
88
+ sendWsMessage(gatewayId: Buffer, requestId: Buffer, data: Buffer, binary: boolean): Promise<void>
89
+ /** Close an open WebSocket connection. */
90
+ closeWebsocket(gatewayId: Buffer, requestId: Buffer, code?: number | undefined | null, reason?: string | undefined | null): Promise<void>
91
+ startServerless(payload: Buffer): Promise<void>
92
+ respondCallback(responseId: string, data: any): Promise<void>
93
+ }
package/index.js ADDED
@@ -0,0 +1,319 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /* prettier-ignore */
4
+
5
+ /* auto-generated by NAPI-RS */
6
+
7
+ const { existsSync, readFileSync } = require('fs')
8
+ const { join } = require('path')
9
+
10
+ const { platform, arch } = process
11
+
12
+ let nativeBinding = null
13
+ let localFileExisted = false
14
+ let loadError = null
15
+
16
+ function isMusl() {
17
+ // For Node 10
18
+ if (!process.report || typeof process.report.getReport !== 'function') {
19
+ try {
20
+ const lddPath = require('child_process').execSync('which ldd').toString().trim()
21
+ return readFileSync(lddPath, 'utf8').includes('musl')
22
+ } catch (e) {
23
+ return true
24
+ }
25
+ } else {
26
+ const { glibcVersionRuntime } = process.report.getReport().header
27
+ return !glibcVersionRuntime
28
+ }
29
+ }
30
+
31
+ switch (platform) {
32
+ case 'android':
33
+ switch (arch) {
34
+ case 'arm64':
35
+ localFileExisted = existsSync(join(__dirname, 'rivetkit-native.android-arm64.node'))
36
+ try {
37
+ if (localFileExisted) {
38
+ nativeBinding = require('./rivetkit-native.android-arm64.node')
39
+ } else {
40
+ nativeBinding = require('@rivetkit/rivetkit-native-android-arm64')
41
+ }
42
+ } catch (e) {
43
+ loadError = e
44
+ }
45
+ break
46
+ case 'arm':
47
+ localFileExisted = existsSync(join(__dirname, 'rivetkit-native.android-arm-eabi.node'))
48
+ try {
49
+ if (localFileExisted) {
50
+ nativeBinding = require('./rivetkit-native.android-arm-eabi.node')
51
+ } else {
52
+ nativeBinding = require('@rivetkit/rivetkit-native-android-arm-eabi')
53
+ }
54
+ } catch (e) {
55
+ loadError = e
56
+ }
57
+ break
58
+ default:
59
+ throw new Error(`Unsupported architecture on Android ${arch}`)
60
+ }
61
+ break
62
+ case 'win32':
63
+ switch (arch) {
64
+ case 'x64':
65
+ localFileExisted = existsSync(
66
+ join(__dirname, 'rivetkit-native.win32-x64-msvc.node')
67
+ )
68
+ try {
69
+ if (localFileExisted) {
70
+ nativeBinding = require('./rivetkit-native.win32-x64-msvc.node')
71
+ } else {
72
+ nativeBinding = require('@rivetkit/rivetkit-native-win32-x64-msvc')
73
+ }
74
+ } catch (e) {
75
+ loadError = e
76
+ }
77
+ break
78
+ case 'ia32':
79
+ localFileExisted = existsSync(
80
+ join(__dirname, 'rivetkit-native.win32-ia32-msvc.node')
81
+ )
82
+ try {
83
+ if (localFileExisted) {
84
+ nativeBinding = require('./rivetkit-native.win32-ia32-msvc.node')
85
+ } else {
86
+ nativeBinding = require('@rivetkit/rivetkit-native-win32-ia32-msvc')
87
+ }
88
+ } catch (e) {
89
+ loadError = e
90
+ }
91
+ break
92
+ case 'arm64':
93
+ localFileExisted = existsSync(
94
+ join(__dirname, 'rivetkit-native.win32-arm64-msvc.node')
95
+ )
96
+ try {
97
+ if (localFileExisted) {
98
+ nativeBinding = require('./rivetkit-native.win32-arm64-msvc.node')
99
+ } else {
100
+ nativeBinding = require('@rivetkit/rivetkit-native-win32-arm64-msvc')
101
+ }
102
+ } catch (e) {
103
+ loadError = e
104
+ }
105
+ break
106
+ default:
107
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
108
+ }
109
+ break
110
+ case 'darwin':
111
+ localFileExisted = existsSync(join(__dirname, 'rivetkit-native.darwin-universal.node'))
112
+ try {
113
+ if (localFileExisted) {
114
+ nativeBinding = require('./rivetkit-native.darwin-universal.node')
115
+ } else {
116
+ nativeBinding = require('@rivetkit/rivetkit-native-darwin-universal')
117
+ }
118
+ break
119
+ } catch {}
120
+ switch (arch) {
121
+ case 'x64':
122
+ localFileExisted = existsSync(join(__dirname, 'rivetkit-native.darwin-x64.node'))
123
+ try {
124
+ if (localFileExisted) {
125
+ nativeBinding = require('./rivetkit-native.darwin-x64.node')
126
+ } else {
127
+ nativeBinding = require('@rivetkit/rivetkit-native-darwin-x64')
128
+ }
129
+ } catch (e) {
130
+ loadError = e
131
+ }
132
+ break
133
+ case 'arm64':
134
+ localFileExisted = existsSync(
135
+ join(__dirname, 'rivetkit-native.darwin-arm64.node')
136
+ )
137
+ try {
138
+ if (localFileExisted) {
139
+ nativeBinding = require('./rivetkit-native.darwin-arm64.node')
140
+ } else {
141
+ nativeBinding = require('@rivetkit/rivetkit-native-darwin-arm64')
142
+ }
143
+ } catch (e) {
144
+ loadError = e
145
+ }
146
+ break
147
+ default:
148
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
149
+ }
150
+ break
151
+ case 'freebsd':
152
+ if (arch !== 'x64') {
153
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
154
+ }
155
+ localFileExisted = existsSync(join(__dirname, 'rivetkit-native.freebsd-x64.node'))
156
+ try {
157
+ if (localFileExisted) {
158
+ nativeBinding = require('./rivetkit-native.freebsd-x64.node')
159
+ } else {
160
+ nativeBinding = require('@rivetkit/rivetkit-native-freebsd-x64')
161
+ }
162
+ } catch (e) {
163
+ loadError = e
164
+ }
165
+ break
166
+ case 'linux':
167
+ switch (arch) {
168
+ case 'x64':
169
+ if (isMusl()) {
170
+ localFileExisted = existsSync(
171
+ join(__dirname, 'rivetkit-native.linux-x64-musl.node')
172
+ )
173
+ try {
174
+ if (localFileExisted) {
175
+ nativeBinding = require('./rivetkit-native.linux-x64-musl.node')
176
+ } else {
177
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-x64-musl')
178
+ }
179
+ } catch (e) {
180
+ loadError = e
181
+ }
182
+ } else {
183
+ localFileExisted = existsSync(
184
+ join(__dirname, 'rivetkit-native.linux-x64-gnu.node')
185
+ )
186
+ try {
187
+ if (localFileExisted) {
188
+ nativeBinding = require('./rivetkit-native.linux-x64-gnu.node')
189
+ } else {
190
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-x64-gnu')
191
+ }
192
+ } catch (e) {
193
+ loadError = e
194
+ }
195
+ }
196
+ break
197
+ case 'arm64':
198
+ if (isMusl()) {
199
+ localFileExisted = existsSync(
200
+ join(__dirname, 'rivetkit-native.linux-arm64-musl.node')
201
+ )
202
+ try {
203
+ if (localFileExisted) {
204
+ nativeBinding = require('./rivetkit-native.linux-arm64-musl.node')
205
+ } else {
206
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-arm64-musl')
207
+ }
208
+ } catch (e) {
209
+ loadError = e
210
+ }
211
+ } else {
212
+ localFileExisted = existsSync(
213
+ join(__dirname, 'rivetkit-native.linux-arm64-gnu.node')
214
+ )
215
+ try {
216
+ if (localFileExisted) {
217
+ nativeBinding = require('./rivetkit-native.linux-arm64-gnu.node')
218
+ } else {
219
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-arm64-gnu')
220
+ }
221
+ } catch (e) {
222
+ loadError = e
223
+ }
224
+ }
225
+ break
226
+ case 'arm':
227
+ if (isMusl()) {
228
+ localFileExisted = existsSync(
229
+ join(__dirname, 'rivetkit-native.linux-arm-musleabihf.node')
230
+ )
231
+ try {
232
+ if (localFileExisted) {
233
+ nativeBinding = require('./rivetkit-native.linux-arm-musleabihf.node')
234
+ } else {
235
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-arm-musleabihf')
236
+ }
237
+ } catch (e) {
238
+ loadError = e
239
+ }
240
+ } else {
241
+ localFileExisted = existsSync(
242
+ join(__dirname, 'rivetkit-native.linux-arm-gnueabihf.node')
243
+ )
244
+ try {
245
+ if (localFileExisted) {
246
+ nativeBinding = require('./rivetkit-native.linux-arm-gnueabihf.node')
247
+ } else {
248
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-arm-gnueabihf')
249
+ }
250
+ } catch (e) {
251
+ loadError = e
252
+ }
253
+ }
254
+ break
255
+ case 'riscv64':
256
+ if (isMusl()) {
257
+ localFileExisted = existsSync(
258
+ join(__dirname, 'rivetkit-native.linux-riscv64-musl.node')
259
+ )
260
+ try {
261
+ if (localFileExisted) {
262
+ nativeBinding = require('./rivetkit-native.linux-riscv64-musl.node')
263
+ } else {
264
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-riscv64-musl')
265
+ }
266
+ } catch (e) {
267
+ loadError = e
268
+ }
269
+ } else {
270
+ localFileExisted = existsSync(
271
+ join(__dirname, 'rivetkit-native.linux-riscv64-gnu.node')
272
+ )
273
+ try {
274
+ if (localFileExisted) {
275
+ nativeBinding = require('./rivetkit-native.linux-riscv64-gnu.node')
276
+ } else {
277
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-riscv64-gnu')
278
+ }
279
+ } catch (e) {
280
+ loadError = e
281
+ }
282
+ }
283
+ break
284
+ case 's390x':
285
+ localFileExisted = existsSync(
286
+ join(__dirname, 'rivetkit-native.linux-s390x-gnu.node')
287
+ )
288
+ try {
289
+ if (localFileExisted) {
290
+ nativeBinding = require('./rivetkit-native.linux-s390x-gnu.node')
291
+ } else {
292
+ nativeBinding = require('@rivetkit/rivetkit-native-linux-s390x-gnu')
293
+ }
294
+ } catch (e) {
295
+ loadError = e
296
+ }
297
+ break
298
+ default:
299
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
300
+ }
301
+ break
302
+ default:
303
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
304
+ }
305
+
306
+ if (!nativeBinding) {
307
+ if (loadError) {
308
+ throw loadError
309
+ }
310
+ throw new Error(`Failed to load native binding`)
311
+ }
312
+
313
+ const { JsNativeDatabase, openDatabaseFromEnvoy, JsEnvoyHandle, startEnvoySyncJs, startEnvoyJs } = nativeBinding
314
+
315
+ module.exports.JsNativeDatabase = JsNativeDatabase
316
+ module.exports.openDatabaseFromEnvoy = openDatabaseFromEnvoy
317
+ module.exports.JsEnvoyHandle = JsEnvoyHandle
318
+ module.exports.startEnvoySyncJs = startEnvoySyncJs
319
+ module.exports.startEnvoyJs = startEnvoyJs
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@rivetkit/rivetkit-native",
3
+ "version": "2.2.1-pr.4600.bc7743e",
4
+ "description": "Native N-API addon for RivetKit providing envoy client and SQLite access",
5
+ "license": "Apache-2.0",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ },
13
+ "./wrapper": {
14
+ "types": "./wrapper.d.ts",
15
+ "default": "./wrapper.js"
16
+ }
17
+ },
18
+ "engines": {
19
+ "node": ">= 20.0.0"
20
+ },
21
+ "napi": {
22
+ "name": "rivetkit-native",
23
+ "triples": {
24
+ "defaults": false,
25
+ "additional": [
26
+ "x86_64-unknown-linux-gnu",
27
+ "aarch64-unknown-linux-gnu",
28
+ "x86_64-apple-darwin",
29
+ "aarch64-apple-darwin",
30
+ "x86_64-pc-windows-msvc"
31
+ ]
32
+ }
33
+ },
34
+ "files": [
35
+ "index.js",
36
+ "index.d.ts",
37
+ "wrapper.js",
38
+ "wrapper.d.ts",
39
+ "package.json"
40
+ ],
41
+ "dependencies": {
42
+ "@napi-rs/cli": "^2.18.4",
43
+ "@rivetkit/engine-envoy-protocol": "2.2.1-pr.4600.bc7743e"
44
+ },
45
+ "optionalDependencies": {
46
+ "@rivetkit/rivetkit-native-darwin-arm64": "2.2.1-pr.4600.bc7743e",
47
+ "@rivetkit/rivetkit-native-darwin-x64": "2.2.1-pr.4600.bc7743e",
48
+ "@rivetkit/rivetkit-native-linux-x64-gnu": "2.2.1-pr.4600.bc7743e",
49
+ "@rivetkit/rivetkit-native-win32-x64-msvc": "2.2.1-pr.4600.bc7743e"
50
+ },
51
+ "scripts": {
52
+ "build": "napi build --platform",
53
+ "build:release": "napi build --platform --release"
54
+ }
55
+ }
package/wrapper.d.ts ADDED
@@ -0,0 +1,144 @@
1
+ import type { JsNativeDatabase, JsKvEntry, JsKvListOptions } from "./index";
2
+
3
+ export type { JsNativeDatabase, JsKvEntry, JsKvListOptions };
4
+
5
+ // Re-export protocol types from the envoy protocol package
6
+ export * as protocol from "@rivetkit/engine-envoy-protocol";
7
+
8
+ export interface HibernatingWebSocketMetadata {
9
+ gatewayId: ArrayBuffer;
10
+ requestId: ArrayBuffer;
11
+ envoyMessageIndex: number;
12
+ rivetMessageIndex: number;
13
+ path: string;
14
+ headers: Record<string, string>;
15
+ }
16
+
17
+ export interface KvListOptions {
18
+ reverse?: boolean;
19
+ limit?: number;
20
+ }
21
+
22
+ /** Matches the TS EnvoyHandle interface from @rivetkit/engine-envoy-client */
23
+ export interface EnvoyHandle {
24
+ shutdown(immediate: boolean): void;
25
+ getProtocolMetadata(): any | undefined;
26
+ getEnvoyKey(): string;
27
+ started(): Promise<void>;
28
+ getActor(actorId: string, generation?: number): any | undefined;
29
+ sleepActor(actorId: string, generation?: number): void;
30
+ stopActor(actorId: string, generation?: number, error?: string): void;
31
+ destroyActor(actorId: string, generation?: number): void;
32
+ setAlarm(actorId: string, alarmTs: number | null, generation?: number): void;
33
+ kvGet(actorId: string, keys: Uint8Array[]): Promise<(Uint8Array | null)[]>;
34
+ kvListAll(actorId: string, options?: KvListOptions): Promise<[Uint8Array, Uint8Array][]>;
35
+ kvListRange(
36
+ actorId: string,
37
+ start: Uint8Array,
38
+ end: Uint8Array,
39
+ exclusive?: boolean,
40
+ options?: KvListOptions,
41
+ ): Promise<[Uint8Array, Uint8Array][]>;
42
+ kvListPrefix(
43
+ actorId: string,
44
+ prefix: Uint8Array,
45
+ options?: KvListOptions,
46
+ ): Promise<[Uint8Array, Uint8Array][]>;
47
+ kvPut(actorId: string, entries: [Uint8Array, Uint8Array][]): Promise<void>;
48
+ kvDelete(actorId: string, keys: Uint8Array[]): Promise<void>;
49
+ kvDeleteRange(actorId: string, start: Uint8Array, end: Uint8Array): Promise<void>;
50
+ kvDrop(actorId: string): Promise<void>;
51
+ restoreHibernatingRequests(
52
+ actorId: string,
53
+ metaEntries: HibernatingWebSocketMetadata[],
54
+ ): void;
55
+ sendHibernatableWebSocketMessageAck(
56
+ gatewayId: ArrayBuffer,
57
+ requestId: ArrayBuffer,
58
+ clientMessageIndex: number,
59
+ ): void;
60
+ startServerlessActor(payload: ArrayBuffer): Promise<void>;
61
+ }
62
+
63
+ /** Matches the TS EnvoyConfig interface from @rivetkit/engine-envoy-client */
64
+ export interface EnvoyConfig {
65
+ logger?: any;
66
+ version: number;
67
+ endpoint: string;
68
+ token?: string;
69
+ namespace: string;
70
+ poolName: string;
71
+ prepopulateActorNames: Record<string, { metadata: Record<string, any> }>;
72
+ metadata?: Record<string, any>;
73
+ notGlobal?: boolean;
74
+ debugLatencyMs?: number;
75
+ serverlessStartPayload?: ArrayBuffer;
76
+ fetch: (
77
+ envoyHandle: EnvoyHandle,
78
+ actorId: string,
79
+ gatewayId: ArrayBuffer,
80
+ requestId: ArrayBuffer,
81
+ request: Request,
82
+ ) => Promise<Response>;
83
+ websocket: (
84
+ envoyHandle: EnvoyHandle,
85
+ actorId: string,
86
+ ws: any,
87
+ gatewayId: ArrayBuffer,
88
+ requestId: ArrayBuffer,
89
+ request: Request,
90
+ path: string,
91
+ headers: Record<string, string>,
92
+ isHibernatable: boolean,
93
+ isRestoringHibernatable: boolean,
94
+ ) => Promise<void>;
95
+ hibernatableWebSocket: {
96
+ canHibernate: (
97
+ actorId: string,
98
+ gatewayId: ArrayBuffer,
99
+ requestId: ArrayBuffer,
100
+ request: Request,
101
+ ) => boolean;
102
+ };
103
+ onActorStart: (
104
+ envoyHandle: EnvoyHandle,
105
+ actorId: string,
106
+ generation: number,
107
+ config: import("@rivetkit/engine-envoy-protocol").ActorConfig,
108
+ preloadedKv: import("@rivetkit/engine-envoy-protocol").PreloadedKv | null,
109
+ ) => Promise<void>;
110
+ onActorStop: (
111
+ envoyHandle: EnvoyHandle,
112
+ actorId: string,
113
+ generation: number,
114
+ reason: import("@rivetkit/engine-envoy-protocol").StopActorReason,
115
+ ) => Promise<void>;
116
+ onShutdown: () => void;
117
+ }
118
+
119
+ /** Start the native envoy synchronously. Returns a handle immediately. */
120
+ export declare function startEnvoySync(config: EnvoyConfig): EnvoyHandle;
121
+
122
+ /** Start the native envoy and wait for it to be ready. */
123
+ export declare function startEnvoy(config: EnvoyConfig): Promise<EnvoyHandle>;
124
+
125
+ /** Open a native database backed by envoy KV for the specified actor. */
126
+ export declare function openDatabaseFromEnvoy(
127
+ handle: EnvoyHandle,
128
+ actorId: string,
129
+ ): Promise<JsNativeDatabase>;
130
+
131
+ export interface NativeRawDatabase {
132
+ execute: <TRow extends Record<string, unknown> = Record<string, unknown>>(
133
+ query: string,
134
+ ...args: unknown[]
135
+ ) => Promise<TRow[]>;
136
+ close: () => Promise<void>;
137
+ }
138
+
139
+ export declare function openRawDatabaseFromEnvoy(
140
+ handle: EnvoyHandle,
141
+ actorId: string,
142
+ ): Promise<NativeRawDatabase>;
143
+
144
+ export declare const utils: {};
package/wrapper.js ADDED
@@ -0,0 +1,422 @@
1
+ /**
2
+ * Thin JS wrapper that adapts native callback envelopes to the
3
+ * EnvoyConfig callback shape used by the TypeScript envoy client.
4
+ *
5
+ * The native addon sends JSON envelopes with a "kind" field.
6
+ * This wrapper routes them to the appropriate EnvoyConfig callbacks.
7
+ */
8
+
9
+ const native = require("./index");
10
+
11
+ // Re-export protocol for consumers that need protocol types at runtime
12
+ let _protocol;
13
+ try {
14
+ _protocol = require("@rivetkit/engine-envoy-protocol");
15
+ } catch {
16
+ _protocol = {};
17
+ }
18
+ module.exports.protocol = _protocol;
19
+ module.exports.utils = {};
20
+
21
+ /**
22
+ * Create a wrapped EnvoyHandle that matches the TS EnvoyHandle interface.
23
+ */
24
+ function wrapHandle(jsHandle) {
25
+ const handle = {
26
+ started: () => jsHandle.started(),
27
+ shutdown: (immediate) => jsHandle.shutdown(immediate ?? false),
28
+ getProtocolMetadata: () => undefined,
29
+ getEnvoyKey: () => jsHandle.envoyKey,
30
+ getActor: (_actorId, _generation) => undefined,
31
+ sleepActor: (actorId, generation) =>
32
+ jsHandle.sleepActor(actorId, generation ?? null),
33
+ stopActor: (actorId, generation, error) =>
34
+ jsHandle.stopActor(actorId, generation ?? null, error ?? null),
35
+ destroyActor: (actorId, generation) =>
36
+ jsHandle.destroyActor(actorId, generation ?? null),
37
+ setAlarm: (actorId, alarmTs, generation) =>
38
+ jsHandle.setAlarm(actorId, alarmTs ?? null, generation ?? null),
39
+ kvGet: async (actorId, keys) => {
40
+ const bufKeys = keys.map((k) => Buffer.from(k));
41
+ const result = await jsHandle.kvGet(actorId, bufKeys);
42
+ return result.map((v) => (v ? new Uint8Array(v) : null));
43
+ },
44
+ kvPut: async (actorId, entries) => {
45
+ const jsEntries = entries.map(([k, v]) => ({
46
+ key: Buffer.from(k),
47
+ value: Buffer.from(v),
48
+ }));
49
+ return jsHandle.kvPut(actorId, jsEntries);
50
+ },
51
+ kvDelete: async (actorId, keys) => {
52
+ const bufKeys = keys.map((k) => Buffer.from(k));
53
+ return jsHandle.kvDelete(actorId, bufKeys);
54
+ },
55
+ kvDeleteRange: async (actorId, start, end) => {
56
+ return jsHandle.kvDeleteRange(
57
+ actorId,
58
+ Buffer.from(start),
59
+ Buffer.from(end),
60
+ );
61
+ },
62
+ kvListAll: async (actorId, options) => {
63
+ const result = await jsHandle.kvListAll(actorId, options || null);
64
+ return result.map((e) => [new Uint8Array(e.key), new Uint8Array(e.value)]);
65
+ },
66
+ kvListRange: async (actorId, start, end, exclusive, options) => {
67
+ const result = await jsHandle.kvListRange(
68
+ actorId,
69
+ Buffer.from(start),
70
+ Buffer.from(end),
71
+ exclusive,
72
+ options || null,
73
+ );
74
+ return result.map((e) => [new Uint8Array(e.key), new Uint8Array(e.value)]);
75
+ },
76
+ kvListPrefix: async (actorId, prefix, options) => {
77
+ const result = await jsHandle.kvListPrefix(
78
+ actorId,
79
+ Buffer.from(prefix),
80
+ options || null,
81
+ );
82
+ return result.map((e) => [new Uint8Array(e.key), new Uint8Array(e.value)]);
83
+ },
84
+ kvDrop: (actorId) => jsHandle.kvDrop(actorId),
85
+ restoreHibernatingRequests: (actorId, metaEntries) => {
86
+ const requests = (metaEntries || []).map((e) => ({
87
+ gatewayId: Buffer.from(e.gatewayId),
88
+ requestId: Buffer.from(e.requestId),
89
+ }));
90
+ jsHandle.restoreHibernatingRequests(actorId, requests);
91
+ },
92
+ sendHibernatableWebSocketMessageAck: (
93
+ gatewayId,
94
+ requestId,
95
+ clientMessageIndex,
96
+ ) =>
97
+ jsHandle.sendHibernatableWebSocketMessageAck(
98
+ Buffer.from(gatewayId),
99
+ Buffer.from(requestId),
100
+ clientMessageIndex,
101
+ ),
102
+ startServerlessActor: (payload) => {
103
+ jsHandle.startServerless(Buffer.from(payload));
104
+ },
105
+ // Internal: expose raw handle for openDatabaseFromEnvoy
106
+ _raw: jsHandle,
107
+ };
108
+ return handle;
109
+ }
110
+
111
+ /**
112
+ * Start the native envoy synchronously with EnvoyConfig callbacks.
113
+ * Returns a wrapped handle matching the TS EnvoyHandle interface.
114
+ */
115
+ function startEnvoySync(config) {
116
+ const wrappedHandle = { current: null };
117
+
118
+ const jsHandle = native.startEnvoySyncJs(
119
+ {
120
+ endpoint: config.endpoint,
121
+ token: config.token || "",
122
+ namespace: config.namespace,
123
+ poolName: config.poolName,
124
+ version: config.version,
125
+ metadata: config.metadata || null,
126
+ notGlobal: config.notGlobal ?? false,
127
+ },
128
+ (event) => {
129
+ handleEvent(event, config, wrappedHandle);
130
+ },
131
+ );
132
+
133
+ const handle = wrapHandle(jsHandle);
134
+ wrappedHandle.current = handle;
135
+ return handle;
136
+ }
137
+
138
+ /**
139
+ * Start the native envoy and wait for it to be ready.
140
+ */
141
+ async function startEnvoy(config) {
142
+ const handle = startEnvoySync(config);
143
+ await handle.started();
144
+ return handle;
145
+ }
146
+
147
+ /**
148
+ * Open a native database backed by envoy KV.
149
+ */
150
+ async function openDatabaseFromEnvoy(handle, actorId) {
151
+ const rawHandle = handle._raw || handle;
152
+ return native.openDatabaseFromEnvoy(rawHandle, actorId);
153
+ }
154
+
155
+ /**
156
+ * Route callback envelopes from the native addon to EnvoyConfig callbacks.
157
+ */
158
+ function handleEvent(event, config, wrappedHandle) {
159
+ const handle = wrappedHandle.current;
160
+
161
+ switch (event.kind) {
162
+ case "actor_start": {
163
+ const input = event.input ? Buffer.from(event.input, "base64") : undefined;
164
+ const actorConfig = {
165
+ name: event.name,
166
+ key: event.key || undefined,
167
+ createTs: event.createTs,
168
+ input,
169
+ };
170
+ Promise.resolve(
171
+ config.onActorStart(
172
+ handle,
173
+ event.actorId,
174
+ event.generation,
175
+ actorConfig,
176
+ null, // preloadedKv
177
+ ),
178
+ ).then(
179
+ () => {
180
+ if (handle._raw) {
181
+ handle._raw.respondCallback(event.responseId, {});
182
+ }
183
+ },
184
+ (err) => {
185
+ console.error("onActorStart error:", err);
186
+ if (handle._raw) {
187
+ handle._raw.respondCallback(event.responseId, {
188
+ error: String(err),
189
+ });
190
+ }
191
+ },
192
+ );
193
+ break;
194
+ }
195
+ case "actor_stop": {
196
+ Promise.resolve(
197
+ config.onActorStop(
198
+ handle,
199
+ event.actorId,
200
+ event.generation,
201
+ event.reason || "stopped",
202
+ ),
203
+ ).then(
204
+ () => {
205
+ if (handle._raw) {
206
+ handle._raw.respondCallback(event.responseId, {});
207
+ }
208
+ },
209
+ (err) => {
210
+ console.error("onActorStop error:", err);
211
+ if (handle._raw) {
212
+ handle._raw.respondCallback(event.responseId, {
213
+ error: String(err),
214
+ });
215
+ }
216
+ },
217
+ );
218
+ break;
219
+ }
220
+ case "http_request": {
221
+ const body = event.body ? Buffer.from(event.body, "base64") : undefined;
222
+ const messageId = Buffer.from(event.messageId);
223
+ const gatewayId = messageId.subarray(0, 4);
224
+ const requestId = messageId.subarray(4, 8);
225
+
226
+ // Build a Request object matching the TS envoy-client interface
227
+ const headers = new Headers(event.headers || {});
228
+ const url = `http://actor${event.path}`;
229
+ const request = new Request(url, {
230
+ method: event.method,
231
+ headers,
232
+ body: body || undefined,
233
+ });
234
+
235
+ Promise.resolve(
236
+ config.fetch(handle, event.actorId, gatewayId, requestId, request),
237
+ ).then(
238
+ async (response) => {
239
+ if (handle._raw && response) {
240
+ const respHeaders = {};
241
+ if (response.headers) {
242
+ response.headers.forEach((value, key) => {
243
+ respHeaders[key] = value;
244
+ });
245
+ }
246
+ const respBody = response.body
247
+ ? Buffer.from(await response.arrayBuffer()).toString("base64")
248
+ : undefined;
249
+ handle._raw.respondCallback(event.responseId, {
250
+ status: response.status || 200,
251
+ headers: respHeaders,
252
+ body: respBody,
253
+ });
254
+ }
255
+ },
256
+ (err) => {
257
+ console.error("fetch callback error:", err);
258
+ if (handle._raw) {
259
+ handle._raw.respondCallback(event.responseId, {
260
+ status: 500,
261
+ headers: { "content-type": "text/plain" },
262
+ body: Buffer.from(String(err)).toString("base64"),
263
+ });
264
+ }
265
+ },
266
+ );
267
+ break;
268
+ }
269
+ case "websocket_open": {
270
+ if (config.websocket) {
271
+ const messageId = Buffer.from(event.messageId);
272
+ const gatewayId = messageId.subarray(0, 4);
273
+ const requestId = messageId.subarray(4, 8);
274
+ const wsIdHex = gatewayId.toString("hex") + requestId.toString("hex");
275
+
276
+ const headers = new Headers(event.headers || {});
277
+ headers.set("Upgrade", "websocket");
278
+ headers.set("Connection", "Upgrade");
279
+ const url = `http://actor${event.path}`;
280
+ const request = new Request(url, {
281
+ method: "GET",
282
+ headers,
283
+ });
284
+
285
+ // Create a WebSocket-like object backed by EventTarget.
286
+ // The EngineActorDriver calls addEventListener on this.
287
+ // Events are dispatched when native websocket_message/close events arrive.
288
+ const target = new EventTarget();
289
+ const OPEN = 1;
290
+ const CLOSED = 3;
291
+ const ws = Object.create(target, {
292
+ readyState: { value: OPEN, writable: true },
293
+ OPEN: { value: OPEN },
294
+ CLOSED: { value: CLOSED },
295
+ send: {
296
+ value: (data) => {
297
+ if (handle._raw) {
298
+ const isBinary =
299
+ data instanceof ArrayBuffer || ArrayBuffer.isView(data);
300
+ const bytes = isBinary
301
+ ? Buffer.from(data instanceof ArrayBuffer ? data : data.buffer, data instanceof ArrayBuffer ? 0 : data.byteOffset, data instanceof ArrayBuffer ? data.byteLength : data.byteLength)
302
+ : Buffer.from(String(data));
303
+ handle._raw.sendWsMessage(gatewayId, requestId, bytes, isBinary);
304
+ }
305
+ }
306
+ },
307
+ close: {
308
+ value: (code, reason) => {
309
+ ws.readyState = CLOSED;
310
+ if (handle._raw) {
311
+ handle._raw.closeWebsocket(
312
+ gatewayId,
313
+ requestId,
314
+ code != null ? code : undefined,
315
+ reason != null ? String(reason) : undefined,
316
+ );
317
+ }
318
+ }
319
+ },
320
+ addEventListener: { value: target.addEventListener.bind(target) },
321
+ removeEventListener: { value: target.removeEventListener.bind(target) },
322
+ dispatchEvent: { value: target.dispatchEvent.bind(target) },
323
+ });
324
+
325
+ // Store the ws object so websocket_message/close events can dispatch to it
326
+ if (!handle._wsMap) handle._wsMap = new Map();
327
+ handle._wsMap.set(wsIdHex, ws);
328
+
329
+ const canHibernate = config.hibernatableWebSocket
330
+ ? config.hibernatableWebSocket.canHibernate(
331
+ event.actorId,
332
+ gatewayId,
333
+ requestId,
334
+ request,
335
+ )
336
+ : false;
337
+
338
+ console.log("[wrapper] websocket_open actorId:", event.actorId?.slice(0, 12), "path:", event.path);
339
+ Promise.resolve(
340
+ config.websocket(
341
+ handle,
342
+ event.actorId,
343
+ ws,
344
+ gatewayId,
345
+ requestId,
346
+ request,
347
+ event.path,
348
+ event.headers || {},
349
+ canHibernate,
350
+ false,
351
+ ),
352
+ ).then(() => {
353
+ console.log("[wrapper] websocket callback resolved, dispatching open event");
354
+ ws.dispatchEvent(new Event("open"));
355
+ console.log("[wrapper] open event dispatched");
356
+ }).catch((err) => {
357
+ console.error("[wrapper] websocket callback error:", err);
358
+ });
359
+ }
360
+ break;
361
+ }
362
+ case "websocket_message": {
363
+ if (handle._wsMap && event.messageId) {
364
+ const messageId = Buffer.from(event.messageId);
365
+ const gatewayId = messageId.subarray(0, 4);
366
+ const requestId = messageId.subarray(4, 8);
367
+ const wsIdHex = gatewayId.toString("hex") + requestId.toString("hex");
368
+
369
+ const ws = handle._wsMap.get(wsIdHex);
370
+
371
+ if (ws) {
372
+ const data = event.data
373
+ ? (event.binary
374
+ ? Buffer.from(event.data, "base64")
375
+ : Buffer.from(event.data, "base64").toString())
376
+ : "";
377
+ const msgEvent = new MessageEvent("message", { data });
378
+ msgEvent.rivetGatewayId = messageId.subarray(0, 4);
379
+ msgEvent.rivetRequestId = messageId.subarray(4, 8);
380
+ msgEvent.rivetMessageIndex = messageId.readUint16LE(8);
381
+ ws.dispatchEvent(msgEvent);
382
+ }
383
+ }
384
+ break;
385
+ }
386
+ case "websocket_close": {
387
+ if (handle._wsMap && event.messageId) {
388
+ const messageId = Buffer.from(event.messageId);
389
+ const gatewayId = messageId.subarray(0, 4);
390
+ const requestId = messageId.subarray(4, 8);
391
+ const wsIdHex = gatewayId.toString("hex") + requestId.toString("hex");
392
+
393
+ const ws = handle._wsMap.get(wsIdHex);
394
+ if (ws) {
395
+ ws.readyState = 3;
396
+ ws.dispatchEvent(new CloseEvent("close", {
397
+ code: event.code || 1000,
398
+ reason: event.reason || "",
399
+ }));
400
+ handle._wsMap.delete(wsIdHex);
401
+ }
402
+ }
403
+ break;
404
+ }
405
+ case "hibernation_restore":
406
+ case "alarm":
407
+ case "wake":
408
+ break;
409
+ case "shutdown": {
410
+ if (config.onShutdown) {
411
+ config.onShutdown();
412
+ }
413
+ break;
414
+ }
415
+ default:
416
+ console.warn("unknown native event kind:", event.kind);
417
+ }
418
+ }
419
+
420
+ module.exports.startEnvoy = startEnvoy;
421
+ module.exports.startEnvoySync = startEnvoySync;
422
+ module.exports.openDatabaseFromEnvoy = openDatabaseFromEnvoy;