sfml.js 1.0.0-rc.2 → 1.1.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 (314) hide show
  1. package/README.ja.md +16 -11
  2. package/README.ko.md +16 -11
  3. package/README.md +16 -11
  4. package/README.zh-CN.md +16 -11
  5. package/assets/breakout.png +0 -0
  6. package/binding.gyp +23 -16
  7. package/dist/audio/effect-processor.js +9 -3
  8. package/dist/audio/input-sound-file.js +41 -15
  9. package/dist/audio/listener.js +33 -11
  10. package/dist/audio/music.js +41 -18
  11. package/dist/audio/output-sound-file.js +4 -2
  12. package/dist/audio/playback-device.js +11 -3
  13. package/dist/audio/sound-buffer-recorder.js +8 -5
  14. package/dist/audio/sound-buffer.js +40 -17
  15. package/dist/audio/sound-file-factory.js +21 -9
  16. package/dist/audio/sound-file-reader.js +21 -7
  17. package/dist/audio/sound-recorder.js +36 -15
  18. package/dist/audio/sound-source.js +117 -40
  19. package/dist/audio/sound-stream.js +14 -5
  20. package/dist/audio/sound.js +9 -4
  21. package/dist/audio/value.js +42 -16
  22. package/dist/graphics/blend-mode.js +2 -1
  23. package/dist/graphics/boolean-vector.d.ts +1 -2
  24. package/dist/graphics/boolean-vector.js +19 -9
  25. package/dist/graphics/color.js +2 -1
  26. package/dist/graphics/convex-shape.js +7 -3
  27. package/dist/graphics/font.js +52 -23
  28. package/dist/graphics/glsl-matrix.js +8 -3
  29. package/dist/graphics/glsl.js +47 -16
  30. package/dist/graphics/glyph.js +27 -10
  31. package/dist/graphics/image.js +30 -14
  32. package/dist/graphics/rect.js +15 -8
  33. package/dist/graphics/render-states.js +18 -9
  34. package/dist/graphics/render-target.js +49 -23
  35. package/dist/graphics/render-texture.js +24 -12
  36. package/dist/graphics/render-window.js +6 -3
  37. package/dist/graphics/shader.js +67 -32
  38. package/dist/graphics/shape.js +25 -13
  39. package/dist/graphics/sprite.js +19 -9
  40. package/dist/graphics/stencil-mode.js +10 -6
  41. package/dist/graphics/swap.js +2 -1
  42. package/dist/graphics/text.js +123 -48
  43. package/dist/graphics/texture.js +48 -24
  44. package/dist/graphics/transform.js +29 -13
  45. package/dist/graphics/transformable.js +2 -1
  46. package/dist/graphics/vertex-array.js +40 -18
  47. package/dist/graphics/vertex-buffer.js +39 -19
  48. package/dist/graphics/vertex.js +6 -3
  49. package/dist/graphics/view.js +30 -14
  50. package/dist/index.d.ts +4 -4
  51. package/dist/internal/audio-codec-registry.js +12 -6
  52. package/dist/internal/audio-input-stream.js +2 -1
  53. package/dist/internal/audio-music-pump.js +58 -24
  54. package/dist/internal/audio-recorder-session.js +17 -7
  55. package/dist/internal/audio-worker-session.js +28 -10
  56. package/dist/internal/audio-worker.js +40 -21
  57. package/dist/internal/glsl-value.d.ts +3 -1
  58. package/dist/internal/glsl-value.js +18 -8
  59. package/dist/internal/graphics-state.js +29 -15
  60. package/dist/internal/input-stream.d.ts +3 -1
  61. package/dist/internal/input-stream.js +16 -7
  62. package/dist/internal/numeric-properties.js +2 -1
  63. package/dist/internal/render-states.js +2 -1
  64. package/dist/internal/scalar.js +22 -13
  65. package/dist/internal/shaped-glyph.d.ts +3 -1
  66. package/dist/internal/shaped-glyph.js +12 -4
  67. package/dist/internal/struct-view.d.ts +3 -1
  68. package/dist/internal/struct-view.js +3 -1
  69. package/dist/internal/transformable.js +2 -1
  70. package/dist/network/common.js +23 -11
  71. package/dist/network/dns.js +25 -17
  72. package/dist/network/ftp.js +43 -18
  73. package/dist/network/http.js +126 -56
  74. package/dist/network/ip-address.js +50 -25
  75. package/dist/network/packet.js +63 -25
  76. package/dist/network/sftp.js +175 -74
  77. package/dist/network/socket-selector.js +28 -14
  78. package/dist/network/socket.js +71 -30
  79. package/dist/network/tcp-listener.js +42 -19
  80. package/dist/network/tcp-socket.js +111 -53
  81. package/dist/network/tls-material.js +2 -1
  82. package/dist/network/udp-socket.js +68 -33
  83. package/dist/system/angle.js +2 -1
  84. package/dist/system/clock.js +4 -2
  85. package/dist/system/err.js +5 -2
  86. package/dist/system/file-input-stream.js +8 -4
  87. package/dist/system/input-stream.js +6 -3
  88. package/dist/system/operators.d.ts +1 -1
  89. package/dist/system/operators.js +90 -50
  90. package/dist/system/sleep.js +4 -2
  91. package/dist/system/time.js +12 -6
  92. package/dist/system/vector2.js +6 -7
  93. package/dist/system/vector3.js +11 -6
  94. package/dist/window/clipboard.js +2 -1
  95. package/dist/window/context.js +10 -5
  96. package/dist/window/cursor.js +2 -1
  97. package/dist/window/event.js +9 -5
  98. package/dist/window/mouse.js +4 -2
  99. package/dist/window/video-mode.js +2 -1
  100. package/dist/window/window.js +19 -12
  101. package/native/addon.cc +25 -12
  102. package/native/audio-binding.cc +266 -126
  103. package/native/audio-codec-binding.cc +147 -77
  104. package/native/audio-device-binding.cc +76 -49
  105. package/native/audio-file-binding.cc +116 -67
  106. package/native/audio-recorder-binding.cc +147 -98
  107. package/native/audio-recorder.h +1 -1
  108. package/native/audio-state.h +53 -30
  109. package/native/audio-stream-binding.cc +148 -94
  110. package/native/audio-stream.h +5 -4
  111. package/native/audio-worker-binding.cc +163 -81
  112. package/native/audio-worker.h +35 -19
  113. package/native/batch-drawable.cc +29 -16
  114. package/native/batch-drawable.h +11 -8
  115. package/native/context-binding.cc +40 -27
  116. package/native/context-settings.h +15 -8
  117. package/native/deferred-delete.cc +9 -6
  118. package/native/deferred-delete.h +7 -8
  119. package/native/error-stream-binding.cc +42 -28
  120. package/native/font-binding.cc +89 -48
  121. package/native/graphics-binding.cc +122 -72
  122. package/native/graphics-state.h +50 -36
  123. package/native/image-binding.cc +38 -24
  124. package/native/image-state.h +26 -18
  125. package/native/input-binding.cc +42 -27
  126. package/native/render-window-binding.cc +38 -23
  127. package/native/shader-binding.cc +115 -50
  128. package/native/shape-binding.cc +94 -49
  129. package/native/shape-state.h +18 -9
  130. package/native/shaped-glyph-storage.h +9 -5
  131. package/native/stream-binding.cc +143 -117
  132. package/native/system-binding.cc +17 -12
  133. package/native/text-binding.cc +68 -35
  134. package/native/text-state.h +25 -18
  135. package/native/texture-binding.cc +42 -33
  136. package/native/texture-resource.h +20 -12
  137. package/native/time-value.h +3 -1
  138. package/native/transformable-binding.cc +12 -6
  139. package/native/transformable-state.h +6 -6
  140. package/native/value-binding.h +46 -28
  141. package/native/value-layout.h +10 -10
  142. package/native/values-binding.cc +29 -23
  143. package/native/vertex-binding.cc +18 -12
  144. package/native/vertex-buffer-binding.cc +38 -24
  145. package/native/vertex-storage.h +2 -2
  146. package/native/view-data.h +4 -3
  147. package/native/window-binding.cc +75 -45
  148. package/native/window-event.h +20 -11
  149. package/native/window-owner.h +15 -12
  150. package/package.json +43 -50
  151. package/src/audio/effect-processor.ts +9 -3
  152. package/src/audio/input-sound-file.ts +108 -36
  153. package/src/audio/listener.ts +39 -13
  154. package/src/audio/music.ts +112 -47
  155. package/src/audio/output-sound-file.ts +40 -11
  156. package/src/audio/playback-device.ts +23 -8
  157. package/src/audio/sound-buffer-recorder.ts +35 -13
  158. package/src/audio/sound-buffer.ts +166 -58
  159. package/src/audio/sound-file-factory.ts +127 -49
  160. package/src/audio/sound-file-reader.ts +49 -15
  161. package/src/audio/sound-recorder.ts +100 -34
  162. package/src/audio/sound-source.ts +142 -45
  163. package/src/audio/sound-stream.ts +15 -5
  164. package/src/audio/sound.ts +12 -4
  165. package/src/audio/value.ts +89 -26
  166. package/src/graphics/blend-mode.ts +3 -1
  167. package/src/graphics/boolean-vector.ts +56 -26
  168. package/src/graphics/color.ts +3 -1
  169. package/src/graphics/convex-shape.ts +10 -3
  170. package/src/graphics/font.ts +171 -50
  171. package/src/graphics/glsl-matrix.ts +9 -3
  172. package/src/graphics/glsl.ts +81 -35
  173. package/src/graphics/glyph.ts +67 -26
  174. package/src/graphics/image.ts +119 -45
  175. package/src/graphics/rect.ts +56 -24
  176. package/src/graphics/render-states.ts +31 -14
  177. package/src/graphics/render-target.ts +197 -81
  178. package/src/graphics/render-texture.ts +36 -12
  179. package/src/graphics/render-window.ts +9 -3
  180. package/src/graphics/shader.ts +186 -61
  181. package/src/graphics/shape.ts +93 -35
  182. package/src/graphics/sprite.ts +64 -25
  183. package/src/graphics/stencil-mode.ts +12 -6
  184. package/src/graphics/swap.ts +3 -2
  185. package/src/graphics/text.ts +229 -74
  186. package/src/graphics/texture.ts +180 -61
  187. package/src/graphics/transform.ts +132 -47
  188. package/src/graphics/transformable.ts +3 -1
  189. package/src/graphics/vertex-array.ts +87 -35
  190. package/src/graphics/vertex-buffer.ts +97 -33
  191. package/src/graphics/vertex.ts +9 -3
  192. package/src/graphics/view.ts +59 -21
  193. package/src/index.ts +101 -23
  194. package/src/internal/audio-codec-registry.ts +18 -6
  195. package/src/internal/audio-input-stream.ts +6 -2
  196. package/src/internal/audio-music-pump.ts +116 -41
  197. package/src/internal/audio-native.ts +249 -96
  198. package/src/internal/audio-recorder-session.ts +68 -24
  199. package/src/internal/audio-worker-session.ts +65 -21
  200. package/src/internal/audio-worker.ts +85 -41
  201. package/src/internal/glsl-value.ts +26 -11
  202. package/src/internal/graphics-state.ts +57 -20
  203. package/src/internal/input-stream.ts +23 -10
  204. package/src/internal/native.ts +252 -99
  205. package/src/internal/numeric-properties.ts +2 -1
  206. package/src/internal/render-states.ts +3 -1
  207. package/src/internal/scalar.ts +33 -14
  208. package/src/internal/shaped-glyph.ts +25 -12
  209. package/src/internal/struct-view.ts +40 -13
  210. package/src/internal/transformable.ts +3 -1
  211. package/src/network/common.ts +35 -11
  212. package/src/network/dns.ts +95 -38
  213. package/src/network/ftp.ts +129 -47
  214. package/src/network/http.ts +232 -77
  215. package/src/network/ip-address.ts +118 -48
  216. package/src/network/packet.ts +161 -54
  217. package/src/network/sftp.ts +498 -168
  218. package/src/network/socket-selector.ts +66 -26
  219. package/src/network/socket.ts +133 -46
  220. package/src/network/tcp-listener.ts +78 -29
  221. package/src/network/tcp-socket.ts +240 -95
  222. package/src/network/tls-material.ts +6 -2
  223. package/src/network/udp-socket.ts +147 -53
  224. package/src/system/angle.ts +3 -1
  225. package/src/system/clock.ts +12 -4
  226. package/src/system/err.ts +5 -2
  227. package/src/system/file-input-stream.ts +12 -4
  228. package/src/system/input-stream.ts +9 -3
  229. package/src/system/operators.ts +290 -115
  230. package/src/system/sleep.ts +6 -2
  231. package/src/system/time.ts +18 -6
  232. package/src/system/timeout-with-predicate.ts +19 -8
  233. package/src/system/vector2.ts +101 -49
  234. package/src/system/vector3.ts +85 -36
  235. package/src/window/clipboard.ts +3 -1
  236. package/src/window/context.ts +15 -5
  237. package/src/window/cursor.ts +3 -1
  238. package/src/window/event.ts +26 -12
  239. package/src/window/mouse.ts +6 -2
  240. package/src/window/video-mode.ts +3 -1
  241. package/src/window/window.ts +119 -37
  242. package/test/native/audio-probes.cc +74 -36
  243. package/test/native/deferred-delete-probes.cc +17 -11
  244. package/test/native/drawable-probes.cc +70 -36
  245. package/test/native/exception-probes.cc +14 -9
  246. package/test/native/image-probes.cc +20 -10
  247. package/test/native/matrix-probes.cc +8 -4
  248. package/test/native/network-oracle.cc +17 -11
  249. package/test/native/render-texture-probes.cc +33 -24
  250. package/test/native/shader-probes.cc +9 -9
  251. package/test/native/shape-probes.cc +37 -23
  252. package/test/native/sprite-probes.cc +41 -25
  253. package/test/native/system-probes.cc +58 -35
  254. package/test/native/text-probes.cc +77 -44
  255. package/test/native/texture-probes.cc +43 -26
  256. package/test/native/time-probes.cc +18 -12
  257. package/test/native/transform-probes.cc +15 -10
  258. package/test/native/transformable-probes.cc +23 -17
  259. package/test/native/value-oracle.h +9 -4
  260. package/test/native/value-probes.cc +31 -23
  261. package/test/native/vertex-array-probes.cc +22 -14
  262. package/test/native/vertex-buffer-probes.cc +28 -17
  263. package/test/native/view-probes.cc +29 -17
  264. package/test/native/window-probes.cc +66 -33
  265. package/tools/ensure-sfml.cjs +9 -0
  266. package/tools/generate-network-constants.cjs +11 -6
  267. package/tools/generate-window-constants.cjs +31 -12
  268. package/docs/README.ja.md +0 -30
  269. package/docs/README.ko.md +0 -30
  270. package/docs/README.md +0 -30
  271. package/docs/README.zh-CN.md +0 -30
  272. package/docs/api.ja.md +0 -84
  273. package/docs/api.ko.md +0 -84
  274. package/docs/api.md +0 -81
  275. package/docs/api.zh-CN.md +0 -81
  276. package/docs/audio.ja.md +0 -249
  277. package/docs/audio.ko.md +0 -251
  278. package/docs/audio.md +0 -252
  279. package/docs/audio.zh-CN.md +0 -252
  280. package/docs/concepts.ja.md +0 -130
  281. package/docs/concepts.ko.md +0 -130
  282. package/docs/concepts.md +0 -131
  283. package/docs/concepts.zh-CN.md +0 -131
  284. package/docs/development.ja.md +0 -148
  285. package/docs/development.ko.md +0 -148
  286. package/docs/development.md +0 -150
  287. package/docs/development.zh-CN.md +0 -150
  288. package/docs/getting-started.ja.md +0 -154
  289. package/docs/getting-started.ko.md +0 -154
  290. package/docs/getting-started.md +0 -156
  291. package/docs/getting-started.zh-CN.md +0 -156
  292. package/docs/graphics.ja.md +0 -256
  293. package/docs/graphics.ko.md +0 -254
  294. package/docs/graphics.md +0 -255
  295. package/docs/graphics.zh-CN.md +0 -257
  296. package/docs/network.ja.md +0 -214
  297. package/docs/network.ko.md +0 -216
  298. package/docs/network.md +0 -218
  299. package/docs/network.zh-CN.md +0 -218
  300. package/docs/system.ja.md +0 -169
  301. package/docs/system.ko.md +0 -171
  302. package/docs/system.md +0 -173
  303. package/docs/system.zh-CN.md +0 -173
  304. package/docs/window.ja.md +0 -138
  305. package/docs/window.ko.md +0 -140
  306. package/docs/window.md +0 -141
  307. package/docs/window.zh-CN.md +0 -141
  308. package/native/exception-boundary.h +0 -97
  309. package/native/numeric-fields.h +0 -96
  310. package/native/shared-handle.h +0 -69
  311. package/test/tsconfig.json +0 -8
  312. package/tools/configure-lsp.cjs +0 -23
  313. package/tools/lib_downloader.cjs +0 -92
  314. package/tools/lib_downloader_urls.cjs +0 -23
