├── .editorconfig ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.txt ├── NOTICE.md ├── README.md ├── codecov.yml ├── docs ├── Base-Templates.md └── Events.md ├── examples ├── basic-css-and-js │ ├── index.js │ └── templates │ │ └── index.html ├── basic-typescript │ ├── README.md │ ├── index.ts │ ├── templates │ │ └── index.html │ └── tsconfig.json ├── basic │ ├── index.js │ └── templates │ │ └── index.html └── custom-tags │ ├── index.js │ └── templates │ └── index.html ├── index.d.ts ├── index.js ├── lib ├── errors.js ├── fetch-template.js ├── filter-headers.js ├── fragment.js ├── parse-link-header.js ├── parse-template.js ├── process-fragment-response.js ├── process-template.js ├── request-fragment.js ├── request-handler.js ├── serializer.js ├── streams │ ├── async-stream.js │ ├── buffer-concat-stream.js │ ├── content-length-stream.js │ ├── head-injector-stream.js │ ├── seobots-guard-stream.js │ └── stringifier-stream.js ├── template-cutter.js ├── tracing.js ├── transform.js ├── utils.js └── wait-fragment-responses.js ├── logo ├── README.md ├── tailor-logo.svg └── tailorx-logo.png ├── package.json ├── perf ├── benchmark.js ├── fragment-server.js └── loadtest.js └── tests ├── fetch-template.js ├── filter-headers.js ├── fragment.events.js ├── fragment.js ├── handle-tag.js ├── parse-link-header.js ├── parse-template.js ├── process-template.js ├── request-fragment.js ├── serializer.js ├── streams ├── async-stream.js ├── buffer-concat-stream.js ├── content-length-stream.js ├── header-injector-stream.js ├── seobots-guard-stream.js └── stringifier-stream.js ├── tailor.events.js ├── tailor.js ├── template-cutter.js └── transform.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # For most files 7 | [*] 8 | end_of_line = lf 9 | insert_final_newline = true 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | indent_style = space 13 | indent_size = 4 14 | 15 | # Override specific settings for npm generated json files to avoid big diffs 16 | [package.json] 17 | indent_size = 2 18 | 19 | [*.{yml,yaml}] 20 | indent_size = 2 21 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | src/pipe.min.js 2 | coverage 3 | examples/fragment-performance/hooks.js 4 | examples/fragment-performance/test.js 5 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "parserOptions": { 4 | "sourceType": "script" 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es2020": true, 9 | "node": true 10 | }, 11 | "plugins": [ 12 | "prettier" 13 | ], 14 | "rules": { 15 | "curly": 2, 16 | "no-underscore-dangle": 0, 17 | "no-constant-condition": 2, 18 | "no-dupe-args": 2, 19 | "no-debugger": 2, 20 | "no-duplicate-case": 2, 21 | "no-empty": 2, 22 | "no-empty-character-class": 2, 23 | "no-eval": 2, 24 | "no-unused-vars": 2, 25 | "no-lonely-if": 2, 26 | "quotes": [0, true, "single"], 27 | "strict": [2, "global"], 28 | "prettier/prettier": [2, { 29 | "singleQuote": true, 30 | "tabWidth": 4 31 | }] 32 | }, 33 | "globals": { 34 | "require": false, 35 | "module": false 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | npm-debug.log 4 | /coverage 5 | /.nyc_output 6 | src/pipe.min.js 7 | jsconfig.json 8 | /.idea 9 | package-lock.json 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | cache: 4 | directories: 5 | - node_modules 6 | after_script: 7 | - npm run codecov 8 | stages: 9 | - lint 10 | - test 11 | jobs: 12 | fast_finish: true 13 | allow_failures: 14 | - script: npm run test:frontend 15 | include: 16 | - stage: lint 17 | node_js: 12 18 | script: npm run lint 19 | - stage: test 20 | node_js: 13 21 | script: 22 | - npm run test 23 | - stage: test 24 | node_js: 12 25 | script: 26 | - npm run test 27 | - stage: test 28 | node_js: 14 29 | script: 30 | - npm run coverage 31 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Tailor Changelog 2 | 3 | # 7.0.0 4 | * [#27](https://github.com/StyleT/tailorx/pull/27) CI for Node.js 14 5 | * [#26](https://github.com/StyleT/tailorx/pull/27) (**breaking change**) simplified error handling of `fetchContext(request)` function 6 | 7 | This ensures that it's impossible to accidentally render page with incorrect context. To preserve old behaviour - wrap your 8 | `fetchContext` function with custom error handler which returns `{}`. 9 | Done by replacing `context:error(request, error)` event with `error(request, error, response)`. 10 | 11 | ## 6.1.1 12 | * [#25](https://github.com/StyleT/tailorx/pull/25) Fixed support for custom attributes, see issue at upstream https://github.com/zalando/tailor/issues/287 13 | 14 | ## 6.1.0 15 | * Added `ignore-invalid-ssl` fragment atrribute [#24](https://github.com/StyleT/tailorx/pull/24) 16 | 17 | ## 6.0.0 18 | * Added `processFragmentResponse(response, context): response` option: 19 | * See PR [#23](https://github.com/StyleT/tailorx/pull/23) 20 | * (**breaking change**) Modified `requestFragment(filterHeaders, processFragmentResponse)(url, attributes, request)` option 21 | 22 | ## 5.7.0 23 | * Treat non 2xx http status codes from non-primary fragments as error by default 24 | [#22](https://github.com/StyleT/tailorx/pull/22) 25 | 26 | ## 5.6.0 27 | * `shouldSetPrimaryFragmentAssetsToPreload` option added [#20](https://github.com/StyleT/tailorx/pull/20) 28 | 29 | ## 5.5.0 30 | * Now it's possible to provide links for preload on per-request basis 31 | [#19](https://github.com/StyleT/tailorx/pull/19) 32 | 33 | ## 5.4.0 34 | * Add ignoring special content during parsing to speed up performance 35 | [#18](https://github.com/StyleT/tailorx/pull/18) 36 | 37 | ## 5.3.0 38 | * Memoization of the CPU expensive template parsing added [#17](https://github.com/StyleT/tailorx/pull/17) 39 | 40 | ## 5.2.0 41 | * keep-alive support 42 | 43 | ## 5.1.0 44 | * "forward-querystring" fragment attribute added [#16](https://github.com/StyleT/tailorx/pull/16) 45 | 46 | ## 5.0.0 47 | * See PR [#15](https://github.com/StyleT/tailorx/pull/15) 48 | * (**breaking change**) removal of the frontend logic & Pipe.js 49 | * "fragmentHooks" option added to TailorX 50 | * More advanced `Link` header parsing logic 51 | * (**breaking change**) `amdLoaderUrl()` option was replaced with `getAssetsToPreload()` 52 | 53 | ## 4.1.0 54 | * added injection of the "title" & "meta" tags onto page header [#11](https://github.com/StyleT/tailorx/pull/11) 55 | * better error handing capabilities [#12](https://github.com/StyleT/tailorx/pull/12) 56 | * SeoBotsGuardStream added [#13](https://github.com/StyleT/tailorx/pull/13) 57 | * "return-headers" fragment attribute added [#14](https://github.com/StyleT/tailorx/pull/14) 58 | 59 | ## 4.0.0 60 | * removal of the "fallback-src" attribute support for fragments 61 | 62 | ### 3.11.0 63 | * adding "data-fragment-id" attr to the CSS links for fragments with "id" attribute specified 64 | * support of "invalid" Link headers added 65 | 66 | ### 3.10.0 67 | * Re-branding to TailorX 68 | 69 | ### 3.9.2 70 | * add timeout field to fragment opentracing span([#270](https://github.com/zalando/tailor/pull/270)) 71 | 72 | ### 3.9.1 73 | * handle streams in object mode while buffering([#258](https://github.com/zalando/tailor/pull/258)) 74 | 75 | ### 3.9.0 76 | * (perf) consume streams in parallel and flush them in series([#256](https://github.com/zalando/tailor/pull/256)) 77 | 78 | ### 3.8.0 79 | * change in parent span operation name([#238](https://github.com/zalando/tailor/pull/238)) 80 | * support multiple subscribers on tailor onDone hook([#243](https://github.com/zalando/tailor/pull/243)) 81 | * (perf) Minor optimization on attributes extraction([#247](https://github.com/zalando/tailor/pull/247)) 82 | * (perf) Improve link header extraction logic for fragments([#248](https://github.com/zalando/tailor/pull/248)) 83 | 84 | ### 3.7.1 85 | * handle parsing & primary error properly([#235](https://github.com/zalando/tailor/pull/235)) 86 | * Opentracing - Pass correct span contexts and add tests([#236](https://github.com/zalando/tailor/pull/236)) 87 | 88 | ### 3.7.0 89 | * Added Opentracing Instrumentation([#232](https://github.com/zalando/tailor/pull/232)) 90 | * Added Typescript definitions([#226](https://github.com/zalando/tailor/pull/226)) 91 | * Support asset preloading for aws custom headers([#229](https://github.com/zalando/tailor/pull/229)) 92 | 93 | ### 3.6.0 94 | * Pass all custom fragment attributes to filterRequestHeaders([#209](https://github.com/zalando/tailor/pull/209)) 95 | * Custom API for adding TTFMP from fragments([#214](https://github.com/zalando/tailor/pull/214)) 96 | 97 | ### 3.5.1 98 | 99 | * (fix) - Pipe the AMD loader script from extended options ([#205](https://github.com/zalando/tailor/pull/205)) 100 | 101 | ### 3.5.0 102 | * Parse comment tags without error in child templates ([#195](https://github.com/zalando/tailor/pull/195)) 103 | * Preload the module loader script with HTTP link headers ([#203](https://github.com/zalando/tailor/pull/203)) 104 | 105 | ### 3.4.0 106 | * Fix for handling comment nodes in child tempaltes ([#191](https://github.com/zalando/tailor/pull/191)) 107 | * Two headers (`x-request-uri` & `x-request-host`) are added to the whitelist along with documentation on how to use them ([#192](https://github.com/zalando/tailor/pull/192)) 108 | 109 | ### 3.3.0 110 | * Add API support for custom performance entries([#187](https://github.com/zalando/tailor/pull/187)) 111 | 112 | ### 3.2.1 113 | * End asyncStream later in the process (before piping) ([#185](https://github.com/zalando/tailor/pull/185)) 114 | 115 | ### 3.2.0 116 | * Extract tag handling logic from request handler([#173](https://github.com/zalando/tailor/pull/173)) 117 | * Prettier Integration([#181](https://github.com/zalando/tailor/pull/181)) 118 | * Proper error propagation on template error([#179](https://github.com/zalando/tailor/pull/179)) 119 | * Code coverage improvements([#182](https://github.com/zalando/tailor/pull/182), [#183](https://github.com/zalando/tailor/pull/183)) 120 | 121 | ### 3.1.1 122 | 123 | * Allow file to be used a template instead of directory([#171](https://github.com/zalando/tailor/pull/171)) 124 | * Use `promisify` module to simpify the code([#174](https://github.com/zalando/tailor/pull/174)) 125 | 126 | ### 3.0.1 127 | * Custom performance hooks should be called for all fragments([#168](https://github.com/zalando/tailor/pull/168)) 128 | 129 | ### 3.0.0 130 | * Support for multiple link headers from fragments ([#140](https://github.com/zalando/tailor/pull/140)) 131 | * Update Buffer to Node 8 Syntax ([#154](https://github.com/zalando/tailor/pull/154)) 132 | * Update fragment performance hooks to support multiple link headers ([#159](https://github.com/zalando/tailor/pull/159)) 133 | * Support to forward headers from primary fragment via filterResponseHeaders ([#148](https://github.com/zalando/tailor/pull/148)) 134 | 135 | ##### Contributors 136 | - Aditya Pratap Singh ([addityasingh](https://github.com/addityasingh)) 137 | - Ramiro Rikkert ([rikkert](https://github.com/rikkert)) 138 | - Iilei ([iilei](https://github.com/iilei)) 139 | 140 | ### 2.3.0 141 | * write response headers once before flushing([#145](https://github.com/zalando/tailor/pull/145)) 142 | 143 | ### 2.2.0 144 | * Fix issue with preloading primary fragment assets([#141](https://github.com/zalando/tailor/pull/141)) 145 | 146 | ### 2.1.1 147 | * Opt out of server push for preloaded JS and CSS([#139](https://github.com/zalando/tailor/pull/139)) 148 | 149 | ### 2.1.0 150 | * Fix uglify-js options to preserve implicit return in IIFE ([#133](https://github.com/zalando/tailor/pull/133)) 151 | * Lock down the dependencies version to avoid issues with external libs ([#135](https://github.com/zalando/tailor/pull/135)) 152 | 153 | ### 2.0.2 154 | * Fix preloading headers for crossorigin scripts([#130](https://github.com/zalando/tailor/pull/130)) 155 | 156 | ### 2.0.1 157 | * [Perf] Preload the Primary fragment's assets ([#127](https://github.com/zalando/tailor/issues/127)) 158 | 159 | ### 2.0.0 160 | * Allow Lazy fragment initialization through promise ([#94](https://github.com/zalando/tailor/issues/94)) 161 | * Hooks for measuring performance of fragments initialization on frontend ([#95](https://github.com/zalando/tailor/issues/95)) 162 | * Migrate codebase to ES6 ([#109](https://github.com/zalando/tailor/issues/109)) 163 | * Html compatible for script tags ([#86](https://github.com/zalando/tailor/issues/86)) 164 | * Configurable options for filtering headers ([#91](https://github.com/zalando/tailor/issues/91)) 165 | 166 | ##### Breaking changes 167 | * Dropped node 4.x.x support 168 | * Modified logic for `pipeInstanceName` and `requestFragment`. Please check the [options](https://github.com/zalando/tailor#options) 169 | 170 | ##### Contributors 171 | - Aditya Pratap Singh ([addityasingh](https://github.com/addityasingh)) 172 | - Simeon Cheeseman ([SimeonC](https://github.com/SimeonC)) 173 | - Boopathi Rajaa ([boopathi](https://github.com/boopathi)) 174 | - Dan Peddle ([dazld](https://github.com/dazld)) 175 | - Vignesh Shanmugam ([vigneshshanmugam](https://github.com/vigneshshanmugam)) 176 | 177 | ### 1.1.0 178 | * Support fragment level compression (gzip/deflate) 179 | 180 | ### 1.0.7 181 | * Inline AMD loader if specified as file URL (Performance) 182 | 183 | ### 1.0.6 184 | * Asynchronous file read in built-in fetch 185 | * Respond 404 in case of not found template 186 | 187 | ### 1.0.5 188 | * Add support for fallback slots 189 | 190 | ### 1.0.4 191 | * Fragment initialization metrics 192 | 193 | ### 1.0.3 194 | * Update loadCSS to fix FF 38 crash on Async Fragments. 195 | 196 | ### 1.0.2 197 | * Fix issue related to unnamed slot behaviour 198 | 199 | ### 1.0.1 200 | * Introduced unnamed default slot 201 | 202 | ### 1.0.0 203 | * Introduced HTML compatible parser 204 | * Base templates using slots 205 | * Flattens nested templates 206 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to TailorX 2 | 3 | **Thank you for your interest in making TailorX even better and more awesome. Your contributions are highly welcome.** 4 | 5 | There are multiple ways of getting involved: 6 | 7 | - [Report a bug](#report-a-bug) 8 | - [Suggest a feature](#suggest-a-feature) 9 | - [Contribute code](#contribute-code) 10 | 11 | Below are a few guidelines we would like you to follow. 12 | If you need help, please reach out to one or more of the maintainers. 13 | 14 | ## Report a bug 15 | Reporting bugs is one of the best ways to contribute. Before creating a bug report, please check that an issue reporting the same problem does not already exist. If there is an such an issue, you may add your information as a comment. 16 | 17 | To report a new bug you should open an issue that summarizes the bug and set the label to "bug". 18 | 19 | If you want to provide a fix along with your bug report: That is great! In this case please send us a pull request as described in section [Contribute Code] (#contribute-code). 20 | 21 | ## Suggest a feature 22 | To request a new feature you should open a GitHub issue and summarize the desired functionality and its use case. Set the issue label to "feature". 23 | 24 | ## Contribute code 25 | This is a rough outline of what the workflow for code contributions looks like: 26 | - Check the list of open issues at GitHub. Either assign an existing issue to yourself, or create a new one that you would like work on and discuss your ideas and use cases. 27 | - Fork the repository on GitHub 28 | - Create a topic branch from where you want to base your work. This is usually master. 29 | - Make commits of logical units. 30 | - Write good commit messages (see below). 31 | - Push your changes to a topic branch in your fork of the repository. 32 | - Submit a pull request 33 | - Your pull request must receive a :thumbsup: from two maintainers 34 | 35 | Thanks for your contributions! 36 | 37 | ### Commit messages 38 | Your commit messages ideally can answer two questions: what changed and why. The subject line should feature the “what” and the body of the commit should describe the “why”. 39 | 40 | When creating a pull request, its comment should reference the corresponding issue id. 41 | 42 | **Have fun and enjoy hacking!** 43 | 44 | ## New version release 45 | To release new version of package use the following steps: 46 | ``` 47 | $ npx -p conventional-changelog-angular -p conventional-changelog-preset-loader -p conventional-recommended-bump conventional-recommended-bump --preset angular 48 | $ npm version [major | minor | patch] 49 | # Review last commit 50 | $ git push && git push --tags 51 | $ npm publish 52 | ``` 53 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /NOTICE.md: -------------------------------------------------------------------------------- 1 | This product contains a modified version of Zalando's "node-tailor", 2 | which can be obtained at: 3 | * LICENSE: 4 | * [MIT](https://github.com/zalando/tailor/blob/master/LICENSE) 5 | * HOMEPAGE: 6 | * https://github.com/zalando/tailor 7 | 8 | ------------------------------------------------------------------------------- 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![TailorX logo](./logo/tailorx-logo.png) 2 | 3 | --- 4 | 5 | [![NPM](https://nodei.co/npm/tailorx.png)](https://npmjs.org/package/tailorx) 6 | [![Build Status](https://travis-ci.com/StyleT/tailorx.svg?branch=master)](https://travis-ci.com/StyleT/tailorx) 7 | [![Test Coverage](https://codecov.io/github/StyleT/tailorx/coverage.svg?precision=0)](https://codecov.io/github/StyleT/tailorx) 8 | [![OpenTracing Badge](https://img.shields.io/badge/OpenTracing-enabled-blue.svg)](http://opentracing.io) 9 | 10 | ## npm status 11 | 12 | [![downloads](https://img.shields.io/npm/dt/tailorx.svg)](https://npmjs.org/package/tailorx) 13 | [![version](https://img.shields.io/npm/v/tailorx.svg)](https://npmjs.org/package/tailorx) 14 | 15 | TailorX is a layout service that uses streams to compose a web page from fragment services. 16 | O'Reilly describes it in the title of 17 | [this blog post](https://www.oreilly.com/ideas/better-streaming-layouts-for-frontend-microservices-with-tailor) 18 | as "a library that provides a middleware which you can integrate into any Node.js server." 19 | It's partially inspired by Facebook’s [BigPipe](https://www.facebook.com/notes/facebook-engineering/bigpipe-pipelining-web-pages-for-high-performance/389414033919/) 20 | and based on [Zalando Tailor](https://github.com/zalando/tailor). 21 | 22 | Some of TailorX's features and benefits: 23 | 24 | * **Composes pre-rendered markup on the backend**. This is important for SEO and fastens the initial render. 25 | * **Ensures a fast Time to First Byte**. TailorX requests fragments in parallel and streams them as soon as possible, without blocking the rest of the page. 26 | * **Enforces performance budget**. This is quite challenging otherwise, because there is no single point where you can control performance. 27 | * **Fault Tolerance**. Render the meaningful output, even if a page fragment has failed or timed out. 28 | 29 | TailorX is part of [Isomorphic Layout Composer Project](https://github.com/StyleT/icl), which aims to help developers create microservices for the frontend. If your front-end team is making the monolith-to-microservices transition, you might find TailorX and its available siblings beneficial. 30 | 31 | ## Why a Layout Service? 32 | 33 | Microservices get a lot of traction these days. They allow multiple teams to work independently from each other, choose their own technology stacks and establish their own release cycles. Unfortunately, frontend development hasn’t fully capitalized yet on the benefits that microservices offer. The common practice for building websites remains “the monolith”: a single frontend codebase that consumes multiple APIs. 34 | 35 | What if we could have microservices on the frontend? This would allow frontend developers to work together with their backend counterparts on the same feature and independently deploy parts of the website — “fragments” such as Header, Product, and Footer. Bringing microservices to the frontend requires a layout service that composes a website out of fragments. Tailor was developed to solve this need. 36 | 37 | ## Installation 38 | 39 | Begin using TailorX with: 40 | 41 | ```sh 42 | npm i tailorx 43 | ``` 44 | 45 | ```javascript 46 | const http = require('http'); 47 | const Tailor = require('tailorx'); 48 | const tailor = new Tailor({/* Options */}); 49 | const server = http.createServer(tailor.requestHandler); 50 | server.listen(process.env.PORT || 8080); 51 | ``` 52 | 53 | ## Options 54 | 55 | * `fetchContext(request)` - Function that returns a promise of the context, that is an object that maps fragment id to fragment url, to be able to override urls of the fragments on the page, defaults to `Promise.resolve({})` 56 | * `fetchTemplate(request, parseTemplate)` - Function that should fetch the template, call `parseTemplate` and return a promise of the result. Useful to implement your own way to retrieve and cache the templates, e.g. from s3. 57 | Default implementation [`lib/fetch-template.js`](./lib/fetch-template.js) fetches the template from the file system 58 | * `templatesPath` - To specify the path where the templates are stored locally, Defaults to `/templates/` 59 | * `fragmentTag` - Name of the fragment tag, defaults to `fragment` 60 | * `handledTags` - An array of custom tags, check [`tests/handle-tag`](./tests/handle-tag.js) for more info 61 | * `baseTemplatesCacheSize` - It is off by default. This cache can speed up parsing base templates. You need to specify it as a number of your base templates to cache the parsing of your templates but don't specify it less than the number of templates that your app has because when you are going to change your template several times without server rebooting than all these changed template's versions are going to be saved on your server which is causing cache issues. 62 | * `handleTag(request, tag, options, context)` - Receives a tag or closing tag and serializes it to a string or returns a stream 63 | * `filterRequestHeaders(attributes, request)` - Function that filters the request headers that are passed to fragment request, check default implementation in [`lib/filter-headers`](./lib/filter-headers.js) 64 | * `filterResponseHeaders(attributes, headers)` - Function that maps the given response headers from the primary & `return-headers` fragments to the final response 65 | * `maxAssetLinks` - Number of `Link` Header directives for CSS and JS respected per fragment - defaults to `1` 66 | * `requestFragment(filterHeaders, processFragmentResponse)(url, attributes, request)` - Function that returns a promise of request to a fragment server, check the default implementation in [`lib/request-fragment`](./lib/request-fragment.js) 67 | * `processFragmentResponse(response, context): response` - Function that processes response from the fragment. Returns response or throws an error. Check the default implementation in [`lib/process-fragment-response`](./lib/process-fragment-response.js) 68 | * `tracer` - Opentracing [compliant Tracer implementation](https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/tracer.js~Tracer.html). 69 | * `botsGuardEnabled` - `false` by default. This option forces TailorX to respond with 500 error code even if non-primary fragment fails in case the request comes from SEO/SM bot. 70 | Bot detection is done via [device-detector-js](https://www.npmjs.com/package/device-detector-js). 71 | * `fragmentHooks` - Allows to override default behaviour of the `insertStart` & `insertEnd` hooks & wrap response from the fragment with custom code. 72 | * `insertStart(stream, attributes, headers, index)` 73 | * `insertEnd(stream, attributes, headers, index)` 74 | * `getAssetsToPreload()` - If specified, should return array of assets that should be added to the response `Link` header for preload. 75 | Return value format: `{styleRefs: ['https://ex.com/style.css'], scriptRefs: ['https://ex.com/script.js']}` 76 | * `shouldSetPrimaryFragmentAssetsToPreload` - `true` by default. This option allows or disallows TailorX to set a primary fragment's assets to the response `Link` header for preload. 77 | 78 | ## Template 79 | 80 | TailorX uses [parse5](https://github.com/inikulin/parse5/) to parse the template, where it replaces each `fragmentTag` with a stream from the fragment server and `handledTags` with the result of `handleTag` function. 81 | 82 | ```html 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | ``` 94 | 95 | ### Fragment attributes 96 | 97 | * `id` - optional unique identifier (autogenerated) 98 | * `src` - URL of the fragment 99 | * `primary` - denotes a fragment that sets the response code of the page 100 | * `timeout` - optional timeout of fragment in milliseconds (default is 3000) 101 | * `async` - postpones the fragment until the end of body tag 102 | * `public` - to prevent TailorX from forwarding filtered request headers from upstream to the fragments. 103 | * `return-headers` - makes TailorX to wait for the fragment response headers & send them in response. 104 | Note that they will be merged with headers from `primary` fragment & may be overwritten by it. 105 | * `forward-querystring` - forwards query parameters from the original request down to the fragment 106 | * `ignore-invalid-ssl` - makes TailorX to ignore invalid SSL certificates while requesting a fragment information from an HTTPS server (default is `false`) 107 | 108 | > Other attributes are allowed and will be passed as well to relevant functions (eg. `filterRequestHeaders`, `filterResponseHeaders`, etc.) 109 | 110 | ### Fragment server 111 | 112 | A fragment is an http(s) server that renders only the part of the page and sets `Link`, `x-head-title`, `x-head-meta` 113 | headers (valid only for primary fragment) to provide urls to CSS and JavaScript resources. 114 | 115 | Primary fragment possible response headers: 116 | * `Link` - Check [reference](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link). 117 | * `x-head-title` - Page title encoded with base64. Will be injected onto `` tag. 118 | Ex: `Buffer.from('Page title', 'utf-8').toString('base64')` 119 | * `x-head-meta` - Page [meta tags](https://www.w3schools.com/tags/tag_meta.asp) encoded with base64. 120 | Ex: `Buffer.from('', 'utf-8').toString('base64')` 121 | 122 | Check [`examples/basic-css-and-js/index.js`](./examples/basic-css-and-js/index.js) for a draft implementation. 123 | 124 | A JavaScript of the fragment is an AMD module, that exports an `init` function, that will be called with DOM element of the fragment as an argument. 125 | 126 | TailorX will not follow redirects even if fragment response contains 'Location' Header, that is on purpose as redirects can introduce unwanted latency. Fragments with the attribute `primary` can do a redirect since it controls the status code of the page. 127 | 128 | **Note: For compatability with AWS the `Link` header can also be passed as `x-amz-meta-link`** 129 | 130 | ### Passing information to fragments 131 | 132 | By default, the incoming request will be used to selecting the template. 133 | 134 | So to get the `index.html` template you go to `/index`. 135 | 136 | If you want to listen to `/product/my-product-123` to go to `product.html` template, you can change the `req.url` to `/product`. 137 | 138 | Every header is filtered by default to avoid leaking information, but you can give the original URI and host by adding it to the headers, `x-request-host` and `x-request-uri`, then reading in the fragment the headers to know what product to fetch and display. 139 | 140 | ```javascript 141 | http 142 | .createServer((req, res) => { 143 | req.headers['x-request-uri'] = req.url 144 | req.url = '/index' 145 | 146 | tailor.requestHandler(req, res); 147 | }) 148 | .listen(8080, function() { 149 | console.log('Tailor server listening on port 8080'); 150 | }); 151 | ``` 152 | 153 | ### Concepts 154 | 155 | Some of the concepts in TailorX are described in detail on the specific docs. 156 | 157 | * [Events](./docs/Events.md) 158 | * [Base Templates](./docs/Base-Templates.md) 159 | * [Hooks](./docs/hooks.md) 160 | * [Performance](./docs/Performance.md) 161 | 162 | ## OpenTracing 163 | 164 | TailorX has out of the box distributed tracing instrumentation with [OpenTracing](https://opentracing.io). 165 | It will pick up any span context on the ingress HTTP request and propagate it to the existing 166 | Remote Procedure Calls (RPCs). 167 | 168 | Currently, only the fetching of fragments is instrumented providing some additional details like the 169 | fragment tag, attributes and some logging payload like the stack trace for errors. 170 | 171 | ## Examples 172 | 173 | * Basic - `node examples/basic` 174 | * CSS and JS - `node examples/basic-css-and-js` 175 | * Multiple Fragments and AMD - `node examples/multiple-fragments-with-custom-amd` 176 | * Fragment Performance - `node examples/fragment-performance` 177 | 178 | Go to [http://localhost:8080/index](http://localhost:8080/index) after running the specific example. 179 | 180 | **Note: Please run the examples with node versions > 12.0.0** 181 | 182 | ## Benchmark 183 | 184 | To start running benchmark execute `npm run benchmark` and wait for couple of seconds to see the results. 185 | 186 | ## Contributing 187 | 188 | Please check the Contributing guidelines [here](./CONTRIBUTING.md). 189 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | status: 3 | project: off 4 | patch: off 5 | -------------------------------------------------------------------------------- /docs/Base-Templates.md: -------------------------------------------------------------------------------- 1 | # Base Templates 2 | 3 | Seeing how multiple templates are sharing quite a few commonalities, the need to be able to define a base template arose. 4 | The implemented solution introduces the concept of slots that you define within these templates. Derived templates will use slots as placeholders for their elements. 5 | 6 | * A derived template will only contain fragments and tags. These elements will be used to populate the base template. 7 | * You can assign any number of elements to a slot. 8 | * If a tag is not valid at the position of the slot then it will be appended to the body of the base template. For example, a div tag is not valid in the head. 9 | * If you need to place your fragment in a slot inside the head, you will need to define it 10 | like this ``. 11 | * All fragments and tags that are not assigned to a slot will be appended to the body of the base template. 12 | 13 | *base-template.html* 14 | ```html 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
Hello
25 | 26 | 27 | ``` 28 | 29 | *example-page.html* 30 | 31 | ```html 32 | 33 | 34 | 35 | Test Template 36 | ``` 37 | 38 | The rendered html output will look like this 39 | ```html 40 | 41 | 42 | 43 | 44 | 45 | 46 | Test Template 47 | 48 | 49 | 50 |
Hello
51 | 52 | 53 | 54 | ``` 55 | -------------------------------------------------------------------------------- /docs/Events.md: -------------------------------------------------------------------------------- 1 | # Events 2 | 3 | `Tailor` extends `EventEmitter`, so you can subscribe to events with `tailor.on('eventName', callback)`. 4 | 5 | Events may be used for logging and monitoring. Check [perf/benchmark.js](https://github.com/zalando/tailor/blob/master/perf/benchmark.js#L28) for an example of getting metrics from Tailor. 6 | 7 | ## Top level events 8 | 9 | * Client request received: `start(request)` 10 | * Response started (headers flushed and stream connected to output): `response(request, status, headers)` 11 | * Response ended (with the total size of response): `end(request, contentSize)` 12 | * Error: `error(request, error)` in case an error from template (parsing,fetching) and primary error(socket/timeout/50x) 13 | May be invoked with 2 signatures: 14 | * `error(request, error)` 15 | * `error(request, error, response)` - if you received event with this signature you must write response, TailorX will do nothing on it's side 16 | 17 | ## Fragment events 18 | 19 | * Request start: `fragment:start(request, fragment.attributes)` 20 | * Response Start when headers received: `fragment:response(request, fragment.attributes, status, headers)` 21 | * Response End (with response size): `fragment:end(request, fragment.attributes, contentSize)` 22 | * Error: `fragment:error(request, fragment.attributes, error)` in case of socket error, timeout, 50x 23 | 24 | 25 | **Note:** `fragment:response`, `fragment:fallback` and `fragment:error` are mutually exclusive. `fragment:end` happens only in case of successful response. 26 | -------------------------------------------------------------------------------- /examples/basic-css-and-js/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | const Tailor = require('../../'); 5 | const tailor = new Tailor({ 6 | templatesPath: __dirname + '/templates' 7 | }); 8 | 9 | // Root Server 10 | http.createServer((req, res) => { 11 | if (req.url === '/favicon.ico') { 12 | res.writeHead(200, { 'Content-Type': 'image/x-icon' }); 13 | return res.end(''); 14 | } 15 | tailor.requestHandler(req, res); 16 | }).listen(8080, function() { 17 | console.log('Tailor server listening on port 8080'); 18 | }); 19 | 20 | // Fragment server - Any http server that can serve fragments 21 | http.createServer((req, res) => { 22 | // just some js 23 | if (req.url === '/script.js') { 24 | res.setHeader('Content-Type', 'application/javascript'); 25 | return res.end( 26 | 'c=!setInterval(\'document.getElementById("c").innerHTML=c++;\', 1e3)' 27 | ); 28 | } 29 | 30 | // and some css 31 | if (req.url === '/styles.css') { 32 | res.setHeader('Content-Type', 'text/css'); 33 | return res.end('body { background: #303F9F; color: white }'); 34 | } 35 | 36 | // Every Fragment sends a link header that describes its resources - css and js 37 | const css = '; rel="stylesheet"'; 38 | // this will be fetched using require-js as an amd module 39 | const js = '; rel="fragment-script"'; 40 | 41 | res.writeHead(200, { 42 | Link: `${css}, ${js}`, 43 | 'Content-Type': 'text/html' 44 | }); 45 | 46 | // fragment content 47 | res.end('
Fragment 1: -1s elapsed
'); 48 | }).listen(8081, function() { 49 | console.log('Fragment Server listening on port 8081'); 50 | }); 51 | -------------------------------------------------------------------------------- /examples/basic-css-and-js/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Basic css and js

3 | 4 | -------------------------------------------------------------------------------- /examples/basic-typescript/README.md: -------------------------------------------------------------------------------- 1 | # Basis-typescript 2 | 3 | This example shows how to use Tailor with Typescript 4 | 5 | ## Run 6 | 7 | Basically there 2 ways to use node with typescript. 8 | In both cases types will be checked and from external point of view they should behave identically. 9 | 10 | ### With ts-node 11 | 12 | Install ts-node and just run this file: 13 | 14 | ```bash 15 | ts-node ./index.ts 16 | ``` 17 | 18 | ### Transpile to javascript 19 | 20 | Install TypeScript compiler and execute following; 21 | 22 | ```bash 23 | tsc ./index.ts 24 | # Now we have index.js and can run it with node.js 25 | node ./index.js 26 | ``` -------------------------------------------------------------------------------- /examples/basic-typescript/index.ts: -------------------------------------------------------------------------------- 1 | import * as http from 'http'; 2 | 3 | import Tailor = require('../../index'); 4 | 5 | const tailor = new Tailor({ 6 | templatesPath: __dirname + '/templates' 7 | }) 8 | 9 | // Root Server 10 | http 11 | .createServer((req, res) => { 12 | tailor.requestHandler(req, res) 13 | }) 14 | .listen(8080, function() { 15 | console.log('Tailor server listening on port 8080'); 16 | }); 17 | 18 | // Fragment server - Any http server that can serve fragments 19 | http 20 | .createServer((req, res) => { 21 | res.writeHead(200, { 22 | 'Content-Type': 'text/html' 23 | }); 24 | res.end('
Fragment 1
'); 25 | }) 26 | .listen(8081, function() { 27 | console.log('Fragment Server listening on port 8081'); 28 | }); 29 | -------------------------------------------------------------------------------- /examples/basic-typescript/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Basic

