create-db 1.0.3-pr48-DC-4894-posthog-fix-17267509345.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/analytics.js +9 -12
- package/index.js +76 -31
- package/package.json +1 -1
package/analytics.js
CHANGED
|
@@ -8,10 +8,11 @@ class EventCaptureError extends Error {
|
|
|
8
8
|
|
|
9
9
|
class PosthogEventCapture {
|
|
10
10
|
async capture(eventName, properties = {}) {
|
|
11
|
-
const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST
|
|
12
|
+
? process.env.POSTHOG_API_HOST + "/capture"
|
|
13
|
+
: "https://proxyhog.prisma-data.net/capture";
|
|
14
|
+
const POSTHOG_KEY = process.env.POSTHOG_API_KEY || "phc_cmc85avbWyuJ2JyKdGPdv7dxXli8xLdWDBPbvIXWJfs";
|
|
15
|
+
|
|
15
16
|
const payload = {
|
|
16
17
|
api_key: POSTHOG_KEY,
|
|
17
18
|
event: eventName,
|
|
@@ -34,15 +35,11 @@ class PosthogEventCapture {
|
|
|
34
35
|
if (!response.ok) {
|
|
35
36
|
throw new EventCaptureError(eventName, response.statusText);
|
|
36
37
|
}
|
|
37
|
-
|
|
38
|
-
// Log success message
|
|
39
|
-
console.log(`${eventName}: Success`);
|
|
40
38
|
} catch (error) {
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
throw error;
|
|
39
|
+
// Silently fail analytics to not disrupt user experience
|
|
40
|
+
if (process.env.NODE_ENV === "development") {
|
|
41
|
+
console.error("Analytics error:", error.message);
|
|
42
|
+
}
|
|
46
43
|
}
|
|
47
44
|
}
|
|
48
45
|
}
|
package/index.js
CHANGED
|
@@ -13,6 +13,74 @@ const CREATE_DB_WORKER_URL =
|
|
|
13
13
|
const CLAIM_DB_WORKER_URL =
|
|
14
14
|
process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
|
|
15
15
|
|
|
16
|
+
async function detectUserLocation() {
|
|
17
|
+
try {
|
|
18
|
+
const response = await fetch("https://ipapi.co/json/", {
|
|
19
|
+
method: "GET",
|
|
20
|
+
headers: {
|
|
21
|
+
"User-Agent": "create-db-cli/1.0",
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new Error(`Failed to fetch location data: ${response.status}`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const data = await response.json();
|
|
30
|
+
return {
|
|
31
|
+
country: data.country_code,
|
|
32
|
+
continent: data.continent_code,
|
|
33
|
+
city: data.city,
|
|
34
|
+
region: data.region,
|
|
35
|
+
latitude: data.latitude,
|
|
36
|
+
longitude: data.longitude,
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Region coordinates (latitude, longitude)
|
|
44
|
+
const REGION_COORDINATES = {
|
|
45
|
+
"ap-southeast-1": { lat: 1.3521, lng: 103.8198 }, // Singapore
|
|
46
|
+
"ap-northeast-1": { lat: 35.6762, lng: 139.6503 }, // Tokyo
|
|
47
|
+
"eu-central-1": { lat: 50.1109, lng: 8.6821 }, // Frankfurt
|
|
48
|
+
"eu-west-3": { lat: 48.8566, lng: 2.3522 }, // Paris
|
|
49
|
+
"us-east-1": { lat: 38.9072, lng: -77.0369 }, // N. Virginia
|
|
50
|
+
"us-west-1": { lat: 37.7749, lng: -122.4194 }, // N. California
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function getRegionClosestToLocation(userLocation) {
|
|
54
|
+
if (!userLocation) return null;
|
|
55
|
+
|
|
56
|
+
const userLat = parseFloat(userLocation.latitude);
|
|
57
|
+
const userLng = parseFloat(userLocation.longitude);
|
|
58
|
+
|
|
59
|
+
let closestRegion = null;
|
|
60
|
+
let minDistance = Infinity;
|
|
61
|
+
|
|
62
|
+
for (const [region, coordinates] of Object.entries(REGION_COORDINATES)) {
|
|
63
|
+
// Simple distance calculation using Haversine formula
|
|
64
|
+
const latDiff = ((userLat - coordinates.lat) * Math.PI) / 180;
|
|
65
|
+
const lngDiff = ((userLng - coordinates.lng) * Math.PI) / 180;
|
|
66
|
+
const a =
|
|
67
|
+
Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
|
|
68
|
+
Math.cos((userLat * Math.PI) / 180) *
|
|
69
|
+
Math.cos((coordinates.lat * Math.PI) / 180) *
|
|
70
|
+
Math.sin(lngDiff / 2) *
|
|
71
|
+
Math.sin(lngDiff / 2);
|
|
72
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
73
|
+
const distance = 6371 * c; // Earth radius in km
|
|
74
|
+
|
|
75
|
+
if (distance < minDistance) {
|
|
76
|
+
minDistance = distance;
|
|
77
|
+
closestRegion = region;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return closestRegion;
|
|
82
|
+
}
|
|
83
|
+
|
|
16
84
|
async function listRegions() {
|
|
17
85
|
try {
|
|
18
86
|
const regions = await getRegions();
|
|
@@ -269,9 +337,7 @@ async function promptForRegion(defaultRegion) {
|
|
|
269
337
|
region: region,
|
|
270
338
|
"selection-method": "interactive",
|
|
271
339
|
});
|
|
272
|
-
} catch (error) {
|
|
273
|
-
console.error("Failed to send region_selected analytics :", error.message);
|
|
274
|
-
}
|
|
340
|
+
} catch (error) {}
|
|
275
341
|
|
|
276
342
|
return region;
|
|
277
343
|
}
|
|
@@ -312,12 +378,7 @@ async function createDatabase(name, region, returnJson = false) {
|
|
|
312
378
|
"error-type": "rate_limit",
|
|
313
379
|
"status-code": 429,
|
|
314
380
|
});
|
|
315
|
-
} catch (error) {
|
|
316
|
-
console.error(
|
|
317
|
-
"Failed to send database_creation_failed analytics:",
|
|
318
|
-
error.message
|
|
319
|
-
);
|
|
320
|
-
}
|
|
381
|
+
} catch (error) {}
|
|
321
382
|
|
|
322
383
|
process.exit(1);
|
|
323
384
|
}
|
|
@@ -346,12 +407,7 @@ async function createDatabase(name, region, returnJson = false) {
|
|
|
346
407
|
"error-type": "invalid_json",
|
|
347
408
|
"status-code": resp.status,
|
|
348
409
|
});
|
|
349
|
-
} catch
|
|
350
|
-
console.error(
|
|
351
|
-
"Failed to send database_creation_failed analytics:",
|
|
352
|
-
error.message
|
|
353
|
-
);
|
|
354
|
-
}
|
|
410
|
+
} catch {}
|
|
355
411
|
process.exit(1);
|
|
356
412
|
}
|
|
357
413
|
|
|
@@ -415,12 +471,7 @@ async function createDatabase(name, region, returnJson = false) {
|
|
|
415
471
|
"error-type": "api_error",
|
|
416
472
|
"error-message": result.error.message,
|
|
417
473
|
});
|
|
418
|
-
} catch (error) {
|
|
419
|
-
console.error(
|
|
420
|
-
"Failed to send database_creation_failed analytics:",
|
|
421
|
-
error.message
|
|
422
|
-
);
|
|
423
|
-
}
|
|
474
|
+
} catch (error) {}
|
|
424
475
|
process.exit(1);
|
|
425
476
|
}
|
|
426
477
|
|
|
@@ -491,9 +542,7 @@ async function main() {
|
|
|
491
542
|
platform: process.platform,
|
|
492
543
|
arch: process.arch,
|
|
493
544
|
});
|
|
494
|
-
} catch (error) {
|
|
495
|
-
console.error("Failed to send cli_command_ran analytics:", error.message);
|
|
496
|
-
}
|
|
545
|
+
} catch (error) {}
|
|
497
546
|
|
|
498
547
|
const { flags } = await parseArgs();
|
|
499
548
|
|
|
@@ -502,7 +551,8 @@ async function main() {
|
|
|
502
551
|
}
|
|
503
552
|
|
|
504
553
|
let name = new Date().toISOString();
|
|
505
|
-
let
|
|
554
|
+
let userLocation = await detectUserLocation();
|
|
555
|
+
let region = getRegionClosestToLocation(userLocation) || "us-east-1";
|
|
506
556
|
let chooseRegionPrompt = false;
|
|
507
557
|
|
|
508
558
|
if (flags.help) {
|
|
@@ -523,12 +573,7 @@ async function main() {
|
|
|
523
573
|
region: region,
|
|
524
574
|
"selection-method": "flag",
|
|
525
575
|
});
|
|
526
|
-
} catch (error) {
|
|
527
|
-
console.error(
|
|
528
|
-
"Failed to send region_selected analytics:",
|
|
529
|
-
error.message
|
|
530
|
-
);
|
|
531
|
-
}
|
|
576
|
+
} catch (error) {}
|
|
532
577
|
}
|
|
533
578
|
|
|
534
579
|
if (flags.interactive) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-db",
|
|
3
|
-
"version": "1.0.3
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "",
|