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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ and they are listed under *LDES 2.0 (experimental)* in every release. Pin the ve
It did not, so after as many failed requests to a domain as there are slots (10 by default), every next request to it waited
forever, and so did the stream. Since 0.5.0, a member that cannot be dereferenced gets there quickly, as its requests are retried.
([#175](https://github.com/KNowledgeOnWebScale/ldes-client/issues/175))
- The client runs in a Web Worker. The Level storage decided between the browser and Node.js on `window`, which a worker
does not have, so it used the filesystem of Node.js there and every replication failed (`(void 0) is not a function`).
It now uses IndexedDB wherever `indexedDB` exists, so a worker can also resume from a `statePath`.
([#177](https://github.com/KNowledgeOnWebScale/ldes-client/issues/177))

## 0.6.0

Expand Down
15 changes: 12 additions & 3 deletions lib/storage/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ import * as os from "os";
import * as path from "path";
import type { KVStore, Storage } from "./index";

/**
* Whether Level stores its data in IndexedDB (a browser) rather than on the filesystem (Node.js).
* This checks for `indexedDB` rather than `window`: a Web Worker is a browser environment without
* `window`, and the filesystem modules are empty there. Node.js and Deno have no `indexedDB` by default.
*/
function usesIndexedDB(): boolean {
return typeof indexedDB !== "undefined";
}

/**
* Structural interface for the Level sublevel methods we actually use.
* Level sublevels always use string keys, so this reflects that reality.
Expand Down Expand Up @@ -91,7 +100,7 @@ export class LevelStorage implements Storage {

private getDefaultLocation(): string {
const id = `${Date.now()}-${Math.random().toString(36).substring(2, 10)}`;
if (typeof window !== "undefined") {
if (usesIndexedDB()) {
return `ldes-client-${id}`;
} else {
return path.join(os.tmpdir(), `ldes-client-${id}`);
Expand Down Expand Up @@ -130,7 +139,7 @@ export class LevelStorage implements Storage {
}

private clearFiles(): void {
if (typeof window === "undefined") {
if (!usesIndexedDB()) {
if (fs.existsSync(this.location)) {
fs.readdirSync(this.location).forEach((file) => {
fs.rmSync(path.join(this.location, file), {
Expand All @@ -146,7 +155,7 @@ export class LevelStorage implements Storage {
}

private ensureStatePath(p: string): void {
if (typeof window === "undefined") {
if (!usesIndexedDB()) {
if (!fs.existsSync(p)) {
fs.mkdirSync(p, { recursive: true });
}
Expand Down
29 changes: 29 additions & 0 deletions tests/browser/worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { describe, expect, test } from "vitest";

// Note: The globalSetup runs the server on port 3042
const LDES = "http://localhost:3042/mock-ldes.ttl";

type Result = { count?: number; error?: string };

function replicateInWorker(statePath?: string): Promise<Result> {
const worker = new Worker(new URL("./worker/replicate.worker.ts", import.meta.url), { type: "module" });
return new Promise<Result>((resolve, reject) => {
worker.onmessage = (event: MessageEvent<Result>) => resolve(event.data);
worker.onerror = (event) => reject(new Error(event.message));
worker.postMessage({ url: LDES, statePath });
}).finally(() => worker.terminate());
}

describe("Client in a Web Worker (Browser)", () => {
test("replicates without a statePath", async () => {
const result = await replicateInWorker();
expect(result.error).toBeUndefined();
expect(result.count).toBeGreaterThan(0);
});

test("replicates with a statePath", async () => {
const result = await replicateInWorker(`ldes-worker-${Date.now()}-${Math.random()}`);
expect(result.error).toBeUndefined();
expect(result.count).toBeGreaterThan(0);
});
});
22 changes: 22 additions & 0 deletions tests/browser/worker/replicate.worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Runs a replication in a Web Worker: a browser environment without `window`.
* See https://github.com/KNowledgeOnWebScale/ldes-client/issues/177
*/
import "../setup";
import { replicateLDES } from "../../../lib/client";

type Request = { url: string; statePath?: string };

self.onmessage = async (event: MessageEvent<Request>) => {
try {
const client = replicateLDES({ url: event.data.url, statePath: event.data.statePath });
let count = 0;
for await (const member of client.stream()) {
if (member.id) count += 1;
}
await client.close();
self.postMessage({ count });
} catch (error) {
self.postMessage({ error: error instanceof Error ? (error.stack ?? error.message) : String(error) });
}
};
Loading