Home / File/ hash.js — svelte Source File

hash.js — svelte Source File

Architecture documentation for hash.js, a javascript file in the svelte codebase. 1 imports, 0 dependents.

File javascript Compiler Parser 1 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  701d9da6_675f_b6d1_ad30_b067ef212b63["hash.js"]
  f596e027_a951_36c9_7695_83acc4f0d6b9["node:fs"]
  701d9da6_675f_b6d1_ad30_b067ef212b63 --> f596e027_a951_36c9_7695_83acc4f0d6b9
  style 701d9da6_675f_b6d1_ad30_b067ef212b63 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import fs from 'node:fs';

/**
 * Detects if a file is text or binary by checking for null bytes
 * and validating UTF-8 encoding
 * @param {string} filepath - Path to the file
 * @returns {boolean} - true if file is text, false if binary
 */
function is_text_file(filepath) {
	const buffer = fs.readFileSync(filepath);
	// Check for null bytes which indicate binary files
	for (let i = 0; i < buffer.length; i++) {
		if (buffer[i] === 0) {
			return false;
		}
	}
	// Validate UTF-8 encoding
	try {
		const text = buffer.toString('utf-8');
		// Verify round-trip encoding to ensure valid UTF-8
		const encoded = Buffer.from(text, 'utf-8');
		return buffer.equals(encoded);
	} catch {
		return false;
	}
}

const files = [];

for (const basename of fs.readdirSync('src')) {
	if (fs.statSync(`src/${basename}`).isDirectory()) continue;

	const filepath = `src/${basename}`;
	const text = is_text_file(filepath);

	files.push({
		type: 'file',
		name: basename,
		basename,
		contents: fs.readFileSync(filepath, text ? 'utf-8' : 'base64'),
		text
	});
}

const payload = JSON.stringify({
	name: 'sandbox',
	files
});

async function compress(payload) {
	const reader = new Blob([payload])
		.stream()
		.pipeThrough(new CompressionStream('gzip'))
		.getReader();

	let buffer = '';
	for (;;) {
		const { done, value } = await reader.read();

		if (done) {
			reader.releaseLock();
			return btoa(buffer).replaceAll('+', '-').replaceAll('/', '_');
		} else {
			for (let i = 0; i < value.length; i++) {
				// decoding as utf-8 will make btoa reject the string
				buffer += String.fromCharCode(value[i]);
			}
		}
	}
}

const hash = await compress(payload);
console.log(`https://svelte.dev/playground/untitled#${hash}`);

Domain

Subdomains

Dependencies

  • node:fs

Frequently Asked Questions

What does hash.js do?
hash.js is a source file in the svelte codebase, written in javascript. It belongs to the Compiler domain, Parser subdomain.
What functions are defined in hash.js?
hash.js defines 2 function(s): compress, is_text_file.
What does hash.js depend on?
hash.js imports 1 module(s): node:fs.
Where is hash.js in the architecture?
hash.js is located at playgrounds/sandbox/scripts/hash.js (domain: Compiler, subdomain: Parser, directory: playgrounds/sandbox/scripts).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free