Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ulixee/secret-agent
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: cc1a8cae35ef431c0f206ed65c03c8880a4b1798
Choose a base ref
..
head repository: ulixee/secret-agent
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 04fac079b39a5c2c348a81b197bab5b0a78b8ed1
Choose a head ref
Showing with 8,979 additions and 5,643 deletions.
  1. +1 −0 .eslintrc.js
  2. +3 −0 .github/FUNDING.yml
  3. +17 −0 .github/workflows/new-version.yml
  4. +136 −0 CHANGELOG.md
  5. +7 −1 README.md
  6. +99 −0 client/CHANGELOG.md
  7. +1 −1 client/connections/ConnectionFactory.ts
  8. +32 −3 client/connections/ConnectionToCore.ts
  9. +2 −2 client/connections/RemoteConnectionToCore.ts
  10. +2 −2 client/lib/Agent.ts
  11. +35 −12 client/lib/CoreCommandQueue.ts
  12. +13 −3 client/lib/CoreFrameEnvironment.ts
  13. +4 −0 client/lib/CoreSessions.ts
  14. +9 −5 client/lib/CoreTab.ts
  15. +38 −7 client/lib/FrameEnvironment.ts
  16. +3 −2 client/lib/FrozenTab.ts
  17. +4 −3 client/lib/Tab.ts
  18. +9 −9 client/package.json
  19. +77 −0 commons/CHANGELOG.md
  20. +55 −0 commons/EventSubscriber.ts
  21. +1 −0 commons/Logger.ts
  22. +21 −11 commons/Resolvable.ts
  23. +8 −1 commons/ShutdownHandler.ts
  24. +1 −1 commons/downloadFile.ts
  25. +5 −55 commons/eventUtils.ts
  26. +4 −4 commons/package.json
  27. +120 −0 core/CHANGELOG.md
  28. +15 −18 core/index.ts
  29. +1 −1 core/injected-scripts/domReplayer.ts
  30. +1 −74 core/injected-scripts/domStorage.ts
  31. +60 −0 core/injected-scripts/indexedDbRestore.ts
  32. +3 −1 core/injected-scripts/jsPath.ts
  33. +6 −2 core/lib/AwaitedEventListener.ts
  34. +7 −0 core/lib/CommandFormatter.ts
  35. +9 −3 core/lib/CommandRecorder.ts
  36. +20 −5 core/lib/CorePlugins.ts
  37. +1 −1 core/lib/DetachedTabState.ts
  38. +42 −11 core/lib/FrameEnvironment.ts
  39. +56 −25 core/lib/FrameNavigations.ts
  40. +1 −1 core/lib/FrameNavigationsObserver.ts
  41. +7 −7 core/lib/InjectedScripts.ts
  42. +20 −13 core/lib/Session.ts
  43. +5 −1 core/lib/SessionState.ts
  44. +81 −41 core/lib/Tab.ts
  45. +64 −58 core/lib/UserProfile.ts
  46. +16 −16 core/package.json
  47. +14 −14 core/server/ConnectionToClient.ts
  48. +39 −17 core/server/ConnectionToReplay.ts
  49. +5 −1 core/server/index.ts
  50. +2 −1 core/start.ts
  51. +3 −3 core/test/domRecorder.test.ts
  52. +77 −2 core/test/domReplay.test.ts
  53. +76 −18 core/test/navigation.test.ts
  54. +40 −3 core/test/user-profile.test.ts
  55. +84 −0 full-client/CHANGELOG.md
  56. +12 −11 full-client/package.json
  57. +4 −0 full-client/test/basic.test.ts
  58. +184 −1 full-client/test/detach.test.ts
  59. +50 −4 full-client/test/filechooser.test.ts
  60. +48 −3 full-client/test/waitForLocation.test.ts
  61. +90 −0 interfaces/CHANGELOG.md
  62. +6 −1 interfaces/IClientPlugin.ts
  63. +2 −1 interfaces/IConnectionTransport.ts
  64. +9 −8 interfaces/ICorePlugin.ts
  65. +2 −0 interfaces/IHttpResourceLoadDetails.ts
  66. +6 −0 interfaces/IHttpSocketAgent.ts
  67. +11 −0 interfaces/IHttpSocketConnectOptions.ts
  68. +23 −0 interfaces/IHttpSocketWrapper.ts
  69. +2 −0 interfaces/IInteractions.ts
  70. +2 −1 interfaces/IPuppetBrowser.ts
  71. +17 −5 interfaces/IPuppetFrame.ts
  72. +43 −3 interfaces/IRegisteredEventListener.ts
  73. +1 −0 interfaces/ISessionCreateOptions.ts
  74. +2 −2 interfaces/package.json
  75. +1 −1 lerna.json
  76. +78 −0 mitm-socket/CHANGELOG.md
  77. +25 −26 mitm-socket/index.ts
  78. +1 −1 mitm-socket/lib/MitmSocketSession.ts
  79. +2 −1 mitm-socket/package.build.json
  80. +5 −5 mitm-socket/package.json
  81. +81 −0 mitm/CHANGELOG.md
  82. +27 −4 mitm/handlers/BaseHttpHandler.ts
  83. +1 −1 mitm/handlers/BlockHandler.ts
  84. +49 −7 mitm/handlers/HeadersHandler.ts
  85. +40 −30 mitm/handlers/Http2PushPromiseHandler.ts
  86. +34 −9 mitm/handlers/HttpRequestHandler.ts
  87. +21 −6 mitm/handlers/HttpUpgradeHandler.ts
  88. +8 −5 mitm/handlers/RequestSession.ts
  89. +1 −0 mitm/lib/BrowserRequestMatcher.ts
  90. +3 −1 mitm/lib/Dns.ts
  91. +9 −5 mitm/lib/DnsOverTlsSocket.ts
  92. +12 −12 mitm/lib/Http2SessionBinding.ts
  93. +35 −14 mitm/lib/MitmProxy.ts
  94. +59 −42 mitm/lib/MitmRequestAgent.ts
  95. +6 −2 mitm/lib/MitmRequestContext.ts
  96. +7 −3 mitm/lib/SocketPool.ts
  97. +11 −11 mitm/package.json
  98. +1 −1 package.build.json
  99. +4 −3 package.json
  100. +67 −0 plugin-utils/CHANGELOG.md
  101. +8 −8 plugin-utils/package.json
  102. +77 −0 plugins/default-browser-emulator/CHANGELOG.md
  103. +31 −13 plugins/default-browser-emulator/index.ts
  104. +32 −0 plugins/default-browser-emulator/injected-scripts/webrtc.ts
  105. +4 −0 plugins/default-browser-emulator/lib/BrowserData.ts
  106. +1 −0 plugins/default-browser-emulator/lib/DataLoader.ts
  107. +4 −0 plugins/default-browser-emulator/lib/DomOverridesBuilder.ts
  108. +1 −0 plugins/default-browser-emulator/lib/VersionUtils.ts
  109. +11 −1 plugins/default-browser-emulator/lib/helpers/configureBrowserLaunchArgs.ts
  110. +90 −0 plugins/default-browser-emulator/lib/helpers/lookupPublicIp.ts
  111. +22 −10 plugins/default-browser-emulator/lib/helpers/selectUserAgentOption.ts
  112. +9 −8 plugins/default-browser-emulator/package.json
  113. +31 −0 plugins/default-browser-emulator/test/publicIp.test.ts
  114. +29 −0 plugins/default-browser-emulator/test/selectUserAgentOptions.test.ts
  115. +70 −0 plugins/default-human-emulator/CHANGELOG.md
  116. +6 −1 plugins/default-human-emulator/index.ts
  117. +4 −4 plugins/default-human-emulator/package.json
  118. +70 −0 plugins/execute-js/CHANGELOG.md
  119. +2 −1 plugins/execute-js/index.ts
  120. +26 −15 plugins/execute-js/lib/ClientPlugin.ts
  121. +7 −11 plugins/execute-js/lib/CorePlugin.ts
  122. +6 −0 plugins/execute-js/lib/IExecuteJsArgs.ts
  123. +8 −8 plugins/execute-js/package.json
  124. +58 −2 plugins/execute-js/test/basic.test.ts
  125. +86 −0 puppet-chrome/CHANGELOG.md
  126. +7 −3 puppet-chrome/lib/Browser.ts
  127. +44 −50 puppet-chrome/lib/BrowserContext.ts
  128. +149 −102 puppet-chrome/lib/Frame.ts
  129. +50 −22 puppet-chrome/lib/FramesManager.ts
  130. +68 −0 puppet-chrome/lib/NavigationLoader.ts
  131. +44 −36 puppet-chrome/lib/NetworkManager.ts
  132. +22 −18 puppet-chrome/lib/Page.ts
  133. +9 −10 puppet-chrome/lib/Worker.ts
  134. +3 −3 puppet-chrome/package.json
  135. +85 −0 puppet/CHANGELOG.md
  136. +56 −31 puppet/index.ts
  137. +164 −0 puppet/lib/BrowserProcess.ts
  138. +31 −34 puppet/lib/PipeTransport.ts
  139. +0 −151 puppet/lib/launchProcess.ts
  140. +8 −8 puppet/package.json
  141. +3 −1 puppet/test/BrowserContext.test.ts
  142. +9 −4 puppet/test/Frames.test.ts
  143. +8 −8 puppet/test/Page.navigate.test.ts
  144. +1 −1 puppet/test/Page.popups.test.ts
  145. +2 −2 puppet/test/Page.test.ts
  146. +12 −7 puppet/test/TestPage.ts
  147. +1 −1 puppet/test/_CustomBrowserEmulator.ts
  148. +2 −2 puppet/test/load.test.ts
  149. +1 −0 replay/.eslintrc.js
  150. +68 −0 replay/CHANGELOG.md
  151. +6 −5 replay/backend/api/ReplayResources.ts
  152. +1 −0 replay/frontend/.eslintrc.js
  153. +68 −0 replay/frontend/CHANGELOG.md
  154. +3 −3 replay/frontend/package.json
  155. +1 −1 replay/package.dist.json
  156. +6 −6 replay/package.json
  157. +64 −0 testing/CHANGELOG.md
  158. +4 −4 testing/package.json
  159. +86 −0 website/CHANGELOG.md
  160. +15 −2 website/docs/BasicInterfaces/Agent.md
  161. +7 −1 website/docs/BasicInterfaces/FrameEnvironment.md
  162. +4 −0 website/docs/BasicInterfaces/Handler.md
  163. +1 −1 website/docs/BasicInterfaces/Tab.md
  164. +9 −9 website/package.json
  165. +40 −23 website/src/pages/Index.vue
  166. +4,560 −4,323 yarn.lock
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -100,6 +100,7 @@ module.exports = {
'no-console': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'import/no-extraneous-dependencies': 'off',
'@typescript-eslint/no-shadow': 'off',
},
},
{
3 changes: 3 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# These are supported funding model platforms

github: ulixee
17 changes: 17 additions & 0 deletions .github/workflows/new-version.yml
Original file line number Diff line number Diff line change
@@ -44,6 +44,23 @@ jobs:
tag_name="${GITHUB_REF##*/}"
hub release edit "${assets[@]}" -m "" "$tag_name"
website:
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v1

- name: Install Node.js, NPM and Yarn
uses: actions/setup-node@v1
with:
node-version: 14

- name: Yarn install
run: yarn install --network-timeout 1000000
env:
SA_SKIP_DOWNLOAD: true

- name: Build Webite
run: yarn build
working-directory: ./website
136 changes: 136 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,142 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.6.5](https://github.com/ulixee/secret-agent/compare/v1.6.4...v1.6.5) (2022-05-25)


### Bug Fixes

* **cookies:** sub domain cookies not saving ([712808d](https://github.com/ulixee/secret-agent/commit/712808d745ab9057ed0ad66edb4e249de63b276f))
* **core:** file picker not working after profile ([4f7c737](https://github.com/ulixee/secret-agent/commit/4f7c737a50dc1b5683bdf551c27fea8368f26f31))
* **core:** speed for many user profile sites ([54a4061](https://github.com/ulixee/secret-agent/commit/54a406141ff851875d14e3ae342e00f76d240b1e))
* dependencies ([65cae71](https://github.com/ulixee/secret-agent/commit/65cae71af84e00d1dca4a74ac45318af43641a1c))
* github no longer allowing git prefix ([9693c89](https://github.com/ulixee/secret-agent/commit/9693c89267eeaeb48f98f91a363658230010210b))
* lint issues ([db3f353](https://github.com/ulixee/secret-agent/commit/db3f3535636707ed2fd9c7f5dcc29f7d4bd07cc7))





## [1.6.4](https://github.com/ulixee/secret-agent/compare/v1.6.3...v1.6.4) (2022-01-24)


### Bug Fixes

* **mitm:** lookup public ip should use https ([5a68228](https://github.com/ulixee/secret-agent/commit/5a6822851a71aff940900ea4f41365c997314ee2))





## [1.6.3](https://github.com/ulixee/secret-agent/compare/v1.6.2...v1.6.3) (2022-01-14)


### Bug Fixes

* **client:** wait for all command queue flushes ([95664f5](https://github.com/ulixee/secret-agent/commit/95664f570b6b7e233ea6b6227ad5f363cce52287))
* **core:** cleanup event listener memory ([6f7d7bb](https://github.com/ulixee/secret-agent/commit/6f7d7bb449714876ab9a90ec4697c012d8c7bff3))
* **core:** in page nav not resolving resource ([2539d78](https://github.com/ulixee/secret-agent/commit/2539d7809d0709d709cff9bba48f411361e8ebc9))





## [1.6.2](https://github.com/ulixee/secret-agent/compare/v1.6.1...v1.6.2) (2021-12-20)


### Bug Fixes

* **client:** reload should return a resource ([83b80b5](https://github.com/ulixee/secret-agent/commit/83b80b5b3d70bc06344fb64d31fed1434ac98114)), closes [#393](https://github.com/ulixee/secret-agent/issues/393)
* **core:** navigations directly to hash fixed ([c02f61b](https://github.com/ulixee/secret-agent/commit/c02f61b549d34c438614ab8f63c8fce17af60d2c)), closes [#404](https://github.com/ulixee/secret-agent/issues/404)


### Features

* **client:** return resource in waitForLocation ([253b33b](https://github.com/ulixee/secret-agent/commit/253b33b3528952432528949ab0dc2c8b4cf9a50c))
* **core:** dialogs should run out of command line ([52a737f](https://github.com/ulixee/secret-agent/commit/52a737ff7ca380b00a1f156aa7dacf5e9af0bcfb)), closes [#397](https://github.com/ulixee/secret-agent/issues/397)





## [1.6.1](https://github.com/ulixee/secret-agent/compare/v1.5.15...v1.6.1) (2021-12-14)


### Bug Fixes

* **core:** frame navigations on redirects ([de3ea24](https://github.com/ulixee/secret-agent/commit/de3ea248a0fda5b8d08c7de961bac4f9fdc0b83e))
* **core:** handle detached when elements not there ([5b46bd9](https://github.com/ulixee/secret-agent/commit/5b46bd97cf1ca1aa3bc0fed625064764726872f4))
* **puppet:** extract navigation loader ([77d8e2e](https://github.com/ulixee/secret-agent/commit/77d8e2e34286469a2df1d9e00e631c6e773760eb))
* **replay:** fix ability to replay data attributes ([9440995](https://github.com/ulixee/secret-agent/commit/944099560870c8479f0be59426fa194daf17ab4d))


### Features

* **plugins:** add frames to execute js ([65258a3](https://github.com/ulixee/secret-agent/commit/65258a3fa6ad23f7263bd0f90d86bebf751d8e94))





# [1.6.0](https://github.com/ulixee/secret-agent/compare/v1.5.15...v1.6.0) (2021-12-14)


### Bug Fixes

* **core:** handle detached when elements not there ([5b46bd9](https://github.com/ulixee/secret-agent/commit/5b46bd97cf1ca1aa3bc0fed625064764726872f4))
* **puppet:** extract navigation loader ([77d8e2e](https://github.com/ulixee/secret-agent/commit/77d8e2e34286469a2df1d9e00e631c6e773760eb))
* **replay:** fix ability to replay data attributes ([9440995](https://github.com/ulixee/secret-agent/commit/944099560870c8479f0be59426fa194daf17ab4d))


### Features

* **plugins:** add frames to execute js ([65258a3](https://github.com/ulixee/secret-agent/commit/65258a3fa6ad23f7263bd0f90d86bebf751d8e94))





## [1.5.15](https://github.com/ulixee/secret-agent/compare/v1.5.14...v1.5.15) (2021-11-24)


### Bug Fixes

* **client:** handle ws closing during shutdown ([09bfe29](https://github.com/ulixee/secret-agent/commit/09bfe29f54a4307fe0df1370a35a87d7950e9378))
* **mitm:** clean invalid response characters ([5d85921](https://github.com/ulixee/secret-agent/commit/5d859217d7cf6aa870058e4234a079c0027e35b3))
* **plugins:** improve user agent selector ([cd708bb](https://github.com/ulixee/secret-agent/commit/cd708bbc8e46705376f9cf3bd438a59c0063b6e7))
* **puppet:** better handling of default loader ([2979ba7](https://github.com/ulixee/secret-agent/commit/2979ba7b323aab6896f9c2f006ca4bbbe40e69cb))
* **puppet:** consisten timeout handling ([ba6bcee](https://github.com/ulixee/secret-agent/commit/ba6bcee3384811824ae3dc3789c5c3fc6088905a))
* **puppet:** don't use contexts we didn't initiate ([710acab](https://github.com/ulixee/secret-agent/commit/710acabd1b4c0275d95048cb9f0053e2775f4d77))
* **puppet:** fix flakiness on frame stopped ([002a516](https://github.com/ulixee/secret-agent/commit/002a51601123565b774f3589ddd9e35494516035))
* **puppet:** fix shutdown issues (port from hero) ([2b70752](https://github.com/ulixee/secret-agent/commit/2b70752054edae95701243cc59afa48e113856fe))


### Features

* **plugins:** mask public ip in webrtc ([14d3c67](https://github.com/ulixee/secret-agent/commit/14d3c673327c149084ae32cd71c944cf60a84df3))





## [1.5.14](https://github.com/ulixee/secret-agent/compare/v1.5.13...v1.5.14) (2021-11-15)


### Bug Fixes

* **core:** don't lose old user-profile storage ([9fe9ae4](https://github.com/ulixee/secret-agent/commit/9fe9ae4d6827a93f6b681dc10455d4695369b37f)), closes [#342](https://github.com/ulixee/secret-agent/issues/342)
* **human-emulator:** fix updated traget point ([98eabd1](https://github.com/ulixee/secret-agent/commit/98eabd183660fd2cbbc44575f2703ae86e5e2950))
* **puppet:** fix stack trace for protocol error ([8a5b4bf](https://github.com/ulixee/secret-agent/commit/8a5b4bf16b53268509b0ad93d3db74559f6de699))


### Features

* **mitm:** add documentUrl to resources ([7fcbf8f](https://github.com/ulixee/secret-agent/commit/7fcbf8faf84a72360d490f14c72910ea54619dc6))







**Note:** Version bump only for package @secret-agent/monorepo
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## SecretAgent 2.0 === Hero

> :bell: SecretAgent 2.0 is named "[Hero](https://github.com/ulixee/hero)" and currently in alpha testing. We're ready for developers to begin to switch over - it's a pretty easy transition ([migration guide](https://ulixee.org/docs/hero/help/migration-guide)). Follow along with development of Hero [here](https://github.com/ulixee/hero) or check out the latest npm packages `@ulixee/hero-fullstack`.
---

# SecretAgent

SecretAgent is a web browser that's built for scraping.
SecretAgent is a web browser that's built for scraping.

- [x] **Built for scraping** - it's the first modern headless browsers designed specifically for scraping instead of just automated testing.
- [x] **Designed for web developers** - We've recreated a fully compliant DOM directly in NodeJS allowing you bypass the headaches of previous scraper tools.
99 changes: 99 additions & 0 deletions client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,105 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [1.6.5](https://github.com/ulixee/secret-agent/compare/v1.6.4...v1.6.5) (2022-05-25)

**Note:** Version bump only for package @secret-agent/client





## [1.6.4](https://github.com/ulixee/secret-agent/compare/v1.6.3...v1.6.4) (2022-01-24)

**Note:** Version bump only for package @secret-agent/client





## [1.6.3](https://github.com/ulixee/secret-agent/compare/v1.6.2...v1.6.3) (2022-01-14)


### Bug Fixes

* **client:** wait for all command queue flushes ([95664f5](https://github.com/ulixee/secret-agent/commit/95664f570b6b7e233ea6b6227ad5f363cce52287))
* **core:** cleanup event listener memory ([6f7d7bb](https://github.com/ulixee/secret-agent/commit/6f7d7bb449714876ab9a90ec4697c012d8c7bff3))





## [1.6.2](https://github.com/ulixee/secret-agent/compare/v1.6.1...v1.6.2) (2021-12-20)


### Bug Fixes

* **client:** reload should return a resource ([83b80b5](https://github.com/ulixee/secret-agent/commit/83b80b5b3d70bc06344fb64d31fed1434ac98114)), closes [#393](https://github.com/ulixee/secret-agent/issues/393)


### Features

* **client:** return resource in waitForLocation ([253b33b](https://github.com/ulixee/secret-agent/commit/253b33b3528952432528949ab0dc2c8b4cf9a50c))
* **core:** dialogs should run out of command line ([52a737f](https://github.com/ulixee/secret-agent/commit/52a737ff7ca380b00a1f156aa7dacf5e9af0bcfb)), closes [#397](https://github.com/ulixee/secret-agent/issues/397)





## [1.6.1](https://github.com/ulixee/secret-agent/compare/v1.5.15...v1.6.1) (2021-12-14)


### Bug Fixes

* **core:** handle detached when elements not there ([5b46bd9](https://github.com/ulixee/secret-agent/commit/5b46bd97cf1ca1aa3bc0fed625064764726872f4))


### Features

* **plugins:** add frames to execute js ([65258a3](https://github.com/ulixee/secret-agent/commit/65258a3fa6ad23f7263bd0f90d86bebf751d8e94))





# [1.6.0](https://github.com/ulixee/secret-agent/compare/v1.5.15...v1.6.0) (2021-12-14)


### Bug Fixes

* **core:** handle detached when elements not there ([5b46bd9](https://github.com/ulixee/secret-agent/commit/5b46bd97cf1ca1aa3bc0fed625064764726872f4))


### Features

* **plugins:** add frames to execute js ([65258a3](https://github.com/ulixee/secret-agent/commit/65258a3fa6ad23f7263bd0f90d86bebf751d8e94))





## [1.5.15](https://github.com/ulixee/secret-agent/compare/v1.5.14...v1.5.15) (2021-11-24)


### Bug Fixes

* **client:** handle ws closing during shutdown ([09bfe29](https://github.com/ulixee/secret-agent/commit/09bfe29f54a4307fe0df1370a35a87d7950e9378))





## [1.5.14](https://github.com/ulixee/secret-agent/compare/v1.5.13...v1.5.14) (2021-11-15)


### Features

* **mitm:** add documentUrl to resources ([7fcbf8f](https://github.com/ulixee/secret-agent/commit/7fcbf8faf84a72360d490f14c72910ea54619dc6))







**Note:** Version bump only for package @secret-agent/client
2 changes: 1 addition & 1 deletion client/connections/ConnectionFactory.ts
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ If you meant to connect to a remote host, include the "host" parameter for your
}
};

connection.connect().then(onError).catch(onError);
connection.connect(true).then(onError).catch(onError);

return connection;
}
35 changes: 32 additions & 3 deletions client/connections/ConnectionToCore.ts
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
public readonly hostOrError: Promise<string | Error>;
public options: IConnectionToCoreOptions;
public isDisconnecting = false;
public isConnectionTerminated = false;

protected resolvedHost: string;

@@ -45,6 +46,7 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{

private connectRequestId: string;
private disconnectRequestId: string;
private didAutoConnect = false;
private coreSessions: CoreSessions;
private readonly pendingRequestsById = new Map<string, IResolvablePromiseWithId>();
private lastId = 0;
@@ -81,14 +83,20 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
protected abstract createConnection(): Promise<Error | null>;
protected abstract destroyConnection(): Promise<any>;

public async connect(): Promise<Error | null> {
public async connect(isAutoConnect = false): Promise<Error | null> {
if (!this.connectPromise) {
this.didAutoConnect = isAutoConnect;
this.connectPromise = new Resolvable();
try {
const startTime = new Date();
const connectError = await this.createConnection();
if (connectError) throw connectError;
if (this.isDisconnecting) throw new DisconnectedFromCoreError(this.resolvedHost);
if (this.isDisconnecting) {
if (this.coreSessions.size > 0 && !this.didAutoConnect) {
throw new DisconnectedFromCoreError(this.resolvedHost);
}
this.connectPromise.resolve();
}
// can be resolved if canceled by a disconnect
if (this.connectPromise.isResolved) return;

@@ -227,10 +235,12 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
sessionId: null,
});

const hasSessions = this.coreSessions?.size > 0;

this.cancelPendingRequests();

if (this.connectPromise) {
if (!this.connectPromise.isResolved) {
if (!this.connectPromise.isResolved && hasSessions && !this.didAutoConnect) {
this.connectPromise.resolve(new DisconnectedFromCoreError(this.resolvedHost));
} else if (beforeClose) {
await beforeClose();
@@ -287,11 +297,30 @@ export default abstract class ConnectionToCore extends TypedEventEmitter<{
session?.onEvent(meta, listenerId, eventArgs);
}

protected async onConnectionTerminated(): Promise<void> {
if (this.isConnectionTerminated) return;

this.isConnectionTerminated = true;
await this.internalDisconnect();
if (this.connectRequestId) {
this.onResponse(this.connectRequestId, {
data: this.didAutoConnect ? new DisconnectedFromCoreError(this.resolvedHost) : null,
});
}
if (this.disconnectRequestId) {
this.onResponse(this.disconnectRequestId, {
data: null,
});
}
}

protected onResponse(id: string, message: ICoreResponsePayload): void {
const pending = this.pendingRequestsById.get(id);
if (!pending) return;
this.pendingRequestsById.delete(id);
const isInternalRequest = this.connectRequestId === id || this.disconnectRequestId === id;
if (this.disconnectRequestId === id) this.disconnectRequestId = null;
if (this.connectRequestId === id) this.connectRequestId = null;

if (message.data instanceof Error) {
let responseError = message.data;
Loading