typenative 0.3.1 → 0.4.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/.github/workflows/ci.yml +2 -0
- package/CHANGELOG.md +258 -0
- package/CONTRIBUTION.md +1 -1
- package/README.md +17 -1
- package/bin/cli.js +26 -11
- package/bin/go/bridge.js +74 -14
- package/bin/go/convert.js +59 -1
- package/bin/go/metadata.js +13 -4
- package/bin/go/overrides.js +4 -6
- package/bin/go/stdlib.generated.js +2 -0
- package/bin/index.js +338 -47
- package/bin/main.js +36 -14
- package/bin/node/assert.js +2 -2
- package/bin/node/buffer.js +9 -0
- package/bin/node/child_process.js +51 -3
- package/bin/node/cluster.js +41 -3
- package/bin/node/core.js +1 -1
- package/bin/node/crypto.js +205 -4
- package/bin/node/dgram.js +230 -6
- package/bin/node/dns.js +471 -8
- package/bin/node/errno_tables.js +445 -0
- package/bin/node/events.js +1 -1
- package/bin/node/fs.js +22 -15
- package/bin/node/fs_watch.js +407 -0
- package/bin/node/http.js +128 -30
- package/bin/node/http2.js +37 -7
- package/bin/node/module_v8.js +142 -0
- package/bin/node/net.js +9 -6
- package/bin/node/process.js +242 -0
- package/bin/node/readline.js +272 -1
- package/bin/node/registry.js +3 -1
- package/bin/node/sqlite.js +60 -12
- package/bin/node/stream.js +145 -10
- package/bin/node/test.js +311 -19
- package/bin/node/timers.js +1 -1
- package/bin/node/typed_arrays.js +608 -0
- package/bin/node/url.js +23 -5
- package/bin/node/util.js +21 -1
- package/bin/node/util_extra.js +6 -30
- package/bin/node/worker_threads.js +79 -19
- package/bin/node/zlib.js +131 -16
- package/bin/transpiler.js +4729 -616
- package/bin/typecheck-node.js +1 -0
- package/bin/version.js +1 -1
- package/conformance.md +1030 -877
- package/examples/README.md +54 -0
- package/examples/fibonacci/README.md +89 -0
- package/examples/fibonacci/bench.mjs +87 -0
- package/examples/fibonacci/main.ts +21 -0
- package/examples/hello-world/README.md +62 -0
- package/examples/hello-world/bench.mjs +66 -0
- package/examples/hello-world/main.ts +7 -0
- package/examples/json-server/README.md +127 -0
- package/examples/json-server/bench.mjs +263 -0
- package/examples/json-server/main.ts +67 -0
- package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
- package/govendor/github.com/andybalholm/brotli/README.md +19 -0
- package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
- package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
- package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
- package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
- package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
- package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
- package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
- package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
- package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
- package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
- package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
- package/govendor/github.com/andybalholm/brotli/command.go +254 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
- package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
- package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
- package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
- package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
- package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
- package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
- package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
- package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
- package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
- package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
- package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
- package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
- package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
- package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
- package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
- package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
- package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
- package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
- package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
- package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
- package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
- package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
- package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
- package/govendor/github.com/andybalholm/brotli/http.go +201 -0
- package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
- package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
- package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
- package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
- package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
- package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
- package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
- package/govendor/github.com/andybalholm/brotli/params.go +37 -0
- package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
- package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
- package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
- package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
- package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
- package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
- package/govendor/github.com/andybalholm/brotli/state.go +313 -0
- package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
- package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
- package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
- package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
- package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
- package/govendor/github.com/andybalholm/brotli/util.go +7 -0
- package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
- package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
- package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
- package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
- package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
- package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
- package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
- package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
- package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
- package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
- package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
- package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
- package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
- package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
- package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/types/typenative-go.d.ts +23 -0
- package/types/typenative-npm.d.ts +241 -5
- package/types/typenative.d.ts +302 -12
|
@@ -10,6 +10,7 @@ type tnSpawnOptions struct {
|
|
|
10
10
|
input []byte
|
|
11
11
|
stdio string
|
|
12
12
|
encoding string
|
|
13
|
+
detached bool
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
// Environment entries from an env option (records, objects, process.env)
|
|
@@ -83,6 +84,9 @@ func tnSpawnOpts(options []interface{}) tnSpawnOptions {
|
|
|
83
84
|
if e, ok := TnGet(opts, "encoding").(string); ok {
|
|
84
85
|
o.encoding = e
|
|
85
86
|
}
|
|
87
|
+
if d, ok := TnGet(opts, "detached").(bool); ok {
|
|
88
|
+
o.detached = d
|
|
89
|
+
}
|
|
86
90
|
return o
|
|
87
91
|
}
|
|
88
92
|
|
|
@@ -102,6 +106,9 @@ func tnCommand(file string, args []string, o tnSpawnOptions) *exec.Cmd {
|
|
|
102
106
|
if o.env != nil {
|
|
103
107
|
cmd.Env = o.env
|
|
104
108
|
}
|
|
109
|
+
if o.detached {
|
|
110
|
+
tnDetach(cmd)
|
|
111
|
+
}
|
|
105
112
|
return cmd
|
|
106
113
|
}
|
|
107
114
|
|
|
@@ -249,6 +256,8 @@ type TnChildProcess struct {
|
|
|
249
256
|
spawnfile string
|
|
250
257
|
// IPC state of forked children (node:cluster / fork)
|
|
251
258
|
ipc interface{}
|
|
259
|
+
// whether the child keeps the event loop alive (unref() drops it)
|
|
260
|
+
refHeld bool
|
|
252
261
|
}
|
|
253
262
|
|
|
254
263
|
func (cp *TnChildProcess) Get_stdin() *TnStream { return cp.stdin }
|
|
@@ -260,6 +269,21 @@ func (cp *TnChildProcess) Get_signalCode() interface{} { return cp.signalCode }
|
|
|
260
269
|
func (cp *TnChildProcess) Get_killed() bool { return cp.killed }
|
|
261
270
|
func (cp *TnChildProcess) Get_spawnfile() string { return cp.spawnfile }
|
|
262
271
|
|
|
272
|
+
// unref(): the parent can exit while the child runs; ref() undoes it
|
|
273
|
+
func (cp *TnChildProcess) unref() {
|
|
274
|
+
if cp.refHeld {
|
|
275
|
+
cp.refHeld = false
|
|
276
|
+
TnLoopUnref()
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
func (cp *TnChildProcess) ref() {
|
|
281
|
+
if !cp.refHeld && cp.exitCode == nil && cp.cmd != nil && cp.cmd.Process != nil {
|
|
282
|
+
cp.refHeld = true
|
|
283
|
+
TnLoopRef()
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
263
287
|
func (cp *TnChildProcess) kill(signal ...interface{}) bool {
|
|
264
288
|
if cp.cmd == nil || cp.cmd.Process == nil || cp.exitCode != nil {
|
|
265
289
|
return false
|
|
@@ -330,14 +354,18 @@ func TnSpawn(file string, args []string, options ...interface{}) *TnChildProcess
|
|
|
330
354
|
return cp
|
|
331
355
|
}
|
|
332
356
|
cp.pid = float64(cmd.Process.Pid)
|
|
357
|
+
cp.refHeld = true
|
|
333
358
|
TnLoopRef()
|
|
334
359
|
TnLoopPost(func() { cp.emit("spawn") })
|
|
335
360
|
go func() {
|
|
336
361
|
streams.Wait()
|
|
337
362
|
err := cmd.Wait()
|
|
338
363
|
status, _ := tnExitStatus(err)
|
|
364
|
+
if ipc, ok := cp.ipc.(interface{ waitDrained() }); ok {
|
|
365
|
+
ipc.waitDrained()
|
|
366
|
+
}
|
|
339
367
|
TnLoopPost(func() {
|
|
340
|
-
defer
|
|
368
|
+
defer cp.unref()
|
|
341
369
|
var code interface{} = status
|
|
342
370
|
var signal interface{}
|
|
343
371
|
if cp.killed {
|
|
@@ -484,10 +512,30 @@ function stringArray(arg) {
|
|
|
484
512
|
function isFunctionArg(node, type) {
|
|
485
513
|
return !!node && (node.kind === 'ArrowFunction' || node.kind === 'FunctionExpression' || type.startsWith('func('));
|
|
486
514
|
}
|
|
515
|
+
// detached: a new session on Unix (setsid, as libuv does), a detached process
|
|
516
|
+
// in a new process group on Windows
|
|
517
|
+
const unixDetachHelper = `func tnDetach(cmd *exec.Cmd) {
|
|
518
|
+
if cmd.SysProcAttr == nil {
|
|
519
|
+
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
520
|
+
}
|
|
521
|
+
cmd.SysProcAttr.Setsid = true
|
|
522
|
+
}`;
|
|
523
|
+
const windowsDetachHelper = `func tnDetach(cmd *exec.Cmd) {
|
|
524
|
+
if cmd.SysProcAttr == nil {
|
|
525
|
+
cmd.SysProcAttr = &syscall.SysProcAttr{}
|
|
526
|
+
}
|
|
527
|
+
// DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP
|
|
528
|
+
cmd.SysProcAttr.CreationFlags |= 0x8 | 0x200
|
|
529
|
+
}`;
|
|
487
530
|
export function registerChildProcess(r) {
|
|
488
531
|
defineModule(r, 'child_process');
|
|
489
|
-
|
|
490
|
-
|
|
532
|
+
for (const goos of ['linux', 'darwin', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'solaris', 'illumos', 'aix']) {
|
|
533
|
+
defineHelper(r, `nodeChildOS@${goos}`, ['os/exec', 'syscall'], [], unixDetachHelper);
|
|
534
|
+
}
|
|
535
|
+
defineHelper(r, 'nodeChildOS@windows', ['os/exec', 'syscall'], [], windowsDetachHelper);
|
|
536
|
+
defineHelper(r, 'nodeChildOS@other', ['os/exec'], [], 'func tnDetach(cmd *exec.Cmd) {}');
|
|
537
|
+
defineHelper(r, 'nodeChild', ['bytes', 'errors', 'fmt', 'io', 'io/fs', 'os', 'os/exec', 'reflect', 'runtime', 'strings', 'sync', 'unsafe'], ['nodeStream', 'fsErrors', 'buffer', 'dynamic', 'nodeInvoke', 'eventLoop', 'promise', 'nodeChildOS@os'], childHelper);
|
|
538
|
+
defineClass(r, 'child_process.ChildProcess', '*TnChildProcess', 'class', 'on=*TnEventEmitter; once=*TnEventEmitter; off=*TnEventEmitter; emit=bool; kill=bool; ref=; unref=', '', 'stdin=*TnStream; stdout=*TnStream; stderr=*TnStream; pid=float64; exitCode=interface{}; signalCode=interface{}; killed=bool; spawnfile=string', '');
|
|
491
539
|
defineClass(r, 'child_process.SpawnSyncReturns', '*TnSpawnSyncResult', 'class', '', 'pid=float64; stdout=string; stderr=string; status=float64; signal=*string; output=[]interface{}; error=*TnError', '', '');
|
|
492
540
|
defineClass(r, 'child_process.SpawnSyncBytes', '*TnSpawnSyncBytes', 'class', '', 'pid=float64; stdout=[]byte; stderr=[]byte; status=float64; signal=*string; output=[]interface{}; error=*TnError', '', '');
|
|
493
541
|
defineClass(r, 'child_process.ExecOutput', '*TnExecOutput', 'class', '', 'stdout=string; stderr=string', '', '');
|
package/bin/node/cluster.js
CHANGED
|
@@ -58,6 +58,7 @@ type tnIpcLink struct {
|
|
|
58
58
|
onControl func(ctl map[string]interface{})
|
|
59
59
|
directControl func(ctl map[string]interface{}) bool
|
|
60
60
|
onClose func()
|
|
61
|
+
done chan struct{}
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
func (l *tnIpcLink) write(kind byte, data []byte) bool {
|
|
@@ -95,6 +96,9 @@ func (l *tnIpcLink) start() {
|
|
|
95
96
|
l.onClose()
|
|
96
97
|
}
|
|
97
98
|
})
|
|
99
|
+
if l.done != nil {
|
|
100
|
+
close(l.done)
|
|
101
|
+
}
|
|
98
102
|
return
|
|
99
103
|
}
|
|
100
104
|
switch kind {
|
|
@@ -138,6 +142,16 @@ type tnChildIpc struct {
|
|
|
138
142
|
connected bool
|
|
139
143
|
onConnect func()
|
|
140
144
|
onControl func(ctl map[string]interface{})
|
|
145
|
+
drained chan struct{}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Holds the child's 'exit' until everything it sent (and the channel's close)
|
|
149
|
+
// is delivered, so 'message' and 'disconnect' come first as in Node
|
|
150
|
+
func (st *tnChildIpc) waitDrained() {
|
|
151
|
+
select {
|
|
152
|
+
case <-st.drained:
|
|
153
|
+
case <-time.After(time.Second):
|
|
154
|
+
}
|
|
141
155
|
}
|
|
142
156
|
|
|
143
157
|
func tnCPIpc(cp *TnChildProcess) *tnChildIpc {
|
|
@@ -210,6 +224,7 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
|
|
|
210
224
|
}
|
|
211
225
|
env = append(env, extraEnv...)
|
|
212
226
|
env = append(env, "TN_IPC_ADDR="+ln.Addr().String(), "TN_IPC_TOKEN="+token)
|
|
227
|
+
env = tnSpawnEntryEnv(env)
|
|
213
228
|
envMap := map[string]interface{}{}
|
|
214
229
|
for _, kv := range env {
|
|
215
230
|
if k, v, ok := strings.Cut(kv, "="); ok {
|
|
@@ -225,7 +240,7 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
|
|
|
225
240
|
spawnOpts["cwd"] = cwd
|
|
226
241
|
}
|
|
227
242
|
cp := TnSpawn(exe, argv, spawnOpts)
|
|
228
|
-
st := &tnChildIpc{connected: true}
|
|
243
|
+
st := &tnChildIpc{connected: true, drained: make(chan struct{})}
|
|
229
244
|
cp.ipc = st
|
|
230
245
|
go func() {
|
|
231
246
|
ln.(*net.TCPListener).SetDeadline(time.Now().Add(30 * time.Second))
|
|
@@ -233,6 +248,7 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
|
|
|
233
248
|
conn, err := ln.Accept()
|
|
234
249
|
if err != nil {
|
|
235
250
|
ln.Close()
|
|
251
|
+
close(st.drained)
|
|
236
252
|
return
|
|
237
253
|
}
|
|
238
254
|
kind, payload, err := tnReadFrame(conn)
|
|
@@ -241,11 +257,12 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
|
|
|
241
257
|
continue
|
|
242
258
|
}
|
|
243
259
|
ln.Close()
|
|
244
|
-
link := &tnIpcLink{conn: conn}
|
|
260
|
+
link := &tnIpcLink{conn: conn, done: st.drained}
|
|
245
261
|
TnLoopPost(func() {
|
|
246
262
|
st.link = link
|
|
247
263
|
if !st.connected {
|
|
248
264
|
link.close()
|
|
265
|
+
close(st.drained)
|
|
249
266
|
return
|
|
250
267
|
}
|
|
251
268
|
for _, data := range st.pending {
|
|
@@ -277,6 +294,13 @@ func tnForkWith(argv []string, opts interface{}, extraEnv []string) *TnChildProc
|
|
|
277
294
|
|
|
278
295
|
// fork(modulePath, args?, options?): this program, run again (a native binary
|
|
279
296
|
// holds one program; branch on process.send / cluster.isPrimary)
|
|
297
|
+
// fork('./child.ts'): runs the entry point compiled from that file
|
|
298
|
+
func TnForkAt(entry string, modulePath interface{}, rest ...interface{}) *TnChildProcess {
|
|
299
|
+
tnNextEntry = entry
|
|
300
|
+
defer func() { tnNextEntry = "" }()
|
|
301
|
+
return TnFork(modulePath, rest...)
|
|
302
|
+
}
|
|
303
|
+
|
|
280
304
|
func TnFork(modulePath interface{}, rest ...interface{}) *TnChildProcess {
|
|
281
305
|
argv := []string{}
|
|
282
306
|
var opts interface{}
|
|
@@ -898,11 +922,15 @@ func tnClusterListen(srv *TnNetServer, network string, address string) (net.List
|
|
|
898
922
|
}`;
|
|
899
923
|
const CLUSTER_EMITTER = 'on=*TnEventEmitter; once=*TnEventEmitter; addListener=*TnEventEmitter; off=*TnEventEmitter; removeListener=*TnEventEmitter; removeAllListeners=*TnEventEmitter; emit=bool; listenerCount=float64';
|
|
900
924
|
export function registerCluster(r) {
|
|
901
|
-
defineHelper(r, 'nodeIpc', ['crypto/rand', 'encoding/binary', 'encoding/hex', 'encoding/json', 'io', 'net', 'os', 'reflect', 'strings', 'sync', 'time'], ['nodeClone', 'nodeChild', 'nodeEvents', 'eventLoop', 'nodeInvoke', 'dynamic'], ipcHelper);
|
|
925
|
+
defineHelper(r, 'nodeIpc', ['crypto/rand', 'encoding/binary', 'encoding/hex', 'encoding/json', 'io', 'net', 'os', 'reflect', 'strings', 'sync', 'time'], ['nodeClone', 'nodeChild', 'nodeEvents', 'eventLoop', 'nodeInvoke', 'dynamic', 'entryPoints'], ipcHelper);
|
|
902
926
|
const useIpc = () => r.helper('nodeIpc');
|
|
903
927
|
// child_process.fork and the child's process.send / connected / disconnect
|
|
904
928
|
r.functions['child_process.fork'] = (c) => {
|
|
905
929
|
useIpc();
|
|
930
|
+
// fork('./child.ts'): that file is compiled in as a second entry point
|
|
931
|
+
const entry = r.entryPoint(c.nodes[0]);
|
|
932
|
+
if (entry !== '')
|
|
933
|
+
return `TnForkAt(${JSON.stringify(entry)}, ${c.args.join(', ')})`;
|
|
906
934
|
return `TnFork(${c.args.join(', ')})`;
|
|
907
935
|
};
|
|
908
936
|
r.results['child_process.fork'] = '*TnChildProcess';
|
|
@@ -923,6 +951,16 @@ export function registerCluster(r) {
|
|
|
923
951
|
return `TnProcessSend(${c.args.join(', ')})`;
|
|
924
952
|
};
|
|
925
953
|
r.results['process.send'] = 'bool';
|
|
954
|
+
// process.send?.(…) / process.disconnect?.(): they exist only in a process
|
|
955
|
+
// with an IPC channel (a forked child)
|
|
956
|
+
r.functions['process.send?.'] = (c) => {
|
|
957
|
+
useIpc();
|
|
958
|
+
return `func() { if tnIpcParent != nil { TnProcessSend(${c.args.join(', ')}) } }()`;
|
|
959
|
+
};
|
|
960
|
+
r.functions['process.disconnect?.'] = (_c) => {
|
|
961
|
+
useIpc();
|
|
962
|
+
return 'func() { if tnIpcParent != nil { TnProcessDisconnect() } }()';
|
|
963
|
+
};
|
|
926
964
|
r.functions['process.disconnect'] = (_c) => {
|
|
927
965
|
useIpc();
|
|
928
966
|
return 'TnProcessDisconnect()';
|
package/bin/node/core.js
CHANGED
|
@@ -85,7 +85,7 @@ func tnInvokeArg(a interface{}, t reflect.Type) reflect.Value {
|
|
|
85
85
|
}`;
|
|
86
86
|
export function registerCore(r) {
|
|
87
87
|
defineHelper(r, 'nodeInvoke', ['reflect'], ['dynamic'], invokeHelper);
|
|
88
|
-
defineClass(r, 'NodeJS.ErrnoException', '*TnError', 'class', '', 'message=string; code=string', 'name=string; stack=string; cause=interface{}; syscall=string; path=string; errno=float64; hostname=string; address=string; port=float64; actual=interface{}; expected=interface{}; operator=string; generatedMessage=bool', '');
|
|
88
|
+
defineClass(r, 'NodeJS.ErrnoException', '*TnError', 'class', '', 'message=string; code=string', 'name=string; stack=string; cause=interface{}; syscall=string; path=string; errno=float64; hostname=string; address=string; port=float64; actual=interface{}; expected=interface{}; operator=string; generatedMessage=bool; errors=[]interface{}', '');
|
|
89
89
|
r.globals['NodeJS'] = 'NodeJS';
|
|
90
90
|
// Callback types of Node's typings (TransformCallback …)
|
|
91
91
|
for (const name of ['stream.TransformCallback', 'fs.NoParamCallback']) {
|
package/bin/node/crypto.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// random bytes / UUIDs / ints, timingSafeEqual, PBKDF2 / HKDF, AES ciphers
|
|
3
3
|
// (cbc / ctr / gcm). Backed by Go's crypto packages.
|
|
4
4
|
import { defineClass, defineHelper, defineModule, setResults } from './registry.js';
|
|
5
|
+
import { streamListenerTypes } from './stream.js';
|
|
5
6
|
const cryptoHelper = `// node:crypto
|
|
6
7
|
func tnHashFactory(alg string) func() hash.Hash {
|
|
7
8
|
switch strings.ToLower(alg) {
|
|
@@ -42,6 +43,9 @@ type TnHash struct {
|
|
|
42
43
|
h hash.Hash
|
|
43
44
|
alg string
|
|
44
45
|
finalized bool
|
|
46
|
+
hmac bool
|
|
47
|
+
// set when the hash is used as a stream (hashStream)
|
|
48
|
+
stream interface{}
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
func TnCreateHash(alg string, options ...interface{}) *TnHash {
|
|
@@ -49,7 +53,7 @@ func TnCreateHash(alg string, options ...interface{}) *TnHash {
|
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
func TnCreateHmac(alg string, key interface{}, options ...interface{}) *TnHash {
|
|
52
|
-
return &TnHash{h: hmac.New(tnHashFactory(alg), tnCryptoBytes(key, "")), alg: alg}
|
|
56
|
+
return &TnHash{h: hmac.New(tnHashFactory(alg), tnCryptoBytes(key, "")), alg: alg, hmac: true}
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
// Bytes of data passed to crypto APIs: strings in an encoding (utf8 by default) or Buffers
|
|
@@ -76,6 +80,10 @@ func (h *TnHash) update(data interface{}, enc ...string) *TnHash {
|
|
|
76
80
|
}
|
|
77
81
|
|
|
78
82
|
func (h *TnHash) digestBytes() []byte {
|
|
83
|
+
// a second hmac.digest() is empty; a second hash.digest() throws
|
|
84
|
+
if h.finalized && h.hmac {
|
|
85
|
+
return []byte{}
|
|
86
|
+
}
|
|
79
87
|
if h.finalized {
|
|
80
88
|
panic(TnNodeError("Error", "ERR_CRYPTO_HASH_FINALIZED", "Digest already called"))
|
|
81
89
|
}
|
|
@@ -189,13 +197,158 @@ type TnCipher struct {
|
|
|
189
197
|
authTag []byte
|
|
190
198
|
autoPadding bool
|
|
191
199
|
finalized bool
|
|
200
|
+
wrapped bool
|
|
192
201
|
}
|
|
193
202
|
|
|
194
203
|
func TnGetCiphers() []string {
|
|
195
|
-
return []string{"aes-128-cbc", "aes-128-ctr", "aes-128-gcm", "aes-192-cbc", "aes-192-ctr", "aes-192-gcm", "aes-256-cbc", "aes-256-ctr", "aes-256-gcm"
|
|
204
|
+
return []string{"aes-128-cbc", "aes-128-ctr", "aes-128-gcm", "aes-192-cbc", "aes-192-ctr", "aes-192-gcm", "aes-256-cbc", "aes-256-ctr", "aes-256-gcm",
|
|
205
|
+
"aes128-wrap", "aes128-wrap-pad", "aes192-wrap", "aes192-wrap-pad", "aes256-wrap", "aes256-wrap-pad",
|
|
206
|
+
"id-aes128-wrap", "id-aes128-wrap-pad", "id-aes192-wrap", "id-aes192-wrap-pad", "id-aes256-wrap", "id-aes256-wrap-pad"}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// AES key wrap (RFC 3394) and key wrap with padding (RFC 5649): the whole
|
|
210
|
+
// input goes through one update(), as with OpenSSL
|
|
211
|
+
func tnAESWrapRounds(block cipher.Block, a []byte, r [][]byte) []byte {
|
|
212
|
+
n := len(r)
|
|
213
|
+
buf := make([]byte, 16)
|
|
214
|
+
for j := 0; j <= 5; j++ {
|
|
215
|
+
for i := 1; i <= n; i++ {
|
|
216
|
+
copy(buf, a)
|
|
217
|
+
copy(buf[8:], r[i-1])
|
|
218
|
+
block.Encrypt(buf, buf)
|
|
219
|
+
t := uint64(n*j + i)
|
|
220
|
+
for k := 0; k < 8; k++ {
|
|
221
|
+
a[k] = buf[k] ^ byte(t>>(56-8*k))
|
|
222
|
+
}
|
|
223
|
+
copy(r[i-1], buf[8:])
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
out := append([]byte{}, a...)
|
|
227
|
+
for _, b := range r {
|
|
228
|
+
out = append(out, b...)
|
|
229
|
+
}
|
|
230
|
+
return out
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
func tnAESUnwrapRounds(block cipher.Block, data []byte) ([]byte, []byte) {
|
|
234
|
+
n := len(data)/8 - 1
|
|
235
|
+
a := append([]byte{}, data[:8]...)
|
|
236
|
+
r := make([][]byte, n)
|
|
237
|
+
for i := range r {
|
|
238
|
+
r[i] = append([]byte{}, data[8*(i+1):8*(i+2)]...)
|
|
239
|
+
}
|
|
240
|
+
buf := make([]byte, 16)
|
|
241
|
+
for j := 5; j >= 0; j-- {
|
|
242
|
+
for i := n; i >= 1; i-- {
|
|
243
|
+
t := uint64(n*j + i)
|
|
244
|
+
for k := 0; k < 8; k++ {
|
|
245
|
+
buf[k] = a[k] ^ byte(t>>(56-8*k))
|
|
246
|
+
}
|
|
247
|
+
copy(buf[8:], r[i-1])
|
|
248
|
+
block.Decrypt(buf, buf)
|
|
249
|
+
copy(a, buf[:8])
|
|
250
|
+
copy(r[i-1], buf[8:])
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
plain := []byte{}
|
|
254
|
+
for _, b := range r {
|
|
255
|
+
plain = append(plain, b...)
|
|
256
|
+
}
|
|
257
|
+
return a, plain
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
func tnWrapState() *TnError {
|
|
261
|
+
return TnNodeError("Error", "", "Trying to add data in unsupported state")
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
func (c *TnCipher) keyWrap(data []byte) []byte {
|
|
265
|
+
if c.wrapped {
|
|
266
|
+
panic(tnWrapState())
|
|
267
|
+
}
|
|
268
|
+
c.wrapped = true
|
|
269
|
+
padded := c.mode == "wrap-pad"
|
|
270
|
+
if !c.decrypt {
|
|
271
|
+
if padded {
|
|
272
|
+
aiv := append(append([]byte{}, c.iv...), byte(len(data)>>24), byte(len(data)>>16), byte(len(data)>>8), byte(len(data)))
|
|
273
|
+
plain := append(append([]byte{}, data...), make([]byte, (8-len(data)%8)%8)...)
|
|
274
|
+
if len(plain) == 8 {
|
|
275
|
+
out := make([]byte, 16)
|
|
276
|
+
c.block.Encrypt(out, append(aiv, plain...))
|
|
277
|
+
return out
|
|
278
|
+
}
|
|
279
|
+
data = plain
|
|
280
|
+
c.iv = aiv
|
|
281
|
+
} else if len(data) < 16 || len(data)%8 != 0 {
|
|
282
|
+
panic(tnWrapState())
|
|
283
|
+
}
|
|
284
|
+
r := make([][]byte, len(data)/8)
|
|
285
|
+
for i := range r {
|
|
286
|
+
r[i] = append([]byte{}, data[8*i:8*i+8]...)
|
|
287
|
+
}
|
|
288
|
+
return tnAESWrapRounds(c.block, append([]byte{}, c.iv...), r)
|
|
289
|
+
}
|
|
290
|
+
if len(data) < 16 || len(data)%8 != 0 {
|
|
291
|
+
panic(tnWrapState())
|
|
292
|
+
}
|
|
293
|
+
var a, plain []byte
|
|
294
|
+
if padded && len(data) == 16 {
|
|
295
|
+
buf := make([]byte, 16)
|
|
296
|
+
c.block.Decrypt(buf, data)
|
|
297
|
+
a, plain = buf[:8], buf[8:]
|
|
298
|
+
} else {
|
|
299
|
+
a, plain = tnAESUnwrapRounds(c.block, data)
|
|
300
|
+
}
|
|
301
|
+
if !padded {
|
|
302
|
+
if subtle.ConstantTimeCompare(a, c.iv) != 1 {
|
|
303
|
+
panic(tnWrapState())
|
|
304
|
+
}
|
|
305
|
+
return plain
|
|
306
|
+
}
|
|
307
|
+
size := int(a[4])<<24 | int(a[5])<<16 | int(a[6])<<8 | int(a[7])
|
|
308
|
+
if subtle.ConstantTimeCompare(a[:4], c.iv) != 1 || size > len(plain) || size <= len(plain)-8 {
|
|
309
|
+
panic(tnWrapState())
|
|
310
|
+
}
|
|
311
|
+
for _, b := range plain[size:] {
|
|
312
|
+
if b != 0 {
|
|
313
|
+
panic(tnWrapState())
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return plain[:size]
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// aes128-wrap / id-aes256-wrap-pad / …
|
|
320
|
+
func tnCreateWrapCipher(alg string, key interface{}, iv interface{}, decrypt bool) *TnCipher {
|
|
321
|
+
name := strings.TrimPrefix(strings.ToLower(alg), "id-")
|
|
322
|
+
mode := "wrap"
|
|
323
|
+
if strings.HasSuffix(name, "-wrap-pad") {
|
|
324
|
+
mode = "wrap-pad"
|
|
325
|
+
}
|
|
326
|
+
bits := strings.TrimSuffix(strings.TrimSuffix(strings.TrimPrefix(name, "aes"), "-pad"), "-wrap")
|
|
327
|
+
k := tnCryptoBytes(key, "")
|
|
328
|
+
if strconv.Itoa(len(k)*8) != bits {
|
|
329
|
+
panic(TnNodeError("RangeError", "ERR_CRYPTO_INVALID_KEYLEN", "Invalid key length"))
|
|
330
|
+
}
|
|
331
|
+
block, err := aes.NewCipher(k)
|
|
332
|
+
if err != nil {
|
|
333
|
+
panic(TnNodeError("RangeError", "ERR_CRYPTO_INVALID_KEYLEN", "Invalid key length"))
|
|
334
|
+
}
|
|
335
|
+
ivBytes := []byte{0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6}
|
|
336
|
+
if mode == "wrap-pad" {
|
|
337
|
+
ivBytes = []byte{0xA6, 0x59, 0x59, 0xA6}
|
|
338
|
+
}
|
|
339
|
+
if iv != nil && !TnIsNil(iv) {
|
|
340
|
+
ivBytes = tnCryptoBytes(iv, "")
|
|
341
|
+
}
|
|
342
|
+
if len(ivBytes) != 8 && mode == "wrap" || len(ivBytes) != 4 && mode == "wrap-pad" {
|
|
343
|
+
panic(TnNodeError("TypeError", "ERR_CRYPTO_INVALID_IV", "Invalid initialization vector"))
|
|
344
|
+
}
|
|
345
|
+
return &TnCipher{decrypt: decrypt, mode: mode, block: block, iv: ivBytes}
|
|
196
346
|
}
|
|
197
347
|
|
|
198
348
|
func TnCreateCipher(alg string, key interface{}, iv interface{}, decrypt bool, options ...interface{}) *TnCipher {
|
|
349
|
+
if strings.Contains(strings.ToLower(alg), "-wrap") {
|
|
350
|
+
return tnCreateWrapCipher(alg, key, iv, decrypt)
|
|
351
|
+
}
|
|
199
352
|
parts := strings.Split(strings.ToLower(alg), "-")
|
|
200
353
|
if len(parts) != 3 || parts[0] != "aes" {
|
|
201
354
|
panic(TnNodeError("Error", "ERR_CRYPTO_UNKNOWN_CIPHER", "Unknown cipher"))
|
|
@@ -238,6 +391,8 @@ func TnCreateCipher(alg string, key interface{}, iv interface{}, decrypt bool, o
|
|
|
238
391
|
|
|
239
392
|
func (c *TnCipher) process(data []byte) []byte {
|
|
240
393
|
switch c.mode {
|
|
394
|
+
case "wrap", "wrap-pad":
|
|
395
|
+
return c.keyWrap(data)
|
|
241
396
|
case "ctr":
|
|
242
397
|
out := make([]byte, len(data))
|
|
243
398
|
c.stream.XORKeyStream(out, data)
|
|
@@ -264,7 +419,7 @@ func (c *TnCipher) finish() []byte {
|
|
|
264
419
|
}
|
|
265
420
|
c.finalized = true
|
|
266
421
|
switch c.mode {
|
|
267
|
-
case "ctr":
|
|
422
|
+
case "ctr", "wrap", "wrap-pad":
|
|
268
423
|
return []byte{}
|
|
269
424
|
case "gcm":
|
|
270
425
|
if c.decrypt {
|
|
@@ -344,6 +499,38 @@ func (c *TnCipher) setAutoPadding(pad ...bool) *TnCipher {
|
|
|
344
499
|
c.autoPadding = len(pad) == 0 || pad[0]
|
|
345
500
|
return c
|
|
346
501
|
}`;
|
|
502
|
+
// Hash / Hmac as streams (write / end / pipe / pipeline): a transform that
|
|
503
|
+
// hashes what is written and pushes the digest at the end, as in Node; only
|
|
504
|
+
// programs that use a hash as a stream carry it
|
|
505
|
+
const hashStreamHelper = `func (h *TnHash) tnStream() *TnStream {
|
|
506
|
+
if h.stream == nil {
|
|
507
|
+
s := TnNewTransform()
|
|
508
|
+
s.goTransform = func(chunk []byte) {
|
|
509
|
+
if !h.finalized {
|
|
510
|
+
h.h.Write(chunk)
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
s.goFlush = func() []byte { return h.digestBytes() }
|
|
514
|
+
h.stream = s
|
|
515
|
+
}
|
|
516
|
+
return h.stream.(*TnStream)
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
func (h *TnHash) write(chunk interface{}, args ...interface{}) bool {
|
|
520
|
+
return h.tnStream().write(chunk, args...)
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
func (h *TnHash) end(args ...interface{}) *TnStream { return h.tnStream().end(args...) }
|
|
524
|
+
|
|
525
|
+
func (h *TnHash) on(event string, fn interface{}) *TnEventEmitter { return h.tnStream().on(event, fn) }
|
|
526
|
+
|
|
527
|
+
func (h *TnHash) once(event string, fn interface{}) *TnEventEmitter {
|
|
528
|
+
return h.tnStream().once(event, fn)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
func (h *TnHash) read(size ...float64) interface{} { return h.tnStream().read(size...) }
|
|
532
|
+
|
|
533
|
+
func (h *TnHash) setEncoding(enc string) *TnStream { return h.tnStream().setEncoding(enc) }`;
|
|
347
534
|
export function registerCrypto(r) {
|
|
348
535
|
defineModule(r, 'crypto');
|
|
349
536
|
r.globals['crypto'] = 'crypto';
|
|
@@ -368,7 +555,21 @@ export function registerCrypto(r) {
|
|
|
368
555
|
'strconv',
|
|
369
556
|
'strings'
|
|
370
557
|
], ['error', 'buffer', 'dynamic'], cryptoHelper);
|
|
371
|
-
defineClass(r, 'crypto.Hash', '*TnHash', 'class', 'update=*TnHash; copy=*TnHash', '', '', '');
|
|
558
|
+
defineClass(r, 'crypto.Hash', '*TnHash', 'class', 'update=*TnHash; copy=*TnHash; write=bool; end=*TnStream; on=*TnEventEmitter; once=*TnEventEmitter; read=interface{}; setEncoding=*TnStream; pipe=interface{}', '', '', '');
|
|
559
|
+
defineHelper(r, 'hashStream', [], ['nodeCrypto', 'nodeStream'], hashStreamHelper);
|
|
560
|
+
for (const method of ['write', 'end', 'on', 'once', 'read', 'setEncoding']) {
|
|
561
|
+
r.methods[`crypto.Hash#${method}`] = (c) => {
|
|
562
|
+
r.helper('hashStream');
|
|
563
|
+
return `${c.recv}.${method}(${c.args.join(', ')})`;
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
r.methods['crypto.Hash#pipe'] = (c) => {
|
|
567
|
+
r.helper('hashStream');
|
|
568
|
+
return `TnPipe(${c.recv}, ${c.args.join(', ')})`;
|
|
569
|
+
};
|
|
570
|
+
r.typers['crypto.Hash#pipe'] = (c) => c.types[0] || '*TnStream';
|
|
571
|
+
r.paramTypers['crypto.Hash#on'] = (nodes) => streamListenerTypes(nodes);
|
|
572
|
+
r.paramTypers['crypto.Hash#once'] = r.paramTypers['crypto.Hash#on'];
|
|
372
573
|
r.classes['crypto.Hmac'] = r.classes['crypto.Hash'];
|
|
373
574
|
defineClass(r, 'crypto.Cipher', '*TnCipher', 'class', 'setAAD=*TnCipher; setAuthTag=*TnCipher; getAuthTag=[]byte; setAutoPadding=*TnCipher', '', '', '');
|
|
374
575
|
r.classes['crypto.Cipheriv'] = r.classes['crypto.Cipher'];
|