url#URL JavaScript Examples
The following examples show how to use
url#URL.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: index.js From clipcc-desktop with GNU Affero General Public License v3.0 | 6 votes |
makeFullUrl = (url, search = null) => {
const baseUrl = (isDevelopment ?
`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}/` :
`file://${__dirname}/`
);
const fullUrl = new URL(url, baseUrl);
if (search) {
fullUrl.search = search; // automatically percent-encodes anything that needs it
}
return fullUrl.toString();
}
Example #2
Source File: index.js From tinyimg-webpack-plugin with MIT License | 6 votes |
downloadImg(url) {
const opts = new URL(url);
return new Promise((resolve, reject) => {
const req = request(opts, res => {
let file = "";
res.setEncoding("binary");
res.on("data", chunk => file += chunk);
res.on("end", () => resolve(file));
});
req.on("error", e => reject(e));
req.end();
});
}
Example #3
Source File: generate-helpers.js From cs-wiki with GNU General Public License v3.0 | 5 votes |
HELPERS_FOLDER = new URL("../src/helpers", import.meta.url)
Example #4
Source File: http.js From mobtime with MIT License | 5 votes |
HttpSub = (dispatch, action, host = 'localhost', port = 4321) => {
const app = express();
const server = http.createServer(app);
const wss = new ws.Server({
server,
clientTracking: false,
});
const log = (...data) => console.log('[Http]', ...data);
app.use(bodyParser.json());
app.use(
helmet({
contentSecurityPolicy: false,
crossOriginOpenerPolicy: 'unsafe-none',
crossOriginEmbedderPolicy: false,
frameguard: false,
crossOriginResourcePolicy: { policy: 'cross-origin' },
}),
);
const tryMiddleware = (_request, response, next) => {
try {
return next();
} catch (err) {
console.log('error');
console.log(err);
return response
.status(500)
.json({ message: err.toString() })
.end();
}
};
app.use('/api', tryMiddleware, apiStatistics());
app.use('/console', apiConsole());
const rootPath = path.resolve(__dirname, '..');
app.use(express.static(path.resolve(rootPath, 'public')));
app.get('/:timerId', async (request, response) => {
const htmlPayload = path.resolve(rootPath, 'public', 'timer.html');
const html = await fs.readFile(htmlPayload, { encoding: 'utf8' });
log('http.connect', request.params.timerId);
return response.status(200).send(html);
});
server.listen(port, host, () => {
console.log(`Local server up: http://${host}:${port}`);
});
wss.on('connection', async (client, request) => {
const url = new URL(request.url, `http://${request.headers.host}`);
const timerId = url.pathname.replace('/', '');
log('websocket.connect', timerId);
client.on('close', () => {
log('websocket.disconnect', timerId);
});
await dispatch(action.AddConnection(client, timerId), 'AddConnection');
});
return () => {
wss.close();
http.close();
};
}
Example #5
Source File: generate-helpers.js From coding-life with MIT License | 5 votes |
HELPERS_FOLDER = new URL("../src/helpers", import.meta.url)
Example #6
Source File: prerender.js From kit with MIT License | 4 votes |
/**
* @param {{
* config: import('types').ValidatedKitConfig;
* entries: string[];
* files: Set<string>;
* log: Logger;
* }} opts
*/
export async function prerender({ config, entries, files, log }) {
/** @type {import('types').Prerendered} */
const prerendered = {
pages: new Map(),
assets: new Map(),
redirects: new Map(),
paths: []
};
if (!config.prerender.enabled) {
return prerendered;
}
installPolyfills();
const server_root = join(config.outDir, 'output');
/** @type {import('types').ServerModule} */
const { Server, override } = await import(pathToFileURL(`${server_root}/server/index.js`).href);
const { manifest } = await import(pathToFileURL(`${server_root}/server/manifest.js`).href);
override({
paths: config.paths,
prerendering: true,
read: (file) => readFileSync(join(config.files.assets, file))
});
const server = new Server(manifest);
const error = normalise_error_handler(log, config.prerender.onError);
const q = queue(config.prerender.concurrency);
/**
* @param {string} path
* @param {boolean} is_html
*/
function output_filename(path, is_html) {
const file = path.slice(config.paths.base.length + 1);
if (file === '') {
return 'index.html';
}
if (is_html && !file.endsWith('.html')) {
return file + (file.endsWith('/') ? 'index.html' : '.html');
}
return file;
}
const seen = new Set();
const written = new Set();
/**
* @param {string | null} referrer
* @param {string} decoded
* @param {string} [encoded]
*/
function enqueue(referrer, decoded, encoded) {
if (seen.has(decoded)) return;
seen.add(decoded);
const file = decoded.slice(config.paths.base.length + 1);
if (files.has(file)) return;
return q.add(() => visit(decoded, encoded || encodeURI(decoded), referrer));
}
/**
* @param {string} decoded
* @param {string} encoded
* @param {string?} referrer
*/
async function visit(decoded, encoded, referrer) {
if (!decoded.startsWith(config.paths.base)) {
error({ status: 404, path: decoded, referrer, referenceType: 'linked' });
return;
}
/** @type {Map<string, import('types').PrerenderDependency>} */
const dependencies = new Map();
const response = await server.respond(new Request(`http://sveltekit-prerender${encoded}`), {
getClientAddress,
prerendering: {
dependencies
}
});
const text = await response.text();
save('pages', response, text, decoded, encoded, referrer, 'linked');
for (const [dependency_path, result] of dependencies) {
// this seems circuitous, but using new URL allows us to not care
// whether dependency_path is encoded or not
const encoded_dependency_path = new URL(dependency_path, 'http://localhost').pathname;
const decoded_dependency_path = decodeURI(encoded_dependency_path);
const body = result.body ?? new Uint8Array(await result.response.arrayBuffer());
save(
'dependencies',
result.response,
body,
decoded_dependency_path,
encoded_dependency_path,
decoded,
'fetched'
);
}
if (config.prerender.crawl && response.headers.get('content-type') === 'text/html') {
for (const href of crawl(text)) {
if (href.startsWith('data:') || href.startsWith('#')) continue;
const resolved = resolve(encoded, href);
if (!is_root_relative(resolved)) continue;
const { pathname, search } = new URL(resolved, 'http://localhost');
if (search) {
// TODO warn that query strings have no effect on statically-exported pages
}
enqueue(decoded, decodeURI(pathname), pathname);
}
}
}
/**
* @param {'pages' | 'dependencies'} category
* @param {Response} response
* @param {string | Uint8Array} body
* @param {string} decoded
* @param {string} encoded
* @param {string | null} referrer
* @param {'linked' | 'fetched'} referenceType
*/
function save(category, response, body, decoded, encoded, referrer, referenceType) {
const response_type = Math.floor(response.status / 100);
const type = /** @type {string} */ (response.headers.get('content-type'));
const is_html = response_type === REDIRECT || type === 'text/html';
const file = output_filename(decoded, is_html);
const dest = `${config.outDir}/output/prerendered/${category}/${file}`;
if (written.has(file)) return;
if (response_type === REDIRECT) {
const location = response.headers.get('location');
if (location) {
const resolved = resolve(encoded, location);
if (is_root_relative(resolved)) {
enqueue(decoded, decodeURI(resolved), resolved);
}
if (!response.headers.get('x-sveltekit-normalize')) {
mkdirp(dirname(dest));
log.warn(`${response.status} ${decoded} -> ${location}`);
writeFileSync(
dest,
`<meta http-equiv="refresh" content=${escape_html_attr(`0;url=${location}`)}>`
);
written.add(file);
if (!prerendered.redirects.has(decoded)) {
prerendered.redirects.set(decoded, {
status: response.status,
location: resolved
});
prerendered.paths.push(normalize_path(decoded, 'never'));
}
}
} else {
log.warn(`location header missing on redirect received from ${decoded}`);
}
return;
}
if (response.status === 200) {
mkdirp(dirname(dest));
log.info(`${response.status} ${decoded}`);
writeFileSync(dest, body);
written.add(file);
if (is_html) {
prerendered.pages.set(decoded, {
file
});
} else {
prerendered.assets.set(decoded, {
type
});
}
prerendered.paths.push(normalize_path(decoded, 'never'));
} else if (response_type !== OK) {
error({ status: response.status, path: decoded, referrer, referenceType });
}
}
if (config.prerender.enabled) {
for (const entry of config.prerender.entries) {
if (entry === '*') {
for (const entry of entries) {
enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
}
} else {
enqueue(null, config.paths.base + entry);
}
}
await q.done();
}
const rendered = await server.respond(new Request('http://sveltekit-prerender/[fallback]'), {
getClientAddress,
prerendering: {
fallback: true,
dependencies: new Map()
}
});
const file = `${config.outDir}/output/prerendered/fallback.html`;
mkdirp(dirname(file));
writeFileSync(file, await rendered.text());
return prerendered;
}
Example #7
Source File: index.js From kit with MIT License | 4 votes |
/**
* @param {import('vite').ViteDevServer} vite
* @param {import('vite').ResolvedConfig} vite_config
* @param {import('types').ValidatedConfig} svelte_config
* @return {Promise<Promise<() => void>>}
*/
export async function dev(vite, vite_config, svelte_config) {
installPolyfills();
sync.init(svelte_config);
const runtime = get_runtime_path(svelte_config.kit);
process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = '0';
/** @type {import('types').Respond} */
const respond = (await import(`${runtime}/server/index.js`)).respond;
/** @type {import('types').SSRManifest} */
let manifest;
function update_manifest() {
const { manifest_data } = sync.update(svelte_config);
manifest = {
appDir: svelte_config.kit.appDir,
assets: new Set(manifest_data.assets.map((asset) => asset.file)),
mimeTypes: get_mime_lookup(manifest_data),
_: {
entry: {
file: `/@fs${runtime}/client/start.js`,
imports: [],
stylesheets: []
},
nodes: manifest_data.components.map((id, index) => {
return async () => {
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;
const module = /** @type {import('types').SSRComponent} */ (
await vite.ssrLoadModule(url, { fixStacktrace: false })
);
return {
module,
index,
file: url.endsWith('.svelte') ? url : url + '?import',
imports: [],
stylesheets: [],
// in dev we inline all styles to avoid FOUC
inline_styles: async () => {
const node = await vite.moduleGraph.getModuleByUrl(url);
if (!node) throw new Error(`Could not find node for ${url}`);
const deps = new Set();
await find_deps(vite, node, deps);
/** @type {Record<string, string>} */
const styles = {};
for (const dep of deps) {
const parsed = new URL(dep.url, 'http://localhost/');
const query = parsed.searchParams;
if (
style_pattern.test(dep.file) ||
(query.has('svelte') && query.get('type') === 'style')
) {
try {
const mod = await vite.ssrLoadModule(dep.url, { fixStacktrace: false });
styles[dep.url] = mod.default;
} catch {
// this can happen with dynamically imported modules, I think
// because the Vite module graph doesn't distinguish between
// static and dynamic imports? TODO investigate, submit fix
}
}
}
return styles;
}
};
};
}),
routes: manifest_data.routes.map((route) => {
const { pattern, names, types } = parse_route_id(route.id);
if (route.type === 'page') {
return {
type: 'page',
id: route.id,
pattern,
names,
types,
shadow: route.shadow
? async () => {
const url = path.resolve(cwd, /** @type {string} */ (route.shadow));
return await vite.ssrLoadModule(url, { fixStacktrace: false });
}
: null,
a: route.a.map((id) => (id ? manifest_data.components.indexOf(id) : undefined)),
b: route.b.map((id) => (id ? manifest_data.components.indexOf(id) : undefined))
};
}
return {
type: 'endpoint',
id: route.id,
pattern,
names,
types,
load: async () => {
const url = path.resolve(cwd, route.file);
return await vite.ssrLoadModule(url, { fixStacktrace: false });
}
};
}),
matchers: async () => {
/** @type {Record<string, import('types').ParamMatcher>} */
const matchers = {};
for (const key in manifest_data.matchers) {
const file = manifest_data.matchers[key];
const url = path.resolve(cwd, file);
const module = await vite.ssrLoadModule(url, { fixStacktrace: false });
if (module.match) {
matchers[key] = module.match;
} else {
throw new Error(`${file} does not export a \`match\` function`);
}
}
return matchers;
}
}
};
}
/** @param {Error} error */
function fix_stack_trace(error) {
return error.stack ? vite.ssrRewriteStacktrace(error.stack) : error.stack;
}
update_manifest();
for (const event of ['add', 'unlink']) {
vite.watcher.on(event, (file) => {
if (file.startsWith(svelte_config.kit.files.routes + path.sep)) {
update_manifest();
}
});
}
const assets = svelte_config.kit.paths.assets ? SVELTE_KIT_ASSETS : svelte_config.kit.paths.base;
const asset_server = sirv(svelte_config.kit.files.assets, {
dev: true,
etag: true,
maxAge: 0,
extensions: []
});
return () => {
const serve_static_middleware = vite.middlewares.stack.find(
(middleware) =>
/** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
);
remove_html_middlewares(vite.middlewares);
vite.middlewares.use(async (req, res) => {
try {
if (!req.url || !req.method) throw new Error('Incomplete request');
const base = `${vite.config.server.https ? 'https' : 'http'}://${
req.headers[':authority'] || req.headers.host
}`;
const decoded = decodeURI(new URL(base + req.url).pathname);
if (decoded.startsWith(assets)) {
const pathname = decoded.slice(assets.length);
const file = svelte_config.kit.files.assets + pathname;
if (fs.existsSync(file) && !fs.statSync(file).isDirectory()) {
if (has_correct_case(file, svelte_config.kit.files.assets)) {
req.url = encodeURI(pathname); // don't need query/hash
asset_server(req, res);
return;
}
}
}
const file = posixify(path.resolve(decoded.slice(1)));
const is_file = fs.existsSync(file) && !fs.statSync(file).isDirectory();
const allowed =
!vite_config.server.fs.strict ||
vite_config.server.fs.allow.some((dir) => file.startsWith(dir));
if (is_file && allowed) {
// @ts-expect-error
serve_static_middleware.handle(req, res);
return;
}
if (!decoded.startsWith(svelte_config.kit.paths.base)) {
return not_found(
res,
`Not found (did you mean ${svelte_config.kit.paths.base + req.url}?)`
);
}
/** @type {Partial<import('types').Hooks>} */
const user_hooks = resolve_entry(svelte_config.kit.files.hooks)
? await vite.ssrLoadModule(`/${svelte_config.kit.files.hooks}`, {
fixStacktrace: false
})
: {};
const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));
/** @type {import('types').Hooks} */
const hooks = {
getSession: user_hooks.getSession || (() => ({})),
handle,
handleError:
user_hooks.handleError ||
(({ /** @type {Error & { frame?: string }} */ error }) => {
console.error(colors.bold().red(error.message));
if (error.frame) {
console.error(colors.gray(error.frame));
}
if (error.stack) {
console.error(colors.gray(error.stack));
}
}),
externalFetch: user_hooks.externalFetch || fetch
};
if (/** @type {any} */ (hooks).getContext) {
// TODO remove this for 1.0
throw new Error(
'The getContext hook has been removed. See https://kit.svelte.dev/docs/hooks'
);
}
if (/** @type {any} */ (hooks).serverFetch) {
// TODO remove this for 1.0
throw new Error('The serverFetch hook has been renamed to externalFetch.');
}
// TODO the / prefix will probably fail if outDir is outside the cwd (which
// could be the case in a monorepo setup), but without it these modules
// can get loaded twice via different URLs, which causes failures. Might
// require changes to Vite to fix
const { default: root } = await vite.ssrLoadModule(
`/${posixify(path.relative(cwd, `${svelte_config.kit.outDir}/generated/root.svelte`))}`,
{ fixStacktrace: false }
);
const paths = await vite.ssrLoadModule(
process.env.BUNDLED
? `/${posixify(path.relative(cwd, `${svelte_config.kit.outDir}/runtime/paths.js`))}`
: `/@fs${runtime}/paths.js`,
{ fixStacktrace: false }
);
paths.set_paths({
base: svelte_config.kit.paths.base,
assets
});
let request;
try {
request = await getRequest(base, req);
} catch (/** @type {any} */ err) {
res.statusCode = err.status || 400;
return res.end(err.reason || 'Invalid request body');
}
const template = load_template(cwd, svelte_config);
const rendered = await respond(
request,
{
csp: svelte_config.kit.csp,
dev: true,
get_stack: (error) => {
return fix_stack_trace(error);
},
handle_error: (error, event) => {
hooks.handleError({
error: new Proxy(error, {
get: (target, property) => {
if (property === 'stack') {
return fix_stack_trace(error);
}
return Reflect.get(target, property, target);
}
}),
event,
// TODO remove for 1.0
// @ts-expect-error
get request() {
throw new Error(
'request in handleError has been replaced with event. See https://github.com/sveltejs/kit/pull/3384 for details'
);
}
});
},
hooks,
hydrate: svelte_config.kit.browser.hydrate,
manifest,
method_override: svelte_config.kit.methodOverride,
paths: {
base: svelte_config.kit.paths.base,
assets
},
prefix: '',
prerender: {
default: svelte_config.kit.prerender.default,
enabled: svelte_config.kit.prerender.enabled
},
read: (file) => fs.readFileSync(path.join(svelte_config.kit.files.assets, file)),
root,
router: svelte_config.kit.browser.router,
template: ({ head, body, assets, nonce }) => {
return (
template
.replace(/%sveltekit\.assets%/g, assets)
.replace(/%sveltekit\.nonce%/g, nonce)
// head and body must be replaced last, in case someone tries to sneak in %sveltekit.assets% etc
.replace('%sveltekit.head%', () => head)
.replace('%sveltekit.body%', () => body)
);
},
template_contains_nonce: template.includes('%sveltekit.nonce%'),
trailing_slash: svelte_config.kit.trailingSlash
},
{
getClientAddress: () => {
const { remoteAddress } = req.socket;
if (remoteAddress) return remoteAddress;
throw new Error('Could not determine clientAddress');
}
}
);
if (rendered.status === 404) {
// @ts-expect-error
serve_static_middleware.handle(req, res, () => {
setResponse(res, rendered);
});
} else {
setResponse(res, rendered);
}
} catch (e) {
const error = coalesce_to_error(e);
vite.ssrFixStacktrace(error);
res.statusCode = 500;
res.end(error.stack);
}
});
};
}
Example #8
Source File: httpURLConnectionClient.js From Lynx with MIT License | 4 votes |
HttpURLConnectionClient = /** @class */ (function () {
function HttpURLConnectionClient() {
}
HttpURLConnectionClient.prototype.request = function (endpoint, json, config, isApiRequired, requestOptions) {
requestOptions.headers = {};
requestOptions.timeout = config.connectionTimeoutMillis;
if (config.certificatePath) {
this.installCertificateVerifier(config.certificatePath);
}
var apiKey = config.apiKey;
if (isApiRequired && !apiKey) {
return Promise.reject(new ApiException("Invalid X-API-Key was used", 401));
}
if (apiKey) {
requestOptions.headers[API_KEY] = apiKey;
}
else {
var authString = config.username + ":" + config.password;
var authStringEnc = new Buffer(authString).toString("base64");
requestOptions.headers.Authorization = "Basic " + authStringEnc;
}
requestOptions.headers[CONTENT_TYPE] = APPLICATION_JSON_TYPE;
var httpConnection = this.createRequest(endpoint, requestOptions, config.applicationName);
return this.doPostRequest(httpConnection, json);
};
HttpURLConnectionClient.prototype.post = function (endpoint, postParameters, config) {
var postQuery = this.getQuery(postParameters);
var connectionRequest = this.createRequest(endpoint, {}, config.applicationName);
return this.doPostRequest(connectionRequest, postQuery);
};
HttpURLConnectionClient.prototype.createRequest = function (endpoint, requestOptions, applicationName) {
if (!requestOptions.headers) {
requestOptions.headers = {};
}
var url = new URL(endpoint);
requestOptions.hostname = url.hostname;
requestOptions.protocol = url.protocol;
requestOptions.port = url.port;
requestOptions.path = url.pathname;
if (requestOptions && requestOptions.idempotencyKey) {
requestOptions.headers[IDEMPOTENCY_KEY] = requestOptions.idempotencyKey;
delete requestOptions.idempotencyKey;
}
if (this.proxy && this.proxy.host) {
var _a = this.proxy, host = _a.host, port = _a.port, options = __rest(_a, ["host", "port"]);
requestOptions.agent = new HttpsProxyAgent(__assign({ host: host, port: port || 443 }, options));
}
else {
requestOptions.agent = new Agent(this.agentOptions);
}
requestOptions.headers["Cache-Control"] = "no-cache";
requestOptions.method = METHOD_POST;
requestOptions.headers[ACCEPT_CHARSET] = HttpURLConnectionClient.CHARSET;
requestOptions.headers[USER_AGENT] = applicationName + " " + Client.LIB_NAME + "/" + Client.LIB_VERSION;
return httpsRequest(requestOptions);
};
HttpURLConnectionClient.prototype.getQuery = function (params) {
return params.map(function (_a) {
var key = _a[0], value = _a[1];
return key + "=" + value;
}).join("&");
};
HttpURLConnectionClient.prototype.doPostRequest = function (connectionRequest, json) {
return new Promise(function (resolve, reject) {
connectionRequest.flushHeaders();
connectionRequest.on("response", function (res) {
var resData = "";
var getException = function () { return new HttpClientException("HTTP Exception: " + res.statusCode + ". " + res.statusMessage, res.statusCode, undefined, res.headers, res); };
var exception = getException();
res.on("data", function (data) {
if (res.statusCode && res.statusCode !== 200) {
try {
var formattedData = JSON.parse(data.toString());
var isApiError = "status" in formattedData;
var isRequestError = "errors" in formattedData;
if (isApiError) {
exception = new HttpClientException("HTTP Exception: " + formattedData.status + ". " + res.statusMessage + ": " + formattedData.message, formattedData.status, formattedData.errorCode, res.headers, res);
}
else if (isRequestError) {
exception = new Error(data);
}
}
catch (e) {
reject(exception);
}
finally {
reject(exception);
}
}
resData += data;
});
res.on("end", function () {
if (!res.complete) {
reject(new Error("The connection was terminated while the message was still being sent"));
}
resolve(resData);
});
res.on("error", reject);
});
connectionRequest.on("timeout", function () {
connectionRequest.abort();
});
connectionRequest.on("error", function (e) { return reject(new ApiException(e.message)); });
connectionRequest.write(Buffer.from(json));
connectionRequest.end();
});
};
HttpURLConnectionClient.prototype.installCertificateVerifier = function (terminalCertificatePath) {
try {
var certificateInput = fs.readFileSync(terminalCertificatePath);
this.agentOptions = {
ca: certificateInput,
checkServerIdentity: checkServerIdentity,
};
}
catch (e) {
return Promise.reject(new HttpClientException("Error loading certificate from path: " + e.message));
}
};
HttpURLConnectionClient.CHARSET = "utf-8";
return HttpURLConnectionClient;
}())