Skip to content

Commit

Permalink
Set process.env.NXJS via esbuild's --define option in tests app
Browse files Browse the repository at this point in the history
So that non-nx.js code will be removed at build time
  • Loading branch information
TooTallNate committed Dec 31, 2024
1 parent 7edd81f commit 816d284
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
2 changes: 1 addition & 1 deletion apps/tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
30 changes: 2 additions & 28 deletions apps/tests/src/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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');
Expand Down
8 changes: 8 additions & 0 deletions apps/tests/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* `NXJS` env var is defined via esbuild's `--define` option.
*/
declare var process: {
env: {
NXJS: string;
};
};
24 changes: 24 additions & 0 deletions apps/tests/src/util.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions apps/tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"allowImportingTsExtensions": true,
"types": [
"@nx.js/runtime"
]
Expand Down

0 comments on commit 816d284

Please sign in to comment.