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.
- package/README.ja.md +16 -11
- package/README.ko.md +16 -11
- package/README.md +16 -11
- package/README.zh-CN.md +16 -11
- package/assets/breakout.png +0 -0
- 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 +43 -50
- 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 -148
- package/docs/development.ko.md +0 -148
- package/docs/development.md +0 -150
- package/docs/development.zh-CN.md +0 -150
- 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 -69
- 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
|
@@ -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(
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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([
|
|
72
|
-
uint(
|
|
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')
|
|
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)
|
|
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 =
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
|
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)
|
|
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(
|
|
129
|
-
|
|
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(
|
|
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(
|
|
139
|
-
|
|
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(
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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 ]
|
|
192
|
+
? [ IpAddressType.IpV6, IpAddressType.IpV4 ]
|
|
193
|
+
: [ type ]) {
|
|
160
194
|
const address = await dns.getPublicAddress(timeout, family);
|
|
161
|
-
if (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(
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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())
|
|
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 =
|
|
187
|
-
this.data
|
|
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 (
|
|
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())
|
|
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())
|
|
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 {
|
|
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 {
|
|
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))
|
|
251
|
-
|
|
252
|
-
|
|
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(
|
|
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(
|
|
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(
|
|
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(
|
|
289
|
-
Uint8Array.from({ length: 16 }, (_, i) => +(i === 15)))
|
|
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 的兼容别名。
|
package/src/network/packet.ts
CHANGED
|
@@ -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,
|
|
125
|
-
|
|
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))
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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 {
|
|
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')
|
|
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)
|
|
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)
|
|
284
|
+
if (!count) {
|
|
285
|
+
throw new TypeError('Unknown packet scalar type');
|
|
286
|
+
}
|
|
262
287
|
this.reserve(count);
|
|
263
|
-
const view = new DataView(
|
|
264
|
-
this.data.
|
|
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')
|
|
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')
|
|
270
|
-
|
|
271
|
-
|
|
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')
|
|
308
|
+
if (typeof value !== 'number') {
|
|
309
|
+
throw new TypeError('Expected a number');
|
|
310
|
+
}
|
|
274
311
|
switch (type) {
|
|
275
|
-
case 'i8':
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
case '
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
case '
|
|
282
|
-
|
|
283
|
-
|
|
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))
|
|
302
|
-
|
|
303
|
-
|
|
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)
|
|
366
|
+
if (length === undefined) {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
310
369
|
const count = length * (type === 'string' ? 4 : 1);
|
|
311
|
-
if (!this.check(count))
|
|
370
|
+
if (!this.check(count)) {
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
312
373
|
let result = '';
|
|
313
374
|
if (type === 'utf8') {
|
|
314
|
-
result = this.data.toString(
|
|
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(
|
|
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)
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
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':
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
case '
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
case '
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
case '
|
|
341
|
-
|
|
342
|
-
|
|
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 {
|
|
369
|
-
|
|
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)
|
|
383
|
-
|
|
384
|
-
|
|
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
|
}
|