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

@promise-watch/core

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@promise-watch/core - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

dist/utils/time.js

52

dist/execute.js

@@ -25,10 +25,11 @@ "use strict";

const glob_promise_1 = require("./utils/glob-promise");
const time_1 = require("./utils/time");
let alive = true;
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
let alive = true;
async function executeJobs({ dir, errorNotifiers = [], globPath = "runs/**/*.{js,ts}" }) {
async function fetchRuns(globPath, dir) {
const files = await (0, glob_promise_1.glob)(globPath);
const imports = await Promise.all(files.map(f => Promise.resolve().then(() => __importStar(require((0, path_1.resolve)(dir, "../", f))))));
const runs = imports.map((r, idx) => {
return imports.map((r, idx) => {
var _a;

@@ -41,21 +42,38 @@ return ({

});
async function sendErrorNotifications(title, body, customNotifiers) {
for (const notify of customNotifiers !== null && customNotifiers !== void 0 ? customNotifiers : errorNotifiers) {
await notify.send({ title, body });
}
async function sendNotifications(title, body, notifiers) {
for (const notify of notifiers) {
await notify.send({ title, body }).catch(console.error);
}
}
const errorMap = new Map();
async function recursiveRun(page, globalNotifiers = []) {
var _a;
const { name, run, options } = page;
const notifiers = (_a = options.notifiers) !== null && _a !== void 0 ? _a : globalNotifiers;
const errorStartTime = errorMap.get(name);
try {
await run();
if (errorStartTime) {
errorMap.delete(name);
const message = `Run is back online! was down for ${(0, time_1.millisecondsToStr)(errorStartTime.getTime())}`;
await sendNotifications(name, message, notifiers);
}
}
async function extracted({ name, run, options }) {
try {
await run();
catch (err) {
if (!errorStartTime) {
errorMap.set(name, new Date());
await sendNotifications(name, err.message, notifiers);
}
catch (err) {
await sendErrorNotifications(name, err.message, options.errorNotifiers);
}
if (alive) {
await sleep(options.interval * 1000);
await extracted({ name, run, options });
}
}
await Promise.allSettled(runs.map(run => extracted(run)));
if (alive) {
await sleep(options.interval * 1000);
await recursiveRun({ name, run, options }, notifiers);
}
}
async function executeJobs(options) {
const { dir, notifiers = [], globPath = "runs/**/*.{js,ts}", } = options;
const runs = await fetchRuns(globPath, dir);
await Promise.allSettled(runs.map(run => recursiveRun(run, notifiers)));
}
exports.executeJobs = executeJobs;

@@ -62,0 +80,0 @@ function shutdown() {

{
"name": "@promise-watch/core",
"version": "0.0.3",
"version": "0.0.4",
"main": "dist/index.js",

@@ -5,0 +5,0 @@ "author": "Jason Raimondi <jason@raimondi.us> (https://jasonraimondi.com)",

import { resolve } from "path";
import { glob } from "./utils/glob-promise";
import { Notifier } from "./notifications";
import { millisecondsToStr } from "./utils/time";

@@ -12,3 +13,3 @@ export type RunPage = {

export type RunOptions = {
errorNotifiers?: Notifier[];
notifiers?: Notifier[];
interval: number;

@@ -20,5 +21,7 @@ };

globPath?: string;
errorNotifiers?: Notifier[];
notifiers?: Notifier[];
};
let alive = true;
function sleep(ms: number) {

@@ -28,8 +31,6 @@ return new Promise(resolve => setTimeout(resolve, ms));

let alive = true;
export async function executeJobs({ dir, errorNotifiers = [], globPath = "runs/**/*.{js,ts}" }: ExecuteOptions) {
async function fetchRuns(globPath: string, dir: string) {
const files = await glob(globPath);
const imports = await Promise.all(files.map(f => import(resolve(dir, "../", f))));
const runs = imports.map((r: RunPage, idx) => ({
return imports.map((r: RunPage, idx) => ({
name: r.name ?? files[idx],

@@ -39,25 +40,51 @@ run: r.run,

}));
}
async function sendErrorNotifications(title: string, body: string, customNotifiers?: Notifier[]) {
for (const notify of customNotifiers ?? errorNotifiers) {
await notify.send({ title, body });
}
async function sendNotifications(title: string, body: string, notifiers: Notifier[]) {
for (const notify of notifiers) {
await notify.send({ title, body }).catch(console.error);
}
}
async function extracted({ name, run, options }: Required<RunPage>) {
try {
await run();
} catch (err) {
await sendErrorNotifications(name, err.message, options.errorNotifiers);
}
const errorMap = new Map<string, Date>();
if (alive) {
await sleep(options.interval * 1000);
await extracted({ name, run, options });
async function recursiveRun(page: Required<RunPage>, globalNotifiers: Notifier[] = []) {
const { name, run, options } = page;
const notifiers = options.notifiers ?? globalNotifiers;
const errorStartTime = errorMap.get(name);
try {
await run();
if (errorStartTime) {
errorMap.delete(name);
const message = `Run is back online! was down for ${millisecondsToStr(errorStartTime.getTime())}`;
await sendNotifications(name, message, notifiers);
}
} catch (err) {
// if we have already notified about the error,
// wait until success before sending another notification
if (!errorStartTime) {
errorMap.set(name, new Date());
await sendNotifications(name, err.message, notifiers);
}
}
await Promise.allSettled(runs.map(run => extracted(run)));
if (alive) {
await sleep(options.interval * 1000);
await recursiveRun({ name, run, options }, notifiers);
}
}
export async function executeJobs(options: ExecuteOptions) {
const {
dir,
notifiers = [],
globPath = "runs/**/*.{js,ts}",
} = options;
const runs = await fetchRuns(globPath, dir);
await Promise.allSettled(runs.map(run => recursiveRun(run, notifiers)));
}
function shutdown() {

@@ -64,0 +91,0 @@ alive = false;

Sorry, the diff of this file is not supported yet

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