Skip to content

Commit 4a69632

Browse files
authored
fix(proto): protobuf exports may be incorrect if minimized (#1279)
1 parent 4b265ec commit 4a69632

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

packages/common/src/converter/protobuf-payload-converters.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ function resetBufferInGlobal(hasChanged: boolean): void {
192192
function isProtobufType(type: unknown): type is Type {
193193
return (
194194
isRecord(type) &&
195-
type.constructor.name === 'Type' &&
195+
// constructor.name may get mangled by minifiers; thanksfuly protobufjs also sets a className property
196+
(type.constructor as any).className === 'Type' &&
196197
hasOwnProperties(type, ['parent', 'name', 'create', 'encode', 'decode']) &&
197198
typeof type.name === 'string' &&
198199
typeof type.create === 'function' &&
@@ -214,7 +215,8 @@ function getNamespacedTypeName(node: Type | Namespace): string {
214215
}
215216

216217
function isRoot(root: unknown): root is Root {
217-
return isRecord(root) && root.constructor.name === 'Root';
218+
// constructor.name may get mangled by minifiers; thanksfuly protobufjs also sets a className property
219+
return isRecord(root) && (root.constructor as any).className === 'Root';
218220
}
219221

220222
export interface DefaultPayloadConverterWithProtobufsOptions {

packages/proto/src/patch-protobuf-root.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ type Type = Record<string, unknown>;
5353
type Namespace = { nested: Record<string, unknown> };
5454

5555
function isType(value: unknown): value is Type {
56-
return isRecord(value) && value.constructor.name === 'Type';
56+
// constructor.name may get mangled by minifiers; thanksfuly protobufjs also sets a constructor.className property
57+
return isRecord(value) && (value.constructor as any).className === 'Type';
5758
}
5859

5960
function isNamespace(value: unknown): value is Namespace {
60-
return isRecord(value) && value.constructor.name === 'Namespace';
61+
// constructor.name may get mangled by minifiers; thanksfuly protobufjs also sets a constructor.className property
62+
return isRecord(value) && (value.constructor as any).className === 'Namespace';
6163
}
6264

6365
// Duplicate from type-helpers instead of importing in order to avoid circular dependency

packages/test/src/test-patch-root.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ test('patchRoot', (t) => {
2323
});
2424

2525
class Namespace {
26+
public static className = 'Namespace';
27+
2628
constructor(props: Record<string, unknown>) {
2729
for (const key in props) {
2830
(this as any)[key] = props[key];
2931
}
3032
}
3133
}
3234

33-
class Type {}
35+
class Type {
36+
public static className = 'Type';
37+
}

0 commit comments

Comments
 (0)