-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathbuild-and-test-openfeature.js
More file actions
94 lines (81 loc) · 3.1 KB
/
Copy pathbuild-and-test-openfeature.js
File metadata and controls
94 lines (81 loc) · 3.1 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
#!/usr/bin/env node
'use strict'
/* eslint-disable no-console */
// Regression test for #8980 under esbuild. When the optional peer
// `@datadog/openfeature-node-server` is installed, the dd-trace esbuild plugin bundles it
// into the output so feature flagging keeps working after the bundle is relocated to a tree
// without the peer on disk (standalone deploys). Without bundling, the opaque runtime require
// resolves from the bundle directory and falls back to the no-op provider.
//
// The complementary #8635 case (peer absent -> build must not follow the optional chain) is
// covered by `openfeature.spec.js`, whose sandbox does not install the peer.
const fs = require('fs')
const os = require('os')
const path = require('path')
const assert = require('assert')
const { execFileSync } = require('child_process')
const esbuild = require('esbuild')
const ddPlugin = require('../../esbuild') // dd-trace/esbuild
const OUTFILE = path.join(__dirname, 'openfeature-out.js')
async function main () {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dd-openfeature-esbuild-'))
try {
assert.strictEqual(
isResolvable('@datadog/openfeature-node-server', __dirname),
true,
'the optional peer must be installed for this scenario; run `yarn install` with devDependencies'
)
await esbuild.build({
entryPoints: [path.join(__dirname, 'openfeature-app.js')],
outfile: OUTFILE,
bundle: true,
platform: 'node',
target: 'node18',
plugins: [ddPlugin],
external: [
'pg', 'mysql2', 'better-sqlite3', 'sqlite3', 'mysql', 'mariadb', 'oracledb', 'pg-query-stream', 'tedious',
'@yaacovcr/transform',
'@datadog/native-appsec', '@datadog/native-iast-taint-tracking', '@datadog/native-metrics',
'@datadog/pprof', '@datadog/libdatadog',
],
})
const bundle = fs.readFileSync(OUTFILE).toString()
assert(
!bundle.includes("requireOptionalPeer('@datadog/openfeature-node-server')"),
'the opaque peer require survived; the plugin did not inline `@datadog/openfeature-node-server`'
)
const relocated = path.join(tmpDir, 'out.js')
fs.copyFileSync(OUTFILE, relocated)
assert.strictEqual(
isResolvable('@datadog/openfeature-node-server', tmpDir),
false,
'the relocation dir must not resolve the peer, otherwise the test proves nothing'
)
const runOutput = execFileSync(process.execPath, [relocated], { encoding: 'utf8' })
assert(
runOutput.includes('PROVIDER_OK'),
`relocated bundle did not load the real OpenFeature provider:\n${runOutput}`
)
console.log('ok')
} finally {
fs.rmSync(OUTFILE, { force: true })
fs.rmSync(tmpDir, { recursive: true, force: true })
}
}
/**
* @param {string} request - Module specifier
* @param {string} fromDir - Directory to resolve from
* @returns {boolean} Whether the module resolves from `fromDir`
*/
function isResolvable (request, fromDir) {
try {
require.resolve(request, { paths: [fromDir] })
return true
} catch {
return false
}
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})