Path: blob/main/components/gitpod-protocol/src/util/generate-workspace-id.ts
2500 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import randomNumber = require("random-number-csprng");78export async function generateWorkspaceID(firstSegment?: string, secondSegment?: string): Promise<string> {9const firstSeg = clean(firstSegment) || (await random(colors));10const secSeg = clean(secondSegment) || (await random(animals));11function fit(makeFit: string, otherSeg: string) {12return makeFit.substring(0, Math.max(segLength, 2 * segLength - otherSeg.length));13}14return fit(firstSeg, secSeg) + "-" + fit(secSeg, firstSeg) + "-" + (await random(characters, segLength));15}1617function clean(segment: string | undefined, maxChars: number = 16) {18if (!segment) {19return undefined;20}21segment = segment.toLowerCase();22let result = "";23for (let i = 0; i < segment.length; i++) {24if (characters.indexOf(segment[i]) !== -1) {25result += segment[i];26}27}28if (result.length >= 2) {29return result.substring(0, maxChars);30}31}3233async function random(array: string[], length: number = 1): Promise<string> {34let result = "";35for (let i = 0; i < length; i++) {36result += array[await randomNumber(0, array.length - 1)];37}38return result;39}4041const segLength = 11;4243const characters = "abcdefghijklmnopqrstuvwxyz0123456789".split("");4445export const colors = [46"amaranth",47"amber",48"amethyst",49"apricot",50"aqua",51"aquamarine",52"azure",53"beige",54"black",55"blue",56"blush",57"bronze",58"brown",59"chocolate",60"coffee",61"copper",62"coral",63"crimson",64"cyan",65"emerald",66"fuchsia",67"gold",68"gray",69"green",70"harlequin",71"indigo",72"ivory",73"jade",74"kumquat",75"lavender",76"lime",77"magenta",78"maroon",79"moccasin",80"olive",81"orange",82"peach",83"pink",84"plum",85"purple",86"red",87"rose",88"salmon",89"sapphire",90"scarlet",91"silver",92"tan",93"teal",94"tomato",95"turquoise",96"violet",97"white",98"yellow",99];100101export const animals = [102"canidae",103"felidae",104"cat",105"cattle",106"dog",107"donkey",108"goat",109"horse",110"pig",111"rabbit",112"aardvark",113"aardwolf",114"albatross",115"alligator",116"alpaca",117"amphibian",118"anaconda",119"angelfish",120"anglerfish",121"ant",122"anteater",123"antelope",124"antlion",125"ape",126"aphid",127"armadillo",128"asp",129"baboon",130"badger",131"bandicoot",132"barnacle",133"barracuda",134"basilisk",135"bass",136"bat",137"bear",138"beaver",139"bedbug",140"bee",141"beetle",142"bird",143"bison",144"blackbird",145"boa",146"boar",147"bobcat",148"bobolink",149"bonobo",150"booby",151"bovid",152"bug",153"butterfly",154"buzzard",155"camel",156"canid",157"capybara",158"cardinal",159"caribou",160"carp",161"cat",162"catshark",163"caterpillar",164"catfish",165"cattle",166"centipede",167"cephalopod",168"chameleon",169"cheetah",170"chickadee",171"chicken",172"chimpanzee",173"chinchilla",174"chipmunk",175"clam",176"clownfish",177"cobra",178"cockroach",179"cod",180"condor",181"constrictor",182"coral",183"cougar",184"cow",185"coyote",186"crab",187"crane",188"crawdad",189"crayfish",190"cricket",191"crocodile",192"crow",193"cuckoo",194"cicada",195"damselfly",196"deer",197"dingo",198"dinosaur",199"dodo",200"dog",201"dolphin",202"donkey",203"dormouse",204"dove",205"dragonfly",206"dragon",207"duck",208"eagle",209"earthworm",210"earwig",211"echidna",212"eel",213"egret",214"elephant",215"elk",216"emu",217"ermine",218"falcon",219"ferret",220"finch",221"firefly",222"fish",223"flamingo",224"flea",225"fly",226"flyingfish",227"fowl",228"fox",229"frog",230"gamefowl",231"galliform",232"gazelle",233"gecko",234"gerbil",235"gibbon",236"giraffe",237"goat",238"goldfish",239"goose",240"gopher",241"gorilla",242"grasshopper",243"grouse",244"guan",245"guanaco",246"guineafowl",247"gull",248"guppy",249"haddock",250"halibut",251"hamster",252"hare",253"harrier",254"hawk",255"hedgehog",256"heron",257"herring",258"hippopotamus",259"hookworm",260"hornet",261"horse",262"hoverfly",263"hummingbird",264"hyena",265"iguana",266"impala",267"jackal",268"jaguar",269"jay",270"jellyfish",271"junglefowl",272"kangaroo",273"kingfisher",274"kite",275"kiwi",276"koala",277"koi",278"krill",279"ladybug",280"lamprey",281"landfowl",282"lark",283"leech",284"lemming",285"lemur",286"leopard",287"leopon",288"limpet",289"lion",290"lizard",291"llama",292"lobster",293"locust",294"loon",295"louse",296"lungfish",297"lynx",298"macaw",299"mackerel",300"magpie",301"mammal",302"manatee",303"mandrill",304"marlin",305"marmoset",306"marmot",307"marsupial",308"marten",309"mastodon",310"meadowlark",311"meerkat",312"mink",313"minnow",314"mite",315"mockingbird",316"mole",317"mollusk",318"mongoose",319"monkey",320"moose",321"mosquito",322"moth",323"mouse",324"mule",325"muskox",326"narwhal",327"newt",328"nightingale",329"ocelot",330"octopus",331"opossum",332"orangutan",333"orca",334"ostrich",335"otter",336"owl",337"ox",338"panda",339"panther",340"parakeet",341"parrot",342"parrotfish",343"partridge",344"peacock",345"peafowl",346"pelican",347"penguin",348"perch",349"pheasant",350"pig",351"pigeon",352"pike",353"pinniped",354"piranha",355"planarian",356"platypus",357"pony",358"porcupine",359"porpoise",360"possum",361"prawn",362"primate",363"ptarmigan",364"puffin",365"puma",366"python",367"quail",368"quelea",369"quokka",370"rabbit",371"raccoon",372"rat",373"rattlesnake",374"raven",375"reindeer",376"reptile",377"rhinoceros",378"roadrunner",379"rodent",380"rook",381"rooster",382"roundworm",383"sailfish",384"salamander",385"salmon",386"sawfish",387"scallop",388"scorpion",389"seahorse",390"shark",391"sheep",392"shrew",393"shrimp",394"silkworm",395"silverfish",396"skink",397"skunk",398"sloth",399"slug",400"smelt",401"snail",402"snake",403"snipe",404"sole",405"sparrow",406"spider",407"spoonbill",408"squid",409"squirrel",410"starfish",411"stingray",412"stoat",413"stork",414"sturgeon",415"swallow",416"swan",417"swift",418"swordfish",419"swordtail",420"tahr",421"takin",422"tapir",423"tarantula",424"tarsier",425"termite",426"tern",427"thrush",428"tick",429"tiger",430"tiglon",431"toad",432"tortoise",433"toucan",434"trout",435"tuna",436"turkey",437"turtle",438"tyrannosaurus",439"urial",440"vicuna",441"viper",442"vole",443"vulture",444"wallaby",445"walrus",446"wasp",447"warbler",448"weasel",449"whale",450"whippet",451"whitefish",452"wildcat",453"wildebeest",454"wildfowl",455"wolf",456"wolverine",457"wombat",458"woodpecker",459"worm",460"wren",461"xerinae",462"yak",463"zebra",464"alpaca",465"cat",466"cattle",467"chicken",468"dog",469"donkey",470"ferret",471"gayal",472"goldfish",473"guppy",474"horse",475"koi",476"llama",477"sheep",478"yak",479"unicorn",480];481482483