-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* expect rawBody and parse inside kit * update adapter-node * ignore type error for now * random lockfile stuff * update docs * add wrangler.toml * use rawBody, bundle worker with esbuild * fix dependencies * fix dependencies * update lockfile * ugh who asked you, eslint * changesets * add test * pass rawBody from netlify adapter * expose getRawBody from kit/http * use getRawBody in adapter-node * use getRawBody in adapter-vercel
- Loading branch information
Rich Harris
authored
Apr 18, 2021
1 parent
361bd3b
commit 1237eb3
Showing
36 changed files
with
410 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-node': patch | ||
--- | ||
|
||
Use getRawBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-cloudflare-workers': patch | ||
--- | ||
|
||
Pass rawBody to SvelteKit, bundle worker with esbuild |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-vercel': patch | ||
--- | ||
|
||
Fix dependencies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-netlify': patch | ||
--- | ||
|
||
Fix dependencies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Expose rawBody on request, and expect rawBody from adapters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-netlify': patch | ||
--- | ||
|
||
Pass rawBody from netlify adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Expose getRawBody from kit/http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/adapter-vercel': patch | ||
--- | ||
|
||
Use getRawBody in adapter-vercel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ yarn.lock | |
.vercel_build_output | ||
.netlify | ||
.svelte | ||
.cloudflare |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,95 @@ | ||
'use strict'; | ||
|
||
const { exec } = require('child_process'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { execSync } = require('child_process'); | ||
const esbuild = require('esbuild'); | ||
const toml = require('toml'); | ||
|
||
module.exports = function () { | ||
/** @type {import('@sveltejs/kit').Adapter} */ | ||
const adapter = { | ||
name: '@sveltejs/adapter-cloudflare-workers', | ||
async adapt(utils) { | ||
let wrangler_config; | ||
|
||
if (fs.existsSync('wrangler.toml')) { | ||
try { | ||
wrangler_config = toml.parse(fs.readFileSync('wrangler.toml', 'utf-8')); | ||
} catch (err) { | ||
err.message = `Error parsing wrangler.toml: ${err.message}`; | ||
throw err; | ||
} | ||
} else { | ||
// TODO offer to create one? | ||
throw new Error( | ||
'Missing a wrangler.toml file. Consult https://developers.cloudflare.com/workers/platform/sites/configuration on how to setup your site' | ||
); | ||
} | ||
|
||
if (!wrangler_config.site || !wrangler_config.site.bucket) { | ||
throw new Error( | ||
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration' | ||
); | ||
} | ||
|
||
const bucket = path.resolve(wrangler_config.site.bucket); | ||
const entrypoint = path.resolve(wrangler_config.site['entry-point'] ?? 'workers-site'); | ||
const { site } = validate_config(utils); | ||
|
||
utils.copy_static_files(bucket); | ||
utils.copy_client_files(bucket); | ||
utils.copy_server_files(entrypoint); | ||
const bucket = site.bucket; | ||
const entrypoint = site['entry-point'] || 'workers-site'; | ||
|
||
utils.rimraf(bucket); | ||
utils.rimraf(entrypoint); | ||
|
||
// copy the renderer | ||
utils.copy(path.resolve(__dirname, 'files/render.js'), `${entrypoint}/index.js`); | ||
utils.copy(path.resolve(__dirname, 'files/_package.json'), `${entrypoint}/package.json`); | ||
utils.log.info('Installing worker dependencies...'); | ||
utils.copy(`${__dirname}/files/_package.json`, '.svelte/cloudflare-workers/package.json'); | ||
|
||
// TODO would be cool if we could make this step unnecessary somehow | ||
const stdout = execSync('npm install', { cwd: '.svelte/cloudflare-workers' }); | ||
utils.log.info(stdout.toString()); | ||
|
||
utils.log.minor('Generating worker...'); | ||
utils.copy(`${__dirname}/files/entry.js`, '.svelte/cloudflare-workers/entry.js'); | ||
|
||
await esbuild.build({ | ||
entryPoints: ['.svelte/cloudflare-workers/entry.js'], | ||
outfile: `${entrypoint}/index.js`, | ||
bundle: true, | ||
platform: 'node' | ||
}); | ||
|
||
utils.log.info('Prerendering static pages...'); | ||
await utils.prerender({ | ||
dest: bucket | ||
}); | ||
|
||
utils.log.info('Installing Worker Dependencies...'); | ||
exec( | ||
'npm install', | ||
{ | ||
cwd: entrypoint | ||
}, | ||
(error, stdout, stderr) => { | ||
utils.log.info(stderr); | ||
if (error) { | ||
utils.log.error(error); | ||
} | ||
} | ||
); | ||
utils.log.minor('Copying assets...'); | ||
utils.copy_static_files(bucket); | ||
utils.copy_client_files(bucket); | ||
} | ||
}; | ||
|
||
return adapter; | ||
}; | ||
|
||
function validate_config(utils) { | ||
if (fs.existsSync('wrangler.toml')) { | ||
let wrangler_config; | ||
|
||
try { | ||
wrangler_config = toml.parse(fs.readFileSync('wrangler.toml', 'utf-8')); | ||
} catch (err) { | ||
err.message = `Error parsing wrangler.toml: ${err.message}`; | ||
throw err; | ||
} | ||
|
||
if (!wrangler_config.site || !wrangler_config.site.bucket) { | ||
throw new Error( | ||
'You must specify site.bucket in wrangler.toml. Consult https://developers.cloudflare.com/workers/platform/sites/configuration' | ||
); | ||
} | ||
|
||
return wrangler_config; | ||
} | ||
|
||
utils.log.error( | ||
'Consult https://developers.cloudflare.com/workers/platform/sites/configuration on how to setup your site' | ||
); | ||
|
||
utils.log( | ||
` | ||
Sample wrangler.toml: | ||
name = "<your-site-name>" | ||
type = "javascript" | ||
account_id = "<your-account-id>" | ||
workers_dev = true | ||
route = "" | ||
zone_id = "" | ||
[site] | ||
bucket = "./.cloudflare/assets" | ||
entry-point = "./.cloudflare/worker"` | ||
.replace(/^\t+/gm, '') | ||
.trim() | ||
); | ||
|
||
throw new Error('Missing a wrangler.toml file'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
1237eb3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: