@defer/client
Advanced tools
Comparing version 1.1.0 to 1.2.0-alpha-20230316211840-d9dcfae
@@ -8,5 +8,5 @@ "use strict"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const crypto_1 = require("crypto"); | ||
const parse_duration_1 = __importDefault(require("parse-duration")); | ||
const constants_js_1 = require("./constants.js"); | ||
const FakeID = "00000000000000000000000000000000"; | ||
const client_js_1 = require("./client.js"); | ||
@@ -16,2 +16,3 @@ const errors_js_1 = require("./errors.js"); | ||
const withDelay = (dt, delay) => new Date(dt.getTime() + (0, parse_duration_1.default)(delay)); | ||
const __database = new Map(); | ||
let __accessToken = process.env["DEFER_TOKEN"]; | ||
@@ -37,7 +38,38 @@ let __endpoint = "https://api.defer.run"; | ||
exports.deferEnabled = deferEnabled; | ||
async function execLocalhost(id, fn, args) { | ||
let state = "succeed"; | ||
let originalResult; | ||
try { | ||
originalResult = await fn(...args); | ||
} | ||
catch (error) { | ||
const e = error; | ||
state = "failed"; | ||
originalResult = { | ||
name: e.name, | ||
message: e.message, | ||
// @ts-expect-error cause field is typed | ||
cause: e.cause, | ||
stack: e.stack, | ||
}; | ||
} | ||
let result; | ||
try { | ||
result = JSON.parse(JSON.stringify(originalResult || "")); | ||
} | ||
catch (error) { | ||
const e = error; | ||
throw new errors_js_1.DeferError(`cannot serialize function return: ${e.message}`); | ||
} | ||
const response = { id, state, result }; | ||
__database.set(id, response); | ||
return response; | ||
} | ||
const getExecution = (id) => { | ||
if (!__httpClient) { | ||
throw new errors_js_1.DeferError("getExecution() is not yet supported in development mode; Please rely on deferEnabled() to conditionally use it."); | ||
} | ||
return (0, client_js_1.fetchExecution)(__httpClient, { id }); | ||
if (__httpClient) | ||
return (0, client_js_1.fetchExecution)(__httpClient, { id }); | ||
const response = __database.get(id); | ||
if (response) | ||
return response; | ||
throw new errors_js_1.APIError("execution not found", ""); | ||
}; | ||
@@ -66,4 +98,6 @@ exports.getExecution = getExecution; | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
await fn(...functionArguments); | ||
return { id: FakeID }; | ||
const id = (0, crypto_1.randomUUID)(); | ||
__database.set(id, { id: id, state: "running" }); | ||
execLocalhost(id, fn, functionArguments); | ||
return { id }; | ||
}; | ||
@@ -129,4 +163,6 @@ ret.__fn = fn; | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
await fn(...functionArguments); | ||
return { id: FakeID }; | ||
const id = (0, crypto_1.randomUUID)(); | ||
__database.set(id, { id: id, state: "running" }); | ||
execLocalhost(id, fn, functionArguments); | ||
return { id }; | ||
}; | ||
@@ -151,2 +187,3 @@ exports.delay = delay; | ||
} | ||
let response; | ||
if (__httpClient) { | ||
@@ -158,31 +195,21 @@ const { id } = await (0, client_js_1.enqueueExecution)(__httpClient, { | ||
}); | ||
const response = await (0, client_js_1.waitExecutionResult)(__httpClient, { id: id }); | ||
if (response.state === "failed") { | ||
let error = new errors_js_1.DeferError("Defer execution failed"); | ||
if (response.result?.message) { | ||
error = new errors_js_1.DeferError(response.result.message); | ||
error.stack = response.result.stack; | ||
} | ||
else if (response.result) { | ||
error = response.result; | ||
} | ||
throw error; | ||
} | ||
return response.result; | ||
response = await (0, client_js_1.waitExecutionResult)(__httpClient, { id: id }); | ||
} | ||
try { | ||
return Promise.resolve(await fn(...functionArguments)); | ||
else { | ||
const id = (0, crypto_1.randomUUID)(); | ||
__database.set(id, { id: id, state: "running" }); | ||
response = await execLocalhost(id, fn, functionArguments); | ||
} | ||
catch (error) { | ||
// const e = error as Error; | ||
let deferError = new Error("Defer execution failed"); | ||
if (error instanceof Error) { | ||
deferError = new Error(error.message); | ||
deferError.stack = error.stack || ""; | ||
if (response.state === "failed") { | ||
let error = new errors_js_1.DeferError("Defer execution failed"); | ||
if (response.result?.message) { | ||
error = new errors_js_1.DeferError(response.result.message); | ||
error.stack = response.result.stack; | ||
} | ||
else { | ||
deferError = error; | ||
else if (response.result) { | ||
error = response.result; | ||
} | ||
throw error; | ||
} | ||
return response.result; | ||
}; | ||
@@ -189,0 +216,0 @@ exports.awaitResult = awaitResult; |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { randomUUID } from "crypto"; | ||
import parseDuration from "parse-duration"; | ||
import { INTERNAL_VERSION } from "./constants.js"; | ||
const FakeID = "00000000000000000000000000000000"; | ||
import { enqueueExecution, fetchExecution, waitExecutionResult, } from "./client.js"; | ||
import { DeferError } from "./errors.js"; | ||
import { APIError, DeferError } from "./errors.js"; | ||
import { makeHTTPClient } from "./httpClient.js"; | ||
const withDelay = (dt, delay) => new Date(dt.getTime() + parseDuration(delay)); | ||
const __database = new Map(); | ||
let __accessToken = process.env["DEFER_TOKEN"]; | ||
@@ -27,7 +28,38 @@ let __endpoint = "https://api.defer.run"; | ||
export const deferEnabled = () => !!__accessToken; | ||
async function execLocalhost(id, fn, args) { | ||
let state = "succeed"; | ||
let originalResult; | ||
try { | ||
originalResult = await fn(...args); | ||
} | ||
catch (error) { | ||
const e = error; | ||
state = "failed"; | ||
originalResult = { | ||
name: e.name, | ||
message: e.message, | ||
// @ts-expect-error cause field is typed | ||
cause: e.cause, | ||
stack: e.stack, | ||
}; | ||
} | ||
let result; | ||
try { | ||
result = JSON.parse(JSON.stringify(originalResult || "")); | ||
} | ||
catch (error) { | ||
const e = error; | ||
throw new DeferError(`cannot serialize function return: ${e.message}`); | ||
} | ||
const response = { id, state, result }; | ||
__database.set(id, response); | ||
return response; | ||
} | ||
export const getExecution = (id) => { | ||
if (!__httpClient) { | ||
throw new DeferError("getExecution() is not yet supported in development mode; Please rely on deferEnabled() to conditionally use it."); | ||
} | ||
return fetchExecution(__httpClient, { id }); | ||
if (__httpClient) | ||
return fetchExecution(__httpClient, { id }); | ||
const response = __database.get(id); | ||
if (response) | ||
return response; | ||
throw new APIError("execution not found", ""); | ||
}; | ||
@@ -55,4 +87,6 @@ export const defer = (fn, options) => { | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
await fn(...functionArguments); | ||
return { id: FakeID }; | ||
const id = randomUUID(); | ||
__database.set(id, { id: id, state: "running" }); | ||
execLocalhost(id, fn, functionArguments); | ||
return { id }; | ||
}; | ||
@@ -117,4 +151,6 @@ ret.__fn = fn; | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
await fn(...functionArguments); | ||
return { id: FakeID }; | ||
const id = randomUUID(); | ||
__database.set(id, { id: id, state: "running" }); | ||
execLocalhost(id, fn, functionArguments); | ||
return { id }; | ||
}; | ||
@@ -138,2 +174,3 @@ /** | ||
} | ||
let response; | ||
if (__httpClient) { | ||
@@ -145,31 +182,21 @@ const { id } = await enqueueExecution(__httpClient, { | ||
}); | ||
const response = await waitExecutionResult(__httpClient, { id: id }); | ||
if (response.state === "failed") { | ||
let error = new DeferError("Defer execution failed"); | ||
if (response.result?.message) { | ||
error = new DeferError(response.result.message); | ||
error.stack = response.result.stack; | ||
} | ||
else if (response.result) { | ||
error = response.result; | ||
} | ||
throw error; | ||
} | ||
return response.result; | ||
response = await waitExecutionResult(__httpClient, { id: id }); | ||
} | ||
try { | ||
return Promise.resolve(await fn(...functionArguments)); | ||
else { | ||
const id = randomUUID(); | ||
__database.set(id, { id: id, state: "running" }); | ||
response = await execLocalhost(id, fn, functionArguments); | ||
} | ||
catch (error) { | ||
// const e = error as Error; | ||
let deferError = new Error("Defer execution failed"); | ||
if (error instanceof Error) { | ||
deferError = new Error(error.message); | ||
deferError.stack = error.stack || ""; | ||
if (response.state === "failed") { | ||
let error = new DeferError("Defer execution failed"); | ||
if (response.result?.message) { | ||
error = new DeferError(response.result.message); | ||
error.stack = response.result.stack; | ||
} | ||
else { | ||
deferError = error; | ||
else if (response.result) { | ||
error = response.result; | ||
} | ||
throw error; | ||
} | ||
return response.result; | ||
}; | ||
@@ -176,0 +203,0 @@ // EXAMPLES: |
{ | ||
"name": "@defer/client", | ||
"version": "1.1.0", | ||
"version": "1.2.0-alpha-20230316211840-d9dcfae", | ||
"description": "Zero infrastructure NodeJS background jobs", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
import { Units } from "parse-duration"; | ||
import { EnqueueExecutionResponse } from "./client.js"; | ||
export type { FetchExecutionResponse } from "./client"; | ||
import { EnqueueExecutionResponse, ExecutionState, FetchExecutionResponse } from "./client.js"; | ||
interface Options { | ||
@@ -11,3 +10,7 @@ accessToken?: string; | ||
export declare const deferEnabled: () => boolean; | ||
export declare const getExecution: (id: string) => Promise<import("./client.js").FetchExecutionResponse>; | ||
export declare const getExecution: (id: string) => Promise<FetchExecutionResponse> | { | ||
id: string; | ||
state: ExecutionState; | ||
result?: any; | ||
}; | ||
export declare type UnPromise<F> = F extends Promise<infer R> ? R : F; | ||
@@ -72,1 +75,2 @@ export declare type DelayString = `${string}${Units}`; | ||
export declare const awaitResult: DeferAwaitResult; | ||
export {}; |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
39439
926
2