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
15 changes: 12 additions & 3 deletions src/fsa-to-node/worker/FsaNodeSyncAdapterWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ let rootId = 0;

export class FsaNodeSyncAdapterWorker implements FsaNodeSyncAdapter {
public static async start(
url: string,
urlOrWorker: string | Worker,
dir: fsa.IFileSystemDirectoryHandle | Promise<fsa.IFileSystemDirectoryHandle>,
): Promise<FsaNodeSyncAdapterWorker> {
const worker = new Worker(url);
let worker: Worker;
if (typeof urlOrWorker === 'string') {
if (urlOrWorker.includes('type=module')) {
worker = new Worker(urlOrWorker, { type: 'module' });
} else {
worker = new Worker(urlOrWorker);
}
} else {
worker = urlOrWorker;
}
const future = new Defer<FsaNodeSyncAdapterWorker>();
let id = rootId++;
const id = rootId++;
let messenger: SyncMessenger | undefined = undefined;
const _dir = await dir;
worker.onmessage = e => {
Expand Down
51 changes: 51 additions & 0 deletions src/fsa-to-node/worker/__tests__/FsaNodeSyncAdapterWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { FsaNodeSyncAdapterWorker } from '../FsaNodeSyncAdapterWorker';

// Simple test to verify the core logic for worker creation
describe('FsaNodeSyncAdapterWorker', () => {
describe('worker creation logic', () => {
test('should handle module URLs correctly', () => {
const moduleUrl = '/src/worker.ts?type=module&worker_file';
const regularUrl = '/src/worker.js';

// Test the URL detection logic for module workers
expect(moduleUrl.includes('type=module')).toBe(true);
expect(regularUrl.includes('type=module')).toBe(false);
});

test('should handle type checking correctly', () => {
const stringUrl = '/src/worker.js';
const mockWorker = { postMessage: () => {} } as any;

// Test type checking logic for string vs Worker instances
expect(typeof stringUrl).toBe('string');
expect(typeof mockWorker).toBe('object');
expect(typeof stringUrl === 'string').toBe(true);
expect(typeof mockWorker === 'string').toBe(false);
});

test('should detect various module URL patterns', () => {
const patterns = [
'/src/worker.ts?type=module&worker_file',
'worker.js?type=module',
'https://example.com/worker.ts?type=module',
'blob:worker?type=module',
];

patterns.forEach(url => {
expect(url.includes('type=module')).toBe(true);
});

const nonModuleUrls = [
'/src/worker.js',
'worker.js',
'https://example.com/worker.js',
'blob:worker',
'/src/worker.ts?worker_file',
];

nonModuleUrls.forEach(url => {
expect(url.includes('type=module')).toBe(false);
});
});
});
});
Loading
Loading