Skip to content

Commit

Permalink
chore: add hashes, shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Dec 15, 2024
1 parent f5cdf80 commit a52b2e6
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 14 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@
"@types/emscripten": "^1.39.13",
"type-fest": "^4.30.1"
},
"overrides": {
"typedoc": {
"typescript": "$typescript"
"pnpm": {
"overrides": {
"[email protected]>vite": "$vite",
"@vitest/[email protected]>vite": "$vite"
}
},
"packageManager": "[email protected]+sha512.76e2379760a4328ec4415815bcd6628dee727af3779aaa4c914e3944156c4299921a89f976381ee107d41f12cfa4b66681ca9c718f0668fa0831ed4c6d8ba56c"
Expand Down
23 changes: 15 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ export interface PrepareZXingModuleOptions {
fireImmediately?: boolean;
}

export function shallow(a: ZXingModuleOverrides, b: ZXingModuleOverrides) {
return (
Object.is(a, b) ||
(Object.keys(a).length === Object.keys(b).length &&
Object.keys(a).every(
(key) =>
Object.prototype.hasOwnProperty.call(b, key) &&
a[key as keyof ZXingModuleOverrides] ===
b[key as keyof ZXingModuleOverrides],
))
);
}

export function prepareZXingModuleWithFactory<T extends ZXingModuleType>(
zxingModuleFactory: ZXingModuleFactory<T>,
options?: Merge<PrepareZXingModuleOptions, { fireImmediately?: false }>,
Expand All @@ -135,7 +148,7 @@ export function prepareZXingModuleWithFactory<T extends ZXingModuleType>(
zxingModuleFactory: ZXingModuleFactory<T>,
{
overrides,
equalityFn = Object.is,
equalityFn = shallow,
fireImmediately = false,
}: PrepareZXingModuleOptions = {},
) {
Expand Down
1 change: 1 addition & 0 deletions src/full/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,4 @@ export {
type ZXingFullModule,
type ZXingModuleOverrides,
} from "../core.js";
export const ZXING_WASM_SHA256 = FULL_HASH;
3 changes: 3 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/// <reference types="vite/client" />
declare const NPM_PACKAGE_VERSION: string;
declare const READER_HASH: string;
declare const WRITER_HASH: string;
declare const FULL_HASH: string;
1 change: 1 addition & 0 deletions src/reader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ export {
type ZXingReaderModule,
type ZXingModuleOverrides,
} from "../core.js";
export const ZXING_WASM_SHA256 = READER_HASH;
1 change: 1 addition & 0 deletions src/writer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,4 @@ export {
type ZXingWriterModule,
type ZXingModuleOverrides,
} from "../core.js";
export const ZXING_WASM_SHA256 = WRITER_HASH;
42 changes: 40 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/// <reference types="vitest" />
/// <reference types="vitest/config" />
/// <reference types="vite/client" />

import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import babel from "vite-plugin-babel";
import { version } from "./package.json";
Expand All @@ -19,7 +24,7 @@ export default defineConfig({
outDir: "dist/es",
rollupOptions: {
output: {
chunkFileNames: "[name]-[hash].js",
chunkFileNames: "[name].js",
manualChunks: (id) => {
if (
/core\.ts|exposedReaderBindings\.ts|exposedWriterBindings\.ts/.test(
Expand All @@ -44,6 +49,39 @@ export default defineConfig({
define: {
NPM_PACKAGE_VERSION: JSON.stringify(version),
"import.meta.vitest": "undefined",
READER_HASH: JSON.stringify(
createHash("sha256")
.update(
await readFile(
fileURLToPath(
new URL("./src/reader/zxing_reader.wasm", import.meta.url),
),
),
)
.digest("hex"),
),
WRITER_HASH: JSON.stringify(
createHash("sha256")
.update(
await readFile(
fileURLToPath(
new URL("./src/writer/zxing_writer.wasm", import.meta.url),
),
),
)
.digest("hex"),
),
FULL_HASH: JSON.stringify(
createHash("sha256")
.update(
await readFile(
fileURLToPath(
new URL("./src/full/zxing_full.wasm", import.meta.url),
),
),
)
.digest("hex"),
),
},
test: {
testTimeout: 10000,
Expand Down

0 comments on commit a52b2e6

Please sign in to comment.