@taqueria/protocol 0.0.1-test.36 → 0.0.4-test

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,10 +1,11 @@
1
1
  {
2
2
  "name": "@taqueria/protocol",
3
- "version": "0.0.1-test.36",
3
+ "version": "0.0.4-test",
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": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "echo 'Nothing to build.' ",
8
9
  "build-ci": "echo \"just passing through for CI, no need to build\" && exit 0",
9
10
  "prod-config-ci": "echo \"just passing through for CI, no need to update SDK dependency\" && exit 0"
10
11
  },
@@ -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 (_) {
@@ -183,9 +187,14 @@ export interface UnvalidatedNetwork {
183
187
  readonly attributes?: Attributes
184
188
  }
185
189
 
190
+ // NOTE: This is a workaround as TypeScript doesn't support
191
+ // recursive / cyclical types yet. :(
192
+ type Accounts_Base = Record<string, AccountDetails>
193
+ export type Accounts = Record<string, (keyof Accounts_Base)|AccountDetails>
194
+
186
195
  export interface UnvalidatedSandbox extends UnvalidatedNetwork {
187
196
  readonly plugin?: string
188
- readonly accounts?: Record<string, AccountDetails>
197
+ readonly accounts?: Accounts
189
198
  }
190
199
 
191
200
  const hookType: unique symbol = Symbol()
@@ -460,7 +469,7 @@ export interface SandboxConfig {
460
469
  readonly rpcUrl?: string
461
470
  readonly protocol?: string
462
471
  readonly attributes?: Attributes
463
- readonly accounts: Record<string|'default', AccountDetails|string>
472
+ readonly accounts: Accounts
464
473
  }
465
474
 
466
475
  export interface NetworkConfig {
@@ -506,6 +515,7 @@ export class EconomicalProtocolHash extends StringLike {
506
515
 
507
516
  const economicProtocalType: unique symbol = Symbol()
508
517
  export class EconomicalProtocol {
518
+ [economicProtocalType]: void
509
519
  readonly hash: EconomicalProtocolHash
510
520
  readonly label: HumanReadableIdentifier | undefined
511
521
  constructor(hash: EconomicalProtocolHash, label: (HumanReadableIdentifier|undefined)=undefined) {
@@ -551,11 +561,11 @@ export class Network {
551
561
 
552
562
  const sandboxType: unique symbol = Symbol()
553
563
  export class Sandbox extends Network {
554
- readonly accounts: Record<string, AccountDetails>
564
+ readonly accounts: Accounts
555
565
  [sandboxType]: void
556
566
  readonly plugin?: string
557
567
 
558
- protected constructor(name: HumanReadableIdentifier, label: StringMax30, rpcUrl: Url, protocol: EconomicalProtocol, attributes: Attributes, plugin?: string, accounts?: Record<string, AccountDetails>) {
568
+ protected constructor(name: HumanReadableIdentifier, label: StringMax30, rpcUrl: Url, protocol: EconomicalProtocol, attributes: Attributes, plugin?: string, accounts?: Accounts) {
559
569
  super(name, label, rpcUrl, protocol, attributes)
560
570
  this.plugin = plugin
561
571
  this.accounts = accounts ? accounts: {}
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
+ };