diff --git a/jslib b/jslib index 4ca7a9709e9..bdb2efd7705 160000 --- a/jslib +++ b/jslib @@ -1 +1 @@ -Subproject commit 4ca7a9709e9ccd0e67ce09309ae605f2057bf089 +Subproject commit bdb2efd77054d28a686dcf50a849ffbf62a996d6 diff --git a/package-lock.json b/package-lock.json index 3040b06fc3e..becdc884c3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3209,6 +3209,11 @@ "yallist": "^2.1.2" } }, + "lunr": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/lunr/-/lunr-2.3.1.tgz", + "integrity": "sha1-ETYWorYC3cEJMqe/ik5uV+v+zfI=" + }, "make-dir": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-1.3.0.tgz", diff --git a/package.json b/package.json index 2358bd37b86..c3e19d4077b 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@types/form-data": "^2.2.1", "@types/inquirer": "^0.0.42", "@types/lowdb": "^1.0.1", - "@types/lunr": "^2.1.5", + "@types/lunr": "^2.1.6", "@types/node": "^10.0.8", "@types/node-fetch": "^1.6.9", "@types/node-forge": "^0.7.1", @@ -69,6 +69,7 @@ "form-data": "2.3.2", "inquirer": "5.2.0", "lowdb": "1.0.0", + "lunr": "2.3.1", "node-fetch": "2.1.2", "node-forge": "0.7.1", "papaparse": "4.3.5" diff --git a/src/bw.ts b/src/bw.ts index e624fbbdebc..48318538fe2 100644 --- a/src/bw.ts +++ b/src/bw.ts @@ -23,6 +23,7 @@ import { LowdbStorageService } from 'jslib/services/lowdbStorage.service'; import { NodeApiService } from 'jslib/services/nodeApi.service'; import { NodeCryptoFunctionService } from 'jslib/services/nodeCryptoFunction.service'; import { PasswordGenerationService } from 'jslib/services/passwordGeneration.service'; +import { SearchService } from 'jslib/services/search.service'; import { SettingsService } from 'jslib/services/settings.service'; import { SyncService } from 'jslib/services/sync.service'; import { TokenService } from 'jslib/services/token.service'; @@ -56,6 +57,7 @@ export class Main { auditService: AuditService; importService: ImportService; exportService: ExportService; + searchService: SearchService; cryptoFunctionService: NodeCryptoFunctionService; authService: AuthService; program: Program; @@ -89,13 +91,15 @@ export class Main { this.containerService = new ContainerService(this.cryptoService, this.platformUtilsService); this.settingsService = new SettingsService(this.userService, this.storageService); this.cipherService = new CipherService(this.cryptoService, this.userService, this.settingsService, - this.apiService, this.storageService, this.i18nService, this.platformUtilsService); + this.apiService, this.storageService, this.i18nService, this.platformUtilsService, null); this.folderService = new FolderService(this.cryptoService, this.userService, this.apiService, this.storageService, this.i18nService, this.cipherService); this.collectionService = new CollectionService(this.cryptoService, this.userService, this.storageService, this.i18nService); + this.searchService = new SearchService(this.cipherService, this.platformUtilsService); this.lockService = new LockService(this.cipherService, this.folderService, this.collectionService, - this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, null); + this.cryptoService, this.platformUtilsService, this.storageService, this.messagingService, + this.searchService, null); this.syncService = new SyncService(this.userService, this.apiService, this.settingsService, this.folderService, this.cipherService, this.cryptoService, this.collectionService, this.storageService, this.messagingService, async (expired: boolean) => await this.logout()); diff --git a/src/commands/get.command.ts b/src/commands/get.command.ts index 04680557cdf..4058fc896ce 100644 --- a/src/commands/get.command.ts +++ b/src/commands/get.command.ts @@ -8,6 +8,7 @@ import { CipherService } from 'jslib/abstractions/cipher.service'; import { CollectionService } from 'jslib/abstractions/collection.service'; import { CryptoService } from 'jslib/abstractions/crypto.service'; import { FolderService } from 'jslib/abstractions/folder.service'; +import { SearchService } from 'jslib/abstractions/search.service'; import { TokenService } from 'jslib/abstractions/token.service'; import { TotpService } from 'jslib/abstractions/totp.service'; import { UserService } from 'jslib/abstractions/user.service'; @@ -43,7 +44,8 @@ export class GetCommand { constructor(private cipherService: CipherService, private folderService: FolderService, private collectionService: CollectionService, private totpService: TotpService, private auditService: AuditService, private cryptoService: CryptoService, - private tokenService: TokenService, private userService: UserService) { } + private tokenService: TokenService, private userService: UserService, + private searchService: SearchService) { } async run(object: string, id: string, cmd: program.Command): Promise { if (id != null) { @@ -87,7 +89,7 @@ export class GetCommand { } } else if (id.trim() !== '') { let ciphers = await this.cipherService.getAllDecrypted(); - ciphers = CliUtils.searchCiphers(ciphers, id); + ciphers = this.searchService.searchCiphersBasic(ciphers, id); if (ciphers.length > 1) { return Response.multipleResults(ciphers.map((c) => c.id)); } diff --git a/src/commands/list.command.ts b/src/commands/list.command.ts index c8b3be50a9d..55921238c85 100644 --- a/src/commands/list.command.ts +++ b/src/commands/list.command.ts @@ -3,6 +3,7 @@ import * as program from 'commander'; import { CipherService } from 'jslib/abstractions/cipher.service'; import { CollectionService } from 'jslib/abstractions/collection.service'; import { FolderService } from 'jslib/abstractions/folder.service'; +import { SearchService } from 'jslib/abstractions/search.service'; import { UserService } from 'jslib/abstractions/user.service'; import { Response } from '../models/response'; @@ -16,7 +17,8 @@ import { CliUtils } from '../utils'; export class ListCommand { constructor(private cipherService: CipherService, private folderService: FolderService, - private collectionService: CollectionService, private userService: UserService) { } + private collectionService: CollectionService, private userService: UserService, + private searchService: SearchService) { } async run(object: string, cmd: program.Command): Promise { switch (object.toLowerCase()) { @@ -75,7 +77,7 @@ export class ListCommand { } if (cmd.search != null && cmd.search.trim() !== '') { - ciphers = CliUtils.searchCiphers(ciphers, cmd.search); + ciphers = this.searchService.searchCiphersBasic(ciphers, cmd.search); } const res = new ListResponse(ciphers.map((o) => new CipherResponse(o))); diff --git a/src/program.ts b/src/program.ts index 4ae9a283c20..592247d20bc 100644 --- a/src/program.ts +++ b/src/program.ts @@ -225,7 +225,7 @@ export class Program { .action(async (object, cmd) => { await this.exitIfLocked(); const command = new ListCommand(this.main.cipherService, this.main.folderService, - this.main.collectionService, this.main.userService); + this.main.collectionService, this.main.userService, this.main.searchService); const response = await command.run(object, cmd); this.processResponse(response); }); @@ -271,7 +271,8 @@ export class Program { await this.exitIfLocked(); const command = new GetCommand(this.main.cipherService, this.main.folderService, this.main.collectionService, this.main.totpService, this.main.auditService, - this.main.cryptoService, this.main.tokenService, this.main.userService); + this.main.cryptoService, this.main.tokenService, this.main.userService, + this.main.searchService); const response = await command.run(object, id, cmd); this.processResponse(response); }); diff --git a/src/utils.ts b/src/utils.ts index 846014e42b0..485f5901030 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -102,22 +102,6 @@ export class CliUtils { }); } - static searchCiphers(ciphers: CipherView[], search: string) { - search = search.toLowerCase(); - return ciphers.filter((c) => { - if (c.name != null && c.name.toLowerCase().indexOf(search) > -1) { - return true; - } - if (c.subTitle != null && c.subTitle.toLowerCase().indexOf(search) > -1) { - return true; - } - if (c.login && c.login.uri != null && c.login.uri.toLowerCase().indexOf(search) > -1) { - return true; - } - return false; - }); - } - static searchFolders(folders: FolderView[], search: string) { search = search.toLowerCase(); return folders.filter((f) => {