Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function init() {
// Our require setup is still alive in the DOM, so we can get at Filer
fs = new Filer.FileSystem({
provider: new Filer.FileSystem.providers.Fallback()
});
rewireScript();
rewireImage();
rewireXHR();
startWatchers();
cleanEnv();
}
var nohost = (function(window) {
// The server's filesystem
var Filer = require('filer');
var Path = Filer.Path;
var fs;
// List of FSWatchers for files in the filesystem
var watchers = {};
// Content helpers from the nohost source
var Content = require('content');
// Path and Dir for current nohost url
var nohostPath = document.location.search.substring(1);
var nohostDir = Path.dirname(nohostPath);
// Change a url to a path in the filesystem to a Blob URL
function rewriteURL(url, encoding, callback) {
if(!url || /\:?\/\//.test(url) || /\s*data\:/.test(url)) {
return callback(null, url);
* algorithms necessary for rsync and
* checksum comparison algorithms to check
* the equivalency of two file systems
* as well as general validation functions
*
* Portions used from Node.js Anchor module
* Copyright(c) 2011 Mihai Tomescu
* Copyright(c) 2011 Tolga Tezel
* MIT Licensed
* https://github.com/ttezel/anchor
*/
var MD5 = require('MD5');
var Filer = require('filer');
var Errors = Filer.Errors;
var Path = Filer.Path;
var async = require('../async-lite');
var fsUtils = require('../fs-utils');
// Rsync Options that can be passed are:
// size - the size of each chunk of data in bytes that should be checksumed
// checksum - true: always calculate checksums [default]
// false: ignore checksums for identical files
// recursive - true: sync each contained node in the path provided
// false: only sync the node for the path provided [default]
// time - true: sync modified times of source/destination files
// false: do not change modified times of destination files [default]
// links - true: sync symbolic links as links in destination
// false: sync symbolic links as the files they link to in destination [default]
// versions - true: do not sync a node if the last synced version matches the version it needs to be synced to [default]
// false: sync nodes irrespective of the last synced version
// superficial- true: if a directory path is provided, only sync the directory and not it's contents
function maybeServeIndexFile() {
const indexPath = Path.join(path, directoryIndex);
fs.stat(indexPath, function(err, stats) {
if(err) {
if(err.code === 'ENOENT' && !disableIndexes) {
// Fallback to a directory listing instead
serveDirListing();
} else {
// Let the error (likely 404) pass through instead
serveError(path, err);
}
} else {
// Index file found, serve that instead
serveFile(indexPath, stats);
}
});
}
function serveFile(path, stats) {
fs.readFile(path, function(err, contents) {
if(err) {
return serveError(path, err);
}
const responseData = formatter.formatFile(path, contents, stats);
// If we are supposed to serve this file or download, add headers
if(responseData.config.status === 200 && download) {
responseData.config.headers['Content-Disposition'] =
formatContentDisposition(path, stats);
}
resolve(new Response(responseData.body, responseData.config));
});
}
function maybeServeIndexFile() {
const indexPath = Path.join(path, directoryIndex);
fs.stat(indexPath, function(err, stats) {
if(err) {
if(err.code === 'ENOENT' && !disableIndexes) {
// Fallback to a directory listing instead
serveDirListing();
} else {
// Let the error (likely 404) pass through instead
serveError(path, err);
}
} else {
// Index file found, serve that instead
serveFile(indexPath, stats);
}
});
}
* Rsync utilities that include hashing
* algorithms necessary for rsync and
* checksum comparison algorithms to check
* the equivalency of two file systems
* as well as general validation functions
*
* Portions used from Node.js Anchor module
* Copyright(c) 2011 Mihai Tomescu
* Copyright(c) 2011 Tolga Tezel
* MIT Licensed
* https://github.com/ttezel/anchor
*/
var MD5 = require('MD5');
var Filer = require('filer');
var Errors = Filer.Errors;
var Path = Filer.Path;
var async = require('../async-lite');
var fsUtils = require('../fs-utils');
// Rsync Options that can be passed are:
// size - the size of each chunk of data in bytes that should be checksumed
// checksum - true: always calculate checksums [default]
// false: ignore checksums for identical files
// recursive - true: sync each contained node in the path provided
// false: only sync the node for the path provided [default]
// time - true: sync modified times of source/destination files
// false: do not change modified times of destination files [default]
// links - true: sync symbolic links as links in destination
// false: sync symbolic links as the files they link to in destination [default]
// versions - true: do not sync a node if the last synced version matches the version it needs to be synced to [default]
// false: sync nodes irrespective of the last synced version
'use strict';
const { fs, Path } = require('filer');
const { route, disableIndexes, directoryIndex } = require('./config');
const sh = new fs.Shell();
// https://tools.ietf.org/html/rfc2183
function formatContentDisposition(path, stats) {
const filename = Path.basename(path);
const modified = stats.mtime.toUTCString();
return `attachment; filename="${filename}"; modification-date="${modified}"; size=${stats.size};`;
}
const serve = function(path, formatter, download) {
return new Promise((resolve) => {
function buildResponse(responseData) {
return new Response(responseData.body, responseData.config);
}
function serveError(path, err) {
if(err.code === 'ENOENT') {
function formatDir(route, dirPath, entries) {
const parent = path.dirname(dirPath) || '/';
// Maintain path sep, but deal with things like spaces in filenames
const url = encodeURI(route + parent);
const header = `
<title>Index of ${dirPath}</title>
<h1>Index of ${dirPath}</h1>
`;
const footer = `<table><tbody><tr><th><img alt="[ICO]" src="${blank}"></th>
<th><b>Name</b></th><th><b>Last modified</b></th>
<th><b>Size</b></th><th><b>Description</b></th></tr>
<tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img alt="[DIR]" src="${back}"></td>
<td><a href="${url}">Parent Directory</a></td><td> </td>
<td align="right"> - </td><td> </td></tr><tr><th colspan="5"><hr></th></tr></tbody></table>${footerClose}`;
const rows = entries.map(entry => {
const rows = entries.map(entry => {
const ext = path.extname(entry.name);
// Maintain path sep, but deal with things like spaces in filenames
const href = encodeURI(`${route}${path.join(dirPath, entry.name)}`);
let icon;
let alt;
// TODO: switch this to entry.isDirectory() if possible
if (entry.type === 'DIRECTORY') {
icon = folder;
alt = '[DIR]';
} else {
if (isImage(ext)) {
icon = image2;
alt = '[IMG]';
} else if (isMedia(ext)) {
icon = movie;
alt = '[MOV]';