Skip to content

fix: test failures due to esm.sh rate limits #5511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docker-compose-wsl2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ version: '3'
services:
# On Windows, run with COMPOSE_CONVERT_WINDOWS_PATHS=1

esm:
image: ghcr.io/esm-dev/esm.sh:latest
ports:
- "8080:8080"
environment:
- NPM_REGISTRY=https://registry.npmjs.org/
volumes:
- esm_data:/esmd

chrome:
image: selenium/node-chrome:110.0
depends_on:
Expand Down Expand Up @@ -56,3 +65,6 @@ services:
ports:
- '4445:4445'
stop_grace_period: 5s

volumes:
esm_data:
175 changes: 175 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/test/web-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"serve-handler": "6.1.6"
},
"devDependencies": {
"@ast-grep/napi": "^0.38.6",
"tsup": "^8.3.6"
}
}
70 changes: 70 additions & 0 deletions packages/test/web-server/src/handleEsm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable no-magic-numbers */
import { parse, Lang, type Edit } from '@ast-grep/napi';

// Rule for rewriting ESM imports/exports
// Use playground to test and modify the rule:
// https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6ImltcG9ydCAkWCBmcm9tICRTUkMiLCJyZXdyaXRlIjoiaW1wb3J0ICRYIGZyb20gJFNSQyIsInN0cmljdG5lc3MiOiJzbWFydCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoiIyBZQU1MIFJ1bGUgaXMgbW9yZSBwb3dlcmZ1bCFcbiMgaHR0cHM6Ly9hc3QtZ3JlcC5naXRodWIuaW8vZ3VpZGUvcnVsZS1jb25maWcuaHRtbCNydWxlXG5ydWxlOlxuICBraW5kOiBzdHJpbmdfZnJhZ21lbnRcbiAgcmVnZXg6IF5cXC9cbiAgcGF0dGVybjogXG4gICAgY29udGV4dDogJEFcbiAgaW5zaWRlOlxuICAgIGtpbmQ6IHN0cmluZ1xuICAgIG50aENoaWxkOlxuICAgICAgcG9zaXRpb246IDFcbiAgICAgIHJldmVyc2U6IHRydWVcbiAgICBpbnNpZGU6XG4gICAgICBhbnk6XG4gICAgICAgIC0ga2luZDogZXhwb3J0X3N0YXRlbWVudFxuICAgICAgICAtIGtpbmQ6IGltcG9ydF9zdGF0ZW1lbnRcbiAgICAgICAgLSBraW5kOiBhcmd1bWVudHNcbiAgICAgICAgICBpbnNpZGU6XG4gICAgICAgICAgICBraW5kOiBjYWxsX2V4cHJlc3Npb25cbiAgICAgICAgICAgIHBhdHRlcm46IGltcG9ydCgkJCQpXG5maXg6XG4gIC9lc20kQVxuIiwic291cmNlIjoiLyogZXNtLnNoIC0gcmVhY3RAMTkuMS4wICovXG5leHBvcnQgKiBmcm9tIFwiL3JlYWN0QDE5LjEuMC9lczIwMjIvcmVhY3QubWpzXCI7XG5leHBvcnQgeyBkZWZhdWx0IH0gZnJvbSBcIi9yZWFjdEAxOS4xLjAvZXMyMDIyL3JlYWN0Lm1qc1wiO1xuaW1wb3J0ICogYXMgZm9vIGZyb20gXCIvcmVhY3RAMTkuMS4wL2VzMjAyMi9yZWFjdC5tanNcIjtcbmltcG9ydCAnL3Rlc3QnXG5cbmltcG9ydCgnL2Zvby8nKVxudGVzdCgnL2Zvby8nKVxuIn0=
const ruleConfig = {
rule: {
kind: 'string_fragment',
regex: '^\\/',
pattern: {
context: '$A'
},
inside: {
kind: 'string',
nthChild: {
position: 1,
reverse: true
},
inside: {
any: [
{
kind: 'export_statement'
},
{
kind: 'import_statement'
},
{
kind: 'arguments',
inside: {
kind: 'call_expression',
pattern: 'import($$$)'
}
}
]
}
}
},
fix: '/esm$A'
};

export const handleEsm = async (req, res) => {
try {
const targetPath = req.url.replace(/^\/esm/u, '');
const upstreamUrl = `http://esm:8080${targetPath}`;

const upstreamRes = await fetch(upstreamUrl);
if (!upstreamRes.ok) {
res.writeHead(upstreamRes.status);
return res.end(await upstreamRes.text());
}
const sourceJS = await upstreamRes.text();

const root = parse(Lang.JavaScript, sourceJS).root();
const edits: Edit[] = [];

for (const match of root.findAll(ruleConfig)) {
const text = match.getMatch('A')?.text();
text && edits.push(match.replace(ruleConfig.fix.replace('$A', text)));
}

const outJS = root.commitEdits(edits);
res.setHeader('Content-Type', 'application/javascript');
return res.end(outJS);
} catch (err) {
console.error('ESM proxy error:', err);
res.writeHead(500);
return res.end(err.stack);
}
};
10 changes: 9 additions & 1 deletion packages/test/web-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { createServer as createSecureServer } from 'node:https';
import selfsigned from 'selfsigned';
import handleServe from 'serve-handler';

import { handleEsm } from './handleEsm';

const {
// eslint-disable-next-line no-magic-numbers
env: { PORT = 5081, PORTS = 5443 }
Expand All @@ -15,7 +17,13 @@ const {
const attrs = [{ name: 'commonName', value: 'webchat2' }];
const pems = selfsigned.generate(attrs, { days: 365 });

const handler = (req, res) => handleServe(req, res, config);
const handler = (req, res) => {
if (req.url.startsWith('/esm/')) {
return handleEsm(req, res);
}

return handleServe(req, res, config);
};

createSecureServer(
{
Expand Down
3 changes: 2 additions & 1 deletion packages/test/web-server/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export default defineConfig({
},
format: 'cjs',
platform: 'node',
noExternal: ['selfsigned', 'serve-handler']
noExternal: ['selfsigned', 'serve-handler'],
external: ['@ast-grep/napi']
});
2 changes: 2 additions & 0 deletions testharness2.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ ENTRYPOINT ["node", "./index.js"]
ADD serve-test.json /var/web/serve.json
ADD packages/test/web-server/dist/index.js /var/web/
RUN echo {}>/var/web/package.json
WORKDIR /var/web/
RUN npm i @ast-grep/[email protected]
Loading