typenative 0.4.0 → 0.5.0

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 (167) hide show
  1. package/.github/workflows/ci.yml +2 -0
  2. package/CHANGELOG.md +372 -0
  3. package/CONTRIBUTION.md +1 -1
  4. package/README.md +62 -22
  5. package/ROUND2-HANDOFF.md +200 -0
  6. package/bin/cli.js +2 -0
  7. package/bin/go/bridge.js +74 -14
  8. package/bin/go/convert.js +48 -0
  9. package/bin/go/metadata.js +13 -4
  10. package/bin/go/overrides.js +4 -6
  11. package/bin/go/stdlib.generated.js +2 -0
  12. package/bin/index.js +130 -2
  13. package/bin/main.js +2 -0
  14. package/bin/node/assert.js +2 -2
  15. package/bin/node/async_hooks.js +1 -1
  16. package/bin/node/buffer.js +16 -5
  17. package/bin/node/child_process.js +57 -9
  18. package/bin/node/cluster.js +44 -6
  19. package/bin/node/core.js +1 -1
  20. package/bin/node/crypto.js +205 -4
  21. package/bin/node/crypto_keys.js +21 -21
  22. package/bin/node/dgram.js +231 -7
  23. package/bin/node/diagnostics_channel.js +3 -3
  24. package/bin/node/dns.js +472 -9
  25. package/bin/node/errno_tables.js +445 -0
  26. package/bin/node/events.js +3 -3
  27. package/bin/node/fetch.js +2 -2
  28. package/bin/node/fs.js +24 -17
  29. package/bin/node/fs_watch.js +5 -5
  30. package/bin/node/http.js +131 -33
  31. package/bin/node/http2.js +7 -4
  32. package/bin/node/module_v8.js +142 -0
  33. package/bin/node/net.js +17 -14
  34. package/bin/node/path.js +17 -3
  35. package/bin/node/process.js +274 -9
  36. package/bin/node/readline.js +272 -1
  37. package/bin/node/registry.js +5 -1
  38. package/bin/node/sqlite.js +65 -17
  39. package/bin/node/stream.js +220 -22
  40. package/bin/node/stream_web.js +10 -10
  41. package/bin/node/system_extra.js +3 -3
  42. package/bin/node/test.js +834 -234
  43. package/bin/node/test_coverage.js +187 -0
  44. package/bin/node/test_report.js +1392 -0
  45. package/bin/node/test_run.js +575 -0
  46. package/bin/node/timers.js +4 -4
  47. package/bin/node/tls.js +5 -5
  48. package/bin/node/url.js +23 -5
  49. package/bin/node/util.js +49 -8
  50. package/bin/node/util_extra.js +13 -37
  51. package/bin/node/webview.js +654 -0
  52. package/bin/node/worker_threads.js +80 -20
  53. package/bin/node/zlib.js +3 -3
  54. package/bin/parse-node.js +4 -0
  55. package/bin/transpiler.js +14457 -1723
  56. package/bin/typecheck-node.js +1 -0
  57. package/bin/version.js +1 -1
  58. package/conformance.md +1153 -884
  59. package/examples/README.md +47 -10
  60. package/examples/desktop-notes/README.md +108 -0
  61. package/examples/desktop-notes/bench.mjs +125 -0
  62. package/examples/desktop-notes/main.ts +131 -0
  63. package/examples/desktop-notes/package-lock.json +726 -0
  64. package/examples/desktop-notes/package.json +9 -0
  65. package/examples/desktop-notes/ui.ts +176 -0
  66. package/examples/dupe-finder/README.md +76 -0
  67. package/examples/dupe-finder/bench.mjs +120 -0
  68. package/examples/dupe-finder/main.ts +87 -0
  69. package/examples/json-server/README.md +31 -29
  70. package/examples/json-server/bench.mjs +130 -38
  71. package/examples/mandelbrot-png/README.md +65 -0
  72. package/examples/mandelbrot-png/bench.mjs +95 -0
  73. package/examples/mandelbrot-png/main.ts +98 -0
  74. package/examples/mandelbrot-png/preview.png +0 -0
  75. package/examples/marked-cli/README.md +159 -0
  76. package/examples/marked-cli/bench.mjs +178 -0
  77. package/examples/marked-cli/main.ts +292 -0
  78. package/examples/marked-cli/parity.mjs +185 -0
  79. package/examples/marked-cli/vendor/marked/LICENSE +44 -0
  80. package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
  81. package/examples/todo-cli/README.md +69 -0
  82. package/examples/todo-cli/bench.mjs +89 -0
  83. package/examples/todo-cli/main.ts +92 -0
  84. package/examples/yaml-cli/README.md +136 -0
  85. package/examples/yaml-cli/bench.mjs +265 -0
  86. package/examples/yaml-cli/main.ts +29 -0
  87. package/examples/yaml-cli/parity.mjs +156 -0
  88. package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
  89. package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
  90. package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
  91. package/govendor/github.com/jchv/go-webview2/README.md +21 -0
  92. package/govendor/github.com/jchv/go-webview2/common.go +84 -0
  93. package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
  94. package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
  95. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
  96. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
  97. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
  98. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
  99. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
  100. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
  101. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
  102. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
  103. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
  104. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
  105. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
  106. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
  107. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
  108. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
  109. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
  110. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
  111. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
  112. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
  113. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
  114. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
  115. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
  116. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
  117. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
  118. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
  119. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
  120. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
  121. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
  122. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
  123. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
  124. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
  125. package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
  126. package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
  127. package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
  128. package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
  129. package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
  130. package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
  131. package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
  132. package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
  133. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
  134. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
  135. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
  136. package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
  137. package/govendor/github.com/jchv/go-winloader/README.md +124 -0
  138. package/govendor/github.com/jchv/go-winloader/common.go +11 -0
  139. package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
  140. package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
  141. package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
  142. package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
  143. package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
  144. package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
  145. package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
  146. package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
  147. package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
  148. package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
  149. package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
  150. package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
  151. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
  152. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
  153. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
  154. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
  155. package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
  156. package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
  157. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
  158. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
  159. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
  160. package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
  161. package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
  162. package/govendor/manifest.txt +3 -1
  163. package/package.json +1 -1
  164. package/tsparser/main.go +4 -0
  165. package/types/typenative-go.d.ts +23 -0
  166. package/types/typenative-npm.d.ts +359 -7
  167. package/types/typenative.d.ts +283 -42
