Skip to content

Commit

Permalink
offline using hono web server
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam McKee committed Oct 16, 2024
1 parent fce76a4 commit e39a3c4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 696 deletions.
6 changes: 2 additions & 4 deletions offline/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@
},
"dependencies": {
"@eighty4/install-github": "workspace:*",
"cookie-parser": "^1.4.6",
"express": "5.0.0-beta.3"
"@hono/node-server": "^1.13.2",
"hono": "4.6.5"
},
"devDependencies": {
"@types/cookie-parser": "^1.4.7",
"@types/express": "^4.17.21",
"typescript": "^5.4.5"
}
}
57 changes: 23 additions & 34 deletions offline/src/Offline.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
import cookieParser from 'cookie-parser'
import express from 'express'
import {serve} from '@hono/node-server'
import {Hono} from 'hono'
import {logger} from 'hono/logger'
import {handleGraphQuery} from './data.js'

const HTTP_PORT = 7411

const app = express()
app.use(cookieParser())
app.use(express.json())
app.use((req, res, next) => {
const start = Date.now()
res.on('finish', () => {
const uri = decodeURI(req.url)
const end = `${Date.now() - start}ms`
if (res.statusCode === 301) {
console.log(req.method, uri, res.statusCode, res.statusMessage, 'to', res.getHeaders().location, end)
} else {
console.log(req.method, uri, res.statusCode, res.statusMessage, end)
}
})
next()
})
const app = new Hono()
app.use(logger())

app.post('/offline/github/graph', (req, res) => {
res.json({data: handleGraphQuery(req.body.query)})
app.post('/offline/github/graph', async (c) => {
const json = await c.req.json()
return c.json({data: handleGraphQuery(json.query)})
})

app.get('/offline/github/oauth', async (req, res) => {
app.get('/offline/github/oauth', (c) => {
const accessToken = '1234'
// todo does Safari work with Secure and SameSite=Strict when the hostname isn't localhost?
if (req.header('user-agent')?.includes('Safari')) {
res.setHeader('Set-Cookie', `ght=${accessToken}; Path=/`)
if (c.req.header('User-Agent')?.includes('Safari')) {
c.res.headers.append('Set-Cookie', `ght=${accessToken}; Path=/`)
} else {
res.setHeader('Set-Cookie', `ght=${accessToken}; Secure; SameSite=Strict; Path=/`)
c.res.headers.append('Set-Cookie', `ght=${accessToken}; Secure; SameSite=Strict; Path=/`)
}
res.redirect('http://localhost:5711')
return c.redirect('http://localhost:5711', 302)
})

const generatedScripts: Array<any> = []

app.get('/offline/api/generated-scripts', async (_req, res) => {
res.json(generatedScripts)
})
app.get('/offline/api/generated-scripts', (c) => c.json(generatedScripts))

app.post('/offline/api/generated-scripts', async (req, res) => {
generatedScripts.push(req.body)
console.log(generatedScripts)
res.end()
app.post('/offline/api/generated-scripts', async (c) => {
const generatedScript = await c.req.json()
generatedScripts.push(generatedScript)
console.log(JSON.stringify(generatedScript, null, 4))
return c.body('')
})

app.listen(HTTP_PORT, () => {
console.log('install.sh http listening on', 7411)
serve({
fetch: app.fetch,
port: HTTP_PORT,
})

Loading

0 comments on commit e39a3c4

Please sign in to comment.