Skip to content
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

Fix language server crash relying on node crypto #657

Merged
merged 2 commits into from
Jun 10, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@
break

case 'port_number':
console.log('Setting port number', content.port)

Check warning

Code scanning / CodeQL

Log injection Medium

Log entry depends on a
user-provided value
.
setEnvKeyValueStorage((prev) => {
let keyExists = false
const updated: [string, string][] = prev.map(([key, value]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import { z } from 'zod'
// import { getVersion, getEnginesVersion } from './lib/wasm/internals'
import BamlProjectManager from './lib/baml_project_manager'
import type { LSOptions, LSSettings } from './lib/types'
;(globalThis as any).crypto = require('node:crypto').webcrypto

const packageJson = require('../../package.json') // eslint-disable-line
function getConnection(options?: LSOptions): Connection {
Expand Down
2 changes: 1 addition & 1 deletion typescript/vscode-ext/packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "baml-extension",
"displayName": "Baml",
"description": "BAML is a DSL for AI applications.",
"version": "0.36.0",
"version": "0.36.2",
"publisher": "Boundary",
"repository": "https://github.com/BoundaryML/baml",
"homepage": "https://www.boundaryml.com",
Expand Down
79 changes: 46 additions & 33 deletions typescript/vscode-ext/packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,16 @@ export function activate(context: vscode.ExtensionContext) {
root_path: 'default',
function_name: args?.functionName ?? 'default',
})
}, 1000)

WebPanelView.currentPanel?.postMessage('port_number', {
port: port,
})
}, 2000)
console.info('Opening BAML panel')
requestDiagnostics()
WebPanelView.currentPanel?.postMessage('port_number', {
port: port,
})
},
)

Expand Down Expand Up @@ -247,42 +254,48 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.executeCommand('baml.openBamlPanel')
}

const app = require('express')()
app.use(cors())
var port: number
const server = app.listen(0, () => {
port = server.address().port
WebPanelView.currentPanel?.postMessage('port_number', {
port: port,
try {
const app = require('express')()
app.use(cors())
var port: number
const server = app.listen(0, () => {
port = server.address().port
console.log('Server started on port ' + port)
WebPanelView.currentPanel?.postMessage('port_number', {
port: port,
})
})
})

app.use(
createProxyMiddleware({
changeOrigin: true,
router: (req) => {
// Extract the original target URL from the custom header
const originalUrl = req.headers['baml-original-url']

if (typeof originalUrl === 'string') {
delete req.headers['baml-original-url']
req.headers['origin'] = `http://localhost:${port}`
return originalUrl
} else {
throw new Error('baml-original-url header is missing or invalid')
}
},
logger: console,
on: {
proxyRes: (proxyRes, req, res) => {
proxyRes.headers['Access-Control-Allow-Origin'] = '*'
app.use(
createProxyMiddleware({
changeOrigin: true,
router: (req) => {
// Extract the original target URL from the custom header
const originalUrl = req.headers['baml-original-url']

if (typeof originalUrl === 'string') {
delete req.headers['baml-original-url']
req.headers['origin'] = `http://localhost:${port}`
return originalUrl
} else {
throw new Error('baml-original-url header is missing or invalid')
}
},
error: (error) => {
console.error('proxy error:', error)
logger: console,
on: {
proxyRes: (proxyRes, req, res) => {
proxyRes.headers['Access-Control-Allow-Origin'] = '*'
},
error: (error) => {
console.error('proxy error:', error)
},
},
},
}),
)
}),
)
} catch (error) {
console.error('Failed to start proxy server:', error)
vscode.window.showErrorMessage('Failed to BAML localhost server. Contact support for help.')
}

// Add a catch-all route to handle 404 errors

Expand Down
Loading