mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
chore: update deno_file to use deno_webidl (#10042)
This changes the custom input converters in deno_file to use deno_webidl converters.
This commit is contained in:
parent
fd65e6de3d
commit
ee07ef2962
5 changed files with 185 additions and 108 deletions
|
@ -3,6 +3,7 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
/// <reference no-default-lib="true" />
|
/// <reference no-default-lib="true" />
|
||||||
/// <reference path="../../core/lib.deno_core.d.ts" />
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
||||||
|
/// <reference path="../webidl/internal.d.ts" />
|
||||||
/// <reference path="../web/internal.d.ts" />
|
/// <reference path="../web/internal.d.ts" />
|
||||||
/// <reference path="../web/lib.deno_web.d.ts" />
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
||||||
/// <reference path="./internal.d.ts" />
|
/// <reference path="./internal.d.ts" />
|
||||||
|
@ -11,6 +12,8 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
|
const webidl = window.__bootstrap.webidl;
|
||||||
|
|
||||||
// TODO(lucacasonato): this needs to not be hardcoded and instead depend on
|
// TODO(lucacasonato): this needs to not be hardcoded and instead depend on
|
||||||
// host os.
|
// host os.
|
||||||
const isWindows = false;
|
const isWindows = false;
|
||||||
|
@ -143,62 +146,39 @@
|
||||||
[_byteSequence];
|
[_byteSequence];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {BlobPart[]} [blobParts]
|
* @param {BlobPart[]} blobParts
|
||||||
* @param {BlobPropertyBag} [options]
|
* @param {BlobPropertyBag} options
|
||||||
*/
|
*/
|
||||||
constructor(blobParts, options) {
|
constructor(blobParts = [], options = {}) {
|
||||||
if (blobParts === undefined) {
|
const prefix = "Failed to construct 'Blob'";
|
||||||
blobParts = [];
|
blobParts = webidl.converters["sequence<BlobPart>"](blobParts, {
|
||||||
}
|
context: "Argument 1",
|
||||||
if (typeof blobParts !== "object") {
|
prefix,
|
||||||
throw new TypeError(
|
});
|
||||||
`Failed to construct 'Blob'. blobParts cannot be converted to a sequence.`,
|
options = webidl.converters["BlobPropertyBag"](options, {
|
||||||
);
|
context: "Argument 2",
|
||||||
}
|
prefix,
|
||||||
|
});
|
||||||
|
|
||||||
const parts = [];
|
this[webidl.brand] = webidl.brand;
|
||||||
const iterator = blobParts[Symbol.iterator]?.();
|
|
||||||
if (iterator === undefined) {
|
|
||||||
throw new TypeError(
|
|
||||||
"Failed to construct 'Blob'. The provided value cannot be converted to a sequence",
|
|
||||||
);
|
|
||||||
}
|
|
||||||
while (true) {
|
|
||||||
const { value: element, done } = iterator.next();
|
|
||||||
if (done) break;
|
|
||||||
if (
|
|
||||||
ArrayBuffer.isView(element) || element instanceof ArrayBuffer ||
|
|
||||||
element instanceof Blob
|
|
||||||
) {
|
|
||||||
parts.push(element);
|
|
||||||
} else {
|
|
||||||
parts.push(String(element));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!options || typeof options === "function") {
|
|
||||||
options = {};
|
|
||||||
}
|
|
||||||
if (typeof options !== "object") {
|
|
||||||
throw new TypeError(
|
|
||||||
`Failed to construct 'Blob'. options is not an object.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
const endings = options.endings?.toString() ?? "transparent";
|
|
||||||
const type = options.type?.toString() ?? "";
|
|
||||||
|
|
||||||
/** @type {Uint8Array} */
|
/** @type {Uint8Array} */
|
||||||
this[_byteSequence] = processBlobParts(parts, endings);
|
this[_byteSequence] = processBlobParts(
|
||||||
this.#type = normalizeType(type);
|
blobParts,
|
||||||
|
options.endings,
|
||||||
|
);
|
||||||
|
this.#type = normalizeType(options.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {number} */
|
/** @returns {number} */
|
||||||
get size() {
|
get size() {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
return this[_byteSequence].byteLength;
|
return this[_byteSequence].byteLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {string} */
|
/** @returns {string} */
|
||||||
get type() {
|
get type() {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
return this.#type;
|
return this.#type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,13 +189,35 @@
|
||||||
* @returns {Blob}
|
* @returns {Blob}
|
||||||
*/
|
*/
|
||||||
slice(start, end, contentType) {
|
slice(start, end, contentType) {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
|
const prefix = "Failed to execute 'slice' on 'Blob'";
|
||||||
|
if (start !== undefined) {
|
||||||
|
start = webidl.converters["long long"](start, {
|
||||||
|
clamp: true,
|
||||||
|
context: "Argument 1",
|
||||||
|
prefix,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (end !== undefined) {
|
||||||
|
end = webidl.converters["long long"](end, {
|
||||||
|
clamp: true,
|
||||||
|
context: "Argument 2",
|
||||||
|
prefix,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (contentType !== undefined) {
|
||||||
|
contentType = webidl.converters["DOMString"](contentType, {
|
||||||
|
context: "Argument 3",
|
||||||
|
prefix,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const O = this;
|
const O = this;
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
let relativeStart;
|
let relativeStart;
|
||||||
if (start === undefined) {
|
if (start === undefined) {
|
||||||
relativeStart = 0;
|
relativeStart = 0;
|
||||||
} else {
|
} else {
|
||||||
start = Number(start);
|
|
||||||
if (start < 0) {
|
if (start < 0) {
|
||||||
relativeStart = Math.max(O.size + start, 0);
|
relativeStart = Math.max(O.size + start, 0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -227,7 +229,6 @@
|
||||||
if (end === undefined) {
|
if (end === undefined) {
|
||||||
relativeEnd = O.size;
|
relativeEnd = O.size;
|
||||||
} else {
|
} else {
|
||||||
end = Number(end);
|
|
||||||
if (end < 0) {
|
if (end < 0) {
|
||||||
relativeEnd = Math.max(O.size + end, 0);
|
relativeEnd = Math.max(O.size + end, 0);
|
||||||
} else {
|
} else {
|
||||||
|
@ -239,7 +240,7 @@
|
||||||
if (contentType === undefined) {
|
if (contentType === undefined) {
|
||||||
relativeContentType = "";
|
relativeContentType = "";
|
||||||
} else {
|
} else {
|
||||||
relativeContentType = normalizeType(String(contentType));
|
relativeContentType = normalizeType(contentType);
|
||||||
}
|
}
|
||||||
return new Blob([
|
return new Blob([
|
||||||
O[_byteSequence].buffer.slice(relativeStart, relativeEnd),
|
O[_byteSequence].buffer.slice(relativeStart, relativeEnd),
|
||||||
|
@ -250,6 +251,7 @@
|
||||||
* @returns {ReadableStream<Uint8Array>}
|
* @returns {ReadableStream<Uint8Array>}
|
||||||
*/
|
*/
|
||||||
stream() {
|
stream() {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
const bytes = this[_byteSequence];
|
const bytes = this[_byteSequence];
|
||||||
const stream = new ReadableStream({
|
const stream = new ReadableStream({
|
||||||
type: "bytes",
|
type: "bytes",
|
||||||
|
@ -267,6 +269,7 @@
|
||||||
* @returns {Promise<string>}
|
* @returns {Promise<string>}
|
||||||
*/
|
*/
|
||||||
async text() {
|
async text() {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
const buffer = await this.arrayBuffer();
|
const buffer = await this.arrayBuffer();
|
||||||
return utf8Decoder.decode(buffer);
|
return utf8Decoder.decode(buffer);
|
||||||
}
|
}
|
||||||
|
@ -275,6 +278,7 @@
|
||||||
* @returns {Promise<ArrayBuffer>}
|
* @returns {Promise<ArrayBuffer>}
|
||||||
*/
|
*/
|
||||||
async arrayBuffer() {
|
async arrayBuffer() {
|
||||||
|
webidl.assertBranded(this, Blob);
|
||||||
const stream = this.stream();
|
const stream = this.stream();
|
||||||
let bytes = new Uint8Array();
|
let bytes = new Uint8Array();
|
||||||
for await (const chunk of stream) {
|
for await (const chunk of stream) {
|
||||||
|
@ -288,6 +292,46 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
webidl.converters["Blob"] = webidl.createInterfaceConverter("Blob", Blob);
|
||||||
|
webidl.converters["BlobPart"] = (V, opts) => {
|
||||||
|
// Union for ((ArrayBuffer or ArrayBufferView) or Blob or USVString)
|
||||||
|
if (typeof V == "object") {
|
||||||
|
if (V instanceof Blob) {
|
||||||
|
return webidl.converters["Blob"](V, opts);
|
||||||
|
}
|
||||||
|
if (V instanceof ArrayBuffer || V instanceof SharedArrayBuffer) {
|
||||||
|
return webidl.converters["ArrayBuffer"](V, opts);
|
||||||
|
}
|
||||||
|
if (ArrayBuffer.isView(V)) {
|
||||||
|
return webidl.converters["ArrayBufferView"](V, opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return webidl.converters["USVString"](V, opts);
|
||||||
|
};
|
||||||
|
webidl.converters["sequence<BlobPart>"] = webidl.createSequenceConverter(
|
||||||
|
webidl.converters["BlobPart"],
|
||||||
|
);
|
||||||
|
webidl.converters["EndingType"] = webidl.createEnumConverter("EndingType", [
|
||||||
|
"transparent",
|
||||||
|
"native",
|
||||||
|
]);
|
||||||
|
const blobPropertyBagDictionary = [
|
||||||
|
{
|
||||||
|
key: "type",
|
||||||
|
converter: webidl.converters["DOMString"],
|
||||||
|
defaultValue: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "endings",
|
||||||
|
converter: webidl.converters["EndingType"],
|
||||||
|
defaultValue: "transparent",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
webidl.converters["BlobPropertyBag"] = webidl.createDictionaryConverter(
|
||||||
|
"BlobPropertyBag",
|
||||||
|
blobPropertyBagDictionary,
|
||||||
|
);
|
||||||
|
|
||||||
const _Name = Symbol("[[Name]]");
|
const _Name = Symbol("[[Name]]");
|
||||||
const _LastModfied = Symbol("[[LastModified]]");
|
const _LastModfied = Symbol("[[LastModified]]");
|
||||||
|
|
||||||
|
@ -300,42 +344,62 @@
|
||||||
/**
|
/**
|
||||||
* @param {BlobPart[]} fileBits
|
* @param {BlobPart[]} fileBits
|
||||||
* @param {string} fileName
|
* @param {string} fileName
|
||||||
* @param {FilePropertyBag} [options]
|
* @param {FilePropertyBag} options
|
||||||
*/
|
*/
|
||||||
constructor(fileBits, fileName, options) {
|
constructor(fileBits, fileName, options = {}) {
|
||||||
if (fileBits === undefined) {
|
const prefix = "Failed to construct 'File'";
|
||||||
throw new TypeError(
|
webidl.requiredArguments(arguments.length, 2, { prefix });
|
||||||
"Failed to construct 'File'. 2 arguments required, but first not specified.",
|
|
||||||
);
|
fileBits = webidl.converters["sequence<BlobPart>"](fileBits, {
|
||||||
}
|
context: "Argument 1",
|
||||||
if (fileName === undefined) {
|
prefix,
|
||||||
throw new TypeError(
|
});
|
||||||
"Failed to construct 'File'. 2 arguments required, but second not specified.",
|
fileName = webidl.converters["USVString"](fileName, {
|
||||||
);
|
context: "Argument 2",
|
||||||
}
|
prefix,
|
||||||
super(fileBits, { endings: options?.endings, type: options?.type });
|
});
|
||||||
|
options = webidl.converters["FilePropertyBag"](options, {
|
||||||
|
context: "Argument 3",
|
||||||
|
prefix,
|
||||||
|
});
|
||||||
|
|
||||||
|
super(fileBits, options);
|
||||||
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
this[_Name] = String(fileName).replaceAll("/", ":");
|
this[_Name] = fileName.replaceAll("/", ":");
|
||||||
if (options?.lastModified === undefined) {
|
if (options.lastModified === undefined) {
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
this[_LastModfied] = new Date().getTime();
|
this[_LastModfied] = new Date().getTime();
|
||||||
} else {
|
} else {
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
this[_LastModfied] = Number(options.lastModified);
|
this[_LastModfied] = options.lastModified;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {string} */
|
/** @returns {string} */
|
||||||
get name() {
|
get name() {
|
||||||
|
webidl.assertBranded(this, File);
|
||||||
return this[_Name];
|
return this[_Name];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @returns {number} */
|
/** @returns {number} */
|
||||||
get lastModified() {
|
get lastModified() {
|
||||||
|
webidl.assertBranded(this, File);
|
||||||
return this[_LastModfied];
|
return this[_LastModfied];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
webidl.converters["FilePropertyBag"] = webidl.createDictionaryConverter(
|
||||||
|
"FilePropertyBag",
|
||||||
|
blobPropertyBagDictionary,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
key: "lastModified",
|
||||||
|
converter: webidl.converters["long long"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
window.__bootstrap.file = {
|
window.__bootstrap.file = {
|
||||||
Blob,
|
Blob,
|
||||||
_byteSequence,
|
_byteSequence,
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
const core = Deno.core;
|
const core = Deno.core;
|
||||||
// const webidl = window.__bootstrap.webidl;
|
const webidl = window.__bootstrap.webidl;
|
||||||
const { _byteSequence } = window.__bootstrap.file;
|
const { _byteSequence } = window.__bootstrap.file;
|
||||||
const { URL } = window.__bootstrap.url;
|
const { URL } = window.__bootstrap.url;
|
||||||
|
|
||||||
|
@ -24,12 +24,12 @@
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function createObjectURL(blob) {
|
function createObjectURL(blob) {
|
||||||
// const prefix = "Failed to execute 'createObjectURL' on 'URL'";
|
const prefix = "Failed to execute 'createObjectURL' on 'URL'";
|
||||||
// webidl.requiredArguments(arguments.length, 1, { prefix });
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
||||||
// blob = webidl.converters["Blob"](blob, {
|
blob = webidl.converters["Blob"](blob, {
|
||||||
// context: "Argument 1",
|
context: "Argument 1",
|
||||||
// prefix,
|
prefix,
|
||||||
// });
|
});
|
||||||
|
|
||||||
const url = core.jsonOpSync(
|
const url = core.jsonOpSync(
|
||||||
"op_file_create_object_url",
|
"op_file_create_object_url",
|
||||||
|
@ -45,12 +45,12 @@
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
function revokeObjectURL(url) {
|
function revokeObjectURL(url) {
|
||||||
// const prefix = "Failed to execute 'revokeObjectURL' on 'URL'";
|
const prefix = "Failed to execute 'revokeObjectURL' on 'URL'";
|
||||||
// webidl.requiredArguments(arguments.length, 1, { prefix });
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
||||||
// url = webidl.converters["DOMString"](url, {
|
url = webidl.converters["DOMString"](url, {
|
||||||
// context: "Argument 1",
|
context: "Argument 1",
|
||||||
// prefix,
|
prefix,
|
||||||
// });
|
});
|
||||||
|
|
||||||
core.jsonOpSync(
|
core.jsonOpSync(
|
||||||
"op_file_revoke_object_url",
|
"op_file_revoke_object_url",
|
||||||
|
|
|
@ -615,6 +615,19 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDictionaryConverter(name, ...dictionaries) {
|
function createDictionaryConverter(name, ...dictionaries) {
|
||||||
|
const allMembers = [];
|
||||||
|
for (const members of dictionaries) {
|
||||||
|
for (const member of members) {
|
||||||
|
allMembers.push(member);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
allMembers.sort((a, b) => {
|
||||||
|
if (a.key == b.key) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return a.key < b.key ? -1 : 1;
|
||||||
|
});
|
||||||
|
|
||||||
return function (V, opts = {}) {
|
return function (V, opts = {}) {
|
||||||
const typeV = type(V);
|
const typeV = type(V);
|
||||||
switch (typeV) {
|
switch (typeV) {
|
||||||
|
@ -633,8 +646,7 @@
|
||||||
|
|
||||||
const idlDict = {};
|
const idlDict = {};
|
||||||
|
|
||||||
for (const members of dictionaries) {
|
for (const member of allMembers) {
|
||||||
for (const member of members) {
|
|
||||||
const key = member.key;
|
const key = member.key;
|
||||||
|
|
||||||
let esMemberValue;
|
let esMemberValue;
|
||||||
|
@ -667,7 +679,6 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return idlDict;
|
return idlDict;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 98398fa7f43aa9d15b4a5757c248a1dd21e59bdb
|
Subproject commit 8467929052737f634eb8842a960093c686ae3b9b
|
|
@ -776,7 +776,6 @@
|
||||||
"Blob-stream.any.js": true,
|
"Blob-stream.any.js": true,
|
||||||
"Blob-text.any.js": true,
|
"Blob-text.any.js": true,
|
||||||
"Blob-constructor.any.js": [
|
"Blob-constructor.any.js": [
|
||||||
"Blob interface object",
|
|
||||||
"Passing a FrozenArray as the blobParts array should work (FrozenArray<MessagePort>)."
|
"Passing a FrozenArray as the blobParts array should work (FrozenArray<MessagePort>)."
|
||||||
],
|
],
|
||||||
"Blob-slice-overflow.any.js": true,
|
"Blob-slice-overflow.any.js": true,
|
||||||
|
@ -785,7 +784,10 @@
|
||||||
"file": {
|
"file": {
|
||||||
"File-constructor.any.js": true
|
"File-constructor.any.js": true
|
||||||
},
|
},
|
||||||
"fileReader.any.js": true
|
"fileReader.any.js": true,
|
||||||
|
"url": {
|
||||||
|
"url-format.any.js": true
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"html": {
|
"html": {
|
||||||
"webappapis": {
|
"webappapis": {
|
||||||
|
|
Loading…
Add table
Reference in a new issue