typenative 0.3.1 → 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 (147) hide show
  1. package/CHANGELOG.md +57 -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 -47
  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/typed_arrays.js +608 -0
  19. package/bin/node/util.js +3 -0
  20. package/bin/node/zlib.js +131 -16
  21. package/bin/transpiler.js +635 -44
  22. package/bin/version.js +1 -1
  23. package/conformance.md +16 -4
  24. package/examples/README.md +54 -0
  25. package/examples/fibonacci/README.md +89 -0
  26. package/examples/fibonacci/bench.mjs +87 -0
  27. package/examples/fibonacci/main.ts +21 -0
  28. package/examples/hello-world/README.md +62 -0
  29. package/examples/hello-world/bench.mjs +66 -0
  30. package/examples/hello-world/main.ts +7 -0
  31. package/examples/json-server/README.md +125 -0
  32. package/examples/json-server/bench.mjs +171 -0
  33. package/examples/json-server/main.ts +67 -0
  34. package/govendor/github.com/andybalholm/brotli/LICENSE +19 -0
  35. package/govendor/github.com/andybalholm/brotli/README.md +19 -0
  36. package/govendor/github.com/andybalholm/brotli/backward_references.go +185 -0
  37. package/govendor/github.com/andybalholm/brotli/backward_references_hq.go +796 -0
  38. package/govendor/github.com/andybalholm/brotli/bit_cost.go +436 -0
  39. package/govendor/github.com/andybalholm/brotli/bit_reader.go +266 -0
  40. package/govendor/github.com/andybalholm/brotli/bitwriter.go +56 -0
  41. package/govendor/github.com/andybalholm/brotli/block_splitter.go +144 -0
  42. package/govendor/github.com/andybalholm/brotli/block_splitter_command.go +434 -0
  43. package/govendor/github.com/andybalholm/brotli/block_splitter_distance.go +433 -0
  44. package/govendor/github.com/andybalholm/brotli/block_splitter_literal.go +433 -0
  45. package/govendor/github.com/andybalholm/brotli/brotli_bit_stream.go +1435 -0
  46. package/govendor/github.com/andybalholm/brotli/cluster.go +30 -0
  47. package/govendor/github.com/andybalholm/brotli/cluster_command.go +164 -0
  48. package/govendor/github.com/andybalholm/brotli/cluster_distance.go +326 -0
  49. package/govendor/github.com/andybalholm/brotli/cluster_literal.go +326 -0
  50. package/govendor/github.com/andybalholm/brotli/command.go +254 -0
  51. package/govendor/github.com/andybalholm/brotli/compress_fragment.go +834 -0
  52. package/govendor/github.com/andybalholm/brotli/compress_fragment_two_pass.go +775 -0
  53. package/govendor/github.com/andybalholm/brotli/constants.go +77 -0
  54. package/govendor/github.com/andybalholm/brotli/context.go +2176 -0
  55. package/govendor/github.com/andybalholm/brotli/decode.go +2570 -0
  56. package/govendor/github.com/andybalholm/brotli/dictionary.go +122890 -0
  57. package/govendor/github.com/andybalholm/brotli/dictionary_hash.go +32779 -0
  58. package/govendor/github.com/andybalholm/brotli/encode.go +1220 -0
  59. package/govendor/github.com/andybalholm/brotli/encoder.go +177 -0
  60. package/govendor/github.com/andybalholm/brotli/encoder_dict.go +22 -0
  61. package/govendor/github.com/andybalholm/brotli/encoder_fast.go +201 -0
  62. package/govendor/github.com/andybalholm/brotli/entropy_encode.go +592 -0
  63. package/govendor/github.com/andybalholm/brotli/entropy_encode_static.go +4399 -0
  64. package/govendor/github.com/andybalholm/brotli/fast_log.go +290 -0
  65. package/govendor/github.com/andybalholm/brotli/find_match_length.go +45 -0
  66. package/govendor/github.com/andybalholm/brotli/go.mod +3 -0
  67. package/govendor/github.com/andybalholm/brotli/h10.go +287 -0
  68. package/govendor/github.com/andybalholm/brotli/h5.go +214 -0
  69. package/govendor/github.com/andybalholm/brotli/h6.go +216 -0
  70. package/govendor/github.com/andybalholm/brotli/hash.go +342 -0
  71. package/govendor/github.com/andybalholm/brotli/hash_composite.go +93 -0
  72. package/govendor/github.com/andybalholm/brotli/hash_forgetful_chain.go +252 -0
  73. package/govendor/github.com/andybalholm/brotli/hash_longest_match_quickly.go +214 -0
  74. package/govendor/github.com/andybalholm/brotli/hash_rolling.go +168 -0
  75. package/govendor/github.com/andybalholm/brotli/histogram.go +226 -0
  76. package/govendor/github.com/andybalholm/brotli/http.go +201 -0
  77. package/govendor/github.com/andybalholm/brotli/huffman.go +653 -0
  78. package/govendor/github.com/andybalholm/brotli/literal_cost.go +182 -0
  79. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain1.go +212 -0
  80. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain2.go +248 -0
  81. package/govendor/github.com/andybalholm/brotli/matchfinder/bargain3.go +278 -0
  82. package/govendor/github.com/andybalholm/brotli/matchfinder/emitter.go +34 -0
  83. package/govendor/github.com/andybalholm/brotli/matchfinder/m0.go +175 -0
  84. package/govendor/github.com/andybalholm/brotli/matchfinder/m4.go +339 -0
  85. package/govendor/github.com/andybalholm/brotli/matchfinder/matchfinder.go +103 -0
  86. package/govendor/github.com/andybalholm/brotli/matchfinder/pathfinder.go +328 -0
  87. package/govendor/github.com/andybalholm/brotli/matchfinder/textencoder.go +53 -0
  88. package/govendor/github.com/andybalholm/brotli/matchfinder/trio.go +327 -0
  89. package/govendor/github.com/andybalholm/brotli/matchfinder/zdfast.go +258 -0
  90. package/govendor/github.com/andybalholm/brotli/matchfinder/zfast.go +206 -0
  91. package/govendor/github.com/andybalholm/brotli/matchfinder/zm.go +302 -0
  92. package/govendor/github.com/andybalholm/brotli/memory.go +66 -0
  93. package/govendor/github.com/andybalholm/brotli/metablock.go +574 -0
  94. package/govendor/github.com/andybalholm/brotli/metablock_command.go +165 -0
  95. package/govendor/github.com/andybalholm/brotli/metablock_distance.go +165 -0
  96. package/govendor/github.com/andybalholm/brotli/metablock_literal.go +165 -0
  97. package/govendor/github.com/andybalholm/brotli/params.go +37 -0
  98. package/govendor/github.com/andybalholm/brotli/platform.go +103 -0
  99. package/govendor/github.com/andybalholm/brotli/prefix.go +30 -0
  100. package/govendor/github.com/andybalholm/brotli/prefix_dec.go +723 -0
  101. package/govendor/github.com/andybalholm/brotli/quality.go +196 -0
  102. package/govendor/github.com/andybalholm/brotli/reader.go +127 -0
  103. package/govendor/github.com/andybalholm/brotli/ringbuffer.go +134 -0
  104. package/govendor/github.com/andybalholm/brotli/state.go +313 -0
  105. package/govendor/github.com/andybalholm/brotli/static_dict.go +662 -0
  106. package/govendor/github.com/andybalholm/brotli/static_dict_lut.go +75094 -0
  107. package/govendor/github.com/andybalholm/brotli/symbol_list.go +22 -0
  108. package/govendor/github.com/andybalholm/brotli/transform.go +641 -0
  109. package/govendor/github.com/andybalholm/brotli/utf8_util.go +70 -0
  110. package/govendor/github.com/andybalholm/brotli/util.go +7 -0
  111. package/govendor/github.com/andybalholm/brotli/write_bits.go +52 -0
  112. package/govendor/github.com/andybalholm/brotli/writer.go +162 -0
  113. package/govendor/github.com/fsnotify/fsnotify/.mailmap +2 -0
  114. package/govendor/github.com/fsnotify/fsnotify/CHANGELOG.md +651 -0
  115. package/govendor/github.com/fsnotify/fsnotify/CONTRIBUTING.md +147 -0
  116. package/govendor/github.com/fsnotify/fsnotify/LICENSE +25 -0
  117. package/govendor/github.com/fsnotify/fsnotify/README.md +214 -0
  118. package/govendor/github.com/fsnotify/fsnotify/backend_fen.go +469 -0
  119. package/govendor/github.com/fsnotify/fsnotify/backend_inotify.go +597 -0
  120. package/govendor/github.com/fsnotify/fsnotify/backend_kqueue.go +735 -0
  121. package/govendor/github.com/fsnotify/fsnotify/backend_other.go +22 -0
  122. package/govendor/github.com/fsnotify/fsnotify/backend_windows.go +702 -0
  123. package/govendor/github.com/fsnotify/fsnotify/fsnotify.go +526 -0
  124. package/govendor/github.com/fsnotify/fsnotify/go.mod +3 -0
  125. package/govendor/github.com/fsnotify/fsnotify/internal/darwin.go +20 -0
  126. package/govendor/github.com/fsnotify/fsnotify/internal/debug_darwin.go +15 -0
  127. package/govendor/github.com/fsnotify/fsnotify/internal/debug_dragonfly.go +15 -0
  128. package/govendor/github.com/fsnotify/fsnotify/internal/debug_freebsd.go +20 -0
  129. package/govendor/github.com/fsnotify/fsnotify/internal/debug_kqueue.go +32 -0
  130. package/govendor/github.com/fsnotify/fsnotify/internal/debug_linux.go +56 -0
  131. package/govendor/github.com/fsnotify/fsnotify/internal/debug_netbsd.go +15 -0
  132. package/govendor/github.com/fsnotify/fsnotify/internal/debug_openbsd.go +16 -0
  133. package/govendor/github.com/fsnotify/fsnotify/internal/debug_solaris.go +45 -0
  134. package/govendor/github.com/fsnotify/fsnotify/internal/debug_windows.go +40 -0
  135. package/govendor/github.com/fsnotify/fsnotify/internal/freebsd.go +20 -0
  136. package/govendor/github.com/fsnotify/fsnotify/internal/internal.go +2 -0
  137. package/govendor/github.com/fsnotify/fsnotify/internal/unix.go +20 -0
  138. package/govendor/github.com/fsnotify/fsnotify/internal/unix2.go +25 -0
  139. package/govendor/github.com/fsnotify/fsnotify/internal/windows.go +40 -0
  140. package/govendor/github.com/fsnotify/fsnotify/shared.go +64 -0
  141. package/govendor/github.com/fsnotify/fsnotify/staticcheck.conf +3 -0
  142. package/govendor/github.com/fsnotify/fsnotify/system_bsd.go +7 -0
  143. package/govendor/github.com/fsnotify/fsnotify/system_darwin.go +8 -0
  144. package/govendor/manifest.txt +3 -1
  145. package/package.json +1 -1
  146. package/types/typenative-npm.d.ts +72 -0
  147. package/types/typenative.d.ts +117 -1
