Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-lippert committed Jan 12, 2024
1 parent 53a5400 commit da525c0
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ export class Alarm {
}
/**
* Increase the alarm duration.
* @param {Request} req
* @param {Request} req
*/
async fetch(req) {
const { url, method, headers } = req
const { pathname, searchParams } = new URL(url)
if (searchParams.has('callback')) {
await this.state.storage.put('callback', this.callback = decodeURIComponent(searchParams.get('callback')))
await this.state.storage.put('callback', (this.callback = decodeURIComponent(searchParams.get('callback'))))
if (this.body) {
delete this.body
await this.state.storage.delete('body')
Expand All @@ -49,12 +49,11 @@ export class Alarm {
}
}
if (method === 'POST') {
await this.state.storage.put('body', this.body = await req.text())
if (headers.has('Content-Type'))
await this.state.storage.put('contentType', this.contentType = headers.get('Content-Type'))
await this.state.storage.put('body', (this.body = await req.text()))
if (headers.has('Content-Type')) await this.state.storage.put('contentType', (this.contentType = headers.get('Content-Type')))
}
if (searchParams.has('every')) {
await this.state.storage.put('every', this.every = parseInt(searchParams.get('every')))
await this.state.storage.put('every', (this.every = parseInt(searchParams.get('every'))))
}
let currentAlarm = await this.storage.getAlarm()
if (currentAlarm == null || Date.now() <= this.due) {
Expand All @@ -69,25 +68,25 @@ export class Alarm {
unit.startsWith('w') ? 604800000 :
unit.startsWith('y') ? 31449600000 :
1
await this.state.storage.put('due', this.due = Date.now() + value)
await this.state.storage.put('due', (this.due = Date.now() + value))
} else if (!searchParams.has('read')) {
// If there is no alarm, set one for 10 seconds from now
await this.state.storage.put('due', this.due = parseInt(searchParams.get('due')) || Date.now() + 10000)
await this.state.storage.put('due', (this.due = parseInt(searchParams.get('due')) || Date.now() + 10000))
}
await this.storage.setAlarm(this.due)
}

return new Response(JSON.stringify({
key: pathname.split('/')[1],
callback: this.callback,
method: this.body ? 'POST': 'GET',
method: this.body ? 'POST' : 'GET',
body: this.body,
contentType: this.contentType,
due: this.due,
every: this.every || undefined,
}), {
headers: {
"content-type": "application/json;charset=UTF-8",
'content-type': 'application/json;charset=UTF-8',
},
})
}
Expand Down

0 comments on commit da525c0

Please sign in to comment.