create-db 1.0.3-pr48-DC-4894-posthog-fix-17269175017.0 → 1.0.3-pr48-DC-4894-posthog-fix-17269833204.0
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/index.js +70 -1
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -33,6 +33,74 @@ const CREATE_DB_WORKER_URL =
|
|
|
33
33
|
const CLAIM_DB_WORKER_URL =
|
|
34
34
|
process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
|
|
35
35
|
|
|
36
|
+
async function detectUserLocation() {
|
|
37
|
+
try {
|
|
38
|
+
const response = await fetch("https://ipapi.co/json/", {
|
|
39
|
+
method: "GET",
|
|
40
|
+
headers: {
|
|
41
|
+
"User-Agent": "create-db-cli/1.0",
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!response.ok) {
|
|
46
|
+
throw new Error(`Failed to fetch location data: ${response.status}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const data = await response.json();
|
|
50
|
+
return {
|
|
51
|
+
country: data.country_code,
|
|
52
|
+
continent: data.continent_code,
|
|
53
|
+
city: data.city,
|
|
54
|
+
region: data.region,
|
|
55
|
+
latitude: data.latitude,
|
|
56
|
+
longitude: data.longitude,
|
|
57
|
+
};
|
|
58
|
+
} catch (error) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Region coordinates (latitude, longitude)
|
|
64
|
+
const REGION_COORDINATES = {
|
|
65
|
+
"ap-southeast-1": { lat: 1.3521, lng: 103.8198 }, // Singapore
|
|
66
|
+
"ap-northeast-1": { lat: 35.6762, lng: 139.6503 }, // Tokyo
|
|
67
|
+
"eu-central-1": { lat: 50.1109, lng: 8.6821 }, // Frankfurt
|
|
68
|
+
"eu-west-3": { lat: 48.8566, lng: 2.3522 }, // Paris
|
|
69
|
+
"us-east-1": { lat: 38.9072, lng: -77.0369 }, // N. Virginia
|
|
70
|
+
"us-west-1": { lat: 37.7749, lng: -122.4194 }, // N. California
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
function getRegionClosestToLocation(userLocation) {
|
|
74
|
+
if (!userLocation) return null;
|
|
75
|
+
|
|
76
|
+
const userLat = parseFloat(userLocation.latitude);
|
|
77
|
+
const userLng = parseFloat(userLocation.longitude);
|
|
78
|
+
|
|
79
|
+
let closestRegion = null;
|
|
80
|
+
let minDistance = Infinity;
|
|
81
|
+
|
|
82
|
+
for (const [region, coordinates] of Object.entries(REGION_COORDINATES)) {
|
|
83
|
+
// Simple distance calculation using Haversine formula
|
|
84
|
+
const latDiff = ((userLat - coordinates.lat) * Math.PI) / 180;
|
|
85
|
+
const lngDiff = ((userLng - coordinates.lng) * Math.PI) / 180;
|
|
86
|
+
const a =
|
|
87
|
+
Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
|
|
88
|
+
Math.cos((userLat * Math.PI) / 180) *
|
|
89
|
+
Math.cos((coordinates.lat * Math.PI) / 180) *
|
|
90
|
+
Math.sin(lngDiff / 2) *
|
|
91
|
+
Math.sin(lngDiff / 2);
|
|
92
|
+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
93
|
+
const distance = 6371 * c; // Earth radius in km
|
|
94
|
+
|
|
95
|
+
if (distance < minDistance) {
|
|
96
|
+
minDistance = distance;
|
|
97
|
+
closestRegion = region;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return closestRegion;
|
|
102
|
+
}
|
|
103
|
+
|
|
36
104
|
async function listRegions() {
|
|
37
105
|
try {
|
|
38
106
|
const regions = await getRegions();
|
|
@@ -515,7 +583,8 @@ async function main() {
|
|
|
515
583
|
}
|
|
516
584
|
|
|
517
585
|
let name = new Date().toISOString();
|
|
518
|
-
let
|
|
586
|
+
let userLocation = await detectUserLocation();
|
|
587
|
+
let region = getRegionClosestToLocation(userLocation) || "us-east-1";
|
|
519
588
|
let chooseRegionPrompt = false;
|
|
520
589
|
|
|
521
590
|
if (flags.help) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-db",
|
|
3
|
-
"version": "1.0.3-pr48-DC-4894-posthog-fix-
|
|
3
|
+
"version": "1.0.3-pr48-DC-4894-posthog-fix-17269833204.0",
|
|
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": "",
|