detect-arch 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/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "detect-arch",
3
+ "version": "1.0.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "​A CPU architecture detection library without relying on User Agent strings or Browser APIs.\nInferring the host CPU architecture by observing NaN bit patterns.",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/typeling1578/detect-arch.git"
12
+ },
13
+ "keywords": [
14
+ "arch",
15
+ "architecture",
16
+ "cpu",
17
+ "cpu info",
18
+ "browser"
19
+ ],
20
+ "author": "typeling1578 <pub@typeling1578.dev>",
21
+ "homepage": "https://github.com/typeling1578/detect-arch",
22
+ "bugs": "https://github.com/typeling1578/detect-arch/issues",
23
+ "main": "./dist/detect-arch.cjs",
24
+ "module": "./dist/detect-arch.mjs",
25
+ "types": "./types/detect-arch.d.ts",
26
+ "exports": {
27
+ ".": {
28
+ "types": "./types/detect-arch.d.ts",
29
+ "import": "./dist/detect-arch.mjs",
30
+ "require": "./dist/detect-arch.cjs"
31
+ }
32
+ },
33
+ "engines": {
34
+ "node": ">=20"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "src",
39
+ "types",
40
+ "README.md",
41
+ "LICENSE",
42
+ "CREDITS.md"
43
+ ],
44
+ "devDependencies": {
45
+ "@rollup/plugin-commonjs": "^29.0.2",
46
+ "@rollup/plugin-node-resolve": "^16.0.3",
47
+ "@rollup/plugin-terser": "^1.0.0",
48
+ "rollup": "^4.59.0"
49
+ },
50
+ "scripts": {
51
+ "build:native": "mkdir -p build && emcc ./src/native/detect-arch.c -O0 -s -o ./build/detect-arch-native.mjs -s EXPORTED_FUNCTIONS='[\"_isX86\",\"_isArm\",\"_isRiscV\"]' -s MODULARIZE=1 -s EXPORT_ES6=1 -s ENVIRONMENT=web,node -s SINGLE_FILE=1",
52
+ "build": "pnpm run build:native && rollup -c"
53
+ }
54
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,27 @@
1
+ import createModule from "../build/detect-arch-native.mjs";
2
+
3
+ let wasmModule;
4
+
5
+ async function initialize() {
6
+ if (wasmModule) return wasmModule;
7
+ wasmModule = await createModule();
8
+ return wasmModule;
9
+ }
10
+
11
+ export async function isX86() {
12
+ return Boolean((await initialize())._isX86());
13
+ }
14
+
15
+ export async function isArm() {
16
+ return Boolean((await initialize())._isArm());
17
+ }
18
+
19
+ export async function isRiscV() {
20
+ return Boolean((await initialize())._isRiscV());
21
+ }
22
+
23
+ export default {
24
+ isX86,
25
+ isArm,
26
+ isRiscV
27
+ }
@@ -0,0 +1,51 @@
1
+ #include <math.h>
2
+ #include <stdint.h>
3
+ #include <string.h>
4
+
5
+ #ifdef BUILD_EXE
6
+ #include <stdio.h>
7
+ #endif
8
+
9
+ int is_nan_signbit_set_from_inf_sub() {
10
+ // https://github.com/google/XNNPACK/blob/5b045a6bb26cf138822855cd8a879195cd2c7437/src/configs/hardware-config.c#L243-L250
11
+ static const volatile float inf = INFINITY;
12
+ return signbit(inf - inf);
13
+ }
14
+
15
+ int is_canonical_nan_after_op() {
16
+ uint32_t qnan_with_payload_bits = 0x7fc01578;
17
+ float qnan_with_payload;
18
+ memcpy(&qnan_with_payload, &qnan_with_payload_bits, 4);
19
+
20
+ float result = qnan_with_payload + 1.0f;
21
+ uint32_t bits;
22
+ memcpy(&bits, &result, 4);
23
+
24
+ return bits == 0x7fc00000;
25
+ }
26
+
27
+ int isX86() {
28
+ return is_nan_signbit_set_from_inf_sub();
29
+ }
30
+
31
+ int isArm() {
32
+ return !is_nan_signbit_set_from_inf_sub() && !is_canonical_nan_after_op();
33
+ }
34
+
35
+ int isRiscV() {
36
+ return !is_nan_signbit_set_from_inf_sub() && is_canonical_nan_after_op();
37
+ }
38
+
39
+ #ifdef BUILD_EXE
40
+ int main(void) {
41
+ if (isX86()) {
42
+ printf("x86\n");
43
+ }
44
+ if (isArm()) {
45
+ printf("Arm\n");
46
+ }
47
+ if (isRiscV()) {
48
+ printf("RISC-V\n");
49
+ }
50
+ }
51
+ #endif
@@ -0,0 +1,11 @@
1
+ export function isX86(): Promise<boolean>;
2
+
3
+ export function isArm(): Promise<boolean>;
4
+
5
+ export function isRiscV(): Promise<boolean>;
6
+
7
+ export default {
8
+ isX86: typeof isX86,
9
+ isArm: typeof isArm,
10
+ isRiscV: typeof isRiscV
11
+ }