3 | 4 | -------------------------------------------------------------------------------- /examples/basic-typescript/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "lib": [ 5 | "es6" 6 | ], 7 | "noImplicitAny": true, 8 | "noImplicitThis": true, 9 | "strictNullChecks": false, 10 | "strictFunctionTypes": true, 11 | "baseUrl": "../", 12 | "typeRoots": [ 13 | "../../" 14 | ], 15 | "types": [], 16 | "noEmit": true, 17 | "forceConsistentCasingInFileNames": true 18 | }, 19 | "files": [ 20 | "./index.ts", 21 | "./../../index.d.ts" 22 | ] 23 | } -------------------------------------------------------------------------------- /examples/basic/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | 5 | const Tailor = require('../../'); 6 | 7 | const tailor = new Tailor({ 8 | templatesPath: __dirname + '/templates' 9 | // The place to define a custom Opentracing tracer like Jaeger, for ex. 10 | // tracer: initTracer(config, options) 11 | }); 12 | 13 | // Root Server 14 | http.createServer((req, res) => { 15 | tailor.requestHandler(req, res); 16 | }).listen(8080, function() { 17 | console.log('Tailor server listening on port 8080'); 18 | }); 19 | 20 | // Fragment server - Any http server that can serve fragments 21 | http.createServer((req, res) => { 22 | res.writeHead(200, { 23 | 'Content-Type': 'text/html' 24 | }); 25 | res.end('
Fragment 1
'); 26 | }).listen(8081, function() { 27 | console.log('Fragment Server listening on port 8081'); 28 | }); 29 | -------------------------------------------------------------------------------- /examples/basic/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 |

Basic

3 | 4 | -------------------------------------------------------------------------------- /examples/custom-tags/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | const path = require('path'); 5 | const url = require('url'); 6 | const processTemplate = require('../../lib/process-template'); 7 | 8 | const Tailor = require('../../'); 9 | 10 | const tailor = new Tailor({ 11 | templatesPath: path.join(__dirname, 'templates'), 12 | handledTags: ['switcher'], 13 | handleTag: (request, tag, options, context) => { 14 | if (tag && tag.name === 'switcher') { 15 | const st = processTemplate(request, options, context); 16 | 17 | const finalSrc = tag.attributes['final-src']; 18 | http.get( 19 | `http://localhost:8081/switcher?nesting=${tag.attributes.nesting}&final_src=${finalSrc}`, 20 | res => { 21 | let data = ''; 22 | res.on('data', chunk => { 23 | data += chunk; 24 | }); 25 | res.on('end', () => { 26 | options 27 | .parseTemplate(data, null, false) 28 | .then(parsedTemplate => { 29 | parsedTemplate.forEach(item => { 30 | st.write(item); 31 | }); 32 | st.end(); 33 | }); 34 | }); 35 | } 36 | ); 37 | return st; 38 | } 39 | 40 | return ''; 41 | } 42 | }); 43 | 44 | // Root Server 45 | http.createServer((req, res) => { 46 | if (req.url === '/favicon.ico') { 47 | res.writeHead(200, { 'Content-Type': 'image/x-icon' }); 48 | return res.end(''); 49 | } 50 | tailor.requestHandler(req, res); 51 | }).listen(8080, function() { 52 | console.log('Tailor server listening on port 8080'); 53 | }); 54 | 55 | // Fragment server - Any http server that can serve fragments 56 | http.createServer((req, res) => { 57 | const urlObj = url.parse(req.url, true); 58 | 59 | if (urlObj.pathname === '/switcher') { 60 | res.setHeader('Content-Type', 'text/html'); 61 | const { nesting, final_src } = urlObj.query; 62 | const currentNesting = parseInt(nesting); 63 | 64 | console.log('Request to switcher with nesting "%s"', currentNesting); 65 | 66 | if (currentNesting === 0) { 67 | return res.end(``); 68 | } else { 69 | return res.end(` 70 |
Switcher ${currentNesting}
71 | 73 | `); 74 | } 75 | } 76 | 77 | res.writeHead(200, { 'Content-Type': 'text/html' }); 78 | 79 | // fragment content 80 | const name = urlObj.query.name; 81 | res.end(`
Fragment, name: ${name}
`); 82 | }).listen(8081, function() { 83 | console.log('Fragment Server listening on port 8081'); 84 | }); 85 | -------------------------------------------------------------------------------- /examples/custom-tags/templates/index.html: -------------------------------------------------------------------------------- 1 |

Example of custom complex tag (switcher)

2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | 3 | import { EventEmitter } from "events"; 4 | import { Stream } from "stream"; 5 | import { Url } from "url"; 6 | import { IncomingMessage, ServerResponse } from "http"; 7 | import { Span, Tracer } from 'opentracing'; 8 | 9 | export = Tailor; 10 | 11 | declare class Tailor extends EventEmitter { 12 | /** 13 | * Creates new instance of Tailor 14 | * @param options Parameters to pass to Tailor 15 | * @param options.amdLoaderUrl URL to AMD loader. Default is RequireJS from cdnjs. 16 | * @param options.fetchContext Function that should fetch the template, call parseTemplate and return a promise of the result. Useful to implement your own way to retrieve and cache the templates. Default implementation: serve templates from local path 17 | * @param options.fetchTemplate Function that should fetch the template, call parseTemplate and return a promise of the result 18 | * @param options.filterRequestHeaders Function that filters the request headers that are passed to fragment request 19 | * @param options.filterResponseHeaders Function that maps the given response headers from the primary fragment request to the final response 20 | * @param options.fragmentTag Name of fragment tag 21 | * @param options.handleTags Array of custom tags. 22 | * @param options.handleTag Receives a tag or closing tag and serializes it to a string or returns as stream 23 | * @param options.maxAssetLinks Number of allowed link headers of CSS and JS for per fragment 24 | * @param options.pipeAttributes Function that returns the minimal set of fragment attributes available on the frontend 25 | * @param options.pipeInstanceName Name of pipe instance that available in the browser window object to consume frontend hooks 26 | * @param options.requestFragment Function that returns a promise of request to a fragment server 27 | * @param options.templatesPath Path to local templates 28 | * @param options.tracer Opentracing compliant Tracer implementation 29 | */ 30 | constructor(options?: { 31 | amdLoaderUrl?: string 32 | , fetchContext?: (req: IncomingMessage) => Promise 33 | , fetchTemplate?: (req: IncomingMessage, parseTemplate: ParseTemplateFunction) => Promise 34 | , filterRequestHeaders?: (attributes: Attributes, req: IncomingMessage) => object 35 | , filterResponseHeaders?: (attributes: Attributes, res: ServerResponse) => object 36 | , fragmentTag?: string 37 | , handledTags?: string[] 38 | , handleTag?: (request: IncomingMessage, tag: object, options: object, context: object) => Stream | string 39 | , maxAssetLinks?: number 40 | , pipeAttributes?: (attributes: Attributes) => object 41 | , pipeInstanceName?: string 42 | , requestFragment?: (filterHeaders: (attributes: Attributes, req: IncomingMessage) => object, url: Url, attributes: Attributes, req: IncomingMessage, span?: Span) => Promise 43 | , templatesPath?: string 44 | , tracer?: Tracer 45 | }) 46 | 47 | requestHandler(request: IncomingMessage, response: ServerResponse): void; 48 | } 49 | 50 | interface Attributes { 51 | id: string, 52 | src: string 53 | async?: boolean 54 | primary?: boolean 55 | public?: boolean 56 | [key: string]: any 57 | } 58 | 59 | type ParseTemplateFunction = (handledTags: string[], insertBeforePipeTags: string[]) => ( 60 | baseTemplate: string, 61 | childTemplate: string, 62 | fullRendering: boolean, 63 | ) => Promise; 64 | 65 | 66 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const EventEmitter = require('events').EventEmitter; 5 | const requestHandler = require('./lib/request-handler'); 6 | const fetchTemplate = require('./lib/fetch-template'); 7 | const parseTemplate = require('./lib/parse-template'); 8 | const requestFragment = require('./lib/request-fragment'); 9 | const filterReqHeadersFn = require('./lib/filter-headers'); 10 | const processFragmentResponseFn = require('./lib/process-fragment-response'); 11 | const { initTracer } = require('./lib/tracing'); 12 | 13 | module.exports = class Tailor extends EventEmitter { 14 | constructor(options) { 15 | super(); 16 | const { 17 | filterRequestHeaders = options.filterHeaders || filterReqHeadersFn, 18 | processFragmentResponse = options.processFragmentResponse || 19 | processFragmentResponseFn, 20 | maxAssetLinks, 21 | templatesPath 22 | } = options; 23 | 24 | options.maxAssetLinks = isNaN(maxAssetLinks) 25 | ? 1 26 | : Math.max(1, maxAssetLinks); 27 | 28 | const requestOptions = Object.assign( 29 | { 30 | fetchContext: () => Promise.resolve({}), 31 | fetchTemplate: fetchTemplate( 32 | templatesPath || path.join(process.cwd(), 'templates') 33 | ), 34 | fragmentTag: 'fragment', 35 | handledTags: [], 36 | baseTemplatesCacheSize: 0, 37 | handleTag: () => '', 38 | requestFragment: requestFragment( 39 | filterRequestHeaders, 40 | processFragmentResponse 41 | ), 42 | botsGuardEnabled: false, 43 | fragmentHooks: {}, 44 | getAssetsToPreload: async () => ({ 45 | styleRefs: [], 46 | scriptRefs: [] 47 | }), 48 | shouldSetPrimaryFragmentAssetsToPreload: true 49 | }, 50 | options 51 | ); 52 | 53 | initTracer(options.tracer); 54 | 55 | requestOptions.parseTemplate = parseTemplate( 56 | [requestOptions.fragmentTag].concat(requestOptions.handledTags), 57 | requestOptions.baseTemplatesCacheSize 58 | ); 59 | 60 | this.requestHandler = requestHandler.bind(this, requestOptions); 61 | } 62 | }; 63 | -------------------------------------------------------------------------------- /lib/errors.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const extendError = require('@namecheap/error-extender'); 4 | 5 | const errors = {}; 6 | errors.TailorError = extendError('TailorError'); 7 | errors.FragmentError = extendError('FragmentError', { 8 | parent: errors.TailorError 9 | }); 10 | errors.FragmentWarn = extendError('FragmentWarn', { 11 | parent: errors.TailorError 12 | }); 13 | 14 | module.exports = Object.freeze(errors); 15 | -------------------------------------------------------------------------------- /lib/fetch-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const path = require('path'); 4 | const url = require('url'); 5 | const fs = require('fs'); 6 | const promisify = require('util.promisify'); 7 | 8 | const TEMPLATE_ERROR = 0; 9 | const TEMPLATE_NOT_FOUND = 1; 10 | 11 | class TemplateError extends Error { 12 | constructor(...args) { 13 | super(...args); 14 | this.code = TEMPLATE_ERROR; 15 | this.presentable = 'template error'; 16 | const [{ code }] = args; 17 | 18 | if (code === 'ENOENT') { 19 | this.code = TEMPLATE_NOT_FOUND; 20 | this.presentable = 'template not found'; 21 | } 22 | } 23 | } 24 | 25 | /** 26 | * Promisify the functions 27 | */ 28 | const readFilePromise = promisify(fs.readFile); 29 | const lstatPromise = promisify(fs.lstat); 30 | 31 | /** 32 | * Read the file from File System 33 | * 34 | * @param {string} path 35 | */ 36 | const readFile = path => 37 | readFilePromise(path, 'utf-8').catch(err => { 38 | throw new TemplateError(err); 39 | }); 40 | 41 | /** 42 | * Returns the template path validating a exactly file or a directory 43 | * 44 | * @param {String} templatesPath - TemplatesPath config 45 | * @param {String} pathname - Path name based on Request Object 46 | * 47 | * @return {Promise} Template Info object on success or TemplateError on fail 48 | */ 49 | const getTemplatePath = (templatesPath, pathname) => 50 | new Promise((resolve, reject) => { 51 | lstatPromise(templatesPath).then( 52 | data => { 53 | let templateStat = { 54 | isFile: data.isFile() 55 | }; 56 | 57 | if (templateStat.isFile) { 58 | templateStat.path = templatesPath; 59 | } else { 60 | templateStat.path = factoryFilePath( 61 | templatesPath, 62 | pathname 63 | ); 64 | } 65 | 66 | return resolve(templateStat); 67 | }, 68 | err => { 69 | return reject(new TemplateError(err)); 70 | } 71 | ); 72 | }); 73 | 74 | /** 75 | * Returns pathname by request 76 | * 77 | * @param {Object} request - Request Object 78 | * 79 | * @return {String} pathname 80 | */ 81 | const getPathName = request => url.parse(request.url, true).pathname; 82 | 83 | /** 84 | * Factory the complete file path 85 | * 86 | * @param {String} templatesPath - Templates dir path 87 | * @param {String} filename - file name without extension 88 | * 89 | * @return {String} complete file path 90 | */ 91 | const factoryFilePath = (templatesPath, filename) => 92 | `${path.join(templatesPath, filename)}.html`; 93 | 94 | /** 95 | * Fetches the template from File System 96 | * 97 | * @param {string} templatesPath - The path where the templates are stored 98 | * @param {function=} baseTemplateFn - Function that returns the Base template name for a given page 99 | */ 100 | module.exports = (templatesPath, baseTemplateFn) => ( 101 | request, 102 | parseTemplate 103 | ) => { 104 | const pathname = getPathName(request); 105 | 106 | return getTemplatePath(templatesPath, pathname).then(templateStat => { 107 | return readFile(templateStat.path).then(baseTemplate => { 108 | if (templateStat.isFile || typeof baseTemplateFn !== 'function') { 109 | return parseTemplate(baseTemplate); 110 | } 111 | 112 | const templateName = baseTemplateFn(pathname); 113 | if (!templateName) { 114 | return parseTemplate(baseTemplate); 115 | } 116 | 117 | const pageTemplate = baseTemplate; 118 | const baseTemplatePath = factoryFilePath( 119 | templatesPath, 120 | templateName 121 | ); 122 | return readFile(baseTemplatePath).then(baseTemplate => 123 | parseTemplate(baseTemplate, pageTemplate) 124 | ); 125 | }); 126 | }); 127 | }; 128 | 129 | module.exports.TEMPLATE_ERROR = TEMPLATE_ERROR; 130 | module.exports.TEMPLATE_NOT_FOUND = TEMPLATE_NOT_FOUND; 131 | -------------------------------------------------------------------------------- /lib/filter-headers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const ACCEPT_HEADERS = [ 3 | 'accept-language', 4 | 'referer', 5 | 'user-agent', 6 | 'x-request-uri', 7 | 'x-request-host' 8 | ]; 9 | 10 | /** 11 | * Filter the request headers that are passed to fragment request. 12 | * @callback filterHeaders 13 | * 14 | * @param {Object} attributes - Attributes object of the fragment node 15 | * @param {string} attributes.public - Denotes the public fragment. 16 | * @param {Object} request - HTTP Request object 17 | * @param {Object} request.headers - request header object 18 | * @returns {Object} New filtered header object 19 | */ 20 | module.exports = (attributes, request) => { 21 | const { public: isPublic } = attributes; 22 | const { headers = {} } = request; 23 | // Headers are not forwarded to public fragment for security reasons 24 | return isPublic 25 | ? {} 26 | : ACCEPT_HEADERS.reduce((newHeaders, key) => { 27 | headers[key] && (newHeaders[key] = headers[key]); 28 | return newHeaders; 29 | }, {}); 30 | }; 31 | -------------------------------------------------------------------------------- /lib/fragment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const EventEmitter = require('events').EventEmitter; 4 | const PassThrough = require('stream').PassThrough; 5 | const zlib = require('zlib'); 6 | const ContentLengthStream = require('./streams/content-length-stream'); 7 | const parseLinkHeader = require('./parse-link-header'); 8 | 9 | const { globalTracer, Tags } = require('opentracing'); 10 | const tracer = globalTracer(); 11 | 12 | const hasValue = value => { 13 | if (value || value === '') { 14 | return true; 15 | } 16 | return false; 17 | }; 18 | 19 | const getFragmentAssetUris = (refs, assetSize) => { 20 | const scriptUris = []; 21 | const styleUris = []; 22 | 23 | for (const ref of refs) { 24 | if (ref.rel === 'fragment-script') { 25 | scriptUris.push(ref.uri); 26 | } else if (ref.rel === 'stylesheet') { 27 | styleUris.push(ref.uri); 28 | } 29 | } 30 | return [scriptUris.slice(0, assetSize), styleUris.slice(0, assetSize)]; 31 | }; 32 | 33 | /** 34 | * Merge the attributes based on the fragment tag attributes and context 35 | * 36 | * @param {object} tag - Fragment tag from the template 37 | * @param {object=} context - Context object for the given fragment 38 | * @returns {object} 39 | */ 40 | const getAttributes = (tag, context) => { 41 | const attributes = Object.assign({}, tag.attributes); 42 | const fragmentId = attributes.id; 43 | 44 | if (context && fragmentId && context[fragmentId]) { 45 | const fragmentCtxt = context[fragmentId]; 46 | Object.assign(attributes, fragmentCtxt); 47 | } 48 | 49 | const { 50 | src, 51 | async: isAsync, 52 | primary, 53 | public: isPublic, 54 | timeout, 55 | 'return-headers': returnHeaders, 56 | 'forward-querystring': forwardQuerystring, 57 | 'ignore-invalid-ssl': ignoreInvalidSsl, 58 | ...rest 59 | } = attributes; 60 | 61 | return { 62 | ...rest, 63 | url: src, 64 | id: fragmentId, 65 | async: hasValue(isAsync), 66 | primary: hasValue(primary), 67 | public: hasValue(isPublic), 68 | timeout: parseInt(timeout || 3000, 10), 69 | returnHeaders: hasValue(returnHeaders), 70 | forwardQuerystring: hasValue(forwardQuerystring), 71 | ignoreInvalidSsl: hasValue(ignoreInvalidSsl) 72 | }; 73 | }; 74 | 75 | /** 76 | * Class representing a Fragment 77 | * @extends EventEmitter 78 | */ 79 | module.exports = class Fragment extends EventEmitter { 80 | /** 81 | * Create a Fragment 82 | * @param {Object} tag - Fragment tag from the template 83 | * @param {object} context - Context object for the given fragment 84 | * @param {number} index - Order of the fragment 85 | * @param {function} requestFragment - Function to request the fragment 86 | * @param {number} maxAssetLinks - Number of `Link` Header directives for CSS and JS respected per fragment 87 | * @param {object} fragmentHooks 88 | * @param {function} fragmentHooks.insertStart 89 | * @param {function} fragmentHooks.insertEnd 90 | */ 91 | constructor({ 92 | tag, 93 | context, 94 | index, 95 | requestFragment, 96 | maxAssetLinks, 97 | fragmentHooks = {} 98 | } = {}) { 99 | super(); 100 | this.attributes = getAttributes(tag, context); 101 | this.index = index; 102 | this.maxAssetLinks = maxAssetLinks; 103 | this.requestFragment = requestFragment; 104 | this.fragmentHooks = fragmentHooks; 105 | this.stream = new PassThrough(); 106 | this.scriptRefs = []; 107 | this.styleRefs = []; 108 | } 109 | 110 | /** 111 | * Handles fetching the fragment 112 | * @param {object} request - HTTP request stream 113 | * @param {object} parentSpan - opentracing Span that will be the parent of the current operation 114 | * @returns {object} Fragment response streams in case of synchronous fragment or buffer in case of async fragment 115 | */ 116 | fetch(request, parentSpan = null) { 117 | this.emit('start'); 118 | 119 | let url = this.attributes.url; 120 | 121 | const spanOptions = parentSpan ? { childOf: parentSpan } : {}; 122 | const span = tracer.startSpan('fetch_fragment', spanOptions); 123 | 124 | const { 125 | id, 126 | primary, 127 | async: isAsync, 128 | public: isPublic, 129 | timeout 130 | } = this.attributes; 131 | 132 | span.addTags({ 133 | [Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_CLIENT, 134 | [Tags.HTTP_URL]: url, 135 | public: isPublic, 136 | async: isAsync, 137 | id: id || 'unnamed', 138 | primary, 139 | timeout 140 | }); 141 | 142 | if (this.attributes.forwardQuerystring) { 143 | const origUrl = new URL('http://fake' + request.url); 144 | const fragmentUrl = new URL(url); 145 | 146 | origUrl.searchParams.forEach((v, k) => { 147 | if (fragmentUrl.searchParams.has(k)) { 148 | return; 149 | } 150 | 151 | fragmentUrl.searchParams.append(k, v); 152 | }); 153 | 154 | url = fragmentUrl.toString(); 155 | } 156 | 157 | this.requestFragment(url, this.attributes, request, span).then( 158 | res => this.onResponse(res, span), 159 | err => { 160 | span.setTag(Tags.ERROR, true); 161 | span.log({ message: err.message }); 162 | this.emit('error', err); 163 | this.stream.end(); 164 | span.finish(); 165 | } 166 | ); 167 | // Async fragments are piped later on the page 168 | if (isAsync) { 169 | //TODO: to be implemented 170 | return Buffer.from( 171 | '' 172 | ); 173 | } 174 | return this.stream; 175 | } 176 | 177 | /** 178 | * Handle the fragment response 179 | * @param {object} response - HTTP response stream from fragment 180 | * @param {object} span - fetch-fragment opentracing span 181 | */ 182 | onResponse(response, span) { 183 | const { statusCode, headers } = response; 184 | 185 | // Extract the assets from fragment link headers. 186 | const refs = parseLinkHeader( 187 | [headers.link, headers['x-amz-meta-link']].join(',') 188 | ); 189 | 190 | if (refs.length > 0) { 191 | [this.scriptRefs, this.styleRefs] = getFragmentAssetUris( 192 | refs, 193 | this.maxAssetLinks 194 | ); 195 | } 196 | 197 | this.emit('response', statusCode, headers); 198 | 199 | this.insertStart(headers); //TODO: add error handling for this hook, now it causes UnhandledPromiseRejection 200 | 201 | const contentLengthStream = new ContentLengthStream(contentLength => { 202 | this.emit('end', contentLength); 203 | }); 204 | 205 | contentLengthStream.on('end', () => { 206 | this.insertEnd(headers); //TODO: add error handling for this hook, now it causes UnhandledPromiseRejection 207 | this.stream.end(); 208 | span.finish(); 209 | }); 210 | 211 | const handleError = err => { 212 | this.emit('warn', err); 213 | span.setTag(Tags.ERROR, true); 214 | span.log({ message: err.message }); 215 | contentLengthStream.end(); 216 | }; 217 | 218 | // Handle errors on all piped streams 219 | response.on('error', handleError); 220 | contentLengthStream.on('error', handleError); 221 | 222 | // Unzip the fragment response if gzipped before piping it to the Client(Browser) - Composition will break otherwise 223 | let responseStream = response; 224 | const contentEncoding = headers['content-encoding']; 225 | if ( 226 | contentEncoding && 227 | (contentEncoding === 'gzip' || contentEncoding === 'deflate') 228 | ) { 229 | let unzipStream = zlib.createUnzip(); 230 | unzipStream.on('error', handleError); 231 | responseStream = response.pipe(unzipStream); 232 | } 233 | 234 | responseStream 235 | .pipe(contentLengthStream) 236 | .pipe(this.stream, { end: false }); 237 | } 238 | 239 | /** 240 | * Insert the placeholder for pipe assets and load the required JS and CSS assets at the start of fragment stream 241 | * 242 | * - JS assets are loading via AMD(requirejs) for both sync and async fragments 243 | * - CSS for the async fragments are loaded using custom loadCSS(available in src/pipe.js) 244 | */ 245 | insertStart(headers) { 246 | const { async: isAsync, id } = this.attributes; 247 | 248 | this.stream.write( 249 | `` 252 | ); 253 | 254 | if (this.fragmentHooks.insertStart) { 255 | this.fragmentHooks.insertStart( 256 | this.stream, 257 | this.attributes, 258 | headers, 259 | this.index 260 | ); 261 | } else { 262 | this.styleRefs.forEach(uri => { 263 | this.stream.write( 264 | isAsync 265 | ? `` 266 | : `` 271 | ); 272 | }); 273 | } 274 | } 275 | 276 | /** 277 | * Insert the placeholder for pipe assets at the end of fragment stream 278 | */ 279 | insertEnd(headers) { 280 | const { id } = this.attributes; 281 | 282 | if (this.fragmentHooks.insertEnd) { 283 | this.fragmentHooks.insertEnd( 284 | this.stream, 285 | this.attributes, 286 | headers, 287 | this.index 288 | ); 289 | } else { 290 | this.scriptRefs.reverse().forEach(uri => { 291 | this.stream.write( 292 | `` 295 | ); 296 | }); 297 | } 298 | 299 | this.stream.write( 300 | `` 303 | ); 304 | } 305 | }; 306 | -------------------------------------------------------------------------------- /lib/parse-link-header.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /** 3 | * Parse link headers 4 | * '; rel="fragment-script"' 5 | * 6 | * [ 7 | * { 8 | * rel: "fragment-script", 9 | * uri: "http://localhost:8080/script.js" 10 | * } 11 | * ] 12 | * 13 | * Based on code from parse-link-header! 14 | * https://github.com/thlorenz/parse-link-header/blob/master/index.js 15 | */ 16 | module.exports = function parseLinkHeader(linkHeader) { 17 | return fixLink(linkHeader) 18 | .split(/,\s* { 20 | const match = link.match(/]*)>(.*)/); 21 | if (!match) { 22 | return null; 23 | } 24 | const linkUrl = match[1]; 25 | return { 26 | uri: linkUrl, 27 | rel: getRelValue(match[2]), 28 | params: getParams(match[2]) 29 | }; 30 | }) 31 | .filter(v => v && v.rel != null) 32 | .reduce((acc, curr) => { 33 | return acc.concat(curr); 34 | }, []); 35 | }; 36 | 37 | /** 38 | * Get the value of rel attribute 39 | * 40 | * rel="fragment-script" -> ["rel", "fragment-script"] 41 | */ 42 | function getRelValue(parts) { 43 | const m = parts.match(/\s*rel\s*=\s*"?([^"]+)"?/); 44 | if (!m) { 45 | return null; 46 | } 47 | return m[1]; 48 | } 49 | 50 | /** 51 | * Get the params of rel attribute 52 | * 53 | * rel="fragment-script" -> ["rel", "fragment-script"] 54 | */ 55 | function getParams(parts) { 56 | return parts.split(';').reduce((acc, attribute) => { 57 | let [key, value] = attribute.trim().split('='); 58 | key = key.trim(); 59 | 60 | if (value === undefined || key === 'rel') { 61 | return acc; 62 | } 63 | 64 | acc[key] = value 65 | .trim() 66 | .replace(/(^"|"$)/g, '') 67 | .trim(); 68 | 69 | return acc; 70 | }, {}); 71 | } 72 | 73 | function fixLink(headerLink) { 74 | return headerLink 75 | .split(',') 76 | .map(link => { 77 | return link 78 | .split(';') 79 | .map((attribute, index) => { 80 | if (index) { 81 | const [key, value] = attribute.trim().split('='); 82 | return !value || value.trim().startsWith('"') 83 | ? attribute 84 | : `${key}="${value}"`; 85 | } else { 86 | return !attribute || attribute.trim().startsWith('<') 87 | ? attribute 88 | : `<${attribute}>`; 89 | } 90 | }) 91 | .join(';'); 92 | }) 93 | .join(','); 94 | } 95 | -------------------------------------------------------------------------------- /lib/parse-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Transform = require('./transform'); 4 | const TemplateCutter = require('./template-cutter'); 5 | 6 | /** 7 | * Parse both base and child templates 8 | * 9 | * @param {Array} handledTags - Tags that should be treated specially and will be handled in the future 10 | * @param {Number} baseTemplatesCacheSize - You can limit base templates cache size which you want to keep, it relates to how many templates you have in your app 11 | * @returns {Promise} Promise that resolves to a serialized array consisting of buffer and fragment objects 12 | */ 13 | module.exports = (handledTags, baseTemplatesCacheSize) => { 14 | const transform = new Transform(handledTags, baseTemplatesCacheSize); 15 | 16 | return (baseTemplate, childTemplate, fullRendering = true) => 17 | Promise.resolve().then(() => { 18 | const templateCutter = new TemplateCutter(); 19 | 20 | const cuttedBaseTemplate = templateCutter.cut(baseTemplate); 21 | 22 | const chunks = transform.applyTransforms( 23 | cuttedBaseTemplate, 24 | childTemplate, 25 | fullRendering 26 | ); 27 | 28 | return templateCutter.restore(chunks); 29 | }); 30 | }; 31 | -------------------------------------------------------------------------------- /lib/process-fragment-response.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {http.ServerResponse} response - fragment response 5 | * @param {Object} context - contextual info about request 6 | * @param {http.IncomingMessage} context.request - incoming request from browser 7 | * @param {Object} context.fragmentAttributes - fragment attributes map 8 | * @param {String} context.fragmentUrl - URL that was requested on fragment 9 | */ 10 | module.exports = (response, context) => { 11 | const isError500 = response.statusCode >= 500; 12 | const isNonPrimaryAndNon200 = 13 | (response.statusCode < 200 || response.statusCode >= 300) && 14 | !context.fragmentAttributes.primary; 15 | 16 | if (isError500 || isNonPrimaryAndNon200) { 17 | throw new Error( 18 | `Request fragment error. statusCode: ${response.statusCode}; statusMessage: ${response.statusMessage}; url: ${context.fragmentUrl};` 19 | ); 20 | } 21 | 22 | return response; 23 | }; 24 | -------------------------------------------------------------------------------- /lib/process-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const StringifierStream = require('./streams/stringifier-stream'); 4 | const Fragment = require('./fragment'); 5 | /** 6 | * Process the parsed template and handle the composition of fragments in the template 7 | * using stringifier stream 8 | * 9 | * @param {Object} request - HTTP Request object 10 | * @param {Object} options - Tailor options 11 | * @param {Object} context - Dynamic overrides for fragments 12 | * 13 | * @return {Object} Composition of fragment streams - StringifierStream 14 | */ 15 | module.exports = function processTemplate(request, options, context) { 16 | const { 17 | maxAssetLinks, 18 | asyncStream, 19 | handleTag, 20 | requestFragment, 21 | fragmentTag, 22 | nextIndex, 23 | fragmentHooks, 24 | 25 | parentSpan 26 | } = options; 27 | 28 | const resultStream = new StringifierStream(tag => { 29 | const { placeholder, name } = tag; 30 | 31 | const fetchFragment = context => { 32 | const fragment = new Fragment({ 33 | tag, 34 | index: nextIndex(), 35 | context, 36 | requestFragment, 37 | maxAssetLinks, 38 | fragmentHooks 39 | }); 40 | 41 | resultStream.emit('fragment:found', fragment); 42 | 43 | const { async } = fragment.attributes; 44 | 45 | if (async) { 46 | asyncStream.write(fragment.stream); 47 | } 48 | 49 | return fragment.fetch(request, false, parentSpan); 50 | }; 51 | 52 | if (placeholder === 'async') { 53 | // end of body tag 54 | return asyncStream; 55 | } 56 | 57 | if (name === fragmentTag) { 58 | return fetchFragment(context); 59 | } 60 | 61 | return handleTag(request, tag, options, context); 62 | }); 63 | 64 | return resultStream; 65 | }; 66 | -------------------------------------------------------------------------------- /lib/request-fragment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | const https = require('https'); 5 | const url = require('url'); 6 | const Agent = require('agentkeepalive'); 7 | const HttpsAgent = require('agentkeepalive').HttpsAgent; 8 | const { globalTracer, FORMAT_HTTP_HEADERS } = require('opentracing'); 9 | const tracer = globalTracer(); 10 | 11 | // By default tailor supports gzipped response from fragments 12 | const requiredHeaders = { 13 | 'accept-encoding': 'gzip, deflate' 14 | }; 15 | 16 | const kaAgent = new Agent(); 17 | const kaAgentHttps = new HttpsAgent(); 18 | 19 | /** 20 | * Simple Request Promise Function that requests the fragment server with 21 | * - filtered headers 22 | * - Specified timeout from fragment attributes 23 | * 24 | * @param {filterHeaders} - Function that handles the header forwarding 25 | * @param {processFragmentResponse} - Function that handles response processing 26 | * @param {string} fragmentUrl - URL of the fragment server 27 | * @param {Object} fragmentAttributes - Attributes passed via fragment tags 28 | * @param {Object} request - HTTP request stream 29 | * @param {Object} span - opentracing span context passed for propagation 30 | * @returns {Promise} Response from the fragment server 31 | */ 32 | module.exports = (filterHeaders, processFragmentResponse) => ( 33 | fragmentUrl, 34 | fragmentAttributes, 35 | request, 36 | span = null 37 | ) => 38 | new Promise((resolve, reject) => { 39 | const parsedUrl = url.parse(fragmentUrl); 40 | const options = Object.assign( 41 | { 42 | headers: Object.assign( 43 | filterHeaders(fragmentAttributes, request), 44 | requiredHeaders 45 | ), 46 | timeout: fragmentAttributes.timeout 47 | }, 48 | parsedUrl 49 | ); 50 | 51 | if (span) { 52 | tracer.inject(span.context(), FORMAT_HTTP_HEADERS, options.headers); 53 | } 54 | 55 | const { protocol: reqProtocol, timeout } = options; 56 | const hasHttpsProtocol = reqProtocol === 'https:'; 57 | const protocol = hasHttpsProtocol ? https : http; 58 | options.agent = hasHttpsProtocol ? kaAgentHttps : kaAgent; 59 | 60 | if (hasHttpsProtocol && fragmentAttributes.ignoreInvalidSsl) { 61 | options.rejectUnauthorized = false; 62 | } 63 | 64 | const fragmentRequest = protocol.request(options); 65 | 66 | if (timeout) { 67 | fragmentRequest.setTimeout(timeout, fragmentRequest.abort); 68 | } 69 | 70 | fragmentRequest.on('response', response => { 71 | try { 72 | resolve( 73 | processFragmentResponse(response, { 74 | request, 75 | fragmentUrl, 76 | fragmentAttributes 77 | }) 78 | ); 79 | } catch (e) { 80 | reject(e); 81 | } 82 | }); 83 | fragmentRequest.on('error', reject); 84 | fragmentRequest.end(); 85 | }); 86 | -------------------------------------------------------------------------------- /lib/request-handler.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const errors = require('./errors'); 4 | 5 | const AsyncStream = require('./streams/async-stream'); 6 | const ContentLengthStream = require('./streams/content-length-stream'); 7 | const HeadInjectorStream = require('./streams/head-injector-stream'); 8 | const BotsGuardStream = require('./streams/seobots-guard-stream'); 9 | const processTemplate = require('./process-template'); 10 | const { 11 | getFragmentAssetsToPreload, 12 | nextIndexGenerator, 13 | assignHeaders 14 | } = require('./utils'); 15 | const WaitForFragmentResponses = require('./wait-fragment-responses'); 16 | 17 | const { globalTracer, Tags, FORMAT_HTTP_HEADERS } = require('opentracing'); 18 | const tracer = globalTracer(); 19 | 20 | // Events emitted by fragments on the template 21 | const FRAGMENT_EVENTS = [ 22 | 'start', 23 | 'response', 24 | 'end', 25 | 'error', 26 | 'timeout', 27 | 'warn' 28 | ]; 29 | // Occurs when Template parsing fails/Primary Fragment Errors out 30 | const INTERNAL_SERVER_ERROR = 'Internal Server Error'; 31 | 32 | /** 33 | * Process the HTTP Request to the Tailor Middleware 34 | * 35 | * @param {Object} options - Options object passed to Tailor 36 | * @param {Object} request - HTTP request stream of Middleware 37 | * @param {Object} response - HTTP response stream of middleware 38 | */ 39 | module.exports = function processRequest(options, request, response) { 40 | this.emit('start', request); 41 | const parentSpanContext = tracer.extract( 42 | FORMAT_HTTP_HEADERS, 43 | request.headers 44 | ); 45 | const spanOptions = parentSpanContext ? { childOf: parentSpanContext } : {}; 46 | const span = tracer.startSpan('compose_page', spanOptions); 47 | span.addTags({ 48 | [Tags.HTTP_URL]: request.url, 49 | [Tags.SPAN_KIND]: Tags.SPAN_KIND_RPC_SERVER 50 | }); 51 | 52 | const { 53 | fetchContext, 54 | fetchTemplate, 55 | parseTemplate, 56 | filterResponseHeaders, 57 | maxAssetLinks, 58 | getAssetsToPreload, 59 | botsGuardEnabled, 60 | shouldSetPrimaryFragmentAssetsToPreload 61 | } = options; 62 | 63 | const waitFragmentsRes = new WaitForFragmentResponses(); 64 | 65 | const asyncStream = new AsyncStream(); 66 | asyncStream.once('plugged', () => { 67 | asyncStream.end(); 68 | }); 69 | 70 | const contextPromise = fetchContext(request); 71 | const templatePromise = fetchTemplate(request, parseTemplate); 72 | const responseHeaders = { 73 | // set default headers if they are provided. 74 | // it's necessary since ILC sets to response "setHeader('Set-Cookie', 'ilc-i18n')". 75 | // So since "setHeader" values are overridden with "writeHead" value, then as result we can lose "ilc-i18n" in case of setting 'Set-Cookie' header by some fragment. 76 | ...response.getHeaders(), 77 | // Disable cache in browsers and proxies 78 | 'Cache-Control': 'no-cache, no-store, must-revalidate', 79 | Pragma: 'no-cache', 80 | 'Content-Type': 'text/html' 81 | }; 82 | 83 | let shouldWriteHead = true; 84 | 85 | const contentLengthStream = new ContentLengthStream(contentLength => { 86 | this.emit('end', request, contentLength); 87 | span.finish(); 88 | }); 89 | 90 | const botGuard = new BotsGuardStream( 91 | botsGuardEnabled, 92 | request.headers, 93 | response 94 | ); 95 | botGuard.on('error', err => { 96 | shouldWriteHead = false; 97 | handleError(err); 98 | }); 99 | 100 | const handleError = err => { 101 | span.setTag(Tags.ERROR, true); 102 | span.log({ message: err.message, stack: err.stack }); 103 | 104 | if (shouldWriteHead) { 105 | shouldWriteHead = false; 106 | 107 | if (this.listeners('error').length > 0) { 108 | this.emit('error', request, err, response); 109 | } else { 110 | response.writeHead(500, responseHeaders); 111 | if (typeof err.presentable === 'string') { 112 | response.end(`${err.presentable}`); 113 | } else { 114 | response.end(INTERNAL_SERVER_ERROR); 115 | } 116 | } 117 | 118 | span.setTag(Tags.HTTP_STATUS_CODE, 500); 119 | 120 | span.finish(); 121 | } else { 122 | if (this.listeners('error').length > 0) { 123 | this.emit('error', request, err); 124 | } 125 | 126 | contentLengthStream.end(); 127 | } 128 | }; 129 | 130 | const handlePrimaryFragment = (fragment, resultStream) => { 131 | if (!shouldWriteHead) { 132 | return; 133 | } 134 | 135 | shouldWriteHead = false; 136 | 137 | fragment.once('response', async (statusCode, headers) => { 138 | // Map response headers 139 | if (typeof filterResponseHeaders === 'function') { 140 | (await waitFragmentsRes.all()).forEach(v => 141 | assignHeaders(responseHeaders, filterResponseHeaders(...v)) 142 | ); 143 | 144 | assignHeaders( 145 | responseHeaders, 146 | filterResponseHeaders(fragment.attributes, headers) 147 | ); 148 | } 149 | 150 | if (headers.location) { 151 | responseHeaders.location = headers.location; 152 | } 153 | 154 | // Make resources early discoverable while processing HTML 155 | const configAssets = await getAssetsToPreload(request); 156 | let assetsToPreload = getFragmentAssetsToPreload( 157 | configAssets.styleRefs || [], 158 | configAssets.scriptRefs || [], 159 | request.headers 160 | ); 161 | 162 | if (shouldSetPrimaryFragmentAssetsToPreload) { 163 | assetsToPreload = assetsToPreload.concat( 164 | getFragmentAssetsToPreload( 165 | fragment.styleRefs, 166 | fragment.scriptRefs, 167 | request.headers 168 | ) 169 | ); 170 | } 171 | 172 | responseHeaders.link = assetsToPreload.join(','); 173 | this.emit('response', request, statusCode, responseHeaders); 174 | 175 | const headInjector = new HeadInjectorStream(headers); 176 | 177 | resultStream.writeHead(statusCode, responseHeaders); 178 | resultStream 179 | .pipe(headInjector) 180 | .pipe(contentLengthStream) 181 | .pipe(response); 182 | }); 183 | 184 | fragment.once('error', origErr => { 185 | const err = new errors.FragmentError({ 186 | message: `Fragment error for "${fragment.attributes.id}"`, 187 | cause: origErr, 188 | data: { fragmentAttrs: fragment.attributes } 189 | }); 190 | 191 | span.addTags({ 192 | [Tags.ERROR]: true, 193 | [Tags.HTTP_STATUS_CODE]: 500 194 | }); 195 | span.log({ 196 | message: err.message, 197 | stack: err.stack 198 | }); 199 | 200 | if (this.listeners('error').length > 0) { 201 | this.emit('error', request, err, response); 202 | } else { 203 | response.writeHead(500, responseHeaders); 204 | response.end(INTERNAL_SERVER_ERROR); 205 | } 206 | 207 | span.finish(); 208 | }); 209 | }; 210 | 211 | Promise.all([templatePromise, contextPromise]) 212 | .then(([parsedTemplate, context]) => { 213 | // extendedOptions are mutated inside processTemplate 214 | const extendedOptions = Object.assign({}, options, { 215 | nextIndex: nextIndexGenerator(0, maxAssetLinks), 216 | parentSpan: span, 217 | asyncStream 218 | }); 219 | 220 | const resultStream = processTemplate( 221 | request, 222 | extendedOptions, 223 | context 224 | ); 225 | let isFragmentFound = false; 226 | 227 | resultStream.pipe(botGuard); 228 | 229 | resultStream.on('fragment:found', fragment => { 230 | isFragmentFound = true; 231 | 232 | botGuard.addFragment(fragment); 233 | 234 | const { attributes } = fragment; 235 | FRAGMENT_EVENTS.forEach(eventName => { 236 | fragment.once(eventName, (...args) => { 237 | const prefixedName = 'fragment:' + eventName; 238 | this.emit(prefixedName, request, attributes, ...args); 239 | }); 240 | }); 241 | 242 | attributes.returnHeaders && waitFragmentsRes.waitFor(fragment); 243 | 244 | attributes.primary && handlePrimaryFragment(fragment, botGuard); 245 | }); 246 | 247 | resultStream.once('finish', async () => { 248 | const statusCode = botGuard.statusCode || 200; 249 | if (shouldWriteHead) { 250 | shouldWriteHead = false; 251 | // Preload the loader script when at least 252 | // one fragment is present on the page 253 | if (isFragmentFound) { 254 | const configAssets = await getAssetsToPreload(request); 255 | const assetsToPreload = getFragmentAssetsToPreload( 256 | configAssets.styleRefs || [], 257 | configAssets.scriptRefs || [], 258 | request.headers 259 | ).join(','); 260 | 261 | if (typeof filterResponseHeaders === 'function') { 262 | (await waitFragmentsRes.all()).forEach(v => 263 | assignHeaders( 264 | responseHeaders, 265 | filterResponseHeaders(...v) 266 | ) 267 | ); 268 | } 269 | 270 | assetsToPreload !== '' && 271 | (responseHeaders.link = assetsToPreload); 272 | } 273 | this.emit('response', request, statusCode, responseHeaders); 274 | 275 | botGuard.writeHead(statusCode, responseHeaders); 276 | botGuard.pipe(contentLengthStream).pipe(response); 277 | } 278 | }); 279 | 280 | resultStream.once('error', handleError); 281 | 282 | parsedTemplate.forEach(item => resultStream.write(item)); 283 | resultStream.end(); 284 | }) 285 | .catch(err => { 286 | handleError(err); 287 | }); 288 | }; 289 | -------------------------------------------------------------------------------- /lib/serializer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Serializer = require('parse5/lib/serializer'); 3 | /** 4 | * CustomSerializer Class 5 | * 6 | * It uses the serializer from parse5 and overrides the serialize functions for handling 7 | * the tags that are passed via handleTags and piping. 8 | * 9 | * All the default nodes like
, go to parse5 serializer 10 | * and the rest are handled in this class. 11 | * 12 | * Node - Represets DOM Tree 13 | */ 14 | module.exports = class CustomSerializer extends Serializer { 15 | constructor(node, { treeAdapter, fullRendering, slotMap, handleTags }) { 16 | super(node, { treeAdapter }); 17 | this.fullRendering = fullRendering; 18 | this.slotMap = slotMap; 19 | this.handleTags = handleTags; 20 | this.lastChildInserted = false; 21 | this.defaultSlotsInserted = false; 22 | this.serializedList = []; 23 | this._serializeNode = this._serializeNode.bind(this); 24 | } 25 | 26 | /** 27 | * Push the serialized content in to the serializedList. 28 | * 29 | * this.html - serialized contents exposed by parse5 30 | */ 31 | pushBuffer() { 32 | if (this.html !== '') { 33 | this.serializedList.push(Buffer.from(this.html)); 34 | this.html = ''; 35 | } 36 | } 37 | 38 | /** 39 | * Extract the serialized HTML content and reset the serialized buffer. 40 | * 41 | * @returns {String} 42 | */ 43 | getHtmlContent() { 44 | let temp = ''; 45 | if (this.html !== '') { 46 | temp = this.html; 47 | this.html = ''; 48 | } 49 | return temp; 50 | } 51 | 52 | /** 53 | * Overidden the serialize function of parse5 Serializer 54 | * 55 | * this.startNode - Denotes the root node 56 | * @returns {Array} 57 | */ 58 | serialize() { 59 | this._serializeChildNodes(this.startNode); 60 | this.pushBuffer(); 61 | return this.serializedList; 62 | } 63 | 64 | /** 65 | * Checks if the node is either slot node / script type=slot 66 | * 67 | * @returns {Boolean} 68 | */ 69 | _isSlotNode(node) { 70 | const { attribs = {}, name } = node; 71 | return ( 72 | name === 'slot' || (name === 'script' && attribs.type === 'slot') 73 | ); 74 | } 75 | 76 | /** 77 | * Checks if the node is one of the nodes passed through handleTags 78 | * 79 | * @returns {Boolean} 80 | */ 81 | _isSpecialNode(node) { 82 | const { attribs = {}, name } = node; 83 | return ( 84 | this.handleTags.includes(name) || 85 | (name === 'script' && this.handleTags.includes(attribs.type)) 86 | ); 87 | } 88 | 89 | /** 90 | * Checks if the node is the lastChild of 91 | * 92 | * @returns {Boolean} 93 | */ 94 | _isLastChildOfBody(node) { 95 | const { 96 | parentNode: { name, lastChild } 97 | } = node; 98 | return name === 'body' && Object.is(node, lastChild); 99 | } 100 | 101 | /** 102 | * Serialize the nodes passed via handleTags 103 | * 104 | * @param {object} node 105 | * 106 | * // Input 107 | * 108 | * 109 | * // Output 110 | * { 111 | * name: 'fragment', 112 | * attributes: { 113 | * async: '', 114 | * primary: '' 115 | * }, 116 | * } 117 | */ 118 | _serializeSpecial(node) { 119 | this.pushBuffer(); 120 | let handledObj; 121 | const { name, attribs: attributes } = node; 122 | if (this.handleTags.includes(name)) { 123 | handledObj = Object.assign({}, { name: name, attributes }); 124 | this.serializedList.push(handledObj); 125 | this._serializeChildNodes(node); 126 | this.pushBuffer(); 127 | } else { 128 | // For handling the script type other than text/javascript 129 | this._serializeChildNodes(node); 130 | handledObj = Object.assign( 131 | {}, 132 | { 133 | name: attributes.type, 134 | attributes, 135 | textContent: this.getHtmlContent() 136 | } 137 | ); 138 | this.serializedList.push(handledObj); 139 | } 140 | this.serializedList.push({ closingTag: name }); 141 | } 142 | 143 | /** 144 | * Serialize the slot nodes from the slot map 145 | * 146 | * @param {object} node 147 | */ 148 | _serializeSlot(node) { 149 | const slotName = node.attribs.name; 150 | if (slotName && slotName !== 'default') { 151 | const childNodes = this.treeAdapter.getChildNodes(node); 152 | const slots = this.slotMap.has(slotName) 153 | ? this.slotMap.get(slotName) 154 | : childNodes; 155 | slots && slots.forEach(this._serializeNode); 156 | } else { 157 | // Handling duplicate slots 158 | if (this.defaultSlotsInserted) { 159 | console.warn( 160 | 'Encountered duplicate Unnamed slots in the template - Skipping the node' 161 | ); 162 | return; 163 | } 164 | const defaultSlots = this.slotMap.get('default'); 165 | this.defaultSlotsInserted = true; 166 | defaultSlots && defaultSlots.forEach(this._serializeNode); 167 | } 168 | } 169 | 170 | /** 171 | * Serialize the nodes in default slot from slot map and insert async placeholder. 172 | * 173 | * should happen before closing the body. 174 | */ 175 | _serializeRest() { 176 | this.lastChildInserted = true; 177 | if (!this.defaultSlotsInserted) { 178 | const defaultSlots = this.slotMap.get('default'); 179 | defaultSlots && defaultSlots.forEach(this._serializeNode); 180 | } 181 | this.pushBuffer(); 182 | this.serializedList.push({ placeholder: 'async' }); 183 | } 184 | 185 | /** 186 | * Serialize all the children of a parent node. 187 | * 188 | * @param {object} parentNode 189 | */ 190 | _serializeChildNodes(parentNode) { 191 | const childNodes = this.treeAdapter.getChildNodes(parentNode); 192 | childNodes && childNodes.forEach(this._serializeNode); 193 | } 194 | 195 | /** 196 | * Serialize the node based on their type 197 | * 198 | * @param {object} currentNode 199 | */ 200 | _serializeNode(currentNode) { 201 | if (this._isSpecialNode(currentNode)) { 202 | this._serializeSpecial(currentNode); 203 | } else if (this._isSlotNode(currentNode)) { 204 | this._serializeSlot(currentNode); 205 | } else if (this.treeAdapter.isElementNode(currentNode)) { 206 | this._serializeElement(currentNode); 207 | } else if (this.treeAdapter.isTextNode(currentNode)) { 208 | this._serializeTextNode(currentNode); 209 | } else if (this.treeAdapter.isCommentNode(currentNode)) { 210 | this._serializeCommentNode(currentNode); 211 | } else if (this.treeAdapter.isDocumentTypeNode(currentNode)) { 212 | this._serializeDocumentTypeNode(currentNode); 213 | } 214 | // Push default slots and async placeholder before body 215 | if ( 216 | this.fullRendering && 217 | !this.lastChildInserted && 218 | this._isLastChildOfBody(currentNode) 219 | ) { 220 | this._serializeRest(); 221 | } 222 | } 223 | }; 224 | -------------------------------------------------------------------------------- /lib/streams/async-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const stream = require('stream'); 4 | const BufferConcatStream = require('./buffer-concat-stream'); 5 | 6 | module.exports = class AsyncStream extends stream.Transform { 7 | constructor() { 8 | super({ objectMode: true }); 9 | this.streams = 0; 10 | } 11 | 12 | _transform(st, encoding, done) { 13 | this.streams += 1; 14 | st.pipe( 15 | new BufferConcatStream(data => { 16 | this.streams -= 1; 17 | this.push(data); 18 | if (this.streams === 0) { 19 | this.emit('alldone'); 20 | } 21 | }) 22 | ); 23 | st.on('error', err => { 24 | this.streams -= 1; 25 | this.emit('error', err); 26 | }); 27 | done(); 28 | } 29 | 30 | _flush(done) { 31 | if (this.streams === 0) { 32 | done(); 33 | } else { 34 | this.on('alldone', done); 35 | } 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /lib/streams/buffer-concat-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const stream = require('stream'); 4 | 5 | module.exports = class BufferConcatStream extends stream.Writable { 6 | constructor(callback) { 7 | super(); 8 | this.data = []; 9 | this.on('finish', () => { 10 | callback(Buffer.concat(this.data)); 11 | }); 12 | } 13 | 14 | _write(chunk, encoding, done) { 15 | this.data.push(chunk); 16 | done(null, chunk); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /lib/streams/content-length-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const stream = require('stream'); 4 | 5 | module.exports = class ContentLengthStream extends stream.Transform { 6 | constructor(callback) { 7 | super(); 8 | this.callback = callback; 9 | this.contentSize = 0; 10 | } 11 | 12 | _transform(chunk, encoding, done) { 13 | this.contentSize += chunk.length; 14 | done(null, chunk); 15 | } 16 | 17 | _flush(done) { 18 | this.callback(this.contentSize); 19 | done(); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /lib/streams/head-injector-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const stream = require('stream'); 4 | 5 | module.exports = class StringifierStream extends stream.Transform { 6 | constructor(primaryFragmentHeaders) { 7 | super({ objectMode: true }); 8 | 9 | this.__title = 10 | primaryFragmentHeaders['x-head-title'] !== undefined 11 | ? Buffer.from( 12 | primaryFragmentHeaders['x-head-title'], 13 | 'base64' 14 | ).toString('utf-8') 15 | : ''; 16 | this.__meta = 17 | primaryFragmentHeaders['x-head-meta'] !== undefined 18 | ? Buffer.from( 19 | primaryFragmentHeaders['x-head-meta'], 20 | 'base64' 21 | ).toString('utf-8') 22 | : ''; 23 | 24 | this.__injected = false; 25 | this.__titleRe = /.*<\/title>/is; 26 | } 27 | 28 | _transform(chunk, encoding, done) { 29 | if (this.__injected || (this.__title === '' && this.__meta === '')) { 30 | this.push(chunk); 31 | return done(); 32 | } 33 | 34 | let schunk = chunk.toString(); 35 | let insertPosStart, insertPosEnd; 36 | 37 | const titleMatch = this.__titleRe.exec(schunk); 38 | if (titleMatch !== null) { 39 | insertPosStart = titleMatch.index; 40 | insertPosEnd = insertPosStart + titleMatch[0].length; 41 | } else { 42 | const pos = schunk.indexOf('</head>'); 43 | if (pos !== -1) { 44 | insertPosStart = pos; 45 | insertPosEnd = insertPosStart; 46 | } 47 | } 48 | 49 | if (insertPosStart !== undefined) { 50 | schunk = 51 | schunk.substring(0, insertPosStart) + 52 | this.__title + 53 | this.__meta + 54 | schunk.substring(insertPosEnd); 55 | this.__injected = true; 56 | } 57 | 58 | this.push(schunk); 59 | done(); 60 | } 61 | }; 62 | -------------------------------------------------------------------------------- /lib/streams/seobots-guard-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const stream = require('stream'); 4 | const BotDetector = require('device-detector-js/dist/parsers/bot'); 5 | const botDetector = new BotDetector(); 6 | 7 | /** 8 | * The goals of this stream is to change behaviour of the Tailor when dealing with SEO/SM bots. 9 | * While it's ok to not to throw an error if non-primary fragment fails when dealing with browser - 10 | * it's not acceptable to bots. This is because browser can attempt to render missing page 11 | * parts at he client while bots can't do so due to the limited JS support. 12 | * 13 | * @type {module.StringifierStream} 14 | */ 15 | module.exports = class StringifierStream extends stream.Transform { 16 | #response; 17 | #isBot = false; 18 | #fragmentsCounter = 0; 19 | #fragmentFound = false; 20 | #batch = []; 21 | #erroredState = false; 22 | #writeHeadArgs = null; 23 | 24 | set statusCode(v) { 25 | this.statusCodeVal = v; 26 | if (this.#isBot) { 27 | return; 28 | } 29 | 30 | this.#response.statusCode = v; 31 | } 32 | get statusCode() { 33 | return this.statusCodeVal; 34 | } 35 | 36 | /** 37 | * @param {boolean} enabled 38 | * @param {http.IncomingHttpHeaders} reqHeaders 39 | * @param {http.ServerResponse} res 40 | */ 41 | constructor(enabled, reqHeaders, res) { 42 | super({ objectMode: true, autoDestroy: true }); 43 | this.#isBot = enabled 44 | ? botDetector.parse(reqHeaders['user-agent']) !== null 45 | : false; 46 | this.#response = res; 47 | } 48 | 49 | _transform(chunk, encoding, done) { 50 | if (!this.#isBot) { 51 | this.push(chunk, encoding); 52 | return done(); 53 | } 54 | 55 | // Stop accepting new data in errored state. 56 | // Default error handler should generate response 57 | if (this.#erroredState) { 58 | return done(); 59 | } 60 | 61 | this.#batch.push(chunk); 62 | 63 | if (this.#fragmentsCounter === 0 && this.#fragmentFound) { 64 | this.#flushBuffer(); 65 | } 66 | 67 | done(); 68 | } 69 | 70 | _flush(done) { 71 | //Not allowing to send broken response to Bot 72 | if (this.#erroredState) { 73 | this.#batch = []; 74 | this.end(); 75 | return; 76 | } 77 | 78 | this.#flushBuffer(); 79 | done(); 80 | } 81 | 82 | addFragment(fragment) { 83 | if (!this.#isBot) { 84 | return; 85 | } 86 | 87 | this.#fragmentsCounter++; 88 | this.#fragmentFound = true; 89 | 90 | fragment.once('response', () => this.#fragmentsCounter--); 91 | fragment.prependOnceListener('error', this.#handleFragmentError); 92 | } 93 | 94 | /** 95 | * Not allowing to write headers before all fragments will respond if dealing with Bot 96 | * Handling case when we received them from primary fragment & non-primary one fails afterwards. 97 | * @param args 98 | * @returns {*} 99 | */ 100 | writeHead(...args) { 101 | if (!this.#isBot) { 102 | return this.#response.writeHead(...args); 103 | } 104 | 105 | this.#writeHeadArgs = args; 106 | 107 | return this.#response; 108 | } 109 | 110 | #flushBuffer = () => { 111 | this.#flushHead(); 112 | this.#batch.forEach(v => this.push(v)); 113 | this.#batch = []; 114 | }; 115 | 116 | #flushHead = () => { 117 | if (this.statusCode) { 118 | this.#response.statusCode = this.statusCode; 119 | } 120 | if (this.#writeHeadArgs !== null) { 121 | this.#response.writeHead(...this.#writeHeadArgs); 122 | this.#writeHeadArgs = null; 123 | } 124 | }; 125 | 126 | #handleFragmentError = () => { 127 | if (!this.#erroredState) { 128 | this.emit( 129 | 'error', 130 | new Error( 131 | 'Fragment error while processing request from SEO/SM bot. See adjacent messages for real cause.' 132 | ) 133 | ); 134 | } 135 | 136 | this.#erroredState = true; 137 | this.#batch = []; 138 | }; 139 | }; 140 | -------------------------------------------------------------------------------- /lib/streams/stringifier-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const stream = require('stream'); 3 | const AsyncStream = require('./async-stream'); 4 | 5 | module.exports = class StringifierStream extends stream.Transform { 6 | constructor(fn) { 7 | super({ objectMode: true }); 8 | this.fn = fn; 9 | this.queue = []; 10 | this.isBusy = false; 11 | this.isFinished = false; 12 | } 13 | 14 | next() { 15 | if (this.isBusy) { 16 | return; 17 | } 18 | if (this.queue.length === 0) { 19 | if (this.isFinished) { 20 | this.emit('alldone'); 21 | } 22 | return; 23 | } 24 | let st = this.queue.shift(); 25 | if (st instanceof stream) { 26 | if (st instanceof AsyncStream) { 27 | st.emit('plugged'); 28 | } 29 | this.isBusy = true; 30 | 31 | /** 32 | * Since streams are consumed in parallel and processed in series, 33 | * We use the endEmitted flag on the stream to know if the stream has ended 34 | * before we even register the 'end' event 35 | */ 36 | if (st._readableState && st._readableState.endEmitted) { 37 | this.onEnd(st); 38 | } else { 39 | st.on('end', () => this.onEnd(st)); 40 | } 41 | } else { 42 | this.push(st); 43 | this.next(); 44 | } 45 | } 46 | 47 | onEnd(st) { 48 | this.push(Buffer.concat(st.buffer)); 49 | this.cleanup(st); 50 | this.processNext(); 51 | } 52 | 53 | processNext() { 54 | this.isBusy = false; 55 | this.next(); 56 | } 57 | 58 | cleanup(st) { 59 | st.buffer = []; 60 | st.removeListener('end', () => this.onEnd(st)); 61 | st.removeListener('error', err => this.onError(err, st)); 62 | st.setMaxListeners(st.getMaxListeners() - 1); 63 | } 64 | 65 | onError(err, st) { 66 | this.emit('error', err); 67 | this.cleanup(st); 68 | this.processNext(); 69 | } 70 | 71 | _transform(chunk, encoding, done) { 72 | if (chunk instanceof Buffer) { 73 | this.queue.push(chunk); 74 | } else if (typeof this.fn !== 'function') { 75 | if (chunk.name) { 76 | this.emit( 77 | 'error', 78 | new Error('Please provide transform function') 79 | ); 80 | } 81 | } else { 82 | const st = this.fn(chunk); 83 | // Consume streams in parallel 84 | if (st instanceof stream) { 85 | st.buffer = []; 86 | const onData = data => { 87 | data = data instanceof Buffer ? data : Buffer.from(data); 88 | st.buffer.push(data); 89 | }; 90 | 91 | st.setMaxListeners(st.getMaxListeners() + 1); 92 | st.on('data', onData); 93 | st.on('error', err => this.onError(err, st)); 94 | } 95 | // Process streams in series 96 | this.queue.push(st); 97 | } 98 | this.next(); 99 | done(); 100 | } 101 | 102 | _flush(done) { 103 | this.isFinished = true; 104 | if (this.queue.length === 0 && !this.isBusy) { 105 | done(); 106 | } else { 107 | this.on('alldone', done); 108 | } 109 | } 110 | }; 111 | -------------------------------------------------------------------------------- /lib/template-cutter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const MARKED_PARTS_TO_IGNORE = /<!-- TailorX: Ignore during parsing START -->.*?<!-- TailorX: Ignore during parsing END -->/gims; 4 | const IGNORED_PART_WITH_INDEX = /<!-- TailorX: Ignored content during parsing #(\d+) -->/gm; 5 | 6 | function replaceIgnorePart(ignoredPartIndex) { 7 | return `<!-- TailorX: Ignored content during parsing #${ignoredPartIndex} -->`; 8 | } 9 | 10 | class TemplateCutter { 11 | constructor() { 12 | this.ignoredParts = []; 13 | } 14 | 15 | /** 16 | * Cut base templates special parts which marked with the help of special HTML comments: 17 | * <!-- TailorX: Ignore during parsing START --> and <!-- TailorX: Ignore during parsing END --> 18 | * before and after a content which you want to ignore during parsing to speed up transforming of a base template 19 | * 20 | * @example <!-- TailorX: Ignore during parsing START --><div class='example-class>Example</div><!-- TailorX: Ignore during parsing END --> 21 | * 22 | * @param {String} baseTemplate - Base template that contains all the necessary tags and fragments for the given page (Used by multiple pages) 23 | * @returns {String} Base template without ignored parts 24 | */ 25 | cut(baseTemplate) { 26 | return baseTemplate.replace(MARKED_PARTS_TO_IGNORE, match => { 27 | const ignorePartsLength = this.ignoredParts.push(match); 28 | return replaceIgnorePart(ignorePartsLength - 1); 29 | }); 30 | } 31 | 32 | /** 33 | * Restore base template's special parts after ignoring them by @method _cut 34 | * 35 | * @param {Array} ignoredParts - Base template's ignored parts 36 | * @param {Array} chunks - Array consiting of Buffers and Objects 37 | * @returns {Array} Array consiting of Buffers and Objects 38 | */ 39 | restore(chunks) { 40 | const restoredChunks = chunks.map(chunk => { 41 | const isBuffer = chunk instanceof Buffer; 42 | const part = isBuffer ? chunk.toString('utf-8') : chunk; 43 | 44 | if (typeof part !== 'string') { 45 | return chunk; 46 | } 47 | 48 | const restoredPart = part.replace( 49 | IGNORED_PART_WITH_INDEX, 50 | (match, ignoredPartIndex) => { 51 | const ignoredPart = this.ignoredParts[ignoredPartIndex]; 52 | 53 | if (typeof ignoredPart !== 'string') { 54 | throw new Error( 55 | `TailorX can not find an ignored part ${ignoredPartIndex} of the current template during restoring!` 56 | ); 57 | } 58 | 59 | return ignoredPart; 60 | } 61 | ); 62 | 63 | return isBuffer ? Buffer.from(restoredPart, 'utf-8') : restoredPart; 64 | }); 65 | 66 | this.ignoredParts = []; 67 | 68 | return restoredChunks; 69 | } 70 | } 71 | 72 | module.exports = TemplateCutter; 73 | -------------------------------------------------------------------------------- /lib/tracing.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const opentracing = require('opentracing'); 4 | 5 | /** 6 | * Sets the given tracer and the Opentracing global tracer 7 | * 8 | * @param {Object} implementation - An Opentracing compliant Tracer implementation 9 | * @see {@link https://doc.esdoc.org/github.com/opentracing/opentracing-javascript/class/src/tracer.js~Tracer.html} 10 | */ 11 | module.exports = { 12 | initTracer: function(implementation) { 13 | opentracing.initGlobalTracer( 14 | implementation || new opentracing.Tracer() 15 | ); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /lib/transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const parse5 = require('parse5'); 4 | const memoize = require('memoizee'); 5 | const treeAdapter = parse5.treeAdapters.htmlparser2; 6 | const CustomSerializer = require('./serializer'); 7 | 8 | /** 9 | * Handles the parsing and serialization of templates. Also takes care of 10 | * merging the base and page templates 11 | */ 12 | 13 | module.exports = class Transform { 14 | constructor(handleTags, baseTemplatesCacheSize) { 15 | this.handleTags = handleTags; 16 | 17 | if (baseTemplatesCacheSize) { 18 | this._bindAndMemoizeInternalMethods(baseTemplatesCacheSize); 19 | } else { 20 | this._bindInternalMethods(); 21 | } 22 | } 23 | 24 | _bindAndMemoizeInternalMethods(baseTemplatesCacheSize) { 25 | const commonMemoizeOptions = { 26 | length: 1, 27 | max: baseTemplatesCacheSize 28 | }; 29 | 30 | this._parse = memoize(parse5.parse.bind(null), commonMemoizeOptions); 31 | this._parseFragment = memoize( 32 | parse5.parseFragment.bind(null), 33 | commonMemoizeOptions 34 | ); 35 | } 36 | 37 | _bindInternalMethods() { 38 | this._parse = parse5.parse.bind(null); 39 | this._parseFragment = parse5.parseFragment.bind(null); 40 | } 41 | 42 | /** 43 | * Parse and serialize the html. 44 | * 45 | * @param {string} baseTemplate - Base template that contains all the necessary tags and fragments for the given page (Used by multiple pages) 46 | * @param {string=} childTemplate - The current page template that gets merged in to the base template 47 | * @returns {Array} Array consiting of Buffers and Objects 48 | */ 49 | applyTransforms(baseTemplate, childTemplate, fullRendering) { 50 | const options = { treeAdapter }; 51 | 52 | const rootNodes = fullRendering 53 | ? this._parse(baseTemplate, options) 54 | : this._parseFragment(baseTemplate, options); 55 | 56 | const slotMap = 57 | childTemplate && typeof childTemplate === 'string' 58 | ? this._groupSlots(parse5.parseFragment(childTemplate, options)) 59 | : new Map(); 60 | 61 | const serializerOptions = { 62 | treeAdapter, 63 | slotMap, 64 | handleTags: this.handleTags, 65 | fullRendering 66 | }; 67 | 68 | const serializer = new CustomSerializer(rootNodes, serializerOptions); 69 | 70 | return serializer.serialize(); 71 | } 72 | 73 | /** 74 | * Group all the nodes after parsing the child template. Nodes with unnamed slots are 75 | * added to default slots 76 | * 77 | * @param {Object} root - The root node of the child template 78 | * @returns {Map} Map with keys as slot attribute name and corresponding values consisting of array of matching nodes 79 | */ 80 | _groupSlots(root) { 81 | const slotMap = new Map([['default', []]]); 82 | const nodes = treeAdapter.getChildNodes(root); 83 | nodes.forEach(node => { 84 | if (!treeAdapter.isTextNode(node)) { 85 | const { slot = 'default' } = node.attribs || {}; 86 | const slotNodes = slotMap.get(slot) || []; 87 | const updatedSlotNodes = [...slotNodes, node]; 88 | slotMap.set(slot, updatedSlotNodes); 89 | this._pushText(node.next, updatedSlotNodes); 90 | node.attribs && delete node.attribs.slot; 91 | } 92 | }); 93 | return slotMap; 94 | } 95 | 96 | /** 97 | * Add the text node to the Slot Map 98 | * 99 | * @param {Object} nextNode 100 | * @param {Array} slot - Array of matching nodes 101 | */ 102 | _pushText(nextNode, slot) { 103 | if (nextNode && treeAdapter.isTextNode(nextNode)) { 104 | slot.push(nextNode); 105 | } 106 | } 107 | }; 108 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const _ = require('lodash'); 4 | 5 | const getCrossOrigin = (url = '', host = '') => { 6 | // Check for the same origin & relative links 7 | if (url.includes(`://${host}`) || !url.includes('://')) { 8 | return ''; 9 | } 10 | return 'crossorigin'; 11 | }; 12 | 13 | const getPreloadAttributes = ({ 14 | assetUrl, 15 | host, 16 | asAttribute, 17 | corsCheck = false, 18 | noPush = true // Disable HTTP/2 Push behaviour until digest spec is implemented by most browsers 19 | }) => { 20 | return ( 21 | assetUrl && 22 | `<${assetUrl}>; rel="preload"; as="${asAttribute}"; ${ 23 | noPush ? 'nopush;' : '' 24 | } ${corsCheck ? getCrossOrigin(assetUrl, host) : ''}`.trim() 25 | ); 26 | }; 27 | 28 | // Early preloading of primary fragments assets to improve Performance 29 | const getFragmentAssetsToPreload = (styleRefs, scriptRefs, { host } = {}) => { 30 | let assetsToPreload = []; 31 | 32 | // Handle Server rendered fragments without depending on assets 33 | if (scriptRefs.length === 0 && styleRefs.length === 0) { 34 | return assetsToPreload; 35 | } 36 | 37 | for (const uri of styleRefs) { 38 | assetsToPreload.push( 39 | getPreloadAttributes({ 40 | assetUrl: uri, 41 | asAttribute: 'style' 42 | }) 43 | ); 44 | } 45 | 46 | for (const uri of scriptRefs) { 47 | assetsToPreload.push( 48 | getPreloadAttributes({ 49 | assetUrl: uri, 50 | asAttribute: 'script', 51 | corsCheck: true, 52 | host 53 | }) 54 | ); 55 | } 56 | 57 | return assetsToPreload; 58 | }; 59 | 60 | const nextIndexGenerator = (initialIndex, step) => { 61 | let index = initialIndex; 62 | 63 | return () => { 64 | let pastIndex = index; 65 | index += step; 66 | return pastIndex; 67 | }; 68 | }; 69 | 70 | const assignHeaders = (to, from) => { 71 | from = Object.assign({}, from); 72 | if (to['set-cookie'] !== undefined && from['set-cookie'] !== undefined) { 73 | from['set-cookie'] = _.uniqBy( 74 | from['set-cookie'].concat(to['set-cookie']), 75 | v => { 76 | const m = v.match(/^(.+?)=/); 77 | if (m && m[1]) { 78 | return m[1].trim(); 79 | } 80 | 81 | return v; 82 | } 83 | ); 84 | } 85 | 86 | return Object.assign(to, from); 87 | }; 88 | 89 | module.exports = { 90 | getCrossOrigin, 91 | getFragmentAssetsToPreload, 92 | nextIndexGenerator, 93 | assignHeaders 94 | }; 95 | -------------------------------------------------------------------------------- /lib/wait-fragment-responses.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class WaitForFragmentResponses { 4 | #promises = []; 5 | 6 | waitFor(fragment) { 7 | const p = new Promise(function(resolve) { 8 | fragment.on('response', function(statusCode, headers) { 9 | resolve([fragment.attributes, headers]); 10 | }); 11 | fragment.on('error', function() { 12 | resolve(null); 13 | }); 14 | }); 15 | 16 | this.#promises.push(p); 17 | } 18 | 19 | all() { 20 | return Promise.all(this.#promises).then(values => 21 | values.filter(v => v !== null) 22 | ); 23 | } 24 | } 25 | 26 | module.exports = WaitForFragmentResponses; 27 | -------------------------------------------------------------------------------- /logo/README.md: -------------------------------------------------------------------------------- 1 | # TailorX logo 2 | 3 | ![TailorX logo](./tailorx-logo.png) 4 | 5 | ## Credits 6 | 7 | The [original Tailor logo](./tailor-logo.svg) was designed by [Nadya Kuzmina](http://nadyakuzmina.com/). 8 | -------------------------------------------------------------------------------- /logo/tailor-logo.svg: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="utf-8"?> 2 | <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 3 | <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 4 | width="500px" height="200px" viewBox="0 0 500 200"> 5 | <path fill="#030405" d="M61.915,200c-1.548,0-2.689-0.795-3.131-2.183c-0.747-2.344-3.018-9.475,47.497-79.405 6 | c7.598-10.518,15.338-20.991,22.462-30.489c5.277-9.31,9.899-17.421,13.649-23.82c2.513-4.288,4.439-7.472,5.871-9.705 7 | c-11.398,3.841-39.671,12.17-67.273,19.289C34.762,85.609,8.257,89.861,2.209,86.323c-1.172-0.686-1.817-1.728-1.817-2.934 8 | c0-2.244,2.295-4.986,18.95-9.81C42.529,66.863,88,58.091,132.491,51.751c21.577-3.075,40.346-5.208,54.279-6.171 9 | c24.227-1.673,25.774,0.772,26.518,1.948c0.283,0.448,0.3,1.014,0.045,1.478c-0.256,0.464-0.744,0.752-1.273,0.752 10 | c-20.324,0-36.387,15.765-44.152,25.163c-11.772,14.245-18.124,29.736-18.124,37.079c0,1.444,0.243,2.569,0.703,3.252 11 | c0.387,0.576,0.923,0.833,1.738,0.833c13.065,0,36.324-31.312,50.22-50.02c11.573-15.58,13.809-18.255,15.99-17.045 12 | c0.436,0.242,0.738,0.652,0.851,1.152c0.421,1.867-2.106,5.674-12.246,19.871c-10.264,14.372-29.369,41.124-26.402,46.89 13 | c0.126,0.245,0.461,0.896,2.207,0.896c4.347,0,19.252-12.451,37.476-31.238c1.581-2.841,3.071-5.369,4.218-7.268 14 | c7.818-12.946,18.828-28.443,25.804-33.877c0.972-0.757,3.931-3.063,6.166-1.605c0.478,0.312,1.206,1.084,0.708,2.76 15 | c-1.646,5.535-18.535,25.129-33.591,40.756c-0.305,0.317-0.624,0.646-0.955,0.988c-8.136,14.673-10.054,22.229-9.154,24.26 16 | c0.083,0.187,0.168,0.38,0.724,0.38c3.306,0,17.946-10.418,42.814-33.081c1.5-2.322,3.003-4.623,4.508-6.896 17 | C272.892,55.88,311.08,0,326.789,0c1.009,0,1.82,0.266,2.412,0.792c0.437,0.387,0.956,1.104,0.956,2.321 18 | c0,10.318-44.168,53.412-53.022,61.945c-5.117,4.933-11.31,10.76-17.806,16.675c-24.256,37.522-42.014,71.876-42.014,81.645 19 | c0,1.01,0.206,1.418,0.294,1.548c0.056,0.079,0.131,0.189,0.507,0.189c4.128,0,16.444-16.54,27.31-31.132 20 | c10.423-13.997,22.236-29.861,33.328-41.243c13.236-13.583,23.214-18.316,30.496-14.47c0.591,0.312,0.893,0.986,0.732,1.635 21 | c-0.161,0.648-0.742,1.104-1.411,1.104c-8.982,0-20.562,8.721-30.977,23.329c-9.518,13.35-15.912,28.645-15.912,38.061 22 | c0,2.804,0.562,4.947,1.671,6.37c1.136,1.458,2.851,2.167,5.242,2.167c7.311,0,18.181-9.478,29.077-25.353 23 | c8.067-11.754,14.292-24.512,16.147-32.68c-2.537,0.188-4.861-0.211-6.776-1.475c-1.771-1.17-2.522-3.063-1.958-4.942 24 | c0.764-2.54,3.651-4.314,7.021-4.314c2.188,0,3.396,1.046,4.023,1.924c0.773,1.082,1.167,2.592,1.167,4.489 25 | c0,0.247-0.007,0.501-0.021,0.764c6.134-1.696,13.315-6.043,17.938-8.841c5.232-3.167,6.81-4.125,8.081-3.015 26 | c1.844,1.613,0.156,5.101-13.897,28.713c-5.189,8.72-11.072,18.604-15.305,26.534c-5.535,10.371-6.224,13.653-6.192,14.666 27 | c0.619-0.286,2.301-1.61,5.795-7.681c2.959-5.142,6.409-12.208,10.063-19.689c10.162-20.81,20.67-42.328,29.715-43.839 28 | c2.044-0.34,3.977,0.331,5.59,1.945c8.473,8.473,24.308,10.658,48.409,6.68c22.575-3.726,52.062-12.863,90.145-27.933 29 | c0.747-0.294,1.592,0.07,1.887,0.817s-0.07,1.591-0.816,1.887c-50.74,20.079-87.452,29.993-111.853,29.995 30 | c-13.746,0-23.581-3.144-29.826-9.39c-0.961-0.961-1.933-1.321-3.056-1.133c-7.568,1.264-19.136,24.952-27.583,42.247 31 | c-9.331,19.108-14.401,29.08-18.722,29.08c-0.971,0-1.785-0.452-2.233-1.242c-2.086-3.668,4.626-16.102,21.484-44.429 32 | c5.519-9.27,11.651-19.576,13.535-23.922c-1.07,0.597-2.44,1.426-3.715,2.197c-5.354,3.24-13.04,7.892-19.841,9.435 33 | c-1.654,8.615-8.105,22.117-16.811,34.799c-5.493,8.003-19.556,26.616-31.475,26.616c-3.301,0-5.836-1.105-7.535-3.286 34 | c-1.516-1.947-2.285-4.69-2.285-8.157c0-9.972,6.611-25.945,16.452-39.748c4.265-5.981,8.679-10.984,13.071-14.88 35 | c-13.016,10.983-28.215,31.396-40.541,47.949c-15.362,20.631-24.313,32.305-29.642,32.305c-1.574,0-2.447-0.79-2.902-1.452 36 | c-0.542-0.788-0.806-1.832-0.806-3.193c0-10.692,17.268-43.137,36.89-74.432c-16.014,14.168-31.989,26.945-37.058,26.945 37 | c-1.7,0-2.982-0.896-3.519-2.458c-1.2-3.493,1.507-10.813,5.021-18.082c-11.14,11.035-26.8,25.386-32.898,25.386 38 | c-2.909,0-4.211-1.344-4.792-2.473c-3.501-6.803,9.396-25.792,26.621-49.91c1.904-2.667,3.897-5.458,5.691-8.026 39 | c-1.77,2.331-3.689,4.915-5.588,7.47c-15.142,20.386-38.026,51.194-52.553,51.194c-1.783,0-3.218-0.733-4.15-2.118 40 | c-0.795-1.181-1.197-2.822-1.197-4.875c0-8.047,6.454-24.003,18.79-38.93c6.854-8.294,19.946-21.347,36.865-25.147 41 | c-7.407-0.114-20.366,0.591-41.458,3.04c-0.09,0.173-0.216,0.331-0.377,0.46c-1.208,1.168-13.649,16.966-29.529,38.121 42 | c-1.834,3.236-3.712,6.556-5.606,9.903c-12.333,21.795-26.311,46.499-38.133,66.09C68.233,197.337,64.194,200,61.915,200z 43 | M61.606,197.051c0.002,0,0.071,0.037,0.262,0.042c1.164-0.324,5.774-4.335,23.575-33.898 44 | c9.826-16.316,21.103-36.078,31.651-54.695c-3.435,4.681-6.921,9.474-10.393,14.3c-14.572,20.254-25.862,36.877-33.558,49.407 45 | C59.785,193.958,61.379,196.798,61.606,197.051L61.606,197.051z M312.107,85.078c-2.339,0-3.902,1.128-4.236,2.243 46 | c-0.194,0.647,0.066,1.212,0.774,1.679c1.499,0.99,3.461,1.225,5.673,0.955c0.048-0.484,0.072-0.942,0.072-1.371 47 | c0-1.26-0.216-2.227-0.623-2.797C313.53,85.456,313.129,85.078,312.107,85.078z M3.3,83.425c0.008,0.075,0.058,0.202,0.377,0.388 48 | c2.43,1.421,14.755,3.005,76.586-12.94c25.572-6.595,49.076-13.485,61.801-17.515c-36.113,4.872-76.622,12.034-103.878,18.384 49 | C5.994,79.24,3.493,82.927,3.3,83.425z M254.207,46.441c-0.968,0.395-3.037,1.696-6.869,5.926 50 | c-5.255,5.8-11.877,14.929-17.998,24.694C242.953,62.32,252.504,50.27,254.207,46.441z M326.789,2.907 51 | c-9.127,0-32.892,26.9-60.876,68.806c0.093-0.087,0.188-0.176,0.28-0.262c22.13-20.754,48.605-47.903,57.865-61.769 52 | c3.266-4.889,3.228-6.443,3.181-6.723C327.173,2.938,327.037,2.907,326.789,2.907z M153.042,52.627 53 | c-1.271,1.316-5.306,8.012-10.744,17.417c6.619-8.629,11.826-15.253,14.571-18.584c-0.978,0.119-1.972,0.24-2.982,0.366 54 | c-0.125,0.285-0.339,0.528-0.621,0.687C153.204,52.547,153.129,52.585,153.042,52.627z"/> 55 | <path fill="#030405" d="M260.346,38.726c-0.989,0-1.862-0.323-2.521-0.934c-1.215-1.125-1.441-2.969-0.623-5.06 56 | c1.076-2.745,3.804-5.584,6.508-5.584c0.98,0,1.882,0.371,2.606,1.072c1.481,1.435,1.872,3.395,1.069,5.376 57 | C266.28,36.33,262.99,38.726,260.346,38.726z M263.709,30.056c-1.179,0-3.062,1.851-3.802,3.737 58 | c-0.337,0.863-0.381,1.613-0.108,1.866c0.043,0.04,0.173,0.159,0.547,0.159c1.352,0,3.623-1.528,4.345-3.312 59 | c0.473-1.167-0.001-1.812-0.397-2.197C264.11,30.131,263.936,30.056,263.709,30.056z"/> 60 | </svg> 61 | -------------------------------------------------------------------------------- /logo/tailorx-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/StyleT/tailorx/44d13e0288d675061aece19e201503260d1339dc/logo/tailorx-logo.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tailorx", 3 | "version": "7.0.1", 4 | "description": "Tailor assembles a web page from multiple fragments", 5 | "keywords": [ 6 | "tailor", 7 | "layout service", 8 | "streaming templates", 9 | "node-tailor" 10 | ], 11 | "scripts": { 12 | "test": "mocha --harmony tests/**", 13 | "coverage": "nyc --reporter=lcov --reporter=text mocha --harmony tests/**", 14 | "codecov": "cat coverage/lcov.info | codecov", 15 | "lint": "eslint .", 16 | "fix": "eslint . --fix", 17 | "benchmark": "node perf/benchmark" 18 | }, 19 | "pre-commit": [ 20 | "lint", 21 | "test" 22 | ], 23 | "engines": { 24 | "node": ">12.0.0" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "ssh://git@github.com:StyleT/tailorx.git" 29 | }, 30 | "license": "Apache-2.0", 31 | "dependencies": { 32 | "@namecheap/error-extender": "^1.1.1", 33 | "agentkeepalive": "^4.1.0", 34 | "device-detector-js": "^2.2.1", 35 | "lodash": "^4.17.15", 36 | "memoizee": "^0.4.14", 37 | "opentracing": "^0.14.3", 38 | "parse5": "^3.0.3", 39 | "util.promisify": "^1.0.0" 40 | }, 41 | "devDependencies": { 42 | "babel-eslint": "^10.0.3", 43 | "codecov": "^3.6.2", 44 | "eslint": "^6.8.0", 45 | "eslint-plugin-prettier": "^3.1.2", 46 | "iamdee": "^0.4.0", 47 | "lazypipe": "^1.0.1", 48 | "loadtest": "^2.3.0", 49 | "metrics": "^0.1.11", 50 | "mocha": "^7.0.1", 51 | "nock": "^11.7.2", 52 | "nyc": "^15.0.0", 53 | "pre-commit": "^1.2.2", 54 | "prettier": "^1.19.1", 55 | "proxyquire": "^1.8.0", 56 | "puppeteer": "^1.0.0", 57 | "sinon": "^8.1.1", 58 | "wd": "^1.2.0" 59 | }, 60 | "files": [ 61 | "lib", 62 | "LICENSE", 63 | "MAINTAINERS", 64 | "index.js", 65 | "index.d.ts", 66 | "README.md", 67 | "src/pipe.min.js", 68 | "yarn.lock" 69 | ], 70 | "types": "index.d.ts", 71 | "nyc": { 72 | "check-coverage": true, 73 | "per-file": true, 74 | "lines": 90, 75 | "statements": 80, 76 | "functions": 80, 77 | "branches": 70 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /perf/benchmark.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | const metrics = require('metrics'); 5 | const { spawn } = require('child_process'); 6 | const histogram = new metrics.Histogram.createUniformHistogram(); 7 | const html = ` 8 | <html> 9 | <head> 10 | <meta charset="utf-8"> 11 | <title>Test Page 12 | 13 | 14 |

Fragment 1:

15 | 16 | 17 |

Fragment 2:

18 | 19 | 20 | 21 | `; 22 | const parseTemplate = require('../lib/parse-template')(['fragment'], []); 23 | const requests = new WeakMap(); 24 | const fragments = new WeakMap(); 25 | const Tailor = require('../'); 26 | const processTemplate = require('../lib/process-template'); 27 | 28 | const handleTag = (request, tag, options, context) => { 29 | if (tag && tag.name === 'switcher') { 30 | const stream = processTemplate(request, options, context); 31 | process.nextTick(() => { 32 | stream.end({ 33 | name: 'fragment', 34 | attributes: { src: 'http://localhost:8081' } 35 | }); 36 | }); 37 | return stream; 38 | } 39 | 40 | return ''; 41 | }; 42 | 43 | const tailor = new Tailor({ 44 | fetchTemplate: () => Promise.resolve(html).then(parseTemplate), 45 | handleTag, 46 | handledTags: ['switcher'] 47 | }); 48 | 49 | const getMillisecs = hrTime => { 50 | // [seconds, nanoseconds] 51 | return hrTime && hrTime[0] * 1e3 + hrTime[1] / 1e6; 52 | }; 53 | 54 | const updateTailorOverhead = (headersTime, primaryOverhead) => { 55 | let duration = headersTime - primaryOverhead; 56 | histogram.update(duration); 57 | }; 58 | 59 | let primaryOverhead = 0; 60 | // Tailor Events to collect Metrics 61 | tailor.on('start', request => { 62 | requests.set(request, process.hrtime()); 63 | }); 64 | 65 | tailor.on('response', request => { 66 | if (primaryOverhead > 0 && requests.has(request)) { 67 | const startTime = requests.get(request); 68 | const timeToHeaders = getMillisecs(process.hrtime(startTime)); 69 | updateTailorOverhead(timeToHeaders, primaryOverhead); 70 | } 71 | }); 72 | 73 | tailor.on('end', () => { 74 | // reset 75 | primaryOverhead = 0; 76 | }); 77 | 78 | tailor.on('fragment:start', (request, fragment) => { 79 | fragments.set(fragment, process.hrtime()); 80 | }); 81 | 82 | tailor.on('fragment:response', (request, fragment) => { 83 | const startTime = fragments.get(fragment); 84 | if (fragment.primary) { 85 | primaryOverhead = getMillisecs(process.hrtime(startTime)); 86 | } 87 | }); 88 | 89 | tailor.on('error', (request, error) => { 90 | console.log('Tailor Error ', error); 91 | }); 92 | 93 | // Tailor server 94 | const server = http.createServer(tailor.requestHandler); 95 | server.listen(8080); 96 | 97 | //Mock fragment server 98 | const fragment = spawn('node', ['perf/fragment-server.js']); 99 | // Load testing 100 | const worker = spawn('node', ['perf/loadtest.js']); 101 | worker.stdout.pipe(process.stdout); 102 | 103 | worker.on('close', () => { 104 | console.log( 105 | 'Tailor Overhead Metrics', 106 | JSON.stringify(histogram.printObj(), null, 2) 107 | ); 108 | fragment.kill(); 109 | worker.kill(); 110 | process.exit(0); 111 | }); 112 | -------------------------------------------------------------------------------- /perf/fragment-server.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const http = require('http'); 4 | const SIZE = 1024; 5 | const CHARS = '0123456789abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY'; 6 | const buffer = Buffer.alloc(SIZE); 7 | for (let i = 0; i < SIZE; i++) { 8 | buffer.write( 9 | CHARS.charAt(Math.round(Math.random() * (CHARS.length - 1))), 10 | i 11 | ); 12 | } 13 | 14 | http.createServer((request, response) => { 15 | response.writeHead(200, { 'Content-Type': 'text/html' }); 16 | response.write(buffer); 17 | response.end(); 18 | }).listen(8081); 19 | -------------------------------------------------------------------------------- /perf/loadtest.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const loadtest = require('loadtest'); 4 | const options = { 5 | url: 'http://localhost:8080', 6 | maxRequests: 5000, 7 | concurrency: 100 8 | }; 9 | 10 | loadtest.loadTest(options, (error, result) => { 11 | console.log('LoadTest Metrics', JSON.stringify(result, null, 2)); 12 | }); 13 | -------------------------------------------------------------------------------- /tests/fetch-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fetchTemplate = require('../lib/fetch-template'); 3 | const assert = require('assert'); 4 | const sinon = require('sinon'); 5 | const path = require('path'); 6 | const fs = require('fs'); 7 | 8 | describe('fetch-template', () => { 9 | let mockParseTemplate; 10 | const mockRequest = { url: 'http://localhost:8080/test' }; 11 | const templatePath = path.join(__dirname); 12 | const baseTemplatePath = path.join(templatePath, 'base-template.html'); 13 | const testTemplatePath = path.join(templatePath, 'test.html'); 14 | 15 | before(() => { 16 | fs.writeFileSync(baseTemplatePath, '
base-template
'); 17 | fs.writeFileSync(testTemplatePath, '
test
'); 18 | }); 19 | 20 | beforeEach(() => (mockParseTemplate = sinon.spy())); 21 | 22 | after(() => { 23 | if (fs.existsSync(baseTemplatePath)) { 24 | fs.unlinkSync(baseTemplatePath); 25 | } 26 | if (fs.existsSync(testTemplatePath)) { 27 | fs.unlinkSync(testTemplatePath); 28 | } 29 | }); 30 | 31 | afterEach(() => mockParseTemplate.resetHistory()); 32 | 33 | describe('templatePath - File', () => { 34 | it('should fetch template from file path', () => { 35 | return fetchTemplate(testTemplatePath)( 36 | mockRequest, 37 | mockParseTemplate 38 | ).then(() => { 39 | assert(mockParseTemplate.calledOnce); 40 | assert(mockParseTemplate.calledWith('
test
')); 41 | }); 42 | }); 43 | 44 | it('should throw TEMPLATE_NOT_FOUND error on wrong file path', () => { 45 | const wrongTemplatePath = path.join( 46 | templatePath, 47 | 'wrong-template.html' 48 | ); 49 | return fetchTemplate(wrongTemplatePath)( 50 | mockRequest, 51 | mockParseTemplate 52 | ).catch(err => { 53 | assert(err.code, 1); 54 | assert(err.presentable, 'template not found'); 55 | }); 56 | }); 57 | }); 58 | 59 | describe('templatePath - Dir', () => { 60 | it('should fetch the template with absolute path when baseTemplateFn is falsy', () => { 61 | const baseTemplateFn = () => null; 62 | return fetchTemplate(templatePath, baseTemplateFn)( 63 | mockRequest, 64 | mockParseTemplate 65 | ).then(() => { 66 | assert(mockParseTemplate.calledOnce); 67 | assert(mockParseTemplate.calledWith('
test
')); 68 | }); 69 | }); 70 | 71 | it('should fetch template with relative path and baseTemplateFn', () => { 72 | const baseTemplateFn = () => 'base-template'; 73 | 74 | return fetchTemplate(templatePath, baseTemplateFn)( 75 | mockRequest, 76 | mockParseTemplate 77 | ).then(() => { 78 | assert(mockParseTemplate.calledOnce); 79 | assert( 80 | mockParseTemplate.calledWith( 81 | '
base-template
', 82 | '
test
' 83 | ) 84 | ); 85 | }); 86 | }); 87 | 88 | it('should throw TEMPLATE_NOT_FOUND error for wrong template path', () => { 89 | const request = { url: 'http://localhost:8080/unknown' }; 90 | return fetchTemplate(templatePath)( 91 | request, 92 | mockParseTemplate 93 | ).catch(err => { 94 | assert(err.code, 1); 95 | assert(err.presentable, 'template not found'); 96 | }); 97 | }); 98 | }); 99 | }); 100 | -------------------------------------------------------------------------------- /tests/filter-headers.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const filterHeaders = require('../lib/filter-headers'); 3 | const assert = require('assert'); 4 | 5 | describe('filter-headers', () => { 6 | const headers = { 7 | 'please-kill-me': '0', 8 | 'accept-language': '1', 9 | referer: '2', 10 | 'user-agent': '3', 11 | 'x-request-uri': '/example', 12 | 'x-request-host': 'localhost' 13 | }; 14 | 15 | it('keeps only certain headers', () => { 16 | const after = { 17 | 'accept-language': '1', 18 | referer: '2', 19 | 'user-agent': '3', 20 | 'x-request-uri': '/example', 21 | 'x-request-host': 'localhost' 22 | }; 23 | assert.deepEqual(filterHeaders({}, { headers }), after); 24 | }); 25 | 26 | it('removes headers if fragment is public', () => { 27 | assert.deepEqual(filterHeaders({ public: true }, { headers }), {}); 28 | }); 29 | }); 30 | -------------------------------------------------------------------------------- /tests/fragment.events.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const assert = require('assert'); 3 | const nock = require('nock'); 4 | const sinon = require('sinon'); 5 | const Fragment = require('../lib/fragment'); 6 | const requestFragment = require('../lib/request-fragment'); 7 | 8 | const TAG = { attributes: { src: 'https://fragment' } }; 9 | const REQUEST = { headers: {} }; 10 | const RESPONSE_HEADERS = { connection: 'close' }; 11 | const filterHeaderFn = () => ({}); 12 | const processFragmentResponseFn = require('../lib/process-fragment-response'); 13 | const getOptions = tag => { 14 | return { 15 | tag, 16 | context: {}, 17 | index: false, 18 | requestFragment: requestFragment( 19 | filterHeaderFn, 20 | processFragmentResponseFn 21 | ) 22 | }; 23 | }; 24 | 25 | describe('Fragment events', () => { 26 | it('triggers `start` event', done => { 27 | nock('https://fragment') 28 | .get('/') 29 | .reply(200, 'OK'); 30 | const fragment = new Fragment(getOptions(TAG)); 31 | fragment.on('start', done); 32 | fragment.fetch(REQUEST); 33 | }); 34 | 35 | it('triggers `response(statusCode, headers)` when received headers', done => { 36 | nock('https://fragment') 37 | .get('/') 38 | .reply(200, 'OK', RESPONSE_HEADERS); 39 | const fragment = new Fragment(getOptions(TAG)); 40 | fragment.on('response', (statusCode, headers) => { 41 | assert.equal(statusCode, 200); 42 | assert.deepEqual(headers, RESPONSE_HEADERS); 43 | done(); 44 | }); 45 | fragment.fetch(REQUEST); 46 | }); 47 | 48 | it('triggers `end(contentSize)` when the content is succesfully retreived', done => { 49 | nock('https://fragment') 50 | .get('/') 51 | .reply(200, '12345'); 52 | const fragment = new Fragment(getOptions(TAG)); 53 | fragment.on('end', contentSize => { 54 | assert.equal(contentSize, 5); 55 | done(); 56 | }); 57 | fragment.fetch(REQUEST); 58 | fragment.stream.resume(); 59 | }); 60 | 61 | it('triggers `error(error)` when fragment responds with 50x', done => { 62 | nock('https://fragment') 63 | .get('/') 64 | .reply(500); 65 | const fragment = new Fragment(getOptions(TAG)); 66 | fragment.on('error', error => { 67 | assert.ok(error); 68 | done(); 69 | }); 70 | fragment.fetch(REQUEST); 71 | }); 72 | 73 | it('should not trigger `response` and `end` if there was an `error`', done => { 74 | const onResponse = sinon.spy(); 75 | const onEnd = sinon.spy(); 76 | const onError = sinon.spy(); 77 | nock('https://fragment') 78 | .get('/') 79 | .reply(500); 80 | const fragment = new Fragment(getOptions(TAG)); 81 | fragment.on('response', onResponse); 82 | fragment.on('end', onEnd); 83 | fragment.on('error', onError); 84 | fragment.fetch(REQUEST); 85 | fragment.stream.on('end', () => { 86 | assert.equal(onResponse.callCount, 0); 87 | assert.equal(onEnd.callCount, 0); 88 | assert.equal(onError.callCount, 1); 89 | done(); 90 | }); 91 | fragment.stream.resume(); 92 | }); 93 | 94 | it('triggers `error(error)` when there is socket error', done => { 95 | const ERROR = { 96 | message: 'something awful happened', 97 | code: 'AWFUL_ERROR' 98 | }; 99 | nock('https://fragment') 100 | .get('/') 101 | .replyWithError(ERROR); 102 | const fragment = new Fragment(getOptions(TAG)); 103 | fragment.on('error', error => { 104 | assert.equal(error.message, ERROR.message); 105 | done(); 106 | }); 107 | fragment.fetch(REQUEST); 108 | }); 109 | 110 | it('triggers `error(error)` when fragment times out', done => { 111 | nock('https://fragment') 112 | .get('/') 113 | .socketDelay(101) 114 | .reply(200); 115 | const tag = { attributes: { src: 'https://fragment', timeout: '100' } }; 116 | const fragment = new Fragment(getOptions(tag)); 117 | fragment.on('error', err => { 118 | assert.equal(err.message, 'socket hang up'); 119 | done(); 120 | }); 121 | fragment.fetch(REQUEST); 122 | }); 123 | }); 124 | -------------------------------------------------------------------------------- /tests/fragment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const Fragment = require('../lib/fragment'); 4 | const assert = require('assert'); 5 | const getOptions = tag => { 6 | return { 7 | tag, 8 | context: {}, 9 | index: false, 10 | requestFragment: () => {} 11 | }; 12 | }; 13 | 14 | describe('Fragment', () => { 15 | it('computed attributes are correctly initiliazed', () => { 16 | const attributes = { 17 | id: 'foo', 18 | src: 'https://fragment', 19 | async: true, 20 | timeout: '4000', 21 | custom: 'bar', 22 | 'ignore-invalid-ssl': true 23 | }; 24 | 25 | const expected = { 26 | id: attributes.id, 27 | url: attributes.src, 28 | async: attributes.async, 29 | forwardQuerystring: false, 30 | returnHeaders: false, 31 | timeout: 4000, 32 | primary: false, 33 | public: false, 34 | ignoreInvalidSsl: attributes['ignore-invalid-ssl'], 35 | custom: attributes.custom 36 | }; 37 | 38 | const tag = { attributes }; 39 | const fragment = new Fragment(getOptions(tag)); 40 | const fattributes = fragment.attributes; 41 | 42 | assert.deepEqual(fattributes, expected); 43 | }); 44 | }); 45 | -------------------------------------------------------------------------------- /tests/handle-tag.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const http = require('http'); 5 | const sinon = require('sinon'); 6 | const Tailor = require('../index'); 7 | const PassThrough = require('stream').PassThrough; 8 | 9 | describe('Handle tag', () => { 10 | let server; 11 | let tailor; 12 | const mockTemplate = sinon.stub(); 13 | const mockHandleTag = sinon.stub(); 14 | 15 | beforeEach(done => { 16 | tailor = new Tailor({ 17 | fetchTemplate: (request, parseTemplate) => { 18 | const template = mockTemplate(request); 19 | if (template) { 20 | return parseTemplate(template); 21 | } else { 22 | return Promise.reject('Error fetching template'); 23 | } 24 | }, 25 | pipeDefinition: () => Buffer.from(''), 26 | handledTags: ['x-tag'], 27 | handleTag: mockHandleTag 28 | }); 29 | server = http.createServer(tailor.requestHandler); 30 | server.listen(8080, 'localhost', done); 31 | }); 32 | 33 | afterEach(done => { 34 | mockTemplate.reset(); 35 | mockHandleTag.reset(); 36 | server.close(done); 37 | }); 38 | 39 | it('calls handleTag for a tag in handledTags', done => { 40 | mockTemplate.returns('test'); 41 | mockHandleTag.returns(''); 42 | http.get('http://localhost:8080/template', response => { 43 | const request = mockHandleTag.args[0][0]; 44 | const tag = mockHandleTag.args[0][1]; 45 | assert.equal(request.url, '/template'); 46 | assert.deepEqual(tag, { 47 | name: 'x-tag', 48 | attributes: { 49 | foo: 'bar' 50 | } 51 | }); 52 | response.resume(); 53 | response.on('end', done); 54 | }); 55 | }); 56 | 57 | it('replaces the original tag with stream or string content', done => { 58 | const st = new PassThrough(); 59 | mockTemplate.returns(''); 60 | mockHandleTag.onCall(0).returns(st); 61 | mockHandleTag.onCall(1).returns(''); 62 | http.get('http://localhost:8080/template', response => { 63 | let data = ''; 64 | response.on('data', chunk => (data += chunk)); 65 | response.on('end', () => { 66 | assert.equal( 67 | data, 68 | '' 69 | ); 70 | done(); 71 | }); 72 | }); 73 | st.write(''); 74 | st.end(''); 75 | }); 76 | 77 | it('should let us inject arbitrary html inside the tags', done => { 78 | mockTemplate.returns('
test
'); 79 | mockHandleTag.onCall(0).returns(''); 80 | mockHandleTag.onCall(1).returns(''); 81 | http.get('http://localhost:8080/template', response => { 82 | let data = ''; 83 | response.on('data', chunk => (data += chunk)); 84 | response.on('end', () => { 85 | assert.equal( 86 | data, 87 | '
test
' 88 | ); 89 | done(); 90 | }); 91 | }); 92 | }); 93 | }); 94 | -------------------------------------------------------------------------------- /tests/parse-link-header.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const parseLinkHeader = require('../lib/parse-link-header'); 4 | const assert = require('assert'); 5 | 6 | describe('Parse Link Header', () => { 7 | it('returns uri and rel of the passed header', () => { 8 | const linkHeader = 9 | '; rel="script",; rel="stylesheet"'; 10 | 11 | assert.deepStrictEqual(parseLinkHeader(linkHeader), [ 12 | { uri: 'http://a.com/app.js', rel: 'script', params: {} }, 13 | { uri: 'http://a.com/app.css', rel: 'stylesheet', params: {} } 14 | ]); 15 | }); 16 | 17 | it('parse attributes other than rel and uri', () => { 18 | const linkHeader = 19 | '; rel="script"; param1 = value1; param2 = "value2"; param3=value3'; 20 | 21 | assert.deepStrictEqual(parseLinkHeader(linkHeader), [ 22 | { 23 | uri: 'http://a.com/app.js', 24 | rel: 'script', 25 | params: { param1: 'value1', param2: 'value2', param3: 'value3' } 26 | } 27 | ]); 28 | }); 29 | 30 | it('correctly handles invalid header links, for backward compatibility reasons', () => { 31 | const linkHeader = 'http://a.com/app.js; rel=script'; 32 | 33 | assert.deepStrictEqual(parseLinkHeader(linkHeader), [ 34 | { uri: 'http://a.com/app.js', rel: 'script', params: {} } 35 | ]); 36 | }); 37 | 38 | it('filters invalid rel attributes', () => { 39 | const linkHeader = 40 | '; aaa="bbb" ; rel="script";, ; rel="stylesheet", ;, , ; rel=""'; 41 | 42 | assert.deepStrictEqual(parseLinkHeader(linkHeader), [ 43 | { 44 | uri: 'http://a.com/app.js', 45 | rel: 'script', 46 | params: { aaa: 'bbb' } 47 | }, 48 | { uri: 'http://a.com/app1.css', rel: 'stylesheet', params: {} } 49 | ]); 50 | }); 51 | 52 | it('do not modify query parms in link urls', () => { 53 | const linkHeader = '; rel="script";'; 54 | 55 | assert.deepStrictEqual(parseLinkHeader(linkHeader), [ 56 | { uri: 'http://a.com/app.js?nocache=1', rel: 'script', params: {} } 57 | ]); 58 | }); 59 | 60 | it('correctly handles empty link header', () => { 61 | const linkHeader = ''; 62 | 63 | assert.deepStrictEqual(parseLinkHeader(linkHeader), []); 64 | }); 65 | }); 66 | -------------------------------------------------------------------------------- /tests/parse-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const parseTemplate = require('../lib/parse-template'); 3 | const assert = require('assert'); 4 | const parseTempatePartial = parseTemplate([], []); 5 | 6 | describe('parseTemplate', () => { 7 | it('returns a Promise', () => { 8 | assert(parseTempatePartial('template') instanceof Promise); 9 | }); 10 | 11 | it('should reject with error for invalid templates', () => { 12 | const template = () => new Error('throw'); 13 | parseTempatePartial(template()).catch(err => { 14 | assert(err instanceof Error); 15 | }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /tests/process-template.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | // const http = require('http'); 5 | const nock = require('nock'); 6 | const sinon = require('sinon'); 7 | 8 | const stream = require('stream'); 9 | 10 | const processTemplate = require('../lib/process-template'); 11 | const requestFragment = require('../lib/request-fragment')( 12 | require('../lib/filter-headers'), 13 | require('../lib/process-fragment-response') 14 | ); 15 | const AsyncStream = require('../lib/streams/async-stream'); 16 | const parseTemplate = require('../lib/parse-template'); 17 | 18 | describe('processTemplate', () => { 19 | let resultStream; 20 | let options; 21 | let handleNestedTag; 22 | 23 | beforeEach(() => { 24 | const mockedRequest = { 25 | headers: {}, 26 | url: '/' 27 | }; 28 | 29 | nock('http://fragment') 30 | .get('/f1') 31 | .reply(200, '', { 32 | Link: 33 | '; rel="fragment-script", ; rel="fragment-script", ; rel="fragment-script",' + 34 | '; rel="fragment-script", ; rel="fragment-script", ; rel="fragment-script"' 35 | }) 36 | .get('/f2') 37 | .reply(200, '') 38 | .get('/f3') 39 | .reply(200, '') 40 | .get('/r1') 41 | .reply(200, '') 42 | .get('/r2') 43 | .reply(200, '') 44 | .get('/primary') 45 | .reply(200, '') 46 | .get('/bad') 47 | .reply(500, 'Internal Error'); 48 | 49 | handleNestedTag = (request, tag, options, context) => { 50 | if (tag && tag.name === 'nested-fragments') { 51 | // Simulate fetching of some remote resource here 52 | // TODO: who does the parsing??? 53 | const stream = processTemplate(request, options, context); 54 | setTimeout(() => { 55 | const remoteEndpointResponse = ` 56 | 57 | 58 | `; 59 | 60 | options 61 | .parseTemplate(remoteEndpointResponse, null, false) 62 | .then(template => { 63 | template.forEach(parsedTag => { 64 | stream.write(parsedTag); 65 | }); 66 | stream.end(); 67 | }); 68 | }, 10); 69 | return stream; 70 | } 71 | 72 | return Buffer.from(''); 73 | }; 74 | 75 | let index = 0; 76 | 77 | options = { 78 | maxAssetLinks: 3, 79 | nextIndex: () => ++index, 80 | fragmentTag: 'fragment', 81 | parseTemplate: parseTemplate( 82 | ['fragment', 'nested-fragments'], 83 | ['script'] 84 | ), 85 | asyncStream: new AsyncStream(), 86 | handleTag: handleNestedTag, 87 | requestFragment: requestFragment 88 | }; 89 | resultStream = processTemplate(mockedRequest, options, {}); 90 | }); 91 | 92 | it('returns a stream', () => { 93 | assert(resultStream instanceof stream); 94 | }); 95 | 96 | describe('result stream', () => { 97 | it('produces buffers as result', done => { 98 | resultStream.on('data', chunk => { 99 | assert(chunk instanceof Buffer); 100 | }); 101 | resultStream.on('end', () => { 102 | done(); 103 | }); 104 | resultStream.on('fragment:found', ({ attributes: { primary } }) => { 105 | primary && options.asyncStream.end(); 106 | }); 107 | resultStream.write({ placeholder: 'pipe' }); 108 | resultStream.write({ 109 | name: 'fragment', 110 | attributes: { id: 'f3', async: true, src: 'http://fragment/f3' } 111 | }); 112 | resultStream.write({ 113 | name: 'fragment', 114 | attributes: { id: 'f1', src: 'http://fragment/f1' } 115 | }); 116 | resultStream.write({ 117 | name: 'fragment', 118 | attributes: { 119 | id: 'f2', 120 | primary: true, 121 | src: 'http://fragment/primary' 122 | } 123 | }); 124 | resultStream.write({ name: 'nested-fragments' }); 125 | resultStream.write({ placeholder: 'async' }); 126 | resultStream.end(); 127 | }); 128 | 129 | it('notifies for every fragment found in the template', done => { 130 | const onFragment = sinon.spy(); 131 | resultStream.on('fragment:found', onFragment); 132 | resultStream.on('end', () => { 133 | assert.equal(onFragment.callCount, 3); 134 | done(); 135 | }); 136 | resultStream.resume(); 137 | resultStream.write({ 138 | name: 'fragment', 139 | attributes: { id: 'f3', async: true, src: 'http://fragment/f3' } 140 | }); 141 | resultStream.write({ 142 | name: 'fragment', 143 | attributes: { id: 'f1', src: 'http://fragment/f1' } 144 | }); 145 | resultStream.write({ 146 | name: 'fragment', 147 | attributes: { 148 | id: 'f2', 149 | primary: true, 150 | src: 'http://fragment/f2' 151 | } 152 | }); 153 | resultStream.end(); 154 | }); 155 | it('write async fragments to a separate stream', done => { 156 | let data = ''; 157 | options.asyncStream.on('data', chunk => { 158 | data += chunk.toString(); 159 | }); 160 | 161 | options.asyncStream.on('end', () => { 162 | assert.equal( 163 | data, 164 | '' 165 | ); 166 | done(); 167 | }); 168 | resultStream.write({ 169 | name: 'fragment', 170 | attributes: { 171 | id: 'f3', 172 | async: false, 173 | src: 'http://fragment/f2' 174 | } 175 | }); 176 | resultStream.write({ 177 | name: 'fragment', 178 | attributes: { id: 'f3', async: true, src: 'http://fragment/f3' } 179 | }); 180 | resultStream.end(); 181 | options.asyncStream.end(); 182 | }); 183 | it('handles indexes correctly', done => { 184 | let data = ''; 185 | 186 | function assertIndex(index, fragmentText) { 187 | const r = new RegExp( 188 | `.+${fragmentText}.+`, 189 | 'g' 190 | ); 191 | const doesMatch = r.test(data); 192 | let message = `No match for the fragmend(index: ${index}, text: ${fragmentText})`; 193 | assert.equal(doesMatch, true, message); 194 | } 195 | 196 | resultStream.on('data', chunk => { 197 | data += chunk.toString(); 198 | }); 199 | resultStream.on('end', () => { 200 | assertIndex(1, 'NormalFragment\\s3'); 201 | assertIndex(2, 'NormalFragment\\s1'); 202 | assertIndex(3, 'Primary'); // nested fragments are handled with delay 203 | assertIndex(4, 'RemoteFragment\\s1'); 204 | assertIndex(5, 'RemoteFragment\\s2'); 205 | done(); 206 | }); 207 | resultStream.on('fragment:found', ({ attributes: { primary } }) => { 208 | primary && options.asyncStream.end(); 209 | }); 210 | resultStream.write({ placeholder: 'pipe' }); 211 | resultStream.write({ 212 | name: 'fragment', 213 | attributes: { id: 'f3', async: true, src: 'http://fragment/f3' } 214 | }); 215 | resultStream.write({ 216 | name: 'fragment', 217 | attributes: { id: 'f1', src: 'http://fragment/f1' } 218 | }); 219 | resultStream.write({ name: 'nested-fragments' }); 220 | resultStream.write({ 221 | name: 'fragment', 222 | attributes: { 223 | id: 'f2', 224 | primary: true, 225 | src: 'http://fragment/primary' 226 | } 227 | }); 228 | resultStream.write({ placeholder: 'async' }); 229 | resultStream.end(); 230 | }); 231 | }); 232 | }); 233 | -------------------------------------------------------------------------------- /tests/request-fragment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const https = require('https'); 4 | const sinon = require('sinon'); 5 | const assert = require('assert'); 6 | const nock = require('nock'); 7 | const filterHeaderFn = () => ({}); 8 | const processFragmentResponseFn = require('../lib/process-fragment-response'); 9 | const requestFragment = require('../lib/request-fragment')( 10 | filterHeaderFn, 11 | processFragmentResponseFn 12 | ); 13 | 14 | describe('requestFragment', () => { 15 | let fragmentAttrb; 16 | beforeEach(() => { 17 | fragmentAttrb = { 18 | timeout: 1000 19 | }; 20 | }); 21 | 22 | it('Should request fragment using http protocol', done => { 23 | nock('http://fragment') 24 | .get('/') 25 | .reply(200, 'HTTP'); 26 | requestFragment('http://fragment/', fragmentAttrb, { 27 | headers: {} 28 | }).then(response => { 29 | const chunks = []; 30 | response.on('data', chunk => { 31 | chunks.push(chunk); 32 | }); 33 | response.on('end', () => { 34 | const data = Buffer.concat(chunks).toString('utf8'); 35 | assert.equal(data, 'HTTP'); 36 | done(); 37 | }); 38 | }); 39 | }); 40 | 41 | describe('while using HTTPS protocol', () => { 42 | let requestSpy; 43 | 44 | const request = { 45 | headers: {} 46 | }; 47 | 48 | beforeEach(() => { 49 | requestSpy = sinon.spy(https, 'request'); 50 | nock('https://fragment') 51 | .get('/') 52 | .reply(200, 'HTTPS'); 53 | }); 54 | 55 | afterEach(() => { 56 | requestSpy.restore(); 57 | }); 58 | 59 | it('Should request fragment', done => { 60 | const fragmentAttributes = { 61 | ...fragmentAttrb, 62 | ignoreInvalidSsl: false 63 | }; 64 | 65 | requestFragment( 66 | 'https://fragment/', 67 | fragmentAttributes, 68 | request 69 | ).then(response => { 70 | let chunks = []; 71 | response.on('data', chunk => { 72 | chunks.push(chunk); 73 | }); 74 | response.on('end', () => { 75 | const data = Buffer.concat(chunks).toString('utf8'); 76 | assert.equal(data, 'HTTPS'); 77 | assert.ok( 78 | requestSpy.neverCalledWithMatch({ 79 | rejectUnauthorized: false 80 | }) 81 | ); 82 | done(); 83 | }); 84 | }); 85 | }); 86 | 87 | it('Should ignore invalid SSL certificates while requesting a fragment', done => { 88 | const fragmentAttributes = { 89 | ...fragmentAttrb, 90 | ignoreInvalidSsl: true 91 | }; 92 | 93 | requestFragment( 94 | 'https://fragment/', 95 | fragmentAttributes, 96 | request 97 | ).then(response => { 98 | let chunks = []; 99 | response.on('data', chunk => { 100 | chunks.push(chunk); 101 | }); 102 | response.on('end', () => { 103 | const data = Buffer.concat(chunks).toString('utf8'); 104 | assert.equal(data, 'HTTPS'); 105 | assert.ok( 106 | requestSpy.calledWithMatch({ 107 | rejectUnauthorized: false 108 | }) 109 | ); 110 | done(); 111 | }); 112 | }); 113 | }); 114 | }); 115 | 116 | it('Should reject promise and respond with error for status code >500', done => { 117 | nock('http://fragment') 118 | .get('/') 119 | .reply(500, 'Internal Server Error'); 120 | requestFragment('http://fragment/', fragmentAttrb, { headers: {} }) 121 | .catch(err => { 122 | assert.equal( 123 | err.message, 124 | 'Request fragment error. statusCode: 500; statusMessage: null; url: http://fragment/;' 125 | ); 126 | }) 127 | .then(done, done); 128 | }); 129 | 130 | it('Should resolve promise for primary fragment with non 2xx response', done => { 131 | nock('http://fragment') 132 | .get('/') 133 | .reply(300, 'Redirect'); 134 | requestFragment( 135 | 'http://fragment/', 136 | { ...fragmentAttrb, primary: true }, 137 | { headers: {} } 138 | ) 139 | .then(response => { 140 | assert.equal(response.statusCode, 300); 141 | }) 142 | .then(done, done); 143 | }); 144 | 145 | it('Should reject promise for non primary fragment with non 2xx response', done => { 146 | nock('http://fragment') 147 | .get('/') 148 | .reply(300, 'Redirect'); 149 | requestFragment( 150 | 'http://fragment/', 151 | { ...fragmentAttrb, primary: false }, 152 | { headers: {} } 153 | ) 154 | .catch(err => { 155 | assert.equal( 156 | err.message, 157 | 'Request fragment error. statusCode: 300; statusMessage: null; url: http://fragment/;' 158 | ); 159 | }) 160 | .then(done, done); 161 | }); 162 | 163 | it('Should timeout when the fragment is not reachable', done => { 164 | nock('http://fragment') 165 | .get('/') 166 | .socketDelay(1001) 167 | .reply(200, 'hello'); 168 | requestFragment('http://fragment/', fragmentAttrb, { headers: {} }) 169 | .catch(err => { 170 | assert.equal(err.message, 'socket hang up'); 171 | }) 172 | .then(done, done); 173 | }); 174 | }); 175 | -------------------------------------------------------------------------------- /tests/serializer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const parse5 = require('parse5'); 5 | const adapter = parse5.treeAdapters.htmlparser2; 6 | const CustomSerializer = require('../lib/serializer'); 7 | 8 | describe('Serializer', () => { 9 | const defaultOpts = { 10 | treeAdapter: adapter, 11 | slotMap: new Map(), 12 | handleTags: ['x-tag', 'fragment', 'text/template'] 13 | }; 14 | const getSerializer = (template, fullRendering = true) => { 15 | const rootNode = parse5.parse(template, { treeAdapter: adapter }); 16 | const serializerOpts = Object.assign({}, defaultOpts, { 17 | fullRendering 18 | }); 19 | return new CustomSerializer(rootNode, serializerOpts); 20 | }; 21 | 22 | it('should output serialized buffer array', () => { 23 | const template = ''; 24 | const serializedList = getSerializer(template).serialize(); 25 | assert(serializedList instanceof Array); 26 | }); 27 | 28 | it('should output placeholder and closing tags for handle tags', () => { 29 | const template = ''; 30 | const serializedList = getSerializer(template).serialize(); 31 | assert.deepEqual(serializedList[1], { attributes: {}, name: 'x-tag' }); 32 | assert.deepEqual(serializedList[2], { closingTag: 'x-tag' }); 33 | }); 34 | 35 | it('should serialize attributes and child nodes inside handle tags', () => { 36 | const template = '
hello
'; 37 | const serializedList = getSerializer(template).serialize(); 38 | assert.deepEqual(serializedList[1], { 39 | attributes: { foo: 'bar' }, 40 | name: 'x-tag' 41 | }); 42 | assert.equal(serializedList[2], '
hello
'); 43 | assert.deepEqual(serializedList[3], { closingTag: 'x-tag' }); 44 | }); 45 | 46 | it('should insert async placeholder before end of body tag', () => { 47 | const template = ''; 48 | const serializedList = getSerializer(template).serialize(); 49 | assert.equal( 50 | serializedList[0].toString().trim(), 51 | '' 52 | ); 53 | assert.deepEqual(serializedList[3], { placeholder: 'async' }); 54 | assert.equal(serializedList[4].toString().trim(), ''); 55 | }); 56 | 57 | it('should not insert palceholders for partial rendering', () => { 58 | const template = ''; 59 | const serializedList = getSerializer(template, false).serialize(); 60 | assert.equal( 61 | serializedList[0].toString(), 62 | '' 63 | ); 64 | assert.deepEqual(serializedList[1], { name: 'x-tag', attributes: {} }); 65 | assert.deepEqual(serializedList[2], { closingTag: 'x-tag' }); 66 | assert.equal(serializedList[3].toString(), ''); 67 | }); 68 | 69 | it('should support script based custom tags for inserting in head', () => { 70 | const template = 71 | ''; 72 | const serializedList = getSerializer(template).serialize(); 73 | assert.equal(serializedList[0].toString().trim(), ''); 74 | assert.deepEqual(serializedList[1], { 75 | name: 'fragment', 76 | attributes: { 77 | type: 'fragment', 78 | primary: '', 79 | async: '', 80 | src: 'https://example.com' 81 | }, 82 | textContent: '' 83 | }); 84 | assert.equal( 85 | serializedList[3].toString().trim(), 86 | '' 87 | ); 88 | }); 89 | 90 | it('should not serialize script tags that are not text/javascript', () => { 91 | const template = 92 | ''; 93 | const serializedList = getSerializer(template).serialize(); 94 | assert.equal(serializedList[0].toString().trim(), ''); 95 | assert.deepEqual(serializedList[1], { 96 | name: 'text/template', 97 | attributes: { type: 'text/template' }, 98 | textContent: 'console.log("yo")' 99 | }); 100 | assert.equal( 101 | serializedList[3].toString().trim(), 102 | '' 103 | ); 104 | }); 105 | }); 106 | -------------------------------------------------------------------------------- /tests/streams/async-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const stream = require('stream'); 5 | const AsyncStream = require('../../lib/streams/async-stream'); 6 | 7 | describe('Async Stream', () => { 8 | it('should join streams in the order of data', done => { 9 | const asyncStream = new AsyncStream(); 10 | const st1 = new stream.PassThrough(); 11 | const st2 = new stream.PassThrough(); 12 | let data = ''; 13 | asyncStream.write(st1); 14 | asyncStream.end(st2); 15 | st2.end('two'); 16 | st1.end('one'); 17 | asyncStream.on('data', chunk => { 18 | data += chunk; 19 | }); 20 | asyncStream.on('end', () => { 21 | assert.equal(data, 'twoone'); 22 | done(); 23 | }); 24 | }); 25 | 26 | it('should end without streams', done => { 27 | const asyncStream = new AsyncStream(); 28 | asyncStream.on('data', () => {}); 29 | asyncStream.on('end', () => { 30 | done(); 31 | }); 32 | asyncStream.end(); 33 | }); 34 | 35 | it('should re-emit errors when fragment stream emit errors', done => { 36 | const asyncStream = new AsyncStream(); 37 | const st1 = new stream.PassThrough(); 38 | asyncStream.on('error', err => { 39 | assert.equal(err.message, 'Test'); 40 | done(); 41 | }); 42 | asyncStream.end(st1); 43 | st1.emit('error', new Error('Test')); 44 | st1.end('aa'); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /tests/streams/buffer-concat-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const assert = require('assert'); 3 | const BufferConcatStream = require('../../lib/streams/buffer-concat-stream'); 4 | 5 | describe('BufferConcatStream', () => { 6 | it('concats the input and calls the callback with the result', done => { 7 | const st = new BufferConcatStream(result => { 8 | assert(result.toString(), 'foobar'); 9 | done(); 10 | }); 11 | st.write(Buffer.from('foo')); 12 | st.end(Buffer.from('bar')); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /tests/streams/content-length-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const assert = require('assert'); 3 | const ContentLengthStream = require('../../lib/streams/content-length-stream'); 4 | const Transform = require('stream').Transform; 5 | 6 | describe('ContentLengthStream', () => { 7 | it('calculates content length and calls callback with the result', done => { 8 | const st = new ContentLengthStream(contentLength => { 9 | assert(contentLength, 'foobar'.length); 10 | done(); 11 | }); 12 | st.write(Buffer.from('foo')); 13 | st.end(Buffer.from('bar')); 14 | }); 15 | 16 | it('is a Transform stream', () => { 17 | const st = new ContentLengthStream(() => {}); 18 | assert(st instanceof Transform); 19 | }); 20 | 21 | it('passes through data chunks', done => { 22 | const chunk = Buffer.from('foo'); 23 | const st = new ContentLengthStream(() => {}); 24 | st.on('data', data => { 25 | assert.equal(data, chunk); 26 | done(); 27 | }); 28 | st.write(chunk); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /tests/streams/header-injector-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const assert = require('assert'); 3 | const HeadInjectorSream = require('../../lib/streams/head-injector-stream'); 4 | 5 | describe('HeadInjectorSream', () => { 6 | it('injects title and meta tags at the end of the "head" tag', done => { 7 | const PAGE1 = ''; 8 | const PAGE2 = ''; 9 | 10 | const TITLE = 'TEST TITLE'; 11 | const META_TAGS = 12 | ''; 13 | 14 | const PAGE_RES = `${TITLE}${META_TAGS}`; 15 | 16 | const headers = { 17 | 'x-head-title': Buffer.from(TITLE, 'utf-8').toString('base64'), 18 | 'x-head-meta': Buffer.from(META_TAGS, 'utf-8').toString('base64') 19 | }; 20 | 21 | let dataBuf = ''; 22 | 23 | const st = new HeadInjectorSream(headers); 24 | st.on('data', data => { 25 | dataBuf += data; 26 | }); 27 | st.on('end', () => { 28 | assert.equal(dataBuf, PAGE_RES); 29 | done(); 30 | }); 31 | st.write(Buffer.from(PAGE1)); 32 | st.write(Buffer.from(PAGE2)); 33 | st.end(); 34 | }); 35 | 36 | it('injects title and meta tags at the position of "title" tag', done => { 37 | const PAGE = 38 | '\n\nTPL_TITLE\n\n\n'; 39 | 40 | const TITLE = 'TEST TITLE'; 41 | const META_TAGS = 42 | ''; 43 | 44 | const PAGE_RES = `\n${TITLE}${META_TAGS}\n\n`; 45 | 46 | const headers = { 47 | 'x-head-title': Buffer.from(TITLE, 'utf-8').toString('base64'), 48 | 'x-head-meta': Buffer.from(META_TAGS, 'utf-8').toString('base64') 49 | }; 50 | 51 | const st = new HeadInjectorSream(headers); 52 | st.on('data', data => { 53 | assert.equal(data, PAGE_RES); 54 | done(); 55 | }); 56 | st.write(Buffer.from(PAGE)); 57 | }); 58 | 59 | it("doesn't break anything", done => { 60 | const PAGE1 = ''; 61 | const PAGE2 = ''; 62 | const PAGE = PAGE1 + PAGE2; 63 | 64 | const headers = {}; 65 | 66 | let dataBuf = ''; 67 | 68 | const st = new HeadInjectorSream(headers); 69 | st.on('data', data => { 70 | dataBuf += data; 71 | }); 72 | st.on('end', () => { 73 | assert.equal(dataBuf, PAGE); 74 | done(); 75 | }); 76 | st.write(Buffer.from(PAGE1)); 77 | st.write(Buffer.from(PAGE2)); 78 | st.end(); 79 | }); 80 | }); 81 | -------------------------------------------------------------------------------- /tests/streams/seobots-guard-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const assert = require('assert'); 3 | const sinon = require('sinon'); 4 | const util = require('util'); 5 | const setTimeoutPromise = util.promisify(setTimeout); 6 | const EventEmitter = require('events').EventEmitter; 7 | 8 | const BotGuardStream = require('../../lib/streams/seobots-guard-stream'); 9 | 10 | const resStub = { 11 | statusCode: 0, 12 | writeHead: () => {} 13 | }; 14 | const resStubSpy = sinon.spy(resStub, 'writeHead'); 15 | 16 | const headersStub = Object.freeze({ 17 | bot: { 'user-agent': 'Googlebot-Image/1.0' }, 18 | notBot: { 19 | 'user-agent': 20 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36' 21 | } 22 | }); 23 | 24 | class Fragment extends EventEmitter {} 25 | 26 | describe('SeoBotsGuardStream', () => { 27 | afterEach(() => { 28 | resStub.statusCode = 0; 29 | resStubSpy.resetHistory(); 30 | }); 31 | 32 | describe('when processes request from Bot', () => { 33 | it('prevents data flow until all fragments will respond', done => { 34 | const fragment1 = new Fragment(); 35 | const fragment2 = new Fragment(); 36 | let bothResponded = false; 37 | 38 | const st = new BotGuardStream(true, headersStub.bot, resStub); 39 | st.on('data', data => { 40 | data = Buffer.from(data).toString('utf-8'); 41 | 42 | try { 43 | assert.equal(data, 'some test data'); 44 | assert.ok( 45 | bothResponded, 46 | 'Data came after response of both fragments' 47 | ); 48 | assert.equal(resStub.statusCode, 200); 49 | assert(resStubSpy.calledOnceWith(200, { a: 'b' })); 50 | } catch (e) { 51 | return done(e); 52 | } 53 | 54 | done(); 55 | }); 56 | 57 | st.write(Buffer.from('some test data')); 58 | st.statusCode = 200; 59 | st.writeHead(200, { a: 'b' }); 60 | 61 | st.addFragment(fragment1); 62 | st.addFragment(fragment2); 63 | 64 | setTimeoutPromise(20) 65 | .then(() => fragment1.emit('response')) 66 | .then(() => setTimeoutPromise(20)) 67 | .then(() => { 68 | assert.equal(resStub.statusCode, 0); 69 | assert(resStubSpy.notCalled); 70 | fragment2.emit('response'); 71 | bothResponded = true; 72 | }) 73 | .then(() => setTimeoutPromise(20)) 74 | .then(() => st.end()) 75 | .catch(e => done(e)); 76 | }); 77 | 78 | it('emits "error" event while blocking data flow in case of one of the fragments error', done => { 79 | const fragment1 = new Fragment(); 80 | const fragment2 = new Fragment(); 81 | const fragment3 = new Fragment(); 82 | 83 | const st = new BotGuardStream(true, headersStub.bot, resStub); 84 | st.addFragment(fragment1); 85 | st.addFragment(fragment2); 86 | st.addFragment(fragment3); 87 | st.on('data', () => done(new Error('Failed to prevent data flow'))); 88 | st.on('error', err => { 89 | try { 90 | assert.equal(resStub.statusCode, 0); 91 | assert(resStubSpy.notCalled); 92 | assert.equal( 93 | err.message, 94 | 'Fragment error while processing request from SEO/SM bot. See adjacent messages for real cause.' 95 | ); 96 | } catch (e) { 97 | return done(e); 98 | } 99 | 100 | done(); 101 | }); 102 | 103 | st.write(Buffer.from('some test data')); 104 | st.statusCode = 200; 105 | st.writeHead(200, { a: 'b' }); 106 | 107 | setTimeoutPromise(20) 108 | .then(() => fragment1.emit('response')) 109 | .then(() => setTimeoutPromise(20)) 110 | .then(() => { 111 | fragment2.emit('error'); 112 | fragment3.emit('error'); 113 | st.write(Buffer.from('some test data')); 114 | }) 115 | .then(() => setTimeoutPromise(20)) 116 | .then(() => st.end()) 117 | .catch(e => done(e)); 118 | }); 119 | 120 | it('does nothing when disabled', done => { 121 | const st = new BotGuardStream(false, headersStub.bot, resStub); 122 | st.on('data', data => { 123 | data = Buffer.from(data).toString('utf-8'); 124 | 125 | try { 126 | assert.equal(data, 'some test data'); 127 | assert.equal(resStub.statusCode, 200); 128 | assert(resStubSpy.calledOnceWith(200, { a: 'b' })); 129 | } catch (e) { 130 | return done(e); 131 | } 132 | 133 | done(); 134 | }); 135 | 136 | st.statusCode = 200; 137 | st.writeHead(200, { a: 'b' }); 138 | st.write(Buffer.from('some test data')); 139 | }); 140 | }); 141 | describe('when processes request NOT from Bot', () => { 142 | it('does nothing', done => { 143 | const st = new BotGuardStream(true, headersStub.notBot, resStub); 144 | st.on('data', data => { 145 | data = Buffer.from(data).toString('utf-8'); 146 | 147 | try { 148 | assert.equal(data, 'some test data'); 149 | assert.equal(resStub.statusCode, 200); 150 | assert(resStubSpy.calledOnceWith(200, { a: 'b' })); 151 | } catch (e) { 152 | return done(e); 153 | } 154 | 155 | done(); 156 | }); 157 | 158 | st.statusCode = 200; 159 | st.writeHead(200, { a: 'b' }); 160 | st.write(Buffer.from('some test data')); 161 | }); 162 | }); 163 | }); 164 | -------------------------------------------------------------------------------- /tests/streams/stringifier-stream.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const PassThrough = require('stream').PassThrough; 3 | const assert = require('assert'); 4 | const parseTemplate = require('../../lib/parse-template'); 5 | const StringifierStream = require('../../lib/streams/stringifier-stream'); 6 | 7 | function getTemplate(template, specialTags, pipeBeforeTags) { 8 | specialTags = specialTags || ['fragment']; 9 | pipeBeforeTags = pipeBeforeTags || []; 10 | return parseTemplate(specialTags, pipeBeforeTags)(template); 11 | } 12 | 13 | describe('Stringifier Stream', () => { 14 | function getRandomDelay() { 15 | const max = 30; 16 | const min = 0; 17 | return Math.floor(Math.random() * (max - min + 1)) + min; 18 | } 19 | 20 | function writeDelayedDataToStream(data, stream) { 21 | const chunks = data.split(' '); 22 | setTimeout(() => scheduleChunk(chunks, stream, 0), getRandomDelay()); 23 | } 24 | 25 | function scheduleChunk(chunks, stream, index) { 26 | if (index === chunks.length - 1) { 27 | stream.end(chunks[index]); 28 | return; 29 | } else { 30 | stream.write(chunks[index]); 31 | } 32 | 33 | setTimeout( 34 | () => scheduleChunk(chunks, stream, index + 1), 35 | getRandomDelay() 36 | ); 37 | } 38 | 39 | it('should stream the content from a fragment tag', done => { 40 | let st = new PassThrough(); 41 | const templatePromise = getTemplate( 42 | '' 43 | ); 44 | templatePromise.then(nodes => { 45 | let data = ''; 46 | const stream = new StringifierStream(tag => { 47 | if (tag && tag.name) { 48 | assert.deepEqual(tag.attributes, { title: 'mock' }); 49 | return st; 50 | } 51 | return ''; 52 | }); 53 | stream.on('data', chunk => { 54 | data += chunk; 55 | }); 56 | stream.on('end', () => { 57 | assert.equal( 58 | data, 59 | 'mock' 60 | ); 61 | done(); 62 | }); 63 | nodes.forEach(node => stream.write(node)); 64 | stream.end(); 65 | st.end('mock'); 66 | }); 67 | }); 68 | 69 | it('should consume stream asynchronously', done => { 70 | const templatePromise = getTemplate( 71 | '' 72 | ); 73 | templatePromise.then(nodes => { 74 | let data = ''; 75 | let streams = [ 76 | new PassThrough(), 77 | new PassThrough(), 78 | new PassThrough() 79 | ]; 80 | const stream = new StringifierStream(tag => { 81 | if (tag && tag.name) { 82 | return streams[tag.attributes.id - 1]; 83 | } 84 | return ''; 85 | }); 86 | stream.on('data', chunk => { 87 | data += chunk; 88 | }); 89 | stream.on('end', () => { 90 | assert.equal( 91 | data, 92 | '123' 93 | ); 94 | done(); 95 | }); 96 | nodes.forEach(node => stream.write(node)); 97 | stream.end(); 98 | setTimeout(() => { 99 | streams[0].end('1'); 100 | }, 10); 101 | 102 | setTimeout(() => { 103 | streams[1].end('2'); 104 | }, 5); 105 | 106 | streams[2].end('3'); 107 | }); 108 | }); 109 | 110 | it('should flush the streams to the client in declared order', done => { 111 | const templatePromise = getTemplate( 112 | '' 113 | ); 114 | 115 | templatePromise.then(nodes => { 116 | let data = ''; 117 | let streams = [ 118 | new PassThrough(), 119 | new PassThrough(), 120 | new PassThrough({ objectMode: true }) 121 | ]; 122 | const stream = new StringifierStream(tag => { 123 | if (tag && tag.name) { 124 | return streams[tag.attributes.id - 1]; 125 | } 126 | return ''; 127 | }); 128 | stream.on('data', chunk => { 129 | data += chunk; 130 | }); 131 | stream.on('end', () => { 132 | assert.equal( 133 | data, 134 | 'DatafromS1DatafromS2DatafromS3' 135 | ); 136 | done(); 137 | }); 138 | nodes.forEach(node => stream.write(node)); 139 | stream.end(); 140 | 141 | writeDelayedDataToStream('Data from S1', streams[0]); 142 | writeDelayedDataToStream('Data from S2', streams[1]); 143 | writeDelayedDataToStream('Data from S3', streams[2]); 144 | }); 145 | }); 146 | 147 | it('should emit an error if a fragment is not handled', done => { 148 | getTemplate('').then(nodes => { 149 | let stream = new StringifierStream(); 150 | stream.on('error', error => { 151 | assert(error instanceof Error); 152 | done(); 153 | }); 154 | nodes.forEach(node => stream.write(node)); 155 | stream.end(); 156 | }); 157 | }); 158 | 159 | it('should re-emit errors from fragment streams', done => { 160 | getTemplate('').then(nodes => { 161 | let st = new PassThrough(); 162 | let stream = new StringifierStream(tag => { 163 | if (tag.name === 'fragment') { 164 | return st; 165 | } 166 | }); 167 | stream.on('error', error => { 168 | assert.equal(error.message, 'sorry!'); 169 | done(); 170 | }); 171 | nodes.forEach(node => stream.write(node)); 172 | stream.end(); 173 | st.emit('error', new Error('sorry!')); 174 | }); 175 | }); 176 | }); 177 | -------------------------------------------------------------------------------- /tests/tailor.events.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const http = require('http'); 5 | const nock = require('nock'); 6 | const sinon = require('sinon'); 7 | const Tailor = require('../index'); 8 | 9 | describe('Tailor events', () => { 10 | let server; 11 | let tailor; 12 | const mockTemplate = sinon.stub(); 13 | const mockContext = sinon.stub(); 14 | 15 | beforeEach(done => { 16 | tailor = new Tailor({ 17 | fetchContext: mockContext, 18 | pipeDefinition: () => Buffer.from(''), 19 | fetchTemplate: (request, parseTemplate) => { 20 | const template = mockTemplate(request); 21 | if (template) { 22 | return parseTemplate(template); 23 | } else { 24 | return Promise.reject('Error fetching template'); 25 | } 26 | } 27 | }); 28 | mockContext.returns(Promise.resolve({})); 29 | server = http.createServer(tailor.requestHandler); 30 | server.listen(8080, 'localhost', done); 31 | }); 32 | 33 | afterEach(done => { 34 | mockContext.reset(); 35 | mockTemplate.reset(); 36 | server.close(done); 37 | }); 38 | 39 | it('forwards `fragment:start(request, fragment)` event from a fragment', done => { 40 | const onFragmentStart = sinon.spy(); 41 | nock('https://fragment') 42 | .get('/') 43 | .reply(200, 'hello'); 44 | mockTemplate.returns(''); 45 | tailor.on('fragment:start', onFragmentStart); 46 | http.get('http://localhost:8080/template', response => { 47 | const request = onFragmentStart.args[0][0]; 48 | const fragment = onFragmentStart.args[0][1]; 49 | assert.equal(request.url, '/template'); 50 | assert.equal(fragment.url, 'https://fragment'); 51 | response.resume(); 52 | response.on('end', done); 53 | }); 54 | }); 55 | 56 | it('emits `start(request)` event', done => { 57 | const onStart = sinon.spy(); 58 | nock('https://fragment') 59 | .get('/') 60 | .reply(200, 'hello'); 61 | mockTemplate.returns(''); 62 | tailor.on('start', onStart); 63 | http.get('http://localhost:8080/template', response => { 64 | response.resume(); 65 | response.on('end', () => { 66 | const request = onStart.args[0][0]; 67 | assert.equal(request.url, '/template'); 68 | assert.equal(onStart.callCount, 1); 69 | done(); 70 | }); 71 | }); 72 | }); 73 | 74 | it('emits `response(request, statusCode, headers)` event', done => { 75 | const onResponse = sinon.spy(); 76 | mockTemplate.returns(''); 77 | tailor.on('response', onResponse); 78 | http.get('http://localhost:8080/template', response => { 79 | response.resume(); 80 | response.on('end', () => { 81 | const request = onResponse.args[0][0]; 82 | const statusCode = onResponse.args[0][1]; 83 | const headers = onResponse.args[0][2]; 84 | assert.equal(request.url, '/template'); 85 | assert.equal(statusCode, 200); 86 | assert.deepEqual(headers, { 87 | 'Cache-Control': 'no-cache, no-store, must-revalidate', 88 | 'Content-Type': 'text/html', 89 | Pragma: 'no-cache' 90 | }); 91 | assert.equal(onResponse.callCount, 1); 92 | done(); 93 | }); 94 | }); 95 | }); 96 | 97 | it('emits `end(request, contentSize)` event', done => { 98 | const onEnd = sinon.spy(); 99 | mockTemplate.returns( 100 | '

' 101 | ); 102 | tailor.on('end', onEnd); 103 | http.get('http://localhost:8080/template', response => { 104 | response.resume(); 105 | response.on('end', () => { 106 | const request = onEnd.args[0][0]; 107 | const contentSize = onEnd.args[0][1]; 108 | assert.equal(request.url, '/template'); 109 | assert.equal(contentSize, 48); 110 | assert.equal(onEnd.callCount, 1); 111 | done(); 112 | }); 113 | }); 114 | }); 115 | 116 | it('emits `error(request, error, response)` event on primary error/timeout', done => { 117 | const onPrimaryError = sinon.stub().callsFake((req, err, res) => { 118 | res.statusCode = 500; 119 | res.end('error response'); 120 | }); 121 | nock('https://fragment') 122 | .get('/') 123 | .reply(500); 124 | mockTemplate.returns( 125 | '' 126 | ); 127 | tailor.on('error', onPrimaryError); 128 | http.get('http://localhost:8080/template', response => { 129 | const request = onPrimaryError.args[0][0]; 130 | const error = onPrimaryError.args[0][1]; 131 | 132 | assert.equal(request.url, '/template'); 133 | assert.equal(error.message, 'Fragment error for "tst_fragment"'); 134 | 135 | let rawData = ''; 136 | response.on('data', chunk => (rawData += chunk)); 137 | response.on('end', () => { 138 | assert.equal(rawData, 'error response'); 139 | done(); 140 | }); 141 | }); 142 | }); 143 | 144 | it('emits `error(request, error, response)` event on template error', done => { 145 | const onTemplateError = sinon.stub().callsFake((req, err, res) => { 146 | res.statusCode = 500; 147 | res.end('error response'); 148 | }); 149 | mockTemplate.returns(false); 150 | tailor.on('error', onTemplateError); 151 | http.get('http://localhost:8080/template', response => { 152 | const request = onTemplateError.args[0][0]; 153 | const error = onTemplateError.args[0][1]; 154 | 155 | assert.equal(request.url, '/template'); 156 | assert.equal(error, 'Error fetching template'); 157 | 158 | let rawData = ''; 159 | response.on('data', chunk => (rawData += chunk)); 160 | response.on('end', () => { 161 | assert.equal(rawData, 'error response'); 162 | done(); 163 | }); 164 | }); 165 | }); 166 | 167 | it('emits `error(request, error, response)` event on error in context function', done => { 168 | const errMsg = 'Error fetching context'; 169 | const onContextError = sinon.spy((req, err, res) => { 170 | res.statusCode = 500; 171 | res.end(); 172 | }); 173 | tailor.on('error', onContextError); 174 | 175 | const rejectPrm = Promise.reject(errMsg); 176 | rejectPrm.catch(() => {}); 177 | mockContext.returns(rejectPrm); 178 | 179 | mockTemplate.returns(''); 180 | 181 | http.get('http://localhost:8080/template', response => { 182 | const request = onContextError.args[0][0]; 183 | const error = onContextError.args[0][1]; 184 | assert.equal(request.url, '/template'); 185 | assert.equal(error, errMsg); 186 | response.resume(); 187 | response.on('end', done); 188 | }); 189 | }); 190 | }); 191 | -------------------------------------------------------------------------------- /tests/template-cutter.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const TemplateCutter = require('../lib/template-cutter'); 5 | 6 | describe('TemplateCutter', () => { 7 | let templateCutter; 8 | 9 | beforeEach(() => { 10 | templateCutter = new TemplateCutter(); 11 | }); 12 | 13 | it('should cut and restore a base template and ignore comment tags without a pair', () => { 14 | const baseTemplate = 15 | '' + 16 | '' + 17 | '' + 18 | '' + 19 | '' + 20 | '' + 21 | '' + 22 | '' + 23 | '
' + 24 | '' + 25 | '
' + 26 | '' + 27 | '' + 28 | '' + 31 | '
' + 32 | '' + 33 | '
' + 34 | '' + 35 | '
' + 36 | '' + 37 | '
' + 38 | '' + 39 | '
' + 40 | '' + 41 | '
' + 42 | '' + 43 | '' + 46 | '' + 47 | ''; 48 | 49 | const correctCuttedBaseTemplate = 50 | '' + 51 | '' + 52 | '' + 53 | '' + 54 | '' + 55 | '' + 56 | '' + 57 | '' + 58 | '' + 59 | '' + 62 | '
' + 63 | '' + 64 | '
' + 65 | '' + 66 | '
' + 67 | '' + 68 | '
' + 69 | '' + 70 | '' + 73 | '' + 74 | ''; 75 | 76 | const chunks = [ 77 | '', 78 | '', 79 | Buffer.from( 80 | '' + 81 | '' + 82 | '' + 83 | '', 84 | 'utf-8' 85 | ), 86 | Buffer.from('', 'utf-8'), 87 | '' + 88 | '' + 89 | '', 92 | Buffer.from(`{}`, 'utf-8'), 93 | Buffer.from( 94 | '
' + 95 | '' + 96 | '
' + 97 | '' + 98 | '
' + 99 | '' + 100 | '
', 101 | 'utf-8' 102 | ), 103 | '' + 104 | '' + 107 | '' + 108 | '' 109 | ]; 110 | 111 | const correctRestoredFirstSlot = 112 | '' + 113 | '
' + 114 | '' + 115 | '
' + 116 | '' + 117 | '' + 118 | ''; 121 | const correctRestoredSecondSlot = 122 | '
' + 123 | '' + 124 | '
' + 125 | '' + 126 | '
' + 127 | '' + 128 | '
' + 129 | '' + 130 | '
' + 131 | '' + 132 | '
'; 133 | 134 | const cuttedBaseTemplate = templateCutter.cut(baseTemplate); 135 | const restoredChunks = templateCutter.restore(chunks); 136 | 137 | assert.equal(cuttedBaseTemplate, correctCuttedBaseTemplate); 138 | assert.equal(restoredChunks[4], correctRestoredFirstSlot); 139 | assert.equal( 140 | restoredChunks[6].toString('utf-8'), 141 | correctRestoredSecondSlot 142 | ); 143 | assert.equal(restoredChunks.length, chunks.length); 144 | assert.equal(templateCutter.ignoredParts.length, 0); 145 | }); 146 | 147 | it('should throw an error when a cutter can not find an ignored part while restoring a base template', () => { 148 | const baseTemplate = 149 | '' + 150 | '' + 151 | '' + 152 | '' + 153 | '' + 154 | '' + 155 | '' + 156 | '' + 157 | '
' + 158 | '' + 159 | '
' + 160 | '' + 161 | '' + 162 | ''; 163 | 164 | const chunks = [ 165 | '', 166 | '', 167 | Buffer.from( 168 | '' + 169 | '' + 170 | '' + 171 | '', 172 | 'utf-8' 173 | ), 174 | Buffer.from('', 'utf-8'), 175 | '' + 176 | '' + 177 | '' + 178 | '' 179 | ]; 180 | 181 | let catchedError; 182 | 183 | templateCutter.cut(baseTemplate); 184 | 185 | try { 186 | templateCutter.restore(chunks); 187 | } catch (error) { 188 | catchedError = error; 189 | } 190 | 191 | assert.equal( 192 | catchedError.message, 193 | 'TailorX can not find an ignored part 100 of the current template during restoring!' 194 | ); 195 | }); 196 | }); 197 | -------------------------------------------------------------------------------- /tests/transform.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const sinon = require('sinon'); 5 | const proxyquire = require('proxyquire').noPreserveCache(); 6 | 7 | describe('Transform', () => { 8 | let Transform; 9 | let transformInstance; 10 | 11 | class MockSerializer { 12 | constructor(node, options) { 13 | this.node = node; 14 | this.options = options; 15 | } 16 | serialize() {} 17 | } 18 | 19 | const mockSerializer = sinon.spy(function() { 20 | return sinon.createStubInstance(MockSerializer); 21 | }); 22 | const handleTags = ['x-tag']; 23 | const baseTemplatesCacheSize = 1; 24 | 25 | beforeEach(() => { 26 | Transform = proxyquire('../lib/transform', { 27 | './serializer': mockSerializer 28 | }); 29 | transformInstance = new Transform(handleTags, baseTemplatesCacheSize); 30 | }); 31 | 32 | afterEach(() => mockSerializer.resetHistory()); 33 | 34 | it('should make child Templates optional', () => { 35 | const childTemplate = ''; 36 | transformInstance.applyTransforms('', childTemplate); 37 | assert.equal(mockSerializer.callCount, 1); // No errros are thrown 38 | }); 39 | 40 | it('should put tags in default slot if type is not defined', () => { 41 | const childTemplate = ''; 42 | transformInstance.applyTransforms('', childTemplate); 43 | const slotMap = mockSerializer.args[0][1].slotMap; 44 | assert(slotMap.has('default')); 45 | }); 46 | 47 | it('should put comment tags in default slot', () => { 48 | const childTemplate = ''; 49 | transformInstance.applyTransforms('', childTemplate); 50 | const slotMap = mockSerializer.args[0][1].slotMap; 51 | assert(slotMap.has('default')); 52 | }); 53 | 54 | it('should group slots based on slot types for child Templates', () => { 55 | const childTemplate = ` 56 | 57 | 58 | 59 | `; 60 | transformInstance.applyTransforms('', childTemplate); 61 | const slotMap = mockSerializer.args[0][1].slotMap; 62 | assert.equal(slotMap.size, 3); 63 | assert.ok(slotMap.get('default')); 64 | assert.ok(slotMap.has('head')); 65 | assert.ok(slotMap.has('body')); 66 | }); 67 | 68 | it('should group text nodes along with the childTemplate nodes', () => { 69 | const childTemplate = ` 70 | 71 | 72 | `; 73 | transformInstance.applyTransforms('', childTemplate); 74 | const slotMap = mockSerializer.args[0][1].slotMap; 75 | assert.equal(slotMap.size, 2); 76 | // Text node that symbolizes next line of HTML 77 | assert.equal(slotMap.get('default')[1].type, 'text'); 78 | assert.equal(slotMap.get('head')[1].type, 'text'); 79 | }); 80 | 81 | it('should call serializer with proper options', () => { 82 | transformInstance.applyTransforms('', ''); 83 | const options = mockSerializer.args[0][1]; 84 | assert(options.slotMap instanceof Map); 85 | assert(options.treeAdapter instanceof Object); 86 | assert.equal(options.handleTags, handleTags); 87 | }); 88 | }); 89 | --------------------------------------------------------------------------------