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
package/src/network/sftp.ts
CHANGED
|
@@ -1,14 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
Client,
|
|
3
|
+
utils,
|
|
4
|
+
type SFTPWrapper,
|
|
5
|
+
type NextAuthHandler,
|
|
6
|
+
type AnyAuthMethod,
|
|
7
|
+
type NegotiatedAlgorithms,
|
|
8
|
+
type Attributes as SshAttributes,
|
|
9
|
+
} from 'ssh2';
|
|
4
10
|
import { Socket as NetSocket } from 'node:net';
|
|
5
11
|
import { EventEmitter } from 'node:events';
|
|
6
12
|
import { createHash } from 'node:crypto';
|
|
7
13
|
import { IpAddress } from './ip-address';
|
|
8
14
|
import { Time } from '../system/time';
|
|
9
15
|
import { TimeoutWithPredicate } from '../system/timeout-with-predicate';
|
|
10
|
-
import {
|
|
11
|
-
|
|
16
|
+
import {
|
|
17
|
+
SerialQueue,
|
|
18
|
+
millisecondsOf,
|
|
19
|
+
bytes,
|
|
20
|
+
uint,
|
|
21
|
+
waitFor,
|
|
22
|
+
type NetworkTime,
|
|
23
|
+
} from './common';
|
|
12
24
|
import { SftpResultValue as Value, SftpHostKeyType } from './gen/constants';
|
|
13
25
|
|
|
14
26
|
export { SftpResultValue, SftpHostKeyType } from './gen/constants';
|
|
@@ -42,28 +54,37 @@ export class SftpResult {
|
|
|
42
54
|
* @ja 結果コードと任意の説明から結果を作成します。
|
|
43
55
|
* @ko 결과 코드와 선택적 설명으로 결과를 생성합니다.
|
|
44
56
|
*/
|
|
45
|
-
constructor(
|
|
57
|
+
constructor(
|
|
58
|
+
private readonly value: Value,
|
|
59
|
+
private readonly message = '',
|
|
60
|
+
) {}
|
|
46
61
|
/**
|
|
47
62
|
* Return whether the result is Success.
|
|
48
63
|
* @zh 返回结果是否为 Success。
|
|
49
64
|
* @ja 結果が Success かどうかを返します。
|
|
50
65
|
* @ko 결과가 Success인지 반환합니다.
|
|
51
66
|
*/
|
|
52
|
-
isOk(): boolean {
|
|
67
|
+
isOk(): boolean {
|
|
68
|
+
return this.value === Value.Success;
|
|
69
|
+
}
|
|
53
70
|
/**
|
|
54
71
|
* Return the result code.
|
|
55
72
|
* @zh 返回结果码。
|
|
56
73
|
* @ja 結果コードを返します。
|
|
57
74
|
* @ko 결과 코드를 반환합니다.
|
|
58
75
|
*/
|
|
59
|
-
getValue(): Value {
|
|
76
|
+
getValue(): Value {
|
|
77
|
+
return this.value;
|
|
78
|
+
}
|
|
60
79
|
/**
|
|
61
80
|
* Return the error or status description.
|
|
62
81
|
* @zh 返回错误或状态说明。
|
|
63
82
|
* @ja エラーまたは状態の説明を返します。
|
|
64
83
|
* @ko 오류 또는 상태 설명을 반환합니다.
|
|
65
84
|
*/
|
|
66
|
-
getMessage(): string {
|
|
85
|
+
getMessage(): string {
|
|
86
|
+
return this.message;
|
|
87
|
+
}
|
|
67
88
|
}
|
|
68
89
|
/**
|
|
69
90
|
* Operation result containing a remote path.
|
|
@@ -78,7 +99,10 @@ export class SftpPathResult extends SftpResult {
|
|
|
78
99
|
* @ja 結果の状態をコピーし、パスを付加します。
|
|
79
100
|
* @ko 결과 상태를 복사하고 경로를 첨부합니다.
|
|
80
101
|
*/
|
|
81
|
-
constructor(
|
|
102
|
+
constructor(
|
|
103
|
+
result: SftpResult,
|
|
104
|
+
private readonly path = '',
|
|
105
|
+
) {
|
|
82
106
|
super(result.getValue(), result.getMessage());
|
|
83
107
|
}
|
|
84
108
|
/**
|
|
@@ -87,7 +111,9 @@ export class SftpPathResult extends SftpResult {
|
|
|
87
111
|
* @ja リモートパスを返します。失敗結果では通常、空文字列です。
|
|
88
112
|
* @ko 원격 경로를 반환하며 실패한 결과에서는 보통 빈 문자열입니다.
|
|
89
113
|
*/
|
|
90
|
-
getPath(): string {
|
|
114
|
+
getPath(): string {
|
|
115
|
+
return this.path;
|
|
116
|
+
}
|
|
91
117
|
}
|
|
92
118
|
/**
|
|
93
119
|
* Remote filesystem entry type.
|
|
@@ -244,8 +270,10 @@ export class SftpAttributesResult extends SftpResult {
|
|
|
244
270
|
* @ja 結果の状態をコピーし、属性オブジェクトを付加します。
|
|
245
271
|
* @ko 결과 상태를 복사하고 속성 객체를 첨부합니다.
|
|
246
272
|
*/
|
|
247
|
-
constructor(
|
|
248
|
-
|
|
273
|
+
constructor(
|
|
274
|
+
result: SftpResult,
|
|
275
|
+
private readonly attributes: SftpAttributes = { path: '' },
|
|
276
|
+
) {
|
|
249
277
|
super(result.getValue(), result.getMessage());
|
|
250
278
|
}
|
|
251
279
|
/**
|
|
@@ -254,7 +282,9 @@ export class SftpAttributesResult extends SftpResult {
|
|
|
254
282
|
* @ja この結果が保持する属性オブジェクトを返します。
|
|
255
283
|
* @ko 이 결과가 보유한 속성 객체를 반환합니다.
|
|
256
284
|
*/
|
|
257
|
-
getAttributes(): SftpAttributes {
|
|
285
|
+
getAttributes(): SftpAttributes {
|
|
286
|
+
return this.attributes;
|
|
287
|
+
}
|
|
258
288
|
}
|
|
259
289
|
/**
|
|
260
290
|
* Result containing a list of directory entry attributes.
|
|
@@ -269,7 +299,10 @@ export class SftpListingResult extends SftpResult {
|
|
|
269
299
|
* @ja 結果の状態をコピーし、エントリー一覧を付加します。
|
|
270
300
|
* @ko 결과 상태를 복사하고 항목 목록을 첨부합니다.
|
|
271
301
|
*/
|
|
272
|
-
constructor(
|
|
302
|
+
constructor(
|
|
303
|
+
result: SftpResult,
|
|
304
|
+
private readonly listing: SftpAttributes[] = [],
|
|
305
|
+
) {
|
|
273
306
|
super(result.getValue(), result.getMessage());
|
|
274
307
|
}
|
|
275
308
|
/**
|
|
@@ -278,7 +311,9 @@ export class SftpListingResult extends SftpResult {
|
|
|
278
311
|
* @ja この結果が保持するディレクトリエントリー一覧を返します。
|
|
279
312
|
* @ko 이 결과가 보유한 디렉터리 항목 목록을 반환합니다.
|
|
280
313
|
*/
|
|
281
|
-
getListing(): readonly SftpAttributes[] {
|
|
314
|
+
getListing(): readonly SftpAttributes[] {
|
|
315
|
+
return this.listing;
|
|
316
|
+
}
|
|
282
317
|
}
|
|
283
318
|
/**
|
|
284
319
|
* SSH handshake negotiation results for application verification
|
|
@@ -392,7 +427,9 @@ export interface SftpSessionInfo {
|
|
|
392
427
|
* @ko 다운로드 청크를 처리하며 false를 반환하면 조기 종료합니다.
|
|
393
428
|
* 빈 청크는 파일 끝을 나타냅니다.
|
|
394
429
|
*/
|
|
395
|
-
export type SftpDownloadCallback = (
|
|
430
|
+
export type SftpDownloadCallback = (
|
|
431
|
+
data: Uint8Array,
|
|
432
|
+
) => boolean | Promise<boolean>;
|
|
396
433
|
/**
|
|
397
434
|
* Writable data chunk received by an upload callback.
|
|
398
435
|
* @zh 上传回调接收的可填充数据块。
|
|
@@ -425,54 +462,106 @@ export interface SftpUploadChunk {
|
|
|
425
462
|
* @ko 업로드 청크를 채우고 size를 설정합니다.
|
|
426
463
|
* false를 반환해도 현재 청크를 보낸 후 종료합니다.
|
|
427
464
|
*/
|
|
428
|
-
export type SftpUploadCallback = (
|
|
465
|
+
export type SftpUploadCallback = (
|
|
466
|
+
chunk: SftpUploadChunk,
|
|
467
|
+
) => boolean | Promise<boolean>;
|
|
429
468
|
class OperationError extends Error {
|
|
430
|
-
constructor(
|
|
469
|
+
constructor(
|
|
470
|
+
readonly value: Value,
|
|
471
|
+
message: string,
|
|
472
|
+
) {
|
|
473
|
+
super(message);
|
|
474
|
+
}
|
|
431
475
|
}
|
|
432
476
|
const ok = () => new SftpResult(Value.Success);
|
|
433
477
|
function position(value: bigint): number {
|
|
434
|
-
if (
|
|
435
|
-
|
|
436
|
-
|
|
478
|
+
if (
|
|
479
|
+
typeof value !== 'bigint' ||
|
|
480
|
+
value < 0n ||
|
|
481
|
+
value > BigInt(Number.MAX_SAFE_INTEGER)
|
|
482
|
+
) {
|
|
483
|
+
throw new RangeError(
|
|
484
|
+
'ssh2 file offsets must be exact integers up to 2^53 - 1',
|
|
485
|
+
);
|
|
437
486
|
}
|
|
438
487
|
return Number(value);
|
|
439
488
|
}
|
|
440
489
|
function result(error: unknown): SftpResult {
|
|
441
|
-
if (error instanceof OperationError)
|
|
490
|
+
if (error instanceof OperationError) {
|
|
491
|
+
return new SftpResult(error.value, error.message);
|
|
492
|
+
}
|
|
442
493
|
const err = error as Error & { code?: number | string; level?: string };
|
|
443
|
-
const statuses = [
|
|
444
|
-
Value.
|
|
445
|
-
Value.
|
|
446
|
-
Value.
|
|
447
|
-
Value.
|
|
448
|
-
Value.
|
|
449
|
-
Value.
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
494
|
+
const statuses = [
|
|
495
|
+
Value.Success,
|
|
496
|
+
Value.EndOfFile,
|
|
497
|
+
Value.NoSuchFile,
|
|
498
|
+
Value.PermissionDenied,
|
|
499
|
+
Value.Failure,
|
|
500
|
+
Value.BadMessage,
|
|
501
|
+
Value.NoConnection,
|
|
502
|
+
Value.ConnectionLost,
|
|
503
|
+
Value.OperationUnsupported,
|
|
504
|
+
Value.InvalidHandle,
|
|
505
|
+
Value.NoSuchPath,
|
|
506
|
+
Value.FileAlreadyExists,
|
|
507
|
+
Value.WriteProtect,
|
|
508
|
+
Value.NoMedia,
|
|
509
|
+
Value.NoSpaceOnFileSystem,
|
|
510
|
+
Value.QuotaExceeded,
|
|
511
|
+
Value.UnknownPrincipal,
|
|
512
|
+
Value.LockConflict,
|
|
513
|
+
Value.DirectoryNotEmpty,
|
|
514
|
+
Value.NotADirectory,
|
|
515
|
+
Value.InvalidFilename,
|
|
516
|
+
Value.LinkLoop,
|
|
517
|
+
];
|
|
518
|
+
const code =
|
|
519
|
+
typeof err.code === 'number'
|
|
520
|
+
? statuses[err.code]
|
|
521
|
+
: err.code === 'ECONNREFUSED'
|
|
522
|
+
? Value.Refused
|
|
523
|
+
: err.level === 'client-authentication'
|
|
524
|
+
? Value.AuthenticationFailed
|
|
525
|
+
: undefined;
|
|
454
526
|
return new SftpResult(code ?? Value.Error, err.message ?? String(error));
|
|
455
527
|
}
|
|
456
|
-
function attributes(
|
|
528
|
+
function attributes(
|
|
529
|
+
path: string,
|
|
530
|
+
input: Partial<SshAttributes>,
|
|
531
|
+
): SftpAttributes {
|
|
457
532
|
const integer = (value?: number) => {
|
|
458
|
-
if (value === undefined)
|
|
533
|
+
if (value === undefined) {
|
|
534
|
+
return undefined;
|
|
535
|
+
}
|
|
459
536
|
if (!Number.isSafeInteger(value) || value < 0) {
|
|
460
537
|
throw new RangeError('SFTP attribute exceeds exact integer range');
|
|
461
538
|
}
|
|
462
539
|
return BigInt(value);
|
|
463
540
|
};
|
|
464
541
|
const types: Record<number, FileType> = {
|
|
465
|
-
8: FileType.Regular,
|
|
466
|
-
|
|
542
|
+
8: FileType.Regular,
|
|
543
|
+
4: FileType.Directory,
|
|
544
|
+
10: FileType.Symlink,
|
|
545
|
+
6: FileType.Block,
|
|
546
|
+
2: FileType.Character,
|
|
547
|
+
1: FileType.Fifo,
|
|
548
|
+
12: FileType.Socket,
|
|
549
|
+
};
|
|
550
|
+
return {
|
|
551
|
+
path,
|
|
552
|
+
type:
|
|
553
|
+
input.mode === undefined
|
|
554
|
+
? undefined
|
|
555
|
+
: (types[Math.floor(input.mode / 4096) % 16] ?? FileType.Unknown),
|
|
556
|
+
size: integer(input.size),
|
|
557
|
+
permissions: input.mode === undefined ? undefined : input.mode % 4096,
|
|
558
|
+
userId: integer(input.uid),
|
|
559
|
+
groupId: integer(input.gid),
|
|
560
|
+
accessTime:
|
|
561
|
+
input.atime === undefined ? undefined : new Date(input.atime * 1000),
|
|
562
|
+
modificationTime:
|
|
563
|
+
input.mtime === undefined ? undefined : new Date(input.mtime * 1000),
|
|
467
564
|
};
|
|
468
|
-
return { path,
|
|
469
|
-
type: input.mode === undefined ? undefined
|
|
470
|
-
: types[Math.floor(input.mode / 4096) % 16] ?? FileType.Unknown,
|
|
471
|
-
size: integer(input.size), permissions: input.mode === undefined
|
|
472
|
-
? undefined : input.mode % 4096,
|
|
473
|
-
userId: integer(input.uid), groupId: integer(input.gid),
|
|
474
|
-
accessTime: input.atime === undefined ? undefined : new Date(input.atime * 1000),
|
|
475
|
-
modificationTime: input.mtime === undefined ? undefined : new Date(input.mtime * 1000) };
|
|
476
565
|
}
|
|
477
566
|
|
|
478
567
|
/**
|
|
@@ -555,9 +644,14 @@ export class Sftp {
|
|
|
555
644
|
* 호스트 신뢰성은 자동으로 검증하지 않습니다. 애플리케이션은 로그인 전에
|
|
556
645
|
* getSessionInfo로 호스트 지문을 검증할 수 있습니다.
|
|
557
646
|
*/
|
|
558
|
-
connect(
|
|
559
|
-
|
|
560
|
-
|
|
647
|
+
connect(
|
|
648
|
+
server: IpAddress,
|
|
649
|
+
port = 22,
|
|
650
|
+
timeout: SftpTimeout = Time.Zero,
|
|
651
|
+
): Promise<SftpResult> {
|
|
652
|
+
if (!(server instanceof IpAddress)) {
|
|
653
|
+
throw new TypeError('Expected IpAddress');
|
|
654
|
+
}
|
|
561
655
|
uint(port, 65535);
|
|
562
656
|
return this.queue.run(async () => {
|
|
563
657
|
this.close();
|
|
@@ -568,39 +662,60 @@ export class Sftp {
|
|
|
568
662
|
this.transport = transport;
|
|
569
663
|
transport.on('data', () => this.progress());
|
|
570
664
|
transport.on('error', error => {
|
|
571
|
-
if (this.transport !== transport)
|
|
572
|
-
|
|
665
|
+
if (this.transport !== transport) {
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
this.failure = error;
|
|
669
|
+
this.events.emit('change');
|
|
573
670
|
});
|
|
574
671
|
client.on('error', error => {
|
|
575
|
-
if (this.client !== client)
|
|
576
|
-
|
|
672
|
+
if (this.client !== client) {
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
this.failure = error;
|
|
676
|
+
this.events.emit('change');
|
|
577
677
|
});
|
|
578
678
|
client.on('close', () => {
|
|
579
|
-
if (this.client !== client)
|
|
679
|
+
if (this.client !== client) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
580
682
|
this.client = undefined;
|
|
581
683
|
this.sftp = undefined;
|
|
582
684
|
this.nextAuth = undefined;
|
|
583
685
|
this.events.emit('change');
|
|
584
686
|
});
|
|
585
687
|
client.on('handshake', algorithms => {
|
|
586
|
-
this.algorithms = algorithms;
|
|
688
|
+
this.algorithms = algorithms;
|
|
689
|
+
this.progress();
|
|
587
690
|
});
|
|
588
691
|
client.on('ready', () => {
|
|
589
|
-
this.authenticated = true;
|
|
692
|
+
this.authenticated = true;
|
|
693
|
+
this.events.emit('change');
|
|
590
694
|
});
|
|
591
695
|
try {
|
|
592
696
|
await this.timed(timeout, async () => {
|
|
593
697
|
transport.connect(port, server.toString());
|
|
594
|
-
client.connect({
|
|
595
|
-
|
|
698
|
+
client.connect({
|
|
699
|
+
sock: transport,
|
|
700
|
+
username: '',
|
|
701
|
+
readyTimeout: 0,
|
|
702
|
+
hostVerifier: (key: Buffer) => {
|
|
703
|
+
this.key = Buffer.from(key);
|
|
704
|
+
return true;
|
|
705
|
+
},
|
|
596
706
|
authHandler: (_methods, _partial, next) => {
|
|
597
|
-
this.nextAuth = next;
|
|
707
|
+
this.nextAuth = next;
|
|
708
|
+
this.authRound++;
|
|
709
|
+
this.events.emit('change');
|
|
598
710
|
},
|
|
599
711
|
});
|
|
600
712
|
await this.state(() => !!this.nextAuth);
|
|
601
713
|
});
|
|
602
714
|
return ok();
|
|
603
|
-
} catch (error) {
|
|
715
|
+
} catch (error) {
|
|
716
|
+
this.close();
|
|
717
|
+
return result(error);
|
|
718
|
+
}
|
|
604
719
|
});
|
|
605
720
|
}
|
|
606
721
|
/**
|
|
@@ -613,8 +728,11 @@ export class Sftp {
|
|
|
613
728
|
* timeout은 인수 검증에만 사용합니다.
|
|
614
729
|
*/
|
|
615
730
|
disconnect(timeout: SftpTimeout = Time.Zero): Promise<SftpResult> {
|
|
616
|
-
millisecondsOf(
|
|
617
|
-
|
|
731
|
+
millisecondsOf(
|
|
732
|
+
timeout instanceof TimeoutWithPredicate
|
|
733
|
+
? timeout.getPeriod()
|
|
734
|
+
: timeout,
|
|
735
|
+
);
|
|
618
736
|
this.close();
|
|
619
737
|
return Promise.resolve(ok());
|
|
620
738
|
}
|
|
@@ -644,7 +762,11 @@ export class Sftp {
|
|
|
644
762
|
* @ja ユーザー名とパスワードで非同期に認証し、SFTP チャネルを開きます。
|
|
645
763
|
* @ko 사용자 이름과 암호로 비동기 인증하고 SFTP 채널을 엽니다.
|
|
646
764
|
*/
|
|
647
|
-
login(
|
|
765
|
+
login(
|
|
766
|
+
name: string,
|
|
767
|
+
password: string,
|
|
768
|
+
timeout?: SftpTimeout,
|
|
769
|
+
): Promise<SftpResult>;
|
|
648
770
|
/**
|
|
649
771
|
* Authenticate asynchronously with matching public/private keys and an
|
|
650
772
|
* optional private-key passphrase; key arguments are contents, not paths.
|
|
@@ -654,45 +776,94 @@ export class Sftp {
|
|
|
654
776
|
* @ko 일치하는 공개 키와 개인 키 및 선택적 개인 키 암호로 비동기 인증합니다.
|
|
655
777
|
* 키 인수는 파일 경로가 아닌 내용입니다.
|
|
656
778
|
*/
|
|
657
|
-
login(
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
779
|
+
login(
|
|
780
|
+
name: string,
|
|
781
|
+
publicKey: string | Uint8Array,
|
|
782
|
+
privateKey: string | Uint8Array,
|
|
783
|
+
passphrase?: string,
|
|
784
|
+
timeout?: SftpTimeout,
|
|
785
|
+
): Promise<SftpResult>;
|
|
786
|
+
login(
|
|
787
|
+
name: string,
|
|
788
|
+
passwordOrPublic: string | Uint8Array,
|
|
789
|
+
keyOrTimeout?: string | Uint8Array | SftpTimeout,
|
|
790
|
+
passphrase = '',
|
|
791
|
+
timeout: SftpTimeout = Time.Zero,
|
|
792
|
+
): Promise<SftpResult> {
|
|
793
|
+
const publicKey =
|
|
794
|
+
typeof keyOrTimeout === 'string' ||
|
|
795
|
+
keyOrTimeout instanceof Uint8Array;
|
|
796
|
+
const duration = publicKey
|
|
797
|
+
? timeout
|
|
798
|
+
: ((keyOrTimeout as SftpTimeout | undefined) ?? Time.Zero);
|
|
664
799
|
let method: AnyAuthMethod;
|
|
665
800
|
if (publicKey) {
|
|
666
|
-
const key = utils.parseKey(
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
801
|
+
const key = utils.parseKey(
|
|
802
|
+
typeof keyOrTimeout === 'string'
|
|
803
|
+
? keyOrTimeout
|
|
804
|
+
: bytes(keyOrTimeout as Uint8Array),
|
|
805
|
+
passphrase,
|
|
806
|
+
);
|
|
807
|
+
if (key instanceof Error) {
|
|
808
|
+
return Promise.resolve(result(key));
|
|
809
|
+
}
|
|
810
|
+
const advertised = utils.parseKey(
|
|
811
|
+
typeof passwordOrPublic === 'string'
|
|
812
|
+
? passwordOrPublic
|
|
813
|
+
: bytes(passwordOrPublic),
|
|
814
|
+
);
|
|
815
|
+
if (
|
|
816
|
+
advertised instanceof Error ||
|
|
817
|
+
!key.getPublicSSH().equals(advertised.getPublicSSH())
|
|
818
|
+
) {
|
|
819
|
+
return Promise.resolve(
|
|
820
|
+
new SftpResult(
|
|
821
|
+
Value.PublicKeyUnverified,
|
|
822
|
+
'Public and private keys do not match',
|
|
823
|
+
),
|
|
824
|
+
);
|
|
674
825
|
}
|
|
675
826
|
method = { type: 'publickey', username: name, key };
|
|
676
827
|
} else {
|
|
677
|
-
if (typeof passwordOrPublic !== 'string')
|
|
678
|
-
|
|
828
|
+
if (typeof passwordOrPublic !== 'string') {
|
|
829
|
+
throw new TypeError('Expected password');
|
|
830
|
+
}
|
|
831
|
+
method = {
|
|
832
|
+
type: 'password',
|
|
833
|
+
username: name,
|
|
834
|
+
password: passwordOrPublic,
|
|
835
|
+
};
|
|
679
836
|
}
|
|
680
837
|
return this.queue.run(async () => {
|
|
681
838
|
try {
|
|
682
839
|
await this.timed(duration, async () => {
|
|
683
|
-
if (!this.nextAuth)
|
|
840
|
+
if (!this.nextAuth) {
|
|
841
|
+
throw new OperationError(
|
|
842
|
+
Value.Disconnected,
|
|
843
|
+
'Connect before login',
|
|
844
|
+
);
|
|
845
|
+
}
|
|
684
846
|
const next = this.nextAuth;
|
|
685
847
|
this.nextAuth = undefined;
|
|
686
848
|
const round = this.authRound;
|
|
687
849
|
next(method);
|
|
688
|
-
await this.state(
|
|
850
|
+
await this.state(
|
|
851
|
+
() => this.authenticated || this.authRound !== round,
|
|
852
|
+
);
|
|
689
853
|
if (!this.authenticated) {
|
|
690
|
-
throw new OperationError(
|
|
854
|
+
throw new OperationError(
|
|
855
|
+
Value.AuthenticationFailed,
|
|
856
|
+
'Authentication rejected',
|
|
857
|
+
);
|
|
691
858
|
}
|
|
692
|
-
this.sftp = await this.rpc<SFTPWrapper>(done =>
|
|
859
|
+
this.sftp = await this.rpc<SFTPWrapper>(done =>
|
|
860
|
+
this.client!.sftp(done),
|
|
861
|
+
);
|
|
693
862
|
});
|
|
694
863
|
return ok();
|
|
695
|
-
} catch (error) {
|
|
864
|
+
} catch (error) {
|
|
865
|
+
return result(error);
|
|
866
|
+
}
|
|
696
867
|
});
|
|
697
868
|
}
|
|
698
869
|
/**
|
|
@@ -705,26 +876,38 @@ export class Sftp {
|
|
|
705
876
|
* 핸드셰이크 정보가 준비되지 않았으면 undefined입니다.
|
|
706
877
|
*/
|
|
707
878
|
getSessionInfo(): SftpSessionInfo | undefined {
|
|
708
|
-
if (!this.key || !this.algorithms)
|
|
879
|
+
if (!this.key || !this.algorithms) {
|
|
880
|
+
return undefined;
|
|
881
|
+
}
|
|
709
882
|
const { key, algorithms: a } = this;
|
|
710
|
-
const keyName =
|
|
711
|
-
|
|
883
|
+
const keyName =
|
|
884
|
+
key.length >= 4
|
|
885
|
+
? key.toString('ascii', 4, 4 + key.readUInt32BE())
|
|
886
|
+
: '';
|
|
712
887
|
const names: Record<string, SftpHostKeyType> = {
|
|
713
|
-
'ssh-rsa': SftpHostKeyType.Rsa,
|
|
888
|
+
'ssh-rsa': SftpHostKeyType.Rsa,
|
|
889
|
+
'ssh-dss': SftpHostKeyType.Dsa,
|
|
714
890
|
'ecdsa-sha2-nistp256': SftpHostKeyType.Ecdsa256,
|
|
715
891
|
'ecdsa-sha2-nistp384': SftpHostKeyType.Ecdsa384,
|
|
716
892
|
'ecdsa-sha2-nistp521': SftpHostKeyType.Ecdsa521,
|
|
717
893
|
'ssh-ed25519': SftpHostKeyType.Ed25519,
|
|
718
894
|
};
|
|
719
|
-
return {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
895
|
+
return {
|
|
896
|
+
hostKey: {
|
|
897
|
+
type: names[keyName] ?? SftpHostKeyType.Unknown,
|
|
898
|
+
data: Uint8Array.from(key),
|
|
899
|
+
sha1: createHash('sha1').update(key).digest(),
|
|
900
|
+
sha256: createHash('sha256').update(key).digest(),
|
|
901
|
+
},
|
|
902
|
+
keyExchangeAlgorithm: a.kex,
|
|
903
|
+
hostKeyAlgorithm: a.serverHostKey,
|
|
904
|
+
clientToServerEncryptionAlgorithm: a.cs.cipher,
|
|
905
|
+
serverToClientEncryptionAlgorithm: a.sc.cipher,
|
|
906
|
+
clientToServerMacAlgorithm: a.cs.mac,
|
|
907
|
+
serverToClientMacAlgorithm: a.sc.mac,
|
|
908
|
+
clientToServerCompressionAlgorithm: a.cs.compress,
|
|
909
|
+
serverToClientCompressionAlgorithm: a.sc.compress,
|
|
910
|
+
};
|
|
728
911
|
}
|
|
729
912
|
/**
|
|
730
913
|
* Ask the server to resolve a canonical absolute path.
|
|
@@ -732,13 +915,19 @@ export class Sftp {
|
|
|
732
915
|
* @ja 正規化された絶対パスの解決をサーバーに要求します。
|
|
733
916
|
* @ko 서버에 정규화된 절대 경로 확인을 요청합니다.
|
|
734
917
|
*/
|
|
735
|
-
resolvePath(
|
|
918
|
+
resolvePath(
|
|
919
|
+
path: string,
|
|
920
|
+
timeout: SftpTimeout = Time.Zero,
|
|
921
|
+
): Promise<SftpPathResult> {
|
|
736
922
|
return this.queue.run(async () => {
|
|
737
923
|
try {
|
|
738
924
|
const resolved = await this.timed(timeout, () =>
|
|
739
|
-
this.rpc<string>(done => this.channel().realpath(path, done))
|
|
925
|
+
this.rpc<string>(done => this.channel().realpath(path, done)),
|
|
926
|
+
);
|
|
740
927
|
return new SftpPathResult(ok(), resolved);
|
|
741
|
-
} catch (error) {
|
|
928
|
+
} catch (error) {
|
|
929
|
+
return new SftpPathResult(result(error));
|
|
930
|
+
}
|
|
742
931
|
});
|
|
743
932
|
}
|
|
744
933
|
/**
|
|
@@ -747,7 +936,9 @@ export class Sftp {
|
|
|
747
936
|
* @ja 現在のセッションに対するサーバーの既定作業ディレクトリを照会します。
|
|
748
937
|
* @ko 현재 세션에 대한 서버의 기본 작업 디렉터리를 조회합니다.
|
|
749
938
|
*/
|
|
750
|
-
getWorkingDirectory(
|
|
939
|
+
getWorkingDirectory(
|
|
940
|
+
timeout: SftpTimeout = Time.Zero,
|
|
941
|
+
): Promise<SftpPathResult> {
|
|
751
942
|
return this.resolvePath('.', timeout);
|
|
752
943
|
}
|
|
753
944
|
/**
|
|
@@ -759,14 +950,24 @@ export class Sftp {
|
|
|
759
950
|
* @ko 원격 속성을 조회합니다. followLinks는 기본적으로 심볼릭 링크를 따르며,
|
|
760
951
|
* false이면 링크 자체를 조회합니다.
|
|
761
952
|
*/
|
|
762
|
-
getAttributes(
|
|
763
|
-
|
|
953
|
+
getAttributes(
|
|
954
|
+
path: string,
|
|
955
|
+
followLinks = true,
|
|
956
|
+
timeout: SftpTimeout = Time.Zero,
|
|
957
|
+
): Promise<SftpAttributesResult> {
|
|
764
958
|
return this.queue.run(async () => {
|
|
765
959
|
try {
|
|
766
|
-
const data = await this.timed(timeout, () =>
|
|
767
|
-
|
|
960
|
+
const data = await this.timed(timeout, () =>
|
|
961
|
+
this.rpc<SshAttributes>(done =>
|
|
962
|
+
(followLinks
|
|
963
|
+
? this.channel().stat(path, done)
|
|
964
|
+
: this.channel().lstat(path, done)),
|
|
965
|
+
),
|
|
966
|
+
);
|
|
768
967
|
return new SftpAttributesResult(ok(), attributes(path, data));
|
|
769
|
-
} catch (error) {
|
|
968
|
+
} catch (error) {
|
|
969
|
+
return new SftpAttributesResult(result(error));
|
|
970
|
+
}
|
|
770
971
|
});
|
|
771
972
|
}
|
|
772
973
|
/**
|
|
@@ -775,16 +976,24 @@ export class Sftp {
|
|
|
775
976
|
* @ja ディレクトリエントリーとその属性を非同期に読み取ります。
|
|
776
977
|
* @ko 디렉터리 항목과 속성을 비동기로 읽습니다.
|
|
777
978
|
*/
|
|
778
|
-
getDirectoryListing(
|
|
779
|
-
|
|
979
|
+
getDirectoryListing(
|
|
980
|
+
path: string,
|
|
981
|
+
timeout: SftpTimeout = Time.Zero,
|
|
982
|
+
): Promise<SftpListingResult> {
|
|
780
983
|
return this.queue.run(async () => {
|
|
781
984
|
try {
|
|
782
985
|
const listing = await this.timed(timeout, () =>
|
|
783
986
|
this.rpc<{ filename: string; attrs: SshAttributes }[]>(done =>
|
|
784
|
-
this.channel().readdir(path, done)
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
987
|
+
this.channel().readdir(path, done),
|
|
988
|
+
),
|
|
989
|
+
);
|
|
990
|
+
return new SftpListingResult(
|
|
991
|
+
ok(),
|
|
992
|
+
listing.map(entry => attributes(entry.filename, entry.attrs)),
|
|
993
|
+
);
|
|
994
|
+
} catch (error) {
|
|
995
|
+
return new SftpListingResult(result(error));
|
|
996
|
+
}
|
|
788
997
|
});
|
|
789
998
|
}
|
|
790
999
|
/**
|
|
@@ -793,11 +1002,17 @@ export class Sftp {
|
|
|
793
1002
|
* @ja リモートディレクトリを作成します。既定の Unix 権限は 0o755 です。
|
|
794
1003
|
* @ko 원격 디렉터리를 생성하며 기본 Unix 권한은 0o755입니다.
|
|
795
1004
|
*/
|
|
796
|
-
createDirectory(
|
|
797
|
-
|
|
1005
|
+
createDirectory(
|
|
1006
|
+
path: string,
|
|
1007
|
+
permissions = 0o755,
|
|
1008
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1009
|
+
): Promise<SftpResult> {
|
|
798
1010
|
uint(permissions, 0o7777);
|
|
799
|
-
return this.run(timeout, () =>
|
|
800
|
-
this.
|
|
1011
|
+
return this.run(timeout, () =>
|
|
1012
|
+
this.rpc<void>(done =>
|
|
1013
|
+
this.channel().mkdir(path, { mode: permissions }, done),
|
|
1014
|
+
),
|
|
1015
|
+
);
|
|
801
1016
|
}
|
|
802
1017
|
/**
|
|
803
1018
|
* Delete an empty remote directory.
|
|
@@ -805,8 +1020,13 @@ export class Sftp {
|
|
|
805
1020
|
* @ja 空のリモートディレクトリを削除します。
|
|
806
1021
|
* @ko 비어 있는 원격 디렉터리를 삭제합니다.
|
|
807
1022
|
*/
|
|
808
|
-
deleteDirectory(
|
|
809
|
-
|
|
1023
|
+
deleteDirectory(
|
|
1024
|
+
path: string,
|
|
1025
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1026
|
+
): Promise<SftpResult> {
|
|
1027
|
+
return this.run(timeout, () =>
|
|
1028
|
+
this.rpc<void>(done => this.channel().rmdir(path, done)),
|
|
1029
|
+
);
|
|
810
1030
|
}
|
|
811
1031
|
/**
|
|
812
1032
|
* Rename a remote path; overwrite true requires server support for
|
|
@@ -817,11 +1037,19 @@ export class Sftp {
|
|
|
817
1037
|
* @ko 원격 경로 이름을 변경합니다. overwrite가 true이면 서버가
|
|
818
1038
|
* OpenSSH 덮어쓰기 이름 변경 확장을 지원해야 합니다.
|
|
819
1039
|
*/
|
|
820
|
-
rename(
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
1040
|
+
rename(
|
|
1041
|
+
oldPath: string,
|
|
1042
|
+
newPath: string,
|
|
1043
|
+
overwrite = false,
|
|
1044
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1045
|
+
): Promise<SftpResult> {
|
|
1046
|
+
return this.run(timeout, () =>
|
|
1047
|
+
this.rpc<void>(done =>
|
|
1048
|
+
(overwrite
|
|
1049
|
+
? this.channel().ext_openssh_rename(oldPath, newPath, done)
|
|
1050
|
+
: this.channel().rename(oldPath, newPath, done)),
|
|
1051
|
+
),
|
|
1052
|
+
);
|
|
825
1053
|
}
|
|
826
1054
|
/**
|
|
827
1055
|
* Delete a remote file or symbolic link.
|
|
@@ -829,8 +1057,13 @@ export class Sftp {
|
|
|
829
1057
|
* @ja リモートファイルまたはシンボリックリンクを削除します。
|
|
830
1058
|
* @ko 원격 파일 또는 심볼릭 링크를 삭제합니다.
|
|
831
1059
|
*/
|
|
832
|
-
deleteFile(
|
|
833
|
-
|
|
1060
|
+
deleteFile(
|
|
1061
|
+
path: string,
|
|
1062
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1063
|
+
): Promise<SftpResult> {
|
|
1064
|
+
return this.run(timeout, () =>
|
|
1065
|
+
this.rpc<void>(done => this.channel().unlink(path, done)),
|
|
1066
|
+
);
|
|
834
1067
|
}
|
|
835
1068
|
/**
|
|
836
1069
|
* Download chunks from byte offset offset, awaiting callback sequentially;
|
|
@@ -841,27 +1074,48 @@ export class Sftp {
|
|
|
841
1074
|
* @ko 바이트 오프셋 offset부터 청크 단위로 다운로드하며 callback을
|
|
842
1075
|
* 순서대로 기다립니다. 최대 오프셋은 2^53-1입니다.
|
|
843
1076
|
*/
|
|
844
|
-
download(
|
|
845
|
-
|
|
1077
|
+
download(
|
|
1078
|
+
path: string,
|
|
1079
|
+
callback: SftpDownloadCallback,
|
|
1080
|
+
offset = 0n,
|
|
1081
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1082
|
+
): Promise<SftpResult> {
|
|
846
1083
|
let at = position(offset);
|
|
847
1084
|
return this.run(timeout, async () => {
|
|
848
1085
|
const channel = this.channel();
|
|
849
|
-
const handle = await this.rpc<Buffer>(done =>
|
|
1086
|
+
const handle = await this.rpc<Buffer>(done =>
|
|
1087
|
+
channel.open(path, 'r', done),
|
|
1088
|
+
);
|
|
850
1089
|
try {
|
|
851
1090
|
for (;;) {
|
|
852
|
-
if (this.sftp !== channel)
|
|
1091
|
+
if (this.sftp !== channel) {
|
|
1092
|
+
throw new OperationError(Value.Disconnected, 'Transfer ended');
|
|
1093
|
+
}
|
|
853
1094
|
const data = Buffer.allocUnsafe(65536);
|
|
854
1095
|
const start = at;
|
|
855
1096
|
const count = await this.rpc<number>(done =>
|
|
856
|
-
channel.read(
|
|
857
|
-
|
|
1097
|
+
channel.read(
|
|
1098
|
+
handle,
|
|
1099
|
+
data,
|
|
1100
|
+
0,
|
|
1101
|
+
data.length,
|
|
1102
|
+
start,
|
|
1103
|
+
(error, count) => done(error, count),
|
|
1104
|
+
),
|
|
1105
|
+
);
|
|
858
1106
|
at = position(BigInt(at) + BigInt(count));
|
|
859
1107
|
const more = await callback(data.subarray(0, count));
|
|
860
|
-
if (typeof more !== 'boolean')
|
|
861
|
-
|
|
1108
|
+
if (typeof more !== 'boolean') {
|
|
1109
|
+
throw new TypeError('Expected callback boolean');
|
|
1110
|
+
}
|
|
1111
|
+
if (!more || !count) {
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
862
1114
|
}
|
|
863
1115
|
} finally {
|
|
864
|
-
if (this.sftp === channel)
|
|
1116
|
+
if (this.sftp === channel) {
|
|
1117
|
+
await this.rpc<void>(done => channel.close(handle, done));
|
|
1118
|
+
}
|
|
865
1119
|
}
|
|
866
1120
|
});
|
|
867
1121
|
}
|
|
@@ -900,9 +1154,15 @@ export class Sftp {
|
|
|
900
1154
|
* @ja timeout - 進捗がない場合のタイムアウト設定。既定では制限なしです。
|
|
901
1155
|
* @ko timeout - 진행이 없을 때의 제한 시간 설정. 기본적으로 제한이 없습니다.
|
|
902
1156
|
*/
|
|
903
|
-
upload(
|
|
904
|
-
|
|
905
|
-
|
|
1157
|
+
upload(
|
|
1158
|
+
path: string,
|
|
1159
|
+
callback: SftpUploadCallback,
|
|
1160
|
+
permissions = 0o644,
|
|
1161
|
+
truncate = true,
|
|
1162
|
+
append = false,
|
|
1163
|
+
offset = 0n,
|
|
1164
|
+
timeout: SftpTimeout = Time.Zero,
|
|
1165
|
+
): Promise<SftpResult> {
|
|
906
1166
|
let at = position(offset);
|
|
907
1167
|
uint(permissions, 0o7777);
|
|
908
1168
|
return this.run(timeout, async () => {
|
|
@@ -910,91 +1170,157 @@ export class Sftp {
|
|
|
910
1170
|
// SSH_FXF_WRITE | CREAT, with TRUNC and APPEND as independent flags.
|
|
911
1171
|
const flags = 2 + 8 + (truncate ? 16 : 0) + (append ? 4 : 0);
|
|
912
1172
|
const handle = await this.rpc<Buffer>(done =>
|
|
913
|
-
channel.open(path, flags, { mode: permissions }, done)
|
|
1173
|
+
channel.open(path, flags, { mode: permissions }, done),
|
|
1174
|
+
);
|
|
914
1175
|
try {
|
|
915
1176
|
for (;;) {
|
|
916
1177
|
const data = Buffer.allocUnsafe(65536);
|
|
917
1178
|
const chunk = { data, size: data.length };
|
|
918
1179
|
const more = await callback(chunk);
|
|
919
|
-
if (this.sftp !== channel)
|
|
1180
|
+
if (this.sftp !== channel) {
|
|
1181
|
+
throw new OperationError(Value.Disconnected, 'Transfer ended');
|
|
1182
|
+
}
|
|
920
1183
|
if (typeof more !== 'boolean' || chunk.data !== data) {
|
|
921
|
-
throw new TypeError(
|
|
1184
|
+
throw new TypeError(
|
|
1185
|
+
'Fill the provided upload buffer and return boolean',
|
|
1186
|
+
);
|
|
922
1187
|
}
|
|
923
1188
|
uint(chunk.size, data.length);
|
|
924
1189
|
if (chunk.size) {
|
|
925
1190
|
const start = at;
|
|
926
1191
|
await this.rpc<void>(done =>
|
|
927
|
-
channel.write(handle, data, 0, chunk.size, start, done)
|
|
1192
|
+
channel.write(handle, data, 0, chunk.size, start, done),
|
|
1193
|
+
);
|
|
928
1194
|
at = position(BigInt(at) + BigInt(chunk.size));
|
|
929
1195
|
}
|
|
930
|
-
if (!more || !chunk.size)
|
|
1196
|
+
if (!more || !chunk.size) {
|
|
1197
|
+
break;
|
|
1198
|
+
}
|
|
931
1199
|
}
|
|
932
1200
|
} finally {
|
|
933
|
-
if (this.sftp === channel)
|
|
1201
|
+
if (this.sftp === channel) {
|
|
1202
|
+
await this.rpc<void>(done => channel.close(handle, done));
|
|
1203
|
+
}
|
|
934
1204
|
}
|
|
935
1205
|
});
|
|
936
1206
|
}
|
|
937
1207
|
/** 将队列操作包装为标准 SftpResult 结果。 */
|
|
938
|
-
private run(
|
|
1208
|
+
private run(
|
|
1209
|
+
timeout: SftpTimeout,
|
|
1210
|
+
action: () => Promise<void>,
|
|
1211
|
+
): Promise<SftpResult> {
|
|
939
1212
|
return this.queue.run(async () => {
|
|
940
|
-
try {
|
|
1213
|
+
try {
|
|
1214
|
+
await this.timed(timeout, action);
|
|
1215
|
+
return ok();
|
|
1216
|
+
} catch (error) {
|
|
1217
|
+
return result(error);
|
|
1218
|
+
}
|
|
941
1219
|
});
|
|
942
1220
|
}
|
|
943
1221
|
/** 返回当前 SFTP 通道,未认证时抛出错误。 */
|
|
944
1222
|
private channel(): SFTPWrapper {
|
|
945
|
-
if (!this.sftp)
|
|
1223
|
+
if (!this.sftp) {
|
|
1224
|
+
throw new OperationError(
|
|
1225
|
+
Value.Disconnected,
|
|
1226
|
+
'SFTP is not authenticated',
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
946
1229
|
return this.sftp;
|
|
947
1230
|
}
|
|
948
1231
|
/** 等待 check 为 true、故障出现或连接关闭。 */
|
|
949
1232
|
private async state(check: () => boolean): Promise<void> {
|
|
950
|
-
await waitFor(
|
|
951
|
-
|
|
952
|
-
|
|
1233
|
+
await waitFor(
|
|
1234
|
+
this.events,
|
|
1235
|
+
() => check() || !!this.failure || !this.client,
|
|
1236
|
+
);
|
|
1237
|
+
if (this.failure) {
|
|
1238
|
+
throw this.failure;
|
|
1239
|
+
}
|
|
1240
|
+
if (!this.client) {
|
|
1241
|
+
throw new OperationError(Value.Disconnected, 'SSH connection closed');
|
|
1242
|
+
}
|
|
953
1243
|
}
|
|
954
1244
|
/** 包装 ssh2 回调为 Promise,连接关闭时自动拒绝。 */
|
|
955
|
-
private rpc<T>(
|
|
956
|
-
|
|
1245
|
+
private rpc<T>(
|
|
1246
|
+
call: (done: (error?: Error | null, value?: T) => void) => void,
|
|
1247
|
+
): Promise<T> {
|
|
957
1248
|
const client = this.client;
|
|
958
|
-
if (!client)
|
|
1249
|
+
if (!client) {
|
|
1250
|
+
return Promise.reject(
|
|
1251
|
+
new OperationError(Value.Disconnected, 'SSH connection closed'),
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
959
1254
|
return new Promise((resolve, reject) => {
|
|
960
1255
|
const done = (error?: Error | null, value?: T) => {
|
|
961
1256
|
client.off('close', closed);
|
|
962
1257
|
this.progress();
|
|
963
|
-
if (error)
|
|
1258
|
+
if (error) {
|
|
1259
|
+
reject(error);
|
|
1260
|
+
} else {
|
|
1261
|
+
resolve(value as T);
|
|
1262
|
+
}
|
|
964
1263
|
};
|
|
965
1264
|
function closed() {
|
|
966
|
-
done(
|
|
1265
|
+
done(
|
|
1266
|
+
new OperationError(Value.Disconnected, 'SSH connection closed'),
|
|
1267
|
+
);
|
|
967
1268
|
}
|
|
968
1269
|
client.once('close', closed);
|
|
969
|
-
try {
|
|
1270
|
+
try {
|
|
1271
|
+
call(done);
|
|
1272
|
+
} catch (error) {
|
|
1273
|
+
done(error as Error);
|
|
1274
|
+
}
|
|
970
1275
|
});
|
|
971
1276
|
}
|
|
972
1277
|
/** 运行操作并应用超时;每次收到数据或 RPC 完成时重置计时。 */
|
|
973
|
-
private async timed<T>(
|
|
974
|
-
|
|
975
|
-
|
|
1278
|
+
private async timed<T>(
|
|
1279
|
+
timeout: SftpTimeout,
|
|
1280
|
+
action: () => Promise<T>,
|
|
1281
|
+
): Promise<T> {
|
|
1282
|
+
const config =
|
|
1283
|
+
timeout instanceof TimeoutWithPredicate
|
|
1284
|
+
? timeout
|
|
1285
|
+
: new TimeoutWithPredicate(timeout);
|
|
976
1286
|
const period = millisecondsOf(config.getPeriod());
|
|
977
1287
|
let timer: NodeJS.Timeout | undefined;
|
|
978
1288
|
let stopped = false;
|
|
979
1289
|
let timedOut = false;
|
|
980
1290
|
let rejectTimeout!: (error: unknown) => void;
|
|
981
|
-
const deadline = new Promise<never>((_resolve, reject) => {
|
|
1291
|
+
const deadline = new Promise<never>((_resolve, reject) => {
|
|
1292
|
+
rejectTimeout = reject;
|
|
1293
|
+
});
|
|
982
1294
|
const client = this.client;
|
|
983
1295
|
const closed = () => {
|
|
984
1296
|
if (client !== this.client && !timedOut) {
|
|
985
|
-
rejectTimeout(
|
|
1297
|
+
rejectTimeout(
|
|
1298
|
+
new OperationError(Value.Disconnected, 'SSH connection closed'),
|
|
1299
|
+
);
|
|
986
1300
|
}
|
|
987
1301
|
};
|
|
988
1302
|
this.events.on('change', closed);
|
|
989
1303
|
const touch = () => {
|
|
990
|
-
if (stopped || !period)
|
|
991
|
-
|
|
1304
|
+
if (stopped || !period) {
|
|
1305
|
+
return;
|
|
1306
|
+
}
|
|
1307
|
+
if (timer) {
|
|
1308
|
+
clearTimeout(timer);
|
|
1309
|
+
}
|
|
992
1310
|
timer = setTimeout(() => {
|
|
993
1311
|
try {
|
|
994
|
-
if (config.getPredicate()()) {
|
|
1312
|
+
if (config.getPredicate()()) {
|
|
1313
|
+
touch();
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
timedOut = true;
|
|
1317
|
+
rejectTimeout(
|
|
1318
|
+
new OperationError(Value.Timeout, 'SFTP operation timed out'),
|
|
1319
|
+
);
|
|
1320
|
+
} catch (error) {
|
|
995
1321
|
timedOut = true;
|
|
996
|
-
rejectTimeout(
|
|
997
|
-
}
|
|
1322
|
+
rejectTimeout(error);
|
|
1323
|
+
}
|
|
998
1324
|
this.close();
|
|
999
1325
|
}, period);
|
|
1000
1326
|
};
|
|
@@ -1003,11 +1329,15 @@ export class Sftp {
|
|
|
1003
1329
|
try {
|
|
1004
1330
|
const operation = action();
|
|
1005
1331
|
const value = await Promise.race([ operation, deadline ]);
|
|
1006
|
-
if (timedOut)
|
|
1332
|
+
if (timedOut) {
|
|
1333
|
+
throw new OperationError(Value.Timeout, 'SFTP operation timed out');
|
|
1334
|
+
}
|
|
1007
1335
|
return value;
|
|
1008
1336
|
} finally {
|
|
1009
1337
|
stopped = true;
|
|
1010
|
-
if (timer)
|
|
1338
|
+
if (timer) {
|
|
1339
|
+
clearTimeout(timer);
|
|
1340
|
+
}
|
|
1011
1341
|
this.events.off('change', closed);
|
|
1012
1342
|
this.progress = () => {};
|
|
1013
1343
|
}
|