Skip to content

fix: misc updates #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 5 additions & 29 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,18 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
node: ["16", "15", "14", "12", engines]
exclude:
# On Windows, run tests with only the LTS environments.
- os: windows-latest
node: engines
- os: windows-latest
node: "14"
# On macOS, run tests with only the LTS environments.
- os: macOS-latest
node: engines
- os: macOS-latest
node: "14"
node: ["17.3"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we include the currently supported versions of Node.js here?

Suggested change
node: ["17.3"]
node: ["12.20.0", "14.13.0", "16.0.0", "17"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The http module loader API changed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can revisit this later.


runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v2

- name: Get Node.JS version from package.json
if: matrix.node == 'engines'
id: get-version
run: echo ::set-output name=node::$(npx --q minimum-node-version)

- uses: actions/setup-node@v2-beta
if: matrix.node != 'engines'
with:
node-version: ${{ matrix.node }}

- uses: actions/setup-node@v2-beta
if: matrix.node == 'engines'
- uses: actions/setup-node@v2
with:
node-version: ${{steps.get-version.outputs.node}}

node-version: '17.3'
- run: npm install

- run: npm test
- run: npm run report -- --colors

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
Expand Down
20 changes: 0 additions & 20 deletions .github/workflows/lint.yml

This file was deleted.

8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pids
*.seed
*.pid.lock



# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

Expand Down Expand Up @@ -47,8 +49,8 @@ typings/
# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache
# Optional cache
.cache

# Optional REPL history
.node_repl_history
Expand All @@ -63,3 +65,5 @@ typings/
.env

*.d.ts
*.d.cts
.DS_Store
5 changes: 5 additions & 0 deletions file.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const _File = class File extends Blob {
get [Symbol.toStringTag] () {
return 'File'
}

static [Symbol.hasInstance] (object) {
return !!object && object instanceof Blob &&
/^(File)$/.test(object[Symbol.toStringTag])
}
}

/** @type {typeof globalThis.File} */// @ts-ignore
Expand Down
12 changes: 4 additions & 8 deletions from.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { statSync, createReadStream, promises as fs } from 'node:fs'
import { basename } from 'node:path'
import { MessageChannel } from 'node:worker_threads'
import DOMException from 'node-domexception'

import File from './file.js'
import Blob from './index.js'

const { stat } = fs

const DOMException = globalThis.DOMException || (() => {
const port = new MessageChannel().port1
const ab = new ArrayBuffer(0)
try { port.postMessage(ab, [ab, ab]) } catch (err) { return err.constructor }
})()

/**
* @param {string} path filepath on the disk
* @param {string} [type] mimetype to use
Expand All @@ -22,12 +16,14 @@ const blobFromSync = (path, type) => fromBlob(statSync(path), path, type)
/**
* @param {string} path filepath on the disk
* @param {string} [type] mimetype to use
* @returns {Promise<Blob>}
*/
const blobFrom = (path, type) => stat(path).then(stat => fromBlob(stat, path, type))

/**
* @param {string} path filepath on the disk
* @param {string} [type] mimetype to use
* @returns {Promise<File>}
*/
const fileFrom = (path, type) => stat(path).then(stat => fromFile(stat, path, type))

Expand Down Expand Up @@ -80,7 +76,7 @@ class BlobDataItem {
path: this.#path,
lastModified: this.lastModified,
size: end - start,
start
start: this.#start + start
})
}

Expand Down
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@

import './streams.cjs'

/** @typedef {import('buffer').Blob} NodeBlob} */

// 64 KiB (same size chrome slice theirs blob into Uint8array's)
const POOL_SIZE = 65536

/** @param {(Blob | NodeBlob | Uint8Array)[]} parts */
/** @param {(Blob | Uint8Array)[]} parts */
async function * toIterator (parts, clone = true) {
for (const part of parts) {
if ('stream' in part) {
yield * part.stream()
yield * (/** @type {AsyncIterableIterator<Uint8Array>} */ (part.stream()))
} else if (ArrayBuffer.isView(part)) {
if (clone) {
let position = part.byteOffset
Expand All @@ -28,17 +26,16 @@ async function * toIterator (parts, clone = true) {
} else {
yield part
}
/* c8 ignore next 10 */
} else {
/* c8 ignore start */
// For blobs that have arrayBuffer but no stream method (nodes buffer.Blob)
let position = 0
while (position !== part.size) {
const chunk = part.slice(position, Math.min(part.size, position + POOL_SIZE))
let position = 0, b = (/** @type {Blob} */ (part))
while (position !== b.size) {
const chunk = b.slice(position, Math.min(b.size, position + POOL_SIZE))
const buffer = await chunk.arrayBuffer()
position += buffer.byteLength
yield new Uint8Array(buffer)
}
/* c8 ignore end */
}
}
}
Expand All @@ -48,14 +45,15 @@ const _Blob = class Blob {
#parts = []
#type = ''
#size = 0
#endings = 'transparent'

/**
* The Blob() constructor returns a new Blob object. The content
* of the blob consists of the concatenation of the values given
* in the parameter array.
*
* @param {*} blobParts
* @param {{ type?: string }} [options]
* @param {{ type?: string, endings?: string }} [options]
*/
constructor (blobParts = [], options = {}) {
if (typeof blobParts !== 'object' || blobParts === null) {
Expand All @@ -82,15 +80,15 @@ const _Blob = class Blob {
} else if (element instanceof Blob) {
part = element
} else {
part = encoder.encode(element)
part = encoder.encode(`${element}`)
}

this.#size += ArrayBuffer.isView(part) ? part.byteLength : part.size
this.#parts.push(part)
}

this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`
const type = options.type === undefined ? '' : String(options.type)

this.#type = /^[\x20-\x7E]*$/.test(type) ? type : ''
}

Expand Down Expand Up @@ -156,6 +154,7 @@ const _Blob = class Blob {
const it = toIterator(this.#parts, true)

return new globalThis.ReadableStream({
// @ts-ignore
type: 'bytes',
async pull (ctrl) {
const chunk = await it.next()
Expand Down
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fetch-blob",
"version": "3.1.3",
"version": "3.1.4",
"description": "Blob & File implementation in Node.js, originally from node-fetch.",
"main": "index.js",
"type": "module",
Expand All @@ -14,10 +14,9 @@
"streams.cjs"
],
"scripts": {
"test-wpt": "node --experimental-loader ./test/http-loader.js ./test/test-wpt-in-node.js",
"test": "ava test.js",
"report": "c8 --reporter json --reporter text ava test.js",
"coverage": "c8 --reporter json --reporter text ava test.js && codecov -f coverage/coverage-final.json",
"test": "node --experimental-loader ./test/http-loader.js ./test/test-wpt-in-node.js",
"report": "c8 --reporter json --reporter text npm run test",
"coverage": "npm run report && codecov -f coverage/coverage-final.json",
"prepublishOnly": "tsc --declaration --emitDeclarationOnly --allowJs index.js from.js"
},
"repository": "https://github.com/node-fetch/fetch-blob.git",
Expand All @@ -36,11 +35,9 @@
},
"homepage": "https://github.com/node-fetch/fetch-blob#readme",
"devDependencies": {
"ava": "^3.15.0",
"c8": "^7.7.2",
"codecov": "^3.8.2",
"node-fetch": "^3.0.0-beta.9",
"typescript": "^4.3.2"
"@types/node": "^17.0.9",
"c8": "^7.11.0",
"typescript": "^4.5.4"
},
"funding": [
{
Expand All @@ -53,6 +50,7 @@
}
],
"dependencies": {
"node-domexception": "^1.0.0",
"web-streams-polyfill": "^3.0.3"
}
}
Loading