Skip to content

Commit

Permalink
fix(response): allow headers in sendResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlindquist committed Sep 27, 2024
1 parent bf196ac commit 2d46931
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 598 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
"color-name": "2.0.0",
"dotenv": "^16.4.5",
"esbuild": "0.24.0",
"express": "^4.21.0",
"filesize": "10.1.6",
"fs-extra": "^11.2.0",
"globby": "^14.0.2",
Expand All @@ -116,8 +115,6 @@
"devDependencies": {
"@biomejs/biome": "1.9.2",
"@types/debug": "4.1.12",
"@types/express": "4.17.21",
"@types/express-ws": "3.0.5",
"@types/node": "^22.6.0",
"@types/node-ipc": "9.2.3",
"ava": "^6.1.3",
Expand Down
324 changes: 0 additions & 324 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

22 changes: 21 additions & 1 deletion src/api/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,27 @@ global.send = (channel: Channel, value?: any) => {
}
}

global.sendResponse = (response: any) => {
global.sendResponse = (
body: any,
headers: Record<string, string> = {}
) => {
let statusCode = 200
if (headers["Status-Code"]) {
statusCode = Number.parseInt(headers["Status-Code"], 10)
headers["Status-Code"] = undefined
}

const responseHeaders = { ...headers }
if (!responseHeaders["Content-Type"]) {
responseHeaders["Content-Type"] = "application/json"
}

const response = {
body,
statusCode,
headers: responseHeaders,
}

return global.sendWait(Channel.RESPONSE, response)
}

Expand Down
31 changes: 0 additions & 31 deletions src/build/server.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,9 @@ export let tagger = (script: Script) => {
tags.push(`keyword: ${script.keyword}`)
if (script.snippet)
tags.push(`snippet ${script.snippet}`)
if (script.expand) {
tags.push(`expand: ${script.expand}`)
}

if (
typeof script.pass === "string" &&
Expand Down
209 changes: 110 additions & 99 deletions src/run/app-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,30 @@ import { configEnv, run } from "../core/utils.js"
// TODO: Fix the types around accepting an early Scriptlet
let script: any = ""
let args = []
let tooEarlyHandler = (data) => {
if (data.channel === Channel.VALUE_SUBMITTED) {
script = data?.value?.scriptlet
? data?.value
: data?.value?.script || data?.state?.value?.filePath
args = data?.value?.args || data?.state?.value?.args || []

// const value = `${process.pid}: ${
// data?.channel
// }: ${script} ${performance.now()}ms`
// process.send({
// channel: Channel.CONSOLE_LOG,
// value,
// });
}
global.headers = {}
let tooEarlyHandler = data => {
if (data.channel === Channel.VALUE_SUBMITTED) {
script = data?.value?.scriptlet
? data?.value
: data?.value?.script || data?.state?.value?.filePath
args =
data?.value?.args || data?.state?.value?.args || []
global.headers = data?.value?.headers || {}

console.log("headers", global.headers)
// const value = `${process.pid}: ${
// data?.channel
// }: ${script} ${performance.now()}ms`
// process.send({
// channel: Channel.CONSOLE_LOG,
// value,
// });
}
}

process.send({
channel: Channel.KIT_LOADING,
value: "app-prompt.ts"
channel: Channel.KIT_LOADING,
value: "app-prompt.ts",
})

process.on("message", tooEarlyHandler)
Expand All @@ -39,20 +43,22 @@ await import("../platform/base.js")

let platform = os.platform()
try {
await import(`../platform/${platform}.js`)
await import(`../platform/${platform}.js`)
} catch (error) {
// console.log(`No ./platform/${platform}.js`)
// console.log(`No ./platform/${platform}.js`)
}

await import("../target/app.js")

if (process.env.KIT_MEASURE) {
let { PerformanceObserver, performance } = await import("node:perf_hooks")
let obs = new PerformanceObserver((list) => {
let entry = list.getEntries()[0]
log(`⌚️ [Perf] ${entry.name}: ${entry.duration}ms`)
})
obs.observe({ entryTypes: ["measure"] })
let { PerformanceObserver, performance } = await import(
"node:perf_hooks"
)
let obs = new PerformanceObserver(list => {
let entry = list.getEntries()[0]
log(`⌚️ [Perf] ${entry.name}: ${entry.duration}ms`)
})
obs.observe({ entryTypes: ["measure"] })
}

let trigger = ""
Expand All @@ -62,96 +68,101 @@ let choices = []
let scriptlet = null
process.title = "Kit Idle - App Prompt"
process.send({
channel: Channel.KIT_READY,
value: result
channel: Channel.KIT_READY,
value: result,
})

try {
result = await new Promise((resolve, reject) => {
process.off("message", tooEarlyHandler)

if (script) {
// process.send({
// channel: Channel.CONSOLE_LOG,
// value: `Too early ${tooEarly}...`,
// })

// TODO: Revisit what causes "too early" and the edge-cases here
if (script?.scriptlet) {
resolve(script)
return
}
resolve({
script,
args,
trigger: Trigger.Trigger
})
return
}

type MessageData = {
channel: Channel
value: any
}

let messageHandler = (data: MessageData) => {
if (data.channel === Channel.HEARTBEAT) {
send(Channel.HEARTBEAT)
}
if (data.channel === Channel.VALUE_SUBMITTED) {
trace.instant({
name: "app-prompt.ts -> VALUE_SUBMITTED",
args: data
})
process.off("message", messageHandler)
resolve(data.value)
}
}
process.on("message", messageHandler)
})
result = await new Promise((resolve, reject) => {
process.off("message", tooEarlyHandler)

if (script) {
// process.send({
// channel: Channel.CONSOLE_LOG,
// value: `Too early ${tooEarly}...`,
// })

// TODO: Revisit what causes "too early" and the edge-cases here
if (script?.scriptlet) {
resolve(script)
return
}
resolve({
script,
args,
trigger: Trigger.Trigger,
})
return
}

type MessageData = {
channel: Channel
value: any
}

let messageHandler = (data: MessageData) => {
if (data.channel === Channel.HEARTBEAT) {
send(Channel.HEARTBEAT)
}
if (data.channel === Channel.VALUE_SUBMITTED) {
trace.instant({
name: "app-prompt.ts -> VALUE_SUBMITTED",
args: data,
})
global.headers = data?.value?.headers || {}
console.log("headers", global.headers)
process.off("message", messageHandler)
resolve(data.value)
}
}
process.on("message", messageHandler)
})
} catch (e) {
global.warn(e)
exit()
global.warn(e)
exit()
}
;({ script, args, trigger, choices, name, scriptlet } = result)
;({ script, args, trigger, choices, name, scriptlet } =
result)

process.env.KIT_TRIGGER = trigger

configEnv()
process.title = `Kit - ${path.basename(script)}`

process.once("beforeExit", () => {
if (global?.trace?.flush) {
global.trace.flush()
global.trace = null
}
send(Channel.BEFORE_EXIT)
if (global?.trace?.flush) {
global.trace.flush()
global.trace = null
}
send(Channel.BEFORE_EXIT)
})

performance.mark("run")

if (choices?.length) {
let inputs = []

if (choices[0].inputs?.length) {
inputs = await arg<string[]>(
{
name,
scriptlet: true,
resize: true,
onEscape: () => {
exit()
}
},
choices
)
}
let { runScriptlet } = await import("../main/scriptlet.js")
await runScriptlet(scriptlet, inputs, flag)
let inputs = []

if (choices[0].inputs?.length) {
inputs = await arg<string[]>(
{
name,
scriptlet: true,
resize: true,
onEscape: () => {
exit()
},
},
choices
)
}
let { runScriptlet } = await import(
"../main/scriptlet.js"
)
await runScriptlet(scriptlet, inputs, flag)
} else {
if (script.includes(".md")) {
log({ script, ugh: "❌" })
exit()
}
await run(script, ...args)
if (script.includes(".md")) {
log({ script, ugh: "❌" })
exit()
}
await run(script, ...args)
}
Loading

0 comments on commit 2d46931

Please sign in to comment.