forked from pubnub/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.js
More file actions
139 lines (108 loc) · 3.37 KB
/
Copy pathnode.js
File metadata and controls
139 lines (108 loc) · 3.37 KB
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
/** @flow */
import { Readable, PassThrough } from 'stream';
import { ReadStream } from 'fs';
import { basename } from 'path';
import { IFile, FileClass } from '../';
type PubNubFileNodeConstructor = {|
stream?: Readable,
data?: string | Buffer,
encoding?: string,
name?: string,
mimeType?: string,
|};
type PubNubFileNodeSupportedInputType = Readable | Buffer | string;
const PubNubFile: FileClass = class PubNubFile implements IFile {
static supportsBlob = false;
static supportsFile = false;
static supportsBuffer = typeof Buffer !== 'undefined';
static supportsStream = true;
static supportsString = true;
static supportsArrayBuffer = false;
static supportsEncryptFile = true;
static supportsFileUri = false;
data: PubNubFileNodeSupportedInputType;
name: string;
mimeType: string;
static create(config: PubNubFileNodeConstructor) {
return new this(config);
}
constructor({ stream, data, encoding, name, mimeType }: PubNubFileNodeConstructor) {
if (stream instanceof Readable) {
this.data = stream;
if (stream instanceof ReadStream) {
// $FlowFixMe: incomplete flow node definitions
this.name = basename(stream.path);
}
} else if (data instanceof Buffer) {
this.data = Buffer.from(data);
} else if (typeof data === 'string') {
// $FlowFixMe: incomplete flow node definitions
this.data = Buffer.from(data, encoding ?? 'utf8');
}
if (name) {
this.name = basename(name);
}
if (mimeType) {
this.mimeType = mimeType;
}
if (this.data === undefined) {
throw new Error("Couldn't construct a file out of supplied options.");
}
if (this.name === undefined) {
throw new Error("Couldn't guess filename out of the options. Please provide one.");
}
}
toBuffer(): Promise<Buffer> {
if (this.data instanceof Buffer) {
return Promise.resolve(Buffer.from(this.data));
}
if (this.data instanceof Readable) {
const stream = this.data;
return new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.once('error', reject);
stream.once('end', () => {
resolve(Buffer.concat(chunks));
});
});
}
if (typeof this.data === 'string') {
return Promise.resolve(Buffer.from(this.data));
}
throw new Error("Can't cast to 'buffer'.");
}
async toArrayBuffer() {
throw new Error('This feature is only supported in browser environments.');
}
async toString(encoding: buffer$NonBufferEncoding = 'utf8') {
const buffer = await this.toBuffer();
return buffer.toString(encoding);
}
async toStream() {
if (!(this.data instanceof Readable)) {
const input = this.data;
return new Readable({
read() {
this.push(Buffer.from(input));
this.push(null);
},
});
}
const stream = new PassThrough();
if (this.data instanceof Readable) {
this.data.pipe(stream);
}
return stream;
}
async toFile() {
throw new Error('This feature is only supported in browser environments.');
}
async toFileUri() {
throw new Error('This feature is only supported in react native environments.');
}
async toBlob() {
throw new Error('This feature is only supported in browser environments.');
}
};
export default PubNubFile;