@defer/client
Advanced tools
Comparing version 1.9.0-alpha-20230817160229-01d1aae to 1.9.0-alpha-20230818134629-71de74e
150
cjs/index.js
"use strict"; | ||
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 (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -6,33 +29,24 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.awaitResult = exports.addMetadata = exports.delay = exports.defer = exports.deferEnabled = exports.configure = exports.__httpClient = exports.__database = exports.cancelExecution = exports.getExecution = void 0; | ||
exports.cancelExecution = exports.getExecution = exports.awaitResult = exports.addMetadata = exports.delay = exports.defer = exports.deferEnabled = exports.__database = void 0; | ||
const parse_duration_1 = __importDefault(require("parse-duration")); | ||
const uuid_1 = require("uuid"); | ||
const constants_js_1 = require("./constants.js"); | ||
const client_js_1 = require("./client.js"); | ||
const client = __importStar(require("./client.js")); | ||
const errors_js_1 = require("./errors.js"); | ||
const httpClient_js_1 = require("./httpClient.js"); | ||
var getExecution_js_1 = require("./getExecution.js"); | ||
Object.defineProperty(exports, "getExecution", { enumerable: true, get: function () { return getExecution_js_1.getExecution; } }); | ||
var cancelExecution_js_1 = require("./cancelExecution.js"); | ||
Object.defineProperty(exports, "cancelExecution", { enumerable: true, get: function () { return cancelExecution_js_1.cancelExecution; } }); | ||
const withDelay = (dt, delay) => new Date(dt.getTime() + (0, parse_duration_1.default)(delay)); | ||
exports.__database = new Map(); | ||
let __accessToken = process.env["DEFER_TOKEN"]; | ||
let __endpoint = process.env["DEFER_ENDPOINT"] || "https://api.defer.run"; | ||
let __verbose = false; | ||
if (__accessToken) | ||
exports.__httpClient = (0, httpClient_js_1.makeHTTPClient)(__endpoint, __accessToken); | ||
function configure(opts = {}) { | ||
if (opts.accessToken) | ||
__accessToken = opts.accessToken; | ||
if (opts.endpoint) | ||
__endpoint = opts.endpoint; | ||
if (opts.verbose) | ||
__verbose = opts.verbose; | ||
if (__accessToken) | ||
exports.__httpClient = (0, httpClient_js_1.makeHTTPClient)(__endpoint, __accessToken); | ||
function debug(...args) { | ||
if (process?.env["DEFER_DEBUG"]) { | ||
console.debug(...args); | ||
} | ||
} | ||
function getHTTPClient() { | ||
const accessToken = process?.env["DEFER_TOKEN"]; | ||
const endpoint = process?.env["DEFER_ENDPOINT"] || "https://api.defer.run"; | ||
if (accessToken) | ||
return (0, httpClient_js_1.makeHTTPClient)(accessToken, endpoint); | ||
return; | ||
} | ||
exports.configure = configure; | ||
const deferEnabled = () => !!__accessToken; | ||
const deferEnabled = () => !!process?.env["DEFER_TOKEN"]; | ||
exports.deferEnabled = deferEnabled; | ||
@@ -118,4 +132,3 @@ async function execLocally(id, fn, args) { | ||
const ret = async (...args) => { | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
let functionArguments; | ||
@@ -129,4 +142,5 @@ try { | ||
} | ||
if (exports.__httpClient) { | ||
return (0, client_js_1.enqueueExecution)(exports.__httpClient, { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -138,4 +152,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = (0, uuid_1.v4)(); | ||
@@ -188,5 +201,5 @@ exports.__database.set(id, { id: id, state: "started" }); | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
if (exports.__httpClient) { | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
let scheduleFor; | ||
@@ -200,3 +213,3 @@ if (delay instanceof Date) { | ||
} | ||
return (0, client_js_1.enqueueExecution)(exports.__httpClient, { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -208,4 +221,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = (0, uuid_1.v4)(); | ||
@@ -244,5 +256,5 @@ exports.__database.set(id, { id: id, state: "started" }); | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
if (exports.__httpClient) { | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
let scheduleFor; | ||
@@ -260,3 +272,3 @@ const delay = deferFn.__execOptions?.delay; | ||
} | ||
return (0, client_js_1.enqueueExecution)(exports.__httpClient, { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -268,4 +280,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = (0, uuid_1.v4)(); | ||
@@ -303,4 +314,5 @@ exports.__database.set(id, { id: id, state: "started" }); | ||
let response; | ||
if (exports.__httpClient) { | ||
const { id } = await (0, client_js_1.enqueueExecution)(exports.__httpClient, { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
const { id } = await client.enqueueExecution(httpClient, { | ||
name: fnName, | ||
@@ -311,3 +323,3 @@ arguments: functionArguments, | ||
}); | ||
response = await (0, client_js_1.waitExecutionResult)(exports.__httpClient, { id: id }); | ||
response = await client.waitExecutionResult(httpClient, { id: id }); | ||
} | ||
@@ -333,32 +345,22 @@ else { | ||
exports.awaitResult = awaitResult; | ||
// EXAMPLES: | ||
// interface Contact { | ||
// id: string; | ||
// name: string; | ||
// } | ||
// const importContacts = (companyId: string, contacts: Contact[]) => { | ||
// return new Promise<{ imported: number; companyId: string }>((resolve) => { | ||
// console.log(`Start importing contacts for company#${companyId}`); | ||
// setTimeout(() => { | ||
// console.log(contacts); | ||
// console.log("Done."); | ||
// resolve({ imported: 10000, companyId }); | ||
// }, 5000); | ||
// }); | ||
// }; | ||
// const importContactsD = defer(importContacts); | ||
// async function myFunction() { | ||
// return 1; | ||
// } | ||
// defer.cron(myFunction, "every day"); | ||
// async function test() { | ||
// await importContactsD("1", []); // fire and forget | ||
// const r = await importContactsD.await("1", []); // wait for execution result | ||
// const awaitImportContact = awaitResult(importContactsD); | ||
// const result = await awaitImportContact("1", []); | ||
// } | ||
// // Delayed | ||
// const delayed = delay(importContactsD, "1h"); | ||
// delayed("", []); | ||
// // Retry options | ||
// const importContactsRetried = defer(importContacts, { retry: 3 }); | ||
async function getExecution(id) { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) | ||
return client.fetchExecution(httpClient, { id }); | ||
console.log("getExecution", id); | ||
const response = exports.__database.get(id); | ||
if (response) | ||
return Promise.resolve({ | ||
...response, | ||
state: response.state, | ||
}); | ||
throw new errors_js_1.APIError("execution not found", "not found"); | ||
} | ||
exports.getExecution = getExecution; | ||
async function cancelExecution(id, force = false) { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) | ||
return client.cancelExecution(httpClient, { id, force }); | ||
return {}; | ||
} | ||
exports.cancelExecution = cancelExecution; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.asNextRoute = void 0; | ||
const getExecution_js_1 = require("../getExecution.js"); | ||
const index_js_1 = require("../index.js"); | ||
const errors_js_1 = require("../errors.js"); | ||
@@ -14,3 +14,3 @@ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/52841#issuecomment-1574002759 | ||
try { | ||
const { state, result } = await (0, getExecution_js_1.getExecution)(id); | ||
const { state, result } = await (0, index_js_1.getExecution)(id); | ||
return ResponseJSON({ id, state, result }); | ||
@@ -17,0 +17,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = "1.9.0-alpha-20230817160229-01d1aae"; | ||
exports.default = "1.9.0-alpha-20230818134629-71de74e"; |
123
esm/index.js
import parseDuration from "parse-duration"; | ||
import { v4 as randomUUID } from "uuid"; | ||
import { INTERNAL_VERSION, RETRY_MAX_ATTEMPTS_PLACEHOLDER, } from "./constants.js"; | ||
import { enqueueExecution, waitExecutionResult, } from "./client.js"; | ||
import { DeferError } from "./errors.js"; | ||
import * as client from "./client.js"; | ||
import { APIError, DeferError } from "./errors.js"; | ||
import { makeHTTPClient } from "./httpClient.js"; | ||
export { getExecution } from "./getExecution.js"; | ||
export { cancelExecution } from "./cancelExecution.js"; | ||
const withDelay = (dt, delay) => new Date(dt.getTime() + parseDuration(delay)); | ||
export const __database = new Map(); | ||
let __accessToken = process.env["DEFER_TOKEN"]; | ||
let __endpoint = process.env["DEFER_ENDPOINT"] || "https://api.defer.run"; | ||
let __verbose = false; | ||
export let __httpClient; | ||
if (__accessToken) | ||
__httpClient = makeHTTPClient(__endpoint, __accessToken); | ||
export function configure(opts = {}) { | ||
if (opts.accessToken) | ||
__accessToken = opts.accessToken; | ||
if (opts.endpoint) | ||
__endpoint = opts.endpoint; | ||
if (opts.verbose) | ||
__verbose = opts.verbose; | ||
if (__accessToken) | ||
__httpClient = makeHTTPClient(__endpoint, __accessToken); | ||
function debug(...args) { | ||
if (process?.env["DEFER_DEBUG"]) { | ||
console.debug(...args); | ||
} | ||
} | ||
function getHTTPClient() { | ||
const accessToken = process?.env["DEFER_TOKEN"]; | ||
const endpoint = process?.env["DEFER_ENDPOINT"] || "https://api.defer.run"; | ||
if (accessToken) | ||
return makeHTTPClient(accessToken, endpoint); | ||
return; | ||
} | ||
export const deferEnabled = () => !!__accessToken; | ||
export const deferEnabled = () => !!process?.env["DEFER_TOKEN"]; | ||
async function execLocally(id, fn, args) { | ||
@@ -108,4 +101,3 @@ let state = "succeed"; | ||
const ret = async (...args) => { | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
let functionArguments; | ||
@@ -119,4 +111,5 @@ try { | ||
} | ||
if (__httpClient) { | ||
return enqueueExecution(__httpClient, { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -128,4 +121,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = randomUUID(); | ||
@@ -177,5 +169,5 @@ __database.set(id, { id: id, state: "started" }); | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
if (__httpClient) { | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
let scheduleFor; | ||
@@ -189,3 +181,3 @@ if (delay instanceof Date) { | ||
} | ||
return enqueueExecution(__httpClient, { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -197,4 +189,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = randomUUID(); | ||
@@ -232,5 +223,5 @@ __database.set(id, { id: id, state: "started" }); | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] invoked.`); | ||
if (__httpClient) { | ||
debug(`[defer.run][${fn.name}] invoked.`); | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
let scheduleFor; | ||
@@ -248,3 +239,3 @@ const delay = deferFn.__execOptions?.delay; | ||
} | ||
return enqueueExecution(__httpClient, { | ||
return client.enqueueExecution(httpClient, { | ||
name: fn.name, | ||
@@ -256,4 +247,3 @@ arguments: functionArguments, | ||
} | ||
if (__verbose) | ||
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
debug(`[defer.run][${fn.name}] defer ignore, no token found.`); | ||
const id = randomUUID(); | ||
@@ -290,4 +280,5 @@ __database.set(id, { id: id, state: "started" }); | ||
let response; | ||
if (__httpClient) { | ||
const { id } = await enqueueExecution(__httpClient, { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) { | ||
const { id } = await client.enqueueExecution(httpClient, { | ||
name: fnName, | ||
@@ -298,3 +289,3 @@ arguments: functionArguments, | ||
}); | ||
response = await waitExecutionResult(__httpClient, { id: id }); | ||
response = await client.waitExecutionResult(httpClient, { id: id }); | ||
} | ||
@@ -319,32 +310,20 @@ else { | ||
}; | ||
// EXAMPLES: | ||
// interface Contact { | ||
// id: string; | ||
// name: string; | ||
// } | ||
// const importContacts = (companyId: string, contacts: Contact[]) => { | ||
// return new Promise<{ imported: number; companyId: string }>((resolve) => { | ||
// console.log(`Start importing contacts for company#${companyId}`); | ||
// setTimeout(() => { | ||
// console.log(contacts); | ||
// console.log("Done."); | ||
// resolve({ imported: 10000, companyId }); | ||
// }, 5000); | ||
// }); | ||
// }; | ||
// const importContactsD = defer(importContacts); | ||
// async function myFunction() { | ||
// return 1; | ||
// } | ||
// defer.cron(myFunction, "every day"); | ||
// async function test() { | ||
// await importContactsD("1", []); // fire and forget | ||
// const r = await importContactsD.await("1", []); // wait for execution result | ||
// const awaitImportContact = awaitResult(importContactsD); | ||
// const result = await awaitImportContact("1", []); | ||
// } | ||
// // Delayed | ||
// const delayed = delay(importContactsD, "1h"); | ||
// delayed("", []); | ||
// // Retry options | ||
// const importContactsRetried = defer(importContacts, { retry: 3 }); | ||
export async function getExecution(id) { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) | ||
return client.fetchExecution(httpClient, { id }); | ||
console.log("getExecution", id); | ||
const response = __database.get(id); | ||
if (response) | ||
return Promise.resolve({ | ||
...response, | ||
state: response.state, | ||
}); | ||
throw new APIError("execution not found", "not found"); | ||
} | ||
export async function cancelExecution(id, force = false) { | ||
const httpClient = getHTTPClient(); | ||
if (httpClient) | ||
return client.cancelExecution(httpClient, { id, force }); | ||
return {}; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { getExecution } from "../getExecution.js"; | ||
import { getExecution } from "../index.js"; | ||
import { APIError } from "../errors.js"; | ||
@@ -3,0 +3,0 @@ // @ts-expect-error https://github.com/microsoft/TypeScript/issues/52841#issuecomment-1574002759 |
@@ -1,1 +0,1 @@ | ||
export default "1.9.0-alpha-20230817160229-01d1aae"; | ||
export default "1.9.0-alpha-20230818134629-71de74e"; |
{ | ||
"name": "@defer/client", | ||
"version": "1.9.0-alpha-20230817160229-01d1aae", | ||
"version": "1.9.0-alpha-20230818134629-71de74e", | ||
"description": "Zero infrastructure NodeJS background jobs", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
export type HTTPClient = ReturnType<typeof makeHTTPClient>; | ||
export declare function makeHTTPClient(apiEndpoint: string, accessToken: string, clientOptions?: RequestInit): <T>(method: string, path: string, body?: string | null) => Promise<T>; | ||
export type HTTPClient = <T>(method: string, path: string, body?: string | null) => Promise<T>; | ||
export declare function makeHTTPClient(apiEndpoint: string, accessToken: string, clientOptions?: RequestInit): HTTPClient; |
import { Units } from "parse-duration"; | ||
import { EnqueueExecutionResponse, ExecutionState } from "./client.js"; | ||
import { HTTPClient } from "./httpClient.js"; | ||
export { getExecution } from "./getExecution.js"; | ||
export { cancelExecution } from "./cancelExecution.js"; | ||
interface Options { | ||
accessToken?: string; | ||
endpoint?: string; | ||
verbose?: boolean; | ||
} | ||
import * as client from "./client.js"; | ||
export declare const __database: Map<string, { | ||
id: string; | ||
state: ExecutionState; | ||
state: client.ExecutionState; | ||
result?: any; | ||
}>; | ||
export declare let __httpClient: HTTPClient | undefined; | ||
export declare function configure(opts?: Options): void; | ||
export declare const deferEnabled: () => boolean; | ||
@@ -43,3 +33,3 @@ export type UnPromise<F> = F extends Promise<infer R> ? R : F; | ||
export interface DeferRetFn<F extends (...args: any | undefined) => Promise<any>> extends HasDeferMetadata { | ||
(...args: Parameters<F>): Promise<EnqueueExecutionResponse>; | ||
(...args: Parameters<F>): Promise<client.EnqueueExecutionResponse>; | ||
__fn: F; | ||
@@ -104,1 +94,4 @@ __execOptions?: DeferExecutionOptions; | ||
export declare const awaitResult: DeferAwaitResult; | ||
export declare function getExecution(id: string): Promise<client.FetchExecutionResponse>; | ||
export declare function cancelExecution(id: string, force?: boolean): Promise<client.CancelExecutionResponse>; | ||
export {}; |
import type { NextRequest, NextResponse } from "next/server"; | ||
import type { DeferRetFn } from "../index.js"; | ||
import { type DeferRetFn } from "../index.js"; | ||
export interface DeferNextRoute { | ||
@@ -4,0 +4,0 @@ GetHandler: (request: NextRequest) => Promise<NextResponse | Response>; |
@@ -1,2 +0,2 @@ | ||
declare const _default: "1.9.0-alpha-20230817160229-01d1aae"; | ||
declare const _default: "1.9.0-alpha-20230818134629-71de74e"; | ||
export default _default; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
65758
48
1464
9