Skip to content

Commit

Permalink
add expiry header
Browse files Browse the repository at this point in the history
  • Loading branch information
mchangrh committed Feb 20, 2024
1 parent 1b1f274 commit e1dbbd8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions worker/binStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ const binDeleteValue = async (ID: string) => {
return new Response('deleted', { status: 200 })
}

const binSetValue = async (ID: string, value: ArrayBuffer, contentType?: contentType, filename?: filename) => {
const binSetValue = async (ID: string, value: ArrayBuffer, contentType?: contentType, filename?: filename, expiry?: string|null) => {
const length = value.byteLength
if (length === 0 ) return 'empty body'
if (length >= MAX_BIN_SIZE) return 'body too large'
// if less than 10k, store for max length, else store according to size, down to 1hr
const expirationTime = (length <= 10000)
const sizeExpiry = (length <= 10000)
? MAX_EXPIRY
: Math.max(MAX_EXPIRY*(1-(length/MAX_BIN_SIZE)), 3600)
const expirationTime = expiry ? Math.min(sizeExpiry, parseInt(expiry)) : sizeExpiry
await BIN_BIN.put(ID, value, { expirationTtl: expirationTime, metadata: { type: contentType, filename }})
return false
}

const binCreateBin = async (body: ArrayBuffer, contentType: contentType, filename?: filename): Promise<{ err: string|false, binID: string }> => {
const binCreateBin = async (body: ArrayBuffer, contentType: contentType, filename?: filename, expiry?: string|null): Promise<{ err: string|false, binID: string }> => {
let binID = genID() // get random ID
const currentBin = await binGetValue(binID) // check if bin is empty
if (currentBin !== null) binID = genID()
const err = await binSetValue(binID, body, contentType, filename) // put into bin
const err = await binSetValue(binID, body, contentType, filename, expiry) // put into bin
if (filename) binID = `${binID}/${filename}`
return { err, binID }
}
Expand Down
4 changes: 2 additions & 2 deletions worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const handleBins = async (request: Request, pathname: string, type: binType) =>

const createBin = async (request: Request, type: binType, pathFilename?: string) => {
if (type === 'bin') {
const {body, contentType, filename} = await parseBody(request)
const { err, binID } = await bin.create(body, contentType, filename ?? pathFilename)
const {body, contentType, filename, expiry} = await parseBody(request)
const { err, binID } = await bin.create(body, contentType, filename ?? pathFilename, expiry)
return (err) ? bodyError(err) : textResponse(binID)
} else { // url bin
const { searchParams } = new URL(request.url)
Expand Down
3 changes: 2 additions & 1 deletion worker/responseHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const parseBody = async(request: Request) => { // parse body
return ({
body: await request.arrayBuffer(),
contentType: request.headers.get('Content-Type'),
filename: request.headers.get('File-Name')
filename: request.headers.get('File-Name'),
expiry: request.headers.get('Expiry')
})
}

Expand Down

0 comments on commit e1dbbd8

Please sign in to comment.