-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdurables.ts
154 lines (141 loc) · 4.1 KB
/
durables.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { Hono } from "hono";
import { TokenResponse } from "auth0";
interface SESSIONInterface {
session: TokenResponse | null;
created: boolean;
createdDate: Date;
endOfLife: number;
state: DurableObjectState;
app: Hono;
}
//things available in storage
type SESSIONRead = Pick<
SESSIONInterface,
"session" | "created" | "createdDate" | "endOfLife"
>;
export class State {
created = false;
state: DurableObjectState;
app: Hono = new Hono();
constructor(state: DurableObjectState) {
this.state = state;
this.state.blockConcurrencyWhile(async () => {
const stored = await this.state.storage?.get<boolean>("created");
this.created = stored || false;
});
this.app.get("/created", async (c) => {
this.created = true;
await this.state.storage?.put("created", this.created);
const dt = new Date();
dt.setDate(dt.getDate() + 1);
this.state.storage.setAlarm(dt);
return c.json(this.created);
});
this.app.get("/", async (c) => {
return c.json(this.created);
});
}
async fetch(request: Request) {
return this.app.fetch(request);
}
async alarm() {
this.created = false;
await this.state.storage.deleteAll();
}
}
export class Session implements SESSIONInterface {
session = null;
created = false;
createdDate = new Date();
endOfLife = 360 * 24 * 60 * 60 + new Date().getTime();
state;
app = new Hono();
constructor(state: DurableObjectState) {
this.state = state;
this.state.blockConcurrencyWhile(async () => {
const stored = await this.state.storage?.get<SESSIONInterface["session" | "created" | "createdDate" | "endOfLife"]>([
"created",
"session",
"createdDate",
"endOfLife",
]);
const created = "created"
this.created = stored.get(created) ?? false;
this.session = stored.get("session") ?? null;
const createdDate = stored.get("createdDate");
if (createdDate) {
this.createdDate = createdDate;
} else {
await this.state.storage?.put("createdDate", this.createdDate);
}
this.endOfLife =
stored.get("endOfLife") || 360 * 24 * 60 * 60 + new Date().getTime();
});
this.app.post("/", async (c) => {
const body: TokenResponse = await c.req.parseBody();
if (this.created == false) {
this.created = true;
await this.state.storage?.put("created", this.created);
const ok = this.extension(c);
if (!ok) {
return c.notFound();
}
}
this.state.storage?.put("session", body);
this.session = body;
console.log("recieved", body);
const response: SESSIONRead = {
createdDate: this.createdDate,
created: this.created,
session: this.session,
};
return c.json(response);
});
this.app.get("/", async (c) => {
const ok = this.extension(c);
if (ok) {
const response: SESSIONRead = {
createdDate: this.createdDate,
created: this.created,
session: this.session,
};
return c.json(response);
} else {
return c.notFound();
}
});
}
async fetch(request: Request) {
return this.app.fetch(request);
}
async alarm() {
this.reset();
}
async reset() {
this.session = null;
this.created = false;
this.createdDate = null;
await this.state.storage.deleteAll();
}
//auth0 has point at which extendion session is impossible
extension(c) {
const days = 24 * 60;
const extension = new Date();
const exp = c.env.AUTH0EXPIRY * 1;
const relo = c.env.AUTH0MANDATORYRELOG * 1;
if (exp == 0 || relo == 0 || isNaN(relo) || isNaN(exp)) {
return true;
}
extension.setDate(extension.getDate() + exp / days);
const origin = this.createdDate;
const endOfLife = new Date(origin.setDate(origin.getDate() + relo / days));
if (endOfLife < extension) {
this.reset();
return false;
}
this.state.storage.setAlarm(extension);
this.endOfLife = extension.getTime();
this.state.storage.put('endOfLife', this.endOfLife)
return true;
}
}