Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use xxhash-wasm for better compatibility with Windows #384

Merged
merged 1 commit into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/sixty-fishes-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"wrangler": patch
---

refactor: use xxhash-wasm for better compatibility with Windows

The previous xxhash package we were using required a build step, which relied upon tooling that was not always available on Window.

This version is a portable WASM package.
24 changes: 10 additions & 14 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"miniflare": "2.2.0",
"path-to-regexp": "^6.2.0",
"semiver": "^1.1.0",
"xxhash-addon": "^1.4.0"
"xxhash-wasm": "^1.0.1"
},
"optionalDependencies": {
"fsevents": "~2.3.2"
Expand Down
8 changes: 1 addition & 7 deletions packages/wrangler/scripts/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,7 @@ async function run() {
platform: "node",
format: "cjs",
// minify: true, // TODO: enable this again
external: [
"fsevents",
"esbuild",
"miniflare",
"@miniflare/core",
"xxhash-addon",
], // todo - bundle miniflare too
external: ["fsevents", "esbuild", "miniflare", "@miniflare/core"], // todo - bundle miniflare too
sourcemap: true,
inject: [path.join(__dirname, "../import_meta_url.js")],
define: {
Expand Down
22 changes: 13 additions & 9 deletions packages/wrangler/src/sites.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { readdir, readFile, stat } from "node:fs/promises";
import * as path from "node:path";
import ignore from "ignore";
import { XXHash64 } from "xxhash-addon";
import xxhash from "xxhash-wasm";
import {
createNamespace,
listNamespaceKeys,
listNamespaces,
putBulkKeyValue,
} from "./kv";
import type { Config } from "./config";
import type { XXHashAPI } from "xxhash-wasm";

/** Paths to always ignore. */
const ALWAYS_IGNORE = ["node_modules"];
Expand Down Expand Up @@ -46,11 +47,8 @@ async function* getFilesInFolder(dirPath: string): AsyncIterable<string> {
* the most important thing here is to detect changes of a single file to invalidate the cache and
* it's impossible to serve two different files with the same name
*/
function hashFileContent(content: string): string {
const hasher = new XXHash64();
hasher.update(Buffer.from(content));
const hash = hasher.digest();
return hash.toString("hex").substring(0, 10);
function hashFileContent(hasher: XXHashAPI, content: string): string {
return hasher.h64ToString(content).substring(0, 10);
}

/**
Expand All @@ -59,11 +57,15 @@ function hashFileContent(content: string): string {
* The key will change if the file path or content of the asset changes.
* The algorithm used here matches that of Wrangler 1.
*/
function hashAsset(filePath: string, content: string): string {
function hashAsset(
hasher: XXHashAPI,
filePath: string,
content: string
): string {
const extName = path.extname(filePath) || "";
const baseName = path.basename(filePath, extName);
const directory = path.dirname(filePath);
const hash = hashFileContent(content);
const hash = hashFileContent(hasher, content);
return urlSafe(path.join(directory, `${baseName}.${hash}${extName}`));
}

Expand Down Expand Up @@ -136,6 +138,8 @@ export async function syncAssets(

const include = createPatternMatcher(siteAssets.includePatterns, false);
const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
const hasher = await xxhash();

// TODO: this can be more efficient by parallelising
for await (const file of getFilesInFolder(siteAssets.baseDirectory)) {
if (!include(file)) {
Expand All @@ -149,7 +153,7 @@ export async function syncAssets(
console.log(`reading ${file}...`);
const content = await readFile(file, "base64");

const assetKey = hashAsset(file, content);
const assetKey = await hashAsset(hasher, file, content);
validateAssetKey(assetKey);

// now put each of the files into kv
Expand Down