1- export const helloWorld = ( ) => {
2- return 'hewwo :3'
3- }
1+ interface InstanceBase {
2+ worldId : string
3+ name : string
4+ region ?: 'eu' | 'jp' | 'us' | 'use'
5+ /**
6+ * @deprecated Removed from VRChat instances
7+ */
8+ nonce ?: string
9+ }
10+
11+ interface InstancePublic extends InstanceBase {
12+ type : 'public'
13+ }
14+
15+ interface InstanceUser extends InstanceBase {
16+ type : 'friends+' | 'friends' | 'invite+' | 'invite'
17+ userId : string
18+ }
19+
20+ interface InstanceGroup extends InstanceBase {
21+ type : 'groupPublic' | 'group+' | 'group'
22+ groupId : string
23+ require18yo : boolean
24+ }
25+
26+ type Instance = InstancePublic | InstanceUser | InstanceGroup
27+
28+ /**
29+ * Parses the location
30+ * @param location location string
31+ * @returns obj with parsed location
32+ */
33+ export const parseLocation = ( location : string ) => {
34+
35+ if ( [ 'offline' , 'traveling' , 'private' , '' ] . includes ( location ) )
36+ return null ;
37+
38+ const [ worldId , instanceId ] = location . split ( ':' )
39+ return parseInstance ( worldId , instanceId )
40+ }
41+
42+ /**
43+ * Parses the instance
44+ * @param worldId id of the world
45+ * @param instanceId instance string
46+ * @returns obj with parsed instance
47+ */
48+ export const parseInstance = ( worldId : string , instanceId : string ) => {
49+ // parse instance string
50+ const instanceIdParts : { [ key : string ] : string | null } = Object . fromEntries (
51+ instanceId . split ( '~' ) . map ( ( x ) => {
52+ const regexRes = x . match ( / ^ (?< key > [ ^ \( ] + ) ( \( (?< value > [ ^ \) ] + ) \) ) ? $ / )
53+ if ( ! regexRes ?. groups ?. key ) {
54+ return [ ]
55+ }
56+ return [ regexRes . groups . key , regexRes . groups . value || null ]
57+ } ) ,
58+ )
59+
60+ const instanceName = Object . keys ( instanceIdParts ) . shift ( )
61+ if ( ! instanceName ) {
62+ return null
63+ }
64+
65+ // if nothing is set its public
66+ let instance : Instance = {
67+ worldId : worldId ,
68+ name : instanceName ,
69+ type : 'public' ,
70+ }
71+
72+ if ( instanceIdParts . region ) {
73+ instance . region = < Instance [ 'region' ] > instanceIdParts . region
74+ }
75+
76+ if ( instanceIdParts . nonce ) {
77+ instance . nonce = instanceIdParts . nonce
78+ }
79+
80+ // hidden(usr_00000000-0000-0000-0000-000000000000)
81+ if ( instanceIdParts . hidden ) {
82+ instance = {
83+ ...instance ,
84+ type : 'friends+' ,
85+ userId : instanceIdParts . hidden ,
86+ }
87+ }
88+ // friends(usr_00000000-0000-0000-0000-000000000000)
89+ else if ( instanceIdParts . friends ) {
90+ instance = {
91+ ...instance ,
92+ type : 'friends' ,
93+ userId : instanceIdParts . friends ,
94+ }
95+ }
96+ // private(usr_00000000-0000-0000-0000-000000000000)
97+ // private(usr_00000000-0000-0000-0000-000000000000)~canRequestInvite
98+ else if ( instanceIdParts . private ) {
99+ instance = {
100+ ...instance ,
101+ type : instanceIdParts . canRequestInvite ? 'invite+' : 'invite' ,
102+ userId : instanceIdParts . private ,
103+ }
104+ }
105+ // group(grp_00000000-0000-0000-0000-000000000000)
106+ else if ( instanceIdParts . group ) {
107+ instance = {
108+ ...instance ,
109+ type : instanceIdParts . groupAccessType == 'public' ? 'groupPublic' : instanceIdParts . groupAccessType == 'plus' ? 'group+' : 'group' ,
110+ groupId : instanceIdParts . group ,
111+ require18yo : 'ageGate' in instanceIdParts
112+ }
113+ }
114+
115+ return instance
116+ }
0 commit comments