Skip to content

Commit f247c03

Browse files
committed
add parseLocation and parseInstance
1 parent e2f0250 commit f247c03

File tree

3 files changed

+163
-3
lines changed

3 files changed

+163
-3
lines changed

.github/workflows/push-to-npm.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Publish Package to npmjs
2+
on:
3+
release:
4+
types: [published]
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
permissions:
9+
contents: read
10+
id-token: write
11+
steps:
12+
- uses: actions/checkout@v4
13+
# Setup .npmrc file to publish to npm
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: '20.x'
17+
registry-url: 'https://registry.npmjs.org'
18+
cache: 'pnpm'
19+
- run: corepack enable
20+
- run: pnpm install --frozen-lockfile
21+
- run: pnpm build # Build TypeScript files
22+
- run: pnpm publish --provenance --access public
23+
env:
24+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
id-token: write
13+
steps:
14+
- uses: actions/checkout@v4
15+
# Setup .npmrc file to publish to npm
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: '20.x'
19+
registry-url: 'https://registry.npmjs.org'
20+
cache: 'pnpm'
21+
- run: corepack enable
22+
- run: pnpm install --frozen-lockfile
23+
- run: pnpm build # Build TypeScript files

src/index.ts

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,116 @@
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

Comments
 (0)