Skip to content

Commit 9b59061

Browse files
committed
update
1 parent bad1b6a commit 9b59061

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

packages/app/server/api/repo/search.get.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -111,48 +111,46 @@ export default defineEventHandler(async (event) => {
111111
};
112112
};
113113

114+
const { repos, cacheStatus } = await getIndexedRepos();
115+
const matches = repos
116+
.map((repo) => {
117+
const name = repo.name.toLowerCase();
118+
const owner = repo.ownerLogin.toLowerCase();
119+
const score = Math.max(
120+
stringSimilarity.compareTwoStrings(name, searchText),
121+
stringSimilarity.compareTwoStrings(owner, searchText),
122+
);
123+
124+
if (
125+
score <= 0.3 &&
126+
!name.includes(searchText) &&
127+
!owner.includes(searchText)
128+
) {
129+
return null;
130+
}
131+
132+
return {
133+
...repo,
134+
score,
135+
};
136+
})
137+
.filter((repo): repo is RepoSearchIndexItem & { score: number } => !!repo)
138+
.sort((a, b) => b.score - a.score || b.stars - a.stars);
139+
114140
setResponseHeaders(event, {
115141
"Content-Type": "text/event-stream",
116142
"Cache-Control": "no-cache",
117143
Connection: "keep-alive",
144+
"x-repo-index-cache": cacheStatus,
118145
});
119146

120147
const stream = new ReadableStream<string>({
121-
async start(controller) {
148+
start(controller) {
122149
const send = (data: string) => {
123150
controller.enqueue(`data: ${data}\n\n`);
124151
};
125152

126153
try {
127-
const { repos, cacheStatus } = await getIndexedRepos();
128-
setResponseHeader(event, "x-repo-index-cache", cacheStatus);
129-
const matches = repos
130-
.map((repo) => {
131-
const name = repo.name.toLowerCase();
132-
const owner = repo.ownerLogin.toLowerCase();
133-
const score = Math.max(
134-
stringSimilarity.compareTwoStrings(name, searchText),
135-
stringSimilarity.compareTwoStrings(owner, searchText),
136-
);
137-
138-
if (
139-
score <= 0.3 &&
140-
!name.includes(searchText) &&
141-
!owner.includes(searchText)
142-
) {
143-
return null;
144-
}
145-
146-
return {
147-
...repo,
148-
score,
149-
};
150-
})
151-
.filter(
152-
(repo): repo is RepoSearchIndexItem & { score: number } => !!repo,
153-
)
154-
.sort((a, b) => b.score - a.score || b.stars - a.stars);
155-
156154
for (const repo of matches) {
157155
if (signal.aborted) {
158156
break;

0 commit comments

Comments
 (0)