@zaisma/cli 0.16.0 → 0.18.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/check.mjs +77 -8
- package/package.json +1 -1
package/check.mjs
CHANGED
|
@@ -77,13 +77,70 @@ const lineAround = (text, index) => {
|
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* Engine and library output: Unity's framework and loader, a .js beside a .wasm of the same name (Godot,
|
|
80
|
-
* Emscripten)
|
|
81
|
-
*
|
|
80
|
+
* Emscripten), wasm-bindgen's glue (Bevy, Trunk) beside its _bg.wasm, macroquad's and miniquad's loader
|
|
81
|
+
* beside a .wasm, and minified libraries. Their code mentions dialogs, cameras and the like without the game
|
|
82
|
+
* using them, so the behavior checks skip them.
|
|
82
83
|
*/
|
|
83
84
|
export function isEngineOutput(path, lowercasePaths) {
|
|
84
85
|
const lowercasePath = path.toLowerCase();
|
|
85
86
|
if (/\.(framework|loader)\.js$/.test(lowercasePath) || /\.min\.m?js$/.test(lowercasePath)) return true;
|
|
86
|
-
|
|
87
|
+
if (!/\.m?js$/.test(lowercasePath)) return false;
|
|
88
|
+
if (lowercasePaths.has(lowercasePath.replace(/\.m?js$/, ".wasm"))) return true;
|
|
89
|
+
if (lowercasePaths.has(lowercasePath.replace(/\.m?js$/, "_bg.wasm"))) return true;
|
|
90
|
+
const folder = lowercasePath.slice(0, lowercasePath.lastIndexOf("/") + 1);
|
|
91
|
+
return /(^|\/)(mq_js_bundle|gl)\.js$/.test(lowercasePath) && [...lowercasePaths].some((other) => other.startsWith(folder) && !other.slice(folder.length).includes("/") && other.endsWith(".wasm"));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The text with JavaScript comments blanked to spaces (line breaks kept), so indexes into it are the original's. */
|
|
95
|
+
function blankScriptComments(text) {
|
|
96
|
+
const out = text.split("");
|
|
97
|
+
const blank = (startIndex, endIndex) => {
|
|
98
|
+
for (let index = startIndex; index < endIndex; index++) if (out[index] !== "\n") out[index] = " ";
|
|
99
|
+
};
|
|
100
|
+
let lastSignificant = "";
|
|
101
|
+
for (let index = 0; index < text.length; index++) {
|
|
102
|
+
const character = text[index];
|
|
103
|
+
const next = text[index + 1];
|
|
104
|
+
if (character === "/" && next === "/") {
|
|
105
|
+
const lineEndIndex = text.indexOf("\n", index);
|
|
106
|
+
blank(index, lineEndIndex === -1 ? text.length : lineEndIndex);
|
|
107
|
+
index = lineEndIndex === -1 ? text.length : lineEndIndex;
|
|
108
|
+
} else if (character === "/" && next === "*") {
|
|
109
|
+
const closeIndex = text.indexOf("*/", index + 2);
|
|
110
|
+
blank(index, closeIndex === -1 ? text.length : closeIndex + 2);
|
|
111
|
+
index = closeIndex === -1 ? text.length : closeIndex + 1;
|
|
112
|
+
} else if (character === '"' || character === "'" || character === "`") {
|
|
113
|
+
// A string: skip to its end, so a // or /* in it isn't a comment.
|
|
114
|
+
let closeIndex = index + 1;
|
|
115
|
+
while (closeIndex < text.length && text[closeIndex] !== character && (character === "`" || text[closeIndex] !== "\n")) closeIndex += text[closeIndex] === "\\" ? 2 : 1;
|
|
116
|
+
index = closeIndex;
|
|
117
|
+
lastSignificant = character;
|
|
118
|
+
} else if (character === "/" && (lastSignificant === "" || "(,=:[!&|?{};+-*%<>~^".includes(lastSignificant))) {
|
|
119
|
+
// A regular expression: skip to its closing slash, outside a [class].
|
|
120
|
+
let closeIndex = index + 1;
|
|
121
|
+
let isInClass = false;
|
|
122
|
+
while (closeIndex < text.length && text[closeIndex] !== "\n" && (isInClass || text[closeIndex] !== "/")) {
|
|
123
|
+
if (text[closeIndex] === "\\") closeIndex++;
|
|
124
|
+
else if (text[closeIndex] === "[") isInClass = true;
|
|
125
|
+
else if (text[closeIndex] === "]") isInClass = false;
|
|
126
|
+
closeIndex++;
|
|
127
|
+
}
|
|
128
|
+
index = closeIndex;
|
|
129
|
+
lastSignificant = "/";
|
|
130
|
+
} else if (!/\s/.test(character)) {
|
|
131
|
+
lastSignificant = character;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out.join("");
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** A page's or module's text with its comments blanked, HTML's and its scripts' JavaScript ones, at the same indexes. */
|
|
138
|
+
function blankComments(text, fileKind) {
|
|
139
|
+
if (fileKind === "module") return blankScriptComments(text);
|
|
140
|
+
const spaces = (part) => part.replace(/[^\n]/g, " ");
|
|
141
|
+
return text
|
|
142
|
+
.replace(/<!--[\s\S]*?(?:-->|$)/g, spaces)
|
|
143
|
+
.replace(/(<script\b[^>]*>)([\s\S]*?)(<\/script\s*>|$)/gi, (...[, openTag, body, closeTag]) => openTag + blankScriptComments(body) + closeTag);
|
|
87
144
|
}
|
|
88
145
|
|
|
89
146
|
/** An attribute's value in a tag, with where the value starts and ends in the whole text. */
|
|
@@ -231,7 +288,17 @@ const FEATURES = [
|
|
|
231
288
|
{ pattern: /\bnavigator\.hid\b/, name: "HID devices (navigator.hid)" },
|
|
232
289
|
];
|
|
233
290
|
const DIALOG_PATTERN = /(?<![\w$.])(?<!function\s+)(?:window\.)?(alert|confirm|prompt)\s*\(/;
|
|
234
|
-
|
|
291
|
+
/** Only when a load fails, like Unity's page template's .catch((message) => { alert(message); }): the game itself works. */
|
|
292
|
+
const FAILURE_HANDLER_BEFORE_PATTERN = /\.catch\(\s*(?:function\s*)?\(?\s*[\w$]*\s*\)?\s*(?:=>\s*)?\{\s*$/;
|
|
293
|
+
/** The first dialog the code opens, besides one only a failed load opens. */
|
|
294
|
+
function dialogOf(code) {
|
|
295
|
+
const pattern = new RegExp(DIALOG_PATTERN.source, "g");
|
|
296
|
+
for (const match of code.matchAll(pattern)) {
|
|
297
|
+
if (!FAILURE_HANDLER_BEFORE_PATTERN.test(code.slice(Math.max(0, match.index - 80), match.index))) return match;
|
|
298
|
+
}
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
const THREADS_PATTERNS = [/\bGODOT_THREADS_ENABLED\s*=\s*true\b/, /\bENVIRONMENT_IS_PTHREAD\b/, /coi-serviceworker/i, /\b__wbindgen_thread_destroy\b/, /\binitThreadPool\b/];
|
|
235
302
|
|
|
236
303
|
/**
|
|
237
304
|
* Checks a game's files: [{ path, sizeBytes, contentText }], contentText a string for the checked text files
|
|
@@ -279,7 +346,7 @@ export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}
|
|
|
279
346
|
const extension = extensionOf(path);
|
|
280
347
|
if (PRECOMPRESSED_EXTENSIONS.includes(extension)) {
|
|
281
348
|
const unpackedPath = path.slice(0, -extension.length - 1);
|
|
282
|
-
add("precompressed_file", { message: `${path} is pre-compressed.`, path, fix: `zaisma upload unpacks it into ${unpackedPath} and points the game at it; Zaisma compresses files for players itself. With upload_files or the API, turn off compression in the engine's build settings (Unity: Publishing Settings, Compression Format: Disabled) and build again.` });
|
|
349
|
+
add("precompressed_file", { message: `${path} is pre-compressed.`, path, fix: `zaisma upload unpacks it into ${unpackedPath} and points the game at it; Zaisma compresses files for players itself, even Unity and Godot data files. With upload_files or the API, turn off compression in the engine's build settings (Unity: Publishing Settings, Compression Format: Disabled) and build again.` });
|
|
283
350
|
} else if (!GAME_FILE_EXTENSIONS.includes(extension)) {
|
|
284
351
|
add("file_type_not_allowed", { message: `${path}: .${extension || "(no extension)"} files aren't allowed in games.`, path, fix: `Leave it out of the game's folder. Games can have: ${GAME_FILE_EXTENSIONS.join(", ")}.` });
|
|
285
352
|
}
|
|
@@ -296,7 +363,7 @@ export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}
|
|
|
296
363
|
if (files.length > UPLOAD_LIMITS.maximumFileCount) add("too_many_files", { message: `The game has ${files.length} files; the limit is 20,000.`, fix: "Leave out files the game doesn't load, or pack small ones together (like sprites into an atlas)." });
|
|
297
364
|
if (sizeBytes > UPLOAD_LIMITS.maximumGameSizeBytes) add("game_too_big", { message: `The game is ${(sizeBytes / 1024 ** 3).toFixed(1)} GB; the limit is 2 GB.`, fix: "Leave out files the game doesn't load, or ask Zaisma (message_zaisma, kind gap) for more." });
|
|
298
365
|
|
|
299
|
-
const threadsFix = "Export without threads: in Godot 4.3 or newer, turn Thread Support off in the Web export preset (Godot 4.0 to 4.2 always use threads, so update); in Unity, leave Native C/C++ Multithreading off in Player Settings, the default. Games on Zaisma can't have SharedArrayBuffer.";
|
|
366
|
+
const threadsFix = "Export without threads: in Godot 4.3 or newer, turn Thread Support off in the Web export preset (Godot 4.0 to 4.2 always use threads, so update); in Unity, leave Native C/C++ Multithreading off in Player Settings, the default; in Rust, build without the `atomics` target feature (the default) and without wasm-bindgen-rayon. Games on Zaisma can't have SharedArrayBuffer.";
|
|
300
367
|
let hasThreadsProblem = false;
|
|
301
368
|
const addThreads = (message, quote, path) => {
|
|
302
369
|
if (!hasThreadsProblem) add("needs_threads", { message, quote, path, fix: threadsFix });
|
|
@@ -339,9 +406,11 @@ export function checkGameFiles(files, { slug = null, ownServerUrls = null } = {}
|
|
|
339
406
|
if (threadsMatch) addThreads(`${path} is from a build that needs threads (SharedArrayBuffer), which games on Zaisma don't have, so it won't start.`, lineAround(text, threadsMatch.index), path);
|
|
340
407
|
|
|
341
408
|
if (isEngineOutput(path, lowercasePaths)) continue;
|
|
342
|
-
|
|
409
|
+
// What the code does, not what its comments say: a library's docs mention alert() and window.open.
|
|
410
|
+
const code = blankComments(text, fileKind);
|
|
411
|
+
const found = (pattern) => pattern.exec(code);
|
|
343
412
|
let match;
|
|
344
|
-
if ((match =
|
|
413
|
+
if ((match = dialogOf(code))) add("browser_dialog", { message: `${path} uses ${match[1]}(), which never shows in the game's frame on Zaisma.`, path, quote: lineAround(text, match.index), fix: "Show the message in the game itself, like on the canvas or in an element over it." });
|
|
345
414
|
if (fileKind === "html" && (match = found(/<form\b[^>]*>/i))) add("form_submission", { message: `${path} has a form, which never submits in the game's frame on Zaisma.`, path, quote: lineAround(text, match.index), fix: "Handle the button's click (or the Enter key) in script instead of submitting the form." });
|
|
346
415
|
if ((match = (fileKind === "html" && found(/<a\b[^>]*\sdownload\b[^>]*>/i)) || found(/\.download\s*=(?!=)/))) add("file_download", { message: `${path} downloads a file, which the game's frame on Zaisma doesn't allow.`, path, quote: lineAround(text, match.index), fix: "Keep it in the game (its browser saves, which Zaisma's cloud saves mirror), or copy it to the clipboard." });
|
|
347
416
|
if ((match = (fileKind === "html" && (found(/<a\b[^>]*\shref\s*=\s*["']?(?:https?:)?\/\/[^>]*>/i) || found(/<a\b[^>]*\starget\s*=\s*["']?_blank[^>]*>/i))) || found(/\bwindow\.open\s*\(/))) {
|