2024-09-09 12:13:10 +02:00
var _ _extends = ( this && this . _ _extends ) || ( function ( ) {
var extendStatics = function ( d , b ) {
extendStatics = Object . setPrototypeOf ||
( { _ _proto _ _ : [ ] } instanceof Array && function ( d , b ) { d . _ _proto _ _ = b ; } ) ||
function ( d , b ) { for ( var p in b ) if ( Object . prototype . hasOwnProperty . call ( b , p ) ) d [ p ] = b [ p ] ; } ;
return extendStatics ( d , b ) ;
} ;
return function ( d , b ) {
if ( typeof b !== "function" && b !== null )
throw new TypeError ( "Class extends value " + String ( b ) + " is not a constructor or null" ) ;
extendStatics ( d , b ) ;
function _ _ ( ) { this . constructor = d ; }
d . prototype = b === null ? Object . create ( b ) : ( _ _ . prototype = b . prototype , new _ _ ( ) ) ;
} ;
} ) ( ) ;
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
// }
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
;
var lib _plankton ;
( function ( lib _plankton ) {
var base ;
( function ( base ) {
/ * *
* @ author fenris
* /
function environment ( ) {
2024-09-12 00:03:29 +02:00
return "node" ;
2024-09-09 12:13:10 +02:00
}
base . environment = environment ;
} ) ( base = lib _plankton . base || ( lib _plankton . base = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* @ author fenris
* /
/*export*/ function pseudopointer _null ( ) {
return {
"value" : null
} ;
}
/ * *
* @ author fenris
* /
/*export*/ function pseudopointer _make ( value ) {
return {
"value" : value
} ;
}
/ * *
* @ author fenris
* /
/*export*/ function pseudopointer _isset ( pseudopointer ) {
return ( pseudopointer . value != null ) ;
}
/ * *
* @ author fenris
* /
/*export*/ function pseudopointer _read ( pseudopointer ) {
if ( pseudopointer . value != null ) {
return pseudopointer . value ;
}
else {
var message = "nullpointer dereferencation" ;
throw ( new Error ( message ) ) ;
}
}
/ * *
* @ author fenris
* /
/*export*/ function pseudopointer _write ( pseudopointer , value ) {
pseudopointer . value = value ;
}
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* @ author fenris
* /
var instance _verbosity = 0 ;
/ * *
* @ author fenris
* /
function instance _collate ( value1 , value2 ) {
if ( typeof ( value1 ) === "object" ) {
if ( value1 == null ) {
return ( value2 == null ) ;
}
else {
if ( "_collate" in value1 ) {
return value1 [ "_collate" ] ( value2 ) ;
}
else {
throw ( new Error ( "[collate]" + " " + "object has no such method" ) ) ;
}
}
}
else {
if ( instance _verbosity >= 1 ) {
// lib_plankton.log.warn("[collate]" + " " + "primitive value; using default implementation");
}
return ( value1 === value2 ) ;
}
}
/ * *
* @ author fenris
* /
function instance _compare ( value1 , value2 ) {
if ( typeof ( value1 ) === "object" ) {
if ( "_compare" in value1 ) {
return value1 [ "_compare" ] ( value2 ) ;
}
else {
throw ( new Error ( "[compare]" + " " + "object has no such method" ) ) ;
}
}
else {
if ( instance _verbosity >= 1 ) {
// lib_plankton.log.warn("[compare]" + " " + "primitive value; using default implementation");
}
return ( value1 <= value2 ) ;
}
}
/ * *
* @ author fenris
* /
function instance _clone ( value ) {
if ( typeof ( value ) === "object" ) {
if ( "_clone" in value ) {
return value [ "_clone" ] ( ) ;
}
else {
throw ( new Error ( "[clone]" + " " + "object has no such method" ) ) ;
}
}
else {
if ( instance _verbosity >= 1 ) {
// lib_plankton.log.warn("[clone]" + " " + "primitive value; using default implementation");
}
return value ;
}
}
/ * *
* @ desc the ability to generate a string out of the element , which identifies it to a high degree
* @ author fenris
* /
function instance _hash ( value ) {
if ( typeof ( value ) === "object" ) {
if ( "_hash" in value ) {
return value [ "_hash" ] ( ) ;
}
else {
throw ( new Error ( "[hash]" + " " + "object has no such method" ) ) ;
}
}
else {
if ( instance _verbosity >= 1 ) {
// lib_plankton.log.warn("[hash]" + " " + "primitive value; using default implementation");
}
return String ( value ) ;
}
}
/ * *
* @ desc the ability to map the element to a textual representation ( most likely not injective )
* @ author fenris
* /
function instance _show ( value ) {
if ( typeof ( value ) === "object" ) {
if ( value == null ) {
return "NULL" ;
}
else {
if ( "_show" in value ) {
return value [ "_show" ] ( ) ;
}
else {
// throw (new Error("[show]" + " " + "object has no such method"));
return JSON . stringify ( value ) ;
}
}
}
else {
if ( instance _verbosity >= 1 ) {
// lib_plankton.log.warn("[show]" + " " + "primitive value; using default implementation");
}
return String ( value ) ;
}
}
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* @ author frac
* /
var class _observer = /** @class */ ( function ( ) {
/ * *
* @ author frac
* /
function class _observer ( ) {
this . counter = 0 ;
this . actions = { } ;
this . buffer = [ ] ;
}
/ * *
* @ author frac
* /
class _observer . prototype . empty = function ( ) {
return ( Object . keys ( this . actions ) . length == 0 ) ;
} ;
/ * *
* @ author frac
* /
class _observer . prototype . flush = function ( ) {
this . actions = { } ;
} ;
/ * *
* @ author frac
* /
class _observer . prototype . set = function ( id , action ) {
this . actions [ id ] = action ;
} ;
/ * *
* @ author frac
* /
class _observer . prototype . del = function ( id ) {
delete this . actions [ id ] ;
} ;
/ * *
* @ author frac
* /
class _observer . prototype . add = function ( action ) {
this . set ( ( this . counter ++ ) . toString ( ) , action ) ;
} ;
/ * *
* @ author frac
* /
class _observer . prototype . notify = function ( information , delayed ) {
var _this = this ;
if ( information === void 0 ) { information = { } ; }
if ( delayed === void 0 ) { delayed = false ; }
if ( delayed ) {
this . buffer . push ( information ) ;
}
else {
Object . keys ( this . actions ) . forEach ( function ( id ) { return _this . actions [ id ] ( information ) ; } ) ;
}
} ;
/ * *
* @ author frac
* /
class _observer . prototype . rollout = function ( ) {
var _this = this ;
this . buffer . forEach ( function ( information ) { return _this . notify ( information , false ) ; } ) ;
this . buffer = [ ] ;
} ;
return class _observer ;
} ( ) ) ;
/ * *
* @ author frac
* /
/ *
export interface interface _readable < type _value > {
| * *
* @ author frac
* |
read ( ) : type _executor < type _value , Error > ;
}
* /
/ * *
* @ author frac
* /
/ *
export interface interface _writeable < type _value > {
| * *
* @ author frac
* |
write ( value : type _value ) : type _executor < void , Error > ;
}
* /
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* @ author frac
* /
var class _error = /** @class */ ( function ( _super ) {
_ _extends ( class _error , _super ) ;
/ * *
* @ author frac
* /
function class _error ( message , suberrors ) {
if ( suberrors === void 0 ) { suberrors = [ ] ; }
var _this = _super . call ( this , message ) || this ;
_this . suberrors = suberrors ;
_this . mess = message ;
return _this ;
}
/ * *
* @ override
* @ author frac
* /
class _error . prototype . toString = function ( ) {
return ( /*super.toString()*/ this . mess + " " + ( "[" + this . suberrors . map ( function ( x ) { return x . toString ( ) ; } ) . join ( "," ) + "]" ) ) ;
} ;
return class _error ;
} ( Error ) ) ;
/ *
This file is part of » bacterio - plankton : base « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var base ;
( function ( base ) {
/ * *
* returns the current UNIX timestamp
*
* @ author fenris
* /
function get _current _timestamp ( rounded ) {
if ( rounded === void 0 ) { rounded = false ; }
var x = ( Date . now ( ) / 1000 ) ;
return ( rounded ? Math . round ( x ) : x ) ;
;
}
base . get _current _timestamp = get _current _timestamp ;
/ * *
* /
function object _merge ( core , mantle ) {
return Object . assign ( core , mantle ) ;
}
base . object _merge = object _merge ;
} ) ( base = lib _plankton . base || ( lib _plankton . base = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : pod « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : pod « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : pod « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : pod « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var pod ;
( function ( pod _1 ) {
/ * *
* @ author fenris
* /
function make _empty ( ) {
return {
"kind" : "empty"
} ;
}
pod _1 . make _empty = make _empty ;
/ * *
* @ author fenris
* /
function make _filled ( value ) {
return {
"kind" : "filled" ,
"value" : value
} ;
}
pod _1 . make _filled = make _filled ;
/ * *
* whether the pod is filled
*
* @ author fenris
* /
function is _filled ( pod ) {
return ( pod . kind === "filled" ) ;
}
pod _1 . is _filled = is _filled ;
/ * *
* return the value , stored in the pod - wrapper
*
* @ author fenris
* /
function cull ( pod ) {
if ( ! is _filled ( pod ) ) {
throw ( new Error ( "cull from empty" ) ) ;
}
else {
return pod . value ;
}
}
pod _1 . cull = cull ;
/ * *
* to pass on a empty - pod or to use a filled - pod
*
* @ author fenris
* /
function propagate ( pod , function _ ) {
if ( ! is _filled ( pod ) ) {
return make _empty ( ) ;
}
else {
return make _filled ( function _ ( pod . value ) ) ;
}
}
pod _1 . propagate = propagate ;
/ * *
* @ author fenris
* /
function distinguish ( pod , function _empty , function _filled ) {
return ( ( ! is _filled ( pod ) )
? function _empty ( )
: function _filled ( pod . value ) ) ;
}
pod _1 . distinguish = distinguish ;
/ * *
* /
function show ( pod , options = { } ) {
options = Object . assign ( {
"show_value" : value => String ( value ) ,
} , options ) ;
if ( ! is _filled ( pod ) ) {
return "<·>" ;
}
else {
return ( "<- " + options . show _value ( pod . value ) + " ->" ) ;
}
}
pod _1 . show = show ;
} ) ( pod = lib _plankton . pod || ( lib _plankton . pod = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : pod « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : pod « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : pod « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : pod « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var pod ;
( function ( pod ) {
/ * *
* /
class class _pod {
constructor ( subject ) { this . subject = subject ; }
2024-09-11 17:24:20 +02:00
tear ( ) { return this . subject ; }
static empty ( ) { return ( new class _pod ( pod . make _empty ( ) ) ) ; }
static filled ( value ) { return ( new class _pod ( pod . make _filled ( value ) ) ) ; }
2024-09-09 12:13:10 +02:00
is _empty ( ) { return ( ! pod . is _filled ( this . subject ) ) ; }
is _filled ( ) { return pod . is _filled ( this . subject ) ; }
cull ( ) { return pod . cull ( this . subject ) ; }
show ( show _value = undefined ) { return pod . show ( this . subject , show _value ) ; }
toString ( ) { return this . show ( ) ; }
propagate ( function _ ) { return new class _pod ( pod . propagate ( this . subject , function _ ) ) ; }
distinguish ( function _empty , function _filled ) { return pod . distinguish ( this . subject , function _empty , function _filled ) ; }
}
pod . class _pod = class _pod ;
} ) ( pod = lib _plankton . pod || ( lib _plankton . pod = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* might be completely obsolete
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ * *
* @ author fenris
* /
function promise _reject ( reason ) {
return Promise . reject ( reason ) ;
}
call . promise _reject = promise _reject ;
/ * *
* @ author fenris
* /
function promise _resolve ( result ) {
return Promise . resolve ( result ) ;
}
call . promise _resolve = promise _resolve ;
/ * *
* @ author fenris
* /
function promise _make ( executor ) {
return ( new Promise ( executor ) ) ;
}
call . promise _make = promise _make ;
/ * *
* @ author fenris
* /
function promise _then _close ( promise , resolver , rejector ) {
promise . then ( resolver , rejector ) ;
}
call . promise _then _close = promise _then _close ;
/ * *
* @ author fenris
* /
function promise _then _append ( promise , resolver , rejector = null ) {
if ( rejector == null ) {
rejector = ( reason ) => promise _reject ( reason ) ;
}
return ( promise . then ( resolver , rejector ) ) ;
}
call . promise _then _append = promise _then _append ;
/ * *
* @ author fenris
* /
function promise _all ( promises ) {
return Promise . all ( promises ) ;
}
call . promise _all = promise _all ;
/ * *
* @ author fenris
* /
function promise _chain ( promises , start = undefined ) {
return ( promises . reduce ( ( chain , promise ) => promise _then _append ( chain , promise ) , promise _resolve ( start ) ) ) ;
}
call . promise _chain = promise _chain ;
/ * *
* @ author fenris
* /
function promise _condense ( promises ) {
return ( promise _chain ( promises . map ( promise => result => promise _then _append ( promise ( ) , element => promise _resolve ( result . concat ( [ element ] ) ) ) ) , [ ] ) ) ;
}
call . promise _condense = promise _condense ;
/ * *
* @ author fenris
* /
function promise _group ( promises , options = {
"serial" : false ,
} ) {
const decorate = function ( promise , name ) {
return ( ( ) => promise _then _append ( promise ( ) , value => promise _resolve ( { "key" : name , "value" : value } ) ) ) ;
} ;
if ( options . serial ) {
return ( promise _then _append ( promise _condense ( Object . keys ( promises )
. map ( name => decorate ( promises [ name ] , name ) ) ) , list => promise _resolve ( Object . fromEntries ( list . map ( ( { "key" : key , "value" : value } ) => ( [ key , value ] ) ) ) ) ) ) ;
}
else {
return ( promise _then _append ( promise _all ( Object . keys ( promises )
. map ( name => decorate ( promises [ name ] , name ) )
. map ( promise => promise ( ) ) ) , list => promise _resolve ( Object . fromEntries ( list . map ( ( { "key" : key , "value" : value } ) => ( [ key , value ] ) ) ) ) ) ) ;
}
}
call . promise _group = promise _group ;
/ * *
* @ author fenris
* /
function promise _wrap ( promise , transformator _result , transformator _reason = lib _plankton . call . id ) {
return ( promise _make ( ( resolve , reject ) => {
promise _then _close ( promise , result => resolve ( transformator _result ( result ) ) , reason => reject ( transformator _reason ( reason ) ) ) ;
} ) ) ;
}
call . promise _wrap = promise _wrap ;
/ * *
* @ author fenris
* /
/ *
export function promise _show < type _result , type _reason > ( label : string ) : ( result : type _result ) => type _promise < type _result , type _reason > {
return (
result => promise _make < type _result , type _reason > (
( resolve , reject ) => {
// lib_plankton.log.info(label + ": " + instance_show(result));
process . stdout . write ( label + ": " + instance _show ( result ) ) ;
resolve ( result ) ;
}
)
) ;
}
* /
/ * *
* @ author fenris
* /
/ *
export function promise _log < type _result , type _reason > ( result : type _result ) : ( result : type _result ) => type _promise < type _result , type _reason > {
return promise _show < type _result , type _reason > ( "log" ) ;
}
* /
/ * *
* @ author fenris
* /
function promise _attach ( state , promise , name ) {
return ( promise _wrap ( promise , result => {
state [ name ] = result ;
return state ;
} ) ) ;
}
call . promise _attach = promise _attach ;
/ * *
* @ author fenris
* /
function promise _delay ( promise , delay ) {
return promise _make ( ( resolve , reject ) => {
call . timeout ( ( ) => {
promise _then _close ( promise , resolve , reject ) ;
return null ;
} , delay ) ;
} ) ;
}
call . promise _delay = promise _delay ;
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ * *
* /
class CancellablePromise extends Promise {
/ * *
* /
constructor ( executor ) {
super ( ( resolve , reject ) => { } ) ;
this . subject = ( new Promise ( ( resolve , reject ) => {
Promise . race ( [
new Promise ( executor ) ,
new Promise ( ( resolve _ , reject _ ) => {
this . interval = setInterval ( ( ) => {
if ( ! this . cancelled ) {
// do nothing
}
else {
reject _ ( new Error ( "cancelled" ) ) ;
this . clear ( ) ;
}
} , 0 ) ;
} ) ,
] )
. then ( resolve , reject ) ;
} ) ) ;
this . cancelled = false ;
this . interval = null ;
}
/ * *
* /
clear ( ) {
if ( this . interval === null ) {
// do nothing
}
else {
clearInterval ( this . interval ) ;
this . interval = null ;
}
}
/ * *
* /
then ( onfulfilled , onrejected ) {
this . clear ( ) ;
return this . subject . then ( onfulfilled , onrejected ) ;
}
/ * *
* /
catch ( x ) {
this . clear ( ) ;
return this . subject . catch ( x ) ;
}
/ * *
* /
cancel ( ) {
this . cancelled = true ;
this . clear ( ) ;
}
}
call . CancellablePromise = CancellablePromise ;
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* initializer might be obsolete , since promises are reusable after having been resolved or rejected
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ * *
* @ author fenris
* /
let enum _initializer _state ;
( function ( enum _initializer _state ) {
enum _initializer _state [ enum _initializer _state [ "initial" ] = 0 ] = "initial" ;
enum _initializer _state [ enum _initializer _state [ "waiting" ] = 1 ] = "waiting" ;
enum _initializer _state [ enum _initializer _state [ "successful" ] = 2 ] = "successful" ;
enum _initializer _state [ enum _initializer _state [ "failed" ] = 3 ] = "failed" ;
} ) ( enum _initializer _state = call . enum _initializer _state || ( call . enum _initializer _state = { } ) ) ;
/ * *
* @ author fenris
* /
function initializer _make ( fetcher ) {
let subject = {
"fetcher" : fetcher ,
"state" : enum _initializer _state . initial ,
"queue" : [ ] ,
"result" : undefined ,
"reason" : undefined ,
} ;
return subject ;
}
call . initializer _make = initializer _make ;
/ * *
* @ author fenris
* /
function initializer _actuate ( subject ) {
switch ( subject . state ) {
case enum _initializer _state . successful : {
subject . queue . forEach ( entry => entry . resolve ( subject . result ) ) ;
break ;
}
case enum _initializer _state . failed : {
subject . queue . forEach ( entry => entry . reject ( subject . reason ) ) ;
break ;
}
default : {
throw ( new Error ( ` unhandled state ${ subject . state } ` ) ) ;
break ;
}
}
}
/ * *
* @ author fenris
* /
function initializer _reset ( subject ) {
subject . state = enum _initializer _state . initial ;
subject . queue = [ ] ;
}
call . initializer _reset = initializer _reset ;
/ * *
* @ author fenris
* /
function initializer _state ( subject ) {
return subject . state ;
}
call . initializer _state = initializer _state ;
/ * *
* @ author fenris
* /
function initializer _get ( subject ) {
switch ( subject . state ) {
case enum _initializer _state . initial : {
subject . state = enum _initializer _state . waiting ;
return ( call . promise _make ( ( resolve , reject ) => {
subject . queue . push ( { "resolve" : resolve , "reject" : reject } ) ;
subject . fetcher ( ) . then ( result => {
subject . state = enum _initializer _state . successful ;
subject . result = result ;
initializer _actuate ( subject ) ;
} , reason => {
subject . state = enum _initializer _state . failed ;
subject . reason = reason ;
initializer _actuate ( subject ) ;
} ) ;
} ) ) ;
break ;
}
case enum _initializer _state . waiting : {
return ( call . promise _make ( ( resolve , reject ) => {
subject . queue . push ( { "resolve" : resolve , "reject" : reject } ) ;
} ) ) ;
break ;
}
case enum _initializer _state . successful : {
return ( call . promise _resolve ( subject . result ) ) ;
break ;
}
case enum _initializer _state . failed : {
return ( call . promise _reject ( subject . reason ) ) ;
break ;
}
default : {
throw ( new Error ( ` unhandled state ${ subject . state } ` ) ) ;
break ;
}
}
}
call . initializer _get = initializer _get ;
/ * *
* @ author fenris
* /
function initializer _get _sync ( subject ) {
switch ( subject . state ) {
case enum _initializer _state . successful : {
return subject . result ;
break ;
}
case enum _initializer _state . failed : {
throw subject . reason ;
break ;
}
default : {
throw ( new Error ( ` unhandled state ${ subject . state } ` ) ) ;
break ;
}
}
}
/ * *
* @ author fenris
* /
function initializer _set _sync ( subject , result ) {
switch ( subject . state ) {
case enum _initializer _state . successful : {
subject . result = result ;
break ;
}
case enum _initializer _state . failed : {
subject . state = enum _initializer _state . successful ;
subject . result = result ;
break ;
}
default : {
throw ( new Error ( ` unhandled state ${ subject . state } ` ) ) ;
break ;
}
}
}
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ *
The core idea of this library is to provide means for asynchronous program flow . The old - school way to do is ,
is to use callbacks . While this approach is simple and easy to understand , it has some disadvantages . As an
attempt to relief and improve this , the promise - system was introduced . In principle it solves most of the
problems found in the callback - approach ; however it has some downsides as well :
- Convolution of multiple principles
Promises unite the ideas of asynchronous program flow and error handling .
- Instant execution
Creating a promise results in the instant execution of the given executor prodecure . While this might be
convenient in some cases , it can be quite disturbing and counter - intuitive in others .
- Broken typing
The Promise system doesn ' t distinguish between an appending "then" ( i . e . passing a function , which returns a
new promise ) and a closing "then" ( i . e . passing a function , which has no return value ) . On top of that it
allows returning simple values in an appending "then" , which results in an implicit call of the executors
"resolve" - function . The price for these "pragmatic" features is that the whole system can ' t be typed well .
And even though JavaScript is not a strictly typed language , it was a quite questionable decision to design
the promise system in a way , which breaks typing from the start .
The deferral - system forseeks to solve these issues while retaining the advantages of the promise - system .
* /
/ * *
* @ author fenris
* @ desc activates the deferral and handles its output according to a given procedure
* @ param { ( value : type _value ) => void } procedure a function which receives the output of the deferral as argument
* /
function deferral _use ( deferral , input , procedure ) {
deferral . representation ( input ) . then ( value => {
procedure ( value ) ;
} , reason => {
throw reason ;
} ) ;
}
call . deferral _use = deferral _use ;
/ * *
* @ author fenris
* @ desc creates a deferral - subject ( similar to "new Promise" , where "convey" reflects "resolve" / "reject" )
* /
function deferral _make ( handler ) {
return ( {
"representation" : ( ( input ) => ( new Promise ( ( resolve , reject ) => {
handler ( input , resolve ) ;
} ) ) )
} ) ;
}
call . deferral _make = deferral _make ;
/ * *
* @ author fenris
* @ desc wraps a simple function into a deferral ( similar to "Promise.resolve" / "Promise.reject" )
* /
function deferral _wrap ( function _ ) {
return ( deferral _make ( ( input , convey ) => convey ( function _ ( input ) ) ) ) ;
}
call . deferral _wrap = deferral _wrap ;
/ * *
* @ author fenris
* /
function deferral _id ( ) {
return ( deferral _make ( ( input , convey ) => convey ( input ) ) ) ;
}
call . deferral _id = deferral _id ;
/ * *
* @ author fenris
* /
function deferral _const ( value ) {
return ( deferral _make ( ( input , convey ) => convey ( value ) ) ) ;
}
call . deferral _const = deferral _const ;
/ * *
* @ author fenris
* /
function deferral _delay ( output , delay ) {
return ( deferral _make ( ( input , convey ) => {
setTimeout ( ( ) => convey ( output ) , delay ) ;
} ) ) ;
}
call . deferral _delay = deferral _delay ;
/ * *
* @ author fenris
* @ desc connects two deferrals to form a new one ; the output of the first is taken as input for the second
* ( similar to "Promise.then" when passing a function which returns a new promise )
* @ param { type _deferral < type _value1 > } first a simple deferral
* @ param { ( value1 : type _value1 ) => type _deferral < type _value2 > } second a function depending from a value returning a deferral
* /
function deferral _compose _serial ( first , second ) {
return {
"representation" : ( ( input ) => first . representation ( input ) . then ( ( between ) => second . representation ( between ) ) )
} ;
}
call . deferral _compose _serial = deferral _compose _serial ;
/ * *
* @ author fenris
* /
function deferral _compose _parallel ( { "left" : deferral _left , "right" : deferral _right , } ) {
return ( deferral _make ( ( input , convey ) => {
let object = {
"left" : lib _plankton . pod . make _empty ( ) ,
"right" : lib _plankton . pod . make _empty ( ) ,
} ;
let finish = function ( ) {
if ( lib _plankton . pod . is _filled ( object . left )
&&
lib _plankton . pod . is _filled ( object . right ) ) {
let result = {
"left" : lib _plankton . pod . cull ( object . left ) ,
"right" : lib _plankton . pod . cull ( object . right ) ,
} ;
convey ( result ) ;
}
else {
// do nothing
}
} ;
deferral _use ( deferral _left , input , output _left => {
object . left = lib _plankton . pod . make _filled ( output _left ) ;
finish ( ) ;
} ) ;
deferral _use ( deferral _right , input , output _right => {
object . right = lib _plankton . pod . make _filled ( output _right ) ;
finish ( ) ;
} ) ;
} ) ) ;
}
call . deferral _compose _parallel = deferral _compose _parallel ;
/ * *
* @ author fenris
* @ desc repeatedly applied serial composition
* /
function deferral _chain ( members ) {
return ( members . reduce (
// (result, current) => deferral_compose_serial<type_value, type_value, type_value>(result, current),
deferral _compose _serial , deferral _id ( ) ) ) ;
}
call . deferral _chain = deferral _chain ;
/ * *
* @ author fenris
* /
/ *
export function deferral _bunch < type _input , type _output > (
members : { [ name : string ] : type _deferral < type _input , type _output > }
) : type _deferral < type _input , { [ name : string ] : type _output } > {
}
* /
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ * *
* @ author fenris
* /
class class _deferral {
/ * *
* @ author fenris
* /
constructor ( subject ) {
this . subject = subject ;
}
/ * *
* @ author fenris
* /
static _cram ( subject ) {
return ( new class _deferral ( subject ) ) ;
}
/ * *
* @ author fenris
* /
static _tear ( instance ) {
return instance . subject ;
}
/ * *
* @ author fenris
* /
static make ( handler ) {
return ( class _deferral . _cram ( call . deferral _make ( handler ) ) ) ;
}
/ * *
* @ author fenris
* /
use ( input , procedure ) {
return ( call . deferral _use ( class _deferral . _tear ( this ) , input , procedure ) ) ;
}
/ * *
* @ author fenris
* /
compose _serial ( second ) {
return ( class _deferral . _cram ( call . deferral _compose _serial ( class _deferral . _tear ( this ) , class _deferral . _tear ( second ) ) ) ) ;
}
/ * *
* @ author fenris
* /
static chain ( members ) {
return ( class _deferral . _cram ( call . deferral _chain ( members . map ( member => class _deferral . _tear ( member ) ) ) ) ) ;
}
/ * *
* @ author fenris
* /
static wrap ( function _ ) {
return ( class _deferral . _cram ( call . deferral _wrap ( function _ ) ) ) ;
}
/ * *
* @ author fenris
* /
static const _ ( value ) {
return ( class _deferral . _cram ( call . deferral _const ( value ) ) ) ;
}
/ * *
* @ author fenris
* /
static delay ( output , delay ) {
return ( class _deferral . _cram ( call . deferral _delay ( output , delay ) ) ) ;
}
}
call . class _deferral = class _deferral ;
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : call « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : call « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : call « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : call « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var call ;
( function ( call ) {
/ * *
* converts the "arguments" - map into an array
*
* @ param { Object } args
* @ author fenris
* /
function args2list ( args ) {
return Object . keys ( args ) . map ( key => args [ key ] ) ;
}
call . args2list = args2list ;
/ * *
* just the empty function ; useful for some callbacks etc .
*
* @ author fenris
* /
function nothing ( ) {
}
call . nothing = nothing ;
/ * *
* just the identity ; useful for some callbacks etc . ; defined as function instead of const for using type parameters
*
* @ author fenris
* /
function id ( x ) {
return x ;
}
call . id = id ;
/ * *
* just the identity ; useful for some callbacks etc .
*
* @ author fenris
* /
function const _ ( x ) {
return ( y => x ) ;
}
call . const _ = const _ ;
/ * *
* composes two functions ( i . e . returns a function that return the result of the successive execution of both input - functions )
*
* @ param { function } function _f
* @ param { function } function _g
* @ author fenris
* /
function compose ( function _f , function _g ) {
return ( function ( x ) {
// return function_g(function_f(x));
return function _g ( function _f . apply ( function _f , args2list ( arguments ) ) ) ;
} ) ;
}
call . compose = compose ;
/ * *
* @ author fenris
* /
function curryfy _real ( f , n ) {
switch ( n ) {
case 0 : {
throw ( new Error ( "[curryfy] impossible" ) ) ;
// break;
}
case 1 : {
return f ;
// break;
}
default : {
return ( function ( x ) {
return ( curryfy _real ( function ( ) { return f . apply ( f , [ x ] . concat ( args2list ( arguments ) ) ) ; } , n - 1 ) ) ;
} ) ;
// break;
}
}
}
/ * *
* transforms a function with sequential input to a function with leveled input ; example : add ( 2 , 3 ) = curryfy ( add ) ( 2 ) ( 3 )
*
* @ param { function } f
* @ return { function } the currified version of the in put function
* @ author fenris
* /
function curryfy ( f ) {
return curryfy _real ( f , f . length ) ;
}
call . curryfy = curryfy ;
/ * *
* @ author fenris
* /
function convey ( value , functions ) {
let result = value ;
functions . forEach ( function _ => {
result = function _ ( result ) ;
} ) ;
return result ;
}
call . convey = convey ;
/ * *
* @ author fenris
* /
function timeout ( procedure , delay _in _seconds ) {
return (
/*window.*/ setTimeout ( procedure , Math . floor ( delay _in _seconds * 1000 ) ) ) ;
}
call . timeout = timeout ;
/ * *
* Promise version of "setTimeout"
*
* @ author fenris
* /
function defer ( seconds , action ) {
return ( new Promise ( ( resolve , reject ) => {
setTimeout ( ( ) => resolve ( action ( ) ) , Math . floor ( seconds * 1000 ) ) ;
} ) ) ;
}
call . defer = defer ;
/ * *
* a definition for a value being "defined"
*
* @ author neuc
* /
function is _def ( obj , options = { } ) {
options = Object . assign ( {
"null_is_valid" : false ,
} , options ) ;
return ( ! ( ( typeof ( obj ) === "undefined" )
||
( ! options . null _is _valid && ( obj === null ) ) ) ) ;
}
call . is _def = is _def ;
/ * *
* returns the value if set and , when a type is specified , if the type is correct , if not return default _value
*
* @ author neuc
* /
function def _val ( value , default _value , options = { } ) {
options = Object . assign ( {
"type" : null ,
"null_is_valid" : false ,
} , options ) ;
if ( is _def ( value , { "null_is_valid" : options . null _is _valid } )
&&
( is _def ( options . type )
? ( ( typeof ( value ) === options . type )
||
( ( value === null )
&&
options . null _is _valid ) )
: true ) ) {
return value ;
}
else {
return default _value ;
}
}
call . def _val = def _val ;
;
/ * *
* provides the call for an attribute of a class as a regular function ; useful for processing lists of objects
*
* @ param { string } name the name of the attribute
* @ return { function }
* @ author fenris
* /
function attribute ( name ) {
return ( object => object [ name ] ) ;
}
call . attribute = attribute ;
/ * *
* provides a method of a class as a regular function ; useful for processing lists of objects
*
* @ param { string } name the name of the method
* @ return { function }
* @ author fenris
* /
function method ( name ) {
return ( function ( object ) { return object [ name ] . apply ( object , args2list ( arguments ) . slice ( 1 ) ) ; } ) ;
}
call . method = method ;
/ * *
* @ author fenris
* /
function distinguish ( coproduct , handlers , options = { } ) {
options = Object . assign ( {
"fallback" : null ,
} , options ) ;
if ( coproduct . kind in handlers ) {
const handler = handlers [ coproduct . kind ] ;
return handler ( coproduct . data ) ;
}
else {
const message = ( "unhandled kind '" + coproduct . kind + "'" ) ;
if ( options . fallback !== null ) {
console . warn ( message ) ;
return options . fallback ( coproduct ) ;
}
else {
throw ( new Error ( message ) ) ;
}
}
}
call . distinguish = distinguish ;
/ * *
* rate limiting algorithm , based on the idea of mana ( magic power ) in video games :
* - an actor has a fixed mana capacity , i . e . the maximum amount of available power
* - an actor has a fixed rate of mana regeneration , i . e . how fast the power is filled up ( linear growth )
* - an action has a defined mana heft , i . e . how much power is required and deducted in order to execute it
* - mana states are represented by snapshots , i . e . the amount of power at a certain point in time
*
* @ author fenris
* /
async function rate _limit _check ( setup , heft ) {
if ( heft > setup . capacity ) {
return Promise . resolve ( {
"granted" : false ,
"seconds" : null ,
} ) ;
}
else {
// get current value
const current _timestamp = ( Date . now ( ) / 1000 ) ;
const old _snapshot _raw = ( await setup . get _snapshot ( ) ) ;
const old _snapshot = ( old _snapshot _raw
? ?
{ "timestamp" : current _timestamp , "value" : setup . capacity } ) ;
const seconds _passed = ( current _timestamp - old _snapshot . timestamp ) ;
const current _value = Math . min ( setup . capacity , ( old _snapshot . value
+
( setup . regeneration _rate
*
seconds _passed ) ) ) ;
// analyze
if ( current _value < heft ) {
// too less
const seconds _needed = ( ( setup . regeneration _rate <= 0 )
? null
: ( ( heft - old _snapshot . value ) / setup . regeneration _rate ) ) ;
return Promise . resolve ( {
"granted" : false ,
"seconds" : ( ( seconds _needed === null ) ? null : ( seconds _needed - seconds _passed ) ) ,
} ) ;
}
else {
// enough -> update snapshot
const new _value = ( current _value - heft ) ;
// set_snapshot
if ( old _snapshot _raw === null ) {
await setup . set _snapshot ( { "timestamp" : current _timestamp , "value" : new _value } ) ;
}
else {
await setup . update _snapshot ( current _timestamp , ( new _value - old _snapshot . value ) ) ;
}
return Promise . resolve ( {
"granted" : true ,
"seconds" : 0 ,
} ) ;
}
}
}
call . rate _limit _check = rate _limit _check ;
} ) ( call = lib _plankton . call || ( lib _plankton . call = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-12 00:03:29 +02:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _generator = ( this && this . _ _generator ) || function ( thisArg , body ) {
var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
function step ( op ) {
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
2024-09-21 10:55:06 +02:00
while ( _ ) try {
2024-09-12 00:03:29 +02:00
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
switch ( op [ 0 ] ) {
case 0 : case 1 : t = op ; break ;
case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
default :
if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
if ( t [ 2 ] ) _ . ops . pop ( ) ;
_ . trys . pop ( ) ; continue ;
}
op = body . call ( thisArg , _ ) ;
} catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
}
} ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : email « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : email « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : email « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var email ;
( function ( email ) {
2024-09-09 12:13:10 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function send ( smtp _credentials , sender , receivers , subject , content ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var nm _nodemailer , transporter , info ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
nm _nodemailer = require ( "nodemailer" ) ;
transporter = nm _nodemailer . createTransport ( {
"host" : smtp _credentials . host ,
"port" : smtp _credentials . port ,
"secure" : false ,
"auth" : {
"user" : smtp _credentials . username ,
"pass" : smtp _credentials . password
} ,
"debug" : true
} ) ;
return [ 4 /*yield*/ , transporter . sendMail ( {
"from" : sender ,
"to" : receivers . join ( ", " ) ,
"subject" : subject ,
"text" : content
} ) ] ;
case 1 :
info = _a . sent ( ) ;
return [ 2 /*return*/ ] ;
}
2024-09-09 12:13:10 +02:00
} ) ;
2024-09-12 00:03:29 +02:00
} ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
email . send = send ;
} ) ( email = lib _plankton . email || ( lib _plankton . email = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-12 00:03:29 +02:00
var _ _extends = ( this && this . _ _extends ) || ( function ( ) {
var extendStatics = function ( d , b ) {
extendStatics = Object . setPrototypeOf ||
( { _ _proto _ _ : [ ] } instanceof Array && function ( d , b ) { d . _ _proto _ _ = b ; } ) ||
function ( d , b ) { for ( var p in b ) if ( Object . prototype . hasOwnProperty . call ( b , p ) ) d [ p ] = b [ p ] ; } ;
return extendStatics ( d , b ) ;
} ;
return function ( d , b ) {
if ( typeof b !== "function" && b !== null )
throw new TypeError ( "Class extends value " + String ( b ) + " is not a constructor or null" ) ;
extendStatics ( d , b ) ;
function _ _ ( ) { this . constructor = d ; }
d . prototype = b === null ? Object . create ( b ) : ( _ _ . prototype = b . prototype , new _ _ ( ) ) ;
} ;
} ) ( ) ;
2024-09-11 17:24:20 +02:00
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
var enum _level ;
( function ( enum _level ) {
enum _level [ enum _level [ "debug" ] = 0 ] = "debug" ;
enum _level [ enum _level [ "info" ] = 1 ] = "info" ;
enum _level [ enum _level [ "notice" ] = 2 ] = "notice" ;
enum _level [ enum _level [ "warning" ] = 3 ] = "warning" ;
enum _level [ enum _level [ "error" ] = 4 ] = "error" ;
} ) ( enum _level = log . enum _level || ( log . enum _level = { } ) ) ;
;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function level _order ( level1 , level2 ) {
return ( level1 <= level2 ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . level _order = level _order ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function level _show ( level ) {
switch ( level ) {
case enum _level . debug : return "debug" ;
case enum _level . info : return "info" ;
case enum _level . notice : return "notice" ;
case enum _level . warning : return "warning" ;
case enum _level . error : return "error" ;
default : return "(unknown)" ;
}
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . level _show = level _show ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : lang « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
/ * *
* @ deprecated
* @ todo remove
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
/*export*/ var level _stack = [ 0 ] ;
function level _push ( level ) { level _stack . push ( level ) ; }
log . level _push = level _push ;
function level _pop ( ) { if ( level _stack . length > 1 ) {
level _stack . pop ( ) ;
} }
log . level _pop = level _pop ;
function level _get ( ) { return level _stack . slice ( - 1 ) [ 0 ] ; }
/ *
export function level _inc ( ) : void { level _push ( level _get ( ) + 1 ) ; }
export function level _dec ( ) : void { level _push ( level _get ( ) - 1 ) ; }
* /
/ * *
* @ author fenris
* /
var indent _stack = [ 0 ] ;
function indent _push ( indent ) { indent _stack . push ( indent ) ; }
log . indent _push = indent _push ;
function indent _pop ( ) { if ( indent _stack . length > 1 ) {
indent _stack . pop ( ) ;
} }
log . indent _pop = indent _pop ;
function indent _get ( ) { return level _stack . slice ( - 1 ) [ 0 ] ; }
function indent _inc ( ) { level _push ( level _get ( ) + 1 ) ; }
log . indent _inc = indent _inc ;
function indent _dec ( ) { level _push ( level _get ( ) - 1 ) ; }
log . indent _dec = indent _dec ;
/ * *
* @ author fenris
* /
function write ( _a ) {
var message = _a [ "message" ] , _b = _a [ "type" ] , type = _b === void 0 ? null : _b , _c = _a [ "prefix" ] , prefix = _c === void 0 ? null : _c , _d = _a [ "level" ] , level = _d === void 0 ? 0 : _d , _e = _a [ "indent" ] , indent = _e === void 0 ? 0 : _e ;
var entry = {
"level" : ( ( type === null )
? lib _plankton . log . enum _level . info
: {
"debug" : lib _plankton . log . enum _level . debug ,
"info" : lib _plankton . log . enum _level . info ,
"notice" : lib _plankton . log . enum _level . notice ,
"warning" : lib _plankton . log . enum _level . warning ,
"error" : lib _plankton . log . enum _level . error
} [ type ] ) ,
"incident" : message ,
"details" : {
"prefix" : prefix ,
"level" : level ,
"indent" : indent
}
} ;
lib _plankton . log . add ( entry ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . write = write ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
/ * *
* /
var class _channel = /** @class */ ( function ( ) {
function class _channel ( ) {
}
return class _channel ;
} ( ) ) ;
log . class _channel = class _channel ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-11 17:24:20 +02:00
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* output for writing log entries to stdout
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
var class _channel _stdout = /** @class */ ( function ( _super ) {
_ _extends ( class _channel _stdout , _super ) ;
function class _channel _stdout ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
2024-09-11 17:24:20 +02:00
}
/ * *
* /
2024-09-12 00:03:29 +02:00
class _channel _stdout . prototype . add = function ( entry ) {
process . stdout . write ( ( "<" + ( new Date ( Date . now ( ) ) ) . toISOString ( ) . slice ( 0 , 19 ) + ">" )
+
" "
+
( "[" + log . level _show ( entry . level ) + "]" )
+
" "
+
( "" + entry . incident + "" )
+
": "
+
JSON . stringify ( entry . details , undefined , " " )
+
"\n" ) ;
} ;
return class _channel _stdout ;
} ( log . class _channel ) ) ;
log . class _channel _stdout = class _channel _stdout ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : log « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
/ * *
* /
var class _channel _file = /** @class */ ( function ( _super ) {
_ _extends ( class _channel _file , _super ) ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* [ constructor ]
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
function class _channel _file ( path , human _readable ) {
var _this = _super . call ( this ) || this ;
_this . path = path ;
_this . human _readable = human _readable ;
return _this ;
2024-09-11 17:24:20 +02:00
}
/ * *
* /
2024-09-12 00:03:29 +02:00
class _channel _file . prototype . add = function ( entry ) {
var _this = this ;
var nm _fs = require ( "fs" ) ;
var line = ( this . human _readable
? ( ( "<" + ( new Date ( Date . now ( ) ) ) . toISOString ( ) . slice ( 0 , 19 ) + ">" )
+
" "
+
( "[" + log . level _show ( entry . level ) + "]" )
+
" "
+
( "" + entry . incident + "" )
+
": "
+
JSON . stringify ( entry . details , undefined , " " )
+
"\n" )
: ( JSON . stringify ( {
"timestamp" : lib _plankton . base . get _current _timestamp ( ) ,
"level_number" : entry . level ,
"level_name" : log . level _show ( entry . level ) ,
"incident" : entry . incident ,
"details" : entry . details
} )
+
"\n" ) ) ;
nm _fs . writeFile ( this . path , line , {
"flag" : "a+"
} , function ( error ) {
if ( error !== null ) {
process . stderr . write ( '-- [plankton] could not add log entry to file ' + _this . path + "\n" ) ;
}
else {
// do nothing
}
} ) ;
} ;
return class _channel _file ;
} ( log . class _channel ) ) ;
log . class _channel _file = class _channel _file ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
var class _channel _email = /** @class */ ( function ( _super ) {
_ _extends ( class _channel _email , _super ) ;
/ * *
* [ constructor ]
* /
function class _channel _email ( smtp _credentials , sender , receivers ) {
var _this = _super . call ( this ) || this ;
_this . smtp _credentials = smtp _credentials ;
_this . sender = sender ;
_this . receivers = receivers ;
return _this ;
}
/ * *
* /
class _channel _email . prototype . add = function ( entry ) {
var nm _fs = require ( "fs" ) ;
lib _plankton . email . send ( this . smtp _credentials , this . sender , this . receivers , ( ( "[" + log . level _show ( entry . level ) + "]" )
+
" "
+
( "" + entry . incident + "" ) ) , JSON . stringify ( entry . details , undefined , " " ) ) ;
2024-09-11 17:24:20 +02:00
} ;
2024-09-12 00:03:29 +02:00
return class _channel _email ;
} ( log . class _channel ) ) ;
log . class _channel _email = class _channel _email ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : log « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
/ * *
* output for desktop notifications via "libnotify"
* /
var class _channel _notify = /** @class */ ( function ( _super ) {
_ _extends ( class _channel _notify , _super ) ;
function class _channel _notify ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
/ * *
* /
class _channel _notify . prototype . add = function ( entry ) {
var nm _child _process = require ( "child_process" ) ;
var command = ( "notify-send"
+
" "
+
( "'"
+
( "[" + log . level _show ( entry . level ) + "]" )
+
" "
+
entry . incident
+
"'" )
+
" "
+
( "'"
+
( Object . keys ( entry . details )
. map ( function ( key ) { return ( key + ": " + JSON . stringify ( entry . details [ key ] ) ) ; } )
. join ( "\n" ) )
+
"'" ) ) ;
nm _child _process . exec ( command , function ( error , stdout , stderr ) {
// do noting
} ) ;
} ;
return class _channel _notify ;
} ( log . class _channel ) ) ;
log . class _channel _notify = class _channel _notify ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : log « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
/ * *
* decorator for filtering out log entries below a certain level threshold
* /
var class _channel _minlevel = /** @class */ ( function ( _super ) {
_ _extends ( class _channel _minlevel , _super ) ;
/ * *
* /
function class _channel _minlevel ( core , threshold ) {
var _this = _super . call ( this ) || this ;
_this . core = core ;
_this . threshold = threshold ;
return _this ;
}
/ * *
* /
class _channel _minlevel . prototype . add = function ( entry ) {
if ( ! log . level _order ( this . threshold , entry . level ) ) {
// do nothing
}
else {
this . core . add ( entry ) ;
}
} ;
return class _channel _minlevel ;
} ( log . class _channel ) ) ;
log . class _channel _minlevel = class _channel _minlevel ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : log « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
/ * *
* /
function translate _level ( level _string ) {
return {
"debug" : log . enum _level . debug ,
"info" : log . enum _level . info ,
"notice" : log . enum _level . notice ,
"warning" : log . enum _level . warning ,
"error" : log . enum _level . error
} [ level _string ] ;
2024-09-11 17:24:20 +02:00
}
/ * *
* /
2024-09-12 00:03:29 +02:00
function channel _make ( description ) {
var _a , _b , _c , _d , _e ;
switch ( description . kind ) {
default : {
throw ( new Error ( "unhandled log channel kind: " + description . kind ) ) ;
break ;
}
case "stdout" : {
return ( new log . class _channel _minlevel ( new log . class _channel _stdout ( ) , translate _level ( ( _a = description . data [ "threshold" ] ) !== null && _a !== void 0 ? _a : "debug" ) ) ) ;
break ;
}
case "file" : {
return ( new log . class _channel _minlevel ( new log . class _channel _file ( ( ( _b = description . data [ "path" ] ) !== null && _b !== void 0 ? _b : "/tmp/plankton.log" ) , false ) , translate _level ( ( _c = description . data [ "threshold" ] ) !== null && _c !== void 0 ? _c : "debug" ) ) ) ;
break ;
}
case "email" : {
return ( new log . class _channel _minlevel ( new log . class _channel _email ( description . data [ "smtp_credentials" ] , description . data [ "sender" ] , description . data [ "receivers" ] ) , translate _level ( ( _d = description . data [ "threshold" ] ) !== null && _d !== void 0 ? _d : "debug" ) ) ) ;
break ;
}
case "notify" : {
return ( new log . class _channel _minlevel ( new log . class _channel _notify ( ) , translate _level ( ( _e = description . data [ "threshold" ] ) !== null && _e !== void 0 ? _e : "debug" ) ) ) ;
break ;
}
}
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . channel _make = channel _make ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function conf _default ( ) {
return [
new log . class _channel _minlevel ( new log . class _channel _stdout ( ) , log . enum _level . notice ) ,
new log . class _channel _minlevel ( new log . class _channel _notify ( ) , log . enum _level . error ) ,
] ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . conf _default = conf _default ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : log « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
var _channel _stack = null ;
/ * *
* pushes a new configuration on the stack and activates it
* /
function conf _push ( channels ) {
if ( _channel _stack === null ) {
_channel _stack = [ ] ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
_channel _stack . push ( channels ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . conf _push = conf _push ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* pops the current active configuration from the stack
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
function conf _pop ( ) {
if ( _channel _stack . length > 0 ) {
_channel _stack . pop ( ) ;
2024-09-11 17:24:20 +02:00
}
else {
2024-09-12 00:03:29 +02:00
// do nothing
2024-09-11 17:24:20 +02:00
}
}
2024-09-12 00:03:29 +02:00
log . conf _pop = conf _pop ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* makes the logging system ready
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
function setup ( ) {
if ( _channel _stack === null ) {
_channel _stack = [ ] ;
conf _push ( log . conf _default ( ) ) ;
}
else {
// do nothing
}
2024-09-11 17:24:20 +02:00
}
/ * *
2024-09-12 00:03:29 +02:00
* consumes a log entry , i . e . sends it to the currently active outputs
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
function add ( entry ) {
setup ( ) ;
_channel _stack . slice ( - 1 ) [ 0 ] . forEach ( function ( channel ) { return channel . add ( entry ) ; } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . add = add ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function debug ( incident , details ) {
if ( details === void 0 ) { details = { } ; }
add ( { "level" : log . enum _level . debug , "incident" : incident , "details" : details } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . debug = debug ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function info ( incident , details ) {
if ( details === void 0 ) { details = { } ; }
add ( { "level" : log . enum _level . info , "incident" : incident , "details" : details } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . info = info ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function notice ( incident , details ) {
if ( details === void 0 ) { details = { } ; }
add ( { "level" : log . enum _level . notice , "incident" : incident , "details" : details } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . notice = notice ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function warning ( incident , details ) {
if ( details === void 0 ) { details = { } ; }
add ( { "level" : log . enum _level . warning , "incident" : incident , "details" : details } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . warning = warning ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
function error ( incident , details ) {
if ( details === void 0 ) { details = { } ; }
add ( { "level" : log . enum _level . error , "incident" : incident , "details" : details } ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
log . error = error ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : log « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : log « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : lang « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : log « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var log ;
( function ( log ) {
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
log . conf _push ( [
log . channel _make ( {
"kind" : "stdout" ,
"data" : {
"threshold" : "info"
}
} ) ,
] ) ;
} ) ( log = lib _plankton . log || ( lib _plankton . log = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : code « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
2024-09-11 17:24:20 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : code « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function inverse _encode ( decode , to ) {
return decode ( to ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . inverse _encode = inverse _encode ;
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function inverse _decode ( encode , from ) {
return encode ( from ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . inverse _decode = inverse _decode ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
var class _code _inverse = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _code _inverse ( subject ) {
this . subject = subject ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* @ implementation
* @ author fenris
* /
class _code _inverse . prototype . encode = function ( to ) {
var _this = this ;
return code . inverse _encode ( function ( x ) { return _this . subject . decode ( x ) ; } , to ) ;
} ;
/ * *
* @ implementation
* @ author fenris
* /
class _code _inverse . prototype . decode = function ( from ) {
var _this = this ;
return code . inverse _decode ( function ( x ) { return _this . subject . encode ( x ) ; } , from ) ;
} ;
return class _code _inverse ;
} ( ) ) ;
code . class _code _inverse = class _code _inverse ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function pair _encode ( encode _first , encode _second , from ) {
var between = encode _first ( from ) ;
var to = encode _second ( between ) ;
return to ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . pair _encode = pair _encode ;
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function pair _decode ( decode _first , decode _second , to ) {
var between = decode _second ( to ) ;
var from = decode _first ( between ) ;
return from ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . pair _decode = pair _decode ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
var class _code _pair = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _code _pair ( first , second ) {
this . first = first ;
this . second = second ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* @ implementation
* @ author fenris
* /
class _code _pair . prototype . encode = function ( from ) {
var _this = this ;
return code . pair _encode ( function ( x ) { return _this . first . encode ( x ) ; } , function ( x ) { return _this . second . encode ( x ) ; } , from ) ;
} ;
/ * *
* @ implementation
* @ author fenris
* /
class _code _pair . prototype . decode = function ( to ) {
var _this = this ;
return code . pair _decode ( function ( x ) { return _this . first . decode ( x ) ; } , function ( x ) { return _this . second . decode ( x ) ; } , to ) ;
} ;
return class _code _pair ;
} ( ) ) ;
code . class _code _pair = class _code _pair ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function chain _encode ( encode _links , from ) {
var value = from ;
encode _links
. forEach ( function ( link ) {
value = link ( value ) ;
} ) ;
return value ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . chain _encode = chain _encode ;
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function chain _decode ( decode _links , to ) {
var value = to ;
decode _links
. reverse ( )
. forEach ( function ( link ) {
value = link ( value ) ;
} ) ;
return value ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . chain _decode = chain _decode ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
var class _code _chain = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _code _chain ( links ) {
this . links = links ;
}
/ * *
* @ implementation
* @ author fenris
* /
class _code _chain . prototype . encode = function ( from ) {
return code . chain _encode ( this . links . map ( function ( link ) { return ( function ( x ) { return link . encode ( x ) ; } ) ; } ) , from ) ;
} ;
/ * *
* @ implementation
* @ author fenris
* /
class _code _chain . prototype . decode = function ( to ) {
return code . chain _decode ( this . links . map ( function ( link ) { return ( function ( x ) { return link . decode ( x ) ; } ) ; } ) , to ) ;
} ;
return class _code _chain ;
} ( ) ) ;
code . class _code _chain = class _code _chain ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : code « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var code ;
( function ( code ) {
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author Christian Fraß < frass @ greenscale . de >
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function flatten _encode ( from , keys ) {
if ( keys === void 0 ) { keys = null ; }
if ( keys === null ) {
if ( from . length > 0 ) {
keys = Object . keys ( from [ 0 ] ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
throw ( new Error ( "encoding impossible" ) ) ;
2024-09-12 00:03:29 +02:00
}
}
2024-09-18 18:17:25 +02:00
return {
"keys" : keys ,
"data" : from . map ( function ( line ) { return keys . map ( function ( name ) { return line [ name ] ; } ) ; } )
} ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . flatten _encode = flatten _encode ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author Christian Fraß < frass @ greenscale . de >
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function flatten _decode ( to ) {
return ( to . data
. map ( function ( dataset ) {
var dataset _ = { } ;
dataset
. forEach ( function ( value , index ) {
var name = to . keys [ index ] ;
dataset _ [ name ] = value ;
} ) ;
return dataset _ ;
} ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
code . flatten _decode = flatten _decode ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : code « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : code « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : code « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var code ;
( function ( code ) {
/ * *
* @ author fenris
* /
var class _code _flatten = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _code _flatten ( ) {
}
/ * *
* @ implementation
* @ author fenris
* /
class _code _flatten . prototype . encode = function ( x ) {
return code . flatten _encode ( x ) ;
2024-09-11 17:24:20 +02:00
} ;
2024-09-18 18:17:25 +02:00
/ * *
* @ implementation
* @ author fenris
* /
class _code _flatten . prototype . decode = function ( x ) {
return code . flatten _decode ( x ) ;
} ;
return class _code _flatten ;
} ( ) ) ;
code . class _code _flatten = class _code _flatten ;
} ) ( code = lib _plankton . code || ( lib _plankton . code = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : json « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : json « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : json « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : json « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var json ;
( function ( json ) {
/ * *
* @ author fenris
* /
function encode ( source , options = { } ) {
options = Object . assign ( {
"formatted" : false ,
} , options ) ;
return JSON . stringify ( source , undefined , ( options . formatted ? "\t" : undefined ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
json . encode = encode ;
/ * *
* @ author fenris
* /
function decode ( target ) {
return JSON . parse ( target ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
json . decode = decode ;
/ * *
* @ author fenris
* /
function implementation _code ( ) {
return {
"encode" : x => encode ( x ) ,
"decode" : decode ,
} ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
json . implementation _code = implementation _code ;
} ) ( json = lib _plankton . json || ( lib _plankton . json = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : json « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : json « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : json « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : json « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var json ;
( function ( json ) {
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
class class _json {
/ * *
* @ author fenris
* /
constructor ( ) {
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* @ implementation
* @ author fenris
* /
encode ( x ) {
return json . encode ( x ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* @ implementation
* @ author fenris
* /
decode ( x ) {
return json . decode ( x ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
json . class _json = class _json ;
} ) ( json = lib _plankton . json || ( lib _plankton . json = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : file « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : file « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : file « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : file « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
var file ;
( function ( file ) {
/ * *
* @ author fenris
* /
function exists ( path ) {
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . stat ( path , function ( error , stats ) {
if ( error ) {
resolve ( false ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
resolve ( true ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
} ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
file . exists = exists ;
/ * *
* @ author fenris
* /
function read ( path ) {
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . readFile ( path , {
"encoding" : "utf8" ,
"flag" : "r"
} , function ( error , content ) {
if ( error == null ) {
resolve ( content ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
reject ( error ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
} ) ) ;
}
file . read = read ;
/ * *
* @ author fenris
* /
function read _buffer ( path ) {
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . readFile ( path , {
"flag" : "r"
} , function ( error , content ) {
if ( error == null ) {
resolve ( content ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
file . read _buffer = read _buffer ;
/ * *
* @ author fenris
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function read _stdin ( ) {
return ( new Promise ( function ( resolve , reject ) {
var input _raw = "" ;
process . stdin . setEncoding ( "utf8" ) ;
process . stdin . on ( "readable" , function ( ) {
var chunk ;
while ( ( chunk = process . stdin . read ( ) ) !== null ) {
input _raw += chunk ;
}
} ) ;
process . stdin . on ( "end" , function ( ) {
resolve ( input _raw ) ;
} ) ;
} ) ) ;
}
file . read _stdin = read _stdin ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function write ( path , content , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"encoding" : "utf-8"
} , options ) ;
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . writeFile ( path , content , {
"encoding" : options . encoding ,
"flag" : "w"
} , function ( error ) {
if ( error == null ) {
resolve ( undefined ) ;
}
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
}
file . write = write ;
/ * *
* @ author fenris
* /
function write _buffer ( path , content , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( { } , options ) ;
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . writeFile ( path , content , {
"flag" : "w"
} , function ( error ) {
if ( error == null ) {
resolve ( undefined ) ;
}
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
}
file . write _buffer = write _buffer ;
/ * *
* /
function delete _ ( path ) {
var nm _fs = require ( "fs" ) ;
return ( new Promise ( function ( resolve , reject ) {
nm _fs . unlink ( path , function ( ) {
resolve ( undefined ) ;
} ) ;
} ) ) ;
}
file . delete _ = delete _ ;
} ) ( file = lib _plankton . file || ( lib _plankton . file = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : object « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : object « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : object « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : object « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var object ;
( function ( object _1 ) {
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
* @ deprecated use the "??" operator instead
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function fetch ( object , fieldname , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"fallback" : null ,
"escalation" : 1
} , options ) ;
if ( ( fieldname in object )
&&
( object [ fieldname ] !== undefined ) ) {
return object [ fieldname ] ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
if ( ! options . escalate ) {
return options . fallback ;
2024-09-11 17:24:20 +02:00
}
else {
2024-09-18 18:17:25 +02:00
throw ( new Error ( "field '" + fieldname + "' not in structure" ) ) ;
}
}
}
object _1 . fetch = fetch ;
/ * *
* /
function map ( object _from , transformator ) {
return ( Object . fromEntries ( Object . entries ( object _from )
. map ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
return ( [ key , transformator ( value , key ) ] ) ;
} ) ) ) ;
}
object _1 . map = map ;
/ * *
* gibt ein Objekt mit bestimmten Einträgen des Eingabe - Objekts zurück
* /
function filter ( object _from , predicate ) {
return ( Object . fromEntries ( Object . entries ( object _from )
. filter ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
return predicate ( value , key ) ;
} ) ) ) ;
}
object _1 . filter = filter ;
/ * *
* wandelt ein Array mit Einträgen der Form { key , value } in ein entsprechendes Objekt um
*
* @ deprecated use Object . fromEntries instead !
* /
function from _array ( array ) {
return ( Object . fromEntries ( array
. map ( function ( _a ) {
var key = _a [ "key" ] , value = _a [ "value" ] ;
return ( [ key , value ] ) ;
} ) ) ) ;
}
object _1 . from _array = from _array ;
/ * *
* wandelt ein Objekt in ein entsprechendes Array mit Einträgen der Form { key , value } um
*
* @ deprecated use Object . entries insetad !
* /
function to _array ( object ) {
return ( Object . entries ( object )
. map ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
return ( { "key" : key , "value" : value } ) ;
} ) ) ;
}
object _1 . to _array = to _array ;
/ * *
* gibt eine Liste von Schlüsseln eines Objekts zurück
*
* @ deprecated use Object . keys instead !
* /
function keys ( object ) {
return Object . keys ( object ) ;
}
object _1 . keys = keys ;
/ * *
* gibt eine Liste von Werten eines Objekts zurück
*
* @ deprecated use Object . values instead !
* /
function values ( object ) {
return Object . values ( object ) ;
}
object _1 . values = values ;
/ * *
* liest ein Baum - artiges Objekt an einer bestimmten Stelle aus
* /
function path _read ( object , path , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"fallback" : null ,
"escalate" : false
} , options ) ;
var steps = ( ( path . length == 0 ) ? [ ] : path . split ( "." ) ) ;
if ( steps . length == 0 ) {
throw ( new Error ( "empty path" ) ) ;
}
else {
var position _1 = object ;
var reachable = ( ( position _1 != null )
&&
( steps . slice ( 0 , steps . length - 1 )
. every ( function ( step ) {
position _1 = lib _plankton . object . fetch ( position _1 , step , {
"fallback" : null ,
"escalate" : false
} ) ;
return ( position _1 != null ) ;
} ) ) ) ;
if ( reachable ) {
return lib _plankton . object . fetch ( position _1 , steps [ steps . length - 1 ] , {
"fallback" : options . fallback ,
"escalate" : options . escalate
} ) ;
}
else {
return lib _plankton . object . fetch ( { } , "_dummy_" , {
"fallback" : options . fallback ,
"escalate" : options . escalate
} ) ;
}
}
}
object _1 . path _read = path _read ;
/ * *
* schreibt einen Wert an eine bestimmte Stelle in einem Baum - artigen Objekt
* /
function path _write ( object , path , value , construct ) {
if ( construct === void 0 ) { construct = true ; }
var steps = ( ( path . length == 0 ) ? [ ] : path . split ( "." ) ) ;
if ( steps . length == 0 ) {
throw ( new Error ( "empty path" ) ) ;
}
else {
var position _2 = object ;
var reachable = steps . slice ( 0 , steps . length - 1 ) . every ( function ( step ) {
var position _ = lib _plankton . object . fetch ( position _2 , step , {
"fallback" : null ,
"escalate" : false
} ) ;
if ( position _ == null ) {
if ( construct ) {
position _2 [ step ] = { } ;
position _2 = position _2 [ step ] ;
return true ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
return false ;
2024-09-11 17:24:20 +02:00
}
}
2024-09-18 18:17:25 +02:00
else {
position _2 = position _ ;
return true ;
}
} ) ;
if ( reachable ) {
position _2 [ steps [ steps . length - 1 ] ] = value ;
}
else {
throw ( new Error ( "path '" + path + "' does not exist and may not be constructed" ) ) ;
2024-09-11 17:24:20 +02:00
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . path _write = path _write ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* prüft ob ein Objekt einem bestimmten Muster entspricht
*
* @ deprecated not very useful
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function matches ( object , pattern , options ) {
if ( options === void 0 ) { options = { } ; }
2024-09-12 00:03:29 +02:00
options = Object . assign ( {
2024-09-18 18:17:25 +02:00
"collate" : instance _collate
2024-09-12 00:03:29 +02:00
} , options ) ;
2024-09-18 18:17:25 +02:00
return ( Object . entries ( pattern )
. every ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
return options . collate ( value , object [ key ] ) ;
} ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . matches = matches ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* erzeugt eine Projektion eines Baum - artigen Objekts in ein Listen - artiges Objekt
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function flatten ( value , options ) {
if ( options === void 0 ) { options = { } ; }
2024-09-12 00:03:29 +02:00
options = Object . assign ( {
2024-09-18 18:17:25 +02:00
"separator" : "." ,
"key_for_array_element" : ( function ( index ) { return ( "element_" + index . toFixed ( 0 ) ) ; } )
2024-09-12 00:03:29 +02:00
} , options ) ;
2024-09-18 18:17:25 +02:00
var integrate = function ( result , key , value ) {
if ( value == null ) {
result [ key ] = value ;
}
else {
// primitive Werte direkt übernehmen
if ( typeof ( value ) != "object" ) {
result [ key ] = value ;
}
// sonst durch rekursiven Aufruf die flache Variante des Wertes ermitteln und einarbeiten
else {
var result _ = flatten ( value , {
"separator" : options . separator ,
"key_for_array_element" : options . key _for _array _element
} ) ;
Object . entries ( result _ ) . forEach ( function ( _a ) {
var key _ = _a [ 0 ] , value _ = _a [ 1 ] ;
result [ ( key + options . separator + key _ ) ] = value _ ;
} ) ;
}
}
2024-09-12 00:03:29 +02:00
} ;
2024-09-18 18:17:25 +02:00
if ( ( value === null )
||
( value === undefined ) ) {
return null ;
}
else {
var result _1 = { } ;
if ( typeof ( value ) != "object" ) {
result _1 [ "value" ] = value ;
}
else {
if ( value instanceof Array ) {
value . forEach ( function ( element , index ) {
integrate ( result _1 , options . key _for _array _element ( index ) , element ) ;
} ) ;
}
else {
Object . entries ( value ) . forEach ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
integrate ( result _1 , key , value ) ;
} ) ;
}
}
return result _1 ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . flatten = flatten ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ deprecated use Object . assign instead !
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function clash ( x , y , options ) {
if ( options === void 0 ) { options = { } ; }
2024-09-12 00:03:29 +02:00
options = Object . assign ( {
2024-09-18 18:17:25 +02:00
"overwrite" : true ,
"hooks" : {
"existing" : function ( key , value _old , value _new ) {
lib _plankton . log . warning ( "object_clash_field_already_defined" , {
"key" : key
} ) ;
}
}
2024-09-12 00:03:29 +02:00
} , options ) ;
2024-09-18 18:17:25 +02:00
var z = { } ;
Object . keys ( x ) . forEach ( function ( key ) {
z [ key ] = x [ key ] ;
} ) ;
Object . keys ( y ) . forEach ( function ( key ) {
if ( key in z ) {
if ( options . hooks . existing != null ) {
options . hooks . existing ( key , z [ key ] , y [ key ] ) ;
}
if ( options . overwrite ) {
z [ key ] = y [ key ] ;
}
}
else {
z [ key ] = y [ key ] ;
}
} ) ;
return z ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . clash = clash ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ deprecated use Object . assign instead !
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function patch ( core , mantle , options ) {
if ( options === void 0 ) { options = { } ; }
2024-09-12 00:03:29 +02:00
options = Object . assign ( {
2024-09-18 18:17:25 +02:00
"deep" : true ,
"path" : null
2024-09-12 00:03:29 +02:00
} , options ) ;
2024-09-18 18:17:25 +02:00
if ( mantle == null ) {
lib _plankton . log . warning ( "object_patch_mantle_is_null" , {
"core" : core
} ) ;
}
else {
Object . keys ( mantle ) . forEach ( function ( key ) {
var path _ = ( ( options . path == null )
?
key
:
( options . path + "." + key ) ) ;
var value _mantle = mantle [ key ] ;
if ( ! ( key in core ) ) {
if ( ( typeof ( value _mantle ) == "object" )
&&
( value _mantle != null )
&&
options . deep ) {
if ( value _mantle instanceof Array ) {
core [ key ] = [ ] ;
value _mantle . forEach ( function ( element ) {
if ( ( typeof ( element ) == "object" )
&&
( element != null ) ) {
var element _ = { } ;
patch ( element _ , element ) ;
core [ key ] . push ( element _ ) ;
}
else {
core [ key ] . push ( element ) ;
}
} ) ;
}
else {
core [ key ] = { } ;
patch ( core [ key ] , value _mantle , {
"deep" : options . deep ,
"path" : path _
} ) ;
}
}
else {
core [ key ] = value _mantle ;
}
}
else {
var value _core = core [ key ] ;
if ( typeof ( value _core ) == typeof ( value _mantle ) ) {
if ( ( typeof ( value _mantle ) == "object" )
&&
( value _mantle != null )
&&
options . deep ) {
patch ( core [ key ] , value _mantle , {
"deep" : options . deep ,
"path" : path _
} ) ;
}
else {
core [ key ] = value _mantle ;
}
}
else {
if ( ( value _core != null )
&&
( value _mantle != null ) ) {
lib _plankton . log . warning ( "object_path_different_shapes" , {
"path" : path _ ,
"core_type" : typeof ( value _core ) ,
"mantle_type" : typeof ( value _mantle )
} ) ;
}
core [ key ] = value _mantle ;
// throw (new Error(message));
}
}
} ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . patch = patch ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ deprecated use Object . assign instead !
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function patched ( core , mantle , options ) {
if ( options === void 0 ) { options = { } ; }
2024-09-12 00:03:29 +02:00
options = Object . assign ( {
2024-09-18 18:17:25 +02:00
"deep" : true
2024-09-12 00:03:29 +02:00
} , options ) ;
2024-09-18 18:17:25 +02:00
var result = { } ;
patch ( result , core , { "deep" : options . deep } ) ;
patch ( result , mantle , { "deep" : options . deep } ) ;
return result ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
object _1 . patched = patched ;
/ * *
* @ deprecated use Object . assign instead !
* /
function attached ( object , key , value ) {
var mantle = { } ;
mantle [ key ] = value ;
return patched ( object , mantle , { "deep" : false } ) ;
}
object _1 . attached = attached ;
/ * *
* @ author fenris
* /
function copy ( object ) {
return patched ( { } , object ) ;
}
object _1 . copy = copy ;
} ) ( object = lib _plankton . object || ( lib _plankton . object = { } ) ) ;
2024-09-11 17:24:20 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : pair « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : pair « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : pair « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : pair « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : pair « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : pair « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : pair « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : pair « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var pair ;
( function ( pair _1 ) {
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function swap ( pair ) {
2024-09-11 17:24:20 +02:00
return {
2024-09-18 18:17:25 +02:00
"first" : pair . second ,
"second" : pair . first
2024-09-11 17:24:20 +02:00
} ;
}
2024-09-18 18:17:25 +02:00
pair _1 . swap = swap ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function show ( pair , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"show_first" : instance _show ,
"show_second" : instance _show
} , options ) ;
return ( "("
+
options . show _first ( pair . first )
+
","
+
options . show _second ( pair . second )
+
")" ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
pair _1 . show = show ;
} ) ( pair = lib _plankton . pair || ( lib _plankton . pair = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
"use strict" ;
/ *
This file is part of » bacterio - plankton : list « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : list « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : list « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : list « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : list « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : list « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : list « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : list « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var list ;
( function ( list _1 ) {
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a certain list of integer numbers
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function range ( from , to , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"step" : 1
} , options ) ;
var list = [ ] ;
for ( var value = from ; value <= to ; value += options . step ) {
list . push ( value ) ;
}
return list ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . range = range ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a certain list of consecutiv integer numbers , beginning with 0
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function sequence ( length ) {
return range ( 0 , length - 1 ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . sequence = sequence ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function from _iterator ( iterator ) {
var list = [ ] ;
// @ts-ignore
for ( var _i = 0 , iterator _1 = iterator ; _i < iterator _1 . length ; _i ++ ) {
var element = iterator _1 [ _i ] ;
list . push ( element ) ;
}
return list ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . from _iterator = from _iterator ;
2024-09-11 17:24:20 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function is _empty ( list ) {
return ( list . length <= 0 ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . is _empty = is _empty ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* combines two lists into one
*
* @ param { boolean } [ options . cut ] whether the result list will be as long as the shortest input list or an exception is thrown if they have different lengths ; default : true
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function zip ( list _first , list _second , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"cut" : true
} , options ) ;
var empty _first = is _empty ( list _first ) ;
var empty _second = is _empty ( list _second ) ;
if ( empty _first || empty _second ) {
if ( options . cut || ( empty _first && empty _second ) ) {
return [ ] ;
}
else {
throw ( new Error ( "lists have different lengths" ) ) ;
}
}
else {
return ( [ { "first" : list _first [ 0 ] , "second" : list _second [ 0 ] } ]
. concat ( zip ( list _first . slice ( 1 ) , list _second . slice ( 1 ) , {
"cut" : options . cut
} ) ) ) ;
}
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . zip = zip ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* checks whether two lists are equal
*
* @ todo define common function "equals" and default predicate to
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function equals ( list1 , list2 , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"collate_element" : instance _collate
} , options ) ;
if ( list1 . length == list2 . length ) {
return ( zip ( list1 , list2 , { "cut" : true } )
. every ( function ( pair ) { return options . collate _element ( pair . first , pair . second ) ; } ) ) ;
}
else {
return false ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . equals = equals ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* creates a list with the elements from the input list , which fulfil a certain predicate ( ~ filter )
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function keep ( list , predicate ) {
return ( list
. filter ( function ( element , index ) { return predicate ( element ) ; } ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . keep = keep ;
2024-09-11 17:24:20 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* creates a list with the elements from the input list , which do not fulfil a certain predicate ( ~ dual filter )
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function drop ( list , predicate ) {
return ( list
. filter ( function ( element , index ) { return ( ! predicate ( element ) ) ; } ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . drop = drop ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function filter _inplace ( list , predicate ) {
var index = 0 ;
while ( index < list . length ) {
var element = list [ index ] ;
if ( predicate ( element ) ) {
index += 1 ;
}
else {
list . splice ( index , 1 ) ;
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . filter _inplace = filter _inplace ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a list with no duplicates ( like unix ' "unique" )
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function cleaned ( list , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"collate_element" : instance _collate
} , options ) ;
var list _ = [ ] ;
list . forEach ( function ( element ) {
if ( ! list _ . some ( function ( element _ ) { return options . collate _element ( element , element _ ) ; } ) ) {
list _ . push ( element ) ;
}
else {
// do nothing
}
2024-09-12 00:03:29 +02:00
} ) ;
2024-09-18 18:17:25 +02:00
return list _ ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . cleaned = cleaned ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* creates a binary partition of the list according to a given predicate
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function separate ( list , predicate ) {
return ( list
. reduce ( function ( seperation , element ) {
return ( predicate ( element )
? { "yes" : seperation . yes . concat ( [ element ] ) , "no" : seperation [ "no" ] }
: { "yes" : seperation . yes , "no" : seperation [ "no" ] . concat ( [ element ] ) } ) ;
} , { "yes" : [ ] , "no" : [ ] } ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . separate = separate ;
;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function clone ( list ) {
return keep ( list , function ( x ) { return true ; } ) ;
}
list _1 . clone = clone ;
/ * *
* /
function reversed ( list ) {
var list _ = clone ( list ) ;
list _ . reverse ( ) ;
return list _ ;
}
list _1 . reversed = reversed ;
/ * *
* @ todo use Array . toSorted ?
* /
function sorted ( list , options ) {
options = Object . assign ( {
"compare_element" : instance _compare
} , options ) ;
var list _ = clone ( list ) ;
list _ . sort ( function ( x , y ) { return ( options . compare _element ( x , y ) ? - 1 : + 1 ) ; } ) ;
return list _ ;
}
list _1 . sorted = sorted ;
/ * *
* die Liste in gleich große Blöcke zerlegen
* /
function chop ( list , chunk _size ) {
var chunks = [ ] ;
var index = 0 ;
while ( index < list . length ) {
var chunk = list . slice ( index , Math . min ( list . length , index + chunk _size ) ) ;
index += chunk _size ;
chunks . push ( chunk ) ;
}
return chunks ;
}
list _1 . chop = chop ;
/ * *
* /
function group ( list , collate _element ) {
var result = [ ] ;
list . forEach ( function ( element ) {
var target = result . find (
// @ts-ignore
function ( group ) { return collate _element ( group [ 0 ] , element ) ; } ) ;
if ( target === undefined ) {
target = [ ] ;
result . push ( target ) ;
}
target . push ( element ) ;
2024-09-12 00:03:29 +02:00
} ) ;
2024-09-18 18:17:25 +02:00
return result ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . group = group ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function has ( list , predicate ) {
return ( list . find ( predicate ) !== undefined ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . has = has ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ deprecate use Array . includes or Array . some
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function contains ( list , element , options ) {
options = Object . assign ( {
"collate" : instance _collate
} , options ) ;
return has ( list , function ( element _ ) { return options . collate _element ( element _ , element ) ; } ) ;
}
list _1 . contains = contains ;
/ * *
* retrieves the element and its index of the list , which has the maximum value
* /
function max ( list , target _function , options ) {
options = Object . assign ( {
"compare_value" : instance _compare
} , options ) ;
if ( is _empty ( list ) ) {
throw ( new Error ( "the max-arg of an empty list is not defined" ) ) ;
}
else {
return ( list
. reduce ( function ( result , element , index ) {
var value = target _function ( element ) ;
if ( ( result == null )
||
( ! options . compare _value ( value , result . value ) ) ) {
return { "index" : index , "element" : element , "value" : value } ;
}
else {
return result ;
}
} , null ) ) ;
}
}
list _1 . max = max ;
/ * *
* retrieves the element and its index of the list , which has the mininum value
* /
function min ( list , target _function , options ) {
options = Object . assign ( {
"compare_value" : instance _compare
} , options ) ;
return max ( list , target _function , {
"compare_value" : function ( x , y ) { return options . compare _value ( y , x ) ; }
2024-09-12 00:03:29 +02:00
} ) ;
}
2024-09-18 18:17:25 +02:00
list _1 . min = min ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* implements the idea of arithmetic distribution like in "(a+b)·(c+d) = (a·c)+(a·d)+(b·c)+(b·d)"
* example : distribute ( [ [ 1 , 2 ] , [ 3 ] , [ 4 , 5 , 6 ] ] ) = [ [ 1 , 3 , 4 ] , [ 1 , 3 , 5 ] , [ 1 , 3 , 6 ] , [ 2 , 3 , 4 ] , [ 2 , 3 , 5 ] , [ 2 , 3 , 6 ] ]
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function distribute ( lists ) {
if ( is _empty ( lists ) ) {
return [ [ ] ] ;
}
else {
var subresult _1 = distribute ( lists . slice ( 1 ) ) ;
return ( lists [ 0 ]
. map ( function ( element ) { return subresult _1 . map ( function ( list ) { return [ element ] . concat ( list ) ; } ) ; } )
. reduce ( function ( x , y ) { return x . concat ( y ) ; } , [ ] ) ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . distribute = distribute ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function contrast ( list _left , extract _key _left , list _right , extract _key _right ) {
var gathering = { } ;
list _left . forEach ( function ( source _left ) {
var _a ;
var key = extract _key _left ( source _left ) ;
gathering [ key ] = Object . assign ( ( ( _a = gathering [ key ] ) !== null && _a !== void 0 ? _a : { } ) , { "left" : source _left } ) ;
} ) ;
list _right . forEach ( function ( source _right ) {
var _a ;
var key = extract _key _right ( source _right ) ;
gathering [ key ] = Object . assign ( ( ( _a = gathering [ key ] ) !== null && _a !== void 0 ? _a : { } ) , { "right" : source _right } ) ;
} ) ;
var result = {
"both" : [ ] ,
"only_left" : [ ] ,
"only_right" : [ ]
2024-09-12 00:03:29 +02:00
} ;
2024-09-18 18:17:25 +02:00
Object . entries ( gathering ) . forEach ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
if ( "left" in value ) {
if ( "right" in value ) {
result . both . push ( { "key" : key , "left" : value . left , "right" : value . right } ) ;
}
else {
result . only _left . push ( { "key" : key , "left" : value . left } ) ;
}
}
else {
if ( "right" in value ) {
result . only _right . push ( { "key" : key , "right" : value . right } ) ;
}
else {
// impossible
// do nothing
}
}
} ) ;
return result ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
list _1 . contrast = contrast ;
} ) ( list = lib _plankton . list || ( lib _plankton . list = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : conf « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : conf « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : conf « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : conf « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : conf « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : conf « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : conf « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : conf « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var conf ;
( function ( conf ) {
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function adapt _primitive ( schema , value ) {
if ( value === undefined ) {
if ( ! ( "default" in schema ) ) {
return {
"reports" : [
{
"incident" : "neither explicit value provided nor default value specified" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( schema . default ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
else {
if ( value === null ) {
if ( ( "nullable" in schema )
&&
( ! schema . nullable ) ) {
return {
"reports" : [
{
"incident" : "null not allowed" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( null ) ,
} ;
}
2024-09-11 17:24:20 +02:00
}
else {
2024-09-18 18:17:25 +02:00
if ( ( "enum" in schema )
&&
( ! schema . enum . includes ( value ) ) ) {
return {
"reports" : [
{
"incident" : "provided value not in enumeration of valid values" ,
"details" : {
"enumerated_values" : schema . enum ,
"provided_value" : value ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
const type _map = {
"boolean" : "boolean" ,
"integer" : "number" ,
"number" : "number" ,
"string" : "string" ,
} ;
// @ts-ignore
if ( ! ( typeof ( value ) === type _map [ schema . type ] ) ) {
return {
"reports" : [
{
// @ts-ignore
"incident" : ( "value should be " + type _map [ schema . type ] ) ,
"details" : {
"provided_value" : value ,
"type" : typeof ( value ) ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( value )
} ;
}
}
2024-09-11 17:24:20 +02:00
}
}
}
/ * *
2024-09-18 18:17:25 +02:00
* @ todo anyOf
* @ todo allOf
* @ todo oneOf
* @ todo not
* @ todo tests
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function adapt ( schema , value _raw ) {
let value = value _raw ;
if ( ! ( "type" in schema ) ) {
if ( "anyOf" in schema ) {
if ( value === undefined ) {
if ( ! ( "default" in schema ) ) {
return {
"reports" : [
{
"incident" : "neither explicit value provided nor default value specified" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
value = schema . default ;
}
}
else {
// do nothing
}
const sub _adaptions = ( schema . anyOf
. map ( ( sub _schema ) => adapt ( sub _schema , value ) ) ) ;
const valid _sub _adaptions = ( sub _adaptions
. filter ( ( sub _adaption ) => lib _plankton . pod . is _filled ( sub _adaption . result ) ) ) ;
if ( valid _sub _adaptions . length <= 0 ) {
return {
"reports" : [
{
"incident" : "no valid apaptions" ,
"details" : {
"sub_adaptions" : sub _adaptions ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
if ( valid _sub _adaptions . length > 1 ) {
return {
"reports" : [
{
"incident" : "multiple valid apaptions" ,
"details" : {
"valid_sub_adaptions" : valid _sub _adaptions ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
return valid _sub _adaptions [ 0 ] ;
}
}
2024-09-11 17:24:20 +02:00
}
else {
2024-09-18 18:17:25 +02:00
throw ( new Error ( "not implemented" ) ) ;
}
}
else {
switch ( schema . type ) {
case "boolean" :
return adapt _primitive ( schema , value ) ;
break ;
case "integer" :
return adapt _primitive ( schema , value ) ;
break ;
case "number" :
return adapt _primitive ( schema , value ) ;
break ;
case "string" : {
return adapt _primitive ( schema , value ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "array" : {
if ( value === undefined ) {
if ( ! ( "default" in schema ) ) {
return {
"reports" : [
{
"incident" : "neither explicit value provided nor default value specified" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
value = schema . default ;
}
}
/*else*/ {
if ( value === null ) {
if ( ( "nullable" in schema )
&&
( ! schema . nullable ) ) {
return {
"reports" : [
{
"incident" : "null not allowed" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( null ) ,
} ;
}
}
else {
/ * i f (
( "enum" in schema )
&&
( ! schema . enum . includes ( value ) ) // TODO
) {
return {
"reports" : [
{
"incident" : "provided value not in enumeration of valid values" ,
"details" : {
"enumerated_values" : schema . enum ,
"provided_value" : value ,
}
}
] ,
"result" : lib _plankton . pod . make _empty < any > ( ) ,
} ;
}
else * / {
if ( ( ! ( typeof ( value ) === "object" ) )
&&
( value . constructor . name !== "Array" ) ) {
return {
"reports" : [
{
"incident" : "value should be array" ,
"details" : {
"provided_value" : value ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
if ( ! ( "items" in schema ) ) {
// do nothing
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( value ) ,
} ;
}
else {
let reports = [ ] ;
for ( let index = 0 ; index < value . length ; index += 1 ) {
const adaption = adapt ( schema . items , value [ index ] ) ;
if ( ! lib _plankton . pod . is _filled ( adaption . result ) ) {
reports = reports . concat ( adaption . reports . map ( ( report _entry ) => ( {
"incident" : report _entry . incident ,
"details" : Object . assign ( report _entry . details , {
"path" : ( report _entry . details . path ? ? [ ] ) . concat ( [ index ] )
} ) ,
} ) ) ) ;
}
else {
value [ index ] = lib _plankton . pod . cull ( adaption . result ) ;
}
}
return {
"reports" : reports ,
"result" : ( ( reports . length > 0 )
?
lib _plankton . pod . make _empty ( )
:
lib _plankton . pod . make _filled ( value ) ) ,
} ;
}
}
}
}
}
break ;
}
case "object" : {
if ( value === undefined ) {
if ( ! ( "default" in schema ) ) {
return {
"reports" : [
{
"incident" : "neither explicit value provided nor default value specified" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
value = schema . default ;
}
}
/*else*/ {
if ( value === null ) {
if ( ( "nullable" in schema )
&&
( ! schema . nullable ) ) {
return {
"reports" : [
{
"incident" : "null not allowed" ,
"details" : { }
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
return {
"reports" : [ ] ,
"result" : lib _plankton . pod . make _filled ( null ) ,
} ;
}
}
else {
/ * i f (
( "enum" in schema )
&&
( ! schema . enum . includes ( value ) ) // TODO
) {
return {
"reports" : [
{
"incident" : "provided value not in enumeration of valid values" ,
"details" : {
"enumerated_values" : schema . enum ,
"provided_value" : value ,
}
}
] ,
"result" : lib _plankton . pod . make _empty < any > ( ) ,
} ;
}
else * / {
if ( ! ( typeof ( value ) === "object" ) ) {
return {
"reports" : [
{
"incident" : "value should be object" ,
"details" : {
"provided_value" : value ,
}
}
] ,
"result" : lib _plankton . pod . make _empty ( ) ,
} ;
}
else {
value = lib _plankton . object . copy ( value ) ;
const contrast = lib _plankton . list . contrast ( Object . keys ( schema . properties ) , x => x , Object . keys ( value ) , x => x ) ;
let reports = [ ] ;
// gratuitous fields
{
if ( contrast . only _right . length <= 0 ) {
// do nothing
}
else {
const additional _properties = ( schema . additionalProperties ? ? false ) ;
if ( additional _properties === false ) {
reports = reports . concat ( contrast . only _right
. map ( ( entry ) => ( {
"incident" : "gratuitous field" ,
"details" : {
"path" : [ entry . right ] ,
}
} ) ) ) ;
}
else {
contrast . only _right . forEach ( ( entry ) => {
const sub _adaption = adapt ( additional _properties , value [ entry . right ] ) ;
if ( ! lib _plankton . pod . is _filled ( sub _adaption . result ) ) {
reports = reports . concat ( sub _adaption . reports
. map ( ( report _entry ) => ( {
"incident" : report _entry . incident ,
"details" : Object . assign ( report _entry . details , {
"path" : ( report _entry . details . path ? ? [ ] ) . concat ( [ entry . right ] )
} ) ,
} ) ) ) ;
}
else {
value [ entry . right ] = lib _plankton . pod . cull ( sub _adaption . result ) ;
}
} ) ;
}
}
}
// missing fields
{
contrast . only _left . forEach ( ( entry ) => {
if ( ( "required" in schema )
&&
schema . required . includes ( entry . left ) ) {
reports . push ( {
"incident" : "missing field" ,
"details" : {
"path" : [ entry . left ] ,
}
} ) ;
}
else {
const sub _adaption = adapt ( schema . properties [ entry . left ] , undefined ) ;
if ( ! lib _plankton . pod . is _filled ( sub _adaption . result ) ) {
reports = reports . concat ( sub _adaption . reports
. map ( ( report _entry ) => ( {
"incident" : report _entry . incident ,
"details" : Object . assign ( report _entry . details , {
"path" : ( report _entry . details . path ? ? [ ] ) . concat ( [ entry . left ] )
} ) ,
} ) ) ) ;
}
else {
value [ entry . left ] = lib _plankton . pod . cull ( sub _adaption . result ) ;
}
}
} ) ;
// regular fields
{
contrast . both . forEach ( ( entry ) => {
const sub _adaption = adapt ( schema . properties [ entry . left ] , value [ entry . right ] ) ;
if ( ! lib _plankton . pod . is _filled ( sub _adaption . result ) ) {
reports = reports . concat ( sub _adaption . reports
. map ( ( report _entry ) => ( {
"incident" : report _entry . incident ,
"details" : Object . assign ( report _entry . details , {
"path" : ( report _entry . details . path ? ? [ ] ) . concat ( [ entry . right ] )
} ) ,
} ) ) ) ;
}
else {
value [ entry . right ] = lib _plankton . pod . cull ( sub _adaption . result ) ;
}
} ) ;
}
return {
"reports" : reports ,
"result" : ( ( reports . length > 0 )
?
lib _plankton . pod . make _empty ( )
:
lib _plankton . pod . make _filled ( value ) ) ,
} ;
}
}
}
}
}
break ;
}
default : {
throw ( new Error ( "unhandled schema type: " + schema . type ) ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
}
2024-09-11 17:24:20 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ todo versioning
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
function refine ( schema , value _raw ) {
const adaption = adapt ( schema , value _raw ) ;
if ( ! lib _plankton . pod . is _filled ( adaption . result ) ) {
throw ( new Error ( "conf could not be loaded:\n"
+
( adaption . reports
. map ( ( report ) => ( "- " + report . incident + " | " + JSON . stringify ( report . details , undefined , "\t" ) ) )
. join ( "\n" ) ) ) ) ;
}
else {
return lib _plankton . pod . cull ( adaption . result ) ;
}
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
conf . refine = refine ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function load ( schema , path ) {
return ( ( ( path === null )
?
Promise . resolve ( undefined )
:
( lib _plankton . file . read ( path )
. then ( ( content ) => Promise . resolve ( lib _plankton . json . decode ( content ) ) ) ) )
. then ( ( data _raw ) => Promise . resolve ( refine ( schema , data _raw ) ) ) ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-18 18:17:25 +02:00
conf . load = load ;
} ) ( conf = lib _plankton . conf || ( lib _plankton . conf = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : string « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : string « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
var plain _text _to _html = function ( text ) {
let ret = text ;
ret = ret . replace ( / /g , " " ) ; // convert multiple whitespace to forced ones
ret = ret . split ( "\n" ) . join ( "<br/>" ) ;
return ret ;
} ;
/ * *
* @ desc makes a valid
2024-09-11 17:24:20 +02:00
* /
2024-09-18 18:17:25 +02:00
var format _sentence = function ( str , rtl = false , caseSense = true ) {
if ( str === "" ) {
return str ;
}
else {
let marks = {
"." : true ,
"?" : true ,
"!" : true
} ;
let default _mark = "." ;
let ret = str . split ( "" ) ;
if ( ! rtl ) {
ret [ 0 ] = ret [ 0 ] . toLocaleUpperCase ( ) ;
if ( ! ( ret [ ret . length - 1 ] in marks ) ) {
ret . push ( default _mark ) ;
2024-09-11 17:24:20 +02:00
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
ret [ ret . length - 1 ] = ret [ ret . length - 1 ] . toLocaleUpperCase ( ) ;
if ( ! ( ret [ 0 ] in marks ) ) {
ret . unshift ( default _mark ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return ret . join ( "" ) ;
}
} ;
var fill _string _template = function ( template _string , object , fabric = function ( object , key ) { return object [ key ] ; } , delimiter = "%" , default _string = null , sloppy ) {
function get _tags ( str ) {
let r = new RegExp ( delimiter + "[^\\s^" + delimiter + "]+" + delimiter , "gi" ) ;
return ( ( str . match ( r ) || [ ] ) . map ( function ( e ) {
return e . slice ( delimiter . length , e . length - delimiter . length ) ;
} ) ) ;
}
function replace _tag ( str , tag , value ) {
let r = new RegExp ( delimiter + tag + delimiter , "gi" ) ;
return str . replace ( r , value ) ;
}
function replace _tags ( str , obj ) {
return ( get _tags ( str ) . reduce ( function ( ret , key ) {
let value = "" ;
try {
value = fabric ( obj , key ) ;
if ( ( ! sloppy && ( value === void 0 ) ) || ( sloppy && ( value == void 0 ) ) ) {
value = default _string ;
}
}
catch ( e ) {
console . warn ( "invalid placeholder " + key ) ;
value = default _string ;
}
return replace _tag ( ret , key , value ) ;
} , str ) ) ;
}
return replace _tags ( template _string , object ) ;
} ;
var make _string _template = function ( _template , _fabrics = { } ) {
function replace _tag ( str , tag , value ) {
var r = new RegExp ( "%" + tag + "%" , "gi" ) ;
return str . replace ( r , value ) ;
}
function replace _tags ( str , obj ) {
return ( Object . keys ( obj ) . reduce ( function ( ret , key ) {
return replace _tag ( ret , key , _fabrics [ key ] || obj [ key ] ) ;
} , str ) ) ;
}
return ( function ( tags ) {
return replace _tags ( _template , tags ) ;
} ) ;
} ;
var make _eml _header = ( function ( ) {
let _template = "" ;
_template += "From: %from%\n" ;
_template += "To: %recipient%\n" ;
_template += "Subject: %subject%\n" ;
_template += "X-Mailer: greenscale-plankton.emlgen\n" ;
return make _string _template ( _template ) ;
} ) ( ) ;
var make _eml _body = ( function ( ) {
let exports = { } ;
exports [ "simple_body" ] = make _string _template ( "Content-Type: %contenttype%\n\n%body%\n\n" ) ;
// very basic implementation
// parts = [{contenttype:"text/html; charset=UTF-8", body: "<h1>foo</h1>" }, {...}]
exports [ "body_boundrary" ] = function ( parts , boundrary ) {
let _template = "" ;
_template += "--%boundrary%\n" ;
_template += "Content-Type: %contenttype%\n\n%body%\n\n" ;
//_template += "--%boundrary%--\n\n";
let maker = make _string _template ( _template ) ;
return ( parts . reduce ( function ( prev , curr ) {
curr . boundrary = boundrary ;
return [ prev , maker ( curr ) ] . join ( "" ) ;
} , "" ) ) ;
} ;
// body must be base64 encoded!
exports [ "attachment_boundrary" ] = function ( parts , boundrary ) {
let _template = "" ;
_template += "--%boundrary%\n" ;
_template += "Content-Type: %contenttype%\n" ;
_template += "Content-Transfer-Encoding: base64\n" ;
_template += "Content-Disposition: %disposition%; filename=\"%name%\"\n\n" ;
_template += "%body%\n\n" ;
//_template += "--%boundrary%--\n\n";
let maker = make _string _template ( _template ) ;
return ( parts . reduce ( function ( prev , curr ) {
curr . boundrary = boundrary ;
if ( curr . disposition === void 0 )
curr . disposition = "inline" ;
return [ prev , maker ( curr ) ] . join ( "" ) ;
} , "" ) ) ;
} ;
exports [ "gen_boundrary" ] = function ( ) {
return ( "xxxxxxxxxxxxxxxxxxxxxx" . replace ( /[xy]/g , function ( c ) {
let r = crypto . getRandomValues ( new Uint8Array ( 1 ) ) [ 0 ] % 16 | 0 , v = c == "x" ? r : ( r & 0x3 | 0x8 ) ;
return v . toString ( 16 ) ;
} ) ) ;
} ;
// simple implementation without alternatives (old rfc)
exports [ "complete_boundrary" ] = function ( bodyparts , attachments ) {
let ret = "" ;
let boundrary = exports [ "gen_boundrary" ] ( ) ;
ret += exports [ "body_boundrary" ] ( bodyparts , boundrary ) ;
ret += exports [ "attachment_boundrary" ] ( attachments , boundrary ) ;
ret += "--" + boundrary + "--\n\nINVISIBLE!!!!" ;
return ( exports [ "simple_body" ] ( {
"contenttype" : sprintf ( "multipart/mixed; boundary=%s" , [ boundrary ] ) ,
"body" : ret
} ) ) ;
} ;
return exports ;
} ) ( ) ;
/ *
This file is part of » bacterio - plankton : string « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : string « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : string « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : string « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var string ;
( function ( string ) {
/ * *
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
const hexdigits = 4 ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
const index _max = ( 1 << ( 4 * hexdigits ) ) ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
var index _is = 0 ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author neuc , frac
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function empty ( str ) {
return ( str . trim ( ) === "" ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . empty = empty ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ desc returns a unique string
* @ param { string } prefix an optional prefix for the generated string
* @ return { string }
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate ( prefix = "string_" ) {
if ( index _is > index _max ) {
throw ( new Error ( "[string_generate] out of valid indices" ) ) ;
}
else {
return string . sprintf ( prefix + "%0" + hexdigits . toString ( ) + "X" , [ index _is ++ ] ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . generate = generate ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function join ( parts , glue = " " ) {
if ( parts . length == 0 ) {
return "" ;
}
else {
return parts . join ( glue ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . join = join ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ desc splits a string , but returns an empty list , if the string is empty
* @ param { string } chain
* @ param { string } separator
* @ return { Array < string > }
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function split ( chain , separator = " " ) {
if ( chain . length == 0 ) {
return [ ] ;
}
else {
return chain . split ( separator ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . split = split ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author neu3no
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function explode ( str , needle , max ) {
let temp = str . split ( needle ) ;
const right = temp . splice ( max - 1 ) ;
temp . push ( right . join ( needle ) ) ;
return temp ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . explode = explode ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ desc concats a given word with itself n times
* @ param { string } word
* @ param { int }
* @ return { string }
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function repeat ( word , count ) {
// return ((count == 0) ? "" : (word + repeat(word, count-1)));
let result = "" ;
for ( let n = 0 ; n < count ; n += 1 ) {
result += word ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return result ;
}
string . repeat = repeat ;
/ * *
* @ desc lengthens a string by repeatedly appending or prepending another string
* @ param { string } word the string to pad
* @ param { int } length the length , which the result shall have
* @ param { string } symbol the string , which will be added ( multiple times )
* @ param { boolean } [ prepend ] ; whether to prepend ( ~ true ) or append ( ~ false ) ; default : false
* @ return { string } the padded string
* @ author fenris
* /
function pad ( word , length , symbol = " " , mode = "append" ) {
switch ( mode ) {
case "prepend" : {
// insert symbols only at the beginning
while ( word . length < length )
word = symbol + word ;
return word . substring ( word . length - length ) ;
break ;
}
case "append" : {
// insert symbols only at the end
while ( word . length < length )
word = word + symbol ;
return word . substring ( 0 , length ) ;
break ;
}
case "widen" : {
// insert symbols at both sides
let left = ( ( ( length - word . length ) & 1 ) === 0 ) ;
while ( word . length < length ) {
word = ( left
? ( symbol + word )
: ( word + symbol ) ) ;
left = ( ! left ) ;
}
return word . substring ( 0 , length ) ;
break ;
}
default : {
const message = ( "unhandled mode '" + mode + "'" ) ;
console . warn ( message ) ;
return word ;
break ;
}
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
}
string . pad = pad ;
/ * *
* @ desc checks if a given string conttains a certain substring
* @ param { string } string
* @ param { string } part
* @ return { boolean }
* @ author fenris
* /
function contains ( chain , part ) {
if ( typeof ( chain ) !== "string" ) {
return false ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return ( chain . indexOf ( part ) >= 0 ) ;
}
string . contains = contains ;
/ * *
* @ desc checks if a given string starts with a certain substring
* @ param { string } string
* @ param { string } part
* @ return { boolean }
* @ author fenris
* /
function startsWith ( chain , part ) {
if ( typeof ( chain ) !== "string" ) {
return false ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
// return (string.indexOf(part) === 0);
return ( ( function ( m , n ) {
if ( n === 0 ) {
return true ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
if ( m === 0 ) {
return false ;
}
else {
return ( ( chain [ 0 ] == part [ 0 ] )
&&
startsWith ( chain . substring ( 1 ) , part . substring ( 1 ) ) ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ( chain . length , part . length ) ) ;
}
string . startsWith = startsWith ;
/ * *
* @ desc checks if a given string ends with a certain substring
* @ param { string } string
* @ param { string } part
* @ return { boolean }
* @ author fenris
* /
function endsWith ( chain , part ) {
if ( typeof ( chain ) !== "string" ) {
return false ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
// return (string.lastIndexOf(part) === string.length-part.length);
return ( ( function ( m , n ) {
if ( n === 0 ) {
return true ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
if ( m === 0 ) {
return false ;
}
else {
// console.info(("(" + string[m-1] + " == " + part[n-1] + ")") + " = " + String(string[m-1] == part[n-1]));
return ( ( chain [ m - 1 ] === part [ n - 1 ] )
&&
endsWith ( chain . substring ( 0 , m - 1 ) , part . substring ( 0 , n - 1 ) ) ) ;
}
}
} ) ( chain . length , part . length ) ) ;
}
string . endsWith = endsWith ;
/ * *
* @ desc count the occourrences of a string in a string
* @ param string haystack _string the string wich should be examined
* @ param string needle _string the string which should be counted
* @ author neuc
* /
function count _occourrences ( haystack _string , needle _string , check _escape ) {
let cnt = 0 ;
let pos = - 1 ;
do {
pos = haystack _string . indexOf ( needle _string , pos + 1 ) ;
if ( ( ! check _escape ) || ( haystack _string [ pos - 1 ] != "\\" ) ) {
cnt ++ ;
}
} while ( pos >= 0 ) ;
return ( cnt - 1 ) ;
}
string . count _occourrences = count _occourrences ;
/ * *
* @ author fenris
* /
function replace ( str , replacements , options = { } ) {
options = Object . assign ( { } , options ) ;
let result = str ;
replacements . forEach ( replacement => {
lib _plankton . log . debug ( "lib_plankton.string.replace" , {
"from" : replacement . from ,
"to" : replacement . to ,
} ) ;
result = result . replace ( new RegExp ( replacement . from , "g" ) , replacement . to ) ;
} ) ;
return result ;
}
string . replace = replace ;
/ * *
* @ desc replaces occurences of "{{name}}" in a string by the corresponding values of an argument object
* @ author fenris
* /
function coin ( str , args , options = { } ) {
options = Object . assign ( {
"legacy" : false ,
"open" : "{{" ,
"close" : "}}" ,
} , options ) ;
Object . keys ( args ) . forEach ( ( key ) => {
// old syntax
{
if ( options . legacy ) {
const value = args [ key ] ;
const regexp _argument = new RegExp ( "\\${" + key + "}" , "g" ) ;
lib _plankton . log . debug ( "lib_plankton.string.coin" , {
"key" : key ,
"regex" : regexp _argument . toString ( ) ,
"value" : value ,
} ) ;
str = str . replace ( regexp _argument , value ) ;
}
}
// new syntax
{
const value = args [ key ] ;
const regexp _argument = new RegExp ( options . open + key + options . close , "g" ) ;
lib _plankton . log . debug ( "lib_plankton.string.coin" , {
"key" : key ,
"regex" : regexp _argument . toString ( ) ,
"value" : value ,
} ) ;
str = str . replace ( regexp _argument , value ) ;
}
} ) ;
return str ;
}
string . coin = coin ;
/ * *
* @ author fenris
* @ deprecated use limit
* /
function cut ( str , length , delimiter = "…" ) {
if ( str . length <= length ) {
return str ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
return ( str . slice ( 0 , length - delimiter . length ) + delimiter ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
string . cut = cut ;
/ * *
* /
function limit ( str , options = { } ) {
options = Object . assign ( {
"length" : 120 ,
"indicator" : "…" ,
} , options ) ;
return ( ( str . length <= options . length )
? str
: ( str . slice ( 0 , options . length - options . indicator . length ) + options . indicator ) ) ;
}
string . limit = limit ;
/ * *
* /
function slice ( str , size ) {
let slices = [ ] ;
let rest = str ;
while ( rest . length > 0 ) {
slices . push ( rest . slice ( 0 , size ) ) ;
rest = rest . slice ( size ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return slices ;
}
string . slice = slice ;
} ) ( string = lib _plankton . string || ( lib _plankton . string = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
/ * *
* @ deprecated
2024-09-09 12:13:10 +02:00
* /
2024-09-18 18:17:25 +02:00
var lib _string ;
( function ( lib _string ) {
lib _string . empty = lib _plankton . string . empty ;
lib _string . generate = lib _plankton . string . generate ;
lib _string . split = lib _plankton . string . split ;
lib _string . explode = lib _plankton . string . repeat ;
lib _string . repeat = lib _plankton . string . repeat ;
lib _string . pad = lib _plankton . string . pad ;
lib _string . contains = lib _plankton . string . contains ;
lib _string . startsWith = lib _plankton . string . startsWith ;
lib _string . endsWith = lib _plankton . string . endsWith ;
lib _string . count _occourrences = lib _plankton . string . count _occourrences ;
lib _string . coin = lib _plankton . string . coin ;
lib _string . stance = lib _plankton . string . coin ;
lib _string . cut = lib _plankton . string . cut ;
} ) ( lib _string || ( lib _string = { } ) ) ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : string « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : string « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var string ;
( function ( string ) {
var pattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/ ;
var gpattern = /%([-+#0 ]*)([0-9]*)[\.]{0,1}([0-9]*)([\w]{1})/g ;
function split _format ( format ) {
var tmp = format . match ( pattern ) ;
if ( tmp === null )
return null ;
2024-09-12 00:03:29 +02:00
return {
2024-09-18 18:17:25 +02:00
'flags' : tmp [ 1 ] . split ( "" ) ,
'width' : Number ( tmp [ 2 ] ) ,
'precision' : tmp [ 3 ] === '' ? null : Number ( tmp [ 3 ] ) ,
'specifier' : tmp [ 4 ] ,
'string' : format
2024-09-09 12:13:10 +02:00
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
function make _err ( format , arg , should ) {
return ( "[sprintf]" + " " + "argument for '" + format . string + "' has to be '" + should + "' but '" + arg + "' is '" + typeof arg + "'!" ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
function test _arg ( format , arg , should ) {
if ( typeof arg !== should ) {
console . warn ( make _err ( format , arg , should ) ) ;
return false ;
}
return true ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
function string _fill ( str , char , len , left ) {
while ( str . length < len ) {
if ( left ) {
str += char ;
}
else {
str = char + str ;
}
}
return str ;
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* the known _parameters are used to parse the different identifiers for the welln known syntax :
* flag width precision identifier
* % { [ 0 # + - ] } { [ 0 - 9 ] * } . { [ 0 - 9 ] * } [ fFdiueEgGsoxXaAsn ]
* flags :
* 0 - fill with '0' instead of ' ' if the string length < width
* # - not implemented
* - - left - justified - > fill on the right side to reach width
* + - force using '+' on positive numbers
* ' ' - add a single space before positive numbers
*
* identifiers
* % f , % F - interpret given number as float , width : the minimal total width ( fill with ' ' or '0' if the
* resulting string is too short , precision : cut more then given decimal places
* % d , % i , % u - interpret number as integer , decimal places will be cut . width : like float , precision :
* fill with '0' on right side until length given in precision is reached
* % e - interpret as float and write as scientifical number , width & precision like in float
* % E - same es % e but uppercase 'E'
* % g - use the shortest string of % f or % e
* % G - use the shortest string of % E or % E
* % s - simply print a string
* % o - print the given number in octal notation
* % x - print the given number in hex notation
* % X - same as % x but with uppercase characters
* % a - alias to % x
* % A - alias to % X
* % n - just print nothing
* @ type { { } }
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
var known _params = { } ;
known _params [ "f" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , "number" ) )
return "Ø" ;
var tmp = Math . abs ( arg ) ;
var sign = ( arg < 0 ) ? - 1 : 1 ;
var tmp _result = null ;
if ( format . precision !== null ) {
tmp = Math . floor ( Math . pow ( 10 , format . precision ) * tmp ) / Math . pow ( 10 , format . precision ) ;
var tmp _ = ( tmp * sign ) . toString ( ) . split ( "." ) ;
if ( tmp _ . length === 1 )
tmp _ . push ( "" ) ;
tmp _ [ 1 ] = string _fill ( tmp _ [ 1 ] , "0" , format . precision , true ) ;
tmp _result = tmp _ . join ( "." ) ;
}
else {
tmp _result = ( sign * tmp ) . toString ( ) ;
}
if ( ( format . flags . indexOf ( " " ) >= 0 ) && ( arg >= 0 ) ) {
tmp _result = " " + tmp ;
}
else if ( ( format . flags . indexOf ( "+" ) >= 0 ) && ( arg >= 0 ) ) {
tmp _result = "+" + tmp ;
}
tmp _result = string _fill ( tmp , ( format . flags . indexOf ( "0" ) >= 0 ) ? "0" : " " , format . width , ( format . flags . indexOf ( "-" ) >= 0 ) ) ;
return tmp _result ;
} ;
known _params [ "F" ] = known _params [ "f" ] ;
known _params [ "d" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
var tmp = ( ( ( arg < 0 && format . specifier !== 'u' ) ? - 1 : 1 ) * Math . floor ( Math . abs ( arg ) ) ) . toString ( ) ;
if ( ( format . specifier === 'd' || format . specifier === 'i' ) && format . flags . indexOf ( ' ' ) >= 0 && arg >= 0 ) {
tmp = ' ' + tmp ;
}
else if ( ( format . specifier === 'd' || format . specifier === 'i' ) && format . flags . indexOf ( '+' ) >= 0 && arg >= 0 ) {
tmp = '+' + tmp ;
}
tmp = string _fill ( tmp , format . flags . indexOf ( '0' ) >= 0 ? '0' : ' ' , format . width , format . flags . indexOf ( '-' ) >= 0 ) ;
tmp = string _fill ( tmp , '0' , format . precision === null ? 0 : format . precision , false ) ;
return tmp ;
} ;
known _params [ "i" ] = known _params [ "d" ] ;
known _params [ "u" ] = known _params [ "d" ] ;
known _params [ "e" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
var tmp = arg . toExponential ( format . precision === null ? undefined : format . precision ) . toString ( ) ;
if ( format . flags . indexOf ( ' ' ) >= 0 && arg >= 0 ) {
tmp = ' ' + tmp ;
}
else if ( format . flags . indexOf ( '+' ) >= 0 && arg >= 0 ) {
tmp = '+' + tmp ;
}
tmp = string _fill ( tmp , format . flags . indexOf ( '0' ) >= 0 ? '0' : ' ' , format . width , format . flags . indexOf ( '-' ) >= 0 ) ;
return tmp ;
} ;
known _params [ "E" ] = function ( format , arg ) {
return known _params [ "e" ] ( format , arg ) . toUpperCase ( ) ;
} ;
known _params [ "g" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
var tmpf = known _params [ "f" ] ( format , arg ) ;
var tmpe = known _params [ "e" ] ( format , arg ) ;
if ( tmpf . length < tmpe . length ) {
return tmpf ;
}
else {
return tmpe ;
}
} ;
known _params [ "G" ] = function ( format , arg ) {
return known _params [ "g" ] ( format , arg ) . toUpperCase ( ) ;
} ;
known _params [ "s" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'string' ) )
return 'o.O' ;
var tmp = format . precision !== null ? arg . substr ( 0 , format . precision ) : arg ;
tmp = string _fill ( tmp , format . flags . indexOf ( '0' ) >= 0 ? '0' : ' ' , format . width , format . flags . indexOf ( '-' ) >= 0 ) ;
return tmp ;
} ;
known _params [ "o" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
var tmp = Math . floor ( Math . round ( Math . abs ( arg ) ) ) * ( ( arg < 0 ) ? - 1 : 1 ) ;
return known _params [ "s" ] ( format , tmp . toString ( 8 ) ) ;
} ;
known _params [ "x" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
var tmp = Math . floor ( Math . round ( Math . abs ( arg ) ) ) * ( ( arg < 0 ) ? - 1 : 1 ) ;
return known _params [ "s" ] ( format , tmp . toString ( 16 ) ) ;
} ;
known _params [ "a" ] = known _params [ "x" ] ;
known _params [ "X" ] = function ( format , arg ) {
if ( ! test _arg ( format , arg , 'number' ) )
return 'Ø' ;
return known _params [ "x" ] ( format , arg ) . toUpperCase ( ) ;
} ;
known _params [ "A" ] = known _params [ "X" ] ;
known _params [ "c" ] = function ( format , arg ) {
var tmp = "" ;
if ( typeof arg === "number" ) {
tmp = String . fromCharCode ( arg ) ;
}
else if ( ( typeof arg === "string" ) && ( arg . length === 1 ) ) {
tmp = arg [ 0 ] ;
}
else {
console . warn ( make _err ( format , arg , "number|string" ) + " and if string it needs to have the length of 1!" ) ;
}
return known _params [ "s" ] ( format , tmp ) ;
} ;
known _params [ "n" ] = function ( ) {
return "" ;
} ;
var decompose = function ( chain , regexp ) {
var result = regexp . exec ( chain ) ;
if ( result == null ) {
return null ;
}
else {
var front = chain . substring ( 0 , result . index ) ;
var back = chain . substring ( result . index + result [ 0 ] . length ) ;
return { "front" : front , "match" : result [ 0 ] , "back" : back } ;
}
} ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* an implementation of c sprintf
* @ param { string } string format string
* @ param { array } args arguments which should be filled into
* @ returns { string }
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
string . sprintf = function ( input , args = [ ] , original = null ) {
if ( original == null )
original = input ;
var components = decompose ( input , pattern ) ;
if ( components == null ) {
if ( args . length > 0 ) {
console . warn ( "[sprintf] superfluous arguments while formatting '" + original + "': " , args ) ;
}
return input ;
}
else {
var arg ;
var rest ;
if ( args . length > 0 ) {
arg = args [ 0 ] ;
rest = args . slice ( 1 ) ;
}
else {
console . warn ( "[sprintf] out of arguments while formatting '" + original + "'" ) ;
arg = null ;
rest = [ ] ;
return input ;
}
var fmt = split _format ( components [ "match" ] ) ;
return ( components [ "front" ]
+ known _params [ fmt . specifier ] ( fmt , arg )
+ string . sprintf ( components [ "back" ] , rest , original ) ) ;
}
} ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* an implementation of c printf
* @ param { string } string format string
* @ param { array } args arguments which should be filled into
* @ returns { string }
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function printf ( format , args ) {
console . log ( string . sprintf ( format , args ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
string . printf = printf ;
} ) ( string = lib _plankton . string || ( lib _plankton . string = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
var sprintf = lib _plankton . string . sprintf ;
var printf = lib _plankton . string . printf ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : string « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : string « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : string « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
2024-09-18 18:17:25 +02:00
var make _logger = ( function ( ) {
var _loggers = { } ;
var make _logger = function ( prefix , current _loglevel ) {
var log = [ ] ;
var level = [
"LOG" , "INFO" , "WARNING" , "DEBUG"
] ;
var logger = function ( obj , lvl ) {
var txt = obj . txt || obj ;
if ( lvl == void 0 )
lvl = 0 ;
var date = new Date ( ) ;
log . push ( {
"message" : sprintf ( "%s [%s:%s] %s" , [ date . toString ( ) , level [ lvl ] , prefix , txt ] ) ,
"timeStamp" : + ( date )
} ) ;
if ( lvl <= current _loglevel ) {
var msg = [ "[" + prefix + "]" , txt ] ;
if ( obj . arg )
msg = [ "[" + prefix + "]" ] . concat ( Array . prototype . slice . call ( obj . arg ) ) ;
if ( lvl === 0 )
console [ "_log" ] . apply ( console , msg ) ;
else if ( lvl === 1 )
console [ "_info" ] . apply ( console , msg ) ;
else if ( lvl === 2 )
console [ "_warn" ] . apply ( console , msg ) ;
else if ( lvl >= 3 )
console [ "_log" ] . apply ( console , msg ) ;
}
} ;
_loggers [ prefix ] = {
"logger" : logger ,
"log" : log
} ;
return logger ;
} ;
make _logger [ "loggers" ] = _loggers ;
make _logger [ "complete_log" ] = function ( ) {
var logs = Object . keys ( _loggers )
. reduce ( function ( p , c ) {
return [ ] . concat ( p , _loggers [ c ] . log ) ;
} , [ ] ) ;
logs . sort ( function ( x , y ) {
return ( ( x . timeStamp > y . timeStamp ) ? - 1 : + 1 ) ;
} ) ;
return logs . map ( function ( x , i , a ) {
return x . message ;
} ) ;
} ;
if ( /*!track_exports*/ true ) {
var _log _all = function ( log , lvl , next = function ( ) { } ) {
return function ( ) {
var msg = [ ] ;
for ( var i = 0 ; i < arguments . length ; i ++ ) {
if ( typeof arguments [ i ] === "string" ) {
msg . push ( arguments [ i ] ) ;
}
else {
msg . push ( JSON . stringify ( arguments [ i ] ) ) ;
}
}
var obj = {
txt : msg . join ( "\t" ) ,
arg : arguments
} ;
log ( obj , lvl ) ;
next ( ) ;
} ;
} ;
{
var _ _warn = make _logger ( "deprecated console.warn" , 99 ) ;
var _ _error = make _logger ( "deprecated console.error" , 99 ) ;
var _ _log = make _logger ( "deprecated console.log" , 99 ) ;
var _ _info = make _logger ( "deprecated console.info" , 99 ) ;
// bad ass
console [ "_log" ] = console . log ;
console [ "_error" ] = console . error ;
console [ "_warn" ] = console . warn ;
console [ "_info" ] = console . info ;
/ *
console [ "log" ] = _log _all ( _ _log , 0 ) ;
console [ "error" ] = _log _all ( _ _error , 2 ) ;
console [ "warn" ] = _log _all ( _ _warn , 2 ) ;
console [ "info" ] = _log _all ( _ _info , 0 ) ;
* /
}
/ *
{
make _logger [ "send_log" ] = function ( ) {
eml _log (
function ( ) {
alert ( "fehlerbericht wurde gesendet!" ) ;
}
) ;
} ;
var error _log = make _logger ( "global.error" , 99 ) ;
window . onerror = _log _all (
error _log ,
1 ,
function ( ) {
if ( global _config == undefined ) {
return false ;
}
if ( global _config . report _error ) {
make _logger [ "send_log" ] ( ) ;
}
}
) ;
}
* /
}
return make _logger ;
} ) ( ) ;
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
/ *
This file is part of » bacterio - plankton : database « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var database ;
( function ( database ) {
/ * *
* /
let enum _type ;
( function ( enum _type ) {
enum _type [ "boolean" ] = "boolean" ;
enum _type [ "integer" ] = "integer" ;
enum _type [ "string_short" ] = "string_short" ;
enum _type [ "string_medium" ] = "string_medium" ;
enum _type [ "string_long" ] = "string_long" ;
enum _type [ "float" ] = "float" ;
} ) ( enum _type = database . enum _type || ( database . enum _type = { } ) ) ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* @ todo default case ?
* /
function sql _common _value _format ( value ) {
if ( value === undefined ) {
throw ( new Error ( "can not format undefined" ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
if ( value === null ) {
return "NULL" ;
}
else {
switch ( typeof ( value ) ) {
case "boolean" : {
return ( value ? "TRUE" : "FALSE" ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "number" : {
return value . toString ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "string" : {
return ( "'" + value + "'" ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
2024-09-12 00:03:29 +02:00
}
2024-09-09 12:13:10 +02:00
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
database . sql _common _value _format = sql _common _value _format ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _common _formulation _create _table ( description _create _table , options = { } ) {
var _a , _b , _c , _d , _e , _f ;
options = Object . assign ( {
"auto_increment_keyword" : "AUTO INCREMENT" ,
"auto_increment_special" : null ,
"omit_comments" : false ,
"type_map" : {
"boolean" : "BOOLEAN" ,
"integer" : "INTEGER" ,
"string_short" : "VARCHAR(63)" ,
"string_medium" : "VARCHAR(255)" ,
"string_long" : "TEXT" ,
"float" : "REAL" ,
} ,
"wrap_name" : ( x => x ) ,
} , options ) ;
return {
"template" : lib _plankton . string . coin ( "CREATE TABLE IF NOT EXISTS\n\t{{name}}(\n{{entries}}\n\t){{comment}}\n;" , {
"name" : options . wrap _name ( description _create _table . name ) ,
"comment" : ( ( options . omit _comments
||
( ( ( _a = description _create _table . description ) !== null && _a !== void 0 ? _a : null ) === null ) )
? ""
: lib _plankton . string . coin ( " COMMENT '{{comment}}'" , {
"comment" : description _create _table . description ,
} ) ) ,
"entries" : ( ( [ ]
// key field
. concat ( ( ( ( _b = description _create _table . key _field ) !== null && _b !== void 0 ? _b : null ) === null )
? [ ]
: lib _plankton . string . coin ( "{{name}} {{parameters}}" , {
"name" : options . wrap _name ( description _create _table . key _field . name ) ,
"parameters" : ( ( ( options . auto _increment _special === null )
?
( [ ]
// type
. concat ( [
options . type _map [ ( _c = description _create _table . key _field . type ) !== null && _c !== void 0 ? _c : "integer" ] ,
] )
// nullability
. concat ( [
"NOT NULL" ,
] )
// primary key
. concat ( [
"PRIMARY KEY" ,
] )
// auto increment
. concat ( ( ( ( _d = description _create _table . key _field . auto _increment ) !== null && _d !== void 0 ? _d : true ) === null )
?
[ ]
:
[
options . auto _increment _keyword ,
] ) )
:
[
options . auto _increment _special
] )
. join ( " " ) ) ,
} ) )
// data fields
. concat ( ( ( _e = description _create _table . data _fields ) !== null && _e !== void 0 ? _e : [ ] )
. map ( ( data _field ) => {
var _a , _b ;
return lib _plankton . string . coin ( "{{name}} {{parameters}}" , {
"name" : options . wrap _name ( data _field . name ) ,
"parameters" : ( ( [ ]
// type
. concat ( [
options . type _map [ data _field . type ] ,
] )
// nullability
. concat ( ( ! ( ( _a = data _field . nullable ) !== null && _a !== void 0 ? _a : false ) )
? [ ]
: [ "NULL" ] )
// default
. concat ( ( ! data _field . hasOwnProperty ( "default" ) )
? [ ]
: [
lib _plankton . string . coin ( " DEFAULT {{value}}" , {
"value" : sql _common _value _format ( data _field . default )
} ) ,
] )
// comment
. concat ( ( options . omit _comments
||
( ( ( _b = data _field . description ) !== null && _b !== void 0 ? _b : null ) === null ) )
? [ ]
: [
lib _plankton . string . coin ( "COMMENT '{{comment}}'" , {
"comment" : data _field . description ,
} ) ,
] ) )
. join ( " " ) )
} ) ;
} ) )
// constraints
. concat ( ( ( _f = description _create _table . constraints ) !== null && _f !== void 0 ? _f : [ ] )
. map ( ( constraint ) => {
switch ( constraint . kind ) {
default : {
throw ( new Error ( "unhandled constraint kind: " + constraint . kind ) ) ;
break ;
}
case "foreign_key" : {
return lib _plankton . string . coin ( "FOREIGN KEY ({{fields}}) REFERENCES {{reference_name}}({{reference_fields}})" , {
"fields" : ( constraint . parameters [ "fields" ]
. map ( x => options . wrap _name ( x ) )
. join ( "," ) ) ,
"reference_name" : options . wrap _name ( constraint . parameters [ "reference" ] [ "name" ] ) ,
"reference_fields" : ( constraint . parameters [ "reference" ] [ "fields" ]
. map ( x => options . wrap _name ( x ) )
. join ( "," ) ) ,
} ) ;
break ;
}
case "unique" : {
return lib _plankton . string . coin ( "UNIQUE ({{fields}})" , {
"fields" : ( constraint . parameters [ "fields" ]
. map ( x => options . wrap _name ( x ) )
. join ( "," ) ) ,
} ) ;
break ;
}
}
} ) ) )
. map ( x => ( "\t\t" + x ) )
. join ( ",\n" ) ) ,
} ) ,
"arguments" : { }
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
database . sql _common _formulation _create _table = sql _common _formulation _create _table ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _common _formulation _insert ( description _insert , options = { } ) {
options = Object . assign ( {
"wrap_name" : ( x => x ) ,
"set_returning" : false ,
} , options ) ;
const field _names = Object . keys ( description _insert . values ) ;
return {
"template" : lib _plankton . string . coin ( "INSERT INTO {{table_name}}({{schema}}) VALUES ({{values}}){{returning}};" , {
"table_name" : options . wrap _name ( description _insert . table _name ) ,
"schema" : ( field _names
. map ( ( field _name ) => lib _plankton . string . coin ( "{{name}}" , {
"name" : options . wrap _name ( field _name ) ,
} ) )
. join ( "," ) ) ,
"values" : ( field _names
. map ( ( field _name ) => lib _plankton . string . coin ( "$value_{{name}}" , {
"name" : field _name ,
} ) )
. join ( "," ) ) ,
"returning" : ( ( options . set _returning
&&
( ( description _insert . returning !== undefined )
&&
( description _insert . returning !== null ) ) )
?
( " RETURNING " + description _insert . returning )
:
"" ) ,
} ) ,
"arguments" : Object . fromEntries ( Object . entries ( description _insert . values )
. map ( ( [ name , value ] ) => ( [
lib _plankton . string . coin ( "value_{{name}}" , {
"name" : name ,
} ) ,
value
] ) ) ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
database . sql _common _formulation _insert = sql _common _formulation _insert ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _common _formulation _update ( description _update , options = { } ) {
var _a , _b ;
options = Object . assign ( {
"wrap_name" : ( x => x ) ,
} , options ) ;
const field _names = Object . keys ( description _update . values ) ;
return {
"template" : lib _plankton . string . coin ( "UPDATE {{table_name}} SET {{assignments}}{{macro_where}};" , {
"table_name" : options . wrap _name ( description _update . table _name ) ,
"assignments" : ( field _names
. map ( ( field _name ) => lib _plankton . string . coin ( "{{name}} = $value_{{suffix}}" , {
"name" : options . wrap _name ( field _name ) ,
"suffix" : field _name ,
} ) )
. join ( ", " ) ) ,
"macro_where" : ( ( ( ( _a = description _update . condition ) !== null && _a !== void 0 ? _a : null ) === null )
? ""
: lib _plankton . string . coin ( " WHERE {{expression}}" , {
"expression" : description _update . condition ,
} ) ) ,
} ) ,
"arguments" : Object . assign ( Object . fromEntries ( Object . entries ( description _update . values )
. map ( ( [ name , value ] ) => ( [
lib _plankton . string . coin ( "value_{{name}}" , {
"name" : name ,
} ) ,
value
] ) ) ) , ( ( _b = description _update . arguments ) !== null && _b !== void 0 ? _b : { } ) )
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
database . sql _common _formulation _update = sql _common _formulation _update ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _common _formulation _delete ( description _delete , options = { } ) {
var _a , _b ;
options = Object . assign ( {
"wrap_name" : ( x => x ) ,
} , options ) ;
return {
"template" : lib _plankton . string . coin ( "DELETE FROM {{table_name}}{{macro_where}};" , {
"table_name" : options . wrap _name ( description _delete . table _name ) ,
"macro_where" : ( ( ( ( _a = description _delete . condition ) !== null && _a !== void 0 ? _a : null ) === null )
? ""
: lib _plankton . string . coin ( " WHERE {{expression}}" , {
"expression" : description _delete . condition ,
} ) ) ,
} ) ,
"arguments" : Object . assign ( { } , ( ( _b = description _delete . arguments ) !== null && _b !== void 0 ? _b : { } ) ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
database . sql _common _formulation _delete = sql _common _formulation _delete ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _common _formulation _select ( description _select , options = { } ) {
var _a , _b , _c , _d , _e , _f , _g ;
options = Object . assign ( {
"wrap_name" : ( x => x ) ,
} , options ) ;
return {
"template" : lib _plankton . string . coin ( "SELECT {{fields}} FROM {{source}}{{macro_where}}{{macro_group_by}}{{macro_having}}{{macro_order_by}}{{macro_limit}};" , {
"source" : options . wrap _name ( description _select . source ) ,
"fields" : ( ( ( ( _a = description _select . fields ) !== null && _a !== void 0 ? _a : null ) === null )
? "*"
: ( description _select . fields
. map ( field _name => lib _plankton . string . coin ( "{{name}}" , { "name" : options . wrap _name ( field _name ) } ) )
. join ( "," ) ) ) ,
"macro_where" : ( ( ( ( _b = description _select . condition ) !== null && _b !== void 0 ? _b : null ) === null )
? ""
: lib _plankton . string . coin ( " WHERE {{expression}}" , {
"expression" : description _select . condition ,
} ) ) ,
"macro_group_by" : ( ( ( ( _c = description _select . group _by ) !== null && _c !== void 0 ? _c : null ) === null )
? ""
: lib _plankton . string . coin ( " GROUP BY {{expression}}" , {
"expression" : description _select . group _by ,
} ) ) ,
"macro_having" : ( ( ( ( _d = description _select . having ) !== null && _d !== void 0 ? _d : null ) === null )
? ""
: lib _plankton . string . coin ( " HAVING {{expression}}" , {
"expression" : description _select . having ,
} ) ) ,
"macro_order_by" : ( ( ( ( _e = description _select . order _by ) !== null && _e !== void 0 ? _e : null ) === null )
? ""
: lib _plankton . string . coin ( " ORDER BY {{expression}}" , {
"expression" : description _select . order _by ,
} ) ) ,
"macro_limit" : ( ( ( ( _f = description _select . limit ) !== null && _f !== void 0 ? _f : null ) === null )
? ""
: lib _plankton . string . coin ( " LIMIT {{expression}}" , {
"expression" : description _select . limit . toFixed ( 0 ) ,
} ) ) ,
} ) ,
"arguments" : Object . assign ( { } , ( ( _g = description _select . arguments ) !== null && _g !== void 0 ? _g : { } ) ) ,
2024-09-12 00:03:29 +02:00
} ;
}
2024-09-18 18:17:25 +02:00
database . sql _common _formulation _select = sql _common _formulation _select ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* /
function sqlite _make ( parameters ) {
return {
"path" : parameters . path ,
"handle" : null ,
} ;
}
database . sqlite _make = sqlite _make ;
/ * *
* /
function sqlite _wrap _name ( name ) {
return ( "`" + name + "`" ) ;
}
database . sqlite _wrap _name = sqlite _wrap _name ;
/ * *
* /
function sqlite _init ( subject ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
if ( subject . handle === null ) {
yield new Promise ( ( resolve , reject ) => {
const _nm _sqlite3 = require ( "sqlite3" ) ;
subject . handle = new _nm _sqlite3 . Database ( subject . path , ( _nm _sqlite3 . OPEN _READWRITE | _nm _sqlite3 . OPEN _CREATE | _nm _sqlite3 . OPEN _FULLMUTEX ) , ( ) => { resolve ( undefined ) ; } ) ;
} ) ;
2024-09-09 12:13:10 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
// do nothing
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return Promise . resolve ( undefined ) ;
} ) ;
}
/ * *
* @ author fenris
* /
function sqlite _adjust _query ( subject , query ) {
const query _adjusted = {
"template" : query . template ,
"arguments" : Object . fromEntries ( Object . entries ( query . arguments )
. map ( ( [ key , value ] ) => ( [
( "$" + key ) ,
( ( ( value === null )
||
( value === undefined ) )
? null
: ( ( typeof ( value ) === "boolean" )
? ( value ? "1" : "0" )
: value . toString ( ) ) )
] ) ) ) ,
} ;
lib _plankton . log . debug ( "database_sqlite_query" , {
"original" : query ,
"adjusted" : query _adjusted ,
} ) ;
return query _adjusted ;
}
/ * *
* @ author fenris
* /
function sqlite _query _free _get ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield sqlite _init ( subject ) ;
const query _adjusted = sqlite _adjust _query ( subject , query ) ;
return ( new Promise ( ( resolve , reject ) => {
subject . handle . all ( query _adjusted . template , query _adjusted . arguments , ( error , rows ) => {
if ( error !== null ) {
reject ( error ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
resolve ( rows ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
} ) ) ;
} ) ;
}
database . sqlite _query _free _get = sqlite _query _free _get ;
/ * *
* @ author fenris
* /
function sqlite _query _free _put ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield sqlite _init ( subject ) ;
const query _adjusted = sqlite _adjust _query ( subject , query ) ;
return ( new Promise ( ( resolve , reject ) => {
subject . handle . run ( query _adjusted . template , query _adjusted . arguments ,
// this MUST be an old style function
function ( error ) {
if ( error ) {
reject ( error ) ;
}
else {
resolve ( this [ "lastID" ] ) ;
}
} ) ;
} ) ) ;
} ) ;
}
database . sqlite _query _free _put = sqlite _query _free _put ;
/ * *
* @ author fenris
* /
function sqlite _query _free _set ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield sqlite _init ( subject ) ;
const query _adjusted = sqlite _adjust _query ( subject , query ) ;
return ( new Promise ( ( resolve , reject ) => {
subject . handle . run ( query _adjusted . template , query _adjusted . arguments ,
// this MUST be an old style function
function ( error ) {
if ( error ) {
reject ( error ) ;
}
else {
resolve ( this [ "changes" ] ) ;
}
} ) ;
} ) ) ;
} ) ;
}
database . sqlite _query _free _set = sqlite _query _free _set ;
/ * *
* /
function sqlite _formulation _create _table ( description _create _table ) {
return database . sql _common _formulation _create _table ( description _create _table , {
"auto_increment_keyword" : "AUTOINCREMENT" ,
"auto_increment_special" : null ,
"omit_comments" : true ,
"type_map" : {
"boolean" : "INTEGER" ,
"integer" : "INTEGER" ,
"string_short" : "TEXT" ,
"string_medium" : "TEXT" ,
"string_long" : "TEXT" ,
"float" : "REAL" ,
} ,
"wrap_name" : sqlite _wrap _name ,
} ) ;
}
database . sqlite _formulation _create _table = sqlite _formulation _create _table ;
/ * *
* /
function sqlite _query _create _table ( subject , description ) {
return ( sqlite _query _free _set ( subject , sqlite _formulation _create _table ( description ) )
. then ( x => Promise . resolve ( undefined ) ) ) ;
}
database . sqlite _query _create _table = sqlite _query _create _table ;
/ * *
* /
function sqlite _formulation _insert ( description _insert ) {
return database . sql _common _formulation _insert ( description _insert , {
"wrap_name" : sqlite _wrap _name ,
} ) ;
}
database . sqlite _formulation _insert = sqlite _formulation _insert ;
/ * *
* /
function sqlite _query _insert ( subject , description _insert ) {
return sqlite _query _free _put ( subject , sqlite _formulation _insert ( description _insert ) ) ;
}
database . sqlite _query _insert = sqlite _query _insert ;
/ * *
* /
function sqlite _formulation _update ( description _update ) {
return database . sql _common _formulation _update ( description _update , {
"wrap_name" : sqlite _wrap _name ,
} ) ;
}
database . sqlite _formulation _update = sqlite _formulation _update ;
/ * *
* /
function sqlite _query _update ( subject , description _update ) {
return sqlite _query _free _set ( subject , sqlite _formulation _update ( description _update ) ) ;
}
database . sqlite _query _update = sqlite _query _update ;
/ * *
* /
function sqlite _formulation _delete ( description _delete ) {
return database . sql _common _formulation _delete ( description _delete , {
"wrap_name" : sqlite _wrap _name ,
} ) ;
}
database . sqlite _formulation _delete = sqlite _formulation _delete ;
/ * *
* /
function sqlite _query _delete ( subject , description _delete ) {
return sqlite _query _free _set ( subject , sqlite _formulation _delete ( description _delete ) ) ;
}
database . sqlite _query _delete = sqlite _query _delete ;
/ * *
* /
function sqlite _formulation _select ( description _select ) {
return database . sql _common _formulation _select ( description _select , {
"wrap_name" : sqlite _wrap _name ,
} ) ;
}
database . sqlite _formulation _select = sqlite _formulation _select ;
/ * *
* /
function sqlite _query _select ( subject , description _select ) {
return sqlite _query _free _get ( subject , sqlite _formulation _select ( description _select ) ) ;
}
database . sqlite _query _select = sqlite _query _select ;
/ * *
* /
function sqlite _database ( parameters ) {
const subject = sqlite _make ( parameters ) ;
return {
"wrap_name" : ( name ) => sqlite _wrap _name ( name ) ,
"query_free_get" : ( query ) => sqlite _query _free _get ( subject , query ) ,
"query_free_put" : ( query ) => sqlite _query _free _put ( subject , query ) ,
"query_free_set" : ( query ) => sqlite _query _free _set ( subject , query ) ,
"query_create_table" : ( description _create _table ) => sqlite _query _create _table ( subject , description _create _table ) ,
"query_insert" : ( description _insert ) => sqlite _query _insert ( subject , description _insert ) ,
"query_update" : ( description _update ) => sqlite _query _update ( subject , description _update ) ,
"query_delete" : ( description _delete ) => sqlite _query _delete ( subject , description _delete ) ,
"query_select" : ( description _select ) => sqlite _query _select ( subject , description _select ) ,
} ;
}
database . sqlite _database = sqlite _database ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* @ author fenris
* /
class class _sqlite {
constructor ( parameters ) { this . subject = database . sqlite _make ( parameters ) ; }
wrap _name ( name ) { return database . sqlite _wrap _name ( name ) ; }
query _free _get ( query ) { return database . sqlite _query _free _get ( this . subject , query ) ; }
query _free _put ( query ) { return database . sqlite _query _free _put ( this . subject , query ) ; }
query _free _set ( query ) { return database . sqlite _query _free _set ( this . subject , query ) ; }
query _create _table ( description _create _table ) { return database . sqlite _query _create _table ( this . subject , description _create _table ) ; }
query _insert ( description _insert ) { return database . sqlite _query _insert ( this . subject , description _insert ) ; }
query _update ( description _update ) { return database . sqlite _query _update ( this . subject , description _update ) ; }
query _delete ( description _delete ) { return database . sqlite _query _delete ( this . subject , description _delete ) ; }
query _select ( description _select ) { return database . sqlite _query _select ( this . subject , description _select ) ; }
}
database . class _sqlite = class _sqlite ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* /
function postgresql _make ( parameters ) {
var _a ;
return {
"host" : parameters . host ,
"port" : ( ( _a = parameters . port ) !== null && _a !== void 0 ? _a : 5432 ) ,
"username" : parameters . username ,
"password" : parameters . password ,
"schema" : parameters . schema ,
"pool" : null ,
} ;
}
database . postgresql _make = postgresql _make ;
/ * *
* /
function postgresql _wrap _name ( name ) {
return ( "" + name + "" ) ;
}
database . postgresql _wrap _name = postgresql _wrap _name ;
/ * *
* https : //node-postgres.com/apis/pool
* /
function postgresql _init ( subject ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
if ( subject . pool === null ) {
const nm _pg = require ( "pg" ) ;
subject . pool = ( new nm _pg . Pool ( {
"host" : subject . host ,
"port" : subject . port ,
"database" : subject . schema ,
"user" : subject . username ,
"password" : subject . password ,
} ) ) ;
}
else {
// do nothing
}
return Promise . resolve ( undefined ) ;
} ) ;
}
/ * *
* @ author fenris
* /
function postgresql _adjust _query ( subject , query ) {
let query _adjusted = {
"template" : query . template ,
"arguments" : [ ]
} ;
let index = 1 ;
while ( true ) {
const regexp = ( new RegExp ( "\\$([a-zA-Z_][0-9a-zA-Z_]*)" , "g" ) ) ;
const matching = regexp . exec ( query _adjusted . template ) ;
if ( matching === null ) {
break ;
}
else {
const part = matching [ 0 ] ;
const name = matching [ 1 ] ;
query _adjusted . template = ( query _adjusted . template . slice ( 0 , matching . index )
+
( "$" + index . toFixed ( 0 ) )
+
query _adjusted . template . slice ( matching . index + part . length ) ) ;
query _adjusted . arguments . push ( query . arguments [ name ] ) ;
index += 1 ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
lib _plankton . log . debug ( "database_postgresql_query" , {
"original" : query ,
"adjusted" : query _adjusted ,
} ) ;
return query _adjusted ;
}
/ * *
* @ author fenris
* @ see https : //node-postgres.com/apis/pool#poolquery
* /
function postgresql _query _free _get ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield postgresql _init ( subject ) ;
const query _adjusted = postgresql _adjust _query ( subject , query ) ;
const result = yield subject . pool . query ( {
"text" : query _adjusted . template ,
"values" : query _adjusted . arguments ,
} ) ;
return result [ "rows" ] ;
} ) ;
}
database . postgresql _query _free _get = postgresql _query _free _get ;
/ * *
* @ author fenris
* @ see https : //node-postgres.com/apis/pool#poolquery
* /
function postgresql _query _free _put ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield postgresql _init ( subject ) ;
const query _adjusted = postgresql _adjust _query ( subject , query ) ;
const result = yield subject . pool . query ( query _adjusted . template , query _adjusted . arguments ) ;
if ( result [ "rows" ] . length <= 0 ) {
return null ;
2024-09-09 12:13:10 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
const x = result [ "rows" ] [ 0 ] ;
const keys = Object . keys ( x ) ;
if ( keys . length <= 0 ) {
return null ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
return x [ Object . keys ( x ) [ 0 ] ] ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
}
database . postgresql _query _free _put = postgresql _query _free _put ;
/ * *
* @ author fenris
* @ see https : //node-postgres.com/apis/pool#poolquery
* /
function postgresql _query _free _set ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
yield postgresql _init ( subject ) ;
const query _adjusted = postgresql _adjust _query ( subject , query ) ;
const result = yield subject . pool . query ( query _adjusted . template , query _adjusted . arguments ) ;
return result [ "rowCount" ] ;
} ) ;
}
database . postgresql _query _free _set = postgresql _query _free _set ;
/ * *
* /
function postgresql _formulation _create _table ( description _create _table ) {
return database . sql _common _formulation _create _table ( description _create _table , {
"auto_increment_keyword" : "" ,
"auto_increment_special" : "SERIAL" ,
"omit_comments" : false ,
"type_map" : {
"boolean" : "BOOLEAN" ,
"integer" : "INTEGER" ,
"string_short" : "VARCHAR(63)" ,
"string_medium" : "VARCHAR(255)" ,
"string_long" : "TEXT" ,
"float" : "REAL" ,
} ,
"wrap_name" : postgresql _wrap _name ,
} ) ;
}
database . postgresql _formulation _create _table = postgresql _formulation _create _table ;
/ * *
* /
function postgresql _query _create _table ( subject , description ) {
return ( postgresql _query _free _set ( subject , postgresql _formulation _create _table ( description ) )
. then ( x => Promise . resolve ( undefined ) ) ) ;
}
database . postgresql _query _create _table = postgresql _query _create _table ;
/ * *
* /
function postgresql _formulation _insert ( description _insert ) {
return database . sql _common _formulation _insert ( description _insert , {
"wrap_name" : postgresql _wrap _name ,
"set_returning" : true
} ) ;
}
database . postgresql _formulation _insert = postgresql _formulation _insert ;
/ * *
* /
function postgresql _query _insert ( subject , description _insert ) {
return postgresql _query _free _put ( subject , postgresql _formulation _insert ( description _insert ) ) ;
}
database . postgresql _query _insert = postgresql _query _insert ;
/ * *
* /
function postgresql _formulation _update ( description _update ) {
return database . sql _common _formulation _update ( description _update , {
"wrap_name" : postgresql _wrap _name ,
} ) ;
}
database . postgresql _formulation _update = postgresql _formulation _update ;
/ * *
* /
function postgresql _query _update ( subject , description _update ) {
return postgresql _query _free _set ( subject , postgresql _formulation _update ( description _update ) ) ;
}
database . postgresql _query _update = postgresql _query _update ;
/ * *
* /
function postgresql _formulation _delete ( description _delete ) {
return database . sql _common _formulation _delete ( description _delete , {
"wrap_name" : postgresql _wrap _name ,
} ) ;
}
database . postgresql _formulation _delete = postgresql _formulation _delete ;
/ * *
* /
function postgresql _query _delete ( subject , description _delete ) {
return postgresql _query _free _set ( subject , postgresql _formulation _delete ( description _delete ) ) ;
}
database . postgresql _query _delete = postgresql _query _delete ;
/ * *
* /
function postgresql _formulation _select ( description _select ) {
return database . sql _common _formulation _select ( description _select , {
"wrap_name" : postgresql _wrap _name ,
} ) ;
}
database . postgresql _formulation _select = postgresql _formulation _select ;
/ * *
* /
function postgresql _query _select ( subject , description _select ) {
return postgresql _query _free _get ( subject , postgresql _formulation _select ( description _select ) ) ;
}
database . postgresql _query _select = postgresql _query _select ;
/ * *
* /
function postgresql _database ( parameters ) {
const subject = postgresql _make ( parameters ) ;
return {
"wrap_name" : ( name ) => postgresql _wrap _name ( name ) ,
"query_free_get" : ( query ) => postgresql _query _free _get ( subject , query ) ,
"query_free_put" : ( query ) => postgresql _query _free _put ( subject , query ) ,
"query_free_set" : ( query ) => postgresql _query _free _set ( subject , query ) ,
"query_create_table" : ( description _create _table ) => postgresql _query _create _table ( subject , description _create _table ) ,
"query_insert" : ( description _insert ) => postgresql _query _insert ( subject , description _insert ) ,
"query_update" : ( description _update ) => postgresql _query _update ( subject , description _update ) ,
"query_delete" : ( description _delete ) => postgresql _query _delete ( subject , description _delete ) ,
"query_select" : ( description _select ) => postgresql _query _select ( subject , description _select ) ,
} ;
}
database . postgresql _database = postgresql _database ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* @ author fenris
* /
class class _postgresql {
constructor ( parameters ) { this . subject = database . postgresql _make ( parameters ) ; }
wrap _name ( name ) { return database . postgresql _wrap _name ( name ) ; }
query _free _get ( query ) { return database . postgresql _query _free _get ( this . subject , query ) ; }
query _free _put ( query ) { return database . postgresql _query _free _put ( this . subject , query ) ; }
query _free _set ( query ) { return database . postgresql _query _free _set ( this . subject , query ) ; }
query _create _table ( description _create _table ) { return database . postgresql _query _create _table ( this . subject , description _create _table ) ; }
query _insert ( description _insert ) { return database . postgresql _query _insert ( this . subject , description _insert ) ; }
query _update ( description _update ) { return database . postgresql _query _update ( this . subject , description _update ) ; }
query _delete ( description _delete ) { return database . postgresql _query _delete ( this . subject , description _delete ) ; }
query _select ( description _select ) { return database . postgresql _query _select ( this . subject , description _select ) ; }
}
database . class _postgresql = class _postgresql ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* /
function mysql _make ( parameters ) {
throw ( new Error ( "not implemented" ) ) ;
}
database . mysql _make = mysql _make ;
/ * *
* /
function mysql _wrap _name ( name ) {
return ( "`" + name + "`" ) ;
}
database . mysql _wrap _name = mysql _wrap _name ;
/ * *
* /
function mysql _init ( subject ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
throw ( new Error ( "not implemented" ) ) ;
} ) ;
}
/ * *
* @ author fenris
* /
function mysql _adjust _query ( subject , query ) {
if ( subject . verbose ) {
console . info ( query ) ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
throw ( new Error ( "not implemented" ) ) ;
}
/ * *
* @ author fenris
* /
function mysql _query _free _get ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
throw ( new Error ( "not implemented" ) ) ;
} ) ;
}
database . mysql _query _free _get = mysql _query _free _get ;
/ * *
* @ author fenris
* /
function mysql _query _free _put ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
throw ( new Error ( "not implemented" ) ) ;
} ) ;
}
database . mysql _query _free _put = mysql _query _free _put ;
/ * *
* @ author fenris
* /
function mysql _query _free _set ( subject , query ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
throw ( new Error ( "not implemented" ) ) ;
} ) ;
}
database . mysql _query _free _set = mysql _query _free _set ;
/ * *
* /
function mysql _formulation _create _table ( description _create _table ) {
return database . sql _common _formulation _create _table ( description _create _table , {
"auto_increment_keyword" : "AUTO INCREMENT" ,
"omit_comments" : false ,
"type_map" : {
"boolean" : "BOOLEAN" ,
"integer" : "INTEGER" ,
"string_short" : "VARCHAR(63)" ,
"string_medium" : "VARCHAR(255)" ,
"string_long" : "TEXT" ,
"float" : "REAL" ,
} ,
"wrap_name" : mysql _wrap _name ,
} ) ;
}
database . mysql _formulation _create _table = mysql _formulation _create _table ;
/ * *
* /
function mysql _query _create _table ( subject , description ) {
return ( mysql _query _free _set ( subject , mysql _formulation _create _table ( description ) )
. then ( x => Promise . resolve ( undefined ) ) ) ;
}
database . mysql _query _create _table = mysql _query _create _table ;
/ * *
* /
function mysql _formulation _insert ( description _insert ) {
return database . sql _common _formulation _insert ( description _insert , {
"wrap_name" : mysql _wrap _name ,
} ) ;
}
database . mysql _formulation _insert = mysql _formulation _insert ;
/ * *
* /
function mysql _query _insert ( subject , description _insert ) {
return mysql _query _free _put ( subject , mysql _formulation _insert ( description _insert ) ) ;
}
database . mysql _query _insert = mysql _query _insert ;
/ * *
* /
function mysql _formulation _update ( description _update ) {
return database . sql _common _formulation _update ( description _update , {
"wrap_name" : mysql _wrap _name ,
} ) ;
}
database . mysql _formulation _update = mysql _formulation _update ;
/ * *
* /
function mysql _query _update ( subject , description _update ) {
return mysql _query _free _set ( subject , mysql _formulation _update ( description _update ) ) ;
}
database . mysql _query _update = mysql _query _update ;
/ * *
* /
function mysql _formulation _delete ( description _delete ) {
return database . sql _common _formulation _delete ( description _delete , {
"wrap_name" : mysql _wrap _name ,
} ) ;
}
database . mysql _formulation _delete = mysql _formulation _delete ;
/ * *
* /
function mysql _query _delete ( subject , description _delete ) {
return mysql _query _free _set ( subject , mysql _formulation _delete ( description _delete ) ) ;
}
database . mysql _query _delete = mysql _query _delete ;
/ * *
* /
function mysql _formulation _select ( description _select ) {
return database . sql _common _formulation _select ( description _select , {
"wrap_name" : mysql _wrap _name ,
} ) ;
}
database . mysql _formulation _select = mysql _formulation _select ;
/ * *
* /
function mysql _query _select ( subject , description _select ) {
return mysql _query _free _get ( subject , mysql _formulation _select ( description _select ) ) ;
}
database . mysql _query _select = mysql _query _select ;
/ * *
* /
function mysql _database ( parameters ) {
const subject = mysql _make ( parameters ) ;
return {
"wrap_name" : ( name ) => mysql _wrap _name ( name ) ,
"query_free_get" : ( query ) => mysql _query _free _get ( subject , query ) ,
"query_free_put" : ( query ) => mysql _query _free _put ( subject , query ) ,
"query_free_set" : ( query ) => mysql _query _free _set ( subject , query ) ,
"query_create_table" : ( description _create _table ) => mysql _query _create _table ( subject , description _create _table ) ,
"query_insert" : ( description _insert ) => mysql _query _insert ( subject , description _insert ) ,
"query_update" : ( description _update ) => mysql _query _update ( subject , description _update ) ,
"query_delete" : ( description _delete ) => mysql _query _delete ( subject , description _delete ) ,
"query_select" : ( description _select ) => mysql _query _select ( subject , description _select ) ,
} ;
}
database . mysql _database = mysql _database ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : database « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : database « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : database « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var database ;
( function ( database ) {
/ * *
* @ author fenris
* /
class class _mysql {
constructor ( parameters ) { this . subject = database . mysql _make ( parameters ) ; }
wrap _name ( name ) { return database . mysql _wrap _name ( name ) ; }
query _free _get ( query ) { return database . mysql _query _free _get ( this . subject , query ) ; }
query _free _put ( query ) { return database . mysql _query _free _put ( this . subject , query ) ; }
query _free _set ( query ) { return database . mysql _query _free _set ( this . subject , query ) ; }
query _create _table ( description _create _table ) { return database . mysql _query _create _table ( this . subject , description _create _table ) ; }
query _insert ( description _insert ) { return database . mysql _query _insert ( this . subject , description _insert ) ; }
query _update ( description _update ) { return database . mysql _query _update ( this . subject , description _update ) ; }
query _delete ( description _delete ) { return database . mysql _query _delete ( this . subject , description _delete ) ; }
query _select ( description _select ) { return database . mysql _query _select ( this . subject , description _select ) ; }
}
database . class _mysql = class _mysql ;
} ) ( database = lib _plankton . database || ( lib _plankton . database = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _generator = ( this && this . _ _generator ) || function ( thisArg , body ) {
var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
function step ( op ) {
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
2024-09-21 10:55:06 +02:00
while ( _ ) try {
2024-09-18 18:17:25 +02:00
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
switch ( op [ 0 ] ) {
case 0 : case 1 : t = op ; break ;
case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
default :
if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
if ( t [ 2 ] ) _ . ops . pop ( ) ;
_ . trys . pop ( ) ; continue ;
}
op = body . call ( thisArg , _ ) ;
} catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
}
} ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
2024-09-12 00:03:29 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
var memory ;
( function ( memory ) {
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function make ( parameters ) {
return {
"data" : { }
} ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
memory . make = make ;
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function clear ( subject ) {
subject . data = { } ;
}
memory . clear = clear ;
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function write ( subject , key , value ) {
var exists = ( key in subject . data ) ;
subject . data [ key ] = value ;
return exists ;
}
memory . write = write ;
/ * *
* @ author fenris
* /
function delete _ ( subject , key ) {
if ( ! ( key in subject . data ) ) {
throw ( new Error ( "no value for key '" + key + "'" ) ) ;
}
else {
delete subject . data [ key ] ;
}
}
memory . delete _ = delete _ ;
/ * *
* @ author fenris
* /
function read ( subject , key ) {
if ( ! ( key in subject . data ) ) {
throw ( new Error ( "no value for key '" + key + "'" ) ) ;
}
else {
return subject . data [ key ] ;
}
}
memory . read = read ;
/ * *
* @ author fenris
* /
function list ( subject ) {
return Object . keys ( subject . data ) ;
}
memory . list = list ;
/ * *
* @ author fenris
* /
function search ( subject , term ) {
return ( list ( subject )
. map ( function ( key ) { return ( { "key" : key , "preview" : key } ) ; } ) ) ;
}
memory . search = search ;
/ * *
* @ author fenris
* /
function implementation _chest ( parameters ) {
function wrap ( core ) {
return ( new Promise ( function ( resolve , reject ) { resolve ( core ( ) ) ; } ) ) ;
}
var subject = make ( parameters ) ;
return {
"setup" : function ( input ) { return Promise . resolve ( undefined ) ; } ,
"clear" : function ( ) { return wrap ( function ( ) { return clear ( subject ) ; } ) ; } ,
"write" : function ( key , value ) { return wrap ( function ( ) { return write ( subject , key , value ) ; } ) ; } ,
"delete" : function ( key ) { return wrap ( function ( ) { return delete _ ( subject , key ) ; } ) ; } ,
"read" : function ( key ) { return wrap ( function ( ) { return read ( subject , key ) ; } ) ; } ,
"search" : function ( term ) { return wrap ( function ( ) { return search ( subject , term ) ; } ) ; }
} ;
}
memory . implementation _chest = implementation _chest ;
} ) ( memory = storage . memory || ( storage . memory = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
var memory ;
( function ( memory ) {
/ * *
* @ author fenris
* /
var class _chest = /** @class */ ( function ( ) {
function class _chest ( parameters ) {
this . subject = memory . make ( parameters ) ;
}
class _chest . prototype . setup = function ( input ) { return Promise . resolve ( undefined ) ; } ;
class _chest . prototype . clear = function ( ) { memory . clear ( this . subject ) ; return Promise . resolve ( undefined ) ; } ;
class _chest . prototype . write = function ( key , value ) { return Promise . resolve ( memory . write ( this . subject , key , value ) ) ; } ;
class _chest . prototype [ "delete" ] = function ( key ) { memory . delete _ ( this . subject , key ) ; return Promise . resolve ( undefined ) ; } ;
class _chest . prototype . read = function ( key ) { return Promise . resolve ( memory . read ( this . subject , key ) ) ; } ;
class _chest . prototype . search = function ( term ) { return Promise . resolve ( memory . search ( this . subject , term ) ) ; } ;
return class _chest ;
} ( ) ) ;
memory . class _chest = class _chest ;
} ) ( memory = storage . memory || ( storage . memory = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
var filesystem ;
( function ( filesystem ) {
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function make ( parameters ) {
return {
"nodemodule" : require ( "fs" )
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
filesystem . make = make ;
/ * *
* /
function clear ( subject ) {
return Promise . reject ( new Error ( "nope" ) ) ;
}
filesystem . clear = clear ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function write ( subject , path , content ) {
return ( new Promise ( function ( resolve , reject ) {
var exists = subject . nodemodule . existsSync ( path ) ;
subject . nodemodule . writeFile ( path , content , { } , function ( error ) {
if ( error === null ) {
resolve ( exists ) ;
}
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
}
filesystem . write = write ;
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function delete _ ( subject , path ) {
return ( new Promise ( function ( resolve , reject ) {
subject . nodemodule . unlink ( path , function ( error ) {
if ( error === null ) {
resolve ( undefined ) ;
}
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
}
filesystem . delete _ = delete _ ;
/ * *
* @ author fenris
* /
function read ( subject , path ) {
return ( new Promise ( function ( resolve , reject ) {
subject . nodemodule . readFile ( path , { } , function ( error , content ) {
if ( error === null ) {
resolve ( content ) ;
}
else {
reject ( error ) ;
}
} ) ;
} ) ) ;
}
filesystem . read = read ;
/ * *
* /
function implementation _chest ( parameters ) {
var subject = make ( parameters ) ;
return {
"setup" : function ( input ) { return Promise . resolve ( undefined ) ; } ,
"clear" : function ( ) { return clear ( subject ) ; } ,
"write" : function ( key , value ) { return write ( subject , key , value ) ; } ,
"delete" : function ( key ) { return delete _ ( subject , key ) ; } ,
"read" : function ( key ) { return read ( subject , key ) ; } ,
"search" : function ( term ) { return Promise . reject ( "not available" ) ; }
} ;
}
filesystem . implementation _chest = implementation _chest ;
} ) ( filesystem = storage . filesystem || ( storage . filesystem = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
2024-09-12 00:03:29 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
var filesystem ;
( function ( filesystem ) {
/ * *
* @ author fenris
* /
var class _chest = /** @class */ ( function ( ) {
function class _chest ( parameters ) {
this . subject = filesystem . make ( parameters ) ;
}
class _chest . prototype . setup = function ( input ) { return Promise . resolve ( undefined ) ; } ;
class _chest . prototype . clear = function ( ) { return filesystem . clear ( this . subject ) ; } ;
class _chest . prototype . write = function ( key , value ) { return filesystem . write ( this . subject , key , value ) ; } ;
class _chest . prototype [ "delete" ] = function ( key ) { return filesystem . delete _ ( this . subject , key ) ; } ;
class _chest . prototype . read = function ( key ) { return filesystem . read ( this . subject , key ) ; } ;
class _chest . prototype . search = function ( searchterm ) { return Promise . reject ( new Error ( "not available" ) ) ; } ;
return class _chest ;
} ( ) ) ;
filesystem . class _chest = class _chest ;
} ) ( filesystem = storage . filesystem || ( storage . filesystem = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
2024-09-09 12:13:10 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function sql _table _autokey _make ( parameters ) {
return {
"database_implementation" : parameters . database _implementation ,
"table_name" : parameters . table _name ,
"key_name" : parameters . key _name
2024-09-12 00:03:29 +02:00
} ;
2024-09-18 18:17:25 +02:00
}
storage . sql _table _autokey _make = sql _table _autokey _make ;
/ * *
* /
function sql _table _autokey _setup ( subject , description _create _table ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
return [ 2 /*return*/ , subject . database _implementation . query _create _table ( description _create _table ) ] ;
} ) ;
} ) ;
}
storage . sql _table _autokey _setup = sql _table _autokey _setup ;
/ * *
* /
function sql _table _autokey _create ( subject , value ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
return [ 2 /*return*/ , subject . database _implementation . query _insert ( {
"table_name" : subject . table _name ,
"values" : value ,
"returning" : subject . key _name
} ) ] ;
} ) ;
} ) ;
}
storage . sql _table _autokey _create = sql _table _autokey _create ;
/ * *
* /
function sql _table _autokey _update ( subject , key , value ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
return [ 2 /*return*/ , ( subject . database _implementation . query _update ( {
"table_name" : subject . table _name ,
"values" : value ,
"condition" : lib _plankton . string . coin ( "{{key_name}} = $key" , {
"key_name" : subject . database _implementation . wrap _name ( subject . key _name )
} ) ,
"arguments" : {
"key" : key
}
} )
. then ( function ( x ) { return Promise . resolve ( undefined ) ; } ) ) ] ;
} ) ;
} ) ;
}
storage . sql _table _autokey _update = sql _table _autokey _update ;
/ * *
* /
function sql _table _autokey _delete ( subject , key ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 : return [ 4 /*yield*/ , subject . database _implementation . query _delete ( {
"table_name" : subject . table _name ,
"condition" : lib _plankton . string . coin ( "{{key_name}} = $key" , {
"key_name" : subject . database _implementation . wrap _name ( subject . key _name )
} ) ,
"arguments" : {
"key" : key
}
} ) ] ;
case 1 :
_a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( undefined ) ] ;
}
} ) ;
} ) ;
}
storage . sql _table _autokey _delete = sql _table _autokey _delete ;
/ * *
* /
function sql _table _autokey _read ( subject , key ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var rows ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 : return [ 4 /*yield*/ , subject . database _implementation . query _select ( {
"source" : subject . table _name ,
"fields" : null ,
"condition" : lib _plankton . string . coin ( "{{key_name}} = $key" , {
"key_name" : subject . database _implementation . wrap _name ( subject . key _name )
} ) ,
"arguments" : {
"key" : key
}
} ) ] ;
case 1 :
rows = _a . sent ( ) ;
if ( rows . length < 1 ) {
return [ 2 /*return*/ , Promise . reject ( "not found" ) ] ;
}
else if ( rows . length > 1 ) {
return [ 2 /*return*/ , Promise . reject ( "ambiguous" ) ] ;
}
else {
delete rows [ 0 ] [ subject . key _name ] ;
return [ 2 /*return*/ , Promise . resolve ( rows [ 0 ] ) ] ;
}
return [ 2 /*return*/ ] ;
}
} ) ;
} ) ;
}
storage . sql _table _autokey _read = sql _table _autokey _read ;
/ * *
* @ todo correct preview
* /
function sql _table _autokey _search ( subject , term ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var rows ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 : return [ 4 /*yield*/ , subject . database _implementation . query _select ( {
"source" : subject . table _name ,
"fields" : null ,
"condition" : ( ( term === null ) ? null : term . expression ) ,
"arguments" : ( ( term === null ) ? null : term . arguments )
} ) ] ;
case 1 :
rows = _a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( rows
. map ( function ( row ) { return ( {
"key" : row [ subject . key _name ] ,
"preview" : row
} ) ; } ) ) ] ;
}
} ) ;
} ) ;
}
storage . sql _table _autokey _search = sql _table _autokey _search ;
/ * *
* /
function sql _table _autokey _store ( parameters ) {
var subject = sql _table _autokey _make ( parameters ) ;
return {
"setup" : function ( input ) { return sql _table _autokey _setup ( subject , input ) ; } ,
"create" : function ( value ) { return sql _table _autokey _create ( subject , value ) ; } ,
"update" : function ( key , value ) { return sql _table _autokey _update ( subject , key , value ) ; } ,
"delete" : function ( key ) { return sql _table _autokey _delete ( subject , key ) ; } ,
"read" : function ( key ) { return sql _table _autokey _read ( subject , key ) ; } ,
"search" : function ( term ) { return sql _table _autokey _search ( subject , term ) ; }
2024-09-12 00:03:29 +02:00
} ;
2024-09-18 18:17:25 +02:00
}
storage . sql _table _autokey _store = sql _table _autokey _store ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
2024-09-09 12:13:10 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-09 12:13:10 +02:00
* /
2024-09-18 18:17:25 +02:00
var class _sql _table _autokey = /** @class */ ( function ( ) {
function class _sql _table _autokey ( parameters ) {
this . subject = storage . sql _table _autokey _make ( parameters ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
class _sql _table _autokey . prototype . setup = function ( input ) { return storage . sql _table _autokey _setup ( this . subject , input ) ; } ;
class _sql _table _autokey . prototype . create = function ( value ) { return storage . sql _table _autokey _create ( this . subject , value ) ; } ;
class _sql _table _autokey . prototype . update = function ( key , value ) { return storage . sql _table _autokey _update ( this . subject , key , value ) ; } ;
class _sql _table _autokey . prototype [ "delete" ] = function ( key ) { return storage . sql _table _autokey _delete ( this . subject , key ) ; } ;
class _sql _table _autokey . prototype . read = function ( key ) { return storage . sql _table _autokey _read ( this . subject , key ) ; } ;
class _sql _table _autokey . prototype . search = function ( term ) { return storage . sql _table _autokey _search ( this . subject , term ) ; } ;
return class _sql _table _autokey ;
} ( ) ) ;
storage . class _sql _table _autokey = class _sql _table _autokey ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var storage ;
( function ( storage ) {
var sql _table _common ;
( function ( sql _table _common ) {
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function make ( parameters ) {
return {
"database_implementation" : parameters . database _implementation ,
"table_name" : parameters . table _name ,
"key_names" : parameters . key _names
} ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
sql _table _common . make = make ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function key _condition ( subject , key ) {
return {
"condition" : lib _string . coin ( "({{clauses}})" , {
"clauses" : ( subject . key _names
. map ( function ( key _name ) { return lib _plankton . string . coin ( "({{name1}} = $key_{{name2}})" , {
"name1" : subject . database _implementation . wrap _name ( key _name ) ,
"name2" : key _name
} ) ; } )
. join ( " AND " ) )
} ) ,
"arguments" : Object . fromEntries ( subject . key _names
. map ( function ( key _name , index ) { return ( [
lib _plankton . string . coin ( "key_{{name}}" , {
"name" : key _name
} ) ,
key [ index ]
] ) ; } ) )
} ;
}
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function setup ( subject , description _create _table ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
return [ 2 /*return*/ , subject . database _implementation . query _create _table ( description _create _table ) ] ;
} ) ;
} ) ;
}
sql _table _common . setup = setup ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function clear ( subject ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 : return [ 4 /*yield*/ , subject . database _implementation . query _delete ( {
"table_name" : subject . table _name ,
"condition" : "TRUE" ,
"arguments" : { }
} ) ] ;
case 1 :
_a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( undefined ) ] ;
}
} ) ;
} ) ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
sql _table _common . clear = clear ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ todo optimize : avoid read
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function write ( subject , key , value ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var exists , error _1 , field _names , condition ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
_a . trys . push ( [ 0 , 2 , , 3 ] ) ;
return [ 4 /*yield*/ , read ( subject , key ) ] ;
case 1 :
_a . sent ( ) ;
exists = true ;
return [ 3 /*break*/ , 3 ] ;
case 2 :
error _1 = _a . sent ( ) ;
exists = false ;
return [ 3 /*break*/ , 3 ] ;
case 3 :
field _names = Object . keys ( value ) ;
if ( ! ! exists ) return [ 3 /*break*/ , 5 ] ;
return [ 4 /*yield*/ , subject . database _implementation . query _insert ( {
"table_name" : subject . table _name ,
"values" : Object . assign (
// key
Object . fromEntries ( subject . key _names . map ( function ( key _name , index ) { return ( [ key _name , key [ index ] ] ) ; } ) ) ,
// value
value )
} ) ] ;
case 4 :
_a . sent ( ) ;
return [ 3 /*break*/ , 7 ] ;
case 5 :
condition = key _condition ( subject , key ) ;
return [ 4 /*yield*/ , subject . database _implementation . query _update ( {
"table_name" : subject . table _name ,
"values" : value ,
"condition" : condition . condition ,
"arguments" : condition . arguments
} ) ] ;
case 6 :
_a . sent ( ) ;
_a . label = 7 ;
case 7 : return [ 2 /*return*/ , Promise . resolve ( exists ) ] ;
}
} ) ;
} ) ;
}
sql _table _common . write = write ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function delete _ ( subject , key ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var condition ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
condition = key _condition ( subject , key ) ;
return [ 4 /*yield*/ , subject . database _implementation . query _delete ( {
"table_name" : subject . table _name ,
"condition" : condition . condition ,
"arguments" : condition . arguments
} ) ] ;
case 1 :
_a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( undefined ) ] ;
}
} ) ;
} ) ;
}
sql _table _common . delete _ = delete _ ;
/ * *
* /
function read ( subject , key ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var condition , rows ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
condition = key _condition ( subject , key ) ;
return [ 4 /*yield*/ , subject . database _implementation . query _select ( {
"source" : subject . table _name ,
"fields" : null ,
"condition" : condition . condition ,
"arguments" : condition . arguments
} ) ] ;
case 1 :
rows = _a . sent ( ) ;
if ( rows . length < 1 ) {
return [ 2 /*return*/ , Promise . reject ( "not found" ) ] ;
}
else if ( rows . length > 1 ) {
return [ 2 /*return*/ , Promise . reject ( "ambiguous" ) ] ;
}
else {
subject . key _names . forEach ( function ( key _name ) { delete rows [ 0 ] [ key _name ] ; } ) ;
return [ 2 /*return*/ , Promise . resolve ( rows [ 0 ] ) ] ;
}
return [ 2 /*return*/ ] ;
}
} ) ;
} ) ;
}
sql _table _common . read = read ;
/ * *
* @ todo correct preview
* /
function search ( subject , term ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var rows ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 : return [ 4 /*yield*/ , subject . database _implementation . query _select ( {
"source" : subject . table _name ,
"fields" : null ,
"condition" : ( ( term === null ) ? null : term . expression ) ,
"arguments" : ( ( term === null ) ? null : term . arguments )
} ) ] ;
case 1 :
rows = _a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( rows
. map ( function ( row ) { return ( {
"key" : subject . key _names . map ( function ( name ) { return row [ name ] ; } ) ,
"preview" : row
} ) ; } ) ) ] ;
}
} ) ;
} ) ;
}
sql _table _common . search = search ;
/ * *
* /
function chest ( parameters ) {
var subject = make ( parameters ) ;
return {
"setup" : function ( input ) { return setup ( subject , input ) ; } ,
"clear" : function ( ) { return clear ( subject ) ; } ,
"write" : function ( key , value ) { return write ( subject , key , value ) ; } ,
"delete" : function ( key ) { return delete _ ( subject , key ) ; } ,
"read" : function ( key ) { return read ( subject , key ) ; } ,
"search" : function ( term ) { return search ( subject , term ) ; }
} ;
}
sql _table _common . chest = chest ;
} ) ( sql _table _common = storage . sql _table _common || ( storage . sql _table _common = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : storage « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
var storage ;
( function ( storage ) {
var sql _table _common ;
( function ( sql _table _common ) {
/ * *
* @ author fenris
* /
var class _chest = /** @class */ ( function ( ) {
function class _chest ( parameters ) {
this . subject = sql _table _common . make ( parameters ) ;
}
class _chest . prototype . setup = function ( input ) { return sql _table _common . setup ( this . subject , input ) ; } ;
class _chest . prototype . clear = function ( ) { return sql _table _common . clear ( this . subject ) ; } ;
class _chest . prototype . write = function ( key , value ) { return sql _table _common . write ( this . subject , key , value ) ; } ;
class _chest . prototype [ "delete" ] = function ( key ) { return sql _table _common . delete _ ( this . subject , key ) ; } ;
class _chest . prototype . read = function ( key ) { return sql _table _common . read ( this . subject , key ) ; } ;
class _chest . prototype . search = function ( term ) { return sql _table _common . search ( this . subject , term ) ; } ;
return class _chest ;
} ( ) ) ;
sql _table _common . class _chest = class _chest ;
} ) ( sql _table _common = storage . sql _table _common || ( storage . sql _table _common = { } ) ) ;
} ) ( storage = lib _plankton . storage || ( lib _plankton . storage = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-12 00:03:29 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _1 ) {
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function inspection _create ( ) {
return {
"flaws" : [ ] ,
"sub" : [ ]
} ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
shape _1 . inspection _create = inspection _create ;
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function inspection _add ( main , flaw ) {
main . flaws . push ( flaw ) ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
shape _1 . inspection _add = inspection _add ;
2024-09-09 12:13:10 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function inspection _extend ( main , prefix , sub ) {
if ( ( sub . flaws . length <= 0 )
&&
( sub . sub . length <= 0 ) ) {
// do nothing
}
else {
main . sub . push ( { "position" : prefix , "inspection" : sub } ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
shape _1 . inspection _extend = inspection _extend ;
/ * *
* /
function inspection _flatten ( inspection ) {
return ( inspection . flaws
. concat ( inspection . sub
. map ( ( entry ) => ( inspection _flatten ( entry . inspection )
. map ( rest => lib _plankton . string . coin ( "[{{position}}] {{rest}}" , {
"position" : entry . position ,
"rest" : rest
} ) ) ) )
. reduce ( ( x , y ) => x . concat ( y ) , [ ] ) ) ) ;
}
/ * *
* /
var _pool = { } ;
/ * *
* @ todo cache
* /
function construct ( shape ) {
return _pool [ shape . kind ] . construct ( shape . parameters ) ;
}
/ * *
* /
function get _logic ( shape ) {
if ( ! ( shape . kind in _pool ) ) {
throw ( new Error ( "missing shape: " + shape . kind ) ) ;
}
else {
return _pool [ shape . kind ] . logic ( construct ( shape ) ) ;
}
}
/ * *
* /
function inspect ( shape , value ) {
return get _logic ( shape ) . inspect ( inspect , value ) ;
}
shape _1 . inspect = inspect ;
/ * *
* /
function inspect _flat ( shape , value ) {
return inspection _flatten ( inspect ( shape , value ) ) ;
}
shape _1 . inspect _flat = inspect _flat ;
/ * *
* /
function show ( shape ) {
return get _logic ( shape ) . show ( show ) ;
}
shape _1 . show = show ;
/ * *
* /
function to _typescript _type ( shape ) {
return get _logic ( shape ) . to _typescript _type ( to _typescript _type ) ;
}
shape _1 . to _typescript _type = to _typescript _type ;
/ * *
* /
function to _jsonschema ( shape ) {
return get _logic ( shape ) . to _jsonschema ( to _jsonschema ) ;
}
shape _1 . to _jsonschema = to _jsonschema ;
/ * *
* /
function to _oas _schema ( shape ) {
return get _logic ( shape ) . to _oas _schema ( to _oas _schema ) ;
}
shape _1 . to _oas _schema = to _oas _schema ;
/ * *
* /
function example ( shape ) {
return get _logic ( shape ) . example ( example ) ;
}
shape _1 . example = example ;
/ * *
* /
function register ( name , construct , logic ) {
_pool [ name ] = {
"construct" : construct ,
"logic" : logic ,
} ;
}
shape _1 . register = register ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : shape « .
2024-09-12 00:03:29 +02:00
2024-09-18 18:17:25 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _2 ) {
var any ;
( function ( any ) {
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function make ( options = { } ) {
return { } ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
any . make = make ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _2 . inspection _create ( ) ;
return inspection ;
2024-09-12 00:03:29 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function show ( sub _show , subject ) {
return "any" ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
return "any" ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
return { } ;
}
/ * *
* /
function example ( sub _example , subject ) {
return null ;
}
shape _2 . register ( "any" , ( parameters ) => make ( { } ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( any = shape _2 . any || ( shape _2 . any = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _3 ) {
var null _ ;
( function ( null _ ) {
/ * *
* /
function make ( options = { } ) {
return { } ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
null _ . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _3 . inspection _create ( ) ;
if ( ! ( value === null ) ) {
shape _3 . inspection _add ( inspection , "null expected" ) ;
}
else {
// all good
}
return inspection ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function show ( sub _show , subject ) {
return "null" ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
return "null" ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
return {
"type" : "null" ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function example ( sub _example , subject ) {
return null ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
shape _3 . register ( "null" , ( parameters ) => make ( { } ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( null _ = shape _3 . null _ || ( shape _3 . null _ = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : shape « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var shape ;
( function ( shape _4 ) {
var boolean ;
( function ( boolean ) {
/ * *
* /
function make ( options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : undefined ,
"description" : undefined ,
} , options ) ;
return {
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
boolean . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _4 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _4 . inspection _add ( inspection , "null is not allowed" ) ;
}
}
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "boolean" ;
if ( jstype _actual === jstype _expected ) {
// all good
}
else {
shape _4 . inspection _add ( inspection , lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ) ;
}
}
return inspection ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function show ( sub _show , subject ) {
const core = "boolean" ;
return ( subject . soft ? ( "~" + core ) : core ) ;
}
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = "boolean" ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "boolean" ,
} ;
return ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
] ,
}
: core ) ;
}
/ * *
* /
function example ( sub _example , subject ) {
return ( subject . soft ? null : false ) ;
}
shape _4 . register ( "boolean" , ( parameters ) => make ( {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
2024-09-12 00:03:29 +02:00
} ) ) ;
2024-09-18 18:17:25 +02:00
} ) ( boolean = shape _4 . boolean || ( shape _4 . boolean = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : shape « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var shape ;
( function ( shape _5 ) {
var integer ;
( function ( integer ) {
/ * *
* /
function make ( options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : undefined ,
"description" : undefined ,
"minimum" : undefined ,
"maximum" : undefined ,
} , options ) ;
return {
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
"minimum" : ( ( options . minimum === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . minimum ) ) ,
"maximum" : ( ( options . minimum === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . maximum ) ) ,
} ;
}
integer . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _5 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _5 . inspection _add ( inspection , "null is not allowed" ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "number" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
shape _5 . inspection _add ( inspection , lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ) ;
}
else {
if ( isNaN ( parseInt ( value ) ) ) {
shape _5 . inspection _add ( inspection , "value is not parsable to a valid int" ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
// minimum
{
if ( ! lib _plankton . pod . distinguish ( subject . minimum , ( ) => true , x => ( value >= x ) ) ) {
shape _5 . inspection _add ( inspection , lib _plankton . string . coin ( "value is below the minimum of '{{minimum}}'" , {
"minimum" : lib _plankton . pod . cull ( subject . minimum ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
// maximum
{
if ( ! lib _plankton . pod . distinguish ( subject . maximum , ( ) => true , x => ( value <= x ) ) ) {
shape _5 . inspection _add ( inspection , lib _plankton . string . coin ( "value is beyond the maximum of '{{maximum}}'" , {
"maximum" : lib _plankton . pod . cull ( subject . maximum ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return inspection ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function show ( sub _show , subject ) {
const core = "integer" ;
return ( subject . soft ? ( "~" + core ) : core ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = "number" ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "integer" ,
"minimum" : lib _plankton . pod . distinguish ( subject . minimum , ( ) => undefined , x => x ) ,
"maximum" : lib _plankton . pod . distinguish ( subject . maximum , ( ) => undefined , x => x ) ,
} ;
return ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
] ,
}
: core ) ;
}
/ * *
* /
function example ( sub _example , subject ) {
return ( subject . soft ? null : 0 ) ;
}
shape _5 . register ( "integer" , ( parameters ) => make ( {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
"minimum" : parameters . minimum ,
"maximum" : parameters . maximum ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( integer = shape _5 . integer || ( shape _5 . integer = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-09 12:13:10 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _6 ) {
var float ;
( function ( float ) {
/ * *
* /
function make ( options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : undefined ,
"description" : undefined ,
"minimum" : undefined ,
"maximum" : undefined ,
} , options ) ;
return {
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
"minimum" : ( ( options . minimum === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . minimum ) ) ,
"maximum" : ( ( options . minimum === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . maximum ) ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
float . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _6 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _6 . inspection _add ( inspection , "null is not allowed" ) ;
}
}
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "number" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
shape _6 . inspection _add ( inspection , lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ) ;
}
else {
if ( isNaN ( parseInt ( value ) ) ) {
shape _6 . inspection _add ( inspection , "value is not parsable to a valid float" ) ;
}
else {
// minimum
{
if ( ! lib _plankton . pod . distinguish ( subject . minimum , ( ) => true , x => ( value >= x ) ) ) {
shape _6 . inspection _add ( inspection , lib _plankton . string . coin ( "value is below the minimum of '{{minimum}}'" , {
"minimum" : lib _plankton . pod . cull ( subject . minimum ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
// maximum
{
if ( ! lib _plankton . pod . distinguish ( subject . maximum , ( ) => true , x => ( value <= x ) ) ) {
shape _6 . inspection _add ( inspection , lib _plankton . string . coin ( "value is beyond the maximum of '{{maximum}}'" , {
"maximum" : lib _plankton . pod . cull ( subject . maximum ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
}
}
}
return inspection ;
2024-09-09 12:13:10 +02:00
}
2024-09-18 18:17:25 +02:00
/ * *
* /
function show ( sub _show , subject ) {
const core = "float" ;
return ( subject . soft ? ( "~" + core ) : core ) ;
}
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = "number" ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
2024-09-12 00:03:29 +02:00
} ;
2024-09-18 18:17:25 +02:00
const core = {
"type" : "float" ,
"minimum" : lib _plankton . pod . distinguish ( subject . minimum , ( ) => undefined , x => x ) ,
"maximum" : lib _plankton . pod . distinguish ( subject . maximum , ( ) => undefined , x => x ) ,
} ;
return ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
] ,
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
: core ) ;
}
/ * *
* /
function example ( sub _example , subject ) {
return ( subject . soft ? null : 0 ) ;
}
shape _6 . register ( "float" , ( parameters ) => make ( {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
"minimum" : parameters . minimum ,
"maximum" : parameters . maximum ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( float = shape _6 . float || ( shape _6 . float = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-09 12:13:10 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 12:13:10 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 12:13:10 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 12:13:10 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 12:13:10 +02:00
* /
2024-09-12 00:03:29 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _7 ) {
var string ;
( function ( string ) {
/ * *
* /
function make ( options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : undefined ,
"description" : undefined ,
"pattern" : undefined ,
"min_length" : undefined ,
"max_lenth" : undefined ,
} , options ) ;
return {
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
"pattern" : ( ( options . pattern === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . pattern ) ) ,
"min_length" : ( ( options . min _length === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . min _length ) ) ,
"max_length" : ( ( options . max _length === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . max _length ) ) ,
} ;
}
string . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _7 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
2024-09-09 12:13:10 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
shape _7 . inspection _add ( inspection , "null is not allowed" ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "string" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
shape _7 . inspection _add ( inspection , lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
const value _ = value ;
// pattern
{
if ( ! lib _plankton . pod . distinguish ( subject . pattern , ( ) => true , x => ( new RegExp ( x ) ) . test ( value _ ) ) ) {
shape _7 . inspection _add ( inspection , lib _plankton . string . coin ( "string does not match the pattern '{{pattern}}'" , {
"pattern" : lib _plankton . pod . cull ( subject . pattern ) ,
} ) ) ;
}
else {
// do nothing
}
}
// min_length
{
if ( ! lib _plankton . pod . distinguish ( subject . min _length , ( ) => true , x => ( value _ . length >= x ) ) ) {
shape _7 . inspection _add ( inspection , lib _plankton . string . coin ( "string is shorter than '{{min_length}}'" , {
"min_length" : lib _plankton . pod . cull ( subject . min _length ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
// max_length
{
if ( ! lib _plankton . pod . distinguish ( subject . max _length , ( ) => true , x => ( value _ . length <= x ) ) ) {
shape _7 . inspection _add ( inspection , lib _plankton . string . coin ( "string is longer than '{{max_length}}'" , {
"min_length" : lib _plankton . pod . cull ( subject . max _length ) . toFixed ( 0 ) ,
} ) ) ;
}
else {
// do nothing
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
return inspection ;
}
/ * *
* /
function show ( sub _show , subject ) {
const core = "string" ;
return ( subject . soft ? ( "~" + core ) : core ) ;
}
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = "string" ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "string" ,
// TODO: transform?
"pattern" : lib _plankton . pod . distinguish ( subject . pattern , ( ) => undefined , x => x ) ,
"minLength" : lib _plankton . pod . distinguish ( subject . min _length , ( ) => undefined , x => x ) ,
"maxLenth" : lib _plankton . pod . distinguish ( subject . max _length , ( ) => undefined , x => x ) ,
} ;
return ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
] ,
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
: core ) ;
}
/ * *
* /
function example ( sub _example , subject ) {
return ( subject . soft ? null : "" ) ;
}
shape _7 . register ( "string" , ( parameters ) => make ( {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
"pattern" : parameters . pattern ,
"min_length" : parameters . min _length ,
"max_length" : parameters . max _length ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _chema ) => to _jsonschema ( sub _to _oas _chema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
2024-09-12 00:03:29 +02:00
} ) ) ;
2024-09-18 18:17:25 +02:00
} ) ( string = shape _7 . string || ( shape _7 . string = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-09 21:00:52 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _8 ) {
var email ;
( function ( email ) {
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function make ( options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : undefined ,
"description" : undefined ,
} , options ) ;
return {
"core" : {
"kind" : "string" ,
"parameters" : {
"soft" : options . soft ,
"defaultvalue" : options . defaultvalue ,
"description" : options . description ,
"pattern" : "[^@]*@[^@]*" ,
"min_length" : 0 ,
"max_length" : 255 ,
}
} ,
} ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
email . make = make ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function inspect ( sub _inspect , subject , value ) {
return sub _inspect ( subject . core , value ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function show ( sub _show , subject ) {
return sub _show ( subject . core ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _typescript _type ( sub _to _typescript _type , subject ) {
return sub _to _typescript _type ( subject . core ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _jsonschema ( sub _to _jsonschema , subject ) {
return sub _to _jsonschema ( subject . core ) ;
2024-09-12 00:03:29 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function example ( sub _example , subject ) {
return sub _example ( subject . core ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
shape _8 . register ( "email" , ( parameters ) => make ( {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _chema ) => to _jsonschema ( sub _to _oas _chema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( email = shape _8 . email || ( shape _8 . email = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-09 21:00:52 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _9 ) {
var list _ ;
( function ( list _ ) {
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function make ( shape _element , options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : lib _plankton . pod . make _empty ( ) ,
"description" : lib _plankton . pod . make _empty ( ) ,
} , options ) ;
return {
"shape_element" : shape _element ,
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
list _ . make = make ;
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _9 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _9 . inspection _add ( inspection , "null is not allowed" ) ;
}
}
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "object" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ;
}
else {
if ( ! ( value instanceof Array ) ) {
shape _9 . inspection _add ( inspection , "value does not seem to be an array-instance" ) ;
}
else {
value . forEach ( ( element , index ) => {
shape _9 . inspection _extend ( inspection , lib _plankton . string . coin ( "element #{{index}}" , {
"index" : index . toFixed ( 0 ) ,
} ) ,
// subject.shape_element.inspect(element)
sub _inspect ( subject . shape _element , element ) ) ;
} ) ;
}
}
}
return inspection ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function show ( sub _show , subject ) {
const core = lib _plankton . string . coin ( "list<{{element}}>" , {
"element" : sub _show ( subject . shape _element ) ,
} ) ;
return ( subject . soft ? ( "~" + core ) : core ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = lib _plankton . string . coin ( "Array<{{element}}>" , {
"element" : sub _to _typescript _type ( subject . shape _element ) ,
} ) ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "array" ,
"items" : sub _to _jsonschema ( subject . shape _element ) ,
} ;
return ( ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
]
}
: core ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function example ( sub _example , subject ) {
return ( subject . soft
? null
: [
sub _example ( subject . shape _element )
] ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
shape _9 . register ( "list" , ( parameters ) => make ( parameters . shape _element , {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( list _ = shape _9 . list _ || ( shape _9 . list _ = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-09 21:00:52 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _10 ) {
var map ;
( function ( map ) {
/ * *
* /
function make ( shape _key , shape _value , options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : lib _plankton . pod . make _empty ( ) ,
"description" : lib _plankton . pod . make _empty ( ) ,
} , options ) ;
return {
"shape_key" : shape _key ,
"shape_value" : shape _value ,
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
} ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
map . make = make ;
/ * *
* /
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _10 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _10 . inspection _add ( inspection , "null is not allowed" ) ;
}
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "object" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ;
}
else {
Object . entries ( value )
. forEach ( ( [ key , value ] ) => {
shape _10 . inspection _extend ( inspection , lib _plankton . string . coin ( "key '{{key}}'" , {
"key" : String ( key ) ,
} ) , sub _inspect ( subject . shape _key , key ) ) ;
shape _10 . inspection _extend ( inspection , lib _plankton . string . coin ( "value for '{{key}}'" , {
"key" : String ( key ) ,
} ) , sub _inspect ( subject . shape _value , value ) ) ;
} ) ;
}
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
return inspection ;
}
/ * *
* /
function show ( sub _show , subject ) {
const core = lib _plankton . string . coin ( "map<{{key}},{{value}}>" , {
"key" : sub _show ( subject . shape _key ) ,
"value" : sub _show ( subject . shape _value ) ,
} ) ;
return ( subject . soft ? ( "~" + core ) : core ) ;
}
/ * *
* /
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = lib _plankton . string . coin ( "Record<{{key}},{{value}}>" , {
"key" : sub _to _typescript _type ( subject . shape _key ) ,
"value" : sub _to _typescript _type ( subject . shape _key ) ,
} ) ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
}
/ * *
* /
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "object" ,
"additionalProperties" : sub _to _jsonschema ( subject . shape _value ) ,
"properties" : { } ,
"required" : [ ] ,
} ;
return ( ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
]
}
: core ) ) ;
}
/ * *
* /
function example ( sub _example , subject ) {
return ( subject . soft
? null
: { } ) ;
}
shape _10 . register ( "map" , ( parameters ) => make ( parameters . shape _key , parameters . shape _value , {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( map = shape _10 . map || ( shape _10 . map = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-09 21:00:52 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : shape « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : shape « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : shape « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var shape ;
( function ( shape _11 ) {
var record ;
( function ( record ) {
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function make ( fields _raw , options = { } ) {
options = Object . assign ( {
"soft" : false ,
"defaultvalue" : lib _plankton . pod . make _empty ( ) ,
"description" : lib _plankton . pod . make _empty ( ) ,
} , options ) ;
return {
"fields" : fields _raw . map ( field _raw => ( {
"name" : field _raw . name ,
"shape" : field _raw . shape ,
"required" : ( field _raw . required ? ? true )
} ) ) ,
"soft" : options . soft ,
"defaultvalue" : ( ( options . defaultvalue === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . defaultvalue ) ) ,
"description" : ( ( options . description === undefined )
? lib _plankton . pod . make _empty ( )
: lib _plankton . pod . make _filled ( options . description ) ) ,
} ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
record . make = make ;
2024-09-09 21:00:52 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function inspect ( sub _inspect , subject , value ) {
let inspection = shape _11 . inspection _create ( ) ;
if ( value === null ) {
if ( subject . soft ) {
// all good
}
else {
shape _11 . inspection _add ( inspection , "null is not allowed" ) ;
}
}
else {
const jstype _actual = typeof ( value ) ;
const jstype _expected = "object" ;
if ( ! ( jstype _actual === jstype _expected ) ) {
shape _11 . inspection _add ( inspection , lib _plankton . string . coin ( "expected JS-type '{{expected}}' but got '{{actual}}'" , {
"expected" : jstype _expected ,
"actual" : jstype _actual ,
} ) ) ;
}
else {
subject . fields . forEach ( field => {
if ( ! ( field . name in value ) ) {
if ( field . required ) {
shape _11 . inspection _add ( inspection , "missing field '" + field . name + "'" ) ;
}
else {
// do nothing
}
}
else {
const value _ = value [ field . name ] ;
shape _11 . inspection _extend ( inspection , ( "field '" + field . name + "'" ) , sub _inspect ( field . shape , value _ ) ) ;
}
} ) ;
// extra fields
{
const allowed = subject . fields . map ( field => field . name ) ;
( Object . keys ( value )
. filter ( name => ( ! allowed . includes ( name ) ) )
. forEach ( name => {
shape _11 . inspection _add ( inspection , "extra field '" + name + "'" ) ;
} ) ) ;
}
}
}
return inspection ;
2024-09-09 21:00:52 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function show ( sub _show , subject ) {
const core = lib _plankton . string . coin ( "record<{{fields}}>" , {
"fields" : ( subject . fields
. map ( field => lib _plankton . string . coin ( "{{prefix}}{{name}}:{{shape}}" , {
"prefix" : ( field . required ? "" : "?" ) ,
"name" : field . name ,
"shape" : sub _show ( field . shape ) ,
} ) )
. join ( "," ) ) ,
} ) ;
return ( subject . soft ? ( "~" + core ) : core ) ;
2024-09-09 21:00:52 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _typescript _type ( sub _to _typescript _type , subject ) {
const core = lib _plankton . string . coin ( "{{{fields}}}" , {
"fields" : ( subject . fields
. map ( field => lib _plankton . string . coin ( "{{name}}{{infix}}{{shape}}" , {
"name" : field . name ,
"infix" : ( field . required ? ":" : "?:" ) ,
"shape" : sub _to _typescript _type ( field . shape ) ,
} ) )
. join ( ";" ) ) ,
} ) ;
return ( subject . soft ? ( "(null | " + core + ")" ) : core ) ;
2024-09-09 21:00:52 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function to _jsonschema ( sub _to _jsonschema , subject ) {
const common = {
"description" : lib _plankton . pod . distinguish ( subject . description , ( ) => undefined , x => x ) ,
"default" : lib _plankton . pod . distinguish ( subject . defaultvalue , ( ) => undefined , x => x ) ,
} ;
const core = {
"type" : "object" ,
"additionalProperties" : false ,
"properties" : ( Object . fromEntries ( subject . fields
. map ( field => ( [
field . name ,
sub _to _jsonschema ( field . shape )
] ) ) ) ) ,
"required" : ( subject . fields
. filter ( field => field . required )
. map ( field => field . name ) ) ,
} ;
return ( ( subject . soft
? {
"anyOf" : [
Object . assign ( { "type" : "null" } , common ) ,
Object . assign ( core , common ) ,
]
}
: core ) ) ;
2024-09-12 00:03:29 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function example ( sub _example , subject ) {
return ( subject . soft
? null
: ( Object . fromEntries ( subject . fields
. filter ( field => field . required )
. map ( field => ( [
field . name ,
sub _example ( field . shape )
] ) ) ) ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
shape _11 . register ( "record" , ( parameters ) => make ( ( parameters . fields
. map ( field _raw => ( {
"name" : field _raw . name ,
"shape" : field _raw . shape ,
"required" : field _raw . required ,
} ) ) ) , {
"soft" : parameters . soft ,
"description" : parameters . description ,
"defaultvalue" : parameters . defaultvalue ,
} ) , ( subject ) => ( {
"inspect" : ( sub _inspect , value ) => inspect ( sub _inspect , subject , value ) ,
"show" : ( sub _show ) => show ( sub _show , subject ) ,
"to_typescript_type" : ( sub _to _typescript _type ) => to _typescript _type ( sub _to _typescript _type , subject ) ,
"to_jsonschema" : ( sub _to _jsonschema ) => to _jsonschema ( sub _to _jsonschema , subject ) ,
"to_oas_schema" : ( sub _to _oas _schema ) => to _jsonschema ( sub _to _oas _schema , subject ) ,
"example" : ( sub _example ) => example ( sub _example , subject ) ,
} ) ) ;
} ) ( record = shape _11 . record || ( shape _11 . record = { } ) ) ;
} ) ( shape = lib _plankton . shape || ( lib _plankton . shape = { } ) ) ;
2024-09-09 21:00:52 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : www _form « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : www _form « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : www _form « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : www _form « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var www _form ;
( function ( www _form ) {
2024-09-09 21:00:52 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function encode ( source ) {
return ( Object . entries ( source )
. map ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
return ( key + "=" + encodeURIComponent ( value ) ) ;
} )
. join ( "&" ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
www _form . encode = encode ;
2024-09-09 21:00:52 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function decode ( target ) {
return ( Object . fromEntries ( target . split ( "&" )
. map ( function ( part ) {
var components = part . split ( "=" ) ;
var key = components [ 0 ] ;
var value = decodeURIComponent ( components . slice ( 1 ) . join ( "=" ) ) ;
return [ key , value ] ;
} ) ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
www _form . decode = decode ;
} ) ( www _form = lib _plankton . www _form || ( lib _plankton . www _form = { } ) ) ;
2024-09-09 21:00:52 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : www _form « .
2024-09-09 21:00:52 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : www _form « is free software : you can redistribute it and / or modify
2024-09-09 21:00:52 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : www _form « is distributed in the hope that it will be useful ,
2024-09-09 21:00:52 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
2024-09-11 17:24:20 +02:00
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : www _form « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-11 17:24:20 +02:00
* /
2024-09-12 00:03:29 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var www _form ;
( function ( www _form ) {
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
var class _www _form = /** @class */ ( function ( ) {
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function class _www _form ( ) {
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ implementation
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
class _www _form . prototype . encode = function ( source ) {
return www _form . encode ( source ) ;
} ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ implementation
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
class _www _form . prototype . decode = function ( target ) {
return www _form . decode ( target ) ;
} ;
return class _www _form ;
} ( ) ) ;
www _form . class _www _form = class _www _form ;
} ) ( www _form = lib _plankton . www _form || ( lib _plankton . www _form = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : url « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : url « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : url « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : url « . If not , see < http : //www.gnu.org/licenses/>.
* /
2024-09-11 17:24:20 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : url « .
2024-09-11 17:24:20 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : url « is free software : you can redistribute it and / or modify
2024-09-11 17:24:20 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : url « is distributed in the hope that it will be useful ,
2024-09-11 17:24:20 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
2024-09-09 21:00:52 +02:00
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : url « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-09 21:00:52 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var url ;
( function ( url _1 ) {
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function encode ( url ) {
let result = "" ;
// scheme
{
if ( url . scheme !== null ) {
result += ( url . scheme + ":" ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
// host
{
if ( url . host !== null ) {
result += "//" ;
// username
{
if ( url . username !== null ) {
result += url . username ;
// password
{
if ( url . password !== null ) {
result += ( ":" + url . password ) ;
}
}
result += "@" ;
}
}
result += url . host ;
}
}
// port
{
if ( url . port !== null ) {
result += ( ":" + url . port . toString ( ) ) ;
}
}
// path
{
if ( url . path !== null ) {
result += url . path ;
}
}
// query
{
if ( url . query !== null ) {
2024-09-19 13:34:07 +02:00
result += ( "?" + url . query ) ;
2024-09-18 18:17:25 +02:00
}
}
// hash
{
if ( url . hash !== null ) {
result += ( "#" + url . hash ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
return result ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
url _1 . encode = encode ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-18 18:17:25 +02:00
* @ todo arguments
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function decode ( url _raw ) {
const builtin _url = new URL ( url _raw ) ;
return {
"scheme" : builtin _url . protocol . slice ( 0 , - 1 ) ,
"host" : builtin _url . hostname ,
"username" : ( ( builtin _url . username !== "" )
?
builtin _url . username
:
null ) ,
"password" : ( ( builtin _url . password !== "" )
?
builtin _url . password
:
null ) ,
"port" : ( ( builtin _url . port !== "" )
?
parseInt ( builtin _url . port )
:
null ) ,
"path" : builtin _url . pathname ,
"query" : builtin _url . search . slice ( 1 ) ,
"hash" : ( ( builtin _url . hash !== "" )
?
builtin _url . hash . slice ( 1 )
:
null ) ,
} ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
url _1 . decode = decode ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function implementation _code ( ) {
return {
"encode" : encode ,
"decode" : decode ,
} ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
url _1 . implementation _code = implementation _code ;
} ) ( url = lib _plankton . url || ( lib _plankton . url = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : url « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : url « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : url « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : url « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var url ;
( function ( url ) {
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
class class _url {
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
constructor ( ) {
}
/ * *
2024-09-18 18:17:25 +02:00
* @ implementation
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
encode ( x ) {
return url . encode ( x ) ;
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ implementation
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
decode ( x ) {
return url . decode ( x ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
url . class _url = class _url ;
} ) ( url = lib _plankton . url || ( lib _plankton . url = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : random « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : random « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : random « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : random « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var random ;
( function ( random ) {
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function sequence ( n ) {
return ( ( n <= 0 ) ? [ ] : sequence ( n - 1 ) . concat ( [ n - 1 ] ) ) ;
2024-09-09 21:00:52 +02:00
}
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function interpolate ( x , y , t ) {
return ( ( ( 1 - t ) * x ) + ( t * y ) ) ;
2024-09-09 21:00:52 +02:00
}
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
var _statestack = [
{
"builtin" : true ,
"seed" : Date . now ( )
} ,
] ;
/ * *
* @ author fenris
* /
function state _get ( ) {
return _statestack [ _statestack . length - 1 ] ;
}
/ * *
* @ author fenris
* /
function state _push ( state ) {
_statestack . push ( state ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . state _push = state _push ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function state _pop ( ) {
if ( _statestack . length <= 1 ) {
throw ( new Error ( "no state to pop" ) ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
return _statestack . pop ( ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . state _pop = state _pop ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function seed _tick ( ) {
var state = state _get ( ) ;
state . seed = ( ( state . seed << 8 ) % 99767 ) ;
2024-09-09 21:00:52 +02:00
}
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function seed _get ( tick ) {
if ( tick === void 0 ) { tick = true ; }
if ( tick ) {
seed _tick ( ) ;
}
else {
// do nothing
}
return state _get ( ) . seed ;
2024-09-09 21:00:52 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* returns a random floating point number in the interval [ 0 , 1 [
*
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _unit ( ) {
if ( state _get ( ) . builtin ) {
return Math . random ( ) ;
}
else {
return ( ( Math . sin ( seed _get ( ) << 8 ) + 1 ) / 2 ) ;
}
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _unit = generate _unit ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a random boolean value
*
* @ param { float } [ probability ] the probability for the return - value "true" ; default : 0.5
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _boolean ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"probability" : 0.5
} , options ) ;
return ( generate _unit ( ) < options . probability ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _boolean = generate _boolean ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a random integer number in the interval [ a , b ]
*
* @ param { int } [ minimum ] the left side of the halfopen interval ( i . e . the smallest included value in the range )
* @ param { int } [ minimum ] the right side of the halfopen interval ( i . e . the smallest excluded value in the range )
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-09 21:00:52 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _integer ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"minimum" : 0 ,
"maximum" : ( ( 1 << 16 ) - 1 )
} , options ) ;
return Math . floor ( interpolate ( options . minimum , options . maximum + 1 , generate _unit ( ) ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _integer = generate _integer ;
random . generate _int = generate _integer ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a random floating point number in the given interval
*
2024-09-09 21:00:52 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _float ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"minimum" : 0.0 ,
"maximum" : 1.0
} , options ) ;
return interpolate ( options . minimum , options . maximum , generate _unit ( ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _float = generate _float ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* returns a random date
*
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _date ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"minimum" : new Date ( 0 ) ,
"maximum" : new Date ( Date . now ( ) )
} , options ) ;
return ( new Date ( generate _integer ( {
"minimum" : options . minimum . getTime ( ) ,
"maximum" : options . maximum . getTime ( )
} ) ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _date = generate _date ;
2024-09-09 21:00:52 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _hexdigit ( ) {
return ( this . generate _integer ( { "minimum" : 0 , "maximum" : 15 } ) . toString ( 16 ) . toUpperCase ( ) ) ;
2024-09-09 21:00:52 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _hexdigit = generate _hexdigit ;
2024-09-09 21:00:52 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* generates a random string with an optional prefix
*
2024-09-09 21:00:52 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _string ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"length" : 8
} , options ) ;
return ( sequence ( options . length )
. map ( function ( x ) { return generate _integer ( { "minimum" : 65 , "maximum" : 90 } ) ; } )
. map ( function ( x ) { return String . fromCharCode ( x ) ; } )
. join ( "" )
. toLowerCase ( ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _string = generate _string ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* chooses a value randomly from a list of values with weights ( a higher weight means a higher probability to be chosen )
*
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function choose _weighted ( sets ) {
var sum = sets . reduce ( function ( sum , set _ ) { return ( sum + set _ . weight ) ; } , 0 ) ;
if ( sum === 0 ) {
throw ( new Error ( "weights sum up to zero; are all zero or are negative weights included?" ) ) ;
}
else {
var position _1 = generate _unit ( ) ;
return ( sets . reduce ( function ( current , set _ ) {
var next = { "index" : null , "value" : null } ;
next . index = ( current . index + ( set _ . weight / sum ) ) ;
next . value = ( ( ( current . index <= position _1 ) && ( position _1 < next . index ) )
? set _ . value
: current . value ) ;
return next ;
} , { "index" : 0 , "value" : null } ) . value ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . choose _weighted = choose _weighted ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* chooses a value randomly from a list of values with equal probabilities
*
2024-09-12 00:03:29 +02:00
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function choose _uniformly ( values ) {
return ( choose _weighted ( values . map ( function ( value ) { return ( { "weight" : 1 , "value" : value } ) ; } ) ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . choose _uniformly = choose _uniformly ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function shuffle ( list ) {
var list _ = [ ] ;
list . forEach ( function ( element ) {
var index = generate _integer ( { "minimum" : 0 , "maximum" : list _ . length } ) ;
list _ . splice ( index , 0 , element ) ;
} ) ;
return list _ ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . shuffle = shuffle ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _vowel ( ) {
return ( choose _weighted ( [
{ "weight" : 1.0 , "value" : "u" } ,
{ "weight" : 4.0 , "value" : "o" } ,
{ "weight" : 6.0 , "value" : "a" } ,
{ "weight" : 4.0 , "value" : "e" } ,
{ "weight" : 1.0 , "value" : "i" } ,
] ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _vowel = generate _vowel ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _halfvowel ( ) {
return ( choose _weighted ( [
{ "weight" : 4.0 , "value" : "y" } ,
{ "weight" : 1.0 , "value" : "w" } ,
] ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _halfvowel = generate _halfvowel ;
2024-09-09 21:00:52 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _consonant ( ) {
return ( choose _weighted ( [
{ "weight" : 5.0 , "value" : "l" } ,
{ "weight" : 5.0 , "value" : "m" } ,
{ "weight" : 5.0 , "value" : "n" } ,
{ "weight" : 4.0 , "value" : "b" } ,
{ "weight" : 4.0 , "value" : "p" } ,
{ "weight" : 4.0 , "value" : "d" } ,
{ "weight" : 2.0 , "value" : "dj" } ,
{ "weight" : 4.0 , "value" : "t" } ,
{ "weight" : 2.0 , "value" : "tc" } ,
{ "weight" : 4.0 , "value" : "g" } ,
{ "weight" : 4.0 , "value" : "k" } ,
{ "weight" : 3.0 , "value" : "v" } ,
{ "weight" : 3.0 , "value" : "f" } ,
{ "weight" : 3.0 , "value" : "z" } ,
{ "weight" : 3.0 , "value" : "s" } ,
{ "weight" : 3.0 , "value" : "j" } ,
{ "weight" : 3.0 , "value" : "c" } ,
{ "weight" : 2.0 , "value" : "r" } ,
{ "weight" : 1.0 , "value" : "h" } ,
{ "weight" : 1.0 , "value" : "x" } ,
] ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _consonant = generate _consonant ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _letter ( ) {
return choose _uniformly ( [
"a" ,
"b" ,
"c" ,
"d" ,
"e" ,
"f" ,
"g" ,
"h" ,
"i" ,
"j" ,
"k" ,
"l" ,
"m" ,
"n" ,
"o" ,
"p" ,
"q" ,
"r" ,
"s" ,
"t" ,
"u" ,
"v" ,
"w" ,
"x" ,
"y" ,
"z" ,
] ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _letter = generate _letter ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _syllable ( ) {
return ( generate _consonant ( )
+
generate _vowel ( )
+
( generate _boolean ( { "probability" : 1 / 8 } )
? generate _halfvowel ( )
: "" )
+
( generate _boolean ( { "probability" : 1 / 4 } )
? generate _consonant ( )
: "" ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _syllable = generate _syllable ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _word ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"syllable_count_minimum" : 2 ,
"syllable_count_maximum" : 5
} , options ) ;
return ( sequence ( generate _integer ( {
"minimum" : options . syllable _count _minimum ,
"maximum" : options . syllable _count _maximum
} ) )
. map ( function ( x ) { return generate _syllable ( ) ; } )
. join ( "" ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _word = generate _word ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _text ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"word_count" : generate _integer ( { "minimum" : 20 , "maximum" : 80 } )
} , options ) ;
return sequence ( options . word _count ) . map ( function ( x ) { return generate _word ( ) ; } ) . join ( " " ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _text = generate _text ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _city ( ) {
var str = (
// prefix
( generate _boolean ( { "probability" : 0.25 } )
? choose _uniformly ( [
"unter" ,
"ober" ,
"mittel" ,
"groß" ,
"klein" ,
"neu" ,
"alt" ,
] )
: "" )
+
// infix
choose _uniformly ( [
"stein" ,
"frei" ,
"berns" ,
"kirch" ,
"fels" ,
"weiden" ,
"buchen" ,
"eichen" ,
"eiben" ,
"linden" ,
"ulmen" ,
"birken" ,
"eschen" ,
"erlen" ,
"grün" ,
"kreuz" ,
"wald" ,
"reichen" ,
"lieben" ,
"schön" ,
"heinrichs" ,
"friedrichs" ,
"johann" ,
"walters" ,
"günthers" ,
"rupperts" ,
"wilhems" ,
"albert" ,
] )
+
// suffix
choose _uniformly ( [
"feld" ,
"thal" ,
"berg" ,
"burg" ,
"stadt" ,
"dorf" ,
"heim" ,
"bach" ,
"au" ,
"rode" ,
"ing" ,
"nitz" ,
"hausen" ,
] ) ) ;
return ( str [ 0 ] . toUpperCase ( ) + str . substring ( 1 ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _city = generate _city ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _street ( ) {
var str = (
// prefix
choose _uniformly ( [
"haupt" ,
"neben" ,
"bahnhof" ,
"markt" ,
"augustus" ,
"wilhelm" ,
"albert" ,
"friedrich" ,
"maximilian" ,
"wagner" ,
"linden" ,
"eichen" ,
"buchen" ,
"tannen" ,
"pappel" ,
"ulmen" ,
"eschen" ,
] )
+
// core
choose _uniformly ( [
"straße" ,
"weg" ,
"allee" ,
"pfad" ,
"hain" ,
"anger" ,
"passage" ,
"platz" ,
] )
+
// number
( " " + generate _integer ( { "minimum" : 1 , "maximum" : 80 } ) . toString ( ) ) ) ;
return ( str [ 0 ] . toUpperCase ( ) + str . substring ( 1 ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _street = generate _street ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _guid ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"with_braces" : false
} , options ) ;
/ *
return (
[ 8 , 4 , 4 , 4 , 12 ]
. map ( length => sequence ( length ) . map ( _ => this . generate _hexdigit ( ) ) . join ( "" ) )
. join ( "-" )
) ;
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
var guid = ( "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( new RegExp ( "[xy]" , "g" ) , function ( symbol ) {
var r = generate _integer ( { "minimum" : 0 , "maximum" : 15 } ) ;
var v = ( ( symbol == "y" ) ? ( ( r & 0x3 ) | 0x8 ) : r ) ;
return ( v . toString ( 16 ) . toUpperCase ( ) ) ;
} ) ) ;
return ( options . with _braces
? ( "{" + guid + "}" )
: guid ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _guid = generate _guid ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _domain ( options ) {
if ( options === void 0 ) { options = { } ; }
options = lib _plankton . base . object _merge ( {
"steps" : 3
} , options ) ;
return ( sequence ( generate _integer ( { "minimum" : 1 , "maximum" : options . steps } ) )
. map ( function ( ) { return generate _word ( ) ; } )
// tld
. concat ( sequence ( generate _integer ( { "minimum" : 2 , "maximum" : 3 } ) )
. map ( function ( ) { return generate _letter ( ) ; } )
. join ( "" ) ) ) . join ( "." ) ;
2024-09-12 00:03:29 +02:00
}
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _url ( ) {
return ( lib _plankton . url . encode ( {
"scheme" : choose _uniformly ( [
"http" ,
"https" ,
"git" ,
"smb" ,
"ftp" ,
] ) ,
"host" : generate _domain ( ) ,
"username" : null ,
"password" : null ,
"port" : generate _integer ( { "minimum" : 20 , "maximum" : 99999 } ) ,
"path" : ( "/"
+
( sequence ( generate _integer ( { "minimum" : 0 , "maximum" : 3 } ) )
. map ( function ( ) { return generate _word ( ) ; } )
. join ( "/" ) ) ) ,
"query" : choose _uniformly ( [
null ,
lib _plankton . www _form . encode ( Object . fromEntries ( sequence ( generate _integer ( { "minimum" : 0 , "maximum" : 3 } ) )
. map ( function ( ) { return ( [
generate _word ( ) ,
generate _integer ( ) . toFixed ( 0 ) ,
] ) ; } ) ) ) ,
] ) ,
"hash" : null
} ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _url = generate _url ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _email _address ( ) {
return (
// user
generate _word ( )
2024-09-12 00:03:29 +02:00
+
2024-09-18 18:17:25 +02:00
"@"
2024-09-12 00:03:29 +02:00
+
2024-09-18 18:17:25 +02:00
generate _domain ( { "steps" : 1 } ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _email _address = generate _email _address ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _telephone _number ( ) {
return (
// country prefix
( "+"
+
generate _integer ( { "minimum" : 1 , "maximum" : 999 } ) . toFixed ( 0 ) )
+
" "
+
// provider
+ ( sequence ( 3 )
. map ( function ( ) { return generate _integer ( { "minimum" : 0 , "maximum" : 9 } ) ; } )
. join ( "" ) )
+
" "
+
// custom
+ ( sequence ( 7 )
. map ( function ( ) { return generate _integer ( { "minimum" : 0 , "maximum" : 9 } ) ; } )
. join ( "" ) ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _telephone _number = generate _telephone _number ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
function generate _time ( ) {
return {
"hour" : generate _integer ( { "minimum" : 0 , "maximum" : 23 } ) ,
"minute" : generate _integer ( { "minimum" : 0 , "maximum" : 59 } ) ,
"second" : generate _integer ( { "minimum" : 0 , "maximum" : 59 } )
} ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _time = generate _time ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
2024-09-18 18:17:25 +02:00
* @ deprecated
* @ todo remove
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function generate _for _shape ( shape ) {
throw ( new Error ( "deprecated! use lib_shape!" ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
random . generate _for _shape = generate _for _shape ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
/ *
export function generate _for _shape _ (
shape : lib _plankton . shape . type _shape
) : any
{
switch ( shape . name ) {
case "boolean" : {
return generate _boolean ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "int" : {
const min = shape . parameters [ "min" ] ;
const max = shape . parameters [ "max" ] ;
return generate _integer (
{
"minimum" : ( ( min == null ) ? undefined : min ) ,
"maximum" : ( ( max == null ) ? undefined : max ) ,
}
) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "float" : {
return generate _float (
{
"minimum" : shape . parameters [ "min" ] ,
"maximum" : shape . parameters [ "max" ] ,
}
) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "string" : {
return generate _string ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "url" : {
return generate _url ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "email" : {
return generate _email _address ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "time" : {
return generate _time ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "array" : {
return (
sequence ( generate _integer ( { "minimum" : 2 , "maximum" : 5 } ) )
. map ( ( ) => generate _for _shape _ ( shape . parameters [ "shape_element" ] ) )
) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "object" : {
return Object . fromEntries (
shape . parameters [ "fields" ]
. map (
field => ( [
field [ "name" ] ,
generate _for _shape _ ( field [ "shape" ] ) ,
] )
)
) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "date" : {
return generate _date ( ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "enumeration" : {
return choose _uniformly < any > ( shape . parameters [ "options" ] ) [ "value" ] ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
default : {
const message : string = ` unhandled shape kind ' ${ shape . name } ' ` ;
// console.warn(message);
// return null;
throw ( new Error ( message ) ) ;
break ;
2024-09-12 00:03:29 +02:00
}
}
}
* /
2024-09-18 18:17:25 +02:00
} ) ( random = lib _plankton . random || ( lib _plankton . random = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _generator = ( this && this . _ _generator ) || function ( thisArg , body ) {
var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
function step ( op ) {
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
2024-09-21 10:55:06 +02:00
while ( _ ) try {
2024-09-18 18:17:25 +02:00
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
switch ( op [ 0 ] ) {
case 0 : case 1 : t = op ; break ;
case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
default :
if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
if ( t [ 2 ] ) _ . ops . pop ( ) ;
_ . trys . pop ( ) ; continue ;
}
op = body . call ( thisArg , _ ) ;
} catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
}
} ;
2024-09-12 00:03:29 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : storage « .
2024-09-12 00:03:29 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : session « is free software : you can redistribute it and / or modify
2024-09-12 00:03:29 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : session « is distributed in the hope that it will be useful ,
2024-09-12 00:03:29 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : storage « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-12 00:03:29 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var session ;
( function ( session _1 ) {
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
var _conf = null ;
/ * *
* /
function check _conf ( ) {
if ( _conf === null ) {
throw ( new Error ( "session system not set up yet" ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
else {
// do nothing
2024-09-12 00:03:29 +02:00
}
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function begin ( name , options ) {
if ( options === void 0 ) { options = { } ; }
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var key , attempts , session _old , error _1 , session _2 , session _raw ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
options = Object . assign ( {
"lifetime" : _conf . default _lifetime ,
"data" : null
} , options ) ;
check _conf ( ) ;
key = null ;
attempts = 0 ;
_a . label = 1 ;
case 1 :
if ( ! true ) return [ 3 /*break*/ , 6 ] ;
attempts += 1 ;
key = lib _plankton . call . convey ( _conf . key _length , [
function ( x ) { return ( new Array ( x ) ) . fill ( null ) ; } ,
function ( x ) { return x . map ( function ( ) { return lib _plankton . random . generate _hexdigit ( ) ; } ) ; } ,
function ( x ) { return x . join ( "" ) ; } ,
] ) ;
session _old = null ;
_a . label = 2 ;
case 2 :
_a . trys . push ( [ 2 , 4 , , 5 ] ) ;
return [ 4 /*yield*/ , _conf . data _chest . read ( key ) ] ;
case 3 :
session _old = _a . sent ( ) ;
return [ 3 /*break*/ , 5 ] ;
case 4 :
error _1 = _a . sent ( ) ;
session _old = null ;
return [ 3 /*break*/ , 5 ] ;
case 5 :
if ( session _old !== null ) {
key = null ;
if ( attempts >= _conf . key _max _attempts ) {
// fail
return [ 3 /*break*/ , 6 ] ;
}
else {
// retry
}
}
else {
// suuccess
return [ 3 /*break*/ , 6 ] ;
}
return [ 3 /*break*/ , 1 ] ;
case 6 :
if ( ! ( key === null ) ) return [ 3 /*break*/ , 7 ] ;
throw ( new Error ( "failed to generate unique session key" ) ) ;
case 7 :
session _2 = {
"key" : key ,
"name" : name ,
"expiry" : ( lib _plankton . base . get _current _timestamp ( ) + options . lifetime ) ,
"data" : options . data
} ;
session _raw = {
"key" : session _2 . key ,
"name" : session _2 . name ,
"expiry" : Math . floor ( session _2 . expiry ) ,
"data" : JSON . stringify ( session _2 . data )
} ;
lib _plankton . call . timeout ( function ( ) {
lib _plankton . log . info ( "session_dropping_due_to_being_expired" , {
"key" : key ,
"name" : name ,
"lifetime" : options . lifetime
} ) ;
end ( key ) ;
} , options . lifetime ) ;
return [ 4 /*yield*/ , _conf . data _chest . write ( key , session _raw ) ] ;
case 8 :
_a . sent ( ) ;
return [ 2 /*return*/ , Promise . resolve ( key ) ] ;
}
} ) ;
} ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
session _1 . begin = begin ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function get ( key ) {
check _conf ( ) ;
return ( _conf . data _chest . read ( key )
. then ( function ( session _raw ) {
var session = {
"key" : session _raw [ "key" ] ,
"name" : session _raw [ "name" ] ,
"expiry" : session _raw [ "expiry" ] ,
"data" : JSON . parse ( session _raw [ "data" ] )
} ;
var now = lib _plankton . base . get _current _timestamp ( ) ;
if ( now > session . expiry ) {
lib _plankton . log . info ( "session_dropping_due_to_being_stale" , {
"key" : session . key ,
"name" : session . name
} ) ;
end ( key ) ;
return Promise . reject ( ) ;
}
else {
return Promise . resolve ( session ) ;
}
} ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
session _1 . get = get ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function end ( key ) {
check _conf ( ) ;
return _conf . data _chest [ "delete" ] ( key ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
session _1 . end = end ;
2024-09-12 00:03:29 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function setup ( options ) {
if ( options === void 0 ) { options = { } ; }
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
options = Object . assign ( {
"key_length" : 16 ,
"key_max_attempts" : 3 ,
"default_lifetime" : 900 ,
"data_chest" : lib _plankton . storage . memory . implementation _chest ( { } ) ,
"clear" : false
} , options ) ;
_conf = {
"key_length" : options . key _length ,
"key_max_attempts" : options . key _max _attempts ,
"default_lifetime" : options . default _lifetime ,
"data_chest" : options . data _chest
} ;
if ( ! options . clear ) return [ 3 /*break*/ , 2 ] ;
return [ 4 /*yield*/ , _conf . data _chest . clear ( ) ] ;
case 1 :
_a . sent ( ) ;
return [ 3 /*break*/ , 2 ] ;
case 2 : return [ 2 /*return*/ , Promise . resolve ( undefined ) ] ;
}
} ) ;
} ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
session _1 . setup = setup ;
} ) ( session = lib _plankton . session || ( lib _plankton . session = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : date « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : date « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : date « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : date « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _et ;
( function ( lib _et ) {
/ * *
* @ author fenris
* /
function div ( x , y ) {
return Math . floor ( x / y ) ;
}
/ * *
* @ author fenris
* /
function mod ( x , y ) {
return ( x - ( y * div ( x , y ) ) ) ;
}
/ * *
* @ author fenris
* /
function json _encode ( obj ) {
return JSON . stringify ( obj ) ;
}
/ * *
* @ desc the maximum of a regular UNIX - timestamp : 2 ^ 31 - 1 ; one second is cut
* @ author fenris
* /
const _modulus = 0x7FFFFFFF ;
/ * *
* @ desc add ( e1 , s1 ) ( e2 , s2 ) = ( e1 + e2 + ( ( s1 + s2 ) div m ) , ( s1 + s2 ) mod m )
* @ author fenris
* /
function add ( et1 , et2 ) {
let era = ( et1 . era + et2 . era + div ( et1 . stamp + et2 . stamp , _modulus ) ) ;
let stamp = mod ( et1 . stamp + et2 . stamp , _modulus ) ;
return { "era" : era , "stamp" : stamp } ;
}
/ * *
* @ desc neutral = ( 0 , 0 )
* /
function neutral ( ) {
return { "era" : 0 , "stamp" : 0 } ;
}
/ * *
* @ desc invert ( e , s ) = ( - 1 - e , m - s )
* /
function invert ( et ) {
2024-09-10 01:11:51 +02:00
let era = ( - 1 - et . era ) ;
let stamp = ( _modulus - et . stamp ) ;
return { "era" : era , "stamp" : stamp } ;
}
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function part ( et1 , et2 ) {
return add ( et1 , invert ( et2 ) ) ;
}
lib _et . part = part ;
/ * *
* @ desc less
* @ author fenris
* /
function before ( reference , et ) {
let et _ = part ( et , reference ) ;
return ( ( et _ . era >= 0 ) && ( et _ . stamp > 0 ) ) ;
}
lib _et . before = before ;
/ * *
* @ desc greater
* @ author fenris
* /
function after ( reference , et ) {
let et _ = part ( reference , et ) ;
return ( ( et _ . era >= 0 ) && ( et _ . stamp > 0 ) ) ;
}
lib _et . after = after ;
/ * *
* @ author fenris
* /
function between ( begin , end , et ) {
return ( before ( begin , et )
&&
after ( end , et ) ) ;
}
lib _et . between = between ;
/ * *
* @ author fenris
* /
function intersect ( begin1 , end1 , begin2 , end2 ) {
return ( ( between ( begin1 , end1 , begin2 )
||
between ( begin1 , end1 , end2 ) )
||
( between ( begin2 , end2 , begin1 )
||
between ( begin2 , end2 , end1 ) ) ) ;
}
lib _et . intersect = intersect ;
/ * *
* @ author fenris
* /
function move ( base , span ) {
return add ( base , span ) ;
}
lib _et . move = move ;
/ * *
* @ desc currified version of "move"
* @ author fenris
* /
function move _ ( span ) {
return ( base => move ( base , span ) ) ;
}
lib _et . move _ = move _ ;
/ * *
* @ author fenris
* /
function from _timestamp ( timestamp ) {
let era = div ( timestamp , _modulus ) ;
let stamp = mod ( timestamp , _modulus ) ;
let et = { "era" : era , "stamp" : stamp } ;
return et ;
}
lib _et . from _timestamp = from _timestamp ;
/ * *
* @ author fenris
* /
function to _timestamp ( et ) {
if ( et . era != 0 ) {
const message = "case (era <> 0) not properly implemented" ;
console . warn ( message + "; well ... i'll do my very best ..." ) ;
// throw (new Error(message));
}
let timestamp = ( ( et . era * _modulus ) + et . stamp ) ;
return timestamp ;
}
lib _et . to _timestamp = to _timestamp ;
/ * *
* @ author fenris
* /
function from _jsdate ( jsdate ) {
const timestamp = Math . floor ( jsdate . getTime ( ) / 1000 ) ;
const et = from _timestamp ( timestamp ) ;
return et ;
}
lib _et . from _jsdate = from _jsdate ;
/ * *
* @ author fenris
* /
function to _jsdate ( et ) {
const timestamp = to _timestamp ( et ) ;
const jsdate = ( new Date ( timestamp * 1000 ) ) ;
return jsdate ;
}
lib _et . to _jsdate = to _jsdate ;
/ * *
* @ author fenris
* /
function from _components ( components ) {
const timestamp = Math . floor ( Date . UTC ( ( components . year ) , ( components . month - 1 ) , ( components . day ) , ( components . hour || 0 ) , ( components . minute || 0 ) , ( components . second || 0 ) )
/
1000 ) ;
const et = from _timestamp ( timestamp ) ;
return et ;
}
lib _et . from _components = from _components ;
/ * *
* @ author fenris
* /
function to _components ( et ) {
const jsdate = to _jsdate ( et ) ;
const components = {
"year" : jsdate . getUTCFullYear ( ) ,
"month" : jsdate . getUTCMonth ( ) + 1 ,
"day" : jsdate . getUTCDate ( ) ,
"hour" : jsdate . getUTCHours ( ) ,
"minute" : jsdate . getUTCMinutes ( ) ,
"second" : jsdate . getUTCSeconds ( ) ,
} ;
return components ;
}
lib _et . to _components = to _components ;
/ * *
* @ author fenris
* /
function now ( ) {
let timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
let et = from _timestamp ( timestamp ) ;
return et ;
}
lib _et . now = now ;
/ * *
* @ author fenris
* /
function to _string ( et ) {
// return (json_encode(et) + " ~ " + json_encode(to_components(et)));
return to _jsdate ( et ) . toUTCString ( ) ;
}
lib _et . to _string = to _string ;
/ * *
* @ author fenris
* /
function to _string _ywd ( et ) {
// return (json_encode(et) + " ~ " + json_encode(to_components(et)));
return to _jsdate ( et ) . toUTCString ( ) ;
}
lib _et . to _string _ywd = to _string _ywd ;
/ * *
* @ desc retrieve week of year
* @ author fenris
* /
function get _woy ( et ) {
let jsdate = to _jsdate ( et ) ;
let begin _of _year = new Date ( jsdate . getFullYear ( ) , 0 , 1 , 12 , 0 , 0 ) ;
let day _of _week = begin _of _year . getDay ( ) ;
let overhang = ( day _of _week >= 4 ) ;
let factor = ( 1000 * 60 * 60 * 24 ) ;
let days = ( ( jsdate . getTime ( ) - begin _of _year . getTime ( ) ) / factor ) ;
let week = ( Math . ceil ( ( days + day _of _week ) / 7 ) - ( overhang ? 1 : 0 ) ) ;
return week ;
}
lib _et . get _woy = get _woy ;
/ * *
* @ desc retrieve day of week
* @ author fenris
* /
function get _dow ( et ) {
let jsdate = to _jsdate ( et ) ;
let dow = ( mod ( jsdate . getDay ( ) - 1 , 7 ) + 1 ) ;
return dow ;
}
lib _et . get _dow = get _dow ;
/ * *
* @ author fenris
* /
function trunc _minute ( et = now ( ) ) {
let components = to _components ( et ) ;
let components _ = {
"year" : components . year ,
"month" : components . month ,
"day" : components . day ,
"hour" : components . hour ,
"minute" : components . minute ,
"second" : 0
} ;
let et _ = from _components ( components _ ) ;
return et _ ;
}
lib _et . trunc _minute = trunc _minute ;
/ * *
* @ author fenris
* /
function trunc _hour ( et = now ( ) ) {
let components = to _components ( et ) ;
let components _ = {
"year" : components . year ,
"month" : components . month ,
"day" : components . day ,
"hour" : components . hour ,
"minute" : 0 ,
"second" : 0
} ;
let et _ = from _components ( components _ ) ;
return et _ ;
}
lib _et . trunc _hour = trunc _hour ;
/ * *
* @ author fenris
* /
function trunc _day ( et = now ( ) ) {
let components = to _components ( et ) ;
let components _ = {
"year" : components . year ,
"month" : components . month ,
"day" : components . day ,
"hour" : 0 ,
"minute" : 0 ,
"second" : 0
} ;
let et _ = from _components ( components _ ) ;
return et _ ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . trunc _day = trunc _day ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function trunc _month ( et = now ( ) ) {
let components = to _components ( et ) ;
let components _ = {
"year" : components . year ,
"month" : components . month ,
"day" : 0 ,
"hour" : 0 ,
"minute" : 0 ,
"second" : 0
} ;
let et _ = from _components ( components _ ) ;
return et _ ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . trunc _month = trunc _month ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function trunc _year ( et = now ( ) ) {
let components = to _components ( et ) ;
let components _ = {
"year" : components . year ,
"month" : 0 ,
"day" : 0 ,
"hour" : 0 ,
"minute" : 0 ,
"second" : 0
} ;
let et _ = from _components ( components _ ) ;
return et _ ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . trunc _year = trunc _year ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function trunc _week ( et = now ( ) ) {
let et _ = trunc _day ( et ) ;
while ( to _jsdate ( et _ ) . getDay ( ) > 1 ) {
et _ = add ( et _ , span _day ( - 1 ) ) ;
}
return et _ ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . trunc _week = trunc _week ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function span _second ( seconds = 1 ) {
return from _timestamp ( seconds ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . span _second = span _second ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function span _minute ( minutes = 1 ) {
return span _second ( minutes * 60 ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . span _minute = span _minute ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function span _hour ( hours = 1 ) {
return span _minute ( hours * 60 ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . span _hour = span _hour ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
* /
2024-09-12 00:03:29 +02:00
function span _day ( days = 1 ) {
return span _hour ( days * 24 ) ;
}
lib _et . span _day = span _day ;
/ * *
* @ author fenris
* /
function span _week ( weeks = 1 ) {
return span _day ( weeks * 7 ) ;
}
lib _et . span _week = span _week ;
/ * *
* @ author fenris
* /
function span _year ( years = 1 ) {
return span _second ( Math . floor ( years * 365.25 * 24 * 60 * 60 ) ) ;
}
lib _et . span _year = span _year ;
} ) ( lib _et || ( lib _et = { } ) ) ;
/ *
This file is part of » bacterio - plankton : date « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : date « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : date « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : date « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _et ;
( function ( lib _et ) {
/ * *
* @ author fenris
* /
class class _et {
/ * *
* @ author fenris
* /
constructor ( subject ) {
this . subject = subject ;
}
/ * *
* @ author fenris
* /
move ( et ) {
return ( new class _et ( lib _et . move ( this . subject , et . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
before ( et ) {
return lib _et . before ( et . subject , this . subject ) ;
}
/ * *
* @ author fenris
* /
after ( et ) {
return lib _et . after ( et . subject , this . subject ) ;
}
/ * *
* @ author fenris
* /
between ( et1 , et2 ) {
return lib _et . between ( et1 . subject , et2 . subject , this . subject ) ;
}
/ * *
* @ author fenris
* /
trunc _minute ( ) {
return ( new class _et ( lib _et . trunc _minute ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
trunc _hour ( ) {
return ( new class _et ( lib _et . trunc _hour ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
trunc _day ( ) {
return ( new class _et ( lib _et . trunc _day ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
trunc _month ( ) {
return ( new class _et ( lib _et . trunc _month ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
trunc _year ( ) {
return ( new class _et ( lib _et . trunc _year ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
trunc _week ( ) {
return ( new class _et ( lib _et . trunc _week ( this . subject ) ) ) ;
}
/ * *
* @ author fenris
* /
static now ( ) {
return ( new class _et ( lib _et . now ( ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _second ( count = 1 ) {
return ( new class _et ( lib _et . span _second ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _minute ( count = 1 ) {
return ( new class _et ( lib _et . span _minute ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _hour ( count = 1 ) {
return ( new class _et ( lib _et . span _hour ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _day ( count = 1 ) {
return ( new class _et ( lib _et . span _day ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _week ( count = 1 ) {
return ( new class _et ( lib _et . span _week ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static span _year ( count = 1 ) {
return ( new class _et ( lib _et . span _year ( count ) ) ) ;
}
/ * *
* @ author fenris
* /
static from _timestamp ( timestamp ) {
return ( new class _et ( lib _et . from _timestamp ( timestamp ) ) ) ;
}
/ * *
* @ author fenris
* /
to _timestamp ( ) {
return lib _et . to _timestamp ( this . subject ) ;
}
/ * *
* @ author fenris
* /
static from _jsdate ( jsdate ) {
return ( new class _et ( lib _et . from _jsdate ( jsdate ) ) ) ;
}
/ * *
* @ author fenris
* /
to _jsdate ( ) {
return lib _et . to _jsdate ( this . subject ) ;
}
/ * *
* @ author fenris
* /
static from _components ( components ) {
return ( new class _et ( lib _et . from _components ( components ) ) ) ;
}
/ * *
* @ author fenris
* /
to _components ( ) {
return lib _et . to _components ( this . subject ) ;
}
/ * *
* @ author fenris
* /
get _woy ( ) {
return lib _et . get _woy ( this . subject ) ;
}
/ * *
* @ author fenris
* /
get _dow ( ) {
return lib _et . get _dow ( this . subject ) ;
}
/ * *
* @ author fenris
* /
to _string ( ) {
return lib _et . to _string ( this . subject ) ;
}
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _et . class _et = class _et ;
} ) ( lib _et || ( lib _et = { } ) ) ;
/ *
This file is part of » bacterio - plankton : date « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : date « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : date « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : date « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ * *
* @ author neuc
* /
var lib _plankton ;
( function ( lib _plankton ) {
var date ;
( function ( date _1 ) {
/ * *
* @ author neu3no , fenris
* /
var _days = [
"Sunday" ,
"Monday" ,
"Tuesday" ,
"Wednesday" ,
"Thursday" ,
"Friday" ,
"Saturday"
] ;
/ * *
* @ author neu3no , fenris
* /
var _months = [
"January" ,
"February" ,
"March" ,
"April" ,
"May" ,
"June" ,
"July" ,
"August" ,
"September" ,
"October" ,
"November" ,
"December"
] ;
/ * *
* @ author neu3no , fenris
* /
var _segments = {
"%a" : ( date => _days [ date . getDay ( ) ] . slice ( 0 , 3 ) ) ,
"%A" : ( date => _days [ date . getDay ( ) ] ) ,
"%b" : ( date => _days [ date . getMonth ( ) ] . slice ( 0 , 3 ) ) ,
"%B" : ( date => _days [ date . getMonth ( ) ] ) ,
"%c" : ( date => date . toLocaleString ( ) ) ,
"%C" : ( date => Math . floor ( ( date . getFullYear ( ) ) / 100 ) . toString ( ) ) ,
"%d" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getDate ( ) ] ) ) ,
"%D" : ( date => parse ( "%m/%d/%y" , date ) ) ,
"%e" : ( date => lib _plankton . string . sprintf ( "%2d" , [ date . getDate ( ) ] ) ) ,
"%F" : ( date => parse ( "%Y-%m-%d" , date ) ) ,
"%g" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getFullYear ( ) % 1000 ] ) ) ,
"%G" : ( date => date . getFullYear ( ) . toString ( ) ) ,
"%h" : ( date => parse ( "%b" , date ) ) ,
"%H" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getHours ( ) ] ) ) ,
"%I" : ( date => lib _plankton . string . sprintf ( "%02d" , [ ( ( date . getHours ( ) > 12 ) ? ( date . getHours ( ) - 12 ) : date . getHours ( ) ) ] ) ) ,
"%j" : ( date => lib _plankton . string . sprintf ( "%03d" , [ helper _dayOfYear ( date ) ] ) ) ,
"%m" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getMonth ( ) + 1 ] ) ) ,
"%M" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getMinutes ( ) ] ) ) ,
"%n" : ( date => "\n" ) ,
"%p" : ( date => ( ( date . getHours ( ) > 12 ) ? "PM" : "AM" ) ) ,
"%r" : ( date => parse ( "%I:%M:%S %p" , date ) ) ,
"%R" : ( date => parse ( "%H:%M" , date ) ) ,
"%S" : ( date => date . getSeconds ( ) . toString ( ) ) ,
"%t" : ( date => "\t" ) ,
"%T" : ( date => parse ( "%H:%M:%S" , date ) ) ,
"%u" : ( date => lib _plankton . string . sprintf ( "%02d" , [ ( ( date . getDay ( ) === 0 ) ? 7 : date . getDay ( ) ) ] ) ) ,
"%U" : ( date => lib _plankton . string . sprintf ( "%02d" , [ helper _englishWeekOfYear ( date ) ] ) ) ,
"%V" : ( date => lib _plankton . string . sprintf ( "%02d" , [ helper _weekOfYear ( date ) ] ) ) ,
"%w" : ( date => lib _plankton . string . sprintf ( "%02d" , [ date . getDay ( ) . toString ( ) ] ) ) ,
"%W" : ( date => parse ( "%w" , date ) ) ,
"%x" : ( date => parse ( "%m/%d/%G" , date ) ) ,
"%X" : ( date => parse ( "%T" , date ) ) ,
"%y" : ( date => parse ( "%g" , date ) ) ,
"%Y" : ( date => parse ( "%G" , date ) ) ,
"%z" : ( date => date . getTimezoneOffset ( ) . toString ( ) ) ,
"%Z" : ( date => date . toUTCString ( ) . split ( ' ' ) . pop ( ) ) ,
"%%" : ( date => "%" ) ,
} ;
/ * *
* @ author neu3no , fenris
* /
var _currentDate = ( new Date ( Date . now ( ) ) ) ;
/ * *
* @ author neu3no , fenris
* /
function set _days ( day _names ) {
_days = day _names ;
}
date _1 . set _days = set _days ;
/ * *
* @ author neu3no , fenris
* /
function set _months ( month _names ) {
_months = month _names ;
}
date _1 . set _months = set _months ;
/ * *
* @ desc source : https : //stackoverflow.com/questions/8619879/javascript-calculate-the-day-of-the-year-1-366
* @ author neu3no , fenris
* /
function helper _dayOfYear ( date ) {
let start = new Date ( date . getFullYear ( ) , 0 , 0 ) ;
let diff = ( date . getTime ( ) - start . getTime ( ) ) ;
let oneDay = ( 1000 * 60 * 60 * 24 ) ;
return Math . floor ( diff / oneDay ) ;
}
/ * *
* @ desc source : http : //weeknumber.net/how-to/javascript
* @ author neu3no , fenris
* /
function helper _weekOfYear ( date _ ) {
let date = new Date ( date _ . getTime ( ) ) ;
date . setHours ( 0 , 0 , 0 , 0 ) ;
// Thursday in current week decides the year.
date . setDate ( date . getDate ( ) + 3 - ( date . getDay ( ) + 6 ) % 7 ) ;
// January 4 is always in week 1.
let week1 = new Date ( date . getFullYear ( ) , 0 , 4 ) ;
2024-09-18 18:17:25 +02:00
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return ( 1
+
Math . round ( ( ( ( date . getTime ( ) - week1 . getTime ( ) ) / 86400000 )
-
3
+
( week1 . getDay ( ) + 6 ) % 7 )
/
7 ) ) ;
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ author neu3no , fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function helper _englishWeekOfYear ( date ) {
let nr = helper _weekOfYear ( date ) ;
if ( date . getDay ( ) === 0 ) {
nr = nr - 1 ;
}
return nr ;
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ desc week of year
* @ param { Date } date
* @ return { int }
* @ author fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function get _week ( date ) {
let begin _of _year = new Date ( date . getFullYear ( ) , 0 , 1 , 12 , 0 , 0 ) ;
let day _of _week = begin _of _year . getDay ( ) ;
let overhang = ( day _of _week >= 4 ) ;
let factor = ( 1000 * 60 * 60 * 24 ) ;
let days = ( ( date . getTime ( ) - begin _of _year . getTime ( ) ) / factor ) ;
let week = ( Math . ceil ( ( days + day _of _week ) / 7 ) - ( overhang ? 1 : 0 ) ) ;
return week ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
date _1 . get _week = get _week ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author neu3no , fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function set _currentDate ( date ) {
_currentDate = date ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
date _1 . set _currentDate = set _currentDate ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author neu3no , fenris
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function parse ( format , date = _currentDate ) {
let ret = format ;
let re = new RegExp ( "%[a-z]" , "gi" ) ;
let match ;
while ( match = re . exec ( format ) ) {
ret = ret . replace ( match [ 0 ] , parse _segment ( match [ 0 ] , date ) ) ;
}
return ret ;
}
date _1 . parse = parse ;
/ * *
* @ author neu3no , fenris
* /
function parse _segment ( segment , date = _currentDate ) {
if ( ! ( segment in _segments ) ) {
let message = ( "unknown format argument '" + segment + "'" ) ;
throw ( new Error ( message ) ) ;
}
else {
return _segments [ segment ] ( date ) ;
}
}
/ * *
* @ author neu3no , fenris
* /
function locale _date ( date = new Date ( ) , ignore _error = false ) {
if ( ! ( date instanceof Date ) ) {
if ( ! ignore _error ) {
throw new SyntaxError ( "date must be instance of Date" ) ;
}
else {
console . warn ( "'" + date + "' seems not to be instance of Date; try to force convert." ) ;
let tmp = date ;
date = new Date ( tmp ) ;
if ( ( date . toString ( ) === "Invalid Date" )
||
( ! ( date < new Date ( 0 ) ) && ! ( date > new Date ( 0 ) ) ) ) {
console . warn ( "conversion didn't work, returning default value" ) ;
return "Ø" ;
2024-09-12 00:03:29 +02:00
}
}
2024-09-18 18:17:25 +02:00
}
let conf = (
/ *
global _config . get _value ( "date" )
||
* /
{
"use_locale_date" : true ,
"format_string" : "%d.%m.%Y"
2024-09-12 00:03:29 +02:00
} ) ;
2024-09-18 18:17:25 +02:00
if ( conf . use _locale _date ) {
return date . toLocaleDateString ( ) ;
}
else {
return strftime . parse ( conf . format _string , date ) ;
}
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
date _1 . locale _date = locale _date ;
/ * *
* /
function now ( ) {
return Math . floor ( Date . now ( ) / 1000 ) ;
}
date _1 . now = now ;
/ * *
* /
function from _components ( components ) {
return Math . floor ( new Date ( Date . parse ( lib _string . coin ( "{{year}}-{{month}}-{{day}}T{{hour}}:{{minute}}:{{second}}.{{milliseconds}}{{timezone_offset}}" , {
"year" : components . year . toFixed ( 0 ) . padStart ( 4 , "0" ) ,
"month" : components . month . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"day" : components . day . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"hour" : components . hour . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"minute" : components . minute . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"second" : components . second . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"milliseconds" : ( 0 ) . toFixed ( 0 ) . padStart ( 3 , "0" ) ,
"timezone_offset" : lib _string . coin ( "{{sign}}{{amount}}:00" , {
"sign" : ( ( components . timezone _offset < 0 ) ? "-" : "+" ) ,
"amount" : Math . abs ( components . timezone _offset ) . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
} ) ,
} ) ) ) . getTime ( )
/
1000 ) ;
}
date _1 . from _components = from _components ;
/ * *
* /
function to _components ( unixtimestamp ) {
const date _object = new Date ( unixtimestamp * 1000 ) ;
const date _string = date _object . toISOString ( ) ;
return {
"timezone_offset" : 0 ,
"year" : parseInt ( date _string . slice ( 0 , 4 ) ) ,
"month" : parseInt ( date _string . slice ( 5 , 7 ) ) ,
"day" : parseInt ( date _string . slice ( 8 , 10 ) ) ,
"hour" : parseInt ( date _string . slice ( 11 , 13 ) ) ,
"minute" : parseInt ( date _string . slice ( 14 , 16 ) ) ,
"second" : parseInt ( date _string . slice ( 17 , 19 ) ) ,
} ;
}
date _1 . to _components = to _components ;
/ * *
* /
function get _timestamp _from _year _and _week _and _day ( year , week , day ) {
const d = ( 1 + ( ( week - 1 ) * 7 ) ) ;
const date _raw = new Date ( year , 0 , d ) ;
return ( Math . round ( date _raw . getTime ( ) / 1000 )
+
( 60 * 60 * 24 * ( day - date _raw . getDay ( ) + 1 ) ) ) ;
}
date _1 . get _timestamp _from _year _and _week _and _day = get _timestamp _from _year _and _week _and _day ;
} ) ( date = lib _plankton . date || ( lib _plankton . date = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
var strftime = lib _plankton . date ;
2024-09-10 01:11:51 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : ical « .
2024-09-10 01:11:51 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : ical « is free software : you can redistribute it and / or modify
2024-09-10 01:11:51 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : ical « is distributed in the hope that it will be useful ,
2024-09-10 01:11:51 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : ical « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var ical ;
( function ( ical ) {
2024-09-10 01:11:51 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
// type type_timestamp = string;
/ * *
* /
let enum _class ;
( function ( enum _class ) {
enum _class [ "public" ] = "public" ;
enum _class [ "private" ] = "private" ;
enum _class [ "confidential" ] = "confidential" ;
} ) ( enum _class = ical . enum _class || ( ical . enum _class = { } ) ) ;
;
/ * *
* /
let enum _event _status ;
( function ( enum _event _status ) {
enum _event _status [ "tentative" ] = "tentative" ;
enum _event _status [ "confirmed" ] = "confirmed" ;
enum _event _status [ "cancelled" ] = "cancelled" ;
} ) ( enum _event _status = ical . enum _event _status || ( ical . enum _event _status = { } ) ) ;
;
/ * *
* /
let enum _transp ;
( function ( enum _transp ) {
enum _transp [ "opaque" ] = "opaque" ;
enum _transp [ "transparent" ] = "transparent" ;
} ) ( enum _transp = ical . enum _transp || ( ical . enum _transp = { } ) ) ;
;
} ) ( ical = lib _plankton . ical || ( lib _plankton . ical = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-10 01:11:51 +02:00
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : ical « .
2024-09-10 01:11:51 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : ical « is free software : you can redistribute it and / or modify
2024-09-10 01:11:51 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : ical « is distributed in the hope that it will be useful ,
2024-09-10 01:11:51 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : ical « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-10 01:11:51 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var ical ;
( function ( ical ) {
2024-09-10 01:11:51 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function date _decode ( date _encoded ) {
return {
"year" : parseInt ( date _encoded . slice ( 0 , 4 ) ) ,
"month" : parseInt ( date _encoded . slice ( 4 , 6 ) ) ,
"day" : parseInt ( date _encoded . slice ( 6 , 8 ) ) ,
} ;
}
2024-09-10 01:11:51 +02:00
/ * *
* /
2024-09-18 18:17:25 +02:00
function time _decode ( time _encoded ) {
return {
"hour" : parseInt ( time _encoded . slice ( 0 , 2 ) ) ,
"minute" : parseInt ( time _encoded . slice ( 2 , 4 ) ) ,
"second" : parseInt ( time _encoded . slice ( 4 , 6 ) ) ,
"utc" : ( ( time _encoded . length >= 7 ) && ( time _encoded [ 6 ] === "Z" ) )
} ;
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function datetime _decode ( datetime _encoded ) {
const parts = datetime _encoded . split ( "T" , 2 ) ;
return {
"date" : date _decode ( parts [ 0 ] ) ,
"time" : ( ( parts . length >= 2 ) ? time _decode ( parts [ 1 ] ) : null ) ,
} ;
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
let enum _decode _state _label ;
( function ( enum _decode _state _label ) {
enum _decode _state _label [ "expect_vcalendar_begin" ] = "expect_vcalendar_begin" ;
enum _decode _state _label [ "expect_vcalendar_property" ] = "expect_vcalendar_property" ;
enum _decode _state _label [ "expect_vevent_property" ] = "expect_vevent_property" ;
enum _decode _state _label [ "done" ] = "done" ;
} ) ( enum _decode _state _label || ( enum _decode _state _label = { } ) ) ;
;
/ * *
* /
function class _encode ( class _ ) {
switch ( class _ ) {
case ical . enum _class . private : {
return "PRIVATE" ;
break ;
}
case ical . enum _class . public : {
return "PUBLIC" ;
break ;
}
case ical . enum _class . confidential : {
return "CONFIDENTIAL" ;
break ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function class _decode ( class _encoded ) {
return {
"PRIVATE" : ical . enum _class . private ,
"PUBLIC" : ical . enum _class . public ,
"CONFIDENTIAL" : ical . enum _class . confidential ,
} [ class _encoded ] ;
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function event _status _encode ( event _status ) {
switch ( event _status ) {
case ical . enum _event _status . tentative : {
return "TENTATIVE" ;
break ;
}
case ical . enum _event _status . confirmed : {
return "CONFIRMED" ;
break ;
}
case ical . enum _event _status . cancelled : {
return "CANCELLED" ;
break ;
}
2024-09-10 01:11:51 +02:00
}
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function event _status _decode ( event _status _encoded ) {
return {
"TENTATIVE" : ical . enum _event _status . tentative ,
"CONFIRMED" : ical . enum _event _status . confirmed ,
"CANCELLED" : ical . enum _event _status . cancelled ,
} [ event _status _encoded ] ;
}
/ * *
* /
function transp _encode ( transp ) {
switch ( transp ) {
case ical . enum _transp . opaque : {
return "OPAQUE" ;
break ;
}
case ical . enum _transp . transparent : {
return "TRANSPARENT" ;
break ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
}
/ * *
* /
function transp _decode ( transp _encoded ) {
return {
"OPAQUE" : ical . enum _transp . opaque ,
"TRANSPARENT" : ical . enum _transp . transparent ,
} [ transp _encoded ] ;
}
/ * *
* /
function datetime _to _unixtimestamp ( datetime ) {
if ( ( datetime . time !== null ) && ( ! datetime . time . utc ) ) {
throw ( new Error ( "can not convert not utc time values" ) ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
return lib _plankton . date . from _components ( {
"timezone_offset" : 0 ,
"year" : datetime . date . year ,
"month" : datetime . date . month ,
"day" : datetime . date . day ,
"hour" : ( ( datetime . time === null ) ? 0 : datetime . time . hour ) ,
"minute" : ( ( datetime . time === null ) ? 0 : datetime . time . minute ) ,
"second" : ( ( datetime . time === null ) ? 0 : datetime . time . second ) ,
} ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
ical . datetime _to _unixtimestamp = datetime _to _unixtimestamp ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ see https : //www.rfc-editor.org/rfc/rfc5545
* @ see https : //icalendar.org/iCalendar-RFC-5545/
* @ todo implement edge cases
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function ics _decode ( ics , options = { } ) {
options = Object . assign ( {
"debug" : false ,
} , options ) ;
// preprocessing
const lines = ics . split ( "\r\n" ) ;
let content _lines = [ ] ;
let content _line _buffer = null ;
lines . forEach ( line => {
if ( line . trim ( ) === "" ) {
// do nothing
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
const is _folding = ( ( line . length >= 2 )
&&
( ( line [ 0 ] === " " )
||
( line [ 0 ] === "\t" ) )
/ *
&&
! (
( line [ 1 ] === " " )
||
( line [ 1 ] === "\t" )
)
* /
) ;
if ( is _folding ) {
content _line _buffer += line . slice ( 1 ) ;
}
else {
if ( content _line _buffer === null ) {
// do nothing
}
else {
content _lines . push ( content _line _buffer ) ;
}
content _line _buffer = line ;
}
}
} ) ;
const instructions = content _lines . map ( ( content _line ) => {
const parts = content _line . split ( ":" ) ;
const parts _left = parts [ 0 ] . split ( ";" ) ;
return {
"command" : parts _left [ 0 ] ,
"parameters" : Object . fromEntries ( parts _left . slice ( 1 ) . map ( x => x . split ( "=" , 2 ) ) ) ,
"value" : ( parts . slice ( 1 ) . join ( ":" )
. split ( ";" )
. map ( x => x . replace ( new RegExp ( "\\\\," , "g" ) , "," ) ) ) ,
} ;
} ) ;
// core
let state = {
"label" : enum _decode _state _label . expect _vcalendar _begin ,
"vcalendar" : null ,
"vevent" : null ,
} ;
instructions . forEach ( ( instruction ) => {
if ( options . debug ) {
console . info ( JSON . stringify ( { "instruction" : instruction , "state" : state } , undefined , "\t" ) ) ;
}
switch ( state . label ) {
default : {
throw ( new Error ( "unhandled state label: " + state . label ) ) ;
break ;
}
case enum _decode _state _label . expect _vcalendar _begin : {
switch ( instruction . command ) {
default : {
throw ( new Error ( "unexpected instruction key: " + instruction . command ) ) ;
break ;
}
case "BEGIN" : {
switch ( instruction . value [ 0 ] ) {
default : {
throw ( new Error ( "unexpected instruction value: " + instruction . value [ 0 ] ) ) ;
break ;
}
case "VCALENDAR" : {
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : {
"version" : "" ,
"prodid" : "" ,
"vevents" : [ ] ,
} ,
"vevent" : null ,
} ;
break ;
}
}
break ;
}
}
break ;
}
case enum _decode _state _label . expect _vcalendar _property : {
switch ( instruction . command ) {
case "VERSION" : {
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : Object . assign ( state . vcalendar , Object . fromEntries ( [ [ "version" , instruction . value [ 0 ] ] ] ) ) ,
"vevent" : state . vevent ,
} ;
break ;
}
case "PRODID" : {
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : Object . assign ( state . vcalendar , Object . fromEntries ( [ [ "prodid" , instruction . value [ 0 ] ] ] ) ) ,
"vevent" : state . vevent ,
} ;
break ;
}
case "METHOD" : {
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : Object . assign ( state . vcalendar , Object . fromEntries ( [ [ "method" , instruction . value [ 0 ] ] ] ) ) ,
"vevent" : state . vevent ,
} ;
break ;
}
case "BEGIN" : {
const object = instruction . value [ 0 ] ;
switch ( object ) {
default : {
throw ( new Error ( "unhandled object: " + object ) ) ;
break ;
}
case "VCALENDAR" : {
throw ( new Error ( "unexpected object: " + object ) ) ;
break ;
}
case "VEVENT" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : {
"uid" : "" ,
"dtstamp" : {
"date" : { "year" : 2000 , "month" : 0 , "day" : 0 } ,
"time" : { "hour" : 0 , "minute" : 0 , "second" : 0 , "utc" : true } ,
} ,
} ,
} ;
break ;
}
}
break ;
}
case "END" : {
const object = instruction . value [ 0 ] ;
switch ( object ) {
default : {
throw ( new Error ( "unhandled object: " + object ) ) ;
break ;
}
case "VCALENDAR" : {
state = {
"label" : enum _decode _state _label . done ,
"vcalendar" : state . vcalendar ,
"vevent" : state . vevent ,
} ;
break ;
}
}
break ;
}
default : {
if ( instruction . command . startsWith ( "X-" ) ) {
const key = instruction . command . slice ( 2 ) . toLowerCase ( ) ;
const value = instruction . value . join ( ";" ) ;
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : Object . assign ( state . vcalendar , {
"x_props" : Object . assign ( ( state . vcalendar . x _props ? ? { } ) , Object . fromEntries ( [ [ key , value ] ] ) )
} ) ,
"vevent" : state . vevent ,
} ;
}
else {
console . info ( { "instruction" : instruction , "state" : state } ) ;
throw ( new Error ( "unhandled instruction key: " + instruction . command ) ) ;
}
break ;
}
}
break ;
}
case enum _decode _state _label . expect _vevent _property : {
switch ( instruction . command ) {
case "UID" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "uid" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "DTSTART" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [
[
"dtstart" ,
{
"tzid" : instruction . parameters [ "tzid" ] ,
"value" : datetime _decode ( instruction . value [ 0 ] ) ,
}
]
] ) ) ,
} ;
break ;
}
case "DTEND" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [
[
"dtend" ,
{
"tzid" : instruction . parameters [ "tzid" ] ,
"value" : datetime _decode ( instruction . value [ 0 ] ) ,
}
]
] ) ) ,
} ;
break ;
}
case "DTSTAMP" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [
[
"dtstamp" ,
datetime _decode ( instruction . value [ 0 ] )
]
] ) ) ,
} ;
break ;
}
case "SEQUENCE" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "sequence" , parseInt ( instruction . value [ 0 ] ) ] ] ) ) ,
} ;
break ;
}
case "TRANSP" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "transp" , transp _decode ( instruction . value [ 0 ] ) ] ] ) ) ,
} ;
break ;
}
case "SUMMARY" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "summary" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "CLASS" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "class" , class _decode ( instruction . value [ 0 ] ) ] ] ) ) ,
} ;
break ;
}
case "STATUS" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "status" , event _status _decode ( instruction . value [ 0 ] ) ] ] ) ) ,
} ;
break ;
}
case "DESCRIPTION" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "description" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "CATEGORIES" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "categories" , instruction . value [ 0 ] . split ( "," ) ] ] ) ) ,
} ;
break ;
}
case "CREATED" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [
[
"created" ,
{
"value" : datetime _decode ( instruction . value [ 0 ] ) ,
}
]
] ) ) ,
} ;
break ;
}
case "LOCATION" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "location" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "URL" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "url" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "LAST-MODIFIED" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [
[
"last_modified" ,
{
"value" : datetime _decode ( instruction . value [ 0 ] ) ,
}
]
] ) ) ,
} ;
break ;
}
case "ATTENDEE" : {
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , Object . fromEntries ( [ [ "attendee" , instruction . value [ 0 ] ] ] ) ) ,
} ;
break ;
}
case "BEGIN" : {
const object = instruction . value [ 0 ] ;
switch ( object ) {
default : {
throw ( new Error ( "unhandled object: " + object ) ) ;
break ;
}
case "VCALENDAR" : {
throw ( new Error ( "unexpected object: " + object ) ) ;
break ;
}
case "VEVENT" : {
throw ( new Error ( "unexpected object: " + object ) ) ;
break ;
}
}
break ;
}
case "END" : {
const object = instruction . value [ 0 ] ;
switch ( object ) {
default : {
throw ( new Error ( "unhandled value: " + object ) ) ;
break ;
}
case "VEVENT" : {
state = {
"label" : enum _decode _state _label . expect _vcalendar _property ,
"vcalendar" : Object . assign ( state . vcalendar , {
"vevents" : state . vcalendar . vevents . concat ( [ state . vevent ] ) ,
} ) ,
"vevent" : null ,
} ;
break ;
}
}
break ;
}
default : {
if ( instruction . command . startsWith ( "X-" ) ) {
const key = instruction . command . slice ( 2 ) . toLowerCase ( ) ;
const value = instruction . value . join ( ";" ) ;
state = {
"label" : enum _decode _state _label . expect _vevent _property ,
"vcalendar" : state . vcalendar ,
"vevent" : Object . assign ( state . vevent , {
"x_props" : Object . assign ( ( state . vevent . x _props ? ? { } ) , Object . fromEntries ( [ [ key , value ] ] ) )
} ) ,
} ;
}
else {
console . info ( { "instruction" : instruction , "state" : state } ) ;
throw ( new Error ( "unhandled instruction key: " + instruction . command ) ) ;
}
break ;
}
}
break ;
}
case enum _decode _state _label . done : {
console . info ( { "instruction" : instruction , "state" : state } ) ;
throw ( new Error ( "end expected" ) ) ;
break ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
return state . vcalendar ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
ical . ics _decode = ics _decode ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ see https : //www.rfc-editor.org/rfc/rfc5545
* @ see https : //icalendar.org/iCalendar-RFC-5545/
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function date _encode ( date ) {
return lib _plankton . string . coin ( "{{year}}{{month}}{{day}}" , {
"year" : date . year . toFixed ( 0 ) . padStart ( 4 , "0" ) ,
"month" : date . month . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"day" : date . day . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
} ) ;
}
/ * *
* /
function time _encode ( time ) {
return lib _plankton . string . coin ( "{{hour}}{{minute}}{{second}}{{utc}}" , {
"hour" : time . hour . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"minute" : time . minute . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"second" : time . second . toFixed ( 0 ) . padStart ( 2 , "0" ) ,
"utc" : ( time . utc ? "Z" : "" ) ,
} ) ;
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-18 18:17:25 +02:00
function datetime _encode ( datetime ) {
return lib _plankton . string . coin ( "{{date}}T{{time}}" , {
"date" : date _encode ( datetime . date ) ,
"time" : time _encode ( datetime . time ) ,
} ) ;
2024-09-10 01:11:51 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ todo method
* @ todo add missing fields
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function ics _encode ( vcalendar ) {
let content _lines = [ ] ;
content _lines . push ( "BEGIN:VCALENDAR" ) ;
content _lines . push ( lib _plankton . string . coin ( "VERSION:{{version}}" , { "version" : vcalendar . version } ) ) ;
content _lines . push ( lib _plankton . string . coin ( "PRODID:{{prodid}}" , { "prodid" : vcalendar . prodid } ) ) ;
content _lines . push ( lib _plankton . string . coin ( "METHOD:{{method}}" , { "method" : vcalendar . method } ) ) ;
vcalendar . vevents . forEach ( ( vevent ) => {
content _lines . push ( "BEGIN:VEVENT" ) ;
{
// uid
content _lines . push ( lib _plankton . string . coin ( "UID:{{uid}}" , {
"uid" : vevent . uid ,
} ) ) ;
// dtstart
content _lines . push ( lib _plankton . string . coin (
// "DTSTART;TZID={{tzid}}:{{value}}",
"DTSTART:{{value}}" , {
"tzid" : vevent . dtstart . tzid ,
"value" : datetime _encode ( vevent . dtstart . value ) ,
} ) ) ;
// dtend
if ( vevent . dtend !== undefined ) {
content _lines . push ( lib _plankton . string . coin (
// "DTEND;TZID={{tzid}}:{{value}}",
"DTEND:{{value}}" , {
"tzid" : vevent . dtend . tzid ,
"value" : datetime _encode ( vevent . dtend . value ) ,
} ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
// dtstamp
content _lines . push ( lib _plankton . string . coin ( "DTSTAMP:{{value}}" , {
"value" : datetime _encode ( vevent . dtstamp ) ,
} ) ) ;
// class
if ( vevent . class !== undefined ) {
content _lines . push ( lib _plankton . string . coin ( "CLASS:{{class}}" , {
"class" : vevent . class ,
2024-09-12 00:03:29 +02:00
} ) ) ;
}
2024-09-18 18:17:25 +02:00
// summary
content _lines . push ( lib _plankton . string . coin ( "SUMMARY:{{summary}}" , {
"summary" : vevent . summary ,
} ) ) ;
// description
if ( vevent . description !== undefined ) {
content _lines . push ( lib _plankton . string . coin ( "DESCRIPTION:{{description}}" , {
"description" : vevent . description ,
} ) ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
// location
if ( vevent . location !== undefined ) {
content _lines . push ( lib _plankton . string . coin ( "LOCATION:{{location}}" , {
"location" : vevent . location ,
} ) ) ;
}
// geo
if ( vevent . geo !== undefined ) {
content _lines . push ( lib _plankton . string . coin ( "GEO:{{geo_latitude}};{{geo_longitude}}" , {
"geo_latitude" : vevent . geo . latitude . toFixed ( 4 ) ,
"geo_longitude" : vevent . geo . longitude . toFixed ( 4 ) ,
} ) ) ;
}
// url
if ( vevent . url !== undefined ) {
content _lines . push ( lib _plankton . string . coin ( "URL:{{url}}" , {
"url" : vevent . url ,
} ) ) ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-18 18:17:25 +02:00
content _lines . push ( "END:VEVENT" ) ;
} ) ;
content _lines . push ( "END:VCALENDAR" ) ;
let lines = [ ] ;
content _lines . forEach ( ( content _line ) => {
const slices = lib _plankton . string . slice ( content _line , 75 - 1 ) ;
lines . push ( slices [ 0 ] ) ;
slices . slice ( 1 ) . forEach ( ( slice ) => { lines . push ( " " + slice ) ; } ) ;
} ) ;
return lines . join ( "\r\n" ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
ical . ics _encode = ics _encode ;
} ) ( ical = lib _plankton . ical || ( lib _plankton . ical = { } ) ) ;
2024-09-10 01:11:51 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : http « .
2024-09-10 01:11:51 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : http « is free software : you can redistribute it and / or modify
2024-09-10 01:11:51 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : http « is distributed in the hope that it will be useful ,
2024-09-10 01:11:51 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : http « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-10 01:11:51 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var http ;
( function ( http ) {
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
let enum _method ;
( function ( enum _method ) {
enum _method [ "options" ] = "options" ;
enum _method [ "head" ] = "head" ;
enum _method [ "get" ] = "get" ;
enum _method [ "delete" ] = "delete" ;
enum _method [ "post" ] = "post" ;
enum _method [ "put" ] = "put" ;
enum _method [ "patch" ] = "patch" ;
} ) ( enum _method = http . enum _method || ( http . enum _method = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( http = lib _plankton . http || ( lib _plankton . http = { } ) ) ;
2024-09-10 01:11:51 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-18 18:17:25 +02:00
This file is part of » bacterio - plankton : http « .
2024-09-10 01:11:51 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-18 18:17:25 +02:00
» bacterio - plankton : http « is free software : you can redistribute it and / or modify
2024-09-10 01:11:51 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-18 18:17:25 +02:00
» bacterio - plankton : http « is distributed in the hope that it will be useful ,
2024-09-10 01:11:51 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-18 18:17:25 +02:00
along with » bacterio - plankton : http « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-10 01:11:51 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-18 18:17:25 +02:00
var http ;
( function ( http ) {
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
const linebreak = "\r\n" ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ todo outsource to string module
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function capitalize ( str ) {
return ( str [ 0 ] . toUpperCase ( ) + str . slice ( 1 ) ) ;
2024-09-10 01:11:51 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ todo outsource to string module
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function capitalize _all ( str ) {
return str . split ( "-" ) . map ( x => capitalize ( x ) ) . join ( "-" ) ;
2024-09-10 01:11:51 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function encode _method ( method ) {
switch ( method ) {
case http . enum _method . get : return "GET" ;
case http . enum _method . post : return "POST" ;
case http . enum _method . patch : return "PATCH" ;
case http . enum _method . put : return "PUT" ;
case http . enum _method . delete : return "DELETE" ;
case http . enum _method . options : return "OPTIONS" ;
case http . enum _method . head : return "HEAD" ;
default : throw ( new Error ( "impossible" ) ) ;
}
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
http . encode _method = encode _method ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function decode _method ( method _raw ) {
switch ( method _raw ) {
case "GET" : return http . enum _method . get ;
case "POST" : return http . enum _method . post ;
case "PATCH" : return http . enum _method . patch ;
case "PUT" : return http . enum _method . put ;
case "DELETE" : return http . enum _method . delete ;
case "OPTIONS" : return http . enum _method . options ;
case "HEAD" : return http . enum _method . head ;
default : throw ( new Error ( "unhandled method: " + method _raw ) ) ;
}
2024-09-10 01:11:51 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function get _statustext ( statuscode ) {
switch ( statuscode ) {
case 100 : return "Continue" ;
case 101 : return "Switching Protocols" ;
case 103 : return "Early Hints" ;
case 200 : return "OK" ;
case 201 : return "Created" ;
case 202 : return "Accepted" ;
case 203 : return "Non-Authoritative Information" ;
case 204 : return "No Content" ;
case 205 : return "Reset Content" ;
case 206 : return "Partial Content" ;
case 300 : return "Multiple Choices" ;
case 301 : return "Moved Permanently" ;
case 302 : return "Found" ;
case 303 : return "See Other" ;
case 304 : return "Not Modified" ;
case 307 : return "Temporary Redirect" ;
case 308 : return "Permanent Redirect" ;
case 400 : return "Bad Request" ;
case 401 : return "Unauthorized" ;
case 402 : return "Payment Required" ;
case 403 : return "Forbidden" ;
case 404 : return "Not Found" ;
case 405 : return "Method Not Allowed" ;
case 406 : return "Not Acceptable" ;
case 407 : return "Proxy Authentication Required" ;
case 408 : return "Request Timeout" ;
case 409 : return "Conflict" ;
case 410 : return "Gone" ;
case 411 : return "Length Required" ;
case 412 : return "Precondition Failed" ;
case 413 : return "Payload Too Large" ;
case 414 : return "URI Too Long" ;
case 415 : return "Unsupported Media Type" ;
case 416 : return "Range Not Satisfiable" ;
case 417 : return "Expectation Failed" ;
case 418 : return "I'm a teapot" ;
case 422 : return "Unprocessable Entity" ;
case 425 : return "Too Early" ;
case 426 : return "Upgrade Required" ;
case 428 : return "Precondition Required" ;
case 429 : return "Too Many Requests" ;
case 431 : return "Request Header Fields Too Large" ;
case 451 : return "Unavailable For Legal Reasons" ;
case 500 : return "Internal Server Error" ;
case 501 : return "Not Implemented" ;
case 502 : return "Bad Gateway" ;
case 503 : return "Service Unavailable" ;
case 504 : return "Gateway Timeout" ;
case 505 : return "HTTP Version Not Supported" ;
case 506 : return "Variant Also Negotiates" ;
case 507 : return "Insufficient Storage" ;
case 508 : return "Loop Detected" ;
case 510 : return "Not Extended" ;
case 511 : return "Network Authentication" ;
default : throw ( new Error ( "unhandled statuscode: " + statuscode . toFixed ( 0 ) ) ) ;
}
2024-09-12 00:03:29 +02:00
}
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function encode _request ( request ) {
let request _raw = "" ;
request _raw += ( encode _method ( request . method )
+
" "
+
request . path
+
( ( request . query === null ) ? "" : request . query )
+
" "
+
request . version
+
linebreak ) ;
if ( request . host === null ) {
// do nothing
}
else {
request _raw += ( "Host: " + request . host + linebreak ) ;
}
for ( const [ key , value ] of Object . entries ( request . headers ) ) {
request _raw += ( capitalize _all ( key ) + ": " + value + linebreak ) ;
}
request _raw += linebreak ;
if ( request . body === null ) {
// do nothing
2024-09-10 01:11:51 +02:00
}
else {
2024-09-18 18:17:25 +02:00
request _raw += request . body . toString ( ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
return request _raw ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
http . encode _request = encode _request ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-10 01:11:51 +02:00
* /
2024-09-18 18:17:25 +02:00
function decode _request ( request _raw ) {
const lines = request _raw . split ( linebreak ) ;
const first = lines . shift ( ) ;
const parts = first . split ( " " ) ;
const method = decode _method ( parts [ 0 ] ) ;
const path _and _query = parts [ 1 ] ;
const parts _ = path _and _query . split ( "?" ) ;
const path = parts _ [ 0 ] ;
const query = ( ( parts _ . length <= 1 ) ? null : ( "?" + parts _ . slice ( 1 ) . join ( "?" ) ) ) ;
const version = parts [ 2 ] ;
let headers = { } ;
while ( true ) {
const line = lines . shift ( ) ;
if ( line === "" ) {
break ;
2024-09-10 01:11:51 +02:00
}
else {
2024-09-18 18:17:25 +02:00
const [ key , value ] = line . split ( ": " , 2 ) ;
headers [ key . toLowerCase ( ) ] = value ;
2024-09-12 00:03:29 +02:00
}
}
2024-09-18 18:17:25 +02:00
const body = ( [ http . enum _method . post , http . enum _method . put , http . enum _method . patch ] . includes ( method )
// @ts-ignore
? Buffer . from ( lines . join ( linebreak ) )
: null ) ;
const request = {
// TODO
"scheme" : "http" ,
"host" : ( headers [ "host" ] ? ? null ) ,
"path" : path ,
"version" : version ,
"method" : method ,
"query" : query ,
"headers" : headers ,
"body" : body ,
} ;
return request ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
http . decode _request = decode _request ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function encode _response ( response ) {
let response _raw = "" ;
response _raw += ( response . version
+
" "
+
response . status _code . toFixed ( 0 )
+
" "
+
get _statustext ( response . status _code )
+
linebreak ) ;
for ( const [ key , value ] of Object . entries ( response . headers ) ) {
response _raw += ( capitalize _all ( key ) + ": " + value + linebreak ) ;
}
response _raw += linebreak ;
response _raw += response . body ;
return response _raw ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
http . encode _response = encode _response ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* @ author fenris < frass @ greenscale . de >
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
function decode _response ( response _raw ) {
const lines = response _raw . split ( linebreak ) ;
const first = lines . shift ( ) ;
const first _parts = first . split ( " " ) ;
const version = first _parts [ 0 ] ;
const status _code = parseInt ( first _parts [ 1 ] ) ;
// first_parts.slice(2) ? probably irrelevant
let headers = { } ;
while ( true ) {
const line = lines . shift ( ) ;
if ( line === "" ) {
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
const [ key , value ] = line . split ( ": " , 2 ) ;
headers [ key . toLowerCase ( ) ] = value ;
2024-09-12 00:03:29 +02:00
}
}
2024-09-18 18:17:25 +02:00
// @ts-ignore
const body = Buffer . from ( lines . join ( linebreak ) ) ;
const response = {
// TODO
"version" : version ,
"status_code" : status _code ,
"headers" : headers ,
"body" : body ,
} ;
return response ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
http . decode _response = decode _response ;
2024-09-12 00:03:29 +02:00
/ * *
2024-09-18 18:17:25 +02:00
* executes an HTTP request
*
* @ todo define type _signal
2024-09-12 00:03:29 +02:00
* /
2024-09-18 18:17:25 +02:00
async function call ( request , options = { } ) {
options = Object . assign ( {
"timeout" : 5.0 ,
"follow_redirects" : false ,
"implementation" : "fetch" ,
} , options ) ;
const target = ( request . scheme
+
"://"
+
request . host
+
request . path
+
( ( request . query === null )
? ""
: request . query ) ) ;
switch ( options . implementation ) {
default : {
return Promise . reject ( "invalid implementation: " + options . implementation ) ;
break ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
case "fetch" : {
function core ( signal ) {
return ( fetch ( target , Object . assign ( {
"method" : ( ( method => {
switch ( method ) {
case http . enum _method . head : return "HEAD" ;
case http . enum _method . options : return "OPTIONS" ;
case http . enum _method . get : return "GET" ;
case http . enum _method . delete : return "DELETE" ;
case http . enum _method . post : return "POST" ;
case http . enum _method . put : return "PUT" ;
case http . enum _method . patch : return "PATCH" ;
}
} ) ( request . method ) ) ,
"headers" : request . headers ,
/ *
"redirect" : (
options . follow _redirects
?
"follow"
:
"manual"
) ,
* /
"signal" : ( signal
? ?
undefined ) ,
// "keepalive": false,
} , ( ( ( ( method => {
switch ( method ) {
case http . enum _method . head : return false ;
case http . enum _method . options : return false ;
case http . enum _method . get : return false ;
case http . enum _method . delete : return false ;
case http . enum _method . post : return true ;
case http . enum _method . put : return true ;
case http . enum _method . patch : return true ;
}
} ) ( request . method ) )
&&
( request . body !== null ) )
? {
"body" : request . body . toString ( ) ,
}
: { } ) ) )
. catch ( ( reason ) => {
// console.info(reason);
return Promise . reject ( reason ) ;
} )
. then ( ( response _raw ) => ( response _raw . text ( )
. then ( ( body ) => Promise . resolve ( {
// TODO
"version" : null ,
"status_code" : response _raw . status ,
"headers" : ( ( headers _raw => {
let headers = { } ;
headers _raw . forEach ( ( value , key ) => {
headers [ key ] = value ;
2024-09-12 00:03:29 +02:00
} ) ;
2024-09-18 18:17:25 +02:00
return headers ;
} ) ( response _raw . headers ) ) ,
"body" : body ,
} ) ) ) ) ) ;
}
function timeout ( controller ) {
return ( new Promise ( ( resolve , reject ) => {
if ( options . timeout === null ) {
// do nothing (neither resolve nor reject ever)
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
else {
2024-09-18 18:17:25 +02:00
setTimeout ( ( ) => {
controller . abort ( ) ;
resolve ( null ) ;
} , ( options . timeout * 1000 ) ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ) ;
}
const controller = new AbortController ( ) ;
const signal = controller . signal ;
const response = await Promise . race ( [
timeout ( controller ) ,
core ( signal ) ,
] ) ;
if ( response === null ) {
throw ( new Error ( "http_request_timeout" ) ) ;
2024-09-12 00:03:29 +02:00
}
else {
2024-09-18 18:17:25 +02:00
return response ;
}
break ;
}
case "http_module" : {
// @ts-ignore
const nm _http = require ( "http" ) ;
// @ts-ignore
const nm _https = require ( "https" ) ;
return ( new Promise ( ( resolve , reject ) => {
const req = ( ( request . scheme === "https" )
? nm _https
: nm _http )
. request ( target , {
"method" : request . method ,
"headers" : request . headers ,
} , ( res ) => {
try {
let response _body = "" ;
res . setEncoding ( "utf8" ) ;
res . on ( "data" , ( chunk ) => {
response _body += chunk ;
} ) ;
res . on ( "end" , ( ) => {
resolve ( {
// TODO
"version" : null ,
"status_code" : res . statusCode ,
"headers" : res . headers ,
"body" : response _body ,
} ) ;
} ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
catch ( error ) {
reject ( error ) ;
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
} ) ;
req . on ( "error" , ( error ) => {
reject ( error ) ;
} ) ;
req . write ( request . body ) ;
req . end ( ) ;
} ) ) ;
break ;
}
2024-09-12 00:03:29 +02:00
}
}
2024-09-18 18:17:25 +02:00
http . call = call ;
} ) ( http = lib _plankton . http || ( lib _plankton . http = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : http « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : http « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : http « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : http « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var http ;
( function ( http ) {
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
class class _http _request {
/ * *
* @ author fenris
* /
constructor ( ) {
}
/ * *
* @ implementation
* @ author fenris
* /
encode ( x ) {
return http . encode _request ( x ) ;
}
/ * *
* @ implementation
* @ author fenris
* /
decode ( x ) {
return http . decode _request ( x ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
http . class _http _request = class _http _request ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
2024-09-18 18:17:25 +02:00
class class _http _response {
/ * *
* @ author fenris
* /
constructor ( ) {
}
/ * *
* @ implementation
* @ author fenris
* /
encode ( x ) {
return http . encode _response ( x ) ;
}
/ * *
* @ implementation
* @ author fenris
* /
decode ( x ) {
return http . decode _response ( x ) ;
}
2024-09-12 00:03:29 +02:00
}
2024-09-18 18:17:25 +02:00
http . class _http _response = class _http _response ;
} ) ( http = lib _plankton . http || ( lib _plankton . http = { } ) ) ;
2024-09-12 00:03:29 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : markdown « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : markdown « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : markdown « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : markdown « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var markdown ;
( function ( markdown ) {
/ * *
* @ author fenris
* /
function code ( content ) {
return lib _plankton . string . coin ( "`{{content}}`" , {
"content" : content
} ) ;
}
markdown . code = code ;
/ * *
* @ author fenris
* /
function paragraph ( content ) {
return lib _plankton . string . coin ( "{{content}}\n\n" , {
"content" : content
} ) ;
}
markdown . paragraph = paragraph ;
/ * *
* @ author fenris
* /
function sectionhead ( level , content ) {
return lib _plankton . string . coin ( "{{grids}} {{content}}\n\n" , {
"grids" : lib _plankton . string . repeat ( "#" , level ) ,
"content" : content
} ) ;
}
markdown . sectionhead = sectionhead ;
} ) ( markdown = lib _plankton . markdown || ( lib _plankton . markdown = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : api « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : api « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : api « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : api « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var api ;
( function ( api ) {
/ * *
* /
let enum _checklevel ;
( function ( enum _checklevel ) {
enum _checklevel [ "none" ] = "none" ;
enum _checklevel [ "soft" ] = "soft" ;
enum _checklevel [ "hard" ] = "hard" ;
} ) ( enum _checklevel = api . enum _checklevel || ( api . enum _checklevel = { } ) ) ;
/ * *
* /
class class _error _permission _denied extends Error {
}
api . class _error _permission _denied = class _error _permission _denied ;
} ) ( api = lib _plankton . api || ( lib _plankton . api = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : api « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : api « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : api « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : api « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var api ;
( function ( api _1 ) {
/ * *
* @ throws Error if the inspection had foundings and the level is high enough
* @ author fenris
* /
function validate ( level , shape , value , options = { } ) {
options = lib _plankton . object . patched ( {
"kind" : "value" ,
"message_factory" : ( ( kind , findings ) => ( "malformed " + kind + ": " + findings . join ( "; " ) ) ) ,
} , options ) ;
if ( level === api _1 . enum _checklevel . none ) {
return value ;
}
else {
const inspection = lib _plankton . shape . inspect _flat ( shape , value ) ;
if ( inspection . length === 0 ) {
return value ;
}
else {
const message = options . message _factory ( options . kind , inspection ) ;
switch ( level ) {
case api _1 . enum _checklevel . soft : {
console . warn ( message ) ;
return value ;
break ;
}
case api _1 . enum _checklevel . hard :
default :
{
throw ( new Error ( message ) ) ;
2024-09-10 01:11:51 +02:00
break ;
}
2024-09-12 00:03:29 +02:00
}
}
}
}
/ * *
* @ author fenris
* /
function make ( title ) {
const api = {
"title" : title ,
"actions" : { } ,
} ;
return api ;
}
api _1 . make = make ;
/ * *
* @ throws Error if a action with the given name has already been registered
* @ author fenris
* /
function register ( api , name , options = { } ) {
options = /*lib_plankton.object.patched*/ Object . assign ( {
"active" : ( version ) => true ,
"execution" : ( version , environment , input ) => lib _plankton . call . promise _reject ( "not implemented" ) ,
"restriction" : ( version , environment ) => true ,
"input_shape" : ( version ) => ( { "kind" : "any" } ) ,
"output_shape" : ( version ) => ( { "kind" : "any" } ) ,
"title" : null ,
"description" : null ,
} , options ) ;
if ( api . actions . hasOwnProperty ( name ) ) {
throw ( new Error ( "an action with the name '" + name + "' has already been registered" ) ) ;
}
else {
const action = {
"name" : name ,
"active" : options . active ,
"execution" : options . execution ,
"restriction" : options . restriction ,
"input_shape" : options . input _shape ,
"output_shape" : options . output _shape ,
"title" : options . title ,
"description" : options . description ,
} ;
api . actions [ name ] = action ;
}
}
api _1 . register = register ;
/ * *
* @ throws Error if not found
* @ author fenris
* /
function get _action ( api , name ) {
if ( api . actions . hasOwnProperty ( name ) ) {
const action = api . actions [ name ] ;
return action ;
}
else {
throw ( new Error ( "no action with name '" + name + "'" ) ) ;
}
}
api _1 . get _action = get _action ;
/ * *
* @ author fenris
* /
function call ( api , name , options = { } ) {
options = /*lib_plankton.object.patched*/ Object . assign ( {
"version" : null ,
"input" : null ,
"environment" : { } ,
"checklevel_restriction" : api _1 . enum _checklevel . hard ,
"checklevel_input" : api _1 . enum _checklevel . soft ,
"checklevel_output" : api _1 . enum _checklevel . soft ,
} , options ) ;
return ( lib _plankton . call . promise _resolve ( undefined )
// get action
. then ( ( ) => lib _plankton . call . promise _resolve ( get _action ( api , name ) ) )
. then ( ( action ) => ( lib _plankton . call . promise _resolve ( undefined )
// check permission
. then ( ( ) => {
let conf ;
switch ( options . checklevel _restriction ) {
case api _1 . enum _checklevel . none : {
conf = {
"actual_check" : false ,
"escalate" : false ,
} ;
break ;
}
case api _1 . enum _checklevel . soft : {
conf = {
"actual_check" : true ,
"escalate" : false ,
} ;
break ;
}
default :
case api _1 . enum _checklevel . hard : {
conf = {
"actual_check" : true ,
"escalate" : true ,
} ;
break ;
}
}
return ( ( conf . actual _check
? action . restriction ( options . version , options . environment )
: Promise . resolve ( true ) )
. then ( ( valid ) => {
if ( ! valid ) {
if ( conf . escalate ) {
return Promise . reject ( new api _1 . class _error _permission _denied ( ) ) ;
}
else {
lib _plankton . log . warning ( "api_permission_missing" , {
"version" : options . version ,
"environment" : options . environment ,
"action_name" : action . name ,
} ) ;
return Promise . resolve ( null ) ;
}
}
else {
return Promise . resolve ( null ) ;
}
} ) ) ;
} )
// validate and adjust input
. then ( ( ) => lib _plankton . call . promise _resolve ( validate ( options . checklevel _input , action . input _shape ( options . version ) , options . input , {
"kind" : "input" ,
} ) ) )
// execute
. then ( ( input ) => action . execution ( options . version , options . environment , options . input ) )
// validate output
. then ( ( output ) => lib _plankton . call . promise _resolve ( validate ( options . checklevel _output , action . output _shape ( options . version ) , output , {
"kind" : "output" ,
} ) ) ) ) ) ) ;
}
api _1 . call = call ;
/ * *
* @ author fenris
* /
function generate _documentation _for _action ( api , name , options = { } ) {
options = Object . assign ( {
"version" : null ,
} , options ) ;
const action = get _action ( api , name ) ;
let result = "" ;
if ( ! action . active ( options . version ) ) {
// do nothing
}
else {
// name
{
result += lib _plankton . markdown . sectionhead ( 2 , lib _plankton . markdown . code ( action . name ) ) ;
}
// description
{
result += lib _plankton . markdown . sectionhead ( 3 , "Description" ) ;
result += lib _plankton . markdown . paragraph ( action . description ? ? "-" ) ;
}
// input shape
{
result += lib _plankton . markdown . sectionhead ( 3 , "Input" ) ;
result += lib _plankton . markdown . paragraph ( lib _plankton . markdown . code ( lib _plankton . shape . show ( action . input _shape ( options . version ) ) ) ) ;
}
// output shape
{
result += lib _plankton . markdown . sectionhead ( 3 , "Output" ) ;
result += lib _plankton . markdown . paragraph ( lib _plankton . markdown . code ( lib _plankton . shape . show ( action . output _shape ( options . version ) ) ) ) ;
}
}
return result ;
}
api _1 . generate _documentation _for _action = generate _documentation _for _action ;
/ * *
* @ author fenris
* /
function generate _documentation ( api , options = { } ) {
options = Object . assign ( {
"version" : null ,
} , options ) ;
let result = "" ;
result += lib _plankton . markdown . paragraph ( api . title ) ;
result += lib _plankton . markdown . sectionhead ( 1 , "Actions" ) ;
// iterate through actions and use "generate_documentation_for_action"
Object . entries ( api . actions )
. forEach ( ( [ action _name , action ] ) => {
result += generate _documentation _for _action ( api , action _name , {
"version" : options . version ,
} ) ;
} ) ;
return result ;
}
api _1 . generate _documentation = generate _documentation ;
} ) ( api = lib _plankton . api || ( lib _plankton . api = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : api « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : api « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : api « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : api « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var api ;
( function ( api ) {
/ * *
* @ author fenris
* /
class class _api {
/ * *
* @ author fenris
* /
constructor ( subject ) {
this . subject = subject ;
}
/ * *
* @ author fenris
* /
static create ( name ) {
const subject = api . make ( name ) ;
return ( new class _api ( subject ) ) ;
}
/ * *
* @ author fenris
* /
register ( name , options = { } ) {
return api . register ( this . subject , name , options ) ;
}
/ * *
* @ author fenris
* /
call ( name , options = { } ) {
return api . call ( this . subject , name , options ) ;
}
/ * *
* @ author fenris
* /
generate _documentation _for _action ( name ) {
return api . generate _documentation _for _action ( this . subject , name ) ;
}
/ * *
* @ author fenris
* /
generate _documentation ( ) {
return api . generate _documentation ( this . subject ) ;
}
}
api . class _api = class _api ;
} ) ( api = lib _plankton . api || ( lib _plankton . api = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : rest « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : rest « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : rest « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : rest « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : rest « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : rest « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : rest « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : rest « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var rest ;
( function ( rest _1 ) {
/ * *
* /
function http _request _method _to _oas ( http _request _method ) {
switch ( http _request _method ) {
case lib _plankton . http . enum _method . get : return "get" ;
case lib _plankton . http . enum _method . post : return "post" ;
case lib _plankton . http . enum _method . patch : return "patch" ;
case lib _plankton . http . enum _method . head : return "head" ;
case lib _plankton . http . enum _method . delete : return "delete" ;
case lib _plankton . http . enum _method . options : return "options" ;
case lib _plankton . http . enum _method . put : return "put" ;
default : throw ( new Error ( "impossible" ) ) ;
}
}
/ * *
* /
function wildcard _step _decode ( step ) {
const matches = ( new RegExp ( "^:(.*)$" ) ) . exec ( step ) ;
if ( ( matches === null ) || ( matches . length < 2 ) ) {
return null ;
}
else {
return matches [ 1 ] ;
}
}
/ * *
* /
function wildcard _step _encode ( name ) {
return ( ":" + name ) ;
}
/ * *
* /
function routenode _iterate ( routenode , procedure , steps ) {
procedure ( routenode , steps ) ;
Object . entries ( routenode . sub _branch ) . forEach ( ( [ key , value ] ) => {
routenode _iterate ( value , procedure , steps . concat ( [ key ] ) ) ;
} ) ;
if ( ! ( routenode . sub _wildcard === null ) ) {
routenode _iterate ( routenode . sub _wildcard . node , procedure , steps . concat ( [ wildcard _step _encode ( routenode . sub _wildcard . name ) ] ) ) ;
}
}
/ * *
* /
function routenode _flatten ( routenode ) {
let list = [ ] ;
routenode _iterate ( routenode , ( routenode _ , steps ) => {
list . push ( { "steps" : steps , "node" : routenode _ } ) ;
} , [ ] ) ;
return list ;
}
/ * *
* /
function routenode _spawn ( steps , http _method , operation ) {
let routenode ;
if ( steps . length <= 0 ) {
routenode = {
"operations" : Object . fromEntries ( [ [ http _method , operation ] ] ) ,
"sub_branch" : { } ,
"sub_wildcard" : null ,
} ;
}
else {
const steps _head = steps [ 0 ] ;
const steps _tail = steps . slice ( 1 ) ;
const sub = routenode _spawn ( steps _tail , http _method , operation ) ;
const wildcard _name = wildcard _step _decode ( steps _head ) ;
if ( wildcard _name === null ) {
// branch
routenode = {
"operations" : { } ,
"sub_branch" : Object . fromEntries ( [ [ steps _head , sub ] ] ) ,
"sub_wildcard" : null ,
} ;
}
else {
// wildcard
routenode = {
"operations" : { } ,
"sub_branch" : { } ,
"sub_wildcard" : { "name" : wildcard _name , "node" : sub } ,
} ;
}
}
return routenode ;
}
/ * *
* /
function routenode _path _read ( routenode , steps ) {
if ( steps . length <= 0 ) {
return {
"steps" : [ ] ,
"rest" : [ ] ,
"routenode" : routenode ,
"parameters" : { } ,
} ;
}
else {
const path _head = steps [ 0 ] ;
const path _tail = steps . slice ( 1 ) ;
if ( path _head in routenode . sub _branch ) {
const result = routenode _path _read ( routenode . sub _branch [ path _head ] , path _tail ) ;
return {
"steps" : [ path _head ] . concat ( result . steps ) ,
"rest" : result . rest ,
"routenode" : result . routenode ,
"parameters" : result . parameters ,
} ;
}
else {
if ( ! ( routenode . sub _wildcard === null ) ) {
const result = routenode _path _read ( routenode . sub _wildcard . node , path _tail ) ;
if ( ! ( routenode . sub _wildcard . name in result . parameters ) ) {
// do nothing
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
else {
lib _plankton . log . warning ( "rest_overwriting_path_parameter" , {
"key" : routenode . sub _wildcard . name ,
"value_old" : result . parameters [ routenode . sub _wildcard . name ] ,
"value_new" : path _head ,
} ) ;
}
return {
"steps" : [ path _head ] . concat ( result . steps ) ,
"rest" : result . rest ,
"routenode" : result . routenode ,
"parameters" : lib _plankton . object . patched ( Object . fromEntries ( [ [ routenode . sub _wildcard . name , path _head ] ] ) , result . parameters ) ,
} ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
else {
return {
"steps" : [ path _head ] ,
"rest" : path _tail ,
"routenode" : routenode ,
"parameters" : { } ,
} ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-12 00:03:29 +02:00
function routenode _path _write ( routenode , steps , http _method , operation , options = { } ) {
options = lib _plankton . object . patched ( {
"create" : false ,
} , options ) ;
if ( steps . length <= 0 ) {
if ( ! ( http _method in routenode . operations ) ) {
// do nothing
}
else {
lib _plankton . log . warning ( "rest_overwriting_action" , {
"http_method" : http _method ,
"steps" : steps ,
} ) ;
}
routenode . operations [ http _method ] = operation ;
}
else {
const steps _head = steps [ 0 ] ;
const steps _tail = steps . slice ( 1 ) ;
const wildcard _name = wildcard _step _decode ( steps _head ) ;
if ( ! ( wildcard _name === null ) ) {
// wildcard
if ( routenode . sub _wildcard === null ) {
if ( ! options . create ) {
throw ( new Error ( "may not create missing route" ) ) ;
}
else {
routenode . sub _wildcard = {
"name" : wildcard _name ,
"node" : routenode _spawn ( steps _tail , http _method , operation ) ,
} ;
}
}
else {
if ( ! ( routenode . sub _wildcard . name === wildcard _name ) ) {
/ *
lib _plankton . log . warning (
"rest_overwriting_wildcard_node" ,
{
"wildcard_name" : wildcard _name ,
}
) ;
* /
throw ( new Error ( "inconsistent wildcard name: '" + routenode . sub _wildcard . name + "' vs. '" + wildcard _name + "'" ) ) ;
}
else {
// walk
routenode _path _write ( routenode . sub _wildcard . node , steps _tail , http _method , operation , options ) ;
}
}
}
else {
if ( steps _head in routenode . sub _branch ) {
// walk branch
routenode _path _write ( routenode . sub _branch [ steps _head ] , steps _tail , http _method , operation , options ) ;
}
else {
// add branch
if ( ! options . create ) {
throw ( new Error ( "may not create missing route" ) ) ;
}
else {
routenode . sub _branch [ steps _head ] = routenode _spawn ( steps _tail , http _method , operation ) ;
}
}
}
}
2024-09-10 01:11:51 +02:00
}
/ * *
* /
2024-09-12 00:03:29 +02:00
function make ( options = { } ) {
options = lib _plankton . object . patched ( {
"title" : "REST-API" ,
"versioning_method" : "none" ,
"versioning_header_name" : "X-Api-Version" ,
"versioning_query_key" : "version" ,
"header_parameters" : [ ] ,
"set_access_control_headers" : false ,
"authentication" : {
"kind" : "none" ,
"parameters" : { } ,
} ,
"actions" : [ ] ,
} , options ) ;
const subject = {
"api" : lib _plankton . api . make ( options . title ) ,
"versioning_method" : options . versioning _method ,
"versioning_header_name" : options . versioning _header _name ,
"versioning_query_key" : options . versioning _query _key ,
"routetree" : {
"operations" : { } ,
"sub_branch" : { } ,
"sub_wildcard" : null ,
} ,
"header_parameters" : options . header _parameters ,
"set_access_control_headers" : options . set _access _control _headers ,
"authentication" : options . authentication ,
} ;
options . actions . forEach ( action _definition => {
rest . register ( subject , action _definition . http _method , action _definition . path , action _definition . options ) ;
2024-09-10 01:11:51 +02:00
} ) ;
2024-09-12 00:03:29 +02:00
return subject ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
rest _1 . make = make ;
/ * *
* /
function register ( rest , http _method , path , options ) {
options = lib _plankton . object . patched ( {
"active" : ( ( version ) => true ) ,
"execution" : ( ( stuff ) => Promise . resolve ( { "status_code" : 501 , "data" : null } ) ) ,
"restriction" : ( ( stuff ) => Promise . resolve ( true ) ) ,
"title" : null ,
"description" : null ,
2024-09-19 13:34:07 +02:00
"query_parameters" : ( ( version ) => ( [ ] ) ) ,
"input_schema" : ( ( version ) => ( { } ) ) ,
"output_schema" : ( ( version ) => ( { } ) ) ,
2024-09-12 00:03:29 +02:00
"request_body_mimetype" : "application/json" ,
"request_body_decode" : ( ( http _request _body , http _request _header _content _type ) => ( ( ( http _request _header _content _type !== null )
&&
( http _request _header _content _type . startsWith ( "application/json" ) )
&&
( http _request _body !== null )
&&
( http _request _body . toString ( ) !== "" ) )
? JSON . parse ( http _request _body . toString ( ) )
: ( ( http _request _body !== null )
? http _request _body . toString ( )
: null ) ) ) ,
"response_body_mimetype" : "application/json" ,
// TODO: no "from"?
"response_body_encode" : ( ( output ) => Buffer [ "from" ] ( JSON . stringify ( output ) ) ) ,
} , options ) ;
const steps = lib _plankton . string . split ( path , "/" ) . slice ( 1 ) ;
const steps _enriched = ( ( rest . versioning _method === "path" )
? [ "{version}" ] . concat ( steps )
: steps ) ;
const action _name = ( steps . concat ( [ lib _plankton . http . encode _method ( http _method ) . toLowerCase ( ) ] )
. join ( "_" ) ) ;
const operation = {
"action_name" : action _name ,
"query_parameters" : options . query _parameters ,
"request_body_mimetype" : options . request _body _mimetype ,
"request_body_decode" : options . request _body _decode ,
"response_body_mimetype" : options . response _body _mimetype ,
"response_body_encode" : options . response _body _encode ,
"input_schema" : options . input _schema ,
"output_schema" : options . output _schema ,
} ;
routenode _path _write ( rest . routetree , steps _enriched , http _method , operation , {
"create" : true ,
} ) ;
lib _plankton . api . register ( rest . api , action _name , {
"active" : options . active ,
"execution" : ( version , environment , input ) => options . execution ( {
"version" : version ,
"path_parameters" : environment . path _parameters ,
"query_parameters" : environment . query _parameters ,
"headers" : environment . headers ,
"input" : input
} ) ,
"restriction" : ( version , environment ) => options . restriction ( {
"version" : version ,
"path_parameters" : environment . path _parameters ,
"query_parameters" : environment . query _parameters ,
"headers" : environment . headers ,
} ) ,
"title" : options . title ,
"description" : options . description ,
// TODO
// "input_shape": options.input_type,
// "output_shape": options.output_type,
} ) ;
lib _plankton . log . debug ( "rest_route_added" , {
"http_method" : http _method ,
"path" : path ,
// "routetree": rest.routetree,
2024-09-10 01:11:51 +02:00
} ) ;
}
2024-09-12 00:03:29 +02:00
rest _1 . register = register ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ todo check request body mimetype ?
* @ todo check query paramater validity
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
async function call ( rest , http _request , options = { } ) {
options = /*lib_plankton.object.patched*/ Object . assign ( {
"checklevel_restriction" : lib _plankton . api . enum _checklevel . hard ,
"checklevel_input" : lib _plankton . api . enum _checklevel . soft ,
"checklevel_output" : lib _plankton . api . enum _checklevel . soft ,
} , options ) ;
lib _plankton . log . info ( "rest_call" , {
"http_request" : {
"scheme" : http _request . scheme ,
"host" : http _request . host ,
"path" : http _request . path ,
"version" : http _request . version ,
"method" : http _request . method ,
"query" : http _request . query ,
"headers" : http _request . headers ,
"body" : String ( http _request . body ) ,
}
} ) ;
// parse target and query parameters
// const url_stuff : URL = new URL("http://dummy" + http_request.target);
const path = http _request . path ;
const query _parameters _raw = new URLSearchParams ( http _request . query ) ;
let query _parameters = { } ;
for ( const [ key , value ] of query _parameters _raw ) {
query _parameters [ key ] = value ;
}
const steps = lib _plankton . string . split ( path , "/" ) . slice ( 1 ) ;
if ( steps . length <= 0 ) {
throw ( new Error ( "empty path" ) ) ;
}
else {
// resolve
const stuff = routenode _path _read ( rest . routetree , steps ) ;
const allowed _methods = ( Object . keys ( stuff . routenode . operations )
. map ( x => lib _plankton . http . encode _method ( x ) )
. join ( ", " ) ) ;
// get version
let version ;
switch ( rest . versioning _method ) {
case "none" : {
version = null ;
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
case "path" : {
version = stuff . parameters [ "version" ] ;
// delete stuff.parameters["version"];
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
case "header" : {
version = http _request . headers [ rest . versioning _header _name ] ;
// delete http_request.headers[rest.versioning_header_name];
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
case "query" : {
version = query _parameters [ rest . versioning _query _key ] ;
// delete query_parameters[rest.versioning_query_key];
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
default : {
throw ( new Error ( "unhandled versioning method: " + rest . versioning _method ) ) ;
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
}
const additional _response _headers = ( rest . set _access _control _headers
? {
"Access-Control-Allow-Headers" : ( ( [
"Content-Type" ,
"X-Api-Key" ,
]
. concat ( ( rest . versioning _header _name !== null )
? [ rest . versioning _header _name ]
: [ ] )
. concat ( ( rest . authentication . kind === "key_header" )
? [ rest . authentication . parameters [ "name" ] ]
: [ ] ) )
. join ( ", " ) ) ,
"Access-Control-Allow-Origin" : "*" ,
"Access-Control-Allow-Methods" : allowed _methods ,
}
: { } ) ;
if ( stuff . rest . length > 0 ) {
return {
"version" : "HTTP/1.1" ,
"status_code" : 404 ,
"headers" : { } ,
"body" : null ,
} ;
}
else {
if ( http _request . method === lib _plankton . http . enum _method . options ) {
return {
"version" : "HTTP/1.1" ,
"status_code" : 200 ,
"headers" : Object . assign ( { } , additional _response _headers ) ,
"body" : null ,
} ;
}
else {
if ( ! ( http _request . method in stuff . routenode . operations ) ) {
if ( Object . keys ( stuff . routenode . operations ) . length <= 0 ) {
return {
"version" : "HTTP/1.1" ,
"status_code" : 404 ,
"headers" : Object . assign ( { } , additional _response _headers ) ,
"body" : null ,
} ;
}
else {
return {
"version" : "HTTP/1.1" ,
"status_code" : 405 ,
"headers" : Object . assign ( {
"Allow" : allowed _methods ,
} , additional _response _headers ) ,
"body" : null ,
} ;
}
}
else {
// call
let result ;
let error ;
const operation = stuff . routenode . operations [ http _request . method ] ;
const stuff _ = {
"version" : version ,
"headers" : http _request . headers ,
"path_parameters" : stuff . parameters ,
"query_parameters" : query _parameters ,
"input" : ( ( http _request . body === null )
? null
: operation . request _body _decode ( http _request . body , ( http _request . headers [ "Content-Type" ]
? ?
http _request . headers [ "content-type" ]
? ?
null ) ) ) ,
} ;
/ *
const allowed : boolean = (
( operation . restriction === null )
? true
: operation . restriction ( stuff _ )
) ;
* /
let response ;
/ *
if ( ! allowed ) {
lib _plankton . log . error (
"rest_access_denied" ,
{
"http_request" : {
"target" : http _request . target ,
"method" : http _request . method ,
"headers" : http _request . headers ,
"body" : (
( http _request . body === null )
? null
: lib _plankton . string . limit ( http _request . body . toString ( ) , { "length" : 200 } )
) ,
} ,
}
) ;
response = {
"version" : "HTTP/1.1" ,
"status_code" : 403 ,
"headers" : Object . assign (
{
} ,
additional _response _headers
) ,
"body" : Buffer [ "from" ] ( "forbidden" ) ,
} ;
}
else * / {
try {
result = await lib _plankton . api . call ( rest . api , operation . action _name , {
"version" : stuff _ . version ,
"environment" : {
"headers" : stuff _ . headers ,
"path_parameters" : stuff _ . path _parameters ,
"query_parameters" : stuff _ . query _parameters ,
} ,
"input" : stuff _ . input ,
"checklevel_restriction" : options . checklevel _restriction ,
"checklevel_input" : options . checklevel _input ,
"checklevel_output" : options . checklevel _output ,
} ) ;
error = null ;
}
catch ( error _ ) {
result = null ;
error = error _ ;
}
if ( ( result === null ) || ( error !== null ) ) {
if ( error instanceof lib _plankton . api . class _error _permission _denied ) {
response = {
"version" : "HTTP/1.1" ,
"status_code" : 403 ,
"headers" : Object . assign ( { } , additional _response _headers ) ,
"body" : null ,
} ;
}
else {
lib _plankton . log . error ( "rest_execution_failed" , {
"http_request" : {
"version" : http _request . version ,
"scheme" : http _request . scheme ,
"method" : http _request . method ,
"path" : http _request . path ,
"query" : http _request . query ,
"headers" : http _request . headers ,
"body" : ( ( http _request . body === null )
? null
: lib _plankton . string . limit ( http _request . body . toString ( ) , { "length" : 200 } ) ) ,
} ,
"error" : {
"message" : error . toString ( ) ,
"file_name" : error . fileName ,
"line_number" : error . lineNumber ,
"stack" : error . stack ,
} ,
} ) ;
response = {
"version" : "HTTP/1.1" ,
"status_code" : 500 ,
"headers" : Object . assign ( { } , additional _response _headers ) ,
"body" : Buffer [ "from" ] ( "internal error" ) ,
} ;
}
}
else {
// encode
response = {
"version" : "HTTP/1.1" ,
"status_code" : result . status _code ,
"headers" : Object . assign ( {
"Content-Type" : operation . response _body _mimetype ,
} , additional _response _headers ) ,
"body" : operation . response _body _encode ( result . data ) ,
} ;
}
}
return response ;
}
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
}
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
rest _1 . call = call ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ see https : //swagger.io/specification/#openrest-object
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
function to _oas ( rest , options = { } ) {
options = lib _plankton . object . patched ( {
"version" : null ,
"servers" : [ ] ,
} , options ) ;
const subject = rest ;
const version = ( options . version ? ? "-" ) ;
return {
"openapi" : "3.0.3" ,
"info" : {
"version" : version ,
"title" : ( rest . api . title ? ? "API" ) ,
// "description": (rest.api.description ?? undefined),
} ,
"servers" : options . servers . map ( url => ( { "url" : url } ) ) ,
"components" : {
"securitySchemes" : ( ( ( description ) => ( {
"none" : { } ,
"key_header" : {
"default_security_schema" : {
"type" : "restKey" ,
"in" : "header" ,
"name" : description . parameters [ "name" ] ,
} ,
} ,
} [ description . kind ] ) ) ( rest . authentication ) ) ,
} ,
"security" : [
{
"default_security_schema" : [ ] ,
}
] ,
"paths" : lib _plankton . call . convey ( rest . routetree , [
routenode _flatten ,
( x ) => x . map ( ( entry ) => {
const steps _ = entry . steps ;
const path = lib _plankton . string . join ( steps _ , "_" ) ;
const key = ( ( steps _ . length <= 0 )
? "/"
: lib _plankton . string . join ( [ "" ] . concat ( steps _
. map ( step => ( ( wildcard _step _decode ( step ) !== null )
? ( "{" + wildcard _step _decode ( step ) + "}" )
: step ) ) ) , "/" ) ) ;
return [
key ,
lib _plankton . call . convey ( entry . node . operations , [
x => Object . entries ( x ) ,
( pairs ) => pairs . map ( ( [ http _method , operation ] ) => ( [
http _request _method _to _oas ( http _method ) ,
{
"operationId" : ( http _request _method _to _oas ( http _method )
+
"_"
+
path ) ,
"summary" : ( operation . title
? ?
[ "" ] . concat ( steps _ ) . join ( " " ) ) ,
"description" : ( lib _plankton . api . get _action ( rest . api , operation . action _name ) . description
? ?
"(missing)" ) ,
"parameters" : [ ] . concat (
// header parameters
rest . header _parameters . map ( header _parameter => ( {
"name" : header _parameter . name ,
"in" : "header" ,
"required" : header _parameter . required ,
"schema" : {
"type" : "string" ,
} ,
"description" : ( header _parameter . description ? ? undefined ) ,
} ) ) ,
// path parameters
lib _plankton . call . convey ( steps _ , [
x => x . map ( y => wildcard _step _decode ( y ) ) ,
x => x . filter ( y => ( ! ( y === null ) ) ) ,
x => x . map ( y => ( {
"name" : y ,
"in" : "path" ,
"required" : true ,
"schema" : {
"type" : "string" ,
} ,
} ) ) ,
] ) ,
// query parameters
2024-09-19 13:34:07 +02:00
operation . query _parameters ( options . version ) . map ( ( query _parameter ) => ( {
2024-09-12 00:03:29 +02:00
"name" : query _parameter . name ,
"in" : "query" ,
"required" : query _parameter . required ,
"schema" : {
"type" : "string" ,
} ,
"description" : ( query _parameter . description ? ? undefined ) ,
} ) ) ) ,
"requestBody" : ( ( [
lib _plankton . http . enum _method . get ,
lib _plankton . http . enum _method . head ,
lib _plankton . http . enum _method . delete ,
] . includes ( http _method ) )
? undefined
: {
"content" : Object . fromEntries ( [
[
operation . request _body _mimetype ,
{
"schema" : operation . input _schema ( options . version ) ,
}
]
] )
} ) ,
"responses" : {
"default" : {
"description" : "" ,
"content" : Object . fromEntries ( [
[
operation . response _body _mimetype ,
{
"schema" : operation . output _schema ( options . version ) ,
}
]
] ) ,
}
} ,
}
] ) ) ,
( pairs ) => Object . fromEntries ( pairs ) ,
] ) ,
] ;
} ) ,
x => x . filter ( y => ( Object . keys ( y [ 1 ] ) . length > 0 ) ) ,
x => Object . fromEntries ( x ) ,
] )
} ;
}
rest _1 . to _oas = to _oas ;
} ) ( rest = lib _plankton . rest || ( lib _plankton . rest = { } ) ) ;
2024-09-10 01:11:51 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-12 00:03:29 +02:00
This file is part of » bacterio - plankton : server « .
2024-09-10 01:11:51 +02:00
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
2024-09-12 00:03:29 +02:00
» bacterio - plankton : server « is free software : you can redistribute it and / or modify
2024-09-10 01:11:51 +02:00
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
2024-09-12 00:03:29 +02:00
» bacterio - plankton : server « is distributed in the hope that it will be useful ,
2024-09-10 01:11:51 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
2024-09-12 00:03:29 +02:00
along with » bacterio - plankton : server « . If not , see < http : //www.gnu.org/licenses/>.
2024-09-10 01:11:51 +02:00
* /
var lib _plankton ;
( function ( lib _plankton ) {
2024-09-12 00:03:29 +02:00
var server ;
( function ( server ) {
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
function make ( handle , options = { } ) {
options = Object . assign ( {
"host" : "::" ,
"port" : 9999 ,
"threshold" : 0.25 ,
} , options ) ;
return {
"host" : options . host ,
"port" : options . port ,
"threshold" : options . threshold ,
"handle" : handle ,
"serverobj" : undefined ,
} ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
server . make = make ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
* @ deprecated
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
function make _old ( port , handle ) {
return make ( handle , {
"host" : "::" ,
"port" : port ,
"threshold" : 0.25 ,
} ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
server . make _old = make _old ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
* @ see https : //nodejs.org/api/net.html#serverlistenport-host-backlog-callback
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
function start ( subject ) {
const net = require ( "net" ) ;
return ( new Promise ( ( resolve , reject ) => {
// @ts-ignore
let input _chunks = [ ] ;
subject . serverobj = net . createServer ( {
"allowHalfOpen" : false ,
} , ( socket ) => {
let timeout _handler = null ;
let ended = false ;
const process _input = function ( ) {
// @ts-ignore
const input = Buffer . concat ( input _chunks ) ;
/ *
const metadata : type _metadata = {
"ip_address" : socket . remoteAddress ,
} ;
* /
lib _plankton . log . debug ( "server_process_input" , {
"input" : input ,
} ) ;
( subject . handle ( input /*, metadata*/ )
. then ( ( output ) => {
lib _plankton . log . debug ( "server_writing" , {
"output" : output ,
} ) ;
socket . write ( output ) ;
socket . end ( ) ;
} )
. catch ( ( error ) => {
lib _plankton . log . warning ( "server_handle_failed" , {
"error" : error . toString ( ) ,
} ) ;
// socket.write("");
socket . end ( ) ;
} )
. then ( ( ) => {
input _chunks = [ ] ;
} ) ) ;
} ;
const timeout _stop = function ( ) {
if ( timeout _handler === null ) {
// do nothing
}
else {
lib _plankton . log . debug ( "server_timeout_cancelling" ) ;
clearTimeout ( timeout _handler ) ;
timeout _handler = null ;
}
} ;
const timeout _start = function ( ) {
if ( subject . threshold === null ) {
process _input ( ) ;
}
else {
if ( timeout _handler === null ) {
timeout _handler = setTimeout ( ( ) => {
lib _plankton . log . debug ( "server_timeout_reached" ) ;
timeout _handler = null ;
process _input ( ) ;
} , ( subject . threshold * 1000 ) ) ;
}
else {
lib _plankton . log . warning ( "server_timeout_already_started" ) ;
// do nothing
}
}
} ;
lib _plankton . log . info ( "server_client connected" , { } ) ;
socket . on ( "data" , ( input _chunk _raw ) => {
lib _plankton . log . debug ( "server_reading_chunk" , {
"chunk_raw" : input _chunk _raw ,
} ) ;
timeout _stop ( ) ;
const input _chunk = ( ( input _chunk _raw instanceof Buffer )
?
input _chunk _raw
:
// @ts-ignore
Buffer . from ( input _chunk _raw ) ) ;
input _chunks . push ( input _chunk ) ;
timeout _start ( ) ;
} ) ;
socket . on ( "end" , ( ) => {
if ( ! ended ) {
lib _plankton . log . info ( "server_client_disconnected" , { } ) ;
ended = true ;
timeout _stop ( ) ;
}
else {
lib _plankton . log . info ( "server_socket_already_ended" ) ;
// do nothing
}
} ) ;
} ) ;
subject . serverobj . on ( "error" , ( error ) => {
// throw error;
process . stderr . write ( "net_error: " + String ( error ) + "\n\n" ) ;
} ) ;
subject . serverobj . listen ( subject . port , subject . host , 511 , ( ) => {
lib _plankton . log . info ( "server_listenting" , {
"host" : subject . host ,
"port" : subject . port ,
} ) ;
resolve ( undefined ) ;
} ) ;
} ) ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
server . start = start ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
function kill ( subject ) {
subject . serverobj . close ( ) ;
lib _plankton . log . info ( "server_stopped" , { } ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
server . kill = kill ;
} ) ( server = lib _plankton . server || ( lib _plankton . server = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : server « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : server « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : server « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : server « . If not , see < common : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var server ;
( function ( server ) {
/ * *
* @ author fenris
* /
class class _server {
/ * *
* @ author fenris
* /
constructor ( handle , options = { } ) {
options = Object . assign ( {
"host" : "::" ,
"port" : 9999 ,
} , options ) ;
this . subject = server . make ( handle , {
"host" : options . host ,
"port" : options . port ,
} ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
start ( ) {
return server . start ( this . subject ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
kill ( ) {
return server . kill ( this . subject ) ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
server . class _server = class _server ;
} ) ( server = lib _plankton . server || ( lib _plankton . server = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
var lib _server = lib _plankton . server ;
/ *
This file is part of » bacterio - plankton : args « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : args « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : args « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : args « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var args ;
( function ( args ) {
2024-09-10 01:11:51 +02:00
/ * *
* /
2024-09-12 00:03:29 +02:00
var enum _environment ;
( function ( enum _environment ) {
enum _environment [ "cli" ] = "cli" ;
enum _environment [ "url" ] = "url" ;
} ) ( enum _environment = args . enum _environment || ( args . enum _environment = { } ) ) ;
;
/ * *
* /
var enum _kind ;
( function ( enum _kind ) {
enum _kind [ "positional" ] = "positional" ;
enum _kind [ "volatile" ] = "volatile" ;
} ) ( enum _kind = args . enum _kind || ( args . enum _kind = { } ) ) ;
;
/ * *
* /
var enum _type ;
( function ( enum _type ) {
enum _type [ "boolean" ] = "boolean" ;
enum _type [ "integer" ] = "int" ;
enum _type [ "float" ] = "float" ;
enum _type [ "string" ] = "string" ;
} ) ( enum _type = args . enum _type || ( args . enum _type = { } ) ) ;
;
/ * *
* /
var enum _mode ;
( function ( enum _mode ) {
enum _mode [ "replace" ] = "replace" ;
enum _mode [ "accumulate" ] = "accumulate" ;
} ) ( enum _mode = args . enum _mode || ( args . enum _mode = { } ) ) ;
;
} ) ( args = lib _plankton . args || ( lib _plankton . args = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : args « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : args « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : args « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : args « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var args ;
( function ( args ) {
/ *
export enum _mode {
replace = "replace" ,
accumulate = "accumulate" ,
} ;
* /
/ * *
* @ author fenris
* /
var class _argument = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _argument ( _a ) {
var name = _a [ "name" ] , _b = _a [ "type" ] , type = _b === void 0 ? args . enum _type . string : _b , _c = _a [ "kind" ] , kind = _c === void 0 ? args . enum _kind . positional : _c , _d = _a [ "mode" ] , mode = _d === void 0 ? args . enum _mode . replace : _d , _e = _a [ "default" ] , default _ = _e === void 0 ? null : _e , _f = _a [ "info" ] , info = _f === void 0 ? null : _f , _g = _a [ "parameters" ] , parameters = _g === void 0 ? { } : _g , _h = _a [ "hidden" ] , hidden = _h === void 0 ? false : _h ;
this . name = name ;
this . type = type ;
this . kind = kind ;
this . mode = mode ;
this . default _ = default _ ;
this . info = info ;
this . parameters = parameters ;
this . hidden = hidden ;
if ( ! this . check ( ) ) {
throw ( new Error ( "invalid argument-setup" ) ) ;
}
}
/ * *
* @ author fenris
* /
class _argument . positional = function ( _a ) {
var name = _a [ "name" ] , _b = _a [ "type" ] , type = _b === void 0 ? args . enum _type . string : _b , _c = _a [ "mode" ] , mode = _c === void 0 ? args . enum _mode . replace : _c , _d = _a [ "default" ] , default _ = _d === void 0 ? null : _d , _e = _a [ "info" ] , info = _e === void 0 ? null : _e , _f = _a [ "hidden" ] , hidden = _f === void 0 ? false : _f , index = _a [ "index" ] ;
return ( new class _argument ( {
"name" : name ,
"kind" : args . enum _kind . positional ,
"type" : type ,
"mode" : mode ,
"default" : default _ ,
"info" : info ,
"hidden" : hidden ,
"parameters" : {
"index" : index
}
} ) ) ;
} ;
/ * *
* @ author fenris
* /
class _argument . volatile = function ( _a ) {
var name = _a [ "name" ] , _b = _a [ "type" ] , type = _b === void 0 ? args . enum _type . string : _b , _c = _a [ "mode" ] , mode = _c === void 0 ? args . enum _mode . replace : _c , _d = _a [ "default" ] , default _ = _d === void 0 ? null : _d , _e = _a [ "info" ] , info = _e === void 0 ? null : _e , _f = _a [ "hidden" ] , hidden = _f === void 0 ? false : _f , indicators _short = _a [ "indicators_short" ] , indicators _long = _a [ "indicators_long" ] ;
return ( new class _argument ( {
"name" : name ,
"kind" : args . enum _kind . volatile ,
"type" : type ,
"mode" : mode ,
"default" : default _ ,
"info" : info ,
"hidden" : hidden ,
"parameters" : {
"indicators_short" : indicators _short ,
"indicators_long" : indicators _long
}
} ) ) ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . check = function ( ) {
var _this = this ;
return [
function ( ) { return ( ( ! ( _this . kind == args . enum _kind . volatile ) )
||
( ( "indicators_long" in _this . parameters )
&&
( _this . parameters [ "indicators_long" ] [ "length" ] >= 0 ) ) ) ; } ,
] . every ( function ( condition ) { return condition ( ) ; } ) ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . name _get = function ( ) {
return this . name ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . kind _get = function ( ) {
return this . kind ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . type _get = function ( ) {
return this . type ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . mode _get = function ( ) {
return this . mode ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . default _get = function ( ) {
return this . default _ ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . parameters _get = function ( ) {
return this . parameters ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . hidden _get = function ( ) {
return this . hidden ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . toString = function ( ) {
return "<" . concat ( this . name , ">" ) ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . indicator _main = function ( ) {
if ( this . kind === args . enum _kind . volatile ) {
return this . parameters [ "indicators_long" ] [ 0 ] ;
2024-09-10 01:11:51 +02:00
}
else {
2024-09-12 00:03:29 +02:00
return null ;
2024-09-10 01:11:51 +02:00
}
} ;
2024-09-12 00:03:29 +02:00
/ * *
* @ author fenris
* /
class _argument . prototype . pattern _value = function ( ) {
switch ( this . type ) {
case args . enum _type . boolean : {
return "false|true" ;
break ;
}
case args . enum _type . integer : {
return "[0-9]+" ;
break ;
}
case args . enum _type . float : {
return "\\d*(?:\\.\\d+)?" ;
break ;
}
case args . enum _type . string : {
return "\\S+" ;
break ;
}
default : {
throw ( new Error ( "unhandled type " . concat ( this . type ) ) ) ;
break ;
}
}
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . extract = function ( raw ) {
switch ( this . type ) {
case args . enum _type . boolean : {
return ( raw != "false" ) ;
break ;
}
case args . enum _type . integer : {
return parseInt ( raw ) ;
break ;
}
case args . enum _type . float : {
return parseFloat ( raw ) ;
break ;
}
case args . enum _type . string : {
return raw ;
break ;
}
default : {
throw ( new Error ( "unhandled type " . concat ( this . type ) ) ) ;
break ;
}
}
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . assign = function ( data , target , raw ) {
var value = this . extract ( raw ) ;
switch ( this . mode ) {
case args . enum _mode . replace : {
data [ target ] = value ;
break ;
}
case args . enum _mode . accumulate : {
/ *
if ( ! ( this . name in data ) ) {
data [ this . name ] = [ ] ;
}
* /
data [ target ] . push ( value ) ;
break ;
}
default : {
throw ( new Error ( "unhandled mode " . concat ( this . mode ) ) ) ;
}
}
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . make = function ( data , target ) {
var value = data [ target ] ;
return value . toString ( ) ;
} ;
/ * *
* @ author fenris
* /
class _argument . prototype . generate _help = function ( ) {
var _this = this ;
var _a , _b , _c , _d ;
var output = "" ;
{
switch ( this . kind ) {
case args . enum _kind . positional : {
var line = "" ;
line += "\t" ;
line += "<" . concat ( this . name , ">" ) ;
line += "\n" ;
output += line ;
}
case args . enum _kind . volatile : {
var line = "" ;
line += "\t" ;
if ( this . type === args . enum _type . boolean ) {
line += ( [ ]
. concat ( ( ( _a = this . parameters [ "indicators_short" ] ) !== null && _a !== void 0 ? _a : [ ] ) . map ( function ( indicator ) { return ( "-" + indicator ) ; } ) )
. concat ( ( ( _b = this . parameters [ "indicators_long" ] ) !== null && _b !== void 0 ? _b : [ ] ) . map ( function ( indicator ) { return ( "--" + indicator ) ; } ) )
. join ( " | " ) ) ;
}
else {
line += ( [ ]
. concat ( ( ( _c = this . parameters [ "indicators_short" ] ) !== null && _c !== void 0 ? _c : [ ] ) . map ( function ( indicator ) { return ( "-" + indicator + " " + ( "<" + _this . name + ">" ) ) ; } ) )
. concat ( ( ( _d = this . parameters [ "indicators_long" ] ) !== null && _d !== void 0 ? _d : [ ] ) . map ( function ( indicator ) { return ( "--" + indicator + "=" + ( "<" + _this . name + ">" ) ) ; } ) )
. join ( " | " ) ) ;
}
line += "\n" ;
output += line ;
}
}
}
{
var line = "" ;
line += "\t\t" ;
var infotext = ( ( this . info == null ) ? "(no info available)" : this . info ) ;
line += infotext ;
if ( ( this . type != "boolean" ) && ( this . default _ != null ) ) {
line += "; default: " . concat ( this . default _ . toString ( ) ) ;
}
line += "\n" ;
output += line ;
}
return output ;
} ;
return class _argument ;
} ( ) ) ;
args . class _argument = class _argument ;
} ) ( args = lib _plankton . args || ( lib _plankton . args = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : args « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : args « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : args « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : args « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var args ;
( function ( args ) {
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
var settings = {
"environment" : {
"cli" : {
"symbols" : {
"delimiter" : " " ,
"prefix" : "--" ,
"assignment" : "="
}
} ,
"url" : {
"symbols" : {
"delimiter" : "&" ,
"prefix" : "" ,
"assignment" : "="
}
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
} ;
/ * *
* @ author fenris
* /
args . verbosity = 0 ;
2024-09-10 01:11:51 +02:00
/ * *
2024-09-12 00:03:29 +02:00
* @ author fenris
* @ todo check validity
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
var class _handler = /** @class */ ( function ( ) {
/ * *
* @ author fenris
* /
function class _handler ( arguments _ ) {
this . arguments _ = arguments _ ;
}
/ * *
* @ author fenris
* /
class _handler . prototype . filter = function ( kind ) {
var arguments _ = { } ;
for ( var _i = 0 , _a = Object . entries ( this . arguments _ ) ; _i < _a . length ; _i ++ ) {
var _b = _a [ _i ] , name = _b [ 0 ] , argument = _b [ 1 ] ;
if ( argument . kind _get ( ) == kind ) {
arguments _ [ name ] = argument ;
}
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
return arguments _ ;
} ;
/ * *
* @ author fenris
* /
class _handler . prototype . read = function ( environment , input , data ) {
var _this = this ;
if ( data === void 0 ) { data = { } ; }
switch ( environment ) {
case args . enum _environment . cli :
case args . enum _environment . url : {
// default values
{
for ( var _i = 0 , _a = Object . entries ( this . arguments _ ) ; _i < _a . length ; _i ++ ) {
var _b = _a [ _i ] , name = _b [ 0 ] , argument = _b [ 1 ] ;
data [ name ] = argument . default _get ( ) ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
}
// preprocessing
{
// short indicators (lil hacky ...)
{
if ( environment == args . enum _environment . cli ) {
for ( var _c = 0 , _d = Object . entries ( this . filter ( args . enum _kind . volatile ) ) ; _c < _d . length ; _c ++ ) {
var _e = _d [ _c ] , name = _e [ 0 ] , argument = _e [ 1 ] ;
// console.info(argument.parameters_get()["indicators_short"].join("|"));
var pattern _from = "" ;
{
pattern _from += "(?:^|" . concat ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] , ")" ) ;
pattern _from += "-" . concat ( argument . parameters _get ( ) [ "indicators_short" ] . join ( "|" ) ) ;
pattern _from += "(?:$|" . concat ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] , ")" ) ;
}
var pattern _to = "" ;
{
pattern _to += settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ;
pattern _to += settings [ "environment" ] [ environment ] [ "symbols" ] [ "prefix" ] ;
pattern _to += argument . indicator _main ( ) ;
if ( argument . type _get ( ) == args . enum _type . boolean ) {
pattern _to += settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ;
}
else {
pattern _to += settings [ "environment" ] [ environment ] [ "symbols" ] [ "assignment" ] ;
}
}
var result = input . replace ( new RegExp ( pattern _from , "g" ) , pattern _to ) ;
lib _plankton . log . debug ( "lib_args:read:replacing" , {
"pattern_from" : pattern _from ,
"pattern_to" : pattern _to ,
"input" : input ,
"result" : result
} ) ;
input = result ;
}
}
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
lib _plankton . log . debug ( "lib_args:read:current_input" , {
"input" : input
} ) ;
}
// parsing
{
var parts = input
. split ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] )
. filter ( function ( x ) { return ( x != "" ) ; } ) ;
var index _expected _1 = 0 ;
parts . forEach ( function ( part ) {
lib _plankton . log . debug ( "lib_args:read:analyzing" , {
"part" : part
2024-09-10 01:11:51 +02:00
} ) ;
2024-09-12 00:03:29 +02:00
var found = [
function ( ) {
lib _plankton . log . debug ( "lib_args:read:probing_as_volatile" , {
"part" : part
} ) ;
for ( var _i = 0 , _a = Object . entries ( _this . filter ( args . enum _kind . volatile ) ) ; _i < _a . length ; _i ++ ) {
var _b = _a [ _i ] , name = _b [ 0 ] , argument = _b [ 1 ] ;
lib _plankton . log . debug ( "lib_args:read:probing_as_volatile:trying" , {
"part" : part ,
"argument" : argument . toString ( )
} ) ;
var pattern = "" ;
{
var pattern _front = "" ;
pattern _front += "" . concat ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "prefix" ] ) ;
pattern _front += "(?:" . concat ( argument . parameters _get ( ) [ "indicators_long" ] . join ( "|" ) , ")" ) ;
pattern += pattern _front ;
}
{
var pattern _back = "" ;
pattern _back += "" . concat ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "assignment" ] ) ;
pattern _back += "(" . concat ( argument . pattern _value ( ) , ")" ) ;
if ( argument . type _get ( ) == args . enum _type . boolean ) {
pattern _back = "(?:" . concat ( pattern _back , ")?" ) ;
}
pattern += pattern _back ;
}
lib _plankton . log . debug ( "lib_args:read:probing_as_volatile:pattern" , {
"pattern" : pattern
} ) ;
var regexp = new RegExp ( pattern ) ;
var matching = regexp . exec ( part ) ;
lib _plankton . log . debug ( "lib_args:read:probing_as_volatile:matching" , {
"matching" : matching
} ) ;
if ( matching == null ) {
// do nothing
}
else {
argument . assign ( data , name , matching [ 1 ] ) ;
return true ;
}
}
return false ;
} ,
function ( ) {
lib _plankton . log . debug ( "lib_args:read:probing_as_positional" , {
"part" : part
} ) ;
var positional = _this . filter ( args . enum _kind . positional ) ;
for ( var _i = 0 , _a = Object . entries ( positional ) ; _i < _a . length ; _i ++ ) {
var _b = _a [ _i ] , name = _b [ 0 ] , argument = _b [ 1 ] ;
if ( argument . parameters _get ( ) [ 'index' ] !== index _expected _1 ) {
// do nothing
}
else {
lib _plankton . log . debug ( "lib_args:read:probing_as_positional:trying" , {
"part" : part ,
"argument" : argument . toString ( )
} ) ;
var pattern = "" ;
{
var pattern _back = "" ;
pattern _back += "(" . concat ( argument . pattern _value ( ) , ")" ) ;
pattern += pattern _back ;
}
lib _plankton . log . debug ( "lib_args:read:probing_as_positional:pattern" , {
"pattern" : pattern
} ) ;
var regexp = new RegExp ( pattern ) ;
var matching = regexp . exec ( part ) ;
lib _plankton . log . debug ( "lib_args:read:probing_as_positional:matching" , {
"matching" : matching
} ) ;
if ( matching == null ) {
return false ;
}
else {
argument . assign ( data , name , matching [ 1 ] ) ;
index _expected _1 += 1 ;
return true ;
}
}
}
return false ;
} ,
] . some ( function ( x ) { return x ( ) ; } ) ;
if ( ! found ) {
lib _plankton . log . warning ( "lib_args:read:could_not_parse" , {
"part" : part
} ) ;
}
} ) ;
}
return data ;
break ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
default : {
throw ( new Error ( "unhandled environment " . concat ( environment ) ) ) ;
break ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
} ;
2024-09-10 01:11:51 +02:00
/ * *
* @ author fenris
2024-09-12 00:03:29 +02:00
* @ todo handle if the data object doesn ' t have the required field or the type is wrong or sth .
2024-09-10 01:11:51 +02:00
* /
2024-09-12 00:03:29 +02:00
class _handler . prototype . write = function ( environment , data ) {
switch ( environment ) {
case args . enum _environment . cli : {
return ( ( [ ]
. concat ( Object . entries ( this . filter ( args . enum _kind . volatile ) ) . map ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
var values ;
switch ( argument . mode _get ( ) ) {
case args . enum _mode . replace : {
values = [ data [ argument . name _get ( ) ] ] ;
break ;
}
case args . enum _mode . accumulate : {
values = data [ argument . name _get ( ) ] ;
break ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
return ( values
. map ( function ( value ) { return ( ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "prefix" ]
+
argument . parameters _get ( ) [ "indicators_long" ] [ 0 ] )
+
( settings [ "environment" ] [ environment ] [ "symbols" ] [ "assignment" ]
+
value . toString ( ) ) ) ; } )
. join ( " " ) ) ;
} ) )
. concat ( Object . entries ( this . filter ( args . enum _kind . positional ) ) . map ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
var raw = "" ;
{
var raw _back = "" ;
raw _back += argument . make ( data , name ) ;
raw += raw _back ;
}
return raw ;
} ) ) )
. join ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ) ) ;
break ;
}
default : {
throw ( new Error ( "unhandled environment " . concat ( environment ) ) ) ;
break ;
2024-09-10 01:11:51 +02:00
}
}
2024-09-12 00:03:29 +02:00
} ;
/ * *
* @ desc manpage - like info - sheet
* @ author fenris
* /
class _handler . prototype . generate _help = function ( _a ) {
var _b = _a [ "programname" ] , programname = _b === void 0 ? null : _b , _c = _a [ "author" ] , author = _c === void 0 ? null : _c , _d = _a [ "description" ] , description = _d === void 0 ? null : _d , _e = _a [ "executable" ] , executable = _e === void 0 ? null : _e ;
var environment = args . enum _environment . cli ;
var output = "" ;
{
var section = "" ;
{
var line = "" ;
line += "" ;
line += "INFO" ;
line += "\n" ;
section += line ;
}
{
var line = "" ;
line += "\t" ;
line += "" . concat ( programname , " -- " ) . concat ( description ) ;
line += "\n" ;
section += line ;
}
section += "\n" ;
output += section ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
{
if ( author != null ) {
var section = "" ;
{
var line = "" ;
line += "" ;
line += "AUTHOR" ;
line += "\n" ;
section += line ;
}
{
var line = "" ;
line += "\t" ;
line += "" . concat ( author ) ;
line += "\n" ;
section += line ;
}
section += "\n" ;
output += section ;
}
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
{
var section = "" ;
{
var line = "" ;
line += "" ;
line += "SYNOPSIS" ;
line += "\n" ;
section += line ;
}
{
var line = "" ;
line += "\t" ;
line += executable ;
line += settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ;
line += Object . entries ( this . filter ( args . enum _kind . positional ) )
. map ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
var part = "" ;
part += "<" . concat ( argument . name _get ( ) , ">" ) ;
return part ;
} )
. join ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ) ;
line += settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ;
line += Object . entries ( this . filter ( args . enum _kind . volatile ) )
. filter ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
return ( ! argument . hidden _get ( ) ) ;
} )
. map ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
var part = "" ;
// part += settings["environment"][environment]["symbols"]["prefix"];
part += "-" ;
part += argument . parameters _get ( ) [ "indicators_short" ] [ 0 ] ;
if ( argument . type _get ( ) != "boolean" ) {
/ *
part += settings [ "environment" ] [ environment ] [ "symbols" ] [ "assignment" ] ;
part += ` < ${ argument . name _get ( ) } > ` ;
* /
part += " " ;
part += "<" . concat ( argument . name _get ( ) , ">" ) ;
}
part = "[" . concat ( part , "]" ) ;
return part ;
} )
. join ( settings [ "environment" ] [ environment ] [ "symbols" ] [ "delimiter" ] ) ;
line += "\n" ;
section += line ;
}
section += "\n" ;
output += section ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
{
var section = "" ;
{
var line = "" ;
line += "" ;
line += "OPTIONS" ;
line += "\n" ;
section += line ;
}
{
section += ( Object . entries ( this . arguments _ )
. filter ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
return ( ! argument . hidden _get ( ) ) ;
} )
. map ( function ( _a ) {
var name = _a [ 0 ] , argument = _a [ 1 ] ;
return argument . generate _help ( ) ;
} )
. join ( "\n" ) ) ;
}
section += "\n" ;
output += section ;
2024-09-10 01:11:51 +02:00
}
2024-09-12 00:03:29 +02:00
return output ;
2024-09-10 01:11:51 +02:00
} ;
2024-09-12 00:03:29 +02:00
return class _handler ;
} ( ) ) ;
args . class _handler = class _handler ;
} ) ( args = lib _plankton . args || ( lib _plankton . args = { } ) ) ;
2024-09-10 01:11:51 +02:00
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-18 18:17:25 +02:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _generator = ( this && this . _ _generator ) || function ( thisArg , body ) {
var _ = { label : 0 , sent : function ( ) { if ( t [ 0 ] & 1 ) throw t [ 1 ] ; return t [ 1 ] ; } , trys : [ ] , ops : [ ] } , f , y , t , g ;
return g = { next : verb ( 0 ) , "throw" : verb ( 1 ) , "return" : verb ( 2 ) } , typeof Symbol === "function" && ( g [ Symbol . iterator ] = function ( ) { return this ; } ) , g ;
function verb ( n ) { return function ( v ) { return step ( [ n , v ] ) ; } ; }
function step ( op ) {
if ( f ) throw new TypeError ( "Generator is already executing." ) ;
2024-09-21 10:55:06 +02:00
while ( _ ) try {
2024-09-18 18:17:25 +02:00
if ( f = 1 , y && ( t = op [ 0 ] & 2 ? y [ "return" ] : op [ 0 ] ? y [ "throw" ] || ( ( t = y [ "return" ] ) && t . call ( y ) , 0 ) : y . next ) && ! ( t = t . call ( y , op [ 1 ] ) ) . done ) return t ;
if ( y = 0 , t ) op = [ op [ 0 ] & 2 , t . value ] ;
switch ( op [ 0 ] ) {
case 0 : case 1 : t = op ; break ;
case 4 : _ . label ++ ; return { value : op [ 1 ] , done : false } ;
case 5 : _ . label ++ ; y = op [ 1 ] ; op = [ 0 ] ; continue ;
case 7 : op = _ . ops . pop ( ) ; _ . trys . pop ( ) ; continue ;
default :
if ( ! ( t = _ . trys , t = t . length > 0 && t [ t . length - 1 ] ) && ( op [ 0 ] === 6 || op [ 0 ] === 2 ) ) { _ = 0 ; continue ; }
if ( op [ 0 ] === 3 && ( ! t || ( op [ 1 ] > t [ 0 ] && op [ 1 ] < t [ 3 ] ) ) ) { _ . label = op [ 1 ] ; break ; }
if ( op [ 0 ] === 6 && _ . label < t [ 1 ] ) { _ . label = t [ 1 ] ; t = op ; break ; }
if ( t && _ . label < t [ 2 ] ) { _ . label = t [ 2 ] ; _ . ops . push ( op ) ; break ; }
if ( t [ 2 ] ) _ . ops . pop ( ) ;
_ . trys . pop ( ) ; continue ;
}
op = body . call ( thisArg , _ ) ;
} catch ( e ) { op = [ 6 , e ] ; y = 0 ; } finally { f = t = 0 ; }
if ( op [ 0 ] & 5 ) throw op [ 1 ] ; return { value : op [ 0 ] ? op [ 1 ] : void 0 , done : true } ;
}
} ;
/ *
This file is part of » bacterio - plankton : bcrypt « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : bcrypt « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : bcrypt « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : bcrypt « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var bcrypt ;
( function ( bcrypt ) {
/ * *
* /
function compute ( input , options ) {
var _a ;
if ( options === void 0 ) { options = { } ; }
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var nm _bcrypt , salt , _b , result ;
return _ _generator ( this , function ( _c ) {
switch ( _c . label ) {
case 0 :
options = Object . assign ( {
"rounds" : 12 ,
"salt" : null
} , options ) ;
nm _bcrypt = require ( "bcrypt" ) ;
if ( ! ( ( _a = options . salt ) !== null && _a !== void 0 ) ) return [ 3 /*break*/ , 1 ] ;
_b = _a ;
return [ 3 /*break*/ , 3 ] ;
case 1 : return [ 4 /*yield*/ , nm _bcrypt . genSalt ( options . rounds ) ] ;
case 2 :
_b = ( _c . sent ( ) ) ;
_c . label = 3 ;
case 3 :
salt = ( _b ) ;
return [ 4 /*yield*/ , nm _bcrypt . hash ( input , salt ) ] ;
case 4 :
result = _c . sent ( ) ;
return [ 2 /*return*/ , result ] ;
}
} ) ;
} ) ;
}
bcrypt . compute = compute ;
/ * *
* /
function compare ( password _shall _image , password _is ) {
return _ _awaiter ( this , void 0 , void 0 , function ( ) {
var nm _bcrypt , result ;
return _ _generator ( this , function ( _a ) {
switch ( _a . label ) {
case 0 :
nm _bcrypt = require ( "bcrypt" ) ;
return [ 4 /*yield*/ , nm _bcrypt . compare ( password _is , password _shall _image ) ] ;
case 1 :
result = _a . sent ( ) ;
return [ 2 /*return*/ , result ] ;
}
} ) ;
} ) ;
}
bcrypt . compare = compare ;
} ) ( bcrypt = lib _plankton . bcrypt || ( lib _plankton . bcrypt = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
2024-09-19 13:34:07 +02:00
/ *
2024-09-21 10:55:06 +02:00
This file is part of » bacterio - plankton : map « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : map « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : map « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : map « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : map « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : map « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : map « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : map « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var map ;
( function ( map _1 ) {
/ * *
* /
function clear ( map ) {
map . iterate ( function ( value , key ) {
map [ "delete" ] ( key ) ;
} ) ;
}
map _1 . clear = clear ;
/ * *
* /
function keys ( map ) {
var keys = [ ] ;
map . iterate ( function ( value , key ) {
keys . push ( key ) ;
} ) ;
return keys ;
}
map _1 . keys = keys ;
/ * *
* /
function values ( map ) {
var values = [ ] ;
map . iterate ( function ( value , key ) {
values . push ( value ) ;
} ) ;
return values ;
}
map _1 . values = values ;
/ * *
* /
function dump ( map ) {
var pairs = [ ] ;
map . iterate ( function ( value , key ) {
pairs . push ( { "key" : key , "value" : value } ) ;
} ) ;
return pairs ;
}
map _1 . dump = dump ;
/ * *
* /
function show ( map , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"show_key" : instance _show ,
"show_value" : instance _show
} , options ) ;
return ( "["
+
( dump ( map )
. map ( function ( _a ) {
var key = _a [ "key" ] , value = _a [ "value" ] ;
return ( options . show _key ( key )
+
" -> "
+
options . show _value ( value ) ) ;
} )
. join ( ", " ) )
+
"]" ) ;
}
map _1 . show = show ;
} ) ( map = lib _plankton . map || ( lib _plankton . map = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : map « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : map « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : map « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : map « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var map ;
( function ( map ) {
var simplemap ;
( function ( simplemap ) {
/ * *
* /
function make ( options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"data" : { }
} , options ) ;
return {
"data" : options . data
} ;
}
simplemap . make = make ;
/ * *
* /
function size ( subject ) {
return Object . keys ( subject . data ) . length ;
}
simplemap . size = size ;
/ * *
* /
function has ( subject , key ) {
return ( key in subject . data ) ;
}
simplemap . has = has ;
/ * *
* /
function get _safe ( subject , key ) {
if ( key in subject . data ) {
return lib _plankton . pod . make _filled ( subject . data [ key ] ) ;
}
else {
return lib _plankton . pod . make _empty ( ) ;
}
}
simplemap . get _safe = get _safe ;
/ * *
* /
function get ( subject , key , fallback ) {
if ( fallback === void 0 ) { fallback = lib _plankton . pod . make _empty ( ) ; }
if ( key in subject . data ) {
return subject . data [ key ] ;
}
else {
if ( ! lib _plankton . pod . is _filled ( fallback ) ) {
throw ( new Error ( "key not found" ) ) ;
}
else {
return lib _plankton . pod . cull ( fallback ) ;
}
}
}
simplemap . get = get ;
/ * *
* /
function set ( subject , key , value ) {
subject . data [ key ] = value ;
}
simplemap . set = set ;
/ * *
* /
function delete _ ( subject , key ) {
delete subject . data [ key ] ;
}
simplemap . delete _ = delete _ ;
/ * *
* /
function iterate ( subject , procedure ) {
Object . entries ( subject . data ) . forEach ( function ( _a ) {
var key = _a [ 0 ] , value = _a [ 1 ] ;
procedure ( value , key ) ;
} ) ;
}
simplemap . iterate = iterate ;
/ * *
* /
function implementation _map ( subject ) {
return {
"size" : function ( ) { return size ( subject ) ; } ,
"has" : function ( key ) { return has ( subject , key ) ; } ,
"get" : function ( key , fallback ) { return get ( subject , key , fallback ) ; } ,
"set" : function ( key , value ) { return set ( subject , key , value ) ; } ,
"delete" : function ( key ) { return delete _ ( subject , key ) ; } ,
"iterate" : function ( function _ ) { return iterate ( subject , function _ ) ; }
} ;
}
simplemap . implementation _map = implementation _map ;
} ) ( simplemap = map . simplemap || ( map . simplemap = { } ) ) ;
} ) ( map = lib _plankton . map || ( lib _plankton . map = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : map « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : map « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : map « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : map « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var map ;
( function ( map ) {
var hashmap ;
( function ( hashmap ) {
/ * *
* /
function make ( hashing , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"pairs" : [ ]
} , options ) ;
var subject = {
"hashing" : hashing ,
"core" : lib _plankton . map . simplemap . make ( { } )
} ;
options . pairs . forEach ( function ( pair ) {
set ( subject , pair . key , pair . value ) ;
} ) ;
return subject ;
}
hashmap . make = make ;
/ * *
* /
function size ( subject ) {
return lib _plankton . map . simplemap . size ( subject . core ) ;
}
hashmap . size = size ;
/ * *
* /
function has ( subject , key ) {
return lib _plankton . map . simplemap . has ( subject . core , subject . hashing ( key ) ) ;
}
hashmap . has = has ;
/ * *
* /
function get ( subject , key , fallback ) {
if ( fallback === void 0 ) { fallback = lib _plankton . pod . make _empty ( ) ; }
/ *
we have to adjust the fallback argument , so that it can be used in our underlying simplemap core
therefore we pack the value together with any key as dummy
* /
return ( lib _plankton . map . simplemap . get ( subject . core , subject . hashing ( key ) , lib _plankton . pod . propagate ( fallback , function ( value ) { return ( {
"key" : key ,
"value" : value
} ) ; } ) )
. value ) ;
}
hashmap . get = get ;
/ * *
* /
function set ( subject , key , value ) {
var key _ = subject . hashing ( key ) ;
return ( lib _plankton . map . simplemap . set ( subject . core , key _ , { "key" : key , "value" : value } ) ) ;
}
hashmap . set = set ;
/ * *
* /
function delete _ ( subject , key ) {
return ( lib _plankton . map . simplemap . delete _ ( subject . core , subject . hashing ( key ) ) ) ;
}
hashmap . delete _ = delete _ ;
/ * *
* /
function iterate ( subject , procedure ) {
return ( lib _plankton . map . simplemap . iterate ( subject . core , function ( _a , key _ ) {
var key = _a [ "key" ] , value = _a [ "value" ] ;
procedure ( value , key ) ;
} ) ) ;
}
hashmap . iterate = iterate ;
/ * *
* /
function implementation _map ( subject ) {
return {
"size" : function ( ) { return size ( subject ) ; } ,
"has" : function ( key ) { return has ( subject , key ) ; } ,
"get" : function ( key , fallback ) { return get ( subject , key , fallback ) ; } ,
"set" : function ( key , value ) { return set ( subject , key , value ) ; } ,
"delete" : function ( key ) { return delete _ ( subject , key ) ; } ,
"iterate" : function ( procedure ) { return iterate ( subject , procedure ) ; }
} ;
}
hashmap . implementation _map = implementation _map ;
} ) ( hashmap = map . hashmap || ( map . hashmap = { } ) ) ;
} ) ( map = lib _plankton . map || ( lib _plankton . map = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : map « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : map « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : map « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
3 GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : map « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var map ;
( function ( map ) {
var collatemap ;
( function ( collatemap ) {
/ * *
* /
function make ( collation , options ) {
if ( options === void 0 ) { options = { } ; }
options = Object . assign ( {
"pairs" : [ ]
} , options ) ;
var subject = {
"collation" : collation ,
"pairs" : [ ]
} ;
options . pairs . forEach ( function ( pair ) {
set ( subject , pair . key , pair . value ) ;
} ) ;
return subject ;
}
collatemap . make = make ;
/ * *
* /
function size ( subject ) {
return subject . pairs . length ;
}
collatemap . size = size ;
/ * *
* /
function has ( subject , key ) {
return ( subject . pairs . some ( function ( pair ) { return subject . collation ( key , pair . key ) ; } ) ) ;
}
collatemap . has = has ;
/ * *
* @ todo use . find
* /
function get ( subject , key , fallback ) {
var value ;
var found = ( subject . pairs
. some ( function ( pair ) {
if ( subject . collation ( key , pair . key ) ) {
value = pair . value ;
return true ;
}
else {
return false ;
}
} ) ) ;
if ( found ) {
return value ;
}
else {
if ( ! lib _plankton . pod . is _filled ( fallback ) ) {
throw ( new Error ( "key not found" ) ) ;
}
else {
return lib _plankton . pod . cull ( fallback ) ;
}
}
}
collatemap . get = get ;
/ * *
* /
function set ( subject , key , value ) {
var found = ( subject . pairs
. some ( function ( pair ) {
if ( subject . collation ( key , pair . key ) ) {
// pair.value = value;
return true ;
}
else {
return false ;
}
} ) ) ;
if ( found ) {
// nothing
}
else {
subject . pairs . push ( {
"key" : key ,
"value" : value
} ) ;
}
}
collatemap . set = set ;
/ * *
* /
function delete _ ( subject , key ) {
var index ;
var found = ( subject . pairs
. some ( function ( pair , index _ ) {
if ( subject . collation ( key , pair . key ) ) {
index = index _ ;
return true ;
}
else {
return false ;
}
} ) ) ;
if ( found ) {
subject . pairs . splice ( index , 1 ) ;
}
else {
// do nothing
}
}
collatemap . delete _ = delete _ ;
/ * *
* /
function iterate ( subject , function _ ) {
subject . pairs
. forEach ( function ( pair ) {
function _ ( pair . value , pair . key ) ;
} ) ;
}
collatemap . iterate = iterate ;
/ * *
* /
function implementation _map ( subject ) {
return {
"size" : function ( ) { return size ( subject ) ; } ,
"has" : function ( key ) { return has ( subject , key ) ; } ,
"get" : function ( key , fallback ) { return get ( subject , key , fallback ) ; } ,
"set" : function ( key , value ) { return set ( subject , key , value ) ; } ,
"delete" : function ( key ) { return delete _ ( subject , key ) ; } ,
"iterate" : function ( function _ ) { return iterate ( subject , function _ ) ; }
} ;
}
collatemap . implementation _map = implementation _map ;
} ) ( collatemap = map . collatemap || ( map . collatemap = { } ) ) ;
} ) ( map = lib _plankton . map || ( lib _plankton . map = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
2024-09-19 13:34:07 +02:00
This file is part of » bacterio - plankton : base64 « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base64 « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base64 « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base64 « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var base64 ;
( function ( base64 ) {
/ * *
* @ author fenris
* /
function encode ( source ) {
return Buffer . from ( source , "utf-8" ) . toString ( "base64" ) ;
}
base64 . encode = encode ;
/ * *
* @ author fenris
* /
function decode ( target ) {
return Buffer . from ( target , "base64" ) . toString ( "utf-8" ) ;
}
base64 . decode = decode ;
/ * *
* @ author fenris
* /
function implementation _code ( ) {
return {
"encode" : encode ,
"decode" : decode ,
} ;
}
base64 . implementation _code = implementation _code ;
} ) ( base64 = lib _plankton . base64 || ( lib _plankton . base64 = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : base64 « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : base64 « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : base64 « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : base64 « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var base64 ;
( function ( base64 ) {
/ * *
* @ author fenris
* /
class class _base64 {
/ * *
* @ author fenris
* /
constructor ( ) {
}
/ * *
* @ implementation
* @ author fenris
* /
encode ( x ) {
return base64 . encode ( x ) ;
}
/ * *
* @ implementation
* @ author fenris
* /
decode ( x ) {
return base64 . decode ( x ) ;
}
}
base64 . class _base64 = class _base64 ;
} ) ( base64 = lib _plankton . base64 || ( lib _plankton . base64 = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : auth « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : auth « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : auth « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : auth « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : auth « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : auth « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : auth « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : auth « . If not , see < http : //www.gnu.org/licenses/>.
* /
/ *
This file is part of » bacterio - plankton : auth « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : auth « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : auth « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : auth « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var auth ;
( function ( auth ) {
var internal ;
( function ( internal ) {
/ * *
* /
function make ( parameters ) {
return {
"parameters" : parameters ,
} ;
}
/ * *
* /
function login _prepare ( subject ) {
return Promise . resolve ( null ) ;
}
/ * *
* /
async function login _execute ( subject , input ) {
let password _image ;
try {
password _image = await subject . parameters . password _image _chest . read ( input . name ) ;
}
catch ( error ) {
password _image = null ;
}
if ( password _image === null ) {
return Promise . reject ( new Error ( "user not found" ) ) ;
}
else {
let valid ;
try {
valid = await subject . parameters . check _password ( password _image , input . password ) ;
}
catch ( error ) {
valid = null ;
}
if ( valid === null ) {
return Promise . reject ( new Error ( "password check failed" ) ) ;
}
else {
if ( ! valid ) {
return Promise . reject ( new Error ( "wrong password" ) ) ;
}
else {
return Promise . resolve ( input . name ) ;
}
}
}
}
/ * *
* /
function login _control ( subject , input ) {
return Promise . reject ( new Error ( "not available" ) ) ;
}
/ * *
* /
function implementation _auth ( parameters ) {
const subject = make ( parameters ) ;
return {
"login_prepare" : ( ) => login _prepare ( subject ) ,
"login_execute" : ( input ) => login _execute ( subject , input ) ,
"login_control" : ( input ) => login _control ( subject , input ) ,
} ;
}
internal . implementation _auth = implementation _auth ;
} ) ( internal = auth . internal || ( auth . internal = { } ) ) ;
} ) ( auth = lib _plankton . auth || ( lib _plankton . auth = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;
/ *
This file is part of » bacterio - plankton : auth « .
Copyright 2016 - 2024 'Christian Fraß, Christian Neubauer, Martin Springwald GbR'
< info @ greenscale . de >
» bacterio - plankton : auth « is free software : you can redistribute it and / or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
» bacterio - plankton : auth « is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with » bacterio - plankton : auth « . If not , see < http : //www.gnu.org/licenses/>.
* /
var lib _plankton ;
( function ( lib _plankton ) {
var auth ;
( function ( auth ) {
var oidc ;
( function ( oidc ) {
/ * *
* /
let enum _state _label ;
( function ( enum _state _label ) {
enum _state _label [ "idle" ] = "idle" ;
enum _state _label [ "wait" ] = "wait" ;
enum _state _label [ "ready" ] = "ready" ;
} ) ( enum _state _label || ( enum _state _label = { } ) ) ;
;
/ * *
* /
function make ( parameters _raw ) {
return {
"parameters" : {
"url_authorization" : parameters _raw . url _authorization ,
"url_token" : parameters _raw . url _token ,
"url_userinfo" : parameters _raw . url _userinfo ,
"client_id" : parameters _raw . client _id ,
"client_secret" : parameters _raw . client _secret ,
"url_redirect" : parameters _raw . url _redirect ,
"scopes" : ( parameters _raw . scopes
? ?
[ "openid" , "profile" ] ) ,
"label" : ( parameters _raw . label
? ?
"OIDC" ) ,
"login_url_mode" : ( parameters _raw . login _url _mode
? ?
"log" ) ,
} ,
"state" : {
"id" : lib _plankton . random . generate _guid ( ) ,
"label" : enum _state _label . idle ,
"handlers" : [ ] ,
"token" : null ,
"name" : null ,
}
} ;
}
/ * *
* @ see https : //openid.net/specs/openid-connect-core-1_0.html#AuthRequest
* @ todo save state for different requests / users ?
* /
function login _prepare ( subject ) {
const url = lib _plankton . url . encode ( Object . assign ( lib _plankton . url . decode ( subject . parameters . url _authorization ) , {
"query" : lib _plankton . www _form . encode ( {
"response_type" : "code" ,
"client_id" : subject . parameters . client _id ,
// "client_secret": subject.parameters.client_secret,
"scope" : subject . parameters . scopes . join ( " " ) ,
"state" : subject . state . id ,
"redirect_uri" : subject . parameters . url _redirect ,
} )
} ) ) ;
/ *
switch ( subject . parameters . login _url _mode ) {
case "log" : {
lib _plankton . log . info (
"auth_login_url" ,
{
"url" : url ,
} ,
) ;
break ;
}
case "open" : {
const nm _opn = require ( "opn" ) ;
nm _opn ( url ) ;
break ;
}
default : {
throw ( new Error ( "invalid login_url_mode: " + subject . parameters . login _url _mode ) ) ;
break ;
}
}
* /
return Promise . resolve ( {
"url" : url ,
"label" : subject . parameters . label ,
} ) ;
}
/ * *
* @ see https : //openid.net/specs/openid-connect-core-1_0.html#AuthRequest
* /
function login _execute ( subject , input ) {
switch ( subject . state . label ) {
case enum _state _label . idle : {
return ( new Promise ( ( resolve , reject ) => {
subject . state = {
"id" : subject . state . id ,
"label" : enum _state _label . wait ,
"handlers" : subject . state . handlers . concat ( [ { "resolve" : resolve , "reject" : reject } ] ) ,
"token" : null ,
"name" : null ,
} ;
} ) ) ;
break ;
}
case enum _state _label . wait : {
return ( new Promise ( ( resolve , reject ) => {
subject . state = {
"id" : subject . state . id ,
"label" : enum _state _label . wait ,
"handlers" : subject . state . handlers . concat ( [ { "resolve" : resolve , "reject" : reject } ] ) ,
"token" : null ,
"name" : null ,
} ;
} ) ) ;
break ;
}
case enum _state _label . ready : {
return Promise . resolve ( subject . state . name ) ;
break ;
}
}
}
/ * *
* @ see https : //openid.net/specs/openid-connect-core-1_0.html#AuthRequest
* @ see https : //openid.net/specs/openid-connect-core-1_0.html#TokenRequest
* @ see https : //openid.net/specs/openid-connect-core-1_0.html#UserInfoRequest
* @ todo authentication
* /
async function login _control ( subject , input ) {
switch ( input . kind ) {
default : {
throw ( new Error ( "unhandled login control kind: " + input . kind ) ) ;
break ;
}
case "authorization_callback" : {
lib _plankton . log . info ( "auth_authorization_callback_input" , {
"input" : input ,
} ) ;
if ( "error" in input . data . stuff ) {
// query_parts["error"]
// query_parts["error_description"]
const handlers = subject . state . handlers ;
subject . state = {
"id" : subject . state . id ,
"label" : enum _state _label . idle ,
"handlers" : [ ] ,
"token" : null ,
"name" : null ,
} ;
handlers . forEach ( ( handler ) => {
handler . reject ( new Error ( "failed" ) ) ;
} ) ;
return Promise . resolve ( undefined ) ;
}
else {
const code = input . data . stuff [ "code" ] ;
const cookie = input . data . cookie ;
const token _url = lib _plankton . url . decode ( subject . parameters . url _token ) ;
const token _http _request = {
"version" : "HTTP/2" ,
"scheme" : ( ( token _url . scheme === "http" ) ? "http" : "https" ) ,
"host" : token _url . host ,
"path" : token _url . path ,
"query" : null ,
"method" : lib _plankton . http . enum _method . post ,
"headers" : {
"Content-Type" : "application/x-www-form-urlencoded" ,
"Cookie" : cookie ,
"Authorization" : lib _plankton . string . coin ( "Basic {{value}}" , {
"value" : lib _plankton . base64 . encode ( lib _plankton . string . coin ( "{{id}}:{{secret}}" , {
"id" : subject . parameters . client _id ,
"secret" : subject . parameters . client _secret ,
} ) ) ,
} ) ,
} ,
"body" : Buffer . from ( lib _plankton . www _form . encode ( {
"grant_type" : "authorization_code" ,
"code" : code ,
"redirect_uri" : subject . parameters . url _redirect ,
} ) ) ,
} ;
lib _plankton . log . info ( "auth_token_request" , {
"request" : Object . assign ( token _http _request , {
"body" : token _http _request . body . toString ( ) ,
} )
} ) ;
const token _http _response = await lib _plankton . http . call ( token _http _request ) ;
lib _plankton . log . info ( "auth_token_response" , {
"response" : token _http _response ,
} ) ;
const token _data = lib _plankton . json . decode ( token _http _response . body . toString ( ) ) ;
const token = token _data [ "access_token" ] ;
const userinfo _url = lib _plankton . url . decode ( subject . parameters . url _userinfo ) ;
const userinfo _http _request = {
"version" : "HTTP/2" ,
"scheme" : ( ( userinfo _url . scheme === "http" ) ? "http" : "https" ) ,
"host" : userinfo _url . host ,
"path" : userinfo _url . path ,
"query" : null ,
"method" : lib _plankton . http . enum _method . get ,
"headers" : {
"Cookie" : cookie ,
"Authorization" : ( "Bearer " + token ) ,
} ,
"body" : null
} ;
lib _plankton . log . info ( "auth_userinfo_request" , {
"request" : userinfo _http _request ,
} ) ;
const userinfo _http _response = await lib _plankton . http . call ( userinfo _http _request ) ;
lib _plankton . log . info ( "auth_userinfo_response" , {
"response" : userinfo _http _response ,
} ) ;
const userinfo _data = lib _plankton . json . decode ( userinfo _http _response . body . toString ( ) ) ;
const name = userinfo _data [ "name" ] ; // TODO: requires "profile" in "scope"
lib _plankton . log . info ( "auth_oidc_name" , { "value" : name } ) ;
const handlers = subject . state . handlers ;
subject . state = {
"id" : subject . state . id ,
"label" : enum _state _label . ready ,
"handlers" : [ ] ,
"token" : token ,
"name" : name ,
} ;
handlers . forEach ( ( handler ) => {
handler . resolve ( subject . state . name ) ;
} ) ;
return Promise . resolve ( undefined ) ;
}
break ;
}
}
}
/ * *
* /
function implementation _auth ( parameters _raw ) {
const subject = make ( parameters _raw ) ;
return {
"login_prepare" : ( ) => login _prepare ( subject ) ,
"login_execute" : ( input ) => login _execute ( subject , input ) ,
"login_control" : ( input ) => login _control ( subject , input ) ,
} ;
}
oidc . implementation _auth = implementation _auth ;
} ) ( oidc = auth . oidc || ( auth . oidc = { } ) ) ;
} ) ( auth = lib _plankton . auth || ( lib _plankton . auth = { } ) ) ;
} ) ( lib _plankton || ( lib _plankton = { } ) ) ;