-
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: api endpoint to get from bucket and gateway using worker binding
- Loading branch information
1 parent
08b48f8
commit d098a81
Showing
17 changed files
with
465 additions
and
413 deletions.
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
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
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,96 +1,28 @@ | ||
/* eslint-env serviceworker, browser */ | ||
/* global Response */ | ||
|
||
import { JSONResponse } from '../utils/json-response.js' | ||
import { getSourceUrl, getNormalizedUrl } from '../utils/url.js' | ||
import { UrlNotFoundError } from '../errors.js' | ||
|
||
/** | ||
* @typedef {import('../env').Env} Env | ||
*/ | ||
|
||
/** | ||
* Handle perma-cache put request | ||
* Handle perma-cache get request | ||
* | ||
* @param {Request} request | ||
* @param {Env} env | ||
*/ | ||
export async function permaCacheListGet(request, env) { | ||
const requestUrl = new URL(request.url) | ||
const { searchParams } = requestUrl | ||
const { size, page, sort, order } = parseSearchParams(searchParams) | ||
|
||
const entries = await env.db.listPermaCache(request.auth.user.id, { | ||
size, | ||
page, | ||
sort, | ||
order, | ||
}) | ||
|
||
// Get next page link | ||
const headers = | ||
entries.length === size | ||
? { | ||
Link: `<${requestUrl.pathname}?size=${size}&page=${ | ||
page + 1 | ||
}>; rel="next"`, | ||
} | ||
: undefined | ||
return new JSONResponse(entries, { headers }) | ||
} | ||
|
||
/** | ||
* @param {URLSearchParams} searchParams | ||
*/ | ||
function parseSearchParams(searchParams) { | ||
// Parse size parameter | ||
let size = 25 | ||
if (searchParams.has('size')) { | ||
const parsedSize = parseInt(searchParams.get('size')) | ||
if (isNaN(parsedSize) || parsedSize <= 0 || parsedSize > 1000) { | ||
throw Object.assign(new Error('invalid page size'), { status: 400 }) | ||
} | ||
size = parsedSize | ||
export async function permaCacheGet(request, env) { | ||
const sourceUrl = getSourceUrl(request, env) | ||
const normalizedUrl = getNormalizedUrl(sourceUrl, env) | ||
const r2Key = normalizedUrl.toString() | ||
|
||
const r2Object = await env.SUPERHOT.get(r2Key) | ||
if (r2Object) { | ||
return new Response(r2Object.body) | ||
} | ||
|
||
// Parse cursor parameter | ||
let page = 0 | ||
if (searchParams.has('page')) { | ||
const parsedPage = parseInt(searchParams.get('page')) | ||
if (isNaN(parsedPage) || parsedPage <= 0) { | ||
throw Object.assign(new Error('invalid page number'), { status: 400 }) | ||
} | ||
page = parsedPage | ||
} | ||
|
||
// Parse sort parameter | ||
let sort = 'date' | ||
if (searchParams.has('sort')) { | ||
const parsedSort = searchParams.get('sort') | ||
if (parsedSort !== 'date' && parsedSort !== 'size') { | ||
throw Object.assign( | ||
new Error('invalid list sort, either "date" or "size"'), | ||
{ status: 400 } | ||
) | ||
} | ||
sort = parsedSort | ||
} | ||
|
||
// Parse order parameter | ||
let order = 'asc' | ||
if (searchParams.has('order')) { | ||
const parsedOrder = searchParams.get('order') | ||
if (parsedOrder !== 'asc' && parsedOrder !== 'desc') { | ||
throw Object.assign( | ||
new Error('invalid list sort order, either "asc" or "desc"'), | ||
{ status: 400 } | ||
) | ||
} | ||
sort = parsedOrder | ||
} | ||
|
||
return { | ||
size, | ||
page, | ||
sort, | ||
order, | ||
} | ||
throw new UrlNotFoundError() | ||
} |
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,4 +1,5 @@ | ||
export { permaCachePost } from './post.js' | ||
export { permaCacheListGet } from './get.js' | ||
export { permaCacheGet } from './get.js' | ||
export { permaCacheAccountGet } from './account.js' | ||
export { permaCacheListGet } from './list.js' | ||
export { permaCacheDelete } from './delete.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,96 @@ | ||
/* eslint-env serviceworker, browser */ | ||
/* global Response */ | ||
|
||
import { JSONResponse } from '../utils/json-response.js' | ||
|
||
/** | ||
* @typedef {import('../env').Env} Env | ||
*/ | ||
|
||
/** | ||
* Handle perma-cache list get request | ||
* | ||
* @param {Request} request | ||
* @param {Env} env | ||
*/ | ||
export async function permaCacheListGet(request, env) { | ||
const requestUrl = new URL(request.url) | ||
const { searchParams } = requestUrl | ||
const { size, page, sort, order } = parseSearchParams(searchParams) | ||
|
||
const entries = await env.db.listPermaCache(request.auth.user.id, { | ||
size, | ||
page, | ||
sort, | ||
order, | ||
}) | ||
|
||
// Get next page link | ||
const headers = | ||
entries.length === size | ||
? { | ||
Link: `<${requestUrl.pathname}?size=${size}&page=${ | ||
page + 1 | ||
}>; rel="next"`, | ||
} | ||
: undefined | ||
return new JSONResponse(entries, { headers }) | ||
} | ||
|
||
/** | ||
* @param {URLSearchParams} searchParams | ||
*/ | ||
function parseSearchParams(searchParams) { | ||
// Parse size parameter | ||
let size = 25 | ||
if (searchParams.has('size')) { | ||
const parsedSize = parseInt(searchParams.get('size')) | ||
if (isNaN(parsedSize) || parsedSize <= 0 || parsedSize > 1000) { | ||
throw Object.assign(new Error('invalid page size'), { status: 400 }) | ||
} | ||
size = parsedSize | ||
} | ||
|
||
// Parse cursor parameter | ||
let page = 0 | ||
if (searchParams.has('page')) { | ||
const parsedPage = parseInt(searchParams.get('page')) | ||
if (isNaN(parsedPage) || parsedPage <= 0) { | ||
throw Object.assign(new Error('invalid page number'), { status: 400 }) | ||
} | ||
page = parsedPage | ||
} | ||
|
||
// Parse sort parameter | ||
let sort = 'date' | ||
if (searchParams.has('sort')) { | ||
const parsedSort = searchParams.get('sort') | ||
if (parsedSort !== 'date' && parsedSort !== 'size') { | ||
throw Object.assign( | ||
new Error('invalid list sort, either "date" or "size"'), | ||
{ status: 400 } | ||
) | ||
} | ||
sort = parsedSort | ||
} | ||
|
||
// Parse order parameter | ||
let order = 'asc' | ||
if (searchParams.has('order')) { | ||
const parsedOrder = searchParams.get('order') | ||
if (parsedOrder !== 'asc' && parsedOrder !== 'desc') { | ||
throw Object.assign( | ||
new Error('invalid list sort order, either "asc" or "desc"'), | ||
{ status: 400 } | ||
) | ||
} | ||
sort = parsedOrder | ||
} | ||
|
||
return { | ||
size, | ||
page, | ||
sort, | ||
order, | ||
} | ||
} |
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
Oops, something went wrong.