@data-fair/dev-server 1.13.1 → 2.0.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 (43) hide show
  1. package/package.json +47 -62
  2. package/src/app.ts +278 -0
  3. package/src/config.ts +12 -0
  4. package/{server/index.js → src/index.ts} +6 -5
  5. package/src/ui-config.ts +6 -0
  6. package/ui/dist/assets/index-B_eKgwUO.css +5 -0
  7. package/ui/dist/assets/index-C7IbVpbV.js +167 -0
  8. package/ui/dist/fonts/XRXV3I6Li01BKofIMeaBXso-C3IBG1kp.woff2 +0 -0
  9. package/ui/dist/fonts/XRXV3I6Li01BKofINeaB-BaTF6Vo7.woff2 +0 -0
  10. package/ui/dist/fonts/XRXV3I6Li01BKofIO-aBXso-DcJfvmGA.woff2 +0 -0
  11. package/ui/dist/fonts/XRXV3I6Li01BKofIOOaBXso-BWI5zH9R.woff2 +0 -0
  12. package/ui/dist/fonts/XRXV3I6Li01BKofIOuaBXso-B55YuedR.woff2 +0 -0
  13. package/ui/dist/fonts/XRXX3I6Li01BKofIMNaDRs4-BbMn9XSX.woff2 +0 -0
  14. package/ui/dist/fonts/XRXX3I6Li01BKofIMNaHRs71cA-Cznx39fA.woff2 +0 -0
  15. package/ui/dist/fonts/XRXX3I6Li01BKofIMNaMRs71cA-CuWrHpFO.woff2 +0 -0
  16. package/ui/dist/fonts/XRXX3I6Li01BKofIMNaNRs71cA-D1eeM49Z.woff2 +0 -0
  17. package/ui/dist/fonts/XRXX3I6Li01BKofIMNaORs71cA-Bm_i0Dk1.woff2 +0 -0
  18. package/ui/dist/index.html +19 -0
  19. package/.eslintignore +0 -5
  20. package/.nvmrc +0 -1
  21. package/config/custom-environment-variables.js +0 -23
  22. package/config/default.js +0 -18
  23. package/config/development.js +0 -1
  24. package/config/production.js +0 -1
  25. package/dist/.nojekyll +0 -0
  26. package/dist/200.html +0 -9
  27. package/dist/_dev-server-nuxt/2c7d7c0.js +0 -1
  28. package/dist/_dev-server-nuxt/3091234.js +0 -1
  29. package/dist/_dev-server-nuxt/3f92c7d.js +0 -1
  30. package/dist/_dev-server-nuxt/439df67.js +0 -1
  31. package/dist/_dev-server-nuxt/5a68472.js +0 -1
  32. package/dist/_dev-server-nuxt/LICENSES +0 -52
  33. package/dist/_dev-server-nuxt/a62e828.js +0 -1
  34. package/dist/_dev-server-nuxt/b39b908.js +0 -1
  35. package/dist/_dev-server-nuxt/b64ddf5.js +0 -2
  36. package/dist/_dev-server-nuxt/c306f6e.js +0 -1
  37. package/dist/_dev-server-nuxt/c5e103d.js +0 -1
  38. package/dist/_dev-server-nuxt/dc90835.js +0 -2
  39. package/dist/_dev-server-nuxt/eb298ad.js +0 -8
  40. package/dist/index.html +0 -9
  41. package/nodemon.json +0 -5
  42. package/nuxt.config.js +0 -59
  43. package/server/app.js +0 -308
package/package.json CHANGED
@@ -1,19 +1,26 @@
1
1
  {
2
2
  "name": "@data-fair/dev-server",
3
- "version": "1.13.1",
3
+ "version": "2.0.0",
4
4
  "description": "A development server for optimal development experience of data-fair applications.",
5
- "main": "server/index.js",
5
+ "main": "src/index.ts",
6
6
  "bin": {
7
- "df-dev-server": "server/index.js"
7
+ "df-dev-server": "src/index.ts"
8
8
  },
9
+ "type": "module",
9
10
  "scripts": {
10
- "dev": "NODE_ENV=development nodemon server",
11
- "dev-src": "http-server test-apps/minimal --port=3000 --cors --silent",
12
- "dev-vue3": "APP_URL=http://localhost:3000/app/ APP_SCRIPT=dev-src-vue3 NODE_ENV=development nodemon server",
13
- "dev-src-vue3": "cd test-apps/vue3 && npm run dev",
14
- "prepare": "npm run lint && nuxt generate",
15
- "lint": "eslint --ext .js,.vue ."
11
+ "dev": "NODE_ENV=development node --watch src/index.ts",
12
+ "dev-test-app-minimal": "http-server test-apps/minimal --port=3000 --cors --silent",
13
+ "dev-test-app-vue3": "cd test-apps/vue3 && npm run dev",
14
+ "dev-zellij": "zellij --layout .zellij.kdl",
15
+ "prepublishOnly": "npm run lint && npm run check-types && npm -w ui run build",
16
+ "lint": "eslint . && npm -w ui run lint",
17
+ "lint-fix": "eslint --fix . && npm -w ui run lint-fix",
18
+ "check-types": "tsc && npm -w ui run check-types",
19
+ "build-types": "df-build-types ."
16
20
  },
21
+ "workspaces": [
22
+ "ui"
23
+ ],
17
24
  "repository": {
18
25
  "type": "git",
19
26
  "url": "git+https://github.com/koumoul-dev/df-dev-server.git"
@@ -22,66 +29,44 @@
22
29
  "data-fair",
23
30
  "dev"
24
31
  ],
25
- "author": "Alban Mouton <alban.mouton@gmail.com>",
32
+ "author": "Alban Mouton <alban.mouton@koumoul.com>",
26
33
  "license": "MIT",
27
34
  "bugs": {
28
35
  "url": "https://github.com/koumoul-dev/df-dev-server/issues"
29
36
  },
30
37
  "homepage": "https://github.com/koumoul-dev/df-dev-server#readme",
31
- "eslintConfig": {
32
- "root": true,
33
- "env": {
34
- "node": true
35
- },
36
- "extends": [
37
- "plugin:vue/essential",
38
- "eslint:recommended",
39
- "standard",
40
- "plugin:vue/recommended"
41
- ],
42
- "rules": {
43
- "vue/no-v-html": "off",
44
- "vue/multi-word-component-names": "off"
45
- }
38
+ "devDependencies": {
39
+ "@data-fair/lib-types-builder": "^1.8.2",
40
+ "@types/config": "^3.3.5",
41
+ "@types/cors": "^2.8.19",
42
+ "@types/debug": "^4.1.12",
43
+ "@types/express": "^5.0.3",
44
+ "@types/node": "^24.0.7",
45
+ "@types/ws": "^8.18.1",
46
+ "eslint": "^9.30.0",
47
+ "eslint-plugin-vue": "^10.2.0",
48
+ "eslint-plugin-vuetify": "^2.5.2",
49
+ "http-server": "^14.1.1",
50
+ "neostandard": "^0.12.1",
51
+ "typescript": "^5.8.3"
46
52
  },
47
53
  "dependencies": {
48
- "@koumoul/nuxt-config-inject": "^0.4.4",
49
- "@koumoul/vjsf": "^2.18.0",
50
- "@koumoul/v-iframe": "^1.3.4",
51
- "ajv": "^8.8.2",
52
- "ajv-i18n": "^4.2.0",
53
- "body-parser": "^1.19.1",
54
- "chalk": "^3.0.0",
55
- "config": "^3.3.6",
54
+ "@data-fair/lib-express": "^1.20.4",
55
+ "@parse5/tools": "^0.6.0",
56
+ "chalk": "^5.4.1",
57
+ "config": "^4.0.0",
56
58
  "cors": "^2.8.5",
57
- "dot-prop": "^6.0.1",
58
- "dotenv": "^10.0.0",
59
- "escape-string-regexp": "^2.0.0",
60
- "event-to-promise": "^0.8.0",
61
- "express": "^4.17.2",
62
- "fs-extra": "^10.0.0",
63
- "html-extractor": "^0.2.2",
64
- "http-proxy-middleware": "^2.0.1",
65
- "iframe-resizer": "^4.3.2",
66
- "parse5": "^6.0.1",
67
- "tree-kill": "^1.2.2",
68
- "ws": "^8.3.0"
59
+ "debug": "^4.4.1",
60
+ "dotenv": "^17.0.0",
61
+ "express": "^5.1.0",
62
+ "http-proxy-middleware": "^3.0.5",
63
+ "parse5": "^7.3.0",
64
+ "prom-client": "^15.1.3",
65
+ "ws": "^8.18.3"
69
66
  },
70
- "devDependencies": {
71
- "@babel/eslint-parser": "^7.16.0",
72
- "@nuxtjs/axios": "^5.13.6",
73
- "@nuxtjs/i18n": "^7.2.0",
74
- "@nuxtjs/vuetify": "^1.12.3",
75
- "eslint": "^7.12.1",
76
- "eslint-config-standard": "^16.0.3",
77
- "eslint-plugin-import": "^2.25.2",
78
- "eslint-plugin-node": "^11.1.0",
79
- "eslint-plugin-promise": "^5.1.1",
80
- "eslint-plugin-vue": "^8.0.3",
81
- "http-server": "^0.12.3",
82
- "nodemon": "^2.0.15",
83
- "nuxt": "^2.15.8",
84
- "reconnecting-websocket": "^4.4.0",
85
- "vuetify": "^2.6.1"
86
- }
67
+ "files": [
68
+ "./config/dist/**/*",
69
+ "./ui/dist/**/*",
70
+ "./src/**/*"
71
+ ]
87
72
  }
package/src/app.ts ADDED
@@ -0,0 +1,278 @@
1
+ // Express app for TaxMan own API and UI
2
+
3
+ import config from './config.ts'
4
+ import uiConfig from './ui-config.ts'
5
+ import { WebSocket, WebSocketServer } from 'ws'
6
+ import { createServer } from 'node:http'
7
+ import express from 'express'
8
+ import cors from 'cors'
9
+ import { existsSync, readFileSync, writeFileSync } from 'node:fs'
10
+ import debugModule from 'debug'
11
+ import { createProxyMiddleware, fixRequestBody } from 'http-proxy-middleware'
12
+ import * as parse5 from 'parse5'
13
+ import chalk from 'chalk'
14
+ import { isElementNode, createTextNode, createElement } from '@parse5/tools'
15
+ import escapeStringRegexp from 'escape-string-regexp'
16
+ import eventPromise from '@data-fair/lib-utils/event-promise.js'
17
+ import { createSpaMiddleware } from '@data-fair/lib-express/serve-spa.js'
18
+ import { resolve } from 'node:path'
19
+
20
+ const debug = debugModule('df-dev-server')
21
+
22
+ const app = express()
23
+ const server = createServer(app)
24
+
25
+ // web socket server, used both by dev-server UI (opened on / path)
26
+ // and by application (opened on /data-fair), in this case we transmit all messages to and from data-fair
27
+ const wss = new WebSocketServer({ noServer: true })
28
+ let devServerWS: WebSocket
29
+ let dataFairWS: WebSocket
30
+ server.on('upgrade', function upgrade (req, socket, head) {
31
+ wss.handleUpgrade(req, socket, head, (ws) => {
32
+ if (req.url === '/data-fair') {
33
+ if (dataFairWS) dataFairWS.terminate()
34
+ dataFairWS = ws
35
+ ws.on('message', (data: string) => {
36
+ if (dataFairOutputWS) {
37
+ const message = JSON.parse(data)
38
+ if (message.type === 'subscribe') message.apiKey = config.dataFair.apiKey
39
+ dataFairOutputWS.send(JSON.stringify(message))
40
+ }
41
+ })
42
+ } else {
43
+ if (devServerWS) devServerWS.terminate()
44
+ devServerWS = ws
45
+ }
46
+ wss.emit('connection', ws, req)
47
+ })
48
+ })
49
+
50
+ const dfWsUrl = config.dataFair.url.replace('http://', 'ws://').replace('https://', 'wss://') + '/'
51
+ const dataFairOutputWS = new WebSocket(dfWsUrl)
52
+ dataFairOutputWS.on('open', () => dataFairOutputWS.send('connect to external websocket server ' + dfWsUrl))
53
+ dataFairOutputWS.on('message', (data) => { if (dataFairWS) dataFairWS.send(data.toString()) })
54
+ dataFairOutputWS.on('error', (err) => console.error('failed to connect to external websocket server', dfWsUrl, err))
55
+
56
+ app.use(cors())
57
+ app.use(express.json())
58
+
59
+ // very basic CRUD of config
60
+ app.get('/config', (req, res, next) => {
61
+ const devConfig = existsSync('.dev-config.json') ? JSON.parse(readFileSync('.dev-config.json', 'utf8')) : {}
62
+ debug('read dev config', devConfig)
63
+ res.send(devConfig)
64
+ })
65
+ app.put('/config', (req, res, next) => {
66
+ debug('save dev config', req.body)
67
+ writeFileSync('.dev-config.json', JSON.stringify(req.body, null, 2))
68
+ res.send(req.body)
69
+ })
70
+ app.post('/config/error', (req, res) => {
71
+ console.log('Application sent an error', req.body)
72
+ if (devServerWS) devServerWS.send(JSON.stringify({ type: 'app-error', data: req.body }))
73
+ else console.error('no dev-server websocket open to send error to')
74
+ res.send()
75
+ })
76
+
77
+ // re-expose the application performing similar modifications to the body as data-fair
78
+ const appUrl = new URL(config.app.url)
79
+ const appPrefix = appUrl.pathname === '/' ? '' : appUrl.pathname
80
+ app.use('/app', createProxyMiddleware({
81
+ target: appUrl.origin,
82
+ secure: false,
83
+ changeOrigin: true,
84
+ selfHandleResponse: true, // so that the onProxyRes takes care of sending the response
85
+ on: {
86
+ proxyReq (proxyReq, req, res) {
87
+ proxyReq.path = appPrefix + proxyReq.path
88
+ proxyReq.setHeader('Accept-Encoding', 'identity') // disable compression
89
+ },
90
+ proxyRes (proxyRes, req, res) {
91
+ const configuration = existsSync('.dev-config.json') ? JSON.parse(readFileSync('.dev-config.json', 'utf8')) : {}
92
+ // console.log('inject config', configuration)
93
+ const dataBuffers: Buffer[] = []
94
+ proxyRes.on('data', (data) => { dataBuffers.push(data) })
95
+ proxyRes.on('end', async () => {
96
+ try {
97
+ let output = Buffer.concat(dataBuffers).toString()
98
+ if (output.includes('%APPLICATION%')) {
99
+ const filledBody = output.replace(/%APPLICATION%/g, JSON.stringify({
100
+ id: 'dev-application',
101
+ title: 'Dev application',
102
+ configuration,
103
+ exposedUrl: `http://localhost:${config.port}/app`,
104
+ href: `http://localhost:${config.port}/config`,
105
+ apiUrl: `http://localhost:${config.port}/data-fair/api/v1`,
106
+ wsUrl: `ws://localhost:${config.port}/data-fair`,
107
+ owner: config.dataFair && config.dataFair.owner
108
+ }))
109
+ const document = parse5.parse(filledBody)
110
+ const html = document.childNodes.filter(isElementNode).find(c => c.tagName === 'html')
111
+ if (!html) throw new Error('HTML structure is broken, expect html, head and body elements')
112
+ const headNode = html.childNodes.filter(isElementNode).find(c => c.tagName === 'head')
113
+ const bodyNode = html.childNodes.filter(isElementNode).find(c => c.tagName === 'body')
114
+ if (!headNode || !bodyNode) throw new Error('HTML structure is broken, expect html, head and body elements')
115
+
116
+ // add a script to simulate instrumentation by capture service
117
+ // @ts-ignore
118
+ const query: Record<string, string> = req.query
119
+ if (query.thumbnail === 'true') {
120
+ const script = createElement('script', { type: 'text/javascript' })
121
+ script.childNodes.push(createTextNode(`
122
+ console.log('[capture] Simulate a screenshot capture context')
123
+ var triggerCalled = false
124
+ window.triggerCapture = function (animationSupported) {
125
+ triggerCalled = true
126
+ console.log('[capture] triggerCapture called')
127
+ if (animationSupported) {
128
+ console.log('[capture] this application supports animated screenshots')
129
+ var i = 0
130
+ const interval = setInterval(function () {
131
+ i++
132
+ if (i === 1800) {
133
+ console.error('[capture] stop after the maximum number of frames was attained')
134
+ clearInterval(interval)
135
+ }
136
+ var stopped = window.animateCaptureFrame()
137
+ if (stopped) {
138
+ console.log('[capture] animation was stopped after ' + i + ' frames')
139
+ clearInterval(interval)
140
+ }
141
+ }, 67)
142
+ return true
143
+ } else {
144
+ console.log('[capture] this application does not support animated screenshots')
145
+ }
146
+ }
147
+ setTimeout(function() {
148
+ if (!triggerCalled) {
149
+ console.error('[capture] triggerCapture was not called after a 5s wait')
150
+ }
151
+ }, 5000)
152
+ `))
153
+ headNode.childNodes.push(script)
154
+ }
155
+
156
+ const meta: Record<string, string> = {}
157
+ for (const node of headNode.childNodes.filter(isElementNode)) {
158
+ if (node.tagName === 'meta') {
159
+ const name = node.attrs.find(a => a.name === 'name')?.value
160
+ const content = node.attrs.find(a => a.name === 'content')?.value
161
+ if (name !== undefined && content !== undefined) meta[name] = content
162
+ }
163
+ }
164
+ if (meta['df:sync-state'] === 'true' || meta['df:overflow'] === 'true') {
165
+ bodyNode.childNodes.push(createElement('script', {
166
+ type: 'text/javascript',
167
+ src: 'https://cdn.jsdelivr.net/npm/@data-fair/frame@0.12/dist/v-iframe-compat/d-frame-content.min.js'
168
+ }))
169
+ }
170
+
171
+ output = parse5.serialize(document)
172
+
173
+ // proxyRes.headers['content-length'] = output.length
174
+ delete proxyRes.headers['content-length']
175
+ delete proxyRes.headers['last-modified']
176
+ delete proxyRes.headers['max-age']
177
+ delete proxyRes.headers.etag
178
+ proxyRes.headers['cache-control'] = 'no-cache'
179
+ }
180
+ res.writeHead(proxyRes.statusCode ?? 200, proxyRes.headers)
181
+ res.end(output)
182
+ } catch (err: any) {
183
+ console.error(err)
184
+ res.writeHead(500)
185
+ res.end(err.message)
186
+ }
187
+ })
188
+ }
189
+ }
190
+ }))
191
+
192
+ for (const proxyPath of config.app.proxyPaths) {
193
+ app.use(proxyPath, createProxyMiddleware({
194
+ target: appUrl.origin,
195
+ secure: false,
196
+ changeOrigin: true,
197
+ ws: true
198
+ }))
199
+ }
200
+
201
+ // re-expose a data-fair instance to access datasets, etc.
202
+ const dfUrl = new URL(config.dataFair.url)
203
+ app.use('/data-fair', createProxyMiddleware({
204
+ target: dfUrl.origin,
205
+ secure: false,
206
+ changeOrigin: true,
207
+ selfHandleResponse: true, // so that the onProxyRes takes care of sending the response
208
+ on: {
209
+ proxyReq (proxyReq, req, res) {
210
+ proxyReq.path = '/data-fair' + proxyReq.path
211
+
212
+ // no gzip so that we can process the content
213
+ proxyReq.setHeader('accept-encoding', 'identity')
214
+
215
+ // authentication through api key only
216
+ proxyReq.setHeader('cookie', '')
217
+ if (config.dataFair.apiKey) proxyReq.setHeader('x-apiKey', config.dataFair.apiKey)
218
+
219
+ // body was already parsed by body-parser and no longer available as a stream
220
+ fixRequestBody(proxyReq, req)
221
+ },
222
+ proxyRes (proxyRes, req, res) {
223
+ // console.log('DF RES', proxyRes)
224
+ if (proxyRes.headers['content-type'] && proxyRes.headers['content-type'].startsWith('application/json')) {
225
+ let body = ''
226
+ proxyRes.on('data', (data) => { body += data.toString() })
227
+ proxyRes.on('end', () => {
228
+ const output = body.replace(new RegExp(escapeStringRegexp(config.dataFair.url), 'g'), 'http://localhost:5888/data-fair')
229
+ // proxyRes.headers['content-length'] = output.length
230
+ delete proxyRes.headers['content-length']
231
+ res.writeHead(proxyRes.statusCode ?? 200, proxyRes.headers)
232
+ // make all references to data-fair url point to local proxy
233
+ res.end(output)
234
+ })
235
+ } else {
236
+ res.writeHead(proxyRes.statusCode ?? 200, proxyRes.headers)
237
+ proxyRes.pipe(res)
238
+ }
239
+ }
240
+ }
241
+ }))
242
+
243
+ // also re-expose the simple-directory instance matching data-fair
244
+ app.use('/simple-directory', createProxyMiddleware({
245
+ target: dfUrl.origin,
246
+ secure: false,
247
+ changeOrigin: true,
248
+ on: {
249
+ proxyReq (proxyReq, req, res) {
250
+ proxyReq.path = '/simple-directory' + proxyReq.path
251
+ }
252
+ }
253
+ }))
254
+
255
+ if (config.serveUi) {
256
+ app.use(await createSpaMiddleware(resolve(import.meta.dirname, '../ui/dist'), uiConfig))
257
+ } else {
258
+ app.use('/', createProxyMiddleware({
259
+ target: 'http://localhost:6220',
260
+ secure: false
261
+ }))
262
+ }
263
+
264
+ // Run app and return it in a promise
265
+ export const run = async () => {
266
+ server.listen(config.port)
267
+ await eventPromise(server, 'listening')
268
+ console.log(chalk.bold.blue('\nDataFair dev server available on ') + chalk.underline.bold.blue(`http://localhost:${config.port}`))
269
+ return server
270
+ }
271
+
272
+ export const stop = async () => {
273
+ if (devServerWS) devServerWS.terminate()
274
+ if (dataFairWS) dataFairWS.terminate()
275
+ server.close()
276
+ await eventPromise(server, 'close')
277
+ await app.get('client').close()
278
+ }
package/src/config.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { assertValid, type DevServerConfig } from '../config/type/index.js'
2
+ import config from 'config'
3
+
4
+ export type { DevServerConfig } from '../config/type/index.ts'
5
+
6
+ // we reload the config instead of using the singleton from the config module for testing purposes
7
+ // @ts-ignore
8
+ const devServerConfig = process.env.NODE_ENV === 'test' ? config.util.loadFileConfigs(process.env.NODE_CONFIG_DIR, { skipConfigSources: true }) : config
9
+
10
+ assertValid(devServerConfig, { lang: 'en', name: 'config', internal: true })
11
+
12
+ export default devServerConfig as DevServerConfig
@@ -1,14 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const path = require('path')
4
- require('dotenv').config()
5
- process.env.NODE_CONFIG_DIR = path.join(__dirname, '../config')
6
- const app = require('./app')
3
+ import 'dotenv/config'
4
+ import path from 'node:path'
5
+
6
+ process.env.NODE_CONFIG_DIR = path.join(import.meta.dirname, '../config')
7
+ const app = await import('./app.ts')
7
8
 
8
9
  app.run().then(app => {
9
10
  // nothing to do
10
11
  }, error => {
11
- console.error('Failure in customers process', error)
12
+ console.error('Failure in dev server process', error)
12
13
  process.exit(-1)
13
14
  })
14
15
 
@@ -0,0 +1,6 @@
1
+ import config from './config.ts'
2
+
3
+ export const uiConfig = { dataFair: config.dataFair }
4
+
5
+ export type UiConfig = typeof uiConfig
6
+ export default uiConfig