Community
Access to non blocking sleep within Onifys sandboxed Node?
Onifys sandboxed Node unfortunately lacks quite a few native Node functions.
I often find code examples that does what I want and it works fine using my locally installed Node but once deployed in Onify it will cause an error cause a lot of the native functions does not exist. 😟
//List some of the native Node functions that are missing in Onify Node sandbox
function listUnavailableGlobals() {
const unavailableGlobals = [
'structuredClone',
'TextEncoder',
'TextDecoder',
'URL',
'fetch',
'setTimeout',
'setInterval',
'clearInterval',
'resolve',
]
for (const key of unavailableGlobals) {
let type
try {
// @ts-ignore
type = typeof eval(key)
} catch (err) {
type = `error: ${err?.message}`
}
console.log(`typeof ${key}: ${type}`)
}
}
As a workaround I have to rewrite them from scratch. For example:
//Workaround as Onify does not have structuredClone
function structuredCloneFallback(obj) {
return JSON.parse(JSON.stringify(obj))
}
//Workaround as Onify does not have TextEncoder
export function stringUtf8Bytes({ string }) {
if (!string) {
return 0
}
let byteCount = 0
for (const char of string) {
const codePoint = char.codePointAt(0)
if (!codePoint) {
continue
}
if (codePoint <= 0x7f) {
byteCount += 1 // 1 byte för ASCII
} else if (codePoint <= 0x7ff) {
byteCount += 2 // 2 byte för tecken upp till U+07FF
} else if (codePoint <= 0xffff) {
byteCount += 3 // 3 byte för de flesta tecken
} else {
byteCount += 4 // 4 byte för ovanliga Unicode-tecken
}
}
return byteCount
}
Also the class URL is really handy but in Onify we have to parse the urls as a string with regex instead.
As you can see - most of the time it is possible to make a workaround, but it feels like a lot of unnecessary work.
Most probably they are also less performant that the native function.
Why are these removed? What harm could they make?
Now to my current problem. To just simulate a small pause we can instead of setTimer use:
export function pauseByDateTime({ milliseconds }) {
const startTime = Date.now()
while (Date.now() - startTime < milliseconds) {
// Busy-wait loop
}
}
Now, we already concluded that static delays should be avoided as of
[https: support.onify.co discuss 66e7f6e454b49100d50593d2](https: support.onify.co discuss 66e7f6e454b49100d50593d2)
so I don’t even use that function.
But even if it works, there is a bigger problem and that is that it is a thread blocking sleep that won’t let any other code run synchronous.
Now I’m really in need of a non blocking sleep.
Can you advice how to make that within Node in Onify? Is it possible?
Long story short - I use a wrapper around GOT which automatically gets a token for the endpoints on the first request and populate each call with the necessary headers automatically and reuses the token as long as they are valid and then gets new ones. Works perfectly, make less code, redundancy and is more maintainable. However, when using Promise.all for a bunch of request i need the first one to set a flag for the others to wait until it is finished getting the token. For this I need a non blocking sleep. :)