@gameslate/sdk 0.3.26 → 0.3.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -90,6 +90,9 @@ the input `downloads/<id>`; the player sees it among the plugin's permissions.
90
90
 
91
91
  `0.3.x` speaks plugin contract (`sdkApi`) 3: new 0.3 versions add tools, never change the contract. The core refuses a plugin built for another contract.
92
92
 
93
+ - **0.3.27**: \`runInSandbox(code, dir, budgetMs)\` from \`@gameslate/sdk/build\`, for tests: bundles
94
+ \`code\` and runs it in QuickJS as the core does, so a cost only the sandbox has (a string built
95
+ by \`+=\`) fails a test instead of a player's build.
93
96
  - **0.3.26**: `Bytes#toString` decodes 8 KiB per call and joins the pieces, since building the
94
97
  string a byte at a time was quadratic in QuickJS (a 1 MB string took 147 s). `isCoreInput(name)`
95
98
  and `REGISTRY_INPUT_NAME` name the inputs the core adds (the registry manifest, downloads), which
@@ -29,3 +29,8 @@ export function zipEntries(
29
29
  /** The loader's limits on a plugin's WebAssembly, checked at build time too. */
30
30
  export const MAX_WASM_MODULES: number;
31
31
  export const MAX_WASM_BYTES: number;
32
+
33
+ /** For tests: runs `code` (TypeScript importing relative to `dir`) in QuickJS as the core's
34
+ * sandbox does and returns what it leaves in `globalThis.result`; fails past `budgetMs`. A cost
35
+ * only the sandbox has (a long string built by `+=`) shows here and not in vitest. */
36
+ export function runInSandbox(code: string, dir: string, budgetMs: number): Promise<unknown>;
@@ -8,6 +8,7 @@ import { checkSpreadLimit } from './spread-limit.mjs';
8
8
  import { renderLicences } from './third-party-licences.mjs';
9
9
  import { zipFolder } from './zip.mjs';
10
10
 
11
+ export { runInSandbox } from './run-in-sandbox.mjs';
11
12
  export { zipEntries } from './zip.mjs';
12
13
 
13
14
  /*
@@ -0,0 +1,39 @@
1
+ import releaseSync from '@jitl/quickjs-wasmfile-release-sync';
2
+ import * as esbuild from 'esbuild';
3
+ import {
4
+ newQuickJSWASMModuleFromVariant,
5
+ shouldInterruptAfterDeadline,
6
+ } from 'quickjs-emscripten-core';
7
+
8
+ /*
9
+ * For tests: runs `code` (TypeScript, importing relative to `dir`) in QuickJS as the core's
10
+ * sandbox does, and returns what it leaves in `globalThis.result`. Some costs exist only there:
11
+ * QuickJS copies a whole string on every `+=`, so a long string built a piece at a time is
12
+ * quadratic in the sandbox and linear in Node. Such a test fails by running out of `budgetMs`.
13
+ */
14
+ export async function runInSandbox(code, dir, budgetMs) {
15
+ const out = await esbuild.build({
16
+ stdin: { contents: code, resolveDir: dir, loader: 'ts' },
17
+ bundle: true,
18
+ format: 'iife',
19
+ platform: 'neutral',
20
+ write: false,
21
+ logLevel: 'silent',
22
+ });
23
+ const [file] = out.outputFiles;
24
+ if (!file) throw new Error('esbuild wrote no bundle');
25
+ const quickJs = await newQuickJSWASMModuleFromVariant(releaseSync);
26
+ const runtime = quickJs.newRuntime();
27
+ runtime.setInterruptHandler(shouldInterruptAfterDeadline(Date.now() + budgetMs));
28
+ const vm = runtime.newContext();
29
+ try {
30
+ vm.unwrapResult(vm.evalCode(file.text)).dispose();
31
+ const result = vm.getProp(vm.global, 'result');
32
+ const value = vm.dump(result);
33
+ result.dispose();
34
+ return value;
35
+ } finally {
36
+ vm.dispose();
37
+ runtime.dispose();
38
+ }
39
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gameslate/sdk",
3
- "version": "0.3.26",
3
+ "version": "0.3.27",
4
4
  "description": "Build plugins for GameSlate, the second-screen companion for PC games: the plugin contract, game-state and catalogue schemas, and the plugin builder.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -852,8 +852,8 @@ the plugin repos) in the same change as the fix. The next game should not have t
852
852
  - **Never build a long string with `+=` a piece at a time.** QuickJS copies the whole string on
853
853
  every `+=`, so it is quadratic where Node is linear: a 1 MB string a byte at a time took 147 s
854
854
  in the sandbox (the sdk's own `Bytes#toString` did this until 0.3.26). Push the pieces into an
855
- array and `join('')`, and run a multi-MB input through the sandbox
856
- (`@gameslate/sdk/host`'s `loadQuickJs`), not only vitest.
855
+ array and `join('')`, and run a multi-MB input through the sandbox with `runInSandbox(code,
856
+ dir, budgetMs)` from `@gameslate/sdk/build` (sdk 0.3.27), not only vitest.
857
857
  - **Follow every source the floor or water query asks.** DK64's floor query merges the map mesh,
858
858
  placed models and actor boxes; Conker's water height comes from water props, trigger-zone
859
859
  volumes and surface type 8 at once. Port the game's query, not one of its sources, and say in