Wasm working again for node

needs testing in browser
This commit is contained in:
Felix Kaspar
2023-11-15 00:24:04 +01:00
parent 063acc6bbf
commit 997f0f349d
6 changed files with 903 additions and 21 deletions

View File

@@ -1,16 +1,18 @@
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
export type ImposeParamsType = {
file: any;
file: PdfFile;
nup: number;
format: string;
}
export type ImposeParamsBaseType = {
file: any;
file: PdfFile;
nup: number;
format: string;
pdfcpuWrapper: any;
}
export async function impose(params: ImposeParamsBaseType) {
return await params.pdfcpuWrapper.oneToOne([
export async function impose(params: ImposeParamsBaseType): Promise<PdfFile> {
const result = new PdfFile(params.file.originalFilename, await params.pdfcpuWrapper.oneToOne([
"pdfcpu.wasm",
"nup",
"-c",
@@ -19,5 +21,7 @@ export async function impose(params: ImposeParamsBaseType) {
"/output.pdf",
String(params.nup),
"input.pdf",
], params.file);
], await params.file.uint8Array), RepresentationType.Uint8Array, params.file.filename + "_imposed");
console.log("ImposeResult: ", result);
return result;
}

View File

@@ -1,8 +1,7 @@
import { WasmFs } from '@wasmer/wasmfs';
import path from "path";
let webWasmLocation = "/wasm/";
let nodeWasmLocation = "./public/wasm/";
let nodeWasmLocation = "../shared-operations/src/wasm/pdfcpu/";
let fs;
const wasmfs = new WasmFs();
@@ -22,7 +21,7 @@ async function configureFs() {
}
async function loadWasm() {
global.crypto = (await import("crypto")).webcrypto; // wasm dependecy
// global.crypto = (await import("crypto")).webcrypto; // wasm dependecy
await import("./wasm_exec.js");
}

View File

@@ -201,14 +201,14 @@
}
}
if (!global.crypto && global.require) {
const nodeCrypto = require("crypto");
global.crypto = {
getRandomValues(b) {
nodeCrypto.randomFillSync(b);
},
};
}
// if (!global.crypto && global.require) {
// const nodeCrypto = require("crypto");
// global.crypto = {
// getRandomValues(b) {
// nodeCrypto.randomFillSync(b);
// },
// };
// }
if (!global.crypto) {
throw new Error("global.crypto is not available, polyfill required (getRandomValues only)");
}

View File

@@ -14,6 +14,7 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
if(actions === undefined || (Array.isArray(actions) && actions.length == 0)) { // isEmpty
console.log("Last Operation");
if(Array.isArray(input)) {
console.log("ArrayOut: ", input);
console.log("operation done: " + input[0].filename + (input.length > 1 ? "+" : ""));
results = results.concat(input);
return;
@@ -62,6 +63,7 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.impose({file: input, nup: action.values["nup"], format: action.values["format"]});
newPdf.filename += "_imposed";
console.log("newPDF: ", newPdf);
return newPdf;
});
break;
@@ -158,12 +160,14 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
}
}
async function * nToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile>): AsyncGenerator<string, void, void> {
async function * nToN(input: PdfFile|PdfFile[], action: Action, callback: (pdf: PdfFile) => Promise<PdfFile|PdfFile[]>): AsyncGenerator<string, void, void> {
if(Array.isArray(input)) {
const nextInputs: PdfFile[] = []
console.log("inputs", input);
let nextInputs: PdfFile[] = []
for (let i = 0; i < input.length; i++) {
nextInputs.concat(await callback(input[i]));
nextInputs = nextInputs.concat(await callback(input[i]));
}
console.log("nextInputs", nextInputs);
yield* nextOperation(action.actions, nextInputs);
}
else {