Skip to content

Commit dc29588

Browse files
authored
fix: misc updates (#132)
* misc updates * bring back c8 for test coverage * use new loader * don't use engines * simplify ci * fix double slice problem
1 parent 8ab587d commit dc29588

File tree

11 files changed

+396
-639
lines changed

11 files changed

+396
-639
lines changed

.github/workflows/ci.yml

+5-29
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,18 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest, windows-latest, macOS-latest]
17-
node: ["16", "15", "14", "12", engines]
18-
exclude:
19-
# On Windows, run tests with only the LTS environments.
20-
- os: windows-latest
21-
node: engines
22-
- os: windows-latest
23-
node: "14"
24-
# On macOS, run tests with only the LTS environments.
25-
- os: macOS-latest
26-
node: engines
27-
- os: macOS-latest
28-
node: "14"
17+
node: ["17.3"]
2918

3019
runs-on: ${{ matrix.os }}
20+
3121
steps:
3222
- uses: actions/checkout@v2
33-
34-
- name: Get Node.JS version from package.json
35-
if: matrix.node == 'engines'
36-
id: get-version
37-
run: echo ::set-output name=node::$(npx --q minimum-node-version)
38-
39-
- uses: actions/setup-node@v2-beta
40-
if: matrix.node != 'engines'
41-
with:
42-
node-version: ${{ matrix.node }}
43-
44-
- uses: actions/setup-node@v2-beta
45-
if: matrix.node == 'engines'
23+
- uses: actions/setup-node@v2
4624
with:
47-
node-version: ${{steps.get-version.outputs.node}}
48-
25+
node-version: '17.3'
4926
- run: npm install
50-
27+
- run: npm test
5128
- run: npm run report -- --colors
52-
5329
- name: Upload coverage to Codecov
5430
uses: codecov/codecov-action@v1
5531
with:

.github/workflows/lint.yml

-20
This file was deleted.

.gitignore

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pids
1616
*.seed
1717
*.pid.lock
1818

19+
20+
1921
# Directory for instrumented libs generated by jscoverage/JSCover
2022
lib-cov
2123

@@ -47,8 +49,8 @@ typings/
4749
# Optional npm cache directory
4850
.npm
4951

50-
# Optional eslint cache
51-
.eslintcache
52+
# Optional cache
53+
.cache
5254

5355
# Optional REPL history
5456
.node_repl_history
@@ -63,3 +65,5 @@ typings/
6365
.env
6466

6567
*.d.ts
68+
*.d.cts
69+
.DS_Store

file.js

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const _File = class File extends Blob {
3737
get [Symbol.toStringTag] () {
3838
return 'File'
3939
}
40+
41+
static [Symbol.hasInstance] (object) {
42+
return !!object && object instanceof Blob &&
43+
/^(File)$/.test(object[Symbol.toStringTag])
44+
}
4045
}
4146

4247
/** @type {typeof globalThis.File} */// @ts-ignore

from.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { statSync, createReadStream, promises as fs } from 'node:fs'
22
import { basename } from 'node:path'
3-
import { MessageChannel } from 'node:worker_threads'
3+
import DOMException from 'node-domexception'
44

55
import File from './file.js'
66
import Blob from './index.js'
77

88
const { stat } = fs
99

10-
const DOMException = globalThis.DOMException || (() => {
11-
const port = new MessageChannel().port1
12-
const ab = new ArrayBuffer(0)
13-
try { port.postMessage(ab, [ab, ab]) } catch (err) { return err.constructor }
14-
})()
15-
1610
/**
1711
* @param {string} path filepath on the disk
1812
* @param {string} [type] mimetype to use
@@ -22,12 +16,14 @@ const blobFromSync = (path, type) => fromBlob(statSync(path), path, type)
2216
/**
2317
* @param {string} path filepath on the disk
2418
* @param {string} [type] mimetype to use
19+
* @returns {Promise<Blob>}
2520
*/
2621
const blobFrom = (path, type) => stat(path).then(stat => fromBlob(stat, path, type))
2722

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

@@ -80,7 +76,7 @@ class BlobDataItem {
8076
path: this.#path,
8177
lastModified: this.lastModified,
8278
size: end - start,
83-
start
79+
start: this.#start + start
8480
})
8581
}
8682

index.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55

66
import './streams.cjs'
77

8-
/** @typedef {import('buffer').Blob} NodeBlob} */
9-
108
// 64 KiB (same size chrome slice theirs blob into Uint8array's)
119
const POOL_SIZE = 65536
1210