@@ -61,15 +61,26 @@ export class IpAddress {
61
61
  * @ko 0~255 범위의 십진수 옥텟 4개로 IPv4 주소를 생성합니다.
62
62
  */
63
63
  constructor(a: number, b: number, c: number, d: number);
64
- constructor(address: number | Uint8Array | IpAddress,
65
- b?: number, c?: number, d?: number) {
66
- if (address instanceof IpAddress) this.data = Buffer.from(address.data);
67
- else if (address instanceof Uint8Array) {
68
- if (address.length !== 16) throw new RangeError('IPv6 needs 16 bytes');
64
+ constructor(
65
+ address: number | Uint8Array | IpAddress,
66
+ b?: number,
67
+ c?: number,
68
+ d?: number,
69
+ ) {
70
+ if (address instanceof IpAddress) {
71
+ this.data = Buffer.from(address.data);
72
+ } else if (address instanceof Uint8Array) {
73
+ if (address.length !== 16) {
74
+ throw new RangeError('IPv6 needs 16 bytes');
75
+ }
69
76
  this.data = Buffer.from(address);
70
77
  } else if (b !== undefined) {
71
- this.data = Buffer.from([ uint(address, 255), uint(b, 255),
72
- uint(c!, 255), uint(d!, 255) ]);
78
+ this.data = Buffer.from([
79
+ uint(address, 255),
80
+ uint(b, 255),
81
+ uint(c!, 255),
82
+ uint(d!, 255),
83
+ ]);
73
84
  } else {
74
85
  this.data = Buffer.alloc(4);
75
86
  this.data.writeUInt32BE(uint(address));
@@ -85,20 +96,30 @@ export class IpAddress {
85
96
  * 허용하지 않으며, 형식이 잘못되면 undefined를 반환합니다.
86
97
  */
87
98
  static fromString(address: string): IpAddress | undefined {
88
- if (typeof address !== 'string') throw new TypeError('Expected an IP string');
99
+ if (typeof address !== 'string') {
100
+ throw new TypeError('Expected an IP string');
101
+ }
89
102
  // SFML inet_pton rejects zone identifiers and non-canonical IPv4 forms.
90
103
  const family = address.includes('%') ? 0 : isIP(address);
91
- if (!family) return undefined;
104
+ if (!family) {
105
+ return undefined;
106
+ }
92
107
  // ipaddr.js normalizes ::a.b.c.d into ::ffff:a.b.c.d. SFML retains the
93
108
  // original 128 bits, so expand the dotted suffix before parsing IPv6.
94
- const normalized = family === 6
95
- ? address.replace(/(?:\d+\.){3}\d+$/, suffix => {
96
- const octets = suffix.split('.').map(Number);
97
- return (octets[0] * 256 + octets[1]).toString(16) + ':' +
98
- (octets[2] * 256 + octets[3]).toString(16);
99
- }) : address;
109
+ const normalized =
110
+ family === 6
111
+ ? address.replace(/(?:\d+\.){3}\d+$/, suffix => {
112
+ const octets = suffix.split('.').map(Number);
113
+ return (
114
+ (octets[0] * 256 + octets[1]).toString(16) +
115
+ ':' +
116
+ (octets[2] * 256 + octets[3]).toString(16)
117
+ );
118
+ })
119
+ : address;
100
120
  const data = Buffer.from(ipaddr.parse(normalized).toByteArray());
101
- return family === 4 ? new IpAddress(data.readUInt32BE())
121
+ return family === 4
122
+ ? new IpAddress(data.readUInt32BE())
102
123
  : new IpAddress(data);
103
124
  }
104
125
  /**
@@ -112,7 +133,9 @@ export class IpAddress {
112
133
  */
113
134
  static async resolve(hostname: string): Promise<IpAddress | undefined> {
114
135
  const literal = this.fromString(hostname);
115
- if (literal) return literal;
136
+ if (literal) {
137
+ return literal;
138
+ }
116
139
  const { resolve } = await import('./dns');
117
140
  return (await resolve(hostname))?.find(address => address.isV4());
118
141
  }
@@ -125,18 +148,24 @@ export class IpAddress {
125
148
  * @ko 시스템 라우팅으로 지정한 주소 계열의 로컬 송신 주소를 조회하며,
126
149
  * 실패 시 undefined를 반환합니다.
127
150
  */
128
- static async getLocalAddress(type = IpAddressType.IpV4):
129
- Promise<IpAddress | undefined> {
151
+ static async getLocalAddress(
152
+ type = IpAddressType.IpV4,
153
+ ): Promise<IpAddress | undefined> {
130
154
  uint(type, 1);
131
155
  return new Promise(resolve => {
132
- const socket = createSocket(type === IpAddressType.IpV4 ? 'udp4' : 'udp6');
156
+ const socket = createSocket(
157
+ type === IpAddressType.IpV4 ? 'udp4' : 'udp6',
158
+ );
133
159
  const finish = (address?: IpAddress) => {
134
160
  socket.close();
135
161
  resolve(address);
136
162
  };
137
163
  socket.once('error', () => finish());
138
- socket.connect(9, type === IpAddressType.IpV4 ? '1.1.1.1' : '2a00::1',
139
- () => finish(IpAddress.fromString(socket.address().address)));
164
+ socket.connect(
165
+ 9,
166
+ type === IpAddressType.IpV4 ? '1.1.1.1' : '2a00::1',
167
+ () => finish(IpAddress.fromString(socket.address().address)),
168
+ );
140
169
  });
141
170
  }
142
171
  /**
@@ -149,24 +178,40 @@ export class IpAddress {
149
178
  * @ko 외부 DNS/HTTP 서비스로 공인 주소를 비동기로 조회합니다.
150
179
  * secure는 HTTPS를 사용하며, 실패 시 undefined를 반환합니다.
151
180
  */
152
- static async getPublicAddress(timeout: NetworkTime = Time.Zero,
153
- type: IpAddressType | undefined = IpAddressType.IpV4, secure = false):
154
- Promise<IpAddress | undefined> {
155
- if (type !== undefined) uint(type, 1);
181
+ static async getPublicAddress(
182
+ timeout: NetworkTime = Time.Zero,
183
+ type: IpAddressType | undefined = IpAddressType.IpV4,
184
+ secure = false,
185
+ ): Promise<IpAddress | undefined> {
186
+ if (type !== undefined) {
187
+ uint(type, 1);
188
+ }
156
189
  if (!secure) {
157
190
  const dns = await import('./dns');
158
191
  for (const family of type === undefined
159
- ? [ IpAddressType.IpV6, IpAddressType.IpV4 ] : [ type ]) {
192
+ ? [ IpAddressType.IpV6, IpAddressType.IpV4 ]
193
+ : [ type ]) {
160
194
  const address = await dns.getPublicAddress(timeout, family);
161
- if (address) return address;
195
+ if (address) {
196
+ return address;
197
+ }
162
198
  }
163
199
  }
164
200
  const { Http } = await import('./http');
165
201
  for (const host of [ 'ifconfig.co', 'ifconfig.me', 'icanhazip.com' ]) {
166
- const client = new Http(`${secure ? 'https' : 'http'}://${host}`, 0, type);
167
- const response = await client.sendRequest(new Http.Request('/ip'),
168
- timeout, secure);
169
- if (response.getStatus() !== 200) continue;
202
+ const client = new Http(
203
+ `${secure ? 'https' : 'http'}://${host}`,
204
+ 0,
205
+ type,
206
+ );
207
+ const response = await client.sendRequest(
208
+ new Http.Request('/ip'),
209
+ timeout,
210
+ secure,
211
+ );
212
+ if (response.getStatus() !== 200) {
213
+ continue;
214
+ }
170
215
  const address = this.fromString(response.getBody().trim());
171
216
  if (address && (type === undefined || address.getType() === type)) {
172
217
  return address;
@@ -181,12 +226,18 @@ export class IpAddress {
181
226
  * @ko 점으로 구분한 십진수 IPv4 또는 압축된 IPv6 텍스트를 반환합니다.
182
227
  */
183
228
  toString(): string {
184
- if (this.isV4()) return [ ...this.data ].join('.');
229
+ if (this.isV4()) {
230
+ return [ ...this.data ].join('.');
231
+ }
185
232
  const compatible = this.data.subarray(0, 12).every(byte => byte === 0);
186
- const mapped = this.data.subarray(0, 10).every(byte => byte === 0) &&
187
- this.data[10] === 255 && this.data[11] === 255;
233
+ const mapped =
234
+ this.data.subarray(0, 10).every(byte => byte === 0) &&
235
+ this.data[10] === 255 &&
236
+ this.data[11] === 255;
188
237
  if (mapped || (compatible && (this.data[12] || this.data[13]))) {
189
- return (mapped ? '::ffff:' : '::') + [ ...this.data.subarray(12) ].join('.');
238
+ return (
239
+ (mapped ? '::ffff:' : '::') + [ ...this.data.subarray(12) ].join('.')
240
+ );
190
241
  }
191
242
  return ipaddr.fromByteArray([ ...this.data ]).toString();
192
243
  }
@@ -200,7 +251,9 @@ export class IpAddress {
200
251
  * IPv6 주소이면 TypeError를 던집니다.
201
252
  */
202
253
  toInteger(): number {
203
- if (!this.isV4()) throw new TypeError('Expected an IPv4 address');
254
+ if (!this.isV4()) {
255
+ throw new TypeError('Expected an IPv4 address');
256
+ }
204
257
  return this.data.readUInt32BE();
205
258
  }
206
259
  /**
@@ -212,7 +265,9 @@ export class IpAddress {
212
265
  * IPv4 주소이면 TypeError를 던집니다.
213
266
  */
214
267
  toBytes(): Uint8Array {
215
- if (!this.isV6()) throw new TypeError('Expected an IPv6 address');
268
+ if (!this.isV6()) {
269
+ throw new TypeError('Expected an IPv6 address');
270
+ }
216
271
  return Uint8Array.from(this.data);
217
272
  }
218
273
  /**
@@ -230,14 +285,18 @@ export class IpAddress {
230
285
  * @ja IPv4 アドレスかどうかを返します。
231
286
  * @ko IPv4 주소인지 반환합니다.
232
287
  */
233
- isV4(): boolean { return this.data.length === 4; }
288
+ isV4(): boolean {
289
+ return this.data.length === 4;
290
+ }
234
291
  /**
235
292
  * Return whether this is an IPv6 address.
236
293
  * @zh 返回是否为 IPv6 地址。
237
294
  * @ja IPv6 アドレスかどうかを返します。
238
295
  * @ko IPv6 주소인지 반환합니다.
239
296
  */
240
- isV6(): boolean { return this.data.length === 16; }
297
+ isV6(): boolean {
298
+ return this.data.length === 16;
299
+ }
241
300
  /**
242
301
  * Compare by address family then address bytes;
243
302
  * return a negative number, zero, or a positive number.
@@ -247,9 +306,13 @@ export class IpAddress {
247
306
  * @ko 주소 계열, 주소 바이트 순서로 비교하며 음수, 0 또는 양수를 반환합니다.
248
307
  */
249
308
  compare(other: IpAddress): number {
250
- if (!(other instanceof IpAddress)) throw new TypeError('Expected IpAddress');
251
- return this.data.length - other.data.length ||
252
- Buffer.compare(this.data, other.data);
309
+ if (!(other instanceof IpAddress)) {
310
+ throw new TypeError('Expected IpAddress');
311
+ }
312
+ return (
313
+ this.data.length - other.data.length ||
314
+ Buffer.compare(this.data, other.data)
315
+ );
253
316
  }
254
317
  /**
255
318
  * IPv4 wildcard interface address 0.0.0.0.
@@ -264,29 +327,36 @@ export class IpAddress {
264
327
  * @ja IPv4 のループバックアドレス 127.0.0.1。
265
328
  * @ko IPv4 루프백 주소 127.0.0.1.
266
329
  */
267
- static readonly LocalHostV4 = Object.freeze(new IpAddress(127, 0, 0, 1)) as IpAddress;
330
+ static readonly LocalHostV4 = Object.freeze(
331
+ new IpAddress(127, 0, 0, 1),
332
+ ) as IpAddress;
268
333
  /**
269
334
  * IPv4 broadcast address 255.255.255.255.
270
335
  * @zh IPv4 广播地址 255.255.255.255。
271
336
  * @ja IPv4 のブロードキャストアドレス 255.255.255.255。
272
337
  * @ko IPv4 브로드캐스트 주소 255.255.255.255.
273
338
  */
274
- static readonly BroadcastV4 = Object.freeze(new IpAddress(0xffffffff)) as IpAddress;
339
+ static readonly BroadcastV4 = Object.freeze(
340
+ new IpAddress(0xffffffff),
341
+ ) as IpAddress;
275
342
  /**
276
343
  * IPv6 wildcard interface address ::.
277
344
  * @zh IPv6 任意接口地址 ::。
278
345
  * @ja IPv6 の任意のインターフェースを表すアドレス ::。
279
346
  * @ko IPv6의 모든 인터페이스를 나타내는 주소 ::.
280
347
  */
281
- static readonly AnyV6 = Object.freeze(new IpAddress(new Uint8Array(16))) as IpAddress;
348
+ static readonly AnyV6 = Object.freeze(
349
+ new IpAddress(new Uint8Array(16)),
350
+ ) as IpAddress;
282
351
  /**
283
352
  * IPv6 loopback address ::1.
284
353
  * @zh IPv6 回环地址 ::1。
285
354
  * @ja IPv6 のループバックアドレス ::1。
286
355
  * @ko IPv6 루프백 주소 ::1.
287
356
  */
288
- static readonly LocalHostV6 = Object.freeze(new IpAddress(
289
- Uint8Array.from({ length: 16 }, (_, i) => +(i === 15)))) as IpAddress;
357
+ static readonly LocalHostV6 = Object.freeze(
358
+ new IpAddress(Uint8Array.from({ length: 16 }, (_, i) => +(i === 15))),
359
+ ) as IpAddress;
290
360
  /**
291
361
  * Compatibility alias for AnyV4.
292
362
  * @zh AnyV4 的兼容别名。
@@ -121,8 +121,17 @@ export const packetSend: unique symbol = Symbol('Packet.send');
121
121
  export const packetReceive: unique symbol = Symbol('Packet.receive');
122
122
  const littleEndian = endianness() === 'LE';
123
123
  const sizes = {
124
- bool: 1, i8: 1, u8: 1, i16: 2, u16: 2, i32: 4, u32: 4,
125
- i64: 8, u64: 8, f32: 4, f64: 8,
124
+ bool: 1,
125
+ i8: 1,
126
+ u8: 1,
127
+ i16: 2,
128
+ u16: 2,
129
+ i32: 4,
130
+ u32: 4,
131
+ i64: 8,
132
+ u64: 8,
133
+ f32: 4,
134
+ f64: 8,
126
135
  } as const;
127
136
 
128
137
  /**
@@ -150,7 +159,9 @@ export class Packet {
150
159
  */
151
160
  constructor(source?: Packet) {
152
161
  if (source !== undefined) {
153
- if (!(source instanceof Packet)) throw new TypeError('Expected a Packet');
162
+ if (!(source instanceof Packet)) {
163
+ throw new TypeError('Expected a Packet');
164
+ }
154
165
  this.append(source.data.subarray(0, source.length));
155
166
  this.position = source.position;
156
167
  this.valid = source.valid;
@@ -202,21 +213,27 @@ export class Packet {
202
213
  * @ja 現在の読み取り位置をバイト単位で返します。
203
214
  * @ko 현재 읽기 위치를 바이트 단위로 반환합니다.
204
215
  */
205
- getReadPosition(): number { return this.position; }
216
+ getReadPosition(): number {
217
+ return this.position;
218
+ }
206
219
  /**
207
220
  * Return the number of content bytes.
208
221
  * @zh 返回有效内容的字节数。
209
222
  * @ja 有効な内容のバイト数を返します。
210
223
  * @ko 유효한 내용의 바이트 수를 반환합니다.
211
224
  */
212
- getDataSize(): number { return this.length; }
225
+ getDataSize(): number {
226
+ return this.length;
227
+ }
213
228
  /**
214
229
  * Return whether the read position has reached the end.
215
230
  * @zh 返回读取位置是否已到末尾。
216
231
  * @ja 読み取り位置が末尾に到達したかどうかを返します。
217
232
  * @ko 읽기 위치가 끝에 도달했는지 반환합니다.
218
233
  */
219
- endOfPacket(): boolean { return this.position >= this.length; }
234
+ endOfPacket(): boolean {
235
+ return this.position >= this.length;
236
+ }
220
237
  /**
221
238
  * Return whether no out-of-bounds read has occurred; after one occurs,
222
239
  * use clear or receive a new packet to recover.
@@ -226,7 +243,9 @@ export class Packet {
226
243
  * @ko 범위를 벗어난 읽기가 아직 발생하지 않았는지 반환합니다.
227
244
  * 발생 후에는 clear 또는 새 패킷 수신으로 복구해야 합니다.
228
245
  */
229
- isValid(): boolean { return this.valid; }
246
+ isValid(): boolean {
247
+ return this.valid;
248
+ }
230
249
  /**
231
250
  * Encode and append a value according to type, returning this;
232
251
  * string uses SFML UTF-32, while utf8 uses a byte string.
@@ -245,7 +264,9 @@ export class Packet {
245
264
  return this;
246
265
  }
247
266
  if (type === 'string' || type === 'utf8') {
248
- if (typeof value !== 'string') throw new TypeError('Expected a string');
267
+ if (typeof value !== 'string') {
268
+ throw new TypeError('Expected a string');
269
+ }
249
270
  if (type === 'utf8') {
250
271
  const encoded = Buffer.from(value, 'utf8');
251
272
  this.write('u32', encoded.length);
@@ -253,34 +274,67 @@ export class Packet {
253
274
  } else {
254
275
  const points = Array.from(value, c => c.codePointAt(0)!);
255
276
  this.write('u32', points.length);
256
- for (const point of points) this.write('u32', point);
277
+ for (const point of points) {
278
+ this.write('u32', point);
279
+ }
257
280
  }
258
281
  return this;
259
282
  }
260
283
  const count = sizes[type as keyof typeof sizes];
261
- if (!count) throw new TypeError('Unknown packet scalar type');
284
+ if (!count) {
285
+ throw new TypeError('Unknown packet scalar type');
286
+ }
262
287
  this.reserve(count);
263
- const view = new DataView(this.data.buffer,
264
- this.data.byteOffset + this.length, count);
288
+ const view = new DataView(
289
+ this.data.buffer,
290
+ this.data.byteOffset + this.length,
291
+ count,
292
+ );
265
293
  if (type === 'bool') {
266
- if (typeof value !== 'boolean') throw new TypeError('Expected a boolean');
294
+ if (typeof value !== 'boolean') {
295
+ throw new TypeError('Expected a boolean');
296
+ }
267
297
  view.setUint8(0, +value);
268
298
  } else if (type === 'i64' || type === 'u64') {
269
- if (typeof value !== 'bigint') throw new TypeError('Expected a bigint');
270
- if (type === 'i64') view.setBigInt64(0, value);
271
- else view.setBigUint64(0, value);
299
+ if (typeof value !== 'bigint') {
300
+ throw new TypeError('Expected a bigint');
301
+ }
302
+ if (type === 'i64') {
303
+ view.setBigInt64(0, value);
304
+ } else {
305
+ view.setBigUint64(0, value);
306
+ }
272
307
  } else {
273
- if (typeof value !== 'number') throw new TypeError('Expected a number');
308
+ if (typeof value !== 'number') {
309
+ throw new TypeError('Expected a number');
310
+ }
274
311
  switch (type) {
275
- case 'i8': view.setInt8(0, value); break;
276
- case 'u8': view.setUint8(0, value); break;
277
- case 'i16': view.setInt16(0, value); break;
278
- case 'u16': view.setUint16(0, value); break;
279
- case 'i32': view.setInt32(0, value); break;
280
- case 'u32': view.setUint32(0, value); break;
281
- case 'f32': view.setFloat32(0, value, littleEndian); break;
282
- case 'f64': view.setFloat64(0, value, littleEndian); break;
283
- default: throw new TypeError('Unknown packet scalar type');
312
+ case 'i8':
313
+ view.setInt8(0, value);
314
+ break;
315
+ case 'u8':
316
+ view.setUint8(0, value);
317
+ break;
318
+ case 'i16':
319
+ view.setInt16(0, value);
320
+ break;
321
+ case 'u16':
322
+ view.setUint16(0, value);
323
+ break;
324
+ case 'i32':
325
+ view.setInt32(0, value);
326
+ break;
327
+ case 'u32':
328
+ view.setUint32(0, value);
329
+ break;
330
+ case 'f32':
331
+ view.setFloat32(0, value, littleEndian);
332
+ break;
333
+ case 'f64':
334
+ view.setFloat64(0, value, littleEndian);
335
+ break;
336
+ default:
337
+ throw new TypeError('Unknown packet scalar type');
284
338
  }
285
339
  }
286
340
  this.length += count;
@@ -298,48 +352,91 @@ export class Packet {
298
352
  read<T extends PacketType>(type: T): PacketValues[T] | undefined {
299
353
  if (type === 'bytes') {
300
354
  const length = this.read('u32');
301
- if (length === undefined || !this.check(length)) return undefined;
302
- const output = Uint8Array.from(this.data.subarray(this.position,
303
- this.position + length));
355
+ if (length === undefined || !this.check(length)) {
356
+ return undefined;
357
+ }
358
+ const output = Uint8Array.from(
359
+ this.data.subarray(this.position, this.position + length),
360
+ );
304
361
  this.position += length;
305
362
  return output as PacketValues[T];
306
363
  }
307
364
  if (type === 'string' || type === 'utf8') {
308
365
  const length = this.read('u32');
309
- if (length === undefined) return undefined;
366
+ if (length === undefined) {
367
+ return undefined;
368
+ }
310
369
  const count = length * (type === 'string' ? 4 : 1);
311
- if (!this.check(count)) return undefined;
370
+ if (!this.check(count)) {
371
+ return undefined;
372
+ }
312
373
  let result = '';
313
374
  if (type === 'utf8') {
314
- result = this.data.toString('utf8', this.position, this.position + count);
375
+ result = this.data.toString(
376
+ 'utf8',
377
+ this.position,
378
+ this.position + count,
379
+ );
315
380
  } else {
316
381
  for (let i = 0; i < count; i += 4) {
317
382
  const point = this.data.readUInt32BE(this.position + i);
318
- result += String.fromCodePoint(point <= 0x10ffff ? point : 0xfffd);
383
+ result += String.fromCodePoint(
384
+ point <= 0x10ffff ? point : 0xfffd,
385
+ );
319
386
  }
320
387
  }
321
388
  this.position += count;
322
389
  return result as PacketValues[T];
323
390
  }
324
391
  const count = sizes[type as keyof typeof sizes];
325
- if (!count) throw new TypeError('Unknown packet scalar type');
326
- if (!this.check(count)) return undefined;
327
- const view = new DataView(this.data.buffer,
328
- this.data.byteOffset + this.position, count);
392
+ if (!count) {
393
+ throw new TypeError('Unknown packet scalar type');
394
+ }
395
+ if (!this.check(count)) {
396
+ return undefined;
397
+ }
398
+ const view = new DataView(
399
+ this.data.buffer,
400
+ this.data.byteOffset + this.position,
401
+ count,
402
+ );
329
403
  let value: number | bigint | boolean;
330
404
  switch (type) {
331
- case 'bool': value = view.getUint8(0) !== 0; break;
332
- case 'i8': value = view.getInt8(0); break;
333
- case 'u8': value = view.getUint8(0); break;
334
- case 'i16': value = view.getInt16(0); break;
335
- case 'u16': value = view.getUint16(0); break;
336
- case 'i32': value = view.getInt32(0); break;
337
- case 'u32': value = view.getUint32(0); break;
338
- case 'i64': value = view.getBigInt64(0); break;
339
- case 'u64': value = view.getBigUint64(0); break;
340
- case 'f32': value = view.getFloat32(0, littleEndian); break;
341
- case 'f64': value = view.getFloat64(0, littleEndian); break;
342
- default: throw new TypeError('Unknown packet scalar type');
405
+ case 'bool':
406
+ value = view.getUint8(0) !== 0;
407
+ break;
408
+ case 'i8':
409
+ value = view.getInt8(0);
410
+ break;
411
+ case 'u8':
412
+ value = view.getUint8(0);
413
+ break;
414
+ case 'i16':
415
+ value = view.getInt16(0);
416
+ break;
417
+ case 'u16':
418
+ value = view.getUint16(0);
419
+ break;
420
+ case 'i32':
421
+ value = view.getInt32(0);
422
+ break;
423
+ case 'u32':
424
+ value = view.getUint32(0);
425
+ break;
426
+ case 'i64':
427
+ value = view.getBigInt64(0);
428
+ break;
429
+ case 'u64':
430
+ value = view.getBigUint64(0);
431
+ break;
432
+ case 'f32':
433
+ value = view.getFloat32(0, littleEndian);
434
+ break;
435
+ case 'f64':
436
+ value = view.getFloat64(0, littleEndian);
437
+ break;
438
+ default:
439
+ throw new TypeError('Unknown packet scalar type');
343
440
  }
344
441
  this.position += count;
345
442
  return value as PacketValues[T];
@@ -365,8 +462,12 @@ export class Packet {
365
462
  * @ko 하위 클래스는 수신 후 데이터 변환을 재정의할 수 있습니다.
366
463
  * 호출 전에 패킷을 비우며 기본적으로 원래 내용을 추가합니다.
367
464
  */
368
- protected onReceive(data: Uint8Array): void { this.append(data); }
369
- [packetSend](): Buffer { return bytes(this.onSend()); }
465
+ protected onReceive(data: Uint8Array): void {
466
+ this.append(data);
467
+ }
468
+ [packetSend](): Buffer {
469
+ return bytes(this.onSend());
470
+ }
370
471
  [packetReceive](data: Uint8Array): void {
371
472
  this.clear();
372
473
  this.onReceive(data);
@@ -379,9 +480,15 @@ export class Packet {
379
480
  /** 确保存储至少容纳当前长度加 count 字节。 */
380
481
  private reserve(count: number): void {
381
482
  const required = uint(this.length + count, 0x7fffffff);
382
- if (required <= this.data.length) return;
383
- const next = Buffer.allocUnsafe(Math.max(required,
384
- Math.min(0x7fffffff, Math.max(64, this.data.length * 2))));
483
+ if (required <= this.data.length) {
484
+ return;
485
+ }
486
+ const next = Buffer.allocUnsafe(
487
+ Math.max(
488
+ required,
489
+ Math.min(0x7fffffff, Math.max(64, this.data.length * 2)),
490
+ ),
491
+ );
385
492
  this.data.copy(next, 0, 0, this.length);
386
493
  this.data = next;
387
494
  }