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

feat: update_lite #997

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cloudflare_workers/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { app as updates } from '../../supabase/functions/_backend/plugins/update
import { app as latency_drizzle } from '../../supabase/functions/_backend/private/latency_drizzle.ts'
import { app as update_stats } from '../../supabase/functions/_backend/private/updates_stats.ts'
import { app as ok } from '../../supabase/functions/_backend/public/ok.ts'
import { app as updates_lite } from '../../supabase/functions/_backend/plugins/updates_lite.ts'

export { AttachmentUploadHandler, UploadHandler } from '../../supabase/functions/_backend/tus/uploadHandler.ts'

Expand All @@ -37,6 +38,7 @@ app.route('/channel_self', channel_self)
app.route('/updates', updates)
app.route('/updates_v2', updates)
app.route('/updates_debug', updates)
app.route('/updates_lite', updates_lite)
app.route('/stats', stats)

app.onError((e, c) => {
Expand Down
100 changes: 100 additions & 0 deletions supabase/functions/_backend/plugins/updates_lite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import type { Context } from '@hono/hono'
import type { AppInfos } from '../utils/types.ts'
import { canParse } from '@std/semver'
import { Hono } from 'hono/tiny'
import { z } from 'zod'
import { update as updateLite } from '../utils/update_lite.ts'
import {
deviceIdRegex,
INVALID_STRING_APP_ID,
INVALID_STRING_DEVICE_ID,
INVALID_STRING_PLATFORM,
INVALID_STRING_PLUGIN_VERSION,
isLimited,
MISSING_STRING_APP_ID,
MISSING_STRING_DEVICE_ID,
MISSING_STRING_PLATFORM,
MISSING_STRING_PLUGIN_VERSION,
MISSING_STRING_VERSION_BUILD,
MISSING_STRING_VERSION_NAME,
NON_STRING_APP_ID,
NON_STRING_DEVICE_ID,
NON_STRING_VERSION_BUILD,
NON_STRING_VERSION_NAME,
reverseDomainRegex,
} from '../utils/utils.ts'

const jsonRequestSchema = z.object({
app_id: z.string({
required_error: MISSING_STRING_APP_ID,
invalid_type_error: NON_STRING_APP_ID,
}).min(1, MISSING_STRING_APP_ID),
device_id: z.string({
required_error: MISSING_STRING_DEVICE_ID,
invalid_type_error: NON_STRING_DEVICE_ID,
}).max(36).min(1, MISSING_STRING_DEVICE_ID).refine(id => deviceIdRegex.test(id), {
message: INVALID_STRING_DEVICE_ID,
}),
version_name: z.string({
required_error: MISSING_STRING_VERSION_NAME,
invalid_type_error: NON_STRING_VERSION_NAME,
}).min(1, MISSING_STRING_VERSION_NAME),
version_build: z.string({
required_error: MISSING_STRING_VERSION_BUILD,
invalid_type_error: NON_STRING_VERSION_BUILD,
}).min(1, MISSING_STRING_VERSION_BUILD),
is_emulator: z.boolean().default(false),
defaultChannel: z.optional(z.string()),
is_prod: z.boolean().default(true),
platform: z.string({
required_error: MISSING_STRING_PLATFORM,
invalid_type_error: INVALID_STRING_PLATFORM,
}).refine(platform => ['android', 'ios'].includes(platform), {
message: INVALID_STRING_PLATFORM,
}),
plugin_version: z.string({
required_error: MISSING_STRING_PLUGIN_VERSION,
invalid_type_error: INVALID_STRING_PLUGIN_VERSION,
}).refine(version => canParse(version), {
message: INVALID_STRING_PLUGIN_VERSION,
}),
}).refine(data => reverseDomainRegex.test(data.app_id), {
message: INVALID_STRING_APP_ID,
}).transform((val) => {
if (val.version_name === 'builtin')
val.version_name = val.version_build
return val
})

export const app = new Hono()

app.post('/', async (c: Context) => {
try {
const body = await c.req.json<AppInfos>()
console.log({ requestId: c.get('requestId'), context: 'post updates body', body })
if (isLimited(c, body.app_id)) {
return c.json({
status: 'Too many requests',
error: 'too_many_requests',
}, 200)
}
const parseResult = jsonRequestSchema.safeParse(body)
if (!parseResult.success) {
const error = parseResult.error.errors[0]
console.log({ requestId: c.get('requestId'), context: 'parseResult', error: error.message })
return c.json({
error: `Cannot parse json: ${error.message}`,
}, 400)
}

return updateLite(c, body)
}
catch (e) {
console.log({ requestId: c.get('requestId'), context: 'error', error: JSON.stringify(e) })
return c.json({ status: 'Cannot get updates', error: JSON.stringify(e) }, 400)
}
})

app.get('/', (c: Context) => {
return c.json({ status: 'ok' })
})
52 changes: 52 additions & 0 deletions supabase/functions/_backend/utils/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,55 @@ export async function getAppOwnerPostgres(
return null
}
}

export function requestInfosPostgresLite(
app_id: string,
version_name: string,
drizzleCient: ReturnType<typeof getDrizzleClient>,
) {
const { versionAlias, channelAlias } = getAlias()

const appVersions = drizzleCient
.select({
id: versionAlias.id,
})
.from(versionAlias)
.where(or(eq(versionAlias.name, version_name), eq(versionAlias.app_id, app_id)))
.limit(1)
.then(data => data.at(0))

const channel = drizzleCient
.select({
version: {
id: sql<number>`${versionAlias.id}`.as('vid'),
name: sql<string>`${versionAlias.name}`.as('vname'),
checksum: sql<string | null>`${versionAlias.checksum}`.as('vchecksum'),
session_key: sql<string | null>`${versionAlias.session_key}`.as('vsession_key'),
storage_provider: sql<string>`${versionAlias.storage_provider}`.as('vstorage_provider'),
external_url: sql<string | null>`${versionAlias.external_url}`.as('vexternal_url'),
min_update_version: sql<string | null>`${versionAlias.min_update_version}`.as('vminUpdateVersion'),
r2_path: sql`${versionAlias.r2_path}`.mapWith(versionAlias.r2_path).as('vr2_path'),
manifest: sql`${versionAlias.manifest}`.mapWith(versionAlias.manifest).as('vmanifest'),
},
channels: {
id: channelAlias.id,
name: channelAlias.name,
app_id: channelAlias.app_id,
public: channelAlias.public,
},
})
.from(channelAlias)
.innerJoin(versionAlias, eq(channelAlias.version, versionAlias.id))
.where(and(
eq(channelAlias.public, true),
eq(channelAlias.app_id, app_id),
))
.limit(1)
.then(data => data.at(0))

return Promise.all([channel, appVersions])
.then(([channelData, versionData]) => ({ versionData, channelData }))
.catch((e) => {
throw e
})
}
Loading
Loading