diff --git a/src/routes/packages.js b/src/routes/packages.js index 0f7b11c..618d30d 100644 --- a/src/routes/packages.js +++ b/src/routes/packages.js @@ -119,6 +119,20 @@ export async function getPackagePackument (c) { return c.json(ret, 200) } +async function mergeTags(db, pkg, tags) { + const selectQuery = `SELECT name, tags FROM packages WHERE name="${pkg}"` + const rows = await db.prepare(selectQuery).run() + if (rows.results.length) { + tags = { ...JSON.parse(rows.results[0].tags), ...tags } + } + const insertQueryFn = (tags) => { + const strTags = `json('${JSON.stringify(tags)}')` + return `INSERT INTO packages (name, tags) VALUES ("${pkg}", ${strTags}) + ON CONFLICT(name) DO UPDATE SET tags=${strTags}` + } + await db.prepare(insertQueryFn(tags)).run() +} + export async function publishPackage (c) { const { pkg } = packageSpec(c) const body = await c.req.json() @@ -135,8 +149,7 @@ export async function publishPackage (c) { const new_versions = Object.keys(body.versions).filter(v => !versions.includes(v)) if (!results || results.length === 0) { - const insertQuery = `INSERT INTO packages (name, tags) VALUES ("${pkg}", json('{"latest": "${new_versions[0]}"}'))` - await c.env.DB.prepare(insertQuery).run() + await mergeTags(c.env.DB, pkg, { latest: new_versions[0] }) } // check for conflicts in publishing vs. existing diff --git a/src/utils/packages.js b/src/utils/packages.js index 9fb2ef9..895bdb7 100644 --- a/src/utils/packages.js +++ b/src/utils/packages.js @@ -5,9 +5,13 @@ import getNpmTarballUrl from 'get-npm-tarball-url' import semver from 'semver' export async function extractPackageJSON (buffer) { + console.log(`ext-1`, buffer); const blob = new Blob([Buffer.from(buffer)]) + console.log(`ext-2`); const stream = blob.stream().pipeThrough(new DecompressionStream('gzip')) + console.log(`ext-3`); for await (const obj of extract(stream)) { + console.log(`ext-3.1`, obj.header.name); if (obj.header.name === 'package/package.json') { return JSON.parse(await obj.text()) }