浏览代码

Minor changes regarding HeadlessPluginContext (#708)

* Minor changes regarding HeadlessPluginContext

* Tweaks
midlik 2 年之前
父节点
当前提交
de46f08bf4

+ 1 - 1
CHANGELOG.md

@@ -6,7 +6,7 @@ Note that since we don't clearly distinguish between a public and private interf
 
 ## [Unreleased]
 
-- Add `HeadlessPluginContext` and `Canvas3DRenderer` to be used in Node.js
+- Add `HeadlessPluginContext` and `HeadlessScreenshotHelper` to be used in Node.js
 - Add example `image-renderer`
 - Fix wrong offset when rendering text with orthographic projection
 - Update camera/handle helper when `devicePixelRatio` changes

+ 3 - 3
src/examples/image-renderer/index.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2021-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Adam Midlik <midlik@gmail.com>
  *
@@ -10,15 +10,15 @@
  */
 
 import { ArgumentParser } from 'argparse';
-import * as fs from 'fs';
+import fs from 'fs';
 import path from 'path';
 
-import { STYLIZED_POSTPROCESSING } from '../../mol-canvas3d/renderer';
 import { Download, ParseCif } from '../../mol-plugin-state/transforms/data';
 import { ModelFromTrajectory, StructureComponent, StructureFromModel, TrajectoryFromMmCif } from '../../mol-plugin-state/transforms/model';
 import { StructureRepresentation3D } from '../../mol-plugin-state/transforms/representation';
 import { HeadlessPluginContext } from '../../mol-plugin/headless-plugin-context';
 import { DefaultPluginSpec } from '../../mol-plugin/spec';
+import { STYLIZED_POSTPROCESSING } from '../../mol-plugin/util/headless-screenshot';
 
 
 interface Args {

+ 6 - 7
src/mol-plugin/headless-plugin-context.ts

@@ -1,25 +1,24 @@
 /**
- * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Adam Midlik <midlik@gmail.com>
  */
 
-import * as fs from 'fs';
-
+import fs from 'fs';
 import { Canvas3D } from '../mol-canvas3d/canvas3d';
 import { PostprocessingProps } from '../mol-canvas3d/passes/postprocessing';
-import { Canvas3DRenderer, ImageRendererOptions } from '../mol-canvas3d/renderer';
 import { PluginContext } from './context';
 import { PluginSpec } from './spec';
+import { HeadlessScreenshotHelper, HeadlessScreenshotHelperOptions } from './util/headless-screenshot';
 
 
 /** PluginContext that can be used in Node.js (without DOM) */
 export class HeadlessPluginContext extends PluginContext {
-    renderer: Canvas3DRenderer;
+    renderer: HeadlessScreenshotHelper;
 
-    constructor(spec: PluginSpec, canvasSize: { width: number, height: number } = { width: 640, height: 480 }, rendererOptions?: ImageRendererOptions) {
+    constructor(spec: PluginSpec, canvasSize: { width: number, height: number } = { width: 640, height: 480 }, rendererOptions?: HeadlessScreenshotHelperOptions) {
         super(spec);
-        this.renderer = new Canvas3DRenderer(canvasSize, undefined, rendererOptions);
+        this.renderer = new HeadlessScreenshotHelper(canvasSize, undefined, rendererOptions);
         (this.canvas3d as Canvas3D) = this.renderer.canvas3d;
     }
 

+ 16 - 17
src/mol-canvas3d/renderer.ts → src/mol-plugin/util/headless-screenshot.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  * @author Jesse Liang <jesse.liang@rcsb.org>
@@ -8,23 +8,22 @@
  * @author Adam Midlik <midlik@gmail.com>
  */
 
-import * as fs from 'fs';
+import fs from 'fs';
 import path from 'path';
-
 import { type BufferRet as JpegBufferRet } from 'jpeg-js'; // Only import type here, the actual import is done by LazyImports
 import { type PNG } from 'pngjs'; // Only import type here, the actual import is done by LazyImports
 
-import { createContext } from '../mol-gl/webgl/context';
-import { AssetManager } from '../mol-util/assets';
-import { ColorNames } from '../mol-util/color/names';
-import { PixelData } from '../mol-util/image';
-import { InputObserver } from '../mol-util/input/input-observer';
-import { LazyImports } from '../mol-util/lazy-imports';
-import { ParamDefinition } from '../mol-util/param-definition';
-import { Canvas3D, Canvas3DContext, Canvas3DProps, DefaultCanvas3DParams } from './canvas3d';
-import { ImagePass, ImageProps } from './passes/image';
-import { Passes } from './passes/passes';
-import { PostprocessingParams, PostprocessingProps } from './passes/postprocessing';
+import { Canvas3D, Canvas3DContext, Canvas3DProps, DefaultCanvas3DParams } from '../../mol-canvas3d/canvas3d';
+import { ImagePass, ImageProps } from '../../mol-canvas3d/passes/image';
+import { Passes } from '../../mol-canvas3d/passes/passes';
+import { PostprocessingParams, PostprocessingProps } from '../../mol-canvas3d/passes/postprocessing';
+import { createContext } from '../../mol-gl/webgl/context';
+import { AssetManager } from '../../mol-util/assets';
+import { ColorNames } from '../../mol-util/color/names';
+import { PixelData } from '../../mol-util/image';
+import { InputObserver } from '../../mol-util/input/input-observer';
+import { LazyImports } from '../../mol-util/lazy-imports';
+import { ParamDefinition } from '../../mol-util/param-definition';
 
 
 const lazyImports = LazyImports.create('gl', 'jpeg-js', 'pngjs') as {
@@ -34,7 +33,7 @@ const lazyImports = LazyImports.create('gl', 'jpeg-js', 'pngjs') as {
 };
 
 
-export type ImageRendererOptions = {
+export type HeadlessScreenshotHelperOptions = {
     webgl?: WebGLContextAttributes,
     canvas?: Partial<Canvas3DProps>,
     imagePass?: Partial<ImageProps>,
@@ -48,11 +47,11 @@ export type RawImageData = {
 
 
 /** To render Canvas3D when running in Node.js (without DOM) */
-export class Canvas3DRenderer {
+export class HeadlessScreenshotHelper {
     readonly canvas3d: Canvas3D;
     readonly imagePass: ImagePass;
 
-    constructor(readonly canvasSize: { width: number, height: number }, canvas3d?: Canvas3D, options?: ImageRendererOptions) {
+    constructor(readonly canvasSize: { width: number, height: number }, canvas3d?: Canvas3D, options?: HeadlessScreenshotHelperOptions) {
         if (canvas3d) {
             this.canvas3d = canvas3d;
         } else {

+ 1 - 1
src/mol-util/assets.ts

@@ -9,7 +9,7 @@ import { UUID } from './uuid';
 import { iterableToArray } from '../mol-data/util';
 import { ajaxGet, DataType, DataResponse, readFromFile } from './data-source';
 import { Task } from '../mol-task';
-import { File_ as File } from './nodejs-browser-io';
+import { File_ as File } from './nodejs-shims';
 
 export { AssetManager, Asset };
 

+ 13 - 8
src/mol-util/data-source.ts

@@ -1,19 +1,24 @@
 /**
- * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ * @author Adam Midlik <midlik@gmail.com>
  *
  * Adapted from LiteMol
  */
 
-import * as fs from 'fs';
-
-import { Task, RuntimeContext } from '../mol-task';
-import { unzip, ungzip } from './zip/zip';
 import { utf8Read } from '../mol-io/common/utf8';
-import { AssetManager, Asset } from './assets';
-import { RUNNING_IN_NODEJS, File_ as File, XMLHttpRequest_ as XMLHttpRequest } from './nodejs-browser-io';
+import { RuntimeContext, Task } from '../mol-task';
+import { Asset, AssetManager } from './assets';
+import { LazyImports } from './lazy-imports';
+import { File_ as File, RUNNING_IN_NODEJS, XMLHttpRequest_ as XMLHttpRequest } from './nodejs-shims';
+import { ungzip, unzip } from './zip/zip';
+
+
+const lazyImports = LazyImports.create('fs') as {
+    'fs': typeof import ('fs'),
+};
 
 
 export enum DataCompressionMethod {
@@ -306,7 +311,7 @@ function ajaxGetInternal_file_NodeJS<T extends DataType>(title: string | undefin
     if (!RUNNING_IN_NODEJS) throw new Error('This function should only be used when running in Node.js');
     if (!url.startsWith('file://')) throw new Error('This function is only for URLs with protocol file://');
     const filename = url.substring('file://'.length);
-    const data = fs.readFileSync(filename);
+    const data = lazyImports.fs.readFileSync(filename);
     const file = new File([data], 'raw-data');
     return readFromFile(file, type);
 }

+ 1 - 1
src/mol-util/lazy-imports.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2019-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Adam Midlik <midlik@gmail.com>
  *

+ 3 - 3
src/mol-util/nodejs-browser-io.ts → src/mol-util/nodejs-shims.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Adam Midlik <midlik@gmail.com>
  *
@@ -8,8 +8,8 @@
  */
 
 
-/** Determines whether the current code is running in Node.js or in a browser */
-export const RUNNING_IN_NODEJS = typeof document === 'undefined';
+/** Determines whether the current code is running in Node.js */
+export const RUNNING_IN_NODEJS = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
 
 /** Like `XMLHttpRequest` but works also in Node.js */
 export const XMLHttpRequest_ = getXMLHttpRequest();