~ekez/moonphase.is

131d5858cf7de595bcc1683630b8e3b0e4583f91 — ekez 2 months ago 6470f3d main
support CORS requests.
2 files changed, 58 insertions(+), 11 deletions(-)

M index.js
M wrangler.toml
M index.js => index.js +57 -10
@@ 31,13 31,25 @@ const phases = ['new moon', 'waxing crescent', 'first quarter',
		'waxing gibbous', 'full moon', 'waning gibbous',
		'last quarter', 'waning crescent']

// ref: <https://developers.cloudflare.com/workers/examples/cors-header-proxy>
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})
/**
 * Respond with hello worker text
 * @param {Request} request
 */
    const request = event.request;
    const url = new URL(request.url);

    if (request.method === 'OPTIONS') {
	event.respondWith(handleOptions(request));
    } else if (request.method === 'GET' || request.method === 'HEAD' || request.method === 'POST') {
	event.respondWith(handleRequest(request));
    } else {
	event.respondWith(
            new Response(null, {
		status: 405,
		statusText: 'Method Not Allowed',
            })
	);
    }
});

async function handleRequest(request) {
    const theDate = new Date()



@@ 48,17 60,27 @@ async function handleRequest(request) {

    const url = new URL(request.url)
    const path = url.pathname
    console.log(path)

    const cors = {
	'Access-Control-Allow-Origin': '*',
	'Vary': 'Origin',
    }

    switch (path) {
    case '/':
	return new Response(calculatePhase(year, month, day), {
	    headers: { 'content-type': 'text/plain' },
	    headers: {
		'content-type': 'text/plain',
		...cors
	    },
	})
    case '/image.svg':
	const phase = calculatePhase(year, month, day)
	return new Response(images[phase], {
	    headers: { 'content-type': 'image/svg+xml'},
	    headers: {
		'content-type': 'image/svg+xml',
		...cors
	    },
	})
    default:
	return new Response('404', { status: 404 })


@@ 66,7 88,32 @@ async function handleRequest(request) {
    }
}

// From: https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf
// ref: <https://developers.cloudflare.com/workers/examples/cors-header-proxy>
function handleOptions(request) {
  let headers = request.headers;
  if (
    headers.get('Origin') !== null &&
    headers.get('Access-Control-Request-Method') !== null &&
    headers.get('Access-Control-Request-Headers') !== null
  ) {
    let respHeaders = {
      ...corsHeaders,
      'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers'),
    };

    return new Response(null, {
      headers: respHeaders,
    });
  } else {
    return new Response(null, {
      headers: {
        Allow: 'GET, HEAD, POST, OPTIONS',
      },
    });
  }
}

// ref: <https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf>
function calculatePhase(Y, M, D) {
    const A = Math.floor(Y / 100)
    const B = Math.floor(A / 4)

M wrangler.toml => wrangler.toml +1 -1
@@ 1,8 1,8 @@
name = "moonphase"
type = "javascript"

account_id = ""
workers_dev = true
route = ""
zone_id = ""
compatibility_date = "2021-10-13"
main = "index.js"