Skip to content

Commit

Permalink
fix(node): deepCopy not working for large Buffers (#909)
Browse files Browse the repository at this point in the history
deepCopy does not work when copying an object
with a large Buffer (see test case).

This causes issues when using multipartUpload with the
`checkpoint` option: e.g.

```js
const file = Buffer.alloc(Math.pow(2, 30) - 1);
// throws RangeError in utils/deepCopy
multipartUpload("name", file, {checkpoint: {file, ...}});
```
  • Loading branch information
aloisklink authored Dec 29, 2020
1 parent 8e1adfd commit f52cd2e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/common/utils/deepCopy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepCopy = void 0;
const isBuffer_1 = require("./isBuffer");
exports.deepCopy = (obj) => {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (isBuffer_1.isBuffer(obj)) {
return Buffer.from(obj);
}
const copy = Array.isArray(obj) ? [] : {};
Object.keys(obj).forEach((key) => {
copy[key] = exports.deepCopy(obj[key]);
Expand Down
5 changes: 5 additions & 0 deletions lib/common/utils/deepCopy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { isBuffer } from './isBuffer';

export const deepCopy = (obj) => {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (isBuffer(obj)) {
return Buffer.from(obj);
}

const copy = Array.isArray(obj) ? [] : {};

Expand Down
15 changes: 15 additions & 0 deletions test/node/utils/deepCopy.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const assert = require('assert');
const { Buffer } = require('buffer');
const { deepCopy } = require('../../../lib/common/utils/deepCopy');

describe('utils/deepCopy()', () => {
it('should copy big Buffers correctly', () => {
// 2^30 - 1 ~ 1GB is max size on 32-bit computer
// See https://nodejs.org/api/buffer.html#buffer_buffer_constants_max_length
const numberBytes = Math.pow(2, 30) - 1;
const obj = {
buffer: Buffer.alloc(numberBytes),
};
assert.deepStrictEqual(deepCopy(obj), obj);
});
});

0 comments on commit f52cd2e

Please sign in to comment.