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.
- package/README.ja.md +10 -9
- package/README.ko.md +10 -9
- package/README.md +10 -9
- package/README.zh-CN.md +10 -9
- package/binding.gyp +23 -16
- package/dist/audio/effect-processor.js +9 -3
- package/dist/audio/input-sound-file.js +41 -15
- package/dist/audio/listener.js +33 -11
- package/dist/audio/music.js +41 -18
- package/dist/audio/output-sound-file.js +4 -2
- package/dist/audio/playback-device.js +11 -3
- package/dist/audio/sound-buffer-recorder.js +8 -5
- package/dist/audio/sound-buffer.js +40 -17
- package/dist/audio/sound-file-factory.js +21 -9
- package/dist/audio/sound-file-reader.js +21 -7
- package/dist/audio/sound-recorder.js +36 -15
- package/dist/audio/sound-source.js +117 -40
- package/dist/audio/sound-stream.js +14 -5
- package/dist/audio/sound.js +9 -4
- package/dist/audio/value.js +42 -16
- package/dist/graphics/blend-mode.js +2 -1
- package/dist/graphics/boolean-vector.d.ts +1 -2
- package/dist/graphics/boolean-vector.js +19 -9
- package/dist/graphics/color.js +2 -1
- package/dist/graphics/convex-shape.js +7 -3
- package/dist/graphics/font.js +52 -23
- package/dist/graphics/glsl-matrix.js +8 -3
- package/dist/graphics/glsl.js +47 -16
- package/dist/graphics/glyph.js +27 -10
- package/dist/graphics/image.js +30 -14
- package/dist/graphics/rect.js +15 -8
- package/dist/graphics/render-states.js +18 -9
- package/dist/graphics/render-target.js +49 -23
- package/dist/graphics/render-texture.js +24 -12
- package/dist/graphics/render-window.js +6 -3
- package/dist/graphics/shader.js +67 -32
- package/dist/graphics/shape.js +25 -13
- package/dist/graphics/sprite.js +19 -9
- package/dist/graphics/stencil-mode.js +10 -6
- package/dist/graphics/swap.js +2 -1
- package/dist/graphics/text.js +123 -48
- package/dist/graphics/texture.js +48 -24
- package/dist/graphics/transform.js +29 -13
- package/dist/graphics/transformable.js +2 -1
- package/dist/graphics/vertex-array.js +40 -18
- package/dist/graphics/vertex-buffer.js +39 -19
- package/dist/graphics/vertex.js +6 -3
- package/dist/graphics/view.js +30 -14
- package/dist/index.d.ts +4 -4
- package/dist/internal/audio-codec-registry.js +12 -6
- package/dist/internal/audio-input-stream.js +2 -1
- package/dist/internal/audio-music-pump.js +58 -24
- package/dist/internal/audio-recorder-session.js +17 -7
- package/dist/internal/audio-worker-session.js +28 -10
- package/dist/internal/audio-worker.js +40 -21
- package/dist/internal/glsl-value.d.ts +3 -1
- package/dist/internal/glsl-value.js +18 -8
- package/dist/internal/graphics-state.js +29 -15
- package/dist/internal/input-stream.d.ts +3 -1
- package/dist/internal/input-stream.js +16 -7
- package/dist/internal/numeric-properties.js +2 -1
- package/dist/internal/render-states.js +2 -1
- package/dist/internal/scalar.js +22 -13
- package/dist/internal/shaped-glyph.d.ts +3 -1
- package/dist/internal/shaped-glyph.js +12 -4
- package/dist/internal/struct-view.d.ts +3 -1
- package/dist/internal/struct-view.js +3 -1
- package/dist/internal/transformable.js +2 -1
- package/dist/network/common.js +23 -11
- package/dist/network/dns.js +25 -17
- package/dist/network/ftp.js +43 -18
- package/dist/network/http.js +126 -56
- package/dist/network/ip-address.js +50 -25
- package/dist/network/packet.js +63 -25
- package/dist/network/sftp.js +175 -74
- package/dist/network/socket-selector.js +28 -14
- package/dist/network/socket.js +71 -30
- package/dist/network/tcp-listener.js +42 -19
- package/dist/network/tcp-socket.js +111 -53
- package/dist/network/tls-material.js +2 -1
- package/dist/network/udp-socket.js +68 -33
- package/dist/system/angle.js +2 -1
- package/dist/system/clock.js +4 -2
- package/dist/system/err.js +5 -2
- package/dist/system/file-input-stream.js +8 -4
- package/dist/system/input-stream.js +6 -3
- package/dist/system/operators.d.ts +1 -1
- package/dist/system/operators.js +90 -50
- package/dist/system/sleep.js +4 -2
- package/dist/system/time.js +12 -6
- package/dist/system/vector2.js +6 -7
- package/dist/system/vector3.js +11 -6
- package/dist/window/clipboard.js +2 -1
- package/dist/window/context.js +10 -5
- package/dist/window/cursor.js +2 -1
- package/dist/window/event.js +9 -5
- package/dist/window/mouse.js +4 -2
- package/dist/window/video-mode.js +2 -1
- package/dist/window/window.js +19 -12
- package/native/addon.cc +25 -12
- package/native/audio-binding.cc +266 -126
- package/native/audio-codec-binding.cc +147 -77
- package/native/audio-device-binding.cc +76 -49
- package/native/audio-file-binding.cc +116 -67
- package/native/audio-recorder-binding.cc +147 -98
- package/native/audio-recorder.h +1 -1
- package/native/audio-state.h +53 -30
- package/native/audio-stream-binding.cc +148 -94
- package/native/audio-stream.h +5 -4
- package/native/audio-worker-binding.cc +163 -81
- package/native/audio-worker.h +35 -19
- package/native/batch-drawable.cc +29 -16
- package/native/batch-drawable.h +11 -8
- package/native/context-binding.cc +40 -27
- package/native/context-settings.h +15 -8
- package/native/deferred-delete.cc +9 -6
- package/native/deferred-delete.h +7 -8
- package/native/error-stream-binding.cc +42 -28
- package/native/font-binding.cc +89 -48
- package/native/graphics-binding.cc +122 -72
- package/native/graphics-state.h +50 -36
- package/native/image-binding.cc +38 -24
- package/native/image-state.h +26 -18
- package/native/input-binding.cc +42 -27
- package/native/render-window-binding.cc +38 -23
- package/native/shader-binding.cc +115 -50
- package/native/shape-binding.cc +94 -49
- package/native/shape-state.h +18 -9
- package/native/shaped-glyph-storage.h +9 -5
- package/native/stream-binding.cc +143 -117
- package/native/system-binding.cc +17 -12
- package/native/text-binding.cc +68 -35
- package/native/text-state.h +25 -18
- package/native/texture-binding.cc +42 -33
- package/native/texture-resource.h +20 -12
- package/native/time-value.h +3 -1
- package/native/transformable-binding.cc +12 -6
- package/native/transformable-state.h +6 -6
- package/native/value-binding.h +46 -28
- package/native/value-layout.h +10 -10
- package/native/values-binding.cc +29 -23
- package/native/vertex-binding.cc +18 -12
- package/native/vertex-buffer-binding.cc +38 -24
- package/native/vertex-storage.h +2 -2
- package/native/view-data.h +4 -3
- package/native/window-binding.cc +75 -45
- package/native/window-event.h +20 -11
- package/native/window-owner.h +15 -12
- package/package.json +40 -48
- package/src/audio/effect-processor.ts +9 -3
- package/src/audio/input-sound-file.ts +108 -36
- package/src/audio/listener.ts +39 -13
- package/src/audio/music.ts +112 -47
- package/src/audio/output-sound-file.ts +40 -11
- package/src/audio/playback-device.ts +23 -8
- package/src/audio/sound-buffer-recorder.ts +35 -13
- package/src/audio/sound-buffer.ts +166 -58
- package/src/audio/sound-file-factory.ts +127 -49
- package/src/audio/sound-file-reader.ts +49 -15
- package/src/audio/sound-recorder.ts +100 -34
- package/src/audio/sound-source.ts +142 -45
- package/src/audio/sound-stream.ts +15 -5
- package/src/audio/sound.ts +12 -4
- package/src/audio/value.ts +89 -26
- package/src/graphics/blend-mode.ts +3 -1
- package/src/graphics/boolean-vector.ts +56 -26
- package/src/graphics/color.ts +3 -1
- package/src/graphics/convex-shape.ts +10 -3
- package/src/graphics/font.ts +171 -50
- package/src/graphics/glsl-matrix.ts +9 -3
- package/src/graphics/glsl.ts +81 -35
- package/src/graphics/glyph.ts +67 -26
- package/src/graphics/image.ts +119 -45
- package/src/graphics/rect.ts +56 -24
- package/src/graphics/render-states.ts +31 -14
- package/src/graphics/render-target.ts +197 -81
- package/src/graphics/render-texture.ts +36 -12
- package/src/graphics/render-window.ts +9 -3
- package/src/graphics/shader.ts +186 -61
- package/src/graphics/shape.ts +93 -35
- package/src/graphics/sprite.ts +64 -25
- package/src/graphics/stencil-mode.ts +12 -6
- package/src/graphics/swap.ts +3 -2
- package/src/graphics/text.ts +229 -74
- package/src/graphics/texture.ts +180 -61
- package/src/graphics/transform.ts +132 -47
- package/src/graphics/transformable.ts +3 -1
- package/src/graphics/vertex-array.ts +87 -35
- package/src/graphics/vertex-buffer.ts +97 -33
- package/src/graphics/vertex.ts +9 -3
- package/src/graphics/view.ts +59 -21
- package/src/index.ts +101 -23
- package/src/internal/audio-codec-registry.ts +18 -6
- package/src/internal/audio-input-stream.ts +6 -2
- package/src/internal/audio-music-pump.ts +116 -41
- package/src/internal/audio-native.ts +249 -96
- package/src/internal/audio-recorder-session.ts +68 -24
- package/src/internal/audio-worker-session.ts +65 -21
- package/src/internal/audio-worker.ts +85 -41
- package/src/internal/glsl-value.ts +26 -11
- package/src/internal/graphics-state.ts +57 -20
- package/src/internal/input-stream.ts +23 -10
- package/src/internal/native.ts +252 -99
- package/src/internal/numeric-properties.ts +2 -1
- package/src/internal/render-states.ts +3 -1
- package/src/internal/scalar.ts +33 -14
- package/src/internal/shaped-glyph.ts +25 -12
- package/src/internal/struct-view.ts +40 -13
- package/src/internal/transformable.ts +3 -1
- package/src/network/common.ts +35 -11
- package/src/network/dns.ts +95 -38
- package/src/network/ftp.ts +129 -47
- package/src/network/http.ts +232 -77
- package/src/network/ip-address.ts +118 -48
- package/src/network/packet.ts +161 -54
- package/src/network/sftp.ts +498 -168
- package/src/network/socket-selector.ts +66 -26
- package/src/network/socket.ts +133 -46
- package/src/network/tcp-listener.ts +78 -29
- package/src/network/tcp-socket.ts +240 -95
- package/src/network/tls-material.ts +6 -2
- package/src/network/udp-socket.ts +147 -53
- package/src/system/angle.ts +3 -1
- package/src/system/clock.ts +12 -4
- package/src/system/err.ts +5 -2
- package/src/system/file-input-stream.ts +12 -4
- package/src/system/input-stream.ts +9 -3
- package/src/system/operators.ts +290 -115
- package/src/system/sleep.ts +6 -2
- package/src/system/time.ts +18 -6
- package/src/system/timeout-with-predicate.ts +19 -8
- package/src/system/vector2.ts +101 -49
- package/src/system/vector3.ts +85 -36
- package/src/window/clipboard.ts +3 -1
- package/src/window/context.ts +15 -5
- package/src/window/cursor.ts +3 -1
- package/src/window/event.ts +26 -12
- package/src/window/mouse.ts +6 -2
- package/src/window/video-mode.ts +3 -1
- package/src/window/window.ts +119 -37
- package/test/native/audio-probes.cc +74 -36
- package/test/native/deferred-delete-probes.cc +17 -11
- package/test/native/drawable-probes.cc +70 -36
- package/test/native/exception-probes.cc +14 -9
- package/test/native/image-probes.cc +20 -10
- package/test/native/matrix-probes.cc +8 -4
- package/test/native/network-oracle.cc +17 -11
- package/test/native/render-texture-probes.cc +33 -24
- package/test/native/shader-probes.cc +9 -9
- package/test/native/shape-probes.cc +37 -23
- package/test/native/sprite-probes.cc +41 -25
- package/test/native/system-probes.cc +58 -35
- package/test/native/text-probes.cc +77 -44
- package/test/native/texture-probes.cc +43 -26
- package/test/native/time-probes.cc +18 -12
- package/test/native/transform-probes.cc +15 -10
- package/test/native/transformable-probes.cc +23 -17
- package/test/native/value-oracle.h +9 -4
- package/test/native/value-probes.cc +31 -23
- package/test/native/vertex-array-probes.cc +22 -14
- package/test/native/vertex-buffer-probes.cc +28 -17
- package/test/native/view-probes.cc +29 -17
- package/test/native/window-probes.cc +66 -33
- package/tools/ensure-sfml.cjs +9 -0
- package/tools/generate-network-constants.cjs +11 -6
- package/tools/generate-window-constants.cjs +31 -12
- package/docs/README.ja.md +0 -30
- package/docs/README.ko.md +0 -30
- package/docs/README.md +0 -30
- package/docs/README.zh-CN.md +0 -30
- package/docs/api.ja.md +0 -84
- package/docs/api.ko.md +0 -84
- package/docs/api.md +0 -81
- package/docs/api.zh-CN.md +0 -81
- package/docs/audio.ja.md +0 -249
- package/docs/audio.ko.md +0 -251
- package/docs/audio.md +0 -252
- package/docs/audio.zh-CN.md +0 -252
- package/docs/concepts.ja.md +0 -130
- package/docs/concepts.ko.md +0 -130
- package/docs/concepts.md +0 -131
- package/docs/concepts.zh-CN.md +0 -131
- package/docs/development.ja.md +0 -144
- package/docs/development.ko.md +0 -144
- package/docs/development.md +0 -146
- package/docs/development.zh-CN.md +0 -146
- package/docs/getting-started.ja.md +0 -154
- package/docs/getting-started.ko.md +0 -154
- package/docs/getting-started.md +0 -156
- package/docs/getting-started.zh-CN.md +0 -156
- package/docs/graphics.ja.md +0 -256
- package/docs/graphics.ko.md +0 -254
- package/docs/graphics.md +0 -255
- package/docs/graphics.zh-CN.md +0 -257
- package/docs/network.ja.md +0 -214
- package/docs/network.ko.md +0 -216
- package/docs/network.md +0 -218
- package/docs/network.zh-CN.md +0 -218
- package/docs/system.ja.md +0 -169
- package/docs/system.ko.md +0 -171
- package/docs/system.md +0 -173
- package/docs/system.zh-CN.md +0 -173
- package/docs/window.ja.md +0 -138
- package/docs/window.ko.md +0 -140
- package/docs/window.md +0 -141
- package/docs/window.zh-CN.md +0 -141
- package/native/exception-boundary.h +0 -97
- package/native/numeric-fields.h +0 -96
- package/native/shared-handle.h +0 -107
- package/test/tsconfig.json +0 -8
- package/tools/configure-lsp.cjs +0 -23
- package/tools/lib_downloader.cjs +0 -92
- package/tools/lib_downloader_urls.cjs +0 -23
package/src/internal/scalar.ts
CHANGED
|
@@ -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
|
|
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')
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (type === '
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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')
|
|
74
|
-
|
|
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))
|
|
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
|
-
/**
|
|
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(
|
|
41
|
-
|
|
42
|
-
|
|
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(
|
|
52
|
-
|
|
53
|
-
|
|
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 {
|
|
66
|
-
|
|
67
|
-
|
|
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 =
|
|
2
|
-
|
|
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) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
/**
|
|
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,
|
|
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(
|
|
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(
|
|
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,
|
|
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(
|
|
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))
|
|
139
|
+
if (!(value instanceof Angle)) {
|
|
140
|
+
throw new TypeError('Expected an Angle');
|
|
141
|
+
}
|
|
140
142
|
return value.asRadians();
|
|
141
143
|
}
|
|
142
144
|
|
package/src/network/common.ts
CHANGED
|
@@ -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))
|
|
7
|
+
if (!(time instanceof Time)) {
|
|
8
|
+
throw new TypeError('Expected a Time');
|
|
9
|
+
}
|
|
8
10
|
const value = time.asMicroseconds();
|
|
9
|
-
if (value < 0n)
|
|
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))
|
|
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)
|
|
45
|
-
|
|
46
|
-
|
|
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 {
|
|
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)
|
|
55
|
-
|
|
56
|
-
|
|
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
|
}
|
package/src/network/dns.ts
CHANGED
|
@@ -61,18 +61,28 @@ export interface SrvRecord {
|
|
|
61
61
|
*/
|
|
62
62
|
priority: number;
|
|
63
63
|
}
|
|
64
|
-
async function query<T>(
|
|
65
|
-
|
|
66
|
-
|
|
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)
|
|
74
|
-
|
|
75
|
-
|
|
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(
|
|
92
|
-
|
|
93
|
-
|
|
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)
|
|
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 {
|
|
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),
|
|
120
|
+
resolver.resolve4(hostname),
|
|
121
|
+
resolver.resolve6(hostname),
|
|
105
122
|
]);
|
|
106
123
|
const addresses = results.flatMap(result =>
|
|
107
|
-
(result.status === 'fulfilled' ? result.value : [])
|
|
108
|
-
|
|
109
|
-
|
|
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(
|
|
123
|
-
|
|
124
|
-
|
|
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(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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(
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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(
|
|
168
|
-
|
|
169
|
-
|
|
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(
|
|
182
|
-
|
|
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 = [
|
|
185
|
-
|
|
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
|
}
|