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
@@ -1,12 +1,27 @@
1
1
  import { Socket as NetSocket } from 'node:net';
2
- import { TLSSocket, connect as tlsConnect, createSecureContext } from 'node:tls';
2
+ import {
3
+ TLSSocket,
4
+ connect as tlsConnect,
5
+ createSecureContext,
6
+ } from 'node:tls';
3
7
  import { Time } from '../system/time';
4
8
  import { IpAddress } from './ip-address';
5
9
  import { Packet, packetReceive, packetSend } from './packet';
6
- import { Socket, SocketStatus as Status,
7
- socketEvents, SocketType, type SocketHandle } from './socket';
8
- import { SerialQueue, bytes, millisecondsOf, uint, waitFor,
9
- type NetworkTime } from './common';
10
+ import {
11
+ Socket,
12
+ SocketStatus as Status,
13
+ socketEvents,
14
+ SocketType,
15
+ type SocketHandle,
16
+ } from './socket';
17
+ import {
18
+ SerialQueue,
19
+ bytes,
20
+ millisecondsOf,
21
+ uint,
22
+ waitFor,
23
+ type NetworkTime,
24
+ } from './common';
10
25
  import { tlsCertificate, tlsPrivateKey } from './tls-material';
11
26
 
12
27
  /**
@@ -143,14 +158,18 @@ export class TcpSocket extends Socket {
143
158
  * @ko 연결되지 않은 TCP 소켓을 생성하며 기본적으로 비동기 블로킹 동작을
144
159
  * 사용합니다.
145
160
  */
146
- constructor() { super(SocketType.Tcp); }
161
+ constructor() {
162
+ super(SocketType.Tcp);
163
+ }
147
164
  /**
148
165
  * Return the local port, or 0 if not yet assigned.
149
166
  * @zh 返回本地端口,尚未分配时为 0。
150
167
  * @ja ローカルポートを返し、未割り当てなら 0 を返します。
151
168
  * @ko 로컬 포트를 반환하며 아직 할당되지 않았으면 0입니다.
152
169
  */
153
- getLocalPort(): number { return this.transport?.localPort ?? 0; }
170
+ getLocalPort(): number {
171
+ return this.transport?.localPort ?? 0;
172
+ }
154
173
  /**
155
174
  * Return the peer address, or undefined when not connected.
156
175
  * @zh 返回对端地址,未连接时为 undefined。
@@ -158,7 +177,9 @@ export class TcpSocket extends Socket {
158
177
  * @ko 상대 주소를 반환하며 연결되지 않았으면 undefined입니다.
159
178
  */
160
179
  getRemoteAddress(): IpAddress | undefined {
161
- const address = this.connected ? this.transport?.remoteAddress : undefined;
180
+ const address = this.connected
181
+ ? this.transport?.remoteAddress
182
+ : undefined;
162
183
  return address ? IpAddress.fromString(address) : undefined;
163
184
  }
164
185
  /**
@@ -168,7 +189,7 @@ export class TcpSocket extends Socket {
168
189
  * @ko 상대 포트를 반환하며 연결되지 않았으면 0입니다.
169
190
  */
170
191
  getRemotePort(): number {
171
- return this.connected ? this.transport?.remotePort ?? 0 : 0;
192
+ return this.connected ? (this.transport?.remotePort ?? 0) : 0;
172
193
  }
173
194
  /**
174
195
  * Start a TCP connection; wait for completion by default, or return NotReady
@@ -180,30 +201,47 @@ export class TcpSocket extends Socket {
180
201
  * @ko TCP 연결을 시작합니다. 기본적으로 완료를 기다리며 논블로킹 모드에서는
181
202
  * NotReady를 반환합니다. Time.Zero는 제한 시간이 없습니다.
182
203
  */
