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
7 changes: 6 additions & 1 deletion dist/fuse-worker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED = "FuseWorker does not support useTok
//#endregion
//#region src/workers/FuseWorker.ts
const DEFAULT_MAX_WORKERS = 8;
const TRUSTED_TYPES_POLICY = globalThis?.window?.trustedTypes?.createPolicy("fuse-trusted-worker-url", { createScriptURL: (url) => url });
function getDefaultWorkerCount() {
const hw = typeof navigator !== "undefined" ? navigator.hardwareConcurrency || 4 : 4;
return Math.min(hw, DEFAULT_MAX_WORKERS);
Expand All @@ -41,7 +42,11 @@ var FuseWorker = class FuseWorker {
this._workerOptions = workerOptions || {};
FuseWorker._assertNoFunctionOptions(this._options);
if (this._options.useTokenSearch) throw new Error(FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED);
this._workerUrl = this._workerOptions.workerUrl || resolveDefaultWorkerUrl();
if (this._workerOptions.workerUrl) this._workerUrl = this._workerOptions.workerUrl;
else {
const defaultWorkerUrl = resolveDefaultWorkerUrl();
this._workerUrl = TRUSTED_TYPES_POLICY ? TRUSTED_TYPES_POLICY.createScriptURL(defaultWorkerUrl.toString()) : defaultWorkerUrl;
}
}
static _assertNoFunctionOptions(options) {
if (typeof options.sortFn === "function") throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION("sortFn"));
Expand Down
17 changes: 16 additions & 1 deletion dist/fuse-worker.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,28 @@ type Expression = string | KeyedLeaf | PathLeafString | {
} | {
$or?: ChildExpression[];
};
/** https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL */
type TrustedScriptURL = {
toString(): string;
};
declare global {
interface Window {
trustedTypes: {
createPolicy: (name: string, options: {
createScriptURL: (url: string) => string;
}) => {
createScriptURL: (url: string) => TrustedScriptURL;
};
};
}
}
//#endregion
//#region src/workers/FuseWorker.d.ts
interface FuseWorkerOptions {
/** Number of parallel workers. Defaults to navigator.hardwareConcurrency (max 8). */
numWorkers?: number;
/** Custom URL to the worker script. If not provided, resolves automatically via import.meta.url. */
workerUrl?: string | URL;
workerUrl?: string | URL | TrustedScriptURL;
}
declare class FuseWorker<T> {
private _options;
Expand Down
17 changes: 16 additions & 1 deletion dist/fuse-worker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,28 @@ type Expression = string | KeyedLeaf | PathLeafString | {
} | {
$or?: ChildExpression[];
};
/** https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL */
type TrustedScriptURL = {
toString(): string;
};
declare global {
interface Window {
trustedTypes: {
createPolicy: (name: string, options: {
createScriptURL: (url: string) => string;
}) => {
createScriptURL: (url: string) => TrustedScriptURL;
};
};
}
}
//#endregion
//#region src/workers/FuseWorker.d.ts
interface FuseWorkerOptions {
/** Number of parallel workers. Defaults to navigator.hardwareConcurrency (max 8). */
numWorkers?: number;
/** Custom URL to the worker script. If not provided, resolves automatically via import.meta.url. */
workerUrl?: string | URL;
workerUrl?: string | URL | TrustedScriptURL;
}
declare class FuseWorker<T> {
private _options;
Expand Down
7 changes: 6 additions & 1 deletion dist/fuse-worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED = "FuseWorker does not support useTok
//#endregion
//#region src/workers/FuseWorker.ts
const DEFAULT_MAX_WORKERS = 8;
const TRUSTED_TYPES_POLICY = globalThis?.window?.trustedTypes?.createPolicy("fuse-trusted-worker-url", { createScriptURL: (url) => url });
function getDefaultWorkerCount() {
const hw = typeof navigator !== "undefined" ? navigator.hardwareConcurrency || 4 : 4;
return Math.min(hw, DEFAULT_MAX_WORKERS);
Expand All @@ -38,7 +39,11 @@ var FuseWorker = class FuseWorker {
this._workerOptions = workerOptions || {};
FuseWorker._assertNoFunctionOptions(this._options);
if (this._options.useTokenSearch) throw new Error(FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED);
this._workerUrl = this._workerOptions.workerUrl || resolveDefaultWorkerUrl();
if (this._workerOptions.workerUrl) this._workerUrl = this._workerOptions.workerUrl;
else {
const defaultWorkerUrl = resolveDefaultWorkerUrl();
this._workerUrl = TRUSTED_TYPES_POLICY ? TRUSTED_TYPES_POLICY.createScriptURL(defaultWorkerUrl.toString()) : defaultWorkerUrl;
}
}
static _assertNoFunctionOptions(options) {
if (typeof options.sortFn === "function") throw new Error(FUSE_WORKER_UNSUPPORTED_FN_OPTION("sortFn"));
Expand Down
15 changes: 15 additions & 0 deletions dist/fuse.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ type Expression = string | KeyedLeaf | PathLeafString | {
} | {
$or?: ChildExpression[];
};
/** https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL */
type TrustedScriptURL = {
toString(): string;
};
declare global {
interface Window {
trustedTypes: {
createPolicy: (name: string, options: {
createScriptURL: (url: string) => string;
}) => {
createScriptURL: (url: string) => TrustedScriptURL;
};
};
}
}
//#endregion
//#region src/tools/FuseIndex.d.ts
declare class FuseIndex<T = any> {
Expand Down
15 changes: 15 additions & 0 deletions dist/fuse.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ type Expression = string | KeyedLeaf | PathLeafString | {
} | {
$or?: ChildExpression[];
};
/** https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL */
type TrustedScriptURL = {
toString(): string;
};
declare global {
interface Window {
trustedTypes: {
createPolicy: (name: string, options: {
createScriptURL: (url: string) => string;
}) => {
createScriptURL: (url: string) => TrustedScriptURL;
};
};
}
}
//#endregion
//#region src/tools/FuseIndex.d.ts
declare class FuseIndex<T = any> {
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,21 @@ export type Expression =
| PathLeafString
| { $and?: ChildExpression[] }
| { $or?: ChildExpression[] }

/** https://developer.mozilla.org/en-US/docs/Web/API/TrustedScriptURL */
export type TrustedScriptURL = {
toString(): string;
}

declare global {
interface Window {
trustedTypes: {
createPolicy: (name: string, options: {
createScriptURL: (url: string) => string,
})=> {

createScriptURL: (url: string) => TrustedScriptURL,
},
}
}
}
27 changes: 23 additions & 4 deletions src/workers/FuseWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import type {
FuseOptionKey,
FuseResult,
FuseSearchOptions,
Expression
Expression,
TrustedScriptURL
} from '../types'

export interface FuseWorkerOptions {
/** Number of parallel workers. Defaults to navigator.hardwareConcurrency (max 8). */
numWorkers?: number
/** Custom URL to the worker script. If not provided, resolves automatically via import.meta.url. */
workerUrl?: string | URL
workerUrl?: string | URL | TrustedScriptURL
}

interface PendingCall {
Expand All @@ -28,6 +29,13 @@ interface Shard {

const DEFAULT_MAX_WORKERS = 8

const TRUSTED_TYPES_POLICY = globalThis?.window?.trustedTypes?.createPolicy(
'fuse-trusted-worker-url',
{
createScriptURL: (url) => url
}
)

function getDefaultWorkerCount(): number {
const hw =
typeof navigator !== 'undefined' ? navigator.hardwareConcurrency || 4 : 4
Expand Down Expand Up @@ -89,7 +97,7 @@ export default class FuseWorker<T> {
private _initPromise: Promise<void> | null = null
private _pending: Map<number, PendingCall> = new Map()
private _nextId = 0
private _workerUrl: string | URL
private _workerUrl: string | URL | TrustedScriptURL

constructor(
docs: ReadonlyArray<T>,
Expand All @@ -108,7 +116,17 @@ export default class FuseWorker<T> {
if (this._options.useTokenSearch) {
throw new Error(ErrorMsg.FUSE_WORKER_TOKEN_SEARCH_UNSUPPORTED)
}
this._workerUrl = this._workerOptions.workerUrl || resolveDefaultWorkerUrl()

if (this._workerOptions.workerUrl) {
// Don't apply the trusted types policy as workerUrl is outside our control
this._workerUrl = this._workerOptions.workerUrl
} else {
const defaultWorkerUrl = resolveDefaultWorkerUrl()

this._workerUrl = TRUSTED_TYPES_POLICY
? TRUSTED_TYPES_POLICY.createScriptURL(defaultWorkerUrl.toString())
: defaultWorkerUrl
}
}

private static _assertNoFunctionOptions<U>(options: IFuseOptions<U>): void {
Expand Down Expand Up @@ -152,6 +170,7 @@ export default class FuseWorker<T> {
}

private _spawnWorker(): Worker {
// @ts-expect-error the types for Worker are not updated yet to accept TrustedScriptURL
const worker = new Worker(this._workerUrl, { type: 'module' })

worker.onmessage = (e: MessageEvent) => {
Expand Down