typenative 0.3.0 → 0.4.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 (148) hide show
  1. package/CHANGELOG.md +71 -0
  2. package/README.md +17 -1
  3. package/bin/cli.js +25 -11
  4. package/bin/go/convert.js +11 -1
  5. package/bin/index.js +221 -44
  6. package/bin/main.js +35 -14
  7. package/bin/node/assert.js +2 -2
  8. package/bin/node/buffer.js +9 -0
  9. package/bin/node/dns.js +1 -1
  10. package/bin/node/events.js +1 -1
  11. package/bin/node/fs.js +1 -1
  12. package/bin/node/fs_watch.js +407 -0
  13. package/bin/node/http2.js +33 -6
  14. package/bin/node/readline.js +1 -1
  15. package/bin/node/registry.js +2 -1
  16. package/bin/node/stream.js +6 -3
  17. package/bin/node/timers.js +1 -1
  18. package/bin/node/tls.js +3 -1
  19. package/bin/node/typed_arrays.js +608 -0
  20. package/bin/node/util.js +3 -0
  21. package/bin/node/zlib.js +131 -16
  22. package/bin/transpiler.js +684 -62
  23. package/bin/version.js +1 -1
  24. package/conformance.md +16 -4
  25. package/examples/README.md +54 -0
  26. package/examples/fibonacci/README.md +89 -0
  27. package/examples/fibonacci/bench.mjs +87 -0
  28. package/examples/fibonacci/main.ts +21 -0
  29. package/examples/hello-world/README.md +62 -0
  30. package/examples/hello-world/bench.mjs +66 -0
  31. package/examples/hello-world/main.ts +7 -0
  32. package/examples/json-server/README.md +125 -0
  33. package/examples/json-server/bench.mjs +171 -0
  34. package/examples/json-server/main.ts +67 -0
  35. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  36. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  37. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  38. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  39. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  40. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  41. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  42. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  43. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  44. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  45. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  46. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  47. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  48. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  49. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  50. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  51. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  52. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  53. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  54. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  55. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  56. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  57. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  58. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  59. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  60. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  61. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  62. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  63. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  64. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  65. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  66. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  67. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  68. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  69. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  70. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  71. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  72. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  73. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  74. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  75. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  76. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  77. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  78. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  79. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  80. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  81. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  82. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  83. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  84. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  85. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  86. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  87. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  88. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  89. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  90. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  91. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  92. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  93. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  94. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  95. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  96. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  97. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  98. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  99. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  100. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  101. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  102. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  103. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  104. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  105. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  106. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  107. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  108. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  109. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  110. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  111. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  112. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  113. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  114. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  115. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  116. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  117. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  118. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  119. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  120. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  121. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  122. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  123. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  124. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  125. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  126. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  127. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  128. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  129. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  130. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  131. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  132. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  133. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  134. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  135. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  136. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  137. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  138. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  139. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  140. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  141. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  142. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  143. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  144. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  145. package/govendor/manifest.txt +3 -1
  146. package/package.json +1 -1
  147. package/types/typenative-npm.d.ts +72 -0
  148. package/types/typenative.d.ts +117 -1
