Skip to content

Commit

Permalink
Merge pull request #1262 from nickcastel50/conway_wasm_profiling
Browse files Browse the repository at this point in the history
Conway wasm profiling + windows build fixes + conway upgrade 0.1.670
  • Loading branch information
nickcastel50 authored Oct 15, 2024
2 parents 4651b25 + b77c0e3 commit 1eeff6a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 31 deletions.
Binary file removed bldrs-ai-conway-0.1.655.tgz
Binary file not shown.
Binary file added bldrs-ai-conway-0.1.670.tgz
Binary file not shown.
Binary file removed bldrs-ai-conway-web-ifc-adapter-0.1.2.tgz
Binary file not shown.
Binary file added bldrs-ai-conway-web-ifc-adapter-0.1.3.tgz
Binary file not shown.
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bldrs",
"version": "1.0.1128",
"version": "1.0.1133",
"main": "src/index.jsx",
"license": "AGPL-3.0",
"homepage": "https://github.com/bldrs-ai/Share",
Expand All @@ -10,8 +10,9 @@
"scripts": {
"build": "yarn build-conway",
"build-conway": "yarn clean && yarn build-share-conway && yarn build-cosmos",
"build-conway-debug":"yarn clean && yarn build-share-conway && yarn build-share-copy-wasm-conway-profile && yarn build-cosmos",
"build-webifc": "yarn clean && yarn build-share-webifc && yarn build-cosmos",
"build-cosmos": "shx mkdir -p docs ; shx rm -rf docs/cosmos; cosmos-export --config .cosmos.config.json && shx mv cosmos-export docs/cosmos",
"build-cosmos": "shx mkdir -p docs && shx rm -rf docs/cosmos && cosmos-export --config .cosmos.config.json && shx mv cosmos-export docs/cosmos",
"build-share": "yarn update-version && node tools/esbuild/build.js && shx mkdir -p docs/static/js && shx cp src/OPFS/OPFS.worker.js docs/ && shx cp src/net/github/Cache.js docs/",
"build-share-conway": "run-script-os",
"build-share-conway:win32": "set USE_WEBIFC_SHIM=true&& yarn build-share",
Expand All @@ -20,6 +21,7 @@
"build-share-webifc:win32": "set USE_WEBIFC_SHIM=false&& yarn build-share && yarn build-share-copy-wasm-webifc",
"build-share-webifc:linux:darwin": "USE_WEBIFC_SHIM=false yarn build-share && yarn build-share-copy-wasm-webifc",
"build-share-copy-wasm-webifc": "shx cp node_modules/web-ifc/*.wasm docs/static/js",
"build-share-copy-wasm-conway-profile": "shx cp node_modules/@bldrs-ai/conway-web-ifc-adapter/node_modules/@bldrs-ai/conway/compiled/dependencies/conway-geom/Dist/* docs/static/js",
"build-share-analyze": "ANALYZE=true node tools/esbuild/build.js",
"clean": "shx rm -rf docs",
"cy": "cypress run --browser chrome",
Expand All @@ -36,8 +38,12 @@
"typecheck": "tsc -p tools/tsconfig.json",
"lint-tools": "yarn eslint cypress tools",
"precommit": "yarn lint && yarn test",
"serve": "SHARE_CONFIG=dev yarn serve-share-conway",
"serve-prod": "SHARE_CONFIG=prod yarn serve-share-conway",
"serve":"run-script-os",
"serve:win32": "set SHARE_CONFIG=dev && yarn serve-share-conway",
"serve:linux:darwin": "SHARE_CONFIG=dev yarn serve-share-conway",
"serve-prod":"run-script-os",
"serve-prod:win32": "set SHARE_CONFIG=prod && yarn serve-share-conway",
"serve-prod:linux:darwin": "SHARE_CONFIG=prod yarn serve-share-conway",
"serve-cosmos": "cosmos --config .cosmos.config.json",
"serve-share-conway": "run-script-os",
"serve-share-conway:win32": "yarn build-share-conway && set USE_WEBIFC_SHIM=true&& node tools/esbuild/serve.js",
Expand All @@ -57,8 +63,7 @@
"@babel/plugin-syntax-import-assertions": "7.18.6",
"@babel/preset-env": "7.18.10",
"@babel/preset-react": "7.18.6",
"@bldrs-ai/conway": "./bldrs-ai-conway-0.1.655.tgz",
"@bldrs-ai/conway-web-ifc-adapter": "./bldrs-ai-conway-web-ifc-adapter-0.1.2.tgz",
"@bldrs-ai/conway-web-ifc-adapter": "./bldrs-ai-conway-web-ifc-adapter-0.1.3.tgz",
"@bldrs-ai/ifclib": "5.3.3",
"@emotion/react": "11.10.0",
"@emotion/styled": "11.10.0",
Expand Down
97 changes: 79 additions & 18 deletions tools/esbuild/proxy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import http from 'node:http'
import https from 'node:https'

/**
* @param {string} proxiedHost The host to which traffic will be sent. E.g. localhost
* @param {number} port The port to which traffic will be sent. E.g. 8079
* @param {boolean} useHttps Whether to use HTTPS for the proxied server
* @return {object} Proxy server
* @see https://esbuild.github.io/api/#serve-proxy
*/
export function createProxyServer(host, port) {
export function createProxyServer(host, port, useHttps = false) {
const requestModule = useHttps ? https : http

return http.createServer((req, res) => {
// Rewrite the URL if it matches the pattern for a .wasm file
req.url = rewriteUrl(req.url)

const options = {
hostname: host,
port: port,
Expand All @@ -16,25 +23,40 @@ export function createProxyServer(host, port) {
headers: req.headers,
}

// Forward each incoming request to esbuild
const proxyReq = http.request(options, (proxyResponse) => {
// If esbuild cannot find the resource to serve, send our react-router bounce.
// Forward each incoming request to the proxied server
const proxyReq = requestModule.request(options, (proxyResponse) => {
// If proxied server cannot find the resource, send a custom not-found page
if (proxyResponse.statusCode === HTTP_NOT_FOUND) {
serveNotFound(res)
return
}

// Set the correct Content-Type for .js files
if (req.url.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript')
// Set the correct Content-Type for specific file types
const contentType = getContentType(req.url)
if (contentType) {
res.setHeader('Content-Type', contentType)
}

// Otherwise, forward the response from esbuild to the client
// Optionally set Cache-Control headers for static assets
if (isCacheable(req.url)) {
res.setHeader('Cache-Control', 'public, max-age=31536000')
}

// Forward the response from the proxied server to the client
res.writeHead(proxyResponse.statusCode, proxyResponse.headers)
proxyResponse.pipe(res, {end: true})
})

// Forward the body of the request to esbuild
// Handle request errors
proxyReq.on('error', (err) => {
// eslint-disable-next-line no-console
console.error(`Proxy request error: ${err.message}`)
// eslint-disable-next-line no-magic-numbers
res.writeHead(500)
res.end('Internal Server Error')
})

// Forward the body of the request to the proxied server
req.pipe(proxyReq, {end: true})
})
}
Expand All @@ -43,31 +65,70 @@ const HTTP_FOUND = 200
const HTTP_NOT_FOUND = 404

/** Serve a 200 bounce page for missing resources. */
const serveNotFound = ((res) => {
const serveNotFound = (res) => {
res.writeHead(HTTP_FOUND, {'Content-Type': 'text/html'})
res.end(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>BLDRS - Redirect</title>
<script type="text/javascript">
// Single Page Apps for GitHub Pages
// MIT License
// https://github.com/rafgraph/spa-github-pages
// This page needs to be > 512 bytes to work for IE. Currently 968.
var pathSegmentsToKeep = window.location.pathname.startsWith('/Share') ? 1 : 0
var l = window.location
var u1 = l.protocol + '//' + l.hostname + (l.port ? ':' + l.port : '')
var u2 = l.pathname.split('/').slice(0, 1 + pathSegmentsToKeep).join('/') + '/?/'
var u3 = l.pathname.slice(1).split('/').slice(pathSegmentsToKeep).join('/').replace(/&/g, '~and~')
var u4 = (l.search ? '&' + l.search.slice(1).replace(/&/g, '~and~') : '')
//console.log('Redirect URL parts: ', u1, u2, u3, u4)
l.replace(u1 + u2 + u3 + u4 + l.hash)
</script>
</head>
<body>
Resource not found. Redirecting...
Resource not found. Redirecting...
</body>
</html>`)
})
}

/**
* Rewrite the URL if it's a .wasm file.
*
* @param {string} url The original request URL
* @return {string} The rewritten URL
*/
function rewriteUrl(url) {
// If the URL is for a .wasm file and starts with '/share/v/p/', rewrite it
if (url.endsWith('.wasm') && url.startsWith('/share/v/p/')) {
return url.replace('/share/v/p/', '/')
}
return url
}

/**
* Get the Content-Type based on file extension.
*
* @param {string} url The request URL
* @return {string|null} The MIME type or null if not recognized
*/
function getContentType(url) {
if (url.endsWith('.js')) {
return 'application/javascript'
} else if (url.endsWith('.wasm')) {
return 'application/wasm'
} else if (url.endsWith('.json')) {
return 'application/json'
} else if (url.endsWith('.css')) {
return 'text/css'
} else if (url.endsWith('.html')) {
return 'text/html'
}
return null
}

/**
* Determine if a resource is cacheable.
*
* @param {string} url The request URL
* @return {boolean} True if the resource should be cached
*/
function isCacheable(url) {
return /\.(js|css|wasm|png|jpg|jpeg|gif|svg|ico)$/.test(url)
}
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2078,17 +2078,17 @@
resolved "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@bldrs-ai/conway-web-ifc-adapter@./bldrs-ai-conway-web-ifc-adapter-0.1.2.tgz":
version "0.1.2"
resolved "./bldrs-ai-conway-web-ifc-adapter-0.1.2.tgz#25e6e8f576a66718cd7d81c8880d89ca624995e3"
"@bldrs-ai/conway-web-ifc-adapter@./bldrs-ai-conway-web-ifc-adapter-0.1.3.tgz":
version "0.1.3"
resolved "./bldrs-ai-conway-web-ifc-adapter-0.1.3.tgz#071650ae2ce7e17f620030179baa7287bffd00b5"
dependencies:
"@bldrs-ai/conway" "./bldrs-ai-conway-0.1.655.tgz"
"@bldrs-ai/conway" "./bldrs-ai-conway-0.1.670.tgz"
"@types/node" "22.5.5"
typescript "5.6.2"

"@bldrs-ai/conway@./bldrs-ai-conway-0.1.655.tgz":
version "0.1.655"
resolved "./bldrs-ai-conway-0.1.655.tgz#069513d7388a774883b22b0a54819c413939d07c"
"@bldrs-ai/conway@./bldrs-ai-conway-0.1.670.tgz":
version "0.1.670"
resolved "./bldrs-ai-conway-0.1.670.tgz#3538df2b625f1a582086582fa3dd51c4b4f84556"
dependencies:
buffer "^6.0.3"
gl-matrix "^3.4.3"
Expand Down

0 comments on commit 1eeff6a

Please sign in to comment.