MCPcopy Create free account
hub / github.com/cryptii/cryptii / performEncode

Method performEncode

src/Encoder/URL.js:34–57  ·  view source on GitHub ↗

* Performs encode on given content. * @protected * @param {Chain} content * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content

(content)

Source from the content-addressed store, hash-verified

32 * @return {number[]|string|Uint8Array|Chain|Promise} Encoded content
33 */
34 performEncode (content) {
35 try {
36 // Try to URL encode string representation of content
37 const chars = content.getChars()
38 let string = ''
39 let char
40 for (let i = 0; i < chars.length; i++) {
41 char = chars[i]
42 string += unreservedURLCharacters.indexOf(char) === -1
43 ? this.encodeBytes(content.getCharBytesAt(i))
44 : char
45 }
46 return string
47 } catch (error) {
48 // Retrieving the string representation of the content may throw an error
49 // due to bad UTF-8 encoded text
50 if (error instanceof TextEncodingError) {
51 // Encode raw bytes (which do not need to be valid UTF-8)
52 return this.encodeBytes(content.getBytes())
53 } else {
54 throw error
55 }
56 }
57 }
58
59 /**
60 * Percent-encode bytes.

Callers

nothing calls this directly

Calls 5

encodeBytesMethod · 0.95
getCharsMethod · 0.80
indexOfMethod · 0.80
getCharBytesAtMethod · 0.80
getBytesMethod · 0.80

Tested by

no test coverage detected