Skip to content

Commit b2edd7e

Browse files
committed
Merge branch 'main' into COMSUP-249_jwt_example
2 parents ead248b + ed73613 commit b2edd7e

File tree

87 files changed

+63125
-8885
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+63125
-8885
lines changed

.github/workflows/deploy-examples-dev.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ concurrency:
1919

2020
env:
2121
# Node version for all the jobs
22-
NODE_VERSION: 16
22+
NODE_VERSION: 18
2323

2424
# Set env var based on branch name
2525
GIT_REF: ${{ github.ref }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
node_modules/
33
**/node_modules/
44
**/.edgio/**
5+
**/.yalc/**
6+
yalc.lock

examples/v7-astro/package-lock.json

+3,118-2,585
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/v7-astro/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"astro": "^3.2.3"
1717
},
1818
"devDependencies": {
19-
"@edgio/astro": "^7.2.7",
20-
"@edgio/cli": "^7.2.7",
21-
"@edgio/core": "^7.2.7",
22-
"@edgio/devtools": "^7.2.7",
23-
"@edgio/prefetch": "^7.2.7"
19+
"@edgio/astro": "^7.2.8",
20+
"@edgio/cli": "^7.2.8",
21+
"@edgio/core": "^7.2.8",
22+
"@edgio/devtools": "^7.2.8",
23+
"@edgio/prefetch": "^7.2.8"
2424
},
2525
"repository": "[email protected]:edgio-docs/edgio-v7-astro-example.git"
2626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

examples/v7-edge-functions/.gitignore

+40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,45 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
.yarn/install-state.gz
8+
9+
# testing
10+
/coverage
11+
12+
# next.js
13+
/.next/
14+
/out/
15+
16+
# production
17+
/build
18+
19+
# misc
20+
.DS_Store
21+
*.pem
22+
23+
# debug
24+
npm-debug.log*
25+
yarn-debug.log*
26+
yarn-error.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts
37+
138
# Temporary Edgio files
239
.edgio
340

441
# Node.js modules
542
node_modules
43+
44+
# Edgio generated build directory
45+
.edgio

examples/v7-edge-functions/edgio.config.js

+63-26
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,65 @@
44
require('dotenv').config();
55

66
module.exports = {
7+
connector: '@edgio/next',
8+
79
// The name of the site in Edgio to which this app should be deployed.
8-
name: 'edgio-functions-examples',
10+
// name: 'my-site-name',
911

10-
// The name of the team in Edgio to which this app should be deployed.
11-
team: 'edge-functions-sandbox',
12+
// The name of the organization in Edgio to which this app should be deployed.
13+
// organization: 'my-organization-name',
1214

1315
// Overrides the default path to the routes file. The path should be relative to the root of your app.
1416
// routes: 'routes.js',
1517

18+
// When set to true or omitted entirely, Edgio includes the deployment number in the cache key,
19+
// effectively purging the cache each time you deploy.
20+
// purgeCacheOnDeploy: false,
21+
22+
next: {
23+
// Output sourcemaps so that stack traces have original source filenames and line numbers when tailing
24+
// the logs in the Edgio developer console.
25+
// This config options replaces the edgioSourceMaps option in next.config.js.
26+
// @default true
27+
// generateSourceMaps: true
28+
//
29+
// Disables the Edgio image optimizer and allows to use the Next's built in image optimizer.
30+
// This config options replaces the disableImageOptimizer option in edgio.config.js root.
31+
// @default false
32+
// disableImageOptimizer: false
33+
//
34+
// Disables the Edgio development tools widget on the site.
35+
// This config options replaces the disableEdgioDevTools option in next.config.js.
36+
// @default false
37+
// disableDevtools: false
38+
//
39+
// Disables the build of the service worker.
40+
// @default false
41+
// disableServiceWorker: false
42+
//
43+
// Forces the @edgio/next connector to use the server build.
44+
// This config option replaces the NEXT_FORCE_SERVER_BUILD env variable.
45+
// @default false
46+
// forceServerBuild: false
47+
//
48+
// Optimizes the server build by bundling all server assets and decreasing the overall startup time.
49+
// This option has no effect on apps with serverless build.
50+
// This option is set to false for Next 13.x apps.
51+
// @default true
52+
// optimizeServerBuild: true
53+
//
54+
// Set this option to false to remove the default rule that proxies all requests to Next.js in serverless.
55+
// This is useful if you want to proxy all unmatched pages to different origin.
56+
// @default true
57+
// proxyToServerlessByDefault: true
58+
//
59+
// Set this option to true to honor Next's internal redirects that either add or remove a trailing slash
60+
// depending on the value of the `trailingSlash` config. When set to false, these internal redirects are not honored,
61+
// so sites that fallback to serving from an origin do not add or remove the trailing slash for origin URLs.
62+
// @default true
63+
// enforceTrailingSlash: true
64+
},
65+
1666
origins: [
1767
{
1868
// The name of the backend origin
@@ -30,6 +80,15 @@ module.exports = {
3080
},
3181
],
3282
},
83+
{
84+
name: 'echo',
85+
override_host_header: 'http-echo.raees.me',
86+
hosts: [
87+
{
88+
location: 'http-echo.raees.me',
89+
},
90+
],
91+
},
3392
{
3493
name: 'planetscale',
3594
override_host_header: 'aws.connect.psdb.cloud',
@@ -63,28 +122,6 @@ module.exports = {
63122
},
64123
],
65124

66-
// Uncomment the following to specify environment specific configs
67-
// environments: {
68-
// production: {
69-
// hostnames: [{ hostname: 'www.mysite.com' }],
70-
// },
71-
// staging: {
72-
// hostnames: [{ hostname: 'staging.mysite.com' }],
73-
// origins: [
74-
// {
75-
// name: 'origin',
76-
// hosts: [{ location: 'staging-origin.mysite.com' }],
77-
// override_host_header: 'staging-origin.mysite.com',
78-
// tls_verify: {
79-
// use_sni: true,
80-
// sni_hint_and_strict_san_check: 'staging-origin.mysite.com',
81-
// },
82-
// shields: { us_east: 'DCD' },
83-
// },
84-
// ],
85-
// },
86-
// },
87-
88125
// Options for hosting serverless functions on Edgio
89126
// serverless: {
90127
// // Set to true to include all packages listed in the dependencies property of package.json when deploying to Edgio.
@@ -100,7 +137,7 @@ module.exports = {
100137
// Defaults to 200, which is the maximum allowed value.
101138
// prerenderConcurrency: 200,
102139

103-
// A list of glob patterns identifying which source files should be uploaded when running edgio deploy --includeSources.
140+
// A list of glob patterns identifying which prerenderConcurrency source files should be uploaded when running edgio deploy --includeSources.
104141
// This option is primarily used to share source code with Edgio support personnel for the purpose of debugging. If omitted,
105142
// edgio deploy --includeSources will result in all files which are not gitignored being uploaded to Edgio.
106143
//

examples/v7-edge-functions/functions/database/upstash/index.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
setCookieToResponse,
55
} from '../../../utils/cookies';
66
import { setEnvFromContext } from '../../../utils/polyfills/process.env';
7+
import '../../../utils/polyfills/URL';
78
import waitingPage from './waiting-room-capacity.html';
89
import landingPage from './waiting-room-landing.html';
910

@@ -59,7 +60,13 @@ function generateId(len = 10) {
5960
* Handle the default response.
6061
*/
6162
async function getDefaultResponse(request, userId) {
62-
const response = new Response(landingPage);
63+
const response = new Response(
64+
landingPage({
65+
domain: getDomainFromRequest(request),
66+
maxUsers: TOTAL_ACTIVE_USERS,
67+
sessionDuration: SESSION_DURATION_SECONDS,
68+
})
69+
);
6370
response.headers.set('content-type', 'text/html;charset=UTF-8');
6471

6572
const cookies = getCookiesFromRequest(request);
@@ -126,7 +133,12 @@ async function setExpiryRecord(key, value, seconds) {
126133
* Response for the waiting room.
127134
*/
128135
async function getWaitingRoomResponse() {
129-
const response = new Response(waitingPage);
136+
const response = new Response(waitingPage());
130137
response.headers.set('content-type', 'text/html;charset=UTF-8');
131138
return response;
132139
}
140+
141+
function getDomainFromRequest(request) {
142+
const url = new URL(request.url);
143+
return url.origin;
144+
}

examples/v7-edge-functions/functions/database/upstash/waiting-room-capacity.html.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const template = `
1+
const template = () => `
22
<!DOCTYPE html>
33
<html lang="en">
44

examples/v7-edge-functions/functions/database/upstash/waiting-room-landing.html.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const template = `
1+
const template = ({ domain, maxUsers, sessionDuration }) => `
22
<!DOCTYPE html>
33
<html lang="en">
44
@@ -103,11 +103,18 @@ const template = `
103103
<div class='container'>
104104
<h1>Welcome to the Waiting Room Demo</h1>
105105
<p>
106-
This demo showcases an effective way to manage website traffic during high-volume periods. When the site is at full capacity, visitors are temporarily placed in a waiting room, ensuring a smooth user experience.
106+
This demo showcases an effective way to manage website traffic during high-volume periods.
107+
When the site is at full capacity, visitors are temporarily placed in a waiting room, ensuring a smooth user experience.
108+
</p>
109+
<p>
110+
This is configured for a maximum of ${maxUsers} active sessions, with each session lasting ${sessionDuration} seconds.
111+
</p>
112+
<p>
113+
Experience this firsthand by opening <a href="${domain}/example/upstash-database">this link</a> in multiple incognito/private browser sessions.
114+
Once the site is at full capacity (2 active sessions), you will be placed in a waiting room until a spot opens up.
107115
</p>
108-
<p>Experience this firsthand by opening <a href="/example/upstash-database">this link</a> in multiple browser sessions. Once the site is at full capacity (2 active sessions), you will be placed in a waiting room until a spot opens up.</p>
109116
<p>Optionally, issue the <pre>curl</pre> command below to make multiple requests:</p>
110-
<div class="code-block">curl https://edgio-community-examples-v7-edge-functions-live.edgio.link/example/upstash-database</div>
117+
<div class="code-block">curl ${domain}/example/upstash-database</div>
111118
<button class="copy-btn" onclick="copyCodeToClipboard()">Copy Code</button>
112119
<p>Dive into the code to see how it works.</p>
113120
<p><a href="https://github.com/edgio-docs/edgio-v7-edge-functions-example" target="_blank">View the demo code on GitHub</a></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import createFetchForOrigin from '../../utils/createFetchForOrigin';
2+
import '../../utils/polyfills/URL';
3+
4+
const fetch = createFetchForOrigin('echo');
5+
6+
export async function handleHttpRequest(request, context) {
7+
const { method } = request;
8+
const body = method === 'POST' ? await request.arrayBuffer() : null;
9+
10+
// get the headers from the incoming request, removing the content-length header
11+
const headers = Object.fromEntries(
12+
[...request.headers.entries()].filter(([key]) => key !== 'content-length')
13+
);
14+
15+
const newRequest = new Request('https://http-echo.raees.me', {
16+
method,
17+
headers,
18+
body,
19+
});
20+
21+
const response = await fetch(newRequest);
22+
23+
// apply caching headers to the response for all HTTP methods
24+
response.headers.set('cache-control', 's-maxage=600');
25+
26+
return response;
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import '../../utils/polyfills/URL';
2+
3+
export async function handleHttpRequest(request, context) {
4+
// Retrieve the client's IP address from the context object
5+
const clientIP = context.client.dst_addr;
6+
7+
const newRequest = new Request(request.url, request);
8+
9+
// Add the true-client-ip header to the incoming request
10+
newRequest.headers.set('true-client-ip', clientIP);
11+
12+
// Continue with the modified request
13+
return fetch(newRequest, {
14+
edgio: { origin: 'echo' },
15+
});
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export async function handleHttpRequest(request, context) {
2+
const country = 'DE'; // Choose a country code
3+
const newUrl = `${request.url}/${country}`; // Change the redirect URL to your choice
4+
5+
if (context.geo.country === country) {
6+
return new Response(null, {
7+
status: 302,
8+
statusText: 'Found',
9+
headers: {
10+
Location: newUrl,
11+
},
12+
});
13+
}
14+
15+
return fetch(request.url, {
16+
edgio: { origin: 'echo' },
17+
});
18+
}

examples/v7-edge-functions/functions/general/sample-html-page.js

+33
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ export async function handleHttpRequest(request, context) {
8383
overflow-wrap: break-word;
8484
white-space: pre-wrap;
8585
}
86+
87+
p pre {
88+
display: inline;
89+
}
8690
</style>
8791
</head>
8892
<body>
@@ -165,6 +169,35 @@ export async function handleHttpRequest(request, context) {
165169
</li>
166170
</ul>
167171
</section>
172+
173+
<section>
174+
<h2>Caching</h2>
175+
<p>Examples demonstrating caching for different request types. Observe unique caching for GET and POST w/ body requests.</p>
176+
<ul>
177+
<li>
178+
<strong>GET Request</strong>
179+
<pre><code>${createCURLCommand(
180+
'/example/caching'
181+
)}</code></pre>
182+
</li>
183+
<li>
184+
<strong>POST Request with JSON payload 1</strong>
185+
<pre><code>curl -i -X POST ${domain}/example/caching -d '{"key": "value1"}'</code></pre>
186+
</li>
187+
<li>
188+
<strong>POST Request with JSON payload 2</strong>
189+
<pre><code>curl -i -X POST ${domain}/example/caching -d '{"key": "value2"}'</code></pre>
190+
</li>
191+
</ul>
192+
</section>
193+
194+
<section>
195+
<h2>Request Signing</h2>
196+
<p>Request signing and verification using <strong>crypto-js</strong>. Generated URLs are valid for 60 seconds.</p>
197+
<p><a href="/example/signed-request/sign/foo/bar">Generate Signed URL</a></p>
198+
</section>
199+
200+
168201
</div>
169202
<div style="margin-top: 30px; text-align: center;">
170203
<a href="https://docs.edg.io/guides/v7/edge-functions" target="_blank">Edge Functions Documentation</a> | <a href="https://github.com/edgio-docs/edgio-v7-edge-functions-example" target="_blank">View the demo code on GitHub</a>

0 commit comments

Comments
 (0)