-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathtest-server-workerd.js
More file actions
81 lines (66 loc) · 1.75 KB
/
test-server-workerd.js
File metadata and controls
81 lines (66 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2025 Cloudflare, Inc.
// Licensed under the MIT license found in the LICENSE.txt file or at:
// https://opensource.org/license/mit
// Test server implemented in workerd instead of Node.
//
// This is only used by the workerd tests, across a service binding.
//
// This file is JavaScript instead of TypeScript because otherwise we'd need to set up a separate
// build step for it. Instead, we're getting by configuring the worker in vitest.config.ts by
// just specifying the raw JS modules.
import { newWorkersRpcResponse } from "../dist/index-workers.js";
import { RpcTarget, DurableObject } from "cloudflare:workers";
// TODO(cleanup): At present we clone the implementation of Counter and TestTarget because
// otherwise we need to set up a build step for `test-util.ts`.
export class Counter extends RpcTarget {
constructor(i) {
super();
this.i = i;
}
increment(amount = 1) {
this.i += amount;
return this.i;
}
}
export class TestDo extends DurableObject {
setValue(val) {
this.value = val;
}
getValue() {
return this.value;
}
}
export class TestTarget extends RpcTarget {
constructor(env) {
super();
this.env = env;
}
square(i) {
return i * i;
}
callSquare(self, i) {
return { result: self.square(i) };
}
throwError() {
throw new RangeError("test error");
}
makeCounter(i) {
return new Counter(i);
}
incrementCounter(c, i = 1) {
return c.increment(i);
}
getDurableObject(name) {
return this.env.TEST_DO.getByName(name);
}
}
export default {
async fetch(req, env, ctx) {
return newWorkersRpcResponse(req, new TestTarget(env), {
onSendError(err) { return err; }
});
},
async greet(name, env, ctx) {
return `Hello, ${name}!`;
}
}