Skip to content

Commit

Permalink
Update target to ES2020 and replace Long with bigint (#912)
Browse files Browse the repository at this point in the history
This commit Streamlines data type handling by replacing Long with native.
It also updates target to ECMAScript 2020.

Despite the introduction of bigint, but Long still remains in Counter. We
need to remove Long.
  • Loading branch information
hackerwins authored and JOOHOJANG committed Oct 22, 2024
1 parent 6e98529 commit 6e7e344
Show file tree
Hide file tree
Showing 18 changed files with 3,663 additions and 1,070 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"only-allow": "^1.2.1",
"eslint-plugin-jsdoc": "^39.3.3",
"eslint-plugin-prettier": "^5.0.0"
}
},
"packageManager": "[email protected]+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1"
}
8 changes: 4 additions & 4 deletions packages/sdk/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ plugins:
- plugin: es
out: .
opt:
- target=js+dts
- js_import_style=module
- target=ts
- import_extension=none
- plugin: connect-es
out: .
opt:
- target=js+dts
- js_import_style=module
- target=ts
- import_extension=none
27 changes: 10 additions & 17 deletions packages/sdk/src/api/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import Long from 'long';
import { ConnectError } from '@connectrpc/connect';
import { ErrorInfo } from '@buf/googleapis_googleapis.bufbuild_es/google/rpc/error_details_pb';
import { Code, YorkieError } from '@yorkie-js-sdk/src/util/error';
Expand Down Expand Up @@ -151,7 +150,7 @@ function toPresenceChange(
*/
function toCheckpoint(checkpoint: Checkpoint): PbCheckpoint {
return new PbCheckpoint({
serverSeq: checkpoint.getServerSeqAsString(),
serverSeq: checkpoint.getServerSeq(),
clientSeq: checkpoint.getClientSeq(),
});
}
Expand All @@ -162,7 +161,7 @@ function toCheckpoint(checkpoint: Checkpoint): PbCheckpoint {
function toChangeID(changeID: ChangeID): PbChangeID {
return new PbChangeID({
clientSeq: changeID.getClientSeq(),
lamport: changeID.getLamportAsString(),
lamport: changeID.getLamport(),
actorId: toUint8Array(changeID.getActorID()),
versionVector: toVersionVector(changeID.getVersionVector()),
});
Expand All @@ -177,7 +176,7 @@ function toTimeTicket(ticket?: TimeTicket): PbTimeTicket | undefined {
}

return new PbTimeTicket({
lamport: ticket.getLamportAsString(),
lamport: ticket.getLamport(),
delimiter: ticket.getDelimiter(),
actorId: toUint8Array(ticket.getActorID()),
});
Expand Down Expand Up @@ -825,17 +824,14 @@ export function errorCodeOf(error: ConnectError): string {
* `fromChangeID` converts the given Protobuf format to model format.
*/
function fromChangeID(pbChangeID: PbChangeID): ChangeID {
let serverSeq: Long | undefined;
if (pbChangeID.serverSeq) {
serverSeq = Long.fromString(pbChangeID.serverSeq, true);
}

// TODO(hackerwins): Remove BigInt conversion. Some of the bigint values are
// passed as string in the protobuf. We should fix this in the future.
return ChangeID.of(
pbChangeID.clientSeq,
Long.fromString(pbChangeID.lamport, true),
BigInt(pbChangeID.lamport),
toHexString(pbChangeID.actorId),
fromVersionVector(pbChangeID.versionVector)!,
serverSeq,
BigInt(pbChangeID.serverSeq),
);
}

Expand All @@ -852,7 +848,7 @@ function fromVersionVector(
const vector = new VersionVector();
Object.entries(pbVersionVector.vector).forEach(([key, value]) => {
// TODO(hackerwins): Remove Long after introducing BigInt.
vector.set(key, Long.fromString(value.toString(), true));
vector.set(key, BigInt(value.toString()));
});
return vector;
}
Expand All @@ -866,7 +862,7 @@ function fromTimeTicket(pbTimeTicket?: PbTimeTicket): TimeTicket | undefined {
}

return TimeTicket.of(
Long.fromString(pbTimeTicket.lamport, true),
BigInt(pbTimeTicket.lamport),
pbTimeTicket.delimiter,
toHexString(pbTimeTicket.actorId),
);
Expand Down Expand Up @@ -1353,10 +1349,7 @@ function fromChanges<P extends Indexable>(
* `fromCheckpoint` converts the given Protobuf format to model format.
*/
function fromCheckpoint(pbCheckpoint: PbCheckpoint): Checkpoint {
return Checkpoint.of(
Long.fromString(pbCheckpoint.serverSeq, true),
pbCheckpoint.clientSeq,
);
return Checkpoint.of(BigInt(pbCheckpoint.serverSeq), pbCheckpoint.clientSeq);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions packages/sdk/src/api/yorkie/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ message Change {

message ChangeID {
uint32 client_seq = 1;
int64 server_seq = 2 [jstype = JS_STRING];
int64 lamport = 3 [jstype = JS_STRING];
int64 server_seq = 2;
int64 lamport = 3;
bytes actor_id = 4;
VersionVector version_vector = 5;
}
Expand Down Expand Up @@ -142,6 +142,12 @@ message Operation {
repeated string attributes_to_remove = 6;
map<string, TimeTicket> created_at_map_by_actor = 7;
}
message ArraySet {
TimeTicket parent_created_at = 1;
TimeTicket created_at = 2;
JSONElementSimple value = 3;
TimeTicket executed_at = 4;
}

oneof body {
Set set = 1;
Expand All @@ -154,6 +160,7 @@ message Operation {
Increase increase = 8;
TreeEdit tree_edit = 9;
TreeStyle tree_style = 10;
ArraySet array_set = 11;
}
}

Expand Down Expand Up @@ -331,7 +338,7 @@ message Presence {
}

message Checkpoint {
int64 server_seq = 1 [jstype = JS_STRING];
int64 server_seq = 1;
uint32 client_seq = 2;
}

Expand All @@ -342,7 +349,7 @@ message TextNodePos {
}

message TimeTicket {
int64 lamport = 1 [jstype = JS_STRING];
int64 lamport = 1;
uint32 delimiter = 2;
bytes actor_id = 3;
}
Expand Down
Loading

0 comments on commit 6e7e344

Please sign in to comment.