@@ -3,18 +3,22 @@ import config from '../config'
33import { AWClient , IEvent } from 'aw-client'
44import retry from 'p-retry'
55import { emitNotification , getBrowser , logHttpError } from './helpers'
6- import { getSyncStatus , setSyncStatus } from '../storage'
6+ import { getHostname , getSyncStatus , setSyncStatus } from '../storage'
77
88export const getClient = ( ) =>
99 new AWClient ( 'aw-client-web' , { testing : config . isDevelopment } )
1010
1111// TODO: We might want to get the hostname somehow, maybe like this:
1212// https://stackoverflow.com/questions/28223087/how-can-i-allow-firefox-or-chrome-to-read-a-pcs-hostname-or-other-assignable
13- export function ensureBucket ( client : AWClient , bucketId : string ) {
13+ export function ensureBucket (
14+ client : AWClient ,
15+ bucketId : string ,
16+ hostname : string ,
17+ ) {
1418 return retry (
1519 ( ) =>
1620 client
17- . ensureBucket ( bucketId , 'web.tab.current' , 'unknown' )
21+ . ensureBucket ( bucketId , 'web.tab.current' , hostname )
1822 . catch ( ( err ) => {
1923 console . error ( 'Failed to create bucket, retrying...' )
2024 logHttpError ( err )
@@ -24,13 +28,41 @@ export function ensureBucket(client: AWClient, bucketId: string) {
2428 )
2529}
2630
31+ export async function detectHostname ( client : AWClient ) {
32+ console . debug ( 'Attempting to detect hostname from server...' )
33+ return retry (
34+ ( ) => {
35+ console . debug ( 'Making request to server for hostname...' )
36+ return client . getInfo ( )
37+ } ,
38+ {
39+ retries : 3 ,
40+ onFailedAttempt : ( error ) => {
41+ console . warn (
42+ `Failed to detect hostname (attempt ${ error . attemptNumber } /${ error . retriesLeft + error . attemptNumber } ):` ,
43+ error . message ,
44+ )
45+ } ,
46+ } ,
47+ )
48+ . then ( ( info ) => {
49+ console . info ( 'Successfully detected hostname:' , info . hostname )
50+ return info . hostname
51+ } )
52+ . catch ( ( err ) => {
53+ console . error ( 'All attempts to detect hostname failed:' , err )
54+ return undefined
55+ } )
56+ }
57+
2758export async function sendHeartbeat (
2859 client : AWClient ,
2960 bucketId : string ,
3061 timestamp : Date ,
3162 data : IEvent [ 'data' ] ,
3263 pulsetime : number ,
3364) {
65+ const hostname = ( await getHostname ( ) ) ?? 'unknown'
3466 const syncStatus = await getSyncStatus ( )
3567 return retry (
3668 ( ) =>
@@ -41,7 +73,8 @@ export async function sendHeartbeat(
4173 } ) ,
4274 {
4375 retries : 3 ,
44- onFailedAttempt : ( ) => ensureBucket ( client , bucketId ) . then ( ( ) => { } ) ,
76+ onFailedAttempt : ( ) =>
77+ ensureBucket ( client , bucketId , hostname ) . then ( ( ) => { } ) ,
4578 } ,
4679 )
4780 . then ( ( ) => {
@@ -67,5 +100,10 @@ export async function sendHeartbeat(
67100
68101export const getBucketId = async ( ) : Promise < string > => {
69102 const browser = await getBrowser ( )
70- return `aw-watcher-web-${ browser } `
103+ const hostname = await getHostname ( )
104+ if ( hostname !== undefined ) {
105+ return `aw-watcher-web-${ browser } _${ hostname } `
106+ } else {
107+ return `aw-watcher-web-${ browser } `
108+ }
71109}
0 commit comments