@@ -18,7 +18,6 @@ const { S3Client,
1818 NotFound } = require ( '@aws-sdk/client-s3' ) ;
1919const { Upload } = require ( '@aws-sdk/lib-storage' ) ;
2020const werelogs = require ( 'werelogs' ) ;
21- const { Readable } = require ( 'stream' ) ;
2221const errors = require ( '../../../errors' ) . default ;
2322const errorInstances = require ( '../../../errors' ) . errorInstances ;
2423const MD5Sum = require ( '../../../s3middleware/MD5Sum' ) . default ;
@@ -121,62 +120,6 @@ class AwsClient {
121120 } ;
122121 }
123122
124- _normalizeSdkStream ( body ) {
125- if ( ! body ) {
126- return Readable . from ( [ ] ) ;
127- }
128-
129- const isReadable = Readable . isReadable
130- ? Readable . isReadable ( body )
131- : body instanceof Readable || typeof body ?. pipe === 'function' ;
132-
133- if ( isReadable ) {
134- return body ;
135- }
136-
137- if ( body . source ) {
138- const source = body . source ;
139- const sourceIsReadable = Readable . isReadable
140- ? Readable . isReadable ( source )
141- : source instanceof Readable || typeof source ?. pipe === 'function' ;
142- if ( sourceIsReadable ) {
143- return source ;
144- }
145- }
146-
147- const fromWeb = Readable . fromWeb ? Readable . fromWeb . bind ( Readable ) : null ;
148-
149- if ( typeof body . transformToWebStream === 'function' && fromWeb ) {
150- return fromWeb ( body . transformToWebStream ( ) ) ;
151- }
152-
153- if ( typeof body . getReader === 'function' && fromWeb ) {
154- return fromWeb ( body ) ;
155- }
156-
157- if ( typeof body . stream === 'function' && fromWeb ) {
158- return fromWeb ( body . stream ( ) ) ;
159- }
160-
161- if ( body instanceof Uint8Array || ArrayBuffer . isView ( body ) ) {
162- return Readable . from ( body ) ;
163- }
164-
165- if ( body instanceof ArrayBuffer ) {
166- return Readable . from ( Buffer . from ( body ) ) ;
167- }
168-
169- if ( typeof body === 'string' ) {
170- return Readable . from ( [ body ] ) ;
171- }
172-
173- if ( typeof body ?. [ Symbol . asyncIterator ] === 'function' ) {
174- return Readable . from ( body ) ;
175- }
176-
177- return undefined ;
178- }
179-
180123 put ( stream , size , keyContext , reqUids , callback ) {
181124 const awsKey = this . _createAwsKey ( keyContext . bucketName ,
182125 keyContext . objectKey , this . _bucketMatch ) ;
@@ -295,70 +238,47 @@ class AwsClient {
295238 const abortController = new AbortController ( ) ;
296239 this . _client . send ( command , { abortSignal : abortController . signal } )
297240 . then ( data => {
298- // const rawBody = data.Body;
299- // const stream = this._normalizeSdkStream(rawBody);
300- // if (!stream || typeof stream.on !== 'function') {
301- // const bodyType = rawBody?.constructor?.name || typeof rawBody;
302- // log.error('unsupported body type from AWS SDK getObject response', {
303- // method: 'AwsClient.get',
304- // backendType: this.clientType,
305- // dataStoreName: this._dataStoreName,
306- // bodyType,
307- // });
308- // return cbOnce(errorInstances.InternalError
309- // .customizeDescription('Unsupported response body type returned from AWS SDK'));
310- // }
311-
312- // if (data.$metadata?.httpHeaders) {
313- // log.trace(`${this.type} GET request response headers`, {
314- // responseHeaders: data.$metadata.httpHeaders,
315- // backendType: this.clientType,
316- // });
317- // }
318-
319- // let finished = false;
320- // const originalDestroy = stream.destroy.bind(stream);
241+ const stream = data . Body ;
242+ if ( ! stream ) {
243+ const bodyType = stream ?. constructor ?. name || typeof stream ;
244+ log . error ( 'unsupported body type from AWS SDK getObject response' , {
245+ method : 'AwsClient.get' ,
246+ backendType : this . clientType ,
247+ dataStoreName : this . _dataStoreName ,
248+ bodyType,
249+ } ) ;
250+ return cbOnce ( errorInstances . InternalError
251+ . customizeDescription ( 'Unsupported response body type returned from AWS SDK' ) ) ;
252+ }
321253
322- // const abortRequest = err => {
323- // if (finished) {
324- // return stream;
325- // }
326- // finished = true;
327- // log.debug('aborting GET request in progress', { objectGetInfo });
328- // abortController.abort(err);
329- // if (rawBody && rawBody !== stream) {
330- // rawBody.destroy?.(err);
331- // }
332- // try {
333- // originalDestroy(err);
334- // } catch (destroyErr) {
335- // log.debug('error while destroying aws sdk stream', {
336- // method: 'AwsClient.get',
337- // backendType: this.clientType,
338- // dataStoreName: this._dataStoreName,
339- // error: destroyErr,
340- // });
341- // }
342- // return stream;
343- // };
254+ if ( data . $metadata ?. httpHeaders ) {
255+ log . trace ( `${ this . type } GET request response headers` , {
256+ responseHeaders : data . $metadata . httpHeaders ,
257+ backendType : this . clientType ,
258+ } ) ;
259+ }
344260
345- // stream.abort = abortRequest;
346- // stream.destroy = err => abortRequest(err);
347- // stream.createReadStream = () => stream;
261+ // Override destroy to also abort the HTTP request
262+ const originalDestroy = stream . destroy . bind ( stream ) ;
263+ stream . destroy = err => {
264+ log . debug ( 'aborting GET request in progress' , { objectGetInfo } ) ;
265+ abortController . abort ( err ) ;
266+ return originalDestroy ( err ) ;
267+ } ;
348268
349- // stream.on('error', err => {
350- // const logLevel = err?.code === 'NotFound' ? 'info' : 'error';
351- // logHelper(
352- // log,
353- // logLevel,
354- // `error streaming data from ${this.type}`,
355- // err,
356- // this._dataStoreName,
357- // this.clientType
358- // );
359- // });
269+ stream . on ( 'error' , err => {
270+ const logLevel = err ?. code === 'NotFound' ? 'info' : 'error' ;
271+ logHelper (
272+ log ,
273+ logLevel ,
274+ `error streaming data from ${ this . type } ` ,
275+ err ,
276+ this . _dataStoreName ,
277+ this . clientType
278+ ) ;
279+ } ) ;
360280
361- return process . nextTick ( ( ) => cbOnce ( null , null ) ) ;
281+ return process . nextTick ( ( ) => cbOnce ( null , stream ) ) ;
362282 } )
363283 . catch ( err => {
364284 if ( err instanceof NoSuchKey || err instanceof NotFound ) {
0 commit comments