forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
144 lines (115 loc) · 2.79 KB
/
file.js
File metadata and controls
144 lines (115 loc) · 2.79 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
140
141
142
143
144
'use strict';
const {
DateNow,
FunctionPrototypeApply,
NumberIsNaN,
ObjectDefineProperties,
ObjectSetPrototypeOf,
StringPrototypeToWellFormed,
Symbol,
SymbolToStringTag,
} = primordials;
const {
Blob,
TransferableBlob,
} = require('internal/blob');
const {
customInspectSymbol: kInspect,
kEnumerableProperty,
kEmptyObject,
} = require('internal/util');
const {
codes: {
ERR_INVALID_THIS,
ERR_MISSING_ARGS,
},
} = require('internal/errors');
const {
inspect,
} = require('internal/util/inspect');
const {
kClone,
kDeserialize,
} = require('internal/worker/js_transferable');
const kState = Symbol('state');
function isFile(object) {
return object?.[kState] !== undefined;
}
class File extends Blob {
[kState] = { __proto__: null };
constructor(fileBits, fileName, options = kEmptyObject) {
if (arguments.length < 2) {
throw new ERR_MISSING_ARGS('fileBits', 'fileName');
}
super(fileBits, options);
let { lastModified } = options ?? kEmptyObject;
if (lastModified !== undefined) {
// Using Number(...) will not throw an error for bigints.
lastModified = +lastModified;
if (NumberIsNaN(lastModified)) {
lastModified = 0;
}
} else {
lastModified = DateNow();
}
this[kState].name = StringPrototypeToWellFormed(`${fileName}`);
this[kState].lastModified = lastModified;
}
get name() {
if (!isFile(this))
throw new ERR_INVALID_THIS('File');
return this[kState].name;
}
get lastModified() {
if (!isFile(this))
throw new ERR_INVALID_THIS('File');
return this[kState].lastModified;
}
[kInspect](depth, options) {
if (depth < 0) {
return this;
}
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1,
};
return `File ${inspect({
size: this.size,
type: this.type,
name: this[kState].name,
lastModified: this[kState].lastModified,
}, opts)}`;
}
[kClone]() {
return {
data: { ...super[kClone]().data, ...this[kState] },
deserializeInfo: 'internal/file:TransferableFile',
};
}
[kDeserialize](data) {
super[kDeserialize](data);
this[kState] = {
__proto__: null,
name: data.name,
lastModified: data.lastModified,
};
}
}
function TransferableFile(handle, length, type = '') {
FunctionPrototypeApply(TransferableBlob, this, [handle, length, type]);
}
ObjectSetPrototypeOf(TransferableFile.prototype, File.prototype);
ObjectSetPrototypeOf(TransferableFile, File);
ObjectDefineProperties(File.prototype, {
name: kEnumerableProperty,
lastModified: kEnumerableProperty,
[SymbolToStringTag]: {
__proto__: null,
configurable: true,
value: 'File',
},
});
module.exports = {
File,
TransferableFile,
};