sfml.js 1.0.0 → 1.2.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 (313) hide show
  1. package/README.ja.md +10 -9
  2. package/README.ko.md +10 -9
  3. package/README.md +10 -9
  4. package/README.zh-CN.md +10 -9
  5. package/binding.gyp +23 -16
  6. package/dist/audio/effect-processor.js +9 -3
  7. package/dist/audio/input-sound-file.js +41 -15
  8. package/dist/audio/listener.js +33 -11
  9. package/dist/audio/music.js +41 -18
  10. package/dist/audio/output-sound-file.js +4 -2
  11. package/dist/audio/playback-device.js +11 -3
  12. package/dist/audio/sound-buffer-recorder.js +8 -5
  13. package/dist/audio/sound-buffer.js +40 -17
  14. package/dist/audio/sound-file-factory.js +21 -9
  15. package/dist/audio/sound-file-reader.js +21 -7
  16. package/dist/audio/sound-recorder.js +36 -15
  17. package/dist/audio/sound-source.js +117 -40
  18. package/dist/audio/sound-stream.js +14 -5
  19. package/dist/audio/sound.js +9 -4
  20. package/dist/audio/value.js +42 -16
  21. package/dist/graphics/blend-mode.js +2 -1
  22. package/dist/graphics/boolean-vector.d.ts +1 -2
  23. package/dist/graphics/boolean-vector.js +19 -9
  24. package/dist/graphics/color.js +2 -1
  25. package/dist/graphics/convex-shape.js +7 -3
  26. package/dist/graphics/font.js +52 -23
  27. package/dist/graphics/glsl-matrix.js +8 -3
  28. package/dist/graphics/glsl.js +47 -16
  29. package/dist/graphics/glyph.js +27 -10
  30. package/dist/graphics/image.js +30 -14
  31. package/dist/graphics/rect.js +15 -8
  32. package/dist/graphics/render-states.js +18 -9
  33. package/dist/graphics/render-target.js +49 -23
  34. package/dist/graphics/render-texture.js +24 -12
  35. package/dist/graphics/render-window.js +6 -3
  36. package/dist/graphics/shader.js +67 -32
  37. package/dist/graphics/shape.js +25 -13
  38. package/dist/graphics/sprite.js +19 -9
  39. package/dist/graphics/stencil-mode.js +10 -6
  40. package/dist/graphics/swap.js +2 -1
  41. package/dist/graphics/text.js +123 -48
  42. package/dist/graphics/texture.js +48 -24
  43. package/dist/graphics/transform.js +29 -13
  44. package/dist/graphics/transformable.js +2 -1
  45. package/dist/graphics/vertex-array.js +40 -18
  46. package/dist/graphics/vertex-buffer.js +39 -19
  47. package/dist/graphics/vertex.js +6 -3
  48. package/dist/graphics/view.js +30 -14
  49. package/dist/index.d.ts +4 -4
  50. package/dist/internal/audio-codec-registry.js +12 -6
  51. package/dist/internal/audio-input-stream.js +2 -1
  52. package/dist/internal/audio-music-pump.js +58 -24
  53. package/dist/internal/audio-recorder-session.js +17 -7
  54. package/dist/internal/audio-worker-session.js +28 -10
  55. package/dist/internal/audio-worker.js +40 -21
  56. package/dist/internal/glsl-value.d.ts +3 -1
  57. package/dist/internal/glsl-value.js +18 -8
  58. package/dist/internal/graphics-state.js +29 -15
  59. package/dist/internal/input-stream.d.ts +3 -1
  60. package/dist/internal/input-stream.js +16 -7
  61. package/dist/internal/numeric-properties.js +2 -1
  62. package/dist/internal/render-states.js +2 -1
  63. package/dist/internal/scalar.js +22 -13
  64. package/dist/internal/shaped-glyph.d.ts +3 -1
  65. package/dist/internal/shaped-glyph.js +12 -4
  66. package/dist/internal/struct-view.d.ts +3 -1
  67. package/dist/internal/struct-view.js +3 -1
  68. package/dist/internal/transformable.js +2 -1
  69. package/dist/network/common.js +23 -11
  70. package/dist/network/dns.js +25 -17
  71. package/dist/network/ftp.js +43 -18
  72. package/dist/network/http.js +126 -56
  73. package/dist/network/ip-address.js +50 -25
  74. package/dist/network/packet.js +63 -25
  75. package/dist/network/sftp.js +175 -74
  76. package/dist/network/socket-selector.js +28 -14
  77. package/dist/network/socket.js +71 -30
  78. package/dist/network/tcp-listener.js +42 -19
  79. package/dist/network/tcp-socket.js +111 -53
  80. package/dist/network/tls-material.js +2 -1
  81. package/dist/network/udp-socket.js +68 -33
  82. package/dist/system/angle.js +2 -1
  83. package/dist/system/clock.js +4 -2
  84. package/dist/system/err.js +5 -2
  85. package/dist/system/file-input-stream.js +8 -4
  86. package/dist/system/input-stream.js +6 -3
  87. package/dist/system/operators.d.ts +1 -1
  88. package/dist/system/operators.js +90 -50
  89. package/dist/system/sleep.js +4 -2
  90. package/dist/system/time.js +12 -6
  91. package/dist/system/vector2.js +6 -7
  92. package/dist/system/vector3.js +11 -6
  93. package/dist/window/clipboard.js +2 -1
  94. package/dist/window/context.js +10 -5
  95. package/dist/window/cursor.js +2 -1
  96. package/dist/window/event.js +9 -5
  97. package/dist/window/mouse.js +4 -2
  98. package/dist/window/video-mode.js +2 -1
  99. package/dist/window/window.js +19 -12
  100. package/native/addon.cc +25 -12
  101. package/native/audio-binding.cc +266 -126
  102. package/native/audio-codec-binding.cc +147 -77
  103. package/native/audio-device-binding.cc +76 -49
  104. package/native/audio-file-binding.cc +116 -67
  105. package/native/audio-recorder-binding.cc +147 -98
  106. package/native/audio-recorder.h +1 -1
  107. package/native/audio-state.h +53 -30
  108. package/native/audio-stream-binding.cc +148 -94
  109. package/native/audio-stream.h +5 -4
  110. package/native/audio-worker-binding.cc +163 -81
  111. package/native/audio-worker.h +35 -19
  112. package/native/batch-drawable.cc +29 -16
  113. package/native/batch-drawable.h +11 -8
  114. package/native/context-binding.cc +40 -27
  115. package/native/context-settings.h +15 -8
  116. package/native/deferred-delete.cc +9 -6
  117. package/native/deferred-delete.h +7 -8
  118. package/native/error-stream-binding.cc +42 -28
  119. package/native/font-binding.cc +89 -48
  120. package/native/graphics-binding.cc +122 -72
  121. package/native/graphics-state.h +50 -36
  122. package/native/image-binding.cc +38 -24
  123. package/native/image-state.h +26 -18
  124. package/native/input-binding.cc +42 -27
  125. package/native/render-window-binding.cc +38 -23
  126. package/native/shader-binding.cc +115 -50
  127. package/native/shape-binding.cc +94 -49
  128. package/native/shape-state.h +18 -9
  129. package/native/shaped-glyph-storage.h +9 -5
  130. package/native/stream-binding.cc +143 -117
  131. package/native/system-binding.cc +17 -12
  132. package/native/text-binding.cc +68 -35
  133. package/native/text-state.h +25 -18
  134. package/native/texture-binding.cc +42 -33
  135. package/native/texture-resource.h +20 -12
  136. package/native/time-value.h +3 -1
  137. package/native/transformable-binding.cc +12 -6
  138. package/native/transformable-state.h +6 -6
  139. package/native/value-binding.h +46 -28
  140. package/native/value-layout.h +10 -10
  141. package/native/values-binding.cc +29 -23
  142. package/native/vertex-binding.cc +18 -12
  143. package/native/vertex-buffer-binding.cc +38 -24
  144. package/native/vertex-storage.h +2 -2
  145. package/native/view-data.h +4 -3
  146. package/native/window-binding.cc +75 -45
  147. package/native/window-event.h +20 -11
  148. package/native/window-owner.h +15 -12
  149. package/package.json +44 -51
  150. package/src/audio/effect-processor.ts +9 -3
  151. package/src/audio/input-sound-file.ts +108 -36
  152. package/src/audio/listener.ts +39 -13
  153. package/src/audio/music.ts +112 -47
  154. package/src/audio/output-sound-file.ts +40 -11
  155. package/src/audio/playback-device.ts +23 -8
  156. package/src/audio/sound-buffer-recorder.ts +35 -13
  157. package/src/audio/sound-buffer.ts +166 -58
  158. package/src/audio/sound-file-factory.ts +127 -49
  159. package/src/audio/sound-file-reader.ts +49 -15
  160. package/src/audio/sound-recorder.ts +100 -34
  161. package/src/audio/sound-source.ts +142 -45
  162. package/src/audio/sound-stream.ts +15 -5
  163. package/src/audio/sound.ts +12 -4
  164. package/src/audio/value.ts +89 -26
  165. package/src/graphics/blend-mode.ts +3 -1
  166. package/src/graphics/boolean-vector.ts +56 -26
  167. package/src/graphics/color.ts +3 -1
  168. package/src/graphics/convex-shape.ts +10 -3
  169. package/src/graphics/font.ts +171 -50
  170. package/src/graphics/glsl-matrix.ts +9 -3
  171. package/src/graphics/glsl.ts +81 -35
  172. package/src/graphics/glyph.ts +67 -26
  173. package/src/graphics/image.ts +119 -45
  174. package/src/graphics/rect.ts +56 -24
  175. package/src/graphics/render-states.ts +31 -14
  176. package/src/graphics/render-target.ts +197 -81
  177. package/src/graphics/render-texture.ts +36 -12
  178. package/src/graphics/render-window.ts +9 -3
  179. package/src/graphics/shader.ts +186 -61
  180. package/src/graphics/shape.ts +93 -35
  181. package/src/graphics/sprite.ts +64 -25
  182. package/src/graphics/stencil-mode.ts +12 -6
  183. package/src/graphics/swap.ts +3 -2
  184. package/src/graphics/text.ts +229 -74
  185. package/src/graphics/texture.ts +180 -61
  186. package/src/graphics/transform.ts +132 -47
  187. package/src/graphics/transformable.ts +3 -1
  188. package/src/graphics/vertex-array.ts +87 -35
  189. package/src/graphics/vertex-buffer.ts +97 -33
  190. package/src/graphics/vertex.ts +9 -3
  191. package/src/graphics/view.ts +59 -21
  192. package/src/index.ts +101 -23
  193. package/src/internal/audio-codec-registry.ts +18 -6
  194. package/src/internal/audio-input-stream.ts +6 -2
  195. package/src/internal/audio-music-pump.ts +116 -41
  196. package/src/internal/audio-native.ts +249 -96
  197. package/src/internal/audio-recorder-session.ts +68 -24
  198. package/src/internal/audio-worker-session.ts +65 -21
  199. package/src/internal/audio-worker.ts +85 -41
  200. package/src/internal/glsl-value.ts +26 -11
  201. package/src/internal/graphics-state.ts +57 -20
  202. package/src/internal/input-stream.ts +23 -10
  203. package/src/internal/native.ts +252 -99
  204. package/src/internal/numeric-properties.ts +2 -1
  205. package/src/internal/render-states.ts +3 -1
  206. package/src/internal/scalar.ts +33 -14
  207. package/src/internal/shaped-glyph.ts +25 -12
  208. package/src/internal/struct-view.ts +40 -13
  209. package/src/internal/transformable.ts +3 -1
  210. package/src/network/common.ts +35 -11
  211. package/src/network/dns.ts +95 -38
  212. package/src/network/ftp.ts +129 -47
  213. package/src/network/http.ts +232 -77
  214. package/src/network/ip-address.ts +118 -48
  215. package/src/network/packet.ts +161 -54
  216. package/src/network/sftp.ts +498 -168
  217. package/src/network/socket-selector.ts +66 -26
  218. package/src/network/socket.ts +133 -46
  219. package/src/network/tcp-listener.ts +78 -29
  220. package/src/network/tcp-socket.ts +240 -95
  221. package/src/network/tls-material.ts +6 -2
  222. package/src/network/udp-socket.ts +147 -53
  223. package/src/system/angle.ts +3 -1
  224. package/src/system/clock.ts +12 -4
  225. package/src/system/err.ts +5 -2
  226. package/src/system/file-input-stream.ts +12 -4
  227. package/src/system/input-stream.ts +9 -3
  228. package/src/system/operators.ts +290 -115
  229. package/src/system/sleep.ts +6 -2
  230. package/src/system/time.ts +18 -6
  231. package/src/system/timeout-with-predicate.ts +19 -8
  232. package/src/system/vector2.ts +101 -49
  233. package/src/system/vector3.ts +85 -36
  234. package/src/window/clipboard.ts +3 -1
  235. package/src/window/context.ts +15 -5
  236. package/src/window/cursor.ts +3 -1
  237. package/src/window/event.ts +26 -12
  238. package/src/window/mouse.ts +6 -2
  239. package/src/window/video-mode.ts +3 -1
  240. package/src/window/window.ts +119 -37
  241. package/test/native/audio-probes.cc +74 -36
  242. package/test/native/deferred-delete-probes.cc +17 -11
  243. package/test/native/drawable-probes.cc +70 -36
  244. package/test/native/exception-probes.cc +14 -9
  245. package/test/native/image-probes.cc +20 -10
  246. package/test/native/matrix-probes.cc +8 -4
  247. package/test/native/network-oracle.cc +17 -11
  248. package/test/native/render-texture-probes.cc +33 -24
  249. package/test/native/shader-probes.cc +9 -9
  250. package/test/native/shape-probes.cc +37 -23
  251. package/test/native/sprite-probes.cc +41 -25
  252. package/test/native/system-probes.cc +58 -35
  253. package/test/native/text-probes.cc +77 -44
  254. package/test/native/texture-probes.cc +43 -26
  255. package/test/native/time-probes.cc +18 -12
  256. package/test/native/transform-probes.cc +15 -10
  257. package/test/native/transformable-probes.cc +23 -17
  258. package/test/native/value-oracle.h +9 -4
  259. package/test/native/value-probes.cc +31 -23
  260. package/test/native/vertex-array-probes.cc +22 -14
  261. package/test/native/vertex-buffer-probes.cc +28 -17
  262. package/test/native/view-probes.cc +29 -17
  263. package/test/native/window-probes.cc +66 -33
  264. package/tools/ensure-sfml.cjs +9 -0
  265. package/tools/generate-network-constants.cjs +11 -6
  266. package/tools/generate-window-constants.cjs +31 -12
  267. package/docs/README.ja.md +0 -30
  268. package/docs/README.ko.md +0 -30
  269. package/docs/README.md +0 -30
  270. package/docs/README.zh-CN.md +0 -30
  271. package/docs/api.ja.md +0 -84
  272. package/docs/api.ko.md +0 -84
  273. package/docs/api.md +0 -81
  274. package/docs/api.zh-CN.md +0 -81
  275. package/docs/audio.ja.md +0 -249
  276. package/docs/audio.ko.md +0 -251
  277. package/docs/audio.md +0 -252
  278. package/docs/audio.zh-CN.md +0 -252
  279. package/docs/concepts.ja.md +0 -130
  280. package/docs/concepts.ko.md +0 -130
  281. package/docs/concepts.md +0 -131
  282. package/docs/concepts.zh-CN.md +0 -131
  283. package/docs/development.ja.md +0 -144
  284. package/docs/development.ko.md +0 -144
  285. package/docs/development.md +0 -146
  286. package/docs/development.zh-CN.md +0 -146
  287. package/docs/getting-started.ja.md +0 -154
  288. package/docs/getting-started.ko.md +0 -154
  289. package/docs/getting-started.md +0 -156
  290. package/docs/getting-started.zh-CN.md +0 -156
  291. package/docs/graphics.ja.md +0 -256
  292. package/docs/graphics.ko.md +0 -254
  293. package/docs/graphics.md +0 -255
  294. package/docs/graphics.zh-CN.md +0 -257
  295. package/docs/network.ja.md +0 -214
  296. package/docs/network.ko.md +0 -216
  297. package/docs/network.md +0 -218
  298. package/docs/network.zh-CN.md +0 -218
  299. package/docs/system.ja.md +0 -169
  300. package/docs/system.ko.md +0 -171
  301. package/docs/system.md +0 -173
  302. package/docs/system.zh-CN.md +0 -173
  303. package/docs/window.ja.md +0 -138
  304. package/docs/window.ko.md +0 -140
  305. package/docs/window.md +0 -141
  306. package/docs/window.zh-CN.md +0 -141
  307. package/native/exception-boundary.h +0 -97
  308. package/native/numeric-fields.h +0 -96
  309. package/native/shared-handle.h +0 -107
  310. package/test/tsconfig.json +0 -8
  311. package/tools/configure-lsp.cjs +0 -23
  312. package/tools/lib_downloader.cjs +0 -92
  313. package/tools/lib_downloader_urls.cjs +0 -23
