The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Fix/enable queue rest management pqmq versions (#40023)
Ali Waseem922780fcc1dd
diff --git a/apps/studio/components/interfaces/Integrations/Queues/QueuesSettings.tsx b/apps/studio/components/interfaces/Integrations/Queues/QueuesSettings.tsxindex eefac54699b..a50b127c2d5 100644--- a/apps/studio/components/interfaces/Integrations/Queues/QueuesSettings.tsx+++ b/apps/studio/components/interfaces/Integrations/Queues/QueuesSettings.tsx@@ -19,6 +19,7 @@ import {QUEUES_SCHEMA,useDatabaseQueueToggleExposeMutation,} from 'data/database-queues/database-queues-toggle-postgrest-mutation'+import { useDatabaseQueuesVersionQuery } from 'data/database-queues/database-queues-version-query'import { useTableUpdateMutation } from 'data/tables/table-update-mutation'import { useTablesQuery } from 'data/tables/tables-query'import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'@@ -78,6 +79,11 @@ export const QueuesSettings = () => {})const schemas = config?.db_schema.replace(/ /g, '').split(',') ?? []+ const { data: pgmqVersion } = useDatabaseQueuesVersionQuery({+ projectRef: project?.ref,+ connectionString: project?.connectionString,+ })+const { mutateAsync: updateTable } = useTableUpdateMutation()const { mutate: updatePostgrestConfig } = useProjectPostgrestConfigUpdateMutation({@@ -161,12 +167,16 @@ export const QueuesSettings = () => {`Failed to toggle queue exposure via PostgREST: Unable to retrieve PostgREST configuration (${configError.message})`)}+ if (!pgmqVersion) {+ return toast.error('Unable to retrieve PGMQ version. Please try again later.')+ }setIsToggling(true)toggleExposeQueuePostgrest({projectRef: project.ref,connectionString: project.connectionString,enable: values.enable,+ pgmqVersion,})}diff --git a/apps/studio/data/database-queues/constants.ts b/apps/studio/data/database-queues/constants.tsnew file mode 100644index 00000000000..c87951ea51d--- /dev/null+++ b/apps/studio/data/database-queues/constants.ts@@ -0,0 +1 @@+export const PGMQ_EXTENSION_NAME = 'pgmq' as constdiff --git a/apps/studio/data/database-queues/database-queues-toggle-postgrest-mutation.ts b/apps/studio/data/database-queues/database-queues-toggle-postgrest-mutation.tsindex 4e1e14566e0..02567e13c47 100644--- a/apps/studio/data/database-queues/database-queues-toggle-postgrest-mutation.ts+++ b/apps/studio/data/database-queues/database-queues-toggle-postgrest-mutation.ts@@ -4,191 +4,207 @@ import { toast } from 'sonner'import { databaseKeys } from 'data/database/keys'import { executeSql } from 'data/sql/execute-sql-query'+import { isGreaterThanOrEqual } from 'lib/semver'import type { ResponseError } from 'types'import { databaseQueuesKeys } from './keys'export type DatabaseQueueExposePostgrestVariables = {projectRef: string- connectionString?: string | nullenable: boolean+ pgmqVersion: string++ connectionString?: string | null}+const CONDITIONAL_READ_SIGNATURE_PGMQ_VERSION = '1.5.0'export const QUEUES_SCHEMA = 'pgmq_public'-const EXPOSE_QUEUES_TO_POSTGREST_SQL = minify(/* SQL */ `-create schema if not exists ${QUEUES_SCHEMA};-grant usage on schema ${QUEUES_SCHEMA} to postgres, anon, authenticated, service_role;--create or replace function ${QUEUES_SCHEMA}.pop(- queue_name text-)- returns setof pgmq.message_record- language plpgsql- set search_path = ''-as $$-begin- return query- select *- from pgmq.pop(- queue_name := queue_name- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.pop(queue_name text) is 'Retrieves and locks the next message from the specified queue.';---create or replace function ${QUEUES_SCHEMA}.send(- queue_name text,- message jsonb,- sleep_seconds integer default 0 -- renamed from 'delay'-)- returns setof bigint- language plpgsql- set search_path = ''-as $$-begin- return query- select *- from pgmq.send(- queue_name := queue_name,- msg := message,- delay := sleep_seconds- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.send(queue_name text, message jsonb, sleep_seconds integer) is 'Sends a message to the specified queue, optionally delaying its availability by a number of seconds.';---create or replace function ${QUEUES_SCHEMA}.send_batch(- queue_name text,- messages jsonb[],- sleep_seconds integer default 0 -- renamed from 'delay'-)- returns setof bigint- language plpgsql- set search_path = ''-as $$-begin- return query- select *- from pgmq.send_batch(- queue_name := queue_name,- msgs := messages,- delay := sleep_seconds- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.send_batch(queue_name text, messages jsonb[], sleep_seconds integer) is 'Sends a batch of messages to the specified queue, optionally delaying their availability by a number of seconds.';---create or replace function ${QUEUES_SCHEMA}.archive(- queue_name text,- message_id bigint-)- returns boolean- language plpgsql- set search_path = ''-as $$-begin- return- pgmq.archive(- queue_name := queue_name,- msg_id := message_id- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.archive(queue_name text, message_id bigint) is 'Archives a message by moving it from the queue to a permanent archive.';---create or replace function ${QUEUES_SCHEMA}.delete(- queue_name text,- message_id bigint-)- returns boolean- language plpgsql- set search_path = ''-as $$-begin- return- pgmq.delete(- queue_name := queue_name,- msg_id := message_id- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.delete(queue_name text, message_id bigint) is 'Permanently deletes a message from the specified queue.';--create or replace function ${QUEUES_SCHEMA}.read(- queue_name text,- sleep_seconds integer,- n integer-)- returns setof pgmq.message_record- language plpgsql- set search_path = ''-as $$-begin- return query- select *- from pgmq.read(- queue_name := queue_name,- vt := sleep_seconds,- qty := n- );-end;-$$;--comment on function ${QUEUES_SCHEMA}.read(queue_name text, sleep_seconds integer, n integer) is 'Reads up to "n" messages from the specified queue with an optional "sleep_seconds" (visibility timeout).';---- Grant execute permissions on wrapper functions to roles-grant execute on function ${QUEUES_SCHEMA}.pop(text) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.pop(text) to postgres, service_role, anon, authenticated;--grant execute on function ${QUEUES_SCHEMA}.send(text, jsonb, integer) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.send(text, jsonb, integer) to postgres, service_role, anon, authenticated;--grant execute on function ${QUEUES_SCHEMA}.send_batch(text, jsonb[], integer) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.send_batch(text, jsonb[], integer) to postgres, service_role, anon, authenticated;--grant execute on function ${QUEUES_SCHEMA}.archive(text, bigint) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.archive(text, bigint) to postgres, service_role, anon, authenticated;--grant execute on function ${QUEUES_SCHEMA}.delete(text, bigint) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.delete(text, bigint) to postgres, service_role, anon, authenticated;--grant execute on function ${QUEUES_SCHEMA}.read(text, integer, integer) to postgres, service_role, anon, authenticated;-grant execute on function pgmq.read(text, integer, integer) to postgres, service_role, anon, authenticated;---- For the service role, we want full access--- Grant permissions on existing tables-grant all privileges on all tables in schema pgmq to postgres, service_role;---- Ensure service_role has permissions on future tables-alter default privileges in schema pgmq grant all privileges on tables to postgres, service_role;--grant usage on schema pgmq to postgres, anon, authenticated, service_role;---/*- Grant access to sequences to API roles by default. Existing table permissions- continue to enforce insert restrictions. This is necessary to accommodate the- on-backup hook that rebuild queue table primary keys to avoid a pg_dump segfault.- This can be removed once logical backups are completely retired.-*/-grant usage, select, update-on all sequences in schema pgmq-to anon, authenticated, service_role;--alter default privileges in schema pgmq-grant usage, select, update-on sequences-to anon, authenticated, service_role;+const getExposeQueuesSQL = (pgmqVersion: string) => {+ const conditionalJsonb = isGreaterThanOrEqual(+ pgmqVersion,+ CONDITIONAL_READ_SIGNATURE_PGMQ_VERSION+ )+ ? `, conditional := '{}'::jsonb`+ : ''+ const jsonBArg = isGreaterThanOrEqual(pgmqVersion, CONDITIONAL_READ_SIGNATURE_PGMQ_VERSION)+ ? `, jsonb`+ : ''++ return minify(/* SQL */ `+ create schema if not exists ${QUEUES_SCHEMA};+ grant usage on schema ${QUEUES_SCHEMA} to postgres, anon, authenticated, service_role;++ create or replace function ${QUEUES_SCHEMA}.pop(+ queue_name text+ )+ returns setof pgmq.message_record+ language plpgsql+ set search_path = ''+ as $$+ begin+ return query+ select *+ from pgmq.pop(+ queue_name := queue_name+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.pop(queue_name text) is 'Retrieves and locks the next message from the specified queue.';+++ create or replace function ${QUEUES_SCHEMA}.send(+ queue_name text,+ message jsonb,+ sleep_seconds integer default 0 -- renamed from 'delay'+ )+ returns setof bigint+ language plpgsql+ set search_path = ''+ as $$+ begin+ return query+ select *+ from pgmq.send(+ queue_name := queue_name,+ msg := message,+ delay := sleep_seconds+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.send(queue_name text, message jsonb, sleep_seconds integer) is 'Sends a message to the specified queue, optionally delaying its availability by a number of seconds.';+++ create or replace function ${QUEUES_SCHEMA}.send_batch(+ queue_name text,+ messages jsonb[],+ sleep_seconds integer default 0 -- renamed from 'delay'+ )+ returns setof bigint+ language plpgsql+ set search_path = ''+ as $$+ begin+ return query+ select *+ from pgmq.send_batch(+ queue_name := queue_name,+ msgs := messages,+ delay := sleep_seconds+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.send_batch(queue_name text, messages jsonb[], sleep_seconds integer) is 'Sends a batch of messages to the specified queue, optionally delaying their availability by a number of seconds.';+++ create or replace function ${QUEUES_SCHEMA}.archive(+ queue_name text,+ message_id bigint+ )+ returns boolean+ language plpgsql+ set search_path = ''+ as $$+ begin+ return+ pgmq.archive(+ queue_name := queue_name,+ msg_id := message_id+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.archive(queue_name text, message_id bigint) is 'Archives a message by moving it from the queue to a permanent archive.';+++ create or replace function ${QUEUES_SCHEMA}.delete(+ queue_name text,+ message_id bigint+ )+ returns boolean+ language plpgsql+ set search_path = ''+ as $$+ begin+ return+ pgmq.delete(+ queue_name := queue_name,+ msg_id := message_id+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.delete(queue_name text, message_id bigint) is 'Permanently deletes a message from the specified queue.';++ create or replace function ${QUEUES_SCHEMA}.read(+ queue_name text,+ sleep_seconds integer,+ n integer+ )+ returns setof pgmq.message_record+ language plpgsql+ set search_path = ''+ as $$+ begin+ return query+ select *+ from pgmq.read(+ queue_name := queue_name,+ vt := sleep_seconds,+ qty := n ${conditionalJsonb}+ );+ end;+ $$;++ comment on function ${QUEUES_SCHEMA}.read(queue_name text, sleep_seconds integer, n integer) is 'Reads up to "n" messages from the specified queue with an optional "sleep_seconds" (visibility timeout).';++ -- Grant execute permissions on wrapper functions to roles+ grant execute on function ${QUEUES_SCHEMA}.pop(text) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.pop(text) to postgres, service_role, anon, authenticated;++ grant execute on function ${QUEUES_SCHEMA}.send(text, jsonb, integer) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.send(text, jsonb, integer) to postgres, service_role, anon, authenticated;++ grant execute on function ${QUEUES_SCHEMA}.send_batch(text, jsonb[], integer) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.send_batch(text, jsonb[], integer) to postgres, service_role, anon, authenticated;++ grant execute on function ${QUEUES_SCHEMA}.archive(text, bigint) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.archive(text, bigint) to postgres, service_role, anon, authenticated;++ grant execute on function ${QUEUES_SCHEMA}.delete(text, bigint) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.delete(text, bigint) to postgres, service_role, anon, authenticated;++ grant execute on function ${QUEUES_SCHEMA}.read(text, integer, integer) to postgres, service_role, anon, authenticated;+ grant execute on function pgmq.read(text, integer, integer ${jsonBArg}) to postgres, service_role, anon, authenticated;++ -- For the service role, we want full access+ -- Grant permissions on existing tables+ grant all privileges on all tables in schema pgmq to postgres, service_role;++ -- Ensure service_role has permissions on future tables+ alter default privileges in schema pgmq grant all privileges on tables to postgres, service_role;++ grant usage on schema pgmq to postgres, anon, authenticated, service_role;+++ /*+ Grant access to sequences to API roles by default. Existing table permissions+ continue to enforce insert restrictions. This is necessary to accommodate the+ on-backup hook that rebuild queue table primary keys to avoid a pg_dump segfault.+ This can be removed once logical backups are completely retired.+ */+ grant usage, select, update+ on all sequences in schema pgmq+ to anon, authenticated, service_role;++ alter default privileges in schema pgmq+ grant usage, select, update+ on sequences+ to anon, authenticated, service_role;`)+}const HIDE_QUEUES_FROM_POSTGREST_SQL = minify(/* SQL */ `drop function if exists@@ -219,10 +235,11 @@ const HIDE_QUEUES_FROM_POSTGREST_SQL = minify(/* SQL */ `export async function toggleQueuesExposurePostgrest({projectRef,+ pgmqVersion,connectionString,enable,}: DatabaseQueueExposePostgrestVariables) {- const sql = enable ? EXPOSE_QUEUES_TO_POSTGREST_SQL : HIDE_QUEUES_FROM_POSTGREST_SQL+ const sql = enable ? getExposeQueuesSQL(pgmqVersion) : HIDE_QUEUES_FROM_POSTGREST_SQLconst { result } = await executeSql({projectRef,diff --git a/apps/studio/data/database-queues/database-queues-version-query.ts b/apps/studio/data/database-queues/database-queues-version-query.tsnew file mode 100644index 00000000000..013b3587824--- /dev/null+++ b/apps/studio/data/database-queues/database-queues-version-query.ts@@ -0,0 +1,30 @@+import { useDatabaseExtensionsQuery } from 'data/database-extensions/database-extensions-query'+import { ResponseError } from 'types'+import { PGMQ_EXTENSION_NAME } from './constants'++export type DatabaseQueuesVersionVariables = {+ projectRef?: string+ connectionString?: string | null+}++export type DatabaseQueueVersionData = string | null+export type DatabaseQueueVersionError = ResponseError++export const useDatabaseQueuesVersionQuery = (+ { projectRef, connectionString }: DatabaseQueuesVersionVariables,+ { enabled = true }: { enabled?: boolean } = {}+) => {+ return useDatabaseExtensionsQuery<DatabaseQueueVersionData>(+ { projectRef, connectionString },+ {+ enabled,+ select: (extensions) => {+ const pgmqExtension = extensions.find((ext) => ext.name === PGMQ_EXTENSION_NAME)++ if (!pgmqExtension?.installed_version) return null++ return pgmqExtension.installed_version+ },+ }+ )+}diff --git a/apps/studio/lib/semver.test.ts b/apps/studio/lib/semver.test.tsnew file mode 100644index 00000000000..2b134f8d5ad--- /dev/null+++ b/apps/studio/lib/semver.test.ts@@ -0,0 +1,166 @@+import { describe, it, expect } from 'vitest'+import {+ parseSemver,+ compareSemver,+ isGreaterThan,+ isLessThan,+ isEqual,+ isGreaterThanOrEqual,+ isLessThanOrEqual,+ isValidSemver,+} from './semver'++describe('parseSemver', () => {+ it('should parse valid semver strings', () => {+ expect(parseSemver('1.2.3')).toEqual({ major: 1, minor: 2, patch: 3 })+ expect(parseSemver('0.0.1')).toEqual({ major: 0, minor: 0, patch: 1 })+ expect(parseSemver('10.20.30')).toEqual({ major: 10, minor: 20, patch: 30 })+ })++ it('should handle strings with extra whitespace', () => {+ expect(parseSemver(' 1.2.3 ')).toEqual({ major: 1, minor: 2, patch: 3 })+ expect(parseSemver(' 1.2.3 ')).toEqual({ major: 1, minor: 2, patch: 3 })+ })++ it('should return null for invalid semver strings', () => {+ expect(parseSemver('1.2')).toBeNull()+ expect(parseSemver('1.2.3.4')).toBeNull()+ expect(parseSemver('1.2.x')).toBeNull()+ expect(parseSemver('a.b.c')).toBeNull()+ expect(parseSemver('')).toBeNull()+ expect(parseSemver('invalid')).toBeNull()+ })++ it('should return null for negative numbers', () => {+ expect(parseSemver('-1.2.3')).toBeNull()+ expect(parseSemver('1.-2.3')).toBeNull()+ expect(parseSemver('1.2.-3')).toBeNull()+ })++ it('should return null for non-string inputs', () => {+ expect(parseSemver(null as any)).toBeNull()+ expect(parseSemver(undefined as any)).toBeNull()+ expect(parseSemver(123 as any)).toBeNull()+ })+})++describe('compareSemver', () => {+ it('should return 0 for equal versions', () => {+ expect(compareSemver('1.2.3', '1.2.3')).toBe(0)+ expect(compareSemver('0.0.0', '0.0.0')).toBe(0)+ expect(compareSemver('10.20.30', '10.20.30')).toBe(0)+ })++ it('should return 1 when first version is greater', () => {+ expect(compareSemver('2.0.0', '1.0.0')).toBe(1)+ expect(compareSemver('1.3.0', '1.2.0')).toBe(1)+ expect(compareSemver('1.2.4', '1.2.3')).toBe(1)+ expect(compareSemver('2.0.0', '1.9.9')).toBe(1)+ })++ it('should return -1 when first version is less', () => {+ expect(compareSemver('1.0.0', '2.0.0')).toBe(-1)+ expect(compareSemver('1.2.0', '1.3.0')).toBe(-1)+ expect(compareSemver('1.2.3', '1.2.4')).toBe(-1)+ expect(compareSemver('1.9.9', '2.0.0')).toBe(-1)+ })++ it('should return null for invalid versions', () => {+ expect(compareSemver('1.2.3', 'invalid')).toBeNull()+ expect(compareSemver('invalid', '1.2.3')).toBeNull()+ expect(compareSemver('1.2', '1.2.3')).toBeNull()+ })++ it('should prioritize major version differences', () => {+ expect(compareSemver('2.0.0', '1.99.99')).toBe(1)+ expect(compareSemver('1.99.99', '2.0.0')).toBe(-1)+ })++ it('should prioritize minor version differences when major is equal', () => {+ expect(compareSemver('1.3.0', '1.2.99')).toBe(1)+ expect(compareSemver('1.2.99', '1.3.0')).toBe(-1)+ })+})++describe('isGreaterThan', () => {+ it('should return true when first version is greater', () => {+ expect(isGreaterThan('2.0.0', '1.0.0')).toBe(true)+ expect(isGreaterThan('1.3.0', '1.2.0')).toBe(true)+ expect(isGreaterThan('1.2.4', '1.2.3')).toBe(true)+ })++ it('should return false when first version is not greater', () => {+ expect(isGreaterThan('1.0.0', '2.0.0')).toBe(false)+ expect(isGreaterThan('1.2.3', '1.2.3')).toBe(false)+ expect(isGreaterThan('1.2.3', 'invalid')).toBe(false)+ })+})++describe('isLessThan', () => {+ it('should return true when first version is less', () => {+ expect(isLessThan('1.0.0', '2.0.0')).toBe(true)+ expect(isLessThan('1.2.0', '1.3.0')).toBe(true)+ expect(isLessThan('1.2.3', '1.2.4')).toBe(true)+ })++ it('should return false when first version is not less', () => {+ expect(isLessThan('2.0.0', '1.0.0')).toBe(false)+ expect(isLessThan('1.2.3', '1.2.3')).toBe(false)+ expect(isLessThan('1.2.3', 'invalid')).toBe(false)+ })+})++describe('isEqual', () => {+ it('should return true when versions are equal', () => {+ expect(isEqual('1.2.3', '1.2.3')).toBe(true)+ expect(isEqual('0.0.0', '0.0.0')).toBe(true)+ expect(isEqual('10.20.30', '10.20.30')).toBe(true)+ })++ it('should return false when versions are not equal', () => {+ expect(isEqual('1.2.3', '1.2.4')).toBe(false)+ expect(isEqual('1.2.3', '2.2.3')).toBe(false)+ expect(isEqual('1.2.3', 'invalid')).toBe(false)+ })+})++describe('isGreaterThanOrEqual', () => {+ it('should return true when first version is greater or equal', () => {+ expect(isGreaterThanOrEqual('2.0.0', '1.0.0')).toBe(true)+ expect(isGreaterThanOrEqual('1.2.3', '1.2.3')).toBe(true)+ expect(isGreaterThanOrEqual('1.2.4', '1.2.3')).toBe(true)+ })++ it('should return false when first version is less', () => {+ expect(isGreaterThanOrEqual('1.0.0', '2.0.0')).toBe(false)+ expect(isGreaterThanOrEqual('1.2.3', 'invalid')).toBe(false)+ })+})++describe('isLessThanOrEqual', () => {+ it('should return true when first version is less or equal', () => {+ expect(isLessThanOrEqual('1.0.0', '2.0.0')).toBe(true)+ expect(isLessThanOrEqual('1.2.3', '1.2.3')).toBe(true)+ expect(isLessThanOrEqual('1.2.3', '1.2.4')).toBe(true)+ })++ it('should return false when first version is greater', () => {+ expect(isLessThanOrEqual('2.0.0', '1.0.0')).toBe(false)+ expect(isLessThanOrEqual('1.2.3', 'invalid')).toBe(false)+ })+})++describe('isValidSemver', () => {+ it('should return true for valid semver strings', () => {+ expect(isValidSemver('1.2.3')).toBe(true)+ expect(isValidSemver('0.0.0')).toBe(true)+ expect(isValidSemver('10.20.30')).toBe(true)+ })++ it('should return false for invalid semver strings', () => {+ expect(isValidSemver('1.2')).toBe(false)+ expect(isValidSemver('1.2.3.4')).toBe(false)+ expect(isValidSemver('invalid')).toBe(false)+ expect(isValidSemver('')).toBe(false)+ })+})diff --git a/apps/studio/lib/semver.ts b/apps/studio/lib/semver.tsnew file mode 100644index 00000000000..969e8d83ff3--- /dev/null+++ b/apps/studio/lib/semver.ts@@ -0,0 +1,130 @@+/**+ * Semantic versioning utility for comparing version strings in the format "x.x.x"+ */++export interface SemverVersion {+ major: number+ minor: number+ patch: number+}++/**+ * Parses a semver string in the format "x.x.x" into its components+ * @param version - The version string to parse (e.g., "1.2.3")+ * @returns The parsed version components or null if invalid+ */+export function parseSemver(version: string): SemverVersion | null {+ if (!version || typeof version !== 'string') {+ return null+ }++ const parts = version.trim().split('.')++ if (parts.length !== 3) {+ return null+ }++ const major = parseInt(parts[0], 10)+ const minor = parseInt(parts[1], 10)+ const patch = parseInt(parts[2], 10)++ if (isNaN(major) || isNaN(minor) || isNaN(patch)) {+ return null+ }++ if (major < 0 || minor < 0 || patch < 0) {+ return null+ }++ return { major, minor, patch }+}++/**+ * Compares two semver version strings+ * @param a - First version string+ * @param b - Second version string+ * @returns -1 if a < b, 0 if a === b, 1 if a > b, or null if either version is invalid+ */+export function compareSemver(a: string, b: string): -1 | 0 | 1 | null {+ const versionA = parseSemver(a)+ const versionB = parseSemver(b)++ if (!versionA || !versionB) {+ return null+ }++ if (versionA.major !== versionB.major) {+ return versionA.major > versionB.major ? 1 : -1+ }++ if (versionA.minor !== versionB.minor) {+ return versionA.minor > versionB.minor ? 1 : -1+ }++ if (versionA.patch !== versionB.patch) {+ return versionA.patch > versionB.patch ? 1 : -1+ }++ return 0+}++/**+ * Checks if version a is greater than version b+ * @param a - First version string+ * @param b - Second version string+ * @returns true if a > b, false otherwise+ */+export function isGreaterThan(a: string, b: string): boolean {+ return compareSemver(a, b) === 1+}++/**+ * Checks if version a is less than version b+ * @param a - First version string+ * @param b - Second version string+ * @returns true if a < b, false otherwise+ */+export function isLessThan(a: string, b: string): boolean {+ return compareSemver(a, b) === -1+}++/**+ * Checks if version a is equal to version b+ * @param a - First version string+ * @param b - Second version string+ * @returns true if a === b, false otherwise+ */+export function isEqual(a: string, b: string): boolean {+ return compareSemver(a, b) === 0+}++/**+ * Checks if version a is greater than or equal to version b+ * @param a - First version string+ * @param b - Second version string+ * @returns true if a >= b, false otherwise+ */+export function isGreaterThanOrEqual(a: string, b: string): boolean {+ const result = compareSemver(a, b)+ return result === 1 || result === 0+}++/**+ * Checks if version a is less than or equal to version b+ * @param a - First version string+ * @param b - Second version string+ * @returns true if a <= b, false otherwise+ */+export function isLessThanOrEqual(a: string, b: string): boolean {+ const result = compareSemver(a, b)+ return result === -1 || result === 0+}++/**+ * Checks if a version string is valid+ * @param version - The version string to validate+ * @returns true if the version is valid, false otherwise+ */+export function isValidSemver(version: string): boolean {+ return parseSemver(version) !== null+}