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
40 changes: 26 additions & 14 deletions dist/firetruss.es2015.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/firetruss.es2015.js.map

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions dist/firetruss.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/firetruss.umd.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/firetruss.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/firetruss.umd.min.js.map

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions src/Bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import _ from 'lodash';
const MIN_WORKER_VERSION = '4.0.0';


// Whether a worker at `version` satisfies `minimum`: the major versions must match precisely, and
// the minor and patch must be greater than or equal. Each component is compared as a number, since
// comparing the digits as text gets two-digit components wrong: a patch of '10' sorts below '2'.
// An unparseable version yields nothing, which the caller treats as one it can't judge rather than
// as incompatible.
export function isVersionSufficient(version, minimum) {
const worker = parseVersion(version);
const min = parseVersion(minimum);
if (!worker || !min) return;
return worker.major === min.major && (
worker.minor > min.minor || worker.minor === min.minor && worker.patch >= min.patch
);
}

function parseVersion(version) {
const parts = version.match(/^(\d+)\.(\d+)\.(\d+)(-.*)?$/);
if (!parts) return;
return {major: _.toNumber(parts[1]), minor: _.toNumber(parts[2]), patch: _.toNumber(parts[3])};
}


class Snapshot {
constructor({path, value, exists, writeSerial}) {
this._path = path;
Expand Down Expand Up @@ -67,20 +88,11 @@ export default class Bridge {
// Some browsers don't like us accessing local storage -- nothing we can do.
}
return this._send({msg: 'init', storage: items, config, lockName}).then(response => {
const workerVersion = response.version.match(/^(\d+)\.(\d+)\.(\d+)(-.*)?$/);
if (workerVersion) {
const minVersion = MIN_WORKER_VERSION.match(/^(\d+)\.(\d+)\.(\d+)(-.*)?$/);
// Major version must match precisely, minor and patch must be greater than or equal.
const sufficient = workerVersion[1] === minVersion[1] && (
workerVersion[2] > minVersion[2] ||
workerVersion[2] === minVersion[2] && workerVersion[3] >= minVersion[3]
);
if (!sufficient) {
return Promise.reject(new Error(
`Incompatible Firetruss worker version: ${response.version} ` +
`(${MIN_WORKER_VERSION} or better required)`
));
}
if (isVersionSufficient(response.version, MIN_WORKER_VERSION) === false) {
return Promise.reject(new Error(
`Incompatible Firetruss worker version: ${response.version} ` +
`(${MIN_WORKER_VERSION} or better required)`
));
}
if (response.livenessLockName) {
navigator.locks.request(response.livenessLockName, () => {
Expand Down
43 changes: 43 additions & 0 deletions src/Bridge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {test} from 'node:test';
import assert from 'node:assert/strict';
import _ from 'lodash';

import './Truss.test.setup.js';
import Bridge, {isVersionSufficient} from './Bridge.js';

// `init()` is where the worker version is checked, so drive it with a stubbed `_send` that reports
// the version under test.
function checkVersion(version) {
const bridge = _.create(Bridge.prototype, {
_send: _.constant(Promise.resolve({version})),
_shared: {}
});
return Bridge.prototype.init.call(bridge, {}, undefined)
.then(_.constant('accepted'), error => error.message);
}

// The components have to be compared as numbers. Compared as text a patch of '10' sorts below '2',
// so a worker past a two-digit boundary would be rejected against a minimum below it. `init()`
// only shows that once the compiled-in minimum has a non-zero component, so the comparison it uses
// is exercised directly against minimums the constant may not currently hold.
test('version components are compared numerically, not as text', () => {
assert.ok(isVersionSufficient('4.1.10', '4.1.2'), 'compared the patch as text');
assert.ok(isVersionSufficient('4.10.0', '4.1.2'), 'compared the minor as text');
assert.ok(isVersionSufficient('4.1.2', '4.1.2'), 'rejected an exact match');
assert.ok(!isVersionSufficient('4.1.1', '4.1.2'), 'accepted a version below the minimum');
assert.ok(!isVersionSufficient('5.0.0', '4.1.2'), 'accepted a mismatched major version');
});

test('a version below the minimum is rejected', async () => {
const rejection = await checkVersion('3.9.9');
assert.match(rejection, /^Incompatible Firetruss worker version: 3\.9\.9 /);
});

test('a mismatched major version is rejected', async () => {
assert.match(await checkVersion('5.0.0'), /^Incompatible Firetruss worker version: 5\.0\.0 /);
});

// An unparseable version is left alone rather than treated as incompatible, as before.
test('an unparseable version is not judged', async () => {
assert.equal(await checkVersion('dev'), 'accepted');
});
Loading