refactor: permit build code to run with other JS runtime
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 12 Jul 2024 22:55:56 +0000 (00:55 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 12 Jul 2024 22:55:56 +0000 (00:55 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
.vscode/settings.json
build-requirements.js
package.json
tests/utils/Utils.test.ts
utils/runtime.js [new file with mode: 0644]

index adb17d15ef744ec390624a47d166930504a0dda2..ba2b5805ca08727eafa0b6ca27b4b949382119a3 100644 (file)
     "rambda",
     "Recurrency",
     "RFID",
+    "runtimes",
     "shutdowning",
     "sonarlint",
     "SRPC",
     "tsdoc",
     "VCAP",
     "webui",
+    "workerd",
     "workerset"
   ]
 }
index 501933c4fa5ae659809f3ab8f96f13f710d4caa4..c4f5dca97960dec7d1181ff7c6a27de7780bcf5c 100644 (file)
@@ -5,6 +5,8 @@ import chalk from 'chalk'
 // eslint-disable-next-line n/no-unpublished-import
 import { satisfies } from 'semver'
 
+import { runtime, runtimes } from './utils/runtime.js'
+
 const packageJson = JSON.parse(readFileSync('./package.json', 'utf8'))
 
 /**
@@ -23,4 +25,15 @@ export const checkNodeVersion = () => {
   }
 }
 
-checkNodeVersion()
+switch (runtime) {
+  case runtimes.node:
+    checkNodeVersion()
+    break
+  case runtimes.bun:
+  case runtimes.deno:
+  case runtimes.workerd:
+  case runtimes.browser:
+  default:
+    console.error(chalk.red(`Unsupported ${runtime} runtime detected`))
+    break
+}
index b8f1c78215f583f4601b46d08a4f40017d811fba..6b8350b57c29970a23e4043da5874e618e038630 100644 (file)
@@ -47,7 +47,7 @@
   },
   "scripts": {
     "prepare": "node prepare.js",
-    "build-requirements": "node --no-warnings build-requirements.js",
+    "build-requirements": "node build-requirements.js",
     "start": "pnpm build && cross-env NODE_ENV=production node dist/start.js",
     "start:dev": "pnpm build:dev && cross-env NODE_ENV=development node --enable-source-maps dist/start.js",
     "start:dev:debug": "pnpm build:dev && cross-env NODE_ENV=development node --enable-source-maps --inspect dist/start.js",
index 6261beebe01d6abc7e1d89cf1db195cb07c1552f..b39c58578a2b1c47dd32a5d6a56b548878907d34 100644 (file)
@@ -33,6 +33,7 @@ import {
   sleep,
   validateUUID
 } from '../../src/utils/Utils.js'
+import { runtime, runtimes } from '../../utils/runtime.js'
 
 await describe('Utils test suite', async () => {
   await it('Verify generateUUID()/validateUUID()', () => {
@@ -315,7 +316,7 @@ await describe('Utils test suite', async () => {
     const date = new Date()
     expect(clone(date)).toStrictEqual(date)
     expect(clone(date) === date).toBe(false)
-    if (satisfies(version, '>=21.0.0')) {
+    if (runtime === runtimes.node && satisfies(version, '>=17.0.0')) {
       const url = new URL('https://domain.tld')
       expect(() => clone(url)).toThrowError(new Error('Cannot clone object of unsupported type.'))
     }
diff --git a/utils/runtime.js b/utils/runtime.js
new file mode 100644 (file)
index 0000000..ae52ed6
--- /dev/null
@@ -0,0 +1,25 @@
+export const runtimes = {
+  bun: 'bun',
+  deno: 'deno',
+  node: 'node',
+  workerd: 'workerd',
+  browser: 'browser'
+}
+
+const isBun = !!globalThis.Bun || !!globalThis.process?.versions?.bun
+const isDeno = !!globalThis.Deno
+const isNode = globalThis.process?.release?.name === 'node'
+// eslint-disable-next-line n/no-unsupported-features/node-builtins
+const isWorkerd = globalThis.navigator?.userAgent === 'Cloudflare-Workers'
+// eslint-disable-next-line n/no-unsupported-features/node-builtins
+const isBrowser = !!globalThis.navigator
+
+export const runtime = (() => {
+  if (isBun) return runtimes.bun
+  if (isDeno) return runtimes.deno
+  if (isNode) return runtimes.node
+  if (isWorkerd) return runtimes.workerd
+  if (isBrowser) return runtimes.browser
+
+  return 'unknown'
+})()