/
sesTransforms.js
47 lines (43 loc) · 1.14 KB
/
sesTransforms.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
const path = require('node:path')
const { PassThrough } = require('readable-stream')
const duplexify = require('duplexify')
const concatStream = require('concat-stream')
const { applySourceTransforms } = require('lavamoat-core')
module.exports = {
createSesWorkaroundsTransform,
}
function createSesWorkaroundsTransform() {
return makeStringTransform(
'ses-workarounds',
{ excludeExtension: ['.json'] },
(content) => {
return applySourceTransforms(content)
}
)
}
function makeStringTransform(name, options, transformHandler) {
return function (file) {
if (options.excludeExtension) {
if (options.excludeExtension.includes(path.extname(file))) {
return new PassThrough()
}
}
return makeTransformStream(transformHandler)
}
}
function makeTransformStream(transformHandler) {
const outStream = new PassThrough()
return duplexify(
concatStream((buffer) => {
let transformed
try {
transformed = transformHandler(buffer.toString('utf8'))
} catch (err) {
outStream.destroy(err)
return
}
outStream.end(transformed)
}),
outStream
)
}