Skip to content

Commit

Permalink
fix(metadata): compatibility with old kv records
Browse files Browse the repository at this point in the history
fix `Error 500: Cannot read properties of null` on old kv records
  • Loading branch information
Misaka13514 committed Apr 16, 2024
1 parent 6d667ab commit 1ede146
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/handlers/handleDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function handleDelete(request, env, ctx) {
if (item.value === null) {
throw new WorkerError(404, `paste of name '${short}' not found`)
} else {
if (passwd !== item.metadata.passwd) {
if (passwd !== item.metadata?.passwd) {
throw new WorkerError(403, `incorrect password for paste '${short}`)
} else {
await env.PB.delete(short)
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/handleRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function pasteCacheHeader(env) {
}

function lastModifiedHeader(paste) {
const lastModified = paste.metadata.lastModified
const lastModified = paste.metadata?.lastModified
return lastModified ? { "last-modified": new Date(lastModified).toGMTString() } : {}
}

Expand Down Expand Up @@ -53,7 +53,7 @@ export async function handleGet(request, env, ctx) {
}

// check `if-modified-since`
const pasteLastModified = item.metadata.lastModified
const pasteLastModified = item.metadata?.lastModified
const headerModifiedSince = request.headers.get("if-modified-since")
if (pasteLastModified && headerModifiedSince) {
let pasteLastModifiedMs = Date.parse(pasteLastModified)
Expand All @@ -68,7 +68,7 @@ export async function handleGet(request, env, ctx) {
}

// determine filename with priority: url path > meta
const returnFilename = filename || item.metadata.filename
const returnFilename = filename || item.metadata?.filename

// handle URL redirection
if (role === "u") {
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/handleWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ export async function handlePostOrPut(request, env, ctx, isPut) {
if (item.value === null) {
throw new WorkerError(404, `paste of name '${short}' is not found`)
} else {
const date = item.metadata.postedAt
if (passwd !== item.metadata.passwd) {
const date = item.metadata?.postedAt
if (passwd !== item.metadata?.passwd) {
throw new WorkerError(403, `incorrect password for paste '${short}`)
} else {
return makeResponse(
Expand Down

0 comments on commit 1ede146

Please sign in to comment.