appmesh 3.1.19 → 3.1.21
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.md +31 -1
- package/dist/appmesh.esm.js +1 -1
- package/package.json +2 -2
- package/src/appmesh.js +210 -19
- package/src/appmesh_tcp.js +226 -120
package/src/appmesh_tcp.js
CHANGED
|
@@ -8,7 +8,7 @@ import { once } from 'events'
|
|
|
8
8
|
import { v1 as uuidv1 } from 'uuid'
|
|
9
9
|
import msgpack from 'msgpack-lite'
|
|
10
10
|
import AppMeshClient, {
|
|
11
|
-
AppMeshError, AppRemovedError, TransportDisconnectedError, DEFAULT_CA_FILE, CONSTANTS,
|
|
11
|
+
AppMeshError, AppMeshRequestError, AppRemovedError, TransportDisconnectedError, DEFAULT_CA_FILE, CONSTANTS,
|
|
12
12
|
_resolveUid, _resolveGid, _resolveUserName, _resolveGroupName,
|
|
13
13
|
} from './appmesh.js'
|
|
14
14
|
import { canonicalQuery } from './request_signature.js'
|
|
@@ -549,17 +549,22 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
549
549
|
* (null=default CA, object=custom ca/cert/key, false=insecure).
|
|
550
550
|
* @param {Array<string, number>|{host: string, port: number}} [tcpAddress=['127.0.0.1', 6062]] - TCP server address
|
|
551
551
|
* @param {number} [restPort=6063] - REST port of the same daemon for the streaming side channel
|
|
552
|
-
* (followOutput / runTaskStream); also honored as a `restPort` key on the
|
|
552
|
+
* (followOutput / runTaskStream / streamRun) for local streams; also honored as a `restPort` key on the
|
|
553
|
+
* `{host, port}` address object
|
|
554
|
+
* @param {number} [agentPort=6060] - Agent REST port on the same host for the streaming side channel when
|
|
555
|
+
* `forwardingHost` is set (the engine listener does not stream through node forwarding, the agent relays it);
|
|
556
|
+
* also honored as an `agentPort` key on the `{host, port}` address object
|
|
553
557
|
*/
|
|
554
|
-
constructor (sslConfig = null, tcpAddress = ['127.0.0.1', 6062], restPort = null) {
|
|
558
|
+
constructor (sslConfig = null, tcpAddress = ['127.0.0.1', 6062], restPort = null, agentPort = null) {
|
|
555
559
|
// Accept the address-first object form: new AppMeshClientTCP({host, port}, sslConfig)
|
|
556
560
|
if (_isAddressObject(sslConfig)) {
|
|
557
561
|
const addr = sslConfig
|
|
558
562
|
sslConfig = (Array.isArray(tcpAddress) || _isAddressObject(tcpAddress)) ? null : tcpAddress
|
|
559
563
|
tcpAddress = addr
|
|
560
564
|
}
|
|
561
|
-
if (_isAddressObject(tcpAddress)
|
|
562
|
-
restPort = tcpAddress.restPort
|
|
565
|
+
if (_isAddressObject(tcpAddress)) {
|
|
566
|
+
if (restPort == null) restPort = tcpAddress.restPort
|
|
567
|
+
if (agentPort == null) agentPort = tcpAddress.agentPort
|
|
563
568
|
}
|
|
564
569
|
tcpAddress = _normalizeTcpAddress(tcpAddress)
|
|
565
570
|
|
|
@@ -576,7 +581,10 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
576
581
|
// The SSE side channel (_sseRequest) dials the daemon's REST listener on the same host.
|
|
577
582
|
this._restHost = tcpAddress[0]
|
|
578
583
|
this._restPort = restPort ?? 6063
|
|
584
|
+
// A forwarded stream dials the agent REST listener instead (see _ensureRestChannel).
|
|
585
|
+
this._agentPort = agentPort ?? 6060
|
|
579
586
|
this._restChannelClient = null
|
|
587
|
+
this._agentChannelClient = null
|
|
580
588
|
}
|
|
581
589
|
|
|
582
590
|
/**
|
|
@@ -591,11 +599,14 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
591
599
|
this.tcpTransport.close()
|
|
592
600
|
this.tcpTransport = null
|
|
593
601
|
}
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
602
|
+
for (const channel of [this._restChannelClient, this._agentChannelClient]) {
|
|
603
|
+
if (channel) {
|
|
604
|
+
// Release the streaming side channel's keep-alive sockets.
|
|
605
|
+
channel._client.defaults.httpsAgent?.destroy?.()
|
|
606
|
+
}
|
|
598
607
|
}
|
|
608
|
+
this._restChannelClient = null
|
|
609
|
+
this._agentChannelClient = null
|
|
599
610
|
}
|
|
600
611
|
|
|
601
612
|
/**
|
|
@@ -604,15 +615,17 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
604
615
|
* @override
|
|
605
616
|
*/
|
|
606
617
|
async _request (method, path, body = null, options = {}) {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
618
|
+
return this._requestOnTransport(null, method, path, body, options)
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// Use the primary transport when ephemeralTransport is null, else bypass the demuxer.
|
|
622
|
+
async _requestOnTransport (ephemeralTransport, method, path, body = null, options = {}) {
|
|
623
|
+
const transport = ephemeralTransport || this.tcpTransport
|
|
624
|
+
if (!ephemeralTransport && this._demuxerStarting) {
|
|
612
625
|
await this._demuxerEnablePromise
|
|
613
626
|
}
|
|
614
|
-
if (!
|
|
615
|
-
await
|
|
627
|
+
if (!transport.connected()) {
|
|
628
|
+
await transport.connect()
|
|
616
629
|
}
|
|
617
630
|
|
|
618
631
|
// Build request message
|
|
@@ -632,9 +645,7 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
632
645
|
}
|
|
633
646
|
|
|
634
647
|
if (this.forwardingHost) {
|
|
635
|
-
request.headers[HTTP_HEADER_KEY_X_TARGET_HOST] = this.
|
|
636
|
-
? this.forwardingHost
|
|
637
|
-
: `${this.forwardingHost}:${this.tcpTransport.address[1]}`
|
|
648
|
+
request.headers[HTTP_HEADER_KEY_X_TARGET_HOST] = this._forwardTargetHeader()
|
|
638
649
|
}
|
|
639
650
|
|
|
640
651
|
if (options.headers) {
|
|
@@ -675,13 +686,21 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
675
686
|
const serialized = request.serialize()
|
|
676
687
|
|
|
677
688
|
let response
|
|
678
|
-
if (
|
|
689
|
+
if (ephemeralTransport) {
|
|
690
|
+
transport.sendMessage(serialized)
|
|
691
|
+
const respData = await transport.receiveMessage()
|
|
692
|
+
if (!respData || respData.length === 0) {
|
|
693
|
+
transport.close()
|
|
694
|
+
throw new Error('Socket connection broken')
|
|
695
|
+
}
|
|
696
|
+
response = new ResponseMessage().deserialize(respData)
|
|
697
|
+
} else if (this._demuxer && this._demuxer._running) {
|
|
679
698
|
// Live demuxer: route response by UUID. A stopped demuxer can never resolve
|
|
680
699
|
// waiters, so fall back to the direct read below. Contract S7: register the
|
|
681
700
|
// waiter BEFORE writing request bytes so a fast response can't be dropped.
|
|
682
701
|
const respPromise = this._demuxer.registerRequest(request.uuid)
|
|
683
702
|
try {
|
|
684
|
-
|
|
703
|
+
transport.sendMessage(serialized)
|
|
685
704
|
} catch (e) {
|
|
686
705
|
respPromise.catch(() => {}) // never dispatched; silence a later stop() rejection
|
|
687
706
|
this._demuxer.unregisterRequest(request.uuid)
|
|
@@ -700,10 +719,10 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
700
719
|
// subscribe waits for this response before starting the demuxer.
|
|
701
720
|
this._directReads++
|
|
702
721
|
try {
|
|
703
|
-
|
|
704
|
-
const respData = await
|
|
722
|
+
transport.sendMessage(serialized)
|
|
723
|
+
const respData = await transport.receiveMessage()
|
|
705
724
|
if (!respData || respData.length === 0) {
|
|
706
|
-
|
|
725
|
+
transport.close()
|
|
707
726
|
throw new Error('Socket connection broken')
|
|
708
727
|
}
|
|
709
728
|
response = new ResponseMessage().deserialize(respData)
|
|
@@ -713,10 +732,13 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
713
732
|
}
|
|
714
733
|
|
|
715
734
|
// Convert to standard response format matching axios structure
|
|
735
|
+
const ctKey = Object.keys(response.headers || {}).find(k => k.toLowerCase() === 'content-type')
|
|
716
736
|
const result = {
|
|
717
737
|
status: response.httpStatus,
|
|
718
738
|
statusText: this._getStatusText(response.httpStatus),
|
|
719
739
|
headers: response.headers,
|
|
740
|
+
// The daemon puts the MIME type in body_msg_type, not a header; expose it like axios's.
|
|
741
|
+
contentType: response.bodyMsgType || (ctKey ? response.headers[ctKey] : undefined),
|
|
720
742
|
data: this._decodeBody(response, options),
|
|
721
743
|
config: options
|
|
722
744
|
}
|
|
@@ -733,7 +755,7 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
733
755
|
if (canRefresh && typeof provider.refreshAccessToken === 'function') {
|
|
734
756
|
const newToken = await provider.refreshAccessToken(token || null)
|
|
735
757
|
if (typeof newToken === 'string' && newToken.trim()) {
|
|
736
|
-
return this.
|
|
758
|
+
return this._requestOnTransport(ephemeralTransport, method, path, body, { ...options, __appmeshRetried: true })
|
|
737
759
|
}
|
|
738
760
|
}
|
|
739
761
|
}
|
|
@@ -809,29 +831,73 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
809
831
|
}
|
|
810
832
|
|
|
811
833
|
/**
|
|
812
|
-
*
|
|
834
|
+
* The X-Target-Host value every request of this client carries, the framed ones and the
|
|
835
|
+
* streaming side channel alike: as given when it has a port, else the target's port is the
|
|
836
|
+
* one this client dials.
|
|
837
|
+
* @private
|
|
838
|
+
*/
|
|
839
|
+
_forwardTargetHeader () {
|
|
840
|
+
if (!this.forwardingHost) return null
|
|
841
|
+
return this.forwardingHost.includes(':')
|
|
842
|
+
? this.forwardingHost
|
|
843
|
+
: `${this.forwardingHost}:${this.tcpTransport.address[1]}`
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Lazily build the direct-HTTPS client the streaming side channel uses, and keep its token
|
|
848
|
+
* provider, signer and forward target in step with this client.
|
|
849
|
+
* @private
|
|
850
|
+
*/
|
|
851
|
+
_syncSideChannel (client) {
|
|
852
|
+
// Re-sync on every use so a later setBearerToken()/enableRequestSignature() reaches the side channel too.
|
|
853
|
+
if (this._tokenProvider && client._tokenProvider !== this._tokenProvider) {
|
|
854
|
+
client.setTokenProvider(this._tokenProvider)
|
|
855
|
+
}
|
|
856
|
+
if (client._requestSigner !== this._requestSigner) {
|
|
857
|
+
client._requestSigner = this._requestSigner
|
|
858
|
+
}
|
|
859
|
+
// Sent as given on the agent hop, without the TCP port the framed requests append:
|
|
860
|
+
// an explicit port names the target's TCP listener, which does not serve streams.
|
|
861
|
+
const target = this.forwardingHost || null
|
|
862
|
+
if (client.forwardingHost !== target) client.forwardingHost = target
|
|
863
|
+
return client
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/**
|
|
867
|
+
* The side-channel client a local stream dials: the engine's REST listener on the same host.
|
|
813
868
|
* @private
|
|
814
869
|
*/
|
|
815
870
|
_ensureRestChannel () {
|
|
816
871
|
if (!this._restChannelClient) {
|
|
817
872
|
this._restChannelClient = new AppMeshClient(`https://${this._restHost}:${this._restPort}`, this._sslConfig)
|
|
818
873
|
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
874
|
+
return this._syncSideChannel(this._restChannelClient)
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* The side-channel client a forwarded stream dials: the agent REST listener on the same
|
|
879
|
+
* host. The engine listener answers X-Target-Host on a stream route with 400, the agent
|
|
880
|
+
* relays the stream through node forwarding, so the target node runs the task.
|
|
881
|
+
* @private
|
|
882
|
+
*/
|
|
883
|
+
_ensureAgentChannel () {
|
|
884
|
+
if (!this._agentChannelClient) {
|
|
885
|
+
this._agentChannelClient = new AppMeshClient(`https://${this._restHost}:${this._agentPort}`, this._sslConfig)
|
|
822
886
|
}
|
|
823
|
-
return this.
|
|
887
|
+
return this._syncSideChannel(this._agentChannelClient)
|
|
824
888
|
}
|
|
825
889
|
|
|
826
890
|
/**
|
|
827
|
-
* Open an SSE stream over direct HTTPS
|
|
828
|
-
*
|
|
829
|
-
*
|
|
891
|
+
* Open an SSE stream over direct HTTPS: the framed transport answers one buffered frame and
|
|
892
|
+
* cannot carry a chunked event stream (bypassed like the WSS file data channel); plain
|
|
893
|
+
* request/response routes stay on the framed transport. A forwarded stream (`forwardingHost`
|
|
894
|
+
* set) goes to the agent REST listener with X-Target-Host, a local one to the engine listener.
|
|
830
895
|
* @private
|
|
831
896
|
* @override
|
|
832
897
|
*/
|
|
833
898
|
async _sseRequest (method, path, options = {}) {
|
|
834
|
-
|
|
899
|
+
const channel = this.forwardingHost ? this._ensureAgentChannel() : this._ensureRestChannel()
|
|
900
|
+
return channel._sseRequest(method, path, options)
|
|
835
901
|
}
|
|
836
902
|
|
|
837
903
|
/**
|
|
@@ -844,77 +910,79 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
844
910
|
* @override
|
|
845
911
|
*/
|
|
846
912
|
async downloadFile(filePath, localFile = null, applyAttrs = false) {
|
|
847
|
-
this._ensureNoActiveDemuxer()
|
|
848
913
|
if (!localFile) localFile = filePath.split(/[\\/]/).pop()
|
|
849
914
|
const headers = {
|
|
850
915
|
[HTTP_HEADER_KEY_X_FILE_PATH]: encodeURIComponent(filePath),
|
|
851
916
|
[HTTP_HEADER_KEY_X_RECV_FILE_SOCKET]: 'true'
|
|
852
917
|
}
|
|
853
918
|
|
|
854
|
-
const
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
headers
|
|
919
|
+
const ephemeral = this._hasActiveDemuxer() ? this._newFileTransport() : null
|
|
920
|
+
const transport = ephemeral || this.tcpTransport
|
|
921
|
+
try {
|
|
922
|
+
const response = ephemeral
|
|
923
|
+
? await this._requestOnTransport(ephemeral, 'get', '/appmesh/file/download', null, { headers })
|
|
924
|
+
: await this._request('get', '/appmesh/file/download', null, { headers })
|
|
925
|
+
|
|
926
|
+
if (!response.headers[HTTP_HEADER_KEY_X_RECV_FILE_SOCKET]) {
|
|
927
|
+
throw new Error(
|
|
928
|
+
`Server did not respond with socket transfer option: ${HTTP_HEADER_KEY_X_RECV_FILE_SOCKET}`
|
|
929
|
+
)
|
|
860
930
|
}
|
|
861
|
-
)
|
|
862
|
-
|
|
863
|
-
if (!response.headers[HTTP_HEADER_KEY_X_RECV_FILE_SOCKET]) {
|
|
864
|
-
throw new Error(
|
|
865
|
-
`Server did not respond with socket transfer option: ${HTTP_HEADER_KEY_X_RECV_FILE_SOCKET}`
|
|
866
|
-
)
|
|
867
|
-
}
|
|
868
931
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
932
|
+
// Receive file chunks
|
|
933
|
+
const writeStream = fs.createWriteStream(localFile)
|
|
934
|
+
try {
|
|
935
|
+
while (true) {
|
|
936
|
+
const chunk = await transport.receiveMessage()
|
|
937
|
+
if (!chunk || chunk.length === 0) {
|
|
938
|
+
break
|
|
939
|
+
}
|
|
940
|
+
// Honor stream backpressure so large downloads do not buffer in memory
|
|
941
|
+
if (!writeStream.write(chunk)) {
|
|
942
|
+
await once(writeStream, 'drain')
|
|
943
|
+
}
|
|
880
944
|
}
|
|
945
|
+
} finally {
|
|
946
|
+
// Await the flush so the file is complete on disk before this resolves.
|
|
947
|
+
await new Promise((resolve, reject) => {
|
|
948
|
+
writeStream.once('finish', resolve)
|
|
949
|
+
writeStream.once('error', reject)
|
|
950
|
+
writeStream.end()
|
|
951
|
+
})
|
|
881
952
|
}
|
|
882
|
-
} finally {
|
|
883
|
-
// Await the flush so the file is complete on disk before this resolves.
|
|
884
|
-
await new Promise((resolve, reject) => {
|
|
885
|
-
writeStream.once('finish', resolve)
|
|
886
|
-
writeStream.once('error', reject)
|
|
887
|
-
writeStream.end()
|
|
888
|
-
})
|
|
889
|
-
}
|
|
890
953
|
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
954
|
+
// Apply file attributes on Unix systems
|
|
955
|
+
if (applyAttrs && process.platform !== 'win32') {
|
|
956
|
+
// Mode is sent as a decimal string (matches the HTTP transport and Python SDK)
|
|
957
|
+
if (response.headers[HTTP_HEADER_KEY_X_FILE_MODE]) {
|
|
958
|
+
const mode = parseInt(response.headers[HTTP_HEADER_KEY_X_FILE_MODE], 10)
|
|
959
|
+
// A malformed or out-of-range remote value must not fail the finished download
|
|
960
|
+
if (Number.isInteger(mode) && mode >= 0 && mode <= 0o777) {
|
|
961
|
+
fs.chmodSync(localFile, mode)
|
|
962
|
+
} else {
|
|
963
|
+
console.warn(`Warning: Invalid X-File-Mode value: ${response.headers[HTTP_HEADER_KEY_X_FILE_MODE]}`)
|
|
964
|
+
}
|
|
901
965
|
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
966
|
+
if (response.headers[HTTP_HEADER_KEY_X_FILE_USER] && response.headers[HTTP_HEADER_KEY_X_FILE_GROUP]) {
|
|
967
|
+
// The daemon sends user/group *names* (os::fileStat); resolve them to
|
|
968
|
+
// numeric uid/gid locally before chown, same as the HTTP transport.
|
|
969
|
+
const uid = await _resolveUid(response.headers[HTTP_HEADER_KEY_X_FILE_USER])
|
|
970
|
+
const gid = await _resolveGid(response.headers[HTTP_HEADER_KEY_X_FILE_GROUP])
|
|
971
|
+
if (uid !== null && gid !== null) {
|
|
972
|
+
try {
|
|
973
|
+
fs.chownSync(localFile, uid, gid)
|
|
974
|
+
} catch (err) {
|
|
975
|
+
console.warn(
|
|
976
|
+
`Warning: Unable to change owner/group of ${localFile}. Operation requires elevated privileges.`
|
|
977
|
+
)
|
|
978
|
+
}
|
|
915
979
|
}
|
|
916
980
|
}
|
|
917
981
|
}
|
|
982
|
+
} finally {
|
|
983
|
+
if (ephemeral) {
|
|
984
|
+
ephemeral.close()
|
|
985
|
+
}
|
|
918
986
|
}
|
|
919
987
|
}
|
|
920
988
|
|
|
@@ -930,7 +998,6 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
930
998
|
* @override
|
|
931
999
|
*/
|
|
932
1000
|
async uploadFile(localFile, filePath = null, applyAttrs = false, overwrite = false) {
|
|
933
|
-
this._ensureNoActiveDemuxer()
|
|
934
1001
|
if (!filePath) filePath = localFile.split(/[\\/]/).pop()
|
|
935
1002
|
if (!fs.existsSync(localFile)) {
|
|
936
1003
|
throw new Error(`Local file not found: ${localFile}`)
|
|
@@ -939,7 +1006,8 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
939
1006
|
const headers = {
|
|
940
1007
|
[HTTP_HEADER_KEY_X_FILE_PATH]: encodeURIComponent(filePath),
|
|
941
1008
|
'Content-Type': 'text/plain',
|
|
942
|
-
[HTTP_HEADER_KEY_X_SEND_FILE_SOCKET]: 'true'
|
|
1009
|
+
[HTTP_HEADER_KEY_X_SEND_FILE_SOCKET]: 'true',
|
|
1010
|
+
'X-File-Upload-Ack': 'true'
|
|
943
1011
|
}
|
|
944
1012
|
if (overwrite) {
|
|
945
1013
|
headers[HTTP_HEADER_KEY_X_FILE_OVERWRITE] = 'true'
|
|
@@ -958,27 +1026,70 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
958
1026
|
headers[HTTP_HEADER_KEY_X_FILE_GROUP] = groupName || String(stats.gid)
|
|
959
1027
|
}
|
|
960
1028
|
|
|
961
|
-
const
|
|
962
|
-
|
|
963
|
-
|
|
1029
|
+
const ephemeral = this._hasActiveDemuxer() ? this._newFileTransport() : null
|
|
1030
|
+
const transport = ephemeral || this.tcpTransport
|
|
1031
|
+
try {
|
|
1032
|
+
const response = ephemeral
|
|
1033
|
+
? await this._requestOnTransport(ephemeral, 'post', '/appmesh/file/upload', null, { headers })
|
|
1034
|
+
: await this._request('post', '/appmesh/file/upload', null, { headers })
|
|
1035
|
+
|
|
1036
|
+
if (!response.headers[HTTP_HEADER_KEY_X_SEND_FILE_SOCKET]) {
|
|
1037
|
+
throw new Error(
|
|
1038
|
+
`Server did not respond with socket transfer option: ${HTTP_HEADER_KEY_X_SEND_FILE_SOCKET}`
|
|
1039
|
+
)
|
|
1040
|
+
}
|
|
964
1041
|
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
)
|
|
969
|
-
}
|
|
1042
|
+
// Send file chunks
|
|
1043
|
+
const readStream = fs.createReadStream(localFile, {
|
|
1044
|
+
highWaterMark: TCP_BLOCK_SIZE
|
|
1045
|
+
})
|
|
970
1046
|
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
})
|
|
1047
|
+
for await (const chunk of readStream) {
|
|
1048
|
+
transport.sendMessage(chunk)
|
|
1049
|
+
}
|
|
975
1050
|
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
}
|
|
1051
|
+
// Send EOF signal
|
|
1052
|
+
transport.sendMessage([])
|
|
979
1053
|
|
|
980
|
-
|
|
981
|
-
|
|
1054
|
+
const ackOpt = _headerValue(response.headers, 'X-File-Upload-Ack')
|
|
1055
|
+
if (ackOpt && String(ackOpt).toLowerCase() === 'true') {
|
|
1056
|
+
const ackData = await transport.receiveMessage()
|
|
1057
|
+
if (!ackData) {
|
|
1058
|
+
throw new AppMeshRequestError('Upload commit acknowledgment failed: connection closed', 502)
|
|
1059
|
+
}
|
|
1060
|
+
const ack = msgpack.decode(ackData)
|
|
1061
|
+
if (ack.http_status !== 200) {
|
|
1062
|
+
const msg = this._extractErrorMessage(ack.body)
|
|
1063
|
+
throw new AppMeshRequestError(`HTTP ${ack.http_status}: ${msg}`, ack.http_status)
|
|
1064
|
+
}
|
|
1065
|
+
} else {
|
|
1066
|
+
// Flush buffered writes on an ephemeral socket before destroy() in finally.
|
|
1067
|
+
const sock = ephemeral && ephemeral.socket
|
|
1068
|
+
if (sock && typeof sock.end === 'function' && !sock.destroyed) {
|
|
1069
|
+
await new Promise((resolve, reject) => {
|
|
1070
|
+
const cleanup = () => {
|
|
1071
|
+
sock.removeListener('error', onError)
|
|
1072
|
+
sock.removeListener('close', onDone)
|
|
1073
|
+
}
|
|
1074
|
+
const onDone = () => {
|
|
1075
|
+
cleanup()
|
|
1076
|
+
resolve()
|
|
1077
|
+
}
|
|
1078
|
+
const onError = (err) => {
|
|
1079
|
+
cleanup()
|
|
1080
|
+
reject(err)
|
|
1081
|
+
}
|
|
1082
|
+
sock.once('error', onError)
|
|
1083
|
+
sock.once('close', onDone)
|
|
1084
|
+
sock.end(onDone)
|
|
1085
|
+
})
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
} finally {
|
|
1089
|
+
if (ephemeral) {
|
|
1090
|
+
ephemeral.close()
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
982
1093
|
}
|
|
983
1094
|
|
|
984
1095
|
/**
|
|
@@ -1062,17 +1173,12 @@ class AppMeshClientTCP extends AppMeshClient {
|
|
|
1062
1173
|
return result
|
|
1063
1174
|
}
|
|
1064
1175
|
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
_ensureNoActiveDemuxer () {
|
|
1072
|
-
if (this._demuxer && this._demuxer._running) {
|
|
1073
|
-
throw new Error('file transfer is not supported while an event subscription is active over the same TCP ' +
|
|
1074
|
-
'connection; use a separate client instance')
|
|
1075
|
-
}
|
|
1176
|
+
_hasActiveDemuxer () {
|
|
1177
|
+
return Boolean((this._demuxer && this._demuxer._running) || this._demuxerStarting)
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
_newFileTransport () {
|
|
1181
|
+
return new TCPTransport(this.tcpTransport.address, this.tcpTransport.sslConfig)
|
|
1076
1182
|
}
|
|
1077
1183
|
|
|
1078
1184
|
/**
|