@@ -4,6 +4,7 @@ import fs from 'fs';
44import { v4 as uuidv4 } from 'uuid' ;
55import { desc , eq } from 'drizzle-orm' ;
66import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3' ;
7+ import { isNumber , isObjectLike , isPlainObject , isString } from 'lodash-es' ;
78import { getCloudAccountsDbPath , getAntigravityDbPaths } from '../../utils/paths' ;
89import { logger } from '../../utils/logger' ;
910import {
@@ -35,14 +36,14 @@ type DrizzleExecutor = Pick<
3536> ;
3637
3738function isSqliteBusyError ( error : unknown ) : boolean {
38- if ( ! error || typeof error !== 'object' ) {
39+ if ( ! isObjectLike ( error ) ) {
3940 return false ;
4041 }
4142 const err = error as { code ?: string ; message ?: string } ;
4243 if ( err . code && SQLITE_BUSY_CODES . has ( err . code ) ) {
4344 return true ;
4445 }
45- if ( typeof err . message === 'string' ) {
46+ if ( isString ( err . message ) ) {
4647 return err . message . includes ( 'SQLITE_BUSY' ) || err . message . includes ( 'SQLITE_LOCKED' ) ;
4748 }
4849 return false ;
@@ -168,32 +169,29 @@ interface MigrationStats {
168169 failedFields : number ;
169170}
170171
171- function isRecord ( value : unknown ) : value is Record < string , unknown > {
172- return Boolean ( value ) && typeof value === 'object' && ! Array . isArray ( value ) ;
173- }
174-
175172function readStringCandidate (
176173 source : Record < string , unknown > ,
177174 ...keys : string [ ]
178175) : string | undefined {
179176 for ( const key of keys ) {
180177 const candidate = source [ key ] ;
181- if ( typeof candidate === 'string' && candidate . length > 0 ) {
178+ if ( isString ( candidate ) && candidate . length > 0 ) {
182179 return candidate ;
183180 }
184181 }
185182 return undefined ;
186183}
187184
188185function normalizeDeviceProfile ( value : unknown ) : DeviceProfile | undefined {
189- if ( ! isRecord ( value ) ) {
186+ if ( ! isPlainObject ( value ) ) {
190187 return undefined ;
191188 }
189+ const valueRecord = value as Record < string , unknown > ;
192190
193- const machineId = readStringCandidate ( value , 'machineId' , 'machine_id' ) ;
194- const macMachineId = readStringCandidate ( value , 'macMachineId' , 'mac_machine_id' ) ;
195- const devDeviceId = readStringCandidate ( value , 'devDeviceId' , 'dev_device_id' ) ;
196- const sqmId = readStringCandidate ( value , 'sqmId' , 'sqm_id' ) ;
191+ const machineId = readStringCandidate ( valueRecord , 'machineId' , 'machine_id' ) ;
192+ const macMachineId = readStringCandidate ( valueRecord , 'macMachineId' , 'mac_machine_id' ) ;
193+ const devDeviceId = readStringCandidate ( valueRecord , 'devDeviceId' , 'dev_device_id' ) ;
194+ const sqmId = readStringCandidate ( valueRecord , 'sqmId' , 'sqm_id' ) ;
197195
198196 if ( ! machineId || ! macMachineId || ! devDeviceId || ! sqmId ) {
199197 return undefined ;
@@ -217,45 +215,47 @@ function areDeviceProfilesEqual(left: DeviceProfile, right: DeviceProfile): bool
217215}
218216
219217function readVersionedProfilePayload ( value : unknown ) : unknown {
220- if ( ! isRecord ( value ) ) {
218+ if ( ! isPlainObject ( value ) ) {
221219 return value ;
222220 }
223- if ( ! ( 'schemaVersion' in value ) ) {
221+ const valueRecord = value as Record < string , unknown > ;
222+ if ( ! ( 'schemaVersion' in valueRecord ) ) {
224223 return value ;
225224 }
226225
227- const schemaVersion = value . schemaVersion ;
228- if ( typeof schemaVersion !== 'number' || ! Number . isFinite ( schemaVersion ) ) {
226+ const schemaVersion = valueRecord . schemaVersion ;
227+ if ( ! isNumber ( schemaVersion ) || ! Number . isFinite ( schemaVersion ) ) {
229228 throw new Error ( 'invalid_device_profile_schema_version' ) ;
230229 }
231230 if ( schemaVersion !== DEVICE_PAYLOAD_SCHEMA_VERSION ) {
232231 throw new Error ( `unsupported_device_profile_schema_version:${ schemaVersion } ` ) ;
233232 }
234- if ( ! ( 'profile' in value ) ) {
233+ if ( ! ( 'profile' in valueRecord ) ) {
235234 throw new Error ( 'invalid_device_profile_payload' ) ;
236235 }
237- return value . profile ;
236+ return valueRecord . profile ;
238237}
239238
240239function readVersionedHistoryPayload ( value : unknown ) : unknown {
241- if ( ! isRecord ( value ) ) {
240+ if ( ! isPlainObject ( value ) ) {
242241 return value ;
243242 }
244- if ( ! ( 'schemaVersion' in value ) ) {
243+ const valueRecord = value as Record < string , unknown > ;
244+ if ( ! ( 'schemaVersion' in valueRecord ) ) {
245245 return value ;
246246 }
247247
248- const schemaVersion = value . schemaVersion ;
249- if ( typeof schemaVersion !== 'number' || ! Number . isFinite ( schemaVersion ) ) {
248+ const schemaVersion = valueRecord . schemaVersion ;
249+ if ( ! isNumber ( schemaVersion ) || ! Number . isFinite ( schemaVersion ) ) {
250250 throw new Error ( 'invalid_device_history_schema_version' ) ;
251251 }
252252 if ( schemaVersion !== DEVICE_PAYLOAD_SCHEMA_VERSION ) {
253253 throw new Error ( `unsupported_device_history_schema_version:${ schemaVersion } ` ) ;
254254 }
255- if ( ! ( 'history' in value ) ) {
255+ if ( ! ( 'history' in valueRecord ) ) {
256256 throw new Error ( 'invalid_device_history_payload' ) ;
257257 }
258- return value . history ;
258+ return valueRecord . history ;
259259}
260260
261261function serializeDeviceProfile ( profile : DeviceProfile | undefined ) : string | null {
@@ -285,23 +285,25 @@ function normalizeDeviceHistory(value: unknown): DeviceProfileVersion[] | undefi
285285
286286 const normalized : DeviceProfileVersion [ ] = [ ] ;
287287 for ( const item of value ) {
288- if ( ! isRecord ( item ) ) {
288+ if ( ! isPlainObject ( item ) ) {
289289 continue ;
290290 }
291+ const itemRecord = item as Record < string , unknown > ;
291292
292- const profile = normalizeDeviceProfile ( item . profile ) ;
293+ const profile = normalizeDeviceProfile ( itemRecord . profile ) ;
293294 if ( ! profile ) {
294295 continue ;
295296 }
296297
297- const id = typeof item . id === 'string' && item . id . length > 0 ? item . id : uuidv4 ( ) ;
298- const createdAtCandidate = item . createdAt ;
298+ const id = isString ( itemRecord . id ) && itemRecord . id . length > 0 ? itemRecord . id : uuidv4 ( ) ;
299+ const createdAtCandidate = itemRecord . createdAt ;
299300 const createdAt =
300- typeof createdAtCandidate === 'number' && Number . isFinite ( createdAtCandidate )
301+ isNumber ( createdAtCandidate ) && Number . isFinite ( createdAtCandidate )
301302 ? Math . floor ( createdAtCandidate )
302303 : Math . floor ( Date . now ( ) / 1000 ) ;
303- const label = typeof item . label === 'string' && item . label . length > 0 ? item . label : 'legacy' ;
304- const isCurrent = item . isCurrent === true ;
304+ const label =
305+ isString ( itemRecord . label ) && itemRecord . label . length > 0 ? itemRecord . label : 'legacy' ;
306+ const isCurrent = itemRecord . isCurrent === true ;
305307
306308 normalized . push ( {
307309 id,
0 commit comments