Type-Safe Web Programming Using Routed Multiparty Session Types in Typescript
Total Page:16
File Type:pdf, Size:1020Kb
MENG INDIVIDUAL PROJECT IMPERIAL COLLEGE LONDON DEPARTMENT OF COMPUTING Type-safe Web Programming Using Routed Multiparty Session Types in TypeScript Supervisor: Prof. Nobuko Yoshida Author: Anson Miu Second Marker: Dr. Iain Phillips June 18, 2020 Abstract Modern web programming involves coordinating interactions between web browser clients and a web server. Typically, the interactions are informally described, making it difficult to verify communication correctness in web-based distributed systems. Multiparty Session Types (MPST) are a typing discipline for concurrent processes that communicate via message passing. MPST theory can ensure communication safety properties and protocol conformance. Existing work in session-typed web development over WebSocket transport is incompatible with modern web program- ming practices and is limited to supporting communication protocols that implement the server-centric network topology. We address limitations in the current state of the art by: (1) implementing SES- SIONTS, a code generation toolchain for session-typed web development over Web- Socket transport using TypeScript; and (2) presenting ROUTEDSESSIONS, a new mul- tiparty session type theory that supports routed communications. SESSIONTS provides developers with TypeScript APIs generated from a communi- cation protocol specification based on multiparty session type theory. Our work is compatible with modern web programming industrial practices. The generated APIs build upon TypeScript concurrency practices, complement the event-driven style of programming in full-stack web development, and are compatible with the Node.js runtime for server-side endpoints and the React.js framework for browser-side end- points. We evaluate the expressiveness of SESSIONTS for modern web programming using case studies of protocols found in web services, and analyse the performance overhead through running benchmarks against a baseline implementation. ROUTEDSESSIONS can express interactions to be routed via an intermediate partic- ipant. Using ROUTEDSESSIONS, we propose an approach for supporting peer-to-peer communication between browser-side endpoints through routing communication via the server in a way that avoids excessive serialisation and preserves communication safety. We evaluate the correctness of ROUTEDSESSIONS by proving communication safety properties, such as deadlock freedom. Acknowledgements I would like to thank Prof. Nobuko Yoshida, Fangyi Zhou and Dr. Francisco Ferreira for their continuous support, encouragement and motivation during the project. I extend my gratitude to the members of the Mobility Reading Group. Their regular meetings have been a source of inspiration for me, and helped me spark new ideas to incorporate into the project. I would also like to thank Prof. Peter Pietzuch for supporting me as my personal tutor throughout the degree. I would like to thank all the friends I have made throughout my time at Imperial. In particular, to the friends under the namespace labelled Reply 404 at the time of writing: I am forever grateful for every bit of shared memory over the past four years. Finally, I would like to thank my family for their unconditional love and support throughout my life, and encouraging me to pursue a degree abroad. Contents List of Figures5 List of Tables6 Listings7 1 Introduction9 1.1 Motivation.................................9 1.2 Objectives................................. 11 1.3 Contributions............................... 13 2 Background 15 2.1 Session Types............................... 15 2.1.1 Process Calculus......................... 16 2.1.2 Binary Session Types....................... 17 2.1.3 Multiparty Session Types..................... 22 2.2 Related Work............................... 24 2.2.1 Scribble and Endpoint API Generation............. 25 2.2.2 Session Types in Web Development............... 29 2.3 TypeScript................................. 31 I Implementing Server-Centric Topologies over WebSockets 33 3 SESSIONTS: Session Type API Generation for TypeScript 34 3.1 Development Workflow......................... 34 3.1.1 Protocol Specification with Scribble............... 35 3.1.2 From Scribble to EFSM...................... 36 3.1.3 API Generation.......................... 36 3.2 Implementation.............................. 38 3.3 Testing................................... 39 4 NODEMPST: Back-End Session Type Web Development 41 4.1 Challenges................................. 42 4.2 Approach................................. 42 4.3 EFSM Encoding.............................. 42 4.3.1 Roles, Labels, Messages..................... 42 1 CONTENTS 4.3.2 Handler APIs........................... 44 4.3.3 Wrapping Handlers in “Implementations”........... 45 4.4 Runtime.................................. 48 4.4.1 Managing Connections...................... 49 4.4.2 Executing the EFSM....................... 50 4.4.3 Sending Messages........................ 53 4.4.4 Receiving Messages........................ 54 4.4.5 Handling Termination...................... 59 4.5 Alternative Designs............................ 61 4.6 Limitations................................ 62 4.7 Summary................................. 62 5 REACTMPST: Front-End Session Type Web Development 63 5.1 Challenges................................. 64 5.2 Approach................................. 64 5.2.1 The React Framework...................... 65 5.3 EFSM Encoding.............................. 66 5.3.1 Send States............................ 67 5.3.2 Receive States........................... 70 5.3.3 Terminal States.......................... 72 5.4 Runtime.................................. 73 5.4.1 Connecting to the Session.................... 75 5.4.2 Executing the EFSM....................... 75 5.4.3 Sending Messages........................ 76 5.4.4 Receiving Messages........................ 78 5.4.5 Handling Termination...................... 79 5.5 Alternative Designs............................ 79 5.6 Limitations................................ 80 5.7 Summary................................. 80 6 Extensions 81 6.1 Supporting Asynchronous Implementations.............. 81 6.1.1 Motivation............................ 81 6.1.2 API Extension........................... 83 6.1.3 Runtime Extension........................ 84 6.1.4 Limitations............................ 85 6.2 Error Handling.............................. 85 6.2.1 Motivation............................ 86 6.2.2 API Extension........................... 87 6.2.3 Runtime Extension........................ 88 6.2.4 Caveats with Asynchronous Operations............. 92 6.2.5 Limitations............................ 92 2 CONTENTS II Implementing Arbitrary Topologies over WebSockets 93 7 Motivation: Supporting Peer-to-Peer Interactions 94 7.1 TWO BUYER Protocol........................... 94 7.2 Proposal: Server as a Router....................... 95 7.3 Challenges................................. 96 8 ROUTEDSESSIONS: A Theory of Routed Multiparty Session Types 98 8.1 Syntax for Global and Local Types.................... 98 8.1.1 Global Types........................... 98 8.1.2 Local Types............................ 99 8.1.3 Projection............................. 101 8.1.4 Well-formedness......................... 102 8.2 Labelled Transition System (LTS) Semantics.............. 103 8.2.1 LTS Semantics over Global Types................ 104 8.2.2 LTS Semantics over Local Types................. 105 8.3 LTS Soundness and Completeness with respect to Projection..... 107 8.3.1 LTS Semantics over Configurations............... 108 8.3.2 Extending Projection for Configurations............ 109 8.3.3 Trace Equivalence........................ 110 8.3.4 Deadlock Freedom........................ 114 8.4 From Canonical MPST to ROUTEDSESSIONS .............. 120 8.4.1 Router-Parameterised Encoding................. 120 8.4.2 Preserving Well-formedness................... 122 8.4.3 Preserving Communication................... 124 8.5 Summary................................. 127 9 Implementing ROUTEDSESSIONS in SESSIONTS 128 9.1 Extending NODEMPST.......................... 128 9.2 Extending REACTMPST......................... 129 III Evaluation and Conclusion 131 10 Evaluation 132 10.1 Multiparty Sessions: NOUGHTS AND CROSSES ............. 133 10.1.1 Game Server........................... 133 10.1.2 Game Players........................... 135 10.1.3 Summary............................. 135 10.2 Routed Multiparty Sessions: TWO BUYERS ............... 136 10.3 Performance Benchmarks........................ 137 10.3.1 Setup............................... 138 10.3.2 Execution Pattern......................... 140 10.3.3 Overhead............................. 141 10.4 Summary................................. 143 3 CONTENTS 11 Conclusion 145 11.1 Contributions............................... 145 11.2 Future Work................................ 146 Bibliography 148 A Lemmas and Proofs 153 A.1 Lemmas and Proofs for Chapter 8.................... 153 B Artefacts for Evaluation 155 B.1 Implementation for NOUGHTS AND CROSSES .............. 155 B.2 Package Dependencies for Benchmarks................. 156 4 List of Figures 1.1 Message-Passing Architecture of Web-Based Flight Booking Service. 10 2.1 Syntax of Asynchronous π-calculus................... 17 2.2 Syntax of Session Calculus with Branching, Selection and Recursion. 18 2.3 Syntax of Session Types......................... 19 2.4 Syntax of Global Types.......................... 22 2.5 Definition of Merging Operator..................... 23 2.6 Type Checking with Multiparty