socket-function 0.28.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/SocketFunction.ts CHANGED
@@ -279,6 +279,9 @@ export class SocketFunction {
279
279
  * to add additional imports to ensure the register call runs.
280
280
  */
281
281
  public static expose(socketRegistered: SocketRegistered) {
282
+ if (!socketRegistered._classGuid) {
283
+ throw new Error("SocketFunction.expose must be called with a classGuid");
284
+ }
282
285
  console.log(`Exposing Controller ${blue(socketRegistered._classGuid)}`);
283
286
  exposeClass(socketRegistered);
284
287
  this.exposedClasses.add(socketRegistered._classGuid);
@@ -8,6 +8,7 @@ import * as fs from "fs";
8
8
  import debugbreak from "debugbreak";
9
9
  import { isNode } from "../src/misc";
10
10
  import { magenta, red } from "../src/formatting/logColors";
11
+ import { formatTime } from "../src/formatting/format";
11
12
 
12
13
  /** Enables some hot reload functionality.
13
14
  * - Triggers a refresh clientside
@@ -136,7 +137,7 @@ class HotReloadControllerBase {
136
137
  clientWatcherNodes.add(callerId);
137
138
  }
138
139
  async fileUpdated(files: string[], changeTime: number) {
139
- console.groupCollapsed(magenta(`Trigger hotreload for files (${Date.now() - changeTime}ms after file change)`));
140
+ console.groupCollapsed(magenta(`Trigger hotreload for files ${formatTime(Date.now() - changeTime)} after file change`));
140
141
  for (let file of files) {
141
142
  console.log(file);
142
143
  }
@@ -171,7 +172,7 @@ class HotReloadControllerBase {
171
172
  for (let callback of hotReloadCallbacks) {
172
173
  callback(modules);
173
174
  }
174
- console.log(magenta(`Hot reload complete (${Date.now() - changeTime}ms after file change)`));
175
+ console.log(magenta(`Hot reload complete ${formatTime(Date.now() - changeTime)} after file change`));
175
176
  }
176
177
  }
177
178
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.28.0",
3
+ "version": "0.30.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -230,6 +230,34 @@ export function formatDateTime(time: number) {
230
230
  return date.getFullYear() + "/" + p(date.getMonth() + 1) + "/" + p(date.getDate()) + " " + strTime;
231
231
  }
232
232
 
233
+ /** 2024 January 1, Monday, 12:53:02pm */
234
+ export function formatNiceDateTime(time: number) {
235
+ function p(s: number) {
236
+ return s.toString().padStart(2, "0");
237
+ }
238
+ let date = new Date(time);
239
+ let hours = date.getHours();
240
+ let minutes = date.getMinutes();
241
+ let seconds = date.getSeconds();
242
+ let ampm = hours >= 12 ? "pm" : "am";
243
+ hours = hours % 12;
244
+ hours = hours ? hours : 12; // the hour '0' should be '12'
245
+ let strTime = p(hours) + ":" + p(minutes) + ":" + p(seconds) + ampm;
246
+ let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
247
+ return date.getFullYear() + " " + date.toLocaleString("default", { month: "long" }) + " " + date.getDate() + ", " + days[date.getDay()] + ", " + strTime;
248
+ }
249
+
250
+ /** 2024 January 1, Monday, 12:53:02pm (4 months ago) */
251
+ export function formatVeryNiceDateTime(time: number) {
252
+ if (!time) {
253
+ return "";
254
+ }
255
+ if (typeof time !== "number") {
256
+ return String(time);
257
+ }
258
+ return `${formatNiceDateTime(time)} (${formatTime(Date.now() - time)})`;
259
+ }
260
+
233
261
  /** YYYY/MM/DD */
234
262
  export function formatDate(time: number) {
235
263
  function p(s: number) {
package/src/misc.ts CHANGED
@@ -149,7 +149,11 @@ export function keyByArray<T, K>(arr: T[], getKey: (value: T) => K): Map<K, T[]>
149
149
 
150
150
  export function deepCloneJSON<T>(obj: T): T {
151
151
  if (obj === undefined) return obj;
152
- return JSON.parse(JSON.stringify(obj));
152
+ let str = JSON.stringify(obj);
153
+ // It is possible for an object to not be defined, but return undefined when JSON.stringified,
154
+ // via overriding the toJSON method.
155
+ if (str === undefined) return undefined as any;
156
+ return JSON.parse(str);
153
157
  }
154
158
 
155
159
  export class PromiseObj<T = void> {
@@ -30,6 +30,8 @@ const measureOverhead = 5 / 1000;
30
30
 
31
31
  const AsyncFunction = (async () => { }).constructor;
32
32
 
33
+ const noDiskLogPrefix = "\u200C";
34
+
33
35
  // TIMING: 1-5us. I have seen timing values greatly vary, but it does seem to be quite high, despite
34
36
  // microbenchmarks saying it is slow. Perhaps it is because getOwnTime breaks the cpu pipeline,
35
37
  // which causes slowness for code around us, but not if we are running in isolation?
@@ -131,12 +133,25 @@ export interface LogMeasureTableConfig {
131
133
  mergeDepth?: number;
132
134
  // Defaults to 10
133
135
  maxTableEntries?: number;
136
+
137
+ // No logging, just returns FormattedMeasureTable
138
+ returnOnly?: boolean;
139
+ }
140
+
141
+ export interface FormattedMeasureTable {
142
+ title: string;
143
+ entries: {
144
+ name: string;
145
+ ownTime: number;
146
+ fraction: number;
147
+ equation: string;
148
+ }[];
134
149
  }
135
150
 
136
151
  export function logMeasureTable(
137
152
  profile: MeasureProfile,
138
153
  config?: LogMeasureTableConfig
139
- ) {
154
+ ): FormattedMeasureTable | undefined {
140
155
  let { useTotalTime, name } = config || {};
141
156
  const thresholdInTable = config?.thresholdInTable ?? 0.05;
142
157
  let minTimeToLog = config?.minTimeToLog ?? 50;
@@ -149,7 +164,7 @@ export function logMeasureTable(
149
164
  entries.sort((a, b) => getTime(b).sum - getTime(a).sum);
150
165
 
151
166
  let totalTime = entries.map(x => getTime(x).sum).reduce((a, b) => a + b, 0);
152
- if (totalTime < minTimeToLog) return;
167
+ if (totalTime < minTimeToLog) return undefined;
153
168
 
154
169
  let mergeDepth = config?.mergeDepth ?? 2;
155
170
  {
@@ -186,7 +201,7 @@ export function logMeasureTable(
186
201
  if (name) {
187
202
  title = `(${blue(name)}) ${title}`;
188
203
  }
189
- console.log(title);
204
+ console.log(noDiskLogPrefix + title);
190
205
  function percent(value: number) {
191
206
  return `${(value * 100).toFixed(2)}%`;
192
207
  }
@@ -244,9 +259,23 @@ export function logMeasureTable(
244
259
  output += red(` (${entry.stillOpenCount} open)`);
245
260
  }
246
261
 
247
- console.log(output);
262
+ console.log(noDiskLogPrefix + output);
248
263
  }
249
264
  console.log();
265
+
266
+ return {
267
+ title,
268
+ entries: entries.map(entry => {
269
+ let time = getTime(entry);
270
+ let fraction = time.sum / totalTime;
271
+ return {
272
+ name: entry.name,
273
+ ownTime: time.sum,
274
+ fraction,
275
+ equation: formatStats(time),
276
+ };
277
+ })
278
+ };
250
279
  }
251
280
 
252
281
  export async function measureCode<T>(code: () => Promise<T>, config?: LogMeasureTableConfig) {