-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http perma cache status get (#70)
- Loading branch information
1 parent
df3318e
commit 271bd85
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { permaCachePost } from './post.js' | ||
export { permaCacheListGet } from './get.js' | ||
export { permaCacheStatusGet } from './status.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-env serviceworker, browser */ | ||
/* global Response */ | ||
import { HTTPError } from '../errors.js' | ||
import { JSONResponse } from '../utils/json-response.js' | ||
|
||
/** | ||
* @typedef {import('../env').Env} Env | ||
*/ | ||
|
||
/** | ||
* Handle perma-cache status get request | ||
* | ||
* @param {Request} request | ||
* @param {Env} env | ||
*/ | ||
export async function permaCacheStatusGet(request, env) { | ||
const kvPrefix = `${request.auth.user.id}` | ||
let usedStorage = 0 | ||
let endCursor | ||
|
||
// Iterate KV user prefix and get all the content | ||
do { | ||
const { | ||
keys, | ||
cursor, | ||
list_complete: listComplete, | ||
} = await env.PERMACACHE.list({ | ||
prefix: kvPrefix, | ||
limit: 1000, | ||
cursor: endCursor, | ||
}) | ||
|
||
if (!keys) { | ||
throw new HTTPError('No perma cached content found for given user') | ||
} | ||
|
||
keys.forEach((key) => { | ||
usedStorage += key.metadata.contentLength | ||
}) | ||
|
||
endCursor = cursor | ||
|
||
// Stop looking if no other entries available | ||
if (listComplete) { | ||
endCursor = undefined | ||
} | ||
} while (endCursor) | ||
|
||
return new JSONResponse({ | ||
usedStorage, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import test from 'ava' | ||
|
||
import { getMiniflare } from './scripts/utils.js' | ||
import { createTestUser } from './scripts/helpers.js' | ||
|
||
test.beforeEach(async (t) => { | ||
const user = await createTestUser({ | ||
grantRequiredTags: true, | ||
}) | ||
|
||
// Create a new Miniflare environment for each test | ||
t.context = { | ||
mf: getMiniflare(), | ||
user, | ||
} | ||
}) | ||
|
||
test('Get perma cache status from user', async (t) => { | ||
const { mf, user } = t.context | ||
const statusResponseEmpty = await mf.dispatchFetch( | ||
'https://localhost:8788/perma-cache/status', | ||
{ | ||
method: 'GET', | ||
headers: { Authorization: `Bearer ${user.token}` }, | ||
} | ||
) | ||
t.is(statusResponseEmpty.status, 200) | ||
|
||
const statusEmpty = await statusResponseEmpty.json() | ||
t.is(statusEmpty.usedStorage, 0) | ||
|
||
const url = | ||
'http://bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq.ipfs.localhost:9081' | ||
const response = await mf.dispatchFetch(getPermaCachePutUrl(url), { | ||
method: 'POST', | ||
headers: { Authorization: `Bearer ${user.token}` }, | ||
}) | ||
t.is(response.status, 200) | ||
const body = await response.json() | ||
|
||
const statusResponseNotEmpty = await mf.dispatchFetch( | ||
'https://localhost:8788/perma-cache/status', | ||
{ | ||
method: 'GET', | ||
headers: { Authorization: `Bearer ${user.token}` }, | ||
} | ||
) | ||
t.is(statusResponseNotEmpty.status, 200) | ||
|
||
const statusNotEmpty = await statusResponseNotEmpty.json() | ||
t.is(statusNotEmpty.usedStorage, body.contentLength) | ||
}) | ||
|
||
const getPermaCachePutUrl = (url) => | ||
`https://localhost:8788/perma-cache/${encodeURIComponent(url)}` |