Page Menu
Home
Software Heritage
Search
Configure Global Search
Log In
Files
F9340890
envelope.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Subscribers
None
envelope.js
View Options
import
{
normalize
}
from
'./normalize.js'
;
import
{
dropUndefinedKeys
}
from
'./object.js'
;
/**
* Creates an envelope.
* Make sure to always explicitly provide the generic to this function
* so that the envelope types resolve correctly.
*/
function
createEnvelope
(
headers
,
items
=
[])
{
return
[
headers
,
items
]
;
}
/**
* Add an item to an envelope.
* Make sure to always explicitly provide the generic to this function
* so that the envelope types resolve correctly.
*/
function
addItemToEnvelope
(
envelope
,
newItem
)
{
const
[
headers
,
items
]
=
envelope
;
return
[
headers
,
[...
items
,
newItem
]]
;
}
/**
* Convenience function to loop through the items and item types of an envelope.
* (This function was mostly created because working with envelope types is painful at the moment)
*/
function
forEachEnvelopeItem
(
envelope
,
callback
,
)
{
const
envelopeItems
=
envelope
[
1
];
envelopeItems
.
forEach
((
envelopeItem
)
=>
{
const
envelopeItemType
=
envelopeItem
[
0
].
type
;
callback
(
envelopeItem
,
envelopeItemType
);
});
}
function
encodeUTF8
(
input
,
textEncoder
)
{
const
utf8
=
textEncoder
||
new
TextEncoder
();
return
utf8
.
encode
(
input
);
}
/**
* Serializes an envelope.
*/
function
serializeEnvelope
(
envelope
,
textEncoder
)
{
const
[
envHeaders
,
items
]
=
envelope
;
// Initially we construct our envelope as a string and only convert to binary chunks if we encounter binary data
let
parts
=
JSON
.
stringify
(
envHeaders
);
function
append
(
next
)
{
if
(
typeof
parts
===
'string'
)
{
parts
=
typeof
next
===
'string'
?
parts
+
next
:
[
encodeUTF8
(
parts
,
textEncoder
),
next
];
}
else
{
parts
.
push
(
typeof
next
===
'string'
?
encodeUTF8
(
next
,
textEncoder
)
:
next
);
}
}
for
(
const
item
of
items
)
{
const
[
itemHeaders
,
payload
]
=
item
;
append
(
`\n
${
JSON
.
stringify
(
itemHeaders
)
}
\n`
);
if
(
typeof
payload
===
'string'
||
payload
instanceof
Uint8Array
)
{
append
(
payload
);
}
else
{
let
stringifiedPayload
;
try
{
stringifiedPayload
=
JSON
.
stringify
(
payload
);
}
catch
(
e
)
{
// In case, despite all our efforts to keep `payload` circular-dependency-free, `JSON.strinify()` still
// fails, we try again after normalizing it again with infinite normalization depth. This of course has a
// performance impact but in this case a performance hit is better than throwing.
stringifiedPayload
=
JSON
.
stringify
(
normalize
(
payload
));
}
append
(
stringifiedPayload
);
}
}
return
typeof
parts
===
'string'
?
parts
:
concatBuffers
(
parts
);
}
function
concatBuffers
(
buffers
)
{
const
totalLength
=
buffers
.
reduce
((
acc
,
buf
)
=>
acc
+
buf
.
length
,
0
);
const
merged
=
new
Uint8Array
(
totalLength
);
let
offset
=
0
;
for
(
const
buffer
of
buffers
)
{
merged
.
set
(
buffer
,
offset
);
offset
+=
buffer
.
length
;
}
return
merged
;
}
/**
* Creates attachment envelope items
*/
function
createAttachmentEnvelopeItem
(
attachment
,
textEncoder
,
)
{
const
buffer
=
typeof
attachment
.
data
===
'string'
?
encodeUTF8
(
attachment
.
data
,
textEncoder
)
:
attachment
.
data
;
return
[
dropUndefinedKeys
({
type
:
'attachment'
,
length
:
buffer
.
length
,
filename
:
attachment
.
filename
,
content_type
:
attachment
.
contentType
,
attachment_type
:
attachment
.
attachmentType
,
}),
buffer
,
];
}
const
ITEM_TYPE_TO_DATA_CATEGORY_MAP
=
{
session
:
'session'
,
sessions
:
'session'
,
attachment
:
'attachment'
,
transaction
:
'transaction'
,
event
:
'error'
,
client_report
:
'internal'
,
user_report
:
'default'
,
};
/**
* Maps the type of an envelope item to a data category.
*/
function
envelopeItemTypeToDataCategory
(
type
)
{
return
ITEM_TYPE_TO_DATA_CATEGORY_MAP
[
type
];
}
export
{
addItemToEnvelope
,
createAttachmentEnvelopeItem
,
createEnvelope
,
envelopeItemTypeToDataCategory
,
forEachEnvelopeItem
,
serializeEnvelope
};
//# sourceMappingURL=envelope.js.map
File Metadata
Details
Attached
Mime Type
text/x-Algol68
Expires
Fri, Jul 4, 11:17 AM (3 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3368678
Attached To
rDWAPPS Web applications
Event Timeline
Log In to Comment