get rid of blob

This commit is contained in:
Randall Schmidt 2021-06-12 15:43:35 -04:00
parent 3d7b27ceb5
commit 34c8887e51
5 changed files with 28 additions and 155 deletions

View File

@ -1,2 +1 @@
test
classes/blob.js

View File

@ -46,10 +46,6 @@ Returns the body as a JavaScript object, parsed from JSON, same as [node-fetch](
Returns the body as a Buffer, same as [node-fetch](https://www.npmjs.com/package/node-fetch).
### async CachedResponse.blob()
Returns the body as a Blob, same as [node-fetch](https://www.npmjs.com/package/node-fetch).
### CachedResponse.status
Returns the HTTP status code of the response, same as [node-fetch](https://www.npmjs.com/package/node-fetch).

View File

@ -1,129 +0,0 @@
/*
This code has been adapted from code in node-fetch.
License text from node-fetch:
The MIT License (MIT)
Copyright (c) 2016 David Frank
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const Stream = require('stream');
const Readable = Stream.Readable;
const BUFFER = Symbol('buffer');
const TYPE = Symbol('type');
class Blob {
constructor() {
this[TYPE] = '';
const blobParts = arguments[0];
const options = arguments[1];
const buffers = [];
let size = 0;
if (blobParts) {
const a = blobParts;
const length = Number(a.length);
for (let i = 0; i < length; i++) {
const element = a[i];
let buffer;
if (element instanceof Buffer) {
buffer = element;
} else if (ArrayBuffer.isView(element)) {
buffer = Buffer.from(element.buffer, element.byteOffset, element.byteLength);
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (element instanceof Blob) {
buffer = element[BUFFER];
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
}
size += buffer.length;
buffers.push(buffer);
}
}
this[BUFFER] = Buffer.concat(buffers);
let type = options && options.type !== undefined && String(options.type).toLowerCase();
if (type && !/[^\u0020-\u007E]/.test(type)) {
this[TYPE] = type;
}
}
get size() {
return this[BUFFER].length;
}
get type() {
return this[TYPE];
}
text() {
return Promise.resolve(this[BUFFER].toString());
}
arrayBuffer() {
const buf = this[BUFFER];
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
return Promise.resolve(ab);
}
stream() {
const readable = new Readable();
readable._read = function () {};
readable.push(this[BUFFER]);
readable.push(null);
return readable;
}
toString() {
return '[object Blob]';
}
slice() {
const size = this.size;
const start = arguments[0];
const end = arguments[1];
let relativeStart, relativeEnd;
if (start === undefined) {
relativeStart = 0;
} else if (start < 0) {
relativeStart = Math.max(size + start, 0);
} else {
relativeStart = Math.min(start, size);
}
if (end === undefined) {
relativeEnd = size;
} else if (end < 0) {
relativeEnd = Math.max(size + end, 0);
} else {
relativeEnd = Math.min(end, size);
}
const span = Math.max(relativeEnd - relativeStart, 0);
const buffer = this[BUFFER];
const slicedBuffer = buffer.slice(relativeStart, relativeStart + span);
const blob = new Blob([], { type: arguments[2] });
blob[BUFFER] = slicedBuffer;
return blob;
}
}
Object.defineProperties(Blob.prototype, {
size: { enumerable: true },
type: { enumerable: true },
slice: { enumerable: true }
});
Object.defineProperty(Blob.prototype, Symbol.toStringTag, {
value: 'Blob',
writable: false,
enumerable: false,
configurable: true
});
module.exports = Blob;

View File

@ -1,5 +1,4 @@
const stream = require('stream');
const Blob = require('./blob.js');
const Headers = require('./headers.js');
class Response {
@ -40,11 +39,6 @@ class Response {
return this.consumeBody();
}
async blob() {
const type = this.headers.get('content-type');
return new Blob([this.consumeBody()], { type });
}
ejectFromCache() {
return this.ejectSelfFromCache();
}

View File

@ -384,21 +384,6 @@ describe('Data tests', function() {
assert.strictEqual(res.fromCache, true);
});
it('Can get PNG blob body', async function() {
let { cachedFetchResponse, standardFetchResponse } = await dualFetch(PNG_BODY_URL);
let cachedBody = await cachedFetchResponse.blob();
let standardBody = await standardFetchResponse.blob();
assert.strictEqual(cachedBody.size, standardBody.size);
assert.strictEqual(cachedBody.type, standardBody.type);
assert.strictEqual(await cachedBody.text(), await standardBody.text());
cachedFetchResponse = await cachedFetch(PNG_BODY_URL);
cachedBody = await cachedFetchResponse.blob();
assert.strictEqual(cachedBody.size, standardBody.size);
assert.strictEqual(cachedBody.type, standardBody.type);
assert.strictEqual(await cachedBody.text(), await standardBody.text());
});
it('Errors if the body type is not supported', async function() {
try {
await cachedFetch(TEXT_BODY_URL, { body: {} });
@ -423,3 +408,31 @@ describe('Memory cache tests', function() {
assert.strictEqual(res.fromCache, false);
});
}).timeout(10000);
describe('File system cache tests', function() {
it('Supports TTL', async function() {
cachedFetch = FetchCache.withCache(new FileSystemCache({ ttl: 100 }));
let res = await cachedFetch(TWO_HUNDRED_URL);
assert.strictEqual(res.fromCache, false);
res = await cachedFetch(TWO_HUNDRED_URL);
assert.strictEqual(res.fromCache, true);
await wait(200);
res = await cachedFetch(TWO_HUNDRED_URL);
assert.strictEqual(res.fromCache, false);
});
it('Can get PNG buffer body', async function() {
cachedFetch = FetchCache.withCache(new FileSystemCache());
res = await cachedFetch(PNG_BODY_URL);
body = await res.buffer();
assert.strictEqual(expectedPngBuffer.equals(body), true);
assert.strictEqual(res.fromCache, false);
res = await cachedFetch(PNG_BODY_URL);
body = await res.buffer();
assert.strictEqual(expectedPngBuffer.equals(body), true);
assert.strictEqual(res.fromCache, true);
});
});