@akashic/headless-driver 2.8.3 → 2.9.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.
Files changed (32) hide show
  1. package/lib/runner/Platform.d.ts +4 -4
  2. package/lib/runner/Platform.js +2 -1
  3. package/lib/runner/Runner.d.ts +3 -3
  4. package/lib/runner/RunnerManager.d.ts +2 -1
  5. package/lib/runner/RunnerManager.js +4 -4
  6. package/lib/runner/types.d.ts +2 -0
  7. package/lib/runner/v1/RunnerV1.js +1 -1
  8. package/lib/runner/v1/platform/ResourceFactory.d.ts +2 -1
  9. package/lib/runner/v1/platform/assets/NodeScriptAsset.d.ts +2 -1
  10. package/lib/runner/v1/platform/assets/NodeScriptAsset.js +1 -1
  11. package/lib/runner/v1/platform/assets/NodeTextAsset.d.ts +2 -1
  12. package/lib/runner/v1/platform/assets/NodeTextAsset.js +1 -1
  13. package/lib/runner/v2/RunnerV2.js +1 -1
  14. package/lib/runner/v2/platform/ResourceFactory.d.ts +2 -1
  15. package/lib/runner/v2/platform/assets/NodeScriptAsset.d.ts +2 -1
  16. package/lib/runner/v2/platform/assets/NodeScriptAsset.js +1 -1
  17. package/lib/runner/v2/platform/assets/NodeTextAsset.d.ts +2 -1
  18. package/lib/runner/v2/platform/assets/NodeTextAsset.js +1 -1
  19. package/lib/runner/v3/RunnerV3.js +1 -1
  20. package/lib/runner/v3/platform/NodeCanvasResourceFactory.d.ts +3 -1
  21. package/lib/runner/v3/platform/NodeCanvasResourceFactory.js +8 -0
  22. package/lib/runner/v3/platform/NullResourceFactory.d.ts +3 -1
  23. package/lib/runner/v3/platform/NullResourceFactory.js +8 -0
  24. package/lib/runner/v3/platform/assets/NodeBinaryAsset.d.ts +15 -0
  25. package/lib/runner/v3/platform/assets/NodeBinaryAsset.js +45 -0
  26. package/lib/runner/v3/platform/assets/NodeScriptAsset.d.ts +4 -3
  27. package/lib/runner/v3/platform/assets/NodeScriptAsset.js +14 -4
  28. package/lib/runner/v3/platform/assets/NodeTextAsset.d.ts +2 -1
  29. package/lib/runner/v3/platform/assets/NodeTextAsset.js +2 -1
  30. package/lib/utils.d.ts +5 -2
  31. package/lib/utils.js +23 -13
  32. package/package.json +6 -6
@@ -1,5 +1,5 @@
1
1
  import type { AMFlow } from "@akashic/amflow";
