MCPcopy Create free account
hub / github.com/Dhravya/notty / fetch

Method fetch

src/durable-objects/user-notes.ts:422–906  ·  view source on GitHub ↗
(request: Request)

Source from the content-addressed store, hash-verified

420
421 private contentHash(content: string): string {
422 let hash = 5381;
423 for (let i = 0; i < content.length; i++) {
424 hash = ((hash << 5) + hash) ^ content.charCodeAt(i);
425 }
426 return (hash >>> 0).toString(36);
427 }
428
429 private beginEditSession(noteId: string): { sessionId: string } {
430 const note = this.getNote(noteId);
431 const branch = this.getCurrentBranch(noteId);
432 const sessionId = crypto.randomUUID();
433 this.sql.exec(
434 `INSERT INTO edit_sessions (id, note_id, branch_id, base_content, base_version_id)
435 VALUES (?, ?, ?, ?, ?)`,
436 sessionId, noteId, branch.id, (note?.content as string) || "", branch.head_version_id
437 );
438 return { sessionId };
439 }
440
441 private async finalizeEditSession(noteId: string, sessionId: string, reason = "finalize"): Promise<{ versionId?: string }> {
442 const session = this.sql.exec(
443 "SELECT id, base_content FROM edit_sessions WHERE id = ? AND note_id = ? AND finalized_at IS NULL",
444 sessionId, noteId
445 ).toArray()[0] as any;
446 if (!session) return {};
447
448 const note = this.getNote(noteId);
449 const currentContent = (note?.content as string) || "";
450 this.sql.exec("UPDATE edit_sessions SET finalized_at = unixepoch(), last_edit_at = unixepoch() WHERE id = ?", sessionId);
451 if (!note || currentContent === (session.base_content || "")) return {};
452
453 const versionId = await this.createVersion(
454 noteId,
455 deriveTitleAndPreview(currentContent).title || (note.title as string) || "Untitled",
456 currentContent,
457 "session",
458 "session",
459 `Editing session (${reason})`
460 ) || undefined;
461 this.sql.exec(
462 "UPDATE notes SET pending_index = 1 WHERE id = ?",
463 noteId
464 );
465 return { versionId };
466 }
467
468 // strict: throw on R2 read errors instead of degrading to "" — used where
469 // the caller must distinguish "genuinely empty" from "couldn't read".
470 private async reconstructVersion(versionId: string, noteId: string, opts?: { strict?: boolean }): Promise<string> {
471 // New-style versions are full snapshots in R2
472 const head = this.sql.exec(
473 "SELECT storage, data FROM note_versions WHERE id = ?", versionId
474 ).toArray()[0] as any;
475 if (head?.storage === "r2") {
476 try {
477 const obj = await this.bucket.get(head.data);
478 if (obj) return await obj.text();
479 } catch (e) {

Callers

nothing calls this directly

Calls 15

getYDocMethod · 0.95
getAllNotesMethod · 0.95
getNotesListMethod · 0.95
getTrashNotesMethod · 0.95
getNoteMethod · 0.95
broadcastJsonMethod · 0.95
extractContentJsonMethod · 0.95
beginEditSessionMethod · 0.95
finalizeEditSessionMethod · 0.95
contentHashMethod · 0.95
ensureDefaultBranchMethod · 0.95
getCurrentBranchMethod · 0.95

Tested by

no test coverage detected