npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloudflare2express

v1.0.8

Published

an adapter for running a worker in a cloudflare pages function and for running either in express

Downloads

19

Readme

cloudflare2express

an adapter for running a worker or cloudflare pages function in express

The original reason for this was to help @namdevel debug my @rhildred/cors-proxy2. By the time I was done I had a test that worked like this with isomorphic-git, the original target for my cors proxy.

import { describe, it, expect } from 'vitest';
import createApp from '../src/ExpressApp.js';
import git from 'isomorphic-git';
import fs from 'fs';
import http from 'isomorphic-git/http/web';

describe("cloudflare cors-proxy for isomorphic git", ()=>{
    it("handles an isomorphic git clone", async ()=>{
        const app = createApp();
        const server = await app.listen(8080);
        await git.clone({
            corsProxy: 'http://127.0.0.1:8080/corsproxy',
            url: 'https://github.com/diy-pwa/cloudflare2express',
            ref: 'main',
            singleBranch: true,
            depth: 10,
            dir: 'test2',
            fs: fs,
            http
        });  
        await server.close();
        expect(true).toBe(true);
    });
});

I was so excited that I decided to share this so that you can run a cloudflare pages function or worker in the vscode debugger or with supertest tests yourself. There seem to be a lot more workers than pages functions so I also included an example of running a worker as a pages function.

To reproduce this test in your own environment:

nvm use 18
npm install --save-dev vitest express isomorphic-git cloudflare2express @rhildred/cors-proxy2

Edit the package.json file to include:

{
  "type":"module",
  "scripts": {
    "test": "rm -rf test2 && vitest run __tests__"
  }
}

You will need to make a test fixture like below (src/ExpressApp.js)

import express from 'express';
import { onRequest as corsproxy } from '../functions/corsproxy/[[corsproxy]].js';
import {functionsAdapter} from 'cloudflare2express';

export default () => {
    const app = express();
    app.all(/\/corsproxy.*/, express.raw({
        inflate: true,
        limit: '50mb',
        type: () => true, // this matches all content types for this route
    }), async (req, res) => {
        functionsAdapter(corsproxy, req, res, {url: req.url.replace(/^.*corsproxy/, "https:/")});
    });
    return app;
}

to adapt the cors-proxy2 worker to a function functions/corsproxy/[[corsproxy]].js:

import {CorsProxyResponse} from "@rhildred/cors-proxy2";

export async function onRequest(context) {
    return await CorsProxyResponse.fetch(context.request, context.env);
}

For completeness, I made an index.js and start script:

import createApp from './src/ExpressApp.js';
const app = createApp();
const server = await app.listen(8080, ()=>console.log(`listening on port ${server.address().port}`));

When we were testing we came across a problem with the inbuilt fetch of node 18. Even though just proxying, node would try to uncompress the response and fail. There is a solution to that problem, using node-fetch, in https://github.com/rhildred/cors-proxy2.

Unfortunately I didn't get multipart form data working with the express adapter. The anticipated use for the express adapter is for running the debugger and supertest so I satisfied myself with this.

I hope that you also discover the joy of being able to write tests with your cloudflare workers and pages functions and debug them in vscode on your local machine.