Skip to content

Commit

Permalink
refactor: internalize shorthash (#3281)
Browse files Browse the repository at this point in the history
* Shorthash has been internalized

* Remove shorthash

* Optimized shorthash

* Changeset

* Added license
  • Loading branch information
JuanM04 committed May 3, 2022
1 parent 13c1f5f commit e2a037b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-years-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Internal: removed `shorthash`
1 change: 0 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions packages/astro/src/@types/shorthash.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const ALWAYS_EXTERNAL = new Set([
'node-fetch',
'prismjs',
'shiki',
'shorthash',
'unified',
'whatwg-url',
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/runtime/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import shorthash from 'shorthash';
import type {
AstroComponentMetadata,
AstroGlobalPartial,
Expand All @@ -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';
Expand Down Expand Up @@ -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(
`<!--${metadata.componentExport!.value}:${metadata.componentUrl}-->\n${html}\n${serializeProps(
props
)}`
Expand Down
67 changes: 67 additions & 0 deletions packages/astro/src/runtime/server/shorthash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* shortdash - https://github.com/bibig/node-shorthash
*
* @license
*
* (The MIT License)
*
* Copyright (c) 2013 Bibig <[email protected]>
*
* 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:https://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;
}
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

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

0 comments on commit e2a037b

Please sign in to comment.