Use IDBFS and NODEFS to process big file

This commit is contained in:
Jerome Wu
2019-12-03 22:06:44 +08:00
parent 72a2ff6e84
commit 3ab760b5bb
20 changed files with 215 additions and 162 deletions

View File

@@ -1,5 +1,6 @@
const worker = require('../');
const getCore = require('./getCore');
const fs = require('../../worker/browser/fs');
global.addEventListener('message', ({ data }) => {
worker.dispatchHandlers(data, (obj) => postMessage(obj));
@@ -7,4 +8,5 @@ global.addEventListener('message', ({ data }) => {
worker.setAdapter({
getCore,
fs,
});

View File

@@ -30,7 +30,7 @@ const load = ({ workerId, payload: { options: { corePath } } }, res) => {
if (Module == null) {
const Core = adapter.getCore(corePath);
Core()
.then((_Module) => {
.then(async (_Module) => {
Module = _Module;
Module.setLogger((message, type) => {
res.progress({
@@ -45,60 +45,37 @@ const load = ({ workerId, payload: { options: { corePath } } }, res) => {
}
};
const write = ({
const syncfs = async ({
payload: {
path,
data,
populate = false,
},
}, res) => {
const d = Uint8Array.from({ ...data, length: Object.keys(data).length });
Module.FS.writeFile(path, d);
res.resolve({ message: `Write ${path} (${d.length} bytes)` });
await Module.syncfs(populate);
res.resolve({ message: `Sync file system with populate=${populate}` });
};
const writeText = ({
payload: {
path,
text,
},
}, res) => {
Module.FS.writeFile(path, text);
res.resolve({ message: `Write ${path} (${text.length} bytes)` });
};
const read = ({
const ls = ({
payload: {
path,
},
}, res) => {
res.resolve(Module.FS.readFile(path));
const dirs = Module.FS.readdir(path);
res.resolve({ message: `List path ${path}`, dirs });
};
const remove = ({
payload: {
path,
},
}, res) => {
Module.FS.unlink(path);
res.resolve({ message: `Delete ${path}` });
};
const mkdir = ({
payload: {
path,
},
}, res) => {
Module.FS.mkdir(path);
res.resolve({ message: `Create Directory ${path}` });
};
const run = ({
const run = async ({
payload: {
args: _args,
options: { inputPath, outputPath, del },
},
}, res) => {
const args = [...defaultArgs, ..._args.trim().split(' ')];
ffmpeg(args.length, strList2ptr(args));
await adapter.fs.writeFile(outputPath, Module.FS.readFile(outputPath));
Module.FS.unlink(outputPath);
if (del && typeof inputPath === 'string') {
await adapter.fs.deleteFile(inputPath);
}
res.resolve({ message: `Complete ${args.join(' ')}` });
};
@@ -118,11 +95,8 @@ exports.dispatchHandlers = (packet, send) => {
try {
({
load,
write,
writeText,
read,
remove,
mkdir,
ls,
syncfs,
run,
})[packet.action](packet, res);
} catch (err) {

View File

@@ -1,5 +1,6 @@
const worker = require('../');
const getCore = require('./getCore');
const fs = require('../../worker/node/fs');
process.on('message', (packet) => {
worker.dispatchHandlers(packet, (obj) => process.send(obj));
@@ -7,4 +8,5 @@ process.on('message', (packet) => {
worker.setAdapter({
getCore,
fs,
});