att-client
Advanced tools
Comparing version 0.2.24 to 0.2.25
@@ -5,5 +5,5 @@ export type { ApiRequest } from './ApiRequest'; | ||
export type { HttpMethod } from './HttpMethod'; | ||
export type { GroupInfo, GroupMemberInfo, GroupRoleInfo, GroupServerInfo, InvitedGroupInfo, JoinedGroupInfo, ServerConnectionInfo, ServerInfo } from './schemas'; | ||
export type { GroupInfo, GroupMemberInfo, GroupRoleInfo, GroupServerInfo, InvitedGroupInfo, JoinedGroupInfo, ServerConnectionInfo, ServerFleet, ServerInfo } from './schemas'; | ||
export { Api } from './Api'; | ||
export { Endpoint } from './Endpoint'; | ||
export { HttpResponseCode } from './HttpResponseCode'; |
@@ -8,2 +8,2 @@ export type { GroupInfo } from './GroupInfo'; | ||
export type { ServerConnectionInfo } from './ServerConnectionInfo'; | ||
export type { ServerInfo } from './ServerInfo'; | ||
export type { ServerFleet, ServerInfo } from './ServerInfo'; |
@@ -5,2 +5,3 @@ declare type ServerOnlinePlayers = { | ||
}; | ||
export declare type ServerFleet = 'att-release' | 'att-quest'; | ||
export declare type ServerInfo = { | ||
@@ -24,5 +25,5 @@ id: number; | ||
type: string; | ||
fleet: string; | ||
fleet: ServerFleet; | ||
up_time: string; | ||
}; | ||
export {}; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const constants_1 = require("../constants"); | ||
const Workers_1 = require("../Workers"); | ||
class Client extends tiny_typed_emitter_1.TypedEmitter { | ||
@@ -51,2 +52,3 @@ constructor(config) { | ||
logVerbosity: configuredLogVerbosity, | ||
maxWorkerConcurrency: config.maxWorkerConcurrency ?? constants_1.DEFAULTS.maxWorkerConcurrency, | ||
scope: config.scope, | ||
@@ -56,2 +58,3 @@ restBaseUrl: config.restBaseUrl ?? constants_1.DEFAULTS.restBaseUrl, | ||
serverHeartbeatTimeout: config.serverHeartbeatTimeout ?? constants_1.DEFAULTS.serverHeartbeatTimeout, | ||
supportedServerFleets: config.supportedServerFleets ?? constants_1.DEFAULTS.supportedServerFleets, | ||
tokenUrl: config.tokenUrl ?? constants_1.DEFAULTS.tokenUrl, | ||
@@ -140,3 +143,5 @@ webSocketMigrationHandoverPeriod: config.webSocketMigrationHandoverPeriod ?? constants_1.DEFAULTS.webSocketMigrationHandoverPeriod, | ||
this.logger.info(`Managing ${joinedGroups.length} group${joinedGroups.length > 1 ? 's' : ''}.`); | ||
await Promise.all(joinedGroups.map(({ group, member }) => this.addGroup(group, member))); | ||
const tasks = joinedGroups.map(({ group, member }) => () => this.addGroup(group, member)); | ||
const workers = new Workers_1.Workers(this.config.maxWorkerConcurrency); | ||
await workers.do(tasks); | ||
} | ||
@@ -147,3 +152,5 @@ /* Accept pending group invites. */ | ||
this.logger.info(`Accepting ${invites.length} pending group invite${invites.length > 1 ? 's' : ''}.`); | ||
await Promise.all(invites.map(async (invite) => this.api.acceptGroupInvite(invite.id))); | ||
const tasks = invites.map(invite => () => this.api.acceptGroupInvite(invite.id)); | ||
const workers = new Workers_1.Workers(this.config.maxWorkerConcurrency); | ||
await workers.do(tasks); | ||
} | ||
@@ -248,3 +255,3 @@ } | ||
*/ | ||
addGroup(group, member) { | ||
async addGroup(group, member) { | ||
this.logger.debug(`Adding group ${group.id} (${group.name}).`); | ||
@@ -251,0 +258,0 @@ if (Object.keys(this.groups).map(Number).includes(group.id)) { |
@@ -0,1 +1,2 @@ | ||
import type { ServerFleet } from '../Api'; | ||
import type { Verbosity } from '../Logger'; | ||
@@ -10,2 +11,3 @@ export declare type Scope = 'group.info' | 'group.invite' | 'group.join' | 'group.leave' | 'group.members' | 'group.view' | 'server.console' | 'server.view' | 'ws.group' | 'ws.group_bans' | 'ws.group_invites' | 'ws.group_members' | 'ws.group_servers'; | ||
logVerbosity?: Verbosity; | ||
maxWorkerConcurrency?: number; | ||
restBaseUrl?: string; | ||
@@ -15,2 +17,3 @@ scope: Scope[]; | ||
serverHeartbeatTimeout?: number; | ||
supportedServerFleets?: ServerFleet[]; | ||
tokenUrl?: string; | ||
@@ -17,0 +20,0 @@ webSocketMigrationHandoverPeriod?: number; |
@@ -7,5 +7,7 @@ "use strict"; | ||
const MINUTE = 60 * SECOND; | ||
const MAX_WORKER_CONCURRENCY = 25; | ||
const REST_BASE_URL = 'https://967phuchye.execute-api.ap-southeast-2.amazonaws.com/prod/api'; | ||
const SERVER_CONNECTION_RECOVERY_DELAY = 10 * SECOND; | ||
const SERVER_HEARTBEAT_TIMEOUT = 10 * MINUTE; | ||
const SUPPORTED_SERVER_FLEETS = ['att-release', 'att-quest']; | ||
const TOKEN_URL = 'https://accounts.townshiptale.com/connect/token'; | ||
@@ -28,4 +30,6 @@ const WEBSOCKET_MIGRATION_HANDOVER_PERIOD = 10 * SECOND; | ||
restBaseUrl: REST_BASE_URL, | ||
maxWorkerConcurrency: MAX_WORKER_CONCURRENCY, | ||
serverConnectionRecoveryDelay: SERVER_CONNECTION_RECOVERY_DELAY, | ||
serverHeartbeatTimeout: SERVER_HEARTBEAT_TIMEOUT, | ||
supportedServerFleets: SUPPORTED_SERVER_FLEETS, | ||
tokenUrl: TOKEN_URL, | ||
@@ -32,0 +36,0 @@ webSocketMigrationHandoverPeriod: WEBSOCKET_MIGRATION_HANDOVER_PERIOD, |
@@ -32,3 +32,3 @@ import type { GroupInfo, GroupMemberInfo } from '../Api/schemas'; | ||
*/ | ||
init(): Promise<void[]>; | ||
init(): Promise<void>; | ||
/** | ||
@@ -35,0 +35,0 @@ * Updates all managed servers with new server info. |
@@ -101,3 +101,3 @@ "use strict"; | ||
]); | ||
return this.updateServers(); | ||
await this.updateServers(); | ||
} | ||
@@ -167,3 +167,5 @@ /** | ||
} | ||
const mayConnect = this.permissions.includes('Console'); | ||
const hasConsolePermission = this.permissions.includes('Console'); | ||
const isSupportedServerFleet = this.client.config.supportedServerFleets.includes(server.fleet); | ||
const mayConnect = hasConsolePermission && isSupportedServerFleet; | ||
const lastHeartbeatAt = +new Date(status.online_ping ?? '2022-06-01T00:00:00.000Z'); | ||
@@ -170,0 +172,0 @@ const timeSinceLastHeartbeat = Date.now() - lastHeartbeatAt; |
@@ -1,2 +0,2 @@ | ||
export type { ApiRequest, ApiResponse, DecodedToken, HttpMethod, HttpResponseCode, GroupInfo, GroupMemberInfo, GroupRoleInfo, GroupServerInfo, InvitedGroupInfo, JoinedGroupInfo, ServerConnectionInfo, ServerInfo } from './Api'; | ||
export type { ApiRequest, ApiResponse, DecodedToken, HttpMethod, HttpResponseCode, GroupInfo, GroupMemberInfo, GroupRoleInfo, GroupServerInfo, InvitedGroupInfo, JoinedGroupInfo, ServerConnectionInfo, ServerInfo, ServerFleet } from './Api'; | ||
export type { Config, Scope } from './Client'; | ||
@@ -10,1 +10,2 @@ export type { CommandResultMessage, SubscriptionEvent, SubscriptionEventMessage } from './ServerConnection'; | ||
export { ServerConnection } from './ServerConnection'; | ||
export { Workers } from './Workers'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ServerConnection = exports.Logger = exports.Server = exports.Group = exports.Client = exports.Api = void 0; | ||
exports.Workers = exports.ServerConnection = exports.Logger = exports.Server = exports.Group = exports.Client = exports.Api = void 0; | ||
var Api_1 = require("./Api"); | ||
@@ -16,1 +16,3 @@ Object.defineProperty(exports, "Api", { enumerable: true, get: function () { return Api_1.Api; } }); | ||
Object.defineProperty(exports, "ServerConnection", { enumerable: true, get: function () { return ServerConnection_1.ServerConnection; } }); | ||
var Workers_1 = require("./Workers"); | ||
Object.defineProperty(exports, "Workers", { enumerable: true, get: function () { return Workers_1.Workers; } }); |
@@ -1,2 +0,2 @@ | ||
import type { ServerInfo } from '../Api'; | ||
import type { ServerFleet, ServerInfo } from '../Api'; | ||
import type { Group } from '../Group'; | ||
@@ -19,3 +19,3 @@ import { TypedEmitter } from 'tiny-typed-emitter'; | ||
status: 'disconnected' | 'connecting' | 'connected'; | ||
fleet: string; | ||
fleet: ServerFleet; | ||
private api; | ||
@@ -22,0 +22,0 @@ private connection?; |
@@ -117,2 +117,3 @@ "use strict"; | ||
this.players = status.online_players; | ||
this.fleet = status.fleet; | ||
this.emit('update', this); | ||
@@ -119,0 +120,0 @@ } |
@@ -65,3 +65,7 @@ /// <reference types="node" /> | ||
dispose(): void; | ||
/** | ||
* Alias for ServerConnection.server.disconnect(). | ||
*/ | ||
disconnect(): void; | ||
} | ||
export {}; |
@@ -157,3 +157,9 @@ "use strict"; | ||
} | ||
/** | ||
* Alias for ServerConnection.server.disconnect(). | ||
*/ | ||
disconnect() { | ||
this.server.disconnect(); | ||
} | ||
} | ||
exports.ServerConnection = ServerConnection; |
@@ -7,2 +7,3 @@ "use strict"; | ||
require("../Api"); | ||
const Workers_1 = require("../Workers"); | ||
class Subscriptions { | ||
@@ -278,12 +279,14 @@ constructor(client) { | ||
/* Resubscribe to all saved subscriptions. */ | ||
const workers = new Workers_1.Workers(this.client.config.maxWorkerConcurrency); | ||
const tasks = Object.entries(subscriptions).map(([entry, callback]) => () => { | ||
const [subscription, key] = entry.split('/'); | ||
if (typeof subscription !== 'string' || typeof key !== 'string') | ||
return Promise.resolve(); | ||
this.events.removeAllListeners(entry); | ||
return (this.subscribe(subscription, key, callback) ?? | ||
Promise.reject(new Error(`WebSocket recovery failed! Resubscribing to ${entry} was unsuccessful. Retrying recovery in ${this.client.config.webSocketRecoveryRetryDelay} ms.`))); | ||
}); | ||
try { | ||
await Promise.race([ | ||
Promise.all(Object.entries(subscriptions).map(([entry, callback]) => { | ||
const [subscription, key] = entry.split('/'); | ||
if (typeof subscription !== 'string' || typeof key !== 'string') | ||
return Promise.resolve(); | ||
this.events.removeAllListeners(entry); | ||
return (this.subscribe(subscription, key, callback) ?? | ||
Promise.reject(new Error(`WebSocket recovery failed! Resubscribing to ${entry} was unsuccessful. Retrying recovery in ${this.client.config.webSocketRecoveryRetryDelay} ms.`))); | ||
})), | ||
await workers.do(tasks), | ||
new Promise((_, reject) => setTimeout(() => { | ||
@@ -290,0 +293,0 @@ reject(new Error(`WebSocket recovery failed! Resubscribing was unsuccessful within ${this.client.config.webSocketRecoveryTimeout} ms. Retrying recovery in ${this.client.config.webSocketRecoveryRetryDelay} ms.`)); |
{ | ||
"name": "att-client", | ||
"version": "0.2.24", | ||
"version": "0.2.25", | ||
"description": "Node bot library for A Township Tale, a VR game by Alta Reality Pty Ltd.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
108375
87
2519