refactor: internalize shorthash (#3281)

* Shorthash has been internalized

* Remove shorthash

* Optimized shorthash

* Changeset

* Added license
This commit is contained in:
Juan Martín Seery 2022-05-03 19:25:35 -03:00 committed by GitHub
parent 13c1f5ff6b
commit e2a037be94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 15 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Internal: removed `shorthash`

View file

@ -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",

View file

@ -1,5 +0,0 @@
declare module 'shorthash' {
function unique(string: string): string;
export default { unique };
}

View file

@ -24,7 +24,6 @@ const ALWAYS_EXTERNAL = new Set([
'node-fetch',
'prismjs',
'shiki',
'shorthash',
'unified',
'whatwg-url',
]);

View file

@ -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(
`<!--${metadata.componentExport!.value}:${metadata.componentUrl}-->\n${html}\n${serializeProps(
props
)}`

View file

@ -0,0 +1,67 @@
/**
* shortdash - https://github.com/bibig/node-shorthash
*
* @license
*
* (The MIT License)
*
* Copyright (c) 2013 Bibig <bibig@me.com>
*
* 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;
}

View file

@ -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: