Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·118 lines (105 loc) · 3.01 KB
/
Copy pathcli.js
File metadata and controls
executable file
·118 lines (105 loc) · 3.01 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
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
#!/usr/bin/env node
import {parseArgs} from 'node:util'
import {PREFIX as NATS_CLIENT_NAME_PREFIX} from './lib/nats.js'
// todo: use import assertions once they're supported by Node.js & ESLint
// https://github.com/tc39/proposal-import-assertions
import {createRequire} from 'node:module'
const require = createRequire(import.meta.url)
const pkg = require('./package.json')
const {
values: flags,
} = parseArgs({
options: {
'help': {
type: 'boolean',
short: 'h',
},
'version': {
type: 'boolean',
short: 'v',
},
'port': {
type: 'string',
short: 'p',
},
'nats-servers': {
type: 'string',
},
'nats-user': {
type: 'string',
},
'nats-client-name': {
type: 'string',
},
'nats-consumer-name': {
type: 'string',
},
't0': {
type: 'string',
},
},
allowPositionals: true,
})
if (flags.help) {
process.stdout.write(`
Usage:
serve-gtfs-rt-from-nats [options]
Options:
--port -p Port to serve the GTFS Realtime feed on.
Default: $PORT, otherwise 3000
--nats-servers NATS server(s) to connect to.
Default: $NATS_SERVERS
--nats-user User to use when authenticating with NATS server.
Default: $NATS_USER
--nats-client-name Name identifying the NATS client among others.
Default: ${NATS_CLIENT_NAME_PREFIX}\${randomHex(4)}
--nats-consumer-name Name of the NATS JetStream consumer on the
GTFS_RT_2 stream.
Default: $GTFS_RT_CONSUMER_NAME, otherwise \`nats-
consuming-gtfs-rt-server\`
--diff-entities-ttl Time to keep DIFFERENTIAL-mode GTFS-RT FeedEntities
in the combined FULL_DATASET-mode feed for, in seconds.
Default: 10 minutes
--t0 UNIX timestamp to use as now, for debugging purposes.
Default: current UNIX timestamp
Examples:
serve-gtfs-rt-from-nats --port 1234 --nats-user foo
\n`)
process.exit(0)
}
if (flags.version) {
process.stdout.write(`${pkg.name} v${pkg.version}\n`)
process.exit(0)
}
import {serveGtfsRtDataFromNats} from './index.js'
import {withSoftExit} from './lib/soft-exit.js'
const cfg = {}
const opt = {
natsOpts: {},
}
if ('port' in flags) {
cfg.port = parseInt(flags.port)
} else if ('PORT' in process.env) {
cfg.port = parseInt(process.env.PORT)
} else {
cfg.port = 3000
}
if ('nats-servers' in flags) {
opt.natsOpts.servers = flags['nats-servers'].split(',')
}
if ('nats-user' in flags) {
opt.natsOpts.user = flags['nats-user']
}
if ('nats-client-name' in flags) {
opt.natsOpts.name = flags['nats-client-name']
}
if ('t0' in flags) {
opt.t0 = parseInt(flags.t0)
}
if ('diff-entities-ttl' in flags) {
opt.differentialEntitiesTtl = parseInt(flags['diff-entities-ttl'])
}
const {
stop,
} = await serveGtfsRtDataFromNats(cfg, opt)
withSoftExit(stop)