-
Notifications
You must be signed in to change notification settings - Fork 432
/
importFromStream.js
103 lines (89 loc) · 2.95 KB
/
importFromStream.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
const fs = require('fs')
const os = require('os')
const path = require('path')
const miss = require('mississippi')
const gunzipMaybe = require('gunzip-maybe')
const peek = require('peek-stream')
const isTar = require('is-tar')
const tar = require('tar-fs')
const globby = require('globby')
const debug = require('debug')('sanity:import:stream')
const {noop} = require('lodash')
const getJsonStreamer = require('./util/getJsonStreamer')
module.exports = (stream, options, importers) =>
new Promise((resolve, reject) => {
const outputPath = path.join(os.tmpdir(), 'sanity-import')
debug('Importing from stream')
let isTarStream = false
let jsonDocuments
const uncompressStream = miss.pipeline(gunzipMaybe(), untarMaybe())
miss.pipe(stream, uncompressStream, (err) => {
if (err) {
reject(err)
return
}
if (isTarStream) {
findAndImport()
} else {
resolve(importers.fromArray(jsonDocuments, options))
}
})
function untarMaybe() {
return peek({newline: false, maxBuffer: 300}, (data, swap) => {
if (isTar(data)) {
debug('Stream is a tarball, extracting to %s', outputPath)
isTarStream = true
return swap(null, tar.extract(outputPath))
}
debug('Stream is an ndjson file, streaming JSON')
const jsonStreamer = getJsonStreamer()
const concatter = miss.concat(resolveNdjsonStream)
const ndjsonStream = miss.pipeline(jsonStreamer, concatter)
ndjsonStream.on('error', (err) => {
uncompressStream.emit('error', err)
destroy([uncompressStream, jsonStreamer, concatter, ndjsonStream])
reject(err)
})
return swap(null, ndjsonStream)
})
}
function resolveNdjsonStream(documents) {
debug('Finished reading ndjson stream')
jsonDocuments = documents
}
async function findAndImport() {
debug('Tarball extracted, looking for ndjson')
const files = await globby('**/*.ndjson', {cwd: outputPath, deep: 2, absolute: true})
if (!files.length) {
reject(new Error('ndjson-file not found in tarball'))
return
}
const importBaseDir = path.dirname(files[0])
resolve(importers.fromFolder(importBaseDir, {...options, deleteOnComplete: true}, importers))
}
})
function destroy(streams) {
streams.forEach((stream) => {
if (isFS(stream)) {
// use close for fs streams to avoid fd leaks
stream.close(noop)
} else if (isRequest(stream)) {
// request.destroy just do .end - .abort is what we want
stream.abort()
} else if (isFn(stream.destroy)) {
stream.destroy()
}
})
}
function isFn(fn) {
return typeof fn === 'function'
}
function isFS(stream) {
return (
(stream instanceof (fs.ReadStream || noop) || stream instanceof (fs.WriteStream || noop)) &&
isFn(stream.close)
)
}
function isRequest(stream) {
return stream.setHeader && isFn(stream.abort)
}