@defer/client
Advanced tools
Comparing version 1.15.0 to 1.16.0-alpha-20240115132427-7e8cdb4
@@ -37,2 +37,3 @@ "use strict"; | ||
const utils_js_1 = require("./utils.js"); | ||
const local_js_1 = require("./local.js"); | ||
if ((0, utils_js_1.getEnv)("DEFER_TOKEN") === undefined) { | ||
@@ -67,34 +68,2 @@ console.log(` | ||
exports.deferEnabled = deferEnabled; | ||
async function execLocally(id, fn, args, shouldThrowErrors = false) { | ||
let state = "succeed"; | ||
let originalResult; | ||
let executionError; | ||
try { | ||
originalResult = await fn(...args); | ||
} | ||
catch (error) { | ||
executionError = error; | ||
state = "failed"; | ||
originalResult = { | ||
name: executionError.name, | ||
message: executionError.message, | ||
cause: executionError.cause, | ||
stack: executionError.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 }; | ||
exports.__database.set(id, response); | ||
if (executionError && shouldThrowErrors) { | ||
throw executionError; | ||
} | ||
return response; | ||
} | ||
async function enqueue(func, ...args) { | ||
@@ -133,3 +102,3 @@ const originalFunction = func.__fn; | ||
exports.__database.set(id, { id: id, state: "started" }); | ||
execLocally(id, originalFunction, functionArguments, true); | ||
(0, local_js_1.execLocally)(id, func, functionArguments, true); | ||
return { id }; | ||
@@ -198,2 +167,9 @@ } | ||
}; | ||
if (!getHTTPClient()) { | ||
wrapped.__localMetadata = { | ||
id: (0, utils_js_1.randomUUID)(), | ||
name: fn.name, | ||
}; | ||
(0, local_js_1.registerFunction)(wrapped); | ||
} | ||
return wrapped; | ||
@@ -208,2 +184,4 @@ } | ||
wrapped.__metadata = { | ||
id: (0, utils_js_1.randomUUID)(), | ||
name: fn.name, | ||
version: constants_js_1.INTERNAL_VERSION, | ||
@@ -279,3 +257,3 @@ retry: parseRetryPolicy(config), | ||
exports.__database.set(id, { id: id, state: "started" }); | ||
response = await execLocally(id, originalFunction, functionArguments); | ||
response = await (0, local_js_1.execLocally)(id, fn, functionArguments); | ||
} | ||
@@ -282,0 +260,0 @@ if (response.state === "failed") { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = "1.15.0"; | ||
exports.default = "1.16.0-alpha-20240115132427-7e8cdb4"; |
@@ -8,2 +8,3 @@ import parseDuration from "parse-duration"; | ||
import { debug, getEnv, randomUUID, sanitizeFunctionArguments, } from "./utils.js"; | ||
import { execLocally, registerFunction } from "./local.js"; | ||
if (getEnv("DEFER_TOKEN") === undefined) { | ||
@@ -37,34 +38,2 @@ console.log(` | ||
export const deferEnabled = () => !!getEnv("DEFER_TOKEN"); | ||
async function execLocally(id, fn, args, shouldThrowErrors = false) { | ||
let state = "succeed"; | ||
let originalResult; | ||
let executionError; | ||
try { | ||
originalResult = await fn(...args); | ||
} | ||
catch (error) { | ||
executionError = error; | ||
state = "failed"; | ||
originalResult = { | ||
name: executionError.name, | ||
message: executionError.message, | ||
cause: executionError.cause, | ||
stack: executionError.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); | ||
if (executionError && shouldThrowErrors) { | ||
throw executionError; | ||
} | ||
return response; | ||
} | ||
async function enqueue(func, ...args) { | ||
@@ -103,3 +72,3 @@ const originalFunction = func.__fn; | ||
__database.set(id, { id: id, state: "started" }); | ||
execLocally(id, originalFunction, functionArguments, true); | ||
execLocally(id, func, functionArguments, true); | ||
return { id }; | ||
@@ -168,2 +137,9 @@ } | ||
}; | ||
if (!getHTTPClient()) { | ||
wrapped.__localMetadata = { | ||
id: randomUUID(), | ||
name: fn.name, | ||
}; | ||
registerFunction(wrapped); | ||
} | ||
return wrapped; | ||
@@ -177,2 +153,4 @@ } | ||
wrapped.__metadata = { | ||
id: randomUUID(), | ||
name: fn.name, | ||
version: INTERNAL_VERSION, | ||
@@ -244,3 +222,3 @@ retry: parseRetryPolicy(config), | ||
__database.set(id, { id: id, state: "started" }); | ||
response = await execLocally(id, originalFunction, functionArguments); | ||
response = await execLocally(id, fn, functionArguments); | ||
} | ||
@@ -247,0 +225,0 @@ if (response.state === "failed") { |
@@ -1,1 +0,1 @@ | ||
export default "1.15.0"; | ||
export default "1.16.0-alpha-20240115132427-7e8cdb4"; |
{ | ||
"name": "@defer/client", | ||
"version": "1.15.0", | ||
"version": "1.16.0-alpha-20240115132427-7e8cdb4", | ||
"description": "Zero infrastructure NodeJS background jobs", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -24,2 +24,6 @@ import { Units } from "parse-duration"; | ||
} | ||
export interface LocalManifest { | ||
id: string; | ||
name: string; | ||
} | ||
export interface RetryPolicy { | ||
@@ -41,2 +45,3 @@ maxAttempts: number; | ||
__metadata: Manifest; | ||
__localMetadata?: LocalManifest; | ||
__fn: F; | ||
@@ -43,0 +48,0 @@ __execOptions?: ExecutionOptions; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "1.15.0"; | ||
declare const _default: "1.16.0-alpha-20240115132427-7e8cdb4"; | ||
export default _default; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76069
60
1693
2