Edge Runtime Code analysis utilities
The @edge-runtime/feature-detector package contains utilities to analyze code running on the edge and its used low-level APIs.
It leverages the excellent ts-morph (opens in a new tab) package, which is a wrapper around TypeScript compiler API to navigate its Abstract Syntax Tree (AST).
It can analyse JavaScript and TypeScript code.
⚠️
Please note this is an alpha version.
Installation
sh npm install @edge-runtime/feature-detector --save
This package includes built-in TypeScript support.
Usage
Here is a test file. It's in JavaScript, but TypeScript is supported as well.
// test.js
if (typeof process !== 'undefined') {
console.log('in node', __dirname)
} else if (typeof window !== 'undefined') {
console.log('in a browser', document)
}
setTimeout(() => console.log('setTimeout is not a global, nor console'), 0)
export default function () {
return Response.json({ message: 'hello world!' })
}
In the code snippet bellow, we're checking:
- whether this file is a function matching the edge signature
- globals used, that are not provided by the Edge Runtime
import { hasEdgeSignature, findGlobals } from '@edge-runtime/feature-detector'
const sourceFilePath = './test.js' // could be TypeScript as well. Must be in current working directory
// 1
if (hasEdgeSignature(sourceFilePath)) {
console.log(`${sourcefilePath} can run on the edge`)
}
// 2
console.log(findGlobals(sourceFilePath)) // ['process', '__dirname', 'window', 'document']
API
⚠️
Work in progress