@kubohiroya/turbowarp-plus-editor-test-runtime 0.0.0-stage → 0.2.0
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/LICENSE +373 -0
- package/README.ja.md +70 -0
- package/README.md +69 -2
- package/THIRD_PARTY_NOTICES.md +7544 -0
- package/dist/build-info.json +10 -0
- package/dist/runtime.js +85872 -0
- package/node/index.d.ts +12 -0
- package/node/index.js +56 -0
- package/package.json +48 -4
- package/page.d.ts +33 -0
- package/pins.json +4 -0
- package/src/buffer-shim.js +1 -0
- package/src/runtime.js +105 -0
package/node/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface EditorRuntimeServer {
|
|
2
|
+
/** The page's URL, ending in `/`. */
|
|
3
|
+
url: string;
|
|
4
|
+
close(): Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
export interface EditorRuntimeServerOptions {
|
|
7
|
+
/** A test's page entry, bundled and loaded after the runtime. */
|
|
8
|
+
script?: string;
|
|
9
|
+
/** Files to add or replace, by served path. */
|
|
10
|
+
files?: Record<string, string | Uint8Array>;
|
|
11
|
+
}
|
|
12
|
+
export declare function serveEditorRuntime(options?: EditorRuntimeServerOptions): Promise<EditorRuntimeServer>;
|
package/node/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Serves the minimal editor runtime page on a free local port.
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {fileURLToPath} from 'node:url';
|
|
6
|
+
import {build} from 'esbuild';
|
|
7
|
+
import {directory as blocksDirectory, scripts as blockScripts} from '@kubohiroya/turbowarp-plus-editor-test-runtime-blocks';
|
|
8
|
+
|
|
9
|
+
const runtimeDirectory = fileURLToPath(new URL('../dist/', import.meta.url));
|
|
10
|
+
const types = {'.html': 'text/html', '.js': 'text/javascript', '.json': 'application/json', '.svg': 'image/svg+xml',
|
|
11
|
+
'.png': 'image/png', '.cur': 'image/x-icon', '.wav': 'audio/wav', '.mp3': 'audio/mpeg', '.css': 'text/css'};
|
|
12
|
+
|
|
13
|
+
const page = test => `<!doctype html>
|
|
14
|
+
<meta charset="utf-8">
|
|
15
|
+
<title>Minimal editor runtime</title>
|
|
16
|
+
<style>html, body {margin: 0; height: 100%} #workspace {position: absolute; inset: 0}</style>
|
|
17
|
+
<div id="workspace"></div>
|
|
18
|
+
${blockScripts.map(file => `<script src="blocks/${file}"></script>`).join('\n')}
|
|
19
|
+
<script src="runtime.js"></script>
|
|
20
|
+
${test ? '<script src="test.js"></script>\n' : ''}`;
|
|
21
|
+
|
|
22
|
+
// Resolves a request path inside `root`, or null when it would leave it.
|
|
23
|
+
const inside = (root, name) => {
|
|
24
|
+
const file = path.resolve(root, name);
|
|
25
|
+
return file.startsWith(root) ? file : null;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Starts the server. `script` is a test's page entry: it is bundled with esbuild and loaded after the runtime,
|
|
30
|
+
* so it can call `EditorRuntime.start` and use the package under test. `files` adds or replaces served files.
|
|
31
|
+
*/
|
|
32
|
+
export async function serveEditorRuntime({script, files = {}} = {}) {
|
|
33
|
+
const served = {'index.html': page(Boolean(script)), ...files};
|
|
34
|
+
if (script) {
|
|
35
|
+
const result = await build({entryPoints: [script], bundle: true, write: false, format: 'iife', platform: 'browser',
|
|
36
|
+
define: {'process.env.NODE_ENV': '"development"'}, loader: {'.css': 'text'}, logLevel: 'error'});
|
|
37
|
+
served['test.js'] = result.outputFiles[0].text;
|
|
38
|
+
}
|
|
39
|
+
const server = http.createServer(async (request, response) => {
|
|
40
|
+
const name = decodeURIComponent(new URL(request.url ?? '/', 'http://localhost').pathname).slice(1) || 'index.html';
|
|
41
|
+
const file = name.startsWith('blocks/') ? inside(blocksDirectory, name.slice('blocks/'.length)) :
|
|
42
|
+
name.startsWith('media/') ? inside(blocksDirectory, name) : inside(runtimeDirectory, name);
|
|
43
|
+
try {
|
|
44
|
+
const body = Object.hasOwn(served, name) ? served[name] : file && await fs.readFile(file);
|
|
45
|
+
if (body === null) throw new Error('outside');
|
|
46
|
+
response.writeHead(200, {'content-type': types[path.extname(name)] ?? 'application/octet-stream'});
|
|
47
|
+
response.end(body);
|
|
48
|
+
} catch {
|
|
49
|
+
response.writeHead(404);
|
|
50
|
+
response.end();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
54
|
+
const {port} = server.address();
|
|
55
|
+
return {url: `http://127.0.0.1:${port}/`, close: () => new Promise(resolve => server.close(resolve))};
|
|
56
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubohiroya/turbowarp-plus-editor-test-runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A minimal TurboWarp Plus Editor runtime for package tests: the Editor's pinned VM and Scratch Blocks with one workspace, implementing the editor host contract",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"author": "Hiroya Kubo",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/kubohiroya/turbowarp-plus-editor-test-runtime.git"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./node/index.d.ts",
|
|
15
|
+
"default": "./node/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./page": {
|
|
18
|
+
"types": "./page.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"node",
|
|
24
|
+
"src",
|
|
25
|
+
"page.d.ts",
|
|
26
|
+
"pins.json",
|
|
27
|
+
"THIRD_PARTY_NOTICES.md",
|
|
28
|
+
"README.md",
|
|
29
|
+
"README.ja.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node scripts/build.mjs",
|
|
34
|
+
"test": "npm run build && node --test test/*.test.js",
|
|
35
|
+
"test:browser": "npm run build && node --test test/*.browser.js"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@kubohiroya/turbowarp-plus-editor-host": "0.1.0",
|
|
39
|
+
"@kubohiroya/turbowarp-plus-editor-test-runtime-blocks": "0.1.0",
|
|
40
|
+
"esbuild": "0.25.12"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@playwright/test": "1.64.1",
|
|
44
|
+
"@turbowarp/scratch-storage": "2.0.0",
|
|
45
|
+
"@turbowarp/scratch-svg-renderer": "1.1.0",
|
|
46
|
+
"buffer": "4.9.2",
|
|
47
|
+
"scratch-render": "git+https://github.com/TurboWarp/scratch-render.git#655edeb5a7b107aa26fb02a3beb0bbcfb8c350ff",
|
|
48
|
+
"scratch-vm": "git+https://github.com/kubohiroya/scratch-vm.git#3ac5fb7052ffc371f1e615245fcc15d61a368237"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/page.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Types for test pages: the global the runtime defines in the browser.
|
|
2
|
+
import type {EditorHost} from '@kubohiroya/turbowarp-plus-editor-host';
|
|
3
|
+
|
|
4
|
+
export interface EditorRuntimeHost extends EditorHost {
|
|
5
|
+
/** The workspace that shows the VM's editing target. */
|
|
6
|
+
workspace: any;
|
|
7
|
+
/** Loads a project (`project.json` contents) and edits its first sprite. The project is then saved. */
|
|
8
|
+
loadProject(project: string | object): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Registers an extension object as the VM registers its built-in ones (no sandbox, Loader or review)
|
|
11
|
+
* and defines its blocks in the workspace. Returns the VM service name.
|
|
12
|
+
*/
|
|
13
|
+
registerExtension(extension: {getInfo(): {id: string}}): string;
|
|
14
|
+
}
|
|
15
|
+
/** The VM's own modules an extension host needs, such as the Loader's registrar. */
|
|
16
|
+
export interface VMModules {
|
|
17
|
+
ScratchCommon: Record<string, any>;
|
|
18
|
+
createTranslate(vm: unknown): unknown;
|
|
19
|
+
staticFetch(url: unknown): Response | null;
|
|
20
|
+
dispatch: {services: Record<string, any>; setServiceSync(name: string, provider: unknown): void};
|
|
21
|
+
}
|
|
22
|
+
export interface EditorRuntimeOptions {
|
|
23
|
+
locale?: string;
|
|
24
|
+
project?: string | object;
|
|
25
|
+
}
|
|
26
|
+
declare global {
|
|
27
|
+
const EditorRuntime: {
|
|
28
|
+
start(container: HTMLElement, options?: EditorRuntimeOptions): Promise<EditorRuntimeHost>;
|
|
29
|
+
vmModules: VMModules;
|
|
30
|
+
/** The VM constructor, for a second VM such as a preview. */
|
|
31
|
+
VirtualMachine: new () => any;
|
|
32
|
+
};
|
|
33
|
+
}
|
package/pins.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {Buffer} from 'buffer';
|
package/src/runtime.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// The minimal editor runtime: the pinned scratch-vm and scratch-blocks, one workspace kept in step with the
|
|
2
|
+
// VM's editing target, and the editor host contract that family packages are written against.
|
|
3
|
+
import VirtualMachine from 'scratch-vm';
|
|
4
|
+
import Blocks from 'scratch-vm/src/engine/blocks.js';
|
|
5
|
+
import {deserializeBlocks} from 'scratch-vm/src/serialization/sb3.js';
|
|
6
|
+
import ScratchStorage from '@turbowarp/scratch-storage';
|
|
7
|
+
import ScratchRender from 'scratch-render';
|
|
8
|
+
import {BitmapAdapter} from '@turbowarp/scratch-svg-renderer';
|
|
9
|
+
import ScratchCommon from 'scratch-vm/src/extension-support/tw-extension-api-common.js';
|
|
10
|
+
import createTranslate from 'scratch-vm/src/extension-support/tw-l10n.js';
|
|
11
|
+
import staticFetch from 'scratch-vm/src/util/tw-static-fetch.js';
|
|
12
|
+
import dispatch from 'scratch-vm/src/dispatch/central-dispatch.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The VM modules an extension host needs as the VM's own instances: `dispatch` is a singleton, so a
|
|
16
|
+
* copy from anywhere else would not be the one this VM uses. The Loader's registrar takes these.
|
|
17
|
+
*/
|
|
18
|
+
export const vmModules = Object.freeze({ScratchCommon, createTranslate, staticFetch, dispatch});
|
|
19
|
+
/** The VM constructor, for a test that needs a second VM, such as a preview. */
|
|
20
|
+
export {VirtualMachine};
|
|
21
|
+
|
|
22
|
+
// Define an extension's blocks in Scratch Blocks, as the GUI does when one is added or changes:
|
|
23
|
+
// custom fields and menus are blocks too, so they are defined first. Dynamic blocks need the GUI's own
|
|
24
|
+
// block factory and are not supported here.
|
|
25
|
+
function defineExtensionBlocks(Blockly, categoryInfo) {
|
|
26
|
+
const json = [
|
|
27
|
+
...Object.values(categoryInfo.customFieldTypes || {}).map(field => field.scratchBlocksDefinition),
|
|
28
|
+
...(categoryInfo.menus || []),
|
|
29
|
+
...(categoryInfo.blocks || [])
|
|
30
|
+
].filter(block => block && block.json && !(block.info && block.info.isDynamic)).map(block => block.json);
|
|
31
|
+
if (json.length) Blockly.defineBlocksWithJsonArray(json);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function workspaceXml(vm, target) {
|
|
35
|
+
const variables = Object.assign({}, vm.runtime.getTargetForStage().variables, target.variables);
|
|
36
|
+
const declarations = Object.values(variables).map(variable => variable.toXML()).join('');
|
|
37
|
+
return `<xml xmlns="http://www.w3.org/1999/xhtml"><variables>${declarations}</variables>${target.blocks.toXML(target.comments)}</xml>`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Starts a VM and a Blockly workspace in `container` and resolves with the editor host. */
|
|
41
|
+
export async function start(container, {locale = 'en', project} = {}) {
|
|
42
|
+
const Blockly = globalThis.Blockly;
|
|
43
|
+
if (!Blockly) throw new Error('scratch-blocks must be loaded before the runtime');
|
|
44
|
+
const vm = new VirtualMachine();
|
|
45
|
+
// Assets come only from what a test adds to storage; nothing is fetched from the network.
|
|
46
|
+
vm.attachStorage(new ScratchStorage());
|
|
47
|
+
// The VM falls back to drawing default costumes, which needs a renderer; the stage canvas stays off screen.
|
|
48
|
+
const canvas = document.createElement('canvas');
|
|
49
|
+
canvas.width = 480; canvas.height = 360;
|
|
50
|
+
vm.attachRenderer(new ScratchRender(canvas));
|
|
51
|
+
vm.attachV2BitmapAdapter(new BitmapAdapter());
|
|
52
|
+
const workspace = Blockly.inject(container, {
|
|
53
|
+
media: './media/', toolbox: '<xml><category name="Motion" id="motion" colour="#4C97FF" secondaryColour="#3373CC"></category></xml>',
|
|
54
|
+
zoom: {controls: false, wheel: false, startScale: 0.675}, scrollbars: true
|
|
55
|
+
});
|
|
56
|
+
workspace.addChangeListener(vm.blockListener);
|
|
57
|
+
const defineBlocks = categoryInfo => defineExtensionBlocks(Blockly, categoryInfo);
|
|
58
|
+
vm.on('EXTENSION_ADDED', defineBlocks);
|
|
59
|
+
vm.on('BLOCKSINFO_UPDATE', defineBlocks);
|
|
60
|
+
vm.on('workspaceUpdate', data => {
|
|
61
|
+
Blockly.Events.disable();
|
|
62
|
+
try {Blockly.Xml.clearWorkspaceAndLoadFromXml(Blockly.Xml.textToDom(data.xml), workspace);}
|
|
63
|
+
finally {Blockly.Events.enable();}
|
|
64
|
+
});
|
|
65
|
+
let dirty = false;
|
|
66
|
+
const listeners = new Set();
|
|
67
|
+
const notify = () => {for (const listener of listeners) listener();};
|
|
68
|
+
vm.on('PROJECT_CHANGED', () => {dirty = true; notify();});
|
|
69
|
+
vm.start();
|
|
70
|
+
const host = {
|
|
71
|
+
vm, locale, workspace,
|
|
72
|
+
getBlockly: async () => Blockly,
|
|
73
|
+
deserializeBlocks,
|
|
74
|
+
createBlockContainer: () => new Blocks(vm.runtime, true),
|
|
75
|
+
isProjectDirty: () => dirty,
|
|
76
|
+
markSaved() {dirty = false; notify();},
|
|
77
|
+
isRunning: () => vm.runtime.threads.some(thread => !thread.updateMonitor && vm.runtime.isActiveThread(thread)),
|
|
78
|
+
subscribe(listener) {listeners.add(listener); return () => listeners.delete(listener);},
|
|
79
|
+
/** Shows external block changes, as the Editor does after reloading scripts in place. */
|
|
80
|
+
refreshBlockViews(targets) {
|
|
81
|
+
if (targets.includes(vm.editingTarget)) vm.emit('workspaceUpdate', {xml: workspaceXml(vm, vm.editingTarget)});
|
|
82
|
+
vm.emit('EXTERNAL_BLOCKS_RELOADED', targets);
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* Registers an extension object with the VM as the VM does for its built-in ones: no sandbox, no
|
|
86
|
+
* Loader and no review. Its blocks are defined in the workspace. Returns the VM service name.
|
|
87
|
+
*/
|
|
88
|
+
registerExtension(extension) {
|
|
89
|
+
const {id} = extension.getInfo();
|
|
90
|
+
if (vm.extensionManager.isExtensionLoaded(id)) throw new Error(`Extension ${id} is already registered`);
|
|
91
|
+
const service = vm.extensionManager._registerInternalExtension(extension);
|
|
92
|
+
vm.extensionManager._loadedExtensions.set(id, service);
|
|
93
|
+
return service;
|
|
94
|
+
},
|
|
95
|
+
async loadProject(json) {
|
|
96
|
+
await vm.loadProject(typeof json === 'string' ? json : JSON.stringify(json));
|
|
97
|
+
const sprite = vm.runtime.targets.find(target => !target.isStage) ?? vm.runtime.targets[0];
|
|
98
|
+
vm.setEditingTarget(sprite.id);
|
|
99
|
+
dirty = false;
|
|
100
|
+
notify();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
if (project) await host.loadProject(project);
|
|
104
|
+
return host;
|
|
105
|
+
}
|