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
@@ -0,0 +1,407 @@
1
+ // node:fs watch / watchFile / unwatchFile and fs/promises watch.
2
+ //
3
+ // fs.watch runs on the vendored github.com/fsnotify/fsnotify (inotify, kqueue,
4
+ // ReadDirectoryChangesW; linked only into programs that call fs.watch): a
5
+ // goroutine reads its events and emits 'change' on the event loop. Recursive
6
+ // watches add every subdirectory, and directories created later.
7
+ // fs.watchFile polls stat() like Node does.
8
+ import { defineClass, defineHelper, setResults } from './registry.js';
9
+ const fsWatchHelper = `// node:fs watch (fsnotify)
10
+ type TnFSWatcher struct {
11
+ TnEventEmitter
12
+ asyncCtx TnAsyncContext
13
+ watcher *fsnotify.Watcher
14
+ root string
15
+ isDir bool
16
+ recursive bool
17
+ closed bool
18
+ refd bool
19
+ // fs/promises watch: events go to a channel instead of 'change'
20
+ events chan *TnFsWatchEvent
21
+ }
22
+
23
+ // One change, as fs/promises watch yields it
24
+ type TnFsWatchEvent struct {
25
+ eventType string
26
+ filename string
27
+ }
28
+
29
+ func tnFsWatchIsFunc(v interface{}) bool {
30
+ return v != nil && reflect.ValueOf(v).Kind() == reflect.Func && !reflect.ValueOf(v).IsNil()
31
+ }
32
+
33
+ // rename for entries appearing / disappearing, change for writes and
34
+ // attribute changes (Node's two event types)
35
+ func tnFsWatchType(op fsnotify.Op) string {
36
+ if op.Has(fsnotify.Create) || op.Has(fsnotify.Remove) || op.Has(fsnotify.Rename) {
37
+ return "rename"
38
+ }
39
+ return "change"
40
+ }
41
+
42
+ // options: an encoding string or { persistent, recursive, encoding, signal }
43
+ // events: fs/promises watch's channel (nil for fs.watch)
44
+ func tnFsWatchStart(path string, options interface{}, events chan *TnFsWatchEvent) *TnFSWatcher {
45
+ w := &TnFSWatcher{asyncCtx: TnAsyncSnapshot(), root: filepath.Clean(path), events: events}
46
+ persistent := true
47
+ if _, isString := options.(string); options != nil && !isString {
48
+ if p, ok := TnGet(options, "persistent").(bool); ok {
49
+ persistent = p
50
+ }
51
+ w.recursive, _ = TnGet(options, "recursive").(bool)
52
+ }
53
+ info, err := os.Stat(path)
54
+ if err != nil {
55
+ panic(TnSysError(err, "watch", path))
56
+ }
57
+ w.isDir = info.IsDir()
58
+ watcher, err := fsnotify.NewWatcher()
59
+ if err != nil {
60
+ panic(TnSysError(err, "watch", path))
61
+ }
62
+ w.watcher = watcher
63
+ if err := w.add(w.root); err != nil {
64
+ watcher.Close()
65
+ panic(TnSysError(err, "watch", path))
66
+ }
67
+ if persistent {
68
+ w.refd = true
69
+ TnLoopRef()
70
+ }
71
+ if options != nil {
72
+ if signal, ok := TnGet(options, "signal").(*TnAbortSignal); ok && signal != nil {
73
+ if signal.aborted {
74
+ TnLoopPost(func() { w.close() })
75
+ } else {
76
+ signal.addEventListener("abort", func(ev *TnEvent) { w.close() })
77
+ }
78
+ }
79
+ }
80
+ return w
81
+ }
82
+
83
+ // Adds a directory (and, for recursive watches, its subdirectories)
84
+ func (w *TnFSWatcher) add(dir string) error {
85
+ if !w.recursive || !w.isDir {
86
+ return w.watcher.Add(dir)
87
+ }
88
+ return filepath.WalkDir(dir, func(p string, d fs.DirEntry, err error) error {
89
+ if err != nil {
90
+ if p == dir {
91
+ return err
92
+ }
93
+ return nil
94
+ }
95
+ if d.IsDir() {
96
+ return w.watcher.Add(p)
97
+ }
98
+ return nil
99
+ })
100
+ }
101
+
102
+ // The filename Node reports: relative to a watched directory, the file's own
103
+ // name for a watched file
104
+ func (w *TnFSWatcher) filename(name string) string {
105
+ if !w.isDir {
106
+ return filepath.Base(w.root)
107
+ }
108
+ if rel, err := filepath.Rel(w.root, name); err == nil {
109
+ return rel
110
+ }
111
+ return filepath.Base(name)
112
+ }
113
+
114
+ // Reads fsnotify's channels until close(); handle runs on the loop
115
+ func (w *TnFSWatcher) run(handle func(eventType string, filename string), fail func(err error)) {
116
+ go func() {
117
+ for {
118
+ select {
119
+ case ev, ok := <-w.watcher.Events:
120
+ if !ok {
121
+ return
122
+ }
123
+ if w.recursive && ev.Op.Has(fsnotify.Create) {
124
+ if info, err := os.Stat(ev.Name); err == nil && info.IsDir() {
125
+ w.add(ev.Name)
126
+ }
127
+ }
128
+ eventType := tnFsWatchType(ev.Op)
129
+ filename := w.filename(ev.Name)
130
+ TnLoopPost(func() {
131
+ if !w.closed {
132
+ TnAsyncRun(w.asyncCtx, func() { handle(eventType, filename) })
133
+ }
134
+ })
135
+ case err, ok := <-w.watcher.Errors:
136
+ if !ok {
137
+ return
138
+ }
139
+ failure := err
140
+ TnLoopPost(func() {
141
+ if !w.closed {
142
+ TnAsyncRun(w.asyncCtx, func() { fail(failure) })
143
+ }
144
+ })
145
+ }
146
+ }
147
+ }()
148
+ }
149
+
150
+ // fs.watch(filename[, options][, listener])
151
+ func TnFsWatch(path string, rest ...interface{}) *TnFSWatcher {
152
+ var options interface{}
153
+ for _, a := range rest {
154
+ if tnFsWatchIsFunc(a) {
155
+ continue
156
+ }
157
+ options = a
158
+ }
159
+ w := tnFsWatchStart(path, options, nil)
160
+ for _, a := range rest {
161
+ if tnFsWatchIsFunc(a) {
162
+ w.on("change", a)
163
+ }
164
+ }
165
+ w.run(func(eventType string, filename string) {
166
+ w.emit("change", eventType, filename)
167
+ }, func(err error) {
168
+ w.emit("error", TnSysError(err, "watch", path))
169
+ })
170
+ return w
171
+ }
172
+
173
+ func (w *TnFSWatcher) close() {
174
+ w.stop(tnAbortError("The operation was aborted", "AbortError"))
175
+ }
176
+
177
+ // Stops watching; fs/promises watch's iterator rejects with reason
178
+ func (w *TnFSWatcher) stop(reason interface{}) {
179
+ if w.closed {
180
+ return
181
+ }
182
+ w.closed = true
183
+ w.watcher.Close()
184
+ if w.refd {
185
+ w.refd = false
186
+ TnLoopUnref()
187
+ }
188
+ if w.events != nil {
189
+ TnReject(w.events, reason)
190
+ return
191
+ }
192
+ TnLoopPost(func() { w.emit("close") })
193
+ }
194
+
195
+ func (w *TnFSWatcher) ref() *TnFSWatcher {
196
+ if !w.refd && !w.closed {
197
+ w.refd = true
198
+ TnLoopRef()
199
+ }
200
+ return w
201
+ }
202
+
203
+ func (w *TnFSWatcher) unref() *TnFSWatcher {
204
+ if w.refd {
205
+ w.refd = false
206
+ TnLoopUnref()
207
+ }
208
+ return w
209
+ }
210
+
211
+ // fs/promises watch(filename[, options]): an async iterator of
212
+ // { eventType, filename }; aborting the signal rejects it with an AbortError
213
+ func TnFsWatchIterator(path string, options ...interface{}) chan *TnFsWatchEvent {
214
+ var opts interface{}
215
+ if len(options) > 0 {
216
+ opts = options[0]
217
+ }
218
+ ch := make(chan *TnFsWatchEvent, 1024)
219
+ w := tnFsWatchStart(path, opts, ch)
220
+ // The consumer's await keeps the program alive
221
+ w.unref()
222
+ w.run(func(eventType string, filename string) {
223
+ select {
224
+ case ch <- &TnFsWatchEvent{eventType: eventType, filename: filename}:
225
+ default:
226
+ }
227
+ }, func(err error) {
228
+ w.stop(TnSysError(err, "watch", path))
229
+ })
230
+ return ch
231
+ }
232
+
233
+ // fs.watchFile: polls stat() every interval (Node's default 5007 ms) and calls
234
+ // listener(curr, prev) when the file changes; a missing file has zeroed Stats
235
+ type TnStatWatcher struct {
236
+ TnEventEmitter
237
+ asyncCtx TnAsyncContext
238
+ path string
239
+ stop chan struct{}
240
+ stopped bool
241
+ refd bool
242
+ }
243
+
244
+ var tnStatWatchers = map[string]*TnStatWatcher{}
245
+
246
+ func tnStatOrZero(path string) *TnStats {
247
+ info, err := os.Stat(path)
248
+ if err != nil {
249
+ zero := time.UnixMilli(0)
250
+ return &TnStats{atime: zero, mtime: zero, ctime: zero, birthtime: zero}
251
+ }
252
+ return tnStatsOf(info)
253
+ }
254
+
255
+ func tnStatsChanged(a *TnStats, b *TnStats) bool {
256
+ return a.mtimeMs != b.mtimeMs || a.size != b.size || a.mode != b.mode
257
+ }
258
+
259
+ // fs.watchFile(filename[, options], listener)
260
+ func TnWatchFile(path string, rest ...interface{}) *TnStatWatcher {
261
+ interval := 5007.0
262
+ persistent := true
263
+ var listener interface{}
264
+ for _, a := range rest {
265
+ if tnFsWatchIsFunc(a) {
266
+ listener = a
267
+ continue
268
+ }
269
+ if a != nil {
270
+ if n, ok := TnGet(a, "interval").(float64); ok && n > 0 {
271
+ interval = n
272
+ }
273
+ if p, ok := TnGet(a, "persistent").(bool); ok {
274
+ persistent = p
275
+ }
276
+ }
277
+ }
278
+ key := filepath.Clean(path)
279
+ if existing, ok := tnStatWatchers[key]; ok {
280
+ if listener != nil {
281
+ existing.on("change", listener)
282
+ }
283
+ return existing
284
+ }
285
+ s := &TnStatWatcher{asyncCtx: TnAsyncSnapshot(), path: path, stop: make(chan struct{})}
286
+ tnStatWatchers[key] = s
287
+ if listener != nil {
288
+ s.on("change", listener)
289
+ }
290
+ if persistent {
291
+ s.refd = true
292
+ TnLoopRef()
293
+ }
294
+ prev := tnStatOrZero(path)
295
+ go func() {
296
+ ticker := time.NewTicker(max(time.Duration(interval*float64(time.Millisecond)), time.Millisecond))
297
+ defer ticker.Stop()
298
+ for {
299
+ select {
300
+ case <-s.stop:
301
+ return
302
+ case <-ticker.C:
303
+ }
304
+ curr := tnStatOrZero(path)
305
+ if !tnStatsChanged(curr, prev) {
306
+ continue
307
+ }
308
+ before := prev
309
+ prev = curr
310
+ TnLoopPost(func() {
311
+ if !s.stopped {
312
+ TnAsyncRun(s.asyncCtx, func() { s.emit("change", curr, before) })
313
+ }
314
+ })
315
+ }
316
+ }()
317
+ return s
318
+ }
319
+
320
+ func (s *TnStatWatcher) halt() {
321
+ if s.stopped {
322
+ return
323
+ }
324
+ s.stopped = true
325
+ close(s.stop)
326
+ s.unref()
327
+ delete(tnStatWatchers, filepath.Clean(s.path))
328
+ }
329
+
330
+ // fs.unwatchFile(filename[, listener]): removes one listener, or all of them;
331
+ // polling stops once none is left
332
+ func TnUnwatchFile(path string, listener ...interface{}) {
333
+ s, ok := tnStatWatchers[filepath.Clean(path)]
334
+ if !ok {
335
+ return
336
+ }
337
+ if len(listener) > 0 && listener[0] != nil {
338
+ s.removeListener("change", listener[0])
339
+ if s.listenerCount("change") > 0 {
340
+ return
341
+ }
342
+ }
343
+ s.halt()
344
+ }
345
+
346
+ func (s *TnStatWatcher) ref() *TnStatWatcher {
347
+ if !s.refd && !s.stopped {
348
+ s.refd = true
349
+ TnLoopRef()
350
+ }
351
+ return s
352
+ }
353
+
354
+ func (s *TnStatWatcher) unref() *TnStatWatcher {
355
+ if s.refd {
356
+ s.refd = false
357
+ TnLoopUnref()
358
+ }
359
+ return s
360
+ }`;
361
+ const EMITTER_METHODS = 'on=*TnEventEmitter; once=*TnEventEmitter; addListener=*TnEventEmitter; off=*TnEventEmitter; removeListener=*TnEventEmitter; removeAllListeners=*TnEventEmitter; emit=bool; listenerCount=float64';
362
+ export function registerFsWatch(r) {
363
+ defineHelper(r, 'nodeFsWatch', ['github.com/fsnotify/fsnotify', 'io/fs', 'os', 'path/filepath', 'reflect', 'time'], ['nodeFs', 'nodeEvents', 'eventLoop', 'promise', 'dynamic', 'nodeInvoke'], fsWatchHelper);
364
+ const use = () => r.helper('nodeFsWatch');
365
+ defineClass(r, 'fs.FSWatcher', '*TnFSWatcher', 'class', `${EMITTER_METHODS}; close=; ref=*TnFSWatcher; unref=*TnFSWatcher`, '', '', '');
366
+ defineClass(r, 'fs.StatWatcher', '*TnStatWatcher', 'class', `${EMITTER_METHODS}; ref=*TnStatWatcher; unref=*TnStatWatcher`, '', '', '');
367
+ defineClass(r, 'fs/promises.FileChangeInfo', '*TnFsWatchEvent', 'class', '', 'eventType=string; filename=string', '', '');
368
+ // Listener parameters: change(eventType, filename) / error(err) / watchFile's (curr, prev)
369
+ const watchListener = (nodes) => {
370
+ const event = nodes[0];
371
+ const name = event && event.kind === 'StringLiteral' ? String(event.text) : '';
372
+ if (name === 'change')
373
+ return ['string', 'string'];
374
+ if (name === 'error')
375
+ return ['interface{}'];
376
+ return [];
377
+ };
378
+ const statListener = (nodes) => {
379
+ const event = nodes[0];
380
+ const name = event && event.kind === 'StringLiteral' ? String(event.text) : '';
381
+ return name === 'change' ? ['*TnStats', '*TnStats'] : [];
382
+ };
383
+ for (const method of ['on', 'once', 'addListener']) {
384
+ r.paramTypers[`fs.FSWatcher#${method}`] = watchListener;
385
+ r.paramTypers[`fs.StatWatcher#${method}`] = statListener;
386
+ }
387
+ r.functions['fs.watch'] = (c) => {
388
+ use();
389
+ return `TnFsWatch(${c.args.join(', ')})`;
390
+ };
391
+ r.paramTypers['fs.watch'] = (_n) => ['string', 'string'];
392
+ r.functions['fs.watchFile'] = (c) => {
393
+ use();
394
+ return `TnWatchFile(${c.args.join(', ')})`;
395
+ };
396
+ r.paramTypers['fs.watchFile'] = (_n) => ['*TnStats', '*TnStats'];
397
+ r.functions['fs.unwatchFile'] = (c) => {
398
+ use();
399
+ return `TnUnwatchFile(${c.args.join(', ')})`;
400
+ };
401
+ setResults(r, 'fs', 'watch=*TnFSWatcher; watchFile=*TnStatWatcher; unwatchFile=');
402
+ r.functions['fs/promises.watch'] = (c) => {
403
+ use();
404
+ return `TnFsWatchIterator(${c.args.join(', ')})`;
405
+ };
406
+ r.results['fs/promises.watch'] = 'chan *TnFsWatchEvent';
407
+ }
package/bin/node/http.js CHANGED
@@ -274,28 +274,43 @@ func tnNewIncoming() *TnIncomingMessage {
274
274
  return m
275
275
  }