2
- import type { RunnerRenderingMode } from "./types";
2
+ import type { RunnerLoadFileHandler, RunnerRenderingMode } from "./types";
3
3
  export interface PlatformParameters {
4
4
  assetBaseUrl: string;
5
5
  configurationBaseUrl?: string;
@@ -8,7 +8,7 @@ export interface PlatformParameters {
8
8
  trusted?: boolean;
9
9
  renderingMode?: RunnerRenderingMode;
10
10
  errorHandler: (err: any) => void;
11
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
11
+ loadFileHandler: RunnerLoadFileHandler;
12
12
  }
13
13
  export declare abstract class Platform {
14
14
  amflow: AMFlow;
@@ -18,8 +18,8 @@ export declare abstract class Platform {
18
18
  renderingMode: RunnerRenderingMode;
19
19
  protected sendToExternalHandler: (data: any) => void;
20
20
  protected errorHandler: (err: any) => void;
21
- protected loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
21
+ protected loadFileHandler: RunnerLoadFileHandler;
22
22
  constructor(param: PlatformParameters);
23
23
  sendToExternal(_playId: string, data: any): void;
24
- loadGameConfiguration: (url: string, callback: (err: Error | null, data?: string) => void) => void;
24
+ loadGameConfiguration: (url: string, callback: (err: Error | null, data?: string | Uint8Array) => void) => void;
25
25
  }
@@ -6,7 +6,7 @@ class Platform {
6
6
  var _a;
7
7
  // eslint-disable-next-line @typescript-eslint/typedef
8
8
  this.loadGameConfiguration = (url, callback) => {
9
- this.loadFileHandler(url, (err, str) => {
9
+ this.loadFileHandler(url, "utf-8", (err, str) => {
10
10
  if (err) {
11
11
  callback(err);
12
12
  }
@@ -14,6 +14,7 @@ class Platform {
14
14
  callback(new Error("Platform#loadGameConfiguration(): No data received"));
15
15
  }
16
16
  else {
17
+ // FIXME: as の回避
17
18
  callback(null, JSON.parse(str));
18
19
  }
19
20
  });
@@ -1,7 +1,7 @@
1
1
  import type { AMFlow } from "@akashic/amflow";
2
2
  import { Trigger } from "@akashic/trigger";
3
3
  import type { Platform } from "./Platform";
4
- import type { RunnerAdvanceConditionFunc, RunnerExecutionMode, RunnerPlayer, RunnerPointEvent, RunnerRenderingMode } from "./types";
4
+ import type { RunnerAdvanceConditionFunc, RunnerExecutionMode, RunnerLoadFileHandler, RunnerPlayer, RunnerPointEvent, RunnerRenderingMode } from "./types";
5
5
  export interface RunnerParameters {
6
6
  contentUrl: string;
7
7
  assetBaseUrl: string;
@@ -14,7 +14,7 @@ export interface RunnerParameters {
14
14
  executionMode: RunnerExecutionMode;
15
15
  trusted?: boolean;
16
16
  renderingMode?: RunnerRenderingMode;
17
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
17
+ loadFileHandler: RunnerLoadFileHandler;
18
18
  external?: {
19
19
  [key: string]: string;
20
20
  };
@@ -52,7 +52,7 @@ export declare abstract class Runner {
52
52
  get executionMode(): RunnerExecutionMode;
53
53
  get trusted(): boolean;
54
54
  get renderingMode(): RunnerRenderingMode;
55
- get loadFileHandler(): (url: string, callback: (err: Error | null, data?: string) => void) => void;
55
+ get loadFileHandler(): RunnerLoadFileHandler;
56
56
  get gameArgs(): any;
57
57
  get player(): RunnerPlayer | undefined;
58
58
  get external(): {
@@ -1,6 +1,7 @@
1
1
  import { NodeVM } from "vm2";
2
2
  import type { AMFlowClient } from "../play/amflow/AMFlowClient";
3
3
  import type { PlayManager } from "../play/PlayManager";
4
+ import type { EncodingType } from "../utils";
4
5
  import type { RunnerStartParameters } from "./Runner";
5
6
  import type { RunnerExecutionMode, RunnerPlayer, RunnerRenderingMode } from "./types";
6
7
  import type { RunnerV1, RunnerV1Game } from "./v1";
@@ -114,7 +115,7 @@ export declare class RunnerManager {
114
115
  protected resolveContent(contentUrl: string): Promise<EngineConfiguration>;
115
116
  protected resolveGameConfiguration(gameJsonUrl: string): Promise<GameConfiguration>;
116
117
  protected loadJSON<T>(contentUrl: string): Promise<T>;
117
- protected createLoadFileHandler(allowedUrls: (string | RegExp)[] | null): (url: string, callback: (err: Error | null, data?: string) => void) => void;
118
+ protected createLoadFileHandler(allowedUrls: (string | RegExp)[] | null): (url: string, encoding: EncodingType, callback: (err: Error | null, data?: string | Uint8Array) => void) => void;
118
119
  protected createVm(trusted?: boolean): NodeVM;
119
120
  }
120
121
  export {};
@@ -14,9 +14,9 @@ const fs = require("fs");
14
14
  const path = require("path");
15
15
  const url = require("url");
16
16
  const vm2_1 = require("vm2");
17
- const ExecVmScriptV3 = require("..//ExecuteVmScriptV3");
18
17
  const ExecVmScriptV1 = require("../ExecuteVmScriptV1");
19
18
  const ExecVmScriptV2 = require("../ExecuteVmScriptV2");
19
+ const ExecVmScriptV3 = require("../ExecuteVmScriptV3");
20
20
  const Logger_1 = require("../Logger");
21
21
  const utils_1 = require("../utils");
22
22
  const requireEngineFiles_1 = require("./v3/requireEngineFiles");
@@ -257,11 +257,11 @@ class RunnerManager {
257
257
  }
258
258
  loadJSON(contentUrl) {
259
259
  return __awaiter(this, void 0, void 0, function* () {
260
- return (0, utils_1.loadFile)(contentUrl).then((text) => JSON.parse(text));
260
+ return (0, utils_1.loadFile)(contentUrl, "utf-8").then((text) => JSON.parse(text));
261
261
  });
262
262
  }
263
263
  createLoadFileHandler(allowedUrls) {
264
- return (url, callback) => {
264
+ return (url, encoding, callback) => {
265
265
  if (allowedUrls != null) {
266
266
  const isAllowedUrl = allowedUrls.some((u) => {
267
267
  if (typeof u === "string") {
@@ -276,7 +276,7 @@ class RunnerManager {
276
276
  return void callback(new Error(`Not allowed to read this URL. ${url}`));
277
277
  }
278
278
  }
279
- (0, utils_1.loadFile)(url)
279
+ (0, utils_1.loadFile)(url, encoding)
280
280
  .then((text) => {
281
281
  callback(null, text);
282
282
  })
@@ -1,3 +1,4 @@
1
+ import type { EncodingType } from "../utils";
1
2
  export type RunnerExecutionMode = "active" | "passive";
2
3
  export type RunnerRenderingMode = "none" | "canvas";
3
4
  export type RunnerAdvanceConditionFunc = () => boolean;
@@ -13,3 +14,4 @@ export interface RunnerPlayer {
13
14
  id: string;
14
15
  name: string;
15
16
  }
17
+ export type RunnerLoadFileHandler = (url: string, encoding: EncodingType, callback: (err: Error | null, data?: string | Uint8Array) => void) => void;
@@ -175,7 +175,7 @@ class RunnerV1 extends Runner_1.Runner {
175
175
  renderingMode: this.renderingMode,
176
176
  sendToExternalHandler: (data) => this.onSendedToExternal(data),
177
177
  errorHandler: (e) => this.onError(e),
178
- loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
178
+ loadFileHandler: (url, encoding, callback) => this.loadFileHandler(url, encoding, callback)
179
179
  });
180
180
  this.pause();
181
181
  const driver = new engine_files_v1_1.gameDriver.GameDriver({
@@ -1,6 +1,7 @@
1
1
  import { akashicEngine as g } from "engine-files-v1";
2
+ import type { RunnerLoadFileHandler } from "../../types";
2
3
  export interface ResourceFactoryParameters {
3
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
4
+ loadFileHandler: RunnerLoadFileHandler;
4
5
  errorHandler: (err: any) => void;
5
6
  }
6
7
  export declare class ResourceFactory extends g.ResourceFactory {
@@ -1,9 +1,10 @@
1
1
  import { akashicEngine as g } from "engine-files-v1";
2
+ import type { RunnerLoadFileHandler } from "../../../types";
2
3
  export interface NodeScriptAssetParameters {
3
4
  id: string;
4
5
  path: string;
5
6
  errorHandler: (err: any) => void;
6
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
7
+ loadFileHandler: RunnerLoadFileHandler;
7
8
  }
8
9
  export declare class NodeScriptAsset extends g.ScriptAsset {
9
10
  static PRE_SCRIPT: string;
@@ -9,7 +9,7 @@ class NodeScriptAsset extends engine_files_v1_1.akashicEngine.ScriptAsset {
9
9
  this.loadFileHandler = param.loadFileHandler;
10
10
  }
11
11
  _load(loader) {
12
- this.loadFileHandler(this.path, (err, text) => {
12
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
13
13
  // NOTE: 過去バージョンのため型については精査しない
14
14
  if (err) {
15
15
  loader._onAssetError(this, err);
@@ -1,8 +1,9 @@
1
1
  import { akashicEngine as g } from "engine-files-v1";
2
+ import type { RunnerLoadFileHandler } from "../../../types";
2
3
  export interface NodeTextAssetParameters {
3
4
  id: string;
4
5
  path: string;
5
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
6
+ loadFileHandler: RunnerLoadFileHandler;
6
7
  }
7
8
  export declare class NodeTextAsset extends g.TextAsset {
8
9
  private loadFileHandler;
@@ -8,7 +8,7 @@ class NodeTextAsset extends engine_files_v1_1.akashicEngine.TextAsset {
8
8
  this.loadFileHandler = param.loadFileHandler;
9
9
  }
10
10
  _load(loader) {
11
- this.loadFileHandler(this.path, (err, text) => {
11
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
12
12
  // NOTE: 過去バージョンのため型については精査しない
13
13
  if (err) {
14
14
  loader._onAssetError(this, err);
@@ -175,7 +175,7 @@ class RunnerV2 extends Runner_1.Runner {
175
175
  renderingMode: this.renderingMode,
176
176
  sendToExternalHandler: (data) => this.onSendedToExternal(data),
177
177
  errorHandler: (e) => this.onError(e),
178
- loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
178
+ loadFileHandler: (url, encoding, callback) => this.loadFileHandler(url, encoding, callback)
179
179
  });
180
180
  this.pause();
181
181
  const driver = new engine_files_v2_1.gameDriver.GameDriver({
@@ -1,6 +1,7 @@
1
1
  import { akashicEngine as g } from "engine-files-v2";
2
+ import type { RunnerLoadFileHandler } from "../../types";
2
3
  export interface ResourceFactoryParameters {
3
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
4
+ loadFileHandler: RunnerLoadFileHandler;
4
5
  errorHandler: (err: any) => void;
5
6
  }
6
7
  export declare class ResourceFactory extends g.ResourceFactory {
@@ -1,9 +1,10 @@
1
1
  import { akashicEngine as g } from "engine-files-v2";
2
+ import type { RunnerLoadFileHandler } from "../../../types";
2
3
  export interface NodeScriptAssetParameters {
3
4
  id: string;
4
5
  path: string;
5
6
  errorHandler: (err: any) => void;
6
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
7
+ loadFileHandler: RunnerLoadFileHandler;
7
8
  }
8
9
  export declare class NodeScriptAsset extends g.ScriptAsset {
9
10
  static PRE_SCRIPT: string;
@@ -9,7 +9,7 @@ class NodeScriptAsset extends engine_files_v2_1.akashicEngine.ScriptAsset {
9
9
  this.loadFileHandler = param.loadFileHandler;
10
10
  }
11
11
  _load(loader) {
12
- this.loadFileHandler(this.path, (err, text) => {
12
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
13
13
  // NOTE: 過去バージョンのため型については精査しない
14
14
  if (err) {
15
15
  loader._onAssetError(this, err);
@@ -1,8 +1,9 @@
1
1
  import { akashicEngine as g } from "engine-files-v2";
2
+ import type { RunnerLoadFileHandler } from "../../../types";
2
3
  export interface NodeTextAssetParameters {
3
4
  id: string;
4
5
  path: string;
5
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
6
+ loadFileHandler: RunnerLoadFileHandler;
6
7
  }
7
8
  export declare class NodeTextAsset extends g.TextAsset {
8
9
  private loadFileHandler;
@@ -8,7 +8,7 @@ class NodeTextAsset extends engine_files_v2_1.akashicEngine.TextAsset {
8
8
  this.loadFileHandler = param.loadFileHandler;
9
9
  }
10
10
  _load(loader) {
11
- this.loadFileHandler(this.path, (err, text) => {
11
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
12
12
  // NOTE: 過去バージョンのため型については精査しない
13
13
  if (err) {
14
14
  loader._onAssetError(this, err);
@@ -191,7 +191,7 @@ class RunnerV3 extends Runner_1.Runner {
191
191
  renderingMode: this.renderingMode,
192
192
  sendToExternalHandler: (data) => this.onSendedToExternal(data),
193
193
  errorHandler: (e) => this.onError(e),
194
- loadFileHandler: (url, callback) => this.loadFileHandler(url, callback)
194
+ loadFileHandler: (url, encoding, callback) => this.loadFileHandler(url, encoding, callback)
195
195
  });
196
196
  this.pause();
197
197
  const driver = new engineFiles_1.gameDriver.GameDriver({
@@ -1,6 +1,7 @@
1
+ import type { RunnerLoadFileHandler } from "../../types";
1
2
  import type { akashicEngine as g } from "../engineFiles";
2
3
  export interface NodeCanvasResourceFactoryParameters {
3
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
4
+ loadFileHandler: RunnerLoadFileHandler;
4
5
  errorHandler: (err: Error) => void;
5
6
  }
6
7
  /**
@@ -17,6 +18,7 @@ export declare class NodeCanvasResourceFactory implements g.ResourceFactory {
17
18
  createAudioPlayer(system: g.AudioSystem): g.AudioPlayer;
18
19
  createTextAsset(id: string, assetPath: string): g.TextAsset;
19
20
  createScriptAsset(id: string, assetPath: string): g.ScriptAsset;
21
+ createBinaryAsset(id: string, assetPath: string): g.BinaryAsset;
20
22
  createSurface(width: number, height: number): g.Surface;
21
23
  createGlyphFactory(fontFamily: string | string[], fontSize: number, baselineHeight?: number, fontColor?: string, strokeWidth?: number, strokeColor?: string, strokeOnly?: boolean, fontWeight?: g.FontWeightString): g.GlyphFactory;
22
24
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NodeCanvasResourceFactory = void 0;
4
4
  const canvas_1 = require("canvas");
5
+ const NodeBinaryAsset_1 = require("./assets/NodeBinaryAsset");
5
6
  const NodeScriptAsset_1 = require("./assets/NodeScriptAsset");
6
7
  const NodeTextAsset_1 = require("./assets/NodeTextAsset");
7
8
  const NullAudioAsset_1 = require("./audios/NullAudioAsset");
@@ -46,6 +47,13 @@ class NodeCanvasResourceFactory {
46
47
  loadFileHandler: this.loadFileHandler
47
48
  });
48
49
  }
50
+ createBinaryAsset(id, assetPath) {
51
+ return new NodeBinaryAsset_1.NodeBinaryAsset({
52
+ id,
53
+ path: assetPath,
54
+ loadFileHandler: this.loadFileHandler
55
+ });
56
+ }
49
57
  createSurface(width, height) {
50
58
  const canvas = new canvas_1.Canvas(width, height);
51
59
  return new NodeCanvasSurface_1.NodeCanvasSurface(canvas);
@@ -1,6 +1,7 @@
1
+ import type { RunnerLoadFileHandler } from "../../types";
1
2
  import type { akashicEngine as g } from "../engineFiles";
2
3
  export interface NullResourceFactoryParameters {
3
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
4
+ loadFileHandler: RunnerLoadFileHandler;
4
5
  errorHandler: (err: Error) => void;
5
6
  }
6
7
  /**
@@ -16,6 +17,7 @@ export declare class NullResourceFactory implements g.ResourceFactory {
16
17
  createAudioPlayer(system: g.AudioSystem): g.AudioPlayer;
17
18
  createTextAsset(id: string, assetPath: string): g.TextAsset;
18
19
  createScriptAsset(id: string, assetPath: string): g.ScriptAsset;
20
+ createBinaryAsset(id: string, assetPath: string): g.BinaryAsset;
19
21
  createSurface(width: number, height: number): g.Surface;
20
22
  createGlyphFactory(fontFamily: string | string[], fontSize: number, baselineHeight?: number, fontColor?: string, strokeWidth?: number, strokeColor?: string, strokeOnly?: boolean, fontWeight?: g.FontWeightString): g.GlyphFactory;
21
23
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NullResourceFactory = void 0;
4
+ const NodeBinaryAsset_1 = require("./assets/NodeBinaryAsset");
4
5
  const NodeScriptAsset_1 = require("./assets/NodeScriptAsset");
5
6
  const NodeTextAsset_1 = require("./assets/NodeTextAsset");
6
7
  const NullAudioAsset_1 = require("./audios/NullAudioAsset");
@@ -44,6 +45,13 @@ class NullResourceFactory {
44
45
  loadFileHandler: this.loadFileHandler
45
46
  });
46
47
  }
48
+ createBinaryAsset(id, assetPath) {
49
+ return new NodeBinaryAsset_1.NodeBinaryAsset({
50
+ id,
51
+ path: assetPath,
52
+ loadFileHandler: this.loadFileHandler
53
+ });
54
+ }
47
55
  createSurface(width, height) {
48
56
  return new NullSurface_1.NullSurface(width, height);
49
57
  }
@@ -0,0 +1,15 @@
1
+ import type { RunnerLoadFileHandler } from "../../../types";
2
+ import type { akashicEngine as g } from "../../engineFiles";
3
+ import { Asset } from "./Asset";
4
+ export interface NodeBinaryAssetParameters {
5
+ id: string;
6
+ path: string;
7
+ loadFileHandler: RunnerLoadFileHandler;
8
+ }
9
+ export declare class NodeBinaryAsset extends Asset implements g.BinaryAsset {
10
+ type: "binary";
11
+ data: ArrayBuffer;
12
+ private loadFileHandler;
13
+ constructor(param: NodeBinaryAssetParameters);
14
+ _load(loader: g.AssetLoadHandler): void;
15
+ }
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NodeBinaryAsset = void 0;
4
+ const Asset_1 = require("./Asset");
5
+ class NodeBinaryAsset extends Asset_1.Asset {
6
+ constructor(param) {
7
+ super(param.id, param.path);
8
+ this.type = "binary";
9
+ this.loadFileHandler = param.loadFileHandler;
10
+ }
11
+ _load(loader) {
12
+ this.loadFileHandler(this.path, "uint8array", (err, data) => {
13
+ if (err) {
14
+ loader._onAssetError(this, {
15
+ name: "AssetLoadError",
16
+ message: err.message,
17
+ retriable: false
18
+ });
19
+ }
20
+ else if (data == null) {
21
+ loader._onAssetError(this, {
22
+ name: "AssetLoadError",
23
+ message: "NodeBinaryAsset#_load(): No data received",
24
+ retriable: false
25
+ });
26
+ }
27
+ else {
28
+ // FIXME: as の回避
29
+ const arrayBuffer = toArrayBuffer(data);
30
+ this.data = arrayBuffer;
31
+ loader._onAssetLoad(this);
32
+ }
33
+ });
34
+ }
35
+ }
36
+ exports.NodeBinaryAsset = NodeBinaryAsset;
37
+ function toArrayBuffer(typedArray) {
38
+ // sandbox の外側で初期化された TypedArray の .buffer を参照しても内容が正常に取得できないため、新たに TypedArray を生成し直す
39
+ const arrayBuffer = new ArrayBuffer(typedArray.length);
40
+ const view = new Uint8Array(arrayBuffer);
41
+ for (let i = 0; i < typedArray.length; ++i) {
42
+ view[i] = typedArray[i];
43
+ }
44
+ return arrayBuffer;
45
+ }
@@ -1,16 +1,17 @@
1
+ import type { RunnerLoadFileHandler } from "../../../types";
1
2
  import type { akashicEngine as g } from "../../engineFiles";
2
3
  import { Asset } from "./Asset";
3
4
  export interface NodeScriptAssetParameters {
4
5
  id: string;
5
6
  path: string;
7
+ exports?: string[];
6
8
  errorHandler: (err: any) => void;
7
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
9
+ loadFileHandler: RunnerLoadFileHandler;
8
10
  }
9
11
  export declare class NodeScriptAsset extends Asset implements g.ScriptAsset {
10
- static PRE_SCRIPT: string;
11
- static POST_SCRIPT: string;
12
12
  type: "script";
13
13
  script: string;
14
+ exports: string[];
14
15
  private errorHandler;
15
16
  private loadFileHandler;
16
17
  constructor(param: NodeScriptAssetParameters);
@@ -4,14 +4,16 @@ exports.NodeScriptAsset = void 0;
4
4
  const Asset_1 = require("./Asset");
5
5
  class NodeScriptAsset extends Asset_1.Asset {
6
6
  constructor(param) {
7
+ var _a;
7
8
  super(param.id, param.path);
8
9
  this.type = "script";
9
10
  this.script = "";
11
+ this.exports = (_a = param.exports) !== null && _a !== void 0 ? _a : [];
10
12
  this.errorHandler = param.errorHandler;
11
13
  this.loadFileHandler = param.loadFileHandler;
12
14
  }
13
15
  _load(loader) {
14
- this.loadFileHandler(this.path, (err, text) => {
16
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
15
17
  if (err) {
16
18
  loader._onAssetError(this, {
17
19
  name: "AssetLoadError",
@@ -27,13 +29,23 @@ class NodeScriptAsset extends Asset_1.Asset {
27
29
  });
28
30
  }
29
31
  else {
32
+ // FIXME: as の回避
30
33
  this.script = text;
31
34
  loader._onAssetLoad(this);
32
35
  }
33
36
  });
34
37
  }
35
38
  execute(execEnv) {
36
- const func = new Function("g", NodeScriptAsset.PRE_SCRIPT + this.script + NodeScriptAsset.POST_SCRIPT);
39
+ let postScript = "";
40
+ for (const key of this.exports) {
41
+ postScript += `exports["${key}"] = typeof ${key} !== "undefined" ? ${key} : undefined;\n`;
42
+ }
43
+ const func = new Function("g", "(function(exports, require, module, __filename, __dirname) {\n" +
44
+ this.script +
45
+ "\n" +
46
+ postScript +
47
+ "\n" +
48
+ "})(g.module.exports, g.module.require, g.module, g.filename, g.dirname);");
37
49
  try {
38
50
  func(execEnv);
39
51
  }
@@ -43,6 +55,4 @@ class NodeScriptAsset extends Asset_1.Asset {
43
55
  return execEnv.module.exports;
44
56
  }
45
57
  }
46
- NodeScriptAsset.PRE_SCRIPT = "(function(exports, require, module, __filename, __dirname) {\n";
47
- NodeScriptAsset.POST_SCRIPT = "\n})(g.module.exports, g.module.require, g.module, g.filename, g.dirname);";
48
58
  exports.NodeScriptAsset = NodeScriptAsset;
@@ -1,9 +1,10 @@
1
+ import type { RunnerLoadFileHandler } from "../../../types";
1
2
  import type { akashicEngine as g } from "../../engineFiles";
2
3
  import { Asset } from "./Asset";
3
4
  export interface NodeTextAssetParameters {
4
5
  id: string;
5
6
  path: string;
6
- loadFileHandler: (url: string, callback: (err: Error | null, data?: string) => void) => void;
7
+ loadFileHandler: RunnerLoadFileHandler;
7
8
  }
8
9
  export declare class NodeTextAsset extends Asset implements g.TextAsset {
9
10
  type: "text";
@@ -10,7 +10,7 @@ class NodeTextAsset extends Asset_1.Asset {
10
10
  this.loadFileHandler = param.loadFileHandler;
11
11
  }
12
12
  _load(loader) {
13
- this.loadFileHandler(this.path, (err, text) => {
13
+ this.loadFileHandler(this.path, "utf-8", (err, text) => {
14
14
  if (err) {
15
15
  loader._onAssetError(this, {
16
16
  name: "AssetLoadError",
@@ -26,6 +26,7 @@ class NodeTextAsset extends Asset_1.Asset {
26
26
  });
27
27
  }
28
28
  else {
29
+ // FIXME: as の回避
29
30
  this.data = text;
30
31
  loader._onAssetLoad(this);
31
32
  }
package/lib/utils.d.ts CHANGED
@@ -1,9 +1,12 @@
1
+ export type EncodingType = "utf-8" | "uint8array";
1
2
  /**
2
3
  * ファイルを読み込む。
3
4
  *
4
5
  * @param url url または path
5
6
  */
6
- export declare function loadFile(url: string): Promise<string>;
7
+ export declare function loadFile(url: string, encoding: EncodingType): Promise<string | Uint8Array>;
8
+ export declare function loadFile(url: string, encoding: "utf-8"): Promise<string>;
9
+ export declare function loadFile(url: string, encoding: "uint8array"): Promise<Uint8Array>;
7
10
  /**
8
11
  * loadFile() の内部実装のうち、特にテストのため外部から参照するものをまとめた namespace 。
9
12
  * 通常の利用では参照する必要はない。
@@ -15,6 +18,6 @@ export declare namespace LoadFileInternal {
15
18
  * 具体的な値は調整の余地がある。少なくとも Windows 環境で、128 以上で問題が起きることがわかっている。
16
19
  */
17
20
  const MAX_PARALLEL_LOAD = 32;
18
- function loadImpl(url: string): Promise<string>;
21
+ function loadImpl(url: string, encoding?: EncodingType): Promise<string | Uint8Array>;
19
22
  }
20
23
  export declare function isHttpProtocol(url: string): boolean;
package/lib/utils.js CHANGED
@@ -14,15 +14,10 @@ const fs = require("fs");
14
14
  const node_fetch_1 = require("node-fetch");
15
15
  const waitings = [];
16
16
  let loadingCount = 0;
17
- /**
18
- * ファイルを読み込む。
19
- *
20
- * @param url url または path
21
- */
22
- function loadFile(url) {
17
+ function loadFile(url, encoding = "utf-8") {
23
18
  return __awaiter(this, void 0, void 0, function* () {
24
19
  const promise = new Promise((resolve, reject) => {
25
- waitings.push({ url, resolve, reject });
20
+ waitings.push({ url, encoding, resolve, reject });
26
21
  });
27
22
  processWaitingLoad();
28
23
  return promise;
@@ -33,10 +28,10 @@ function processWaitingLoad() {
33
28
  return __awaiter(this, void 0, void 0, function* () {
34
29
  if (loadingCount >= LoadFileInternal.MAX_PARALLEL_LOAD || waitings.length === 0)
35
30
  return;
36
- const { url, resolve, reject } = waitings.shift();
31
+ const { url, encoding, resolve, reject } = waitings.shift();
37
32
  try {
38
33
  ++loadingCount;
39
- resolve(yield LoadFileInternal.loadImpl(url));
34
+ resolve(yield LoadFileInternal.loadImpl(url, encoding));
40
35
  }
41
36
  catch (e) {
42
37
  reject(e);
@@ -59,15 +54,30 @@ var LoadFileInternal;
59
54
  * 具体的な値は調整の余地がある。少なくとも Windows 環境で、128 以上で問題が起きることがわかっている。
60
55
  */
61
56
  LoadFileInternal.MAX_PARALLEL_LOAD = 32;
62
- function loadImpl(url) {
57
+ function loadImpl(url, encoding = "utf-8") {
63
58
  return __awaiter(this, void 0, void 0, function* () {
64
59
  if (isHttpProtocol(url)) {
65
60
  const res = yield (0, node_fetch_1.default)(url, { method: "GET" });
66
- return res.text();
61
+ if (encoding === "utf-8") {
62
+ return res.text();
63
+ }
64
+ else {
65
+ // NOTE: vm2 の sandbox 環境外から sandbox 内へ ArrayBuffer を渡してもデータの中身を参照することができない。
66
+ // おそらく Proxy の挙動によるものだと推測されるが原因については精査できていない。
67
+ // 暫定対応として ArrayBuffer の代わりに Uint8Array を渡し、 sandbox 内で ArrayBuffer のインスタンスを生成するようにする。
68
+ const arrayBuffer = yield res.arrayBuffer();
69
+ return new Uint8Array(arrayBuffer);
70
+ }
67
71
  }
68
72
  else {
69
- const str = fs.readFileSync(url, { encoding: "utf8" });
70
- return str;
73
+ if (encoding === "utf-8") {
74
+ return fs.readFileSync(url, { encoding: "utf-8" });
75
+ }
76
+ else {
77
+ // 上記 NOTE と同様の理由により Uint8Array を返す。
78
+ const buffer = fs.readFileSync(url, { encoding: null });
79
+ return Uint8Array.from(buffer);
80
+ }
71
81
  }
72
82
  });
73
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akashic/headless-driver",
3
- "version": "2.8.3",
3
+ "version": "2.9.0",
4
4
  "description": "A library to execute contents using Akashic Engine headlessly",
5
5
  "main": "lib/index.js",
6
6
  "author": "DWANGO Co., Ltd.",
@@ -24,12 +24,12 @@
24
24
  "lib"
25
25
  ],
26
26
  "dependencies": {
27
- "@akashic/amflow": "^3.1.0",
28
- "@akashic/playlog": "^3.1.0",
27
+ "@akashic/amflow": "^3.2.0",
28
+ "@akashic/playlog": "^3.2.0",
29
29
  "@akashic/trigger": "^2.0.0",
30
- "engine-files-v1": "npm:@akashic/engine-files@1.2.1",
31
- "engine-files-v2": "npm:@akashic/engine-files@2.2.2",
32
- "engine-files-v3": "npm:@akashic/engine-files@3.7.1",
30
+ "engine-files-v1": "npm:@akashic/engine-files@1.2.2",
31
+ "engine-files-v2": "npm:@akashic/engine-files@2.2.3",
32
+ "engine-files-v3": "npm:@akashic/engine-files@3.7.3",
33
33
  "js-sha256": "^0.9.0",
34
34
  "lodash.clonedeep": "^4.5.0",
35
35
  "node-fetch": "^2.6.7",