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