-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom-setup): introduce custom npm registries (#542)
Co-authored-by: Roman Kuba <[email protected]>
- Loading branch information
Showing
21 changed files
with
1,392 additions
and
49 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 @@ | ||
--install.ignore-engines true |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
storage |
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 @@ | ||
--install.ignore-engines true |
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,26 @@ | ||
# Sandpack custom npm registry (proxy) | ||
|
||
This project works as a recipe to proxy NPM registries. The main goal is to enable the consumption of private packages and expose them through a private registry without any authentication token (GitHub or Npm) or requiring an authentication process. | ||
|
||
This new private registry can be used on a Sandpack instance and run private packages public. | ||
|
||
Disclaimer: it's essential to keep the information and tokens of the npm registry private! By using this method, it's best to keep in mind that it could expose all private packages in your account. Be careful where and how this proxy will be used. Make sure to use authentication tokens with **read-only access**. | ||
|
||
It's also possible to expose only specific packages. If the custom scopes are `@scope/package-name` instead of `@scope/*`, it will only expose that particular package. You can even do something like `@scope/design-system*` to expose all packages of the design system. | ||
|
||
## How it works | ||
This project relies on [Verdaccio](https://verdaccio.org/), an open-source project that creates a private registry and can proxy other registries, such as GitHub and Npm. | ||
|
||
## How to use | ||
|
||
1. Host this project somewhere, and make sure it has permission to create new folders and files - Verdaccio needs to create temp storage to perform some optimizations; | ||
2. Configure your project correctly, for example, if you want to proxy NPM, GitHub, or both. You can find instructions in `/index.js`; | ||
3. Set the environments variables according to the type of registry you want to use; | ||
|
||
|
||
## Environment variables | ||
|
||
| Name | Description | | ||
| - | - | | ||
| `VERDACCIO_PUBLIC_URL` | is intended to be used behind proxies, and replace the final URL (optional) | | ||
| `GH_PKG_TOKEN` | GitHub personal token with `read:packages` permission | |
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 @@ | ||
test:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M= |
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,50 @@ | ||
const { runServer } = require("@verdaccio/node-api"); | ||
|
||
/** | ||
* Custom configuration | ||
*/ | ||
const customScopes = [`@codesandbox/*`]; | ||
const registries = { | ||
npmjs: { | ||
url: "https://registry.npmjs.org/", | ||
cache: false, | ||
}, | ||
github: { | ||
url: "https://npm.pkg.github.com/", | ||
cache: false, | ||
auth: { | ||
type: "bearer", | ||
token: process.env.GH_PKG_TOKEN, | ||
}, | ||
}, | ||
}; | ||
|
||
/** | ||
* Default configuration | ||
*/ | ||
const defaultPermission = { | ||
access: "$all", | ||
publish: "$authenticated", | ||
unpublish: "$authenticated", | ||
}; | ||
const defaultConfiguration = { | ||
config_path: "./config.yaml", | ||
storage: "./storage", | ||
uplinks: registries, | ||
packages: { | ||
...customScopes.reduce((acc, scope) => { | ||
acc[scope] = { ...defaultPermission, proxy: "github npmjs" }; | ||
return acc; | ||
}, {}), | ||
"@*/*": { ...defaultPermission, proxy: "npmjs" }, | ||
"**": { ...defaultPermission, proxy: "npmjs" }, | ||
}, | ||
server: { keepAliveTimeout: 60 }, | ||
}; | ||
|
||
(async () => { | ||
const app = await runServer(defaultConfiguration); | ||
app.listen(4000, () => { | ||
console.log("server started"); | ||
}); | ||
})(); |
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,23 @@ | ||
{ | ||
"name": "custom-npm-registry", | ||
"version": "1.0.0", | ||
"description": "", | ||
"dependencies": { | ||
"@verdaccio/node-api": "^6.0.0-6-next.32", | ||
"@verdaccio/ui-theme": "^3.4.1" | ||
}, | ||
"license": "Apache-2.0", | ||
"author": "CodeSandbox", | ||
"devDependencies": {}, | ||
"scripts": { | ||
"start": "node index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/danilowoz/sandpack-proxy.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/danilowoz/sandpack-proxy/issues" | ||
}, | ||
"homepage": "https://github.com/danilowoz/sandpack-proxy#readme" | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
sidebar_position: 2 | ||
title: Private packages | ||
--- | ||
|
||
<big>The custom private NPM registry allows Sandpack instances to retrieve private NPM packages from your own registry. This option requires running a third service (Node.js server) and configuring your Sandpack provider to consume these dependencies from another registry, not the public ones.</big> | ||
|
||
<br/> | ||
<br/> | ||
|
||
![Private packages](/img/private-package.png) | ||
|
||
<br/> | ||
|
||
**You'll need:** | ||
- Host a Node.js server, which will run registry proxy; | ||
- GitHub/NPM authentication token with read access; | ||
|
||
## Self-host the proxy | ||
|
||
We recommend hosting a service that allows you to proxy your private packages from a registry (GitHub/Npm/your own) to a new one, which would make the packages available through another URL. | ||
As Sandpack bundles everything in-browser, it needs to find a way to connect to the registry which provides the project dependencies. | ||
First, Sandpack will try to fetch all dependencies from public registries, for example, `react` or `redux`. Then you can let Sandpack know which dependencies (or scoped dependencies) should be fetched from a different registry. For example, your custom registry. | ||
|
||
### Our recommendation | ||
Suppose you don't already have a public registry, we recommend using [Verdaccio](https://verdaccio.org/). An open-source project that creates a private registry and can proxy other registries, such as GitHub and Npm. | ||
You can find examples of how to use the [examples folder](https://github.com/codesandbox/sandpack/tree/main/examples) in the main repository. | ||
|
||
## Sandpack configuration | ||
|
||
Once the proxy is running and configured, you need to set some options in your Sandpack context: | ||
|
||
```jsx | ||
<Sandpack | ||
customSetup={{ | ||
dependencies: { "@codesandbox/test-package": "1.0.5" }, | ||
npmRegistries: [ | ||
{ | ||
enabledScopes: ["@codesandbox"], | ||
limitToScopes: true, | ||
registryUrl: "PROXY_URL", | ||
}, | ||
], | ||
}} | ||
files={{ | ||
"/App.js": `import { Button } from "@codesandbox/test-package" | ||
export default function App() { | ||
return ( | ||
<div> | ||
<Button>I'm a private Package</Button> | ||
</div> | ||
) | ||
} | ||
`, | ||
}} | ||
template="react" | ||
/> | ||
``` | ||
|
||
## Security | ||
It's essential to keep the information and tokens of the npm registry private! By using this method, it's best to keep in mind that it could expose all private packages in your account. Be careful where and how this proxy will be used. Make sure to use authentication tokens with **read-only access**. | ||
|
||
It's also possible to expose only specific packages. If the custom scopes are `@scope/package-name` instead of `@scope/*`, it will only expose that particular package. You can even do something like `@scope/design-system*` to expose all packages of the design system. | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.