|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import stream from 'node:stream'; |
| 4 | +import util from 'node:util'; |
3 | 5 | import { stringify, NIL, parse } from 'uuid';
|
4 | 6 | import fetch, { HeadersInit } from 'node-fetch';
|
5 | 7 | import {
|
@@ -36,6 +38,9 @@ import { DBSQLParameter, DBSQLParameterValue } from './DBSQLParameter';
|
36 | 38 | import ParameterError from './errors/ParameterError';
|
37 | 39 | import IClientContext, { ClientConfig } from './contracts/IClientContext';
|
38 | 40 |
|
| 41 | +// Explicitly promisify a callback-style `pipeline` because `node:stream/promises` is not available in Node 14 |
| 42 | +const pipeline = util.promisify(stream.pipeline); |
| 43 | + |
39 | 44 | const defaultMaxRows = 100000;
|
40 | 45 |
|
41 | 46 | interface OperationResponseShape {
|
@@ -271,8 +276,10 @@ export default class DBSQLSession implements IDBSQLSession {
|
271 | 276 | if (!response.ok) {
|
272 | 277 | throw new StagingError(`HTTP error ${response.status} ${response.statusText}`);
|
273 | 278 | }
|
274 |
| - const buffer = await response.arrayBuffer(); |
275 |
| - fs.writeFileSync(localFile, Buffer.from(buffer)); |
| 279 | + |
| 280 | + const fileStream = fs.createWriteStream(localFile); |
| 281 | + // `pipeline` will do all the dirty job for us, including error handling and closing all the streams properly |
| 282 | + return pipeline(response.body, fileStream); |
276 | 283 | }
|
277 | 284 |
|
278 | 285 | private async handleStagingRemove(presignedUrl: string, headers: HeadersInit): Promise<void> {
|
@@ -301,8 +308,19 @@ export default class DBSQLSession implements IDBSQLSession {
|
301 | 308 | const connectionProvider = await this.context.getConnectionProvider();
|
302 | 309 | const agent = await connectionProvider.getAgent();
|
303 | 310 |
|
304 |
| - const data = fs.readFileSync(localFile); |
305 |
| - const response = await fetch(presignedUrl, { method: 'PUT', headers, agent, body: data }); |
| 311 | + const fileStream = fs.createReadStream(localFile); |
| 312 | + const fileInfo = fs.statSync(localFile, { bigint: true }); |
| 313 | + |
| 314 | + const response = await fetch(presignedUrl, { |
| 315 | + method: 'PUT', |
| 316 | + headers: { |
| 317 | + ...headers, |
| 318 | + // This header is required by server |
| 319 | + 'Content-Length': fileInfo.size.toString(), |
| 320 | + }, |
| 321 | + agent, |
| 322 | + body: fileStream, |
| 323 | + }); |
306 | 324 | if (!response.ok) {
|
307 | 325 | throw new StagingError(`HTTP error ${response.status} ${response.statusText}`);
|
308 | 326 | }
|
|
0 commit comments