diff --git a/.changeset/big-years-type.md b/.changeset/big-years-type.md new file mode 100644 index 000000000000..bb68c4c235fa --- /dev/null +++ b/.changeset/big-years-type.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Internal: removed `shorthash` diff --git a/packages/astro/package.json b/packages/astro/package.json index c53ecfee25e7..6ee6b2ee89b0 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -122,7 +122,6 @@ "rollup": "^2.70.2", "semver": "^7.3.7", "shiki": "^0.10.1", - "shorthash": "^0.0.2", "sirv": "^2.0.2", "slash": "^4.0.0", "sourcemap-codec": "^1.4.8", diff --git a/packages/astro/src/@types/shorthash.d.ts b/packages/astro/src/@types/shorthash.d.ts deleted file mode 100644 index 8dc53e6efc9c..000000000000 --- a/packages/astro/src/@types/shorthash.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -declare module 'shorthash' { - function unique(string: string): string; - - export default { unique }; -} diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index acb97c5f486a..01a8e486bb6e 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -24,7 +24,6 @@ const ALWAYS_EXTERNAL = new Set([ 'node-fetch', 'prismjs', 'shiki', - 'shorthash', 'unified', 'whatwg-url', ]); diff --git a/packages/astro/src/runtime/server/index.ts b/packages/astro/src/runtime/server/index.ts index f888a48526fa..c9df2e62b089 100644 --- a/packages/astro/src/runtime/server/index.ts +++ b/packages/astro/src/runtime/server/index.ts @@ -1,4 +1,3 @@ -import shorthash from 'shorthash'; import type { AstroComponentMetadata, AstroGlobalPartial, @@ -11,6 +10,7 @@ import type { import { escapeHTML, HTMLString, markHTMLString } from './escape.js'; import { extractDirectives, generateHydrateScript, serializeProps } from './hydration.js'; import { serializeListValue } from './util.js'; +import { shorthash } from './shorthash.js'; export { markHTMLString, markHTMLString as unescapeHTML } from './escape.js'; export type { Metadata } from './metadata'; @@ -300,7 +300,7 @@ If you're still stuck, please open an issue on GitHub or join us at https://astr } // Include componentExport name, componentUrl, and props in hash to dedupe identical islands - const astroId = shorthash.unique( + const astroId = shorthash( `\n${html}\n${serializeProps( props )}` diff --git a/packages/astro/src/runtime/server/shorthash.ts b/packages/astro/src/runtime/server/shorthash.ts new file mode 100644 index 000000000000..99a691ac4e71 --- /dev/null +++ b/packages/astro/src/runtime/server/shorthash.ts @@ -0,0 +1,67 @@ +/** + * shortdash - https://github.com/bibig/node-shorthash + * + * @license + * + * (The MIT License) + * + * Copyright (c) 2013 Bibig + * + * Permission is hereby granted, free of charge, to any person + * obtaining a copy of this software and associated documentation + * files (the "Software"), to deal in the Software without + * restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following + * conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + */ + +const dictionary = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY'; +const binary = dictionary.length; + +// refer to: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ +function bitwise(str: string) { + let hash = 0; + if (str.length === 0) return hash; + for (let i = 0; i < str.length; i++) { + const ch = str.charCodeAt(i); + hash = (hash << 5) - hash + ch; + hash = hash & hash; // Convert to 32bit integer + } + return hash; +} + +export function shorthash(text: string) { + let num: number; + let result = ''; + + let integer = bitwise(text); + const sign = integer < 0 ? 'Z' : ''; // It it's negative, start with Z, which isn't in the dictionary + + integer = Math.abs(integer); + + while (integer >= binary) { + num = integer % binary; + integer = Math.floor(integer / binary); + result = dictionary[num] + result; + } + + if (integer > 0) { + result = dictionary[integer] + result; + } + + return sign + result; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 18f908f2c83f..92c0483befcf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -530,7 +530,6 @@ importers: sass: ^1.50.1 semver: ^7.3.7 shiki: ^0.10.1 - shorthash: ^0.0.2 sirv: ^2.0.2 slash: ^4.0.0 sourcemap-codec: ^1.4.8 @@ -590,7 +589,6 @@ importers: rollup: 2.70.2 semver: 7.3.7 shiki: 0.10.1 - shorthash: 0.0.2 sirv: 2.0.2 slash: 4.0.0 sourcemap-codec: 1.4.8 @@ -9271,10 +9269,6 @@ packages: vscode-textmate: 5.2.0 dev: false - /shorthash/0.0.2: - resolution: {integrity: sha1-WbJo7sveWQOLMNogK8+93rLEpOs=} - dev: false - /side-channel/1.0.4: resolution: {integrity: sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==} dependencies: