Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ jobs:

strategy:
matrix:
node-version: [21.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

node-version: [22, 24]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
46 changes: 24 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{
"name": "@garmin/fitsdk",
"version": "21.202.0",
"description": "FIT JavaScript SDK",
"main": "src/index.js",
"type": "module",
"scripts": {
"build": "node .",
"test": "vitest run"
},
"author": "Garmin International, Inc.",
"license": "SEE LICENSE IN LICENSE.txt",
"repository": {
"type": "git",
"url": "https://github.com/garmin/fit-javascript-sdk"
},
"files": [
"src/"
],
"devDependencies": {
"vitest": "^2.1.8"
}
}
"name": "@garmin/fitsdk",
"version": "21.205.0",
"description": "FIT JavaScript SDK",
"main": "src/index.js",
"types": "src/index.d.ts",
"type": "module",
"scripts": {
"test": "vitest run --silent=passed-only --typecheck"
},
"author": "Garmin International, Inc.",
"license": "SEE LICENSE IN LICENSE.txt",
"repository": {
"type": "git",
"url": "https://github.com/garmin/fit-javascript-sdk"
},
"files": [
"src/"
],
"devDependencies": {
"@types/node": "^22.0.0",
"typescript": "^5.0.0",
"vitest": "^4.1.5"
}
}
4 changes: 2 additions & 2 deletions src/accumulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.202.0Release
// Tag = production/release/21.202.0-0-g9a57aebc
// Profile Version = 21.205.0Release
// Tag = production/release/21.205.0-0-gb3c261eb
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
65 changes: 56 additions & 9 deletions src/bit-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,28 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.202.0Release
// Tag = production/release/21.202.0-0-g9a57aebc
// Profile Version = 21.205.0Release
// Tag = production/release/21.205.0-0-gb3c261eb
/////////////////////////////////////////////////////////////////////////////////////////////


import FIT from "./fit.js";

class BitStream {
#data = null;
#array = null;
#currentArrayPosition = 0;
#bitPerPosition = 0;
#isBigInt = false;
#currentByte = 0;
#currentBit = 0;
#bitsAvailable = 0;

constructor(data, baseType = FIT.BaseType.UINT8) {
this.#array = Array.isArray(data) ? data : [data];
this.#data = Array.isArray(data) ? data : [data];
const baseTypeSize = FIT.BaseTypeDefinitions[baseType].size;
this.#bitPerPosition = baseTypeSize * 8;
this.#isBigInt = this.#bitPerPosition > 32;
this.reset();
}

Expand All @@ -36,33 +39,73 @@ class BitStream {
}

reset() {
this.#array = null;
this.#currentArrayPosition = 0;
this.#bitsAvailable = this.#bitPerPosition * this.#array.length;
this.#nextByte();
this.#bitsAvailable = this.#bitPerPosition * this.#data.length;
this.#currentByte = 0;
this.#currentBit = 0;
}

readBit() {
if (!this.hasBitsAvailable) {
this.#throwError();
}

if (this.#array === null) {
this.#array = this.#data;
this.#currentArrayPosition = this.#data.length - (this.#bitsAvailable / this.#bitPerPosition);
this.#nextByte();
}

if (this.#currentBit >= this.#bitPerPosition) {
this.#nextByte();
}

const bit = this.#currentByte & 0x01;
this.#currentByte = this.#currentByte >> 1;
let bit;
if (this.#isBigInt) {
bit = Number(this.#currentByte & 1n);
this.#currentByte >>= 1n;
} else {
bit = this.#currentByte & 0x01;
this.#currentByte >>= 1;
}
this.#currentBit++;
this.#bitsAvailable--;

return bit;
}

readBits(nBitsToRead) {
// Fast path: reading exactly one value's worth of bits and #array not yet initialized
if (nBitsToRead === this.#bitPerPosition && this.#array === null) {
if (!this.hasBitsAvailable) {
this.#throwError();
}
const index = this.#data.length - (this.#bitsAvailable / this.#bitPerPosition);
const value = this.#data[index];
this.#bitsAvailable -= nBitsToRead;

if (this.#isBigInt) {
return Number(BigInt.asUintN(nBitsToRead, value));
}
if (nBitsToRead < 32) {
return (value & ((1 << nBitsToRead) - 1)) >>> 0;
}
return value >>> 0;
}

if (nBitsToRead <= 32) {
let value = 0;
for (let i = 0; i < nBitsToRead; i++) {
value |= this.readBit() << i;
}
return value >>> 0;
}

let value = 0n;

for (let i = 0n; i < nBitsToRead; i++) {
value |= BigInt(this.readBit()) << i;
for (let i = 0, shift = 0n; i < nBitsToRead; i++, shift++) {
value |= BigInt(this.readBit()) << shift;
}

return Number(value);
Expand All @@ -77,6 +120,10 @@ class BitStream {
this.#currentBit = 0;
}

get array() {
return this.#array;
}

#throwError(error = "") {
throw Error("FIT Runtime Error no bits available.");
}
Expand Down
4 changes: 2 additions & 2 deletions src/crc-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
// Transfer (FIT) Protocol License.
/////////////////////////////////////////////////////////////////////////////////////////////
// ****WARNING**** This file is auto-generated! Do NOT edit this file.
// Profile Version = 21.202.0Release
// Tag = production/release/21.202.0-0-g9a57aebc
// Profile Version = 21.205.0Release
// Tag = production/release/21.205.0-0-gb3c261eb
/////////////////////////////////////////////////////////////////////////////////////////////


Expand Down
Loading
Loading