-
Notifications
You must be signed in to change notification settings - Fork 576
/
util.ts
92 lines (75 loc) · 2.32 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
import { digestAlgorithms } from "../../../_wasm_crypto/mod.ts";
import { getCiphers } from "../../_crypto/crypto_browserify/browserify_aes/mod.js";
import { notImplemented } from "../../_utils.ts";
import { Buffer } from "../../buffer.ts";
import { ERR_INVALID_ARG_TYPE, hideStackFrames } from "../errors.ts";
import { isAnyArrayBuffer, isArrayBufferView } from "../util/types.ts";
import { crypto as constants } from "../../internal_binding/constants.ts";
import { kHandle, kKeyObject } from "./constants.ts";
let defaultEncoding = "buffer";
export function setDefaultEncoding(val: string) {
defaultEncoding = val;
}
export function getDefaultEncoding(): string {
return defaultEncoding;
}
// This is here because many functions accepted binary strings without
// any explicit encoding in older versions of node, and we don't want
// to break them unnecessarily.
export function toBuf(val: string | Buffer, encoding?: string): Buffer {
if (typeof val === "string") {
if (encoding === "buffer") {
encoding = "utf8";
}
return Buffer.from(val, encoding);
}
return val;
}
export const validateByteSource = hideStackFrames((val, name) => {
val = toBuf(val);
if (isAnyArrayBuffer(val) || isArrayBufferView(val)) {
return;
}
throw new ERR_INVALID_ARG_TYPE(
name,
["string", "ArrayBuffer", "TypedArray", "DataView", "Buffer"],
val,
);
});
/**
* Returns an array of the names of the supported hash algorithms, such as 'sha1'.
*/
export function getHashes(): readonly string[] {
return digestAlgorithms;
}
export function getCurves(): readonly string[] {
notImplemented("crypto.getCurves");
}
export interface SecureHeapUsage {
total: number;
min: number;
used: number;
utilization: number;
}
export function secureHeapUsed(): SecureHeapUsage {
notImplemented("crypto.secureHeapUsed");
}
export function setEngine(_engine: string, _flags: typeof constants) {
notImplemented("crypto.setEngine");
}
export { getCiphers, kHandle, kKeyObject };
export default {
getDefaultEncoding,
getHashes,
setDefaultEncoding,
getCiphers,
getCurves,
secureHeapUsed,
setEngine,
validateByteSource,
toBuf,
kHandle,
kKeyObject,
};