esm.dev 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/.prettierignore +4 -0
  2. package/LICENSE +21 -0
  3. package/README.md +68 -0
  4. package/commitlint.config.cjs +8 -0
  5. package/dist/cli.js +17 -0
  6. package/dist/commands/ESMDevCommand.js +6 -0
  7. package/dist/commands/InitCommand.js +40 -0
  8. package/dist/commands/LoginCommand.js +12 -0
  9. package/dist/commands/MustacheGeneratorCommand.js +9 -0
  10. package/dist/commands/RepublishCommand.js +11 -0
  11. package/dist/commands/StartCommand.js +16 -0
  12. package/dist/commands/TokenCommand.js +20 -0
  13. package/dist/commands/WaitForRegistryCommand.js +12 -0
  14. package/dist/commands/WatchCommand.js +12 -0
  15. package/dist/commands/mixins/CommandClass.js +1 -0
  16. package/dist/commands/mixins/ESMServed.js +9 -0
  17. package/dist/commands/mixins/ESMStorageSpecific.js +9 -0
  18. package/dist/commands/mixins/PackagePathSpecific.js +14 -0
  19. package/dist/commands/mixins/RegistrySpecific.js +9 -0
  20. package/dist/commands/mixins/Retryable.js +28 -0
  21. package/dist/commands/mixins/Servable.js +11 -0
  22. package/dist/commands/mixins/Watchable.js +9 -0
  23. package/dist/lib/MustacheGenerator.js +9 -0
  24. package/dist/lib/getPackageMeta.js +10 -0
  25. package/dist/lib/login.js +31 -0
  26. package/dist/lib/publish.js +5 -0
  27. package/dist/lib/queue.js +33 -0
  28. package/dist/lib/republish.js +9 -0
  29. package/dist/lib/server.js +23 -0
  30. package/dist/lib/unpublish.js +25 -0
  31. package/dist/lib/until.js +16 -0
  32. package/dist/lib/watch.js +61 -0
  33. package/dist/options/EnvOption.js +6 -0
  34. package/package.json +66 -0
  35. package/release.config.cjs +20 -0
  36. package/src/cli.ts +18 -0
  37. package/src/commands/ESMDevCommand.ts +8 -0
  38. package/src/commands/InitCommand.ts +57 -0
  39. package/src/commands/LoginCommand.ts +15 -0
  40. package/src/commands/MustacheGeneratorCommand.ts +10 -0
  41. package/src/commands/RepublishCommand.ts +15 -0
  42. package/src/commands/StartCommand.ts +21 -0
  43. package/src/commands/TokenCommand.ts +26 -0
  44. package/src/commands/WaitForRegistryCommand.ts +17 -0
  45. package/src/commands/WatchCommand.ts +15 -0
  46. package/src/commands/mixins/CommandClass.ts +3 -0
  47. package/src/commands/mixins/ESMServed.ts +12 -0
  48. package/src/commands/mixins/ESMStorageSpecific.ts +16 -0
  49. package/src/commands/mixins/PackagePathSpecific.ts +22 -0
  50. package/src/commands/mixins/RegistrySpecific.ts +12 -0
  51. package/src/commands/mixins/Retryable.ts +35 -0
  52. package/src/commands/mixins/Servable.ts +14 -0
  53. package/src/commands/mixins/Watchable.ts +13 -0
  54. package/src/commands/templates/init/docker-compose.yml.mustache +38 -0
  55. package/src/lib/MustacheGenerator.ts +10 -0
  56. package/src/lib/getPackageMeta.ts +15 -0
  57. package/src/lib/login.ts +35 -0
  58. package/src/lib/publish.ts +14 -0
  59. package/src/lib/queue.ts +39 -0
  60. package/src/lib/republish.ts +16 -0
  61. package/src/lib/server.ts +29 -0
  62. package/src/lib/unpublish.ts +43 -0
  63. package/src/lib/until.ts +22 -0
  64. package/src/lib/watch.ts +84 -0
  65. package/src/options/EnvOption.ts +27 -0
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "esm.dev",
3
+ "version": "1.5.0",
4
+ "description": "TypeScript library template",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "exports": {
8
+ ".": "./src/cli.ts"
9
+ },
10
+ "bin": {
11
+ "esm.dev": "./dist/cli.js"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "esm.dev": "bun ./src/cli.ts",
16
+ "prepare": "husky",
17
+ "start": "docker compose up --detach --remove-orphans",
18
+ "stop": "docker compose down"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/johngeorgewright/esm.dev.git"
23
+ },
24
+ "keywords": [],
25
+ "author": "John Wright <johngeorge.wright@gmail.com>",
26
+ "license": "MIT",
27
+ "bugs": {
28
+ "url": "https://github.com/johngeorgewright/esm.dev/issues"
29
+ },
30
+ "homepage": "https://github.com/johngeorgewright/esm.dev#readme",
31
+ "devDependencies": {
32
+ "@commitlint/cli": "19.8.1",
33
+ "@commitlint/config-conventional": "19.8.1",
34
+ "@commitlint/types": "19.8.1",
35
+ "@types/bun": "latest",
36
+ "@types/http-proxy": "^1.17.16",
37
+ "@types/lodash.debounce": "^4.0.9",
38
+ "@types/mustache": "^4.2.6",
39
+ "@types/ramda": "^0.30.2",
40
+ "husky": "9.1.7",
41
+ "lint-staged": "16.0.0",
42
+ "prettier": "3.5.3",
43
+ "rimraf": "6.0.1",
44
+ "semantic-release": "^24.2.3",
45
+ "typescript": "5.8.3"
46
+ },
47
+ "lint-staged": {
48
+ "*.{md,json,js,jsx,ts,tsx,yml,yaml}": [
49
+ "bunx prettier --ignore-path .gitignore --write"
50
+ ]
51
+ },
52
+ "dependencies": {
53
+ "clipanion": "^4.0.0-rc.4",
54
+ "clipanion-generator-command": "^1.2.0",
55
+ "escape-string-regexp": "^5.0.0",
56
+ "glob": "^11.0.2",
57
+ "http-proxy": "^1.18.1",
58
+ "lodash.debounce": "^4.0.8",
59
+ "mustache": "^4.2.0",
60
+ "npm": "^11.3.0",
61
+ "ramda": "^0.30.1",
62
+ "throat": "^6.0.2",
63
+ "tslib": "^2.8.1",
64
+ "zod": "^3.24.4"
65
+ }
66
+ }
@@ -0,0 +1,20 @@
1
+ // @ts-check
2
+
3
+ /**
4
+ * @type {import('semantic-release').Options}
5
+ */
6
+ module.exports = {
7
+ branches: ['master'],
8
+ plugins: [
9
+ '@semantic-release/commit-analyzer',
10
+ '@semantic-release/release-notes-generator',
11
+ '@semantic-release/npm',
12
+ [
13
+ '@semantic-release/git',
14
+ {
15
+ message: 'chore(release): ${nextRelease.version} [skip ci]',
16
+ },
17
+ ],
18
+ '@semantic-release/github',
19
+ ],
20
+ }
package/src/cli.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { runExit } from 'clipanion'
2
+ import { WatchCommand } from './commands/WatchCommand.js'
3
+ import { RepublishCommand } from './commands/RepublishCommand.js'
4
+ import { LoginCommand } from './commands/LoginCommand.js'
5
+ import { TokenCommand } from './commands/TokenCommand.js'
6
+ import { WaitForRegistryCommand } from './commands/WaitForRegistryCommand.js'
7
+ import { StartCommand } from './commands/StartCommand.js'
8
+ import { InitCommand } from './commands/InitCommand.js'
9
+
10
+ runExit([
11
+ InitCommand,
12
+ LoginCommand,
13
+ TokenCommand,
14
+ RepublishCommand,
15
+ StartCommand,
16
+ WaitForRegistryCommand,
17
+ WatchCommand,
18
+ ])
@@ -0,0 +1,8 @@
1
+ import { Command } from 'clipanion'
2
+ import { RegistrySpecific } from './mixins/RegistrySpecific.js'
3
+ import { ESMStorageSpecific } from './mixins/ESMStorageSpecific.js'
4
+ import { PackagePathSpecific } from './mixins/PackagePathSpecific.js'
5
+
6
+ export abstract class ESMDevCommand extends ESMStorageSpecific(
7
+ RegistrySpecific(PackagePathSpecific(Command)),
8
+ ) {}
@@ -0,0 +1,57 @@
1
+ import * as path from 'node:path'
2
+ import { ESMDevCommand } from './ESMDevCommand'
3
+ import { PackagePathSpecific } from './mixins/PackagePathSpecific'
4
+ import { MustacheGeneratorCommand } from './MustacheGeneratorCommand'
5
+ import { Option } from 'clipanion'
6
+
7
+ export class InitCommand extends PackagePathSpecific(MustacheGeneratorCommand) {
8
+ static override paths = [['init']]
9
+
10
+ static override usage = ESMDevCommand.Usage({
11
+ description: 'Initialises your repo ready to work with ESM.sh locally',
12
+ })
13
+
14
+ readonly esmOrigin = Option.String(
15
+ '-e,--esm-origin',
16
+ 'http://localhost:8080',
17
+ {
18
+ description: 'The base URL of the ESM Server',
19
+ },
20
+ )
21
+
22
+ readonly esmStoragePath = Option.String(
23
+ '-s,--esm-storage-path',
24
+ './docker-storage/esm/esmd',
25
+ {
26
+ description: "Path to ESM.sh's storage",
27
+ },
28
+ )
29
+
30
+ readonly port = Option.String('-p,--port', '3000', {
31
+ description: "The server's port",
32
+ })
33
+
34
+ readonly outputDirectory = Option.String('-o,--output-dir', '.', {
35
+ description: 'The output directory',
36
+ })
37
+
38
+ readonly registry = Option.String('-r,--registry', 'http://localhost:4873', {
39
+ description: 'The URL of your local registry',
40
+ })
41
+
42
+ esmURL!: URL
43
+ npmRegistryURL!: URL
44
+ packages!: { path: string; basename: string }[]
45
+
46
+ override async execute() {
47
+ this.templateDir = path.join(import.meta.dirname, 'templates', 'init')
48
+ this.destinationDir = this.outputDirectory
49
+ this.esmURL = new URL(this.esmOrigin)
50
+ this.npmRegistryURL = new URL(this.registry)
51
+ this.packages = this.packagePaths.map((packagePath) => ({
52
+ path: packagePath,
53
+ basename: path.basename(packagePath),
54
+ }))
55
+ await super.execute()
56
+ }
57
+ }
@@ -0,0 +1,15 @@
1
+ import { Command } from 'clipanion'
2
+ import { RegistrySpecific } from './mixins/RegistrySpecific.js'
3
+
4
+ export class LoginCommand extends RegistrySpecific(Command) {
5
+ static override paths = [['login']]
6
+
7
+ static override usage = Command.Usage({
8
+ description: 'Login in to the NPM registry',
9
+ })
10
+
11
+ override async execute() {
12
+ const { login } = await import('../lib/login.js')
13
+ return login(this.registry)
14
+ }
15
+ }
@@ -0,0 +1,10 @@
1
+ import { GeneratorCommand } from 'clipanion-generator-command'
2
+ import { MustacheGenerator } from '../lib/MustacheGenerator'
3
+
4
+ export abstract class MustacheGeneratorCommand extends GeneratorCommand {
5
+ override get generator() {
6
+ return new MustacheGenerator(this.templateDir, this.destinationDir, [
7
+ '.mustache',
8
+ ])
9
+ }
10
+ }
@@ -0,0 +1,15 @@
1
+ import { ESMDevCommand } from './ESMDevCommand.js'
2
+
3
+ export class RepublishCommand extends ESMDevCommand {
4
+ static override paths = [['republish']]
5
+
6
+ static override usage = ESMDevCommand.Usage({
7
+ description:
8
+ 'Removes the references, of given packages, from the ESM server and registry and republishes.',
9
+ })
10
+
11
+ override async execute() {
12
+ const { republish } = await import('../lib/republish.js')
13
+ await this.eachPackagePath((packagePath) => republish(packagePath, this))
14
+ }
15
+ }
@@ -0,0 +1,21 @@
1
+ import { ESMDevCommand } from './ESMDevCommand.js'
2
+ import { Servable } from './mixins/Servable.js'
3
+ import { Watchable } from './mixins/Watchable.js'
4
+ import { ESMServed } from './mixins/ESMServed.js'
5
+
6
+ export class StartCommand extends Servable(
7
+ Watchable(ESMServed(ESMDevCommand)),
8
+ ) {
9
+ static override paths = [['start']]
10
+
11
+ static override usage = ESMDevCommand.Usage({
12
+ description: 'Runs a server and concurrently watches a file system',
13
+ })
14
+
15
+ override async execute() {
16
+ const { watch } = await import('../lib/watch.js')
17
+ const { serve } = await import('../lib/server.js')
18
+ serve(this.port, this.esmOrigin)
19
+ await this.eachPackagePath((packagePath) => watch(packagePath, this))
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ import { Command } from 'clipanion'
2
+ import { RegistrySpecific } from './mixins/RegistrySpecific.js'
3
+
4
+ export class TokenCommand extends RegistrySpecific(Command) {
5
+ static override paths = [['token']]
6
+
7
+ static override usage = Command.Usage({
8
+ description: 'Get the NPM token',
9
+ })
10
+
11
+ override async execute() {
12
+ const { default: escapeStringRegexp } = await import('escape-string-regexp')
13
+ const npmrc = await Bun.file(`~/.npmrc`).text()
14
+ const url = new URL(this.registry)
15
+ const regex = new RegExp(
16
+ `^//${escapeStringRegexp(url.hostname)}/:_authToken=(.*)$`,
17
+ 'm',
18
+ )
19
+ const match = regex.exec(npmrc)
20
+ if (!match) {
21
+ this.context.stderr.write(`Token has not been created yet\n`)
22
+ return 1
23
+ }
24
+ this.context.stdout.write(match[1] + '\n')
25
+ }
26
+ }
@@ -0,0 +1,17 @@
1
+ import { Command } from 'clipanion'
2
+ import { RegistrySpecific } from './mixins/RegistrySpecific.js'
3
+ import { Retryable } from './mixins/Retryable.js'
4
+
5
+ export class WaitForRegistryCommand extends Retryable(
6
+ RegistrySpecific(Command),
7
+ ) {
8
+ static override paths = [['wait-for-registry'], ['wfr']]
9
+
10
+ static override usage = Command.Usage({
11
+ description: 'wait for the registry to be online',
12
+ })
13
+
14
+ override async execute() {
15
+ return this.retry(this.registry)
16
+ }
17
+ }
@@ -0,0 +1,15 @@
1
+ import { ESMDevCommand } from './ESMDevCommand.js'
2
+ import { Watchable } from './mixins/Watchable.js'
3
+
4
+ export class WatchCommand extends Watchable(ESMDevCommand) {
5
+ static override paths = [['watch']]
6
+
7
+ static override usage = ESMDevCommand.Usage({
8
+ description: 'Watches directories and republishes on changes',
9
+ })
10
+
11
+ override async execute() {
12
+ const { watch } = await import('../lib/watch.js')
13
+ await this.eachPackagePath((packagePath) => watch(packagePath, this))
14
+ }
15
+ }
@@ -0,0 +1,3 @@
1
+ import type { Command } from 'clipanion'
2
+
3
+ export type CommandClass = abstract new (...args: any[]) => Command
@@ -0,0 +1,12 @@
1
+ import { EnvOption } from '../../options/EnvOption'
2
+ import type { CommandClass } from './CommandClass'
3
+
4
+ export function ESMServed<T extends CommandClass>(Base: T) {
5
+ abstract class ESMServed extends Base {
6
+ readonly esmOrigin = EnvOption('ESM_ORIGIN', '-e,--esm-origin', {
7
+ description: 'The base URL of the ESM Server',
8
+ })
9
+ }
10
+
11
+ return ESMServed
12
+ }
@@ -0,0 +1,16 @@
1
+ import type { CommandClass } from './CommandClass'
2
+ import { EnvOption } from '../../options/EnvOption.js'
3
+
4
+ export function ESMStorageSpecific<T extends CommandClass>(Base: T) {
5
+ abstract class ESMStorageSpecific extends Base {
6
+ readonly esmStoragePath = EnvOption(
7
+ 'ESM_STORAGE_PATH',
8
+ '-s,--esm-storage-path',
9
+ {
10
+ description: "Path to ESM.sh's storage",
11
+ },
12
+ )
13
+ }
14
+
15
+ return ESMStorageSpecific
16
+ }
@@ -0,0 +1,22 @@
1
+ import { Option } from 'clipanion'
2
+ import type { CommandClass } from './CommandClass.js'
3
+
4
+ export function PackagePathSpecific<T extends CommandClass>(Base: T) {
5
+ abstract class PackagePathSpecific extends Base {
6
+ readonly packagePaths = Option.Rest({
7
+ name: 'packages',
8
+ })
9
+
10
+ protected async eachPackagePath(cb: (packagePath: string) => Promise<any>) {
11
+ const { glob } = await import('glob')
12
+ const packagePaths = (
13
+ await Promise.all(
14
+ this.packagePaths.map((packagePath) => glob(packagePath)),
15
+ )
16
+ ).flat()
17
+ await Promise.all(packagePaths.map(cb))
18
+ }
19
+ }
20
+
21
+ return PackagePathSpecific
22
+ }
@@ -0,0 +1,12 @@
1
+ import type { CommandClass } from './CommandClass.js'
2
+ import { EnvOption } from '../../options/EnvOption.js'
3
+
4
+ export function RegistrySpecific<TBase extends CommandClass>(Base: TBase) {
5
+ abstract class RegistrySpecific extends Base {
6
+ readonly registry = EnvOption('NPM_REGISTRY', '-r,--registry', {
7
+ description: 'The URL of your local registry',
8
+ })
9
+ }
10
+
11
+ return RegistrySpecific
12
+ }
@@ -0,0 +1,35 @@
1
+ import { Option } from 'clipanion'
2
+ import { isNumber } from 'typanion'
3
+ import type { CommandClass } from './CommandClass.js'
4
+
5
+ export function Retryable<T extends CommandClass>(Base: T) {
6
+ abstract class Retryable extends Base {
7
+ readonly timeout = Option.String('-t,--timeout', '10000', {
8
+ description: 'The amount of total ms to keep trying for',
9
+ validator: isNumber(),
10
+ })
11
+
12
+ readonly interval = Option.String('-i,--interval', '300', {
13
+ description: 'The amount of ms inbetween each attempt',
14
+ validator: isNumber(),
15
+ })
16
+
17
+ protected async retry(endpoint: string) {
18
+ const { until } = await import('../../lib/until.js')
19
+ if (
20
+ !(await until({
21
+ ...this,
22
+ try: async (signal) => {
23
+ const response = await fetch(endpoint, { signal })
24
+ return response.ok
25
+ },
26
+ }))
27
+ ) {
28
+ this.context.stderr.write(`${endpoint} is not available\n`)
29
+ return 1
30
+ }
31
+ }
32
+ }
33
+
34
+ return Retryable
35
+ }
@@ -0,0 +1,14 @@
1
+ import { isNumber } from 'typanion'
2
+ import { EnvOption } from '../../options/EnvOption.js'
3
+ import type { CommandClass } from './CommandClass.js'
4
+
5
+ export function Servable<T extends CommandClass>(Base: T) {
6
+ abstract class Servable extends Base {
7
+ readonly port = EnvOption('PORT', '-p,--port', {
8
+ description: 'The port to run the server on',
9
+ validator: isNumber(),
10
+ })
11
+ }
12
+
13
+ return Servable
14
+ }
@@ -0,0 +1,13 @@
1
+ import { Option } from 'clipanion'
2
+ import type { CommandClass } from './CommandClass'
3
+
4
+ export function Watchable<T extends CommandClass>(Base: T) {
5
+ abstract class Watchable extends Base {
6
+ readonly legacyMethod = Option.Boolean('-l,--legacy-method', false, {
7
+ description:
8
+ 'Use a less performant, legacy, watch method. Sometimes needed in docker or vagrant environments.',
9
+ })
10
+ }
11
+
12
+ return Watchable
13
+ }
@@ -0,0 +1,38 @@
1
+ services:
2
+ esm.dev:
3
+ image: ghcr.io/johngeorgewright/esm.dev:latest
4
+ depends_on:
5
+ - esm
6
+ - npm
7
+ environment:
8
+ - NPM_REGISTRY=http://npm:{{npmRegistryURL.port}}
9
+ - ESM_ORIGIN=http://esm:{{esmURL.port}}
10
+ - ESM_STORAGE_PATH=/esmd
11
+ - PORT={{port}}
12
+ command:
13
+ - start
14
+ - /watch/*
15
+ ports:
16
+ - '{{port}}:{{port}}'
17
+ volumes:
18
+ - {{esmStoragePath}}:/esmd
19
+ # The following are paths to all the packages you wish to auto publish
20
+ {{#packages}}
21
+ - {{path}}:/watch/{{basename}}:ro
22
+ {{/packages}}
23
+
24
+ esm:
25
+ image: ghcr.io/esm-dev/esm:latest
26
+ environment:
27
+ - NPM_REGISTRY=http://npm:{{npmRegistryURL.port}}
28
+ - NPM_TOKEN=fake
29
+ - LOG_LEVEL=debug
30
+ ports:
31
+ - '{{esmURL.port}}:8080'
32
+ volumes:
33
+ - {{esmStoragePath}}:/esmd
34
+
35
+ npm:
36
+ image: verdaccio/verdaccio:latest
37
+ ports:
38
+ - '{{npmRegistryURL.port}}:4873'
@@ -0,0 +1,10 @@
1
+ import Mustache from 'mustache'
2
+ import { Generator } from 'clipanion-generator-command/Generator'
3
+
4
+ export class MustacheGenerator extends Generator {
5
+ override renderTemplate(templateContext: any, template: string) {
6
+ return Mustache.render(template, templateContext, undefined, {
7
+ escape: (x) => x,
8
+ })
9
+ }
10
+ }
@@ -0,0 +1,15 @@
1
+ import { readFile, stat as getStat } from 'node:fs/promises'
2
+ import * as path from 'node:path'
3
+
4
+ export async function getPackageMeta(
5
+ packagePath: string,
6
+ ): Promise<{ name: string; packageRoot: string; private?: boolean }> {
7
+ const stat = await getStat(packagePath)
8
+ const packageRoot = stat.isDirectory()
9
+ ? packagePath
10
+ : path.dirname(packagePath)
11
+ const pckg = JSON.parse(
12
+ await readFile(path.join(packageRoot, 'package.json'), 'utf-8'),
13
+ )
14
+ return { name: pckg.name, packageRoot, private: pckg.private }
15
+ }
@@ -0,0 +1,35 @@
1
+ import { spawn } from 'node:child_process'
2
+
3
+ export function login(registry: string) {
4
+ return new Promise<number>((resolve) => {
5
+ const child = spawn('bunx', [
6
+ 'npm',
7
+ 'login',
8
+ '--registry',
9
+ registry,
10
+ '--quiet',
11
+ ])
12
+
13
+ child.stderr.on('data', (d) => console.error(d.toString()))
14
+
15
+ child.stdout.on('data', (d) => {
16
+ const data = d.toString()
17
+ switch (true) {
18
+ case /username/i.test(data):
19
+ child.stdin.write('esm.dev\n')
20
+ break
21
+ case /password/i.test(data):
22
+ child.stdin.write('esm.dev\n')
23
+ break
24
+ case /email/i.test(data):
25
+ child.stdin.write('esm.dev@esm.dev.com')
26
+ break
27
+ case /logged in as/i.test(data):
28
+ child.stdin.end()
29
+ break
30
+ }
31
+ })
32
+
33
+ child.on('exit', resolve)
34
+ })
35
+ }
@@ -0,0 +1,14 @@
1
+ import { $ } from 'bun'
2
+ import * as path from 'node:path'
3
+
4
+ export async function publish({
5
+ packageRoot,
6
+ registry,
7
+ }: {
8
+ packageRoot: string
9
+ registry: string
10
+ }) {
11
+ await $`bunx npm publish --registry ${registry}`.cwd(
12
+ path.resolve(packageRoot),
13
+ )
14
+ }
@@ -0,0 +1,39 @@
1
+ import debounce from 'lodash.debounce'
2
+ import throat from 'throat'
3
+
4
+ export const queue = throat(1)
5
+
6
+ /**
7
+ * Just like debounce, but the first call will add a promise to the queue
8
+ * and that promise won't resolve until the debounced function is finally called.
9
+ */
10
+ export function queuedDebounce<Args extends unknown[], R>(
11
+ fn: (...args: Args) => R,
12
+ delay: number,
13
+ ): (...args: Args) => Promise<R> {
14
+ let promiseWithResolvers: PromiseWithResolvers<R> | undefined
15
+
16
+ const debounced = debounce(async (...args: Args) => {
17
+ try {
18
+ const result = await fn(...args)
19
+ promiseWithResolvers?.resolve(result)
20
+ } catch (error: any) {
21
+ promiseWithResolvers?.reject(error)
22
+ } finally {
23
+ promiseWithResolvers = undefined
24
+ }
25
+ }, delay)
26
+
27
+ return (...args: Args) => {
28
+ start()
29
+ debounced(...args)
30
+ return promiseWithResolvers!.promise
31
+ }
32
+
33
+ function start() {
34
+ if (!promiseWithResolvers) {
35
+ promiseWithResolvers = Promise.withResolvers<R>()
36
+ queue(async () => promiseWithResolvers?.promise)
37
+ }
38
+ }
39
+ }
@@ -0,0 +1,16 @@
1
+ import { getPackageMeta } from './getPackageMeta.js'
2
+ import { publish } from './publish.js'
3
+ import { unpublish } from './unpublish.js'
4
+
5
+ export async function republish(
6
+ packagePath: string,
7
+ opts: {
8
+ registry: string
9
+ esmStoragePath: string
10
+ },
11
+ ) {
12
+ console.info(`Republishing ${packagePath}`)
13
+ const { name, packageRoot } = await getPackageMeta(packagePath)
14
+ await unpublish({ ...opts, name })
15
+ await publish({ ...opts, packageRoot })
16
+ }
@@ -0,0 +1,29 @@
1
+ import { queue } from './queue.js'
2
+ import { createServer } from 'node:http'
3
+ import httpProxy from 'http-proxy'
4
+
5
+ export function serve(port: number, esmOrigin: string) {
6
+ const proxy = httpProxy.createProxyServer()
7
+
8
+ const server = createServer((req, res) => {
9
+ queue(
10
+ () =>
11
+ new Promise<void>((resolve, reject) => {
12
+ console.info('Proxying', req.url)
13
+ res.on('error', reject)
14
+ res.on('close', resolve)
15
+ res.on('finish', resolve)
16
+ proxy.web(req, res, { target: esmOrigin })
17
+ }),
18
+ ).catch((error) => {
19
+ res.statusCode = 500
20
+ res.setHeader('Content-Type', 'application/json; charset=utf-8')
21
+ res.write(JSON.stringify(error))
22
+ res.end()
23
+ })
24
+ }).listen(port, () => {
25
+ console.info('ESM proxy server listining on', server.address())
26
+ })
27
+
28
+ return server
29
+ }