@@ -0,0 +1,526 @@
1
+ // Package fsnotify provides a cross-platform interface for file system
2
+ // notifications.
3
+ //
4
+ // Currently supported systems:
5
+ //
6
+ // - Linux via inotify
7
+ // - BSD, macOS via kqueue
8
+ // - Windows via ReadDirectoryChangesW
9
+ // - illumos via FEN
10
+ //
11
+ // # FSNOTIFY_DEBUG
12
+ //
13
+ // Set the FSNOTIFY_DEBUG environment variable to "1" to print debug messages to
14
+ // stderr. This can be useful to track down some problems, especially in cases
15
+ // where fsnotify is used as an indirect dependency.
16
+ //
17
+ // Every event will be printed as soon as there's something useful to print,
18
+ // with as little processing from fsnotify.
19
+ //
20
+ // Example output:
21
+ //
22
+ // FSNOTIFY_DEBUG: 11:34:23.633087586 256:IN_CREATE → "/tmp/file-1"
23
+ // FSNOTIFY_DEBUG: 11:34:23.633202319 4:IN_ATTRIB → "/tmp/file-1"
24
+ // FSNOTIFY_DEBUG: 11:34:28.989728764 512:IN_DELETE → "/tmp/file-1"
25
+ package fsnotify
26
+
27
+ import (
28
+ "errors"
29
+ "fmt"
30
+ "os"
31
+ "path/filepath"
32
+ "strings"
33
+ )
34
+
35
+ // Watcher watches a set of paths, delivering events on a channel.
36
+ //
37
+ // A watcher should not be copied (e.g. pass it by pointer, rather than by
38
+ // value).
39
+ //
40
+ // # Linux notes
41
+ //
42
+ // When a file is removed a Remove event won't be emitted until all file
43
+ // descriptors are closed, and deletes will always emit a Chmod. For example:
44
+ //
45
+ // fp := os.Open("file")
46
+ // os.Remove("file") // Triggers Chmod
47
+ // fp.Close() // Triggers Remove
48
+ //
49
+ // This is the event that inotify sends, so not much can be changed about this.
50
+ //
51
+ // The fs.inotify.max_user_watches sysctl variable specifies the upper limit
52
+ // for the number of watches per user, and fs.inotify.max_user_instances
53
+ // specifies the maximum number of inotify instances per user. Every Watcher you
54
+ // create is an "instance", and every path you add is a "watch". Reaching the
55
+ // limit will result in a "no space left on device" or "too many open files"
56
+ // error.
57
+ //
58
+ // These are also exposed in /proc as /proc/sys/fs/inotify/max_user_watches and
59
+ // /proc/sys/fs/inotify/max_user_instances. The default values differ per distro
60
+ // and available memory.
61
+ //
62
+ // To increase them you can use sysctl or write the value to the /proc file:
63
+ //
64
+ // sysctl fs.inotify.max_user_watches=200000
65
+ // sysctl fs.inotify.max_user_instances=256
66
+ //
67
+ // To make the changes persist on reboot edit /etc/sysctl.conf or
68
+ // /usr/lib/sysctl.d/50-default.conf (details differ per Linux distro; check
69
+ // your distro's documentation):
70
+ //
71
+ // fs.inotify.max_user_watches=200000
72
+ // fs.inotify.max_user_instances=256
73
+ //
74
+ // # kqueue notes (macOS, BSD)
75
+ //
76
+ // kqueue requires opening a file descriptor for every file that's being watched;
77
+ // so if you're watching a directory with five files then that's six file
78
+ // descriptors. You will run in to your system's "max open files" limit faster on
79
+ // these platforms.
80
+ //
81
+ // The sysctl variables kern.maxfiles and kern.maxfilesperproc can be used to
82
+ // control the maximum number of open files, as well as /etc/login.conf on BSD
83
+ // systems.
84
+ //
85
+ // # Windows notes
86
+ //
87
+ // Paths can be added as "C:\\path\\to\\dir", but forward slashes
88
+ // ("C:/path/to/dir") will also work.
89
+ //
90
+ // When a watched directory is removed it will always send an event for the
91
+ // directory itself, but may not send events for all files in that directory.
92
+ // Sometimes it will send events for all files, sometimes it will send no
93
+ // events, and often only for some files.
94
+ //
95
+ // Recursive watching is not currently enabled through fsnotify's public
96
+ // API; the recursive code path is gated and only exercised by fsnotify's
97
+ // own tests. The note below describes backend behavior observed when
98
+ // recursive watching is enabled internally, and is kept here as a
99
+ // reference for maintainers and contributors who encounter it.
100
+ //
101
+ // When recursive watching is enabled and you watch a directory, you may
102
+ // receive a Write event for an intermediate directory whenever a child
103
+ // entry inside it is created, renamed, or removed. For example, with a
104
+ // recursive watch on /a and a new file /a/b/c, you will receive
105
+ // Create /a/b/c and may also receive Write /a/b.
106
+ //
107
+ // This happens because, on NTFS-backed volumes, modifying the entries of a
108
+ // directory updates that directory's last-write time, and the Windows
109
+ // backend requests FILE_NOTIFY_CHANGE_LAST_WRITE to support Write events
110
+ // on files. The same Write filter therefore picks up the directory's
111
+ // metadata update.
112
+ //
113
+ // Whether the directory Write is actually delivered alongside the child
114
+ // events is not guaranteed; it depends on ReadDirectoryChangesW buffering,
115
+ // NTFS metadata update timing, and event coalescing.
116
+ //
117
+ // The default ReadDirectoryChangesW() buffer size is 64K, which is the largest
118
+ // value that is guaranteed to work with SMB filesystems. If you have many
119
+ // events in quick succession this may not be enough, and you will have to use
120
+ // [WithBufferSize] to increase the value.
121
+ type Watcher struct {
122
+ b backend
123
+
124
+ // Events sends the filesystem change events.
125
+ //
126
+ // fsnotify can send the following events; a "path" here can refer to a
127
+ // file, directory, symbolic link, or special file like a FIFO.
128
+ //
129
+ // fsnotify.Create A new path was created; this may be followed by one
130
+ // or more Write events if data also gets written to a
131
+ // file.
132
+ //
133
+ // fsnotify.Remove A path was removed.
134
+ //
135
+ // fsnotify.Rename A path was renamed. A rename is always sent with the
136
+ // old path as Event.Name, and a Create event will be
137
+ // sent with the new name. Renames are only sent for
138
+ // paths that are currently watched; e.g. moving an
139
+ // unmonitored file into a monitored directory will
140
+ // show up as just a Create. Similarly, renaming a file
141
+ // to outside a monitored directory will show up as
142
+ // only a Rename.
143
+ //
144
+ // fsnotify.Write A file or named pipe was written to. A Truncate will
145
+ // also trigger a Write. A single "write action"
146
+ // initiated by the user may show up as one or multiple
147
+ // writes, depending on when the system syncs things to
148
+ // disk. For example when compiling a large Go program
149
+ // you may get hundreds of Write events, and you may
150
+ // want to wait until you've stopped receiving them
151
+ // (see the dedup example in cmd/fsnotify).
152
+ //
153
+ // Some systems also send Write events for directories
154
+ // when the directory contents change. This is the
155
+ // case for kqueue, and on Windows for the directory
156
+ // that contains a created, renamed, or removed child
157
+ // entry. It does not happen on inotify. See the
158
+ // per-platform notes on [Watcher].
159
+ //
160
+ // fsnotify.Chmod Attributes were changed. On Linux this is also sent
161
+ // when a file is removed (or more accurately, when a
162
+ // link to an inode is removed). On kqueue it's sent
163
+ // when a file is truncated. On Windows it's never
164
+ // sent.
165
+ Events chan Event
166
+
167
+ // Errors sends any errors.
168
+ Errors chan error
169
+ }
170
+
171
+ // Event represents a file system notification.
172
+ type Event struct {
173
+ // Path to the file or directory.
174
+ //
175
+ // Paths are relative to the input; for example with Add("dir") the Name
176
+ // will be set to "dir/file" if you create that file, but if you use
177
+ // Add("/path/to/dir") it will be "/path/to/dir/file".
178
+ Name string
179
+
180
+ // File operation that triggered the event.
181
+ //
182
+ // This is a bitmask and some systems may send multiple operations at once.
183
+ // Use the Event.Has() method instead of comparing with ==.
184
+ Op Op
185
+
186
+ // Create events will have this set to the old path if it's a rename. This
187
+ // only works when both the source and destination are watched. It's not
188
+ // reliable when watching individual files, only directories.
189
+ //
190
+ // For example "mv /tmp/file /tmp/rename" will emit:
191
+ //
192
+ // Event{Op: Rename, Name: "/tmp/file"}
193
+ // Event{Op: Create, Name: "/tmp/rename", RenamedFrom: "/tmp/file"}
194
+ renamedFrom string
195
+ }
196
+
197
+ // Op describes a set of file operations.
198
+ type Op uint32
199
+
200
+ // The operations fsnotify can trigger; see the documentation on [Watcher] for a
201
+ // full description, and check them with [Event.Has].
202
+ const (
203
+ // A new pathname was created.
204
+ Create Op = 1 << iota
205
+
206
+ // The pathname was written to; this does *not* mean the write has finished,
207
+ // and a write can be followed by more writes. On Windows and kqueue, a
208
+ // Write on a directory can also indicate that its contents changed; see
209
+ // the per-platform notes on [Watcher].
210
+ Write
211
+
212
+ // The path was removed; any watches on it will be removed. Some "remove"
213
+ // operations may trigger a Rename if the file is actually moved (for
214
+ // example "remove to trash" is often a rename).
215
+ Remove
216
+
217
+ // The path was renamed to something else; any watches on it will be
218
+ // removed.
219
+ Rename
220
+
221
+ // File attributes were changed.
222
+ //
223
+ // It's generally not recommended to take action on this event, as it may
224
+ // get triggered very frequently by some software. For example, Spotlight
225
+ // indexing on macOS, anti-virus software, backup software, etc.
226
+ Chmod
227
+
228
+ // File descriptor was opened.
229
+ //
230
+ // Only works on Linux and FreeBSD.
231
+ xUnportableOpen
232
+
233
+ // File was read from.
234
+ //
235
+ // Only works on Linux and FreeBSD.
236
+ xUnportableRead
237
+
238
+ // File opened for writing was closed.
239
+ //
240
+ // Only works on Linux and FreeBSD.
241
+ //
242
+ // The advantage of using this over Write is that it's more reliable than
243
+ // waiting for Write events to stop. It's also faster (if you're not
244
+ // listening to Write events): copying a file of a few GB can easily
245
+ // generate tens of thousands of Write events in a short span of time.
246
+ xUnportableCloseWrite
247
+
248
+ // File opened for reading was closed.
249
+ //
250
+ // Only works on Linux.
251
+ xUnportableCloseRead
252
+ )
253
+
254
+ var (
255
+ // ErrNonExistentWatch is used when Remove() is called on a path that's not
256
+ // added.
257
+ ErrNonExistentWatch = errors.New("fsnotify: can't remove non-existent watch")
258
+
259
+ // ErrClosed is used when trying to operate on a closed Watcher.
260
+ ErrClosed = errors.New("fsnotify: watcher already closed")
261
+
262
+ // ErrEventOverflow is reported from the Errors channel when there are too
263
+ // many events:
264
+ //
265
+ // - inotify: inotify returns IN_Q_OVERFLOW – because there are too
266
+ // many queued events (the fs.inotify.max_queued_events
267
+ // sysctl can be used to increase this).
268
+ // - windows: The buffer size is too small; WithBufferSize() can be used to increase it.
269
+ // - kqueue, fen: Not used.
270
+ ErrEventOverflow = errors.New("fsnotify: queue or buffer overflow")
271
+
272
+ // ErrUnsupported is returned by AddWith() when WithOps() specified an
273
+ // Unportable event that's not supported on this platform.
274
+ //lint:ignore ST1012 not relevant
275
+ xErrUnsupported = errors.New("fsnotify: not supported with this backend")
276
+ )
277
+
278
+ // NewWatcher creates a new Watcher.
279
+ func NewWatcher() (*Watcher, error) {
280
+ ev, errs := make(chan Event, defaultBufferSize), make(chan error)
281
+ b, err := newBackend(ev, errs)
282
+ if err != nil {
283
+ return nil, err
284
+ }
285
+ return &Watcher{b: b, Events: ev, Errors: errs}, nil
286
+ }
287
+
288
+ // NewBufferedWatcher creates a new Watcher with a buffered Watcher.Events
289
+ // channel.
290
+ //
291
+ // The main use case for this is situations with a very large number of events
292
+ // where the kernel buffer size can't be increased (e.g. due to lack of
293
+ // permissions). An unbuffered Watcher will perform better for almost all use
294
+ // cases, and whenever possible you will be better off increasing the kernel
295
+ // buffers instead of adding a large userspace buffer.
296
+ func NewBufferedWatcher(sz uint) (*Watcher, error) {
297
+ ev, errs := make(chan Event, sz), make(chan error)
298
+ b, err := newBackend(ev, errs)
299
+ if err != nil {
300
+ return nil, err
301
+ }
302
+ return &Watcher{b: b, Events: ev, Errors: errs}, nil
303
+ }
304
+
305
+ // Add starts monitoring the path for changes.
306
+ //
307
+ // A path can only be watched once; watching it more than once is a no-op and will
308
+ // not return an error. Paths that do not yet exist on the filesystem cannot be
309
+ // watched.
310
+ //
311
+ // A watch will be automatically removed if the watched path is deleted or
312
+ // renamed. The exception is the Windows backend, which doesn't remove the
313
+ // watcher on renames.
314
+ //
315
+ // Notifications on network filesystems (NFS, SMB, FUSE, etc.) or special
316
+ // filesystems (/proc, /sys, etc.) generally don't work.
317
+ //
318
+ // Returns [ErrClosed] if [Watcher.Close] was called.
319
+ //
320
+ // See [Watcher.AddWith] for a version that allows adding options.
321
+ //
322
+ // # Watching directories
323
+ //
324
+ // All files in a directory are monitored, including new files that are created
325
+ // after the watcher is started. Subdirectories are not watched (i.e. it's
326
+ // non-recursive).
327
+ //
328
+ // # Watching files
329
+ //
330
+ // Watching individual files (rather than directories) is generally not
331
+ // recommended as many programs (especially editors) update files atomically: it
332
+ // will write to a temporary file which is then moved to destination,
333
+ // overwriting the original (or some variant thereof). The watcher on the
334
+ // original file is now lost, as that no longer exists.
335
+ //
336
+ // The upshot of this is that a power failure or crash won't leave a
337
+ // half-written file.
338
+ //
339
+ // Watch the parent directory and use Event.Name to filter out files you're not
340
+ // interested in. There is an example of this in cmd/fsnotify/file.go.
341
+ func (w *Watcher) Add(path string) error { return w.b.Add(path) }
342
+
343
+ // AddWith is like [Watcher.Add], but allows adding options. When using Add()
344
+ // the defaults described below are used.
345
+ //
346
+ // Possible options are:
347
+ //
348
+ // - [WithBufferSize] sets the buffer size for the Windows backend; no-op on
349
+ // other platforms. The default is 64K (65536 bytes).
350
+ func (w *Watcher) AddWith(path string, opts ...addOpt) error { return w.b.AddWith(path, opts...) }
351
+
352
+ // Remove stops monitoring the path for changes.
353
+ //
354
+ // Directories are always removed non-recursively. For example, if you added
355
+ // /tmp/dir and /tmp/dir/subdir then you will need to remove both.
356
+ //
357
+ // Removing a path that has not yet been added returns [ErrNonExistentWatch].
358
+ //
359
+ // Returns nil if [Watcher.Close] was called.
360
+ func (w *Watcher) Remove(path string) error { return w.b.Remove(path) }
361
+
362
+ // Close removes all watches and closes the Events channel.
363
+ func (w *Watcher) Close() error { return w.b.Close() }
364
+
365
+ // WatchList returns all paths explicitly added with [Watcher.Add] (and are not
366
+ // yet removed).
367
+ //
368
+ // The order is undefined, and may differ per call. Returns nil if
369
+ // [Watcher.Close] was called.
370
+ func (w *Watcher) WatchList() []string { return w.b.WatchList() }
371
+
372
+ // Supports reports if all the listed operations are supported by this platform.
373
+ //
374
+ // Create, Write, Remove, Rename, and Chmod are always supported. It can only
375
+ // return false for an Op starting with Unportable.
376
+ func (w *Watcher) xSupports(op Op) bool { return w.b.xSupports(op) }
377
+
378
+ func (o Op) String() string {
379
+ var b strings.Builder
380
+ if o.Has(Create) {
381
+ b.WriteString("|CREATE")
382
+ }
383
+ if o.Has(Remove) {
384
+ b.WriteString("|REMOVE")
385
+ }
386
+ if o.Has(Write) {
387
+ b.WriteString("|WRITE")
388
+ }
389
+ if o.Has(xUnportableOpen) {
390
+ b.WriteString("|OPEN")
391
+ }
392
+ if o.Has(xUnportableRead) {
393
+ b.WriteString("|READ")
394
+ }
395
+ if o.Has(xUnportableCloseWrite) {
396
+ b.WriteString("|CLOSE_WRITE")
397
+ }
398
+ if o.Has(xUnportableCloseRead) {
399
+ b.WriteString("|CLOSE_READ")
400
+ }
401
+ if o.Has(Rename) {
402
+ b.WriteString("|RENAME")
403
+ }
404
+ if o.Has(Chmod) {
405
+ b.WriteString("|CHMOD")
406
+ }
407
+ if b.Len() == 0 {
408
+ return "[no events]"
409
+ }
410
+ return b.String()[1:]
411
+ }
412
+
413
+ // Has reports if this operation has the given operation.
414
+ func (o Op) Has(h Op) bool { return o&h != 0 }
415
+
416
+ // Has reports if this event has the given operation.
417
+ func (e Event) Has(op Op) bool { return e.Op.Has(op) }
418
+
419
+ // String returns a string representation of the event with their path.
420
+ func (e Event) String() string {
421
+ if e.renamedFrom != "" {
422
+ return fmt.Sprintf("%-13s %q ← %q", e.Op.String(), e.Name, e.renamedFrom)
423
+ }
424
+ return fmt.Sprintf("%-13s %q", e.Op.String(), e.Name)
425
+ }
426
+
427
+ type (
428
+ backend interface {
429
+ Add(string) error
430
+ AddWith(string, ...addOpt) error
431
+ Remove(string) error
432
+ WatchList() []string
433
+ Close() error
434
+ xSupports(Op) bool
435
+ }
436
+ addOpt func(opt *withOpts)
437
+ withOpts struct {
438
+ bufsize int
439
+ op Op
440
+ sendCreate bool
441
+ }
442
+ )
443
+
444
+ var debug = func() bool {
445
+ // Check for exactly "1" (rather than mere existence) so we can add
446
+ // options/flags in the future. I don't know if we ever want that, but it's
447
+ // nice to leave the option open.
448
+ return os.Getenv("FSNOTIFY_DEBUG") == "1"
449
+ }()
450
+
451
+ var defaultOpts = withOpts{
452
+ bufsize: 65536, // 64K
453
+ op: Create | Write | Remove | Rename | Chmod,
454
+ }
455
+
456
+ func getOptions(opts ...addOpt) withOpts {
457
+ with := defaultOpts
458
+ for _, o := range opts {
459
+ if o != nil {
460
+ o(&with)
461
+ }
462
+ }
463
+ return with
464
+ }
465
+
466
+ // WithBufferSize sets the [ReadDirectoryChangesW] buffer size.
467
+ //
468
+ // This only has effect on Windows systems, and is a no-op for other backends.
469
+ //
470
+ // The default value is 64K (65536 bytes) which is the highest value that works
471
+ // on all filesystems and should be enough for most applications, but if you
472
+ // have a large burst of events it may not be enough. You can increase it if
473
+ // you're hitting "queue or buffer overflow" errors ([ErrEventOverflow]).
474
+ //
475
+ // [ReadDirectoryChangesW]: https://learn.microsoft.com/en-gb/windows/win32/api/winbase/nf-winbase-readdirectorychangesw
476
+ func WithBufferSize(bytes int) addOpt {
477
+ return func(opt *withOpts) { opt.bufsize = bytes }
478
+ }
479
+
480
+ // WithOps sets which operations to listen for. The default is [Create],
481
+ // [Write], [Remove], [Rename], and [Chmod].
482
+ //
483
+ // Excluding operations you're not interested in can save quite a bit of CPU
484
+ // time; in some use cases there may be hundreds of thousands of useless Write
485
+ // or Chmod operations per second.
486
+ //
487
+ // This can also be used to add unportable operations not supported by all
488
+ // platforms; unportable operations all start with "Unportable":
489
+ // [UnportableOpen], [UnportableRead], [UnportableCloseWrite], and
490
+ // [UnportableCloseRead].
491
+ //
492
+ // AddWith returns an error when using an unportable operation that's not
493
+ // supported. Use [Watcher.Support] to check for support.
494
+ func withOps(op Op) addOpt {
495
+ return func(opt *withOpts) { opt.op = op }
496
+ }
497
+
498
+ // "Internal" option for recursive watches on inotify.
499
+ func withCreate() addOpt {
500
+ return func(opt *withOpts) { opt.sendCreate = true }
501
+ }
502
+
503
+ var enableRecurse = false
504
+
505
+ // Check if this path is recursive (ends with "/..." or "\..."), and return the
506
+ // path with the /... stripped.
507
+ func recursivePath(path string) (string, bool) {
508
+ path = filepath.Clean(path)
509
+ if !enableRecurse { // Only enabled in tests for now.
510
+ return path, false
511
+ }
512
+ if filepath.Base(path) == "..." {
513
+ return filepath.Dir(path), true
514
+ }
515
+ return path, false
516
+ }
517
+
518
+ type watchFlag uint8
519
+
520
+ const (
521
+ // Added by user with Add(), rather than an internal watch.
522
+ flagByUser = watchFlag(0x01)
523
+ // Part of recursive watch; as the top-level path added by the user or an
524
+ // "internal" watch.
525
+ flagRecurse = watchFlag(0x02)
526
+ )
@@ -0,0 +1,3 @@
1
+ module github.com/fsnotify/fsnotify
2
+
3
+ go 1.23
@@ -0,0 +1,20 @@
1
+ //go:build darwin
2
+
3
+ package internal
4
+
5
+ import (
6
+ "syscall"
7
+
8
+ "golang.org/x/sys/unix"
9
+ )
10
+
11
+ var (
12
+ ErrSyscallEACCES = syscall.EACCES
13
+ ErrUnixEACCES = unix.EACCES
14
+ )
15
+
16
+ var maxfiles uint64
17
+
18
+ func Maxfiles() uint64 { return maxfiles }
19
+ func Mkfifo(path string, mode uint32) error { return unix.Mkfifo(path, mode) }
20
+ func Mknod(path string, mode uint32, dev int) error { return unix.Mknod(path, mode, dev) }
@@ -0,0 +1,15 @@
1
+ package internal
2
+
3
+ import "golang.org/x/sys/unix"
4
+
5
+ var names = []struct {
6
+ n string
7
+ m uint32
8
+ }{
9
+ {"NOTE_ATTRIB", unix.NOTE_ATTRIB},
10
+ {"NOTE_DELETE", unix.NOTE_DELETE},
11
+ {"NOTE_EXTEND", unix.NOTE_EXTEND},
12
+ {"NOTE_LINK", unix.NOTE_LINK},
13
+ {"NOTE_RENAME", unix.NOTE_RENAME},
14
+ {"NOTE_WRITE", unix.NOTE_WRITE},
15
+ }
@@ -0,0 +1,15 @@
1
+ package internal
2
+
3
+ import "golang.org/x/sys/unix"
4
+
5
+ var names = []struct {
6
+ n string
7
+ m uint32
8
+ }{
9
+ {"NOTE_ATTRIB", unix.NOTE_ATTRIB},
10
+ {"NOTE_DELETE", unix.NOTE_DELETE},
11
+ {"NOTE_EXTEND", unix.NOTE_EXTEND},
12
+ {"NOTE_LINK", unix.NOTE_LINK},
13
+ {"NOTE_RENAME", unix.NOTE_RENAME},
14
+ {"NOTE_WRITE", unix.NOTE_WRITE},
15
+ }
@@ -0,0 +1,20 @@
1
+ package internal
2
+
3
+ import "golang.org/x/sys/unix"
4
+
5
+ var names = []struct {
6
+ n string
7
+ m uint32
8
+ }{
9
+ {"NOTE_DELETE", unix.NOTE_DELETE},
10
+ {"NOTE_WRITE", unix.NOTE_WRITE},
11
+ {"NOTE_EXTEND", unix.NOTE_EXTEND},
12
+ {"NOTE_ATTRIB", unix.NOTE_ATTRIB},
13
+ {"NOTE_LINK", unix.NOTE_LINK},
14
+ {"NOTE_RENAME", unix.NOTE_RENAME},
15
+ {"NOTE_REVOKE", unix.NOTE_REVOKE},
16
+ {"NOTE_OPEN", unix.NOTE_OPEN},
17
+ {"NOTE_CLOSE", unix.NOTE_CLOSE},
18
+ {"NOTE_CLOSE_WRITE", unix.NOTE_CLOSE_WRITE},
19
+ {"NOTE_READ", unix.NOTE_READ},
20
+ }
@@ -0,0 +1,32 @@
1
+ //go:build freebsd || openbsd || netbsd || dragonfly || darwin
2
+
3
+ package internal
4
+
5
+ import (
6
+ "fmt"
7
+ "os"
8
+ "strings"
9
+ "time"
10
+
11
+ "golang.org/x/sys/unix"
12
+ )
13
+
14
+ func Debug(name string, kevent *unix.Kevent_t) {
15
+ mask := uint32(kevent.Fflags)
16
+
17
+ var (
18
+ l []string
19
+ unknown = mask
20
+ )
21
+ for _, n := range names {
22
+ if mask&n.m == n.m {
23
+ l = append(l, n.n)
24
+ unknown ^= n.m
25
+ }
26
+ }
27
+ if unknown > 0 {
28
+ l = append(l, fmt.Sprintf("0x%x", unknown))
29
+ }
30
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s %10d:%-20s → %q\n",
31
+ time.Now().Format("15:04:05.000000000"), mask, strings.Join(l, " | "), name)
32
+ }
@@ -0,0 +1,56 @@
1
+ package internal
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ "strings"
7
+ "time"
8
+
9
+ "golang.org/x/sys/unix"
10
+ )
11
+
12
+ func Debug(name string, mask, cookie uint32) {
13
+ names := []struct {
14
+ n string
15
+ m uint32
16
+ }{
17
+ {"IN_ACCESS", unix.IN_ACCESS},
18
+ {"IN_ATTRIB", unix.IN_ATTRIB},
19
+ {"IN_CLOSE", unix.IN_CLOSE},
20
+ {"IN_CLOSE_NOWRITE", unix.IN_CLOSE_NOWRITE},
21
+ {"IN_CLOSE_WRITE", unix.IN_CLOSE_WRITE},
22
+ {"IN_CREATE", unix.IN_CREATE},
23
+ {"IN_DELETE", unix.IN_DELETE},
24
+ {"IN_DELETE_SELF", unix.IN_DELETE_SELF},
25
+ {"IN_IGNORED", unix.IN_IGNORED},
26
+ {"IN_ISDIR", unix.IN_ISDIR},
27
+ {"IN_MODIFY", unix.IN_MODIFY},
28
+ {"IN_MOVE", unix.IN_MOVE},
29
+ {"IN_MOVED_FROM", unix.IN_MOVED_FROM},
30
+ {"IN_MOVED_TO", unix.IN_MOVED_TO},
31
+ {"IN_MOVE_SELF", unix.IN_MOVE_SELF},
32
+ {"IN_OPEN", unix.IN_OPEN},
33
+ {"IN_Q_OVERFLOW", unix.IN_Q_OVERFLOW},
34
+ {"IN_UNMOUNT", unix.IN_UNMOUNT},
35
+ }
36
+
37
+ var (
38
+ l []string
39
+ unknown = mask
40
+ )
41
+ for _, n := range names {
42
+ if mask&n.m == n.m {
43
+ l = append(l, n.n)
44
+ unknown ^= n.m
45
+ }
46
+ }
47
+ if unknown > 0 {
48
+ l = append(l, fmt.Sprintf("0x%x", unknown))
49
+ }
50
+ var c string
51
+ if cookie > 0 {
52
+ c = fmt.Sprintf("(cookie: %d) ", cookie)
53
+ }
54
+ fmt.Fprintf(os.Stderr, "FSNOTIFY_DEBUG: %s %-30s → %s%q\n",
55
+ time.Now().Format("15:04:05.000000000"), strings.Join(l, "|"), c, name)
56
+ }