Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add listing functionality #13

Merged
merged 3 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,33 @@ erisa@Tuturu:~$ curl -X POST -H "Authorization: mysecret" https://erisa.link/ \
}
```

It is a planned feature to be able to list all URLs via a `GET` on `/` with `Authorization`.
You can list all URLs by sending a `GET` to `/` (with the `Authorization` header set to your secret, of course).

For the time being you can view them from your Cloudflare Dashboard:
Cloudflare Dashboard -> Workers -> KV -> View on the namespace.
```json
kot@Starry:~$ curl -H "Authorization: mysecret" "https://erisa.link/?prefix=%2F&limit=1"
{
"list_complete": false,
"cursor": "AAAAAJhOXekucRAqut7Xs7Q2f09GCZyStWBfONvq6u5JP05Bg-z5FM5gf7krRaDrsvyxqfDuvFWUHIZp2n9OZ7Au92h-x68xwg8-bwerIoPd7fesG5w-ZB6f6oXopZHNXDCscmVUQ0OIaDEOx_6pruyEcCKfD3WpOstj6lO_sYJG_zQKdBgmYvLoMFQpK-cK7t8mCLWQA2t351xc9sJ08SM0JniY73t7bOdSxF3ADVTV6ihMSti0Z6svhpknfCn9VHjT",
"links": [
{
"key": "/0031qr7q7"
},
{
"key": "/00ybqita"
},
{
"key": "/02ji9wlg"
}
]
}
```

The endpoint is paginated by default (1000/page). Just send `cursor` in the query string to access the next page.

You can set `limit` in the query string to `0` to retrieve all URLs.

You can also view URLs from your Cloudflare Dashboard:
`Cloudflare Dashboard -> Workers -> KV -> View` on the namespace.

## Plausible Analytics

Expand Down
58 changes: 49 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, Hono } from 'hono'
import { Context as HonoContext, Env as HonoEnv, Hono } from 'hono'
import * as st from 'simple-runtypes'

type Variables = {
Expand All @@ -9,6 +9,7 @@ type Variables = {

type Bindings = {
WORKERLINKS_SECRET: string
PLAUSIBLE_HOST?: string
KV: KVNamespace
kv: KVNamespace
}
Expand All @@ -31,7 +32,18 @@ const bulkValidator = st.dictionary(
url,
)

const app = new Hono<{ Variables: Variables; Bindings: Bindings }>()
const checkAuth = (c: Context) =>
c.req.headers.get('Authorization') === c.env.WORKERLINKS_SECRET

const unauthorized = (c: Context) =>
c.json({ code: '401 Unauthorized', message: 'Unauthorized' }, 401)

type Env = {
Bindings: Bindings
Variables: Variables
}
type Context = HonoContext<Env>
const app = new Hono<Env>()

// store the path, key and short url for reference in requeests
// e.g. c.get('key')
Expand All @@ -57,20 +69,48 @@ app.use('*', async (c, next) => {

// handle auth
app.use('*', async (c, next) => {
c.res.headers.set('Vary', 'Authorization')

if (c.env.WORKERLINKS_SECRET === undefined) {
return c.text('Secret is not defined. Please add WORKERLINKS_SECRET.')
}

if (
!['GET', 'HEAD'].includes(c.req.method) &&
c.req.headers.get('Authorization') !== c.env.WORKERLINKS_SECRET
) {
return c.json({ code: '401 Unauthorized', message: 'Unauthorized' }, 401)
if (!['GET', 'HEAD'].includes(c.req.method) && !checkAuth(c)) {
return unauthorized(c)
}

await next()
})

// retrieve list of keys
app.get('/', async (c) => {
if (c.req.header('Authorization')) {
if (!checkAuth(c)) {
return unauthorized(c)
}

let { prefix, cursor, limit: limitStr } = c.req.query()
prefix = prefix ? decodeURIComponent(prefix) : ''
cursor = cursor ? decodeURIComponent(cursor) : ''
let limit = limitStr ? parseInt(decodeURIComponent(limitStr)) : 1000

let { keys, ...list } = await c.env.KV.list({
limit,
prefix,
cursor,
})

return c.json({
...list,
links: keys.map((key) => ({
key: key.name,
})),
})
} else {
return handleGetHead(c)
}
})

// retrieve key
app.get('*', handleGetHead)

Expand All @@ -84,7 +124,7 @@ async function handleGetHead(c: Context) {

if (urlResult == null) {
return c.json(
{ code: '404 Not Found', message: ' Key does not exist.' },
{ code: '404 Not Found', message: 'Key does not exist.' },
404,
)
} else {
Expand All @@ -108,7 +148,7 @@ app.delete('*', async (c) => {

if (urlResult == null) {
return c.json(
{ code: '404 Not Found', message: ' Key does not exist.' },
{ code: '404 Not Found', message: 'Key does not exist.' },
404,
)
} else {
Expand Down