Skip to content

Commit

Permalink
fix: Add missing globalThis prefixes. (#988)
Browse files Browse the repository at this point in the history
Fixes #987.
  • Loading branch information
stephenh authored Jan 20, 2024
1 parent c0068ba commit 86aca54
Show file tree
Hide file tree
Showing 25 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -779,5 +779,5 @@ function isSet(value: any): boolean {
}

function fail(message?: string): never {
throw new Error(message ?? "Failed");
throw new globalThis.Error(message ?? "Failed");
}
2 changes: 1 addition & 1 deletion integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,5 +779,5 @@ function isSet(value: any): boolean {
}

function fail(message?: string): never {
throw new Error(message ?? "Failed");
throw new globalThis.Error(message ?? "Failed");
}
2 changes: 1 addition & 1 deletion integration/extensions/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,5 +625,5 @@ export interface Extension<T> {
}

function fail(message?: string): never {
throw new Error(message ?? "Failed");
throw new globalThis.Error(message ?? "Failed");
}
2 changes: 1 addition & 1 deletion integration/grpc-js/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
4 changes: 2 additions & 2 deletions integration/map-bigint-optional/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ export const MapBigInt_MapEntry = {
encode(message: MapBigInt_MapEntry, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.key !== BigInt("0")) {
if (BigInt.asUintN(64, message.key) !== message.key) {
throw new Error("value provided for field message.key of type fixed64 too large");
throw new globalThis.Error("value provided for field message.key of type fixed64 too large");
}
writer.uint32(9).fixed64(message.key.toString());
}
if (message.value !== BigInt("0")) {
if (BigInt.asIntN(64, message.value) !== message.value) {
throw new Error("value provided for field message.value of type int64 too large");
throw new globalThis.Error("value provided for field message.value of type int64 too large");
}
writer.uint32(16).int64(message.value.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion integration/nestjs-simple/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = Struct.wrap(value);
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new gt.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/nice-grpc/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/oneof-unions-snake/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export const Value = {
} else if (typeof value === "object") {
result.kind = { $case: "struct_value", struct_value: value };
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/oneof-unions/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export const Value = {
} else if (typeof value === "object") {
result.kind = { $case: "structValue", structValue: value };
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const Timestamp = {
encode(message: Timestamp, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.seconds !== BigInt("0")) {
if (BigInt.asIntN(64, message.seconds) !== message.seconds) {
throw new Error("value provided for field message.seconds of type int64 too large");
throw new globalThis.Error("value provided for field message.seconds of type int64 too large");
}
writer.uint32(8).int64(message.seconds.toString());
}
Expand Down
4 changes: 2 additions & 2 deletions integration/simple-long-bigint/google/protobuf/wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const Int64Value = {
encode(message: Int64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== BigInt("0")) {
if (BigInt.asIntN(64, message.value) !== message.value) {
throw new Error("value provided for field message.value of type int64 too large");
throw new globalThis.Error("value provided for field message.value of type int64 too large");
}
writer.uint32(8).int64(message.value.toString());
}
Expand Down Expand Up @@ -276,7 +276,7 @@ export const UInt64Value = {
encode(message: UInt64Value, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.value !== BigInt("0")) {
if (BigInt.asUintN(64, message.value) !== message.value) {
throw new Error("value provided for field message.value of type uint64 too large");
throw new globalThis.Error("value provided for field message.value of type uint64 too large");
}
writer.uint32(8).uint64(message.value.toString());
}
Expand Down
12 changes: 6 additions & 6 deletions integration/simple-long-bigint/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const Numbers = {
}
if (message.int64 !== BigInt("0")) {
if (BigInt.asIntN(64, message.int64) !== message.int64) {
throw new Error("value provided for field message.int64 of type int64 too large");
throw new globalThis.Error("value provided for field message.int64 of type int64 too large");
}
writer.uint32(32).int64(message.int64.toString());
}
Expand All @@ -66,7 +66,7 @@ export const Numbers = {
}
if (message.uint64 !== BigInt("0")) {
if (BigInt.asUintN(64, message.uint64) !== message.uint64) {
throw new Error("value provided for field message.uint64 of type uint64 too large");
throw new globalThis.Error("value provided for field message.uint64 of type uint64 too large");
}
writer.uint32(48).uint64(message.uint64.toString());
}
Expand All @@ -75,7 +75,7 @@ export const Numbers = {
}
if (message.sint64 !== BigInt("0")) {
if (BigInt.asIntN(64, message.sint64) !== message.sint64) {
throw new Error("value provided for field message.sint64 of type sint64 too large");
throw new globalThis.Error("value provided for field message.sint64 of type sint64 too large");
}
writer.uint32(64).sint64(message.sint64.toString());
}
Expand All @@ -84,7 +84,7 @@ export const Numbers = {
}
if (message.fixed64 !== BigInt("0")) {
if (BigInt.asUintN(64, message.fixed64) !== message.fixed64) {
throw new Error("value provided for field message.fixed64 of type fixed64 too large");
throw new globalThis.Error("value provided for field message.fixed64 of type fixed64 too large");
}
writer.uint32(81).fixed64(message.fixed64.toString());
}
Expand All @@ -93,7 +93,7 @@ export const Numbers = {
}
if (message.sfixed64 !== BigInt("0")) {
if (BigInt.asIntN(64, message.sfixed64) !== message.sfixed64) {
throw new Error("value provided for field message.sfixed64 of type sfixed64 too large");
throw new globalThis.Error("value provided for field message.sfixed64 of type sfixed64 too large");
}
writer.uint32(97).sfixed64(message.sfixed64.toString());
}
Expand All @@ -106,7 +106,7 @@ export const Numbers = {
writer.uint32(122).fork();
for (const v of message.uint64s) {
if (BigInt.asUintN(64, v) !== v) {
throw new Error("a value provided in array field uint64s of type uint64 is too large");
throw new globalThis.Error("a value provided in array field uint64s of type uint64 is too large");
}
writer.uint64(v.toString());
}
Expand Down
2 changes: 1 addition & 1 deletion integration/simple-snake/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.struct_value = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/simple-string-enums/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/static-only/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/struct/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/type-annotations/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/type-registry/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/use-map-type/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/use-readonly-types/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ export const Value = {
} else if (typeof value === "object") {
result.kind = { $case: "structValue", structValue: value };
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
2 changes: 1 addition & 1 deletion integration/value/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const Value = {
} else if (typeof value === "object") {
result.structValue = value;
} else if (typeof value !== "undefined") {
throw new Error("Unsupported any value type: " + typeof value);
throw new globalThis.Error("Unsupported any value type: " + typeof value);
}
return result;
},
Expand Down
6 changes: 3 additions & 3 deletions src/generate-struct-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function generateWrapDeep(ctx: Context, fullProtoTypeName: string, fieldN
} else if (typeof value === 'object') {
result.${fieldNames.structValue} = Struct.wrap(value);
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value);
}
return result;
}`);
Expand Down Expand Up @@ -196,7 +196,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie
} else if (typeof value === 'object') {
result.kind = {$case: '${fieldNames.structValue}', ${fieldNames.structValue}: value};
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value);
}
return result;
}`);
Expand All @@ -216,7 +216,7 @@ export function generateWrapShallow(ctx: Context, fullProtoTypeName: string, fie
} else if (typeof value === 'object') {
result.${fieldNames.structValue} = value;
} else if (typeof value !== 'undefined') {
throw new Error('Unsupported any value type: ' + typeof value);
throw new ${ctx.utils.globalThis}.Error('Unsupported any value type: ' + typeof value);
}
return result;
}`);
Expand Down
18 changes: 11 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export function makeUtils(options: Options): Utils {
...makeNiceGrpcServerStreamingMethodResult(),
...makeGrpcWebErrorClass(bytes),
...makeExtensionClass(options),
...makeAssertionUtils(),
...makeAssertionUtils(bytes),
};
}

Expand Down Expand Up @@ -869,12 +869,12 @@ function makeExtensionClass(options: Options) {
return { Extension };
}

function makeAssertionUtils() {
function makeAssertionUtils(bytes: ReturnType<typeof makeByteUtils>) {
const fail = conditionalOutput(
"fail",
code`
function fail(message?: string): never {
throw new Error(message ?? "Failed");
throw new ${bytes.globalThis}.Error(message ?? "Failed");
}
`,
);
Expand Down Expand Up @@ -1298,13 +1298,13 @@ function getEncodeWriteSnippet(ctx: Context, field: FieldDescriptorProto): (plac
case "sint64":
case "sfixed64":
return (place) => code`if (BigInt.asIntN(64, ${place}) !== ${place}) {
throw new Error('value provided for field ${place} of type ${fieldType} too large');
throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large');
}
writer.uint32(${tag}).${toReaderCall(field)}(${place}.toString())`;
case "uint64":
case "fixed64":
return (place) => code`if (BigInt.asUintN(64, ${place}) !== ${place}) {
throw new Error('value provided for field ${place} of type ${fieldType} too large');
throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large');
}
writer.uint32(${tag}).${toReaderCall(field)}(${place}.toString())`;
default:
Expand Down Expand Up @@ -1474,7 +1474,9 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP
writer.uint32(${tag}).fork();
for (const v of ${messageProperty}) {
if (BigInt.asIntN(64, v) !== v) {
throw new Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
throw new ${
utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
}
writer.${toReaderCall(field)}(${rhs("v")});
}
Expand All @@ -1487,7 +1489,9 @@ function generateEncode(ctx: Context, fullName: string, messageDesc: DescriptorP
writer.uint32(${tag}).fork();
for (const v of ${messageProperty}) {
if (BigInt.asUintN(64, v) !== v) {
throw new Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
throw new ${
utils.globalThis
}.Error('a value provided in array field ${fieldName} of type ${fieldType} is too large');
}
writer.${toReaderCall(field)}(${rhs("v")});
}
Expand Down

0 comments on commit 86aca54

Please sign in to comment.