relaxnative 0.1.0 → 0.1.2
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/README.md +130 -0
- package/dist/chunk-5QC7YU3I.js +1042 -0
- package/dist/chunk-CRQZJC5B.js +397 -0
- package/dist/chunk-GVGWP3E2.js +786 -0
- package/dist/chunk-O4ZLYK3D.js +757 -0
- package/dist/chunk-RP7PBEUQ.js +301 -0
- package/dist/chunk-ZTBXEEMF.js +1042 -0
- package/dist/cli.js +3 -3
- package/dist/index.js +3 -3
- package/dist/memory/memory.selftest.js +2 -2
- package/dist/worker/processEntry.js +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -55,6 +55,65 @@ Requirements:
|
|
|
55
55
|
|
|
56
56
|
## Quickstart
|
|
57
57
|
|
|
58
|
+
### 0) Full end-to-end example (new folder → run)
|
|
59
|
+
|
|
60
|
+
This is the fastest way to try Relaxnative in a clean folder.
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
mkdir -p my-relaxnative-app/native
|
|
64
|
+
cd my-relaxnative-app
|
|
65
|
+
npm init -y
|
|
66
|
+
npm i relaxnative
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Optional `package.json` (ESM + a run script):
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"name": "my-relaxnative-app",
|
|
74
|
+
"private": true,
|
|
75
|
+
"type": "module",
|
|
76
|
+
"scripts": {
|
|
77
|
+
"start": "node index.js"
|
|
78
|
+
},
|
|
79
|
+
"dependencies": {
|
|
80
|
+
"relaxnative": "^0.1.0"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Create `native/add.c`:
|
|
86
|
+
|
|
87
|
+
```c
|
|
88
|
+
// @sync
|
|
89
|
+
int add(int a, int b) {
|
|
90
|
+
return a + b;
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Create `index.js`:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { loadNative } from 'relaxnative';
|
|
98
|
+
|
|
99
|
+
const mod = await loadNative('native/add.c', { isolation: 'worker' });
|
|
100
|
+
console.log('add(1,2)=', mod.add(1, 2));
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Run it:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm start
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
If something goes wrong, re-run with tracing:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
RELAXNATIVE_TRACE=1 npm start
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
58
117
|
### 1) Create a native file
|
|
59
118
|
|
|
60
119
|
`native/add.c`
|
|
@@ -77,6 +136,30 @@ console.log(mod.add(1, 2));
|
|
|
77
136
|
|
|
78
137
|
---
|
|
79
138
|
|
|
139
|
+
## Tracing (RELAXNATIVE_TRACE)
|
|
140
|
+
|
|
141
|
+
When native code crashes or a worker/process boundary hides the real error, enable tracing.
|
|
142
|
+
|
|
143
|
+
```bash
|
|
144
|
+
RELAXNATIVE_TRACE=1 node index.js
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
More control:
|
|
148
|
+
|
|
149
|
+
- `RELAXNATIVE_TRACE=1` enables trace events.
|
|
150
|
+
- `RELAXNATIVE_TRACE_LEVEL=info|debug` controls verbosity (default: `info`).
|
|
151
|
+
|
|
152
|
+
What you’ll see (examples):
|
|
153
|
+
|
|
154
|
+
- Load lifecycle: `loadNative.begin`, `loadNative.build.begin`, `loadNative.build.done`, `loadNative.done`
|
|
155
|
+
- Parsed signatures (debug): `loadNative.bindings`
|
|
156
|
+
- Dispatch decisions: `dispatch`, `isolation.worker.dispatch`, `isolation.process.call`
|
|
157
|
+
- Process helper lifecycle: `isolation.process.helper.start|exit|error`
|
|
158
|
+
|
|
159
|
+
Tip: `RELAXNATIVE_TRACE_LEVEL=debug` will print parsed function signatures.
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
80
163
|
## API
|
|
81
164
|
|
|
82
165
|
### `loadNative(sourcePath, options?)`
|
|
@@ -119,16 +202,56 @@ console.log(buf.address); // numeric pointer
|
|
|
119
202
|
- fastest
|
|
120
203
|
- unsafe: native crashes take down your Node process
|
|
121
204
|
|
|
205
|
+
Example:
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
import { loadNative } from 'relaxnative';
|
|
209
|
+
|
|
210
|
+
const mod = await loadNative('native/add.c', { isolation: 'in-process' });
|
|
211
|
+
console.log(mod.add(1, 2));
|
|
212
|
+
```
|
|
213
|
+
|
|
122
214
|
### `worker`
|
|
123
215
|
- worker-thread dispatch for async calls
|
|
124
216
|
- sync calls may execute directly for low overhead
|
|
125
217
|
|
|
218
|
+
Example (async heavy work):
|
|
219
|
+
|
|
220
|
+
```c
|
|
221
|
+
// native/heavy.c
|
|
222
|
+
// @async
|
|
223
|
+
int heavy(int n) {
|
|
224
|
+
long x = 0;
|
|
225
|
+
for (int i = 0; i < n * 10000000; i++) x += i;
|
|
226
|
+
return (int)x;
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
import { loadNative } from 'relaxnative';
|
|
232
|
+
|
|
233
|
+
const mod = await loadNative('native/heavy.c', { isolation: 'worker' });
|
|
234
|
+
const result = await mod.heavy(5);
|
|
235
|
+
console.log(result);
|
|
236
|
+
```
|
|
237
|
+
|
|
126
238
|
### `process`
|
|
127
239
|
- forked helper process
|
|
128
240
|
- crash containment
|
|
129
241
|
- best-effort Node runtime guards (module import denial for fs/network/spawn)
|
|
130
242
|
- call timeout enforcement (kills helper)
|
|
131
243
|
|
|
244
|
+
Example:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
import { loadNative } from 'relaxnative';
|
|
248
|
+
|
|
249
|
+
// Process isolation always returns async wrappers.
|
|
250
|
+
const mod = await loadNative('native/heavy.c', { isolation: 'process' });
|
|
251
|
+
const result = await mod.heavy(5);
|
|
252
|
+
console.log(result);
|
|
253
|
+
```
|
|
254
|
+
|
|
132
255
|
---
|
|
133
256
|
|
|
134
257
|
## Annotations
|
|
@@ -140,6 +263,13 @@ Supported:
|
|
|
140
263
|
- `@async`
|
|
141
264
|
- `@cost low|medium|high`
|
|
142
265
|
|
|
266
|
+
### Annotation + isolation quick rules
|
|
267
|
+
|
|
268
|
+
- `@sync` + `in-process`: fastest, but a crash kills your app.
|
|
269
|
+
- `@sync` + `worker`: may execute directly (fast) unless the binding is marked async/high-cost.
|
|
270
|
+
- `@async` + `worker`: always goes through the worker thread and returns a Promise.
|
|
271
|
+
- `process` isolation: **always async**, regardless of annotations (IPC boundary).
|
|
272
|
+
|
|
143
273
|
### What annotations mean
|
|
144
274
|
|
|
145
275
|
- `@sync`
|