Skip to content

Commit

Permalink
Merge pull request #195 from powersync-ja/feat/es2024
Browse files Browse the repository at this point in the history
Upgrade typescript and target ES version
  • Loading branch information
simolus3 authored Feb 6, 2025
2 parents e26e434 + 9a9e668 commit 7f9bb19
Show file tree
Hide file tree
Showing 13 changed files with 147 additions and 71 deletions.
6 changes: 6 additions & 0 deletions .changeset/twenty-jeans-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@powersync/service-module-postgres-storage': minor
'@powersync/service-core': minor
---

Target Node.JS version 22, ES2024
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.12.2
v22.13.1
2 changes: 1 addition & 1 deletion modules/module-postgres-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@
},
"devDependencies": {
"@types/uuid": "^9.0.4",
"typescript": "^5.2.2"
"typescript": "^5.7.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,8 @@ export class PostgresBucketBatch

let existingBuckets: CurrentBucket[] = [];
let newBuckets: CurrentBucket[] = [];
let existingLookups: Buffer[] = [];
let newLookups: Buffer[] = [];
let existingLookups: Buffer<ArrayBuffer>[] = [];
let newLookups: Buffer<ArrayBuffer>[] = [];

if (this.options.skip_existing_rows) {
if (record.tag == storage.SaveOperationTag.INSERT) {
Expand Down Expand Up @@ -695,7 +695,7 @@ export class PostgresBucketBatch
}
}

let afterData: Buffer | undefined;
let afterData: Buffer<ArrayBuffer> | undefined;
if (afterId != null && !this.options.store_current_data) {
afterData = storage.serializeBson({});
} else if (afterId != null) {
Expand Down
2 changes: 0 additions & 2 deletions modules/module-postgres-storage/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"declarationDir": "dist/@types",
"tsBuildInfoFile": "dist/.tsbuildinfo",
"rootDir": "src",
"target": "ES2022",
"lib": ["ES2022", "esnext.disposable"],
"skipLibCheck": true
},
"include": ["src"],
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"devDependencies": {
"@changesets/cli": "^2.27.8",
"@types/node": "^22.5.5",
"@types/node": "^22.13.1",
"async": "^3.2.4",
"bson": "^6.8.0",
"concurrently": "^8.2.2",
Expand All @@ -36,7 +36,7 @@
"semver": "^7.5.4",
"tsc-watch": "^6.2.0",
"ts-node-dev": "^2.0.0",
"typescript": "^5.6.2",
"typescript": "^5.7.3",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^2.1.1",
"ws": "^8.2.3"
Expand Down
2 changes: 1 addition & 1 deletion packages/service-core-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"vitest": "^2.1.1"
},
"devDependencies": {
"typescript": "^5.6.2"
"typescript": "^5.7.3"
}
}
16 changes: 9 additions & 7 deletions packages/service-core/src/storage/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as bson from 'bson';
import { SqliteJsonValue } from '@powersync/service-sync-rules';
import { ReplicaId } from './BucketStorage.js';

type NodeBuffer = Buffer<ArrayBuffer>;

export const BSON_DESERIALIZE_OPTIONS: bson.DeserializeOptions = {
// use bigint instead of Long
useBigInt64: true
Expand All @@ -12,15 +14,15 @@ export const BSON_DESERIALIZE_OPTIONS: bson.DeserializeOptions = {
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
* @param lookup
*/
export const serializeLookupBuffer = (lookup: SqliteJsonValue[]): Buffer => {
export const serializeLookupBuffer = (lookup: SqliteJsonValue[]): NodeBuffer => {
const normalized = lookup.map((value) => {
if (typeof value == 'number' && Number.isInteger(value)) {
return BigInt(value);
} else {
return value;
}
});
return bson.serialize({ l: normalized }) as Buffer;
return bson.serialize({ l: normalized }) as NodeBuffer;
};

export const serializeLookup = (lookup: SqliteJsonValue[]) => {
Expand All @@ -40,8 +42,8 @@ export const isUUID = (value: any): value is bson.UUID => {
return uuid._bsontype == 'Binary' && uuid.sub_type == bson.Binary.SUBTYPE_UUID;
};

export const serializeReplicaId = (id: ReplicaId): Buffer => {
return bson.serialize({ id }) as Buffer;
export const serializeReplicaId = (id: ReplicaId): NodeBuffer => {
return bson.serialize({ id }) as NodeBuffer;
};

export const deserializeReplicaId = (id: Buffer): ReplicaId => {
Expand All @@ -53,8 +55,8 @@ export const deserializeBson = (buffer: Buffer) => {
return bson.deserialize(buffer, BSON_DESERIALIZE_OPTIONS);
};

export const serializeBson = (document: any): Buffer => {
return bson.serialize(document) as Buffer;
export const serializeBson = (document: any): NodeBuffer => {
return bson.serialize(document) as NodeBuffer;
};

/**
Expand All @@ -73,6 +75,6 @@ export const replicaIdEquals = (a: ReplicaId, b: ReplicaId) => {
return false;
} else {
// There are many possible primitive values, this covers them all
return serializeReplicaId(a).equals(serializeReplicaId(b) as ArrayBuffer as Uint8Array);
return serializeReplicaId(a).equals(serializeReplicaId(b));
}
};
4 changes: 2 additions & 2 deletions packages/service-core/src/util/memory-tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export function trackMemoryUsage() {
for (let key of Object.keys(bufferMemory)) {
const typedKey = key as keyof typeof bufferMemory;
const originalFunction = Buffer[typedKey] as (...args: any[]) => Buffer;
Buffer[typedKey] = function (...args: any[]) {
const buffer = originalFunction.apply(this, args);
Buffer[typedKey] = function <TArrayBuffer extends ArrayBufferLike = ArrayBufferLike>(...args: any[]) {
const buffer = originalFunction.apply(this, args) as Buffer<TArrayBuffer>;
bufferMemory[typedKey] += buffer.byteLength;
bufferRegistry.register(buffer, [typedKey, buffer.byteLength]);
return buffer;
Expand Down
Loading

0 comments on commit 7f9bb19

Please # to comment.