Skip to content

Commit

Permalink
Add /$/diff/N..N/ special folder for to expose drive.diff() API
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmturner committed Aug 14, 2023
1 parent fdc0bb1 commit 1146b92
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const EXTENSION_EVENT = 'extension-message'
const VERSION_FOLDER_NAME = 'version'
const PEER_OPEN = 'peer-open'
const PEER_REMOVE = 'peer-remove'
const DIFF_FOLDER_NAME = 'diff'

const MIME_TEXT_PLAIN = 'text/plain; charset=utf-8'
const MIME_APPLICATION_JSON = 'application/json'
Expand Down Expand Up @@ -103,6 +104,7 @@ export default async function makeHyperFetch ({

router.head(`hyper://*/${SPECIAL_FOLDER}/${VERSION_FOLDER_NAME}/**`, headFilesVersioned)
router.get(`hyper://*/${SPECIAL_FOLDER}/${VERSION_FOLDER_NAME}/**`, getFilesVersioned)
router.get(`hyper://*/${SPECIAL_FOLDER}/${DIFF_FOLDER_NAME}/**`, diffVersions)
router.get('hyper://*/**', getFiles)
router.head('hyper://*/**', headFiles)

Expand Down Expand Up @@ -746,6 +748,50 @@ export default async function makeHyperFetch ({
return serveFile(drive, path, isRanged)
}

async function diffVersions (request) {
const url = new URL(request.url)
const { hostname, pathname: rawPathname } = url
const pathname = decodeURI(ensureLeadingSlash(rawPathname))

const parts = pathname.split('/')
const versions = parts[3]
let [oldVersion, newVersion] = versions.split(/\.\./).map(Number)
const realPath = ensureLeadingSlash(parts.slice(4).join('/'))

const drive = await getDrive(`hyper://${hostname}/`, true)

const resHeaders = {
Link: `<${url.href}>; rel="canonical"`
}

if (oldVersion > drive.version || newVersion > drive.version) {
return {
status: 405,
body: 'Versions out of range',
headers: {
...resHeaders,
ETag: drive.version
}
}
}

const snapshot = await drive.checkout(newVersion)

let diffs = []

for await (const diff of snapshot.diff(oldVersion, realPath)) {
// TODO: Use try/catch?
diffs.push(diff)
}

return {
status: 200,
// TODO: More headers?
headers: resHeaders,
body: JSON.stringify(diffs, null, '\t')
}
}

return fetch
}

Expand Down

0 comments on commit 1146b92

Please sign in to comment.