From 8ce2220e3f95d5cbf35884956c298898dd52e016 Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Wed, 4 Oct 2023 02:18:29 -0600 Subject: [PATCH] =?UTF-8?q?feat(helia):=20=E2=9C=A8=20Add=20version=20stri?= =?UTF-8?q?ng.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- src/heliaServer.ts | 30 +++++++++++++++++++++++++++++- src/types.d.ts | 4 ++++ tsconfig.json | 6 +++++- 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/types.d.ts diff --git a/src/heliaServer.ts b/src/heliaServer.ts index bed01e8..32c8d79 100644 --- a/src/heliaServer.ts +++ b/src/heliaServer.ts @@ -3,6 +3,8 @@ import { type Request, type Response } from 'express' import { DEFAULT_MIME_TYPE, parseContentType } from './contentType.js' import { HeliaFetch } from './heliaFetch.js' +const HELIA_RELEASE_INFO_API = (version: string): string => `https://api.github.com/repos/ipfs/helia/git/ref/tags/helia-v${version}` + export interface IRouteEntry { path: string type: 'get' | 'post' @@ -42,8 +44,12 @@ class HeliaServer { type: 'get', handler: async (request, response): Promise => this.fetchIpns({ request, response }) }, { - path: '/api/v0/repo/gc', + path: '/api/v0/version', type: 'get', + handler: async (request, response): Promise => this.heliaVersion({ request, response }) + }, { + path: '/api/v0/repo/gc', + type: 'post', handler: async (request, response): Promise => this.gc({ request, response }) }, { path: '/*', @@ -132,6 +138,28 @@ class HeliaServer { } } + /** + * Get the helia version + */ + async heliaVersion ({ response }: IRouteHandler): Promise { + await this.isReady + + try { + const { default: packageJson } = await import('../../node_modules/helia/package.json', { + assert: { type: 'json' } + }) + const { version: heliaVersionString } = packageJson + + const ghResp = await (await fetch(HELIA_RELEASE_INFO_API(heliaVersionString))).json() + response.json({ + Version: heliaVersionString, + Commit: ghResp.object.sha.slice(0, 7) + }) + } catch (error) { + response.status(500).end() + } + } + /** * GC the node */ diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 0000000..8838047 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,4 @@ +declare module '*/package.json' { + export const name: string + export const version: string +} diff --git a/tsconfig.json b/tsconfig.json index 05bfbfd..900044b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,11 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { - "outDir": "dist" + "outDir": "dist", + "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, + "moduleResolution": "NodeNext", + "module": "NodeNext" }, "include": [ "src",