Project

General

Profile

Download (2.72 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / jsonschema / urllib_mock.js @ 57ce414c

1
/* SPDX-License-Identifier: MIT AND CC0-1.0
2
 *
3
 * This file is part of Haketilo.
4
 *
5
 * Function: Replacement for require('url') to use with jsonschema library.
6
 *
7
 * License for the resolve() function:
8
 *
9
 ***************************************
10
 *
11
 * Copyright Node.js contributors. All rights reserved.
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to
15
 * deal in the Software without restriction, including without limitation the
16
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
17
 * sell copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in
21
 * all copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29
 * IN THE SOFTWARE.
30
 *
31
 *******************************************************************************
32
 *
33
 * License notice for the rest of the file:
34
 *
35
 ***************************************
36
 *
37
 * Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
38
 *
39
 * This program is free software: you can redistribute it and/or modify
40
 * it under the terms of the CC0 1.0 Universal License as published by
41
 * the Creative Commons Corporation.
42
 *
43
 * This program is distributed in the hope that it will be useful,
44
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46
 * CC0 1.0 Universal License for more details.
47
 */
48

    
49
/*
50
 * The original jsonschema code used legacy resolve() function from
51
 * require('url'). Here we define in a replacement for it.
52
 */
53

    
54
const dummy_host = 'haketilo.resolve.example.com';
55
const dummy_prefix = `http://${dummy_host}/`;
56

    
57
function resolve(from, to) {
58
    const resolvedUrl = new URL(to, new URL(from, dummy_prefix));
59
    if (resolvedUrl.hostname === dummy_host) {
60
	// `from` is a relative URL.
61
	const { pathname, search, hash } = resolvedUrl;
62
	return pathname + search + hash;
63
    }
64
    return resolvedUrl.toString();
65
}
66
#EXPORT resolve
67

    
68
function parse(url_string) {
69
    if (arguments.length > 1)
70
	throw "error: this is a mocked version of parse() that only accepts one argument";
71

    
72
    return new URL(url_string);
73
}
74
#EXPORT parse
(4-4/5)