From 54241bcbf4e738ba2a09002a321ba6cf0f4e2999 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 10:45:42 +0100 Subject: [PATCH 01/15] fix(build): remove need for `typesVersions` breaking VSCode imports --- package.json | 46 ++++++++++++++---------------------- src/version.ts | 16 +------------ tsconfig.build.json | 10 ++++++++ tsconfig.json | 6 ++--- utils/fix-ts-sub-exports.mjs | 18 -------------- 5 files changed, 32 insertions(+), 64 deletions(-) create mode 100644 tsconfig.build.json delete mode 100644 utils/fix-ts-sub-exports.mjs diff --git a/package.json b/package.json index bacc7d83..e2ced10e 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "version": "2.2.1", "description": "Zero infrastructure NodeJS background jobs", "type": "module", - "main": "dist/cjs/index.js", - "module": "dist/esm/index.js", + "main": "dist/index.js", + "module": "dist/index.js", "repository": { "type": "git", "url": "git+ssh://git@github.com/defer-run/defer.client.git" @@ -15,7 +15,8 @@ "node": ">=18" }, "scripts": { - "build": "node utils/update-version.mjs && npx bob build && node utils/fix-ts-sub-exports.mjs", + "build": "npm run clean && node utils/update-version.mjs && tsc --project tsconfig.build.json && cp package.json LICENSE README.md CHANGELOG.md dist", + "clean": "rm -rf ./dist", "lint": "eslint src/**/*.ts", "release": "changeset publish", "test": "npx jest --passWithNoTests --config ./tests/jest.ts --silent --runInBand", @@ -31,7 +32,6 @@ "@types/jest": "^29.5.11", "@types/react": "^18.2.48", "@typescript-eslint/eslint-plugin": "^6.20.0", - "bob-the-bundler": "^7.0.1", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", "jest": "^29.7", @@ -42,51 +42,41 @@ "ts-node": "^10.9.2", "typescript": "^5.3" }, - "typings": "dist/typings/index.d.ts", + "typings": "dist/index.d.ts", "typescript": { - "definition": "dist/typings/index.d.ts" + "definition": "dist/index.d.ts" }, "exports": { ".": { "require": { - "types": "./dist/typings/index.d.cts", - "default": "./dist/cjs/index.js" + "types": "./dist/index.d.cts", + "default": "./dist/index.js" }, "import": { - "types": "./dist/typings/index.d.ts", - "default": "./dist/esm/index.js" + "types": "./dist/index.d.ts", + "default": "./dist/index.js" }, "default": { - "types": "./dist/typings/index.d.ts", - "default": "./dist/esm/index.js" + "types": "./dist/index.d.ts", + "default": "./dist/index.js" } }, "./next": { "require": { - "types": "./dist/typings/next/index.d.cts", - "default": "./dist/cjs/next/index.js" + "types": "./dist/next/index.d.cts", + "default": "./dist/next/index.js" }, "import": { - "types": "./dist/typings/next/index.d.ts", - "default": "./dist/esm/next/index.js" + "types": "./dist/next/index.d.ts", + "default": "./dist/next/index.js" }, "default": { - "types": "./dist/typings/next/index.d.ts", - "default": "./dist/esm/next/index.js" + "types": "./dist/next/index.d.ts", + "default": "./dist/next/index.js" } }, "./package.json": "./package.json" }, - "typesVersions": { - "*": { - "index": [ - "typings/index.d.ts" - ], - "next": [ - "typings/next/index.d.ts" - ] - } - }, "publishConfig": { "directory": "dist", "access": "public" diff --git a/src/version.ts b/src/version.ts index e1629e70..ae1877e0 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,15 +1 @@ -// Copyright (c) 2021-2023 Defer SAS . -// -// Permission to use, copy, modify, and/or distribute this software for any -// purpose with or without fee is hereby granted, provided that the above -// copyright notice and this permission notice appear in all copies. -// -// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY -// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -// PERFORMANCE OF THIS SOFTWARE. - -export default "unknown"; +export default "2.2.1"; diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 00000000..ce570190 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "exclude": [ + "src/**/*.spec.ts", + "src/**/*.test.ts", + "src/test/**/*", + "scripts/**/*" + ] + } + \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 12d2a14f..8f84d06f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,11 @@ { "$schema": "https://json.schemastore.org/tsconfig", "compilerOptions": { - "lib": ["es2022", "dom"], - "outDir": "./dist", + "target": "es2017", + "lib": ["es2022", "DOM"], "module": "commonjs", + "outDir": "./dist", "declaration": true, - "target": "es2021", "strict": true, "esModuleInterop": true, "skipLibCheck": true, diff --git a/utils/fix-ts-sub-exports.mjs b/utils/fix-ts-sub-exports.mjs deleted file mode 100644 index b4b57edd..00000000 --- a/utils/fix-ts-sub-exports.mjs +++ /dev/null @@ -1,18 +0,0 @@ -import fs from "fs"; -import { dirname, join } from "path"; -import { fileURLToPath } from "url"; - -const __dirname = dirname(fileURLToPath(import.meta.url)); -const file = fs.readFileSync(join(__dirname, "../dist/package.json"), "utf-8"); -const pkg = JSON.parse(file); - -(pkg.typesVersions = { - "*": { - index: ["typings/index.d.ts"], - next: ["typings/next/index.d.ts"], - }, -}), - fs.writeFileSync( - join(__dirname, "../dist/package.json"), - JSON.stringify(pkg, null, 2) - ); From 3136c7966a04bc1ebf4f3d668859e308defa1988 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 10:47:06 +0100 Subject: [PATCH 02/15] chore: changeset --- .changeset/cyan-cycles-rhyme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/cyan-cycles-rhyme.md diff --git a/.changeset/cyan-cycles-rhyme.md b/.changeset/cyan-cycles-rhyme.md new file mode 100644 index 00000000..728ebe15 --- /dev/null +++ b/.changeset/cyan-cycles-rhyme.md @@ -0,0 +1,5 @@ +--- +"@defer/client": minor +--- + +Fix VSCode dynamic imports resolving on `@defer/client/index` From b94888d7461e174953177e24c67820edefab3d63 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:00:12 +0100 Subject: [PATCH 03/15] revert --- src/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/version.ts b/src/version.ts index ae1877e0..2b30c6ad 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default "2.2.1"; +// Copyright (c) 2021-2023 Defer SAS . \ No newline at end of file From ff01add19f1218775583e716a3d5aeafff060553 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:00:25 +0100 Subject: [PATCH 04/15] fix --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index e2ced10e..7eab81aa 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "@defer/client", "version": "2.2.1", "description": "Zero infrastructure NodeJS background jobs", - "type": "module", "main": "dist/index.js", "module": "dist/index.js", "repository": { From 838d5298870f34f8e6d7ef9faff9c41e9f6fce2f Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:00:30 +0100 Subject: [PATCH 05/15] remove bob --- .github/workflows/release.yml | 1 - .github/workflows/tests.yml | 2 -- .gitignore | 1 - .prettierignore | 1 - tests/jest.ts | 1 - 5 files changed, 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 51ce6971..32d8dbcc 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,7 +22,6 @@ jobs: access_token: ${{ secrets.GITHUB_TOKEN }} - run: "npm ci --no-audit" - run: "npm run build" - - run: "npx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 - name: "release / pull_request" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e65ebade..99b418c6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,6 @@ jobs: - run: "bunx prettier -c src/" - run: "bun run test" - run: "bun run build" - - run: "bunx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 @@ -50,6 +49,5 @@ jobs: - run: "npx prettier -c src/" - run: "npm run test" - run: "npm run build" - - run: "npx bob check" env: DEFER_NO_LOCAL_SCHEDULER: 1 diff --git a/.gitignore b/.gitignore index a0f1737e..ce2de85d 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,3 @@ dist # TernJS port file .tern-port -.bob diff --git a/.prettierignore b/.prettierignore index 2c81cf92..2975f3ad 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,3 @@ node_modules/ coverage/ dist/ yarn.lock -.bob/ diff --git a/tests/jest.ts b/tests/jest.ts index 15ce2bcd..ab58de4f 100644 --- a/tests/jest.ts +++ b/tests/jest.ts @@ -15,7 +15,6 @@ const config: Config.InitialOptions = { }, coverageDirectory: "../coverage", testEnvironment: "node", - resolver: "bob-the-bundler/jest-resolver", }; export default config; From a479631512d163396e12b6a77578bd627f13f4cc Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:00:47 +0100 Subject: [PATCH 06/15] update `package-lock.json` --- package-lock.json | 269 ---------------------------------------------- 1 file changed, 269 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7f8729fd..07971135 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,7 +17,6 @@ "@types/jest": "^29.5.11", "@types/react": "^18.2.48", "@typescript-eslint/eslint-plugin": "^6.20.0", - "bob-the-bundler": "^7.0.1", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", "jest": "^29.7", @@ -2966,159 +2965,6 @@ "node": ">=4" } }, - "node_modules/bob-the-bundler": { - "version": "7.0.1", - "dev": true, - "license": "MIT", - "dependencies": { - "consola": "^3.0.0", - "execa": "7.1.1", - "fs-extra": "^11.1.0", - "globby": "^13.1.3", - "js-yaml": "^4.1.0", - "lodash.get": "^4.4.2", - "p-limit": "^4.0.0", - "resolve.exports": "^2.0.0", - "tslib": "^2.0.0", - "yargs": "^17.6.2", - "zod": "^3.20.2" - }, - "bin": { - "bob": "dist/index.js" - }, - "engines": { - "node": ">=16", - "pnpm": ">=8" - }, - "peerDependencies": { - "typescript": "^5.0.0" - } - }, - "node_modules/bob-the-bundler/node_modules/argparse": { - "version": "2.0.1", - "dev": true, - "license": "Python-2.0" - }, - "node_modules/bob-the-bundler/node_modules/fast-glob": { - "version": "3.3.1", - "dev": true, - "license": "MIT", - "dependencies": { - "@nodelib/fs.stat": "^2.0.2", - "@nodelib/fs.walk": "^1.2.3", - "glob-parent": "^5.1.2", - "merge2": "^1.3.0", - "micromatch": "^4.0.4" - }, - "engines": { - "node": ">=8.6.0" - } - }, - "node_modules/bob-the-bundler/node_modules/fs-extra": { - "version": "11.1.1", - "dev": true, - "license": "MIT", - "dependencies": { - "graceful-fs": "^4.2.0", - "jsonfile": "^6.0.1", - "universalify": "^2.0.0" - }, - "engines": { - "node": ">=14.14" - } - }, - "node_modules/bob-the-bundler/node_modules/globby": { - "version": "13.2.2", - "dev": true, - "license": "MIT", - "dependencies": { - "dir-glob": "^3.0.1", - "fast-glob": "^3.3.0", - "ignore": "^5.2.4", - "merge2": "^1.4.1", - "slash": "^4.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/js-yaml": { - "version": "4.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "argparse": "^2.0.1" - }, - "bin": { - "js-yaml": "bin/js-yaml.js" - } - }, - "node_modules/bob-the-bundler/node_modules/jsonfile": { - "version": "6.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "universalify": "^2.0.0" - }, - "optionalDependencies": { - "graceful-fs": "^4.1.6" - } - }, - "node_modules/bob-the-bundler/node_modules/p-limit": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "dependencies": { - "yocto-queue": "^1.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/slash": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/universalify": { - "version": "2.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">= 10.0.0" - } - }, - "node_modules/bob-the-bundler/node_modules/yocto-queue": { - "version": "1.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12.20" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/bob-the-bundler/node_modules/zod": { - "version": "3.22.2", - "dev": true, - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/colinhacks" - } - }, "node_modules/brace-expansion": { "version": "1.1.11", "dev": true, @@ -3399,14 +3245,6 @@ "dev": true, "license": "MIT" }, - "node_modules/consola": { - "version": "3.2.3", - "dev": true, - "license": "MIT", - "engines": { - "node": "^14.18.0 || >=16.10.0" - } - }, "node_modules/convert-source-map": { "version": "1.9.0", "dev": true, @@ -4002,28 +3840,6 @@ "node": ">=0.10.0" } }, - "node_modules/execa": { - "version": "7.1.1", - "dev": true, - "license": "MIT", - "dependencies": { - "cross-spawn": "^7.0.3", - "get-stream": "^6.0.1", - "human-signals": "^4.3.0", - "is-stream": "^3.0.0", - "merge-stream": "^2.0.0", - "npm-run-path": "^5.1.0", - "onetime": "^6.0.0", - "signal-exit": "^3.0.7", - "strip-final-newline": "^3.0.0" - }, - "engines": { - "node": "^14.18.0 || ^16.14.0 || >=18.0.0" - }, - "funding": { - "url": "https://github.com/sindresorhus/execa?sponsor=1" - } - }, "node_modules/exit": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/exit/-/exit-0.1.2.tgz", @@ -4472,14 +4288,6 @@ "integrity": "sha512-UNopramDEhHJD+VR+ehk8rOslwSfByxPIZyJRfV739NDhN5LF1fa1MqnzKm2lGTQRjNrjK19Q5fhkgIfjlVUKw==", "dev": true }, - "node_modules/human-signals": { - "version": "4.3.1", - "dev": true, - "license": "Apache-2.0", - "engines": { - "node": ">=14.18.0" - } - }, "node_modules/iconv-lite": { "version": "0.4.24", "dev": true, @@ -4766,17 +4574,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/is-stream": { - "version": "3.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/is-string": { "version": "1.0.7", "dev": true, @@ -5715,11 +5512,6 @@ "node": ">=8" } }, - "node_modules/lodash.get": { - "version": "4.4.2", - "dev": true, - "license": "MIT" - }, "node_modules/lodash.memoize": { "version": "4.1.2", "dev": true, @@ -5882,17 +5674,6 @@ "node": ">=8.6" } }, - "node_modules/mimic-fn": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/min-indent": { "version": "1.0.1", "dev": true, @@ -6058,31 +5839,6 @@ "node": ">=0.10.0" } }, - "node_modules/npm-run-path": { - "version": "5.1.0", - "dev": true, - "license": "MIT", - "dependencies": { - "path-key": "^4.0.0" - }, - "engines": { - "node": "^12.20.0 || ^14.13.1 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, - "node_modules/npm-run-path/node_modules/path-key": { - "version": "4.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/object-inspect": { "version": "1.12.2", "dev": true, @@ -6124,20 +5880,6 @@ "wrappy": "1" } }, - "node_modules/onetime": { - "version": "6.0.0", - "dev": true, - "license": "MIT", - "dependencies": { - "mimic-fn": "^4.0.0" - }, - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", @@ -7191,17 +6933,6 @@ "node": ">=4" } }, - "node_modules/strip-final-newline": { - "version": "3.0.0", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=12" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/strip-indent": { "version": "3.0.0", "dev": true, From 967f19ce1da494deceacb326c813cb8ffaaffd35 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:04:11 +0100 Subject: [PATCH 07/15] fix: `dist/` not necessary --- package.json | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 7eab81aa..af38c2be 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,8 @@ "name": "@defer/client", "version": "2.2.1", "description": "Zero infrastructure NodeJS background jobs", - "main": "dist/index.js", - "module": "dist/index.js", + "main": "index.js", + "module": "index.js", "repository": { "type": "git", "url": "git+ssh://git@github.com/defer-run/defer.client.git" @@ -41,37 +41,37 @@ "ts-node": "^10.9.2", "typescript": "^5.3" }, - "typings": "dist/index.d.ts", + "typings": "index.d.ts", "typescript": { - "definition": "dist/index.d.ts" + "definition": "index.d.ts" }, "exports": { ".": { "require": { - "types": "./dist/index.d.cts", - "default": "./dist/index.js" + "types": "./index.d.cts", + "default": "./index.js" }, "import": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" + "types": "./index.d.ts", + "default": "./index.js" }, "default": { - "types": "./dist/index.d.ts", - "default": "./dist/index.js" + "types": "./index.d.ts", + "default": "./index.js" } }, "./next": { "require": { - "types": "./dist/next/index.d.cts", - "default": "./dist/next/index.js" + "types": "./next/index.d.cts", + "default": "./next/index.js" }, "import": { - "types": "./dist/next/index.d.ts", - "default": "./dist/next/index.js" + "types": "./next/index.d.ts", + "default": "./next/index.js" }, "default": { - "types": "./dist/next/index.d.ts", - "default": "./dist/next/index.js" + "types": "./next/index.d.ts", + "default": "./next/index.js" } }, "./package.json": "./package.json" @@ -90,7 +90,7 @@ "/tests/", "node_modules/", "coverage/", - "dist/", + "", "package-lock.json" ], "overrides": [ From 6ef61f844c7ef011e153043feeb4dc7e0942dfbd Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:07:32 +0100 Subject: [PATCH 08/15] fix --- src/version.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/version.ts b/src/version.ts index 2b30c6ad..6fa4e169 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1,15 @@ -// Copyright (c) 2021-2023 Defer SAS . \ No newline at end of file +// Copyright (c) 2021-2023 Defer SAS . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +export default "unknown"; \ No newline at end of file From 70e49247d685fba112deb890b41a1589bc908eca Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:07:37 +0100 Subject: [PATCH 09/15] ESM fixes --- src/backend.ts | 2 +- src/backend/local.ts | 46 ++++++++++++------------- src/backend/remote.ts | 8 ++--- src/backend/remote/httpClient.ts | 6 ++-- src/index.ts | 12 +++---- src/logger.ts | 4 +-- src/next/asNextRoute.ts | 8 ++--- src/next/index.ts | 4 +-- src/next/useDeferRoute.ts | 8 ++--- tests/backend/local.test.ts | 6 ++-- tests/backend/local/counter.test.ts | 2 +- tests/backend/local/kv.test.ts | 2 +- tests/backend/remote.test.ts | 6 ++-- tests/backend/remote/httpClient.test.ts | 2 +- tests/index.test.ts | 2 +- tests/logger.test.ts | 2 +- tests/next/asNextRoute.spec.ts | 8 ++--- tests/next/fixtures/routeWithProxy.ts | 2 +- tests/utils.test.ts | 2 +- 19 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/backend.ts b/src/backend.ts index 9e2b2b65..3a5ae926 100644 --- a/src/backend.ts +++ b/src/backend.ts @@ -12,7 +12,7 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { DeferableFunction, DeferredFunction } from "./index.js"; +import { DeferableFunction, DeferredFunction } from "./index"; export type ExecutionState = | "created" diff --git a/src/backend/local.ts b/src/backend/local.ts index 17f25acc..81887016 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -32,17 +32,17 @@ import { PageResult, ReRunExecutionResult, RescheduleExecutionResult, -} from "../backend.js"; +} from "../backend"; import { DeferableFunction, DeferredFunction, ExecutionMetadata, -} from "../index.js"; -import { error, info } from "../logger.js"; -import { getEnv, randomUUID, sleep, stringify } from "../utils.js"; -import version from "../version.js"; -import { Counter } from "./local/counter.js"; -import { KV } from "./local/kv.js"; +} from "../index"; +import { error, info } from "../logger"; +import { getEnv, randomUUID, sleep, stringify } from "../utils"; +import version from "../version"; +import { Counter } from "./local/counter"; +import { KV } from "./local/kv"; interface InternalExecution { id: string; @@ -86,7 +86,7 @@ const banner = ` function paginate( page: PageRequest | undefined, - records: Map, + records: Map ): PageResult { let edges = Array.from(records.keys()).reverse(); let hasNextPage: boolean = false; @@ -137,7 +137,7 @@ function paginate( function isExecutionMatchFilter( filters: ExecutionFilters | undefined, - execution: InternalExecution, + execution: InternalExecution ): boolean { if ( filters?.states && @@ -163,13 +163,13 @@ function isExecutionMatchFilter( if (filters?.metadata && filters.metadata.length > 0 && execution.metadata) { const metadataFilters = filters.metadata.filter( - (mdFilter) => mdFilter.values.length > 0, + (mdFilter) => mdFilter.values.length > 0 ); if ( !metadataFilters.some((mdFilter) => mdFilter.values.some( - (value) => execution.metadata[mdFilter.key] === value, - ), + (value) => execution.metadata[mdFilter.key] === value + ) ) ) { return false; @@ -264,7 +264,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); return execution; - }, + } ); if (shouldDiscard) { @@ -320,7 +320,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); execution.errorCode = errorCode; return execution; - }, + } ); }; @@ -342,7 +342,7 @@ export async function enqueue( args: Parameters, scheduleFor: Date, discardAfter: Date | undefined, - metadata: { [key: string]: string } | undefined, + metadata: { [key: string]: string } | undefined ): Promise { let functionId = functionIdMapping.get(func.__metadata.name); if (functionId === undefined) { @@ -396,7 +396,7 @@ export async function getExecutionResult(id: string): Promise { export async function cancelExecution( id: string, - force: boolean, + force: boolean ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -407,7 +407,7 @@ export async function cancelExecution( switch (execution.state) { case "aborting": throw new ExecutionAbortingAlreadyInProgress( - "aborting execution already in progress", + "aborting execution already in progress" ); case "created": execution.state = "cancelled"; @@ -417,7 +417,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state`, + `cannot cancel execution in "${execution.state}" state` ); } } else { @@ -427,7 +427,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state`, + `cannot cancel execution in "${execution.state}" state` ); } } @@ -441,7 +441,7 @@ export async function cancelExecution( export async function rescheduleExecution( id: string, - scheduleFor: Date, + scheduleFor: Date ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -460,7 +460,7 @@ export async function rescheduleExecution( } export async function reRunExecution( - id: string, + id: string ): Promise { const execution = await executionsStore.get(id); if (execution === undefined) @@ -487,7 +487,7 @@ export async function reRunExecution( export async function listExecutions( pageRequest?: PageRequest, - filters?: ExecutionFilters, + filters?: ExecutionFilters ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); @@ -505,7 +505,7 @@ export async function listExecutions( export async function listExecutionAttempts( id: string, pageRequest?: PageRequest, - filters?: ExecutionFilters, + filters?: ExecutionFilters ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); diff --git a/src/backend/remote.ts b/src/backend/remote.ts index d2d7810f..6487a3a5 100644 --- a/src/backend/remote.ts +++ b/src/backend/remote.ts @@ -33,10 +33,10 @@ import { PageRequest, ReRunExecutionResult, RescheduleExecutionResult, -} from "../backend.js"; -import { DeferableFunction, DeferredFunction } from "../index.js"; -import { getEnv, stringify } from "../utils.js"; -import { HTTPClient, makeHTTPClient } from "./remote/httpClient.js"; +} from "../backend"; +import { DeferableFunction, DeferredFunction } from "../index"; +import { getEnv, stringify } from "../utils"; +import { HTTPClient, makeHTTPClient } from "./remote/httpClient"; export interface SingleObjectResponse { data: T; diff --git a/src/backend/remote/httpClient.ts b/src/backend/remote/httpClient.ts index 4da9592c..44c1fcf0 100644 --- a/src/backend/remote/httpClient.ts +++ b/src/backend/remote/httpClient.ts @@ -12,9 +12,9 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { DeferError } from "../../backend.js"; -import { errorMessage } from "../../utils.js"; -import VERSION from "../../version.js"; +import { DeferError } from "../../backend"; +import { errorMessage } from "../../utils"; +import VERSION from "../../version"; export type HTTPClient = ( method: string, diff --git a/src/index.ts b/src/index.ts index ef97ee9b..142915c0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,12 +24,12 @@ import { PageRequest, ReRunExecutionResult, RescheduleExecutionResult, -} from "./backend.js"; -import * as localBackend from "./backend/local.js"; -import * as remoteBackend from "./backend/remote.js"; -import { jitter } from "./jitter.js"; -import { info, warn } from "./logger.js"; -import { Duration, fromDurationToDate, getEnv, sleep } from "./utils.js"; +} from "./backend"; +import * as localBackend from "./backend/local"; +import * as remoteBackend from "./backend/remote"; +import { jitter } from "./jitter"; +import { info, warn } from "./logger"; +import { Duration, fromDurationToDate, getEnv, sleep } from "./utils"; const INTERNAL_VERSION = 6; const RETRY_MAX_ATTEMPTS_PLACEHOLDER = 13; diff --git a/src/logger.ts b/src/logger.ts index 54d6a48d..526f0da4 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -12,9 +12,9 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -import { isDebugEnabled } from "./utils.js"; +import { isDebugEnabled } from "./utils"; -// source: https://github.com/csquared/node-logfmt/blob/master/lib/stringify.js +// source: https://github.com/csquared/node-logfmt/blob/master/lib/stringify function fmtData(data: any): string { var line = ""; diff --git a/src/next/asNextRoute.ts b/src/next/asNextRoute.ts index 41ea3438..8e69cd50 100644 --- a/src/next/asNextRoute.ts +++ b/src/next/asNextRoute.ts @@ -1,10 +1,10 @@ import type { NextRequest, NextResponse } from "next/server"; -import { DeferError } from "../backend.js"; +import { DeferError } from "../backend"; import { DeferredFunction, getExecution, getExecutionResult, -} from "../index.js"; +} from "../index"; export interface DeferNextRoute { GetHandler: (request: NextRequest) => Promise; @@ -18,7 +18,7 @@ interface Options< proxy?: (request: NextRequest) => Promise>; } -const ResponseJSON = Response.json; +const ResponseJSON = Responseon; export function asNextRoute Promise>( deferFn: DeferredFunction, @@ -64,7 +64,7 @@ export function asNextRoute Promise>( PostHandler: async (request: NextRequest) => { const args: any = options?.proxy ? await options.proxy(request) - : await request.json(); + : await requeston(); if (Array.isArray(args)) { // @ts-expect-error Charly: need to be refined const execution = await deferFn(...args); diff --git a/src/next/index.ts b/src/next/index.ts index 385d1a11..be1f574d 100644 --- a/src/next/index.ts +++ b/src/next/index.ts @@ -1,2 +1,2 @@ -export { DeferNextRoute, asNextRoute } from "./asNextRoute.js"; -export { UseDeferRoute, useDeferRoute } from "./useDeferRoute.js"; +export { DeferNextRoute, asNextRoute } from "./asNextRoute"; +export { UseDeferRoute, useDeferRoute } from "./useDeferRoute"; diff --git a/src/next/useDeferRoute.ts b/src/next/useDeferRoute.ts index ee809e90..5b34c0f1 100644 --- a/src/next/useDeferRoute.ts +++ b/src/next/useDeferRoute.ts @@ -1,7 +1,7 @@ "use client"; import { useCallback, useRef, useState } from "react"; -import type { ExecutionState } from "../backend.js"; -import type { DeferredFunction } from "../index.js"; +import type { ExecutionState } from "../backend"; +import type { DeferredFunction } from "../index"; export type UseDeferRoute = [ execute: (...args: ARA extends true ? any : A) => void, @@ -40,7 +40,7 @@ export const useDeferRoute = < const res = await fetch(`${routePath}?id=${executionId}`, { method: "GET", }); - const data = (await res.json()) as any; + const data = (await reson()) as any; setStatus(data.state); if (["succeed", "failed"].includes(data.state) && intervalRef.current) { clearInterval(intervalRef.current); @@ -67,7 +67,7 @@ export const useDeferRoute = < "Content-type": "application/json", }, }); - const data = await result.json(); + const data = await resulton(); intervalRef.current = setInterval( pollExecution(data.id), Math.max(500, refreshInterval), diff --git a/tests/backend/local.test.ts b/tests/backend/local.test.ts index b915ee26..ea758023 100644 --- a/tests/backend/local.test.ts +++ b/tests/backend/local.test.ts @@ -1,15 +1,15 @@ process.env["DEFER_NO_LOCAL_SCHEDULER"] = "1"; process.env["DEFER_NO_BANNER"] = "1"; -import { ExecutionNotFound } from "../../src/backend.js"; +import { ExecutionNotFound } from "../../src/backend"; import { cancelExecution, enqueue, getExecution, reRunExecution, rescheduleExecution, -} from "../../src/backend/local.js"; -import { defer } from "../../src/index.js"; +} from "../../src/backend/local"; +import { defer } from "../../src/index"; const myFunc = async () => console.log("the cake is a lie"); const myDeferedFunc = defer(myFunc); diff --git a/tests/backend/local/counter.test.ts b/tests/backend/local/counter.test.ts index 3dad3871..980bf293 100644 --- a/tests/backend/local/counter.test.ts +++ b/tests/backend/local/counter.test.ts @@ -1,4 +1,4 @@ -import { Counter } from "../../../src/backend/local/counter.js"; +import { Counter } from "../../../src/backend/local/counter"; describe("incr/1", () => { describe("increment non existing key", () => { diff --git a/tests/backend/local/kv.test.ts b/tests/backend/local/kv.test.ts index 720c1499..b908938c 100644 --- a/tests/backend/local/kv.test.ts +++ b/tests/backend/local/kv.test.ts @@ -1,4 +1,4 @@ -import { KV } from "../../../src/backend/local/kv.js"; +import { KV } from "../../../src/backend/local/kv"; describe("set/2", () => { it("adds value in the store", async () => { diff --git a/tests/backend/remote.test.ts b/tests/backend/remote.test.ts index 014ce987..c27d3d0a 100644 --- a/tests/backend/remote.test.ts +++ b/tests/backend/remote.test.ts @@ -7,7 +7,7 @@ import { ExecutionNotCancellable, ExecutionNotFound, ExecutionNotReschedulable, -} from "../../src/backend.js"; +} from "../../src/backend"; import { cancelExecution, enqueue, @@ -16,8 +16,8 @@ import { listExecutions, reRunExecution, rescheduleExecution, -} from "../../src/backend/remote.js"; -import { defer } from "../../src/index.js"; +} from "../../src/backend/remote"; +import { defer } from "../../src/index"; global.fetch = jest.fn(); diff --git a/tests/backend/remote/httpClient.test.ts b/tests/backend/remote/httpClient.test.ts index 3428984b..8a576e0b 100644 --- a/tests/backend/remote/httpClient.test.ts +++ b/tests/backend/remote/httpClient.test.ts @@ -1,7 +1,7 @@ import { makeHTTPClient, ClientError, -} from "../../../src/backend/remote/httpClient.js"; +} from "../../../src/backend/remote/httpClient"; global.fetch = jest.fn(); diff --git a/tests/index.test.ts b/tests/index.test.ts index 55d2357d..166438ee 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -15,7 +15,7 @@ import { listExecutions, reRunExecution, rescheduleExecution, -} from "../src/index.js"; +} from "../src/index"; var stop: () => Promise | undefined; diff --git a/tests/logger.test.ts b/tests/logger.test.ts index b4e593d6..59745938 100644 --- a/tests/logger.test.ts +++ b/tests/logger.test.ts @@ -1,4 +1,4 @@ -import { error, info, log, warn } from "../src/logger.js"; +import { error, info, log, warn } from "../src/logger"; describe("log/3", () => { it("test without data", () => { diff --git a/tests/next/asNextRoute.spec.ts b/tests/next/asNextRoute.spec.ts index 4681531e..2e72152c 100644 --- a/tests/next/asNextRoute.spec.ts +++ b/tests/next/asNextRoute.spec.ts @@ -36,7 +36,7 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith(); expect(res.status).toBe(200); - expect(await res.json()).toEqual({ id: "test-id" }); + expect(await reson()).toEqual({ id: "test-id" }); }); }); @@ -62,7 +62,7 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith("charly"); expect(res.status).toBe(200); - expect(await res.json()).toEqual({ id: "test-id" }); + expect(await reson()).toEqual({ id: "test-id" }); }); }); @@ -85,7 +85,7 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith("prefix-charly"); expect(res.status).toBe(200); - expect(await res.json()).toEqual({ id: "test-id" }); + expect(await reson()).toEqual({ id: "test-id" }); }); }); }); @@ -152,7 +152,7 @@ describe("asNextRoute()", () => { } expect(res.status).toBe(expectedResponseStatus); - expect(await res.json()).toEqual(expectedResponseBody); + expect(await reson()).toEqual(expectedResponseBody); }); } ); diff --git a/tests/next/fixtures/routeWithProxy.ts b/tests/next/fixtures/routeWithProxy.ts index ea0bf2c5..f3e12a49 100644 --- a/tests/next/fixtures/routeWithProxy.ts +++ b/tests/next/fixtures/routeWithProxy.ts @@ -4,7 +4,7 @@ import helloWorld from "./helloWorld"; const { GetHandler, PostHandler } = asNextRoute(helloWorld, { // @ts-expect-error too strict return type async proxy(request) { - const args = await request.json(); + const args = await requeston(); return [`prefix-${args[0]}`]; }, }); diff --git a/tests/utils.test.ts b/tests/utils.test.ts index bc9b221f..03728c47 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -3,7 +3,7 @@ import { getEnv, isDebugEnabled, randomUUID, -} from "../src/utils.js"; +} from "../src/utils"; describe("randomUUID/0", () => { describe("when globalThis.crypto exist", () => { From c0322041cbab181db410146d91989cee3eaf9045 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:09:48 +0100 Subject: [PATCH 10/15] typos --- src/next/asNextRoute.ts | 4 ++-- src/next/useDeferRoute.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/next/asNextRoute.ts b/src/next/asNextRoute.ts index 8e69cd50..0fea16cd 100644 --- a/src/next/asNextRoute.ts +++ b/src/next/asNextRoute.ts @@ -18,7 +18,7 @@ interface Options< proxy?: (request: NextRequest) => Promise>; } -const ResponseJSON = Responseon; +const ResponseJSON = Response.json; export function asNextRoute Promise>( deferFn: DeferredFunction, @@ -64,7 +64,7 @@ export function asNextRoute Promise>( PostHandler: async (request: NextRequest) => { const args: any = options?.proxy ? await options.proxy(request) - : await requeston(); + : await request.json(); if (Array.isArray(args)) { // @ts-expect-error Charly: need to be refined const execution = await deferFn(...args); diff --git a/src/next/useDeferRoute.ts b/src/next/useDeferRoute.ts index 5b34c0f1..aaf32709 100644 --- a/src/next/useDeferRoute.ts +++ b/src/next/useDeferRoute.ts @@ -40,7 +40,7 @@ export const useDeferRoute = < const res = await fetch(`${routePath}?id=${executionId}`, { method: "GET", }); - const data = (await reson()) as any; + const data = (await res.json()) as any; setStatus(data.state); if (["succeed", "failed"].includes(data.state) && intervalRef.current) { clearInterval(intervalRef.current); @@ -67,7 +67,7 @@ export const useDeferRoute = < "Content-type": "application/json", }, }); - const data = await resulton(); + const data = await result.json(); intervalRef.current = setInterval( pollExecution(data.id), Math.max(500, refreshInterval), From 5c187684377c717ecc669c4fefd50430a61b1c4a Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 11:11:17 +0100 Subject: [PATCH 11/15] style --- src/backend/local.ts | 32 ++++++++++++++++---------------- src/next/asNextRoute.ts | 6 +----- src/version.ts | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/backend/local.ts b/src/backend/local.ts index 81887016..8393923e 100644 --- a/src/backend/local.ts +++ b/src/backend/local.ts @@ -86,7 +86,7 @@ const banner = ` function paginate( page: PageRequest | undefined, - records: Map + records: Map, ): PageResult { let edges = Array.from(records.keys()).reverse(); let hasNextPage: boolean = false; @@ -137,7 +137,7 @@ function paginate( function isExecutionMatchFilter( filters: ExecutionFilters | undefined, - execution: InternalExecution + execution: InternalExecution, ): boolean { if ( filters?.states && @@ -163,13 +163,13 @@ function isExecutionMatchFilter( if (filters?.metadata && filters.metadata.length > 0 && execution.metadata) { const metadataFilters = filters.metadata.filter( - (mdFilter) => mdFilter.values.length > 0 + (mdFilter) => mdFilter.values.length > 0, ); if ( !metadataFilters.some((mdFilter) => mdFilter.values.some( - (value) => execution.metadata[mdFilter.key] === value - ) + (value) => execution.metadata[mdFilter.key] === value, + ), ) ) { return false; @@ -264,7 +264,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); return execution; - } + }, ); if (shouldDiscard) { @@ -320,7 +320,7 @@ async function loop(shouldRun: () => boolean): Promise { execution.updatedAt = new Date(); execution.errorCode = errorCode; return execution; - } + }, ); }; @@ -342,7 +342,7 @@ export async function enqueue( args: Parameters, scheduleFor: Date, discardAfter: Date | undefined, - metadata: { [key: string]: string } | undefined + metadata: { [key: string]: string } | undefined, ): Promise { let functionId = functionIdMapping.get(func.__metadata.name); if (functionId === undefined) { @@ -396,7 +396,7 @@ export async function getExecutionResult(id: string): Promise { export async function cancelExecution( id: string, - force: boolean + force: boolean, ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -407,7 +407,7 @@ export async function cancelExecution( switch (execution.state) { case "aborting": throw new ExecutionAbortingAlreadyInProgress( - "aborting execution already in progress" + "aborting execution already in progress", ); case "created": execution.state = "cancelled"; @@ -417,7 +417,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state` + `cannot cancel execution in "${execution.state}" state`, ); } } else { @@ -427,7 +427,7 @@ export async function cancelExecution( break; default: throw new ExecutionNotCancellable( - `cannot cancel execution in "${execution.state}" state` + `cannot cancel execution in "${execution.state}" state`, ); } } @@ -441,7 +441,7 @@ export async function cancelExecution( export async function rescheduleExecution( id: string, - scheduleFor: Date + scheduleFor: Date, ): Promise { let execution = await executionsStore.get(id); if (execution === undefined) @@ -460,7 +460,7 @@ export async function rescheduleExecution( } export async function reRunExecution( - id: string + id: string, ): Promise { const execution = await executionsStore.get(id); if (execution === undefined) @@ -487,7 +487,7 @@ export async function reRunExecution( export async function listExecutions( pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); @@ -505,7 +505,7 @@ export async function listExecutions( export async function listExecutionAttempts( id: string, pageRequest?: PageRequest, - filters?: ExecutionFilters + filters?: ExecutionFilters, ): Promise { const executionIds = await executionsStore.keys(); const data = new Map(); diff --git a/src/next/asNextRoute.ts b/src/next/asNextRoute.ts index 0fea16cd..73c39e0c 100644 --- a/src/next/asNextRoute.ts +++ b/src/next/asNextRoute.ts @@ -1,10 +1,6 @@ import type { NextRequest, NextResponse } from "next/server"; import { DeferError } from "../backend"; -import { - DeferredFunction, - getExecution, - getExecutionResult, -} from "../index"; +import { DeferredFunction, getExecution, getExecutionResult } from "../index"; export interface DeferNextRoute { GetHandler: (request: NextRequest) => Promise; diff --git a/src/version.ts b/src/version.ts index 6fa4e169..e1629e70 100644 --- a/src/version.ts +++ b/src/version.ts @@ -12,4 +12,4 @@ // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. -export default "unknown"; \ No newline at end of file +export default "unknown"; From e5990352f37fcf9edd15f24830b07c0674a17e47 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Tue, 5 Mar 2024 15:59:36 +0100 Subject: [PATCH 12/15] fixes --- package.json | 2 +- src/logger.ts | 2 +- ...waitResult.spec.ts => awaitResult.test.ts} | 8 +++---- ...sNextRoute.spec.ts => asNextRoute.test.ts} | 21 +++++++++---------- tests/next/fixtures/routeWithProxy.ts | 2 +- ...ferRoute.spec.ts => useDeferRoute.test.ts} | 0 tsconfig.build.json | 17 +++++++-------- 7 files changed, 25 insertions(+), 27 deletions(-) rename tests/{awaitResult.spec.ts => awaitResult.test.ts} (92%) rename tests/next/{asNextRoute.spec.ts => asNextRoute.test.ts} (91%) rename tests/next/{useDeferRoute.spec.ts => useDeferRoute.test.ts} (100%) diff --git a/package.json b/package.json index af38c2be..ff3428a6 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "/tests/", "node_modules/", "coverage/", - "", + "dist/", "package-lock.json" ], "overrides": [ diff --git a/src/logger.ts b/src/logger.ts index 526f0da4..563a451d 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -14,7 +14,7 @@ import { isDebugEnabled } from "./utils"; -// source: https://github.com/csquared/node-logfmt/blob/master/lib/stringify +// source: https://github.com/csquared/node-logfmt/blob/master/lib/stringify.js function fmtData(data: any): string { var line = ""; diff --git a/tests/awaitResult.spec.ts b/tests/awaitResult.test.ts similarity index 92% rename from tests/awaitResult.spec.ts rename to tests/awaitResult.test.ts index 4b36c095..d9720d86 100644 --- a/tests/awaitResult.spec.ts +++ b/tests/awaitResult.test.ts @@ -1,9 +1,9 @@ import { awaitResult, defer } from "../src"; -import { DeferError } from "../src/errors"; -import { makeHTTPClient } from "../src/httpClient"; +import { DeferError } from "../src/backend"; +import { makeHTTPClient } from "../src/backend/remote/httpClient"; import { jitter } from "../src/jitter"; -jest.mock("../src/httpClient"); +jest.mock("../src/backend/remote/httpClient"); jest.mock("../src/jitter"); jest.setTimeout(20000); @@ -57,7 +57,7 @@ describe("awaitResult(deferFn)", () => { const awaitable = awaitResult(deferred); const result = await awaitable(""); - expect(result).toEqual("coucou"); + expect(result).toEqual("Hello World!"); expect(myFunction).not.toHaveBeenCalled(); }); }); diff --git a/tests/next/asNextRoute.spec.ts b/tests/next/asNextRoute.test.ts similarity index 91% rename from tests/next/asNextRoute.spec.ts rename to tests/next/asNextRoute.test.ts index 2e72152c..e15a03fa 100644 --- a/tests/next/asNextRoute.spec.ts +++ b/tests/next/asNextRoute.test.ts @@ -3,7 +3,7 @@ import helloWorld from "./fixtures/helloWorld"; import { POST, GET } from "./fixtures/route"; import { POST as POSTWithProxy } from "./fixtures/routeWithProxy"; import { getExecution } from "../../src/index"; -import { APIError } from "../../src/errors"; +import { ExecutionNotFound } from "../../src/backend"; jest.mock("./fixtures/helloWorld"); jest.mock("../../src/index", () => { @@ -19,7 +19,7 @@ describe("asNextRoute()", () => { describe("POST() - background function invocation", () => { describe("valid empty params, without proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); test("properly call the background function and forward the execution ID", async () => { @@ -36,13 +36,13 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith(); expect(res.status).toBe(200); - expect(await reson()).toEqual({ id: "test-id" }); + expect(await res.json()).toEqual({ id: "test-id" }); }); }); describe("valid params, without proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); afterEach(() => { jest.mocked(helloWorld).mockReset(); @@ -62,13 +62,13 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith("charly"); expect(res.status).toBe(200); - expect(await reson()).toEqual({ id: "test-id" }); + expect(await res.json()).toEqual({ id: "test-id" }); }); }); describe("valid params, with proxy", () => { beforeEach(() => - jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" }) + jest.mocked(helloWorld).mockResolvedValue({ id: "test-id" } as any) ); test("properly call the background function with params and forward the execution ID", async () => { @@ -85,7 +85,7 @@ describe("asNextRoute()", () => { expect(helloWorld).toHaveBeenCalledWith("prefix-charly"); expect(res.status).toBe(200); - expect(await reson()).toEqual({ id: "test-id" }); + expect(await res.json()).toEqual({ id: "test-id" }); }); }); }); @@ -104,7 +104,7 @@ describe("asNextRoute()", () => { "valid query params, invalid execution ID", "?id=not-found-id", "not-found-id", - new APIError("execution not found", ""), + new ExecutionNotFound("execution not found"), [500, { error: "Error: execution not found", id: "not-found-id" }], ], [ @@ -129,8 +129,7 @@ describe("asNextRoute()", () => { throw executionResult; }); } else { - // @ts-expect-error unflexible TS \o/ - jest.mocked(getExecution).mockResolvedValue(executionResult); + jest.mocked(getExecution).mockResolvedValue(executionResult as any); } }); afterEach(() => { @@ -152,7 +151,7 @@ describe("asNextRoute()", () => { } expect(res.status).toBe(expectedResponseStatus); - expect(await reson()).toEqual(expectedResponseBody); + expect(await res.json()).toEqual(expectedResponseBody); }); } ); diff --git a/tests/next/fixtures/routeWithProxy.ts b/tests/next/fixtures/routeWithProxy.ts index f3e12a49..ea0bf2c5 100644 --- a/tests/next/fixtures/routeWithProxy.ts +++ b/tests/next/fixtures/routeWithProxy.ts @@ -4,7 +4,7 @@ import helloWorld from "./helloWorld"; const { GetHandler, PostHandler } = asNextRoute(helloWorld, { // @ts-expect-error too strict return type async proxy(request) { - const args = await requeston(); + const args = await request.json(); return [`prefix-${args[0]}`]; }, }); diff --git a/tests/next/useDeferRoute.spec.ts b/tests/next/useDeferRoute.test.ts similarity index 100% rename from tests/next/useDeferRoute.spec.ts rename to tests/next/useDeferRoute.test.ts diff --git a/tsconfig.build.json b/tsconfig.build.json index ce570190..d01964e6 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,10 +1,9 @@ { - "extends": "./tsconfig.json", - "exclude": [ - "src/**/*.spec.ts", - "src/**/*.test.ts", - "src/test/**/*", - "scripts/**/*" - ] - } - \ No newline at end of file + "extends": "./tsconfig.json", + "exclude": [ + "src/**/*.spec.ts", + "src/**/*.test.ts", + "src/test/**/*", + "scripts/**/*" + ] +} \ No newline at end of file From 0c4e0159193b0e1c957f1cee08f3b673caf20f51 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Wed, 6 Mar 2024 09:41:46 +0100 Subject: [PATCH 13/15] remove deprecated tests --- tests/awaitResult.test.ts | 80 --------------------------------------- 1 file changed, 80 deletions(-) delete mode 100644 tests/awaitResult.test.ts diff --git a/tests/awaitResult.test.ts b/tests/awaitResult.test.ts deleted file mode 100644 index d9720d86..00000000 --- a/tests/awaitResult.test.ts +++ /dev/null @@ -1,80 +0,0 @@ -import { awaitResult, defer } from "../src"; -import { DeferError } from "../src/backend"; -import { makeHTTPClient } from "../src/backend/remote/httpClient"; -import { jitter } from "../src/jitter"; - -jest.mock("../src/backend/remote/httpClient"); -jest.mock("../src/jitter"); -jest.setTimeout(20000); - -describe("awaitResult(deferFn)", () => { - describe("common", () => { - it("defer() raises a type error is function is not async", () => { - function myFunction(_customerId: string) {} - - // @ts-expect-error Type 'void' is not assignable to type 'Promise'.ts(2345) - const deferred = defer(myFunction); - }); - }); - - describe("when Defer is active (`DEFER_TOKEN` is set)", () => { - describe("with successful functions execution", () => { - beforeAll(() => { - const responseFn = jest - .fn() - // first `/exec` call - .mockImplementationOnce(() => { - return { id: "1" }; - }) - // first `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "created" }; - }) - // second `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "started" }; - }) - // third `/runs/:id` call - .mockImplementationOnce(() => { - return { id: "1", state: "succeed", result: "coucou" }; - }); - - jest.mocked(jitter).mockReturnValue(0); - jest - .mocked(makeHTTPClient) - // first `/exec` call - .mockImplementation(() => async () => { - return responseFn(); - }); - - process.env["DEFER_TOKEN"] = "test"; - process.env["DEFER_DEBUG"] = "1"; - }); - - it("should NOT call the wrapped function and return the function execution result", async () => { - const myFunction = jest.fn(async (_str: string) => "Hello World!"); - const deferred = defer(myFunction); - - const awaitable = awaitResult(deferred); - const result = await awaitable(""); - expect(result).toEqual("Hello World!"); - expect(myFunction).not.toHaveBeenCalled(); - }); - }); - }); - - describe("when Defer is inactive (`DEFER_TOKEN` is unset)", () => { - beforeAll(() => { - process.env["DEFER_TOKEN"] = ""; - }); - it("should rethrow any error as `DeferError`", async () => { - const myFunction = async (_str: string) => { - throw new Error("my function failed locally!"); - }; - const deferred = defer(myFunction); - const awaitable = awaitResult(deferred); - - await expect(awaitable("")).rejects.toThrow(DeferError); - }); - }); -}); From d88da58c1f0ca8defe45352aad76ff844daae6aa Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Wed, 6 Mar 2024 09:47:22 +0100 Subject: [PATCH 14/15] fix test hanging up --- tests/next/asNextRoute.test.ts | 13 ++++++++++++- tests/next/useDeferRoute.test.ts | 10 ---------- 2 files changed, 12 insertions(+), 11 deletions(-) delete mode 100644 tests/next/useDeferRoute.test.ts diff --git a/tests/next/asNextRoute.test.ts b/tests/next/asNextRoute.test.ts index e15a03fa..898dad3b 100644 --- a/tests/next/asNextRoute.test.ts +++ b/tests/next/asNextRoute.test.ts @@ -2,7 +2,7 @@ import { NextRequest } from "next/server"; import helloWorld from "./fixtures/helloWorld"; import { POST, GET } from "./fixtures/route"; import { POST as POSTWithProxy } from "./fixtures/routeWithProxy"; -import { getExecution } from "../../src/index"; +import { backend, getExecution } from "../../src/index"; import { ExecutionNotFound } from "../../src/backend"; jest.mock("./fixtures/helloWorld"); @@ -15,6 +15,17 @@ jest.mock("../../src/index", () => { }; }); +var stop: () => Promise | undefined; + +beforeAll(function () { + stop = (backend as any).start(); +}); + +afterAll(function () { + stop(); +}); + + describe("asNextRoute()", () => { describe("POST() - background function invocation", () => { describe("valid empty params, without proxy", () => { diff --git a/tests/next/useDeferRoute.test.ts b/tests/next/useDeferRoute.test.ts deleted file mode 100644 index 8be58858..00000000 --- a/tests/next/useDeferRoute.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -// import { useDeferRoute } from "../../src/next/useDeferRoute"; -// import helloWorld from "./fixtures/helloWorld"; - -describe("useDeferRoute()", () => { - test("1+1", () => { - expect(1 + 1).toEqual(2); - }); -}); - -// const [coucou, { loading, result }] = useDeferRoute('') From 49d104989dee1ada474ee6863159cb9416b44c98 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Wed, 6 Mar 2024 10:14:48 +0100 Subject: [PATCH 15/15] fix tests --- tests/next/asNextRoute.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/next/asNextRoute.test.ts b/tests/next/asNextRoute.test.ts index 898dad3b..3c32208b 100644 --- a/tests/next/asNextRoute.test.ts +++ b/tests/next/asNextRoute.test.ts @@ -1,3 +1,6 @@ +process.env["DEFER_NO_LOCAL_SCHEDULER"] = "1"; +process.env["DEFER_NO_BANNER"] = "1"; + import { NextRequest } from "next/server"; import helloWorld from "./fixtures/helloWorld"; import { POST, GET } from "./fixtures/route";