/
cli.js
215 lines (193 loc) 路 5.67 KB
/
cli.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env node
import net from 'net'
import fs from 'fs'
import path from 'path'
import { execa } from 'execa'
import { fileURLToPath } from 'url'
import sade from 'sade'
import { build } from 'esbuild'
import git from 'git-rev-sync'
import Sentry from '@sentry/cli'
import pWaitFor from 'p-wait-for'
import { create } from 'ipfs-http-client'
import globSource from 'ipfs-utils/src/files/glob-source.js'
import { denylistSyncCmd, denylistAddCmd } from './denylist.js'
import { heartbeatCmd } from './heartbeat.js'
const IPFS_API_URL = 'http://127.0.0.1:9089'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const pkg = JSON.parse(
fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf8')
)
const prog = sade('gateway')
prog
.command('build')
.describe('Build the worker.')
.option('--env', 'Environment', process.env.ENV)
.action(buildCmd)
.command('ipfs')
.describe('Run ipfs node')
.option('--start', 'Start docker container', false)
.option('--stop', 'Stop and clean all dockers artifacts', false)
.action(ipfsCmd)
.command('heartbeat', 'Ping opsgenie heartbeat')
.option('--token', 'Opsgenie Token')
.option('--name', 'Heartbeat Name')
.action(heartbeatCmd)
.command('denylist sync')
.describe('Sync the gateway deny list with various sources.')
.option('--env', 'Wrangler environment to use.', 'dev')
.action(denylistSyncCmd)
.command('denylist add <cid>')
.describe(
'Add a CID (or CID + path) to the local deny list. Note: we currently DO NOT support denying by CID + path in the API.'
)
.option('--status', 'HTTP status to send in response.')
.option('--reason', 'Reason for deny. Note: may be communicated in response')
.action(denylistAddCmd)
async function buildCmd(opts) {
const sentryRelease = `nft-gateway@${pkg.version}-${opts.env}+${git.short(
__dirname
)}`
console.log(`Building ${sentryRelease}`)
await build({
entryPoints: [path.join(__dirname, '..', 'src', 'index.js')],
bundle: true,
format: 'esm',
outfile: path.join(__dirname, '..', 'dist', 'worker.mjs'),
legalComments: 'external',
define: {
SENTRY_RELEASE: JSON.stringify(sentryRelease),
VERSION: JSON.stringify(pkg.version),
COMMITHASH: JSON.stringify(git.long(__dirname)),
BRANCH: JSON.stringify(git.branch(__dirname)),
global: 'globalThis',
},
minify: opts.env === 'dev' ? false : true,
sourcemap: 'external',
})
// Sentry release and sourcemap upload
if (process.env.SENTRY_UPLOAD === 'true') {
const cli = new Sentry(undefined, {
authToken: process.env.SENTRY_TOKEN,
org: 'protocol-labs-it',
project: 'nft-gateway',
dist: git.short(__dirname),
})
await cli.releases.new(sentryRelease)
await cli.releases.setCommits(sentryRelease, {
auto: true,
ignoreEmpty: true,
ignoreMissing: true,
})
await cli.releases.uploadSourceMaps(sentryRelease, {
include: [path.join(__dirname, '..', 'dist')],
ext: ['map', 'mjs'],
})
await cli.releases.finalize(sentryRelease)
await cli.releases.newDeploy(sentryRelease, {
env: opts.env,
})
}
}
async function ipfsCmd({ start, stop }) {
const project = 'ipfs-daemon'
const composePath = path.join(__dirname, '../docker/docker-compose.yml')
if (start) {
if (await isPortReachable(8080)) {
throw new Error(
'IPFS daemon is already running. Please check if you have any docker project or cluster deamon already running.'
)
}
await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'up',
'--detach',
])
await pWaitFor(async () => {
const { stdout } = await execa('docker', ['logs', '-t', 'ipfs0'])
return stdout.includes('Daemon is ready')
})
console.log('docker started')
// Add fixture files
const client = create({ url: IPFS_API_URL })
await Promise.all([
// bafkreidyeivj7adnnac6ljvzj2e3rd5xdw3revw4da7mx2ckrstapoupoq
client.add('Hello nft.storage! 馃槑', {
rawLeaves: true,
}),
// bafkreibxkbyybantsznyvlq2bhf24u4gew7pj6erjgduqp4mvqv54qjng4
client.add('Hello nft.storage! 馃槑馃槑', {
rawLeaves: true,
}),
// bafkreihbjbbccwxn7hzv5hun5pxuswide7q3lhjvfbvmd7r3kf2sodybgi
client.add('Hello nft.storage! 馃槑馃槑馃槑', {
rawLeaves: true,
}),
])
// bafybeih74zqc6kamjpruyra4e4pblnwdpickrvk4hvturisbtveghflovq
for await (const _ of client.addAll(
globSource(path.join(__dirname, '../test/fixtures/directory'), '**/*'),
{
rawLeaves: true,
wrapWithDirectory: true,
cidVersion: 1,
}
)) {
}
}
if (stop) {
await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'stop',
])
await execa('docker-compose', [
'--file',
composePath,
'--project-name',
project,
'down',
'--volumes',
'--rmi',
'local',
'--remove-orphans',
])
}
}
/**
* @param {number} port
*/
export default async function isPortReachable(
port,
{ host = 'localhost', timeout = 1000 } = {}
) {
if (typeof host !== 'string') {
throw new TypeError('Specify a `host`')
}
const promise = new Promise((resolve, reject) => {
const socket = new net.Socket()
const onError = (err) => {
socket.destroy()
reject(err)
}
socket.setTimeout(timeout)
socket.once('error', onError)
socket.once('timeout', onError)
socket.connect(port, host, () => {
socket.end()
resolve(undefined)
})
})
try {
await promise
return true
} catch {
return false
}
}
prog.parse(process.argv)