-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.js
238 lines (197 loc) · 5.84 KB
/
router.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const Router = require('express-promise-router')
const Ably = require('ably')
const ably = new Ably.Realtime(process.env.ABLY_AKY_KEY)
const { db } = require('./db')
const isProduction = process.env.FLY_APP_NAME != null
const READ_ONLY_SQL_TRANSACTION = 25006
const router = new Router()
module.exports = router
router.get('/', async (req, res) => {
let result
if (isProduction) {
const { FLY_APP_NAME: appName, FLY_REGION: runningRegion } = process.env
const edgeNodeRegion = req.get('Fly-Region')
result = { env: 'prod', appName, runningRegion, edgeNodeRegion }
} else {
result = { env: 'dev' }
}
res.json(result)
})
router.post('/replicache-pull', async (req, res) => {
const pull = req.body
const { list_id: listID } = req.query
console.log(`Processing pull`, JSON.stringify(pull))
const t0 = Date.now()
try {
await db.tx(async (t) => {
const lastMutationID = parseInt(
(
await t.oneOrNone(
'select last_mutation_id from replicache_clients where id = $1',
pull.clientID,
)
)?.last_mutation_id ?? '0',
)
const changed = await t.manyOrNone(
'select id, completed, content, ord, deleted, version from todos where list_id = $1',
listID,
)
const patch = []
const cookie = {}
if (pull.cookie == null) {
patch.push({ op: 'clear' })
}
changed.forEach(({ id, completed, content, ord, version, deleted }) => {
cookie[id] = version
const key = `todo/${id}`
if (pull.cookie == null || pull.cookie[id] !== version) {
if (deleted) {
patch.push({
op: 'del',
key,
})
} else {
patch.push({
op: 'put',
key,
value: {
id,
completed,
content,
order: ord,
},
})
}
}
})
res.json({ lastMutationID, cookie, patch })
res.end()
})
} catch (e) {
console.error(e)
res.status(500).send(e.toString())
} finally {
console.log('Processed pull in', Date.now() - t0)
}
})
router.post('/replicache-push', async (req, res) => {
const { list_id: listID } = req.query
const push = req.body
console.log('Processing push', JSON.stringify(push))
const t0 = Date.now()
try {
await db.tx(async (t) => {
let lastMutationID = await getLastMutationID(t, push.clientID)
for (const mutation of push.mutations) {
const t1 = Date.now()
const expectedMutationID = lastMutationID + 1
if (mutation.id < expectedMutationID) {
console.log(
`Mutation ${mutation.id} has already been processed - skipping`,
)
continue
}
if (mutation.id > expectedMutationID) {
console.warn(`Mutation ${mutation.id} is from the future - aborting`)
break
}
console.log('Processing mutation:', JSON.stringify(mutation))
switch (mutation.name) {
case 'createTodo':
await createTodo(t, mutation.args, listID)
break
case 'updateTodoCompleted':
await updateTodoCompleted(t, mutation.args)
break
case 'updateTodoOrder':
await updateTodoOrder(t, mutation.args)
break
case 'deleteTodo':
await deleteTodo(t, mutation.args)
break
default:
throw new Error(`Unknown mutation: ${mutation.name}`)
}
lastMutationID = expectedMutationID
console.log('Processed mutation in', Date.now() - t1)
}
const channel = ably.channels.get(`todos-of-${listID}`)
channel.publish('change', {})
console.log(
'setting',
push.clientID,
'last_mutation_id to',
lastMutationID,
)
await t.none(
'UPDATE replicache_clients SET last_mutation_id = $1 WHERE id = $2',
[lastMutationID, push.clientID],
)
res.send('{}')
})
} catch (e) {
console.error(e)
// This should be handled at the application level.
// We only deal with it at this route for simplicity's reason and because only this route *write* to the db.
if ((e.code = READ_ONLY_SQL_TRANSACTION)) {
const { PRIMARY_REGION: primary, FLY_REGION: current } = process.env
res.set('fly-replay', `region=${primary}`)
const replayMessage = `replaying from ${current} to ${primary}`
console.log(replayMessage)
res.status(409).send(replayMessage)
} else {
res.status(500).send(e.toString())
}
} finally {
console.log('Processed push in', Date.now() - t0)
}
})
async function getLastMutationID(t, clientID) {
const clientRow = await t.oneOrNone(
'SELECT last_mutation_id FROM replicache_clients WHERE id = $1',
clientID,
)
if (clientRow) {
return parseInt(clientRow.last_mutation_id)
}
await t.none(
'INSERT INTO replicache_clients (id, last_mutation_id) VALUES ($1, 0)',
clientID,
)
return 0
}
async function createTodo(t, { id, completed, content, order }, listID) {
await t.none(
`INSERT INTO todos (
id, completed, content, ord, list_id) values
($1, $2, $3, $4, $5)`,
[id, completed, content, order, listID],
)
}
async function updateTodoCompleted(t, { id, completed }) {
await t.none(
`UPDATE todos
SET completed = $2, version = gen_random_uuid()
WHERE id = $1
`,
[id, completed],
)
}
async function updateTodoOrder(t, { id, order }) {
await t.none(
`UPDATE todos
SET ord = $2, version = gen_random_uuid()
WHERE id = $1
`,
[id, order],
)
}
async function deleteTodo(t, { id }) {
await t.none(
`UPDATE todos
SET deleted = true, version = gen_random_uuid()
WHERE id = $1
`,
[id],
)
}