vercel-cronjob 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nikoewe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # vercel-cronjob
2
+
3
+ Standalone cron job runner that reads `vercel.json` and replicates Vercel's cron behavior — scheduled HTTP GET requests with the same headers Vercel sends.
4
+
5
+ Useful for running Vercel cron jobs in environments where Vercel isn't handling scheduling (e.g., Kubernetes production deployments).
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install github:nikoewe/vercel-cronjob
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { runCrons } from "vercel-cronjob";
17
+
18
+ runCrons({ url: "http://localhost:3000" });
19
+ ```
20
+
21
+ With all options:
22
+
23
+ ```typescript
24
+ runCrons({
25
+ url: "http://localhost:3000",
26
+ cronSecret: "my-secret", // optional — sent as Authorization: Bearer <secret>
27
+ vercelJsonPath: "./vercel.json", // optional — defaults to ./vercel.json
28
+ });
29
+ ```
30
+
31
+ ## CLI
32
+
33
+ The package also ships a CLI that reads configuration from environment variables:
34
+
35
+ | Variable | Required | Description |
36
+ |----------|----------|-------------|
37
+ | `BASE_URL` | Yes | Target app URL |
38
+ | `CRON_SECRET` | No | Sent as `Authorization: Bearer <secret>` |
39
+ | `VERCEL_JSON_PATH` | No | Path to vercel.json (default: `./vercel.json`) |
40
+
41
+ ```bash
42
+ BASE_URL=http://localhost:3000 npx vercel-cronjob
43
+ ```
44
+
45
+ ## License
46
+
47
+ MIT
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,18 @@
1
+ import { runCrons } from "./index.js";
2
+ async function main() {
3
+ const url = process.env.BASE_URL;
4
+ if (!url) {
5
+ console.error("ERROR: BASE_URL environment variable is required");
6
+ process.exit(1);
7
+ }
8
+ await runCrons({
9
+ url,
10
+ cronSecret: process.env.CRON_SECRET,
11
+ vercelJsonPath: process.env.VERCEL_JSON_PATH,
12
+ });
13
+ }
14
+ main().catch((err) => {
15
+ console.error("Fatal:", err);
16
+ process.exit(1);
17
+ });
18
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,KAAK,UAAU,IAAI;IACjB,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IACjC,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO,CAAC,KAAK,CAAC,kDAAkD,CAAC,CAAC;QAClE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAED,MAAM,QAAQ,CAAC;QACb,GAAG;QACH,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,WAAW;QACnC,cAAc,EAAE,OAAO,CAAC,GAAG,CAAC,gBAAgB;KAC7C,CAAC,CAAC;AACL,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACnB,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IAC7B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export interface CronEntry {
2
+ path: string;
3
+ schedule: string;
4
+ }
5
+ export declare function loadCrons(vercelJsonPath?: string): Promise<CronEntry[]>;
package/dist/config.js ADDED
@@ -0,0 +1,25 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+ export async function loadCrons(vercelJsonPath) {
4
+ const filePath = resolve(vercelJsonPath ?? "./vercel.json");
5
+ const raw = await readFile(filePath, "utf-8");
6
+ const parsed = JSON.parse(raw);
7
+ if (!parsed.crons || !Array.isArray(parsed.crons)) {
8
+ throw new Error(`No "crons" array found in ${filePath}`);
9
+ }
10
+ const crons = [];
11
+ for (const entry of parsed.crons) {
12
+ if (!entry.path || typeof entry.path !== "string") {
13
+ throw new Error(`Invalid cron entry: missing "path"`);
14
+ }
15
+ if (!entry.schedule || typeof entry.schedule !== "string") {
16
+ throw new Error(`Invalid cron entry for ${entry.path}: missing "schedule"`);
17
+ }
18
+ crons.push({ path: entry.path, schedule: entry.schedule });
19
+ }
20
+ if (crons.length === 0) {
21
+ throw new Error(`"crons" array is empty in ${filePath}`);
22
+ }
23
+ return crons;
24
+ }
25
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,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0,0 +1 @@
1
+ export {};
@@ -0,0 +1,64 @@
1
+ import { describe, it, expect, afterEach } from "vitest";
2
+ import { writeFile, mkdtemp, rm } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { tmpdir } from "node:os";
5
+ import { loadCrons } from "./config.js";
6
+ describe("loadCrons", () => {
7
+ const tmpDirs = [];
8
+ async function writeTempVercelJson(content) {
9
+ const dir = await mkdtemp(join(tmpdir(), "vercel-cron-test-"));
10
+ tmpDirs.push(dir);
11
+ const filePath = join(dir, "vercel.json");
12
+ await writeFile(filePath, JSON.stringify(content));
13
+ return filePath;
14
+ }
15
+ afterEach(async () => {
16
+ for (const dir of tmpDirs) {
17
+ await rm(dir, { recursive: true, force: true });
18
+ }
19
+ tmpDirs.length = 0;
20
+ });
21
+ it("parses valid vercel.json with a single cron", async () => {
22
+ const path = await writeTempVercelJson({
23
+ crons: [{ path: "/api/test", schedule: "0 7 * * *" }],
24
+ });
25
+ const crons = await loadCrons(path);
26
+ expect(crons).toEqual([{ path: "/api/test", schedule: "0 7 * * *" }]);
27
+ });
28
+ it("parses valid vercel.json with multiple crons", async () => {
29
+ const path = await writeTempVercelJson({
30
+ crons: [
31
+ { path: "/api/one", schedule: "0 7 * * *" },
32
+ { path: "/api/two", schedule: "*/5 * * * *" },
33
+ ],
34
+ });
35
+ const crons = await loadCrons(path);
36
+ expect(crons).toHaveLength(2);
37
+ expect(crons[0].path).toBe("/api/one");
38
+ expect(crons[1].path).toBe("/api/two");
39
+ });
40
+ it("throws when file does not exist", async () => {
41
+ await expect(loadCrons("/tmp/nonexistent/vercel.json")).rejects.toThrow();
42
+ });
43
+ it("throws when no crons array", async () => {
44
+ const path = await writeTempVercelJson({ builds: [] });
45
+ await expect(loadCrons(path)).rejects.toThrow('No "crons" array');
46
+ });
47
+ it("throws when crons is empty", async () => {
48
+ const path = await writeTempVercelJson({ crons: [] });
49
+ await expect(loadCrons(path)).rejects.toThrow("empty");
50
+ });
51
+ it("throws when entry missing path", async () => {
52
+ const path = await writeTempVercelJson({
53
+ crons: [{ schedule: "0 7 * * *" }],
54
+ });
55
+ await expect(loadCrons(path)).rejects.toThrow('missing "path"');
56
+ });
57
+ it("throws when entry missing schedule", async () => {
58
+ const path = await writeTempVercelJson({
59
+ crons: [{ path: "/api/test" }],
60
+ });
61
+ await expect(loadCrons(path)).rejects.toThrow('missing "schedule"');
62
+ });
63
+ });
64
+ //# sourceMappingURL=config.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.test.js","sourceRoot":"","sources":["../src/config.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,MAAM,OAAO,GAAa,EAAE,CAAC;IAE7B,KAAK,UAAU,mBAAmB,CAAC,OAAgB;QACjD,MAAM,GAAG,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,mBAAmB,CAAC,CAAC,CAAC;QAC/D,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,aAAa,CAAC,CAAC;QAC1C,MAAM,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;QACnD,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,SAAS,CAAC,KAAK,IAAI,EAAE;QACnB,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE,CAAC;YAC1B,MAAM,EAAE,CAAC,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C,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ export interface CronResult {
2
+ path: string;
3
+ status: number;
4
+ ok: boolean;
5
+ durationMs: number;
6
+ }
7
+ export declare function executeCron(baseUrl: string, path: string, cronSecret?: string): Promise<CronResult>;
@@ -0,0 +1,22 @@
1
+ export async function executeCron(baseUrl, path, cronSecret) {
2
+ const url = `${baseUrl}${path}`;
3
+ const headers = {
4
+ "User-Agent": "vercel-cron/1.0",
5
+ };
6
+ if (cronSecret) {
7
+ headers["Authorization"] = `Bearer ${cronSecret}`;
8
+ }
9
+ const start = performance.now();
10
+ const response = await fetch(url, {
11
+ method: "GET",
12
+ headers,
13
+ });
14
+ const durationMs = Math.round(performance.now() - start);
15
+ return {
16
+ path,
17
+ status: response.status,
18
+ ok: response.ok,
19
+ durationMs,
20
+ };
21
+ }
22
+ //# sourceMappingURL=executor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.js","sourceRoot":"","sources":["../src/executor.ts"],"names":[],"mappings":"AAOA,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,OAAe,EACf,IAAY,EACZ,UAAmB;IAEnB,MAAM,GAAG,GAAG,GAAG,OAAO,GAAG,IAAI,EAAE,CAAC;IAChC,MAAM,OAAO,GAA2B;QACtC,YAAY,EAAE,iBAAiB;KAChC,CAAC;IAEF,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,CAAC,eAAe,CAAC,GAAG,UAAU,UAAU,EAAE,CAAC;IACpD,CAAC;IAED,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;IAEhC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE;QAChC,MAAM,EAAE,KAAK;QACb,OAAO;KACR,CAAC,CAAC;IAEH,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,CAAC;IAEzD,OAAO;QACL,IAAI;QACJ,MAAM,EAAE,QAAQ,CAAC,MAAM;QACvB,EAAE,EAAE,QAAQ,CAAC,EAAE;QACf,UAAU;KACX,CAAC;AACJ,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { executeCron } from "./executor.js";
3
+ describe("executeCron", () => {
4
+ const originalFetch = globalThis.fetch;
5
+ beforeEach(() => {
6
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
7
+ status: 200,
8
+ ok: true,
9
+ }));
10
+ });
11
+ afterEach(() => {
12
+ globalThis.fetch = originalFetch;
13
+ });
14
+ it("sends GET request to correct URL", async () => {
15
+ await executeCron("http://localhost:3000", "/api/test");
16
+ expect(fetch).toHaveBeenCalledWith("http://localhost:3000/api/test", {
17
+ method: "GET",
18
+ headers: expect.objectContaining({
19
+ "User-Agent": "vercel-cron/1.0",
20
+ }),
21
+ });
22
+ });
23
+ it("includes Authorization header when cronSecret is provided", async () => {
24
+ await executeCron("http://localhost:3000", "/api/test", "my-secret");
25
+ expect(fetch).toHaveBeenCalledWith(expect.any(String), expect.objectContaining({
26
+ headers: expect.objectContaining({
27
+ Authorization: "Bearer my-secret",
28
+ }),
29
+ }));
30
+ });
31
+ it("omits Authorization header when no cronSecret", async () => {
32
+ await executeCron("http://localhost:3000", "/api/test");
33
+ const call = vi.mocked(fetch).mock.calls[0];
34
+ const headers = call[1].headers;
35
+ expect(headers).not.toHaveProperty("Authorization");
36
+ });
37
+ it("returns path, status, ok, and durationMs", async () => {
38
+ const result = await executeCron("http://localhost:3000", "/api/test");
39
+ expect(result).toEqual({
40
+ path: "/api/test",
41
+ status: 200,
42
+ ok: true,
43
+ durationMs: expect.any(Number),
44
+ });
45
+ });
46
+ it("handles non-200 responses", async () => {
47
+ vi.stubGlobal("fetch", vi.fn().mockResolvedValue({
48
+ status: 500,
49
+ ok: false,
50
+ }));
51
+ const result = await executeCron("http://localhost:3000", "/api/test");
52
+ expect(result.status).toBe(500);
53
+ expect(result.ok).toBe(false);
54
+ });
55
+ });
56
+ //# sourceMappingURL=executor.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"executor.test.js","sourceRoot":"","sources":["../src/executor.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AACzE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,MAAM,aAAa,GAAG,UAAU,CAAC,KAAK,CAAC;IAEvC,UAAU,CAAC,GAAG,EAAE;QACd,EAAE,CAAC,UAAU,CACX,OAAO,EACP,EAAE,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC;YACxB,MAAM,EAAE,GAAG;YACX,EAAE,EAAE,IAAI;SACT,CAAC,CACH,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACb,UAAU,CAAC,KAAK,GAAG,aAAa,CAAC;IACnC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAChD,MAAM,WAAW,CAAC,uBAAuB,EAAE,WAAW,CAAC,CAAC;QACxD,MAAM,CAAC,KAAK,CAAC,CAAC,oBAAoB,CAAC,gCAAgC,EAAE;YACnE,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,MAAM,CAAC,gBAAgB,CAAC;gBAC/B,YAAY,EAAE,iBAAiB;aAChC,CAAC;SACH,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;QACzE,MAAM,WAAW,CAAC,uBAAuB,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;QACrE,MAAM,CAAC,KAAK,CAAC,CAAC,oBAAoB,CAChC,MAAM,CAAC,GA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,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAChC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,12 @@
1
+ export { loadCrons } from "./config.js";
2
+ export type { CronEntry } from "./config.js";
3
+ export { executeCron } from "./executor.js";
4
+ export type { CronResult } from "./executor.js";
5
+ export { startScheduler } from "./scheduler.js";
6
+ export type { SchedulerConfig } from "./scheduler.js";
7
+ export interface RunCronsOptions {
8
+ url: string;
9
+ cronSecret?: string;
10
+ vercelJsonPath?: string;
11
+ }
12
+ export declare function runCrons(options: RunCronsOptions): Promise<import("node-cron").ScheduledTask[]>;
package/dist/index.js ADDED
@@ -0,0 +1,20 @@
1
+ import { loadCrons } from "./config.js";
2
+ import { startScheduler } from "./scheduler.js";
3
+ export { loadCrons } from "./config.js";
4
+ export { executeCron } from "./executor.js";
5
+ export { startScheduler } from "./scheduler.js";
6
+ export async function runCrons(options) {
7
+ const { url, cronSecret, vercelJsonPath } = options;
8
+ const crons = await loadCrons(vercelJsonPath);
9
+ console.log(`vercel-cronjob starting...`);
10
+ console.log(` URL: ${url}`);
11
+ console.log(` CRON_SECRET: ${cronSecret ? "***" : "(not set)"}`);
12
+ console.log(`\nRegistered ${crons.length} cron(s):`);
13
+ for (const entry of crons) {
14
+ console.log(` ${entry.schedule} → ${entry.path}`);
15
+ }
16
+ const tasks = startScheduler({ crons, baseUrl: url, cronSecret });
17
+ console.log("\nScheduler running. Waiting for triggers...\n");
18
+ return tasks;
19
+ }
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAShD,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,OAAwB;IACrD,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,cAAc,EAAE,GAAG,OAAO,CAAC;IAEpD,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,cAAc,CAAC,CAAC;IAE9C,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;IAC1C,OAAO,CAAC,GAAG,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC;IAC7B,OAAO,CAAC,GAAG,CAAC,kBAAkB,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC;IAClE,OAAO,CAAC,GAAG,CAAC,gBAAgB,KAAK,CAAC,MAAM,WAAW,CAAC,CAAC;IACrD,KAAK,MAAM,KAAK,IAAI,KAAK,EAAE,CAAC;QAC1B,OAAO,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,QAAQ,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;IACvD,CAAC;IAED,MAAM,KAAK,GAAG,cAAc,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;IAElE,OAAO,CAAC,GAAG,CAAC,gDAAgD,CAAC,CAAC;IAE9D,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,8 @@
1
+ import cron from "node-cron";
2
+ import type { CronEntry } from "./config.js";
3
+ export interface SchedulerConfig {
4
+ crons: CronEntry[];
5
+ baseUrl: string;
6
+ cronSecret?: string;
7
+ }
8
+ export declare function startScheduler(config: SchedulerConfig): cron.ScheduledTask[];
@@ -0,0 +1,31 @@
1
+ import cron from "node-cron";
2
+ import { executeCron } from "./executor.js";
3
+ export function startScheduler(config) {
4
+ const { crons, baseUrl, cronSecret } = config;
5
+ const tasks = [];
6
+ for (const entry of crons) {
7
+ const task = cron.schedule(entry.schedule, async () => {
8
+ const timestamp = new Date().toISOString();
9
+ console.log(`[${timestamp}] Triggering ${entry.path}`);
10
+ try {
11
+ const result = await executeCron(baseUrl, entry.path, cronSecret);
12
+ console.log(`[${timestamp}] ${result.path} → ${result.status} (${result.durationMs}ms)`);
13
+ }
14
+ catch (err) {
15
+ console.error(`[${timestamp}] ${entry.path} → FAILED:`, err);
16
+ }
17
+ });
18
+ tasks.push(task);
19
+ }
20
+ const shutdown = () => {
21
+ console.log("\nShutting down scheduler...");
22
+ for (const task of tasks) {
23
+ task.stop();
24
+ }
25
+ process.exit(0);
26
+ };
27
+ process.on("SIGTERM", shutdown);
28
+ process.on("SIGINT", shutdown);
29
+ return tasks;
30
+ }
31
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAQ5C,MAAM,UAAU,cAAc,CAAC,MAAuB;IACpD,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;IAC9C,MAAM,KAAK,GAAyB,EAAE,CAAC;IAEvC,KAAK,MAAM,KAAK,IAAI,KAAK,EAAE,CAAC;QAC1B,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;YACpD,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;YAC3C,OAAO,CAAC,GAAG,CAAC,IAAI,SAAS,gBAAgB,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;YAEvD,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;gBAClE,OAAO,CAAC,GAAG,CACT,IAAI,SAAS,KAAK,MAAM,CAAC,IAAI,MAAM,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,UAAU,KAAK,CAC5E,CAAC;YACJ,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,CAAC,KAAK,CAAC,IAAI,SAAS,KAAK,KAAK,CAAC,IAAI,YAAY,EAAE,GAAG,CAAC,CAAC;YAC/D,CAAC;QACH,CAAC,CAAC,CAAC;QAEH,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACnB,CAAC;IAED,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,OAAO,CAAC,GAAG,CAAC,8BAA8B,CAAC,CAAC;QAC5C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,IAAI,EAAE,CAAC;QACd,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAChC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IAE/B,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import cron from "node-cron";
3
+ import { startScheduler } from "./scheduler.js";
4
+ vi.mock("node-cron", () => ({
5
+ default: {
6
+ schedule: vi.fn(() => ({ stop: vi.fn() })),
7
+ },
8
+ }));
9
+ describe("startScheduler", () => {
10
+ beforeEach(() => {
11
+ vi.clearAllMocks();
12
+ });
13
+ it("registers a cron task for each entry", () => {
14
+ const tasks = startScheduler({
15
+ crons: [
16
+ { path: "/api/one", schedule: "0 7 * * *" },
17
+ { path: "/api/two", schedule: "*/5 * * * *" },
18
+ ],
19
+ baseUrl: "http://localhost:3000",
20
+ });
21
+ expect(cron.schedule).toHaveBeenCalledTimes(2);
22
+ expect(cron.schedule).toHaveBeenCalledWith("0 7 * * *", expect.any(Function));
23
+ expect(cron.schedule).toHaveBeenCalledWith("*/5 * * * *", expect.any(Function));
24
+ expect(tasks).toHaveLength(2);
25
+ });
26
+ it("returns tasks that can be stopped", () => {
27
+ const tasks = startScheduler({
28
+ crons: [{ path: "/api/test", schedule: "0 7 * * *" }],
29
+ baseUrl: "http://localhost:3000",
30
+ });
31
+ expect(tasks[0].stop).toBeDefined();
32
+ tasks[0].stop();
33
+ });
34
+ });
35
+ //# sourceMappingURL=scheduler.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.test.js","sourceRoot":"","sources":["../src/scheduler.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IAC1B,OAAO,EAAE;QACP,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC;KAC3C;CACF,CAAC,CAAC,CAAC;AAEJ,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;IAC9B,UAAU,CAAC,GAAG,EAAE;QACd,EAAE,CAAC,aAAa,EAAE,CAAC;IACrB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAC9C,MAAM,KAAK,GAAG,cAAc,CAAC;YAC3B,KAAK,EAAE;gBACL,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,EAAE;gBAC3C,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,aAAa,EAAE;aAC9C;YACD,OAAO,EAAE,uBAAuB;SACjC,CAAC,CAAC;QAEH,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;QAC/C,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,oBAAoB,CACxC,WAAW,EACX,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CACrB,CAAC;QACF,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,oBAAoB,CACxC,aAAa,EACb,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CACrB,CAAC;QACF,MAAM,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;IAChC,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,mCAAmC,EAAE,GAAG,EAAE;QAC3C,MAAM,KAAK,GAAG,cAAc,CAAC;YAC3B,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;YACrD,OAAO,EAAE,uBAAuB;SACjC,CAAC,CAAC;QAEH,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;QACpC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "vercel-cronjob",
3
+ "version": "1.0.0",
4
+ "description": "Standalone Vercel cron job runner — replicates Vercel's cron behavior for non-Vercel deployments",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "vercel-cronjob": "./dist/cli.js"
10
+ },
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "start": "node dist/cli.js",
14
+ "dev": "tsx src/cli.ts",
15
+ "test": "vitest run --exclude dist"
16
+ },
17
+ "dependencies": {
18
+ "node-cron": "^3.0.3"
19
+ },
20
+ "devDependencies": {
21
+ "@types/node": "^20.11.0",
22
+ "@types/node-cron": "^3.0.11",
23
+ "tsx": "^4.7.0",
24
+ "typescript": "^5.3.3",
25
+ "vitest": "^4.0.18"
26
+ },
27
+ "license": "MIT"
28
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { runCrons } from "./index.js";
2
+
3
+ async function main() {
4
+ const url = process.env.BASE_URL;
5
+ if (!url) {
6
+ console.error("ERROR: BASE_URL environment variable is required");
7
+ process.exit(1);
8
+ }
9
+
10
+ await runCrons({
11
+ url,
12
+ cronSecret: process.env.CRON_SECRET,
13
+ vercelJsonPath: process.env.VERCEL_JSON_PATH,
14
+ });
15
+ }
16
+
17
+ main().catch((err) => {
18
+ console.error("Fatal:", err);
19
+ process.exit(1);
20
+ });
@@ -0,0 +1,73 @@
1
+ import { describe, it, expect, afterEach } from "vitest";
2
+ import { writeFile, mkdtemp, rm } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { tmpdir } from "node:os";
5
+ import { loadCrons } from "./config.js";
6
+
7
+ describe("loadCrons", () => {
8
+ const tmpDirs: string[] = [];
9
+
10
+ async function writeTempVercelJson(content: unknown): Promise<string> {
11
+ const dir = await mkdtemp(join(tmpdir(), "vercel-cron-test-"));
12
+ tmpDirs.push(dir);
13
+ const filePath = join(dir, "vercel.json");
14
+ await writeFile(filePath, JSON.stringify(content));
15
+ return filePath;
16
+ }
17
+
18
+ afterEach(async () => {
19
+ for (const dir of tmpDirs) {
20
+ await rm(dir, { recursive: true, force: true });
21
+ }
22
+ tmpDirs.length = 0;
23
+ });
24
+
25
+ it("parses valid vercel.json with a single cron", async () => {
26
+ const path = await writeTempVercelJson({
27
+ crons: [{ path: "/api/test", schedule: "0 7 * * *" }],
28
+ });
29
+ const crons = await loadCrons(path);
30
+ expect(crons).toEqual([{ path: "/api/test", schedule: "0 7 * * *" }]);
31
+ });
32
+
33
+ it("parses valid vercel.json with multiple crons", async () => {
34
+ const path = await writeTempVercelJson({
35
+ crons: [
36
+ { path: "/api/one", schedule: "0 7 * * *" },
37
+ { path: "/api/two", schedule: "*/5 * * * *" },
38
+ ],
39
+ });
40
+ const crons = await loadCrons(path);
41
+ expect(crons).toHaveLength(2);
42
+ expect(crons[0].path).toBe("/api/one");
43
+ expect(crons[1].path).toBe("/api/two");
44
+ });
45
+
46
+ it("throws when file does not exist", async () => {
47
+ await expect(loadCrons("/tmp/nonexistent/vercel.json")).rejects.toThrow();
48
+ });
49
+
50
+ it("throws when no crons array", async () => {
51
+ const path = await writeTempVercelJson({ builds: [] });
52
+ await expect(loadCrons(path)).rejects.toThrow('No "crons" array');
53
+ });
54
+
55
+ it("throws when crons is empty", async () => {
56
+ const path = await writeTempVercelJson({ crons: [] });
57
+ await expect(loadCrons(path)).rejects.toThrow("empty");
58
+ });
59
+
60
+ it("throws when entry missing path", async () => {
61
+ const path = await writeTempVercelJson({
62
+ crons: [{ schedule: "0 7 * * *" }],
63
+ });
64
+ await expect(loadCrons(path)).rejects.toThrow('missing "path"');
65
+ });
66
+
67
+ it("throws when entry missing schedule", async () => {
68
+ const path = await writeTempVercelJson({
69
+ crons: [{ path: "/api/test" }],
70
+ });
71
+ await expect(loadCrons(path)).rejects.toThrow('missing "schedule"');
72
+ });
73
+ });
package/src/config.ts ADDED
@@ -0,0 +1,43 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { resolve } from "node:path";
3
+
4
+ export interface CronEntry {
5
+ path: string;
6
+ schedule: string;
7
+ }
8
+
9
+ interface VercelJson {
10
+ crons?: CronEntry[];
11
+ }
12
+
13
+ export async function loadCrons(
14
+ vercelJsonPath?: string
15
+ ): Promise<CronEntry[]> {
16
+ const filePath = resolve(vercelJsonPath ?? "./vercel.json");
17
+ const raw = await readFile(filePath, "utf-8");
18
+ const parsed: VercelJson = JSON.parse(raw);
19
+
20
+ if (!parsed.crons || !Array.isArray(parsed.crons)) {
21
+ throw new Error(`No "crons" array found in ${filePath}`);
22
+ }
23
+
24
+ const crons: CronEntry[] = [];
25
+
26
+ for (const entry of parsed.crons) {
27
+ if (!entry.path || typeof entry.path !== "string") {
28
+ throw new Error(`Invalid cron entry: missing "path"`);
29
+ }
30
+ if (!entry.schedule || typeof entry.schedule !== "string") {
31
+ throw new Error(
32
+ `Invalid cron entry for ${entry.path}: missing "schedule"`
33
+ );
34
+ }
35
+ crons.push({ path: entry.path, schedule: entry.schedule });
36
+ }
37
+
38
+ if (crons.length === 0) {
39
+ throw new Error(`"crons" array is empty in ${filePath}`);
40
+ }
41
+
42
+ return crons;
43
+ }
@@ -0,0 +1,72 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { executeCron } from "./executor.js";
3
+
4
+ describe("executeCron", () => {
5
+ const originalFetch = globalThis.fetch;
6
+
7
+ beforeEach(() => {
8
+ vi.stubGlobal(
9
+ "fetch",
10
+ vi.fn().mockResolvedValue({
11
+ status: 200,
12
+ ok: true,
13
+ })
14
+ );
15
+ });
16
+
17
+ afterEach(() => {
18
+ globalThis.fetch = originalFetch;
19
+ });
20
+
21
+ it("sends GET request to correct URL", async () => {
22
+ await executeCron("http://localhost:3000", "/api/test");
23
+ expect(fetch).toHaveBeenCalledWith("http://localhost:3000/api/test", {
24
+ method: "GET",
25
+ headers: expect.objectContaining({
26
+ "User-Agent": "vercel-cron/1.0",
27
+ }),
28
+ });
29
+ });
30
+
31
+ it("includes Authorization header when cronSecret is provided", async () => {
32
+ await executeCron("http://localhost:3000", "/api/test", "my-secret");
33
+ expect(fetch).toHaveBeenCalledWith(
34
+ expect.any(String),
35
+ expect.objectContaining({
36
+ headers: expect.objectContaining({
37
+ Authorization: "Bearer my-secret",
38
+ }),
39
+ })
40
+ );
41
+ });
42
+
43
+ it("omits Authorization header when no cronSecret", async () => {
44
+ await executeCron("http://localhost:3000", "/api/test");
45
+ const call = vi.mocked(fetch).mock.calls[0];
46
+ const headers = (call[1] as RequestInit).headers as Record<string, string>;
47
+ expect(headers).not.toHaveProperty("Authorization");
48
+ });
49
+
50
+ it("returns path, status, ok, and durationMs", async () => {
51
+ const result = await executeCron("http://localhost:3000", "/api/test");
52
+ expect(result).toEqual({
53
+ path: "/api/test",
54
+ status: 200,
55
+ ok: true,
56
+ durationMs: expect.any(Number),
57
+ });
58
+ });
59
+
60
+ it("handles non-200 responses", async () => {
61
+ vi.stubGlobal(
62
+ "fetch",
63
+ vi.fn().mockResolvedValue({
64
+ status: 500,
65
+ ok: false,
66
+ })
67
+ );
68
+ const result = await executeCron("http://localhost:3000", "/api/test");
69
+ expect(result.status).toBe(500);
70
+ expect(result.ok).toBe(false);
71
+ });
72
+ });
@@ -0,0 +1,37 @@
1
+ export interface CronResult {
2
+ path: string;
3
+ status: number;
4
+ ok: boolean;
5
+ durationMs: number;
6
+ }
7
+
8
+ export async function executeCron(
9
+ baseUrl: string,
10
+ path: string,
11
+ cronSecret?: string
12
+ ): Promise<CronResult> {
13
+ const url = `${baseUrl}${path}`;
14
+ const headers: Record<string, string> = {
15
+ "User-Agent": "vercel-cron/1.0",
16
+ };
17
+
18
+ if (cronSecret) {
19
+ headers["Authorization"] = `Bearer ${cronSecret}`;
20
+ }
21
+
22
+ const start = performance.now();
23
+
24
+ const response = await fetch(url, {
25
+ method: "GET",
26
+ headers,
27
+ });
28
+
29
+ const durationMs = Math.round(performance.now() - start);
30
+
31
+ return {
32
+ path,
33
+ status: response.status,
34
+ ok: response.ok,
35
+ durationMs,
36
+ };
37
+ }
package/src/index.ts ADDED
@@ -0,0 +1,35 @@
1
+ import { loadCrons } from "./config.js";
2
+ import { startScheduler } from "./scheduler.js";
3
+
4
+ export { loadCrons } from "./config.js";
5
+ export type { CronEntry } from "./config.js";
6
+ export { executeCron } from "./executor.js";
7
+ export type { CronResult } from "./executor.js";
8
+ export { startScheduler } from "./scheduler.js";
9
+ export type { SchedulerConfig } from "./scheduler.js";
10
+
11
+ export interface RunCronsOptions {
12
+ url: string;
13
+ cronSecret?: string;
14
+ vercelJsonPath?: string;
15
+ }
16
+
17
+ export async function runCrons(options: RunCronsOptions) {
18
+ const { url, cronSecret, vercelJsonPath } = options;
19
+
20
+ const crons = await loadCrons(vercelJsonPath);
21
+
22
+ console.log(`vercel-cronjob starting...`);
23
+ console.log(` URL: ${url}`);
24
+ console.log(` CRON_SECRET: ${cronSecret ? "***" : "(not set)"}`);
25
+ console.log(`\nRegistered ${crons.length} cron(s):`);
26
+ for (const entry of crons) {
27
+ console.log(` ${entry.schedule} → ${entry.path}`);
28
+ }
29
+
30
+ const tasks = startScheduler({ crons, baseUrl: url, cronSecret });
31
+
32
+ console.log("\nScheduler running. Waiting for triggers...\n");
33
+
34
+ return tasks;
35
+ }
@@ -0,0 +1,46 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+ import cron from "node-cron";
3
+ import { startScheduler } from "./scheduler.js";
4
+
5
+ vi.mock("node-cron", () => ({
6
+ default: {
7
+ schedule: vi.fn(() => ({ stop: vi.fn() })),
8
+ },
9
+ }));
10
+
11
+ describe("startScheduler", () => {
12
+ beforeEach(() => {
13
+ vi.clearAllMocks();
14
+ });
15
+
16
+ it("registers a cron task for each entry", () => {
17
+ const tasks = startScheduler({
18
+ crons: [
19
+ { path: "/api/one", schedule: "0 7 * * *" },
20
+ { path: "/api/two", schedule: "*/5 * * * *" },
21
+ ],
22
+ baseUrl: "http://localhost:3000",
23
+ });
24
+
25
+ expect(cron.schedule).toHaveBeenCalledTimes(2);
26
+ expect(cron.schedule).toHaveBeenCalledWith(
27
+ "0 7 * * *",
28
+ expect.any(Function)
29
+ );
30
+ expect(cron.schedule).toHaveBeenCalledWith(
31
+ "*/5 * * * *",
32
+ expect.any(Function)
33
+ );
34
+ expect(tasks).toHaveLength(2);
35
+ });
36
+
37
+ it("returns tasks that can be stopped", () => {
38
+ const tasks = startScheduler({
39
+ crons: [{ path: "/api/test", schedule: "0 7 * * *" }],
40
+ baseUrl: "http://localhost:3000",
41
+ });
42
+
43
+ expect(tasks[0].stop).toBeDefined();
44
+ tasks[0].stop();
45
+ });
46
+ });
@@ -0,0 +1,45 @@
1
+ import cron from "node-cron";
2
+ import type { CronEntry } from "./config.js";
3
+ import { executeCron } from "./executor.js";
4
+
5
+ export interface SchedulerConfig {
6
+ crons: CronEntry[];
7
+ baseUrl: string;
8
+ cronSecret?: string;
9
+ }
10
+
11
+ export function startScheduler(config: SchedulerConfig): cron.ScheduledTask[] {
12
+ const { crons, baseUrl, cronSecret } = config;
13
+ const tasks: cron.ScheduledTask[] = [];
14
+
15
+ for (const entry of crons) {
16
+ const task = cron.schedule(entry.schedule, async () => {
17
+ const timestamp = new Date().toISOString();
18
+ console.log(`[${timestamp}] Triggering ${entry.path}`);
19
+
20
+ try {
21
+ const result = await executeCron(baseUrl, entry.path, cronSecret);
22
+ console.log(
23
+ `[${timestamp}] ${result.path} → ${result.status} (${result.durationMs}ms)`
24
+ );
25
+ } catch (err) {
26
+ console.error(`[${timestamp}] ${entry.path} → FAILED:`, err);
27
+ }
28
+ });
29
+
30
+ tasks.push(task);
31
+ }
32
+
33
+ const shutdown = () => {
34
+ console.log("\nShutting down scheduler...");
35
+ for (const task of tasks) {
36
+ task.stop();
37
+ }
38
+ process.exit(0);
39
+ };
40
+
41
+ process.on("SIGTERM", shutdown);
42
+ process.on("SIGINT", shutdown);
43
+
44
+ return tasks;
45
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "Node16",
5
+ "moduleResolution": "Node16",
6
+ "outDir": "./dist",
7
+ "rootDir": "./src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "declaration": true,
13
+ "sourceMap": true
14
+ },
15
+ "include": ["src"],
16
+ "exclude": ["node_modules", "dist", "src/**/*.test.ts"]
17
+ }