Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
141 changes: 138 additions & 3 deletions packages/build-tools/package-lock.json

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

10 changes: 7 additions & 3 deletions packages/build-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@spinframework/build-tools",
"version": "1.0.1",
"version": "1.0.2",
"description": "",
"main": "index.js",
"scripts": {
"build": "make && rm -rf dist && tsc && chmod +x dist/*",
"build": "make && rm -rf dist && tsc && chmod +x dist/* && cp src/*.js dist/",
"fmt": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\"",
"test": "npm run build && mocha --require ts-node/register test/**/*.spec.ts"
},
Expand All @@ -31,7 +31,11 @@
"dependencies": {
"@bytecodealliance/componentize-js": "^0.18.1",
"@bytecodealliance/jco": "^1.10.2",
"yargs": "^17.7.2"
"yargs": "^17.7.2",
"acorn-walk": "^8.3.4",
"acron": "^1.0.5",
"magic-string": "^0.30.17",
"regexpu-core": "^6.2.0"
},
"files": [
"lib",
Expand Down
17 changes: 14 additions & 3 deletions packages/build-tools/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import { componentize } from '@bytecodealliance/componentize-js';
import { version as componentizeVersion } from '@bytecodealliance/componentize-js';
import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js';

import {
calculateChecksum,
saveBuildData,
} from './utils.js';
import { getCliArgs } from './cli.js';
import { getBuildDataPath, ShouldComponentize } from './build.js';
import { writeFile } from 'node:fs/promises';
import { readFile, writeFile } from 'node:fs/promises';
import { mergeWit } from '../lib/wit_tools.js';
//@ts-ignore
import { precompile } from "./precompile.js"
import path from 'node:path'

async function main() {
try {
Expand Down Expand Up @@ -61,8 +63,17 @@ async function main() {
'combined-wit:[email protected]',
);

const source = await readFile(src, 'utf8');
const precompiledSource = precompile(source, src, true) as string;

// Write precompiled source to disk for debugging purposes In the future we
// will also write a source map to make debugging easier
let srcDir = path.dirname(src);
let precompiledSourcePath = path.join(srcDir, 'precompiled-source.js');
await writeFile(precompiledSourcePath, precompiledSource);

const { component } = await componentize({
sourcePath: src,
sourcePath: precompiledSourcePath,
// @ts-ignore
witWorld: inlineWit,
runtimeArgs,
Expand Down
62 changes: 62 additions & 0 deletions packages/build-tools/src/precompile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Fastly Inc.
// License: the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

import regexpuc from 'regexpu-core';
import { parse } from 'acorn';
import MagicString from 'magic-string';
import { simple as simpleWalk } from 'acorn-walk';

const PREAMBLE = `;{
// Precompiled regular expressions
const precompile = (r) => { r.exec('a'); r.exec('\\u1000'); };`;
const POSTAMBLE = '}';

/// Emit a block of javascript that will pre-compile the regular expressions given. As spidermonkey
/// will intern regular expressions, duplicating them at the top level and testing them with both
/// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch
/// handler.
export function precompile(source, filename = '<input>', moduleMode = false) {
const magicString = new MagicString(source, {
filename,
});

const ast = parse(source, {
ecmaVersion: 'latest',
sourceType: moduleMode ? 'module' : 'script',
});

const precompileCalls = [];
simpleWalk(ast, {
Literal(node) {
if (!node.regex) return;
let transpiledPattern;
try {
transpiledPattern = regexpuc(node.regex.pattern, node.regex.flags, {
unicodePropertyEscapes: 'transform',
});
} catch {
// swallow regex parse errors here to instead throw them at the engine level
// this then also avoids regex parser bugs being thrown unnecessarily
transpiledPattern = node.regex.pattern;
}
const transpiledRegex = `/${transpiledPattern}/${node.regex.flags}`;
precompileCalls.push(`precompile(${transpiledRegex});`);
magicString.overwrite(node.start, node.end, transpiledRegex);
},
});

if (!precompileCalls.length) return source;

magicString.prepend(`${PREAMBLE}${precompileCalls.join('\n')}${POSTAMBLE}`);

// When we're ready to pipe in source maps:
// const map = magicString.generateMap({
// source: 'source.js',
// file: 'converted.js.map',
// includeContent: true
// });

return magicString.toString();
}
16 changes: 10 additions & 6 deletions test/test-app/package-lock.json

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

3 changes: 1 addition & 2 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ echo "built the test app successfully"
# Start the spin app in the background
echo "Starting Spin app"
spin up &
SPIN_PID=$!

# wait for app to be up and running
echo "Waiting for Spin app to be ready"
Expand All @@ -36,7 +35,7 @@ echo "\n\nTest completed"

# kill the spin app
echo "Stopping Spin"
kill -9 $SPIN_PID
killall spin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can keep this to the specific process by changing from kill -9 to kill -6. Can you test that and see if it works? I had this result in very unexpected behavior before, and am certain that we (and others) would run into it again in the future.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This did not work, I can take a look at how to fix this in a separate PR.



if [ "$isFailed" = true ] ; then
Expand Down