183
- async connect(address: IpAddress, port: number,
184
- timeout: NetworkTime = Time.Zero): Promise<Status> {
185
- if (!(address instanceof IpAddress)) throw new TypeError('Expected IpAddress');
204
+ async connect(
205
+ address: IpAddress,
206
+ port: number,
207
+ timeout: NetworkTime = Time.Zero,
208
+ ): Promise<Status> {
209
+ if (!(address instanceof IpAddress)) {
210
+ throw new TypeError('Expected IpAddress');
211
+ }
186
212
  uint(port, 65535);
187
213
  const milliseconds = millisecondsOf(timeout);
188
214
  this.create(address.getType());
189
215
  const generation = this.epoch;
190
216
  const socket = this.transport!;
191
217
  let timer: NodeJS.Timeout | undefined;
192
- const done = waitFor(this[socketEvents], () => this.connected || this.ended ||
193
- this.epoch !== generation).then(() => {
194
- if (timer) clearTimeout(timer);
195
- return this.epoch !== generation ? Status.Disconnected
196
- : this.connected ? Status.Done : Status.Error;
218
+ const done = waitFor(
219
+ this[socketEvents],
220
+ () => this.connected || this.ended || this.epoch !== generation,
221
+ ).then(() => {
222
+ if (timer) {
223
+ clearTimeout(timer);
224
+ }
225
+ return this.epoch !== generation
226
+ ? Status.Disconnected
227
+ : this.connected
228
+ ? Status.Done
229
+ : Status.Error;
197
230
  });
198
231
  if (milliseconds) {
199
232
  timer = setTimeout(() => {
200
- if (this.epoch !== generation) return;
233
+ if (this.epoch !== generation) {
234
+ return;
235
+ }
201
236
  this.failure = new Error('Connection timed out');
202
237
  socket.destroy(this.failure);
203
238
  }, milliseconds);
204
239
  }
205
- socket.connect({ host: address.toString(), port,
206
- family: address.isV4() ? 4 : 6 });
240
+ socket.connect({
241
+ host: address.toString(),
242
+ port,
243
+ family: address.isV4() ? 4 : 6,
244
+ });
207
245
  return this.blocking ? done : Status.NotReady;
208
246
  }
209
247
  /**
@@ -212,7 +250,9 @@ export class TcpSocket extends Socket {
212
250
  * @ja 接続を即座に閉じ、未完了のパケット送受信状態を消去します。
213
251
  * @ko 즉시 연결을 닫고 미완료 패킷 송수신 상태를 지웁니다.
214
252
  */
215
- disconnect(): void { this.close(); }
253
+ disconnect(): void {
254
+ this.close();
255
+ }
216
256
  /**
217
257
  * Reject UDP or listener transports before adoption.
218
258
  * @zh 接管前拒绝 UDP 或监听传输。
@@ -220,7 +260,9 @@ export class TcpSocket extends Socket {
220
260
  * @ko 인수 전에 UDP 또는 리스너 전송 객체를 거부합니다.
221
261
  */
222
262
  protected validateHandle(handle: SocketHandle): void {
223
- if (!(handle instanceof NetSocket)) throw new TypeError('Expected Node TCP');
263
+ if (!(handle instanceof NetSocket)) {
264
+ throw new TypeError('Expected Node TCP');
265
+ }
224
266
  }
225
267
  /**
226
268
  * Integrate the handle allocated or adopted by protected create into
@@ -270,14 +312,26 @@ export class TcpSocket extends Socket {
270
312
  * @ko 유휴 연결을 TLS 클라이언트로 업그레이드합니다.
271
313
  * 기본적으로 검증하며 CA 바이트는 PEM 또는 DER를 지원합니다.
272
314
  */
273
- async setupTlsClient(hostname: string,
274
- verifyOrCa: boolean | string | Uint8Array = true): Promise<TlsStatus> {
275
- if (typeof hostname !== 'string') throw new TypeError('Expected hostname');
276
- return this.setupTls(() => tlsConnect({
277
- socket: this.transport, servername: hostname,
278
- rejectUnauthorized: verifyOrCa !== false,
279
- ca: typeof verifyOrCa === 'boolean' ? undefined : tlsCertificate(verifyOrCa),
280
- }), 'secureConnect');
315
+ async setupTlsClient(
316
+ hostname: string,
317
+ verifyOrCa: boolean | string | Uint8Array = true,
318
+ ): Promise<TlsStatus> {
319
+ if (typeof hostname !== 'string') {
320
+ throw new TypeError('Expected hostname');
321
+ }
322
+ return this.setupTls(
323
+ () =>
324
+ tlsConnect({
325
+ socket: this.transport,
326
+ servername: hostname,
327
+ rejectUnauthorized: verifyOrCa !== false,
328
+ ca:
329
+ typeof verifyOrCa === 'boolean'
330
+ ? undefined
331
+ : tlsCertificate(verifyOrCa),
332
+ }),
333
+ 'secureConnect',
334
+ );
281
335
  }
282
336
  /**
283
337
  * Upgrade to a TLS server using a PEM/DER certificate and private key;
@@ -288,16 +342,22 @@ export class TcpSocket extends Socket {
288
342
  * @ko PEM/DER 인증서와 개인 키로 TLS 서버로 업그레이드합니다.
289
343
  * 암호 바이트는 원시 내용과 하위 뷰 범위를 유지합니다.
290
344
  */
291
- async setupTlsServer(certificate: string | Uint8Array,
292
- key: string | Uint8Array, password: string | Uint8Array = ''):
293
- Promise<TlsStatus> {
294
- return this.setupTls(() => new TLSSocket(this.transport!, {
295
- isServer: true,
296
- secureContext: createSecureContext({
297
- cert: tlsCertificate(certificate),
298
- key: tlsPrivateKey(key, password),
299
- }),
300
- }), 'secure');
345
+ async setupTlsServer(
346
+ certificate: string | Uint8Array,
347
+ key: string | Uint8Array,
348
+ password: string | Uint8Array = '',
349
+ ): Promise<TlsStatus> {
350
+ return this.setupTls(
351
+ () =>
352
+ new TLSSocket(this.transport!, {
353
+ isServer: true,
354
+ secureContext: createSecureContext({
355
+ cert: tlsCertificate(certificate),
356
+ key: tlsPrivateKey(key, password),
357
+ }),
358
+ }),
359
+ 'secure',
360
+ );
301
361
  }
302
362
  /**
303
363
  * Return the cipher suite name negotiated during the handshake,
@@ -311,8 +371,9 @@ export class TcpSocket extends Socket {
311
371
  getCurrentCiphersuiteName(): string | undefined {
312
372
  return this.tlsStatus === TlsStatus.HandshakeComplete &&
313
373
  this.transport instanceof TLSSocket
314
- ? this.transport.getCipher()?.standardName ??
315
- this.transport.getCipher()?.name : undefined;
374
+ ? (this.transport.getCipher()?.standardName ??
375
+ this.transport.getCipher()?.name)
376
+ : undefined;
316
377
  }
317
378
  /**
318
379
  * Send a call-time packet snapshot with an automatic network-order u32 length
@@ -337,9 +398,12 @@ export class TcpSocket extends Socket {
337
398
  send(data: Packet | Uint8Array): Promise<Status | SocketSendResult> {
338
399
  const generation = this.epoch;
339
400
  // Snapshot at call time, including for nonblocking writes accepted by Node.
340
- const snapshot = data instanceof Packet
341
- ? this.outgoing?.packet === data ? undefined : Buffer.from(data[packetSend]())
342
- : Buffer.from(bytes(data));
401
+ const snapshot =
402
+ data instanceof Packet
403
+ ? this.outgoing?.packet === data
404
+ ? undefined
405
+ : Buffer.from(data[packetSend]())
406
+ : Buffer.from(bytes(data));
343
407
  return this.writes.run(async () => {
344
408
  const packet = data instanceof Packet ? data : undefined;
345
409
  const result = (status: Status, sent = 0) =>
@@ -362,16 +426,23 @@ export class TcpSocket extends Socket {
362
426
  this.outgoing = { packet, data: wire, position: 0 };
363
427
  }
364
428
  const input = this.outgoing
365
- ? this.outgoing.data.subarray(this.outgoing.position) : snapshot!;
429
+ ? this.outgoing.data.subarray(this.outgoing.position)
430
+ : snapshot!;
366
431
  const socket = this.transport!;
367
- if (!input.length) return result(Status.Done);
368
- const room = this.blocking ? input.length
432
+ if (!input.length) {
433
+ return result(Status.Done);
434
+ }
435
+ const room = this.blocking
436
+ ? input.length
369
437
  : Math.max(0, socket.writableHighWaterMark - socket.writableLength);
370
438
  const count = Math.min(input.length, room);
371
- if (!count) return result(Status.NotReady);
439
+ if (!count) {
440
+ return result(Status.NotReady);
441
+ }
372
442
  const completion = new Promise<Status>(resolve => {
373
443
  socket.write(input.subarray(0, count), error =>
374
- resolve(error ? Status.Error : Status.Done));
444
+ resolve(error ? Status.Error : Status.Done),
445
+ );
375
446
  });
376
447
  if (this.outgoing) {
377
448
  this.outgoing.position += count;
@@ -380,10 +451,17 @@ export class TcpSocket extends Socket {
380
451
  }
381
452
  }
382
453
  const status = this.blocking
383
- ? await this.awaitWrite(completion, generation) : Status.Done;
384
- if (generation !== this.epoch) return result(Status.Disconnected, count);
385
- return result(status === Status.Done && count < input.length
386
- ? Status.Partial : status, count);
454
+ ? await this.awaitWrite(completion, generation)
455
+ : Status.Done;
456
+ if (generation !== this.epoch) {
457
+ return result(Status.Disconnected, count);
458
+ }
459
+ return result(
460
+ status === Status.Done && count < input.length
461
+ ? Status.Partial
462
+ : status,
463
+ count,
464
+ );
387
465
  });
388
466
  }
389
467
  /**
@@ -407,30 +485,41 @@ export class TcpSocket extends Socket {
407
485
  * 완료 전에 대상 버퍼를 수정하지 마세요.
408
486
  */
409
487
  receive(data: Uint8Array): Promise<SocketTransfer>;
410
- receive(destination: Packet | Uint8Array): Promise<Status | SocketTransfer> {
411
- const output = destination instanceof Packet ? undefined : bytes(destination);
488
+ receive(
489
+ destination: Packet | Uint8Array,
490
+ ): Promise<Status | SocketTransfer> {
491
+ const output =
492
+ destination instanceof Packet ? undefined : bytes(destination);
412
493
  const generation = this.epoch;
413
494
  return this.reads.run(async () => {
414
495
  if (destination instanceof Packet) {
415
496
  for (;;) {
416
497
  const header = this.packetSize === undefined;
417
- const needed = header ? 4 - this.prefixSize
498
+ const needed = header
499
+ ? 4 - this.prefixSize
418
500
  : this.packetSize! - this.packetBytes;
419
501
  if (!needed && !header) {
420
- const payload = Buffer.concat(this.packetChunks, this.packetBytes);
502
+ const payload = Buffer.concat(
503
+ this.packetChunks,
504
+ this.packetBytes,
505
+ );
421
506
  this.resetPacket();
422
507
  destination[packetReceive](payload);
423
508
  return Status.Done;
424
509
  }
425
510
  const chunk = await this.readChunk(needed, generation);
426
- if (typeof chunk === 'number') return chunk;
511
+ if (typeof chunk === 'number') {
512
+ return chunk;
513
+ }
427
514
  if (header) {
428
515
  chunk.copy(this.prefix, this.prefixSize);
429
516
  this.prefixSize += chunk.length;
430
517
  if (this.prefixSize === 4) {
431
518
  this.packetSize = this.prefix.readUInt32BE();
432
519
  if (this.packetSize > 64 * 1024 * 1024) {
433
- this.failure = new Error('Packet exceeds 64 MiB receive limit');
520
+ this.failure = new Error(
521
+ 'Packet exceeds 64 MiB receive limit',
522
+ );
434
523
  this.disconnect();
435
524
  return Status.Error;
436
525
  }
@@ -444,9 +533,13 @@ export class TcpSocket extends Socket {
444
533
  if (this.prefixSize || this.packetSize !== undefined) {
445
534
  throw new Error('Finish receiving the pending Packet first');
446
535
  }
447
- if (!output!.length) return { status: Status.Error, received: 0 };
536
+ if (!output!.length) {
537
+ return { status: Status.Error, received: 0 };
538
+ }
448
539
  const chunk = await this.readChunk(output!.length, generation);
449
- if (typeof chunk === 'number') return { status: chunk, received: 0 };
540
+ if (typeof chunk === 'number') {
541
+ return { status: chunk, received: 0 };
542
+ }
450
543
  chunk.copy(output!);
451
544
  return { status: Status.Done, received: chunk.length };
452
545
  });
@@ -457,7 +550,9 @@ export class TcpSocket extends Socket {
457
550
  * @ja TCP トランスポートの生存状態を返します。派生クラスで拡張できます。
458
551
  * @ko TCP 전송 객체의 활성 상태를 반환하며 파생 클래스에서 확장할 수 있습니다.
459
552
  */
460
- protected isAlive(): boolean { return !!this.transport && !this.ended; }
553
+ protected isAlive(): boolean {
554
+ return !!this.transport && !this.ended;
555
+ }
461
556
  /**
462
557
  * Return readiness bits for receiving, sending, and the TLS handshake.
463
558
  * @zh 返回接收、发送及 TLS 握手对应的就绪位。
@@ -466,14 +561,22 @@ export class TcpSocket extends Socket {
466
561
  */
467
562
  protected getReadiness(mask: number): number {
468
563
  const socket = this.transport;
469
- const receive = socket && (socket.readableLength > 0 || this.ended) ? 1 : 0;
470
- const send = this.connected && socket && !socket.destroyed &&
471
- !socket.writableNeedDrain && this.tlsStatus !== TlsStatus.HandshakeStarted
472
- ? 2 : 0;
564
+ const receive =
565
+ socket && (socket.readableLength > 0 || this.ended) ? 1 : 0;
566
+ const send =
567
+ this.connected &&
568
+ socket &&
569
+ !socket.destroyed &&
570
+ !socket.writableNeedDrain &&
571
+ this.tlsStatus !== TlsStatus.HandshakeStarted
572
+ ? 2
573
+ : 0;
473
574
  // eslint-disable-next-line no-bitwise
474
575
  return (receive | send) & mask;
475
576
  }
476
- protected getNativeHandle(): NetSocket | undefined { return this.transport; }
577
+ protected getNativeHandle(): NetSocket | undefined {
578
+ return this.transport;
579
+ }
477
580
  /** 重置 Packet 分帧累积状态。 */
478
581
  private resetPacket(): void {
479
582
  this.prefixSize = 0;
@@ -482,16 +585,24 @@ export class TcpSocket extends Socket {
482
585
  this.packetBytes = 0;
483
586
  }
484
587
  /** 异步读取最多 max 字节,含代际校验。 */
485
- private async readChunk(max: number, generation: number):
486
- Promise<Buffer | Status> {
487
- const available = () => this.ended ||
488
- (this.transport?.readableLength ?? 0) > 0;
588
+ private async readChunk(
589
+ max: number,
590
+ generation: number,
591
+ ): Promise<Buffer | Status> {
592
+ const available = () =>
593
+ this.ended || (this.transport?.readableLength ?? 0) > 0;
489
594
  await this.awaitReady(available, generation);
490
- if (generation !== this.epoch) return Status.Disconnected;
595
+ if (generation !== this.epoch) {
596
+ return Status.Disconnected;
597
+ }
491
598
  const socket = this.transport;
492
599
  const count = Math.min(max, socket?.readableLength ?? 0);
493
- if (count) return socket!.read(count) as Buffer;
494
- if (this.ended) return this.failure ? Status.Error : Status.Disconnected;
600
+ if (count) {
601
+ return socket!.read(count) as Buffer;
602
+ }
603
+ if (this.ended) {
604
+ return this.failure ? Status.Error : Status.Disconnected;
605
+ }
495
606
  return Status.NotReady;
496
607
  }
497
608
  /** 绑定套接字事件监听并设置 TCP_NODELAY。 */
@@ -501,28 +612,45 @@ export class TcpSocket extends Socket {
501
612
  this.ended = socket.destroyed || socket.readableEnded;
502
613
  socket.setNoDelay(true);
503
614
  const generation = this.epoch;
504
- const current = () => socket === this.transport && generation === this.epoch;
505
- const readable = () => { if (current()) this.changed(); };
615
+ const current = () =>
616
+ socket === this.transport && generation === this.epoch;
617
+ const readable = () => {
618
+ if (current()) {
619
+ this.changed();
620
+ }
621
+ };
506
622
  const connected = () => {
507
- if (!current()) return;
508
- this.connected = true; this.changed();
623
+ if (!current()) {
624
+ return;
625
+ }
626
+ this.connected = true;
627
+ this.changed();
509
628
  };
510
629
  const ended = () => {
511
- if (!current()) return;
512
- this.connected = false; this.ended = true; this.changed();
630
+ if (!current()) {
631
+ return;
632
+ }
633
+ this.connected = false;
634
+ this.ended = true;
635
+ this.changed();
513
636
  };
514
637
  const error = (error: Error) => {
515
- if (!current()) return;
516
- this.failure = error; ended();
638
+ if (!current()) {
639
+ return;
640
+ }
641
+ this.failure = error;
642
+ ended();
517
643
  };
518
- socket.on('readable', readable)
644
+ socket
645
+ .on('readable', readable)
519
646
  .on('drain', readable)
520
647
  .on('connect', connected)
521
648
  .on('end', ended)
522
649
  .on('close', ended)
523
650
  .on('error', error);
524
651
  this.detach = () => {
525
- socket.off('readable', readable)
652
+ socket
653
+ .off('readable', readable)
526
654
  .off('drain', readable)
527
655
  .off('connect', connected)
528
656
  .off('end', ended)
@@ -531,12 +659,19 @@ export class TcpSocket extends Socket {
531
659
  };
532
660
  }
533
661
  /** 将空闲连接升级为 TLS 并等待握手完成。 */
534
- private async setupTls(create: () => TLSSocket, event: string):
535
- Promise<TlsStatus> {
536
- if (!this.connected) return TlsStatus.NotConnected;
662
+ private async setupTls(
663
+ create: () => TLSSocket,
664
+ event: string,
665
+ ): Promise<TlsStatus> {
666
+ if (!this.connected) {
667
+ return TlsStatus.NotConnected;
668
+ }
537
669
  const generation = this.epoch;
538
670
  if (this.tlsStatus === TlsStatus.NotConnected) {
539
- if (this.transport!.readableLength || this.transport!.writableLength) {
671
+ if (
672
+ this.transport!.readableLength ||
673
+ this.transport!.writableLength
674
+ ) {
540
675
  throw new Error('TLS upgrade requires an empty plaintext queue');
541
676
  }
542
677
  try {
@@ -545,13 +680,18 @@ export class TcpSocket extends Socket {
545
680
  this.attach(socket);
546
681
  this.tlsStatus = TlsStatus.HandshakeStarted;
547
682
  const secure = () => {
548
- if (socket !== this.transport || generation !== this.epoch) return;
683
+ if (socket !== this.transport || generation !== this.epoch) {
684
+ return;
685
+ }
549
686
  this.tlsStatus = TlsStatus.HandshakeComplete;
550
687
  this.changed();
551
688
  };
552
689
  socket.once(event, secure);
553
690
  const detach = this.detach;
554
- this.detach = () => { detach?.(); socket.off(event, secure); };
691
+ this.detach = () => {
692
+ detach?.();
693
+ socket.off(event, secure);
694
+ };
555
695
  } catch (error) {
556
696
  this.failure = error as Error;
557
697
  this.disconnect();
@@ -559,11 +699,16 @@ export class TcpSocket extends Socket {
559
699
  }
560
700
  }
561
701
  if (this.blocking && this.tlsStatus === TlsStatus.HandshakeStarted) {
562
- await waitFor(this[socketEvents], () => this.ended ||
563
- this.tlsStatus !== TlsStatus.HandshakeStarted ||
564
- generation !== this.epoch);
702
+ await waitFor(
703
+ this[socketEvents],
704
+ () =>
705
+ this.ended ||
706
+ this.tlsStatus !== TlsStatus.HandshakeStarted ||
707
+ generation !== this.epoch,
708
+ );
565
709
  }
566
- return this.ended || generation !== this.epoch ? TlsStatus.Error
710
+ return this.ended || generation !== this.epoch
711
+ ? TlsStatus.Error
567
712
  : this.tlsStatus;
568
713
  }
569
714
  }
@@ -11,7 +11,9 @@ import { bytes } from './common';
11
11
  * Node TLS가 허용하는 PEM으로 변환합니다.
12
12
  */
13
13
  export function tlsCertificate(input: string | Uint8Array): string | Buffer {
14
- if (typeof input === 'string') return input;
14
+ if (typeof input === 'string') {
15
+ return input;
16
+ }
15
17
  const data = bytes(input);
16
18
  return data.includes('-----BEGIN CERTIFICATE-----')
17
19
  ? data : new X509Certificate(data).toString();
@@ -40,7 +42,9 @@ export function tlsPrivateKey(input: string | Uint8Array,
40
42
  try {
41
43
  return createPrivateKey({ key, format: 'der', type, passphrase })
42
44
  .export({ format: 'pem', type: 'pkcs8' });
43
- } catch (error) { failure ??= error; }
45
+ } catch (error) {
46
+ failure ??= error;
47
+ }
44
48
  }
45
49
  throw failure;
46
50
  }