@@ -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{}
@@ -111,7 +113,7 @@ func tnSqliteFlag(options interface{}, key string, def bool) bool {
111
113
  if options == nil {
112
114
  return def
113
115
  }
114
- switch v := TnGet(options, key).(type) {
116
+ switch v := TnGetValue(options, key).(type) {
115
117
  case bool:
116
118
  return v
117
119
  case *bool:
@@ -126,7 +128,7 @@ func tnSqliteNumber(options interface{}, key string, def float64) float64 {
126
128
  if options == nil {
127
129
  return def
128
130
  }
129
- switch v := TnGet(options, key).(type) {
131
+ switch v := TnGetValue(options, key).(type) {
130
132
  case float64:
131
133
  return v
132
134
  case *float64:
@@ -141,7 +143,7 @@ func tnSqliteString(options interface{}, key string, def string) string {
141
143
  if options == nil {
142
144
  return def
143
145
  }
144
- switch v := TnGet(options, key).(type) {
146
+ switch v := TnGetValue(options, key).(type) {
145
147
  case string:
146
148
  return v
147
149
  case *string:
@@ -182,7 +184,7 @@ func TnNewSqliteDb(path interface{}, opts ...interface{}) *TnSqliteDb {
182
184
  case []byte:
183
185
  db.path = string(p)
184
186
  default:
185
- if href, ok := TnGet(path, "href").(string); ok && path != nil {
187
+ if href, ok := TnGetValue(path, "href").(string); ok && path != nil {
186
188
  db.path = href
187
189
  } else {
188
190
  panic(tnSqliteArgType("The \\"path\\" argument must be a string, Uint8Array, or URL without null bytes."))
@@ -262,7 +264,7 @@ func (db *TnSqliteDb) open() {
262
264
  }
263
265
  if db.limitOptions != nil {
264
266
  for i, name := range tnSqliteLimitNames {
265
- if v, ok := TnGet(db.limitOptions, name).(float64); ok {
267
+ if v, ok := TnGetValue(db.limitOptions, name).(float64); ok {
266
268
  db.limits.set(i, v)
267
269
  }
268
270
  }
@@ -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 := TnGetValue(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
@@ -281,6 +317,8 @@ func (s *TnStream) errorLater(err interface{}) {
281
317
  func (s *TnStream) on(event string, fn interface{}) *TnEventEmitter {
282
318
  s.TnEventEmitter.on(event, fn)
283
319
  s.listening(event)
320
+ // (process.stdin, used through any reference: alive while flowing)
321
+ s.stdinRef(s.flowing == 1)
284
322
  return &s.TnEventEmitter
285
323
  }
286
324
 
@@ -289,10 +327,36 @@ func (s *TnStream) addListener(event string, fn interface{}) *TnEventEmitter { r
289
327
  func (s *TnStream) once(event string, fn interface{}) *TnEventEmitter {
290
328
  s.TnEventEmitter.once(event, fn)
291
329
  s.listening(event)
330
+ s.stdinRef(s.flowing == 1)
292
331
  return &s.TnEventEmitter
293
332
  }
294
333
 
334
+ // stream.on(…) / once(…) in TypeScript: the stream itself, for chaining (as in Node)
335
+ func (s *TnStream) onChain(event string, fn interface{}) *TnStream {
336
+ s.on(event, fn)
337
+ return s
338
+ }
339
+
340
+ func (s *TnStream) onceChain(event string, fn interface{}) *TnStream {
341
+ s.once(event, fn)
342
+ return s
343
+ }
344
+
345
+ // process.stdin holds the event loop while it flows (a no-op for other streams)
346
+ func (s *TnStream) stdinRef(flowing bool) {
347
+ if fn, ok := s.hooks["_stdinRef"].(func(bool)); ok {
348
+ fn(flowing)
349
+ }
350
+ }
351
+
295
352
  func (s *TnStream) listening(event string) {
353
+ if event == "readable" && s.readableSide {
354
+ s.readableListening = true
355
+ s.flowing = 0
356
+ s.requestRead()
357
+ s.scheduleFlow()
358
+ return
359
+ }
296
360
  if event == "data" && s.flowing != 0 && s.readableSide {
297
361
  s.resume()
298
362
  }
@@ -304,17 +368,22 @@ func (s *TnStream) listening(event string) {
304
368
  func (s *TnStream) resume() *TnStream {
305
369
  s.flowing = 1
306
370
  s.scheduleFlow()
371
+ s.stdinRef(true)
307
372
  return s
308
373
  }
309
374
 
310
375
  func (s *TnStream) pause() *TnStream {
311
376
  s.flowing = 0
377
+ s.stdinRef(false)
312
378
  return s
313
379
  }
314
380
 
315
381
  func (s *TnStream) isPaused() bool { return s.flowing == 0 }
316
382
 
317
- // read(): paused-mode pull of the next buffered chunk (null when none)
383
+ // read(size?): paused-mode pull (null when nothing is buffered). As in Node,
384
+ // without a size it returns everything buffered as one Buffer / string, and
385
+ // with one it waits for that many bytes (characters with an encoding) unless
386
+ // the stream has ended; object mode returns one object
318
387
  func (s *TnStream) read(size ...float64) interface{} {
319
388
  if len(s.queue) == 0 {
320
389
  s.requestRead()
@@ -323,9 +392,83 @@ func (s *TnStream) read(size ...float64) interface{} {
323
392
  }
324
393
  return nil
325
394
  }
326
- chunk := s.queue[0]
327
- s.queue = s.queue[1:]
328
- return chunk
395
+ if s.objectMode {
396
+ chunk := s.queue[0]
397
+ s.queue = s.queue[1:]
398
+ s.emit("data", chunk)
399
+ return chunk
400
+ }
401
+ n := -1
402
+ if len(size) > 0 && size[0] >= 0 {
403
+ n = int(size[0])
404
+ }
405
+ total := 0
406
+ for _, c := range s.queue {
407
+ total += tnChunkLen(c)
408
+ }
409
+ if n == 0 || (n > total && !s.readEnded) {
410
+ s.requestRead()
411
+ return nil
412
+ }
413
+ if n < 0 || n > total {
414
+ n = total
415
+ }
416
+ var text []rune
417
+ var data []byte
418
+ isText := false
419
+ // (empty chunks, such as a split UTF-8 sequence, go along with the rest)
420
+ for len(s.queue) > 0 && (n > 0 || tnChunkLen(s.queue[0]) == 0) {
421
+ switch c := s.queue[0].(type) {
422
+ case string:
423
+ isText = true
424
+ r := []rune(c)
425
+ if len(r) > n {
426
+ text = append(text, r[:n]...)
427
+ s.queue[0] = string(r[n:])
428
+ n = 0
429
+ continue
430
+ }
431
+ text = append(text, r...)
432
+ n -= len(r)
433
+ case []byte:
434
+ if len(c) > n {
435
+ data = append(data, c[:n]...)
436
+ s.queue[0] = c[n:]
437
+ n = 0
438
+ continue
439
+ }
440
+ data = append(data, c...)
441
+ n -= len(c)
442
+ default:
443
+ n--
444
+ }
445
+ s.queue = s.queue[1:]
446
+ }
447
+ if len(s.queue) == 0 && !s.readEnded {
448
+ s.requestRead()
449
+ }
450
+ var out interface{}
451
+ if isText && len(text) > 0 {
452
+ out = string(text)
453
+ } else if len(data) > 0 {
454
+ out = data
455
+ }
456
+ // what read() returns is also a 'data' event, as in Node
457
+ if out != nil {
458
+ s.emit("data", out)
459
+ }
460
+ return out
461
+ }
462
+
463
+ // The length read(size) counts: bytes, or characters of decoded strings
464
+ func tnChunkLen(c interface{}) int {
465
+ switch x := c.(type) {
466
+ case string:
467
+ return utf8.RuneCountInString(x)
468
+ case []byte:
469
+ return len(x)
470
+ }
471
+ return 1
329
472
  }
330
473
 
331
474
  func (s *TnStream) setEncoding(enc string) *TnStream {
@@ -606,7 +749,7 @@ func TnPipe[D tnStreamLike](source tnStreamLike, dest D, options ...interface{})
606
749
  dst := dest.tnStream()
607
750
  end := true
608
751
  if len(options) > 0 && options[0] != nil {
609
- if e, ok := TnGet(options[0], "end").(bool); ok {
752
+ if e, ok := TnGetValue(options[0], "end").(bool); ok {
610
753
  end = e
611
754
  }
612
755
  }
@@ -628,7 +771,7 @@ func TnReadableFrom(iterable interface{}, options ...interface{}) *TnStream {
628
771
  s := TnNewReadable(options...)
629
772
  s.objectMode = true
630
773
  if len(options) > 0 && options[0] != nil {
631
- if b, ok := TnGet(options[0], "objectMode").(bool); ok {
774
+ if b, ok := TnGetValue(options[0], "objectMode").(bool); ok {
632
775
  s.objectMode = b
633
776
  }
634
777
  }
@@ -670,6 +813,46 @@ func TnReadableFrom(iterable interface{}, options ...interface{}) *TnStream {
670
813
  return s
671
814
  }
672
815
 
816
+ // readable.compose(stream | fn): the source piped into a stream, or handed to
817
+ // a function (an async generator over the chunks, a node:test reporter) whose
818
+ // output becomes a readable
819
+ func (s *TnStream) compose(target interface{}, options ...interface{}) *TnStream {
820
+ if dst, ok := target.(tnStreamLike); ok {
821
+ return TnPipe(s, dst.tnStream())
822
+ }
823
+ fv := reflect.ValueOf(target)
824
+ if fv.Kind() != reflect.Func || fv.IsNil() {
825
+ panic(TnNodeError("TypeError", "ERR_INVALID_ARG_TYPE", "The \\"stream\\" argument must be of type function or an instance of Stream, Iterable, or AsyncIterable. Received "+TnJSString(target)))
826
+ }
827
+ ft := fv.Type()
828
+ var out []reflect.Value
829
+ if ft.NumIn() == 0 {
830
+ // a stream factory (node:test/reporters' spec / lcov)
831
+ out = fv.Call(nil)
832
+ } else if in := ft.In(0); in.Kind() == reflect.Chan {
833
+ // an async generator function: the chunks as a channel of its element type
834
+ src := s.tnIterator()
835
+ ch := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, in.Elem()), 65536)
836
+ go func() {
837
+ for v := range src {
838
+ ch.Send(tnInvokeArg(v, in.Elem()))
839
+ }
840
+ ch.Close()
841
+ }()
842
+ out = fv.Call([]reflect.Value{ch})
843
+ } else {
844
+ out = fv.Call([]reflect.Value{tnInvokeArg(s, in)})
845
+ }
846
+ if len(out) == 0 {
847
+ return s
848
+ }
849
+ result := out[0].Interface()
850
+ if dst, ok := result.(tnStreamLike); ok {
851
+ return TnPipe(s, dst.tnStream())
852
+ }
853
+ return TnReadableFrom(result)
854
+ }
855
+
673
856
  // for await (const chunk of readable)
674
857
  func (s *TnStream) tnIterator() chan interface{} {
675
858
  ch := make(chan interface{}, 65536)
@@ -892,12 +1075,6 @@ func TnStdin() *TnStream {
892
1075
  }
893
1076
  }
894
1077
  return s
895
- }
896
-
897
- func (s *TnStream) stdinRef(flowing bool) {
898
- if fn, ok := s.hooks["_stdinRef"].(func(bool)); ok {
899
- fn(flowing)
900
- }
901
1078
  }`;
902
1079
  const fileStreamHelper = `// fs.createReadStream / createWriteStream
903
1080
  func TnCreateReadStream(path string, options ...interface{}) *TnStream {
@@ -910,16 +1087,16 @@ func TnCreateReadStream(path string, options ...interface{}) *TnStream {
910
1087
  if enc, ok := opts.(string); ok {
911
1088
  s.setEncoding(enc)
912
1089
  } else {
913
- if enc, ok := TnGet(opts, "encoding").(string); ok && enc != "" {
1090
+ if enc, ok := TnGetValue(opts, "encoding").(string); ok && enc != "" {
914
1091
  s.setEncoding(enc)
915
1092
  }
916
- if v, ok := TnGet(opts, "start").(float64); ok {
1093
+ if v, ok := TnGetValue(opts, "start").(float64); ok {
917
1094
  start = int64(v)
918
1095
  }
919
- if v, ok := TnGet(opts, "end").(float64); ok {
1096
+ if v, ok := TnGetValue(opts, "end").(float64); ok {
920
1097
  end = int64(v)
921
1098
  }
922
- if v, ok := TnGet(opts, "highWaterMark").(float64); ok && v > 0 {
1099
+ if v, ok := TnGetValue(opts, "highWaterMark").(float64); ok && v > 0 {
923
1100
  chunkSize = int(v)
924
1101
  }
925
1102
  }
@@ -980,10 +1157,10 @@ func TnCreateWriteStream(path string, options ...interface{}) *TnStream {
980
1157
  s.path = path
981
1158
  flags := "w"
982
1159
  if len(options) > 0 && options[0] != nil {
983
- if f, ok := TnGet(options[0], "flags").(string); ok && f != "" {
1160
+ if f, ok := TnGetValue(options[0], "flags").(string); ok && f != "" {
984
1161
  flags = f
985
1162
  }
986
- if enc, ok := TnGet(options[0], "encoding").(string); ok && enc != "" {
1163
+ if enc, ok := TnGetValue(options[0], "encoding").(string); ok && enc != "" {
987
1164
  s.decodeStrings = true
988
1165
  }
989
1166
  }
@@ -1039,7 +1216,7 @@ func TnZlibStream(format string, decompress bool, options ...interface{}) *TnStr
1039
1216
  }`;
1040
1217
  // Go parameter types of option / override methods
1041
1218
  // Method and getter specs of TnStream, shared with classes that embed it (net.Socket, http messages)
1042
- export const STREAM_METHODS = 'on=*TnEventEmitter; once=*TnEventEmitter; addListener=*TnEventEmitter; off=*TnEventEmitter; removeListener=*TnEventEmitter; removeAllListeners=*TnEventEmitter; emit=bool; listenerCount=float64; push=bool; unshift=; read=interface{}; resume=*TnStream; pause=*TnStream; isPaused=bool; setEncoding=*TnStream; destroy=*TnStream; write=bool; end=*TnStream; cork=; uncork=; setDefaultEncoding=*TnStream';
1219
+ export const STREAM_METHODS = 'on=*TnStream; once=*TnStream; addListener=*TnEventEmitter; off=*TnEventEmitter; removeListener=*TnEventEmitter; removeAllListeners=*TnEventEmitter; emit=bool; listenerCount=float64; compose=*TnStream; push=bool; unshift=; read=interface{}; resume=*TnStream; pause=*TnStream; isPaused=bool; setEncoding=*TnStream; destroy=*TnStream; write=bool; end=*TnStream; cork=; uncork=; setDefaultEncoding=*TnStream';
1043
1220
  export const STREAM_GETTERS = 'readable=bool; writable=bool; readableEnded=bool; writableEnded=bool; writableFinished=bool; destroyed=bool; closed=bool; errored=interface{}; readableLength=float64; writableLength=float64; readableObjectMode=bool; writableObjectMode=bool; readableFlowing=interface{}; bytesRead=float64; bytesWritten=float64; path=string';
1044
1221
  const STREAM_HOOK_PARAMS = {
1045
1222
  read: ['float64'],
@@ -1054,7 +1231,7 @@ export function registerStream(r) {
1054
1231
  defineModule(r, mod);
1055
1232
  r.namespaces['stream.promises'] = 'stream/promises';
1056
1233
  r.namespaces['stream.default'] = 'stream.Stream';
1057
- defineHelper(r, 'nodeStream', ['encoding/json', 'os', 'reflect'], ['nodeEvents', 'buffer', 'stringDecoder', 'eventLoop', 'promise', 'dynamic', 'nodeInvoke', 'stdinReader'], streamHelper);
1234
+ defineHelper(r, 'nodeStream', ['encoding/json', 'os', 'reflect', 'unicode/utf8'], ['nodeEvents', 'buffer', 'stringDecoder', 'eventLoop', 'promise', 'dynamic', 'nodeInvoke', 'stdinReader'], streamHelper);
1058
1235
  defineHelper(r, 'stdinReader', ['bufio', 'os'], [], `// One buffered reader shared by process.stdin, readline and question()
1059
1236
  var tnStdinBuffered *bufio.Reader
1060
1237
 
@@ -1071,6 +1248,9 @@ func tnStdin() *bufio.Reader {
1071
1248
  r.classes[`stream.${name}`] = cls;
1072
1249
  for (const name of ['ReadStream', 'WriteStream'])
1073
1250
  r.classes[`fs.${name}`] = cls;
1251
+ // the global stream interfaces of Node's typings (process.stdin is one)
1252
+ for (const name of ['ReadableStream', 'WritableStream', 'ReadWriteStream'])
1253
+ r.classes[`NodeJS.${name}`] = cls;
1074
1254
  r.classes['stream.default'] = cls;
1075
1255
  r.iterators['*TnStream'] = 'tnIterator';
1076
1256
  cls.methods['tnIterator'] = 'chan interface{}';
@@ -1109,10 +1289,17 @@ func tnStdin() *bufio.Reader {
1109
1289
  r.hookParams[`_${hook}`] = STREAM_HOOK_PARAMS[hook];
1110
1290
  r.methods['stream.Duplex#pipe'] = (c) => {
1111
1291
  use();
1292
+ useHashStream(r, c.types);
1112
1293
  return `TnPipe(${c.recv}, ${c.args.join(', ')})`;
1113
1294
  };
1114
1295
  r.typers['stream.Duplex#pipe'] = (c) => c.types[0] || '*TnStream';
1115
1296
  r.paramTypers['stream.Duplex#on'] = (nodes) => streamListenerTypes(nodes);
1297
+ for (const method of ['on', 'once']) {
1298
+ r.methods[`stream.Duplex#${method}`] = (c) => {
1299
+ use();
1300
+ return `${c.recv}.${method}Chain(${c.args.join(', ')})`;
1301
+ };
1302
+ }
1116
1303
  r.paramTypers['stream.Duplex#once'] = r.paramTypers['stream.Duplex#on'];
1117
1304
  r.functions['stream.Readable.from'] = (call) => {
1118
1305
  use();
@@ -1121,6 +1308,7 @@ func tnStdin() *bufio.Reader {
1121
1308
  r.results['stream.Readable.from'] = '*TnStream';
1122
1309
  const pipeline = (call) => {
1123
1310
  use();
1311
+ useHashStream(r, call.types);
1124
1312
  const last = call.nodes.length - 1;
1125
1313
  const hasCallback = last >= 0 && (call.nodes[last].kind === 'ArrowFunction' || call.nodes[last].kind === 'FunctionExpression');
1126
1314
  const streams = hasCallback ? call.args.slice(0, last) : call.args;
@@ -1136,6 +1324,7 @@ func tnStdin() *bufio.Reader {
1136
1324
  r.paramTypers['stream.finished'] = (_n) => ['interface{}'];
1137
1325
  r.functions['stream/promises.pipeline'] = (call) => {
1138
1326
  use();
1327
+ useHashStream(r, call.types);
1139
1328
  return `TnPipelinePromise(${call.args.join(', ')})`;
1140
1329
  };
1141
1330
  r.functions['stream/promises.finished'] = (call) => {
@@ -1202,8 +1391,10 @@ func tnStdin() *bufio.Reader {
1202
1391
  for (const method of ['on', 'once', 'resume', 'pause', 'setEncoding', 'pipe', 'read', 'destroy']) {
1203
1392
  r.functions[`process.stdin.${method}`] = (call) => {
1204
1393
  use();
1205
- if (method === 'pipe')
1394
+ if (method === 'pipe') {
1395
+ useHashStream(r, call.types);
1206
1396
  return `TnPipe(TnStdin(), ${call.args.join(', ')})`;
1397
+ }
1207
1398
  const refCall = method === 'destroy' ? '; TnStdin().stdinRef(false)' : '; TnStdin().stdinRef(TnStdin().flowing == 1)';
1208
1399
  return `func() *TnStream { TnStdin().${method}(${call.args.join(', ')})${refCall}; return TnStdin() }()`;
1209
1400
  };
@@ -1212,6 +1403,11 @@ func tnStdin() *bufio.Reader {
1212
1403
  r.paramTypers['process.stdin.on'] = (nodes) => streamListenerTypes(nodes);
1213
1404
  r.paramTypers['process.stdin.once'] = r.paramTypers['process.stdin.on'];
1214
1405
  }
1406
+ // Pulls in crypto's hashStream when a Hash is passed to pipe() / pipeline()
1407
+ function useHashStream(r, types) {
1408
+ if (types.indexOf('*TnHash') >= 0)
1409
+ r.helper('hashStream');
1410
+ }
1215
1411
  // Listener parameters of stream events
1216
1412
  export function streamListenerTypes(nodes) {
1217
1413
  const event = nodes[0];
@@ -1220,5 +1416,7 @@ export function streamListenerTypes(nodes) {
1220
1416
  return ['interface{}'];
1221
1417
  if (name === 'error')
1222
1418
  return ['interface{}'];
1419
+ if (name === 'keypress')
1420
+ return ['string', '*TnKey'];
1223
1421
  return [];
1224
1422
  }
@@ -255,7 +255,7 @@ func TnNewReadableStream(args ...interface{}) *TnReadableStream {
255
255
  s.startWith(nil)
256
256
  return s
257
257
  }
258
- if t, _ := TnGet(source, "type").(string); t == "bytes" {
258
+ if t, _ := TnGetValue(source, "type").(string); t == "bytes" {
259
259
  s.strategy.bytes = true
260
260
  if len(rest) == 0 {
261
261
  s.strategy.hwm = 0
@@ -823,7 +823,7 @@ func (s *TnReadableStream) pipeTo(dest *TnWritableStream, options ...interface{}
823
823
  ch := make(chan struct{})
824
824
  opts := tnWebArg(options)
825
825
  flag := func(name string) bool {
826
- b, _ := TnGet(opts, name).(bool)
826
+ b, _ := TnGetValue(opts, name).(bool)
827
827
  return b
828
828
  }
829
829
  preventClose, preventAbort, preventCancel := flag("preventClose"), flag("preventAbort"), flag("preventCancel")
@@ -847,7 +847,7 @@ func (s *TnReadableStream) pipeTo(dest *TnWritableStream, options ...interface{}
847
847
  }
848
848
  close(ch)
849
849
  }
850
- if signal, ok := TnGet(opts, "signal").(*TnAbortSignal); ok && signal != nil {
850
+ if signal, ok := TnGetValue(opts, "signal").(*TnAbortSignal); ok && signal != nil {
851
851
  onAbort := func() {
852
852
  reason := tnWebAbortReason(signal)
853
853
  if !preventAbort {
@@ -932,8 +932,8 @@ func (s *TnReadableStream) pipeThrough(transform interface{}, options ...interfa
932
932
  if t, ok := transform.(*TnTransformStream); ok {
933
933
  writable, readable = t.writable, t.readable
934
934
  } else {
935
- writable, _ = TnGet(transform, "writable").(*TnWritableStream)
936
- readable, _ = TnGet(transform, "readable").(*TnReadableStream)
935
+ writable, _ = TnGetValue(transform, "writable").(*TnWritableStream)
936
+ readable, _ = TnGetValue(transform, "readable").(*TnReadableStream)
937
937
  }
938
938
  if writable == nil || readable == nil {
939
939
  panic(tnWebTypeError("ERR_INVALID_ARG_TYPE", "The \\"transform\\" argument must have readable and writable streams"))
@@ -1533,8 +1533,8 @@ func TnNewTextDecoderStream(args ...interface{}) *TnTransformStream {
1533
1533
  ts.encoding = "utf-8"
1534
1534
  }
1535
1535
  if len(args) > 1 && args[1] != nil {
1536
- ts.fatal, _ = TnGet(args[1], "fatal").(bool)
1537
- ts.ignoreBOM, _ = TnGet(args[1], "ignoreBOM").(bool)
1536
+ ts.fatal, _ = TnGetValue(args[1], "fatal").(bool)
1537
+ ts.ignoreBOM, _ = TnGetValue(args[1], "ignoreBOM").(bool)
1538
1538
  }
1539
1539
  return ts
1540
1540
  }
@@ -1747,8 +1747,8 @@ func TnDuplexFromWeb(pair interface{}, options ...interface{}) *TnStream {
1747
1747
  if t, ok := pair.(*TnTransformStream); ok {
1748
1748
  readable, writable = t.readable, t.writable
1749
1749
  } else {
1750
- readable, _ = TnGet(pair, "readable").(*TnReadableStream)
1751
- writable, _ = TnGet(pair, "writable").(*TnWritableStream)
1750
+ readable, _ = TnGetValue(pair, "readable").(*TnReadableStream)
1751
+ writable, _ = TnGetValue(pair, "writable").(*TnWritableStream)
1752
1752
  }
1753
1753
  r := TnReadableFromWeb(readable, options...)
1754
1754
  w := TnWritableFromWeb(writable, options...)
@@ -1855,7 +1855,7 @@ func tnBlobParts(parts interface{}) []byte {
1855
1855
  }
1856
1856
 
1857
1857
  func tnBlobType(options interface{}) string {
1858
- t, _ := TnGet(options, "type").(string)
1858
+ t, _ := TnGetValue(options, "type").(string)
1859
1859
  for _, c := range t {
1860
1860
  if c < 0x20 || c > 0x7e {
1861
1861
  return ""
@@ -303,9 +303,9 @@ func (o *TnPerformanceObserver) observe(options interface{}) {
303
303
  o.types[TnJSString(rv.Index(i).Interface())] = true
304
304
  }
305
305
  }
306
- if t, ok := TnGet(options, "type").(string); ok {
306
+ if t, ok := TnGetValue(options, "type").(string); ok {
307
307
  o.types[t] = true
308
- if buffered, _ := TnGet(options, "buffered").(bool); buffered {
308
+ if buffered, _ := TnGetValue(options, "buffered").(bool); buffered {
309
309
  for _, e := range TnPerfEntries("", t) {
310
310
  o.queue(e)
311
311
  }
@@ -516,7 +516,7 @@ func (h *TnHistogram) reset() {
516
516
  func TnMonitorEventLoopDelay(options ...interface{}) *TnHistogram {
517
517
  h := &TnHistogram{interval: 10}
518
518
  if len(options) > 0 && options[0] != nil {
519
- if n, ok := TnGet(options[0], "resolution").(float64); ok && n > 0 {
519
+ if n, ok := TnGetValue(options[0], "resolution").(float64); ok && n > 0 {
520
520
  h.interval = n
521
521
  }
522
522
  }