-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ponyfill only export the explicitly typed values (#124)
- Loading branch information
Showing
8 changed files
with
837 additions
and
670 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@edge-runtime/ponyfill': patch | ||
--- | ||
|
||
Only export the specific values from the global scope as we define in our types |
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,5 @@ | ||
--- | ||
'@edge-runtime/primitives': patch | ||
--- | ||
|
||
Mark the createCaches export as nullable as it does not exist in Edge Runtime |
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 |
---|---|---|
@@ -1,2 +1,42 @@ | ||
module.exports = | ||
typeof EdgeRuntime === 'string' ? self : require('@edge-runtime/primitives') | ||
typeof EdgeRuntime === 'string' ? edge() : require('@edge-runtime/primitives') | ||
|
||
function edge() { | ||
return { | ||
AbortController, | ||
AbortSignal, | ||
Blob, | ||
Cache, | ||
CacheStorage, | ||
Crypto, | ||
CryptoKey, | ||
Event, | ||
EventTarget, | ||
FetchEvent, | ||
File, | ||
FormData, | ||
Headers, | ||
PromiseRejectionEvent, | ||
ReadableStream, | ||
ReadableStreamBYOBReader, | ||
ReadableStreamDefaultReader, | ||
Request, | ||
Response, | ||
SubtleCrypto, | ||
TextDecoder, | ||
TextEncoder, | ||
TransformStream, | ||
URL, | ||
URLPattern, | ||
URLSearchParams, | ||
WritableStream, | ||
WritableStreamDefaultWriter, | ||
atob, | ||
btoa, | ||
caches, | ||
console, | ||
crypto, | ||
fetch, | ||
structuredClone, | ||
} | ||
} |
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,4 @@ | ||
declare module 'acorn-loose' { | ||
const exported: typeof import('acorn') | ||
export = exported | ||
} |
73 changes: 73 additions & 0 deletions
73
packages/ponyfill/test/compliance-with-primitives.node.test.ts
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,73 @@ | ||
import { parse } from 'acorn-loose' | ||
import { promises as fs } from 'fs' | ||
import { simple } from 'acorn-walk' | ||
import { EdgeVM } from '@edge-runtime/vm' | ||
|
||
test('exports all primitives in Edge Runtime', async () => { | ||
const exportedNames = await getExportedNames() | ||
|
||
const anyObject = exportedNames.reduce( | ||
(acc: Record<string, any>, name: string) => { | ||
acc[name] = expect.anything() | ||
return acc | ||
}, | ||
{} | ||
) | ||
|
||
const runtime = new EdgeVM({ | ||
codeGeneration: { strings: false, wasm: false }, | ||
}) | ||
const result = runtime.require(require.resolve('..')) | ||
|
||
for (const key of LIMBO_STATE) { | ||
delete anyObject[key] | ||
delete result[key] | ||
} | ||
|
||
expect(result).toEqual(anyObject) | ||
}) | ||
|
||
test('exports all primitives in Node.js', async () => { | ||
const exportedNames = await getExportedNames() | ||
|
||
const anyObject = exportedNames.reduce( | ||
(acc: Record<string, any>, name: string) => { | ||
acc[name] = expect.anything() | ||
return acc | ||
}, | ||
{} | ||
) | ||
|
||
const result = { ...require('..') } | ||
|
||
for (const key of LIMBO_STATE) { | ||
delete anyObject[key] | ||
delete result[key] | ||
} | ||
|
||
expect(result).toEqual(anyObject) | ||
}) | ||
|
||
async function getExportedNames() { | ||
const typesPath = require.resolve('@edge-runtime/primitives/types/index.d.ts') | ||
const typesContents = await fs.readFile(typesPath, 'utf8') | ||
const ast = parse(typesContents, { ecmaVersion: 'latest' }) | ||
const exportedNames: string[] = [] | ||
simple(ast, { | ||
ExportNamedDeclaration(node: any) { | ||
for (const specifier of node.specifiers) { | ||
if (specifier.exported?.name) { | ||
exportedNames.push(specifier.exported.name) | ||
} | ||
} | ||
}, | ||
}) | ||
return exportedNames | ||
} | ||
|
||
export const LIMBO_STATE = [ | ||
'createCaches', | ||
'DOMException', | ||
'setGlobalDispatcher', | ||
'getGlobalDispatcher', | ||
] |
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
Oops, something went wrong.
0cede1e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
edge-runtime – ./
edge-runtime.vercel.app
edge-runtime.vercel.sh
edge-runtime-git-main.vercel.sh