diff --git a/apps/tests/package.json b/apps/tests/package.json index 3dac807c..89faa1f8 100644 --- a/apps/tests/package.json +++ b/apps/tests/package.json @@ -5,7 +5,7 @@ "type": "module", "description": "nx.js API tests", "scripts": { - "build": "esbuild --bundle --sourcemap --sources-content=false --target=es2022 --format=esm --outdir=romfs src/main.ts", + "build": "esbuild --bundle --sourcemap --sources-content=false --target=es2022 --format=esm --outdir=romfs --define:process.env.NXJS=\\\"1\\\" src/main.ts", "nro": "nxjs-nro", "nsp": "nxjs-nsp" }, diff --git a/apps/tests/src/crypto.test.ts b/apps/tests/src/crypto.test.ts index 1b05e442..81342fef 100644 --- a/apps/tests/src/crypto.test.ts +++ b/apps/tests/src/crypto.test.ts @@ -1,35 +1,9 @@ import { suite } from 'uvu'; import * as assert from 'uvu/assert'; +import { toHex, fromHex, concat } from './util.ts'; const test = suite('crypto'); -const isNXJS = typeof Switch !== 'undefined'; - -function toHex(arr: ArrayBuffer) { - return Array.from(new Uint8Array(arr)) - .map((v) => v.toString(16).padStart(2, '0')) - .join(''); -} - -function fromHex(hex: string): ArrayBuffer { - const arr = new Uint8Array(hex.length / 2); - for (let i = 0; i < hex.length; i += 2) { - arr[i / 2] = parseInt(hex.substr(i, 2), 16); - } - return arr.buffer; -} - -function concat(...buffers: ArrayBuffer[]): ArrayBuffer { - const size = buffers.reduce((acc, buf) => acc + buf.byteLength, 0); - let offset = 0; - const result = new Uint8Array(size); - for (const buf of buffers) { - result.set(new Uint8Array(buf), offset); - offset += buf.byteLength; - } - return result.buffer; -} - test('`crypto.getRandomValues()`', () => { const arr = new Uint8Array(5); assert.equal(toHex(arr.buffer), '0000000000'); @@ -234,7 +208,7 @@ test("`crypto.subtle.decrypt()` with 'AES-CBC' algorithm, 256-bit key", async () }); // Non-standard APIs that are only going to work on nx.js -if (isNXJS) { +if (process.env.NXJS === '1') { test("`crypto.subtle.importKey()` with 'raw' format and 'AES-XTS' algorithm, two 128-bit keys", async () => { const key0 = fromHex('0a316b344a7bc7b4db239c61017b86f7'); const key1 = fromHex('cc4f4df7cb2becb9a307bb17e8eb01f3'); diff --git a/apps/tests/src/types.d.ts b/apps/tests/src/types.d.ts new file mode 100644 index 00000000..d246d367 --- /dev/null +++ b/apps/tests/src/types.d.ts @@ -0,0 +1,8 @@ +/** + * `NXJS` env var is defined via esbuild's `--define` option. + */ +declare var process: { + env: { + NXJS: string; + }; +}; diff --git a/apps/tests/src/util.ts b/apps/tests/src/util.ts new file mode 100644 index 00000000..331ff864 --- /dev/null +++ b/apps/tests/src/util.ts @@ -0,0 +1,24 @@ +export function toHex(arr: ArrayBuffer): string { + return Array.from(new Uint8Array(arr)) + .map((v) => v.toString(16).padStart(2, '0')) + .join(''); +} + +export function fromHex(hex: string): ArrayBuffer { + const arr = new Uint8Array(hex.length / 2); + for (let i = 0; i < hex.length; i += 2) { + arr[i / 2] = parseInt(hex.substr(i, 2), 16); + } + return arr.buffer; +} + +export function concat(...buffers: ArrayBuffer[]): ArrayBuffer { + const size = buffers.reduce((acc, buf) => acc + buf.byteLength, 0); + let offset = 0; + const result = new Uint8Array(size); + for (const buf of buffers) { + result.set(new Uint8Array(buf), offset); + offset += buf.byteLength; + } + return result.buffer; +} diff --git a/apps/tests/tsconfig.json b/apps/tests/tsconfig.json index ca3234f7..18c39416 100644 --- a/apps/tests/tsconfig.json +++ b/apps/tests/tsconfig.json @@ -8,6 +8,7 @@ "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, + "allowImportingTsExtensions": true, "types": [ "@nx.js/runtime" ]