From 5615f936681d6c4269700642d9ec6540581d84b5 Mon Sep 17 00:00:00 2001 From: "Liangying.Wei" Date: Thu, 20 Sep 2018 17:57:26 +0800 Subject: [PATCH 1/8] Merge dev to master for releasing 1.0.0 (#211) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * New protocol used for SDK and Service (#11) * New protocol used for SDK and Service This PR is a subset of PR https://github.com/Azure/azure-signalr/pull/10. It is just for code review purpose. * update the dictionary if the same key existed Add timestamp for metric requirement * refactor code according to review comments * refactor the protocol per review comments * Service protocol for making refactoring version work (#16) The service protocol still needs to be refactoring in next step. * New SDK API and architecture refactor (#17) * SDK new API and architecture refactoring This commit includes: 1. architecture refactoring (all in HubHost folder, and has been reviewed) 2. API refactor: HubProxy and ServiceProvider folders 3. negotiation response implementation: HubHostBuilder.cs 4. ChatSample refactor: refers to relative new signalr.js, using new API * rollback the change of VS version * Fix most issues accroding to review comments * remove unnecessary Task returun * handle the connection completion * gracefully complete the connection pipes when client drops * Use the SignalR of Apr 18 to make client negotiate work The client javascripts are copied from SignalR with CI: commit f36313e58519dec240b3f327489995ffcdcb4056 Merge: 9a0c190f e9937ffb Author: BrennanConroy Date: Wed Apr 18 17:45:08 2018 -0700 * simplify the logic to start HubConnection * Try to gracefully close of client connection * gracefully close pipe * correct the logic of WaitOnTask * Update the REST API 1. SignalRServiceContext is the interface to use. 2. Hub does not make sense in REST API under severless mode. 3. In order to communicate with SignalR service, I need to create my own service provider to access SignalR protocol. * Remove 'SignalR' prefix * explain why we need all protocols * fix a compilation issue * New service protocol(#21) * Refine REST API and ServiceOptions (#22) * Refine REST API and ServiceOptions 1. AddAzureSignalR will register a default ServiceOptions 2. Fix an issue of CreateServiceContext 3. Rename the classes 4. Using option to get connection string in ChatSample * refine the error message * API refactor 1. For AddAzureSignalR(), we read connection string by ServiceOptionSetup which looks up connection string from Configuration. 2. Add a standalone console application to demonstrate Rest API usage. * Using AddAzureSignalR(connectionString) in CloudSignalR * remove unnecessary dependency on MessagePack * refine according to review comments * use SendAsync overload directly * Use new service protocol (#24) * Make the OWIN project compile (#30) * Made some perf changes and sample changes (#31) - Use ReadOnlyMemory in ConnectionDataMessage - Use IReadOnlyList instead of string[] in protocol messages. This lets us remove ToArray() everywhere. - Don't copy the buffer twice when writing it from the application pipe to the service connection. Just write the memory as is. - Updated the sample to use the default connection string config key. * Use all claims by default (#32) - If no delegate was specfied then use all of the current user's claims. - Renamed Claims to ClaimsProvider * Fixed sample from merge * Use the default WebHostBuilder (#33) * Upgrade to the latest SignalR rc1 build (#34) - Some things were made internal - Added a test project and a fake test to make sure we can run more. * Slight refactoring and renaming (#35) - Remove ConnectionProtocol and ServiceResponse - Change protocol to types to use ReadOnlyMemory instead of byte[] - Rename methods (WriteAsync and StartAsync) * Fixed null ref * Send directly to the connection if we have a mapping (#36) - Sending directly to a connection shorts writes to the hub connection context if it can. * Improve error message and delete connection string from config. (#38) * Improve error message and delete connection string from config. * initial clean up of Rest API code (#39) * Update to the latest SignalR and ASP.NET Core dependencies (#41) * Dispose the HttpConnection if StartAsync fails (#40) - This shouldn't matter as much but it's more correct to dispose the httpConnection if StartAsync fails. See https://github.com/aspnet/SignalR/issues/2134. - The reason it shouldn't matter is because we both skip negotiation and are using websockets which means we shouldn't ever allocate an HttpClient. * Send server Id to service (#42) * Send server's user Id to Service when connecion * make sure we only generate single userId for all connections * use machine name + GUID as server Id It is friendly for diagnostic. * reconnection implementation (#26) * implement reconnection implement reconnection refactor the loop refine per review comments * Do not clean client connection contexts unless service asks to Some cleaning up to align with async/await single loop pattern * add "CleanupConnections" back * rename * remove _connected and refactor AbortOnClose * add the missing lock check * safely stop the connection in timer's callback * remove unnecessary null check * remove the locker helper * Fix issue and polish code * small fixes * Renames and cleanup (#43) - Moved public APIs to the root - Renamed classes to add the Service* prefix * provide default value for connection count (#44) * Remove CloseTimeout (#45) - We don't want to wait 300 seconds for the other side to close the websocket. * Use the latest javascript client (#47) * Don't pass the ConnectionDelegate in StartAsync (#46) - Make it part of the ServiceConnection construction * Add a heartbeat (#49) * Add a heartbeat - Implement the required features to support sending keep alives * Handle shutdown properly (#51) - Don't close the application input, we already complete it when the transport stops writing - Handled exceptions leaking from the application task - Added comments and fixed method names * Make AccessTokenLifetime configurable (#52) - Default to 1 hour instead of 30 seconds - Added error message when connection string fails to parse * refine log (#53) * refine logs close the issue https://github.com/Azure/azure-signalr/issues/27 * Enable user secrets (#58) * set connectionID for HttpConnection (#59) generate connection ID for each service connection, and pass "cid=connectionId" through query string to service. * build scripts (#61) add scripts for build BuildTools is applied for our build. The same as SignalR used for build. Give the developers consistent building experience. All scripts come from: https://github.com/aspnet/BuildTools/tree/dev/scripts/bootstrapper It is very easy to build on both Windows and Linux: build.cmd or build.sh They will install the required dotnet in your system. It will try to find the *.sln to build and run test defaultly. * Quit the loop if the connection closed (#63) * Some updates to korebuild usage (#64) - Pin korebuild to the release 2.1 branch - Check in a korebuild-lock.txt which will pin the resolved version - Make the .sh files executable with chmod * Delete OWIN project (#65) * Remove OWIN project from solution * Add handshake with service (#67) * Always read connection string defaults from config (#70) * Always read connection string defaults from config - Added tests * Remove REST Api support in SDK (#71) * Remove dependencies and delete the REST sample (#72) * Code clean up (#73) - Remove small async methods that are called once - Remove extra async state machines * upgrade to 1.0.0-rc1-30677 (#74) * Add service protocol test case & add doc comments (#75) * add service protocol test case * add doc comments * Refine connection string parsing (#78) * update package name to 1.0.0-preview1-* (#80) * Add swagger file and update README (#81) * Update dependencies to rc1-final (#82) * Enable travis ci build * Add build status to README.md * Point package links to Nuget (#86) * Fix format in README (#88) * Add missing XML doc (#92) * Fixed auth and tests (#90) - Made it so tests don't depend on timeouts - Fixed the auth bug and added unit tests * Update external files and remove unused (#93) * Make AuthenticationHelper internal (#94) * Make AuthenticationHelper internal * Added timeout to tests to make sure things don't hang if there's a bug (#95) * Added timeout to tests to make sure things don't hang if there's a bug * Fix races in test * Added more ServiceConnection tests (#96) * Support multiple hubs (#98) * WIP * Removed Hub * Updated to netcoreapp2.1 added another hub to sample * fix issue #99 (#100) Korebuild's config for dotnet SDK needs to be updated. Otherwise, it reports exception: "Could not load file or assembly 'System.Memory, Version=4.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'" dotnet 2.1.300-rc1-008673 works fine. But lower version has this issue. * Add signing project (#103) * Add signing proj * Add Microbuild Reference * Add parameters into dependencies * Fix MultiUserDataMessage type (#105) * Use condition PackageReference in MicroBuild (#106) * send PingMessage to service to keep alive (#102) * Add package restore tests (#111) * Add restore test * More parameterize * Update version * filter claims in OpenConnectionMessage for unauthenticated user (#107) * update SignalR dependencies to 1.0.0 (#114) * Change Microbuild PackageReference to a separate proj (#116) * change sign dependency * Update * add required metadata for nuget package (#117) * add API descriptions in swagger (#128) * Write the ReadOnlySequence as a single ConnectionDataMessage (#130) - Manually write the message pack header and ReadOnlySequence to the Stream directly. - Added IClientConnectionFactory to make it possible to override creation of client connections. - Added test for writing multi segment message through the ServiceConnection. * load connection string from ConnectionStrings section if default key not exist (#129) * Add headers and query string in OpenConnectionMessage (#131) * Throw more friendly exception when calling AddAzureSignalR without UseAzureSignalR (#136) * Show friendly message if not call UseAzureSignalR * Add blank line * Change parameter name and add more check so that exception message can be more friendly. * Add test case * Push private package to myget (#137) * Update travis to push package * update travis * update travis * Fix dotnet not found (#138) * update travis * Fix wrong options in dotnet nuget push (#139) * Add Test cases for EndpointUtilty, LifetimeManager and ServiceConnection (#133) * Add EndpointUtilty Test * Add tests for lifetimeManager * Add LifetimeManager IT * Format the codes * Format codes * Remove mock and use TestClass instead. * Add a few reconnection test * Add message fieds verification * Add reconnect test * Update reconnection test * Update reconnection when connection throws exception * Update reconnection logic * Format codes * Add keepalive reconnect * Fix typo * fix merge conflict * clean up test infrastructure * PR comments * fix typo * Add Endpoint validation (#142) * Add endpoint validation * Fix typo * Update error message and add tests * Avoid connection timeout when debugger is attached (#143) For ServiceConnection Handshake, for better debugging experience * Update xunit related test package version (#146) to latest * Add custom UserAgent for general telemetry tracking (#148) Add custom UserAgent for general telemetry tracking * send group message from a fixed service message (#147) * Extract a base connection class to for service connection and utiliti… (#155) * Extract a base connection class to for service connection and utilities (#153) * Extract a base connection class to for basic service connection actions, such as keepalive, handshake, and process incoming messages to a Common csproj * Change all class to internal as it is a SDK project * support version in connection string. (#154) * rename IServiceEndpointUtility to IServiceEndpointProvider. (#157) * Add Constants to Common project (#164) * Shorten AzureSignalRPrefix in claimType, as longer one will be result… (#165) * Shorten AzureSignalRPrefix in claimType, as longer one will be resulted in longer url * Loop over the dictionary itself rather than keys (#168) > Performance tip: It's more efficient to loop over the ConcurrentDictionary itself rather than keys or values. Each of those collections takes a lock on all of the buckets. * Send UpdateConnectionMessage when there is custom IUserIdProvider (#163) * Revert "Send UpdateConnectionMessage when there is custom IUserIdProvider (#163)" (#171) This reverts commit 52cfc85012360fd1aa36501da490dfc184df5a80. * Use IUserIdProvider to generate user id and include it in access token (#170) * Change partitioning hashset algorithm (#177) * Refactor ServiceEndpointProvider (#176) * Include CustomUserId claim as internal claims (#178) * Check system claims in a case-sensitive way (#180) * Move ConnectionStringParser and IServiceEndpointGenerator to Common p… (#181) * Move ConnectionStringParser and IServiceEndpointGenerator to Common project * Move tests too * Preserve original request path of client connection (#179) * Include original query string in negotiate response (#184) * Add missing copyright (#185) * Minor refine of README (#188) * Fix a VSTS restore test bug (#201) * change suffix to rtm * fix restore bugs * change back to preview1 * Add swagger and documentation for V1 REST API (#195) * rename custom header name to avoid header restriction in asp.net framework (#200) * rename custom header name to avoid header restriction in asp.net * add comments for the fix. * header name convention * Add client IP address in Context.HttpContext (#207) * Merge AspNet related changes to dev branch (#209) * Add Microsoft.Azure.SignalR.AspNet.csproj and SDK skeleton (#144) * Add Microsoft.Azure.AspNet.SignalR.csproj and SDK skeleton * Remove unneccessory package reference * Rename to Microsoft.Azure.AspNet.SignalR Implement RunAzureSignalR in the API, add necessory class skeleton Resolve comments, and make ServiceOptions configurable Add test project and RunAzureSignalR test fix format: using System directives should be first Add 2 to-dos Reference Common project update interface (#158) Implement ServiceConnection for Azure AspNet Service (#159) * Implement ServiceConnection for Azure AspNet Service, dispatcher should be per connection per instance, and add a TCS when service connection starts Implement ConnectionFactory, and add a container for partitional writ… (#175) * Implement ConnectionFactory, and add a container for partitional write message * Use a more effective way to json serialize, and remove AddConnection from manager * return 400 for JSONP [ASP.NET SignalR]Implement ServiceEndpointPrvoider for ASP.NET SignalR SDK (#182) * Implement ServiceEndpointPrvoider for ASP.NET SignalR SDK Update the negotiate response (#186) 1. `ProtocolVersion` up to 2.0 2. Use `RedirectUrl` instead of already in-use `Url` try to support /reconnect (#187) * try to support /reconnect Add dependencies from Common project to here (#193) Although `PrivateAssets` and set workarounds as https://github.com/nuget/home/issues/3891#issuecomment-397481361 shows, it is unable to detect what Common project references and add it to the references of current project. So for now, we still need to make sure the references of Common project is added to this project's references, until https://github.com/nuget/home/issues/3891 is resolved. Add application name to the public API (#190) * Add application name to the public API, and add comments, add an app connection to the service connection Fix userid can be null (#204) Make the one from web.config as the default connection string and fix build failure (#206) * Fix build failure * Make the one from web.config as the default connection string * Add appsettings support [ASP.NET SignalR]Implement ServiceMessageBus and ServiceConnectionManager (#152) * Implement ServiceMessageBus and ConnectionManager * disable net461 tests and samples in travis and add appveyor Skip test related reference * Add appveyor build status * Update SignalR SDK version * Update sources.props * fix claim in negotiate handler. (#205) --- .gitignore | 5 +- .travis.yml | 16 + AzureSignalR.sln | 59 +- Directory.Build.props | 8 + README.md | 45 +- appveyor.yml | 4 + build.cmd | 2 + build.sh | 8 + build/dependencies.props | 51 +- build/microbuild.proj | 8 + build/package-test.proj | 11 + build/sign.proj | 37 + build/sources.props | 13 +- docs/api-reference.md | 266 - docs/images/serverless-arch.png | Bin 0 -> 19334 bytes docs/rest-api.md | 145 + docs/swagger/v1-preview.json | 221 + docs/swagger/v1.json | 217 + korebuild-lock.txt | 2 + korebuild.json | 4 + pack.cmd | 20 - publish.cmd | 4 +- run.cmd | 2 + run.ps1 | 208 + run.sh | 245 + samples/ChatSample/ChatSample.csproj | 18 +- .../Controllers/SignalRController.cs | 38 - samples/ChatSample/Hub/Chat.cs | 3 +- samples/ChatSample/Hub/NotificationHub.cs | 9 + samples/ChatSample/Program.cs | 21 +- .../ChatSample/Properties/launchSettings.json | 27 + samples/ChatSample/Startup.cs | 35 +- samples/ChatSample/TimeService.cs | 38 - .../ChatSample/appsettings.Development.json | 15 - samples/ChatSample/appsettings.json | 17 +- samples/ChatSample/package-lock.json | 92 + samples/ChatSample/package.json | 8 + samples/ChatSample/wwwroot/index.html | 71 +- .../ChatSample/wwwroot/scripts/msgpack5.js | 7566 +++++++++++++++++ .../wwwroot/scripts/msgpack5.min.js | 1 + .../scripts/signalr-protocol-msgpack.js | 2443 ++++++ .../scripts/signalr-protocol-msgpack.js.map | 1 + .../scripts/signalr-protocol-msgpack.min.js | 16 + .../signalr-protocol-msgpack.min.js.map | 1 + samples/ChatSample/wwwroot/scripts/signalr.js | 3425 +++++--- .../ChatSample/wwwroot/scripts/signalr.js.map | 1 + .../ChatSample/wwwroot/scripts/signalr.min.js | 11 +- .../wwwroot/scripts/signalr.min.js.map | 1 + specs/ServiceProtocol.md | 242 + src/Common/BinaryMessageFormatter.cs | 54 + src/Common/BinaryMessageParser.cs | 86 + src/Common/MemoryBufferWriter.cs | 341 + src/Directory.Build.props | 7 +- .../AspNetConstants.cs | 19 + .../IServiceEndpointProvider.cs | 20 + .../ServiceEndpointProvider.cs | 87 + .../Hubs/ServiceHubDispatcher.cs | 118 + .../Infrastructure/AppMessage.cs | 21 + .../Infrastructure/ClientConnectionManager.cs | 136 + .../Infrastructure/ConnectionFactory.cs | 125 + .../Infrastructure/EmptyProtectedData.cs | 24 + .../Infrastructure/HubMessage.cs | 18 + .../IClientConnectionManager.cs | 12 + .../Infrastructure/IConnectionFactory.cs | 18 + .../Infrastructure/IMessageParser.cs | 13 + .../IServiceConnectionContainer.cs | 13 + .../IServiceConnectionManager.cs | 15 + .../Infrastructure/ServiceConnection.Log.cs | 231 + .../Infrastructure/ServiceConnection.cs | 155 + .../ServiceConnectionContainer.cs | 65 + .../ServiceConnectionExtensions.cs | 27 + .../ServiceConnectionManager.cs | 128 + .../Infrastructure/ServiceMessageBus.cs | 82 + .../Infrastructure/SignalRMessageParser.cs | 224 + .../Internals/ListHelper.cs | 16 + .../Internals/MemoryPoolTextWriter.cs | 197 + .../Internals/PrefixHelper.cs | 99 + .../Microsoft.Azure.SignalR.AspNet.csproj | 45 + .../OwinExtensions.cs | 212 + .../Properties/AssemblyInfo.cs | 4 + .../ServiceOptions.cs | 44 + .../TraceManagerLoggerProvider.cs | 29 + .../Transports/AzureTransport.cs | 86 + .../Transports/AzureTransportManager.cs | 30 + .../ConnectionStringParser.cs | 99 + .../Constants.cs | 33 + .../DuplexPipe.cs | 42 + .../IServiceConnection.cs | 17 + .../IServiceEndpointGenerator.cs | 13 + .../Microsoft.Azure.SignalR.Common.csproj | 16 + .../Properties/AssemblyInfo.cs | 7 + .../ServiceConnectionBase.cs | 648 ++ .../StaticRandom.cs | 0 .../TimerAwaitable.cs | 109 + .../AppBuilderExtensions.cs | 22 - .../Microsoft.Azure.SignalR.Owin.csproj | 15 - .../ConnectionMessage.cs | 133 + .../GroupMessage.cs | 59 + .../IServiceProtocol.cs | 41 + .../Microsoft.Azure.SignalR.Protocols.csproj | 21 + .../MulticastDataMessage.cs | 177 + .../ServiceMessage.cs | 70 + .../ServiceProtocol.cs | 695 ++ .../ServiceProtocolConstants.cs | 23 + .../ApplicationBuilderExtensions.cs | 33 +- src/Microsoft.Azure.SignalR/CloudSignalR.cs | 110 - .../DependencyInjectionExtensions.cs | 73 +- .../EndpointProvider.cs | 60 - .../DefaultServiceEndpointGenerator.cs | 66 + .../IServiceEndpointProvider.cs | 21 + .../PreviewServiceEndpointGenerator.cs | 58 + .../ServiceEndpointProvider.cs | 97 + .../HubHost/ClientConnectionFactory.cs | 15 + .../HubHost/ClientConnectionManager.cs | 27 + .../HubHost/CloudConnection.cs | 357 - .../HubHost/CloudHubConnectionContext.cs | 97 - .../HubHost/HeartBeat.cs | 57 + .../HubHost/HubHost.cs | 112 - .../HubHost/HubHostBuilder.cs | 32 - .../HubHost/HubHostDispatcher.cs | 458 - .../HubHost/HubHostLifetimeManager.cs | 239 - .../HubHost/HubHostOptions.cs | 28 - .../HubHost/IClientConnectionFactory.cs | 12 + .../HubHost/IClientConnectionManager.cs | 16 + .../HubHost/IConnectionFactory.cs | 18 + .../HubHost/IServiceConnectionManager.cs | 20 + .../HubHost/InvocationMessageBuilder.cs | 71 - .../HubHost/ServiceConnection.Log.cs | 223 + .../HubHost/ServiceConnection.cs | 240 + .../HubHost/ServiceConnectionContext.cs | 171 + .../HubHost/ServiceConnectionManager.cs | 48 + .../HubHost/ServiceHubDispatcher.cs | 127 + .../HubHost/ServiceLifetimeManager.cs | 214 + .../HubHost/ServiceOptionsSetup.cs | 37 + .../HubProxy/ClientProxy.cs | 56 - .../HubProxy/ClientProxyFactory.cs | 112 - .../HubProxy/GroupManagerProxy.cs | 86 - .../HubProxy/HubClientsProxy.cs | 92 - .../HubProxy/HubProxy.cs | 44 - .../HubProxy/HubProxyOptions.cs | 13 - .../HubProxy/IClientProxy.cs | 19 - .../Internal/AsyncEnumeratorAdapters.cs | 193 - .../Internal/AzureSignalRMarkerService.cs | 10 + .../Internal/ProductInfo.cs | 23 + .../Microsoft.Azure.SignalR.csproj | 30 +- .../NegotiateHandler.cs | 67 + .../Properties/AssemblyInfo.cs | 4 + .../ServiceHubConnectionContext.cs | 73 + src/Microsoft.Azure.SignalR/ServiceOptions.cs | 46 + .../ServiceRouteBuilder.cs | 99 + src/Microsoft.Azure.SignalR/TokenProvider.cs | 87 - .../Utilities/AuthenticationHelper.cs | 2 +- .../Utilities/AuthorizeHelper.cs | 70 + .../HubConnectionContextExtensions.cs | 17 - .../HubInvocationMessageExtensions.cs | 140 - .../Utilities/StringHelper.cs | 15 - .../ClientConnectionManagerTests.cs | 80 + .../Infrastructure/LogRecord.cs | 21 + .../Infrastructure/LogSinkProvider.cs | 77 + .../Infrastructure/ServiceConnectionProxy.cs | 186 + .../Infrastructure/VerifiableLoggedTest.cs | 31 + .../Infrastructure/VerifyNoErrorsScope.cs | 65 + ...icrosoft.Azure.SignalR.AspNet.Tests.csproj | 34 + .../RunAzureSignalRTests.cs | 250 + .../ServiceConnectionTests.cs | 69 + .../ServiceEndpointProviderTests.cs | 103 + .../ServiceMessageBusTests.cs | 280 + .../SignalRMessageParserTest.cs | 298 + .../SignalRMessageUtility.cs | 105 + .../ConnectionStringParserFacts.cs | 85 + ...icrosoft.Azure.SignalR.Common.Tests.csproj | 20 + ...osoft.Azure.SignalR.Protocols.Tests.csproj | 26 + .../ServiceMessageEqualityComparer.cs | 244 + .../ServiceProtocolFacts.cs | 292 + .../AddAzureSignalRFacts.cs | 123 + .../AzureSignalRMarkerServiceFact.cs | 110 + .../Infrastructure/HandshakeUtils.cs | 75 + .../Infrastructure/PipeReaderExtensions.cs | 110 + .../Infrastructure/ServiceConnectionProxy.cs | 180 + .../Infrastructure/TaskExtensions.cs | 103 + .../Infrastructure/TestConnection.cs | 35 + .../Infrastructure/TestConnectionFactory.cs | 82 + .../Infrastructure/TestHub.cs | 11 + .../TestServiceConnectionManager.cs | 52 + .../Microsoft.Azure.SignalR.Tests.csproj | 24 + .../NegotiateHandlerFacts.cs | 151 + .../ProductInfoFacts.cs | 18 + .../ServiceConnectionFacts.cs | 382 + .../ServiceContextFacts.cs | 141 + .../ServiceEndpointProviderFacts.cs | 192 + .../ServiceLifetimeManagerFacts.cs | 209 + version.props | 2 +- 192 files changed, 25086 insertions(+), 4357 deletions(-) create mode 100644 .travis.yml create mode 100644 appveyor.yml create mode 100644 build.cmd create mode 100755 build.sh create mode 100644 build/microbuild.proj create mode 100644 build/package-test.proj create mode 100644 build/sign.proj delete mode 100644 docs/api-reference.md create mode 100644 docs/images/serverless-arch.png create mode 100644 docs/rest-api.md create mode 100644 docs/swagger/v1-preview.json create mode 100644 docs/swagger/v1.json create mode 100644 korebuild-lock.txt create mode 100644 korebuild.json delete mode 100644 pack.cmd create mode 100644 run.cmd create mode 100644 run.ps1 create mode 100755 run.sh delete mode 100644 samples/ChatSample/Controllers/SignalRController.cs create mode 100644 samples/ChatSample/Hub/NotificationHub.cs create mode 100644 samples/ChatSample/Properties/launchSettings.json delete mode 100644 samples/ChatSample/TimeService.cs delete mode 100644 samples/ChatSample/appsettings.Development.json create mode 100644 samples/ChatSample/package-lock.json create mode 100644 samples/ChatSample/package.json create mode 100644 samples/ChatSample/wwwroot/scripts/msgpack5.js create mode 100644 samples/ChatSample/wwwroot/scripts/msgpack5.min.js create mode 100644 samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js create mode 100644 samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js.map create mode 100644 samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js create mode 100644 samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js.map create mode 100644 samples/ChatSample/wwwroot/scripts/signalr.js.map create mode 100644 samples/ChatSample/wwwroot/scripts/signalr.min.js.map create mode 100644 specs/ServiceProtocol.md create mode 100644 src/Common/BinaryMessageFormatter.cs create mode 100644 src/Common/BinaryMessageParser.cs create mode 100644 src/Common/MemoryBufferWriter.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/AspNetConstants.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/IServiceEndpointProvider.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/ServiceEndpointProvider.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Hubs/ServiceHubDispatcher.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/AppMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ClientConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/EmptyProtectedData.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/HubMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IClientConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IConnectionFactory.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IMessageParser.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionContainer.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.Log.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionContainer.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionExtensions.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceMessageBus.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Infrastructure/SignalRMessageParser.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Internals/ListHelper.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Internals/MemoryPoolTextWriter.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Internals/PrefixHelper.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Microsoft.Azure.SignalR.AspNet.csproj create mode 100644 src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Properties/AssemblyInfo.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/TraceManagerLoggerProvider.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransport.cs create mode 100644 src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransportManager.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/ConnectionStringParser.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/Constants.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/DuplexPipe.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/IServiceConnection.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/IServiceEndpointGenerator.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/Microsoft.Azure.SignalR.Common.csproj create mode 100644 src/Microsoft.Azure.SignalR.Common/Properties/AssemblyInfo.cs create mode 100644 src/Microsoft.Azure.SignalR.Common/ServiceConnectionBase.cs rename src/{Microsoft.Azure.SignalR/Utilities => Microsoft.Azure.SignalR.Common}/StaticRandom.cs (100%) create mode 100644 src/Microsoft.Azure.SignalR.Common/TimerAwaitable.cs delete mode 100644 src/Microsoft.Azure.SignalR.Owin/AppBuilderExtensions.cs delete mode 100644 src/Microsoft.Azure.SignalR.Owin/Microsoft.Azure.SignalR.Owin.csproj create mode 100644 src/Microsoft.Azure.SignalR.Protocols/ConnectionMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/GroupMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/IServiceProtocol.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/Microsoft.Azure.SignalR.Protocols.csproj create mode 100644 src/Microsoft.Azure.SignalR.Protocols/MulticastDataMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs create mode 100644 src/Microsoft.Azure.SignalR.Protocols/ServiceProtocolConstants.cs delete mode 100644 src/Microsoft.Azure.SignalR/CloudSignalR.cs delete mode 100644 src/Microsoft.Azure.SignalR/EndpointProvider.cs create mode 100644 src/Microsoft.Azure.SignalR/EndpointProvider/DefaultServiceEndpointGenerator.cs create mode 100644 src/Microsoft.Azure.SignalR/EndpointProvider/IServiceEndpointProvider.cs create mode 100644 src/Microsoft.Azure.SignalR/EndpointProvider/PreviewServiceEndpointGenerator.cs create mode 100644 src/Microsoft.Azure.SignalR/EndpointProvider/ServiceEndpointProvider.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ClientConnectionFactory.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ClientConnectionManager.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/CloudConnection.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/CloudHubConnectionContext.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/HeartBeat.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/HubHost.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/HubHostBuilder.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/HubHostDispatcher.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/HubHostLifetimeManager.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/HubHostOptions.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/IClientConnectionFactory.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/IClientConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/IConnectionFactory.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/IServiceConnectionManager.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubHost/InvocationMessageBuilder.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.Log.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionContext.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionManager.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs create mode 100644 src/Microsoft.Azure.SignalR/HubHost/ServiceOptionsSetup.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/ClientProxy.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/ClientProxyFactory.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/GroupManagerProxy.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/HubClientsProxy.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/HubProxy.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/HubProxyOptions.cs delete mode 100644 src/Microsoft.Azure.SignalR/HubProxy/IClientProxy.cs delete mode 100644 src/Microsoft.Azure.SignalR/Internal/AsyncEnumeratorAdapters.cs create mode 100644 src/Microsoft.Azure.SignalR/Internal/AzureSignalRMarkerService.cs create mode 100644 src/Microsoft.Azure.SignalR/Internal/ProductInfo.cs create mode 100644 src/Microsoft.Azure.SignalR/NegotiateHandler.cs create mode 100644 src/Microsoft.Azure.SignalR/Properties/AssemblyInfo.cs create mode 100644 src/Microsoft.Azure.SignalR/ServiceHubConnectionContext.cs create mode 100644 src/Microsoft.Azure.SignalR/ServiceOptions.cs create mode 100644 src/Microsoft.Azure.SignalR/ServiceRouteBuilder.cs delete mode 100644 src/Microsoft.Azure.SignalR/TokenProvider.cs create mode 100644 src/Microsoft.Azure.SignalR/Utilities/AuthorizeHelper.cs delete mode 100644 src/Microsoft.Azure.SignalR/Utilities/HubConnectionContextExtensions.cs delete mode 100644 src/Microsoft.Azure.SignalR/Utilities/HubInvocationMessageExtensions.cs delete mode 100644 src/Microsoft.Azure.SignalR/Utilities/StringHelper.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/ClientConnectionManagerTests.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogRecord.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogSinkProvider.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/ServiceConnectionProxy.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifiableLoggedTest.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifyNoErrorsScope.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/Microsoft.Azure.SignalR.AspNet.Tests.csproj create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/RunAzureSignalRTests.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceConnectionTests.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceEndpointProviderTests.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceMessageBusTests.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageParserTest.cs create mode 100644 test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageUtility.cs create mode 100644 test/Microsoft.Azure.SignalR.Common.Tests/ConnectionStringParserFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Common.Tests/Microsoft.Azure.SignalR.Common.Tests.csproj create mode 100644 test/Microsoft.Azure.SignalR.Protocols.Tests/Microsoft.Azure.SignalR.Protocols.Tests.csproj create mode 100644 test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceMessageEqualityComparer.cs create mode 100644 test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceProtocolFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/AddAzureSignalRFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/AzureSignalRMarkerServiceFact.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/HandshakeUtils.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/PipeReaderExtensions.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/ServiceConnectionProxy.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/TaskExtensions.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnection.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnectionFactory.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestHub.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestServiceConnectionManager.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/Microsoft.Azure.SignalR.Tests.csproj create mode 100644 test/Microsoft.Azure.SignalR.Tests/NegotiateHandlerFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/ProductInfoFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/ServiceConnectionFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/ServiceContextFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/ServiceEndpointProviderFacts.cs create mode 100644 test/Microsoft.Azure.SignalR.Tests/ServiceLifetimeManagerFacts.cs diff --git a/.gitignore b/.gitignore index e15d56efc..bbe3b5df0 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,9 @@ bld/ # Uncomment if you have tasks that create the project's static files in wwwroot #wwwroot/ +# Visual Studio Code settings +.vscode/ + # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* @@ -46,7 +49,7 @@ dlldata.c project.lock.json project.fragment.lock.json artifacts/ -**/Properties/launchSettings.json +global.json *_i.c *_p.c diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..f1870bd27 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: csharp +branches: + only: + - dev +dist: trusty +mono: none +dotnet: 2.1.301 +os: +- linux +- osx +addons: + apt: + packages: + - libunwind8 +script: +- "./build.sh --ci /p:BuildNumber=$((10000+TRAVIS_BUILD_NUMBER))\\;DisableNet461Tests=true" diff --git a/AzureSignalR.sln b/AzureSignalR.sln index 45987bd9e..673faed92 100644 --- a/AzureSignalR.sln +++ b/AzureSignalR.sln @@ -1,3 +1,4 @@ + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27130.2020 @@ -11,8 +12,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{C4BC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR", "src\Microsoft.Azure.SignalR\Microsoft.Azure.SignalR.csproj", "{7B124E40-36AC-45CC-9467-F6A50D5FC149}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Owin", "src\Microsoft.Azure.SignalR.Owin\Microsoft.Azure.SignalR.Owin.csproj", "{7E2FD40B-18D4-4ADD-806A-3C6CED44F19D}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChatSample", "samples\ChatSample\ChatSample.csproj", "{B23C2F84-B0B8-47D1-A8D2-E524F6511215}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{36024D6C-A549-4269-B11B-4BFC5F08BD11}" @@ -27,6 +26,22 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution version.props = version.props EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Protocols", "src\Microsoft.Azure.SignalR.Protocols\Microsoft.Azure.SignalR.Protocols.csproj", "{2EBFCD26-6E31-4855-ABF5-0A82E78F87C7}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2429FBD8-1FCE-4C42-AA28-DF32F7249E77}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Tests", "test\Microsoft.Azure.SignalR.Tests\Microsoft.Azure.SignalR.Tests.csproj", "{764DCFF5-D28D-4CEF-B186-B7A62FBB3E98}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Protocols.Tests", "test\Microsoft.Azure.SignalR.Protocols.Tests\Microsoft.Azure.SignalR.Protocols.Tests.csproj", "{E2094C87-C0F2-471D-A399-0B78CC32C1AA}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Common", "src\Microsoft.Azure.SignalR.Common\Microsoft.Azure.SignalR.Common.csproj", "{FAB21D29-0CBF-4243-94AF-067EBFB15EAE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.Common.Tests", "test\Microsoft.Azure.SignalR.Common.Tests\Microsoft.Azure.SignalR.Common.Tests.csproj", "{367FD2C0-A505-4761-B229-AA3948D25F21}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.AspNet", "src\Microsoft.Azure.SignalR.AspNet\Microsoft.Azure.SignalR.AspNet.csproj", "{235BF797-E3A2-4EEB-8D82-0C0BE1380663}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Azure.SignalR.AspNet.Tests", "test\Microsoft.Azure.SignalR.AspNet.Tests\Microsoft.Azure.SignalR.AspNet.Tests.csproj", "{4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,22 +52,52 @@ Global {7B124E40-36AC-45CC-9467-F6A50D5FC149}.Debug|Any CPU.Build.0 = Debug|Any CPU {7B124E40-36AC-45CC-9467-F6A50D5FC149}.Release|Any CPU.ActiveCfg = Release|Any CPU {7B124E40-36AC-45CC-9467-F6A50D5FC149}.Release|Any CPU.Build.0 = Release|Any CPU - {7E2FD40B-18D4-4ADD-806A-3C6CED44F19D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {7E2FD40B-18D4-4ADD-806A-3C6CED44F19D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {7E2FD40B-18D4-4ADD-806A-3C6CED44F19D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {7E2FD40B-18D4-4ADD-806A-3C6CED44F19D}.Release|Any CPU.Build.0 = Release|Any CPU {B23C2F84-B0B8-47D1-A8D2-E524F6511215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {B23C2F84-B0B8-47D1-A8D2-E524F6511215}.Debug|Any CPU.Build.0 = Debug|Any CPU {B23C2F84-B0B8-47D1-A8D2-E524F6511215}.Release|Any CPU.ActiveCfg = Release|Any CPU {B23C2F84-B0B8-47D1-A8D2-E524F6511215}.Release|Any CPU.Build.0 = Release|Any CPU + {2EBFCD26-6E31-4855-ABF5-0A82E78F87C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2EBFCD26-6E31-4855-ABF5-0A82E78F87C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2EBFCD26-6E31-4855-ABF5-0A82E78F87C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2EBFCD26-6E31-4855-ABF5-0A82E78F87C7}.Release|Any CPU.Build.0 = Release|Any CPU + {764DCFF5-D28D-4CEF-B186-B7A62FBB3E98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {764DCFF5-D28D-4CEF-B186-B7A62FBB3E98}.Debug|Any CPU.Build.0 = Debug|Any CPU + {764DCFF5-D28D-4CEF-B186-B7A62FBB3E98}.Release|Any CPU.ActiveCfg = Release|Any CPU + {764DCFF5-D28D-4CEF-B186-B7A62FBB3E98}.Release|Any CPU.Build.0 = Release|Any CPU + {E2094C87-C0F2-471D-A399-0B78CC32C1AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E2094C87-C0F2-471D-A399-0B78CC32C1AA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E2094C87-C0F2-471D-A399-0B78CC32C1AA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E2094C87-C0F2-471D-A399-0B78CC32C1AA}.Release|Any CPU.Build.0 = Release|Any CPU + {FAB21D29-0CBF-4243-94AF-067EBFB15EAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAB21D29-0CBF-4243-94AF-067EBFB15EAE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAB21D29-0CBF-4243-94AF-067EBFB15EAE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAB21D29-0CBF-4243-94AF-067EBFB15EAE}.Release|Any CPU.Build.0 = Release|Any CPU + {367FD2C0-A505-4761-B229-AA3948D25F21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {367FD2C0-A505-4761-B229-AA3948D25F21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {367FD2C0-A505-4761-B229-AA3948D25F21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {367FD2C0-A505-4761-B229-AA3948D25F21}.Release|Any CPU.Build.0 = Release|Any CPU + {235BF797-E3A2-4EEB-8D82-0C0BE1380663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {235BF797-E3A2-4EEB-8D82-0C0BE1380663}.Debug|Any CPU.Build.0 = Debug|Any CPU + {235BF797-E3A2-4EEB-8D82-0C0BE1380663}.Release|Any CPU.ActiveCfg = Release|Any CPU + {235BF797-E3A2-4EEB-8D82-0C0BE1380663}.Release|Any CPU.Build.0 = Release|Any CPU + {4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {7B124E40-36AC-45CC-9467-F6A50D5FC149} = {DA69F624-5398-4884-87E4-B816698CDE65} - {7E2FD40B-18D4-4ADD-806A-3C6CED44F19D} = {DA69F624-5398-4884-87E4-B816698CDE65} {B23C2F84-B0B8-47D1-A8D2-E524F6511215} = {C4BC9889-B49F-41B6-806B-F84941B2549B} + {2EBFCD26-6E31-4855-ABF5-0A82E78F87C7} = {DA69F624-5398-4884-87E4-B816698CDE65} + {764DCFF5-D28D-4CEF-B186-B7A62FBB3E98} = {2429FBD8-1FCE-4C42-AA28-DF32F7249E77} + {E2094C87-C0F2-471D-A399-0B78CC32C1AA} = {2429FBD8-1FCE-4C42-AA28-DF32F7249E77} + {FAB21D29-0CBF-4243-94AF-067EBFB15EAE} = {DA69F624-5398-4884-87E4-B816698CDE65} + {367FD2C0-A505-4761-B229-AA3948D25F21} = {2429FBD8-1FCE-4C42-AA28-DF32F7249E77} + {235BF797-E3A2-4EEB-8D82-0C0BE1380663} = {DA69F624-5398-4884-87E4-B816698CDE65} + {4F12F3CF-3B6F-4E7F-9DF9-0E128978BD77} = {2429FBD8-1FCE-4C42-AA28-DF32F7249E77} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7945A4E4-ACDB-4F6E-95CA-6AC6E7C2CD59} diff --git a/Directory.Build.props b/Directory.Build.props index df60744f9..676a58023 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -9,6 +9,13 @@ Azure SignalR + Microsoft + © Microsoft Corporation. All rights reserved. + microsoft,azure-sdk + https://raw.githubusercontent.com/Azure/azure-signalr/dev/LICENSE + true + https://aka.ms/signalr_service + http://go.microsoft.com/fwlink/?LinkID=288890 https://github.com/azure/azure-signalr git $(MSBuildThisFileDirectory) @@ -17,5 +24,6 @@ true true latest + 0.3.0 diff --git a/README.md b/README.md index cf8f03c38..3d84e8285 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,44 @@ -# Microsoft Azure SignalR SDK for .NET +# Azure SignalR Service SDK for .NET -Microsoft Azure SignalR SDK for .NET helps you to instantly build Azure applications with real-time messaging functionality, taking advantage of scalable cloud computing resources. +Azure SignalR Service SDK for .NET helps you to instantly build Azure applications with real-time messaging functionality, taking advantage of scalable cloud computing resources. This repository contains the open source subset of the .NET SDK. +## Build Status + +[![Travis build status](https://img.shields.io/travis/Azure/azure-signalr.svg?label=travis-ci&branch=dev&style=flat-square)](https://travis-ci.org/Azure/azure-signalr/branches) +[![AppVeyor build status](https://img.shields.io/appveyor/ci/vicancy/azure-signalr.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/vicancy/azure-signalr) + ## Nuget Packages -Package Name | Target Framework | Version ----|---|--- -Microsoft.Azure.SignalR | .NET Standard 2.0 | ![MyGet](https://img.shields.io/myget/azure-signalr-dev/v/Microsoft.Azure.SignalR.svg) -Microsoft.Azure.SignalR.Owin | .NET Framework 4.6.1 | ![MyGet](https://img.shields.io/myget/azure-signalr-dev/v/Microsoft.Azure.SignalR.Owin.svg) +Package Name | Target Framework | NuGet | MyGet +---|---|---|--- +Microsoft.Azure.SignalR | .NET Standard 2.0 | [![NuGet](https://img.shields.io/nuget/v/Microsoft.Azure.SignalR.svg)](https://www.nuget.org/packages/Microsoft.Azure.SignalR) | [![MyGet](https://img.shields.io/myget/azure-signalr-dev/v/Microsoft.Azure.SignalR.svg)](https://www.myget.org/feed/azure-signalr-dev/package/nuget/Microsoft.Azure.SignalR) +Microsoft.Azure.SignalR.Protocols | .NET Standard 2.0 | [![NuGet](https://img.shields.io/nuget/v/Microsoft.Azure.SignalR.Protocols.svg)](https://www.nuget.org/packages/Microsoft.Azure.SignalR.Protocols) | [![MyGet](https://img.shields.io/myget/azure-signalr-dev/v/Microsoft.Azure.SignalR.Protocols.svg)](https://www.myget.org/feed/azure-signalr-dev/package/nuget/Microsoft.Azure.SignalR.Protocols) ## Getting Started -1. Add below MyGet feed URL as a package source in your `NuGet.config`. +Azure SignalR Service is based on [ASP.NET Core SignalR](https://github.com/aspnet/SignalR) framework. And SignalR Service SDK also depends on ASP.NET Core SignalR packages. If you are not familiar with ASP.NET Core SignalR yet, we recommend you to read [ASP.NET Core SignalR's documentation](https://docs.microsoft.com/en-us/aspnet/core/signalr/) first. + +Follow the tutorial at [here](https://aka.ms/signalr_service_doc) to get started with Azure SignalR Service. + +More samples on how to use Azure SignalR Service can be found at [here](https://github.com/aspnet/AzureSignalR-samples/). + +## Next Steps - `https://www.myget.org/F/azure-signalr-dev/api/v3/index.json` +For more information, see the following resources. -2. Add Azure SignalR package to your project. +- [REST API support](./docs/rest-api.md) - I. For ASP.NET Core, run below command - ```bash - dotnet add package Microsoft.Azure.SignalR - ``` +## Developer Getting Started - II. For ASP.NET MVC with OWIN, run below command - ```powershell - install-package Microsoft.AspNet.SignalR.Owin - ``` +### Building from source -## API Reference +Run `build.cmd` or `build.sh` without arguments for a complete build including tests. +See [Building documents](https://github.com/aspnet/Home/wiki/Building-from-source) for more details. -Detailed API Reference is at [here](./docs/api-reference.md). -## Contributing +### Contributing This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 000000000..4868357d8 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,4 @@ +version: 1.0.{build} +build_script: +- cmd: ./build.cmd -ci /p:BuildNumber=%APPVEYOR_BUILD_NUMBER% +test: off diff --git a/build.cmd b/build.cmd new file mode 100644 index 000000000..c0050bda1 --- /dev/null +++ b/build.cmd @@ -0,0 +1,2 @@ +@ECHO OFF +PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0run.ps1' default-build %*; exit $LASTEXITCODE" diff --git a/build.sh b/build.sh new file mode 100755 index 000000000..98a4b2276 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -euo pipefail +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Call "sync" between "chmod" and execution to prevent "text file busy" error in Docker (aufs) +chmod +x "$DIR/run.sh"; sync +"$DIR/run.sh" default-build "$@" diff --git a/build/dependencies.props b/build/dependencies.props index d95fe07ef..685fca5a3 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -3,31 +3,38 @@ $(MSBuildAllProjects);$(MSBuildThisFileFullPath) - - 2.1.0-preview2-30131 - 1.0.0-preview2-30186 - 1.0.0-preview2-30186 - 1.0.0-preview2-30186 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 + + 1.7.3.4 + 1.0.0 + 1.0.0 3.19.1 2.3.2 - 1.0 + 4.5.0 + 4.5.0 + 5.2.1 + 2.1.0 + 2.1.0 + 2.1.0 + + + 2.4.0-preview1-20180919-06 + 2.1.0 + 2.1.0 + + + 0.3.0 + - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 2.1.0-preview2-30131 - 10.0.1 + 2.1.0 + 11.0.2 + + + 1.0.0 + 15.6.1 + 4.7.49 + 2.4.0 + 2.4.0 + 4.0.0 diff --git a/build/microbuild.proj b/build/microbuild.proj new file mode 100644 index 000000000..98619762a --- /dev/null +++ b/build/microbuild.proj @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/build/package-test.proj b/build/package-test.proj new file mode 100644 index 000000000..827129bcf --- /dev/null +++ b/build/package-test.proj @@ -0,0 +1,11 @@ + + + netcoreapp2.1 + $(VersionPrefix)-$(VersionSuffix) + + + + + + + diff --git a/build/sign.proj b/build/sign.proj new file mode 100644 index 000000000..8bf7b0fcd --- /dev/null +++ b/build/sign.proj @@ -0,0 +1,37 @@ + + + + + + + $(UserProfile)\.nuget\packages + $(NuGetPackagesDir)\microbuild.core\$(MicroBuildCorePackageVersion)\build\ + + + + + + + + + + + + + + + $(RepositoryRoot)\ + $(RepositoryRoot)\artifacts\build + + + + + + NuGet + + + + + + diff --git a/build/sources.props b/build/sources.props index 9feff29d0..e3d4f27d0 100644 --- a/build/sources.props +++ b/build/sources.props @@ -2,15 +2,18 @@ - $(DotNetRestoreSources) - - $(RestoreSources); - https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json; - https://dotnet.myget.org/F/aspnetcore-tools/api/v3/index.json; + + $(DotNetRestoreSources); + https://dotnet.myget.org/F/dotnet-core/api/v3/index.json; + https://dotnet.myget.org/F/aspnet-signalr/api/v3/index.json; $(RestoreSources); https://api.nuget.org/v3/index.json; + + $(LocalFeed); + https://api.nuget.org/v3/index.json; + diff --git a/docs/api-reference.md b/docs/api-reference.md deleted file mode 100644 index 7ebee080c..000000000 --- a/docs/api-reference.md +++ /dev/null @@ -1,266 +0,0 @@ -# Azure SignalR SDK for .NET - API Reference - -## Namespaces - -Namespace | Description ----|--- -[Microsoft.Extensions.DependencyInjection](#microsoftextensionsdependencyinjection-namespace) | Contains extension methods for Azure SignalR dependency injection in ASP.NET Core 2.x -[Microsoft.AspNetCore.Builder](#microsoftaspnetcorebuilder-namespace) | Contains extension methods to initialize connections with Azure SignalR in ASP.NET Core 2.x -[Microsoft.Azure.SignalR](#microsoftazuresignalr-namespace) | Contains core functionalities to interact with Azure SignalR. -[Owin](#owin-namespace) | Contains extension methods to initialize connections with Azure SignalR in OWIN startup. - - -### `Microsoft.Extensions.DependencyInjection` Namespace - -Class | Description ----|--- -[AzureSignalRDependencyInjectionExtensions](#azuresignalrdependencyinjectionextensions-class) | Add Azure SignalR dependencies in ASP.NET Core 2.x - - -### `Microsoft.AspNetCore.Builder` Namespace - -Class | Description ----|--- -[AzureSignalRApplicationBuilderExtensions](#azuresignalrapplicationbuilderextensions-class) | Initialize connections with Azure SignalR in ASP.NET Core 2.x - - -### `Microsoft.Azure.SignalR` Namespace - -Class | Description ----|--- -[CloudSignalR](#cloudsignalr-class) | Entry class to create Azure SignalR objects from connection string. -[EndpointProvider](#endpointprovider-class) | Provide endpoint for clients and hub hosts. -[TokenProvider](#tokenprovider-class) | Provide access tokens for clients and hub hosts. -[HubProxy](#hubproxy-class) | Proxy class to send message to Azure SignalR instance with REST API calls. -[HubClientsProxy](#hubclientsproxy-class) | Proxy class to send message to Azure SignalR clients with REST API calls. -[GroupManagerProxy](#groupmanagerproxy-class) | Proxy class to manage groups in Azure SignalR with REST API calls. -[HubHostOptions](#hubhostoptions-class) | Options to configure Hub host. -[HubProxyOptions](#hubproxyoptions-class) | Options to configure HubProxy. - -### `Owin` Namespace - -Class | Description ----|--- -[AzureSignalRAppBuilderExtensions](#azuresignalrappbuilderextensions-class) | Initialize connections with Azure SignalR in OWIN startup. - - - -## Classes - -### `AzureSignalRDependencyInjectionExtensions` Class - -**Namespace**: `Microsoft.Extensions.DependencyInjection`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -#### `AddAzureSignalR(this IServiceCollection services, Action configure = null)` Method -**Return**: `Microsoft.Extensions.DependencyInjection.IServiceCollection` - ---- - -### `AzureSignalRApplicationBuilderExtensions` Class - -**Namespace**: `Microsoft.AspNetCore.Builder`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -#### `UseAzureSignalR(this IApplicationBuilder app, string connectionString, Action configure)` Method -**Return**: `Microsoft.AspNetCore.Builder.IApplicationBuilder` - ---- - -### `AzureSignalRAppBuilderExtensions` Class - -**Namespace**: `Owin`
-**Assembly**: `Microsoft.Azure.SignalR.Owin.dll` - -#### `UseAzureSignalR(this IAppBuilder app, string connectionString, Action configure)` Method -**Return**: `Owin.IAppBuilder` - ---- - -### `CloudSignalR` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Method | Description ----|--- -`CreateEndpointProviderFromConnectionString` | Create an instance of `EndpointProvider` for type `THub` from Azure SignalR connection string. Type `THub` is subclass of `Microsoft.Azure.SignalR.Hub` class. -`CreateEndpointProviderFromConnectionString` | Create an instance of `EndpointProvider` for a named hub from Azure SignalR connection string. -`CreateTokenProviderFromConnectionString` | Create an instance of `TokenProvider` for type `THub` from Azure SignalR connection string. -`CreateTokenProviderFromConnectionString` | Create an instance of `TokenProvider` for a named hub from Azure SignalR connection string. -`CreateHubProxyFromConnectionString` | Create an instance of `HubProxy` for type `THub` from Azure SignalR connection string. -`CreateHubProxyFromConnectionString` | Create an instance of `HubProxy` for a named hub from Azure SignalR connection string. - -#### `CloudSignalR.CreateEndpointProviderFromConnectionString(string connectionString)` Method -**Return**: `Microsoft.Azure.SignalR.EndpointProvider` - -#### `CloudSignalR.CreateEndpointProviderFromConnectionString(string connectionString, string hubName)` Method -**Return**: `Microsoft.Azure.SignalR.EndpointProvider` - -#### `CloudSignalR.CreateTokenProviderFromConnectionString(string connectionString)` Method -**Return**: `Microsoft.Azure.SignalR.TokenProvider` - -#### `CloudSignalR.CreateTokenProviderFromConnectionString(string connectionString, string hubName)` Method -**Return**: `Microsoft.Azure.SignalR.TokenProvider` - -#### `CloudSignalR.CreateHubProxyFromConnectionString(string connectionString)` Method -**Return**: `Microsoft.Azure.SignalR.HubProxy` - -#### `CloudSignalR.CreateHubProxyFromConnectionString(string connectionString, string hubName)` Method -**Return**: `Microsoft.Azure.SignalR.HubProxy` - -#### `CloudSignalR.CreateHubProxyFromConnectionString(string connectionString, HubHostOptions options)` Method -**Return**: `Microsoft.Azure.SignalR.HubProxy` - -#### `CloudSignalR.CreateHubProxyFromConnectionString(string connectionString, string hubName, HubHostOptions options)` Method -**Return**: `Microsoft.Azure.SignalR.HubProxy` - ---- - -### `EndpointProvider` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Method | Description ----|--- -`GetClientEndpoint` | Get client endpoint of type `THub`. `THub` is a subclass of `Microsoft.Azure.SignalR.Hub`. -`GetClientEndpoint` | Get client endpoint of a hub named `hubName`. -`GetServerEndpoint` | Get hub host endpoint of type `THub`. `THub` is a subclass of `Microsoft.Azure.SignalR.Hub`. -`GetServerEndpoint` | Get hub host endpoint of a hub named `hubName`. - -#### `EndpointProvider.GetClientEndpoint() where THub : Hub` Method -**Return**: `System.String` - -#### `EndpointProvider.GetClientEndpoint()` Method -**Return**: `System.String` - -#### `EndpointProvider.GetServerEndpoint() where THub : Hub` Method -**Return**: `System.String` - -#### `EndpointProvider.GetServerEndpoint()` Method -**Return**: `System.String` - ---- - -### `TokenProvider` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Method | Description ----|--- -`GenerateClientAccessToken` | Generate client access token of type `THub`. `THub` is a subclass of `Microsoft.Azure.SignalR.Hub`. -`GenerateClientAccessToken` | Generate client access token of a hub named `hubName`. -`GenerateServerAccessToken` | Generate hub host access token of type `THub`. `THub` is a subclass of `Microsoft.Azure.SignalR.Hub`. -`GenerateServerAccessToken` | Generate hub host access token of a hub named `hubName`. - -#### `TokenProvider.GenerateClientAccessToken(IEnumerable claims = null, TimeSpan? lifetime = null) where THub : Hub` Method -**Return**: `System.String` - -#### `TokenProvider.GenerateClientAccessToken(string hubName, IEnumerable claims = null, TimeSpan? lifetime = null)` Method -**Return**: `System.String` - -#### `TokenProvider.GenerateServerAccessToken(TimeSpan? lifetime = null) where THub : Hub` Method -**Return**: `System.String` - -#### `TokenProvider.GenerateServerAccessToken(string hubName, TimeSpan? lifetime = null)` Method -**Return**: `System.String` - ---- - -### `HubProxy` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Property | Description ----|--- -`Clients` | Proxy to clients connected to Azure SignalR. -`Groups` | Proxy to groups within Azure SignalR. - -#### `IHubClients Clients` Property - -#### `IGroupManager Groups` Property - ---- - -### `HubClientsProxy : IHubClients` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Method | Description ----|--- -`AllExcept` | Broadcast message except the specified clients. -`Client` | Send message to the specified client. -`Clients` | Send message to the specified clients. -`Group` | Send message to the specified group. -`Groups` | Send message to the specified groups. -`GroupExcept` | Send message to the specified group except the specified client. -`User` | Send message to the specified user. -`Users` | Send message to the specified users. - -#### `HubClientsProxy.AllExcept(IReadOnlyList excludedIds)` Method -**Return**: `Task` - -#### `HubClientsProxy.Client(string connectionId)` Method -**Return**: `Task` - -#### `HubClientsAProxy.Clients(IReadOnlyList connectionIds)` Method -**Return**: `Task` - -#### `HubClientsAProxy.Group(string groupName)` Method -**Return**: `Task` - -#### `HubClientsAProxy.Groups(IReadOnlyList groupNames)` Method -**Return**: `Task` - -#### `HubClientsAProxy.GroupExcept(string groupName, IReadOnlyList excludeIds)` Method -**Return**: `Task` - -#### `HubClientsAProxy.User(string userId)` Method -**Return**: `Task` - -#### `HubClientsAProxy.Users(IReadOnlyList userIds)` Methods -**Return**: `Task` - ---- - -### `GroupManagerProxy : IGroupManager` Class - -**Namespace**: `Microsoft.Azure.SignalR`
-**Assembly**: `Microsoft.Azure.SignalR.dll` - -Method | Description ----|--- -`AddAsync` | Add a specified connection to a specified group. -`RemoveAsync` | Remove a specified connection fromss a specified group. - -#### `GroupManagerProxy.AddAsync(string connectionId, string groupName)` Method -**Return**: `Task` - -#### `GroupManagerProxy.RemoveAsync(string connectionId, string groupName)` Method -**Return**: `Task` - ---- - -### `HubHostOptions` Class - -Property | Type | Default Value | Description ----|---|---|--- -`ConnectionNumber` | `int` | 5 | Number of connections from Hub host to Azure SignalR. -`ProtocolType` | `ProtocolType` | `ProtocolType.Text` | Protocol used between Hub host and Azure SignalR. -`ServerTimeout` | `TimeSpan` | 30 seconds | Timeout interval between ping messages from Azure SignalR. -`OnConnected` | `Func` | null | User callback when connection between Hub host and Azure SignalR is established or re-established. -`OnDisconnected` | `Func` | null | User callback when connection between Hub host and Azure SignalR is closed. -`AutoReconnect` | `bool` | `True` | Flag to control whether to reconnect when connection between Hub host and Azure SignalR is closed. - ---- - -### `HubProxyOptions` Class - -Property | Type | Default Value | Description ----|---|---|--- -`ApiVersion` | `string` | `v1-preview` | REST API version when calling Azure SignalR. - diff --git a/docs/images/serverless-arch.png b/docs/images/serverless-arch.png new file mode 100644 index 0000000000000000000000000000000000000000..447ffb177b726074a17471ac669a0973d39427b1 GIT binary patch literal 19334 zcmbTeby$_#);=tV5`uI|mox|{As|SXbf=3}7Af6Gw{*9Z)S?zhH%f}sqDutn?)Xji z+2_3PxxW4W^XcUUJ~3m=F~=D9825Zal@+D2FrHxCyLS&uRz^bg-aVu(@XrYCA-EFZ zc-wgI9`!w0iP!3`hT9qFjs)G)K?|?|`cfnUoT=BQaR#Dj;orO^iR_UiaR`y7P$XXK ztCaT@poM+B;>ccqFk2qRicu6&MVF1^7lJ~AOyhxkt`zBPQ#dLN{_>~^Uk?eSd%XEl z(&FCICuA@_KBsaH{wxNg$oQhr#*6k%v0cSFKK$-uwg}bbRpqI&=MW|?{v%LCLKVIc zr$$$^ww0KH@?MiUIKDcCiHtb1DqlWV{||{e6MiJiex;>04<%uLH)1N?#z2L@`3Gq= z5+1c54nBW+dYA#K5@a%mz;yLjj{V$wCQUYsd=q(EUfw5W#&R@@SGTGR;^d@hAGrMn zw)9O+f77zFl98qeWZwELbv)P-j*md7GW=-rHq%$Bz4$2887=-pS*4t(9SE{CQdEPgDS%KbR~FBdPF3L zvfRe0nB9~Ks`so456sLC=gelrYTvlK1NY$!J(2;k+ZyL$BSq^{ zQXV5$_6Ok_Tm2W*sszrWXRgoCKZ(|(HJ$BHW^3C!S{kPDY}hgQZKu*wm%I2h9`X@% zK8R>3FD^DY!cakY=dJkBiy|*k+Z`x8{M~dz_t4iAWKlSXr|4&ri(SR)rI+uq z&1a_{CiL~{miu?WBO*{gfq8|u4f!+f4{4!t)!16av#AK_LtqAYdugK1Hk6uPHCTL3 zw!W+z8gO;&Kx=g3M{$3>Qs)u1670n|Qt=_qF0jp6wv88=%Ci}jB2DwP;G@Y}X6Vdb zH7%I?Tii3}Q~NFcBVu>QdLivMFhe{v0l(pvndn3;mK<^X4{0MzTb~L;uww6_Yt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bn)@>i5LC^eaVPk{J<_derxcJeHun d@UZ-oXK8-+zb#E|4)A;}22WQ%mvv4FO#rU9Z`%L> literal 0 HcmV?d00001 diff --git a/docs/rest-api.md b/docs/rest-api.md new file mode 100644 index 000000000..4fde55a18 --- /dev/null +++ b/docs/rest-api.md @@ -0,0 +1,145 @@ +# REST API in Azure SignalR Service + +- [Typical Server-less Architecture](#serverless) +- [API](#api) + - [Broadcast message to all clients](#broadcast) + - [Broadcast message to a group](#broadcast-group) + - [Send message to a user](#send-user) + - [Add a user to a group](#add-user-to-group) + - [Remove a user from a group](#remove-user-from-group) +- [Using REST API](#using-rest-api) + - [Authentication](#authentication) + - [Signing Algorithm and Signature](#signing) + - [Claims](#claims) + - [User-related REST API](#user-api) + - [Sample](#sample) + +On top of classical client-server pattern, Azure SignalR Service provides a set of REST APIs, so that you can easily integrate real-time functionality into your server-less architecture. + + +## Typical Server-less Architecture with Azure Functions + +The following diagram shows a typical server-less architecture of using Azure SignalR Service with Azure Functions. + +![Typical Serverless Architecture](./images/serverless-arch.png) + +- `negotiate` function will return negotiation response and redirect all clients to Azure SignalR Service. +- `broadcast` function will call Azure SignalR Service's REST API. Then SignalR Service will broadcast the message to all connected clients. + +In server-less architecture, clients still have persistent connections to Azure SignalR Service. +Since there are no application server to handle traffic, clients are in `LISTEN` mode, which means they can only receive messages but can't send messages. +SignalR Service will disconnect any client who sends messages because it is an invalid operation. + +> NOTE: In version 1.1.0-preview of ASP.NET Core SignalR, client will send `PingMessage` to server. +> Right now it will cause SignalR Service disconnecting clients. +> So the latest 1.0.x version of ASP.NET Core SignalR client library is recommended to use with Azure SignalR Service. +> We are working in progress to support the new version of ASP.NET Core SignalR. + +You can find a complete sample of using Azure SignalR Service with Azure Functions at [here](https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/RealtimeSignIn). + +## API + +The following table shows all versions of REST API we have for now. You can also find the swagger file for each version of REST API. + +API Version | Status | Port | Spec +---|---|---|--- +`1.0-preview` | Available | 5002 | [swagger](./swagger/v1-preview.json) +`1.0` | *Not Available until GA* | Standard | [swagger](./swagger/v1.json) + +Available APIs of each version are listed as following. + +API | `1.0-preview` | `1.0` +---|---|--- +[Broadcast to all](#broadcast) | :heavy_check_mark: | :heavy_check_mark: +[Broadcast to a group](#broadcast-group) | :heavy_check_mark: | :heavy_check_mark: +Broadcast to a few groups | :heavy_check_mark: (Deprecated) | `N/A` +[Send to a user](#send-user) | :heavy_check_mark: | :heavy_check_mark: +Send to a few users | :heavy_check_mark: (Deprecated) | `N/A` +[Add a user to a group](#add-user-to-group) | `N/A` | :heavy_check_mark: +[Remove a user from a group](#remove-user-from-group) | `N/A` | :heavy_check_mark: + + +### Broadcast message to all clients + +API Version | HTTP Method | Request URL | Request Body +---|---|---|--- +`1.0-preview` | `POST` | `https://.service.signalr.net:5002/api/v1-preview/hub/` | `{ "target":"", "arguments":[ ... ] }` +`1.0` | `POST` | `https://.service.signalr.net/api/v1/hubs/` | Same as above + + +### Broadcast message to a group + +API Version | HTTP Method | Request URL | Request Body +---|---|---|--- +`1.0-preview` | `POST` | `https://.service.signalr.net:5002/api/v1-preview/hub//group/` | `{ "target":"", "arguments":[ ... ] }` +`1.0` | `POST` | `https://.service.signalr.net/api/v1/hubs//groups/` | Same as above + + +### Send message to a user + +API Version | HTTP Method | Request URL | Request Body +---|---|---|--- +`1.0-preview` | `POST` | `https://.service.signalr.net:5002/api/v1-preview/hub//user/` | `{ "target":"", "arguments":[ ... ] }` +`1.0` | `POST` | `https://.service.signalr.net/api/v1/hubs//users/` | Same as above + + +### Add a user to a group + +API Version | HTTP Method | Request URL +---|---|--- +`1.0` | `PUT` | `https://.service.signalr.net/api/v1/hubs//groups//users/` + + +### Remove a user from a group + +API Version | HTTP Method | Request URL +---|---|--- +`1.0` | `DELETE` | `https://.service.signalr.net/api/v1/hubs//groups//users/` + +## Using REST API + +### Authentication + +In each HTTP request, an authorization header with a [JSON Web Token (JWT)](https://en.wikipedia.org/wiki/JSON_Web_Token) is required to authenticate with Azure SignalR Service. + + +#### Signing Algorithm and Signature + +`HS256`, namely HMAC-SHA256, is used as the signing algorithm. + +You should use the `AccessKey` in Azure SignalR Service instance's connection string to sign the generated JWT token. + +#### Claims + +Below claims are required to be included in the JWT token. + +Claim Type | Is Required | Description +---|---|--- +`aud` | true | Should be the **SAME** as your HTTP request url, trailing slash and query paramters not included. For example, a broadcast request's audience should look like: `https://example.service.signalr.net/api/v1/hubs/myhub`. +`exp` | true | Epoch time when this token will be expired. + + +### User-related REST API + +In order to call user-related REST API, each of your clients should identify themselves to Azure SignalR Service. +Otherwise SignalR Service can't find target connections from a given user id. + +This can be achieved by including a `nameid` claim in each client's JWT token when they are connecting to Azure SignalR Service. +Then SignalR Service will use the value of `nameid` claim as the user id of each client connection. + +As shown in the [architecture section](#serverlss), the `negotiate` function will return a redirect negotiation response to client. +A typical negotiation response looks like as folllowing. The `nameid` claim should be included in the access token. + + ```json + { + "url":"https://test.service.signalr.net:5001/client/?hub=chat&...", + "accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1MzY2NTYzMjYsImV4cCI6MTUzNjY1OTkyNiwiaWF0IjoxNTM2NjU2MzI2LCJhdWQiOiJodHRwczovL3Rlc3Quc2VydmljZS5zaWduYWxyLm5ldDo1MDAxL2NsaWVudC8_aHViPWNoYXQiLCJuYW1laWQiOiJ1c2VyLWlkIn0.k24D5kk7KeA_JKmxaEU0gGtF4JhOlyQ2VDmITHrzPtA" , + "availableTransports":[] + } + ``` + +Read more about redirecting client to Azure SignalR Service at [here](TODO). + +### Sample + +You can find a complete console app to demonstrate how to use REST API in Azure SignalR Service at [here](https://github.com/aspnet/AzureSignalR-samples/tree/master/samples/Serverless). diff --git a/docs/swagger/v1-preview.json b/docs/swagger/v1-preview.json new file mode 100644 index 000000000..d428c6f39 --- /dev/null +++ b/docs/swagger/v1-preview.json @@ -0,0 +1,221 @@ +{ + "swagger": "2.0", + "info": { + "version": "v1-preview", + "title": "Azure SignalR Service REST API" + }, + "paths": { + "/api/v1-preview/hub/{hub}/user/{id}": { + "post": { + "description": "Send a message to a single user.", + "tags": [], + "operationId": "SendMessageToUser", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "id", + "in": "path", + "required": true, + "type": "string", + "description": "Target user Id." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1-preview/hub/{hub}/users/{userList}": { + "post": { + "description": "Send a message to multiple users.", + "tags": [], + "operationId": "SendMessageToUsers", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "userList", + "in": "path", + "required": true, + "type": "string", + "description": "Comma-separated list of user Ids." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1-preview/hub/{hub}": { + "post": { + "description": "Broadcast a message to all clients connected to target hub.", + "tags": [], + "operationId": "BroadcastMessage", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1-preview/hub/{hub}/group/{group}": { + "post": { + "description": "Broadcast a message to all clients within the target group.", + "tags": [], + "operationId": "SendMessageToGroup", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "group", + "in": "path", + "required": true, + "type": "string", + "description": "Target group name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1-preview/hub/{hub}/groups/{groupList}": { + "post": { + "description": "Broadcast a message to all clients within the target groups.", + "tags": [], + "operationId": "SendMessageToGroups", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "groupList", + "in": "path", + "required": true, + "type": "string", + "description": "Comma-separated list of group names. Each group name should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + } + }, + "definitions": { + "Message": { + "type": "object", + "description": "Method invocation message.", + "properties": { + "target": { + "type": "string", + "description": "Target method name." + }, + "arguments": { + "type": "array", + "items": { + "type": "object" + }, + "description": "Target method arguments." + } + } + } + } +} diff --git a/docs/swagger/v1.json b/docs/swagger/v1.json new file mode 100644 index 000000000..20dfcf36a --- /dev/null +++ b/docs/swagger/v1.json @@ -0,0 +1,217 @@ +{ + "swagger": "2.0", + "info": { + "version": "v1", + "title": "Azure SignalR Service REST API" + }, + "paths": { + "/api/v1/hubs/{hub}/users/{id}": { + "post": { + "description": "Send a message to a single user.", + "tags": [], + "operationId": "SendMessageToUser", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "id", + "in": "path", + "required": true, + "type": "string", + "description": "Target user Id." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1/hubs/{hub}": { + "post": { + "description": "Broadcast a message to all clients connected to target hub.", + "tags": [], + "operationId": "BroadcastMessage", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1/hubs/{hub}/groups/{group}": { + "post": { + "description": "Broadcast a message to all clients within the target group.", + "tags": [], + "operationId": "SendMessageToGroup", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "group", + "in": "path", + "required": true, + "type": "string", + "description": "Target group name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "message", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Message" + } + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + }, + "/api/v1/hubs/{hub}/groups/{group}/users/{id}": { + "put": { + "description": "Add a user to the target group.", + "tags": [], + "operationId": "AddUserToGroup", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "group", + "in": "path", + "required": true, + "type": "string", + "description": "Target group name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "id", + "in": "path", + "required": true, + "type": "string", + "description": "Target user Id." + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + }, + "delete": { + "description": "Remove a user from the target group.", + "tags": [], + "operationId": "RemoveUserFromGroup", + "consumes": [ + "application/json" + ], + "produces": [], + "parameters": [ + { + "name": "hub", + "in": "path", + "required": true, + "type": "string", + "description": "Target hub name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "group", + "in": "path", + "required": true, + "type": "string", + "description": "Target group name, which should start with alphabetic characters and only contain alpha-numeric characters or underscore." + }, + { + "name": "id", + "in": "path", + "required": true, + "type": "string", + "description": "Target user Id." + } + ], + "responses": { + "202": { + "description": "Accepted" + } + } + } + } + }, + "definitions": { + "Message": { + "type": "object", + "description": "Method invocation message.", + "properties": { + "target": { + "type": "string", + "description": "Target method name." + }, + "arguments": { + "type": "array", + "items": { + "type": "object" + }, + "description": "Target method arguments." + } + } + } + } +} diff --git a/korebuild-lock.txt b/korebuild-lock.txt new file mode 100644 index 000000000..27e94579a --- /dev/null +++ b/korebuild-lock.txt @@ -0,0 +1,2 @@ +version:2.1.0-rtm-15783 +commithash:5fc2b2f607f542a2ffde11c19825e786fc1a3774 diff --git a/korebuild.json b/korebuild.json new file mode 100644 index 000000000..729b71454 --- /dev/null +++ b/korebuild.json @@ -0,0 +1,4 @@ +{ + "$schema": "https://raw.githubusercontent.com/aspnet/BuildTools/release/2.1/tools/korebuild.schema.json", + "channel": "release/2.1" + } \ No newline at end of file diff --git a/pack.cmd b/pack.cmd deleted file mode 100644 index 51a2291a8..000000000 --- a/pack.cmd +++ /dev/null @@ -1,20 +0,0 @@ -@ECHO OFF - -SETLOCAL -IF NOT [%~1]==[] SET BuildNumber=%~1 - -PUSHD %~dp0 - -RD /S /Q .publish -MKDIR .publish - -CD src/ -FOR /f %%p IN ('DIR . /AD /B') do ( - PUSHD %%p - IF EXIST *.csproj (dotnet pack -c Release -o ..\..\.publish) - POPD -) - -POPD - -ENDLOCAL diff --git a/publish.cmd b/publish.cmd index c7addb904..880da69d3 100644 --- a/publish.cmd +++ b/publish.cmd @@ -16,9 +16,9 @@ SET NugetServerUrl=https://www.myget.org/F/azure-signalr-dev/api/v2/package PUSHD %~dp0 -CALL ./pack.cmd %BuildNumber% +CALL ./build.cmd /p:BuildNumber=%BuildNumber% -CD ./.publish +CD ./artifacts/build CALL dotnet nuget push *.nupkg -k %ApiKey% -s %NugetServerUrl% POPD diff --git a/run.cmd b/run.cmd new file mode 100644 index 000000000..d52d5c7e6 --- /dev/null +++ b/run.cmd @@ -0,0 +1,2 @@ +@ECHO OFF +PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0run.ps1' %*; exit $LASTEXITCODE" diff --git a/run.ps1 b/run.ps1 new file mode 100644 index 000000000..412062239 --- /dev/null +++ b/run.ps1 @@ -0,0 +1,208 @@ +#!/usr/bin/env powershell +#requires -version 4 + +<# +.SYNOPSIS +Executes KoreBuild commands. + +.DESCRIPTION +Downloads korebuild if required. Then executes the KoreBuild command. To see available commands, execute with `-Command help`. + +.PARAMETER Command +The KoreBuild command to run. + +.PARAMETER Path +The folder to build. Defaults to the folder containing this script. + +.PARAMETER Channel +The channel of KoreBuild to download. Overrides the value from the config file. + +.PARAMETER DotNetHome +The directory where .NET Core tools will be stored. + +.PARAMETER ToolsSource +The base url where build tools can be downloaded. Overrides the value from the config file. + +.PARAMETER Update +Updates KoreBuild to the latest version even if a lock file is present. + +.PARAMETER Reinstall +Re-installs KoreBuild + +.PARAMETER ConfigFile +The path to the configuration file that stores values. Defaults to korebuild.json. + +.PARAMETER ToolsSourceSuffix +The Suffix to append to the end of the ToolsSource. Useful for query strings in blob stores. + +.PARAMETER CI +Sets up CI specific settings and variables. + +.PARAMETER Arguments +Arguments to be passed to the command + +.NOTES +This function will create a file $PSScriptRoot/korebuild-lock.txt. This lock file can be committed to source, but does not have to be. +When the lockfile is not present, KoreBuild will create one using latest available version from $Channel. + +The $ConfigFile is expected to be an JSON file. It is optional, and the configuration values in it are optional as well. Any options set +in the file are overridden by command line parameters. + +.EXAMPLE +Example config file: +```json +{ + "$schema": "https://raw.githubusercontent.com/aspnet/BuildTools/dev/tools/korebuild.schema.json", + "channel": "dev", + "toolsSource": "https://aspnetcore.blob.core.windows.net/buildtools" +} +``` +#> +[CmdletBinding(PositionalBinding = $false)] +param( + [Parameter(Mandatory = $true, Position = 0)] + [string]$Command, + [string]$Path = $PSScriptRoot, + [Alias('c')] + [string]$Channel, + [Alias('d')] + [string]$DotNetHome, + [Alias('s')] + [string]$ToolsSource, + [Alias('u')] + [switch]$Update, + [switch]$Reinstall, + [string]$ToolsSourceSuffix, + [string]$ConfigFile = $null, + [switch]$CI, + [Parameter(ValueFromRemainingArguments = $true)] + [string[]]$Arguments +) + +Set-StrictMode -Version 2 +$ErrorActionPreference = 'Stop' + +# +# Functions +# + +function Get-KoreBuild { + + $lockFile = Join-Path $Path 'korebuild-lock.txt' + + if (!(Test-Path $lockFile) -or $Update) { + Get-RemoteFile "$ToolsSource/korebuild/channels/$Channel/latest.txt" $lockFile $ToolsSourceSuffix + } + + $version = Get-Content $lockFile | Where-Object { $_ -like 'version:*' } | Select-Object -first 1 + if (!$version) { + Write-Error "Failed to parse version from $lockFile. Expected a line that begins with 'version:'" + } + $version = $version.TrimStart('version:').Trim() + $korebuildPath = Join-Paths $DotNetHome ('buildtools', 'korebuild', $version) + + if ($Reinstall -and (Test-Path $korebuildPath)) { + Remove-Item -Force -Recurse $korebuildPath + } + + if (!(Test-Path $korebuildPath)) { + Write-Host -ForegroundColor Magenta "Downloading KoreBuild $version" + New-Item -ItemType Directory -Path $korebuildPath | Out-Null + $remotePath = "$ToolsSource/korebuild/artifacts/$version/korebuild.$version.zip" + + try { + $tmpfile = Join-Path ([IO.Path]::GetTempPath()) "KoreBuild-$([guid]::NewGuid()).zip" + Get-RemoteFile $remotePath $tmpfile $ToolsSourceSuffix + if (Get-Command -Name 'Microsoft.PowerShell.Archive\Expand-Archive' -ErrorAction Ignore) { + # Use built-in commands where possible as they are cross-plat compatible + Microsoft.PowerShell.Archive\Expand-Archive -Path $tmpfile -DestinationPath $korebuildPath + } + else { + # Fallback to old approach for old installations of PowerShell + Add-Type -AssemblyName System.IO.Compression.FileSystem + [System.IO.Compression.ZipFile]::ExtractToDirectory($tmpfile, $korebuildPath) + } + } + catch { + Remove-Item -Recurse -Force $korebuildPath -ErrorAction Ignore + throw + } + finally { + Remove-Item $tmpfile -ErrorAction Ignore + } + } + + return $korebuildPath +} + +function Join-Paths([string]$path, [string[]]$childPaths) { + $childPaths | ForEach-Object { $path = Join-Path $path $_ } + return $path +} + +function Get-RemoteFile([string]$RemotePath, [string]$LocalPath, [string]$RemoteSuffix) { + if ($RemotePath -notlike 'http*') { + Copy-Item $RemotePath $LocalPath + return + } + + $retries = 10 + while ($retries -gt 0) { + $retries -= 1 + try { + Invoke-WebRequest -UseBasicParsing -Uri $($RemotePath + $RemoteSuffix) -OutFile $LocalPath + return + } + catch { + Write-Verbose "Request failed. $retries retries remaining" + } + } + + Write-Error "Download failed: '$RemotePath'." +} + +# +# Main +# + +# Load configuration or set defaults + +$Path = Resolve-Path $Path +if (!$ConfigFile) { $ConfigFile = Join-Path $Path 'korebuild.json' } + +if (Test-Path $ConfigFile) { + try { + $config = Get-Content -Raw -Encoding UTF8 -Path $ConfigFile | ConvertFrom-Json + if ($config) { + if (!($Channel) -and (Get-Member -Name 'channel' -InputObject $config)) { [string] $Channel = $config.channel } + if (!($ToolsSource) -and (Get-Member -Name 'toolsSource' -InputObject $config)) { [string] $ToolsSource = $config.toolsSource} + } + } + catch { + Write-Warning "$ConfigFile could not be read. Its settings will be ignored." + Write-Warning $Error[0] + } +} + +if (!$DotNetHome) { + $DotNetHome = if ($env:DOTNET_HOME) { $env:DOTNET_HOME } ` + elseif ($env:USERPROFILE) { Join-Path $env:USERPROFILE '.dotnet'} ` + elseif ($env:HOME) {Join-Path $env:HOME '.dotnet'}` + else { Join-Path $PSScriptRoot '.dotnet'} +} + +if (!$Channel) { $Channel = 'dev' } +if (!$ToolsSource) { $ToolsSource = 'https://aspnetcore.blob.core.windows.net/buildtools' } + +# Execute + +$korebuildPath = Get-KoreBuild +Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1') + +try { + Set-KoreBuildSettings -ToolsSource $ToolsSource -DotNetHome $DotNetHome -RepoPath $Path -ConfigFile $ConfigFile -CI:$CI + Invoke-KoreBuildCommand $Command @Arguments +} +finally { + Remove-Module 'KoreBuild' -ErrorAction Ignore +} diff --git a/run.sh b/run.sh new file mode 100755 index 000000000..4606a42e7 --- /dev/null +++ b/run.sh @@ -0,0 +1,245 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# +# variables +# + +RESET="\033[0m" +RED="\033[0;31m" +YELLOW="\033[0;33m" +MAGENTA="\033[0;95m" +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +[ -z "${DOTNET_HOME:-}" ] && DOTNET_HOME="$HOME/.dotnet" +verbose=false +update=false +reinstall=false +repo_path="$DIR" +channel='' +tools_source='' +tools_source_suffix='' +ci=false + +# +# Functions +# +__usage() { + echo "Usage: $(basename "${BASH_SOURCE[0]}") command [options] [[--] ...]" + echo "" + echo "Arguments:" + echo " command The command to be run." + echo " ... Arguments passed to the command. Variable number of arguments allowed." + echo "" + echo "Options:" + echo " --verbose Show verbose output." + echo " -c|--channel The channel of KoreBuild to download. Overrides the value from the config file.." + echo " --config-file The path to the configuration file that stores values. Defaults to korebuild.json." + echo " -d|--dotnet-home The directory where .NET Core tools will be stored. Defaults to '\$DOTNET_HOME' or '\$HOME/.dotnet." + echo " --path The directory to build. Defaults to the directory containing the script." + echo " -s|--tools-source|-ToolsSource The base url where build tools can be downloaded. Overrides the value from the config file." + echo " --tools-source-suffix|-ToolsSourceSuffix The suffix to append to tools-source. Useful for query strings." + echo " -u|--update Update to the latest KoreBuild even if the lock file is present." + echo " --reinstall Reinstall KoreBuild." + echo " --ci Apply CI specific settings and environment variables." + echo "" + echo "Description:" + echo " This function will create a file \$DIR/korebuild-lock.txt. This lock file can be committed to source, but does not have to be." + echo " When the lockfile is not present, KoreBuild will create one using latest available version from \$channel." + + if [[ "${1:-}" != '--no-exit' ]]; then + exit 2 + fi +} + +get_korebuild() { + local version + local lock_file="$repo_path/korebuild-lock.txt" + if [ ! -f "$lock_file" ] || [ "$update" = true ]; then + __get_remote_file "$tools_source/korebuild/channels/$channel/latest.txt" "$lock_file" "$tools_source_suffix" + fi + version="$(grep 'version:*' -m 1 "$lock_file")" + if [[ "$version" == '' ]]; then + __error "Failed to parse version from $lock_file. Expected a line that begins with 'version:'" + return 1 + fi + version="$(echo "${version#version:}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" + local korebuild_path="$DOTNET_HOME/buildtools/korebuild/$version" + + if [ "$reinstall" = true ] && [ -d "$korebuild_path" ]; then + rm -rf "$korebuild_path" + fi + + { + if [ ! -d "$korebuild_path" ]; then + mkdir -p "$korebuild_path" + local remote_path="$tools_source/korebuild/artifacts/$version/korebuild.$version.zip" + tmpfile="$(mktemp)" + echo -e "${MAGENTA}Downloading KoreBuild ${version}${RESET}" + if __get_remote_file "$remote_path" "$tmpfile" "$tools_source_suffix"; then + unzip -q -d "$korebuild_path" "$tmpfile" + fi + rm "$tmpfile" || true + fi + + source "$korebuild_path/KoreBuild.sh" + } || { + if [ -d "$korebuild_path" ]; then + echo "Cleaning up after failed installation" + rm -rf "$korebuild_path" || true + fi + return 1 + } +} + +__error() { + echo -e "${RED}error: $*${RESET}" 1>&2 +} + +__warn() { + echo -e "${YELLOW}warning: $*${RESET}" +} + +__machine_has() { + hash "$1" > /dev/null 2>&1 + return $? +} + +__get_remote_file() { + local remote_path=$1 + local local_path=$2 + local remote_path_suffix=$3 + + if [[ "$remote_path" != 'http'* ]]; then + cp "$remote_path" "$local_path" + return 0 + fi + + local failed=false + if __machine_has wget; then + wget --tries 10 --quiet -O "$local_path" "${remote_path}${remote_path_suffix}" || failed=true + else + failed=true + fi + + if [ "$failed" = true ] && __machine_has curl; then + failed=false + curl --retry 10 -sSL -f --create-dirs -o "$local_path" "${remote_path}${remote_path_suffix}" || failed=true + fi + + if [ "$failed" = true ]; then + __error "Download failed: $remote_path" 1>&2 + return 1 + fi +} + +# +# main +# + +command="${1:-}" +shift + +while [[ $# -gt 0 ]]; do + case $1 in + -\?|-h|--help) + __usage --no-exit + exit 0 + ;; + -c|--channel|-Channel) + shift + channel="${1:-}" + [ -z "$channel" ] && __usage + ;; + --config-file|-ConfigFile) + shift + config_file="${1:-}" + [ -z "$config_file" ] && __usage + if [ ! -f "$config_file" ]; then + __error "Invalid value for --config-file. $config_file does not exist." + exit 1 + fi + ;; + -d|--dotnet-home|-DotNetHome) + shift + DOTNET_HOME="${1:-}" + [ -z "$DOTNET_HOME" ] && __usage + ;; + --path|-Path) + shift + repo_path="${1:-}" + [ -z "$repo_path" ] && __usage + ;; + -s|--tools-source|-ToolsSource) + shift + tools_source="${1:-}" + [ -z "$tools_source" ] && __usage + ;; + --tools-source-suffix|-ToolsSourceSuffix) + shift + tools_source_suffix="${1:-}" + [ -z "$tools_source_suffix" ] && __usage + ;; + -u|--update|-Update) + update=true + ;; + --reinstall|-[Rr]einstall) + reinstall=true + ;; + --ci) + ci=true + ;; + --verbose|-Verbose) + verbose=true + ;; + --) + shift + break + ;; + *) + break + ;; + esac + shift +done + +if ! __machine_has unzip; then + __error 'Missing required command: unzip' + exit 1 +fi + +if ! __machine_has curl && ! __machine_has wget; then + __error 'Missing required command. Either wget or curl is required.' + exit 1 +fi + +[ -z "${config_file:-}" ] && config_file="$repo_path/korebuild.json" +if [ -f "$config_file" ]; then + if __machine_has jq ; then + if jq '.' "$config_file" >/dev/null ; then + config_channel="$(jq -r 'select(.channel!=null) | .channel' "$config_file")" + config_tools_source="$(jq -r 'select(.toolsSource!=null) | .toolsSource' "$config_file")" + else + __warn "$config_file is invalid JSON. Its settings will be ignored." + fi + elif __machine_has python ; then + if python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'))" >/dev/null ; then + config_channel="$(python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'));print(obj['channel'] if 'channel' in obj else '')")" + config_tools_source="$(python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'));print(obj['toolsSource'] if 'toolsSource' in obj else '')")" + else + __warn "$config_file is invalid JSON. Its settings will be ignored." + fi + else + __warn 'Missing required command: jq or pyton. Could not parse the JSON file. Its settings will be ignored.' + fi + + [ ! -z "${config_channel:-}" ] && channel="$config_channel" + [ ! -z "${config_tools_source:-}" ] && tools_source="$config_tools_source" +fi + +[ -z "$channel" ] && channel='dev' +[ -z "$tools_source" ] && tools_source='https://aspnetcore.blob.core.windows.net/buildtools' + +get_korebuild +set_korebuildsettings "$tools_source" "$DOTNET_HOME" "$repo_path" "$config_file" "$ci" +invoke_korebuild_command "$command" "$@" diff --git a/samples/ChatSample/ChatSample.csproj b/samples/ChatSample/ChatSample.csproj index d0e04d3bc..562340c05 100644 --- a/samples/ChatSample/ChatSample.csproj +++ b/samples/ChatSample/ChatSample.csproj @@ -1,24 +1,16 @@ - netcoreapp2.0 - false + netcoreapp2.1 + chatsample - - - - - - - - - - + + + - diff --git a/samples/ChatSample/Controllers/SignalRController.cs b/samples/ChatSample/Controllers/SignalRController.cs deleted file mode 100644 index 984292f66..000000000 --- a/samples/ChatSample/Controllers/SignalRController.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Security.Claims; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.SignalR; - -namespace ChatSample -{ - [Route("signalr")] - public class SignalRController : Controller - { - private readonly EndpointProvider _endpointProvider; - private readonly TokenProvider _tokenProvider; - - public SignalRController(EndpointProvider endpointProvider, TokenProvider tokenProvider) - { - _endpointProvider = endpointProvider; - _tokenProvider = tokenProvider; - } - - [HttpGet("{hubName}")] - public IActionResult GenerateJwtBearer(string hubName) - { - return new OkObjectResult( - new - { - serviceUrl = _endpointProvider.GetClientEndpoint(hubName), - accessToken = _tokenProvider.GenerateClientAccessToken(hubName, - new[] - { - new Claim(ClaimTypes.Name, "username"), - new Claim(ClaimTypes.NameIdentifier, "userId") - }) - }); - } - } -} \ No newline at end of file diff --git a/samples/ChatSample/Hub/Chat.cs b/samples/ChatSample/Hub/Chat.cs index e143ae1ac..97b767349 100644 --- a/samples/ChatSample/Hub/Chat.cs +++ b/samples/ChatSample/Hub/Chat.cs @@ -13,10 +13,9 @@ public void BroadcastMessage(string name, string message) Clients.All.SendAsync("broadcastMessage", name, message); } - [Authorize] public void Echo(string name, string message) { - Clients.Client(Context.ConnectionId).SendAsync("echo", name, message + " (echo from server)"); + Clients.Client(Context.ConnectionId).SendAsync("echo", name, $"{message} (echo from server, Client IP: {Context.GetHttpContext().Connection.RemoteIpAddress})"); } } } diff --git a/samples/ChatSample/Hub/NotificationHub.cs b/samples/ChatSample/Hub/NotificationHub.cs new file mode 100644 index 000000000..53e34c58c --- /dev/null +++ b/samples/ChatSample/Hub/NotificationHub.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.SignalR; + +namespace ChatSample +{ + public class NotificationHub : Hub + { + + } +} \ No newline at end of file diff --git a/samples/ChatSample/Program.cs b/samples/ChatSample/Program.cs index 908f63c45..0e0ba6594 100644 --- a/samples/ChatSample/Program.cs +++ b/samples/ChatSample/Program.cs @@ -2,7 +2,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.IO; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace ChatSample @@ -11,20 +13,11 @@ public class Program { public static void Main(string[] args) { - new WebHostBuilder() - .UseSetting(WebHostDefaults.PreventHostingStartupKey, "true") - .ConfigureLogging((context, factory) => - { - factory.AddConfiguration(context.Configuration.GetSection("Logging")); - factory.AddConsole(); - factory.AddDebug(); - }) - .UseKestrel() - .UseUrls("http://*:5050/") - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseStartup() - .Build() - .Run(); + CreateWebHostBuilder(args).Build().Run(); } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args) + .UseStartup(); } } diff --git a/samples/ChatSample/Properties/launchSettings.json b/samples/ChatSample/Properties/launchSettings.json new file mode 100644 index 000000000..699bf3be8 --- /dev/null +++ b/samples/ChatSample/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:8080", + "sslPort": 0 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "ChatSample": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "http://localhost:5050", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/samples/ChatSample/Startup.cs b/samples/ChatSample/Startup.cs index c7ea21da9..bf06512b7 100644 --- a/samples/ChatSample/Startup.cs +++ b/samples/ChatSample/Startup.cs @@ -1,9 +1,10 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Security.Claims; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.Azure.SignalR; +using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -11,43 +12,29 @@ namespace ChatSample { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); - - builder.AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); - services.AddAzureSignalR(); - - var connStr = Configuration["AzureSignalR:ConnectionString"]; - services.AddSingleton(typeof(TokenProvider), - CloudSignalR.CreateTokenProviderFromConnectionString(connStr)); - services.AddSingleton(typeof(EndpointProvider), - CloudSignalR.CreateEndpointProviderFromConnectionString(connStr)); - - var timeService = - new TimeService(CloudSignalR.CreateHubProxyFromConnectionString(connStr)); - services.AddSingleton(typeof(TimeService), timeService); + services.AddSignalR() + .AddAzureSignalR(); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvc(); app.UseFileServer(); - app.UseAzureSignalR(Configuration["AzureSignalR:ConnectionString"], - builder => { builder.UseHub(); }); + app.UseAzureSignalR(routes => + { + routes.MapHub("/chat"); + routes.MapHub("/notifications"); + }); } } } diff --git a/samples/ChatSample/TimeService.cs b/samples/ChatSample/TimeService.cs deleted file mode 100644 index 73db5bfa7..000000000 --- a/samples/ChatSample/TimeService.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Globalization; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Azure.SignalR; - -namespace ChatSample -{ - public class TimeService - { - private readonly HubProxy _hubProxy; - private readonly Timer _timer; - - public TimeService(HubProxy hubProxy) - { - _hubProxy = hubProxy ?? throw new ArgumentNullException(nameof(hubProxy)); - _timer = new Timer(Run, this, 100, 60 * 1000); - } - - private static void Run(object state) - { - _ = ((TimeService) state).Broadcast(); - } - - private async Task Broadcast() - { - await _hubProxy.Clients.All.SendAsync("broadcastMessage", - new object[] - { - "_BROADCAST_", - DateTime.UtcNow.ToString(CultureInfo.InvariantCulture) - }); - } - } -} diff --git a/samples/ChatSample/appsettings.Development.json b/samples/ChatSample/appsettings.Development.json deleted file mode 100644 index 19b8c1529..000000000 --- a/samples/ChatSample/appsettings.Development.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Debug" - } - }, - "Console": { - "LogLevel": { - "Default": "Debug" - } - } - } -} diff --git a/samples/ChatSample/appsettings.json b/samples/ChatSample/appsettings.json index be314d44b..ee97d338a 100644 --- a/samples/ChatSample/appsettings.json +++ b/samples/ChatSample/appsettings.json @@ -1,18 +1,7 @@ { - "AzureSignalR": { - "ConnectionString": "Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;" - }, - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Debug" - } - }, - "Console": { - "LogLevel": { - "Default": "Debug" - } + "Azure": { + "SignalR": { + "ConnectionString": "" } } } diff --git a/samples/ChatSample/package-lock.json b/samples/ChatSample/package-lock.json new file mode 100644 index 000000000..287364b9a --- /dev/null +++ b/samples/ChatSample/package-lock.json @@ -0,0 +1,92 @@ +{ + "name": "chatsample", + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "@aspnet/signalr": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@aspnet/signalr/-/signalr-1.0.0.tgz", + "integrity": "sha512-7fXNdSTnp2y7a3i7BnvBpQpDEoG71DNq1J/Caowr+3v/nzGivnJApRg40VgBp6FlyeJqoBEQO/QuDPE0kTpczg==" + }, + "@aspnet/signalr-protocol-msgpack": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@aspnet/signalr-protocol-msgpack/-/signalr-protocol-msgpack-1.0.0.tgz", + "integrity": "sha512-gaE7tJWZVbKW/hr2HzAM3d0l3Nfa/sxtlLWHxrhIDN7SxT/+9rMBEYMoZfuYt5yovJE6ACYDPeqsj7US5xyBcQ==", + "requires": { + "msgpack5": "4.2.0" + } + }, + "bl": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-2.0.0.tgz", + "integrity": "sha512-nqdgzTR3A/DdvWXolezBa/lJbF3+e/KXarzx5LmQGZP/jbgeOmC+fi2v9iaJllcnVQBOh6WTK+CuDlbsvdX22Q==", + "requires": { + "readable-stream": "2.3.6", + "safe-buffer": "5.1.2" + } + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "msgpack5": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/msgpack5/-/msgpack5-4.2.0.tgz", + "integrity": "sha512-tQkRlwO4f3/E8Kq5qm6PcVw+J+K4+U/XNqeD9Ebo1qVsrjkcKb2FfmdtuuIslw42CGT+K3ZVKAvKfSPp3QRplQ==", + "requires": { + "bl": "2.0.0", + "inherits": "2.0.3", + "readable-stream": "2.3.6", + "safe-buffer": "5.1.2" + } + }, + "process-nextick-args": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz", + "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==" + }, + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "1.0.0", + "process-nextick-args": "2.0.0", + "safe-buffer": "5.1.2", + "string_decoder": "1.1.1", + "util-deprecate": "1.0.2" + } + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "5.1.2" + } + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + } + } +} diff --git a/samples/ChatSample/package.json b/samples/ChatSample/package.json new file mode 100644 index 000000000..7fdb6bd96 --- /dev/null +++ b/samples/ChatSample/package.json @@ -0,0 +1,8 @@ +{ + "name": "chatsample", + "private": true, + "dependencies": { + "@aspnet/signalr": "^1.0.0", + "@aspnet/signalr-protocol-msgpack": "^1.0.0" + } +} diff --git a/samples/ChatSample/wwwroot/index.html b/samples/ChatSample/wwwroot/index.html index 2fe13b979..afca852e9 100644 --- a/samples/ChatSample/wwwroot/index.html +++ b/samples/ChatSample/wwwroot/index.html @@ -38,7 +38,9 @@

+ + - \ No newline at end of file + diff --git a/samples/ChatSample/wwwroot/scripts/msgpack5.js b/samples/ChatSample/wwwroot/scripts/msgpack5.js new file mode 100644 index 000000000..f45be5606 --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/msgpack5.js @@ -0,0 +1,7566 @@ +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.msgpack5 = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o= 0, 'must have a non-negative type') + assert(decode, 'must have a decode function') + + decodingTypes.push({ + type: type, decode: decode + }) + + return this + } + + function register (type, constructor, encode, decode) { + assert(constructor, 'must have a constructor') + assert(encode, 'must have an encode function') + assert(type >= 0, 'must have a non-negative type') + assert(decode, 'must have a decode function') + + function check (obj) { + return (obj instanceof constructor) + } + + function reEncode (obj) { + var buf = bl() + var header = Buffer.allocUnsafe(1) + + header.writeInt8(type, 0) + + buf.append(header) + buf.append(encode(obj)) + + return buf + } + + this.registerEncoder(check, reEncode) + this.registerDecoder(type, decode) + + return this + } + + return { + encode: buildEncode(encodingTypes, options.forceFloat64, options.compatibilityMode, options.disableTimestampEncoding), + decode: buildDecode(decodingTypes), + register: register, + registerEncoder: registerEncoder, + registerDecoder: registerDecoder, + encoder: streams.encoder, + decoder: streams.decoder, + // needed for levelup support + buffer: true, + type: 'msgpack5', + IncompleteBufferError: buildDecode.IncompleteBufferError + } +} + +module.exports = msgpack + +},{"./lib/decoder":2,"./lib/encoder":3,"./lib/streams":4,"assert":5,"bl":7,"safe-buffer":28}],2:[function(require,module,exports){ +var bl = require('bl') +var util = require('util') + +function IncompleteBufferError (message) { + Error.call(this) // super constructor + if (Error.captureStackTrace) { + Error.captureStackTrace(this, this.constructor) // super helper method to include stack trace in error object + } + this.name = this.constructor.name + this.message = message || 'unable to decode' +} + +util.inherits(IncompleteBufferError, Error) + +module.exports = function buildDecode (decodingTypes) { + return decode + + function getSize (first) { + switch (first) { + case 0xc4: + return 2 + case 0xc5: + return 3 + case 0xc6: + return 5 + case 0xc7: + return 3 + case 0xc8: + return 4 + case 0xc9: + return 6 + case 0xca: + return 5 + case 0xcb: + return 9 + case 0xcc: + return 2 + case 0xcd: + return 3 + case 0xce: + return 5 + case 0xcf: + return 9 + case 0xd0: + return 2 + case 0xd1: + return 3 + case 0xd2: + return 5 + case 0xd3: + return 9 + case 0xd4: + return 3 + case 0xd5: + return 4 + case 0xd6: + return 6 + case 0xd7: + return 10 + case 0xd8: + return 18 + case 0xd9: + return 2 + case 0xda: + return 3 + case 0xdb: + return 5 + case 0xde: + return 3 + default: + return -1 + } + } + + function hasMinBufferSize (first, length) { + var size = getSize(first) + + if (size !== -1 && length < size) { + return false + } else { + return true + } + } + + function isValidDataSize (dataLength, bufLength, headerLength) { + return bufLength >= headerLength + dataLength + } + + function buildDecodeResult (value, bytesConsumed) { + return { + value: value, + bytesConsumed: bytesConsumed + } + } + + function decode (buf) { + if (!(buf instanceof bl)) { + buf = bl().append(buf) + } + + var result = tryDecode(buf) + if (result) { + buf.consume(result.bytesConsumed) + return result.value + } else { + throw new IncompleteBufferError() + } + } + + function tryDecode (buf, offset) { + offset = offset === undefined ? 0 : offset + var bufLength = buf.length - offset + if (bufLength <= 0) { + return null + } + + var first = buf.readUInt8(offset) + var length + var result = 0 + var type + var bytePos + + if (!hasMinBufferSize(first, bufLength)) { + return null + } + + switch (first) { + case 0xc0: + return buildDecodeResult(null, 1) + case 0xc2: + return buildDecodeResult(false, 1) + case 0xc3: + return buildDecodeResult(true, 1) + case 0xcc: + // 1-byte unsigned int + result = buf.readUInt8(offset + 1) + return buildDecodeResult(result, 2) + case 0xcd: + // 2-bytes BE unsigned int + result = buf.readUInt16BE(offset + 1) + return buildDecodeResult(result, 3) + case 0xce: + // 4-bytes BE unsigned int + result = buf.readUInt32BE(offset + 1) + return buildDecodeResult(result, 5) + case 0xcf: + // 8-bytes BE unsigned int + // Read long byte by byte, big-endian + for (bytePos = 7; bytePos >= 0; bytePos--) { + result += (buf.readUInt8(offset + bytePos + 1) * Math.pow(2, (8 * (7 - bytePos)))) + } + return buildDecodeResult(result, 9) + case 0xd0: + // 1-byte signed int + result = buf.readInt8(offset + 1) + return buildDecodeResult(result, 2) + case 0xd1: + // 2-bytes signed int + result = buf.readInt16BE(offset + 1) + return buildDecodeResult(result, 3) + case 0xd2: + // 4-bytes signed int + result = buf.readInt32BE(offset + 1) + return buildDecodeResult(result, 5) + case 0xd3: + result = readInt64BE(buf.slice(offset + 1, offset + 9), 0) + return buildDecodeResult(result, 9) + case 0xca: + // 4-bytes float + result = buf.readFloatBE(offset + 1) + return buildDecodeResult(result, 5) + case 0xcb: + // 8-bytes double + result = buf.readDoubleBE(offset + 1) + return buildDecodeResult(result, 9) + case 0xd9: + // strings up to 2^8 - 1 bytes + length = buf.readUInt8(offset + 1) + if (!isValidDataSize(length, bufLength, 2)) { + return null + } + result = buf.toString('utf8', offset + 2, offset + 2 + length) + return buildDecodeResult(result, 2 + length) + case 0xda: + // strings up to 2^16 - 2 bytes + length = buf.readUInt16BE(offset + 1) + if (!isValidDataSize(length, bufLength, 3)) { + return null + } + result = buf.toString('utf8', offset + 3, offset + 3 + length) + return buildDecodeResult(result, 3 + length) + case 0xdb: + // strings up to 2^32 - 4 bytes + length = buf.readUInt32BE(offset + 1) + if (!isValidDataSize(length, bufLength, 5)) { + return null + } + result = buf.toString('utf8', offset + 5, offset + 5 + length) + return buildDecodeResult(result, 5 + length) + case 0xc4: + // buffers up to 2^8 - 1 bytes + length = buf.readUInt8(offset + 1) + if (!isValidDataSize(length, bufLength, 2)) { + return null + } + result = buf.slice(offset + 2, offset + 2 + length) + return buildDecodeResult(result, 2 + length) + case 0xc5: + // buffers up to 2^16 - 1 bytes + length = buf.readUInt16BE(offset + 1) + if (!isValidDataSize(length, bufLength, 3)) { + return null + } + result = buf.slice(offset + 3, offset + 3 + length) + return buildDecodeResult(result, 3 + length) + case 0xc6: + // buffers up to 2^32 - 1 bytes + length = buf.readUInt32BE(offset + 1) + if (!isValidDataSize(length, bufLength, 5)) { + return null + } + result = buf.slice(offset + 5, offset + 5 + length) + return buildDecodeResult(result, 5 + length) + case 0xdc: + // array up to 2^16 elements - 2 bytes + if (bufLength < 3) { + return null + } + + length = buf.readUInt16BE(offset + 1) + return decodeArray(buf, offset, length, 3) + case 0xdd: + // array up to 2^32 elements - 4 bytes + if (bufLength < 5) { + return null + } + + length = buf.readUInt32BE(offset + 1) + return decodeArray(buf, offset, length, 5) + case 0xde: + // maps up to 2^16 elements - 2 bytes + length = buf.readUInt16BE(offset + 1) + return decodeMap(buf, offset, length, 3) + case 0xdf: + throw new Error('map too big to decode in JS') + case 0xd4: + return decodeFixExt(buf, offset, 1) + case 0xd5: + return decodeFixExt(buf, offset, 2) + case 0xd6: + return decodeFixExt(buf, offset, 4) + case 0xd7: + return decodeFixExt(buf, offset, 8) + case 0xd8: + return decodeFixExt(buf, offset, 16) + case 0xc7: + // ext up to 2^8 - 1 bytes + length = buf.readUInt8(offset + 1) + type = buf.readUInt8(offset + 2) + if (!isValidDataSize(length, bufLength, 3)) { + return null + } + return decodeExt(buf, offset, type, length, 3) + case 0xc8: + // ext up to 2^16 - 1 bytes + length = buf.readUInt16BE(offset + 1) + type = buf.readUInt8(offset + 3) + if (!isValidDataSize(length, bufLength, 4)) { + return null + } + return decodeExt(buf, offset, type, length, 4) + case 0xc9: + // ext up to 2^32 - 1 bytes + length = buf.readUInt32BE(offset + 1) + type = buf.readUInt8(offset + 5) + if (!isValidDataSize(length, bufLength, 6)) { + return null + } + return decodeExt(buf, offset, type, length, 6) + } + + if ((first & 0xf0) === 0x90) { + // we have an array with less than 15 elements + length = first & 0x0f + return decodeArray(buf, offset, length, 1) + } else if ((first & 0xf0) === 0x80) { + // we have a map with less than 15 elements + length = first & 0x0f + return decodeMap(buf, offset, length, 1) + } else if ((first & 0xe0) === 0xa0) { + // fixstr up to 31 bytes + length = first & 0x1f + if (isValidDataSize(length, bufLength, 1)) { + result = buf.toString('utf8', offset + 1, offset + length + 1) + return buildDecodeResult(result, length + 1) + } else { + return null + } + } else if (first >= 0xe0) { + // 5 bits negative ints + result = first - 0x100 + return buildDecodeResult(result, 1) + } else if (first < 0x80) { + // 7-bits positive ints + return buildDecodeResult(first, 1) + } else { + throw new Error('not implemented yet') + } + } + + function readInt64BE (buf, offset) { + var negate = (buf[offset] & 0x80) == 0x80 // eslint-disable-line + + if (negate) { + var carry = 1 + for (var i = offset + 7; i >= offset; i--) { + var v = (buf[i] ^ 0xff) + carry + buf[i] = v & 0xff + carry = v >> 8 + } + } + + var hi = buf.readUInt32BE(offset + 0) + var lo = buf.readUInt32BE(offset + 4) + return (hi * 4294967296 + lo) * (negate ? -1 : +1) + } + + function decodeArray (buf, offset, length, headerLength) { + var result = [] + var i + var totalBytesConsumed = 0 + + offset += headerLength + for (i = 0; i < length; i++) { + var decodeResult = tryDecode(buf, offset) + if (decodeResult) { + result.push(decodeResult.value) + offset += decodeResult.bytesConsumed + totalBytesConsumed += decodeResult.bytesConsumed + } else { + return null + } + } + return buildDecodeResult(result, headerLength + totalBytesConsumed) + } + + function decodeMap (buf, offset, length, headerLength) { + var result = {} + var key + var i + var totalBytesConsumed = 0 + + offset += headerLength + for (i = 0; i < length; i++) { + var keyResult = tryDecode(buf, offset) + if (keyResult) { + offset += keyResult.bytesConsumed + var valueResult = tryDecode(buf, offset) + if (valueResult) { + key = keyResult.value + result[key] = valueResult.value + offset += valueResult.bytesConsumed + totalBytesConsumed += (keyResult.bytesConsumed + valueResult.bytesConsumed) + } else { + return null + } + } else { + return null + } + } + return buildDecodeResult(result, headerLength + totalBytesConsumed) + } + + function decodeFixExt (buf, offset, size) { + var type = buf.readInt8(offset + 1) // Signed + return decodeExt(buf, offset, type, size, 2) + } + function decodeTimestamp (buf, size, headerSize) { + var seconds, nanoseconds + nanoseconds = 0 + + switch (size) { + case 4: + // timestamp 32 stores the number of seconds that have elapsed since 1970-01-01 00:00:00 UTC in an 32-bit unsigned integer + seconds = buf.readUInt32BE() + break + + case 8: // Timestamp 64 stores the number of seconds and nanoseconds that have elapsed + // since 1970-01-01 00:00:00 UTC in 32-bit unsigned integers, split 30/34 bits + var upper = buf.readUInt32BE() + var lower = buf.readUInt32BE(4) + nanoseconds = upper / 4 + seconds = ((upper & 0x03) * Math.pow(2, 32)) + lower // If we use bitwise operators, we get truncated to 32bits + break + + case 12: + throw new Error('timestamp 96 is not yet implemented') + } + + var millis = (seconds * 1000) + Math.round(nanoseconds / 1E6) + return buildDecodeResult(new Date(millis), size + headerSize) + } + + function decodeExt (buf, offset, type, size, headerSize) { + var i, + toDecode + + offset += headerSize + + // Pre-defined + if (type < 0) { // Reserved for future extensions + switch (type) { + case -1: // Tiemstamp https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type + toDecode = buf.slice(offset, offset + size) + return decodeTimestamp(toDecode, size, headerSize) + } + } + + for (i = 0; i < decodingTypes.length; i++) { + if (type === decodingTypes[i].type) { + toDecode = buf.slice(offset, offset + size) + var value = decodingTypes[i].decode(toDecode) + return buildDecodeResult(value, headerSize + size) + } + } + + throw new Error('unable to find ext type ' + type) + } +} + +module.exports.IncompleteBufferError = IncompleteBufferError + +},{"bl":7,"util":33}],3:[function(require,module,exports){ +'use strict' + +var Buffer = require('safe-buffer').Buffer +var bl = require('bl') +var TOLERANCE = 0.1 + +module.exports = function buildEncode (encodingTypes, forceFloat64, compatibilityMode, disableTimestampEncoding) { + function encode (obj, avoidSlice) { + var buf, + len + + if (obj === undefined) { + throw new Error('undefined is not encodable in msgpack!') + } else if (obj === null) { + buf = Buffer.allocUnsafe(1) + buf[0] = 0xc0 + } else if (obj === true) { + buf = Buffer.allocUnsafe(1) + buf[0] = 0xc3 + } else if (obj === false) { + buf = Buffer.allocUnsafe(1) + buf[0] = 0xc2 + } else if (typeof obj === 'string') { + len = Buffer.byteLength(obj) + if (len < 32) { + buf = Buffer.allocUnsafe(1 + len) + buf[0] = 0xa0 | len + if (len > 0) { + buf.write(obj, 1) + } + } else if (len <= 0xff && !compatibilityMode) { + // str8, but only when not in compatibility mode + buf = Buffer.allocUnsafe(2 + len) + buf[0] = 0xd9 + buf[1] = len + buf.write(obj, 2) + } else if (len <= 0xffff) { + buf = Buffer.allocUnsafe(3 + len) + buf[0] = 0xda + buf.writeUInt16BE(len, 1) + buf.write(obj, 3) + } else { + buf = Buffer.allocUnsafe(5 + len) + buf[0] = 0xdb + buf.writeUInt32BE(len, 1) + buf.write(obj, 5) + } + } else if (obj && (obj.readUInt32LE || obj instanceof Uint8Array)) { + if (obj instanceof Uint8Array) { + obj = Buffer.from(obj) + } + // weird hack to support Buffer + // and Buffer-like objects + if (obj.length <= 0xff) { + buf = Buffer.allocUnsafe(2) + buf[0] = 0xc4 + buf[1] = obj.length + } else if (obj.length <= 0xffff) { + buf = Buffer.allocUnsafe(3) + buf[0] = 0xc5 + buf.writeUInt16BE(obj.length, 1) + } else { + buf = Buffer.allocUnsafe(5) + buf[0] = 0xc6 + buf.writeUInt32BE(obj.length, 1) + } + + buf = bl([buf, obj]) + } else if (Array.isArray(obj)) { + if (obj.length < 16) { + buf = Buffer.allocUnsafe(1) + buf[0] = 0x90 | obj.length + } else if (obj.length < 65536) { + buf = Buffer.allocUnsafe(3) + buf[0] = 0xdc + buf.writeUInt16BE(obj.length, 1) + } else { + buf = Buffer.allocUnsafe(5) + buf[0] = 0xdd + buf.writeUInt32BE(obj.length, 1) + } + + buf = obj.reduce(function (acc, obj) { + acc.append(encode(obj, true)) + return acc + }, bl().append(buf)) + } else if (!disableTimestampEncoding && typeof obj.getDate === 'function') { + return encodeDate(obj) + } else if (typeof obj === 'object') { + buf = encodeExt(obj) || encodeObject(obj) + } else if (typeof obj === 'number') { + if (isFloat(obj)) { + return encodeFloat(obj, forceFloat64) + } else if (obj >= 0) { + if (obj < 128) { + buf = Buffer.allocUnsafe(1) + buf[0] = obj + } else if (obj < 256) { + buf = Buffer.allocUnsafe(2) + buf[0] = 0xcc + buf[1] = obj + } else if (obj < 65536) { + buf = Buffer.allocUnsafe(3) + buf[0] = 0xcd + buf.writeUInt16BE(obj, 1) + } else if (obj <= 0xffffffff) { + buf = Buffer.allocUnsafe(5) + buf[0] = 0xce + buf.writeUInt32BE(obj, 1) + } else if (obj <= 9007199254740991) { + buf = Buffer.allocUnsafe(9) + buf[0] = 0xcf + write64BitUint(buf, obj) + } else { + return encodeFloat(obj, true) + } + } else { + if (obj >= -32) { + buf = Buffer.allocUnsafe(1) + buf[0] = 0x100 + obj + } else if (obj >= -128) { + buf = Buffer.allocUnsafe(2) + buf[0] = 0xd0 + buf.writeInt8(obj, 1) + } else if (obj >= -32768) { + buf = Buffer.allocUnsafe(3) + buf[0] = 0xd1 + buf.writeInt16BE(obj, 1) + } else if (obj > -214748365) { + buf = Buffer.allocUnsafe(5) + buf[0] = 0xd2 + buf.writeInt32BE(obj, 1) + } else if (obj >= -9007199254740991) { + buf = Buffer.allocUnsafe(9) + buf[0] = 0xd3 + write64BitInt(buf, 1, obj) + } else { + return encodeFloat(obj, true) + } + } + } + + if (!buf) { + throw new Error('not implemented yet') + } + + if (avoidSlice) { + return buf + } else { + return buf.slice() + } + } + + function encodeDate (dt) { + var encoded + var millis = dt * 1 + var seconds = Math.floor(millis / 1000) + var nanos = (millis - (seconds * 1000)) * 1E6 + + if (nanos || seconds > 0xFFFFFFFF) { + // Timestamp64 + encoded = new Buffer(10) + encoded[0] = 0xd7 + encoded[1] = -1 + + var upperNanos = ((nanos * 4)) + var upperSeconds = seconds / Math.pow(2, 32) + var upper = (upperNanos + upperSeconds) & 0xFFFFFFFF + var lower = seconds & 0xFFFFFFFF + + encoded.writeInt32BE(upper, 2) + encoded.writeInt32BE(lower, 6) + } else { + // Timestamp32 + encoded = new Buffer(6) + encoded[0] = 0xd6 + encoded[1] = -1 + encoded.writeUInt32BE(Math.floor(millis / 1000), 2) + } + return bl().append(encoded) + } + + function encodeExt (obj) { + var i + var encoded + var length = -1 + var headers = [] + + for (i = 0; i < encodingTypes.length; i++) { + if (encodingTypes[i].check(obj)) { + encoded = encodingTypes[i].encode(obj) + break + } + } + + if (!encoded) { + return null + } + + // we subtract 1 because the length does not + // include the type + length = encoded.length - 1 + + if (length === 1) { + headers.push(0xd4) + } else if (length === 2) { + headers.push(0xd5) + } else if (length === 4) { + headers.push(0xd6) + } else if (length === 8) { + headers.push(0xd7) + } else if (length === 16) { + headers.push(0xd8) + } else if (length < 256) { + headers.push(0xc7) + headers.push(length) + } else if (length < 0x10000) { + headers.push(0xc8) + headers.push(length >> 8) + headers.push(length & 0x00ff) + } else { + headers.push(0xc9) + headers.push(length >> 24) + headers.push((length >> 16) & 0x000000ff) + headers.push((length >> 8) & 0x000000ff) + headers.push(length & 0x000000ff) + } + + return bl().append(Buffer.from(headers)).append(encoded) + } + + function encodeObject (obj) { + var acc = [] + var length = 0 + var key + var header + + for (key in obj) { + if (obj.hasOwnProperty(key) && + obj[key] !== undefined && + typeof obj[key] !== 'function') { + ++length + acc.push(encode(key, true)) + acc.push(encode(obj[key], true)) + } + } + + if (length < 16) { + header = Buffer.allocUnsafe(1) + header[0] = 0x80 | length + } else { + header = Buffer.allocUnsafe(3) + header[0] = 0xde + header.writeUInt16BE(length, 1) + } + + acc.unshift(header) + + var result = acc.reduce(function (list, buf) { + return list.append(buf) + }, bl()) + + return result + } + + return encode +} + +function write64BitUint (buf, obj) { + // Write long byte by byte, in big-endian order + for (var currByte = 7; currByte >= 0; currByte--) { + buf[currByte + 1] = (obj & 0xff) + obj = obj / 256 + } +} + +function write64BitInt (buf, offset, num) { + var negate = num < 0 + + if (negate) { + num = Math.abs(num) + } + + var lo = num % 4294967296 + var hi = num / 4294967296 + buf.writeUInt32BE(Math.floor(hi), offset + 0) + buf.writeUInt32BE(lo, offset + 4) + + if (negate) { + var carry = 1 + for (var i = offset + 7; i >= offset; i--) { + var v = (buf[i] ^ 0xff) + carry + buf[i] = v & 0xff + carry = v >> 8 + } + } +} + +function isFloat (n) { + return n !== Math.floor(n) +} + +function encodeFloat (obj, forceFloat64) { + var buf + + buf = Buffer.allocUnsafe(5) + buf[0] = 0xca + buf.writeFloatBE(obj, 1) + + // FIXME is there a way to check if a + // value fits in a float? + if (forceFloat64 || Math.abs(obj - buf.readFloatBE(1)) > TOLERANCE) { + buf = Buffer.allocUnsafe(9) + buf[0] = 0xcb + buf.writeDoubleBE(obj, 1) + } + + return buf +} + +},{"bl":7,"safe-buffer":28}],4:[function(require,module,exports){ +'use strict' + +var Transform = require('readable-stream').Transform +var inherits = require('inherits') +var bl = require('bl') + +function Base (opts) { + opts = opts || {} + + opts.objectMode = true + opts.highWaterMark = 16 + + Transform.call(this, opts) + + this._msgpack = opts.msgpack +} + +inherits(Base, Transform) + +function Encoder (opts) { + if (!(this instanceof Encoder)) { + opts = opts || {} + opts.msgpack = this + return new Encoder(opts) + } + + Base.call(this, opts) +} + +inherits(Encoder, Base) + +Encoder.prototype._transform = function (obj, enc, done) { + var buf = null + + try { + buf = this._msgpack.encode(obj).slice(0) + } catch (err) { + this.emit('error', err) + return done() + } + + this.push(buf) + done() +} + +function Decoder (opts) { + if (!(this instanceof Decoder)) { + opts = opts || {} + opts.msgpack = this + return new Decoder(opts) + } + + Base.call(this, opts) + + this._chunks = bl() +} + +inherits(Decoder, Base) + +Decoder.prototype._transform = function (buf, enc, done) { + if (buf) { + this._chunks.append(buf) + } + + try { + var result = this._msgpack.decode(this._chunks) + this.push(result) + } catch (err) { + if (err instanceof this._msgpack.IncompleteBufferError) { + done() + } else { + this.emit('error', err) + } + return + } + + if (this._chunks.length > 0) { + this._transform(null, enc, done) + } else { + done() + } +} + +module.exports.decoder = Decoder +module.exports.encoder = Encoder + +},{"bl":7,"inherits":13,"readable-stream":27}],5:[function(require,module,exports){ +(function (global){ +'use strict'; + +// compare and isBuffer taken from https://github.com/feross/buffer/blob/680e9e5e488f22aac27599a57dc844a6315928dd/index.js +// original notice: + +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +function compare(a, b) { + if (a === b) { + return 0; + } + + var x = a.length; + var y = b.length; + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i]; + y = b[i]; + break; + } + } + + if (x < y) { + return -1; + } + if (y < x) { + return 1; + } + return 0; +} +function isBuffer(b) { + if (global.Buffer && typeof global.Buffer.isBuffer === 'function') { + return global.Buffer.isBuffer(b); + } + return !!(b != null && b._isBuffer); +} + +// based on node assert, original notice: + +// http://wiki.commonjs.org/wiki/Unit_Testing/1.0 +// +// THIS IS NOT TESTED NOR LIKELY TO WORK OUTSIDE V8! +// +// Originally from narwhal.js (http://narwhaljs.org) +// Copyright (c) 2009 Thomas Robinson <280north.com> +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the 'Software'), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +var util = require('util/'); +var hasOwn = Object.prototype.hasOwnProperty; +var pSlice = Array.prototype.slice; +var functionsHaveNames = (function () { + return function foo() {}.name === 'foo'; +}()); +function pToString (obj) { + return Object.prototype.toString.call(obj); +} +function isView(arrbuf) { + if (isBuffer(arrbuf)) { + return false; + } + if (typeof global.ArrayBuffer !== 'function') { + return false; + } + if (typeof ArrayBuffer.isView === 'function') { + return ArrayBuffer.isView(arrbuf); + } + if (!arrbuf) { + return false; + } + if (arrbuf instanceof DataView) { + return true; + } + if (arrbuf.buffer && arrbuf.buffer instanceof ArrayBuffer) { + return true; + } + return false; +} +// 1. The assert module provides functions that throw +// AssertionError's when particular conditions are not met. The +// assert module must conform to the following interface. + +var assert = module.exports = ok; + +// 2. The AssertionError is defined in assert. +// new assert.AssertionError({ message: message, +// actual: actual, +// expected: expected }) + +var regex = /\s*function\s+([^\(\s]*)\s*/; +// based on https://github.com/ljharb/function.prototype.name/blob/adeeeec8bfcc6068b187d7d9fb3d5bb1d3a30899/implementation.js +function getName(func) { + if (!util.isFunction(func)) { + return; + } + if (functionsHaveNames) { + return func.name; + } + var str = func.toString(); + var match = str.match(regex); + return match && match[1]; +} +assert.AssertionError = function AssertionError(options) { + this.name = 'AssertionError'; + this.actual = options.actual; + this.expected = options.expected; + this.operator = options.operator; + if (options.message) { + this.message = options.message; + this.generatedMessage = false; + } else { + this.message = getMessage(this); + this.generatedMessage = true; + } + var stackStartFunction = options.stackStartFunction || fail; + if (Error.captureStackTrace) { + Error.captureStackTrace(this, stackStartFunction); + } else { + // non v8 browsers so we can have a stacktrace + var err = new Error(); + if (err.stack) { + var out = err.stack; + + // try to strip useless frames + var fn_name = getName(stackStartFunction); + var idx = out.indexOf('\n' + fn_name); + if (idx >= 0) { + // once we have located the function frame + // we need to strip out everything before it (and its line) + var next_line = out.indexOf('\n', idx + 1); + out = out.substring(next_line + 1); + } + + this.stack = out; + } + } +}; + +// assert.AssertionError instanceof Error +util.inherits(assert.AssertionError, Error); + +function truncate(s, n) { + if (typeof s === 'string') { + return s.length < n ? s : s.slice(0, n); + } else { + return s; + } +} +function inspect(something) { + if (functionsHaveNames || !util.isFunction(something)) { + return util.inspect(something); + } + var rawname = getName(something); + var name = rawname ? ': ' + rawname : ''; + return '[Function' + name + ']'; +} +function getMessage(self) { + return truncate(inspect(self.actual), 128) + ' ' + + self.operator + ' ' + + truncate(inspect(self.expected), 128); +} + +// At present only the three keys mentioned above are used and +// understood by the spec. Implementations or sub modules can pass +// other keys to the AssertionError's constructor - they will be +// ignored. + +// 3. All of the following functions must throw an AssertionError +// when a corresponding condition is not met, with a message that +// may be undefined if not provided. All assertion methods provide +// both the actual and expected values to the assertion error for +// display purposes. + +function fail(actual, expected, message, operator, stackStartFunction) { + throw new assert.AssertionError({ + message: message, + actual: actual, + expected: expected, + operator: operator, + stackStartFunction: stackStartFunction + }); +} + +// EXTENSION! allows for well behaved errors defined elsewhere. +assert.fail = fail; + +// 4. Pure assertion tests whether a value is truthy, as determined +// by !!guard. +// assert.ok(guard, message_opt); +// This statement is equivalent to assert.equal(true, !!guard, +// message_opt);. To test strictly for the value true, use +// assert.strictEqual(true, guard, message_opt);. + +function ok(value, message) { + if (!value) fail(value, true, message, '==', assert.ok); +} +assert.ok = ok; + +// 5. The equality assertion tests shallow, coercive equality with +// ==. +// assert.equal(actual, expected, message_opt); + +assert.equal = function equal(actual, expected, message) { + if (actual != expected) fail(actual, expected, message, '==', assert.equal); +}; + +// 6. The non-equality assertion tests for whether two objects are not equal +// with != assert.notEqual(actual, expected, message_opt); + +assert.notEqual = function notEqual(actual, expected, message) { + if (actual == expected) { + fail(actual, expected, message, '!=', assert.notEqual); + } +}; + +// 7. The equivalence assertion tests a deep equality relation. +// assert.deepEqual(actual, expected, message_opt); + +assert.deepEqual = function deepEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'deepEqual', assert.deepEqual); + } +}; + +assert.deepStrictEqual = function deepStrictEqual(actual, expected, message) { + if (!_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'deepStrictEqual', assert.deepStrictEqual); + } +}; + +function _deepEqual(actual, expected, strict, memos) { + // 7.1. All identical values are equivalent, as determined by ===. + if (actual === expected) { + return true; + } else if (isBuffer(actual) && isBuffer(expected)) { + return compare(actual, expected) === 0; + + // 7.2. If the expected value is a Date object, the actual value is + // equivalent if it is also a Date object that refers to the same time. + } else if (util.isDate(actual) && util.isDate(expected)) { + return actual.getTime() === expected.getTime(); + + // 7.3 If the expected value is a RegExp object, the actual value is + // equivalent if it is also a RegExp object with the same source and + // properties (`global`, `multiline`, `lastIndex`, `ignoreCase`). + } else if (util.isRegExp(actual) && util.isRegExp(expected)) { + return actual.source === expected.source && + actual.global === expected.global && + actual.multiline === expected.multiline && + actual.lastIndex === expected.lastIndex && + actual.ignoreCase === expected.ignoreCase; + + // 7.4. Other pairs that do not both pass typeof value == 'object', + // equivalence is determined by ==. + } else if ((actual === null || typeof actual !== 'object') && + (expected === null || typeof expected !== 'object')) { + return strict ? actual === expected : actual == expected; + + // If both values are instances of typed arrays, wrap their underlying + // ArrayBuffers in a Buffer each to increase performance + // This optimization requires the arrays to have the same type as checked by + // Object.prototype.toString (aka pToString). Never perform binary + // comparisons for Float*Arrays, though, since e.g. +0 === -0 but their + // bit patterns are not identical. + } else if (isView(actual) && isView(expected) && + pToString(actual) === pToString(expected) && + !(actual instanceof Float32Array || + actual instanceof Float64Array)) { + return compare(new Uint8Array(actual.buffer), + new Uint8Array(expected.buffer)) === 0; + + // 7.5 For all other Object pairs, including Array objects, equivalence is + // determined by having the same number of owned properties (as verified + // with Object.prototype.hasOwnProperty.call), the same set of keys + // (although not necessarily the same order), equivalent values for every + // corresponding key, and an identical 'prototype' property. Note: this + // accounts for both named and indexed properties on Arrays. + } else if (isBuffer(actual) !== isBuffer(expected)) { + return false; + } else { + memos = memos || {actual: [], expected: []}; + + var actualIndex = memos.actual.indexOf(actual); + if (actualIndex !== -1) { + if (actualIndex === memos.expected.indexOf(expected)) { + return true; + } + } + + memos.actual.push(actual); + memos.expected.push(expected); + + return objEquiv(actual, expected, strict, memos); + } +} + +function isArguments(object) { + return Object.prototype.toString.call(object) == '[object Arguments]'; +} + +function objEquiv(a, b, strict, actualVisitedObjects) { + if (a === null || a === undefined || b === null || b === undefined) + return false; + // if one is a primitive, the other must be same + if (util.isPrimitive(a) || util.isPrimitive(b)) + return a === b; + if (strict && Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) + return false; + var aIsArgs = isArguments(a); + var bIsArgs = isArguments(b); + if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) + return false; + if (aIsArgs) { + a = pSlice.call(a); + b = pSlice.call(b); + return _deepEqual(a, b, strict); + } + var ka = objectKeys(a); + var kb = objectKeys(b); + var key, i; + // having the same number of owned properties (keys incorporates + // hasOwnProperty) + if (ka.length !== kb.length) + return false; + //the same set of keys (although not necessarily the same order), + ka.sort(); + kb.sort(); + //~~~cheap key test + for (i = ka.length - 1; i >= 0; i--) { + if (ka[i] !== kb[i]) + return false; + } + //equivalent values for every corresponding key, and + //~~~possibly expensive deep test + for (i = ka.length - 1; i >= 0; i--) { + key = ka[i]; + if (!_deepEqual(a[key], b[key], strict, actualVisitedObjects)) + return false; + } + return true; +} + +// 8. The non-equivalence assertion tests for any deep inequality. +// assert.notDeepEqual(actual, expected, message_opt); + +assert.notDeepEqual = function notDeepEqual(actual, expected, message) { + if (_deepEqual(actual, expected, false)) { + fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); + } +}; + +assert.notDeepStrictEqual = notDeepStrictEqual; +function notDeepStrictEqual(actual, expected, message) { + if (_deepEqual(actual, expected, true)) { + fail(actual, expected, message, 'notDeepStrictEqual', notDeepStrictEqual); + } +} + + +// 9. The strict equality assertion tests strict equality, as determined by ===. +// assert.strictEqual(actual, expected, message_opt); + +assert.strictEqual = function strictEqual(actual, expected, message) { + if (actual !== expected) { + fail(actual, expected, message, '===', assert.strictEqual); + } +}; + +// 10. The strict non-equality assertion tests for strict inequality, as +// determined by !==. assert.notStrictEqual(actual, expected, message_opt); + +assert.notStrictEqual = function notStrictEqual(actual, expected, message) { + if (actual === expected) { + fail(actual, expected, message, '!==', assert.notStrictEqual); + } +}; + +function expectedException(actual, expected) { + if (!actual || !expected) { + return false; + } + + if (Object.prototype.toString.call(expected) == '[object RegExp]') { + return expected.test(actual); + } + + try { + if (actual instanceof expected) { + return true; + } + } catch (e) { + // Ignore. The instanceof check doesn't work for arrow functions. + } + + if (Error.isPrototypeOf(expected)) { + return false; + } + + return expected.call({}, actual) === true; +} + +function _tryBlock(block) { + var error; + try { + block(); + } catch (e) { + error = e; + } + return error; +} + +function _throws(shouldThrow, block, expected, message) { + var actual; + + if (typeof block !== 'function') { + throw new TypeError('"block" argument must be a function'); + } + + if (typeof expected === 'string') { + message = expected; + expected = null; + } + + actual = _tryBlock(block); + + message = (expected && expected.name ? ' (' + expected.name + ').' : '.') + + (message ? ' ' + message : '.'); + + if (shouldThrow && !actual) { + fail(actual, expected, 'Missing expected exception' + message); + } + + var userProvidedMessage = typeof message === 'string'; + var isUnwantedException = !shouldThrow && util.isError(actual); + var isUnexpectedException = !shouldThrow && actual && !expected; + + if ((isUnwantedException && + userProvidedMessage && + expectedException(actual, expected)) || + isUnexpectedException) { + fail(actual, expected, 'Got unwanted exception' + message); + } + + if ((shouldThrow && actual && expected && + !expectedException(actual, expected)) || (!shouldThrow && actual)) { + throw actual; + } +} + +// 11. Expected to throw an error: +// assert.throws(block, Error_opt, message_opt); + +assert.throws = function(block, /*optional*/error, /*optional*/message) { + _throws(true, block, error, message); +}; + +// EXTENSION! This is annoying to write outside this module. +assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) { + _throws(false, block, error, message); +}; + +assert.ifError = function(err) { if (err) throw err; }; + +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + if (hasOwn.call(obj, key)) keys.push(key); + } + return keys; +}; + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"util/":33}],6:[function(require,module,exports){ +'use strict' + +exports.byteLength = byteLength +exports.toByteArray = toByteArray +exports.fromByteArray = fromByteArray + +var lookup = [] +var revLookup = [] +var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array + +var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' +for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i] + revLookup[code.charCodeAt(i)] = i +} + +revLookup['-'.charCodeAt(0)] = 62 +revLookup['_'.charCodeAt(0)] = 63 + +function placeHoldersCount (b64) { + var len = b64.length + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // the number of equal signs (place holders) + // if there are two placeholders, than the two characters before it + // represent one byte + // if there is only one, then the three characters before it represent 2 bytes + // this is just a cheap hack to not do indexOf twice + return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 +} + +function byteLength (b64) { + // base64 is 4/3 + up to two characters of the original data + return (b64.length * 3 / 4) - placeHoldersCount(b64) +} + +function toByteArray (b64) { + var i, l, tmp, placeHolders, arr + var len = b64.length + placeHolders = placeHoldersCount(b64) + + arr = new Arr((len * 3 / 4) - placeHolders) + + // if there are placeholders, only get up to the last complete 4 chars + l = placeHolders > 0 ? len - 4 : len + + var L = 0 + + for (i = 0; i < l; i += 4) { + tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] + arr[L++] = (tmp >> 16) & 0xFF + arr[L++] = (tmp >> 8) & 0xFF + arr[L++] = tmp & 0xFF + } + + if (placeHolders === 2) { + tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4) + arr[L++] = tmp & 0xFF + } else if (placeHolders === 1) { + tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2) + arr[L++] = (tmp >> 8) & 0xFF + arr[L++] = tmp & 0xFF + } + + return arr +} + +function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] +} + +function encodeChunk (uint8, start, end) { + var tmp + var output = [] + for (var i = start; i < end; i += 3) { + tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]) + output.push(tripletToBase64(tmp)) + } + return output.join('') +} + +function fromByteArray (uint8) { + var tmp + var len = uint8.length + var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes + var output = '' + var parts = [] + var maxChunkLength = 16383 // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))) + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1] + output += lookup[tmp >> 2] + output += lookup[(tmp << 4) & 0x3F] + output += '==' + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + (uint8[len - 1]) + output += lookup[tmp >> 10] + output += lookup[(tmp >> 4) & 0x3F] + output += lookup[(tmp << 2) & 0x3F] + output += '=' + } + + parts.push(output) + + return parts.join('') +} + +},{}],7:[function(require,module,exports){ +(function (Buffer){ +var DuplexStream = require('readable-stream/duplex') + , util = require('util') + + +function BufferList (callback) { + if (!(this instanceof BufferList)) + return new BufferList(callback) + + this._bufs = [] + this.length = 0 + + if (typeof callback == 'function') { + this._callback = callback + + var piper = function piper (err) { + if (this._callback) { + this._callback(err) + this._callback = null + } + }.bind(this) + + this.on('pipe', function onPipe (src) { + src.on('error', piper) + }) + this.on('unpipe', function onUnpipe (src) { + src.removeListener('error', piper) + }) + } else { + this.append(callback) + } + + DuplexStream.call(this) +} + + +util.inherits(BufferList, DuplexStream) + + +BufferList.prototype._offset = function _offset (offset) { + var tot = 0, i = 0, _t + if (offset === 0) return [ 0, 0 ] + for (; i < this._bufs.length; i++) { + _t = tot + this._bufs[i].length + if (offset < _t || i == this._bufs.length - 1) + return [ i, offset - tot ] + tot = _t + } +} + + +BufferList.prototype.append = function append (buf) { + var i = 0 + + if (Buffer.isBuffer(buf)) { + this._appendBuffer(buf); + } else if (Array.isArray(buf)) { + for (; i < buf.length; i++) + this.append(buf[i]) + } else if (buf instanceof BufferList) { + // unwrap argument into individual BufferLists + for (; i < buf._bufs.length; i++) + this.append(buf._bufs[i]) + } else if (buf != null) { + // coerce number arguments to strings, since Buffer(number) does + // uninitialized memory allocation + if (typeof buf == 'number') + buf = buf.toString() + + this._appendBuffer(new Buffer(buf)); + } + + return this +} + + +BufferList.prototype._appendBuffer = function appendBuffer (buf) { + this._bufs.push(buf) + this.length += buf.length +} + + +BufferList.prototype._write = function _write (buf, encoding, callback) { + this._appendBuffer(buf) + + if (typeof callback == 'function') + callback() +} + + +BufferList.prototype._read = function _read (size) { + if (!this.length) + return this.push(null) + + size = Math.min(size, this.length) + this.push(this.slice(0, size)) + this.consume(size) +} + + +BufferList.prototype.end = function end (chunk) { + DuplexStream.prototype.end.call(this, chunk) + + if (this._callback) { + this._callback(null, this.slice()) + this._callback = null + } +} + + +BufferList.prototype.get = function get (index) { + return this.slice(index, index + 1)[0] +} + + +BufferList.prototype.slice = function slice (start, end) { + if (typeof start == 'number' && start < 0) + start += this.length + if (typeof end == 'number' && end < 0) + end += this.length + return this.copy(null, 0, start, end) +} + + +BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) { + if (typeof srcStart != 'number' || srcStart < 0) + srcStart = 0 + if (typeof srcEnd != 'number' || srcEnd > this.length) + srcEnd = this.length + if (srcStart >= this.length) + return dst || new Buffer(0) + if (srcEnd <= 0) + return dst || new Buffer(0) + + var copy = !!dst + , off = this._offset(srcStart) + , len = srcEnd - srcStart + , bytes = len + , bufoff = (copy && dstStart) || 0 + , start = off[1] + , l + , i + + // copy/slice everything + if (srcStart === 0 && srcEnd == this.length) { + if (!copy) { // slice, but full concat if multiple buffers + return this._bufs.length === 1 + ? this._bufs[0] + : Buffer.concat(this._bufs, this.length) + } + + // copy, need to copy individual buffers + for (i = 0; i < this._bufs.length; i++) { + this._bufs[i].copy(dst, bufoff) + bufoff += this._bufs[i].length + } + + return dst + } + + // easy, cheap case where it's a subset of one of the buffers + if (bytes <= this._bufs[off[0]].length - start) { + return copy + ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes) + : this._bufs[off[0]].slice(start, start + bytes) + } + + if (!copy) // a slice, we need something to copy in to + dst = new Buffer(len) + + for (i = off[0]; i < this._bufs.length; i++) { + l = this._bufs[i].length - start + + if (bytes > l) { + this._bufs[i].copy(dst, bufoff, start) + } else { + this._bufs[i].copy(dst, bufoff, start, start + bytes) + break + } + + bufoff += l + bytes -= l + + if (start) + start = 0 + } + + return dst +} + +BufferList.prototype.shallowSlice = function shallowSlice (start, end) { + start = start || 0 + end = end || this.length + + if (start < 0) + start += this.length + if (end < 0) + end += this.length + + var startOffset = this._offset(start) + , endOffset = this._offset(end) + , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1) + + if (endOffset[1] == 0) + buffers.pop() + else + buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1]) + + if (startOffset[1] != 0) + buffers[0] = buffers[0].slice(startOffset[1]) + + return new BufferList(buffers) +} + +BufferList.prototype.toString = function toString (encoding, start, end) { + return this.slice(start, end).toString(encoding) +} + +BufferList.prototype.consume = function consume (bytes) { + while (this._bufs.length) { + if (bytes >= this._bufs[0].length) { + bytes -= this._bufs[0].length + this.length -= this._bufs[0].length + this._bufs.shift() + } else { + this._bufs[0] = this._bufs[0].slice(bytes) + this.length -= bytes + break + } + } + return this +} + + +BufferList.prototype.duplicate = function duplicate () { + var i = 0 + , copy = new BufferList() + + for (; i < this._bufs.length; i++) + copy.append(this._bufs[i]) + + return copy +} + + +BufferList.prototype.destroy = function destroy () { + this._bufs.length = 0 + this.length = 0 + this.push(null) +} + + +;(function () { + var methods = { + 'readDoubleBE' : 8 + , 'readDoubleLE' : 8 + , 'readFloatBE' : 4 + , 'readFloatLE' : 4 + , 'readInt32BE' : 4 + , 'readInt32LE' : 4 + , 'readUInt32BE' : 4 + , 'readUInt32LE' : 4 + , 'readInt16BE' : 2 + , 'readInt16LE' : 2 + , 'readUInt16BE' : 2 + , 'readUInt16LE' : 2 + , 'readInt8' : 1 + , 'readUInt8' : 1 + } + + for (var m in methods) { + (function (m) { + BufferList.prototype[m] = function (offset) { + return this.slice(offset, offset + methods[m])[m](0) + } + }(m)) + } +}()) + + +module.exports = BufferList + +}).call(this,require("buffer").Buffer) +},{"buffer":9,"readable-stream/duplex":18,"util":33}],8:[function(require,module,exports){ + +},{}],9:[function(require,module,exports){ +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +/* eslint-disable no-proto */ + +'use strict' + +var base64 = require('base64-js') +var ieee754 = require('ieee754') + +exports.Buffer = Buffer +exports.SlowBuffer = SlowBuffer +exports.INSPECT_MAX_BYTES = 50 + +var K_MAX_LENGTH = 0x7fffffff +exports.kMaxLength = K_MAX_LENGTH + +/** + * If `Buffer.TYPED_ARRAY_SUPPORT`: + * === true Use Uint8Array implementation (fastest) + * === false Print warning and recommend using `buffer` v4.x which has an Object + * implementation (most compatible, even IE6) + * + * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, + * Opera 11.6+, iOS 4.2+. + * + * We report that the browser does not support typed arrays if the are not subclassable + * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` + * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support + * for __proto__ and has a buggy typed array implementation. + */ +Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport() + +if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && + typeof console.error === 'function') { + console.error( + 'This browser lacks typed array (Uint8Array) support which is required by ' + + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' + ) +} + +function typedArraySupport () { + // Can typed array instances can be augmented? + try { + var arr = new Uint8Array(1) + arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }} + return arr.foo() === 42 + } catch (e) { + return false + } +} + +function createBuffer (length) { + if (length > K_MAX_LENGTH) { + throw new RangeError('Invalid typed array length') + } + // Return an augmented `Uint8Array` instance + var buf = new Uint8Array(length) + buf.__proto__ = Buffer.prototype + return buf +} + +/** + * The Buffer constructor returns instances of `Uint8Array` that have their + * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of + * `Uint8Array`, so the returned instances will have all the node `Buffer` methods + * and the `Uint8Array` methods. Square bracket notation works as expected -- it + * returns a single octet. + * + * The `Uint8Array` prototype remains unmodified. + */ + +function Buffer (arg, encodingOrOffset, length) { + // Common case. + if (typeof arg === 'number') { + if (typeof encodingOrOffset === 'string') { + throw new Error( + 'If encoding is specified then the first argument must be a string' + ) + } + return allocUnsafe(arg) + } + return from(arg, encodingOrOffset, length) +} + +// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 +if (typeof Symbol !== 'undefined' && Symbol.species && + Buffer[Symbol.species] === Buffer) { + Object.defineProperty(Buffer, Symbol.species, { + value: null, + configurable: true, + enumerable: false, + writable: false + }) +} + +Buffer.poolSize = 8192 // not used by this implementation + +function from (value, encodingOrOffset, length) { + if (typeof value === 'number') { + throw new TypeError('"value" argument must not be a number') + } + + if (isArrayBuffer(value)) { + return fromArrayBuffer(value, encodingOrOffset, length) + } + + if (typeof value === 'string') { + return fromString(value, encodingOrOffset) + } + + return fromObject(value) +} + +/** + * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError + * if value is a number. + * Buffer.from(str[, encoding]) + * Buffer.from(array) + * Buffer.from(buffer) + * Buffer.from(arrayBuffer[, byteOffset[, length]]) + **/ +Buffer.from = function (value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length) +} + +// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: +// https://github.com/feross/buffer/pull/148 +Buffer.prototype.__proto__ = Uint8Array.prototype +Buffer.__proto__ = Uint8Array + +function assertSize (size) { + if (typeof size !== 'number') { + throw new TypeError('"size" argument must be a number') + } else if (size < 0) { + throw new RangeError('"size" argument must not be negative') + } +} + +function alloc (size, fill, encoding) { + assertSize(size) + if (size <= 0) { + return createBuffer(size) + } + if (fill !== undefined) { + // Only pay attention to encoding if it's a string. This + // prevents accidentally sending in a number that would + // be interpretted as a start offset. + return typeof encoding === 'string' + ? createBuffer(size).fill(fill, encoding) + : createBuffer(size).fill(fill) + } + return createBuffer(size) +} + +/** + * Creates a new filled Buffer instance. + * alloc(size[, fill[, encoding]]) + **/ +Buffer.alloc = function (size, fill, encoding) { + return alloc(size, fill, encoding) +} + +function allocUnsafe (size) { + assertSize(size) + return createBuffer(size < 0 ? 0 : checked(size) | 0) +} + +/** + * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. + * */ +Buffer.allocUnsafe = function (size) { + return allocUnsafe(size) +} +/** + * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. + */ +Buffer.allocUnsafeSlow = function (size) { + return allocUnsafe(size) +} + +function fromString (string, encoding) { + if (typeof encoding !== 'string' || encoding === '') { + encoding = 'utf8' + } + + if (!Buffer.isEncoding(encoding)) { + throw new TypeError('"encoding" must be a valid string encoding') + } + + var length = byteLength(string, encoding) | 0 + var buf = createBuffer(length) + + var actual = buf.write(string, encoding) + + if (actual !== length) { + // Writing a hex string, for example, that contains invalid characters will + // cause everything after the first invalid character to be ignored. (e.g. + // 'abxxcd' will be treated as 'ab') + buf = buf.slice(0, actual) + } + + return buf +} + +function fromArrayLike (array) { + var length = array.length < 0 ? 0 : checked(array.length) | 0 + var buf = createBuffer(length) + for (var i = 0; i < length; i += 1) { + buf[i] = array[i] & 255 + } + return buf +} + +function fromArrayBuffer (array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) { + throw new RangeError('\'offset\' is out of bounds') + } + + if (array.byteLength < byteOffset + (length || 0)) { + throw new RangeError('\'length\' is out of bounds') + } + + var buf + if (byteOffset === undefined && length === undefined) { + buf = new Uint8Array(array) + } else if (length === undefined) { + buf = new Uint8Array(array, byteOffset) + } else { + buf = new Uint8Array(array, byteOffset, length) + } + + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype + return buf +} + +function fromObject (obj) { + if (Buffer.isBuffer(obj)) { + var len = checked(obj.length) | 0 + var buf = createBuffer(len) + + if (buf.length === 0) { + return buf + } + + obj.copy(buf, 0, 0, len) + return buf + } + + if (obj) { + if (isArrayBufferView(obj) || 'length' in obj) { + if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { + return createBuffer(0) + } + return fromArrayLike(obj) + } + + if (obj.type === 'Buffer' && Array.isArray(obj.data)) { + return fromArrayLike(obj.data) + } + } + + throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') +} + +function checked (length) { + // Note: cannot use `length < K_MAX_LENGTH` here because that fails when + // length is NaN (which is otherwise coerced to zero.) + if (length >= K_MAX_LENGTH) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') + } + return length | 0 +} + +function SlowBuffer (length) { + if (+length != length) { // eslint-disable-line eqeqeq + length = 0 + } + return Buffer.alloc(+length) +} + +Buffer.isBuffer = function isBuffer (b) { + return b != null && b._isBuffer === true +} + +Buffer.compare = function compare (a, b) { + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + throw new TypeError('Arguments must be Buffers') + } + + if (a === b) return 0 + + var x = a.length + var y = b.length + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i] + y = b[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +} + +Buffer.isEncoding = function isEncoding (encoding) { + switch (String(encoding).toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'latin1': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return true + default: + return false + } +} + +Buffer.concat = function concat (list, length) { + if (!Array.isArray(list)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + + if (list.length === 0) { + return Buffer.alloc(0) + } + + var i + if (length === undefined) { + length = 0 + for (i = 0; i < list.length; ++i) { + length += list[i].length + } + } + + var buffer = Buffer.allocUnsafe(length) + var pos = 0 + for (i = 0; i < list.length; ++i) { + var buf = list[i] + if (!Buffer.isBuffer(buf)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + buf.copy(buffer, pos) + pos += buf.length + } + return buffer +} + +function byteLength (string, encoding) { + if (Buffer.isBuffer(string)) { + return string.length + } + if (isArrayBufferView(string) || isArrayBuffer(string)) { + return string.byteLength + } + if (typeof string !== 'string') { + string = '' + string + } + + var len = string.length + if (len === 0) return 0 + + // Use a for loop to avoid recursion + var loweredCase = false + for (;;) { + switch (encoding) { + case 'ascii': + case 'latin1': + case 'binary': + return len + case 'utf8': + case 'utf-8': + case undefined: + return utf8ToBytes(string).length + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return len * 2 + case 'hex': + return len >>> 1 + case 'base64': + return base64ToBytes(string).length + default: + if (loweredCase) return utf8ToBytes(string).length // assume utf8 + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } +} +Buffer.byteLength = byteLength + +function slowToString (encoding, start, end) { + var loweredCase = false + + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) { + start = 0 + } + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) { + return '' + } + + if (end === undefined || end > this.length) { + end = this.length + } + + if (end <= 0) { + return '' + } + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0 + start >>>= 0 + + if (end <= start) { + return '' + } + + if (!encoding) encoding = 'utf8' + + while (true) { + switch (encoding) { + case 'hex': + return hexSlice(this, start, end) + + case 'utf8': + case 'utf-8': + return utf8Slice(this, start, end) + + case 'ascii': + return asciiSlice(this, start, end) + + case 'latin1': + case 'binary': + return latin1Slice(this, start, end) + + case 'base64': + return base64Slice(this, start, end) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return utf16leSlice(this, start, end) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = (encoding + '').toLowerCase() + loweredCase = true + } + } +} + +// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) +// to detect a Buffer instance. It's not possible to use `instanceof Buffer` +// reliably in a browserify context because there could be multiple different +// copies of the 'buffer' package in use. This method works even for Buffer +// instances that were created from another copy of the `buffer` package. +// See: https://github.com/feross/buffer/issues/154 +Buffer.prototype._isBuffer = true + +function swap (b, n, m) { + var i = b[n] + b[n] = b[m] + b[m] = i +} + +Buffer.prototype.swap16 = function swap16 () { + var len = this.length + if (len % 2 !== 0) { + throw new RangeError('Buffer size must be a multiple of 16-bits') + } + for (var i = 0; i < len; i += 2) { + swap(this, i, i + 1) + } + return this +} + +Buffer.prototype.swap32 = function swap32 () { + var len = this.length + if (len % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits') + } + for (var i = 0; i < len; i += 4) { + swap(this, i, i + 3) + swap(this, i + 1, i + 2) + } + return this +} + +Buffer.prototype.swap64 = function swap64 () { + var len = this.length + if (len % 8 !== 0) { + throw new RangeError('Buffer size must be a multiple of 64-bits') + } + for (var i = 0; i < len; i += 8) { + swap(this, i, i + 7) + swap(this, i + 1, i + 6) + swap(this, i + 2, i + 5) + swap(this, i + 3, i + 4) + } + return this +} + +Buffer.prototype.toString = function toString () { + var length = this.length + if (length === 0) return '' + if (arguments.length === 0) return utf8Slice(this, 0, length) + return slowToString.apply(this, arguments) +} + +Buffer.prototype.equals = function equals (b) { + if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true + return Buffer.compare(this, b) === 0 +} + +Buffer.prototype.inspect = function inspect () { + var str = '' + var max = exports.INSPECT_MAX_BYTES + if (this.length > 0) { + str = this.toString('hex', 0, max).match(/.{2}/g).join(' ') + if (this.length > max) str += ' ... ' + } + return '' +} + +Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { + if (!Buffer.isBuffer(target)) { + throw new TypeError('Argument must be a Buffer') + } + + if (start === undefined) { + start = 0 + } + if (end === undefined) { + end = target ? target.length : 0 + } + if (thisStart === undefined) { + thisStart = 0 + } + if (thisEnd === undefined) { + thisEnd = this.length + } + + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { + throw new RangeError('out of range index') + } + + if (thisStart >= thisEnd && start >= end) { + return 0 + } + if (thisStart >= thisEnd) { + return -1 + } + if (start >= end) { + return 1 + } + + start >>>= 0 + end >>>= 0 + thisStart >>>= 0 + thisEnd >>>= 0 + + if (this === target) return 0 + + var x = thisEnd - thisStart + var y = end - start + var len = Math.min(x, y) + + var thisCopy = this.slice(thisStart, thisEnd) + var targetCopy = target.slice(start, end) + + for (var i = 0; i < len; ++i) { + if (thisCopy[i] !== targetCopy[i]) { + x = thisCopy[i] + y = targetCopy[i] + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +} + +// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, +// OR the last index of `val` in `buffer` at offset <= `byteOffset`. +// +// Arguments: +// - buffer - a Buffer to search +// - val - a string, Buffer, or number +// - byteOffset - an index into `buffer`; will be clamped to an int32 +// - encoding - an optional encoding, relevant is val is a string +// - dir - true for indexOf, false for lastIndexOf +function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { + // Empty buffer means no match + if (buffer.length === 0) return -1 + + // Normalize byteOffset + if (typeof byteOffset === 'string') { + encoding = byteOffset + byteOffset = 0 + } else if (byteOffset > 0x7fffffff) { + byteOffset = 0x7fffffff + } else if (byteOffset < -0x80000000) { + byteOffset = -0x80000000 + } + byteOffset = +byteOffset // Coerce to Number. + if (numberIsNaN(byteOffset)) { + // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer + byteOffset = dir ? 0 : (buffer.length - 1) + } + + // Normalize byteOffset: negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = buffer.length + byteOffset + if (byteOffset >= buffer.length) { + if (dir) return -1 + else byteOffset = buffer.length - 1 + } else if (byteOffset < 0) { + if (dir) byteOffset = 0 + else return -1 + } + + // Normalize val + if (typeof val === 'string') { + val = Buffer.from(val, encoding) + } + + // Finally, search either indexOf (if dir is true) or lastIndexOf + if (Buffer.isBuffer(val)) { + // Special case: looking for empty string/buffer always fails + if (val.length === 0) { + return -1 + } + return arrayIndexOf(buffer, val, byteOffset, encoding, dir) + } else if (typeof val === 'number') { + val = val & 0xFF // Search for a byte value [0-255] + if (typeof Uint8Array.prototype.indexOf === 'function') { + if (dir) { + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) + } else { + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) + } + } + return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) + } + + throw new TypeError('val must be string, number or Buffer') +} + +function arrayIndexOf (arr, val, byteOffset, encoding, dir) { + var indexSize = 1 + var arrLength = arr.length + var valLength = val.length + + if (encoding !== undefined) { + encoding = String(encoding).toLowerCase() + if (encoding === 'ucs2' || encoding === 'ucs-2' || + encoding === 'utf16le' || encoding === 'utf-16le') { + if (arr.length < 2 || val.length < 2) { + return -1 + } + indexSize = 2 + arrLength /= 2 + valLength /= 2 + byteOffset /= 2 + } + } + + function read (buf, i) { + if (indexSize === 1) { + return buf[i] + } else { + return buf.readUInt16BE(i * indexSize) + } + } + + var i + if (dir) { + var foundIndex = -1 + for (i = byteOffset; i < arrLength; i++) { + if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { + if (foundIndex === -1) foundIndex = i + if (i - foundIndex + 1 === valLength) return foundIndex * indexSize + } else { + if (foundIndex !== -1) i -= i - foundIndex + foundIndex = -1 + } + } + } else { + if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength + for (i = byteOffset; i >= 0; i--) { + var found = true + for (var j = 0; j < valLength; j++) { + if (read(arr, i + j) !== read(val, j)) { + found = false + break + } + } + if (found) return i + } + } + + return -1 +} + +Buffer.prototype.includes = function includes (val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1 +} + +Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) +} + +Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false) +} + +function hexWrite (buf, string, offset, length) { + offset = Number(offset) || 0 + var remaining = buf.length - offset + if (!length) { + length = remaining + } else { + length = Number(length) + if (length > remaining) { + length = remaining + } + } + + // must be an even number of digits + var strLen = string.length + if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') + + if (length > strLen / 2) { + length = strLen / 2 + } + for (var i = 0; i < length; ++i) { + var parsed = parseInt(string.substr(i * 2, 2), 16) + if (numberIsNaN(parsed)) return i + buf[offset + i] = parsed + } + return i +} + +function utf8Write (buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) +} + +function asciiWrite (buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length) +} + +function latin1Write (buf, string, offset, length) { + return asciiWrite(buf, string, offset, length) +} + +function base64Write (buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length) +} + +function ucs2Write (buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) +} + +Buffer.prototype.write = function write (string, offset, length, encoding) { + // Buffer#write(string) + if (offset === undefined) { + encoding = 'utf8' + length = this.length + offset = 0 + // Buffer#write(string, encoding) + } else if (length === undefined && typeof offset === 'string') { + encoding = offset + length = this.length + offset = 0 + // Buffer#write(string, offset[, length][, encoding]) + } else if (isFinite(offset)) { + offset = offset >>> 0 + if (isFinite(length)) { + length = length >>> 0 + if (encoding === undefined) encoding = 'utf8' + } else { + encoding = length + length = undefined + } + } else { + throw new Error( + 'Buffer.write(string, encoding, offset[, length]) is no longer supported' + ) + } + + var remaining = this.length - offset + if (length === undefined || length > remaining) length = remaining + + if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { + throw new RangeError('Attempt to write outside buffer bounds') + } + + if (!encoding) encoding = 'utf8' + + var loweredCase = false + for (;;) { + switch (encoding) { + case 'hex': + return hexWrite(this, string, offset, length) + + case 'utf8': + case 'utf-8': + return utf8Write(this, string, offset, length) + + case 'ascii': + return asciiWrite(this, string, offset, length) + + case 'latin1': + case 'binary': + return latin1Write(this, string, offset, length) + + case 'base64': + // Warning: maxLength not taken into account in base64Write + return base64Write(this, string, offset, length) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return ucs2Write(this, string, offset, length) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = ('' + encoding).toLowerCase() + loweredCase = true + } + } +} + +Buffer.prototype.toJSON = function toJSON () { + return { + type: 'Buffer', + data: Array.prototype.slice.call(this._arr || this, 0) + } +} + +function base64Slice (buf, start, end) { + if (start === 0 && end === buf.length) { + return base64.fromByteArray(buf) + } else { + return base64.fromByteArray(buf.slice(start, end)) + } +} + +function utf8Slice (buf, start, end) { + end = Math.min(buf.length, end) + var res = [] + + var i = start + while (i < end) { + var firstByte = buf[i] + var codePoint = null + var bytesPerSequence = (firstByte > 0xEF) ? 4 + : (firstByte > 0xDF) ? 3 + : (firstByte > 0xBF) ? 2 + : 1 + + if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint + + switch (bytesPerSequence) { + case 1: + if (firstByte < 0x80) { + codePoint = firstByte + } + break + case 2: + secondByte = buf[i + 1] + if ((secondByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) + if (tempCodePoint > 0x7F) { + codePoint = tempCodePoint + } + } + break + case 3: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) + if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { + codePoint = tempCodePoint + } + } + break + case 4: + secondByte = buf[i + 1] + thirdByte = buf[i + 2] + fourthByte = buf[i + 3] + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) + if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { + codePoint = tempCodePoint + } + } + } + } + + if (codePoint === null) { + // we did not generate a valid codePoint so insert a + // replacement char (U+FFFD) and advance only 1 byte + codePoint = 0xFFFD + bytesPerSequence = 1 + } else if (codePoint > 0xFFFF) { + // encode to utf16 (surrogate pair dance) + codePoint -= 0x10000 + res.push(codePoint >>> 10 & 0x3FF | 0xD800) + codePoint = 0xDC00 | codePoint & 0x3FF + } + + res.push(codePoint) + i += bytesPerSequence + } + + return decodeCodePointsArray(res) +} + +// Based on http://stackoverflow.com/a/22747272/680742, the browser with +// the lowest limit is Chrome, with 0x10000 args. +// We go 1 magnitude less, for safety +var MAX_ARGUMENTS_LENGTH = 0x1000 + +function decodeCodePointsArray (codePoints) { + var len = codePoints.length + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints) // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = '' + var i = 0 + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ) + } + return res +} + +function asciiSlice (buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i] & 0x7F) + } + return ret +} + +function latin1Slice (buf, start, end) { + var ret = '' + end = Math.min(buf.length, end) + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i]) + } + return ret +} + +function hexSlice (buf, start, end) { + var len = buf.length + + if (!start || start < 0) start = 0 + if (!end || end < 0 || end > len) end = len + + var out = '' + for (var i = start; i < end; ++i) { + out += toHex(buf[i]) + } + return out +} + +function utf16leSlice (buf, start, end) { + var bytes = buf.slice(start, end) + var res = '' + for (var i = 0; i < bytes.length; i += 2) { + res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)) + } + return res +} + +Buffer.prototype.slice = function slice (start, end) { + var len = this.length + start = ~~start + end = end === undefined ? len : ~~end + + if (start < 0) { + start += len + if (start < 0) start = 0 + } else if (start > len) { + start = len + } + + if (end < 0) { + end += len + if (end < 0) end = 0 + } else if (end > len) { + end = len + } + + if (end < start) end = start + + var newBuf = this.subarray(start, end) + // Return an augmented `Uint8Array` instance + newBuf.__proto__ = Buffer.prototype + return newBuf +} + +/* + * Need to make sure that buffer isn't trying to write out of bounds. + */ +function checkOffset (offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') +} + +Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + + return val +} + +Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + checkOffset(offset, byteLength, this.length) + } + + var val = this[offset + --byteLength] + var mul = 1 + while (byteLength > 0 && (mul *= 0x100)) { + val += this[offset + --byteLength] * mul + } + + return val +} + +Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + return this[offset] +} + +Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return this[offset] | (this[offset + 1] << 8) +} + +Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + return (this[offset] << 8) | this[offset + 1] +} + +Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000) +} + +Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) +} + +Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var val = this[offset] + var mul = 1 + var i = 0 + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val +} + +Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) checkOffset(offset, byteLength, this.length) + + var i = byteLength + var mul = 1 + var val = this[offset + --i] + while (i > 0 && (mul *= 0x100)) { + val += this[offset + --i] * mul + } + mul *= 0x80 + + if (val >= mul) val -= Math.pow(2, 8 * byteLength) + + return val +} + +Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 1, this.length) + if (!(this[offset] & 0x80)) return (this[offset]) + return ((0xff - this[offset] + 1) * -1) +} + +Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset] | (this[offset + 1] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val +} + +Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 2, this.length) + var val = this[offset + 1] | (this[offset] << 8) + return (val & 0x8000) ? val | 0xFFFF0000 : val +} + +Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) +} + +Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) +} + +Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, true, 23, 4) +} + +Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 4, this.length) + return ieee754.read(this, offset, false, 23, 4) +} + +Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, true, 52, 8) +} + +Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { + offset = offset >>> 0 + if (!noAssert) checkOffset(offset, 8, this.length) + return ieee754.read(this, offset, false, 52, 8) +} + +function checkInt (buf, value, offset, ext, max, min) { + if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') + if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') + if (offset + ext > buf.length) throw new RangeError('Index out of range') +} + +Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var mul = 1 + var i = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + byteLength = byteLength >>> 0 + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1 + checkInt(this, value, offset, byteLength, maxBytes, 0) + } + + var i = byteLength - 1 + var mul = 1 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0) + this[offset] = (value & 0xff) + return offset + 1 +} + +Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 +} + +Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 +} + +Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset + 3] = (value >>> 24) + this[offset + 2] = (value >>> 16) + this[offset + 1] = (value >>> 8) + this[offset] = (value & 0xff) + return offset + 4 +} + +Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0) + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 +} + +Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = 0 + var mul = 1 + var sub = 0 + this[offset] = value & 0xFF + while (++i < byteLength && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1) + + checkInt(this, value, offset, byteLength, limit - 1, -limit) + } + + var i = byteLength - 1 + var mul = 1 + var sub = 0 + this[offset + i] = value & 0xFF + while (--i >= 0 && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { + sub = 1 + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF + } + + return offset + byteLength +} + +Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80) + if (value < 0) value = 0xff + value + 1 + this[offset] = (value & 0xff) + return offset + 1 +} + +Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + return offset + 2 +} + +Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000) + this[offset] = (value >>> 8) + this[offset + 1] = (value & 0xff) + return offset + 2 +} + +Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + this[offset] = (value & 0xff) + this[offset + 1] = (value >>> 8) + this[offset + 2] = (value >>> 16) + this[offset + 3] = (value >>> 24) + return offset + 4 +} + +Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000) + if (value < 0) value = 0xffffffff + value + 1 + this[offset] = (value >>> 24) + this[offset + 1] = (value >>> 16) + this[offset + 2] = (value >>> 8) + this[offset + 3] = (value & 0xff) + return offset + 4 +} + +function checkIEEE754 (buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) throw new RangeError('Index out of range') + if (offset < 0) throw new RangeError('Index out of range') +} + +function writeFloat (buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38) + } + ieee754.write(buf, value, offset, littleEndian, 23, 4) + return offset + 4 +} + +Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert) +} + +Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert) +} + +function writeDouble (buf, value, offset, littleEndian, noAssert) { + value = +value + offset = offset >>> 0 + if (!noAssert) { + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308) + } + ieee754.write(buf, value, offset, littleEndian, 52, 8) + return offset + 8 +} + +Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert) +} + +Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert) +} + +// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) +Buffer.prototype.copy = function copy (target, targetStart, start, end) { + if (!start) start = 0 + if (!end && end !== 0) end = this.length + if (targetStart >= target.length) targetStart = target.length + if (!targetStart) targetStart = 0 + if (end > 0 && end < start) end = start + + // Copy 0 bytes; we're done + if (end === start) return 0 + if (target.length === 0 || this.length === 0) return 0 + + // Fatal error conditions + if (targetStart < 0) { + throw new RangeError('targetStart out of bounds') + } + if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') + if (end < 0) throw new RangeError('sourceEnd out of bounds') + + // Are we oob? + if (end > this.length) end = this.length + if (target.length - targetStart < end - start) { + end = target.length - targetStart + start + } + + var len = end - start + var i + + if (this === target && start < targetStart && targetStart < end) { + // descending copy from end + for (i = len - 1; i >= 0; --i) { + target[i + targetStart] = this[i + start] + } + } else if (len < 1000) { + // ascending copy from start + for (i = 0; i < len; ++i) { + target[i + targetStart] = this[i + start] + } + } else { + Uint8Array.prototype.set.call( + target, + this.subarray(start, start + len), + targetStart + ) + } + + return len +} + +// Usage: +// buffer.fill(number[, offset[, end]]) +// buffer.fill(buffer[, offset[, end]]) +// buffer.fill(string[, offset[, end]][, encoding]) +Buffer.prototype.fill = function fill (val, start, end, encoding) { + // Handle string cases: + if (typeof val === 'string') { + if (typeof start === 'string') { + encoding = start + start = 0 + end = this.length + } else if (typeof end === 'string') { + encoding = end + end = this.length + } + if (val.length === 1) { + var code = val.charCodeAt(0) + if (code < 256) { + val = code + } + } + if (encoding !== undefined && typeof encoding !== 'string') { + throw new TypeError('encoding must be a string') + } + if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + } else if (typeof val === 'number') { + val = val & 255 + } + + // Invalid ranges are not set to a default, so can range check early. + if (start < 0 || this.length < start || this.length < end) { + throw new RangeError('Out of range index') + } + + if (end <= start) { + return this + } + + start = start >>> 0 + end = end === undefined ? this.length : end >>> 0 + + if (!val) val = 0 + + var i + if (typeof val === 'number') { + for (i = start; i < end; ++i) { + this[i] = val + } + } else { + var bytes = Buffer.isBuffer(val) + ? val + : new Buffer(val, encoding) + var len = bytes.length + for (i = 0; i < end - start; ++i) { + this[i + start] = bytes[i % len] + } + } + + return this +} + +// HELPER FUNCTIONS +// ================ + +var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g + +function base64clean (str) { + // Node strips out invalid characters like \n and \t from the string, base64-js does not + str = str.trim().replace(INVALID_BASE64_RE, '') + // Node converts strings with length < 2 to '' + if (str.length < 2) return '' + // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not + while (str.length % 4 !== 0) { + str = str + '=' + } + return str +} + +function toHex (n) { + if (n < 16) return '0' + n.toString(16) + return n.toString(16) +} + +function utf8ToBytes (string, units) { + units = units || Infinity + var codePoint + var length = string.length + var leadSurrogate = null + var bytes = [] + + for (var i = 0; i < length; ++i) { + codePoint = string.charCodeAt(i) + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (!leadSurrogate) { + // no lead yet + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + continue + } + + // valid lead + leadSurrogate = codePoint + + continue + } + + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + leadSurrogate = codePoint + continue + } + + // valid surrogate pair + codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000 + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) + } + + leadSurrogate = null + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint) + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else if (codePoint < 0x110000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ) + } else { + throw new Error('Invalid code point') + } + } + + return bytes +} + +function asciiToBytes (str) { + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + // Node's code seems to be doing this and not & 0x7F.. + byteArray.push(str.charCodeAt(i) & 0xFF) + } + return byteArray +} + +function utf16leToBytes (str, units) { + var c, hi, lo + var byteArray = [] + for (var i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) break + + c = str.charCodeAt(i) + hi = c >> 8 + lo = c % 256 + byteArray.push(lo) + byteArray.push(hi) + } + + return byteArray +} + +function base64ToBytes (str) { + return base64.toByteArray(base64clean(str)) +} + +function blitBuffer (src, dst, offset, length) { + for (var i = 0; i < length; ++i) { + if ((i + offset >= dst.length) || (i >= src.length)) break + dst[i + offset] = src[i] + } + return i +} + +// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check +// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 +function isArrayBuffer (obj) { + return obj instanceof ArrayBuffer || + (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && + typeof obj.byteLength === 'number') +} + +// Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` +function isArrayBufferView (obj) { + return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj) +} + +function numberIsNaN (obj) { + return obj !== obj // eslint-disable-line no-self-compare +} + +},{"base64-js":6,"ieee754":12}],10:[function(require,module,exports){ +(function (Buffer){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. + +function isArray(arg) { + if (Array.isArray) { + return Array.isArray(arg); + } + return objectToString(arg) === '[object Array]'; +} +exports.isArray = isArray; + +function isBoolean(arg) { + return typeof arg === 'boolean'; +} +exports.isBoolean = isBoolean; + +function isNull(arg) { + return arg === null; +} +exports.isNull = isNull; + +function isNullOrUndefined(arg) { + return arg == null; +} +exports.isNullOrUndefined = isNullOrUndefined; + +function isNumber(arg) { + return typeof arg === 'number'; +} +exports.isNumber = isNumber; + +function isString(arg) { + return typeof arg === 'string'; +} +exports.isString = isString; + +function isSymbol(arg) { + return typeof arg === 'symbol'; +} +exports.isSymbol = isSymbol; + +function isUndefined(arg) { + return arg === void 0; +} +exports.isUndefined = isUndefined; + +function isRegExp(re) { + return objectToString(re) === '[object RegExp]'; +} +exports.isRegExp = isRegExp; + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} +exports.isObject = isObject; + +function isDate(d) { + return objectToString(d) === '[object Date]'; +} +exports.isDate = isDate; + +function isError(e) { + return (objectToString(e) === '[object Error]' || e instanceof Error); +} +exports.isError = isError; + +function isFunction(arg) { + return typeof arg === 'function'; +} +exports.isFunction = isFunction; + +function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; +} +exports.isPrimitive = isPrimitive; + +exports.isBuffer = Buffer.isBuffer; + +function objectToString(o) { + return Object.prototype.toString.call(o); +} + +}).call(this,{"isBuffer":require("../../is-buffer/index.js")}) +},{"../../is-buffer/index.js":14}],11:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +function EventEmitter() { + this._events = this._events || {}; + this._maxListeners = this._maxListeners || undefined; +} +module.exports = EventEmitter; + +// Backwards-compat with node 0.10.x +EventEmitter.EventEmitter = EventEmitter; + +EventEmitter.prototype._events = undefined; +EventEmitter.prototype._maxListeners = undefined; + +// By default EventEmitters will print a warning if more than 10 listeners are +// added to it. This is a useful default which helps finding memory leaks. +EventEmitter.defaultMaxListeners = 10; + +// Obviously not all Emitters should be limited to 10. This function allows +// that to be increased. Set to zero for unlimited. +EventEmitter.prototype.setMaxListeners = function(n) { + if (!isNumber(n) || n < 0 || isNaN(n)) + throw TypeError('n must be a positive number'); + this._maxListeners = n; + return this; +}; + +EventEmitter.prototype.emit = function(type) { + var er, handler, len, args, i, listeners; + + if (!this._events) + this._events = {}; + + // If there is no 'error' event listener then throw. + if (type === 'error') { + if (!this._events.error || + (isObject(this._events.error) && !this._events.error.length)) { + er = arguments[1]; + if (er instanceof Error) { + throw er; // Unhandled 'error' event + } else { + // At least give some kind of context to the user + var err = new Error('Uncaught, unspecified "error" event. (' + er + ')'); + err.context = er; + throw err; + } + } + } + + handler = this._events[type]; + + if (isUndefined(handler)) + return false; + + if (isFunction(handler)) { + switch (arguments.length) { + // fast cases + case 1: + handler.call(this); + break; + case 2: + handler.call(this, arguments[1]); + break; + case 3: + handler.call(this, arguments[1], arguments[2]); + break; + // slower + default: + args = Array.prototype.slice.call(arguments, 1); + handler.apply(this, args); + } + } else if (isObject(handler)) { + args = Array.prototype.slice.call(arguments, 1); + listeners = handler.slice(); + len = listeners.length; + for (i = 0; i < len; i++) + listeners[i].apply(this, args); + } + + return true; +}; + +EventEmitter.prototype.addListener = function(type, listener) { + var m; + + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + if (!this._events) + this._events = {}; + + // To avoid recursion in the case that type === "newListener"! Before + // adding it to the listeners, first emit "newListener". + if (this._events.newListener) + this.emit('newListener', type, + isFunction(listener.listener) ? + listener.listener : listener); + + if (!this._events[type]) + // Optimize the case of one listener. Don't need the extra array object. + this._events[type] = listener; + else if (isObject(this._events[type])) + // If we've already got an array, just append. + this._events[type].push(listener); + else + // Adding the second element, need to change to array. + this._events[type] = [this._events[type], listener]; + + // Check for listener leak + if (isObject(this._events[type]) && !this._events[type].warned) { + if (!isUndefined(this._maxListeners)) { + m = this._maxListeners; + } else { + m = EventEmitter.defaultMaxListeners; + } + + if (m && m > 0 && this._events[type].length > m) { + this._events[type].warned = true; + console.error('(node) warning: possible EventEmitter memory ' + + 'leak detected. %d listeners added. ' + + 'Use emitter.setMaxListeners() to increase limit.', + this._events[type].length); + if (typeof console.trace === 'function') { + // not supported in IE 10 + console.trace(); + } + } + } + + return this; +}; + +EventEmitter.prototype.on = EventEmitter.prototype.addListener; + +EventEmitter.prototype.once = function(type, listener) { + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + var fired = false; + + function g() { + this.removeListener(type, g); + + if (!fired) { + fired = true; + listener.apply(this, arguments); + } + } + + g.listener = listener; + this.on(type, g); + + return this; +}; + +// emits a 'removeListener' event iff the listener was removed +EventEmitter.prototype.removeListener = function(type, listener) { + var list, position, length, i; + + if (!isFunction(listener)) + throw TypeError('listener must be a function'); + + if (!this._events || !this._events[type]) + return this; + + list = this._events[type]; + length = list.length; + position = -1; + + if (list === listener || + (isFunction(list.listener) && list.listener === listener)) { + delete this._events[type]; + if (this._events.removeListener) + this.emit('removeListener', type, listener); + + } else if (isObject(list)) { + for (i = length; i-- > 0;) { + if (list[i] === listener || + (list[i].listener && list[i].listener === listener)) { + position = i; + break; + } + } + + if (position < 0) + return this; + + if (list.length === 1) { + list.length = 0; + delete this._events[type]; + } else { + list.splice(position, 1); + } + + if (this._events.removeListener) + this.emit('removeListener', type, listener); + } + + return this; +}; + +EventEmitter.prototype.removeAllListeners = function(type) { + var key, listeners; + + if (!this._events) + return this; + + // not listening for removeListener, no need to emit + if (!this._events.removeListener) { + if (arguments.length === 0) + this._events = {}; + else if (this._events[type]) + delete this._events[type]; + return this; + } + + // emit removeListener for all listeners on all events + if (arguments.length === 0) { + for (key in this._events) { + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); + this._events = {}; + return this; + } + + listeners = this._events[type]; + + if (isFunction(listeners)) { + this.removeListener(type, listeners); + } else if (listeners) { + // LIFO order + while (listeners.length) + this.removeListener(type, listeners[listeners.length - 1]); + } + delete this._events[type]; + + return this; +}; + +EventEmitter.prototype.listeners = function(type) { + var ret; + if (!this._events || !this._events[type]) + ret = []; + else if (isFunction(this._events[type])) + ret = [this._events[type]]; + else + ret = this._events[type].slice(); + return ret; +}; + +EventEmitter.prototype.listenerCount = function(type) { + if (this._events) { + var evlistener = this._events[type]; + + if (isFunction(evlistener)) + return 1; + else if (evlistener) + return evlistener.length; + } + return 0; +}; + +EventEmitter.listenerCount = function(emitter, type) { + return emitter.listenerCount(type); +}; + +function isFunction(arg) { + return typeof arg === 'function'; +} + +function isNumber(arg) { + return typeof arg === 'number'; +} + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} + +function isUndefined(arg) { + return arg === void 0; +} + +},{}],12:[function(require,module,exports){ +exports.read = function (buffer, offset, isLE, mLen, nBytes) { + var e, m + var eLen = nBytes * 8 - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var nBits = -7 + var i = isLE ? (nBytes - 1) : 0 + var d = isLE ? -1 : 1 + var s = buffer[offset + i] + + i += d + + e = s & ((1 << (-nBits)) - 1) + s >>= (-nBits) + nBits += eLen + for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1) + e >>= (-nBits) + nBits += mLen + for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity) + } else { + m = m + Math.pow(2, mLen) + e = e - eBias + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen) +} + +exports.write = function (buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c + var eLen = nBytes * 8 - mLen - 1 + var eMax = (1 << eLen) - 1 + var eBias = eMax >> 1 + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0) + var i = isLE ? 0 : (nBytes - 1) + var d = isLE ? 1 : -1 + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0 + + value = Math.abs(value) + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0 + e = eMax + } else { + e = Math.floor(Math.log(value) / Math.LN2) + if (value * (c = Math.pow(2, -e)) < 1) { + e-- + c *= 2 + } + if (e + eBias >= 1) { + value += rt / c + } else { + value += rt * Math.pow(2, 1 - eBias) + } + if (value * c >= 2) { + e++ + c /= 2 + } + + if (e + eBias >= eMax) { + m = 0 + e = eMax + } else if (e + eBias >= 1) { + m = (value * c - 1) * Math.pow(2, mLen) + e = e + eBias + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen) + e = 0 + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m + eLen += mLen + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128 +} + +},{}],13:[function(require,module,exports){ +if (typeof Object.create === 'function') { + // implementation from standard node.js 'util' module + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + ctor.prototype = Object.create(superCtor.prototype, { + constructor: { + value: ctor, + enumerable: false, + writable: true, + configurable: true + } + }); + }; +} else { + // old school shim for old browsers + module.exports = function inherits(ctor, superCtor) { + ctor.super_ = superCtor + var TempCtor = function () {} + TempCtor.prototype = superCtor.prototype + ctor.prototype = new TempCtor() + ctor.prototype.constructor = ctor + } +} + +},{}],14:[function(require,module,exports){ +/*! + * Determine if an object is a Buffer + * + * @author Feross Aboukhadijeh + * @license MIT + */ + +// The _isBuffer check is for Safari 5-7 support, because it's missing +// Object.prototype.constructor. Remove this eventually +module.exports = function (obj) { + return obj != null && (isBuffer(obj) || isSlowBuffer(obj) || !!obj._isBuffer) +} + +function isBuffer (obj) { + return !!obj.constructor && typeof obj.constructor.isBuffer === 'function' && obj.constructor.isBuffer(obj) +} + +// For Node v0.10 support. Remove this eventually. +function isSlowBuffer (obj) { + return typeof obj.readFloatLE === 'function' && typeof obj.slice === 'function' && isBuffer(obj.slice(0, 0)) +} + +},{}],15:[function(require,module,exports){ +var toString = {}.toString; + +module.exports = Array.isArray || function (arr) { + return toString.call(arr) == '[object Array]'; +}; + +},{}],16:[function(require,module,exports){ +(function (process){ +'use strict'; + +if (!process.version || + process.version.indexOf('v0.') === 0 || + process.version.indexOf('v1.') === 0 && process.version.indexOf('v1.8.') !== 0) { + module.exports = nextTick; +} else { + module.exports = process.nextTick; +} + +function nextTick(fn, arg1, arg2, arg3) { + if (typeof fn !== 'function') { + throw new TypeError('"callback" argument must be a function'); + } + var len = arguments.length; + var args, i; + switch (len) { + case 0: + case 1: + return process.nextTick(fn); + case 2: + return process.nextTick(function afterTickOne() { + fn.call(null, arg1); + }); + case 3: + return process.nextTick(function afterTickTwo() { + fn.call(null, arg1, arg2); + }); + case 4: + return process.nextTick(function afterTickThree() { + fn.call(null, arg1, arg2, arg3); + }); + default: + args = new Array(len - 1); + i = 0; + while (i < args.length) { + args[i++] = arguments[i]; + } + return process.nextTick(function afterTick() { + fn.apply(null, args); + }); + } +} + +}).call(this,require('_process')) +},{"_process":17}],17:[function(require,module,exports){ +// shim for using process in browser +var process = module.exports = {}; + +// cached from whatever global is present so that test runners that stub it +// don't break things. But we need to wrap it in a try catch in case it is +// wrapped in strict mode code which doesn't define any globals. It's inside a +// function because try/catches deoptimize in certain engines. + +var cachedSetTimeout; +var cachedClearTimeout; + +function defaultSetTimout() { + throw new Error('setTimeout has not been defined'); +} +function defaultClearTimeout () { + throw new Error('clearTimeout has not been defined'); +} +(function () { + try { + if (typeof setTimeout === 'function') { + cachedSetTimeout = setTimeout; + } else { + cachedSetTimeout = defaultSetTimout; + } + } catch (e) { + cachedSetTimeout = defaultSetTimout; + } + try { + if (typeof clearTimeout === 'function') { + cachedClearTimeout = clearTimeout; + } else { + cachedClearTimeout = defaultClearTimeout; + } + } catch (e) { + cachedClearTimeout = defaultClearTimeout; + } +} ()) +function runTimeout(fun) { + if (cachedSetTimeout === setTimeout) { + //normal enviroments in sane situations + return setTimeout(fun, 0); + } + // if setTimeout wasn't available but was latter defined + if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) { + cachedSetTimeout = setTimeout; + return setTimeout(fun, 0); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedSetTimeout(fun, 0); + } catch(e){ + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedSetTimeout.call(null, fun, 0); + } catch(e){ + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error + return cachedSetTimeout.call(this, fun, 0); + } + } + + +} +function runClearTimeout(marker) { + if (cachedClearTimeout === clearTimeout) { + //normal enviroments in sane situations + return clearTimeout(marker); + } + // if clearTimeout wasn't available but was latter defined + if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) { + cachedClearTimeout = clearTimeout; + return clearTimeout(marker); + } + try { + // when when somebody has screwed with setTimeout but no I.E. maddness + return cachedClearTimeout(marker); + } catch (e){ + try { + // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally + return cachedClearTimeout.call(null, marker); + } catch (e){ + // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error. + // Some versions of I.E. have different rules for clearTimeout vs setTimeout + return cachedClearTimeout.call(this, marker); + } + } + + + +} +var queue = []; +var draining = false; +var currentQueue; +var queueIndex = -1; + +function cleanUpNextTick() { + if (!draining || !currentQueue) { + return; + } + draining = false; + if (currentQueue.length) { + queue = currentQueue.concat(queue); + } else { + queueIndex = -1; + } + if (queue.length) { + drainQueue(); + } +} + +function drainQueue() { + if (draining) { + return; + } + var timeout = runTimeout(cleanUpNextTick); + draining = true; + + var len = queue.length; + while(len) { + currentQueue = queue; + queue = []; + while (++queueIndex < len) { + if (currentQueue) { + currentQueue[queueIndex].run(); + } + } + queueIndex = -1; + len = queue.length; + } + currentQueue = null; + draining = false; + runClearTimeout(timeout); +} + +process.nextTick = function (fun) { + var args = new Array(arguments.length - 1); + if (arguments.length > 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + runTimeout(drainQueue); + } +}; + +// v8 likes predictible objects +function Item(fun, array) { + this.fun = fun; + this.array = array; +} +Item.prototype.run = function () { + this.fun.apply(null, this.array); +}; +process.title = 'browser'; +process.browser = true; +process.env = {}; +process.argv = []; +process.version = ''; // empty string to avoid regexp issues +process.versions = {}; + +function noop() {} + +process.on = noop; +process.addListener = noop; +process.once = noop; +process.off = noop; +process.removeListener = noop; +process.removeAllListeners = noop; +process.emit = noop; +process.prependListener = noop; +process.prependOnceListener = noop; + +process.listeners = function (name) { return [] } + +process.binding = function (name) { + throw new Error('process.binding is not supported'); +}; + +process.cwd = function () { return '/' }; +process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); +}; +process.umask = function() { return 0; }; + +},{}],18:[function(require,module,exports){ +module.exports = require('./lib/_stream_duplex.js'); + +},{"./lib/_stream_duplex.js":19}],19:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a duplex stream is just a stream that is both readable and writable. +// Since JS doesn't have multiple prototypal inheritance, this class +// prototypally inherits from Readable, and then parasitically from +// Writable. + +'use strict'; + +/**/ + +var processNextTick = require('process-nextick-args'); +/**/ + +/**/ +var objectKeys = Object.keys || function (obj) { + var keys = []; + for (var key in obj) { + keys.push(key); + }return keys; +}; +/**/ + +module.exports = Duplex; + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +var Readable = require('./_stream_readable'); +var Writable = require('./_stream_writable'); + +util.inherits(Duplex, Readable); + +var keys = objectKeys(Writable.prototype); +for (var v = 0; v < keys.length; v++) { + var method = keys[v]; + if (!Duplex.prototype[method]) Duplex.prototype[method] = Writable.prototype[method]; +} + +function Duplex(options) { + if (!(this instanceof Duplex)) return new Duplex(options); + + Readable.call(this, options); + Writable.call(this, options); + + if (options && options.readable === false) this.readable = false; + + if (options && options.writable === false) this.writable = false; + + this.allowHalfOpen = true; + if (options && options.allowHalfOpen === false) this.allowHalfOpen = false; + + this.once('end', onend); +} + +// the no-half-open enforcer +function onend() { + // if we allow half-open state, or if the writable side ended, + // then we're ok. + if (this.allowHalfOpen || this._writableState.ended) return; + + // no more data can be written. + // But allow more writes to happen in this tick. + processNextTick(onEndNT, this); +} + +function onEndNT(self) { + self.end(); +} + +Object.defineProperty(Duplex.prototype, 'destroyed', { + get: function () { + if (this._readableState === undefined || this._writableState === undefined) { + return false; + } + return this._readableState.destroyed && this._writableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (this._readableState === undefined || this._writableState === undefined) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + this._writableState.destroyed = value; + } +}); + +Duplex.prototype._destroy = function (err, cb) { + this.push(null); + this.end(); + + processNextTick(cb, err); +}; + +function forEach(xs, f) { + for (var i = 0, l = xs.length; i < l; i++) { + f(xs[i], i); + } +} +},{"./_stream_readable":21,"./_stream_writable":23,"core-util-is":10,"inherits":13,"process-nextick-args":16}],20:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a passthrough stream. +// basically just the most minimal sort of Transform stream. +// Every written chunk gets output as-is. + +'use strict'; + +module.exports = PassThrough; + +var Transform = require('./_stream_transform'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +util.inherits(PassThrough, Transform); + +function PassThrough(options) { + if (!(this instanceof PassThrough)) return new PassThrough(options); + + Transform.call(this, options); +} + +PassThrough.prototype._transform = function (chunk, encoding, cb) { + cb(null, chunk); +}; +},{"./_stream_transform":22,"core-util-is":10,"inherits":13}],21:[function(require,module,exports){ +(function (process,global){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +'use strict'; + +/**/ + +var processNextTick = require('process-nextick-args'); +/**/ + +module.exports = Readable; + +/**/ +var isArray = require('isarray'); +/**/ + +/**/ +var Duplex; +/**/ + +Readable.ReadableState = ReadableState; + +/**/ +var EE = require('events').EventEmitter; + +var EElistenerCount = function (emitter, type) { + return emitter.listeners(type).length; +}; +/**/ + +/**/ +var Stream = require('./internal/streams/stream'); +/**/ + +// TODO(bmeurer): Change this back to const once hole checks are +// properly optimized away early in Ignition+TurboFan. +/**/ +var Buffer = require('safe-buffer').Buffer; +var OurUint8Array = global.Uint8Array || function () {}; +function _uint8ArrayToBuffer(chunk) { + return Buffer.from(chunk); +} +function _isUint8Array(obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; +} +/**/ + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +/**/ +var debugUtil = require('util'); +var debug = void 0; +if (debugUtil && debugUtil.debuglog) { + debug = debugUtil.debuglog('stream'); +} else { + debug = function () {}; +} +/**/ + +var BufferList = require('./internal/streams/BufferList'); +var destroyImpl = require('./internal/streams/destroy'); +var StringDecoder; + +util.inherits(Readable, Stream); + +var kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume']; + +function prependListener(emitter, event, fn) { + // Sadly this is not cacheable as some libraries bundle their own + // event emitter implementation with them. + if (typeof emitter.prependListener === 'function') { + return emitter.prependListener(event, fn); + } else { + // This is a hack to make sure that our error handler is attached before any + // userland ones. NEVER DO THIS. This is here only because this code needs + // to continue to work with older versions of Node.js that do not include + // the prependListener() method. The goal is to eventually remove this hack. + if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; + } +} + +function ReadableState(options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // object stream flag. Used to make read(n) ignore n and to + // make all the buffer merging and length checks go away + this.objectMode = !!options.objectMode; + + if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.readableObjectMode; + + // the point at which it stops calling _read() to fill the buffer + // Note: 0 is a valid value, means "don't call _read preemptively ever" + var hwm = options.highWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // A linked list is used to store data chunks instead of an array because the + // linked list can remove elements from the beginning faster than + // array.shift() + this.buffer = new BufferList(); + this.length = 0; + this.pipes = null; + this.pipesCount = 0; + this.flowing = null; + this.ended = false; + this.endEmitted = false; + this.reading = false; + + // a flag to be able to tell if the event 'readable'/'data' is emitted + // immediately, or on a later tick. We set this to true at first, because + // any actions that shouldn't happen until "later" should generally also + // not happen before the first read call. + this.sync = true; + + // whenever we return null, then we set a flag to say + // that we're awaiting a 'readable' event emission. + this.needReadable = false; + this.emittedReadable = false; + this.readableListening = false; + this.resumeScheduled = false; + + // has it been destroyed + this.destroyed = false; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // the number of writers that are awaiting a drain event in .pipe()s + this.awaitDrain = 0; + + // if true, a maybeReadMore has been scheduled + this.readingMore = false; + + this.decoder = null; + this.encoding = null; + if (options.encoding) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this.decoder = new StringDecoder(options.encoding); + this.encoding = options.encoding; + } +} + +function Readable(options) { + Duplex = Duplex || require('./_stream_duplex'); + + if (!(this instanceof Readable)) return new Readable(options); + + this._readableState = new ReadableState(options, this); + + // legacy + this.readable = true; + + if (options) { + if (typeof options.read === 'function') this._read = options.read; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + } + + Stream.call(this); +} + +Object.defineProperty(Readable.prototype, 'destroyed', { + get: function () { + if (this._readableState === undefined) { + return false; + } + return this._readableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._readableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._readableState.destroyed = value; + } +}); + +Readable.prototype.destroy = destroyImpl.destroy; +Readable.prototype._undestroy = destroyImpl.undestroy; +Readable.prototype._destroy = function (err, cb) { + this.push(null); + cb(err); +}; + +// Manually shove something into the read() buffer. +// This returns true if the highWaterMark has not been hit yet, +// similar to how Writable.write() returns true if you should +// write() some more. +Readable.prototype.push = function (chunk, encoding) { + var state = this._readableState; + var skipChunkCheck; + + if (!state.objectMode) { + if (typeof chunk === 'string') { + encoding = encoding || state.defaultEncoding; + if (encoding !== state.encoding) { + chunk = Buffer.from(chunk, encoding); + encoding = ''; + } + skipChunkCheck = true; + } + } else { + skipChunkCheck = true; + } + + return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); +}; + +// Unshift should *always* be something directly out of read() +Readable.prototype.unshift = function (chunk) { + return readableAddChunk(this, chunk, null, true, false); +}; + +function readableAddChunk(stream, chunk, encoding, addToFront, skipChunkCheck) { + var state = stream._readableState; + if (chunk === null) { + state.reading = false; + onEofChunk(stream, state); + } else { + var er; + if (!skipChunkCheck) er = chunkInvalid(state, chunk); + if (er) { + stream.emit('error', er); + } else if (state.objectMode || chunk && chunk.length > 0) { + if (typeof chunk !== 'string' && !state.objectMode && Object.getPrototypeOf(chunk) !== Buffer.prototype) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (addToFront) { + if (state.endEmitted) stream.emit('error', new Error('stream.unshift() after end event'));else addChunk(stream, state, chunk, true); + } else if (state.ended) { + stream.emit('error', new Error('stream.push() after EOF')); + } else { + state.reading = false; + if (state.decoder && !encoding) { + chunk = state.decoder.write(chunk); + if (state.objectMode || chunk.length !== 0) addChunk(stream, state, chunk, false);else maybeReadMore(stream, state); + } else { + addChunk(stream, state, chunk, false); + } + } + } else if (!addToFront) { + state.reading = false; + } + } + + return needMoreData(state); +} + +function addChunk(stream, state, chunk, addToFront) { + if (state.flowing && state.length === 0 && !state.sync) { + stream.emit('data', chunk); + stream.read(0); + } else { + // update the buffer info. + state.length += state.objectMode ? 1 : chunk.length; + if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); + + if (state.needReadable) emitReadable(stream); + } + maybeReadMore(stream, state); +} + +function chunkInvalid(state, chunk) { + var er; + if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + return er; +} + +// if it's past the high water mark, we can push in some more. +// Also, if we have no data yet, we can stand some +// more bytes. This is to work around cases where hwm=0, +// such as the repl. Also, if the push() triggered a +// readable event, and the user called read(largeNumber) such that +// needReadable was set, then we ought to push more, so that another +// 'readable' event will be triggered. +function needMoreData(state) { + return !state.ended && (state.needReadable || state.length < state.highWaterMark || state.length === 0); +} + +Readable.prototype.isPaused = function () { + return this._readableState.flowing === false; +}; + +// backwards compatibility. +Readable.prototype.setEncoding = function (enc) { + if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; + this._readableState.decoder = new StringDecoder(enc); + this._readableState.encoding = enc; + return this; +}; + +// Don't raise the hwm > 8MB +var MAX_HWM = 0x800000; +function computeNewHighWaterMark(n) { + if (n >= MAX_HWM) { + n = MAX_HWM; + } else { + // Get the next highest power of 2 to prevent increasing hwm excessively in + // tiny amounts + n--; + n |= n >>> 1; + n |= n >>> 2; + n |= n >>> 4; + n |= n >>> 8; + n |= n >>> 16; + n++; + } + return n; +} + +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function howMuchToRead(n, state) { + if (n <= 0 || state.length === 0 && state.ended) return 0; + if (state.objectMode) return 1; + if (n !== n) { + // Only flow one buffer at a time + if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; + } + // If we're asking for more than the current hwm, then raise the hwm. + if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); + if (n <= state.length) return n; + // Don't have enough + if (!state.ended) { + state.needReadable = true; + return 0; + } + return state.length; +} + +// you can override either this method, or the async _read(n) below. +Readable.prototype.read = function (n) { + debug('read', n); + n = parseInt(n, 10); + var state = this._readableState; + var nOrig = n; + + if (n !== 0) state.emittedReadable = false; + + // if we're doing read(0) to trigger a readable event, but we + // already have a bunch of data in the buffer, then just trigger + // the 'readable' event and move on. + if (n === 0 && state.needReadable && (state.length >= state.highWaterMark || state.ended)) { + debug('read: emitReadable', state.length, state.ended); + if (state.length === 0 && state.ended) endReadable(this);else emitReadable(this); + return null; + } + + n = howMuchToRead(n, state); + + // if we've ended, and we're now clear, then finish it up. + if (n === 0 && state.ended) { + if (state.length === 0) endReadable(this); + return null; + } + + // All the actual chunk generation logic needs to be + // *below* the call to _read. The reason is that in certain + // synthetic stream cases, such as passthrough streams, _read + // may be a completely synchronous operation which may change + // the state of the read buffer, providing enough data when + // before there was *not* enough. + // + // So, the steps are: + // 1. Figure out what the state of things will be after we do + // a read from the buffer. + // + // 2. If that resulting state will trigger a _read, then call _read. + // Note that this may be asynchronous, or synchronous. Yes, it is + // deeply ugly to write APIs this way, but that still doesn't mean + // that the Readable class should behave improperly, as streams are + // designed to be sync/async agnostic. + // Take note if the _read call is sync or async (ie, if the read call + // has returned yet), so that we know whether or not it's safe to emit + // 'readable' etc. + // + // 3. Actually pull the requested chunks out of the buffer and return. + + // if we need a readable event, then we need to do some reading. + var doRead = state.needReadable; + debug('need readable', doRead); + + // if we currently have less than the highWaterMark, then also read some + if (state.length === 0 || state.length - n < state.highWaterMark) { + doRead = true; + debug('length less than watermark', doRead); + } + + // however, if we've ended, then there's no point, and if we're already + // reading, then it's unnecessary. + if (state.ended || state.reading) { + doRead = false; + debug('reading or ended', doRead); + } else if (doRead) { + debug('do read'); + state.reading = true; + state.sync = true; + // if the length is currently zero, then we *need* a readable event. + if (state.length === 0) state.needReadable = true; + // call internal read method + this._read(state.highWaterMark); + state.sync = false; + // If _read pushed data synchronously, then `reading` will be false, + // and we need to re-evaluate how much data we can return to the user. + if (!state.reading) n = howMuchToRead(nOrig, state); + } + + var ret; + if (n > 0) ret = fromList(n, state);else ret = null; + + if (ret === null) { + state.needReadable = true; + n = 0; + } else { + state.length -= n; + } + + if (state.length === 0) { + // If we have nothing in the buffer, then we want to know + // as soon as we *do* get something into the buffer. + if (!state.ended) state.needReadable = true; + + // If we tried to read() past the EOF, then emit end on the next tick. + if (nOrig !== n && state.ended) endReadable(this); + } + + if (ret !== null) this.emit('data', ret); + + return ret; +}; + +function onEofChunk(stream, state) { + if (state.ended) return; + if (state.decoder) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) { + state.buffer.push(chunk); + state.length += state.objectMode ? 1 : chunk.length; + } + } + state.ended = true; + + // emit 'readable' now to make sure it gets picked up. + emitReadable(stream); +} + +// Don't emit readable right away in sync mode, because this can trigger +// another read() call => stack overflow. This way, it might trigger +// a nextTick recursion warning, but that's not so bad. +function emitReadable(stream) { + var state = stream._readableState; + state.needReadable = false; + if (!state.emittedReadable) { + debug('emitReadable', state.flowing); + state.emittedReadable = true; + if (state.sync) processNextTick(emitReadable_, stream);else emitReadable_(stream); + } +} + +function emitReadable_(stream) { + debug('emit readable'); + stream.emit('readable'); + flow(stream); +} + +// at this point, the user has presumably seen the 'readable' event, +// and called read() to consume some data. that may have triggered +// in turn another _read(n) call, in which case reading = true if +// it's in progress. +// However, if we're not ended, or reading, and the length < hwm, +// then go ahead and try to read some more preemptively. +function maybeReadMore(stream, state) { + if (!state.readingMore) { + state.readingMore = true; + processNextTick(maybeReadMore_, stream, state); + } +} + +function maybeReadMore_(stream, state) { + var len = state.length; + while (!state.reading && !state.flowing && !state.ended && state.length < state.highWaterMark) { + debug('maybeReadMore read 0'); + stream.read(0); + if (len === state.length) + // didn't get any data, stop spinning. + break;else len = state.length; + } + state.readingMore = false; +} + +// abstract method. to be overridden in specific implementation classes. +// call cb(er, data) where data is <= n in length. +// for virtual (non-string, non-buffer) streams, "length" is somewhat +// arbitrary, and perhaps not very meaningful. +Readable.prototype._read = function (n) { + this.emit('error', new Error('_read() is not implemented')); +}; + +Readable.prototype.pipe = function (dest, pipeOpts) { + var src = this; + var state = this._readableState; + + switch (state.pipesCount) { + case 0: + state.pipes = dest; + break; + case 1: + state.pipes = [state.pipes, dest]; + break; + default: + state.pipes.push(dest); + break; + } + state.pipesCount += 1; + debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); + + var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; + + var endFn = doEnd ? onend : unpipe; + if (state.endEmitted) processNextTick(endFn);else src.once('end', endFn); + + dest.on('unpipe', onunpipe); + function onunpipe(readable, unpipeInfo) { + debug('onunpipe'); + if (readable === src) { + if (unpipeInfo && unpipeInfo.hasUnpiped === false) { + unpipeInfo.hasUnpiped = true; + cleanup(); + } + } + } + + function onend() { + debug('onend'); + dest.end(); + } + + // when the dest drains, it reduces the awaitDrain counter + // on the source. This would be more elegant with a .once() + // handler in flow(), but adding and removing repeatedly is + // too slow. + var ondrain = pipeOnDrain(src); + dest.on('drain', ondrain); + + var cleanedUp = false; + function cleanup() { + debug('cleanup'); + // cleanup event handlers once the pipe is broken + dest.removeListener('close', onclose); + dest.removeListener('finish', onfinish); + dest.removeListener('drain', ondrain); + dest.removeListener('error', onerror); + dest.removeListener('unpipe', onunpipe); + src.removeListener('end', onend); + src.removeListener('end', unpipe); + src.removeListener('data', ondata); + + cleanedUp = true; + + // if the reader is waiting for a drain event from this + // specific writer, then it would cause it to never start + // flowing again. + // So, if this is awaiting a drain, then we just call it now. + // If we don't know, then assume that we are waiting for one. + if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); + } + + // If the user pushes more data while we're writing to dest then we'll end up + // in ondata again. However, we only want to increase awaitDrain once because + // dest will only emit one 'drain' event for the multiple writes. + // => Introduce a guard on increasing awaitDrain. + var increasedAwaitDrain = false; + src.on('data', ondata); + function ondata(chunk) { + debug('ondata'); + increasedAwaitDrain = false; + var ret = dest.write(chunk); + if (false === ret && !increasedAwaitDrain) { + // If the user unpiped during `dest.write()`, it is possible + // to get stuck in a permanently paused state if that write + // also returned false. + // => Check whether `dest` is still a piping destination. + if ((state.pipesCount === 1 && state.pipes === dest || state.pipesCount > 1 && indexOf(state.pipes, dest) !== -1) && !cleanedUp) { + debug('false write response, pause', src._readableState.awaitDrain); + src._readableState.awaitDrain++; + increasedAwaitDrain = true; + } + src.pause(); + } + } + + // if the dest has an error, then stop piping into it. + // however, don't suppress the throwing behavior for this. + function onerror(er) { + debug('onerror', er); + unpipe(); + dest.removeListener('error', onerror); + if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); + } + + // Make sure our error handler is attached before userland ones. + prependListener(dest, 'error', onerror); + + // Both close and finish should trigger unpipe, but only once. + function onclose() { + dest.removeListener('finish', onfinish); + unpipe(); + } + dest.once('close', onclose); + function onfinish() { + debug('onfinish'); + dest.removeListener('close', onclose); + unpipe(); + } + dest.once('finish', onfinish); + + function unpipe() { + debug('unpipe'); + src.unpipe(dest); + } + + // tell the dest that it's being piped to + dest.emit('pipe', src); + + // start the flow if it hasn't been started already. + if (!state.flowing) { + debug('pipe resume'); + src.resume(); + } + + return dest; +}; + +function pipeOnDrain(src) { + return function () { + var state = src._readableState; + debug('pipeOnDrain', state.awaitDrain); + if (state.awaitDrain) state.awaitDrain--; + if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { + state.flowing = true; + flow(src); + } + }; +} + +Readable.prototype.unpipe = function (dest) { + var state = this._readableState; + var unpipeInfo = { hasUnpiped: false }; + + // if we're not piping anywhere, then do nothing. + if (state.pipesCount === 0) return this; + + // just one destination. most common case. + if (state.pipesCount === 1) { + // passed in one, but it's not the right one. + if (dest && dest !== state.pipes) return this; + + if (!dest) dest = state.pipes; + + // got a match. + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + if (dest) dest.emit('unpipe', this, unpipeInfo); + return this; + } + + // slow case. multiple pipe destinations. + + if (!dest) { + // remove all. + var dests = state.pipes; + var len = state.pipesCount; + state.pipes = null; + state.pipesCount = 0; + state.flowing = false; + + for (var i = 0; i < len; i++) { + dests[i].emit('unpipe', this, unpipeInfo); + }return this; + } + + // try to find the right one. + var index = indexOf(state.pipes, dest); + if (index === -1) return this; + + state.pipes.splice(index, 1); + state.pipesCount -= 1; + if (state.pipesCount === 1) state.pipes = state.pipes[0]; + + dest.emit('unpipe', this, unpipeInfo); + + return this; +}; + +// set up data events if they are asked for +// Ensure readable listeners eventually get something +Readable.prototype.on = function (ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + + if (ev === 'data') { + // Start flowing on next tick if stream isn't explicitly paused + if (this._readableState.flowing !== false) this.resume(); + } else if (ev === 'readable') { + var state = this._readableState; + if (!state.endEmitted && !state.readableListening) { + state.readableListening = state.needReadable = true; + state.emittedReadable = false; + if (!state.reading) { + processNextTick(nReadingNextTick, this); + } else if (state.length) { + emitReadable(this); + } + } + } + + return res; +}; +Readable.prototype.addListener = Readable.prototype.on; + +function nReadingNextTick(self) { + debug('readable nexttick read 0'); + self.read(0); +} + +// pause() and resume() are remnants of the legacy readable stream API +// If the user uses them, then switch into old mode. +Readable.prototype.resume = function () { + var state = this._readableState; + if (!state.flowing) { + debug('resume'); + state.flowing = true; + resume(this, state); + } + return this; +}; + +function resume(stream, state) { + if (!state.resumeScheduled) { + state.resumeScheduled = true; + processNextTick(resume_, stream, state); + } +} + +function resume_(stream, state) { + if (!state.reading) { + debug('resume read 0'); + stream.read(0); + } + + state.resumeScheduled = false; + state.awaitDrain = 0; + stream.emit('resume'); + flow(stream); + if (state.flowing && !state.reading) stream.read(0); +} + +Readable.prototype.pause = function () { + debug('call pause flowing=%j', this._readableState.flowing); + if (false !== this._readableState.flowing) { + debug('pause'); + this._readableState.flowing = false; + this.emit('pause'); + } + return this; +}; + +function flow(stream) { + var state = stream._readableState; + debug('flow', state.flowing); + while (state.flowing && stream.read() !== null) {} +} + +// wrap an old-style stream as the async data source. +// This is *not* part of the readable stream interface. +// It is an ugly unfortunate mess of history. +Readable.prototype.wrap = function (stream) { + var state = this._readableState; + var paused = false; + + var self = this; + stream.on('end', function () { + debug('wrapped end'); + if (state.decoder && !state.ended) { + var chunk = state.decoder.end(); + if (chunk && chunk.length) self.push(chunk); + } + + self.push(null); + }); + + stream.on('data', function (chunk) { + debug('wrapped data'); + if (state.decoder) chunk = state.decoder.write(chunk); + + // don't skip over falsy values in objectMode + if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; + + var ret = self.push(chunk); + if (!ret) { + paused = true; + stream.pause(); + } + }); + + // proxy all the other methods. + // important when wrapping filters and duplexes. + for (var i in stream) { + if (this[i] === undefined && typeof stream[i] === 'function') { + this[i] = function (method) { + return function () { + return stream[method].apply(stream, arguments); + }; + }(i); + } + } + + // proxy certain important events. + for (var n = 0; n < kProxyEvents.length; n++) { + stream.on(kProxyEvents[n], self.emit.bind(self, kProxyEvents[n])); + } + + // when we try to consume some more bytes, simply unpause the + // underlying stream. + self._read = function (n) { + debug('wrapped _read', n); + if (paused) { + paused = false; + stream.resume(); + } + }; + + return self; +}; + +// exposed for testing purposes only. +Readable._fromList = fromList; + +// Pluck off n bytes from an array of buffers. +// Length is the combined lengths of all the buffers in the list. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function fromList(n, state) { + // nothing buffered + if (state.length === 0) return null; + + var ret; + if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { + // read it all, truncate the list + if (state.decoder) ret = state.buffer.join('');else if (state.buffer.length === 1) ret = state.buffer.head.data;else ret = state.buffer.concat(state.length); + state.buffer.clear(); + } else { + // read part of list + ret = fromListPartial(n, state.buffer, state.decoder); + } + + return ret; +} + +// Extracts only enough buffered data to satisfy the amount requested. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function fromListPartial(n, list, hasStrings) { + var ret; + if (n < list.head.data.length) { + // slice is the same for buffers and strings + ret = list.head.data.slice(0, n); + list.head.data = list.head.data.slice(n); + } else if (n === list.head.data.length) { + // first chunk is a perfect match + ret = list.shift(); + } else { + // result spans more than one buffer + ret = hasStrings ? copyFromBufferString(n, list) : copyFromBuffer(n, list); + } + return ret; +} + +// Copies a specified amount of characters from the list of buffered data +// chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function copyFromBufferString(n, list) { + var p = list.head; + var c = 1; + var ret = p.data; + n -= ret.length; + while (p = p.next) { + var str = p.data; + var nb = n > str.length ? str.length : n; + if (nb === str.length) ret += str;else ret += str.slice(0, n); + n -= nb; + if (n === 0) { + if (nb === str.length) { + ++c; + if (p.next) list.head = p.next;else list.head = list.tail = null; + } else { + list.head = p; + p.data = str.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; +} + +// Copies a specified amount of bytes from the list of buffered data chunks. +// This function is designed to be inlinable, so please take care when making +// changes to the function body. +function copyFromBuffer(n, list) { + var ret = Buffer.allocUnsafe(n); + var p = list.head; + var c = 1; + p.data.copy(ret); + n -= p.data.length; + while (p = p.next) { + var buf = p.data; + var nb = n > buf.length ? buf.length : n; + buf.copy(ret, ret.length - n, 0, nb); + n -= nb; + if (n === 0) { + if (nb === buf.length) { + ++c; + if (p.next) list.head = p.next;else list.head = list.tail = null; + } else { + list.head = p; + p.data = buf.slice(nb); + } + break; + } + ++c; + } + list.length -= c; + return ret; +} + +function endReadable(stream) { + var state = stream._readableState; + + // If we get here before consuming all the bytes, then that is a + // bug in node. Should never happen. + if (state.length > 0) throw new Error('"endReadable()" called on non-empty stream'); + + if (!state.endEmitted) { + state.ended = true; + processNextTick(endReadableNT, state, stream); + } +} + +function endReadableNT(state, stream) { + // Check that we didn't get one last unshift. + if (!state.endEmitted && state.length === 0) { + state.endEmitted = true; + stream.readable = false; + stream.emit('end'); + } +} + +function forEach(xs, f) { + for (var i = 0, l = xs.length; i < l; i++) { + f(xs[i], i); + } +} + +function indexOf(xs, x) { + for (var i = 0, l = xs.length; i < l; i++) { + if (xs[i] === x) return i; + } + return -1; +} +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./_stream_duplex":19,"./internal/streams/BufferList":24,"./internal/streams/destroy":25,"./internal/streams/stream":26,"_process":17,"core-util-is":10,"events":11,"inherits":13,"isarray":15,"process-nextick-args":16,"safe-buffer":28,"string_decoder/":29,"util":8}],22:[function(require,module,exports){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// a transform stream is a readable/writable stream where you do +// something with the data. Sometimes it's called a "filter", +// but that's not a great name for it, since that implies a thing where +// some bits pass through, and others are simply ignored. (That would +// be a valid example of a transform, of course.) +// +// While the output is causally related to the input, it's not a +// necessarily symmetric or synchronous transformation. For example, +// a zlib stream might take multiple plain-text writes(), and then +// emit a single compressed chunk some time in the future. +// +// Here's how this works: +// +// The Transform stream has all the aspects of the readable and writable +// stream classes. When you write(chunk), that calls _write(chunk,cb) +// internally, and returns false if there's a lot of pending writes +// buffered up. When you call read(), that calls _read(n) until +// there's enough pending readable data buffered up. +// +// In a transform stream, the written data is placed in a buffer. When +// _read(n) is called, it transforms the queued up data, calling the +// buffered _write cb's as it consumes chunks. If consuming a single +// written chunk would result in multiple output chunks, then the first +// outputted bit calls the readcb, and subsequent chunks just go into +// the read buffer, and will cause it to emit 'readable' if necessary. +// +// This way, back-pressure is actually determined by the reading side, +// since _read has to be called to start processing a new chunk. However, +// a pathological inflate type of transform can cause excessive buffering +// here. For example, imagine a stream where every byte of input is +// interpreted as an integer from 0-255, and then results in that many +// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in +// 1kb of data being output. In this case, you could write a very small +// amount of input, and end up with a very large amount of output. In +// such a pathological inflating mechanism, there'd be no way to tell +// the system to stop doing the transform. A single 4MB write could +// cause the system to run out of memory. +// +// However, even in such a pathological case, only a single written chunk +// would be consumed, and then the rest would wait (un-transformed) until +// the results of the previous transformed chunk were consumed. + +'use strict'; + +module.exports = Transform; + +var Duplex = require('./_stream_duplex'); + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +util.inherits(Transform, Duplex); + +function TransformState(stream) { + this.afterTransform = function (er, data) { + return afterTransform(stream, er, data); + }; + + this.needTransform = false; + this.transforming = false; + this.writecb = null; + this.writechunk = null; + this.writeencoding = null; +} + +function afterTransform(stream, er, data) { + var ts = stream._transformState; + ts.transforming = false; + + var cb = ts.writecb; + + if (!cb) { + return stream.emit('error', new Error('write callback called multiple times')); + } + + ts.writechunk = null; + ts.writecb = null; + + if (data !== null && data !== undefined) stream.push(data); + + cb(er); + + var rs = stream._readableState; + rs.reading = false; + if (rs.needReadable || rs.length < rs.highWaterMark) { + stream._read(rs.highWaterMark); + } +} + +function Transform(options) { + if (!(this instanceof Transform)) return new Transform(options); + + Duplex.call(this, options); + + this._transformState = new TransformState(this); + + var stream = this; + + // start out asking for a readable event once data is transformed. + this._readableState.needReadable = true; + + // we have implemented the _read method, and done the other things + // that Readable wants before the first _read call, so unset the + // sync guard flag. + this._readableState.sync = false; + + if (options) { + if (typeof options.transform === 'function') this._transform = options.transform; + + if (typeof options.flush === 'function') this._flush = options.flush; + } + + // When the writable side finishes, then flush out anything remaining. + this.once('prefinish', function () { + if (typeof this._flush === 'function') this._flush(function (er, data) { + done(stream, er, data); + });else done(stream); + }); +} + +Transform.prototype.push = function (chunk, encoding) { + this._transformState.needTransform = false; + return Duplex.prototype.push.call(this, chunk, encoding); +}; + +// This is the part where you do stuff! +// override this function in implementation classes. +// 'chunk' is an input chunk. +// +// Call `push(newChunk)` to pass along transformed output +// to the readable side. You may call 'push' zero or more times. +// +// Call `cb(err)` when you are done with this chunk. If you pass +// an error, then that'll put the hurt on the whole operation. If you +// never call cb(), then you'll never get another chunk. +Transform.prototype._transform = function (chunk, encoding, cb) { + throw new Error('_transform() is not implemented'); +}; + +Transform.prototype._write = function (chunk, encoding, cb) { + var ts = this._transformState; + ts.writecb = cb; + ts.writechunk = chunk; + ts.writeencoding = encoding; + if (!ts.transforming) { + var rs = this._readableState; + if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark); + } +}; + +// Doesn't matter what the args are here. +// _transform does all the work. +// That we got here means that the readable side wants more data. +Transform.prototype._read = function (n) { + var ts = this._transformState; + + if (ts.writechunk !== null && ts.writecb && !ts.transforming) { + ts.transforming = true; + this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); + } else { + // mark that we need a transform, so that any data that comes in + // will get processed, now that we've asked for it. + ts.needTransform = true; + } +}; + +Transform.prototype._destroy = function (err, cb) { + var _this = this; + + Duplex.prototype._destroy.call(this, err, function (err2) { + cb(err2); + _this.emit('close'); + }); +}; + +function done(stream, er, data) { + if (er) return stream.emit('error', er); + + if (data !== null && data !== undefined) stream.push(data); + + // if there's nothing in the write buffer, then that means + // that nothing more will ever be provided + var ws = stream._writableState; + var ts = stream._transformState; + + if (ws.length) throw new Error('Calling transform done when ws.length != 0'); + + if (ts.transforming) throw new Error('Calling transform done when still transforming'); + + return stream.push(null); +} +},{"./_stream_duplex":19,"core-util-is":10,"inherits":13}],23:[function(require,module,exports){ +(function (process,global){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +// A bit simpler than readable streams. +// Implement an async ._write(chunk, encoding, cb), and it'll handle all +// the drain event emission and buffering. + +'use strict'; + +/**/ + +var processNextTick = require('process-nextick-args'); +/**/ + +module.exports = Writable; + +/* */ +function WriteReq(chunk, encoding, cb) { + this.chunk = chunk; + this.encoding = encoding; + this.callback = cb; + this.next = null; +} + +// It seems a linked list but it is not +// there will be only 2 of these for each stream +function CorkedRequest(state) { + var _this = this; + + this.next = null; + this.entry = null; + this.finish = function () { + onCorkedFinish(_this, state); + }; +} +/* */ + +/**/ +var asyncWrite = !process.browser && ['v0.10', 'v0.9.'].indexOf(process.version.slice(0, 5)) > -1 ? setImmediate : processNextTick; +/**/ + +/**/ +var Duplex; +/**/ + +Writable.WritableState = WritableState; + +/**/ +var util = require('core-util-is'); +util.inherits = require('inherits'); +/**/ + +/**/ +var internalUtil = { + deprecate: require('util-deprecate') +}; +/**/ + +/**/ +var Stream = require('./internal/streams/stream'); +/**/ + +/**/ +var Buffer = require('safe-buffer').Buffer; +var OurUint8Array = global.Uint8Array || function () {}; +function _uint8ArrayToBuffer(chunk) { + return Buffer.from(chunk); +} +function _isUint8Array(obj) { + return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; +} +/**/ + +var destroyImpl = require('./internal/streams/destroy'); + +util.inherits(Writable, Stream); + +function nop() {} + +function WritableState(options, stream) { + Duplex = Duplex || require('./_stream_duplex'); + + options = options || {}; + + // object stream flag to indicate whether or not this stream + // contains buffers or objects. + this.objectMode = !!options.objectMode; + + if (stream instanceof Duplex) this.objectMode = this.objectMode || !!options.writableObjectMode; + + // the point at which write() starts returning false + // Note: 0 is a valid value, means that we always return false if + // the entire buffer is not flushed immediately on write() + var hwm = options.highWaterMark; + var defaultHwm = this.objectMode ? 16 : 16 * 1024; + this.highWaterMark = hwm || hwm === 0 ? hwm : defaultHwm; + + // cast to ints. + this.highWaterMark = Math.floor(this.highWaterMark); + + // if _final has been called + this.finalCalled = false; + + // drain event flag. + this.needDrain = false; + // at the start of calling end() + this.ending = false; + // when end() has been called, and returned + this.ended = false; + // when 'finish' is emitted + this.finished = false; + + // has it been destroyed + this.destroyed = false; + + // should we decode strings into buffers before passing to _write? + // this is here so that some node-core streams can optimize string + // handling at a lower level. + var noDecode = options.decodeStrings === false; + this.decodeStrings = !noDecode; + + // Crypto is kind of old and crusty. Historically, its default string + // encoding is 'binary' so we have to make this configurable. + // Everything else in the universe uses 'utf8', though. + this.defaultEncoding = options.defaultEncoding || 'utf8'; + + // not an actual buffer we keep track of, but a measurement + // of how much we're waiting to get pushed to some underlying + // socket or file. + this.length = 0; + + // a flag to see when we're in the middle of a write. + this.writing = false; + + // when true all writes will be buffered until .uncork() call + this.corked = 0; + + // a flag to be able to tell if the onwrite cb is called immediately, + // or on a later tick. We set this to true at first, because any + // actions that shouldn't happen until "later" should generally also + // not happen before the first write call. + this.sync = true; + + // a flag to know if we're processing previously buffered items, which + // may call the _write() callback in the same tick, so that we don't + // end up in an overlapped onwrite situation. + this.bufferProcessing = false; + + // the callback that's passed to _write(chunk,cb) + this.onwrite = function (er) { + onwrite(stream, er); + }; + + // the callback that the user supplies to write(chunk,encoding,cb) + this.writecb = null; + + // the amount that is being written when _write is called. + this.writelen = 0; + + this.bufferedRequest = null; + this.lastBufferedRequest = null; + + // number of pending user-supplied write callbacks + // this must be 0 before 'finish' can be emitted + this.pendingcb = 0; + + // emit prefinish if the only thing we're waiting for is _write cbs + // This is relevant for synchronous Transform streams + this.prefinished = false; + + // True if the error was already emitted and should not be thrown again + this.errorEmitted = false; + + // count buffered requests + this.bufferedRequestCount = 0; + + // allocate the first CorkedRequest, there is always + // one allocated and free to use, and we maintain at most two + this.corkedRequestsFree = new CorkedRequest(this); +} + +WritableState.prototype.getBuffer = function getBuffer() { + var current = this.bufferedRequest; + var out = []; + while (current) { + out.push(current); + current = current.next; + } + return out; +}; + +(function () { + try { + Object.defineProperty(WritableState.prototype, 'buffer', { + get: internalUtil.deprecate(function () { + return this.getBuffer(); + }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.', 'DEP0003') + }); + } catch (_) {} +})(); + +// Test _writableState for inheritance to account for Duplex streams, +// whose prototype chain only points to Readable. +var realHasInstance; +if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { + realHasInstance = Function.prototype[Symbol.hasInstance]; + Object.defineProperty(Writable, Symbol.hasInstance, { + value: function (object) { + if (realHasInstance.call(this, object)) return true; + + return object && object._writableState instanceof WritableState; + } + }); +} else { + realHasInstance = function (object) { + return object instanceof this; + }; +} + +function Writable(options) { + Duplex = Duplex || require('./_stream_duplex'); + + // Writable ctor is applied to Duplexes, too. + // `realHasInstance` is necessary because using plain `instanceof` + // would return false, as no `_writableState` property is attached. + + // Trying to use the custom `instanceof` for Writable here will also break the + // Node.js LazyTransform implementation, which has a non-trivial getter for + // `_writableState` that would lead to infinite recursion. + if (!realHasInstance.call(Writable, this) && !(this instanceof Duplex)) { + return new Writable(options); + } + + this._writableState = new WritableState(options, this); + + // legacy. + this.writable = true; + + if (options) { + if (typeof options.write === 'function') this._write = options.write; + + if (typeof options.writev === 'function') this._writev = options.writev; + + if (typeof options.destroy === 'function') this._destroy = options.destroy; + + if (typeof options.final === 'function') this._final = options.final; + } + + Stream.call(this); +} + +// Otherwise people can pipe Writable streams, which is just wrong. +Writable.prototype.pipe = function () { + this.emit('error', new Error('Cannot pipe, not readable')); +}; + +function writeAfterEnd(stream, cb) { + var er = new Error('write after end'); + // TODO: defer error events consistently everywhere, not just the cb + stream.emit('error', er); + processNextTick(cb, er); +} + +// Checks that a user-supplied chunk is valid, especially for the particular +// mode the stream is in. Currently this means that `null` is never accepted +// and undefined/non-string values are only allowed in object mode. +function validChunk(stream, state, chunk, cb) { + var valid = true; + var er = false; + + if (chunk === null) { + er = new TypeError('May not write null values to stream'); + } else if (typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { + er = new TypeError('Invalid non-string/buffer chunk'); + } + if (er) { + stream.emit('error', er); + processNextTick(cb, er); + valid = false; + } + return valid; +} + +Writable.prototype.write = function (chunk, encoding, cb) { + var state = this._writableState; + var ret = false; + var isBuf = _isUint8Array(chunk) && !state.objectMode; + + if (isBuf && !Buffer.isBuffer(chunk)) { + chunk = _uint8ArrayToBuffer(chunk); + } + + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; + + if (typeof cb !== 'function') cb = nop; + + if (state.ended) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) { + state.pendingcb++; + ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); + } + + return ret; +}; + +Writable.prototype.cork = function () { + var state = this._writableState; + + state.corked++; +}; + +Writable.prototype.uncork = function () { + var state = this._writableState; + + if (state.corked) { + state.corked--; + + if (!state.writing && !state.corked && !state.finished && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); + } +}; + +Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) { + // node::ParseEncoding() requires lower case. + if (typeof encoding === 'string') encoding = encoding.toLowerCase(); + if (!(['hex', 'utf8', 'utf-8', 'ascii', 'binary', 'base64', 'ucs2', 'ucs-2', 'utf16le', 'utf-16le', 'raw'].indexOf((encoding + '').toLowerCase()) > -1)) throw new TypeError('Unknown encoding: ' + encoding); + this._writableState.defaultEncoding = encoding; + return this; +}; + +function decodeChunk(state, chunk, encoding) { + if (!state.objectMode && state.decodeStrings !== false && typeof chunk === 'string') { + chunk = Buffer.from(chunk, encoding); + } + return chunk; +} + +// if we're already writing something, then just put this +// in the queue, and wait our turn. Otherwise, call _write +// If we return false, then we need a drain event, so set that flag. +function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { + if (!isBuf) { + var newChunk = decodeChunk(state, chunk, encoding); + if (chunk !== newChunk) { + isBuf = true; + encoding = 'buffer'; + chunk = newChunk; + } + } + var len = state.objectMode ? 1 : chunk.length; + + state.length += len; + + var ret = state.length < state.highWaterMark; + // we must ensure that previous needDrain will not be reset to false. + if (!ret) state.needDrain = true; + + if (state.writing || state.corked) { + var last = state.lastBufferedRequest; + state.lastBufferedRequest = { + chunk: chunk, + encoding: encoding, + isBuf: isBuf, + callback: cb, + next: null + }; + if (last) { + last.next = state.lastBufferedRequest; + } else { + state.bufferedRequest = state.lastBufferedRequest; + } + state.bufferedRequestCount += 1; + } else { + doWrite(stream, state, false, len, chunk, encoding, cb); + } + + return ret; +} + +function doWrite(stream, state, writev, len, chunk, encoding, cb) { + state.writelen = len; + state.writecb = cb; + state.writing = true; + state.sync = true; + if (writev) stream._writev(chunk, state.onwrite);else stream._write(chunk, encoding, state.onwrite); + state.sync = false; +} + +function onwriteError(stream, state, sync, er, cb) { + --state.pendingcb; + + if (sync) { + // defer the callback if we are being called synchronously + // to avoid piling up things on the stack + processNextTick(cb, er); + // this can emit finish, and it will always happen + // after error + processNextTick(finishMaybe, stream, state); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + } else { + // the caller expect this to happen before if + // it is async + cb(er); + stream._writableState.errorEmitted = true; + stream.emit('error', er); + // this can emit finish, but finish must + // always follow error + finishMaybe(stream, state); + } +} + +function onwriteStateUpdate(state) { + state.writing = false; + state.writecb = null; + state.length -= state.writelen; + state.writelen = 0; +} + +function onwrite(stream, er) { + var state = stream._writableState; + var sync = state.sync; + var cb = state.writecb; + + onwriteStateUpdate(state); + + if (er) onwriteError(stream, state, sync, er, cb);else { + // Check if we're actually ready to finish, but don't emit yet + var finished = needFinish(state); + + if (!finished && !state.corked && !state.bufferProcessing && state.bufferedRequest) { + clearBuffer(stream, state); + } + + if (sync) { + /**/ + asyncWrite(afterWrite, stream, state, finished, cb); + /**/ + } else { + afterWrite(stream, state, finished, cb); + } + } +} + +function afterWrite(stream, state, finished, cb) { + if (!finished) onwriteDrain(stream, state); + state.pendingcb--; + cb(); + finishMaybe(stream, state); +} + +// Must force callback to be called on nextTick, so that we don't +// emit 'drain' before the write() consumer gets the 'false' return +// value, and has a chance to attach a 'drain' listener. +function onwriteDrain(stream, state) { + if (state.length === 0 && state.needDrain) { + state.needDrain = false; + stream.emit('drain'); + } +} + +// if there's something in the buffer waiting, then process it +function clearBuffer(stream, state) { + state.bufferProcessing = true; + var entry = state.bufferedRequest; + + if (stream._writev && entry && entry.next) { + // Fast case, write everything using _writev() + var l = state.bufferedRequestCount; + var buffer = new Array(l); + var holder = state.corkedRequestsFree; + holder.entry = entry; + + var count = 0; + var allBuffers = true; + while (entry) { + buffer[count] = entry; + if (!entry.isBuf) allBuffers = false; + entry = entry.next; + count += 1; + } + buffer.allBuffers = allBuffers; + + doWrite(stream, state, true, state.length, buffer, '', holder.finish); + + // doWrite is almost always async, defer these to save a bit of time + // as the hot path ends with doWrite + state.pendingcb++; + state.lastBufferedRequest = null; + if (holder.next) { + state.corkedRequestsFree = holder.next; + holder.next = null; + } else { + state.corkedRequestsFree = new CorkedRequest(state); + } + } else { + // Slow case, write chunks one-by-one + while (entry) { + var chunk = entry.chunk; + var encoding = entry.encoding; + var cb = entry.callback; + var len = state.objectMode ? 1 : chunk.length; + + doWrite(stream, state, false, len, chunk, encoding, cb); + entry = entry.next; + // if we didn't call the onwrite immediately, then + // it means that we need to wait until it does. + // also, that means that the chunk and cb are currently + // being processed, so move the buffer counter past them. + if (state.writing) { + break; + } + } + + if (entry === null) state.lastBufferedRequest = null; + } + + state.bufferedRequestCount = 0; + state.bufferedRequest = entry; + state.bufferProcessing = false; +} + +Writable.prototype._write = function (chunk, encoding, cb) { + cb(new Error('_write() is not implemented')); +}; + +Writable.prototype._writev = null; + +Writable.prototype.end = function (chunk, encoding, cb) { + var state = this._writableState; + + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); + + // .end() fully uncorks + if (state.corked) { + state.corked = 1; + this.uncork(); + } + + // ignore unnecessary end() calls. + if (!state.ending && !state.finished) endWritable(this, state, cb); +}; + +function needFinish(state) { + return state.ending && state.length === 0 && state.bufferedRequest === null && !state.finished && !state.writing; +} +function callFinal(stream, state) { + stream._final(function (err) { + state.pendingcb--; + if (err) { + stream.emit('error', err); + } + state.prefinished = true; + stream.emit('prefinish'); + finishMaybe(stream, state); + }); +} +function prefinish(stream, state) { + if (!state.prefinished && !state.finalCalled) { + if (typeof stream._final === 'function') { + state.pendingcb++; + state.finalCalled = true; + processNextTick(callFinal, stream, state); + } else { + state.prefinished = true; + stream.emit('prefinish'); + } + } +} + +function finishMaybe(stream, state) { + var need = needFinish(state); + if (need) { + prefinish(stream, state); + if (state.pendingcb === 0) { + state.finished = true; + stream.emit('finish'); + } + } + return need; +} + +function endWritable(stream, state, cb) { + state.ending = true; + finishMaybe(stream, state); + if (cb) { + if (state.finished) processNextTick(cb);else stream.once('finish', cb); + } + state.ended = true; + stream.writable = false; +} + +function onCorkedFinish(corkReq, state, err) { + var entry = corkReq.entry; + corkReq.entry = null; + while (entry) { + var cb = entry.callback; + state.pendingcb--; + cb(err); + entry = entry.next; + } + if (state.corkedRequestsFree) { + state.corkedRequestsFree.next = corkReq; + } else { + state.corkedRequestsFree = corkReq; + } +} + +Object.defineProperty(Writable.prototype, 'destroyed', { + get: function () { + if (this._writableState === undefined) { + return false; + } + return this._writableState.destroyed; + }, + set: function (value) { + // we ignore the value if the stream + // has not been initialized yet + if (!this._writableState) { + return; + } + + // backward compatibility, the user is explicitly + // managing destroyed + this._writableState.destroyed = value; + } +}); + +Writable.prototype.destroy = destroyImpl.destroy; +Writable.prototype._undestroy = destroyImpl.undestroy; +Writable.prototype._destroy = function (err, cb) { + this.end(); + cb(err); +}; +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./_stream_duplex":19,"./internal/streams/destroy":25,"./internal/streams/stream":26,"_process":17,"core-util-is":10,"inherits":13,"process-nextick-args":16,"safe-buffer":28,"util-deprecate":30}],24:[function(require,module,exports){ +'use strict'; + +/**/ + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +var Buffer = require('safe-buffer').Buffer; +/**/ + +function copyBuffer(src, target, offset) { + src.copy(target, offset); +} + +module.exports = function () { + function BufferList() { + _classCallCheck(this, BufferList); + + this.head = null; + this.tail = null; + this.length = 0; + } + + BufferList.prototype.push = function push(v) { + var entry = { data: v, next: null }; + if (this.length > 0) this.tail.next = entry;else this.head = entry; + this.tail = entry; + ++this.length; + }; + + BufferList.prototype.unshift = function unshift(v) { + var entry = { data: v, next: this.head }; + if (this.length === 0) this.tail = entry; + this.head = entry; + ++this.length; + }; + + BufferList.prototype.shift = function shift() { + if (this.length === 0) return; + var ret = this.head.data; + if (this.length === 1) this.head = this.tail = null;else this.head = this.head.next; + --this.length; + return ret; + }; + + BufferList.prototype.clear = function clear() { + this.head = this.tail = null; + this.length = 0; + }; + + BufferList.prototype.join = function join(s) { + if (this.length === 0) return ''; + var p = this.head; + var ret = '' + p.data; + while (p = p.next) { + ret += s + p.data; + }return ret; + }; + + BufferList.prototype.concat = function concat(n) { + if (this.length === 0) return Buffer.alloc(0); + if (this.length === 1) return this.head.data; + var ret = Buffer.allocUnsafe(n >>> 0); + var p = this.head; + var i = 0; + while (p) { + copyBuffer(p.data, ret, i); + i += p.data.length; + p = p.next; + } + return ret; + }; + + return BufferList; +}(); +},{"safe-buffer":28}],25:[function(require,module,exports){ +'use strict'; + +/**/ + +var processNextTick = require('process-nextick-args'); +/**/ + +// undocumented cb() API, needed for core, not for public API +function destroy(err, cb) { + var _this = this; + + var readableDestroyed = this._readableState && this._readableState.destroyed; + var writableDestroyed = this._writableState && this._writableState.destroyed; + + if (readableDestroyed || writableDestroyed) { + if (cb) { + cb(err); + } else if (err && (!this._writableState || !this._writableState.errorEmitted)) { + processNextTick(emitErrorNT, this, err); + } + return; + } + + // we set destroyed to true before firing error callbacks in order + // to make it re-entrance safe in case destroy() is called within callbacks + + if (this._readableState) { + this._readableState.destroyed = true; + } + + // if this is a duplex stream mark the writable part as destroyed as well + if (this._writableState) { + this._writableState.destroyed = true; + } + + this._destroy(err || null, function (err) { + if (!cb && err) { + processNextTick(emitErrorNT, _this, err); + if (_this._writableState) { + _this._writableState.errorEmitted = true; + } + } else if (cb) { + cb(err); + } + }); +} + +function undestroy() { + if (this._readableState) { + this._readableState.destroyed = false; + this._readableState.reading = false; + this._readableState.ended = false; + this._readableState.endEmitted = false; + } + + if (this._writableState) { + this._writableState.destroyed = false; + this._writableState.ended = false; + this._writableState.ending = false; + this._writableState.finished = false; + this._writableState.errorEmitted = false; + } +} + +function emitErrorNT(self, err) { + self.emit('error', err); +} + +module.exports = { + destroy: destroy, + undestroy: undestroy +}; +},{"process-nextick-args":16}],26:[function(require,module,exports){ +module.exports = require('events').EventEmitter; + +},{"events":11}],27:[function(require,module,exports){ +exports = module.exports = require('./lib/_stream_readable.js'); +exports.Stream = exports; +exports.Readable = exports; +exports.Writable = require('./lib/_stream_writable.js'); +exports.Duplex = require('./lib/_stream_duplex.js'); +exports.Transform = require('./lib/_stream_transform.js'); +exports.PassThrough = require('./lib/_stream_passthrough.js'); + +},{"./lib/_stream_duplex.js":19,"./lib/_stream_passthrough.js":20,"./lib/_stream_readable.js":21,"./lib/_stream_transform.js":22,"./lib/_stream_writable.js":23}],28:[function(require,module,exports){ +/* eslint-disable node/no-deprecated-api */ +var buffer = require('buffer') +var Buffer = buffer.Buffer + +// alternative to using Object.keys for old browsers +function copyProps (src, dst) { + for (var key in src) { + dst[key] = src[key] + } +} +if (Buffer.from && Buffer.alloc && Buffer.allocUnsafe && Buffer.allocUnsafeSlow) { + module.exports = buffer +} else { + // Copy properties from require('buffer') + copyProps(buffer, exports) + exports.Buffer = SafeBuffer +} + +function SafeBuffer (arg, encodingOrOffset, length) { + return Buffer(arg, encodingOrOffset, length) +} + +// Copy static methods from Buffer +copyProps(Buffer, SafeBuffer) + +SafeBuffer.from = function (arg, encodingOrOffset, length) { + if (typeof arg === 'number') { + throw new TypeError('Argument must not be a number') + } + return Buffer(arg, encodingOrOffset, length) +} + +SafeBuffer.alloc = function (size, fill, encoding) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + var buf = Buffer(size) + if (fill !== undefined) { + if (typeof encoding === 'string') { + buf.fill(fill, encoding) + } else { + buf.fill(fill) + } + } else { + buf.fill(0) + } + return buf +} + +SafeBuffer.allocUnsafe = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + return Buffer(size) +} + +SafeBuffer.allocUnsafeSlow = function (size) { + if (typeof size !== 'number') { + throw new TypeError('Argument must be a number') + } + return buffer.SlowBuffer(size) +} + +},{"buffer":9}],29:[function(require,module,exports){ +'use strict'; + +var Buffer = require('safe-buffer').Buffer; + +var isEncoding = Buffer.isEncoding || function (encoding) { + encoding = '' + encoding; + switch (encoding && encoding.toLowerCase()) { + case 'hex':case 'utf8':case 'utf-8':case 'ascii':case 'binary':case 'base64':case 'ucs2':case 'ucs-2':case 'utf16le':case 'utf-16le':case 'raw': + return true; + default: + return false; + } +}; + +function _normalizeEncoding(enc) { + if (!enc) return 'utf8'; + var retried; + while (true) { + switch (enc) { + case 'utf8': + case 'utf-8': + return 'utf8'; + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return 'utf16le'; + case 'latin1': + case 'binary': + return 'latin1'; + case 'base64': + case 'ascii': + case 'hex': + return enc; + default: + if (retried) return; // undefined + enc = ('' + enc).toLowerCase(); + retried = true; + } + } +}; + +// Do not cache `Buffer.isEncoding` when checking encoding names as some +// modules monkey-patch it to support additional encodings +function normalizeEncoding(enc) { + var nenc = _normalizeEncoding(enc); + if (typeof nenc !== 'string' && (Buffer.isEncoding === isEncoding || !isEncoding(enc))) throw new Error('Unknown encoding: ' + enc); + return nenc || enc; +} + +// StringDecoder provides an interface for efficiently splitting a series of +// buffers into a series of JS strings without breaking apart multi-byte +// characters. +exports.StringDecoder = StringDecoder; +function StringDecoder(encoding) { + this.encoding = normalizeEncoding(encoding); + var nb; + switch (this.encoding) { + case 'utf16le': + this.text = utf16Text; + this.end = utf16End; + nb = 4; + break; + case 'utf8': + this.fillLast = utf8FillLast; + nb = 4; + break; + case 'base64': + this.text = base64Text; + this.end = base64End; + nb = 3; + break; + default: + this.write = simpleWrite; + this.end = simpleEnd; + return; + } + this.lastNeed = 0; + this.lastTotal = 0; + this.lastChar = Buffer.allocUnsafe(nb); +} + +StringDecoder.prototype.write = function (buf) { + if (buf.length === 0) return ''; + var r; + var i; + if (this.lastNeed) { + r = this.fillLast(buf); + if (r === undefined) return ''; + i = this.lastNeed; + this.lastNeed = 0; + } else { + i = 0; + } + if (i < buf.length) return r ? r + this.text(buf, i) : this.text(buf, i); + return r || ''; +}; + +StringDecoder.prototype.end = utf8End; + +// Returns only complete characters in a Buffer +StringDecoder.prototype.text = utf8Text; + +// Attempts to complete a partial non-UTF-8 character using bytes from a Buffer +StringDecoder.prototype.fillLast = function (buf) { + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, this.lastTotal - this.lastNeed, 0, buf.length); + this.lastNeed -= buf.length; +}; + +// Checks the type of a UTF-8 byte, whether it's ASCII, a leading byte, or a +// continuation byte. +function utf8CheckByte(byte) { + if (byte <= 0x7F) return 0;else if (byte >> 5 === 0x06) return 2;else if (byte >> 4 === 0x0E) return 3;else if (byte >> 3 === 0x1E) return 4; + return -1; +} + +// Checks at most 3 bytes at the end of a Buffer in order to detect an +// incomplete multi-byte UTF-8 character. The total number of bytes (2, 3, or 4) +// needed to complete the UTF-8 character (if applicable) are returned. +function utf8CheckIncomplete(self, buf, i) { + var j = buf.length - 1; + if (j < i) return 0; + var nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 1; + return nb; + } + if (--j < i) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) self.lastNeed = nb - 2; + return nb; + } + if (--j < i) return 0; + nb = utf8CheckByte(buf[j]); + if (nb >= 0) { + if (nb > 0) { + if (nb === 2) nb = 0;else self.lastNeed = nb - 3; + } + return nb; + } + return 0; +} + +// Validates as many continuation bytes for a multi-byte UTF-8 character as +// needed or are available. If we see a non-continuation byte where we expect +// one, we "replace" the validated continuation bytes we've seen so far with +// UTF-8 replacement characters ('\ufffd'), to match v8's UTF-8 decoding +// behavior. The continuation byte check is included three times in the case +// where all of the continuation bytes for a character exist in the same buffer. +// It is also done this way as a slight performance increase instead of using a +// loop. +function utf8CheckExtraBytes(self, buf, p) { + if ((buf[0] & 0xC0) !== 0x80) { + self.lastNeed = 0; + return '\ufffd'.repeat(p); + } + if (self.lastNeed > 1 && buf.length > 1) { + if ((buf[1] & 0xC0) !== 0x80) { + self.lastNeed = 1; + return '\ufffd'.repeat(p + 1); + } + if (self.lastNeed > 2 && buf.length > 2) { + if ((buf[2] & 0xC0) !== 0x80) { + self.lastNeed = 2; + return '\ufffd'.repeat(p + 2); + } + } + } +} + +// Attempts to complete a multi-byte UTF-8 character using bytes from a Buffer. +function utf8FillLast(buf) { + var p = this.lastTotal - this.lastNeed; + var r = utf8CheckExtraBytes(this, buf, p); + if (r !== undefined) return r; + if (this.lastNeed <= buf.length) { + buf.copy(this.lastChar, p, 0, this.lastNeed); + return this.lastChar.toString(this.encoding, 0, this.lastTotal); + } + buf.copy(this.lastChar, p, 0, buf.length); + this.lastNeed -= buf.length; +} + +// Returns all complete UTF-8 characters in a Buffer. If the Buffer ended on a +// partial character, the character's bytes are buffered until the required +// number of bytes are available. +function utf8Text(buf, i) { + var total = utf8CheckIncomplete(this, buf, i); + if (!this.lastNeed) return buf.toString('utf8', i); + this.lastTotal = total; + var end = buf.length - (total - this.lastNeed); + buf.copy(this.lastChar, 0, end); + return buf.toString('utf8', i, end); +} + +// For UTF-8, a replacement character for each buffered byte of a (partial) +// character needs to be added to the output. +function utf8End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + '\ufffd'.repeat(this.lastTotal - this.lastNeed); + return r; +} + +// UTF-16LE typically needs two bytes per character, but even if we have an even +// number of bytes available, we need to check if we end on a leading/high +// surrogate. In that case, we need to wait for the next two bytes in order to +// decode the last character properly. +function utf16Text(buf, i) { + if ((buf.length - i) % 2 === 0) { + var r = buf.toString('utf16le', i); + if (r) { + var c = r.charCodeAt(r.length - 1); + if (c >= 0xD800 && c <= 0xDBFF) { + this.lastNeed = 2; + this.lastTotal = 4; + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + return r.slice(0, -1); + } + } + return r; + } + this.lastNeed = 1; + this.lastTotal = 2; + this.lastChar[0] = buf[buf.length - 1]; + return buf.toString('utf16le', i, buf.length - 1); +} + +// For UTF-16LE we do not explicitly append special replacement characters if we +// end on a partial character, we simply let v8 handle that. +function utf16End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) { + var end = this.lastTotal - this.lastNeed; + return r + this.lastChar.toString('utf16le', 0, end); + } + return r; +} + +function base64Text(buf, i) { + var n = (buf.length - i) % 3; + if (n === 0) return buf.toString('base64', i); + this.lastNeed = 3 - n; + this.lastTotal = 3; + if (n === 1) { + this.lastChar[0] = buf[buf.length - 1]; + } else { + this.lastChar[0] = buf[buf.length - 2]; + this.lastChar[1] = buf[buf.length - 1]; + } + return buf.toString('base64', i, buf.length - n); +} + +function base64End(buf) { + var r = buf && buf.length ? this.write(buf) : ''; + if (this.lastNeed) return r + this.lastChar.toString('base64', 0, 3 - this.lastNeed); + return r; +} + +// Pass bytes on through for single-byte encodings (e.g. ascii, latin1, hex) +function simpleWrite(buf) { + return buf.toString(this.encoding); +} + +function simpleEnd(buf) { + return buf && buf.length ? this.write(buf) : ''; +} +},{"safe-buffer":28}],30:[function(require,module,exports){ +(function (global){ + +/** + * Module exports. + */ + +module.exports = deprecate; + +/** + * Mark that a method should not be used. + * Returns a modified function which warns once by default. + * + * If `localStorage.noDeprecation = true` is set, then it is a no-op. + * + * If `localStorage.throwDeprecation = true` is set, then deprecated functions + * will throw an Error when invoked. + * + * If `localStorage.traceDeprecation = true` is set, then deprecated functions + * will invoke `console.trace()` instead of `console.error()`. + * + * @param {Function} fn - the function to deprecate + * @param {String} msg - the string to print to the console when `fn` is invoked + * @returns {Function} a new "deprecated" version of `fn` + * @api public + */ + +function deprecate (fn, msg) { + if (config('noDeprecation')) { + return fn; + } + + var warned = false; + function deprecated() { + if (!warned) { + if (config('throwDeprecation')) { + throw new Error(msg); + } else if (config('traceDeprecation')) { + console.trace(msg); + } else { + console.warn(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; +} + +/** + * Checks `localStorage` for boolean values for the given `name`. + * + * @param {String} name + * @returns {Boolean} + * @api private + */ + +function config (name) { + // accessing global.localStorage can trigger a DOMException in sandboxed iframes + try { + if (!global.localStorage) return false; + } catch (_) { + return false; + } + var val = global.localStorage[name]; + if (null == val) return false; + return String(val).toLowerCase() === 'true'; +} + +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{}],31:[function(require,module,exports){ +arguments[4][13][0].apply(exports,arguments) +},{"dup":13}],32:[function(require,module,exports){ +module.exports = function isBuffer(arg) { + return arg && typeof arg === 'object' + && typeof arg.copy === 'function' + && typeof arg.fill === 'function' + && typeof arg.readUInt8 === 'function'; +} +},{}],33:[function(require,module,exports){ +(function (process,global){ +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +var formatRegExp = /%[sdj%]/g; +exports.format = function(f) { + if (!isString(f)) { + var objects = []; + for (var i = 0; i < arguments.length; i++) { + objects.push(inspect(arguments[i])); + } + return objects.join(' '); + } + + var i = 1; + var args = arguments; + var len = args.length; + var str = String(f).replace(formatRegExp, function(x) { + if (x === '%%') return '%'; + if (i >= len) return x; + switch (x) { + case '%s': return String(args[i++]); + case '%d': return Number(args[i++]); + case '%j': + try { + return JSON.stringify(args[i++]); + } catch (_) { + return '[Circular]'; + } + default: + return x; + } + }); + for (var x = args[i]; i < len; x = args[++i]) { + if (isNull(x) || !isObject(x)) { + str += ' ' + x; + } else { + str += ' ' + inspect(x); + } + } + return str; +}; + + +// Mark that a method should not be used. +// Returns a modified function which warns once by default. +// If --no-deprecation is set, then it is a no-op. +exports.deprecate = function(fn, msg) { + // Allow for deprecating things in the process of starting up. + if (isUndefined(global.process)) { + return function() { + return exports.deprecate(fn, msg).apply(this, arguments); + }; + } + + if (process.noDeprecation === true) { + return fn; + } + + var warned = false; + function deprecated() { + if (!warned) { + if (process.throwDeprecation) { + throw new Error(msg); + } else if (process.traceDeprecation) { + console.trace(msg); + } else { + console.error(msg); + } + warned = true; + } + return fn.apply(this, arguments); + } + + return deprecated; +}; + + +var debugs = {}; +var debugEnviron; +exports.debuglog = function(set) { + if (isUndefined(debugEnviron)) + debugEnviron = process.env.NODE_DEBUG || ''; + set = set.toUpperCase(); + if (!debugs[set]) { + if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) { + var pid = process.pid; + debugs[set] = function() { + var msg = exports.format.apply(exports, arguments); + console.error('%s %d: %s', set, pid, msg); + }; + } else { + debugs[set] = function() {}; + } + } + return debugs[set]; +}; + + +/** + * Echos the value of a value. Trys to print the value out + * in the best way possible given the different types. + * + * @param {Object} obj The object to print out. + * @param {Object} opts Optional options object that alters the output. + */ +/* legacy: obj, showHidden, depth, colors*/ +function inspect(obj, opts) { + // default options + var ctx = { + seen: [], + stylize: stylizeNoColor + }; + // legacy... + if (arguments.length >= 3) ctx.depth = arguments[2]; + if (arguments.length >= 4) ctx.colors = arguments[3]; + if (isBoolean(opts)) { + // legacy... + ctx.showHidden = opts; + } else if (opts) { + // got an "options" object + exports._extend(ctx, opts); + } + // set default options + if (isUndefined(ctx.showHidden)) ctx.showHidden = false; + if (isUndefined(ctx.depth)) ctx.depth = 2; + if (isUndefined(ctx.colors)) ctx.colors = false; + if (isUndefined(ctx.customInspect)) ctx.customInspect = true; + if (ctx.colors) ctx.stylize = stylizeWithColor; + return formatValue(ctx, obj, ctx.depth); +} +exports.inspect = inspect; + + +// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics +inspect.colors = { + 'bold' : [1, 22], + 'italic' : [3, 23], + 'underline' : [4, 24], + 'inverse' : [7, 27], + 'white' : [37, 39], + 'grey' : [90, 39], + 'black' : [30, 39], + 'blue' : [34, 39], + 'cyan' : [36, 39], + 'green' : [32, 39], + 'magenta' : [35, 39], + 'red' : [31, 39], + 'yellow' : [33, 39] +}; + +// Don't use 'blue' not visible on cmd.exe +inspect.styles = { + 'special': 'cyan', + 'number': 'yellow', + 'boolean': 'yellow', + 'undefined': 'grey', + 'null': 'bold', + 'string': 'green', + 'date': 'magenta', + // "name": intentionally not styling + 'regexp': 'red' +}; + + +function stylizeWithColor(str, styleType) { + var style = inspect.styles[styleType]; + + if (style) { + return '\u001b[' + inspect.colors[style][0] + 'm' + str + + '\u001b[' + inspect.colors[style][1] + 'm'; + } else { + return str; + } +} + + +function stylizeNoColor(str, styleType) { + return str; +} + + +function arrayToHash(array) { + var hash = {}; + + array.forEach(function(val, idx) { + hash[val] = true; + }); + + return hash; +} + + +function formatValue(ctx, value, recurseTimes) { + // Provide a hook for user-specified inspect functions. + // Check that value is an object with an inspect function on it + if (ctx.customInspect && + value && + isFunction(value.inspect) && + // Filter out the util module, it's inspect function is special + value.inspect !== exports.inspect && + // Also filter out any prototype objects using the circular check. + !(value.constructor && value.constructor.prototype === value)) { + var ret = value.inspect(recurseTimes, ctx); + if (!isString(ret)) { + ret = formatValue(ctx, ret, recurseTimes); + } + return ret; + } + + // Primitive types cannot have properties + var primitive = formatPrimitive(ctx, value); + if (primitive) { + return primitive; + } + + // Look up the keys of the object. + var keys = Object.keys(value); + var visibleKeys = arrayToHash(keys); + + if (ctx.showHidden) { + keys = Object.getOwnPropertyNames(value); + } + + // IE doesn't make error fields non-enumerable + // http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx + if (isError(value) + && (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) { + return formatError(value); + } + + // Some type of object without properties can be shortcutted. + if (keys.length === 0) { + if (isFunction(value)) { + var name = value.name ? ': ' + value.name : ''; + return ctx.stylize('[Function' + name + ']', 'special'); + } + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } + if (isDate(value)) { + return ctx.stylize(Date.prototype.toString.call(value), 'date'); + } + if (isError(value)) { + return formatError(value); + } + } + + var base = '', array = false, braces = ['{', '}']; + + // Make Array say that they are Array + if (isArray(value)) { + array = true; + braces = ['[', ']']; + } + + // Make functions say that they are functions + if (isFunction(value)) { + var n = value.name ? ': ' + value.name : ''; + base = ' [Function' + n + ']'; + } + + // Make RegExps say that they are RegExps + if (isRegExp(value)) { + base = ' ' + RegExp.prototype.toString.call(value); + } + + // Make dates with properties first say the date + if (isDate(value)) { + base = ' ' + Date.prototype.toUTCString.call(value); + } + + // Make error with message first say the error + if (isError(value)) { + base = ' ' + formatError(value); + } + + if (keys.length === 0 && (!array || value.length == 0)) { + return braces[0] + base + braces[1]; + } + + if (recurseTimes < 0) { + if (isRegExp(value)) { + return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp'); + } else { + return ctx.stylize('[Object]', 'special'); + } + } + + ctx.seen.push(value); + + var output; + if (array) { + output = formatArray(ctx, value, recurseTimes, visibleKeys, keys); + } else { + output = keys.map(function(key) { + return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array); + }); + } + + ctx.seen.pop(); + + return reduceToSingleString(output, base, braces); +} + + +function formatPrimitive(ctx, value) { + if (isUndefined(value)) + return ctx.stylize('undefined', 'undefined'); + if (isString(value)) { + var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '') + .replace(/'/g, "\\'") + .replace(/\\"/g, '"') + '\''; + return ctx.stylize(simple, 'string'); + } + if (isNumber(value)) + return ctx.stylize('' + value, 'number'); + if (isBoolean(value)) + return ctx.stylize('' + value, 'boolean'); + // For some reason typeof null is "object", so special case here. + if (isNull(value)) + return ctx.stylize('null', 'null'); +} + + +function formatError(value) { + return '[' + Error.prototype.toString.call(value) + ']'; +} + + +function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { + var output = []; + for (var i = 0, l = value.length; i < l; ++i) { + if (hasOwnProperty(value, String(i))) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + String(i), true)); + } else { + output.push(''); + } + } + keys.forEach(function(key) { + if (!key.match(/^\d+$/)) { + output.push(formatProperty(ctx, value, recurseTimes, visibleKeys, + key, true)); + } + }); + return output; +} + + +function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { + var name, str, desc; + desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; + if (desc.get) { + if (desc.set) { + str = ctx.stylize('[Getter/Setter]', 'special'); + } else { + str = ctx.stylize('[Getter]', 'special'); + } + } else { + if (desc.set) { + str = ctx.stylize('[Setter]', 'special'); + } + } + if (!hasOwnProperty(visibleKeys, key)) { + name = '[' + key + ']'; + } + if (!str) { + if (ctx.seen.indexOf(desc.value) < 0) { + if (isNull(recurseTimes)) { + str = formatValue(ctx, desc.value, null); + } else { + str = formatValue(ctx, desc.value, recurseTimes - 1); + } + if (str.indexOf('\n') > -1) { + if (array) { + str = str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n').substr(2); + } else { + str = '\n' + str.split('\n').map(function(line) { + return ' ' + line; + }).join('\n'); + } + } + } else { + str = ctx.stylize('[Circular]', 'special'); + } + } + if (isUndefined(name)) { + if (array && key.match(/^\d+$/)) { + return str; + } + name = JSON.stringify('' + key); + if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) { + name = name.substr(1, name.length - 2); + name = ctx.stylize(name, 'name'); + } else { + name = name.replace(/'/g, "\\'") + .replace(/\\"/g, '"') + .replace(/(^"|"$)/g, "'"); + name = ctx.stylize(name, 'string'); + } + } + + return name + ': ' + str; +} + + +function reduceToSingleString(output, base, braces) { + var numLinesEst = 0; + var length = output.reduce(function(prev, cur) { + numLinesEst++; + if (cur.indexOf('\n') >= 0) numLinesEst++; + return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1; + }, 0); + + if (length > 60) { + return braces[0] + + (base === '' ? '' : base + '\n ') + + ' ' + + output.join(',\n ') + + ' ' + + braces[1]; + } + + return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1]; +} + + +// NOTE: These type checking functions intentionally don't use `instanceof` +// because it is fragile and can be easily faked with `Object.create()`. +function isArray(ar) { + return Array.isArray(ar); +} +exports.isArray = isArray; + +function isBoolean(arg) { + return typeof arg === 'boolean'; +} +exports.isBoolean = isBoolean; + +function isNull(arg) { + return arg === null; +} +exports.isNull = isNull; + +function isNullOrUndefined(arg) { + return arg == null; +} +exports.isNullOrUndefined = isNullOrUndefined; + +function isNumber(arg) { + return typeof arg === 'number'; +} +exports.isNumber = isNumber; + +function isString(arg) { + return typeof arg === 'string'; +} +exports.isString = isString; + +function isSymbol(arg) { + return typeof arg === 'symbol'; +} +exports.isSymbol = isSymbol; + +function isUndefined(arg) { + return arg === void 0; +} +exports.isUndefined = isUndefined; + +function isRegExp(re) { + return isObject(re) && objectToString(re) === '[object RegExp]'; +} +exports.isRegExp = isRegExp; + +function isObject(arg) { + return typeof arg === 'object' && arg !== null; +} +exports.isObject = isObject; + +function isDate(d) { + return isObject(d) && objectToString(d) === '[object Date]'; +} +exports.isDate = isDate; + +function isError(e) { + return isObject(e) && + (objectToString(e) === '[object Error]' || e instanceof Error); +} +exports.isError = isError; + +function isFunction(arg) { + return typeof arg === 'function'; +} +exports.isFunction = isFunction; + +function isPrimitive(arg) { + return arg === null || + typeof arg === 'boolean' || + typeof arg === 'number' || + typeof arg === 'string' || + typeof arg === 'symbol' || // ES6 symbol + typeof arg === 'undefined'; +} +exports.isPrimitive = isPrimitive; + +exports.isBuffer = require('./support/isBuffer'); + +function objectToString(o) { + return Object.prototype.toString.call(o); +} + + +function pad(n) { + return n < 10 ? '0' + n.toString(10) : n.toString(10); +} + + +var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec']; + +// 26 Feb 16:19:34 +function timestamp() { + var d = new Date(); + var time = [pad(d.getHours()), + pad(d.getMinutes()), + pad(d.getSeconds())].join(':'); + return [d.getDate(), months[d.getMonth()], time].join(' '); +} + + +// log is just a thin wrapper to console.log that prepends a timestamp +exports.log = function() { + console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments)); +}; + + +/** + * Inherit the prototype methods from one constructor into another. + * + * The Function.prototype.inherits from lang.js rewritten as a standalone + * function (not on Function.prototype). NOTE: If this file is to be loaded + * during bootstrapping this function needs to be rewritten using some native + * functions as prototype setup using normal JavaScript does not work as + * expected during bootstrapping (see mirror.js in r114903). + * + * @param {function} ctor Constructor function which needs to inherit the + * prototype. + * @param {function} superCtor Constructor function to inherit prototype from. + */ +exports.inherits = require('inherits'); + +exports._extend = function(origin, add) { + // Don't do anything if add isn't an object + if (!add || !isObject(add)) return origin; + + var keys = Object.keys(add); + var i = keys.length; + while (i--) { + origin[keys[i]] = add[keys[i]]; + } + return origin; +}; + +function hasOwnProperty(obj, prop) { + return Object.prototype.hasOwnProperty.call(obj, prop); +} + +}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) +},{"./support/isBuffer":32,"_process":17,"inherits":31}]},{},[1])(1) +}); \ No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/msgpack5.min.js b/samples/ChatSample/wwwroot/scripts/msgpack5.min.js new file mode 100644 index 000000000..a69152918 --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/msgpack5.min.js @@ -0,0 +1 @@ +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.msgpack5=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o=0,"must have a non-negative type");assert(decode,"must have a decode function");decodingTypes.push({type:type,decode:decode});return this}function register(type,constructor,encode,decode){assert(constructor,"must have a constructor");assert(encode,"must have an encode function");assert(type>=0,"must have a non-negative type");assert(decode,"must have a decode function");function check(obj){return obj instanceof constructor}function reEncode(obj){var buf=bl();var header=Buffer.allocUnsafe(1);header.writeInt8(type,0);buf.append(header);buf.append(encode(obj));return buf}this.registerEncoder(check,reEncode);this.registerDecoder(type,decode);return this}return{encode:buildEncode(encodingTypes,options.forceFloat64,options.compatibilityMode,options.disableTimestampEncoding),decode:buildDecode(decodingTypes),register:register,registerEncoder:registerEncoder,registerDecoder:registerDecoder,encoder:streams.encoder,decoder:streams.decoder,buffer:true,type:"msgpack5",IncompleteBufferError:buildDecode.IncompleteBufferError}}module.exports=msgpack},{"./lib/decoder":2,"./lib/encoder":3,"./lib/streams":4,assert:5,bl:7,"safe-buffer":28}],2:[function(require,module,exports){var bl=require("bl");var util=require("util");function IncompleteBufferError(message){Error.call(this);if(Error.captureStackTrace){Error.captureStackTrace(this,this.constructor)}this.name=this.constructor.name;this.message=message||"unable to decode"}util.inherits(IncompleteBufferError,Error);module.exports=function buildDecode(decodingTypes){return decode;function getSize(first){switch(first){case 196:return 2;case 197:return 3;case 198:return 5;case 199:return 3;case 200:return 4;case 201:return 6;case 202:return 5;case 203:return 9;case 204:return 2;case 205:return 3;case 206:return 5;case 207:return 9;case 208:return 2;case 209:return 3;case 210:return 5;case 211:return 9;case 212:return 3;case 213:return 4;case 214:return 6;case 215:return 10;case 216:return 18;case 217:return 2;case 218:return 3;case 219:return 5;case 222:return 3;default:return-1}}function hasMinBufferSize(first,length){var size=getSize(first);if(size!==-1&&length=headerLength+dataLength}function buildDecodeResult(value,bytesConsumed){return{value:value,bytesConsumed:bytesConsumed}}function decode(buf){if(!(buf instanceof bl)){buf=bl().append(buf)}var result=tryDecode(buf);if(result){buf.consume(result.bytesConsumed);return result.value}else{throw new IncompleteBufferError}}function tryDecode(buf,offset){offset=offset===undefined?0:offset;var bufLength=buf.length-offset;if(bufLength<=0){return null}var first=buf.readUInt8(offset);var length;var result=0;var type;var bytePos;if(!hasMinBufferSize(first,bufLength)){return null}switch(first){case 192:return buildDecodeResult(null,1);case 194:return buildDecodeResult(false,1);case 195:return buildDecodeResult(true,1);case 204:result=buf.readUInt8(offset+1);return buildDecodeResult(result,2);case 205:result=buf.readUInt16BE(offset+1);return buildDecodeResult(result,3);case 206:result=buf.readUInt32BE(offset+1);return buildDecodeResult(result,5);case 207:for(bytePos=7;bytePos>=0;bytePos--){result+=buf.readUInt8(offset+bytePos+1)*Math.pow(2,8*(7-bytePos))}return buildDecodeResult(result,9);case 208:result=buf.readInt8(offset+1);return buildDecodeResult(result,2);case 209:result=buf.readInt16BE(offset+1);return buildDecodeResult(result,3);case 210:result=buf.readInt32BE(offset+1);return buildDecodeResult(result,5);case 211:result=readInt64BE(buf.slice(offset+1,offset+9),0);return buildDecodeResult(result,9);case 202:result=buf.readFloatBE(offset+1);return buildDecodeResult(result,5);case 203:result=buf.readDoubleBE(offset+1);return buildDecodeResult(result,9);case 217:length=buf.readUInt8(offset+1);if(!isValidDataSize(length,bufLength,2)){return null}result=buf.toString("utf8",offset+2,offset+2+length);return buildDecodeResult(result,2+length);case 218:length=buf.readUInt16BE(offset+1);if(!isValidDataSize(length,bufLength,3)){return null}result=buf.toString("utf8",offset+3,offset+3+length);return buildDecodeResult(result,3+length);case 219:length=buf.readUInt32BE(offset+1);if(!isValidDataSize(length,bufLength,5)){return null}result=buf.toString("utf8",offset+5,offset+5+length);return buildDecodeResult(result,5+length);case 196:length=buf.readUInt8(offset+1);if(!isValidDataSize(length,bufLength,2)){return null}result=buf.slice(offset+2,offset+2+length);return buildDecodeResult(result,2+length);case 197:length=buf.readUInt16BE(offset+1);if(!isValidDataSize(length,bufLength,3)){return null}result=buf.slice(offset+3,offset+3+length);return buildDecodeResult(result,3+length);case 198:length=buf.readUInt32BE(offset+1);if(!isValidDataSize(length,bufLength,5)){return null}result=buf.slice(offset+5,offset+5+length);return buildDecodeResult(result,5+length);case 220:if(bufLength<3){return null}length=buf.readUInt16BE(offset+1);return decodeArray(buf,offset,length,3);case 221:if(bufLength<5){return null}length=buf.readUInt32BE(offset+1);return decodeArray(buf,offset,length,5);case 222:length=buf.readUInt16BE(offset+1);return decodeMap(buf,offset,length,3);case 223:throw new Error("map too big to decode in JS");case 212:return decodeFixExt(buf,offset,1);case 213:return decodeFixExt(buf,offset,2);case 214:return decodeFixExt(buf,offset,4);case 215:return decodeFixExt(buf,offset,8);case 216:return decodeFixExt(buf,offset,16);case 199:length=buf.readUInt8(offset+1);type=buf.readUInt8(offset+2);if(!isValidDataSize(length,bufLength,3)){return null}return decodeExt(buf,offset,type,length,3);case 200:length=buf.readUInt16BE(offset+1);type=buf.readUInt8(offset+3);if(!isValidDataSize(length,bufLength,4)){return null}return decodeExt(buf,offset,type,length,4);case 201:length=buf.readUInt32BE(offset+1);type=buf.readUInt8(offset+5);if(!isValidDataSize(length,bufLength,6)){return null}return decodeExt(buf,offset,type,length,6)}if((first&240)===144){length=first&15;return decodeArray(buf,offset,length,1)}else if((first&240)===128){length=first&15;return decodeMap(buf,offset,length,1)}else if((first&224)===160){length=first&31;if(isValidDataSize(length,bufLength,1)){result=buf.toString("utf8",offset+1,offset+length+1);return buildDecodeResult(result,length+1)}else{return null}}else if(first>=224){result=first-256;return buildDecodeResult(result,1)}else if(first<128){return buildDecodeResult(first,1)}else{throw new Error("not implemented yet")}}function readInt64BE(buf,offset){var negate=(buf[offset]&128)==128;if(negate){var carry=1;for(var i=offset+7;i>=offset;i--){var v=(buf[i]^255)+carry;buf[i]=v&255;carry=v>>8}}var hi=buf.readUInt32BE(offset+0);var lo=buf.readUInt32BE(offset+4);return(hi*4294967296+lo)*(negate?-1:+1)}function decodeArray(buf,offset,length,headerLength){var result=[];var i;var totalBytesConsumed=0;offset+=headerLength;for(i=0;i0){buf.write(obj,1)}}else if(len<=255&&!compatibilityMode){buf=Buffer.allocUnsafe(2+len);buf[0]=217;buf[1]=len;buf.write(obj,2)}else if(len<=65535){buf=Buffer.allocUnsafe(3+len);buf[0]=218;buf.writeUInt16BE(len,1);buf.write(obj,3)}else{buf=Buffer.allocUnsafe(5+len);buf[0]=219;buf.writeUInt32BE(len,1);buf.write(obj,5)}}else if(obj&&(obj.readUInt32LE||obj instanceof Uint8Array)){if(obj instanceof Uint8Array){obj=Buffer.from(obj)}if(obj.length<=255){buf=Buffer.allocUnsafe(2);buf[0]=196;buf[1]=obj.length}else if(obj.length<=65535){buf=Buffer.allocUnsafe(3);buf[0]=197;buf.writeUInt16BE(obj.length,1)}else{buf=Buffer.allocUnsafe(5);buf[0]=198;buf.writeUInt32BE(obj.length,1)}buf=bl([buf,obj])}else if(Array.isArray(obj)){if(obj.length<16){buf=Buffer.allocUnsafe(1);buf[0]=144|obj.length}else if(obj.length<65536){buf=Buffer.allocUnsafe(3);buf[0]=220;buf.writeUInt16BE(obj.length,1)}else{buf=Buffer.allocUnsafe(5);buf[0]=221;buf.writeUInt32BE(obj.length,1)}buf=obj.reduce(function(acc,obj){acc.append(encode(obj,true));return acc},bl().append(buf))}else if(!disableTimestampEncoding&&typeof obj.getDate==="function"){return encodeDate(obj)}else if(typeof obj==="object"){buf=encodeExt(obj)||encodeObject(obj)}else if(typeof obj==="number"){if(isFloat(obj)){return encodeFloat(obj,forceFloat64)}else if(obj>=0){if(obj<128){buf=Buffer.allocUnsafe(1);buf[0]=obj}else if(obj<256){buf=Buffer.allocUnsafe(2);buf[0]=204;buf[1]=obj}else if(obj<65536){buf=Buffer.allocUnsafe(3);buf[0]=205;buf.writeUInt16BE(obj,1)}else if(obj<=4294967295){buf=Buffer.allocUnsafe(5);buf[0]=206;buf.writeUInt32BE(obj,1)}else if(obj<=9007199254740991){buf=Buffer.allocUnsafe(9);buf[0]=207;write64BitUint(buf,obj)}else{return encodeFloat(obj,true)}}else{if(obj>=-32){buf=Buffer.allocUnsafe(1);buf[0]=256+obj}else if(obj>=-128){buf=Buffer.allocUnsafe(2);buf[0]=208;buf.writeInt8(obj,1)}else if(obj>=-32768){buf=Buffer.allocUnsafe(3);buf[0]=209;buf.writeInt16BE(obj,1)}else if(obj>-214748365){buf=Buffer.allocUnsafe(5);buf[0]=210;buf.writeInt32BE(obj,1)}else if(obj>=-9007199254740991){buf=Buffer.allocUnsafe(9);buf[0]=211;write64BitInt(buf,1,obj)}else{return encodeFloat(obj,true)}}}if(!buf){throw new Error("not implemented yet")}if(avoidSlice){return buf}else{return buf.slice()}}function encodeDate(dt){var encoded;var millis=dt*1;var seconds=Math.floor(millis/1e3);var nanos=(millis-seconds*1e3)*1e6;if(nanos||seconds>4294967295){encoded=new Buffer(10);encoded[0]=215;encoded[1]=-1;var upperNanos=nanos*4;var upperSeconds=seconds/Math.pow(2,32);var upper=upperNanos+upperSeconds&4294967295;var lower=seconds&4294967295;encoded.writeInt32BE(upper,2);encoded.writeInt32BE(lower,6)}else{encoded=new Buffer(6);encoded[0]=214;encoded[1]=-1;encoded.writeUInt32BE(Math.floor(millis/1e3),2)}return bl().append(encoded)}function encodeExt(obj){var i;var encoded;var length=-1;var headers=[];for(i=0;i>8);headers.push(length&255)}else{headers.push(201);headers.push(length>>24);headers.push(length>>16&255);headers.push(length>>8&255);headers.push(length&255)}return bl().append(Buffer.from(headers)).append(encoded)}function encodeObject(obj){var acc=[];var length=0;var key;var header;for(key in obj){if(obj.hasOwnProperty(key)&&obj[key]!==undefined&&typeof obj[key]!=="function"){++length;acc.push(encode(key,true));acc.push(encode(obj[key],true))}}if(length<16){header=Buffer.allocUnsafe(1);header[0]=128|length}else{header=Buffer.allocUnsafe(3);header[0]=222;header.writeUInt16BE(length,1)}acc.unshift(header);var result=acc.reduce(function(list,buf){return list.append(buf)},bl());return result}return encode};function write64BitUint(buf,obj){for(var currByte=7;currByte>=0;currByte--){buf[currByte+1]=obj&255;obj=obj/256}}function write64BitInt(buf,offset,num){var negate=num<0;if(negate){num=Math.abs(num)}var lo=num%4294967296;var hi=num/4294967296;buf.writeUInt32BE(Math.floor(hi),offset+0);buf.writeUInt32BE(lo,offset+4);if(negate){var carry=1;for(var i=offset+7;i>=offset;i--){var v=(buf[i]^255)+carry;buf[i]=v&255;carry=v>>8}}}function isFloat(n){return n!==Math.floor(n)}function encodeFloat(obj,forceFloat64){var buf;buf=Buffer.allocUnsafe(5);buf[0]=202;buf.writeFloatBE(obj,1);if(forceFloat64||Math.abs(obj-buf.readFloatBE(1))>TOLERANCE){buf=Buffer.allocUnsafe(9);buf[0]=203;buf.writeDoubleBE(obj,1)}return buf}},{bl:7,"safe-buffer":28}],4:[function(require,module,exports){"use strict";var Transform=require("readable-stream").Transform;var inherits=require("inherits");var bl=require("bl");function Base(opts){opts=opts||{};opts.objectMode=true;opts.highWaterMark=16;Transform.call(this,opts);this._msgpack=opts.msgpack}inherits(Base,Transform);function Encoder(opts){if(!(this instanceof Encoder)){opts=opts||{};opts.msgpack=this;return new Encoder(opts)}Base.call(this,opts)}inherits(Encoder,Base);Encoder.prototype._transform=function(obj,enc,done){var buf=null;try{buf=this._msgpack.encode(obj).slice(0)}catch(err){this.emit("error",err);return done()}this.push(buf);done()};function Decoder(opts){if(!(this instanceof Decoder)){opts=opts||{};opts.msgpack=this;return new Decoder(opts)}Base.call(this,opts);this._chunks=bl()}inherits(Decoder,Base);Decoder.prototype._transform=function(buf,enc,done){if(buf){this._chunks.append(buf)}try{var result=this._msgpack.decode(this._chunks);this.push(result)}catch(err){if(err instanceof this._msgpack.IncompleteBufferError){done()}else{this.emit("error",err)}return}if(this._chunks.length>0){this._transform(null,enc,done)}else{done()}};module.exports.decoder=Decoder;module.exports.encoder=Encoder},{bl:7,inherits:13,"readable-stream":27}],5:[function(require,module,exports){(function(global){"use strict";function compare(a,b){if(a===b){return 0}var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i=0){var next_line=out.indexOf("\n",idx+1);out=out.substring(next_line+1)}this.stack=out}}};util.inherits(assert.AssertionError,Error);function truncate(s,n){if(typeof s==="string"){return s.length=0;i--){if(ka[i]!==kb[i])return false}for(i=ka.length-1;i>=0;i--){key=ka[i];if(!_deepEqual(a[key],b[key],strict,actualVisitedObjects))return false}return true}assert.notDeepEqual=function notDeepEqual(actual,expected,message){if(_deepEqual(actual,expected,false)){fail(actual,expected,message,"notDeepEqual",assert.notDeepEqual)}};assert.notDeepStrictEqual=notDeepStrictEqual;function notDeepStrictEqual(actual,expected,message){if(_deepEqual(actual,expected,true)){fail(actual,expected,message,"notDeepStrictEqual",notDeepStrictEqual)}}assert.strictEqual=function strictEqual(actual,expected,message){if(actual!==expected){fail(actual,expected,message,"===",assert.strictEqual)}};assert.notStrictEqual=function notStrictEqual(actual,expected,message){if(actual===expected){fail(actual,expected,message,"!==",assert.notStrictEqual)}};function expectedException(actual,expected){if(!actual||!expected){return false}if(Object.prototype.toString.call(expected)=="[object RegExp]"){return expected.test(actual)}try{if(actual instanceof expected){return true}}catch(e){}if(Error.isPrototypeOf(expected)){return false}return expected.call({},actual)===true}function _tryBlock(block){var error;try{block()}catch(e){error=e}return error}function _throws(shouldThrow,block,expected,message){var actual;if(typeof block!=="function"){throw new TypeError('"block" argument must be a function')}if(typeof expected==="string"){message=expected;expected=null}actual=_tryBlock(block);message=(expected&&expected.name?" ("+expected.name+").":".")+(message?" "+message:".");if(shouldThrow&&!actual){fail(actual,expected,"Missing expected exception"+message)}var userProvidedMessage=typeof message==="string";var isUnwantedException=!shouldThrow&&util.isError(actual);var isUnexpectedException=!shouldThrow&&actual&&!expected;if(isUnwantedException&&userProvidedMessage&&expectedException(actual,expected)||isUnexpectedException){fail(actual,expected,"Got unwanted exception"+message)}if(shouldThrow&&actual&&expected&&!expectedException(actual,expected)||!shouldThrow&&actual){throw actual}}assert.throws=function(block,error,message){_throws(true,block,error,message)};assert.doesNotThrow=function(block,error,message){_throws(false,block,error,message)};assert.ifError=function(err){if(err)throw err};var objectKeys=Object.keys||function(obj){var keys=[];for(var key in obj){if(hasOwn.call(obj,key))keys.push(key)}return keys}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"util/":33}],6:[function(require,module,exports){"use strict";exports.byteLength=byteLength;exports.toByteArray=toByteArray;exports.fromByteArray=fromByteArray;var lookup=[];var revLookup=[];var Arr=typeof Uint8Array!=="undefined"?Uint8Array:Array;var code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var i=0,len=code.length;i0){throw new Error("Invalid string. Length must be a multiple of 4")}return b64[len-2]==="="?2:b64[len-1]==="="?1:0}function byteLength(b64){return b64.length*3/4-placeHoldersCount(b64)}function toByteArray(b64){var i,l,tmp,placeHolders,arr;var len=b64.length;placeHolders=placeHoldersCount(b64);arr=new Arr(len*3/4-placeHolders);l=placeHolders>0?len-4:len;var L=0;for(i=0;i>16&255;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}if(placeHolders===2){tmp=revLookup[b64.charCodeAt(i)]<<2|revLookup[b64.charCodeAt(i+1)]>>4;arr[L++]=tmp&255}else if(placeHolders===1){tmp=revLookup[b64.charCodeAt(i)]<<10|revLookup[b64.charCodeAt(i+1)]<<4|revLookup[b64.charCodeAt(i+2)]>>2;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}return arr}function tripletToBase64(num){return lookup[num>>18&63]+lookup[num>>12&63]+lookup[num>>6&63]+lookup[num&63]}function encodeChunk(uint8,start,end){var tmp;var output=[];for(var i=start;ilen2?len2:i+maxChunkLength))}if(extraBytes===1){tmp=uint8[len-1];output+=lookup[tmp>>2];output+=lookup[tmp<<4&63];output+="=="}else if(extraBytes===2){tmp=(uint8[len-2]<<8)+uint8[len-1];output+=lookup[tmp>>10];output+=lookup[tmp>>4&63];output+=lookup[tmp<<2&63];output+="="}parts.push(output);return parts.join("")}},{}],7:[function(require,module,exports){(function(Buffer){var DuplexStream=require("readable-stream/duplex"),util=require("util");function BufferList(callback){if(!(this instanceof BufferList))return new BufferList(callback);this._bufs=[];this.length=0;if(typeof callback=="function"){this._callback=callback;var piper=function piper(err){if(this._callback){this._callback(err);this._callback=null}}.bind(this);this.on("pipe",function onPipe(src){src.on("error",piper)});this.on("unpipe",function onUnpipe(src){src.removeListener("error",piper)})}else{this.append(callback)}DuplexStream.call(this)}util.inherits(BufferList,DuplexStream);BufferList.prototype._offset=function _offset(offset){var tot=0,i=0,_t;if(offset===0)return[0,0];for(;ithis.length)srcEnd=this.length;if(srcStart>=this.length)return dst||new Buffer(0);if(srcEnd<=0)return dst||new Buffer(0);var copy=!!dst,off=this._offset(srcStart),len=srcEnd-srcStart,bytes=len,bufoff=copy&&dstStart||0,start=off[1],l,i;if(srcStart===0&&srcEnd==this.length){if(!copy){return this._bufs.length===1?this._bufs[0]:Buffer.concat(this._bufs,this.length)}for(i=0;il){this._bufs[i].copy(dst,bufoff,start)}else{this._bufs[i].copy(dst,bufoff,start,start+bytes);break}bufoff+=l;bytes-=l;if(start)start=0}return dst};BufferList.prototype.shallowSlice=function shallowSlice(start,end){start=start||0;end=end||this.length;if(start<0)start+=this.length;if(end<0)end+=this.length;var startOffset=this._offset(start),endOffset=this._offset(end),buffers=this._bufs.slice(startOffset[0],endOffset[0]+1);if(endOffset[1]==0)buffers.pop();else buffers[buffers.length-1]=buffers[buffers.length-1].slice(0,endOffset[1]);if(startOffset[1]!=0)buffers[0]=buffers[0].slice(startOffset[1]);return new BufferList(buffers)};BufferList.prototype.toString=function toString(encoding,start,end){return this.slice(start,end).toString(encoding)};BufferList.prototype.consume=function consume(bytes){while(this._bufs.length){if(bytes>=this._bufs[0].length){bytes-=this._bufs[0].length;this.length-=this._bufs[0].length;this._bufs.shift()}else{this._bufs[0]=this._bufs[0].slice(bytes);this.length-=bytes;break}}return this};BufferList.prototype.duplicate=function duplicate(){var i=0,copy=new BufferList;for(;iK_MAX_LENGTH){throw new RangeError("Invalid typed array length")}var buf=new Uint8Array(length);buf.__proto__=Buffer.prototype;return buf}function Buffer(arg,encodingOrOffset,length){if(typeof arg==="number"){if(typeof encodingOrOffset==="string"){throw new Error("If encoding is specified then the first argument must be a string")}return allocUnsafe(arg)}return from(arg,encodingOrOffset,length)}if(typeof Symbol!=="undefined"&&Symbol.species&&Buffer[Symbol.species]===Buffer){Object.defineProperty(Buffer,Symbol.species,{value:null,configurable:true,enumerable:false,writable:false})}Buffer.poolSize=8192;function from(value,encodingOrOffset,length){if(typeof value==="number"){throw new TypeError('"value" argument must not be a number')}if(isArrayBuffer(value)){return fromArrayBuffer(value,encodingOrOffset,length)}if(typeof value==="string"){return fromString(value,encodingOrOffset)}return fromObject(value)}Buffer.from=function(value,encodingOrOffset,length){return from(value,encodingOrOffset,length)};Buffer.prototype.__proto__=Uint8Array.prototype;Buffer.__proto__=Uint8Array;function assertSize(size){if(typeof size!=="number"){throw new TypeError('"size" argument must be a number')}else if(size<0){throw new RangeError('"size" argument must not be negative')}}function alloc(size,fill,encoding){assertSize(size);if(size<=0){return createBuffer(size)}if(fill!==undefined){return typeof encoding==="string"?createBuffer(size).fill(fill,encoding):createBuffer(size).fill(fill)}return createBuffer(size)}Buffer.alloc=function(size,fill,encoding){return alloc(size,fill,encoding)};function allocUnsafe(size){assertSize(size);return createBuffer(size<0?0:checked(size)|0)}Buffer.allocUnsafe=function(size){return allocUnsafe(size)};Buffer.allocUnsafeSlow=function(size){return allocUnsafe(size)};function fromString(string,encoding){if(typeof encoding!=="string"||encoding===""){encoding="utf8"}if(!Buffer.isEncoding(encoding)){throw new TypeError('"encoding" must be a valid string encoding')}var length=byteLength(string,encoding)|0;var buf=createBuffer(length);var actual=buf.write(string,encoding);if(actual!==length){buf=buf.slice(0,actual)}return buf}function fromArrayLike(array){var length=array.length<0?0:checked(array.length)|0;var buf=createBuffer(length);for(var i=0;i=K_MAX_LENGTH){throw new RangeError("Attempt to allocate Buffer larger than maximum "+"size: 0x"+K_MAX_LENGTH.toString(16)+" bytes")}return length|0}function SlowBuffer(length){if(+length!=length){length=0}return Buffer.alloc(+length)}Buffer.isBuffer=function isBuffer(b){return b!=null&&b._isBuffer===true};Buffer.compare=function compare(a,b){if(!Buffer.isBuffer(a)||!Buffer.isBuffer(b)){throw new TypeError("Arguments must be Buffers")}if(a===b)return 0;var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i>>1;case"base64":return base64ToBytes(string).length;default:if(loweredCase)return utf8ToBytes(string).length;encoding=(""+encoding).toLowerCase();loweredCase=true}}}Buffer.byteLength=byteLength;function slowToString(encoding,start,end){var loweredCase=false;if(start===undefined||start<0){start=0}if(start>this.length){return""}if(end===undefined||end>this.length){end=this.length}if(end<=0){return""}end>>>=0;start>>>=0;if(end<=start){return""}if(!encoding)encoding="utf8";while(true){switch(encoding){case"hex":return hexSlice(this,start,end);case"utf8":case"utf-8":return utf8Slice(this,start,end);case"ascii":return asciiSlice(this,start,end);case"latin1":case"binary":return latin1Slice(this,start,end);case"base64":return base64Slice(this,start,end);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return utf16leSlice(this,start,end);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(encoding+"").toLowerCase();loweredCase=true}}}Buffer.prototype._isBuffer=true;function swap(b,n,m){var i=b[n];b[n]=b[m];b[m]=i}Buffer.prototype.swap16=function swap16(){var len=this.length;if(len%2!==0){throw new RangeError("Buffer size must be a multiple of 16-bits")}for(var i=0;i0){str=this.toString("hex",0,max).match(/.{2}/g).join(" ");if(this.length>max)str+=" ... "}return""};Buffer.prototype.compare=function compare(target,start,end,thisStart,thisEnd){if(!Buffer.isBuffer(target)){throw new TypeError("Argument must be a Buffer")}if(start===undefined){start=0}if(end===undefined){end=target?target.length:0}if(thisStart===undefined){thisStart=0}if(thisEnd===undefined){thisEnd=this.length}if(start<0||end>target.length||thisStart<0||thisEnd>this.length){throw new RangeError("out of range index")}if(thisStart>=thisEnd&&start>=end){return 0}if(thisStart>=thisEnd){return-1}if(start>=end){return 1}start>>>=0;end>>>=0;thisStart>>>=0;thisEnd>>>=0;if(this===target)return 0;var x=thisEnd-thisStart;var y=end-start;var len=Math.min(x,y);var thisCopy=this.slice(thisStart,thisEnd);var targetCopy=target.slice(start,end);for(var i=0;i2147483647){byteOffset=2147483647}else if(byteOffset<-2147483648){byteOffset=-2147483648}byteOffset=+byteOffset;if(numberIsNaN(byteOffset)){byteOffset=dir?0:buffer.length-1}if(byteOffset<0)byteOffset=buffer.length+byteOffset;if(byteOffset>=buffer.length){if(dir)return-1;else byteOffset=buffer.length-1}else if(byteOffset<0){if(dir)byteOffset=0;else return-1}if(typeof val==="string"){val=Buffer.from(val,encoding)}if(Buffer.isBuffer(val)){if(val.length===0){return-1}return arrayIndexOf(buffer,val,byteOffset,encoding,dir)}else if(typeof val==="number"){val=val&255;if(typeof Uint8Array.prototype.indexOf==="function"){if(dir){return Uint8Array.prototype.indexOf.call(buffer,val,byteOffset)}else{return Uint8Array.prototype.lastIndexOf.call(buffer,val,byteOffset)}}return arrayIndexOf(buffer,[val],byteOffset,encoding,dir)}throw new TypeError("val must be string, number or Buffer")}function arrayIndexOf(arr,val,byteOffset,encoding,dir){var indexSize=1;var arrLength=arr.length;var valLength=val.length;if(encoding!==undefined){encoding=String(encoding).toLowerCase();if(encoding==="ucs2"||encoding==="ucs-2"||encoding==="utf16le"||encoding==="utf-16le"){if(arr.length<2||val.length<2){return-1}indexSize=2;arrLength/=2;valLength/=2;byteOffset/=2}}function read(buf,i){if(indexSize===1){return buf[i]}else{return buf.readUInt16BE(i*indexSize)}}var i;if(dir){var foundIndex=-1;for(i=byteOffset;iarrLength)byteOffset=arrLength-valLength;for(i=byteOffset;i>=0;i--){var found=true;for(var j=0;jremaining){length=remaining}}var strLen=string.length;if(strLen%2!==0)throw new TypeError("Invalid hex string");if(length>strLen/2){length=strLen/2}for(var i=0;i>>0;if(isFinite(length)){length=length>>>0;if(encoding===undefined)encoding="utf8"}else{encoding=length;length=undefined}}else{throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported")}var remaining=this.length-offset;if(length===undefined||length>remaining)length=remaining;if(string.length>0&&(length<0||offset<0)||offset>this.length){throw new RangeError("Attempt to write outside buffer bounds")}if(!encoding)encoding="utf8";var loweredCase=false;for(;;){switch(encoding){case"hex":return hexWrite(this,string,offset,length);case"utf8":case"utf-8":return utf8Write(this,string,offset,length);case"ascii":return asciiWrite(this,string,offset,length);case"latin1":case"binary":return latin1Write(this,string,offset,length);case"base64":return base64Write(this,string,offset,length);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return ucs2Write(this,string,offset,length);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(""+encoding).toLowerCase();loweredCase=true}}};Buffer.prototype.toJSON=function toJSON(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function base64Slice(buf,start,end){if(start===0&&end===buf.length){return base64.fromByteArray(buf)}else{return base64.fromByteArray(buf.slice(start,end))}}function utf8Slice(buf,start,end){end=Math.min(buf.length,end);var res=[];var i=start;while(i239?4:firstByte>223?3:firstByte>191?2:1;if(i+bytesPerSequence<=end){var secondByte,thirdByte,fourthByte,tempCodePoint;switch(bytesPerSequence){case 1:if(firstByte<128){codePoint=firstByte}break;case 2:secondByte=buf[i+1];if((secondByte&192)===128){tempCodePoint=(firstByte&31)<<6|secondByte&63;if(tempCodePoint>127){codePoint=tempCodePoint}}break;case 3:secondByte=buf[i+1];thirdByte=buf[i+2];if((secondByte&192)===128&&(thirdByte&192)===128){tempCodePoint=(firstByte&15)<<12|(secondByte&63)<<6|thirdByte&63;if(tempCodePoint>2047&&(tempCodePoint<55296||tempCodePoint>57343)){codePoint=tempCodePoint}}break;case 4:secondByte=buf[i+1];thirdByte=buf[i+2];fourthByte=buf[i+3];if((secondByte&192)===128&&(thirdByte&192)===128&&(fourthByte&192)===128){tempCodePoint=(firstByte&15)<<18|(secondByte&63)<<12|(thirdByte&63)<<6|fourthByte&63;if(tempCodePoint>65535&&tempCodePoint<1114112){codePoint=tempCodePoint}}}}if(codePoint===null){codePoint=65533;bytesPerSequence=1}else if(codePoint>65535){codePoint-=65536;res.push(codePoint>>>10&1023|55296);codePoint=56320|codePoint&1023}res.push(codePoint);i+=bytesPerSequence}return decodeCodePointsArray(res)}var MAX_ARGUMENTS_LENGTH=4096;function decodeCodePointsArray(codePoints){var len=codePoints.length;if(len<=MAX_ARGUMENTS_LENGTH){return String.fromCharCode.apply(String,codePoints)}var res="";var i=0;while(ilen)end=len;var out="";for(var i=start;ilen){start=len}if(end<0){end+=len;if(end<0)end=0}else if(end>len){end=len}if(endlength)throw new RangeError("Trying to access beyond buffer length")}Buffer.prototype.readUIntLE=function readUIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i>>0;byteLength=byteLength>>>0;if(!noAssert){checkOffset(offset,byteLength,this.length)}var val=this[offset+--byteLength];var mul=1;while(byteLength>0&&(mul*=256)){val+=this[offset+--byteLength]*mul}return val};Buffer.prototype.readUInt8=function readUInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);return this[offset]};Buffer.prototype.readUInt16LE=function readUInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]|this[offset+1]<<8};Buffer.prototype.readUInt16BE=function readUInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]<<8|this[offset+1]};Buffer.prototype.readUInt32LE=function readUInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return(this[offset]|this[offset+1]<<8|this[offset+2]<<16)+this[offset+3]*16777216};Buffer.prototype.readUInt32BE=function readUInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]*16777216+(this[offset+1]<<16|this[offset+2]<<8|this[offset+3])};Buffer.prototype.readIntLE=function readIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readIntBE=function readIntBE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var i=byteLength;var mul=1;var val=this[offset+--i];while(i>0&&(mul*=256)){val+=this[offset+--i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readInt8=function readInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);if(!(this[offset]&128))return this[offset];return(255-this[offset]+1)*-1};Buffer.prototype.readInt16LE=function readInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset]|this[offset+1]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt16BE=function readInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset+1]|this[offset]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt32LE=function readInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]|this[offset+1]<<8|this[offset+2]<<16|this[offset+3]<<24};Buffer.prototype.readInt32BE=function readInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]<<24|this[offset+1]<<16|this[offset+2]<<8|this[offset+3]};Buffer.prototype.readFloatLE=function readFloatLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,true,23,4)};Buffer.prototype.readFloatBE=function readFloatBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,false,23,4)};Buffer.prototype.readDoubleLE=function readDoubleLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,true,52,8)};Buffer.prototype.readDoubleBE=function readDoubleBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,false,52,8)};function checkInt(buf,value,offset,ext,max,min){if(!Buffer.isBuffer(buf))throw new TypeError('"buffer" argument must be a Buffer instance');if(value>max||valuebuf.length)throw new RangeError("Index out of range")}Buffer.prototype.writeUIntLE=function writeUIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var mul=1;var i=0;this[offset]=value&255;while(++i>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var i=byteLength-1;var mul=1;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUInt8=function writeUInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,255,0);this[offset]=value&255;return offset+1};Buffer.prototype.writeUInt16LE=function writeUInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeUInt16BE=function writeUInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeUInt32LE=function writeUInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset+3]=value>>>24;this[offset+2]=value>>>16;this[offset+1]=value>>>8;this[offset]=value&255;return offset+4};Buffer.prototype.writeUInt32BE=function writeUInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};Buffer.prototype.writeIntLE=function writeIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=0;var mul=1;var sub=0;this[offset]=value&255;while(++i>0)-sub&255}return offset+byteLength};Buffer.prototype.writeIntBE=function writeIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=byteLength-1;var mul=1;var sub=0;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){if(value<0&&sub===0&&this[offset+i+1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeInt8=function writeInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,127,-128);if(value<0)value=255+value+1;this[offset]=value&255;return offset+1};Buffer.prototype.writeInt16LE=function writeInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeInt16BE=function writeInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeInt32LE=function writeInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);this[offset]=value&255;this[offset+1]=value>>>8;this[offset+2]=value>>>16;this[offset+3]=value>>>24;return offset+4};Buffer.prototype.writeInt32BE=function writeInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(value<0)value=4294967295+value+1;this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};function checkIEEE754(buf,value,offset,ext,max,min){if(offset+ext>buf.length)throw new RangeError("Index out of range");if(offset<0)throw new RangeError("Index out of range")}function writeFloat(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,4,3.4028234663852886e38,-3.4028234663852886e38)}ieee754.write(buf,value,offset,littleEndian,23,4);return offset+4}Buffer.prototype.writeFloatLE=function writeFloatLE(value,offset,noAssert){return writeFloat(this,value,offset,true,noAssert)};Buffer.prototype.writeFloatBE=function writeFloatBE(value,offset,noAssert){return writeFloat(this,value,offset,false,noAssert)};function writeDouble(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,8,1.7976931348623157e308,-1.7976931348623157e308)}ieee754.write(buf,value,offset,littleEndian,52,8);return offset+8}Buffer.prototype.writeDoubleLE=function writeDoubleLE(value,offset,noAssert){return writeDouble(this,value,offset,true,noAssert)};Buffer.prototype.writeDoubleBE=function writeDoubleBE(value,offset,noAssert){return writeDouble(this,value,offset,false,noAssert)};Buffer.prototype.copy=function copy(target,targetStart,start,end){if(!start)start=0;if(!end&&end!==0)end=this.length;if(targetStart>=target.length)targetStart=target.length;if(!targetStart)targetStart=0;if(end>0&&end=this.length)throw new RangeError("sourceStart out of bounds");if(end<0)throw new RangeError("sourceEnd out of bounds");if(end>this.length)end=this.length;if(target.length-targetStart=0;--i){target[i+targetStart]=this[i+start]}}else if(len<1e3){for(i=0;i>>0;end=end===undefined?this.length:end>>>0;if(!val)val=0;var i;if(typeof val==="number"){for(i=start;i55295&&codePoint<57344){if(!leadSurrogate){if(codePoint>56319){if((units-=3)>-1)bytes.push(239,191,189);continue}else if(i+1===length){if((units-=3)>-1)bytes.push(239,191,189);continue}leadSurrogate=codePoint;continue}if(codePoint<56320){if((units-=3)>-1)bytes.push(239,191,189);leadSurrogate=codePoint;continue}codePoint=(leadSurrogate-55296<<10|codePoint-56320)+65536}else if(leadSurrogate){if((units-=3)>-1)bytes.push(239,191,189)}leadSurrogate=null;if(codePoint<128){if((units-=1)<0)break;bytes.push(codePoint)}else if(codePoint<2048){if((units-=2)<0)break;bytes.push(codePoint>>6|192,codePoint&63|128)}else if(codePoint<65536){if((units-=3)<0)break;bytes.push(codePoint>>12|224,codePoint>>6&63|128,codePoint&63|128)}else if(codePoint<1114112){if((units-=4)<0)break;bytes.push(codePoint>>18|240,codePoint>>12&63|128,codePoint>>6&63|128,codePoint&63|128)}else{throw new Error("Invalid code point")}}return bytes}function asciiToBytes(str){var byteArray=[];for(var i=0;i>8;lo=c%256;byteArray.push(lo);byteArray.push(hi)}return byteArray}function base64ToBytes(str){return base64.toByteArray(base64clean(str))}function blitBuffer(src,dst,offset,length){for(var i=0;i=dst.length||i>=src.length)break;dst[i+offset]=src[i]}return i}function isArrayBuffer(obj){return obj instanceof ArrayBuffer||obj!=null&&obj.constructor!=null&&obj.constructor.name==="ArrayBuffer"&&typeof obj.byteLength==="number"}function isArrayBufferView(obj){return typeof ArrayBuffer.isView==="function"&&ArrayBuffer.isView(obj)}function numberIsNaN(obj){return obj!==obj}},{"base64-js":6,ieee754:12}],10:[function(require,module,exports){(function(Buffer){function isArray(arg){if(Array.isArray){return Array.isArray(arg)}return objectToString(arg)==="[object Array]"}exports.isArray=isArray;function isBoolean(arg){return typeof arg==="boolean"}exports.isBoolean=isBoolean;function isNull(arg){return arg===null}exports.isNull=isNull;function isNullOrUndefined(arg){return arg==null}exports.isNullOrUndefined=isNullOrUndefined;function isNumber(arg){return typeof arg==="number"}exports.isNumber=isNumber;function isString(arg){return typeof arg==="string"}exports.isString=isString;function isSymbol(arg){return typeof arg==="symbol"}exports.isSymbol=isSymbol;function isUndefined(arg){return arg===void 0}exports.isUndefined=isUndefined;function isRegExp(re){return objectToString(re)==="[object RegExp]"}exports.isRegExp=isRegExp;function isObject(arg){return typeof arg==="object"&&arg!==null}exports.isObject=isObject;function isDate(d){return objectToString(d)==="[object Date]"}exports.isDate=isDate;function isError(e){return objectToString(e)==="[object Error]"||e instanceof Error}exports.isError=isError;function isFunction(arg){return typeof arg==="function"}exports.isFunction=isFunction;function isPrimitive(arg){return arg===null||typeof arg==="boolean"||typeof arg==="number"||typeof arg==="string"||typeof arg==="symbol"||typeof arg==="undefined"}exports.isPrimitive=isPrimitive;exports.isBuffer=Buffer.isBuffer;function objectToString(o){return Object.prototype.toString.call(o)}}).call(this,{isBuffer:require("../../is-buffer/index.js")})},{"../../is-buffer/index.js":14}],11:[function(require,module,exports){function EventEmitter(){this._events=this._events||{};this._maxListeners=this._maxListeners||undefined}module.exports=EventEmitter;EventEmitter.EventEmitter=EventEmitter;EventEmitter.prototype._events=undefined;EventEmitter.prototype._maxListeners=undefined;EventEmitter.defaultMaxListeners=10;EventEmitter.prototype.setMaxListeners=function(n){if(!isNumber(n)||n<0||isNaN(n))throw TypeError("n must be a positive number");this._maxListeners=n;return this};EventEmitter.prototype.emit=function(type){var er,handler,len,args,i,listeners;if(!this._events)this._events={};if(type==="error"){if(!this._events.error||isObject(this._events.error)&&!this._events.error.length){er=arguments[1];if(er instanceof Error){throw er}else{var err=new Error('Uncaught, unspecified "error" event. ('+er+")");err.context=er;throw err}}}handler=this._events[type];if(isUndefined(handler))return false;if(isFunction(handler)){switch(arguments.length){case 1:handler.call(this);break;case 2:handler.call(this,arguments[1]);break;case 3:handler.call(this,arguments[1],arguments[2]);break;default:args=Array.prototype.slice.call(arguments,1);handler.apply(this,args)}}else if(isObject(handler)){args=Array.prototype.slice.call(arguments,1);listeners=handler.slice();len=listeners.length;for(i=0;i0&&this._events[type].length>m){this._events[type].warned=true;console.error("(node) warning: possible EventEmitter memory "+"leak detected. %d listeners added. "+"Use emitter.setMaxListeners() to increase limit.",this._events[type].length);if(typeof console.trace==="function"){console.trace()}}}return this};EventEmitter.prototype.on=EventEmitter.prototype.addListener;EventEmitter.prototype.once=function(type,listener){if(!isFunction(listener))throw TypeError("listener must be a function");var fired=false;function g(){this.removeListener(type,g);if(!fired){fired=true;listener.apply(this,arguments)}}g.listener=listener;this.on(type,g);return this};EventEmitter.prototype.removeListener=function(type,listener){var list,position,length,i;if(!isFunction(listener))throw TypeError("listener must be a function");if(!this._events||!this._events[type])return this;list=this._events[type];length=list.length;position=-1;if(list===listener||isFunction(list.listener)&&list.listener===listener){delete this._events[type];if(this._events.removeListener)this.emit("removeListener",type,listener)}else if(isObject(list)){for(i=length;i-- >0;){if(list[i]===listener||list[i].listener&&list[i].listener===listener){position=i;break}}if(position<0)return this;if(list.length===1){list.length=0;delete this._events[type]}else{list.splice(position,1)}if(this._events.removeListener)this.emit("removeListener",type,listener)}return this};EventEmitter.prototype.removeAllListeners=function(type){var key,listeners;if(!this._events)return this;if(!this._events.removeListener){if(arguments.length===0)this._events={};else if(this._events[type])delete this._events[type];return this}if(arguments.length===0){for(key in this._events){if(key==="removeListener")continue;this.removeAllListeners(key)}this.removeAllListeners("removeListener");this._events={};return this}listeners=this._events[type];if(isFunction(listeners)){this.removeListener(type,listeners)}else if(listeners){while(listeners.length)this.removeListener(type,listeners[listeners.length-1])}delete this._events[type];return this};EventEmitter.prototype.listeners=function(type){var ret;if(!this._events||!this._events[type])ret=[];else if(isFunction(this._events[type]))ret=[this._events[type]];else ret=this._events[type].slice();return ret};EventEmitter.prototype.listenerCount=function(type){if(this._events){var evlistener=this._events[type];if(isFunction(evlistener))return 1;else if(evlistener)return evlistener.length}return 0};EventEmitter.listenerCount=function(emitter,type){return emitter.listenerCount(type)};function isFunction(arg){return typeof arg==="function"}function isNumber(arg){return typeof arg==="number"}function isObject(arg){return typeof arg==="object"&&arg!==null}function isUndefined(arg){return arg===void 0}},{}],12:[function(require,module,exports){exports.read=function(buffer,offset,isLE,mLen,nBytes){var e,m;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var nBits=-7;var i=isLE?nBytes-1:0;var d=isLE?-1:1;var s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8){}m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8){}if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)};exports.write=function(buffer,value,offset,isLE,mLen,nBytes){var e,m,c;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0;var i=isLE?0:nBytes-1;var d=isLE?1:-1;var s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8){}e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8){}buffer[offset+i-d]|=s*128}},{}],13:[function(require,module,exports){if(typeof Object.create==="function"){module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;ctor.prototype=Object.create(superCtor.prototype,{constructor:{value:ctor,enumerable:false,writable:true,configurable:true}})}}else{module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;var TempCtor=function(){};TempCtor.prototype=superCtor.prototype;ctor.prototype=new TempCtor;ctor.prototype.constructor=ctor}}},{}],14:[function(require,module,exports){module.exports=function(obj){return obj!=null&&(isBuffer(obj)||isSlowBuffer(obj)||!!obj._isBuffer)};function isBuffer(obj){return!!obj.constructor&&typeof obj.constructor.isBuffer==="function"&&obj.constructor.isBuffer(obj)}function isSlowBuffer(obj){return typeof obj.readFloatLE==="function"&&typeof obj.slice==="function"&&isBuffer(obj.slice(0,0))}},{}],15:[function(require,module,exports){var toString={}.toString;module.exports=Array.isArray||function(arr){return toString.call(arr)=="[object Array]"}},{}],16:[function(require,module,exports){(function(process){"use strict";if(!process.version||process.version.indexOf("v0.")===0||process.version.indexOf("v1.")===0&&process.version.indexOf("v1.8.")!==0){module.exports=nextTick}else{module.exports=process.nextTick}function nextTick(fn,arg1,arg2,arg3){if(typeof fn!=="function"){throw new TypeError('"callback" argument must be a function')}var len=arguments.length;var args,i;switch(len){case 0:case 1:return process.nextTick(fn);case 2:return process.nextTick(function afterTickOne(){fn.call(null,arg1)});case 3:return process.nextTick(function afterTickTwo(){fn.call(null,arg1,arg2)});case 4:return process.nextTick(function afterTickThree(){fn.call(null,arg1,arg2,arg3)});default:args=new Array(len-1);i=0;while(i1){for(var i=1;i0){if(typeof chunk!=="string"&&!state.objectMode&&Object.getPrototypeOf(chunk)!==Buffer.prototype){chunk=_uint8ArrayToBuffer(chunk)}if(addToFront){if(state.endEmitted)stream.emit("error",new Error("stream.unshift() after end event"));else addChunk(stream,state,chunk,true)}else if(state.ended){stream.emit("error",new Error("stream.push() after EOF"))}else{state.reading=false;if(state.decoder&&!encoding){chunk=state.decoder.write(chunk);if(state.objectMode||chunk.length!==0)addChunk(stream,state,chunk,false);else maybeReadMore(stream,state)}else{addChunk(stream,state,chunk,false)}}}else if(!addToFront){state.reading=false}}return needMoreData(state)}function addChunk(stream,state,chunk,addToFront){if(state.flowing&&state.length===0&&!state.sync){stream.emit("data",chunk);stream.read(0)}else{state.length+=state.objectMode?1:chunk.length;if(addToFront)state.buffer.unshift(chunk);else state.buffer.push(chunk);if(state.needReadable)emitReadable(stream)}maybeReadMore(stream,state)}function chunkInvalid(state,chunk){var er;if(!_isUint8Array(chunk)&&typeof chunk!=="string"&&chunk!==undefined&&!state.objectMode){er=new TypeError("Invalid non-string/buffer chunk")}return er}function needMoreData(state){return!state.ended&&(state.needReadable||state.length=MAX_HWM){n=MAX_HWM}else{n--;n|=n>>>1;n|=n>>>2;n|=n>>>4;n|=n>>>8;n|=n>>>16;n++}return n}function howMuchToRead(n,state){if(n<=0||state.length===0&&state.ended)return 0;if(state.objectMode)return 1;if(n!==n){if(state.flowing&&state.length)return state.buffer.head.data.length;else return state.length}if(n>state.highWaterMark)state.highWaterMark=computeNewHighWaterMark(n);if(n<=state.length)return n;if(!state.ended){state.needReadable=true;return 0}return state.length}Readable.prototype.read=function(n){debug("read",n);n=parseInt(n,10);var state=this._readableState;var nOrig=n;if(n!==0)state.emittedReadable=false;if(n===0&&state.needReadable&&(state.length>=state.highWaterMark||state.ended)){debug("read: emitReadable",state.length,state.ended);if(state.length===0&&state.ended)endReadable(this);else emitReadable(this);return null}n=howMuchToRead(n,state);if(n===0&&state.ended){if(state.length===0)endReadable(this);return null}var doRead=state.needReadable;debug("need readable",doRead);if(state.length===0||state.length-n0)ret=fromList(n,state);else ret=null;if(ret===null){state.needReadable=true;n=0}else{state.length-=n}if(state.length===0){if(!state.ended)state.needReadable=true;if(nOrig!==n&&state.ended)endReadable(this)}if(ret!==null)this.emit("data",ret);return ret};function onEofChunk(stream,state){if(state.ended)return;if(state.decoder){var chunk=state.decoder.end();if(chunk&&chunk.length){state.buffer.push(chunk);state.length+=state.objectMode?1:chunk.length}}state.ended=true;emitReadable(stream)}function emitReadable(stream){var state=stream._readableState;state.needReadable=false;if(!state.emittedReadable){debug("emitReadable",state.flowing);state.emittedReadable=true;if(state.sync)processNextTick(emitReadable_,stream);else emitReadable_(stream)}}function emitReadable_(stream){debug("emit readable");stream.emit("readable");flow(stream)}function maybeReadMore(stream,state){if(!state.readingMore){state.readingMore=true;processNextTick(maybeReadMore_,stream,state)}}function maybeReadMore_(stream,state){var len=state.length;while(!state.reading&&!state.flowing&&!state.ended&&state.length1&&indexOf(state.pipes,dest)!==-1)&&!cleanedUp){debug("false write response, pause",src._readableState.awaitDrain);src._readableState.awaitDrain++;increasedAwaitDrain=true}src.pause()}}function onerror(er){debug("onerror",er);unpipe();dest.removeListener("error",onerror);if(EElistenerCount(dest,"error")===0)dest.emit("error",er)}prependListener(dest,"error",onerror);function onclose(){dest.removeListener("finish",onfinish);unpipe()}dest.once("close",onclose);function onfinish(){debug("onfinish");dest.removeListener("close",onclose);unpipe()}dest.once("finish",onfinish);function unpipe(){debug("unpipe");src.unpipe(dest)}dest.emit("pipe",src);if(!state.flowing){debug("pipe resume");src.resume()}return dest};function pipeOnDrain(src){return function(){var state=src._readableState;debug("pipeOnDrain",state.awaitDrain);if(state.awaitDrain)state.awaitDrain--;if(state.awaitDrain===0&&EElistenerCount(src,"data")){state.flowing=true;flow(src)}}}Readable.prototype.unpipe=function(dest){var state=this._readableState;var unpipeInfo={hasUnpiped:false};if(state.pipesCount===0)return this;if(state.pipesCount===1){if(dest&&dest!==state.pipes)return this;if(!dest)dest=state.pipes;state.pipes=null;state.pipesCount=0;state.flowing=false;if(dest)dest.emit("unpipe",this,unpipeInfo);return this}if(!dest){var dests=state.pipes;var len=state.pipesCount;state.pipes=null;state.pipesCount=0;state.flowing=false;for(var i=0;i=state.length){if(state.decoder)ret=state.buffer.join("");else if(state.buffer.length===1)ret=state.buffer.head.data;else ret=state.buffer.concat(state.length);state.buffer.clear()}else{ret=fromListPartial(n,state.buffer,state.decoder)}return ret}function fromListPartial(n,list,hasStrings){var ret;if(nstr.length?str.length:n;if(nb===str.length)ret+=str;else ret+=str.slice(0,n);n-=nb;if(n===0){if(nb===str.length){++c;if(p.next)list.head=p.next;else list.head=list.tail=null}else{list.head=p;p.data=str.slice(nb)}break}++c}list.length-=c;return ret}function copyFromBuffer(n,list){var ret=Buffer.allocUnsafe(n);var p=list.head;var c=1;p.data.copy(ret);n-=p.data.length;while(p=p.next){var buf=p.data;var nb=n>buf.length?buf.length:n;buf.copy(ret,ret.length-n,0,nb);n-=nb;if(n===0){if(nb===buf.length){++c;if(p.next)list.head=p.next;else list.head=list.tail=null}else{list.head=p;p.data=buf.slice(nb)}break}++c}list.length-=c;return ret}function endReadable(stream){var state=stream._readableState;if(state.length>0)throw new Error('"endReadable()" called on non-empty stream');if(!state.endEmitted){state.ended=true;processNextTick(endReadableNT,state,stream)}}function endReadableNT(state,stream){if(!state.endEmitted&&state.length===0){state.endEmitted=true;stream.readable=false;stream.emit("end")}}function forEach(xs,f){for(var i=0,l=xs.length;i-1?setImmediate:processNextTick;var Duplex;Writable.WritableState=WritableState;var util=require("core-util-is");util.inherits=require("inherits");var internalUtil={deprecate:require("util-deprecate")};var Stream=require("./internal/streams/stream");var Buffer=require("safe-buffer").Buffer;var OurUint8Array=global.Uint8Array||function(){};function _uint8ArrayToBuffer(chunk){return Buffer.from(chunk)}function _isUint8Array(obj){return Buffer.isBuffer(obj)||obj instanceof OurUint8Array}var destroyImpl=require("./internal/streams/destroy");util.inherits(Writable,Stream);function nop(){}function WritableState(options,stream){Duplex=Duplex||require("./_stream_duplex");options=options||{};this.objectMode=!!options.objectMode;if(stream instanceof Duplex)this.objectMode=this.objectMode||!!options.writableObjectMode;var hwm=options.highWaterMark;var defaultHwm=this.objectMode?16:16*1024;this.highWaterMark=hwm||hwm===0?hwm:defaultHwm;this.highWaterMark=Math.floor(this.highWaterMark);this.finalCalled=false;this.needDrain=false;this.ending=false;this.ended=false;this.finished=false;this.destroyed=false;var noDecode=options.decodeStrings===false;this.decodeStrings=!noDecode;this.defaultEncoding=options.defaultEncoding||"utf8";this.length=0;this.writing=false;this.corked=0;this.sync=true;this.bufferProcessing=false;this.onwrite=function(er){onwrite(stream,er)};this.writecb=null;this.writelen=0;this.bufferedRequest=null;this.lastBufferedRequest=null;this.pendingcb=0;this.prefinished=false;this.errorEmitted=false;this.bufferedRequestCount=0;this.corkedRequestsFree=new CorkedRequest(this)}WritableState.prototype.getBuffer=function getBuffer(){var current=this.bufferedRequest;var out=[];while(current){out.push(current);current=current.next}return out};(function(){try{Object.defineProperty(WritableState.prototype,"buffer",{get:internalUtil.deprecate(function(){return this.getBuffer()},"_writableState.buffer is deprecated. Use _writableState.getBuffer "+"instead.","DEP0003")})}catch(_){}})();var realHasInstance;if(typeof Symbol==="function"&&Symbol.hasInstance&&typeof Function.prototype[Symbol.hasInstance]==="function"){realHasInstance=Function.prototype[Symbol.hasInstance];Object.defineProperty(Writable,Symbol.hasInstance,{value:function(object){if(realHasInstance.call(this,object))return true;return object&&object._writableState instanceof WritableState}})}else{realHasInstance=function(object){return object instanceof this}}function Writable(options){Duplex=Duplex||require("./_stream_duplex");if(!realHasInstance.call(Writable,this)&&!(this instanceof Duplex)){return new Writable(options)}this._writableState=new WritableState(options,this);this.writable=true;if(options){if(typeof options.write==="function")this._write=options.write;if(typeof options.writev==="function")this._writev=options.writev;if(typeof options.destroy==="function")this._destroy=options.destroy;if(typeof options.final==="function")this._final=options.final}Stream.call(this)}Writable.prototype.pipe=function(){this.emit("error",new Error("Cannot pipe, not readable"))};function writeAfterEnd(stream,cb){var er=new Error("write after end");stream.emit("error",er);processNextTick(cb,er)}function validChunk(stream,state,chunk,cb){var valid=true;var er=false;if(chunk===null){er=new TypeError("May not write null values to stream")}else if(typeof chunk!=="string"&&chunk!==undefined&&!state.objectMode){er=new TypeError("Invalid non-string/buffer chunk")}if(er){stream.emit("error",er);processNextTick(cb,er);valid=false}return valid}Writable.prototype.write=function(chunk,encoding,cb){var state=this._writableState;var ret=false;var isBuf=_isUint8Array(chunk)&&!state.objectMode;if(isBuf&&!Buffer.isBuffer(chunk)){chunk=_uint8ArrayToBuffer(chunk)}if(typeof encoding==="function"){cb=encoding;encoding=null}if(isBuf)encoding="buffer";else if(!encoding)encoding=state.defaultEncoding;if(typeof cb!=="function")cb=nop;if(state.ended)writeAfterEnd(this,cb);else if(isBuf||validChunk(this,state,chunk,cb)){state.pendingcb++;ret=writeOrBuffer(this,state,isBuf,chunk,encoding,cb)}return ret};Writable.prototype.cork=function(){var state=this._writableState;state.corked++};Writable.prototype.uncork=function(){var state=this._writableState;if(state.corked){state.corked--;if(!state.writing&&!state.corked&&!state.finished&&!state.bufferProcessing&&state.bufferedRequest)clearBuffer(this,state)}};Writable.prototype.setDefaultEncoding=function setDefaultEncoding(encoding){if(typeof encoding==="string")encoding=encoding.toLowerCase();if(!(["hex","utf8","utf-8","ascii","binary","base64","ucs2","ucs-2","utf16le","utf-16le","raw"].indexOf((encoding+"").toLowerCase())>-1))throw new TypeError("Unknown encoding: "+encoding);this._writableState.defaultEncoding=encoding;return this};function decodeChunk(state,chunk,encoding){if(!state.objectMode&&state.decodeStrings!==false&&typeof chunk==="string"){chunk=Buffer.from(chunk,encoding)}return chunk}function writeOrBuffer(stream,state,isBuf,chunk,encoding,cb){if(!isBuf){var newChunk=decodeChunk(state,chunk,encoding);if(chunk!==newChunk){isBuf=true;encoding="buffer";chunk=newChunk}}var len=state.objectMode?1:chunk.length;state.length+=len;var ret=state.length0)this.tail.next=entry;else this.head=entry;this.tail=entry;++this.length};BufferList.prototype.unshift=function unshift(v){var entry={data:v,next:this.head};if(this.length===0)this.tail=entry;this.head=entry;++this.length};BufferList.prototype.shift=function shift(){if(this.length===0)return;var ret=this.head.data;if(this.length===1)this.head=this.tail=null;else this.head=this.head.next;--this.length;return ret};BufferList.prototype.clear=function clear(){this.head=this.tail=null;this.length=0};BufferList.prototype.join=function join(s){if(this.length===0)return"";var p=this.head;var ret=""+p.data;while(p=p.next){ret+=s+p.data}return ret};BufferList.prototype.concat=function concat(n){if(this.length===0)return Buffer.alloc(0);if(this.length===1)return this.head.data;var ret=Buffer.allocUnsafe(n>>>0);var p=this.head;var i=0;while(p){copyBuffer(p.data,ret,i);i+=p.data.length;p=p.next}return ret};return BufferList}()},{"safe-buffer":28}],25:[function(require,module,exports){"use strict";var processNextTick=require("process-nextick-args");function destroy(err,cb){var _this=this;var readableDestroyed=this._readableState&&this._readableState.destroyed;var writableDestroyed=this._writableState&&this._writableState.destroyed;if(readableDestroyed||writableDestroyed){if(cb){cb(err)}else if(err&&(!this._writableState||!this._writableState.errorEmitted)){processNextTick(emitErrorNT,this,err)}return}if(this._readableState){this._readableState.destroyed=true}if(this._writableState){this._writableState.destroyed=true}this._destroy(err||null,function(err){if(!cb&&err){processNextTick(emitErrorNT,_this,err);if(_this._writableState){_this._writableState.errorEmitted=true}}else if(cb){cb(err)}})}function undestroy(){if(this._readableState){this._readableState.destroyed=false;this._readableState.reading=false;this._readableState.ended=false;this._readableState.endEmitted=false}if(this._writableState){this._writableState.destroyed=false;this._writableState.ended=false;this._writableState.ending=false;this._writableState.finished=false;this._writableState.errorEmitted=false}}function emitErrorNT(self,err){self.emit("error",err)}module.exports={destroy:destroy,undestroy:undestroy}},{"process-nextick-args":16}],26:[function(require,module,exports){module.exports=require("events").EventEmitter},{events:11}],27:[function(require,module,exports){exports=module.exports=require("./lib/_stream_readable.js");exports.Stream=exports;exports.Readable=exports;exports.Writable=require("./lib/_stream_writable.js");exports.Duplex=require("./lib/_stream_duplex.js");exports.Transform=require("./lib/_stream_transform.js");exports.PassThrough=require("./lib/_stream_passthrough.js")},{"./lib/_stream_duplex.js":19,"./lib/_stream_passthrough.js":20,"./lib/_stream_readable.js":21,"./lib/_stream_transform.js":22,"./lib/_stream_writable.js":23}],28:[function(require,module,exports){var buffer=require("buffer");var Buffer=buffer.Buffer;function copyProps(src,dst){for(var key in src){dst[key]=src[key]}}if(Buffer.from&&Buffer.alloc&&Buffer.allocUnsafe&&Buffer.allocUnsafeSlow){module.exports=buffer}else{copyProps(buffer,exports);exports.Buffer=SafeBuffer}function SafeBuffer(arg,encodingOrOffset,length){return Buffer(arg,encodingOrOffset,length)}copyProps(Buffer,SafeBuffer);SafeBuffer.from=function(arg,encodingOrOffset,length){if(typeof arg==="number"){throw new TypeError("Argument must not be a number")}return Buffer(arg,encodingOrOffset,length)};SafeBuffer.alloc=function(size,fill,encoding){if(typeof size!=="number"){throw new TypeError("Argument must be a number")}var buf=Buffer(size);if(fill!==undefined){if(typeof encoding==="string"){buf.fill(fill,encoding)}else{buf.fill(fill)}}else{buf.fill(0)}return buf};SafeBuffer.allocUnsafe=function(size){if(typeof size!=="number"){throw new TypeError("Argument must be a number")}return Buffer(size)};SafeBuffer.allocUnsafeSlow=function(size){if(typeof size!=="number"){throw new TypeError("Argument must be a number")}return buffer.SlowBuffer(size)}},{buffer:9}],29:[function(require,module,exports){"use strict";var Buffer=require("safe-buffer").Buffer;var isEncoding=Buffer.isEncoding||function(encoding){encoding=""+encoding;switch(encoding&&encoding.toLowerCase()){case"hex":case"utf8":case"utf-8":case"ascii":case"binary":case"base64":case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":case"raw":return true;default:return false}};function _normalizeEncoding(enc){if(!enc)return"utf8";var retried;while(true){switch(enc){case"utf8":case"utf-8":return"utf8";case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return"utf16le";case"latin1":case"binary":return"latin1";case"base64":case"ascii":case"hex":return enc;default:if(retried)return;enc=(""+enc).toLowerCase();retried=true}}}function normalizeEncoding(enc){var nenc=_normalizeEncoding(enc);if(typeof nenc!=="string"&&(Buffer.isEncoding===isEncoding||!isEncoding(enc)))throw new Error("Unknown encoding: "+enc);return nenc||enc}exports.StringDecoder=StringDecoder;function StringDecoder(encoding){this.encoding=normalizeEncoding(encoding);var nb;switch(this.encoding){case"utf16le":this.text=utf16Text;this.end=utf16End;nb=4;break;case"utf8":this.fillLast=utf8FillLast;nb=4;break;case"base64":this.text=base64Text;this.end=base64End;nb=3;break;default:this.write=simpleWrite;this.end=simpleEnd;return}this.lastNeed=0;this.lastTotal=0;this.lastChar=Buffer.allocUnsafe(nb)}StringDecoder.prototype.write=function(buf){if(buf.length===0)return"";var r;var i;if(this.lastNeed){r=this.fillLast(buf);if(r===undefined)return"";i=this.lastNeed;this.lastNeed=0}else{i=0}if(i>5===6)return 2;else if(byte>>4===14)return 3;else if(byte>>3===30)return 4;return-1}function utf8CheckIncomplete(self,buf,i){var j=buf.length-1;if(j=0){if(nb>0)self.lastNeed=nb-1;return nb}if(--j=0){if(nb>0)self.lastNeed=nb-2;return nb}if(--j=0){if(nb>0){if(nb===2)nb=0;else self.lastNeed=nb-3}return nb}return 0}function utf8CheckExtraBytes(self,buf,p){if((buf[0]&192)!==128){self.lastNeed=0;return"�".repeat(p)}if(self.lastNeed>1&&buf.length>1){if((buf[1]&192)!==128){self.lastNeed=1;return"�".repeat(p+1)}if(self.lastNeed>2&&buf.length>2){if((buf[2]&192)!==128){self.lastNeed=2;return"�".repeat(p+2)}}}}function utf8FillLast(buf){var p=this.lastTotal-this.lastNeed;var r=utf8CheckExtraBytes(this,buf,p);if(r!==undefined)return r;if(this.lastNeed<=buf.length){buf.copy(this.lastChar,p,0,this.lastNeed);return this.lastChar.toString(this.encoding,0,this.lastTotal)}buf.copy(this.lastChar,p,0,buf.length);this.lastNeed-=buf.length}function utf8Text(buf,i){var total=utf8CheckIncomplete(this,buf,i);if(!this.lastNeed)return buf.toString("utf8",i);this.lastTotal=total;var end=buf.length-(total-this.lastNeed);buf.copy(this.lastChar,0,end);return buf.toString("utf8",i,end)}function utf8End(buf){var r=buf&&buf.length?this.write(buf):"";if(this.lastNeed)return r+"�".repeat(this.lastTotal-this.lastNeed);return r}function utf16Text(buf,i){if((buf.length-i)%2===0){var r=buf.toString("utf16le",i);if(r){var c=r.charCodeAt(r.length-1);if(c>=55296&&c<=56319){this.lastNeed=2;this.lastTotal=4;this.lastChar[0]=buf[buf.length-2];this.lastChar[1]=buf[buf.length-1];return r.slice(0,-1)}}return r}this.lastNeed=1;this.lastTotal=2;this.lastChar[0]=buf[buf.length-1];return buf.toString("utf16le",i,buf.length-1)}function utf16End(buf){var r=buf&&buf.length?this.write(buf):"";if(this.lastNeed){var end=this.lastTotal-this.lastNeed;return r+this.lastChar.toString("utf16le",0,end)}return r}function base64Text(buf,i){var n=(buf.length-i)%3;if(n===0)return buf.toString("base64",i);this.lastNeed=3-n;this.lastTotal=3;if(n===1){this.lastChar[0]=buf[buf.length-1]}else{this.lastChar[0]=buf[buf.length-2];this.lastChar[1]=buf[buf.length-1]}return buf.toString("base64",i,buf.length-n)}function base64End(buf){var r=buf&&buf.length?this.write(buf):"";if(this.lastNeed)return r+this.lastChar.toString("base64",0,3-this.lastNeed);return r}function simpleWrite(buf){return buf.toString(this.encoding)}function simpleEnd(buf){return buf&&buf.length?this.write(buf):""}},{"safe-buffer":28}],30:[function(require,module,exports){(function(global){module.exports=deprecate;function deprecate(fn,msg){if(config("noDeprecation")){return fn}var warned=false;function deprecated(){if(!warned){if(config("throwDeprecation")){throw new Error(msg)}else if(config("traceDeprecation")){console.trace(msg)}else{console.warn(msg)}warned=true}return fn.apply(this,arguments)}return deprecated}function config(name){try{if(!global.localStorage)return false}catch(_){return false}var val=global.localStorage[name];if(null==val)return false;return String(val).toLowerCase()==="true"}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}],31:[function(require,module,exports){arguments[4][13][0].apply(exports,arguments)},{dup:13}],32:[function(require,module,exports){module.exports=function isBuffer(arg){return arg&&typeof arg==="object"&&typeof arg.copy==="function"&&typeof arg.fill==="function"&&typeof arg.readUInt8==="function"}},{}],33:[function(require,module,exports){(function(process,global){var formatRegExp=/%[sdj%]/g;exports.format=function(f){if(!isString(f)){var objects=[];for(var i=0;i=len)return x;switch(x){case"%s":return String(args[i++]);case"%d":return Number(args[i++]);case"%j":try{return JSON.stringify(args[i++])}catch(_){return"[Circular]"}default:return x}});for(var x=args[i];i=3)ctx.depth=arguments[2];if(arguments.length>=4)ctx.colors=arguments[3];if(isBoolean(opts)){ctx.showHidden=opts}else if(opts){exports._extend(ctx,opts)}if(isUndefined(ctx.showHidden))ctx.showHidden=false;if(isUndefined(ctx.depth))ctx.depth=2;if(isUndefined(ctx.colors))ctx.colors=false;if(isUndefined(ctx.customInspect))ctx.customInspect=true;if(ctx.colors)ctx.stylize=stylizeWithColor;return formatValue(ctx,obj,ctx.depth)}exports.inspect=inspect;inspect.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]};inspect.styles={special:"cyan",number:"yellow",boolean:"yellow",undefined:"grey",null:"bold",string:"green",date:"magenta",regexp:"red"};function stylizeWithColor(str,styleType){var style=inspect.styles[styleType];if(style){return"["+inspect.colors[style][0]+"m"+str+"["+inspect.colors[style][1]+"m"}else{return str}}function stylizeNoColor(str,styleType){return str}function arrayToHash(array){var hash={};array.forEach(function(val,idx){hash[val]=true});return hash}function formatValue(ctx,value,recurseTimes){if(ctx.customInspect&&value&&isFunction(value.inspect)&&value.inspect!==exports.inspect&&!(value.constructor&&value.constructor.prototype===value)){var ret=value.inspect(recurseTimes,ctx);if(!isString(ret)){ret=formatValue(ctx,ret,recurseTimes)}return ret}var primitive=formatPrimitive(ctx,value);if(primitive){return primitive}var keys=Object.keys(value);var visibleKeys=arrayToHash(keys);if(ctx.showHidden){keys=Object.getOwnPropertyNames(value)}if(isError(value)&&(keys.indexOf("message")>=0||keys.indexOf("description")>=0)){return formatError(value)}if(keys.length===0){if(isFunction(value)){var name=value.name?": "+value.name:"";return ctx.stylize("[Function"+name+"]","special")}if(isRegExp(value)){return ctx.stylize(RegExp.prototype.toString.call(value),"regexp")}if(isDate(value)){return ctx.stylize(Date.prototype.toString.call(value),"date")}if(isError(value)){return formatError(value)}}var base="",array=false,braces=["{","}"];if(isArray(value)){array=true;braces=["[","]"]}if(isFunction(value)){var n=value.name?": "+value.name:"";base=" [Function"+n+"]"}if(isRegExp(value)){base=" "+RegExp.prototype.toString.call(value)}if(isDate(value)){base=" "+Date.prototype.toUTCString.call(value)}if(isError(value)){base=" "+formatError(value)}if(keys.length===0&&(!array||value.length==0)){return braces[0]+base+braces[1]}if(recurseTimes<0){if(isRegExp(value)){return ctx.stylize(RegExp.prototype.toString.call(value),"regexp")}else{return ctx.stylize("[Object]","special")}}ctx.seen.push(value);var output;if(array){output=formatArray(ctx,value,recurseTimes,visibleKeys,keys)}else{output=keys.map(function(key){return formatProperty(ctx,value,recurseTimes,visibleKeys,key,array)})}ctx.seen.pop();return reduceToSingleString(output,base,braces)}function formatPrimitive(ctx,value){if(isUndefined(value))return ctx.stylize("undefined","undefined");if(isString(value)){var simple="'"+JSON.stringify(value).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return ctx.stylize(simple,"string")}if(isNumber(value))return ctx.stylize(""+value,"number");if(isBoolean(value))return ctx.stylize(""+value,"boolean");if(isNull(value))return ctx.stylize("null","null")}function formatError(value){return"["+Error.prototype.toString.call(value)+"]"}function formatArray(ctx,value,recurseTimes,visibleKeys,keys){var output=[];for(var i=0,l=value.length;i-1){if(array){str=str.split("\n").map(function(line){return" "+line}).join("\n").substr(2)}else{str="\n"+str.split("\n").map(function(line){return" "+line}).join("\n")}}}else{str=ctx.stylize("[Circular]","special")}}if(isUndefined(name)){if(array&&key.match(/^\d+$/)){return str}name=JSON.stringify(""+key);if(name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)){name=name.substr(1,name.length-2);name=ctx.stylize(name,"name")}else{name=name.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'");name=ctx.stylize(name,"string")}}return name+": "+str}function reduceToSingleString(output,base,braces){var numLinesEst=0;var length=output.reduce(function(prev,cur){numLinesEst++;if(cur.indexOf("\n")>=0)numLinesEst++;return prev+cur.replace(/\u001b\[\d\d?m/g,"").length+1},0);if(length>60){return braces[0]+(base===""?"":base+"\n ")+" "+output.join(",\n ")+" "+braces[1]}return braces[0]+base+" "+output.join(", ")+" "+braces[1]}function isArray(ar){return Array.isArray(ar)}exports.isArray=isArray;function isBoolean(arg){return typeof arg==="boolean"}exports.isBoolean=isBoolean;function isNull(arg){return arg===null}exports.isNull=isNull;function isNullOrUndefined(arg){return arg==null}exports.isNullOrUndefined=isNullOrUndefined;function isNumber(arg){return typeof arg==="number"}exports.isNumber=isNumber;function isString(arg){return typeof arg==="string"}exports.isString=isString;function isSymbol(arg){return typeof arg==="symbol"}exports.isSymbol=isSymbol;function isUndefined(arg){return arg===void 0}exports.isUndefined=isUndefined;function isRegExp(re){return isObject(re)&&objectToString(re)==="[object RegExp]"}exports.isRegExp=isRegExp;function isObject(arg){return typeof arg==="object"&&arg!==null}exports.isObject=isObject;function isDate(d){return isObject(d)&&objectToString(d)==="[object Date]"}exports.isDate=isDate;function isError(e){return isObject(e)&&(objectToString(e)==="[object Error]"||e instanceof Error)}exports.isError=isError;function isFunction(arg){return typeof arg==="function"}exports.isFunction=isFunction;function isPrimitive(arg){return arg===null||typeof arg==="boolean"||typeof arg==="number"||typeof arg==="string"||typeof arg==="symbol"||typeof arg==="undefined"}exports.isPrimitive=isPrimitive;exports.isBuffer=require("./support/isBuffer");function objectToString(o){return Object.prototype.toString.call(o)}function pad(n){return n<10?"0"+n.toString(10):n.toString(10)}var months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];function timestamp(){var d=new Date;var time=[pad(d.getHours()),pad(d.getMinutes()),pad(d.getSeconds())].join(":");return[d.getDate(),months[d.getMonth()],time].join(" ")}exports.log=function(){console.log("%s - %s",timestamp(),exports.format.apply(exports,arguments))};exports.inherits=require("inherits");exports._extend=function(origin,add){if(!add||!isObject(add))return origin;var keys=Object.keys(add);var i=keys.length;while(i--){origin[keys[i]]=add[keys[i]]}return origin};function hasOwnProperty(obj,prop){return Object.prototype.hasOwnProperty.call(obj,prop)}}).call(this,require("_process"),typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./support/isBuffer":32,_process:17,inherits:31}]},{},[1])(1)}); \ No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js new file mode 100644 index 000000000..60581e8ea --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js @@ -0,0 +1,2443 @@ +/** + * @overview ASP.NET Core SignalR JavaScript Client. + * @version 1.0.0. + * @license + * Copyright (c) .NET Foundation. All rights reserved. + * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + */ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('msgpack5'), require('@aspnet/signalr')) : + typeof define === 'function' && define.amd ? define(['msgpack5', '@aspnet/signalr'], factory) : + (global.signalR = global.signalR || {}, global.signalR.protocols = global.signalR.protocols || {}, global.signalR.protocols.msgpack = factory(global.msgpack5,global.signalR)); +}(this, (function (msgpack5,signalr) { 'use strict'; + +msgpack5 = msgpack5 && msgpack5.hasOwnProperty('default') ? msgpack5['default'] : msgpack5; +signalr = signalr && signalr.hasOwnProperty('default') ? signalr['default'] : signalr; + +function unwrapExports (x) { + return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; +} + +function createCommonjsModule(fn, module) { + return module = { exports: {} }, fn(module, module.exports), module.exports; +} + +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + +var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + +function __extends(d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} + +var __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; +} + +function __decorate(decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +} + +function __param(paramIndex, decorator) { + return function (target, key) { decorator(target, key, paramIndex); } +} + +function __metadata(metadataKey, metadataValue) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); +} + +function __awaiter(thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +} + +function __generator(thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [0, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +} + +function __exportStar(m, exports) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} + +function __values(o) { + var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; + if (m) return m.call(o); + return { + next: function () { + if (o && i >= o.length) o = void 0; + return { value: o && o[i++], done: !o }; + } + }; +} + +function __read(o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +} + +function __spread() { + for (var ar = [], i = 0; i < arguments.length; i++) + ar = ar.concat(__read(arguments[i])); + return ar; +} + +function __await(v) { + return this instanceof __await ? (this.v = v, this) : new __await(v); +} + +function __asyncGenerator(thisArg, _arguments, generator) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var g = generator.apply(thisArg, _arguments || []), i, q = []; + return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; + function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } + function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } + function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } + function fulfill(value) { resume("next", value); } + function reject(value) { resume("throw", value); } + function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } +} + +function __asyncDelegator(o) { + var i, p; + return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; + function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; }; } +} + +function __asyncValues(o) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var m = o[Symbol.asyncIterator]; + return m ? m.call(o) : typeof __values === "function" ? __values(o) : o[Symbol.iterator](); +} + +function __makeTemplateObject(cooked, raw) { + if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } + return cooked; +} + +function __importStar(mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result.default = mod; + return result; +} + +function __importDefault(mod) { + return (mod && mod.__esModule) ? mod : { default: mod }; +} + + +var tslib_es6 = Object.freeze({ + __extends: __extends, + __assign: __assign, + __rest: __rest, + __decorate: __decorate, + __param: __param, + __metadata: __metadata, + __awaiter: __awaiter, + __generator: __generator, + __exportStar: __exportStar, + __values: __values, + __read: __read, + __spread: __spread, + __await: __await, + __asyncGenerator: __asyncGenerator, + __asyncDelegator: __asyncDelegator, + __asyncValues: __asyncValues, + __makeTemplateObject: __makeTemplateObject, + __importStar: __importStar, + __importDefault: __importDefault +}); + +var byteLength_1 = byteLength; +var toByteArray_1 = toByteArray; +var fromByteArray_1 = fromByteArray; + +var lookup = []; +var revLookup = []; +var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array; + +var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; +for (var i = 0, len = code.length; i < len; ++i) { + lookup[i] = code[i]; + revLookup[code.charCodeAt(i)] = i; +} + +revLookup['-'.charCodeAt(0)] = 62; +revLookup['_'.charCodeAt(0)] = 63; + +function placeHoldersCount (b64) { + var len = b64.length; + if (len % 4 > 0) { + throw new Error('Invalid string. Length must be a multiple of 4') + } + + // the number of equal signs (place holders) + // if there are two placeholders, than the two characters before it + // represent one byte + // if there is only one, then the three characters before it represent 2 bytes + // this is just a cheap hack to not do indexOf twice + return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0 +} + +function byteLength (b64) { + // base64 is 4/3 + up to two characters of the original data + return (b64.length * 3 / 4) - placeHoldersCount(b64) +} + +function toByteArray (b64) { + var i, l, tmp, placeHolders, arr; + var len = b64.length; + placeHolders = placeHoldersCount(b64); + + arr = new Arr((len * 3 / 4) - placeHolders); + + // if there are placeholders, only get up to the last complete 4 chars + l = placeHolders > 0 ? len - 4 : len; + + var L = 0; + + for (i = 0; i < l; i += 4) { + tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]; + arr[L++] = (tmp >> 16) & 0xFF; + arr[L++] = (tmp >> 8) & 0xFF; + arr[L++] = tmp & 0xFF; + } + + if (placeHolders === 2) { + tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4); + arr[L++] = tmp & 0xFF; + } else if (placeHolders === 1) { + tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2); + arr[L++] = (tmp >> 8) & 0xFF; + arr[L++] = tmp & 0xFF; + } + + return arr +} + +function tripletToBase64 (num) { + return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F] +} + +function encodeChunk (uint8, start, end) { + var tmp; + var output = []; + for (var i = start; i < end; i += 3) { + tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); + output.push(tripletToBase64(tmp)); + } + return output.join('') +} + +function fromByteArray (uint8) { + var tmp; + var len = uint8.length; + var extraBytes = len % 3; // if we have 1 byte left, pad 2 bytes + var output = ''; + var parts = []; + var maxChunkLength = 16383; // must be multiple of 3 + + // go through the array every three bytes, we'll deal with trailing stuff later + for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) { + parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength))); + } + + // pad the end with zeros, but make sure to not forget the extra bytes + if (extraBytes === 1) { + tmp = uint8[len - 1]; + output += lookup[tmp >> 2]; + output += lookup[(tmp << 4) & 0x3F]; + output += '=='; + } else if (extraBytes === 2) { + tmp = (uint8[len - 2] << 8) + (uint8[len - 1]); + output += lookup[tmp >> 10]; + output += lookup[(tmp >> 4) & 0x3F]; + output += lookup[(tmp << 2) & 0x3F]; + output += '='; + } + + parts.push(output); + + return parts.join('') +} + +var base64Js = { + byteLength: byteLength_1, + toByteArray: toByteArray_1, + fromByteArray: fromByteArray_1 +}; + +var read = function (buffer, offset, isLE, mLen, nBytes) { + var e, m; + var eLen = nBytes * 8 - mLen - 1; + var eMax = (1 << eLen) - 1; + var eBias = eMax >> 1; + var nBits = -7; + var i = isLE ? (nBytes - 1) : 0; + var d = isLE ? -1 : 1; + var s = buffer[offset + i]; + + i += d; + + e = s & ((1 << (-nBits)) - 1); + s >>= (-nBits); + nBits += eLen; + for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + m = e & ((1 << (-nBits)) - 1); + e >>= (-nBits); + nBits += mLen; + for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {} + + if (e === 0) { + e = 1 - eBias; + } else if (e === eMax) { + return m ? NaN : ((s ? -1 : 1) * Infinity) + } else { + m = m + Math.pow(2, mLen); + e = e - eBias; + } + return (s ? -1 : 1) * m * Math.pow(2, e - mLen) +}; + +var write = function (buffer, value, offset, isLE, mLen, nBytes) { + var e, m, c; + var eLen = nBytes * 8 - mLen - 1; + var eMax = (1 << eLen) - 1; + var eBias = eMax >> 1; + var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0); + var i = isLE ? 0 : (nBytes - 1); + var d = isLE ? 1 : -1; + var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0; + + value = Math.abs(value); + + if (isNaN(value) || value === Infinity) { + m = isNaN(value) ? 1 : 0; + e = eMax; + } else { + e = Math.floor(Math.log(value) / Math.LN2); + if (value * (c = Math.pow(2, -e)) < 1) { + e--; + c *= 2; + } + if (e + eBias >= 1) { + value += rt / c; + } else { + value += rt * Math.pow(2, 1 - eBias); + } + if (value * c >= 2) { + e++; + c /= 2; + } + + if (e + eBias >= eMax) { + m = 0; + e = eMax; + } else if (e + eBias >= 1) { + m = (value * c - 1) * Math.pow(2, mLen); + e = e + eBias; + } else { + m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen); + e = 0; + } + } + + for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {} + + e = (e << mLen) | m; + eLen += mLen; + for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {} + + buffer[offset + i - d] |= s * 128; +}; + +var ieee754 = { + read: read, + write: write +}; + +var buffer = createCommonjsModule(function (module, exports) { +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +/* eslint-disable no-proto */ + +exports.Buffer = Buffer; +exports.SlowBuffer = SlowBuffer; +exports.INSPECT_MAX_BYTES = 50; + +var K_MAX_LENGTH = 0x7fffffff; +exports.kMaxLength = K_MAX_LENGTH; + +/** + * If `Buffer.TYPED_ARRAY_SUPPORT`: + * === true Use Uint8Array implementation (fastest) + * === false Print warning and recommend using `buffer` v4.x which has an Object + * implementation (most compatible, even IE6) + * + * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+, + * Opera 11.6+, iOS 4.2+. + * + * We report that the browser does not support typed arrays if the are not subclassable + * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array` + * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support + * for __proto__ and has a buggy typed array implementation. + */ +Buffer.TYPED_ARRAY_SUPPORT = typedArraySupport(); + +if (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' && + typeof console.error === 'function') { + console.error( + 'This browser lacks typed array (Uint8Array) support which is required by ' + + '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.' + ); +} + +function typedArraySupport () { + // Can typed array instances can be augmented? + try { + var arr = new Uint8Array(1); + arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}; + return arr.foo() === 42 + } catch (e) { + return false + } +} + +function createBuffer (length) { + if (length > K_MAX_LENGTH) { + throw new RangeError('Invalid typed array length') + } + // Return an augmented `Uint8Array` instance + var buf = new Uint8Array(length); + buf.__proto__ = Buffer.prototype; + return buf +} + +/** + * The Buffer constructor returns instances of `Uint8Array` that have their + * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of + * `Uint8Array`, so the returned instances will have all the node `Buffer` methods + * and the `Uint8Array` methods. Square bracket notation works as expected -- it + * returns a single octet. + * + * The `Uint8Array` prototype remains unmodified. + */ + +function Buffer (arg, encodingOrOffset, length) { + // Common case. + if (typeof arg === 'number') { + if (typeof encodingOrOffset === 'string') { + throw new Error( + 'If encoding is specified then the first argument must be a string' + ) + } + return allocUnsafe(arg) + } + return from(arg, encodingOrOffset, length) +} + +// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 +if (typeof Symbol !== 'undefined' && Symbol.species && + Buffer[Symbol.species] === Buffer) { + Object.defineProperty(Buffer, Symbol.species, { + value: null, + configurable: true, + enumerable: false, + writable: false + }); +} + +Buffer.poolSize = 8192; // not used by this implementation + +function from (value, encodingOrOffset, length) { + if (typeof value === 'number') { + throw new TypeError('"value" argument must not be a number') + } + + if (isArrayBuffer(value)) { + return fromArrayBuffer(value, encodingOrOffset, length) + } + + if (typeof value === 'string') { + return fromString(value, encodingOrOffset) + } + + return fromObject(value) +} + +/** + * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError + * if value is a number. + * Buffer.from(str[, encoding]) + * Buffer.from(array) + * Buffer.from(buffer) + * Buffer.from(arrayBuffer[, byteOffset[, length]]) + **/ +Buffer.from = function (value, encodingOrOffset, length) { + return from(value, encodingOrOffset, length) +}; + +// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug: +// https://github.com/feross/buffer/pull/148 +Buffer.prototype.__proto__ = Uint8Array.prototype; +Buffer.__proto__ = Uint8Array; + +function assertSize (size) { + if (typeof size !== 'number') { + throw new TypeError('"size" argument must be a number') + } else if (size < 0) { + throw new RangeError('"size" argument must not be negative') + } +} + +function alloc (size, fill, encoding) { + assertSize(size); + if (size <= 0) { + return createBuffer(size) + } + if (fill !== undefined) { + // Only pay attention to encoding if it's a string. This + // prevents accidentally sending in a number that would + // be interpretted as a start offset. + return typeof encoding === 'string' + ? createBuffer(size).fill(fill, encoding) + : createBuffer(size).fill(fill) + } + return createBuffer(size) +} + +/** + * Creates a new filled Buffer instance. + * alloc(size[, fill[, encoding]]) + **/ +Buffer.alloc = function (size, fill, encoding) { + return alloc(size, fill, encoding) +}; + +function allocUnsafe (size) { + assertSize(size); + return createBuffer(size < 0 ? 0 : checked(size) | 0) +} + +/** + * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. + * */ +Buffer.allocUnsafe = function (size) { + return allocUnsafe(size) +}; +/** + * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. + */ +Buffer.allocUnsafeSlow = function (size) { + return allocUnsafe(size) +}; + +function fromString (string, encoding) { + if (typeof encoding !== 'string' || encoding === '') { + encoding = 'utf8'; + } + + if (!Buffer.isEncoding(encoding)) { + throw new TypeError('"encoding" must be a valid string encoding') + } + + var length = byteLength(string, encoding) | 0; + var buf = createBuffer(length); + + var actual = buf.write(string, encoding); + + if (actual !== length) { + // Writing a hex string, for example, that contains invalid characters will + // cause everything after the first invalid character to be ignored. (e.g. + // 'abxxcd' will be treated as 'ab') + buf = buf.slice(0, actual); + } + + return buf +} + +function fromArrayLike (array) { + var length = array.length < 0 ? 0 : checked(array.length) | 0; + var buf = createBuffer(length); + for (var i = 0; i < length; i += 1) { + buf[i] = array[i] & 255; + } + return buf +} + +function fromArrayBuffer (array, byteOffset, length) { + if (byteOffset < 0 || array.byteLength < byteOffset) { + throw new RangeError('\'offset\' is out of bounds') + } + + if (array.byteLength < byteOffset + (length || 0)) { + throw new RangeError('\'length\' is out of bounds') + } + + var buf; + if (byteOffset === undefined && length === undefined) { + buf = new Uint8Array(array); + } else if (length === undefined) { + buf = new Uint8Array(array, byteOffset); + } else { + buf = new Uint8Array(array, byteOffset, length); + } + + // Return an augmented `Uint8Array` instance + buf.__proto__ = Buffer.prototype; + return buf +} + +function fromObject (obj) { + if (Buffer.isBuffer(obj)) { + var len = checked(obj.length) | 0; + var buf = createBuffer(len); + + if (buf.length === 0) { + return buf + } + + obj.copy(buf, 0, 0, len); + return buf + } + + if (obj) { + if (isArrayBufferView(obj) || 'length' in obj) { + if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) { + return createBuffer(0) + } + return fromArrayLike(obj) + } + + if (obj.type === 'Buffer' && Array.isArray(obj.data)) { + return fromArrayLike(obj.data) + } + } + + throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') +} + +function checked (length) { + // Note: cannot use `length < K_MAX_LENGTH` here because that fails when + // length is NaN (which is otherwise coerced to zero.) + if (length >= K_MAX_LENGTH) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes') + } + return length | 0 +} + +function SlowBuffer (length) { + if (+length != length) { // eslint-disable-line eqeqeq + length = 0; + } + return Buffer.alloc(+length) +} + +Buffer.isBuffer = function isBuffer (b) { + return b != null && b._isBuffer === true +}; + +Buffer.compare = function compare (a, b) { + if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) { + throw new TypeError('Arguments must be Buffers') + } + + if (a === b) return 0 + + var x = a.length; + var y = b.length; + + for (var i = 0, len = Math.min(x, y); i < len; ++i) { + if (a[i] !== b[i]) { + x = a[i]; + y = b[i]; + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +}; + +Buffer.isEncoding = function isEncoding (encoding) { + switch (String(encoding).toLowerCase()) { + case 'hex': + case 'utf8': + case 'utf-8': + case 'ascii': + case 'latin1': + case 'binary': + case 'base64': + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return true + default: + return false + } +}; + +Buffer.concat = function concat (list, length) { + if (!Array.isArray(list)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + + if (list.length === 0) { + return Buffer.alloc(0) + } + + var i; + if (length === undefined) { + length = 0; + for (i = 0; i < list.length; ++i) { + length += list[i].length; + } + } + + var buffer = Buffer.allocUnsafe(length); + var pos = 0; + for (i = 0; i < list.length; ++i) { + var buf = list[i]; + if (!Buffer.isBuffer(buf)) { + throw new TypeError('"list" argument must be an Array of Buffers') + } + buf.copy(buffer, pos); + pos += buf.length; + } + return buffer +}; + +function byteLength (string, encoding) { + if (Buffer.isBuffer(string)) { + return string.length + } + if (isArrayBufferView(string) || isArrayBuffer(string)) { + return string.byteLength + } + if (typeof string !== 'string') { + string = '' + string; + } + + var len = string.length; + if (len === 0) return 0 + + // Use a for loop to avoid recursion + var loweredCase = false; + for (;;) { + switch (encoding) { + case 'ascii': + case 'latin1': + case 'binary': + return len + case 'utf8': + case 'utf-8': + case undefined: + return utf8ToBytes(string).length + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return len * 2 + case 'hex': + return len >>> 1 + case 'base64': + return base64ToBytes(string).length + default: + if (loweredCase) return utf8ToBytes(string).length // assume utf8 + encoding = ('' + encoding).toLowerCase(); + loweredCase = true; + } + } +} +Buffer.byteLength = byteLength; + +function slowToString (encoding, start, end) { + var loweredCase = false; + + // No need to verify that "this.length <= MAX_UINT32" since it's a read-only + // property of a typed array. + + // This behaves neither like String nor Uint8Array in that we set start/end + // to their upper/lower bounds if the value passed is out of range. + // undefined is handled specially as per ECMA-262 6th Edition, + // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. + if (start === undefined || start < 0) { + start = 0; + } + // Return early if start > this.length. Done here to prevent potential uint32 + // coercion fail below. + if (start > this.length) { + return '' + } + + if (end === undefined || end > this.length) { + end = this.length; + } + + if (end <= 0) { + return '' + } + + // Force coersion to uint32. This will also coerce falsey/NaN values to 0. + end >>>= 0; + start >>>= 0; + + if (end <= start) { + return '' + } + + if (!encoding) encoding = 'utf8'; + + while (true) { + switch (encoding) { + case 'hex': + return hexSlice(this, start, end) + + case 'utf8': + case 'utf-8': + return utf8Slice(this, start, end) + + case 'ascii': + return asciiSlice(this, start, end) + + case 'latin1': + case 'binary': + return latin1Slice(this, start, end) + + case 'base64': + return base64Slice(this, start, end) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return utf16leSlice(this, start, end) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = (encoding + '').toLowerCase(); + loweredCase = true; + } + } +} + +// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package) +// to detect a Buffer instance. It's not possible to use `instanceof Buffer` +// reliably in a browserify context because there could be multiple different +// copies of the 'buffer' package in use. This method works even for Buffer +// instances that were created from another copy of the `buffer` package. +// See: https://github.com/feross/buffer/issues/154 +Buffer.prototype._isBuffer = true; + +function swap (b, n, m) { + var i = b[n]; + b[n] = b[m]; + b[m] = i; +} + +Buffer.prototype.swap16 = function swap16 () { + var len = this.length; + if (len % 2 !== 0) { + throw new RangeError('Buffer size must be a multiple of 16-bits') + } + for (var i = 0; i < len; i += 2) { + swap(this, i, i + 1); + } + return this +}; + +Buffer.prototype.swap32 = function swap32 () { + var len = this.length; + if (len % 4 !== 0) { + throw new RangeError('Buffer size must be a multiple of 32-bits') + } + for (var i = 0; i < len; i += 4) { + swap(this, i, i + 3); + swap(this, i + 1, i + 2); + } + return this +}; + +Buffer.prototype.swap64 = function swap64 () { + var len = this.length; + if (len % 8 !== 0) { + throw new RangeError('Buffer size must be a multiple of 64-bits') + } + for (var i = 0; i < len; i += 8) { + swap(this, i, i + 7); + swap(this, i + 1, i + 6); + swap(this, i + 2, i + 5); + swap(this, i + 3, i + 4); + } + return this +}; + +Buffer.prototype.toString = function toString () { + var length = this.length; + if (length === 0) return '' + if (arguments.length === 0) return utf8Slice(this, 0, length) + return slowToString.apply(this, arguments) +}; + +Buffer.prototype.equals = function equals (b) { + if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') + if (this === b) return true + return Buffer.compare(this, b) === 0 +}; + +Buffer.prototype.inspect = function inspect () { + var str = ''; + var max = exports.INSPECT_MAX_BYTES; + if (this.length > 0) { + str = this.toString('hex', 0, max).match(/.{2}/g).join(' '); + if (this.length > max) str += ' ... '; + } + return '' +}; + +Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { + if (!Buffer.isBuffer(target)) { + throw new TypeError('Argument must be a Buffer') + } + + if (start === undefined) { + start = 0; + } + if (end === undefined) { + end = target ? target.length : 0; + } + if (thisStart === undefined) { + thisStart = 0; + } + if (thisEnd === undefined) { + thisEnd = this.length; + } + + if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { + throw new RangeError('out of range index') + } + + if (thisStart >= thisEnd && start >= end) { + return 0 + } + if (thisStart >= thisEnd) { + return -1 + } + if (start >= end) { + return 1 + } + + start >>>= 0; + end >>>= 0; + thisStart >>>= 0; + thisEnd >>>= 0; + + if (this === target) return 0 + + var x = thisEnd - thisStart; + var y = end - start; + var len = Math.min(x, y); + + var thisCopy = this.slice(thisStart, thisEnd); + var targetCopy = target.slice(start, end); + + for (var i = 0; i < len; ++i) { + if (thisCopy[i] !== targetCopy[i]) { + x = thisCopy[i]; + y = targetCopy[i]; + break + } + } + + if (x < y) return -1 + if (y < x) return 1 + return 0 +}; + +// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`, +// OR the last index of `val` in `buffer` at offset <= `byteOffset`. +// +// Arguments: +// - buffer - a Buffer to search +// - val - a string, Buffer, or number +// - byteOffset - an index into `buffer`; will be clamped to an int32 +// - encoding - an optional encoding, relevant is val is a string +// - dir - true for indexOf, false for lastIndexOf +function bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) { + // Empty buffer means no match + if (buffer.length === 0) return -1 + + // Normalize byteOffset + if (typeof byteOffset === 'string') { + encoding = byteOffset; + byteOffset = 0; + } else if (byteOffset > 0x7fffffff) { + byteOffset = 0x7fffffff; + } else if (byteOffset < -0x80000000) { + byteOffset = -0x80000000; + } + byteOffset = +byteOffset; // Coerce to Number. + if (numberIsNaN(byteOffset)) { + // byteOffset: it it's undefined, null, NaN, "foo", etc, search whole buffer + byteOffset = dir ? 0 : (buffer.length - 1); + } + + // Normalize byteOffset: negative offsets start from the end of the buffer + if (byteOffset < 0) byteOffset = buffer.length + byteOffset; + if (byteOffset >= buffer.length) { + if (dir) return -1 + else byteOffset = buffer.length - 1; + } else if (byteOffset < 0) { + if (dir) byteOffset = 0; + else return -1 + } + + // Normalize val + if (typeof val === 'string') { + val = Buffer.from(val, encoding); + } + + // Finally, search either indexOf (if dir is true) or lastIndexOf + if (Buffer.isBuffer(val)) { + // Special case: looking for empty string/buffer always fails + if (val.length === 0) { + return -1 + } + return arrayIndexOf(buffer, val, byteOffset, encoding, dir) + } else if (typeof val === 'number') { + val = val & 0xFF; // Search for a byte value [0-255] + if (typeof Uint8Array.prototype.indexOf === 'function') { + if (dir) { + return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset) + } else { + return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset) + } + } + return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir) + } + + throw new TypeError('val must be string, number or Buffer') +} + +function arrayIndexOf (arr, val, byteOffset, encoding, dir) { + var indexSize = 1; + var arrLength = arr.length; + var valLength = val.length; + + if (encoding !== undefined) { + encoding = String(encoding).toLowerCase(); + if (encoding === 'ucs2' || encoding === 'ucs-2' || + encoding === 'utf16le' || encoding === 'utf-16le') { + if (arr.length < 2 || val.length < 2) { + return -1 + } + indexSize = 2; + arrLength /= 2; + valLength /= 2; + byteOffset /= 2; + } + } + + function read (buf, i) { + if (indexSize === 1) { + return buf[i] + } else { + return buf.readUInt16BE(i * indexSize) + } + } + + var i; + if (dir) { + var foundIndex = -1; + for (i = byteOffset; i < arrLength; i++) { + if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { + if (foundIndex === -1) foundIndex = i; + if (i - foundIndex + 1 === valLength) return foundIndex * indexSize + } else { + if (foundIndex !== -1) i -= i - foundIndex; + foundIndex = -1; + } + } + } else { + if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength; + for (i = byteOffset; i >= 0; i--) { + var found = true; + for (var j = 0; j < valLength; j++) { + if (read(arr, i + j) !== read(val, j)) { + found = false; + break + } + } + if (found) return i + } + } + + return -1 +} + +Buffer.prototype.includes = function includes (val, byteOffset, encoding) { + return this.indexOf(val, byteOffset, encoding) !== -1 +}; + +Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, true) +}; + +Buffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) { + return bidirectionalIndexOf(this, val, byteOffset, encoding, false) +}; + +function hexWrite (buf, string, offset, length) { + offset = Number(offset) || 0; + var remaining = buf.length - offset; + if (!length) { + length = remaining; + } else { + length = Number(length); + if (length > remaining) { + length = remaining; + } + } + + // must be an even number of digits + var strLen = string.length; + if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') + + if (length > strLen / 2) { + length = strLen / 2; + } + for (var i = 0; i < length; ++i) { + var parsed = parseInt(string.substr(i * 2, 2), 16); + if (numberIsNaN(parsed)) return i + buf[offset + i] = parsed; + } + return i +} + +function utf8Write (buf, string, offset, length) { + return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) +} + +function asciiWrite (buf, string, offset, length) { + return blitBuffer(asciiToBytes(string), buf, offset, length) +} + +function latin1Write (buf, string, offset, length) { + return asciiWrite(buf, string, offset, length) +} + +function base64Write (buf, string, offset, length) { + return blitBuffer(base64ToBytes(string), buf, offset, length) +} + +function ucs2Write (buf, string, offset, length) { + return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) +} + +Buffer.prototype.write = function write (string, offset, length, encoding) { + // Buffer#write(string) + if (offset === undefined) { + encoding = 'utf8'; + length = this.length; + offset = 0; + // Buffer#write(string, encoding) + } else if (length === undefined && typeof offset === 'string') { + encoding = offset; + length = this.length; + offset = 0; + // Buffer#write(string, offset[, length][, encoding]) + } else if (isFinite(offset)) { + offset = offset >>> 0; + if (isFinite(length)) { + length = length >>> 0; + if (encoding === undefined) encoding = 'utf8'; + } else { + encoding = length; + length = undefined; + } + } else { + throw new Error( + 'Buffer.write(string, encoding, offset[, length]) is no longer supported' + ) + } + + var remaining = this.length - offset; + if (length === undefined || length > remaining) length = remaining; + + if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { + throw new RangeError('Attempt to write outside buffer bounds') + } + + if (!encoding) encoding = 'utf8'; + + var loweredCase = false; + for (;;) { + switch (encoding) { + case 'hex': + return hexWrite(this, string, offset, length) + + case 'utf8': + case 'utf-8': + return utf8Write(this, string, offset, length) + + case 'ascii': + return asciiWrite(this, string, offset, length) + + case 'latin1': + case 'binary': + return latin1Write(this, string, offset, length) + + case 'base64': + // Warning: maxLength not taken into account in base64Write + return base64Write(this, string, offset, length) + + case 'ucs2': + case 'ucs-2': + case 'utf16le': + case 'utf-16le': + return ucs2Write(this, string, offset, length) + + default: + if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) + encoding = ('' + encoding).toLowerCase(); + loweredCase = true; + } + } +}; + +Buffer.prototype.toJSON = function toJSON () { + return { + type: 'Buffer', + data: Array.prototype.slice.call(this._arr || this, 0) + } +}; + +function base64Slice (buf, start, end) { + if (start === 0 && end === buf.length) { + return base64Js.fromByteArray(buf) + } else { + return base64Js.fromByteArray(buf.slice(start, end)) + } +} + +function utf8Slice (buf, start, end) { + end = Math.min(buf.length, end); + var res = []; + + var i = start; + while (i < end) { + var firstByte = buf[i]; + var codePoint = null; + var bytesPerSequence = (firstByte > 0xEF) ? 4 + : (firstByte > 0xDF) ? 3 + : (firstByte > 0xBF) ? 2 + : 1; + + if (i + bytesPerSequence <= end) { + var secondByte, thirdByte, fourthByte, tempCodePoint; + + switch (bytesPerSequence) { + case 1: + if (firstByte < 0x80) { + codePoint = firstByte; + } + break + case 2: + secondByte = buf[i + 1]; + if ((secondByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F); + if (tempCodePoint > 0x7F) { + codePoint = tempCodePoint; + } + } + break + case 3: + secondByte = buf[i + 1]; + thirdByte = buf[i + 2]; + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F); + if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { + codePoint = tempCodePoint; + } + } + break + case 4: + secondByte = buf[i + 1]; + thirdByte = buf[i + 2]; + fourthByte = buf[i + 3]; + if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { + tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F); + if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { + codePoint = tempCodePoint; + } + } + } + } + + if (codePoint === null) { + // we did not generate a valid codePoint so insert a + // replacement char (U+FFFD) and advance only 1 byte + codePoint = 0xFFFD; + bytesPerSequence = 1; + } else if (codePoint > 0xFFFF) { + // encode to utf16 (surrogate pair dance) + codePoint -= 0x10000; + res.push(codePoint >>> 10 & 0x3FF | 0xD800); + codePoint = 0xDC00 | codePoint & 0x3FF; + } + + res.push(codePoint); + i += bytesPerSequence; + } + + return decodeCodePointsArray(res) +} + +// Based on http://stackoverflow.com/a/22747272/680742, the browser with +// the lowest limit is Chrome, with 0x10000 args. +// We go 1 magnitude less, for safety +var MAX_ARGUMENTS_LENGTH = 0x1000; + +function decodeCodePointsArray (codePoints) { + var len = codePoints.length; + if (len <= MAX_ARGUMENTS_LENGTH) { + return String.fromCharCode.apply(String, codePoints) // avoid extra slice() + } + + // Decode in chunks to avoid "call stack size exceeded". + var res = ''; + var i = 0; + while (i < len) { + res += String.fromCharCode.apply( + String, + codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH) + ); + } + return res +} + +function asciiSlice (buf, start, end) { + var ret = ''; + end = Math.min(buf.length, end); + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i] & 0x7F); + } + return ret +} + +function latin1Slice (buf, start, end) { + var ret = ''; + end = Math.min(buf.length, end); + + for (var i = start; i < end; ++i) { + ret += String.fromCharCode(buf[i]); + } + return ret +} + +function hexSlice (buf, start, end) { + var len = buf.length; + + if (!start || start < 0) start = 0; + if (!end || end < 0 || end > len) end = len; + + var out = ''; + for (var i = start; i < end; ++i) { + out += toHex(buf[i]); + } + return out +} + +function utf16leSlice (buf, start, end) { + var bytes = buf.slice(start, end); + var res = ''; + for (var i = 0; i < bytes.length; i += 2) { + res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256)); + } + return res +} + +Buffer.prototype.slice = function slice (start, end) { + var len = this.length; + start = ~~start; + end = end === undefined ? len : ~~end; + + if (start < 0) { + start += len; + if (start < 0) start = 0; + } else if (start > len) { + start = len; + } + + if (end < 0) { + end += len; + if (end < 0) end = 0; + } else if (end > len) { + end = len; + } + + if (end < start) end = start; + + var newBuf = this.subarray(start, end); + // Return an augmented `Uint8Array` instance + newBuf.__proto__ = Buffer.prototype; + return newBuf +}; + +/* + * Need to make sure that buffer isn't trying to write out of bounds. + */ +function checkOffset (offset, ext, length) { + if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint') + if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length') +} + +Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var val = this[offset]; + var mul = 1; + var i = 0; + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul; + } + + return val +}; + +Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + checkOffset(offset, byteLength, this.length); + } + + var val = this[offset + --byteLength]; + var mul = 1; + while (byteLength > 0 && (mul *= 0x100)) { + val += this[offset + --byteLength] * mul; + } + + return val +}; + +Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 1, this.length); + return this[offset] +}; + +Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + return this[offset] | (this[offset + 1] << 8) +}; + +Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + return (this[offset] << 8) | this[offset + 1] +}; + +Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return ((this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16)) + + (this[offset + 3] * 0x1000000) +}; + +Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset] * 0x1000000) + + ((this[offset + 1] << 16) | + (this[offset + 2] << 8) | + this[offset + 3]) +}; + +Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var val = this[offset]; + var mul = 1; + var i = 0; + while (++i < byteLength && (mul *= 0x100)) { + val += this[offset + i] * mul; + } + mul *= 0x80; + + if (val >= mul) val -= Math.pow(2, 8 * byteLength); + + return val +}; + +Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) { + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) checkOffset(offset, byteLength, this.length); + + var i = byteLength; + var mul = 1; + var val = this[offset + --i]; + while (i > 0 && (mul *= 0x100)) { + val += this[offset + --i] * mul; + } + mul *= 0x80; + + if (val >= mul) val -= Math.pow(2, 8 * byteLength); + + return val +}; + +Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 1, this.length); + if (!(this[offset] & 0x80)) return (this[offset]) + return ((0xff - this[offset] + 1) * -1) +}; + +Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + var val = this[offset] | (this[offset + 1] << 8); + return (val & 0x8000) ? val | 0xFFFF0000 : val +}; + +Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 2, this.length); + var val = this[offset + 1] | (this[offset] << 8); + return (val & 0x8000) ? val | 0xFFFF0000 : val +}; + +Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset]) | + (this[offset + 1] << 8) | + (this[offset + 2] << 16) | + (this[offset + 3] << 24) +}; + +Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + + return (this[offset] << 24) | + (this[offset + 1] << 16) | + (this[offset + 2] << 8) | + (this[offset + 3]) +}; + +Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + return ieee754.read(this, offset, true, 23, 4) +}; + +Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 4, this.length); + return ieee754.read(this, offset, false, 23, 4) +}; + +Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 8, this.length); + return ieee754.read(this, offset, true, 52, 8) +}; + +Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) { + offset = offset >>> 0; + if (!noAssert) checkOffset(offset, 8, this.length); + return ieee754.read(this, offset, false, 52, 8) +}; + +function checkInt (buf, value, offset, ext, max, min) { + if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') + if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') + if (offset + ext > buf.length) throw new RangeError('Index out of range') +} + +Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(this, value, offset, byteLength, maxBytes, 0); + } + + var mul = 1; + var i = 0; + this[offset] = value & 0xFF; + while (++i < byteLength && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF; + } + + return offset + byteLength +}; + +Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + var maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(this, value, offset, byteLength, maxBytes, 0); + } + + var i = byteLength - 1; + var mul = 1; + this[offset + i] = value & 0xFF; + while (--i >= 0 && (mul *= 0x100)) { + this[offset + i] = (value / mul) & 0xFF; + } + + return offset + byteLength +}; + +Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0); + this[offset] = (value & 0xff); + return offset + 1 +}; + +Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + return offset + 2 +}; + +Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0); + this[offset] = (value >>> 8); + this[offset + 1] = (value & 0xff); + return offset + 2 +}; + +Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0); + this[offset + 3] = (value >>> 24); + this[offset + 2] = (value >>> 16); + this[offset + 1] = (value >>> 8); + this[offset] = (value & 0xff); + return offset + 4 +}; + +Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0); + this[offset] = (value >>> 24); + this[offset + 1] = (value >>> 16); + this[offset + 2] = (value >>> 8); + this[offset + 3] = (value & 0xff); + return offset + 4 +}; + +Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1); + + checkInt(this, value, offset, byteLength, limit - 1, -limit); + } + + var i = 0; + var mul = 1; + var sub = 0; + this[offset] = value & 0xFF; + while (++i < byteLength && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { + sub = 1; + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF; + } + + return offset + byteLength +}; + +Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + var limit = Math.pow(2, (8 * byteLength) - 1); + + checkInt(this, value, offset, byteLength, limit - 1, -limit); + } + + var i = byteLength - 1; + var mul = 1; + var sub = 0; + this[offset + i] = value & 0xFF; + while (--i >= 0 && (mul *= 0x100)) { + if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { + sub = 1; + } + this[offset + i] = ((value / mul) >> 0) - sub & 0xFF; + } + + return offset + byteLength +}; + +Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80); + if (value < 0) value = 0xff + value + 1; + this[offset] = (value & 0xff); + return offset + 1 +}; + +Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + return offset + 2 +}; + +Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000); + this[offset] = (value >>> 8); + this[offset + 1] = (value & 0xff); + return offset + 2 +}; + +Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000); + this[offset] = (value & 0xff); + this[offset + 1] = (value >>> 8); + this[offset + 2] = (value >>> 16); + this[offset + 3] = (value >>> 24); + return offset + 4 +}; + +Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000); + if (value < 0) value = 0xffffffff + value + 1; + this[offset] = (value >>> 24); + this[offset + 1] = (value >>> 16); + this[offset + 2] = (value >>> 8); + this[offset + 3] = (value & 0xff); + return offset + 4 +}; + +function checkIEEE754 (buf, value, offset, ext, max, min) { + if (offset + ext > buf.length) throw new RangeError('Index out of range') + if (offset < 0) throw new RangeError('Index out of range') +} + +function writeFloat (buf, value, offset, littleEndian, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38); + } + ieee754.write(buf, value, offset, littleEndian, 23, 4); + return offset + 4 +} + +Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) { + return writeFloat(this, value, offset, true, noAssert) +}; + +Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) { + return writeFloat(this, value, offset, false, noAssert) +}; + +function writeDouble (buf, value, offset, littleEndian, noAssert) { + value = +value; + offset = offset >>> 0; + if (!noAssert) { + checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308); + } + ieee754.write(buf, value, offset, littleEndian, 52, 8); + return offset + 8 +} + +Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) { + return writeDouble(this, value, offset, true, noAssert) +}; + +Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) { + return writeDouble(this, value, offset, false, noAssert) +}; + +// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length) +Buffer.prototype.copy = function copy (target, targetStart, start, end) { + if (!start) start = 0; + if (!end && end !== 0) end = this.length; + if (targetStart >= target.length) targetStart = target.length; + if (!targetStart) targetStart = 0; + if (end > 0 && end < start) end = start; + + // Copy 0 bytes; we're done + if (end === start) return 0 + if (target.length === 0 || this.length === 0) return 0 + + // Fatal error conditions + if (targetStart < 0) { + throw new RangeError('targetStart out of bounds') + } + if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds') + if (end < 0) throw new RangeError('sourceEnd out of bounds') + + // Are we oob? + if (end > this.length) end = this.length; + if (target.length - targetStart < end - start) { + end = target.length - targetStart + start; + } + + var len = end - start; + var i; + + if (this === target && start < targetStart && targetStart < end) { + // descending copy from end + for (i = len - 1; i >= 0; --i) { + target[i + targetStart] = this[i + start]; + } + } else if (len < 1000) { + // ascending copy from start + for (i = 0; i < len; ++i) { + target[i + targetStart] = this[i + start]; + } + } else { + Uint8Array.prototype.set.call( + target, + this.subarray(start, start + len), + targetStart + ); + } + + return len +}; + +// Usage: +// buffer.fill(number[, offset[, end]]) +// buffer.fill(buffer[, offset[, end]]) +// buffer.fill(string[, offset[, end]][, encoding]) +Buffer.prototype.fill = function fill (val, start, end, encoding) { + // Handle string cases: + if (typeof val === 'string') { + if (typeof start === 'string') { + encoding = start; + start = 0; + end = this.length; + } else if (typeof end === 'string') { + encoding = end; + end = this.length; + } + if (val.length === 1) { + var code = val.charCodeAt(0); + if (code < 256) { + val = code; + } + } + if (encoding !== undefined && typeof encoding !== 'string') { + throw new TypeError('encoding must be a string') + } + if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { + throw new TypeError('Unknown encoding: ' + encoding) + } + } else if (typeof val === 'number') { + val = val & 255; + } + + // Invalid ranges are not set to a default, so can range check early. + if (start < 0 || this.length < start || this.length < end) { + throw new RangeError('Out of range index') + } + + if (end <= start) { + return this + } + + start = start >>> 0; + end = end === undefined ? this.length : end >>> 0; + + if (!val) val = 0; + + var i; + if (typeof val === 'number') { + for (i = start; i < end; ++i) { + this[i] = val; + } + } else { + var bytes = Buffer.isBuffer(val) + ? val + : new Buffer(val, encoding); + var len = bytes.length; + for (i = 0; i < end - start; ++i) { + this[i + start] = bytes[i % len]; + } + } + + return this +}; + +// HELPER FUNCTIONS +// ================ + +var INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g; + +function base64clean (str) { + // Node strips out invalid characters like \n and \t from the string, base64-js does not + str = str.trim().replace(INVALID_BASE64_RE, ''); + // Node converts strings with length < 2 to '' + if (str.length < 2) return '' + // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not + while (str.length % 4 !== 0) { + str = str + '='; + } + return str +} + +function toHex (n) { + if (n < 16) return '0' + n.toString(16) + return n.toString(16) +} + +function utf8ToBytes (string, units) { + units = units || Infinity; + var codePoint; + var length = string.length; + var leadSurrogate = null; + var bytes = []; + + for (var i = 0; i < length; ++i) { + codePoint = string.charCodeAt(i); + + // is surrogate component + if (codePoint > 0xD7FF && codePoint < 0xE000) { + // last char was a lead + if (!leadSurrogate) { + // no lead yet + if (codePoint > 0xDBFF) { + // unexpected trail + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + continue + } else if (i + 1 === length) { + // unpaired lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + continue + } + + // valid lead + leadSurrogate = codePoint; + + continue + } + + // 2 leads in a row + if (codePoint < 0xDC00) { + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + leadSurrogate = codePoint; + continue + } + + // valid surrogate pair + codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000; + } else if (leadSurrogate) { + // valid bmp char, but last char was a lead + if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD); + } + + leadSurrogate = null; + + // encode utf8 + if (codePoint < 0x80) { + if ((units -= 1) < 0) break + bytes.push(codePoint); + } else if (codePoint < 0x800) { + if ((units -= 2) < 0) break + bytes.push( + codePoint >> 0x6 | 0xC0, + codePoint & 0x3F | 0x80 + ); + } else if (codePoint < 0x10000) { + if ((units -= 3) < 0) break + bytes.push( + codePoint >> 0xC | 0xE0, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ); + } else if (codePoint < 0x110000) { + if ((units -= 4) < 0) break + bytes.push( + codePoint >> 0x12 | 0xF0, + codePoint >> 0xC & 0x3F | 0x80, + codePoint >> 0x6 & 0x3F | 0x80, + codePoint & 0x3F | 0x80 + ); + } else { + throw new Error('Invalid code point') + } + } + + return bytes +} + +function asciiToBytes (str) { + var byteArray = []; + for (var i = 0; i < str.length; ++i) { + // Node's code seems to be doing this and not & 0x7F.. + byteArray.push(str.charCodeAt(i) & 0xFF); + } + return byteArray +} + +function utf16leToBytes (str, units) { + var c, hi, lo; + var byteArray = []; + for (var i = 0; i < str.length; ++i) { + if ((units -= 2) < 0) break + + c = str.charCodeAt(i); + hi = c >> 8; + lo = c % 256; + byteArray.push(lo); + byteArray.push(hi); + } + + return byteArray +} + +function base64ToBytes (str) { + return base64Js.toByteArray(base64clean(str)) +} + +function blitBuffer (src, dst, offset, length) { + for (var i = 0; i < length; ++i) { + if ((i + offset >= dst.length) || (i >= src.length)) break + dst[i + offset] = src[i]; + } + return i +} + +// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check +// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166 +function isArrayBuffer (obj) { + return obj instanceof ArrayBuffer || + (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' && + typeof obj.byteLength === 'number') +} + +// Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView` +function isArrayBufferView (obj) { + return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj) +} + +function numberIsNaN (obj) { + return obj !== obj // eslint-disable-line no-self-compare +} +}); + +var buffer_1 = buffer.Buffer; +var buffer_2 = buffer.SlowBuffer; +var buffer_3 = buffer.INSPECT_MAX_BYTES; +var buffer_4 = buffer.kMaxLength; + +var BinaryMessageFormat_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// Not exported from index. +var BinaryMessageFormat = /** @class */ (function () { + function BinaryMessageFormat() { + } + // The length prefix of binary messages is encoded as VarInt. Read the comment in + // the BinaryMessageParser.TryParseMessage for details. + BinaryMessageFormat.write = function (output) { + // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser + // in which case .byteLength does will be undefined + var size = output.byteLength || output.length; + var lenBuffer = []; + do { + var sizePart = size & 0x7f; + size = size >> 7; + if (size > 0) { + sizePart |= 0x80; + } + lenBuffer.push(sizePart); + } while (size > 0); + // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser + // in which case .byteLength does will be undefined + size = output.byteLength || output.length; + var buffer = new Uint8Array(lenBuffer.length + size); + buffer.set(lenBuffer, 0); + buffer.set(output, lenBuffer.length); + return buffer.buffer; + }; + BinaryMessageFormat.parse = function (input) { + var result = []; + var uint8Array = new Uint8Array(input); + var maxLengthPrefixSize = 5; + var numBitsToShift = [0, 7, 14, 21, 28]; + for (var offset = 0; offset < input.byteLength;) { + var numBytes = 0; + var size = 0; + var byteRead = void 0; + do { + byteRead = uint8Array[offset + numBytes]; + size = size | ((byteRead & 0x7f) << (numBitsToShift[numBytes])); + numBytes++; + } while (numBytes < Math.min(maxLengthPrefixSize, input.byteLength - offset) && (byteRead & 0x80) !== 0); + if ((byteRead & 0x80) !== 0 && numBytes < maxLengthPrefixSize) { + throw new Error("Cannot read message size."); + } + if (numBytes === maxLengthPrefixSize && byteRead > 7) { + throw new Error("Messages bigger than 2GB are not supported."); + } + if (uint8Array.byteLength >= (offset + numBytes + size)) { + // IE does not support .slice() so use subarray + result.push(uint8Array.slice + ? uint8Array.slice(offset + numBytes, offset + numBytes + size) + : uint8Array.subarray(offset + numBytes, offset + numBytes + size)); + } + else { + throw new Error("Incomplete message."); + } + offset = offset + numBytes + size; + } + return result; + }; + return BinaryMessageFormat; +}()); +exports.BinaryMessageFormat = BinaryMessageFormat; + +}); + +unwrapExports(BinaryMessageFormat_1); +var BinaryMessageFormat_2 = BinaryMessageFormat_1.BinaryMessageFormat; + +var MessagePackHubProtocol_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + +// TypeDoc's @inheritDoc and @link don't work across modules :( +/** Implements the MessagePack Hub Protocol */ +var MessagePackHubProtocol = /** @class */ (function () { + function MessagePackHubProtocol() { + /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */ + this.name = "messagepack"; + /** The version of the protocol. */ + this.version = 1; + /** The TransferFormat of the protocol. */ + this.transferFormat = signalr.TransferFormat.Binary; + } + /** Creates an array of HubMessage objects from the specified serialized representation. + * + * @param {ArrayBuffer} input An ArrayBuffer containing the serialized representation. + * @param {ILogger} logger A logger that will be used to log messages that occur during parsing. + */ + MessagePackHubProtocol.prototype.parseMessages = function (input, logger) { + var _this = this; + // The interface does allow "string" to be passed in, but this implementation does not. So let's throw a useful error. + if (!(input instanceof ArrayBuffer)) { + throw new Error("Invalid input for MessagePack hub protocol. Expected an ArrayBuffer."); + } + if (logger === null) { + logger = signalr.NullLogger.instance; + } + return BinaryMessageFormat_1.BinaryMessageFormat.parse(input).map(function (m) { return _this.parseMessage(m, logger); }); + }; + /** Writes the specified HubMessage to an ArrayBuffer and returns it. + * + * @param {HubMessage} message The message to write. + * @returns {ArrayBuffer} An ArrayBuffer containing the serialized representation of the message. + */ + MessagePackHubProtocol.prototype.writeMessage = function (message) { + switch (message.type) { + case signalr.MessageType.Invocation: + return this.writeInvocation(message); + case signalr.MessageType.StreamInvocation: + return this.writeStreamInvocation(message); + case signalr.MessageType.StreamItem: + case signalr.MessageType.Completion: + throw new Error("Writing messages of type '" + message.type + "' is not supported."); + default: + throw new Error("Invalid message type."); + } + }; + MessagePackHubProtocol.prototype.parseMessage = function (input, logger) { + if (input.length === 0) { + throw new Error("Invalid payload."); + } + var msgpack = msgpack5(); + var properties = msgpack.decode(new buffer.Buffer(input)); + if (properties.length === 0 || !(properties instanceof Array)) { + throw new Error("Invalid payload."); + } + var messageType = properties[0]; + switch (messageType) { + case signalr.MessageType.Invocation: + return this.createInvocationMessage(this.readHeaders(properties), properties); + case signalr.MessageType.StreamItem: + return this.createStreamItemMessage(this.readHeaders(properties), properties); + case signalr.MessageType.Completion: + return this.createCompletionMessage(this.readHeaders(properties), properties); + case signalr.MessageType.Ping: + return this.createPingMessage(properties); + case signalr.MessageType.Close: + return this.createCloseMessage(properties); + default: + // Future protocol changes can add message types, old clients can ignore them + logger.log(signalr.LogLevel.Information, "Unknown message type '" + messageType + "' ignored."); + return null; + } + }; + MessagePackHubProtocol.prototype.createCloseMessage = function (properties) { + // check minimum length to allow protocol to add items to the end of objects in future releases + if (properties.length < 2) { + throw new Error("Invalid payload for Close message."); + } + return { + // Close messages have no headers. + error: properties[1], + type: signalr.MessageType.Close, + }; + }; + MessagePackHubProtocol.prototype.createPingMessage = function (properties) { + // check minimum length to allow protocol to add items to the end of objects in future releases + if (properties.length < 1) { + throw new Error("Invalid payload for Ping message."); + } + return { + // Ping messages have no headers. + type: signalr.MessageType.Ping, + }; + }; + MessagePackHubProtocol.prototype.createInvocationMessage = function (headers, properties) { + // check minimum length to allow protocol to add items to the end of objects in future releases + if (properties.length < 5) { + throw new Error("Invalid payload for Invocation message."); + } + var invocationId = properties[2]; + if (invocationId) { + return { + arguments: properties[4], + headers: headers, + invocationId: invocationId, + target: properties[3], + type: signalr.MessageType.Invocation, + }; + } + else { + return { + arguments: properties[4], + headers: headers, + target: properties[3], + type: signalr.MessageType.Invocation, + }; + } + }; + MessagePackHubProtocol.prototype.createStreamItemMessage = function (headers, properties) { + // check minimum length to allow protocol to add items to the end of objects in future releases + if (properties.length < 4) { + throw new Error("Invalid payload for StreamItem message."); + } + return { + headers: headers, + invocationId: properties[2], + item: properties[3], + type: signalr.MessageType.StreamItem, + }; + }; + MessagePackHubProtocol.prototype.createCompletionMessage = function (headers, properties) { + // check minimum length to allow protocol to add items to the end of objects in future releases + if (properties.length < 4) { + throw new Error("Invalid payload for Completion message."); + } + var errorResult = 1; + var voidResult = 2; + var nonVoidResult = 3; + var resultKind = properties[3]; + if (resultKind !== voidResult && properties.length < 5) { + throw new Error("Invalid payload for Completion message."); + } + var completionMessage = { + error: null, + headers: headers, + invocationId: properties[2], + result: null, + type: signalr.MessageType.Completion, + }; + switch (resultKind) { + case errorResult: + completionMessage.error = properties[4]; + break; + case nonVoidResult: + completionMessage.result = properties[4]; + break; + } + return completionMessage; + }; + MessagePackHubProtocol.prototype.writeInvocation = function (invocationMessage) { + var msgpack = msgpack5(); + var payload = msgpack.encode([signalr.MessageType.Invocation, invocationMessage.headers || {}, invocationMessage.invocationId || null, + invocationMessage.target, invocationMessage.arguments]); + return BinaryMessageFormat_1.BinaryMessageFormat.write(payload.slice()); + }; + MessagePackHubProtocol.prototype.writeStreamInvocation = function (streamInvocationMessage) { + var msgpack = msgpack5(); + var payload = msgpack.encode([signalr.MessageType.StreamInvocation, streamInvocationMessage.headers || {}, streamInvocationMessage.invocationId, + streamInvocationMessage.target, streamInvocationMessage.arguments]); + return BinaryMessageFormat_1.BinaryMessageFormat.write(payload.slice()); + }; + MessagePackHubProtocol.prototype.readHeaders = function (properties) { + var headers = properties[1]; + if (typeof headers !== "object") { + throw new Error("Invalid headers."); + } + return headers; + }; + return MessagePackHubProtocol; +}()); +exports.MessagePackHubProtocol = MessagePackHubProtocol; + +}); + +unwrapExports(MessagePackHubProtocol_1); +var MessagePackHubProtocol_2 = MessagePackHubProtocol_1.MessagePackHubProtocol; + +var cjs = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// Version token that will be replaced by the prepack command +/** The version of the SignalR Message Pack protocol library. */ +exports.VERSION = "0.0.0-DEV_BUILD"; + +exports.MessagePackHubProtocol = MessagePackHubProtocol_1.MessagePackHubProtocol; + +}); + +unwrapExports(cjs); +var cjs_1 = cjs.VERSION; +var cjs_2 = cjs.MessagePackHubProtocol; + +var browserIndex = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + +// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds. +tslib_es6.__exportStar(cjs, exports); + +}); + +var browserIndex$1 = unwrapExports(browserIndex); + +return browserIndex$1; + +}))); +//# sourceMappingURL=signalr-protocol-msgpack.js.map diff --git a/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js.map b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js.map new file mode 100644 index 000000000..09ce2983a --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.js.map @@ -0,0 +1 @@ +{"version":3,"file":"signalr-protocol-msgpack.js","sources":["../../../node_modules/tslib/tslib.es6.js","../../node_modules/base64-js/index.js","../../node_modules/ieee754/index.js","../../node_modules/buffer/index.js","../../src/BinaryMessageFormat.ts","../../src/MessagePackHubProtocol.ts","../../src/index.ts","../../src/browser-index.ts"],"sourcesContent":["/*! *****************************************************************************\r\nCopyright (c) Microsoft Corporation. All rights reserved.\r\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use\r\nthis file except in compliance with the License. You may obtain a copy of the\r\nLicense at http://www.apache.org/licenses/LICENSE-2.0\r\n\r\nTHIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r\nKIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED\r\nWARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,\r\nMERCHANTABLITY OR NON-INFRINGEMENT.\r\n\r\nSee the Apache Version 2.0 License for specific language governing permissions\r\nand limitations under the License.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\r\n\r\nexport function __extends(d, b) {\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)\r\n t[p[i]] = s[p[i]];\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport function __exportStar(m, exports) {\r\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\r\n}\r\n\r\nexport function __values(o) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator], i = 0;\r\n if (m) return m.call(o);\r\n return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; }; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator];\r\n return m ? m.call(o) : typeof __values === \"function\" ? __values(o) : o[Symbol.iterator]();\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];\r\n result.default = mod;\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n","'use strict'\n\nexports.byteLength = byteLength\nexports.toByteArray = toByteArray\nexports.fromByteArray = fromByteArray\n\nvar lookup = []\nvar revLookup = []\nvar Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array\n\nvar code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\nfor (var i = 0, len = code.length; i < len; ++i) {\n lookup[i] = code[i]\n revLookup[code.charCodeAt(i)] = i\n}\n\nrevLookup['-'.charCodeAt(0)] = 62\nrevLookup['_'.charCodeAt(0)] = 63\n\nfunction placeHoldersCount (b64) {\n var len = b64.length\n if (len % 4 > 0) {\n throw new Error('Invalid string. Length must be a multiple of 4')\n }\n\n // the number of equal signs (place holders)\n // if there are two placeholders, than the two characters before it\n // represent one byte\n // if there is only one, then the three characters before it represent 2 bytes\n // this is just a cheap hack to not do indexOf twice\n return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0\n}\n\nfunction byteLength (b64) {\n // base64 is 4/3 + up to two characters of the original data\n return (b64.length * 3 / 4) - placeHoldersCount(b64)\n}\n\nfunction toByteArray (b64) {\n var i, l, tmp, placeHolders, arr\n var len = b64.length\n placeHolders = placeHoldersCount(b64)\n\n arr = new Arr((len * 3 / 4) - placeHolders)\n\n // if there are placeholders, only get up to the last complete 4 chars\n l = placeHolders > 0 ? len - 4 : len\n\n var L = 0\n\n for (i = 0; i < l; i += 4) {\n tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]\n arr[L++] = (tmp >> 16) & 0xFF\n arr[L++] = (tmp >> 8) & 0xFF\n arr[L++] = tmp & 0xFF\n }\n\n if (placeHolders === 2) {\n tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)\n arr[L++] = tmp & 0xFF\n } else if (placeHolders === 1) {\n tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)\n arr[L++] = (tmp >> 8) & 0xFF\n arr[L++] = tmp & 0xFF\n }\n\n return arr\n}\n\nfunction tripletToBase64 (num) {\n return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]\n}\n\nfunction encodeChunk (uint8, start, end) {\n var tmp\n var output = []\n for (var i = start; i < end; i += 3) {\n tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])\n output.push(tripletToBase64(tmp))\n }\n return output.join('')\n}\n\nfunction fromByteArray (uint8) {\n var tmp\n var len = uint8.length\n var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes\n var output = ''\n var parts = []\n var maxChunkLength = 16383 // must be multiple of 3\n\n // go through the array every three bytes, we'll deal with trailing stuff later\n for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))\n }\n\n // pad the end with zeros, but make sure to not forget the extra bytes\n if (extraBytes === 1) {\n tmp = uint8[len - 1]\n output += lookup[tmp >> 2]\n output += lookup[(tmp << 4) & 0x3F]\n output += '=='\n } else if (extraBytes === 2) {\n tmp = (uint8[len - 2] << 8) + (uint8[len - 1])\n output += lookup[tmp >> 10]\n output += lookup[(tmp >> 4) & 0x3F]\n output += lookup[(tmp << 2) & 0x3F]\n output += '='\n }\n\n parts.push(output)\n\n return parts.join('')\n}\n","exports.read = function (buffer, offset, isLE, mLen, nBytes) {\n var e, m\n var eLen = nBytes * 8 - mLen - 1\n var eMax = (1 << eLen) - 1\n var eBias = eMax >> 1\n var nBits = -7\n var i = isLE ? (nBytes - 1) : 0\n var d = isLE ? -1 : 1\n var s = buffer[offset + i]\n\n i += d\n\n e = s & ((1 << (-nBits)) - 1)\n s >>= (-nBits)\n nBits += eLen\n for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}\n\n m = e & ((1 << (-nBits)) - 1)\n e >>= (-nBits)\n nBits += mLen\n for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}\n\n if (e === 0) {\n e = 1 - eBias\n } else if (e === eMax) {\n return m ? NaN : ((s ? -1 : 1) * Infinity)\n } else {\n m = m + Math.pow(2, mLen)\n e = e - eBias\n }\n return (s ? -1 : 1) * m * Math.pow(2, e - mLen)\n}\n\nexports.write = function (buffer, value, offset, isLE, mLen, nBytes) {\n var e, m, c\n var eLen = nBytes * 8 - mLen - 1\n var eMax = (1 << eLen) - 1\n var eBias = eMax >> 1\n var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)\n var i = isLE ? 0 : (nBytes - 1)\n var d = isLE ? 1 : -1\n var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0\n\n value = Math.abs(value)\n\n if (isNaN(value) || value === Infinity) {\n m = isNaN(value) ? 1 : 0\n e = eMax\n } else {\n e = Math.floor(Math.log(value) / Math.LN2)\n if (value * (c = Math.pow(2, -e)) < 1) {\n e--\n c *= 2\n }\n if (e + eBias >= 1) {\n value += rt / c\n } else {\n value += rt * Math.pow(2, 1 - eBias)\n }\n if (value * c >= 2) {\n e++\n c /= 2\n }\n\n if (e + eBias >= eMax) {\n m = 0\n e = eMax\n } else if (e + eBias >= 1) {\n m = (value * c - 1) * Math.pow(2, mLen)\n e = e + eBias\n } else {\n m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)\n e = 0\n }\n }\n\n for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}\n\n e = (e << mLen) | m\n eLen += mLen\n for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}\n\n buffer[offset + i - d] |= s * 128\n}\n","/*!\n * The buffer module from node.js, for the browser.\n *\n * @author Feross Aboukhadijeh \n * @license MIT\n */\n/* eslint-disable no-proto */\n\n'use strict'\n\nvar base64 = require('base64-js')\nvar ieee754 = require('ieee754')\n\nexports.Buffer = Buffer\nexports.SlowBuffer = SlowBuffer\nexports.INSPECT_MAX_BYTES = 50\n\nvar K_MAX_LENGTH = 0x7fffffff\nexports.kMaxLength = K_MAX_LENGTH\n\n/**\n * If `Buffer.TYPED_ARRAY_SUPPORT`:\n * === true Use Uint8Array implementation (fastest)\n * === false Print warning and recommend using `buffer` v4.x which has an Object\n * implementation (most compatible, even IE6)\n *\n * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,\n * Opera 11.6+, iOS 4.2+.\n *\n * We report that the browser does not support typed arrays if the are not subclassable\n * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`\n * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support\n * for __proto__ and has a buggy typed array implementation.\n */\nBuffer.TYPED_ARRAY_SUPPORT = typedArraySupport()\n\nif (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&\n typeof console.error === 'function') {\n console.error(\n 'This browser lacks typed array (Uint8Array) support which is required by ' +\n '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'\n )\n}\n\nfunction typedArraySupport () {\n // Can typed array instances can be augmented?\n try {\n var arr = new Uint8Array(1)\n arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}\n return arr.foo() === 42\n } catch (e) {\n return false\n }\n}\n\nfunction createBuffer (length) {\n if (length > K_MAX_LENGTH) {\n throw new RangeError('Invalid typed array length')\n }\n // Return an augmented `Uint8Array` instance\n var buf = new Uint8Array(length)\n buf.__proto__ = Buffer.prototype\n return buf\n}\n\n/**\n * The Buffer constructor returns instances of `Uint8Array` that have their\n * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of\n * `Uint8Array`, so the returned instances will have all the node `Buffer` methods\n * and the `Uint8Array` methods. Square bracket notation works as expected -- it\n * returns a single octet.\n *\n * The `Uint8Array` prototype remains unmodified.\n */\n\nfunction Buffer (arg, encodingOrOffset, length) {\n // Common case.\n if (typeof arg === 'number') {\n if (typeof encodingOrOffset === 'string') {\n throw new Error(\n 'If encoding is specified then the first argument must be a string'\n )\n }\n return allocUnsafe(arg)\n }\n return from(arg, encodingOrOffset, length)\n}\n\n// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97\nif (typeof Symbol !== 'undefined' && Symbol.species &&\n Buffer[Symbol.species] === Buffer) {\n Object.defineProperty(Buffer, Symbol.species, {\n value: null,\n configurable: true,\n enumerable: false,\n writable: false\n })\n}\n\nBuffer.poolSize = 8192 // not used by this implementation\n\nfunction from (value, encodingOrOffset, length) {\n if (typeof value === 'number') {\n throw new TypeError('\"value\" argument must not be a number')\n }\n\n if (isArrayBuffer(value)) {\n return fromArrayBuffer(value, encodingOrOffset, length)\n }\n\n if (typeof value === 'string') {\n return fromString(value, encodingOrOffset)\n }\n\n return fromObject(value)\n}\n\n/**\n * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError\n * if value is a number.\n * Buffer.from(str[, encoding])\n * Buffer.from(array)\n * Buffer.from(buffer)\n * Buffer.from(arrayBuffer[, byteOffset[, length]])\n **/\nBuffer.from = function (value, encodingOrOffset, length) {\n return from(value, encodingOrOffset, length)\n}\n\n// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:\n// https://github.com/feross/buffer/pull/148\nBuffer.prototype.__proto__ = Uint8Array.prototype\nBuffer.__proto__ = Uint8Array\n\nfunction assertSize (size) {\n if (typeof size !== 'number') {\n throw new TypeError('\"size\" argument must be a number')\n } else if (size < 0) {\n throw new RangeError('\"size\" argument must not be negative')\n }\n}\n\nfunction alloc (size, fill, encoding) {\n assertSize(size)\n if (size <= 0) {\n return createBuffer(size)\n }\n if (fill !== undefined) {\n // Only pay attention to encoding if it's a string. This\n // prevents accidentally sending in a number that would\n // be interpretted as a start offset.\n return typeof encoding === 'string'\n ? createBuffer(size).fill(fill, encoding)\n : createBuffer(size).fill(fill)\n }\n return createBuffer(size)\n}\n\n/**\n * Creates a new filled Buffer instance.\n * alloc(size[, fill[, encoding]])\n **/\nBuffer.alloc = function (size, fill, encoding) {\n return alloc(size, fill, encoding)\n}\n\nfunction allocUnsafe (size) {\n assertSize(size)\n return createBuffer(size < 0 ? 0 : checked(size) | 0)\n}\n\n/**\n * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.\n * */\nBuffer.allocUnsafe = function (size) {\n return allocUnsafe(size)\n}\n/**\n * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.\n */\nBuffer.allocUnsafeSlow = function (size) {\n return allocUnsafe(size)\n}\n\nfunction fromString (string, encoding) {\n if (typeof encoding !== 'string' || encoding === '') {\n encoding = 'utf8'\n }\n\n if (!Buffer.isEncoding(encoding)) {\n throw new TypeError('\"encoding\" must be a valid string encoding')\n }\n\n var length = byteLength(string, encoding) | 0\n var buf = createBuffer(length)\n\n var actual = buf.write(string, encoding)\n\n if (actual !== length) {\n // Writing a hex string, for example, that contains invalid characters will\n // cause everything after the first invalid character to be ignored. (e.g.\n // 'abxxcd' will be treated as 'ab')\n buf = buf.slice(0, actual)\n }\n\n return buf\n}\n\nfunction fromArrayLike (array) {\n var length = array.length < 0 ? 0 : checked(array.length) | 0\n var buf = createBuffer(length)\n for (var i = 0; i < length; i += 1) {\n buf[i] = array[i] & 255\n }\n return buf\n}\n\nfunction fromArrayBuffer (array, byteOffset, length) {\n if (byteOffset < 0 || array.byteLength < byteOffset) {\n throw new RangeError('\\'offset\\' is out of bounds')\n }\n\n if (array.byteLength < byteOffset + (length || 0)) {\n throw new RangeError('\\'length\\' is out of bounds')\n }\n\n var buf\n if (byteOffset === undefined && length === undefined) {\n buf = new Uint8Array(array)\n } else if (length === undefined) {\n buf = new Uint8Array(array, byteOffset)\n } else {\n buf = new Uint8Array(array, byteOffset, length)\n }\n\n // Return an augmented `Uint8Array` instance\n buf.__proto__ = Buffer.prototype\n return buf\n}\n\nfunction fromObject (obj) {\n if (Buffer.isBuffer(obj)) {\n var len = checked(obj.length) | 0\n var buf = createBuffer(len)\n\n if (buf.length === 0) {\n return buf\n }\n\n obj.copy(buf, 0, 0, len)\n return buf\n }\n\n if (obj) {\n if (isArrayBufferView(obj) || 'length' in obj) {\n if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {\n return createBuffer(0)\n }\n return fromArrayLike(obj)\n }\n\n if (obj.type === 'Buffer' && Array.isArray(obj.data)) {\n return fromArrayLike(obj.data)\n }\n }\n\n throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')\n}\n\nfunction checked (length) {\n // Note: cannot use `length < K_MAX_LENGTH` here because that fails when\n // length is NaN (which is otherwise coerced to zero.)\n if (length >= K_MAX_LENGTH) {\n throw new RangeError('Attempt to allocate Buffer larger than maximum ' +\n 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')\n }\n return length | 0\n}\n\nfunction SlowBuffer (length) {\n if (+length != length) { // eslint-disable-line eqeqeq\n length = 0\n }\n return Buffer.alloc(+length)\n}\n\nBuffer.isBuffer = function isBuffer (b) {\n return b != null && b._isBuffer === true\n}\n\nBuffer.compare = function compare (a, b) {\n if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {\n throw new TypeError('Arguments must be Buffers')\n }\n\n if (a === b) return 0\n\n var x = a.length\n var y = b.length\n\n for (var i = 0, len = Math.min(x, y); i < len; ++i) {\n if (a[i] !== b[i]) {\n x = a[i]\n y = b[i]\n break\n }\n }\n\n if (x < y) return -1\n if (y < x) return 1\n return 0\n}\n\nBuffer.isEncoding = function isEncoding (encoding) {\n switch (String(encoding).toLowerCase()) {\n case 'hex':\n case 'utf8':\n case 'utf-8':\n case 'ascii':\n case 'latin1':\n case 'binary':\n case 'base64':\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return true\n default:\n return false\n }\n}\n\nBuffer.concat = function concat (list, length) {\n if (!Array.isArray(list)) {\n throw new TypeError('\"list\" argument must be an Array of Buffers')\n }\n\n if (list.length === 0) {\n return Buffer.alloc(0)\n }\n\n var i\n if (length === undefined) {\n length = 0\n for (i = 0; i < list.length; ++i) {\n length += list[i].length\n }\n }\n\n var buffer = Buffer.allocUnsafe(length)\n var pos = 0\n for (i = 0; i < list.length; ++i) {\n var buf = list[i]\n if (!Buffer.isBuffer(buf)) {\n throw new TypeError('\"list\" argument must be an Array of Buffers')\n }\n buf.copy(buffer, pos)\n pos += buf.length\n }\n return buffer\n}\n\nfunction byteLength (string, encoding) {\n if (Buffer.isBuffer(string)) {\n return string.length\n }\n if (isArrayBufferView(string) || isArrayBuffer(string)) {\n return string.byteLength\n }\n if (typeof string !== 'string') {\n string = '' + string\n }\n\n var len = string.length\n if (len === 0) return 0\n\n // Use a for loop to avoid recursion\n var loweredCase = false\n for (;;) {\n switch (encoding) {\n case 'ascii':\n case 'latin1':\n case 'binary':\n return len\n case 'utf8':\n case 'utf-8':\n case undefined:\n return utf8ToBytes(string).length\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return len * 2\n case 'hex':\n return len >>> 1\n case 'base64':\n return base64ToBytes(string).length\n default:\n if (loweredCase) return utf8ToBytes(string).length // assume utf8\n encoding = ('' + encoding).toLowerCase()\n loweredCase = true\n }\n }\n}\nBuffer.byteLength = byteLength\n\nfunction slowToString (encoding, start, end) {\n var loweredCase = false\n\n // No need to verify that \"this.length <= MAX_UINT32\" since it's a read-only\n // property of a typed array.\n\n // This behaves neither like String nor Uint8Array in that we set start/end\n // to their upper/lower bounds if the value passed is out of range.\n // undefined is handled specially as per ECMA-262 6th Edition,\n // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.\n if (start === undefined || start < 0) {\n start = 0\n }\n // Return early if start > this.length. Done here to prevent potential uint32\n // coercion fail below.\n if (start > this.length) {\n return ''\n }\n\n if (end === undefined || end > this.length) {\n end = this.length\n }\n\n if (end <= 0) {\n return ''\n }\n\n // Force coersion to uint32. This will also coerce falsey/NaN values to 0.\n end >>>= 0\n start >>>= 0\n\n if (end <= start) {\n return ''\n }\n\n if (!encoding) encoding = 'utf8'\n\n while (true) {\n switch (encoding) {\n case 'hex':\n return hexSlice(this, start, end)\n\n case 'utf8':\n case 'utf-8':\n return utf8Slice(this, start, end)\n\n case 'ascii':\n return asciiSlice(this, start, end)\n\n case 'latin1':\n case 'binary':\n return latin1Slice(this, start, end)\n\n case 'base64':\n return base64Slice(this, start, end)\n\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return utf16leSlice(this, start, end)\n\n default:\n if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)\n encoding = (encoding + '').toLowerCase()\n loweredCase = true\n }\n }\n}\n\n// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)\n// to detect a Buffer instance. It's not possible to use `instanceof Buffer`\n// reliably in a browserify context because there could be multiple different\n// copies of the 'buffer' package in use. This method works even for Buffer\n// instances that were created from another copy of the `buffer` package.\n// See: https://github.com/feross/buffer/issues/154\nBuffer.prototype._isBuffer = true\n\nfunction swap (b, n, m) {\n var i = b[n]\n b[n] = b[m]\n b[m] = i\n}\n\nBuffer.prototype.swap16 = function swap16 () {\n var len = this.length\n if (len % 2 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 16-bits')\n }\n for (var i = 0; i < len; i += 2) {\n swap(this, i, i + 1)\n }\n return this\n}\n\nBuffer.prototype.swap32 = function swap32 () {\n var len = this.length\n if (len % 4 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 32-bits')\n }\n for (var i = 0; i < len; i += 4) {\n swap(this, i, i + 3)\n swap(this, i + 1, i + 2)\n }\n return this\n}\n\nBuffer.prototype.swap64 = function swap64 () {\n var len = this.length\n if (len % 8 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 64-bits')\n }\n for (var i = 0; i < len; i += 8) {\n swap(this, i, i + 7)\n swap(this, i + 1, i + 6)\n swap(this, i + 2, i + 5)\n swap(this, i + 3, i + 4)\n }\n return this\n}\n\nBuffer.prototype.toString = function toString () {\n var length = this.length\n if (length === 0) return ''\n if (arguments.length === 0) return utf8Slice(this, 0, length)\n return slowToString.apply(this, arguments)\n}\n\nBuffer.prototype.equals = function equals (b) {\n if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')\n if (this === b) return true\n return Buffer.compare(this, b) === 0\n}\n\nBuffer.prototype.inspect = function inspect () {\n var str = ''\n var max = exports.INSPECT_MAX_BYTES\n if (this.length > 0) {\n str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')\n if (this.length > max) str += ' ... '\n }\n return ''\n}\n\nBuffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {\n if (!Buffer.isBuffer(target)) {\n throw new TypeError('Argument must be a Buffer')\n }\n\n if (start === undefined) {\n start = 0\n }\n if (end === undefined) {\n end = target ? target.length : 0\n }\n if (thisStart === undefined) {\n thisStart = 0\n }\n if (thisEnd === undefined) {\n thisEnd = this.length\n }\n\n if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {\n throw new RangeError('out of range index')\n }\n\n if (thisStart >= thisEnd && start >= end) {\n return 0\n }\n if (thisStart >= thisEnd) {\n return -1\n }\n if (start >= end) {\n return 1\n }\n\n start >>>= 0\n end >>>= 0\n thisStart >>>= 0\n thisEnd >>>= 0\n\n if (this === target) return 0\n\n var x = thisEnd - thisStart\n var y = end - start\n var len = Math.min(x, y)\n\n var thisCopy = this.slice(thisStart, thisEnd)\n var targetCopy = target.slice(start, end)\n\n for (var i = 0; i < len; ++i) {\n if (thisCopy[i] !== targetCopy[i]) {\n x = thisCopy[i]\n y = targetCopy[i]\n break\n }\n }\n\n if (x < y) return -1\n if (y < x) return 1\n return 0\n}\n\n// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,\n// OR the last index of `val` in `buffer` at offset <= `byteOffset`.\n//\n// Arguments:\n// - buffer - a Buffer to search\n// - val - a string, Buffer, or number\n// - byteOffset - an index into `buffer`; will be clamped to an int32\n// - encoding - an optional encoding, relevant is val is a string\n// - dir - true for indexOf, false for lastIndexOf\nfunction bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {\n // Empty buffer means no match\n if (buffer.length === 0) return -1\n\n // Normalize byteOffset\n if (typeof byteOffset === 'string') {\n encoding = byteOffset\n byteOffset = 0\n } else if (byteOffset > 0x7fffffff) {\n byteOffset = 0x7fffffff\n } else if (byteOffset < -0x80000000) {\n byteOffset = -0x80000000\n }\n byteOffset = +byteOffset // Coerce to Number.\n if (numberIsNaN(byteOffset)) {\n // byteOffset: it it's undefined, null, NaN, \"foo\", etc, search whole buffer\n byteOffset = dir ? 0 : (buffer.length - 1)\n }\n\n // Normalize byteOffset: negative offsets start from the end of the buffer\n if (byteOffset < 0) byteOffset = buffer.length + byteOffset\n if (byteOffset >= buffer.length) {\n if (dir) return -1\n else byteOffset = buffer.length - 1\n } else if (byteOffset < 0) {\n if (dir) byteOffset = 0\n else return -1\n }\n\n // Normalize val\n if (typeof val === 'string') {\n val = Buffer.from(val, encoding)\n }\n\n // Finally, search either indexOf (if dir is true) or lastIndexOf\n if (Buffer.isBuffer(val)) {\n // Special case: looking for empty string/buffer always fails\n if (val.length === 0) {\n return -1\n }\n return arrayIndexOf(buffer, val, byteOffset, encoding, dir)\n } else if (typeof val === 'number') {\n val = val & 0xFF // Search for a byte value [0-255]\n if (typeof Uint8Array.prototype.indexOf === 'function') {\n if (dir) {\n return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)\n } else {\n return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)\n }\n }\n return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)\n }\n\n throw new TypeError('val must be string, number or Buffer')\n}\n\nfunction arrayIndexOf (arr, val, byteOffset, encoding, dir) {\n var indexSize = 1\n var arrLength = arr.length\n var valLength = val.length\n\n if (encoding !== undefined) {\n encoding = String(encoding).toLowerCase()\n if (encoding === 'ucs2' || encoding === 'ucs-2' ||\n encoding === 'utf16le' || encoding === 'utf-16le') {\n if (arr.length < 2 || val.length < 2) {\n return -1\n }\n indexSize = 2\n arrLength /= 2\n valLength /= 2\n byteOffset /= 2\n }\n }\n\n function read (buf, i) {\n if (indexSize === 1) {\n return buf[i]\n } else {\n return buf.readUInt16BE(i * indexSize)\n }\n }\n\n var i\n if (dir) {\n var foundIndex = -1\n for (i = byteOffset; i < arrLength; i++) {\n if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {\n if (foundIndex === -1) foundIndex = i\n if (i - foundIndex + 1 === valLength) return foundIndex * indexSize\n } else {\n if (foundIndex !== -1) i -= i - foundIndex\n foundIndex = -1\n }\n }\n } else {\n if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength\n for (i = byteOffset; i >= 0; i--) {\n var found = true\n for (var j = 0; j < valLength; j++) {\n if (read(arr, i + j) !== read(val, j)) {\n found = false\n break\n }\n }\n if (found) return i\n }\n }\n\n return -1\n}\n\nBuffer.prototype.includes = function includes (val, byteOffset, encoding) {\n return this.indexOf(val, byteOffset, encoding) !== -1\n}\n\nBuffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {\n return bidirectionalIndexOf(this, val, byteOffset, encoding, true)\n}\n\nBuffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {\n return bidirectionalIndexOf(this, val, byteOffset, encoding, false)\n}\n\nfunction hexWrite (buf, string, offset, length) {\n offset = Number(offset) || 0\n var remaining = buf.length - offset\n if (!length) {\n length = remaining\n } else {\n length = Number(length)\n if (length > remaining) {\n length = remaining\n }\n }\n\n // must be an even number of digits\n var strLen = string.length\n if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')\n\n if (length > strLen / 2) {\n length = strLen / 2\n }\n for (var i = 0; i < length; ++i) {\n var parsed = parseInt(string.substr(i * 2, 2), 16)\n if (numberIsNaN(parsed)) return i\n buf[offset + i] = parsed\n }\n return i\n}\n\nfunction utf8Write (buf, string, offset, length) {\n return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)\n}\n\nfunction asciiWrite (buf, string, offset, length) {\n return blitBuffer(asciiToBytes(string), buf, offset, length)\n}\n\nfunction latin1Write (buf, string, offset, length) {\n return asciiWrite(buf, string, offset, length)\n}\n\nfunction base64Write (buf, string, offset, length) {\n return blitBuffer(base64ToBytes(string), buf, offset, length)\n}\n\nfunction ucs2Write (buf, string, offset, length) {\n return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)\n}\n\nBuffer.prototype.write = function write (string, offset, length, encoding) {\n // Buffer#write(string)\n if (offset === undefined) {\n encoding = 'utf8'\n length = this.length\n offset = 0\n // Buffer#write(string, encoding)\n } else if (length === undefined && typeof offset === 'string') {\n encoding = offset\n length = this.length\n offset = 0\n // Buffer#write(string, offset[, length][, encoding])\n } else if (isFinite(offset)) {\n offset = offset >>> 0\n if (isFinite(length)) {\n length = length >>> 0\n if (encoding === undefined) encoding = 'utf8'\n } else {\n encoding = length\n length = undefined\n }\n } else {\n throw new Error(\n 'Buffer.write(string, encoding, offset[, length]) is no longer supported'\n )\n }\n\n var remaining = this.length - offset\n if (length === undefined || length > remaining) length = remaining\n\n if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {\n throw new RangeError('Attempt to write outside buffer bounds')\n }\n\n if (!encoding) encoding = 'utf8'\n\n var loweredCase = false\n for (;;) {\n switch (encoding) {\n case 'hex':\n return hexWrite(this, string, offset, length)\n\n case 'utf8':\n case 'utf-8':\n return utf8Write(this, string, offset, length)\n\n case 'ascii':\n return asciiWrite(this, string, offset, length)\n\n case 'latin1':\n case 'binary':\n return latin1Write(this, string, offset, length)\n\n case 'base64':\n // Warning: maxLength not taken into account in base64Write\n return base64Write(this, string, offset, length)\n\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return ucs2Write(this, string, offset, length)\n\n default:\n if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)\n encoding = ('' + encoding).toLowerCase()\n loweredCase = true\n }\n }\n}\n\nBuffer.prototype.toJSON = function toJSON () {\n return {\n type: 'Buffer',\n data: Array.prototype.slice.call(this._arr || this, 0)\n }\n}\n\nfunction base64Slice (buf, start, end) {\n if (start === 0 && end === buf.length) {\n return base64.fromByteArray(buf)\n } else {\n return base64.fromByteArray(buf.slice(start, end))\n }\n}\n\nfunction utf8Slice (buf, start, end) {\n end = Math.min(buf.length, end)\n var res = []\n\n var i = start\n while (i < end) {\n var firstByte = buf[i]\n var codePoint = null\n var bytesPerSequence = (firstByte > 0xEF) ? 4\n : (firstByte > 0xDF) ? 3\n : (firstByte > 0xBF) ? 2\n : 1\n\n if (i + bytesPerSequence <= end) {\n var secondByte, thirdByte, fourthByte, tempCodePoint\n\n switch (bytesPerSequence) {\n case 1:\n if (firstByte < 0x80) {\n codePoint = firstByte\n }\n break\n case 2:\n secondByte = buf[i + 1]\n if ((secondByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)\n if (tempCodePoint > 0x7F) {\n codePoint = tempCodePoint\n }\n }\n break\n case 3:\n secondByte = buf[i + 1]\n thirdByte = buf[i + 2]\n if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)\n if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {\n codePoint = tempCodePoint\n }\n }\n break\n case 4:\n secondByte = buf[i + 1]\n thirdByte = buf[i + 2]\n fourthByte = buf[i + 3]\n if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)\n if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {\n codePoint = tempCodePoint\n }\n }\n }\n }\n\n if (codePoint === null) {\n // we did not generate a valid codePoint so insert a\n // replacement char (U+FFFD) and advance only 1 byte\n codePoint = 0xFFFD\n bytesPerSequence = 1\n } else if (codePoint > 0xFFFF) {\n // encode to utf16 (surrogate pair dance)\n codePoint -= 0x10000\n res.push(codePoint >>> 10 & 0x3FF | 0xD800)\n codePoint = 0xDC00 | codePoint & 0x3FF\n }\n\n res.push(codePoint)\n i += bytesPerSequence\n }\n\n return decodeCodePointsArray(res)\n}\n\n// Based on http://stackoverflow.com/a/22747272/680742, the browser with\n// the lowest limit is Chrome, with 0x10000 args.\n// We go 1 magnitude less, for safety\nvar MAX_ARGUMENTS_LENGTH = 0x1000\n\nfunction decodeCodePointsArray (codePoints) {\n var len = codePoints.length\n if (len <= MAX_ARGUMENTS_LENGTH) {\n return String.fromCharCode.apply(String, codePoints) // avoid extra slice()\n }\n\n // Decode in chunks to avoid \"call stack size exceeded\".\n var res = ''\n var i = 0\n while (i < len) {\n res += String.fromCharCode.apply(\n String,\n codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)\n )\n }\n return res\n}\n\nfunction asciiSlice (buf, start, end) {\n var ret = ''\n end = Math.min(buf.length, end)\n\n for (var i = start; i < end; ++i) {\n ret += String.fromCharCode(buf[i] & 0x7F)\n }\n return ret\n}\n\nfunction latin1Slice (buf, start, end) {\n var ret = ''\n end = Math.min(buf.length, end)\n\n for (var i = start; i < end; ++i) {\n ret += String.fromCharCode(buf[i])\n }\n return ret\n}\n\nfunction hexSlice (buf, start, end) {\n var len = buf.length\n\n if (!start || start < 0) start = 0\n if (!end || end < 0 || end > len) end = len\n\n var out = ''\n for (var i = start; i < end; ++i) {\n out += toHex(buf[i])\n }\n return out\n}\n\nfunction utf16leSlice (buf, start, end) {\n var bytes = buf.slice(start, end)\n var res = ''\n for (var i = 0; i < bytes.length; i += 2) {\n res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))\n }\n return res\n}\n\nBuffer.prototype.slice = function slice (start, end) {\n var len = this.length\n start = ~~start\n end = end === undefined ? len : ~~end\n\n if (start < 0) {\n start += len\n if (start < 0) start = 0\n } else if (start > len) {\n start = len\n }\n\n if (end < 0) {\n end += len\n if (end < 0) end = 0\n } else if (end > len) {\n end = len\n }\n\n if (end < start) end = start\n\n var newBuf = this.subarray(start, end)\n // Return an augmented `Uint8Array` instance\n newBuf.__proto__ = Buffer.prototype\n return newBuf\n}\n\n/*\n * Need to make sure that buffer isn't trying to write out of bounds.\n */\nfunction checkOffset (offset, ext, length) {\n if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')\n if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')\n}\n\nBuffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var val = this[offset]\n var mul = 1\n var i = 0\n while (++i < byteLength && (mul *= 0x100)) {\n val += this[offset + i] * mul\n }\n\n return val\n}\n\nBuffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n checkOffset(offset, byteLength, this.length)\n }\n\n var val = this[offset + --byteLength]\n var mul = 1\n while (byteLength > 0 && (mul *= 0x100)) {\n val += this[offset + --byteLength] * mul\n }\n\n return val\n}\n\nBuffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 1, this.length)\n return this[offset]\n}\n\nBuffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n return this[offset] | (this[offset + 1] << 8)\n}\n\nBuffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n return (this[offset] << 8) | this[offset + 1]\n}\n\nBuffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return ((this[offset]) |\n (this[offset + 1] << 8) |\n (this[offset + 2] << 16)) +\n (this[offset + 3] * 0x1000000)\n}\n\nBuffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset] * 0x1000000) +\n ((this[offset + 1] << 16) |\n (this[offset + 2] << 8) |\n this[offset + 3])\n}\n\nBuffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var val = this[offset]\n var mul = 1\n var i = 0\n while (++i < byteLength && (mul *= 0x100)) {\n val += this[offset + i] * mul\n }\n mul *= 0x80\n\n if (val >= mul) val -= Math.pow(2, 8 * byteLength)\n\n return val\n}\n\nBuffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var i = byteLength\n var mul = 1\n var val = this[offset + --i]\n while (i > 0 && (mul *= 0x100)) {\n val += this[offset + --i] * mul\n }\n mul *= 0x80\n\n if (val >= mul) val -= Math.pow(2, 8 * byteLength)\n\n return val\n}\n\nBuffer.prototype.readInt8 = function readInt8 (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 1, this.length)\n if (!(this[offset] & 0x80)) return (this[offset])\n return ((0xff - this[offset] + 1) * -1)\n}\n\nBuffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n var val = this[offset] | (this[offset + 1] << 8)\n return (val & 0x8000) ? val | 0xFFFF0000 : val\n}\n\nBuffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n var val = this[offset + 1] | (this[offset] << 8)\n return (val & 0x8000) ? val | 0xFFFF0000 : val\n}\n\nBuffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset]) |\n (this[offset + 1] << 8) |\n (this[offset + 2] << 16) |\n (this[offset + 3] << 24)\n}\n\nBuffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset] << 24) |\n (this[offset + 1] << 16) |\n (this[offset + 2] << 8) |\n (this[offset + 3])\n}\n\nBuffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n return ieee754.read(this, offset, true, 23, 4)\n}\n\nBuffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n return ieee754.read(this, offset, false, 23, 4)\n}\n\nBuffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 8, this.length)\n return ieee754.read(this, offset, true, 52, 8)\n}\n\nBuffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 8, this.length)\n return ieee754.read(this, offset, false, 52, 8)\n}\n\nfunction checkInt (buf, value, offset, ext, max, min) {\n if (!Buffer.isBuffer(buf)) throw new TypeError('\"buffer\" argument must be a Buffer instance')\n if (value > max || value < min) throw new RangeError('\"value\" argument is out of bounds')\n if (offset + ext > buf.length) throw new RangeError('Index out of range')\n}\n\nBuffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n var maxBytes = Math.pow(2, 8 * byteLength) - 1\n checkInt(this, value, offset, byteLength, maxBytes, 0)\n }\n\n var mul = 1\n var i = 0\n this[offset] = value & 0xFF\n while (++i < byteLength && (mul *= 0x100)) {\n this[offset + i] = (value / mul) & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n var maxBytes = Math.pow(2, 8 * byteLength) - 1\n checkInt(this, value, offset, byteLength, maxBytes, 0)\n }\n\n var i = byteLength - 1\n var mul = 1\n this[offset + i] = value & 0xFF\n while (--i >= 0 && (mul *= 0x100)) {\n this[offset + i] = (value / mul) & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)\n this[offset] = (value & 0xff)\n return offset + 1\n}\n\nBuffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n return offset + 2\n}\n\nBuffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)\n this[offset] = (value >>> 8)\n this[offset + 1] = (value & 0xff)\n return offset + 2\n}\n\nBuffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)\n this[offset + 3] = (value >>> 24)\n this[offset + 2] = (value >>> 16)\n this[offset + 1] = (value >>> 8)\n this[offset] = (value & 0xff)\n return offset + 4\n}\n\nBuffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)\n this[offset] = (value >>> 24)\n this[offset + 1] = (value >>> 16)\n this[offset + 2] = (value >>> 8)\n this[offset + 3] = (value & 0xff)\n return offset + 4\n}\n\nBuffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n var limit = Math.pow(2, (8 * byteLength) - 1)\n\n checkInt(this, value, offset, byteLength, limit - 1, -limit)\n }\n\n var i = 0\n var mul = 1\n var sub = 0\n this[offset] = value & 0xFF\n while (++i < byteLength && (mul *= 0x100)) {\n if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {\n sub = 1\n }\n this[offset + i] = ((value / mul) >> 0) - sub & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n var limit = Math.pow(2, (8 * byteLength) - 1)\n\n checkInt(this, value, offset, byteLength, limit - 1, -limit)\n }\n\n var i = byteLength - 1\n var mul = 1\n var sub = 0\n this[offset + i] = value & 0xFF\n while (--i >= 0 && (mul *= 0x100)) {\n if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {\n sub = 1\n }\n this[offset + i] = ((value / mul) >> 0) - sub & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)\n if (value < 0) value = 0xff + value + 1\n this[offset] = (value & 0xff)\n return offset + 1\n}\n\nBuffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n return offset + 2\n}\n\nBuffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)\n this[offset] = (value >>> 8)\n this[offset + 1] = (value & 0xff)\n return offset + 2\n}\n\nBuffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n this[offset + 2] = (value >>> 16)\n this[offset + 3] = (value >>> 24)\n return offset + 4\n}\n\nBuffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)\n if (value < 0) value = 0xffffffff + value + 1\n this[offset] = (value >>> 24)\n this[offset + 1] = (value >>> 16)\n this[offset + 2] = (value >>> 8)\n this[offset + 3] = (value & 0xff)\n return offset + 4\n}\n\nfunction checkIEEE754 (buf, value, offset, ext, max, min) {\n if (offset + ext > buf.length) throw new RangeError('Index out of range')\n if (offset < 0) throw new RangeError('Index out of range')\n}\n\nfunction writeFloat (buf, value, offset, littleEndian, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)\n }\n ieee754.write(buf, value, offset, littleEndian, 23, 4)\n return offset + 4\n}\n\nBuffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {\n return writeFloat(this, value, offset, true, noAssert)\n}\n\nBuffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {\n return writeFloat(this, value, offset, false, noAssert)\n}\n\nfunction writeDouble (buf, value, offset, littleEndian, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)\n }\n ieee754.write(buf, value, offset, littleEndian, 52, 8)\n return offset + 8\n}\n\nBuffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {\n return writeDouble(this, value, offset, true, noAssert)\n}\n\nBuffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {\n return writeDouble(this, value, offset, false, noAssert)\n}\n\n// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)\nBuffer.prototype.copy = function copy (target, targetStart, start, end) {\n if (!start) start = 0\n if (!end && end !== 0) end = this.length\n if (targetStart >= target.length) targetStart = target.length\n if (!targetStart) targetStart = 0\n if (end > 0 && end < start) end = start\n\n // Copy 0 bytes; we're done\n if (end === start) return 0\n if (target.length === 0 || this.length === 0) return 0\n\n // Fatal error conditions\n if (targetStart < 0) {\n throw new RangeError('targetStart out of bounds')\n }\n if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')\n if (end < 0) throw new RangeError('sourceEnd out of bounds')\n\n // Are we oob?\n if (end > this.length) end = this.length\n if (target.length - targetStart < end - start) {\n end = target.length - targetStart + start\n }\n\n var len = end - start\n var i\n\n if (this === target && start < targetStart && targetStart < end) {\n // descending copy from end\n for (i = len - 1; i >= 0; --i) {\n target[i + targetStart] = this[i + start]\n }\n } else if (len < 1000) {\n // ascending copy from start\n for (i = 0; i < len; ++i) {\n target[i + targetStart] = this[i + start]\n }\n } else {\n Uint8Array.prototype.set.call(\n target,\n this.subarray(start, start + len),\n targetStart\n )\n }\n\n return len\n}\n\n// Usage:\n// buffer.fill(number[, offset[, end]])\n// buffer.fill(buffer[, offset[, end]])\n// buffer.fill(string[, offset[, end]][, encoding])\nBuffer.prototype.fill = function fill (val, start, end, encoding) {\n // Handle string cases:\n if (typeof val === 'string') {\n if (typeof start === 'string') {\n encoding = start\n start = 0\n end = this.length\n } else if (typeof end === 'string') {\n encoding = end\n end = this.length\n }\n if (val.length === 1) {\n var code = val.charCodeAt(0)\n if (code < 256) {\n val = code\n }\n }\n if (encoding !== undefined && typeof encoding !== 'string') {\n throw new TypeError('encoding must be a string')\n }\n if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {\n throw new TypeError('Unknown encoding: ' + encoding)\n }\n } else if (typeof val === 'number') {\n val = val & 255\n }\n\n // Invalid ranges are not set to a default, so can range check early.\n if (start < 0 || this.length < start || this.length < end) {\n throw new RangeError('Out of range index')\n }\n\n if (end <= start) {\n return this\n }\n\n start = start >>> 0\n end = end === undefined ? this.length : end >>> 0\n\n if (!val) val = 0\n\n var i\n if (typeof val === 'number') {\n for (i = start; i < end; ++i) {\n this[i] = val\n }\n } else {\n var bytes = Buffer.isBuffer(val)\n ? val\n : new Buffer(val, encoding)\n var len = bytes.length\n for (i = 0; i < end - start; ++i) {\n this[i + start] = bytes[i % len]\n }\n }\n\n return this\n}\n\n// HELPER FUNCTIONS\n// ================\n\nvar INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g\n\nfunction base64clean (str) {\n // Node strips out invalid characters like \\n and \\t from the string, base64-js does not\n str = str.trim().replace(INVALID_BASE64_RE, '')\n // Node converts strings with length < 2 to ''\n if (str.length < 2) return ''\n // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not\n while (str.length % 4 !== 0) {\n str = str + '='\n }\n return str\n}\n\nfunction toHex (n) {\n if (n < 16) return '0' + n.toString(16)\n return n.toString(16)\n}\n\nfunction utf8ToBytes (string, units) {\n units = units || Infinity\n var codePoint\n var length = string.length\n var leadSurrogate = null\n var bytes = []\n\n for (var i = 0; i < length; ++i) {\n codePoint = string.charCodeAt(i)\n\n // is surrogate component\n if (codePoint > 0xD7FF && codePoint < 0xE000) {\n // last char was a lead\n if (!leadSurrogate) {\n // no lead yet\n if (codePoint > 0xDBFF) {\n // unexpected trail\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n continue\n } else if (i + 1 === length) {\n // unpaired lead\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n continue\n }\n\n // valid lead\n leadSurrogate = codePoint\n\n continue\n }\n\n // 2 leads in a row\n if (codePoint < 0xDC00) {\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n leadSurrogate = codePoint\n continue\n }\n\n // valid surrogate pair\n codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000\n } else if (leadSurrogate) {\n // valid bmp char, but last char was a lead\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n }\n\n leadSurrogate = null\n\n // encode utf8\n if (codePoint < 0x80) {\n if ((units -= 1) < 0) break\n bytes.push(codePoint)\n } else if (codePoint < 0x800) {\n if ((units -= 2) < 0) break\n bytes.push(\n codePoint >> 0x6 | 0xC0,\n codePoint & 0x3F | 0x80\n )\n } else if (codePoint < 0x10000) {\n if ((units -= 3) < 0) break\n bytes.push(\n codePoint >> 0xC | 0xE0,\n codePoint >> 0x6 & 0x3F | 0x80,\n codePoint & 0x3F | 0x80\n )\n } else if (codePoint < 0x110000) {\n if ((units -= 4) < 0) break\n bytes.push(\n codePoint >> 0x12 | 0xF0,\n codePoint >> 0xC & 0x3F | 0x80,\n codePoint >> 0x6 & 0x3F | 0x80,\n codePoint & 0x3F | 0x80\n )\n } else {\n throw new Error('Invalid code point')\n }\n }\n\n return bytes\n}\n\nfunction asciiToBytes (str) {\n var byteArray = []\n for (var i = 0; i < str.length; ++i) {\n // Node's code seems to be doing this and not & 0x7F..\n byteArray.push(str.charCodeAt(i) & 0xFF)\n }\n return byteArray\n}\n\nfunction utf16leToBytes (str, units) {\n var c, hi, lo\n var byteArray = []\n for (var i = 0; i < str.length; ++i) {\n if ((units -= 2) < 0) break\n\n c = str.charCodeAt(i)\n hi = c >> 8\n lo = c % 256\n byteArray.push(lo)\n byteArray.push(hi)\n }\n\n return byteArray\n}\n\nfunction base64ToBytes (str) {\n return base64.toByteArray(base64clean(str))\n}\n\nfunction blitBuffer (src, dst, offset, length) {\n for (var i = 0; i < length; ++i) {\n if ((i + offset >= dst.length) || (i >= src.length)) break\n dst[i + offset] = src[i]\n }\n return i\n}\n\n// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check\n// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166\nfunction isArrayBuffer (obj) {\n return obj instanceof ArrayBuffer ||\n (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' &&\n typeof obj.byteLength === 'number')\n}\n\n// Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView`\nfunction isArrayBufferView (obj) {\n return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj)\n}\n\nfunction numberIsNaN (obj) {\n return obj !== obj // eslint-disable-line no-self-compare\n}\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Not exported from index.\r\nexport class BinaryMessageFormat {\r\n\r\n // The length prefix of binary messages is encoded as VarInt. Read the comment in\r\n // the BinaryMessageParser.TryParseMessage for details.\r\n\r\n public static write(output: Uint8Array): ArrayBuffer {\r\n // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser\r\n // in which case .byteLength does will be undefined\r\n let size = output.byteLength || output.length;\r\n const lenBuffer = [];\r\n do {\r\n let sizePart = size & 0x7f;\r\n size = size >> 7;\r\n if (size > 0) {\r\n sizePart |= 0x80;\r\n }\r\n lenBuffer.push(sizePart);\r\n }\r\n while (size > 0);\r\n\r\n // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser\r\n // in which case .byteLength does will be undefined\r\n size = output.byteLength || output.length;\r\n\r\n const buffer = new Uint8Array(lenBuffer.length + size);\r\n buffer.set(lenBuffer, 0);\r\n buffer.set(output, lenBuffer.length);\r\n return buffer.buffer;\r\n }\r\n\r\n public static parse(input: ArrayBuffer): Uint8Array[] {\r\n const result: Uint8Array[] = [];\r\n const uint8Array = new Uint8Array(input);\r\n const maxLengthPrefixSize = 5;\r\n const numBitsToShift = [0, 7, 14, 21, 28 ];\r\n\r\n for (let offset = 0; offset < input.byteLength;) {\r\n let numBytes = 0;\r\n let size = 0;\r\n let byteRead;\r\n do {\r\n byteRead = uint8Array[offset + numBytes];\r\n size = size | ((byteRead & 0x7f) << (numBitsToShift[numBytes]));\r\n numBytes++;\r\n }\r\n while (numBytes < Math.min(maxLengthPrefixSize, input.byteLength - offset) && (byteRead & 0x80) !== 0);\r\n\r\n if ((byteRead & 0x80) !== 0 && numBytes < maxLengthPrefixSize) {\r\n throw new Error(\"Cannot read message size.\");\r\n }\r\n\r\n if (numBytes === maxLengthPrefixSize && byteRead > 7) {\r\n throw new Error(\"Messages bigger than 2GB are not supported.\");\r\n }\r\n\r\n if (uint8Array.byteLength >= (offset + numBytes + size)) {\r\n // IE does not support .slice() so use subarray\r\n result.push(uint8Array.slice\r\n ? uint8Array.slice(offset + numBytes, offset + numBytes + size)\r\n : uint8Array.subarray(offset + numBytes, offset + numBytes + size));\r\n } else {\r\n throw new Error(\"Incomplete message.\");\r\n }\r\n\r\n offset = offset + numBytes + size;\r\n }\r\n\r\n return result;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { Buffer } from \"buffer\";\r\nimport * as msgpack5 from \"msgpack5\";\r\n\r\nimport { CompletionMessage, HubMessage, IHubProtocol, ILogger, InvocationMessage, LogLevel, MessageHeaders, MessageType, NullLogger, StreamInvocationMessage, StreamItemMessage, TransferFormat } from \"@aspnet/signalr\";\r\n\r\nimport { BinaryMessageFormat } from \"./BinaryMessageFormat\";\r\n\r\n// TypeDoc's @inheritDoc and @link don't work across modules :(\r\n\r\n/** Implements the MessagePack Hub Protocol */\r\nexport class MessagePackHubProtocol implements IHubProtocol {\r\n /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */\r\n public readonly name: string = \"messagepack\";\r\n /** The version of the protocol. */\r\n public readonly version: number = 1;\r\n /** The TransferFormat of the protocol. */\r\n public readonly transferFormat: TransferFormat = TransferFormat.Binary;\r\n\r\n /** Creates an array of HubMessage objects from the specified serialized representation.\r\n *\r\n * @param {ArrayBuffer} input An ArrayBuffer containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n public parseMessages(input: ArrayBuffer, logger: ILogger): HubMessage[] {\r\n // The interface does allow \"string\" to be passed in, but this implementation does not. So let's throw a useful error.\r\n if (!(input instanceof ArrayBuffer)) {\r\n throw new Error(\"Invalid input for MessagePack hub protocol. Expected an ArrayBuffer.\");\r\n }\r\n\r\n if (logger === null) {\r\n logger = NullLogger.instance;\r\n }\r\n return BinaryMessageFormat.parse(input).map((m) => this.parseMessage(m, logger));\r\n }\r\n\r\n /** Writes the specified HubMessage to an ArrayBuffer and returns it.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {ArrayBuffer} An ArrayBuffer containing the serialized representation of the message.\r\n */\r\n public writeMessage(message: HubMessage): ArrayBuffer {\r\n switch (message.type) {\r\n case MessageType.Invocation:\r\n return this.writeInvocation(message as InvocationMessage);\r\n case MessageType.StreamInvocation:\r\n return this.writeStreamInvocation(message as StreamInvocationMessage);\r\n case MessageType.StreamItem:\r\n case MessageType.Completion:\r\n throw new Error(`Writing messages of type '${message.type}' is not supported.`);\r\n default:\r\n throw new Error(\"Invalid message type.\");\r\n }\r\n }\r\n\r\n private parseMessage(input: Uint8Array, logger: ILogger): HubMessage {\r\n if (input.length === 0) {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n\r\n const msgpack = msgpack5();\r\n const properties = msgpack.decode(new Buffer(input));\r\n if (properties.length === 0 || !(properties instanceof Array)) {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n\r\n const messageType = properties[0] as MessageType;\r\n\r\n switch (messageType) {\r\n case MessageType.Invocation:\r\n return this.createInvocationMessage(this.readHeaders(properties), properties);\r\n case MessageType.StreamItem:\r\n return this.createStreamItemMessage(this.readHeaders(properties), properties);\r\n case MessageType.Completion:\r\n return this.createCompletionMessage(this.readHeaders(properties), properties);\r\n case MessageType.Ping:\r\n return this.createPingMessage(properties);\r\n case MessageType.Close:\r\n return this.createCloseMessage(properties);\r\n default:\r\n // Future protocol changes can add message types, old clients can ignore them\r\n logger.log(LogLevel.Information, \"Unknown message type '\" + messageType + \"' ignored.\");\r\n return null;\r\n }\r\n }\r\n\r\n private createCloseMessage(properties: any[]): HubMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 2) {\r\n throw new Error(\"Invalid payload for Close message.\");\r\n }\r\n\r\n return {\r\n // Close messages have no headers.\r\n error: properties[1],\r\n type: MessageType.Close,\r\n } as HubMessage;\r\n }\r\n\r\n private createPingMessage(properties: any[]): HubMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 1) {\r\n throw new Error(\"Invalid payload for Ping message.\");\r\n }\r\n\r\n return {\r\n // Ping messages have no headers.\r\n type: MessageType.Ping,\r\n } as HubMessage;\r\n }\r\n\r\n private createInvocationMessage(headers: MessageHeaders, properties: any[]): InvocationMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 5) {\r\n throw new Error(\"Invalid payload for Invocation message.\");\r\n }\r\n\r\n const invocationId = properties[2] as string;\r\n if (invocationId) {\r\n return {\r\n arguments: properties[4],\r\n headers,\r\n invocationId,\r\n target: properties[3] as string,\r\n type: MessageType.Invocation,\r\n };\r\n } else {\r\n return {\r\n arguments: properties[4],\r\n headers,\r\n target: properties[3],\r\n type: MessageType.Invocation,\r\n };\r\n }\r\n\r\n }\r\n\r\n private createStreamItemMessage(headers: MessageHeaders, properties: any[]): StreamItemMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 4) {\r\n throw new Error(\"Invalid payload for StreamItem message.\");\r\n }\r\n\r\n return {\r\n headers,\r\n invocationId: properties[2],\r\n item: properties[3],\r\n type: MessageType.StreamItem,\r\n } as StreamItemMessage;\r\n }\r\n\r\n private createCompletionMessage(headers: MessageHeaders, properties: any[]): CompletionMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 4) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n const errorResult = 1;\r\n const voidResult = 2;\r\n const nonVoidResult = 3;\r\n\r\n const resultKind = properties[3];\r\n\r\n if (resultKind !== voidResult && properties.length < 5) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n const completionMessage = {\r\n error: null as string,\r\n headers,\r\n invocationId: properties[2],\r\n result: null as any,\r\n type: MessageType.Completion,\r\n };\r\n\r\n switch (resultKind) {\r\n case errorResult:\r\n completionMessage.error = properties[4];\r\n break;\r\n case nonVoidResult:\r\n completionMessage.result = properties[4];\r\n break;\r\n }\r\n\r\n return completionMessage as CompletionMessage;\r\n }\r\n\r\n private writeInvocation(invocationMessage: InvocationMessage): ArrayBuffer {\r\n const msgpack = msgpack5();\r\n const payload = msgpack.encode([MessageType.Invocation, invocationMessage.headers || {}, invocationMessage.invocationId || null,\r\n invocationMessage.target, invocationMessage.arguments]);\r\n\r\n return BinaryMessageFormat.write(payload.slice());\r\n }\r\n\r\n private writeStreamInvocation(streamInvocationMessage: StreamInvocationMessage): ArrayBuffer {\r\n const msgpack = msgpack5();\r\n const payload = msgpack.encode([MessageType.StreamInvocation, streamInvocationMessage.headers || {}, streamInvocationMessage.invocationId,\r\n streamInvocationMessage.target, streamInvocationMessage.arguments]);\r\n\r\n return BinaryMessageFormat.write(payload.slice());\r\n }\r\n\r\n private readHeaders(properties: any): MessageHeaders {\r\n const headers: MessageHeaders = properties[1] as MessageHeaders;\r\n if (typeof headers !== \"object\") {\r\n throw new Error(\"Invalid headers.\");\r\n }\r\n return headers;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Version token that will be replaced by the prepack command\r\n/** The version of the SignalR Message Pack protocol library. */\r\nexport const VERSION = \"0.0.0-DEV_BUILD\";\r\n\r\nexport { MessagePackHubProtocol } from \"./MessagePackHubProtocol\";\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds.\r\n\r\nexport * from \"./index\";\r\n"],"names":["base64","signalr_1","buffer_1","tslib_1"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc;KACpC,EAAE,SAAS,EAAE,EAAE,EAAE,YAAY,KAAK,IAAI,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;IAC5E,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;;AAE/E,AAAO,SAAS,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE;IAC5B,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpB,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC,EAAE;IACvC,CAAC,CAAC,SAAS,GAAG,CAAC,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;C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 No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js new file mode 100644 index 000000000..1cc9a6aa7 --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js @@ -0,0 +1,16 @@ +/** + * @overview ASP.NET Core SignalR JavaScript Client. + * @version 1.0.0. + * @license + * Copyright (c) .NET Foundation. All rights reserved. + * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + */ +(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory(require("msgpack5"),require("@aspnet/signalr")):typeof define==="function"&&define.amd?define(["msgpack5","@aspnet/signalr"],factory):(global.signalR=global.signalR||{},global.signalR.protocols=global.signalR.protocols||{},global.signalR.protocols.msgpack=factory(global.msgpack5,global.signalR))})(this,function(msgpack5,signalr){"use strict";msgpack5=msgpack5&&msgpack5.hasOwnProperty("default")?msgpack5["default"]:msgpack5;signalr=signalr&&signalr.hasOwnProperty("default")?signalr["default"]:signalr;function unwrapExports(x){return x&&x.__esModule&&Object.prototype.hasOwnProperty.call(x,"default")?x["default"]:x}function createCommonjsModule(fn,module){return module={exports:{}},fn(module,module.exports),module.exports}var extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};function __extends(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}var __assign=Object.assign||function __assign(t){for(var s,i=1,n=arguments.length;i=0;i--)if(d=decorators[i])r=(c<3?d(r):c>3?d(target,key,r):d(target,key))||r;return c>3&&r&&Object.defineProperty(target,key,r),r}function __param(paramIndex,decorator){return function(target,key){decorator(target,key,paramIndex)}}function __metadata(metadataKey,metadataValue){if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(metadataKey,metadataValue)}function __awaiter(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})}function __generator(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=y[op[0]&2?"return":op[0]?"throw":"next"])&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[0,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]=o.length)o=void 0;return{value:o&&o[i++],done:!o}}}}function __read(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar}function __spread(){for(var ar=[],i=0;i1||resume(n,v)})}}function resume(n,v){try{step(g[n](v))}catch(e){settle(q[0][3],e)}}function step(r){r.value instanceof __await?Promise.resolve(r.value.v).then(fulfill,reject):settle(q[0][2],r)}function fulfill(value){resume("next",value)}function reject(value){resume("throw",value)}function settle(f,v){if(f(v),q.shift(),q.length)resume(q[0][0],q[0][1])}}function __asyncDelegator(o){var i,p;return i={},verb("next"),verb("throw",function(e){throw e}),verb("return"),i[Symbol.iterator]=function(){return this},i;function verb(n,f){if(o[n])i[n]=function(v){return(p=!p)?{value:__await(o[n](v)),done:n==="return"}:f?f(v):v}}}function __asyncValues(o){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var m=o[Symbol.asyncIterator];return m?m.call(o):typeof __values==="function"?__values(o):o[Symbol.iterator]()}function __makeTemplateObject(cooked,raw){if(Object.defineProperty){Object.defineProperty(cooked,"raw",{value:raw})}else{cooked.raw=raw}return cooked}function __importStar(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result.default=mod;return result}function __importDefault(mod){return mod&&mod.__esModule?mod:{default:mod}}var tslib_es6=Object.freeze({__extends:__extends,__assign:__assign,__rest:__rest,__decorate:__decorate,__param:__param,__metadata:__metadata,__awaiter:__awaiter,__generator:__generator,__exportStar:__exportStar,__values:__values,__read:__read,__spread:__spread,__await:__await,__asyncGenerator:__asyncGenerator,__asyncDelegator:__asyncDelegator,__asyncValues:__asyncValues,__makeTemplateObject:__makeTemplateObject,__importStar:__importStar,__importDefault:__importDefault});var byteLength_1=byteLength;var toByteArray_1=toByteArray;var fromByteArray_1=fromByteArray;var lookup=[];var revLookup=[];var Arr=typeof Uint8Array!=="undefined"?Uint8Array:Array;var code="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";for(var i=0,len=code.length;i0){throw new Error("Invalid string. Length must be a multiple of 4")}return b64[len-2]==="="?2:b64[len-1]==="="?1:0}function byteLength(b64){return b64.length*3/4-placeHoldersCount(b64)}function toByteArray(b64){var i,l,tmp,placeHolders,arr;var len=b64.length;placeHolders=placeHoldersCount(b64);arr=new Arr(len*3/4-placeHolders);l=placeHolders>0?len-4:len;var L=0;for(i=0;i>16&255;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}if(placeHolders===2){tmp=revLookup[b64.charCodeAt(i)]<<2|revLookup[b64.charCodeAt(i+1)]>>4;arr[L++]=tmp&255}else if(placeHolders===1){tmp=revLookup[b64.charCodeAt(i)]<<10|revLookup[b64.charCodeAt(i+1)]<<4|revLookup[b64.charCodeAt(i+2)]>>2;arr[L++]=tmp>>8&255;arr[L++]=tmp&255}return arr}function tripletToBase64(num){return lookup[num>>18&63]+lookup[num>>12&63]+lookup[num>>6&63]+lookup[num&63]}function encodeChunk(uint8,start,end){var tmp;var output=[];for(var i=start;ilen2?len2:i+maxChunkLength))}if(extraBytes===1){tmp=uint8[len-1];output+=lookup[tmp>>2];output+=lookup[tmp<<4&63];output+="=="}else if(extraBytes===2){tmp=(uint8[len-2]<<8)+uint8[len-1];output+=lookup[tmp>>10];output+=lookup[tmp>>4&63];output+=lookup[tmp<<2&63];output+="="}parts.push(output);return parts.join("")}var base64Js={byteLength:byteLength_1,toByteArray:toByteArray_1,fromByteArray:fromByteArray_1};var read=function(buffer,offset,isLE,mLen,nBytes){var e,m;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var nBits=-7;var i=isLE?nBytes-1:0;var d=isLE?-1:1;var s=buffer[offset+i];i+=d;e=s&(1<<-nBits)-1;s>>=-nBits;nBits+=eLen;for(;nBits>0;e=e*256+buffer[offset+i],i+=d,nBits-=8){}m=e&(1<<-nBits)-1;e>>=-nBits;nBits+=mLen;for(;nBits>0;m=m*256+buffer[offset+i],i+=d,nBits-=8){}if(e===0){e=1-eBias}else if(e===eMax){return m?NaN:(s?-1:1)*Infinity}else{m=m+Math.pow(2,mLen);e=e-eBias}return(s?-1:1)*m*Math.pow(2,e-mLen)};var write=function(buffer,value,offset,isLE,mLen,nBytes){var e,m,c;var eLen=nBytes*8-mLen-1;var eMax=(1<>1;var rt=mLen===23?Math.pow(2,-24)-Math.pow(2,-77):0;var i=isLE?0:nBytes-1;var d=isLE?1:-1;var s=value<0||value===0&&1/value<0?1:0;value=Math.abs(value);if(isNaN(value)||value===Infinity){m=isNaN(value)?1:0;e=eMax}else{e=Math.floor(Math.log(value)/Math.LN2);if(value*(c=Math.pow(2,-e))<1){e--;c*=2}if(e+eBias>=1){value+=rt/c}else{value+=rt*Math.pow(2,1-eBias)}if(value*c>=2){e++;c/=2}if(e+eBias>=eMax){m=0;e=eMax}else if(e+eBias>=1){m=(value*c-1)*Math.pow(2,mLen);e=e+eBias}else{m=value*Math.pow(2,eBias-1)*Math.pow(2,mLen);e=0}}for(;mLen>=8;buffer[offset+i]=m&255,i+=d,m/=256,mLen-=8){}e=e<0;buffer[offset+i]=e&255,i+=d,e/=256,eLen-=8){}buffer[offset+i-d]|=s*128};var ieee754={read:read,write:write};var buffer=createCommonjsModule(function(module,exports){ +/*! + * The buffer module from node.js, for the browser. + * + * @author Feross Aboukhadijeh + * @license MIT + */ +exports.Buffer=Buffer;exports.SlowBuffer=SlowBuffer;exports.INSPECT_MAX_BYTES=50;var K_MAX_LENGTH=2147483647;exports.kMaxLength=K_MAX_LENGTH;Buffer.TYPED_ARRAY_SUPPORT=typedArraySupport();if(!Buffer.TYPED_ARRAY_SUPPORT&&typeof console!=="undefined"&&typeof console.error==="function"){console.error("This browser lacks typed array (Uint8Array) support which is required by "+"`buffer` v5.x. Use `buffer` v4.x if you require old browser support.")}function typedArraySupport(){try{var arr=new Uint8Array(1);arr.__proto__={__proto__:Uint8Array.prototype,foo:function(){return 42}};return arr.foo()===42}catch(e){return false}}function createBuffer(length){if(length>K_MAX_LENGTH){throw new RangeError("Invalid typed array length")}var buf=new Uint8Array(length);buf.__proto__=Buffer.prototype;return buf}function Buffer(arg,encodingOrOffset,length){if(typeof arg==="number"){if(typeof encodingOrOffset==="string"){throw new Error("If encoding is specified then the first argument must be a string")}return allocUnsafe(arg)}return from(arg,encodingOrOffset,length)}if(typeof Symbol!=="undefined"&&Symbol.species&&Buffer[Symbol.species]===Buffer){Object.defineProperty(Buffer,Symbol.species,{value:null,configurable:true,enumerable:false,writable:false})}Buffer.poolSize=8192;function from(value,encodingOrOffset,length){if(typeof value==="number"){throw new TypeError('"value" argument must not be a number')}if(isArrayBuffer(value)){return fromArrayBuffer(value,encodingOrOffset,length)}if(typeof value==="string"){return fromString(value,encodingOrOffset)}return fromObject(value)}Buffer.from=function(value,encodingOrOffset,length){return from(value,encodingOrOffset,length)};Buffer.prototype.__proto__=Uint8Array.prototype;Buffer.__proto__=Uint8Array;function assertSize(size){if(typeof size!=="number"){throw new TypeError('"size" argument must be a number')}else if(size<0){throw new RangeError('"size" argument must not be negative')}}function alloc(size,fill,encoding){assertSize(size);if(size<=0){return createBuffer(size)}if(fill!==undefined){return typeof encoding==="string"?createBuffer(size).fill(fill,encoding):createBuffer(size).fill(fill)}return createBuffer(size)}Buffer.alloc=function(size,fill,encoding){return alloc(size,fill,encoding)};function allocUnsafe(size){assertSize(size);return createBuffer(size<0?0:checked(size)|0)}Buffer.allocUnsafe=function(size){return allocUnsafe(size)};Buffer.allocUnsafeSlow=function(size){return allocUnsafe(size)};function fromString(string,encoding){if(typeof encoding!=="string"||encoding===""){encoding="utf8"}if(!Buffer.isEncoding(encoding)){throw new TypeError('"encoding" must be a valid string encoding')}var length=byteLength(string,encoding)|0;var buf=createBuffer(length);var actual=buf.write(string,encoding);if(actual!==length){buf=buf.slice(0,actual)}return buf}function fromArrayLike(array){var length=array.length<0?0:checked(array.length)|0;var buf=createBuffer(length);for(var i=0;i=K_MAX_LENGTH){throw new RangeError("Attempt to allocate Buffer larger than maximum "+"size: 0x"+K_MAX_LENGTH.toString(16)+" bytes")}return length|0}function SlowBuffer(length){if(+length!=length){length=0}return Buffer.alloc(+length)}Buffer.isBuffer=function isBuffer(b){return b!=null&&b._isBuffer===true};Buffer.compare=function compare(a,b){if(!Buffer.isBuffer(a)||!Buffer.isBuffer(b)){throw new TypeError("Arguments must be Buffers")}if(a===b)return 0;var x=a.length;var y=b.length;for(var i=0,len=Math.min(x,y);i>>1;case"base64":return base64ToBytes(string).length;default:if(loweredCase)return utf8ToBytes(string).length;encoding=(""+encoding).toLowerCase();loweredCase=true}}}Buffer.byteLength=byteLength;function slowToString(encoding,start,end){var loweredCase=false;if(start===undefined||start<0){start=0}if(start>this.length){return""}if(end===undefined||end>this.length){end=this.length}if(end<=0){return""}end>>>=0;start>>>=0;if(end<=start){return""}if(!encoding)encoding="utf8";while(true){switch(encoding){case"hex":return hexSlice(this,start,end);case"utf8":case"utf-8":return utf8Slice(this,start,end);case"ascii":return asciiSlice(this,start,end);case"latin1":case"binary":return latin1Slice(this,start,end);case"base64":return base64Slice(this,start,end);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return utf16leSlice(this,start,end);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(encoding+"").toLowerCase();loweredCase=true}}}Buffer.prototype._isBuffer=true;function swap(b,n,m){var i=b[n];b[n]=b[m];b[m]=i}Buffer.prototype.swap16=function swap16(){var len=this.length;if(len%2!==0){throw new RangeError("Buffer size must be a multiple of 16-bits")}for(var i=0;i0){str=this.toString("hex",0,max).match(/.{2}/g).join(" ");if(this.length>max)str+=" ... "}return""};Buffer.prototype.compare=function compare(target,start,end,thisStart,thisEnd){if(!Buffer.isBuffer(target)){throw new TypeError("Argument must be a Buffer")}if(start===undefined){start=0}if(end===undefined){end=target?target.length:0}if(thisStart===undefined){thisStart=0}if(thisEnd===undefined){thisEnd=this.length}if(start<0||end>target.length||thisStart<0||thisEnd>this.length){throw new RangeError("out of range index")}if(thisStart>=thisEnd&&start>=end){return 0}if(thisStart>=thisEnd){return-1}if(start>=end){return 1}start>>>=0;end>>>=0;thisStart>>>=0;thisEnd>>>=0;if(this===target)return 0;var x=thisEnd-thisStart;var y=end-start;var len=Math.min(x,y);var thisCopy=this.slice(thisStart,thisEnd);var targetCopy=target.slice(start,end);for(var i=0;i2147483647){byteOffset=2147483647}else if(byteOffset<-2147483648){byteOffset=-2147483648}byteOffset=+byteOffset;if(numberIsNaN(byteOffset)){byteOffset=dir?0:buffer.length-1}if(byteOffset<0)byteOffset=buffer.length+byteOffset;if(byteOffset>=buffer.length){if(dir)return-1;else byteOffset=buffer.length-1}else if(byteOffset<0){if(dir)byteOffset=0;else return-1}if(typeof val==="string"){val=Buffer.from(val,encoding)}if(Buffer.isBuffer(val)){if(val.length===0){return-1}return arrayIndexOf(buffer,val,byteOffset,encoding,dir)}else if(typeof val==="number"){val=val&255;if(typeof Uint8Array.prototype.indexOf==="function"){if(dir){return Uint8Array.prototype.indexOf.call(buffer,val,byteOffset)}else{return Uint8Array.prototype.lastIndexOf.call(buffer,val,byteOffset)}}return arrayIndexOf(buffer,[val],byteOffset,encoding,dir)}throw new TypeError("val must be string, number or Buffer")}function arrayIndexOf(arr,val,byteOffset,encoding,dir){var indexSize=1;var arrLength=arr.length;var valLength=val.length;if(encoding!==undefined){encoding=String(encoding).toLowerCase();if(encoding==="ucs2"||encoding==="ucs-2"||encoding==="utf16le"||encoding==="utf-16le"){if(arr.length<2||val.length<2){return-1}indexSize=2;arrLength/=2;valLength/=2;byteOffset/=2}}function read(buf,i){if(indexSize===1){return buf[i]}else{return buf.readUInt16BE(i*indexSize)}}var i;if(dir){var foundIndex=-1;for(i=byteOffset;iarrLength)byteOffset=arrLength-valLength;for(i=byteOffset;i>=0;i--){var found=true;for(var j=0;jremaining){length=remaining}}var strLen=string.length;if(strLen%2!==0)throw new TypeError("Invalid hex string");if(length>strLen/2){length=strLen/2}for(var i=0;i>>0;if(isFinite(length)){length=length>>>0;if(encoding===undefined)encoding="utf8"}else{encoding=length;length=undefined}}else{throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported")}var remaining=this.length-offset;if(length===undefined||length>remaining)length=remaining;if(string.length>0&&(length<0||offset<0)||offset>this.length){throw new RangeError("Attempt to write outside buffer bounds")}if(!encoding)encoding="utf8";var loweredCase=false;for(;;){switch(encoding){case"hex":return hexWrite(this,string,offset,length);case"utf8":case"utf-8":return utf8Write(this,string,offset,length);case"ascii":return asciiWrite(this,string,offset,length);case"latin1":case"binary":return latin1Write(this,string,offset,length);case"base64":return base64Write(this,string,offset,length);case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return ucs2Write(this,string,offset,length);default:if(loweredCase)throw new TypeError("Unknown encoding: "+encoding);encoding=(""+encoding).toLowerCase();loweredCase=true}}};Buffer.prototype.toJSON=function toJSON(){return{type:"Buffer",data:Array.prototype.slice.call(this._arr||this,0)}};function base64Slice(buf,start,end){if(start===0&&end===buf.length){return base64Js.fromByteArray(buf)}else{return base64Js.fromByteArray(buf.slice(start,end))}}function utf8Slice(buf,start,end){end=Math.min(buf.length,end);var res=[];var i=start;while(i239?4:firstByte>223?3:firstByte>191?2:1;if(i+bytesPerSequence<=end){var secondByte,thirdByte,fourthByte,tempCodePoint;switch(bytesPerSequence){case 1:if(firstByte<128){codePoint=firstByte}break;case 2:secondByte=buf[i+1];if((secondByte&192)===128){tempCodePoint=(firstByte&31)<<6|secondByte&63;if(tempCodePoint>127){codePoint=tempCodePoint}}break;case 3:secondByte=buf[i+1];thirdByte=buf[i+2];if((secondByte&192)===128&&(thirdByte&192)===128){tempCodePoint=(firstByte&15)<<12|(secondByte&63)<<6|thirdByte&63;if(tempCodePoint>2047&&(tempCodePoint<55296||tempCodePoint>57343)){codePoint=tempCodePoint}}break;case 4:secondByte=buf[i+1];thirdByte=buf[i+2];fourthByte=buf[i+3];if((secondByte&192)===128&&(thirdByte&192)===128&&(fourthByte&192)===128){tempCodePoint=(firstByte&15)<<18|(secondByte&63)<<12|(thirdByte&63)<<6|fourthByte&63;if(tempCodePoint>65535&&tempCodePoint<1114112){codePoint=tempCodePoint}}}}if(codePoint===null){codePoint=65533;bytesPerSequence=1}else if(codePoint>65535){codePoint-=65536;res.push(codePoint>>>10&1023|55296);codePoint=56320|codePoint&1023}res.push(codePoint);i+=bytesPerSequence}return decodeCodePointsArray(res)}var MAX_ARGUMENTS_LENGTH=4096;function decodeCodePointsArray(codePoints){var len=codePoints.length;if(len<=MAX_ARGUMENTS_LENGTH){return String.fromCharCode.apply(String,codePoints)}var res="";var i=0;while(ilen)end=len;var out="";for(var i=start;ilen){start=len}if(end<0){end+=len;if(end<0)end=0}else if(end>len){end=len}if(endlength)throw new RangeError("Trying to access beyond buffer length")}Buffer.prototype.readUIntLE=function readUIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i>>0;byteLength=byteLength>>>0;if(!noAssert){checkOffset(offset,byteLength,this.length)}var val=this[offset+--byteLength];var mul=1;while(byteLength>0&&(mul*=256)){val+=this[offset+--byteLength]*mul}return val};Buffer.prototype.readUInt8=function readUInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);return this[offset]};Buffer.prototype.readUInt16LE=function readUInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]|this[offset+1]<<8};Buffer.prototype.readUInt16BE=function readUInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);return this[offset]<<8|this[offset+1]};Buffer.prototype.readUInt32LE=function readUInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return(this[offset]|this[offset+1]<<8|this[offset+2]<<16)+this[offset+3]*16777216};Buffer.prototype.readUInt32BE=function readUInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]*16777216+(this[offset+1]<<16|this[offset+2]<<8|this[offset+3])};Buffer.prototype.readIntLE=function readIntLE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var val=this[offset];var mul=1;var i=0;while(++i=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readIntBE=function readIntBE(offset,byteLength,noAssert){offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert)checkOffset(offset,byteLength,this.length);var i=byteLength;var mul=1;var val=this[offset+--i];while(i>0&&(mul*=256)){val+=this[offset+--i]*mul}mul*=128;if(val>=mul)val-=Math.pow(2,8*byteLength);return val};Buffer.prototype.readInt8=function readInt8(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,1,this.length);if(!(this[offset]&128))return this[offset];return(255-this[offset]+1)*-1};Buffer.prototype.readInt16LE=function readInt16LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset]|this[offset+1]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt16BE=function readInt16BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,2,this.length);var val=this[offset+1]|this[offset]<<8;return val&32768?val|4294901760:val};Buffer.prototype.readInt32LE=function readInt32LE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]|this[offset+1]<<8|this[offset+2]<<16|this[offset+3]<<24};Buffer.prototype.readInt32BE=function readInt32BE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return this[offset]<<24|this[offset+1]<<16|this[offset+2]<<8|this[offset+3]};Buffer.prototype.readFloatLE=function readFloatLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,true,23,4)};Buffer.prototype.readFloatBE=function readFloatBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,4,this.length);return ieee754.read(this,offset,false,23,4)};Buffer.prototype.readDoubleLE=function readDoubleLE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,true,52,8)};Buffer.prototype.readDoubleBE=function readDoubleBE(offset,noAssert){offset=offset>>>0;if(!noAssert)checkOffset(offset,8,this.length);return ieee754.read(this,offset,false,52,8)};function checkInt(buf,value,offset,ext,max,min){if(!Buffer.isBuffer(buf))throw new TypeError('"buffer" argument must be a Buffer instance');if(value>max||valuebuf.length)throw new RangeError("Index out of range")}Buffer.prototype.writeUIntLE=function writeUIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var mul=1;var i=0;this[offset]=value&255;while(++i>>0;byteLength=byteLength>>>0;if(!noAssert){var maxBytes=Math.pow(2,8*byteLength)-1;checkInt(this,value,offset,byteLength,maxBytes,0)}var i=byteLength-1;var mul=1;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){this[offset+i]=value/mul&255}return offset+byteLength};Buffer.prototype.writeUInt8=function writeUInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,255,0);this[offset]=value&255;return offset+1};Buffer.prototype.writeUInt16LE=function writeUInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeUInt16BE=function writeUInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,65535,0);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeUInt32LE=function writeUInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset+3]=value>>>24;this[offset+2]=value>>>16;this[offset+1]=value>>>8;this[offset]=value&255;return offset+4};Buffer.prototype.writeUInt32BE=function writeUInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,4294967295,0);this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};Buffer.prototype.writeIntLE=function writeIntLE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=0;var mul=1;var sub=0;this[offset]=value&255;while(++i>0)-sub&255}return offset+byteLength};Buffer.prototype.writeIntBE=function writeIntBE(value,offset,byteLength,noAssert){value=+value;offset=offset>>>0;if(!noAssert){var limit=Math.pow(2,8*byteLength-1);checkInt(this,value,offset,byteLength,limit-1,-limit)}var i=byteLength-1;var mul=1;var sub=0;this[offset+i]=value&255;while(--i>=0&&(mul*=256)){if(value<0&&sub===0&&this[offset+i+1]!==0){sub=1}this[offset+i]=(value/mul>>0)-sub&255}return offset+byteLength};Buffer.prototype.writeInt8=function writeInt8(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,1,127,-128);if(value<0)value=255+value+1;this[offset]=value&255;return offset+1};Buffer.prototype.writeInt16LE=function writeInt16LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value&255;this[offset+1]=value>>>8;return offset+2};Buffer.prototype.writeInt16BE=function writeInt16BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,2,32767,-32768);this[offset]=value>>>8;this[offset+1]=value&255;return offset+2};Buffer.prototype.writeInt32LE=function writeInt32LE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);this[offset]=value&255;this[offset+1]=value>>>8;this[offset+2]=value>>>16;this[offset+3]=value>>>24;return offset+4};Buffer.prototype.writeInt32BE=function writeInt32BE(value,offset,noAssert){value=+value;offset=offset>>>0;if(!noAssert)checkInt(this,value,offset,4,2147483647,-2147483648);if(value<0)value=4294967295+value+1;this[offset]=value>>>24;this[offset+1]=value>>>16;this[offset+2]=value>>>8;this[offset+3]=value&255;return offset+4};function checkIEEE754(buf,value,offset,ext,max,min){if(offset+ext>buf.length)throw new RangeError("Index out of range");if(offset<0)throw new RangeError("Index out of range")}function writeFloat(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,4,3.4028234663852886e38,-3.4028234663852886e38)}ieee754.write(buf,value,offset,littleEndian,23,4);return offset+4}Buffer.prototype.writeFloatLE=function writeFloatLE(value,offset,noAssert){return writeFloat(this,value,offset,true,noAssert)};Buffer.prototype.writeFloatBE=function writeFloatBE(value,offset,noAssert){return writeFloat(this,value,offset,false,noAssert)};function writeDouble(buf,value,offset,littleEndian,noAssert){value=+value;offset=offset>>>0;if(!noAssert){checkIEEE754(buf,value,offset,8,1.7976931348623157e308,-1.7976931348623157e308)}ieee754.write(buf,value,offset,littleEndian,52,8);return offset+8}Buffer.prototype.writeDoubleLE=function writeDoubleLE(value,offset,noAssert){return writeDouble(this,value,offset,true,noAssert)};Buffer.prototype.writeDoubleBE=function writeDoubleBE(value,offset,noAssert){return writeDouble(this,value,offset,false,noAssert)};Buffer.prototype.copy=function copy(target,targetStart,start,end){if(!start)start=0;if(!end&&end!==0)end=this.length;if(targetStart>=target.length)targetStart=target.length;if(!targetStart)targetStart=0;if(end>0&&end=this.length)throw new RangeError("sourceStart out of bounds");if(end<0)throw new RangeError("sourceEnd out of bounds");if(end>this.length)end=this.length;if(target.length-targetStart=0;--i){target[i+targetStart]=this[i+start]}}else if(len<1e3){for(i=0;i>>0;end=end===undefined?this.length:end>>>0;if(!val)val=0;var i;if(typeof val==="number"){for(i=start;i55295&&codePoint<57344){if(!leadSurrogate){if(codePoint>56319){if((units-=3)>-1)bytes.push(239,191,189);continue}else if(i+1===length){if((units-=3)>-1)bytes.push(239,191,189);continue}leadSurrogate=codePoint;continue}if(codePoint<56320){if((units-=3)>-1)bytes.push(239,191,189);leadSurrogate=codePoint;continue}codePoint=(leadSurrogate-55296<<10|codePoint-56320)+65536}else if(leadSurrogate){if((units-=3)>-1)bytes.push(239,191,189)}leadSurrogate=null;if(codePoint<128){if((units-=1)<0)break;bytes.push(codePoint)}else if(codePoint<2048){if((units-=2)<0)break;bytes.push(codePoint>>6|192,codePoint&63|128)}else if(codePoint<65536){if((units-=3)<0)break;bytes.push(codePoint>>12|224,codePoint>>6&63|128,codePoint&63|128)}else if(codePoint<1114112){if((units-=4)<0)break;bytes.push(codePoint>>18|240,codePoint>>12&63|128,codePoint>>6&63|128,codePoint&63|128)}else{throw new Error("Invalid code point")}}return bytes}function asciiToBytes(str){var byteArray=[];for(var i=0;i>8;lo=c%256;byteArray.push(lo);byteArray.push(hi)}return byteArray}function base64ToBytes(str){return base64Js.toByteArray(base64clean(str))}function blitBuffer(src,dst,offset,length){for(var i=0;i=dst.length||i>=src.length)break;dst[i+offset]=src[i]}return i}function isArrayBuffer(obj){return obj instanceof ArrayBuffer||obj!=null&&obj.constructor!=null&&obj.constructor.name==="ArrayBuffer"&&typeof obj.byteLength==="number"}function isArrayBufferView(obj){return typeof ArrayBuffer.isView==="function"&&ArrayBuffer.isView(obj)}function numberIsNaN(obj){return obj!==obj}});var buffer_1=buffer.Buffer;var buffer_2=buffer.SlowBuffer;var buffer_3=buffer.INSPECT_MAX_BYTES;var buffer_4=buffer.kMaxLength;var BinaryMessageFormat_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var BinaryMessageFormat=function(){function BinaryMessageFormat(){}BinaryMessageFormat.write=function(output){var size=output.byteLength||output.length;var lenBuffer=[];do{var sizePart=size&127;size=size>>7;if(size>0){sizePart|=128}lenBuffer.push(sizePart)}while(size>0);size=output.byteLength||output.length;var buffer=new Uint8Array(lenBuffer.length+size);buffer.set(lenBuffer,0);buffer.set(output,lenBuffer.length);return buffer.buffer};BinaryMessageFormat.parse=function(input){var result=[];var uint8Array=new Uint8Array(input);var maxLengthPrefixSize=5;var numBitsToShift=[0,7,14,21,28];for(var offset=0;offset7){throw new Error("Messages bigger than 2GB are not supported.")}if(uint8Array.byteLength>=offset+numBytes+size){result.push(uint8Array.slice?uint8Array.slice(offset+numBytes,offset+numBytes+size):uint8Array.subarray(offset+numBytes,offset+numBytes+size))}else{throw new Error("Incomplete message.")}offset=offset+numBytes+size}return result};return BinaryMessageFormat}();exports.BinaryMessageFormat=BinaryMessageFormat});unwrapExports(BinaryMessageFormat_1);var BinaryMessageFormat_2=BinaryMessageFormat_1.BinaryMessageFormat;var MessagePackHubProtocol_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var MessagePackHubProtocol=function(){function MessagePackHubProtocol(){this.name="messagepack";this.version=1;this.transferFormat=signalr.TransferFormat.Binary}MessagePackHubProtocol.prototype.parseMessages=function(input,logger){var _this=this;if(!(input instanceof ArrayBuffer)){throw new Error("Invalid input for MessagePack hub protocol. Expected an ArrayBuffer.")}if(logger===null){logger=signalr.NullLogger.instance}return BinaryMessageFormat_1.BinaryMessageFormat.parse(input).map(function(m){return _this.parseMessage(m,logger)})};MessagePackHubProtocol.prototype.writeMessage=function(message){switch(message.type){case signalr.MessageType.Invocation:return this.writeInvocation(message);case signalr.MessageType.StreamInvocation:return this.writeStreamInvocation(message);case signalr.MessageType.StreamItem:case signalr.MessageType.Completion:throw new Error("Writing messages of type '"+message.type+"' is not supported.");default:throw new Error("Invalid message type.")}};MessagePackHubProtocol.prototype.parseMessage=function(input,logger){if(input.length===0){throw new Error("Invalid payload.")}var msgpack=msgpack5();var properties=msgpack.decode(new buffer.Buffer(input));if(properties.length===0||!(properties instanceof Array)){throw new Error("Invalid payload.")}var messageType=properties[0];switch(messageType){case signalr.MessageType.Invocation:return this.createInvocationMessage(this.readHeaders(properties),properties);case signalr.MessageType.StreamItem:return this.createStreamItemMessage(this.readHeaders(properties),properties);case signalr.MessageType.Completion:return this.createCompletionMessage(this.readHeaders(properties),properties);case signalr.MessageType.Ping:return this.createPingMessage(properties);case signalr.MessageType.Close:return this.createCloseMessage(properties);default:logger.log(signalr.LogLevel.Information,"Unknown message type '"+messageType+"' ignored.");return null}};MessagePackHubProtocol.prototype.createCloseMessage=function(properties){if(properties.length<2){throw new Error("Invalid payload for Close message.")}return{error:properties[1],type:signalr.MessageType.Close}};MessagePackHubProtocol.prototype.createPingMessage=function(properties){if(properties.length<1){throw new Error("Invalid payload for Ping message.")}return{type:signalr.MessageType.Ping}};MessagePackHubProtocol.prototype.createInvocationMessage=function(headers,properties){if(properties.length<5){throw new Error("Invalid payload for Invocation message.")}var invocationId=properties[2];if(invocationId){return{arguments:properties[4],headers:headers,invocationId:invocationId,target:properties[3],type:signalr.MessageType.Invocation}}else{return{arguments:properties[4],headers:headers,target:properties[3],type:signalr.MessageType.Invocation}}};MessagePackHubProtocol.prototype.createStreamItemMessage=function(headers,properties){if(properties.length<4){throw new Error("Invalid payload for StreamItem message.")}return{headers:headers,invocationId:properties[2],item:properties[3],type:signalr.MessageType.StreamItem}};MessagePackHubProtocol.prototype.createCompletionMessage=function(headers,properties){if(properties.length<4){throw new Error("Invalid payload for Completion message.")}var errorResult=1;var voidResult=2;var nonVoidResult=3;var resultKind=properties[3];if(resultKind!==voidResult&&properties.length<5){throw new Error("Invalid payload for Completion message.")}var completionMessage={error:null,headers:headers,invocationId:properties[2],result:null,type:signalr.MessageType.Completion};switch(resultKind){case errorResult:completionMessage.error=properties[4];break;case nonVoidResult:completionMessage.result=properties[4];break}return completionMessage};MessagePackHubProtocol.prototype.writeInvocation=function(invocationMessage){var msgpack=msgpack5();var payload=msgpack.encode([signalr.MessageType.Invocation,invocationMessage.headers||{},invocationMessage.invocationId||null,invocationMessage.target,invocationMessage.arguments]);return BinaryMessageFormat_1.BinaryMessageFormat.write(payload.slice())};MessagePackHubProtocol.prototype.writeStreamInvocation=function(streamInvocationMessage){var msgpack=msgpack5();var payload=msgpack.encode([signalr.MessageType.StreamInvocation,streamInvocationMessage.headers||{},streamInvocationMessage.invocationId,streamInvocationMessage.target,streamInvocationMessage.arguments]);return BinaryMessageFormat_1.BinaryMessageFormat.write(payload.slice())};MessagePackHubProtocol.prototype.readHeaders=function(properties){var headers=properties[1];if(typeof headers!=="object"){throw new Error("Invalid headers.")}return headers};return MessagePackHubProtocol}();exports.MessagePackHubProtocol=MessagePackHubProtocol});unwrapExports(MessagePackHubProtocol_1);var MessagePackHubProtocol_2=MessagePackHubProtocol_1.MessagePackHubProtocol;var cjs=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});exports.VERSION="0.0.0-DEV_BUILD";exports.MessagePackHubProtocol=MessagePackHubProtocol_1.MessagePackHubProtocol});unwrapExports(cjs);var cjs_1=cjs.VERSION;var cjs_2=cjs.MessagePackHubProtocol;var browserIndex=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});tslib_es6.__exportStar(cjs,exports)});var browserIndex$1=unwrapExports(browserIndex);return browserIndex$1}); +//# sourceMappingURL=signalr-protocol-msgpack.min.js.map \ No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js.map b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js.map new file mode 100644 index 000000000..f8a59eee9 --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/signalr-protocol-msgpack.min.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../../node_modules/tslib/tslib.es6.js","../../node_modules/base64-js/index.js","../../node_modules/ieee754/index.js","../../node_modules/buffer/index.js","../../src/BinaryMessageFormat.ts","../../src/MessagePackHubProtocol.ts","../../src/index.ts","../../src/browser-index.ts"],"names":["extendStatics","Object","setPrototypeOf","__proto__","Array","d","b","p","hasOwnProperty","__extends","__","this","constructor","prototype","create","__assign","assign","t","s","i","n","arguments","length","call","__rest","e","indexOf","getOwnPropertySymbols","__decorate","decorators","target","key","desc","c","r","getOwnPropertyDescriptor","Reflect","decorate","defineProperty","__param","paramIndex","decorator","__metadata","metadataKey","metadataValue","metadata","__awaiter","thisArg","_arguments","P","generator","Promise","resolve","reject","fulfilled","value","step","next","rejected","result","done","then","apply","__generator","body","_","label","sent","trys","ops","f","y","g","verb","throw","return","Symbol","iterator","v","op","TypeError","pop","push","__exportStar","m","exports","__values","o","__read","ar","error","__spread","concat","__await","__asyncGenerator","asyncIterator","q","a","resume","settle","fulfill","shift","__asyncDelegator","__asyncValues","__makeTemplateObject","cooked","raw","__importStar","mod","__esModule","k","default","__importDefault","byteLength_1","byteLength","toByteArray_1","toByteArray","fromByteArray_1","fromByteArray","lookup","revLookup","Arr","Uint8Array","code","len","charCodeAt","placeHoldersCount","b64","Error","l","tmp","placeHolders","arr","L","tripletToBase64","num","encodeChunk","uint8","start","end","output","join","extraBytes","parts","maxChunkLength","len2","read","buffer","offset","isLE","mLen","nBytes","eLen","eMax","eBias","nBits","NaN","Infinity","Math","pow","write","rt","abs","isNaN","floor","log","LN2","Buffer","SlowBuffer","INSPECT_MAX_BYTES","K_MAX_LENGTH","kMaxLength","TYPED_ARRAY_SUPPORT","typedArraySupport","console","foo","createBuffer","RangeError","buf","arg","encodingOrOffset","allocUnsafe","from","species","configurable","enumerable","writable","poolSize","isArrayBuffer","fromArrayBuffer","fromString","fromObject","assertSize","size","alloc","fill","encoding","undefined","checked","allocUnsafeSlow","string","isEncoding","actual","slice","fromArrayLike","array","byteOffset","obj","isBuffer","copy","isArrayBufferView","numberIsNaN","type","isArray","data","toString","_isBuffer","compare","x","min","String","toLowerCase","list","pos","loweredCase","utf8ToBytes","base64ToBytes","slowToString","hexSlice","utf8Slice","asciiSlice","latin1Slice","base64Slice","utf16leSlice","swap","swap16","swap32","swap64","equals","inspect","str","max","match","thisStart","thisEnd","thisCopy","targetCopy","bidirectionalIndexOf","val","dir","arrayIndexOf","lastIndexOf","indexSize","arrLength","valLength","readUInt16BE","foundIndex","found","j","includes","hexWrite","Number","remaining","strLen","parsed","parseInt","substr","utf8Write","blitBuffer","asciiWrite","asciiToBytes","latin1Write","base64Write","ucs2Write","utf16leToBytes","isFinite","toJSON","_arr","base64","res","firstByte","codePoint","bytesPerSequence","secondByte","thirdByte","fourthByte","tempCodePoint","decodeCodePointsArray","MAX_ARGUMENTS_LENGTH","codePoints","fromCharCode","ret","out","toHex","bytes","newBuf","subarray","checkOffset","ext","readUIntLE","noAssert","mul","readUIntBE","readUInt8","readUInt16LE","readUInt32LE","readUInt32BE","readIntLE","readIntBE","readInt8","readInt16LE","readInt16BE","readInt32LE","readInt32BE","readFloatLE","ieee754","readFloatBE","readDoubleLE","readDoubleBE","checkInt","writeUIntLE","maxBytes","writeUIntBE","writeUInt8","writeUInt16LE","writeUInt16BE","writeUInt32LE","writeUInt32BE","writeIntLE","limit","sub","writeIntBE","writeInt8","writeInt16LE","writeInt16BE","writeInt32LE","writeInt32BE","checkIEEE754","writeFloat","littleEndian","writeFloatLE","writeFloatBE","writeDouble","writeDoubleLE","writeDoubleBE","targetStart","set","INVALID_BASE64_RE","base64clean","trim","replace","units","leadSurrogate","byteArray","hi","lo","src","dst","ArrayBuffer","name","isView","BinaryMessageFormat","lenBuffer","sizePart","parse","input","uint8Array","maxLengthPrefixSize","numBitsToShift","numBytes","byteRead","MessagePackHubProtocol","version","transferFormat","signalr_1","Binary","parseMessages","logger","_this","instance","BinaryMessageFormat_1","map","parseMessage","writeMessage","message","Invocation","writeInvocation","StreamInvocation","writeStreamInvocation","StreamItem","Completion","msgpack","msgpack5","properties","decode","buffer_1","messageType","createInvocationMessage","readHeaders","createStreamItemMessage","createCompletionMessage","Ping","createPingMessage","Close","createCloseMessage","Information","headers","invocationId","item","errorResult","voidResult","nonVoidResult","resultKind","completionMessage","invocationMessage","payload","encode","streamInvocationMessage","VERSION","MessagePackHubProtocol_1","tslib_1"],"mappings":";;;;;;;i0BAgBA,IAAIA,cAAgBC,OAAOC,iBACpBC,wBAA2BC,OAAS,SAAUC,EAAGC,GAAKD,EAAEF,UAAYG,IACvE,SAAUD,EAAGC,GAAK,IAAK,IAAIC,KAAKD,EAAG,GAAIA,EAAEE,eAAeD,GAAIF,EAAEE,GAAKD,EAAEC,IAEzE,SAAgBE,UAAUJ,EAAGC,GACzBN,cAAcK,EAAGC,GACjB,SAASI,KAAOC,KAAKC,YAAcP,EACnCA,EAAEQ,UAAYP,IAAM,KAAOL,OAAOa,OAAOR,IAAMI,GAAGG,UAAYP,EAAEO,UAAW,IAAIH,IAGnF,IAAWK,SAAWd,OAAOe,QAAU,SAASD,SAASE,GACrD,IAAK,IAAIC,EAAGC,EAAI,EAA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OuW,sBAAAnB,oBAAoBzM,MAAM+P,QAAQpN,UAGrCwK,uBAAAtW,UAAAuX,sBAAR,SAA8B6B,yBAC1B,IAAM1B,QAAUC,WAChB,IAAMuB,QAAUxB,QAAQyB,QAAQ1C,QAAAA,YAAYa,iBAAkB8B,wBAAwBX,YAAeW,wBAAwBV,aAC7HU,wBAAwBnY,OAAQmY,wBAAwB5Y,YAExD,OAAOuW,sBAAAnB,oBAAoBzM,MAAM+P,QAAQpN,UAGrCwK,uBAAAtW,UAAAiY,YAAR,SAAoBL,YAChB,IAAMa,QAA0Bb,WAAW,GAC3C,UAAWa,UAAY,SAAU,CAC7B,MAAM,IAAItR,MAAM,oBAEpB,OAAOsR,SAEf,OAAAnC,uBAvMA,GAAa9R,QAAA8R,uBAAAA,6PCRA9R,QAAA6U,QAAU,kBAEd7U,QAAA8R,uBAAAgD,yBAAAhD,+NCFTiD,UAAAA,aAAAA,IAAAA","sourcesContent":["/*! *****************************************************************************\r\nCopyright (c) Microsoft Corporation. All rights reserved.\r\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use\r\nthis file except in compliance with the License. You may obtain a copy of the\r\nLicense at http://www.apache.org/licenses/LICENSE-2.0\r\n\r\nTHIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r\nKIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED\r\nWARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,\r\nMERCHANTABLITY OR NON-INFRINGEMENT.\r\n\r\nSee the Apache Version 2.0 License for specific language governing permissions\r\nand limitations under the License.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\r\n\r\nexport function __extends(d, b) {\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)\r\n t[p[i]] = s[p[i]];\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport function __exportStar(m, exports) {\r\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\r\n}\r\n\r\nexport function __values(o) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator], i = 0;\r\n if (m) return m.call(o);\r\n return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; }; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator];\r\n return m ? m.call(o) : typeof __values === \"function\" ? __values(o) : o[Symbol.iterator]();\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];\r\n result.default = mod;\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n","'use strict'\n\nexports.byteLength = byteLength\nexports.toByteArray = toByteArray\nexports.fromByteArray = fromByteArray\n\nvar lookup = []\nvar revLookup = []\nvar Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array\n\nvar code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'\nfor (var i = 0, len = code.length; i < len; ++i) {\n lookup[i] = code[i]\n revLookup[code.charCodeAt(i)] = i\n}\n\nrevLookup['-'.charCodeAt(0)] = 62\nrevLookup['_'.charCodeAt(0)] = 63\n\nfunction placeHoldersCount (b64) {\n var len = b64.length\n if (len % 4 > 0) {\n throw new Error('Invalid string. Length must be a multiple of 4')\n }\n\n // the number of equal signs (place holders)\n // if there are two placeholders, than the two characters before it\n // represent one byte\n // if there is only one, then the three characters before it represent 2 bytes\n // this is just a cheap hack to not do indexOf twice\n return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0\n}\n\nfunction byteLength (b64) {\n // base64 is 4/3 + up to two characters of the original data\n return (b64.length * 3 / 4) - placeHoldersCount(b64)\n}\n\nfunction toByteArray (b64) {\n var i, l, tmp, placeHolders, arr\n var len = b64.length\n placeHolders = placeHoldersCount(b64)\n\n arr = new Arr((len * 3 / 4) - placeHolders)\n\n // if there are placeholders, only get up to the last complete 4 chars\n l = placeHolders > 0 ? len - 4 : len\n\n var L = 0\n\n for (i = 0; i < l; i += 4) {\n tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]\n arr[L++] = (tmp >> 16) & 0xFF\n arr[L++] = (tmp >> 8) & 0xFF\n arr[L++] = tmp & 0xFF\n }\n\n if (placeHolders === 2) {\n tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)\n arr[L++] = tmp & 0xFF\n } else if (placeHolders === 1) {\n tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)\n arr[L++] = (tmp >> 8) & 0xFF\n arr[L++] = tmp & 0xFF\n }\n\n return arr\n}\n\nfunction tripletToBase64 (num) {\n return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]\n}\n\nfunction encodeChunk (uint8, start, end) {\n var tmp\n var output = []\n for (var i = start; i < end; i += 3) {\n tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])\n output.push(tripletToBase64(tmp))\n }\n return output.join('')\n}\n\nfunction fromByteArray (uint8) {\n var tmp\n var len = uint8.length\n var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes\n var output = ''\n var parts = []\n var maxChunkLength = 16383 // must be multiple of 3\n\n // go through the array every three bytes, we'll deal with trailing stuff later\n for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {\n parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))\n }\n\n // pad the end with zeros, but make sure to not forget the extra bytes\n if (extraBytes === 1) {\n tmp = uint8[len - 1]\n output += lookup[tmp >> 2]\n output += lookup[(tmp << 4) & 0x3F]\n output += '=='\n } else if (extraBytes === 2) {\n tmp = (uint8[len - 2] << 8) + (uint8[len - 1])\n output += lookup[tmp >> 10]\n output += lookup[(tmp >> 4) & 0x3F]\n output += lookup[(tmp << 2) & 0x3F]\n output += '='\n }\n\n parts.push(output)\n\n return parts.join('')\n}\n","exports.read = function (buffer, offset, isLE, mLen, nBytes) {\n var e, m\n var eLen = nBytes * 8 - mLen - 1\n var eMax = (1 << eLen) - 1\n var eBias = eMax >> 1\n var nBits = -7\n var i = isLE ? (nBytes - 1) : 0\n var d = isLE ? -1 : 1\n var s = buffer[offset + i]\n\n i += d\n\n e = s & ((1 << (-nBits)) - 1)\n s >>= (-nBits)\n nBits += eLen\n for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}\n\n m = e & ((1 << (-nBits)) - 1)\n e >>= (-nBits)\n nBits += mLen\n for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}\n\n if (e === 0) {\n e = 1 - eBias\n } else if (e === eMax) {\n return m ? NaN : ((s ? -1 : 1) * Infinity)\n } else {\n m = m + Math.pow(2, mLen)\n e = e - eBias\n }\n return (s ? -1 : 1) * m * Math.pow(2, e - mLen)\n}\n\nexports.write = function (buffer, value, offset, isLE, mLen, nBytes) {\n var e, m, c\n var eLen = nBytes * 8 - mLen - 1\n var eMax = (1 << eLen) - 1\n var eBias = eMax >> 1\n var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)\n var i = isLE ? 0 : (nBytes - 1)\n var d = isLE ? 1 : -1\n var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0\n\n value = Math.abs(value)\n\n if (isNaN(value) || value === Infinity) {\n m = isNaN(value) ? 1 : 0\n e = eMax\n } else {\n e = Math.floor(Math.log(value) / Math.LN2)\n if (value * (c = Math.pow(2, -e)) < 1) {\n e--\n c *= 2\n }\n if (e + eBias >= 1) {\n value += rt / c\n } else {\n value += rt * Math.pow(2, 1 - eBias)\n }\n if (value * c >= 2) {\n e++\n c /= 2\n }\n\n if (e + eBias >= eMax) {\n m = 0\n e = eMax\n } else if (e + eBias >= 1) {\n m = (value * c - 1) * Math.pow(2, mLen)\n e = e + eBias\n } else {\n m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)\n e = 0\n }\n }\n\n for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}\n\n e = (e << mLen) | m\n eLen += mLen\n for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}\n\n buffer[offset + i - d] |= s * 128\n}\n","/*!\n * The buffer module from node.js, for the browser.\n *\n * @author Feross Aboukhadijeh \n * @license MIT\n */\n/* eslint-disable no-proto */\n\n'use strict'\n\nvar base64 = require('base64-js')\nvar ieee754 = require('ieee754')\n\nexports.Buffer = Buffer\nexports.SlowBuffer = SlowBuffer\nexports.INSPECT_MAX_BYTES = 50\n\nvar K_MAX_LENGTH = 0x7fffffff\nexports.kMaxLength = K_MAX_LENGTH\n\n/**\n * If `Buffer.TYPED_ARRAY_SUPPORT`:\n * === true Use Uint8Array implementation (fastest)\n * === false Print warning and recommend using `buffer` v4.x which has an Object\n * implementation (most compatible, even IE6)\n *\n * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,\n * Opera 11.6+, iOS 4.2+.\n *\n * We report that the browser does not support typed arrays if the are not subclassable\n * using __proto__. Firefox 4-29 lacks support for adding new properties to `Uint8Array`\n * (See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438). IE 10 lacks support\n * for __proto__ and has a buggy typed array implementation.\n */\nBuffer.TYPED_ARRAY_SUPPORT = typedArraySupport()\n\nif (!Buffer.TYPED_ARRAY_SUPPORT && typeof console !== 'undefined' &&\n typeof console.error === 'function') {\n console.error(\n 'This browser lacks typed array (Uint8Array) support which is required by ' +\n '`buffer` v5.x. Use `buffer` v4.x if you require old browser support.'\n )\n}\n\nfunction typedArraySupport () {\n // Can typed array instances can be augmented?\n try {\n var arr = new Uint8Array(1)\n arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }}\n return arr.foo() === 42\n } catch (e) {\n return false\n }\n}\n\nfunction createBuffer (length) {\n if (length > K_MAX_LENGTH) {\n throw new RangeError('Invalid typed array length')\n }\n // Return an augmented `Uint8Array` instance\n var buf = new Uint8Array(length)\n buf.__proto__ = Buffer.prototype\n return buf\n}\n\n/**\n * The Buffer constructor returns instances of `Uint8Array` that have their\n * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of\n * `Uint8Array`, so the returned instances will have all the node `Buffer` methods\n * and the `Uint8Array` methods. Square bracket notation works as expected -- it\n * returns a single octet.\n *\n * The `Uint8Array` prototype remains unmodified.\n */\n\nfunction Buffer (arg, encodingOrOffset, length) {\n // Common case.\n if (typeof arg === 'number') {\n if (typeof encodingOrOffset === 'string') {\n throw new Error(\n 'If encoding is specified then the first argument must be a string'\n )\n }\n return allocUnsafe(arg)\n }\n return from(arg, encodingOrOffset, length)\n}\n\n// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97\nif (typeof Symbol !== 'undefined' && Symbol.species &&\n Buffer[Symbol.species] === Buffer) {\n Object.defineProperty(Buffer, Symbol.species, {\n value: null,\n configurable: true,\n enumerable: false,\n writable: false\n })\n}\n\nBuffer.poolSize = 8192 // not used by this implementation\n\nfunction from (value, encodingOrOffset, length) {\n if (typeof value === 'number') {\n throw new TypeError('\"value\" argument must not be a number')\n }\n\n if (isArrayBuffer(value)) {\n return fromArrayBuffer(value, encodingOrOffset, length)\n }\n\n if (typeof value === 'string') {\n return fromString(value, encodingOrOffset)\n }\n\n return fromObject(value)\n}\n\n/**\n * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError\n * if value is a number.\n * Buffer.from(str[, encoding])\n * Buffer.from(array)\n * Buffer.from(buffer)\n * Buffer.from(arrayBuffer[, byteOffset[, length]])\n **/\nBuffer.from = function (value, encodingOrOffset, length) {\n return from(value, encodingOrOffset, length)\n}\n\n// Note: Change prototype *after* Buffer.from is defined to workaround Chrome bug:\n// https://github.com/feross/buffer/pull/148\nBuffer.prototype.__proto__ = Uint8Array.prototype\nBuffer.__proto__ = Uint8Array\n\nfunction assertSize (size) {\n if (typeof size !== 'number') {\n throw new TypeError('\"size\" argument must be a number')\n } else if (size < 0) {\n throw new RangeError('\"size\" argument must not be negative')\n }\n}\n\nfunction alloc (size, fill, encoding) {\n assertSize(size)\n if (size <= 0) {\n return createBuffer(size)\n }\n if (fill !== undefined) {\n // Only pay attention to encoding if it's a string. This\n // prevents accidentally sending in a number that would\n // be interpretted as a start offset.\n return typeof encoding === 'string'\n ? createBuffer(size).fill(fill, encoding)\n : createBuffer(size).fill(fill)\n }\n return createBuffer(size)\n}\n\n/**\n * Creates a new filled Buffer instance.\n * alloc(size[, fill[, encoding]])\n **/\nBuffer.alloc = function (size, fill, encoding) {\n return alloc(size, fill, encoding)\n}\n\nfunction allocUnsafe (size) {\n assertSize(size)\n return createBuffer(size < 0 ? 0 : checked(size) | 0)\n}\n\n/**\n * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.\n * */\nBuffer.allocUnsafe = function (size) {\n return allocUnsafe(size)\n}\n/**\n * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.\n */\nBuffer.allocUnsafeSlow = function (size) {\n return allocUnsafe(size)\n}\n\nfunction fromString (string, encoding) {\n if (typeof encoding !== 'string' || encoding === '') {\n encoding = 'utf8'\n }\n\n if (!Buffer.isEncoding(encoding)) {\n throw new TypeError('\"encoding\" must be a valid string encoding')\n }\n\n var length = byteLength(string, encoding) | 0\n var buf = createBuffer(length)\n\n var actual = buf.write(string, encoding)\n\n if (actual !== length) {\n // Writing a hex string, for example, that contains invalid characters will\n // cause everything after the first invalid character to be ignored. (e.g.\n // 'abxxcd' will be treated as 'ab')\n buf = buf.slice(0, actual)\n }\n\n return buf\n}\n\nfunction fromArrayLike (array) {\n var length = array.length < 0 ? 0 : checked(array.length) | 0\n var buf = createBuffer(length)\n for (var i = 0; i < length; i += 1) {\n buf[i] = array[i] & 255\n }\n return buf\n}\n\nfunction fromArrayBuffer (array, byteOffset, length) {\n if (byteOffset < 0 || array.byteLength < byteOffset) {\n throw new RangeError('\\'offset\\' is out of bounds')\n }\n\n if (array.byteLength < byteOffset + (length || 0)) {\n throw new RangeError('\\'length\\' is out of bounds')\n }\n\n var buf\n if (byteOffset === undefined && length === undefined) {\n buf = new Uint8Array(array)\n } else if (length === undefined) {\n buf = new Uint8Array(array, byteOffset)\n } else {\n buf = new Uint8Array(array, byteOffset, length)\n }\n\n // Return an augmented `Uint8Array` instance\n buf.__proto__ = Buffer.prototype\n return buf\n}\n\nfunction fromObject (obj) {\n if (Buffer.isBuffer(obj)) {\n var len = checked(obj.length) | 0\n var buf = createBuffer(len)\n\n if (buf.length === 0) {\n return buf\n }\n\n obj.copy(buf, 0, 0, len)\n return buf\n }\n\n if (obj) {\n if (isArrayBufferView(obj) || 'length' in obj) {\n if (typeof obj.length !== 'number' || numberIsNaN(obj.length)) {\n return createBuffer(0)\n }\n return fromArrayLike(obj)\n }\n\n if (obj.type === 'Buffer' && Array.isArray(obj.data)) {\n return fromArrayLike(obj.data)\n }\n }\n\n throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')\n}\n\nfunction checked (length) {\n // Note: cannot use `length < K_MAX_LENGTH` here because that fails when\n // length is NaN (which is otherwise coerced to zero.)\n if (length >= K_MAX_LENGTH) {\n throw new RangeError('Attempt to allocate Buffer larger than maximum ' +\n 'size: 0x' + K_MAX_LENGTH.toString(16) + ' bytes')\n }\n return length | 0\n}\n\nfunction SlowBuffer (length) {\n if (+length != length) { // eslint-disable-line eqeqeq\n length = 0\n }\n return Buffer.alloc(+length)\n}\n\nBuffer.isBuffer = function isBuffer (b) {\n return b != null && b._isBuffer === true\n}\n\nBuffer.compare = function compare (a, b) {\n if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {\n throw new TypeError('Arguments must be Buffers')\n }\n\n if (a === b) return 0\n\n var x = a.length\n var y = b.length\n\n for (var i = 0, len = Math.min(x, y); i < len; ++i) {\n if (a[i] !== b[i]) {\n x = a[i]\n y = b[i]\n break\n }\n }\n\n if (x < y) return -1\n if (y < x) return 1\n return 0\n}\n\nBuffer.isEncoding = function isEncoding (encoding) {\n switch (String(encoding).toLowerCase()) {\n case 'hex':\n case 'utf8':\n case 'utf-8':\n case 'ascii':\n case 'latin1':\n case 'binary':\n case 'base64':\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return true\n default:\n return false\n }\n}\n\nBuffer.concat = function concat (list, length) {\n if (!Array.isArray(list)) {\n throw new TypeError('\"list\" argument must be an Array of Buffers')\n }\n\n if (list.length === 0) {\n return Buffer.alloc(0)\n }\n\n var i\n if (length === undefined) {\n length = 0\n for (i = 0; i < list.length; ++i) {\n length += list[i].length\n }\n }\n\n var buffer = Buffer.allocUnsafe(length)\n var pos = 0\n for (i = 0; i < list.length; ++i) {\n var buf = list[i]\n if (!Buffer.isBuffer(buf)) {\n throw new TypeError('\"list\" argument must be an Array of Buffers')\n }\n buf.copy(buffer, pos)\n pos += buf.length\n }\n return buffer\n}\n\nfunction byteLength (string, encoding) {\n if (Buffer.isBuffer(string)) {\n return string.length\n }\n if (isArrayBufferView(string) || isArrayBuffer(string)) {\n return string.byteLength\n }\n if (typeof string !== 'string') {\n string = '' + string\n }\n\n var len = string.length\n if (len === 0) return 0\n\n // Use a for loop to avoid recursion\n var loweredCase = false\n for (;;) {\n switch (encoding) {\n case 'ascii':\n case 'latin1':\n case 'binary':\n return len\n case 'utf8':\n case 'utf-8':\n case undefined:\n return utf8ToBytes(string).length\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return len * 2\n case 'hex':\n return len >>> 1\n case 'base64':\n return base64ToBytes(string).length\n default:\n if (loweredCase) return utf8ToBytes(string).length // assume utf8\n encoding = ('' + encoding).toLowerCase()\n loweredCase = true\n }\n }\n}\nBuffer.byteLength = byteLength\n\nfunction slowToString (encoding, start, end) {\n var loweredCase = false\n\n // No need to verify that \"this.length <= MAX_UINT32\" since it's a read-only\n // property of a typed array.\n\n // This behaves neither like String nor Uint8Array in that we set start/end\n // to their upper/lower bounds if the value passed is out of range.\n // undefined is handled specially as per ECMA-262 6th Edition,\n // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.\n if (start === undefined || start < 0) {\n start = 0\n }\n // Return early if start > this.length. Done here to prevent potential uint32\n // coercion fail below.\n if (start > this.length) {\n return ''\n }\n\n if (end === undefined || end > this.length) {\n end = this.length\n }\n\n if (end <= 0) {\n return ''\n }\n\n // Force coersion to uint32. This will also coerce falsey/NaN values to 0.\n end >>>= 0\n start >>>= 0\n\n if (end <= start) {\n return ''\n }\n\n if (!encoding) encoding = 'utf8'\n\n while (true) {\n switch (encoding) {\n case 'hex':\n return hexSlice(this, start, end)\n\n case 'utf8':\n case 'utf-8':\n return utf8Slice(this, start, end)\n\n case 'ascii':\n return asciiSlice(this, start, end)\n\n case 'latin1':\n case 'binary':\n return latin1Slice(this, start, end)\n\n case 'base64':\n return base64Slice(this, start, end)\n\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return utf16leSlice(this, start, end)\n\n default:\n if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)\n encoding = (encoding + '').toLowerCase()\n loweredCase = true\n }\n }\n}\n\n// This property is used by `Buffer.isBuffer` (and the `is-buffer` npm package)\n// to detect a Buffer instance. It's not possible to use `instanceof Buffer`\n// reliably in a browserify context because there could be multiple different\n// copies of the 'buffer' package in use. This method works even for Buffer\n// instances that were created from another copy of the `buffer` package.\n// See: https://github.com/feross/buffer/issues/154\nBuffer.prototype._isBuffer = true\n\nfunction swap (b, n, m) {\n var i = b[n]\n b[n] = b[m]\n b[m] = i\n}\n\nBuffer.prototype.swap16 = function swap16 () {\n var len = this.length\n if (len % 2 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 16-bits')\n }\n for (var i = 0; i < len; i += 2) {\n swap(this, i, i + 1)\n }\n return this\n}\n\nBuffer.prototype.swap32 = function swap32 () {\n var len = this.length\n if (len % 4 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 32-bits')\n }\n for (var i = 0; i < len; i += 4) {\n swap(this, i, i + 3)\n swap(this, i + 1, i + 2)\n }\n return this\n}\n\nBuffer.prototype.swap64 = function swap64 () {\n var len = this.length\n if (len % 8 !== 0) {\n throw new RangeError('Buffer size must be a multiple of 64-bits')\n }\n for (var i = 0; i < len; i += 8) {\n swap(this, i, i + 7)\n swap(this, i + 1, i + 6)\n swap(this, i + 2, i + 5)\n swap(this, i + 3, i + 4)\n }\n return this\n}\n\nBuffer.prototype.toString = function toString () {\n var length = this.length\n if (length === 0) return ''\n if (arguments.length === 0) return utf8Slice(this, 0, length)\n return slowToString.apply(this, arguments)\n}\n\nBuffer.prototype.equals = function equals (b) {\n if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')\n if (this === b) return true\n return Buffer.compare(this, b) === 0\n}\n\nBuffer.prototype.inspect = function inspect () {\n var str = ''\n var max = exports.INSPECT_MAX_BYTES\n if (this.length > 0) {\n str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')\n if (this.length > max) str += ' ... '\n }\n return ''\n}\n\nBuffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {\n if (!Buffer.isBuffer(target)) {\n throw new TypeError('Argument must be a Buffer')\n }\n\n if (start === undefined) {\n start = 0\n }\n if (end === undefined) {\n end = target ? target.length : 0\n }\n if (thisStart === undefined) {\n thisStart = 0\n }\n if (thisEnd === undefined) {\n thisEnd = this.length\n }\n\n if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {\n throw new RangeError('out of range index')\n }\n\n if (thisStart >= thisEnd && start >= end) {\n return 0\n }\n if (thisStart >= thisEnd) {\n return -1\n }\n if (start >= end) {\n return 1\n }\n\n start >>>= 0\n end >>>= 0\n thisStart >>>= 0\n thisEnd >>>= 0\n\n if (this === target) return 0\n\n var x = thisEnd - thisStart\n var y = end - start\n var len = Math.min(x, y)\n\n var thisCopy = this.slice(thisStart, thisEnd)\n var targetCopy = target.slice(start, end)\n\n for (var i = 0; i < len; ++i) {\n if (thisCopy[i] !== targetCopy[i]) {\n x = thisCopy[i]\n y = targetCopy[i]\n break\n }\n }\n\n if (x < y) return -1\n if (y < x) return 1\n return 0\n}\n\n// Finds either the first index of `val` in `buffer` at offset >= `byteOffset`,\n// OR the last index of `val` in `buffer` at offset <= `byteOffset`.\n//\n// Arguments:\n// - buffer - a Buffer to search\n// - val - a string, Buffer, or number\n// - byteOffset - an index into `buffer`; will be clamped to an int32\n// - encoding - an optional encoding, relevant is val is a string\n// - dir - true for indexOf, false for lastIndexOf\nfunction bidirectionalIndexOf (buffer, val, byteOffset, encoding, dir) {\n // Empty buffer means no match\n if (buffer.length === 0) return -1\n\n // Normalize byteOffset\n if (typeof byteOffset === 'string') {\n encoding = byteOffset\n byteOffset = 0\n } else if (byteOffset > 0x7fffffff) {\n byteOffset = 0x7fffffff\n } else if (byteOffset < -0x80000000) {\n byteOffset = -0x80000000\n }\n byteOffset = +byteOffset // Coerce to Number.\n if (numberIsNaN(byteOffset)) {\n // byteOffset: it it's undefined, null, NaN, \"foo\", etc, search whole buffer\n byteOffset = dir ? 0 : (buffer.length - 1)\n }\n\n // Normalize byteOffset: negative offsets start from the end of the buffer\n if (byteOffset < 0) byteOffset = buffer.length + byteOffset\n if (byteOffset >= buffer.length) {\n if (dir) return -1\n else byteOffset = buffer.length - 1\n } else if (byteOffset < 0) {\n if (dir) byteOffset = 0\n else return -1\n }\n\n // Normalize val\n if (typeof val === 'string') {\n val = Buffer.from(val, encoding)\n }\n\n // Finally, search either indexOf (if dir is true) or lastIndexOf\n if (Buffer.isBuffer(val)) {\n // Special case: looking for empty string/buffer always fails\n if (val.length === 0) {\n return -1\n }\n return arrayIndexOf(buffer, val, byteOffset, encoding, dir)\n } else if (typeof val === 'number') {\n val = val & 0xFF // Search for a byte value [0-255]\n if (typeof Uint8Array.prototype.indexOf === 'function') {\n if (dir) {\n return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset)\n } else {\n return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset)\n }\n }\n return arrayIndexOf(buffer, [ val ], byteOffset, encoding, dir)\n }\n\n throw new TypeError('val must be string, number or Buffer')\n}\n\nfunction arrayIndexOf (arr, val, byteOffset, encoding, dir) {\n var indexSize = 1\n var arrLength = arr.length\n var valLength = val.length\n\n if (encoding !== undefined) {\n encoding = String(encoding).toLowerCase()\n if (encoding === 'ucs2' || encoding === 'ucs-2' ||\n encoding === 'utf16le' || encoding === 'utf-16le') {\n if (arr.length < 2 || val.length < 2) {\n return -1\n }\n indexSize = 2\n arrLength /= 2\n valLength /= 2\n byteOffset /= 2\n }\n }\n\n function read (buf, i) {\n if (indexSize === 1) {\n return buf[i]\n } else {\n return buf.readUInt16BE(i * indexSize)\n }\n }\n\n var i\n if (dir) {\n var foundIndex = -1\n for (i = byteOffset; i < arrLength; i++) {\n if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {\n if (foundIndex === -1) foundIndex = i\n if (i - foundIndex + 1 === valLength) return foundIndex * indexSize\n } else {\n if (foundIndex !== -1) i -= i - foundIndex\n foundIndex = -1\n }\n }\n } else {\n if (byteOffset + valLength > arrLength) byteOffset = arrLength - valLength\n for (i = byteOffset; i >= 0; i--) {\n var found = true\n for (var j = 0; j < valLength; j++) {\n if (read(arr, i + j) !== read(val, j)) {\n found = false\n break\n }\n }\n if (found) return i\n }\n }\n\n return -1\n}\n\nBuffer.prototype.includes = function includes (val, byteOffset, encoding) {\n return this.indexOf(val, byteOffset, encoding) !== -1\n}\n\nBuffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {\n return bidirectionalIndexOf(this, val, byteOffset, encoding, true)\n}\n\nBuffer.prototype.lastIndexOf = function lastIndexOf (val, byteOffset, encoding) {\n return bidirectionalIndexOf(this, val, byteOffset, encoding, false)\n}\n\nfunction hexWrite (buf, string, offset, length) {\n offset = Number(offset) || 0\n var remaining = buf.length - offset\n if (!length) {\n length = remaining\n } else {\n length = Number(length)\n if (length > remaining) {\n length = remaining\n }\n }\n\n // must be an even number of digits\n var strLen = string.length\n if (strLen % 2 !== 0) throw new TypeError('Invalid hex string')\n\n if (length > strLen / 2) {\n length = strLen / 2\n }\n for (var i = 0; i < length; ++i) {\n var parsed = parseInt(string.substr(i * 2, 2), 16)\n if (numberIsNaN(parsed)) return i\n buf[offset + i] = parsed\n }\n return i\n}\n\nfunction utf8Write (buf, string, offset, length) {\n return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)\n}\n\nfunction asciiWrite (buf, string, offset, length) {\n return blitBuffer(asciiToBytes(string), buf, offset, length)\n}\n\nfunction latin1Write (buf, string, offset, length) {\n return asciiWrite(buf, string, offset, length)\n}\n\nfunction base64Write (buf, string, offset, length) {\n return blitBuffer(base64ToBytes(string), buf, offset, length)\n}\n\nfunction ucs2Write (buf, string, offset, length) {\n return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)\n}\n\nBuffer.prototype.write = function write (string, offset, length, encoding) {\n // Buffer#write(string)\n if (offset === undefined) {\n encoding = 'utf8'\n length = this.length\n offset = 0\n // Buffer#write(string, encoding)\n } else if (length === undefined && typeof offset === 'string') {\n encoding = offset\n length = this.length\n offset = 0\n // Buffer#write(string, offset[, length][, encoding])\n } else if (isFinite(offset)) {\n offset = offset >>> 0\n if (isFinite(length)) {\n length = length >>> 0\n if (encoding === undefined) encoding = 'utf8'\n } else {\n encoding = length\n length = undefined\n }\n } else {\n throw new Error(\n 'Buffer.write(string, encoding, offset[, length]) is no longer supported'\n )\n }\n\n var remaining = this.length - offset\n if (length === undefined || length > remaining) length = remaining\n\n if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {\n throw new RangeError('Attempt to write outside buffer bounds')\n }\n\n if (!encoding) encoding = 'utf8'\n\n var loweredCase = false\n for (;;) {\n switch (encoding) {\n case 'hex':\n return hexWrite(this, string, offset, length)\n\n case 'utf8':\n case 'utf-8':\n return utf8Write(this, string, offset, length)\n\n case 'ascii':\n return asciiWrite(this, string, offset, length)\n\n case 'latin1':\n case 'binary':\n return latin1Write(this, string, offset, length)\n\n case 'base64':\n // Warning: maxLength not taken into account in base64Write\n return base64Write(this, string, offset, length)\n\n case 'ucs2':\n case 'ucs-2':\n case 'utf16le':\n case 'utf-16le':\n return ucs2Write(this, string, offset, length)\n\n default:\n if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)\n encoding = ('' + encoding).toLowerCase()\n loweredCase = true\n }\n }\n}\n\nBuffer.prototype.toJSON = function toJSON () {\n return {\n type: 'Buffer',\n data: Array.prototype.slice.call(this._arr || this, 0)\n }\n}\n\nfunction base64Slice (buf, start, end) {\n if (start === 0 && end === buf.length) {\n return base64.fromByteArray(buf)\n } else {\n return base64.fromByteArray(buf.slice(start, end))\n }\n}\n\nfunction utf8Slice (buf, start, end) {\n end = Math.min(buf.length, end)\n var res = []\n\n var i = start\n while (i < end) {\n var firstByte = buf[i]\n var codePoint = null\n var bytesPerSequence = (firstByte > 0xEF) ? 4\n : (firstByte > 0xDF) ? 3\n : (firstByte > 0xBF) ? 2\n : 1\n\n if (i + bytesPerSequence <= end) {\n var secondByte, thirdByte, fourthByte, tempCodePoint\n\n switch (bytesPerSequence) {\n case 1:\n if (firstByte < 0x80) {\n codePoint = firstByte\n }\n break\n case 2:\n secondByte = buf[i + 1]\n if ((secondByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)\n if (tempCodePoint > 0x7F) {\n codePoint = tempCodePoint\n }\n }\n break\n case 3:\n secondByte = buf[i + 1]\n thirdByte = buf[i + 2]\n if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)\n if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {\n codePoint = tempCodePoint\n }\n }\n break\n case 4:\n secondByte = buf[i + 1]\n thirdByte = buf[i + 2]\n fourthByte = buf[i + 3]\n if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {\n tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)\n if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {\n codePoint = tempCodePoint\n }\n }\n }\n }\n\n if (codePoint === null) {\n // we did not generate a valid codePoint so insert a\n // replacement char (U+FFFD) and advance only 1 byte\n codePoint = 0xFFFD\n bytesPerSequence = 1\n } else if (codePoint > 0xFFFF) {\n // encode to utf16 (surrogate pair dance)\n codePoint -= 0x10000\n res.push(codePoint >>> 10 & 0x3FF | 0xD800)\n codePoint = 0xDC00 | codePoint & 0x3FF\n }\n\n res.push(codePoint)\n i += bytesPerSequence\n }\n\n return decodeCodePointsArray(res)\n}\n\n// Based on http://stackoverflow.com/a/22747272/680742, the browser with\n// the lowest limit is Chrome, with 0x10000 args.\n// We go 1 magnitude less, for safety\nvar MAX_ARGUMENTS_LENGTH = 0x1000\n\nfunction decodeCodePointsArray (codePoints) {\n var len = codePoints.length\n if (len <= MAX_ARGUMENTS_LENGTH) {\n return String.fromCharCode.apply(String, codePoints) // avoid extra slice()\n }\n\n // Decode in chunks to avoid \"call stack size exceeded\".\n var res = ''\n var i = 0\n while (i < len) {\n res += String.fromCharCode.apply(\n String,\n codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)\n )\n }\n return res\n}\n\nfunction asciiSlice (buf, start, end) {\n var ret = ''\n end = Math.min(buf.length, end)\n\n for (var i = start; i < end; ++i) {\n ret += String.fromCharCode(buf[i] & 0x7F)\n }\n return ret\n}\n\nfunction latin1Slice (buf, start, end) {\n var ret = ''\n end = Math.min(buf.length, end)\n\n for (var i = start; i < end; ++i) {\n ret += String.fromCharCode(buf[i])\n }\n return ret\n}\n\nfunction hexSlice (buf, start, end) {\n var len = buf.length\n\n if (!start || start < 0) start = 0\n if (!end || end < 0 || end > len) end = len\n\n var out = ''\n for (var i = start; i < end; ++i) {\n out += toHex(buf[i])\n }\n return out\n}\n\nfunction utf16leSlice (buf, start, end) {\n var bytes = buf.slice(start, end)\n var res = ''\n for (var i = 0; i < bytes.length; i += 2) {\n res += String.fromCharCode(bytes[i] + (bytes[i + 1] * 256))\n }\n return res\n}\n\nBuffer.prototype.slice = function slice (start, end) {\n var len = this.length\n start = ~~start\n end = end === undefined ? len : ~~end\n\n if (start < 0) {\n start += len\n if (start < 0) start = 0\n } else if (start > len) {\n start = len\n }\n\n if (end < 0) {\n end += len\n if (end < 0) end = 0\n } else if (end > len) {\n end = len\n }\n\n if (end < start) end = start\n\n var newBuf = this.subarray(start, end)\n // Return an augmented `Uint8Array` instance\n newBuf.__proto__ = Buffer.prototype\n return newBuf\n}\n\n/*\n * Need to make sure that buffer isn't trying to write out of bounds.\n */\nfunction checkOffset (offset, ext, length) {\n if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')\n if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')\n}\n\nBuffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var val = this[offset]\n var mul = 1\n var i = 0\n while (++i < byteLength && (mul *= 0x100)) {\n val += this[offset + i] * mul\n }\n\n return val\n}\n\nBuffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n checkOffset(offset, byteLength, this.length)\n }\n\n var val = this[offset + --byteLength]\n var mul = 1\n while (byteLength > 0 && (mul *= 0x100)) {\n val += this[offset + --byteLength] * mul\n }\n\n return val\n}\n\nBuffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 1, this.length)\n return this[offset]\n}\n\nBuffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n return this[offset] | (this[offset + 1] << 8)\n}\n\nBuffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n return (this[offset] << 8) | this[offset + 1]\n}\n\nBuffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return ((this[offset]) |\n (this[offset + 1] << 8) |\n (this[offset + 2] << 16)) +\n (this[offset + 3] * 0x1000000)\n}\n\nBuffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset] * 0x1000000) +\n ((this[offset + 1] << 16) |\n (this[offset + 2] << 8) |\n this[offset + 3])\n}\n\nBuffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var val = this[offset]\n var mul = 1\n var i = 0\n while (++i < byteLength && (mul *= 0x100)) {\n val += this[offset + i] * mul\n }\n mul *= 0x80\n\n if (val >= mul) val -= Math.pow(2, 8 * byteLength)\n\n return val\n}\n\nBuffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) checkOffset(offset, byteLength, this.length)\n\n var i = byteLength\n var mul = 1\n var val = this[offset + --i]\n while (i > 0 && (mul *= 0x100)) {\n val += this[offset + --i] * mul\n }\n mul *= 0x80\n\n if (val >= mul) val -= Math.pow(2, 8 * byteLength)\n\n return val\n}\n\nBuffer.prototype.readInt8 = function readInt8 (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 1, this.length)\n if (!(this[offset] & 0x80)) return (this[offset])\n return ((0xff - this[offset] + 1) * -1)\n}\n\nBuffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n var val = this[offset] | (this[offset + 1] << 8)\n return (val & 0x8000) ? val | 0xFFFF0000 : val\n}\n\nBuffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 2, this.length)\n var val = this[offset + 1] | (this[offset] << 8)\n return (val & 0x8000) ? val | 0xFFFF0000 : val\n}\n\nBuffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset]) |\n (this[offset + 1] << 8) |\n (this[offset + 2] << 16) |\n (this[offset + 3] << 24)\n}\n\nBuffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n\n return (this[offset] << 24) |\n (this[offset + 1] << 16) |\n (this[offset + 2] << 8) |\n (this[offset + 3])\n}\n\nBuffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n return ieee754.read(this, offset, true, 23, 4)\n}\n\nBuffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 4, this.length)\n return ieee754.read(this, offset, false, 23, 4)\n}\n\nBuffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 8, this.length)\n return ieee754.read(this, offset, true, 52, 8)\n}\n\nBuffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {\n offset = offset >>> 0\n if (!noAssert) checkOffset(offset, 8, this.length)\n return ieee754.read(this, offset, false, 52, 8)\n}\n\nfunction checkInt (buf, value, offset, ext, max, min) {\n if (!Buffer.isBuffer(buf)) throw new TypeError('\"buffer\" argument must be a Buffer instance')\n if (value > max || value < min) throw new RangeError('\"value\" argument is out of bounds')\n if (offset + ext > buf.length) throw new RangeError('Index out of range')\n}\n\nBuffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n var maxBytes = Math.pow(2, 8 * byteLength) - 1\n checkInt(this, value, offset, byteLength, maxBytes, 0)\n }\n\n var mul = 1\n var i = 0\n this[offset] = value & 0xFF\n while (++i < byteLength && (mul *= 0x100)) {\n this[offset + i] = (value / mul) & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n byteLength = byteLength >>> 0\n if (!noAssert) {\n var maxBytes = Math.pow(2, 8 * byteLength) - 1\n checkInt(this, value, offset, byteLength, maxBytes, 0)\n }\n\n var i = byteLength - 1\n var mul = 1\n this[offset + i] = value & 0xFF\n while (--i >= 0 && (mul *= 0x100)) {\n this[offset + i] = (value / mul) & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)\n this[offset] = (value & 0xff)\n return offset + 1\n}\n\nBuffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n return offset + 2\n}\n\nBuffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)\n this[offset] = (value >>> 8)\n this[offset + 1] = (value & 0xff)\n return offset + 2\n}\n\nBuffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)\n this[offset + 3] = (value >>> 24)\n this[offset + 2] = (value >>> 16)\n this[offset + 1] = (value >>> 8)\n this[offset] = (value & 0xff)\n return offset + 4\n}\n\nBuffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)\n this[offset] = (value >>> 24)\n this[offset + 1] = (value >>> 16)\n this[offset + 2] = (value >>> 8)\n this[offset + 3] = (value & 0xff)\n return offset + 4\n}\n\nBuffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n var limit = Math.pow(2, (8 * byteLength) - 1)\n\n checkInt(this, value, offset, byteLength, limit - 1, -limit)\n }\n\n var i = 0\n var mul = 1\n var sub = 0\n this[offset] = value & 0xFF\n while (++i < byteLength && (mul *= 0x100)) {\n if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {\n sub = 1\n }\n this[offset + i] = ((value / mul) >> 0) - sub & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n var limit = Math.pow(2, (8 * byteLength) - 1)\n\n checkInt(this, value, offset, byteLength, limit - 1, -limit)\n }\n\n var i = byteLength - 1\n var mul = 1\n var sub = 0\n this[offset + i] = value & 0xFF\n while (--i >= 0 && (mul *= 0x100)) {\n if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {\n sub = 1\n }\n this[offset + i] = ((value / mul) >> 0) - sub & 0xFF\n }\n\n return offset + byteLength\n}\n\nBuffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)\n if (value < 0) value = 0xff + value + 1\n this[offset] = (value & 0xff)\n return offset + 1\n}\n\nBuffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n return offset + 2\n}\n\nBuffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)\n this[offset] = (value >>> 8)\n this[offset + 1] = (value & 0xff)\n return offset + 2\n}\n\nBuffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)\n this[offset] = (value & 0xff)\n this[offset + 1] = (value >>> 8)\n this[offset + 2] = (value >>> 16)\n this[offset + 3] = (value >>> 24)\n return offset + 4\n}\n\nBuffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)\n if (value < 0) value = 0xffffffff + value + 1\n this[offset] = (value >>> 24)\n this[offset + 1] = (value >>> 16)\n this[offset + 2] = (value >>> 8)\n this[offset + 3] = (value & 0xff)\n return offset + 4\n}\n\nfunction checkIEEE754 (buf, value, offset, ext, max, min) {\n if (offset + ext > buf.length) throw new RangeError('Index out of range')\n if (offset < 0) throw new RangeError('Index out of range')\n}\n\nfunction writeFloat (buf, value, offset, littleEndian, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)\n }\n ieee754.write(buf, value, offset, littleEndian, 23, 4)\n return offset + 4\n}\n\nBuffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {\n return writeFloat(this, value, offset, true, noAssert)\n}\n\nBuffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {\n return writeFloat(this, value, offset, false, noAssert)\n}\n\nfunction writeDouble (buf, value, offset, littleEndian, noAssert) {\n value = +value\n offset = offset >>> 0\n if (!noAssert) {\n checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)\n }\n ieee754.write(buf, value, offset, littleEndian, 52, 8)\n return offset + 8\n}\n\nBuffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {\n return writeDouble(this, value, offset, true, noAssert)\n}\n\nBuffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {\n return writeDouble(this, value, offset, false, noAssert)\n}\n\n// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)\nBuffer.prototype.copy = function copy (target, targetStart, start, end) {\n if (!start) start = 0\n if (!end && end !== 0) end = this.length\n if (targetStart >= target.length) targetStart = target.length\n if (!targetStart) targetStart = 0\n if (end > 0 && end < start) end = start\n\n // Copy 0 bytes; we're done\n if (end === start) return 0\n if (target.length === 0 || this.length === 0) return 0\n\n // Fatal error conditions\n if (targetStart < 0) {\n throw new RangeError('targetStart out of bounds')\n }\n if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')\n if (end < 0) throw new RangeError('sourceEnd out of bounds')\n\n // Are we oob?\n if (end > this.length) end = this.length\n if (target.length - targetStart < end - start) {\n end = target.length - targetStart + start\n }\n\n var len = end - start\n var i\n\n if (this === target && start < targetStart && targetStart < end) {\n // descending copy from end\n for (i = len - 1; i >= 0; --i) {\n target[i + targetStart] = this[i + start]\n }\n } else if (len < 1000) {\n // ascending copy from start\n for (i = 0; i < len; ++i) {\n target[i + targetStart] = this[i + start]\n }\n } else {\n Uint8Array.prototype.set.call(\n target,\n this.subarray(start, start + len),\n targetStart\n )\n }\n\n return len\n}\n\n// Usage:\n// buffer.fill(number[, offset[, end]])\n// buffer.fill(buffer[, offset[, end]])\n// buffer.fill(string[, offset[, end]][, encoding])\nBuffer.prototype.fill = function fill (val, start, end, encoding) {\n // Handle string cases:\n if (typeof val === 'string') {\n if (typeof start === 'string') {\n encoding = start\n start = 0\n end = this.length\n } else if (typeof end === 'string') {\n encoding = end\n end = this.length\n }\n if (val.length === 1) {\n var code = val.charCodeAt(0)\n if (code < 256) {\n val = code\n }\n }\n if (encoding !== undefined && typeof encoding !== 'string') {\n throw new TypeError('encoding must be a string')\n }\n if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {\n throw new TypeError('Unknown encoding: ' + encoding)\n }\n } else if (typeof val === 'number') {\n val = val & 255\n }\n\n // Invalid ranges are not set to a default, so can range check early.\n if (start < 0 || this.length < start || this.length < end) {\n throw new RangeError('Out of range index')\n }\n\n if (end <= start) {\n return this\n }\n\n start = start >>> 0\n end = end === undefined ? this.length : end >>> 0\n\n if (!val) val = 0\n\n var i\n if (typeof val === 'number') {\n for (i = start; i < end; ++i) {\n this[i] = val\n }\n } else {\n var bytes = Buffer.isBuffer(val)\n ? val\n : new Buffer(val, encoding)\n var len = bytes.length\n for (i = 0; i < end - start; ++i) {\n this[i + start] = bytes[i % len]\n }\n }\n\n return this\n}\n\n// HELPER FUNCTIONS\n// ================\n\nvar INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g\n\nfunction base64clean (str) {\n // Node strips out invalid characters like \\n and \\t from the string, base64-js does not\n str = str.trim().replace(INVALID_BASE64_RE, '')\n // Node converts strings with length < 2 to ''\n if (str.length < 2) return ''\n // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not\n while (str.length % 4 !== 0) {\n str = str + '='\n }\n return str\n}\n\nfunction toHex (n) {\n if (n < 16) return '0' + n.toString(16)\n return n.toString(16)\n}\n\nfunction utf8ToBytes (string, units) {\n units = units || Infinity\n var codePoint\n var length = string.length\n var leadSurrogate = null\n var bytes = []\n\n for (var i = 0; i < length; ++i) {\n codePoint = string.charCodeAt(i)\n\n // is surrogate component\n if (codePoint > 0xD7FF && codePoint < 0xE000) {\n // last char was a lead\n if (!leadSurrogate) {\n // no lead yet\n if (codePoint > 0xDBFF) {\n // unexpected trail\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n continue\n } else if (i + 1 === length) {\n // unpaired lead\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n continue\n }\n\n // valid lead\n leadSurrogate = codePoint\n\n continue\n }\n\n // 2 leads in a row\n if (codePoint < 0xDC00) {\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n leadSurrogate = codePoint\n continue\n }\n\n // valid surrogate pair\n codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000\n } else if (leadSurrogate) {\n // valid bmp char, but last char was a lead\n if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)\n }\n\n leadSurrogate = null\n\n // encode utf8\n if (codePoint < 0x80) {\n if ((units -= 1) < 0) break\n bytes.push(codePoint)\n } else if (codePoint < 0x800) {\n if ((units -= 2) < 0) break\n bytes.push(\n codePoint >> 0x6 | 0xC0,\n codePoint & 0x3F | 0x80\n )\n } else if (codePoint < 0x10000) {\n if ((units -= 3) < 0) break\n bytes.push(\n codePoint >> 0xC | 0xE0,\n codePoint >> 0x6 & 0x3F | 0x80,\n codePoint & 0x3F | 0x80\n )\n } else if (codePoint < 0x110000) {\n if ((units -= 4) < 0) break\n bytes.push(\n codePoint >> 0x12 | 0xF0,\n codePoint >> 0xC & 0x3F | 0x80,\n codePoint >> 0x6 & 0x3F | 0x80,\n codePoint & 0x3F | 0x80\n )\n } else {\n throw new Error('Invalid code point')\n }\n }\n\n return bytes\n}\n\nfunction asciiToBytes (str) {\n var byteArray = []\n for (var i = 0; i < str.length; ++i) {\n // Node's code seems to be doing this and not & 0x7F..\n byteArray.push(str.charCodeAt(i) & 0xFF)\n }\n return byteArray\n}\n\nfunction utf16leToBytes (str, units) {\n var c, hi, lo\n var byteArray = []\n for (var i = 0; i < str.length; ++i) {\n if ((units -= 2) < 0) break\n\n c = str.charCodeAt(i)\n hi = c >> 8\n lo = c % 256\n byteArray.push(lo)\n byteArray.push(hi)\n }\n\n return byteArray\n}\n\nfunction base64ToBytes (str) {\n return base64.toByteArray(base64clean(str))\n}\n\nfunction blitBuffer (src, dst, offset, length) {\n for (var i = 0; i < length; ++i) {\n if ((i + offset >= dst.length) || (i >= src.length)) break\n dst[i + offset] = src[i]\n }\n return i\n}\n\n// ArrayBuffers from another context (i.e. an iframe) do not pass the `instanceof` check\n// but they should be treated as valid. See: https://github.com/feross/buffer/issues/166\nfunction isArrayBuffer (obj) {\n return obj instanceof ArrayBuffer ||\n (obj != null && obj.constructor != null && obj.constructor.name === 'ArrayBuffer' &&\n typeof obj.byteLength === 'number')\n}\n\n// Node 0.10 supports `ArrayBuffer` but lacks `ArrayBuffer.isView`\nfunction isArrayBufferView (obj) {\n return (typeof ArrayBuffer.isView === 'function') && ArrayBuffer.isView(obj)\n}\n\nfunction numberIsNaN (obj) {\n return obj !== obj // eslint-disable-line no-self-compare\n}\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Not exported from index.\r\nexport class BinaryMessageFormat {\r\n\r\n // The length prefix of binary messages is encoded as VarInt. Read the comment in\r\n // the BinaryMessageParser.TryParseMessage for details.\r\n\r\n public static write(output: Uint8Array): ArrayBuffer {\r\n // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser\r\n // in which case .byteLength does will be undefined\r\n let size = output.byteLength || output.length;\r\n const lenBuffer = [];\r\n do {\r\n let sizePart = size & 0x7f;\r\n size = size >> 7;\r\n if (size > 0) {\r\n sizePart |= 0x80;\r\n }\r\n lenBuffer.push(sizePart);\r\n }\r\n while (size > 0);\r\n\r\n // msgpack5 uses returns Buffer instead of Uint8Array on IE10 and some other browser\r\n // in which case .byteLength does will be undefined\r\n size = output.byteLength || output.length;\r\n\r\n const buffer = new Uint8Array(lenBuffer.length + size);\r\n buffer.set(lenBuffer, 0);\r\n buffer.set(output, lenBuffer.length);\r\n return buffer.buffer;\r\n }\r\n\r\n public static parse(input: ArrayBuffer): Uint8Array[] {\r\n const result: Uint8Array[] = [];\r\n const uint8Array = new Uint8Array(input);\r\n const maxLengthPrefixSize = 5;\r\n const numBitsToShift = [0, 7, 14, 21, 28 ];\r\n\r\n for (let offset = 0; offset < input.byteLength;) {\r\n let numBytes = 0;\r\n let size = 0;\r\n let byteRead;\r\n do {\r\n byteRead = uint8Array[offset + numBytes];\r\n size = size | ((byteRead & 0x7f) << (numBitsToShift[numBytes]));\r\n numBytes++;\r\n }\r\n while (numBytes < Math.min(maxLengthPrefixSize, input.byteLength - offset) && (byteRead & 0x80) !== 0);\r\n\r\n if ((byteRead & 0x80) !== 0 && numBytes < maxLengthPrefixSize) {\r\n throw new Error(\"Cannot read message size.\");\r\n }\r\n\r\n if (numBytes === maxLengthPrefixSize && byteRead > 7) {\r\n throw new Error(\"Messages bigger than 2GB are not supported.\");\r\n }\r\n\r\n if (uint8Array.byteLength >= (offset + numBytes + size)) {\r\n // IE does not support .slice() so use subarray\r\n result.push(uint8Array.slice\r\n ? uint8Array.slice(offset + numBytes, offset + numBytes + size)\r\n : uint8Array.subarray(offset + numBytes, offset + numBytes + size));\r\n } else {\r\n throw new Error(\"Incomplete message.\");\r\n }\r\n\r\n offset = offset + numBytes + size;\r\n }\r\n\r\n return result;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { Buffer } from \"buffer\";\r\nimport * as msgpack5 from \"msgpack5\";\r\n\r\nimport { CompletionMessage, HubMessage, IHubProtocol, ILogger, InvocationMessage, LogLevel, MessageHeaders, MessageType, NullLogger, StreamInvocationMessage, StreamItemMessage, TransferFormat } from \"@aspnet/signalr\";\r\n\r\nimport { BinaryMessageFormat } from \"./BinaryMessageFormat\";\r\n\r\n// TypeDoc's @inheritDoc and @link don't work across modules :(\r\n\r\n/** Implements the MessagePack Hub Protocol */\r\nexport class MessagePackHubProtocol implements IHubProtocol {\r\n /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */\r\n public readonly name: string = \"messagepack\";\r\n /** The version of the protocol. */\r\n public readonly version: number = 1;\r\n /** The TransferFormat of the protocol. */\r\n public readonly transferFormat: TransferFormat = TransferFormat.Binary;\r\n\r\n /** Creates an array of HubMessage objects from the specified serialized representation.\r\n *\r\n * @param {ArrayBuffer} input An ArrayBuffer containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n public parseMessages(input: ArrayBuffer, logger: ILogger): HubMessage[] {\r\n // The interface does allow \"string\" to be passed in, but this implementation does not. So let's throw a useful error.\r\n if (!(input instanceof ArrayBuffer)) {\r\n throw new Error(\"Invalid input for MessagePack hub protocol. Expected an ArrayBuffer.\");\r\n }\r\n\r\n if (logger === null) {\r\n logger = NullLogger.instance;\r\n }\r\n return BinaryMessageFormat.parse(input).map((m) => this.parseMessage(m, logger));\r\n }\r\n\r\n /** Writes the specified HubMessage to an ArrayBuffer and returns it.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {ArrayBuffer} An ArrayBuffer containing the serialized representation of the message.\r\n */\r\n public writeMessage(message: HubMessage): ArrayBuffer {\r\n switch (message.type) {\r\n case MessageType.Invocation:\r\n return this.writeInvocation(message as InvocationMessage);\r\n case MessageType.StreamInvocation:\r\n return this.writeStreamInvocation(message as StreamInvocationMessage);\r\n case MessageType.StreamItem:\r\n case MessageType.Completion:\r\n throw new Error(`Writing messages of type '${message.type}' is not supported.`);\r\n default:\r\n throw new Error(\"Invalid message type.\");\r\n }\r\n }\r\n\r\n private parseMessage(input: Uint8Array, logger: ILogger): HubMessage {\r\n if (input.length === 0) {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n\r\n const msgpack = msgpack5();\r\n const properties = msgpack.decode(new Buffer(input));\r\n if (properties.length === 0 || !(properties instanceof Array)) {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n\r\n const messageType = properties[0] as MessageType;\r\n\r\n switch (messageType) {\r\n case MessageType.Invocation:\r\n return this.createInvocationMessage(this.readHeaders(properties), properties);\r\n case MessageType.StreamItem:\r\n return this.createStreamItemMessage(this.readHeaders(properties), properties);\r\n case MessageType.Completion:\r\n return this.createCompletionMessage(this.readHeaders(properties), properties);\r\n case MessageType.Ping:\r\n return this.createPingMessage(properties);\r\n case MessageType.Close:\r\n return this.createCloseMessage(properties);\r\n default:\r\n // Future protocol changes can add message types, old clients can ignore them\r\n logger.log(LogLevel.Information, \"Unknown message type '\" + messageType + \"' ignored.\");\r\n return null;\r\n }\r\n }\r\n\r\n private createCloseMessage(properties: any[]): HubMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 2) {\r\n throw new Error(\"Invalid payload for Close message.\");\r\n }\r\n\r\n return {\r\n // Close messages have no headers.\r\n error: properties[1],\r\n type: MessageType.Close,\r\n } as HubMessage;\r\n }\r\n\r\n private createPingMessage(properties: any[]): HubMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 1) {\r\n throw new Error(\"Invalid payload for Ping message.\");\r\n }\r\n\r\n return {\r\n // Ping messages have no headers.\r\n type: MessageType.Ping,\r\n } as HubMessage;\r\n }\r\n\r\n private createInvocationMessage(headers: MessageHeaders, properties: any[]): InvocationMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 5) {\r\n throw new Error(\"Invalid payload for Invocation message.\");\r\n }\r\n\r\n const invocationId = properties[2] as string;\r\n if (invocationId) {\r\n return {\r\n arguments: properties[4],\r\n headers,\r\n invocationId,\r\n target: properties[3] as string,\r\n type: MessageType.Invocation,\r\n };\r\n } else {\r\n return {\r\n arguments: properties[4],\r\n headers,\r\n target: properties[3],\r\n type: MessageType.Invocation,\r\n };\r\n }\r\n\r\n }\r\n\r\n private createStreamItemMessage(headers: MessageHeaders, properties: any[]): StreamItemMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 4) {\r\n throw new Error(\"Invalid payload for StreamItem message.\");\r\n }\r\n\r\n return {\r\n headers,\r\n invocationId: properties[2],\r\n item: properties[3],\r\n type: MessageType.StreamItem,\r\n } as StreamItemMessage;\r\n }\r\n\r\n private createCompletionMessage(headers: MessageHeaders, properties: any[]): CompletionMessage {\r\n // check minimum length to allow protocol to add items to the end of objects in future releases\r\n if (properties.length < 4) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n const errorResult = 1;\r\n const voidResult = 2;\r\n const nonVoidResult = 3;\r\n\r\n const resultKind = properties[3];\r\n\r\n if (resultKind !== voidResult && properties.length < 5) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n const completionMessage = {\r\n error: null as string,\r\n headers,\r\n invocationId: properties[2],\r\n result: null as any,\r\n type: MessageType.Completion,\r\n };\r\n\r\n switch (resultKind) {\r\n case errorResult:\r\n completionMessage.error = properties[4];\r\n break;\r\n case nonVoidResult:\r\n completionMessage.result = properties[4];\r\n break;\r\n }\r\n\r\n return completionMessage as CompletionMessage;\r\n }\r\n\r\n private writeInvocation(invocationMessage: InvocationMessage): ArrayBuffer {\r\n const msgpack = msgpack5();\r\n const payload = msgpack.encode([MessageType.Invocation, invocationMessage.headers || {}, invocationMessage.invocationId || null,\r\n invocationMessage.target, invocationMessage.arguments]);\r\n\r\n return BinaryMessageFormat.write(payload.slice());\r\n }\r\n\r\n private writeStreamInvocation(streamInvocationMessage: StreamInvocationMessage): ArrayBuffer {\r\n const msgpack = msgpack5();\r\n const payload = msgpack.encode([MessageType.StreamInvocation, streamInvocationMessage.headers || {}, streamInvocationMessage.invocationId,\r\n streamInvocationMessage.target, streamInvocationMessage.arguments]);\r\n\r\n return BinaryMessageFormat.write(payload.slice());\r\n }\r\n\r\n private readHeaders(properties: any): MessageHeaders {\r\n const headers: MessageHeaders = properties[1] as MessageHeaders;\r\n if (typeof headers !== \"object\") {\r\n throw new Error(\"Invalid headers.\");\r\n }\r\n return headers;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Version token that will be replaced by the prepack command\r\n/** The version of the SignalR Message Pack protocol library. */\r\nexport const VERSION = \"0.0.0-DEV_BUILD\";\r\n\r\nexport { MessagePackHubProtocol } from \"./MessagePackHubProtocol\";\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds.\r\n\r\nexport * from \"./index\";\r\n"]} \ No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/signalr.js b/samples/ChatSample/wwwroot/scripts/signalr.js index cd1d7cf2d..185d9941b 100644 --- a/samples/ChatSample/wwwroot/scripts/signalr.js +++ b/samples/ChatSample/wwwroot/scripts/signalr.js @@ -1,7 +1,10 @@ -/* @license - * Copyright (c) .NET Foundation. All rights reserved. - * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -*/ +/** + * @overview ASP.NET Core SignalR JavaScript Client. + * @version 1.0.0. + * @license + * Copyright (c) .NET Foundation. All rights reserved. + * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : @@ -22,6 +25,208 @@ function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } +/*! ***************************************************************************** +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ +/* global Reflect, Promise */ + +var extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + +function __extends(d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); +} + +var __assign = Object.assign || function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; + } + return t; +}; + +function __rest(s, e) { + var t = {}; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) + t[p] = s[p]; + if (s != null && typeof Object.getOwnPropertySymbols === "function") + for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) + t[p[i]] = s[p[i]]; + return t; +} + +function __decorate(decorators, target, key, desc) { + var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; + if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); + else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; + return c > 3 && r && Object.defineProperty(target, key, r), r; +} + +function __param(paramIndex, decorator) { + return function (target, key) { decorator(target, key, paramIndex); } +} + +function __metadata(metadataKey, metadataValue) { + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); +} + +function __awaiter(thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +} + +function __generator(thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [0, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +} + +function __exportStar(m, exports) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} + +function __values(o) { + var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; + if (m) return m.call(o); + return { + next: function () { + if (o && i >= o.length) o = void 0; + return { value: o && o[i++], done: !o }; + } + }; +} + +function __read(o, n) { + var m = typeof Symbol === "function" && o[Symbol.iterator]; + if (!m) return o; + var i = m.call(o), r, ar = [], e; + try { + while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); + } + catch (error) { e = { error: error }; } + finally { + try { + if (r && !r.done && (m = i["return"])) m.call(i); + } + finally { if (e) throw e.error; } + } + return ar; +} + +function __spread() { + for (var ar = [], i = 0; i < arguments.length; i++) + ar = ar.concat(__read(arguments[i])); + return ar; +} + +function __await(v) { + return this instanceof __await ? (this.v = v, this) : new __await(v); +} + +function __asyncGenerator(thisArg, _arguments, generator) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var g = generator.apply(thisArg, _arguments || []), i, q = []; + return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; + function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } + function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } + function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } + function fulfill(value) { resume("next", value); } + function reject(value) { resume("throw", value); } + function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } +} + +function __asyncDelegator(o) { + var i, p; + return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; + function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; }; } +} + +function __asyncValues(o) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var m = o[Symbol.asyncIterator]; + return m ? m.call(o) : typeof __values === "function" ? __values(o) : o[Symbol.iterator](); +} + +function __makeTemplateObject(cooked, raw) { + if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } + return cooked; +} + +function __importStar(mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result.default = mod; + return result; +} + +function __importDefault(mod) { + return (mod && mod.__esModule) ? mod : { default: mod }; +} + + +var tslib_1 = Object.freeze({ + __extends: __extends, + __assign: __assign, + __rest: __rest, + __decorate: __decorate, + __param: __param, + __metadata: __metadata, + __awaiter: __awaiter, + __generator: __generator, + __exportStar: __exportStar, + __values: __values, + __read: __read, + __spread: __spread, + __await: __await, + __asyncGenerator: __asyncGenerator, + __asyncDelegator: __asyncDelegator, + __asyncValues: __asyncValues, + __makeTemplateObject: __makeTemplateObject, + __importStar: __importStar, + __importDefault: __importDefault +}); + var es6Promise_auto = createCommonjsModule(function (module, exports) { /*! * @overview es6-promise - a tiny implementation of Promises/A+. @@ -1214,36 +1419,51 @@ return Promise$2; }); var Errors = createCommonjsModule(function (module, exports) { -var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () { - var extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; - return function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -Object.defineProperty(exports, "__esModule", { value: true }); -var HttpError = /** @class */ (function (_super) { - __extends(HttpError, _super); - function HttpError(errorMessage, statusCode) { - var _this = _super.call(this, errorMessage) || this; - _this.statusCode = statusCode; - return _this; - } - return HttpError; -}(Error)); -exports.HttpError = HttpError; -var TimeoutError = /** @class */ (function (_super) { - __extends(TimeoutError, _super); - function TimeoutError(errorMessage) { - if (errorMessage === void 0) { errorMessage = "A timeout occurred."; } - return _super.call(this, errorMessage) || this; - } - return TimeoutError; -}(Error)); -exports.TimeoutError = TimeoutError; +Object.defineProperty(exports, "__esModule", { value: true }); + +/** Error thrown when an HTTP request fails. */ +var HttpError = /** @class */ (function (_super) { + tslib_1.__extends(HttpError, _super); + /** Constructs a new instance of {@link HttpError}. + * + * @param {string} errorMessage A descriptive error message. + * @param {number} statusCode The HTTP status code represented by this error. + */ + function HttpError(errorMessage, statusCode) { + var _newTarget = this.constructor; + var _this = this; + var trueProto = _newTarget.prototype; + _this = _super.call(this, errorMessage) || this; + _this.statusCode = statusCode; + // Workaround issue in Typescript compiler + // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 + _this.__proto__ = trueProto; + return _this; + } + return HttpError; +}(Error)); +exports.HttpError = HttpError; +/** Error thrown when a timeout elapses. */ +var TimeoutError = /** @class */ (function (_super) { + tslib_1.__extends(TimeoutError, _super); + /** Constructs a new instance of {@link TimeoutError}. + * + * @param {string} errorMessage A descriptive error message. + */ + function TimeoutError(errorMessage) { + var _newTarget = this.constructor; + if (errorMessage === void 0) { errorMessage = "A timeout occurred."; } + var _this = this; + var trueProto = _newTarget.prototype; + _this = _super.call(this, errorMessage) || this; + // Workaround issue in Typescript compiler + // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200 + _this.__proto__ = trueProto; + return _this; + } + return TimeoutError; +}(Error)); +exports.TimeoutError = TimeoutError; }); @@ -1251,95 +1471,127 @@ unwrapExports(Errors); var Errors_1 = Errors.HttpError; var Errors_2 = Errors.TimeoutError; -var HttpClient_1 = createCommonjsModule(function (module, exports) { -var __extends = (commonjsGlobal && commonjsGlobal.__extends) || (function () { - var extendStatics = Object.setPrototypeOf || - ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || - function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; - return function (d, b) { - extendStatics(d, b); - function __() { this.constructor = d; } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; -})(); -var __assign = (commonjsGlobal && commonjsGlobal.__assign) || Object.assign || function(t) { - for (var s, i = 1, n = arguments.length; i < n; i++) { - s = arguments[i]; - for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) - t[p] = s[p]; - } - return t; -}; -Object.defineProperty(exports, "__esModule", { value: true }); +var ILogger = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// These values are designed to match the ASP.NET Log Levels since that's the pattern we're emulating here. +/** Indicates the severity of a log message. + * + * Log Levels are ordered in increasing severity. So `Debug` is more severe than `Trace`, etc. + */ +var LogLevel; +(function (LogLevel) { + /** Log level for very low severity diagnostic messages. */ + LogLevel[LogLevel["Trace"] = 0] = "Trace"; + /** Log level for low severity diagnostic messages. */ + LogLevel[LogLevel["Debug"] = 1] = "Debug"; + /** Log level for informational diagnostic messages. */ + LogLevel[LogLevel["Information"] = 2] = "Information"; + /** Log level for diagnostic messages that indicate a non-fatal problem. */ + LogLevel[LogLevel["Warning"] = 3] = "Warning"; + /** Log level for diagnostic messages that indicate a failure in the current operation. */ + LogLevel[LogLevel["Error"] = 4] = "Error"; + /** Log level for diagnostic messages that indicate a failure that will terminate the entire application. */ + LogLevel[LogLevel["Critical"] = 5] = "Critical"; + /** The highest possible log level. Used when configuring logging to indicate that no log messages should be emitted. */ + LogLevel[LogLevel["None"] = 6] = "None"; +})(LogLevel = exports.LogLevel || (exports.LogLevel = {})); -var HttpResponse = /** @class */ (function () { - function HttpResponse(statusCode, statusText, content) { - this.statusCode = statusCode; - this.statusText = statusText; - this.content = content; - } - return HttpResponse; -}()); -exports.HttpResponse = HttpResponse; -var HttpClient = /** @class */ (function () { - function HttpClient() { - } - HttpClient.prototype.get = function (url, options) { - return this.send(__assign({}, options, { method: "GET", url: url })); - }; - HttpClient.prototype.post = function (url, options) { - return this.send(__assign({}, options, { method: "POST", url: url })); - }; - return HttpClient; -}()); -exports.HttpClient = HttpClient; -var DefaultHttpClient = /** @class */ (function (_super) { - __extends(DefaultHttpClient, _super); - function DefaultHttpClient() { - return _super !== null && _super.apply(this, arguments) || this; - } - DefaultHttpClient.prototype.send = function (request) { - return new Promise(function (resolve, reject) { - var xhr = new XMLHttpRequest(); - xhr.open(request.method, request.url, true); - xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); - if (request.headers) { - request.headers.forEach(function (value, header) { return xhr.setRequestHeader(header, value); }); - } - if (request.responseType) { - xhr.responseType = request.responseType; - } - if (request.abortSignal) { - request.abortSignal.onabort = function () { - xhr.abort(); - }; - } - if (request.timeout) { - xhr.timeout = request.timeout; - } - xhr.onload = function () { - if (request.abortSignal) { - request.abortSignal.onabort = null; - } - if (xhr.status >= 200 && xhr.status < 300) { - resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText)); - } - else { - reject(new Errors.HttpError(xhr.statusText, xhr.status)); - } - }; - xhr.onerror = function () { - reject(new Errors.HttpError(xhr.statusText, xhr.status)); - }; - xhr.ontimeout = function () { - reject(new Errors.TimeoutError()); - }; - xhr.send(request.content || ""); - }); - }; - return DefaultHttpClient; -}(HttpClient)); -exports.DefaultHttpClient = DefaultHttpClient; +}); + +unwrapExports(ILogger); +var ILogger_1 = ILogger.LogLevel; + +var HttpClient_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + +/** Represents an HTTP response. */ +var HttpResponse = /** @class */ (function () { + function HttpResponse(statusCode, statusText, content) { + this.statusCode = statusCode; + this.statusText = statusText; + this.content = content; + } + return HttpResponse; +}()); +exports.HttpResponse = HttpResponse; +/** Abstraction over an HTTP client. + * + * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms. + */ +var HttpClient = /** @class */ (function () { + function HttpClient() { + } + HttpClient.prototype.get = function (url, options) { + return this.send(tslib_1.__assign({}, options, { method: "GET", url: url })); + }; + HttpClient.prototype.post = function (url, options) { + return this.send(tslib_1.__assign({}, options, { method: "POST", url: url })); + }; + HttpClient.prototype.delete = function (url, options) { + return this.send(tslib_1.__assign({}, options, { method: "DELETE", url: url })); + }; + return HttpClient; +}()); +exports.HttpClient = HttpClient; +/** Default implementation of {@link HttpClient}. */ +var DefaultHttpClient = /** @class */ (function (_super) { + tslib_1.__extends(DefaultHttpClient, _super); + /** Creates a new instance of the {@link DefaultHttpClient}, using the provided {@link ILogger} to log messages. */ + function DefaultHttpClient(logger) { + var _this = _super.call(this) || this; + _this.logger = logger; + return _this; + } + /** @inheritDoc */ + DefaultHttpClient.prototype.send = function (request) { + var _this = this; + return new Promise(function (resolve, reject) { + var xhr = new XMLHttpRequest(); + xhr.open(request.method, request.url, true); + xhr.withCredentials = true; + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + if (request.headers) { + Object.keys(request.headers) + .forEach(function (header) { return xhr.setRequestHeader(header, request.headers[header]); }); + } + if (request.responseType) { + xhr.responseType = request.responseType; + } + if (request.abortSignal) { + request.abortSignal.onabort = function () { + xhr.abort(); + }; + } + if (request.timeout) { + xhr.timeout = request.timeout; + } + xhr.onload = function () { + if (request.abortSignal) { + request.abortSignal.onabort = null; + } + if (xhr.status >= 200 && xhr.status < 300) { + resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText)); + } + else { + reject(new Errors.HttpError(xhr.statusText, xhr.status)); + } + }; + xhr.onerror = function () { + _this.logger.log(ILogger.LogLevel.Warning, "Error from HTTP request. " + xhr.status + ": " + xhr.statusText); + reject(new Errors.HttpError(xhr.statusText, xhr.status)); + }; + xhr.ontimeout = function () { + _this.logger.log(ILogger.LogLevel.Warning, "Timeout from HTTP request."); + reject(new Errors.TimeoutError()); + }; + xhr.send(request.content || ""); + }); + }; + return DefaultHttpClient; +}(HttpClient)); +exports.DefaultHttpClient = DefaultHttpClient; }); @@ -1348,1219 +1600,1834 @@ var HttpClient_2 = HttpClient_1.HttpResponse; var HttpClient_3 = HttpClient_1.HttpClient; var HttpClient_4 = HttpClient_1.DefaultHttpClient; -var ILogger = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); -var LogLevel; -(function (LogLevel) { - LogLevel[LogLevel["Trace"] = 0] = "Trace"; - LogLevel[LogLevel["Information"] = 1] = "Information"; - LogLevel[LogLevel["Warning"] = 2] = "Warning"; - LogLevel[LogLevel["Error"] = 3] = "Error"; - LogLevel[LogLevel["None"] = 4] = "None"; -})(LogLevel = exports.LogLevel || (exports.LogLevel = {})); +var TextMessageFormat_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// Not exported from index +var TextMessageFormat = /** @class */ (function () { + function TextMessageFormat() { + } + TextMessageFormat.write = function (output) { + return "" + output + TextMessageFormat.RecordSeparator; + }; + TextMessageFormat.parse = function (input) { + if (input[input.length - 1] !== TextMessageFormat.RecordSeparator) { + throw new Error("Message is incomplete."); + } + var messages = input.split(TextMessageFormat.RecordSeparator); + messages.pop(); + return messages; + }; + TextMessageFormat.RecordSeparatorCode = 0x1e; + TextMessageFormat.RecordSeparator = String.fromCharCode(TextMessageFormat.RecordSeparatorCode); + return TextMessageFormat; +}()); +exports.TextMessageFormat = TextMessageFormat; }); -unwrapExports(ILogger); -var ILogger_1 = ILogger.LogLevel; - -var Loggers = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); +unwrapExports(TextMessageFormat_1); +var TextMessageFormat_2 = TextMessageFormat_1.TextMessageFormat; -var NullLogger = /** @class */ (function () { - function NullLogger() { - } - NullLogger.prototype.log = function (logLevel, message) { - }; - return NullLogger; -}()); -exports.NullLogger = NullLogger; -var ConsoleLogger = /** @class */ (function () { - function ConsoleLogger(minimumLogLevel) { - this.minimumLogLevel = minimumLogLevel; - } - ConsoleLogger.prototype.log = function (logLevel, message) { - if (logLevel >= this.minimumLogLevel) { - switch (logLevel) { - case ILogger.LogLevel.Error: - console.error(ILogger.LogLevel[logLevel] + ": " + message); - break; - case ILogger.LogLevel.Warning: - console.warn(ILogger.LogLevel[logLevel] + ": " + message); - break; - case ILogger.LogLevel.Information: - console.info(ILogger.LogLevel[logLevel] + ": " + message); - break; - default: - console.log(ILogger.LogLevel[logLevel] + ": " + message); - break; - } - } - }; - return ConsoleLogger; -}()); -exports.ConsoleLogger = ConsoleLogger; -var LoggerFactory = /** @class */ (function () { - function LoggerFactory() { - } - LoggerFactory.createLogger = function (logging) { - if (logging === undefined) { - return new ConsoleLogger(ILogger.LogLevel.Information); - } - if (logging === null) { - return new NullLogger(); - } - if (logging.log) { - return logging; - } - return new ConsoleLogger(logging); - }; - return LoggerFactory; -}()); -exports.LoggerFactory = LoggerFactory; +var HandshakeProtocol_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + +var HandshakeProtocol = /** @class */ (function () { + function HandshakeProtocol() { + } + // Handshake request is always JSON + HandshakeProtocol.prototype.writeHandshakeRequest = function (handshakeRequest) { + return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(handshakeRequest)); + }; + HandshakeProtocol.prototype.parseHandshakeResponse = function (data) { + var responseMessage; + var messageData; + var remainingData; + if (data instanceof ArrayBuffer) { + // Format is binary but still need to read JSON text from handshake response + var binaryData = new Uint8Array(data); + var separatorIndex = binaryData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparatorCode); + if (separatorIndex === -1) { + throw new Error("Message is incomplete."); + } + // content before separator is handshake response + // optional content after is additional messages + var responseLength = separatorIndex + 1; + messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength)); + remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null; + } + else { + var textData = data; + var separatorIndex = textData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparator); + if (separatorIndex === -1) { + throw new Error("Message is incomplete."); + } + // content before separator is handshake response + // optional content after is additional messages + var responseLength = separatorIndex + 1; + messageData = textData.substring(0, responseLength); + remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null; + } + // At this point we should have just the single handshake message + var messages = TextMessageFormat_1.TextMessageFormat.parse(messageData); + responseMessage = JSON.parse(messages[0]); + // multiple messages could have arrived with handshake + // return additional data to be parsed as usual, or null if all parsed + return [remainingData, responseMessage]; + }; + return HandshakeProtocol; +}()); +exports.HandshakeProtocol = HandshakeProtocol; }); -unwrapExports(Loggers); -var Loggers_1 = Loggers.NullLogger; -var Loggers_2 = Loggers.ConsoleLogger; -var Loggers_3 = Loggers.LoggerFactory; +unwrapExports(HandshakeProtocol_1); +var HandshakeProtocol_2 = HandshakeProtocol_1.HandshakeProtocol; -var AbortController_1 = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); -// Rough polyfill of https://developer.mozilla.org/en-US/docs/Web/API/AbortController -// We don't actually ever use the API being polyfilled, we always use the polyfill because -// it's a very new API right now. -var AbortController = /** @class */ (function () { - function AbortController() { - this.isAborted = false; - } - AbortController.prototype.abort = function () { - if (!this.isAborted) { - this.isAborted = true; - if (this.onabort) { - this.onabort(); - } - } - }; - Object.defineProperty(AbortController.prototype, "signal", { - get: function () { - return this; - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(AbortController.prototype, "aborted", { - get: function () { - return this.isAborted; - }, - enumerable: true, - configurable: true - }); - return AbortController; -}()); -exports.AbortController = AbortController; +var IHubProtocol = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +/** Defines the type of a Hub Message. */ +var MessageType; +(function (MessageType) { + /** Indicates the message is an Invocation message and implements the {@link InvocationMessage} interface. */ + MessageType[MessageType["Invocation"] = 1] = "Invocation"; + /** Indicates the message is a StreamItem message and implements the {@link StreamItemMessage} interface. */ + MessageType[MessageType["StreamItem"] = 2] = "StreamItem"; + /** Indicates the message is a Completion message and implements the {@link CompletionMessage} interface. */ + MessageType[MessageType["Completion"] = 3] = "Completion"; + /** Indicates the message is a Stream Invocation message and implements the {@link StreamInvocationMessage} interface. */ + MessageType[MessageType["StreamInvocation"] = 4] = "StreamInvocation"; + /** Indicates the message is a Cancel Invocation message and implements the {@link CancelInvocationMessage} interface. */ + MessageType[MessageType["CancelInvocation"] = 5] = "CancelInvocation"; + /** Indicates the message is a Ping message and implements the {@link PingMessage} interface. */ + MessageType[MessageType["Ping"] = 6] = "Ping"; + /** Indicates the message is a Close message and implements the {@link CloseMessage} interface. */ + MessageType[MessageType["Close"] = 7] = "Close"; +})(MessageType = exports.MessageType || (exports.MessageType = {})); }); -unwrapExports(AbortController_1); -var AbortController_2 = AbortController_1.AbortController; - -var Transports = createCommonjsModule(function (module, exports) { -var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [0, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); - - +unwrapExports(IHubProtocol); +var IHubProtocol_1 = IHubProtocol.MessageType; -var TransportType; -(function (TransportType) { - TransportType[TransportType["WebSockets"] = 0] = "WebSockets"; - TransportType[TransportType["ServerSentEvents"] = 1] = "ServerSentEvents"; - TransportType[TransportType["LongPolling"] = 2] = "LongPolling"; -})(TransportType = exports.TransportType || (exports.TransportType = {})); -var WebSocketTransport = /** @class */ (function () { - function WebSocketTransport(accessTokenFactory, logger) { - this.logger = logger; - this.accessTokenFactory = accessTokenFactory || (function () { return null; }); - } - WebSocketTransport.prototype.connect = function (url, requestedTransferMode, connection) { - var _this = this; - return new Promise(function (resolve, reject) { - url = url.replace(/^http/, "ws"); - var token = _this.accessTokenFactory(); - if (token) { - url += (url.indexOf("?") < 0 ? "?" : "&") + ("access_token=" + encodeURIComponent(token)); - } - var webSocket = new WebSocket(url); - if (requestedTransferMode === 2 /* Binary */) { - webSocket.binaryType = "arraybuffer"; - } - webSocket.onopen = function (event) { - _this.logger.log(ILogger.LogLevel.Information, "WebSocket connected to " + url); - _this.webSocket = webSocket; - resolve(requestedTransferMode); - }; - webSocket.onerror = function (event) { - reject(); - }; - webSocket.onmessage = function (message) { - _this.logger.log(ILogger.LogLevel.Trace, "(WebSockets transport) data received: " + message.data); - if (_this.onreceive) { - _this.onreceive(message.data); - } - }; - webSocket.onclose = function (event) { - // webSocket will be null if the transport did not start successfully - if (_this.onclose && _this.webSocket) { - if (event.wasClean === false || event.code !== 1000) { - _this.onclose(new Error("Websocket closed with status code: " + event.code + " (" + event.reason + ")")); - } - else { - _this.onclose(); - } - } - }; - }); - }; - WebSocketTransport.prototype.send = function (data) { - if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) { - this.webSocket.send(data); - return Promise.resolve(); - } - return Promise.reject("WebSocket is not in the OPEN state"); - }; - WebSocketTransport.prototype.stop = function () { - if (this.webSocket) { - this.webSocket.close(); - this.webSocket = null; - } - return Promise.resolve(); - }; - return WebSocketTransport; -}()); -exports.WebSocketTransport = WebSocketTransport; -var ServerSentEventsTransport = /** @class */ (function () { - function ServerSentEventsTransport(httpClient, accessTokenFactory, logger) { - this.httpClient = httpClient; - this.accessTokenFactory = accessTokenFactory || (function () { return null; }); - this.logger = logger; - } - ServerSentEventsTransport.prototype.connect = function (url, requestedTransferMode, connection) { - var _this = this; - if (typeof (EventSource) === "undefined") { - Promise.reject("EventSource not supported by the browser."); - } - this.url = url; - return new Promise(function (resolve, reject) { - var token = _this.accessTokenFactory(); - if (token) { - url += (url.indexOf("?") < 0 ? "?" : "&") + ("access_token=" + encodeURIComponent(token)); - } - var eventSource = new EventSource(url); - try { - eventSource.onmessage = function (e) { - if (_this.onreceive) { - try { - _this.logger.log(ILogger.LogLevel.Trace, "(SSE transport) data received: " + e.data); - _this.onreceive(e.data); - } - catch (error) { - if (_this.onclose) { - _this.onclose(error); - } - return; - } - } - }; - eventSource.onerror = function (e) { - reject(); - // don't report an error if the transport did not start successfully - if (_this.eventSource && _this.onclose) { - _this.onclose(new Error(e.message || "Error occurred")); - } - }; - eventSource.onopen = function () { - _this.logger.log(ILogger.LogLevel.Information, "SSE connected to " + _this.url); - _this.eventSource = eventSource; - // SSE is a text protocol - resolve(1 /* Text */); - }; - } - catch (e) { - return Promise.reject(e); - } - }); - }; - ServerSentEventsTransport.prototype.send = function (data) { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/, send(this.httpClient, this.url, this.accessTokenFactory, data)]; - }); - }); - }; - ServerSentEventsTransport.prototype.stop = function () { - if (this.eventSource) { - this.eventSource.close(); - this.eventSource = null; - } - return Promise.resolve(); - }; - return ServerSentEventsTransport; -}()); -exports.ServerSentEventsTransport = ServerSentEventsTransport; -var LongPollingTransport = /** @class */ (function () { - function LongPollingTransport(httpClient, accessTokenFactory, logger) { - this.httpClient = httpClient; - this.accessTokenFactory = accessTokenFactory || (function () { return null; }); - this.logger = logger; - this.pollAbort = new AbortController_1.AbortController(); - } - LongPollingTransport.prototype.connect = function (url, requestedTransferMode, connection) { - this.url = url; - // Set a flag indicating we have inherent keep-alive in this transport. - connection.features.inherentKeepAlive = true; - if (requestedTransferMode === 2 /* Binary */ && (typeof new XMLHttpRequest().responseType !== "string")) { - // This will work if we fix: https://github.com/aspnet/SignalR/issues/742 - throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported."); - } - this.poll(this.url, requestedTransferMode); - return Promise.resolve(requestedTransferMode); - }; - LongPollingTransport.prototype.poll = function (url, transferMode) { - return __awaiter(this, void 0, void 0, function () { - var pollOptions, token, pollUrl, response, e_1; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - pollOptions = { - abortSignal: this.pollAbort.signal, - headers: new Map(), - timeout: 120000, - }; - if (transferMode === 2 /* Binary */) { - pollOptions.responseType = "arraybuffer"; - } - token = this.accessTokenFactory(); - if (token) { - pollOptions.headers.set("Authorization", "Bearer " + token); - } - _a.label = 1; - case 1: - if (!!this.pollAbort.signal.aborted) return [3 /*break*/, 6]; - _a.label = 2; - case 2: - _a.trys.push([2, 4, , 5]); - pollUrl = url + "&_=" + Date.now(); - this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) polling: " + pollUrl); - return [4 /*yield*/, this.httpClient.get(pollUrl, pollOptions)]; - case 3: - response = _a.sent(); - if (response.statusCode === 204) { - this.logger.log(ILogger.LogLevel.Information, "(LongPolling transport) Poll terminated by server"); - // Poll terminated by server - if (this.onclose) { - this.onclose(); - } - this.pollAbort.abort(); - } - else if (response.statusCode !== 200) { - this.logger.log(ILogger.LogLevel.Error, "(LongPolling transport) Unexpected response code: " + response.statusCode); - // Unexpected status code - if (this.onclose) { - this.onclose(new Errors.HttpError(response.statusText, response.statusCode)); - } - this.pollAbort.abort(); - } - else { - // Process the response - if (response.content) { - this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) data received: " + response.content); - if (this.onreceive) { - this.onreceive(response.content); - } - } - else { - // This is another way timeout manifest. - this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); - } - } - return [3 /*break*/, 5]; - case 4: - e_1 = _a.sent(); - if (e_1 instanceof Errors.TimeoutError) { - // Ignore timeouts and reissue the poll. - this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); - } - else { - // Close the connection with the error as the result. - if (this.onclose) { - this.onclose(e_1); - } - this.pollAbort.abort(); - } - return [3 /*break*/, 5]; - case 5: return [3 /*break*/, 1]; - case 6: return [2 /*return*/]; - } - }); - }); - }; - LongPollingTransport.prototype.send = function (data) { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - return [2 /*return*/, send(this.httpClient, this.url, this.accessTokenFactory, data)]; - }); - }); - }; - LongPollingTransport.prototype.stop = function () { - this.pollAbort.abort(); - return Promise.resolve(); - }; - return LongPollingTransport; -}()); -exports.LongPollingTransport = LongPollingTransport; -function send(httpClient, url, accessTokenFactory, content) { - return __awaiter(this, void 0, void 0, function () { - var headers, token; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - token = accessTokenFactory(); - if (token) { - headers = new Map(); - headers.set("Authorization", "Bearer " + accessTokenFactory()); - } - return [4 /*yield*/, httpClient.post(url, { - content: content, - headers: headers, - })]; - case 1: - _a.sent(); - return [2 /*return*/]; - } - }); - }); -} +var Loggers = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +/** A logger that does nothing when log messages are sent to it. */ +var NullLogger = /** @class */ (function () { + function NullLogger() { + } + /** @inheritDoc */ + NullLogger.prototype.log = function (logLevel, message) { + }; + /** The singleton instance of the {@link NullLogger}. */ + NullLogger.instance = new NullLogger(); + return NullLogger; +}()); +exports.NullLogger = NullLogger; }); -unwrapExports(Transports); -var Transports_1 = Transports.TransportType; -var Transports_2 = Transports.WebSocketTransport; -var Transports_3 = Transports.ServerSentEventsTransport; -var Transports_4 = Transports.LongPollingTransport; - -var HttpConnection_1 = createCommonjsModule(function (module, exports) { -var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [0, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); +unwrapExports(Loggers); +var Loggers_1 = Loggers.NullLogger; +var Utils = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + +var Arg = /** @class */ (function () { + function Arg() { + } + Arg.isRequired = function (val, name) { + if (val === null || val === undefined) { + throw new Error("The '" + name + "' argument is required."); + } + }; + Arg.isIn = function (val, values, name) { + // TypeScript enums have keys for **both** the name and the value of each enum member on the type itself. + if (!(val in values)) { + throw new Error("Unknown " + name + " value: " + val + "."); + } + }; + return Arg; +}()); +exports.Arg = Arg; +function getDataDetail(data, includeContent) { + var length = null; + if (data instanceof ArrayBuffer) { + length = "Binary data of length " + data.byteLength; + if (includeContent) { + length += ". Content: '" + formatArrayBuffer(data) + "'"; + } + } + else if (typeof data === "string") { + length = "String data of length " + data.length; + if (includeContent) { + length += ". Content: '" + data + "'."; + } + } + return length; +} +exports.getDataDetail = getDataDetail; +function formatArrayBuffer(data) { + var view = new Uint8Array(data); + // Uint8Array.map only supports returning another Uint8Array? + var str = ""; + view.forEach(function (num) { + var pad = num < 16 ? "0" : ""; + str += "0x" + pad + num.toString(16) + " "; + }); + // Trim of trailing space. + return str.substr(0, str.length - 1); +} +exports.formatArrayBuffer = formatArrayBuffer; +function sendMessage(logger, transportName, httpClient, url, accessTokenFactory, content, logMessageContent) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var headers, token, response, _a; + return tslib_1.__generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, accessTokenFactory()]; + case 1: + token = _b.sent(); + if (token) { + headers = (_a = {}, _a["Authorization"] = "Bearer " + token, _a); + } + logger.log(ILogger.LogLevel.Trace, "(" + transportName + " transport) sending data. " + getDataDetail(content, logMessageContent) + "."); + return [4 /*yield*/, httpClient.post(url, { + content: content, + headers: headers, + })]; + case 2: + response = _b.sent(); + logger.log(ILogger.LogLevel.Trace, "(" + transportName + " transport) request complete. Response status: " + response.statusCode + "."); + return [2 /*return*/]; + } + }); + }); +} +exports.sendMessage = sendMessage; +function createLogger(logger) { + if (logger === undefined) { + return new ConsoleLogger(ILogger.LogLevel.Information); + } + if (logger === null) { + return Loggers.NullLogger.instance; + } + if (logger.log) { + return logger; + } + return new ConsoleLogger(logger); +} +exports.createLogger = createLogger; +var Subject = /** @class */ (function () { + function Subject(cancelCallback) { + this.observers = []; + this.cancelCallback = cancelCallback; + } + Subject.prototype.next = function (item) { + for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { + var observer = _a[_i]; + observer.next(item); + } + }; + Subject.prototype.error = function (err) { + for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { + var observer = _a[_i]; + if (observer.error) { + observer.error(err); + } + } + }; + Subject.prototype.complete = function () { + for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { + var observer = _a[_i]; + if (observer.complete) { + observer.complete(); + } + } + }; + Subject.prototype.subscribe = function (observer) { + this.observers.push(observer); + return new SubjectSubscription(this, observer); + }; + return Subject; +}()); +exports.Subject = Subject; +var SubjectSubscription = /** @class */ (function () { + function SubjectSubscription(subject, observer) { + this.subject = subject; + this.observer = observer; + } + SubjectSubscription.prototype.dispose = function () { + var index = this.subject.observers.indexOf(this.observer); + if (index > -1) { + this.subject.observers.splice(index, 1); + } + if (this.subject.observers.length === 0) { + this.subject.cancelCallback().catch(function (_) { }); + } + }; + return SubjectSubscription; +}()); +exports.SubjectSubscription = SubjectSubscription; +var ConsoleLogger = /** @class */ (function () { + function ConsoleLogger(minimumLogLevel) { + this.minimumLogLevel = minimumLogLevel; + } + ConsoleLogger.prototype.log = function (logLevel, message) { + if (logLevel >= this.minimumLogLevel) { + switch (logLevel) { + case ILogger.LogLevel.Critical: + case ILogger.LogLevel.Error: + console.error(ILogger.LogLevel[logLevel] + ": " + message); + break; + case ILogger.LogLevel.Warning: + console.warn(ILogger.LogLevel[logLevel] + ": " + message); + break; + case ILogger.LogLevel.Information: + console.info(ILogger.LogLevel[logLevel] + ": " + message); + break; + default: + // console.debug only goes to attached debuggers in Node, so we use console.log for Trace and Debug + console.log(ILogger.LogLevel[logLevel] + ": " + message); + break; + } + } + }; + return ConsoleLogger; +}()); +exports.ConsoleLogger = ConsoleLogger; +}); +unwrapExports(Utils); +var Utils_1 = Utils.Arg; +var Utils_2 = Utils.getDataDetail; +var Utils_3 = Utils.formatArrayBuffer; +var Utils_4 = Utils.sendMessage; +var Utils_5 = Utils.createLogger; +var Utils_6 = Utils.Subject; +var Utils_7 = Utils.SubjectSubscription; +var Utils_8 = Utils.ConsoleLogger; -var HttpConnection = /** @class */ (function () { - function HttpConnection(url, options) { - if (options === void 0) { options = {}; } - this.features = {}; - this.logger = Loggers.LoggerFactory.createLogger(options.logger); - this.baseUrl = this.resolveUrl(url); - options = options || {}; - options.accessTokenFactory = options.accessTokenFactory || (function () { return null; }); - this.httpClient = options.httpClient || new HttpClient_1.DefaultHttpClient(); - this.connectionState = 2 /* Disconnected */; - this.options = options; - } - HttpConnection.prototype.start = function () { - return __awaiter(this, void 0, void 0, function () { - return __generator(this, function (_a) { - if (this.connectionState !== 2 /* Disconnected */) { - return [2 /*return*/, Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state."))]; - } - this.connectionState = 0 /* Connecting */; - this.startPromise = this.startInternal(); - return [2 /*return*/, this.startPromise]; - }); - }); - }; - HttpConnection.prototype.startInternal = function () { - return __awaiter(this, void 0, void 0, function () { - var _this = this; - var headers, token, negotiatePayload, negotiateResponse, requestedTransferMode, _a, e_1; - return __generator(this, function (_b) { - switch (_b.label) { - case 0: - _b.trys.push([0, 5, , 6]); - if (!(this.options.transport === Transports.TransportType.WebSockets)) return [3 /*break*/, 1]; - // No need to add a connection ID in this case - this.url = this.baseUrl; - this.transport = this.createTransport(this.options.transport, [Transports.TransportType[Transports.TransportType.WebSockets]]); - return [3 /*break*/, 3]; - case 1: - headers = void 0; - token = this.options.accessTokenFactory(); - if (token) { - headers = new Map(); - headers.set("Authorization", "Bearer " + token); - } - return [4 /*yield*/, this.httpClient.post(this.resolveNegotiateUrl(this.baseUrl), { - content: "", - headers: headers, - })]; - case 2: - negotiatePayload = _b.sent(); - negotiateResponse = JSON.parse(negotiatePayload.content); - this.connectionId = negotiateResponse.connectionId; - // the user tries to stop the the connection when it is being started - if (this.connectionState === 2 /* Disconnected */) { - return [2 /*return*/]; - } - if (this.connectionId) { - this.url = this.baseUrl + (this.baseUrl.indexOf("?") === -1 ? "?" : "&") + ("id=" + this.connectionId); - this.transport = this.createTransport(this.options.transport, negotiateResponse.availableTransports); - } - _b.label = 3; - case 3: - this.transport.onreceive = this.onreceive; - this.transport.onclose = function (e) { return _this.stopConnection(true, e); }; - requestedTransferMode = this.features.transferMode === 2 /* Binary */ - ? 2 /* Binary */ - : 1 /* Text */; - _a = this.features; - return [4 /*yield*/, this.transport.connect(this.url, requestedTransferMode, this)]; - case 4: - _a.transferMode = _b.sent(); - // only change the state if we were connecting to not overwrite - // the state if the connection is already marked as Disconnected - this.changeState(0 /* Connecting */, 1 /* Connected */); - return [3 /*break*/, 6]; - case 5: - e_1 = _b.sent(); - this.logger.log(ILogger.LogLevel.Error, "Failed to start the connection. " + e_1); - this.connectionState = 2 /* Disconnected */; - this.transport = null; - throw e_1; - case 6: return [2 /*return*/]; - } - }); - }); - }; - HttpConnection.prototype.createTransport = function (transport, availableTransports) { - if ((transport === null || transport === undefined) && availableTransports.length > 0) { - transport = Transports.TransportType[availableTransports[0]]; - } - if (transport === Transports.TransportType.WebSockets && availableTransports.indexOf(Transports.TransportType[transport]) >= 0) { - return new Transports.WebSocketTransport(this.options.accessTokenFactory, this.logger); - } - if (transport === Transports.TransportType.ServerSentEvents && availableTransports.indexOf(Transports.TransportType[transport]) >= 0) { - return new Transports.ServerSentEventsTransport(this.httpClient, this.options.accessTokenFactory, this.logger); - } - if (transport === Transports.TransportType.LongPolling && availableTransports.indexOf(Transports.TransportType[transport]) >= 0) { - return new Transports.LongPollingTransport(this.httpClient, this.options.accessTokenFactory, this.logger); - } - if (this.isITransport(transport)) { - return transport; - } - throw new Error("No available transports found."); - }; - HttpConnection.prototype.isITransport = function (transport) { - return typeof (transport) === "object" && "connect" in transport; - }; - HttpConnection.prototype.changeState = function (from, to) { - if (this.connectionState === from) { - this.connectionState = to; - return true; - } - return false; - }; - HttpConnection.prototype.send = function (data) { - if (this.connectionState !== 1 /* Connected */) { - throw new Error("Cannot send data if the connection is not in the 'Connected' State"); - } - return this.transport.send(data); - }; - HttpConnection.prototype.stop = function (error) { - return __awaiter(this, void 0, void 0, function () { - var previousState, e_2; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - previousState = this.connectionState; - this.connectionState = 2 /* Disconnected */; - _a.label = 1; - case 1: - _a.trys.push([1, 3, , 4]); - return [4 /*yield*/, this.startPromise]; - case 2: - _a.sent(); - return [3 /*break*/, 4]; - case 3: - e_2 = _a.sent(); - return [3 /*break*/, 4]; - case 4: - this.stopConnection(/*raiseClosed*/ previousState === 1 /* Connected */, error); - return [2 /*return*/]; - } - }); - }); - }; - HttpConnection.prototype.stopConnection = function (raiseClosed, error) { - if (this.transport) { - this.transport.stop(); - this.transport = null; - } - if (error) { - this.logger.log(ILogger.LogLevel.Error, "Connection disconnected with error '" + error + "'."); - } - else { - this.logger.log(ILogger.LogLevel.Information, "Connection disconnected."); - } - this.connectionState = 2 /* Disconnected */; - if (raiseClosed && this.onclose) { - this.onclose(error); - } - }; - HttpConnection.prototype.resolveUrl = function (url) { - // startsWith is not supported in IE - if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) { - return url; - } - if (typeof window === "undefined" || !window || !window.document) { - throw new Error("Cannot resolve '" + url + "'."); - } - var parser = window.document.createElement("a"); - parser.href = url; - var baseUrl = (!parser.protocol || parser.protocol === ":") - ? window.document.location.protocol + "//" + (parser.host || window.document.location.host) - : parser.protocol + "//" + parser.host; - if (!url || url[0] !== "/") { - url = "/" + url; - } - var normalizedUrl = baseUrl + url; - this.logger.log(ILogger.LogLevel.Information, "Normalizing '" + url + "' to '" + normalizedUrl + "'"); - return normalizedUrl; - }; - HttpConnection.prototype.resolveNegotiateUrl = function (url) { - var index = url.indexOf("?"); - var negotiateUrl = url.substring(0, index === -1 ? url.length : index); - if (negotiateUrl[negotiateUrl.length - 1] !== "/") { - negotiateUrl += "/"; - } - negotiateUrl += "negotiate"; - negotiateUrl += index === -1 ? "" : url.substring(index); - return negotiateUrl; - }; - return HttpConnection; -}()); -exports.HttpConnection = HttpConnection; +var HubConnection_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + + +var DEFAULT_TIMEOUT_IN_MS = 30 * 1000; +/** Represents a connection to a SignalR Hub. */ +var HubConnection = /** @class */ (function () { + function HubConnection(connection, logger, protocol) { + var _this = this; + Utils.Arg.isRequired(connection, "connection"); + Utils.Arg.isRequired(logger, "logger"); + Utils.Arg.isRequired(protocol, "protocol"); + this.serverTimeoutInMilliseconds = DEFAULT_TIMEOUT_IN_MS; + this.logger = logger; + this.protocol = protocol; + this.connection = connection; + this.handshakeProtocol = new HandshakeProtocol_1.HandshakeProtocol(); + this.connection.onreceive = function (data) { return _this.processIncomingData(data); }; + this.connection.onclose = function (error) { return _this.connectionClosed(error); }; + this.callbacks = {}; + this.methods = {}; + this.closedCallbacks = []; + this.id = 0; + } + /** @internal */ + // Using a public static factory method means we can have a private constructor and an _internal_ + // create method that can be used by HubConnectionBuilder. An "internal" constructor would just + // be stripped away and the '.d.ts' file would have no constructor, which is interpreted as a + // public parameter-less constructor. + HubConnection.create = function (connection, logger, protocol) { + return new HubConnection(connection, logger, protocol); + }; + /** Starts the connection. + * + * @returns {Promise} A Promise that resolves when the connection has been successfully established, or rejects with an error. + */ + HubConnection.prototype.start = function () { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var handshakeRequest; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + handshakeRequest = { + protocol: this.protocol.name, + version: this.protocol.version, + }; + this.logger.log(ILogger.LogLevel.Debug, "Starting HubConnection."); + this.receivedHandshakeResponse = false; + return [4 /*yield*/, this.connection.start(this.protocol.transferFormat)]; + case 1: + _a.sent(); + this.logger.log(ILogger.LogLevel.Debug, "Sending handshake request."); + return [4 /*yield*/, this.connection.send(this.handshakeProtocol.writeHandshakeRequest(handshakeRequest))]; + case 2: + _a.sent(); + this.logger.log(ILogger.LogLevel.Information, "Using HubProtocol '" + this.protocol.name + "'."); + // defensively cleanup timeout in case we receive a message from the server before we finish start + this.cleanupTimeout(); + this.configureTimeout(); + return [2 /*return*/]; + } + }); + }); + }; + /** Stops the connection. + * + * @returns {Promise} A Promise that resolves when the connection has been successfully terminated, or rejects with an error. + */ + HubConnection.prototype.stop = function () { + this.logger.log(ILogger.LogLevel.Debug, "Stopping HubConnection."); + this.cleanupTimeout(); + return this.connection.stop(); + }; + /** Invokes a streaming hub method on the server using the specified name and arguments. + * + * @typeparam T The type of the items returned by the server. + * @param {string} methodName The name of the server method to invoke. + * @param {any[]} args The arguments used to invoke the server method. + * @returns {IStreamResult} An object that yields results from the server as they are received. + */ + HubConnection.prototype.stream = function (methodName) { + var _this = this; + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + var invocationDescriptor = this.createStreamInvocation(methodName, args); + var subject = new Utils.Subject(function () { + var cancelInvocation = _this.createCancelInvocation(invocationDescriptor.invocationId); + var cancelMessage = _this.protocol.writeMessage(cancelInvocation); + delete _this.callbacks[invocationDescriptor.invocationId]; + return _this.connection.send(cancelMessage); + }); + this.callbacks[invocationDescriptor.invocationId] = function (invocationEvent, error) { + if (error) { + subject.error(error); + return; + } + if (invocationEvent.type === IHubProtocol.MessageType.Completion) { + if (invocationEvent.error) { + subject.error(new Error(invocationEvent.error)); + } + else { + subject.complete(); + } + } + else { + subject.next((invocationEvent.item)); + } + }; + var message = this.protocol.writeMessage(invocationDescriptor); + this.connection.send(message) + .catch(function (e) { + subject.error(e); + delete _this.callbacks[invocationDescriptor.invocationId]; + }); + return subject; + }; + /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. + * + * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still + * be processing the invocation. + * + * @param {string} methodName The name of the server method to invoke. + * @param {any[]} args The arguments used to invoke the server method. + * @returns {Promise} A Promise that resolves when the invocation has been successfully sent, or rejects with an error. + */ + HubConnection.prototype.send = function (methodName) { + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + var invocationDescriptor = this.createInvocation(methodName, args, true); + var message = this.protocol.writeMessage(invocationDescriptor); + return this.connection.send(message); + }; + /** Invokes a hub method on the server using the specified name and arguments. + * + * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise + * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of + * resolving the Promise. + * + * @typeparam T The expected return type. + * @param {string} methodName The name of the server method to invoke. + * @param {any[]} args The arguments used to invoke the server method. + * @returns {Promise} A Promise that resolves with the result of the server method (if any), or rejects with an error. + */ + HubConnection.prototype.invoke = function (methodName) { + var _this = this; + var args = []; + for (var _i = 1; _i < arguments.length; _i++) { + args[_i - 1] = arguments[_i]; + } + var invocationDescriptor = this.createInvocation(methodName, args, false); + var p = new Promise(function (resolve, reject) { + _this.callbacks[invocationDescriptor.invocationId] = function (invocationEvent, error) { + if (error) { + reject(error); + return; + } + if (invocationEvent.type === IHubProtocol.MessageType.Completion) { + var completionMessage = invocationEvent; + if (completionMessage.error) { + reject(new Error(completionMessage.error)); + } + else { + resolve(completionMessage.result); + } + } + else { + reject(new Error("Unexpected message type: " + invocationEvent.type)); + } + }; + var message = _this.protocol.writeMessage(invocationDescriptor); + _this.connection.send(message) + .catch(function (e) { + reject(e); + delete _this.callbacks[invocationDescriptor.invocationId]; + }); + }); + return p; + }; + /** Registers a handler that will be invoked when the hub method with the specified method name is invoked. + * + * @param {string} methodName The name of the hub method to define. + * @param {Function} newMethod The handler that will be raised when the hub method is invoked. + */ + HubConnection.prototype.on = function (methodName, newMethod) { + if (!methodName || !newMethod) { + return; + } + methodName = methodName.toLowerCase(); + if (!this.methods[methodName]) { + this.methods[methodName] = []; + } + // Preventing adding the same handler multiple times. + if (this.methods[methodName].indexOf(newMethod) !== -1) { + return; + } + this.methods[methodName].push(newMethod); + }; + HubConnection.prototype.off = function (methodName, method) { + if (!methodName) { + return; + } + methodName = methodName.toLowerCase(); + var handlers = this.methods[methodName]; + if (!handlers) { + return; + } + if (method) { + var removeIdx = handlers.indexOf(method); + if (removeIdx !== -1) { + handlers.splice(removeIdx, 1); + if (handlers.length === 0) { + delete this.methods[methodName]; + } + } + } + else { + delete this.methods[methodName]; + } + }; + /** Registers a handler that will be invoked when the connection is closed. + * + * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any). + */ + HubConnection.prototype.onclose = function (callback) { + if (callback) { + this.closedCallbacks.push(callback); + } + }; + HubConnection.prototype.processIncomingData = function (data) { + this.cleanupTimeout(); + if (!this.receivedHandshakeResponse) { + data = this.processHandshakeResponse(data); + this.receivedHandshakeResponse = true; + } + // Data may have all been read when processing handshake response + if (data) { + // Parse the messages + var messages = this.protocol.parseMessages(data, this.logger); + for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) { + var message = messages_1[_i]; + switch (message.type) { + case IHubProtocol.MessageType.Invocation: + this.invokeClientMethod(message); + break; + case IHubProtocol.MessageType.StreamItem: + case IHubProtocol.MessageType.Completion: + var callback = this.callbacks[message.invocationId]; + if (callback != null) { + if (message.type === IHubProtocol.MessageType.Completion) { + delete this.callbacks[message.invocationId]; + } + callback(message); + } + break; + case IHubProtocol.MessageType.Ping: + // Don't care about pings + break; + case IHubProtocol.MessageType.Close: + this.logger.log(ILogger.LogLevel.Information, "Close message received from server."); + this.connection.stop(message.error ? new Error("Server returned an error on close: " + message.error) : null); + break; + default: + this.logger.log(ILogger.LogLevel.Warning, "Invalid message type: " + message.type); + break; + } + } + } + this.configureTimeout(); + }; + HubConnection.prototype.processHandshakeResponse = function (data) { + var responseMessage; + var remainingData; + try { + _a = this.handshakeProtocol.parseHandshakeResponse(data), remainingData = _a[0], responseMessage = _a[1]; + } + catch (e) { + var message = "Error parsing handshake response: " + e; + this.logger.log(ILogger.LogLevel.Error, message); + var error = new Error(message); + this.connection.stop(error); + throw error; + } + if (responseMessage.error) { + var message = "Server returned handshake error: " + responseMessage.error; + this.logger.log(ILogger.LogLevel.Error, message); + this.connection.stop(new Error(message)); + } + else { + this.logger.log(ILogger.LogLevel.Debug, "Server handshake complete."); + } + return remainingData; + var _a; + }; + HubConnection.prototype.configureTimeout = function () { + var _this = this; + if (!this.connection.features || !this.connection.features.inherentKeepAlive) { + // Set the timeout timer + this.timeoutHandle = setTimeout(function () { return _this.serverTimeout(); }, this.serverTimeoutInMilliseconds); + } + }; + HubConnection.prototype.serverTimeout = function () { + // The server hasn't talked to us in a while. It doesn't like us anymore ... :( + // Terminate the connection + this.connection.stop(new Error("Server timeout elapsed without receiving a message from the server.")); + }; + HubConnection.prototype.invokeClientMethod = function (invocationMessage) { + var _this = this; + var methods = this.methods[invocationMessage.target.toLowerCase()]; + if (methods) { + methods.forEach(function (m) { return m.apply(_this, invocationMessage.arguments); }); + if (invocationMessage.invocationId) { + // This is not supported in v1. So we return an error to avoid blocking the server waiting for the response. + var message = "Server requested a response, which is not supported in this version of the client."; + this.logger.log(ILogger.LogLevel.Error, message); + this.connection.stop(new Error(message)); + } + } + else { + this.logger.log(ILogger.LogLevel.Warning, "No client method with the name '" + invocationMessage.target + "' found."); + } + }; + HubConnection.prototype.connectionClosed = function (error) { + var _this = this; + var callbacks = this.callbacks; + this.callbacks = {}; + Object.keys(callbacks) + .forEach(function (key) { + var callback = callbacks[key]; + callback(undefined, error ? error : new Error("Invocation canceled due to connection being closed.")); + }); + this.cleanupTimeout(); + this.closedCallbacks.forEach(function (c) { return c.apply(_this, [error]); }); + }; + HubConnection.prototype.cleanupTimeout = function () { + if (this.timeoutHandle) { + clearTimeout(this.timeoutHandle); + } + }; + HubConnection.prototype.createInvocation = function (methodName, args, nonblocking) { + if (nonblocking) { + return { + arguments: args, + target: methodName, + type: IHubProtocol.MessageType.Invocation, + }; + } + else { + var id = this.id; + this.id++; + return { + arguments: args, + invocationId: id.toString(), + target: methodName, + type: IHubProtocol.MessageType.Invocation, + }; + } + }; + HubConnection.prototype.createStreamInvocation = function (methodName, args) { + var id = this.id; + this.id++; + return { + arguments: args, + invocationId: id.toString(), + target: methodName, + type: IHubProtocol.MessageType.StreamInvocation, + }; + }; + HubConnection.prototype.createCancelInvocation = function (id) { + return { + invocationId: id, + type: IHubProtocol.MessageType.CancelInvocation, + }; + }; + return HubConnection; +}()); +exports.HubConnection = HubConnection; }); -unwrapExports(HttpConnection_1); -var HttpConnection_2 = HttpConnection_1.HttpConnection; - -var Base64EncodedHubProtocol_1 = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); -var Base64EncodedHubProtocol = /** @class */ (function () { - function Base64EncodedHubProtocol(protocol) { - this.wrappedProtocol = protocol; - this.name = this.wrappedProtocol.name; - this.type = 1 /* Text */; - } - Base64EncodedHubProtocol.prototype.parseMessages = function (input) { - // The format of the message is `size:message;` - var pos = input.indexOf(":"); - if (pos === -1 || input[input.length - 1] !== ";") { - throw new Error("Invalid payload."); - } - var lenStr = input.substring(0, pos); - if (!/^[0-9]+$/.test(lenStr)) { - throw new Error("Invalid length: '" + lenStr + "'"); - } - var messageSize = parseInt(lenStr, 10); - // 2 accounts for ':' after message size and trailing ';' - if (messageSize !== input.length - pos - 2) { - throw new Error("Invalid message size."); - } - var encodedMessage = input.substring(pos + 1, input.length - 1); - // atob/btoa are browsers APIs but they can be polyfilled. If this becomes problematic we can use - // base64-js module - var s = atob(encodedMessage); - var payload = new Uint8Array(s.length); - for (var i = 0; i < payload.length; i++) { - payload[i] = s.charCodeAt(i); - } - return this.wrappedProtocol.parseMessages(payload.buffer); - }; - Base64EncodedHubProtocol.prototype.writeMessage = function (message) { - var payload = new Uint8Array(this.wrappedProtocol.writeMessage(message)); - var s = ""; - for (var i = 0; i < payload.byteLength; i++) { - s += String.fromCharCode(payload[i]); - } - // atob/btoa are browsers APIs but they can be polyfilled. If this becomes problematic we can use - // base64-js module - var encodedMessage = btoa(s); - return encodedMessage.length.toString() + ":" + encodedMessage + ";"; - }; - return Base64EncodedHubProtocol; -}()); -exports.Base64EncodedHubProtocol = Base64EncodedHubProtocol; +unwrapExports(HubConnection_1); +var HubConnection_2 = HubConnection_1.HubConnection; + +var ITransport = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// This will be treated as a bit flag in the future, so we keep it using power-of-two values. +/** Specifies a specific HTTP transport type. */ +var HttpTransportType; +(function (HttpTransportType) { + /** Specifies no transport preference. */ + HttpTransportType[HttpTransportType["None"] = 0] = "None"; + /** Specifies the WebSockets transport. */ + HttpTransportType[HttpTransportType["WebSockets"] = 1] = "WebSockets"; + /** Specifies the Server-Sent Events transport. */ + HttpTransportType[HttpTransportType["ServerSentEvents"] = 2] = "ServerSentEvents"; + /** Specifies the Long Polling transport. */ + HttpTransportType[HttpTransportType["LongPolling"] = 4] = "LongPolling"; +})(HttpTransportType = exports.HttpTransportType || (exports.HttpTransportType = {})); +/** Specifies the transfer format for a connection. */ +var TransferFormat; +(function (TransferFormat) { + /** Specifies that only text data will be transmitted over the connection. */ + TransferFormat[TransferFormat["Text"] = 1] = "Text"; + /** Specifies that binary data will be transmitted over the connection. */ + TransferFormat[TransferFormat["Binary"] = 2] = "Binary"; +})(TransferFormat = exports.TransferFormat || (exports.TransferFormat = {})); }); -unwrapExports(Base64EncodedHubProtocol_1); -var Base64EncodedHubProtocol_2 = Base64EncodedHubProtocol_1.Base64EncodedHubProtocol; +unwrapExports(ITransport); +var ITransport_1 = ITransport.HttpTransportType; +var ITransport_2 = ITransport.TransferFormat; -var TextMessageFormat_1 = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); -var TextMessageFormat = /** @class */ (function () { - function TextMessageFormat() { - } - TextMessageFormat.write = function (output) { - return "" + output + TextMessageFormat.RecordSeparator; - }; - TextMessageFormat.parse = function (input) { - if (input[input.length - 1] !== TextMessageFormat.RecordSeparator) { - throw new Error("Message is incomplete."); - } - var messages = input.split(TextMessageFormat.RecordSeparator); - messages.pop(); - return messages; - }; - TextMessageFormat.RecordSeparator = String.fromCharCode(0x1e); - return TextMessageFormat; -}()); -exports.TextMessageFormat = TextMessageFormat; +var AbortController_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); +// Rough polyfill of https://developer.mozilla.org/en-US/docs/Web/API/AbortController +// We don't actually ever use the API being polyfilled, we always use the polyfill because +// it's a very new API right now. +// Not exported from index. +var AbortController = /** @class */ (function () { + function AbortController() { + this.isAborted = false; + } + AbortController.prototype.abort = function () { + if (!this.isAborted) { + this.isAborted = true; + if (this.onabort) { + this.onabort(); + } + } + }; + Object.defineProperty(AbortController.prototype, "signal", { + get: function () { + return this; + }, + enumerable: true, + configurable: true + }); + Object.defineProperty(AbortController.prototype, "aborted", { + get: function () { + return this.isAborted; + }, + enumerable: true, + configurable: true + }); + return AbortController; +}()); +exports.AbortController = AbortController; }); -unwrapExports(TextMessageFormat_1); -var TextMessageFormat_2 = TextMessageFormat_1.TextMessageFormat; - -var JsonHubProtocol_1 = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); +unwrapExports(AbortController_1); +var AbortController_2 = AbortController_1.AbortController; -exports.JSON_HUB_PROTOCOL_NAME = "json"; -var JsonHubProtocol = /** @class */ (function () { - function JsonHubProtocol() { - this.name = exports.JSON_HUB_PROTOCOL_NAME; - this.type = 1 /* Text */; - } - JsonHubProtocol.prototype.parseMessages = function (input) { - if (!input) { - return []; - } - // Parse the messages - var messages = TextMessageFormat_1.TextMessageFormat.parse(input); - var hubMessages = []; - for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) { - var message = messages_1[_i]; - hubMessages.push(JSON.parse(message)); - } - return hubMessages; - }; - JsonHubProtocol.prototype.writeMessage = function (message) { - return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(message)); - }; - return JsonHubProtocol; -}()); -exports.JsonHubProtocol = JsonHubProtocol; +var LongPollingTransport_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + + + +var SHUTDOWN_TIMEOUT = 5 * 1000; +// Not exported from 'index', this type is internal. +var LongPollingTransport = /** @class */ (function () { + function LongPollingTransport(httpClient, accessTokenFactory, logger, logMessageContent, shutdownTimeout) { + this.httpClient = httpClient; + this.accessTokenFactory = accessTokenFactory || (function () { return null; }); + this.logger = logger; + this.pollAbort = new AbortController_1.AbortController(); + this.logMessageContent = logMessageContent; + this.shutdownTimeout = shutdownTimeout || SHUTDOWN_TIMEOUT; + } + Object.defineProperty(LongPollingTransport.prototype, "pollAborted", { + // This is an internal type, not exported from 'index' so this is really just internal. + get: function () { + return this.pollAbort.aborted; + }, + enumerable: true, + configurable: true + }); + LongPollingTransport.prototype.connect = function (url, transferFormat) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var pollOptions, token, closeError, pollUrl, response; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + Utils.Arg.isRequired(url, "url"); + Utils.Arg.isRequired(transferFormat, "transferFormat"); + Utils.Arg.isIn(transferFormat, ITransport.TransferFormat, "transferFormat"); + this.url = url; + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Connecting"); + if (transferFormat === ITransport.TransferFormat.Binary && (typeof new XMLHttpRequest().responseType !== "string")) { + // This will work if we fix: https://github.com/aspnet/SignalR/issues/742 + throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported."); + } + pollOptions = { + abortSignal: this.pollAbort.signal, + headers: {}, + timeout: 90000, + }; + if (transferFormat === ITransport.TransferFormat.Binary) { + pollOptions.responseType = "arraybuffer"; + } + return [4 /*yield*/, this.accessTokenFactory()]; + case 1: + token = _a.sent(); + this.updateHeaderToken(pollOptions, token); + pollUrl = url + "&_=" + Date.now(); + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) polling: " + pollUrl); + return [4 /*yield*/, this.httpClient.get(pollUrl, pollOptions)]; + case 2: + response = _a.sent(); + if (response.statusCode !== 200) { + this.logger.log(ILogger.LogLevel.Error, "(LongPolling transport) Unexpected response code: " + response.statusCode); + // Mark running as false so that the poll immediately ends and runs the close logic + closeError = new Errors.HttpError(response.statusText, response.statusCode); + this.running = false; + } + else { + this.running = true; + } + this.poll(this.url, pollOptions, closeError); + return [2 /*return*/, Promise.resolve()]; + } + }); + }); + }; + LongPollingTransport.prototype.updateHeaderToken = function (request, token) { + if (token) { + // tslint:disable-next-line:no-string-literal + request.headers["Authorization"] = "Bearer " + token; + return; + } + // tslint:disable-next-line:no-string-literal + if (request.headers["Authorization"]) { + // tslint:disable-next-line:no-string-literal + delete request.headers["Authorization"]; + } + }; + LongPollingTransport.prototype.poll = function (url, pollOptions, closeError) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var token, pollUrl, response, e_1; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, , 8, 9]); + _a.label = 1; + case 1: + if (!this.running) return [3 /*break*/, 7]; + return [4 /*yield*/, this.accessTokenFactory()]; + case 2: + token = _a.sent(); + this.updateHeaderToken(pollOptions, token); + _a.label = 3; + case 3: + _a.trys.push([3, 5, , 6]); + pollUrl = url + "&_=" + Date.now(); + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) polling: " + pollUrl); + return [4 /*yield*/, this.httpClient.get(pollUrl, pollOptions)]; + case 4: + response = _a.sent(); + if (response.statusCode === 204) { + this.logger.log(ILogger.LogLevel.Information, "(LongPolling transport) Poll terminated by server"); + this.running = false; + } + else if (response.statusCode !== 200) { + this.logger.log(ILogger.LogLevel.Error, "(LongPolling transport) Unexpected response code: " + response.statusCode); + // Unexpected status code + closeError = new Errors.HttpError(response.statusText, response.statusCode); + this.running = false; + } + else { + // Process the response + if (response.content) { + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) data received. " + Utils.getDataDetail(response.content, this.logMessageContent)); + if (this.onreceive) { + this.onreceive(response.content); + } + } + else { + // This is another way timeout manifest. + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); + } + } + return [3 /*break*/, 6]; + case 5: + e_1 = _a.sent(); + if (!this.running) { + // Log but disregard errors that occur after we were stopped by DELETE + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Poll errored after shutdown: " + e_1.message); + } + else { + if (e_1 instanceof Errors.TimeoutError) { + // Ignore timeouts and reissue the poll. + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Poll timed out, reissuing."); + } + else { + // Close the connection with the error as the result. + closeError = e_1; + this.running = false; + } + } + return [3 /*break*/, 6]; + case 6: return [3 /*break*/, 1]; + case 7: return [3 /*break*/, 9]; + case 8: + // Indicate that we've stopped so the shutdown timer doesn't get registered. + this.stopped = true; + // Clean up the shutdown timer if it was registered + if (this.shutdownTimer) { + clearTimeout(this.shutdownTimer); + } + // Fire our onclosed event + if (this.onclose) { + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Firing onclose event. Error: " + (closeError || "")); + this.onclose(closeError); + } + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) Transport finished."); + return [7 /*endfinally*/]; + case 9: return [2 /*return*/]; + } + }); + }); + }; + LongPollingTransport.prototype.send = function (data) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + return tslib_1.__generator(this, function (_a) { + if (!this.running) { + return [2 /*return*/, Promise.reject(new Error("Cannot send until the transport is connected"))]; + } + return [2 /*return*/, Utils.sendMessage(this.logger, "LongPolling", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent)]; + }); + }); + }; + LongPollingTransport.prototype.stop = function () { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var _this = this; + var deleteOptions, token, response; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + _a.trys.push([0, , 3, 4]); + this.running = false; + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) sending DELETE request to " + this.url + "."); + deleteOptions = { + headers: {}, + }; + return [4 /*yield*/, this.accessTokenFactory()]; + case 1: + token = _a.sent(); + this.updateHeaderToken(deleteOptions, token); + return [4 /*yield*/, this.httpClient.delete(this.url, deleteOptions)]; + case 2: + response = _a.sent(); + this.logger.log(ILogger.LogLevel.Trace, "(LongPolling transport) DELETE request accepted."); + return [3 /*break*/, 4]; + case 3: + // Abort the poll after the shutdown timeout if the server doesn't stop the poll. + if (!this.stopped) { + this.shutdownTimer = setTimeout(function () { + _this.logger.log(ILogger.LogLevel.Warning, "(LongPolling transport) server did not terminate after DELETE request, canceling poll."); + // Abort any outstanding poll + _this.pollAbort.abort(); + }, this.shutdownTimeout); + } + return [7 /*endfinally*/]; + case 4: return [2 /*return*/]; + } + }); + }); + }; + return LongPollingTransport; +}()); +exports.LongPollingTransport = LongPollingTransport; }); -unwrapExports(JsonHubProtocol_1); -var JsonHubProtocol_2 = JsonHubProtocol_1.JSON_HUB_PROTOCOL_NAME; -var JsonHubProtocol_3 = JsonHubProtocol_1.JsonHubProtocol; - -var Observable = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); -var Subscription = /** @class */ (function () { - function Subscription(subject, observer) { - this.subject = subject; - this.observer = observer; - } - Subscription.prototype.dispose = function () { - var index = this.subject.observers.indexOf(this.observer); - if (index > -1) { - this.subject.observers.splice(index, 1); - } - if (this.subject.observers.length === 0) { - this.subject.cancelCallback().catch(function (_) { }); - } - }; - return Subscription; -}()); -exports.Subscription = Subscription; -var Subject = /** @class */ (function () { - function Subject(cancelCallback) { - this.observers = []; - this.cancelCallback = cancelCallback; - } - Subject.prototype.next = function (item) { - for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { - var observer = _a[_i]; - observer.next(item); - } - }; - Subject.prototype.error = function (err) { - for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { - var observer = _a[_i]; - if (observer.error) { - observer.error(err); - } - } - }; - Subject.prototype.complete = function () { - for (var _i = 0, _a = this.observers; _i < _a.length; _i++) { - var observer = _a[_i]; - if (observer.complete) { - observer.complete(); - } - } - }; - Subject.prototype.subscribe = function (observer) { - this.observers.push(observer); - return new Subscription(this, observer); - }; - return Subject; -}()); -exports.Subject = Subject; +unwrapExports(LongPollingTransport_1); +var LongPollingTransport_2 = LongPollingTransport_1.LongPollingTransport; + +var ServerSentEventsTransport_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + +var ServerSentEventsTransport = /** @class */ (function () { + function ServerSentEventsTransport(httpClient, accessTokenFactory, logger, logMessageContent) { + this.httpClient = httpClient; + this.accessTokenFactory = accessTokenFactory || (function () { return null; }); + this.logger = logger; + this.logMessageContent = logMessageContent; + } + ServerSentEventsTransport.prototype.connect = function (url, transferFormat) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var _this = this; + var token; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + Utils.Arg.isRequired(url, "url"); + Utils.Arg.isRequired(transferFormat, "transferFormat"); + Utils.Arg.isIn(transferFormat, ITransport.TransferFormat, "transferFormat"); + if (typeof (EventSource) === "undefined") { + throw new Error("'EventSource' is not supported in your environment."); + } + this.logger.log(ILogger.LogLevel.Trace, "(SSE transport) Connecting"); + return [4 /*yield*/, this.accessTokenFactory()]; + case 1: + token = _a.sent(); + if (token) { + url += (url.indexOf("?") < 0 ? "?" : "&") + ("access_token=" + encodeURIComponent(token)); + } + this.url = url; + return [2 /*return*/, new Promise(function (resolve, reject) { + var opened = false; + if (transferFormat !== ITransport.TransferFormat.Text) { + reject(new Error("The Server-Sent Events transport only supports the 'Text' transfer format")); + } + var eventSource = new EventSource(url, { withCredentials: true }); + try { + eventSource.onmessage = function (e) { + if (_this.onreceive) { + try { + _this.logger.log(ILogger.LogLevel.Trace, "(SSE transport) data received. " + Utils.getDataDetail(e.data, _this.logMessageContent) + "."); + _this.onreceive(e.data); + } + catch (error) { + if (_this.onclose) { + _this.onclose(error); + } + return; + } + } + }; + eventSource.onerror = function (e) { + var error = new Error(e.message || "Error occurred"); + if (opened) { + _this.close(error); + } + else { + reject(error); + } + }; + eventSource.onopen = function () { + _this.logger.log(ILogger.LogLevel.Information, "SSE connected to " + _this.url); + _this.eventSource = eventSource; + opened = true; + resolve(); + }; + } + catch (e) { + return Promise.reject(e); + } + })]; + } + }); + }); + }; + ServerSentEventsTransport.prototype.send = function (data) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + return tslib_1.__generator(this, function (_a) { + if (!this.eventSource) { + return [2 /*return*/, Promise.reject(new Error("Cannot send until the transport is connected"))]; + } + return [2 /*return*/, Utils.sendMessage(this.logger, "SSE", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent)]; + }); + }); + }; + ServerSentEventsTransport.prototype.stop = function () { + this.close(); + return Promise.resolve(); + }; + ServerSentEventsTransport.prototype.close = function (e) { + if (this.eventSource) { + this.eventSource.close(); + this.eventSource = null; + if (this.onclose) { + this.onclose(e); + } + } + }; + return ServerSentEventsTransport; +}()); +exports.ServerSentEventsTransport = ServerSentEventsTransport; }); -unwrapExports(Observable); -var Observable_1 = Observable.Subscription; -var Observable_2 = Observable.Subject; - -var HubConnection_1 = createCommonjsModule(function (module, exports) { -var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) { - return new (P || (P = Promise))(function (resolve, reject) { - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } - function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } - step((generator = generator.apply(thisArg, _arguments || [])).next()); - }); -}; -var __generator = (commonjsGlobal && commonjsGlobal.__generator) || function (thisArg, body) { - var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; - return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; - function verb(n) { return function (v) { return step([n, v]); }; } - function step(op) { - if (f) throw new TypeError("Generator is already executing."); - while (_) try { - if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t; - if (y = 0, t) op = [0, t.value]; - switch (op[0]) { - case 0: case 1: t = op; break; - case 4: _.label++; return { value: op[1], done: false }; - case 5: _.label++; y = op[1]; op = [0]; continue; - case 7: op = _.ops.pop(); _.trys.pop(); continue; - default: - if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } - if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } - if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } - if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } - if (t[2]) _.ops.pop(); - _.trys.pop(); continue; - } - op = body.call(thisArg, _); - } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } - if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; - } -}; -Object.defineProperty(exports, "__esModule", { value: true }); - +unwrapExports(ServerSentEventsTransport_1); +var ServerSentEventsTransport_2 = ServerSentEventsTransport_1.ServerSentEventsTransport; + +var WebSocketTransport_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + +var WebSocketTransport = /** @class */ (function () { + function WebSocketTransport(accessTokenFactory, logger, logMessageContent) { + this.logger = logger; + this.accessTokenFactory = accessTokenFactory || (function () { return null; }); + this.logMessageContent = logMessageContent; + } + WebSocketTransport.prototype.connect = function (url, transferFormat) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var _this = this; + var token; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + Utils.Arg.isRequired(url, "url"); + Utils.Arg.isRequired(transferFormat, "transferFormat"); + Utils.Arg.isIn(transferFormat, ITransport.TransferFormat, "transferFormat"); + if (typeof (WebSocket) === "undefined") { + throw new Error("'WebSocket' is not supported in your environment."); + } + this.logger.log(ILogger.LogLevel.Trace, "(WebSockets transport) Connecting"); + return [4 /*yield*/, this.accessTokenFactory()]; + case 1: + token = _a.sent(); + if (token) { + url += (url.indexOf("?") < 0 ? "?" : "&") + ("access_token=" + encodeURIComponent(token)); + } + return [2 /*return*/, new Promise(function (resolve, reject) { + url = url.replace(/^http/, "ws"); + var webSocket = new WebSocket(url); + if (transferFormat === ITransport.TransferFormat.Binary) { + webSocket.binaryType = "arraybuffer"; + } + webSocket.onopen = function (event) { + _this.logger.log(ILogger.LogLevel.Information, "WebSocket connected to " + url); + _this.webSocket = webSocket; + resolve(); + }; + webSocket.onerror = function (event) { + reject(event.error); + }; + webSocket.onmessage = function (message) { + _this.logger.log(ILogger.LogLevel.Trace, "(WebSockets transport) data received. " + Utils.getDataDetail(message.data, _this.logMessageContent) + "."); + if (_this.onreceive) { + _this.onreceive(message.data); + } + }; + webSocket.onclose = function (event) { + // webSocket will be null if the transport did not start successfully + _this.logger.log(ILogger.LogLevel.Trace, "(WebSockets transport) socket closed."); + if (_this.onclose) { + if (event.wasClean === false || event.code !== 1000) { + _this.onclose(new Error("Websocket closed with status code: " + event.code + " (" + event.reason + ")")); + } + else { + _this.onclose(); + } + } + }; + })]; + } + }); + }); + }; + WebSocketTransport.prototype.send = function (data) { + if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) { + this.logger.log(ILogger.LogLevel.Trace, "(WebSockets transport) sending data. " + Utils.getDataDetail(data, this.logMessageContent) + "."); + this.webSocket.send(data); + return Promise.resolve(); + } + return Promise.reject("WebSocket is not in the OPEN state"); + }; + WebSocketTransport.prototype.stop = function () { + if (this.webSocket) { + this.webSocket.close(); + this.webSocket = null; + } + return Promise.resolve(); + }; + return WebSocketTransport; +}()); +exports.WebSocketTransport = WebSocketTransport; +}); +unwrapExports(WebSocketTransport_1); +var WebSocketTransport_2 = WebSocketTransport_1.WebSocketTransport; -exports.JsonHubProtocol = JsonHubProtocol_1.JsonHubProtocol; +var HttpConnection_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + + + + + +var MAX_REDIRECTS = 100; +var HttpConnection = /** @class */ (function () { + function HttpConnection(url, options) { + if (options === void 0) { options = {}; } + this.features = {}; + Utils.Arg.isRequired(url, "url"); + this.logger = Utils.createLogger(options.logger); + this.baseUrl = this.resolveUrl(url); + options = options || {}; + options.accessTokenFactory = options.accessTokenFactory || (function () { return null; }); + options.logMessageContent = options.logMessageContent || false; + this.httpClient = options.httpClient || new HttpClient_1.DefaultHttpClient(this.logger); + this.connectionState = 2 /* Disconnected */; + this.options = options; + } + HttpConnection.prototype.start = function (transferFormat) { + transferFormat = transferFormat || ITransport.TransferFormat.Binary; + Utils.Arg.isIn(transferFormat, ITransport.TransferFormat, "transferFormat"); + this.logger.log(ILogger.LogLevel.Debug, "Starting connection with transfer format '" + ITransport.TransferFormat[transferFormat] + "'."); + if (this.connectionState !== 2 /* Disconnected */) { + return Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state.")); + } + this.connectionState = 0 /* Connecting */; + this.startPromise = this.startInternal(transferFormat); + return this.startPromise; + }; + HttpConnection.prototype.send = function (data) { + if (this.connectionState !== 1 /* Connected */) { + throw new Error("Cannot send data if the connection is not in the 'Connected' State."); + } + return this.transport.send(data); + }; + HttpConnection.prototype.stop = function (error) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var e_1; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + this.connectionState = 2 /* Disconnected */; + _a.label = 1; + case 1: + _a.trys.push([1, 3, , 4]); + return [4 /*yield*/, this.startPromise]; + case 2: + _a.sent(); + return [3 /*break*/, 4]; + case 3: + e_1 = _a.sent(); + return [3 /*break*/, 4]; + case 4: + if (!this.transport) return [3 /*break*/, 6]; + this.stopError = error; + return [4 /*yield*/, this.transport.stop()]; + case 5: + _a.sent(); + this.transport = null; + _a.label = 6; + case 6: return [2 /*return*/]; + } + }); + }); + }; + HttpConnection.prototype.startInternal = function (transferFormat) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var _this = this; + var url, negotiateResponse, redirects, _loop_1, this_1, state_1, e_2; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + url = this.baseUrl; + this.accessTokenFactory = this.options.accessTokenFactory; + _a.label = 1; + case 1: + _a.trys.push([1, 12, , 13]); + if (!this.options.skipNegotiation) return [3 /*break*/, 5]; + if (!(this.options.transport === ITransport.HttpTransportType.WebSockets)) return [3 /*break*/, 3]; + // No need to add a connection ID in this case + this.transport = this.constructTransport(ITransport.HttpTransportType.WebSockets); + // We should just call connect directly in this case. + // No fallback or negotiate in this case. + return [4 /*yield*/, this.transport.connect(url, transferFormat)]; + case 2: + // We should just call connect directly in this case. + // No fallback or negotiate in this case. + _a.sent(); + return [3 /*break*/, 4]; + case 3: throw Error("Negotiation can only be skipped when using the WebSocket transport directly."); + case 4: return [3 /*break*/, 11]; + case 5: + negotiateResponse = null; + redirects = 0; + _loop_1 = function () { + var accessToken_1; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: return [4 /*yield*/, this_1.getNegotiationResponse(url)]; + case 1: + negotiateResponse = _a.sent(); + // the user tries to stop the connection when it is being started + if (this_1.connectionState === 2 /* Disconnected */) { + return [2 /*return*/, { value: void 0 }]; + } + if (negotiateResponse.url) { + url = negotiateResponse.url; + } + if (negotiateResponse.accessToken) { + accessToken_1 = negotiateResponse.accessToken; + this_1.accessTokenFactory = function () { return accessToken_1; }; + } + redirects++; + return [2 /*return*/]; + } + }); + }; + this_1 = this; + _a.label = 6; + case 6: return [5 /*yield**/, _loop_1()]; + case 7: + state_1 = _a.sent(); + if (typeof state_1 === "object") + return [2 /*return*/, state_1.value]; + _a.label = 8; + case 8: + if (negotiateResponse.url && redirects < MAX_REDIRECTS) return [3 /*break*/, 6]; + _a.label = 9; + case 9: + if (redirects === MAX_REDIRECTS && negotiateResponse.url) { + throw Error("Negotiate redirection limit exceeded."); + } + return [4 /*yield*/, this.createTransport(url, this.options.transport, negotiateResponse, transferFormat)]; + case 10: + _a.sent(); + _a.label = 11; + case 11: + if (this.transport instanceof LongPollingTransport_1.LongPollingTransport) { + this.features.inherentKeepAlive = true; + } + this.transport.onreceive = this.onreceive; + this.transport.onclose = function (e) { return _this.stopConnection(e); }; + // only change the state if we were connecting to not overwrite + // the state if the connection is already marked as Disconnected + this.changeState(0 /* Connecting */, 1 /* Connected */); + return [3 /*break*/, 13]; + case 12: + e_2 = _a.sent(); + this.logger.log(ILogger.LogLevel.Error, "Failed to start the connection: " + e_2); + this.connectionState = 2 /* Disconnected */; + this.transport = null; + throw e_2; + case 13: return [2 /*return*/]; + } + }); + }); + }; + HttpConnection.prototype.getNegotiationResponse = function (url) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var token, headers, negotiateUrl, response, e_3, _a; + return tslib_1.__generator(this, function (_b) { + switch (_b.label) { + case 0: return [4 /*yield*/, this.accessTokenFactory()]; + case 1: + token = _b.sent(); + if (token) { + headers = (_a = {}, _a["Authorization"] = "Bearer " + token, _a); + } + negotiateUrl = this.resolveNegotiateUrl(url); + this.logger.log(ILogger.LogLevel.Debug, "Sending negotiation request: " + negotiateUrl); + _b.label = 2; + case 2: + _b.trys.push([2, 4, , 5]); + return [4 /*yield*/, this.httpClient.post(negotiateUrl, { + content: "", + headers: headers, + })]; + case 3: + response = _b.sent(); + if (response.statusCode !== 200) { + throw Error("Unexpected status code returned from negotiate " + response.statusCode); + } + return [2 /*return*/, JSON.parse(response.content)]; + case 4: + e_3 = _b.sent(); + this.logger.log(ILogger.LogLevel.Error, "Failed to complete negotiation with the server: " + e_3); + throw e_3; + case 5: return [2 /*return*/]; + } + }); + }); + }; + HttpConnection.prototype.createConnectUrl = function (url, connectionId) { + return url + (url.indexOf("?") === -1 ? "?" : "&") + ("id=" + connectionId); + }; + HttpConnection.prototype.createTransport = function (url, requestedTransport, negotiateResponse, requestedTransferFormat) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + var connectUrl, transports, _i, transports_1, endpoint, transport, ex_1; + return tslib_1.__generator(this, function (_a) { + switch (_a.label) { + case 0: + connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId); + if (!this.isITransport(requestedTransport)) return [3 /*break*/, 2]; + this.logger.log(ILogger.LogLevel.Debug, "Connection was provided an instance of ITransport, using that directly."); + this.transport = requestedTransport; + return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)]; + case 1: + _a.sent(); + // only change the state if we were connecting to not overwrite + // the state if the connection is already marked as Disconnected + this.changeState(0 /* Connecting */, 1 /* Connected */); + return [2 /*return*/]; + case 2: + transports = negotiateResponse.availableTransports; + _i = 0, transports_1 = transports; + _a.label = 3; + case 3: + if (!(_i < transports_1.length)) return [3 /*break*/, 9]; + endpoint = transports_1[_i]; + this.connectionState = 0 /* Connecting */; + transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat); + if (!(typeof transport === "number")) return [3 /*break*/, 8]; + this.transport = this.constructTransport(transport); + if (!(negotiateResponse.connectionId === null)) return [3 /*break*/, 5]; + return [4 /*yield*/, this.getNegotiationResponse(url)]; + case 4: + negotiateResponse = _a.sent(); + connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId); + _a.label = 5; + case 5: + _a.trys.push([5, 7, , 8]); + return [4 /*yield*/, this.transport.connect(connectUrl, requestedTransferFormat)]; + case 6: + _a.sent(); + this.changeState(0 /* Connecting */, 1 /* Connected */); + return [2 /*return*/]; + case 7: + ex_1 = _a.sent(); + this.logger.log(ILogger.LogLevel.Error, "Failed to start the transport '" + ITransport.HttpTransportType[transport] + "': " + ex_1); + this.connectionState = 2 /* Disconnected */; + negotiateResponse.connectionId = null; + return [3 /*break*/, 8]; + case 8: + _i++; + return [3 /*break*/, 3]; + case 9: throw new Error("Unable to initialize any of the available transports."); + } + }); + }); + }; + HttpConnection.prototype.constructTransport = function (transport) { + switch (transport) { + case ITransport.HttpTransportType.WebSockets: + return new WebSocketTransport_1.WebSocketTransport(this.accessTokenFactory, this.logger, this.options.logMessageContent); + case ITransport.HttpTransportType.ServerSentEvents: + return new ServerSentEventsTransport_1.ServerSentEventsTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent); + case ITransport.HttpTransportType.LongPolling: + return new LongPollingTransport_1.LongPollingTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent); + default: + throw new Error("Unknown transport: " + transport + "."); + } + }; + HttpConnection.prototype.resolveTransport = function (endpoint, requestedTransport, requestedTransferFormat) { + var transport = ITransport.HttpTransportType[endpoint.transport]; + if (transport === null || transport === undefined) { + this.logger.log(ILogger.LogLevel.Debug, "Skipping transport '" + endpoint.transport + "' because it is not supported by this client."); + } + else { + var transferFormats = endpoint.transferFormats.map(function (s) { return ITransport.TransferFormat[s]; }); + if (transportMatches(requestedTransport, transport)) { + if (transferFormats.indexOf(requestedTransferFormat) >= 0) { + if ((transport === ITransport.HttpTransportType.WebSockets && typeof WebSocket === "undefined") || + (transport === ITransport.HttpTransportType.ServerSentEvents && typeof EventSource === "undefined")) { + this.logger.log(ILogger.LogLevel.Debug, "Skipping transport '" + ITransport.HttpTransportType[transport] + "' because it is not supported in your environment.'"); + } + else { + this.logger.log(ILogger.LogLevel.Debug, "Selecting transport '" + ITransport.HttpTransportType[transport] + "'"); + return transport; + } + } + else { + this.logger.log(ILogger.LogLevel.Debug, "Skipping transport '" + ITransport.HttpTransportType[transport] + "' because it does not support the requested transfer format '" + ITransport.TransferFormat[requestedTransferFormat] + "'."); + } + } + else { + this.logger.log(ILogger.LogLevel.Debug, "Skipping transport '" + ITransport.HttpTransportType[transport] + "' because it was disabled by the client."); + } + } + return null; + }; + HttpConnection.prototype.isITransport = function (transport) { + return transport && typeof (transport) === "object" && "connect" in transport; + }; + HttpConnection.prototype.changeState = function (from, to) { + if (this.connectionState === from) { + this.connectionState = to; + return true; + } + return false; + }; + HttpConnection.prototype.stopConnection = function (error) { + return tslib_1.__awaiter(this, void 0, void 0, function () { + return tslib_1.__generator(this, function (_a) { + this.transport = null; + // If we have a stopError, it takes precedence over the error from the transport + error = this.stopError || error; + if (error) { + this.logger.log(ILogger.LogLevel.Error, "Connection disconnected with error '" + error + "'."); + } + else { + this.logger.log(ILogger.LogLevel.Information, "Connection disconnected."); + } + this.connectionState = 2 /* Disconnected */; + if (this.onclose) { + this.onclose(error); + } + return [2 /*return*/]; + }); + }); + }; + HttpConnection.prototype.resolveUrl = function (url) { + // startsWith is not supported in IE + if (url.lastIndexOf("https://", 0) === 0 || url.lastIndexOf("http://", 0) === 0) { + return url; + } + if (typeof window === "undefined" || !window || !window.document) { + throw new Error("Cannot resolve '" + url + "'."); + } + // Setting the url to the href propery of an anchor tag handles normalization + // for us. There are 3 main cases. + // 1. Relative path normalization e.g "b" -> "http://localhost:5000/a/b" + // 2. Absolute path normalization e.g "/a/b" -> "http://localhost:5000/a/b" + // 3. Networkpath reference normalization e.g "//localhost:5000/a/b" -> "http://localhost:5000/a/b" + var aTag = window.document.createElement("a"); + aTag.href = url; + this.logger.log(ILogger.LogLevel.Information, "Normalizing '" + url + "' to '" + aTag.href + "'."); + return aTag.href; + }; + HttpConnection.prototype.resolveNegotiateUrl = function (url) { + var index = url.indexOf("?"); + var negotiateUrl = url.substring(0, index === -1 ? url.length : index); + if (negotiateUrl[negotiateUrl.length - 1] !== "/") { + negotiateUrl += "/"; + } + negotiateUrl += "negotiate"; + negotiateUrl += index === -1 ? "" : url.substring(index); + return negotiateUrl; + }; + return HttpConnection; +}()); +exports.HttpConnection = HttpConnection; +function transportMatches(requestedTransport, actualTransport) { + return !requestedTransport || ((actualTransport & requestedTransport) !== 0); +} +}); +unwrapExports(HttpConnection_1); +var HttpConnection_2 = HttpConnection_1.HttpConnection; -var DEFAULT_TIMEOUT_IN_MS = 30 * 1000; -var HubConnection = /** @class */ (function () { - function HubConnection(urlOrConnection, options) { - if (options === void 0) { options = {}; } - var _this = this; - options = options || {}; - this.timeoutInMilliseconds = options.timeoutInMilliseconds || DEFAULT_TIMEOUT_IN_MS; - if (typeof urlOrConnection === "string") { - this.connection = new HttpConnection_1.HttpConnection(urlOrConnection, options); - } - else { - this.connection = urlOrConnection; - } - this.logger = Loggers.LoggerFactory.createLogger(options.logger); - this.protocol = options.protocol || new JsonHubProtocol_1.JsonHubProtocol(); - this.connection.onreceive = function (data) { return _this.processIncomingData(data); }; - this.connection.onclose = function (error) { return _this.connectionClosed(error); }; - this.callbacks = new Map(); - this.methods = new Map(); - this.closedCallbacks = []; - this.id = 0; - } - HubConnection.prototype.processIncomingData = function (data) { - if (this.timeoutHandle !== undefined) { - clearTimeout(this.timeoutHandle); - } - // Parse the messages - var messages = this.protocol.parseMessages(data); - for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) { - var message = messages_1[_i]; - switch (message.type) { - case 1 /* Invocation */: - this.invokeClientMethod(message); - break; - case 2 /* StreamItem */: - case 3 /* Completion */: - var callback = this.callbacks.get(message.invocationId); - if (callback != null) { - if (message.type === 3 /* Completion */) { - this.callbacks.delete(message.invocationId); - } - callback(message); - } - break; - case 6 /* Ping */: - // Don't care about pings - break; - default: - this.logger.log(ILogger.LogLevel.Warning, "Invalid message type: " + data); - break; - } - } - this.configureTimeout(); - }; - HubConnection.prototype.configureTimeout = function () { - var _this = this; - if (!this.connection.features || !this.connection.features.inherentKeepAlive) { - // Set the timeout timer - this.timeoutHandle = setTimeout(function () { return _this.serverTimeout(); }, this.timeoutInMilliseconds); - } - }; - HubConnection.prototype.serverTimeout = function () { - // The server hasn't talked to us in a while. It doesn't like us anymore ... :( - // Terminate the connection - this.connection.stop(new Error("Server timeout elapsed without receiving a message from the server.")); - }; - HubConnection.prototype.invokeClientMethod = function (invocationMessage) { - var _this = this; - var methods = this.methods.get(invocationMessage.target.toLowerCase()); - if (methods) { - methods.forEach(function (m) { return m.apply(_this, invocationMessage.arguments); }); - if (invocationMessage.invocationId) { - // This is not supported in v1. So we return an error to avoid blocking the server waiting for the response. - var message = "Server requested a response, which is not supported in this version of the client."; - this.logger.log(ILogger.LogLevel.Error, message); - this.connection.stop(new Error(message)); - } - } - else { - this.logger.log(ILogger.LogLevel.Warning, "No client method with the name '" + invocationMessage.target + "' found."); - } - }; - HubConnection.prototype.connectionClosed = function (error) { - var _this = this; - this.callbacks.forEach(function (callback) { - callback(undefined, error ? error : new Error("Invocation canceled due to connection being closed.")); - }); - this.callbacks.clear(); - this.closedCallbacks.forEach(function (c) { return c.apply(_this, [error]); }); - this.cleanupTimeout(); - }; - HubConnection.prototype.start = function () { - return __awaiter(this, void 0, void 0, function () { - var requestedTransferMode, actualTransferMode; - return __generator(this, function (_a) { - switch (_a.label) { - case 0: - requestedTransferMode = (this.protocol.type === 2 /* Binary */) - ? 2 /* Binary */ - : 1 /* Text */; - this.connection.features.transferMode = requestedTransferMode; - return [4 /*yield*/, this.connection.start()]; - case 1: - _a.sent(); - actualTransferMode = this.connection.features.transferMode; - return [4 /*yield*/, this.connection.send(TextMessageFormat_1.TextMessageFormat.write(JSON.stringify({ protocol: this.protocol.name })))]; - case 2: - _a.sent(); - this.logger.log(ILogger.LogLevel.Information, "Using HubProtocol '" + this.protocol.name + "'."); - if (requestedTransferMode === 2 /* Binary */ && actualTransferMode === 1 /* Text */) { - this.protocol = new Base64EncodedHubProtocol_1.Base64EncodedHubProtocol(this.protocol); - } - this.configureTimeout(); - return [2 /*return*/]; - } - }); - }); - }; - HubConnection.prototype.stop = function () { - this.cleanupTimeout(); - return this.connection.stop(); - }; - HubConnection.prototype.stream = function (methodName) { - var _this = this; - var args = []; - for (var _i = 1; _i < arguments.length; _i++) { - args[_i - 1] = arguments[_i]; - } - var invocationDescriptor = this.createStreamInvocation(methodName, args); - var subject = new Observable.Subject(function () { - var cancelInvocation = _this.createCancelInvocation(invocationDescriptor.invocationId); - var cancelMessage = _this.protocol.writeMessage(cancelInvocation); - _this.callbacks.delete(invocationDescriptor.invocationId); - return _this.connection.send(cancelMessage); - }); - this.callbacks.set(invocationDescriptor.invocationId, function (invocationEvent, error) { - if (error) { - subject.error(error); - return; - } - if (invocationEvent.type === 3 /* Completion */) { - if (invocationEvent.error) { - subject.error(new Error(invocationEvent.error)); - } - else { - subject.complete(); - } - } - else { - subject.next((invocationEvent.item)); - } - }); - var message = this.protocol.writeMessage(invocationDescriptor); - this.connection.send(message) - .catch(function (e) { - subject.error(e); - _this.callbacks.delete(invocationDescriptor.invocationId); - }); - return subject; - }; - HubConnection.prototype.send = function (methodName) { - var args = []; - for (var _i = 1; _i < arguments.length; _i++) { - args[_i - 1] = arguments[_i]; - } - var invocationDescriptor = this.createInvocation(methodName, args, true); - var message = this.protocol.writeMessage(invocationDescriptor); - return this.connection.send(message); - }; - HubConnection.prototype.invoke = function (methodName) { - var _this = this; - var args = []; - for (var _i = 1; _i < arguments.length; _i++) { - args[_i - 1] = arguments[_i]; - } - var invocationDescriptor = this.createInvocation(methodName, args, false); - var p = new Promise(function (resolve, reject) { - _this.callbacks.set(invocationDescriptor.invocationId, function (invocationEvent, error) { - if (error) { - reject(error); - return; - } - if (invocationEvent.type === 3 /* Completion */) { - var completionMessage = invocationEvent; - if (completionMessage.error) { - reject(new Error(completionMessage.error)); - } - else { - resolve(completionMessage.result); - } - } - else { - reject(new Error("Unexpected message type: " + invocationEvent.type)); - } - }); - var message = _this.protocol.writeMessage(invocationDescriptor); - _this.connection.send(message) - .catch(function (e) { - reject(e); - _this.callbacks.delete(invocationDescriptor.invocationId); - }); - }); - return p; - }; - HubConnection.prototype.on = function (methodName, method) { - if (!methodName || !method) { - return; - } - methodName = methodName.toLowerCase(); - if (!this.methods.has(methodName)) { - this.methods.set(methodName, []); - } - this.methods.get(methodName).push(method); - }; - HubConnection.prototype.off = function (methodName, method) { - if (!methodName || !method) { - return; - } - methodName = methodName.toLowerCase(); - var handlers = this.methods.get(methodName); - if (!handlers) { - return; - } - var removeIdx = handlers.indexOf(method); - if (removeIdx !== -1) { - handlers.splice(removeIdx, 1); - } - }; - HubConnection.prototype.onclose = function (callback) { - if (callback) { - this.closedCallbacks.push(callback); - } - }; - HubConnection.prototype.cleanupTimeout = function () { - if (this.timeoutHandle) { - clearTimeout(this.timeoutHandle); - } - }; - HubConnection.prototype.createInvocation = function (methodName, args, nonblocking) { - if (nonblocking) { - return { - arguments: args, - target: methodName, - type: 1 /* Invocation */, - }; - } - else { - var id = this.id; - this.id++; - return { - arguments: args, - invocationId: id.toString(), - target: methodName, - type: 1 /* Invocation */, - }; - } - }; - HubConnection.prototype.createStreamInvocation = function (methodName, args) { - var id = this.id; - this.id++; - return { - arguments: args, - invocationId: id.toString(), - target: methodName, - type: 4 /* StreamInvocation */, - }; - }; - HubConnection.prototype.createCancelInvocation = function (id) { - return { - invocationId: id, - type: 5 /* CancelInvocation */, - }; - }; - return HubConnection; -}()); -exports.HubConnection = HubConnection; +var JsonHubProtocol_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + + +var JSON_HUB_PROTOCOL_NAME = "json"; +/** Implements the JSON Hub Protocol. */ +var JsonHubProtocol = /** @class */ (function () { + function JsonHubProtocol() { + /** @inheritDoc */ + this.name = JSON_HUB_PROTOCOL_NAME; + /** @inheritDoc */ + this.version = 1; + /** @inheritDoc */ + this.transferFormat = ITransport.TransferFormat.Text; + } + /** Creates an array of {@link HubMessage} objects from the specified serialized representation. + * + * @param {string} input A string containing the serialized representation. + * @param {ILogger} logger A logger that will be used to log messages that occur during parsing. + */ + JsonHubProtocol.prototype.parseMessages = function (input, logger) { + // The interface does allow "ArrayBuffer" to be passed in, but this implementation does not. So let's throw a useful error. + if (typeof input !== "string") { + throw new Error("Invalid input for JSON hub protocol. Expected a string."); + } + if (!input) { + return []; + } + if (logger === null) { + logger = Loggers.NullLogger.instance; + } + // Parse the messages + var messages = TextMessageFormat_1.TextMessageFormat.parse(input); + var hubMessages = []; + for (var _i = 0, messages_1 = messages; _i < messages_1.length; _i++) { + var message = messages_1[_i]; + var parsedMessage = JSON.parse(message); + if (typeof parsedMessage.type !== "number") { + throw new Error("Invalid payload."); + } + switch (parsedMessage.type) { + case IHubProtocol.MessageType.Invocation: + this.isInvocationMessage(parsedMessage); + break; + case IHubProtocol.MessageType.StreamItem: + this.isStreamItemMessage(parsedMessage); + break; + case IHubProtocol.MessageType.Completion: + this.isCompletionMessage(parsedMessage); + break; + case IHubProtocol.MessageType.Ping: + // Single value, no need to validate + break; + case IHubProtocol.MessageType.Close: + // All optional values, no need to validate + break; + default: + // Future protocol changes can add message types, old clients can ignore them + logger.log(ILogger.LogLevel.Information, "Unknown message type '" + parsedMessage.type + "' ignored."); + continue; + } + hubMessages.push(parsedMessage); + } + return hubMessages; + }; + /** Writes the specified {@link HubMessage} to a string and returns it. + * + * @param {HubMessage} message The message to write. + * @returns {string} A string containing the serialized representation of the message. + */ + JsonHubProtocol.prototype.writeMessage = function (message) { + return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(message)); + }; + JsonHubProtocol.prototype.isInvocationMessage = function (message) { + this.assertNotEmptyString(message.target, "Invalid payload for Invocation message."); + if (message.invocationId !== undefined) { + this.assertNotEmptyString(message.invocationId, "Invalid payload for Invocation message."); + } + }; + JsonHubProtocol.prototype.isStreamItemMessage = function (message) { + this.assertNotEmptyString(message.invocationId, "Invalid payload for StreamItem message."); + if (message.item === undefined) { + throw new Error("Invalid payload for StreamItem message."); + } + }; + JsonHubProtocol.prototype.isCompletionMessage = function (message) { + if (message.result && message.error) { + throw new Error("Invalid payload for Completion message."); + } + if (!message.result && message.error) { + this.assertNotEmptyString(message.error, "Invalid payload for Completion message."); + } + this.assertNotEmptyString(message.invocationId, "Invalid payload for Completion message."); + }; + JsonHubProtocol.prototype.assertNotEmptyString = function (value, errorMessage) { + if (typeof value !== "string" || value === "") { + throw new Error(errorMessage); + } + }; + return JsonHubProtocol; +}()); +exports.JsonHubProtocol = JsonHubProtocol; }); -unwrapExports(HubConnection_1); -var HubConnection_2 = HubConnection_1.JsonHubProtocol; -var HubConnection_3 = HubConnection_1.HubConnection; - -var IHubProtocol = createCommonjsModule(function (module, exports) { -Object.defineProperty(exports, "__esModule", { value: true }); +unwrapExports(JsonHubProtocol_1); +var JsonHubProtocol_2 = JsonHubProtocol_1.JsonHubProtocol; + +var HubConnectionBuilder_1 = createCommonjsModule(function (module, exports) { +Object.defineProperty(exports, "__esModule", { value: true }); + + + + + +/** A builder for configuring {@link HubConnection} instances. */ +var HubConnectionBuilder = /** @class */ (function () { + function HubConnectionBuilder() { + } + HubConnectionBuilder.prototype.configureLogging = function (logging) { + Utils.Arg.isRequired(logging, "logging"); + if (isLogger(logging)) { + this.logger = logging; + } + else { + this.logger = new Utils.ConsoleLogger(logging); + } + return this; + }; + HubConnectionBuilder.prototype.withUrl = function (url, transportTypeOrOptions) { + Utils.Arg.isRequired(url, "url"); + this.url = url; + // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed + // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called. + if (typeof transportTypeOrOptions === "object") { + this.httpConnectionOptions = transportTypeOrOptions; + } + else { + this.httpConnectionOptions = { + transport: transportTypeOrOptions, + }; + } + return this; + }; + /** Configures the {@link HubConnection} to use the specified Hub Protocol. + * + * @param {IHubProtocol} protocol The {@link IHubProtocol} implementation to use. + */ + HubConnectionBuilder.prototype.withHubProtocol = function (protocol) { + Utils.Arg.isRequired(protocol, "protocol"); + this.protocol = protocol; + return this; + }; + /** Creates a {@link HubConnection} from the configuration options specified in this builder. + * + * @returns {HubConnection} The configured {@link HubConnection}. + */ + HubConnectionBuilder.prototype.build = function () { + // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one + // provided to configureLogger + var httpConnectionOptions = this.httpConnectionOptions || {}; + // If it's 'null', the user **explicitly** asked for null, don't mess with it. + if (httpConnectionOptions.logger === undefined) { + // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it. + httpConnectionOptions.logger = this.logger; + } + // Now create the connection + if (!this.url) { + throw new Error("The 'HubConnectionBuilder.withUrl' method must be called before building the connection."); + } + var connection = new HttpConnection_1.HttpConnection(this.url, httpConnectionOptions); + return HubConnection_1.HubConnection.create(connection, this.logger || Loggers.NullLogger.instance, this.protocol || new JsonHubProtocol_1.JsonHubProtocol()); + }; + return HubConnectionBuilder; +}()); +exports.HubConnectionBuilder = HubConnectionBuilder; +function isLogger(logger) { + return logger.log !== undefined; +} }); -unwrapExports(IHubProtocol); +unwrapExports(HubConnectionBuilder_1); +var HubConnectionBuilder_2 = HubConnectionBuilder_1.HubConnectionBuilder; var cjs = createCommonjsModule(function (module, exports) { -function __export(m) { - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; -} -Object.defineProperty(exports, "__esModule", { value: true }); -__export(Errors); -__export(HttpClient_1); -__export(HttpConnection_1); -__export(HubConnection_1); -__export(IHubProtocol); -__export(ILogger); -__export(Loggers); -__export(Transports); -__export(Observable); +Object.defineProperty(exports, "__esModule", { value: true }); +// Version token that will be replaced by the prepack command +/** The version of the SignalR client. */ +exports.VERSION = "0.0.0-DEV_BUILD"; + +exports.HttpError = Errors.HttpError; +exports.TimeoutError = Errors.TimeoutError; + +exports.DefaultHttpClient = HttpClient_1.DefaultHttpClient; +exports.HttpClient = HttpClient_1.HttpClient; +exports.HttpResponse = HttpClient_1.HttpResponse; + +exports.HubConnection = HubConnection_1.HubConnection; + +exports.HubConnectionBuilder = HubConnectionBuilder_1.HubConnectionBuilder; + +exports.MessageType = IHubProtocol.MessageType; + +exports.LogLevel = ILogger.LogLevel; + +exports.HttpTransportType = ITransport.HttpTransportType; +exports.TransferFormat = ITransport.TransferFormat; + +exports.NullLogger = Loggers.NullLogger; + +exports.JsonHubProtocol = JsonHubProtocol_1.JsonHubProtocol; }); unwrapExports(cjs); +var cjs_1 = cjs.VERSION; +var cjs_2 = cjs.HttpError; +var cjs_3 = cjs.TimeoutError; +var cjs_4 = cjs.DefaultHttpClient; +var cjs_5 = cjs.HttpClient; +var cjs_6 = cjs.HttpResponse; +var cjs_7 = cjs.HubConnection; +var cjs_8 = cjs.HubConnectionBuilder; +var cjs_9 = cjs.MessageType; +var cjs_10 = cjs.LogLevel; +var cjs_11 = cjs.HttpTransportType; +var cjs_12 = cjs.TransferFormat; +var cjs_13 = cjs.NullLogger; +var cjs_14 = cjs.JsonHubProtocol; var browserIndex = createCommonjsModule(function (module, exports) { -function __export(m) { - for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; -} -Object.defineProperty(exports, "__esModule", { value: true }); -// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds. - -__export(cjs); +Object.defineProperty(exports, "__esModule", { value: true }); + +// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds. + +// Copy from Array.prototype into Uint8Array to polyfill on IE. It's OK because the implementations of indexOf and slice use properties +// that exist on Uint8Array with the same name, and JavaScript is magic. +// We make them 'writable' because the Buffer polyfill messes with it as well. +if (!Uint8Array.prototype.indexOf) { + Object.defineProperty(Uint8Array.prototype, "indexOf", { + value: Array.prototype.indexOf, + writable: true, + }); +} +if (!Uint8Array.prototype.slice) { + Object.defineProperty(Uint8Array.prototype, "slice", { + value: Array.prototype.slice, + writable: true, + }); +} +if (!Uint8Array.prototype.forEach) { + Object.defineProperty(Uint8Array.prototype, "forEach", { + value: Array.prototype.forEach, + writable: true, + }); +} +tslib_1.__exportStar(cjs, exports); }); diff --git a/samples/ChatSample/wwwroot/scripts/signalr.js.map b/samples/ChatSample/wwwroot/scripts/signalr.js.map new file mode 100644 index 000000000..ca3ee106b --- /dev/null +++ b/samples/ChatSample/wwwroot/scripts/signalr.js.map @@ -0,0 +1 @@ +{"version":3,"file":"signalr.js","sources":["../../../node_modules/tslib/tslib.es6.js","../../node_modules/es6-promise/dist/lib/es6-promise/utils.js","../../node_modules/es6-promise/dist/lib/es6-promise/asap.js","../../node_modules/es6-promise/dist/lib/es6-promise/then.js","../../node_modules/es6-promise/dist/lib/es6-promise/promise/resolve.js","../../node_modules/es6-promise/dist/lib/es6-promise/-internal.js","../../node_modules/es6-promise/dist/lib/es6-promise/enumerator.js","../../node_modules/es6-promise/dist/lib/es6-promise/promise/all.js","../../node_modules/es6-promise/dist/lib/es6-promise/promise/race.js","../../node_modules/es6-promise/dist/lib/es6-promise/promise/reject.js","../../node_modules/es6-promise/dist/lib/es6-promise/promise.js","../../node_modules/es6-promise/dist/lib/es6-promise/polyfill.js","../../node_modules/es6-promise/dist/lib/es6-promise.js","../../node_modules/es6-promise/dist/lib/es6-promise.auto.js","../../src/Errors.ts","../../src/ILogger.ts","../../src/HttpClient.ts","../../src/TextMessageFormat.ts","../../src/HandshakeProtocol.ts","../../src/IHubProtocol.ts","../../src/Loggers.ts","../../src/Utils.ts","../../src/HubConnection.ts","../../src/ITransport.ts","../../src/AbortController.ts","../../src/LongPollingTransport.ts","../../src/ServerSentEventsTransport.ts","../../src/WebSocketTransport.ts","../../src/HttpConnection.ts","../../src/JsonHubProtocol.ts","../../src/HubConnectionBuilder.ts","../../src/index.ts","../../src/browser-index.ts"],"sourcesContent":["/*! *****************************************************************************\r\nCopyright (c) Microsoft Corporation. All rights reserved.\r\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use\r\nthis file except in compliance with the License. You may obtain a copy of the\r\nLicense at http://www.apache.org/licenses/LICENSE-2.0\r\n\r\nTHIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r\nKIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED\r\nWARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,\r\nMERCHANTABLITY OR NON-INFRINGEMENT.\r\n\r\nSee the Apache Version 2.0 License for specific language governing permissions\r\nand limitations under the License.\r\n***************************************************************************** */\r\n/* global Reflect, Promise */\r\n\r\nvar extendStatics = Object.setPrototypeOf ||\r\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\r\n function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\r\n\r\nexport function __extends(d, b) {\r\n extendStatics(d, b);\r\n function __() { this.constructor = d; }\r\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\r\n}\r\n\r\nexport var __assign = Object.assign || function __assign(t) {\r\n for (var s, i = 1, n = arguments.length; i < n; i++) {\r\n s = arguments[i];\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];\r\n }\r\n return t;\r\n}\r\n\r\nexport function __rest(s, e) {\r\n var t = {};\r\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)\r\n t[p] = s[p];\r\n if (s != null && typeof Object.getOwnPropertySymbols === \"function\")\r\n for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)\r\n t[p[i]] = s[p[i]];\r\n return t;\r\n}\r\n\r\nexport function __decorate(decorators, target, key, desc) {\r\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\r\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\r\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport function __exportStar(m, exports) {\r\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\r\n}\r\n\r\nexport function __values(o) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator], i = 0;\r\n if (m) return m.call(o);\r\n return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; }; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator];\r\n return m ? m.call(o) : typeof __values === \"function\" ? __values(o) : o[Symbol.iterator]();\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];\r\n result.default = mod;\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n","export function objectOrFunction(x) {\n var type = typeof x;\n return x !== null && (type === 'object' || type === 'function');\n}\n\nexport function isFunction(x) {\n return typeof x === 'function';\n}\n\nexport function isMaybeThenable(x) {\n return x !== null && typeof x === 'object';\n}\n\nvar _isArray = void 0;\nif (Array.isArray) {\n _isArray = Array.isArray;\n} else {\n _isArray = function (x) {\n return Object.prototype.toString.call(x) === '[object Array]';\n };\n}\n\nexport var isArray = _isArray;","var len = 0;\nvar vertxNext = void 0;\nvar customSchedulerFn = void 0;\n\nexport var asap = function asap(callback, arg) {\n queue[len] = callback;\n queue[len + 1] = arg;\n len += 2;\n if (len === 2) {\n // If len is 2, that means that we need to schedule an async flush.\n // If additional callbacks are queued before the queue is flushed, they\n // will be processed by this flush that we are scheduling.\n if (customSchedulerFn) {\n customSchedulerFn(flush);\n } else {\n scheduleFlush();\n }\n }\n};\n\nexport function setScheduler(scheduleFn) {\n customSchedulerFn = scheduleFn;\n}\n\nexport function setAsap(asapFn) {\n asap = asapFn;\n}\n\nvar browserWindow = typeof window !== 'undefined' ? window : undefined;\nvar browserGlobal = browserWindow || {};\nvar BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;\nvar isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';\n\n// test for web worker but not in IE10\nvar isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined';\n\n// node\nfunction useNextTick() {\n // node version 0.10.x displays a deprecation warning when nextTick is used recursively\n // see https://github.com/cujojs/when/issues/410 for details\n return function () {\n return process.nextTick(flush);\n };\n}\n\n// vertx\nfunction useVertxTimer() {\n if (typeof vertxNext !== 'undefined') {\n return function () {\n vertxNext(flush);\n };\n }\n\n return useSetTimeout();\n}\n\nfunction useMutationObserver() {\n var iterations = 0;\n var observer = new BrowserMutationObserver(flush);\n var node = document.createTextNode('');\n observer.observe(node, { characterData: true });\n\n return function () {\n node.data = iterations = ++iterations % 2;\n };\n}\n\n// web worker\nfunction useMessageChannel() {\n var channel = new MessageChannel();\n channel.port1.onmessage = flush;\n return function () {\n return channel.port2.postMessage(0);\n };\n}\n\nfunction useSetTimeout() {\n // Store setTimeout reference so es6-promise will be unaffected by\n // other code modifying setTimeout (like sinon.useFakeTimers())\n var globalSetTimeout = setTimeout;\n return function () {\n return globalSetTimeout(flush, 1);\n };\n}\n\nvar queue = new Array(1000);\nfunction flush() {\n for (var i = 0; i < len; i += 2) {\n var callback = queue[i];\n var arg = queue[i + 1];\n\n callback(arg);\n\n queue[i] = undefined;\n queue[i + 1] = undefined;\n }\n\n len = 0;\n}\n\nfunction attemptVertx() {\n try {\n var r = require;\n var vertx = r('vertx');\n vertxNext = vertx.runOnLoop || vertx.runOnContext;\n return useVertxTimer();\n } catch (e) {\n return useSetTimeout();\n }\n}\n\nvar scheduleFlush = void 0;\n// Decide what async method to use to triggering processing of queued callbacks:\nif (isNode) {\n scheduleFlush = useNextTick();\n} else if (BrowserMutationObserver) {\n scheduleFlush = useMutationObserver();\n} else if (isWorker) {\n scheduleFlush = useMessageChannel();\n} else if (browserWindow === undefined && typeof require === 'function') {\n scheduleFlush = attemptVertx();\n} else {\n scheduleFlush = useSetTimeout();\n}","import { invokeCallback, subscribe, FULFILLED, REJECTED, noop, makePromise, PROMISE_ID } from './-internal';\n\nimport { asap } from './asap';\n\nexport default function then(onFulfillment, onRejection) {\n var parent = this;\n\n var child = new this.constructor(noop);\n\n if (child[PROMISE_ID] === undefined) {\n makePromise(child);\n }\n\n var _state = parent._state;\n\n\n if (_state) {\n var callback = arguments[_state - 1];\n asap(function () {\n return invokeCallback(_state, child, callback, parent._result);\n });\n } else {\n subscribe(parent, child, onFulfillment, onRejection);\n }\n\n return child;\n}","import { noop, resolve as _resolve } from '../-internal';\n\n/**\n `Promise.resolve` returns a promise that will become resolved with the\n passed `value`. It is shorthand for the following:\n\n ```javascript\n let promise = new Promise(function(resolve, reject){\n resolve(1);\n });\n\n promise.then(function(value){\n // value === 1\n });\n ```\n\n Instead of writing the above, your code now simply becomes the following:\n\n ```javascript\n let promise = Promise.resolve(1);\n\n promise.then(function(value){\n // value === 1\n });\n ```\n\n @method resolve\n @static\n @param {Any} value value that the returned promise will be resolved with\n Useful for tooling.\n @return {Promise} a promise that will become fulfilled with the given\n `value`\n*/\nexport default function resolve(object) {\n /*jshint validthis:true */\n var Constructor = this;\n\n if (object && typeof object === 'object' && object.constructor === Constructor) {\n return object;\n }\n\n var promise = new Constructor(noop);\n _resolve(promise, object);\n return promise;\n}","import { objectOrFunction, isFunction } from './utils';\n\nimport { asap } from './asap';\n\nimport originalThen from './then';\nimport originalResolve from './promise/resolve';\n\nexport var PROMISE_ID = Math.random().toString(36).substring(16);\n\nfunction noop() {}\n\nvar PENDING = void 0;\nvar FULFILLED = 1;\nvar REJECTED = 2;\n\nvar GET_THEN_ERROR = new ErrorObject();\n\nfunction selfFulfillment() {\n return new TypeError(\"You cannot resolve a promise with itself\");\n}\n\nfunction cannotReturnOwn() {\n return new TypeError('A promises callback cannot return that same promise.');\n}\n\nfunction getThen(promise) {\n try {\n return promise.then;\n } catch (error) {\n GET_THEN_ERROR.error = error;\n return GET_THEN_ERROR;\n }\n}\n\nfunction tryThen(then, value, fulfillmentHandler, rejectionHandler) {\n try {\n then.call(value, fulfillmentHandler, rejectionHandler);\n } catch (e) {\n return e;\n }\n}\n\nfunction handleForeignThenable(promise, thenable, then) {\n asap(function (promise) {\n var sealed = false;\n var error = tryThen(then, thenable, function (value) {\n if (sealed) {\n return;\n }\n sealed = true;\n if (thenable !== value) {\n resolve(promise, value);\n } else {\n fulfill(promise, value);\n }\n }, function (reason) {\n if (sealed) {\n return;\n }\n sealed = true;\n\n reject(promise, reason);\n }, 'Settle: ' + (promise._label || ' unknown promise'));\n\n if (!sealed && error) {\n sealed = true;\n reject(promise, error);\n }\n }, promise);\n}\n\nfunction handleOwnThenable(promise, thenable) {\n if (thenable._state === FULFILLED) {\n fulfill(promise, thenable._result);\n } else if (thenable._state === REJECTED) {\n reject(promise, thenable._result);\n } else {\n subscribe(thenable, undefined, function (value) {\n return resolve(promise, value);\n }, function (reason) {\n return reject(promise, reason);\n });\n }\n}\n\nfunction handleMaybeThenable(promise, maybeThenable, then) {\n if (maybeThenable.constructor === promise.constructor && then === originalThen && maybeThenable.constructor.resolve === originalResolve) {\n handleOwnThenable(promise, maybeThenable);\n } else {\n if (then === GET_THEN_ERROR) {\n reject(promise, GET_THEN_ERROR.error);\n GET_THEN_ERROR.error = null;\n } else if (then === undefined) {\n fulfill(promise, maybeThenable);\n } else if (isFunction(then)) {\n handleForeignThenable(promise, maybeThenable, then);\n } else {\n fulfill(promise, maybeThenable);\n }\n }\n}\n\nfunction resolve(promise, value) {\n if (promise === value) {\n reject(promise, selfFulfillment());\n } else if (objectOrFunction(value)) {\n handleMaybeThenable(promise, value, getThen(value));\n } else {\n fulfill(promise, value);\n }\n}\n\nfunction publishRejection(promise) {\n if (promise._onerror) {\n promise._onerror(promise._result);\n }\n\n publish(promise);\n}\n\nfunction fulfill(promise, value) {\n if (promise._state !== PENDING) {\n return;\n }\n\n promise._result = value;\n promise._state = FULFILLED;\n\n if (promise._subscribers.length !== 0) {\n asap(publish, promise);\n }\n}\n\nfunction reject(promise, reason) {\n if (promise._state !== PENDING) {\n return;\n }\n promise._state = REJECTED;\n promise._result = reason;\n\n asap(publishRejection, promise);\n}\n\nfunction subscribe(parent, child, onFulfillment, onRejection) {\n var _subscribers = parent._subscribers;\n var length = _subscribers.length;\n\n\n parent._onerror = null;\n\n _subscribers[length] = child;\n _subscribers[length + FULFILLED] = onFulfillment;\n _subscribers[length + REJECTED] = onRejection;\n\n if (length === 0 && parent._state) {\n asap(publish, parent);\n }\n}\n\nfunction publish(promise) {\n var subscribers = promise._subscribers;\n var settled = promise._state;\n\n if (subscribers.length === 0) {\n return;\n }\n\n var child = void 0,\n callback = void 0,\n detail = promise._result;\n\n for (var i = 0; i < subscribers.length; i += 3) {\n child = subscribers[i];\n callback = subscribers[i + settled];\n\n if (child) {\n invokeCallback(settled, child, callback, detail);\n } else {\n callback(detail);\n }\n }\n\n promise._subscribers.length = 0;\n}\n\nfunction ErrorObject() {\n this.error = null;\n}\n\nvar TRY_CATCH_ERROR = new ErrorObject();\n\nfunction tryCatch(callback, detail) {\n try {\n return callback(detail);\n } catch (e) {\n TRY_CATCH_ERROR.error = e;\n return TRY_CATCH_ERROR;\n }\n}\n\nfunction invokeCallback(settled, promise, callback, detail) {\n var hasCallback = isFunction(callback),\n value = void 0,\n error = void 0,\n succeeded = void 0,\n failed = void 0;\n\n if (hasCallback) {\n value = tryCatch(callback, detail);\n\n if (value === TRY_CATCH_ERROR) {\n failed = true;\n error = value.error;\n value.error = null;\n } else {\n succeeded = true;\n }\n\n if (promise === value) {\n reject(promise, cannotReturnOwn());\n return;\n }\n } else {\n value = detail;\n succeeded = true;\n }\n\n if (promise._state !== PENDING) {\n // noop\n } else if (hasCallback && succeeded) {\n resolve(promise, value);\n } else if (failed) {\n reject(promise, error);\n } else if (settled === FULFILLED) {\n fulfill(promise, value);\n } else if (settled === REJECTED) {\n reject(promise, value);\n }\n}\n\nfunction initializePromise(promise, resolver) {\n try {\n resolver(function resolvePromise(value) {\n resolve(promise, value);\n }, function rejectPromise(reason) {\n reject(promise, reason);\n });\n } catch (e) {\n reject(promise, e);\n }\n}\n\nvar id = 0;\nfunction nextId() {\n return id++;\n}\n\nfunction makePromise(promise) {\n promise[PROMISE_ID] = id++;\n promise._state = undefined;\n promise._result = undefined;\n promise._subscribers = [];\n}\n\nexport { nextId, makePromise, getThen, noop, resolve, reject, fulfill, subscribe, publish, publishRejection, initializePromise, invokeCallback, FULFILLED, REJECTED, PENDING, handleMaybeThenable };","function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nimport { isArray, isMaybeThenable } from './utils';\n\nimport { noop, reject, fulfill, subscribe, FULFILLED, REJECTED, PENDING, getThen, handleMaybeThenable } from './-internal';\n\nimport then from './then';\nimport Promise from './promise';\nimport originalResolve from './promise/resolve';\nimport originalThen from './then';\nimport { makePromise, PROMISE_ID } from './-internal';\n\nfunction validationError() {\n return new Error('Array Methods must be provided an Array');\n}\n\nfunction validationError() {\n return new Error('Array Methods must be provided an Array');\n};\n\nvar Enumerator = function () {\n function Enumerator(Constructor, input) {\n this._instanceConstructor = Constructor;\n this.promise = new Constructor(noop);\n\n if (!this.promise[PROMISE_ID]) {\n makePromise(this.promise);\n }\n\n if (isArray(input)) {\n this.length = input.length;\n this._remaining = input.length;\n\n this._result = new Array(this.length);\n\n if (this.length === 0) {\n fulfill(this.promise, this._result);\n } else {\n this.length = this.length || 0;\n this._enumerate(input);\n if (this._remaining === 0) {\n fulfill(this.promise, this._result);\n }\n }\n } else {\n reject(this.promise, validationError());\n }\n }\n\n Enumerator.prototype._enumerate = function _enumerate(input) {\n for (var i = 0; this._state === PENDING && i < input.length; i++) {\n this._eachEntry(input[i], i);\n }\n };\n\n Enumerator.prototype._eachEntry = function _eachEntry(entry, i) {\n var c = this._instanceConstructor;\n var resolve = c.resolve;\n\n\n if (resolve === originalResolve) {\n var _then = getThen(entry);\n\n if (_then === originalThen && entry._state !== PENDING) {\n this._settledAt(entry._state, i, entry._result);\n } else if (typeof _then !== 'function') {\n this._remaining--;\n this._result[i] = entry;\n } else if (c === Promise) {\n var promise = new c(noop);\n handleMaybeThenable(promise, entry, _then);\n this._willSettleAt(promise, i);\n } else {\n this._willSettleAt(new c(function (resolve) {\n return resolve(entry);\n }), i);\n }\n } else {\n this._willSettleAt(resolve(entry), i);\n }\n };\n\n Enumerator.prototype._settledAt = function _settledAt(state, i, value) {\n var promise = this.promise;\n\n\n if (promise._state === PENDING) {\n this._remaining--;\n\n if (state === REJECTED) {\n reject(promise, value);\n } else {\n this._result[i] = value;\n }\n }\n\n if (this._remaining === 0) {\n fulfill(promise, this._result);\n }\n };\n\n Enumerator.prototype._willSettleAt = function _willSettleAt(promise, i) {\n var enumerator = this;\n\n subscribe(promise, undefined, function (value) {\n return enumerator._settledAt(FULFILLED, i, value);\n }, function (reason) {\n return enumerator._settledAt(REJECTED, i, reason);\n });\n };\n\n return Enumerator;\n}();\n\nexport default Enumerator;","import Enumerator from '../enumerator';\n\n/**\n `Promise.all` accepts an array of promises, and returns a new promise which\n is fulfilled with an array of fulfillment values for the passed promises, or\n rejected with the reason of the first passed promise to be rejected. It casts all\n elements of the passed iterable to promises as it runs this algorithm.\n\n Example:\n\n ```javascript\n let promise1 = resolve(1);\n let promise2 = resolve(2);\n let promise3 = resolve(3);\n let promises = [ promise1, promise2, promise3 ];\n\n Promise.all(promises).then(function(array){\n // The array here would be [ 1, 2, 3 ];\n });\n ```\n\n If any of the `promises` given to `all` are rejected, the first promise\n that is rejected will be given as an argument to the returned promises's\n rejection handler. For example:\n\n Example:\n\n ```javascript\n let promise1 = resolve(1);\n let promise2 = reject(new Error(\"2\"));\n let promise3 = reject(new Error(\"3\"));\n let promises = [ promise1, promise2, promise3 ];\n\n Promise.all(promises).then(function(array){\n // Code here never runs because there are rejected promises!\n }, function(error) {\n // error.message === \"2\"\n });\n ```\n\n @method all\n @static\n @param {Array} entries array of promises\n @param {String} label optional string for labeling the promise.\n Useful for tooling.\n @return {Promise} promise that is fulfilled when all `promises` have been\n fulfilled, or rejected if any of them become rejected.\n @static\n*/\nexport default function all(entries) {\n return new Enumerator(this, entries).promise;\n}","import { isArray } from \"../utils\";\n\n/**\n `Promise.race` returns a new promise which is settled in the same way as the\n first passed promise to settle.\n\n Example:\n\n ```javascript\n let promise1 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 1');\n }, 200);\n });\n\n let promise2 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 2');\n }, 100);\n });\n\n Promise.race([promise1, promise2]).then(function(result){\n // result === 'promise 2' because it was resolved before promise1\n // was resolved.\n });\n ```\n\n `Promise.race` is deterministic in that only the state of the first\n settled promise matters. For example, even if other promises given to the\n `promises` array argument are resolved, but the first settled promise has\n become rejected before the other promises became fulfilled, the returned\n promise will become rejected:\n\n ```javascript\n let promise1 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 1');\n }, 200);\n });\n\n let promise2 = new Promise(function(resolve, reject){\n setTimeout(function(){\n reject(new Error('promise 2'));\n }, 100);\n });\n\n Promise.race([promise1, promise2]).then(function(result){\n // Code here never runs\n }, function(reason){\n // reason.message === 'promise 2' because promise 2 became rejected before\n // promise 1 became fulfilled\n });\n ```\n\n An example real-world use case is implementing timeouts:\n\n ```javascript\n Promise.race([ajax('foo.json'), timeout(5000)])\n ```\n\n @method race\n @static\n @param {Array} promises array of promises to observe\n Useful for tooling.\n @return {Promise} a promise which settles in the same way as the first passed\n promise to settle.\n*/\nexport default function race(entries) {\n /*jshint validthis:true */\n var Constructor = this;\n\n if (!isArray(entries)) {\n return new Constructor(function (_, reject) {\n return reject(new TypeError('You must pass an array to race.'));\n });\n } else {\n return new Constructor(function (resolve, reject) {\n var length = entries.length;\n for (var i = 0; i < length; i++) {\n Constructor.resolve(entries[i]).then(resolve, reject);\n }\n });\n }\n}","import { noop, reject as _reject } from '../-internal';\n\n/**\n `Promise.reject` returns a promise rejected with the passed `reason`.\n It is shorthand for the following:\n\n ```javascript\n let promise = new Promise(function(resolve, reject){\n reject(new Error('WHOOPS'));\n });\n\n promise.then(function(value){\n // Code here doesn't run because the promise is rejected!\n }, function(reason){\n // reason.message === 'WHOOPS'\n });\n ```\n\n Instead of writing the above, your code now simply becomes the following:\n\n ```javascript\n let promise = Promise.reject(new Error('WHOOPS'));\n\n promise.then(function(value){\n // Code here doesn't run because the promise is rejected!\n }, function(reason){\n // reason.message === 'WHOOPS'\n });\n ```\n\n @method reject\n @static\n @param {Any} reason value that the returned promise will be rejected with.\n Useful for tooling.\n @return {Promise} a promise rejected with the given `reason`.\n*/\nexport default function reject(reason) {\n /*jshint validthis:true */\n var Constructor = this;\n var promise = new Constructor(noop);\n _reject(promise, reason);\n return promise;\n}","function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nimport { isFunction } from './utils';\n\nimport { noop, nextId, PROMISE_ID, initializePromise } from './-internal';\n\nimport { asap, setAsap, setScheduler } from './asap';\n\nimport all from './promise/all';\nimport race from './promise/race';\nimport Resolve from './promise/resolve';\nimport Reject from './promise/reject';\nimport then from './then';\n\nfunction needsResolver() {\n throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');\n}\n\nfunction needsNew() {\n throw new TypeError(\"Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.\");\n}\n\n/**\n Promise objects represent the eventual result of an asynchronous operation. The\n primary way of interacting with a promise is through its `then` method, which\n registers callbacks to receive either a promise's eventual value or the reason\n why the promise cannot be fulfilled.\n\n Terminology\n -----------\n\n - `promise` is an object or function with a `then` method whose behavior conforms to this specification.\n - `thenable` is an object or function that defines a `then` method.\n - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).\n - `exception` is a value that is thrown using the throw statement.\n - `reason` is a value that indicates why a promise was rejected.\n - `settled` the final resting state of a promise, fulfilled or rejected.\n\n A promise can be in one of three states: pending, fulfilled, or rejected.\n\n Promises that are fulfilled have a fulfillment value and are in the fulfilled\n state. Promises that are rejected have a rejection reason and are in the\n rejected state. A fulfillment value is never a thenable.\n\n Promises can also be said to *resolve* a value. If this value is also a\n promise, then the original promise's settled state will match the value's\n settled state. So a promise that *resolves* a promise that rejects will\n itself reject, and a promise that *resolves* a promise that fulfills will\n itself fulfill.\n\n\n Basic Usage:\n ------------\n\n ```js\n let promise = new Promise(function(resolve, reject) {\n // on success\n resolve(value);\n\n // on failure\n reject(reason);\n });\n\n promise.then(function(value) {\n // on fulfillment\n }, function(reason) {\n // on rejection\n });\n ```\n\n Advanced Usage:\n ---------------\n\n Promises shine when abstracting away asynchronous interactions such as\n `XMLHttpRequest`s.\n\n ```js\n function getJSON(url) {\n return new Promise(function(resolve, reject){\n let xhr = new XMLHttpRequest();\n\n xhr.open('GET', url);\n xhr.onreadystatechange = handler;\n xhr.responseType = 'json';\n xhr.setRequestHeader('Accept', 'application/json');\n xhr.send();\n\n function handler() {\n if (this.readyState === this.DONE) {\n if (this.status === 200) {\n resolve(this.response);\n } else {\n reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));\n }\n }\n };\n });\n }\n\n getJSON('/posts.json').then(function(json) {\n // on fulfillment\n }, function(reason) {\n // on rejection\n });\n ```\n\n Unlike callbacks, promises are great composable primitives.\n\n ```js\n Promise.all([\n getJSON('/posts'),\n getJSON('/comments')\n ]).then(function(values){\n values[0] // => postsJSON\n values[1] // => commentsJSON\n\n return values;\n });\n ```\n\n @class Promise\n @param {Function} resolver\n Useful for tooling.\n @constructor\n*/\n\nvar Promise = function () {\n function Promise(resolver) {\n this[PROMISE_ID] = nextId();\n this._result = this._state = undefined;\n this._subscribers = [];\n\n if (noop !== resolver) {\n typeof resolver !== 'function' && needsResolver();\n this instanceof Promise ? initializePromise(this, resolver) : needsNew();\n }\n }\n\n /**\n The primary way of interacting with a promise is through its `then` method,\n which registers callbacks to receive either a promise's eventual value or the\n reason why the promise cannot be fulfilled.\n ```js\n findUser().then(function(user){\n // user is available\n }, function(reason){\n // user is unavailable, and you are given the reason why\n });\n ```\n Chaining\n --------\n The return value of `then` is itself a promise. This second, 'downstream'\n promise is resolved with the return value of the first promise's fulfillment\n or rejection handler, or rejected if the handler throws an exception.\n ```js\n findUser().then(function (user) {\n return user.name;\n }, function (reason) {\n return 'default name';\n }).then(function (userName) {\n // If `findUser` fulfilled, `userName` will be the user's name, otherwise it\n // will be `'default name'`\n });\n findUser().then(function (user) {\n throw new Error('Found user, but still unhappy');\n }, function (reason) {\n throw new Error('`findUser` rejected and we're unhappy');\n }).then(function (value) {\n // never reached\n }, function (reason) {\n // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.\n // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.\n });\n ```\n If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.\n ```js\n findUser().then(function (user) {\n throw new PedagogicalException('Upstream error');\n }).then(function (value) {\n // never reached\n }).then(function (value) {\n // never reached\n }, function (reason) {\n // The `PedgagocialException` is propagated all the way down to here\n });\n ```\n Assimilation\n ------------\n Sometimes the value you want to propagate to a downstream promise can only be\n retrieved asynchronously. This can be achieved by returning a promise in the\n fulfillment or rejection handler. The downstream promise will then be pending\n until the returned promise is settled. This is called *assimilation*.\n ```js\n findUser().then(function (user) {\n return findCommentsByAuthor(user);\n }).then(function (comments) {\n // The user's comments are now available\n });\n ```\n If the assimliated promise rejects, then the downstream promise will also reject.\n ```js\n findUser().then(function (user) {\n return findCommentsByAuthor(user);\n }).then(function (comments) {\n // If `findCommentsByAuthor` fulfills, we'll have the value here\n }, function (reason) {\n // If `findCommentsByAuthor` rejects, we'll have the reason here\n });\n ```\n Simple Example\n --------------\n Synchronous Example\n ```javascript\n let result;\n try {\n result = findResult();\n // success\n } catch(reason) {\n // failure\n }\n ```\n Errback Example\n ```js\n findResult(function(result, err){\n if (err) {\n // failure\n } else {\n // success\n }\n });\n ```\n Promise Example;\n ```javascript\n findResult().then(function(result){\n // success\n }, function(reason){\n // failure\n });\n ```\n Advanced Example\n --------------\n Synchronous Example\n ```javascript\n let author, books;\n try {\n author = findAuthor();\n books = findBooksByAuthor(author);\n // success\n } catch(reason) {\n // failure\n }\n ```\n Errback Example\n ```js\n function foundBooks(books) {\n }\n function failure(reason) {\n }\n findAuthor(function(author, err){\n if (err) {\n failure(err);\n // failure\n } else {\n try {\n findBoooksByAuthor(author, function(books, err) {\n if (err) {\n failure(err);\n } else {\n try {\n foundBooks(books);\n } catch(reason) {\n failure(reason);\n }\n }\n });\n } catch(error) {\n failure(err);\n }\n // success\n }\n });\n ```\n Promise Example;\n ```javascript\n findAuthor().\n then(findBooksByAuthor).\n then(function(books){\n // found books\n }).catch(function(reason){\n // something went wrong\n });\n ```\n @method then\n @param {Function} onFulfilled\n @param {Function} onRejected\n Useful for tooling.\n @return {Promise}\n */\n\n /**\n `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same\n as the catch block of a try/catch statement.\n ```js\n function findAuthor(){\n throw new Error('couldn't find that author');\n }\n // synchronous\n try {\n findAuthor();\n } catch(reason) {\n // something went wrong\n }\n // async with promises\n findAuthor().catch(function(reason){\n // something went wrong\n });\n ```\n @method catch\n @param {Function} onRejection\n Useful for tooling.\n @return {Promise}\n */\n\n\n Promise.prototype.catch = function _catch(onRejection) {\n return this.then(null, onRejection);\n };\n\n /**\n `finally` will be invoked regardless of the promise's fate just as native\n try/catch/finally behaves\n \n Synchronous example:\n \n ```js\n findAuthor() {\n if (Math.random() > 0.5) {\n throw new Error();\n }\n return new Author();\n }\n \n try {\n return findAuthor(); // succeed or fail\n } catch(error) {\n return findOtherAuther();\n } finally {\n // always runs\n // doesn't affect the return value\n }\n ```\n \n Asynchronous example:\n \n ```js\n findAuthor().catch(function(reason){\n return findOtherAuther();\n }).finally(function(){\n // author was either found, or not\n });\n ```\n \n @method finally\n @param {Function} callback\n @return {Promise}\n */\n\n\n Promise.prototype.finally = function _finally(callback) {\n var promise = this;\n var constructor = promise.constructor;\n\n return promise.then(function (value) {\n return constructor.resolve(callback()).then(function () {\n return value;\n });\n }, function (reason) {\n return constructor.resolve(callback()).then(function () {\n throw reason;\n });\n });\n };\n\n return Promise;\n}();\n\nPromise.prototype.then = then;\nexport default Promise;\nPromise.all = all;\nPromise.race = race;\nPromise.resolve = Resolve;\nPromise.reject = Reject;\nPromise._setScheduler = setScheduler;\nPromise._setAsap = setAsap;\nPromise._asap = asap;","/*global self*/\nimport Promise from './promise';\n\nexport default function polyfill() {\n var local = void 0;\n\n if (typeof global !== 'undefined') {\n local = global;\n } else if (typeof self !== 'undefined') {\n local = self;\n } else {\n try {\n local = Function('return this')();\n } catch (e) {\n throw new Error('polyfill failed because global object is unavailable in this environment');\n }\n }\n\n var P = local.Promise;\n\n if (P) {\n var promiseToString = null;\n try {\n promiseToString = Object.prototype.toString.call(P.resolve());\n } catch (e) {\n // silently ignored\n }\n\n if (promiseToString === '[object Promise]' && !P.cast) {\n return;\n }\n }\n\n local.Promise = Promise;\n}","import Promise from './es6-promise/promise';\nimport polyfill from './es6-promise/polyfill';\n\n// Strange compat..\nPromise.polyfill = polyfill;\nPromise.Promise = Promise;\nexport default Promise;","import Promise from './es6-promise';\nPromise.polyfill();\nexport default Promise;","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n/** Error thrown when an HTTP request fails. */\r\nexport class HttpError extends Error {\r\n // tslint:disable-next-line:variable-name\r\n private __proto__: Error;\r\n\r\n /** The HTTP status code represented by this error. */\r\n public statusCode: number;\r\n\r\n /** Constructs a new instance of {@link HttpError}.\r\n *\r\n * @param {string} errorMessage A descriptive error message.\r\n * @param {number} statusCode The HTTP status code represented by this error.\r\n */\r\n constructor(errorMessage: string, statusCode: number) {\r\n const trueProto = new.target.prototype;\r\n super(errorMessage);\r\n this.statusCode = statusCode;\r\n\r\n // Workaround issue in Typescript compiler\r\n // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200\r\n this.__proto__ = trueProto;\r\n }\r\n}\r\n\r\n/** Error thrown when a timeout elapses. */\r\nexport class TimeoutError extends Error {\r\n // tslint:disable-next-line:variable-name\r\n private __proto__: Error;\r\n\r\n /** Constructs a new instance of {@link TimeoutError}.\r\n *\r\n * @param {string} errorMessage A descriptive error message.\r\n */\r\n constructor(errorMessage: string = \"A timeout occurred.\") {\r\n const trueProto = new.target.prototype;\r\n super(errorMessage);\r\n\r\n // Workaround issue in Typescript compiler\r\n // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200\r\n this.__proto__ = trueProto;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// These values are designed to match the ASP.NET Log Levels since that's the pattern we're emulating here.\r\n/** Indicates the severity of a log message.\r\n *\r\n * Log Levels are ordered in increasing severity. So `Debug` is more severe than `Trace`, etc.\r\n */\r\nexport enum LogLevel {\r\n /** Log level for very low severity diagnostic messages. */\r\n Trace = 0,\r\n /** Log level for low severity diagnostic messages. */\r\n Debug = 1,\r\n /** Log level for informational diagnostic messages. */\r\n Information = 2,\r\n /** Log level for diagnostic messages that indicate a non-fatal problem. */\r\n Warning = 3,\r\n /** Log level for diagnostic messages that indicate a failure in the current operation. */\r\n Error = 4,\r\n /** Log level for diagnostic messages that indicate a failure that will terminate the entire application. */\r\n Critical = 5,\r\n /** The highest possible log level. Used when configuring logging to indicate that no log messages should be emitted. */\r\n None = 6,\r\n}\r\n\r\n/** An abstraction that provides a sink for diagnostic messages. */\r\nexport interface ILogger {\r\n /** Called by the framework to emit a diagnostic message.\r\n *\r\n * @param {LogLevel} logLevel The severity level of the message.\r\n * @param {string} message The message.\r\n */\r\n log(logLevel: LogLevel, message: string): void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { AbortSignal } from \"./AbortController\";\r\nimport { HttpError, TimeoutError } from \"./Errors\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\n\r\n/** Represents an HTTP request. */\r\nexport interface HttpRequest {\r\n /** The HTTP method to use for the request. */\r\n method?: string;\r\n\r\n /** The URL for the request. */\r\n url?: string;\r\n\r\n /** The body content for the request. May be a string or an ArrayBuffer (for binary data). */\r\n content?: string | ArrayBuffer;\r\n\r\n /** An object describing headers to apply to the request. */\r\n headers?: { [key: string]: string };\r\n\r\n /** The XMLHttpRequestResponseType to apply to the request. */\r\n responseType?: XMLHttpRequestResponseType;\r\n\r\n /** An AbortSignal that can be monitored for cancellation. */\r\n abortSignal?: AbortSignal;\r\n\r\n /** The time to wait for the request to complete before throwing a TimeoutError. Measured in milliseconds. */\r\n timeout?: number;\r\n}\r\n\r\n/** Represents an HTTP response. */\r\nexport class HttpResponse {\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n */\r\n constructor(statusCode: number);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code and message.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n */\r\n constructor(statusCode: number, statusText: string);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code, message and string content.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n * @param {string} content The content of the response.\r\n */\r\n constructor(statusCode: number, statusText: string, content: string);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code, message and binary content.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n * @param {ArrayBuffer} content The content of the response.\r\n */\r\n constructor(statusCode: number, statusText: string, content: ArrayBuffer);\r\n constructor(\r\n public readonly statusCode: number,\r\n public readonly statusText?: string,\r\n public readonly content?: string | ArrayBuffer) {\r\n }\r\n}\r\n\r\n/** Abstraction over an HTTP client.\r\n *\r\n * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms.\r\n */\r\nexport abstract class HttpClient {\r\n /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public get(url: string): Promise;\r\n\r\n /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public get(url: string, options: HttpRequest): Promise;\r\n public get(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"GET\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public post(url: string): Promise;\r\n\r\n /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public post(url: string, options: HttpRequest): Promise;\r\n public post(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"POST\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public delete(url: string): Promise;\r\n\r\n /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public delete(url: string, options: HttpRequest): Promise;\r\n public delete(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"DELETE\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP request to the specified URL, returning a {@link Promise} that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {HttpRequest} request An {@link HttpRequest} describing the request to send.\r\n * @returns {Promise} A Promise that resolves with an HttpResponse describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public abstract send(request: HttpRequest): Promise;\r\n}\r\n\r\n/** Default implementation of {@link HttpClient}. */\r\nexport class DefaultHttpClient extends HttpClient {\r\n private readonly logger: ILogger;\r\n\r\n /** Creates a new instance of the {@link DefaultHttpClient}, using the provided {@link ILogger} to log messages. */\r\n public constructor(logger: ILogger) {\r\n super();\r\n this.logger = logger;\r\n }\r\n\r\n /** @inheritDoc */\r\n public send(request: HttpRequest): Promise {\r\n return new Promise((resolve, reject) => {\r\n const xhr = new XMLHttpRequest();\r\n\r\n xhr.open(request.method, request.url, true);\r\n xhr.withCredentials = true;\r\n xhr.setRequestHeader(\"X-Requested-With\", \"XMLHttpRequest\");\r\n\r\n if (request.headers) {\r\n Object.keys(request.headers)\r\n .forEach((header) => xhr.setRequestHeader(header, request.headers[header]));\r\n }\r\n\r\n if (request.responseType) {\r\n xhr.responseType = request.responseType;\r\n }\r\n\r\n if (request.abortSignal) {\r\n request.abortSignal.onabort = () => {\r\n xhr.abort();\r\n };\r\n }\r\n\r\n if (request.timeout) {\r\n xhr.timeout = request.timeout;\r\n }\r\n\r\n xhr.onload = () => {\r\n if (request.abortSignal) {\r\n request.abortSignal.onabort = null;\r\n }\r\n\r\n if (xhr.status >= 200 && xhr.status < 300) {\r\n resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText));\r\n } else {\r\n reject(new HttpError(xhr.statusText, xhr.status));\r\n }\r\n };\r\n\r\n xhr.onerror = () => {\r\n this.logger.log(LogLevel.Warning, `Error from HTTP request. ${xhr.status}: ${xhr.statusText}`);\r\n reject(new HttpError(xhr.statusText, xhr.status));\r\n };\r\n\r\n xhr.ontimeout = () => {\r\n this.logger.log(LogLevel.Warning, `Timeout from HTTP request.`);\r\n reject(new TimeoutError());\r\n };\r\n\r\n xhr.send(request.content || \"\");\r\n });\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Not exported from index\r\nexport class TextMessageFormat {\r\n public static RecordSeparatorCode = 0x1e;\r\n public static RecordSeparator = String.fromCharCode(TextMessageFormat.RecordSeparatorCode);\r\n\r\n public static write(output: string): string {\r\n return `${output}${TextMessageFormat.RecordSeparator}`;\r\n }\r\n\r\n public static parse(input: string): string[] {\r\n if (input[input.length - 1] !== TextMessageFormat.RecordSeparator) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n const messages = input.split(TextMessageFormat.RecordSeparator);\r\n messages.pop();\r\n return messages;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { TextMessageFormat } from \"./TextMessageFormat\";\r\n\r\nexport interface HandshakeRequestMessage {\r\n readonly protocol: string;\r\n readonly version: number;\r\n}\r\n\r\nexport interface HandshakeResponseMessage {\r\n readonly error: string;\r\n}\r\n\r\nexport class HandshakeProtocol {\r\n // Handshake request is always JSON\r\n public writeHandshakeRequest(handshakeRequest: HandshakeRequestMessage): string {\r\n return TextMessageFormat.write(JSON.stringify(handshakeRequest));\r\n }\r\n\r\n public parseHandshakeResponse(data: any): [any, HandshakeResponseMessage] {\r\n let responseMessage: HandshakeResponseMessage;\r\n let messageData: string;\r\n let remainingData: any;\r\n\r\n if (data instanceof ArrayBuffer) {\r\n // Format is binary but still need to read JSON text from handshake response\r\n const binaryData = new Uint8Array(data);\r\n const separatorIndex = binaryData.indexOf(TextMessageFormat.RecordSeparatorCode);\r\n if (separatorIndex === -1) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n // content before separator is handshake response\r\n // optional content after is additional messages\r\n const responseLength = separatorIndex + 1;\r\n messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength));\r\n remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null;\r\n } else {\r\n const textData: string = data;\r\n const separatorIndex = textData.indexOf(TextMessageFormat.RecordSeparator);\r\n if (separatorIndex === -1) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n // content before separator is handshake response\r\n // optional content after is additional messages\r\n const responseLength = separatorIndex + 1;\r\n messageData = textData.substring(0, responseLength);\r\n remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null;\r\n }\r\n\r\n // At this point we should have just the single handshake message\r\n const messages = TextMessageFormat.parse(messageData);\r\n responseMessage = JSON.parse(messages[0]);\r\n\r\n // multiple messages could have arrived with handshake\r\n // return additional data to be parsed as usual, or null if all parsed\r\n return [remainingData, responseMessage];\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger } from \"./ILogger\";\r\nimport { TransferFormat } from \"./ITransport\";\r\n\r\n/** Defines the type of a Hub Message. */\r\nexport enum MessageType {\r\n /** Indicates the message is an Invocation message and implements the {@link InvocationMessage} interface. */\r\n Invocation = 1,\r\n /** Indicates the message is a StreamItem message and implements the {@link StreamItemMessage} interface. */\r\n StreamItem = 2,\r\n /** Indicates the message is a Completion message and implements the {@link CompletionMessage} interface. */\r\n Completion = 3,\r\n /** Indicates the message is a Stream Invocation message and implements the {@link StreamInvocationMessage} interface. */\r\n StreamInvocation = 4,\r\n /** Indicates the message is a Cancel Invocation message and implements the {@link CancelInvocationMessage} interface. */\r\n CancelInvocation = 5,\r\n /** Indicates the message is a Ping message and implements the {@link PingMessage} interface. */\r\n Ping = 6,\r\n /** Indicates the message is a Close message and implements the {@link CloseMessage} interface. */\r\n Close = 7,\r\n}\r\n\r\n/** Defines a dictionary of string keys and string values representing headers attached to a Hub message. */\r\nexport interface MessageHeaders {\r\n /** Gets or sets the header with the specified key. */\r\n [key: string]: string;\r\n}\r\n\r\n/** Union type of all known Hub messages. */\r\nexport type HubMessage =\r\n InvocationMessage |\r\n StreamInvocationMessage |\r\n StreamItemMessage |\r\n CompletionMessage |\r\n CancelInvocationMessage |\r\n PingMessage |\r\n CloseMessage;\r\n\r\n/** Defines properties common to all Hub messages. */\r\nexport interface HubMessageBase {\r\n /** A {@link MessageType} value indicating the type of this message. */\r\n readonly type: MessageType;\r\n}\r\n\r\n/** Defines properties common to all Hub messages relating to a specific invocation. */\r\nexport interface HubInvocationMessage extends HubMessageBase {\r\n /** A {@link MessageHeaders} dictionary containing headers attached to the message. */\r\n readonly headers?: MessageHeaders;\r\n /** The ID of the invocation relating to this message.\r\n *\r\n * This is expected to be present for {@link StreamInvocationMessage} and {@link CompletionMessage}. It may\r\n * be 'undefined' for an {@link InvocationMessage} if the sender does not expect a response.\r\n */\r\n readonly invocationId?: string;\r\n}\r\n\r\n/** A hub message representing a non-streaming invocation. */\r\nexport interface InvocationMessage extends HubInvocationMessage {\r\n readonly type: MessageType.Invocation;\r\n /** The target method name. */\r\n readonly target: string;\r\n /** The target method arguments. */\r\n readonly arguments: any[];\r\n}\r\n\r\n/** A hub message representing a streaming invocation. */\r\nexport interface StreamInvocationMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.StreamInvocation;\r\n\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n /** The target method name. */\r\n readonly target: string;\r\n /** The target method arguments. */\r\n readonly arguments: any[];\r\n}\r\n\r\n/** A hub message representing a single item produced as part of a result stream. */\r\nexport interface StreamItemMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.StreamItem;\r\n\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n\r\n /** The item produced by the server. */\r\n readonly item?: any;\r\n}\r\n\r\n/** A hub message representing the result of an invocation. */\r\nexport interface CompletionMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Completion;\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n /** The error produced by the invocation, if any.\r\n *\r\n * Either {@link error} or {@link result} must be defined, but not both.\r\n */\r\n readonly error?: string;\r\n /** The result produced by the invocation, if any.\r\n *\r\n * Either {@link error} or {@link result} must be defined, but not both.\r\n */\r\n readonly result?: any;\r\n}\r\n\r\n/** A hub message indicating that the sender is still active. */\r\nexport interface PingMessage extends HubMessageBase {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Ping;\r\n}\r\n\r\n/** A hub message indicating that the sender is closing the connection.\r\n *\r\n * If {@link error} is defined, the sender is closing the connection due to an error.\r\n */\r\nexport interface CloseMessage extends HubMessageBase {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Close;\r\n /** The error that triggered the close, if any.\r\n *\r\n * If this property is undefined, the connection was closed normally and without error.\r\n */\r\n readonly error?: string;\r\n}\r\n\r\n/** A hub message sent to request that a streaming invocation be canceled. */\r\nexport interface CancelInvocationMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.CancelInvocation;\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n}\r\n\r\n/** A protocol abstraction for communicating with SignalR Hubs. */\r\nexport interface IHubProtocol {\r\n /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */\r\n readonly name: string;\r\n /** The version of the protocol. */\r\n readonly version: number;\r\n /** The {@link TransferFormat} of the protocol. */\r\n readonly transferFormat: TransferFormat;\r\n\r\n /** Creates an array of {@link HubMessage} objects from the specified serialized representation.\r\n *\r\n * If {@link transferFormat} is 'Text', the {@link input} parameter must be a string, otherwise it must be an ArrayBuffer.\r\n *\r\n * @param {string | ArrayBuffer} input A string, or ArrayBuffer containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];\r\n\r\n /** Writes the specified {@link HubMessage} to a string or ArrayBuffer and returns it.\r\n *\r\n * If {@link transferFormat} is 'Text', the result of this method will be a string, otherwise it will be an ArrayBuffer.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {string | ArrayBuffer} A string or ArrayBuffer containing the serialized representation of the message.\r\n */\r\n writeMessage(message: HubMessage): string | ArrayBuffer;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\n\r\n/** A logger that does nothing when log messages are sent to it. */\r\nexport class NullLogger implements ILogger {\r\n /** The singleton instance of the {@link NullLogger}. */\r\n public static instance: ILogger = new NullLogger();\r\n\r\n private constructor() {}\r\n\r\n /** @inheritDoc */\r\n public log(logLevel: LogLevel, message: string): void {\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpClient } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { IStreamResult, IStreamSubscriber, ISubscription } from \"./Stream\";\r\n\r\nexport class Arg {\r\n public static isRequired(val: any, name: string): void {\r\n if (val === null || val === undefined) {\r\n throw new Error(`The '${name}' argument is required.`);\r\n }\r\n }\r\n\r\n public static isIn(val: any, values: any, name: string): void {\r\n // TypeScript enums have keys for **both** the name and the value of each enum member on the type itself.\r\n if (!(val in values)) {\r\n throw new Error(`Unknown ${name} value: ${val}.`);\r\n }\r\n }\r\n}\r\n\r\nexport function getDataDetail(data: any, includeContent: boolean): string {\r\n let length: string = null;\r\n if (data instanceof ArrayBuffer) {\r\n length = `Binary data of length ${data.byteLength}`;\r\n if (includeContent) {\r\n length += `. Content: '${formatArrayBuffer(data)}'`;\r\n }\r\n } else if (typeof data === \"string\") {\r\n length = `String data of length ${data.length}`;\r\n if (includeContent) {\r\n length += `. Content: '${data}'.`;\r\n }\r\n }\r\n return length;\r\n}\r\n\r\nexport function formatArrayBuffer(data: ArrayBuffer): string {\r\n const view = new Uint8Array(data);\r\n\r\n // Uint8Array.map only supports returning another Uint8Array?\r\n let str = \"\";\r\n view.forEach((num) => {\r\n const pad = num < 16 ? \"0\" : \"\";\r\n str += `0x${pad}${num.toString(16)} `;\r\n });\r\n\r\n // Trim of trailing space.\r\n return str.substr(0, str.length - 1);\r\n}\r\n\r\nexport async function sendMessage(logger: ILogger, transportName: string, httpClient: HttpClient, url: string, accessTokenFactory: () => string | Promise, content: string | ArrayBuffer, logMessageContent: boolean): Promise {\r\n let headers;\r\n const token = await accessTokenFactory();\r\n if (token) {\r\n headers = {\r\n [\"Authorization\"]: `Bearer ${token}`,\r\n };\r\n }\r\n\r\n logger.log(LogLevel.Trace, `(${transportName} transport) sending data. ${getDataDetail(content, logMessageContent)}.`);\r\n\r\n const response = await httpClient.post(url, {\r\n content,\r\n headers,\r\n });\r\n\r\n logger.log(LogLevel.Trace, `(${transportName} transport) request complete. Response status: ${response.statusCode}.`);\r\n}\r\n\r\nexport function createLogger(logger?: ILogger | LogLevel) {\r\n if (logger === undefined) {\r\n return new ConsoleLogger(LogLevel.Information);\r\n }\r\n\r\n if (logger === null) {\r\n return NullLogger.instance;\r\n }\r\n\r\n if ((logger as ILogger).log) {\r\n return logger as ILogger;\r\n }\r\n\r\n return new ConsoleLogger(logger as LogLevel);\r\n}\r\n\r\nexport class Subject implements IStreamResult {\r\n public observers: Array>;\r\n public cancelCallback: () => Promise;\r\n\r\n constructor(cancelCallback: () => Promise) {\r\n this.observers = [];\r\n this.cancelCallback = cancelCallback;\r\n }\r\n\r\n public next(item: T): void {\r\n for (const observer of this.observers) {\r\n observer.next(item);\r\n }\r\n }\r\n\r\n public error(err: any): void {\r\n for (const observer of this.observers) {\r\n if (observer.error) {\r\n observer.error(err);\r\n }\r\n }\r\n }\r\n\r\n public complete(): void {\r\n for (const observer of this.observers) {\r\n if (observer.complete) {\r\n observer.complete();\r\n }\r\n }\r\n }\r\n\r\n public subscribe(observer: IStreamSubscriber): ISubscription {\r\n this.observers.push(observer);\r\n return new SubjectSubscription(this, observer);\r\n }\r\n}\r\n\r\nexport class SubjectSubscription implements ISubscription {\r\n private subject: Subject;\r\n private observer: IStreamSubscriber;\r\n\r\n constructor(subject: Subject, observer: IStreamSubscriber) {\r\n this.subject = subject;\r\n this.observer = observer;\r\n }\r\n\r\n public dispose(): void {\r\n const index: number = this.subject.observers.indexOf(this.observer);\r\n if (index > -1) {\r\n this.subject.observers.splice(index, 1);\r\n }\r\n\r\n if (this.subject.observers.length === 0) {\r\n this.subject.cancelCallback().catch((_) => { });\r\n }\r\n }\r\n}\r\n\r\nexport class ConsoleLogger implements ILogger {\r\n private readonly minimumLogLevel: LogLevel;\r\n\r\n constructor(minimumLogLevel: LogLevel) {\r\n this.minimumLogLevel = minimumLogLevel;\r\n }\r\n\r\n public log(logLevel: LogLevel, message: string): void {\r\n if (logLevel >= this.minimumLogLevel) {\r\n switch (logLevel) {\r\n case LogLevel.Critical:\r\n case LogLevel.Error:\r\n console.error(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n case LogLevel.Warning:\r\n console.warn(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n case LogLevel.Information:\r\n console.info(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n default:\r\n // console.debug only goes to attached debuggers in Node, so we use console.log for Trace and Debug\r\n console.log(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HandshakeProtocol, HandshakeRequestMessage, HandshakeResponseMessage } from \"./HandshakeProtocol\";\r\nimport { IConnection } from \"./IConnection\";\r\nimport { CancelInvocationMessage, CompletionMessage, IHubProtocol, InvocationMessage, MessageType, StreamInvocationMessage, StreamItemMessage } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { IStreamResult } from \"./Stream\";\r\nimport { Arg, Subject } from \"./Utils\";\r\n\r\nconst DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000;\r\n\r\n/** Represents a connection to a SignalR Hub. */\r\nexport class HubConnection {\r\n private readonly connection: IConnection;\r\n private readonly logger: ILogger;\r\n private protocol: IHubProtocol;\r\n private handshakeProtocol: HandshakeProtocol;\r\n private callbacks: { [invocationId: string]: (invocationEvent: StreamItemMessage | CompletionMessage, error?: Error) => void };\r\n private methods: { [name: string]: Array<(...args: any[]) => void> };\r\n private id: number;\r\n private closedCallbacks: Array<(error?: Error) => void>;\r\n private timeoutHandle: NodeJS.Timer;\r\n private receivedHandshakeResponse: boolean;\r\n\r\n /** The server timeout in milliseconds.\r\n *\r\n * If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.\r\n * The default timeout value is 30,000 milliseconds (30 seconds).\r\n */\r\n public serverTimeoutInMilliseconds: number;\r\n\r\n /** @internal */\r\n // Using a public static factory method means we can have a private constructor and an _internal_\r\n // create method that can be used by HubConnectionBuilder. An \"internal\" constructor would just\r\n // be stripped away and the '.d.ts' file would have no constructor, which is interpreted as a\r\n // public parameter-less constructor.\r\n public static create(connection: IConnection, logger: ILogger, protocol: IHubProtocol): HubConnection {\r\n return new HubConnection(connection, logger, protocol);\r\n }\r\n\r\n private constructor(connection: IConnection, logger: ILogger, protocol: IHubProtocol) {\r\n Arg.isRequired(connection, \"connection\");\r\n Arg.isRequired(logger, \"logger\");\r\n Arg.isRequired(protocol, \"protocol\");\r\n\r\n this.serverTimeoutInMilliseconds = DEFAULT_TIMEOUT_IN_MS;\r\n\r\n this.logger = logger;\r\n this.protocol = protocol;\r\n this.connection = connection;\r\n this.handshakeProtocol = new HandshakeProtocol();\r\n\r\n this.connection.onreceive = (data: any) => this.processIncomingData(data);\r\n this.connection.onclose = (error?: Error) => this.connectionClosed(error);\r\n\r\n this.callbacks = {};\r\n this.methods = {};\r\n this.closedCallbacks = [];\r\n this.id = 0;\r\n }\r\n\r\n /** Starts the connection.\r\n *\r\n * @returns {Promise} A Promise that resolves when the connection has been successfully established, or rejects with an error.\r\n */\r\n public async start(): Promise {\r\n const handshakeRequest: HandshakeRequestMessage = {\r\n protocol: this.protocol.name,\r\n version: this.protocol.version,\r\n };\r\n\r\n this.logger.log(LogLevel.Debug, \"Starting HubConnection.\");\r\n\r\n this.receivedHandshakeResponse = false;\r\n\r\n await this.connection.start(this.protocol.transferFormat);\r\n\r\n this.logger.log(LogLevel.Debug, \"Sending handshake request.\");\r\n\r\n await this.connection.send(this.handshakeProtocol.writeHandshakeRequest(handshakeRequest));\r\n\r\n this.logger.log(LogLevel.Information, `Using HubProtocol '${this.protocol.name}'.`);\r\n\r\n // defensively cleanup timeout in case we receive a message from the server before we finish start\r\n this.cleanupTimeout();\r\n this.configureTimeout();\r\n }\r\n\r\n /** Stops the connection.\r\n *\r\n * @returns {Promise} A Promise that resolves when the connection has been successfully terminated, or rejects with an error.\r\n */\r\n public stop(): Promise {\r\n this.logger.log(LogLevel.Debug, \"Stopping HubConnection.\");\r\n\r\n this.cleanupTimeout();\r\n return this.connection.stop();\r\n }\r\n\r\n /** Invokes a streaming hub method on the server using the specified name and arguments.\r\n *\r\n * @typeparam T The type of the items returned by the server.\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {IStreamResult} An object that yields results from the server as they are received.\r\n */\r\n public stream(methodName: string, ...args: any[]): IStreamResult {\r\n const invocationDescriptor = this.createStreamInvocation(methodName, args);\r\n\r\n const subject = new Subject(() => {\r\n const cancelInvocation: CancelInvocationMessage = this.createCancelInvocation(invocationDescriptor.invocationId);\r\n const cancelMessage: any = this.protocol.writeMessage(cancelInvocation);\r\n\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n\r\n return this.connection.send(cancelMessage);\r\n });\r\n\r\n this.callbacks[invocationDescriptor.invocationId] = (invocationEvent: CompletionMessage | StreamItemMessage, error?: Error) => {\r\n if (error) {\r\n subject.error(error);\r\n return;\r\n }\r\n\r\n if (invocationEvent.type === MessageType.Completion) {\r\n if (invocationEvent.error) {\r\n subject.error(new Error(invocationEvent.error));\r\n } else {\r\n subject.complete();\r\n }\r\n } else {\r\n subject.next((invocationEvent.item) as T);\r\n }\r\n };\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n this.connection.send(message)\r\n .catch((e) => {\r\n subject.error(e);\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n });\r\n\r\n return subject;\r\n }\r\n\r\n /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver.\r\n *\r\n * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still\r\n * be processing the invocation.\r\n *\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {Promise} A Promise that resolves when the invocation has been successfully sent, or rejects with an error.\r\n */\r\n public send(methodName: string, ...args: any[]): Promise {\r\n const invocationDescriptor = this.createInvocation(methodName, args, true);\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n return this.connection.send(message);\r\n }\r\n\r\n /** Invokes a hub method on the server using the specified name and arguments.\r\n *\r\n * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise\r\n * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of\r\n * resolving the Promise.\r\n *\r\n * @typeparam T The expected return type.\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {Promise} A Promise that resolves with the result of the server method (if any), or rejects with an error.\r\n */\r\n public invoke(methodName: string, ...args: any[]): Promise {\r\n const invocationDescriptor = this.createInvocation(methodName, args, false);\r\n\r\n const p = new Promise((resolve, reject) => {\r\n this.callbacks[invocationDescriptor.invocationId] = (invocationEvent: StreamItemMessage | CompletionMessage, error?: Error) => {\r\n if (error) {\r\n reject(error);\r\n return;\r\n }\r\n if (invocationEvent.type === MessageType.Completion) {\r\n const completionMessage = invocationEvent as CompletionMessage;\r\n if (completionMessage.error) {\r\n reject(new Error(completionMessage.error));\r\n } else {\r\n resolve(completionMessage.result);\r\n }\r\n } else {\r\n reject(new Error(`Unexpected message type: ${invocationEvent.type}`));\r\n }\r\n };\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n this.connection.send(message)\r\n .catch((e) => {\r\n reject(e);\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n });\r\n });\r\n\r\n return p;\r\n }\r\n\r\n /** Registers a handler that will be invoked when the hub method with the specified method name is invoked.\r\n *\r\n * @param {string} methodName The name of the hub method to define.\r\n * @param {Function} newMethod The handler that will be raised when the hub method is invoked.\r\n */\r\n public on(methodName: string, newMethod: (...args: any[]) => void) {\r\n if (!methodName || !newMethod) {\r\n return;\r\n }\r\n\r\n methodName = methodName.toLowerCase();\r\n if (!this.methods[methodName]) {\r\n this.methods[methodName] = [];\r\n }\r\n\r\n // Preventing adding the same handler multiple times.\r\n if (this.methods[methodName].indexOf(newMethod) !== -1) {\r\n return;\r\n }\r\n\r\n this.methods[methodName].push(newMethod);\r\n }\r\n\r\n /** Removes all handlers for the specified hub method.\r\n *\r\n * @param {string} methodName The name of the method to remove handlers for.\r\n */\r\n public off(methodName: string): void;\r\n\r\n /** Removes the specified handler for the specified hub method.\r\n *\r\n * You must pass the exact same Function instance as was previously passed to {@link on}. Passing a different instance (even if the function\r\n * body is the same) will not remove the handler.\r\n *\r\n * @param {string} methodName The name of the method to remove handlers for.\r\n * @param {Function} method The handler to remove. This must be the same Function instance as the one passed to {@link on}.\r\n */\r\n public off(methodName: string, method: (...args: any[]) => void): void;\r\n public off(methodName: string, method?: (...args: any[]) => void): void {\r\n if (!methodName) {\r\n return;\r\n }\r\n\r\n methodName = methodName.toLowerCase();\r\n const handlers = this.methods[methodName];\r\n if (!handlers) {\r\n return;\r\n }\r\n if (method) {\r\n const removeIdx = handlers.indexOf(method);\r\n if (removeIdx !== -1) {\r\n handlers.splice(removeIdx, 1);\r\n if (handlers.length === 0) {\r\n delete this.methods[methodName];\r\n }\r\n }\r\n } else {\r\n delete this.methods[methodName];\r\n }\r\n\r\n }\r\n\r\n /** Registers a handler that will be invoked when the connection is closed.\r\n *\r\n * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).\r\n */\r\n public onclose(callback: (error?: Error) => void) {\r\n if (callback) {\r\n this.closedCallbacks.push(callback);\r\n }\r\n }\r\n\r\n private processIncomingData(data: any) {\r\n this.cleanupTimeout();\r\n\r\n if (!this.receivedHandshakeResponse) {\r\n data = this.processHandshakeResponse(data);\r\n this.receivedHandshakeResponse = true;\r\n }\r\n\r\n // Data may have all been read when processing handshake response\r\n if (data) {\r\n // Parse the messages\r\n const messages = this.protocol.parseMessages(data, this.logger);\r\n\r\n for (const message of messages) {\r\n switch (message.type) {\r\n case MessageType.Invocation:\r\n this.invokeClientMethod(message);\r\n break;\r\n case MessageType.StreamItem:\r\n case MessageType.Completion:\r\n const callback = this.callbacks[message.invocationId];\r\n if (callback != null) {\r\n if (message.type === MessageType.Completion) {\r\n delete this.callbacks[message.invocationId];\r\n }\r\n callback(message);\r\n }\r\n break;\r\n case MessageType.Ping:\r\n // Don't care about pings\r\n break;\r\n case MessageType.Close:\r\n this.logger.log(LogLevel.Information, \"Close message received from server.\");\r\n this.connection.stop(message.error ? new Error(\"Server returned an error on close: \" + message.error) : null);\r\n break;\r\n default:\r\n this.logger.log(LogLevel.Warning, \"Invalid message type: \" + message.type);\r\n break;\r\n }\r\n }\r\n }\r\n\r\n this.configureTimeout();\r\n }\r\n\r\n private processHandshakeResponse(data: any): any {\r\n let responseMessage: HandshakeResponseMessage;\r\n let remainingData: any;\r\n\r\n try {\r\n [remainingData, responseMessage] = this.handshakeProtocol.parseHandshakeResponse(data);\r\n } catch (e) {\r\n const message = \"Error parsing handshake response: \" + e;\r\n this.logger.log(LogLevel.Error, message);\r\n\r\n const error = new Error(message);\r\n this.connection.stop(error);\r\n throw error;\r\n }\r\n if (responseMessage.error) {\r\n const message = \"Server returned handshake error: \" + responseMessage.error;\r\n this.logger.log(LogLevel.Error, message);\r\n this.connection.stop(new Error(message));\r\n } else {\r\n this.logger.log(LogLevel.Debug, \"Server handshake complete.\");\r\n }\r\n\r\n return remainingData;\r\n }\r\n\r\n private configureTimeout() {\r\n if (!this.connection.features || !this.connection.features.inherentKeepAlive) {\r\n // Set the timeout timer\r\n this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);\r\n }\r\n }\r\n\r\n private serverTimeout() {\r\n // The server hasn't talked to us in a while. It doesn't like us anymore ... :(\r\n // Terminate the connection\r\n this.connection.stop(new Error(\"Server timeout elapsed without receiving a message from the server.\"));\r\n }\r\n\r\n private invokeClientMethod(invocationMessage: InvocationMessage) {\r\n const methods = this.methods[invocationMessage.target.toLowerCase()];\r\n if (methods) {\r\n methods.forEach((m) => m.apply(this, invocationMessage.arguments));\r\n if (invocationMessage.invocationId) {\r\n // This is not supported in v1. So we return an error to avoid blocking the server waiting for the response.\r\n const message = \"Server requested a response, which is not supported in this version of the client.\";\r\n this.logger.log(LogLevel.Error, message);\r\n this.connection.stop(new Error(message));\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Warning, `No client method with the name '${invocationMessage.target}' found.`);\r\n }\r\n }\r\n\r\n private connectionClosed(error?: Error) {\r\n const callbacks = this.callbacks;\r\n this.callbacks = {};\r\n\r\n Object.keys(callbacks)\r\n .forEach((key) => {\r\n const callback = callbacks[key];\r\n callback(undefined, error ? error : new Error(\"Invocation canceled due to connection being closed.\"));\r\n });\r\n\r\n this.cleanupTimeout();\r\n\r\n this.closedCallbacks.forEach((c) => c.apply(this, [error]));\r\n }\r\n\r\n private cleanupTimeout(): void {\r\n if (this.timeoutHandle) {\r\n clearTimeout(this.timeoutHandle);\r\n }\r\n }\r\n\r\n private createInvocation(methodName: string, args: any[], nonblocking: boolean): InvocationMessage {\r\n if (nonblocking) {\r\n return {\r\n arguments: args,\r\n target: methodName,\r\n type: MessageType.Invocation,\r\n };\r\n } else {\r\n const id = this.id;\r\n this.id++;\r\n\r\n return {\r\n arguments: args,\r\n invocationId: id.toString(),\r\n target: methodName,\r\n type: MessageType.Invocation,\r\n };\r\n }\r\n }\r\n\r\n private createStreamInvocation(methodName: string, args: any[]): StreamInvocationMessage {\r\n const id = this.id;\r\n this.id++;\r\n\r\n return {\r\n arguments: args,\r\n invocationId: id.toString(),\r\n target: methodName,\r\n type: MessageType.StreamInvocation,\r\n };\r\n }\r\n\r\n private createCancelInvocation(id: string): CancelInvocationMessage {\r\n return {\r\n invocationId: id,\r\n type: MessageType.CancelInvocation,\r\n };\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This will be treated as a bit flag in the future, so we keep it using power-of-two values.\r\n/** Specifies a specific HTTP transport type. */\r\nexport enum HttpTransportType {\r\n /** Specifies no transport preference. */\r\n None = 0,\r\n /** Specifies the WebSockets transport. */\r\n WebSockets = 1,\r\n /** Specifies the Server-Sent Events transport. */\r\n ServerSentEvents = 2,\r\n /** Specifies the Long Polling transport. */\r\n LongPolling = 4,\r\n}\r\n\r\n/** Specifies the transfer format for a connection. */\r\nexport enum TransferFormat {\r\n /** Specifies that only text data will be transmitted over the connection. */\r\n Text = 1,\r\n /** Specifies that binary data will be transmitted over the connection. */\r\n Binary,\r\n}\r\n\r\n/** An abstraction over the behavior of transports. This is designed to support the framework and not intended for use by applications. */\r\nexport interface ITransport {\r\n connect(url: string, transferFormat: TransferFormat): Promise;\r\n send(data: any): Promise;\r\n stop(): Promise;\r\n onreceive: (data: string | ArrayBuffer) => void;\r\n onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Rough polyfill of https://developer.mozilla.org/en-US/docs/Web/API/AbortController\r\n// We don't actually ever use the API being polyfilled, we always use the polyfill because\r\n// it's a very new API right now.\r\n\r\n// Not exported from index.\r\nexport class AbortController implements AbortSignal {\r\n private isAborted: boolean = false;\r\n public onabort: () => void;\r\n\r\n public abort() {\r\n if (!this.isAborted) {\r\n this.isAborted = true;\r\n if (this.onabort) {\r\n this.onabort();\r\n }\r\n }\r\n }\r\n\r\n get signal(): AbortSignal {\r\n return this;\r\n }\r\n\r\n get aborted(): boolean {\r\n return this.isAborted;\r\n }\r\n}\r\n\r\n/** Represents a signal that can be monitored to determine if a request has been aborted. */\r\nexport interface AbortSignal {\r\n /** Indicates if the request has been aborted. */\r\n aborted: boolean;\r\n /** Set this to a handler that will be invoked when the request is aborted. */\r\n onabort: () => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { AbortController } from \"./AbortController\";\r\nimport { HttpError, TimeoutError } from \"./Errors\";\r\nimport { HttpClient, HttpRequest } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail, sendMessage } from \"./Utils\";\r\n\r\nconst SHUTDOWN_TIMEOUT = 5 * 1000;\r\n\r\n// Not exported from 'index', this type is internal.\r\nexport class LongPollingTransport implements ITransport {\r\n private readonly httpClient: HttpClient;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logger: ILogger;\r\n private readonly logMessageContent: boolean;\r\n\r\n private url: string;\r\n private pollXhr: XMLHttpRequest;\r\n private pollAbort: AbortController;\r\n private shutdownTimer: any; // We use 'any' because this is an object in NodeJS. But it still gets passed to clearTimeout, so it doesn't really matter\r\n private shutdownTimeout: number;\r\n private running: boolean;\r\n private stopped: boolean;\r\n\r\n // This is an internal type, not exported from 'index' so this is really just internal.\r\n public get pollAborted() {\r\n return this.pollAbort.aborted;\r\n }\r\n\r\n constructor(httpClient: HttpClient, accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean, shutdownTimeout?: number) {\r\n this.httpClient = httpClient;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logger = logger;\r\n this.pollAbort = new AbortController();\r\n this.logMessageContent = logMessageContent;\r\n this.shutdownTimeout = shutdownTimeout || SHUTDOWN_TIMEOUT;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n this.url = url;\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Connecting\");\r\n\r\n if (transferFormat === TransferFormat.Binary && (typeof new XMLHttpRequest().responseType !== \"string\")) {\r\n // This will work if we fix: https://github.com/aspnet/SignalR/issues/742\r\n throw new Error(\"Binary protocols over XmlHttpRequest not implementing advanced features are not supported.\");\r\n }\r\n\r\n const pollOptions: HttpRequest = {\r\n abortSignal: this.pollAbort.signal,\r\n headers: {},\r\n timeout: 90000,\r\n };\r\n\r\n if (transferFormat === TransferFormat.Binary) {\r\n pollOptions.responseType = \"arraybuffer\";\r\n }\r\n\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(pollOptions, token);\r\n\r\n let closeError: Error;\r\n\r\n // Make initial long polling request\r\n // Server uses first long polling request to finish initializing connection and it returns without data\r\n const pollUrl = `${url}&_=${Date.now()}`;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}`);\r\n const response = await this.httpClient.get(pollUrl, pollOptions);\r\n if (response.statusCode !== 200) {\r\n this.logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}`);\r\n\r\n // Mark running as false so that the poll immediately ends and runs the close logic\r\n closeError = new HttpError(response.statusText, response.statusCode);\r\n this.running = false;\r\n } else {\r\n this.running = true;\r\n }\r\n\r\n this.poll(this.url, pollOptions, closeError);\r\n return Promise.resolve();\r\n }\r\n\r\n private updateHeaderToken(request: HttpRequest, token: string) {\r\n if (token) {\r\n // tslint:disable-next-line:no-string-literal\r\n request.headers[\"Authorization\"] = `Bearer ${token}`;\r\n return;\r\n }\r\n // tslint:disable-next-line:no-string-literal\r\n if (request.headers[\"Authorization\"]) {\r\n // tslint:disable-next-line:no-string-literal\r\n delete request.headers[\"Authorization\"];\r\n }\r\n }\r\n\r\n private async poll(url: string, pollOptions: HttpRequest, closeError: Error): Promise {\r\n try {\r\n while (this.running) {\r\n // We have to get the access token on each poll, in case it changes\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(pollOptions, token);\r\n\r\n try {\r\n const pollUrl = `${url}&_=${Date.now()}`;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}`);\r\n const response = await this.httpClient.get(pollUrl, pollOptions);\r\n\r\n if (response.statusCode === 204) {\r\n this.logger.log(LogLevel.Information, \"(LongPolling transport) Poll terminated by server\");\r\n\r\n this.running = false;\r\n } else if (response.statusCode !== 200) {\r\n this.logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}`);\r\n\r\n // Unexpected status code\r\n closeError = new HttpError(response.statusText, response.statusCode);\r\n this.running = false;\r\n } else {\r\n // Process the response\r\n if (response.content) {\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) data received. ${getDataDetail(response.content, this.logMessageContent)}`);\r\n if (this.onreceive) {\r\n this.onreceive(response.content);\r\n }\r\n } else {\r\n // This is another way timeout manifest.\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Poll timed out, reissuing.\");\r\n }\r\n }\r\n } catch (e) {\r\n if (!this.running) {\r\n // Log but disregard errors that occur after we were stopped by DELETE\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) Poll errored after shutdown: ${e.message}`);\r\n } else {\r\n if (e instanceof TimeoutError) {\r\n // Ignore timeouts and reissue the poll.\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Poll timed out, reissuing.\");\r\n } else {\r\n // Close the connection with the error as the result.\r\n closeError = e;\r\n this.running = false;\r\n }\r\n }\r\n }\r\n }\r\n } finally {\r\n // Indicate that we've stopped so the shutdown timer doesn't get registered.\r\n this.stopped = true;\r\n\r\n // Clean up the shutdown timer if it was registered\r\n if (this.shutdownTimer) {\r\n clearTimeout(this.shutdownTimer);\r\n }\r\n\r\n // Fire our onclosed event\r\n if (this.onclose) {\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) Firing onclose event. Error: ${closeError || \"\"}`);\r\n this.onclose(closeError);\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Transport finished.\");\r\n }\r\n }\r\n\r\n public async send(data: any): Promise {\r\n if (!this.running) {\r\n return Promise.reject(new Error(\"Cannot send until the transport is connected\"));\r\n }\r\n return sendMessage(this.logger, \"LongPolling\", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent);\r\n }\r\n\r\n public async stop(): Promise {\r\n // Send a DELETE request to stop the poll\r\n try {\r\n this.running = false;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) sending DELETE request to ${this.url}.`);\r\n\r\n const deleteOptions: HttpRequest = {\r\n headers: {},\r\n };\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(deleteOptions, token);\r\n const response = await this.httpClient.delete(this.url, deleteOptions);\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) DELETE request accepted.\");\r\n } finally {\r\n // Abort the poll after the shutdown timeout if the server doesn't stop the poll.\r\n if (!this.stopped) {\r\n this.shutdownTimer = setTimeout(() => {\r\n this.logger.log(LogLevel.Warning, \"(LongPolling transport) server did not terminate after DELETE request, canceling poll.\");\r\n\r\n // Abort any outstanding poll\r\n this.pollAbort.abort();\r\n }, this.shutdownTimeout);\r\n }\r\n }\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpClient } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail, sendMessage } from \"./Utils\";\r\n\r\nexport class ServerSentEventsTransport implements ITransport {\r\n private readonly httpClient: HttpClient;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logger: ILogger;\r\n private readonly logMessageContent: boolean;\r\n private eventSource: EventSource;\r\n private url: string;\r\n\r\n constructor(httpClient: HttpClient, accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean) {\r\n this.httpClient = httpClient;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logger = logger;\r\n this.logMessageContent = logMessageContent;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n if (typeof (EventSource) === \"undefined\") {\r\n throw new Error(\"'EventSource' is not supported in your environment.\");\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(SSE transport) Connecting\");\r\n\r\n const token = await this.accessTokenFactory();\r\n if (token) {\r\n url += (url.indexOf(\"?\") < 0 ? \"?\" : \"&\") + `access_token=${encodeURIComponent(token)}`;\r\n }\r\n\r\n this.url = url;\r\n return new Promise((resolve, reject) => {\r\n let opened = false;\r\n if (transferFormat !== TransferFormat.Text) {\r\n reject(new Error(\"The Server-Sent Events transport only supports the 'Text' transfer format\"));\r\n }\r\n\r\n const eventSource = new EventSource(url, { withCredentials: true });\r\n\r\n try {\r\n eventSource.onmessage = (e: MessageEvent) => {\r\n if (this.onreceive) {\r\n try {\r\n this.logger.log(LogLevel.Trace, `(SSE transport) data received. ${getDataDetail(e.data, this.logMessageContent)}.`);\r\n this.onreceive(e.data);\r\n } catch (error) {\r\n if (this.onclose) {\r\n this.onclose(error);\r\n }\r\n return;\r\n }\r\n }\r\n };\r\n\r\n eventSource.onerror = (e: any) => {\r\n const error = new Error(e.message || \"Error occurred\");\r\n if (opened) {\r\n this.close(error);\r\n } else {\r\n reject(error);\r\n }\r\n };\r\n\r\n eventSource.onopen = () => {\r\n this.logger.log(LogLevel.Information, `SSE connected to ${this.url}`);\r\n this.eventSource = eventSource;\r\n opened = true;\r\n resolve();\r\n };\r\n } catch (e) {\r\n return Promise.reject(e);\r\n }\r\n });\r\n }\r\n\r\n public async send(data: any): Promise {\r\n if (!this.eventSource) {\r\n return Promise.reject(new Error(\"Cannot send until the transport is connected\"));\r\n }\r\n return sendMessage(this.logger, \"SSE\", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent);\r\n }\r\n\r\n public stop(): Promise {\r\n this.close();\r\n return Promise.resolve();\r\n }\r\n\r\n private close(e?: Error) {\r\n if (this.eventSource) {\r\n this.eventSource.close();\r\n this.eventSource = null;\r\n\r\n if (this.onclose) {\r\n this.onclose(e);\r\n }\r\n }\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail } from \"./Utils\";\r\n\r\nexport class WebSocketTransport implements ITransport {\r\n private readonly logger: ILogger;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logMessageContent: boolean;\r\n private webSocket: WebSocket;\r\n\r\n constructor(accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean) {\r\n this.logger = logger;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logMessageContent = logMessageContent;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n if (typeof (WebSocket) === \"undefined\") {\r\n throw new Error(\"'WebSocket' is not supported in your environment.\");\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) Connecting\");\r\n\r\n const token = await this.accessTokenFactory();\r\n if (token) {\r\n url += (url.indexOf(\"?\") < 0 ? \"?\" : \"&\") + `access_token=${encodeURIComponent(token)}`;\r\n }\r\n\r\n return new Promise((resolve, reject) => {\r\n url = url.replace(/^http/, \"ws\");\r\n const webSocket = new WebSocket(url);\r\n if (transferFormat === TransferFormat.Binary) {\r\n webSocket.binaryType = \"arraybuffer\";\r\n }\r\n\r\n webSocket.onopen = (event: Event) => {\r\n this.logger.log(LogLevel.Information, `WebSocket connected to ${url}`);\r\n this.webSocket = webSocket;\r\n resolve();\r\n };\r\n\r\n webSocket.onerror = (event: ErrorEvent) => {\r\n reject(event.error);\r\n };\r\n\r\n webSocket.onmessage = (message: MessageEvent) => {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) data received. ${getDataDetail(message.data, this.logMessageContent)}.`);\r\n if (this.onreceive) {\r\n this.onreceive(message.data);\r\n }\r\n };\r\n\r\n webSocket.onclose = (event: CloseEvent) => {\r\n // webSocket will be null if the transport did not start successfully\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) socket closed.\");\r\n if (this.onclose) {\r\n if (event.wasClean === false || event.code !== 1000) {\r\n this.onclose(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`));\r\n } else {\r\n this.onclose();\r\n }\r\n }\r\n };\r\n });\r\n }\r\n\r\n public send(data: any): Promise {\r\n if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) sending data. ${getDataDetail(data, this.logMessageContent)}.`);\r\n this.webSocket.send(data);\r\n return Promise.resolve();\r\n }\r\n\r\n return Promise.reject(\"WebSocket is not in the OPEN state\");\r\n }\r\n\r\n public stop(): Promise {\r\n if (this.webSocket) {\r\n this.webSocket.close();\r\n this.webSocket = null;\r\n }\r\n return Promise.resolve();\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { DefaultHttpClient, HttpClient } from \"./HttpClient\";\r\nimport { IConnection } from \"./IConnection\";\r\nimport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { HttpTransportType, ITransport, TransferFormat } from \"./ITransport\";\r\nimport { LongPollingTransport } from \"./LongPollingTransport\";\r\nimport { ServerSentEventsTransport } from \"./ServerSentEventsTransport\";\r\nimport { Arg, createLogger } from \"./Utils\";\r\nimport { WebSocketTransport } from \"./WebSocketTransport\";\r\n\r\nconst enum ConnectionState {\r\n Connecting,\r\n Connected,\r\n Disconnected,\r\n}\r\n\r\nexport interface INegotiateResponse {\r\n connectionId?: string;\r\n availableTransports?: IAvailableTransport[];\r\n url?: string;\r\n accessToken?: string;\r\n}\r\n\r\nexport interface IAvailableTransport {\r\n transport: keyof typeof HttpTransportType;\r\n transferFormats: Array;\r\n}\r\n\r\nconst MAX_REDIRECTS = 100;\r\n\r\nexport class HttpConnection implements IConnection {\r\n private connectionState: ConnectionState;\r\n private baseUrl: string;\r\n private readonly httpClient: HttpClient;\r\n private readonly logger: ILogger;\r\n private readonly options: IHttpConnectionOptions;\r\n private transport: ITransport;\r\n private startPromise: Promise;\r\n private stopError?: Error;\r\n private accessTokenFactory?: () => string | Promise;\r\n\r\n public readonly features: any = {};\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (e?: Error) => void;\r\n\r\n constructor(url: string, options: IHttpConnectionOptions = {}) {\r\n Arg.isRequired(url, \"url\");\r\n\r\n this.logger = createLogger(options.logger);\r\n this.baseUrl = this.resolveUrl(url);\r\n\r\n options = options || {};\r\n options.accessTokenFactory = options.accessTokenFactory || (() => null);\r\n options.logMessageContent = options.logMessageContent || false;\r\n\r\n this.httpClient = options.httpClient || new DefaultHttpClient(this.logger);\r\n this.connectionState = ConnectionState.Disconnected;\r\n this.options = options;\r\n }\r\n\r\n public start(): Promise;\r\n public start(transferFormat: TransferFormat): Promise;\r\n public start(transferFormat?: TransferFormat): Promise {\r\n transferFormat = transferFormat || TransferFormat.Binary;\r\n\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n this.logger.log(LogLevel.Debug, `Starting connection with transfer format '${TransferFormat[transferFormat]}'.`);\r\n\r\n if (this.connectionState !== ConnectionState.Disconnected) {\r\n return Promise.reject(new Error(\"Cannot start a connection that is not in the 'Disconnected' state.\"));\r\n }\r\n\r\n this.connectionState = ConnectionState.Connecting;\r\n\r\n this.startPromise = this.startInternal(transferFormat);\r\n return this.startPromise;\r\n }\r\n\r\n public send(data: string | ArrayBuffer): Promise {\r\n if (this.connectionState !== ConnectionState.Connected) {\r\n throw new Error(\"Cannot send data if the connection is not in the 'Connected' State.\");\r\n }\r\n\r\n return this.transport.send(data);\r\n }\r\n\r\n public async stop(error?: Error): Promise {\r\n this.connectionState = ConnectionState.Disconnected;\r\n\r\n try {\r\n await this.startPromise;\r\n } catch (e) {\r\n // this exception is returned to the user as a rejected Promise from the start method\r\n }\r\n\r\n // The transport's onclose will trigger stopConnection which will run our onclose event.\r\n if (this.transport) {\r\n this.stopError = error;\r\n await this.transport.stop();\r\n this.transport = null;\r\n }\r\n }\r\n\r\n private async startInternal(transferFormat: TransferFormat): Promise {\r\n // Store the original base url and the access token factory since they may change\r\n // as part of negotiating\r\n let url = this.baseUrl;\r\n this.accessTokenFactory = this.options.accessTokenFactory;\r\n\r\n try {\r\n if (this.options.skipNegotiation) {\r\n if (this.options.transport === HttpTransportType.WebSockets) {\r\n // No need to add a connection ID in this case\r\n this.transport = this.constructTransport(HttpTransportType.WebSockets);\r\n // We should just call connect directly in this case.\r\n // No fallback or negotiate in this case.\r\n await this.transport.connect(url, transferFormat);\r\n } else {\r\n throw Error(\"Negotiation can only be skipped when using the WebSocket transport directly.\");\r\n }\r\n } else {\r\n let negotiateResponse: INegotiateResponse = null;\r\n let redirects = 0;\r\n\r\n do {\r\n negotiateResponse = await this.getNegotiationResponse(url);\r\n // the user tries to stop the connection when it is being started\r\n if (this.connectionState === ConnectionState.Disconnected) {\r\n return;\r\n }\r\n\r\n if (negotiateResponse.url) {\r\n url = negotiateResponse.url;\r\n }\r\n\r\n if (negotiateResponse.accessToken) {\r\n // Replace the current access token factory with one that uses\r\n // the returned access token\r\n const accessToken = negotiateResponse.accessToken;\r\n this.accessTokenFactory = () => accessToken;\r\n }\r\n\r\n redirects++;\r\n }\r\n while (negotiateResponse.url && redirects < MAX_REDIRECTS);\r\n\r\n if (redirects === MAX_REDIRECTS && negotiateResponse.url) {\r\n throw Error(\"Negotiate redirection limit exceeded.\");\r\n }\r\n\r\n await this.createTransport(url, this.options.transport, negotiateResponse, transferFormat);\r\n }\r\n\r\n if (this.transport instanceof LongPollingTransport) {\r\n this.features.inherentKeepAlive = true;\r\n }\r\n\r\n this.transport.onreceive = this.onreceive;\r\n this.transport.onclose = (e) => this.stopConnection(e);\r\n\r\n // only change the state if we were connecting to not overwrite\r\n // the state if the connection is already marked as Disconnected\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n } catch (e) {\r\n this.logger.log(LogLevel.Error, \"Failed to start the connection: \" + e);\r\n this.connectionState = ConnectionState.Disconnected;\r\n this.transport = null;\r\n throw e;\r\n }\r\n }\r\n\r\n private async getNegotiationResponse(url: string): Promise {\r\n const token = await this.accessTokenFactory();\r\n let headers;\r\n if (token) {\r\n headers = {\r\n [\"Authorization\"]: `Bearer ${token}`,\r\n };\r\n }\r\n\r\n const negotiateUrl = this.resolveNegotiateUrl(url);\r\n this.logger.log(LogLevel.Debug, `Sending negotiation request: ${negotiateUrl}`);\r\n try {\r\n const response = await this.httpClient.post(negotiateUrl, {\r\n content: \"\",\r\n headers,\r\n });\r\n\r\n if (response.statusCode !== 200) {\r\n throw Error(`Unexpected status code returned from negotiate ${response.statusCode}`);\r\n }\r\n\r\n return JSON.parse(response.content as string) as INegotiateResponse;\r\n } catch (e) {\r\n this.logger.log(LogLevel.Error, \"Failed to complete negotiation with the server: \" + e);\r\n throw e;\r\n }\r\n }\r\n\r\n private createConnectUrl(url: string, connectionId: string) {\r\n return url + (url.indexOf(\"?\") === -1 ? \"?\" : \"&\") + `id=${connectionId}`;\r\n }\r\n\r\n private async createTransport(url: string, requestedTransport: HttpTransportType | ITransport, negotiateResponse: INegotiateResponse, requestedTransferFormat: TransferFormat): Promise {\r\n let connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);\r\n if (this.isITransport(requestedTransport)) {\r\n this.logger.log(LogLevel.Debug, \"Connection was provided an instance of ITransport, using that directly.\");\r\n this.transport = requestedTransport;\r\n await this.transport.connect(connectUrl, requestedTransferFormat);\r\n\r\n // only change the state if we were connecting to not overwrite\r\n // the state if the connection is already marked as Disconnected\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n return;\r\n }\r\n\r\n const transports = negotiateResponse.availableTransports;\r\n for (const endpoint of transports) {\r\n this.connectionState = ConnectionState.Connecting;\r\n const transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat);\r\n if (typeof transport === \"number\") {\r\n this.transport = this.constructTransport(transport);\r\n if (negotiateResponse.connectionId === null) {\r\n negotiateResponse = await this.getNegotiationResponse(url);\r\n connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);\r\n }\r\n try {\r\n await this.transport.connect(connectUrl, requestedTransferFormat);\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n return;\r\n } catch (ex) {\r\n this.logger.log(LogLevel.Error, `Failed to start the transport '${HttpTransportType[transport]}': ${ex}`);\r\n this.connectionState = ConnectionState.Disconnected;\r\n negotiateResponse.connectionId = null;\r\n }\r\n }\r\n }\r\n\r\n throw new Error(\"Unable to initialize any of the available transports.\");\r\n }\r\n\r\n private constructTransport(transport: HttpTransportType) {\r\n switch (transport) {\r\n case HttpTransportType.WebSockets:\r\n return new WebSocketTransport(this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n case HttpTransportType.ServerSentEvents:\r\n return new ServerSentEventsTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n case HttpTransportType.LongPolling:\r\n return new LongPollingTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n default:\r\n throw new Error(`Unknown transport: ${transport}.`);\r\n }\r\n }\r\n\r\n private resolveTransport(endpoint: IAvailableTransport, requestedTransport: HttpTransportType, requestedTransferFormat: TransferFormat): HttpTransportType | null {\r\n const transport = HttpTransportType[endpoint.transport];\r\n if (transport === null || transport === undefined) {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${endpoint.transport}' because it is not supported by this client.`);\r\n } else {\r\n const transferFormats = endpoint.transferFormats.map((s) => TransferFormat[s]);\r\n if (transportMatches(requestedTransport, transport)) {\r\n if (transferFormats.indexOf(requestedTransferFormat) >= 0) {\r\n if ((transport === HttpTransportType.WebSockets && typeof WebSocket === \"undefined\") ||\r\n (transport === HttpTransportType.ServerSentEvents && typeof EventSource === \"undefined\")) {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it is not supported in your environment.'`);\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Selecting transport '${HttpTransportType[transport]}'`);\r\n return transport;\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it does not support the requested transfer format '${TransferFormat[requestedTransferFormat]}'.`);\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it was disabled by the client.`);\r\n }\r\n }\r\n return null;\r\n }\r\n\r\n private isITransport(transport: any): transport is ITransport {\r\n return transport && typeof (transport) === \"object\" && \"connect\" in transport;\r\n }\r\n\r\n private changeState(from: ConnectionState, to: ConnectionState): boolean {\r\n if (this.connectionState === from) {\r\n this.connectionState = to;\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private async stopConnection(error?: Error): Promise {\r\n this.transport = null;\r\n\r\n // If we have a stopError, it takes precedence over the error from the transport\r\n error = this.stopError || error;\r\n\r\n if (error) {\r\n this.logger.log(LogLevel.Error, `Connection disconnected with error '${error}'.`);\r\n } else {\r\n this.logger.log(LogLevel.Information, \"Connection disconnected.\");\r\n }\r\n\r\n this.connectionState = ConnectionState.Disconnected;\r\n\r\n if (this.onclose) {\r\n this.onclose(error);\r\n }\r\n }\r\n\r\n private resolveUrl(url: string): string {\r\n // startsWith is not supported in IE\r\n if (url.lastIndexOf(\"https://\", 0) === 0 || url.lastIndexOf(\"http://\", 0) === 0) {\r\n return url;\r\n }\r\n\r\n if (typeof window === \"undefined\" || !window || !window.document) {\r\n throw new Error(`Cannot resolve '${url}'.`);\r\n }\r\n\r\n // Setting the url to the href propery of an anchor tag handles normalization\r\n // for us. There are 3 main cases.\r\n // 1. Relative path normalization e.g \"b\" -> \"http://localhost:5000/a/b\"\r\n // 2. Absolute path normalization e.g \"/a/b\" -> \"http://localhost:5000/a/b\"\r\n // 3. Networkpath reference normalization e.g \"//localhost:5000/a/b\" -> \"http://localhost:5000/a/b\"\r\n const aTag = window.document.createElement(\"a\");\r\n aTag.href = url;\r\n\r\n this.logger.log(LogLevel.Information, `Normalizing '${url}' to '${aTag.href}'.`);\r\n return aTag.href;\r\n }\r\n\r\n private resolveNegotiateUrl(url: string): string {\r\n const index = url.indexOf(\"?\");\r\n let negotiateUrl = url.substring(0, index === -1 ? url.length : index);\r\n if (negotiateUrl[negotiateUrl.length - 1] !== \"/\") {\r\n negotiateUrl += \"/\";\r\n }\r\n negotiateUrl += \"negotiate\";\r\n negotiateUrl += index === -1 ? \"\" : url.substring(index);\r\n return negotiateUrl;\r\n }\r\n}\r\n\r\nfunction transportMatches(requestedTransport: HttpTransportType, actualTransport: HttpTransportType) {\r\n return !requestedTransport || ((actualTransport & requestedTransport) !== 0);\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { CompletionMessage, HubMessage, IHubProtocol, InvocationMessage, MessageType, StreamItemMessage } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { TransferFormat } from \"./ITransport\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { TextMessageFormat } from \"./TextMessageFormat\";\r\n\r\nconst JSON_HUB_PROTOCOL_NAME: string = \"json\";\r\n\r\n/** Implements the JSON Hub Protocol. */\r\nexport class JsonHubProtocol implements IHubProtocol {\r\n\r\n /** @inheritDoc */\r\n public readonly name: string = JSON_HUB_PROTOCOL_NAME;\r\n /** @inheritDoc */\r\n public readonly version: number = 1;\r\n\r\n /** @inheritDoc */\r\n public readonly transferFormat: TransferFormat = TransferFormat.Text;\r\n\r\n /** Creates an array of {@link HubMessage} objects from the specified serialized representation.\r\n *\r\n * @param {string} input A string containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n public parseMessages(input: string, logger: ILogger): HubMessage[] {\r\n // The interface does allow \"ArrayBuffer\" to be passed in, but this implementation does not. So let's throw a useful error.\r\n if (typeof input !== \"string\") {\r\n throw new Error(\"Invalid input for JSON hub protocol. Expected a string.\");\r\n }\r\n\r\n if (!input) {\r\n return [];\r\n }\r\n\r\n if (logger === null) {\r\n logger = NullLogger.instance;\r\n }\r\n\r\n // Parse the messages\r\n const messages = TextMessageFormat.parse(input);\r\n\r\n const hubMessages = [];\r\n for (const message of messages) {\r\n const parsedMessage = JSON.parse(message) as HubMessage;\r\n if (typeof parsedMessage.type !== \"number\") {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n switch (parsedMessage.type) {\r\n case MessageType.Invocation:\r\n this.isInvocationMessage(parsedMessage);\r\n break;\r\n case MessageType.StreamItem:\r\n this.isStreamItemMessage(parsedMessage);\r\n break;\r\n case MessageType.Completion:\r\n this.isCompletionMessage(parsedMessage);\r\n break;\r\n case MessageType.Ping:\r\n // Single value, no need to validate\r\n break;\r\n case MessageType.Close:\r\n // All optional values, no need to validate\r\n break;\r\n default:\r\n // Future protocol changes can add message types, old clients can ignore them\r\n logger.log(LogLevel.Information, \"Unknown message type '\" + parsedMessage.type + \"' ignored.\");\r\n continue;\r\n }\r\n hubMessages.push(parsedMessage);\r\n }\r\n\r\n return hubMessages;\r\n }\r\n\r\n /** Writes the specified {@link HubMessage} to a string and returns it.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {string} A string containing the serialized representation of the message.\r\n */\r\n public writeMessage(message: HubMessage): string {\r\n return TextMessageFormat.write(JSON.stringify(message));\r\n }\r\n\r\n private isInvocationMessage(message: InvocationMessage): void {\r\n this.assertNotEmptyString(message.target, \"Invalid payload for Invocation message.\");\r\n\r\n if (message.invocationId !== undefined) {\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for Invocation message.\");\r\n }\r\n }\r\n\r\n private isStreamItemMessage(message: StreamItemMessage): void {\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for StreamItem message.\");\r\n\r\n if (message.item === undefined) {\r\n throw new Error(\"Invalid payload for StreamItem message.\");\r\n }\r\n }\r\n\r\n private isCompletionMessage(message: CompletionMessage): void {\r\n if (message.result && message.error) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n if (!message.result && message.error) {\r\n this.assertNotEmptyString(message.error, \"Invalid payload for Completion message.\");\r\n }\r\n\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for Completion message.\");\r\n }\r\n\r\n private assertNotEmptyString(value: any, errorMessage: string): void {\r\n if (typeof value !== \"string\" || value === \"\") {\r\n throw new Error(errorMessage);\r\n }\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpConnection } from \"./HttpConnection\";\r\nimport { HubConnection } from \"./HubConnection\";\r\nimport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nimport { IHubProtocol } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { HttpTransportType } from \"./ITransport\";\r\nimport { JsonHubProtocol } from \"./JsonHubProtocol\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { Arg, ConsoleLogger } from \"./Utils\";\r\n\r\n/** A builder for configuring {@link HubConnection} instances. */\r\nexport class HubConnectionBuilder {\r\n /** @internal */\r\n public protocol: IHubProtocol;\r\n /** @internal */\r\n public httpConnectionOptions: IHttpConnectionOptions;\r\n /** @internal */\r\n public url: string;\r\n /** @internal */\r\n public logger: ILogger;\r\n\r\n /** Configures console logging for the {@link HubConnection}.\r\n *\r\n * @param {LogLevel} logLevel The minimum level of messages to log. Anything at this level, or a more severe level, will be logged.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public configureLogging(logLevel: LogLevel): HubConnectionBuilder;\r\n\r\n /** Configures custom logging for the {@link HubConnection}.\r\n *\r\n * @param {ILogger} logger An object implementing the {@link ILogger} interface, which will be used to write all log messages.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public configureLogging(logger: ILogger): HubConnectionBuilder;\r\n public configureLogging(logging: LogLevel | ILogger): HubConnectionBuilder {\r\n Arg.isRequired(logging, \"logging\");\r\n\r\n if (isLogger(logging)) {\r\n this.logger = logging;\r\n } else {\r\n this.logger = new ConsoleLogger(logging);\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /** Configures the {@link HubConnection} to use HTTP-based transports to connect to the specified URL.\r\n *\r\n * The transport will be selected automatically based on what the server and client support.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string): HubConnectionBuilder;\r\n\r\n /** Configures the {@link HubConnection} to use the specified HTTP-based transport to connect to the specified URL.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @param {HttpTransportType} transportType The specific transport to use.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string, transportType: HttpTransportType): HubConnectionBuilder;\r\n\r\n /** Configures the {@link HubConnection} to use HTTP-based transports to connect to the specified URL.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @param {IHttpConnectionOptions} options An options object used to configure the connection.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string, options: IHttpConnectionOptions): HubConnectionBuilder;\r\n public withUrl(url: string, transportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType): HubConnectionBuilder {\r\n Arg.isRequired(url, \"url\");\r\n\r\n this.url = url;\r\n\r\n // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed\r\n // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called.\r\n if (typeof transportTypeOrOptions === \"object\") {\r\n this.httpConnectionOptions = transportTypeOrOptions;\r\n } else {\r\n this.httpConnectionOptions = {\r\n transport: transportTypeOrOptions,\r\n };\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /** Configures the {@link HubConnection} to use the specified Hub Protocol.\r\n *\r\n * @param {IHubProtocol} protocol The {@link IHubProtocol} implementation to use.\r\n */\r\n public withHubProtocol(protocol: IHubProtocol): HubConnectionBuilder {\r\n Arg.isRequired(protocol, \"protocol\");\r\n\r\n this.protocol = protocol;\r\n return this;\r\n }\r\n\r\n /** Creates a {@link HubConnection} from the configuration options specified in this builder.\r\n *\r\n * @returns {HubConnection} The configured {@link HubConnection}.\r\n */\r\n public build(): HubConnection {\r\n // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one\r\n // provided to configureLogger\r\n const httpConnectionOptions = this.httpConnectionOptions || {};\r\n\r\n // If it's 'null', the user **explicitly** asked for null, don't mess with it.\r\n if (httpConnectionOptions.logger === undefined) {\r\n // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.\r\n httpConnectionOptions.logger = this.logger;\r\n }\r\n\r\n // Now create the connection\r\n if (!this.url) {\r\n throw new Error(\"The 'HubConnectionBuilder.withUrl' method must be called before building the connection.\");\r\n }\r\n const connection = new HttpConnection(this.url, httpConnectionOptions);\r\n\r\n return HubConnection.create(\r\n connection,\r\n this.logger || NullLogger.instance,\r\n this.protocol || new JsonHubProtocol());\r\n }\r\n}\r\n\r\nfunction isLogger(logger: any): logger is ILogger {\r\n return logger.log !== undefined;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Version token that will be replaced by the prepack command\r\n/** The version of the SignalR client. */\r\nexport const VERSION: string = \"0.0.0-DEV_BUILD\";\r\n\r\n// Everything that users need to access must be exported here. Including interfaces.\r\nexport { AbortSignal } from \"./AbortController\";\r\nexport { HttpError, TimeoutError } from \"./Errors\";\r\nexport { DefaultHttpClient, HttpClient, HttpRequest, HttpResponse } from \"./HttpClient\";\r\nexport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nexport { HubConnection } from \"./HubConnection\";\r\nexport { HubConnectionBuilder } from \"./HubConnectionBuilder\";\r\nexport { MessageType, MessageHeaders, HubMessage, HubMessageBase, HubInvocationMessage, InvocationMessage, StreamInvocationMessage, StreamItemMessage, CompletionMessage, PingMessage, CloseMessage, CancelInvocationMessage, IHubProtocol } from \"./IHubProtocol\";\r\nexport { ILogger, LogLevel } from \"./ILogger\";\r\nexport { HttpTransportType, TransferFormat, ITransport } from \"./ITransport\";\r\nexport { IStreamSubscriber, IStreamResult, ISubscription } from \"./Stream\";\r\nexport { NullLogger } from \"./Loggers\";\r\nexport { JsonHubProtocol } from \"./JsonHubProtocol\";\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds.\r\n\r\nimport \"es6-promise/dist/es6-promise.auto.js\";\r\n\r\n// Copy from Array.prototype into Uint8Array to polyfill on IE. It's OK because the implementations of indexOf and slice use properties\r\n// that exist on Uint8Array with the same name, and JavaScript is magic.\r\n// We make them 'writable' because the Buffer polyfill messes with it as well.\r\nif (!Uint8Array.prototype.indexOf) {\r\n Object.defineProperty(Uint8Array.prototype, \"indexOf\", {\r\n value: Array.prototype.indexOf,\r\n writable: true,\r\n });\r\n}\r\nif (!Uint8Array.prototype.slice) {\r\n Object.defineProperty(Uint8Array.prototype, \"slice\", {\r\n value: Array.prototype.slice,\r\n writable: true,\r\n });\r\n}\r\nif (!Uint8Array.prototype.forEach) {\r\n Object.defineProperty(Uint8Array.prototype, \"forEach\", {\r\n value: Array.prototype.forEach,\r\n writable: true,\r\n });\r\n}\r\n\r\nexport * from \"./index\";\r\n"],"names":["require","resolve","_resolve","then","originalThen","originalResolve","Promise","reject","_reject","Resolve","Reject","global","Errors_1","ILogger_1","Loggers_1","Utils_1","IHubProtocol_1","ITransport_1"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;;;AAgBA,IAAI,aAAa,GAAG,MAAM,CAAC,cAAc;KACpC,EAAE,SAAS,EAAE,EAAE,EAAE,YAAY,KAAK,IAAI,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,GAAG,CAAC,CAAC,EAAE,CAAC;IAC5E,UAAU,CAAC,EAAE,CAAC,EAAE,EAAE,KAAK,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;;AAE/E,AAAO,SAAS,SAAS,CAAC,CAAC,EAAE,CAAC,EAAE;IAC5B,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACpB,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,WAAW,GAAG,CAAC,CAAC,EAAE;IACvC,CAAC,CAAC,SAAS,GAAG,CAAC,KAAK,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,SAAS,GAAG,CAAC,CAAC,SAAS,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;CACxF;;AAED,AAAO,IAAI,QAAQ,GAAG,MAAM,CAAC,MAAM,IAAI,SAAS,QAAQ,CAAC,CAAC,EAAE;IACxD,KAAK,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,S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AC,CAAC;CACN;AACD,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,OAAO,EAAE;IAC/B,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,SAAS,EAAE,SAAS,EAAE;QACnD,KAAK,EAAE,KAAK,CAAC,SAAS,CAAC,OAAO;QAC9B,QAAQ,EAAE,IAAI;KACjB,CAAC,CAAC;CACN;AAED,mCAAwB;;;;;;;;;;;;"} \ No newline at end of file diff --git a/samples/ChatSample/wwwroot/scripts/signalr.min.js b/samples/ChatSample/wwwroot/scripts/signalr.min.js index 1071d32c3..989b5c644 100644 --- a/samples/ChatSample/wwwroot/scripts/signalr.min.js +++ b/samples/ChatSample/wwwroot/scripts/signalr.min.js @@ -1,8 +1,11 @@ -/* @license +/** + * @overview ASP.NET Core SignalR JavaScript Client. + * @version 1.0.0. + * @license * Copyright (c) .NET Foundation. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -*/ -(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):global.signalR=factory()})(this,function(){"use strict";var commonjsGlobal=typeof window!=="undefined"?window:typeof global!=="undefined"?global:typeof self!=="undefined"?self:{};function commonjsRequire(){throw new Error("Dynamic requires are not currently supported by rollup-plugin-commonjs")}function unwrapExports(x){return x&&x.__esModule&&Object.prototype.hasOwnProperty.call(x,"default")?x["default"]:x}function createCommonjsModule(fn,module){return module={exports:{}},fn(module,module.exports),module.exports}var es6Promise_auto=createCommonjsModule(function(module,exports){ + */ +(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?module.exports=factory():typeof define==="function"&&define.amd?define(factory):global.signalR=factory()})(this,function(){"use strict";var commonjsGlobal=typeof window!=="undefined"?window:typeof global!=="undefined"?global:typeof self!=="undefined"?self:{};function commonjsRequire(){throw new Error("Dynamic requires are not currently supported by rollup-plugin-commonjs")}function unwrapExports(x){return x&&x.__esModule&&Object.prototype.hasOwnProperty.call(x,"default")?x["default"]:x}function createCommonjsModule(fn,module){return module={exports:{}},fn(module,module.exports),module.exports}var extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(d,b){d.__proto__=b}||function(d,b){for(var p in b)if(b.hasOwnProperty(p))d[p]=b[p]};function __extends(d,b){extendStatics(d,b);function __(){this.constructor=d}d.prototype=b===null?Object.create(b):(__.prototype=b.prototype,new __)}var __assign=Object.assign||function __assign(t){for(var s,i=1,n=arguments.length;i=0;i--)if(d=decorators[i])r=(c<3?d(r):c>3?d(target,key,r):d(target,key))||r;return c>3&&r&&Object.defineProperty(target,key,r),r}function __param(paramIndex,decorator){return function(target,key){decorator(target,key,paramIndex)}}function __metadata(metadataKey,metadataValue){if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(metadataKey,metadataValue)}function __awaiter(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})}function __generator(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=y[op[0]&2?"return":op[0]?"throw":"next"])&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[0,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]=o.length)o=void 0;return{value:o&&o[i++],done:!o}}}}function __read(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar}function __spread(){for(var ar=[],i=0;i1||resume(n,v)})}}function resume(n,v){try{step(g[n](v))}catch(e){settle(q[0][3],e)}}function step(r){r.value instanceof __await?Promise.resolve(r.value.v).then(fulfill,reject):settle(q[0][2],r)}function fulfill(value){resume("next",value)}function reject(value){resume("throw",value)}function settle(f,v){if(f(v),q.shift(),q.length)resume(q[0][0],q[0][1])}}function __asyncDelegator(o){var i,p;return i={},verb("next"),verb("throw",function(e){throw e}),verb("return"),i[Symbol.iterator]=function(){return this},i;function verb(n,f){if(o[n])i[n]=function(v){return(p=!p)?{value:__await(o[n](v)),done:n==="return"}:f?f(v):v}}}function __asyncValues(o){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var m=o[Symbol.asyncIterator];return m?m.call(o):typeof __values==="function"?__values(o):o[Symbol.iterator]()}function __makeTemplateObject(cooked,raw){if(Object.defineProperty){Object.defineProperty(cooked,"raw",{value:raw})}else{cooked.raw=raw}return cooked}function __importStar(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(Object.hasOwnProperty.call(mod,k))result[k]=mod[k];result.default=mod;return result}function __importDefault(mod){return mod&&mod.__esModule?mod:{default:mod}}var tslib_1=Object.freeze({__extends:__extends,__assign:__assign,__rest:__rest,__decorate:__decorate,__param:__param,__metadata:__metadata,__awaiter:__awaiter,__generator:__generator,__exportStar:__exportStar,__values:__values,__read:__read,__spread:__spread,__await:__await,__asyncGenerator:__asyncGenerator,__asyncDelegator:__asyncDelegator,__asyncValues:__asyncValues,__makeTemplateObject:__makeTemplateObject,__importStar:__importStar,__importDefault:__importDefault});var es6Promise_auto=createCommonjsModule(function(module,exports){ /*! * @overview es6-promise - a tiny implementation of Promises/A+. * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald) @@ -10,5 +13,5 @@ * See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE * @version v4.2.2+97478eb6 */ -(function(global,factory){module.exports=factory()})(commonjsGlobal,function(){function objectOrFunction(x){var type=typeof x;return x!==null&&(type==="object"||type==="function")}function isFunction(x){return typeof x==="function"}var _isArray=void 0;if(Array.isArray){_isArray=Array.isArray}else{_isArray=function(x){return Object.prototype.toString.call(x)==="[object Array]"}}var isArray=_isArray;var len=0;var vertxNext=void 0;var customSchedulerFn=void 0;var asap=function asap(callback,arg){queue[len]=callback;queue[len+1]=arg;len+=2;if(len===2){if(customSchedulerFn){customSchedulerFn(flush)}else{scheduleFlush()}}};function setScheduler(scheduleFn){customSchedulerFn=scheduleFn}function setAsap(asapFn){asap=asapFn}var browserWindow=typeof window!=="undefined"?window:undefined;var browserGlobal=browserWindow||{};var BrowserMutationObserver=browserGlobal.MutationObserver||browserGlobal.WebKitMutationObserver;var isNode=typeof self==="undefined"&&typeof process!=="undefined"&&{}.toString.call(process)==="[object process]";var isWorker=typeof Uint8ClampedArray!=="undefined"&&typeof importScripts!=="undefined"&&typeof MessageChannel!=="undefined";function useNextTick(){return function(){return process.nextTick(flush)}}function useVertxTimer(){if(typeof vertxNext!=="undefined"){return function(){vertxNext(flush)}}return useSetTimeout()}function useMutationObserver(){var iterations=0;var observer=new BrowserMutationObserver(flush);var node=document.createTextNode("");observer.observe(node,{characterData:true});return function(){node.data=iterations=++iterations%2}}function useMessageChannel(){var channel=new MessageChannel;channel.port1.onmessage=flush;return function(){return channel.port2.postMessage(0)}}function useSetTimeout(){var globalSetTimeout=setTimeout;return function(){return globalSetTimeout(flush,1)}}var queue=new Array(1e3);function flush(){for(var i=0;i=200&&xhr.status<300){resolve(new HttpResponse(xhr.status,xhr.statusText,xhr.response||xhr.responseText))}else{reject(new Errors.HttpError(xhr.statusText,xhr.status))}};xhr.onerror=function(){reject(new Errors.HttpError(xhr.statusText,xhr.status))};xhr.ontimeout=function(){reject(new Errors.TimeoutError)};xhr.send(request.content||"")})};return DefaultHttpClient}(HttpClient);exports.DefaultHttpClient=DefaultHttpClient});unwrapExports(HttpClient_1);var HttpClient_2=HttpClient_1.HttpResponse;var HttpClient_3=HttpClient_1.HttpClient;var HttpClient_4=HttpClient_1.DefaultHttpClient;var ILogger=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var LogLevel;(function(LogLevel){LogLevel[LogLevel["Trace"]=0]="Trace";LogLevel[LogLevel["Information"]=1]="Information";LogLevel[LogLevel["Warning"]=2]="Warning";LogLevel[LogLevel["Error"]=3]="Error";LogLevel[LogLevel["None"]=4]="None"})(LogLevel=exports.LogLevel||(exports.LogLevel={}))});unwrapExports(ILogger);var ILogger_1=ILogger.LogLevel;var Loggers=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var NullLogger=function(){function NullLogger(){}NullLogger.prototype.log=function(logLevel,message){};return NullLogger}();exports.NullLogger=NullLogger;var ConsoleLogger=function(){function ConsoleLogger(minimumLogLevel){this.minimumLogLevel=minimumLogLevel}ConsoleLogger.prototype.log=function(logLevel,message){if(logLevel>=this.minimumLogLevel){switch(logLevel){case ILogger.LogLevel.Error:console.error(ILogger.LogLevel[logLevel]+": "+message);break;case ILogger.LogLevel.Warning:console.warn(ILogger.LogLevel[logLevel]+": "+message);break;case ILogger.LogLevel.Information:console.info(ILogger.LogLevel[logLevel]+": "+message);break;default:console.log(ILogger.LogLevel[logLevel]+": "+message);break}}};return ConsoleLogger}();exports.ConsoleLogger=ConsoleLogger;var LoggerFactory=function(){function LoggerFactory(){}LoggerFactory.createLogger=function(logging){if(logging===undefined){return new ConsoleLogger(ILogger.LogLevel.Information)}if(logging===null){return new NullLogger}if(logging.log){return logging}return new ConsoleLogger(logging)};return LoggerFactory}();exports.LoggerFactory=LoggerFactory});unwrapExports(Loggers);var Loggers_1=Loggers.NullLogger;var Loggers_2=Loggers.ConsoleLogger;var Loggers_3=Loggers.LoggerFactory;var AbortController_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var AbortController=function(){function AbortController(){this.isAborted=false}AbortController.prototype.abort=function(){if(!this.isAborted){this.isAborted=true;if(this.onabort){this.onabort()}}};Object.defineProperty(AbortController.prototype,"signal",{get:function(){return this},enumerable:true,configurable:true});Object.defineProperty(AbortController.prototype,"aborted",{get:function(){return this.isAborted},enumerable:true,configurable:true});return AbortController}();exports.AbortController=AbortController});unwrapExports(AbortController_1);var AbortController_2=AbortController_1.AbortController;var Transports=createCommonjsModule(function(module,exports){var __awaiter=commonjsGlobal&&commonjsGlobal.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=commonjsGlobal&&commonjsGlobal.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=y[op[0]&2?"return":op[0]?"throw":"next"])&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[0,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]0){transport=Transports.TransportType[availableTransports[0]]}if(transport===Transports.TransportType.WebSockets&&availableTransports.indexOf(Transports.TransportType[transport])>=0){return new Transports.WebSocketTransport(this.options.accessTokenFactory,this.logger)}if(transport===Transports.TransportType.ServerSentEvents&&availableTransports.indexOf(Transports.TransportType[transport])>=0){return new Transports.ServerSentEventsTransport(this.httpClient,this.options.accessTokenFactory,this.logger)}if(transport===Transports.TransportType.LongPolling&&availableTransports.indexOf(Transports.TransportType[transport])>=0){return new Transports.LongPollingTransport(this.httpClient,this.options.accessTokenFactory,this.logger)}if(this.isITransport(transport)){return transport}throw new Error("No available transports found.")};HttpConnection.prototype.isITransport=function(transport){return typeof transport==="object"&&"connect"in transport};HttpConnection.prototype.changeState=function(from,to){if(this.connectionState===from){this.connectionState=to;return true}return false};HttpConnection.prototype.send=function(data){if(this.connectionState!==1){throw new Error("Cannot send data if the connection is not in the 'Connected' State")}return this.transport.send(data)};HttpConnection.prototype.stop=function(error){return __awaiter(this,void 0,void 0,function(){var previousState,e_2;return __generator(this,function(_a){switch(_a.label){case 0:previousState=this.connectionState;this.connectionState=2;_a.label=1;case 1:_a.trys.push([1,3,,4]);return[4,this.startPromise];case 2:_a.sent();return[3,4];case 3:e_2=_a.sent();return[3,4];case 4:this.stopConnection(previousState===1,error);return[2]}})})};HttpConnection.prototype.stopConnection=function(raiseClosed,error){if(this.transport){this.transport.stop();this.transport=null}if(error){this.logger.log(ILogger.LogLevel.Error,"Connection disconnected with error '"+error+"'.")}else{this.logger.log(ILogger.LogLevel.Information,"Connection disconnected.")}this.connectionState=2;if(raiseClosed&&this.onclose){this.onclose(error)}};HttpConnection.prototype.resolveUrl=function(url){if(url.lastIndexOf("https://",0)===0||url.lastIndexOf("http://",0)===0){return url}if(typeof window==="undefined"||!window||!window.document){throw new Error("Cannot resolve '"+url+"'.")}var parser=window.document.createElement("a");parser.href=url;var baseUrl=!parser.protocol||parser.protocol===":"?window.document.location.protocol+"//"+(parser.host||window.document.location.host):parser.protocol+"//"+parser.host;if(!url||url[0]!=="/"){url="/"+url}var normalizedUrl=baseUrl+url;this.logger.log(ILogger.LogLevel.Information,"Normalizing '"+url+"' to '"+normalizedUrl+"'");return normalizedUrl};HttpConnection.prototype.resolveNegotiateUrl=function(url){var index=url.indexOf("?");var negotiateUrl=url.substring(0,index===-1?url.length:index);if(negotiateUrl[negotiateUrl.length-1]!=="/"){negotiateUrl+="/"}negotiateUrl+="negotiate";negotiateUrl+=index===-1?"":url.substring(index);return negotiateUrl};return HttpConnection}();exports.HttpConnection=HttpConnection});unwrapExports(HttpConnection_1);var HttpConnection_2=HttpConnection_1.HttpConnection;var Base64EncodedHubProtocol_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var Base64EncodedHubProtocol=function(){function Base64EncodedHubProtocol(protocol){this.wrappedProtocol=protocol;this.name=this.wrappedProtocol.name;this.type=1}Base64EncodedHubProtocol.prototype.parseMessages=function(input){var pos=input.indexOf(":");if(pos===-1||input[input.length-1]!==";"){throw new Error("Invalid payload.")}var lenStr=input.substring(0,pos);if(!/^[0-9]+$/.test(lenStr)){throw new Error("Invalid length: '"+lenStr+"'")}var messageSize=parseInt(lenStr,10);if(messageSize!==input.length-pos-2){throw new Error("Invalid message size.")}var encodedMessage=input.substring(pos+1,input.length-1);var s=atob(encodedMessage);var payload=new Uint8Array(s.length);for(var i=0;i-1){this.subject.observers.splice(index,1)}if(this.subject.observers.length===0){this.subject.cancelCallback().catch(function(_){})}};return Subscription}();exports.Subscription=Subscription;var Subject=function(){function Subject(cancelCallback){this.observers=[];this.cancelCallback=cancelCallback}Subject.prototype.next=function(item){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];observer.next(item)}};Subject.prototype.error=function(err){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];if(observer.error){observer.error(err)}}};Subject.prototype.complete=function(){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];if(observer.complete){observer.complete()}}};Subject.prototype.subscribe=function(observer){this.observers.push(observer);return new Subscription(this,observer)};return Subject}();exports.Subject=Subject});unwrapExports(Observable);var Observable_1=Observable.Subscription;var Observable_2=Observable.Subject;var HubConnection_1=createCommonjsModule(function(module,exports){var __awaiter=commonjsGlobal&&commonjsGlobal.__awaiter||function(thisArg,_arguments,P,generator){return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=commonjsGlobal&&commonjsGlobal.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=y[op[0]&2?"return":op[0]?"throw":"next"])&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[0,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]=200&&xhr.status<300){resolve(new HttpResponse(xhr.status,xhr.statusText,xhr.response||xhr.responseText))}else{reject(new Errors.HttpError(xhr.statusText,xhr.status))}};xhr.onerror=function(){_this.logger.log(ILogger.LogLevel.Warning,"Error from HTTP request. "+xhr.status+": "+xhr.statusText);reject(new Errors.HttpError(xhr.statusText,xhr.status))};xhr.ontimeout=function(){_this.logger.log(ILogger.LogLevel.Warning,"Timeout from HTTP request.");reject(new Errors.TimeoutError)};xhr.send(request.content||"")})};return DefaultHttpClient}(HttpClient);exports.DefaultHttpClient=DefaultHttpClient});unwrapExports(HttpClient_1);var HttpClient_2=HttpClient_1.HttpResponse;var HttpClient_3=HttpClient_1.HttpClient;var HttpClient_4=HttpClient_1.DefaultHttpClient;var TextMessageFormat_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var TextMessageFormat=function(){function TextMessageFormat(){}TextMessageFormat.write=function(output){return""+output+TextMessageFormat.RecordSeparator};TextMessageFormat.parse=function(input){if(input[input.length-1]!==TextMessageFormat.RecordSeparator){throw new Error("Message is incomplete.")}var messages=input.split(TextMessageFormat.RecordSeparator);messages.pop();return messages};TextMessageFormat.RecordSeparatorCode=30;TextMessageFormat.RecordSeparator=String.fromCharCode(TextMessageFormat.RecordSeparatorCode);return TextMessageFormat}();exports.TextMessageFormat=TextMessageFormat});unwrapExports(TextMessageFormat_1);var TextMessageFormat_2=TextMessageFormat_1.TextMessageFormat;var HandshakeProtocol_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var HandshakeProtocol=function(){function HandshakeProtocol(){}HandshakeProtocol.prototype.writeHandshakeRequest=function(handshakeRequest){return TextMessageFormat_1.TextMessageFormat.write(JSON.stringify(handshakeRequest))};HandshakeProtocol.prototype.parseHandshakeResponse=function(data){var responseMessage;var messageData;var remainingData;if(data instanceof ArrayBuffer){var binaryData=new Uint8Array(data);var separatorIndex=binaryData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparatorCode);if(separatorIndex===-1){throw new Error("Message is incomplete.")}var responseLength=separatorIndex+1;messageData=String.fromCharCode.apply(null,binaryData.slice(0,responseLength));remainingData=binaryData.byteLength>responseLength?binaryData.slice(responseLength).buffer:null}else{var textData=data;var separatorIndex=textData.indexOf(TextMessageFormat_1.TextMessageFormat.RecordSeparator);if(separatorIndex===-1){throw new Error("Message is incomplete.")}var responseLength=separatorIndex+1;messageData=textData.substring(0,responseLength);remainingData=textData.length>responseLength?textData.substring(responseLength):null}var messages=TextMessageFormat_1.TextMessageFormat.parse(messageData);responseMessage=JSON.parse(messages[0]);return[remainingData,responseMessage]};return HandshakeProtocol}();exports.HandshakeProtocol=HandshakeProtocol});unwrapExports(HandshakeProtocol_1);var HandshakeProtocol_2=HandshakeProtocol_1.HandshakeProtocol;var IHubProtocol=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var MessageType;(function(MessageType){MessageType[MessageType["Invocation"]=1]="Invocation";MessageType[MessageType["StreamItem"]=2]="StreamItem";MessageType[MessageType["Completion"]=3]="Completion";MessageType[MessageType["StreamInvocation"]=4]="StreamInvocation";MessageType[MessageType["CancelInvocation"]=5]="CancelInvocation";MessageType[MessageType["Ping"]=6]="Ping";MessageType[MessageType["Close"]=7]="Close"})(MessageType=exports.MessageType||(exports.MessageType={}))});unwrapExports(IHubProtocol);var IHubProtocol_1=IHubProtocol.MessageType;var Loggers=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var NullLogger=function(){function NullLogger(){}NullLogger.prototype.log=function(logLevel,message){};NullLogger.instance=new NullLogger;return NullLogger}();exports.NullLogger=NullLogger});unwrapExports(Loggers);var Loggers_1=Loggers.NullLogger;var Utils=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var Arg=function(){function Arg(){}Arg.isRequired=function(val,name){if(val===null||val===undefined){throw new Error("The '"+name+"' argument is required.")}};Arg.isIn=function(val,values,name){if(!(val in values)){throw new Error("Unknown "+name+" value: "+val+".")}};return Arg}();exports.Arg=Arg;function getDataDetail(data,includeContent){var length=null;if(data instanceof ArrayBuffer){length="Binary data of length "+data.byteLength;if(includeContent){length+=". Content: '"+formatArrayBuffer(data)+"'"}}else if(typeof data==="string"){length="String data of length "+data.length;if(includeContent){length+=". Content: '"+data+"'."}}return length}exports.getDataDetail=getDataDetail;function formatArrayBuffer(data){var view=new Uint8Array(data);var str="";view.forEach(function(num){var pad=num<16?"0":"";str+="0x"+pad+num.toString(16)+" "});return str.substr(0,str.length-1)}exports.formatArrayBuffer=formatArrayBuffer;function sendMessage(logger,transportName,httpClient,url,accessTokenFactory,content,logMessageContent){return tslib_1.__awaiter(this,void 0,void 0,function(){var headers,token,response,_a;return tslib_1.__generator(this,function(_b){switch(_b.label){case 0:return[4,accessTokenFactory()];case 1:token=_b.sent();if(token){headers=(_a={},_a["Authorization"]="Bearer "+token,_a)}logger.log(ILogger.LogLevel.Trace,"("+transportName+" transport) sending data. "+getDataDetail(content,logMessageContent)+".");return[4,httpClient.post(url,{content:content,headers:headers})];case 2:response=_b.sent();logger.log(ILogger.LogLevel.Trace,"("+transportName+" transport) request complete. Response status: "+response.statusCode+".");return[2]}})})}exports.sendMessage=sendMessage;function createLogger(logger){if(logger===undefined){return new ConsoleLogger(ILogger.LogLevel.Information)}if(logger===null){return Loggers.NullLogger.instance}if(logger.log){return logger}return new ConsoleLogger(logger)}exports.createLogger=createLogger;var Subject=function(){function Subject(cancelCallback){this.observers=[];this.cancelCallback=cancelCallback}Subject.prototype.next=function(item){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];observer.next(item)}};Subject.prototype.error=function(err){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];if(observer.error){observer.error(err)}}};Subject.prototype.complete=function(){for(var _i=0,_a=this.observers;_i<_a.length;_i++){var observer=_a[_i];if(observer.complete){observer.complete()}}};Subject.prototype.subscribe=function(observer){this.observers.push(observer);return new SubjectSubscription(this,observer)};return Subject}();exports.Subject=Subject;var SubjectSubscription=function(){function SubjectSubscription(subject,observer){this.subject=subject;this.observer=observer}SubjectSubscription.prototype.dispose=function(){var index=this.subject.observers.indexOf(this.observer);if(index>-1){this.subject.observers.splice(index,1)}if(this.subject.observers.length===0){this.subject.cancelCallback().catch(function(_){})}};return SubjectSubscription}();exports.SubjectSubscription=SubjectSubscription;var ConsoleLogger=function(){function ConsoleLogger(minimumLogLevel){this.minimumLogLevel=minimumLogLevel}ConsoleLogger.prototype.log=function(logLevel,message){if(logLevel>=this.minimumLogLevel){switch(logLevel){case ILogger.LogLevel.Critical:case ILogger.LogLevel.Error:console.error(ILogger.LogLevel[logLevel]+": "+message);break;case ILogger.LogLevel.Warning:console.warn(ILogger.LogLevel[logLevel]+": "+message);break;case ILogger.LogLevel.Information:console.info(ILogger.LogLevel[logLevel]+": "+message);break;default:console.log(ILogger.LogLevel[logLevel]+": "+message);break}}};return ConsoleLogger}();exports.ConsoleLogger=ConsoleLogger});unwrapExports(Utils);var Utils_1=Utils.Arg;var Utils_2=Utils.getDataDetail;var Utils_3=Utils.formatArrayBuffer;var Utils_4=Utils.sendMessage;var Utils_5=Utils.createLogger;var Utils_6=Utils.Subject;var Utils_7=Utils.SubjectSubscription;var Utils_8=Utils.ConsoleLogger;var HubConnection_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var DEFAULT_TIMEOUT_IN_MS=30*1e3;var HubConnection=function(){function HubConnection(connection,logger,protocol){var _this=this;Utils.Arg.isRequired(connection,"connection");Utils.Arg.isRequired(logger,"logger");Utils.Arg.isRequired(protocol,"protocol");this.serverTimeoutInMilliseconds=DEFAULT_TIMEOUT_IN_MS;this.logger=logger;this.protocol=protocol;this.connection=connection;this.handshakeProtocol=new HandshakeProtocol_1.HandshakeProtocol;this.connection.onreceive=function(data){return _this.processIncomingData(data)};this.connection.onclose=function(error){return _this.connectionClosed(error)};this.callbacks={};this.methods={};this.closedCallbacks=[];this.id=0}HubConnection.create=function(connection,logger,protocol){return new HubConnection(connection,logger,protocol)};HubConnection.prototype.start=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var handshakeRequest;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:handshakeRequest={protocol:this.protocol.name,version:this.protocol.version};this.logger.log(ILogger.LogLevel.Debug,"Starting HubConnection.");this.receivedHandshakeResponse=false;return[4,this.connection.start(this.protocol.transferFormat)];case 1:_a.sent();this.logger.log(ILogger.LogLevel.Debug,"Sending handshake request.");return[4,this.connection.send(this.handshakeProtocol.writeHandshakeRequest(handshakeRequest))];case 2:_a.sent();this.logger.log(ILogger.LogLevel.Information,"Using HubProtocol '"+this.protocol.name+"'.");this.cleanupTimeout();this.configureTimeout();return[2]}})})};HubConnection.prototype.stop=function(){this.logger.log(ILogger.LogLevel.Debug,"Stopping HubConnection.");this.cleanupTimeout();return this.connection.stop()};HubConnection.prototype.stream=function(methodName){var _this=this;var args=[];for(var _i=1;_i"));this.onclose(closeError)}this.logger.log(ILogger.LogLevel.Trace,"(LongPolling transport) Transport finished.");return[7];case 9:return[2]}})})};LongPollingTransport.prototype.send=function(data){return tslib_1.__awaiter(this,void 0,void 0,function(){return tslib_1.__generator(this,function(_a){if(!this.running){return[2,Promise.reject(new Error("Cannot send until the transport is connected"))]}return[2,Utils.sendMessage(this.logger,"LongPolling",this.httpClient,this.url,this.accessTokenFactory,data,this.logMessageContent)]})})};LongPollingTransport.prototype.stop=function(){return tslib_1.__awaiter(this,void 0,void 0,function(){var _this=this;var deleteOptions,token,response;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:_a.trys.push([0,,3,4]);this.running=false;this.logger.log(ILogger.LogLevel.Trace,"(LongPolling transport) sending DELETE request to "+this.url+".");deleteOptions={headers:{}};return[4,this.accessTokenFactory()];case 1:token=_a.sent();this.updateHeaderToken(deleteOptions,token);return[4,this.httpClient.delete(this.url,deleteOptions)];case 2:response=_a.sent();this.logger.log(ILogger.LogLevel.Trace,"(LongPolling transport) DELETE request accepted.");return[3,4];case 3:if(!this.stopped){this.shutdownTimer=setTimeout(function(){_this.logger.log(ILogger.LogLevel.Warning,"(LongPolling transport) server did not terminate after DELETE request, canceling poll.");_this.pollAbort.abort()},this.shutdownTimeout)}return[7];case 4:return[2]}})})};return LongPollingTransport}();exports.LongPollingTransport=LongPollingTransport});unwrapExports(LongPollingTransport_1);var LongPollingTransport_2=LongPollingTransport_1.LongPollingTransport;var ServerSentEventsTransport_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var ServerSentEventsTransport=function(){function ServerSentEventsTransport(httpClient,accessTokenFactory,logger,logMessageContent){this.httpClient=httpClient;this.accessTokenFactory=accessTokenFactory||function(){return null};this.logger=logger;this.logMessageContent=logMessageContent}ServerSentEventsTransport.prototype.connect=function(url,transferFormat){return tslib_1.__awaiter(this,void 0,void 0,function(){var _this=this;var token;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:Utils.Arg.isRequired(url,"url");Utils.Arg.isRequired(transferFormat,"transferFormat");Utils.Arg.isIn(transferFormat,ITransport.TransferFormat,"transferFormat");if(typeof EventSource==="undefined"){throw new Error("'EventSource' is not supported in your environment.")}this.logger.log(ILogger.LogLevel.Trace,"(SSE transport) Connecting");return[4,this.accessTokenFactory()];case 1:token=_a.sent();if(token){url+=(url.indexOf("?")<0?"?":"&")+("access_token="+encodeURIComponent(token))}this.url=url;return[2,new Promise(function(resolve,reject){var opened=false;if(transferFormat!==ITransport.TransferFormat.Text){reject(new Error("The Server-Sent Events transport only supports the 'Text' transfer format"))}var eventSource=new EventSource(url,{withCredentials:true});try{eventSource.onmessage=function(e){if(_this.onreceive){try{_this.logger.log(ILogger.LogLevel.Trace,"(SSE transport) data received. "+Utils.getDataDetail(e.data,_this.logMessageContent)+".");_this.onreceive(e.data)}catch(error){if(_this.onclose){_this.onclose(error)}return}}};eventSource.onerror=function(e){var error=new Error(e.message||"Error occurred");if(opened){_this.close(error)}else{reject(error)}};eventSource.onopen=function(){_this.logger.log(ILogger.LogLevel.Information,"SSE connected to "+_this.url);_this.eventSource=eventSource;opened=true;resolve()}}catch(e){return Promise.reject(e)}})]}})})};ServerSentEventsTransport.prototype.send=function(data){return tslib_1.__awaiter(this,void 0,void 0,function(){return tslib_1.__generator(this,function(_a){if(!this.eventSource){return[2,Promise.reject(new Error("Cannot send until the transport is connected"))]}return[2,Utils.sendMessage(this.logger,"SSE",this.httpClient,this.url,this.accessTokenFactory,data,this.logMessageContent)]})})};ServerSentEventsTransport.prototype.stop=function(){this.close();return Promise.resolve()};ServerSentEventsTransport.prototype.close=function(e){if(this.eventSource){this.eventSource.close();this.eventSource=null;if(this.onclose){this.onclose(e)}}};return ServerSentEventsTransport}();exports.ServerSentEventsTransport=ServerSentEventsTransport});unwrapExports(ServerSentEventsTransport_1);var ServerSentEventsTransport_2=ServerSentEventsTransport_1.ServerSentEventsTransport;var WebSocketTransport_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var WebSocketTransport=function(){function WebSocketTransport(accessTokenFactory,logger,logMessageContent){this.logger=logger;this.accessTokenFactory=accessTokenFactory||function(){return null};this.logMessageContent=logMessageContent}WebSocketTransport.prototype.connect=function(url,transferFormat){return tslib_1.__awaiter(this,void 0,void 0,function(){var _this=this;var token;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:Utils.Arg.isRequired(url,"url");Utils.Arg.isRequired(transferFormat,"transferFormat");Utils.Arg.isIn(transferFormat,ITransport.TransferFormat,"transferFormat");if(typeof WebSocket==="undefined"){throw new Error("'WebSocket' is not supported in your environment.")}this.logger.log(ILogger.LogLevel.Trace,"(WebSockets transport) Connecting");return[4,this.accessTokenFactory()];case 1:token=_a.sent();if(token){url+=(url.indexOf("?")<0?"?":"&")+("access_token="+encodeURIComponent(token))}return[2,new Promise(function(resolve,reject){url=url.replace(/^http/,"ws");var webSocket=new WebSocket(url);if(transferFormat===ITransport.TransferFormat.Binary){webSocket.binaryType="arraybuffer"}webSocket.onopen=function(event){_this.logger.log(ILogger.LogLevel.Information,"WebSocket connected to "+url);_this.webSocket=webSocket;resolve()};webSocket.onerror=function(event){reject(event.error)};webSocket.onmessage=function(message){_this.logger.log(ILogger.LogLevel.Trace,"(WebSockets transport) data received. "+Utils.getDataDetail(message.data,_this.logMessageContent)+".");if(_this.onreceive){_this.onreceive(message.data)}};webSocket.onclose=function(event){_this.logger.log(ILogger.LogLevel.Trace,"(WebSockets transport) socket closed.");if(_this.onclose){if(event.wasClean===false||event.code!==1e3){_this.onclose(new Error("Websocket closed with status code: "+event.code+" ("+event.reason+")"))}else{_this.onclose()}}}})]}})})};WebSocketTransport.prototype.send=function(data){if(this.webSocket&&this.webSocket.readyState===WebSocket.OPEN){this.logger.log(ILogger.LogLevel.Trace,"(WebSockets transport) sending data. "+Utils.getDataDetail(data,this.logMessageContent)+".");this.webSocket.send(data);return Promise.resolve()}return Promise.reject("WebSocket is not in the OPEN state")};WebSocketTransport.prototype.stop=function(){if(this.webSocket){this.webSocket.close();this.webSocket=null}return Promise.resolve()};return WebSocketTransport}();exports.WebSocketTransport=WebSocketTransport});unwrapExports(WebSocketTransport_1);var WebSocketTransport_2=WebSocketTransport_1.WebSocketTransport;var HttpConnection_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var MAX_REDIRECTS=100;var HttpConnection=function(){function HttpConnection(url,options){if(options===void 0){options={}}this.features={};Utils.Arg.isRequired(url,"url");this.logger=Utils.createLogger(options.logger);this.baseUrl=this.resolveUrl(url);options=options||{};options.accessTokenFactory=options.accessTokenFactory||function(){return null};options.logMessageContent=options.logMessageContent||false;this.httpClient=options.httpClient||new HttpClient_1.DefaultHttpClient(this.logger);this.connectionState=2;this.options=options}HttpConnection.prototype.start=function(transferFormat){transferFormat=transferFormat||ITransport.TransferFormat.Binary;Utils.Arg.isIn(transferFormat,ITransport.TransferFormat,"transferFormat");this.logger.log(ILogger.LogLevel.Debug,"Starting connection with transfer format '"+ITransport.TransferFormat[transferFormat]+"'.");if(this.connectionState!==2){return Promise.reject(new Error("Cannot start a connection that is not in the 'Disconnected' state."))}this.connectionState=0;this.startPromise=this.startInternal(transferFormat);return this.startPromise};HttpConnection.prototype.send=function(data){if(this.connectionState!==1){throw new Error("Cannot send data if the connection is not in the 'Connected' State.")}return this.transport.send(data)};HttpConnection.prototype.stop=function(error){return tslib_1.__awaiter(this,void 0,void 0,function(){var e_1;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:this.connectionState=2;_a.label=1;case 1:_a.trys.push([1,3,,4]);return[4,this.startPromise];case 2:_a.sent();return[3,4];case 3:e_1=_a.sent();return[3,4];case 4:if(!this.transport)return[3,6];this.stopError=error;return[4,this.transport.stop()];case 5:_a.sent();this.transport=null;_a.label=6;case 6:return[2]}})})};HttpConnection.prototype.startInternal=function(transferFormat){return tslib_1.__awaiter(this,void 0,void 0,function(){var _this=this;var url,negotiateResponse,redirects,_loop_1,this_1,state_1,e_2;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:url=this.baseUrl;this.accessTokenFactory=this.options.accessTokenFactory;_a.label=1;case 1:_a.trys.push([1,12,,13]);if(!this.options.skipNegotiation)return[3,5];if(!(this.options.transport===ITransport.HttpTransportType.WebSockets))return[3,3];this.transport=this.constructTransport(ITransport.HttpTransportType.WebSockets);return[4,this.transport.connect(url,transferFormat)];case 2:_a.sent();return[3,4];case 3:throw Error("Negotiation can only be skipped when using the WebSocket transport directly.");case 4:return[3,11];case 5:negotiateResponse=null;redirects=0;_loop_1=function(){var accessToken_1;return tslib_1.__generator(this,function(_a){switch(_a.label){case 0:return[4,this_1.getNegotiationResponse(url)];case 1:negotiateResponse=_a.sent();if(this_1.connectionState===2){return[2,{value:void 0}]}if(negotiateResponse.url){url=negotiateResponse.url}if(negotiateResponse.accessToken){accessToken_1=negotiateResponse.accessToken;this_1.accessTokenFactory=function(){return accessToken_1}}redirects++;return[2]}})};this_1=this;_a.label=6;case 6:return[5,_loop_1()];case 7:state_1=_a.sent();if(typeof state_1==="object")return[2,state_1.value];_a.label=8;case 8:if(negotiateResponse.url&&redirects=0){if(transport===ITransport.HttpTransportType.WebSockets&&typeof WebSocket==="undefined"||transport===ITransport.HttpTransportType.ServerSentEvents&&typeof EventSource==="undefined"){this.logger.log(ILogger.LogLevel.Debug,"Skipping transport '"+ITransport.HttpTransportType[transport]+"' because it is not supported in your environment.'")}else{this.logger.log(ILogger.LogLevel.Debug,"Selecting transport '"+ITransport.HttpTransportType[transport]+"'");return transport}}else{this.logger.log(ILogger.LogLevel.Debug,"Skipping transport '"+ITransport.HttpTransportType[transport]+"' because it does not support the requested transfer format '"+ITransport.TransferFormat[requestedTransferFormat]+"'.")}}else{this.logger.log(ILogger.LogLevel.Debug,"Skipping transport '"+ITransport.HttpTransportType[transport]+"' because it was disabled by the client.")}}return null};HttpConnection.prototype.isITransport=function(transport){return transport&&typeof transport==="object"&&"connect"in transport};HttpConnection.prototype.changeState=function(from,to){if(this.connectionState===from){this.connectionState=to;return true}return false};HttpConnection.prototype.stopConnection=function(error){return tslib_1.__awaiter(this,void 0,void 0,function(){return tslib_1.__generator(this,function(_a){this.transport=null;error=this.stopError||error;if(error){this.logger.log(ILogger.LogLevel.Error,"Connection disconnected with error '"+error+"'.")}else{this.logger.log(ILogger.LogLevel.Information,"Connection disconnected.")}this.connectionState=2;if(this.onclose){this.onclose(error)}return[2]})})};HttpConnection.prototype.resolveUrl=function(url){if(url.lastIndexOf("https://",0)===0||url.lastIndexOf("http://",0)===0){return url}if(typeof window==="undefined"||!window||!window.document){throw new Error("Cannot resolve '"+url+"'.")}var aTag=window.document.createElement("a");aTag.href=url;this.logger.log(ILogger.LogLevel.Information,"Normalizing '"+url+"' to '"+aTag.href+"'.");return aTag.href};HttpConnection.prototype.resolveNegotiateUrl=function(url){var index=url.indexOf("?");var negotiateUrl=url.substring(0,index===-1?url.length:index);if(negotiateUrl[negotiateUrl.length-1]!=="/"){negotiateUrl+="/"}negotiateUrl+="negotiate";negotiateUrl+=index===-1?"":url.substring(index);return negotiateUrl};return HttpConnection}();exports.HttpConnection=HttpConnection;function transportMatches(requestedTransport,actualTransport){return!requestedTransport||(actualTransport&requestedTransport)!==0}});unwrapExports(HttpConnection_1);var HttpConnection_2=HttpConnection_1.HttpConnection;var JsonHubProtocol_1=createCommonjsModule(function(module,exports){Object.defineProperty(exports,"__esModule",{value:true});var JSON_HUB_PROTOCOL_NAME="json";var JsonHubProtocol=function(){function JsonHubProtocol(){this.name=JSON_HUB_PROTOCOL_NAME;this.version=1;this.transferFormat=ITransport.TransferFormat.Text}JsonHubProtocol.prototype.parseMessages=function(input,logger){if(typeof input!=="string"){throw new Error("Invalid input for JSON hub protocol. Expected a string.")}if(!input){return[]}if(logger===null){logger=Loggers.NullLogger.instance}var messages=TextMessageFormat_1.TextMessageFormat.parse(input);var hubMessages=[];for(var _i=0,messages_1=messages;_i= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\r\n return c > 3 && r && Object.defineProperty(target, key, r), r;\r\n}\r\n\r\nexport function __param(paramIndex, decorator) {\r\n return function (target, key) { decorator(target, key, paramIndex); }\r\n}\r\n\r\nexport function __metadata(metadataKey, metadataValue) {\r\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(metadataKey, metadataValue);\r\n}\r\n\r\nexport function __awaiter(thisArg, _arguments, P, generator) {\r\n return new (P || (P = Promise))(function (resolve, reject) {\r\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\r\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\r\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\r\n step((generator = generator.apply(thisArg, _arguments || [])).next());\r\n });\r\n}\r\n\r\nexport function __generator(thisArg, body) {\r\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\r\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\r\n function verb(n) { return function (v) { return step([n, v]); }; }\r\n function step(op) {\r\n if (f) throw new TypeError(\"Generator is already executing.\");\r\n while (_) try {\r\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\r\n if (y = 0, t) op = [0, t.value];\r\n switch (op[0]) {\r\n case 0: case 1: t = op; break;\r\n case 4: _.label++; return { value: op[1], done: false };\r\n case 5: _.label++; y = op[1]; op = [0]; continue;\r\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\r\n default:\r\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\r\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\r\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\r\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\r\n if (t[2]) _.ops.pop();\r\n _.trys.pop(); continue;\r\n }\r\n op = body.call(thisArg, _);\r\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\r\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\r\n }\r\n}\r\n\r\nexport function __exportStar(m, exports) {\r\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\r\n}\r\n\r\nexport function __values(o) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator], i = 0;\r\n if (m) return m.call(o);\r\n return {\r\n next: function () {\r\n if (o && i >= o.length) o = void 0;\r\n return { value: o && o[i++], done: !o };\r\n }\r\n };\r\n}\r\n\r\nexport function __read(o, n) {\r\n var m = typeof Symbol === \"function\" && o[Symbol.iterator];\r\n if (!m) return o;\r\n var i = m.call(o), r, ar = [], e;\r\n try {\r\n while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);\r\n }\r\n catch (error) { e = { error: error }; }\r\n finally {\r\n try {\r\n if (r && !r.done && (m = i[\"return\"])) m.call(i);\r\n }\r\n finally { if (e) throw e.error; }\r\n }\r\n return ar;\r\n}\r\n\r\nexport function __spread() {\r\n for (var ar = [], i = 0; i < arguments.length; i++)\r\n ar = ar.concat(__read(arguments[i]));\r\n return ar;\r\n}\r\n\r\nexport function __await(v) {\r\n return this instanceof __await ? (this.v = v, this) : new __await(v);\r\n}\r\n\r\nexport function __asyncGenerator(thisArg, _arguments, generator) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var g = generator.apply(thisArg, _arguments || []), i, q = [];\r\n return i = {}, verb(\"next\"), verb(\"throw\"), verb(\"return\"), i[Symbol.asyncIterator] = function () { return this; }, i;\r\n function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }\r\n function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }\r\n function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }\r\n function fulfill(value) { resume(\"next\", value); }\r\n function reject(value) { resume(\"throw\", value); }\r\n function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }\r\n}\r\n\r\nexport function __asyncDelegator(o) {\r\n var i, p;\r\n return i = {}, verb(\"next\"), verb(\"throw\", function (e) { throw e; }), verb(\"return\"), i[Symbol.iterator] = function () { return this; }, i;\r\n function verb(n, f) { if (o[n]) i[n] = function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === \"return\" } : f ? f(v) : v; }; }\r\n}\r\n\r\nexport function __asyncValues(o) {\r\n if (!Symbol.asyncIterator) throw new TypeError(\"Symbol.asyncIterator is not defined.\");\r\n var m = o[Symbol.asyncIterator];\r\n return m ? m.call(o) : typeof __values === \"function\" ? __values(o) : o[Symbol.iterator]();\r\n}\r\n\r\nexport function __makeTemplateObject(cooked, raw) {\r\n if (Object.defineProperty) { Object.defineProperty(cooked, \"raw\", { value: raw }); } else { cooked.raw = raw; }\r\n return cooked;\r\n};\r\n\r\nexport function __importStar(mod) {\r\n if (mod && mod.__esModule) return mod;\r\n var result = {};\r\n if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];\r\n result.default = mod;\r\n return result;\r\n}\r\n\r\nexport function __importDefault(mod) {\r\n return (mod && mod.__esModule) ? mod : { default: mod };\r\n}\r\n","export function objectOrFunction(x) {\n var type = typeof x;\n return x !== null && (type === 'object' || type === 'function');\n}\n\nexport function isFunction(x) {\n return typeof x === 'function';\n}\n\nexport function isMaybeThenable(x) {\n return x !== null && typeof x === 'object';\n}\n\nvar _isArray = void 0;\nif (Array.isArray) {\n _isArray = Array.isArray;\n} else {\n _isArray = function (x) {\n return Object.prototype.toString.call(x) === '[object Array]';\n };\n}\n\nexport var isArray = _isArray;","var len = 0;\nvar vertxNext = void 0;\nvar customSchedulerFn = void 0;\n\nexport var asap = function asap(callback, arg) {\n queue[len] = callback;\n queue[len + 1] = arg;\n len += 2;\n if (len === 2) {\n // If len is 2, that means that we need to schedule an async flush.\n // If additional callbacks are queued before the queue is flushed, they\n // will be processed by this flush that we are scheduling.\n if (customSchedulerFn) {\n customSchedulerFn(flush);\n } else {\n scheduleFlush();\n }\n }\n};\n\nexport function setScheduler(scheduleFn) {\n customSchedulerFn = scheduleFn;\n}\n\nexport function setAsap(asapFn) {\n asap = asapFn;\n}\n\nvar browserWindow = typeof window !== 'undefined' ? window : undefined;\nvar browserGlobal = browserWindow || {};\nvar BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;\nvar isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';\n\n// test for web worker but not in IE10\nvar isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined';\n\n// node\nfunction useNextTick() {\n // node version 0.10.x displays a deprecation warning when nextTick is used recursively\n // see https://github.com/cujojs/when/issues/410 for details\n return function () {\n return process.nextTick(flush);\n };\n}\n\n// vertx\nfunction useVertxTimer() {\n if (typeof vertxNext !== 'undefined') {\n return function () {\n vertxNext(flush);\n };\n }\n\n return useSetTimeout();\n}\n\nfunction useMutationObserver() {\n var iterations = 0;\n var observer = new BrowserMutationObserver(flush);\n var node = document.createTextNode('');\n observer.observe(node, { characterData: true });\n\n return function () {\n node.data = iterations = ++iterations % 2;\n };\n}\n\n// web worker\nfunction useMessageChannel() {\n var channel = new MessageChannel();\n channel.port1.onmessage = flush;\n return function () {\n return channel.port2.postMessage(0);\n };\n}\n\nfunction useSetTimeout() {\n // Store setTimeout reference so es6-promise will be unaffected by\n // other code modifying setTimeout (like sinon.useFakeTimers())\n var globalSetTimeout = setTimeout;\n return function () {\n return globalSetTimeout(flush, 1);\n };\n}\n\nvar queue = new Array(1000);\nfunction flush() {\n for (var i = 0; i < len; i += 2) {\n var callback = queue[i];\n var arg = queue[i + 1];\n\n callback(arg);\n\n queue[i] = undefined;\n queue[i + 1] = undefined;\n }\n\n len = 0;\n}\n\nfunction attemptVertx() {\n try {\n var r = require;\n var vertx = r('vertx');\n vertxNext = vertx.runOnLoop || vertx.runOnContext;\n return useVertxTimer();\n } catch (e) {\n return useSetTimeout();\n }\n}\n\nvar scheduleFlush = void 0;\n// Decide what async method to use to triggering processing of queued callbacks:\nif (isNode) {\n scheduleFlush = useNextTick();\n} else if (BrowserMutationObserver) {\n scheduleFlush = useMutationObserver();\n} else if (isWorker) {\n scheduleFlush = useMessageChannel();\n} else if (browserWindow === undefined && typeof require === 'function') {\n scheduleFlush = attemptVertx();\n} else {\n scheduleFlush = useSetTimeout();\n}","import { invokeCallback, subscribe, FULFILLED, REJECTED, noop, makePromise, PROMISE_ID } from './-internal';\n\nimport { asap } from './asap';\n\nexport default function then(onFulfillment, onRejection) {\n var parent = this;\n\n var child = new this.constructor(noop);\n\n if (child[PROMISE_ID] === undefined) {\n makePromise(child);\n }\n\n var _state = parent._state;\n\n\n if (_state) {\n var callback = arguments[_state - 1];\n asap(function () {\n return invokeCallback(_state, child, callback, parent._result);\n });\n } else {\n subscribe(parent, child, onFulfillment, onRejection);\n }\n\n return child;\n}","import { noop, resolve as _resolve } from '../-internal';\n\n/**\n `Promise.resolve` returns a promise that will become resolved with the\n passed `value`. It is shorthand for the following:\n\n ```javascript\n let promise = new Promise(function(resolve, reject){\n resolve(1);\n });\n\n promise.then(function(value){\n // value === 1\n });\n ```\n\n Instead of writing the above, your code now simply becomes the following:\n\n ```javascript\n let promise = Promise.resolve(1);\n\n promise.then(function(value){\n // value === 1\n });\n ```\n\n @method resolve\n @static\n @param {Any} value value that the returned promise will be resolved with\n Useful for tooling.\n @return {Promise} a promise that will become fulfilled with the given\n `value`\n*/\nexport default function resolve(object) {\n /*jshint validthis:true */\n var Constructor = this;\n\n if (object && typeof object === 'object' && object.constructor === Constructor) {\n return object;\n }\n\n var promise = new Constructor(noop);\n _resolve(promise, object);\n return promise;\n}","import { objectOrFunction, isFunction } from './utils';\n\nimport { asap } from './asap';\n\nimport originalThen from './then';\nimport originalResolve from './promise/resolve';\n\nexport var PROMISE_ID = Math.random().toString(36).substring(16);\n\nfunction noop() {}\n\nvar PENDING = void 0;\nvar FULFILLED = 1;\nvar REJECTED = 2;\n\nvar GET_THEN_ERROR = new ErrorObject();\n\nfunction selfFulfillment() {\n return new TypeError(\"You cannot resolve a promise with itself\");\n}\n\nfunction cannotReturnOwn() {\n return new TypeError('A promises callback cannot return that same promise.');\n}\n\nfunction getThen(promise) {\n try {\n return promise.then;\n } catch (error) {\n GET_THEN_ERROR.error = error;\n return GET_THEN_ERROR;\n }\n}\n\nfunction tryThen(then, value, fulfillmentHandler, rejectionHandler) {\n try {\n then.call(value, fulfillmentHandler, rejectionHandler);\n } catch (e) {\n return e;\n }\n}\n\nfunction handleForeignThenable(promise, thenable, then) {\n asap(function (promise) {\n var sealed = false;\n var error = tryThen(then, thenable, function (value) {\n if (sealed) {\n return;\n }\n sealed = true;\n if (thenable !== value) {\n resolve(promise, value);\n } else {\n fulfill(promise, value);\n }\n }, function (reason) {\n if (sealed) {\n return;\n }\n sealed = true;\n\n reject(promise, reason);\n }, 'Settle: ' + (promise._label || ' unknown promise'));\n\n if (!sealed && error) {\n sealed = true;\n reject(promise, error);\n }\n }, promise);\n}\n\nfunction handleOwnThenable(promise, thenable) {\n if (thenable._state === FULFILLED) {\n fulfill(promise, thenable._result);\n } else if (thenable._state === REJECTED) {\n reject(promise, thenable._result);\n } else {\n subscribe(thenable, undefined, function (value) {\n return resolve(promise, value);\n }, function (reason) {\n return reject(promise, reason);\n });\n }\n}\n\nfunction handleMaybeThenable(promise, maybeThenable, then) {\n if (maybeThenable.constructor === promise.constructor && then === originalThen && maybeThenable.constructor.resolve === originalResolve) {\n handleOwnThenable(promise, maybeThenable);\n } else {\n if (then === GET_THEN_ERROR) {\n reject(promise, GET_THEN_ERROR.error);\n GET_THEN_ERROR.error = null;\n } else if (then === undefined) {\n fulfill(promise, maybeThenable);\n } else if (isFunction(then)) {\n handleForeignThenable(promise, maybeThenable, then);\n } else {\n fulfill(promise, maybeThenable);\n }\n }\n}\n\nfunction resolve(promise, value) {\n if (promise === value) {\n reject(promise, selfFulfillment());\n } else if (objectOrFunction(value)) {\n handleMaybeThenable(promise, value, getThen(value));\n } else {\n fulfill(promise, value);\n }\n}\n\nfunction publishRejection(promise) {\n if (promise._onerror) {\n promise._onerror(promise._result);\n }\n\n publish(promise);\n}\n\nfunction fulfill(promise, value) {\n if (promise._state !== PENDING) {\n return;\n }\n\n promise._result = value;\n promise._state = FULFILLED;\n\n if (promise._subscribers.length !== 0) {\n asap(publish, promise);\n }\n}\n\nfunction reject(promise, reason) {\n if (promise._state !== PENDING) {\n return;\n }\n promise._state = REJECTED;\n promise._result = reason;\n\n asap(publishRejection, promise);\n}\n\nfunction subscribe(parent, child, onFulfillment, onRejection) {\n var _subscribers = parent._subscribers;\n var length = _subscribers.length;\n\n\n parent._onerror = null;\n\n _subscribers[length] = child;\n _subscribers[length + FULFILLED] = onFulfillment;\n _subscribers[length + REJECTED] = onRejection;\n\n if (length === 0 && parent._state) {\n asap(publish, parent);\n }\n}\n\nfunction publish(promise) {\n var subscribers = promise._subscribers;\n var settled = promise._state;\n\n if (subscribers.length === 0) {\n return;\n }\n\n var child = void 0,\n callback = void 0,\n detail = promise._result;\n\n for (var i = 0; i < subscribers.length; i += 3) {\n child = subscribers[i];\n callback = subscribers[i + settled];\n\n if (child) {\n invokeCallback(settled, child, callback, detail);\n } else {\n callback(detail);\n }\n }\n\n promise._subscribers.length = 0;\n}\n\nfunction ErrorObject() {\n this.error = null;\n}\n\nvar TRY_CATCH_ERROR = new ErrorObject();\n\nfunction tryCatch(callback, detail) {\n try {\n return callback(detail);\n } catch (e) {\n TRY_CATCH_ERROR.error = e;\n return TRY_CATCH_ERROR;\n }\n}\n\nfunction invokeCallback(settled, promise, callback, detail) {\n var hasCallback = isFunction(callback),\n value = void 0,\n error = void 0,\n succeeded = void 0,\n failed = void 0;\n\n if (hasCallback) {\n value = tryCatch(callback, detail);\n\n if (value === TRY_CATCH_ERROR) {\n failed = true;\n error = value.error;\n value.error = null;\n } else {\n succeeded = true;\n }\n\n if (promise === value) {\n reject(promise, cannotReturnOwn());\n return;\n }\n } else {\n value = detail;\n succeeded = true;\n }\n\n if (promise._state !== PENDING) {\n // noop\n } else if (hasCallback && succeeded) {\n resolve(promise, value);\n } else if (failed) {\n reject(promise, error);\n } else if (settled === FULFILLED) {\n fulfill(promise, value);\n } else if (settled === REJECTED) {\n reject(promise, value);\n }\n}\n\nfunction initializePromise(promise, resolver) {\n try {\n resolver(function resolvePromise(value) {\n resolve(promise, value);\n }, function rejectPromise(reason) {\n reject(promise, reason);\n });\n } catch (e) {\n reject(promise, e);\n }\n}\n\nvar id = 0;\nfunction nextId() {\n return id++;\n}\n\nfunction makePromise(promise) {\n promise[PROMISE_ID] = id++;\n promise._state = undefined;\n promise._result = undefined;\n promise._subscribers = [];\n}\n\nexport { nextId, makePromise, getThen, noop, resolve, reject, fulfill, subscribe, publish, publishRejection, initializePromise, invokeCallback, FULFILLED, REJECTED, PENDING, handleMaybeThenable };","function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nimport { isArray, isMaybeThenable } from './utils';\n\nimport { noop, reject, fulfill, subscribe, FULFILLED, REJECTED, PENDING, getThen, handleMaybeThenable } from './-internal';\n\nimport then from './then';\nimport Promise from './promise';\nimport originalResolve from './promise/resolve';\nimport originalThen from './then';\nimport { makePromise, PROMISE_ID } from './-internal';\n\nfunction validationError() {\n return new Error('Array Methods must be provided an Array');\n}\n\nfunction validationError() {\n return new Error('Array Methods must be provided an Array');\n};\n\nvar Enumerator = function () {\n function Enumerator(Constructor, input) {\n this._instanceConstructor = Constructor;\n this.promise = new Constructor(noop);\n\n if (!this.promise[PROMISE_ID]) {\n makePromise(this.promise);\n }\n\n if (isArray(input)) {\n this.length = input.length;\n this._remaining = input.length;\n\n this._result = new Array(this.length);\n\n if (this.length === 0) {\n fulfill(this.promise, this._result);\n } else {\n this.length = this.length || 0;\n this._enumerate(input);\n if (this._remaining === 0) {\n fulfill(this.promise, this._result);\n }\n }\n } else {\n reject(this.promise, validationError());\n }\n }\n\n Enumerator.prototype._enumerate = function _enumerate(input) {\n for (var i = 0; this._state === PENDING && i < input.length; i++) {\n this._eachEntry(input[i], i);\n }\n };\n\n Enumerator.prototype._eachEntry = function _eachEntry(entry, i) {\n var c = this._instanceConstructor;\n var resolve = c.resolve;\n\n\n if (resolve === originalResolve) {\n var _then = getThen(entry);\n\n if (_then === originalThen && entry._state !== PENDING) {\n this._settledAt(entry._state, i, entry._result);\n } else if (typeof _then !== 'function') {\n this._remaining--;\n this._result[i] = entry;\n } else if (c === Promise) {\n var promise = new c(noop);\n handleMaybeThenable(promise, entry, _then);\n this._willSettleAt(promise, i);\n } else {\n this._willSettleAt(new c(function (resolve) {\n return resolve(entry);\n }), i);\n }\n } else {\n this._willSettleAt(resolve(entry), i);\n }\n };\n\n Enumerator.prototype._settledAt = function _settledAt(state, i, value) {\n var promise = this.promise;\n\n\n if (promise._state === PENDING) {\n this._remaining--;\n\n if (state === REJECTED) {\n reject(promise, value);\n } else {\n this._result[i] = value;\n }\n }\n\n if (this._remaining === 0) {\n fulfill(promise, this._result);\n }\n };\n\n Enumerator.prototype._willSettleAt = function _willSettleAt(promise, i) {\n var enumerator = this;\n\n subscribe(promise, undefined, function (value) {\n return enumerator._settledAt(FULFILLED, i, value);\n }, function (reason) {\n return enumerator._settledAt(REJECTED, i, reason);\n });\n };\n\n return Enumerator;\n}();\n\nexport default Enumerator;","import Enumerator from '../enumerator';\n\n/**\n `Promise.all` accepts an array of promises, and returns a new promise which\n is fulfilled with an array of fulfillment values for the passed promises, or\n rejected with the reason of the first passed promise to be rejected. It casts all\n elements of the passed iterable to promises as it runs this algorithm.\n\n Example:\n\n ```javascript\n let promise1 = resolve(1);\n let promise2 = resolve(2);\n let promise3 = resolve(3);\n let promises = [ promise1, promise2, promise3 ];\n\n Promise.all(promises).then(function(array){\n // The array here would be [ 1, 2, 3 ];\n });\n ```\n\n If any of the `promises` given to `all` are rejected, the first promise\n that is rejected will be given as an argument to the returned promises's\n rejection handler. For example:\n\n Example:\n\n ```javascript\n let promise1 = resolve(1);\n let promise2 = reject(new Error(\"2\"));\n let promise3 = reject(new Error(\"3\"));\n let promises = [ promise1, promise2, promise3 ];\n\n Promise.all(promises).then(function(array){\n // Code here never runs because there are rejected promises!\n }, function(error) {\n // error.message === \"2\"\n });\n ```\n\n @method all\n @static\n @param {Array} entries array of promises\n @param {String} label optional string for labeling the promise.\n Useful for tooling.\n @return {Promise} promise that is fulfilled when all `promises` have been\n fulfilled, or rejected if any of them become rejected.\n @static\n*/\nexport default function all(entries) {\n return new Enumerator(this, entries).promise;\n}","import { isArray } from \"../utils\";\n\n/**\n `Promise.race` returns a new promise which is settled in the same way as the\n first passed promise to settle.\n\n Example:\n\n ```javascript\n let promise1 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 1');\n }, 200);\n });\n\n let promise2 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 2');\n }, 100);\n });\n\n Promise.race([promise1, promise2]).then(function(result){\n // result === 'promise 2' because it was resolved before promise1\n // was resolved.\n });\n ```\n\n `Promise.race` is deterministic in that only the state of the first\n settled promise matters. For example, even if other promises given to the\n `promises` array argument are resolved, but the first settled promise has\n become rejected before the other promises became fulfilled, the returned\n promise will become rejected:\n\n ```javascript\n let promise1 = new Promise(function(resolve, reject){\n setTimeout(function(){\n resolve('promise 1');\n }, 200);\n });\n\n let promise2 = new Promise(function(resolve, reject){\n setTimeout(function(){\n reject(new Error('promise 2'));\n }, 100);\n });\n\n Promise.race([promise1, promise2]).then(function(result){\n // Code here never runs\n }, function(reason){\n // reason.message === 'promise 2' because promise 2 became rejected before\n // promise 1 became fulfilled\n });\n ```\n\n An example real-world use case is implementing timeouts:\n\n ```javascript\n Promise.race([ajax('foo.json'), timeout(5000)])\n ```\n\n @method race\n @static\n @param {Array} promises array of promises to observe\n Useful for tooling.\n @return {Promise} a promise which settles in the same way as the first passed\n promise to settle.\n*/\nexport default function race(entries) {\n /*jshint validthis:true */\n var Constructor = this;\n\n if (!isArray(entries)) {\n return new Constructor(function (_, reject) {\n return reject(new TypeError('You must pass an array to race.'));\n });\n } else {\n return new Constructor(function (resolve, reject) {\n var length = entries.length;\n for (var i = 0; i < length; i++) {\n Constructor.resolve(entries[i]).then(resolve, reject);\n }\n });\n }\n}","import { noop, reject as _reject } from '../-internal';\n\n/**\n `Promise.reject` returns a promise rejected with the passed `reason`.\n It is shorthand for the following:\n\n ```javascript\n let promise = new Promise(function(resolve, reject){\n reject(new Error('WHOOPS'));\n });\n\n promise.then(function(value){\n // Code here doesn't run because the promise is rejected!\n }, function(reason){\n // reason.message === 'WHOOPS'\n });\n ```\n\n Instead of writing the above, your code now simply becomes the following:\n\n ```javascript\n let promise = Promise.reject(new Error('WHOOPS'));\n\n promise.then(function(value){\n // Code here doesn't run because the promise is rejected!\n }, function(reason){\n // reason.message === 'WHOOPS'\n });\n ```\n\n @method reject\n @static\n @param {Any} reason value that the returned promise will be rejected with.\n Useful for tooling.\n @return {Promise} a promise rejected with the given `reason`.\n*/\nexport default function reject(reason) {\n /*jshint validthis:true */\n var Constructor = this;\n var promise = new Constructor(noop);\n _reject(promise, reason);\n return promise;\n}","function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError(\"Cannot call a class as a function\"); } }\n\nimport { isFunction } from './utils';\n\nimport { noop, nextId, PROMISE_ID, initializePromise } from './-internal';\n\nimport { asap, setAsap, setScheduler } from './asap';\n\nimport all from './promise/all';\nimport race from './promise/race';\nimport Resolve from './promise/resolve';\nimport Reject from './promise/reject';\nimport then from './then';\n\nfunction needsResolver() {\n throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');\n}\n\nfunction needsNew() {\n throw new TypeError(\"Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.\");\n}\n\n/**\n Promise objects represent the eventual result of an asynchronous operation. The\n primary way of interacting with a promise is through its `then` method, which\n registers callbacks to receive either a promise's eventual value or the reason\n why the promise cannot be fulfilled.\n\n Terminology\n -----------\n\n - `promise` is an object or function with a `then` method whose behavior conforms to this specification.\n - `thenable` is an object or function that defines a `then` method.\n - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).\n - `exception` is a value that is thrown using the throw statement.\n - `reason` is a value that indicates why a promise was rejected.\n - `settled` the final resting state of a promise, fulfilled or rejected.\n\n A promise can be in one of three states: pending, fulfilled, or rejected.\n\n Promises that are fulfilled have a fulfillment value and are in the fulfilled\n state. Promises that are rejected have a rejection reason and are in the\n rejected state. A fulfillment value is never a thenable.\n\n Promises can also be said to *resolve* a value. If this value is also a\n promise, then the original promise's settled state will match the value's\n settled state. So a promise that *resolves* a promise that rejects will\n itself reject, and a promise that *resolves* a promise that fulfills will\n itself fulfill.\n\n\n Basic Usage:\n ------------\n\n ```js\n let promise = new Promise(function(resolve, reject) {\n // on success\n resolve(value);\n\n // on failure\n reject(reason);\n });\n\n promise.then(function(value) {\n // on fulfillment\n }, function(reason) {\n // on rejection\n });\n ```\n\n Advanced Usage:\n ---------------\n\n Promises shine when abstracting away asynchronous interactions such as\n `XMLHttpRequest`s.\n\n ```js\n function getJSON(url) {\n return new Promise(function(resolve, reject){\n let xhr = new XMLHttpRequest();\n\n xhr.open('GET', url);\n xhr.onreadystatechange = handler;\n xhr.responseType = 'json';\n xhr.setRequestHeader('Accept', 'application/json');\n xhr.send();\n\n function handler() {\n if (this.readyState === this.DONE) {\n if (this.status === 200) {\n resolve(this.response);\n } else {\n reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));\n }\n }\n };\n });\n }\n\n getJSON('/posts.json').then(function(json) {\n // on fulfillment\n }, function(reason) {\n // on rejection\n });\n ```\n\n Unlike callbacks, promises are great composable primitives.\n\n ```js\n Promise.all([\n getJSON('/posts'),\n getJSON('/comments')\n ]).then(function(values){\n values[0] // => postsJSON\n values[1] // => commentsJSON\n\n return values;\n });\n ```\n\n @class Promise\n @param {Function} resolver\n Useful for tooling.\n @constructor\n*/\n\nvar Promise = function () {\n function Promise(resolver) {\n this[PROMISE_ID] = nextId();\n this._result = this._state = undefined;\n this._subscribers = [];\n\n if (noop !== resolver) {\n typeof resolver !== 'function' && needsResolver();\n this instanceof Promise ? initializePromise(this, resolver) : needsNew();\n }\n }\n\n /**\n The primary way of interacting with a promise is through its `then` method,\n which registers callbacks to receive either a promise's eventual value or the\n reason why the promise cannot be fulfilled.\n ```js\n findUser().then(function(user){\n // user is available\n }, function(reason){\n // user is unavailable, and you are given the reason why\n });\n ```\n Chaining\n --------\n The return value of `then` is itself a promise. This second, 'downstream'\n promise is resolved with the return value of the first promise's fulfillment\n or rejection handler, or rejected if the handler throws an exception.\n ```js\n findUser().then(function (user) {\n return user.name;\n }, function (reason) {\n return 'default name';\n }).then(function (userName) {\n // If `findUser` fulfilled, `userName` will be the user's name, otherwise it\n // will be `'default name'`\n });\n findUser().then(function (user) {\n throw new Error('Found user, but still unhappy');\n }, function (reason) {\n throw new Error('`findUser` rejected and we're unhappy');\n }).then(function (value) {\n // never reached\n }, function (reason) {\n // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.\n // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.\n });\n ```\n If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.\n ```js\n findUser().then(function (user) {\n throw new PedagogicalException('Upstream error');\n }).then(function (value) {\n // never reached\n }).then(function (value) {\n // never reached\n }, function (reason) {\n // The `PedgagocialException` is propagated all the way down to here\n });\n ```\n Assimilation\n ------------\n Sometimes the value you want to propagate to a downstream promise can only be\n retrieved asynchronously. This can be achieved by returning a promise in the\n fulfillment or rejection handler. The downstream promise will then be pending\n until the returned promise is settled. This is called *assimilation*.\n ```js\n findUser().then(function (user) {\n return findCommentsByAuthor(user);\n }).then(function (comments) {\n // The user's comments are now available\n });\n ```\n If the assimliated promise rejects, then the downstream promise will also reject.\n ```js\n findUser().then(function (user) {\n return findCommentsByAuthor(user);\n }).then(function (comments) {\n // If `findCommentsByAuthor` fulfills, we'll have the value here\n }, function (reason) {\n // If `findCommentsByAuthor` rejects, we'll have the reason here\n });\n ```\n Simple Example\n --------------\n Synchronous Example\n ```javascript\n let result;\n try {\n result = findResult();\n // success\n } catch(reason) {\n // failure\n }\n ```\n Errback Example\n ```js\n findResult(function(result, err){\n if (err) {\n // failure\n } else {\n // success\n }\n });\n ```\n Promise Example;\n ```javascript\n findResult().then(function(result){\n // success\n }, function(reason){\n // failure\n });\n ```\n Advanced Example\n --------------\n Synchronous Example\n ```javascript\n let author, books;\n try {\n author = findAuthor();\n books = findBooksByAuthor(author);\n // success\n } catch(reason) {\n // failure\n }\n ```\n Errback Example\n ```js\n function foundBooks(books) {\n }\n function failure(reason) {\n }\n findAuthor(function(author, err){\n if (err) {\n failure(err);\n // failure\n } else {\n try {\n findBoooksByAuthor(author, function(books, err) {\n if (err) {\n failure(err);\n } else {\n try {\n foundBooks(books);\n } catch(reason) {\n failure(reason);\n }\n }\n });\n } catch(error) {\n failure(err);\n }\n // success\n }\n });\n ```\n Promise Example;\n ```javascript\n findAuthor().\n then(findBooksByAuthor).\n then(function(books){\n // found books\n }).catch(function(reason){\n // something went wrong\n });\n ```\n @method then\n @param {Function} onFulfilled\n @param {Function} onRejected\n Useful for tooling.\n @return {Promise}\n */\n\n /**\n `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same\n as the catch block of a try/catch statement.\n ```js\n function findAuthor(){\n throw new Error('couldn't find that author');\n }\n // synchronous\n try {\n findAuthor();\n } catch(reason) {\n // something went wrong\n }\n // async with promises\n findAuthor().catch(function(reason){\n // something went wrong\n });\n ```\n @method catch\n @param {Function} onRejection\n Useful for tooling.\n @return {Promise}\n */\n\n\n Promise.prototype.catch = function _catch(onRejection) {\n return this.then(null, onRejection);\n };\n\n /**\n `finally` will be invoked regardless of the promise's fate just as native\n try/catch/finally behaves\n \n Synchronous example:\n \n ```js\n findAuthor() {\n if (Math.random() > 0.5) {\n throw new Error();\n }\n return new Author();\n }\n \n try {\n return findAuthor(); // succeed or fail\n } catch(error) {\n return findOtherAuther();\n } finally {\n // always runs\n // doesn't affect the return value\n }\n ```\n \n Asynchronous example:\n \n ```js\n findAuthor().catch(function(reason){\n return findOtherAuther();\n }).finally(function(){\n // author was either found, or not\n });\n ```\n \n @method finally\n @param {Function} callback\n @return {Promise}\n */\n\n\n Promise.prototype.finally = function _finally(callback) {\n var promise = this;\n var constructor = promise.constructor;\n\n return promise.then(function (value) {\n return constructor.resolve(callback()).then(function () {\n return value;\n });\n }, function (reason) {\n return constructor.resolve(callback()).then(function () {\n throw reason;\n });\n });\n };\n\n return Promise;\n}();\n\nPromise.prototype.then = then;\nexport default Promise;\nPromise.all = all;\nPromise.race = race;\nPromise.resolve = Resolve;\nPromise.reject = Reject;\nPromise._setScheduler = setScheduler;\nPromise._setAsap = setAsap;\nPromise._asap = asap;","/*global self*/\nimport Promise from './promise';\n\nexport default function polyfill() {\n var local = void 0;\n\n if (typeof global !== 'undefined') {\n local = global;\n } else if (typeof self !== 'undefined') {\n local = self;\n } else {\n try {\n local = Function('return this')();\n } catch (e) {\n throw new Error('polyfill failed because global object is unavailable in this environment');\n }\n }\n\n var P = local.Promise;\n\n if (P) {\n var promiseToString = null;\n try {\n promiseToString = Object.prototype.toString.call(P.resolve());\n } catch (e) {\n // silently ignored\n }\n\n if (promiseToString === '[object Promise]' && !P.cast) {\n return;\n }\n }\n\n local.Promise = Promise;\n}","import Promise from './es6-promise/promise';\nimport polyfill from './es6-promise/polyfill';\n\n// Strange compat..\nPromise.polyfill = polyfill;\nPromise.Promise = Promise;\nexport default Promise;","import Promise from './es6-promise';\nPromise.polyfill();\nexport default Promise;","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n/** Error thrown when an HTTP request fails. */\r\nexport class HttpError extends Error {\r\n // tslint:disable-next-line:variable-name\r\n private __proto__: Error;\r\n\r\n /** The HTTP status code represented by this error. */\r\n public statusCode: number;\r\n\r\n /** Constructs a new instance of {@link HttpError}.\r\n *\r\n * @param {string} errorMessage A descriptive error message.\r\n * @param {number} statusCode The HTTP status code represented by this error.\r\n */\r\n constructor(errorMessage: string, statusCode: number) {\r\n const trueProto = new.target.prototype;\r\n super(errorMessage);\r\n this.statusCode = statusCode;\r\n\r\n // Workaround issue in Typescript compiler\r\n // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200\r\n this.__proto__ = trueProto;\r\n }\r\n}\r\n\r\n/** Error thrown when a timeout elapses. */\r\nexport class TimeoutError extends Error {\r\n // tslint:disable-next-line:variable-name\r\n private __proto__: Error;\r\n\r\n /** Constructs a new instance of {@link TimeoutError}.\r\n *\r\n * @param {string} errorMessage A descriptive error message.\r\n */\r\n constructor(errorMessage: string = \"A timeout occurred.\") {\r\n const trueProto = new.target.prototype;\r\n super(errorMessage);\r\n\r\n // Workaround issue in Typescript compiler\r\n // https://github.com/Microsoft/TypeScript/issues/13965#issuecomment-278570200\r\n this.__proto__ = trueProto;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// These values are designed to match the ASP.NET Log Levels since that's the pattern we're emulating here.\r\n/** Indicates the severity of a log message.\r\n *\r\n * Log Levels are ordered in increasing severity. So `Debug` is more severe than `Trace`, etc.\r\n */\r\nexport enum LogLevel {\r\n /** Log level for very low severity diagnostic messages. */\r\n Trace = 0,\r\n /** Log level for low severity diagnostic messages. */\r\n Debug = 1,\r\n /** Log level for informational diagnostic messages. */\r\n Information = 2,\r\n /** Log level for diagnostic messages that indicate a non-fatal problem. */\r\n Warning = 3,\r\n /** Log level for diagnostic messages that indicate a failure in the current operation. */\r\n Error = 4,\r\n /** Log level for diagnostic messages that indicate a failure that will terminate the entire application. */\r\n Critical = 5,\r\n /** The highest possible log level. Used when configuring logging to indicate that no log messages should be emitted. */\r\n None = 6,\r\n}\r\n\r\n/** An abstraction that provides a sink for diagnostic messages. */\r\nexport interface ILogger {\r\n /** Called by the framework to emit a diagnostic message.\r\n *\r\n * @param {LogLevel} logLevel The severity level of the message.\r\n * @param {string} message The message.\r\n */\r\n log(logLevel: LogLevel, message: string): void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { AbortSignal } from \"./AbortController\";\r\nimport { HttpError, TimeoutError } from \"./Errors\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\n\r\n/** Represents an HTTP request. */\r\nexport interface HttpRequest {\r\n /** The HTTP method to use for the request. */\r\n method?: string;\r\n\r\n /** The URL for the request. */\r\n url?: string;\r\n\r\n /** The body content for the request. May be a string or an ArrayBuffer (for binary data). */\r\n content?: string | ArrayBuffer;\r\n\r\n /** An object describing headers to apply to the request. */\r\n headers?: { [key: string]: string };\r\n\r\n /** The XMLHttpRequestResponseType to apply to the request. */\r\n responseType?: XMLHttpRequestResponseType;\r\n\r\n /** An AbortSignal that can be monitored for cancellation. */\r\n abortSignal?: AbortSignal;\r\n\r\n /** The time to wait for the request to complete before throwing a TimeoutError. Measured in milliseconds. */\r\n timeout?: number;\r\n}\r\n\r\n/** Represents an HTTP response. */\r\nexport class HttpResponse {\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n */\r\n constructor(statusCode: number);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code and message.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n */\r\n constructor(statusCode: number, statusText: string);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code, message and string content.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n * @param {string} content The content of the response.\r\n */\r\n constructor(statusCode: number, statusText: string, content: string);\r\n\r\n /** Constructs a new instance of {@link HttpResponse} with the specified status code, message and binary content.\r\n *\r\n * @param {number} statusCode The status code of the response.\r\n * @param {string} statusText The status message of the response.\r\n * @param {ArrayBuffer} content The content of the response.\r\n */\r\n constructor(statusCode: number, statusText: string, content: ArrayBuffer);\r\n constructor(\r\n public readonly statusCode: number,\r\n public readonly statusText?: string,\r\n public readonly content?: string | ArrayBuffer) {\r\n }\r\n}\r\n\r\n/** Abstraction over an HTTP client.\r\n *\r\n * This class provides an abstraction over an HTTP client so that a different implementation can be provided on different platforms.\r\n */\r\nexport abstract class HttpClient {\r\n /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public get(url: string): Promise;\r\n\r\n /** Issues an HTTP GET request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public get(url: string, options: HttpRequest): Promise;\r\n public get(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"GET\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public post(url: string): Promise;\r\n\r\n /** Issues an HTTP POST request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public post(url: string, options: HttpRequest): Promise;\r\n public post(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"POST\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public delete(url: string): Promise;\r\n\r\n /** Issues an HTTP DELETE request to the specified URL, returning a Promise that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {string} url The URL for the request.\r\n * @param {HttpRequest} options Additional options to configure the request. The 'url' field in this object will be overridden by the url parameter.\r\n * @returns {Promise} A Promise that resolves with an {@link HttpResponse} describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public delete(url: string, options: HttpRequest): Promise;\r\n public delete(url: string, options?: HttpRequest): Promise {\r\n return this.send({\r\n ...options,\r\n method: \"DELETE\",\r\n url,\r\n });\r\n }\r\n\r\n /** Issues an HTTP request to the specified URL, returning a {@link Promise} that resolves with an {@link HttpResponse} representing the result.\r\n *\r\n * @param {HttpRequest} request An {@link HttpRequest} describing the request to send.\r\n * @returns {Promise} A Promise that resolves with an HttpResponse describing the response, or rejects with an Error indicating a failure.\r\n */\r\n public abstract send(request: HttpRequest): Promise;\r\n}\r\n\r\n/** Default implementation of {@link HttpClient}. */\r\nexport class DefaultHttpClient extends HttpClient {\r\n private readonly logger: ILogger;\r\n\r\n /** Creates a new instance of the {@link DefaultHttpClient}, using the provided {@link ILogger} to log messages. */\r\n public constructor(logger: ILogger) {\r\n super();\r\n this.logger = logger;\r\n }\r\n\r\n /** @inheritDoc */\r\n public send(request: HttpRequest): Promise {\r\n return new Promise((resolve, reject) => {\r\n const xhr = new XMLHttpRequest();\r\n\r\n xhr.open(request.method, request.url, true);\r\n xhr.withCredentials = true;\r\n xhr.setRequestHeader(\"X-Requested-With\", \"XMLHttpRequest\");\r\n\r\n if (request.headers) {\r\n Object.keys(request.headers)\r\n .forEach((header) => xhr.setRequestHeader(header, request.headers[header]));\r\n }\r\n\r\n if (request.responseType) {\r\n xhr.responseType = request.responseType;\r\n }\r\n\r\n if (request.abortSignal) {\r\n request.abortSignal.onabort = () => {\r\n xhr.abort();\r\n };\r\n }\r\n\r\n if (request.timeout) {\r\n xhr.timeout = request.timeout;\r\n }\r\n\r\n xhr.onload = () => {\r\n if (request.abortSignal) {\r\n request.abortSignal.onabort = null;\r\n }\r\n\r\n if (xhr.status >= 200 && xhr.status < 300) {\r\n resolve(new HttpResponse(xhr.status, xhr.statusText, xhr.response || xhr.responseText));\r\n } else {\r\n reject(new HttpError(xhr.statusText, xhr.status));\r\n }\r\n };\r\n\r\n xhr.onerror = () => {\r\n this.logger.log(LogLevel.Warning, `Error from HTTP request. ${xhr.status}: ${xhr.statusText}`);\r\n reject(new HttpError(xhr.statusText, xhr.status));\r\n };\r\n\r\n xhr.ontimeout = () => {\r\n this.logger.log(LogLevel.Warning, `Timeout from HTTP request.`);\r\n reject(new TimeoutError());\r\n };\r\n\r\n xhr.send(request.content || \"\");\r\n });\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Not exported from index\r\nexport class TextMessageFormat {\r\n public static RecordSeparatorCode = 0x1e;\r\n public static RecordSeparator = String.fromCharCode(TextMessageFormat.RecordSeparatorCode);\r\n\r\n public static write(output: string): string {\r\n return `${output}${TextMessageFormat.RecordSeparator}`;\r\n }\r\n\r\n public static parse(input: string): string[] {\r\n if (input[input.length - 1] !== TextMessageFormat.RecordSeparator) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n const messages = input.split(TextMessageFormat.RecordSeparator);\r\n messages.pop();\r\n return messages;\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { TextMessageFormat } from \"./TextMessageFormat\";\r\n\r\nexport interface HandshakeRequestMessage {\r\n readonly protocol: string;\r\n readonly version: number;\r\n}\r\n\r\nexport interface HandshakeResponseMessage {\r\n readonly error: string;\r\n}\r\n\r\nexport class HandshakeProtocol {\r\n // Handshake request is always JSON\r\n public writeHandshakeRequest(handshakeRequest: HandshakeRequestMessage): string {\r\n return TextMessageFormat.write(JSON.stringify(handshakeRequest));\r\n }\r\n\r\n public parseHandshakeResponse(data: any): [any, HandshakeResponseMessage] {\r\n let responseMessage: HandshakeResponseMessage;\r\n let messageData: string;\r\n let remainingData: any;\r\n\r\n if (data instanceof ArrayBuffer) {\r\n // Format is binary but still need to read JSON text from handshake response\r\n const binaryData = new Uint8Array(data);\r\n const separatorIndex = binaryData.indexOf(TextMessageFormat.RecordSeparatorCode);\r\n if (separatorIndex === -1) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n // content before separator is handshake response\r\n // optional content after is additional messages\r\n const responseLength = separatorIndex + 1;\r\n messageData = String.fromCharCode.apply(null, binaryData.slice(0, responseLength));\r\n remainingData = (binaryData.byteLength > responseLength) ? binaryData.slice(responseLength).buffer : null;\r\n } else {\r\n const textData: string = data;\r\n const separatorIndex = textData.indexOf(TextMessageFormat.RecordSeparator);\r\n if (separatorIndex === -1) {\r\n throw new Error(\"Message is incomplete.\");\r\n }\r\n\r\n // content before separator is handshake response\r\n // optional content after is additional messages\r\n const responseLength = separatorIndex + 1;\r\n messageData = textData.substring(0, responseLength);\r\n remainingData = (textData.length > responseLength) ? textData.substring(responseLength) : null;\r\n }\r\n\r\n // At this point we should have just the single handshake message\r\n const messages = TextMessageFormat.parse(messageData);\r\n responseMessage = JSON.parse(messages[0]);\r\n\r\n // multiple messages could have arrived with handshake\r\n // return additional data to be parsed as usual, or null if all parsed\r\n return [remainingData, responseMessage];\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger } from \"./ILogger\";\r\nimport { TransferFormat } from \"./ITransport\";\r\n\r\n/** Defines the type of a Hub Message. */\r\nexport enum MessageType {\r\n /** Indicates the message is an Invocation message and implements the {@link InvocationMessage} interface. */\r\n Invocation = 1,\r\n /** Indicates the message is a StreamItem message and implements the {@link StreamItemMessage} interface. */\r\n StreamItem = 2,\r\n /** Indicates the message is a Completion message and implements the {@link CompletionMessage} interface. */\r\n Completion = 3,\r\n /** Indicates the message is a Stream Invocation message and implements the {@link StreamInvocationMessage} interface. */\r\n StreamInvocation = 4,\r\n /** Indicates the message is a Cancel Invocation message and implements the {@link CancelInvocationMessage} interface. */\r\n CancelInvocation = 5,\r\n /** Indicates the message is a Ping message and implements the {@link PingMessage} interface. */\r\n Ping = 6,\r\n /** Indicates the message is a Close message and implements the {@link CloseMessage} interface. */\r\n Close = 7,\r\n}\r\n\r\n/** Defines a dictionary of string keys and string values representing headers attached to a Hub message. */\r\nexport interface MessageHeaders {\r\n /** Gets or sets the header with the specified key. */\r\n [key: string]: string;\r\n}\r\n\r\n/** Union type of all known Hub messages. */\r\nexport type HubMessage =\r\n InvocationMessage |\r\n StreamInvocationMessage |\r\n StreamItemMessage |\r\n CompletionMessage |\r\n CancelInvocationMessage |\r\n PingMessage |\r\n CloseMessage;\r\n\r\n/** Defines properties common to all Hub messages. */\r\nexport interface HubMessageBase {\r\n /** A {@link MessageType} value indicating the type of this message. */\r\n readonly type: MessageType;\r\n}\r\n\r\n/** Defines properties common to all Hub messages relating to a specific invocation. */\r\nexport interface HubInvocationMessage extends HubMessageBase {\r\n /** A {@link MessageHeaders} dictionary containing headers attached to the message. */\r\n readonly headers?: MessageHeaders;\r\n /** The ID of the invocation relating to this message.\r\n *\r\n * This is expected to be present for {@link StreamInvocationMessage} and {@link CompletionMessage}. It may\r\n * be 'undefined' for an {@link InvocationMessage} if the sender does not expect a response.\r\n */\r\n readonly invocationId?: string;\r\n}\r\n\r\n/** A hub message representing a non-streaming invocation. */\r\nexport interface InvocationMessage extends HubInvocationMessage {\r\n readonly type: MessageType.Invocation;\r\n /** The target method name. */\r\n readonly target: string;\r\n /** The target method arguments. */\r\n readonly arguments: any[];\r\n}\r\n\r\n/** A hub message representing a streaming invocation. */\r\nexport interface StreamInvocationMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.StreamInvocation;\r\n\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n /** The target method name. */\r\n readonly target: string;\r\n /** The target method arguments. */\r\n readonly arguments: any[];\r\n}\r\n\r\n/** A hub message representing a single item produced as part of a result stream. */\r\nexport interface StreamItemMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.StreamItem;\r\n\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n\r\n /** The item produced by the server. */\r\n readonly item?: any;\r\n}\r\n\r\n/** A hub message representing the result of an invocation. */\r\nexport interface CompletionMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Completion;\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n /** The error produced by the invocation, if any.\r\n *\r\n * Either {@link error} or {@link result} must be defined, but not both.\r\n */\r\n readonly error?: string;\r\n /** The result produced by the invocation, if any.\r\n *\r\n * Either {@link error} or {@link result} must be defined, but not both.\r\n */\r\n readonly result?: any;\r\n}\r\n\r\n/** A hub message indicating that the sender is still active. */\r\nexport interface PingMessage extends HubMessageBase {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Ping;\r\n}\r\n\r\n/** A hub message indicating that the sender is closing the connection.\r\n *\r\n * If {@link error} is defined, the sender is closing the connection due to an error.\r\n */\r\nexport interface CloseMessage extends HubMessageBase {\r\n /** @inheritDoc */\r\n readonly type: MessageType.Close;\r\n /** The error that triggered the close, if any.\r\n *\r\n * If this property is undefined, the connection was closed normally and without error.\r\n */\r\n readonly error?: string;\r\n}\r\n\r\n/** A hub message sent to request that a streaming invocation be canceled. */\r\nexport interface CancelInvocationMessage extends HubInvocationMessage {\r\n /** @inheritDoc */\r\n readonly type: MessageType.CancelInvocation;\r\n /** The invocation ID. */\r\n readonly invocationId: string;\r\n}\r\n\r\n/** A protocol abstraction for communicating with SignalR Hubs. */\r\nexport interface IHubProtocol {\r\n /** The name of the protocol. This is used by SignalR to resolve the protocol between the client and server. */\r\n readonly name: string;\r\n /** The version of the protocol. */\r\n readonly version: number;\r\n /** The {@link TransferFormat} of the protocol. */\r\n readonly transferFormat: TransferFormat;\r\n\r\n /** Creates an array of {@link HubMessage} objects from the specified serialized representation.\r\n *\r\n * If {@link transferFormat} is 'Text', the {@link input} parameter must be a string, otherwise it must be an ArrayBuffer.\r\n *\r\n * @param {string | ArrayBuffer} input A string, or ArrayBuffer containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];\r\n\r\n /** Writes the specified {@link HubMessage} to a string or ArrayBuffer and returns it.\r\n *\r\n * If {@link transferFormat} is 'Text', the result of this method will be a string, otherwise it will be an ArrayBuffer.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {string | ArrayBuffer} A string or ArrayBuffer containing the serialized representation of the message.\r\n */\r\n writeMessage(message: HubMessage): string | ArrayBuffer;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\n\r\n/** A logger that does nothing when log messages are sent to it. */\r\nexport class NullLogger implements ILogger {\r\n /** The singleton instance of the {@link NullLogger}. */\r\n public static instance: ILogger = new NullLogger();\r\n\r\n private constructor() {}\r\n\r\n /** @inheritDoc */\r\n public log(logLevel: LogLevel, message: string): void {\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpClient } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { IStreamResult, IStreamSubscriber, ISubscription } from \"./Stream\";\r\n\r\nexport class Arg {\r\n public static isRequired(val: any, name: string): void {\r\n if (val === null || val === undefined) {\r\n throw new Error(`The '${name}' argument is required.`);\r\n }\r\n }\r\n\r\n public static isIn(val: any, values: any, name: string): void {\r\n // TypeScript enums have keys for **both** the name and the value of each enum member on the type itself.\r\n if (!(val in values)) {\r\n throw new Error(`Unknown ${name} value: ${val}.`);\r\n }\r\n }\r\n}\r\n\r\nexport function getDataDetail(data: any, includeContent: boolean): string {\r\n let length: string = null;\r\n if (data instanceof ArrayBuffer) {\r\n length = `Binary data of length ${data.byteLength}`;\r\n if (includeContent) {\r\n length += `. Content: '${formatArrayBuffer(data)}'`;\r\n }\r\n } else if (typeof data === \"string\") {\r\n length = `String data of length ${data.length}`;\r\n if (includeContent) {\r\n length += `. Content: '${data}'.`;\r\n }\r\n }\r\n return length;\r\n}\r\n\r\nexport function formatArrayBuffer(data: ArrayBuffer): string {\r\n const view = new Uint8Array(data);\r\n\r\n // Uint8Array.map only supports returning another Uint8Array?\r\n let str = \"\";\r\n view.forEach((num) => {\r\n const pad = num < 16 ? \"0\" : \"\";\r\n str += `0x${pad}${num.toString(16)} `;\r\n });\r\n\r\n // Trim of trailing space.\r\n return str.substr(0, str.length - 1);\r\n}\r\n\r\nexport async function sendMessage(logger: ILogger, transportName: string, httpClient: HttpClient, url: string, accessTokenFactory: () => string | Promise, content: string | ArrayBuffer, logMessageContent: boolean): Promise {\r\n let headers;\r\n const token = await accessTokenFactory();\r\n if (token) {\r\n headers = {\r\n [\"Authorization\"]: `Bearer ${token}`,\r\n };\r\n }\r\n\r\n logger.log(LogLevel.Trace, `(${transportName} transport) sending data. ${getDataDetail(content, logMessageContent)}.`);\r\n\r\n const response = await httpClient.post(url, {\r\n content,\r\n headers,\r\n });\r\n\r\n logger.log(LogLevel.Trace, `(${transportName} transport) request complete. Response status: ${response.statusCode}.`);\r\n}\r\n\r\nexport function createLogger(logger?: ILogger | LogLevel) {\r\n if (logger === undefined) {\r\n return new ConsoleLogger(LogLevel.Information);\r\n }\r\n\r\n if (logger === null) {\r\n return NullLogger.instance;\r\n }\r\n\r\n if ((logger as ILogger).log) {\r\n return logger as ILogger;\r\n }\r\n\r\n return new ConsoleLogger(logger as LogLevel);\r\n}\r\n\r\nexport class Subject implements IStreamResult {\r\n public observers: Array>;\r\n public cancelCallback: () => Promise;\r\n\r\n constructor(cancelCallback: () => Promise) {\r\n this.observers = [];\r\n this.cancelCallback = cancelCallback;\r\n }\r\n\r\n public next(item: T): void {\r\n for (const observer of this.observers) {\r\n observer.next(item);\r\n }\r\n }\r\n\r\n public error(err: any): void {\r\n for (const observer of this.observers) {\r\n if (observer.error) {\r\n observer.error(err);\r\n }\r\n }\r\n }\r\n\r\n public complete(): void {\r\n for (const observer of this.observers) {\r\n if (observer.complete) {\r\n observer.complete();\r\n }\r\n }\r\n }\r\n\r\n public subscribe(observer: IStreamSubscriber): ISubscription {\r\n this.observers.push(observer);\r\n return new SubjectSubscription(this, observer);\r\n }\r\n}\r\n\r\nexport class SubjectSubscription implements ISubscription {\r\n private subject: Subject;\r\n private observer: IStreamSubscriber;\r\n\r\n constructor(subject: Subject, observer: IStreamSubscriber) {\r\n this.subject = subject;\r\n this.observer = observer;\r\n }\r\n\r\n public dispose(): void {\r\n const index: number = this.subject.observers.indexOf(this.observer);\r\n if (index > -1) {\r\n this.subject.observers.splice(index, 1);\r\n }\r\n\r\n if (this.subject.observers.length === 0) {\r\n this.subject.cancelCallback().catch((_) => { });\r\n }\r\n }\r\n}\r\n\r\nexport class ConsoleLogger implements ILogger {\r\n private readonly minimumLogLevel: LogLevel;\r\n\r\n constructor(minimumLogLevel: LogLevel) {\r\n this.minimumLogLevel = minimumLogLevel;\r\n }\r\n\r\n public log(logLevel: LogLevel, message: string): void {\r\n if (logLevel >= this.minimumLogLevel) {\r\n switch (logLevel) {\r\n case LogLevel.Critical:\r\n case LogLevel.Error:\r\n console.error(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n case LogLevel.Warning:\r\n console.warn(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n case LogLevel.Information:\r\n console.info(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n default:\r\n // console.debug only goes to attached debuggers in Node, so we use console.log for Trace and Debug\r\n console.log(`${LogLevel[logLevel]}: ${message}`);\r\n break;\r\n }\r\n }\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HandshakeProtocol, HandshakeRequestMessage, HandshakeResponseMessage } from \"./HandshakeProtocol\";\r\nimport { IConnection } from \"./IConnection\";\r\nimport { CancelInvocationMessage, CompletionMessage, IHubProtocol, InvocationMessage, MessageType, StreamInvocationMessage, StreamItemMessage } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { IStreamResult } from \"./Stream\";\r\nimport { Arg, Subject } from \"./Utils\";\r\n\r\nconst DEFAULT_TIMEOUT_IN_MS: number = 30 * 1000;\r\n\r\n/** Represents a connection to a SignalR Hub. */\r\nexport class HubConnection {\r\n private readonly connection: IConnection;\r\n private readonly logger: ILogger;\r\n private protocol: IHubProtocol;\r\n private handshakeProtocol: HandshakeProtocol;\r\n private callbacks: { [invocationId: string]: (invocationEvent: StreamItemMessage | CompletionMessage, error?: Error) => void };\r\n private methods: { [name: string]: Array<(...args: any[]) => void> };\r\n private id: number;\r\n private closedCallbacks: Array<(error?: Error) => void>;\r\n private timeoutHandle: NodeJS.Timer;\r\n private receivedHandshakeResponse: boolean;\r\n\r\n /** The server timeout in milliseconds.\r\n *\r\n * If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error.\r\n * The default timeout value is 30,000 milliseconds (30 seconds).\r\n */\r\n public serverTimeoutInMilliseconds: number;\r\n\r\n /** @internal */\r\n // Using a public static factory method means we can have a private constructor and an _internal_\r\n // create method that can be used by HubConnectionBuilder. An \"internal\" constructor would just\r\n // be stripped away and the '.d.ts' file would have no constructor, which is interpreted as a\r\n // public parameter-less constructor.\r\n public static create(connection: IConnection, logger: ILogger, protocol: IHubProtocol): HubConnection {\r\n return new HubConnection(connection, logger, protocol);\r\n }\r\n\r\n private constructor(connection: IConnection, logger: ILogger, protocol: IHubProtocol) {\r\n Arg.isRequired(connection, \"connection\");\r\n Arg.isRequired(logger, \"logger\");\r\n Arg.isRequired(protocol, \"protocol\");\r\n\r\n this.serverTimeoutInMilliseconds = DEFAULT_TIMEOUT_IN_MS;\r\n\r\n this.logger = logger;\r\n this.protocol = protocol;\r\n this.connection = connection;\r\n this.handshakeProtocol = new HandshakeProtocol();\r\n\r\n this.connection.onreceive = (data: any) => this.processIncomingData(data);\r\n this.connection.onclose = (error?: Error) => this.connectionClosed(error);\r\n\r\n this.callbacks = {};\r\n this.methods = {};\r\n this.closedCallbacks = [];\r\n this.id = 0;\r\n }\r\n\r\n /** Starts the connection.\r\n *\r\n * @returns {Promise} A Promise that resolves when the connection has been successfully established, or rejects with an error.\r\n */\r\n public async start(): Promise {\r\n const handshakeRequest: HandshakeRequestMessage = {\r\n protocol: this.protocol.name,\r\n version: this.protocol.version,\r\n };\r\n\r\n this.logger.log(LogLevel.Debug, \"Starting HubConnection.\");\r\n\r\n this.receivedHandshakeResponse = false;\r\n\r\n await this.connection.start(this.protocol.transferFormat);\r\n\r\n this.logger.log(LogLevel.Debug, \"Sending handshake request.\");\r\n\r\n await this.connection.send(this.handshakeProtocol.writeHandshakeRequest(handshakeRequest));\r\n\r\n this.logger.log(LogLevel.Information, `Using HubProtocol '${this.protocol.name}'.`);\r\n\r\n // defensively cleanup timeout in case we receive a message from the server before we finish start\r\n this.cleanupTimeout();\r\n this.configureTimeout();\r\n }\r\n\r\n /** Stops the connection.\r\n *\r\n * @returns {Promise} A Promise that resolves when the connection has been successfully terminated, or rejects with an error.\r\n */\r\n public stop(): Promise {\r\n this.logger.log(LogLevel.Debug, \"Stopping HubConnection.\");\r\n\r\n this.cleanupTimeout();\r\n return this.connection.stop();\r\n }\r\n\r\n /** Invokes a streaming hub method on the server using the specified name and arguments.\r\n *\r\n * @typeparam T The type of the items returned by the server.\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {IStreamResult} An object that yields results from the server as they are received.\r\n */\r\n public stream(methodName: string, ...args: any[]): IStreamResult {\r\n const invocationDescriptor = this.createStreamInvocation(methodName, args);\r\n\r\n const subject = new Subject(() => {\r\n const cancelInvocation: CancelInvocationMessage = this.createCancelInvocation(invocationDescriptor.invocationId);\r\n const cancelMessage: any = this.protocol.writeMessage(cancelInvocation);\r\n\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n\r\n return this.connection.send(cancelMessage);\r\n });\r\n\r\n this.callbacks[invocationDescriptor.invocationId] = (invocationEvent: CompletionMessage | StreamItemMessage, error?: Error) => {\r\n if (error) {\r\n subject.error(error);\r\n return;\r\n }\r\n\r\n if (invocationEvent.type === MessageType.Completion) {\r\n if (invocationEvent.error) {\r\n subject.error(new Error(invocationEvent.error));\r\n } else {\r\n subject.complete();\r\n }\r\n } else {\r\n subject.next((invocationEvent.item) as T);\r\n }\r\n };\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n this.connection.send(message)\r\n .catch((e) => {\r\n subject.error(e);\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n });\r\n\r\n return subject;\r\n }\r\n\r\n /** Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver.\r\n *\r\n * The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still\r\n * be processing the invocation.\r\n *\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {Promise} A Promise that resolves when the invocation has been successfully sent, or rejects with an error.\r\n */\r\n public send(methodName: string, ...args: any[]): Promise {\r\n const invocationDescriptor = this.createInvocation(methodName, args, true);\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n return this.connection.send(message);\r\n }\r\n\r\n /** Invokes a hub method on the server using the specified name and arguments.\r\n *\r\n * The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise\r\n * resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of\r\n * resolving the Promise.\r\n *\r\n * @typeparam T The expected return type.\r\n * @param {string} methodName The name of the server method to invoke.\r\n * @param {any[]} args The arguments used to invoke the server method.\r\n * @returns {Promise} A Promise that resolves with the result of the server method (if any), or rejects with an error.\r\n */\r\n public invoke(methodName: string, ...args: any[]): Promise {\r\n const invocationDescriptor = this.createInvocation(methodName, args, false);\r\n\r\n const p = new Promise((resolve, reject) => {\r\n this.callbacks[invocationDescriptor.invocationId] = (invocationEvent: StreamItemMessage | CompletionMessage, error?: Error) => {\r\n if (error) {\r\n reject(error);\r\n return;\r\n }\r\n if (invocationEvent.type === MessageType.Completion) {\r\n const completionMessage = invocationEvent as CompletionMessage;\r\n if (completionMessage.error) {\r\n reject(new Error(completionMessage.error));\r\n } else {\r\n resolve(completionMessage.result);\r\n }\r\n } else {\r\n reject(new Error(`Unexpected message type: ${invocationEvent.type}`));\r\n }\r\n };\r\n\r\n const message = this.protocol.writeMessage(invocationDescriptor);\r\n\r\n this.connection.send(message)\r\n .catch((e) => {\r\n reject(e);\r\n delete this.callbacks[invocationDescriptor.invocationId];\r\n });\r\n });\r\n\r\n return p;\r\n }\r\n\r\n /** Registers a handler that will be invoked when the hub method with the specified method name is invoked.\r\n *\r\n * @param {string} methodName The name of the hub method to define.\r\n * @param {Function} newMethod The handler that will be raised when the hub method is invoked.\r\n */\r\n public on(methodName: string, newMethod: (...args: any[]) => void) {\r\n if (!methodName || !newMethod) {\r\n return;\r\n }\r\n\r\n methodName = methodName.toLowerCase();\r\n if (!this.methods[methodName]) {\r\n this.methods[methodName] = [];\r\n }\r\n\r\n // Preventing adding the same handler multiple times.\r\n if (this.methods[methodName].indexOf(newMethod) !== -1) {\r\n return;\r\n }\r\n\r\n this.methods[methodName].push(newMethod);\r\n }\r\n\r\n /** Removes all handlers for the specified hub method.\r\n *\r\n * @param {string} methodName The name of the method to remove handlers for.\r\n */\r\n public off(methodName: string): void;\r\n\r\n /** Removes the specified handler for the specified hub method.\r\n *\r\n * You must pass the exact same Function instance as was previously passed to {@link on}. Passing a different instance (even if the function\r\n * body is the same) will not remove the handler.\r\n *\r\n * @param {string} methodName The name of the method to remove handlers for.\r\n * @param {Function} method The handler to remove. This must be the same Function instance as the one passed to {@link on}.\r\n */\r\n public off(methodName: string, method: (...args: any[]) => void): void;\r\n public off(methodName: string, method?: (...args: any[]) => void): void {\r\n if (!methodName) {\r\n return;\r\n }\r\n\r\n methodName = methodName.toLowerCase();\r\n const handlers = this.methods[methodName];\r\n if (!handlers) {\r\n return;\r\n }\r\n if (method) {\r\n const removeIdx = handlers.indexOf(method);\r\n if (removeIdx !== -1) {\r\n handlers.splice(removeIdx, 1);\r\n if (handlers.length === 0) {\r\n delete this.methods[methodName];\r\n }\r\n }\r\n } else {\r\n delete this.methods[methodName];\r\n }\r\n\r\n }\r\n\r\n /** Registers a handler that will be invoked when the connection is closed.\r\n *\r\n * @param {Function} callback The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).\r\n */\r\n public onclose(callback: (error?: Error) => void) {\r\n if (callback) {\r\n this.closedCallbacks.push(callback);\r\n }\r\n }\r\n\r\n private processIncomingData(data: any) {\r\n this.cleanupTimeout();\r\n\r\n if (!this.receivedHandshakeResponse) {\r\n data = this.processHandshakeResponse(data);\r\n this.receivedHandshakeResponse = true;\r\n }\r\n\r\n // Data may have all been read when processing handshake response\r\n if (data) {\r\n // Parse the messages\r\n const messages = this.protocol.parseMessages(data, this.logger);\r\n\r\n for (const message of messages) {\r\n switch (message.type) {\r\n case MessageType.Invocation:\r\n this.invokeClientMethod(message);\r\n break;\r\n case MessageType.StreamItem:\r\n case MessageType.Completion:\r\n const callback = this.callbacks[message.invocationId];\r\n if (callback != null) {\r\n if (message.type === MessageType.Completion) {\r\n delete this.callbacks[message.invocationId];\r\n }\r\n callback(message);\r\n }\r\n break;\r\n case MessageType.Ping:\r\n // Don't care about pings\r\n break;\r\n case MessageType.Close:\r\n this.logger.log(LogLevel.Information, \"Close message received from server.\");\r\n this.connection.stop(message.error ? new Error(\"Server returned an error on close: \" + message.error) : null);\r\n break;\r\n default:\r\n this.logger.log(LogLevel.Warning, \"Invalid message type: \" + message.type);\r\n break;\r\n }\r\n }\r\n }\r\n\r\n this.configureTimeout();\r\n }\r\n\r\n private processHandshakeResponse(data: any): any {\r\n let responseMessage: HandshakeResponseMessage;\r\n let remainingData: any;\r\n\r\n try {\r\n [remainingData, responseMessage] = this.handshakeProtocol.parseHandshakeResponse(data);\r\n } catch (e) {\r\n const message = \"Error parsing handshake response: \" + e;\r\n this.logger.log(LogLevel.Error, message);\r\n\r\n const error = new Error(message);\r\n this.connection.stop(error);\r\n throw error;\r\n }\r\n if (responseMessage.error) {\r\n const message = \"Server returned handshake error: \" + responseMessage.error;\r\n this.logger.log(LogLevel.Error, message);\r\n this.connection.stop(new Error(message));\r\n } else {\r\n this.logger.log(LogLevel.Debug, \"Server handshake complete.\");\r\n }\r\n\r\n return remainingData;\r\n }\r\n\r\n private configureTimeout() {\r\n if (!this.connection.features || !this.connection.features.inherentKeepAlive) {\r\n // Set the timeout timer\r\n this.timeoutHandle = setTimeout(() => this.serverTimeout(), this.serverTimeoutInMilliseconds);\r\n }\r\n }\r\n\r\n private serverTimeout() {\r\n // The server hasn't talked to us in a while. It doesn't like us anymore ... :(\r\n // Terminate the connection\r\n this.connection.stop(new Error(\"Server timeout elapsed without receiving a message from the server.\"));\r\n }\r\n\r\n private invokeClientMethod(invocationMessage: InvocationMessage) {\r\n const methods = this.methods[invocationMessage.target.toLowerCase()];\r\n if (methods) {\r\n methods.forEach((m) => m.apply(this, invocationMessage.arguments));\r\n if (invocationMessage.invocationId) {\r\n // This is not supported in v1. So we return an error to avoid blocking the server waiting for the response.\r\n const message = \"Server requested a response, which is not supported in this version of the client.\";\r\n this.logger.log(LogLevel.Error, message);\r\n this.connection.stop(new Error(message));\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Warning, `No client method with the name '${invocationMessage.target}' found.`);\r\n }\r\n }\r\n\r\n private connectionClosed(error?: Error) {\r\n const callbacks = this.callbacks;\r\n this.callbacks = {};\r\n\r\n Object.keys(callbacks)\r\n .forEach((key) => {\r\n const callback = callbacks[key];\r\n callback(undefined, error ? error : new Error(\"Invocation canceled due to connection being closed.\"));\r\n });\r\n\r\n this.cleanupTimeout();\r\n\r\n this.closedCallbacks.forEach((c) => c.apply(this, [error]));\r\n }\r\n\r\n private cleanupTimeout(): void {\r\n if (this.timeoutHandle) {\r\n clearTimeout(this.timeoutHandle);\r\n }\r\n }\r\n\r\n private createInvocation(methodName: string, args: any[], nonblocking: boolean): InvocationMessage {\r\n if (nonblocking) {\r\n return {\r\n arguments: args,\r\n target: methodName,\r\n type: MessageType.Invocation,\r\n };\r\n } else {\r\n const id = this.id;\r\n this.id++;\r\n\r\n return {\r\n arguments: args,\r\n invocationId: id.toString(),\r\n target: methodName,\r\n type: MessageType.Invocation,\r\n };\r\n }\r\n }\r\n\r\n private createStreamInvocation(methodName: string, args: any[]): StreamInvocationMessage {\r\n const id = this.id;\r\n this.id++;\r\n\r\n return {\r\n arguments: args,\r\n invocationId: id.toString(),\r\n target: methodName,\r\n type: MessageType.StreamInvocation,\r\n };\r\n }\r\n\r\n private createCancelInvocation(id: string): CancelInvocationMessage {\r\n return {\r\n invocationId: id,\r\n type: MessageType.CancelInvocation,\r\n };\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This will be treated as a bit flag in the future, so we keep it using power-of-two values.\r\n/** Specifies a specific HTTP transport type. */\r\nexport enum HttpTransportType {\r\n /** Specifies no transport preference. */\r\n None = 0,\r\n /** Specifies the WebSockets transport. */\r\n WebSockets = 1,\r\n /** Specifies the Server-Sent Events transport. */\r\n ServerSentEvents = 2,\r\n /** Specifies the Long Polling transport. */\r\n LongPolling = 4,\r\n}\r\n\r\n/** Specifies the transfer format for a connection. */\r\nexport enum TransferFormat {\r\n /** Specifies that only text data will be transmitted over the connection. */\r\n Text = 1,\r\n /** Specifies that binary data will be transmitted over the connection. */\r\n Binary,\r\n}\r\n\r\n/** An abstraction over the behavior of transports. This is designed to support the framework and not intended for use by applications. */\r\nexport interface ITransport {\r\n connect(url: string, transferFormat: TransferFormat): Promise;\r\n send(data: any): Promise;\r\n stop(): Promise;\r\n onreceive: (data: string | ArrayBuffer) => void;\r\n onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Rough polyfill of https://developer.mozilla.org/en-US/docs/Web/API/AbortController\r\n// We don't actually ever use the API being polyfilled, we always use the polyfill because\r\n// it's a very new API right now.\r\n\r\n// Not exported from index.\r\nexport class AbortController implements AbortSignal {\r\n private isAborted: boolean = false;\r\n public onabort: () => void;\r\n\r\n public abort() {\r\n if (!this.isAborted) {\r\n this.isAborted = true;\r\n if (this.onabort) {\r\n this.onabort();\r\n }\r\n }\r\n }\r\n\r\n get signal(): AbortSignal {\r\n return this;\r\n }\r\n\r\n get aborted(): boolean {\r\n return this.isAborted;\r\n }\r\n}\r\n\r\n/** Represents a signal that can be monitored to determine if a request has been aborted. */\r\nexport interface AbortSignal {\r\n /** Indicates if the request has been aborted. */\r\n aborted: boolean;\r\n /** Set this to a handler that will be invoked when the request is aborted. */\r\n onabort: () => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { AbortController } from \"./AbortController\";\r\nimport { HttpError, TimeoutError } from \"./Errors\";\r\nimport { HttpClient, HttpRequest } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail, sendMessage } from \"./Utils\";\r\n\r\nconst SHUTDOWN_TIMEOUT = 5 * 1000;\r\n\r\n// Not exported from 'index', this type is internal.\r\nexport class LongPollingTransport implements ITransport {\r\n private readonly httpClient: HttpClient;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logger: ILogger;\r\n private readonly logMessageContent: boolean;\r\n\r\n private url: string;\r\n private pollXhr: XMLHttpRequest;\r\n private pollAbort: AbortController;\r\n private shutdownTimer: any; // We use 'any' because this is an object in NodeJS. But it still gets passed to clearTimeout, so it doesn't really matter\r\n private shutdownTimeout: number;\r\n private running: boolean;\r\n private stopped: boolean;\r\n\r\n // This is an internal type, not exported from 'index' so this is really just internal.\r\n public get pollAborted() {\r\n return this.pollAbort.aborted;\r\n }\r\n\r\n constructor(httpClient: HttpClient, accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean, shutdownTimeout?: number) {\r\n this.httpClient = httpClient;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logger = logger;\r\n this.pollAbort = new AbortController();\r\n this.logMessageContent = logMessageContent;\r\n this.shutdownTimeout = shutdownTimeout || SHUTDOWN_TIMEOUT;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n this.url = url;\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Connecting\");\r\n\r\n if (transferFormat === TransferFormat.Binary && (typeof new XMLHttpRequest().responseType !== \"string\")) {\r\n // This will work if we fix: https://github.com/aspnet/SignalR/issues/742\r\n throw new Error(\"Binary protocols over XmlHttpRequest not implementing advanced features are not supported.\");\r\n }\r\n\r\n const pollOptions: HttpRequest = {\r\n abortSignal: this.pollAbort.signal,\r\n headers: {},\r\n timeout: 90000,\r\n };\r\n\r\n if (transferFormat === TransferFormat.Binary) {\r\n pollOptions.responseType = \"arraybuffer\";\r\n }\r\n\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(pollOptions, token);\r\n\r\n let closeError: Error;\r\n\r\n // Make initial long polling request\r\n // Server uses first long polling request to finish initializing connection and it returns without data\r\n const pollUrl = `${url}&_=${Date.now()}`;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}`);\r\n const response = await this.httpClient.get(pollUrl, pollOptions);\r\n if (response.statusCode !== 200) {\r\n this.logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}`);\r\n\r\n // Mark running as false so that the poll immediately ends and runs the close logic\r\n closeError = new HttpError(response.statusText, response.statusCode);\r\n this.running = false;\r\n } else {\r\n this.running = true;\r\n }\r\n\r\n this.poll(this.url, pollOptions, closeError);\r\n return Promise.resolve();\r\n }\r\n\r\n private updateHeaderToken(request: HttpRequest, token: string) {\r\n if (token) {\r\n // tslint:disable-next-line:no-string-literal\r\n request.headers[\"Authorization\"] = `Bearer ${token}`;\r\n return;\r\n }\r\n // tslint:disable-next-line:no-string-literal\r\n if (request.headers[\"Authorization\"]) {\r\n // tslint:disable-next-line:no-string-literal\r\n delete request.headers[\"Authorization\"];\r\n }\r\n }\r\n\r\n private async poll(url: string, pollOptions: HttpRequest, closeError: Error): Promise {\r\n try {\r\n while (this.running) {\r\n // We have to get the access token on each poll, in case it changes\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(pollOptions, token);\r\n\r\n try {\r\n const pollUrl = `${url}&_=${Date.now()}`;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) polling: ${pollUrl}`);\r\n const response = await this.httpClient.get(pollUrl, pollOptions);\r\n\r\n if (response.statusCode === 204) {\r\n this.logger.log(LogLevel.Information, \"(LongPolling transport) Poll terminated by server\");\r\n\r\n this.running = false;\r\n } else if (response.statusCode !== 200) {\r\n this.logger.log(LogLevel.Error, `(LongPolling transport) Unexpected response code: ${response.statusCode}`);\r\n\r\n // Unexpected status code\r\n closeError = new HttpError(response.statusText, response.statusCode);\r\n this.running = false;\r\n } else {\r\n // Process the response\r\n if (response.content) {\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) data received. ${getDataDetail(response.content, this.logMessageContent)}`);\r\n if (this.onreceive) {\r\n this.onreceive(response.content);\r\n }\r\n } else {\r\n // This is another way timeout manifest.\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Poll timed out, reissuing.\");\r\n }\r\n }\r\n } catch (e) {\r\n if (!this.running) {\r\n // Log but disregard errors that occur after we were stopped by DELETE\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) Poll errored after shutdown: ${e.message}`);\r\n } else {\r\n if (e instanceof TimeoutError) {\r\n // Ignore timeouts and reissue the poll.\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Poll timed out, reissuing.\");\r\n } else {\r\n // Close the connection with the error as the result.\r\n closeError = e;\r\n this.running = false;\r\n }\r\n }\r\n }\r\n }\r\n } finally {\r\n // Indicate that we've stopped so the shutdown timer doesn't get registered.\r\n this.stopped = true;\r\n\r\n // Clean up the shutdown timer if it was registered\r\n if (this.shutdownTimer) {\r\n clearTimeout(this.shutdownTimer);\r\n }\r\n\r\n // Fire our onclosed event\r\n if (this.onclose) {\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) Firing onclose event. Error: ${closeError || \"\"}`);\r\n this.onclose(closeError);\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) Transport finished.\");\r\n }\r\n }\r\n\r\n public async send(data: any): Promise {\r\n if (!this.running) {\r\n return Promise.reject(new Error(\"Cannot send until the transport is connected\"));\r\n }\r\n return sendMessage(this.logger, \"LongPolling\", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent);\r\n }\r\n\r\n public async stop(): Promise {\r\n // Send a DELETE request to stop the poll\r\n try {\r\n this.running = false;\r\n this.logger.log(LogLevel.Trace, `(LongPolling transport) sending DELETE request to ${this.url}.`);\r\n\r\n const deleteOptions: HttpRequest = {\r\n headers: {},\r\n };\r\n const token = await this.accessTokenFactory();\r\n this.updateHeaderToken(deleteOptions, token);\r\n const response = await this.httpClient.delete(this.url, deleteOptions);\r\n\r\n this.logger.log(LogLevel.Trace, \"(LongPolling transport) DELETE request accepted.\");\r\n } finally {\r\n // Abort the poll after the shutdown timeout if the server doesn't stop the poll.\r\n if (!this.stopped) {\r\n this.shutdownTimer = setTimeout(() => {\r\n this.logger.log(LogLevel.Warning, \"(LongPolling transport) server did not terminate after DELETE request, canceling poll.\");\r\n\r\n // Abort any outstanding poll\r\n this.pollAbort.abort();\r\n }, this.shutdownTimeout);\r\n }\r\n }\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpClient } from \"./HttpClient\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail, sendMessage } from \"./Utils\";\r\n\r\nexport class ServerSentEventsTransport implements ITransport {\r\n private readonly httpClient: HttpClient;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logger: ILogger;\r\n private readonly logMessageContent: boolean;\r\n private eventSource: EventSource;\r\n private url: string;\r\n\r\n constructor(httpClient: HttpClient, accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean) {\r\n this.httpClient = httpClient;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logger = logger;\r\n this.logMessageContent = logMessageContent;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n if (typeof (EventSource) === \"undefined\") {\r\n throw new Error(\"'EventSource' is not supported in your environment.\");\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(SSE transport) Connecting\");\r\n\r\n const token = await this.accessTokenFactory();\r\n if (token) {\r\n url += (url.indexOf(\"?\") < 0 ? \"?\" : \"&\") + `access_token=${encodeURIComponent(token)}`;\r\n }\r\n\r\n this.url = url;\r\n return new Promise((resolve, reject) => {\r\n let opened = false;\r\n if (transferFormat !== TransferFormat.Text) {\r\n reject(new Error(\"The Server-Sent Events transport only supports the 'Text' transfer format\"));\r\n }\r\n\r\n const eventSource = new EventSource(url, { withCredentials: true });\r\n\r\n try {\r\n eventSource.onmessage = (e: MessageEvent) => {\r\n if (this.onreceive) {\r\n try {\r\n this.logger.log(LogLevel.Trace, `(SSE transport) data received. ${getDataDetail(e.data, this.logMessageContent)}.`);\r\n this.onreceive(e.data);\r\n } catch (error) {\r\n if (this.onclose) {\r\n this.onclose(error);\r\n }\r\n return;\r\n }\r\n }\r\n };\r\n\r\n eventSource.onerror = (e: any) => {\r\n const error = new Error(e.message || \"Error occurred\");\r\n if (opened) {\r\n this.close(error);\r\n } else {\r\n reject(error);\r\n }\r\n };\r\n\r\n eventSource.onopen = () => {\r\n this.logger.log(LogLevel.Information, `SSE connected to ${this.url}`);\r\n this.eventSource = eventSource;\r\n opened = true;\r\n resolve();\r\n };\r\n } catch (e) {\r\n return Promise.reject(e);\r\n }\r\n });\r\n }\r\n\r\n public async send(data: any): Promise {\r\n if (!this.eventSource) {\r\n return Promise.reject(new Error(\"Cannot send until the transport is connected\"));\r\n }\r\n return sendMessage(this.logger, \"SSE\", this.httpClient, this.url, this.accessTokenFactory, data, this.logMessageContent);\r\n }\r\n\r\n public stop(): Promise {\r\n this.close();\r\n return Promise.resolve();\r\n }\r\n\r\n private close(e?: Error) {\r\n if (this.eventSource) {\r\n this.eventSource.close();\r\n this.eventSource = null;\r\n\r\n if (this.onclose) {\r\n this.onclose(e);\r\n }\r\n }\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { ITransport, TransferFormat } from \"./ITransport\";\r\nimport { Arg, getDataDetail } from \"./Utils\";\r\n\r\nexport class WebSocketTransport implements ITransport {\r\n private readonly logger: ILogger;\r\n private readonly accessTokenFactory: () => string | Promise;\r\n private readonly logMessageContent: boolean;\r\n private webSocket: WebSocket;\r\n\r\n constructor(accessTokenFactory: () => string | Promise, logger: ILogger, logMessageContent: boolean) {\r\n this.logger = logger;\r\n this.accessTokenFactory = accessTokenFactory || (() => null);\r\n this.logMessageContent = logMessageContent;\r\n }\r\n\r\n public async connect(url: string, transferFormat: TransferFormat): Promise {\r\n Arg.isRequired(url, \"url\");\r\n Arg.isRequired(transferFormat, \"transferFormat\");\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n if (typeof (WebSocket) === \"undefined\") {\r\n throw new Error(\"'WebSocket' is not supported in your environment.\");\r\n }\r\n\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) Connecting\");\r\n\r\n const token = await this.accessTokenFactory();\r\n if (token) {\r\n url += (url.indexOf(\"?\") < 0 ? \"?\" : \"&\") + `access_token=${encodeURIComponent(token)}`;\r\n }\r\n\r\n return new Promise((resolve, reject) => {\r\n url = url.replace(/^http/, \"ws\");\r\n const webSocket = new WebSocket(url);\r\n if (transferFormat === TransferFormat.Binary) {\r\n webSocket.binaryType = \"arraybuffer\";\r\n }\r\n\r\n webSocket.onopen = (event: Event) => {\r\n this.logger.log(LogLevel.Information, `WebSocket connected to ${url}`);\r\n this.webSocket = webSocket;\r\n resolve();\r\n };\r\n\r\n webSocket.onerror = (event: ErrorEvent) => {\r\n reject(event.error);\r\n };\r\n\r\n webSocket.onmessage = (message: MessageEvent) => {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) data received. ${getDataDetail(message.data, this.logMessageContent)}.`);\r\n if (this.onreceive) {\r\n this.onreceive(message.data);\r\n }\r\n };\r\n\r\n webSocket.onclose = (event: CloseEvent) => {\r\n // webSocket will be null if the transport did not start successfully\r\n this.logger.log(LogLevel.Trace, \"(WebSockets transport) socket closed.\");\r\n if (this.onclose) {\r\n if (event.wasClean === false || event.code !== 1000) {\r\n this.onclose(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`));\r\n } else {\r\n this.onclose();\r\n }\r\n }\r\n };\r\n });\r\n }\r\n\r\n public send(data: any): Promise {\r\n if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {\r\n this.logger.log(LogLevel.Trace, `(WebSockets transport) sending data. ${getDataDetail(data, this.logMessageContent)}.`);\r\n this.webSocket.send(data);\r\n return Promise.resolve();\r\n }\r\n\r\n return Promise.reject(\"WebSocket is not in the OPEN state\");\r\n }\r\n\r\n public stop(): Promise {\r\n if (this.webSocket) {\r\n this.webSocket.close();\r\n this.webSocket = null;\r\n }\r\n return Promise.resolve();\r\n }\r\n\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (error?: Error) => void;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { DefaultHttpClient, HttpClient } from \"./HttpClient\";\r\nimport { IConnection } from \"./IConnection\";\r\nimport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { HttpTransportType, ITransport, TransferFormat } from \"./ITransport\";\r\nimport { LongPollingTransport } from \"./LongPollingTransport\";\r\nimport { ServerSentEventsTransport } from \"./ServerSentEventsTransport\";\r\nimport { Arg, createLogger } from \"./Utils\";\r\nimport { WebSocketTransport } from \"./WebSocketTransport\";\r\n\r\nconst enum ConnectionState {\r\n Connecting,\r\n Connected,\r\n Disconnected,\r\n}\r\n\r\nexport interface INegotiateResponse {\r\n connectionId?: string;\r\n availableTransports?: IAvailableTransport[];\r\n url?: string;\r\n accessToken?: string;\r\n}\r\n\r\nexport interface IAvailableTransport {\r\n transport: keyof typeof HttpTransportType;\r\n transferFormats: Array;\r\n}\r\n\r\nconst MAX_REDIRECTS = 100;\r\n\r\nexport class HttpConnection implements IConnection {\r\n private connectionState: ConnectionState;\r\n private baseUrl: string;\r\n private readonly httpClient: HttpClient;\r\n private readonly logger: ILogger;\r\n private readonly options: IHttpConnectionOptions;\r\n private transport: ITransport;\r\n private startPromise: Promise;\r\n private stopError?: Error;\r\n private accessTokenFactory?: () => string | Promise;\r\n\r\n public readonly features: any = {};\r\n public onreceive: (data: string | ArrayBuffer) => void;\r\n public onclose: (e?: Error) => void;\r\n\r\n constructor(url: string, options: IHttpConnectionOptions = {}) {\r\n Arg.isRequired(url, \"url\");\r\n\r\n this.logger = createLogger(options.logger);\r\n this.baseUrl = this.resolveUrl(url);\r\n\r\n options = options || {};\r\n options.accessTokenFactory = options.accessTokenFactory || (() => null);\r\n options.logMessageContent = options.logMessageContent || false;\r\n\r\n this.httpClient = options.httpClient || new DefaultHttpClient(this.logger);\r\n this.connectionState = ConnectionState.Disconnected;\r\n this.options = options;\r\n }\r\n\r\n public start(): Promise;\r\n public start(transferFormat: TransferFormat): Promise;\r\n public start(transferFormat?: TransferFormat): Promise {\r\n transferFormat = transferFormat || TransferFormat.Binary;\r\n\r\n Arg.isIn(transferFormat, TransferFormat, \"transferFormat\");\r\n\r\n this.logger.log(LogLevel.Debug, `Starting connection with transfer format '${TransferFormat[transferFormat]}'.`);\r\n\r\n if (this.connectionState !== ConnectionState.Disconnected) {\r\n return Promise.reject(new Error(\"Cannot start a connection that is not in the 'Disconnected' state.\"));\r\n }\r\n\r\n this.connectionState = ConnectionState.Connecting;\r\n\r\n this.startPromise = this.startInternal(transferFormat);\r\n return this.startPromise;\r\n }\r\n\r\n public send(data: string | ArrayBuffer): Promise {\r\n if (this.connectionState !== ConnectionState.Connected) {\r\n throw new Error(\"Cannot send data if the connection is not in the 'Connected' State.\");\r\n }\r\n\r\n return this.transport.send(data);\r\n }\r\n\r\n public async stop(error?: Error): Promise {\r\n this.connectionState = ConnectionState.Disconnected;\r\n\r\n try {\r\n await this.startPromise;\r\n } catch (e) {\r\n // this exception is returned to the user as a rejected Promise from the start method\r\n }\r\n\r\n // The transport's onclose will trigger stopConnection which will run our onclose event.\r\n if (this.transport) {\r\n this.stopError = error;\r\n await this.transport.stop();\r\n this.transport = null;\r\n }\r\n }\r\n\r\n private async startInternal(transferFormat: TransferFormat): Promise {\r\n // Store the original base url and the access token factory since they may change\r\n // as part of negotiating\r\n let url = this.baseUrl;\r\n this.accessTokenFactory = this.options.accessTokenFactory;\r\n\r\n try {\r\n if (this.options.skipNegotiation) {\r\n if (this.options.transport === HttpTransportType.WebSockets) {\r\n // No need to add a connection ID in this case\r\n this.transport = this.constructTransport(HttpTransportType.WebSockets);\r\n // We should just call connect directly in this case.\r\n // No fallback or negotiate in this case.\r\n await this.transport.connect(url, transferFormat);\r\n } else {\r\n throw Error(\"Negotiation can only be skipped when using the WebSocket transport directly.\");\r\n }\r\n } else {\r\n let negotiateResponse: INegotiateResponse = null;\r\n let redirects = 0;\r\n\r\n do {\r\n negotiateResponse = await this.getNegotiationResponse(url);\r\n // the user tries to stop the connection when it is being started\r\n if (this.connectionState === ConnectionState.Disconnected) {\r\n return;\r\n }\r\n\r\n if (negotiateResponse.url) {\r\n url = negotiateResponse.url;\r\n }\r\n\r\n if (negotiateResponse.accessToken) {\r\n // Replace the current access token factory with one that uses\r\n // the returned access token\r\n const accessToken = negotiateResponse.accessToken;\r\n this.accessTokenFactory = () => accessToken;\r\n }\r\n\r\n redirects++;\r\n }\r\n while (negotiateResponse.url && redirects < MAX_REDIRECTS);\r\n\r\n if (redirects === MAX_REDIRECTS && negotiateResponse.url) {\r\n throw Error(\"Negotiate redirection limit exceeded.\");\r\n }\r\n\r\n await this.createTransport(url, this.options.transport, negotiateResponse, transferFormat);\r\n }\r\n\r\n if (this.transport instanceof LongPollingTransport) {\r\n this.features.inherentKeepAlive = true;\r\n }\r\n\r\n this.transport.onreceive = this.onreceive;\r\n this.transport.onclose = (e) => this.stopConnection(e);\r\n\r\n // only change the state if we were connecting to not overwrite\r\n // the state if the connection is already marked as Disconnected\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n } catch (e) {\r\n this.logger.log(LogLevel.Error, \"Failed to start the connection: \" + e);\r\n this.connectionState = ConnectionState.Disconnected;\r\n this.transport = null;\r\n throw e;\r\n }\r\n }\r\n\r\n private async getNegotiationResponse(url: string): Promise {\r\n const token = await this.accessTokenFactory();\r\n let headers;\r\n if (token) {\r\n headers = {\r\n [\"Authorization\"]: `Bearer ${token}`,\r\n };\r\n }\r\n\r\n const negotiateUrl = this.resolveNegotiateUrl(url);\r\n this.logger.log(LogLevel.Debug, `Sending negotiation request: ${negotiateUrl}`);\r\n try {\r\n const response = await this.httpClient.post(negotiateUrl, {\r\n content: \"\",\r\n headers,\r\n });\r\n\r\n if (response.statusCode !== 200) {\r\n throw Error(`Unexpected status code returned from negotiate ${response.statusCode}`);\r\n }\r\n\r\n return JSON.parse(response.content as string) as INegotiateResponse;\r\n } catch (e) {\r\n this.logger.log(LogLevel.Error, \"Failed to complete negotiation with the server: \" + e);\r\n throw e;\r\n }\r\n }\r\n\r\n private createConnectUrl(url: string, connectionId: string) {\r\n return url + (url.indexOf(\"?\") === -1 ? \"?\" : \"&\") + `id=${connectionId}`;\r\n }\r\n\r\n private async createTransport(url: string, requestedTransport: HttpTransportType | ITransport, negotiateResponse: INegotiateResponse, requestedTransferFormat: TransferFormat): Promise {\r\n let connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);\r\n if (this.isITransport(requestedTransport)) {\r\n this.logger.log(LogLevel.Debug, \"Connection was provided an instance of ITransport, using that directly.\");\r\n this.transport = requestedTransport;\r\n await this.transport.connect(connectUrl, requestedTransferFormat);\r\n\r\n // only change the state if we were connecting to not overwrite\r\n // the state if the connection is already marked as Disconnected\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n return;\r\n }\r\n\r\n const transports = negotiateResponse.availableTransports;\r\n for (const endpoint of transports) {\r\n this.connectionState = ConnectionState.Connecting;\r\n const transport = this.resolveTransport(endpoint, requestedTransport, requestedTransferFormat);\r\n if (typeof transport === \"number\") {\r\n this.transport = this.constructTransport(transport);\r\n if (negotiateResponse.connectionId === null) {\r\n negotiateResponse = await this.getNegotiationResponse(url);\r\n connectUrl = this.createConnectUrl(url, negotiateResponse.connectionId);\r\n }\r\n try {\r\n await this.transport.connect(connectUrl, requestedTransferFormat);\r\n this.changeState(ConnectionState.Connecting, ConnectionState.Connected);\r\n return;\r\n } catch (ex) {\r\n this.logger.log(LogLevel.Error, `Failed to start the transport '${HttpTransportType[transport]}': ${ex}`);\r\n this.connectionState = ConnectionState.Disconnected;\r\n negotiateResponse.connectionId = null;\r\n }\r\n }\r\n }\r\n\r\n throw new Error(\"Unable to initialize any of the available transports.\");\r\n }\r\n\r\n private constructTransport(transport: HttpTransportType) {\r\n switch (transport) {\r\n case HttpTransportType.WebSockets:\r\n return new WebSocketTransport(this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n case HttpTransportType.ServerSentEvents:\r\n return new ServerSentEventsTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n case HttpTransportType.LongPolling:\r\n return new LongPollingTransport(this.httpClient, this.accessTokenFactory, this.logger, this.options.logMessageContent);\r\n default:\r\n throw new Error(`Unknown transport: ${transport}.`);\r\n }\r\n }\r\n\r\n private resolveTransport(endpoint: IAvailableTransport, requestedTransport: HttpTransportType, requestedTransferFormat: TransferFormat): HttpTransportType | null {\r\n const transport = HttpTransportType[endpoint.transport];\r\n if (transport === null || transport === undefined) {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${endpoint.transport}' because it is not supported by this client.`);\r\n } else {\r\n const transferFormats = endpoint.transferFormats.map((s) => TransferFormat[s]);\r\n if (transportMatches(requestedTransport, transport)) {\r\n if (transferFormats.indexOf(requestedTransferFormat) >= 0) {\r\n if ((transport === HttpTransportType.WebSockets && typeof WebSocket === \"undefined\") ||\r\n (transport === HttpTransportType.ServerSentEvents && typeof EventSource === \"undefined\")) {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it is not supported in your environment.'`);\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Selecting transport '${HttpTransportType[transport]}'`);\r\n return transport;\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it does not support the requested transfer format '${TransferFormat[requestedTransferFormat]}'.`);\r\n }\r\n } else {\r\n this.logger.log(LogLevel.Debug, `Skipping transport '${HttpTransportType[transport]}' because it was disabled by the client.`);\r\n }\r\n }\r\n return null;\r\n }\r\n\r\n private isITransport(transport: any): transport is ITransport {\r\n return transport && typeof (transport) === \"object\" && \"connect\" in transport;\r\n }\r\n\r\n private changeState(from: ConnectionState, to: ConnectionState): boolean {\r\n if (this.connectionState === from) {\r\n this.connectionState = to;\r\n return true;\r\n }\r\n return false;\r\n }\r\n\r\n private async stopConnection(error?: Error): Promise {\r\n this.transport = null;\r\n\r\n // If we have a stopError, it takes precedence over the error from the transport\r\n error = this.stopError || error;\r\n\r\n if (error) {\r\n this.logger.log(LogLevel.Error, `Connection disconnected with error '${error}'.`);\r\n } else {\r\n this.logger.log(LogLevel.Information, \"Connection disconnected.\");\r\n }\r\n\r\n this.connectionState = ConnectionState.Disconnected;\r\n\r\n if (this.onclose) {\r\n this.onclose(error);\r\n }\r\n }\r\n\r\n private resolveUrl(url: string): string {\r\n // startsWith is not supported in IE\r\n if (url.lastIndexOf(\"https://\", 0) === 0 || url.lastIndexOf(\"http://\", 0) === 0) {\r\n return url;\r\n }\r\n\r\n if (typeof window === \"undefined\" || !window || !window.document) {\r\n throw new Error(`Cannot resolve '${url}'.`);\r\n }\r\n\r\n // Setting the url to the href propery of an anchor tag handles normalization\r\n // for us. There are 3 main cases.\r\n // 1. Relative path normalization e.g \"b\" -> \"http://localhost:5000/a/b\"\r\n // 2. Absolute path normalization e.g \"/a/b\" -> \"http://localhost:5000/a/b\"\r\n // 3. Networkpath reference normalization e.g \"//localhost:5000/a/b\" -> \"http://localhost:5000/a/b\"\r\n const aTag = window.document.createElement(\"a\");\r\n aTag.href = url;\r\n\r\n this.logger.log(LogLevel.Information, `Normalizing '${url}' to '${aTag.href}'.`);\r\n return aTag.href;\r\n }\r\n\r\n private resolveNegotiateUrl(url: string): string {\r\n const index = url.indexOf(\"?\");\r\n let negotiateUrl = url.substring(0, index === -1 ? url.length : index);\r\n if (negotiateUrl[negotiateUrl.length - 1] !== \"/\") {\r\n negotiateUrl += \"/\";\r\n }\r\n negotiateUrl += \"negotiate\";\r\n negotiateUrl += index === -1 ? \"\" : url.substring(index);\r\n return negotiateUrl;\r\n }\r\n}\r\n\r\nfunction transportMatches(requestedTransport: HttpTransportType, actualTransport: HttpTransportType) {\r\n return !requestedTransport || ((actualTransport & requestedTransport) !== 0);\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { CompletionMessage, HubMessage, IHubProtocol, InvocationMessage, MessageType, StreamItemMessage } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { TransferFormat } from \"./ITransport\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { TextMessageFormat } from \"./TextMessageFormat\";\r\n\r\nconst JSON_HUB_PROTOCOL_NAME: string = \"json\";\r\n\r\n/** Implements the JSON Hub Protocol. */\r\nexport class JsonHubProtocol implements IHubProtocol {\r\n\r\n /** @inheritDoc */\r\n public readonly name: string = JSON_HUB_PROTOCOL_NAME;\r\n /** @inheritDoc */\r\n public readonly version: number = 1;\r\n\r\n /** @inheritDoc */\r\n public readonly transferFormat: TransferFormat = TransferFormat.Text;\r\n\r\n /** Creates an array of {@link HubMessage} objects from the specified serialized representation.\r\n *\r\n * @param {string} input A string containing the serialized representation.\r\n * @param {ILogger} logger A logger that will be used to log messages that occur during parsing.\r\n */\r\n public parseMessages(input: string, logger: ILogger): HubMessage[] {\r\n // The interface does allow \"ArrayBuffer\" to be passed in, but this implementation does not. So let's throw a useful error.\r\n if (typeof input !== \"string\") {\r\n throw new Error(\"Invalid input for JSON hub protocol. Expected a string.\");\r\n }\r\n\r\n if (!input) {\r\n return [];\r\n }\r\n\r\n if (logger === null) {\r\n logger = NullLogger.instance;\r\n }\r\n\r\n // Parse the messages\r\n const messages = TextMessageFormat.parse(input);\r\n\r\n const hubMessages = [];\r\n for (const message of messages) {\r\n const parsedMessage = JSON.parse(message) as HubMessage;\r\n if (typeof parsedMessage.type !== \"number\") {\r\n throw new Error(\"Invalid payload.\");\r\n }\r\n switch (parsedMessage.type) {\r\n case MessageType.Invocation:\r\n this.isInvocationMessage(parsedMessage);\r\n break;\r\n case MessageType.StreamItem:\r\n this.isStreamItemMessage(parsedMessage);\r\n break;\r\n case MessageType.Completion:\r\n this.isCompletionMessage(parsedMessage);\r\n break;\r\n case MessageType.Ping:\r\n // Single value, no need to validate\r\n break;\r\n case MessageType.Close:\r\n // All optional values, no need to validate\r\n break;\r\n default:\r\n // Future protocol changes can add message types, old clients can ignore them\r\n logger.log(LogLevel.Information, \"Unknown message type '\" + parsedMessage.type + \"' ignored.\");\r\n continue;\r\n }\r\n hubMessages.push(parsedMessage);\r\n }\r\n\r\n return hubMessages;\r\n }\r\n\r\n /** Writes the specified {@link HubMessage} to a string and returns it.\r\n *\r\n * @param {HubMessage} message The message to write.\r\n * @returns {string} A string containing the serialized representation of the message.\r\n */\r\n public writeMessage(message: HubMessage): string {\r\n return TextMessageFormat.write(JSON.stringify(message));\r\n }\r\n\r\n private isInvocationMessage(message: InvocationMessage): void {\r\n this.assertNotEmptyString(message.target, \"Invalid payload for Invocation message.\");\r\n\r\n if (message.invocationId !== undefined) {\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for Invocation message.\");\r\n }\r\n }\r\n\r\n private isStreamItemMessage(message: StreamItemMessage): void {\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for StreamItem message.\");\r\n\r\n if (message.item === undefined) {\r\n throw new Error(\"Invalid payload for StreamItem message.\");\r\n }\r\n }\r\n\r\n private isCompletionMessage(message: CompletionMessage): void {\r\n if (message.result && message.error) {\r\n throw new Error(\"Invalid payload for Completion message.\");\r\n }\r\n\r\n if (!message.result && message.error) {\r\n this.assertNotEmptyString(message.error, \"Invalid payload for Completion message.\");\r\n }\r\n\r\n this.assertNotEmptyString(message.invocationId, \"Invalid payload for Completion message.\");\r\n }\r\n\r\n private assertNotEmptyString(value: any, errorMessage: string): void {\r\n if (typeof value !== \"string\" || value === \"\") {\r\n throw new Error(errorMessage);\r\n }\r\n }\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\nimport { HttpConnection } from \"./HttpConnection\";\r\nimport { HubConnection } from \"./HubConnection\";\r\nimport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nimport { IHubProtocol } from \"./IHubProtocol\";\r\nimport { ILogger, LogLevel } from \"./ILogger\";\r\nimport { HttpTransportType } from \"./ITransport\";\r\nimport { JsonHubProtocol } from \"./JsonHubProtocol\";\r\nimport { NullLogger } from \"./Loggers\";\r\nimport { Arg, ConsoleLogger } from \"./Utils\";\r\n\r\n/** A builder for configuring {@link HubConnection} instances. */\r\nexport class HubConnectionBuilder {\r\n /** @internal */\r\n public protocol: IHubProtocol;\r\n /** @internal */\r\n public httpConnectionOptions: IHttpConnectionOptions;\r\n /** @internal */\r\n public url: string;\r\n /** @internal */\r\n public logger: ILogger;\r\n\r\n /** Configures console logging for the {@link HubConnection}.\r\n *\r\n * @param {LogLevel} logLevel The minimum level of messages to log. Anything at this level, or a more severe level, will be logged.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public configureLogging(logLevel: LogLevel): HubConnectionBuilder;\r\n\r\n /** Configures custom logging for the {@link HubConnection}.\r\n *\r\n * @param {ILogger} logger An object implementing the {@link ILogger} interface, which will be used to write all log messages.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public configureLogging(logger: ILogger): HubConnectionBuilder;\r\n public configureLogging(logging: LogLevel | ILogger): HubConnectionBuilder {\r\n Arg.isRequired(logging, \"logging\");\r\n\r\n if (isLogger(logging)) {\r\n this.logger = logging;\r\n } else {\r\n this.logger = new ConsoleLogger(logging);\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /** Configures the {@link HubConnection} to use HTTP-based transports to connect to the specified URL.\r\n *\r\n * The transport will be selected automatically based on what the server and client support.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string): HubConnectionBuilder;\r\n\r\n /** Configures the {@link HubConnection} to use the specified HTTP-based transport to connect to the specified URL.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @param {HttpTransportType} transportType The specific transport to use.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string, transportType: HttpTransportType): HubConnectionBuilder;\r\n\r\n /** Configures the {@link HubConnection} to use HTTP-based transports to connect to the specified URL.\r\n *\r\n * @param {string} url The URL the connection will use.\r\n * @param {IHttpConnectionOptions} options An options object used to configure the connection.\r\n * @returns The {@link HubConnectionBuilder} instance, for chaining.\r\n */\r\n public withUrl(url: string, options: IHttpConnectionOptions): HubConnectionBuilder;\r\n public withUrl(url: string, transportTypeOrOptions?: IHttpConnectionOptions | HttpTransportType): HubConnectionBuilder {\r\n Arg.isRequired(url, \"url\");\r\n\r\n this.url = url;\r\n\r\n // Flow-typing knows where it's at. Since HttpTransportType is a number and IHttpConnectionOptions is guaranteed\r\n // to be an object, we know (as does TypeScript) this comparison is all we need to figure out which overload was called.\r\n if (typeof transportTypeOrOptions === \"object\") {\r\n this.httpConnectionOptions = transportTypeOrOptions;\r\n } else {\r\n this.httpConnectionOptions = {\r\n transport: transportTypeOrOptions,\r\n };\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /** Configures the {@link HubConnection} to use the specified Hub Protocol.\r\n *\r\n * @param {IHubProtocol} protocol The {@link IHubProtocol} implementation to use.\r\n */\r\n public withHubProtocol(protocol: IHubProtocol): HubConnectionBuilder {\r\n Arg.isRequired(protocol, \"protocol\");\r\n\r\n this.protocol = protocol;\r\n return this;\r\n }\r\n\r\n /** Creates a {@link HubConnection} from the configuration options specified in this builder.\r\n *\r\n * @returns {HubConnection} The configured {@link HubConnection}.\r\n */\r\n public build(): HubConnection {\r\n // If httpConnectionOptions has a logger, use it. Otherwise, override it with the one\r\n // provided to configureLogger\r\n const httpConnectionOptions = this.httpConnectionOptions || {};\r\n\r\n // If it's 'null', the user **explicitly** asked for null, don't mess with it.\r\n if (httpConnectionOptions.logger === undefined) {\r\n // If our logger is undefined or null, that's OK, the HttpConnection constructor will handle it.\r\n httpConnectionOptions.logger = this.logger;\r\n }\r\n\r\n // Now create the connection\r\n if (!this.url) {\r\n throw new Error(\"The 'HubConnectionBuilder.withUrl' method must be called before building the connection.\");\r\n }\r\n const connection = new HttpConnection(this.url, httpConnectionOptions);\r\n\r\n return HubConnection.create(\r\n connection,\r\n this.logger || NullLogger.instance,\r\n this.protocol || new JsonHubProtocol());\r\n }\r\n}\r\n\r\nfunction isLogger(logger: any): logger is ILogger {\r\n return logger.log !== undefined;\r\n}\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// Version token that will be replaced by the prepack command\r\n/** The version of the SignalR client. */\r\nexport const VERSION: string = \"0.0.0-DEV_BUILD\";\r\n\r\n// Everything that users need to access must be exported here. Including interfaces.\r\nexport { AbortSignal } from \"./AbortController\";\r\nexport { HttpError, TimeoutError } from \"./Errors\";\r\nexport { DefaultHttpClient, HttpClient, HttpRequest, HttpResponse } from \"./HttpClient\";\r\nexport { IHttpConnectionOptions } from \"./IHttpConnectionOptions\";\r\nexport { HubConnection } from \"./HubConnection\";\r\nexport { HubConnectionBuilder } from \"./HubConnectionBuilder\";\r\nexport { MessageType, MessageHeaders, HubMessage, HubMessageBase, HubInvocationMessage, InvocationMessage, StreamInvocationMessage, StreamItemMessage, CompletionMessage, PingMessage, CloseMessage, CancelInvocationMessage, IHubProtocol } from \"./IHubProtocol\";\r\nexport { ILogger, LogLevel } from \"./ILogger\";\r\nexport { HttpTransportType, TransferFormat, ITransport } from \"./ITransport\";\r\nexport { IStreamSubscriber, IStreamResult, ISubscription } from \"./Stream\";\r\nexport { NullLogger } from \"./Loggers\";\r\nexport { JsonHubProtocol } from \"./JsonHubProtocol\";\r\n","// Copyright (c) .NET Foundation. All rights reserved.\r\n// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.\r\n\r\n// This is where we add any polyfills we'll need for the browser. It is the entry module for browser-specific builds.\r\n\r\nimport \"es6-promise/dist/es6-promise.auto.js\";\r\n\r\n// Copy from Array.prototype into Uint8Array to polyfill on IE. It's OK because the implementations of indexOf and slice use properties\r\n// that exist on Uint8Array with the same name, and JavaScript is magic.\r\n// We make them 'writable' because the Buffer polyfill messes with it as well.\r\nif (!Uint8Array.prototype.indexOf) {\r\n Object.defineProperty(Uint8Array.prototype, \"indexOf\", {\r\n value: Array.prototype.indexOf,\r\n writable: true,\r\n });\r\n}\r\nif (!Uint8Array.prototype.slice) {\r\n Object.defineProperty(Uint8Array.prototype, \"slice\", {\r\n value: Array.prototype.slice,\r\n writable: true,\r\n });\r\n}\r\nif (!Uint8Array.prototype.forEach) {\r\n Object.defineProperty(Uint8Array.prototype, \"forEach\", {\r\n value: Array.prototype.forEach,\r\n writable: true,\r\n });\r\n}\r\n\r\nexport * from \"./index\";\r\n"]} \ No newline at end of file diff --git a/specs/ServiceProtocol.md b/specs/ServiceProtocol.md new file mode 100644 index 000000000..880e0e089 --- /dev/null +++ b/specs/ServiceProtocol.md @@ -0,0 +1,242 @@ +# Azure SignalR Service Protocol + +The Azure SignalR Service Protocol is a protocol between Azure SignalR Service and user application (server side) to provide an abstract transport between application clients and application server. + +## Terms + +- Service - Azure SignalR Service. It accepts connections from both clients and servers, acting as the abstract transport between them. It will internally maintain a one-to-one mapping between clients and servers, to make sure that messages are correctly routed to the recipients as if it is a physical transport. +- Server - Application server node, which is connected to the Azure SignalR Service, using this protocol to receive data from and send data to clients through Azure SignalR Service. +- Client - The SignalR client connected to the Azure SignalR Service. The Azure SignalR Service will look exactly the same as a self-hosted SignalR server from the client's perspective. + +## Overview + +Azure SignalR Service Protocol uses WebSockets and MessagePack to proxy messages between Service and Server. +Messages are categorized into three groups: + +### Service Connection Message + +These messages are used to establish and maintain the physical connection between Service and Server. + +Message Name | Sender | Description +---|---|--- +HandshakeRequest | Server | Sent by Server to negotiate the protocol version before the physical connection is established. +HandshakeResponse | Service | Sent by Service to tell Server whether the requested protocol version is supported. If yes, connection will be successfully established. Otherwise, connection will be closed. +Ping | Service or Server | Sent by either side to check the connection is alive. + +### Generic Client Connection Message + + Multiple logical client connections will be multiplexed in one or a few (far less than the number of client connections) physical connections between Service and Server. These messages are used to operate a single logical client connection within a physical connection. + +Message Name | Sender | Description +---|---|--- +OpenConnection| Service | Sent by Service to notify Server there is a new client connected. +CloseConnection | Service or Server | Sent by either side to notify the other side that the specified connection should be closed. +ConnectionData | Service or Server | When sent from Service to Server, it contains data from a single client. When sent from Server to Service, it contains data which should be delivered to a single client. + +### SignalR-specific Message + +These messages map to the various operations in the SignalR framework. + +Message Name | Sender | Description +---|---|--- +MultiConnectionData | Server | Sent from Server to Service. Payloads in the message will be sent to multiple connections by Service. +UserData | Server | Sent from Server to Service. Payloads in the message will be sent to the target user (possible multiple connections) by Service. +MultiUserData | Server | Sent from Server to Service. Payloads in the message will be sent to multiple connections by Service. +BroadcastData | Server | Sent from Server to Service. Payloads in the message will be broadcasted to all connected clients by Service. +JoinGroup | Server | Sent by server to ask Service adding the target connection to the target group. +LeaveGroup | Server | Sent by server to ask Service removing the target connection from the target group. +GroupBroadcastData | Server | Sent from Server to Service. Payloads in the message will be broadcasted to all connections within the target group by Service. +MultiGroupBroadcastData | Server | Sent from Server to Service. Payloads in the message will be broadcasted to all connections within the target groups by Service. + +## Communication Model + +This protocol will be used between Service and Server. There will be one or a few physical connections between Service and Server. Data from/to multiple client connections will be multiplexed within these physical connections. Each client connection will be identified by a unique connection Id. + +The number of client connections will be far more (over 100 times) than the number of physical connections between Service and Server. + +### Handshake + +Server will initiate a physical connection to Service, using WebSocket transport. Once the WebSocket connection is established, Server will send a `HandshakeRequest` message with a requested version number of Azure SignalR Service Protocol to service. +- If the protocol version is supported, Service will send a success `HandshakeResponse` message to Server. Then the communication begins. +- Otherwise, Service will send a `HandshakeResponse` message with error, and the physical connection will be closed. + +### New Client Connect + +When a new client is connected to Service, a `OpenConnection` message will be sent by Service to Server. + +### Client Disconnect + +- When a client is disconnected from Service, a `CloseConnection` message will be sent by Service to Server. +- When Server wants to disconnect a client, a `CloseConnection` message will be sent by Server to Service. Then Service will disconnect the phyical connection with the target client. + +### Client Data Pass Through + +- When a client sends data to Service, a `ConnectionData` message will be sent by Service to Server. +- When Server wants to send data to a client, a `ConnectionData` message will be sent by Server to Service. + +### SignalR scenarios + +Service supports various scenarios in SignalR to send data from Server to multiple clients. +- When Server wants to send data to a specific set of connections, a `MultiConnectionData` message is sent to Service, containing the list of the target connections. +- When Server wants to send data to a specific user, a `UserData` message is sent to Service. +- When Server wants to send data to a specific set of users, a `MultiUserData` message is sent to Service, containing the list of the target users. +- When Server wants to send data to all clients, a `BroadcastData` message is sent to Service. +- When Server wants to send data to a specific group, a `GroupBroadcastData` message is sent to Service. +- When Server wants to send data to a couple of groups, a `MultiGroupBroadcastData` message is sent to Service. + +## Message Encodings + +In Azure SignalR Service Protocol, each message is represented as a single MessagePack array containing items that correspond to properties of the given service message. + +MessagePack uses different formats to encode values. Refer to the [MessagePack Format Spec](https://github.com/msgpack/msgpack/blob/master/spec.md#formats) for format definitions. + +### HandshakeRequest Message +`HandshakeRequest` messages have the following structure. +``` +[1, Version] +``` +- 1 - Message Type, indicating this is a `HandshakeRequest` message. +- Version - A `Int32` encoding number of the protocol version. + +#### Example: TODO + +### HandshakeResponse Message +`HandshakeResponse` messages have the following structure. +``` +[2, ErrorMessage] +``` +- 2 - Message Type, indicating this is a `HandshakeResponse` message. +- ErrorMessage - A `String` encoding error message. Null means handshake success, otherwise it means there is error. + +#### Example: TODO + +### Ping Message + `Ping` messages have the following structure. +``` +[3] +``` +- 3 - Message Type, indicating this is a `Ping` message. + +#### Example: TODO + +### OpenConnection Message +`OpenConnection` messages have the following structure. +``` +[4, ConnectionId, Claims] +``` +- 4 - Message Type, indicating this is a `OpenConnection` message. +- ConnectionId - A `String` encoding unique Id for the connection. +- Claims - A MessagePack Map containing all claims of this client. + +#### Example: TODO + +### CloseConnection Message +`CloseConnection` messages have the following structure. +``` +[5, ConnectionId, ErrorMessage] +``` +- 5 - Message Type, indicating this is a `CloseConnection` message. +- ConnectionId - A `String` encoding unique Id of the connection. +- ErrorMessage - Optional `String` encoding error message. + +#### Example: TODO + +### ConnectionData Message +`ConnectionData` messages have the following structure. +``` +[6, ConnectionId, Payload] +``` +- 6 - Message Type, indicating this is a `ConnectionData` message. +- ConnectionId - A `String` encoding unique Id for the connection. +- Payload - `Binary` encoding of the raw bytes from/to the connection. + +#### Example: TODO + +### MultiConnectionData Message +`MultiConnectionData` messages have the following structure. +``` +[7, ConnectionList, Payloads] +``` +- 7 - Message Type, indicating this is a `MultiConnectionData` message. +- ConnectionList - An array containing `String` encoding Ids of the target connections. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO + +### UserData Message +`UserData` messages have the following structure. +``` +[8, UserId, Payloads] +``` +- 8 - Message Type, indicating this is a `UserData` message. +- UserId - A `String` encoding unique Id for the user. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO + +### MultiUserData Message +`MultiUserData` messages have the following structure. +``` +[9, UserList, Payloads] +``` +- 9 - Message Type, indicating this is a `MultiUserData` message. +- UserList - An array containing `String` encoding Ids of the target users. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO + +### BroadcastData Message +`BroadcastData` messages have the following structure. +``` +[10, ExcludedList, Payloads] +``` +- 10 - Message Type, indicating this is a `BroadcastData` message. +- ExcludedList - An array containing `String` encoding Ids of the connections, which will not receive payload in this message. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO + +### JoinGroup Message +`JoinGroup` messages have the following structure. +``` +[11, ConnectionId, GroupName] +``` +- 11 - Message Type, indicating this is a `JoinGroup` message. +- ConnectionId - A `String` encoding unique Id for the connection. +- GroupName - A `String` encoding group name, which the connection will join. + +#### Example: TODO + +### LeaveGroup Message +`LeaveGroup` messages have the following structure. +``` +[12, ConnectionId, GroupName] +``` +- 12 - Message Type, indicating this is a `LeaveGroup` message. +- ConnectionId - A `String` encoding unique Id for the connection. +- GroupName - A `String` encoding group name, which the connection will leave. + +#### Example: TODO + +### GroupBroadcastData Message +`GroupBroadcastData` messages have the following structure. +``` +[13, GroupName, ExcludedList, Payloads] +``` +- 13 - Message Type, indicating this is a `GroupBroadcastData` message. +- GroupName - A `String` encoding target group name. +- ExcludedList - An array containing `String` encoding Ids of the connections, which will not receive payload in this message. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO + +### MultiGroupBroadcastData Message +`MultiGroupBroadcastData` messages have the following structure. +``` +[14, GroupList, Payloads] +``` +- 14 - Message Type, indicating this is a `MultiGroupBroadcastData` message. +- GroupList - An array containing `String` encoding target group names. +- Payloads - A MessagePack Map containing payloads, with string keys and byte array values. The key is the protocol name of the value. + +#### Example: TODO diff --git a/src/Common/BinaryMessageFormatter.cs b/src/Common/BinaryMessageFormatter.cs new file mode 100644 index 000000000..247f22eb6 --- /dev/null +++ b/src/Common/BinaryMessageFormatter.cs @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; + +namespace Microsoft.Azure.SignalR.Protocol +{ + internal static class BinaryMessageFormatter + { + public static void WriteLengthPrefix(long length, IBufferWriter output) + { + Span lenBuffer = stackalloc byte[5]; + + var lenNumBytes = WriteLengthPrefix(length, lenBuffer); + + output.Write(lenBuffer.Slice(0, lenNumBytes)); + } + + public static int WriteLengthPrefix(long length, Span output) + { + // This code writes length prefix of the message as a VarInt. Read the comment in + // the BinaryMessageParser.TryParseMessage for details. + var lenNumBytes = 0; + do + { + ref var current = ref output[lenNumBytes]; + current = (byte)(length & 0x7f); + length >>= 7; + if (length > 0) + { + current |= 0x80; + } + lenNumBytes++; + } + while (length > 0); + + return lenNumBytes; + } + + public static int LengthPrefixLength(long length) + { + var lenNumBytes = 0; + do + { + length >>= 7; + lenNumBytes++; + } + while (length > 0); + + return lenNumBytes; + } + } +} diff --git a/src/Common/BinaryMessageParser.cs b/src/Common/BinaryMessageParser.cs new file mode 100644 index 000000000..9089f5b84 --- /dev/null +++ b/src/Common/BinaryMessageParser.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; + +namespace Microsoft.Azure.SignalR.Protocol +{ + internal static class BinaryMessageParser + { + private const int MaxLengthPrefixSize = 5; + + public static bool TryParseMessage(ref ReadOnlySequence buffer, out ReadOnlySequence payload) + { + if (buffer.IsEmpty) + { + payload = default; + return false; + } + + // The payload starts with a length prefix encoded as a VarInt. VarInts use the most significant bit + // as a marker whether the byte is the last byte of the VarInt or if it spans to the next byte. Bytes + // appear in the reverse order - i.e. the first byte contains the least significant bits of the value + // Examples: + // VarInt: 0x35 - %00110101 - the most significant bit is 0 so the value is %x0110101 i.e. 0x35 (53) + // VarInt: 0x80 0x25 - %10000000 %00101001 - the most significant bit of the first byte is 1 so the + // remaining bits (%x0000000) are the lowest bits of the value. The most significant bit of the second + // byte is 0 meaning this is last byte of the VarInt. The actual value bits (%x0101001) need to be + // prepended to the bits we already read so the values is %01010010000000 i.e. 0x1480 (5248) + // We support paylads up to 2GB so the biggest number we support is 7fffffff which when encoded as + // VarInt is 0xFF 0xFF 0xFF 0xFF 0x07 - hence the maximum length prefix is 5 bytes. + + var length = 0U; + var numBytes = 0; + + var lengthPrefixBuffer = buffer.Slice(0, Math.Min(MaxLengthPrefixSize, buffer.Length)); + var span = GetSpan(lengthPrefixBuffer); + + byte byteRead; + do + { + byteRead = span[numBytes]; + length = length | (((uint)(byteRead & 0x7f)) << (numBytes * 7)); + numBytes++; + } + while (numBytes < lengthPrefixBuffer.Length && ((byteRead & 0x80) != 0)); + + // size bytes are missing + if ((byteRead & 0x80) != 0 && (numBytes < MaxLengthPrefixSize)) + { + payload = default; + return false; + } + + if ((byteRead & 0x80) != 0 || (numBytes == MaxLengthPrefixSize && byteRead > 7)) + { + throw new FormatException("Messages over 2GB in size are not supported."); + } + + // We don't have enough data + if (buffer.Length < length + numBytes) + { + payload = default; + return false; + } + + // Get the payload + payload = buffer.Slice(numBytes, (int)length); + + // Skip the payload + buffer = buffer.Slice(numBytes + (int)length); + return true; + } + + private static ReadOnlySpan GetSpan(in ReadOnlySequence lengthPrefixBuffer) + { + if (lengthPrefixBuffer.IsSingleSegment) + { + return lengthPrefixBuffer.First.Span; + } + + // Should be rare + return lengthPrefixBuffer.ToArray(); + } + } +} \ No newline at end of file diff --git a/src/Common/MemoryBufferWriter.cs b/src/Common/MemoryBufferWriter.cs new file mode 100644 index 000000000..53423592e --- /dev/null +++ b/src/Common/MemoryBufferWriter.cs @@ -0,0 +1,341 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Azure.SignalR +{ + internal sealed class MemoryBufferWriter : Stream, IBufferWriter + { + [ThreadStatic] + private static MemoryBufferWriter _cachedInstance; + +#if DEBUG + private bool _inUse; +#endif + + private readonly int _minimumSegmentSize; + private int _bytesWritten; + + private List _completedSegments; + private byte[] _currentSegment; + private int _position; + + public MemoryBufferWriter(int minimumSegmentSize = 4096) + { + _minimumSegmentSize = minimumSegmentSize; + } + + public override long Length => _bytesWritten; + public override bool CanRead => false; + public override bool CanSeek => false; + public override bool CanWrite => true; + public override long Position + { + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); + } + + public static MemoryBufferWriter Get() + { + var writer = _cachedInstance; + if (writer == null) + { + writer = new MemoryBufferWriter(); + } + else + { + // Taken off the thread static + _cachedInstance = null; + } +#if DEBUG + if (writer._inUse) + { + throw new InvalidOperationException("The reader wasn't returned!"); + } + + writer._inUse = true; +#endif + + return writer; + } + + public static void Return(MemoryBufferWriter writer) + { + _cachedInstance = writer; +#if DEBUG + writer._inUse = false; +#endif + writer.Reset(); + } + + public void Reset() + { + if (_completedSegments != null) + { + for (var i = 0; i < _completedSegments.Count; i++) + { + _completedSegments[i].Return(); + } + + _completedSegments.Clear(); + } + + if (_currentSegment != null) + { + ArrayPool.Shared.Return(_currentSegment); + _currentSegment = null; + } + + _bytesWritten = 0; + _position = 0; + } + + public void Advance(int count) + { + _bytesWritten += count; + _position += count; + } + + public Memory GetMemory(int sizeHint = 0) + { + EnsureCapacity(sizeHint); + + return _currentSegment.AsMemory(_position, _currentSegment.Length - _position); + } + + public Span GetSpan(int sizeHint = 0) + { + EnsureCapacity(sizeHint); + + return _currentSegment.AsSpan(_position, _currentSegment.Length - _position); + } + + public void CopyTo(IBufferWriter destination) + { + if (_completedSegments != null) + { + // Copy completed segments + var count = _completedSegments.Count; + for (var i = 0; i < count; i++) + { + destination.Write(_completedSegments[i].Span); + } + } + + destination.Write(_currentSegment.AsSpan(0, _position)); + } + + public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) + { + if (_completedSegments == null) + { + // There is only one segment so write without awaiting. + return destination.WriteAsync(_currentSegment, 0, _position); + } + + return CopyToSlowAsync(destination); + } + + private void EnsureCapacity(int sizeHint) + { + // This does the Right Thing. It only subtracts _position from the current segment length if it's non-null. + // If _currentSegment is null, it returns 0. + var remainingSize = _currentSegment?.Length - _position ?? 0; + + // If the sizeHint is 0, any capacity will do + // Otherwise, the buffer must have enough space for the entire size hint, or we need to add a segment. + if ((sizeHint == 0 && remainingSize > 0) || (sizeHint > 0 && remainingSize >= sizeHint)) + { + // We have capacity in the current segment + return; + } + + AddSegment(sizeHint); + } + + private void AddSegment(int sizeHint = 0) + { + if (_currentSegment != null) + { + // We're adding a segment to the list + if (_completedSegments == null) + { + _completedSegments = new List(); + } + + // Position might be less than the segment length if there wasn't enough space to satisfy the sizeHint when + // GetMemory was called. In that case we'll take the current segment and call it "completed", but need to + // ignore any empty space in it. + _completedSegments.Add(new CompletedBuffer(_currentSegment, _position)); + } + + // Get a new buffer using the minimum segment size, unless the size hint is larger than a single segment. + _currentSegment = ArrayPool.Shared.Rent(Math.Max(_minimumSegmentSize, sizeHint)); + _position = 0; + } + + private async Task CopyToSlowAsync(Stream destination) + { + if (_completedSegments != null) + { + // Copy full segments + var count = _completedSegments.Count; + for (var i = 0; i < count; i++) + { + var segment = _completedSegments[i]; + await destination.WriteAsync(segment.Buffer, 0, segment.Length); + } + } + + await destination.WriteAsync(_currentSegment, 0, _position); + } + + public byte[] ToArray() + { + if (_currentSegment == null) + { + return Array.Empty(); + } + + var result = new byte[_bytesWritten]; + + var totalWritten = 0; + + if (_completedSegments != null) + { + // Copy full segments + var count = _completedSegments.Count; + for (var i = 0; i < count; i++) + { + var segment = _completedSegments[i]; + segment.Span.CopyTo(result.AsSpan(totalWritten)); + totalWritten += segment.Span.Length; + } + } + + // Copy current incomplete segment + _currentSegment.AsSpan(0, _position).CopyTo(result.AsSpan(totalWritten)); + + return result; + } + + public void CopyTo(Span span) + { + Debug.Assert(span.Length >= _bytesWritten); + + if (_currentSegment == null) + { + return; + } + + var totalWritten = 0; + + if (_completedSegments != null) + { + // Copy full segments + var count = _completedSegments.Count; + for (var i = 0; i < count; i++) + { + var segment = _completedSegments[i]; + segment.Span.CopyTo(span.Slice(totalWritten)); + totalWritten += segment.Span.Length; + } + } + + // Copy current incomplete segment + _currentSegment.AsSpan(0, _position).CopyTo(span.Slice(totalWritten)); + + Debug.Assert(_bytesWritten == totalWritten + _position); + } + + public override void Flush() { } + public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException(); + public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); + public override void SetLength(long value) => throw new NotSupportedException(); + + public override void WriteByte(byte value) + { + if (_currentSegment != null && (uint)_position < (uint)_currentSegment.Length) + { + _currentSegment[_position] = value; + } + else + { + AddSegment(); + _currentSegment[0] = value; + } + + _position++; + _bytesWritten++; + } + + public override void Write(byte[] buffer, int offset, int count) + { + var position = _position; + if (_currentSegment != null && position < _currentSegment.Length - count) + { + Buffer.BlockCopy(buffer, offset, _currentSegment, position, count); + + _position = position + count; + _bytesWritten += count; + } + else + { + BuffersExtensions.Write(this, buffer.AsSpan(offset, count)); + } + } + +#if NETCOREAPP2_2 + public override void Write(ReadOnlySpan span) + { + if (_currentSegment != null && span.TryCopyTo(_currentSegment.AsSpan(_position))) + { + _position += span.Length; + _bytesWritten += span.Length; + } + else + { + BuffersExtensions.Write(this, span); + } + } +#endif + + protected override void Dispose(bool disposing) + { + if (disposing) + { + Reset(); + } + } + + /// + /// Holds a byte[] from the pool and a size value. Basically a Memory but guaranteed to be backed by an ArrayPool byte[], so that we know we can return it. + /// + private readonly struct CompletedBuffer + { + public byte[] Buffer { get; } + public int Length { get; } + + public ReadOnlySpan Span => Buffer.AsSpan(0, Length); + + public CompletedBuffer(byte[] buffer, int length) + { + Buffer = buffer; + Length = length; + } + + public void Return() + { + ArrayPool.Shared.Return(Buffer); + } + } + } +} diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 30905ca52..c9b2ce2a8 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -4,13 +4,8 @@ $(NoWarn);CS1591 true - aspnetcore;signalr + aspnetcore;azure;signalr false - diff --git a/src/Microsoft.Azure.SignalR.AspNet/AspNetConstants.cs b/src/Microsoft.Azure.SignalR.AspNet/AspNetConstants.cs new file mode 100644 index 000000000..5e7c17ec0 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/AspNetConstants.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal static class AspNetConstants + { + public static class QueryString + { + public const string ConnectionToken = "connectionToken"; + } + + public static class Context + { + public const string AzureServiceConnectionKey = "azure.serviceconnection"; + public const string AzureSignalRTransportKey = "signalr.transport"; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/IServiceEndpointProvider.cs b/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/IServiceEndpointProvider.cs new file mode 100644 index 000000000..79f119710 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/IServiceEndpointProvider.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IServiceEndpointProvider + { + string GenerateServerAccessToken(string hubName, string userId, TimeSpan? lifetime = null); + + string GenerateClientAccessToken(IEnumerable claims = null, TimeSpan? lifetime = null); + + string GetServerEndpoint(string hubName); + + string GetClientEndpoint(); + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/ServiceEndpointProvider.cs b/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/ServiceEndpointProvider.cs new file mode 100644 index 000000000..e6bfb5b7c --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/EndpointProvider/ServiceEndpointProvider.cs @@ -0,0 +1,87 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ServiceEndpointProvider : IServiceEndpointProvider + { + private const string ClientPath = "aspnetclient"; + private const string ServerPath = "aspnetserver"; + + private static readonly string ConnectionStringNotFound = + "No connection string was specified. " + + $"Please specify a configuration entry for {ServiceOptions.ConnectionStringDefaultKey}, " + + "or explicitly pass one using IAppBuilder.RunAzureSignalR(connectionString) in Startup.ConfigureServices."; + + private readonly string _endpoint; + private readonly string _accessKey; + private readonly int? _port; + private readonly TimeSpan _accessTokenLifetime; + + public ServiceEndpointProvider(ServiceOptions options) + { + var connectionString = options.ConnectionString; + if (string.IsNullOrEmpty(connectionString)) + { + throw new ArgumentException(ConnectionStringNotFound); + } + + _accessTokenLifetime = options.AccessTokenLifetime; + + // Version is ignored for aspnet signalr case + (_endpoint, _accessKey, _, _port) = ConnectionStringParser.Parse(connectionString); + } + + public string GenerateClientAccessToken(IEnumerable claims = null, TimeSpan? lifetime = null) + { + var audience = $"{_endpoint}/{ClientPath}"; + return InternalGenerateAccessToken(audience, claims, lifetime ?? _accessTokenLifetime); + } + + public string GenerateServerAccessToken(string hubName, string userId, TimeSpan? lifetime = null) + { + IEnumerable claims = null; + if (userId != null) + { + claims = new[] + { + new Claim(ClaimTypes.NameIdentifier, userId) + }; + } + + var audience = $"{_endpoint}/{ServerPath}/?hub={hubName.ToLower()}"; + + return InternalGenerateAccessToken(audience, claims, lifetime ?? _accessTokenLifetime); + } + + public string GetClientEndpoint() + { + return _port.HasValue ? + $"{_endpoint}:{_port}/{ClientPath}" : + $"{_endpoint}/{ClientPath}"; + } + + public string GetServerEndpoint(string hubName) + { + return _port.HasValue ? + $"{_endpoint}:{_port}/{ServerPath}/?hub={hubName.ToLower()}" : + $"{_endpoint}/{ServerPath}/?hub={hubName.ToLower()}"; + } + + private string InternalGenerateAccessToken(string audience, IEnumerable claims, TimeSpan lifetime) + { + var expire = DateTime.UtcNow.Add(lifetime); + + return AuthenticationHelper.GenerateJwtBearer( + audience: audience, + claims: claims, + expires: expire, + signingKey: _accessKey + ); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Hubs/ServiceHubDispatcher.cs b/src/Microsoft.Azure.SignalR.AspNet/Hubs/ServiceHubDispatcher.cs new file mode 100644 index 000000000..48ce5a4d5 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Hubs/ServiceHubDispatcher.cs @@ -0,0 +1,118 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Hosting; +using Microsoft.AspNet.SignalR.Hubs; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Json; +using Newtonsoft.Json; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ServiceHubDispatcher : HubDispatcher + { + private static readonly ProtocolResolver ProtocolResolver = new ProtocolResolver(); + + private readonly string _appName; + + private IServiceEndpointProvider _endpoint; + + public ServiceHubDispatcher(HubConfiguration configuration, string appName) : base(configuration) + { + _appName = appName; + } + + public override void Initialize(IDependencyResolver resolver) + { + _endpoint = resolver.Resolve(); + base.Initialize(resolver); + } + + public override Task ProcessRequest(HostContext context) + { + // Redirect negotiation to service + if (IsNegotiationRequest(context.Request)) + { + return ProcessNegotiationRequest(context); + } + + return base.ProcessRequest(context); + } + + private Task ProcessNegotiationRequest(HostContext context) + { + var user = new Owin.OwinContext(context.Environment).Authentication.User; + var claims = user?.Claims; + var authenticationType = user?.Identity?.AuthenticationType; + var userId = UserIdProvider.GetUserId(context.Request); + var advancedClaims = claims?.ToList() ?? new List(); + + if (authenticationType != null) + { + advancedClaims.Add(new Claim(Constants.ClaimType.AuthenticationType, authenticationType)); + } + + // UserId can be null in self-host Owin + if (userId != null) + { + advancedClaims.Add(new Claim(Constants.ClaimType.UserId, userId)); + } + + // Pass appname through jwt token to client, so that when client establishes connection with service, it will also create a corresponding AppName-connection + advancedClaims.Add(new Claim(Constants.ClaimType.AppName, _appName)); + + // Redirect to Service + var url = _endpoint.GetClientEndpoint(); + var accessToken = _endpoint.GenerateClientAccessToken(advancedClaims); + + return SendJsonResponse(context, GetRedirectNegotiateResponse(url, accessToken)); + } + + private static string GetRedirectNegotiateResponse(string url, string token) + { + var sb = new StringBuilder(); + using (var jsonWriter = new JsonTextWriter(new StringWriter(sb))) + { + jsonWriter.WriteStartObject(); + jsonWriter.WritePropertyName("ProtocolVersion"); + jsonWriter.WriteValue("2.0"); + jsonWriter.WritePropertyName("RedirectUrl"); + jsonWriter.WriteValue(url); + jsonWriter.WritePropertyName("AccessToken"); + jsonWriter.WriteValue(token); + jsonWriter.WriteEndObject(); + } + + return sb.ToString(); + } + + private static bool IsNegotiationRequest(IRequest request) + { + return request.LocalPath.EndsWith(Constants.Path.Negotiate, StringComparison.OrdinalIgnoreCase); + } + + private static Task SendJsonResponse(HostContext context, string jsonPayload) + { + var callback = context.Request.QueryString["callback"]; + if (String.IsNullOrEmpty(callback)) + { + // Send normal JSON response + context.Response.ContentType = JsonUtility.JsonMimeType; + return context.Response.End(jsonPayload); + } + + // JSONP response is no longer supported. + context.Response.StatusCode = 400; + return context.Response.End("JSONP is no longer supported."); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/AppMessage.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/AppMessage.cs new file mode 100644 index 000000000..ca748e5f9 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/AppMessage.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class AppMessage + { + public ServiceMessage Message { get; } + + public Message RawMessage { get; } + + public AppMessage(ServiceMessage message, Message rawMessage) + { + Message = message; + RawMessage = rawMessage; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ClientConnectionManager.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ClientConnectionManager.cs new file mode 100644 index 000000000..0175e3df9 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ClientConnectionManager.cs @@ -0,0 +1,136 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Security.Claims; +using System.Web; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Hosting; +using Microsoft.AspNet.SignalR.Hubs; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Owin; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ClientConnectionManager : IClientConnectionManager + { + private static readonly ClaimsPrincipal EmptyPrincipal = new ClaimsPrincipal(new ClaimsIdentity()); + + private static readonly string[] SystemClaims = + { + "aud", // Audience claim, used by service to make sure token is matched with target resource. + "exp", // Expiration time claims. A token is valid only before its expiration time. + "iat", // Issued At claim. Added by default. It is not validated by service. + "nbf" // Not Before claim. Added by default. It is not validated by service. + }; + + private readonly HubConfiguration _configuration; + + public ClientConnectionManager(HubConfiguration configuration) + { + _configuration = configuration; + } + + public AzureTransport CreateConnection(OpenConnectionMessage message, IServiceConnection serviceConnection) + { + var dispatcher = new HubDispatcher(_configuration); + dispatcher.Initialize(_configuration.Resolver); + + var connectionId = message.ConnectionId; + + var responseStream = new MemoryStream(); + var hostContext = GetHostContext(message, responseStream, serviceConnection); + + if (dispatcher.Authorize(hostContext.Request)) + { + // ProcessRequest checks if the connectionToken matches "{connectionid}:{userName}" format with context.User + _ = dispatcher.ProcessRequest(hostContext); + + // TODO: check for errors written to the response + if (hostContext.Response.StatusCode != 200) + { + Debug.Fail("Response StatusCode is " + hostContext.Response.StatusCode); + var errorResponse = GetContentAndDispose(responseStream); + throw new InvalidOperationException(errorResponse); + } + return (AzureTransport)hostContext.Environment[AspNetConstants.Context.AzureSignalRTransportKey]; + } + + // This happens when hub is not found + Debug.Fail("Unauthorized"); + throw new InvalidOperationException("Unable to authorize request"); + } + + internal HostContext GetHostContext(OpenConnectionMessage message, Stream responseStream, IServiceConnection serviceConnection) + { + var connectionId = message.ConnectionId; + var context = new OwinContext(); + var response = context.Response; + var request = context.Request; + + response.Body = responseStream; + + var user = request.User = GetUserPrincipal(message); + + request.Path = new PathString("/"); + + var userToken = string.IsNullOrEmpty(user.Identity.Name) ? string.Empty : ":" + user.Identity.Name; + + // TODO: when https://github.com/SignalR/SignalR/issues/4175 is resolved, we can get rid of paring query string + var queryCollection = HttpUtility.ParseQueryString(message.QueryString ?? string.Empty); + queryCollection[AspNetConstants.QueryString.ConnectionToken] = $"{connectionId}{userToken}"; + + request.QueryString = new QueryString(queryCollection.ToString()); + + if (message.Headers != null) + { + foreach (var pair in message.Headers) + { + request.Headers.Add(pair.Key, pair.Value); + } + } + + context.Environment[AspNetConstants.Context.AzureServiceConnectionKey] = serviceConnection; + return new HostContext(context.Environment); + } + + internal static ClaimsPrincipal GetUserPrincipal(OpenConnectionMessage message) + { + if (message.Claims == null || message.Claims.Length == 0) + { + return EmptyPrincipal; + } + + var claims = new List(); + var authenticationType = "Bearer"; + + foreach (var claim in message.Claims) + { + // TODO: Add prefix "azure.signalr.user." to user claims instead of guessing them? + if (claim.Type == Constants.ClaimType.AuthenticationType) + { + authenticationType = claim.Value; + } + else if (!SystemClaims.Contains(claim.Type) && !claim.Type.StartsWith(Constants.ClaimType.AzureSignalRSysPrefix)) + { + claims.Add(claim); + } + } + + return new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType)); + } + + internal static string GetContentAndDispose(MemoryStream stream) + { + stream.Seek(0, SeekOrigin.Begin); + using (var reader = new StreamReader(stream)) + { + return reader.ReadToEnd(); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs new file mode 100644 index 000000000..edb09f233 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs @@ -0,0 +1,125 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.Http.Connections.Client; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; + +namespace Microsoft.Azure.SignalR.AspNet +{ + /// + /// TODO: This factory class is responsible for creating, disposing and starting the server-service connections + /// + internal class ConnectionFactory : IConnectionFactory + { + private readonly ServiceOptions _options; + private readonly HubConfiguration _config; + private readonly ILoggerFactory _loggerFactory; + private readonly ILogger _logger; + private readonly IReadOnlyList _hubNames; + private readonly IServiceConnectionManager _serviceConnectionManager; + private readonly IClientConnectionManager _clientConnectionManager; + private readonly IServiceProtocol _protocol; + private readonly IServiceEndpointProvider _endpoint; + private readonly string _name; + private readonly string _userId; + + public ConnectionFactory(IReadOnlyList hubNames, HubConfiguration hubConfig) + { + _config = hubConfig; + _hubNames = hubNames; + _name = $"{nameof(ConnectionFactory)}[{string.Join(",", hubNames)}]"; + _userId = GenerateServerName(); + + _loggerFactory = hubConfig.Resolver.Resolve() ?? NullLoggerFactory.Instance; + _protocol = hubConfig.Resolver.Resolve(); + _serviceConnectionManager = hubConfig.Resolver.Resolve(); + _clientConnectionManager = hubConfig.Resolver.Resolve(); + _endpoint = hubConfig.Resolver.Resolve(); + _options = hubConfig.Resolver.Resolve>().Value; + + _logger = _loggerFactory.CreateLogger(); + } + + public async Task ConnectAsync(TransferFormat transferFormat, string connectionId, string hubName, CancellationToken cancellationToken = default) + { + var httpConnectionOptions = new HttpConnectionOptions + { + Url = GetServiceUrl(connectionId, hubName), + AccessTokenProvider = () => Task.FromResult(_endpoint.GenerateServerAccessToken(hubName, _userId)), + Transports = HttpTransportType.WebSockets, + SkipNegotiation = true + }; + var httpConnection = new HttpConnection(httpConnectionOptions, _loggerFactory); + try + { + await httpConnection.StartAsync(transferFormat); + return httpConnection; + } + catch + { + await httpConnection.DisposeAsync(); + throw; + } + } + + public Task DisposeAsync(ConnectionContext connection) + { + return ((HttpConnection)connection).DisposeAsync(); + } + + public Task StartAsync() + { + _serviceConnectionManager.Initialize( + hub => new ServiceConnection(hub, Guid.NewGuid().ToString(), _protocol, this, _clientConnectionManager, _logger), + _options.ConnectionCount); + + Log.StartingConnection(_logger, _name, _options.ConnectionCount, _hubNames.Count); + + return _serviceConnectionManager.StartAsync(); + } + + private Uri GetServiceUrl(string connectionId, string hubName) + { + var baseUri = new UriBuilder(_endpoint.GetServerEndpoint(hubName)); + var query = "cid=" + connectionId; + if (baseUri.Query != null && baseUri.Query.Length > 1) + { + baseUri.Query = baseUri.Query.Substring(1) + "&" + query; + } + else + { + baseUri.Query = query; + } + return baseUri.Uri; + } + + private static string GenerateServerName() + { + // Use the machine name for convenient diagnostics, but add a guid to make it unique. + // Example: MyServerName_02db60e5fab243b890a847fa5c4dcb29 + return $"{Environment.MachineName}_{Guid.NewGuid():N}"; + } + + private static class Log + { + private static readonly Action _startingConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "StartingConnection"), "Starting {name} with {hubCount} hubs and {connectionCount} per hub connections..."); + + public static void StartingConnection(ILogger logger, string name, int connectionCount, int hubCount) + { + _startingConnection(logger, name, connectionCount, hubCount, null); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/EmptyProtectedData.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/EmptyProtectedData.cs new file mode 100644 index 000000000..608e6c2b4 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/EmptyProtectedData.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNet.SignalR.Infrastructure; + +namespace Microsoft.Azure.SignalR.AspNet +{ + /// + /// The token is no long Base64 encoded as the DefaultProtectedData does + /// TODO: remove it when SignalR is ready + /// + internal class EmptyProtectedData : IProtectedData + { + public string Protect(string data, string purpose) + { + return data; + } + + public string Unprotect(string protectedValue, string purpose) + { + return protectedValue; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/HubMessage.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/HubMessage.cs new file mode 100644 index 000000000..2c87c9958 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/HubMessage.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class HubMessage : AppMessage + { + public string HubName { get; } + + public HubMessage(string name, ServiceMessage message, Message rawMessage) : base(message, rawMessage) + { + HubName = name; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IClientConnectionManager.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IClientConnectionManager.cs new file mode 100644 index 000000000..e2aa2757e --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IClientConnectionManager.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IClientConnectionManager + { + AzureTransport CreateConnection(OpenConnectionMessage message, IServiceConnection serviceConnection); + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IConnectionFactory.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IConnectionFactory.cs new file mode 100644 index 000000000..706598dfa --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IConnectionFactory.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IConnectionFactory + { + Task ConnectAsync(TransferFormat transferFormat, string connectionId, string hubName, CancellationToken cancellationToken = default); + + // Current plan for IAsyncDisposable is that DisposeAsync will NOT take a CancellationToken + // https://github.com/dotnet/csharplang/blob/195efa07806284d7b57550e7447dc8bd39c156bf/proposals/async-streams.md#iasyncdisposable + Task DisposeAsync(ConnectionContext connection); + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IMessageParser.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IMessageParser.cs new file mode 100644 index 000000000..153f14b26 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IMessageParser.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using Microsoft.AspNet.SignalR.Messaging; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IMessageParser + { + IEnumerable GetMessages(Message message); + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionContainer.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionContainer.cs new file mode 100644 index 000000000..709af0f10 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionContainer.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading.Tasks; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IServiceConnectionContainer : IServiceConnection + { + Task WriteAsync(string partitionKey, ServiceMessage serviceMessage); + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionManager.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionManager.cs new file mode 100644 index 000000000..ac1126806 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/IServiceConnectionManager.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal interface IServiceConnectionManager : IServiceConnectionContainer + { + void Initialize(Func connectionGenerator, int connectionCount); + + IServiceConnectionContainer WithHub(string hubName); + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.Log.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.Log.cs new file mode 100644 index 000000000..0805f34cf --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.Log.cs @@ -0,0 +1,231 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal partial class ServiceConnection + { + private static class Log + { + // Category: ServiceConnection + private static readonly Action _failedToWrite = + LoggerMessage.Define(LogLevel.Error, new EventId(1, "FailedToWrite"), "Failed to send message to the service."); + + private static readonly Action _failedToConnect = + LoggerMessage.Define(LogLevel.Error, new EventId(2, "FailedToConnect"), "Failed to connect to the service."); + + private static readonly Action _errorProcessingMessages = + LoggerMessage.Define(LogLevel.Error, new EventId(3, "ErrorProcessingMessages"), "Error when processing messages."); + + private static readonly Action _connectionDropped = + LoggerMessage.Define(LogLevel.Error, new EventId(4, "ConnectionDropped"), "Connection {ServiceConnectionId} to the service was dropped."); + + private static readonly Action _failedToCleanupConnections = + LoggerMessage.Define(LogLevel.Error, new EventId(5, "FailedToCleanupConnection"), "Failed to clean up client connections."); + + private static readonly Action _errorSendingMessage = + LoggerMessage.Define(LogLevel.Error, new EventId(6, "ErrorSendingMessage"), "Error while sending message to the service."); + + private static readonly Action _sendLoopStopped = + LoggerMessage.Define(LogLevel.Error, new EventId(7, "SendLoopStopped"), "Error while processing messages from {TransportConnectionId}."); + + private static readonly Action _applicationTaskFailed = + LoggerMessage.Define(LogLevel.Error, new EventId(8, "ApplicationTaskFailed"), "Application task failed."); + + private static readonly Action _failToWriteMessageToApplication = + LoggerMessage.Define(LogLevel.Error, new EventId(9, "FailToWriteMessageToApplication"), "Failed to write message to {TransportConnectionId}."); + + private static readonly Action _receivedMessageForNonExistentConnection = + LoggerMessage.Define(LogLevel.Warning, new EventId(10, "ReceivedMessageForNonExistentConnection"), "Received message for connection {TransportConnectionId} which does not exist."); + + private static readonly Action _connectedStarting = + LoggerMessage.Define(LogLevel.Debug, new EventId(11, "ConnectedStarting"), "Connection {TransportConnectionId} started."); + + private static readonly Action _connectedStartingFailed = + LoggerMessage.Define(LogLevel.Error, new EventId(11, "ConnectedStartingFailed"), "Connection {TransportConnectionId} failed to start."); + + private static readonly Action _connectedEnding = + LoggerMessage.Define(LogLevel.Debug, new EventId(12, "ConnectedEnding"), "Connection {TransportConnectionId} ended."); + + private static readonly Action _closeConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(13, "CloseConnection"), "Sending close connection message to the service for {TransportConnectionId}."); + + private static readonly Action _serviceConnectionClosed = + LoggerMessage.Define(LogLevel.Debug, new EventId(14, "serviceConnectionClose"), "Service connection {ServiceConnectionId} closed."); + + private static readonly Action _readingCancelled = + LoggerMessage.Define(LogLevel.Trace, new EventId(15, "ReadingCancelled"), "Reading from service connection {ServiceConnectionId} cancelled."); + + private static readonly Action _receivedMessage = + LoggerMessage.Define(LogLevel.Debug, new EventId(16, "ReceivedMessage"), "Received {ReceivedBytes} bytes from service {ServiceConnectionId}."); + + private static readonly Action _startingKeepAliveTimer = + LoggerMessage.Define(LogLevel.Trace, new EventId(17, "StartingKeepAliveTimer"), "Starting keep-alive timer. Duration: {KeepAliveInterval:0.00}ms"); + + private static readonly Action _serviceTimeout = + LoggerMessage.Define(LogLevel.Error, new EventId(18, "ServiceTimeout"), "Service timeout. {ServiceTimeout:0.00}ms elapsed without receiving a message from service."); + + private static readonly Action _writeMessageToApplication = + LoggerMessage.Define(LogLevel.Trace, new EventId(19, "WriteMessageToApplication"), "Writing {ReceivedBytes} to connection {TransportConnectionId}."); + + private static readonly Action _serviceConnectionConnected = + LoggerMessage.Define(LogLevel.Debug, new EventId(20, "ServiceConnectionConnected"), "Service connection {ServiceConnectionId} connected."); + + private static readonly Action _sendingHandshakeRequest = + LoggerMessage.Define(LogLevel.Debug, new EventId(21, "SendingHandshakeRequest"), "Sending Handshake request to service."); + + private static readonly Action _handshakeComplete = + LoggerMessage.Define(LogLevel.Debug, new EventId(22, "HandshakeComplete"), "Handshake with service completes."); + + private static readonly Action _errorReceivingHandshakeResponse = + LoggerMessage.Define(LogLevel.Error, new EventId(23, "ErrorReceivingHandshakeResponse"), "Error receiving handshake response."); + + private static readonly Action _handshakeError = + LoggerMessage.Define(LogLevel.Critical, new EventId(24, "HandshakeError"), "Service returned handshake error: {Error}"); + + private static readonly Action _sentPing = + LoggerMessage.Define(LogLevel.Debug, new EventId(25, "SentPing"), "Sent a ping message to service."); + + private static readonly Action _failedSendingPing = + LoggerMessage.Define(LogLevel.Warning, new EventId(26, "FailedSendingPing"), "Failed sending a ping message to service."); + + public static void FailedToWrite(ILogger logger, Exception exception) + { + _failedToWrite(logger, exception); + } + + public static void FailedToConnect(ILogger logger, Exception exception) + { + _failedToConnect(logger, exception); + } + + public static void ErrorProcessingMessages(ILogger logger, Exception exception) + { + _errorProcessingMessages(logger, exception); + } + + public static void ConnectionDropped(ILogger logger, string serviceConnectionId, Exception exception) + { + _connectionDropped(logger, serviceConnectionId, exception); + } + + public static void FailedToCleanupConnections(ILogger logger, Exception exception) + { + _failedToCleanupConnections(logger, exception); + } + + public static void ErrorSendingMessage(ILogger logger, Exception exception) + { + _errorSendingMessage(logger, exception); + } + + public static void SendLoopStopped(ILogger logger, string connectionId, Exception exception) + { + _sendLoopStopped(logger, connectionId, exception); + } + + public static void ApplicaitonTaskFailed(ILogger logger, Exception exception) + { + _applicationTaskFailed(logger, exception); + } + + public static void FailToWriteMessageToApplication(ILogger logger, string connectionId, Exception exception) + { + _failToWriteMessageToApplication(logger, connectionId, exception); + } + + public static void ReceivedMessageForNonExistentConnection(ILogger logger, string connectionId) + { + _receivedMessageForNonExistentConnection(logger, connectionId, null); + } + + public static void ConnectedStarting(ILogger logger, string connectionId) + { + _connectedStarting(logger, connectionId, null); + } + + public static void ConnectedStartingFailed(ILogger logger, string connectionId, Exception e) + { + _connectedStartingFailed(logger, connectionId, e); + } + + public static void ConnectedEnding(ILogger logger, string connectionId) + { + _connectedEnding(logger, connectionId, null); + } + + public static void CloseConnection(ILogger logger, string connectionId) + { + _closeConnection(logger, connectionId, null); + } + + public static void ServiceConnectionClosed(ILogger logger, string serviceConnectionId) + { + _serviceConnectionClosed(logger, serviceConnectionId, null); + } + + public static void ServiceConnectionConnected(ILogger logger, string serviceConnectionId) + { + _serviceConnectionConnected(logger, serviceConnectionId, null); + } + + public static void ReadingCancelled(ILogger logger, string serviceConnectionId) + { + _readingCancelled(logger, serviceConnectionId, null); + } + + public static void ReceivedMessage(ILogger logger, long bytes, string serviceConnectionId) + { + _receivedMessage(logger, bytes, serviceConnectionId, null); + } + + public static void StartingKeepAliveTimer(ILogger logger, TimeSpan keepAliveInterval) + { + _startingKeepAliveTimer(logger, keepAliveInterval.TotalMilliseconds, null); + } + + public static void ServiceTimeout(ILogger logger, TimeSpan serviceTimeout) + { + _serviceTimeout(logger, serviceTimeout.TotalMilliseconds, null); + } + + public static void WriteMessageToApplication(ILogger logger, long count, string connectionId) + { + _writeMessageToApplication(logger, count, connectionId, null); + } + + public static void SendingHandshakeRequest(ILogger logger) + { + _sendingHandshakeRequest(logger, null); + } + + public static void HandshakeComplete(ILogger logger) + { + _handshakeComplete(logger, null); + } + + public static void ErrorReceivingHandshakeResponse(ILogger logger, Exception exception) + { + _errorReceivingHandshakeResponse(logger, exception); + } + + public static void HandshakeError(ILogger logger, string error) + { + _handshakeError(logger, error, null); + } + + public static void SentPing(ILogger logger) + { + _sentPing(logger, null); + } + + public static void FailedSendingPing(ILogger logger, Exception exception) + { + _failedSendingPing(logger, exception); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.cs new file mode 100644 index 000000000..3d6cd6085 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnection.cs @@ -0,0 +1,155 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Concurrent; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNetCore.Connections; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal partial class ServiceConnection : ServiceConnectionBase + { + private const string ReconnectMessage = "asrs:reconnect"; + private readonly ConcurrentDictionary _clientConnections = new ConcurrentDictionary(StringComparer.Ordinal); + + private readonly string _hubName; + private readonly IConnectionFactory _connectionFactory; + private readonly IClientConnectionManager _clientConnectionManager; + + public ServiceConnection( + string hubName, + string connectionId, + IServiceProtocol serviceProtocol, + IConnectionFactory connectionFactory, + IClientConnectionManager clientConnectionManager, + ILogger logger) + : base(serviceProtocol, logger, connectionId) + { + _hubName = hubName; + _connectionFactory = connectionFactory; + _clientConnectionManager = clientConnectionManager; + } + + protected override Task CreateConnection() + { + return _connectionFactory.ConnectAsync(TransferFormat.Binary, _connectionId, _hubName); + } + + protected override Task DisposeConnection() + { + var connection = _connection; + _connection = null; + return _connectionFactory.DisposeAsync(connection); + } + + protected override Task CleanupConnections() + { + try + { + foreach (var connection in _clientConnections) + { + PerformDisconnectCore(connection.Key); + } + } + catch (Exception ex) + { + Log.FailedToCleanupConnections(_logger, ex); + } + + return Task.CompletedTask; + } + + protected override Task OnConnectedAsync(OpenConnectionMessage openConnectionMessage) + { + // Writing from the application to the service + _ = OnConnectedAsyncCore(openConnectionMessage); + + return Task.CompletedTask; + } + + protected override Task OnDisconnectedAsync(CloseConnectionMessage closeConnectionMessage) + { + var connectionId = closeConnectionMessage.ConnectionId; + PerformDisconnectCore(connectionId); + + return Task.CompletedTask; + } + + protected override Task OnMessageAsync(ConnectionDataMessage connectionDataMessage) + { + if (_clientConnections.TryGetValue(connectionDataMessage.ConnectionId, out var transport)) + { + try + { + var payload = connectionDataMessage.Payload; + Log.WriteMessageToApplication(_logger, payload.Length, connectionDataMessage.ConnectionId); + var message = GetString(payload); + if (message == ReconnectMessage) + { + transport.Reconnected?.Invoke(); + } + else + { + transport.OnReceived(message); + } + } + catch (Exception ex) + { + Log.FailToWriteMessageToApplication(_logger, connectionDataMessage.ConnectionId, ex); + } + } + + // Unexpected error + Log.ReceivedMessageForNonExistentConnection(_logger, connectionDataMessage.ConnectionId); + + return Task.CompletedTask; + } + + private void PerformDisconnectCore(string connectionId) + { + if (_clientConnections.TryRemove(connectionId, out var transport)) + { + transport.OnDisconnected(); + } + + Log.ConnectedEnding(_logger, connectionId); + } + + private Task OnConnectedAsyncCore(OpenConnectionMessage message) + { + var connectionId = message.ConnectionId; + try + { + var transport = _clientConnectionManager.CreateConnection(message, this); + _clientConnections.TryAdd(transport.ConnectionId, transport); + Log.ConnectedStarting(_logger, connectionId); + return Task.CompletedTask; + } + catch (Exception e) + { + Log.ConnectedStartingFailed(_logger, connectionId, e); + PerformDisconnectCore(connectionId); + return WriteAsync(new CloseConnectionMessage(connectionId, e.Message)); + } + } + + private static string GetString(ReadOnlySequence buffer) + { + if (buffer.IsSingleSegment) + { + MemoryMarshal.TryGetArray(buffer.First, out var segment); + return Encoding.UTF8.GetString(segment.Array, segment.Offset, segment.Count); + } + + return Encoding.UTF8.GetString(buffer.ToArray()); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionContainer.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionContainer.cs new file mode 100644 index 000000000..f92baba16 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionContainer.cs @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ServiceConnectionContainer : IServiceConnectionContainer + { + private readonly List _serviceConnections; + + public ServiceConnectionContainer(Func generator, int count) + { + if (generator == null) + { + throw new ArgumentNullException(nameof(generator)); + } + + if (count <= 0) + { + throw new ArgumentException($"{nameof(count)} must be greater than 0."); + } + + _serviceConnections = new List(count); + for (int i = 0; i < count; i++) + { + _serviceConnections.Add(generator()); + } + } + + public Task StartAsync() + { + return Task.WhenAll(_serviceConnections.Select(c => c.StartAsync())); + } + + public Task StopAsync() + { + return Task.WhenAll(_serviceConnections.Select(c => c.StopAsync())); + } + + public Task WriteAsync(ServiceMessage serviceMessage) + { + var index = StaticRandom.Next(_serviceConnections.Count); + return _serviceConnections[index].WriteAsync(serviceMessage); + } + + public async Task WriteAsync(string partitionKey, ServiceMessage serviceMessage) + { + if (_serviceConnections.Count == 0) return; + + // If we hit this check, it is a code bug. + if (string.IsNullOrEmpty(partitionKey)) + { + throw new ArgumentNullException(nameof(partitionKey)); + } + + var index = Math.Abs(partitionKey.GetHashCode() % _serviceConnections.Count); + await _serviceConnections[index].WriteAsync(serviceMessage); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionExtensions.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionExtensions.cs new file mode 100644 index 000000000..de6367dd0 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionExtensions.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.Azure.SignalR.Protocol; +using Newtonsoft.Json; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal static class ServiceConnectionExtensions + { + public static Task WriteAsync(this IServiceConnection serviceConnection, string connectionId, object value, IServiceProtocol protocol, JsonSerializer serializer, IMemoryPool pool) + { + using (var writer = new MemoryPoolTextWriter(pool)) + { + serializer.Serialize(writer, value); + writer.Flush(); + + // Reuse ConnectionDataMessage to wrap the payload + var wrapped = new ConnectionDataMessage(string.Empty, writer.Buffer); + return serviceConnection.WriteAsync(new ConnectionDataMessage(connectionId, protocol.GetMessageBytes(wrapped))); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionManager.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionManager.cs new file mode 100644 index 000000000..55e357281 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceConnectionManager.cs @@ -0,0 +1,128 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ServiceConnectionManager : IServiceConnectionManager + { + private IReadOnlyDictionary _serviceConnections = null; + + private readonly object _lock = new object(); + + private readonly IReadOnlyList _hubs; + private readonly string _appName; + + private IServiceConnectionContainer _appConnection; + + public ServiceConnectionManager(string appName, IReadOnlyList hubs) + { + _hubs = hubs; + _appName = appName; + } + + public void Initialize(Func connectionGenerator, int connectionCount) + { + if (connectionGenerator == null) + { + throw new ArgumentNullException(nameof(connectionGenerator)); + } + + if (connectionCount <= 0) + { + throw new ArgumentException($"{nameof(connectionCount)} must be larger than 0."); + } + + if (_serviceConnections != null) + { + // TODO: log something to indicate the connection is already initialized. + return; + } + + lock (_lock) + { + if (_serviceConnections != null) + { + return; + } + + var connections = new Dictionary(); + + _appConnection = new ServiceConnectionContainer( + () => connectionGenerator(_appName), + connectionCount); + + foreach (var hub in _hubs) + { + var connection = new ServiceConnectionContainer( + () => connectionGenerator(hub), + connectionCount); + connections.Add(hub, connection); + } + + _serviceConnections = connections; + } + } + + public Task StartAsync() + { + return Task.WhenAll(GetConnections().Select(s => s.StartAsync())); + } + + public Task StopAsync() + { + return Task.WhenAll(GetConnections().Select(s => s.StopAsync())); + } + + public IServiceConnectionContainer WithHub(string hubName) + { + if (_serviceConnections == null ||!_serviceConnections.TryGetValue(hubName, out var connection)) + { + throw new KeyNotFoundException($"Service connection with Hub {hubName} does not exist"); + } + + return connection; + } + + public Task WriteAsync(ServiceMessage serviceMessage) + { + if (_appConnection == null) + { + throw new InvalidOperationException("App connection is not yet initialized."); + } + + return _appConnection.WriteAsync(serviceMessage); + } + + public Task WriteAsync(string partitionKey, ServiceMessage serviceMessage) + { + if (_appConnection == null) + { + throw new InvalidOperationException("App connection is not yet initialized."); + } + + return _appConnection.WriteAsync(partitionKey, serviceMessage); + } + + private IEnumerable GetConnections() + { + if (_appConnection != null) + { + yield return _appConnection; + } + + if (_serviceConnections != null) + { + foreach (var conn in _serviceConnections) + { + yield return conn.Value; + } + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceMessageBus.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceMessageBus.cs new file mode 100644 index 000000000..a6d3e893f --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ServiceMessageBus.cs @@ -0,0 +1,82 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class ServiceMessageBus : MessageBus + { + private readonly IMessageParser _parser; + private readonly IServiceConnectionManager _serviceConnectionManager; + private readonly IAckHandler _ackHandler; + + public ServiceMessageBus(IDependencyResolver resolver) : base(resolver) + { + // TODO: find a more decent way instead of DI, it can be easily overriden + _serviceConnectionManager = resolver.Resolve() ?? throw new ArgumentNullException(nameof(IServiceConnectionManager)); + _parser = resolver.Resolve() ?? throw new ArgumentNullException(nameof(IMessageParser)); + _ackHandler = resolver.Resolve() ?? throw new ArgumentNullException(nameof(IAckHandler)); + } + + public override Task Publish(Message message) + { + var messages = _parser.GetMessages(message).ToList(); + if (messages.Count == 0) + { + return Task.CompletedTask; + } + + if (messages.Count == 1) + { + return ProcessMessage(messages[0]); + } + + return Task.WhenAll(messages.Select(m => ProcessMessage(m))); + } + + private Task ProcessMessage(AppMessage message) + { + if (message is HubMessage hubMessage) + { + return WriteMessage(_serviceConnectionManager.WithHub(hubMessage.HubName), message); + } + + return WriteMessage(_serviceConnectionManager, message); + } + + private async Task WriteMessage(IServiceConnectionContainer connection, AppMessage appMessage) + { + var message = appMessage.Message; + switch (message) + { + // For group related messages, make sure messages are written to the same partition + case JoinGroupMessage joinGroupMessage: + try + { + await connection.WriteAsync(joinGroupMessage.GroupName, joinGroupMessage); + } + finally + { + _ackHandler.TriggerAck(appMessage.RawMessage.CommandId); + } + break; + case LeaveGroupMessage leaveGroupMessage: + await connection.WriteAsync(leaveGroupMessage.GroupName, leaveGroupMessage); + break; + case GroupBroadcastDataMessage groupBroadcastMessage: + await connection.WriteAsync(groupBroadcastMessage.GroupName, groupBroadcastMessage); + break; + default: + await connection.WriteAsync(message); + break; + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/SignalRMessageParser.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/SignalRMessageParser.cs new file mode 100644 index 000000000..d024ede03 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/SignalRMessageParser.cs @@ -0,0 +1,224 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Json; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.Protocol; +using Newtonsoft.Json; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class SignalRMessageParser : IMessageParser + { + private const char DotChar = '.'; + + private readonly HashSet _hubNameWithDots = new HashSet(); + private readonly HashSet _hubs = new HashSet(); + + private readonly JsonSerializer _serializer; + private readonly IMemoryPool _pool; + private readonly IServiceProtocol _serviceProtocol; + + public SignalRMessageParser(IReadOnlyList hubs, IDependencyResolver resolver) + { + _serializer = resolver.Resolve() ?? throw new ArgumentNullException(nameof(JsonSerializer)); + _serviceProtocol = resolver.Resolve() ?? throw new ArgumentNullException(nameof(IServiceProtocol)); + _pool = resolver.Resolve() ?? throw new ArgumentNullException(nameof(IMemoryPool)); + + // Hubs are fetched from IHubManager.GetHubs()'s Name property, and by default it is the type Name instead of FullName, or it is the value of HubName attribute + foreach (var hub in hubs) + { + // It is possible that the hub contains dot character, while the fully qualified name is formed as {HubName}.{Name} (Name can be connectionId or userId or groupId) + // So keep a copy of the hub names containing dots and return all the possible combinations when the fully qualified name is provided + if (hub.IndexOf(DotChar) > -1) + { + _hubNameWithDots.Add(hub); + } + + _hubs.Add(hub); + } + } + + public IEnumerable GetMessages(Message message) + { + if (message.IsCommand) + { + var command = _serializer.Parse(message.Value, message.Encoding); + switch (command.CommandType) + { + case CommandType.AddToGroup: + { + // name is hg-{HubName}.{GroupName}, consider the whole as the actual group name + // this message always goes through the appName-connection + var groupName = command.Value; + var connectionId = GetName(message.Key, PrefixHelper.ConnectionIdPrefix); + + // go through the app connection + // use groupName as the partitionkey so that commands towards the same group always goes into the same service connection + yield return new AppMessage(new JoinGroupMessage(connectionId, groupName), message); + yield break; + } + case CommandType.RemoveFromGroup: + { + // this message always goes through the appName-connection + var groupName = command.Value; + var connectionId = GetName(message.Key, PrefixHelper.ConnectionIdPrefix); + + // go through the app connection + // use groupName as the partitionkey so that commands towards the same group always goes into the same service connection + yield return new AppMessage(new LeaveGroupMessage(connectionId, groupName), message); + yield break; + } + case CommandType.Initializing: + yield break; + case CommandType.Abort: + yield break; + } + } + + var segment = GetPayload(message); + + // broadcast case + if (TryGetName(message.Key, PrefixHelper.HubPrefix, out var hubName)) + { + yield return new HubMessage(hubName, new BroadcastDataMessage(excludedList: GetExcludedIds(message.Filter), payloads: GetPayloads(segment)), message); + } + // echo case + else if (TryGetName(message.Key, PrefixHelper.HubConnectionIdPrefix, out _)) + { + // naming: hc-{HubName}.{ConnectionId} + // ConnectionId can NEVER contain . + var index = message.Key.LastIndexOf('.'); + if (index < 0 || index == message.Key.Length - 1) + { + throw new ArgumentException($"Key must contain '.' in between but it is not: {message.Key}"); + } + + var connectionId = message.Key.Substring(index + 1); + + // Go through the app connection + yield return new AppMessage(new ConnectionDataMessage(connectionId, segment), message); + } + // group broadcast case + else if (TryGetName(message.Key, PrefixHelper.HubGroupPrefix, out _)) + { + // naming: hg-{HubName}.{GroupName}, it as a whole is the group name per the JoinGroup implementation + // go through the app connection + // use groupName as the partitionkey so that commands towards the same group always goes into the same service connection + var groupName = message.Key; + yield return new AppMessage(new GroupBroadcastDataMessage(groupName, excludedList: GetExcludedIds(message.Filter), payloads: GetPayloads(segment)), message); + } + // user case + else if (TryGetName(message.Key, PrefixHelper.HubUserPrefix, out var userWithHubPrefix)) + { + // naming: hu-{HubName}.{UserName}, HubName can contain '.' and UserName can contain '.' + // Go through all the possibilities + foreach (var (hub, user) in GetPossibleNames(userWithHubPrefix)) + { + // For old protocol, it is always single user per message https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Infrastructure/Connection.cs#L162 + yield return new HubMessage(hub, new UserDataMessage(user, GetPayloads(segment)), message); + } + } + else + { + throw new NotSupportedException($"Message {message.Key} is not supported."); + } + } + + private ReadOnlyMemory GetPayload(Message message) + { + IJsonWritable value = new PersistentResponse(m => false, tw => tw.Write("0")) + { + Messages = new List> + { + new ArraySegment(new[] {message}) + }, + TotalCount = 1 + }; + + using (var writer = new MemoryPoolTextWriter(_pool)) + { + value.WriteJson(writer); + writer.Flush(); + + // Reuse ConnectionDataMessage to wrap the payload + return _serviceProtocol.GetMessageBytes(new ConnectionDataMessage(string.Empty, writer.Buffer)); + } + } + + private IEnumerable<(string hub, string name)> GetPossibleNames(string fullName) + { + var index = fullName.IndexOf(DotChar); + if (index == -1) + { + throw new InvalidDataException($"Name {fullName} does not contain the required separator {DotChar}"); + } + + // It is rare that hubname contains '.' + foreach (var hub in _hubNameWithDots) + { + if (fullName.Length > hub.Length + 1 + && fullName[hub.Length] == DotChar + && hub == fullName.Substring(0, hub.Length)) + { + yield return (hub, fullName.Substring(hub.Length + 1)); + } + } + + var hubName = fullName.Substring(0, index); + if (_hubs.Contains(hubName)) + { + var name = fullName.Substring(index + 1); + yield return (hubName, name); + } + } + + private static IReadOnlyList GetExcludedIds(string filter) + { + if (string.IsNullOrEmpty(filter)) + { + return null; + } + + return filter.Split('|').Select(s => GetName(s, PrefixHelper.ConnectionIdPrefix)).ToArray(); + } + + private static Dictionary> GetPayloads(ReadOnlyMemory data) + { + return new Dictionary> + { + { "json", data } + }; + } + + private static bool TryGetName(string qualifiedName, string prefix, out string name) + { + if (qualifiedName.Length > prefix.Length && qualifiedName.StartsWith(prefix)) + { + name = qualifiedName.Substring(prefix.Length); + + return true; + } + + name = default; + return false; + } + + private static string GetName(string qualifiedName, string prefix) + { + if (TryGetName(qualifiedName, prefix, out var name)) + { + return name; + } + + throw new InvalidDataException($"{qualifiedName} is not valid."); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Internals/ListHelper.cs b/src/Microsoft.Azure.SignalR.AspNet/Internals/ListHelper.cs new file mode 100644 index 000000000..03fb919b2 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Internals/ListHelper.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Collections.Generic; +using System.Collections.ObjectModel; + +namespace Microsoft.AspNet.SignalR.Infrastructure +{ + /// + /// Copied from https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Infrastructure/ListHelper.cs + /// + internal class ListHelper + { + public static readonly IList Empty = new ReadOnlyCollection(new List()); + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Internals/MemoryPoolTextWriter.cs b/src/Microsoft.Azure.SignalR.AspNet/Internals/MemoryPoolTextWriter.cs new file mode 100644 index 000000000..2e76f312b --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Internals/MemoryPoolTextWriter.cs @@ -0,0 +1,197 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Text; +using System.IO; +using System.Globalization; + +namespace Microsoft.AspNet.SignalR.Infrastructure +{ + /// + /// Copied from https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Infrastructure/MemoryPoolTextWriter.cs + /// + internal class MemoryPoolTextWriter : TextWriter + { + private readonly IMemoryPool _memory; + + private char[] _textArray; + private int _textBegin; + private int _textEnd; + // ReSharper disable InconsistentNaming + private const int _textLength = 128; + // ReSharper restore InconsistentNaming + + private byte[] _dataArray; + private int _dataEnd; + + private readonly Encoder _encoder; + + internal static readonly byte[] EmptyArray = new byte[0]; + + public ArraySegment Buffer + { + get + { + return new ArraySegment(_dataArray, 0, _dataEnd); + } + } + + public MemoryPoolTextWriter(IMemoryPool memory) + : base(CultureInfo.InvariantCulture) + { + _memory = memory; + _textArray = _memory.AllocChar(_textLength); + _dataArray = EmptyArray; + _encoder = Encoding.UTF8.GetEncoder(); + } + + public override Encoding Encoding + { + get + { + return Encoding.UTF8; + } + } + + protected override void Dispose(bool disposing) + { + try + { + if (disposing) + { + if (_textArray != null) + { + _memory.FreeChar(_textArray); + _textArray = null; + } + if (_dataArray != null) + { + _memory.FreeByte(_dataArray); + _dataArray = null; + } + } + } + finally + { + base.Dispose(disposing); + } + } + + private void Encode(bool flush) + { + var bytesNeeded = _encoder.GetByteCount( + _textArray, + _textBegin, + _textEnd - _textBegin, + flush); + + Grow(bytesNeeded); + + var bytesUsed = _encoder.GetBytes( + _textArray, + _textBegin, + _textEnd - _textBegin, + _dataArray, + _dataEnd, + flush); + + _textBegin = _textEnd = 0; + _dataEnd += bytesUsed; + } + + protected void Grow(int minimumAvailable) + { + if (_dataArray.Length - _dataEnd >= minimumAvailable) + { + return; + } + + var newLength = _dataArray.Length + Math.Max(_dataArray.Length, minimumAvailable); + var newArray = _memory.AllocByte(newLength); + Array.Copy(_dataArray, 0, newArray, 0, _dataEnd); + _memory.FreeByte(_dataArray); + _dataArray = newArray; + } + + public override void Write(char value) + { + if (_textLength == _textEnd) + { + Encode(false); + if (_textLength == _textEnd) + { + throw new InvalidOperationException("Unexplainable failure to encode text"); + } + } + + _textArray[_textEnd++] = value; + } + + public override void Write(char[] value, int index, int length) + { + // this override exists as an optimization to avoid too many calls to Write(char) + var sourceIndex = index; + var sourceLength = index + length; + while (sourceIndex < sourceLength) + { + if (_textLength == _textEnd) + { + Encode(false); + } + + var count = sourceLength - sourceIndex; + if (count > _textLength - _textEnd) + { + count = _textLength - _textEnd; + } + + Array.Copy(value, sourceIndex, _textArray, _textEnd, count); + sourceIndex += count; + _textEnd += count; + } + } + + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")] + public override void Write(string value) + { + var sourceIndex = 0; + var sourceLength = value.Length; + while (sourceIndex < sourceLength) + { + if (_textLength == _textEnd) + { + Encode(false); + } + + var count = sourceLength - sourceIndex; + if (count > _textLength - _textEnd) + { + count = _textLength - _textEnd; + } + + value.CopyTo(sourceIndex, _textArray, _textEnd, count); + sourceIndex += count; + _textEnd += count; + } + } + + public override void Flush() + { + while (_textBegin != _textEnd) + { + Encode(true); + } + } + + public void Write(ArraySegment data) + { + Flush(); + + Grow(data.Count); + + System.Buffer.BlockCopy(data.Array, data.Offset, _dataArray, _dataEnd, data.Count); + _dataEnd += data.Count; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Internals/PrefixHelper.cs b/src/Microsoft.Azure.SignalR.AspNet/Internals/PrefixHelper.cs new file mode 100644 index 000000000..b504b55bd --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Internals/PrefixHelper.cs @@ -0,0 +1,99 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Microsoft.AspNet.SignalR.Infrastructure +{ + /// + /// Copied from https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Infrastructure/PrefixHelper.cs + /// + internal static class PrefixHelper + { + // Hubs + internal const string HubPrefix = "h-"; + internal const string HubGroupPrefix = "hg-"; + internal const string HubConnectionIdPrefix = "hc-"; + internal const string HubUserPrefix = "hu-"; + + // Persistent Connections + internal const string PersistentConnectionPrefix = "pc-"; + internal const string PersistentConnectionGroupPrefix = "pcg-"; + + // Both + internal const string ConnectionIdPrefix = "c-"; + + public static bool HasGroupPrefix(string value) + { + return value.StartsWith(HubGroupPrefix, StringComparison.Ordinal) || + value.StartsWith(PersistentConnectionGroupPrefix, StringComparison.Ordinal); + } + + public static string GetConnectionId(string connectionId) + { + return ConnectionIdPrefix + connectionId; + } + + public static string GetHubConnectionId(string connectionId) + { + return HubConnectionIdPrefix + connectionId; + } + + public static string GetHubName(string connectionId) + { + return HubPrefix + connectionId; + } + + public static string GetHubGroupName(string groupName) + { + return HubGroupPrefix + groupName; + } + + public static string GetHubUserId(string userId) + { + return HubUserPrefix + userId; + } + + public static string GetPersistentConnectionGroupName(string groupName) + { + return PersistentConnectionGroupPrefix + groupName; + } + + public static string GetPersistentConnectionName(string connectionName) + { + return PersistentConnectionPrefix + connectionName; + } + + public static IList GetPrefixedConnectionIds(IList connectionIds) + { + if (connectionIds.Count == 0) + { + return ListHelper.Empty; + } + + return connectionIds.Select(PrefixHelper.GetConnectionId).ToList(); + } + + public static IEnumerable RemoveGroupPrefixes(IEnumerable groups) + { + return groups.Select(PrefixHelper.RemoveGroupPrefix); + } + + public static string RemoveGroupPrefix(string name) + { + if (name.StartsWith(HubGroupPrefix, StringComparison.Ordinal)) + { + return name.Substring(HubGroupPrefix.Length); + } + + if (name.StartsWith(PersistentConnectionGroupPrefix, StringComparison.Ordinal)) + { + return name.Substring(PersistentConnectionGroupPrefix.Length); + } + + return name; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Microsoft.Azure.SignalR.AspNet.csproj b/src/Microsoft.Azure.SignalR.AspNet/Microsoft.Azure.SignalR.AspNet.csproj new file mode 100644 index 000000000..787740e5c --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Microsoft.Azure.SignalR.AspNet.csproj @@ -0,0 +1,45 @@ + + + .NET Framework SDK for Azure ASP.NET SignalR. + net461 + Microsoft.Azure.SignalR.AspNet + true + + $(VersionPrefix)-preview1-$(BuildNumber) + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage + + + + + + + + + diff --git a/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs b/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs new file mode 100644 index 000000000..3328e636d --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs @@ -0,0 +1,212 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Hubs; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.AspNet.SignalR.Tracing; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.AspNet; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; + +namespace Owin +{ + public static partial class OwinExtensions + { + /// + /// Maps Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + /// The app builder + public static IAppBuilder MapAzureSignalR(this IAppBuilder builder, string applicationName) + { + return builder.MapAzureSignalR(applicationName, new HubConfiguration()); + } + + /// + /// Maps Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + /// The hub configuration . + /// The app builder + public static IAppBuilder MapAzureSignalR(this IAppBuilder builder, string applicationName, HubConfiguration configuration) + { + return builder.MapAzureSignalR("/signalr", applicationName, configuration); + } + + /// + /// Maps Azure SignalR hubs to the app builder pipeline at the specified path. + /// + /// The app builder . + /// The path to map signalr hubs. + /// The name of your app, it is case-incensitive. + /// The hub configuration . + /// The app builder + public static IAppBuilder MapAzureSignalR(this IAppBuilder builder, string path, string applicationName, HubConfiguration configuration) + { + return builder.Map(path, subApp => subApp.RunAzureSignalR(applicationName, configuration)); + } + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName) + { + builder.RunAzureSignalR(applicationName, new HubConfiguration()); + } + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + /// The connection string of an Azure SignalR Service instance. + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName, string connectionString) + { + RunAzureSignalR(builder, applicationName, connectionString, new HubConfiguration()); + } + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr" using the connection string specified in web.config + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + /// The hub configuration . + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName, HubConfiguration configuration) + { + RunAzureSignalR(builder, applicationName, configuration, null); + } + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder . + /// The name of your app, it is case-incensitive. + /// The connection string of an Azure SignalR Service instance. + /// The hub configuration . + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName, string connectionString, HubConfiguration configuration) + { + RunAzureSignalR(builder, applicationName, configuration, s => s.ConnectionString = connectionString); + } + + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder + /// The name of your app, it is case-incensitive + /// A callback to configure the . + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName, Action optionsConfigure) + { + RunAzureSignalR(builder, applicationName, new HubConfiguration(), optionsConfigure); + } + + /// + /// Adds Azure SignalR hubs to the app builder pipeline at "/signalr". + /// + /// The app builder + /// The name of your app, it is case-incensitive + /// The hub configuration + /// A callback to configure the . + public static void RunAzureSignalR(this IAppBuilder builder, string applicationName, HubConfiguration configuration, Action optionsConfigure) + { + var serviceOptions = new ServiceOptions(); + optionsConfigure?.Invoke(serviceOptions); + RunAzureSignalRCore(builder, applicationName, configuration, serviceOptions); + } + + private static void RunAzureSignalRCore(IAppBuilder builder, string applicationName, HubConfiguration configuration, ServiceOptions options) + { + // applicationName is case insensitive, it will be lower cased in the service side + if (string.IsNullOrEmpty(applicationName)) + { + throw new ArgumentException(nameof(applicationName), "Empty application name is not allowed."); + } + + var hubs = GetAvailableHubNames(configuration); + + // TODO: Update to use Middleware when SignalR SDK is ready + // Replace default HubDispatcher with a custom one, which has its own negotiation logic + // https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Hosting/PersistentConnectionFactory.cs#L42 + configuration.Resolver.Register(typeof(PersistentConnection), () => new ServiceHubDispatcher(configuration, applicationName)); + builder.RunSignalR(typeof(PersistentConnection), configuration); + + RegisterServiceObjects(configuration, options, applicationName, hubs); + + ILoggerFactory logger; + var traceManager = configuration.Resolver.Resolve(); + if (traceManager != null) + { + logger = new LoggerFactory(new ILoggerProvider[] { new TraceManagerLoggerProvider(traceManager) }); + } + else + { + logger = new NullLoggerFactory(); + } + + if (hubs?.Count > 0) + { + // Start the server->service connection asynchronously + _ = new ConnectionFactory(hubs, configuration).StartAsync(); + } + else + { + logger.CreateLogger().Log(LogLevel.Warning, "No hubs found."); + } + } + + private static void RegisterServiceObjects(HubConfiguration configuration, ServiceOptions options, string applicationName, IReadOnlyList hubs) + { + // TODO: Using IOptions looks wierd, thinking of a way removing it + // share the same object all through + var serviceOptions = Options.Create(options); + + // For safety, ALWAYS register abstract classes or interfaces + // Some third-party DI frameworks such as Ninject, implicit self-binding concrete types: + // https://github.com/ninject/ninject/wiki/dependency-injection-with-ninject#skipping-the-type-binding-bit--implicit-self-binding-of-concrete-types + configuration.Resolver.Register(typeof(IOptions), () => serviceOptions); + + var serviceProtocol = new ServiceProtocol(); + configuration.Resolver.Register(typeof(IServiceProtocol), () => serviceProtocol); + + var provider = new EmptyProtectedData(); + configuration.Resolver.Register(typeof(IProtectedData), () => provider); + + var endpoint = new ServiceEndpointProvider(serviceOptions.Value); + configuration.Resolver.Register(typeof(IServiceEndpointProvider), () => endpoint); + + var scm = new ServiceConnectionManager(applicationName, hubs); + configuration.Resolver.Register(typeof(IServiceConnectionManager), () => scm); + + var ccm = new ClientConnectionManager(configuration); + configuration.Resolver.Register(typeof(IClientConnectionManager), () => ccm); + + var atm = new AzureTransportManager(configuration.Resolver); + configuration.Resolver.Register(typeof(ITransportManager), () => atm); + + var parser = new SignalRMessageParser(hubs, configuration.Resolver); + configuration.Resolver.Register(typeof(IMessageParser), () => parser); + + var smb = new ServiceMessageBus(configuration.Resolver); + configuration.Resolver.Register(typeof(IMessageBus), () => smb); + } + + private static IReadOnlyList GetAvailableHubNames(HubConfiguration configuration) + { + var hubManager = configuration.Resolver.Resolve(); + return hubManager?.GetHubs().Select(s => s.Name).ToList(); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Properties/AssemblyInfo.cs b/src/Microsoft.Azure.SignalR.AspNet/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..65d49384e --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.AspNet.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] + diff --git a/src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs b/src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs new file mode 100644 index 000000000..0c1a24a50 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/ServiceOptions.cs @@ -0,0 +1,44 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Configuration; + +namespace Microsoft.Azure.SignalR.AspNet +{ + /// + /// Configurable options when using Azure SignalR Service. + /// + public class ServiceOptions + { + /// + /// The key which will be used to read connection string from environment variables. + /// + public static readonly string ConnectionStringDefaultKey = Constants.Config.ConnectionStringKey; + + // Default access token lifetime + internal static readonly TimeSpan DefaultAccessTokenLifetime = TimeSpan.FromHours(1); + + /// + /// Gets or sets the connection string of Azure SignalR Service instance. + /// + public string ConnectionString { get; set; } = GetDefaultConnectionString(); + + /// + /// Gets or sets the total number of connections from SDK to Azure SignalR Service. Default value is 5. + /// + public int ConnectionCount { get; set; } = 5; + + /// + /// Gets or sets the lifetime of auto-generated access token, which will be used to authenticate with Azure SignalR Service. + /// Default value is one hour. + /// + public TimeSpan AccessTokenLifetime { get; set; } = DefaultAccessTokenLifetime; + + private static string GetDefaultConnectionString() + { + return ConfigurationManager.ConnectionStrings[ConnectionStringDefaultKey]?.ConnectionString + ?? ConfigurationManager.AppSettings[ConnectionStringDefaultKey]; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.AspNet/TraceManagerLoggerProvider.cs b/src/Microsoft.Azure.SignalR.AspNet/TraceManagerLoggerProvider.cs new file mode 100644 index 000000000..87359710f --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/TraceManagerLoggerProvider.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNet.SignalR.Tracing; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.TraceSource; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class TraceManagerLoggerProvider : ILoggerProvider + { + private readonly ITraceManager _traceManager; + + public TraceManagerLoggerProvider(ITraceManager traceManager) + { + _traceManager = traceManager; + } + + public ILogger CreateLogger(string categoryName) + { + var traceSource = _traceManager[categoryName]; + return new TraceSourceLogger(traceSource); + } + + public void Dispose() + { + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransport.cs b/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransport.cs new file mode 100644 index 000000000..97542c2b6 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransport.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Hosting; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.Protocol; +using Newtonsoft.Json; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class AzureTransport : ITransport + { + private readonly TaskCompletionSource _lifetimeTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + private readonly HostContext _context; + private readonly IMemoryPool _pool; + private readonly JsonSerializer _serializer; + private readonly IServiceProtocol _serviceProtocol; + + public AzureTransport(HostContext context, IDependencyResolver resolver) + { + _context = context; + context.Environment[AspNetConstants.Context.AzureSignalRTransportKey] = this; + _pool = resolver.Resolve(); + _serializer = resolver.Resolve(); + _serviceProtocol = resolver.Resolve(); + } + + public Func Received { get; set; } + + public Func Connected { get; set; } + + public Func Reconnected { get; set; } + + public Func Disconnected { get; set; } + + public string ConnectionId { get; set; } + + public Task GetGroupsToken() + { + return Task.FromResult(null); + } + + public async Task ProcessRequest(ITransportConnection connection) + { + var connected = Connected; + if (connected != null) + { + await connected(); + } + + await _lifetimeTcs.Task; + + var disconnected = Disconnected; + if (disconnected != null) + { + await disconnected(true); + } + } + + public Task Send(object value) + { + if (_context.Environment.TryGetValue(AspNetConstants.Context.AzureServiceConnectionKey, out var connection) && connection is IServiceConnection serviceConnection) + { + // Invoke service connection + return serviceConnection.WriteAsync(ConnectionId, value, _serviceProtocol, _serializer, _pool); + } + + throw new InvalidOperationException("No service connection found when sending message"); + } + + public void OnReceived(string value) + { + var received = Received; + if (received != null) + { + _ = received(value); + } + } + + public void OnDisconnected() => _lifetimeTcs.TrySetResult(null); + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransportManager.cs b/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransportManager.cs new file mode 100644 index 000000000..c3e56e577 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.AspNet/Transports/AzureTransportManager.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Hosting; +using Microsoft.AspNet.SignalR.Transports; + +namespace Microsoft.Azure.SignalR.AspNet +{ + internal class AzureTransportManager : ITransportManager + { + private readonly IDependencyResolver _resolver; + + public AzureTransportManager(IDependencyResolver resolver) + { + _resolver = resolver; + } + + public ITransport GetTransport(HostContext hostContext) + { + return new AzureTransport(hostContext, _resolver); + } + + public bool SupportsTransport(string transportName) + { + // This is only called for websockets, and should never be called in this flow + return false; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Common/ConnectionStringParser.cs b/src/Microsoft.Azure.SignalR.Common/ConnectionStringParser.cs new file mode 100644 index 000000000..76fead4ba --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/ConnectionStringParser.cs @@ -0,0 +1,99 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace Microsoft.Azure.SignalR +{ + internal static class ConnectionStringParser + { + private const string EndpointProperty = "endpoint"; + private const string AccessKeyProperty = "accesskey"; + private const string VersionProperty = "version"; + private const string PortProperty = "port"; + // For SDK 1.x, only support Azure SignalR Service 1.x + private const string SupportedVersion = "1"; + private const string ValidVersionRegex = "^" + SupportedVersion + @"\.\d+(?:[\w-.]+)?$"; + + private static readonly string MissingRequiredProperty = + $"Connection string missing required properties {EndpointProperty} and {AccessKeyProperty}."; + + private const string InvalidVersionValueFormat = "Version {0} is not supported."; + + private static readonly string InvalidPortValue = $"Invalid value for {PortProperty} property."; + + private static readonly char[] PropertySeparator = { ';' }; + private static readonly char[] KeyValueSeparator = { '=' }; + + internal static (string endpoint, string accessKey, string version, int? port) Parse(string connectionString) + { + var properties = connectionString.Split(PropertySeparator, StringSplitOptions.RemoveEmptyEntries); + if (properties.Length < 2) + { + throw new ArgumentException(MissingRequiredProperty, nameof(connectionString)); + } + + var dict = new Dictionary(StringComparer.OrdinalIgnoreCase); + foreach (var property in properties) + { + var kvp = property.Split(KeyValueSeparator, 2); + if (kvp.Length != 2) continue; + + var key = kvp[0].Trim(); + if (dict.ContainsKey(key)) + { + throw new ArgumentException($"Duplicate properties found in connection string: {key}."); + } + + dict.Add(key, kvp[1].Trim()); + } + + if (!dict.ContainsKey(EndpointProperty) || !dict.ContainsKey(AccessKeyProperty)) + { + throw new ArgumentException(MissingRequiredProperty, nameof(connectionString)); + } + + if (!ValidateEndpoint(dict[EndpointProperty])) + { + throw new ArgumentException($"Endpoint property in connection string is not a valid URI: {dict[EndpointProperty]}."); + } + + string version = null; + if (dict.TryGetValue(VersionProperty, out var v)) + { + if (Regex.IsMatch(v, ValidVersionRegex)) + { + version = v; + } + else + { + throw new ArgumentException(string.Format(InvalidVersionValueFormat, v), nameof(connectionString)); + } + } + + int? port = null; + if (dict.TryGetValue(PortProperty, out var s)) + { + if (int.TryParse(s, out var p) && + p > 0 && p <= 0xFFFF) + { + port = p; + } + else + { + throw new ArgumentException(InvalidPortValue, nameof(connectionString)); + } + } + + return (dict[EndpointProperty].TrimEnd('/'), dict[AccessKeyProperty], version, port); + } + + internal static bool ValidateEndpoint(string endpoint) + { + return Uri.TryCreate(endpoint, UriKind.Absolute, out var uriResult) && + (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR.Common/Constants.cs b/src/Microsoft.Azure.SignalR.Common/Constants.cs new file mode 100644 index 000000000..1b23da5ae --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/Constants.cs @@ -0,0 +1,33 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR +{ + internal static class Constants + { + public static class ClaimType + { + public const string AzureSignalRSysPrefix = "asrs.s."; + public const string AuthenticationType = AzureSignalRSysPrefix + "aut"; + public const string UserId = AzureSignalRSysPrefix + "uid"; + public const string AppName = AzureSignalRSysPrefix + "apn"; + + public const string AzureSignalRUserPrefix = "asrs.u."; + } + + public static class Path + { + public const string Negotiate = "/negotiate"; + } + + public static class QueryParameter + { + public const string OriginalPath = "asrs.op"; + } + + public static class Config + { + public static readonly string ConnectionStringKey = "Azure:SignalR:ConnectionString"; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Common/DuplexPipe.cs b/src/Microsoft.Azure.SignalR.Common/DuplexPipe.cs new file mode 100644 index 000000000..cee167b20 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/DuplexPipe.cs @@ -0,0 +1,42 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace System.IO.Pipelines +{ + internal class DuplexPipe : IDuplexPipe + { + public DuplexPipe(PipeReader reader, PipeWriter writer) + { + Input = reader; + Output = writer; + } + + public PipeReader Input { get; } + + public PipeWriter Output { get; } + + public static DuplexPipePair CreateConnectionPair(PipeOptions inputOptions, PipeOptions outputOptions) + { + var input = new Pipe(inputOptions); + var output = new Pipe(outputOptions); + + var transportToApplication = new DuplexPipe(output.Reader, input.Writer); + var applicationToTransport = new DuplexPipe(input.Reader, output.Writer); + + return new DuplexPipePair(applicationToTransport, transportToApplication); + } + + // This class exists to work around issues with value tuple on .NET Framework + public readonly struct DuplexPipePair + { + public IDuplexPipe Transport { get; } + public IDuplexPipe Application { get; } + + public DuplexPipePair(IDuplexPipe transport, IDuplexPipe application) + { + Transport = transport; + Application = application; + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Common/IServiceConnection.cs b/src/Microsoft.Azure.SignalR.Common/IServiceConnection.cs new file mode 100644 index 000000000..e3535f62f --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/IServiceConnection.cs @@ -0,0 +1,17 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading.Tasks; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR +{ + internal interface IServiceConnection + { + Task StartAsync(); + + Task WriteAsync(ServiceMessage serviceMessage); + + Task StopAsync(); + } +} diff --git a/src/Microsoft.Azure.SignalR.Common/IServiceEndpointGenerator.cs b/src/Microsoft.Azure.SignalR.Common/IServiceEndpointGenerator.cs new file mode 100644 index 000000000..d5f326bf7 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/IServiceEndpointGenerator.cs @@ -0,0 +1,13 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR +{ + internal interface IServiceEndpointGenerator + { + string GetClientAudience(string hubName); + string GetClientEndpoint(string hubName, string originalPath, string queryString); + string GetServerAudience(string hubName); + string GetServerEndpoint(string hubName); + } +} diff --git a/src/Microsoft.Azure.SignalR.Common/Microsoft.Azure.SignalR.Common.csproj b/src/Microsoft.Azure.SignalR.Common/Microsoft.Azure.SignalR.Common.csproj new file mode 100644 index 000000000..3044d7b85 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/Microsoft.Azure.SignalR.Common.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + false + + + + + + + + + + + diff --git a/src/Microsoft.Azure.SignalR.Common/Properties/AssemblyInfo.cs b/src/Microsoft.Azure.SignalR.Common/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..bb98da4d1 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/Properties/AssemblyInfo.cs @@ -0,0 +1,7 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.Common.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.AspNet, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.AspNet.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] diff --git a/src/Microsoft.Azure.SignalR.Common/ServiceConnectionBase.cs b/src/Microsoft.Azure.SignalR.Common/ServiceConnectionBase.cs new file mode 100644 index 000000000..5ed297ad7 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/ServiceConnectionBase.cs @@ -0,0 +1,648 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Diagnostics; +using System.IO; +using System.IO.Pipelines; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR +{ + internal abstract class ServiceConnectionBase : IServiceConnection + { + private static readonly TimeSpan DefaultHandshakeTimeout = TimeSpan.FromSeconds(15); + // Service ping rate is 15 sec; this is 2 times that. + private static readonly TimeSpan DefaultServiceTimeout = TimeSpan.FromSeconds(30); + private static readonly long DefaultServiceTimeoutTicks = DefaultServiceTimeout.Seconds * Stopwatch.Frequency; + // App server ping rate is 5 sec. So service can detect an irresponsive server connection in 10 seconds at most. + private static readonly TimeSpan DefaultKeepAliveInterval = TimeSpan.FromSeconds(5); + private static readonly int MaxReconnectBackoffInternalInMilliseconds = 1000; + + // Start reconnect after a random interval less than 1 second + private static TimeSpan ReconnectInterval => + TimeSpan.FromMilliseconds(StaticRandom.Next(MaxReconnectBackoffInternalInMilliseconds)); + + private readonly ReadOnlyMemory _cachedPingBytes; + private readonly HandshakeRequestMessage _handshakeRequest; + + private readonly SemaphoreSlim _serviceConnectionLock = new SemaphoreSlim(1, 1); + private readonly IServiceProtocol _serviceProtocol; + + private readonly TaskCompletionSource _serviceConnectionStartTcs = new TaskCompletionSource(TaskContinuationOptions.RunContinuationsAsynchronously); + + protected readonly ILogger _logger; + protected readonly string _connectionId; + + private bool _isStopped; + private long _lastReceiveTimestamp; + protected ConnectionContext _connection; + + public Task WaitForConnectionStart => _serviceConnectionStartTcs.Task; + + public ServiceConnectionBase(IServiceProtocol serviceProtocol, ILogger logger, string connectionId) + { + _serviceProtocol = serviceProtocol; + _logger = logger; + _connectionId = connectionId; + + _cachedPingBytes = _serviceProtocol.GetMessageBytes(PingMessage.Instance); + _handshakeRequest = new HandshakeRequestMessage(_serviceProtocol.Version); + } + + public async Task StartAsync() + { + while (!_isStopped) + { + // If we are not able to start, we will quit this connection. + if (!await StartAsyncCore()) + { + _serviceConnectionStartTcs.TrySetResult(false); + return; + } + + _serviceConnectionStartTcs.TrySetResult(true); + + await ProcessIncomingAsync(); + } + } + + // For test purpose only + public Task StopAsync() + { + _isStopped = true; + _connection?.Transport.Input.CancelPendingRead(); + return Task.CompletedTask; + } + + public async virtual Task WriteAsync(ServiceMessage serviceMessage) + { + // We have to lock around outgoing sends since the pipe is single writer. + // The lock is per serviceConnection + await _serviceConnectionLock.WaitAsync(); + + if (_connection == null) + { + _serviceConnectionLock.Release(); + throw new InvalidOperationException("The connection is not active, data cannot be sent to the service."); + } + + try + { + // Write the service protocol message + _serviceProtocol.WriteMessage(serviceMessage, _connection.Transport.Output); + await _connection.Transport.Output.FlushAsync(); + } + catch (Exception ex) + { + Log.FailedToWrite(_logger, ex); + } + finally + { + _serviceConnectionLock.Release(); + } + } + + protected abstract Task CreateConnection(); + + protected abstract Task DisposeConnection(); + + protected abstract Task CleanupConnections(); + + protected abstract Task OnConnectedAsync(OpenConnectionMessage openConnectionMessage); + + protected abstract Task OnDisconnectedAsync(CloseConnectionMessage closeConnectionMessage); + + protected abstract Task OnMessageAsync(ConnectionDataMessage connectionDataMessage); + + private async Task StartAsyncCore() + { + // Always try until connected + while (true) + { + // Lock here in case somebody tries to send before the connection is assigned + await _serviceConnectionLock.WaitAsync(); + + try + { + _connection = await CreateConnection(); + + if (await HandshakeAsync()) + { + Log.ServiceConnectionConnected(_logger, _connectionId); + return true; + } + else + { + // False means we got a HandshakeResponseMessage with error. Will take below actions: + // - Dispose the connection + // - Stop reconnect + await DisposeConnection(); + + return false; + } + } + catch (Exception ex) + { + Log.FailedToConnect(_logger, ex); + + await DisposeConnection(); + + await Task.Delay(ReconnectInterval); + } + finally + { + _serviceConnectionLock.Release(); + } + } + } + + private async Task HandshakeAsync() + { + await SendHandshakeRequestAsync(_connection.Transport.Output); + + try + { + using (var cts = new CancellationTokenSource()) + { + if (!Debugger.IsAttached) + { + cts.CancelAfter(DefaultHandshakeTimeout); + } + + if (await ReceiveHandshakeResponseAsync(_connection.Transport.Input, cts.Token)) + { + Log.HandshakeComplete(_logger); + return true; + } + + return false; + } + } + catch (Exception ex) + { + Log.ErrorReceivingHandshakeResponse(_logger, ex); + throw; + } + } + + private async Task SendHandshakeRequestAsync(PipeWriter output) + { + Log.SendingHandshakeRequest(_logger); + + _serviceProtocol.WriteMessage(_handshakeRequest, output); + var sendHandshakeResult = await output.FlushAsync(); + if (sendHandshakeResult.IsCompleted) + { + throw new InvalidOperationException("Service disconnected before handshake complete."); + } + } + + private async Task ReceiveHandshakeResponseAsync(PipeReader input, CancellationToken token) + { + while (true) + { + var result = await input.ReadAsync(token); + + var buffer = result.Buffer; + var consumed = buffer.Start; + var examined = buffer.End; + + try + { + if (result.IsCanceled) + { + throw new InvalidOperationException("Connection cancelled before handshake complete."); + } + + if (!buffer.IsEmpty) + { + if (_serviceProtocol.TryParseMessage(ref buffer, out var message)) + { + consumed = buffer.Start; + examined = consumed; + + if (!(message is HandshakeResponseMessage handshakeResponse)) + { + throw new InvalidDataException( + $"{message.GetType().Name} received when waiting for handshake response."); + } + + if (string.IsNullOrEmpty(handshakeResponse.ErrorMessage)) + { + return true; + } + + // Handshake error. Will stop reconnect. + Log.HandshakeError(_logger, handshakeResponse.ErrorMessage); + return false; + } + } + + if (result.IsCompleted) + { + // Not enough data, and we won't be getting any more data. + throw new InvalidOperationException("Service disconnected before sending a handshake response."); + } + } + finally + { + input.AdvanceTo(consumed, examined); + } + } + } + + private async Task ProcessIncomingAsync() + { + var keepAliveTimer = StartKeepAliveTimer(); + try + { + while (true) + { + var result = await _connection.Transport.Input.ReadAsync(); + var buffer = result.Buffer; + + try + { + if (result.IsCanceled) + { + Log.ReadingCancelled(_logger, _connectionId); + break; + } + + if (!buffer.IsEmpty) + { + Log.ReceivedMessage(_logger, buffer.Length, _connectionId); + + UpdateReceiveTimestamp(); + + while (_serviceProtocol.TryParseMessage(ref buffer, out var message)) + { + _ = DispatchMessageAsync(message); + } + } + + if (result.IsCompleted) + { + // The connection is closed (reconnect) + Log.ServiceConnectionClosed(_logger, _connectionId); + break; + } + } + catch (Exception ex) + { + // Error occurs in handling the message, but the connection between SDK and service still works. + // So, just log error instead of breaking the connection + Log.ErrorProcessingMessages(_logger, ex); + } + finally + { + _connection.Transport.Input.AdvanceTo(buffer.Start, buffer.End); + } + } + } + catch (Exception ex) + { + // Fatal error: There is something wrong for the connection between SDK and service. + // Abort all the client connections, close the httpConnection. + // Only reconnect can recover. + Log.ConnectionDropped(_logger, _connectionId, ex); + } + finally + { + keepAliveTimer.Stop(); + + await _serviceConnectionLock.WaitAsync(); + try + { + await DisposeConnection(); + } + finally + { + _serviceConnectionLock.Release(); + } + } + + // TODO: Never cleanup connections unless Service asks us to do that + // Current implementation is based on assumption that Service will drop clients + // if server connection fails. + await CleanupConnections(); + } + + private Task DispatchMessageAsync(ServiceMessage message) + { + switch (message) + { + case OpenConnectionMessage openConnectionMessage: + return OnConnectedAsync(openConnectionMessage); + case CloseConnectionMessage closeConnectionMessage: + return OnDisconnectedAsync(closeConnectionMessage); + case ConnectionDataMessage connectionDataMessage: + return OnMessageAsync(connectionDataMessage); + case PingMessage _: + // ignore ping + break; + } + return Task.CompletedTask; + } + + private TimerAwaitable StartKeepAliveTimer() + { + Log.StartingKeepAliveTimer(_logger, DefaultKeepAliveInterval); + + _lastReceiveTimestamp = Stopwatch.GetTimestamp(); + var timer = new TimerAwaitable(DefaultKeepAliveInterval, DefaultKeepAliveInterval); + _ = KeepAliveAsync(timer); + + return timer; + } + + private void UpdateReceiveTimestamp() + { + Interlocked.Exchange(ref _lastReceiveTimestamp, Stopwatch.GetTimestamp()); + } + + private async Task KeepAliveAsync(TimerAwaitable timer) + { + using (timer) + { + timer.Start(); + + while (await timer) + { + if (Stopwatch.GetTimestamp() - Interlocked.Read(ref _lastReceiveTimestamp) > DefaultServiceTimeoutTicks) + { + AbortConnection(); + // We shouldn't get here twice. + continue; + } + + // Send PingMessage to Service + await TrySendPingAsync(); + } + } + } + + private async ValueTask TrySendPingAsync() + { + if (!_serviceConnectionLock.Wait(0)) + { + // Skip sending PingMessage when failed getting lock + return; + } + + try + { + await _connection.Transport.Output.WriteAsync(_cachedPingBytes); + Log.SentPing(_logger); + } + catch (Exception ex) + { + Log.FailedSendingPing(_logger, ex); + } + finally + { + _serviceConnectionLock.Release(); + } + } + + private void AbortConnection() + { + if (!_serviceConnectionLock.Wait(0)) + { + // Couldn't get the lock so skip the cancellation (we could be in the middle of reconnecting?) + return; + } + + try + { + // Stop the reading from connection + if (_connection != null) + { + _connection.Transport.Input.CancelPendingRead(); + Log.ServiceTimeout(_logger, DefaultServiceTimeout); + } + } + finally + { + _serviceConnectionLock.Release(); + } + } + + private static class Log + { + // Category: ServiceConnection + private static readonly Action _failedToWrite = + LoggerMessage.Define(LogLevel.Error, new EventId(1, "FailedToWrite"), "Failed to send message to the service."); + + private static readonly Action _failedToConnect = + LoggerMessage.Define(LogLevel.Error, new EventId(2, "FailedToConnect"), "Failed to connect to the service."); + + private static readonly Action _errorProcessingMessages = + LoggerMessage.Define(LogLevel.Error, new EventId(3, "ErrorProcessingMessages"), "Error when processing messages."); + + private static readonly Action _connectionDropped = + LoggerMessage.Define(LogLevel.Error, new EventId(4, "ConnectionDropped"), "Connection {ServiceConnectionId} to the service was dropped."); + + private static readonly Action _failedToCleanupConnections = + LoggerMessage.Define(LogLevel.Error, new EventId(5, "FailedToCleanupConnection"), "Failed to clean up client connections."); + + private static readonly Action _errorSendingMessage = + LoggerMessage.Define(LogLevel.Error, new EventId(6, "ErrorSendingMessage"), "Error while sending message to the service."); + + private static readonly Action _sendLoopStopped = + LoggerMessage.Define(LogLevel.Error, new EventId(7, "SendLoopStopped"), "Error while processing messages from {TransportConnectionId}."); + + private static readonly Action _applicationTaskFailed = + LoggerMessage.Define(LogLevel.Error, new EventId(8, "ApplicationTaskFailed"), "Application task failed."); + + private static readonly Action _failToWriteMessageToApplication = + LoggerMessage.Define(LogLevel.Error, new EventId(9, "FailToWriteMessageToApplication"), "Failed to write message to {TransportConnectionId}."); + + private static readonly Action _receivedMessageForNonExistentConnection = + LoggerMessage.Define(LogLevel.Warning, new EventId(10, "ReceivedMessageForNonExistentConnection"), "Received message for connection {TransportConnectionId} which does not exist."); + + private static readonly Action _connectedStarting = + LoggerMessage.Define(LogLevel.Debug, new EventId(11, "ConnectedStarting"), "Connection {TransportConnectionId} started."); + + private static readonly Action _connectedEnding = + LoggerMessage.Define(LogLevel.Debug, new EventId(12, "ConnectedEnding"), "Connection {TransportConnectionId} ended."); + + private static readonly Action _closeConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(13, "CloseConnection"), "Sending close connection message to the service for {TransportConnectionId}."); + + private static readonly Action _serviceConnectionClosed = + LoggerMessage.Define(LogLevel.Debug, new EventId(14, "serviceConnectionClose"), "Service connection {ServiceConnectionId} closed."); + + private static readonly Action _readingCancelled = + LoggerMessage.Define(LogLevel.Trace, new EventId(15, "ReadingCancelled"), "Reading from service connection {ServiceConnectionId} cancelled."); + + private static readonly Action _receivedMessage = + LoggerMessage.Define(LogLevel.Debug, new EventId(16, "ReceivedMessage"), "Received {ReceivedBytes} bytes from service {ServiceConnectionId}."); + + private static readonly Action _startingKeepAliveTimer = + LoggerMessage.Define(LogLevel.Trace, new EventId(17, "StartingKeepAliveTimer"), "Starting keep-alive timer. Duration: {KeepAliveInterval:0.00}ms"); + + private static readonly Action _serviceTimeout = + LoggerMessage.Define(LogLevel.Error, new EventId(18, "ServiceTimeout"), "Service timeout. {ServiceTimeout:0.00}ms elapsed without receiving a message from service."); + + private static readonly Action _writeMessageToApplication = + LoggerMessage.Define(LogLevel.Trace, new EventId(19, "WriteMessageToApplication"), "Writing {ReceivedBytes} to connection {TransportConnectionId}."); + + private static readonly Action _serviceConnectionConnected = + LoggerMessage.Define(LogLevel.Debug, new EventId(20, "ServiceConnectionConnected"), "Service connection {ServiceConnectionId} connected."); + + private static readonly Action _sendingHandshakeRequest = + LoggerMessage.Define(LogLevel.Debug, new EventId(21, "SendingHandshakeRequest"), "Sending Handshake request to service."); + + private static readonly Action _handshakeComplete = + LoggerMessage.Define(LogLevel.Debug, new EventId(22, "HandshakeComplete"), "Handshake with service completes."); + + private static readonly Action _errorReceivingHandshakeResponse = + LoggerMessage.Define(LogLevel.Error, new EventId(23, "ErrorReceivingHandshakeResponse"), "Error receiving handshake response."); + + private static readonly Action _handshakeError = + LoggerMessage.Define(LogLevel.Critical, new EventId(24, "HandshakeError"), "Service returned handshake error: {Error}"); + + private static readonly Action _sentPing = + LoggerMessage.Define(LogLevel.Debug, new EventId(25, "SentPing"), "Sent a ping message to service."); + + private static readonly Action _failedSendingPing = + LoggerMessage.Define(LogLevel.Warning, new EventId(26, "FailedSendingPing"), "Failed sending a ping message to service."); + + public static void FailedToWrite(ILogger logger, Exception exception) + { + _failedToWrite(logger, exception); + } + + public static void FailedToConnect(ILogger logger, Exception exception) + { + _failedToConnect(logger, exception); + } + + public static void ErrorProcessingMessages(ILogger logger, Exception exception) + { + _errorProcessingMessages(logger, exception); + } + + public static void ConnectionDropped(ILogger logger, string serviceConnectionId, Exception exception) + { + _connectionDropped(logger, serviceConnectionId, exception); + } + + public static void FailedToCleanupConnections(ILogger logger, Exception exception) + { + _failedToCleanupConnections(logger, exception); + } + + public static void ErrorSendingMessage(ILogger logger, Exception exception) + { + _errorSendingMessage(logger, exception); + } + + public static void SendLoopStopped(ILogger logger, string connectionId, Exception exception) + { + _sendLoopStopped(logger, connectionId, exception); + } + + public static void ApplicaitonTaskFailed(ILogger logger, Exception exception) + { + _applicationTaskFailed(logger, exception); + } + + public static void FailToWriteMessageToApplication(ILogger logger, string connectionId, Exception exception) + { + _failToWriteMessageToApplication(logger, connectionId, exception); + } + + public static void ReceivedMessageForNonExistentConnection(ILogger logger, string connectionId) + { + _receivedMessageForNonExistentConnection(logger, connectionId, null); + } + + public static void ConnectedStarting(ILogger logger, string connectionId) + { + _connectedStarting(logger, connectionId, null); + } + + public static void ConnectedEnding(ILogger logger, string connectionId) + { + _connectedEnding(logger, connectionId, null); + } + + public static void CloseConnection(ILogger logger, string connectionId) + { + _closeConnection(logger, connectionId, null); + } + + public static void ServiceConnectionClosed(ILogger logger, string serviceConnectionId) + { + _serviceConnectionClosed(logger, serviceConnectionId, null); + } + + public static void ServiceConnectionConnected(ILogger logger, string serviceConnectionId) + { + _serviceConnectionConnected(logger, serviceConnectionId, null); + } + + public static void ReadingCancelled(ILogger logger, string serviceConnectionId) + { + _readingCancelled(logger, serviceConnectionId, null); + } + + public static void ReceivedMessage(ILogger logger, long bytes, string serviceConnectionId) + { + _receivedMessage(logger, bytes, serviceConnectionId, null); + } + + public static void StartingKeepAliveTimer(ILogger logger, TimeSpan keepAliveInterval) + { + _startingKeepAliveTimer(logger, keepAliveInterval.TotalMilliseconds, null); + } + + public static void ServiceTimeout(ILogger logger, TimeSpan serviceTimeout) + { + _serviceTimeout(logger, serviceTimeout.TotalMilliseconds, null); + } + + public static void WriteMessageToApplication(ILogger logger, long count, string connectionId) + { + _writeMessageToApplication(logger, count, connectionId, null); + } + + public static void SendingHandshakeRequest(ILogger logger) + { + _sendingHandshakeRequest(logger, null); + } + + public static void HandshakeComplete(ILogger logger) + { + _handshakeComplete(logger, null); + } + + public static void ErrorReceivingHandshakeResponse(ILogger logger, Exception exception) + { + _errorReceivingHandshakeResponse(logger, exception); + } + + public static void HandshakeError(ILogger logger, string error) + { + _handshakeError(logger, error, null); + } + + public static void SentPing(ILogger logger) + { + _sentPing(logger, null); + } + + public static void FailedSendingPing(ILogger logger, Exception exception) + { + _failedSendingPing(logger, exception); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/Utilities/StaticRandom.cs b/src/Microsoft.Azure.SignalR.Common/StaticRandom.cs similarity index 100% rename from src/Microsoft.Azure.SignalR/Utilities/StaticRandom.cs rename to src/Microsoft.Azure.SignalR.Common/StaticRandom.cs diff --git a/src/Microsoft.Azure.SignalR.Common/TimerAwaitable.cs b/src/Microsoft.Azure.SignalR.Common/TimerAwaitable.cs new file mode 100644 index 000000000..a20c20726 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Common/TimerAwaitable.cs @@ -0,0 +1,109 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.Azure.SignalR +{ + internal class TimerAwaitable : IDisposable, ICriticalNotifyCompletion + { + private Timer _timer; + private Action _callback; + private static readonly Action _callbackCompleted = () => { }; + + private readonly TimeSpan _period; + + private readonly TimeSpan _dueTime; + private bool _disposed; + private bool _running = true; + private object _lockObj = new object(); + + public TimerAwaitable(TimeSpan dueTime, TimeSpan period) + { + _dueTime = dueTime; + _period = period; + } + + public void Start() + { + if (_timer == null) + { + lock (_lockObj) + { + if (_disposed) + { + return; + } + + if (_timer == null) + { + _timer = new Timer(state => ((TimerAwaitable)state).Tick(), this, _dueTime, _period); + } + } + } + } + + public TimerAwaitable GetAwaiter() => this; + public bool IsCompleted => ReferenceEquals(_callback, _callbackCompleted); + + public bool GetResult() + { + _callback = null; + + return _running; + } + + private void Tick() + { + var continuation = Interlocked.Exchange(ref _callback, _callbackCompleted); + continuation?.Invoke(); + } + + public void OnCompleted(Action continuation) + { + if (ReferenceEquals(_callback, _callbackCompleted) || + ReferenceEquals(Interlocked.CompareExchange(ref _callback, continuation, null), _callbackCompleted)) + { + Task.Run(continuation); + } + } + + public void UnsafeOnCompleted(Action continuation) + { + OnCompleted(continuation); + } + + public void Stop() + { + lock (_lockObj) + { + // Stop should be used to trigger the call to end the loop which disposes + if (_disposed) + { + throw new ObjectDisposedException(GetType().FullName); + } + + _running = false; + } + + // Call tick here to make sure that we yield the callback, + // if it's currently waiting, we don't need to wait for the next period + Tick(); + } + + void IDisposable.Dispose() + { + lock (_lockObj) + { + _disposed = true; + + _timer?.Dispose(); + + _timer = null; + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Owin/AppBuilderExtensions.cs b/src/Microsoft.Azure.SignalR.Owin/AppBuilderExtensions.cs deleted file mode 100644 index d5516760c..000000000 --- a/src/Microsoft.Azure.SignalR.Owin/AppBuilderExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Microsoft.Azure.SignalR; - -namespace Owin -{ - public static class AzureSignalRAppBuilderExtensions - { - public static IAppBuilder UseAzureSignalR(this IAppBuilder app, string connectionString, - Action configure) - { - var builder = new HubHostBuilder(CloudSignalR.ServiceProvider, - CloudSignalR.CreateEndpointProviderFromConnectionString(connectionString), - CloudSignalR.CreateTokenProviderFromConnectionString(connectionString)); - configure(builder); - - return app; - } - } -} diff --git a/src/Microsoft.Azure.SignalR.Owin/Microsoft.Azure.SignalR.Owin.csproj b/src/Microsoft.Azure.SignalR.Owin/Microsoft.Azure.SignalR.Owin.csproj deleted file mode 100644 index 9284658ef..000000000 --- a/src/Microsoft.Azure.SignalR.Owin/Microsoft.Azure.SignalR.Owin.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - net461 - - - - - - - - - - - diff --git a/src/Microsoft.Azure.SignalR.Protocols/ConnectionMessage.cs b/src/Microsoft.Azure.SignalR.Protocols/ConnectionMessage.cs new file mode 100644 index 000000000..c9316fdb1 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/ConnectionMessage.cs @@ -0,0 +1,133 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.Extensions.Primitives; + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// Base class of connection-specific messages between Azure SignalR Service and SDK. + /// + public abstract class ConnectionMessage : ServiceMessage + { + protected ConnectionMessage(string connectionId) + { + ConnectionId = connectionId; + } + + /// + /// Gets or sets the connection Id. + /// + public string ConnectionId { get; set; } + } + + /// + /// A open-connection message. + /// + public class OpenConnectionMessage : ConnectionMessage + { + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// An array of associated with the connection. + public OpenConnectionMessage(string connectionId, Claim[] claims) + : this(connectionId, claims, new Dictionary(StringComparer.OrdinalIgnoreCase), string.Empty) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// An array of associated with the connection. + /// A associated with the connection. + /// Query string associated with the connection. + public OpenConnectionMessage(string connectionId, Claim[] claims, IDictionary headers, string queryString) + : base(connectionId) + { + Claims = claims; + Headers = headers; + QueryString = queryString; + } + + /// + /// Gets or sets the associated claims. + /// + public Claim[] Claims { get; set; } + + /// + /// Gets or sets the associated headers. + /// + public IDictionary Headers { get; set; } + + /// + /// Gets or sets the associated query string. + /// + public string QueryString { get; set; } + } + + /// + /// A close-connection message. + /// + public class CloseConnectionMessage : ConnectionMessage + { + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + public CloseConnectionMessage(string connectionId) : this(connectionId, null) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// Optional error message. + public CloseConnectionMessage(string connectionId, string errorMessage) : base(connectionId) + { + ErrorMessage = errorMessage; + } + + /// + /// Gets or sets the error message. + /// + public string ErrorMessage { get; set; } + } + + /// + /// A connection data message. + /// + public class ConnectionDataMessage : ConnectionMessage + { + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// Binary data to be delivered. + public ConnectionDataMessage(string connectionId, ReadOnlyMemory payload) : base(connectionId) + { + Payload = new ReadOnlySequence(payload); + } + + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// Binary data to be delivered. + public ConnectionDataMessage(string connectionId, ReadOnlySequence payload) : base(connectionId) + { + Payload = payload; + } + + /// + /// Gets or sets the binary payload. + /// + public ReadOnlySequence Payload { get; set; } + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/GroupMessage.cs b/src/Microsoft.Azure.SignalR.Protocols/GroupMessage.cs new file mode 100644 index 000000000..bfc0222dc --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/GroupMessage.cs @@ -0,0 +1,59 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// A join-group message. + /// + public class JoinGroupMessage : ServiceMessage + { + /// + /// Gets or sets the connection Id. + /// + public string ConnectionId { get; set; } + + /// + /// Gets or sets the group name. + /// + public string GroupName { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// The group name, to which the connection will join. + public JoinGroupMessage(string connectionId, string groupName) + { + ConnectionId = connectionId; + GroupName = groupName; + } + } + + /// + /// A leave-group message. + /// + public class LeaveGroupMessage : ServiceMessage + { + /// + /// Gets or sets the connection Id. + /// + public string ConnectionId { get; set; } + + /// + /// Gets or sets the group name. + /// + public string GroupName { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The connection Id. + /// The group name, from which the connection will leave. + public LeaveGroupMessage(string connectionId, string groupName) + { + ConnectionId = connectionId; + GroupName = groupName; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/IServiceProtocol.cs b/src/Microsoft.Azure.SignalR.Protocols/IServiceProtocol.cs new file mode 100644 index 000000000..12425a552 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/IServiceProtocol.cs @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// A protocol abstraction for communication between Azure SignalR Service and SDK. + /// + public interface IServiceProtocol + { + /// + /// Gets the version of the protocol. + /// + int Version { get; } + + /// + /// Creates a new from the specified serialized representation. + /// + /// The serialized representation of the message. + /// When this method returns true, contains the parsed message. + /// A value that is true if the was successfully parsed; otherwise, false. + bool TryParseMessage(ref ReadOnlySequence input, out ServiceMessage message); + + /// + /// Writes the specified to a writer. + /// + /// The message to write. + /// The output writer. + void WriteMessage(ServiceMessage message, IBufferWriter output); + + /// + /// Converts the specified to its serialized representation. + /// + /// The message to convert. + /// The serialized representation of the message. + ReadOnlyMemory GetMessageBytes(ServiceMessage message); + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/Microsoft.Azure.SignalR.Protocols.csproj b/src/Microsoft.Azure.SignalR.Protocols/Microsoft.Azure.SignalR.Protocols.csproj new file mode 100644 index 000000000..fc60006b9 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/Microsoft.Azure.SignalR.Protocols.csproj @@ -0,0 +1,21 @@ + + + .NET Standard SDK for Azure SignalR Service protocol. + netstandard2.0 + Microsoft.Azure.SignalR.Protocol + true + + + + + + + + + + + + + + + diff --git a/src/Microsoft.Azure.SignalR.Protocols/MulticastDataMessage.cs b/src/Microsoft.Azure.SignalR.Protocols/MulticastDataMessage.cs new file mode 100644 index 000000000..d583835a1 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/MulticastDataMessage.cs @@ -0,0 +1,177 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// Base class for multicast data messages between Azure SignalR Service and SDK. + /// + public abstract class MulticastDataMessage : ServiceMessage + { + protected MulticastDataMessage(IDictionary> payloads) + { + Payloads = payloads; + } + + /// + /// Gets or sets the payload dictionary which contains binary payload of multiple protocols. + /// + public IDictionary> Payloads { get; set; } + } + + /// + /// A data message which will be sent to multiple connections. + /// + public class MultiConnectionDataMessage : MulticastDataMessage + { + /// + /// Initializes a new instance of the class. + /// + /// The list of connection Ids. + /// The payload dictionary which contains binary payload of multiple protocols. + public MultiConnectionDataMessage(IReadOnlyList connectionList, + IDictionary> payloads) : base(payloads) + { + ConnectionList = connectionList; + } + + /// + /// Gets or sets the list of connections which will receive this message. + /// + public IReadOnlyList ConnectionList { get; set; } + } + + /// + /// A data message which will be sent to a user. + /// + public class UserDataMessage : MulticastDataMessage + { + /// + /// Gets or sets the user Id. + /// + public string UserId { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The user Id. + /// The payload dictionary which contains binary payload of multiple protocols. + public UserDataMessage(string userId, IDictionary> payloads) : base(payloads) + { + UserId = userId; + } + } + + /// + /// A data message which will be sent to multiple users. + /// + public class MultiUserDataMessage : MulticastDataMessage + { + /// + /// Initializes a new instance of the class. + /// + /// The list of user Ids. + /// The payload dictionary which contains binary payload of multiple protocols. + public MultiUserDataMessage(IReadOnlyList userList, IDictionary> payloads) : base(payloads) + { + UserList = userList; + } + + /// + /// Gets or sets the list of user Ids. + /// + public IReadOnlyList UserList { get; set; } + } + + /// + /// A data message which will be broadcasted. + /// + public class BroadcastDataMessage : MulticastDataMessage + { + /// + /// Gets or sets the list of excluded connection Ids. + /// + public IReadOnlyList ExcludedList { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The payload dictionary which contains binary payload of multiple protocols. + public BroadcastDataMessage(IDictionary> payloads) : this(null, payloads) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The list of excluded connection Ids. + /// The payload dictionary which contains binary payload of multiple protocols. + public BroadcastDataMessage(IReadOnlyList excludedList, IDictionary> payloads) : base(payloads) + { + ExcludedList = excludedList; + } + } + + /// + /// A data message which will be broadcasted within a group. + /// + public class GroupBroadcastDataMessage : MulticastDataMessage + { + /// + /// Gets or sets the group name. + /// + public string GroupName { get; set; } + + /// + /// Gets or sets the list of excluded connection Ids. + /// + public IReadOnlyList ExcludedList { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The group name. + /// The payload dictionary which contains binary payload of multiple protocols. + public GroupBroadcastDataMessage(string groupName, IDictionary> payloads) + : this(groupName, null, payloads) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The group name. + /// The list of excluded connection Ids. + /// The payload dictionary which contains binary payload of multiple protocols. + public GroupBroadcastDataMessage(string groupName, IReadOnlyList excludedList, IDictionary> payloads) + : base(payloads) + { + GroupName = groupName; + ExcludedList = excludedList; + } + } + + /// + /// A data message which will be broadcasted within multiple groups. + /// + public class MultiGroupBroadcastDataMessage : MulticastDataMessage + { + /// + /// Gets or sets the list of group names. + /// + public IReadOnlyList GroupList { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// The list of group names. + /// The payload dictionary which contains binary payload of multiple protocols. + public MultiGroupBroadcastDataMessage(IReadOnlyList groupList, IDictionary> payloads) : base(payloads) + { + GroupList = groupList; + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs b/src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs new file mode 100644 index 000000000..cf9dd7ef3 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/ServiceMessage.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// Base class of messages between Azure SignalR Service and SDK. + /// + public abstract class ServiceMessage + { + } + + /// + /// A handshake request message. + /// + public class HandshakeRequestMessage : ServiceMessage + { + /// + /// Gets or sets the requested protocol version. + /// + public int Version { get; set; } + + /// + /// Initializes a new instance of the class. + /// + /// + public HandshakeRequestMessage(int version) + { + Version = version; + } + } + + /// + /// A handshake response message. + /// + public class HandshakeResponseMessage : ServiceMessage + { + /// + /// Gets or sets the optional error message. + /// + public string ErrorMessage { get; set; } + + /// + /// Initializes a new instance of the class. + /// + public HandshakeResponseMessage() : this(string.Empty) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// An optional response error message. A null or empty error message indicates a succesful handshake. + public HandshakeResponseMessage(string errorMessage) + { + ErrorMessage = errorMessage; + } + } + + /// + /// A ping message. + /// + public class PingMessage : ServiceMessage + { + /// + /// A static ping message. + /// + public static PingMessage Instance = new PingMessage(); + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs b/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs new file mode 100644 index 000000000..3de3879a1 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocol.cs @@ -0,0 +1,695 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Security.Claims; +using MessagePack; +using Microsoft.Extensions.Primitives; + +namespace Microsoft.Azure.SignalR.Protocol +{ + /// + /// Implements the Azure SignalR Service Protocol. + /// + public class ServiceProtocol : IServiceProtocol + { + private static readonly int ProtocolVersion = 1; + + /// + public int Version => ProtocolVersion; + + /// + public bool TryParseMessage(ref ReadOnlySequence input, out ServiceMessage message) + { + if (!BinaryMessageParser.TryParseMessage(ref input, out var payload)) + { + message = null; + return false; + } + + var arraySegment = GetArraySegment(payload); + + message = ParseMessage(arraySegment.Array, arraySegment.Offset); + return true; + } + + private static ArraySegment GetArraySegment(in ReadOnlySequence input) + { + if (input.IsSingleSegment) + { + var isArray = MemoryMarshal.TryGetArray(input.First, out var arraySegment); + // This will never be false unless we started using un-managed buffers + Debug.Assert(isArray); + return arraySegment; + } + + // Should be rare + return new ArraySegment(input.ToArray()); + } + + private static ServiceMessage ParseMessage(byte[] input, int startOffset) + { + var arrayLength = MessagePackBinary.ReadArrayHeader(input, startOffset, out var readSize); + startOffset += readSize; + + var messageType = ReadInt32(input, ref startOffset, "messageType"); + + switch (messageType) + { + case ServiceProtocolConstants.HandshakeRequestType: + return CreateHandshakeRequestMessage(input, ref startOffset); + case ServiceProtocolConstants.HandshakeResponseType: + return CreateHandshakeResponseMessage(input, ref startOffset); + case ServiceProtocolConstants.PingMessageType: + return PingMessage.Instance; + case ServiceProtocolConstants.OpenConnectionMessageType: + return CreateOpenConnectionMessage(arrayLength, input, ref startOffset); + case ServiceProtocolConstants.CloseConnectionMessageType: + return CreateCloseConnectionMessage(input, ref startOffset); + case ServiceProtocolConstants.ConnectionDataMessageType: + return CreateConnectionDataMessage(input, ref startOffset); + case ServiceProtocolConstants.MultiConnectionDataMessageType: + return CreateMultiConnectionDataMessage(input, ref startOffset); + case ServiceProtocolConstants.UserDataMessageType: + return CreateUserDataMessage(input, ref startOffset); + case ServiceProtocolConstants.MultiUserDataMessageType: + return CreateMultiUserDataMessage(input, ref startOffset); + case ServiceProtocolConstants.BroadcastDataMessageType: + return CreateBroadcastDataMessage(input, ref startOffset); + case ServiceProtocolConstants.JoinGroupMessageType: + return CreateJoinGroupMessage(input, ref startOffset); + case ServiceProtocolConstants.LeaveGroupMessageType: + return CreateLeaveGroupMessage(input, ref startOffset); + case ServiceProtocolConstants.GroupBroadcastDataMessageType: + return CreateGroupBroadcastDataMessage(input, ref startOffset); + case ServiceProtocolConstants.MultiGroupBroadcastDataMessageType: + return CreateMultiGroupBroadcastDataMessage(input, ref startOffset); + default: + // Future protocol changes can add message types, old clients can ignore them + return null; + } + } + + /// + public void WriteMessage(ServiceMessage message, IBufferWriter output) + { + var writer = MemoryBufferWriter.Get(); + + try + { + // Write message to a buffer so we can get its length + WriteMessageCore(message, writer); + + // Write length then message to output + BinaryMessageFormatter.WriteLengthPrefix(writer.Length, output); + writer.CopyTo(output); + } + finally + { + MemoryBufferWriter.Return(writer); + } + } + + /// + public ReadOnlyMemory GetMessageBytes(ServiceMessage message) + { + var writer = MemoryBufferWriter.Get(); + + try + { + // Write message to a buffer so we can get its length + WriteMessageCore(message, writer); + + var dataLength = writer.Length; + var prefixLength = BinaryMessageFormatter.LengthPrefixLength(writer.Length); + + var array = new byte[dataLength + prefixLength]; + var span = array.AsSpan(); + + // Write length then message to output + var written = BinaryMessageFormatter.WriteLengthPrefix(writer.Length, span); + Debug.Assert(written == prefixLength); + writer.CopyTo(span.Slice(prefixLength)); + + return array; + } + finally + { + MemoryBufferWriter.Return(writer); + } + } + + private static void WriteMessageCore(ServiceMessage message, Stream packer) + { + switch (message) + { + case HandshakeRequestMessage handshakeRequestMessage: + WriteHandshakeRequestMessage(handshakeRequestMessage, packer); + break; + case HandshakeResponseMessage handshakeResponseMessage: + WriteHandshakeResponseMessage(handshakeResponseMessage, packer); + break; + case PingMessage pingMessage: + WritePingMessage(pingMessage, packer); + break; + case OpenConnectionMessage openConnectionMessage: + WriteOpenConnectionMessage(openConnectionMessage, packer); + break; + case CloseConnectionMessage closeConnectionMessage: + WriteCloseConnectionMessage(closeConnectionMessage, packer); + break; + case ConnectionDataMessage connectionDataMessage: + WriteConnectionDataMessage(connectionDataMessage, packer); + break; + case MultiConnectionDataMessage multiConnectionDataMessage: + WriteMultiConnectionDataMessage(multiConnectionDataMessage, packer); + break; + case UserDataMessage userDataMessage: + WriteUserDataMessage(userDataMessage, packer); + break; + case MultiUserDataMessage multiUserDataMessage: + WriteMultiUserDataMessage(multiUserDataMessage, packer); + break; + case BroadcastDataMessage broadcastDataMessage: + WriteBroadcastDataMessage(broadcastDataMessage, packer); + break; + case JoinGroupMessage joinGroupMessage: + WriteJoinGroupMessage(joinGroupMessage, packer); + break; + case LeaveGroupMessage leaveGroupMessage: + WriteLeaveGroupMessage(leaveGroupMessage, packer); + break; + case GroupBroadcastDataMessage groupBroadcastDataMessage: + WriteGroupBroadcastDataMessage(groupBroadcastDataMessage, packer); + break; + case MultiGroupBroadcastDataMessage multiGroupBroadcastDataMessage: + WriteMultiGroupBroadcastDataMessage(multiGroupBroadcastDataMessage, packer); + break; + default: + throw new InvalidDataException($"Unexpected message type: {message.GetType().Name}"); + } + } + + private static void WriteHandshakeRequestMessage(HandshakeRequestMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 2); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.HandshakeRequestType); + MessagePackBinary.WriteInt32(packer, message.Version); + } + + private static void WriteHandshakeResponseMessage(HandshakeResponseMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 2); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.HandshakeResponseType); + MessagePackBinary.WriteString(packer, message.ErrorMessage); + } + + private static void WritePingMessage(PingMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 1); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.PingMessageType); + } + + private static void WriteOpenConnectionMessage(OpenConnectionMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 5); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.OpenConnectionMessageType); + MessagePackBinary.WriteString(packer, message.ConnectionId); + + if (message.Claims?.Length > 0) + { + MessagePackBinary.WriteMapHeader(packer, message.Claims.Length); + foreach (var claim in message.Claims) + { + MessagePackBinary.WriteString(packer, claim.Type); + MessagePackBinary.WriteString(packer, claim.Value); + } + } + else + { + MessagePackBinary.WriteMapHeader(packer, 0); + } + + WriteHeaders(message.Headers, packer); + + MessagePackBinary.WriteString(packer, message.QueryString); + } + + private static void WriteCloseConnectionMessage(CloseConnectionMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.CloseConnectionMessageType); + MessagePackBinary.WriteString(packer, message.ConnectionId); + MessagePackBinary.WriteString(packer, message.ErrorMessage); + } + + private static void WriteConnectionDataMessage(ConnectionDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.ConnectionDataMessageType); + MessagePackBinary.WriteString(packer, message.ConnectionId); + WriteBinary(packer, message.Payload); + } + + private static void WriteBinary(Stream packer, ReadOnlySequence payload) + { + // We're manually writing the message pack binary payload to the stream directly + // because MessagePack-Csharp doesn't support writing the binary header outside of + // calling WriteBytes directly + var count = (int)payload.Length; + if (count <= byte.MaxValue) + { + packer.WriteByte(MessagePackCode.Bin8); + packer.WriteByte((byte)count); + } + else if (count <= UInt16.MaxValue) + { + packer.WriteByte(MessagePackCode.Bin16); + packer.WriteByte((byte)(count >> 8)); + packer.WriteByte((byte)count); + } + else + { + packer.WriteByte(MessagePackCode.Bin32); + packer.WriteByte((byte)(count >> 24)); + packer.WriteByte((byte)(count >> 16)); + packer.WriteByte((byte)(count >> 8)); + packer.WriteByte((byte)count); + } + + // Now writes the raw bytes to the stream directly + var position = payload.Start; + while (payload.TryGet(ref position, out var memory)) + { + bool isArray = MemoryMarshal.TryGetArray(memory, out var segment); + Debug.Assert(isArray, "We're not using managed memory"); + packer.Write(segment.Array, segment.Offset, segment.Count); + } + } + + private static void WriteMultiConnectionDataMessage(MultiConnectionDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.MultiConnectionDataMessageType); + WriteStringArray(message.ConnectionList, packer); + WritePayloads(message.Payloads, packer); + } + + private static void WriteUserDataMessage(UserDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.UserDataMessageType); + MessagePackBinary.WriteString(packer, message.UserId); + WritePayloads(message.Payloads, packer); + } + + private static void WriteMultiUserDataMessage(MultiUserDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.MultiUserDataMessageType); + WriteStringArray(message.UserList, packer); + WritePayloads(message.Payloads, packer); + } + + private static void WriteBroadcastDataMessage(BroadcastDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.BroadcastDataMessageType); + WriteStringArray(message.ExcludedList, packer); + WritePayloads(message.Payloads, packer); + } + + private static void WriteJoinGroupMessage(JoinGroupMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.JoinGroupMessageType); + MessagePackBinary.WriteString(packer, message.ConnectionId); + MessagePackBinary.WriteString(packer, message.GroupName); + } + + private static void WriteLeaveGroupMessage(LeaveGroupMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.LeaveGroupMessageType); + MessagePackBinary.WriteString(packer, message.ConnectionId); + MessagePackBinary.WriteString(packer, message.GroupName); + } + + private static void WriteGroupBroadcastDataMessage(GroupBroadcastDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 4); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.GroupBroadcastDataMessageType); + MessagePackBinary.WriteString(packer, message.GroupName); + WriteStringArray(message.ExcludedList, packer); + WritePayloads(message.Payloads, packer); + } + + private static void WriteMultiGroupBroadcastDataMessage(MultiGroupBroadcastDataMessage message, Stream packer) + { + MessagePackBinary.WriteArrayHeader(packer, 3); + MessagePackBinary.WriteInt32(packer, ServiceProtocolConstants.MultiGroupBroadcastDataMessageType); + WriteStringArray(message.GroupList, packer); + WritePayloads(message.Payloads, packer); + } + + private static void WriteStringArray(IReadOnlyList array, Stream packer) + { + if (array?.Count > 0) + { + MessagePackBinary.WriteArrayHeader(packer, array.Count); + foreach (var value in array) + { + MessagePackBinary.WriteString(packer, value); + } + } + else + { + MessagePackBinary.WriteArrayHeader(packer, 0); + } + } + + private static void WritePayloads(IDictionary> payloads, Stream packer) + { + if (payloads?.Count > 0) + { + MessagePackBinary.WriteMapHeader(packer, payloads.Count); + foreach (var payload in payloads) + { + MessagePackBinary.WriteString(packer, payload.Key); + bool isArray = MemoryMarshal.TryGetArray(payload.Value, out var segment); + Debug.Assert(isArray, "We're not using managed memory"); + MessagePackBinary.WriteBytes(packer, segment.Array, segment.Offset, segment.Count); + } + } + else + { + MessagePackBinary.WriteMapHeader(packer, 0); + } + } + + private static void WriteHeaders(IDictionary headers, Stream packer) + { + if (headers?.Count > 0) + { + MessagePackBinary.WriteMapHeader(packer, headers.Count); + foreach (var header in headers) + { + MessagePackBinary.WriteString(packer, header.Key); + MessagePackBinary.WriteArrayHeader(packer, header.Value.Count); + foreach (var stringValue in header.Value) + { + MessagePackBinary.WriteString(packer, stringValue); + } + } + } + else + { + MessagePackBinary.WriteMapHeader(packer, 0); + } + } + + private static HandshakeRequestMessage CreateHandshakeRequestMessage(byte[] input, ref int offset) + { + var version = ReadInt32(input, ref offset, "version"); + + return new HandshakeRequestMessage(version); + } + + private static HandshakeResponseMessage CreateHandshakeResponseMessage(byte[] input, ref int offset) + { + var errorMessage = ReadString(input, ref offset, "errorMessage"); + + return new HandshakeResponseMessage(errorMessage); + } + + private static OpenConnectionMessage CreateOpenConnectionMessage(int arrayLength, byte[] input, ref int offset) + { + var connectionId = ReadString(input, ref offset, "connectionId"); + var claims = ReadClaims(input, ref offset); + + // Backward compatible with old versions + if (arrayLength > 3) + { + var headers = ReadHeaders(input, ref offset); + var queryString = ReadString(input, ref offset, "queryString"); + + return new OpenConnectionMessage(connectionId, claims, headers, queryString); + } + else + { + return new OpenConnectionMessage(connectionId, claims); + } + } + + private static CloseConnectionMessage CreateCloseConnectionMessage(byte[] input, ref int offset) + { + var connectionId = ReadString(input, ref offset, "connectionId"); + var errorMessage = ReadString(input, ref offset, "errorMessage"); + + return new CloseConnectionMessage(connectionId, errorMessage); + } + + private static ConnectionDataMessage CreateConnectionDataMessage(byte[] input, ref int offset) + { + var connectionId = ReadString(input, ref offset, "connectionId"); + var payload = ReadBytes(input, ref offset, "payload"); + + return new ConnectionDataMessage(connectionId, payload); + } + + private static MultiConnectionDataMessage CreateMultiConnectionDataMessage(byte[] input, ref int offset) + { + var connectionList = ReadStringArray(input, ref offset, "connectionList"); + var payloads = ReadPayloads(input, ref offset); + + return new MultiConnectionDataMessage(connectionList, payloads); + } + + private static ServiceMessage CreateUserDataMessage(byte[] input, ref int offset) + { + var userId = ReadString(input, ref offset, "userId"); + var payloads = ReadPayloads(input, ref offset); + + return new UserDataMessage(userId, payloads); + } + + private static MultiUserDataMessage CreateMultiUserDataMessage(byte[] input, ref int offset) + { + var userList = ReadStringArray(input, ref offset, "userList"); + var payloads = ReadPayloads(input, ref offset); + + return new MultiUserDataMessage(userList, payloads); + } + + private static BroadcastDataMessage CreateBroadcastDataMessage(byte[] input, ref int offset) + { + var excludedList = ReadStringArray(input, ref offset, "excludedList"); + var payloads = ReadPayloads(input, ref offset); + + return new BroadcastDataMessage(excludedList, payloads); + } + + private static JoinGroupMessage CreateJoinGroupMessage(byte[] input, ref int offset) + { + var connectionId = ReadString(input, ref offset, "connectionId"); + var groupName = ReadString(input, ref offset, "groupName"); + + return new JoinGroupMessage(connectionId, groupName); + } + + private static LeaveGroupMessage CreateLeaveGroupMessage(byte[] input, ref int offset) + { + var connectionId = ReadString(input, ref offset, "connectionId"); + var groupName = ReadString(input, ref offset, "groupName"); + + return new LeaveGroupMessage(connectionId, groupName); + } + + private static GroupBroadcastDataMessage CreateGroupBroadcastDataMessage(byte[] input, ref int offset) + { + var groupName = ReadString(input, ref offset, "groupName"); + var excludedList = ReadStringArray(input, ref offset, "excludedList"); + var payloads = ReadPayloads(input, ref offset); + + return new GroupBroadcastDataMessage(groupName, excludedList, payloads); + } + + private static MultiGroupBroadcastDataMessage CreateMultiGroupBroadcastDataMessage(byte[] input, ref int offset) + { + var groupList = ReadStringArray(input, ref offset, "groupList"); + var payloads = ReadPayloads(input, ref offset); + + return new MultiGroupBroadcastDataMessage(groupList, payloads); + } + + private static Claim[] ReadClaims(byte[] input, ref int offset) + { + var claimCount = ReadMapLength(input, ref offset, "claims"); + if (claimCount > 0) + { + var claims = new Claim[claimCount]; + + for (var i = 0; i < claimCount; i++) + { + var type = ReadString(input, ref offset, $"claims[{i}].Type"); + var value = ReadString(input, ref offset, $"claims[{i}].Value"); + claims[i] = new Claim(type, value); + } + + return claims; + } + + return null; + } + + private static IDictionary> ReadPayloads(byte[] input, ref int offset) + { + var payloadCount = ReadMapLength(input, ref offset, "payloads"); + if (payloadCount > 0) + { + var payloads = new Dictionary>((int)payloadCount); + for (var i = 0; i < payloadCount; i++) + { + var key = ReadString(input, ref offset, $"payloads[{i}].key"); + var value = ReadBytes(input, ref offset, $"payloads[{i}].value"); + payloads.Add(key, value); + } + + return payloads; + } + + return null; + } + + private static Dictionary ReadHeaders(byte[] input, ref int offset) + { + var headerCount = ReadMapLength(input, ref offset, "headers"); + if (headerCount > 0) + { + var headers = new Dictionary((int)headerCount, StringComparer.OrdinalIgnoreCase); + for (var i = 0; i < headerCount; i++) + { + var key = ReadString(input, ref offset, $"headers[{i}].key"); + var count = ReadArrayLength(input, ref offset, $"headers[{i}].value.length"); + var stringValues = new string[count]; + for (var j = 0; j < count; j++) + { + stringValues[j] = ReadString(input, ref offset, $"headers[{i}].value[{j}]"); + } + headers.Add(key, stringValues); + } + + return headers; + } + + return new Dictionary(StringComparer.OrdinalIgnoreCase); + } + + private static int ReadInt32(byte[] input, ref int offset, string field) + { + Exception msgPackException = null; + try + { + var readInt = MessagePackBinary.ReadInt32(input, offset, out var readSize); + offset += readSize; + return readInt; + } + catch (Exception e) + { + msgPackException = e; + } + + throw new InvalidDataException($"Reading '{field}' as Int32 failed.", msgPackException); + } + + private static string ReadString(byte[] input, ref int offset, string field) + { + Exception msgPackException = null; + try + { + var readString = MessagePackBinary.ReadString(input, offset, out var readSize); + offset += readSize; + return readString; + } + catch (Exception e) + { + msgPackException = e; + } + + throw new InvalidDataException($"Reading '{field}' as String failed.", msgPackException); + } + + private static string[] ReadStringArray(byte[] input, ref int offset, string field) + { + var arrayLength = ReadArrayLength(input, ref offset, field); + if (arrayLength > 0) + { + var array = new string[arrayLength]; + for (int i = 0; i < arrayLength; i++) + { + array[i] = ReadString(input, ref offset, $"{field}[{i}]"); + } + + return array; + } + + return null; + } + + private static byte[] ReadBytes(byte[] input, ref int offset, string field) + { + Exception msgPackException = null; + try + { + var readBytes = MessagePackBinary.ReadBytes(input, offset, out var readSize); + offset += readSize; + return readBytes; + } + catch (Exception e) + { + msgPackException = e; + } + + throw new InvalidDataException($"Reading '{field}' as Byte[] failed.", msgPackException); + } + + private static long ReadMapLength(byte[] input, ref int offset, string field) + { + Exception msgPackException = null; + try + { + var readMap = MessagePackBinary.ReadMapHeader(input, offset, out var readSize); + offset += readSize; + return readMap; + } + catch (Exception e) + { + msgPackException = e; + } + + throw new InvalidDataException($"Reading map length for '{field}' failed.", msgPackException); + } + + private static long ReadArrayLength(byte[] input, ref int offset, string field) + { + Exception msgPackException = null; + try + { + var readArray = MessagePackBinary.ReadArrayHeader(input, offset, out var readSize); + offset += readSize; + return readArray; + } + catch (Exception e) + { + msgPackException = e; + } + + throw new InvalidDataException($"Reading array length for '{field}' failed.", msgPackException); + } + } +} diff --git a/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocolConstants.cs b/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocolConstants.cs new file mode 100644 index 000000000..66cae77c9 --- /dev/null +++ b/src/Microsoft.Azure.SignalR.Protocols/ServiceProtocolConstants.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR.Protocol +{ + public static class ServiceProtocolConstants + { + public const int HandshakeRequestType = 1; + public const int HandshakeResponseType = 2; + public const int PingMessageType = 3; + public const int OpenConnectionMessageType = 4; + public const int CloseConnectionMessageType = 5; + public const int ConnectionDataMessageType = 6; + public const int MultiConnectionDataMessageType = 7; + public const int UserDataMessageType = 8; + public const int MultiUserDataMessageType = 9; + public const int BroadcastDataMessageType = 10; + public const int JoinGroupMessageType = 11; + public const int LeaveGroupMessageType = 12; + public const int GroupBroadcastDataMessageType = 13; + public const int MultiGroupBroadcastDataMessageType = 14; + } +} diff --git a/src/Microsoft.Azure.SignalR/ApplicationBuilderExtensions.cs b/src/Microsoft.Azure.SignalR/ApplicationBuilderExtensions.cs index 7db54b428..abb3bf5d2 100644 --- a/src/Microsoft.Azure.SignalR/ApplicationBuilderExtensions.cs +++ b/src/Microsoft.Azure.SignalR/ApplicationBuilderExtensions.cs @@ -2,23 +2,36 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using Microsoft.AspNetCore.Routing; using Microsoft.Azure.SignalR; +using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Builder { + /// + /// Extension methods for . + /// public static class AzureSignalRApplicationBuilderExtensions { - public static IApplicationBuilder UseAzureSignalR(this IApplicationBuilder app, - string connectionString, Action configure) + /// + /// Adds Azure SignalR Service to the request execution pipeline. + /// + /// The . + /// A callback to configure the . + /// The same instance of the for chaining. + public static IApplicationBuilder UseAzureSignalR(this IApplicationBuilder app, Action configure) { - // Assign only once - CloudSignalR.ServiceProvider = app.ApplicationServices; - - var builder = new HubHostBuilder(app.ApplicationServices, - CloudSignalR.CreateEndpointProviderFromConnectionString(connectionString), - CloudSignalR.CreateTokenProviderFromConnectionString(connectionString)); - configure(builder); - + var marker = app.ApplicationServices.GetService(); + if (marker == null) + { + throw new InvalidOperationException( + "Unable to find the required services. Please add all the required services by calling " + + "'IServiceCollection.AddAzureSignalR' inside the call to 'ConfigureServices(...)' in the application startup code."); + } + marker.IsConfigured = true; + var routes = new RouteBuilder(app); + configure(new ServiceRouteBuilder(routes)); + app.UseRouter(routes.Build()); return app; } } diff --git a/src/Microsoft.Azure.SignalR/CloudSignalR.cs b/src/Microsoft.Azure.SignalR/CloudSignalR.cs deleted file mode 100644 index 489dfa1de..000000000 --- a/src/Microsoft.Azure.SignalR/CloudSignalR.cs +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Linq; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; - -namespace Microsoft.Azure.SignalR -{ - public class CloudSignalR - { - public class ConnectionString - { - public string Endpoint { get; set; } - - public string AccessKey { get; set; } - } - - private const string EndpointProperty = "endpoint"; - private const string AccessKeyProperty = "accesskey"; - - public static ConnectionString ParseConnectionString(string connectionString) - { - if (!string.IsNullOrEmpty(connectionString)) - { - var dict = connectionString.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries) - .Select(x => x.Split(new[] {'='}, 2)) - .ToDictionary(t => t[0].Trim().ToLower(), t => t[1].Trim(), - StringComparer.OrdinalIgnoreCase); - if (dict.ContainsKey(EndpointProperty) && dict.ContainsKey(AccessKeyProperty)) - { - return new ConnectionString - { - Endpoint = dict[EndpointProperty], - AccessKey = dict[AccessKeyProperty] - }; - } - } - - throw new ArgumentException($"Invalid Azure SignalR connection string: {connectionString}"); - } - - public static EndpointProvider CreateEndpointProviderFromConnectionString(string connectionString) - { - var connStr = ParseConnectionString(connectionString); - return new EndpointProvider(connStr.Endpoint); - } - - public static TokenProvider CreateTokenProviderFromConnectionString(string connectionString) - { - var connStr = ParseConnectionString(connectionString); - return new TokenProvider(connStr.Endpoint, connStr.AccessKey); - } - - public static HubProxy CreateHubProxyFromConnectionString(string connectionString) where THub : Hub - { - return CreateHubProxyFromConnectionString(connectionString, null); - } - - public static HubProxy CreateHubProxyFromConnectionString(string connectionString, HubProxyOptions options) - where THub : Hub - { - return CreateHubProxyFromConnectionString(connectionString, typeof(THub).Name, options); - } - - public static HubProxy CreateHubProxyFromConnectionString(string connectionString, string hubName) - { - return CreateHubProxyFromConnectionString(connectionString, hubName, null); - } - - public static HubProxy CreateHubProxyFromConnectionString(string connectionString, string hubName, HubProxyOptions options) - { - var connStr = ParseConnectionString(connectionString); - return new HubProxy(connStr.Endpoint, connStr.AccessKey, hubName); - } - - public static void ConfigureAuthorization(Action configure) - { - if (configure != null) _authorizationConfigure = configure; - } - - private static Action _authorizationConfigure = _ => { }; - - private static IServiceProvider _externalServiceProvider = null; - - private static readonly Lazy _internalServiceProvider = - new Lazy( - () => - { - var serviceCollection = new ServiceCollection(); - - serviceCollection.AddLogging(); - serviceCollection.AddAuthorization(_authorizationConfigure); - serviceCollection.AddAzureSignalR(); - - return serviceCollection.BuildServiceProvider(); - }); - - public static IServiceProvider ServiceProvider - { - get => _externalServiceProvider ?? _internalServiceProvider.Value; - internal set => _externalServiceProvider = value; - } - } -} diff --git a/src/Microsoft.Azure.SignalR/DependencyInjectionExtensions.cs b/src/Microsoft.Azure.SignalR/DependencyInjectionExtensions.cs index fba34316e..3f4d65737 100644 --- a/src/Microsoft.Azure.SignalR/DependencyInjectionExtensions.cs +++ b/src/Microsoft.Azure.SignalR/DependencyInjectionExtensions.cs @@ -3,34 +3,71 @@ using System; using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Core; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; using Microsoft.Azure.SignalR; -using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; namespace Microsoft.Extensions.DependencyInjection { + /// + /// Extension methods for . + /// public static class AzureSignalRDependencyInjectionExtensions { - public static IServiceCollection AddAzureSignalR(this IServiceCollection services, - Action configure = null) + /// + /// Adds the minimum essential Azure SignalR services to the specified . + /// + /// The . + /// The same instance of the for chaining. + public static ISignalRServerBuilder AddAzureSignalR(this ISignalRServerBuilder builder) { - if (configure != null) services.Configure(configure); - - services.AddSingleton(typeof(HubLifetimeManager<>), typeof(HubHostLifetimeManager<>)); - services.AddSingleton(typeof(IHubProtocolResolver), typeof(DefaultHubProtocolResolver)); - services.TryAddEnumerable(ServiceDescriptor.Singleton()); - services.AddSingleton(typeof(IHubContext<>), typeof(HubContext<>)); - services.AddSingleton(typeof(IUserIdProvider), typeof(DefaultUserIdProvider)); - services.AddScoped(typeof(IHubActivator<>), typeof(DefaultHubActivator<>)); + builder.Services.AddSingleton, ServiceOptionsSetup>(); + return builder.AddAzureSignalRCore(); + } - services.AddSingleton(typeof(HubDispatcher<>), typeof(HubHostDispatcher<>)); - services.AddSingleton(typeof(HubHost<>)); + /// + /// Adds the minimum essential Azure SignalR services to the specified . + /// + /// The . + /// The connection string of an Azure SignalR Service instance. + /// The same instance of the for chaining. + public static ISignalRServerBuilder AddAzureSignalR(this ISignalRServerBuilder builder, string connectionString) + { + return builder.AddAzureSignalR(options => + { + options.ConnectionString = connectionString; + }); + } - services.AddAuthorization(); + /// + /// Adds the minimum essential Azure SignalR services to the specified . + /// + /// The . + /// A callback to configure the . + /// The same instance of the for chaining. + public static ISignalRServerBuilder AddAzureSignalR(this ISignalRServerBuilder builder, Action configure) + { + builder.AddAzureSignalR() + .Services.Configure(configure); - return services; + return builder; + } + + private static ISignalRServerBuilder AddAzureSignalRCore(this ISignalRServerBuilder builder) + { + builder.Services + .AddSingleton(typeof(HubLifetimeManager<>), typeof(ServiceLifetimeManager<>)) + .AddSingleton(typeof(IServiceProtocol), typeof(ServiceProtocol)) + .AddSingleton(typeof(IClientConnectionManager), typeof(ClientConnectionManager)) + .AddSingleton(typeof(IServiceConnectionManager<>), typeof(ServiceConnectionManager<>)) + .AddSingleton(typeof(IServiceEndpointProvider), typeof(ServiceEndpointProvider)) + .AddSingleton(typeof(ServiceHubDispatcher<>)) + .AddSingleton(typeof(AzureSignalRMarkerService)) + .AddSingleton() + .AddSingleton() + .AddSingleton(); + return builder; } } } diff --git a/src/Microsoft.Azure.SignalR/EndpointProvider.cs b/src/Microsoft.Azure.SignalR/EndpointProvider.cs deleted file mode 100644 index d86e6c481..000000000 --- a/src/Microsoft.Azure.SignalR/EndpointProvider.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Microsoft.AspNetCore.SignalR; - -namespace Microsoft.Azure.SignalR -{ - public class EndpointProvider - { - private const int ClientPort = 5001; - private const int ServerPort = 5002; - - private readonly string _endpoint; - - public EndpointProvider(string endpoint) - { - if (string.IsNullOrEmpty(endpoint)) - { - throw new ArgumentNullException(nameof(endpoint)); - } - _endpoint = endpoint.TrimEnd('/'); - } - - public string GetClientEndpoint() where THub : Hub - { - return GetClientEndpoint(typeof(THub).Name); - } - - public string GetClientEndpoint(string hubName) - { - if (string.IsNullOrEmpty(hubName)) - { - throw new ArgumentNullException(nameof(hubName)); - } - - return InternalGetEndpoint(ClientPort, "client", hubName); - } - - public string GetServerEndpoint() where THub : Hub - { - return GetServerEndpoint(typeof(THub).Name); - } - - public string GetServerEndpoint(string hubName) - { - if (string.IsNullOrEmpty(hubName)) - { - throw new ArgumentNullException(nameof(hubName)); - } - - return InternalGetEndpoint(ServerPort, "server", hubName); - } - - private string InternalGetEndpoint(int port, string path, string hubName) - { - return $"{_endpoint}:{port}/{path}/?hub={hubName.ToLower()}"; - } - } -} diff --git a/src/Microsoft.Azure.SignalR/EndpointProvider/DefaultServiceEndpointGenerator.cs b/src/Microsoft.Azure.SignalR/EndpointProvider/DefaultServiceEndpointGenerator.cs new file mode 100644 index 000000000..aba1a7b2f --- /dev/null +++ b/src/Microsoft.Azure.SignalR/EndpointProvider/DefaultServiceEndpointGenerator.cs @@ -0,0 +1,66 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Net; +using System.Text; + +namespace Microsoft.Azure.SignalR +{ + internal sealed class DefaultServiceEndpointGenerator : IServiceEndpointGenerator + { + private const string ClientPath = "client"; + private const string ServerPath = "server"; + + public string Endpoint { get; } + + public string AccessKey { get; } + + public string Version { get; } + + public int? Port { get; } + + public DefaultServiceEndpointGenerator(string endpoint, string accessKey, string version, int? port) + { + Endpoint = endpoint; + AccessKey = accessKey; + Version = version; + Port = port; + } + + public string GetClientAudience(string hubName) => + InternalGetAudience(ClientPath, hubName); + + public string GetClientEndpoint(string hubName, string originalPath, string queryString) + { + var queryBuilder = new StringBuilder(); + if (!string.IsNullOrEmpty(originalPath)) + { + queryBuilder.Append("&") + .Append(Constants.QueryParameter.OriginalPath) + .Append("=") + .Append(WebUtility.UrlEncode(originalPath)); + } + + if (!string.IsNullOrEmpty(queryString)) + { + queryBuilder.Append("&").Append(queryString); + } + + return $"{InternalGetEndpoint(ClientPath, hubName)}{queryBuilder}"; + } + + public string GetServerAudience(string hubName) => + InternalGetAudience(ServerPath, hubName); + + public string GetServerEndpoint(string hubName) => + InternalGetEndpoint(ServerPath, hubName); + + private string InternalGetEndpoint(string path, string hubName) => + Port.HasValue ? + $"{Endpoint}:{Port}/{path}/?hub={hubName.ToLower()}" : + $"{Endpoint}/{path}/?hub={hubName.ToLower()}"; + + private string InternalGetAudience(string path, string hubName) => + $"{Endpoint}/{path}/?hub={hubName.ToLower()}"; + } +} diff --git a/src/Microsoft.Azure.SignalR/EndpointProvider/IServiceEndpointProvider.cs b/src/Microsoft.Azure.SignalR/EndpointProvider/IServiceEndpointProvider.cs new file mode 100644 index 000000000..a773b9a4e --- /dev/null +++ b/src/Microsoft.Azure.SignalR/EndpointProvider/IServiceEndpointProvider.cs @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.AspNetCore.SignalR; + +namespace Microsoft.Azure.SignalR +{ + internal interface IServiceEndpointProvider + { + string GenerateClientAccessToken(string hubName, IEnumerable claims = null, TimeSpan? lifetime = null); + + string GetClientEndpoint(string hubName, string originalPath, string queryString); + + string GenerateServerAccessToken(string userId, TimeSpan? lifetime = null) where THub : Hub; + + string GetServerEndpoint() where THub : Hub; + } +} diff --git a/src/Microsoft.Azure.SignalR/EndpointProvider/PreviewServiceEndpointGenerator.cs b/src/Microsoft.Azure.SignalR/EndpointProvider/PreviewServiceEndpointGenerator.cs new file mode 100644 index 000000000..c19cad98a --- /dev/null +++ b/src/Microsoft.Azure.SignalR/EndpointProvider/PreviewServiceEndpointGenerator.cs @@ -0,0 +1,58 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Net; +using System.Text; + +namespace Microsoft.Azure.SignalR +{ + internal sealed class PreviewServiceEndpointGenerator : IServiceEndpointGenerator + { + private const int ClientPort = 5001; + private const int ServerPort = 5002; + private const string ClientPath = "client"; + private const string ServerPath = "server"; + + public string Endpoint { get; } + + public string AccessKey { get; } + + public PreviewServiceEndpointGenerator(string endpoint, string accessKey) + { + Endpoint = endpoint; + AccessKey = accessKey; + } + + public string GetClientAudience(string hubName) => + InternalGetEndpoint(ClientPort, ClientPath, hubName); + + public string GetClientEndpoint(string hubName, string originalPath, string queryString) + { + var queryBuilder = new StringBuilder(); + if (!string.IsNullOrEmpty(originalPath)) + { + queryBuilder.Append("&") + .Append(Constants.QueryParameter.OriginalPath) + .Append("=") + .Append(WebUtility.UrlEncode(originalPath)); + } + + if (!string.IsNullOrEmpty(queryString)) + { + queryBuilder.Append("&").Append(queryString); + } + + return $"{InternalGetEndpoint(ClientPort, ClientPath, hubName)}{queryBuilder}"; + } + + + public string GetServerAudience(string hubName) => + InternalGetEndpoint(ServerPort, ServerPath, hubName); + + public string GetServerEndpoint(string hubName) => + InternalGetEndpoint(ServerPort, ServerPath, hubName); + + private string InternalGetEndpoint(int port, string path, string hubName) => + $"{Endpoint}:{port}/{path}/?hub={hubName.ToLower()}"; + } +} diff --git a/src/Microsoft.Azure.SignalR/EndpointProvider/ServiceEndpointProvider.cs b/src/Microsoft.Azure.SignalR/EndpointProvider/ServiceEndpointProvider.cs new file mode 100644 index 000000000..cf6d6d199 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/EndpointProvider/ServiceEndpointProvider.cs @@ -0,0 +1,97 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Options; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceEndpointProvider : IServiceEndpointProvider + { + private const string PreviewVersion = "1.0-preview"; + + private static readonly string ConnectionStringNotFound = + "No connection string was specified. " + + $"Please specify a configuration entry for {ServiceOptions.ConnectionStringDefaultKey}, " + + "or explicitly pass one using IServiceCollection.AddAzureSignalR(connectionString) in Startup.ConfigureServices."; + + private readonly string _endpoint; + private readonly string _accessKey; + private readonly TimeSpan _accessTokenLifetime; + private readonly IServiceEndpointGenerator _generator; + + public ServiceEndpointProvider(IOptions options) + { + var connectionString = options.Value.ConnectionString; + if (string.IsNullOrEmpty(connectionString)) + { + throw new ArgumentException(ConnectionStringNotFound); + } + + _accessTokenLifetime = options.Value.AccessTokenLifetime; + + string version; + int? port; + (_endpoint, _accessKey, version, port) = ConnectionStringParser.Parse(connectionString); + + if (version == null || version == PreviewVersion) + { + _generator = new PreviewServiceEndpointGenerator(_endpoint, _accessKey); + } + else + { + _generator = new DefaultServiceEndpointGenerator(_endpoint, _accessKey, version, port); + } + } + + public string GenerateClientAccessToken(string hubName, IEnumerable claims = null, TimeSpan? lifetime = null) + { + if (string.IsNullOrEmpty(hubName)) + { + throw new ArgumentNullException(nameof(hubName)); + } + + var audience = _generator.GetClientAudience(hubName); + + return InternalGenerateAccessToken(audience, claims, lifetime ?? _accessTokenLifetime); + } + + public string GenerateServerAccessToken(string userId, TimeSpan? lifetime = null) where THub : Hub + { + var audience = _generator.GetServerAudience(typeof(THub).Name); + var claims = userId != null ? new[] {new Claim(ClaimTypes.NameIdentifier, userId)} : null; + + return InternalGenerateAccessToken(audience, claims, lifetime ?? _accessTokenLifetime); + } + + public string GetClientEndpoint(string hubName, string originalPath, string queryString) + { + if (string.IsNullOrEmpty(hubName)) + { + throw new ArgumentNullException(nameof(hubName)); + } + + return _generator.GetClientEndpoint(hubName, originalPath, queryString); + } + + public string GetServerEndpoint() where THub : Hub + { + return _generator.GetServerEndpoint(typeof(THub).Name); + } + + private string InternalGenerateAccessToken(string audience, IEnumerable claims, TimeSpan lifetime) + { + var expire = DateTime.UtcNow.Add(lifetime); + + return AuthenticationHelper.GenerateJwtBearer( + audience: audience, + claims: claims, + expires: expire, + signingKey: _accessKey + ); + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionFactory.cs b/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionFactory.cs new file mode 100644 index 000000000..1c27b6087 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionFactory.cs @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR +{ + internal class ClientConnectionFactory : IClientConnectionFactory + { + public ServiceConnectionContext CreateConnection(OpenConnectionMessage message) + { + return new ServiceConnectionContext(message); + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionManager.cs b/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionManager.cs new file mode 100644 index 000000000..274c0b79d --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ClientConnectionManager.cs @@ -0,0 +1,27 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Concurrent; + +namespace Microsoft.Azure.SignalR +{ + internal class ClientConnectionManager : IClientConnectionManager + { + public ClientConnectionManager() + { + ClientConnections = new ConcurrentDictionary(); + } + + public void AddClientConnection(ServiceConnectionContext clientConnection) + { + ClientConnections[clientConnection.ConnectionId] = clientConnection; + } + + public void RemoveClientConnection(string connectionId) + { + ClientConnections.TryRemove(connectionId, out _); + } + + public ConcurrentDictionary ClientConnections { get; } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/CloudConnection.cs b/src/Microsoft.Azure.SignalR/HubHost/CloudConnection.cs deleted file mode 100644 index b647b9ca1..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/CloudConnection.cs +++ /dev/null @@ -1,357 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Diagnostics; -using System.IO; -using System.Security.Claims; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Encoders; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.AspNetCore.Sockets; -using Microsoft.AspNetCore.Sockets.Client; -using Microsoft.AspNetCore.Sockets.Features; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; - -namespace Microsoft.Azure.SignalR -{ - public class CloudConnection where THub : Hub - { - private const int MaxReconnectInterval = 1000; // in milliseconds - private const string OnConnectedAsyncMethod = "onconnectedasync"; - private const string OnDisconnectedAsyncMethod = "ondisconnectedasync"; - private const string ConnectCallback = "HubHostOptions.OnConnected"; - private const string DisconnectCallback = "HubHostOptions.OnDisconnected"; - - private readonly ILoggerFactory _loggerFactory; - private readonly ILogger _logger; - private readonly IConnection _connection; - private readonly HubHostOptions _options; - private readonly IHubProtocol _protocol; - private HubProtocolReaderWriter _protocolReaderWriter; - - private readonly HubLifetimeManager _lifetimeManager; - private readonly HubDispatcher _hubDispatcher; - private readonly HubConnectionList _connections = new HubConnectionList(); - - private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); - - // TODO: expose connection status to user code - private volatile bool _isConnected; - private readonly Timer _timeoutTimer; - private bool _needKeepAlive; - - private readonly Timer _reconnectTimer; - private int _reconnectIntervalInMS => StaticRandom.Next(MaxReconnectInterval); - - public CloudConnection(IConnection connection, - IHubProtocol protocol, - HubHostOptions options, - HubLifetimeManager lifetimeManager, - HubDispatcher hubDispatcher, - ILoggerFactory loggerFactory) - { - _connection = connection ?? throw new ArgumentNullException(nameof(connection)); - _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol)); - _options = options ?? throw new ArgumentNullException(nameof(options)); - _lifetimeManager = lifetimeManager ?? throw new ArgumentNullException(nameof(lifetimeManager)); - _hubDispatcher = hubDispatcher ?? throw new ArgumentNullException(nameof(hubDispatcher)); - - _loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; - _logger = _loggerFactory.CreateLogger>(); - - _timeoutTimer = new Timer(state => ((CloudConnection) state).TimeoutElapsed(), this, Timeout.Infinite, - Timeout.Infinite); - - _reconnectTimer = - new Timer(state => ((CloudConnection) state).StartAsync().GetAwaiter().GetResult(), this, - Timeout.Infinite, Timeout.Infinite); - - connection.OnReceived((data, state) => ((CloudConnection) state).OnDataReceivedAsync(data), this); - connection.Closed += OnHttpConnectionClosed; - } - - public async Task StartAsync() - { - try - { - await StartAsyncCore(); - _isConnected = true; - } - catch (Exception ex) - { - _logger.LogError($"Failed to connect to Azure SignalR due to error: {ex.Message}"); - ResetReconnectTimer(); - return; - } - - await RunUserCallbackAsync(ConnectCallback, _options.OnConnected); - } - - private async Task StartAsyncCore() - { - var transferModeFeature = GetOrAddTransferModeFeature(); - var requestedMode = transferModeFeature.TransferMode; - - await _connection.StartAsync(); - - var actualMode = transferModeFeature.TransferMode; - _protocolReaderWriter = GetProtocolReaderWriter(requestedMode, actualMode); - - await NegotiateProtocol(); - - _needKeepAlive = _connection.Features.Get() == null; - ResetTimeoutTimer(); - } - - private ITransferModeFeature GetOrAddTransferModeFeature() - { - var transferModeFeature = _connection.Features.Get(); - if (transferModeFeature == null) - { - transferModeFeature = new TransferModeFeature(); - _connection.Features.Set(transferModeFeature); - } - - transferModeFeature.TransferMode = _protocol.Type == ProtocolType.Binary - ? TransferMode.Binary - : TransferMode.Text; - - return transferModeFeature; - } - - private HubProtocolReaderWriter GetProtocolReaderWriter(TransferMode requestedMode, TransferMode actualMode) - { - return new HubProtocolReaderWriter(_protocol, GetDataEncoder(requestedMode, actualMode)); - } - - private IDataEncoder GetDataEncoder(TransferMode requestedMode, TransferMode actualMode) - { - if (requestedMode == TransferMode.Binary && actualMode == TransferMode.Text) - { - // This is for instance for SSE which is a Text protocol and the user wants to use a binary - // protocol so we need to encode messages. - return new Base64Encoder(); - } - - Debug.Assert(requestedMode == actualMode, "All transports besides SSE are expected to support binary mode."); - - return new PassThroughEncoder(); - } - - private async Task NegotiateProtocol() - { - _logger.LogDebug($"Negotiate to use hub protocol: {_protocol.Name}"); - using (var memoryStream = new MemoryStream()) - { - NegotiationProtocol.WriteMessage(new NegotiationMessage(_protocol.Name), memoryStream); - await _connection.SendAsync(memoryStream.ToArray(), CancellationToken.None); - } - } - - // TODO: Right now no one is calling this API. We should probably hide it. - public async Task StopAsync() - { - if (!_cancellationTokenSource.IsCancellationRequested) - { - _cancellationTokenSource.Cancel(); - } - - await StopAsyncCore(); - - _isConnected = false; - - await RunUserCallbackAsync(DisconnectCallback, () => _options.OnDisconnected?.Invoke(null)); - } - - private Task StopAsyncCore() => _connection.StopAsync(); - - private void TimeoutElapsed() - { - _connection.AbortAsync(new TimeoutException( - $"Server timeout ({_options.ServerTimeout.TotalMilliseconds:0.00}ms) elapsed without receiving a message from the server.")); - } - - private void ResetTimeoutTimer() - { - if (_needKeepAlive) - { - _logger.LogDebug("Resetting keep alive timer..."); - _timeoutTimer.Change(_options.ServerTimeout, Timeout.InfiniteTimeSpan); - } - } - - private void ResetReconnectTimer() - { - if (_options.AutoReconnect) - { - var interval = _reconnectIntervalInMS; - _logger.LogDebug($"Auto-reconnect is enabled. Will reconnect in {interval} ms."); - _reconnectTimer.Change(TimeSpan.FromMilliseconds(interval), Timeout.InfiniteTimeSpan); - } - else - { - _logger.LogDebug("Auto-reconnect is disabled."); - } - } - - private void OnHttpConnectionClosed(Exception ex) - { - _isConnected = false; - - // Quick return when StopAsync has been called. - if (_cancellationTokenSource.IsCancellationRequested) - { - return; - } - - if (ex != null) - { - _logger.LogError($"Connection to Azure SignalR is closed due to error: {ex.Message}"); - } - else - { - _logger.LogWarning("Connection to Azure SignalR is closed."); - } - - RunUserCallbackAsync(DisconnectCallback, () => _options.OnDisconnected?.Invoke(ex)) - .GetAwaiter().GetResult(); - - ResetReconnectTimer(); - } - - private async Task RunUserCallbackAsync(string name, Func callback) - { - try - { - if (callback != null) - { - await (callback() ?? Task.CompletedTask); - } - } - catch (Exception ex) - { - _logger.LogError($"Exception when calling {name}: {ex}"); - } - } - - private async Task OnDataReceivedAsync(byte[] data) - { - if (!_isConnected) - { - _logger.LogWarning("Message processing is disabled when disconnected."); - return; - } - - ResetTimeoutTimer(); - - if (_protocolReaderWriter.ReadMessages(data, _hubDispatcher, out var messages)) - { - foreach (var message in messages) - { - if (message is HubInvocationMessage invocationMessage) - { - _ = DispatchAsync(invocationMessage); - } - else - { - _logger.LogDebug($"Ignore non-HubInvocationMesssage: {message}"); - } - } - - await Task.CompletedTask; - } - } - - private async Task DispatchAsync(HubInvocationMessage message) - { - _logger.LogInformation($"Received message: {message}"); - var isMethodInvocation = message is HubMethodInvocationMessage; - if (isMethodInvocation && - OnConnectedAsyncMethod.IgnoreCaseEquals(((HubMethodInvocationMessage) message).Target)) - { - await OnClientConnectedAsync(message); - return; - } - - var connection = GetHubConnectionContext(message); - if (connection == null) - { - await SendMessageAsync(CompletionMessage.WithError(message.InvocationId, "No connection found.")); - return; - } - - if (isMethodInvocation && - OnDisconnectedAsyncMethod.IgnoreCaseEquals(((HubMethodInvocationMessage) message).Target)) - { - await OnClientDisconnectedAsync(connection); - return; - } - - await _hubDispatcher.DispatchMessageAsync(connection, message); - } - - private async Task OnClientConnectedAsync(HubInvocationMessage message) - { - var connection = CreateHubConnectionContext(message); - _connections.Add(connection); - - await _lifetimeManager.OnConnectedAsync(connection); - - await _hubDispatcher.OnConnectedAsync(connection); - } - - private async Task OnClientDisconnectedAsync(HubConnectionContext connection) - { - await _hubDispatcher.OnDisconnectedAsync(connection, null); - - await _lifetimeManager.OnDisconnectedAsync(connection); - - _connections.Remove(connection); - } - - private HubConnectionContext CreateHubConnectionContext(HubInvocationMessage message) - { - return new CloudHubConnectionContext(_connection, CreateConnectionContext(message), Timeout.InfiniteTimeSpan, _loggerFactory) - { - ProtocolReaderWriter = _protocolReaderWriter - }; - } - - private DefaultConnectionContext CreateConnectionContext(HubInvocationMessage message) - { - var connectionId = message.GetConnectionId(); - // TODO: - // No physical pipeline for logical ConnectionContext. These pipelines won't be used in current context. - // So no exception or error will be thrown. - // We should have a cleaner approach to reuse DefaultConnectionContext for Azure SignalR. - var connectionContext = new DefaultConnectionContext(connectionId, null, null); - if (message.TryGetClaims(out var claims)) - { - connectionContext.User = new ClaimsPrincipal(); - connectionContext.User.AddIdentity(new ClaimsIdentity(claims, "Bearer")); - } - return connectionContext; - } - - private HubConnectionContext GetHubConnectionContext(HubInvocationMessage message) - { - return message.TryGetConnectionId(out var connectionId) ? _connections[connectionId] : null; - } - - private async Task SendMessageAsync(HubMessage hubMessage) - { - var payload = _protocolReaderWriter.WriteMessage(hubMessage); - await _connection.SendAsync(payload, CancellationToken.None); - } - - private class TransferModeFeature : ITransferModeFeature - { - public TransferMode TransferMode { get; set; } - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/CloudHubConnectionContext.cs b/src/Microsoft.Azure.SignalR/HubHost/CloudHubConnectionContext.cs deleted file mode 100644 index 0e3d903e2..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/CloudHubConnectionContext.cs +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Concurrent; -using System.Runtime.ExceptionServices; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Protocols; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.AspNetCore.Sockets.Client; -using Microsoft.Extensions.Logging; - -namespace Microsoft.Azure.SignalR -{ - public class CloudHubConnectionContext : HubConnectionContext - { - private static Action _abortedCallback = AbortConnection; - - private readonly IConnection _connection; - private readonly ILogger _logger; - private readonly CancellationTokenSource _connectionAbortedTokenSource = new CancellationTokenSource(); - private readonly TaskCompletionSource _abortCompletedTcs = new TaskCompletionSource(); - - private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1); - - public CloudHubConnectionContext(IConnection connection, ConnectionContext connectionContext, TimeSpan keepAliveInterval, ILoggerFactory loggerFactory) - : base(connectionContext, keepAliveInterval, loggerFactory) - { - _connection = connection ?? throw new ArgumentNullException(nameof(connection)); - _logger = loggerFactory.CreateLogger(); - } - - internal virtual HubProtocolReaderWriter ProtocolReaderWriter { get; set; } - - internal ExceptionDispatchInfo AbortException { get; private set; } - - // Currently used only for streaming methods - internal ConcurrentDictionary ActiveRequestCancellationSources { get; } = new ConcurrentDictionary(); - - public override async Task WriteAsync(HubMessage message) - { - try - { - await _writeLock.WaitAsync(); - - var buffer = ProtocolReaderWriter.WriteMessage(message); - - await _connection.SendAsync(buffer, CancellationToken.None); - } - finally - { - _writeLock.Release(); - } - } - - public override void Abort() - { - // If we already triggered the token then noop, this isn't thread safe but it's good enough - // to avoid spawning a new task in the most common cases - if (_connectionAbortedTokenSource.IsCancellationRequested) - { - return; - } - - // We fire and forget since this can trigger user code to run - Task.Factory.StartNew(_abortedCallback, this); - } - - internal void Abort(Exception exception) - { - AbortException = ExceptionDispatchInfo.Capture(exception); - Abort(); - } - - private static void AbortConnection(object state) - { - var connection = (CloudHubConnectionContext)state; - try - { - connection._connectionAbortedTokenSource.Cancel(); - - // Communicate the fact that we're finished triggering abort callbacks - connection._abortCompletedTcs.TrySetResult(null); - } - catch (Exception ex) - { - // TODO: Should we log if the cancellation callback fails? This is more preventative to make sure - // we don't end up with an unobserved task - connection._abortCompletedTcs.TrySetException(ex); - } - } - - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HeartBeat.cs b/src/Microsoft.Azure.SignalR/HubHost/HeartBeat.cs new file mode 100644 index 000000000..d1206da6d --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/HeartBeat.cs @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.Extensions.Hosting; + +namespace Microsoft.Azure.SignalR +{ + internal class HeartBeat : BackgroundService + { + private static readonly TimeSpan _heartbeatTickRate = TimeSpan.FromSeconds(1); + private readonly IClientConnectionManager _connectionManager; + private readonly TimerAwaitable _nextHeartbeat; + + public HeartBeat(IClientConnectionManager connectionManager) + { + _connectionManager = connectionManager; + _nextHeartbeat = new TimerAwaitable(_heartbeatTickRate, _heartbeatTickRate); + } + + public override Task StartAsync(CancellationToken cancellationToken) + { + // This will get called when + _nextHeartbeat.Start(); + + return base.StartAsync(cancellationToken); + } + + public override Task StopAsync(CancellationToken cancellationToken) + { + _nextHeartbeat.Stop(); + + return base.StopAsync(cancellationToken); + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + // Dispose the timer when all the code consuming callbacks has completed + using (_nextHeartbeat) + { + // The TimerAwaitable will return true until Stop is called + while (await _nextHeartbeat) + { + // Trigger each connection heartbeat + foreach (var connection in _connectionManager.ClientConnections) + { + connection.Value.TickHeartbeat(); + } + } + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HubHost.cs b/src/Microsoft.Azure.SignalR/HubHost/HubHost.cs deleted file mode 100644 index 427ba1a10..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/HubHost.cs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Linq; -using System.Collections.Generic; -using Microsoft.Extensions.Logging; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.AspNetCore.Sockets; -using Microsoft.AspNetCore.Sockets.Client; -using Microsoft.AspNetCore.Sockets.Client.Http; -using Microsoft.Extensions.Options; - -namespace Microsoft.Azure.SignalR -{ - public class HubHost where THub : Hub - { - private readonly List> _cloudConnections = new List>(); - - private readonly HubLifetimeManager _lifetimeManager; - private readonly IHubProtocolResolver _protocolResolver; - private readonly HubDispatcher _hubDispatcher; - private readonly ILoggerFactory _loggerFactory; - private readonly ILogger> _logger; - - private HubHostOptions _options; - private EndpointProvider _endpointProvider; - private TokenProvider _tokenProvider; - - private readonly string _name = $"HubHost<{typeof(THub).FullName}>"; - - public HubHost(HubLifetimeManager lifetimeManager, - IHubProtocolResolver protocolResolver, - HubDispatcher hubDispatcher, - IOptions options, - ILoggerFactory loggerFactory) - { - _lifetimeManager = lifetimeManager ?? throw new ArgumentNullException(nameof(lifetimeManager)); - _protocolResolver = protocolResolver ?? throw new ArgumentNullException(nameof(protocolResolver)); - _hubDispatcher = hubDispatcher ?? throw new ArgumentNullException(nameof(hubDispatcher)); - _options = options != null ? options.Value : throw new ArgumentNullException(nameof(options)); - - _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); - _logger = loggerFactory.CreateLogger>(); - } - - internal void Configure(EndpointProvider endpointProvider, TokenProvider tokenProvider, - HubHostOptions options = null) - { - if (_endpointProvider != null || _tokenProvider != null) - { - throw new InvalidOperationException( - $"{typeof(THub).FullName} can only bind with one Azure SignalR instance. Binding to multiple instances is forbidden."); - } - - _endpointProvider = endpointProvider ?? throw new ArgumentNullException(nameof(endpointProvider)); - _tokenProvider = tokenProvider ?? throw new ArgumentNullException(nameof(tokenProvider)); - if (options != null) _options = options; - - var serviceUrl = GetServiceUrl(); - var httpOptions = new HttpOptions - { - AccessTokenFactory = () => _tokenProvider.GenerateServerAccessToken() - }; - - // Simply create a couple of connections which connect to Azure SignalR - for (var i = 0; i < _options.ConnectionNumber; i++) - { - var cloudConnection = CreateCloudConnection(serviceUrl, httpOptions); - _cloudConnections.Add(cloudConnection); - } - } - - public async Task StartAsync() - { - _logger.LogInformation($"Starting {_name}..."); - var tasks = _cloudConnections.Select(c => c.StartAsync()); - await Task.WhenAll(tasks); - } - - // TODO: Do not expose this API for now because auto-reconnect is enabled by default. - //public async Task StopAsync() - //{ - // _logger.LogInformation($"Stopping {_name}..."); - // var tasks = _cloudConnections.Select(c => c.StopAsync()); - // await Task.WhenAll(tasks); - //} - - private Uri GetServiceUrl() - { - return new Uri(_endpointProvider.GetServerEndpoint()); - } - - private CloudConnection CreateCloudConnection(Uri serviceUrl, HttpOptions httpOptions) - { - var httpConnection = new HttpConnection(serviceUrl, TransportType.WebSockets, _loggerFactory, httpOptions); - var protocol = GetProtocol(); - return new CloudConnection(httpConnection, protocol, _options, _lifetimeManager, _hubDispatcher, - _loggerFactory); - } - - private IHubProtocol GetProtocol() - { - return _options.ProtocolType == ProtocolType.Text - ? _protocolResolver.GetProtocol("json", null) - : _protocolResolver.GetProtocol("messagepack", null); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HubHostBuilder.cs b/src/Microsoft.Azure.SignalR/HubHost/HubHostBuilder.cs deleted file mode 100644 index 6558c36bd..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/HubHostBuilder.cs +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.Azure.SignalR -{ - public class HubHostBuilder - { - private readonly IServiceProvider _serviceProvider; - private readonly EndpointProvider _endpointProvider; - private readonly TokenProvider _tokenProvider; - - public HubHostBuilder(IServiceProvider serviceProvider, EndpointProvider endpointProvider, - TokenProvider tokenProvider) - { - _serviceProvider = serviceProvider; - _endpointProvider = endpointProvider; - _tokenProvider = tokenProvider; - } - - public HubHost UseHub(HubHostOptions options = null) where THub: Hub - { - var hubHost = _serviceProvider.GetRequiredService>(); - hubHost.Configure(_endpointProvider, _tokenProvider, options); - hubHost.StartAsync().GetAwaiter().GetResult(); - return hubHost; - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HubHostDispatcher.cs b/src/Microsoft.Azure.SignalR/HubHost/HubHostDispatcher.cs deleted file mode 100644 index 7dcb6945c..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/HubHostDispatcher.cs +++ /dev/null @@ -1,458 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Reflection; -using System.Security.Claims; -using System.Threading; -using System.Threading.Channels; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Microsoft.Azure.SignalR.Internal; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Internal; -using Microsoft.Extensions.Logging; - -namespace Microsoft.Azure.SignalR -{ - public class HubHostDispatcher : HubDispatcher where THub : Hub - { - private readonly Dictionary _methods = - new Dictionary(StringComparer.OrdinalIgnoreCase); - - private readonly IServiceScopeFactory _serviceScopeFactory; - private readonly IHubContext _hubContext; - private readonly ILogger> _logger; - - public HubHostDispatcher(IServiceScopeFactory serviceScopeFactory, IHubContext hubContext, - ILogger> logger) - { - _hubContext = hubContext; - _logger = logger; - _serviceScopeFactory = serviceScopeFactory; - - DiscoverHubMethods(); - } - - public override async Task OnConnectedAsync(HubConnectionContext connection) - { - using (var scope = _serviceScopeFactory.CreateScope()) - { - var hubActivator = scope.ServiceProvider.GetRequiredService>(); - var hub = hubActivator.Create(); - try - { - InitializeHub(hub, connection); - await hub.OnConnectedAsync(); - } - finally - { - hubActivator.Release(hub); - } - } - } - - public override async Task OnDisconnectedAsync(HubConnectionContext connection, Exception exception) - { - using (var scope = _serviceScopeFactory.CreateScope()) - { - var hubActivator = scope.ServiceProvider.GetRequiredService>(); - var hub = hubActivator.Create(); - try - { - InitializeHub(hub, connection); - await hub.OnDisconnectedAsync(exception); - } - finally - { - hubActivator.Release(hub); - } - } - } - - public override async Task DispatchMessageAsync(HubConnectionContext connection, HubMessage hubMessage) - { - if (!(connection is CloudHubConnectionContext cloudConnection)) - { - _logger.LogError("Invalid type. CloudHubConnectionContext expected."); - return; - } - - switch (hubMessage) - { - case InvocationMessage invocationMessage: - _logger.LogDebug($"Received invocation: {invocationMessage}"); - await ProcessInvocation(cloudConnection, invocationMessage, isStreamedInvocation: false); - break; - - case StreamInvocationMessage streamInvocationMessage: - _logger.LogDebug($"Received stream invocation: {streamInvocationMessage}"); - await ProcessInvocation(cloudConnection, streamInvocationMessage, isStreamedInvocation: true); - break; - - case CompletionMessage completionMessage: - break; - - case CancelInvocationMessage cancelInvocationMessage: - // Check if there is an associated active stream and cancel it if it exists. - // The cts will be removed when the streaming method completes executing - if (cloudConnection.ActiveRequestCancellationSources.TryGetValue(cancelInvocationMessage.InvocationId, out var cts)) - { - _logger.LogDebug($"Cancel stream invocation: {cancelInvocationMessage.InvocationId}"); - cts.Cancel(); - } - else - { - _logger.LogWarning("Unexpected stream invocation cancel."); - } - break; - - case PingMessage _: - // We don't care about pings - break; - - // Other kind of message we weren't expecting - default: - _logger.LogError($"Received unsupported message type: {hubMessage.GetType().FullName}"); - throw new NotSupportedException($"Received unsupported message: {hubMessage}"); - } - } - - public override IReadOnlyList GetParameterTypes(string methodName) - { - return !_methods.TryGetValue(methodName, out var descriptor) ? Type.EmptyTypes : descriptor.ParameterTypes; - } - - public override Type GetReturnType(string invocationId) - { - return typeof(object); - } - - private async Task ProcessInvocation(CloudHubConnectionContext connection, HubMethodInvocationMessage hubMethodInvocationMessage, bool isStreamedInvocation) - { - try - { - // If an unexpected exception occurs then we want to kill the entire connection - // by ending the processing loop - if (!_methods.TryGetValue(hubMethodInvocationMessage.Target, out var descriptor)) - { - // Send an error to the client. Then let the normal completion process occur - _logger.LogWarning($"Unknown hub method: {hubMethodInvocationMessage.Target}"); - await SendInvocationError(hubMethodInvocationMessage, connection, - $"Unknown hub method '{hubMethodInvocationMessage.Target}'"); - } - else - { - await Invoke(descriptor, connection, hubMethodInvocationMessage, isStreamedInvocation); - } - } - catch (Exception ex) - { - // Abort the entire connection if the invocation fails in an unexpected way - connection.Abort(ex); - } - } - - private async Task Invoke(HubMethodDescriptor descriptor, CloudHubConnectionContext connection, - HubMethodInvocationMessage hubMethodInvocationMessage, bool isStreamedInvocation) - { - var methodExecutor = descriptor.MethodExecutor; - - using (var scope = _serviceScopeFactory.CreateScope()) - { - if (!await IsHubMethodAuthorized(scope.ServiceProvider, connection.User, descriptor.Policies)) - { - _logger.LogError($"Hub method unauthorized: {hubMethodInvocationMessage.Target}"); - await SendInvocationError(hubMethodInvocationMessage, connection, - $"Failed to invoke '{hubMethodInvocationMessage.Target}' because user is unauthorized"); - return; - } - - if (!await ValidateInvocationMode(methodExecutor.MethodReturnType, isStreamedInvocation, hubMethodInvocationMessage, connection)) - { - return; - } - - var hubActivator = scope.ServiceProvider.GetRequiredService>(); - var hub = hubActivator.Create(); - - try - { - InitializeHub(hub, connection); - - var result = await ExecuteHubMethod(methodExecutor, hub, hubMethodInvocationMessage.Arguments); - - if (isStreamedInvocation) - { - var enumerator = GetStreamingEnumerator(connection, hubMethodInvocationMessage.InvocationId, methodExecutor, result, methodExecutor.MethodReturnType); - //Log.StreamingResult(_logger, hubMethodInvocationMessage.InvocationId, methodExecutor); - await StreamResultsAsync(hubMethodInvocationMessage.InvocationId, connection, enumerator); - } - // Non-empty/null InvocationId ==> Blocking invocation that needs a response - else if (!string.IsNullOrEmpty(hubMethodInvocationMessage.InvocationId)) - { - //Log.SendingResult(_logger, hubMethodInvocationMessage.InvocationId, methodExecutor); - await connection.ReturnResultAsync(CompletionMessage.WithResult(hubMethodInvocationMessage.InvocationId, result)); - } - } - catch (TargetInvocationException ex) - { - //Log.FailedInvokingHubMethod(_logger, hubMethodInvocationMessage.Target, ex); - await SendInvocationError(hubMethodInvocationMessage, connection, ex.InnerException.Message); - } - catch (Exception ex) - { - //Log.FailedInvokingHubMethod(_logger, hubMethodInvocationMessage.Target, ex); - await SendInvocationError(hubMethodInvocationMessage, connection, ex.Message); - } - finally - { - hubActivator.Release(hub); - } - } - } - - private async Task StreamResultsAsync(string invocationId, CloudHubConnectionContext connection, IAsyncEnumerator enumerator) - { - string error = null; - - try - { - while (await enumerator.MoveNextAsync()) - { - // Send the stream item - await connection.ReturnResultAsync(new StreamItemMessage(invocationId, enumerator.Current)); - } - } - catch (ChannelClosedException ex) - { - // If the channel closes from an exception in the streaming method, grab the innerException for the error from the streaming method - error = ex.InnerException == null ? ex.Message : ex.InnerException.Message; - } - catch (Exception ex) - { - // If the streaming method was canceled we don't want to send a HubException message - this is not an error case - if (!(ex is OperationCanceledException && connection.ActiveRequestCancellationSources.TryGetValue(invocationId, out var cts) - && cts.IsCancellationRequested)) - { - error = ex.Message; - } - } - finally - { - await connection.ReturnResultAsync(new CompletionMessage(invocationId, error: error, result: null, hasResult: false)); - - if (connection.ActiveRequestCancellationSources.TryRemove(invocationId, out var cts)) - { - cts.Dispose(); - } - } - } - - private static async Task ExecuteHubMethod(ObjectMethodExecutor methodExecutor, THub hub, object[] arguments) - { - // ReadableChannel is awaitable but we don't want to await it. - if (methodExecutor.IsMethodAsync && !IsChannel(methodExecutor.MethodReturnType, out _)) - { - if (methodExecutor.MethodReturnType == typeof(Task)) - { - await (Task)methodExecutor.Execute(hub, arguments); - } - else - { - return await methodExecutor.ExecuteAsync(hub, arguments); - } - } - else - { - return methodExecutor.Execute(hub, arguments); - } - - return null; - } - - private async Task SendInvocationError(HubMethodInvocationMessage hubMethodInvocationMessage, - HubConnectionContext connection, string errorMessage) - { - if (string.IsNullOrEmpty(hubMethodInvocationMessage.InvocationId)) - { - return; - } - - await connection.ReturnResultAsync(CompletionMessage.WithError(hubMethodInvocationMessage.InvocationId, errorMessage)); - } - - private void InitializeHub(THub hub, HubConnectionContext connection) - { - hub.Clients = new HubCallerClients(_hubContext.Clients, connection.ConnectionId); - hub.Context = new HubCallerContext(connection); - hub.Groups = _hubContext.Groups; - } - - private static bool IsChannel(Type type, out Type payloadType) - { - var channelType = type.AllBaseTypes().FirstOrDefault(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ChannelReader<>)); - if (channelType == null) - { - payloadType = null; - return false; - } - else - { - payloadType = channelType.GetGenericArguments()[0]; - return true; - } - } - - private async Task IsHubMethodAuthorized(IServiceProvider provider, ClaimsPrincipal principal, IList policies) - { - // If there are no policies we don't need to run auth - if (!policies.Any()) - { - return true; - } - - var authService = provider.GetRequiredService(); - var policyProvider = provider.GetRequiredService(); - - var authorizePolicy = await AuthorizationPolicy.CombineAsync(policyProvider, policies); - // AuthorizationPolicy.CombineAsync only returns null if there are no policies and we check that above - Debug.Assert(authorizePolicy != null); - - var authorizationResult = await authService.AuthorizeAsync(principal, authorizePolicy); - // Only check authorization success, challenge or forbid wouldn't make sense from a hub method invocation - return authorizationResult.Succeeded; - } - - private async Task ValidateInvocationMode(Type resultType, bool isStreamedInvocation, - HubMethodInvocationMessage hubMethodInvocationMessage, HubConnectionContext connection) - { - var isStreamedResult = IsStreamed(resultType); - if (isStreamedResult && !isStreamedInvocation) - { - // Non-null/empty InvocationId? Blocking - if (!string.IsNullOrEmpty(hubMethodInvocationMessage.InvocationId)) - { - //Log.StreamingMethodCalledWithInvoke(_logger, hubMethodInvocationMessage); - await SendInvocationError(hubMethodInvocationMessage, connection, - $"The client attempted to invoke the streaming '{hubMethodInvocationMessage.Target}' method in a non-streaming fashion."); - } - - return false; - } - - if (!isStreamedResult && isStreamedInvocation) - { - //Log.NonStreamingMethodCalledWithStream(_logger, hubMethodInvocationMessage); - await SendInvocationError(hubMethodInvocationMessage, connection, - $"The client attempted to invoke the non-streaming '{hubMethodInvocationMessage.Target}' method in a streaming fashion."); - - return false; - } - - return true; - } - - private static bool IsStreamed(Type resultType) - { - var observableInterface = IsIObservable(resultType) ? - resultType : - resultType.GetInterfaces().FirstOrDefault(IsIObservable); - - if (observableInterface != null) - { - return true; - } - - if (IsChannel(resultType, out _)) - { - return true; - } - - return false; - } - - private IAsyncEnumerator GetStreamingEnumerator(CloudHubConnectionContext connection, - string invocationId, ObjectMethodExecutor methodExecutor, object result, Type resultType) - { - if (result != null) - { - var observableInterface = IsIObservable(resultType) ? - resultType : - resultType.GetInterfaces().FirstOrDefault(IsIObservable); - if (observableInterface != null) - { - return AsyncEnumeratorAdapters.FromObservable(result, observableInterface, CreateCancellation()); - } - - if (IsChannel(resultType, out var payloadType)) - { - return AsyncEnumeratorAdapters.FromChannel(result, payloadType, CreateCancellation()); - } - } - - //Log.InvalidReturnValueFromStreamingMethod(_logger, methodExecutor.MethodInfo.Name); - throw new InvalidOperationException($"The value returned by the streaming method '{methodExecutor.MethodInfo.Name}' is null, does not implement the IObservable<> interface or is not a ReadableChannel<>."); - - CancellationToken CreateCancellation() - { - var streamCts = new CancellationTokenSource(); - connection.ActiveRequestCancellationSources.TryAdd(invocationId, streamCts); - return CancellationTokenSource.CreateLinkedTokenSource(connection.ConnectionAbortedToken, streamCts.Token).Token; - } - } - - private static bool IsIObservable(Type iface) - { - return iface.IsGenericType && iface.GetGenericTypeDefinition() == typeof(IObservable<>); - } - - private void DiscoverHubMethods() - { - var hubType = typeof(THub); - var hubTypeInfo = hubType.GetTypeInfo(); - var hubName = hubType.Name; - - foreach (var methodInfo in HubReflectionHelper.GetHubMethods(hubType)) - { - var methodName = - methodInfo.GetCustomAttribute()?.Name ?? - methodInfo.Name; - - if (_methods.ContainsKey(methodName)) - { - throw new NotSupportedException($"Duplicate definitions of '{methodName}'. Overloading is not supported."); - } - - var executor = ObjectMethodExecutor.Create(methodInfo, hubTypeInfo); - var authorizeAttributes = methodInfo.GetCustomAttributes(inherit: true); - _methods[methodName] = new HubMethodDescriptor(executor, authorizeAttributes); - - //Log.HubMethodBound(_logger, hubName, methodName); - } - } - - // REVIEW: We can decide to move this out of here if we want pluggable hub discovery - private class HubMethodDescriptor - { - public HubMethodDescriptor(ObjectMethodExecutor methodExecutor, IEnumerable policies) - { - MethodExecutor = methodExecutor; - ParameterTypes = methodExecutor.MethodParameters.Select(p => p.ParameterType).ToArray(); - Policies = policies.ToArray(); - } - - public ObjectMethodExecutor MethodExecutor { get; } - - public IReadOnlyList ParameterTypes { get; } - - public IList Policies { get; } - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HubHostLifetimeManager.cs b/src/Microsoft.Azure.SignalR/HubHost/HubHostLifetimeManager.cs deleted file mode 100644 index 50cb525e0..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/HubHostLifetimeManager.cs +++ /dev/null @@ -1,239 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.Logging; - -namespace Microsoft.Azure.SignalR -{ - public class HubHostLifetimeManager : HubLifetimeManager - { - private readonly HubConnectionList _connections = new HubConnectionList(); - private readonly HubGroupList _groups = new HubGroupList(); - private readonly ILogger> _logger; - - private long _nextInvocationId; - private string InvocationId => Interlocked.Increment(ref _nextInvocationId).ToString(); - - public HubHostLifetimeManager(ILogger> logger) - { - _logger = logger; - } - - public override Task OnConnectedAsync(HubConnectionContext connection) - { - _connections.Add(connection); - return Task.CompletedTask; - } - - public override Task OnDisconnectedAsync(HubConnectionContext connection) - { - _connections.Remove(connection); - return Task.CompletedTask; - } - - public override Task SendAllAsync(string methodName, object[] args) - { - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connection = _connections.FirstOrDefault(); - if (IsNullObject(connection, "No connection found to broadcast message.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendAllAsync)) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList excludedIds) - { - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connection = _connections.FirstOrDefault(); - if (IsNullObject(connection, "No connection found to broadcast message.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendAllExceptAsync)) - .WithExcludedIds(excludedIds) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendConnectionAsync(string connectionId, string methodName, object[] args) - { - if (IsInvalidStringArgument(nameof(connectionId), connectionId)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connection = _connections[connectionId]; - if (IsNullObject(connection, $"Connection[{connectionId}] not found.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendConnectionAsync)) - .WithConnectionId(connectionId) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendConnectionsAsync(IReadOnlyList connectionIds, string methodName, object[] args) - { - if (IsInvalidListArgument(nameof(connectionIds), connectionIds)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connectionId = connectionIds.FirstOrDefault(id => _connections[id] != null); - if (IsEmptyString(connectionId, "No valid connection found.")) return Task.CompletedTask; - - var connection = _connections[connectionId]; - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendConnectionsAsync)) - .WithConnectionIds(connectionIds) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendGroupAsync(string groupName, string methodName, object[] args) - { - if (IsInvalidStringArgument(nameof(groupName), groupName)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var group = _groups[groupName]; - if (IsNullObject(group, $"Group[{groupName}] not found.")) return Task.CompletedTask; - - var connection = group.Values.FirstOrDefault(); - if (IsNullObject(connection, $"Group[{groupName}] is empty.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendGroupAsync)) - .WithGroup(groupName) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendGroupsAsync(IReadOnlyList groupNames, string methodName, object[] args) - { - if (IsInvalidListArgument(nameof(groupNames), groupNames)) return Task.CompletedTask; - - var groupName = groupNames.FirstOrDefault(group => _groups[group]?.Values.FirstOrDefault() != null); - if (IsEmptyString(groupName, "No valid group found.")) return Task.CompletedTask; - - var connection = _groups[groupName].Values.First(); - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendGroupsAsync)) - .WithGroups(groupNames) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList excludedIds) - { - if (IsInvalidStringArgument(nameof(groupName), groupName)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var group = _groups[groupName]; - if (IsNullObject(group, $"Group[{groupName}] not found.")) return Task.CompletedTask; - - var connection = group.Values.FirstOrDefault(); - if (IsNullObject(connection, $"Group[{groupName}] is empty.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendGroupExceptAsync)) - .WithGroup(groupName) - .WithExcludedIds(excludedIds) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendUserAsync(string userId, string methodName, object[] args) - { - if (IsInvalidStringArgument(nameof(userId), userId)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connection = _connections.FirstOrDefault(); - if (IsNullObject(connection, "No connection found to send message.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendUserAsync)) - .WithUser(userId) - .Build(); - return connection.WriteAsync(message); - } - - public override Task SendUsersAsync(IReadOnlyList userIds, string methodName, object[] args) - { - if (IsInvalidListArgument(nameof(userIds), userIds)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(methodName), methodName)) return Task.CompletedTask; - - var connection = _connections.FirstOrDefault(); - if (IsNullObject(connection, "No connection found to send message.")) return Task.CompletedTask; - - var message = new InvocationMessageBuilder(InvocationId, methodName, args) - .WithAction(nameof(SendUsersAsync)) - .WithUsers(userIds) - .Build(); - return connection.WriteAsync(message); - } - - public override Task AddGroupAsync(string connectionId, string groupName) - { - if (IsInvalidStringArgument(nameof(connectionId), connectionId)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(groupName), groupName)) return Task.CompletedTask; - - var connection = _connections[connectionId]; - if (IsNullObject(connection, $"Connection[{connectionId}] not found.")) return Task.CompletedTask; - - _groups.Add(connection, groupName); - var message = new InvocationMessageBuilder(InvocationId, nameof(AddGroupAsync), new object[0]) - .WithAction(nameof(AddGroupAsync)) - .WithConnectionId(connectionId) - .WithGroup(groupName) - .Build(); - return connection.WriteAsync(message); - } - - public override Task RemoveGroupAsync(string connectionId, string groupName) - { - if (IsInvalidStringArgument(nameof(connectionId), connectionId)) return Task.CompletedTask; - if (IsInvalidStringArgument(nameof(groupName), groupName)) return Task.CompletedTask; - - var connection = _connections[connectionId]; - if (IsNullObject(connection, $"Connection[{connectionId}] not found.")) return Task.CompletedTask; - - _groups.Remove(connectionId, groupName); - var message = new InvocationMessageBuilder(InvocationId, nameof(RemoveGroupAsync), new object[0]) - .WithAction(nameof(RemoveGroupAsync)) - .WithConnectionId(connectionId) - .WithGroup(groupName) - .Build(); - return connection.WriteAsync(message); - } - - private bool IsInvalidStringArgument(string name, string value) - { - return IsEmptyString(value, $"Null/empty string argument: {name}"); - } - - private bool IsInvalidListArgument(string name, IReadOnlyList list) - { - if (list != null && list.Any()) return false; - _logger.LogWarning($"Null/empty list argument: {name}"); - return true; - } - - private bool IsEmptyString(string value, string message) - { - if (!string.IsNullOrEmpty(value)) return false; - _logger.LogWarning(message); - return true; - } - - private bool IsNullObject(object value, string message) - { - if (value != null) return false; - _logger.LogWarning(message); - return true; - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/HubHostOptions.cs b/src/Microsoft.Azure.SignalR/HubHost/HubHostOptions.cs deleted file mode 100644 index f2a51773f..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/HubHostOptions.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; - -namespace Microsoft.Azure.SignalR -{ - public class HubHostOptions - { - public static readonly int DefaultConnectionNumber = 5; - public static readonly ProtocolType DefaultProtocolType = ProtocolType.Text; - private static readonly TimeSpan DefaultServerTimeout = TimeSpan.FromSeconds(30); // Server ping rate is 15 sec, this is 2 times that. - - public int ConnectionNumber { get; set; } = DefaultConnectionNumber; - - public ProtocolType ProtocolType { get; set; } = DefaultProtocolType; - - public TimeSpan ServerTimeout { get; set; } = DefaultServerTimeout; - - public Func OnConnected { get; set; } = null; - - public Func OnDisconnected { get; set; } = null; - - public bool AutoReconnect { get; set; } = true; - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionFactory.cs b/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionFactory.cs new file mode 100644 index 000000000..e3ccd01cc --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionFactory.cs @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR +{ + internal interface IClientConnectionFactory + { + ServiceConnectionContext CreateConnection(OpenConnectionMessage message); + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionManager.cs b/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionManager.cs new file mode 100644 index 000000000..5541f1e86 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/IClientConnectionManager.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Concurrent; + +namespace Microsoft.Azure.SignalR +{ + internal interface IClientConnectionManager + { + void AddClientConnection(ServiceConnectionContext clientConnection); + + void RemoveClientConnection(string connectionId); + + ConcurrentDictionary ClientConnections { get; } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/IConnectionFactory.cs b/src/Microsoft.Azure.SignalR/HubHost/IConnectionFactory.cs new file mode 100644 index 000000000..50ab76d83 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/IConnectionFactory.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; + +namespace Microsoft.Azure.SignalR +{ + internal interface IConnectionFactory + { + Task ConnectAsync(TransferFormat transferFormat, string connectionId, CancellationToken cancellationToken = default); + + // Current plan for IAsyncDisposable is that DisposeAsync will NOT take a CancellationToken + // https://github.com/dotnet/csharplang/blob/195efa07806284d7b57550e7447dc8bd39c156bf/proposals/async-streams.md#iasyncdisposable + Task DisposeAsync(ConnectionContext connection); + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/IServiceConnectionManager.cs b/src/Microsoft.Azure.SignalR/HubHost/IServiceConnectionManager.cs new file mode 100644 index 000000000..703f4fbe8 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/IServiceConnectionManager.cs @@ -0,0 +1,20 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR +{ + internal interface IServiceConnectionManager where THub : Hub + { + void AddServiceConnection(ServiceConnection serviceConnection); + + Task StartAsync(); + + Task WriteAsync(ServiceMessage seviceMessage); + + Task WriteAsync(string partitionKey, ServiceMessage serviceMessage); + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/InvocationMessageBuilder.cs b/src/Microsoft.Azure.SignalR/HubHost/InvocationMessageBuilder.cs deleted file mode 100644 index 32a08a5e3..000000000 --- a/src/Microsoft.Azure.SignalR/HubHost/InvocationMessageBuilder.cs +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Collections.Generic; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; - -namespace Microsoft.Azure.SignalR -{ - internal class InvocationMessageBuilder - { - private readonly InvocationMessage _message; - - public InvocationMessageBuilder(string invocationId, string method, object[] args) - { - _message = new InvocationMessage(invocationId, method, null, args); - } - - public InvocationMessage Build() - { - return _message; - } - - public InvocationMessageBuilder WithAction(string action) - { - _message.AddAction(action); - return this; - } - - public InvocationMessageBuilder WithConnectionId(string connectionId) - { - _message.AddConnectionId(connectionId); - return this; - } - - public InvocationMessageBuilder WithConnectionIds(IReadOnlyList connectionIds) - { - _message.AddConnectionIds(connectionIds); - return this; - } - - public InvocationMessageBuilder WithUser(string userId) - { - _message.AddUser(userId); - return this; - } - - public InvocationMessageBuilder WithUsers(IReadOnlyList userIds) - { - _message.AddUsers(userIds); - return this; - } - - public InvocationMessageBuilder WithGroup(string group) - { - _message.AddGroupName(group); - return this; - } - - public InvocationMessageBuilder WithGroups(IReadOnlyList groups) - { - _message.AddGroupNames(groups); - return this; - } - - public InvocationMessageBuilder WithExcludedIds(IReadOnlyList excludedIds) - { - _message.AddExcludedIds(excludedIds); - return this; - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.Log.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.Log.cs new file mode 100644 index 000000000..de88617af --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.Log.cs @@ -0,0 +1,223 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR +{ + internal partial class ServiceConnection + { + private static class Log + { + // Category: ServiceConnection + private static readonly Action _failedToWrite = + LoggerMessage.Define(LogLevel.Error, new EventId(1, "FailedToWrite"), "Failed to send message to the service."); + + private static readonly Action _failedToConnect = + LoggerMessage.Define(LogLevel.Error, new EventId(2, "FailedToConnect"), "Failed to connect to the service."); + + private static readonly Action _errorProcessingMessages = + LoggerMessage.Define(LogLevel.Error, new EventId(3, "ErrorProcessingMessages"), "Error when processing messages."); + + private static readonly Action _connectionDropped = + LoggerMessage.Define(LogLevel.Error, new EventId(4, "ConnectionDropped"), "Connection {ServiceConnectionId} to the service was dropped."); + + private static readonly Action _failedToCleanupConnections = + LoggerMessage.Define(LogLevel.Error, new EventId(5, "FailedToCleanupConnection"), "Failed to clean up client connections."); + + private static readonly Action _errorSendingMessage = + LoggerMessage.Define(LogLevel.Error, new EventId(6, "ErrorSendingMessage"), "Error while sending message to the service."); + + private static readonly Action _sendLoopStopped = + LoggerMessage.Define(LogLevel.Error, new EventId(7, "SendLoopStopped"), "Error while processing messages from {TransportConnectionId}."); + + private static readonly Action _applicationTaskFailed = + LoggerMessage.Define(LogLevel.Error, new EventId(8, "ApplicationTaskFailed"), "Application task failed."); + + private static readonly Action _failToWriteMessageToApplication = + LoggerMessage.Define(LogLevel.Error, new EventId(9, "FailToWriteMessageToApplication"), "Failed to write message to {TransportConnectionId}."); + + private static readonly Action _receivedMessageForNonExistentConnection = + LoggerMessage.Define(LogLevel.Warning, new EventId(10, "ReceivedMessageForNonExistentConnection"), "Received message for connection {TransportConnectionId} which does not exist."); + + private static readonly Action _connectedStarting = + LoggerMessage.Define(LogLevel.Debug, new EventId(11, "ConnectedStarting"), "Connection {TransportConnectionId} started."); + + private static readonly Action _connectedEnding = + LoggerMessage.Define(LogLevel.Debug, new EventId(12, "ConnectedEnding"), "Connection {TransportConnectionId} ended."); + + private static readonly Action _closeConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(13, "CloseConnection"), "Sending close connection message to the service for {TransportConnectionId}."); + + private static readonly Action _serviceConnectionClosed = + LoggerMessage.Define(LogLevel.Debug, new EventId(14, "serviceConnectionClose"), "Service connection {ServiceConnectionId} closed."); + + private static readonly Action _readingCancelled = + LoggerMessage.Define(LogLevel.Trace, new EventId(15, "ReadingCancelled"), "Reading from service connection {ServiceConnectionId} cancelled."); + + private static readonly Action _receivedMessage = + LoggerMessage.Define(LogLevel.Debug, new EventId(16, "ReceivedMessage"), "Received {ReceivedBytes} bytes from service {ServiceConnectionId}."); + + private static readonly Action _startingKeepAliveTimer = + LoggerMessage.Define(LogLevel.Trace, new EventId(17, "StartingKeepAliveTimer"), "Starting keep-alive timer. Duration: {KeepAliveInterval:0.00}ms"); + + private static readonly Action _serviceTimeout = + LoggerMessage.Define(LogLevel.Error, new EventId(18, "ServiceTimeout"), "Service timeout. {ServiceTimeout:0.00}ms elapsed without receiving a message from service."); + + private static readonly Action _writeMessageToApplication = + LoggerMessage.Define(LogLevel.Trace, new EventId(19, "WriteMessageToApplication"), "Writing {ReceivedBytes} to connection {TransportConnectionId}."); + + private static readonly Action _serviceConnectionConnected = + LoggerMessage.Define(LogLevel.Debug, new EventId(20, "ServiceConnectionConnected"), "Service connection {ServiceConnectionId} connected."); + + private static readonly Action _sendingHandshakeRequest = + LoggerMessage.Define(LogLevel.Debug, new EventId(21, "SendingHandshakeRequest"), "Sending Handshake request to service."); + + private static readonly Action _handshakeComplete = + LoggerMessage.Define(LogLevel.Debug, new EventId(22, "HandshakeComplete"), "Handshake with service completes."); + + private static readonly Action _errorReceivingHandshakeResponse = + LoggerMessage.Define(LogLevel.Error, new EventId(23, "ErrorReceivingHandshakeResponse"), "Error receiving handshake response."); + + private static readonly Action _handshakeError = + LoggerMessage.Define(LogLevel.Critical, new EventId(24, "HandshakeError"), "Service returned handshake error: {Error}"); + + private static readonly Action _sentPing = + LoggerMessage.Define(LogLevel.Debug, new EventId(25, "SentPing"), "Sent a ping message to service."); + + private static readonly Action _failedSendingPing = + LoggerMessage.Define(LogLevel.Warning, new EventId(26, "FailedSendingPing"), "Failed sending a ping message to service."); + + public static void FailedToWrite(ILogger logger, Exception exception) + { + _failedToWrite(logger, exception); + } + + public static void FailedToConnect(ILogger logger, Exception exception) + { + _failedToConnect(logger, exception); + } + + public static void ErrorProcessingMessages(ILogger logger, Exception exception) + { + _errorProcessingMessages(logger, exception); + } + + public static void ConnectionDropped(ILogger logger, string serviceConnectionId, Exception exception) + { + _connectionDropped(logger, serviceConnectionId, exception); + } + + public static void FailedToCleanupConnections(ILogger logger, Exception exception) + { + _failedToCleanupConnections(logger, exception); + } + + public static void ErrorSendingMessage(ILogger logger, Exception exception) + { + _errorSendingMessage(logger, exception); + } + + public static void SendLoopStopped(ILogger logger, string connectionId, Exception exception) + { + _sendLoopStopped(logger, connectionId, exception); + } + + public static void ApplicaitonTaskFailed(ILogger logger, Exception exception) + { + _applicationTaskFailed(logger, exception); + } + + public static void FailToWriteMessageToApplication(ILogger logger, string connectionId, Exception exception) + { + _failToWriteMessageToApplication(logger, connectionId, exception); + } + + public static void ReceivedMessageForNonExistentConnection(ILogger logger, string connectionId) + { + _receivedMessageForNonExistentConnection(logger, connectionId, null); + } + + public static void ConnectedStarting(ILogger logger, string connectionId) + { + _connectedStarting(logger, connectionId, null); + } + + public static void ConnectedEnding(ILogger logger, string connectionId) + { + _connectedEnding(logger, connectionId, null); + } + + public static void CloseConnection(ILogger logger, string connectionId) + { + _closeConnection(logger, connectionId, null); + } + + public static void ServiceConnectionClosed(ILogger logger, string serviceConnectionId) + { + _serviceConnectionClosed(logger, serviceConnectionId, null); + } + + public static void ServiceConnectionConnected(ILogger logger, string serviceConnectionId) + { + _serviceConnectionConnected(logger, serviceConnectionId, null); + } + + public static void ReadingCancelled(ILogger logger, string serviceConnectionId) + { + _readingCancelled(logger, serviceConnectionId, null); + } + + public static void ReceivedMessage(ILogger logger, long bytes, string serviceConnectionId) + { + _receivedMessage(logger, bytes, serviceConnectionId, null); + } + + public static void StartingKeepAliveTimer(ILogger logger, TimeSpan keepAliveInterval) + { + _startingKeepAliveTimer(logger, keepAliveInterval.TotalMilliseconds, null); + } + + public static void ServiceTimeout(ILogger logger, TimeSpan serviceTimeout) + { + _serviceTimeout(logger, serviceTimeout.TotalMilliseconds, null); + } + + public static void WriteMessageToApplication(ILogger logger, long count, string connectionId) + { + _writeMessageToApplication(logger, count, connectionId, null); + } + + public static void SendingHandshakeRequest(ILogger logger) + { + _sendingHandshakeRequest(logger, null); + } + + public static void HandshakeComplete(ILogger logger) + { + _handshakeComplete(logger, null); + } + + public static void ErrorReceivingHandshakeResponse(ILogger logger, Exception exception) + { + _errorReceivingHandshakeResponse(logger, exception); + } + + public static void HandshakeError(ILogger logger, string error) + { + _handshakeError(logger, error, null); + } + + public static void SentPing(ILogger logger) + { + _sentPing(logger, null); + } + + public static void FailedSendingPing(ILogger logger, Exception exception) + { + _failedSendingPing(logger, exception); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.cs new file mode 100644 index 000000000..826a721bc --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnection.cs @@ -0,0 +1,240 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR +{ + internal partial class ServiceConnection : ServiceConnectionBase + { + private readonly IConnectionFactory _connectionFactory; + private readonly IClientConnectionFactory _clientConnectionFactory; + private readonly IClientConnectionManager _clientConnectionManager; + + private readonly ConcurrentDictionary _connectionIds = + new ConcurrentDictionary(StringComparer.Ordinal); + + private readonly ConnectionDelegate _connectionDelegate; + + public ServiceConnection(IServiceProtocol serviceProtocol, + IClientConnectionManager clientConnectionManager, + IConnectionFactory connectionFactory, + ILoggerFactory loggerFactory, + ConnectionDelegate connectionDelegate, + IClientConnectionFactory clientConnectionFactory, + string connectionId) : + base(serviceProtocol, loggerFactory.CreateLogger(), connectionId) + { + _clientConnectionManager = clientConnectionManager; + _connectionFactory = connectionFactory; + _connectionDelegate = connectionDelegate; + _clientConnectionFactory = clientConnectionFactory; + } + + protected override Task CreateConnection() + { + return _connectionFactory.ConnectAsync(TransferFormat.Binary, _connectionId); + } + + protected override Task DisposeConnection() + { + var connection = _connection; + _connection = null; + return _connectionFactory.DisposeAsync(connection); + } + + protected override async Task CleanupConnections() + { + try + { + if (_connectionIds.Count == 0) + { + return; + } + var tasks = new List(_connectionIds.Count); + foreach (var connection in _connectionIds) + { + tasks.Add(PerformDisconnectAsyncCore(connection.Key)); + } + await Task.WhenAll(tasks); + } + catch (Exception ex) + { + Log.FailedToCleanupConnections(_logger, ex); + } + } + + private async Task ProcessOutgoingMessagesAsync(ServiceConnectionContext connection) + { + try + { + while (true) + { + var result = await connection.Application.Input.ReadAsync(); + var buffer = result.Buffer; + if (!buffer.IsEmpty) + { + try + { + // Forward the message to the service + await WriteAsync(new ConnectionDataMessage(connection.ConnectionId, buffer)); + } + catch (Exception ex) + { + Log.ErrorSendingMessage(_logger, ex); + } + } + + if (result.IsCompleted) + { + // This connection ended (the application itself shut down) we should remove it from the list of connections + break; + } + + connection.Application.Input.AdvanceTo(buffer.End); + } + } + catch (Exception ex) + { + Log.SendLoopStopped(_logger, connection.ConnectionId, ex); + } + finally + { + connection.Application.Input.Complete(); + } + } + + private void AddClientConnection(ServiceConnectionContext connection) + { + _clientConnectionManager.AddClientConnection(connection); + _connectionIds.TryAdd(connection.ConnectionId, connection.ConnectionId); + } + + private void RemoveClientConnection(string connectionId) + { + _clientConnectionManager.RemoveClientConnection(connectionId); + _connectionIds.TryRemove(connectionId, out _); + } + + protected override Task OnConnectedAsync(OpenConnectionMessage message) + { + var connection = _clientConnectionFactory.CreateConnection(message); + AddClientConnection(connection); + Log.ConnectedStarting(_logger, connection.ConnectionId); + + // Execute the application code + connection.ApplicationTask = _connectionDelegate(connection); + + // Writing from the application to the service + _ = ProcessOutgoingMessagesAsync(connection); + + // Waiting for the application to shutdown so we can clean up the connection + _ = WaitOnApplicationTask(connection); + return Task.CompletedTask; + } + + private async Task WaitOnApplicationTask(ServiceConnectionContext connection) + { + Exception exception = null; + + try + { + // Wait for the application task to complete + await connection.ApplicationTask; + } + catch (Exception ex) + { + // Capture the exception to communicate it to the transport (this isn't strictly required) + exception = ex; + } + finally + { + // Close the transport side since the application is no longer running + connection.Transport.Output.Complete(exception); + connection.Transport.Input.Complete(); + } + + // If we aren't already aborted, we send the abort message to the service + if (connection.AbortOnClose) + { + // Inform the Service that we will remove the client because SignalR told us it is disconnected. + var serviceMessage = new CloseConnectionMessage(connection.ConnectionId, errorMessage: ""); + await WriteAsync(serviceMessage); + Log.CloseConnection(_logger, connection.ConnectionId); + } + } + + protected override Task OnDisconnectedAsync(CloseConnectionMessage closeConnectionMessage) + { + var connectionId = closeConnectionMessage.ConnectionId; + return PerformDisconnectAsyncCore(connectionId); + } + + private async Task PerformDisconnectAsyncCore(string connectionId) + { + if (_clientConnectionManager.ClientConnections.TryGetValue(connectionId, out var connection)) + { + // Service already knows the client is closed, no need to be informed. + connection.AbortOnClose = false; + + // We're done writing to the application output + connection.Application.Output.Complete(); + + // Wait on the application task to complete + try + { + await connection.ApplicationTask; + } + catch (Exception ex) + { + Log.ApplicaitonTaskFailed(_logger, ex); + } + } + // Close this connection gracefully then remove it from the list, + // this will trigger the hub shutdown logic appropriately + RemoveClientConnection(connectionId); + Log.ConnectedEnding(_logger, connectionId); + } + + protected override async Task OnMessageAsync(ConnectionDataMessage connectionDataMessage) + { + if (_clientConnectionManager.ClientConnections.TryGetValue(connectionDataMessage.ConnectionId, out var connection)) + { + try + { + var payload = connectionDataMessage.Payload; + Log.WriteMessageToApplication(_logger, payload.Length, connectionDataMessage.ConnectionId); + + if (payload.IsSingleSegment) + { + // Write the raw connection payload to the pipe let the upstream handle it + await connection.Application.Output.WriteAsync(payload.First); + } + else + { + var position = payload.Start; + while (connectionDataMessage.Payload.TryGet(ref position, out var memory)) + { + await connection.Application.Output.WriteAsync(memory); + } + } + } + catch (Exception ex) + { + Log.FailToWriteMessageToApplication(_logger, connectionDataMessage.ConnectionId, ex); + } + } + else + { + // Unexpected error + Log.ReceivedMessageForNonExistentConnection(_logger, connectionDataMessage.ConnectionId); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionContext.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionContext.cs new file mode 100644 index 000000000..8d14a754b --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionContext.cs @@ -0,0 +1,171 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO.Pipelines; +using System.Linq; +using System.Net; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Connections.Features; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Connections.Features; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.Http.Features.Authentication; +using Microsoft.AspNetCore.SignalR; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Primitives; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceConnectionContext : ConnectionContext, + IConnectionUserFeature, + IConnectionItemsFeature, + IConnectionIdFeature, + IConnectionTransportFeature, + IConnectionHeartbeatFeature, + IHttpContextFeature + { + private static readonly string[] SystemClaims = + { + "aud", // Audience claim, used by service to make sure token is matched with target resource. + "exp", // Expiration time claims. A token is valid only before its expiration time. + "iat", // Issued At claim. Added by default. It is not validated by service. + "nbf" // Not Before claim. Added by default. It is not validated by service. + }; + + private static readonly PipeOptions DefaultPipeOptions = new PipeOptions(pauseWriterThreshold: 0, + resumeWriterThreshold: 0, + readerScheduler: PipeScheduler.ThreadPool, + useSynchronizationContext: false); + + private readonly object _heartbeatLock = new object(); + private List<(Action handler, object state)> _heartbeatHandlers; + + public ServiceConnectionContext(OpenConnectionMessage serviceMessage, PipeOptions transportPipeOptions = null, PipeOptions appPipeOptions = null) + { + ConnectionId = serviceMessage.ConnectionId; + User = new ClaimsPrincipal(); + User.AddIdentity(IsAuthenticatedUser(serviceMessage.Claims) + ? new ClaimsIdentity(serviceMessage.Claims, "Bearer") + : new ClaimsIdentity()); + + // Create the Duplix Pipeline for the virtual connection + transportPipeOptions = transportPipeOptions ?? DefaultPipeOptions; + appPipeOptions = appPipeOptions ?? DefaultPipeOptions; + + var pair = DuplexPipe.CreateConnectionPair(transportPipeOptions, appPipeOptions); + Transport = pair.Application; + Application = pair.Transport; + + HttpContext = BuildHttpContext(serviceMessage); + + Features = BuildFeatures(); + } + + public void OnHeartbeat(Action action, object state) + { + lock (_heartbeatLock) + { + if (_heartbeatHandlers == null) + { + _heartbeatHandlers = new List<(Action handler, object state)>(); + } + _heartbeatHandlers.Add((action, state)); + } + } + + public void TickHeartbeat() + { + lock (_heartbeatLock) + { + if (_heartbeatHandlers == null) + { + return; + } + + foreach (var (handler, state) in _heartbeatHandlers) + { + handler(state); + } + } + } + + // Send "Abort" to service on close except that Service asks SDK to close + public bool AbortOnClose { get; set; } = true; + + public override string ConnectionId { get; set; } + + public override IFeatureCollection Features { get; } + + public override IDictionary Items { get; set; } = new ConnectionItems(new ConcurrentDictionary()); + + public override IDuplexPipe Transport { get; set; } + + public IDuplexPipe Application { get; set; } + + public ClaimsPrincipal User { get; set; } + + public Task ApplicationTask { get; set; } + + // The associated HubConnectionContext + public HubConnectionContext HubConnectionContext { get; set; } + + public HttpContext HttpContext { get; set; } + + private static bool IsAuthenticatedUser(IReadOnlyCollection claims) + { + return claims?.Count > 0 && + claims.Any(claim => + !SystemClaims.Contains(claim.Type) && + // Claim name is case sensitive. See https://tools.ietf.org/html/rfc7519#section-10.1.1 . + !claim.Type.StartsWith(Constants.ClaimType.AzureSignalRSysPrefix, StringComparison.Ordinal)); + } + + private FeatureCollection BuildFeatures() + { + var features = new FeatureCollection(); + features.Set(this); + features.Set(this); + features.Set(this); + features.Set(this); + features.Set(this); + features.Set(this); + return features; + } + + private HttpContext BuildHttpContext(OpenConnectionMessage message) + { + var httpContextFeatures = new FeatureCollection(); + httpContextFeatures.Set(new HttpRequestFeature + { + Headers = new HeaderDictionary((Dictionary)message.Headers), + QueryString = message.QueryString, + Path = GetOriginalPath(message.QueryString) + }); + httpContextFeatures.Set(new HttpAuthenticationFeature + { + User = User + }); + if (message.Headers.ContainsKey("X-Forwarded-For") && + IPAddress.TryParse(message.Headers["X-Forwarded-For"][0], out var address)) + { + httpContextFeatures.Set(new HttpConnectionFeature { RemoteIpAddress = address }); + } + + return new DefaultHttpContext(httpContextFeatures); + } + + private static string GetOriginalPath(string queryString) + { + var query = QueryHelpers.ParseNullableQuery(queryString); + return query?.TryGetValue(Constants.QueryParameter.OriginalPath, out var originalPath) == true + ? originalPath.FirstOrDefault() + : string.Empty; + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionManager.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionManager.cs new file mode 100644 index 000000000..1e1ac8a65 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceConnectionManager.cs @@ -0,0 +1,48 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceConnectionManager : IServiceConnectionManager where THub : Hub + { + private readonly List _serviceConnections = new List(); + + public void AddServiceConnection(ServiceConnection serviceConnection) + { + _serviceConnections.Add(serviceConnection); + } + + public async Task StartAsync() + { + var tasks = _serviceConnections.Select(c => c.StartAsync()); + await Task.WhenAll(tasks); + } + + public async Task WriteAsync(ServiceMessage serviceMessage) + { + var index = StaticRandom.Next(_serviceConnections.Count); + await _serviceConnections[index].WriteAsync(serviceMessage); + } + + public async Task WriteAsync(string partitionKey, ServiceMessage serviceMessage) + { + if (_serviceConnections.Count == 0) return; + + // If we hit this check, it is a code bug. + if (string.IsNullOrEmpty(partitionKey)) + { + throw new ArgumentNullException(nameof(partitionKey)); + } + + var index = (partitionKey.GetHashCode() & int.MaxValue) % _serviceConnections.Count; + await _serviceConnections[index].WriteAsync(serviceMessage); + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs new file mode 100644 index 000000000..600832f40 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs @@ -0,0 +1,127 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.Http.Connections.Client; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceHubDispatcher : IConnectionFactory where THub : Hub + { + private readonly ILoggerFactory _loggerFactory; + private readonly ILogger> _logger; + private readonly ServiceOptions _options; + private readonly IServiceEndpointProvider _serviceEndpointProvider; + private readonly IServiceConnectionManager _serviceConnectionManager; + private readonly IClientConnectionManager _clientConnectionManager; + private readonly IServiceProtocol _serviceProtocol; + private readonly IClientConnectionFactory _clientConnectionFactory; + private readonly string _userId; + private static readonly string Name = $"ServiceHubDispatcher<{typeof(THub).FullName}>"; + // Fix issue: https://github.com/Azure/azure-signalr/issues/198 + // .NET Framework has restriction about reserved string as the header name like "User-Agent" + private static Dictionary CustomHeader = new Dictionary { { "Asrs-User-Agent", ProductInfo.GetProductInfo() } }; + + public ServiceHubDispatcher(IServiceProtocol serviceProtocol, + IServiceConnectionManager serviceConnectionManager, + IClientConnectionManager clientConnectionManager, + IServiceEndpointProvider serviceEndpointProvider, + IOptions options, + ILoggerFactory loggerFactory, + IClientConnectionFactory clientConnectionFactory) + { + _serviceProtocol = serviceProtocol; + _serviceConnectionManager = serviceConnectionManager; + _clientConnectionManager = clientConnectionManager; + _serviceEndpointProvider = serviceEndpointProvider; + _options = options != null ? options.Value : throw new ArgumentNullException(nameof(options)); + + _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); + _logger = loggerFactory.CreateLogger>(); + _clientConnectionFactory = clientConnectionFactory; + _userId = GenerateServerName(); + } + + private static string GenerateServerName() + { + // Use the machine name for convenient diagnostics, but add a guid to make it unique. + // Example: MyServerName_02db60e5fab243b890a847fa5c4dcb29 + return $"{Environment.MachineName}_{Guid.NewGuid():N}"; + } + + public void Start(ConnectionDelegate connectionDelegate) + { + // Simply create a couple of connections which connect to Azure SignalR + for (var i = 0; i < _options.ConnectionCount; i++) + { + var serviceConnection = new ServiceConnection(_serviceProtocol, _clientConnectionManager, this, _loggerFactory, connectionDelegate, _clientConnectionFactory, Guid.NewGuid().ToString()); + _serviceConnectionManager.AddServiceConnection(serviceConnection); + } + Log.StartingConnection(_logger, Name, _options.ConnectionCount); + _ = _serviceConnectionManager.StartAsync(); + } + + private Uri GetServiceUrl(string connectionId) + { + var baseUri = new UriBuilder(_serviceEndpointProvider.GetServerEndpoint()); + var query = "cid=" + connectionId; + if (baseUri.Query != null && baseUri.Query.Length > 1) + { + baseUri.Query = baseUri.Query.Substring(1) + "&" + query; + } + else + { + baseUri.Query = query; + } + return baseUri.Uri; + } + + public async Task ConnectAsync(TransferFormat transferFormat, string connectionId, CancellationToken cancellationToken = default) + { + var httpConnectionOptions = new HttpConnectionOptions + { + Url = GetServiceUrl(connectionId), + AccessTokenProvider = () => Task.FromResult(_serviceEndpointProvider.GenerateServerAccessToken(_userId)), + Transports = HttpTransportType.WebSockets, + SkipNegotiation = true, + Headers = CustomHeader + }; + var httpConnection = new HttpConnection(httpConnectionOptions, _loggerFactory); + try + { + await httpConnection.StartAsync(transferFormat); + return httpConnection; + } + catch + { + await httpConnection.DisposeAsync(); + throw; + } + } + + public Task DisposeAsync(ConnectionContext connection) + { + return ((HttpConnection)connection).DisposeAsync(); + } + + private static class Log + { + private static readonly Action _startingConnection = + LoggerMessage.Define(LogLevel.Debug, new EventId(1, "StartingConnection"), "Staring {name} with {connectionNumber} connections..."); + + public static void StartingConnection(ILogger logger, string name, int connectionNumber) + { + _startingConnection(logger, name, connectionNumber, null); + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs new file mode 100644 index 000000000..fdc7deef6 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceLifetimeManager.cs @@ -0,0 +1,214 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.AspNetCore.SignalR.Protocol; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceLifetimeManager : HubLifetimeManager where THub : Hub + { + private const string MarkerNotConfiguredError = + "'AddAzureSignalR(...)' was called without a matching call to 'IApplicationBuilder.UseAzureSignalR(...)'."; + + private readonly ILogger> _logger; + private readonly IReadOnlyList _allProtocols; + + private readonly IServiceConnectionManager _serviceConnectionManager; + private readonly IClientConnectionManager _clientConnectionManager; + + public ServiceLifetimeManager(IServiceConnectionManager serviceConnectionManager, + IClientConnectionManager clientConnectionManager, IHubProtocolResolver protocolResolver, + ILogger> logger, AzureSignalRMarkerService marker) + { + if (!marker.IsConfigured) + { + throw new InvalidOperationException(MarkerNotConfiguredError); + } + + _serviceConnectionManager = serviceConnectionManager; + _clientConnectionManager = clientConnectionManager; + _allProtocols = protocolResolver.AllProtocols; + _logger = logger; + } + + public override Task OnConnectedAsync(HubConnectionContext connection) + { + if (_clientConnectionManager.ClientConnections.TryGetValue(connection.ConnectionId, out var serviceConnectionContext)) + { + serviceConnectionContext.HubConnectionContext = connection; + } + + return Task.CompletedTask; + } + + public override Task OnDisconnectedAsync(HubConnectionContext connection) + { + return Task.CompletedTask; + } + + public override Task SendAllAsync(string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + return _serviceConnectionManager.WriteAsync( + new BroadcastDataMessage(null, SerializeAllProtocols(methodName, args))); + } + + public override Task SendAllExceptAsync(string methodName, object[] args, IReadOnlyList excludedIds, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + return _serviceConnectionManager.WriteAsync( + new BroadcastDataMessage(excludedIds, SerializeAllProtocols(methodName, args))); + } + + public override Task SendConnectionAsync(string connectionId, string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(connectionId) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + if (!_clientConnectionManager.ClientConnections.TryGetValue(connectionId, out var serviceConnectionContext)) + { + // Connection isn't on this server so serialize to all protocols and send it to the service + return _serviceConnectionManager.WriteAsync( + new MultiConnectionDataMessage(new[] { connectionId }, SerializeAllProtocols(methodName, args))); + } + + var message = new InvocationMessage(methodName, args); + + // Write directly to this connection + return serviceConnectionContext.HubConnectionContext.WriteAsync(message).AsTask(); + } + + public override Task SendConnectionsAsync(IReadOnlyList connectionIds, string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(connectionIds) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + return _serviceConnectionManager.WriteAsync( + new MultiConnectionDataMessage(connectionIds, SerializeAllProtocols(methodName, args))); + } + + public override Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(groupName) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + var message = new GroupBroadcastDataMessage(groupName, null, SerializeAllProtocols(methodName, args)); + // Send this message from a fixed service connection, so that message order can be reserved. + return _serviceConnectionManager.WriteAsync(groupName, message); + } + + public override Task SendGroupsAsync(IReadOnlyList groupNames, string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(groupNames)) + { + return Task.CompletedTask; + } + + // Send this message from a random service connection because this message involves of multiple groups. + // Unless we send message for each group one by one, we can not guarantee the message order for all groups. + return _serviceConnectionManager.WriteAsync( + new MultiGroupBroadcastDataMessage(groupNames, SerializeAllProtocols(methodName, args))); + } + + public override Task SendGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList excludedIds, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(groupName) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + var message = new GroupBroadcastDataMessage(groupName, excludedIds, SerializeAllProtocols(methodName, args)); + // Send this message from a fixed service connection, so that message order can be reserved. + return _serviceConnectionManager.WriteAsync(groupName, message); + } + + public override Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(userId) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + return _serviceConnectionManager.WriteAsync( + new UserDataMessage(userId, SerializeAllProtocols(methodName, args))); + } + + public override Task SendUsersAsync(IReadOnlyList userIds, string methodName, object[] args, + CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(userIds) || IsInvalidArgument(methodName)) + { + return Task.CompletedTask; + } + + return _serviceConnectionManager.WriteAsync( + new MultiUserDataMessage(userIds, SerializeAllProtocols(methodName, args))); + } + + public override Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(connectionId) || IsInvalidArgument(groupName)) + { + return Task.CompletedTask; + } + + var message = new JoinGroupMessage(connectionId, groupName); + // Send this message from a fixed service connection, so that message order can be reserved. + return _serviceConnectionManager.WriteAsync(groupName, message); + } + + public override Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default) + { + if (IsInvalidArgument(connectionId) || IsInvalidArgument(groupName)) + { + return Task.CompletedTask; + } + + var message = new LeaveGroupMessage(connectionId, groupName); + // Send this message from a fixed service connection, so that message order can be reserved. + return _serviceConnectionManager.WriteAsync(groupName, message); + } + + private static bool IsInvalidArgument(string value) + { + return string.IsNullOrEmpty(value); + } + + private static bool IsInvalidArgument(IReadOnlyList list) + { + return list == null || list.Count == 0; + } + + private IDictionary> SerializeAllProtocols(string method, object[] args) + { + var payloads = new Dictionary>(); + var message = new InvocationMessage(method, args); + foreach (var hubProtocol in _allProtocols) + { + payloads.Add(hubProtocol.Name, hubProtocol.GetMessageBytes(message)); + } + return payloads; + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceOptionsSetup.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceOptionsSetup.cs new file mode 100644 index 000000000..2b8a1146c --- /dev/null +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceOptionsSetup.cs @@ -0,0 +1,37 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Options; + +namespace Microsoft.Azure.SignalR +{ + internal class ServiceOptionsSetup : IConfigureOptions + { + private static readonly string ConnectionStringSecondaryKey = + $"ConnectionStrings:{ServiceOptions.ConnectionStringDefaultKey}"; + + private readonly string _connectionString; + + public ServiceOptionsSetup(IConfiguration configuration) + { + var connectionString = configuration.GetSection(ServiceOptions.ConnectionStringDefaultKey).Value; + + // Load connection string from "ConnectionStrings" section when default key doesn't exist or holds an empty value. + if (string.IsNullOrEmpty(connectionString)) + { + connectionString = configuration.GetSection(ConnectionStringSecondaryKey).Value; + } + + _connectionString = connectionString; + } + + public void Configure(ServiceOptions options) + { + if (options.ConnectionString == null) + { + options.ConnectionString = _connectionString; + } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/ClientProxy.cs b/src/Microsoft.Azure.SignalR/HubProxy/ClientProxy.cs deleted file mode 100644 index 047041672..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/ClientProxy.cs +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; -using System.Threading.Tasks; -using Newtonsoft.Json; - -namespace Microsoft.Azure.SignalR -{ - public class ClientProxy : IClientProxy - { - private readonly string _url; - private readonly Func _jwtBearerProvider; - private readonly IReadOnlyList _excludedIds; - - public ClientProxy(string url, Func jwtBearerProvider, IReadOnlyList excludedIds = null) - { - _url = url; - _jwtBearerProvider = jwtBearerProvider; - _excludedIds = excludedIds; - } - - // TODO: Translate HttpResponseMessage to typed error - public Task SendAsync(string method, object[] args) - { - var request = new HttpRequestMessage - { - Method = HttpMethod.Post, - RequestUri = new Uri(_url) - }; - - request.Headers.Authorization = - new AuthenticationHeaderValue("Bearer", _jwtBearerProvider.Invoke()); - - request.Headers.Accept.Clear(); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Headers.AcceptCharset.Clear(); - request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("UTF-8")); - - request.Content = new StringContent( - JsonConvert.SerializeObject(new - { - method = method, - arguments = args, - excluded = _excludedIds - }), Encoding.UTF8, "application/json"); - - var client = new HttpClient (); - return client.SendAsync(request); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/ClientProxyFactory.cs b/src/Microsoft.Azure.SignalR/HubProxy/ClientProxyFactory.cs deleted file mode 100644 index 6db963e86..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/ClientProxyFactory.cs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Security.Claims; - -namespace Microsoft.Azure.SignalR -{ - internal class ClientProxyFactory - { - private const int ProxyPort = 5002; - - public static ClientProxy CreateAllClientsProxy(string endpoint, string apiVersion, string accessKey, - string hubName) - { - return InternalCreateClientProxy( - GetBaseUrl(endpoint, apiVersion, hubName), - accessKey, hubName); - } - - public static ClientProxy CreateAllClientsExceptProxy(string endpoint, string apiVersion, string accessKey, - string hubName, IReadOnlyList excludedIds) - { - return InternalCreateClientProxy( - GetBaseUrl(endpoint, apiVersion, hubName), - accessKey, hubName, excludedIds); - } - - public static ClientProxy CreateSingleClientProxy(string endpoint, string apiVersion, string accessKey, - string hubName, string connectionId) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/connection/{connectionId}", - accessKey, hubName); - } - - public static ClientProxy CreateMultipleClientProxy(string endpoint, string apiVersion, string accessKey, - string hubName, IReadOnlyList connectionIds) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/connections/{string.Join(",", connectionIds)}", - accessKey, hubName); - } - - public static ClientProxy CreateSingleUserProxy(string endpoint, string apiVersion, string accessKey, - string hubName, string userId) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/user/{userId}", - accessKey, hubName); - } - - public static ClientProxy CreateMultipleUserProxy(string endpoint, string apiVersion, string accessKey, - string hubName, IReadOnlyList userIds) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/users/{string.Join(",", userIds)}", - accessKey, hubName); - } - - public static ClientProxy CreateSingleGroupProxy(string endpoint, string apiVersion, string accessKey, - string hubName, string groupName) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/group/{groupName}", - accessKey, hubName); - } - - public static ClientProxy CreateMultipleGroupProxy(string endpoint, string apiVersion, string accessKey, - string hubName, IReadOnlyList groupNames) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/groups/{string.Join(",", groupNames)}", - accessKey, hubName); - } - - public static ClientProxy CreateSingleGroupExceptProxy(string endpoint, string apiVersion, string accessKey, - string hubName, string groupName, IReadOnlyList excludedIds) - { - return InternalCreateClientProxy( - $"{GetBaseUrl(endpoint, apiVersion, hubName)}/group/{groupName}", - accessKey, hubName, excludedIds); - } - - private static string GetBaseUrl(string endpoint, string apiVersion, string hubName) - { - return $"{endpoint}:{ProxyPort}/{apiVersion}/hub/{hubName}"; - } - - private static ClientProxy InternalCreateClientProxy(string url, string accessKey, string hubName, - IReadOnlyList excludedIds = null) - { - return new ClientProxy(url, () => GenerateAccessToken(url, accessKey, hubName), excludedIds); - } - - private static string GenerateAccessToken(string audience, string accessKey, string hubName) - { - var name = $"HubProxy[{hubName}]"; - return AuthenticationHelper.GenerateJwtBearer( - audience: audience, - claims: new[] - { - new Claim(ClaimTypes.Name, name), - new Claim(ClaimTypes.NameIdentifier, name) - }, - expires: DateTime.UtcNow.Add(TokenProvider.DefaultAccessTokenLifetime), - signingKey: accessKey - ); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.Azure.SignalR/HubProxy/GroupManagerProxy.cs b/src/Microsoft.Azure.SignalR/HubProxy/GroupManagerProxy.cs deleted file mode 100644 index b1b7e5ec2..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/GroupManagerProxy.cs +++ /dev/null @@ -1,86 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; - -namespace Microsoft.Azure.SignalR -{ - public class GroupManagerProxy : IGroupManager - { - private const int ProxyPort = 5002; - - private readonly string _baseUri; - private readonly string _accessKey; - - public GroupManagerProxy(string endpoint, string accessKey, string hubName, HubProxyOptions options) - { - if (string.IsNullOrEmpty(endpoint)) - { - throw new ArgumentNullException(nameof(endpoint)); - } - - if (string.IsNullOrEmpty(accessKey)) - { - throw new ArgumentNullException(nameof(accessKey)); - } - - if (string.IsNullOrEmpty(hubName)) - { - throw new ArgumentNullException(nameof(hubName)); - } - - var apiVersion = options?.ApiVersion ?? HubProxyOptions.DefaultApiVersion; - _baseUri = $"{endpoint}:{ProxyPort}/{apiVersion}/hub/{hubName.ToLower()}/group"; - _accessKey = accessKey; - } - - public Task AddAsync(string connectionId, string groupName) - { - var uri = GetRequestUri(connectionId, groupName); - return SendAsync(uri, HttpMethod.Post); - } - - public Task RemoveAsync(string connectionId, string groupName) - { - var uri = GetRequestUri(connectionId, groupName); - return SendAsync(uri, HttpMethod.Delete); - } - - private string GetRequestUri(string connectionId, string groupName) - { - return $"{_baseUri}/{groupName}/connection/{connectionId}"; - } - - private Task SendAsync(string uri, HttpMethod method) - { - var request = new HttpRequestMessage - { - Method = method, - RequestUri = new Uri(uri) - }; - - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", GenerateAccessToken(uri)); - - request.Headers.Accept.Clear(); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Headers.AcceptCharset.Clear(); - request.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("UTF-8")); - - return new HttpClient().SendAsync(request); - } - - private string GenerateAccessToken(string audience) - { - return AuthenticationHelper.GenerateJwtBearer( - audience: audience, - claims: null, - expires: DateTime.UtcNow.Add(TokenProvider.DefaultAccessTokenLifetime), - signingKey: _accessKey - ); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/HubClientsProxy.cs b/src/Microsoft.Azure.SignalR/HubProxy/HubClientsProxy.cs deleted file mode 100644 index efe0c48df..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/HubClientsProxy.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.SignalR; - -namespace Microsoft.Azure.SignalR -{ - public class HubClientsProxy : IHubClients - { - private readonly string _endpoint; - private readonly string _accessKey; - private readonly string _hubName; - private readonly HubProxyOptions _options; - - public HubClientsProxy(string endpoint, string accessKey, string hubName, HubProxyOptions options) - { - if (string.IsNullOrEmpty(endpoint)) - { - throw new ArgumentNullException(nameof(endpoint)); - } - - if (string.IsNullOrEmpty(accessKey)) - { - throw new ArgumentNullException(nameof(accessKey)); - } - - if (string.IsNullOrEmpty(hubName)) - { - throw new ArgumentNullException(nameof(hubName)); - } - - _endpoint = endpoint; - _accessKey = accessKey; - _hubName = hubName.ToLower(); - _options = options ?? HubProxyOptions.DefaultHubProxyOptions; - - All = ClientProxyFactory.CreateAllClientsProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName); - } - - public IClientProxy All { get; } - - public IClientProxy AllExcept(IReadOnlyList excludedIds) - { - return ClientProxyFactory.CreateAllClientsExceptProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - excludedIds); - } - - public IClientProxy Client(string connectionId) - { - return ClientProxyFactory.CreateSingleClientProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - connectionId); - } - - public IClientProxy Clients(IReadOnlyList connectionIds) - { - return ClientProxyFactory.CreateMultipleClientProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - connectionIds); - } - - public IClientProxy Group(string groupName) - { - return ClientProxyFactory.CreateSingleGroupProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - groupName); - } - - public IClientProxy Groups(IReadOnlyList groupNames) - { - return ClientProxyFactory.CreateMultipleGroupProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - groupNames); - } - - public IClientProxy GroupExcept(string groupName, IReadOnlyList excludeIds) - { - return ClientProxyFactory.CreateSingleGroupExceptProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - groupName, excludeIds); - } - - public IClientProxy User(string userId) - { - return ClientProxyFactory.CreateSingleUserProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - userId); - } - - public IClientProxy Users(IReadOnlyList userIds) - { - return ClientProxyFactory.CreateMultipleUserProxy(_endpoint, _options.ApiVersion, _accessKey, _hubName, - userIds); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/HubProxy.cs b/src/Microsoft.Azure.SignalR/HubProxy/HubProxy.cs deleted file mode 100644 index 8e1aeb9d5..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/HubProxy.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using Microsoft.AspNetCore.SignalR; - -namespace Microsoft.Azure.SignalR -{ - public class HubProxy - { - public HubProxy(string endpoint, string accessKey, string hubName) : this(endpoint, accessKey, hubName, null) - { - } - - public HubProxy(string endpoint, string accessKey, string hubName, HubProxyOptions options) - { - if (string.IsNullOrEmpty(endpoint)) - { - throw new ArgumentNullException(nameof(endpoint)); - } - - if (string.IsNullOrEmpty(accessKey)) - { - throw new ArgumentNullException(nameof(accessKey)); - } - - if (string.IsNullOrEmpty(hubName)) - { - throw new ArgumentNullException(nameof(hubName)); - } - - Clients = new HubClientsProxy(endpoint, accessKey, hubName, options); - Groups = new GroupManagerProxy(endpoint, accessKey, hubName, options); - - HubName = hubName.ToLower(); - } - - public string HubName { get; } - - public IHubClients Clients { get; } - - public IGroupManager Groups { get; } - } -} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/HubProxyOptions.cs b/src/Microsoft.Azure.SignalR/HubProxy/HubProxyOptions.cs deleted file mode 100644 index b98ab29f7..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/HubProxyOptions.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -namespace Microsoft.Azure.SignalR -{ - public class HubProxyOptions - { - public const string DefaultApiVersion = "v1-preview"; - public static HubProxyOptions DefaultHubProxyOptions = new HubProxyOptions(); - - public string ApiVersion { get; set; } = DefaultApiVersion; - } -} diff --git a/src/Microsoft.Azure.SignalR/HubProxy/IClientProxy.cs b/src/Microsoft.Azure.SignalR/HubProxy/IClientProxy.cs deleted file mode 100644 index d73a40d5d..000000000 --- a/src/Microsoft.Azure.SignalR/HubProxy/IClientProxy.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Net.Http; -using System.Threading.Tasks; - -namespace Microsoft.Azure.SignalR -{ - public interface IClientProxy - { - /// - /// Invokes a method on the connection(s) in Azure SignalR represented by the instance. - /// - /// name of the method to invoke - /// arguments to pass to the client - /// A task that represents when the data has been sent to the client in SignalR service. - Task SendAsync(string method, object[] args); - } -} diff --git a/src/Microsoft.Azure.SignalR/Internal/AsyncEnumeratorAdapters.cs b/src/Microsoft.Azure.SignalR/Internal/AsyncEnumeratorAdapters.cs deleted file mode 100644 index aded88e4c..000000000 --- a/src/Microsoft.Azure.SignalR/Internal/AsyncEnumeratorAdapters.cs +++ /dev/null @@ -1,193 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Linq; -using System.Reflection; -using System.Threading; -using System.Threading.Tasks; -using System.Threading.Channels; - -namespace Microsoft.Azure.SignalR.Internal -{ - // True-internal because this is a weird and tricky class to use :) - internal static class AsyncEnumeratorAdapters - { - private static readonly MethodInfo _boxEnumeratorMethod = typeof(AsyncEnumeratorAdapters) - .GetRuntimeMethods() - .Single(m => m.Name.Equals(nameof(BoxEnumerator)) && m.IsGenericMethod); - - private static readonly MethodInfo _fromObservableMethod = typeof(AsyncEnumeratorAdapters) - .GetRuntimeMethods() - .Single(m => m.Name.Equals(nameof(FromObservable)) && m.IsGenericMethod); - - private static readonly MethodInfo _getAsyncEnumeratorMethod = typeof(AsyncEnumeratorAdapters) - .GetRuntimeMethods() - .Single(m => m.Name.Equals(nameof(GetAsyncEnumerator)) && m.IsGenericMethod); - - public static IAsyncEnumerator FromObservable(object observable, Type observableInterface, CancellationToken cancellationToken) - { - // TODO: Cache expressions by observable.GetType()? - return (IAsyncEnumerator)_fromObservableMethod - .MakeGenericMethod(observableInterface.GetGenericArguments()) - .Invoke(null, new[] { observable, cancellationToken }); - } - - public static IAsyncEnumerator FromObservable(IObservable observable, CancellationToken cancellationToken) - { - // TODO: Allow bounding and optimizations? - var channel = Channel.CreateUnbounded(); - - var subscription = observable.Subscribe(new ChannelObserver(channel.Writer, cancellationToken)); - - // Dispose the subscription when the token is cancelled - cancellationToken.Register(state => ((IDisposable)state).Dispose(), subscription); - - return GetAsyncEnumerator(channel.Reader, cancellationToken); - } - - public static IAsyncEnumerator FromChannel(object readableChannelOfT, Type payloadType, CancellationToken cancellationToken) - { - var enumerator = _getAsyncEnumeratorMethod - .MakeGenericMethod(payloadType) - .Invoke(null, new object[] { readableChannelOfT, cancellationToken }); - - if (payloadType.IsValueType) - { - return (IAsyncEnumerator)_boxEnumeratorMethod - .MakeGenericMethod(payloadType) - .Invoke(null, new[] { enumerator }); - } - else - { - return (IAsyncEnumerator)enumerator; - } - } - - private static IAsyncEnumerator BoxEnumerator(IAsyncEnumerator input) where T : struct - { - return new BoxingEnumerator(input); - } - - private class ChannelObserver : IObserver - { - private ChannelWriter _output; - private CancellationToken _cancellationToken; - - public ChannelObserver(ChannelWriter output, CancellationToken cancellationToken) - { - _output = output; - _cancellationToken = cancellationToken; - } - - public void OnCompleted() - { - _output.TryComplete(); - } - - public void OnError(Exception error) - { - _output.TryComplete(error); - } - - public void OnNext(T value) - { - if (_cancellationToken.IsCancellationRequested) - { - // Noop, someone else is handling the cancellation - return; - } - - // This will block the thread emitting the object if the channel is bounded and full - // I think this is OK, since we want to push the backpressure up. However, we may need - // to find a way to force the entire subscription off to a dedicated thread in order to - // ensure we don't block other tasks - - // Right now however, we use unbounded channels, so all of the above is moot because TryWrite will always succeed - while (!_output.TryWrite(value)) - { - // Wait for a spot - if (!_output.WaitToWriteAsync(_cancellationToken).Result) - { - // Channel was closed. - throw new InvalidOperationException("Output channel was closed"); - } - } - } - } - - private class BoxingEnumerator : IAsyncEnumerator where T : struct - { - private IAsyncEnumerator _input; - - public BoxingEnumerator(IAsyncEnumerator input) - { - _input = input; - } - - public object Current => _input.Current; - public Task MoveNextAsync() => _input.MoveNextAsync(); - } - - public static IAsyncEnumerator GetAsyncEnumerator(ChannelReader channel, CancellationToken cancellationToken = default(CancellationToken)) - { - return new AsyncEnumerator(channel, cancellationToken); - } - - /// Provides an async enumerator for the data in a channel. - internal class AsyncEnumerator : IAsyncEnumerator - { - /// The channel being enumerated. - private readonly ChannelReader _channel; - /// Cancellation token used to cancel the enumeration. - private readonly CancellationToken _cancellationToken; - /// The current element of the enumeration. - private T _current; - - internal AsyncEnumerator(ChannelReader channel, CancellationToken cancellationToken) - { - _channel = channel; - _cancellationToken = cancellationToken; - } - - public T Current => _current; - - public Task MoveNextAsync() - { - ValueTask result = _channel.ReadAsync(_cancellationToken); - - if (result.IsCompletedSuccessfully) - { - _current = result.Result; - return Task.FromResult(true); - } - - return result.AsTask().ContinueWith((t, s) => - { - var thisRef = (AsyncEnumerator)s; - if (t.IsFaulted && t.Exception.InnerException is ChannelClosedException cce && cce.InnerException == null) - { - return false; - } - thisRef._current = t.GetAwaiter().GetResult(); - return true; - }, this, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.NotOnCanceled, TaskScheduler.Default); - } - } - } - - /// Represents an enumerator accessed asynchronously. - /// Specifies the type of the data enumerated. - internal interface IAsyncEnumerator - { - /// Asynchronously move the enumerator to the next element. - /// - /// A task that returns true if the enumerator was successfully advanced to the next item, - /// or false if no more data was available in the collection. - /// - Task MoveNextAsync(); - - /// Gets the current element being enumerated. - T Current { get; } - } -} diff --git a/src/Microsoft.Azure.SignalR/Internal/AzureSignalRMarkerService.cs b/src/Microsoft.Azure.SignalR/Internal/AzureSignalRMarkerService.cs new file mode 100644 index 000000000..6d6438cb8 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/Internal/AzureSignalRMarkerService.cs @@ -0,0 +1,10 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.Azure.SignalR +{ + internal class AzureSignalRMarkerService + { + internal bool IsConfigured { set; get; } + } +} diff --git a/src/Microsoft.Azure.SignalR/Internal/ProductInfo.cs b/src/Microsoft.Azure.SignalR/Internal/ProductInfo.cs new file mode 100644 index 000000000..f30c9b73a --- /dev/null +++ b/src/Microsoft.Azure.SignalR/Internal/ProductInfo.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Reflection; +using System.Runtime.InteropServices; + +namespace Microsoft.Azure.SignalR +{ + internal static class ProductInfo + { + public static string GetProductInfo() + { + var assembly = typeof(ProductInfo).GetTypeInfo().Assembly; + var packageId = assembly.GetName().Name; + var version = assembly.GetCustomAttribute().InformationalVersion; + var runtime = RuntimeInformation.FrameworkDescription?.Trim(); + var operatingSystem = RuntimeInformation.OSDescription?.Trim(); + var processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().Trim(); + + return $"{packageId}/{version} ({runtime}; {operatingSystem}; {processorArchitecture})"; + } + } +} diff --git a/src/Microsoft.Azure.SignalR/Microsoft.Azure.SignalR.csproj b/src/Microsoft.Azure.SignalR/Microsoft.Azure.SignalR.csproj index b91dd3370..c4bf11f98 100644 --- a/src/Microsoft.Azure.SignalR/Microsoft.Azure.SignalR.csproj +++ b/src/Microsoft.Azure.SignalR/Microsoft.Azure.SignalR.csproj @@ -3,15 +3,35 @@ .NET Standard SDK for Azure SignalR. netstandard2.0 Microsoft.Azure.SignalR + true + true - + + + + + + + + + + + - - - - + + + + + $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage + + + + + + + diff --git a/src/Microsoft.Azure.SignalR/NegotiateHandler.cs b/src/Microsoft.Azure.SignalR/NegotiateHandler.cs new file mode 100644 index 000000000..5e69735ce --- /dev/null +++ b/src/Microsoft.Azure.SignalR/NegotiateHandler.cs @@ -0,0 +1,67 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Options; + +namespace Microsoft.Azure.SignalR +{ + internal class NegotiateHandler + { + private readonly IServiceEndpointProvider _endpointProvider; + private readonly IUserIdProvider _userIdProvider; + private readonly Func> _claimsProvider; + + public NegotiateHandler(IServiceEndpointProvider endpointProvider, IUserIdProvider userIdProvider, IOptions options) + { + _endpointProvider = endpointProvider ?? throw new ArgumentNullException(nameof(endpointProvider)); + _userIdProvider = userIdProvider ?? throw new ArgumentNullException(nameof(userIdProvider)); + _claimsProvider = options?.Value?.ClaimsProvider; + } + + public NegotiationResponse Process(HttpContext context, string hubName) + { + var claims = BuildClaims(context); + var request = context.Request; + var originalPath = GetOriginalPath(request.Path); + return new NegotiationResponse + { + Url = _endpointProvider.GetClientEndpoint(hubName, originalPath, + request.QueryString.HasValue ? request.QueryString.Value.Substring(1) : string.Empty), + AccessToken = _endpointProvider.GenerateClientAccessToken(hubName, claims), + // Need to set this even though it's technically protocol violation https://github.com/aspnet/SignalR/issues/2133 + AvailableTransports = new List() + }; + } + + private IEnumerable BuildClaims(HttpContext context) + { + var userId = _userIdProvider.GetUserId(new ServiceHubConnectionContext(context)); + if (userId != null) + { + yield return new Claim(Constants.ClaimType.UserId, userId); + } + + var claims = _claimsProvider == null ? context.User.Claims : _claimsProvider.Invoke(context); + if (claims == null) yield break; + + foreach (var claim in claims) + { + yield return claim; + } + } + + private static string GetOriginalPath(string path) + { + path = path.TrimEnd('/'); + return path.EndsWith(Constants.Path.Negotiate) + ? path.Substring(0, path.Length - Constants.Path.Negotiate.Length) + : string.Empty; + } + } +} diff --git a/src/Microsoft.Azure.SignalR/Properties/AssemblyInfo.cs b/src/Microsoft.Azure.SignalR/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..a1dfbbe62 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Microsoft.Azure.SignalR.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] + diff --git a/src/Microsoft.Azure.SignalR/ServiceHubConnectionContext.cs b/src/Microsoft.Azure.SignalR/ServiceHubConnectionContext.cs new file mode 100644 index 000000000..4297c20bc --- /dev/null +++ b/src/Microsoft.Azure.SignalR/ServiceHubConnectionContext.cs @@ -0,0 +1,73 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO.Pipelines; +using System.Security.Claims; +using System.Threading; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Connections.Features; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Connections.Features; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.SignalR; +using Microsoft.AspNetCore.SignalR.Protocol; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Microsoft.Azure.SignalR +{ + /// + /// ServiceHubConnectionContext is only passed to IUserIdProvider.GetUserId as a parameter. + /// Only HttpContext and User properties are available for access. + /// Exception will be thrown when other properties are accessed. + /// + internal class ServiceHubConnectionContext : HubConnectionContext + { + public const string UnavailableErrorTemplate = " property is not available in context of Azure SignalR Service."; + public const string ConnectionAbortedUnavailableError = nameof(ConnectionAborted) + UnavailableErrorTemplate; + public const string ConnectionIdUnavailableError = nameof(ConnectionId) + UnavailableErrorTemplate; + public const string ItemsUnavailableError = nameof(Items) + UnavailableErrorTemplate; + public const string ProtocolUnavailableError = nameof(Protocol) + UnavailableErrorTemplate; + + public ServiceHubConnectionContext(HttpContext context) + : base(new DummyConnectionContext(context), TimeSpan.MaxValue, NullLoggerFactory.Instance) + { + } + + public override CancellationToken ConnectionAborted => throw new InvalidOperationException(ConnectionAbortedUnavailableError); + + public override string ConnectionId => throw new InvalidOperationException(ConnectionIdUnavailableError); + + public override IDictionary Items => throw new InvalidOperationException(ItemsUnavailableError); + + public override IHubProtocol Protocol => throw new InvalidOperationException(ProtocolUnavailableError); + + private class DummyConnectionContext : ConnectionContext, + IHttpContextFeature, + IConnectionUserFeature + { + public DummyConnectionContext(HttpContext context) + { + HttpContext = context; + User = context?.User; + + Features = new FeatureCollection(); + Features.Set(this); + Features.Set(this); + } + + public override string ConnectionId { get; set; } + + public override IFeatureCollection Features { get; } + + public override IDictionary Items { get; set; } + + public override IDuplexPipe Transport { get; set; } + + public HttpContext HttpContext { get; set; } + + public ClaimsPrincipal User { get; set; } + } + } +} diff --git a/src/Microsoft.Azure.SignalR/ServiceOptions.cs b/src/Microsoft.Azure.SignalR/ServiceOptions.cs new file mode 100644 index 000000000..4c17a094d --- /dev/null +++ b/src/Microsoft.Azure.SignalR/ServiceOptions.cs @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.AspNetCore.Http; + +namespace Microsoft.Azure.SignalR +{ + /// + /// Configurable options when using Azure SignalR Service. + /// + public class ServiceOptions + { + /// + /// The key which will be used to read connection string from environment variables. + /// + public static readonly string ConnectionStringDefaultKey = "Azure:SignalR:ConnectionString"; + + // Default access token lifetime + internal static readonly TimeSpan DefaultAccessTokenLifetime = TimeSpan.FromHours(1); + + /// + /// Gets or sets the connection string of Azure SignalR Service instance. + /// + public string ConnectionString { get; set; } = null; + + /// + /// Gets or sets the total number of connections from SDK to Azure SignalR Service. Default value is 5. + /// + public int ConnectionCount { get; set; } = 5; + + /// + /// Gets or sets the func to generate claims from . + /// The claims will be included in the auto-generated token for clients. + /// + public Func> ClaimsProvider { get; set; } = null; + + /// + /// Gets or sets the lifetime of auto-generated access token, which will be used to authenticate with Azure SignalR Service. + /// Default value is one hour. + /// + public TimeSpan AccessTokenLifetime { get; set; } = DefaultAccessTokenLifetime; + } +} diff --git a/src/Microsoft.Azure.SignalR/ServiceRouteBuilder.cs b/src/Microsoft.Azure.SignalR/ServiceRouteBuilder.cs new file mode 100644 index 000000000..326e043b5 --- /dev/null +++ b/src/Microsoft.Azure.SignalR/ServiceRouteBuilder.cs @@ -0,0 +1,99 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.Azure.SignalR +{ + /// + /// Maps incoming requests to types. + /// + public class ServiceRouteBuilder + { + private readonly IServiceProvider _serviceProvider; + private readonly RouteBuilder _routes; + private readonly NegotiateHandler _negotiateHandler; + + /// + /// Initializes a new instance of the class. + /// + /// The routes builder. + public ServiceRouteBuilder(RouteBuilder routes) + { + _routes = routes; + _serviceProvider = _routes.ServiceProvider; + _negotiateHandler = _serviceProvider.GetRequiredService(); + } + + /// + /// Maps incoming requests with the specified path to the specified type. + /// + /// The type to map requests to. + /// The request path. + public void MapHub(string path) where THub : Hub + => MapHub(new PathString(path)); + + /// + /// Maps incoming requests with the specified path to the specified type. + /// + /// The type to map requests to. + /// The request path. + public void MapHub(PathString path) where THub: Hub + { + // find auth attributes + var authorizeAttributes = typeof(THub).GetCustomAttributes(inherit: true); + var authorizationData = new List(); + foreach (var attribute in authorizeAttributes) + { + authorizationData.Add(attribute); + } + _routes.MapRoute(path + Constants.Path.Negotiate, c => RedirectToService(c, typeof(THub).Name, authorizationData)); + + Start(); + } + + private async Task RedirectToService(HttpContext context, string hubName, IList authorizationData) + { + if (!await AuthorizeHelper.AuthorizeAsync(context, authorizationData)) + { + return; + } + + var negotiateResponse = _negotiateHandler.Process(context, hubName); + + var writer = new MemoryBufferWriter(); + try + { + context.Response.ContentType = "application/json"; + NegotiateProtocol.WriteResponse(negotiateResponse, writer); + // Write it out to the response with the right content length + context.Response.ContentLength = writer.Length; + await writer.CopyToAsync(context.Response.Body); + } + finally + { + writer.Reset(); + } + } + + private void Start() where THub : Hub + { + var app = new ConnectionBuilder(_serviceProvider) + .UseHub() + .Build(); + + var dispatcher = _serviceProvider.GetRequiredService>(); + dispatcher.Start(app); + } + } +} diff --git a/src/Microsoft.Azure.SignalR/TokenProvider.cs b/src/Microsoft.Azure.SignalR/TokenProvider.cs deleted file mode 100644 index c2c1320c7..000000000 --- a/src/Microsoft.Azure.SignalR/TokenProvider.cs +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Security.Claims; -using Microsoft.AspNetCore.SignalR; - -namespace Microsoft.Azure.SignalR -{ - public class TokenProvider - { - public static readonly TimeSpan DefaultAccessTokenLifetime = TimeSpan.FromSeconds(30); - - private readonly EndpointProvider _endpointProvider; - private readonly string _accessKey; - private TimeSpan? _lifetime; - - public DateTime ExpireTime => DateTime.UtcNow.Add(TokenLifetime); - - public TimeSpan TokenLifetime { - get => _lifetime ?? DefaultAccessTokenLifetime; - set => _lifetime = value; - } - - public TokenProvider(string endpoint, string accessKey) - : this(new EndpointProvider(endpoint), accessKey, null) - { - } - - public TokenProvider(string endpoint, string accessKey, TimeSpan lifetime) - : this(new EndpointProvider(endpoint), accessKey, lifetime) - { - } - - public TokenProvider(EndpointProvider endpointProvider, string accessKey) - : this(endpointProvider, accessKey, null) - { - } - - public TokenProvider(EndpointProvider endpointProvider, string accessKey, TimeSpan? lifetime) - { - if (string.IsNullOrEmpty(accessKey)) - { - throw new ArgumentNullException(nameof(accessKey)); - } - - _endpointProvider = endpointProvider ?? throw new ArgumentNullException(nameof(endpointProvider)); - _accessKey = accessKey; - _lifetime = lifetime; - } - - public string GenerateClientAccessToken(IEnumerable claims = null, TimeSpan? lifetime = null) - where THub : Hub - { - return GenerateClientAccessToken(typeof(THub).Name, claims, lifetime); - } - - public string GenerateClientAccessToken(string hubName, IEnumerable claims = null, - TimeSpan? lifetime = null) - { - var expire = lifetime.HasValue ? DateTime.UtcNow.Add(lifetime.Value) : ExpireTime; - return AuthenticationHelper.GenerateJwtBearer( - audience: _endpointProvider.GetClientEndpoint(hubName), - claims: claims, - expires: expire, - signingKey: _accessKey - ); - } - - public string GenerateServerAccessToken(TimeSpan? lifetime = null) where THub : Hub - { - return GenerateServerAccessToken(typeof(THub).Name, lifetime); - } - - public string GenerateServerAccessToken(string hubName, TimeSpan? lifetime = null) - { - var expire = lifetime.HasValue ? DateTime.UtcNow.Add(lifetime.Value) : ExpireTime; - return AuthenticationHelper.GenerateJwtBearer( - audience: _endpointProvider.GetServerEndpoint(hubName), - claims: null, - expires: expire, - signingKey: _accessKey - ); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/Utilities/AuthenticationHelper.cs b/src/Microsoft.Azure.SignalR/Utilities/AuthenticationHelper.cs index 18640f998..c68dd3231 100644 --- a/src/Microsoft.Azure.SignalR/Utilities/AuthenticationHelper.cs +++ b/src/Microsoft.Azure.SignalR/Utilities/AuthenticationHelper.cs @@ -10,7 +10,7 @@ namespace Microsoft.Azure.SignalR { - public class AuthenticationHelper + internal static class AuthenticationHelper { private static readonly JwtSecurityTokenHandler JwtTokenHandler = new JwtSecurityTokenHandler(); diff --git a/src/Microsoft.Azure.SignalR/Utilities/AuthorizeHelper.cs b/src/Microsoft.Azure.SignalR/Utilities/AuthorizeHelper.cs new file mode 100644 index 000000000..422a401eb --- /dev/null +++ b/src/Microsoft.Azure.SignalR/Utilities/AuthorizeHelper.cs @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization.Policy; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.Azure.SignalR +{ + internal static class AuthorizeHelper + { + public static async Task AuthorizeAsync(HttpContext context, IList policies) + { + if (policies.Count == 0) + { + return true; + } + + var policyProvider = context.RequestServices.GetRequiredService(); + + var authorizePolicy = await AuthorizationPolicy.CombineAsync(policyProvider, policies); + + var policyEvaluator = context.RequestServices.GetRequiredService(); + + // This will set context.User if required + var authenticateResult = await policyEvaluator.AuthenticateAsync(authorizePolicy, context); + + var authorizeResult = await policyEvaluator.AuthorizeAsync(authorizePolicy, authenticateResult, context, resource: null); + if (authorizeResult.Succeeded) + { + return true; + } + else if (authorizeResult.Challenged) + { + if (authorizePolicy.AuthenticationSchemes.Count > 0) + { + foreach (var scheme in authorizePolicy.AuthenticationSchemes) + { + await context.ChallengeAsync(scheme); + } + } + else + { + await context.ChallengeAsync(); + } + return false; + } + else if (authorizeResult.Forbidden) + { + if (authorizePolicy.AuthenticationSchemes.Count > 0) + { + foreach (var scheme in authorizePolicy.AuthenticationSchemes) + { + await context.ForbidAsync(scheme); + } + } + else + { + await context.ForbidAsync(); + } + return false; + } + return false; + } + } +} diff --git a/src/Microsoft.Azure.SignalR/Utilities/HubConnectionContextExtensions.cs b/src/Microsoft.Azure.SignalR/Utilities/HubConnectionContextExtensions.cs deleted file mode 100644 index 1a292f7f5..000000000 --- a/src/Microsoft.Azure.SignalR/Utilities/HubConnectionContextExtensions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.AspNetCore.SignalR; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; - -namespace Microsoft.Azure.SignalR -{ - public static class HubConnectionContextExtensions - { - public static Task ReturnResultAsync(this HubConnectionContext connection, HubInvocationMessage message) - { - if (message == null) return Task.CompletedTask; - message.AddAction(nameof(HubLifetimeManager.SendConnectionAsync)) - .AddConnectionId(connection.ConnectionId); - return connection.WriteAsync(message); - } - } -} diff --git a/src/Microsoft.Azure.SignalR/Utilities/HubInvocationMessageExtensions.cs b/src/Microsoft.Azure.SignalR/Utilities/HubInvocationMessageExtensions.cs deleted file mode 100644 index f5ee4f987..000000000 --- a/src/Microsoft.Azure.SignalR/Utilities/HubInvocationMessageExtensions.cs +++ /dev/null @@ -1,140 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System.Collections.Generic; -using System.Linq; -using System.Security.Claims; -using Microsoft.AspNetCore.SignalR.Internal.Protocol; -using Newtonsoft.Json; - -namespace Microsoft.Azure.SignalR -{ - public static class HubInvocationMessageExtension - { - private const string Separator = ","; - internal const string ActionKeyName = "action"; - internal const string ConnectionIdKeyName = "connId"; - internal const string ConnectionIdsKeyName = "connIds"; - internal const string UserKeyName = "user"; - internal const string UsersKeyName = "users"; - internal const string GroupNameKeyName = "group"; - internal const string GroupNamesKeyName = "groups"; - internal const string ExcludedIdsKeyName = "excluded"; - internal const string ClaimsKeyName = "claims"; - - public static TMessage AddHeader(this TMessage message, string key, string value) - where TMessage : HubInvocationMessage - { - if (message != null && !string.IsNullOrEmpty(key)) - { - message.Headers.Add(key, value); - } - return message; - } - - public static bool TryGetHeader(this TMessage message, string headerName, out string headerValue) - where TMessage : HubInvocationMessage - { - if (message.Headers != null && - message.Headers.TryGetValue(headerName, out headerValue)) - { - return true; - } - headerValue = null; - return false; - } - - public static TMessage AddConnectionId(this TMessage message, string connectionId) - where TMessage : HubInvocationMessage - { - return string.IsNullOrEmpty(connectionId) ? message : message.AddHeader(ConnectionIdKeyName, connectionId); - } - - public static string GetConnectionId(this TMessage message) where TMessage : HubInvocationMessage - { - message.Headers.TryGetValue(ConnectionIdKeyName, out var connectionId); - return connectionId; - } - - public static TMessage AddConnectionIds(this TMessage message, IReadOnlyList connectionIds) - where TMessage : HubInvocationMessage - { - return message.AddHeader(ConnectionIdsKeyName, string.Join(Separator, connectionIds)); - } - - public static bool TryGetConnectionId(this TMessage message, out string connectionId) - where TMessage : HubInvocationMessage - { - return message.TryGetHeader(ConnectionIdKeyName, out connectionId); - } - - public static TMessage AddUser(this TMessage message, string userId) - where TMessage : HubInvocationMessage - { - return string.IsNullOrEmpty(userId) ? message : message.AddHeader(UserKeyName, userId); - } - - public static TMessage AddUsers(this TMessage message, IReadOnlyList userIds) - where TMessage : HubInvocationMessage - { - return userIds != null && userIds.Any() ? message.AddHeader(UsersKeyName, string.Join(Separator, userIds)) : message; - } - - public static TMessage AddGroupName(this TMessage message, string groupName) - where TMessage : HubInvocationMessage - { - return string.IsNullOrEmpty(groupName) ? message : message.AddHeader(GroupNameKeyName, groupName); - } - - public static TMessage AddGroupNames(this TMessage message, IReadOnlyList groupNames) - where TMessage : HubInvocationMessage - { - return groupNames != null && groupNames.Any() ? message.AddHeader(GroupNamesKeyName, string.Join(Separator, groupNames)) : message; - } - - public static TMessage AddExcludedIds(this TMessage message, IReadOnlyList excludedIds) - where TMessage : HubInvocationMessage - { - return excludedIds != null && excludedIds.Any() ? message.AddHeader(ExcludedIdsKeyName, string.Join(Separator, excludedIds)) : message; - } - - public static TMessage AddAction(this TMessage message, string actionName) - where TMessage : HubInvocationMessage - { - return string.IsNullOrEmpty(actionName) ? message : message.AddHeader(ActionKeyName, actionName); - } - - public static bool TryGetClaims(this TMessage message, out IEnumerable claims) - where TMessage : HubInvocationMessage - { - claims = message.TryGetHeader(ClaimsKeyName, out var serializedClaims) - ? JsonConvert.DeserializeObject>(serializedClaims).Select(x => x.ToClaim()) - : null; - - return claims != null; - } - - internal class ClaimEntry - { - [JsonProperty("t")] - public string Type { get; set; } - - [JsonProperty("v")] - public string Value { get; set; } - - public static ClaimEntry FromClaim(Claim claim) - { - return new ClaimEntry - { - Type = claim.Type, - Value = claim.Value - }; - } - - public Claim ToClaim() - { - return new Claim(Type, Value); - } - } - } -} diff --git a/src/Microsoft.Azure.SignalR/Utilities/StringHelper.cs b/src/Microsoft.Azure.SignalR/Utilities/StringHelper.cs deleted file mode 100644 index 81a5fd8fe..000000000 --- a/src/Microsoft.Azure.SignalR/Utilities/StringHelper.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; - -namespace Microsoft.Azure.SignalR -{ - internal static class StringHelper - { - internal static bool IgnoreCaseEquals(this string left, string right) - { - return left.Equals(right, StringComparison.OrdinalIgnoreCase); - } - } -} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/ClientConnectionManagerTests.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/ClientConnectionManagerTests.cs new file mode 100644 index 000000000..f472ae25b --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/ClientConnectionManagerTests.cs @@ -0,0 +1,80 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Claims; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Primitives; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class ClientConnectionManagerTests + { + private readonly ClientConnectionManager _clientConnectionManager; + + public ClientConnectionManagerTests() + { + var hubConfig = new HubConfiguration(); + var protectedData = new EmptyProtectedData(); + var transport = new AzureTransportManager(hubConfig.Resolver); + hubConfig.Resolver.Register(typeof(IProtectedData), () => protectedData); + hubConfig.Resolver.Register(typeof(ITransportManager), () => transport); + + _clientConnectionManager = new ClientConnectionManager(hubConfig); + } + + [Theory] + [InlineData("?transport=webSockets")] + [InlineData("?connectionToken=invalidOne")] + public void TestCreateConnectionWithCustomQueryStringSucceeds(string queryString) + { + var message = new OpenConnectionMessage(Guid.NewGuid().ToString("N"), new Claim[0], null, queryString); + var connection = _clientConnectionManager.CreateConnection(message, null); + } + + [Theory] + [InlineData("?connectionToken=anotherone")] + [InlineData("")] + [InlineData("transport=webSockets")] + public void TestGetHostContext(string queryString) + { + var message = new OpenConnectionMessage(Guid.NewGuid().ToString("N"), + new Claim[] { + new Claim(ClaimTypes.Name, "user1") + }, + new Dictionary + { + ["custom1"] = "value1" + } + , queryString); + var response = new MemoryStream(); + var context = _clientConnectionManager.GetHostContext(message, response, null); + Assert.Equal(200, context.Response.StatusCode); + Assert.Equal("", ClientConnectionManager.GetContentAndDispose(response)); + Assert.Equal("value1", context.Request.Headers["custom1"]); + Assert.Equal($"{message.ConnectionId}:user1", context.Request.QueryString["connectionToken"]); + } + + [Theory] + [InlineData("Bearer", null, 2, "custom", "custom2")] + [InlineData("Bearer", null, 2, "aud", "custom", "custom2")] + [InlineData("Bearer", null, 1, "aud", "exp", "iat", "nbf", "custom")] + [InlineData("Bearer", ClaimTypes.Name, 2, "aud", "exp", "iat", "nbf", "custom", ClaimTypes.Name)] + [InlineData(Constants.ClaimType.AuthenticationType, ClaimTypes.Name, 1, "aud", "exp", "iat", "nbf", Constants.ClaimType.AuthenticationType, ClaimTypes.Name)] + public void TestGetUserPrincipal(string expectedAuthenticationType, string expectedUserName, int expectedUserClaimCount, params string[] claims) + { + var message = new OpenConnectionMessage(Guid.NewGuid().ToString("N"), claims.Select(s => new Claim(s, s)).ToArray()); + var principal = ClientConnectionManager.GetUserPrincipal(message); + Assert.Equal(expectedAuthenticationType, principal.Identity.AuthenticationType); + Assert.Equal(expectedUserName, principal.Identity.Name); + Assert.Equal(expectedUserClaimCount, principal.Claims.Count()); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogRecord.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogRecord.cs new file mode 100644 index 000000000..5fe7c0f94 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogRecord.cs @@ -0,0 +1,21 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using Microsoft.Extensions.Logging.Testing; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + // WriteContext, but with a timestamp... + internal class LogRecord + { + public DateTime Timestamp { get; } + public WriteContext Write { get; } + + public LogRecord(DateTime timestamp, WriteContext write) + { + Timestamp = timestamp; + Write = write; + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogSinkProvider.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogSinkProvider.cs new file mode 100644 index 000000000..f5bf1de8d --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/LogSinkProvider.cs @@ -0,0 +1,77 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + // TestSink does not have an event + internal class LogSinkProvider : ILoggerProvider + { + private readonly ConcurrentQueue _logs = new ConcurrentQueue(); + + public event Action RecordLogged; + + public ILogger CreateLogger(string categoryName) + { + return new LogSinkLogger(categoryName, this); + } + + public void Dispose() + { + } + + public IList GetLogs() => _logs.ToList(); + + public void Log(string categoryName, LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + var record = new LogRecord( + DateTime.Now, + new WriteContext + { + LoggerName = categoryName, + LogLevel = logLevel, + EventId = eventId, + State = state, + Exception = exception, + Formatter = (o, e) => formatter((TState)o, e), + }); + _logs.Enqueue(record); + + RecordLogged?.Invoke(record); + } + + private class LogSinkLogger : ILogger + { + private readonly string _categoryName; + private readonly LogSinkProvider _logSinkProvider; + + public LogSinkLogger(string categoryName, LogSinkProvider logSinkProvider) + { + _categoryName = categoryName; + _logSinkProvider = logSinkProvider; + } + + public IDisposable BeginScope(TState state) + { + return null; + } + + public bool IsEnabled(LogLevel logLevel) + { + return true; + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + _logSinkProvider.Log(_categoryName, logLevel, eventId, state, exception, formatter); + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/ServiceConnectionProxy.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/ServiceConnectionProxy.cs new file mode 100644 index 000000000..eda9595b1 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/ServiceConnectionProxy.cs @@ -0,0 +1,186 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO.Pipelines; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + internal class ServiceConnectionProxy : ServiceConnection, IDisposable + { + private static readonly ServiceProtocol ServiceProtocol = new ServiceProtocol(); + + private readonly ConcurrentDictionary> _waitForConnectionOpen = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> _waitForConnectionClose = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> _waitForApplicationMessage = new ConcurrentDictionary>(); + + private TestConnectionContext _connectionContext; + + public ServiceConnectionProxy(IClientConnectionManager clientConnectionManager, ILoggerFactory loggerFactory, ConnectionDelegate callback = null, PipeOptions clientPipeOptions = null) : + base( + Guid.NewGuid().ToString("N"), + Guid.NewGuid().ToString("N"), + ServiceProtocol, + new TestConnectionFactory(), + clientConnectionManager, + loggerFactory.CreateLogger()) + { + } + + public async Task StartServiceAsync() + { + _ = StartAsync(); + + await WaitForConnectionStart; + } + + protected override async Task CreateConnection() + { + _connectionContext = await base.CreateConnection() as TestConnectionContext; + + await WriteMessageAsync(new HandshakeResponseMessage()); + return _connectionContext; + } + + protected override async Task OnConnectedAsync(OpenConnectionMessage openConnectionMessage) + { + await base.OnConnectedAsync(openConnectionMessage); + + var tcs = _waitForConnectionOpen.GetOrAdd(openConnectionMessage.ConnectionId, i => new TaskCompletionSource()); + + tcs.TrySetResult(null); + } + + protected override async Task OnDisconnectedAsync(CloseConnectionMessage closeConnectionMessage) + { + await base.OnDisconnectedAsync(closeConnectionMessage); + var tcs = _waitForConnectionClose.GetOrAdd(closeConnectionMessage.ConnectionId, i => new TaskCompletionSource()); + + tcs.TrySetResult(null); + } + + protected override async Task OnMessageAsync(ConnectionDataMessage connectionDataMessage) + { + await base.OnMessageAsync(connectionDataMessage); + + var tcs = _waitForApplicationMessage.GetOrAdd(connectionDataMessage.ConnectionId, i => new TaskCompletionSource()); + + tcs.TrySetResult(connectionDataMessage); + } + + public Task WaitForClientConnectAsync(string connectionId) + { + var tcs = _waitForConnectionOpen.GetOrAdd(connectionId, i => new TaskCompletionSource()); + + return tcs.Task; + } + + public Task WaitForApplicationMessageAsync(string connectionId) + { + var tcs = _waitForApplicationMessage.GetOrAdd(connectionId, i => new TaskCompletionSource()); + + return tcs.Task; + } + + public Task WaitForClientDisconnectAsync(string connectionId) + { + var tcs = _waitForConnectionClose.GetOrAdd(connectionId, i => new TaskCompletionSource()); + + return tcs.Task; + } + + public async Task WriteMessageAsync(ServiceMessage message) + { + if (_connectionContext == null) + { + throw new InvalidOperationException("Server connection is not yet established."); + } + + ServiceProtocol.WriteMessage(message, _connectionContext.Application.Output); + await _connectionContext.Application.Output.FlushAsync(); + } + + public void Dispose() + { + _ = StopAsync(); + } + + private sealed class TestConnectionFactory : IConnectionFactory + { + private readonly ConnectionDelegate _connectCallback; + private TaskCompletionSource _waitForServerConnection = new TaskCompletionSource(); + + public TestConnectionFactory(ConnectionDelegate connectCallback = null) + { + _connectCallback = connectCallback ?? OnConnectionAsync; + } + + public Task GetConnectedServerAsync() + { + return _waitForServerConnection.Task; + } + + public Task ConnectAsync(TransferFormat transferFormat, string connectionId, string hubName, CancellationToken cancellationToken = default) + { + var connection = new TestConnectionContext(); + _connectCallback?.Invoke(connection); + + _waitForServerConnection.TrySetResult(connection); + return Task.FromResult(connection); + } + + public Task DisposeAsync(ConnectionContext connection) + { + return Task.CompletedTask; + } + + private Task OnConnectionAsync(ConnectionContext connection) + { + var tcs = new TaskCompletionSource(); + + // Wait for the connection to close + connection.Transport.Input.OnWriterCompleted((ex, state) => + { + tcs.TrySetResult(null); + }, + null); + + return tcs.Task; + } + } + + private sealed class TestConnectionContext : ConnectionContext + { + public TestConnectionContext() + { + Features = new FeatureCollection(); + Items = new ConcurrentDictionary(); + + var pipeOptions = new PipeOptions(); + var pair = DuplexPipe.CreateConnectionPair(pipeOptions, pipeOptions); + var proxyToApplication = DuplexPipe.CreateConnectionPair(pipeOptions, pipeOptions); + + Transport = pair.Transport; + Application = pair.Application; + } + + public override string ConnectionId { get; set; } + + public override IFeatureCollection Features { get; } + + public override IDictionary Items { get; set; } + + public override IDuplexPipe Transport { get; set; } + + public IDuplexPipe Application { get; set; } + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifiableLoggedTest.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifiableLoggedTest.cs new file mode 100644 index 000000000..af55f3d8e --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifiableLoggedTest.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; +using Xunit.Abstractions; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class VerifiableLoggedTest : LoggedTest + { + public VerifiableLoggedTest(ITestOutputHelper output) : base(output) + { + } + + public virtual IDisposable StartVerifiableLog(out ILoggerFactory loggerFactory, [CallerMemberName] string testName = null, Func expectedErrorsFilter = null) + { + var disposable = StartLog(out loggerFactory, testName); + + return new VerifyNoErrorsScope(loggerFactory, disposable, expectedErrorsFilter); + } + + public virtual IDisposable StartVerifiableLog(out ILoggerFactory loggerFactory, LogLevel minLogLevel, [CallerMemberName] string testName = null, Func expectedErrorsFilter = null) + { + var disposable = StartLog(out loggerFactory, minLogLevel, testName); + + return new VerifyNoErrorsScope(loggerFactory, disposable, expectedErrorsFilter); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifyNoErrorsScope.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifyNoErrorsScope.cs new file mode 100644 index 000000000..dc87ef1ce --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Infrastructure/VerifyNoErrorsScope.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Testing; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + internal class VerifyNoErrorsScope : IDisposable + { + private readonly IDisposable _wrappedDisposable; + private readonly Func _expectedErrorsFilter; + private readonly LogSinkProvider _sink; + + public ILoggerFactory LoggerFactory { get; } + + public VerifyNoErrorsScope(ILoggerFactory loggerFactory = null, IDisposable wrappedDisposable = null, Func expectedErrorsFilter = null) + { + _wrappedDisposable = wrappedDisposable; + _expectedErrorsFilter = expectedErrorsFilter; + _sink = new LogSinkProvider(); + + LoggerFactory = loggerFactory ?? new LoggerFactory(); + LoggerFactory.AddProvider(_sink); + } + + public void Dispose() + { + _wrappedDisposable?.Dispose(); + + var results = _sink.GetLogs().Where(w => w.Write.LogLevel >= LogLevel.Error).ToList(); + + if (_expectedErrorsFilter != null) + { + results = results.Where(w => !_expectedErrorsFilter(w.Write)).ToList(); + } + + if (results.Count > 0) + { + string errorMessage = $"{results.Count} error(s) logged."; + errorMessage += Environment.NewLine; + errorMessage += string.Join(Environment.NewLine, results.Select(record => + { + var r = record.Write; + + string lineMessage = r.LoggerName + " - " + r.EventId.ToString() + " - " + r.Formatter(r.State, r.Exception); + if (r.Exception != null) + { + lineMessage += Environment.NewLine; + lineMessage += "==================="; + lineMessage += Environment.NewLine; + lineMessage += r.Exception; + lineMessage += Environment.NewLine; + lineMessage += "==================="; + } + return lineMessage; + })); + + throw new Exception(errorMessage); + } + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/Microsoft.Azure.SignalR.AspNet.Tests.csproj b/test/Microsoft.Azure.SignalR.AspNet.Tests/Microsoft.Azure.SignalR.AspNet.Tests.csproj new file mode 100644 index 000000000..03b76d6bf --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/Microsoft.Azure.SignalR.AspNet.Tests.csproj @@ -0,0 +1,34 @@ + + + + net461 + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/RunAzureSignalRTests.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/RunAzureSignalRTests.cs new file mode 100644 index 000000000..4119f46c3 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/RunAzureSignalRTests.cs @@ -0,0 +1,250 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Configuration; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Options; +using Microsoft.Owin.Hosting; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Owin; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class RunAzureSignalRTests + { + private const string ServiceUrl = "http://localhost:8086"; + private const string ConnectionString = "Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;"; + private const string ConnectionString2 = "Endpoint=http://localhost2;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;"; + private const string AppName = "AzureSignalRTest"; + + [Fact] + public void TestRunAzureSignalRWithDefaultOptions() + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, ConnectionString, hubConfig))) + { + var resolver = hubConfig.Resolver; + var options = resolver.Resolve>(); + Assert.Equal(ConnectionString, options.Value.ConnectionString); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + Assert.IsType(resolver.Resolve()); + } + } + + [Fact] + public void TestRunAzureSignalRWithoutConnectionString() + { + var exception = Assert.Throws( + () => + { + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName))) + { + } + }); + Assert.StartsWith("No connection string was specified.", exception.Message); + } + + [Fact] + public void TestRunAzureSignalRWithConnectionString() + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, ConnectionString, hubConfig))) + { + var options = hubConfig.Resolver.Resolve>(); + Assert.Equal(ConnectionString, options.Value.ConnectionString); + } + } + + [Fact] + public void TestRunAzureSignalRWithConfig() + { + // Prepare the configuration + using (new AppSettingsConfigScope(ConnectionString)) + using (new ConnectionStringConfigScope(ConnectionString2)) + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig))) + { + var options = hubConfig.Resolver.Resolve>(); + + // The one in ConnectionString wins + Assert.Equal(ConnectionString2, options.Value.ConnectionString); + } + } + } + + [Fact] + public void TestRunAzureSignalRWithAppSettings() + { + // Prepare the configuration + using (new AppSettingsConfigScope(ConnectionString)) + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig))) + { + var options = hubConfig.Resolver.Resolve>(); + + Assert.Equal(ConnectionString, options.Value.ConnectionString); + } + } + } + + [Fact] + public void TestRunAzureSignalRWithOptionsContainDefaultValue() + { + using (new ConnectionStringConfigScope(ConnectionString2)) + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig, o => + { + o.ConnectionCount = -1; + }))) + { + var options = hubConfig.Resolver.Resolve>(); + Assert.Equal(ConnectionString2, options.Value.ConnectionString); + Assert.Equal(-1, options.Value.ConnectionCount); + } + } + } + + [Fact] + public void TestRunAzureSignalRWithOptions() + { + var hubConfig = new HubConfiguration(); + using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig, o => + { + o.ConnectionString = ConnectionString; + o.ConnectionCount = -1; + }))) + { + var options = hubConfig.Resolver.Resolve>(); + Assert.Equal(ConnectionString, options.Value.ConnectionString); + Assert.Equal(-1, options.Value.ConnectionCount); + } + } + + [Theory] + [InlineData(typeof(NullUserIdProvider), null)] + [InlineData(typeof(CustomUserIdProvider), "hello")] + public async Task TestNegotiateWithRunAzureSignalR(Type providerType, string expectedUser) + { + var hubConfiguration = new HubConfiguration(); + hubConfiguration.Resolver.Register(typeof(IUserIdProvider), () => Activator.CreateInstance(providerType)); + using (WebApp.Start(ServiceUrl, a => a.RunAzureSignalR(AppName, ConnectionString, hubConfiguration))) + { + var client = new HttpClient { BaseAddress = new Uri(ServiceUrl) }; + var response = await client.GetAsync("/negotiate"); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + var message = await response.Content.ReadAsStringAsync(); + var responseObject = JsonConvert.DeserializeObject(message); + Assert.Equal("2.0", responseObject.ProtocolVersion); + Assert.Equal("http://localhost/aspnetclient", responseObject.RedirectUrl); + Assert.NotNull(responseObject.AccessToken); + var token = JwtSecurityTokenHandler.ReadJwtToken(responseObject.AccessToken); + Assert.Equal(AppName, token.Claims.FirstOrDefault(s => s.Type == Constants.ClaimType.AppName).Value); + var user = token.Claims.FirstOrDefault(s => s.Type == Constants.ClaimType.UserId)?.Value; + Assert.Equal(expectedUser, user); + } + } + + private sealed class AppSettingsConfigScope : IDisposable + { + private readonly string _originalSetting; + + public AppSettingsConfigScope(string setting) + { + _originalSetting = ConfigurationManager.AppSettings[ServiceOptions.ConnectionStringDefaultKey]; + ConfigurationManager.AppSettings[ServiceOptions.ConnectionStringDefaultKey] = setting; + } + + public void Dispose() + { + ConfigurationManager.AppSettings[ServiceOptions.ConnectionStringDefaultKey] = _originalSetting; + } + } + + private sealed class ConnectionStringConfigScope : IDisposable + { + private readonly string _originalConnectionString; + + public ConnectionStringConfigScope(string connectionString) + { + _originalConnectionString = ConfigurationManager.ConnectionStrings[ServiceOptions.ConnectionStringDefaultKey]?.ConnectionString; + SetConnectionStringConfig(connectionString); + } + + public void Dispose() + { + SetConnectionStringConfig(_originalConnectionString); + } + + private void SetConnectionStringConfig(string connectionString) + { + var settings = ConfigurationManager.ConnectionStrings; + + var element = typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); + var collection = typeof(ConfigurationElementCollection).GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); + + element.SetValue(settings, false); + collection.SetValue(settings, false); + if (connectionString == null) + { + settings.Remove(ServiceOptions.ConnectionStringDefaultKey); + } + else + { + settings.Add(new ConnectionStringSettings(ServiceOptions.ConnectionStringDefaultKey, connectionString)); + } + + collection.SetValue(settings, true); + element.SetValue(settings, true); + } + } + + private sealed class NullUserIdProvider : IUserIdProvider + { + public string GetUserId(IRequest request) + { + return null; + } + } + + private sealed class CustomUserIdProvider : IUserIdProvider + { + public string GetUserId(IRequest request) + { + return "hello"; + } + } + + private static readonly JwtSecurityTokenHandler JwtSecurityTokenHandler = new JwtSecurityTokenHandler(); + + private sealed class ResponseMessage + { + public string ProtocolVersion { get; set; } + + public string RedirectUrl { get; set; } + + public string AccessToken { get; set; } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceConnectionTests.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceConnectionTests.cs new file mode 100644 index 000000000..9598a0a98 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceConnectionTests.cs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Security.Claims; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Transports; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public partial class ServiceConnectionTests : VerifiableLoggedTest + { + private static readonly ServiceProtocol Protocol = new ServiceProtocol(); + + private readonly ClientConnectionManager _clientConnectionManager; + + public ServiceConnectionTests(ITestOutputHelper output) : base(output) + { + var hubConfig = new HubConfiguration(); + var protectedData = new EmptyProtectedData(); + var transport = new AzureTransportManager(hubConfig.Resolver); + hubConfig.Resolver.Register(typeof(IProtectedData), () => protectedData); + hubConfig.Resolver.Register(typeof(ITransportManager), () => transport); + + _clientConnectionManager = new ClientConnectionManager(hubConfig); + } + + [Fact] + public async Task ServiceConnectionDispatchTest() + { + using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug)) + { + using (var proxy = new ServiceConnectionProxy(_clientConnectionManager, loggerFactory: loggerFactory)) + { + // start the server connection + await proxy.StartServiceAsync().OrTimeout(); + + var clientConnection = Guid.NewGuid().ToString("N"); + + // Application layer sends OpenConnectionMessage + var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null, "?transport=webSockets"); + await proxy.WriteMessageAsync(new OpenConnectionMessage(clientConnection, new Claim[0])); + + await proxy.WaitForClientConnectAsync(clientConnection).OrTimeout(); + + // TODO: Check response when integrated with ServiceMessageBus + await proxy.WriteMessageAsync(new ConnectionDataMessage(clientConnection, GetPayload("Hello World"))); + + await proxy.WaitForApplicationMessageAsync(clientConnection).OrTimeout(); + + await proxy.WriteMessageAsync(new CloseConnectionMessage(clientConnection)); + + await proxy.WaitForClientDisconnectAsync(clientConnection).OrTimeout(); + } + } + } + + private ReadOnlyMemory GetPayload(string message) + { + return Protocol.GetMessageBytes(new ConnectionDataMessage(string.Empty, System.Text.Encoding.UTF8.GetBytes(message))); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceEndpointProviderTests.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceEndpointProviderTests.cs new file mode 100644 index 000000000..a1c59007c --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceEndpointProviderTests.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Text; +using Microsoft.IdentityModel.Tokens; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class ServiceEndpointProviderTests + { + private const string SigningKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + private static readonly SymmetricSecurityKey SecurityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(SigningKey)); + + [Theory] + [InlineData("Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost/aspnetclient")] + [InlineData("Endpoint=http://localhost/;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost/aspnetclient")] + [InlineData("Endpoint=https://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;", "https://localhost/aspnetclient")] + public void TestGenerateClientAccessToken(string connectionString, string expectedAudience) + { + var provider = new ServiceEndpointProvider(new ServiceOptions + { + ConnectionString = connectionString + }); + + var clientToken = provider.GenerateClientAccessToken(new Claim[] + { + new Claim("type1", "value1") + }); + + var handler = new JwtSecurityTokenHandler(); + var principal = handler.ValidateToken(clientToken, new TokenValidationParameters + { + ValidateIssuer = false, + IssuerSigningKey = SecurityKey, + ValidAudience = expectedAudience + }, out var token); + + var customClaims = principal.FindAll("type1").ToList(); + Assert.Single(customClaims); + Assert.Equal("value1", customClaims[0].Value); + } + + [Theory] + [InlineData("Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost:8080/aspnetclient")] + [InlineData("Endpoint=http://localhost/;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost:8080/aspnetclient")] + [InlineData("Endpoint=https://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;", "https://localhost/aspnetclient")] + public void TestGenerateClientEndpoint(string connectionString, string expectedEndpoint) + { + var provider = new ServiceEndpointProvider(new ServiceOptions + { + ConnectionString = connectionString + }); + + var clientEndpoint = provider.GetClientEndpoint(); + + Assert.Equal(expectedEndpoint, clientEndpoint); + } + + [Theory] + [InlineData("Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost/aspnetserver/?hub=hub1")] + [InlineData("Endpoint=http://localhost/;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost/aspnetserver/?hub=hub1")] + [InlineData("Endpoint=https://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;", "https://localhost/aspnetserver/?hub=hub1")] + public void TestGenerateServerAccessToken(string connectionString, string expectedAudience) + { + var provider = new ServiceEndpointProvider(new ServiceOptions + { + ConnectionString = connectionString + }); + + var clientToken = provider.GenerateServerAccessToken("hub1", "user1"); + + var handler = new JwtSecurityTokenHandler(); + var principal = handler.ValidateToken(clientToken, new TokenValidationParameters + { + ValidateIssuer = false, + IssuerSigningKey = SecurityKey, + ValidAudience = expectedAudience + }, out var token); + + Assert.Equal("user1", principal.FindFirst(ClaimTypes.NameIdentifier).Value); + } + + [Theory] + [InlineData("Endpoint=http://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost:8080/aspnetserver/?hub=hub1")] + [InlineData("Endpoint=http://localhost/;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Port=8080;Version=1.0", "http://localhost:8080/aspnetserver/?hub=hub1")] + [InlineData("Endpoint=https://localhost;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;", "https://localhost/aspnetserver/?hub=hub1")] + public void TestGenerateServerEndpoint(string connectionString, string expectedEndpoint) + { + var provider = new ServiceEndpointProvider(new ServiceOptions + { + ConnectionString = connectionString + }); + + var clientEndpoint = provider.GetServerEndpoint("hub1"); + + Assert.Equal(expectedEndpoint, clientEndpoint); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceMessageBusTests.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceMessageBusTests.cs new file mode 100644 index 000000000..e445b5112 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/ServiceMessageBusTests.cs @@ -0,0 +1,280 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class ServiceMessageBusTests + { + private const string AppName = nameof(ServiceMessageBusTests); + + [Fact] + public async Task PublishInvalidMessageThrows() + { + var dr = GetDefaultResolver(new string[] { }, out _); + using (var bus = new ServiceMessageBus(dr)) + { + await Assert.ThrowsAsync(() => bus.Publish("test", "key", "1")); + } + } + + [Fact] + public async Task PublishMessageToNotExistHubThrows() + { + var dr = GetDefaultResolver(new string[] { }, out _); + using (var bus = new ServiceMessageBus(dr)) + { + await Assert.ThrowsAsync(() => bus.Publish("test", "h-key", "1")); + } + } + + public static IEnumerable BroadcastTestMessages => new object[][] + { + // hub connection "c1" gets this connection message + new object[] + { + "h-c1", "hello", new string[] { "h-c1", "h", "c1" }, new string[] { "c1" } + }, + // hub connection "a.b" gets this connection message + new object[] + { + "h-a.b", "hello", new string[] { "h-c1", "h", "a.b" }, new string[] { "a.b" }, + }, + // hub connection "..." gets this connection message + new object[] + { + "h-...", "hello", new string[] { "h-c1", "h", "a.b", "...", "..", "." }, new string[] { "..." }, + } + }; + + [Theory] + [MemberData(nameof(BroadcastTestMessages))] + public async Task PublishBroadcastMessagesTest(string messageKey, string messageValue, string[] availableHubs, string[] expectedHubs) + { + var dr = GetDefaultResolver(availableHubs, out var scm); + + PrepareConnection(scm, out var result); + + using (var bus = new ServiceMessageBus(dr)) + { + await bus.Publish(SignalRMessageUtility.CreateMessage(messageKey, messageValue)); + } + + Assert.Equal(expectedHubs.Length, result.Count); + + for (var i = 0; i < expectedHubs.Length; i++) + { + Assert.True(result.TryGetValue(expectedHubs[i], out var current)); + var message = current as BroadcastDataMessage; + Assert.NotNull(message); + Assert.Equal(messageValue, message.Payloads["json"].GetSingleFramePayload()); + } + } + + public static IEnumerable ConnectionDataTestMessages => new object[][] + { + // app connection gets this connection message + new object[] + { + "hc-hub1.c1", "hello", new string[] { "hc", "hc-", "hub1", "hub1.c", "hub1.c1" }, new string[] { AppName }, new string[] { "c1" } + }, + // app connection gets this connection message + new object[] + { + "hc-hub1.bi.conn1", "hello", new string[] { "hc", "hc-hub1", "hub1", "hub1.bi", "hub1.bi.conn" }, new string[] { AppName }, new string[] { "conn1" } + } + }; + + [Theory] + [MemberData(nameof(ConnectionDataTestMessages))] + public async Task PublishConnectionDataMessagesTest(string messageKey, string messageValue, string[] availableHubs, string[] expectedHubs, string[] expectedConnectionIds) + { + var dr = GetDefaultResolver(availableHubs, out var scm); + + PrepareConnection(scm, out var result); + + using (var bus = new ServiceMessageBus(dr)) + { + await bus.Publish(SignalRMessageUtility.CreateMessage(messageKey, messageValue)); + } + + Assert.Equal(expectedHubs.Length, result.Count); + Assert.Equal(expectedConnectionIds.Length, result.Count); + + for (var i = 0; i < expectedHubs.Length; i++) + { + Assert.True(result.TryGetValue(expectedHubs[i], out var current)); + var message = current as ConnectionDataMessage; + Assert.NotNull(message); + + Assert.Equal(expectedConnectionIds[i], message.ConnectionId); + Assert.Equal(messageValue, message.Payload.First.GetSingleFramePayload()); + } + } + + public static IEnumerable GroupBroadcastTestMessages => new object[][] + { + // app connection gets this connection message + new object[] + { + // For groups, group name as a whole is considered as the group name + "hg-h1.group1", "hello", new string[] { "hg-h1", "hg", "h1" }, new string[] { AppName }, new string[] { "hg-h1.group1" } + }, + // app connection gets this connection message + new object[] + { + "hg-h1.a1.group1", "hello", new string[] { "hg-h1", "hg", "h1", "h1.a1" }, new string[] { AppName }, new string[] { "hg-h1.a1.group1" } + } + }; + + [Theory] + [MemberData(nameof(GroupBroadcastTestMessages))] + public async Task PublishGroupBroadcastDataMessagesTest(string messageKey, string messageValue, string[] availableHubs, string[] expectedHubs, string[] expectedGroups) + { + var dr = GetDefaultResolver(availableHubs, out var scm); + + PrepareConnection(scm, out var result); + + using (var bus = new ServiceMessageBus(dr)) + { + await bus.Publish(SignalRMessageUtility.CreateMessage(messageKey, messageValue)); + } + + Assert.Equal(expectedHubs.Length, result.Count); + Assert.Equal(expectedGroups.Length, result.Count); + + for (var i = 0; i < expectedHubs.Length; i++) + { + Assert.True(result.TryGetValue(expectedHubs[i], out var current)); + var message = current as GroupBroadcastDataMessage; + Assert.NotNull(message); + Assert.Equal(message.GroupName, expectedGroups[i]); + Assert.Equal(messageValue, message.Payloads["json"].GetSingleFramePayload()); + } + } + + public static IEnumerable UserDataTestMessages => new object[][] + { + // hub connection "hub1" gets this connection message + new object[] + { + "hu-hub1.user1", "hello", new string[] { "hu", "hu-", "hub1", "hub1.u", "hub1.user1" }, new string[] { "hub1" }, new string[] { "user1" } + }, + // hub connection "hub1" & "hub1.bi" gets this connection message + new object[] + { + "hu-hub1.bi.user1", "hello", new string[] { "hu", "hu-hub1", "hub1", "hub1.bi", "hub1.bi.user" }, new string[] { "hub1", "hub1.bi" }, new string[] { "bi.user1", "user1" } + } + }; + + [Theory] + [MemberData(nameof(UserDataTestMessages))] + public async Task PublishUserDataMessagesTest(string messageKey, string messageValue, string[] availableHubs, string[] expectedHubs, string[] expectedUsers) + { + var dr = GetDefaultResolver(availableHubs, out var scm); + + PrepareConnection(scm, out var result); + + using (var bus = new ServiceMessageBus(dr)) + { + await bus.Publish(SignalRMessageUtility.CreateMessage(messageKey, messageValue)); + } + + Assert.Equal(expectedHubs.Length, result.Count); + Assert.Equal(expectedUsers.Length, result.Count); + + for (var i = 0; i < expectedHubs.Length; i++) + { + Assert.True(result.TryGetValue(expectedHubs[i], out var current)); + var message = current as UserDataMessage; + Assert.NotNull(message); + + Assert.Equal(expectedUsers[i], message.UserId); + Assert.Equal(messageValue, message.Payloads["json"].GetSingleFramePayload()); + } + } + + private static void PrepareConnection(IServiceConnectionManager scm, out SortedList output) + { + var result = new SortedList(); + scm.Initialize(hub => new TestServiceConnection(hub, + m => + { + lock (result) + { + result.Add(hub, m.Item1); + } + }), 5); + output = result; + } + + private static IDependencyResolver GetDefaultResolver(IReadOnlyList hubs, out IServiceConnectionManager scm) + { + var resolver = new DefaultDependencyResolver(); + resolver.Register(typeof(IServiceProtocol), () => new ServiceProtocol()); + var connectionManager = new ServiceConnectionManager(AppName, hubs); + resolver.Register(typeof(IServiceConnectionManager), () => connectionManager); + resolver.Register(typeof(IMessageParser), () => new SignalRMessageParser(hubs, resolver)); + scm = connectionManager; + return resolver; + } + + private sealed class TestServiceConnection : IServiceConnectionContainer + { + private readonly Action<(ServiceMessage, IServiceConnectionContainer)> _validator; + + public string HubName { get; } + + public TestServiceConnection(string name, Action<(ServiceMessage, IServiceConnectionContainer)> validator) + { + _validator = validator; + HubName = name; + } + + public Task StartAsync() + { + return Task.CompletedTask; + } + + public Task WriteAsync(ServiceMessage serviceMessage) + { + _validator((serviceMessage, this)); + return Task.CompletedTask; + } + + public Task StopAsync() + { + return Task.CompletedTask; + } + + public Task WriteAsync(string partitionKey, ServiceMessage serviceMessage) + { + return WriteAsync(serviceMessage); + } + } + + private static IServiceProtocol DefaultServiceProtocol = new ServiceProtocol(); + + private static ReadOnlyMemory GenerateSingleFrameBuffer(ReadOnlyMemory inner) + { + var singleFrameMessage = new ConnectionDataMessage(string.Empty, inner); + return DefaultServiceProtocol.GetMessageBytes(singleFrameMessage); + } + + private static ReadOnlyMemory GenerateSingleFrameBuffer(string message) + { + var inner = Encoding.UTF8.GetBytes(message); + var singleFrameMessage = new ConnectionDataMessage(string.Empty, inner); + return DefaultServiceProtocol.GetMessageBytes(singleFrameMessage); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageParserTest.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageParserTest.cs new file mode 100644 index 000000000..c9aeece1e --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageParserTest.cs @@ -0,0 +1,298 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.AspNet.SignalR; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Json; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + public class SignalRMessageParserTest + { + private readonly IDependencyResolver _resolver = GetDefaultResolver(); + private readonly MemoryPool _pool = new MemoryPool(); + private readonly JsonSerializer _serializer = new JsonSerializer(); + + [Theory] + [InlineData("")] + [InlineData("key")] + [InlineData("pc-SignalRMessageParserTest")] + [InlineData("pcg-GroupSignalRMessageParserTest")] + [InlineData("c-GroupSignalRMessageParserTest")] + public void TestGetMessageWithUnknownKeyThrows(string key) + { + var hubs = new List { }; + var parser = new SignalRMessageParser(hubs, _resolver); + var raw = ""; + var message = new Message("foo", key, new ArraySegment(Encoding.Default.GetBytes(raw))); + + var result = Assert.Throws(() => parser.GetMessages(message).ToList()); + } + + [Fact] + public void TestAddToGroupCommandMessage() + { + var hubs = new List { }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + var command = new Command + { + CommandType = CommandType.AddToGroup, + Value = groupName, + WaitForAck = true + }; + + var connectionId = GenerateRandomName(); + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command); + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as JoinGroupMessage; + Assert.NotNull(msg); + Assert.Equal(connectionId, msg.ConnectionId); + Assert.Equal(groupName, msg.GroupName); + } + + [Theory] + [InlineData(CommandType.AddToGroup, "")] + [InlineData(CommandType.AddToGroup, "a")] + [InlineData(CommandType.AddToGroup, "c-")] + [InlineData(CommandType.AddToGroup, "h-a")] + [InlineData(CommandType.RemoveFromGroup, "")] + [InlineData(CommandType.RemoveFromGroup, "a")] + [InlineData(CommandType.RemoveFromGroup, "c-")] + [InlineData(CommandType.RemoveFromGroup, "h-a")] + public void TestAddToGroupCommandMessageWithInvalidKeyThrows(CommandType type, string invalidKey) + { + var hubs = new List { }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + var command = new Command + { + CommandType = type, + Value = groupName, + WaitForAck = true + }; + + var message = SignalRMessageUtility.CreateMessage(invalidKey, command); + + Assert.Throws(() => parser.GetMessages(message).ToList()); + } + + [Fact] + public void TestRemoveFromGroupCommandMessage() + { + var hubs = new List { }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + var command = new Command + { + CommandType = CommandType.RemoveFromGroup, + Value = groupName, + WaitForAck = true + }; + + var connectionId = GenerateRandomName(); + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command); + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as LeaveGroupMessage; + Assert.NotNull(msg); + Assert.Equal(connectionId, msg.ConnectionId); + Assert.Equal(groupName, msg.GroupName); + } + + [Theory] + [InlineData(CommandType.Initializing)] + [InlineData(CommandType.Abort)] + public void TestOtherGroupCommandMessagesAreIgnored(CommandType type) + { + var hubs = new List { }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + var command = new Command + { + CommandType = type, + Value = groupName, + WaitForAck = true + }; + + var connectionId = GenerateRandomName(); + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetConnectionId(connectionId), command); + + var msgs = parser.GetMessages(message).ToList(); + Assert.Empty(msgs); + } + + + [Theory] + [InlineData("connection1", "msg")] + [InlineData("a.connection1", null)] + [InlineData("h-a.connection1", "")] + [InlineData("h-", "")] + [InlineData("", "", typeof(NotSupportedException))] + public void TestHubMessage(string connectionId, string input, Type exceptionType = null) + { + var hubs = new List { "h-", "a", "a.connection1" }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubName(connectionId), input); + var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() }; + message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList()); + + if (exceptionType != null) + { + Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList()); + return; + } + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as BroadcastDataMessage; + Assert.NotNull(msg); + Assert.Equal(excludedConnectionIds, msg.ExcludedList); + Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload()); + } + + [Theory] + [InlineData("hc-hub.hub1", null, "hub1")] + [InlineData("hc-hub.hub1.h....user1", null, "user1")] + [InlineData("hc-hub1.connection1", "", "connection1")] + [InlineData("hub1.connection1", "", "connection1")] + [InlineData("key", "", null, typeof(ArgumentException))] + [InlineData("", "", null, typeof(NotSupportedException))] + [InlineData("hc-hub1", "msg", null, typeof(ArgumentException))] + public void TestHubConnectionMessage(string connectionId, string input, string expectedId, Type exceptionType = null) + { + var hubs = new List { "hub", "hub1", "hub.hub1", "h", "hub.hub1.h.hub2", "hub.hub1.h" }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubConnectionId(connectionId), input); + var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() }; + message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList()); + + if (exceptionType != null) + { + Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList()); + return; + } + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as ConnectionDataMessage; + Assert.NotNull(msg); + Assert.Equal(expectedId, msg.ConnectionId); + Assert.Equal(input, msg.Payload.First.GetSingleFramePayload()); + } + + [Theory] + [InlineData("msg", "hub1")] + [InlineData(null, "hub.hub1")] + [InlineData("", "hub.hub1.h.hub2")] + public void TestHubGroupMessage(string input, string hub) + { + var hubs = new List { "hub1", "hub.hub1", "hub.hub1.h.hub2" }; + var parser = new SignalRMessageParser(hubs, _resolver); + var groupName = GenerateRandomName(); + var fullName = PrefixHelper.GetHubGroupName(hub + "." + groupName); + var message = SignalRMessageUtility.CreateMessage(fullName, input); + var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() }; + message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList()); + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as GroupBroadcastDataMessage; + Assert.NotNull(msg); + + // For group message, it is the full name as the group, e.g. hg-hub.hub1.h.hub2.abcde + Assert.Equal(fullName, msg.GroupName); + Assert.Equal(excludedConnectionIds, msg.ExcludedList); + Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload()); + } + + [Theory] + [InlineData("user", "msg", "hub1")] + [InlineData(".", null, "hub.hub1")] + [InlineData("..hub1.user1", "", "hub2.hub1.h.hub2")] + public void TestHubUserMessage(string userName, string input, string hub, Type exceptionType = null) + { + var hubs = new List { "hub1", "hub.hub1", "hub2.hub1.h.hub2", ".", ".." }; + var parser = new SignalRMessageParser(hubs, _resolver); + + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubUserId(hub + "." + userName), input); + var excludedConnectionIds = new string[] { GenerateRandomName(), GenerateRandomName() }; + message.Filter = GetFilter(excludedConnectionIds.Select(s => PrefixHelper.GetConnectionId(s)).ToList()); + + if (exceptionType != null) + { + Assert.Throws(exceptionType, () => parser.GetMessages(message).ToList()); + return; + } + + var msgs = parser.GetMessages(message).ToList(); + Assert.Single(msgs); + var msg = msgs[0].Message as UserDataMessage; + Assert.NotNull(msg); + Assert.Equal(userName, msg.UserId); + Assert.Equal(input, msg.Payloads["json"].GetSingleFramePayload()); + } + + [Fact] + public void TestHubUserMessageWithMultiplePossiblities() + { + var hubs = new List { "hub", "hub.hub1", "hub.hub1.h.hub2", ".", "......" }; + var parser = new SignalRMessageParser(hubs, _resolver); + var fullName = "hub.hub1.h.hub2.user1"; + var message = SignalRMessageUtility.CreateMessage(PrefixHelper.GetHubUserId(fullName), null); + + var msgs = parser.GetMessages(message).ToList(); + Assert.Equal(3, msgs.Count); + var msg = msgs[0].Message as UserDataMessage; + Assert.NotNull(msg); + Assert.Equal("h.hub2.user1", msg.UserId); + msg = msgs[1].Message as UserDataMessage; + Assert.NotNull(msg); + Assert.Equal("user1", msg.UserId); + msg = msgs[2].Message as UserDataMessage; + Assert.NotNull(msg); + Assert.Equal("hub1.h.hub2.user1", msg.UserId); + } + + private string GenerateRandomName() + { + return Guid.NewGuid().ToString("N"); + } + + private static IDependencyResolver GetDefaultResolver() + { + var config = new HubConfiguration(); + var resolver = config.Resolver; + resolver.Register(typeof(JsonSerializer), () => new JsonSerializer()); + resolver.Register(typeof(IServiceProtocol), () => new ServiceProtocol()); + resolver.Register(typeof(IMemoryPool), () => new MemoryPool()); + return resolver; + } + + private static string GetFilter(IList excludedSignals) + { + if (excludedSignals != null) + { + return String.Join("|", excludedSignals); + } + + return null; + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageUtility.cs b/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageUtility.cs new file mode 100644 index 000000000..a67f7c9d0 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.AspNet.Tests/SignalRMessageUtility.cs @@ -0,0 +1,105 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Text; +using Microsoft.AspNet.SignalR.Infrastructure; +using Microsoft.AspNet.SignalR.Json; +using Microsoft.AspNet.SignalR.Messaging; +using Microsoft.Azure.SignalR.Protocol; +using Newtonsoft.Json; +using Xunit; + +namespace Microsoft.Azure.SignalR.AspNet.Tests +{ + internal static class SignalRMessageUtility + { + private static readonly ServiceProtocol DefaultServiceProtocol = new ServiceProtocol(); + private static readonly JsonSerializer DefaultJsonSerializer = new JsonSerializer(); + private static readonly MemoryPool DefaultPool = new MemoryPool(); + + public static string GetSingleFramePayload(this ReadOnlyMemory payload) + { + var buffer = new ReadOnlySequence(payload); + DefaultServiceProtocol.TryParseMessage(ref buffer, out var message); + var frame = message as ConnectionDataMessage; + Assert.NotNull(frame); + var msg = Encoding.UTF8.GetString(frame.Payload.First.ToArray()); + Assert.NotNull(msg); + var response = JsonConvert.DeserializeObject(msg); + Assert.NotNull(response); + Assert.Equal("0", response.C); + Assert.Single(response.M); + return response.M[0]; + } + + public static Message CreateMessage(string key, object value) + { + ArraySegment messageBuffer = GetMessageBuffer(value); + + var message = new Message(Guid.NewGuid().ToString("N"), key, messageBuffer); + + var command = value as Command; + if (command != null) + { + // Set the command id + message.CommandId = command.Id; + message.WaitForAck = command.WaitForAck; + } + + return message; + } + + private static ArraySegment GetMessageBuffer(object value) + { + ArraySegment messageBuffer; + // We can't use "as" like we do for Command since ArraySegment is a struct + if (value is ArraySegment) + { + // We assume that any ArraySegment is already JSON serialized + messageBuffer = (ArraySegment)value; + } + else + { + messageBuffer = SerializeMessageValue(value); + } + return messageBuffer; + } + + private static ArraySegment SerializeMessageValue(object value) + { + using (var writer = new MemoryPoolTextWriter(DefaultPool)) + { + + var selfSerializer = value as IJsonWritable; + + if (selfSerializer != null) + { + selfSerializer.WriteJson(writer); + } + else + { + DefaultJsonSerializer.Serialize(writer, value); + } + + writer.Flush(); + + var data = writer.Buffer; + + var buffer = new byte[data.Count]; + + Buffer.BlockCopy(data.Array, data.Offset, buffer, 0, data.Count); + + return new ArraySegment(buffer); + } + } + + private sealed class Response + { + public string C { get; set; } + public List M { get; set; } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.Common.Tests/ConnectionStringParserFacts.cs b/test/Microsoft.Azure.SignalR.Common.Tests/ConnectionStringParserFacts.cs new file mode 100644 index 000000000..4b0a3b21b --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Common.Tests/ConnectionStringParserFacts.cs @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using Xunit; + +namespace Microsoft.Azure.SignalR.Common.Tests +{ + public class ConnectionStringParserFacts + { + [Theory] + [InlineData("https://aaa", "endpoint=https://aaa;AccessKey=bbb;")] + [InlineData("https://aaa", "ENDPOINT=https://aaa/;ACCESSKEY=bbb;")] + [InlineData("http://aaa", "endpoint=http://aaa;AccessKey=bbb;")] + [InlineData("http://aaa", "ENDPOINT=http://aaa/;ACCESSKEY=bbb;")] + public void ValidPreviewConnectionString(string expectedEndpoint, string connectionString) + { + var (endpoint, accessKey, version, port) = ConnectionStringParser.Parse(connectionString); + + Assert.Equal(expectedEndpoint, endpoint); + Assert.Equal("bbb", accessKey); + Assert.Null(version); + Assert.Null(port); + } + + [Theory] + [InlineData("https://aaa", "1.0", null, "endpoint=https://aaa;AccessKey=bbb;version=1.0")] + [InlineData("https://aaa", "1.0-preview", null, "ENDPOINT=https://aaa/;ACCESSKEY=bbb;VERSION=1.0-preview")] + [InlineData("http://aaa", "1.1", null, "endpoint=http://aaa;AccessKey=bbb;Version=1.1")] + [InlineData("http://aaa", "1.1-beta2", 1234, "ENDPOINT=http://aaa/;ACCESSKEY=bbb;Version=1.1-beta2;Port=1234")] + public void ValidConnectionString(string expectedEndpoint, string expectedVersion, int? expectedPort, string connectionString) + { + var (endpoint, accessKey, version, port) = ConnectionStringParser.Parse(connectionString); + + Assert.Equal(expectedEndpoint, endpoint); + Assert.Equal("bbb", accessKey); + Assert.Equal(expectedVersion, version); + Assert.Equal(expectedPort, port); + } + + [Theory] + [InlineData("Endpoint=xxx")] + [InlineData("AccessKey=xxx")] + [InlineData("XXX=yyy")] + [InlineData("XXX")] + public void InvalidConnectionStrings(string connectionString) + { + var exception = Assert.Throws(() => ConnectionStringParser.Parse(connectionString)); + + Assert.Contains("Connection string missing required properties", exception.Message); + } + + [Theory] + [InlineData("Endpoint=aaa;AccessKey=bbb;")] + [InlineData("Endpoint=endpoint=aaa;AccessKey=bbb;")] + public void InvalidEndpoint(string connectionString) + { + var exception = Assert.Throws(() => ConnectionStringParser.Parse(connectionString)); + + Assert.Contains("Endpoint property in connection string is not a valid URI", exception.Message); + } + + [Theory] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=abc", "abc")] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=1.x", "1.x")] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=2.0", "2.0")] + public void InvalidVersion(string connectionString, string version) + { + var exception = Assert.Throws(() => ConnectionStringParser.Parse(connectionString)); + + Assert.Contains(string.Format("Version {0} is not supported.", version), exception.Message); + } + + [Theory] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=1.0;port=2.3")] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=1.1;port=1000000")] + [InlineData("Endpoint=https://aaa;AccessKey=bbb;version=1.0-preview;port=0")] + public void InvalidPort(string connectionString) + { + var exception = Assert.Throws(() => ConnectionStringParser.Parse(connectionString)); + + Assert.Contains(@"Invalid value for port property.", exception.Message); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Common.Tests/Microsoft.Azure.SignalR.Common.Tests.csproj b/test/Microsoft.Azure.SignalR.Common.Tests/Microsoft.Azure.SignalR.Common.Tests.csproj new file mode 100644 index 000000000..cb5312dab --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Common.Tests/Microsoft.Azure.SignalR.Common.Tests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + diff --git a/test/Microsoft.Azure.SignalR.Protocols.Tests/Microsoft.Azure.SignalR.Protocols.Tests.csproj b/test/Microsoft.Azure.SignalR.Protocols.Tests/Microsoft.Azure.SignalR.Protocols.Tests.csproj new file mode 100644 index 000000000..261a34d2e --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Protocols.Tests/Microsoft.Azure.SignalR.Protocols.Tests.csproj @@ -0,0 +1,26 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + + + + + + + diff --git a/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceMessageEqualityComparer.cs b/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceMessageEqualityComparer.cs new file mode 100644 index 000000000..4ebf56f11 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceMessageEqualityComparer.cs @@ -0,0 +1,244 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using Microsoft.Extensions.Primitives; + +namespace Microsoft.Azure.SignalR.Protocol.Tests +{ + public class ServiceMessageEqualityComparer : IEqualityComparer + { + public static readonly ServiceMessageEqualityComparer Instance = new ServiceMessageEqualityComparer(); + + public bool Equals(ServiceMessage x, ServiceMessage y) + { + if (x.GetType() != y.GetType()) + { + return false; + } + + switch (x) + { + case HandshakeRequestMessage handshakeRequestMessage: + return HandshakeRequestMessagesEqual(handshakeRequestMessage, (HandshakeRequestMessage)y); + case HandshakeResponseMessage handshakeResponseMessage: + return HandshakeResponseMessagesEqual(handshakeResponseMessage, (HandshakeResponseMessage)y); + case PingMessage _: + return y is PingMessage; + case OpenConnectionMessage openConnectionMessage: + return OpenConnectionMessagesEqual(openConnectionMessage, (OpenConnectionMessage)y); + case CloseConnectionMessage closeConnectionMessage: + return CloseConnectionMessagesEqual(closeConnectionMessage, (CloseConnectionMessage)y); + case ConnectionDataMessage connectionDataMessage: + return ConnectionDataMessagesEqual(connectionDataMessage, (ConnectionDataMessage)y); + case MultiConnectionDataMessage multiConnectionDataMessage: + return MultiConnectionDataMessagesEqual(multiConnectionDataMessage, (MultiConnectionDataMessage)y); + case UserDataMessage userDataMessage: + return UserDataMessagesEqual(userDataMessage, (UserDataMessage)y); + case MultiUserDataMessage multiUserDataMessage: + return MultiUserDataMessagesEqual(multiUserDataMessage, (MultiUserDataMessage)y); + case BroadcastDataMessage broadcastDataMessage: + return BroadcastDataMessagesEqual(broadcastDataMessage, (BroadcastDataMessage)y); + case JoinGroupMessage joinGroupMessage: + return JoinGroupMessagesEqual(joinGroupMessage, (JoinGroupMessage)y); + case LeaveGroupMessage leaveGroupMessage: + return LeaveGroupMessagesEqual(leaveGroupMessage, (LeaveGroupMessage)y); + case GroupBroadcastDataMessage groupBroadcastDataMessage: + return GroupBroadcastDataMessagesEqual(groupBroadcastDataMessage, (GroupBroadcastDataMessage)y); + case MultiGroupBroadcastDataMessage multiGroupBroadcastDataMessage: + return MultiGroupBroadcastDataMessagesEqual(multiGroupBroadcastDataMessage, + (MultiGroupBroadcastDataMessage)y); + default: + throw new InvalidOperationException($"Unknown message type: {x.GetType().FullName}"); + } + } + + public int GetHashCode(ServiceMessage obj) + { + return 0; + } + + private bool HandshakeRequestMessagesEqual(HandshakeRequestMessage x, HandshakeRequestMessage y) + { + return x.Version == y.Version; + } + + private bool HandshakeResponseMessagesEqual(HandshakeResponseMessage x, HandshakeResponseMessage y) + { + return StringEqual(x.ErrorMessage, y.ErrorMessage); + } + + private bool OpenConnectionMessagesEqual(OpenConnectionMessage x, OpenConnectionMessage y) + { + return StringEqual(x.ConnectionId, y.ConnectionId) && + ClaimsEqual(x.Claims, y.Claims) && + HeadersEqual(x.Headers, y.Headers) && + StringEqual(x.QueryString, y.QueryString); + } + + private bool CloseConnectionMessagesEqual(CloseConnectionMessage x, CloseConnectionMessage y) + { + return StringEqual(x.ConnectionId, y.ConnectionId) && StringEqual(x.ErrorMessage, y.ErrorMessage); + } + + private bool ConnectionDataMessagesEqual(ConnectionDataMessage x, ConnectionDataMessage y) + { + return StringEqual(x.ConnectionId, y.ConnectionId) && SequenceEqual(x.Payload.ToArray(), y.Payload.ToArray()); + } + + private bool MultiConnectionDataMessagesEqual(MultiConnectionDataMessage x, MultiConnectionDataMessage y) + { + return SequenceEqual(x.ConnectionList, y.ConnectionList) && PayloadsEqual(x.Payloads, y.Payloads); + } + + private bool UserDataMessagesEqual(UserDataMessage x, UserDataMessage y) + { + return StringEqual(x.UserId, y.UserId) && PayloadsEqual(x.Payloads, y.Payloads); + } + + private bool MultiUserDataMessagesEqual(MultiUserDataMessage x, MultiUserDataMessage y) + { + return SequenceEqual(x.UserList, y.UserList) && PayloadsEqual(x.Payloads, y.Payloads); + } + + private bool BroadcastDataMessagesEqual(BroadcastDataMessage x, BroadcastDataMessage y) + { + return SequenceEqual(x.ExcludedList, y.ExcludedList) && + PayloadsEqual(x.Payloads, y.Payloads); + } + + private bool JoinGroupMessagesEqual(JoinGroupMessage x, JoinGroupMessage y) + { + return StringEqual(x.ConnectionId, y.ConnectionId) && StringEqual(x.GroupName, y.GroupName); + } + + private bool LeaveGroupMessagesEqual(LeaveGroupMessage x, LeaveGroupMessage y) + { + return StringEqual(x.ConnectionId, y.ConnectionId) && StringEqual(x.GroupName, y.GroupName); + } + + private bool GroupBroadcastDataMessagesEqual(GroupBroadcastDataMessage x, GroupBroadcastDataMessage y) + { + return StringEqual(x.GroupName, y.GroupName) && + SequenceEqual(x.ExcludedList, y.ExcludedList) && + PayloadsEqual(x.Payloads, y.Payloads); + } + + private bool MultiGroupBroadcastDataMessagesEqual(MultiGroupBroadcastDataMessage x, + MultiGroupBroadcastDataMessage y) + { + return SequenceEqual(x.GroupList, y.GroupList) && PayloadsEqual(x.Payloads, y.Payloads); + } + + private static bool StringEqual(string x, string y) + { + return string.Equals(x, y, StringComparison.Ordinal); + } + + private static bool StringEqualIgnoreCase(string x, string y) + { + return string.Equals(x, y, StringComparison.OrdinalIgnoreCase); + } + + private static bool ClaimsEqual(Claim[] x, Claim[] y) + { + if (x == null && y == null) + { + return true; + } + + if (x?.Length != y?.Length) + { + return false; + } + + return !x.Where((t, i) => t.Type != y[i].Type || !StringEqual(t.Value, y[i].Value)).Any(); + } + + private static bool PayloadsEqual(IDictionary> x, + IDictionary> y) + { + if (x == null && y == null) + { + return true; + } + + if (x?.Count != y?.Count) + { + return false; + } + + for (int i = 0; i < x.Count; i++) + { + if (!StringEqual(x.ElementAt(i).Key, y.ElementAt(i).Key) || + !SequenceEqual(x.ElementAt(i).Value.ToArray(), y.ElementAt(i).Value.ToArray())) + { + return false; + } + } + + return true; + } + + private static bool HeadersEqual(IDictionary x, IDictionary y) + { + if (x == null && y == null) + { + return true; + } + + if (x?.Count != y?.Count) + { + return false; + } + + for (int i = 0; i < x.Count; i++) + { + var xKey = x.ElementAt(i).Key; + var yKey = y.ElementAt(i).Key; + if (!StringEqualIgnoreCase(xKey, yKey) || + !SequenceEqual(x[xKey], y[yKey]) || + !SequenceEqual(x[xKey], y[yKey.ToUpper()])) + { + return false; + } + } + + return true; + } + + private static bool SequenceEqual(object left, object right) + { + if (left == null && right == null) + { + return true; + } + + var leftEnumerable = left as IEnumerable; + var rightEnumerable = right as IEnumerable; + if (leftEnumerable == null || rightEnumerable == null) + { + return false; + } + + var leftEnumerator = leftEnumerable.GetEnumerator(); + var rightEnumerator = rightEnumerable.GetEnumerator(); + var leftMoved = leftEnumerator.MoveNext(); + var rightMoved = rightEnumerator.MoveNext(); + for (; leftMoved && rightMoved; leftMoved = leftEnumerator.MoveNext(), rightMoved = rightEnumerator.MoveNext()) + { + if (!Equals(leftEnumerator.Current, rightEnumerator.Current)) + { + return false; + } + } + + return !leftMoved && !rightMoved; + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceProtocolFacts.cs b/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceProtocolFacts.cs new file mode 100644 index 000000000..f6fcc806e --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Protocols.Tests/ServiceProtocolFacts.cs @@ -0,0 +1,292 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using Microsoft.Extensions.Primitives; +using Xunit; + +namespace Microsoft.Azure.SignalR.Protocol.Tests +{ + public class ServiceProtocolFacts + { + private static readonly IServiceProtocol Protocol = new ServiceProtocol(); + + public static IEnumerable TestDataNames + { + get + { + foreach (var k in TestData.Keys) + { + yield return new object[] { k }; + } + } + } + + public static IDictionary TestData => new[] + { + new ProtocolTestData( + name: "HandshakeRequest", + message: new HandshakeRequestMessage(1), + binary: "kgEB"), + new ProtocolTestData( + name: "HandshakeResponse", + message: new HandshakeResponseMessage(), + binary: "kgKg"), + new ProtocolTestData( + name: "HandshakeResponseWithError", + message: new HandshakeResponseMessage("Version mismatch."), + binary: "kgKxVmVyc2lvbiBtaXNtYXRjaC4="), + new ProtocolTestData( + name: "Ping", + message: PingMessage.Instance, + binary: "kQM="), + new ProtocolTestData( + name: "OpenConnection", + message: new OpenConnectionMessage("conn1", null), + binary: "lQSlY29ubjGAgKA="), + new ProtocolTestData( + name: "OpenConnectionWithClaims", + message: new OpenConnectionMessage("conn2", new [] {new Claim(ClaimTypes.NameIdentifier, "user1")}), + binary: "lQSlY29ubjKB2URodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllcqV1c2VyMYCg"), + new ProtocolTestData( + name: "OpenConnectionWithHeaders", + message: new OpenConnectionMessage("conn3", null, new Dictionary + { + {"header-key-1", "header-value-1"}, + {"header-key-2", new[] {"heaer-value-2a", "header-value-2b"}}, + {"header-key-3", new[] {"heaer-value-3a", "header-value-3b", "header-value-3c"}} + }, string.Empty), + binary: "lQSlY29ubjOAg6xoZWFkZXIta2V5LTGRrmhlYWRlci12YWx1ZS0xrGhlYWRlci1rZXktMpKuaGVhZXItdmFsdWUtMmGvaGVhZGVyLXZhbHVlLTJirGhlYWRlci1rZXktM5OuaGVhZXItdmFsdWUtM2GvaGVhZGVyLXZhbHVlLTNir2hlYWRlci12YWx1ZS0zY6A="), + new ProtocolTestData( + name: "OpenConnectionWithQueryString1", + message: new OpenConnectionMessage("conn4", null, new Dictionary(), "query1=value1"), + binary: "lQSlY29ubjSAgK1xdWVyeTE9dmFsdWUx"), + new ProtocolTestData( + name: "OpenConnectionWithQueryString2", + message: new OpenConnectionMessage("conn4", null, new Dictionary(), "query1=value1&query2=query2&query3=value3"), + binary: "lQSlY29ubjSAgNkpcXVlcnkxPXZhbHVlMSZxdWVyeTI9cXVlcnkyJnF1ZXJ5Mz12YWx1ZTM="), + new ProtocolTestData( + name: "CloseConnection", + message: new CloseConnectionMessage("conn3"), + binary: "kwWlY29ubjPA"), + new ProtocolTestData( + name: "CloseConnectionWithError", + message: new CloseConnectionMessage("conn4", "Error message."), + binary: "kwWlY29ubjSuRXJyb3IgbWVzc2FnZS4="), + new ProtocolTestData( + name: "ConnectionData", + message: new ConnectionDataMessage("conn5", new byte[] {1, 2, 3, 4, 5, 6, 7}), + binary: "kwalY29ubjXEBwECAwQFBgc="), + new ProtocolTestData( + name: "MultiConnectionData", + message: new MultiConnectionDataMessage(new [] {"conn6", "conn7"}, new Dictionary> + { + ["json"] = new byte[] {2, 3, 4, 5, 6, 7, 1}, + ["messagepack"] = new byte[] {3, 4, 5, 6, 7, 1, 2} + }), + binary: "kweSpWNvbm42pWNvbm43gqRqc29uxAcCAwQFBgcBq21lc3NhZ2VwYWNrxAcDBAUGBwEC"), + new ProtocolTestData( + name: "UserData", + message: new UserDataMessage("user1", + new Dictionary> + { + ["json"] = new byte[] {6, 7, 1, 2, 3, 4, 5}, + ["messagepack"] = new byte[] {7, 1, 2, 3, 4, 5, 6} + }), + binary: "kwildXNlcjGCpGpzb27EBwYHAQIDBAWrbWVzc2FnZXBhY2vEBwcBAgMEBQY="), + new ProtocolTestData( + name: "MultiUserData", + message: new MultiUserDataMessage(new [] {"user1", "user2"}, + new Dictionary> + { + ["json"] = new byte[] {6, 7, 1, 2, 3, 4, 5}, + ["messagepack"] = new byte[] {7, 1, 2, 3, 4, 5, 6} + }), + binary: "kwmSpXVzZXIxpXVzZXIygqRqc29uxAcGBwECAwQFq21lc3NhZ2VwYWNrxAcHAQIDBAUG"), + new ProtocolTestData( + name: "Broadcast", + message: new BroadcastDataMessage(new Dictionary> + { + ["json"] = new byte[] {4, 5, 6, 7, 1, 2, 3}, + ["messagepack"] = new byte[] {5, 6, 7, 1, 2, 3, 4} + }), + binary: "kwqQgqRqc29uxAcEBQYHAQIDq21lc3NhZ2VwYWNrxAcFBgcBAgME"), + new ProtocolTestData( + name: "BroadcastExcept", + message: new BroadcastDataMessage(new[] {"conn7", "conn8", "conn9"}, + new Dictionary> + { + ["json"] = new byte[] {6, 7, 1, 2, 3, 4, 5}, + ["messagepack"] = new byte[] {7, 1, 2, 3, 4, 5, 6} + }), + binary: "kwqTpWNvbm43pWNvbm44pWNvbm45gqRqc29uxAcGBwECAwQFq21lc3NhZ2VwYWNrxAcHAQIDBAUG"), + new ProtocolTestData( + name: "JoinGroup", + message: new JoinGroupMessage("conn10", "group1"), + binary: "kwumY29ubjEwpmdyb3VwMQ=="), + new ProtocolTestData( + name: "LeaveGroup", + message: new LeaveGroupMessage("conn11", "group2"), + binary: "kwymY29ubjExpmdyb3VwMg=="), + new ProtocolTestData( + name: "GroupBroadcast", + message: new GroupBroadcastDataMessage("group3", + new Dictionary> + { + ["json"] = new byte[] {6, 7, 1, 2, 3, 4, 5}, + ["messagepack"] = new byte[] {7, 1, 2, 3, 4, 5, 6} + }), + binary: "lA2mZ3JvdXAzkIKkanNvbsQHBgcBAgMEBattZXNzYWdlcGFja8QHBwECAwQFBg=="), + new ProtocolTestData( + name: "GroupBroadcastExcept", + message: new GroupBroadcastDataMessage("group3", new [] {"conn12", "conn13"}, + new Dictionary> + { + ["json"] = new byte[] {6, 7, 1, 2, 3, 4, 5}, + ["messagepack"] = new byte[] {7, 1, 2, 3, 4, 5, 6} + }), + binary: "lA2mZ3JvdXAzkqZjb25uMTKmY29ubjEzgqRqc29uxAcGBwECAwQFq21lc3NhZ2VwYWNrxAcHAQIDBAUG"), + new ProtocolTestData( + name: "MultiGroupBroadcast", + message: new MultiGroupBroadcastDataMessage(new [] {"group4", "group5"}, + new Dictionary> + { + ["json"] = new byte[] {1, 2, 3, 4, 5, 6, 7, 8}, + ["messagepack"] = new byte[] {7, 8, 1, 2, 3, 4, 5, 6} + }), + binary: "kw6Spmdyb3VwNKZncm91cDWCpGpzb27ECAECAwQFBgcIq21lc3NhZ2VwYWNrxAgHCAECAwQFBg=="), + }.ToDictionary(t => t.Name); + + [Theory] + [MemberData(nameof(TestDataNames))] + public void ParseMessages(string testDataName) + { + var testData = TestData[testDataName]; + + // Verify that the input binary string decodes to the expected MsgPack primitives + var bytes = Convert.FromBase64String(testData.Binary); + + // Parse the input fully now. + bytes = Frame(bytes); + var message = ParseServiceMessage(bytes); + Assert.Equal(testData.Message, message, ServiceMessageEqualityComparer.Instance); + } + + [Theory] + [MemberData(nameof(TestDataNames))] + public void WriteMessages(string testDataName) + { + var testData = TestData[testDataName]; + + var bytes = Protocol.GetMessageBytes(testData.Message); + + // Unframe the message to check the binary encoding + var byteSpan = new ReadOnlySequence(bytes); + Assert.True(BinaryMessageParser.TryParseMessage(ref byteSpan, out var unframed)); + + // Check the baseline binary encoding, use Assert.True in order to configure the error message + var actual = Convert.ToBase64String(unframed.ToArray()); + Assert.True(string.Equals(actual, testData.Binary, StringComparison.Ordinal), +$@"Binary encoding changed from + [{testData.Binary}] +to + [{actual}] +Please verify the MsgPack output and update the baseline"); + } + + [Fact] + public void ParseMessageWithExtraData() + { + // Legacy protocol + var expectedMessage = new OpenConnectionMessage("id", null); + var bytes = new byte[] + { + ArrayBytes(3), // Array Length + 4, // Message Type: OpenConnectionMessage + StringBytes(2), (byte)'i', (byte)'d', // OpenConnectionMessage.ConnectionId + MapBytes(0), // OpenConnectionMessage.Claims + StringBytes(2), (byte)'e', (byte)'x' // Extra trailing data + }; + + bytes = Frame(bytes); + var message = ParseServiceMessage(bytes); + var openConnectionMessage = Assert.IsType(message); + Assert.Equal(expectedMessage, openConnectionMessage, ServiceMessageEqualityComparer.Instance); + + // Current protocol + expectedMessage = new OpenConnectionMessage("id", null, new Dictionary(), "?k=v"); + bytes = new byte[] + { + ArrayBytes(5), // Array Length + 4, // Message Type: OpenConnectionMessage + StringBytes(2), (byte)'i', (byte)'d', // OpenConnectionMessage.ConnectionId + MapBytes(0), // OpenConnectionMessage.Claims + MapBytes(0), // OpenConnectionMessage.Headers + StringBytes(4), (byte)'?', (byte)'k', (byte)'=', (byte)'v', // OpenConnectionMessage.QueryString + StringBytes(2), (byte)'e', (byte)'x' // Extra trailing data + }; + + bytes = Frame(bytes); + message = ParseServiceMessage(bytes); + openConnectionMessage = Assert.IsType(message); + Assert.Equal(expectedMessage, openConnectionMessage, ServiceMessageEqualityComparer.Instance); + } + + private static byte ArrayBytes(int size) + { + return (byte) (0x90 | size); + } + + private static byte StringBytes(int size) + { + return (byte) (0xa0 | size); + } + + private static byte MapBytes(int size) + { + return (byte) (0x80 | size); + } + + private static byte[] Frame(byte[] input) + { + var stream = MemoryBufferWriter.Get(); + try + { + BinaryMessageFormatter.WriteLengthPrefix(input.Length, stream); + stream.Write(input); + return stream.ToArray(); + } + finally + { + MemoryBufferWriter.Return(stream); + } + } + + private static ServiceMessage ParseServiceMessage(byte[] bytes) + { + var data = new ReadOnlySequence(bytes); + Assert.True(Protocol.TryParseMessage(ref data, out var message)); + return message; + } + + public class ProtocolTestData + { + public string Name { get; } + public string Binary { get; } + public ServiceMessage Message { get; } + + public ProtocolTestData(string name, ServiceMessage message, string binary) + { + Name = name; + Message = message; + Binary = binary; + } + + public override string ToString() => Name; + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/AddAzureSignalRFacts.cs b/test/Microsoft.Azure.SignalR.Tests/AddAzureSignalRFacts.cs new file mode 100644 index 000000000..116bc9ac4 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/AddAzureSignalRFacts.cs @@ -0,0 +1,123 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class AddAzureSignalRFacts + { + private const string CustomValue = "customconnectionstring"; + private const string DefaultValue = "defaultconnectionstring"; + private const string SecondaryValue = "secondaryconnectionstring"; + + [Fact] + public void AddAzureSignalRReadsDefaultConfigurationKeyForConnectionString() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + var serviceProvider = services.AddSignalR() + .AddAzureSignalR() + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var options = serviceProvider.GetRequiredService>().Value; + + Assert.Equal(DefaultValue, options.ConnectionString); + Assert.Equal(5, options.ConnectionCount); + Assert.Equal(TimeSpan.FromHours(1), options.AccessTokenLifetime); + Assert.Null(options.ClaimsProvider); + } + + [Fact] + public void AddAzureUsesDefaultConnectionStringIfSpecifiedAndOptionsOverridden() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + var serviceProvider = services.AddSignalR() + .AddAzureSignalR(o => { o.ConnectionCount = 1; }) + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var options = serviceProvider.GetRequiredService>().Value; + + Assert.Equal(DefaultValue, options.ConnectionString); + Assert.Equal(1, options.ConnectionCount); + Assert.Equal(TimeSpan.FromHours(1), options.AccessTokenLifetime); + Assert.Null(options.ClaimsProvider); + } + + [Fact] + public void AddAzureReadsConnectionStringFirst() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + string capturedConnectionString = null; + var serviceProvider = services.AddSignalR() + .AddAzureSignalR(o => { capturedConnectionString = o.ConnectionString; }) + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var options = serviceProvider.GetRequiredService>().Value; + + Assert.Equal(DefaultValue, options.ConnectionString); + Assert.Equal(DefaultValue, capturedConnectionString); + } + + [Theory] + [InlineData(CustomValue, null, null, CustomValue)] + [InlineData(CustomValue, DefaultValue, SecondaryValue, CustomValue)] + [InlineData(null, DefaultValue, SecondaryValue, DefaultValue)] + [InlineData(null, null, SecondaryValue, SecondaryValue)] + public void AddAzureSignalRLoadConnectionStringOrder(string customValue, string defaultValue, + string secondaryValue, string expected) + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", defaultValue}, + {"ConnectionStrings:Azure:SignalR:ConnectionString", secondaryValue} + }) + .Build(); + var serviceProvider = services.AddSignalR() + .AddAzureSignalR(o => + { + if (customValue != null) + { + o.ConnectionString = customValue; + } + }) + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var options = serviceProvider.GetRequiredService>().Value; + + Assert.Equal(expected, options.ConnectionString); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.Tests/AzureSignalRMarkerServiceFact.cs b/test/Microsoft.Azure.SignalR.Tests/AzureSignalRMarkerServiceFact.cs new file mode 100644 index 000000000..5906edce6 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/AzureSignalRMarkerServiceFact.cs @@ -0,0 +1,110 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Threading; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder.Internal; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class AzureSignalRMarkerServiceFact + { + private const string DefaultValue = "Endpoint=https://abc;AccessKey=abc123;"; + + [Fact] + public void UseAzureSignalRWithAddAzureSignalR() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + var serviceProvider = services.AddLogging() + .AddSignalR() + .AddAzureSignalR() + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var app = new ApplicationBuilder(serviceProvider); + app.UseAzureSignalR(routes => + { + routes.MapHub("/chat"); + }); + + Assert.NotNull(serviceProvider.GetService>()); + } + + [Fact] + public void UseSignalRWithAddAzureSignalR() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + var serviceProvider = services.AddLogging() + .AddSignalR() + .AddAzureSignalR() + .Services + .AddSingleton(new EmptyApplicationLifetime()) + .AddSingleton(config) + .BuildServiceProvider(); + + var app = new ApplicationBuilder(serviceProvider); + app.UseSignalR(routes => + { + Assert.Throws(() => routes.MapHub("/chat")); + }); + } + + [Fact] + public void UseAzureSignalRWithAddSignalR() + { + var services = new ServiceCollection(); + var config = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + {"Azure:SignalR:ConnectionString", DefaultValue} + }) + .Build(); + var serviceProvider = services.AddLogging() + .AddSignalR() + .AddMessagePackProtocol() + .Services + .AddSingleton(new EmptyApplicationLifetime()) + .AddSingleton(config) + .BuildServiceProvider(); + + var app = new ApplicationBuilder(serviceProvider); + Assert.Throws(() => app.UseAzureSignalR(routes => + { + routes.MapHub("/chat"); + })); + } + } + + public class EmptyApplicationLifetime : IApplicationLifetime + { + public CancellationToken ApplicationStarted => CancellationToken.None; + + public CancellationToken ApplicationStopping => CancellationToken.None; + + public CancellationToken ApplicationStopped => CancellationToken.None; + + public void StopApplication() + { + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/HandshakeUtils.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/HandshakeUtils.cs new file mode 100644 index 000000000..5f358b353 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/HandshakeUtils.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IO; +using System.IO.Pipelines; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.Tests +{ + public static class HandshakeUtils + { + private static readonly TimeSpan DefaultHandshakeTimeout = TimeSpan.FromSeconds(5); + private static readonly IServiceProtocol ServiceProtocol = new ServiceProtocol(); + + public static async Task ReceiveHandshakeRequestAsync(PipeReader input) + { + using (var cts = new CancellationTokenSource(DefaultHandshakeTimeout)) + { + while (true) + { + var result = await input.ReadAsync(cts.Token); + + var buffer = result.Buffer; + var consumed = buffer.Start; + var examined = buffer.End; + + try + { + if (!buffer.IsEmpty) + { + if (ServiceProtocol.TryParseMessage(ref buffer, out var message)) + { + consumed = buffer.Start; + examined = consumed; + + if (!(message is HandshakeRequestMessage handshakeRequest)) + { + throw new InvalidDataException( + $"{message.GetType().Name} received when waiting for handshake request."); + } + + if (handshakeRequest.Version != ServiceProtocol.Version) + { + throw new InvalidDataException("Protocol version not supported."); + } + + break; + } + } + + if (result.IsCompleted) + { + // Not enough data, and we won't be getting any more data. + throw new InvalidOperationException( + "Service connectioned disconnected before sending a handshake request"); + } + } + finally + { + input.AdvanceTo(consumed, examined); + } + } + } + } + + public static Task SendHandshakeResponseAsync(PipeWriter output, HandshakeResponseMessage response = null) + { + ServiceProtocol.WriteMessage(response ?? new HandshakeResponseMessage(), output); + return output.FlushAsync().AsTask(); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/PipeReaderExtensions.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/PipeReaderExtensions.cs new file mode 100644 index 000000000..7cdee1117 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/PipeReaderExtensions.cs @@ -0,0 +1,110 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace System.IO.Pipelines +{ + public static class PipeReaderExtensions + { + public static async Task WaitToReadAsync(this PipeReader pipeReader) + { + while (true) + { + var result = await pipeReader.ReadAsync(); + + try + { + if (!result.Buffer.IsEmpty) + { + return true; + } + + if (result.IsCompleted) + { + return false; + } + } + finally + { + // Don't consume or advance + pipeReader.AdvanceTo(result.Buffer.Start, result.Buffer.Start); + } + } + } + + public static async Task ReadSingleAsync(this PipeReader pipeReader) + { + while (true) + { + var result = await pipeReader.ReadAsync(); + + try + { + return result.Buffer.ToArray(); + } + finally + { + pipeReader.AdvanceTo(result.Buffer.End); + } + } + } + + public static async Task ConsumeAsync(this PipeReader pipeReader, int numBytes) + { + while (true) + { + var result = await pipeReader.ReadAsync(); + if (result.Buffer.Length < numBytes) + { + pipeReader.AdvanceTo(result.Buffer.Start, result.Buffer.End); + continue; + } + + pipeReader.AdvanceTo(result.Buffer.GetPosition(numBytes)); + break; + } + } + + public static async Task ReadAllAsync(this PipeReader pipeReader) + { + while (true) + { + var result = await pipeReader.ReadAsync(); + + if (result.IsCompleted) + { + return result.Buffer.ToArray(); + } + + // Consume nothing, just wait for everything + pipeReader.AdvanceTo(result.Buffer.Start, result.Buffer.End); + } + } + + public static async Task ReadAsync(this PipeReader pipeReader, int numBytes) + { + while (true) + { + var result = await pipeReader.ReadAsync(); + if (result.Buffer.Length < numBytes) + { + pipeReader.AdvanceTo(result.Buffer.Start, result.Buffer.End); + continue; + } + + var buffer = result.Buffer.Slice(0, numBytes); + + var bytes = buffer.ToArray(); + + pipeReader.AdvanceTo(buffer.End); + + return bytes; + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/ServiceConnectionProxy.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/ServiceConnectionProxy.cs new file mode 100644 index 000000000..58f8632a9 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/ServiceConnectionProxy.cs @@ -0,0 +1,180 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.IO.Pipelines; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging.Abstractions; + +namespace Microsoft.Azure.SignalR.Tests +{ + internal class ServiceConnectionProxy : IClientConnectionManager, IClientConnectionFactory + { + private static readonly IServiceProtocol ServiceProtocol = new ServiceProtocol(); + private readonly PipeOptions _clientPipeOptions; + + public TestConnectionFactory ConnectionFactory { get; } + + public IClientConnectionManager ClientConnectionManager { get; } + + public TestConnection ConnectionContext => ConnectionFactory.CurrentConnectionContext; + + public ServiceConnection ServiceConnection { get; } + + public ConcurrentDictionary ClientConnections => ClientConnectionManager.ClientConnections; + + private readonly ConcurrentDictionary> _waitForConnectionOpen = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> _waitForConnectionClose = new ConcurrentDictionary>(); + private readonly ConcurrentDictionary> _waitForApplicationMessage = new ConcurrentDictionary>(); + + public ServiceConnectionProxy(ConnectionDelegate callback = null, PipeOptions clientPipeOptions = null, + TestConnectionFactory connectionFactory = null) + { + ConnectionFactory = connectionFactory ?? new TestConnectionFactory(); + ClientConnectionManager = new ClientConnectionManager(); + _clientPipeOptions = clientPipeOptions; + + ServiceConnection = new ServiceConnection( + ServiceProtocol, + this, + ConnectionFactory, + NullLoggerFactory.Instance, + callback ?? OnConnectionAsync, + this, + Guid.NewGuid().ToString("N")); + } + + public Task StartAsync() + { + return ServiceConnection.StartAsync(); + } + + public async Task ProcessApplicationMessagesAsync() + { + try + { + while (true) + { + var result = await ConnectionContext.Application.Input.ReadAsync(); + var buffer = result.Buffer; + + var consumed = buffer.Start; + var examined = buffer.End; + + try + { + if (result.IsCanceled) + { + break; + } + + if (!buffer.IsEmpty) + { + if (ServiceProtocol.TryParseMessage(ref buffer, out var message)) + { + consumed = buffer.Start; + examined = consumed; + + AddApplicationMessage(message.GetType(), message); + } + } + + if (result.IsCompleted) + { + break; + } + } + finally + { + ConnectionContext.Application.Input.AdvanceTo(consumed, examined); + } + } + } + catch + { + // Ignored. + } + } + + public void Stop() + { + _ = ServiceConnection.StopAsync(); + } + + public async Task WriteMessageAsync(ServiceMessage message) + { + ServiceProtocol.WriteMessage(message, ConnectionContext.Application.Output); + await ConnectionContext.Application.Output.FlushAsync(); + } + + public Task WaitForConnectionAsync(string connectionId) + { + return _waitForConnectionOpen.GetOrAdd(connectionId, key => new TaskCompletionSource()).Task; + } + + public Task WaitForConnectionCloseAsync(string connectionId) + { + return _waitForConnectionClose.GetOrAdd(connectionId, key => new TaskCompletionSource()).Task; + } + + public Task WaitForApplicationMessageAsync(Type type) + { + return _waitForApplicationMessage.GetOrAdd(type, key => new TaskCompletionSource()).Task; + } + + public Task WaitForServerConnectionAsync(int count) + { + return ConnectionFactory.WaitForConnectionAsync(count); + } + + private Task OnConnectionAsync(ConnectionContext connection) + { + var tcs = new TaskCompletionSource(); + + // Wait for the connection to close + connection.Transport.Input.OnWriterCompleted((ex, state) => + { + tcs.TrySetResult(null); + }, + null); + + return tcs.Task; + } + + public void AddClientConnection(ServiceConnectionContext clientConnection) + { + ClientConnectionManager.AddClientConnection(clientConnection); + + if (_waitForConnectionOpen.TryGetValue(clientConnection.ConnectionId, out var tcs)) + { + tcs.TrySetResult(clientConnection); + } + } + + public void RemoveClientConnection(string connectionId) + { + ClientConnectionManager.RemoveClientConnection(connectionId); + + if (_waitForConnectionClose.TryGetValue(connectionId, out var tcs)) + { + tcs.TrySetResult(null); + } + } + + public ServiceConnectionContext CreateConnection(OpenConnectionMessage message) + { + return new ServiceConnectionContext(message, _clientPipeOptions, _clientPipeOptions); + } + + private void AddApplicationMessage(Type type, ServiceMessage message) + { + if (_waitForApplicationMessage.TryGetValue(type, out var tcs)) + { + tcs.TrySetResult(message); + } + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TaskExtensions.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TaskExtensions.cs new file mode 100644 index 000000000..9f0c00673 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TaskExtensions.cs @@ -0,0 +1,103 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Diagnostics; +using System.IO.Pipelines; +using System.Runtime.CompilerServices; + +namespace System.Threading.Tasks +{ + public static class TaskExtensions + { + private const int DefaultTimeout = 5000; + + public static Task OrTimeout(this Task task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + } + + public static async Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + if (task.IsCompleted) + { + await task; + return; + } + + var cts = new CancellationTokenSource(); + var completed = await Task.WhenAny(task, Task.Delay(Debugger.IsAttached ? Timeout.InfiniteTimeSpan : timeout, cts.Token)); + if (completed != task) + { + throw new TimeoutException(GetMessage(memberName, filePath, lineNumber)); + } + cts.Cancel(); + + await task; + } + + public static Task OrTimeout(this ValueTask task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) => + OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + + public static Task OrTimeout(this ValueTask task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) => + task.AsTask().OrTimeout(timeout, memberName, filePath, lineNumber); + + public static Task OrTimeout(this Task task, int milliseconds = DefaultTimeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + return OrTimeout(task, new TimeSpan(0, 0, 0, 0, milliseconds), memberName, filePath, lineNumber); + } + + public static async Task OrTimeout(this Task task, TimeSpan timeout, [CallerMemberName] string memberName = null, [CallerFilePath] string filePath = null, [CallerLineNumber] int? lineNumber = null) + { + if (task.IsCompleted) + { + return await task; + } + + var cts = new CancellationTokenSource(); + var completed = await Task.WhenAny(task, Task.Delay(Debugger.IsAttached ? Timeout.InfiniteTimeSpan : timeout, cts.Token)); + if (completed != task) + { + throw new TimeoutException(GetMessage(memberName, filePath, lineNumber)); + } + cts.Cancel(); + + return await task; + } + + public static async Task OrThrowIfOtherFails(this Task task, Task otherTask) + { + var completed = await Task.WhenAny(task, otherTask); + if (completed == otherTask && otherTask.IsFaulted) + { + // Manifest the exception + otherTask.GetAwaiter().GetResult(); + throw new Exception("Unreachable code"); + } + else + { + // Await the task we were asked to await. Either it's finished, or the otherTask finished successfully, and it's not our job to check that + await task; + } + } + + public static async Task OrThrowIfOtherFails(this Task task, Task otherTask) + { + await OrThrowIfOtherFails((Task)task, otherTask); + + // If we get here, 'task' is finished and succeeded. + return task.GetAwaiter().GetResult(); + } + + private static string GetMessage(string memberName, string filePath, int? lineNumber) + { + if (!string.IsNullOrEmpty(memberName)) + { + return $"Operation in {memberName} timed out at {filePath}:{lineNumber}"; + } + else + { + return "Operation timed out"; + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnection.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnection.cs new file mode 100644 index 000000000..0af6a901f --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnection.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO.Pipelines; +using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.Http.Features; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class TestConnection : ConnectionContext + { + public TestConnection() + { + Features = new FeatureCollection(); + Items = new ConcurrentDictionary(); + + var pipeOptions = new PipeOptions(); + var pair = DuplexPipe.CreateConnectionPair(pipeOptions, pipeOptions); + Transport = pair.Transport; + Application = pair.Application; + } + + public override string ConnectionId { get; set; } + + public override IFeatureCollection Features { get; } + + public override IDictionary Items { get; set; } + + public override IDuplexPipe Transport { get; set; } + + public IDuplexPipe Application { get; set; } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnectionFactory.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnectionFactory.cs new file mode 100644 index 000000000..96d11a495 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestConnectionFactory.cs @@ -0,0 +1,82 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Connections; + +namespace Microsoft.Azure.SignalR.Tests +{ + internal class TestConnectionFactory : IConnectionFactory + { + private readonly Func _connectCallback; + + private int _connectionCount; + + private readonly ConcurrentDictionary> _waitForConnection = + new ConcurrentDictionary>(); + + public virtual TestConnection CurrentConnectionContext { get; private set; } + + public TestConnectionFactory(Func connectCallback = null) + { + _connectCallback = connectCallback; + } + + public async Task ConnectAsync(TransferFormat transferFormat, string connectionId, + CancellationToken cancellationToken = default) + { + CurrentConnectionContext = null; + + var connection = new TestConnection(); + // Start a task to process handshake request from the newly-created server connection. + _ = HandshakeAsync(connection); + + if (_connectCallback != null) + { + await _connectCallback(connection); + } + + CurrentConnectionContext = connection; + return connection; + } + + public Task DisposeAsync(ConnectionContext connection) + { + return Task.CompletedTask; + } + + private async Task HandshakeAsync(TestConnection connection) + { + await DoHandshakeAsync(connection); + AddConnection(connection); + } + + /// + /// Allow sub-class to override the handshake behavior + /// + protected virtual async Task DoHandshakeAsync(TestConnection connection) + { + await HandshakeUtils.ReceiveHandshakeRequestAsync(connection.Application.Input); + await HandshakeUtils.SendHandshakeResponseAsync(connection.Application.Output); + } + + public Task WaitForConnectionAsync(int connectionCount) + { + return _waitForConnection + .GetOrAdd(connectionCount, key => new TaskCompletionSource()).Task; + } + + private void AddConnection(ConnectionContext connection) + { + var count = Interlocked.Increment(ref _connectionCount); + + if (_waitForConnection.TryGetValue(count, out var tcs)) + { + tcs.TrySetResult(connection); + } + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestHub.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestHub.cs new file mode 100644 index 000000000..2d4129d79 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestHub.cs @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.AspNetCore.SignalR; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class TestHub : Hub + { + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestServiceConnectionManager.cs b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestServiceConnectionManager.cs new file mode 100644 index 000000000..3f86bcadc --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Infrastructure/TestServiceConnectionManager.cs @@ -0,0 +1,52 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Concurrent; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Azure.SignalR.Protocol; + +namespace Microsoft.Azure.SignalR.Tests +{ + internal class TestServiceConnectionManager : IServiceConnectionManager where THub : Hub + { + private readonly ConcurrentDictionary _writeAsyncCallCount = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _partitionedWriteAsyncCallCount = new ConcurrentDictionary(); + + public ServiceMessage ServiceMessage { get; private set; } + + public void AddServiceConnection(ServiceConnection serviceConnection) + { + } + + public async Task StartAsync() + { + await Task.CompletedTask; + } + + public Task WriteAsync(ServiceMessage serviceMessage) + { + _writeAsyncCallCount.AddOrUpdate(serviceMessage.GetType(), 1, (_, value) => value + 1); + ServiceMessage = serviceMessage; + return Task.CompletedTask; + } + + public Task WriteAsync(string partitionKey, ServiceMessage serviceMessage) + { + _partitionedWriteAsyncCallCount.AddOrUpdate(serviceMessage.GetType(), 1, (_, value) => value + 1); + ServiceMessage = serviceMessage; + return Task.CompletedTask; + } + + public int GetCallCount(Type type) + { + return _writeAsyncCallCount.TryGetValue(type, out var count) ? count : 0; + } + + public int GetPartitionedCallCount(Type type) + { + return _partitionedWriteAsyncCallCount.TryGetValue(type, out var count) ? count : 0; + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/Microsoft.Azure.SignalR.Tests.csproj b/test/Microsoft.Azure.SignalR.Tests/Microsoft.Azure.SignalR.Tests.csproj new file mode 100644 index 000000000..e0b16d3b6 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/Microsoft.Azure.SignalR.Tests.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + + + + + + diff --git a/test/Microsoft.Azure.SignalR.Tests/NegotiateHandlerFacts.cs b/test/Microsoft.Azure.SignalR.Tests/NegotiateHandlerFacts.cs new file mode 100644 index 000000000..f2a6b04a6 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/NegotiateHandlerFacts.cs @@ -0,0 +1,151 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class NegotiateHandlerFacts + { + private const string CustomClaimType = "custom.claim"; + private const string CustomUserId = "customUserId"; + private const string DefaultUserId = "nameId"; + private const string DefaultConnectionString = "Endpoint=https://localhost;AccessKey=nOu3jXsHnsO5urMumc87M9skQbUWuQ+PE5IvSUEic8w=;"; + private const string UserPath = "/user/path"; + + private static readonly JwtSecurityTokenHandler JwtSecurityTokenHandler = new JwtSecurityTokenHandler(); + + [Theory] + [InlineData(typeof(CustomUserIdProvider), CustomUserId)] + [InlineData(typeof(NullUserIdProvider), null)] + [InlineData(typeof(DefaultUserIdProvider), DefaultUserId)] + public void GenerateNegotiateResponseWithUserId(Type type, string expectedUserId) + { + var config = new ConfigurationBuilder().Build(); + var serviceProvider = new ServiceCollection().AddSignalR() + .AddAzureSignalR(o => o.ConnectionString = DefaultConnectionString) + .Services + .AddSingleton(config) + .AddSingleton(typeof(IUserIdProvider), type) + .BuildServiceProvider(); + + var httpContext = new DefaultHttpContext + { + User = new ClaimsPrincipal(new ClaimsIdentity(new[] + { + new Claim(CustomClaimType, CustomUserId), + new Claim(ClaimTypes.NameIdentifier, DefaultUserId) + })) + }; + + var handler = serviceProvider.GetRequiredService(); + var negotiateResponse = handler.Process(httpContext, "hub"); + + Assert.NotNull(negotiateResponse); + Assert.NotNull(negotiateResponse.Url); + Assert.NotNull(negotiateResponse.AccessToken); + Assert.Null(negotiateResponse.ConnectionId); + Assert.Empty(negotiateResponse.AvailableTransports); + + var token = JwtSecurityTokenHandler.ReadJwtToken(negotiateResponse.AccessToken); + Assert.Equal(expectedUserId, token.Claims.FirstOrDefault(x => x.Type == Constants.ClaimType.UserId)?.Value); + } + + [Theory] + [InlineData("/user/path/negotiate", "", "asrs.op=%2Fuser%2Fpath")] + [InlineData("/user/path/negotiate/", "", "asrs.op=%2Fuser%2Fpath")] + [InlineData("", "?customKey=customeValue", "customKey=customeValue")] + [InlineData("/user/path/negotiate", "?customKey=customeValue", "asrs.op=%2Fuser%2Fpath&customKey=customeValue")] + public void GenerateNegotiateResponseWithPathAndQuery(string path, string queryString, string expectedQueryString) + { + var config = new ConfigurationBuilder().Build(); + var serviceProvider = new ServiceCollection().AddSignalR() + .AddAzureSignalR(o => o.ConnectionString = DefaultConnectionString) + .Services + .AddSingleton(config) + .BuildServiceProvider(); + + var requestFeature = new HttpRequestFeature + { + Path = path, + QueryString = queryString + }; + var features = new FeatureCollection(); + features.Set(requestFeature); + var httpContext = new DefaultHttpContext(features); + + var handler = serviceProvider.GetRequiredService(); + var negotiateResponse = handler.Process(httpContext, "chat"); + + Assert.NotNull(negotiateResponse); + Assert.EndsWith($"?hub=chat&{expectedQueryString}", negotiateResponse.Url); + } + + [Theory] + [InlineData(typeof(ConnectionIdUserIdProvider), ServiceHubConnectionContext.ConnectionIdUnavailableError)] + [InlineData(typeof(ConnectionAbortedTokenUserIdProvider), ServiceHubConnectionContext.ConnectionAbortedUnavailableError)] + [InlineData(typeof(ItemsUserIdProvider), ServiceHubConnectionContext.ItemsUnavailableError)] + [InlineData(typeof(ProtocolUserIdProvider), ServiceHubConnectionContext.ProtocolUnavailableError)] + public void CustomUserIdProviderAccessUnavailablePropertyThrowsException(Type type, string errorMessage) + { + var config = new ConfigurationBuilder().Build(); + var serviceProvider = new ServiceCollection().AddSignalR() + .AddAzureSignalR(o => o.ConnectionString = DefaultConnectionString) + .Services + .AddSingleton(config) + .AddSingleton(typeof(IUserIdProvider), type) + .BuildServiceProvider(); + + var handler = serviceProvider.GetRequiredService(); + var httpContext = new DefaultHttpContext + { + User = new ClaimsPrincipal() + }; + + var exception = Assert.Throws(() => handler.Process(httpContext, "hub")); + Assert.Equal(errorMessage, exception.Message); + } + + private class CustomUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) + { + return connection.GetHttpContext()?.User?.Claims?.First(c => c.Type == CustomClaimType)?.Value; + } + } + + private class NullUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) => null; + } + + private class ConnectionIdUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) => connection.ConnectionId; + } + + private class ConnectionAbortedTokenUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) => connection.ConnectionAborted.IsCancellationRequested.ToString(); + } + + private class ItemsUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) => connection.Items.ToString(); + } + + private class ProtocolUserIdProvider : IUserIdProvider + { + public string GetUserId(HubConnectionContext connection) => connection.Protocol.Name; + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/ProductInfoFacts.cs b/test/Microsoft.Azure.SignalR.Tests/ProductInfoFacts.cs new file mode 100644 index 000000000..30bd240e9 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/ProductInfoFacts.cs @@ -0,0 +1,18 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class ProductInfoFacts + { + [Fact] + public void GetProductInfo() + { + var productInfo = ProductInfo.GetProductInfo(); + + Assert.NotNull(productInfo); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/ServiceConnectionFacts.cs b/test/Microsoft.Azure.SignalR.Tests/ServiceConnectionFacts.cs new file mode 100644 index 000000000..611a5b7cc --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/ServiceConnectionFacts.cs @@ -0,0 +1,382 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Buffers; +using System.Collections.Generic; +using System.IO.Pipelines; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http.Connections; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Primitives; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class ServiceConnectionFacts + { + private static readonly ServiceProtocol Protocol = new ServiceProtocol(); + + [Fact] + public async Task ServiceConnectionStartsConnection() + { + var connectionId1 = Guid.NewGuid().ToString("N"); + var connectionId2 = Guid.NewGuid().ToString("N"); + + var proxy = new ServiceConnectionProxy(); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + Assert.Empty(proxy.ClientConnectionManager.ClientConnections); + + // Wait for the connection to appear, we need to do this before + // sending the message to avoid races + var connection1Task = proxy.WaitForConnectionAsync(connectionId1); + + // Create a new client connection + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId1, null)); + + var connection1 = await connection1Task.OrTimeout(); + + Assert.Single(proxy.ClientConnectionManager.ClientConnections); + + var httpContext1 = connection1.GetHttpContext(); + Assert.NotNull(httpContext1); + Assert.Empty(httpContext1.Request.Headers); + Assert.Empty(httpContext1.Request.Query); + Assert.Equal(string.Empty, httpContext1.Request.Path); + + // Wait for the connection to appear + var connectionTask2 = proxy.WaitForConnectionAsync(connectionId2); + + // Create another client connection + const string headerKey1 = "custom-header-1"; + const string headerValue1 = "custom-value-1"; + const string headerKey2 = "custom-header-2"; + var headerValue2 = new[] {"custom-value-2a", "custom-value-2b"}; + const string headerKey3 = "custom-header-3"; + var headerValue3 = new[] {"custom-value-3a", "custom-value-3b", "custom-value-3c"}; + const string path = "/this/is/user/path"; + + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId2, null, + new Dictionary + { + {headerKey1, headerValue1}, + {headerKey2, headerValue2}, + {headerKey3, headerValue3} + }, + $"?customQuery1=customValue1&customQuery2=customValue2&{Constants.QueryParameter.OriginalPath}={WebUtility.UrlEncode(path)}")); + + var connection2 = await connectionTask2.OrTimeout(); + + Assert.Equal(2, proxy.ClientConnectionManager.ClientConnections.Count); + + var httpContext2 = connection2.GetHttpContext(); + Assert.NotNull(httpContext2); + Assert.Equal(3, httpContext2.Request.Headers.Count); + Assert.Equal(headerValue1, httpContext2.Request.Headers[headerKey1]); + Assert.Equal(headerValue2, httpContext2.Request.Headers[headerKey2]); + Assert.Equal(headerValue3, httpContext2.Request.Headers[headerKey3]); + Assert.Equal(3, httpContext2.Request.Query.Count); + Assert.Equal("customValue1", httpContext2.Request.Query["customQuery1"]); + Assert.Equal("customValue2", httpContext2.Request.Query["customQuery2"]); + Assert.Equal(path, httpContext2.Request.Query[Constants.QueryParameter.OriginalPath]); + Assert.Equal(path, httpContext2.Request.Path); + + // Send a message to client 1 + await proxy.WriteMessageAsync(new ConnectionDataMessage(connectionId1, Encoding.ASCII.GetBytes("Hello"))); + + var item = await connection1.Transport.Input.ReadSingleAsync().OrTimeout(); + + Assert.Equal("Hello", Encoding.ASCII.GetString(item)); + + var connection1CloseTask = proxy.WaitForConnectionCloseAsync(connectionId1); + + // Close client 1 + await proxy.WriteMessageAsync(new CloseConnectionMessage(connectionId1, null)); + + await connection1CloseTask.OrTimeout(); + + Assert.Single(proxy.ClientConnectionManager.ClientConnections); + + // Close client 2 + var connection2CloseTask = proxy.WaitForConnectionCloseAsync(connectionId2); + + await proxy.WriteMessageAsync(new CloseConnectionMessage(connectionId2, null)); + + await connection2CloseTask.OrTimeout(); + + Assert.Empty(proxy.ClientConnectionManager.ClientConnections); + + proxy.Stop(); + } + + [Fact] + public async Task ClosingConnectionSendsCloseMessage() + { + var proxy = new ServiceConnectionProxy(context => + { + // Just let the connection end immediately + return Task.CompletedTask; + }); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var task = proxy.WaitForConnectionAsync("1"); + + await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null)); + + var connection = await task.OrTimeout(); + + var message = await ReadServiceMessageAsync(proxy.ConnectionContext.Application.Input); + Assert.Equal(message.ConnectionId, connection.ConnectionId); + + proxy.Stop(); + } + + [Fact] + public async Task WritingMessagesFromConnectionGetsSentAsConnectionData() + { + var proxy = new ServiceConnectionProxy(); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var task = proxy.WaitForConnectionAsync("1"); + + await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null)); + + var connection = await task.OrTimeout(); + + await connection.Transport.Output.WriteAsync(Encoding.ASCII.GetBytes("Hello World")); + + var message = await ReadServiceMessageAsync(proxy.ConnectionContext.Application.Input); + Assert.Equal(message.ConnectionId, connection.ConnectionId); + Assert.Equal("Hello World", Encoding.ASCII.GetString(message.Payload.ToArray())); + + proxy.Stop(); + } + + [Fact] + public async Task WritingMultiSegmentMessageConnectionMessageWritesSingleMessage() + { + // 10 byte segment size + var clientPipeOptions = new PipeOptions(minimumSegmentSize: 10); + var proxy = new ServiceConnectionProxy(clientPipeOptions: clientPipeOptions); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var task = proxy.WaitForConnectionAsync("1"); + + await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null)); + + var connection = await task.OrTimeout(); + var outputMessage = "This message should be more than 10 bytes"; + + await connection.Transport.Output.WriteAsync(Encoding.ASCII.GetBytes(outputMessage)); + + var message = await ReadServiceMessageAsync(proxy.ConnectionContext.Application.Input); + Assert.Equal(message.ConnectionId, connection.ConnectionId); + Assert.Equal(outputMessage, Encoding.ASCII.GetString(message.Payload.ToArray())); + + proxy.Stop(); + } + + [Fact] + public async Task ServiceConnectionSendsPingMessage() + { + var proxy = new ServiceConnectionProxy(); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + // Check more than once since it happens periodically + for (int i = 0; i < 2; i++) + { + await ReadServiceMessageAsync(proxy.ConnectionContext.Application.Input, 6000); + } + + proxy.Stop(); + } + + /// + /// When keep-alive is timed out, service connection should start reconnecting to service. + /// + /// + [Fact] + public async Task ReconnectWhenKeepAliveFailed() + { + var proxy = new ServiceConnectionProxy(); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + // Wait for 35s to make the server side timeout + // Assert the server will reconnect + var serverTask2 = proxy.WaitForServerConnectionAsync(2); + Assert.False(Task.WaitAll(new Task[] {serverTask2}, TimeSpan.FromSeconds(1))); + + await Task.Delay(TimeSpan.FromSeconds(35)); + + await serverTask2.OrTimeout(); + } + + /// + /// When having intermittent connectivity failure, service connection should keep reconnecting to service. + /// + [Fact] + public async Task ReconnectWhenHavingIntermittentConnectivityFailure() + { + var connectionFactory = new TestConnectionFactory(new IntermittentConnectivityFailure().ConnectCallback); + var proxy = new ServiceConnectionProxy(connectionFactory: connectionFactory); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var connectionId = Guid.NewGuid().ToString("N"); + + var connectionTask = proxy.WaitForConnectionAsync(connectionId); + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId, null)); + await connectionTask.OrTimeout(); + } + + /// + /// Service connection should stop reconnecting to service after receiving a handshake response with error message. + /// + [Fact] + public async Task StopReconnectAfterReceivingHandshakeErrorMessage() + { + var proxy = new ServiceConnectionProxy(connectionFactory: new TestConnectionFactoryWithHandshakeError()); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var connectionId = Guid.NewGuid().ToString("N"); + var connectionTask = proxy.WaitForConnectionAsync(connectionId); + + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId, null)); + + // Connection exits so the Task should be timeout + Assert.False(Task.WaitAll(new Task[] {connectionTask}, TimeSpan.FromSeconds(1))); + } + + /// + /// Service connection should keep reconnecting to service when receiving invalid handshake response messages intermittently. + /// + [Fact] + public async Task ReconnectWhenHandshakeThrowException() + { + var connectionFactory = new TestConnectionFactory(new IntermittentInvalidHandshakeResponseMessage().ConnectCallback); + var proxy = new ServiceConnectionProxy(connectionFactory: connectionFactory); + + // Throw exception for 3 times and will be success in the 4th retry + var serverTask = proxy.WaitForServerConnectionAsync(4); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + var connectionId = Guid.NewGuid().ToString("N"); + + var connectionTask = proxy.WaitForConnectionAsync(connectionId); + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId, null)); + await connectionTask.OrTimeout(); + } + + /// + /// Service connection should keep reconnecting to service when it is closed after handshake complete. + /// + [Fact] + public async Task ReconnectWhenConnectionThrowException() + { + var proxy = new ServiceConnectionProxy(); + + var serverTask1 = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + var serverConnection1 = await serverTask1.OrTimeout(); + + // Try to wait the second handshake after reconnect + var serverTask2 = proxy.WaitForServerConnectionAsync(2); + Assert.False(Task.WaitAll(new Task[] {serverTask2 }, TimeSpan.FromSeconds(1))); + + // Dispose the connection, then server will throw exception and reconnect + serverConnection1.Transport.Input.CancelPendingRead(); + + await serverTask2.OrTimeout(); + + // Verify the server connection works well + var connectionId = Guid.NewGuid().ToString("N"); + var connectionTask = proxy.WaitForConnectionAsync(connectionId); + await proxy.WriteMessageAsync(new OpenConnectionMessage(connectionId, null)); + + await connectionTask.OrTimeout(); + } + + private static async Task ReadServiceMessageAsync(PipeReader input, int timeout = 5000) + where T : ServiceMessage + { + var data = await input.ReadSingleAsync().OrTimeout(timeout); + var buffer = new ReadOnlySequence(data); + Assert.True(Protocol.TryParseMessage(ref buffer, out var message)); + return Assert.IsType(message); + } + + private class IntermittentConnectivityFailure + { + private const int MaxErrorCount = 3; + + private int _connectCount; + + public Task ConnectCallback(TestConnection connection) + { + // Throw exception to test reconnect + if (_connectCount < MaxErrorCount) + { + _connectCount++; + throw new Exception("Connect error."); + } + + return Task.CompletedTask; + } + } + + private class TestConnectionFactoryWithHandshakeError : TestConnectionFactory + { + protected override async Task DoHandshakeAsync(TestConnection connection) + { + await HandshakeUtils.ReceiveHandshakeRequestAsync(connection.Application.Input); + await HandshakeUtils.SendHandshakeResponseAsync(connection.Application.Output, + new HandshakeResponseMessage("Handshake error.")); + } + } + + private class IntermittentInvalidHandshakeResponseMessage + { + private const int MaxErrorCount = 3; + + private int _connectCount; + + public async Task ConnectCallback(TestConnection connection) + { + // Throw exception to test reconnect + if (_connectCount < MaxErrorCount) + { + _connectCount++; + Protocol.WriteMessage(PingMessage.Instance, connection.Application.Output); + await connection.Application.Output.FlushAsync(); + } + } + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/ServiceContextFacts.cs b/test/Microsoft.Azure.SignalR.Tests/ServiceContextFacts.cs new file mode 100644 index 000000000..0bc965c9d --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/ServiceContextFacts.cs @@ -0,0 +1,141 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Security.Claims; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Primitives; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class ServiceContextFacts + { + private static readonly IDictionary EmptyHeaders = new Dictionary(); + + [Fact] + public void ServiceConnectionContextWithEmptyClaimsIsUnauthenticated() + { + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", new Claim[0])); + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.False(serviceConnectionContext.User.Identity.IsAuthenticated); + } + + [Fact] + public void ServiceConnectionContextWithNullClaimsIsUnauthenticated() + { + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", null)); + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.False(serviceConnectionContext.User.Identity.IsAuthenticated); + } + + [Fact] + public void ServiceConnectionContextWithSystemClaimsIsUnauthenticated() + { + var claims = new[] + { + new Claim("aud", "http://localhost"), + new Claim("exp", "1234567890"), + new Claim("iat", "1234567890"), + new Claim("nbf", "1234567890"), + new Claim(Constants.ClaimType.UserId, "customUserId"), + }; + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", claims)); + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.False(serviceConnectionContext.User.Identity.IsAuthenticated); + } + + [Fact] + public void ServiceConnectionContextWithClaimsCreatesIdentityWithClaims() + { + var claims = new[] + { + new Claim("k1", "v1"), + new Claim("k2", "v2") + }; + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", claims)); + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.True(serviceConnectionContext.User.Identity.IsAuthenticated); + var contextClaims = serviceConnectionContext.User.Claims.ToList(); + Assert.Equal("k1", contextClaims[0].Type); + Assert.Equal("v1", contextClaims[0].Value); + Assert.Equal("k2", contextClaims[1].Type); + Assert.Equal("v2", contextClaims[1].Value); + } + + [Fact] + public void ServiceConnectionContextWithEmptyHttpContextByDefault() + { + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", new Claim[0])); + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.NotNull(serviceConnectionContext.HttpContext); + Assert.Equal(serviceConnectionContext.User, serviceConnectionContext.HttpContext.User); + Assert.Empty(serviceConnectionContext.HttpContext.Request.Headers); + Assert.Empty(serviceConnectionContext.HttpContext.Request.Query); + } + + [Fact] + public void ServiceConnectionContextWithNonEmptyHeaders() + { + const string key1 = "header-key-1"; + const string key2 = "header-key-2"; + const string value1 = "header-value-1"; + var value2 = new[] {"header-value-2a", "header-value-2b"}; + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", new Claim[0], + new Dictionary (StringComparer.OrdinalIgnoreCase) + { + {key1, value1}, + {key2, value2} + }, string.Empty)); + + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.NotNull(serviceConnectionContext.HttpContext); + Assert.Equal(serviceConnectionContext.User, serviceConnectionContext.HttpContext.User); + var request = serviceConnectionContext.HttpContext.Request; + Assert.Equal(2, request.Headers.Count); + Assert.Equal(value1, request.Headers[key1]); + Assert.Equal(value2, request.Headers[key2]); + Assert.Empty(request.Query); + Assert.Equal(string.Empty, request.Path); + } + + [Fact] + public void ServiceConnectionContextWithNonEmptyQueries() + { + const string queryString = "?query1=value1&query2=value2&query3=value3"; + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", new Claim[0], EmptyHeaders, queryString)); + + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.NotNull(serviceConnectionContext.HttpContext); + Assert.Equal(serviceConnectionContext.User, serviceConnectionContext.HttpContext.User); + var request = serviceConnectionContext.HttpContext.Request; + Assert.Empty(request.Headers); + Assert.Equal(queryString, request.QueryString.Value); + Assert.Equal(3, request.Query.Count); + Assert.Equal("value1", request.Query["query1"]); + Assert.Equal("value2", request.Query["query2"]); + Assert.Equal("value3", request.Query["query3"]); + Assert.Equal(string.Empty, request.Path); + } + + [Fact] + public void ServiceConnectionContextWithRequestPath() + { + const string path = "/this/is/user/path"; + var queryString = $"?{Constants.QueryParameter.OriginalPath}={WebUtility.UrlEncode(path)}"; + var serviceConnectionContext = new ServiceConnectionContext(new OpenConnectionMessage("1", null, EmptyHeaders, queryString)); + + Assert.NotNull(serviceConnectionContext.User.Identity); + Assert.NotNull(serviceConnectionContext.HttpContext); + Assert.Equal(serviceConnectionContext.User, serviceConnectionContext.HttpContext.User); + var request = serviceConnectionContext.HttpContext.Request; + Assert.Empty(request.Headers); + Assert.Equal(1, request.Query.Count); + Assert.Equal(path, request.Query[Constants.QueryParameter.OriginalPath]); + Assert.Equal(path, request.Path); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/ServiceEndpointProviderFacts.cs b/test/Microsoft.Azure.SignalR.Tests/ServiceEndpointProviderFacts.cs new file mode 100644 index 000000000..e35b6779c --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/ServiceEndpointProviderFacts.cs @@ -0,0 +1,192 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.IdentityModel.Tokens.Jwt; +using System.Linq; +using System.Security.Claims; +using System.Text; +using Microsoft.Extensions.Options; +using Microsoft.IdentityModel.Tokens; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class ServiceEndpointProviderFacts + { + private const string Endpoint = "https://myendpoint"; + private const string AccessKey = "nOu3jXsHnsO5urMumc87M9skQbUWuQ+PE5IvSUEic8w="; + private static readonly string HubName = nameof(TestHub).ToLower(); + + private static readonly string PreviewConnectionStringWithoutVersion = + $"Endpoint={Endpoint};AccessKey={AccessKey};"; + + private static readonly string PreviewConnectionStringWithVersion = + $"Endpoint={Endpoint};AccessKey={AccessKey};Version=1.0-preview"; + + private static readonly string V1ConnectionString = $"Endpoint={Endpoint};AccessKey={AccessKey};Version=1.0"; + + private static readonly ServiceEndpointProvider[] PreviewEndpointProviderArray = + { + new ServiceEndpointProvider( + Options.Create(new ServiceOptions {ConnectionString = PreviewConnectionStringWithoutVersion})), + new ServiceEndpointProvider( + Options.Create(new ServiceOptions {ConnectionString = PreviewConnectionStringWithVersion})) + }; + + private static readonly IServiceEndpointProvider V1EndpointProvider = + new ServiceEndpointProvider(Options.Create(new ServiceOptions { ConnectionString = V1ConnectionString })); + + private static readonly JwtSecurityTokenHandler JwtSecurityTokenHandler = new JwtSecurityTokenHandler(); + + private static readonly (string path, string queryString, string expectedQuery)[] PathAndQueryArray = + { + ("", "", ""), + (null, "", ""), + ("/user/path", "", $"&{Constants.QueryParameter.OriginalPath}=%2Fuser%2Fpath"), + ("", "customKey=customValue", "&customKey=customValue"), + ("/user/path", "customKey=customValue", $"&{Constants.QueryParameter.OriginalPath}=%2Fuser%2Fpath&customKey=customValue") + }; + + public static IEnumerable PreviewEndpointProviders => + PreviewEndpointProviderArray.Select(provider => new object[] {provider}); + + public static IEnumerable PathAndQueries => + PathAndQueryArray.Select(t => new object[] {t.path, t.queryString, t.expectedQuery}); + + public static IEnumerable PreviewEndpointProvidersWithPath => + from provider in PreviewEndpointProviderArray + from t in PathAndQueryArray + select new object[] { provider, t.path, t.queryString, t.expectedQuery} ; + + [Theory] + [MemberData(nameof(PreviewEndpointProviders))] + internal void GetPreviewServerEndpoint(IServiceEndpointProvider provider) + { + var expected = $"{Endpoint}:5002/server/?hub={HubName}"; + var actual = provider.GetServerEndpoint(); + Assert.Equal(expected, actual); + } + + [Theory] + [MemberData(nameof(PreviewEndpointProvidersWithPath))] + internal void GetPreviewClientEndpoint(IServiceEndpointProvider provider, string path, string queryString, string expectedQueryString) + { + var expected = $"{Endpoint}:5001/client/?hub={HubName}{expectedQueryString}"; + var actual = provider.GetClientEndpoint(HubName, path, queryString); + Assert.Equal(expected, actual); + } + + [Theory] + [MemberData(nameof(PreviewEndpointProviders))] + internal void GeneratePreviewServerAccessToken(IServiceEndpointProvider provider) + { + const string userId = "UserA"; + var tokenString = provider.GenerateServerAccessToken(userId); + var token = JwtSecurityTokenHandler.ReadJwtToken(tokenString); + + var expectedTokenString = GenerateJwtBearer($"{Endpoint}:5002/server/?hub={HubName}", + new[] + { + new Claim(ClaimTypes.NameIdentifier, userId) + }, + token.ValidTo, + token.ValidFrom, + token.ValidFrom, + AccessKey); + + Assert.Equal(expectedTokenString, tokenString); + } + + [Theory] + [MemberData(nameof(PreviewEndpointProviders))] + internal void GeneratePreviewClientAccessToken(IServiceEndpointProvider provider) + { + var tokenString = provider.GenerateClientAccessToken(HubName); + var token = JwtSecurityTokenHandler.ReadJwtToken(tokenString); + + var expectedTokenString = GenerateJwtBearer($"{Endpoint}:5001/client/?hub={HubName}", + null, + token.ValidTo, + token.ValidFrom, + token.ValidFrom, + AccessKey); + + Assert.Equal(expectedTokenString, tokenString); + } + + [Fact] + public void GetV1ServerEndpoint() + { + var expected = $"{Endpoint}/server/?hub={HubName}"; + var actual = V1EndpointProvider.GetServerEndpoint(); + Assert.Equal(expected, actual); + } + + [Theory] + [MemberData(nameof(PathAndQueries))] + public void GetV1ClientEndpoint(string path, string queryString, string expectedQueryString) + { + var expected = $"{Endpoint}/client/?hub={HubName}{expectedQueryString}"; + var actual = V1EndpointProvider.GetClientEndpoint(HubName, path, queryString); + Assert.Equal(expected, actual); + } + + [Fact] + public void GenerateV1ServerAccessToken() + { + const string userId = "UserA"; + var tokenString = V1EndpointProvider.GenerateServerAccessToken(userId); + var token = JwtSecurityTokenHandler.ReadJwtToken(tokenString); + + var expectedTokenString = GenerateJwtBearer($"{Endpoint}/server/?hub={HubName}", + new[] + { + new Claim(ClaimTypes.NameIdentifier, userId) + }, + token.ValidTo, + token.ValidFrom, + token.ValidFrom, + AccessKey); + + Assert.Equal(expectedTokenString, tokenString); + } + + [Fact] + public void GenerateV1ClientAccessToken() + { + var tokenString = V1EndpointProvider.GenerateClientAccessToken(HubName); + var token = JwtSecurityTokenHandler.ReadJwtToken(tokenString); + + var expectedTokenString = GenerateJwtBearer($"{Endpoint}/client/?hub={HubName}", + null, + token.ValidTo, + token.ValidFrom, + token.ValidFrom, + AccessKey); + + Assert.Equal(expectedTokenString, tokenString); + } + + private string GenerateJwtBearer(string audience, + IEnumerable subject, + DateTime expires, + DateTime notBefore, + DateTime issueAt, + string signingKey) + { + var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(signingKey)); + var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); + + return JwtSecurityTokenHandler.WriteToken(JwtSecurityTokenHandler.CreateJwtSecurityToken( + issuer: null, + audience: audience, + subject: subject == null ? null : new ClaimsIdentity(subject), + notBefore: notBefore, + expires: expires, + issuedAt: issueAt, + signingCredentials: credentials)); + } + } +} diff --git a/test/Microsoft.Azure.SignalR.Tests/ServiceLifetimeManagerFacts.cs b/test/Microsoft.Azure.SignalR.Tests/ServiceLifetimeManagerFacts.cs new file mode 100644 index 000000000..041f4d059 --- /dev/null +++ b/test/Microsoft.Azure.SignalR.Tests/ServiceLifetimeManagerFacts.cs @@ -0,0 +1,209 @@ +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.AspNetCore.SignalR.Internal; +using Microsoft.AspNetCore.SignalR.Protocol; +using Microsoft.Azure.SignalR.Protocol; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Xunit; + +namespace Microsoft.Azure.SignalR.Tests +{ + public class ServiceLifetimeManagerFacts + { + private static readonly List TestUsers = new List {"user1", "user2"}; + + private static readonly List TestGroups = new List {"group1", "group2"}; + + private const string TestMethod = "TestMethod"; + + private static readonly object[] TestArgs = {"TestArgs"}; + + private static readonly List TestConnectionIds = new List {"connection1", "connection2"}; + + private static readonly IHubProtocolResolver HubProtocolResolver = + new DefaultHubProtocolResolver(new IHubProtocol[] + { + new JsonHubProtocol(), + new MessagePackHubProtocol() + }, + NullLogger.Instance); + + private static readonly ILogger> Logger = + NullLogger>.Instance; + + private static readonly AzureSignalRMarkerService Marker = new AzureSignalRMarkerService(); + + public ServiceLifetimeManagerFacts() + { + Marker.IsConfigured = true; + } + + [Theory] + [InlineData("SendAllAsync", typeof(BroadcastDataMessage))] + [InlineData("SendAllExceptAsync", typeof(BroadcastDataMessage))] + [InlineData("SendConnectionsAsync", typeof(MultiConnectionDataMessage))] + [InlineData("SendGroupsAsync", typeof(MultiGroupBroadcastDataMessage))] + [InlineData("SendUserAsync", typeof(UserDataMessage))] + [InlineData("SendUsersAsync", typeof(MultiUserDataMessage))] + public async void ServiceLifetimeManagerTest(string functionName, Type type) + { + var serviceConnectionManager = new TestServiceConnectionManager(); + var serviceLifetimeManager = new ServiceLifetimeManager(serviceConnectionManager, + new ClientConnectionManager(), HubProtocolResolver, Logger, Marker); + + await InvokeMethod(serviceLifetimeManager, functionName); + + Assert.Equal(1, serviceConnectionManager.GetCallCount(type)); + VerifyServiceMessage(functionName, serviceConnectionManager.ServiceMessage); + } + + [Theory] + [InlineData("SendGroupAsync", typeof(GroupBroadcastDataMessage))] + [InlineData("SendGroupExceptAsync", typeof(GroupBroadcastDataMessage))] + [InlineData("AddToGroupAsync", typeof(JoinGroupMessage))] + [InlineData("RemoveFromGroupAsync", typeof(LeaveGroupMessage))] + public async void ServiceLifetimeManagerGroupTest(string functionName, Type type) + { + var serviceConnectionManager = new TestServiceConnectionManager(); + var serviceLifetimeManager = new ServiceLifetimeManager(serviceConnectionManager, + new ClientConnectionManager(), HubProtocolResolver, Logger, Marker); + + await InvokeMethod(serviceLifetimeManager, functionName); + + Assert.Equal(1, serviceConnectionManager.GetPartitionedCallCount(type)); + VerifyServiceMessage(functionName, serviceConnectionManager.ServiceMessage); + } + + [Theory] + [InlineData("SendAllAsync", typeof(BroadcastDataMessage))] + [InlineData("SendAllExceptAsync", typeof(BroadcastDataMessage))] + [InlineData("SendConnectionAsync", typeof(MultiConnectionDataMessage))] + [InlineData("SendConnectionsAsync", typeof(MultiConnectionDataMessage))] + [InlineData("SendGroupAsync", typeof(GroupBroadcastDataMessage))] + [InlineData("SendGroupsAsync", typeof(MultiGroupBroadcastDataMessage))] + [InlineData("SendGroupExceptAsync", typeof(GroupBroadcastDataMessage))] + [InlineData("SendUserAsync", typeof(UserDataMessage))] + [InlineData("SendUsersAsync", typeof(MultiUserDataMessage))] + [InlineData("AddToGroupAsync", typeof(JoinGroupMessage))] + [InlineData("RemoveFromGroupAsync", typeof(LeaveGroupMessage))] + public async void ServiceLifetimeManagerIntegrationTest(string methodName, Type messageType) + { + var proxy = new ServiceConnectionProxy(); + + var serviceConnectionManager = new ServiceConnectionManager(); + serviceConnectionManager.AddServiceConnection(proxy.ServiceConnection); + + var serviceLifetimeManager = new ServiceLifetimeManager(serviceConnectionManager, + proxy.ClientConnectionManager, HubProtocolResolver, Logger, Marker); + + var serverTask = proxy.WaitForServerConnectionAsync(1); + _ = proxy.StartAsync(); + await serverTask.OrTimeout(); + + _ = proxy.ProcessApplicationMessagesAsync(); + + var task = proxy.WaitForApplicationMessageAsync(messageType); + + await InvokeMethod(serviceLifetimeManager, methodName); + + var message = await task.OrTimeout(); + + VerifyServiceMessage(methodName, message); + } + + private static async Task InvokeMethod(HubLifetimeManager serviceLifetimeManager, string methodName) + { + switch (methodName) + { + case "SendAllAsync": + await serviceLifetimeManager.SendAllAsync(TestMethod, TestArgs); + break; + case "SendAllExceptAsync": + await serviceLifetimeManager.SendAllExceptAsync(TestMethod, TestArgs, TestConnectionIds); + break; + case "SendConnectionAsync": + await serviceLifetimeManager.SendConnectionAsync(TestConnectionIds[0], TestMethod, TestArgs); + break; + case "SendConnectionsAsync": + await serviceLifetimeManager.SendConnectionsAsync(TestConnectionIds, TestMethod, TestArgs); + break; + case "SendGroupAsync": + await serviceLifetimeManager.SendGroupAsync(TestGroups[0], TestMethod, TestArgs); + break; + case "SendGroupsAsync": + await serviceLifetimeManager.SendGroupsAsync(TestGroups, TestMethod, TestArgs); + break; + case "SendGroupExceptAsync": + await serviceLifetimeManager.SendGroupExceptAsync(TestGroups[0], TestMethod, TestArgs, + TestConnectionIds); + break; + case "SendUserAsync": + await serviceLifetimeManager.SendUserAsync(TestUsers[0], TestMethod, TestArgs); + break; + case "SendUsersAsync": + await serviceLifetimeManager.SendUsersAsync(TestUsers, TestMethod, TestArgs); + break; + case "AddToGroupAsync": + await serviceLifetimeManager.AddToGroupAsync(TestConnectionIds[0], TestGroups[0]); + break; + case "RemoveFromGroupAsync": + await serviceLifetimeManager.RemoveFromGroupAsync(TestConnectionIds[0], TestGroups[0]); + break; + default: + break; + } + } + + private static void VerifyServiceMessage(string methodName, ServiceMessage serviceMessage) + { + switch (methodName) + { + case "SendAllAsync": + Assert.Null(((BroadcastDataMessage) serviceMessage).ExcludedList); + break; + case "SendAllExceptAsync": + Assert.Equal(TestConnectionIds, ((BroadcastDataMessage) serviceMessage).ExcludedList); + break; + case "SendConnectionAsync": + Assert.Equal(TestConnectionIds[0], ((MultiConnectionDataMessage) serviceMessage).ConnectionList[0]); + break; + case "SendConnectionsAsync": + Assert.Equal(TestConnectionIds, ((MultiConnectionDataMessage) serviceMessage).ConnectionList); + break; + case "SendGroupAsync": + Assert.Equal(TestGroups[0], ((GroupBroadcastDataMessage) serviceMessage).GroupName); + Assert.Null(((GroupBroadcastDataMessage) serviceMessage).ExcludedList); + break; + case "SendGroupsAsync": + Assert.Equal(TestGroups, ((MultiGroupBroadcastDataMessage) serviceMessage).GroupList); + break; + case "SendGroupExceptAsync": + Assert.Equal(TestGroups[0], ((GroupBroadcastDataMessage) serviceMessage).GroupName); + Assert.Equal(TestConnectionIds, ((GroupBroadcastDataMessage) serviceMessage).ExcludedList); + break; + case "SendUserAsync": + Assert.Equal(TestUsers[0], ((UserDataMessage) serviceMessage).UserId); + break; + case "SendUsersAsync": + Assert.Equal(TestUsers, ((MultiUserDataMessage) serviceMessage).UserList); + break; + case "AddToGroupAsync": + Assert.Equal(TestConnectionIds[0], ((JoinGroupMessage) serviceMessage).ConnectionId); + Assert.Equal(TestGroups[0], ((JoinGroupMessage) serviceMessage).GroupName); + break; + case "RemoveFromGroupAsync": + Assert.Equal(TestConnectionIds[0], ((LeaveGroupMessage) serviceMessage).ConnectionId); + Assert.Equal(TestGroups[0], ((LeaveGroupMessage) serviceMessage).GroupName); + break; + default: + break; + } + } + } +} diff --git a/version.props b/version.props index 853216d59..516dc6232 100644 --- a/version.props +++ b/version.props @@ -1,7 +1,7 @@ 1.0.0 - preview + preview1 $(VersionPrefix) $(VersionPrefix)-$(VersionSuffix)-final t000 From 80b26d84894245203d56eacd05489abd08bf0300 Mon Sep 17 00:00:00 2001 From: "Liangying.Wei" Date: Fri, 21 Sep 2018 09:28:02 +0800 Subject: [PATCH 2/8] update aspnet signalr version to latest (#213) --- build/dependencies.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/dependencies.props b/build/dependencies.props index 685fca5a3..75b2e06a3 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -17,7 +17,7 @@ 2.1.0 - 2.4.0-preview1-20180919-06 + 2.4.0-preview1-20180920-03 2.1.0 2.1.0 From d4fe91cba5ebcb71d16bc8daae7778a2e1b3689c Mon Sep 17 00:00:00 2001 From: Liangying Wei Date: Fri, 21 Sep 2018 09:47:16 +0800 Subject: [PATCH 3/8] For master branch, show master branch build status --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3d84e8285..5198e97bb 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ This repository contains the open source subset of the .NET SDK. ## Build Status -[![Travis build status](https://img.shields.io/travis/Azure/azure-signalr.svg?label=travis-ci&branch=dev&style=flat-square)](https://travis-ci.org/Azure/azure-signalr/branches) -[![AppVeyor build status](https://img.shields.io/appveyor/ci/vicancy/azure-signalr.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/vicancy/azure-signalr) +[![Travis build status](https://img.shields.io/travis/Azure/azure-signalr.svg?label=travis-ci&branch=master&style=flat-square)](https://travis-ci.org/Azure/azure-signalr/branches) +[![AppVeyor build status](https://img.shields.io/appveyor/ci/vicancy/azure-signalr/master.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/vicancy/azure-signalr) ## Nuget Packages From fad4ce3de9677082eec5b4de907c1e5c2f913030 Mon Sep 17 00:00:00 2001 From: "Liangying.Wei" Date: Fri, 21 Sep 2018 09:52:03 +0800 Subject: [PATCH 4/8] Update aspnet signalr version to latest (#214) * update aspnet signalr version to latest (#213) * For master branch, show master branch build status --- README.md | 4 ++-- build/dependencies.props | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3d84e8285..5198e97bb 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,8 @@ This repository contains the open source subset of the .NET SDK. ## Build Status -[![Travis build status](https://img.shields.io/travis/Azure/azure-signalr.svg?label=travis-ci&branch=dev&style=flat-square)](https://travis-ci.org/Azure/azure-signalr/branches) -[![AppVeyor build status](https://img.shields.io/appveyor/ci/vicancy/azure-signalr.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/vicancy/azure-signalr) +[![Travis build status](https://img.shields.io/travis/Azure/azure-signalr.svg?label=travis-ci&branch=master&style=flat-square)](https://travis-ci.org/Azure/azure-signalr/branches) +[![AppVeyor build status](https://img.shields.io/appveyor/ci/vicancy/azure-signalr/master.svg?label=appveyor&style=flat-square)](https://ci.appveyor.com/project/vicancy/azure-signalr) ## Nuget Packages diff --git a/build/dependencies.props b/build/dependencies.props index 685fca5a3..75b2e06a3 100644 --- a/build/dependencies.props +++ b/build/dependencies.props @@ -17,7 +17,7 @@ 2.1.0 - 2.4.0-preview1-20180919-06 + 2.4.0-preview1-20180920-03 2.1.0 2.1.0 From 4ba072842a835658a5de2eac27c2210022ece19a Mon Sep 17 00:00:00 2001 From: Liangying Wei Date: Fri, 21 Sep 2018 11:05:44 +0800 Subject: [PATCH 5/8] Fix null exception when connection fails to start --- .../Infrastructure/ConnectionFactory.cs | 7 ++++++- .../HubHost/ServiceHubDispatcher.cs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs index edb09f233..585f950c8 100644 --- a/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs +++ b/src/Microsoft.Azure.SignalR.AspNet/Infrastructure/ConnectionFactory.cs @@ -75,7 +75,12 @@ public async Task ConnectAsync(TransferFormat transferFormat, public Task DisposeAsync(ConnectionContext connection) { - return ((HttpConnection)connection).DisposeAsync(); + if (connection != null) + { + return ((HttpConnection)connection).DisposeAsync(); + } + + return Task.CompletedTask; } public Task StartAsync() diff --git a/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs b/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs index 600832f40..d98aad671 100644 --- a/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs +++ b/src/Microsoft.Azure.SignalR/HubHost/ServiceHubDispatcher.cs @@ -110,7 +110,12 @@ public async Task ConnectAsync(TransferFormat transferFormat, public Task DisposeAsync(ConnectionContext connection) { - return ((HttpConnection)connection).DisposeAsync(); + if (connection != null) + { + return ((HttpConnection)connection).DisposeAsync(); + } + + return Task.CompletedTask; } private static class Log From fabd89e647441df1d5a56657aa62797fc411d465 Mon Sep 17 00:00:00 2001 From: "Liangying.Wei" Date: Thu, 24 Jan 2019 16:25:08 +0800 Subject: [PATCH 6/8] Fix the logging not work issue in ASP.NET one (#362) --- .../Internals/OwinConstants.cs | 13 --------- .../Internals/OwinEnvironmentExtensions.cs | 28 ------------------- .../OwinExtensions.cs | 21 ++++++-------- 3 files changed, 8 insertions(+), 54 deletions(-) delete mode 100644 src/Microsoft.Azure.SignalR.AspNet/Internals/OwinConstants.cs delete mode 100644 src/Microsoft.Azure.SignalR.AspNet/Internals/OwinEnvironmentExtensions.cs diff --git a/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinConstants.cs b/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinConstants.cs deleted file mode 100644 index aed6c207c..000000000 --- a/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinConstants.cs +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -namespace Microsoft.AspNet.SignalR -{ - /// - /// Copied from https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Owin/Infrastructure/OwinConstants.cs - /// - internal static class OwinConstants - { - public const string HostTraceOutputKey = "host.TraceOutput"; - } -} diff --git a/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinEnvironmentExtensions.cs b/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinEnvironmentExtensions.cs deleted file mode 100644 index bea9ef283..000000000 --- a/src/Microsoft.Azure.SignalR.AspNet/Internals/OwinEnvironmentExtensions.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.IO; -using System.Reflection; -using System.Threading; - -namespace Microsoft.AspNet.SignalR -{ - /// - /// Copied from https://github.com/SignalR/SignalR/blob/dev/src/Microsoft.AspNet.SignalR.Core/Owin/Infrastructure/OwinEnvironmentExtensions.cs - /// - internal static class OwinEnvironmentExtensions - { - internal static TextWriter GetTraceOutput(this IDictionary environment) - { - object value; - if (environment.TryGetValue(OwinConstants.HostTraceOutputKey, out value)) - { - return value as TextWriter; - } - - return null; - } - } -} diff --git a/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs b/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs index 1ce375898..5146f7aa7 100644 --- a/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs +++ b/src/Microsoft.Azure.SignalR.AspNet/OwinExtensions.cs @@ -207,19 +207,7 @@ private static void RunAzureSignalRCore(IAppBuilder builder, string applicationN SignatureConversions.AddConversions(builder); // ServiceEndpointManager needs the logger - ILoggerFactory loggerFactory; - var traceOutput = builder.Properties.GetTraceOutput(); - if (traceOutput != null) - { - var hostTraceListener = new TextWriterTraceListener(traceOutput); - var traceManager = new TraceManager(hostTraceListener); - loggerFactory = new LoggerFactory(new ILoggerProvider[] { new TraceManagerLoggerProvider(traceManager) }); - resolver.Register(typeof(ILoggerFactory), () => loggerFactory); - } - else - { - loggerFactory = NullLoggerFactory.Instance; - } + var loggerFactory = new LoggerFactory(); var hubs = GetAvailableHubNames(configuration); @@ -233,6 +221,13 @@ private static void RunAzureSignalRCore(IAppBuilder builder, string applicationN builder.RunSignalR(configuration); + // Fetch the trace manager from DI and add logger provider + var traceManager = configuration.Resolver.Resolve(); + if (traceManager != null) + { + loggerFactory.AddProvider(new TraceManagerLoggerProvider(traceManager)); + } + var dispatcher = PrepareAndGetDispatcher(configuration, options, endpoint, router, applicationName, hubs, loggerFactory); if (dispatcher != null) { From cb8c3efba9a57f1d0ab341fa88e7c456db00607d Mon Sep 17 00:00:00 2001 From: Wanpeng Li <38236089+realwanpengli@users.noreply.github.com> Date: Mon, 25 Mar 2019 17:10:55 +0800 Subject: [PATCH 7/8] mark management sdk version as preview (#456) * mark management sdk version as preview * update --- .../Microsoft.Azure.SignalR.Management.csproj | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj b/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj index 00f4751d9..44eeb2373 100644 --- a/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj +++ b/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj @@ -2,6 +2,7 @@ netstandard2.0 + 1.0.0-preview1-$(BuildNumber) @@ -19,4 +20,16 @@ + + + + $(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage + + + + + + + + From 648c61602f8d0bda1aaad33c58670c5039674275 Mon Sep 17 00:00:00 2001 From: Wanpeng Li <38236089+realwanpengli@users.noreply.github.com> Date: Mon, 25 Mar 2019 18:49:27 +0800 Subject: [PATCH 8/8] copy common pkg (#457) --- .../Microsoft.Azure.SignalR.Management.csproj | 2 +- .../Microsoft.Azure.SignalR.Management.Tests.csproj | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj b/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj index 44eeb2373..e0bf93cb4 100644 --- a/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj +++ b/src/Microsoft.Azure.SignalR.Management/Microsoft.Azure.SignalR.Management.csproj @@ -16,7 +16,7 @@ - + diff --git a/test/Microsoft.Azure.SignalR.Management.Tests/Microsoft.Azure.SignalR.Management.Tests.csproj b/test/Microsoft.Azure.SignalR.Management.Tests/Microsoft.Azure.SignalR.Management.Tests.csproj index 4c857b47f..7307802ba 100644 --- a/test/Microsoft.Azure.SignalR.Management.Tests/Microsoft.Azure.SignalR.Management.Tests.csproj +++ b/test/Microsoft.Azure.SignalR.Management.Tests/Microsoft.Azure.SignalR.Management.Tests.csproj @@ -20,6 +20,7 @@ +