js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
240 lines (239 loc) • 8.13 kB
JavaScript
/**
* Environment detection and cross-platform utilities
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.pathUtils = exports.fileSystem = exports.isNode = exports.isBrowser = void 0;
/**
* Check if code is running in a browser environment
*/
exports.isBrowser = typeof window !== "undefined";
/**
* Check if code is running in a Node.js environment
*/
exports.isNode = !exports.isBrowser &&
typeof process !== "undefined" &&
typeof process.versions !== "undefined" &&
typeof process.versions.node !== "undefined";
/**
* File system utilities that work in both environments
*/
exports.fileSystem = {
/**
* Read a file asynchronously
* @param path Path to the file
* @returns Promise resolving to the file contents as a string
*/
readFile: async (path) => {
if (exports.isNode) {
// Node.js implementation
const fs = await Promise.resolve().then(() => __importStar(require("node:fs/promises")));
return fs.readFile(path, "utf-8");
}
// Browser implementation - fetch from URL
const response = await fetch(path);
if (!response.ok) {
throw new Error(`Failed to fetch ${path}: ${response.status} ${response.statusText}`);
}
return response.text();
},
/**
* Read a file synchronously
* @param path Path to the file
* @returns File contents as a string
*/
readFileSync: (path) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require("node:fs");
return fs.readFileSync(path, "utf-8");
}
throw new Error("Synchronous file reading is not supported in browsers");
},
/**
* Write a file asynchronously
* @param path Path to the file
* @param data Data to write
* @returns Promise resolving when the file is written
*/
writeFile: async (path, data) => {
if (exports.isNode) {
// Node.js implementation
const fs = await Promise.resolve().then(() => __importStar(require("node:fs/promises")));
return fs.writeFile(path, data);
}
// Browser implementation - download file
const blob = new Blob([data], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = path.split("/").pop() || "download";
document.body.appendChild(a);
a.click();
setTimeout(() => {
if (document?.body) {
document.body.removeChild(a);
}
URL.revokeObjectURL(url);
}, 100);
},
/**
* Write a file synchronously
* @param path Path to the file
* @param data Data to write
*/
writeFileSync: (path, data) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require("node:fs");
fs.writeFileSync(path, data);
}
else {
throw new Error("Synchronous file writing is not supported in browsers");
}
},
/**
* Check if a file exists asynchronously
* @param path Path to the file
* @returns Promise resolving to true if the file exists, false otherwise
*/
exists: async (path) => {
if (exports.isNode) {
// Node.js implementation
const fs = await Promise.resolve().then(() => __importStar(require("node:fs/promises")));
try {
await fs.access(path);
return true;
}
catch {
return false;
}
}
else {
// Browser implementation - try to fetch
try {
const response = await fetch(path, { method: "HEAD" });
return response.ok;
}
catch {
return false;
}
}
},
/**
* Check if a file exists synchronously
* @param path Path to the file
* @returns True if the file exists, false otherwise
*/
existsSync: (path) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require("node:fs");
return fs.existsSync(path);
}
throw new Error("Synchronous file existence check is not supported in browsers");
},
};
/**
* Path utilities that work in both environments
*/
exports.pathUtils = {
/**
* Join path segments
* @param paths Path segments to join
* @returns Joined path
*/
join: (...paths) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("node:path");
return path.join(...paths);
}
// Browser implementation
return paths.join("/").replace(/\/+/g, "/");
},
/**
* Get the directory name of a path
* @param path Path
* @returns Directory name
*/
dirname: (path) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodePath = require("node:path");
return nodePath.dirname(path);
}
// Browser implementation
return path.split("/").slice(0, -1).join("/") || ".";
},
/**
* Get the base name of a path
* @param path Path
* @returns Base name
*/
basename: (path) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodePath = require("node:path");
return nodePath.basename(path);
}
// Browser implementation
return path.split("/").pop() || "";
},
/**
* Get the extension of a path
* @param path Path
* @returns Extension
*/
extname: (path) => {
if (exports.isNode) {
// Node.js implementation
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nodePath = require("node:path");
return nodePath.extname(path);
}
// Browser implementation
const basename = path.split("/").pop() || "";
const dotIndex = basename.lastIndexOf(".");
return dotIndex === -1 ? "" : basename.slice(dotIndex);
},
};
;