From b6c80af059e6c62839574c1d73294cb9b8749855 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Sat, 1 Nov 2025 09:58:06 -0400 Subject: [PATCH] http api actually works --- graph/graph.js | 29 +++++++++-------------------- service-worker.js | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/graph/graph.js b/graph/graph.js index 58b3735..9bebd49 100644 --- a/graph/graph.js +++ b/graph/graph.js @@ -1,5 +1,4 @@ /** @import * as TYPES from "./types.ts" */ -import * as FSAccess from "../store-directory-handle.js"; /** @type {TYPES.GraphBuilder} */ export function Room({user, role, part, desk, pass}) @@ -103,22 +102,13 @@ export function Room({user, role, part, desk, pass}) { this.time = Date.now(); - const handle = await FSAccess.getDirectoryHandle(); - const path = ["store", context.Path, passID, user.id+".json"]; - let text = await FSAccess.Read(handle, path) || "{}"; - /** @type {TYPES.UserPassFile} */ - const json = JSON.parse(text); + const body = JSON.stringify({ + [partObj.id]:[this.time, data] + }) + + const http = await fetch(`./store/${context.Path}/${passID}/${user.id}.json`, {method:"PUT", body}); + console.log(http); - let field = json[partObj.id]; - if(!field) - { - field = []; - json[partObj.id] = field; - } - field.push([this.time, data]); - text = JSON.stringify(json, null, 2); - await FSAccess.Write(handle, path, text); - this.work.push(/** @type {TYPES.Work}*/([this.time, data, user])); partObj.make.forEach((arg)=>{Scan(arg, passObj)}); partObj.need.forEach((arg)=>{Scan(arg, passObj)}); @@ -137,11 +127,10 @@ export function Room({user, role, part, desk, pass}) const [userID, userObject] = userData[i]; try { - const handle = await FSAccess.getDirectoryHandle(); - const text = await FSAccess.Read(handle, ["store", context.Path, passID, userID+".json"]); + const http = await fetch(`./store/${context.Path}/${passID}/${userID}.json`); /** @type {TYPES.UserPassFile} */ - const json = JSON.parse(text); - + const json = await http.json(); + Object.entries(json).forEach(([partID, payload])=>{ let latest = 0; diff --git a/service-worker.js b/service-worker.js index d583547..9a134cc 100644 --- a/service-worker.js +++ b/service-worker.js @@ -21,15 +21,46 @@ async function Interceptor(event) const pathname = url.pathname.substring(1); let parts = pathname.split("/"); - if(parts[0] == "graph") + if(parts[0] == "graph" || parts[0] == "store") { + console.log("intercept:", pathname); const handle = await FSAccess.getDirectoryHandle(); + + if(event.request.method == "PUT") + { + console.log("put operation"); + + const payloadJSON = await event.request.clone().json(); + + let text = await FSAccess.Read(handle, parts) || "{}"; + const fileJSON = JSON.parse(text); + + for(const key in payloadJSON) + { + const value = payloadJSON[key]; + let field = fileJSON[key]; + if(!field) + { + field = []; + fileJSON[key] = field; + } + field.push(value); + } + + console.log(`incoming:`, payloadJSON); + console.log(`outgoing:`, fileJSON); + + text = JSON.stringify(fileJSON, null, 2); + const result = await FSAccess.Write(handle, parts, text); + return new Response(result+"", options); + } + const text = await FSAccess.Read(handle, parts); if(text) { - return new Response(text, options); + return new Response(text, options); } }