Http Trigger V4
Check the Azure Handler docs to make sure you have all the necessary settings to work with this adapter.
The adapter to handle requests from Azure Http Trigger V4.
When an error is thrown during forwarding and the responseWithErrors option is true, we return a 500 status WITH error stack in the response.
Requirements
This adapter was created to work with runtime 4.x, see the docs to know more about how to check and change the version if you need.
In theory, this adapter works with runtime 3.x but I haven't tested it.
About the adapter
This adapter transforms every request coming from Azure Http Trigger V4 into an HTTP request to your framework.
{
"method":"POST",
"url":"https://serverless-adapter.azurewebsites.net/api/test-serverless-adapter/events?code=sE_d8h7XJ4YYsGJ7mgVta_t-32323%253D%253D",
"headers":{
"accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-encoding":"gzip, deflate, br",
"accept-language":"pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,bg;q=0.6",
"cache-control":"max-age=0",
"host":"serverless-adapter.azurewebsites.net",
"user-agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
"sec-ch-ua":"\".Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"103\", \"Chromium\";v=\"103\"",
"sec-ch-ua-mobile":"?0",
"sec-ch-ua-platform":"\"Linux\"",
"client-ip":"2.3.3.3:30750",
"x-forwarded-proto":"https",
"x-appservice-proto":"https",
"x-arr-ssl":"2048|256|CN=Microsoft Azure TLS Issuing CA 01, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US",
"x-forwarded-tlsversion":"1.2",
"x-forwarded-for":"3.3.3.3:49196",
"x-original-url":"/api/test-serverless-adapter?code=sE_d8h7XJ4YYsGJ7mgVta_t-32323%3D%3D",
"x-waws-unencoded-url":"/api/test-serverless-adapter?code=sE_d8h7XJ4YYsGJ7mgVta_t-32323%3D%3D",
"disguised-host":"serverless-adapter.azurewebsites.net"
},
"query":{
"code":"sE_d8h7XJ4YYsGJ7mgVta_t-32323%3D%3D"
},
"params":{},
"body":{
"name":"H4ad Event"
},
"rawBody":"{\"name\":\"H4ad Event\"}",
"user":null
}
So, to add support to the above request, we must have registered the /api/test-serverless-adapter/events route as POST and when Http Trigger sends this event, you will get:
body:{"name":"H4ad Event"}queryString:code=sE_d8h7XJ4YYsGJ7mgVta_t-32323%3D%3D
Customizing
You can strip base path with the option stripBasePath inside HttpTriggerV4AdapterOptions.
You can configure this option based on your api base url like: /api/test-serverless-adapter, then the request coming from /api/test-serverless-adapter/events
will be transformed into /events.
You can also return binary response bodies as Uint8Array with the option shouldReturnBodyAsUint8Array. This is useful when Azure returns ERR_CONTENT_LENGTH_MISMATCH for binary responses.
This option only changes responses that were already marked as binary by setBinarySettings. The adapter only decodes base64 response bodies into Uint8Array; it does not convert plain text, JSON, or any other non-base64 body format.
Configure setBinarySettings so the framework response is encoded as base64, then use shouldReturnBodyAsUint8Array to decide which base64 responses Azure should receive as Uint8Array.
The callback receives the flattened response headers with lower-case keys. Return true only for responses that should be decoded from base64 and sent to Azure as a Uint8Array.
const adapter = new HttpTriggerV4Adapter({
shouldReturnBodyAsUint8Array: headers =>
headers['content-type']?.startsWith('image/') ||
headers['content-type'] === 'application/pdf',
});
Usage
To add support to Azure Http Trigger you do the following:
import { ServerlessAdapter } from '@h4ad/serverless-adapter';
import { AzureHandlerV3 } from '@h4ad/serverless-adapter/handlers/azure';
import { PromiseResolver } from '@h4ad/serverless-adapter/resolvers/promise';
import { HttpTriggerV4Adapter } from '@h4ad/serverless-adapter/adapters/azure';
import app from './app';
export const handler = ServerlessAdapter.new(app)
.setHandler(new AzureHandlerV3())
// .setFramework(new ExpressFramework())
.setResolver(new PromiseResolver())
// Use this when returning binary responses such as images or PDFs.
// .setBinarySettings({
// contentTypes: ['image/png', 'image/jpeg', 'application/pdf'],
// contentEncodings: [],
// })
.addAdapter(new HttpTriggerV4Adapter())
// customizing:
// .addAdapter(new HttpTriggerV4Adapter({ stripBasePath: '/api/test-serverless-adapter' }))
// .addAdapter(new HttpTriggerV4Adapter({
// shouldReturnBodyAsUint8Array: headers => headers['content-type']?.startsWith('image/'),
// }))
.build();
You MUST use the PromiseResolver combined with Azure Handler to make this adapter work.
Credits
This handler was created inspired by code written by @Shamshiel and @zachabney, thank you very much to them!