mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
fix: local search keyword undefined errors (#3992)
* fix: local search keyword undefined errors * Update indexer.ts
This commit is contained in:
parent
e445db0a1f
commit
35cc57b79e
6 changed files with 700 additions and 419 deletions
5
.changeset/shiny-worms-retire.md
Normal file
5
.changeset/shiny-worms-retire.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@verdaccio/search': patch
|
||||
---
|
||||
|
||||
fix: keyword undefined errors
|
|
@ -87,6 +87,7 @@
|
|||
"@verdaccio/eslint-config": "workspace:*",
|
||||
"@verdaccio/types": "workspace:*",
|
||||
"@verdaccio/ui-theme": "workspace:*",
|
||||
"@vitest/coverage-v8": "^0.34.3",
|
||||
"babel-core": "7.0.0-bridge.0",
|
||||
"babel-jest": "29.4.3",
|
||||
"babel-plugin-dynamic-import-node": "2.3.3",
|
||||
|
@ -123,7 +124,8 @@
|
|||
"verdaccio-audit": "workspace:*",
|
||||
"verdaccio-auth-memory": "workspace:*",
|
||||
"verdaccio-htpasswd": "workspace:*",
|
||||
"verdaccio-memory": "workspace:*"
|
||||
"verdaccio-memory": "workspace:*",
|
||||
"vitest": "^0.34.3"
|
||||
},
|
||||
"scripts": {
|
||||
"prepare": "husky install",
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
},
|
||||
"scripts": {
|
||||
"clean": "rimraf ./build",
|
||||
"test": "echo 1",
|
||||
"test": "vitest run",
|
||||
"type-check": "tsc --noEmit -p tsconfig.build.json",
|
||||
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
|
||||
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",
|
||||
|
@ -39,7 +39,7 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@verdaccio/types": "workspace:12.0.0-next.0",
|
||||
"@orama/orama": "1.1.1",
|
||||
"@orama/orama": "1.2.1",
|
||||
"debug": "4.3.4",
|
||||
"esbuild": "0.14.10"
|
||||
},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { create, insert, remove, search } from '@orama/orama';
|
||||
import buildDebug from 'debug';
|
||||
|
||||
import { Version } from '@verdaccio/types';
|
||||
import { Logger, Version } from '@verdaccio/types';
|
||||
|
||||
const debug = buildDebug('verdaccio:search:indexer');
|
||||
|
||||
|
@ -10,6 +10,7 @@ type Results = any;
|
|||
class SearchMemoryIndexer {
|
||||
private database: any | undefined;
|
||||
private storage: any;
|
||||
private logger: Logger | undefined;
|
||||
|
||||
/**
|
||||
* Set up the {Storage}
|
||||
|
@ -31,13 +32,21 @@ class SearchMemoryIndexer {
|
|||
debug('searching %s at indexer', term);
|
||||
const searchResult = await search(this.database, {
|
||||
term,
|
||||
properties: '*',
|
||||
});
|
||||
|
||||
return searchResult;
|
||||
}
|
||||
}
|
||||
|
||||
private prepareKeywords(keywords?: string[] | string): string {
|
||||
if (typeof keywords === 'undefined') {
|
||||
return '';
|
||||
} else if (typeof keywords === 'string') {
|
||||
return keywords;
|
||||
}
|
||||
return keywords.join(',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new element to index
|
||||
* @param {*} pkg the package
|
||||
|
@ -46,14 +55,15 @@ class SearchMemoryIndexer {
|
|||
if (this.database) {
|
||||
const name = pkg.name;
|
||||
debug('adding item %s to the indexer', name);
|
||||
insert(this.database, {
|
||||
const item = {
|
||||
id: name,
|
||||
name: name,
|
||||
description: pkg.description,
|
||||
version: `v${pkg.version}`,
|
||||
keywords: pkg.keywords,
|
||||
author: pkg._npmUser ? pkg._npmUser.name : '???',
|
||||
});
|
||||
version: pkg.version,
|
||||
keywords: this.prepareKeywords(pkg.keywords),
|
||||
author: pkg._npmUser ? pkg._npmUser.name : '',
|
||||
};
|
||||
await insert(this.database, item);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,9 +81,9 @@ class SearchMemoryIndexer {
|
|||
/**
|
||||
* Force a re-index.
|
||||
*/
|
||||
public reindex(): void {
|
||||
public async reindex(): Promise<void> {
|
||||
debug('reindexing search indexer');
|
||||
this.storage?.getLocalDatabase((error, packages): void => {
|
||||
this.storage?.getLocalDatabase(async (error, packages): Promise<void> => {
|
||||
if (error) {
|
||||
// that function shouldn't produce any
|
||||
throw error;
|
||||
|
@ -86,13 +96,18 @@ class SearchMemoryIndexer {
|
|||
while (i--) {
|
||||
const pkg = packages[i];
|
||||
debug('indexing package %s', pkg?.name);
|
||||
this.add(pkg);
|
||||
try {
|
||||
await this.add(pkg);
|
||||
} catch (err: any) {
|
||||
this.logger?.error({ err: err.message }, 'error @{err} indexing package');
|
||||
}
|
||||
}
|
||||
debug('reindexed search indexer');
|
||||
});
|
||||
}
|
||||
|
||||
public async init() {
|
||||
public async init(logger: Logger) {
|
||||
this.logger = logger;
|
||||
this.database = await create({
|
||||
schema: {
|
||||
id: 'string',
|
||||
|
|
47
packages/search/test/index.spec.ts
Normal file
47
packages/search/test/index.spec.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import { expect, test } from 'vitest';
|
||||
|
||||
import { Logger } from '@verdaccio/types';
|
||||
|
||||
import { SearchMemoryIndexer } from '../src';
|
||||
|
||||
class MockStore {
|
||||
getLocalDatabase(cb) {
|
||||
return cb(null, [
|
||||
{
|
||||
name: 'verdaccio-search',
|
||||
version: '1.0.0',
|
||||
readme: 'foo',
|
||||
description: 'foo',
|
||||
keywords: ['foo', 'bar'],
|
||||
},
|
||||
{
|
||||
name: 'verdaccio-utils',
|
||||
version: '2.0.0',
|
||||
readme: 'foo',
|
||||
description: 'foo',
|
||||
keywords: 'some',
|
||||
},
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
const logger = {
|
||||
// eslint-disable-next-line no-console
|
||||
error: (...arg) => console.error(...arg),
|
||||
} as Logger;
|
||||
|
||||
test('should search', async () => {
|
||||
const store = new MockStore();
|
||||
|
||||
SearchMemoryIndexer.configureStorage(store);
|
||||
await SearchMemoryIndexer.init(logger);
|
||||
// @ts-expect-error
|
||||
await SearchMemoryIndexer.add({
|
||||
name: 'verdaccio',
|
||||
version: '2.0.0',
|
||||
readme: 'foo',
|
||||
description: '',
|
||||
});
|
||||
const query = await SearchMemoryIndexer.query('verdaccio');
|
||||
expect(query.hits.map((item) => item.id)).toEqual(['verdaccio', 'verdaccio-utils']);
|
||||
});
|
1022
pnpm-lock.yaml
1022
pnpm-lock.yaml
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue