@rosen-bridge/utils 0.1.0 → 0.3.0

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.
Files changed (63) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/constants.d.ts +3 -0
  3. package/dist/constants.d.ts.map +1 -0
  4. package/dist/constants.js +3 -0
  5. package/dist/downloadRosenAssets.d.ts +13 -0
  6. package/dist/downloadRosenAssets.d.ts.map +1 -0
  7. package/dist/downloadRosenAssets.js +48 -0
  8. package/dist/downloadTssBinary.d.ts +13 -0
  9. package/dist/downloadTssBinary.d.ts.map +1 -0
  10. package/dist/downloadTssBinary.js +45 -0
  11. package/dist/error.d.ts +4 -0
  12. package/dist/error.d.ts.map +1 -0
  13. package/dist/error.js +4 -0
  14. package/dist/index.d.ts +4 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +4 -0
  17. package/dist/lib/constants.d.ts +0 -1
  18. package/dist/lib/constants.d.ts.map +1 -1
  19. package/dist/lib/constants.js +1 -2
  20. package/dist/lib/downloadRosenAssets.d.ts.map +1 -1
  21. package/dist/lib/downloadRosenAssets.js +5 -4
  22. package/dist/lib/downloadTssBinary.d.ts.map +1 -1
  23. package/dist/lib/downloadTssBinary.js +2 -2
  24. package/dist/lib/index.d.ts +1 -0
  25. package/dist/lib/index.d.ts.map +1 -1
  26. package/dist/lib/index.js +2 -1
  27. package/dist/lib/types/index.d.ts +1 -0
  28. package/dist/lib/types/index.d.ts.map +1 -1
  29. package/dist/lib/types/index.js +1 -1
  30. package/dist/lib/utils/github.d.ts +123 -392
  31. package/dist/lib/utils/github.d.ts.map +1 -1
  32. package/dist/lib/utils/github.js +42 -11
  33. package/dist/lib/utils/rosen.d.ts +5 -0
  34. package/dist/lib/utils/rosen.d.ts.map +1 -1
  35. package/dist/lib/utils/rosen.js +6 -1
  36. package/dist/tsconfig.build.tsbuildinfo +1 -1
  37. package/dist/types/index.d.ts +7 -0
  38. package/dist/types/index.d.ts.map +1 -0
  39. package/dist/types/index.js +2 -0
  40. package/dist/utils/github.d.ts +241 -0
  41. package/dist/utils/github.d.ts.map +1 -0
  42. package/dist/utils/github.js +113 -0
  43. package/dist/utils/rosen.d.ts +19 -0
  44. package/dist/utils/rosen.d.ts.map +1 -0
  45. package/dist/utils/rosen.js +19 -0
  46. package/lib/constants.ts +0 -1
  47. package/lib/downloadRosenAssets.ts +5 -3
  48. package/lib/downloadTssBinary.ts +61 -0
  49. package/lib/index.ts +1 -0
  50. package/lib/types/index.ts +2 -0
  51. package/lib/utils/github.ts +60 -10
  52. package/lib/utils/rosen.ts +7 -0
  53. package/package.json +7 -12
  54. package/tests/data/octokit.data.ts +88 -10
  55. package/tests/downloadRosenAssets.spec.ts +9 -9
  56. package/tests/downloadTssBinary.spec.ts +153 -0
  57. package/tests/mocks/octokit.mock.ts +4 -4
  58. package/tests/utils/github.spec.ts +173 -21
  59. package/tests/utils/rosen.spec.ts +39 -1
  60. package/tsconfig.build.json +4 -0
  61. package/tsconfig.build.tsbuildinfo +1 -0
  62. package/tsconfig.json +2 -2
  63. package/dist/tsconfig.tsbuildinfo +0 -1
@@ -6,6 +6,13 @@
6
6
  export const isValidAssetName = (chainType: string) => (assetName: string) =>
7
7
  new RegExp(`(contracts-.+|tokensMap)-${chainType}-.+.json`).test(assetName);
8
8
 
