Skip to content

Commit

Permalink
Added Wasm for GET /api/tree
Browse files Browse the repository at this point in the history
  • Loading branch information
ItalyPaleAle committed Oct 8, 2020
1 parent ec309af commit 062e3a4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 18 deletions.
13 changes: 13 additions & 0 deletions ui/src/sw/lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Returns a JavaScript Response object containing the given data.
*
* @param {*} data - Data that will be included in the response
*/
export function JSONResponse(data) {
const headers = new Headers()
headers.set('Content-Type', 'application/json')
return new Response(
JSON.stringify(data),
{headers}
)
}
15 changes: 10 additions & 5 deletions ui/src/sw/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
import fileHandler from './requests/file'
import apiRepoUnlockHandler from './requests/api-repo-unlock'
import apiInfoHandler from './requests/api-info'
import apiTreeHandler from './requests/api-tree'

// List of fetch requests to intercept and their handlers
// Path can either be a string, which matches the pathname's prefix, or a regular expression matching the pathname
const requests = [
{
path: '/file',
handler: fileHandler
path: '/api/info',
handler: apiInfoHandler
},
{
path: '/api/repo/unlock',
handler: apiRepoUnlockHandler
},
{
path: '/api/info',
handler: apiInfoHandler
}
path: '/api/tree',
handler: apiTreeHandler
},
{
path: '/file',
handler: fileHandler
},
]

/**
Expand Down
9 changes: 5 additions & 4 deletions ui/src/sw/requests/api-info.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Utils
import {JSONResponse} from '../lib/utils'

// Stores
import stores from '../stores'

Expand All @@ -13,7 +16,7 @@ export default async function(req) {
let res = await fetch(req)

// Check if the repo is unlocked
if (stores.masterKey) {
if (stores.masterKey && stores.index) {
// Read the response
const data = await res.json()
if (!data) {
Expand All @@ -31,9 +34,7 @@ export default async function(req) {
data.readOnly = true

// Rebuild the Response object
const headers = new Headers()
headers.set('Content-Type', 'application/json')
res = new Response(JSON.stringify(data), {headers})
res = JSONResponse(data)
}

return res
Expand Down
16 changes: 7 additions & 9 deletions ui/src/sw/requests/api-repo-unlock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* global Prvt */

// Utils
import {JSONResponse} from '../lib/utils'

// Stores
import stores from '../stores'

Expand Down Expand Up @@ -28,13 +31,8 @@ export default async function(req) {
stores.index = Prvt.getIndex(stores.masterKey)

// Return a Response object just like the API server would for /api/repo/unlock
const headers = new Headers()
headers.set('Content-Type', 'application/json')
return new Response(
JSON.stringify({
keyId: result.keyId,
type: 'passphrase'
}),
{headers}
)
return JSONResponse({
keyId: result.keyId,
type: 'passphrase'
})
}
46 changes: 46 additions & 0 deletions ui/src/sw/requests/api-tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Utils
import {JSONResponse} from '../lib/utils'

// Stores
import stores from '../stores'

/**
* Handler for the /api/tree/:path requests.
*
* The GET method returns the list of files in the folder.
* The POST method is not yet implemented; eventually, it will allow uploading files.
*
* @param {Request} req - Request object from the client
* @returns {Response} Response object for the request
*/
export default async function(req) {
// Get the method of the request
// Only GET requests are implemented for now
const method = req.method
if (method != 'GET') {
throw Error('Invalid request method')
}

// Ensure we have the master key
if (!stores.masterKey || !stores.index) {
throw Error('Repository is not unlocked')
}

// Get the file path from the URL
const reqPath = (new URL(req.url)).pathname
if (!reqPath || !reqPath.startsWith('/api/tree')) {
throw Error('Invalid request path')
}
let path = reqPath.substr(9) || '/'

// Ensure path starts with /
if (path.charAt(0) != '/') {
path = '/' + path
}

// Get the list of files
const list = await stores.index.listFolder(path) || []

// Return a Response object with the list of files
return JSONResponse(list)
}

0 comments on commit 062e3a4

Please sign in to comment.