134 lines
139 KiB
JavaScript
134 lines
139 KiB
JavaScript
|
// This file helps make the compiled js file be imported as a web worker by the src/magickApi.ts file
|
||
|
|
||
|
const stdout = []
|
||
|
const stderr = []
|
||
|
let exitCode = 0
|
||
|
|
||
|
function ChangeUrl(url, fileName)
|
||
|
{
|
||
|
let splitUrl = url.split('/')
|
||
|
splitUrl[splitUrl.length -1] = fileName
|
||
|
return splitUrl.join('/')
|
||
|
}
|
||
|
// const magickJsCurrentPath = 'https://knicknic.github.io/wasm-imagemagick/magick.js';
|
||
|
function GetCurrentUrlDifferentFilename(fileName)
|
||
|
{
|
||
|
return ChangeUrl(magickJsCurrentPath, fileName)
|
||
|
}
|
||
|
|
||
|
if (typeof Module == 'undefined') {
|
||
|
Module = {
|
||
|
noInitialRun: true,
|
||
|
moduleLoaded: false,
|
||
|
messagesToProcess: [],
|
||
|
|
||
|
print: text => {
|
||
|
stdout.push(text)
|
||
|
console.log(text)
|
||
|
},
|
||
|
printErr: text => {
|
||
|
stderr.push(text)
|
||
|
console.error(text);
|
||
|
},
|
||
|
quit: status=> {
|
||
|
exitCode = status
|
||
|
}
|
||
|
}
|
||
|
if(typeof magickJsCurrentPath !== "undefined"){
|
||
|
Module.locateFile = GetCurrentUrlDifferentFilename;
|
||
|
}
|
||
|
|
||
|
// see https://kripken.github.io/emscripten-site/docs/api_reference/module.html
|
||
|
Module.onRuntimeInitialized = function () {
|
||
|
FS.mkdir('/pictures')
|
||
|
FS.currentPath = '/pictures'
|
||
|
|
||
|
Module.moduleLoaded = true
|
||
|
processFiles()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
processFiles = function () {
|
||
|
if (!Module.moduleLoaded) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// clean up stdout, stderr and exitCode
|
||
|
stdout.splice(0, stdout.length)
|
||
|
stderr.splice(0, stderr.length)
|
||
|
exitCode = undefined
|
||
|
for (let message of Module.messagesToProcess) {
|
||
|
|
||
|
for (let file of message.files) {
|
||
|
let fileData = file.content
|
||
|
if(fileData instanceof ArrayBuffer)
|
||
|
{
|
||
|
// fileData = new DataView(fileData)
|
||
|
fileData = new Uint8Array(fileData);
|
||
|
}
|
||
|
FS.writeFile(file.name, fileData)
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
Module.callMain(message.args)
|
||
|
}
|
||
|
catch (e) { }
|
||
|
for (let file of message.files) {
|
||
|
// cleanup source files
|
||
|
// mogrify then output files have same name, so skip
|
||
|
if (message.args[0] != 'mogrify') {
|
||
|
FS.unlink(file.name)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let dir = FS.open('/pictures')
|
||
|
let files = dir.node.contents
|
||
|
let responseFiles = []
|
||
|
let transfer = []
|
||
|
for (let destFilename in files) {
|
||
|
let processed = {}
|
||
|
processed.name = destFilename
|
||
|
let read = FS.readFile(destFilename)
|
||
|
// cleanup read file
|
||
|
FS.unlink(destFilename)
|
||
|
|
||
|
if('transferable' in message)
|
||
|
{
|
||
|
processed.buffer = read
|
||
|
transfer.push(read.buffer)
|
||
|
}
|
||
|
else{
|
||
|
processed.blob = new Blob([read])
|
||
|
processed.buffer = read
|
||
|
}
|
||
|
responseFiles.push(processed)
|
||
|
}
|
||
|
message.outputFiles = responseFiles
|
||
|
message.stdout = stdout.map(s => s)
|
||
|
message.stderr = stderr.map(s => s)
|
||
|
message.exitCode = exitCode
|
||
|
|
||
|
for (let file of message.files) {
|
||
|
if(file.content instanceof ArrayBuffer)
|
||
|
{
|
||
|
transfer.push(file.content)
|
||
|
}
|
||
|
else{
|
||
|
transfer.push(file.content.buffer)
|
||
|
}
|
||
|
}
|
||
|
postMessage(message) //, transfer)
|
||
|
}
|
||
|
Module.messagesToProcess = []
|
||
|
}
|
||
|
|
||
|
onmessage = function (magickRequest) {
|
||
|
Module.messagesToProcess.push(magickRequest.data)
|
||
|
processFiles()
|
||
|
}
|
||
|
|
||
|
var Module=typeof Module!=="undefined"?Module:{};var moduleOverrides={};var key;for(key in Module){if(Module.hasOwnProperty(key)){moduleOverrides[key]=Module[key]}}Module["arguments"]=[];Module["thisProgram"]="./this.program";Module["quit"]=(function(status,toThrow){throw toThrow});Module["preRun"]=[];Module["postRun"]=[];var ENVIRONMENT_IS_WEB=false;var ENVIRONMENT_IS_WORKER=false;var ENVIRONMENT_IS_NODE=false;var ENVIRONMENT_IS_SHELL=false;ENVIRONMENT_IS_WEB=typeof window==="object";ENVIRONMENT_IS_WORKER=typeof importScripts==="function";ENVIRONMENT_IS_NODE=typeof process==="object"&&typeof require==="function"&&!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_WORKER;ENVIRONMENT_IS_SHELL=!ENVIRONMENT_IS_WEB&&!ENVIRONMENT_IS_NODE&&!ENVIRONMENT_IS_WORKER;var scriptDirectory="";function locateFile(path){if(Module["locateFile"]){return Module["locateFile"](path,scriptDirectory)}else{return scriptDirectory+path}}if(ENVIRONMENT_IS_NODE){scriptDirectory=__dirname+"/";var nodeFS;var nodePath;Module["read"]=function shell_read(filename,binary){var ret;if(!nodeFS)nodeFS=require("fs");if(!nodePath)nodePath=require("path");filename=nodePath["normalize"](filename);ret=nodeFS["readFileSync"](filename);return binary?ret:ret.toString()};Module["readBinary"]=function readBinary(filename){var ret=Module["read"](filename,true);if(!ret.buffer){ret=new Uint8Array(ret)}assert(ret.buffer);return ret};if(process["argv"].length>1){Module["thisProgram"]=process["argv"][1].replace(/\\/g,"/")}Module["arguments"]=process["argv"].slice(2);if(typeof module!=="undefined"){module["exports"]=Module}process["on"]("uncaughtException",(function(ex){if(!(ex instanceof ExitStatus)){throw ex}}));process["on"]("unhandledRejection",(function(reason,p){process["exit"](1)}));Module["quit"]=(function(status){process["exit"](status)});Module["inspect"]=(function(){return"[Emscripten Module object]"})}else if(ENVIRONMENT_IS_SHELL){if(typeof read!="undefined"){Module["read"]=function shell_read(f){return read(f)}}Module["readBinary"]=function readBinary(f){var data;if(typeof readbuffer==="function"){return new Uint8Array(readbuffer(f))}data=read(f,"binary");assert(typeof data==="object");return data};if(typeof scriptArgs!="undefined"){Module["arguments"]=scriptArgs}else if(typeof arguments!="undefined"){Module["arguments"]=arguments}if(typeof quit==="function"){Module["quit"]=(function(status){quit(status)})}}else if(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER){if(ENVIRONMENT_IS_WEB){if(document.currentScript){scriptDirectory=document.currentScript.src}}else{scriptDirectory=self.location.href}if(scriptDirectory.indexOf("blob:")!==0){scriptDirectory=scriptDirectory.substr(0,scriptDirectory.lastIndexOf("/")+1)}else{scriptDirectory=""}Module["read"]=function shell_read(url){var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.send(null);return xhr.responseText};if(ENVIRONMENT_IS_WORKER){Module["readBinary"]=function readBinary(url){var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.responseType="arraybuffer";xhr.send(null);return new Uint8Array(xhr.response)}}Module["readAsync"]=function readAsync(url,onload,onerror){var xhr=new XMLHttpRequest;xhr.open("GET",url,true);xhr.responseType="arraybuffer";xhr.onload=function xhr_onload(){if(xhr.status==200||xhr.status==0&&xhr.response){onload(xhr.response);return}onerror()};xhr.onerror=onerror;xhr.send(null)};Module["setWindowTitle"]=(function(title){document.title=title})}else{}var out=Module["print"]||(typeof console!=="undefined"?console.log.bind(console):typeof print!=="undefined"?print:null);var err=Module["printErr"]||(typeof printErr!=="undefined"?printErr:typeof console!=="undefined"&&console.warn.bind(console)||out);for(key in moduleOverrides){if(moduleOverrides.hasOwnProperty(key)){Module[key]=moduleOverrides[key]}}moduleOverrides=undefined;var STACK_ALIGN=16;function staticAlloc(size){var ret=STATICTOP;STATICTOP=STATICTOP+size+15&-16;return ret}function dynamicAlloc(size){var ret=HEAP32[DYNAMICTOP_PTR>>2];var end=ret+size+15&-16;HEAP32[DYNAMICTOP_PTR>>2]=end;if(end>=TOTAL_MEMORY){var success=enlargeMemory();if(!succes
|
||
|
|
||
|
|
||
|
|