@akashic/engine-files-reftest-helper 1.3.0-beta.0 → 1.3.0-beta.1
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/lib/index.d.ts +11 -1
- package/lib/index.js +67 -6
- package/package.json +1 -1
- package/lib/command.d.ts +0 -11
- package/lib/command.js +0 -64
package/lib/index.d.ts
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import type { CustomWindow, ConsoleApiParameter, OutputType } from "./types";
|
|
2
|
+
/** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
|
|
3
|
+
export declare const SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
|
|
4
|
+
/** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
|
|
5
|
+
export declare const END_NOTIFICATION_MESSAGE = "engine-files-reftest:finish";
|
|
6
|
+
export declare function postConsole(param: ConsoleApiParameter): Promise<Response>;
|
|
7
|
+
export declare function screenshot(fileName: string, base64: string): Promise<Response | void>;
|
|
8
|
+
export declare function finish(): Promise<Response | void>;
|
|
9
|
+
export declare function getImageDataUrlFromCanvas(): string;
|
|
10
|
+
export declare function getCustomWindow(): CustomWindow;
|
|
11
|
+
export declare function setPropsToCustomWindow(outputType: OutputType, consoleApiUrl: string): void;
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
3
|
+
exports.END_NOTIFICATION_MESSAGE = exports.SCREENSHOT_NOTIFICATION_MESSAGE = void 0;
|
|
4
|
+
exports.postConsole = postConsole;
|
|
5
|
+
exports.screenshot = screenshot;
|
|
6
|
+
exports.finish = finish;
|
|
7
|
+
exports.getImageDataUrlFromCanvas = getImageDataUrlFromCanvas;
|
|
8
|
+
exports.getCustomWindow = getCustomWindow;
|
|
9
|
+
exports.setPropsToCustomWindow = setPropsToCustomWindow;
|
|
10
|
+
// TODO 以下2つの定数は大規模化したら別リポジトリに切り出す
|
|
11
|
+
/** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
|
|
12
|
+
exports.SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
|
|
13
|
+
/** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
|
|
14
|
+
exports.END_NOTIFICATION_MESSAGE = "engine-files-reftest:finish";
|
|
15
|
+
function postConsole(param) {
|
|
16
|
+
const windowAsCustom = getCustomWindow();
|
|
17
|
+
const headers = {
|
|
18
|
+
"Accept": "application/json",
|
|
19
|
+
"Content-Type": "application/json; charset=utf-8"
|
|
20
|
+
};
|
|
21
|
+
return fetch(windowAsCustom.consoleApiUrl, { method: "POST", headers, body: JSON.stringify(param) });
|
|
22
|
+
}
|
|
23
|
+
function screenshot(fileName, base64) {
|
|
24
|
+
const windowAsCustom = getCustomWindow();
|
|
25
|
+
switch (windowAsCustom.outputType) {
|
|
26
|
+
case "post":
|
|
27
|
+
return postConsole({ commandName: "screenshot", screenshotData: { fileName, base64 } });
|
|
28
|
+
case "console":
|
|
29
|
+
console.log(exports.SCREENSHOT_NOTIFICATION_MESSAGE, fileName, base64);
|
|
30
|
+
return Promise.resolve();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function finish() {
|
|
34
|
+
const windowAsCustom = getCustomWindow();
|
|
35
|
+
switch (windowAsCustom.outputType) {
|
|
36
|
+
case "post":
|
|
37
|
+
return postConsole({ commandName: "finish" });
|
|
38
|
+
case "console":
|
|
39
|
+
console.log(exports.END_NOTIFICATION_MESSAGE);
|
|
40
|
+
return Promise.resolve();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function getImageDataUrlFromCanvas() {
|
|
44
|
+
getCustomWindow(); // window の存在確認 のために呼び出す
|
|
45
|
+
g.game.render(); // 描画がスキップされてしまうことがあるので、スクリーンショット取得前に現フレームでの描画を行う
|
|
46
|
+
const canvasElements = window.document.getElementsByTagName("canvas");
|
|
47
|
+
const imageUrl = canvasElements[0].toDataURL("image/png");
|
|
48
|
+
const data = imageUrl.match(/^data:image\/png;base64,(.+)$/);
|
|
49
|
+
if (!data) {
|
|
50
|
+
throw new Error("Invalid image data URL");
|
|
51
|
+
}
|
|
52
|
+
return data[1];
|
|
53
|
+
}
|
|
54
|
+
function getCustomWindow() {
|
|
55
|
+
if (typeof window === "undefined") {
|
|
56
|
+
throw new Error("window is undefined");
|
|
57
|
+
}
|
|
58
|
+
return window;
|
|
59
|
+
}
|
|
60
|
+
function setPropsToCustomWindow(outputType, consoleApiUrl) {
|
|
61
|
+
const windowAsCustom = getCustomWindow();
|
|
62
|
+
windowAsCustom.outputType = outputType;
|
|
63
|
+
windowAsCustom.consoleApiUrl = consoleApiUrl;
|
|
64
|
+
}
|
|
4
65
|
try {
|
|
5
|
-
|
|
66
|
+
setPropsToCustomWindow("console", "");
|
|
6
67
|
}
|
|
7
68
|
catch (e) {
|
|
8
69
|
// window がない環境で必ずエラーになるので無視する
|
|
@@ -17,7 +78,7 @@ if (typeof g !== "undefined") {
|
|
|
17
78
|
case "init":
|
|
18
79
|
const option = eventData.command.options;
|
|
19
80
|
try {
|
|
20
|
-
|
|
81
|
+
setPropsToCustomWindow(option.outputType, option.outputUrl);
|
|
21
82
|
}
|
|
22
83
|
catch (e) {
|
|
23
84
|
// window がない環境で必ずエラーになるので無視する
|
|
@@ -25,8 +86,8 @@ if (typeof g !== "undefined") {
|
|
|
25
86
|
break;
|
|
26
87
|
case "screenshot":
|
|
27
88
|
try {
|
|
28
|
-
const url =
|
|
29
|
-
screenshotPromises.push(
|
|
89
|
+
const url = getImageDataUrlFromCanvas();
|
|
90
|
+
screenshotPromises.push(screenshot(eventData.command.options.fileName, url));
|
|
30
91
|
}
|
|
31
92
|
catch (e) {
|
|
32
93
|
// window がない環境で必ずエラーになるので無視する
|
|
@@ -34,7 +95,7 @@ if (typeof g !== "undefined") {
|
|
|
34
95
|
break;
|
|
35
96
|
case "finish":
|
|
36
97
|
// スクリーンショット送信よりも finish() の通知が先に完了してしまうと、実行が中断されてスクリーンショットが取れない場合がある。それを避けるために完了を待ってから finish() する
|
|
37
|
-
Promise.all(screenshotPromises).then(() =>
|
|
98
|
+
Promise.all(screenshotPromises).then(() => finish());
|
|
38
99
|
break;
|
|
39
100
|
default:
|
|
40
101
|
throw new Error(`${eventData.command.name} is undefined.`);
|
package/package.json
CHANGED
package/lib/command.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { ConsoleApiParameter, CustomWindow, OutputType } from "./types";
|
|
2
|
-
/** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
|
|
3
|
-
export declare const SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
|
|
4
|
-
/** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
|
|
5
|
-
export declare const END_NOTIFICATION_MESSAGE = "engine-files-reftest:finish";
|
|
6
|
-
export declare function postConsole(param: ConsoleApiParameter): Promise<Response>;
|
|
7
|
-
export declare function screenshot(fileName: string, base64: string): Promise<Response | void>;
|
|
8
|
-
export declare function finish(): Promise<Response | void>;
|
|
9
|
-
export declare function getImageDataUrlFromCanvas(): string;
|
|
10
|
-
export declare function getCustomWindow(): CustomWindow;
|
|
11
|
-
export declare function setPropsToCustomWindow(outputType: OutputType, consoleApiUrl: string): void;
|
package/lib/command.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.END_NOTIFICATION_MESSAGE = exports.SCREENSHOT_NOTIFICATION_MESSAGE = void 0;
|
|
4
|
-
exports.postConsole = postConsole;
|
|
5
|
-
exports.screenshot = screenshot;
|
|
6
|
-
exports.finish = finish;
|
|
7
|
-
exports.getImageDataUrlFromCanvas = getImageDataUrlFromCanvas;
|
|
8
|
-
exports.getCustomWindow = getCustomWindow;
|
|
9
|
-
exports.setPropsToCustomWindow = setPropsToCustomWindow;
|
|
10
|
-
// TODO 以下2つの定数は大規模化したら別リポジトリに切り出す
|
|
11
|
-
/** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
|
|
12
|
-
exports.SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
|
|
13
|
-
/** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
|
|
14
|
-
exports.END_NOTIFICATION_MESSAGE = "engine-files-reftest:finish";
|
|
15
|
-
function postConsole(param) {
|
|
16
|
-
const windowAsCustom = getCustomWindow();
|
|
17
|
-
const headers = {
|
|
18
|
-
"Accept": "application/json",
|
|
19
|
-
"Content-Type": "application/json; charset=utf-8"
|
|
20
|
-
};
|
|
21
|
-
return fetch(windowAsCustom.consoleApiUrl, { method: "POST", headers, body: JSON.stringify(param) });
|
|
22
|
-
}
|
|
23
|
-
function screenshot(fileName, base64) {
|
|
24
|
-
const windowAsCustom = getCustomWindow();
|
|
25
|
-
switch (windowAsCustom.outputType) {
|
|
26
|
-
case "post":
|
|
27
|
-
return postConsole({ commandName: "screenshot", screenshotData: { fileName, base64 } });
|
|
28
|
-
case "console":
|
|
29
|
-
console.log(exports.SCREENSHOT_NOTIFICATION_MESSAGE, fileName, base64);
|
|
30
|
-
return Promise.resolve();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
function finish() {
|
|
34
|
-
const windowAsCustom = getCustomWindow();
|
|
35
|
-
switch (windowAsCustom.outputType) {
|
|
36
|
-
case "post":
|
|
37
|
-
return postConsole({ commandName: "finish" });
|
|
38
|
-
case "console":
|
|
39
|
-
console.log(exports.END_NOTIFICATION_MESSAGE);
|
|
40
|
-
return Promise.resolve();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
function getImageDataUrlFromCanvas() {
|
|
44
|
-
getCustomWindow(); // window の存在確認 のために呼び出す
|
|
45
|
-
g.game.render(); // 描画がスキップされてしまうことがあるので、スクリーンショット取得前に現フレームでの描画を行う
|
|
46
|
-
const canvasElements = window.document.getElementsByTagName("canvas");
|
|
47
|
-
const imageUrl = canvasElements[0].toDataURL("image/png");
|
|
48
|
-
const data = imageUrl.match(/^data:image\/png;base64,(.+)$/);
|
|
49
|
-
if (!data) {
|
|
50
|
-
throw new Error("Invalid image data URL");
|
|
51
|
-
}
|
|
52
|
-
return data[1];
|
|
53
|
-
}
|
|
54
|
-
function getCustomWindow() {
|
|
55
|
-
if (typeof window === "undefined") {
|
|
56
|
-
throw new Error("window is undefined");
|
|
57
|
-
}
|
|
58
|
-
return window;
|
|
59
|
-
}
|
|
60
|
-
function setPropsToCustomWindow(outputType, consoleApiUrl) {
|
|
61
|
-
const windowAsCustom = getCustomWindow();
|
|
62
|
-
windowAsCustom.outputType = outputType;
|
|
63
|
-
windowAsCustom.consoleApiUrl = consoleApiUrl;
|
|
64
|
-
}
|