276
276
 
277
- // Streams a body into the message on a goroutine; done runs (off the loop) at the end
278
- func tnPumpBody(body io.ReadCloser, m *TnIncomingMessage, done func()) {
277
+ // Read buffers of the body pumps (chunks are copied out of them)
278
+ var tnBodyBufs = sync.Pool{New: func() any { b := make([]byte, 32768); return &b }}
279
+
280
+ // Streams a body into the message on a goroutine; atEOF (optional) runs on the
281
+ // loop just before the end of the stream, done (off the loop) at the end
282
+ func tnPumpBody(body io.ReadCloser, m *TnIncomingMessage, atEOF func(), done func()) {
279
283
  go func() {
280
284
  defer done()
281
285
  defer body.Close()
282
- buf := make([]byte, 32768)
286
+ bp := tnBodyBufs.Get().(*[]byte)
287
+ defer tnBodyBufs.Put(bp)
283
288
  for {
284
- n, err := body.Read(buf)
289
+ n, err := body.Read(*bp)
290
+ var chunk []byte
285
291
  if n > 0 {
286
- chunk := append([]byte{}, buf[:n]...)
287
- TnLoopPost(func() {
288
- if !m.destroyed {
289
- m.push(chunk)
290
- }
291
- })
292
+ chunk = append([]byte{}, (*bp)[:n]...)
293
+ if err == nil {
294
+ tnLoopRunOrPost(func() {
295
+ if !m.destroyed {
296
+ m.push(chunk)
297
+ }
298
+ })
299
+ }
292
300
  }
293
301
  if err != nil {
294
- TnLoopPost(func() {
302
+ // The last chunk usually comes with the EOF: one task for both
303
+ tnLoopRunOrPost(func() {
295
304
  if m.destroyed {
296
305
  return
297
306
  }
307
+ if chunk != nil {
308
+ m.push(chunk)
309
+ }
298
310
  if err == io.EOF {
311
+ if atEOF != nil {
312
+ atEOF()
313
+ }
299
314
  m.complete = true
300
315
  m.push(nil)
301
316
  return
@@ -326,10 +341,13 @@ func tnInfoSocket(local net.Addr, remote string) *TnSocket {
326
341
 
327
342
  // One operation of a response, applied by the handler goroutine
328
343
  type tnResOp struct {
329
- status int
330
- header http.Header
344
+ status int
345
+ header http.Header
346
+ trailer http.Header
331
347
  data []byte
332
348
  end bool
349
+ // Header ops: send the headers now (flushHeaders) instead of with the body
350
+ flush bool
333
351
  done func(err error)
334
352
  }
335
353
 
@@ -346,16 +364,25 @@ type TnServerResponse struct {
346
364
  mu sync.Mutex
347
365
  ops []tnResOp
348
366
  signal chan struct{}
367
+ trailer http.Header
368
+ // end(chunk) is writing its chunk; endPosted: that write also ended the response
369
+ lastWrite bool
370
+ endPosted bool
349
371
  }
350
372
 
351
373
  func (res *TnServerResponse) Get_headersSent() bool { return res.headersSent }
352
374
  func (res *TnServerResponse) Get_req() *TnIncomingMessage { return res.req }
353
375
  func (res *TnServerResponse) Get_socket() *TnSocket { return res.req.socket }
354
376
 
355
- func (res *TnServerResponse) post(op tnResOp) {
377
+ // Queues an op without waking the handler goroutine (it goes out with the next post)
378
+ func (res *TnServerResponse) queue(op tnResOp) {
356
379
  res.mu.Lock()
357
380
  res.ops = append(res.ops, op)
358
381
  res.mu.Unlock()
382
+ }
383
+
384
+ func (res *TnServerResponse) post(op tnResOp) {
385
+ res.queue(op)
359
386
  select {
360
387
  case res.signal <- struct{}{}:
361
388
  default:
@@ -401,6 +428,19 @@ func (res *TnServerResponse) appendHeader(name string, value interface{}) *TnSer
401
428
  return res
402
429
  }
403
430
 
431
+ // addTrailers(headers): sent after a chunked body (Node, too, sends them only
432
+ // without a Content-Length)
433
+ func (res *TnServerResponse) addTrailers(headers interface{}) {
434
+ if res.trailer == nil {
435
+ res.trailer = http.Header{}
436
+ }
437
+ for _, e := range tnEntries(headers) {
438
+ for _, v := range tnHeaderStrings(e[1]) {
439
+ res.trailer.Add(fmt.Sprint(e[0]), v)
440
+ }
441
+ }
442
+ }
443
+
404
444
  func (res *TnServerResponse) getHeader(name string) interface{} { return res.store.get(name) }
405
445
  func (res *TnServerResponse) getHeaders() *TnMap[string, interface{}] { return res.store.record() }
406
446
  func (res *TnServerResponse) getHeaderNames() []string { return res.store.keys() }
@@ -422,13 +462,16 @@ func (res *TnServerResponse) writeHead(status float64, args ...interface{}) *TnS
422
462
  }
423
463
  }
424
464
  }
425
- res.post(res.headerOp())
465
+ // As in Node, the headers go out with the first write or end()
466
+ res.queue(res.headerOp())
426
467
  return res
427
468
  }
428
469
 
429
470
  func (res *TnServerResponse) flushHeaders() {
430
471
  if !res.headersSent {
431
- res.post(res.headerOp())
472
+ op := res.headerOp()
473
+ op.flush = true
474
+ res.post(op)
432
475
  }
433
476
  }
434
477
 
@@ -445,7 +488,11 @@ func (res *TnServerResponse) end(args ...interface{}) *TnServerResponse {
445
488
  if len(args) > 1 {
446
489
  enc, _ = args[1].(string)
447
490
  }
448
- size = len(TnBufferFromString(d, enc))
491
+ if tnEncoding(enc) == "utf8" {
492
+ size = len(d)
493
+ } else {
494
+ size = len(TnBufferFromString(d, enc))
495
+ }
449
496
  case []byte:
450
497
  size = len(d)
451
498
  }
@@ -454,6 +501,7 @@ func (res *TnServerResponse) end(args ...interface{}) *TnServerResponse {
454
501
  res.store.set("Content-Length", strconv.Itoa(size))
455
502
  }
456
503
  }
504
+ res.lastWrite = true
457
505
  res.TnStream.end(args...)
458
506
  return res
459
507
  }
@@ -469,7 +517,7 @@ func tnNewServerResponse(req *TnIncomingMessage) *TnServerResponse {
469
517
  res.tnSelf = res
470
518
  res.sink = func(chunk interface{}, done func(err interface{})) {
471
519
  if !res.headersSent {
472
- res.post(res.headerOp())
520
+ res.queue(res.headerOp())
473
521
  }
474
522
  var data []byte
475
523
  switch c := chunk.(type) {
@@ -479,7 +527,13 @@ func tnNewServerResponse(req *TnIncomingMessage) *TnServerResponse {
479
527
  data = []byte(c)
480
528
  }
481
529
  res.wroteData = true
482
- res.post(tnResOp{data: data, done: func(err error) {
530
+ // end(chunk)'s last chunk also ends the response: the handler goroutine
531
+ // finishes without another round trip through the loop
532
+ last := res.lastWrite && len(res.writeQueue) == 0
533
+ if last {
534
+ res.endPosted = true
535
+ }
536
+ res.post(tnResOp{data: data, end: last, trailer: res.trailer, done: func(err error) {
483
537
  TnLoopPost(func() {
484
538
  if err != nil {
485
539
  done(TnNetError(err, "write", "", -1))
@@ -490,10 +544,14 @@ func tnNewServerResponse(req *TnIncomingMessage) *TnServerResponse {
490
544
  }})
491
545
  }
492
546
  res.sinkFinal = func(done func(err interface{})) {
547
+ if res.endPosted {
548
+ done(nil)
549
+ return
550
+ }
493
551
  if !res.headersSent {
494
- res.post(res.headerOp())
552
+ res.queue(res.headerOp())
495
553
  }
496
- res.post(tnResOp{end: true, done: func(err error) {
554
+ res.post(tnResOp{end: true, trailer: res.trailer, done: func(err error) {
497
555
  TnLoopPost(func() { done(nil) })
498
556
  }})
499
557
  }
@@ -546,10 +604,20 @@ func (hs *TnHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
546
604
  return
547
605
  }
548
606
  http.NewResponseController(w).EnableFullDuplex()
549
- // The messages are created on the loop (streams capture its async context)
607
+ // The messages are created on the loop (streams capture its async context);
608
+ // when it is idle, right here
550
609
  created := make(chan *TnServerResponse, 1)
551
610
  bodyDone := make(chan struct{})
552
- TnLoopPost(func() {
611
+ // A small body that comes with the headers (a Content-Length, no Expect:
612
+ // 100-continue) is read here first, so the request needs no reader goroutine
613
+ var small []byte
614
+ var smallErr error
615
+ if r.ProtoMajor == 1 && r.ContentLength > 0 && r.ContentLength <= 65536 && r.Header.Get("Expect") == "" {
616
+ small = make([]byte, r.ContentLength)
617
+ _, smallErr = io.ReadFull(r.Body, small)
618
+ r.Body.Close()
619
+ }
620
+ tnLoopRunOrPost(func() {
553
621
  TnAsyncRun(hs.asyncCtx, func() {
554
622
  req := hs.incoming(r, local)
555
623
  req.socket = tnInfoSocket(local, r.RemoteAddr)
@@ -559,7 +627,24 @@ func (hs *TnHttpServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
559
627
  res := tnNewServerResponse(req)
560
628
  created <- res
561
629
  hs.emit("request", req, res)
562
- tnPumpBody(r.Body, req, func() { close(bodyDone) })
630
+ if small != nil {
631
+ if smallErr != nil {
632
+ req.aborted = true
633
+ req.destroy(TnNodeError("Error", "ECONNRESET", "aborted"))
634
+ } else {
635
+ req.push(small)
636
+ req.complete = true
637
+ req.push(nil)
638
+ }
639
+ close(bodyDone)
640
+ } else if r.Body == nil || r.Body == http.NoBody {
641
+ // No body: the request ends at once, without a reader goroutine
642
+ req.complete = true
643
+ req.push(nil)
644
+ close(bodyDone)
645
+ } else {
646
+ tnPumpBody(r.Body, req, nil, func() { close(bodyDone) })
647
+ }
563
648
  })
564
649
  })
565
650
  res := <-created
@@ -683,10 +768,21 @@ func tnServeOps(w http.ResponseWriter, r *http.Request, res *TnServerResponse, a
683
768
  }
684
769
  }
685
770
  w.WriteHeader(op.status)
686
- rc.Flush()
771
+ if op.flush {
772
+ rc.Flush()
773
+ }
687
774
  case op.end:
775
+ // end(chunk): its data goes out with the headers; net/http flushes
776
+ // once the handler returns
777
+ var err error
778
+ if len(op.data) > 0 {
779
+ _, err = w.Write(op.data)
780
+ }
781
+ for k, v := range op.trailer {
782
+ w.Header()[http.TrailerPrefix+k] = v
783
+ }
688
784
  ended = true
689
- op.done(nil)
785
+ op.done(err)
690
786
  default:
691
787
  _, err := w.Write(op.data)
692
788
  if err == nil {
@@ -1215,12 +1311,14 @@ func (cr *TnClientRequest) start(body []byte, complete bool) {
1215
1311
  } else {
1216
1312
  res.resume()
1217
1313
  }
1314
+ // The trailers are read with the end of the body, before 'end'
1218
1315
  tnPumpBody(resp.Body, res, func() {
1316
+ for k, v := range resp.Trailer {
1317
+ res.trailers.Set(strings.ToLower(k), strings.Join(v, ", "))
1318
+ }
1319
+ }, func() {
1219
1320
  TnLoopPost(func() {
1220
1321
  defer TnLoopUnref()
1221
- for k, v := range resp.Trailer {
1222
- res.trailers.Set(strings.ToLower(k), strings.Join(v, ", "))
1223
- }
1224
1322
  cr.closeHeld = false
1225
1323
  cr.emitClose()
1226
1324
  })
@@ -1397,7 +1495,7 @@ func TnHttpGet(secure bool, args ...interface{}) *TnClientRequest {
1397
1495
  const CLIENT_REQUEST_METHODS = STREAM_METHODS.replace('end=*TnStream', 'end=*TnClientRequest').replace('destroy=*TnStream', 'destroy=*TnClientRequest') +
1398
1496
  '; setHeader=*TnClientRequest; getHeader=interface{}; getHeaders=*TnMap[string, interface{}]; getHeaderNames=[]string; getRawHeaderNames=[]string; hasHeader=bool; removeHeader=; flushHeaders=; setTimeout=*TnClientRequest; setNoDelay=; setSocketKeepAlive=; abort=';
1399
1497
  const SERVER_RESPONSE_METHODS = STREAM_METHODS.replace('end=*TnStream', 'end=*TnServerResponse') +
1400
- '; setHeader=*TnServerResponse; setHeaders=*TnServerResponse; appendHeader=*TnServerResponse; getHeader=interface{}; getHeaders=*TnMap[string, interface{}]; getHeaderNames=[]string; hasHeader=bool; removeHeader=; writeHead=*TnServerResponse; flushHeaders=; writeContinue=; setTimeout=*TnServerResponse';
1498
+ '; setHeader=*TnServerResponse; setHeaders=*TnServerResponse; appendHeader=*TnServerResponse; getHeader=interface{}; getHeaders=*TnMap[string, interface{}]; getHeaderNames=[]string; hasHeader=bool; removeHeader=; addTrailers=; writeHead=*TnServerResponse; flushHeaders=; writeContinue=; setTimeout=*TnServerResponse';
1401
1499
  function eventName(nodes) {
1402
1500
  const event = nodes[0];
1403
1501
  return event && event.kind === 'StringLiteral' ? String(event.text) : '';