Skip to content
Open
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
5 changes: 5 additions & 0 deletions Change-log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

# Unreleased

- Fall back to interval-based outgoing heartbeats when a Web Worker fails, and
revoke the worker's blob URL ([#751](https://github.com/stomp-js/stompjs/issues/751)).

# 7.5.0 (2026-10-15)

- Fix [#743](https://github.com/stomp-js/stompjs/issues/743)
Expand Down
81 changes: 80 additions & 1 deletion spec/unit/ticker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { TickerStrategy } from '../../src/types.js';

class FakeWorker {
public onmessage: ((ev: { data: any }) => void) | null = null;
public onerror: ((ev: { message: string }) => void) | null = null;
public terminate = sinon.spy();
public readonly url: string;

Expand All @@ -17,6 +18,10 @@ class FakeWorker {
this.onmessage?.({ data });
}

fail(message = 'worker failed'): void {
this.onerror?.({ message });
}

static instances: FakeWorker[] = [];
static reset(): void {
FakeWorker.instances = [];
Expand All @@ -26,15 +31,23 @@ class FakeWorker {
test.describe('Ticker', () => {
let originalWorker: any;
let debug: sinon.SinonSpy;
let createObjectURL: sinon.SinonStub;
let revokeObjectURL: sinon.SinonStub;

test.beforeEach(() => {
FakeWorker.reset();
originalWorker = (globalThis as any).Worker;
(globalThis as any).Worker = FakeWorker;
debug = sinon.spy();
createObjectURL = sinon
.stub(URL, 'createObjectURL')
.returns('blob:heartbeat-worker');
revokeObjectURL = sinon.stub(URL, 'revokeObjectURL');
});

test.afterEach(() => {
createObjectURL.restore();
revokeObjectURL.restore();
if (originalWorker === undefined) {
delete (globalThis as any).Worker;
} else {
Expand Down Expand Up @@ -86,8 +99,11 @@ test.describe('Ticker', () => {

expect(FakeWorker.instances.length).toBe(1);
const worker = FakeWorker.instances[0];
expect(worker.url).toMatch(/^blob:/);
expect(worker.url).toBe('blob:heartbeat-worker');
expect(typeof worker.onmessage).toBe('function');
expect(revokeObjectURL.calledOnceWith('blob:heartbeat-worker')).toBe(
true,
);

worker.fire(123);
worker.fire(456);
Expand All @@ -100,6 +116,69 @@ test.describe('Ticker', () => {
ticker.stop();
});

test('falls back to interval when Worker construction throws', async () => {
globalThis.Worker = class {
constructor() {
throw new Error('worker construction failed');
}
} as unknown as typeof Worker;
const ticker = new Ticker(20, TickerStrategy.Worker, debug);
const tick = sinon.spy();

ticker.start(tick);
await new Promise(r => setTimeout(r, 70));

expect(tick.callCount).toBeGreaterThanOrEqual(2);
expect(
debug.calledWith('Web Worker failed, falling back to setInterval'),
).toBe(true);
expect(debug.calledWith('Using runInterval for outgoing pings')).toBe(
true,
);
expect(revokeObjectURL.calledOnceWith('blob:heartbeat-worker')).toBe(
true,
);

ticker.stop();
});

test('falls back to interval when Worker reports an error', async () => {
const ticker = new Ticker(20, TickerStrategy.Worker, debug);
const tick = sinon.spy();

ticker.start(tick);
const worker = FakeWorker.instances[0];
worker.fail();
await new Promise(r => setTimeout(r, 70));

expect(worker.terminate.calledOnce).toBe(true);
expect(tick.callCount).toBeGreaterThanOrEqual(2);
expect(
debug.calledWith('Web Worker failed, falling back to setInterval'),
).toBe(true);
expect(debug.calledWith('Using runInterval for outgoing pings')).toBe(
true,
);

ticker.stop();
});

test('ignores a Worker error after the ticker has stopped', async () => {
const ticker = new Ticker(20, TickerStrategy.Worker, debug);
const tick = sinon.spy();

ticker.start(tick);
const worker = FakeWorker.instances[0];
ticker.stop();
worker.fail();
await new Promise(r => setTimeout(r, 50));

expect(tick.callCount).toBe(0);
expect(
debug.calledWith('Web Worker failed, falling back to setInterval'),
).toBe(false);
});

test('stop terminates the worker', () => {
const ticker = new Ticker(50, TickerStrategy.Worker, debug);
ticker.start(() => {});
Expand Down
34 changes: 28 additions & 6 deletions src/ticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,35 @@ export class Ticker {

private runWorker(tick: (elapsedTime: number) => void): void {
this._debug('Using runWorker for outgoing pings');
if (!this._worker) {
this._worker = new Worker(
URL.createObjectURL(
new Blob([this._workerScript], { type: 'text/javascript' }),
),
if (this._worker) {
return;
}

let workerUrl: string | undefined;
try {
workerUrl = URL.createObjectURL(
new Blob([this._workerScript], { type: 'text/javascript' }),
);
this._worker.onmessage = message => tick(message.data);
const worker = new Worker(workerUrl);
this._worker = worker;
worker.onmessage = message => tick(message.data);
worker.onerror = () => {
// Ignore an error from a worker that has already been stopped or replaced.
if (this._worker !== worker) {
return;
}
this._debug('Web Worker failed, falling back to setInterval');
this.disposeWorker();
this.runInterval(tick);
};
} catch {
this._debug('Web Worker failed, falling back to setInterval');
this.disposeWorker();
this.runInterval(tick);
} finally {
if (workerUrl) {
URL.revokeObjectURL(workerUrl);
}
}
}

Expand Down
Loading