Skip to content

Commit 566bd00

Browse files
committed
simplify the code
1 parent 29f5afa commit 566bd00

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

src/utils/utf8-wasm.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,20 @@
1111

1212
import { wasmBinary } from "./utf8-wasm-binary.ts";
1313

14-
// Check environment variable for wasm mode
15-
declare const process: { env?: Record<string, string | undefined> } | undefined;
16-
1714
function getWasmMode(): "force" | "never" | "auto" {
18-
try {
19-
if (process?.env) {
20-
const mode = process.env["MSGPACK_WASM"];
21-
if (mode) {
22-
switch (mode.toLowerCase()) {
23-
case "force":
24-
return "force";
25-
case "never":
26-
return "never";
27-
default:
28-
return "auto";
29-
}
15+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
16+
if (typeof process !== "undefined" && process.env) {
17+
const mode = process.env["MSGPACK_WASM"];
18+
if (mode) {
19+
switch (mode.toLowerCase()) {
20+
case "force":
21+
return "force";
22+
case "never":
23+
return "never";
24+
default:
25+
return "auto";
3026
}
3127
}
32-
} catch {
33-
// process may not be defined in browser
3428
}
3529
return "auto";
3630
}
@@ -53,16 +47,22 @@ let wasmInstance: WasmExports | null = null;
5347
let wasmInitError: Error | null = null;
5448

5549
function base64ToBytes(base64: string): Uint8Array {
56-
if (typeof atob === "function") {
50+
// @ts-expect-error - fromBase64 is not yet supported in TypeScript
51+
if (Uint8Array.fromBase64) {
52+
// @ts-expect-error - fromBase64 is not yet supported in TypeScript
53+
return Uint8Array.fromBase64(base64);
54+
} else if (typeof Buffer !== "undefined") {
55+
// Node.js
56+
return new Uint8Array(Buffer.from(base64, "base64"));
57+
} else {
58+
// Legacy fallback
5759
const binary = atob(base64);
5860
const bytes = new Uint8Array(binary.length);
5961
for (let i = 0; i < binary.length; i++) {
6062
bytes[i] = binary.charCodeAt(i);
6163
}
6264
return bytes;
6365
}
64-
// Node.js fallback
65-
return new Uint8Array(Buffer.from(base64, "base64"));
6666
}
6767

6868
function tryInitializeWasmInstance(): void {

tsconfig.json

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,78 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"target": "ES2020", /* the baseline */
5-
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6-
"lib": ["ES2024", "DOM"], /* Specify library files to be included in the compilation. */
4+
"target": "es2020", /* the baseline */
5+
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
6+
"lib": [
7+
"esnext",
8+
"dom"
9+
], /* Specify library files to be included in the compilation. */
710
// "allowJs": true, /* Allow javascript files to be compiled. */
811
// "checkJs": true, /* Report errors in .js files. */
912
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
1013
// "declaration": true, /* Generates corresponding '.d.ts' file. */
1114
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
12-
"sourceMap": true, /* Generates corresponding '.map' file. */
15+
"sourceMap": true, /* Generates corresponding '.map' file. */
1316
// "outFile": "./", /* Concatenate and emit output to single file. */
14-
"outDir": "./build", /* Redirect output structure to the directory. */
17+
"outDir": "./build", /* Redirect output structure to the directory. */
1518
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
1619
// "composite": true, /* Enable project compilation */
17-
"incremental": true, /* Enable incremental compilation */
20+
"incremental": true, /* Enable incremental compilation */
1821
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
1922
// "removeComments": true, /* Do not emit comments to output. */
2023
// "noEmit": true, /* Do not emit outputs. */
21-
"importHelpers": false, /* Import emit helpers from 'tslib'. */
24+
"importHelpers": false, /* Import emit helpers from 'tslib'. */
2225
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
2326
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
24-
2527
/* Strict Type-Checking Options */
26-
"strict": true, /* Enable all strict type-checking options. */
28+
"strict": true, /* Enable all strict type-checking options. */
2729
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
2830
// "strictNullChecks": true, /* Enable strict null checks. */
2931
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
3032
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
3133
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
3234
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
3335
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
34-
3536
/* Additional Checks */
3637
// "noUnusedLocals": true, /* Report errors on unused locals. */
3738
// "noUnusedParameters": true, /* Report errors on unused parameters. */
38-
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
39-
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
39+
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
40+
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
4041
"noUncheckedIndexedAccess": true,
4142
"noPropertyAccessFromIndexSignature": true,
4243
"noImplicitOverride": true,
4344
"verbatimModuleSyntax": false,
4445
"allowImportingTsExtensions": true,
4546
"noEmit": true,
46-
4747
/* Module Resolution Options */
48-
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
48+
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4949
// "paths": {
5050
// "@msgpack/msgpack": ["./src"]
5151
// }, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
5252
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
5353
// "typeRoots": [], /* List of folders to include type definitions from. */
5454
// "types": [], /* Type declaration files to be included in compilation. */
5555
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
56-
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
56+
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5757
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
5858
"resolveJsonModule": true,
5959
"skipLibCheck": true,
6060
"forceConsistentCasingInFileNames": true
61-
6261
// "erasableSyntaxOnly": true
63-
6462
/* Source Map Options */
6563
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
6664
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
6765
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
6866
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
69-
7067
/* Experimental Options */
7168
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
7269
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
7370
},
74-
"exclude": ["example", "benchmark", "test/bun*", "test/deno*", "mod.ts"]
71+
"exclude": [
72+
"example",
73+
"benchmark",
74+
"test/bun*",
75+
"test/deno*",
76+
"mod.ts"
77+
]
7578
}

0 commit comments

Comments
 (0)