Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add high-resolution timing function "fastly.now()" behind featu…
…re flag "--enable-experimental-high-resolution-time-methods" This function accepts no arguments and returns the number of Microseconds since the epoch, midnight, January 1, 1970 UTC. This function is very useful to have for running performance tests within JavaScript applications.
- Loading branch information
1 parent
b1c4848
commit f090838
Showing
13 changed files
with
174 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* eslint-env serviceworker */ | ||
|
||
import { env } from 'fastly:env'; | ||
import { pass, fail, assert } from "../../../assertions.js"; | ||
|
||
addEventListener("fetch", event => { | ||
event.respondWith(app(event)) | ||
}) | ||
/** | ||
* @param {FetchEvent} event | ||
* @returns {Response} | ||
*/ | ||
async function app(event) { | ||
try { | ||
const path = (new URL(event.request.url)).pathname; | ||
console.log(`path: ${path}`) | ||
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`) | ||
if (routes.has(path)) { | ||
const routeHandler = routes.get(path); | ||
return await routeHandler() | ||
} | ||
return fail(`${path} endpoint does not exist`) | ||
} catch (error) { | ||
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack) | ||
} | ||
} | ||
|
||
const routes = new Map(); | ||
routes.set('/', () => { | ||
routes.delete('/'); | ||
let test_routes = Array.from(routes.keys()) | ||
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } }); | ||
}); | ||
// fastly.now | ||
{ | ||
routes.set("/fastly/now", function () { | ||
let error = assert(typeof fastly.now, 'function', 'typeof fastly.now') | ||
if (error) { return error } | ||
|
||
error = assert(fastly.now.name, 'now', 'fastly.now.name') | ||
if (error) { return error } | ||
|
||
error = assert(fastly.now.length, 0, 'fastly.now.length') | ||
if (error) { return error } | ||
|
||
error = assert(typeof fastly.now(), 'number', `typeof fastly.now()`) | ||
if (error) { return error } | ||
|
||
error = assert(fastly.now() > Date.now(), true, `fastly.now() > Date.now()`) | ||
if (error) { return error } | ||
|
||
console.log(fastly.now()) | ||
|
||
return pass() | ||
}) | ||
} |
12 changes: 12 additions & 0 deletions
12
integration-tests/js-compute/fixtures/fastly/fastly.toml.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file describes a Fastly Compute@Edge package. To learn more visit: | ||
# https://developer.fastly.com/reference/fastly-toml/ | ||
|
||
authors = ["me@jakechampion.name"] | ||
description = "" | ||
language = "other" | ||
manifest_version = 2 | ||
name = "fastly" | ||
service_id = "" | ||
|
||
[scripts] | ||
build = "node ../../../../js-compute-runtime-cli.js --enable-experimental-high-resolution-time-methods" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"GET /fastly/now": { | ||
"environments": ["c@e", "viceroy"], | ||
"downstream_request": { | ||
"method": "GET", | ||
"pathname": "/fastly/now" | ||
}, | ||
"downstream_response": { | ||
"status": 200 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters