0

Problem: multiple api runtime in single npm module

I developed a very simple no CORS proxy for Azure and Cloudflare serverless environments.
obviously some code is shared between the two environments and obviously some code is different !

Thinking DRY I decided to create a mono repo with a maximum of sharing.

My first idea was to create a single npm module but I did not found an elegant solution for having the same module with two different runtime api.
In an ideal world index.ts should look like:

//Ignore import while in Cloudflare
import { proxyAzureRequest as _proxyAzureRequest } from "./Azure/index.js";
//Ignore import while in Azure
import { proxyPagesRequest as _proxyPagesRequest } from "./Pages/index.js";

export const proxyAzureRequest = _proxyAzureRequest;
export const proxyPagesRequest = _proxyPagesRequest;

In the real world it throws many issues because for example Cloudflare runtime does not support node streams.

Actual solution

Today I produce two modules one with:

import { proxyPagesRequest as _proxyPagesRequest } from "./Pages/index.js";
export const proxyPagesRequest = _proxyPagesRequest;

the other with:

import { proxyAzureRequest as _proxyAzureRequest } from "./Azure/index.js";
export const proxyAzureRequest = _proxyAzureRequest;

It uses a ugly script and two workflows in Github
which produces two modules:
https://www.npmjs.com/package/@sctg/nocors-azure
https://www.npmjs.com/package/@sctg/nocors-pages
But I do not like this/my solution

What I tried.

I tried to dynamically import with a try/catch closure but it fails. .
I also tried to detect presence of a Cloudflare specific variable (Websocket) and a if/else block but it also fails.
Is there an elegant solution to share a single module for multiple runtimes ?

Albert Tinon
  • 136
  • 5
  • This might be related https://stackoverflow.com/questions/36367532/how-can-i-conditionally-import-an-es6-module – pe.kne Nov 15 '22 at 11:12
  • @pe.kne Thank you I already saw this question and tried it (without babel since I'm on ES2019 module) but it fails because even in the *if block* . Cloudflare runtime looks for all dependencies and crash due the missing ones. – Albert Tinon Nov 15 '22 at 11:16
  • Side note: You don't need `import` and then `export`, you can combine them: `export { proxyAzureRequest } from "./Azure/index.js";` – T.J. Crowder Nov 15 '22 at 11:16
  • What about your solution do you not like? That you end up exporting two modules? To me, it makes sense to have separate modules for things that don't work in the same environment... – T.J. Crowder Nov 15 '22 at 11:17
  • @T.J.Crowder yes having two modules because I have a project that can be deployed in the two environments . Importing the two modules imports two times the same code… but for today it works… For me it is important to keep the two entries proxyAzureRequest and proxyPagesRequest but not two modules. – Albert Tinon Nov 15 '22 at 11:20
  • @AlbertTinon - But if you only import from one or the other based on the environment, you *aren't* importing the same code twice...? – T.J. Crowder Nov 15 '22 at 11:22
  • [`import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) may help. – T.J. Crowder Nov 15 '22 at 11:22

0 Answers0