@keeex/fusion-io
v2.2.1
Published
I/O operations for `@keeex/js-fusion` and `@keeex/js-fusion-verifier`
Readme
@keeex/fusion-io
Collection of classes to handle various kind of I/O depending on the available environment. This is used with KeeeX tools that have to interact with our Fusion product; it is shared between the private packages and the public packages.
General usage
You have to create input and output files separately. This library provides multiple classes for input and output that can be used depending on your execution environment/runtime.
The general layout is as follow:
- create an input file
- create an output file
- pass them to KeeeX tools
- close the input file
- save/close the output file
For input files, the close() function is all you need.
For output files, they will close themselves after you call saveOutput().
Alternatively, some implementation will also provide a failureClose() function that can be used in case of error, to close the resource without saving the content.
If so, calling failureClose() after saveOutput() is acceptable, and will be a no-op.
Secure usage
Using input/output requires calling close functions after usage, even in case of errors. This is an example on how it can be done.
import * as kxReactNative from "@keeex/fusion-io/reactnative.js";
import Fusion from "@keeex/js-fusion/web/fusion/index.js";
import type {KeeexCmdOptions, KeeexCmdOutput} from "@keeex/fusion-types/fusion/cmd/keeex.js";
import type {IKeypair} from "@keeex/js-keys/shared/keypair.js";
import "@keeex/crypto-provider-reactnative";
export const keeexFile = async ({
filePath,
key,
license,
destPath,
}: {
filePath: string;
key: IKeypair;
license: string;
destPath: string;
}): Promise<{
res: KeeexCmdOutput;
destPath: string;
}> => {
const kxInputFile = await kxReactNative.KxInput.createFromPath(filePath, null);
try {
const kxFileOutput = kxReactNative.KxOutput.createFromPath(destPath);
try {
const fusion = new Fusion({license});
const kxOptions: KeeexCmdOptions = {
optimization: {
noIdxInSrc: true,
srcNotKeeexed: true,
},
};
const res = await fusion.keeex({
dst: kxFileOutput,
mdata: {
author: "Mobile app",
description: "",
identities: {type: "kxkey", key},
name: "Media",
previous: [],
properties: {},
references: [],
},
options: kxOptions,
src: kxInputFile,
});
kxFileOutput.saveOutput();
return {res, destPath};
} catch (err) {
kxFileOutput.failureClose();
console.error(err);
throw new Error("Error keeexing file");
}
} catch (err) {
console.error(err);
throw new Error("Error keeexing file");
} finally {
kxInputFile.close();
}
};Generic buffers
These classes are available in all JavaScript environments.
They only operate on Uint8Array instances, so fully work in memory.
import * as kxBuf8 from "@keeex/fusion-io/buf8.js";
const data: Uint8Array;
const input = kxBuf8.KxInput.create(data, true, "somefile.jpg");
const output = new kxBuf8.KxOutput();
// Process
const result: Uint8Array = output.saveOutput();NodeJs FS
These are only available in NodeJs environments, and works with files on regular file systems.
import * as kxNodeFs from "@keeex/fusion-io/nodefs.js";
const input = await kxNodeFs.KxInput.createFromPath("/some/file.jpg", true, "mydata.jpg");
// It is important to provide the `input` to allow for some optimization and special cases
const output = await kxNodeFs.KxOutput.createOutputFile("/some/output.jpg", input);
// Process
await output.saveOutput();Browser/Blob
For browsers, only an input is currently provided. Later versions will support creating output files using browser APIs.
import * as kxBlob from "@keeex/fusion-io/browserblob.js";
const blob: Blob;
const input = await kxBlob.KxInput.createFromBlob(blob, true);React-Native
Support for React-Native specific files requires the installation of the following dependencies in the top-level of your application:
react-native-nitro-file-systemreact-native-nitro-modulesreact-native-nitro-buffer
The path arguments are the same as the react-native-nitro-file-system ones.
The library should handle gracefully passing full path, without protocol prefix, unless you're actually reading from a custom resource.
Two variant are provided; the regular ones (recommended) have synchronous underlying calls. The Async variant can be used if you need more interlacing to keep your main thread more reactive.
Input
Synchronous (recommended):
import * as kxReactNative from "@keeex/fusion-io/reactnative.js";
const input = await kxReactNative.KxInput.createFromPath("/some/file.jpg", true, "mydata.jpg");
const output = kxReactNative.KxOutput.createOutputFile("/some/output.jpg");
// Process
// File is fully written and closed only after this call
output.saveOutput();Asynchronous:
import * as kxReactNative from "@keeex/fusion-io/reactnative.js";
const input = await kxReactNative.async.KxInput.createFromPath(
"/some/file.jpg",
true,
"mydata.jpg",
);
const output = await kxReactNative.async.KxOutput.createOutputFile("/some/output.jpg");
// Process
// File is fully written and closed only after this call
await output.saveOutput();