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