Skip to content

Commit

Permalink
feat: add sdk_version to HANDSHAKE message (#262)
Browse files Browse the repository at this point in the history
* feat: add sdk_version to HANDSHAKE message

* fix: remove try/catch

* fix: fix the handshake test

* fix: check mobileVersion against minimum for handshake sdk

* fix: remove semver, manually parse and compare major version from mobile_app_version

* chore: update interface & mock to have mobileAppVersion and sdkVersion

* fix: return UNKNOWN_VERSION_NUMBER when cannot parse
  • Loading branch information
afgiel authored Oct 1, 2024
1 parent 5c2f6cb commit 86a3a17
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 90 deletions.
147 changes: 72 additions & 75 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@rollup/plugin-commonjs": "^26.0.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@rollup/plugin-json": "^6.1.0",
"@types/events": "^3.0.0",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^8.6.0",
Expand Down
4 changes: 3 additions & 1 deletion rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import {join} from 'path';

const srcDir = join(__dirname, 'src');
Expand Down Expand Up @@ -53,8 +54,9 @@ function buildConfig(format) {
declaration: true,
outDir: outDir,
}),
json(),
],
};
}

export default [buildConfig('cjs'), buildConfig('esm')];
export default [buildConfig('cjs'), buildConfig('esm')];
3 changes: 3 additions & 0 deletions src/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ export const Permissions = Object.freeze({
CREATE_INSTANT_INVITE: BigFlagUtils.getFlag(0),
ADMINISTRATOR: BigFlagUtils.getFlag(3),
});

export const UNKNOWN_VERSION_NUMBER = -1;
export const HANDSHAKE_SDK_VERSION_MINIUM_MOBILE_VERSION = 250;
54 changes: 41 additions & 13 deletions src/Discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import commands, {Commands} from './commands';
import {v4 as uuidv4} from 'uuid';
import {SDKError} from './error';
import {EventSchema, ERROR, Events as RPCEvents} from './schema/events';
import {Platform, RPCCloseCodes} from './Constants';
import {
Platform,
RPCCloseCodes,
HANDSHAKE_SDK_VERSION_MINIUM_MOBILE_VERSION,
UNKNOWN_VERSION_NUMBER,
} from './Constants';
import getDefaultSdkConfiguration from './utils/getDefaultSdkConfiguration';
import {ConsoleLevel, consoleLevels, wrapConsoleMethod} from './utils/console';
import type {TSendCommand, TSendCommandPayload} from './schema/types';
import {IDiscordSDK, MaybeZodObjectArray, SdkConfiguration} from './interface';
import {version as sdkVersion} from '../package.json';

export enum Opcodes {
HANDSHAKE = 0,
Expand Down Expand Up @@ -46,12 +52,22 @@ function getRPCServerSource(): [Window, string] {
return [window.parent.opener ?? window.parent, !!document.referrer ? document.referrer : '*'];
}

interface HandshakePayload {
v: number;
encoding: string;
client_id: string;
frame_id: string;
sdk_version?: string;
}

export class DiscordSDK implements IDiscordSDK {
readonly clientId: string;
readonly instanceId: string;
readonly platform: Platform;
readonly guildId: string | null;
readonly channelId: string | null;
readonly sdkVersion: string = sdkVersion;
readonly mobileAppVersion: string | null = null;
readonly configuration: SdkConfiguration;
readonly source: Window | WindowProxy | null = null;
readonly sourceOrigin: string = '';
Expand Down Expand Up @@ -135,6 +151,8 @@ export class DiscordSDK implements IDiscordSDK {

this.guildId = urlParams.get('guild_id');
this.channelId = urlParams.get('channel_id');

this.mobileAppVersion = urlParams.get('mobile_app_version');
// END Capture URL Query Params

[this.source, this.sourceOrigin] = getRPCServerSource();
Expand Down Expand Up @@ -195,19 +213,29 @@ export class DiscordSDK implements IDiscordSDK {
}
}

private parseMajorMobileVersion(): number {
if (this.mobileAppVersion && this.mobileAppVersion.includes('.')) {
try {
return parseInt(this.mobileAppVersion.split('.')[0]);
} catch {
return UNKNOWN_VERSION_NUMBER;
}
}
return UNKNOWN_VERSION_NUMBER;
}

private handshake() {
this.source?.postMessage(
[
Opcodes.HANDSHAKE,
{
v: 1,
encoding: 'json',
client_id: this.clientId,
frame_id: this.frameId,
},
],
this.sourceOrigin,
);
const handshakePayload: HandshakePayload = {
v: 1,
encoding: 'json',
client_id: this.clientId,
frame_id: this.frameId,
};
const majorMobileVersion = this.parseMajorMobileVersion();
if (this.platform === Platform.DESKTOP || majorMobileVersion >= HANDSHAKE_SDK_VERSION_MINIUM_MOBILE_VERSION) {
handshakePayload['sdk_version'] = this.sdkVersion;
}
this.source?.postMessage([Opcodes.HANDSHAKE, handshakePayload], this.sourceOrigin);
}

private addOnReadyListener() {
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/discordSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import {Opcodes} from '../Discord';
import {DiscordSDK, Events, Platform} from '../index';
import {DISPATCH} from '../schema/common';
import {version as sdkVersion} from '../../package.json';

describe('DiscordSDK', () => {
it('Can be constructed and await a "ready" event', async () => {
Expand Down Expand Up @@ -48,6 +49,7 @@ describe('DiscordSDK', () => {
encoding: 'json',
frame_id: frame_id,
v: 1,
sdk_version: sdkVersion,
},
],
'*',
Expand Down
Loading

0 comments on commit 86a3a17

Please sign in to comment.