@akashic/engine-files-reftest-helper 1.3.0-beta.1 → 1.3.0-beta.3

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 CHANGED
@@ -1,4 +1,12 @@
1
- import type { CustomWindow, ConsoleApiParameter, OutputType } from "./types";
1
+ import type { ConsoleApiParameter, OutputType } from "./types";
2
+ export interface ReftestHelperBridge {
3
+ outputType: OutputType;
4
+ consoleApiUrl: string;
5
+ postConsole: (param: ConsoleApiParameter) => Promise<Response>;
6
+ screenshot: (fileName: string, base64: string) => Promise<Response | void>;
7
+ finish: () => Promise<Response | void>;
8
+ getImageDataUrlFromCanvas: () => string;
9
+ }
2
10
  /** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
3
11
  export declare const SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
4
12
  /** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
@@ -7,5 +15,4 @@ export declare function postConsole(param: ConsoleApiParameter): Promise<Respons
7
15
  export declare function screenshot(fileName: string, base64: string): Promise<Response | void>;
8
16
  export declare function finish(): Promise<Response | void>;
9
17
  export declare function getImageDataUrlFromCanvas(): string;
10
- export declare function getCustomWindow(): CustomWindow;
11
- export declare function setPropsToCustomWindow(outputType: OutputType, consoleApiUrl: string): void;
18
+ export declare function getReftestHelperBridge(): ReftestHelperBridge;
package/lib/index.js CHANGED
@@ -5,24 +5,23 @@ exports.postConsole = postConsole;
5
5
  exports.screenshot = screenshot;
6
6
  exports.finish = finish;
7
7
  exports.getImageDataUrlFromCanvas = getImageDataUrlFromCanvas;
8
- exports.getCustomWindow = getCustomWindow;
9
- exports.setPropsToCustomWindow = setPropsToCustomWindow;
8
+ exports.getReftestHelperBridge = getReftestHelperBridge;
10
9
  // TODO 以下2つの定数は大規模化したら別リポジトリに切り出す
11
10
  /** engine-files-reftest側にスクリーンショットの保存を要求するためのメッセージ */
12
11
  exports.SCREENSHOT_NOTIFICATION_MESSAGE = "engine-files-reftest:image";
13
12
  /** engine-files-reftest側にコンテンツ実行終了を要求するためのメッセージ */
14
13
  exports.END_NOTIFICATION_MESSAGE = "engine-files-reftest:finish";
15
14
  function postConsole(param) {
16
- const windowAsCustom = getCustomWindow();
15
+ const reftest = getReftestHelperBridge();
17
16
  const headers = {
18
17
  "Accept": "application/json",
19
18
  "Content-Type": "application/json; charset=utf-8"
20
19
  };
21
- return fetch(windowAsCustom.consoleApiUrl, { method: "POST", headers, body: JSON.stringify(param) });
20
+ return fetch(reftest.consoleApiUrl, { method: "POST", headers, body: JSON.stringify(param) });
22
21
  }
23
22
  function screenshot(fileName, base64) {
24
- const windowAsCustom = getCustomWindow();
25
- switch (windowAsCustom.outputType) {
23
+ const reftest = getReftestHelperBridge();
24
+ switch (reftest.outputType) {
26
25
  case "post":
27
26
  return postConsole({ commandName: "screenshot", screenshotData: { fileName, base64 } });
28
27
  case "console":
@@ -31,8 +30,8 @@ function screenshot(fileName, base64) {
31
30
  }
32
31
  }
33
32
  function finish() {
34
- const windowAsCustom = getCustomWindow();
35
- switch (windowAsCustom.outputType) {
33
+ const reftest = getReftestHelperBridge();
34
+ switch (reftest.outputType) {
36
35
  case "post":
37
36
  return postConsole({ commandName: "finish" });
38
37
  case "console":
@@ -41,7 +40,12 @@ function finish() {
41
40
  }
42
41
  }
43
42
  function getImageDataUrlFromCanvas() {
44
- getCustomWindow(); // window の存在確認 のために呼び出す
43
+ if (typeof window === "undefined" || !window.document) {
44
+ throw new Error("window or document is undefined");
45
+ }
46
+ if (typeof g === "undefined" || !g.game) {
47
+ throw new Error("g.game is undefined");
48
+ }
45
49
  g.game.render(); // 描画がスキップされてしまうことがあるので、スクリーンショット取得前に現フレームでの描画を行う
46
50
  const canvasElements = window.document.getElementsByTagName("canvas");
47
51
  const imageUrl = canvasElements[0].toDataURL("image/png");
@@ -51,24 +55,22 @@ function getImageDataUrlFromCanvas() {
51
55
  }
52
56
  return data[1];
53
57
  }
54
- function getCustomWindow() {
55
- if (typeof window === "undefined") {
56
- throw new Error("window is undefined");
58
+ function getReftestHelperBridge() {
59
+ if (typeof g === "undefined" || !g.game || !g.game.vars || !g.game.vars.reftest) {
60
+ throw new Error("g.game.vars.reftest is undefined");
57
61
  }
58
- return window;
59
- }
60
- function setPropsToCustomWindow(outputType, consoleApiUrl) {
61
- const windowAsCustom = getCustomWindow();
62
- windowAsCustom.outputType = outputType;
63
- windowAsCustom.consoleApiUrl = consoleApiUrl;
64
- }
65
- try {
66
- setPropsToCustomWindow("console", "");
67
- }
68
- catch (e) {
69
- // window がない環境で必ずエラーになるので無視する
62
+ return g.game.vars.reftest;
70
63
  }
71
64
  if (typeof g !== "undefined") {
65
+ // reftest 関連のプロパティを g.game.vars にセットする
66
+ g.game.vars.reftest = {
67
+ outputType: "console",
68
+ consoleApiUrl: "",
69
+ postConsole,
70
+ screenshot,
71
+ finish,
72
+ getImageDataUrlFromCanvas
73
+ };
72
74
  const screenshotPromises = [];
73
75
  const handleScenario = (msg) => {
74
76
  var _a;
@@ -77,12 +79,8 @@ if (typeof g !== "undefined") {
77
79
  switch (eventData.command.name) {
78
80
  case "init":
79
81
  const option = eventData.command.options;
80
- try {
81
- setPropsToCustomWindow(option.outputType, option.outputUrl);
82
- }
83
- catch (e) {
84
- // window がない環境で必ずエラーになるので無視する
85
- }
82
+ g.game.vars.reftest.outputType = option.outputType;
83
+ g.game.vars.reftest.consoleApiUrl = option.outputUrl;
86
84
  break;
87
85
  case "screenshot":
88
86
  try {
@@ -3,4 +3,3 @@ export * from "./ConsoleApiParameter";
3
3
  export * from "./InitCommandOption";
4
4
  export * from "./OutputType";
5
5
  export * from "./ScreenshotData";
6
- export * from "./CustomWindow";
@@ -19,4 +19,3 @@ __exportStar(require("./ConsoleApiParameter"), exports);
19
19
  __exportStar(require("./InitCommandOption"), exports);
20
20
  __exportStar(require("./OutputType"), exports);
21
21
  __exportStar(require("./ScreenshotData"), exports);
22
- __exportStar(require("./CustomWindow"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akashic/engine-files-reftest-helper",
3
- "version": "1.3.0-beta.1",
3
+ "version": "1.3.0-beta.3",
4
4
  "description": "engine-files-reftestでコンテンツを自動実行するためのライブラリ",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -1,5 +0,0 @@
1
- import type { OutputType } from ".";
2
- export interface CustomWindow extends Window {
3
- consoleApiUrl: string;
4
- outputType: OutputType;
5
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });