Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(engine-core): data proxy integration patch version resolver #13699

Merged
merged 4 commits into from
Jun 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 21 additions & 13 deletions packages/engine-core/src/data-proxy/utils/getClientVersion.ts
Original file line number Diff line number Diff line change
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}`)
millsp marked this conversation as resolved.
Show resolved Hide resolved
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`
}