Skip to content

Commit

Permalink
fix(engine-core): data proxy integration patch version resolver (#13699)
Browse files Browse the repository at this point in the history
Co-authored-by: Joël Galeran <Jolg42@users.noreply.github.com>
  • Loading branch information
millsp and Jolg42 committed Jun 9, 2022
1 parent c6bab0e commit 89c1288
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions packages/engine-core/src/data-proxy/utils/getClientVersion.ts
Expand Up @@ -6,7 +6,6 @@ import { NotImplementedYetError } from '../errors/NotImplementedYetError'
import { request } from './request'

const semverRegex = /^[1-9][0-9]*\.[0-9]+\.[0-9]+$/
const prismaNpm = 'https://registry.npmjs.org/prisma'
const debug = Debug('prisma:client:dataproxyEngine')

async function _getClientVersion(config: EngineConfig) {
Expand All @@ -24,22 +23,18 @@ async function _getClientVersion(config: EngineConfig) {
return version
}

// if it's an integration version, we resolve its data proxy
if (suffix === 'integration' || suffix?.startsWith('dev') || clientVersion === '0.0.0') {
// we infer the data proxy version from the engine version
// if it is an integration or dev version, we resolve its dataproxy
// for this we infer the data proxy version from the engine version
if (suffix !== undefined || clientVersion === '0.0.0') {
const [version] = engineVersion.split('-') ?? []
const [major, minor, patch] = version.split('.')

// if a patch has happened, then we return that version
if (patch !== '0') return `${major}.${minor}.${patch}`
// to ensure that the data proxy exists, we check if it's published
// we resolve with the closest or previous version published on npm
const pkgURL = prismaPkgURL(`<=${major}.${minor}.${patch}`)
const res = await request(pkgURL, { clientVersion })

// if not, we know that the minor must be minus with 1
const published = `${major}.${parseInt(minor) - 1}.x`

// we don't know what `x` is, so we query the registry
const res = await request(`${prismaNpm}/${published}`, { clientVersion })

return ((await res.json())['version'] as string) ?? 'undefined'
return (await res.json())['version'] as string
}

// nothing matched, meaning that the provided version is invalid
Expand All @@ -60,3 +55,16 @@ export async function getClientVersion(config: EngineConfig) {

return version
}

/**
* We use unpkg.com to resolve the version of the data proxy. We chose this over
* registry.npmjs.com because they cache their queries/responses so it is fast.
* Moreover, unpkg.com is able to support comparison operators like `<=1.0.0`.
* For us, that means we can provide a version that is too high (not published),
* and it will be able to resolve to the closest existing (published) version.
* @param version
* @returns
*/
function prismaPkgURL(version: string) {
return `https://unpkg.com/prisma@${version}/package.json`
}

0 comments on commit 89c1288

Please sign in to comment.