-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test runner and prepare doc
- Loading branch information
1 parent
8928f4a
commit 1b1424d
Showing
19 changed files
with
474 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,4 +129,6 @@ dist | |
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
.tshy | ||
.tshy | ||
|
||
src/modules/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.tshy | ||
.vscode | ||
.env | ||
node_modules | ||
docs | ||
examples | ||
src | ||
vendor | ||
bun* | ||
biome* | ||
*.ts | ||
**/*.test.* | ||
|
||
!dist | ||
!package.json | ||
!README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# QuickJS Sandbox in Javascript & Typescript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Running tests in QuickJS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const code = ` | ||
import 'test' | ||
describe('mocha', ()=> { | ||
it('should work',()=>{ | ||
expect(true).to.be.true | ||
}) | ||
}) | ||
const testResult = await runTests(); | ||
export default testResult | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { quickJS } from './src/quickJS.js' | ||
|
||
const code = `export default await new Promise((resolve)=> setTimeout(()=>resolve('DONE'), 20_000)) | ||
` | ||
|
||
let runtime: Awaited<ReturnType<typeof quickJS>> | undefined | ||
|
||
while (true) { | ||
if (!runtime) { | ||
runtime = await quickJS() | ||
} | ||
|
||
const { evalCode } = await runtime.createRuntime({ | ||
executionTimeout: 2, | ||
allowFs: true, | ||
allowFetch: true, | ||
env: {}, | ||
}) | ||
|
||
const result = await evalCode(code) | ||
|
||
console.log(result) | ||
|
||
if (!result.ok && result.error.name === 'ExecutionTimeout') { | ||
runtime = undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,74 @@ | ||
import { getQuickJS } from 'quickjs-emscripten' | ||
import fs from 'node:fs' | ||
import path from 'node:path' | ||
|
||
const QuickJS = await getQuickJS() | ||
interface ExportsField { | ||
import?: string | ExportsField | ||
require?: string | ExportsField | ||
[key: string]: any | ||
} | ||
|
||
const vm = QuickJS.newContext() | ||
interface PackageJson { | ||
name: string | ||
main?: string | ||
module?: string | ||
exports?: string | ExportsField | ||
[key: string]: any | ||
} | ||
|
||
setInterval(() => vm.runtime.executePendingJobs(), 1) | ||
function resolveExportsField(exportsField: ExportsField, subPath: string, isEsm: boolean): string | undefined { | ||
if (typeof exportsField === 'string') { | ||
return subPath ? undefined : exportsField | ||
} | ||
if (isEsm && exportsField.import) { | ||
return resolveExportsField(exportsField.import as ExportsField, subPath, isEsm) | ||
} | ||
if (!isEsm && exportsField.require) { | ||
return resolveExportsField(exportsField.require as ExportsField, subPath, isEsm) | ||
} | ||
if (exportsField[subPath]) { | ||
return isEsm ? exportsField[subPath].import : exportsField[subPath].require | ||
} | ||
return undefined | ||
} | ||
|
||
// Evaluate code that uses `readFile`, which returns a promise | ||
const result = vm.evalCode(`(async () => { | ||
function getEntryPointPath(importPath: string): string | undefined { | ||
const [packageName, subPath] = importPath.split(/\/(.+)/) | ||
|
||
const x = ()=> new Promise( (resolve, reject) => { | ||
resolve('resolve') | ||
}) | ||
const packageJsonPath = path.resolve('node_modules', packageName, 'package.json') | ||
if (!fs.existsSync(packageJsonPath)) { | ||
throw new Error(`Cannot find package.json for package: ${packageName}`) | ||
} | ||
|
||
const y = ()=>'hello' | ||
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf-8') | ||
const packageJson: PackageJson = JSON.parse(packageJsonContent) | ||
|
||
const content = await y() | ||
return content.toUpperCase() | ||
})()`) | ||
const promiseHandle = vm.unwrapResult(result) | ||
let entryPoint: string | undefined | ||
if (packageJson.exports) { | ||
if (typeof packageJson.exports === 'string') { | ||
entryPoint = subPath ? undefined : packageJson.exports | ||
} else if (typeof packageJson.exports === 'object') { | ||
// Try to resolve ESM entry point first | ||
entryPoint = resolveExportsField(packageJson.exports as ExportsField, subPath, true) | ||
if (!entryPoint) { | ||
// Fallback to CommonJS entry point | ||
entryPoint = resolveExportsField(packageJson.exports as ExportsField, subPath, false) | ||
} | ||
} | ||
} | ||
if (!entryPoint && packageJson.module) { | ||
entryPoint = subPath ? undefined : packageJson.module | ||
} | ||
if (!entryPoint && packageJson.main) { | ||
entryPoint = subPath ? undefined : packageJson.main | ||
} | ||
if (!entryPoint) { | ||
return undefined | ||
} | ||
|
||
const resolvedResult = await vm.resolvePromise(promiseHandle) | ||
promiseHandle.dispose() | ||
const resolvedHandle = vm.unwrapResult(resolvedResult) | ||
console.log('Result:', vm.getString(resolvedHandle)) | ||
resolvedHandle.dispose() | ||
const finalPath = subPath ? path.join(path.dirname(entryPoint), subPath) : entryPoint | ||
return path.resolve('/', 'node_modules', packageJson.name, finalPath) | ||
} | ||
|
||
// Example usage: | ||
console.log(getEntryPointPath('my-package')) // should return /node_modules/my-package/dist/index.mjs | ||
console.log(getEntryPointPath('my-package/feature')) // should return /node_modules/my-package/dist/feature.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { join } from 'node:path' | ||
|
||
const testRunnerResult = await Bun.build({ | ||
entrypoints: ['./vendor/testrunner/testRunner.ts'], | ||
format: 'esm', | ||
minify: true, | ||
}) | ||
|
||
for (const res of testRunnerResult.outputs) { | ||
const content = await res.text() | ||
|
||
Bun.write(join('src', 'modules', 'build', 'test-lib.js'), content) | ||
|
||
console.info('test lib generated') | ||
} |
Oops, something went wrong.