Skip to content

Commit 39f8b2e

Browse files
authored
feat: add base send-file transform (#9)
* add base send-file transform * update test files for send-file --------- Co-authored-by: Filip Kudła <[email protected]>
1 parent c2a8346 commit 39f8b2e

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { testSpecBuilder } from './util'
2+
3+
testSpecBuilder('send-file')
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import express from "express";
2+
import path from "path";
3+
import sendfile from "other-place"
4+
5+
const app = express();
6+
const options = {
7+
root: path.join(__dirname, 'public'),
8+
dotfiles: 'deny',
9+
headers: {
10+
'x-timestamp': Date.now(),
11+
'x-sent': true
12+
}
13+
}
14+
15+
const sharedSendFile = (res, next, fileName) => {
16+
res.sendfile(fileName, options, (err) => {
17+
if (err) {
18+
next(err)
19+
} else {
20+
console.log('Sent:', fileName)
21+
}
22+
})
23+
}
24+
25+
app.get('/file/:name', (req, res, next) => {
26+
const fileName = req.params.name
27+
28+
res.sendfile(fileName, options, (err) => {
29+
if (err) {
30+
next(err)
31+
} else {
32+
console.log('Sent:', fileName)
33+
}
34+
})
35+
sharedSendFile(res, next, fileName);
36+
})
37+
38+
app.get('/filename/:name', function (req, res, next) {
39+
const fileName = req.params.name
40+
41+
res.sendfile(fileName, options, (err) => {
42+
if (err) {
43+
next(err)
44+
} else {
45+
console.log('Sent:', fileName)
46+
}
47+
})
48+
sharedSendFile(res, next, fileName);
49+
})
50+
51+
app.get('/file-handler', (req, res, next) => {
52+
sendfile('test', options, (err) => {
53+
if (err) {
54+
next(err)
55+
} else {
56+
console.log('Sent:', 'test')
57+
}
58+
})
59+
})
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import express from "express";
2+
import path from "path";
3+
import sendfile from "other-place"
4+
5+
const app = express();
6+
const options = {
7+
root: path.join(__dirname, 'public'),
8+
dotfiles: 'deny',
9+
headers: {
10+
'x-timestamp': Date.now(),
11+
'x-sent': true
12+
}
13+
}
14+
15+
const sharedSendFile = (res, next, fileName) => {
16+
res.sendFile(fileName, options, (err) => {
17+
if (err) {
18+
next(err)
19+
} else {
20+
console.log('Sent:', fileName)
21+
}
22+
})
23+
}
24+
25+
app.get('/file/:name', (req, res, next) => {
26+
const fileName = req.params.name
27+
28+
res.sendFile(fileName, options, (err) => {
29+
if (err) {
30+
next(err)
31+
} else {
32+
console.log('Sent:', fileName)
33+
}
34+
})
35+
sharedSendFile(res, next, fileName);
36+
})
37+
38+
app.get('/filename/:name', function (req, res, next) {
39+
const fileName = req.params.name
40+
41+
res.sendFile(fileName, options, (err) => {
42+
if (err) {
43+
next(err)
44+
} else {
45+
console.log('Sent:', fileName)
46+
}
47+
})
48+
sharedSendFile(res, next, fileName);
49+
})
50+
51+
app.get('/file-handler', (req, res, next) => {
52+
sendfile('test', options, (err) => {
53+
if (err) {
54+
next(err)
55+
} else {
56+
console.log('Sent:', 'test')
57+
}
58+
})
59+
})

transforms/send-file.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { API, FileInfo } from 'jscodeshift'
2+
import { CallExpression, withParser } from 'jscodeshift'
3+
4+
export default function transformer(file: FileInfo, _api: API): string {
5+
const parser = withParser('ts')
6+
7+
return parser(file.source)
8+
.find(CallExpression, {
9+
callee: {
10+
property: {
11+
name: 'sendfile',
12+
},
13+
},
14+
})
15+
.map((path) => {
16+
if (path.node.callee.type === 'MemberExpression' && path.node.callee.property.type === 'Identifier') {
17+
path.node.callee.property.name = 'sendFile'
18+
}
19+
20+
return path
21+
})
22+
.toSource()
23+
}

0 commit comments

Comments
 (0)