From 5c1f1ab15fafc37c0adbcc859be95f7ee44328c5 Mon Sep 17 00:00:00 2001 From: pasha Date: Sat, 22 Jul 2023 19:11:21 +0300 Subject: [PATCH] feat: WASM fixes and documentation --- docs/_includes/quick-start-snippet.md | 3 +- docs/guides/attach-index.md | 2 +- summa-core/src/components/index_holder.rs | 2 +- summa-wasm/Cargo.toml | 2 +- summa-wasm/package.json | 2 +- summa-wasm/src/seeds.ts | 65 ++--------------------- summa-wasm/src/service-worker-register.ts | 11 +++- summa-wasm/src/service-worker.ts | 9 ++-- 8 files changed, 23 insertions(+), 73 deletions(-) diff --git a/docs/_includes/quick-start-snippet.md b/docs/_includes/quick-start-snippet.md index e26952b5..7ca68a53 100644 --- a/docs/_includes/quick-start-snippet.md +++ b/docs/_includes/quick-start-snippet.md @@ -29,8 +29,7 @@ docker run izihawa/summa-server:testing generate-config -d /data \ -a 0.0.0.0:8082 > summa.yaml # Launch `summa-server` -docker run -v $(pwd)/summa.yaml:/summa.yaml -v $(pwd)/data:/data \ --p 8082:8082 -p 8080:8080 -p 4444:4444 -p 4445:4445 \ +docker run -v $(pwd)/summa.yaml:/summa.yaml -v $(pwd)/data:/data -p 8082:8082 \ izihawa/summa-server:testing serve /summa.yaml ``` diff --git a/docs/guides/attach-index.md b/docs/guides/attach-index.md index d9c93ee6..cd75ffb9 100644 --- a/docs/guides/attach-index.md +++ b/docs/guides/attach-index.md @@ -10,7 +10,7 @@ First, you should set up Summa Server and create a test index using our [Quick-S ### Local File Indices Put downloaded directory with index files to `data/bin/` folder and then do -`summa-cli 0.0.0.0:82 attach-index '{"file": {}}'` +`summa-cli 0.0.0.0:8082 attach-index '{"file": {}}'` ### Remote File Indices You may also attach any index available through HTTP: diff --git a/summa-core/src/components/index_holder.rs b/summa-core/src/components/index_holder.rs index 015f5070..026d636a 100644 --- a/summa-core/src/components/index_holder.rs +++ b/summa-core/src/components/index_holder.rs @@ -234,7 +234,7 @@ impl IndexHolder { #[cfg(feature = "fs")] pub async fn create_file_index(index_path: &Path, index_builder: IndexBuilder) -> SummaResult { if index_path.exists() { - return Err(crate::errors::ValidationError::ExistingPath(index_path.to_owned()).into()); + return Err(ValidationError::ExistingPath(index_path.to_owned()).into()); } tokio::fs::create_dir_all(index_path).await?; let index = index_builder.create_in_dir(index_path)?; diff --git a/summa-wasm/Cargo.toml b/summa-wasm/Cargo.toml index d374c446..f2642733 100644 --- a/summa-wasm/Cargo.toml +++ b/summa-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "summa-wasm" -version = "0.125.2" +version = "0.125.10" authors = ["Pasha Podolsky "] edition = "2021" license-file = "LICENSE" diff --git a/summa-wasm/package.json b/summa-wasm/package.json index 57ae9b11..fedfd41c 100644 --- a/summa-wasm/package.json +++ b/summa-wasm/package.json @@ -1,7 +1,7 @@ { "name": "summa-wasm", "description": "WASM-bindings for Summa", - "version": "0.125.2", + "version": "0.125.10", "keywords": [ "search", "database", diff --git a/summa-wasm/src/seeds.ts b/summa-wasm/src/seeds.ts index 592bc0e3..0e3e0bdf 100644 --- a/summa-wasm/src/seeds.ts +++ b/summa-wasm/src/seeds.ts @@ -1,6 +1,4 @@ import { summa } from "./proto"; -import { get_ipfs_hostname, get_ipfs_url } from "./utils"; -import axios from "axios"; export interface IIndexSeed { retrieve_remote_engine_config(): Promise; @@ -14,6 +12,9 @@ export class LocalDatabaseSeed implements IIndexSeed { if (!ipfs_path.endsWith("/")) { ipfs_path += "/"; } + if (!ipfs_path.startsWith("/")) { + ipfs_path = "/" + ipfs_path; + } this.ipfs_path = ipfs_path; this.cache_config = cache_config; } @@ -27,63 +28,3 @@ export class LocalDatabaseSeed implements IIndexSeed { }); } } - -export class IpfsDatabaseSeed implements IIndexSeed { - ipfs_path: string; - cache_config: summa.proto.CacheConfig; - ipfs_url?: string; - - constructor(ipfs_path: string, cache_config: summa.proto.CacheConfig, ipfs_url?: string) { - this.ipfs_path = ipfs_path; - this.cache_config = cache_config; - this.ipfs_url = ipfs_url; - } - - async retrieve_remote_engine_config(): Promise { - const ipfs_url = this.ipfs_url || get_ipfs_url(); - const { ipfs_hostname, ipfs_http_protocol } = get_ipfs_hostname(ipfs_url) - const response = await axios.get(ipfs_url + this.ipfs_path); - let ipfs_hash = response.headers["x-ipfs-roots"]; - if ( - ipfs_hash === undefined && - response.headers["content-type"] === "text/html" - ) { - const parser = new DOMParser(); - const htmlDoc = parser.parseFromString(response.data, "text/html"); - if (htmlDoc.getElementsByClassName("ipfs-hash").length > 0) { - // Kubo - ipfs_hash = htmlDoc - .getElementsByClassName("ipfs-hash")[0] - .textContent!.trim(); - } else { - // Iroh - ipfs_hash = htmlDoc - .getElementsByTagName("title")[0] - .textContent!.replace("/ipfs/", "") - .trim(); - if (ipfs_hash.endsWith("/")) { - ipfs_hash = ipfs_hash.substring(0, ipfs_hash.length - 1); - } - } - } - try { - // ToDo: Create separate check function - await axios.get( - `${ipfs_http_protocol}//${ipfs_hash}.ipfs.${ipfs_hostname}/meta.json` - ); - return summa.proto.RemoteEngineConfig.create({ - method: "GET", - url_template: `${ipfs_http_protocol}//${ipfs_hash}.ipfs.${ipfs_hostname}/{file_name}`, - headers_template: { range: "bytes={start}-{end}" }, - cache_config: this.cache_config, - }); - } catch { - return summa.proto.RemoteEngineConfig.create({ - method: "GET", - url_template: `${ipfs_http_protocol}//${ipfs_hostname}/ipfs/${ipfs_hash}/{file_name}`, - headers_template: { range: "bytes={start}-{end}" }, - cache_config: this.cache_config, - }); - } - } -} diff --git a/summa-wasm/src/service-worker-register.ts b/summa-wasm/src/service-worker-register.ts index 0a4c8a3b..84841090 100644 --- a/summa-wasm/src/service-worker-register.ts +++ b/summa-wasm/src/service-worker-register.ts @@ -6,8 +6,15 @@ doReload: () => window.location.reload(), }; - const n = navigator; + console.info("SW Environment Data:", { + 'service_worker': n.serviceWorker, + 'is_secure_context': window.isSecureContext, + 'cross_origin_isolated': window.crossOriginIsolated, + 'should_register': coi.shouldRegister() + }); + + if (coi.shouldDeregister() && n.serviceWorker && n.serviceWorker.controller) { n.serviceWorker.controller.postMessage({ type: "deregister" }); } @@ -23,7 +30,7 @@ // In some environments (e.g. Chrome incognito mode) this won't be available if (n.serviceWorker) { n.serviceWorker - .register("/service-worker.js") + .register(window.location.pathname.replace(/\/$/, "") + "/service-worker.js", {scope: window.location.pathname}) .then( (registration) => { registration.addEventListener("updatefound", () => { diff --git a/summa-wasm/src/service-worker.ts b/summa-wasm/src/service-worker.ts index 0e72a22f..9177337b 100644 --- a/summa-wasm/src/service-worker.ts +++ b/summa-wasm/src/service-worker.ts @@ -65,6 +65,7 @@ async function handle_request(event: FetchEvent) { const request = event.request let filename = request.url; let url = request.url; + let is_development = (new URL(request.url)).host == "localhost:5173" let is_immutable_file = filename.endsWith(".fast") || filename.endsWith(".term") || @@ -74,12 +75,14 @@ async function handle_request(event: FetchEvent) { filename.endsWith(".idx") || filename.endsWith(".del") || filename.endsWith(".wasm") || - filename.endsWith(".bin") || + filename.endsWith(".bin"); + + let is_view_file = (filename.endsWith(".json") && !filename.endsWith("meta.json")) || event.request.destination === "image" || event.request.destination === "font" || event.request.destination === "style" || - (event.request.destination === "script" && !request.url.startsWith("chrome-extension")); + (event.request.destination === "script" && !request.url.startsWith("chrome-extension")) let crop_after: boolean = filename.endsWith(".del"); let range_start = '0' @@ -101,7 +104,7 @@ async function handle_request(event: FetchEvent) { } } - let caching_enabled = is_immutable_file && request.method === "GET"; + let caching_enabled = (is_immutable_file || (is_view_file && !is_development)) && request.method === "GET"; const cache = await caches.open("cache_v2"); let response = undefined;