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.
Files changed (170) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +258 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +17 -1
  5. package/bin/cli.js +26 -11
  6. package/bin/go/bridge.js +74 -14
  7. package/bin/go/convert.js +59 -1
  8. package/bin/go/metadata.js +13 -4
  9. package/bin/go/overrides.js +4 -6
  10. package/bin/go/stdlib.generated.js +2 -0
  11. package/bin/index.js +338 -47
  12. package/bin/main.js +36 -14
  13. package/bin/node/assert.js +2 -2
  14. package/bin/node/buffer.js +9 -0
  15. package/bin/node/child_process.js +51 -3
  16. package/bin/node/cluster.js +41 -3
  17. package/bin/node/core.js +1 -1
  18. package/bin/node/crypto.js +205 -4
  19. package/bin/node/dgram.js +230 -6
  20. package/bin/node/dns.js +471 -8
  21. package/bin/node/errno_tables.js +445 -0
  22. package/bin/node/events.js +1 -1
  23. package/bin/node/fs.js +22 -15
  24. package/bin/node/fs_watch.js +407 -0
  25. package/bin/node/http.js +128 -30
  26. package/bin/node/http2.js +37 -7
  27. package/bin/node/module_v8.js +142 -0
  28. package/bin/node/net.js +9 -6
  29. package/bin/node/process.js +242 -0
  30. package/bin/node/readline.js +272 -1
  31. package/bin/node/registry.js +3 -1
  32. package/bin/node/sqlite.js +60 -12
  33. package/bin/node/stream.js +145 -10
  34. package/bin/node/test.js +311 -19
  35. package/bin/node/timers.js +1 -1
  36. package/bin/node/typed_arrays.js +608 -0
  37. package/bin/node/url.js +23 -5
  38. package/bin/node/util.js +21 -1
  39. package/bin/node/util_extra.js +6 -30
  40. package/bin/node/worker_threads.js +79 -19
  41. package/bin/node/zlib.js +131 -16
  42. package/bin/transpiler.js +4729 -616
  43. package/bin/typecheck-node.js +1 -0
  44. package/bin/version.js +1 -1
  45. package/conformance.md +1030 -877
  46. package/examples/README.md +54 -0
  47. package/examples/fibonacci/README.md +89 -0
  48. package/examples/fibonacci/bench.mjs +87 -0
  49. package/examples/fibonacci/main.ts +21 -0
  50. package/examples/hello-world/README.md +62 -0
  51. package/examples/hello-world/bench.mjs +66 -0
  52. package/examples/hello-world/main.ts +7 -0
  53. package/examples/json-server/README.md +127 -0
  54. package/examples/json-server/bench.mjs +263 -0
  55. package/examples/json-server/main.ts +67 -0
  56. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  57. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  58. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  59. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  60. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  61. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  62. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  63. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  64. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  65. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  66. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  67. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  68. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  69. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  70. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  71. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  72. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  73. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  74. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  75. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  76. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  77. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  78. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  79. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  80. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  81. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  82. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  83. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  84. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  85. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  86. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  87. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  88. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  89. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  90. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  91. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  92. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  93. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  94. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  95. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  96. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  97. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  98. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  99. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  100. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  101. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  102. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  103. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  104. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  105. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  106. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  107. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  108. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  109. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  110. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  111. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  112. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  113. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  114. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  115. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  116. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  117. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  118. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  119. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  120. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  121. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  122. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  123. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  124. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  125. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  126. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  127. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  128. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  129. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  130. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  131. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  132. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  133. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  134. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  135. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  136. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  137. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  138. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  140. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  141. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  142. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  143. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  144. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  145. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  146. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  147. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  148. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  149. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  150. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  151. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  152. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  153. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  154. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  155. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  156. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  157. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  158. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  159. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  160. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  161. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  162. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  163. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  164. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  165. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  166. package/govendor/manifest.txt +3 -1
  167. package/package.json +1 -1
  168. package/types/typenative-go.d.ts +23 -0
  169. package/types/typenative-npm.d.ts +241 -5
  170. package/types/typenative.d.ts +302 -12
@@ -104,7 +104,7 @@ func (rl *TnReadline) questionAsync(query string, options ...interface{}) chan s
104
104
  rl.writeOut(query)
105
105
  rl.pendingQuestion = func(answer string) {
106
106
  TnLoopUnref()
107
- ch <- answer
107
+ TnAsyncSend(ch, answer)
108
108
  }
109
109
  return ch
110
110
  }
