mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-10 07:55:06 +00:00
clean: remove redundant promise.js
This commit is contained in:
1 parent
86a9c959e0
commit
09b0d744d5
28 files changed
+30
-188
No files matched your search
@@ -22,7 +22,7 @@ const commands = require('./commands').commands;
|
||||
const APIError = require('../../api/APIError');
|
||||
const { Context } = require('../../util/context');
|
||||
const config = require('../../config');
|
||||
const { TeePromise } = require('../../util/promise');
|
||||
const { TeePromise } = require('@heyputer/putility');
|
||||
const { WorkUnit } = require('../../modules/core/lib/expect');
|
||||
|
||||
class BatchExecutor extends AdvancedBase {
|
||||
|
||||
@@ -23,7 +23,7 @@ const StringParam = require("../../api/filesystem/StringParam");
|
||||
const UserParam = require("../../api/filesystem/UserParam");
|
||||
const config = require("../../config");
|
||||
const { chkperm, validate_fsentry_name } = require("../../helpers");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require("@heyputer/putility").libs.promise;
|
||||
const { pausing_tee, logging_stream, offset_write_stream, stream_to_the_void } = require("../../util/streamutil");
|
||||
const { TYPE_DIRECTORY } = require("../FSNodeContext");
|
||||
const { LLRead } = require("../ll_operations/ll_read");
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const config = require('../config');
|
||||
const { TeePromise } = require('../util/promise');
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
const es_import_promise = new TeePromise();
|
||||
let stringLength;
|
||||
|
||||
@@ -5,7 +5,7 @@ const { whatis } = require("../../util/langutil");
|
||||
const { PassThrough } = require("stream");
|
||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const APIError = require("../../api/APIError");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
const PUTER_PROMPT = `
|
||||
You are running on an open-source platform called Puter,
|
||||
|
||||
@@ -3,7 +3,7 @@ const { PassThrough } = require("stream");
|
||||
const BaseService = require("../../services/BaseService");
|
||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const { nou } = require("../../util/langutil");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,7 @@ const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const { nou } = require("../../util/langutil");
|
||||
|
||||
const axios = require('axios');
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,7 +6,7 @@ const { TypedValue } = require('../../services/drivers/meta/Runtime');
|
||||
const { Context } = require('../../util/context');
|
||||
const SmolUtil = require('../../util/smolutil');
|
||||
const { nou } = require('../../util/langutil');
|
||||
const { TeePromise } = require('../../util/promise');
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@ const { PassThrough } = require("stream");
|
||||
const BaseService = require("../../services/BaseService");
|
||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const { nou } = require("../../util/langutil");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@ const BaseService = require("../../services/BaseService");
|
||||
const { whatis, nou } = require("../../util/langutil");
|
||||
const { PassThrough } = require("stream");
|
||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
const PUTER_PROMPT = `
|
||||
You are running on an open-source platform called Puter,
|
||||
|
||||
@@ -22,7 +22,7 @@ const { NodePathSelector } = require("../../filesystem/node/selectors");
|
||||
const { surrounding_box } = require("../../fun/dev-console-ui-utils");
|
||||
const { get_user, invalidate_cached_user } = require("../../helpers");
|
||||
const { Context } = require("../../util/context");
|
||||
const { asyncSafeSetInterval } = require("../../util/promise");
|
||||
const { asyncSafeSetInterval } = require('@heyputer/putility').libs.promise;
|
||||
const { buffer_to_stream } = require("../../util/streamutil");
|
||||
const BaseService = require("../../services/BaseService");
|
||||
const { Actor, UserActorType } = require("../../services/auth/Actor");
|
||||
|
||||
@@ -23,7 +23,7 @@ const { TypeSpec } = require("../../services/drivers/meta/Construct");
|
||||
const { TypedValue } = require("../../services/drivers/meta/Runtime");
|
||||
const { Context } = require("../../util/context");
|
||||
const { whatis } = require("../../util/langutil");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { valid_file_size } = require("../../util/validutil");
|
||||
|
||||
let _handle_multipart;
|
||||
|
||||
@@ -23,7 +23,7 @@ const PathResolver = require("./PathResolver");
|
||||
const { Context } = require("../../../util/context");
|
||||
const Busboy = require('busboy');
|
||||
const { BatchExecutor } = require("../../../filesystem/batch/BatchExecutor");
|
||||
const { TeePromise } = require("../../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { EWMA, MovingMode } = require("../../../util/opmath");
|
||||
const { get_app } = require('../../../helpers');
|
||||
const { valid_file_size } = require("../../../util/validutil");
|
||||
|
||||
@@ -23,7 +23,7 @@ const { HLWrite } = require('../../filesystem/hl_operations/hl_write.js');
|
||||
const { boolify } = require('../../util/hl_types.js');
|
||||
const { Context } = require('../../util/context.js');
|
||||
const Busboy = require('busboy');
|
||||
const { TeePromise } = require('../../util/promise.js');
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const APIError = require('../../api/APIError.js');
|
||||
const { valid_file_size } = require('../../util/validutil.js');
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { asyncSafeSetInterval } = require("../util/promise");
|
||||
const { asyncSafeSetInterval } = require('@heyputer/putility').libs.promise;
|
||||
const { MINUTE, SECOND } = require("@heyputer/putility").libs.time;
|
||||
const { origin_from_url } = require("../util/urlutil");
|
||||
const { DB_READ } = require("./database/consts");
|
||||
|
||||
@@ -21,7 +21,7 @@ const { AdvancedBase } = require("@heyputer/putility");
|
||||
const config = require("../config");
|
||||
const { Context } = require("../util/context");
|
||||
const { CompositeError } = require("../util/errorutil");
|
||||
const { TeePromise } = require("../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
// 17 lines of code instead of an entire dependency-injection framework
|
||||
/**
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { LocalDiskStorageStrategy } = require("../filesystem/strategies/storage_a/LocalDiskStorageStrategy");
|
||||
const { TeePromise } = require("../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { progress_stream, size_limit_stream } = require("../util/streamutil");
|
||||
const BaseService = require("./BaseService");
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
const APIError = require("../api/APIError");
|
||||
const auth2 = require("../middleware/auth2");
|
||||
const { Endpoint } = require("../util/expressutil");
|
||||
const { TeePromise } = require("../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const BaseService = require("./BaseService");
|
||||
const { DB_WRITE } = require("./database/consts");
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// METADATA // {"ai-commented":{"service":"openai-completion","model":"gpt-4o-mini"}}
|
||||
const { get_user } = require("../helpers");
|
||||
const { Context } = require("../util/context");
|
||||
const { TeePromise } = require("../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { Actor, UserActorType } = require("./auth/Actor");
|
||||
const BaseService = require("./BaseService");
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { invalidate_cached_user, get_user } = require("../helpers");
|
||||
const { asyncSafeSetInterval } = require("../util/promise");
|
||||
const { asyncSafeSetInterval } = require('@heyputer/putility').libs.promise;
|
||||
const { MINUTE, SECOND } = require("@heyputer/putility").libs.time;
|
||||
const BaseService = require("./BaseService");
|
||||
const { DB_WRITE } = require("./database/consts");
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { Context } = require("../../util/context");
|
||||
const { asyncSafeSetInterval } = require("../../util/promise");
|
||||
const { asyncSafeSetInterval } = require('@heyputer/putility').libs.promise;
|
||||
const { quot } = require('@heyputer/putility').libs.string;
|
||||
|
||||
const { MINUTE, HOUR } = require('@heyputer/putility').libs.time;
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { RWLock } = require("../../util/lockutil");
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const BaseService = require("../BaseService");
|
||||
|
||||
// Constant representing the read lock mode used for distinguishing between read and write operations.
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const BaseService = require("../BaseService");
|
||||
const { SECOND } = require("@heyputer/putility").libs.time;
|
||||
const { time, promise } = require("@heyputer/putility").libs;
|
||||
const { parse_meminfo } = require("../../util/linux");
|
||||
const { asyncSafeSetInterval, TeePromise } = require("../../util/promise");
|
||||
|
||||
|
||||
/**
|
||||
@@ -135,11 +134,11 @@ class ServerHealthService extends BaseService {
|
||||
* @param {none} - No parameters are passed to this method.
|
||||
* @returns {void}
|
||||
*/
|
||||
asyncSafeSetInterval(async () => {
|
||||
promise.asyncSafeSetInterval(async () => {
|
||||
this.log.tick('service checks');
|
||||
const check_failures = [];
|
||||
for ( const { name, fn, chainable } of this.checks_ ) {
|
||||
const p_timeout = new TeePromise();
|
||||
const p_timeout = new promise.TeePromise();
|
||||
/**
|
||||
* Creates a TeePromise to handle potential timeouts during health checks.
|
||||
*
|
||||
@@ -147,7 +146,7 @@ class ServerHealthService extends BaseService {
|
||||
*/
|
||||
const timeout = setTimeout(() => {
|
||||
p_timeout.reject(new Error('Health check timed out'));
|
||||
}, 5 * SECOND);
|
||||
}, 5 * time.SECOND);
|
||||
try {
|
||||
await Promise.race([
|
||||
fn(),
|
||||
@@ -180,7 +179,7 @@ class ServerHealthService extends BaseService {
|
||||
}
|
||||
|
||||
this.failures_ = check_failures;
|
||||
}, 10 * SECOND, null, {
|
||||
}, 10 * time.SECOND, null, {
|
||||
onBehindSchedule: (drift) => {
|
||||
svc_alarm.create(
|
||||
'health-checks-behind-schedule',
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
const { TeePromise } = require("../../util/promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { AdvancedBase } = require('@heyputer/putility');
|
||||
const FormData = require("form-data");
|
||||
const { stream_to_the_void, buffer_to_stream } = require('../../util/streamutil');
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { Lock } = require("../util/promise");
|
||||
const { Lock } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
class SyncFeature {
|
||||
constructor (method_include_list) {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { TeePromise } = require("./promise");
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
class RWLock {
|
||||
static TYPE_READ = Symbol('read');
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
const { context, trace, SpanStatusCode } = require('@opentelemetry/api');
|
||||
const { Context } = require('./context');
|
||||
const { TeePromise } = require('./promise');
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
|
||||
/*
|
||||
parallel span example from GPT-4:
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2024 Puter Technologies Inc.
|
||||
*
|
||||
* This file is part of Puter.
|
||||
*
|
||||
* Puter is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
class TeePromise {
|
||||
static STATUS_PENDING = Symbol('pending');
|
||||
static STATUS_RUNNING = {};
|
||||
static STATUS_DONE = Symbol('done');
|
||||
constructor () {
|
||||
this.status_ = this.constructor.STATUS_PENDING;
|
||||
this.donePromise = new Promise((resolve, reject) => {
|
||||
this.doneResolve = resolve;
|
||||
this.doneReject = reject;
|
||||
});
|
||||
}
|
||||
get status () {
|
||||
return this.status_;
|
||||
}
|
||||
set status (status) {
|
||||
this.status_ = status;
|
||||
if ( status === this.constructor.STATUS_DONE ) {
|
||||
this.doneResolve();
|
||||
}
|
||||
}
|
||||
resolve (value) {
|
||||
this.status_ = this.constructor.STATUS_DONE;
|
||||
this.doneResolve(value);
|
||||
}
|
||||
awaitDone () {
|
||||
return this.donePromise;
|
||||
}
|
||||
then (fn, ...a) {
|
||||
return this.donePromise.then(fn, ...a);
|
||||
}
|
||||
|
||||
reject (err) {
|
||||
this.status_ = this.constructor.STATUS_DONE;
|
||||
this.doneReject(err);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use then() instead
|
||||
*/
|
||||
onComplete(fn) {
|
||||
return this.then(fn);
|
||||
}
|
||||
}
|
||||
|
||||
class Lock {
|
||||
constructor() {
|
||||
this._locked = false;
|
||||
this._waiting = [];
|
||||
}
|
||||
|
||||
async acquire(callback) {
|
||||
await new Promise(resolve => {
|
||||
if ( ! this._locked ) {
|
||||
this._locked = true;
|
||||
resolve();
|
||||
} else {
|
||||
this._waiting.push({
|
||||
resolve,
|
||||
});
|
||||
}
|
||||
})
|
||||
if ( callback ) {
|
||||
let retval;
|
||||
try {
|
||||
retval = await callback();
|
||||
} finally {
|
||||
this.release();
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
release() {
|
||||
if (this._waiting.length > 0) {
|
||||
const { resolve } = this._waiting.shift();
|
||||
resolve();
|
||||
} else {
|
||||
this._locked = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @callback behindScheduleCallback
|
||||
* @param {number} drift - The number of milliseconds that the callback was
|
||||
* called behind schedule.
|
||||
* @returns {boolean} - If the callback returns true, the timer will be
|
||||
* cancelled.
|
||||
*/
|
||||
|
||||
/**
|
||||
* When passing an async callback to setInterval, it's possible for the
|
||||
* callback to be called again before the previous invocation has finished.
|
||||
*
|
||||
* This function wraps setInterval and ensures that the callback is not
|
||||
* called again until the previous invocation has finished.
|
||||
*
|
||||
* @param {Function} callback - The function to call when the timer elapses.
|
||||
* @param {number} delay - The minimum number of milliseconds between invocations.
|
||||
* @param {?Array<any>} args - Additional arguments to pass to setInterval.
|
||||
* @param {?Object} options - Additional options.
|
||||
* @param {behindScheduleCallback} options.onBehindSchedule - A callback to call when the callback is called behind schedule.
|
||||
*/
|
||||
const asyncSafeSetInterval = async (callback, delay, args, options) => {
|
||||
args = args ?? [];
|
||||
options = options ?? {};
|
||||
const { onBehindSchedule } = options;
|
||||
|
||||
const sleep = (ms) => new Promise(rslv => setTimeout(rslv, ms));
|
||||
|
||||
for ( ;; ) {
|
||||
await sleep(delay);
|
||||
|
||||
const ts_start = Date.now();
|
||||
await callback(...args);
|
||||
const ts_end = Date.now();
|
||||
|
||||
const runtime = ts_end - ts_start;
|
||||
const sleep_time = delay - runtime;
|
||||
|
||||
if ( sleep_time < 0 ) {
|
||||
if ( onBehindSchedule ) {
|
||||
const cancel = await onBehindSchedule(-sleep_time);
|
||||
if ( cancel ) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
await sleep(sleep_time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
TeePromise,
|
||||
Lock,
|
||||
asyncSafeSetInterval,
|
||||
};
|
||||
@@ -17,7 +17,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
const { PassThrough, Readable, Transform } = require('stream');
|
||||
const { TeePromise } = require('./promise');
|
||||
const { TeePromise } = require('@heyputer/putility').libs.promise;
|
||||
const { EWMA } = require('./opmath');
|
||||
|
||||
class StreamBuffer extends TeePromise {
|
||||
|
||||
Reference in new issue
Block a user