Skip to content

Commit 3e83429

Browse files
authored
feat: add polyfills for stackblitz (#8130)
* feat: add polyfills for Stackblitz * chore: changeset
1 parent 43140b8 commit 3e83429

7 files changed

Lines changed: 86 additions & 6 deletions

File tree

.changeset/thin-ants-repeat.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@astrojs/telemetry': patch
3+
'astro': patch
4+
---
5+
6+
Add some polyfills for Stackblitz until they support Node 18. Running Astro on Node 16 is still not officially supported, however.

packages/astro/astro.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ const CI_INSTRUCTIONS = {
1616
const engines = '>=18.14.1';
1717
const skipSemverCheckIfAbove = 19;
1818

19+
// HACK (2023-08-18) Stackblitz does not support Node 18 yet, so we'll fake Node 16 support for some time until it's supported
20+
// TODO: Remove when Node 18 is supported on Stackblitz
21+
const isStackblitz = process.env.SHELL === '/bin/jsh' && process.versions.webcontainer != null;
22+
1923
/** `astro *` */
2024
async function main() {
2125
const version = process.versions.node;
2226
// Fast-path for higher Node.js versions
23-
if ((parseInt(version) || 0) <= skipSemverCheckIfAbove) {
27+
if (!isStackblitz && (parseInt(version) || 0) <= skipSemverCheckIfAbove) {
2428
try {
2529
const semver = await import('semver');
2630
if (!semver.satisfies(version, engines)) {

packages/astro/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@
147147
"fast-glob": "^3.2.12",
148148
"github-slugger": "^2.0.0",
149149
"gray-matter": "^4.0.3",
150-
"http-cache-semantics": "^4.1.1",
151150
"html-escaper": "^3.0.3",
151+
"http-cache-semantics": "^4.1.1",
152152
"js-yaml": "^4.1.0",
153153
"kleur": "^4.1.4",
154154
"magic-string": "^0.30.2",
@@ -166,6 +166,7 @@
166166
"string-width": "^5.1.2",
167167
"strip-ansi": "^7.1.0",
168168
"tsconfig-resolver": "^3.0.1",
169+
"undici": "^5.23.0",
169170
"unist-util-visit": "^4.1.2",
170171
"vfile": "^5.3.7",
171172
"vite": "^4.4.6",

packages/astro/src/core/polyfill.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,67 @@
11
import { File } from 'node:buffer';
22
import crypto from 'node:crypto';
3+
import {
4+
ByteLengthQueuingStrategy,
5+
CountQueuingStrategy,
6+
ReadableByteStreamController,
7+
ReadableStream,
8+
ReadableStreamBYOBReader,
9+
ReadableStreamBYOBRequest,
10+
ReadableStreamDefaultController,
11+
ReadableStreamDefaultReader,
12+
TransformStream,
13+
WritableStream,
14+
WritableStreamDefaultController,
15+
WritableStreamDefaultWriter,
16+
} from 'node:stream/web';
17+
import { FormData, Headers, Request, Response, fetch, File as undiciFile } from 'undici';
318

419
// NOTE: This file does not intend to polyfill everything that exists, its main goal is to make life easier
520
// for users deploying to runtime that do support these features. In the future, we hope for this file to disappear.
621

22+
// HACK (2023-08-18) Stackblitz does not support Node 18 yet, so we'll fake Node 16 support for some time until it's supported
23+
// TODO: Remove when Node 18 is supported on Stackblitz
24+
const isStackblitz = process.env.SHELL === '/bin/jsh' && process.versions.webcontainer != null;
25+
726
export function apply() {
27+
if (isStackblitz) {
28+
const neededPolyfills = [
29+
ByteLengthQueuingStrategy,
30+
CountQueuingStrategy,
31+
ReadableByteStreamController,
32+
ReadableStream,
33+
ReadableStreamBYOBReader,
34+
ReadableStreamBYOBRequest,
35+
ReadableStreamDefaultController,
36+
ReadableStreamDefaultReader,
37+
TransformStream,
38+
WritableStream,
39+
WritableStreamDefaultController,
40+
WritableStreamDefaultWriter,
41+
undiciFile,
42+
FormData,
43+
Headers,
44+
Request,
45+
Response,
46+
fetch,
47+
];
48+
49+
for (let polyfillName of Object.keys(neededPolyfills)) {
50+
if (Object.hasOwnProperty.call(globalThis, polyfillName)) continue;
51+
52+
// Add polyfill to globalThis
53+
Object.defineProperty(globalThis, polyfillName, {
54+
configurable: true,
55+
enumerable: true,
56+
writable: true,
57+
value:
58+
neededPolyfills[
59+
(polyfillName === 'undiciFile' ? 'File' : polyfillName) as keyof typeof neededPolyfills
60+
],
61+
});
62+
}
63+
}
64+
865
// Remove when Node 18 is dropped for Node 20
966
if (!globalThis.crypto) {
1067
Object.defineProperty(globalThis, 'crypto', {

packages/telemetry/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"dset": "^3.1.2",
3636
"is-docker": "^3.0.0",
3737
"is-wsl": "^2.2.0",
38-
"which-pm-runs": "^1.1.0"
38+
"which-pm-runs": "^1.1.0",
39+
"undici": "^5.23.0"
3940
},
4041
"devDependencies": {
4142
"@types/debug": "^4.1.8",

packages/telemetry/src/post.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const ASTRO_TELEMETRY_ENDPOINT = `https://telemetry.astro.build/api/v1/record`;
2+
import { fetch } from 'undici';
23

34
export function post(body: Record<string, any>): Promise<any> {
45
return fetch(ASTRO_TELEMETRY_ENDPOINT, {

pnpm-lock.yaml

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)