@nutrient-sdk/node
Version:
Convert documents in your Node.js apps.
67 lines (61 loc) • 3.29 kB
JavaScript
/*!
* Nutrient for Node.js 1.3.0 (https://www.nutrient.io/nodejs)
*
* Copyright © 2024-2026 PSPDFKit GmbH. All rights reserved.
*
* THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
* AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE PSPDFKIT LICENSE AGREEMENT.
* UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
* This notice may not be removed from this file.
*
* Nutrient uses several open source third-party components: https://www.nutrient.io/legal/acknowledgements/nodejs-acknowledgements/
*/
import { dotnet } from './dotnet.js';
/**
*
* @param baseUrl - The URL where instance is being hosted.
* @param resourceLoader - Optional callback to override the file fetching. This is useful when want to override fetching parameters, or location.
* @returns {Promise<{BaseUrl: string, Assemblies: *, Module: *}>}
*/
/**
* Extra information on resourceLoader. This is a callback passed directly to .NET WASMs configuration found at (https://github.com/dotnet/runtime/blob/477de3419157d809dc266ea03ff3fb4c05f3d1c1/src/mono/browser/runtime/dotnet.d.ts#L119)
*
* NICK's NOTE: I found that it's not possible to return Promise<Response> for types of "dotnetjs" | "dotnetwasm". Therefore, ensure you only return a URL for these if wanting to override them.
*
* Overrides the built-in boot resource loading mechanism so that boot resources can be fetched
* from a custom source, such as an external CDN.
* @param type The type of the resource to be loaded.
* @param name The name of the resource to be loaded.
* @param defaultUri The URI from which the framework would fetch the resource by default. The URI may be relative or absolute.
* @param integrity The integrity string representing the expected content in the response.
* @param behavior The detailed behavior/type of the resource to be loaded.
* @returns A URI string or a Response promise to override the loading process, or null/undefined to allow the default loading behavior.
* When returned string is not qualified with `./` or absolute URL, it will be resolved against the application base URI.
*
* type LoadBootResourceCallback = (type: WebAssemblyBootResourceType, name: string, defaultUri: string, integrity: string, behavior: AssetBehaviors) => string | Promise < Response > | null | undefined;
*/
export async function initDotnet(baseUrl, resourceLoader = undefined) {
// Testing if the `baseUrl` is a legit path or URL.
if(baseUrl === null || typeof baseUrl !== 'string' || baseUrl.trim().length === 0) {
throw Error("`baseUrl` must be a string passed to `initDotnet` and be non-empty.")
}
const { getAssemblyExports, getConfig, Module } = await dotnet
.withConfig({
locateFile: (path) => {
return `${baseUrl}/${path}`;
}
})
.withResourceLoader(resourceLoader)
.create();
globalThis.gdPicture = {
module: Module,
baseUrl: baseUrl
};
const assemblies = await getAssemblyExports(getConfig().mainAssemblyName);
await assemblies.GdPictureWasm.API.Initialize();
return {
Assemblies: assemblies,
Module: Module,
BaseUrl: baseUrl
};
}