-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add makeSandeboxEndpoint function
- Loading branch information
1 parent
badfefa
commit 163dcb3
Showing
5 changed files
with
104 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,11 @@ | |
"version": "0.0.0", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"repository": | ||
"[email protected]:graphql-boilerplates/graphql-boilerplate-install.git", | ||
"repository": "[email protected]:graphql-boilerplates/graphql-boilerplate-install.git", | ||
"author": "Johannes Schickling <[email protected]>", | ||
"contributors": ["Kim Brandwijk <[email protected]>"], | ||
"contributors": [ | ||
"Kim Brandwijk <[email protected]>" | ||
], | ||
"license": "MIT", | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
|
@@ -18,7 +19,9 @@ | |
}, | ||
"dependencies": { | ||
"cross-spawn": "5.1.0", | ||
"npm-run": "4.1.2" | ||
"node-fetch": "^2.1.2", | ||
"npm-run": "4.1.2", | ||
"sillyname": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "8.5.7", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { replaceInFile, replaceInFiles } from './utils' | ||
export { deploy, writeEnv, getInfo } from './prisma' | ||
export { deploy, writeEnv, getInfo, makeSandboxEndpoint } from './prisma' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as fetch from 'node-fetch' | ||
export type Region = 'EU_WEST_1' | 'US_WEST_2' | ||
|
||
const regionMap = { | ||
EU_WEST_1: 'eu1', | ||
US_WEST_2: 'us1', | ||
} | ||
|
||
async function runPing(url: string): Promise<number> { | ||
const pingUrl = async () => { | ||
const start = Date.now() | ||
|
||
if (process.env.NODE_ENV !== 'test') { | ||
await fetch(url) | ||
} | ||
|
||
return Date.now() - start | ||
} | ||
const pings = await Promise.all([0, 0].map(pingUrl)) | ||
|
||
return sum(pings) / pings.length | ||
} | ||
|
||
export const regions: Region[] = ['EU_WEST_1', 'US_WEST_2'] | ||
|
||
export async function getFastestRegion(): Promise<string> { | ||
const pingResults = await Promise.all( | ||
regions.map(async (region: Region) => { | ||
const ping = await runPing(getPingUrl(region)) | ||
return { | ||
region, | ||
ping, | ||
} | ||
}), | ||
) | ||
|
||
const fastestRegion: { region: Region; ping: number } = pingResults.reduce( | ||
(min, curr) => { | ||
if (curr.ping < min.ping) { | ||
return curr | ||
} | ||
return min | ||
}, | ||
{ region: 'EU_WEST_1', ping: Infinity }, | ||
) | ||
|
||
return regionMap[fastestRegion.region] | ||
} | ||
|
||
export const getDynamoUrl = (region: string) => | ||
`http://dynamodb.${region.toLowerCase().replace(/_/g, '-')}.amazonaws.com` | ||
|
||
const getPingUrl = (region: string) => | ||
`${getDynamoUrl(region)}/ping?x=${randomString()}` | ||
|
||
function sum(arr) { | ||
return arr.reduce((acc, curr) => acc + curr, 0) | ||
} | ||
|
||
function randomString() { | ||
return Math.random() | ||
.toString(36) | ||
.substring(7) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters