Skip to content

Commit

Permalink
Recompile tx-signer.proto with the option importSuffix=.js
Browse files Browse the repository at this point in the history
Importing `pocketjs-transaction-builder` as an ES module caused
ERR_MODULE_NOT_FOUND because index.js imported protobufjs without
extension like `import r from"protobufjs/minimal"`.

This patch updates the script to compile .proto with the new option
`importSuffix`, which was introduced in ts-proto v1.117.0, to add
".js" extension in importing statements.
  • Loading branch information
msmania committed Feb 20, 2024
1 parent 077fe7b commit 7dd626f
Show file tree
Hide file tree
Showing 10 changed files with 1,161 additions and 1,116 deletions.
3 changes: 3 additions & 0 deletions packages/transaction-builder/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
"^(\\.\\/.+)\\.js$": "$1",
},
}
4 changes: 2 additions & 2 deletions packages/transaction-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"@tendermint/belt": "^0.3.0",
"buffer": "^6.0.3",
"long": "^5.2.0",
"protobufjs": "6.8.8",
"ts-proto": "^1.110.4",
"protobufjs": "6.11.4",
"ts-proto": "^1.167.8",
"varint": "^6.0.0"
}
}
20 changes: 8 additions & 12 deletions packages/transaction-builder/src/models/proto/gen.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
protoc --plugin=../../../node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=generated ./tx-signer.proto --ts_proto_opt=esModuleInterop=true
#!/bin/sh

# when running this gen file import * as _m0 from "protobufjs/minimal" causes import bugs and does not result in same import pattern as previous releases.
# This commit here shows that potentially it was manually added (https://github.com/pokt-foundation/pocket-js/commit/00e85a5c0b6a20ced85a3df7a12bb25004d691a9#diff-ecd6dda6a974a6196f2d4d4681856ae6668c31a4f0a45a0d06b8cb2c74739273R8)
# Not sure how modify gen script to result in same import, so the below statements will replace import * as _m0 from "protobufjs/minimal" to import _m0 from "protobufjs/minimal"
file_paths=(
"generated/tx-signer.ts"
"generated/google/protobuf/any.ts"
)
[ -d generated ] && rm -rf generated
mkdir generated

# Perform the import replacement for each file path using a for loop
for file_path in "${file_paths[@]}"; do
sed -i 's/import \* as _m0 from '\''protobufjs\/minimal'\''/import _m0 from '\''protobufjs\/minimal'\''/' "$file_path"
done
protoc --plugin=../../../node_modules/.bin/protoc-gen-ts_proto \
--ts_proto_out=generated \
--ts_proto_opt=esModuleInterop=true \
--ts_proto_opt=importSuffix=.js \
./tx-signer.proto

This file was deleted.

195 changes: 92 additions & 103 deletions packages/transaction-builder/src/models/proto/generated/google/protobuf/any.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable */
import Long from 'long'
import _m0 from 'protobufjs/minimal'
import _m0 from "protobufjs/minimal.js";

export const protobufPackage = 'google.protobuf'
export const protobufPackage = "google.protobuf";

/**
* `Any` contains an arbitrary serialized protocol buffer message along with a
Expand All @@ -29,6 +28,10 @@ export const protobufPackage = 'google.protobuf'
* if (any.is(Foo.class)) {
* foo = any.unpack(Foo.class);
* }
* // or ...
* if (any.isSameTypeAs(Foo.getDefaultInstance())) {
* foo = any.unpack(Foo.getDefaultInstance());
* }
*
* Example 3: Pack and unpack a message in Python.
*
Expand All @@ -43,10 +46,13 @@ export const protobufPackage = 'google.protobuf'
* Example 4: Pack and unpack a message in Go
*
* foo := &pb.Foo{...}
* any, err := ptypes.MarshalAny(foo)
* any, err := anypb.New(foo)
* if err != nil {
* ...
* }
* ...
* foo := &pb.Foo{}
* if err := ptypes.UnmarshalAny(any, foo); err != nil {
* if err := any.UnmarshalTo(foo); err != nil {
* ...
* }
*
Expand All @@ -56,7 +62,6 @@ export const protobufPackage = 'google.protobuf'
* in the type URL, for example "foo.bar.com/x/y.z" will yield type
* name "y.z".
*
*
* JSON
* ====
* The JSON representation of an `Any` value uses the regular
Expand Down Expand Up @@ -88,7 +93,8 @@ export const protobufPackage = 'google.protobuf'
export interface Any {
/**
* A URL/resource name that uniquely identifies the type of the serialized
* protocol buffer message. The last segment of the URL's path must represent
* protocol buffer message. This string must contain at least
* one "/" character. The last segment of the URL's path must represent
* the fully qualified name of the type (as in
* `path/google.protobuf.Duration`). The name should be in a canonical form
* (e.g., leading "." is not accepted).
Expand All @@ -109,145 +115,128 @@ export interface Any {
*
* Note: this functionality is not currently available in the official
* protobuf release, and it is not used for type URLs beginning with
* type.googleapis.com.
* type.googleapis.com. As of May 2023, there are no widely used type server
* implementations and no plans to implement one.
*
* Schemes other than `http`, `https` (or the empty scheme) might be
* used with implementation specific semantics.
*/
typeUrl: string
typeUrl: string;
/** Must be a valid serialized protocol buffer of the above specified type. */
value: Uint8Array
value: Uint8Array;
}