13-
/** @param {(Blob | NodeBlob | Uint8Array)[]} parts */
11+
/** @param {(Blob | Uint8Array)[]} parts */
1412
async function * toIterator (parts, clone = true) {
1513
for (const part of parts) {
1614
if ('stream' in part) {
17-
yield * part.stream()
15+
yield * (/** @type {AsyncIterableIterator<Uint8Array>} */ (part.stream()))
1816
} else if (ArrayBuffer.isView(part)) {
1917
if (clone) {
2018
let position = part.byteOffset
@@ -28,17 +26,16 @@ async function * toIterator (parts, clone = true) {
2826
} else {
2927
yield part
3028
}
29+
/* c8 ignore next 10 */
3130
} else {
32-
/* c8 ignore start */
3331
// For blobs that have arrayBuffer but no stream method (nodes buffer.Blob)
34-
let position = 0
35-
while (position !== part.size) {
36-
const chunk = part.slice(position, Math.min(part.size, position + POOL_SIZE))
32+
let position = 0, b = (/** @type {Blob} */ (part))
33+
while (position !== b.size) {
34+
const chunk = b.slice(position, Math.min(b.size, position + POOL_SIZE))
3735
const buffer = await chunk.arrayBuffer()
3836
position += buffer.byteLength
3937
yield new Uint8Array(buffer)
4038
}
41-
/* c8 ignore end */
4239
}
4340
}
4441
}
@@ -48,14 +45,15 @@ const _Blob = class Blob {
4845
#parts = []
4946
#type = ''
5047
#size = 0
48+
#endings = 'transparent'
5149

5250
/**
5351
* The Blob() constructor returns a new Blob object. The content
5452
* of the blob consists of the concatenation of the values given
5553
* in the parameter array.
5654
*
5755
* @param {*} blobParts
58-
* @param {{ type?: string }} [options]
56+
* @param {{ type?: string, endings?: string }} [options]
5957
*/
6058
constructor (blobParts = [], options = {}) {
6159
if (typeof blobParts !== 'object' || blobParts === null) {
@@ -82,15 +80,15 @@ const _Blob = class Blob {
8280
} else if (element instanceof Blob) {
8381
part = element
8482
} else {
85-
part = encoder.encode(element)
83+
part = encoder.encode(`${element}`)
8684
}
8785

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

90+
this.#endings = `${options.endings === undefined ? 'transparent' : options.endings}`
9291
const type = options.type === undefined ? '' : String(options.type)
93-
9492
this.#type = /^[\x20-\x7E]*$/.test(type) ? type : ''
9593
}
9694

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

158156
return new globalThis.ReadableStream({
157+
// @ts-ignore
159158
type: 'bytes',
160159
async pull (ctrl) {
161160
const chunk = await it.next()

package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fetch-blob",
3-
"version": "3.1.3",
3+
"version": "3.1.4",
44
"description": "Blob & File implementation in Node.js, originally from node-fetch.",
55
"main": "index.js",
66
"type": "module",
@@ -14,10 +14,9 @@
1414
"streams.cjs"
1515
],
1616
"scripts": {
17-
"test-wpt": "node --experimental-loader ./test/http-loader.js ./test/test-wpt-in-node.js",
18-
"test": "ava test.js",
19-
"report": "c8 --reporter json --reporter text ava test.js",
20-
"coverage": "c8 --reporter json --reporter text ava test.js && codecov -f coverage/coverage-final.json",
17+
"test": "node --experimental-loader ./test/http-loader.js ./test/test-wpt-in-node.js",
18+
"report": "c8 --reporter json --reporter text npm run test",
19+
"coverage": "npm run report && codecov -f coverage/coverage-final.json",
2120
"prepublishOnly": "tsc --declaration --emitDeclarationOnly --allowJs index.js from.js"
2221
},
2322
"repository": "https://github.com/node-fetch/fetch-blob.git",
@@ -36,11 +35,9 @@
3635
},
3736
"homepage": "https://github.com/node-fetch/fetch-blob#readme",
3837
"devDependencies": {
39-
"ava": "^3.15.0",
40-
"c8": "^7.7.2",
41-
"codecov": "^3.8.2",
42-
"node-fetch": "^3.0.0-beta.9",
43-
"typescript": "^4.3.2"
38+
"@types/node": "^17.0.9",
39+
"c8": "^7.11.0",
40+
"typescript": "^4.5.4"
4441
},
4542
"funding": [
4643
{
@@ -53,6 +50,7 @@
5350
}
5451
],
5552
"dependencies": {
53+
"node-domexception": "^1.0.0",
5654
"web-streams-polyfill": "^3.0.3"
5755
}
5856
}

0 commit comments

Comments
 (0)