Skip to content

Commit f9e9025

Browse files
Misaka13514SharzyL
authored andcommitted
fix(metadata): compatibility with old kv records
fix `Error 500: Cannot read properties of null` on old kv records
1 parent 6d667ab commit f9e9025

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/handlers/handleDelete.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export async function handleDelete(request, env, ctx) {
77
if (item.value === null) {
88
throw new WorkerError(404, `paste of name '${short}' not found`)
99
} else {
10-
if (passwd !== item.metadata.passwd) {
10+
if (passwd !== item.metadata?.passwd) {
1111
throw new WorkerError(403, `incorrect password for paste '${short}`)
1212
} else {
1313
await env.PB.delete(short)

src/handlers/handleRead.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function pasteCacheHeader(env) {
1616
}
1717

1818
function lastModifiedHeader(paste) {
19-
const lastModified = paste.metadata.lastModified
19+
const lastModified = paste.metadata?.lastModified
2020
return lastModified ? { "last-modified": new Date(lastModified).toGMTString() } : {}
2121
}
2222

@@ -53,7 +53,7 @@ export async function handleGet(request, env, ctx) {
5353
}
5454

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

7070
// determine filename with priority: url path > meta
71-
const returnFilename = filename || item.metadata.filename
71+
const returnFilename = filename || item.metadata?.filename
7272

7373
// handle URL redirection
7474
if (role === "u") {

src/handlers/handleWrite.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ export async function handlePostOrPut(request, env, ctx, isPut) {
132132
if (item.value === null) {
133133
throw new WorkerError(404, `paste of name '${short}' is not found`)
134134
} else {
135-
const date = item.metadata.postedAt
136-
if (passwd !== item.metadata.passwd) {
135+
const date = item.metadata?.postedAt
136+
if (passwd !== item.metadata?.passwd) {
137137
throw new WorkerError(403, `incorrect password for paste '${short}`)
138138
} else {
139139
return makeResponse(

0 commit comments

Comments
 (0)