@stryke/fs
Version:
A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.
1 lines • 4.41 kB
Source Map (JSON)
{"version":3,"file":"buffer.mjs","names":[],"sources":["../src/buffer.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/stryke\n Documentation: https://docs.stormsoftware.com/projects/stryke\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { findFilePath } from \"@stryke/path\";\nimport { Buffer } from \"node:buffer\";\nimport { existsSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { readFile, writeFile } from \"node:fs/promises\";\nimport { createDirectory, createDirectorySync } from \"./helpers\";\n\n/**\n * Reads a file from the given path and returns its content as an ArrayBuffer. The file is expected to be located relative to the current directory of this module.\n *\n * @param filePath - The path to the file to read.\n * @returns The content of the file as an ArrayBuffer.\n */\nexport async function readFileBuffer(filePath: string): Promise<ArrayBuffer> {\n if (!filePath) {\n throw new Error(\"No file path provided to read data\");\n }\n if (!existsSync(filePath)) {\n throw new Error(`File does not exist at path: ${filePath}`);\n }\n\n const b = await readFile(filePath);\n\n return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);\n}\n\n/**\n * Reads a file from the given path and returns its content as an ArrayBuffer. The file is expected to be located relative to the current directory of this module.\n *\n * @param filePath - The path to the file to read.\n * @returns The content of the file as an ArrayBuffer.\n */\nexport function readFileBufferSync(filePath: string): ArrayBuffer {\n if (!filePath) {\n throw new Error(\"No file path provided to read data\");\n }\n if (!existsSync(filePath)) {\n throw new Error(`File does not exist at path: ${filePath}`);\n }\n\n const b = readFileSync(filePath);\n\n return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);\n}\n\n/**\n * Writes an ArrayBuffer to a file at the specified path.\n *\n * @param filePath - The path to the file where the data should be written.\n * @param data - The ArrayBuffer containing the data to write.\n */\nexport async function writeFileBuffer(filePath: string, data: ArrayBuffer) {\n if (!filePath) {\n throw new Error(\"No file path provided to write data\");\n }\n if (!existsSync(findFilePath(filePath))) {\n await createDirectory(findFilePath(filePath));\n }\n\n await writeFile(filePath, Buffer.from(data));\n}\n\n/**\n * Writes an ArrayBuffer to a file at the specified path.\n *\n * @param filePath - The path to the file where the data should be written.\n * @param data - The ArrayBuffer containing the data to write.\n */\nexport function writeFileBufferSync(filePath: string, data: ArrayBuffer) {\n if (!filePath) {\n throw new Error(\"No file path provided to write data\");\n }\n if (!existsSync(findFilePath(filePath))) {\n createDirectorySync(findFilePath(filePath));\n }\n\n writeFileSync(filePath, Buffer.from(data));\n}\n"],"mappings":";;;;;;;;;;;;;AA8BA,eAAsB,eAAe,UAAwC;AAC3E,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,qCAAqC;AAEvD,KAAI,CAAC,WAAW,SAAS,CACvB,OAAM,IAAI,MAAM,gCAAgC,WAAW;CAG7D,MAAM,IAAI,MAAM,SAAS,SAAS;AAElC,QAAO,EAAE,OAAO,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW;;;;;;;;AASlE,SAAgB,mBAAmB,UAA+B;AAChE,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,qCAAqC;AAEvD,KAAI,CAAC,WAAW,SAAS,CACvB,OAAM,IAAI,MAAM,gCAAgC,WAAW;CAG7D,MAAM,IAAI,aAAa,SAAS;AAEhC,QAAO,EAAE,OAAO,MAAM,EAAE,YAAY,EAAE,aAAa,EAAE,WAAW;;;;;;;;AASlE,eAAsB,gBAAgB,UAAkB,MAAmB;AACzE,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,sCAAsC;AAExD,KAAI,CAAC,WAAW,aAAa,SAAS,CAAC,CACrC,OAAM,gBAAgB,aAAa,SAAS,CAAC;AAG/C,OAAM,UAAU,UAAU,OAAO,KAAK,KAAK,CAAC;;;;;;;;AAS9C,SAAgB,oBAAoB,UAAkB,MAAmB;AACvE,KAAI,CAAC,SACH,OAAM,IAAI,MAAM,sCAAsC;AAExD,KAAI,CAAC,WAAW,aAAa,SAAS,CAAC,CACrC,qBAAoB,aAAa,SAAS,CAAC;AAG7C,eAAc,UAAU,OAAO,KAAK,KAAK,CAAC"}