@@ -228,6 +228,270 @@ func TnClearLine(stream interface{}, dir float64, rest ...interface{}) bool {
228
228
  func TnClearScreenDown(stream interface{}, rest ...interface{}) bool {
229
229
  return tnCursorWrite(stream, "\\x1b[0J", tnCursorCallback(rest))
230
230
  }`;
231
+ // readline.emitKeypressEvents(stream): 'keypress' (str, key) events decoded
232
+ // from the stream's data, a port of Node's emitKeys
233
+ const keypressHelper = `// A decoded key; "" stands for undefined
234
+ type TnKey struct {
235
+ sequence string
236
+ name string
237
+ ctrl bool
238
+ meta bool
239
+ shift bool
240
+ code string
241
+ }
242
+
243
+ type tnKeyDecoder struct {
244
+ buf []rune // -1 is the escape timeout ('' in Node)
245
+ timer *TnTimeout
246
+ }
247
+
248
+ var tnKeyDecoders = map[*TnStream]*tnKeyDecoder{}
249
+
250
+ func TnEmitKeypressEvents(stream interface{}, rest ...interface{}) {
251
+ sl, ok := stream.(tnStreamLike)
252
+ if !ok || TnIsNil(stream) {
253
+ return
254
+ }
255
+ s := sl.tnStream()
256
+ if tnKeyDecoders[s] != nil {
257
+ return
258
+ }
259
+ d := &tnKeyDecoder{}
260
+ tnKeyDecoders[s] = d
261
+ var pending []byte
262
+ s.TnEventEmitter.on("data", func(chunk interface{}) {
263
+ if s.listenerCount("keypress") == 0 {
264
+ return
265
+ }
266
+ switch c := chunk.(type) {
267
+ case string:
268
+ pending = append(pending, c...)
269
+ case []byte:
270
+ pending = append(pending, c...)
271
+ default:
272
+ pending = append(pending, TnJSString(chunk)...)
273
+ }
274
+ // whole UTF-8 characters only, as a StringDecoder would
275
+ text := []rune{}
276
+ for len(pending) > 0 && utf8.FullRune(pending) {
277
+ r, size := utf8.DecodeRune(pending)
278
+ text = append(text, r)
279
+ pending = pending[size:]
280
+ }
281
+ if len(text) == 0 {
282
+ return
283
+ }
284
+ if d.timer != nil {
285
+ TnClearTimeout(d.timer)
286
+ d.timer = nil
287
+ }
288
+ for _, r := range text {
289
+ d.feed(s, r)
290
+ }
291
+ // a lone ESC at the end is the Escape key unless more follows soon
292
+ if text[len(text)-1] == 0x1b {
293
+ d.timer = TnSetTimeout(500, func() {
294
+ d.timer = nil
295
+ if len(d.buf) > 0 {
296
+ d.feed(s, -1)
297
+ }
298
+ })
299
+ }
300
+ })
301
+ s.listening("data")
302
+ }
303
+
304
+ func (d *tnKeyDecoder) feed(s *TnStream, r rune) {
305
+ d.buf = append(d.buf, r)
306
+ for len(d.buf) > 0 {
307
+ key, str, emit, n := tnDecodeKey(d.buf)
308
+ if n == 0 {
309
+ return
310
+ }
311
+ d.buf = d.buf[n:]
312
+ if emit {
313
+ s.emit("keypress", str, key)
314
+ }
315
+ }
316
+ }
317
+
318
+ type tnKeyNeedMore struct{}
319
+
320
+ var tnKeyTilde = regexp.MustCompile("^(?:(\\\\d\\\\d?)(?:;(\\\\d))?([~^$])|(\\\\d{3}~))$")
321
+ var tnKeyLetter = regexp.MustCompile("^((\\\\d;)?(\\\\d))?([A-Za-z])$")
322
+
323
+ var tnKeyCodes = map[string]string{
324
+ "[P": "f1", "[Q": "f2", "[R": "f3", "[S": "f4",
325
+ "OP": "f1", "OQ": "f2", "OR": "f3", "OS": "f4",
326
+ "[11~": "f1", "[12~": "f2", "[13~": "f3", "[14~": "f4",
327
+ "[200~": "paste-start", "[201~": "paste-end",
328
+ "[[A": "f1", "[[B": "f2", "[[C": "f3", "[[D": "f4", "[[E": "f5",
329
+ "[15~": "f5", "[17~": "f6", "[18~": "f7", "[19~": "f8", "[20~": "f9", "[21~": "f10", "[23~": "f11", "[24~": "f12",
330
+ "[A": "up", "[B": "down", "[C": "right", "[D": "left", "[E": "clear", "[F": "end", "[H": "home",
331
+ "OA": "up", "OB": "down", "OC": "right", "OD": "left", "OE": "clear", "OF": "end", "OH": "home",
332
+ "[1~": "home", "[2~": "insert", "[3~": "delete", "[4~": "end", "[5~": "pageup", "[6~": "pagedown",
333
+ "[[5~": "pageup", "[[6~": "pagedown", "[7~": "home", "[8~": "end",
334
+ "[a": "up shift", "[b": "down shift", "[c": "right shift", "[d": "left shift", "[e": "clear shift",
335
+ "[2$": "insert shift", "[3$": "delete shift", "[5$": "pageup shift", "[6$": "pagedown shift", "[7$": "home shift", "[8$": "end shift",
336
+ "Oa": "up ctrl", "Ob": "down ctrl", "Oc": "right ctrl", "Od": "left ctrl", "Oe": "clear ctrl",
337
+ "[2^": "insert ctrl", "[3^": "delete ctrl", "[5^": "pageup ctrl", "[6^": "pagedown ctrl", "[7^": "home ctrl", "[8^": "end ctrl",
338
+ "[Z": "tab shift",
339
+ }
340
+
341
+ // One key from the start of in: n is how many characters it used (0 when
342
+ // it needs more input)
343
+ func tnDecodeKey(in []rune) (key *TnKey, str string, emit bool, n int) {
344
+ defer func() {
345
+ if x := recover(); x != nil {
346
+ if _, ok := x.(tnKeyNeedMore); !ok {
347
+ panic(x)
348
+ }
349
+ key, str, emit, n = nil, "", false, 0
350
+ }
351
+ }()
352
+ i := 0
353
+ s := ""
354
+ next := func() rune {
355
+ if i >= len(in) {
356
+ panic(tnKeyNeedMore{})
357
+ }
358
+ r := in[i]
359
+ i++
360
+ if r >= 0 {
361
+ s += string(r)
362
+ }
363
+ return r
364
+ }
365
+ isDigit := func(r rune) bool { return r >= '0' && r <= '9' }
366
+ k := &TnKey{}
367
+ ch := next()
368
+ escaped := false
369
+ if ch == 0x1b {
370
+ escaped = true
371
+ ch = next()
372
+ if ch == 0x1b {
373
+ ch = next()
374
+ }
375
+ }
376
+ if escaped && (ch == 'O' || ch == '[') {
377
+ code := string(ch)
378
+ modifier := 0
379
+ if ch == 'O' {
380
+ ch = next()
381
+ if isDigit(ch) {
382
+ modifier = int(ch-'0') - 1
383
+ ch = next()
384
+ }
385
+ if ch >= 0 {
386
+ code += string(ch)
387
+ }
388
+ } else {
389
+ ch = next()
390
+ if ch == '[' {
391
+ code += "["
392
+ ch = next()
393
+ }
394
+ // the command starts at ch (Node: s.length - 1)
395
+ cmdStart := len(s) - 1
396
+ if ch >= 0 {
397
+ cmdStart = len(s) - utf8.RuneLen(ch)
398
+ }
399
+ if isDigit(ch) {
400
+ ch = next()
401
+ if isDigit(ch) {
402
+ ch = next()
403
+ if isDigit(ch) {
404
+ ch = next()
405
+ }
406
+ }
407
+ }
408
+ if ch == ';' {
409
+ ch = next()
410
+ if isDigit(ch) {
411
+ next()
412
+ }
413
+ }
414
+ cmd := s[cmdStart:]
415
+ if m := tnKeyTilde.FindStringSubmatch(cmd); m != nil {
416
+ if m[4] != "" {
417
+ code += m[4]
418
+ } else {
419
+ code += m[1] + m[3]
420
+ modifier = 0
421
+ if m[2] != "" {
422
+ modifier, _ = strconv.Atoi(m[2])
423
+ modifier--
424
+ }
425
+ }
426
+ } else if m := tnKeyLetter.FindStringSubmatch(cmd); m != nil {
427
+ code += m[4]
428
+ modifier = 0
429
+ if m[3] != "" {
430
+ modifier, _ = strconv.Atoi(m[3])
431
+ modifier--
432
+ }
433
+ } else {
434
+ code += cmd
435
+ }
436
+ }
437
+ k.ctrl = modifier&4 != 0
438
+ k.meta = modifier&10 != 0
439
+ k.shift = modifier&1 != 0
440
+ k.code = code
441
+ if named, ok := tnKeyCodes[code]; ok {
442
+ parts := strings.SplitN(named, " ", 2)
443
+ k.name = parts[0]
444
+ if len(parts) == 2 && parts[1] == "shift" {
445
+ k.shift = true
446
+ } else if len(parts) == 2 {
447
+ k.ctrl = true
448
+ }
449
+ } else {
450
+ k.name = "undefined"
451
+ }
452
+ } else {
453
+ named := true
454
+ switch {
455
+ case ch == '\\r':
456
+ k.name, k.meta = "return", escaped
457
+ case ch == '\\n':
458
+ k.name, k.meta = "enter", escaped
459
+ case ch == '\\t':
460
+ k.name, k.meta = "tab", escaped
461
+ case ch == '\\b' || ch == 0x7f:
462
+ k.name, k.meta = "backspace", escaped
463
+ case ch == 0x1b:
464
+ k.name, k.meta = "escape", escaped
465
+ case ch == ' ':
466
+ k.name, k.meta = "space", escaped
467
+ case !escaped && ch >= 0 && ch <= 0x1a:
468
+ k.name, k.ctrl = string(rune(ch+'a'-1)), true
469
+ case isDigit(ch) || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'):
470
+ k.name = strings.ToLower(string(ch))
471
+ k.shift = ch >= 'A' && ch <= 'Z'
472
+ k.meta = escaped
473
+ case escaped:
474
+ if ch < 0 {
475
+ k.name = "escape"
476
+ } else {
477
+ named = false
478
+ }
479
+ k.meta = true
480
+ default:
481
+ named = false
482
+ }
483
+ if !named && !escaped {
484
+ k.sequence = s
485
+ // a single unnamed character, e.g. "."
486
+ return k, s, utf8.RuneCountInString(s) == 1, i
487
+ }
488
+ }
489
+ k.sequence = s
490
+ if escaped {
491
+ return k, "", s != "", i
492
+ }
493
+ return k, s, s != "", i
494
+ }`;
231
495
  export function registerReadline(r) {
232
496
  defineModule(r, 'readline');
233
497
  defineModule(r, 'readline/promises');
@@ -280,6 +544,13 @@ export function registerReadline(r) {
280
544
  return event && event.kind === 'StringLiteral' && event.text === 'line' ? ['string'] : [];
281
545
  };
282
546
  r.paramTypers['readline.Interface#once'] = r.paramTypers['readline.Interface#on'];
547
+ defineHelper(r, 'nodeKeypress', ['regexp', 'strconv', 'strings', 'unicode/utf8'], ['nodeStream', 'nodeEvents', 'timerWait'], keypressHelper);
548
+ defineClass(r, 'readline.Key', '*TnKey', 'class', '', 'sequence=string; name=string; ctrl=bool; meta=bool; shift=bool; code=string', '', '');
549
+ r.functions['readline.emitKeypressEvents'] = (call) => {
550
+ r.helper('nodeKeypress');
551
+ return `TnEmitKeypressEvents(${call.args.join(', ')})`;
552
+ };
553
+ r.results['readline.emitKeypressEvents'] = '';
283
554
  defineHelper(r, 'nodeReadlineCursor', [], ['nodeStream', 'nodeInvoke', 'eventLoop'], cursorHelper);
284
555
  const cursorFns = {
285
556
  cursorTo: 'TnCursorTo',
@@ -39,7 +39,9 @@ export function createNodeRegistry() {
39
39
  temp: (prefix) => prefix,
40
40
  typeOf: (_node) => '',
41
41
  visit: (_node) => '',
42
- categoryOf: (_node) => ''
42
+ categoryOf: (_node) => '',
43
+ isArrayBuffer: (_node) => false,
44
+ entryPoint: (_node) => ''
43
45
  };
44
46
  return registry;
45
47
  }
@@ -24,7 +24,9 @@ type TnSqliteDb struct {
24
24
  allowUnknown bool
25
25
  defensive bool
26
26
  limitOptions interface{}
27
- stmts []*TnSqliteStmt
27
+ // prepared statements not finalized yet; the JS objects are not kept, so
28
+ // one that becomes unreachable is finalized on the event loop
29
+ live map[*sqlite3.Stmt]bool
28
30
  limits *TnSqliteLimits
29
31
  // A JS throw inside a user-defined function, rethrown after the statement
30
32
  pending interface{}
@@ -361,13 +363,10 @@ func (db *TnSqliteDb) setAuthorizer(callback interface{}) {
361
363
 
362
364
  func (db *TnSqliteDb) close() {
363
365
  db.requireOpen()
364
- for _, st := range db.stmts {
365
- if st.s != nil {
366
- st.s.Close()
367
- st.s = nil
368
- }
366
+ for s := range db.live {
367
+ s.Close()
369
368
  }
370
- db.stmts = nil
369
+ db.live = nil
371
370
  err := db.conn.Close()
372
371
  db.conn = nil
373
372
  if err != nil {
@@ -417,7 +416,18 @@ func (db *TnSqliteDb) prepare(sql string, opts ...interface{}) *TnSqliteStmt {
417
416
  st.allowBare = tnSqliteFlag(opts[0], "allowBareNamedParameters", st.allowBare)
418
417
  st.allowUnknown = tnSqliteFlag(opts[0], "allowUnknownNamedParameters", st.allowUnknown)
419
418
  }
420
- db.stmts = append(db.stmts, st)
419
+ if db.live == nil {
420
+ db.live = map[*sqlite3.Stmt]bool{}
421
+ }
422
+ db.live[s] = true
423
+ runtime.AddCleanup(st, func(s *sqlite3.Stmt) {
424
+ TnLoopPost(func() {
425
+ if db.live[s] {
426
+ delete(db.live, s)
427
+ s.Close()
428
+ }
429
+ })
430
+ }, s)
421
431
  return st
422
432
  }
423
433
 
@@ -622,18 +632,56 @@ func (db *TnSqliteDb) applyChangeset(changeset []byte, options ...interface{}) b
622
632
  panic(TnNodeError("Error", "ERR_SQLITE_ERROR", "Changesets are not supported: the bundled SQLite has no session extension"))
623
633
  }
624
634
 
635
+ // serialize(dbName = 'main') / deserialize(buffer, { dbName }): the database
636
+ // image goes through a temporary file and SQLite's backup API (the driver
637
+ // has no sqlite3_serialize binding)
638
+ func tnSqliteTempFile() string {
639
+ f, err := os.CreateTemp("", "tnsqlite-*.db")
640
+ if err != nil {
641
+ panic(tnSqliteError(err))
642
+ }
643
+ f.Close()
644
+ return f.Name()
645
+ }
646
+
625
647
  func (db *TnSqliteDb) serialize(name ...interface{}) []byte {
626
648
  db.requireOpen()
627
- panic(TnNodeError("Error", "ERR_SQLITE_ERROR", "serialize() is not supported: the bundled SQLite driver has no sqlite3_serialize binding"))
649
+ schema := "main"
650
+ if len(name) > 0 {
651
+ if s, ok := name[0].(string); ok {
652
+ schema = s
653
+ }
654
+ }
655
+ path := tnSqliteTempFile()
656
+ defer os.Remove(path)
657
+ os.Remove(path)
658
+ db.check(db.conn.Backup(schema, path))
659
+ data, err := os.ReadFile(path)
660
+ if err != nil {
661
+ panic(tnSqliteError(err))
662
+ }
663
+ return data
628
664
  }
629
665
 
630
666
  func (db *TnSqliteDb) deserialize(data []byte, options ...interface{}) {
631
667
  db.requireOpen()
632
- panic(TnNodeError("Error", "ERR_SQLITE_ERROR", "deserialize() is not supported: the bundled SQLite driver has no sqlite3_deserialize binding"))
668
+ schema := "main"
669
+ if len(options) > 0 && options[0] != nil {
670
+ if s, ok := TnGet(options[0], "dbName").(string); ok {
671
+ schema = s
672
+ }
673
+ }
674
+ path := tnSqliteTempFile()
675
+ defer os.Remove(path)
676
+ if err := os.WriteFile(path, data, 0o600); err != nil {
677
+ panic(tnSqliteError(err))
678
+ }
679
+ db.check(db.conn.Restore(schema, path))
633
680
  }
634
681
 
635
682
  func (st *TnSqliteStmt) requireLive() {
636
- if st.s == nil {
683
+ // db.close() (or a reopen) finalized it
684
+ if st.s == nil || !st.db.live[st.s] {
637
685
  panic(tnSqliteState("statement has been finalized"))
638
686
  }
639
687
  st.db.requireOpen()
@@ -1063,7 +1111,7 @@ func TnSqliteBackup(db *TnSqliteDb, path string, opts ...interface{}) chan float
1063
1111
  }`;
1064
1112
  export function registerSqlite(r) {
1065
1113
  defineModule(r, 'sqlite');
1066
- defineHelper(r, 'nodeSqlite', ['github.com/ncruces/go-sqlite3', 'errors', 'fmt', 'math', 'math/big', 'reflect', 'sort', 'strconv', 'strings', 'time', 'unsafe'], ['dynamic', 'orderedMap', 'nodeInvoke', 'eventLoop', 'promise'], sqliteHelper);
1114
+ defineHelper(r, 'nodeSqlite', ['github.com/ncruces/go-sqlite3', 'errors', 'fmt', 'math', 'math/big', 'os', 'reflect', 'runtime', 'sort', 'strconv', 'strings', 'time', 'unsafe'], ['dynamic', 'orderedMap', 'nodeInvoke', 'eventLoop', 'promise'], sqliteHelper);
1067
1115
  const use = () => r.helper('nodeSqlite');
1068
1116
  defineClass(r, 'sqlite.DatabaseSync', '*TnSqliteDb', 'class', 'open=; close=; exec=; prepare=*TnSqliteStmt; location=*string; function=; aggregate=; enableLoadExtension=; enableDefensive=; loadExtension=; setAuthorizer=; createTagStore=*TnSqliteTagStore; createSession=interface{}; applyChangeset=bool; serialize=[]byte; deserialize=', '', 'isOpen=bool; isTransaction=bool; limits=*TnSqliteLimits', '');
1069
1117
  defineClass(r, 'sqlite.StatementSync', '*TnSqliteStmt', 'class', 'run=*TnSqliteRunResult; get=interface{}; all=[]interface{}; iterate=*TnSqliteIterator; columns=[]interface{}; setReadBigInts=; setReturnArrays=; setAllowBareNamedParameters=; setAllowUnknownNamedParameters=', '', 'sourceSQL=string; expandedSQL=string', '');
@@ -29,6 +29,8 @@ type TnStream struct {
29
29
  readEnded bool
30
30
  endEmitted bool
31
31
  flowing int
32
+ // a 'readable' listener: paused mode, the consumer pulls with read()
33
+ readableListening bool
32
34
  reading bool
33
35
  flowScheduled bool
34
36
  source func(s *TnStream)
@@ -170,7 +172,11 @@ func (s *TnStream) toChunk(chunk interface{}, enc string) interface{} {
170
172
  }
171
173
 
172
174
  func (s *TnStream) push(chunk interface{}, encoding ...string) bool {
173
- if chunk == nil {
175
+ // nullable strings (cond ? text : null) and typed nils
176
+ if p, ok := chunk.(*string); ok && p != nil {
177
+ chunk = *p
178
+ }
179
+ if chunk == nil || TnIsNil(chunk) {
174
180
  s.readEnded = true
175
181
  if s.decoder != nil {
176
182
  if rest := s.decoder.end(); rest != "" {
@@ -216,6 +222,10 @@ func (s *TnStream) flow() {
216
222
  if s.destroyed {
217
223
  return
218
224
  }
225
+ if s.readableListening && s.flowing != 1 {
226
+ s.flowReadable()
227
+ return
228
+ }
219
229
  for s.flowing == 1 && len(s.queue) > 0 {
220
230
  chunk := s.queue[0]
221
231
  s.queue = s.queue[1:]
@@ -230,6 +240,32 @@ func (s *TnStream) flow() {
230
240
  }
231
241
  }
232
242
 
243
+ // Paused mode: 'readable' while there is data, then one last 'readable' (read()
244
+ // gives null) before 'end', as in Node
245
+ func (s *TnStream) flowReadable() {
246
+ for !s.endEmitted && (len(s.queue) > 0 || s.readEnded) {
247
+ before := len(s.queue)
248
+ s.emit("readable")
249
+ if s.destroyed || s.flowing == 1 {
250
+ return
251
+ }
252
+ if len(s.queue) == 0 && s.readEnded {
253
+ if before == 0 {
254
+ s.emitEnd()
255
+ return
256
+ }
257
+ continue
258
+ }
259
+ // the consumer left data buffered: wait for read() or more data
260
+ if len(s.queue) >= before {
261
+ return
262
+ }
263
+ }
264
+ if len(s.queue) == 0 {
265
+ s.requestRead()
266
+ }
267
+ }
268
+
233
269
  func (s *TnStream) requestRead() {
234
270
  if s.reading || s.readEnded || s.destroyed {
235
271
  return
@@ -293,6 +329,13 @@ func (s *TnStream) once(event string, fn interface{}) *TnEventEmitter {
293
329
  }
294
330
 
295
331
  func (s *TnStream) listening(event string) {
332
+ if event == "readable" && s.readableSide {
333
+ s.readableListening = true
334
+ s.flowing = 0
335
+ s.requestRead()
336
+ s.scheduleFlow()
337
+ return
338
+ }
296
339
  if event == "data" && s.flowing != 0 && s.readableSide {
297
340
  s.resume()
298
341
  }
@@ -314,7 +357,10 @@ func (s *TnStream) pause() *TnStream {
314
357
 
315
358
  func (s *TnStream) isPaused() bool { return s.flowing == 0 }
316
359
 
317
- // read(): paused-mode pull of the next buffered chunk (null when none)
360
+ // read(size?): paused-mode pull (null when nothing is buffered). As in Node,
361
+ // without a size it returns everything buffered as one Buffer / string, and
362
+ // with one it waits for that many bytes (characters with an encoding) unless
363
+ // the stream has ended; object mode returns one object
318
364
  func (s *TnStream) read(size ...float64) interface{} {
319
365
  if len(s.queue) == 0 {
320
366
  s.requestRead()
@@ -323,9 +369,83 @@ func (s *TnStream) read(size ...float64) interface{} {
323
369
  }
324
370
  return nil
325
371
  }
326
- chunk := s.queue[0]
327
- s.queue = s.queue[1:]
328
- return chunk
372
+ if s.objectMode {
373
+ chunk := s.queue[0]
374
+ s.queue = s.queue[1:]
375
+ s.emit("data", chunk)
376
+ return chunk
377
+ }
378
+ n := -1
379
+ if len(size) > 0 && size[0] >= 0 {
380
+ n = int(size[0])
381
+ }
382
+ total := 0
383
+ for _, c := range s.queue {
384
+ total += tnChunkLen(c)
385
+ }
386
+ if n == 0 || (n > total && !s.readEnded) {
387
+ s.requestRead()
388
+ return nil
389
+ }
390
+ if n < 0 || n > total {
391
+ n = total
392
+ }
393
+ var text []rune
394
+ var data []byte
395
+ isText := false
396
+ // (empty chunks, such as a split UTF-8 sequence, go along with the rest)
397
+ for len(s.queue) > 0 && (n > 0 || tnChunkLen(s.queue[0]) == 0) {
398
+ switch c := s.queue[0].(type) {
399
+ case string:
400
+ isText = true
401
+ r := []rune(c)
402
+ if len(r) > n {
403
+ text = append(text, r[:n]...)
404
+ s.queue[0] = string(r[n:])
405
+ n = 0
406
+ continue
407
+ }
408
+ text = append(text, r...)
409
+ n -= len(r)
410
+ case []byte:
411
+ if len(c) > n {
412
+ data = append(data, c[:n]...)
413
+ s.queue[0] = c[n:]
414
+ n = 0
415
+ continue
416
+ }
417
+ data = append(data, c...)
418
+ n -= len(c)
419
+ default:
420
+ n--
421
+ }
422
+ s.queue = s.queue[1:]
423
+ }
424
+ if len(s.queue) == 0 && !s.readEnded {
425
+ s.requestRead()
426
+ }
427
+ var out interface{}
428
+ if isText && len(text) > 0 {
429
+ out = string(text)
430
+ } else if len(data) > 0 {
431
+ out = data
432
+ }
433
+ // what read() returns is also a 'data' event, as in Node
434
+ if out != nil {
435
+ s.emit("data", out)
436
+ }
437
+ return out
438
+ }
439
+
440
+ // The length read(size) counts: bytes, or characters of decoded strings
441
+ func tnChunkLen(c interface{}) int {
442
+ switch x := c.(type) {
443
+ case string:
444
+ return utf8.RuneCountInString(x)
445
+ case []byte:
446
+ return len(x)
447
+ }
448
+ return 1
329
449
  }
330
450
 
331
451
  func (s *TnStream) setEncoding(enc string) *TnStream {
@@ -1027,14 +1147,13 @@ func TnCreateWriteStream(path string, options ...interface{}) *TnStream {
1027
1147
  const zlibStreamHelper = `// zlib streams: Transforms buffering the input, (de)compressed at the end
1028
1148
  func TnZlibStream(format string, decompress bool, options ...interface{}) *TnStream {
1029
1149
  s := TnNewTransform()
1030
- level := tnZlibLevel(options)
1031
1150
  input := []byte{}
1032
1151
  s.goTransform = func(chunk []byte) { input = append(input, chunk...) }
1033
1152
  s.goFlush = func() []byte {
1034
1153
  if decompress {
1035
1154
  return tnDecompress(format, input)
1036
1155
  }
1037
- return tnCompress(format, input, level)
1156
+ return tnCompress(format, input, options)
1038
1157
  }
1039
1158
  return s
1040
1159
  }`;
@@ -1055,7 +1174,7 @@ export function registerStream(r) {
1055
1174
  defineModule(r, mod);
1056
1175
  r.namespaces['stream.promises'] = 'stream/promises';
1057
1176
  r.namespaces['stream.default'] = 'stream.Stream';
1058
- defineHelper(r, 'nodeStream', ['encoding/json', 'os', 'reflect'], ['nodeEvents', 'buffer', 'stringDecoder', 'eventLoop', 'promise', 'dynamic', 'nodeInvoke', 'stdinReader'], streamHelper);
1177
+ defineHelper(r, 'nodeStream', ['encoding/json', 'os', 'reflect', 'unicode/utf8'], ['nodeEvents', 'buffer', 'stringDecoder', 'eventLoop', 'promise', 'dynamic', 'nodeInvoke', 'stdinReader'], streamHelper);
1059
1178
  defineHelper(r, 'stdinReader', ['bufio', 'os'], [], `// One buffered reader shared by process.stdin, readline and question()
1060
1179
  var tnStdinBuffered *bufio.Reader
1061
1180
 
@@ -1110,6 +1229,7 @@ func tnStdin() *bufio.Reader {
1110
1229
  r.hookParams[`_${hook}`] = STREAM_HOOK_PARAMS[hook];
1111
1230
  r.methods['stream.Duplex#pipe'] = (c) => {
1112
1231
  use();
1232
+ useHashStream(r, c.types);
1113
1233
  return `TnPipe(${c.recv}, ${c.args.join(', ')})`;
1114
1234
  };
1115
1235
  r.typers['stream.Duplex#pipe'] = (c) => c.types[0] || '*TnStream';
@@ -1122,6 +1242,7 @@ func tnStdin() *bufio.Reader {
1122
1242
  r.results['stream.Readable.from'] = '*TnStream';
1123
1243
  const pipeline = (call) => {
1124
1244
  use();
1245
+ useHashStream(r, call.types);
1125
1246
  const last = call.nodes.length - 1;
1126
1247
  const hasCallback = last >= 0 && (call.nodes[last].kind === 'ArrowFunction' || call.nodes[last].kind === 'FunctionExpression');
1127
1248
  const streams = hasCallback ? call.args.slice(0, last) : call.args;
@@ -1137,6 +1258,7 @@ func tnStdin() *bufio.Reader {
1137
1258
  r.paramTypers['stream.finished'] = (_n) => ['interface{}'];
1138
1259
  r.functions['stream/promises.pipeline'] = (call) => {
1139
1260
  use();
1261
+ useHashStream(r, call.types);
1140
1262
  return `TnPipelinePromise(${call.args.join(', ')})`;
1141
1263
  };
1142
1264
  r.functions['stream/promises.finished'] = (call) => {
@@ -1185,12 +1307,16 @@ func tnStdin() *bufio.Reader {
1185
1307
  createInflate: 'inflate:true',
1186
1308
  createDeflateRaw: 'deflateRaw:false',
1187
1309
  createInflateRaw: 'inflateRaw:true',
1188
- createUnzip: 'unzip:true'
1310
+ createUnzip: 'unzip:true',
1311
+ createBrotliCompress: 'brotli:false',
1312
+ createBrotliDecompress: 'brotli:true'
1189
1313
  };
1190
1314
  for (const name of Object.keys(zlibStreams)) {
1191
1315
  const parts = zlibStreams[name].split(':');
1192
1316
  r.functions[`zlib.${name}`] = (call) => {
1193
1317
  r.helper('zlibStreams');
1318
+ if (parts[0] === 'brotli')
1319
+ r.helper('nodeBrotli');
1194
1320
  const options = call.args.length > 0 ? `, ${call.args[0]}` : '';
1195
1321
  return `TnZlibStream(${JSON.stringify(parts[0])}, ${parts[1]}${options})`;
1196
1322
  };
@@ -1199,8 +1325,10 @@ func tnStdin() *bufio.Reader {
1199
1325
  for (const method of ['on', 'once', 'resume', 'pause', 'setEncoding', 'pipe', 'read', 'destroy']) {
1200
1326
  r.functions[`process.stdin.${method}`] = (call) => {
1201
1327
  use();
1202
- if (method === 'pipe')
1328
+ if (method === 'pipe') {
1329
+ useHashStream(r, call.types);
1203
1330
  return `TnPipe(TnStdin(), ${call.args.join(', ')})`;
1331
+ }
1204
1332
  const refCall = method === 'destroy' ? '; TnStdin().stdinRef(false)' : '; TnStdin().stdinRef(TnStdin().flowing == 1)';
1205
1333
  return `func() *TnStream { TnStdin().${method}(${call.args.join(', ')})${refCall}; return TnStdin() }()`;
1206
1334
  };
@@ -1209,6 +1337,11 @@ func tnStdin() *bufio.Reader {
1209
1337
  r.paramTypers['process.stdin.on'] = (nodes) => streamListenerTypes(nodes);
1210
1338
  r.paramTypers['process.stdin.once'] = r.paramTypers['process.stdin.on'];
1211
1339
  }
1340
+ // Pulls in crypto's hashStream when a Hash is passed to pipe() / pipeline()
1341
+ function useHashStream(r, types) {
1342
+ if (types.indexOf('*TnHash') >= 0)
1343
+ r.helper('hashStream');
1344
+ }
1212
1345
  // Listener parameters of stream events
1213
1346
  export function streamListenerTypes(nodes) {
1214
1347
  const event = nodes[0];
@@ -1217,5 +1350,7 @@ export function streamListenerTypes(nodes) {
1217
1350
  return ['interface{}'];
1218
1351
  if (name === 'error')
1219
1352
  return ['interface{}'];
1353
+ if (name === 'keypress')
1354
+ return ['string', '*TnKey'];
1220
1355
  return [];
1221
1356
  }