Skip to content

Use a WeakMap for private properties #42

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 7 commits into from
Jun 6, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
truly private internal properties
  • Loading branch information
jimmywarting committed Jun 5, 2020
commit 802c03b45f96e6e682acff9311713f30bb5cf635
33 changes: 16 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@

const {Readable: ReadableStream} = require('stream');

const BUFFER = Symbol('buffer');
const TYPE = Symbol('type');
const wm = new WeakMap();

class Blob {
constructor(blobParts = [], options = { type: '' }) {
const buffers = [];
/* eslint-disable-next-line no-unused-vars */
let size = 0;

if (blobParts) {
Expand All @@ -22,7 +20,7 @@ class Blob {
} else if (element instanceof ArrayBuffer) {
buffer = Buffer.from(element);
} else if (element instanceof Blob) {
buffer = element[BUFFER];
buffer = wm.get(element).buffer;
} else {
buffer = Buffer.from(typeof element === 'string' ? element : String(element));
}
Expand All @@ -34,35 +32,37 @@ class Blob {

const buffer = Buffer.concat(buffers, size);

const type = options && options.type !== undefined && String(options.type).toLowerCase();
if (type && !/[^\u0020-\u007E]/.test(type)) {
this[TYPE] = type;
}
const type = options.type === undefined ? '' : String(options.type).toLowerCase();

wm.set(this, {
type: /[^\u0020-\u007E]/.test(type) ? '' : type,
size,
buffer
});
}

get size() {
return this[BUFFER].length;
return wm.get(this).size;
}

get type() {
return this[TYPE];
return wm.get(this).type;
}

text() {
return Promise.resolve(this[BUFFER].toString());
return Promise.resolve(wm.get(this).buffer.toString());
}

arrayBuffer() {
const buf = this[BUFFER];
const buf = wm.get(this).buffer;
const ab = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
return Promise.resolve(ab);
}

stream() {
const readable = new ReadableStream();
readable._read = () => { };
readable.push(this[BUFFER]);
readable.push(wm.get(this).buffer);
readable.push(null);
return readable;
}
Expand Down Expand Up @@ -96,14 +96,13 @@ class Blob {
}

const span = Math.max(relativeEnd - relativeStart, 0);

const buffer = this[BUFFER];
const slicedBuffer = buffer.slice(
const slicedBuffer = wm.get(this).buffer.slice(
relativeStart,
relativeStart + span
);
const blob = new Blob([], {type: args[2]});
blob[BUFFER] = slicedBuffer;
const _ = wm.get(blob);
_.buffer = slicedBuffer;
return blob;
}
}
Expand Down