/
data.js
406 lines (353 loc) · 10.4 KB
/
data.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Data File (files) and Dataset objects (datasets)
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const stream = require('stream')
const url = require('url')
const chardet = require('chardet')
const fetch = require('node-fetch')
const lodash = require('lodash')
const mime = require('mime-types')
const urljoin = require('url-join')
const toArray = require('stream-to-array')
const infer = require('tableschema').infer
const {csvParser} = require('./parser/csv')
const {xlsxParser} = require('./parser/xlsx')
const DEFAULT_ENCODING = 'utf-8'
/**
* A single data file - local or remote
*
* Key properties
*
* descriptor: metadata descriptor for this resource
* stream: a node stream of the contents of this resource
* rows: a node object stream over the contents of this resource (if it has a tabular structure)
*
* Note:
*
* size and hash are direct properties as they are lazily evaluated (if not already specified)
*/
// TODO: support initializing with data
class File {
static load(pathOrDescriptor, {basePath} = {}) {
let descriptor = null
if (lodash.isPlainObject(pathOrDescriptor)) {
descriptor = lodash.cloneDeep(pathOrDescriptor)
// NB: data must come first - we could have data and path in which path
// is not used (data comes from data)
if (descriptor.data) {
return new FileInline(descriptor, {basePath})
} else if (descriptor.path) {
// We want properties already in our descriptor to take priority over
// those inferred from path so we assign in this order
descriptor = Object.assign(parsePath(descriptor.path), descriptor)
}
} else if (lodash.isString(pathOrDescriptor)) {
descriptor = parsePath(pathOrDescriptor, basePath)
} else {
throw new TypeError(`Cannot create File with ${pathOrDescriptor}`)
}
const isRemote = (descriptor.pathType === 'remote' || isUrl(basePath))
if (isRemote) {
return new FileRemote(descriptor, {basePath})
}
return new FileLocal(descriptor, {basePath})
}
constructor(descriptor, {basePath} = {}) {
this.descriptor = descriptor
this._basePath = basePath
}
get path() {
throw new Error('This is an abstract base class which you should not instantiate. Use .load() instead')
}
/**
* Get readable stream
*
* @returns Promise with readable stream object on resolve
*/
stream() {
return null
}
/**
* Get this file as a buffer (async)
*
* @returns: promise which resolves to the buffer
*/
get buffer() {
return (async () => {
const stream = await this.stream()
const buffers = await toArray(stream)
return Buffer.concat(buffers)
})()
}
/**
* Get rows
* @returns Promise with parsed JS objects (depends on file format)
*/
rows({keyed} = {}) {
return this._rows({keyed})
}
_rows({keyed} = {}) {
if (this.descriptor.format in parserDatabase) {
const parser = parserDatabase[this.descriptor.format]
return parser(this, keyed)
}
throw new Error(`We do not have a parser for that format: ${this.descriptor.format}`)
}
async addSchema() {
// Ensure resource is tabular
if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
throw new Error('File is not in known tabular format.')
}
const rows = await toArray(await this.rows())
this.descriptor.schema = await infer(rows)
}
}
class FileLocal extends File {
get path() {
return this._basePath ? path.join(this._basePath, this.descriptor.path) : this.descriptor.path
}
stream() {
return fs.createReadStream(this.path)
}
get size() {
return fs.statSync(this.path).size
}
get hash() {
return crypto.createHash('md5')
.update(fs.readFileSync(this.path))
.digest('base64')
}
get encoding() {
return chardet.detectFileSync(this.path)
}
}
class FileRemote extends File {
get path() {
return this._basePath ? urljoin(this._basePath, this.descriptor.path) : this.descriptor.path
}
stream() {
return (async () => {
const res = await fetch(this.path)
return res.body
})()
}
get encoding() {
return DEFAULT_ENCODING
}
}
class FileInline extends File {
constructor(descriptor, {basePath} = {}) {
super(descriptor, {basePath})
// JSON is special case ...
if (lodash.isString(this.descriptor.data)) {
this._buffer = Buffer.from(this.descriptor.data)
} else { // It is json/javascript
this._buffer = Buffer.from(JSON.stringify(this.descriptor.data))
}
}
// Not really sure this should exist here ... - have it for tests atm
get path() {
return this.descriptor.path
}
get size() {
return this._buffer.byteLength
}
get hash() {
return crypto.createHash('md5')
.update(this._buffer)
.digest('base64')
}
stream() {
const bufferStream = new stream.PassThrough()
bufferStream.end(this._buffer)
return bufferStream
}
rows({keyed} = {}) {
if (lodash.isArray(this.descriptor.data)) {
const rowStream = new stream.PassThrough({objectMode: true})
this.descriptor.data.forEach(row => {
rowStream.write(row)
})
rowStream.end()
return rowStream
}
return this._rows({keyed})
}
}
// Available parsers per file format
const parserDatabase = {
csv: csvParser,
xlsx: xlsxParser,
xls: xlsxParser
}
// List of formats that are known as tabular
const knownTabularFormats = ['csv', 'tsv', 'dsv']
const parsePath = (path_, basePath = null) => {
const isItUrl = isUrl(path_) || isUrl(basePath)
// eslint-disable-next-line no-useless-escape
const fileName = path_.replace(/^.*[\\\/]/, '')
const extension = path.extname(fileName)
return {
path: path_,
pathType: isItUrl ? 'remote' : 'local',
name: fileName.replace(extension, ''),
format: extension.slice(1),
mediatype: mime.lookup(path_) || ''
}
}
const parsePackageIdentifier = path_ => {
return {
path: path_,
type: isUrl(path_) ? 'remote' : 'local'
}
}
const isUrl = path_ => {
const r = new RegExp('^(?:[a-z]+:)?//', 'i')
return r.test(path_)
}
const isPackage = path_ => {
// If it is a path to file we assume it is not a Dataset
// Only exception is 'datapackage.json':
if (path_.endsWith('datapackage.json')) {
return true
}
const isItUrl = isUrl(path_)
if (isItUrl) {
// Guess by path_'s extension
// Get path from url
const pathFromUrl = url.parse(path_).path
// Split path by dots
const splitPath = pathFromUrl.split('.')
const extension = splitPath.pop()
if (!extension.includes('/')) {
return false
}
} else if (fs.lstatSync(path_).isFile()) {
return false
}
// All other cases are true
return true
}
// ========================================================
// Dataset
/**
* A collection of data resources
*
* Under the hood it stores metadata in data package format.
*/
class Dataset {
// TODO: handle owner
constructor(descriptor = {}, identifier = {path: null, owner: null}) {
if (!lodash.isPlainObject(descriptor)) {
throw new TypeError(`To create a new Dataset please use Dataset.load`)
}
this._descriptor = descriptor
this._resources = []
this._identifier = identifier
this._readme = null
}
// eslint-disable-next-line no-unused-vars
static async load(pathOrDescriptor, {path = null, owner = null} = {}) {
if (
!(lodash.isString(pathOrDescriptor) || lodash.isPlainObject(pathOrDescriptor))
) {
throw new TypeError('Dataset needs to be created with descriptor Object or identifier string')
}
const descriptor = lodash.isPlainObject(pathOrDescriptor) ? pathOrDescriptor : {}
const _path = lodash.isPlainObject(pathOrDescriptor) ? null : pathOrDescriptor
const identifier = _path ? parsePackageIdentifier(_path) : {
path: null,
owner
}
// TODO: owner if provided should override anything parsed from path
const pkg = new Dataset(descriptor, identifier)
await pkg._sync()
return pkg
}
// Bootstrap ourselves with {this.path}/datapackage.json and readme if exists
async _sync() {
const readmePath = this._path('README.md')
// eslint-disable-next-line default-case
switch (this.identifier.type) {
case 'remote': {
let res = await fetch(this.dataPackageJsonPath)
this._descriptor = await res.json()
res = await fetch(readmePath)
// May not exist and that is ok!
if (res.status === 200) {
this._readme = await res.text()
}
break
}
case 'local': {
this._descriptor = JSON.parse(fs.readFileSync(this.dataPackageJsonPath))
// Now get README from local disk if exists
if (fs.existsSync(readmePath)) {
this._readme = fs.readFileSync(readmePath).toString()
}
break
}
}
// Now load each resource ...
this._resources = this.descriptor.resources.map(resource => {
return File.load(resource, {basePath: this.path})
})
}
get identifier() {
return this._identifier
}
get descriptor() {
return this._descriptor
}
get path() {
return this.identifier.path
}
get dataPackageJsonPath() {
return this._path('datapackage.json')
}
get readme() {
return this._readme
}
// Array of File objects
get resources() {
return this._resources
}
addResource(resource) {
if (lodash.isPlainObject(resource)) {
this.descriptor.resources.push(resource)
this.resources.push(File.load(resource))
} else if (lodash.isObject(resource)) { // It is already a resource object!
this.descriptor.resources.push(resource.descriptor)
this.resources.push(resource)
} else {
throw new TypeError(`addResource requirese a resource descriptor or an instantiated resources but got: ${resource}`)
}
}
// Path relative to this package
_path(offset = null) {
const path_ = this.path ? this.path.replace('datapackage.json', '') : this.path
// TODO: ensure offset is relative (security etc)
switch (this.identifier.type) {
case 'local':
return path.join(path_, offset)
case 'remote':
return urljoin(path_, offset)
case undefined:
return offset
default:
throw new Error(`Unknown path type: ${this.identifier.type}`)
}
}
}
module.exports = {
File,
FileLocal,
FileRemote,
FileInline,
parsePath,
parsePackageIdentifier,
isUrl,
isPackage,
Dataset
}