-
Notifications
You must be signed in to change notification settings - Fork 392
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(custom-setup): introduce custom npm registries #542
Changes from all commits
3902029
f27c6fe
8653e30
aa13980
fe9f286
0b38918
2f425c7
7266df7
7ceee2f
e4953de
e71d06a
9746b6f
9cba9a7
fb915e8
0fb0125
58b3a2a
bab656e
8e2718b
0049b2b
11cc6ac
ebfa6f3
ea06516
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--install.ignore-engines true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
storage |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--install.ignore-engines true |
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 | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M= | ||
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"); | ||
}); | ||
})(); |
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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,13 @@ title: FAQ | |
|
||
Sandpack is an open ecosystem of components and utilities that allow you to compile and run modern frameworks in the browser. You can either use one of our predefined `components` for embedding the *CodeSandbox* experience into your projects, or you can build your own version of `sandpack`, on top of our standard components and utilities. | ||
|
||
#### How to load local or private dependencies? | ||
#### How to load private dependencies? | ||
|
||
Currently, Sandpack doesn’t have a way to consume private dependencies from any kind of registry service, because the bundler host is shared with all Sandpack consumers apps. However, you can pass local dependencies just like a regular file or using the external resource API: | ||
Read the following [guide](/guides/private-packages). | ||
|
||
#### How to load local dependencies? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ESModules might also be a solution for local dependencies, as we support importing files as a full url. although it never really took of in codesandbox itself, don't even think we ever even announced that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. humm good to know. I think I'm going to work on local dependencies in another PR, where we could explore how Verdaccio can also help in this regard. |
||
|
||
Currently, Sandpack doesn’t have a way to consume local dependencies, because the bundler host is shared with all Sandpack consumers apps. However, you can pass local dependencies just like a regular file or using the external resource API: | ||
|
||
```jsx | ||
<Sandpack | ||
|
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/> | ||
|
||
 | ||
|
||
<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. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment mentioning this is only a test user with password
test