-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathserve.ts
More file actions
executable file
·93 lines (76 loc) · 2.24 KB
/
serve.ts
File metadata and controls
executable file
·93 lines (76 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
/* eslint-disable no-console */
/**
* This script is used to start everything needed to run the service worker
* gateway like a complete IPFS gateway.
*
* It will start a kubo node, an ipfs gateway, and a reverse proxy for the
* front-end assets.
*
* This file expects that `build:tsc` was ran first, and this will be handled
* for you if ran via `npm run start`.
*/
import { parseArgs } from 'node:util'
import { execa } from 'execa'
import { startServers } from './test-e2e/fixtures/serve/index.ts'
import type { ResultPromise } from 'execa'
import type { Server } from 'node:http'
function toBoolean (val?: string | boolean, def: boolean = false): boolean {
if (val == null) {
return def
}
if (val === true || val === false) {
return val
}
return val === 'true'
}
const args = parseArgs({
args: process.argv,
strict: false,
options: {
'load-fixtures': {
type: 'boolean',
default: false
},
'start-frontend': {
type: 'boolean',
default: true
},
watch: {
type: 'boolean',
default: false
}
}
})
const servers = await startServers({
loadFixtures: toBoolean(args.values['load-fixtures'], false),
startFrontend: toBoolean(args.values['start-frontend'], true)
})
const info = await servers.kubo.info()
console.info('Kubo gateway:', info.gateway)
console.info('Kubo RPC API:', info.api)
if (servers.serviceWorker != null) {
console.info('👉️ service-worker-gateway:', `http://localhost:${getPort(servers.serviceWorker)}`)
}
let build: ResultPromise | undefined
if (toBoolean(args.values.watch, false)) {
// rebuild the app on changes
build = execa('node', ['build.js', '--watch'])
build?.stdout?.pipe(process.stdout)
build?.stderr?.pipe(process.stderr)
}
const cleanup = async (): Promise<void> => {
build?.kill()
await servers.stop?.()
}
// when the process exits, stop the reverse proxy
build?.on('exit', () => { void cleanup() })
process.on('SIGINT', () => { void cleanup() })
process.on('SIGTERM', () => { void cleanup() })
function getPort (server?: Server): number {
const address = server?.address()
if (address == null || typeof address === 'string') {
throw new Error('Server was not listening')
}
return address.port
}