@@ -32,7 +32,10 @@ export enum ReadinessType {
32
32
  * @ko 이번 wait에서 얻은 준비 상태 비트 플래그를 받습니다.
33
33
  */
34
34
  export type ReadyCallback = (readiness: number) => void;
35
- interface Entry { mask: number; callback?: ReadyCallback }
35
+ interface Entry {
36
+ mask: number;
37
+ callback?: ReadyCallback;
38
+ }
36
39
 
37
40
  /**
38
41
  * Wait asynchronously for a set of sockets to become ready,
@@ -69,7 +72,9 @@ export class SocketSelector {
69
72
  * @ko 빈 선택기를 생성하거나 등록 집합, 콜백 및 준비 상태 스냅샷을 복사합니다.
70
73
  */
71
74
  constructor(source?: SocketSelector) {
72
- if (source !== undefined) this.assign(source);
75
+ if (source !== undefined) {
76
+ this.assign(source);
77
+ }
73
78
  }
74
79
  /**
75
80
  * Copy another selector's registrations and snapshot, returning this.
@@ -84,8 +89,12 @@ export class SocketSelector {
84
89
  if (!(source instanceof SocketSelector)) {
85
90
  throw new TypeError('Expected a SocketSelector');
86
91
  }
87
- if (source === this) return this;
88
- if (this.waiting) throw new Error('Finish the pending wait before assigning');
92
+ if (source === this) {
93
+ return this;
94
+ }
95
+ if (this.waiting) {
96
+ throw new Error('Finish the pending wait before assigning');
97
+ }
89
98
  this.entries = new Map(source.entries);
90
99
  this.ready = new Map(source.ready);
91
100
  return this;
@@ -99,15 +108,24 @@ export class SocketSelector {
99
108
  * @ko 활성 소켓과 준비 조건을 등록합니다. 중복되거나 열리지 않았으면 false를
100
109
  * 반환하며 콜백은 명시적으로 dispatch해야 합니다.
101
110
  */
102
- add(socket: Socket, readiness = ReadinessType.Receive,
103
- callback?: ReadyCallback): boolean {
104
- if (!(socket instanceof Socket)) throw new TypeError('Expected a Socket');
111
+ add(
112
+ socket: Socket,
113
+ readiness = ReadinessType.Receive,
114
+ callback?: ReadyCallback,
115
+ ): boolean {
116
+ if (!(socket instanceof Socket)) {
117
+ throw new TypeError('Expected a Socket');
118
+ }
105
119
  uint(readiness, 3);
106
- if (!readiness) throw new RangeError('Expected Receive and/or Send');
120
+ if (!readiness) {
121
+ throw new RangeError('Expected Receive and/or Send');
122
+ }
107
123
  if (callback !== undefined && typeof callback !== 'function') {
108
124
  throw new TypeError('Expected a callback');
109
125
  }
110
- if (!socket[socketAlive]() || this.entries.has(socket)) return false;
126
+ if (!socket[socketAlive]() || this.entries.has(socket)) {
127
+ return false;
128
+ }
111
129
  this.entries.set(socket, { mask: readiness, callback });
112
130
  this.cancel?.abort();
113
131
  return true;
@@ -153,30 +171,48 @@ export class SocketSelector {
153
171
  */
154
172
  async wait(timeout: NetworkTime = Time.Zero): Promise<boolean> {
155
173
  const milliseconds = millisecondsOf(timeout);
156
- if (this.waiting) throw new Error('A selector wait is already pending');
174
+ if (this.waiting) {
175
+ throw new Error('A selector wait is already pending');
176
+ }
157
177
  this.ready.clear();
158
- if (!this.entries.size) return false;
178
+ if (!this.entries.size) {
179
+ return false;
180
+ }
159
181
  this.waiting = true;
160
182
  this.cancel = new AbortController();
161
183
  const events = new EventEmitter();
162
184
  const notify = () => events.emit('change');
163
185
  const sockets = [ ...this.entries.keys() ];
164
- for (const socket of sockets) socket[socketEvents].on('change', notify);
186
+ for (const socket of sockets) {
187
+ socket[socketEvents].on('change', notify);
188
+ }
165
189
  try {
166
- await waitFor(events, () => {
167
- let count = 0;
168
- for (const [ socket, entry ] of this.entries) {
169
- const mask = socket[socketReady](entry.mask);
170
- if (mask) { this.ready.set(socket, mask); count++; } else {
171
- this.ready.delete(socket);
190
+ await waitFor(
191
+ events,
192
+ () => {
193
+ let count = 0;
194
+ for (const [ socket, entry ] of this.entries) {
195
+ const mask = socket[socketReady](entry.mask);
196
+ if (mask) {
197
+ this.ready.set(socket, mask);
198
+ count++;
199
+ } else {
200
+ this.ready.delete(socket);
201
+ }
172
202
  }
173
- }
174
- return count !== 0 || [ ...this.entries.keys() ]
175
- .every(socket => !socket[socketAlive]());
176
- }, milliseconds, this.cancel.signal);
203
+ return (
204
+ count !== 0 ||
205
+ [ ...this.entries.keys() ].every(socket => !socket[socketAlive]())
206
+ );
207
+ },
208
+ milliseconds,
209
+ this.cancel.signal,
210
+ );
177
211
  return this.ready.size > 0;
178
212
  } finally {
179
- for (const socket of sockets) socket[socketEvents].off('change', notify);
213
+ for (const socket of sockets) {
214
+ socket[socketEvents].off('change', notify);
215
+ }
180
216
  this.waiting = false;
181
217
  this.cancel = undefined;
182
218
  }
@@ -199,8 +235,12 @@ export class SocketSelector {
199
235
  * @ko 최근 wait에서 준비된 항목의 등록된 콜백을 동기적으로 호출합니다.
200
236
  */
201
237
  dispatchReadyCallbacks(): void {
202
- const callbacks = [ ...this.ready ].map(([ socket, mask ]) =>
203
- ({ callback: this.entries.get(socket)?.callback, mask }));
204
- for (const { callback, mask } of callbacks) callback?.(mask);
238
+ const callbacks = [ ...this.ready ].map(([ socket, mask ]) => ({
239
+ callback: this.entries.get(socket)?.callback,
240
+ mask,
241
+ }));
242
+ for (const { callback, mask } of callbacks) {
243
+ callback?.(mask);
244
+ }
205
245
  }
206
246
  }
@@ -95,9 +95,15 @@ function closeDatagram(handle: UdpHandle): void {
95
95
  handle.off('close', cleanup);
96
96
  };
97
97
  const close = () => {
98
- try { handle.close(); } catch { cleanup(); }
98
+ try {
99
+ handle.close();
100
+ } catch {
101
+ cleanup();
102
+ }
103
+ };
104
+ retry = () => {
105
+ queueMicrotask(close);
99
106
  };
100
- retry = () => { queueMicrotask(close); };
101
107
  handle.on('error', retry);
102
108
  handle.once('close', cleanup);
103
109
  close();
@@ -173,7 +179,9 @@ export abstract class Socket {
173
179
  * false이면 현재 큐 상태에 따라 NotReady 또는 Partial을 반환합니다.
174
180
  */
175
181
  setBlocking(blocking: boolean): void {
176
- if (typeof blocking !== 'boolean') throw new TypeError('Expected a boolean');
182
+ if (typeof blocking !== 'boolean') {
183
+ throw new TypeError('Expected a boolean');
184
+ }
177
185
  this.blocking = blocking;
178
186
  this.changed();
179
187
  }
@@ -183,20 +191,30 @@ export abstract class Socket {
183
191
  * @ja 非同期待機によるブロッキング動作を使うかどうかを返します。
184
192
  * @ko 비동기 대기를 통한 블로킹 동작을 사용하는지 반환합니다.
185
193
  */
186
- isBlocking(): boolean { return this.blocking; }
194
+ isBlocking(): boolean {
195
+ return this.blocking;
196
+ }
187
197
  /**
188
198
  * Return the latest underlying error, or undefined if none is recorded.
189
199
  * @zh 返回最近的底层错误;未记录错误时为 undefined。
190
200
  * @ja 最新の下位層のエラーを返し、記録がなければ undefined を返します。
191
201
  * @ko 최근 하위 계층 오류를 반환하며 기록된 오류가 없으면 undefined입니다.
192
202
  */
193
- getLastError(): Error | undefined { return this.failure; }
194
- protected changed(): void { this[socketEvents].emit('change'); }
195
- protected async awaitReady(check: () => boolean, generation: number):
196
- Promise<boolean> {
203
+ getLastError(): Error | undefined {
204
+ return this.failure;
205
+ }
206
+ protected changed(): void {
207
+ this[socketEvents].emit('change');
208
+ }
209
+ protected async awaitReady(
210
+ check: () => boolean,
211
+ generation: number,
212
+ ): Promise<boolean> {
197
213
  if (!check() && this.blocking && this.epoch === generation) {
198
- await waitFor(this[socketEvents], () => check() || !this.blocking ||
199
- this.epoch !== generation);
214
+ await waitFor(
215
+ this[socketEvents],
216
+ () => check() || !this.blocking || this.epoch !== generation,
217
+ );
200
218
  }
201
219
  return this.epoch === generation && check();
202
220
  }
@@ -206,14 +224,18 @@ export abstract class Socket {
206
224
  * @ja サブクラスで上書き可能な準備条件を Selector に公開します。
207
225
  * @ko 하위 클래스에서 재정의할 수 있는 준비 조건을 Selector에 제공합니다.
208
226
  */
209
- [socketReady](mask: number): number { return this.getReadiness(mask); }
227
+ [socketReady](mask: number): number {
228
+ return this.getReadiness(mask);
229
+ }
210
230
  /**
211
231
  * Expose overridable liveness state to Selector.
212
232
  * @zh 供 Selector 查询子类可覆盖的存活状态。
213
233
  * @ja サブクラスで上書き可能な生存状態を Selector に公開します。
214
234
  * @ko 하위 클래스에서 재정의할 수 있는 활성 상태를 Selector에 제공합니다.
215
235
  */
216
- [socketAlive](): boolean { return this.isAlive(); }
236
+ [socketAlive](): boolean {
237
+ return this.isAlive();
238
+ }
217
239
  /**
218
240
  * Provide subclasses with the underlying Node transport,
219
241
  * or undefined if not yet created.
@@ -223,7 +245,9 @@ export abstract class Socket {
223
245
  * @ko 하위 클래스에 기반 Node 전송 객체 접근을 제공합니다.
224
246
  * 아직 생성되지 않았으면 undefined입니다.
225
247
  */
226
- protected getNativeHandle(): SocketHandle | undefined { return this.transport; }
248
+ protected getNativeHandle(): SocketHandle | undefined {
249
+ return this.transport;
250
+ }
227
251
  /**
228
252
  * Allocate a transport for the given address family without implicitly
229
253
  * connecting or binding a port.
@@ -244,12 +268,17 @@ export abstract class Socket {
244
268
  * 원시 파일 디스크립터는 인수하지 않습니다.
245
269
  */
246
270
  protected create(handle: SocketHandle): void;
247
- protected create(value: IpAddressType | SocketHandle = IpAddressType.IpV4):
248
- void {
249
- const handle = typeof value === 'number'
250
- ? this.allocateHandle(uint(value, 1)) : value;
271
+ protected create(
272
+ value: IpAddressType | SocketHandle = IpAddressType.IpV4,
273
+ ): void {
274
+ const handle =
275
+ typeof value === 'number'
276
+ ? this.allocateHandle(uint(value, 1))
277
+ : value;
251
278
  this.validateHandle(handle);
252
- if (handle === this.transport) return;
279
+ if (handle === this.transport) {
280
+ return;
281
+ }
253
282
  this.close();
254
283
  this.failure = undefined;
255
284
  this.transport = handle;
@@ -272,15 +301,21 @@ export abstract class Socket {
272
301
  this.detach = undefined;
273
302
  this.transport = undefined;
274
303
  this.onClose();
275
- if (handle instanceof UdpHandle) closeDatagram(handle);
276
- else if (handle) {
304
+ if (handle instanceof UdpHandle) {
305
+ closeDatagram(handle);
306
+ } else if (handle) {
277
307
  // 销毁期间可能产生迟到的错误;不再保留捕获当前实例的监听闭包。
278
308
  const ignore = () => {};
279
309
  handle.on('error', ignore);
280
310
  handle.once('close', () => handle.off('error', ignore));
281
- if (handle instanceof TcpHandle) handle.destroy();
282
- else {
283
- try { handle.close(); } catch { handle.off('error', ignore); }
311
+ if (handle instanceof TcpHandle) {
312
+ handle.destroy();
313
+ } else {
314
+ try {
315
+ handle.close();
316
+ } catch {
317
+ handle.off('error', ignore);
318
+ }
284
319
  }
285
320
  }
286
321
  this.changed();
@@ -295,7 +330,8 @@ export abstract class Socket {
295
330
  * 리스너는 Node Server를 사용하도록 재정의할 수 있습니다.
296
331
  */
297
332
  protected allocateHandle(addressType: IpAddressType): SocketHandle {
298
- return this.type === SocketType.Tcp ? new TcpHandle()
333
+ return this.type === SocketType.Tcp
334
+ ? new TcpHandle()
299
335
  : createSocket(addressType === IpAddressType.IpV4 ? 'udp4' : 'udp6');
300
336
  }
301
337
  /**
@@ -308,8 +344,11 @@ export abstract class Socket {
308
344
  * 전송 객체 타입을 검증합니다.
309
345
  */
310
346
  protected validateHandle(handle: SocketHandle): void {
311
- if (this.type === SocketType.Udp ? !(handle instanceof UdpHandle)
312
- : !(handle instanceof TcpHandle || handle instanceof Server)) {
347
+ if (
348
+ this.type === SocketType.Udp
349
+ ? !(handle instanceof UdpHandle)
350
+ : !(handle instanceof TcpHandle || handle instanceof Server)
351
+ ) {
313
352
  throw new TypeError('Expected a matching Node socket transport');
314
353
  }
315
354
  }
@@ -325,17 +364,36 @@ export abstract class Socket {
325
364
  protected onCreate(): void {
326
365
  const handle = this.transport!;
327
366
  const generation = this.epoch;
328
- const current = () => this.epoch === generation && this.transport === handle;
329
- const notify = () => { if (current()) this.changed(); };
367
+ const current = () =>
368
+ this.epoch === generation && this.transport === handle;
369
+ const notify = () => {
370
+ if (current()) {
371
+ this.changed();
372
+ }
373
+ };
330
374
  const error = (error: Error) => {
331
- if (!current()) return;
332
- this.failure = error; this.changed();
375
+ if (!current()) {
376
+ return;
377
+ }
378
+ this.failure = error;
379
+ this.changed();
333
380
  };
334
- const events = [ 'readable', 'drain', 'connect', 'end', 'close', 'listening' ];
335
- for (const event of events) handle.on(event, notify);
381
+ const events = [
382
+ 'readable',
383
+ 'drain',
384
+ 'connect',
385
+ 'end',
386
+ 'close',
387
+ 'listening',
388
+ ];
389
+ for (const event of events) {
390
+ handle.on(event, notify);
391
+ }
336
392
  handle.on('error', error);
337
393
  this.detach = () => {
338
- for (const event of events) handle.off(event, notify);
394
+ for (const event of events) {
395
+ handle.off(event, notify);
396
+ }
339
397
  handle.off('error', error);
340
398
  };
341
399
  }
@@ -360,10 +418,19 @@ export abstract class Socket {
360
418
  */
361
419
  protected isAlive(): boolean {
362
420
  const handle = this.transport;
363
- if (handle instanceof TcpHandle) return !handle.destroyed;
364
- if (handle instanceof Server) return handle.listening;
421
+ if (handle instanceof TcpHandle) {
422
+ return !handle.destroyed;
423
+ }
424
+ if (handle instanceof Server) {
425
+ return handle.listening;
426
+ }
365
427
  if (handle instanceof UdpHandle) {
366
- try { handle.address(); return true; } catch { return false; }
428
+ try {
429
+ handle.address();
430
+ return true;
431
+ } catch {
432
+ return false;
433
+ }
367
434
  }
368
435
  return false;
369
436
  }
@@ -384,12 +451,25 @@ export abstract class Socket {
384
451
  const handle = this.transport;
385
452
  let ready = 0;
386
453
  if (handle instanceof TcpHandle) {
387
- if (handle.readableLength || handle.readableEnded || handle.destroyed) {
454
+ if (
455
+ handle.readableLength ||
456
+ handle.readableEnded ||
457
+ handle.destroyed
458
+ ) {
388
459
  ready += 1;
389
460
  }
390
- if (handle.remoteAddress && !handle.connecting && !handle.destroyed &&
391
- handle.writable && !handle.writableNeedDrain) ready += 2;
392
- } else if (handle instanceof UdpHandle && this.isAlive()) ready = 2;
461
+ if (
462
+ handle.remoteAddress &&
463
+ !handle.connecting &&
464
+ !handle.destroyed &&
465
+ handle.writable &&
466
+ !handle.writableNeedDrain
467
+ ) {
468
+ ready += 2;
469
+ }
470
+ } else if (handle instanceof UdpHandle && this.isAlive()) {
471
+ ready = 2;
472
+ }
393
473
  // eslint-disable-next-line no-bitwise
394
474
  return ready & mask;
395
475
  }
@@ -402,13 +482,20 @@ export abstract class Socket {
402
482
  * @ko 쓰기 또는 세대 취소를 기다립니다.
403
483
  * 닫을 때 하위 쓰기 콜백의 실행 여부에 의존하지 않습니다.
404
484
  */
405
- protected async awaitWrite(completion: Promise<SocketStatus>,
406
- generation: number): Promise<SocketStatus> {
485
+ protected async awaitWrite(
486
+ completion: Promise<SocketStatus>,
487
+ generation: number,
488
+ ): Promise<SocketStatus> {
407
489
  const cancel = new AbortController();
408
- const closed = waitFor(this[socketEvents],
409
- () => this.epoch !== generation, 0, cancel.signal)
410
- .then(() => SocketStatus.Disconnected);
411
- try { return await Promise.race([ completion, closed ]); } finally {
490
+ const closed = waitFor(
491
+ this[socketEvents],
492
+ () => this.epoch !== generation,
493
+ 0,
494
+ cancel.signal,
495
+ ).then(() => SocketStatus.Disconnected);
496
+ try {
497
+ return await Promise.race([ completion, closed ]);
498
+ } finally {
412
499
  cancel.abort();
413
500
  }
414
501
  }
@@ -1,6 +1,11 @@
1
1
  import { createServer, Server, type Socket as NetSocket } from 'node:net';
2
- import { Socket, SocketStatus as Status,
3
- socketEvents, SocketType, type SocketHandle } from './socket';
2
+ import {
3
+ Socket,
4
+ SocketStatus as Status,
5
+ socketEvents,
6
+ SocketType,
7
+ type SocketHandle,
8
+ } from './socket';
4
9
  import { IpAddress } from './ip-address';
5
10
  import { TcpSocket, adoptTcp } from './tcp-socket';
6
11
  import { SerialQueue, uint, waitFor } from './common';
@@ -28,7 +33,9 @@ export class TcpListener extends Socket {
28
33
  * @ja まだ待ち受けていない TCP リスナーを作成します。
29
34
  * @ko 아직 수신 대기하지 않는 TCP 리스너를 생성합니다.
30
35
  */
31
- constructor() { super(SocketType.Tcp); }
36
+ constructor() {
37
+ super(SocketType.Tcp);
38
+ }
32
39
  /**
33
40
  * Return the listening port, or 0 when not listening.
34
41
  * @zh 返回监听端口;未监听时为 0。
@@ -48,19 +55,32 @@ export class TcpListener extends Socket {
48
55
  * @ko 비동기로 바인딩하고 수신 대기합니다. port가 0이면 자동 할당하며,
49
56
  * 기본적으로 모든 IPv4 인터페이스에 바인딩합니다.
50
57
  */
51
- async listen(port: number, address: IpAddress = IpAddress.Any):
52
- Promise<Status> {
58
+ async listen(
59
+ port: number,
60
+ address: IpAddress = IpAddress.Any,
61
+ ): Promise<Status> {
53
62
  uint(port, 65535);
54
- if (!(address instanceof IpAddress)) throw new TypeError('Expected IpAddress');
63
+ if (!(address instanceof IpAddress)) {
64
+ throw new TypeError('Expected IpAddress');
65
+ }
55
66
  this.create(address.getType());
56
67
  const generation = this.epoch;
57
68
  const server = this.transport!;
58
- const ready = waitFor(this[socketEvents], () => this.listening ||
59
- !!this.failure || generation !== this.epoch);
60
- server.listen({ host: address.toString(), port, ipv6Only: address.isV6() });
69
+ const ready = waitFor(
70
+ this[socketEvents],
71
+ () => this.listening || !!this.failure || generation !== this.epoch,
72
+ );
73
+ server.listen({
74
+ host: address.toString(),
75
+ port,
76
+ ipv6Only: address.isV6(),
77
+ });
61
78
  await ready;
62
- return generation !== this.epoch ? Status.Disconnected
63
- : this.listening ? Status.Done : Status.Error;
79
+ return generation !== this.epoch
80
+ ? Status.Disconnected
81
+ : this.listening
82
+ ? Status.Done
83
+ : Status.Error;
64
84
  }
65
85
  /**
66
86
  * Allocate a Server for the listener without creating a client transport
@@ -81,7 +101,9 @@ export class TcpListener extends Socket {
81
101
  * @ko 이 리스너는 Node Server만 인수합니다.
82
102
  */
83
103
  protected validateHandle(handle: SocketHandle): void {
84
- if (!(handle instanceof Server)) throw new TypeError('Expected Node Server');
104
+ if (!(handle instanceof Server)) {
105
+ throw new TypeError('Expected Node Server');
106
+ }
85
107
  }
86
108
  /**
87
109
  * Set up the accept queue and listening state, supporting adoption
@@ -95,7 +117,8 @@ export class TcpListener extends Socket {
95
117
  protected onCreate(): void {
96
118
  const server = this.transport!;
97
119
  const generation = this.epoch;
98
- const current = () => server === this.transport && generation === this.epoch;
120
+ const current = () =>
121
+ server === this.transport && generation === this.epoch;
99
122
  this.listening = server.listening;
100
123
  const connection = (socket: NetSocket) => {
101
124
  if (!current() || this.pending.length >= 128) {
@@ -107,25 +130,35 @@ export class TcpListener extends Socket {
107
130
  this.changed();
108
131
  };
109
132
  const error = (error: Error) => {
110
- if (!current()) return;
133
+ if (!current()) {
134
+ return;
135
+ }
111
136
  this.failure = error;
112
137
  this.listening = false;
113
138
  this.changed();
114
139
  };
115
140
  const listening = () => {
116
- if (!current()) return;
117
- this.listening = true; this.changed();
141
+ if (!current()) {
142
+ return;
143
+ }
144
+ this.listening = true;
145
+ this.changed();
118
146
  };
119
147
  const closed = () => {
120
- if (!current()) return;
121
- this.listening = false; this.changed();
148
+ if (!current()) {
149
+ return;
150
+ }
151
+ this.listening = false;
152
+ this.changed();
122
153
  };
123
- server.on('connection', connection)
154
+ server
155
+ .on('connection', connection)
124
156
  .on('error', error)
125
157
  .on('listening', listening)
126
158
  .on('close', closed);
127
159
  this.detach = () => {
128
- server.off('connection', connection)
160
+ server
161
+ .off('connection', connection)
129
162
  .off('error', error)
130
163
  .off('listening', listening)
131
164
  .off('close', closed);
@@ -137,7 +170,9 @@ export class TcpListener extends Socket {
137
170
  * @ja 待ち受けを停止し、まだ accept されていない接続を閉じます。
138
171
  * @ko 수신 대기를 중지하고 아직 accept하지 않은 연결을 닫습니다.
139
172
  */
140
- close(): void { super.close(); }
173
+ close(): void {
174
+ super.close();
175
+ }
141
176
  /**
142
177
  * Clear connections from the previous generation that were not accepted.
143
178
  * @zh 清空旧代际中尚未接受的连接。
@@ -146,7 +181,9 @@ export class TcpListener extends Socket {
146
181
  */
147
182
  protected onClose(): void {
148
183
  this.listening = false;
149
- for (const socket of this.pending) socket.destroy();
184
+ for (const socket of this.pending) {
185
+ socket.destroy();
186
+ }
150
187
  this.pending = [];
151
188
  }
152
189
  /**
@@ -160,14 +197,22 @@ export class TcpListener extends Socket {
160
197
  * 논블로킹 모드에서 연결이 없으면 NotReady를 반환합니다.
161
198
  */
162
199
  accept(socket: TcpSocket): Promise<Status> {
163
- if (!(socket instanceof TcpSocket)) throw new TypeError('Expected TcpSocket');
200
+ if (!(socket instanceof TcpSocket)) {
201
+ throw new TypeError('Expected TcpSocket');
202
+ }
164
203
  const generation = this.epoch;
165
204
  return this.accepts.run(async () => {
166
- await this.awaitReady(() => this.pending.length > 0 || !this.listening,
167
- generation);
168
- if (generation !== this.epoch || !this.listening) return Status.Error;
205
+ await this.awaitReady(
206
+ () => this.pending.length > 0 || !this.listening,
207
+ generation,
208
+ );
209
+ if (generation !== this.epoch || !this.listening) {
210
+ return Status.Error;
211
+ }
169
212
  const accepted = this.pending.shift();
170
- if (!accepted) return Status.NotReady;
213
+ if (!accepted) {
214
+ return Status.NotReady;
215
+ }
171
216
  socket[adoptTcp](accepted);
172
217
  return Status.Done;
173
218
  });
@@ -178,7 +223,9 @@ export class TcpListener extends Socket {
178
223
  * @ja 待ち受け状態を返します。派生クラスで拡張できます。
179
224
  * @ko 수신 대기 상태를 반환하며 파생 클래스에서 확장할 수 있습니다.
180
225
  */
181
- protected isAlive(): boolean { return this.listening; }
226
+ protected isAlive(): boolean {
227
+ return this.listening;
228
+ }
182
229
  /**
183
230
  * Report receive readiness when the accept queue is not empty.
184
231
  * @zh 接受队列非空时报告接收就绪。
@@ -189,5 +236,7 @@ export class TcpListener extends Socket {
189
236
  // eslint-disable-next-line no-bitwise
190
237
  return this.pending.length ? mask & 1 : 0;
191
238
  }
192
- protected getNativeHandle(): Server | undefined { return this.transport; }
239
+ protected getNativeHandle(): Server | undefined {
240
+ return this.transport;
241
+ }
193
242
  }