-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
197 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export function Component(properties: {message: string}) { | ||
return ( | ||
<body> | ||
<h1>{properties.message}</h1> | ||
</body> | ||
); | ||
} | ||
|
||
export const HelloWorld = <Component message='Hello from server!' />; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,85 @@ | ||
import semver from 'semver'; | ||
import {getLatestRelease, getAssetsForPlatform} from '../services/github-service'; | ||
|
||
export const electronController = { | ||
async getLatest(platform: string, version: string, channel: string) { | ||
const release = await getLatestRelease('electron'); | ||
|
||
if (!release) { | ||
return {status: 404, error: 'No release found'}; | ||
} | ||
|
||
/** | ||
* Backwards compatibility for electron versions < 1.4.0 | ||
* It expects the response to be in the format: | ||
* { | ||
* version: '1.3.0', | ||
* ... | ||
* } | ||
*/ | ||
if (semver.lt(version, '1.4.0')) { | ||
return { | ||
version: release.tag_name.replace(/v/, ''), | ||
name: release.name, | ||
assets: getAssetsForPlatform(release.assets, platform), | ||
releaseDate: release.published_at, | ||
notes: release.body, | ||
}; | ||
} | ||
/** | ||
* | ||
* @param platform | ||
* @param version | ||
* @param channel | ||
* @returns | ||
*/ | ||
async function getLatest(platform: string, version: string, channel: string) { | ||
const release = await getLatestRelease('electron'); | ||
|
||
if (!release) { | ||
return {status: 404, error: 'No release found'}; | ||
} | ||
|
||
/** | ||
* Backwards compatibility for electron versions < 1.4.0 | ||
* It expects the response to be in the format: | ||
* { | ||
* version: '1.3.0', | ||
* ... | ||
* } | ||
*/ | ||
if (semver.lt(version, '1.4.0')) { | ||
return { | ||
status: 200, | ||
data: { | ||
version: release.tag_name.replace(/v/, ''), | ||
name: release.name, | ||
assets: getAssetsForPlatform(release.assets, platform), | ||
releaseDate: release.published_at, | ||
notes: release.body, | ||
}, | ||
version: release.tag_name.replace(/v/, ''), | ||
name: release.name, | ||
assets: getAssetsForPlatform(release.assets, platform), | ||
releaseDate: release.published_at, | ||
notes: release.body, | ||
}; | ||
}, | ||
} | ||
|
||
return { | ||
status: 200, | ||
data: { | ||
version: release.tag_name.replace(/v/, ''), | ||
name: release.name, | ||
assets: getAssetsForPlatform(release.assets, platform), | ||
releaseDate: release.published_at, | ||
notes: release.body, | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Just getting latest at the moment, but could be adjust to get specific version | ||
* | ||
* @param platform | ||
* @param version | ||
* @param channel | ||
*/ | ||
async function getDownloadUrl(platform: string, version: string, channel: string) { | ||
const release = await getLatestRelease('electron'); | ||
|
||
// Define a mapping of platforms to their corresponding file extensions and partial identifiers | ||
const platformMapping = { | ||
'darwin-arm64': {extension: 'dmg', identifier: 'arm64'}, | ||
'darwin-x64': {extension: 'dmg', identifier: 'x64'}, | ||
'win32-x64': {extension: 'exe', identifier: 'Setup'}, // This is a hack, I need to label the assets properly | ||
}; | ||
|
||
const {extension, identifier} = platformMapping[platform]; | ||
|
||
// Filter for assets matching the specified platform by extension and, if applicable, identifier | ||
const filteredAssets = release.assets.filter(asset => { | ||
const matchesExtension = asset.name.endsWith(`.${extension}`); | ||
// If an identifier is specified for the platform, check for its presence | ||
const matchesIdentifier = identifier ? asset.name.includes(identifier) : true; | ||
return matchesExtension && matchesIdentifier; | ||
}); | ||
|
||
// Assuming we're interested in the first match | ||
return filteredAssets.length > 0 ? filteredAssets[0].browser_download_url : { | ||
status: 404, | ||
error: 'No download found', | ||
}; | ||
} | ||
|
||
export const electronController = { | ||
getLatest, | ||
getDownloadUrl, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,58 @@ | ||
import {Elysia, t} from 'elysia'; | ||
import {Stream} from '@elysiajs/stream'; | ||
import {electronController} from '../controllers/electron-controller'; | ||
import type {ElysiaApp} from '../types'; | ||
|
||
export const electronRoutes = (app: ElysiaApp) => { | ||
app.get('/electron/:platform/:version', | ||
const handleDownload = async ({params, set}) => { | ||
// If 'latest' or no version is provided, you might want to dynamically determine the latest version | ||
const version = params.version || 'latest'; | ||
const downloadUrl = await electronController.getDownloadUrl(params.platform, version, ''); | ||
|
||
if (typeof downloadUrl === 'string') { | ||
// Extract the filename from the downloadUrl | ||
const filename = downloadUrl.split('/').pop(); | ||
set.headers['Content-Disposition'] = `attachment; filename="${filename}"`; | ||
|
||
return new Stream( | ||
fetch(downloadUrl, { | ||
headers: { | ||
Authorization: `token ${process.env.GITHUB_PAT}`, | ||
Accept: 'application/octet-stream', | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
set.status = downloadUrl?.status; | ||
return downloadUrl; | ||
}; | ||
|
||
export const electronRoutes = new Elysia({prefix: '/electron'}) | ||
// Route with version | ||
.get('/download/:platform/:version', | ||
handleDownload, | ||
{ | ||
params: t.Object({ | ||
platform: t.String(), | ||
version: t.String(), | ||
// Channel: t.Union([t.String(), t.Undefined()]), | ||
}), | ||
}, | ||
) | ||
// Route without version, treating version as optional | ||
.get('/download/:platform', handleDownload, { | ||
params: t.Object({ | ||
platform: t.String(), | ||
// Version is omitted here, making it optional in practice | ||
}), | ||
}) | ||
.get('/:platform/:version', | ||
async ({params}) => electronController.getLatest(params.platform, params.version, ''), | ||
{ | ||
params: t.Object({ | ||
platform: t.String(), | ||
version: t.String(), | ||
// Channel: t.Union([t.String(), t.Undefined()]), | ||
// Channel: t.Union([t.String(), t.Undefined()]), | ||
}), | ||
}, | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import {Elysia, t} from 'elysia'; | ||
import {renderToReadableStream} from 'react-dom/server'; | ||
import {Stream} from '@elysiajs/stream'; | ||
import {HelloWorld} from '../components/hello-world.tsx'; | ||
|
||
export const wpAdminRoutes = new Elysia({prefix: '/wp-admin'}) | ||
.get('/landing', | ||
async ({params, query, set}) => { | ||
// Set CORS headers | ||
set.headers['Access-Control-Allow-Origin'] = '*'; | ||
set.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'; | ||
set.headers['Access-Control-Allow-Headers'] = 'Content-Type'; | ||
set.headers['Access-Control-Allow-Credentials'] = 'true'; | ||
|
||
const stream = await renderToReadableStream( | ||
HelloWorld, | ||
); | ||
|
||
return new Stream( | ||
stream, | ||
// { | ||
// status: 200, | ||
// headers: { | ||
// 'Content-Type': 'text/html', | ||
// }, | ||
// }, | ||
); | ||
}, | ||
{ | ||
// Params: t.Object({ | ||
// version: t.String(), | ||
// }), | ||
// query: t.Object({ | ||
// key: t.String(), | ||
// instance: t.String(), | ||
// }), | ||
// Response: responseModels, | ||
}, | ||
); | ||
|