feat: don't require --unstable flag for npm programs (#16520)

This PR adds copies of several unstable APIs that are available
in "Deno[Deno.internal].nodeUnstable" namespace.

These copies do not perform unstable check (ie. don't require
"--unstable" flag to be present). Otherwise they work exactly
the same, including permission checks.

These APIs are not meant to be used by users directly and
can change at any time.

Copies of following APIs are available in that namespace:
- Deno.spawnChild
- Deno.spawn
- Deno.spawnSync
- Deno.serve
- Deno.upgradeHttpRaw
- Deno.listenDatagram
This commit is contained in:
Bartek Iwańczuk 2022-11-10 22:03:28 +01:00 committed by GitHub
parent 53e974b276
commit 8d5c0112fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 576 additions and 329 deletions

View file

@ -700,6 +700,7 @@ delete Intl.v8BreakIterator;
const wrapConsole = window.__bootstrap.console.wrapConsole;
// Remove bootstrapping data from the global scope
const __bootstrap = globalThis.__bootstrap;
delete globalThis.__bootstrap;
delete globalThis.bootstrap;
util.log("bootstrapMainRuntime");
@ -755,6 +756,27 @@ delete Intl.v8BreakIterator;
const internalSymbol = Symbol("Deno.internal");
// These have to initialized here and not in `90_deno_ns.js` because
// the op function that needs to be passed will be invalidated by creating
// a snapshot
ObjectAssign(internals, {
nodeUnstable: {
spawnChild: __bootstrap.spawn.createSpawnChild(
ops.op_node_unstable_spawn_child,
),
spawn: __bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child),
spawnSync: __bootstrap.spawn.createSpawnSync(
ops.op_node_unstable_spawn_sync,
),
serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve),
upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw,
listenDatagram: __bootstrap.net.createListenDatagram(
ops.op_node_unstable_net_listen_udp,
ops.op_node_unstable_net_listen_unixpacket,
),
},
});
const finalDenoNs = {
core,
internal: internalSymbol,
@ -773,6 +795,19 @@ delete Intl.v8BreakIterator;
if (runtimeOptions.unstableFlag) {
ObjectAssign(finalDenoNs, denoNsUnstable);
// These have to initialized here and not in `90_deno_ns.js` because
// the op function that needs to be passed will be invalidated by creating
// a snapshot
ObjectAssign(finalDenoNs, {
spawnChild: __bootstrap.spawn.createSpawnChild(ops.op_spawn_child),
spawn: __bootstrap.spawn.createSpawn(ops.op_spawn_child),
spawnSync: __bootstrap.spawn.createSpawnSync(ops.op_spawn_sync),
serve: __bootstrap.flash.createServe(ops.op_flash_serve),
listenDatagram: __bootstrap.net.createListenDatagram(
ops.op_net_listen_udp,
ops.op_net_listen_unixpacket,
),
});
}
// Setup `Deno` global - we're actually overriding already existing global
@ -800,6 +835,7 @@ delete Intl.v8BreakIterator;
const wrapConsole = window.__bootstrap.console.wrapConsole;
// Remove bootstrapping data from the global scope
const __bootstrap = globalThis.__bootstrap;
delete globalThis.__bootstrap;
delete globalThis.bootstrap;
util.log("bootstrapWorkerRuntime");
@ -849,6 +885,27 @@ delete Intl.v8BreakIterator;
const internalSymbol = Symbol("Deno.internal");
// These have to initialized here and not in `90_deno_ns.js` because
// the op function that needs to be passed will be invalidated by creating
// a snapshot
ObjectAssign(internals, {
nodeUnstable: {
spawnChild: __bootstrap.spawn.createSpawnChild(
ops.op_node_unstable_spawn_child,
),
spawn: __bootstrap.spawn.createSpawn(ops.op_node_unstable_spawn_child),
spawnSync: __bootstrap.spawn.createSpawnSync(
ops.op_node_unstable_spawn_sync,
),
serve: __bootstrap.flash.createServe(ops.op_node_unstable_flash_serve),
upgradeHttpRaw: __bootstrap.flash.upgradeHttpRaw,
listenDatagram: __bootstrap.net.createListenDatagram(
ops.op_node_unstable_net_listen_udp,
ops.op_node_unstable_net_listen_unixpacket,
),
},
});
const finalDenoNs = {
core,
internal: internalSymbol,
@ -859,6 +916,19 @@ delete Intl.v8BreakIterator;
};
if (runtimeOptions.unstableFlag) {
ObjectAssign(finalDenoNs, denoNsUnstable);
// These have to initialized here and not in `90_deno_ns.js` because
// the op function that needs to be passed will be invalidated by creating
// a snapshot
ObjectAssign(finalDenoNs, {
spawnChild: __bootstrap.spawn.createSpawnChild(ops.op_spawn_child),
spawn: __bootstrap.spawn.createSpawn(ops.op_spawn_child),
spawnSync: __bootstrap.spawn.createSpawnSync(ops.op_spawn_sync),
serve: __bootstrap.flash.createServe(ops.op_flash_serve),
listenDatagram: __bootstrap.net.createListenDatagram(
ops.op_net_listen_udp,
ops.op_net_listen_unixpacket,
),
});
}
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(runtimeOptions.pid),