diff --git a/packages/adapter-node/package.json b/packages/adapter-node/package.json index 3867e5abc579..430ba7c583dd 100644 --- a/packages/adapter-node/package.json +++ b/packages/adapter-node/package.json @@ -15,7 +15,10 @@ "prepublishOnly": "npm run build" }, "devDependencies": { + "@rollup/plugin-json": "^4.1.0", "@sveltejs/app-utils": "workspace:*", + "compression": "^1.7.4", + "polka": "^0.5.2", "rollup": "^2.38.3", "sirv": "^1.0.11" } diff --git a/packages/adapter-node/rollup.config.js b/packages/adapter-node/rollup.config.js index 96d88977e2f6..1ae7ff01d547 100644 --- a/packages/adapter-node/rollup.config.js +++ b/packages/adapter-node/rollup.config.js @@ -1,5 +1,6 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; +import json from '@rollup/plugin-json'; export default { input: 'src/server.js', @@ -8,6 +9,6 @@ export default { format: 'esm', sourcemap: true }, - plugins: [nodeResolve(), commonjs()], + plugins: [nodeResolve(), commonjs(), json()], external: ['./app.js', ...require('module').builtinModules] }; diff --git a/packages/adapter-node/src/server.js b/packages/adapter-node/src/server.js index 14f54832f947..9bfa281a2fe3 100644 --- a/packages/adapter-node/src/server.js +++ b/packages/adapter-node/src/server.js @@ -1,8 +1,9 @@ +import compression from 'compression'; import * as fs from 'fs'; -import * as http from 'http'; +import polka from 'polka'; import { dirname, join } from 'path'; -import { parse, URLSearchParams, fileURLToPath } from 'url'; import sirv from 'sirv'; +import { parse, URLSearchParams, fileURLToPath } from 'url'; import { get_body } from '@sveltejs/app-utils/http'; // App is a dynamic file built from the application layer. /*eslint import/no-unresolved: [2, { ignore: ['\.\/app\.js$'] }]*/ @@ -27,30 +28,29 @@ const assets_handler = sirv(join(__dirname, '/assets'), { immutable: true }); -const server = http.createServer((req, res) => { - const parsed = parse(req.url || ''); - - assets_handler(req, res, () => { - prerendered_handler(req, res, async () => { - const rendered = await app.render({ - method: req.method, - headers: req.headers, // TODO: what about repeated headers, i.e. string[] - path: parsed.pathname, - body: await get_body(req), - query: new URLSearchParams(parsed.query || '') - }); - - if (rendered) { - res.writeHead(rendered.status, rendered.headers); - res.end(rendered.body); - } else { - res.statusCode = 404; - res.end('Not found'); - } +polka() + .use(compression({ threshold: 0 }), assets_handler, prerendered_handler, async (req, res) => { + const parsed = parse(req.url || ''); + const rendered = await app.render({ + method: req.method, + headers: req.headers, // TODO: what about repeated headers, i.e. string[] + path: parsed.pathname, + body: await get_body(req), + query: new URLSearchParams(parsed.query || '') }); - }); -}); -server.listen(PORT, () => { - console.log(`Listening on port ${PORT}`); -}); + if (rendered) { + res.writeHead(rendered.status, rendered.headers); + res.end(rendered.body); + } else { + res.statusCode = 404; + res.end('Not found'); + } + }) + .listen(PORT, (err) => { + if (err) { + console.log('error', err); + } else { + console.log(`Listening on port ${PORT}`); + } + }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 8c8fd9069c63..c4ad6c6a7251 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -92,11 +92,17 @@ importers: toml: ^3.0.0 packages/adapter-node: devDependencies: + '@rollup/plugin-json': 4.1.0_rollup@2.40.0 '@sveltejs/app-utils': link:../app-utils + compression: 1.7.4 + polka: 0.5.2 rollup: 2.40.0 sirv: 1.0.11 specifiers: + '@rollup/plugin-json': ^4.1.0 '@sveltejs/app-utils': workspace:* + compression: ^1.7.4 + polka: ^0.5.2 rollup: ^2.38.3 sirv: ^1.0.11 packages/adapter-static: @@ -262,7 +268,14 @@ packages: engines: node: '>=10' resolution: - integrity: sha512-mXn5Ha5a3tEOV5vkGHLdrDinSRfO8g9s72gKuOWA13HJtGcfI+jkYrEdQbLqe63gz0RPQN27h0e1IKc2kSzqHw== + integrity: sha512-5VnJNg4O49QSAW2hYPpG0pkgwBF5xGzQTn2fiNifztCynLrGNgyaloFrn1eVMIGDjq85+x6a5jai+zKN+dKUSA== + tarball: '@architect/parser/-/parser-3.0.1.tgz' + /@arr/every/1.0.1: + dev: true + engines: + node: '>=4' + resolution: + integrity: sha512-UQFQ6SgyJ6LX42W8rHCs8KVc0JS0tzVL9ct4XYedJukskYVWTo49tNiMEK9C2HTyarbNiT/RVIRSY82vH+6sTg== /@babel/code-frame/7.12.11: dependencies: '@babel/highlight': 7.13.8 @@ -539,6 +552,10 @@ packages: node: '>= 8' resolution: integrity: sha512-8Broas6vTtW4GIXTAHDoE32hnN2M5ykgCpWGbuXHQ15vEMqr23pB76e/GZcYsZCHALv50ktd24qhEyKr6wBtow== + /@polka/url/0.5.0: + dev: true + resolution: + integrity: sha512-oZLYFEAzUKyi3SKnXvj32ZCEGH6RDnao7COuCVhDydMS9NrCSVXhM79VaKyP5+Zc33m0QXEd2DN3UkU7OsHcfw== /@polka/url/1.0.0-next.11: dev: true resolution: @@ -782,6 +799,12 @@ packages: node: ^8.10.0 || ^10.13.0 || >=11.10.1 resolution: integrity: sha512-6IlZv9JaurqV0jkEg923cV49aAn8V6+1H1DRfhRcvZUrptQ+UtSKHb5kwTayzOYTJJ/RsYZdcvhOEKiBLyc0Cw== + /@typescript-eslint/types/4.14.2: + dev: true + engines: + node: ^8.10.0 || ^10.13.0 || >=11.10.1 + resolution: + integrity: sha512-LltxawRW6wXy4Gck6ZKlBD05tCHQUj4KLn4iR69IyRiDHX3d3NCAhO+ix5OR2Q+q9bjCrHE/HKt+riZkd1At8Q== /@typescript-eslint/types/4.16.1: dev: true engines: @@ -810,13 +833,23 @@ packages: integrity: sha512-m8I/DKHa8YbeHt31T+UGd/l8Kwr0XCTCZL3H4HMvvLCT7HU9V7yYdinTOv1gf/zfqNeDcCgaFH2BMsS8x6NvJg== /@typescript-eslint/visitor-keys/4.16.1: dependencies: - '@typescript-eslint/types': 4.16.1 + '@typescript-eslint/types': 4.14.2 eslint-visitor-keys: 2.0.0 dev: true engines: node: ^8.10.0 || ^10.13.0 || >=11.10.1 resolution: - integrity: sha512-s/aIP1XcMkEqCNcPQtl60ogUYjSM8FU2mq1O7y5cFf3Xcob1z1iXWNB6cC43Op+NGRTFgGolri6s8z/efA9i1w== + integrity: sha512-KBB+xLBxnBdTENs/rUgeUKO0UkPBRs2vD09oMRRIkj5BEN8PX1ToXV532desXfpQnZsYTyLLviS7JrPhdL154w== + tarball: '@typescript-eslint/visitor-keys/-/visitor-keys-4.16.1.tgz' + /accepts/1.3.7: + dependencies: + mime-types: 2.1.29 + negotiator: 0.6.2 + dev: true + engines: + node: '>= 0.6' + resolution: + integrity: sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA== /acorn-jsx/5.3.1_acorn@7.4.1: dependencies: acorn: 7.4.1 @@ -1017,6 +1050,12 @@ packages: node: '>=6' resolution: integrity: sha512-lGzLKcioL90C7wMczpkY0n/oART3MbBa8R9OFGE1rJxoVI86u4WAGfEk8Wjv10eKSyTHVGkSo3bvBylCEtk7LA== + /bytes/3.0.0: + dev: true + engines: + node: '>= 0.8' + resolution: + integrity: sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg= /call-bind/1.0.2: dependencies: function-bind: 1.1.1 @@ -1165,6 +1204,28 @@ packages: dev: true resolution: integrity: sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs= + /compressible/2.0.18: + dependencies: + mime-db: 1.46.0 + dev: true + engines: + node: '>= 0.6' + resolution: + integrity: sha512-AF3r7P5dWxL8MxyITRMlORQNaOA2IkAFaTr4k7BUumjPtRpGDTZpl0Pb1XCO6JeDCBdp126Cgs9sMxqSjgYyRg== + /compression/1.7.4: + dependencies: + accepts: 1.3.7 + bytes: 3.0.0 + compressible: 2.0.18 + debug: 2.6.9 + on-headers: 1.0.2 + safe-buffer: 5.1.2 + vary: 1.1.2 + dev: true + engines: + node: '>= 0.8.0' + resolution: + integrity: sha512-jaSIDzP9pZVS4ZfQ+TzvtiWhdpFhE2RDHz8QJkpX9SIpLq88VueF5jJw6t+6CUQcAoA6t+x89MLrWAqpfDE8iQ== /concat-map/0.0.1: dev: true resolution: @@ -2316,7 +2377,16 @@ packages: node: '>= 8.16.2' hasBin: true resolution: - integrity: sha512-H8lIX2SvyitGX+TRdtS06m1jHMijKN/XjfH6Ooii9fvxMlh8QdqBfBDkGUpMWH2kQNrtixjzYUa3SH8ROTgRRw== + integrity: sha512-lzmFjGnzWHkmbk85q/ILZjFoHHJIQGF+SxGEfIdGk/XhiTPhqGs37gbru6Kkd48diJnEyYwnG67nru0Z2gQtuQ== + tarball: marked/-/marked-1.2.9.tgz + /matchit/1.1.0: + dependencies: + '@arr/every': 1.0.1 + dev: true + engines: + node: '>=6' + resolution: + integrity: sha512-+nGYoOlfHmxe5BW5tE0EMJppXEwdSf8uBA1GTZC7Q77kbT35+VKLYJMzVNWCHSsga1ps1tPYFtFyvxvKzWVmMA== /meow/6.1.1: dependencies: '@types/minimist': 1.2.1 @@ -2426,6 +2496,12 @@ packages: dev: true resolution: integrity: sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= + /negotiator/0.6.2: + dev: true + engines: + node: '>= 0.6' + resolution: + integrity: sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw== /node-fetch/2.6.1: engines: node: 4.x || >=6.0.0 @@ -2479,7 +2555,14 @@ packages: engines: node: '>= 0.4' resolution: - integrity: sha512-nkF6PfDB9alkOUxpf1HNm/QlkeW3SReqL5WXeBLpEJJnlPSvRaDQpW3gQTksTN3fgJX4hL42RzKyOin6ff3tyw== + integrity: sha512-MYC0jvJopr8EK6dPBiO8Nb9mvjdypOachO5REGk6MXzujbBrAisKo3HmdEI6kZDL6fC31Mwee/5YbtMebixeag== + tarball: object.values/-/object.values-1.1.3.tgz + /on-headers/1.0.2: + dev: true + engines: + node: '>= 0.8' + resolution: + integrity: sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA== /once/1.4.0: dependencies: wrappy: 1.0.2 @@ -2729,6 +2812,13 @@ packages: node: '>=10.13.0' resolution: integrity: sha512-40QW5YalBNfQo5yRYmiw7Yz6TKKVr3h6970B2YE+3fQpsWcrbj1PzJgxeJ19DRQjhMbKPIuMY8rFaXc8moolVw== + /polka/0.5.2: + dependencies: + '@polka/url': 0.5.0 + trouter: 2.0.1 + dev: true + resolution: + integrity: sha512-FVg3vDmCqP80tOrs+OeNlgXYmFppTXdjD5E7I4ET1NjvtNmQrb1/mJibybKkb/d4NA7YWAr1ojxuhpL3FHqdlw== /port-authority/1.1.2: dev: true resolution: @@ -2985,6 +3075,10 @@ packages: node: '>= 6' resolution: integrity: sha512-y5yauMD93rX840MwUJr7C1ysLFBgMspsdTo4UVrDg3fXDvtwOyIqykhVAAm6fk/3au77773itJStObgK+LKaiA== + /safe-buffer/5.1.2: + dev: true + resolution: + integrity: sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g== /safer-buffer/2.1.2: dev: true resolution: @@ -3319,6 +3413,14 @@ packages: node: '>=8' resolution: integrity: sha512-C4+gOpvmxaSMKuEf9Qc134F1ZuOHVXKRbtEflf4NTtuuJDEIJ9p5PXsalL8SkeRw+qit1Mo+yuvMPAKwWg/1hA== + /trouter/2.0.1: + dependencies: + matchit: 1.1.0 + dev: true + engines: + node: '>=6' + resolution: + integrity: sha512-kr8SKKw94OI+xTGOkfsvwZQ8mWoikZDd2n8XZHjJVZUARZT+4/VV6cacRS6CLsH9bNm+HFIPU1Zx4CnNnb4qlQ== /tsconfig-paths/3.9.0: dependencies: '@types/json5': 0.0.29 @@ -3439,6 +3541,12 @@ packages: dev: true resolution: integrity: sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew== + /vary/1.1.2: + dev: true + engines: + node: '>= 0.8' + resolution: + integrity: sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= /vite/2.0.4: dependencies: esbuild: 0.8.52