@@ -0,0 +1,735 @@
1
+ //go:build freebsd || openbsd || netbsd || dragonfly || darwin
2
+
3
+ package fsnotify
4
+
5
+ import (
6
+ "errors"
7
+ "fmt"
8
+ "os"
9
+ "path/filepath"
10
+ "runtime"
11
+ "sort"
12
+ "sync"
13
+ "time"
14
+
15
+ "github.com/fsnotify/fsnotify/internal"
16
+ "golang.org/x/sys/unix"
17
+ )
18
+
19
+ type kqueue struct {
20
+ *shared
21
+ Events chan Event
22
+ Errors chan error
23
+
24
+ kq int // File descriptor (as returned by the kqueue() syscall).
25
+ closepipe [2]int // Pipe used for closing kq.
26
+ watches *watches
27
+ }
28
+
29
+ type (
30
+ watches struct {
31
+ mu sync.RWMutex
32
+ wd map[int]watch // wd → watch
33
+ path map[string]int // pathname → wd
34
+ byDir map[string]map[int]struct{} // dirname(path) → wd
35
+ seen map[string]struct{} // Keep track of if we know this file exists.
36
+ byUser map[string]struct{} // Watches added with Watcher.Add()
37
+ }
38
+ watch struct {
39
+ wd int
40
+ name string
41
+ linkName string // In case of links; name is the target, and this is the link.
42
+ isDir bool
43
+ dirFlags uint32
44
+ }
45
+ )
46
+
47
+ func newWatches() *watches {
48
+ return &watches{
49
+ wd: make(map[int]watch),
50
+ path: make(map[string]int),
51
+ byDir: make(map[string]map[int]struct{}),
52
+ seen: make(map[string]struct{}),
53
+ byUser: make(map[string]struct{}),
54
+ }
55
+ }
56
+
57
+ func (w *watches) listPaths(userOnly bool) []string {
58
+ w.mu.RLock()
59
+ defer w.mu.RUnlock()
60
+
61
+ if userOnly {
62
+ l := make([]string, 0, len(w.byUser))
63
+ for p := range w.byUser {
64
+ l = append(l, p)
65
+ }
66
+ return l
67
+ }
68
+
69
+ l := make([]string, 0, len(w.path))
70
+ for p := range w.path {
71
+ l = append(l, p)
72
+ }
73
+ return l
74
+ }
75
+
76
+ func (w *watches) watchesInDir(path string) []string {
77
+ w.mu.RLock()
78
+ defer w.mu.RUnlock()
79
+
80
+ l := make([]string, 0, 4)
81
+ for fd := range w.byDir[path] {
82
+ info := w.wd[fd]
83
+ if _, ok := w.byUser[info.name]; !ok {
84
+ l = append(l, info.name)
85
+ }
86
+ }
87
+ return l
88
+ }
89
+
90
+ // Mark path as added by the user.
91
+ func (w *watches) addUserWatch(path string) {
92
+ w.mu.Lock()
93
+ defer w.mu.Unlock()
94
+ w.byUser[path] = struct{}{}
95
+ }
96
+
97
+ func (w *watches) addLink(path string, fd int) {
98
+ w.mu.Lock()
99
+ defer w.mu.Unlock()
100
+
101
+ w.path[path] = fd
102
+ w.seen[path] = struct{}{}
103
+ }
104
+
105
+ func (w *watches) add(path, linkPath string, fd int, isDir bool) {
106
+ w.mu.Lock()
107
+ defer w.mu.Unlock()
108
+
109
+ w.path[path] = fd
110
+ w.wd[fd] = watch{wd: fd, name: path, linkName: linkPath, isDir: isDir}
111
+
112
+ parent := filepath.Dir(path)
113
+ byDir, ok := w.byDir[parent]
114
+ if !ok {
115
+ byDir = make(map[int]struct{}, 1)
116
+ w.byDir[parent] = byDir
117
+ }
118
+ byDir[fd] = struct{}{}
119
+ }
120
+
121
+ func (w *watches) byWd(fd int) (watch, bool) {
122
+ w.mu.RLock()
123
+ defer w.mu.RUnlock()
124
+ info, ok := w.wd[fd]
125
+ return info, ok
126
+ }
127
+
128
+ func (w *watches) byPath(path string) (watch, bool) {
129
+ w.mu.RLock()
130
+ defer w.mu.RUnlock()
131
+ info, ok := w.wd[w.path[path]]
132
+ return info, ok
133
+ }
134
+
135
+ func (w *watches) updateDirFlags(path string, flags uint32) bool {
136
+ w.mu.Lock()
137
+ defer w.mu.Unlock()
138
+
139
+ fd, ok := w.path[path]
140
+ if !ok { // Already deleted: don't re-set it here.
141
+ return false
142
+ }
143
+ info := w.wd[fd]
144
+ info.dirFlags = flags
145
+ w.wd[fd] = info
146
+ return true
147
+ }
148
+
149
+ func (w *watches) remove(fd int, path string) bool {
150
+ w.mu.Lock()
151
+ defer w.mu.Unlock()
152
+
153
+ isDir := w.wd[fd].isDir
154
+ delete(w.path, path)
155
+ delete(w.byUser, path)
156
+
157
+ parent := filepath.Dir(path)
158
+ delete(w.byDir[parent], fd)
159
+
160
+ if len(w.byDir[parent]) == 0 {
161
+ delete(w.byDir, parent)
162
+ }
163
+
164
+ delete(w.wd, fd)
165
+ delete(w.seen, path)
166
+ return isDir
167
+ }
168
+
169
+ func (w *watches) markSeen(path string, exists bool) {
170
+ w.mu.Lock()
171
+ defer w.mu.Unlock()
172
+ if exists {
173
+ w.seen[path] = struct{}{}
174
+ } else {
175
+ delete(w.seen, path)
176
+ }
177
+ }
178
+
179
+ func (w *watches) seenBefore(path string) bool {
180
+ w.mu.RLock()
181
+ defer w.mu.RUnlock()
182
+ _, ok := w.seen[path]
183
+ return ok
184
+ }
185
+
186
+ var defaultBufferSize = 0
187
+
188
+ func newBackend(ev chan Event, errs chan error) (backend, error) {
189
+ kq, closepipe, err := newKqueue()
190
+ if err != nil {
191
+ return nil, err
192
+ }
193
+
194
+ w := &kqueue{
195
+ shared: newShared(ev, errs),
196
+ Events: ev,
197
+ Errors: errs,
198
+ kq: kq,
199
+ closepipe: closepipe,
200
+ watches: newWatches(),
201
+ }
202
+
203
+ go w.readEvents()
204
+ return w, nil
205
+ }
206
+
207
+ // newKqueue creates a new kernel event queue and returns a descriptor.
208
+ //
209
+ // This registers a new event on closepipe, which will trigger an event when
210
+ // it's closed. This way we can use kevent() without timeout/polling; without
211
+ // the closepipe, it would block forever and we wouldn't be able to stop it at
212
+ // all.
213
+ func newKqueue() (kq int, closepipe [2]int, err error) {
214
+ kq, err = unix.Kqueue()
215
+ if err != nil {
216
+ return kq, closepipe, err
217
+ }
218
+
219
+ // Register the close pipe.
220
+ err = unix.Pipe(closepipe[:])
221
+ if err != nil {
222
+ unix.Close(kq)
223
+ return kq, closepipe, err
224
+ }
225
+ unix.CloseOnExec(closepipe[0])
226
+ unix.CloseOnExec(closepipe[1])
227
+
228
+ // Register changes to listen on the closepipe.
229
+ changes := make([]unix.Kevent_t, 1)
230
+ // SetKevent converts int to the platform-specific types.
231
+ unix.SetKevent(&changes[0], closepipe[0], unix.EVFILT_READ,
232
+ unix.EV_ADD|unix.EV_ENABLE|unix.EV_ONESHOT)
233
+
234
+ ok, err := unix.Kevent(kq, changes, nil, nil)
235
+ if ok == -1 {
236
+ unix.Close(kq)
237
+ unix.Close(closepipe[0])
238
+ unix.Close(closepipe[1])
239
+ return kq, closepipe, err
240
+ }
241
+ return kq, closepipe, nil
242
+ }
243
+
244
+ func (w *kqueue) Close() error {
245
+ if w.shared.close() {
246
+ return nil
247
+ }
248
+
249
+ // Snapshot and drop all watches directly. w.Remove -> w.remove
250
+ // short-circuits on isClosed() (which is already true after
251
+ // w.shared.close() above), so calling Remove here in the happy path
252
+ // leaked every watched directory + file descriptor. On macOS a
253
+ // single directory watch opens an fd for every file in the dir, so
254
+ // long-running processes that recreate watchers (hot-reload dev
255
+ // servers, etc.) ran out of fds with EMFILE (#732).
256
+ pathsToRemove := w.watches.listPaths(false)
257
+ for _, name := range pathsToRemove {
258
+ info, ok := w.watches.byPath(name)
259
+ if !ok {
260
+ // w.path has an entry for name but w.wd doesn't --
261
+ // drop the stale lookup entry so the map state is
262
+ // consistent after Close.
263
+ w.watches.remove(0, name)
264
+ continue
265
+ }
266
+ _ = w.register([]int{info.wd}, unix.EV_DELETE, 0)
267
+ unix.Close(info.wd)
268
+ w.watches.remove(info.wd, name)
269
+ }
270
+
271
+ unix.Close(w.closepipe[1]) // Send "quit" message to readEvents
272
+ return nil
273
+ }
274
+
275
+ func (w *kqueue) Add(name string) error { return w.AddWith(name) }
276
+
277
+ func (w *kqueue) AddWith(name string, opts ...addOpt) error {
278
+ if debug {
279
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s AddWith(%q)\n",
280
+ time.Now().Format("15:04:05.000000000"), name)
281
+ }
282
+
283
+ with := getOptions(opts...)
284
+ if !w.xSupports(with.op) {
285
+ return fmt.Errorf("%w: %s", xErrUnsupported, with.op)
286
+ }
287
+
288
+ _, err := w.addWatch(name, noteAllEvents, false)
289
+ if err != nil {
290
+ return err
291
+ }
292
+ w.watches.addUserWatch(name)
293
+ return nil
294
+ }
295
+
296
+ func (w *kqueue) Remove(name string) error {
297
+ if debug {
298
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s Remove(%q)\n",
299
+ time.Now().Format("15:04:05.000000000"), name)
300
+ }
301
+ return w.remove(name, true)
302
+ }
303
+
304
+ func (w *kqueue) remove(name string, unwatchFiles bool) error {
305
+ if w.isClosed() {
306
+ return nil
307
+ }
308
+
309
+ name = filepath.Clean(name)
310
+ info, ok := w.watches.byPath(name)
311
+ if !ok {
312
+ return fmt.Errorf("%w: %s", ErrNonExistentWatch, name)
313
+ }
314
+
315
+ err := w.register([]int{info.wd}, unix.EV_DELETE, 0)
316
+ if err != nil {
317
+ return err
318
+ }
319
+
320
+ unix.Close(info.wd)
321
+
322
+ isDir := w.watches.remove(info.wd, name)
323
+
324
+ // Find all watched paths that are in this directory that are not external.
325
+ if unwatchFiles && isDir {
326
+ pathsToRemove := w.watches.watchesInDir(name)
327
+ for _, name := range pathsToRemove {
328
+ // Since these are internal, not much sense in propagating error to
329
+ // the user, as that will just confuse them with an error about a
330
+ // path they did not explicitly watch themselves.
331
+ w.Remove(name)
332
+ }
333
+ }
334
+ return nil
335
+ }
336
+
337
+ func (w *kqueue) WatchList() []string {
338
+ if w.isClosed() {
339
+ return nil
340
+ }
341
+ return w.watches.listPaths(true)
342
+ }
343
+
344
+ // Watch all events (except NOTE_EXTEND, NOTE_LINK, NOTE_REVOKE)
345
+ const noteAllEvents = unix.NOTE_DELETE | unix.NOTE_WRITE | unix.NOTE_ATTRIB | unix.NOTE_RENAME
346
+
347
+ // addWatch adds name to the watched file set; the flags are interpreted as
348
+ // described in kevent(2).
349
+ //
350
+ // Returns the real path to the file which was added, with symlinks resolved.
351
+ func (w *kqueue) addWatch(name string, flags uint32, listDir bool) (string, error) {
352
+ if w.isClosed() {
353
+ return "", ErrClosed
354
+ }
355
+
356
+ name = filepath.Clean(name)
357
+
358
+ info, alreadyWatching := w.watches.byPath(name)
359
+ if !alreadyWatching {
360
+ fi, err := os.Lstat(name)
361
+ if err != nil {
362
+ return "", err
363
+ }
364
+
365
+ // Don't watch sockets or named pipes.
366
+ if (fi.Mode()&os.ModeSocket == os.ModeSocket) || (fi.Mode()&os.ModeNamedPipe == os.ModeNamedPipe) {
367
+ return "", nil
368
+ }
369
+
370
+ // Follow symlinks, but only for paths added with Add(), and not paths
371
+ // we're adding from internalWatch from a listdir.
372
+ if !listDir && fi.Mode()&os.ModeSymlink == os.ModeSymlink {
373
+ link, err := os.Readlink(name)
374
+ if err != nil {
375
+ return "", err
376
+ }
377
+ if !filepath.IsAbs(link) {
378
+ link = filepath.Join(filepath.Dir(name), link)
379
+ }
380
+
381
+ _, alreadyWatching = w.watches.byPath(link)
382
+ if alreadyWatching {
383
+ // Add to watches so we don't get spurious Create events later
384
+ // on when we diff the directories.
385
+ w.watches.addLink(name, 0)
386
+ return link, nil
387
+ }
388
+
389
+ info.linkName = name
390
+ name = link
391
+ fi, err = os.Lstat(name)
392
+ if err != nil {
393
+ return "", err
394
+ }
395
+ }
396
+
397
+ info.wd, err = internal.IgnoringEINTR(func() (int, error) {
398
+ return unix.Open(name, openMode, 0)
399
+ })
400
+ if err != nil {
401
+ return "", err
402
+ }
403
+ info.isDir = fi.IsDir()
404
+ }
405
+
406
+ err := w.register([]int{info.wd}, unix.EV_ADD|unix.EV_CLEAR|unix.EV_ENABLE, flags)
407
+ if err != nil {
408
+ unix.Close(info.wd)
409
+ return "", err
410
+ }
411
+
412
+ if !alreadyWatching {
413
+ w.watches.add(name, info.linkName, info.wd, info.isDir)
414
+ }
415
+
416
+ // Watch the directory if it has not been watched before, or if it was
417
+ // watched before, but perhaps only a NOTE_DELETE (watchDirectoryFiles)
418
+ if info.isDir {
419
+ watchDir := (flags&unix.NOTE_WRITE) == unix.NOTE_WRITE &&
420
+ (!alreadyWatching || (info.dirFlags&unix.NOTE_WRITE) != unix.NOTE_WRITE)
421
+ if !w.watches.updateDirFlags(name, flags) {
422
+ return "", nil
423
+ }
424
+
425
+ if watchDir {
426
+ d := name
427
+ if info.linkName != "" {
428
+ d = info.linkName
429
+ }
430
+ if err := w.watchDirectoryFiles(d); err != nil {
431
+ return "", err
432
+ }
433
+ }
434
+ }
435
+ return name, nil
436
+ }
437
+
438
+ // readEvents reads from kqueue and converts the received kevents into
439
+ // Event values that it sends down the Events channel.
440
+ func (w *kqueue) readEvents() {
441
+ defer func() {
442
+ close(w.Events)
443
+ close(w.Errors)
444
+ _ = unix.Close(w.kq)
445
+ unix.Close(w.closepipe[0])
446
+ }()
447
+
448
+ eventBuffer := make([]unix.Kevent_t, 10)
449
+ for {
450
+ kevents, err := internal.IgnoringEINTR(func() ([]unix.Kevent_t, error) {
451
+ return w.read(eventBuffer)
452
+ })
453
+ if err != nil {
454
+ if !w.sendError(fmt.Errorf("fsnotify.readEvents: %w", err)) {
455
+ return
456
+ }
457
+ }
458
+
459
+ for _, kevent := range kevents {
460
+ var (
461
+ wd = int(kevent.Ident)
462
+ mask = uint32(kevent.Fflags)
463
+ )
464
+
465
+ // Shut down the loop when the pipe is closed, but only after all
466
+ // other events have been processed.
467
+ if wd == w.closepipe[0] {
468
+ return
469
+ }
470
+
471
+ path, ok := w.watches.byWd(wd)
472
+ if debug {
473
+ internal.Debug(path.name, &kevent)
474
+ }
475
+
476
+ // On macOS it seems that sometimes an event with Ident=0 is
477
+ // delivered, and no other flags/information beyond that, even
478
+ // though we never saw such a file descriptor. For example in
479
+ // TestWatchSymlink/277 (usually at the end, but sometimes sooner):
480
+ //
481
+ // fmt.Printf("READ: %2d %#v\n", kevent.Ident, kevent)
482
+ // unix.Kevent_t{Ident:0x2a, Filter:-4, Flags:0x25, Fflags:0x2, Data:0, Udata:(*uint8)(nil)}
483
+ // unix.Kevent_t{Ident:0x0, Filter:-4, Flags:0x25, Fflags:0x2, Data:0, Udata:(*uint8)(nil)}
484
+ //
485
+ // The first is a normal event, the second with Ident 0. No error
486
+ // flag, no data, no ... nothing.
487
+ //
488
+ // I read a bit through bsd/kern_event.c from the xnu source, but I
489
+ // don't really see an obvious location where this is triggered –
490
+ // this doesn't seem intentional, but idk...
491
+ //
492
+ // Technically fd 0 is a valid descriptor, so only skip it if
493
+ // there's no path, and if we're on macOS.
494
+ if !ok && kevent.Ident == 0 && runtime.GOOS == "darwin" {
495
+ continue
496
+ }
497
+
498
+ event := w.newEvent(path.name, path.linkName, mask)
499
+
500
+ if event.Has(Rename) || event.Has(Remove) {
501
+ w.remove(event.Name, false)
502
+ w.watches.markSeen(event.Name, false)
503
+ }
504
+
505
+ if path.isDir && event.Has(Write) && !event.Has(Remove) {
506
+ w.dirChange(event.Name)
507
+ } else if !w.sendEvent(event) {
508
+ return
509
+ }
510
+
511
+ if event.Has(Remove) {
512
+ // Look for a file that may have overwritten this; for example,
513
+ // mv f1 f2 will delete f2, then create f2.
514
+ if path.isDir {
515
+ fileDir := filepath.Clean(event.Name)
516
+ _, found := w.watches.byPath(fileDir)
517
+ if found {
518
+ // TODO: this branch is never triggered in any test.
519
+ // Added in d6220df (2012).
520
+ // isDir check added in 8611c35 (2016): https://github.com/fsnotify/fsnotify/pull/111
521
+ //
522
+ // I don't really get how this can be triggered either.
523
+ // And it wasn't triggered in the patch that added it,
524
+ // either.
525
+ //
526
+ // Original also had a comment:
527
+ // make sure the directory exists before we watch for
528
+ // changes. When we do a recursive watch and perform
529
+ // rm -rf, the parent directory might have gone
530
+ // missing, ignore the missing directory and let the
531
+ // upcoming delete event remove the watch from the
532
+ // parent directory.
533
+ err := w.dirChange(fileDir)
534
+ if !w.sendError(err) {
535
+ return
536
+ }
537
+ }
538
+ } else {
539
+ path := filepath.Clean(event.Name)
540
+ if fi, err := os.Lstat(path); err == nil {
541
+ err := w.sendCreateIfNew(path, fi)
542
+ if !w.sendError(err) {
543
+ return
544
+ }
545
+ }
546
+ }
547
+ }
548
+ }
549
+ }
550
+ }
551
+
552
+ // newEvent returns an platform-independent Event based on kqueue Fflags.
553
+ func (w *kqueue) newEvent(name, linkName string, mask uint32) Event {
554
+ e := Event{Name: name}
555
+ if linkName != "" {
556
+ // If the user watched "/path/link" then emit events as "/path/link"
557
+ // rather than "/path/target".
558
+ e.Name = linkName
559
+ }
560
+
561
+ if mask&unix.NOTE_DELETE == unix.NOTE_DELETE {
562
+ e.Op |= Remove
563
+ }
564
+ if mask&unix.NOTE_WRITE == unix.NOTE_WRITE {
565
+ e.Op |= Write
566
+ }
567
+ if mask&unix.NOTE_RENAME == unix.NOTE_RENAME {
568
+ e.Op |= Rename
569
+ }
570
+ if mask&unix.NOTE_ATTRIB == unix.NOTE_ATTRIB {
571
+ e.Op |= Chmod
572
+ }
573
+ // No point sending a write and delete event at the same time: if it's gone,
574
+ // then it's gone.
575
+ if e.Op.Has(Write) && e.Op.Has(Remove) {
576
+ e.Op &^= Write
577
+ }
578
+ return e
579
+ }
580
+
581
+ // watchDirectoryFiles to mimic inotify when adding a watch on a directory
582
+ func (w *kqueue) watchDirectoryFiles(dirPath string) error {
583
+ files, err := os.ReadDir(dirPath)
584
+ if err != nil {
585
+ return err
586
+ }
587
+
588
+ for _, f := range files {
589
+ path := filepath.Join(dirPath, f.Name())
590
+
591
+ fi, err := f.Info()
592
+ if err != nil {
593
+ return fmt.Errorf("%q: %w", path, err)
594
+ }
595
+
596
+ cleanPath, err := w.internalWatch(path, fi)
597
+ if err != nil {
598
+ // No permission, or the entry resolved to a missing target
599
+ // (e.g. a dangling symlink): not a problem, just skip. But
600
+ // do mark it as seen to prevent it from being picked up as
601
+ // a "new" file later (it still shows up in the directory
602
+ // listing).
603
+ switch {
604
+ case errors.Is(err, unix.EACCES) || errors.Is(err, unix.EPERM) ||
605
+ errors.Is(err, os.ErrNotExist):
606
+ cleanPath = filepath.Clean(path)
607
+ default:
608
+ return fmt.Errorf("%q: %w", path, err)
609
+ }
610
+ }
611
+
612
+ w.watches.markSeen(cleanPath, true)
613
+ }
614
+
615
+ return nil
616
+ }
617
+
618
+ // Search the directory for new files and send an event for them.
619
+ //
620
+ // This functionality is to have the BSD watcher match the inotify, which sends
621
+ // a create event for files created in a watched directory.
622
+ func (w *kqueue) dirChange(dir string) error {
623
+ files, err := os.ReadDir(dir)
624
+ if err != nil {
625
+ // Directory no longer exists: we can ignore this safely. kqueue will
626
+ // still give us the correct events.
627
+ if errors.Is(err, os.ErrNotExist) {
628
+ return nil
629
+ }
630
+ return fmt.Errorf("fsnotify.dirChange %q: %w", dir, err)
631
+ }
632
+
633
+ for _, f := range files {
634
+ fi, err := f.Info()
635
+ if err != nil {
636
+ if errors.Is(err, os.ErrNotExist) {
637
+ return nil
638
+ }
639
+ return fmt.Errorf("fsnotify.dirChange: %w", err)
640
+ }
641
+
642
+ err = w.sendCreateIfNew(filepath.Join(dir, fi.Name()), fi)
643
+ if err != nil {
644
+ // Don't need to send an error if this file isn't readable.
645
+ if errors.Is(err, unix.EACCES) || errors.Is(err, unix.EPERM) || errors.Is(err, os.ErrNotExist) {
646
+ return nil
647
+ }
648
+ return fmt.Errorf("fsnotify.dirChange: %w", err)
649
+ }
650
+ }
651
+ return nil
652
+ }
653
+
654
+ // Send a create event if the file isn't already being tracked, and start
655
+ // watching this file.
656
+ func (w *kqueue) sendCreateIfNew(path string, fi os.FileInfo) error {
657
+ if !w.watches.seenBefore(path) {
658
+ if !w.sendEvent(Event{Name: path, Op: Create}) {
659
+ return nil
660
+ }
661
+ }
662
+
663
+ // Like watchDirectoryFiles, but without doing another ReadDir.
664
+ path, err := w.internalWatch(path, fi)
665
+ if err != nil {
666
+ return err
667
+ }
668
+ w.watches.markSeen(path, true)
669
+ return nil
670
+ }
671
+
672
+ func (w *kqueue) internalWatch(name string, fi os.FileInfo) (string, error) {
673
+ if fi.IsDir() {
674
+ // mimic Linux providing delete events for subdirectories, but preserve
675
+ // the flags used if currently watching subdirectory
676
+ info, _ := w.watches.byPath(name)
677
+ return w.addWatch(name, info.dirFlags|unix.NOTE_DELETE|unix.NOTE_RENAME, true)
678
+ }
679
+
680
+ // Watch file to mimic Linux inotify.
681
+ return w.addWatch(name, noteAllEvents, true)
682
+ }
683
+
684
+ // Register events with the queue.
685
+ func (w *kqueue) register(fds []int, flags int, fflags uint32) error {
686
+ changes := make([]unix.Kevent_t, len(fds))
687
+ for i, fd := range fds {
688
+ // SetKevent converts int to the platform-specific types.
689
+ unix.SetKevent(&changes[i], fd, unix.EVFILT_VNODE, flags)
690
+ changes[i].Fflags = fflags
691
+ }
692
+
693
+ // Register the events.
694
+ success, err := unix.Kevent(w.kq, changes, nil, nil)
695
+ if success == -1 {
696
+ return err
697
+ }
698
+ return nil
699
+ }
700
+
701
+ // read retrieves pending events, or waits until an event occurs.
702
+ func (w *kqueue) read(events []unix.Kevent_t) ([]unix.Kevent_t, error) {
703
+ n, err := unix.Kevent(w.kq, nil, events, nil)
704
+ if err != nil {
705
+ return nil, err
706
+ }
707
+ return events[0:n], nil
708
+ }
709
+
710
+ func (w *kqueue) xSupports(op Op) bool {
711
+ //if runtime.GOOS == "freebsd" {
712
+ // return true // Supports everything.
713
+ //}
714
+ if op.Has(xUnportableOpen) || op.Has(xUnportableRead) ||
715
+ op.Has(xUnportableCloseWrite) || op.Has(xUnportableCloseRead) {
716
+ return false
717
+ }
718
+ return true
719
+ }
720
+
721
+ func (w *kqueue) state() {
722
+ w.watches.mu.Lock()
723
+ defer w.watches.mu.Unlock()
724
+
725
+ all := make([]int, 0, len(w.watches.wd))
726
+ for wd := range w.watches.wd {
727
+ all = append(all, wd)
728
+ }
729
+ sort.Ints(all)
730
+
731
+ for _, wd := range all {
732
+ ww := w.watches.wd[wd]
733
+ fmt.Fprintf(os.Stderr, "%4d %q linkname=%q\n", wd, ww.name, ww.linkName)
734
+ }
735
+ }