Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Add support of Schedule event on Cloudworker #165

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare interface CloudworkerInit {

declare class Cloudworker {
constructor (script: string, opts?: CloudworkerInit)
triggerCronJob (): Promise<Response>
dispatch (request: Request): Promise<Response>
listen (...args: any[]): http.Server
}
Expand Down
17 changes: 17 additions & 0 deletions lib/__tests__/cloudworker.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Cloudworker = require('../cloudworker')
const runtime = require('../runtime')
const httpMocks = require('node-mocks-http')
const { KeyValueStore } = require('../kv')
const simpleScript = `addEventListener('fetch', event => {
event.respondWith(new Response('hello', {status: 200}))
})
Expand Down Expand Up @@ -165,4 +166,20 @@ describe('cloudworker', () => {
expect(res.headers.get('x-colo')).toEqual('YYZ')
done()
})

test('triggerCronJob sends scheduled event to worker', async done => {
const store = new KeyValueStore()
store.put('hello', 'world')
const bindings = {TestNamespace: store}
const scheduleScript = `
addEventListener('scheduled', async(event) => {
await TestNamespace.put("hello", "test");
})
`
const cw = new Cloudworker(scheduleScript, {bindings})
await cw.triggerCronJob()
const valueTest = await store.get('hello', 'text')
expect(valueTest).toEqual('test')
done()
})
})
1 change: 1 addition & 0 deletions lib/__tests__/runtime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe('runtime', () => {
'RegExp',
'Request',
'Response',
'ScheduleEvent',
'Set',
'SharedArrayBuffer',
'String',
Expand Down
17 changes: 17 additions & 0 deletions lib/cloudworker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ class Cloudworker {
this._load(workerScript, this.context)
}

async triggerCronJob () {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve()
}, 1000)

const error = async (error) => {
reject(error)
}

const event = new runtime.ScheduleEvent('scheduled', {scheduledTime: 0})
event.onError = error
this.dispatcher.emit('scheduled', event)
})
return promise
}

async dispatch (request) {
if (!(request instanceof runtime.Request)) {
throw new TypeError('argument must be a Request')
Expand Down
3 changes: 3 additions & 0 deletions lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { Request, Response, fetch, Headers, freezeHeaders, bindCfProperty } = req
const { URL, URLSearchParams } = require('./runtime/url')
const { ReadableStream, WritableStream, TransformStream } = require('./runtime/stream')
const { FetchEvent } = require('./runtime/fetch-event')
const { ScheduleEvent } = require('./runtime/schedule-event')
const { crypto } = require('./runtime/crypto')
const { TextDecoder, TextEncoder } = require('./runtime/text-encoder')
const { atob, btoa } = require('./runtime/base64')
Expand All @@ -23,6 +24,7 @@ class Context {
this.WritableStream = WritableStream
this.TransformStream = TransformStream
this.FetchEvent = FetchEvent
this.ScheduleEvent = ScheduleEvent
this.caches = cacheFactory
this.crypto = crypto
this.TextDecoder = TextDecoder
Expand Down Expand Up @@ -98,6 +100,7 @@ module.exports = {
Context,
fetch,
FetchEvent,
ScheduleEvent,
freezeHeaders,
bindCfProperty,
Headers,
Expand Down
15 changes: 15 additions & 0 deletions lib/runtime/schedule-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ScheduleEvent {
constructor (type, init) {
this.scheduledTime = init.scheduledTime
}

waitUntil (promise) {

}

passThroughOnException () {

}
}

module.exports.ScheduleEvent = ScheduleEvent