Skip to content

Commit 418d0b2

Browse files
committed
reg,index: catch exceptions sync w/ await
1 parent df4ff8d commit 418d0b2

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

src/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ async function handle(r, env, ctx) {
6565
if ((await admit(env, r)) === false) {
6666
return r429(`wsf: ${ray} rate limited`);
6767
}
68-
return forwardToWs(env, r);
68+
return await forwardToWs(env, r);
6969
}
7070

7171
if (path == null || path.length === 0) return r302(home);
@@ -97,14 +97,14 @@ async function handle(r, env, ctx) {
9797
// d; device registration
9898
// d/?did=hex&cid=hex[&test]
9999
// metadata as json in the body
100-
return registerDevice(env, r);
100+
return await registerDevice(env, r);
101101
} else if (p[1] === urlstripe) {
102102
// s; stripe webhook
103103
const whsec = env.STRIPE_WEBHOOK_SECRET;
104104
const apikey = env.STRIPE_API_KEY;
105105
const db = env.DB;
106106
// opt: p[2] === "checkout"
107-
return stripeCheckout(r, db, apikey, whsec);
107+
return await stripeCheckout(r, db, apikey, whsec);
108108
} else if (p[1] === urlgplay) {
109109
// g; play store subs rtdn at g/rtdn
110110
const p2 = p[2] ? p[2].toLowerCase() : "";
@@ -113,7 +113,7 @@ async function handle(r, env, ctx) {
113113
}
114114

115115
if (p2 === "rtdn") {
116-
return googlePlayNotification(env, r);
116+
return await googlePlayNotification(env, r);
117117
}
118118

119119
const vcode = url.searchParams.get("vcode");
@@ -133,30 +133,30 @@ async function handle(r, env, ctx) {
133133
// g/ack/[vcode]?cid&purchaseToken&vcode[&force&sku&test]
134134
if (r.method !== "POST")
135135
return r405(`g/ack: ${ray} method not allowed`);
136-
return googlePlayAcknowledgePurchase(env, r);
136+
return await googlePlayAcknowledgePurchase(env, r);
137137
} else if (p2 === "con") {
138138
// g/con/[vcode]?cid&purchaseToken&vcode[&sku&test]
139139
if (r.method !== "POST")
140140
return r405(`g/con: ${ray} method not allowed`);
141-
return googlePlayConsumePurchase(env, r);
141+
return await googlePlayConsumePurchase(env, r);
142142
} else if (p2 === "ent") {
143143
// TODO: mere possession of cid is auth, right now
144144
// will get entitlement for onetime purchase too, if &sku=onetime.tier
145145
// g/entitlements/[vcode]?cid&vcode&test[&sku]
146146
if (r.method !== "GET") return r405(`g/ent: ${ray} method not allowed`);
147-
return googlePlayGetEntitlements(env, r);
147+
return await googlePlayGetEntitlements(env, r);
148148
} else if (p2 === "stop") {
149149
// will refund and revoke onetime purchase, if &sku=onetime.tier
150150
// g/stop/[vcode]?cid&purchaseToken&vcode[&sku&test]
151151
if (r.method !== "POST")
152152
return r405(`g/stop: ${ray} method not allowed`);
153-
return cancelSubscription(env, r);
153+
return await cancelSubscription(env, r);
154154
} else if (p2 === "refund") {
155155
// will refund and revoke onetime purchase, if &sku=onetime.tier
156156
// g/refund/[vcode]?cid&purchaseToken&vcode[&sku&test]
157157
if (r.method !== "POST")
158158
return r405(`g/refund: ${ray} method not allowed`);
159-
return revokeSubscription(env, r);
159+
return await revokeSubscription(env, r);
160160
}
161161
return r400(`g: ${ray} unknown resource ${p2}`);
162162
} else if (p[1] === urlmoney1) {
@@ -165,13 +165,13 @@ async function handle(r, env, ctx) {
165165
const db = env.DB;
166166
const pubkeys = rsapubmodulus(env);
167167
// blindMsg
168-
return finalizeOrder(r, psk, pubkeys, db);
168+
return await finalizeOrder(r, psk, pubkeys, db);
169169
} else if (p[1] === urlmoney2) {
170170
// mt; token
171171
const psk = env.PRE_SHARED_KEY_SVC;
172172
const db = env.DB;
173173
// msg:rsaSig:sha256(rsaSig):hashedtoken(rand)
174-
return generateToken(r, psk, db);
174+
return await generateToken(r, psk, db);
175175
} else if (p[1] === urlsproxy) {
176176
// p; proxy metadata
177177
const clientVCode = p[2];
@@ -202,6 +202,7 @@ async function handle(r, env, ctx) {
202202
}
203203
}
204204
const svcs = svcstatus(env);
205+
// TODO: move json to class
205206
return r200j({
206207
vcode: clientVCode,
207208
minvcode: minVCodeNeeded,
@@ -591,8 +592,8 @@ function mustWsFwd(url) {
591592
}
592593

593594
/**
594-
* @param {any} env
595-
* @param {Request} r
595+
* @param {any} env - Worker environment
596+
* @param {Request} r - The incoming request
596597
* @returns {Promise<boolean>} - True if the request is allowed, false otherwise
597598
*/
598599
async function admit2(env, r) {

src/reg.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export async function registerDevice(env, req) {
3434

3535
if (
3636
emptyString(did) ||
37-
did.length <= mindidlength ||
37+
did.length < mindidlength ||
3838
!/^[a-fA-F0-9]+$/.test(did) ||
3939
emptyString(cid) ||
40-
cid.length <= mincidlength ||
40+
cid.length < mincidlength ||
4141
!/^[a-fA-F0-9]+$/.test(cid)
4242
) {
4343
return r400(`${ray} invalid identifiers`);
@@ -49,11 +49,18 @@ export async function registerDevice(env, req) {
4949
} catch (_) {
5050
// body missing or not valid JSON; proceed with null meta
5151
}
52-
const db = dbx.db2(env, test);
53-
const out = await dbx.upsertDevice(db, did, cid, meta || null, kindphone);
5452

55-
if (out == null || !out.success) {
56-
return r500(`database error: ${ray}`);
53+
try {
54+
const db = dbx.db2(env, test);
55+
const out = await dbx.upsertDevice(db, did, cid, meta || null, kindphone);
56+
57+
if (out == null || !out.success) {
58+
return r500(`database error: ${ray}`);
59+
}
60+
} catch (e) {
61+
// ex: Error: D1_ERROR: FOREIGN KEY constraint failed: SQLITE_CONSTRAINT
62+
log.e(ray, "registerDevice error:", e);
63+
return r500(`db error: ${e.message}`);
5764
}
5865

5966
log.d(ray, "register", did, "for c:", cid, "meta?", meta, "test?", test);
@@ -95,6 +102,7 @@ export async function retrieveDevices(env, cid, test, ray = "") {
95102
entry.ctime != null ? new Date(entry.ctime).toISOString() : null;
96103
const mtime =
97104
entry.mtime != null ? new Date(entry.mtime).toISOString() : null;
105+
// TODO: define json as class
98106
json.push({
99107
did: did.substring(0, 8), // partial
100108
meta: meta,

0 commit comments

Comments
 (0)