socket-function 0.9.6 → 0.10.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.
@@ -0,0 +1,6 @@
1
+ module.allowclient = true;
2
+ import { isNode } from "./src/misc";
3
+
4
+ if (!isNode()) {
5
+ process.env.CBOR_NATIVE_ACCELERATION_DISABLED = "true";
6
+ }
package/SocketFunction.ts CHANGED
@@ -12,6 +12,7 @@ import { lazy } from "./src/caching";
12
12
  import { delay } from "./src/batching";
13
13
  import { blue, magenta } from "./src/formatting/logColors";
14
14
  import { JSONLACKS } from "./src/JSONLACKS/JSONLACKS";
15
+ import "./SetProcessVariables";
15
16
  import cborx from "cbor-x";
16
17
  import { setFlag } from "./require/compileFlags";
17
18
  setFlag(require, "cbor-x", "allowclient", true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.9.6",
3
+ "version": "0.10.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",
@@ -10,7 +10,7 @@
10
10
  "mobx": "^6.6.2",
11
11
  "node-forge": "https://github.com/sliftist/forge#name",
12
12
  "preact": "^10.10.6",
13
- "typenode": "^4.9.4-j",
13
+ "typenode": "5.3.3",
14
14
  "ws": "^8.8.0"
15
15
  },
16
16
  "optionalDependencies": {
@@ -9,6 +9,9 @@
9
9
  // Mirror the tnode.js setting
10
10
  NODE_ENV: "production"
11
11
  },
12
+ versions: {
13
+
14
+ },
12
15
  },
13
16
  setImmediate(callback) {
14
17
  setTimeout(callback, 0);
package/src/batching.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { isNode } from "./misc";
2
2
  import { measureWrap } from "./profiling/measure";
3
+ import { MaybePromise } from "./types";
3
4
 
4
5
  /*
5
6
  "numbers" use setTimeout
@@ -35,6 +36,14 @@ export function delay(delayTime: DelayType): Promise<void> {
35
36
  }
36
37
  }
37
38
 
39
+ // NOTE: This is an easy way to turn off batching, without having to strip the extra batch handling code
40
+ export function batchFunctionNone<Arg, Result = void>(
41
+ config: unknown,
42
+ fnc: (arg: Arg[]) => (Promise<Result> | Result)
43
+ ): (arg: Arg) => Promise<Result> {
44
+ return async arg => fnc([arg]);
45
+ }
46
+
38
47
  export function batchFunction<Arg, Result = void>(
39
48
  config: {
40
49
  delay: DelayType;
@@ -50,6 +50,8 @@ export function getNodeIdsFromRequest(request: http.IncomingMessage) {
50
50
 
51
51
  export async function httpCallHandler(request: http.IncomingMessage, response: http.ServerResponse) {
52
52
  try {
53
+ // Always set x-frame-options, to prevent iframe embedding click hijacking
54
+ response.setHeader("X-Frame-Options", "SAMEORIGIN");
53
55
 
54
56
  let urlBase = request.url;
55
57
  if (!urlBase) {
package/src/misc.ts CHANGED
@@ -313,4 +313,53 @@ export function last<T>(arr: T[]): T | undefined {
313
313
  export type ObjectValues<T> = T[keyof T];
314
314
  export function entries<Obj extends { [key: string]: unknown }>(obj: Obj): [keyof Obj, ObjectValues<Obj>][] {
315
315
  return Object.entries(obj) as any;
316
+ }
317
+
318
+ export function sort<T>(arr: T[], sortKey: (obj: T) => unknown) {
319
+ if (arr.length <= 1) return arr;
320
+ arr.sort((a, b) => compare(sortKey(a), sortKey(b)));
321
+ return arr;
322
+ }
323
+
324
+ // NOTE: If there are duplicates, returns the first match.
325
+ export function binarySearchIndex(listCount: number, compare: (lhsIndex: number) => number): number {
326
+ if (listCount === 0) {
327
+ return ~0;
328
+ }
329
+ let min = 0;
330
+ let max = listCount - 1;
331
+ while (min < max) {
332
+ let fingerIndex = Math.floor((max + min) / 2);
333
+ let comparisonValue = compare(fingerIndex);
334
+ if (comparisonValue < 0) {
335
+ min = fingerIndex + 1;
336
+ } else {
337
+ max = fingerIndex;
338
+ }
339
+ }
340
+ let comparison = compare(min);
341
+ if (comparison === 0) return min;
342
+ if (comparison > 0) return ~min;
343
+ return ~(min + 1);
344
+ }
345
+
346
+ export function compare(lhs: unknown, rhs: unknown): number {
347
+ if (typeof lhs !== typeof rhs) {
348
+ return compare(typeof lhs, typeof rhs);
349
+ }
350
+ if (lhs === rhs) return 0;
351
+ if (lhs as any < (rhs as any)) return -1;
352
+ return 1;
353
+ }
354
+
355
+ export function insertIntoSortedList<T>(list: T[], map: (val: T) => string | number, element: T) {
356
+ let searchValue = map(element);
357
+ let index = binarySearchIndex(list.length, i => compare(map(list[i]), searchValue));
358
+ if (index < 0) index = ~index;
359
+ list.splice(index, 0, element);
360
+ }
361
+ export function removeFromSortedList<T>(list: T[], map: (val: T) => string | number, searchValue: string | number) {
362
+ let index = binarySearchIndex(list.length, i => compare(map(list[i]), searchValue));
363
+ if (index < 0) return;
364
+ list.splice(index, 1);
316
365
  }
package/src/nodeCache.ts CHANGED
@@ -50,9 +50,16 @@ export function getNodeIdLocation(nodeId: string): { address: string, port: numb
50
50
  }
51
51
 
52
52
  export function getNodeIdDomain(nodeId: string): string {
53
+ let result = getNodeIdDomainMaybeUndefined(nodeId);
54
+ if (result === undefined) {
55
+ throw new Error(`Cannot get domain from nodeId, which is only usable as a client. NodeId: ${JSON.stringify(nodeId)}`);
56
+ }
57
+ return result;
58
+ }
59
+ export function getNodeIdDomainMaybeUndefined(nodeId: string): string | undefined {
53
60
  let location = getNodeIdLocation(nodeId);
54
61
  if (!location) {
55
- throw new Error(`Cannot get domain from nodeId, which is only usable as a client. NodeId: ${JSON.stringify(nodeId)}`);
62
+ return undefined;
56
63
  }
57
64
  return new URL(location.address).hostname.split(".").slice(-2).join(".");
58
65
  }