This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparser.js
137 lines (107 loc) · 3.21 KB
/
parser.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const Content = require('@hapi/content')
const multipart = require('it-multipart')
const multipartFormdataType = 'multipart/form-data'
const applicationDirectory = 'application/x-directory'
const applicationSymlink = 'application/symlink'
const isDirectory = (mediatype) => mediatype === multipartFormdataType || mediatype === applicationDirectory
const parseDisposition = (disposition) => {
const details = {}
details.type = disposition.split(';')[0]
if (details.type === 'file' || details.type === 'form-data') {
const filenamePattern = / filename="(.[^"]+)"/
const filenameMatches = disposition.match(filenamePattern)
details.filename = filenameMatches ? filenameMatches[1] : ''
const namePattern = / name="(.[^"]+)"/
const nameMatches = disposition.match(namePattern)
details.name = nameMatches ? nameMatches[1] : ''
}
return details
}
const collect = async (stream) => {
const buffers = []
let size = 0
for await (const buf of stream) {
size += buf.length
buffers.push(buf)
}
return Buffer.concat(buffers, size)
}
const ignore = async (stream) => {
for await (const _ of stream) { // eslint-disable-line no-unused-vars
}
}
async function * parseEntry (stream, options) {
for await (const part of stream) {
if (!part.headers['content-type']) {
throw new Error('No content-type in multipart part')
}
const type = Content.type(part.headers['content-type'])
if (type.boundary) {
// recursively parse nested multiparts
yield * parser(part.body, {
...options,
boundary: type.boundary
})
continue
}
if (!part.headers['content-disposition']) {
throw new Error('No content disposition in multipart part')
}
const entry = {}
if (part.headers.mtime) {
entry.mtime = {
secs: parseInt(part.headers.mtime, 10)
}
if (part.headers['mtime-nsecs']) {
entry.mtime.nsecs = parseInt(part.headers['mtime-nsecs'], 10)
}
}
if (part.headers.mode) {
entry.mode = parseInt(part.headers.mode, 8)
}
if (isDirectory(type.mime)) {
entry.type = 'directory'
} else if (type.mime === applicationSymlink) {
entry.type = 'symlink'
} else {
entry.type = 'file'
}
const disposition = parseDisposition(part.headers['content-disposition'])
entry.name = decodeURIComponent(disposition.filename)
entry.body = part.body
yield entry
}
}
async function * parser (stream, options) {
for await (const entry of parseEntry(multipart(stream, options.boundary), options)) {
if (entry.type === 'directory') {
yield {
type: 'directory',
name: entry.name,
mtime: entry.mtime,
mode: entry.mode
}
await ignore(entry.body)
}
if (entry.type === 'symlink') {
yield {
type: 'symlink',
name: entry.name,
target: (await collect(entry.body)).toString('utf8'),
mtime: entry.mtime,
mode: entry.mode
}
}
if (entry.type === 'file') {
yield {
type: 'file',
name: entry.name,
content: entry.body,
mtime: entry.mtime,
mode: entry.mode
}
}
}
}
module.exports = parser