@taqueria/protocol 0.0.1-test.36 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taqueria/protocol",
3
- "version": "0.0.1-test.36",
3
+ "version": "0.0.1",
4
4
  "description": "A TypeScript package which contains types that are to be shared between @taqueria/node-sdk and @taqueria/taqueria.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,11 +20,13 @@
20
20
  ],
21
21
  "author": "ECAD Labs",
22
22
  "license": "Apache-2.0",
23
+
23
24
  "repository": {
24
25
  "type": "git",
25
26
  "url": "https://github.com/ecadlabs/taqueria.git",
26
27
  "directory": "taqueria-protocol"
27
28
  },
29
+
28
30
  "bugs": {
29
31
  "url": "https://github.com/ecadlabs/taqueria/issues"
30
32
  },
@@ -3,6 +3,10 @@
3
3
  // or errors emited
4
4
  // @ts-ignore
5
5
  import {SanitizedAbsPath, SHA256} from '../taqueria-utils/taqueria-utils-types.ts'
6
+ // @ts-ignore
7
+ import {urlParse} from './url-parse.ts'
8
+
9
+ type URL = ReturnType<typeof urlParse>
6
10
 
7
11
  /**
8
12
  * String-like types
@@ -104,7 +108,7 @@ export class Url {
104
108
  }
105
109
  static create(value: string): Url | undefined {
106
110
  try {
107
- const url = new URL(value)
111
+ const url = urlParse(value)
108
112
  return new Url(url)
109
113
  }
110
114
  catch (_) {
package/url-join.ts ADDED
@@ -0,0 +1,73 @@
1
+ export const urlJoin = function (...args: any[]) {
2
+ let input;
3
+
4
+ if (typeof args[0] === 'object') {
5
+ input = args[0];
6
+ } else {
7
+ input = [].slice.call(args);
8
+ }
9
+
10
+ return normalize(input);
11
+ };
12
+
13
+ const normalize = (strArray: Array<string>) => {
14
+ const resultArray = [];
15
+ if (strArray.length === 0) {
16
+ return '';
17
+ }
18
+
19
+ if (typeof strArray[0] !== 'string') {
20
+ throw new TypeError('Url must be a string. Received ' + strArray[0]);
21
+ }
22
+
23
+ // If the first part is a plain protocol, we combine it with the next part.
24
+ if (strArray[0].match(/^[^/:]+:\/*$/) && strArray.length > 1) {
25
+ const first = strArray.shift();
26
+ strArray[0] = first + strArray[0];
27
+ }
28
+
29
+ // There must be two or three slashes in the file protocol, two slashes in anything else.
30
+ if (strArray[0].match(/^file:\/\/\//)) {
31
+ strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1:///');
32
+ } else {
33
+ strArray[0] = strArray[0].replace(/^([^/:]+):\/*/, '$1://');
34
+ }
35
+
36
+ for (let i = 0; i < strArray.length; i++) {
37
+ let component = strArray[i];
38
+
39
+ if (typeof component !== 'string') {
40
+ throw new TypeError('Url must be a string. Received ' + component);
41
+ }
42
+
43
+ if (component === '') {
44
+ continue;
45
+ }
46
+
47
+ if (i > 0) {
48
+ // Removing the starting slashes for each component but the first.
49
+ component = component.replace(/^[\/]+/, '');
50
+ }
51
+ if (i < strArray.length - 1) {
52
+ // Removing the ending slashes for each component but the last.
53
+ component = component.replace(/[\/]+$/, '');
54
+ } else {
55
+ // For the last component we will combine multiple slashes to a single one.
56
+ component = component.replace(/[\/]+$/, '/');
57
+ }
58
+
59
+ resultArray.push(component);
60
+ }
61
+
62
+ let str = resultArray.join('/');
63
+ // Each input component is now separated by a single slash except the possible first plain protocol part.
64
+
65
+ // remove trailing slash before parameters or hash
66
+ str = str.replace(/\/(\?|&|#[^!])/g, '$1');
67
+
68
+ // replace ? in parameters with &
69
+ let parts = str.split('?');
70
+ str = parts.shift() + (parts.length > 0 ? '?' : '') + parts.join('&');
71
+
72
+ return str;
73
+ };
package/url-parse.ts ADDED
@@ -0,0 +1,150 @@
1
+ // @ts-ignore
2
+ import { urlJoin } from "./url-join.ts"
3
+
4
+ export interface IUrlParams {
5
+ key: string;
6
+ value: string;
7
+ }
8
+
9
+ export interface IUrlParse {
10
+ protocol?: string;
11
+ hostname?: string;
12
+ pathname?: Array<string> | string;
13
+ port?: string | number;
14
+ username?: string;
15
+ password?: string;
16
+ query?: Array<IUrlParams> | null;
17
+ }
18
+
19
+ export interface IUrl {
20
+ href: string;
21
+ origin: string;
22
+ host: string;
23
+ protocol: string;
24
+ username: string;
25
+ password: string;
26
+ hostname: string;
27
+ port: string;
28
+ pathname: string;
29
+ hash: string;
30
+ search: string;
31
+ toString: () => string;
32
+ }
33
+
34
+ export const urlParse = (
35
+ data: IUrlParse | string | null | undefined = {}
36
+ ): IUrl => {
37
+ let url = new URL("http://localhost");
38
+ let innerData: IUrlParse = {};
39
+
40
+ if (typeof data === "string") {
41
+ url = new URL(data);
42
+
43
+ if (url.protocol) {
44
+ innerData.protocol = url.protocol;
45
+ }
46
+ if (url.hostname) {
47
+ innerData.hostname = url.hostname;
48
+ }
49
+ if (url.port) {
50
+ innerData.port = url.port;
51
+ }
52
+ if (url.username) {
53
+ innerData.username = url.username;
54
+ }
55
+ if (url.password) {
56
+ innerData.password = url.password;
57
+ }
58
+ if (url.searchParams) {
59
+ url.searchParams.forEach((value, key) => {
60
+ innerData?.query?.push({ value, key });
61
+ });
62
+ }
63
+ } else {
64
+ if (!data) {
65
+ // if null or undefined
66
+ innerData = {};
67
+ } else {
68
+ // if data exists
69
+ innerData = data;
70
+ }
71
+ }
72
+
73
+ const {
74
+ protocol = "http",
75
+ hostname,
76
+ pathname,
77
+ port,
78
+ username,
79
+ password,
80
+ query = null,
81
+ } = innerData;
82
+
83
+ if (!protocol || !hostname) {
84
+ throw new Error("You should at least set the protocol and the hostname");
85
+ }
86
+
87
+ if (protocol) {
88
+ url.protocol = protocol;
89
+ }
90
+
91
+ if (hostname) {
92
+ url.hostname = hostname;
93
+ }
94
+
95
+ if (pathname) {
96
+ if (Array.isArray(pathname)) {
97
+ url.pathname = urlJoin(...pathname);
98
+ } else {
99
+ url.pathname = pathname;
100
+ }
101
+ }
102
+
103
+ if (typeof port === "number") {
104
+ url.port = String(port).toString();
105
+ } else {
106
+ if (port) {
107
+ url.port = port;
108
+ }
109
+ }
110
+ if (username) {
111
+ url.username = username;
112
+ }
113
+ if (password) {
114
+ url.password = password;
115
+ }
116
+ if (query && Array.isArray(query)) {
117
+ query.forEach(({ key, value }) => {
118
+ url.searchParams.append(key, value);
119
+ });
120
+ }
121
+
122
+ const result: IUrl = {
123
+ href: url.href,
124
+ origin: url.origin,
125
+ protocol: url.protocol,
126
+ username: url.username,
127
+ password: url.password,
128
+ host: url.host,
129
+ hostname: url.hostname,
130
+ port: url.port,
131
+ pathname: url.pathname,
132
+ hash: url.hash,
133
+ search: url.search,
134
+ toString() {
135
+ return this.href;
136
+ },
137
+ };
138
+
139
+ if (protocol) {
140
+ const toUseProtocol = protocol.trim().endsWith(":")
141
+ ? protocol.trim()
142
+ : `${protocol.trim()}:`;
143
+
144
+ result.href = result.href.replace(result.protocol, toUseProtocol);
145
+ result.origin = result.origin.replace(result.protocol, toUseProtocol);
146
+ result.protocol = toUseProtocol;
147
+ }
148
+
149
+ return result;
150
+ };