Skip to content

Commit

Permalink
fix: file support for electron renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 21, 2021
1 parent fb3180c commit 6526bf3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 65 deletions.
66 changes: 66 additions & 0 deletions file/src/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Blob } from "./package.js"

/**
* @implements {globalThis.File}
*/
export const WebFile = class File extends Blob {
/**
*
* @param {BlobPart[]} init
* @param {string} name - A USVString representing the file name or the path
* to the file.
* @param {FilePropertyBag} [options]
*/
constructor(
init,
name = panic(new TypeError("File constructor requires name argument")),
options = {}
) {
super(init, options)
// Per File API spec https://w3c.github.io/FileAPI/#file-constructor
// Every "/" character of file name must be replaced with a ":".
/** @private */
this._name = name
// It appears that browser do not follow the spec here.
// String(name).replace(/\//g, ":")
/** @private */
this._lastModified = options.lastModified || Date.now()
}

/**
* The name of the file referenced by the File object.
* @type {string}
*/
get name() {
return this._name
}

/**
* The path the URL of the File is relative to.
* @type {string}
*/
get webkitRelativePath() {
return ""
}

/**
* Returns the last modified time of the file, in millisecond since the UNIX
* epoch (January 1st, 1970 at Midnight).
* @returns {number}
*/
get lastModified() {
return this._lastModified
}

get [Symbol.toStringTag]() {
return "File"
}
}

/**
* @param {*} error
* @returns {never}
*/
const panic = error => {
throw error
}
69 changes: 4 additions & 65 deletions file/src/lib.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,13 @@
"use strict"

import { Blob } from "./package.js"
import { WebFile } from "./file.js"

/**
* @implements {globalThis.File}
*/
const WebFile = class File extends Blob {
/**
*
* @param {BlobPart[]} init
* @param {string} name - A USVString representing the file name or the path
* to the file.
* @param {FilePropertyBag} [options]
*/
constructor(
init,
name = panic(new TypeError("File constructor requires name argument")),
options = {}
) {
super(init, options)
// Per File API spec https://w3c.github.io/FileAPI/#file-constructor
// Every "/" character of file name must be replaced with a ":".
/** @private */
this._name = name
// It appears that browser do not follow the spec here.
// String(name).replace(/\//g, ":")
/** @private */
this._lastModified = options.lastModified || Date.now()
}

/**
* The name of the file referenced by the File object.
* @type {string}
*/
get name() {
return this._name
}

/**
* The path the URL of the File is relative to.
* @type {string}
*/
get webkitRelativePath() {
return ""
}

/**
* Returns the last modified time of the file, in millisecond since the UNIX
* epoch (January 1st, 1970 at Midnight).
* @returns {number}
*/
get lastModified() {
return this._lastModified
}

get [Symbol.toStringTag]() {
return "File"
}
}

/**
* @param {*} error
* @returns {never}
*/
const panic = error => {
throw error
}
// Electron-renderer has `XMLHttpRequest` and should get the browser implementation
// instead of node.

// Marking export as a DOM File object instead of custom class.
/** @type {typeof globalThis.File} */
const File = WebFile
const File = typeof XMLHttpRequest === 'function' ? globalThis.File : WebFile

export { File, Blob }

0 comments on commit 6526bf3

Please sign in to comment.