function createBaseAny(): Any {
return { typeUrl: '', value: new Uint8Array() }
return { typeUrl: "", value: new Uint8Array(0) };
}

export const Any = {
encode(message: Any, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.typeUrl !== '') {
writer.uint32(10).string(message.typeUrl)
if (message.typeUrl !== "") {
writer.uint32(10).string(message.typeUrl);
}
if (message.value.length !== 0) {
writer.uint32(18).bytes(message.value)
writer.uint32(18).bytes(message.value);
}
return writer
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): Any {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input)
let end = length === undefined ? reader.len : reader.pos + length
const message = createBaseAny()
const reader = input instanceof _m0.Reader ? input : _m0.Reader.create(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseAny();
while (reader.pos < end) {
const tag = reader.uint32()
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.typeUrl = reader.string()
break
if (tag !== 10) {
break;
}

message.typeUrl = reader.string();
continue;
case 2:
message.value = reader.bytes()
break
default:
reader.skipType(tag & 7)
break
if (tag !== 18) {
break;
}

message.value = reader.bytes();
continue;
}
if ((tag & 7) === 4 || tag === 0) {
break;
}
reader.skipType(tag & 7);
}
return message
return message;
},

fromJSON(object: any): Any {
return {
typeUrl: isSet(object.typeUrl) ? String(object.typeUrl) : '',
value: isSet(object.value)
? bytesFromBase64(object.value)
: new Uint8Array(),
}
typeUrl: isSet(object.typeUrl) ? globalThis.String(object.typeUrl) : "",
value: isSet(object.value) ? bytesFromBase64(object.value) : new Uint8Array(0),
};
},

toJSON(message: Any): unknown {
const obj: any = {}
message.typeUrl !== undefined && (obj.typeUrl = message.typeUrl)
message.value !== undefined &&
(obj.value = base64FromBytes(
message.value !== undefined ? message.value : new Uint8Array()
))
return obj
const obj: any = {};
if (message.typeUrl !== "") {
obj.typeUrl = message.typeUrl;
}
if (message.value.length !== 0) {
obj.value = base64FromBytes(message.value);
}
return obj;
},

create<I extends Exact<DeepPartial<Any>, I>>(base?: I): Any {
return Any.fromPartial(base ?? ({} as any));
},
fromPartial<I extends Exact<DeepPartial<Any>, I>>(object: I): Any {
const message = createBaseAny()
message.typeUrl = object.typeUrl ?? ''
message.value = object.value ?? new Uint8Array()
return message
const message = createBaseAny();
message.typeUrl = object.typeUrl ?? "";
message.value = object.value ?? new Uint8Array(0);
return message;
},
}
};

declare var self: any | undefined
declare var window: any | undefined
declare var global: any | undefined
var globalThis: any = (() => {
if (typeof globalThis !== 'undefined') return globalThis
if (typeof self !== 'undefined') return self
if (typeof window !== 'undefined') return window
if (typeof global !== 'undefined') return global
throw 'Unable to locate global object'
})()

const atob: (b64: string) => string =
globalThis.atob ||
((b64) => globalThis.Buffer.from(b64, 'base64').toString('binary'))
function bytesFromBase64(b64: string): Uint8Array {
const bin = atob(b64)
const arr = new Uint8Array(bin.length)
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i)
if ((globalThis as any).Buffer) {
return Uint8Array.from(globalThis.Buffer.from(b64, "base64"));
} else {
const bin = globalThis.atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; ++i) {
arr[i] = bin.charCodeAt(i);
}
return arr;
}
return arr
}

const btoa: (bin: string) => string =
globalThis.btoa ||
((bin) => globalThis.Buffer.from(bin, 'binary').toString('base64'))
function base64FromBytes(arr: Uint8Array): string {
const bin: string[] = []
arr.forEach((byte) => {
bin.push(String.fromCharCode(byte))
})
return btoa(bin.join(''))
if ((globalThis as any).Buffer) {
return globalThis.Buffer.from(arr).toString("base64");
} else {
const bin: string[] = [];
arr.forEach((byte) => {
bin.push(globalThis.String.fromCharCode(byte));
});
return globalThis.btoa(bin.join(""));
}
}

type Builtin =
| Date
| Function
| Uint8Array
| string
| number
| boolean
| undefined

export type DeepPartial<T> = T extends Builtin
? T
: T extends Array<infer U>
? Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U>
? ReadonlyArray<DeepPartial<U>>
: T extends {}
? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>

type KeysOfUnion<T> = T extends T ? keyof T : never
export type Exact<P, I extends P> = P extends Builtin
? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & Record<
Exclude<keyof I, KeysOfUnion<P>>,
never
>

if (_m0.util.Long !== Long) {
_m0.util.Long = Long as any
_m0.configure()
}
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;

export type DeepPartial<T> = T extends Builtin ? T
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>>
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>>
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> }
: Partial<T>;

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type Exact<P, I extends P> = P extends Builtin ? P
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never };

function isSet(value: any): boolean {
return value !== null && value !== undefined
return value !== null && value !== undefined;
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 7dd626f

Please sign in to comment.