Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
I
iaam-libs
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
IAAM
iaam-libs
Commits
9464eaa4
Commit
9464eaa4
authored
Nov 26, 2025
by
Jukkrapong Ponharn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement UTF-8 safe Base64 encode/decode functions and update PolicyRequest class to use them
parent
2f8cd049
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
3 deletions
+40
-3
index.ts
src/index.ts
+40
-3
No files found.
src/index.ts
View file @
9464eaa4
import
{
encode
as
base64Encode
,
decode
as
base64Decode
}
from
'base-64'
;
import
axios
from
'axios'
;
/**
...
...
@@ -81,6 +80,44 @@ async function getPublicIP(): Promise<string | null> {
}
}
// UTF-8 safe Base64 encode/decode helpers (Node + browser)
function
base64EncodeUtf8
(
input
:
string
):
string
{
try
{
if
(
typeof
Buffer
!==
'undefined'
&&
typeof
Buffer
.
from
===
'function'
)
{
return
Buffer
.
from
(
input
,
'utf8'
).
toString
(
'base64'
);
}
const
btoaFn
=
(
globalThis
as
any
).
btoa
;
if
(
typeof
btoaFn
===
'function'
)
{
// encodeURIComponent -> percent-encode -> unescape to binary string
return
btoaFn
(
unescape
(
encodeURIComponent
(
input
)));
}
}
catch
(
e
)
{
// fallthrough to throw below
}
throw
new
Error
(
'No available method to perform base64 encoding'
);
}
function
base64DecodeUtf8
(
b64
:
string
):
string
{
try
{
if
(
typeof
Buffer
!==
'undefined'
&&
typeof
Buffer
.
from
===
'function'
)
{
return
Buffer
.
from
(
b64
,
'base64'
).
toString
(
'utf8'
);
}
const
atobFn
=
(
globalThis
as
any
).
atob
;
if
(
typeof
atobFn
===
'function'
)
{
const
binary
=
atobFn
(
b64
);
// convert binary string to percent-encoded string, then decode
const
percentEncoded
=
Array
.
prototype
.
map
.
call
(
binary
,
(
ch
:
string
)
=>
{
const
code
=
ch
.
charCodeAt
(
0
).
toString
(
16
).
toUpperCase
();
return
'%'
+
(
code
.
length
<
2
?
'0'
+
code
:
code
);
}).
join
(
''
);
return
decodeURIComponent
(
percentEncoded
);
}
}
catch
(
e
)
{
// fallthrough to throw below
}
throw
new
Error
(
'No available method to perform base64 decoding'
);
}
/**
* Policy Request Instance Configuration
*/
...
...
@@ -179,7 +216,7 @@ export class PolicyRequest {
};
const
jsonString
=
JSON
.
stringify
(
data
);
return
base64Encode
(
jsonString
);
return
base64Encode
Utf8
(
jsonString
);
}
/**
...
...
@@ -217,7 +254,7 @@ export class PolicyRequest {
*/
static
decode
(
base64String
:
string
):
PolicyRequestData
{
try
{
const
decoded
=
base64Decode
(
base64String
);
const
decoded
=
base64Decode
Utf8
(
base64String
);
return
JSON
.
parse
(
decoded
);
}
catch
(
error
)
{
throw
new
Error
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment