FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CommunitySolidServer/src/storage/patch/ConvertingPatcher.ts at main · ls-css/CommunitySolidServer · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ls-css
/
CommunitySolidServer
Public
forked from
CommunitySolidServer/CommunitySolidServer
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
CommunitySolidServer
/
src
/
storage
/
patch
/
ConvertingPatcher.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
83 lines (74 loc) · 3.82 KB
Breadcrumbs
CommunitySolidServer
/
src
/
storage
/
patch
/
ConvertingPatcher.ts
Copy path
File metadata and controls
83 lines (74 loc) · 3.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import
{
BasicRepresentation
}
from
'../../http/representation/BasicRepresentation'
;
import
type
{
Representation
}
from
'../../http/representation/Representation'
;
import
{
getLoggerFor
}
from
'../../logging/LogUtil'
;
import
type
{
RepresentationConverter
}
from
'../conversion/RepresentationConverter'
;
import
type
{
RepresentationPatcherInput
}
from
'./RepresentationPatcher'
;
import
{
RepresentationPatcher
}
from
'./RepresentationPatcher'
;
/**
* A `ConvertingPatcher` converts a document to its `intermediateType`,
* sends the result to the wrapped patcher, and then converts back to its original type.
* No changes will take place if no `intermediateType` is provided.
*
* In case there is no resource yet and a new one needs to be created,
* the result of the wrapped patcher will be converted to the provided `defaultType`.
* In case no `defaultType` is provided, the patcher output will be returned directly.
*/
export
class
ConvertingPatcher
extends
RepresentationPatcher
<
Representation
>
{
protected
readonly
logger
=
getLoggerFor
(
this
)
;
private
readonly
patcher
:
RepresentationPatcher
<
Representation
>
;
private
readonly
converter
:
RepresentationConverter
;
private
readonly
intermediateType
?:
string
;
private
readonly
defaultType
?:
string
;
/**
*
@param
patcher - Patcher that will be called with the Representation.
*
@param
converter - Converter that will be used to generate intermediate Representation.
*
@param
intermediateType - Content-type of the intermediate Representation if conversion is needed.
*
@param
defaultType - Content-type in case a new resource gets created and needs to be converted.
*/
public
constructor
(
patcher
:
RepresentationPatcher
<
Representation
>
,
converter
:
RepresentationConverter
,
intermediateType
?:
string
,
defaultType
?:
string
,
)
{
super
(
)
;
this
.
patcher
=
patcher
;
this
.
converter
=
converter
;
this
.
intermediateType
=
intermediateType
;
this
.
defaultType
=
defaultType
;
}
public
async
canHandle
(
input
:
RepresentationPatcherInput
<
Representation
>
)
:
Promise
<
void
>
{
// Verify the converter can handle the input representation if needed
const
{
identifier
,
representation
}
=
input
;
let
convertedPlaceholder
=
representation
;
if
(
representation
&&
this
.
intermediateType
)
{
const
preferences
=
{
type
:
{
[
this
.
intermediateType
]
:
1
}
}
;
await
this
.
converter
.
canHandle
(
{
representation
,
identifier
,
preferences
}
)
;
convertedPlaceholder
=
new
BasicRepresentation
(
[
]
,
representation
.
metadata
,
this
.
intermediateType
)
;
}
// Verify the patcher can handle the (converted) representation
await
this
.
patcher
.
canHandle
(
{
...
input
,
representation
:
convertedPlaceholder
}
)
;
}
public
async
handle
(
input
:
RepresentationPatcherInput
<
Representation
>
)
:
Promise
<
Representation
>
{
const
{
identifier
,
representation
}
=
input
;
let
outputType
:
string
|
undefined
;
let
converted
=
representation
;
if
(
!
representation
)
{
// If there is no representation the output will need to be converted to the default type
outputType
=
this
.
defaultType
;
}
else
if
(
this
.
intermediateType
)
{
// Convert incoming representation to the requested type
outputType
=
representation
.
metadata
.
contentType
;
const
preferences
=
{
type
:
{
[
this
.
intermediateType
]
:
1
}
}
;
converted
=
await
this
.
converter
.
handle
(
{
representation
,
identifier
,
preferences
}
)
;
}
// Call the wrapped patcher with the (potentially) converted representation
let
result
=
await
this
.
patcher
.
handle
(
{
...
input
,
representation
:
converted
}
)
;
// Convert the output back to its original type or the default type depending on what was set
if
(
outputType
)
{
const
preferences
=
{
type
:
{
[
outputType
]
:
1
}
}
;
result
=
await
this
.
converter
.
handle
(
{
representation
:
result
,
identifier
,
preferences
}
)
;
}
return
result
;
}
}
Back
|
FazBrowse Home
|
New Git URL