9
+ /**
10
+ * Check if an OS name is a valid supported tss OS
11
+ * @param OSName
12
+ */
13
+ export const isValidOS = (OSName: string) => (releaseAssetName: string) =>
14
+ new RegExp(`(rosenTss-${OSName}-.+).zip`).test(releaseAssetName);
15
+
9
16
  /**
10
17
  * Remove chain type and tag from the asset name and optionally replaces them
11
18
  * with a suffix
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@rosen-bridge/utils",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Misc utility functions used inside Rosen bridge projects",
5
- "main": "dist/lib/index.js",
6
- "types": "dist/lib/index.d.ts",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
7
  "type": "module",
8
8
  "scripts": {
9
9
  "prettify": "prettier --write . --ignore-path .gitignore",
@@ -18,20 +18,15 @@
18
18
  "type": "git",
19
19
  "url": "git+https://github.com/rosen-bridge/utils.git"
20
20
  },
21
- "license": "GPL-3.0",
21
+ "license": "MIT",
22
22
  "devDependencies": {
23
- "@types/download": "^8.0.2",
24
23
  "@babel/preset-env": "^7.19.0",
24
+ "@types/download": "^8.0.2",
25
25
  "@types/jest": "^29.1.2",
26
- "@types/node": "^18.0.0",
27
- "@typescript-eslint/eslint-plugin": "^5.30.7",
28
- "@typescript-eslint/parser": "^5.26.0",
29
- "eslint": "^8.16.0",
30
- "eslint-config-prettier": "^8.5.0",
26
+ "@types/node": "^20.11.0",
31
27
  "jest": "^29.2.0",
32
- "prettier": "^2.8.4",
33
28
  "ts-jest": "^29.0.3",
34
- "typescript": "^4.9.5"
29
+ "typescript": "^5.8.3"
35
30
  },
36
31
  "directories": {
37
32
  "lib": "lib",
@@ -4,7 +4,7 @@ export type PartialReleases = Partial<GithubRelease>[];
4
4
 
5
5
  export const mainNetPrereleaseRelease = {
6
6
  id: 1,
7
- tag: '1',
7
+ tag_name: '1',
8
8
  prerelease: true,
9
9
  assets: [
10
10
  {
@@ -21,7 +21,7 @@ export const mainNetPrereleaseRelease = {
21
21
 
22
22
  export const mainNetStableRelease = {
23
23
  id: 2,
24
- tag: '2',
24
+ tag_name: '2',
25
25
  prerelease: false,
26
26
  assets: [
27
27
  {
@@ -36,9 +36,80 @@ export const mainNetStableRelease = {
36
36
  ],
37
37
  };
38
38
 
39
+ export const tssTag1 = {
40
+ id: 1,
41
+ tag_name: 'tss-api-1.0.0',
42
+ name: 'tss-api-1.0.0',
43
+ prerelease: false,
44
+ assets: [
45
+ {
46
+ name: 'rosenTss-linux-tss-api-1.0.0.zip',
47
+ browser_download_url:
48
+ 'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-linux-tss-api-1.0.0.zip',
49
+ } as any,
50
+ {
51
+ name: 'rosenTss-macOS-tss-api-1.0.0.zip',
52
+ browser_download_url:
53
+ 'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-macOS-tss-api-1.0.0.zip',
54
+ } as any,
55
+ {
56
+ name: 'rosenTss-windows-tss-api-1.0.0.zip',
57
+ browser_download_url:
58
+ 'https://example.com/sign-protocols/releases/download/tss-api-1.0.0/rosenTss-windows-tss-api-1.0.0.zip',
59
+ } as any,
60
+ ],
61
+ };
62
+ export const tssTag2 = {
63
+ id: 2,
64
+ tag_name: 'tss-api-2.0.0',
65
+ name: 'tss-api-2.0.0',
66
+ prerelease: false,
67
+ assets: [
68
+ {
69
+ name: 'rosenTss-linux-tss-api-2.0.0.zip',
70
+ browser_download_url:
71
+ 'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-linux-tss-api-2.0.0.zip',
72
+ } as any,
73
+ {
74
+ name: 'rosenTss-macOS-tss-api-2.0.0.zip',
75
+ browser_download_url:
76
+ 'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-macOS-tss-api-2.0.0.zip',
77
+ } as any,
78
+ {
79
+ name: 'rosenTss-windows-tss-api-2.0.0.zip',
80
+ browser_download_url:
81
+ 'https://example.com/sign-protocols/releases/download/tss-api-2.0.0/rosenTss-windows-tss-api-2.0.0.zip',
82
+ } as any,
83
+ ],
84
+ };
85
+
86
+ export const tssTag3PreRelease = {
87
+ id: 3,
88
+ tag_name: 'tss-api-3.0.0',
89
+ name: 'tss-api-3.0.0',
90
+ prerelease: true,
91
+ assets: [
92
+ {
93
+ name: 'rosenTss-linux-tss-api-3.0.0.zip',
94
+ browser_download_url:
95
+ 'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-linux-tss-api-3.0.0.zip',
96
+ } as any,
97
+ {
98
+ name: 'rosenTss-macOS-tss-api-3.0.0.zip',
99
+ browser_download_url:
100
+ 'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-macOS-tss-api-3.0.0.zip',
101
+ } as any,
102
+ {
103
+ name: 'rosenTss-windows-tss-api-3.0.0.zip',
104
+ browser_download_url:
105
+ 'https://example.com/sign-protocols/releases/download/tss-api-3.0.0/rosenTss-windows-tss-api-3.0.0.zip',
106
+ } as any,
107
+ ],
108
+ };
109
+
39
110
  export const testNetPrereleaseRelease = {
40
111
  id: 4,
41
- tag: '4',
112
+ tag_name: '4',
42
113
  prerelease: true,
43
114
  assets: [
44
115
  {
@@ -51,7 +122,7 @@ export const testNetPrereleaseRelease = {
51
122
 
52
123
  export const testNetStableRelease = {
53
124
  id: 5,
54
- tag: '5',
125
+ tag_name: '5',
55
126
  prerelease: false,
56
127
  assets: [
57
128
  {
@@ -62,12 +133,12 @@ export const testNetStableRelease = {
62
133
  ],
63
134
  };
64
135
 
65
- export const releases = [
136
+ export const contractReleases = [
66
137
  mainNetPrereleaseRelease,
67
138
  mainNetStableRelease,
68
139
  {
69
140
  id: 3,
70
- tag: '3',
141
+ tag_name: '3',
71
142
  prerelease: false,
72
143
  assets: [
73
144
  {
@@ -81,7 +152,7 @@ export const releases = [
81
152
  testNetStableRelease,
82
153
  {
83
154
  id: 6,
84
- tag: '6',
155
+ tag_name: '6',
85
156
  prerelease: false,
86
157
  assets: [
87
158
  {
@@ -93,7 +164,7 @@ export const releases = [
93
164
  },
94
165
  {
95
166
  id: 7,
96
- tag: '7',
167
+ tag_name: '7',
97
168
  prerelease: false,
98
169
  assets: [
99
170
  {
@@ -105,7 +176,7 @@ export const releases = [
105
176
  },
106
177
  {
107
178
  id: 8,
108
- tag: '8',
179
+ tag_name: '8',
109
180
  prerelease: false,
110
181
  assets: [
111
182
  {
@@ -117,7 +188,7 @@ export const releases = [
117
188
  },
118
189
  {
119
190
  id: 9,
120
- tag: '9',
191
+ tag_name: '9',
121
192
  prerelease: false,
122
193
  assets: [
123
194
  {
@@ -128,3 +199,10 @@ export const releases = [
128
199
  ],
129
200
  },
130
201
  ] satisfies PartialReleases;
202
+
203
+ export const tssReleases = [
204
+ mainNetStableRelease, // in case of sign-protocols mono repo we have other tags
205
+ tssTag3PreRelease,
206
+ tssTag2,
207
+ tssTag1,
208
+ ] satisfies PartialReleases;
@@ -7,7 +7,7 @@ import { RosenAssetsDownloadError } from '../lib';
7
7
  import {
8
8
  mainNetPrereleaseRelease,
9
9
  mainNetStableRelease,
10
- releases,
10
+ contractReleases,
11
11
  } from './data/octokit.data';
12
12
 
13
13
  import { mockOctokit, mockOctokitGetReleaseByTag } from './mocks/octokit.mock';
@@ -16,7 +16,7 @@ jest.mock('download');
16
16
 
17
17
  describe('downloadRosenAssets', () => {
18
18
  beforeEach(() => {
19
- mockOctokit();
19
+ mockOctokit(contractReleases);
20
20
  });
21
21
 
22
22
  /**
@@ -24,7 +24,7 @@ describe('downloadRosenAssets', () => {
24
24
  * @dependencies
25
25
  * - mocked Octokit
26
26
  * @scenario
27
- * - mock Octokit `listReleases` to return 9 releases
27
+ * - mock Octokit `listReleases` to return 9 contractReleases
28
28
  * - call `downloadRosenAssets` with mainnet chain type and `rosen` download
29
29
  * directory
30
30
  * @expected
@@ -60,7 +60,7 @@ describe('downloadRosenAssets', () => {
60
60
  * @dependencies
61
61
  * - mocked Octokit
62
62
  * @scenario
63
- * - mock Octokit `listReleases` to return 9 releases
63
+ * - mock Octokit `listReleases` to return 9 contractReleases
64
64
  * - call `downloadRosenAssets` with mainnet chain type, `rosen` download
65
65
  * directory and including prereleases
66
66
  * @expected
@@ -99,7 +99,7 @@ describe('downloadRosenAssets', () => {
99
99
  * @dependencies
100
100
  * - mocked Octokit
101
101
  * @scenario
102
- * - mock Octokit `listReleases` to return 9 releases
102
+ * - mock Octokit `listReleases` to return 9 contractReleases
103
103
  * - call `downloadRosenAssets` with mainnet chain type, `rosen` download
104
104
  * directory, including prereleases and a providing a suffix
105
105
  * @expected
@@ -141,13 +141,13 @@ describe('downloadRosenAssets', () => {
141
141
  * - `download` should be called with the assets of "3" tag release
142
142
  */
