mirror of
https://github.com/sasjs/server.git
synced 2026-01-16 10:20:05 +00:00
fix(file): fixes response headers
This commit is contained in:
@@ -41,7 +41,10 @@ const executeSASCode = async (req: any, { code }: ExecuteSASCodePayload) => {
|
|||||||
true
|
true
|
||||||
)) as ExecuteReturnJson
|
)) as ExecuteReturnJson
|
||||||
|
|
||||||
if (webout instanceof Buffer) return webout
|
if (webout instanceof Buffer) {
|
||||||
|
;(req as any).sasHeaders = httpHeaders
|
||||||
|
return webout
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
|
|||||||
@@ -148,7 +148,7 @@ ${program}`
|
|||||||
? await readFile(headersPath)
|
? await readFile(headersPath)
|
||||||
: ''
|
: ''
|
||||||
const httpHeaders: HTTPHeaders = extractHeaders(headersContent)
|
const httpHeaders: HTTPHeaders = extractHeaders(headersContent)
|
||||||
const fileResponse: boolean = httpHeaders.hasOwnProperty('Content-Type')
|
const fileResponse: boolean = httpHeaders.hasOwnProperty('content-type')
|
||||||
|
|
||||||
const webout = (await fileExists(weboutPath))
|
const webout = (await fileExists(weboutPath))
|
||||||
? fileResponse
|
? fileResponse
|
||||||
|
|||||||
@@ -113,8 +113,9 @@ const executeReturnRaw = async (
|
|||||||
|
|
||||||
req.res?.set(httpHeaders)
|
req.res?.set(httpHeaders)
|
||||||
|
|
||||||
// if (result instanceof Buffer)
|
if (result instanceof Buffer) {
|
||||||
// req.res?.writeHead(200, httpHeaders)
|
;(req as any).sasHeaders = httpHeaders
|
||||||
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
@@ -148,7 +149,10 @@ const executeReturnJson = async (
|
|||||||
true
|
true
|
||||||
)) as ExecuteReturnJson
|
)) as ExecuteReturnJson
|
||||||
|
|
||||||
if (webout instanceof Buffer) return webout
|
if (webout instanceof Buffer) {
|
||||||
|
;(req as any).sasHeaders = httpHeaders
|
||||||
|
return webout
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: 'success',
|
status: 'success',
|
||||||
|
|||||||
@@ -12,6 +12,12 @@ runRouter.post('/execute', async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await controller.executeSASCode(req, body)
|
const response = await controller.executeSASCode(req, body)
|
||||||
|
|
||||||
|
if (response instanceof Buffer) {
|
||||||
|
res.writeHead(200, (req as any).sasHeaders)
|
||||||
|
return res.end(response)
|
||||||
|
}
|
||||||
|
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const statusCode = err.code
|
const statusCode = err.code
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ stpRouter.get('/execute', async (req, res) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await controller.executeReturnRaw(req, query._program)
|
const response = await controller.executeReturnRaw(req, query._program)
|
||||||
|
|
||||||
|
if (response instanceof Buffer) {
|
||||||
|
res.writeHead(200, (req as any).sasHeaders)
|
||||||
|
return res.end(response)
|
||||||
|
}
|
||||||
|
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const statusCode = err.code
|
const statusCode = err.code
|
||||||
@@ -40,6 +46,12 @@ stpRouter.post(
|
|||||||
body,
|
body,
|
||||||
query?._program
|
query?._program
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (response instanceof Buffer) {
|
||||||
|
res.writeHead(200, (req as any).sasHeaders)
|
||||||
|
return res.end(response)
|
||||||
|
}
|
||||||
|
|
||||||
res.send(response)
|
res.send(response)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const statusCode = err.code
|
const statusCode = err.code
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ export const extractHeaders = (content?: string): HTTPHeaders => {
|
|||||||
const [key, value] = headerStr.split(':').map((data) => data.trim())
|
const [key, value] = headerStr.split(':').map((data) => data.trim())
|
||||||
|
|
||||||
if (value && headerUtils.validateHeader(key, value)) {
|
if (value && headerUtils.validateHeader(key, value)) {
|
||||||
headersObj[key] = value
|
headersObj[key.toLowerCase()] = value
|
||||||
} else {
|
} else {
|
||||||
delete headersObj[key]
|
delete headersObj[key.toLowerCase()]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ describe('extractHeaders', () => {
|
|||||||
`)
|
`)
|
||||||
|
|
||||||
expect(headers).toEqual({
|
expect(headers).toEqual({
|
||||||
'Content-type': 'application/zip',
|
'content-type': 'application/zip',
|
||||||
'Cache-Control': 'public, max-age=1000'
|
'cache-control': 'public, max-age=1000'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -25,7 +25,7 @@ describe('extractHeaders', () => {
|
|||||||
Content-type:
|
Content-type:
|
||||||
`)
|
`)
|
||||||
|
|
||||||
expect(headers).toEqual({ 'Cache-Control': 'public, max-age=1000' })
|
expect(headers).toEqual({ 'cache-control': 'public, max-age=1000' })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return only valid http headers', () => {
|
it('should return only valid http headers', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user