From d34bcc107effe589e843fcddc418f8e50b3bad61 Mon Sep 17 00:00:00 2001 From: Samuel Bodin Date: Mon, 12 Jul 2021 19:38:12 +0200 Subject: [PATCH] fix: mini fixes (#659) --- src/bootstrap.ts | 3 +-- src/npm/Prefetcher.ts | 9 +++++++-- src/npm/index.ts | 7 +++++-- src/watch.ts | 26 +++++++++++++++++--------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/bootstrap.ts b/src/bootstrap.ts index f6dd4e126..e1b9fc3b6 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -92,7 +92,6 @@ export async function run( // Push nothing to trigger event consumer.push(null as any); - processing = false; } consumer.pause(); @@ -159,7 +158,7 @@ function createPkgConsumer( try { datadog.increment('packages'); - const res = await npm.getDoc(pkg.id); + const res = await npm.getDoc(pkg.id, pkg.value.rev); if (isFailure(res)) { log.error('Got an error', res.error); diff --git a/src/npm/Prefetcher.ts b/src/npm/Prefetcher.ts index ab4e38e18..55fd2da47 100644 --- a/src/npm/Prefetcher.ts +++ b/src/npm/Prefetcher.ts @@ -1,12 +1,17 @@ -import type { DocumentListParams } from 'nano'; +import type { DocumentListParams, DocumentResponseRow } from 'nano'; import { config } from '../config'; import { log } from '../utils/log'; import { wait } from '../utils/wait'; +import type { GetPackage } from './types'; + import * as npm from './index'; -export type PrefetchedPkg = { id: string }; +export type PrefetchedPkg = Pick< + DocumentResponseRow, + 'id' | 'value' +>; export class Prefetcher { #limit: number = config.bootstrapConcurrency; diff --git a/src/npm/index.ts b/src/npm/index.ts index e0b78d9c8..ddb7bf464 100644 --- a/src/npm/index.ts +++ b/src/npm/index.ts @@ -84,10 +84,13 @@ async function getChanges( return results; } -async function getDoc(name: string): Promise { +async function getDoc( + name: string, + rev: string +): Promise { const start = Date.now(); - const doc = await db.get(name); + const doc = await db.get(name, { rev }); datadog.timing('npm.getDoc', Date.now() - start); diff --git a/src/watch.ts b/src/watch.ts index fc9b5249b..254b4e126 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -112,19 +112,27 @@ async function loop( if (change.deleted) { // Delete package directly in index // Filter does not support async/await but there is no concurrency issue with this - mainIndex.deleteObject(change.id); - log.info(`Deleted`, change.id); - return; + throw new Error('deleted'); } - const res = await npm.getDoc(change.id); + try { + const res = await npm.getDoc(change.id, change.changes[0].rev); - if (isFailure(res)) { - log.error('Got an error', res.error); - return; - } + if (isFailure(res)) { + log.error('Got an error', res.error); + return; + } - await saveDoc({ row: res, index: mainIndex }); + await saveDoc({ row: res, index: mainIndex }); + } catch (e) { + // this error can be thrown by us or by nano if: + // - we received a change that is not marked as "deleted" + // - and the package has since been deleted + if (e.message === 'deleted') { + mainIndex.deleteObject(change.id); + log.info(`deleted`, change.id); + } + } } /**