sfml.js 1.0.0 → 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 (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 +40 -48
  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
@@ -27,7 +27,8 @@ export type FloatScalar = 'f32' | 'f64';
27
27
  type ScalarValues = { [T in NumberScalar]: number } &
28
28
  { [T in BigIntScalar]: bigint };
29
29
  export type Scalar<T extends ScalarType> = ScalarValues[T];
30
- // Explicit view types remain compatible with TS 5.0 and TS 5.9's generic buffers.
30
+ // Explicit view types remain compatible with TS 5.0
31
+ // and TS 5.9's generic buffers.
31
32
  interface ScalarViews {
32
33
  i8: Int8Array;
33
34
  u8: Uint8Array;
@@ -53,16 +54,28 @@ export function validateScalar(type: string): asserts type is ScalarType {
53
54
  }
54
55
 
55
56
  export function toScalar(type: ScalarType, value: number): number {
56
- if (type === 'f32') return Math.fround(value);
57
- if (type === 'f64') return value;
58
- // eslint-disable-next-line no-bitwise
59
- if (type === 'i8') return (value << 24) >> 24;
60
- // eslint-disable-next-line no-bitwise
61
- if (type === 'u8') return value & 255;
62
- // eslint-disable-next-line no-bitwise
63
- if (type === 'i16') return (value << 16) >> 16;
64
- // eslint-disable-next-line no-bitwise
65
- if (type === 'u16') return value & 65535;
57
+ if (type === 'f32') {
58
+ return Math.fround(value);
59
+ }
60
+ if (type === 'f64') {
61
+ return value;
62
+ }
63
+ if (type === 'i8') {
64
+ // eslint-disable-next-line no-bitwise
65
+ return (value << 24) >> 24;
66
+ }
67
+ if (type === 'u8') {
68
+ // eslint-disable-next-line no-bitwise
69
+ return value & 255;
70
+ }
71
+ if (type === 'i16') {
72
+ // eslint-disable-next-line no-bitwise
73
+ return (value << 16) >> 16;
74
+ }
75
+ if (type === 'u16') {
76
+ // eslint-disable-next-line no-bitwise
77
+ return value & 65535;
78
+ }
66
79
  // eslint-disable-next-line no-bitwise
67
80
  return type === 'i32' ? value | 0 : value >>> 0;
68
81
  }
@@ -70,8 +83,12 @@ export function toScalar(type: ScalarType, value: number): number {
70
83
  export function scalarProduct(
71
84
  type: ScalarType, lhs: number, rhs: number,
72
85
  ): number {
73
- if (type === 'f32') return Math.fround(lhs * rhs);
74
- if (type === 'f64') return lhs * rhs;
86
+ if (type === 'f32') {
87
+ return Math.fround(lhs * rhs);
88
+ }
89
+ if (type === 'f64') {
90
+ return lhs * rhs;
91
+ }
75
92
  return Math.imul(lhs, rhs);
76
93
  }
77
94
 
@@ -104,7 +121,9 @@ export function convertScalar(
104
121
  const rest = magnitude - (rounded << bits);
105
122
  const half = 1n << (bits - 1n);
106
123
  /* eslint-enable no-bitwise */
107
- if (rest > half || (rest === half && rounded % 2n !== 0n)) rounded++;
124
+ if (rest > half || (rest === half && rounded % 2n !== 0n)) {
125
+ rounded++;
126
+ }
108
127
  return (negative ? -1 : 1) * Number(rounded) * 2 ** shift;
109
128
  }
110
129
  } else if (type !== 'f64') {
@@ -6,7 +6,9 @@ import { glyphFromState, type Glyph } from '../graphics/glyph';
6
6
  import { vector2FromState, type Vector2f } from '../system/vector2';
7
7
  import type { ShapedGlyph, TextDirection } from '../graphics/text';
8
8
 
9
- /** One set of typed views per native allocation, shared by all glyph wrappers. */
9
+ /**
10
+ * One set of typed views per native allocation, shared by all glyph wrappers.
11
+ */
10
12
  export class ShapedGlyphBuffer {
11
13
  readonly bytes: Uint8Array;
12
14
  readonly floats: Float32Array;
@@ -37,10 +39,12 @@ export class ShapedGlyphView implements ShapedGlyph {
37
39
  get glyph(): R<Glyph> {
38
40
  if (!this.glyphView) {
39
41
  const { storage, index } = this;
40
- this.glyphView = glyphFromState(new LazyValue(
41
- storage.bytes.subarray(index * 80, index * 80 + 44),
42
- () => native.shapedGlyphMember(storage.mapping.handle, index, 0),
43
- ));
42
+ this.glyphView = glyphFromState(
43
+ new LazyValue(
44
+ storage.bytes.subarray(index * 80, index * 80 + 44),
45
+ () => native.shapedGlyphMember(storage.mapping.handle, index, 0),
46
+ ),
47
+ );
44
48
  }
45
49
  return this.glyphView;
46
50
  }
@@ -48,10 +52,13 @@ export class ShapedGlyphView implements ShapedGlyph {
48
52
  get position(): R<Vector2f> {
49
53
  if (!this.positionView) {
50
54
  const { storage, index } = this;
51
- this.positionView = vector2FromState('f32', new LazyValue(
52
- storage.floats.subarray(index * 20 + 11, index * 20 + 13),
53
- () => native.shapedGlyphMember(storage.mapping.handle, index, 1),
54
- ));
55
+ this.positionView = vector2FromState(
56
+ 'f32',
57
+ new LazyValue(
58
+ storage.floats.subarray(index * 20 + 11, index * 20 + 13),
59
+ () => native.shapedGlyphMember(storage.mapping.handle, index, 1),
60
+ ),
61
+ );
55
62
  }
56
63
  return this.positionView;
57
64
  }
@@ -62,7 +69,13 @@ export class ShapedGlyphView implements ShapedGlyph {
62
69
  get textDirection(): TextDirection {
63
70
  return this.storage.words[this.index * 20 + 14];
64
71
  }
65
- get baseline(): number { return this.storage.floats[this.index * 20 + 15]; }
66
- get vertexOffset(): number { return this.storage.doubles[this.index * 10 + 8]; }
67
- get vertexCount(): number { return this.storage.doubles[this.index * 10 + 9]; }
72
+ get baseline(): number {
73
+ return this.storage.floats[this.index * 20 + 15];
74
+ }
75
+ get vertexOffset(): number {
76
+ return this.storage.doubles[this.index * 10 + 8];
77
+ }
78
+ get vertexCount(): number {
79
+ return this.storage.doubles[this.index * 10 + 9];
80
+ }
68
81
  }
@@ -1,5 +1,16 @@
1
- type FieldType = 'i8' | 'u8' | 'bool' | 'u32' | 'f32' | 'f64' | 'i64' | 'u64' | 'size64';
2
- export type StructFields<T> = Partial<Record<keyof T, readonly [FieldType, number]>>;
1
+ type FieldType =
2
+ | 'i8'
3
+ | 'u8'
4
+ | 'bool'
5
+ | 'u32'
6
+ | 'f32'
7
+ | 'f64'
8
+ | 'i64'
9
+ | 'u64'
10
+ | 'size64';
11
+ export type StructFields<T> = Partial<
12
+ Record<keyof T, readonly [FieldType, number]>
13
+ >;
3
14
 
4
15
  export interface NativeStruct {
5
16
  buffers: ArrayBuffer[];
@@ -11,32 +22,46 @@ const readers = {
11
22
  i8: (data: DataView, offset: number) => data.getInt8(offset),
12
23
  u8: (data: DataView, offset: number) => data.getUint8(offset),
13
24
  bool: (data: DataView, offset: number) => data.getUint8(offset) !== 0,
14
- u32: (data: DataView, offset: number) => data.getUint32(offset, littleEndian),
15
- f32: (data: DataView, offset: number) => data.getFloat32(offset, littleEndian),
16
- f64: (data: DataView, offset: number) => data.getFloat64(offset, littleEndian),
17
- u64: (data: DataView, offset: number) => data.getBigUint64(offset, littleEndian),
18
- i64: (data: DataView, offset: number) => data.getBigInt64(offset, littleEndian),
25
+ u32: (data: DataView, offset: number) =>
26
+ data.getUint32(offset, littleEndian),
27
+ f32: (data: DataView, offset: number) =>
28
+ data.getFloat32(offset, littleEndian),
29
+ f64: (data: DataView, offset: number) =>
30
+ data.getFloat64(offset, littleEndian),
31
+ u64: (data: DataView, offset: number) =>
32
+ data.getBigUint64(offset, littleEndian),
33
+ i64: (data: DataView, offset: number) =>
34
+ data.getBigInt64(offset, littleEndian),
19
35
  size64: (data: DataView, offset: number) =>
20
36
  Number(data.getBigUint64(offset, littleEndian)),
21
37
  };
22
38
 
23
- /** Read-only resource metadata, with offsets supplied by native member pointers. */
39
+ /**
40
+ * Read-only resource metadata, with offsets supplied by native member pointers.
41
+ */
24
42
  export function nativeStruct<T extends object>(
25
- mapping: NativeStruct, extra: Partial<T> = {},
43
+ mapping: NativeStruct,
44
+ extra: Partial<T> = {},
26
45
  ): Readonly<T> {
27
46
  const data = mapping.buffers.map(buffer => new DataView(buffer));
28
47
  const descriptors: PropertyDescriptorMap = {};
29
- for (const [ key, [ type, region, offset ]] of Object.entries(mapping.fields)) {
48
+ for (const [ key, [ type, region, offset ]] of Object.entries(
49
+ mapping.fields,
50
+ )) {
30
51
  const view = data[region];
31
52
  const read = readers[type];
32
53
  descriptors[key] = { enumerable: true, get: () => read(view, offset) };
33
54
  }
34
- return Object.freeze(Object.defineProperties({ ...extra }, descriptors)) as T;
55
+ return Object.freeze(
56
+ Object.defineProperties({ ...extra }, descriptors),
57
+ ) as T;
35
58
  }
36
59
 
37
60
  /** Frozen named getters over native-owned bytes; no mirrored scalar fields. */
38
61
  export function readonlyStruct<T extends object>(
39
- buffer: ArrayBuffer, fields: StructFields<T>, extra: Partial<T> = {},
62
+ buffer: ArrayBuffer,
63
+ fields: StructFields<T>,
64
+ extra: Partial<T> = {},
40
65
  ): Readonly<T> {
41
66
  const data = new DataView(buffer);
42
67
  const descriptors: PropertyDescriptorMap = {};
@@ -45,5 +70,7 @@ export function readonlyStruct<T extends object>(
45
70
  const read = readers[type];
46
71
  descriptors[key] = { enumerable: true, get: () => read(data, offset) };
47
72
  }
48
- return Object.freeze(Object.defineProperties({ ...extra }, descriptors)) as T;
73
+ return Object.freeze(
74
+ Object.defineProperties({ ...extra }, descriptors),
75
+ ) as T;
49
76
  }
@@ -136,7 +136,9 @@ function vectorData(value: R<Vector2f>): Float32Array {
136
136
  }
137
137
 
138
138
  function angleData(value: R<Angle>): number {
139
- if (!(value instanceof Angle)) throw new TypeError('Expected an Angle');
139
+ if (!(value instanceof Angle)) {
140
+ throw new TypeError('Expected an Angle');
141
+ }
140
142
  return value.asRadians();
141
143
  }
142
144
 
@@ -4,9 +4,13 @@ import type { ReadonlyValue } from '../internal/value-storage';
4
4
 
5
5
  export type NetworkTime = ReadonlyValue<Time>;
6
6
  export function millisecondsOf(time: NetworkTime): number {
7
- if (!(time instanceof Time)) throw new TypeError('Expected a Time');
7
+ if (!(time instanceof Time)) {
8
+ throw new TypeError('Expected a Time');
9
+ }
8
10
  const value = time.asMicroseconds();
9
- if (value < 0n) throw new RangeError('Timeout must be non-negative');
11
+ if (value < 0n) {
12
+ throw new RangeError('Timeout must be non-negative');
13
+ }
10
14
  return value === 0n ? 0 : Math.max(1, Number(value / 1000n));
11
15
  }
12
16
  export function uint(value: number, max = 0xffffffff): number {
@@ -16,7 +20,9 @@ export function uint(value: number, max = 0xffffffff): number {
16
20
  return value;
17
21
  }
18
22
  export function bytes(value: Uint8Array): Buffer {
19
- if (!(value instanceof Uint8Array)) throw new TypeError('Expected bytes');
23
+ if (!(value instanceof Uint8Array)) {
24
+ throw new TypeError('Expected bytes');
25
+ }
20
26
  return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
21
27
  }
22
28
  export class SerialQueue {
@@ -41,18 +47,36 @@ export function waitFor(events: EventEmitter, check: () => boolean,
41
47
  function finish(value: boolean, error?: unknown) {
42
48
  events.off('change', inspect);
43
49
  signal?.removeEventListener('abort', abort);
44
- if (timer) clearTimeout(timer);
45
- if (error) reject(error);
46
- else resolve(value);
50
+ if (timer) {
51
+ clearTimeout(timer);
52
+ }
53
+ if (error) {
54
+ reject(error);
55
+ } else {
56
+ resolve(value);
57
+ }
47
58
  }
48
59
  function inspect() {
49
- try { if (check()) finish(true); } catch (error) { finish(false, error); }
60
+ try {
61
+ if (check()) {
62
+ finish(true);
63
+ }
64
+ } catch (error) {
65
+ finish(false, error);
66
+ }
67
+ }
68
+ function abort() {
69
+ finish(false);
50
70
  }
51
- function abort() { finish(false); }
52
71
  events.on('change', inspect);
53
72
  signal?.addEventListener('abort', abort, { once: true });
54
- if (timeout) timer = setTimeout(() => finish(false), timeout);
55
- if (signal?.aborted) abort();
56
- else inspect();
73
+ if (timeout) {
74
+ timer = setTimeout(() => finish(false), timeout);
75
+ }
76
+ if (signal?.aborted) {
77
+ abort();
78
+ } else {
79
+ inspect();
80
+ }
57
81
  });
58
82
  }
@@ -61,18 +61,28 @@ export interface SrvRecord {
61
61
  */
62
62
  priority: number;
63
63
  }
64
- async function query<T>(servers: readonly IpAddress[],
65
- timeout: NetworkTime | undefined, action: (resolver: Resolver) => Promise<T>):
66
- Promise<T | undefined> {
64
+ async function query<T>(
65
+ servers: readonly IpAddress[],
66
+ timeout: NetworkTime | undefined,
67
+ action: (resolver: Resolver) => Promise<T>,
68
+ ): Promise<T | undefined> {
67
69
  const milliseconds = timeout ? millisecondsOf(timeout) : 0;
68
70
  const resolver = new Resolver({ timeout: milliseconds || -1, tries: 1 });
69
71
  if (servers.length) {
70
72
  resolver.setServers(servers.map(address => address.toString()));
71
73
  }
72
74
  let timer: NodeJS.Timeout | undefined;
73
- if (milliseconds) timer = setTimeout(() => resolver.cancel(), milliseconds);
74
- try { return await action(resolver); } catch { return undefined; } finally {
75
- if (timer) clearTimeout(timer);
75
+ if (milliseconds) {
76
+ timer = setTimeout(() => resolver.cancel(), milliseconds);
77
+ }
78
+ try {
79
+ return await action(resolver);
80
+ } catch {
81
+ return undefined;
82
+ } finally {
83
+ if (timer) {
84
+ clearTimeout(timer);
85
+ }
76
86
  }
77
87
  }
78
88
  /**
@@ -88,25 +98,37 @@ async function query<T>(servers: readonly IpAddress[],
88
98
  * servers가 비어 있으면 시스템 리졸버를 사용하고 timeout은 적용하지 않습니다.
89
99
  * DNS 서버를 지정하면 기본 제한 시간은 1초입니다.
90
100
  */
91
- export async function resolve(hostname: string,
92
- servers: readonly IpAddress[] = [], timeout: NetworkTime | undefined = seconds(1)):
93
- Promise<IpAddress[] | undefined> {
101
+ export async function resolve(
102
+ hostname: string,
103
+ servers: readonly IpAddress[] = [],
104
+ timeout: NetworkTime | undefined = seconds(1),
105
+ ): Promise<IpAddress[] | undefined> {
94
106
  const literal = IpAddress.fromString(hostname);
95
- if (literal) return [ literal ];
107
+ if (literal) {
108
+ return [ literal ];
109
+ }
96
110
  if (!servers.length) {
97
111
  try {
98
112
  const records = await lookup(hostname, { all: true });
99
113
  return records.map(record => IpAddress.fromString(record.address)!);
100
- } catch { return undefined; }
114
+ } catch {
115
+ return undefined;
116
+ }
101
117
  }
102
118
  return query(servers, timeout, async resolver => {
103
119
  const results = await Promise.allSettled([
104
- resolver.resolve4(hostname), resolver.resolve6(hostname),
120
+ resolver.resolve4(hostname),
121
+ resolver.resolve6(hostname),
105
122
  ]);
106
123
  const addresses = results.flatMap(result =>
107
- (result.status === 'fulfilled' ? result.value : []));
108
- if (!addresses.length && results.every(result =>
109
- result.status === 'rejected')) throw new Error('DNS lookup failed');
124
+ (result.status === 'fulfilled' ? result.value : []),
125
+ );
126
+ if (
127
+ !addresses.length &&
128
+ results.every(result => result.status === 'rejected')
129
+ ) {
130
+ throw new Error('DNS lookup failed');
131
+ }
110
132
  return addresses.map(address => IpAddress.fromString(address)!);
111
133
  });
112
134
  }
@@ -119,10 +141,16 @@ export async function resolve(hostname: string,
119
141
  * @ko 네임 서버 레코드를 조회합니다. 기본값은 시스템 DNS와 1초 제한 시간이며,
120
142
  * 조회 실패 시 빈 배열을 반환합니다.
121
143
  */
122
- export async function queryNs(hostname: string, servers: readonly IpAddress[] = [],
123
- timeout: NetworkTime | undefined = seconds(1)): Promise<string[]> {
124
- return await query(servers, timeout, resolver => resolver.resolveNs(hostname))
125
- ?? [];
144
+ export async function queryNs(
145
+ hostname: string,
146
+ servers: readonly IpAddress[] = [],
147
+ timeout: NetworkTime | undefined = seconds(1),
148
+ ): Promise<string[]> {
149
+ return (
150
+ (await query(servers, timeout, resolver =>
151
+ resolver.resolveNs(hostname),
152
+ )) ?? []
153
+ );
126
154
  }
127
155
  /**
128
156
  * Query mail exchanger records with a default 1-second timeout;
@@ -133,11 +161,18 @@ export async function queryNs(hostname: string, servers: readonly IpAddress[] =
133
161
  * @ko 메일 교환 레코드를 조회합니다. 기본 제한 시간은 1초이며,
134
162
  * 조회 실패 시 빈 배열을 반환합니다.
135
163
  */
136
- export async function queryMx(hostname: string, servers: readonly IpAddress[] = [],
137
- timeout: NetworkTime | undefined = seconds(1)): Promise<MxRecord[]> {
138
- return (await query(servers, timeout, resolver => resolver.resolveMx(hostname))
139
- ?? []).map(record => ({
140
- exchange: record.exchange, preference: record.priority,
164
+ export async function queryMx(
165
+ hostname: string,
166
+ servers: readonly IpAddress[] = [],
167
+ timeout: NetworkTime | undefined = seconds(1),
168
+ ): Promise<MxRecord[]> {
169
+ return (
170
+ (await query(servers, timeout, resolver =>
171
+ resolver.resolveMx(hostname),
172
+ )) ?? []
173
+ ).map(record => ({
174
+ exchange: record.exchange,
175
+ preference: record.priority,
141
176
  }));
142
177
  }
143
178
  /**
@@ -149,11 +184,21 @@ export async function queryMx(hostname: string, servers: readonly IpAddress[] =
149
184
  * @ko 서비스 검색 레코드(예: _service._tcp.domain)를 조회하며,
150
185
  * 실패 시 빈 배열을 반환합니다.
151
186
  */
152
- export async function querySrv(hostname: string, servers: readonly IpAddress[] = [],
153
- timeout: NetworkTime | undefined = seconds(1)): Promise<SrvRecord[]> {
154
- return (await query(servers, timeout, resolver => resolver.resolveSrv(hostname))
155
- ?? []).map(record => ({ target: record.name, port: record.port,
156
- weight: record.weight, priority: record.priority }));
187
+ export async function querySrv(
188
+ hostname: string,
189
+ servers: readonly IpAddress[] = [],
190
+ timeout: NetworkTime | undefined = seconds(1),
191
+ ): Promise<SrvRecord[]> {
192
+ return (
193
+ (await query(servers, timeout, resolver =>
194
+ resolver.resolveSrv(hostname),
195
+ )) ?? []
196
+ ).map(record => ({
197
+ target: record.name,
198
+ port: record.port,
199
+ weight: record.weight,
200
+ priority: record.priority,
201
+ }));
157
202
  }
158
203
  /**
159
204
  * Query TXT records, each containing multiple text segments;
@@ -164,10 +209,16 @@ export async function querySrv(hostname: string, servers: readonly IpAddress[] =
164
209
  * @ko TXT 레코드를 조회합니다. 각 레코드는 여러 텍스트 조각을 포함하며,
165
210
  * 실패 시 빈 배열을 반환합니다.
166
211
  */
167
- export async function queryTxt(hostname: string, servers: readonly IpAddress[] = [],
168
- timeout: NetworkTime | undefined = seconds(1)): Promise<string[][]> {
169
- return await query(servers, timeout, resolver => resolver.resolveTxt(hostname))
170
- ?? [];
212
+ export async function queryTxt(
213
+ hostname: string,
214
+ servers: readonly IpAddress[] = [],
215
+ timeout: NetworkTime | undefined = seconds(1),
216
+ ): Promise<string[][]> {
217
+ return (
218
+ (await query(servers, timeout, resolver =>
219
+ resolver.resolveTxt(hostname),
220
+ )) ?? []
221
+ );
171
222
  }
172
223
  /**
173
224
  * Query the public outbound address for the given address family via OpenDNS;
@@ -178,14 +229,20 @@ export async function queryTxt(hostname: string, servers: readonly IpAddress[] =
178
229
  * @ko OpenDNS로 지정한 주소 계열의 공인 송신 주소를 조회하며,
179
230
  * 실패 시 undefined를 반환합니다.
180
231
  */
181
- export async function getPublicAddress(timeout: NetworkTime | undefined = Time.Zero,
182
- type = IpAddressType.IpV4): Promise<IpAddress | undefined> {
232
+ export async function getPublicAddress(
233
+ timeout: NetworkTime | undefined = Time.Zero,
234
+ type = IpAddressType.IpV4,
235
+ ): Promise<IpAddress | undefined> {
183
236
  uint(type, 1);
184
- const servers = [ IpAddress.fromString(type === IpAddressType.IpV4
185
- ? '208.67.222.222' : '2620:119:35::35')! ];
237
+ const servers = [
238
+ IpAddress.fromString(
239
+ type === IpAddressType.IpV4 ? '208.67.222.222' : '2620:119:35::35',
240
+ )!,
241
+ ];
186
242
  const addresses = await query(servers, timeout, resolver =>
187
243
  (type === IpAddressType.IpV4
188
244
  ? resolver.resolve4('myip.opendns.com')
189
- : resolver.resolve6('myip.opendns.com')));
245
+ : resolver.resolve6('myip.opendns.com')),
246
+ );
190
247
  return addresses?.length ? IpAddress.fromString(addresses[0]) : undefined;
191
248
  }