Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@squared-functions/module

Package Overview
Dependencies
Maintainers
1
Versions
355
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@squared-functions/module - npm Package Compare versions

Comparing version 0.12.0 to 0.13.0

3

index.d.ts

@@ -5,3 +5,2 @@ import type { ModuleConstructor } from '../types/lib';

export = Module;
export as namespace Module;
export = Module;

@@ -1,2 +0,2 @@

/* @squared-functions/module 0.12.0
/* @squared-functions/module 0.13.0
https://github.com/anpham6/squared-functions */

@@ -11,4 +11,2 @@

const chalk = require("chalk");
let SETTINGS = {};
// eslint-disable-next-line no-shadow
var LOG_TYPE;

@@ -26,90 +24,21 @@ (function (LOG_TYPE) {

})(LOG_TYPE = exports.LOG_TYPE || (exports.LOG_TYPE = {}));
const Module = class {
let SETTINGS = {};
function allSettled(values) {
return Promise.all(values.map((promise) => promise.then(value => ({ status: 'fulfilled', value })).catch(reason => ({ status: 'rejected', reason }))));
}
function applyFailStyle(options = {}) {
for (const attr in Module.LOG_STYLE_FAIL) {
if (!(attr in options)) {
options[attr] || (options[attr] = Module.LOG_STYLE_FAIL[attr]);
}
}
return options;
}
class Module {
constructor() {
this.tempDir = 'tmp';
this.errors = [];
[this.major, this.minor, this.patch] = process.version.substring(1).split('.').map(value => +value);
}
static loadSettings(value) {
if (value.logger) {
SETTINGS = value.logger;
}
}
static getFileSize(localUri) {
try {
return fs.statSync(localUri).size;
}
catch (_a) {
}
return 0;
}
static toPosix(value, filename) {
return value ? value.replace(/\\+/g, '/').replace(/\/+$/, '') + (filename ? '/' + filename : '') : '';
}
static renameExt(value, ext) {
const index = value.lastIndexOf('.');
return (index !== -1 ? value.substring(0, index) : value) + '.' + ext;
}
static isLocalPath(value) {
return /^\.?\.[\\/]/.test(value);
}
static fromSameOrigin(value, other) {
return new URL(value).origin === new URL(other).origin;
}
supported(major, minor, patch = 0) {
if (this.major < major) {
return false;
}
else if (this.major === major) {
if (this.minor < minor) {
return false;
}
else if (this.minor === minor) {
return this.patch >= patch;
}
return true;
}
return true;
}
parseFunction(value) {
if (Module.isLocalPath(value = value.trim())) {
try {
value = fs.readFileSync(path.resolve(value), 'utf8').trim();
}
catch (err) {
this.writeFail(['Could not load function', value], err);
return null;
}
}
return value.startsWith('function') ? eval(`(${value})`) : null;
}
getTempDir(subDir, filename = '') {
return process.cwd() + path.sep + this.tempDir + path.sep + (subDir ? uuid.v4() + path.sep : '') + (filename.startsWith('.') ? uuid.v4() : '') + filename;
}
joinPosix(...paths) {
paths = paths.filter(value => value && value.trim());
let result = '';
for (let i = 0; i < paths.length; ++i) {
const trailing = paths[i].replace(/\\+/g, '/');
if (i === 0) {
result = trailing;
}
else {
const leading = paths[i - 1];
result += (leading && trailing && !leading.endsWith('/') && !trailing.startsWith('/') ? '/' : '') + trailing;
}
}
return result;
}
writeTimeElapsed(title, value, time, options = {}) {
this.formatMessage(LOG_TYPE.TIME_ELAPSED, title, ['Completed', (Date.now() - time) / 1000 + 's'], value, options);
}
writeFail(value, message) {
this.formatFail(LOG_TYPE.SYSTEM, 'FAIL', value, message);
}
formatFail(type, title, value, message, options = {}) {
options.titleColor || (options.titleColor = 'white');
options.titleBgColor || (options.titleBgColor = 'bgRed');
this.formatMessage(type, title, value, message, options);
}
formatMessage(type, title, value, message, options = {}) {
static formatMessage(type, title, value, message, options = {}) {
switch (type) {

@@ -168,4 +97,4 @@ case LOG_TYPE.SYSTEM:

if (Array.isArray(value)) {
const length = value[1] ? value[1].length : 0;
if (length) {
let length = 0;
if (value[1] && (length = value[1].length)) {
const formatHint = (hint) => {

@@ -190,5 +119,2 @@ const { hintColor, hintBgColor } = options;

}
this.writeMessage(title.padEnd(6), value, message, options);
}
writeMessage(title, value, message, options = {}) {
const { titleColor = 'green', titleBgColor = 'bgBlack', valueColor, valueBgColor, messageColor, messageBgColor } = options;

@@ -210,8 +136,209 @@ if (valueColor) {

}
console.log(chalk[titleBgColor].bold[titleColor](title.toUpperCase()) + chalk.blackBright(':') + ' ' + value + (message || ''));
console.log(chalk[titleBgColor].bold[titleColor](title.toUpperCase().padEnd(7)) + chalk.blackBright(':') + ' ' + value + (message || '')); // eslint-disable-line no-console
}
static writeFail(value, message) {
this.formatMessage(LOG_TYPE.SYSTEM, 'FAIL', value, message, applyFailStyle());
}
static parseFunction(value, name) {
const uri = Module.fromLocalPath(value = value.trim());
if (uri) {
try {
value = fs.readFileSync(uri, 'utf8').trim();
}
catch (err) {
this.writeFail(['Could not load function', value], err);
return;
}
}
if (value.startsWith('function')) {
return (0, eval)(`(${value})`);
}
if (name) {
try {
const handler = require(value);
if (typeof handler === 'function' && handler.name === name) {
return handler;
}
}
catch (_a) {
}
}
}
static toPosix(value, filename) {
return value ? value.replace(/\\+/g, '/').replace(/\/+$/, '') + (filename ? '/' + filename : '') : '';
}
static renameExt(value, ext) {
const index = value.lastIndexOf('.');
return (index !== -1 ? value.substring(0, index) : value) + (ext[0] === ':' ? ext + path.extname(value) : '.' + ext);
}
static fromLocalPath(value) {
return /^\.?\.?[\\/]/.test(value = value.trim()) ? value[0] !== '.' ? path.join(process.cwd(), value) : path.resolve(value) : '';
}
static hasSameOrigin(value, other) {
try {
return new URL(value).origin === new URL(other).origin;
}
catch (_a) {
}
return false;
}
static isFileHTTP(value) {
return /^https?:\/\/[^/]/i.test(value);
}
static isFileUNC(value) {
return /^\\\\([\w.-]+)\\([\w-]+\$?)((?<=\$)(?:[^\\]*|\\.+)|\\.+)$/.test(value);
}
static isDirectoryUNC(value) {
return /^\\\\([\w.-]+)\\([\w-]+\$|[\w-]+\$\\.+|[\w-]+\\.*)$/.test(value);
}
static isUUID(value) {
return /^[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}$/.test(value);
}
static resolveUri(value) {
if (value.startsWith('file://')) {
try {
let url = new URL(value).pathname;
if (path.isAbsolute(url)) {
if (path.sep === '\\' && /^\/[A-Za-z]:\//.test(url)) {
url = url.substring(1);
}
return path.resolve(url);
}
}
catch (_a) {
}
return '';
}
return value;
}
static resolvePath(value, href) {
if ((value = value.trim()).startsWith('http')) {
return value;
}
if (href.startsWith('http')) {
try {
const url = new URL(href);
const origin = url.origin;
const pathname = url.pathname.split('/');
--pathname.length;
value = value.replace(/\\/g, '/');
if (value[0] === '/') {
return origin + value;
}
else if (value.startsWith('../')) {
const trailing = [];
for (const dir of value.split('/')) {
if (dir === '..') {
if (trailing.length === 0) {
pathname.pop();
}
else {
--trailing.length;
}
}
else {
trailing.push(dir);
}
}
value = trailing.join('/');
}
return Module.joinPosix(origin, pathname.join('/'), value);
}
catch (_a) {
}
}
return '';
}
static joinPosix(...values) {
values = values.filter(value => value && value.trim().replace(/\\+/g, '/'));
let result = '';
for (let i = 0; i < values.length; ++i) {
const trailing = values[i];
if (i === 0) {
result = trailing;
}
else {
const leading = values[i - 1];
result += (leading && trailing && !leading.endsWith('/') && trailing[0] !== '/' ? '/' : '') + trailing;
}
}
return result;
}
static getFileSize(value) {
try {
return fs.statSync(value).size;
}
catch (_a) {
}
return 0;
}
static responseError(err, hint) {
return {
success: false,
error: {
hint,
message: err instanceof Error ? err.message : err.toString()
}
};
}
static allSettled(values, rejected, errors) {
const promise = Promise.allSettled ? Promise.allSettled(values) : allSettled(values);
if (rejected) {
promise.then(result => {
for (const item of result) {
if (item.status === 'rejected' && item.reason) {
this.writeFail(rejected, item.reason);
if (errors) {
errors.push(item.reason.toString());
}
}
}
});
}
return promise;
}
static loadSettings(value) {
if (value.logger) {
SETTINGS = value.logger;
}
}
supported(major, minor, patch = 0) {
if (this.major < major) {
return false;
}
else if (this.major === major) {
if (this.minor < minor) {
return false;
}
else if (this.minor === minor) {
return this.patch >= patch;
}
return true;
}
return true;
}
getTempDir(uuidDir, filename = '') {
return process.cwd() + path.sep + this.tempDir + path.sep + (uuidDir ? uuid.v4() + path.sep : '') + (filename[0] === '.' ? uuid.v4() : '') + filename;
}
writeFail(value, message) {
this.formatFail(LOG_TYPE.SYSTEM, ' FAIL! ', value, message);
}
writeTimeElapsed(title, value, time, options) {
Module.formatMessage(LOG_TYPE.TIME_ELAPSED, title, ['Completed', (Date.now() - time) / 1000 + 's'], value, options);
}
formatFail(type, title, value, message, options) {
Module.formatMessage(type, title, value, message, applyFailStyle(options));
if (message) {
this.errors.push(message instanceof Error ? message.message : message.toString());
}
}
formatMessage(type, title, value, message, options) {
Module.formatMessage(type, title, value, message, options);
}
get logType() {
return LOG_TYPE;
}
};
}
Module.LOG_TYPE = LOG_TYPE;
Module.LOG_STYLE_FAIL = { titleColor: 'white', titleBgColor: 'bgRed' };
if (typeof module !== 'undefined' && module.exports) {

@@ -218,0 +345,0 @@ module.exports = Module;

{
"name": "@squared-functions/module",
"version": "0.12.0",
"version": "0.13.0",
"description": "Module extension class for squared-functions",

@@ -18,3 +18,3 @@ "main": "index.js",

"dependencies": {
"@squared-functions/types": "^0.12.0",
"@squared-functions/types": "^0.13.0",
"uuid": "^8.3.2",

@@ -21,0 +21,0 @@ "chalk": "^4.1.0"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc