Remove empty buffer constant

This commit is contained in:
Dustin Brett
2022-03-09 20:38:58 -08:00
parent 814b581f39
commit 9de4545f64
10 changed files with 25 additions and 26 deletions

View File

@@ -10,7 +10,7 @@ import type { CommandInterface } from "emulators";
import type { DosInstance } from "emulators-ui/dist/types/js-dos";
import { basename, extname, join } from "path";
import { useCallback, useEffect, useState } from "react";
import { EMPTY_BUFFER, SAVE_PATH } from "utils/constants";
import { SAVE_PATH } from "utils/constants";
import { bufferToUrl, cleanUpBufferUrl } from "utils/functions";
import { cleanUpGlobals } from "utils/globals";
import { addFileToZip, isFileInZip, zipAsync } from "utils/zipFunctions";
@@ -21,7 +21,7 @@ const addJsDosConfig = async (
): Promise<Buffer> =>
Object.entries(zipConfigFiles).reduce(
async (newBuffer, [zipPath, fsPath]) =>
(await newBuffer) !== EMPTY_BUFFER &&
(await newBuffer).length > 0 &&
(await isFileInZip(await newBuffer, zipPath))
? newBuffer
: addFileToZip(await newBuffer, fsPath, zipPath, readFile),
@@ -66,11 +66,13 @@ const useDosCI = (
[dosCI, dosInstance, exists, mkdirRecursive, updateFolder, writeFile]
);
const loadBundle = useCallback(async () => {
if (!url) return;
const [currentUrl] = Object.keys(dosCI);
if (currentUrl) closeBundle(currentUrl);
const urlBuffer = url ? await readFile(url) : EMPTY_BUFFER;
const urlBuffer = await readFile(url);
const extension = extname(url).toLowerCase();
const zipBuffer =
extension !== ".exe"

View File

@@ -31,7 +31,7 @@ import { useProcesses } from "contexts/process";
import processDirectory from "contexts/process/directory";
import { basename, dirname, extname, isAbsolute, join } from "path";
import { useCallback, useEffect, useRef } from "react";
import { EMPTY_BUFFER, HOME, ONE_DAY_IN_MILLISECONDS } from "utils/constants";
import { HOME, ONE_DAY_IN_MILLISECONDS } from "utils/constants";
import { transcode } from "utils/ffmpeg";
import { getTZOffsetISOString } from "utils/functions";
import { convert } from "utils/imagemagick";
@@ -503,7 +503,7 @@ const useCommandInterpreter = (
if (file) {
const fullPath = getFullPath(file);
await writeFile(fullPath, EMPTY_BUFFER);
await writeFile(fullPath, Buffer.from(""));
updateFile(fullPath);
}
break;

View File

@@ -14,7 +14,7 @@ import { useFileSystem } from "contexts/fileSystem";
import { useProcesses } from "contexts/process";
import { basename, extname, join } from "path";
import { useCallback, useEffect, useRef, useState } from "react";
import { EMPTY_BUFFER, SAVE_PATH } from "utils/constants";
import { SAVE_PATH } from "utils/constants";
import { bufferToUrl, cleanUpBufferUrl, loadFiles } from "utils/functions";
const useV86 = (
@@ -64,11 +64,13 @@ const useV86 = (
[emulator, exists, mkdirRecursive, saveStateAsync, updateFolder, writeFile]
);
const loadDiskImage = useCallback(async () => {
if (!url) return;
const [currentUrl] = Object.keys(emulator);
if (currentUrl) await closeDiskImage(currentUrl);
const imageContents = url ? await readFile(url) : EMPTY_BUFFER;
const imageContents = await readFile(url);
const isISO = extname(url).toLowerCase() === ".iso";
const bufferUrl = bufferToUrl(imageContents);
const v86ImageConfig: V86ImageConfig = {

View File

@@ -12,7 +12,6 @@ import { extname, join } from "path";
import index from "public/.index/fs.9p.json";
import {
BASE_2D_CONTEXT_OPTIONS,
EMPTY_BUFFER,
FOLDER_ICON,
IMAGE_FILE_EXTENSIONS,
MOUNTED_FOLDER_ICON,
@@ -86,7 +85,7 @@ export const getIconFromIni = (
new Promise((resolve) => {
fs.readFile(
join(directory, "desktop.ini"),
(error, contents = EMPTY_BUFFER) => {
(error, contents = Buffer.from("")) => {
if (!error) {
const {
ShellClassInfo: { IconFile = "" },
@@ -187,7 +186,7 @@ export const getInfoWithExtension = (
});
if (extension === SHORTCUT_EXTENSION) {
fs.readFile(path, (error, contents = EMPTY_BUFFER) => {
fs.readFile(path, (error, contents = Buffer.from("")) => {
subIcons.push(SHORTCUT_ICON);
if (error) {
@@ -229,7 +228,7 @@ export const getInfoWithExtension = (
});
} else if (IMAGE_FILE_EXTENSIONS.has(extension)) {
getInfoByFileExtension("/System/Icons/photo.png", () =>
fs.readFile(path, (error, contents = EMPTY_BUFFER) => {
fs.readFile(path, (error, contents = Buffer.from("")) => {
if (!error && contents.length > 0) {
getInfoByFileExtension(bufferToUrl(contents));
}
@@ -238,7 +237,7 @@ export const getInfoWithExtension = (
} else if (VIDEO_FILE_EXTENSIONS.has(extension)) {
subIcons.push(processDirectory["VideoPlayer"].icon);
getInfoByFileExtension(processDirectory["VideoPlayer"].icon, () =>
fs.readFile(path, (error, contents = EMPTY_BUFFER) => {
fs.readFile(path, (error, contents = Buffer.from("")) => {
if (!error) {
const video = document.createElement("video");
@@ -270,7 +269,7 @@ export const getInfoWithExtension = (
getInfoByFileExtension(
`/System/Icons/${extensions[".mp3"].icon as string}.png`,
() =>
fs.readFile(path, (error, contents = EMPTY_BUFFER) => {
fs.readFile(path, (error, contents = Buffer.from("")) => {
if (!error) {
import("music-metadata-browser").then(
({ parseBuffer, selectCover }) =>

View File

@@ -24,7 +24,6 @@ import ini from "ini";
import { basename, dirname, extname, join, relative } from "path";
import { useCallback, useEffect, useState } from "react";
import {
EMPTY_BUFFER,
FOLDER_ICON,
INVALID_FILE_CHARACTERS,
MOUNTABLE_EXTENSIONS,
@@ -313,7 +312,7 @@ const useFolder = (
const pathExtension = extname(path).toLowerCase();
if (pathExtension === SHORTCUT_EXTENSION) {
fs?.readFile(path, (_readError, contents = EMPTY_BUFFER) =>
fs?.readFile(path, (_readError, contents = Buffer.from("")) =>
newPath(basename(path), contents)
);
} else {

View File

@@ -8,7 +8,7 @@ import { useFileSystem } from "contexts/fileSystem";
import { useMenu } from "contexts/menu";
import type { ContextMenuCapture } from "contexts/menu/useMenuContextState";
import { useCallback } from "react";
import { EMPTY_BUFFER, FOLDER_ICON, MENU_SEPERATOR } from "utils/constants";
import { FOLDER_ICON, MENU_SEPERATOR } from "utils/constants";
const NEW_FOLDER = "New folder";
const NEW_TEXT_DOCUMENT = "New Text Document.txt";
@@ -101,12 +101,12 @@ const useFolderContextMenu = (
},
MENU_SEPERATOR,
{
action: () => newPath(NEW_RTF_DOCUMENT, EMPTY_BUFFER, true),
action: () => newPath(NEW_RTF_DOCUMENT, Buffer.from(""), true),
icon: richTextDocumentIcon,
label: "Rich Text Document",
},
{
action: () => newPath(NEW_TEXT_DOCUMENT, EMPTY_BUFFER, true),
action: () => newPath(NEW_TEXT_DOCUMENT, Buffer.from(""), true),
icon: textDocumentIcon,
label: "Text Document",
},

View File

@@ -10,7 +10,6 @@ import FileSystemConfig from "contexts/fileSystem/FileSystemConfig";
import { supportsIndexedDB } from "contexts/fileSystem/functions";
import * as BrowserFS from "public/System/BrowserFS/browserfs.min.js";
import { useEffect, useMemo, useState } from "react";
import { EMPTY_BUFFER } from "utils/constants";
export type AsyncFS = {
exists: (path: string) => Promise<boolean>;
@@ -84,7 +83,7 @@ const useAsyncFs = (): AsyncFSModule => {
}),
readFile: (path) =>
new Promise((resolve, reject) => {
fs?.readFile(path, (error, data = EMPTY_BUFFER) => {
fs?.readFile(path, (error, data = Buffer.from("")) => {
if (!error) return resolve(data);
if (error.code === "EISDIR" && rootFs?.mntMap[path]?.data) {

View File

@@ -14,8 +14,6 @@ export const DEFAULT_WINDOW_SIZE: Size = {
width: 405,
};
export const EMPTY_BUFFER = Buffer.from("");
export const FOCUSABLE_ELEMENT = { tabIndex: -1 };
export const FS_HANDLES = "FileSystemAccessHandles";

View File

@@ -1,6 +1,5 @@
import type { LocalEcho } from "components/apps/Terminal/types";
import { basename, dirname, extname, join } from "path";
import { EMPTY_BUFFER } from "utils/constants";
import { loadFiles } from "utils/functions";
import type { ImageMagickConvertFile } from "utils/imagemagick/types";
@@ -35,7 +34,9 @@ export const convert = async (
returnFiles.push([
join(dirname(fileName), newName),
image.blob ? Buffer.from(await image.blob.arrayBuffer()) : EMPTY_BUFFER,
image.blob
? Buffer.from(await image.blob.arrayBuffer())
: Buffer.from(""),
]);
})
);

View File

@@ -1,6 +1,5 @@
import type { AsyncZipOptions, AsyncZippable, Unzipped } from "fflate";
import { join } from "path";
import { EMPTY_BUFFER } from "utils/constants";
const unRarLib = "/System/Unrar.js/unrar.wasm";
@@ -46,7 +45,7 @@ export const addFileToZip = async (
Buffer.from(
await zipAsync(
{
...(buffer !== EMPTY_BUFFER && (await unzipAsync(buffer))),
...(buffer.length > 0 && (await unzipAsync(buffer))),
...addFileToZippable(zipFilePath, await readFile(filePath)),
},
{ level: 0 }