Skip to content

Commit 9cb44a9

Browse files
Rasmus SybergMTschannett
authored andcommitted
Added check for Buffer module (aka not Node.js)
1 parent 6263881 commit 9cb44a9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "class-transformer",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors",
55
"license": "MIT",
66
"readmeFilename": "README.md",

src/TransformOperationExecutor.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ export class TransformOperationExecutor {
9797
if (value === null || value === undefined)
9898
return value;
9999
return new Date(value);
100-
101-
} else if ((targetType === Buffer || value instanceof Buffer) && !isMap) {
100+
101+
} else if (testForBuffer() && (targetType === Buffer || value instanceof Buffer) && !isMap) {
102102
if (value === null || value === undefined)
103103
return value;
104104
return Buffer.from(value);
@@ -411,10 +411,19 @@ export class TransformOperationExecutor {
411411

412412
}
413413

414-
function instantiateArrayType (arrayType: Function): Array<any> | Set<any> {
414+
function instantiateArrayType(arrayType: Function): Array<any> | Set<any> {
415415
const array = new (arrayType as any)();
416416
if (!(array instanceof Set) && !("push" in array)) {
417417
return [];
418418
}
419419
return array;
420420
}
421+
422+
423+
function testForBuffer(): boolean {
424+
try {
425+
Buffer
426+
return true;
427+
} catch {}
428+
return false;
429+
}

0 commit comments

Comments
 (0)