Skip to content

Commit

Permalink
feat: WASM fixes and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodolsky committed Jul 22, 2023
1 parent 5f20e3f commit 5c1f1ab
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 73 deletions.
3 changes: 1 addition & 2 deletions docs/_includes/quick-start-snippet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/attach-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<index_name>` folder and then do
`summa-cli 0.0.0.0:82 attach-index <index_name> '{"file": {}}'`
`summa-cli 0.0.0.0:8082 attach-index <index_name> '{"file": {}}'`

### Remote File Indices
You may also attach any index available through HTTP:
Expand Down
2 changes: 1 addition & 1 deletion summa-core/src/components/index_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl IndexHolder {
#[cfg(feature = "fs")]
pub async fn create_file_index(index_path: &Path, index_builder: IndexBuilder) -> SummaResult<Index> {
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)?;
Expand Down
2 changes: 1 addition & 1 deletion summa-wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "summa-wasm"
version = "0.125.2"
version = "0.125.10"
authors = ["Pasha Podolsky <ppodolsky@me.com>"]
edition = "2021"
license-file = "LICENSE"
Expand Down
2 changes: 1 addition & 1 deletion summa-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "summa-wasm",
"description": "WASM-bindings for Summa",
"version": "0.125.2",
"version": "0.125.10",
"keywords": [
"search",
"database",
Expand Down
65 changes: 3 additions & 62 deletions summa-wasm/src/seeds.ts
Original file line number Diff line number Diff line change
@@ -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<summa.proto.RemoteEngineConfig>;
Expand All @@ -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;
}
Expand All @@ -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<summa.proto.RemoteEngineConfig> {
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,
});
}
}
}
11 changes: 9 additions & 2 deletions summa-wasm/src/service-worker-register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
Expand All @@ -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", () => {
Expand Down
9 changes: 6 additions & 3 deletions summa-wasm/src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand All @@ -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'
Expand All @@ -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;
Expand Down

0 comments on commit 5c1f1ab

Please sign in to comment.