@bnaya/objectbuffer
Advanced tools
Comparing version 0.1.1 to 0.1.2
export declare function createObjectBuffer<T = any>(textDecoder: any, textEncoder: any, size: number, initialValue: T): T; | ||
export declare function getUnderlyingArrayBuffer(objectBuffer: any): ArrayBuffer; | ||
export declare function createObjectBufferFromArrayBuffer<T = any>(textDecoder: any, textEncoder: any, arrayBuffer: ArrayBuffer): any; | ||
export declare function createObjectBufferFromArrayBuffer<T = any>(textDecoder: any, textEncoder: any, arrayBuffer: ArrayBuffer, shouldInitializeArrayBuffer?: boolean): T; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,33 +0,20 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createObjectBuffer = createObjectBuffer; | ||
exports.getUnderlyingArrayBuffer = getUnderlyingArrayBuffer; | ||
exports.createObjectBufferFromArrayBuffer = createObjectBufferFromArrayBuffer; | ||
var _store = require("./internal/store"); | ||
var _objectSaver = require("./internal/objectSaver"); | ||
var _objectWrapper = require("./internal/objectWrapper"); | ||
function createObjectBuffer(textDecoder, textEncoder, size, initialValue) { | ||
import { initializeArrayBuffer } from "./internal/store"; | ||
import { objectSaver } from "./internal/objectSaver"; | ||
import { createObjectWrapper, GET_UNDERLYING_ARRAY_BUFFER_SYMBOL } from "./internal/objectWrapper"; | ||
export function createObjectBuffer(textDecoder, textEncoder, size, initialValue) { | ||
const arrayBuffer = new ArrayBuffer(size); | ||
const dataView = (0, _store.initializeArrayBuffer)(arrayBuffer); | ||
const dataView = initializeArrayBuffer(arrayBuffer); | ||
const { | ||
start | ||
} = (0, _objectSaver.objectSaver)(textEncoder, dataView, initialValue); | ||
} = objectSaver(textEncoder, dataView, initialValue); | ||
dataView.setUint32(8, start); | ||
return (0, _objectWrapper.createObjectWrapper)(dataView, start, textDecoder, textEncoder, true); | ||
return createObjectWrapper(dataView, start, textDecoder, textEncoder, true); | ||
} | ||
function getUnderlyingArrayBuffer(objectBuffer) { | ||
return objectBuffer[_objectWrapper.GET_UNDERLYING_ARRAY_BUFFER_SYMBOL]; | ||
export function getUnderlyingArrayBuffer(objectBuffer) { | ||
return objectBuffer[GET_UNDERLYING_ARRAY_BUFFER_SYMBOL]; | ||
} | ||
function createObjectBufferFromArrayBuffer(textDecoder, textEncoder, arrayBuffer) { | ||
const dataView = new DataView(arrayBuffer); | ||
return (0, _objectWrapper.createObjectWrapper)(dataView, dataView.getUint32(8), textDecoder, textEncoder); | ||
export function createObjectBufferFromArrayBuffer(textDecoder, textEncoder, arrayBuffer, // set to true if the give array buffer is not one from `getUnderlyingArrayBuffer` | ||
shouldInitializeArrayBuffer = false) { | ||
const dataView = shouldInitializeArrayBuffer ? initializeArrayBuffer(arrayBuffer) : new DataView(arrayBuffer); | ||
return createObjectWrapper(dataView, dataView.getUint32(8), textDecoder, textEncoder); | ||
} |
@@ -1,13 +0,4 @@ | ||
"use strict"; | ||
import { createKnownTypeGuard } from "./utils"; | ||
export let ENTRY_TYPE; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isPrimitiveEntryType = exports.PRIMITIVE_TYPES = exports.ENTRY_TYPE = void 0; | ||
var _utils = require("./utils"); | ||
let ENTRY_TYPE; | ||
exports.ENTRY_TYPE = ENTRY_TYPE; | ||
(function (ENTRY_TYPE) { | ||
@@ -28,7 +19,5 @@ ENTRY_TYPE[ENTRY_TYPE["NULL"] = 0] = "NULL"; | ||
ENTRY_TYPE[ENTRY_TYPE["DATE"] = 13] = "DATE"; | ||
})(ENTRY_TYPE || (exports.ENTRY_TYPE = ENTRY_TYPE = {})); | ||
})(ENTRY_TYPE || (ENTRY_TYPE = {})); | ||
const PRIMITIVE_TYPES = [ENTRY_TYPE.NULL, ENTRY_TYPE.UNDEFINED, ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT, ENTRY_TYPE.UBIGINT, ENTRY_TYPE.BOOLEAN, ENTRY_TYPE.STRING]; | ||
exports.PRIMITIVE_TYPES = PRIMITIVE_TYPES; | ||
const isPrimitiveEntryType = (0, _utils.createKnownTypeGuard)(PRIMITIVE_TYPES); | ||
exports.isPrimitiveEntryType = isPrimitiveEntryType; | ||
export const PRIMITIVE_TYPES = [ENTRY_TYPE.NULL, ENTRY_TYPE.UNDEFINED, ENTRY_TYPE.NUMBER, ENTRY_TYPE.BIGINT, ENTRY_TYPE.UBIGINT, ENTRY_TYPE.BOOLEAN, ENTRY_TYPE.STRING]; | ||
export const isPrimitiveEntryType = createKnownTypeGuard(PRIMITIVE_TYPES); |
@@ -1,17 +0,7 @@ | ||
"use strict"; | ||
// import { isPrimitive, primitiveValueToEntry } from "./utils"; | ||
import { appendEntry } from "./store"; | ||
import { ENTRY_TYPE } from "./entry-types"; | ||
import { saveValue } from "./saveValue"; // DETECT circularities using set ?? | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.objectSaver = objectSaver; | ||
var _store = require("./store"); | ||
var _entryTypes = require("./entry-types"); | ||
var _saveValue = require("./saveValue"); | ||
// import { isPrimitive, primitiveValueToEntry } from "./utils"; | ||
// DETECT circularities using set ?? | ||
function objectSaver(textEncoder, dataView, objectToSave) { | ||
export function objectSaver(textEncoder, dataView, objectToSave) { | ||
let totalWrittenBytes = 0; // const writtenLength = 0; | ||
@@ -23,5 +13,5 @@ | ||
for (const [key, value] of objectEntries) { | ||
const rOfValue = (0, _saveValue.saveValue)(textEncoder, dataView, value); | ||
const rOfValue = saveValue(textEncoder, dataView, value); | ||
const objectPropEntry = { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT_PROP, | ||
type: ENTRY_TYPE.OBJECT_PROP, | ||
value: { | ||
@@ -33,3 +23,3 @@ value: rOfValue.start, | ||
}; | ||
const rOfPropEntry = (0, _store.appendEntry)(dataView, objectPropEntry, textEncoder); | ||
const rOfPropEntry = appendEntry(dataView, objectPropEntry, textEncoder); | ||
totalWrittenBytes += rOfPropEntry.length + rOfValue.length; | ||
@@ -40,6 +30,6 @@ nextObjectEntryPointer = rOfPropEntry.start; | ||
const objectStartEntry = { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT, | ||
type: ENTRY_TYPE.OBJECT, | ||
value: nextObjectEntryPointer | ||
}; | ||
const objectEntryAppendResult = (0, _store.appendEntry)(dataView, objectStartEntry, textEncoder); | ||
const objectEntryAppendResult = appendEntry(dataView, objectStartEntry, textEncoder); | ||
totalWrittenBytes += objectEntryAppendResult.length; | ||
@@ -46,0 +36,0 @@ return { |
@@ -1,21 +0,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.createObjectWrapper = createObjectWrapper; | ||
exports.ObjectWrapper = exports.GET_UNDERLYING_ARRAY_BUFFER_SYMBOL = void 0; | ||
var _store = require("./store"); | ||
var _entryTypes = require("./entry-types"); | ||
var _objectWrapperHelpers = require("./objectWrapperHelpers"); | ||
var _saveValue = require("./saveValue"); | ||
const GET_UNDERLYING_ARRAY_BUFFER_SYMBOL = Symbol("GET_UNDERLYING_ARRAY_BUFFER_SYMBOL"); | ||
exports.GET_UNDERLYING_ARRAY_BUFFER_SYMBOL = GET_UNDERLYING_ARRAY_BUFFER_SYMBOL; | ||
class ObjectWrapper { | ||
import { readEntry, writeEntry, appendEntry } from "./store"; | ||
import { ENTRY_TYPE, isPrimitiveEntryType } from "./entry-types"; | ||
import { findObjectPropertyEntry, getObjectPropertiesEntries, deleteObjectPropertyEntryByKey, findLastObjectPropertyEntry } from "./objectWrapperHelpers"; | ||
import { saveValue } from "./saveValue"; | ||
export const GET_UNDERLYING_ARRAY_BUFFER_SYMBOL = Symbol("GET_UNDERLYING_ARRAY_BUFFER_SYMBOL"); | ||
export class ObjectWrapper { | ||
constructor(dataView, entryPointer, textDecoder, textEncoder, isTopLevel) { | ||
@@ -47,3 +33,3 @@ this.dataView = dataView; | ||
const foundEntry = (0, _objectWrapperHelpers.findObjectPropertyEntry)(this.dataView, this.entryPointer, // Add validation ? | ||
const foundEntry = findObjectPropertyEntry(this.dataView, this.entryPointer, // Add validation ? | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
@@ -57,17 +43,17 @@ // @ts-ignore | ||
const [valueEntry] = (0, _store.readEntry)(this.dataView, foundEntry[1].value.value, this.textDecoder); | ||
const [valueEntry] = readEntry(this.dataView, foundEntry[1].value.value, this.textDecoder); | ||
if (valueEntry.type === _entryTypes.ENTRY_TYPE.NULL) { | ||
if (valueEntry.type === ENTRY_TYPE.NULL) { | ||
return null; | ||
} | ||
if (valueEntry.type === _entryTypes.ENTRY_TYPE.UNDEFINED) { | ||
if (valueEntry.type === ENTRY_TYPE.UNDEFINED) { | ||
return undefined; | ||
} | ||
if ((0, _entryTypes.isPrimitiveEntryType)(valueEntry.type)) { | ||
if (isPrimitiveEntryType(valueEntry.type)) { | ||
return valueEntry.value; | ||
} | ||
if (valueEntry.type === _entryTypes.ENTRY_TYPE.OBJECT) { | ||
if (valueEntry.type === ENTRY_TYPE.OBJECT) { | ||
return createObjectWrapper(this.dataView, foundEntry[1].value.value, this.textDecoder, this.textEncoder); | ||
@@ -80,7 +66,7 @@ } | ||
deleteProperty(target, p) { | ||
return (0, _objectWrapperHelpers.deleteObjectPropertyEntryByKey)(this.dataView, this.textDecoder, this.textEncoder, this.entryPointer, p); | ||
return deleteObjectPropertyEntryByKey(this.dataView, this.textDecoder, this.textEncoder, this.entryPointer, p); | ||
} | ||
enumerate() { | ||
const gotEntries = (0, _objectWrapperHelpers.getObjectPropertiesEntries)(this.dataView, this.entryPointer, this.textDecoder); | ||
const gotEntries = getObjectPropertiesEntries(this.dataView, this.entryPointer, this.textDecoder); | ||
return gotEntries.map(e => e.value.key); | ||
@@ -90,3 +76,3 @@ } | ||
ownKeys() { | ||
const gotEntries = (0, _objectWrapperHelpers.getObjectPropertiesEntries)(this.dataView, this.entryPointer, this.textDecoder); | ||
const gotEntries = getObjectPropertiesEntries(this.dataView, this.entryPointer, this.textDecoder); | ||
return gotEntries.map(e => e.value.key); | ||
@@ -108,3 +94,3 @@ } | ||
const foundEntry = (0, _objectWrapperHelpers.findObjectPropertyEntry)(this.dataView, this.entry.value, // Add validation ? | ||
const foundEntry = findObjectPropertyEntry(this.dataView, this.entry.value, // Add validation ? | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
@@ -119,4 +105,4 @@ // @ts-ignore | ||
start: newValueEntryPointer | ||
} = (0, _saveValue.saveValue)(this.textEncoder, this.dataView, value); | ||
const foundPropEntry = (0, _objectWrapperHelpers.findObjectPropertyEntry)(this.dataView, this.entryPointer, p, this.textDecoder); // new prop | ||
} = saveValue(this.textEncoder, this.dataView, value); | ||
const foundPropEntry = findObjectPropertyEntry(this.dataView, this.entryPointer, p, this.textDecoder); // new prop | ||
@@ -126,4 +112,4 @@ if (foundPropEntry === undefined) { | ||
start: newPropEntryPointer | ||
} = (0, _store.appendEntry)(this.dataView, { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT_PROP, | ||
} = appendEntry(this.dataView, { | ||
type: ENTRY_TYPE.OBJECT_PROP, | ||
value: { | ||
@@ -135,12 +121,12 @@ next: 0, | ||
}, this.textEncoder); | ||
const [lastItemPointer, lastItemEntry] = (0, _objectWrapperHelpers.findLastObjectPropertyEntry)(this.dataView, this.entryPointer, this.textDecoder); | ||
const [lastItemPointer, lastItemEntry] = findLastObjectPropertyEntry(this.dataView, this.entryPointer, this.textDecoder); | ||
if (lastItemEntry.type === _entryTypes.ENTRY_TYPE.OBJECT) { | ||
(0, _store.writeEntry)(this.dataView, lastItemPointer, { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT, | ||
if (lastItemEntry.type === ENTRY_TYPE.OBJECT) { | ||
writeEntry(this.dataView, lastItemPointer, { | ||
type: ENTRY_TYPE.OBJECT, | ||
value: newPropEntryPointer | ||
}, this.textEncoder); | ||
} else { | ||
(0, _store.writeEntry)(this.dataView, lastItemPointer, { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT_PROP, | ||
writeEntry(this.dataView, lastItemPointer, { | ||
type: ENTRY_TYPE.OBJECT_PROP, | ||
value: { | ||
@@ -155,4 +141,4 @@ next: newPropEntryPointer, | ||
// overwrite value | ||
(0, _store.writeEntry)(this.dataView, foundPropEntry[0], { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT_PROP, | ||
writeEntry(this.dataView, foundPropEntry[0], { | ||
type: ENTRY_TYPE.OBJECT_PROP, | ||
value: { | ||
@@ -196,10 +182,7 @@ key: foundPropEntry[1].value.key, | ||
get entry() { | ||
return (0, _store.readEntry)(this.dataView, this.entryPointer, this.textDecoder)[0]; | ||
return readEntry(this.dataView, this.entryPointer, this.textDecoder)[0]; | ||
} | ||
} | ||
exports.ObjectWrapper = ObjectWrapper; | ||
function createObjectWrapper(dataView, entryPointer, textDecoder, textEncoder, isTopLevel = false) { | ||
export function createObjectWrapper(dataView, entryPointer, textDecoder, textEncoder, isTopLevel = false) { | ||
return new Proxy({ | ||
@@ -206,0 +189,0 @@ bla: 1 |
@@ -1,22 +0,8 @@ | ||
"use strict"; | ||
import { readEntry, writeEntry } from "./store"; | ||
import { invariant } from "./utils"; | ||
import { ENTRY_TYPE } from "./entry-types"; | ||
export function deleteObjectPropertyEntryByKey(dataView, textDecoder, textEncoder, containingObjectEntryPointer, keyToDeleteBy) { | ||
invariant(containingObjectEntryPointer !== 0, "containingObjectEntryPointer Must not be 0"); | ||
const objectEntry = readEntry(dataView, containingObjectEntryPointer, textDecoder)[0]; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.deleteObjectPropertyEntryByKey = deleteObjectPropertyEntryByKey; | ||
exports.findLastObjectPropertyEntry = findLastObjectPropertyEntry; | ||
exports.findObjectPropertyEntry = findObjectPropertyEntry; | ||
exports.findObjectPropertyEntryOld = findObjectPropertyEntryOld; | ||
exports.getObjectPropertiesEntries = getObjectPropertiesEntries; | ||
var _store = require("./store"); | ||
var _utils = require("./utils"); | ||
var _entryTypes = require("./entry-types"); | ||
function deleteObjectPropertyEntryByKey(dataView, textDecoder, textEncoder, containingObjectEntryPointer, keyToDeleteBy) { | ||
(0, _utils.invariant)(containingObjectEntryPointer !== 0, "containingObjectEntryPointer Must not be 0"); | ||
const objectEntry = (0, _store.readEntry)(dataView, containingObjectEntryPointer, textDecoder)[0]; | ||
if (objectEntry.value === 0) { | ||
@@ -27,7 +13,7 @@ // Nothing to delete here | ||
const firstPropEntry = (0, _store.readEntry)(dataView, objectEntry.value, textDecoder)[0]; | ||
const firstPropEntry = readEntry(dataView, objectEntry.value, textDecoder)[0]; | ||
if (firstPropEntry.value.key === keyToDeleteBy) { | ||
(0, _store.writeEntry)(dataView, containingObjectEntryPointer, { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT, | ||
writeEntry(dataView, containingObjectEntryPointer, { | ||
type: ENTRY_TYPE.OBJECT, | ||
value: firstPropEntry.value.next | ||
@@ -43,3 +29,3 @@ }, textEncoder); | ||
while (entryToMaybeUpdate.value.next !== 0) { | ||
entryToMaybeDelete = (0, _store.readEntry)(dataView, entryToMaybeUpdate.value.next, textDecoder)[0]; | ||
entryToMaybeDelete = readEntry(dataView, entryToMaybeUpdate.value.next, textDecoder)[0]; | ||
@@ -55,4 +41,4 @@ if (entryToMaybeDelete.value.key === keyToDeleteBy) { | ||
if (entryToMaybeDelete && entryToMaybeDelete.value.key === keyToDeleteBy) { | ||
(0, _store.writeEntry)(dataView, entryToMaybeUpdatePointer, { | ||
type: _entryTypes.ENTRY_TYPE.OBJECT_PROP, | ||
writeEntry(dataView, entryToMaybeUpdatePointer, { | ||
type: ENTRY_TYPE.OBJECT_PROP, | ||
value: { | ||
@@ -78,6 +64,5 @@ key: entryToMaybeUpdate.value.key, | ||
export function findLastObjectPropertyEntry(dataView, containingObjectEntryPointer, textDecoder) { | ||
const [containingObjectEntry] = readEntry(dataView, containingObjectEntryPointer, textDecoder); | ||
function findLastObjectPropertyEntry(dataView, containingObjectEntryPointer, textDecoder) { | ||
const [containingObjectEntry] = (0, _store.readEntry)(dataView, containingObjectEntryPointer, textDecoder); | ||
if (containingObjectEntry.value === 0) { | ||
@@ -90,3 +75,3 @@ return [containingObjectEntryPointer, containingObjectEntry]; | ||
while (true) { | ||
const [objectPropEntry] = (0, _store.readEntry)(dataView, nextElementPointer, textDecoder); | ||
const [objectPropEntry] = readEntry(dataView, nextElementPointer, textDecoder); | ||
@@ -100,5 +85,4 @@ if (objectPropEntry.value.next === 0) { | ||
} | ||
function findObjectPropertyEntry(dataView, containingObjectEntryPointer, key, textDecoder) { | ||
const [containingObjectEntry] = (0, _store.readEntry)(dataView, containingObjectEntryPointer, textDecoder); | ||
export function findObjectPropertyEntry(dataView, containingObjectEntryPointer, key, textDecoder) { | ||
const [containingObjectEntry] = readEntry(dataView, containingObjectEntryPointer, textDecoder); | ||
let currentPointer = containingObjectEntry.value; | ||
@@ -113,3 +97,3 @@ let objectPropEntry; | ||
while (true) { | ||
[objectPropEntry] = (0, _store.readEntry)(dataView, currentPointer, textDecoder); | ||
[objectPropEntry] = readEntry(dataView, currentPointer, textDecoder); | ||
@@ -129,5 +113,4 @@ if (objectPropEntry.value.key === key || objectPropEntry.value.next === 0) { | ||
} | ||
function findObjectPropertyEntryOld(dataView, containingObjectEntryPointer, key, textDecoder) { | ||
const [containingObjectEntry] = (0, _store.readEntry)(dataView, containingObjectEntryPointer, textDecoder); | ||
export function findObjectPropertyEntryOld(dataView, containingObjectEntryPointer, key, textDecoder) { | ||
const [containingObjectEntry] = readEntry(dataView, containingObjectEntryPointer, textDecoder); | ||
let nextElementPointer = containingObjectEntry.value; | ||
@@ -137,3 +120,3 @@ let objectPropEntry; | ||
do { | ||
[objectPropEntry] = (0, _store.readEntry)(dataView, nextElementPointer, textDecoder); | ||
[objectPropEntry] = readEntry(dataView, nextElementPointer, textDecoder); | ||
nextElementPointer = objectPropEntry.value.next; | ||
@@ -148,5 +131,4 @@ } while (objectPropEntry.value.key !== key && nextElementPointer !== 0); | ||
} | ||
function getObjectPropertiesEntries(dataView, containingObjectEntryPointer, textDecoder) { | ||
const [containingObjectEntry] = (0, _store.readEntry)(dataView, containingObjectEntryPointer, textDecoder); | ||
export function getObjectPropertiesEntries(dataView, containingObjectEntryPointer, textDecoder) { | ||
const [containingObjectEntry] = readEntry(dataView, containingObjectEntryPointer, textDecoder); | ||
const foundProps = []; | ||
@@ -157,3 +139,3 @@ let nextElementPointer = containingObjectEntry.value; | ||
do { | ||
[objectPropEntry] = (0, _store.readEntry)(dataView, nextElementPointer, textDecoder); | ||
[objectPropEntry] = readEntry(dataView, nextElementPointer, textDecoder); | ||
foundProps.push(objectPropEntry); | ||
@@ -160,0 +142,0 @@ nextElementPointer = objectPropEntry.value.next; |
@@ -1,24 +0,14 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.saveValue = saveValue; | ||
var _utils = require("./utils"); | ||
var _store = require("./store"); | ||
var _objectSaver = require("./objectSaver"); | ||
function saveValue(textEncoder, dataView, value) { | ||
import { primitiveValueToEntry, isPrimitive } from "./utils"; | ||
import { appendEntry } from "./store"; | ||
import { objectSaver } from "./objectSaver"; | ||
export function saveValue(textEncoder, dataView, value) { | ||
let totalWrittenBytes = 0; | ||
let valuePointer = 0; | ||
if ((0, _utils.isPrimitive)(value)) { | ||
const entry = (0, _utils.primitiveValueToEntry)(value); | ||
if (isPrimitive(value)) { | ||
const entry = primitiveValueToEntry(value); | ||
const { | ||
start, | ||
length | ||
} = (0, _store.appendEntry)(dataView, entry, textEncoder); | ||
} = appendEntry(dataView, entry, textEncoder); | ||
valuePointer = start; | ||
@@ -30,3 +20,3 @@ totalWrittenBytes += length; | ||
length | ||
} = (0, _objectSaver.objectSaver)(textEncoder, dataView, value); | ||
} = objectSaver(textEncoder, dataView, value); | ||
valuePointer = start; | ||
@@ -33,0 +23,0 @@ totalWrittenBytes += length; |
@@ -1,17 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.initializeArrayBuffer = initializeArrayBuffer; | ||
exports.writeEntry = writeEntry; | ||
exports.appendEntry = appendEntry; | ||
exports.readEntry = readEntry; | ||
exports.Store = void 0; | ||
var _entryTypes = require("./entry-types"); | ||
import { ENTRY_TYPE } from "./entry-types"; | ||
const DEFAULT_ARRAY_BUFFER_SIZE = 10 ^ 6; | ||
class Store { | ||
export class Store { | ||
constructor(arrayBuffer) { | ||
@@ -23,6 +10,3 @@ this.arrayBuffer = arrayBuffer ? arrayBuffer : new ArrayBuffer(DEFAULT_ARRAY_BUFFER_SIZE); | ||
} | ||
exports.Store = Store; | ||
function initializeArrayBuffer(arrayBuffer) { | ||
export function initializeArrayBuffer(arrayBuffer) { | ||
const dataView = new DataView(arrayBuffer); // End of data pointer | ||
@@ -35,4 +19,3 @@ | ||
} | ||
function writeEntry(dataView, startingCursor, entry, | ||
export function writeEntry(dataView, startingCursor, entry, | ||
/** | ||
@@ -50,9 +33,9 @@ * pass yours env's textEncoder. (node and the browser has it) | ||
switch (entry.type) { | ||
case _entryTypes.ENTRY_TYPE.UNDEFINED: | ||
case ENTRY_TYPE.UNDEFINED: | ||
break; | ||
case _entryTypes.ENTRY_TYPE.NULL: | ||
case ENTRY_TYPE.NULL: | ||
break; | ||
case _entryTypes.ENTRY_TYPE.BOOLEAN: | ||
case ENTRY_TYPE.BOOLEAN: | ||
dataView.setUint8(cursor, entry.value ? 1 : 0); | ||
@@ -62,3 +45,3 @@ cursor += Uint8Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.NUMBER: | ||
case ENTRY_TYPE.NUMBER: | ||
dataView.setFloat64(cursor, entry.value); | ||
@@ -68,3 +51,3 @@ cursor += Float64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.STRING: | ||
case ENTRY_TYPE.STRING: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -82,3 +65,3 @@ const encodedString = textEncoder.encode(entry.value); | ||
case _entryTypes.ENTRY_TYPE.BIGINT: | ||
case ENTRY_TYPE.BIGINT: | ||
dataView.setBigInt64(cursor, entry.value); | ||
@@ -88,3 +71,3 @@ cursor += BigInt64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.UBIGINT: | ||
case ENTRY_TYPE.UBIGINT: | ||
dataView.setBigUint64(cursor, entry.value); | ||
@@ -94,3 +77,3 @@ cursor += BigUint64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.OBJECT: | ||
case ENTRY_TYPE.OBJECT: | ||
dataView.setUint32(cursor, entry.value); | ||
@@ -100,3 +83,3 @@ cursor += Uint32Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.OBJECT_PROP: | ||
case ENTRY_TYPE.OBJECT_PROP: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -119,3 +102,3 @@ const encodedStringKey = textEncoder.encode(entry.value.key); | ||
default: | ||
throw new Error(_entryTypes.ENTRY_TYPE[entry.type] + " Not implemented yet"); | ||
throw new Error(ENTRY_TYPE[entry.type] + " Not implemented yet"); | ||
} | ||
@@ -125,4 +108,3 @@ | ||
} | ||
function appendEntry(dataView, entry, | ||
export function appendEntry(dataView, entry, | ||
/** | ||
@@ -141,4 +123,3 @@ * pass yours env's textEncoder. (node and the browser has it) | ||
} | ||
function readEntry(dataView, startingCursor, | ||
export function readEntry(dataView, startingCursor, | ||
/** | ||
@@ -157,9 +138,9 @@ * pass yours env's textDecoder. (node and the browser has it) | ||
switch (entryType) { | ||
case _entryTypes.ENTRY_TYPE.UNDEFINED: | ||
case ENTRY_TYPE.UNDEFINED: | ||
break; | ||
case _entryTypes.ENTRY_TYPE.NULL: | ||
case ENTRY_TYPE.NULL: | ||
break; | ||
case _entryTypes.ENTRY_TYPE.BOOLEAN: | ||
case ENTRY_TYPE.BOOLEAN: | ||
entry.value = dataView.getUint8(cursor); | ||
@@ -169,3 +150,3 @@ cursor += Uint8Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.NUMBER: | ||
case ENTRY_TYPE.NUMBER: | ||
entry.value = dataView.getFloat64(cursor); | ||
@@ -175,3 +156,3 @@ cursor += Float64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.STRING: | ||
case ENTRY_TYPE.STRING: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -184,3 +165,3 @@ const stringLength = dataView.getUint16(cursor); | ||
case _entryTypes.ENTRY_TYPE.BIGINT: | ||
case ENTRY_TYPE.BIGINT: | ||
entry.value = dataView.getBigInt64(cursor); | ||
@@ -190,3 +171,3 @@ cursor += BigInt64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.UBIGINT: | ||
case ENTRY_TYPE.UBIGINT: | ||
entry.value = dataView.getBigUint64(cursor); | ||
@@ -196,3 +177,3 @@ cursor += BigUint64Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.OBJECT: | ||
case ENTRY_TYPE.OBJECT: | ||
entry.value = dataView.getUint32(cursor); | ||
@@ -202,3 +183,3 @@ cursor += Uint32Array.BYTES_PER_ELEMENT; | ||
case _entryTypes.ENTRY_TYPE.OBJECT_PROP: | ||
case ENTRY_TYPE.OBJECT_PROP: | ||
// eslint-disable-next-line no-case-declarations | ||
@@ -218,3 +199,3 @@ const keyStringLength = dataView.getUint16(cursor); | ||
default: | ||
throw new Error(_entryTypes.ENTRY_TYPE[entryType] + " Not implemented yet"); | ||
throw new Error(ENTRY_TYPE[entryType] + " Not implemented yet"); | ||
} | ||
@@ -221,0 +202,0 @@ |
@@ -1,9 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.arrayBuffer2HexArray = arrayBuffer2HexArray; | ||
function arrayBuffer2HexArray(buffer, withByteNumber = false) { | ||
export function arrayBuffer2HexArray(buffer, withByteNumber = false) { | ||
if (withByteNumber) { | ||
@@ -10,0 +3,0 @@ return Array.prototype.map.call(new Uint8Array(buffer), (x, index) => `${index}:${x.toString(16).padStart(4, "0x")}`); |
@@ -1,16 +0,4 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.isPrimitive = isPrimitive; | ||
exports.primitiveValueToEntry = primitiveValueToEntry; | ||
exports.createKnownTypeGuard = createKnownTypeGuard; | ||
exports.invariant = invariant; | ||
var _entryTypes = require("./entry-types"); | ||
import { ENTRY_TYPE } from "./entry-types"; | ||
const primitives = ["string", "number", "bigint", "boolean", "undefined"]; | ||
function isPrimitive(value) { | ||
export function isPrimitive(value) { | ||
if (primitives.includes(typeof value)) { | ||
@@ -26,7 +14,6 @@ return true; | ||
} | ||
function primitiveValueToEntry(value) { | ||
export function primitiveValueToEntry(value) { | ||
if (typeof value === "string") { | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.STRING, | ||
type: ENTRY_TYPE.STRING, | ||
value | ||
@@ -38,3 +25,3 @@ }; | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.NUMBER, | ||
type: ENTRY_TYPE.NUMBER, | ||
value | ||
@@ -46,3 +33,3 @@ }; | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.BIGINT, | ||
type: ENTRY_TYPE.BIGINT, | ||
value | ||
@@ -54,3 +41,3 @@ }; | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.BOOLEAN, | ||
type: ENTRY_TYPE.BOOLEAN, | ||
value | ||
@@ -62,3 +49,3 @@ }; | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.UNDEFINED | ||
type: ENTRY_TYPE.UNDEFINED | ||
}; | ||
@@ -69,3 +56,3 @@ } | ||
return { | ||
type: _entryTypes.ENTRY_TYPE.NULL | ||
type: ENTRY_TYPE.NULL | ||
}; | ||
@@ -76,4 +63,3 @@ } | ||
} | ||
function createKnownTypeGuard(arr) { | ||
export function createKnownTypeGuard(arr) { | ||
return function knownTypeGuard(v) { | ||
@@ -83,4 +69,3 @@ return arr.includes(v); | ||
} | ||
function invariant(condition, message) { | ||
export function invariant(condition, message) { | ||
if (!condition) { | ||
@@ -87,0 +72,0 @@ throw new Error(message); |
{ | ||
"name": "@bnaya/objectbuffer", | ||
"description": "Object like api, backed by an array buffer", | ||
"version": "0.1.1", | ||
"main": "dist/index.js", | ||
"version": "0.1.2", | ||
"main": "dist/objectbuffer.cjs.js", | ||
"module": "dist/index.js", | ||
"author": "Bnaya Peretz <me@bnaya.net>", | ||
@@ -15,3 +16,3 @@ "license": "MIT", | ||
"cleanup": "rimraf dist", | ||
"build": "yarn cleanup; yarn babel-build; yarn build-declarations; echo `git rev-parse HEAD` > COMMIT", | ||
"build": "yarn cleanup; yarn babel-build; yarn build-declarations; rollup -c; echo `git rev-parse HEAD` > COMMIT", | ||
"prepack": "yarn build" | ||
@@ -36,2 +37,5 @@ }, | ||
"rimraf": "^3.0.0", | ||
"rollup": "^1.21.2", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"typescript": "^3.6.2", | ||
@@ -38,0 +42,0 @@ "yarn-deduplicate": "^1.1.1" |
# WIP - object like api backed by a single array buffer. | ||
For Modern browsers and node. | ||
For Modern browsers and node. Zero direct dependencies. | ||
"design" doc: () | ||
"design" doc: | ||
https://docs.google.com/document/d/1-UlUyH3HgOrN58avyScZlfjQtfJxgVwK_yE35mQHpYw/edit?usp=sharing | ||
@@ -6,0 +6,0 @@ |
@@ -31,5 +31,9 @@ import { initializeArrayBuffer } from "./internal/store"; | ||
textEncoder: any, | ||
arrayBuffer: ArrayBuffer | ||
): any { | ||
const dataView = new DataView(arrayBuffer); | ||
arrayBuffer: ArrayBuffer, | ||
// set to true if the give array buffer is not one from `getUnderlyingArrayBuffer` | ||
shouldInitializeArrayBuffer = false | ||
): T { | ||
const dataView = shouldInitializeArrayBuffer | ||
? initializeArrayBuffer(arrayBuffer) | ||
: new DataView(arrayBuffer); | ||
@@ -36,0 +40,0 @@ return createObjectWrapper( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
155797
54
4361
21