native-file-system-adapter
Advanced tools
Comparing version 2.0.4 to 3.0.0
{ | ||
"name": "native-file-system-adapter", | ||
"version": "2.0.4", | ||
"version": "3.0.0", | ||
"description": "Native File System API", | ||
@@ -27,3 +27,3 @@ "main": "src/es6.js", | ||
}, | ||
"types": "types", | ||
"types": "/types/mod.d.ts", | ||
"keywords": [ | ||
@@ -53,8 +53,4 @@ "filesystem", | ||
"optionalDependencies": { | ||
"web-streams-polyfill": "^3.1.1" | ||
"fetch-blob": "^3.2.0" | ||
}, | ||
"dependencies": { | ||
"fetch-blob": "^3.1.5", | ||
"node-domexception": "^1.0.0" | ||
}, | ||
"standard": { | ||
@@ -61,0 +57,0 @@ "globals": [ |
@@ -36,2 +36,31 @@ # Native File System adapter (ponyfill) | ||
### Updating from 2.x to 3.x | ||
v3 removed all top level await that conditionally loaded polyfills like | ||
WritableStream, DOMException, and Blob/File. considering that now all latest | ||
up to date env have this built in globally on `globalThis` namespace. This makes | ||
the file system adapter lighter for ppl who want a smaller bundle and supports | ||
newer engines. | ||
But if you still need to provide polyfills for older environments | ||
then you can provide your own polyfill and set it up with our config before any | ||
other script is evaluated | ||
```js | ||
import config from 'native-file-system-adapter/config.js' | ||
// This is the default config that you could override as needed. | ||
Object.assign(config, { | ||
ReadableStream: globalThis.ReadableStream, | ||
WritableStream: globalThis.WritableStream, | ||
TransformStream: globalThis.TransformStream, | ||
DOMException: globalThis.DOMException, | ||
Blob: globalThis.Blob, | ||
File: globalThis.File | ||
}) | ||
// continue like normal. | ||
import xyz from 'native-file-system-adapter' | ||
``` | ||
### ES import in browser | ||
@@ -38,0 +67,0 @@ |
/* global Blob, DOMException, Response, MessageChannel */ | ||
import { errors } from '../util.js' | ||
import config from '../config.js' | ||
const { | ||
WritableStream, | ||
TransformStream, | ||
DOMException, | ||
Blob | ||
} = config | ||
const { GONE } = errors | ||
// @ts-ignore | ||
const isSafari = /constructor/i.test(window.HTMLElement) || window.safari || window.WebKitPoint | ||
let TransformStream = globalThis.TransformStream | ||
let WritableStream = globalThis.WritableStream | ||
@@ -29,8 +35,2 @@ export class FileHandle { | ||
async createWritable (options = {}) { | ||
if (!TransformStream) { | ||
// @ts-ignore | ||
const ponyfill = await import('https://cdn.jsdelivr.net/npm/web-streams-polyfill@3/dist/ponyfill.es2018.mjs') | ||
TransformStream = ponyfill.TransformStream | ||
WritableStream = ponyfill.WritableStream | ||
} | ||
const sw = await navigator.serviceWorker?.getRegistration() | ||
@@ -37,0 +37,0 @@ const link = document.createElement('a') |
import { errors } from '../util.js' | ||
/** @type {typeof window.File} */ | ||
const File = globalThis.File || await import('fetch-blob/file.js').then(m => m.File) | ||
/** @type {typeof window.Blob} */ | ||
const Blob = globalThis.Blob || await import('fetch-blob').then(m => m.Blob) | ||
import config from '../config.js' | ||
const { File, Blob, DOMException } = config | ||
const { INVALID, GONE, MISMATCH, MOD_ERR, SYNTAX, SECURITY, DISALLOWED } = errors | ||
@@ -8,0 +6,0 @@ |
import fs from 'node:fs/promises' | ||
import { join } from 'node:path' | ||
import 'node-domexception' | ||
import Blob from 'fetch-blob' | ||
import { fileFrom } from 'fetch-blob/from.js' | ||
import { errors } from '../util.js' | ||
// import mime from 'mime-types' | ||
import config from '../config.js' | ||
const { | ||
DOMException | ||
} = config | ||
const { INVALID, GONE, MISMATCH, MOD_ERR, SYNTAX } = errors | ||
/** | ||
* @see https://github.com/node-fetch/fetch-blob/blob/0455796ede330ecffd9eb6b9fdf206cc15f90f3e/index.js#L232 | ||
* @param {*} object | ||
* @returns {object is Blob} | ||
*/ | ||
function isBlob (object) { | ||
return ( | ||
object && | ||
typeof object === 'object' && | ||
typeof object.constructor === 'function' && | ||
( | ||
typeof object.stream === 'function' || | ||
typeof object.arrayBuffer === 'function' | ||
) && | ||
/^(Blob|File)$/.test(object[Symbol.toStringTag]) | ||
) | ||
} | ||
export class Sink { | ||
/** | ||
@@ -68,3 +87,3 @@ * @param {fs.FileHandle} fileHandle | ||
chunk = Buffer.from(chunk) | ||
} else if (chunk instanceof Blob) { | ||
} else if (isBlob(chunk)) { | ||
for await (const data of chunk.stream()) { | ||
@@ -105,2 +124,5 @@ const res = await this._fileHandle.writev([data], this._position) | ||
}) | ||
// TODO: replace once https://github.com/nodejs/node/issues/37340 is fixed | ||
const { fileFrom } = await import('fetch-blob/from.js') | ||
return fileFrom(this._path) | ||
@@ -107,0 +129,0 @@ } |
@@ -1,6 +0,6 @@ | ||
/** @type {typeof WritableStream} */ | ||
const ws = globalThis.WritableStream || await import('https://cdn.jsdelivr.net/npm/web-streams-polyfill@3/dist/ponyfill.es2018.mjs').then(r => r.WritableStream).catch(() => import('web-streams-polyfill').then(r => r.WritableStream)) | ||
import config from './config.js' | ||
// TODO: add types for ws | ||
class FileSystemWritableFileStream extends ws { | ||
const { WritableStream } = config | ||
class FileSystemWritableFileStream extends WritableStream { | ||
constructor (...args) { | ||
@@ -7,0 +7,0 @@ super(...args) |
@@ -27,7 +27,2 @@ /** @typedef {import('./FileSystemDirectoryHandle.js').default} FileSystemDirectoryHandle */ | ||
async function getOriginPrivateDirectory (driver, options = {}) { | ||
if (typeof DataTransfer === 'function' && driver instanceof DataTransfer) { | ||
console.warn('deprecated getOriginPrivateDirectory(dataTransfer). Use "dt.items.getAsFileSystemHandle()"') | ||
const entries = Array.from(driver.items).map(item => item.webkitGetAsEntry()) | ||
return import('./util.js').then(m => m.fromDataTransfer(entries)) | ||
} | ||
if (!driver) { | ||
@@ -34,0 +29,0 @@ return globalThis.navigator?.storage?.getDirectory() || globalThis.getOriginPrivateDirectory() |
106900
1
44
2678
262
- Removedfetch-blob@^3.1.5
- Removednode-domexception@^1.0.0