Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ return modelResponse

## Return value

The resolved value is the object the model returns: a `chat.completion` object from a chat model, a `list` of embeddings from an embedding model, and the ranked results from a reranking model. The generated text of a chat model sits at `modelResponse?.choices?.[0]?.message?.content`, which is the content of the first entry in `choices`. Every field of the three shapes is listed in [Model invocation](/en/documentation/platform/ai-inference/model-invocation/#response-fields).
The resolved value is the object the model returns. A chat model returns a `chat.completion` object, an embedding model returns a `list` of embeddings, and a reranking model returns the ranked results. The generated text of a chat model sits at `modelResponse?.choices?.[0]?.message?.content`, which is the content of the first entry in `choices`. Every field of the three shapes is listed in [Model invocation](/en/documentation/platform/ai-inference/model-invocation/#response-fields).

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ The third argument of `get` and `getWithMetadata` is an object, and it carries o
| --- | --- | --- |
| `cacheTtl` | number | Caches the result of the read for that many seconds, so a later read of the same key is served from the cache |

For example, a configuration object that every request reads and a deploy rewrites once a day is read with `cacheTtl` set to 300, so repeated reads within five minutes come from the cache instead of the store.
For example, every request reads a configuration object, and a deploy rewrites it once a day. When the function reads that object with `cacheTtl` set to 300, repeated reads within five minutes come from the cache instead of the store.

This handler reads a configuration key with a cached result:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

The Azion Runtime Metadata API gives a function a set of values about each request: the GeoIP location of the client, its address and port, the protocol, the TLS session, fingerprints, and identifiers. Functions that run on an Application or on a Firewall can read it. Use it to filter and manage access to your application, or to apply different logic for each scenario.
The Azion Runtime Metadata API gives a function a set of values about each request. These values cover the GeoIP location of the client, its address and port, the protocol, the TLS session, fingerprints, and identifiers. Functions that run on an Application or on a Firewall can read it. Use it to filter and manage access to your application, or to apply different logic for each scenario.

:::note
Under `azion dev`, the metadata object is `undefined`. Test metadata reads on a deployed function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import DocItem from '@aziontech/webkit/doc-item'
The Azion Runtime Object Storage API gives a function from [Functions](/en/documentation/platform/functions/) read and write access to the objects of an [Object Storage](/en/documentation/platform/object-storage/) bucket. The function imports the `Storage` class from the `azion:storage` module, names a bucket, and calls `put`, `get`, `delete`, and `list` on it. No token is involved: the constructor takes the bucket name and nothing else.

:::note
Under `azion dev`, the module stores objects on the local disk and behaves differently: `put` without an `options` object fails with `TypeError: Cannot read properties of undefined (reading 'content-length')`, a number in `metadata` is accepted, a missing key throws `ENOENT` instead of `StorageError: Object not found`, and `list` returns keys without the leading slash and without `content_length`. Test storage calls on a deployed function.
Under `azion dev`, the module stores objects on the local disk and behaves differently. Without an `options` object, `put` fails with `TypeError: Cannot read properties of undefined (reading 'content-length')`. The module accepts a number in `metadata`. A missing key throws `ENOENT` instead of `StorageError: Object not found`. The `list` method returns keys without the leading slash and without `content_length`. Test storage calls on a deployed function.
:::

---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A serverless JavaScript runtime runs your code once per request, inside a sandbo

Azion Runtime is that environment for [Functions](/en/documentation/platform/functions/). It runs JavaScript built on Web standards, calls the handler your function exports, and gives the handler the APIs of the platform. Your project reaches it through Azion Bundler, which builds the code and resolves Node.js APIs with polyfills at build time.

The sections cover the execution model, the request lifecycle, handler shapes, the bundler and polyfills, strict mode, time inside a request, environment variables and secrets, and how `azion dev` differs from a deployed function. What decides when a function runs, the rule and the function instance, is on [How Functions works](/en/documentation/platform/functions/how-it-works/).
The sections cover the execution model, the request lifecycle, handler shapes, the bundler and polyfills, and strict mode. They then cover time inside a request, environment variables and secrets, and how `azion dev` differs from a deployed function. What decides when a function runs, the rule and the function instance, is on [How Functions works](/en/documentation/platform/functions/how-it-works/).

---

Expand Down Expand Up @@ -123,7 +123,7 @@ Strict mode changes the normal semantics of JavaScript in three ways:
- It removes mistakes that keep JavaScript engines from optimizing code, so strict-mode code can sometimes run faster than the same code outside strict mode.
- It prohibits some syntax that later versions of ECMAScript are likely to define.

The first change has a cost for code written without strict mode. A mistake that JavaScript outside strict mode ignores throws in a function, and the error surfaces on the request, so an older library that relies on silent failures needs a test before you deploy it.
The first change has a cost for code written without strict mode. A mistake that JavaScript outside strict mode ignores throws in a function, and the error surfaces on the request. As a result, an older library that relies on silent failures needs a test before you deploy it.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

Return a fixed string directly from a function, without contacting an origin server. This is the smallest form of the pattern every function follows, which makes it a useful check that a function is deployed and serving traffic before you add logic to it.
Return a fixed string directly from a function, without contacting an origin server. This is the smallest form of the pattern every function follows. That makes it a useful check that a function is deployed and serving traffic before you add logic to it.

```js
async function handleRequest(request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import DocItem from '@aziontech/webkit/doc-item'

Copy any of these JavaScript snippets into a function as a starting point for your own code. The examples cover the patterns functions are most often used for: returning a response or a JSON payload, redirecting traffic, reading cookies, running an A/B test, and denying a request by country. They are grouped by where the function runs: Applications, for functions that handle the `fetch` event on an application, and Firewall, for functions that handle the `firewall` event on a firewall.

Most handlers in this collection are written in the Service Worker pattern, which registers the handler with `addEventListener`. New code uses ES Modules, and [Process a request body](/en/documentation/platform/functions/process-request-body/) is written that way: refer to [Best practices](/en/documentation/platform/functions/best-practices/) for the current pattern, and to [Migrate handler patterns in Functions](/en/documentation/guides/application-development/functions-and-runtime/migrate-handler-patterns/) for the before-and-after of each handler.
Most handlers in this collection are written in the Service Worker pattern, which registers the handler with `addEventListener`. New code uses ES Modules, and [Process a request body](/en/documentation/platform/functions/process-request-body/) is written that way. Refer to [Best practices](/en/documentation/platform/functions/best-practices/) for the current pattern, and to [Migrate handler patterns in Functions](/en/documentation/guides/application-development/functions-and-runtime/migrate-handler-patterns/) for the before-and-after of each handler.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ When there is a body, the handler reads the `Content-Type` header and routes to
| `application/octet-stream` | `request.blob()` | Reads the body as binary and returns its text |
| Anything else | `request.body.getReader()` | Reads the stream chunk by chunk, decoding each chunk as UTF-8 |

The stream branch is the fallback, so an unrecognized content type is still read rather than dropped. It is also the branch to use when the body is large enough that holding the whole payload in memory matters: the reader hands over one chunk at a time, and the loop ends when `done` is true.
The stream branch is the fallback, so an unrecognized content type is still read rather than dropped. It is also the branch to use when the body is large enough that holding the whole payload in memory matters. The reader hands over one chunk at a time, and the loop ends when `done` is true.

A body can be read only once. Calling two parsing methods on the same request throws, which is why the handler picks exactly one branch and returns it.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ export default {

## How it works

Each example is a complete function in the ES Modules pattern. The `fetch` handler calls `fetch()` with the URL of the API and an options object: `method` selects the HTTP verb, `headers` carries the `Content-Type` and the authorization token, and `body` carries the JSON string that `JSON.stringify()` produces. The token comes from the `API_TOKEN` [environment variable](/en/documentation/devtools/runtime/api-reference/environment-variables/) through `Azion.env.get()`, so it stays out of the code. `await fetch()` resolves to a `Response`. The GET example reads it with `response.json()`, and the other examples return the status and body of the API to the client. Replace `https://api.example.com/items` and the sample values with the ones of your API.
Each example is a complete function in the ES Modules pattern. The `fetch` handler calls `fetch()` with the URL of the API and an options object. In that object, `method` selects the HTTP verb, and `headers` carries the `Content-Type` and the authorization token. The `body` field carries the JSON string that `JSON.stringify()` produces.

The token comes from the `API_TOKEN` [environment variable](/en/documentation/devtools/runtime/api-reference/environment-variables/) through `Azion.env.get()`, so it stays out of the code. `await fetch()` resolves to a `Response`. The GET example reads it with `response.json()`, and the other examples return the status and body of the API to the client. Replace `https://api.example.com/items` and the sample values with the ones of your API.

## Related resources

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Generate a complete HTML page from a string and return it directly from a functi

## How it works

The markup is stored in a template literal assigned to the `html` constant, so the whole page lives inside the function. When the `fetch` event fires, `handleRequest` builds a new `Response` with that string as the body and sets the `content-type` header to `text/html;charset=UTF-8`, which tells the browser to render the payload as a web page instead of plain text. `event.respondWith()` then returns the page to the client, with no request to an origin.
The markup is stored in a template literal assigned to the `html` constant, so the whole page lives inside the function. When the `fetch` event fires, `handleRequest` builds a new `Response` with that string as the body. It also sets the `content-type` header to `text/html;charset=UTF-8`. That header tells the browser to render the payload as a web page instead of plain text. `event.respondWith()` then returns the page to the client, with no request to an origin.

The `content-type` header is the part that matters most here. Without it, the browser has no instruction to render the body as a page, so keep it set whenever a function returns HTML.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

Return a JSON payload directly from a function, without forwarding the request to an origin. Use this pattern for lightweight APIs and middleware, such as API mocks, feature flags, configuration endpoints, and health checks, where the response is structured data and no backend service has to be involved.
Return a JSON payload directly from a function, without forwarding the request to an origin. Use this pattern for lightweight APIs and middleware, such as API mocks, feature flags, configuration endpoints, and health checks. In these cases, the response is structured data and no backend service has to be involved.

```js
addEventListener("fetch", event => {
Expand All @@ -36,9 +36,9 @@ Return a JSON payload directly from a function, without forwarding the request t

When the `fetch` event fires, the handler defines a plain JavaScript object and serializes it with `JSON.stringify(data, null, 2)`, where the `2` adds indentation so the output is easy to read. It then creates a new `Response` with that string as the body and sets the `content-type` header to `application/json;charset=UTF-8`, which tells the client the payload is JSON. `event.respondWith()` returns the response, with no request to an origin.

The same shape covers richer responses. Set a status code or add CORS and cache-control headers in the second argument to `Response`, and build the object from request data, such as query string parameters, cookies, or headers, instead of hardcoding it.
The same shape covers richer responses. Set a status code or add CORS and cache-control headers in the second argument to `Response`. Build the object from request data, such as query string parameters, cookies, or headers, instead of hardcoding it.

The object can also be assembled from data the function fetches. A handler that calls `fetch()` for each of several URLs, parses each response with `response.json()`, and awaits them together with `Promise.all()` returns one JSON body built from all of them. For a worked example, refer to [Example: Async event handlers](/en/documentation/devtools/runtime/node/events/#async-event-handlers).
The object can also be assembled from data the function fetches. In that case, the handler calls `fetch()` for each of several URLs and parses each response with `response.json()`. It awaits them together with `Promise.all()` and returns one JSON body built from all of them. For a worked example, refer to [Example: Async event handlers](/en/documentation/devtools/runtime/node/events/#async-event-handlers).

## Related resources

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ The Args set on the function instance that runs this code:

The `fetch` handler reads `event.args.value` and passes it to `handleRequest`, which returns that value in the body of a new `Response`, along with a custom header set through `new Headers([...])` and an explicit `status: 200`. The `value` key comes from the JSON object above, so changing that object changes what the function returns.

Args are not part of the code. You set them on the [function instance](/en/documentation/platform/applications/functions-instances/) that binds the function to an application or a firewall, which is why the same function can behave differently on each instance without being edited or redeployed. [How it works](/en/documentation/platform/functions/how-it-works/) covers the rest of the chain, from the function to the instance to the Rules Engine rule that runs it.
Args are not part of the code. You set them on the [function instance](/en/documentation/platform/applications/functions-instances/) that binds the function to an application or a firewall. That is why the same function can behave differently on each instance without being edited or redeployed. [How it works](/en/documentation/platform/functions/how-it-works/) covers the rest of the chain, from the function to the instance to the Rules Engine rule that runs it.

## Related resources

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

The Azion CLI `config` commands manage Azion resources from manifest files: `init` creates an empty `azion.json` file, `apply` creates or updates the resources that an [azion.config](/en/documentation/devtools/cli/azion-config-js/) file declares, and `delete` deletes the resources that `azion.json` lists. The options every command accepts, such as `--format`, `--out`, and `-y`, are on [Global options](/en/documentation/devtools/cli/globals/).
The Azion CLI `config` commands manage Azion resources from manifest files. The `init` command creates an empty `azion.json` file. The `apply` command creates or updates the resources that an [azion.config](/en/documentation/devtools/cli/azion-config-js/) file declares. The `delete` command deletes the resources that `azion.json` lists. The options every command accepts, such as `--format`, `--out`, and `-y`, are on [Global options](/en/documentation/devtools/cli/globals/).

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

The Azion CLI `dev` command builds the project in the current directory and starts a local development server, so you can test an application or a function on your machine before you deploy it. Run it from the root folder of a project that [Azion CLI init](/en/documentation/devtools/cli/init/) created or that [Azion CLI link](/en/documentation/devtools/cli/link-command/) set up. For an existing project that is not set up for Azion, run `azion link` first. The options every command accepts, such as `--debug`, `--format`, and `-y`, are on [Global options](/en/documentation/devtools/cli/globals/).
The Azion CLI `dev` command builds the project in the current directory and starts a local development server. With that server, you can test an application or a function on your machine before you deploy it. Run it from the root folder of a project that [Azion CLI init](/en/documentation/devtools/cli/init/) created or that [Azion CLI link](/en/documentation/devtools/cli/link-command/) set up. For an existing project that is not set up for Azion, run `azion link` first. The options every command accepts, such as `--debug`, `--format`, and `-y`, are on [Global options](/en/documentation/devtools/cli/globals/).

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import FrameBox from '@aziontech/webkit/frame-box'
import ItemList from '@aziontech/webkit/item-list'
import DocItem from '@aziontech/webkit/doc-item'

Global options are the flags that the [Azion CLI](/en/documentation/devtools/cli/) commands share. They go after the command and its subcommand, as in `azion list application --debug`, and set the configuration folder, the token, the log level, the request timeout, the output format, and the answers to yes/no questions.
Global options are the flags that the [Azion CLI](/en/documentation/devtools/cli/) commands share. They go after the command and its subcommand, as in `azion list application --debug`. They set the configuration folder, the token, the log level, the request timeout, the output format, and the answers to yes/no questions.

---

Expand Down
Loading