2021-07-15 09:38:06 +00:00
|
|
|
import { resolve } from 'path'
|
|
|
|
import { promises as fsp } from 'fs'
|
2021-09-22 20:31:58 +00:00
|
|
|
import { TextEncoder, TextDecoder } from 'util'
|
2021-04-23 19:52:32 +00:00
|
|
|
import { JSDOM } from 'jsdom'
|
|
|
|
|
2021-07-15 09:38:06 +00:00
|
|
|
import { setupTest, testNitroBehavior } from './_tests.mjs'
|
2021-04-23 19:52:32 +00:00
|
|
|
|
|
|
|
// TODO: fix SyntaxError: Unexpected end of input on script executation
|
2021-07-15 09:38:06 +00:00
|
|
|
describe('nitro:preset:cloudflare', () => {
|
|
|
|
const ctx = setupTest('cloudflare')
|
2021-04-23 19:52:32 +00:00
|
|
|
testNitroBehavior(ctx, async () => {
|
2021-07-15 09:38:06 +00:00
|
|
|
const script = await fsp.readFile(resolve(ctx.outDir, 'server/index.mjs'), 'utf-8')
|
2021-04-23 19:52:32 +00:00
|
|
|
const dom = new JSDOM(
|
|
|
|
`<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
global = window
|
|
|
|
window.Response = class Response {
|
|
|
|
constructor (body, { headers, status, statusText } = {}) {
|
|
|
|
this.body = body
|
|
|
|
this.status = status || 200
|
|
|
|
this.headers = headers || {}
|
|
|
|
this.statusText = statusText || ''
|
|
|
|
}
|
|
|
|
get ok() {
|
|
|
|
return this.status === 200
|
|
|
|
}
|
|
|
|
async text() {
|
|
|
|
return this.body
|
|
|
|
}
|
|
|
|
async json() {
|
|
|
|
return JSON.parse(this.body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener = (method, handler) => {
|
|
|
|
window.handleEvent = async event => {
|
|
|
|
event.respondWith = response => {
|
|
|
|
event.response = response
|
|
|
|
}
|
|
|
|
await handler(event)
|
|
|
|
return event.response
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
2021-09-22 20:31:58 +00:00
|
|
|
<script>window._load = function() { ${script} }</script>
|
2021-04-23 19:52:32 +00:00
|
|
|
</body>
|
|
|
|
</html>`,
|
|
|
|
{ runScripts: 'dangerously' }
|
|
|
|
)
|
|
|
|
|
2021-09-22 20:31:58 +00:00
|
|
|
// https://github.com/jsdom/jsdom/issues/2524
|
|
|
|
// https://github.com/inrupt/solid-client-authn-js/issues/1676#issuecomment-917016646
|
|
|
|
dom.window.TextEncoder = TextEncoder
|
|
|
|
dom.window.TextDecoder = TextDecoder
|
|
|
|
dom.window._load()
|
|
|
|
|
2021-04-23 19:52:32 +00:00
|
|
|
return async ({ url, headers, method, body }) => {
|
|
|
|
const data = await dom.window.handleEvent({
|
|
|
|
request: {
|
|
|
|
url: 'http://localhost' + url,
|
|
|
|
headers: headers || {},
|
|
|
|
method: method || 'GET',
|
|
|
|
redirect: null,
|
|
|
|
body: body || null
|
|
|
|
}
|
|
|
|
}).then(r => r.text())
|
|
|
|
|
|
|
|
return { data }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|