143
143
  it('should download a Rosen asset by tag', async () => {
144
- mockOctokitGetReleaseByTag();
144
+ mockOctokitGetReleaseByTag(contractReleases);
145
145
  await downloadRosenAssets('mainnet', 'rosen', {
146
146
  tag: '3',
147
147
  });
148
148
 
149
149
  expect(download).toHaveBeenCalledWith(
150
- releases[2].assets[0].browser_download_url,
150
+ contractReleases[2].assets[0].browser_download_url,
151
151
  'rosen',
152
152
  {
153
153
  filename: 'contracts-awesomechain.json',
@@ -162,7 +162,7 @@ describe('downloadRosenAssets', () => {
162
162
  * - mocked Octokit
163
163
  * - emptied mocked download package
164
164
  * @scenario
165
- * - mock Octokit `listReleases` to return 9 releases
165
+ * - mock Octokit `listReleases` to return 9 contractReleases
166
166
  * - clear download package mock data (so that we can check calls count)
167
167
  * - call `downloadRosenAssets` with "no-release-net" chain type and `rosen`
168
168
  * download directory
@@ -183,7 +183,7 @@ describe('downloadRosenAssets', () => {
183
183
  * - mocked Octokit
184
184
  * - mocked download package
185
185
  * @scenario
186
- * - mock Octokit `listReleases` to return 9 releases
186
+ * - mock Octokit `listReleases` to return 9 contractReleases
187
187
  * - mock download package to throw an error
188
188
  * - call `downloadRosenAssets` with mainnet chain type and `rosen` download
189
189
  * directory
@@ -0,0 +1,153 @@
1
+ import download from 'download';
2
+
3
+ import { downloadTssBinary } from '../lib';
4
+ import { RosenAssetsDownloadError } from '../lib';
5
+
6
+ import {
7
+ tssReleases,
8
+ tssTag1,
9
+ tssTag2,
10
+ tssTag3PreRelease,
11
+ } from './data/octokit.data';
12
+
13
+ import { mockOctokit, mockOctokitGetReleaseByTag } from './mocks/octokit.mock';
14
+
15
+ jest.mock('download');
16
+
17
+ describe('downloadTssBinary', () => {
18
+ beforeEach(() => {
19
+ mockOctokit(tssReleases);
20
+ });
21
+
22
+ /**
23
+ * @target `downloadTssBinary` should download Tss binary correctly by specific tag
24
+ * @dependencies
25
+ * - mocked Octokit
26
+ * - mocked Octokit GetReleaseByTag
27
+ * @scenario
28
+ * - mock Octokit `listReleases` to return tssReleases
29
+ * - mock Octokit GetReleaseByTag to return desired release
30
+ * - call `downloadTssBinary` with Windows OS name, `tss-api-1.0.0` and `rosen` download
31
+ * directory
32
+ * @expected
33
+ * - `download` function should be called with third asset of tss-api stable
34
+ * release (windows) download url and `bin` download directory
35
+ */
36
+ it('should download Tss binary correctly with specific tag', async () => {
37
+ mockOctokitGetReleaseByTag(tssReleases);
38
+ await downloadTssBinary('bin', {
39
+ osName: 'windows',
40
+ tag: 'tss-api-1.0.0',
41
+ regex: false,
42
+ });
43
+
44
+ expect(download).toHaveBeenCalledWith(
45
+ tssTag1.assets[2].browser_download_url,
46
+ 'bin'
47
+ );
48
+ });
49
+
50
+ /**
51
+ * @target `downloadTssBinary` should download Tss binary correctly by
52
+ * prefix of a tag
53
+ * @dependencies
54
+ * - mocked Octokit
55
+ * @scenario
56
+ * - mock Octokit `listReleases` to return tssReleases
57
+ * - call `downloadTssBinary` with linux OS name, prefix tag `tss-api` and
58
+ * regex should be true
59
+ * @expected
60
+ * - `download` function should be called with first asset of tss-api stable
61
+ * release (linux) download url and `bin` download directory
62
+ */
63
+ it('should download Tss binary correctly with prefix of tag', async () => {
64
+ await downloadTssBinary('bin', {
65
+ osName: 'linux',
66
+ tag: 'tss-api',
67
+ regex: true,
68
+ });
69
+
70
+ expect(download).toHaveBeenCalledWith(
71
+ tssTag2.assets[0].browser_download_url,
72
+ 'bin'
73
+ );
74
+ });
75
+
76
+ /**
77
+ * @target `downloadTssBinary` should download Tss binary correctly by
78
+ * prefix of a tag for an included prereleases release
79
+ * @dependencies
80
+ * - mocked Octokit
81
+ * @scenario
82
+ * - mock Octokit `listReleases` to return tssReleases
83
+ * - call `downloadTssBinary` with linux OS name, prefix tag `tss-api`,
84
+ * enable includePrereleases and regex should be true
85
+ * @expected
86
+ * - `download` function should be called with first asset of tss-api prerelease
87
+ * release (linux) download url and `bin` download directory
88
+ */
89
+ it('should download prerelease Tss binary correctly with prefix of tag', async () => {
90
+ await downloadTssBinary('bin', {
91
+ osName: 'linux',
92
+ tag: 'tss-api',
93
+ regex: true,
94
+ includePrereleases: true,
95
+ });
96
+
97
+ expect(download).toHaveBeenCalledWith(
98
+ tssTag3PreRelease.assets[0].browser_download_url,
99
+ 'bin'
100
+ );
101
+ });
102
+
103
+ /**
104
+ * @target `downloadTssBinary` should not call `download` function when no
105
+ * matching release is found
106
+ * @dependencies
107
+ * - mocked Octokit
108
+ * - emptied mocked download package
109
+ * @scenario
110
+ * - mock Octokit `listReleases` to return tssReleases
111
+ * - clear download package mock data (so that we can check calls count)
112
+ * - call `downloadTssBinary` with "no-release-tag", osName linux and `bin`
113
+ * download directory
114
+ * @expected
115
+ * - `download` function should not get called
116
+ */
117
+ it('should not call `download` function when no matching release is found', async () => {
118
+ jest.mocked(download).mockClear();
119
+
120
+ await downloadTssBinary('bin', {
121
+ osName: 'linux',
122
+ tag: 'no-release-tag',
123
+ regex: true,
124
+ });
125
+
126
+ expect(download).toHaveBeenCalledTimes(0);
127
+ });
128
+
129
+ /**
130
+ * @target `downloadTssBinary` should throw an error when an error happens
131
+ * @dependencies
132
+ * - mocked Octokit
133
+ * - mocked download package
134
+ * @scenario
135
+ * - mock Octokit `listReleases` to return tssReleases
136
+ * - mock download package to throw an error
137
+ * - call `downloadTssBinary` with linux OS name, prefix tag `tss-api` and
138
+ * regex should be true
139
+ * @expected
140
+ * - `download` function should throw `RosenAssetsDownloadError`
141
+ */
142
+ it('should throw an error when an error happens', async () => {
143
+ jest.mocked(download).mockRejectedValue(new Error('Bad!'));
144
+
145
+ const downloadPromise = downloadTssBinary('bin', {
146
+ osName: 'linux',
147
+ tag: 'tss-api',
148
+ regex: true,
149
+ });
150
+
151
+ await expect(downloadPromise).rejects.toThrow(RosenAssetsDownloadError);
152
+ });
153
+ });
@@ -1,10 +1,10 @@
1
1
  import { Octokit } from 'octokit';
2
2
 
3
- import { releases } from '../data/octokit.data';
3
+ import { contractReleases, PartialReleases } from '../data/octokit.data';
4
4
 
5
5
  import { DEFAULT_RELEASES_FETCHING_PAGE_SIZE } from '../../lib/constants';
6
6
 
7
- export const mockOctokit = () =>
7
+ export const mockOctokit = (releases: any[]) =>
8
8
  jest.mocked(Octokit).mockImplementation(() => {
9
9
  let page = 0;
10
10
  return {
@@ -25,14 +25,14 @@ export const mockOctokit = () =>
25
25
  /**
26
26
  * mock `getReleaseByTag` of Octokit
27
27
  */
28
- export const mockOctokitGetReleaseByTag = () =>
28
+ export const mockOctokitGetReleaseByTag = (releases: PartialReleases) =>
29
29
  jest.mocked(Octokit).mockImplementation(() => {
30
30
  return {
31
31
  rest: {
32
32
  repos: {
33
33
  getReleaseByTag: async ({ tag }: { tag: string }) => {
34
34
  return {
35
- data: releases.find((release) => release.tag === tag),
35
+ data: releases.find((release) => release.tag_name === tag),
36
36
  };
37
37
  },
38
38
  },