Introducing Edgio Applications v7Find out what's new.
Edgio
Edgio

Content Manipulation and Stitching

Content manipulation and stitching is a powerful feature that allows you to modify the content of a response before it is returned to the client. This can be useful for adding custom content to a response, modifying the response headers, or stitching together multiple responses into a single response.

Basic Content Manipulation

The response body from the origin can be accessed and modified using the Response object in the Fetch API. This object provides methods for accessing and modifying the response body, including text(), json(), and arrayBuffer(). These methods can be used to add, remove, or modify the content of the response before it is returned to the client.

Router Configuration

JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';
2
3export default new Router()
4 .use(edgioRoutes)
5
6 .match('/some/path', {
7 edge_function: './edge-functions/main.js',
8 })

Edge Function

JavaScriptedge-functions/main.js
1export async function handleHttpRequest(request, context) {
2 const resp = await fetch(request.url, {
3 edgio: {
4 origin: 'web',
5 },
6 });
7 let html = await resp.text();
8
9 // Inject a marquee tag into the response
10 const marquee =
11 '<marquee>This paragraph was injected by an edge function.</marquee>';
12 html = html.replace(/(<center[^>]*>)/i, `$1${marquee}`);
13
14 return new Response(html, resp);
15}

M3U Manifest Manipulation

The M3U manifest is a text-based file format that contains information about the media files in a playlist. It is commonly used for streaming video and audio, and is supported by most media players and streaming services. The M3U manifest can be modified to remove or add media files, change the order of the files, or modify the metadata of the files.

Router Configuration

JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';
2
3export default new Router()
4 .use(edgioRoutes)
5
6 .match('/some/path', {
7 edge_function: './edge-functions/main.js',
8 });

Edge Function

JavaScriptedge-functions/main.js
1import {URL} from 'whatwg-url';
2
3export async function handleHttpRequest(request, context) {
4 // Get the manifest from the upstream server
5 const url = new URL(request.url);
6 url.pathname = '/assets/tears-of-steel.m3u';
7
8 const upstreamResponse = await fetch(url.toString(), {
9 edgio: {
10 origin: 'web',
11 },
12 });
13
14 // Modify the manifest to remove the 1680x750 rendition
15 const manifestBody = await upstreamResponse.text();
16 const lines = manifestBody
17 .split('\n')
18 .filter((line, index, lines) => !lines[index - 1]?.includes('1680x750'))
19 .filter((line) => !line.includes('1680x750'));
20
21 // Return the modified manifest
22 const modifiedResponse = new Response(lines.join('\n'), {
23 headers: {'content-type': upstreamResponse.headers.get('content-type')},
24 });
25
26 return modifiedResponse;
27}

Content Stitching

Content stitching is a powerful feature that allows you to combine multiple responses into a single response, such as combining the results of multiple API calls into a single response.

Router Configuration

JavaScriptroutes.js
1import {Router, edgioRoutes} from '@edgio/core';
2
3export default new Router()
4 .use(edgioRoutes)
5
6 .match('/some/path', {
7 edge_function: './edge-functions/main.js',
8 });

Edge Function

JavaScriptedge-functions/main.js
1import {URL} from 'whatwg-url';
2
3export async function handleHttpRequest(request, context) {
4 const url = new URL(request.url);
5 const originConfig = {
6 edgio: {
7 origin: 'web',
8 },
9 };
10
11 // Get the list of phone contacts
12 const phonePromise = fetch(new Request(`${url.origin}/phone`), originConfig);
13
14 // In Parallel, get the list of e-mail contacts
15 const emailPromise = fetch(new Request(`${url.origin}/email`), originConfig);
16
17 // Wait for both requests to complete.
18 const [phoneResponse, emailResponse] = await Promise.all([
19 phonePromise,
20 emailPromise,
21 ]);
22
23 // Combine the two response bodies into a single response
24 const body = {
25 phone: await phoneResponse.json(),
26 email: await emailResponse.json(),
27 };
28
29 // Return the response and end the edge function as JSON
30 const jsonBody = JSON.stringify(body);
31
32 return new Response(jsonBody, 200, {
33 headers: {'Content-Type': 'application/json'},
34 });
35}