1.0.0 • Published 3 years ago

@d3x0r/object-storage v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Object Storage

Object storage interface is a hybrid combination of virtual file system and JSOX for object encoding.

Dependancies

Server Support

This is a remote storage interface

import {ObjectStorage} from "@d3x0r/object-storage"
Object Storage constructor argumentsDescription
WebSocketAn optional websocket connection to an object storage server. If not provided, falls back to localStorage.

Once you have an object storage instance, the storage is accessed primarily with get() and put() methods.

Object methodsReturnArgumentsDescription
getRootpromise()Promise resolves with root directory
putunique ID( object [, options] )Stores or updates an object into storage. If the object was not previously stored or loaded, a unique ID for the object is returned, otherwise the existing ID results. The second parameter is an optional option object, see options below.
getpromise( string or Option object )Returns a promise; success is passed the object loaded from storage. Loads an object using the specified ID.
mappromise( id )Same as get, but also loads any objects of specified ID.
addDecoders( array of decoders )Adds additional decoder values for stored objects
addEncoders( array of encoders )Adds additional encoder values for stored objects
stringifystring(object,...)calls the internal JSOX stringifier on the object; using specified extra decoders
--
delete(id/object)remove object from storage... (danging references in other stored records?)
on( ID, callback )When the specified ID changes, the callback is called with the conent of the object.

get() options

When getting an object, either its ID may be used directly, or the ID and optional extra decoders for the specified object are provided. Extra Decoders may also be assigned to the storage object.

Object get Options namestypeDescription
idstringuse this id for the object storage target
extraDecoders { tag:"tag", p:Type } An array of optional object decoder(parse) types to use when getting objects.
Object put Options namestypeDescription
idstringuse this id for the object storage target
signboolwhether this record should be digitally signed; makes record readonly
keystring(todo) use to make record readable only with specified key.
extraEncoders { tag:"tag", p:Type } An array of optional object encoder(stringify) types to use when putting objects.
fastboolTODO: use a memory only fast cache - for small shared data
localboolTODO: prefer local get

These are some other options under developemnt...

Object put Options namestypeDescription
signedstringthis data should be signed with a permanent id; record becomes readonly.
readKeystringdata to prevent foreign read
sealantstringdata to encode object id
objectHashstringsomething

Simple File System Emulation

This provides a simple file system interface on top of objects. It provides a known root identifier to track other object indentifiers which have otherwise random values.

const storage = new ObjectStorage();


const dir = await storage.getRoot();
Object Storage Directory MethodsReturnargumentsDescription
createFileEntry(filename)creates a new file, ready to be written into.
openFileEntry( filename )opens a file relative to the current root.
folderFileDirectory( pathname )gets a path within the current path.
storenone()saves any changes.
hasBoolean( pathanem )returns true/false indicating whether this directory contains the specified pathname.
removePromise( filename )delete specified file from the file directory.

Once you have a directory you can load file content from it.

const file = await dir.open( "filename" );
file.read();
file.write( content );
Object Storage File MethodsReturnargumentsDescription
openFileDirectory( pathane )opens a file within this file as a folder
getLengthNumber()returns the current length of this file
readPromise( pos, length)Takes optional parameters (length), or (position, length); returns a promise that resolves with an ArrayBuffer
writePromise( ArrayBufferString )Write this array buffer or string to the file. Promise resolves with the id of the object written.
storage.getRoot().then( 
	root=>root.open("filename")
        	.catch( ()=>root.create( "filename" ) )
                	.then( (file)=>file.read().then( data=>{
			   console.log( "File Data:", data );
		} ) ) )

storage.getRoot().then( root=>
	root.open("filename")
		.catch( ()=>
			root.create( "filename" ).then( file=>{
				file.write( "Initial content?" ); 
			} )
		.then( (file)=>
			file.read()
				.then( data=>{
					console.log( "File Data:", data );
				} ) 
			) 
		)