2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license.
|
|
|
|
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
import { notImplemented } from "internal:deno_node/_utils.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isArrayBufferView,
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
} from "internal:deno_node/internal/util/types.ts";
|
|
|
|
import { ERR_INVALID_ARG_TYPE } from "internal:deno_node/internal/errors.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
validateInt32,
|
|
|
|
validateString,
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
} from "internal:deno_node/internal/validators.mjs";
|
|
|
|
import { Buffer } from "internal:deno_node/buffer.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import {
|
|
|
|
getDefaultEncoding,
|
|
|
|
toBuf,
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
} from "internal:deno_node/internal/crypto/util.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
import type {
|
|
|
|
BinaryLike,
|
|
|
|
BinaryToTextEncoding,
|
|
|
|
ECDHKeyFormat,
|
refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten
to include it.
Example:
```
include_js_files! {
dir "js",
"hello.js",
}
```
The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`.
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in:
`import ... from "internal:<ext_name>/hello.js"`.
The directory the files are stored in, is an implementation detail and
in some cases might result in a significant size difference for the
snapshot. As an example, in "deno_node" extension, we store the
source code in "polyfills" directory; which resulted in each specifier
to look like "internal:deno_node/polyfills/<module_name>", but with
this change it's "internal:deno_node/<module_name>".
Given that "deno_node" has over 100 files, many of them having
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-04 21:31:38 -05:00
|
|
|
} from "internal:deno_node/internal/crypto/types.ts";
|
|
|
|
import { KeyObject } from "internal:deno_node/internal/crypto/keys.ts";
|
|
|
|
import type { BufferEncoding } from "internal:deno_node/_global.d.ts";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
const DH_GENERATOR = 2;
|
|
|
|
|
|
|
|
export class DiffieHellman {
|
|
|
|
verifyError!: number;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
sizeOrKey: unknown,
|
|
|
|
keyEncoding?: unknown,
|
|
|
|
generator?: unknown,
|
|
|
|
genEncoding?: unknown,
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
typeof sizeOrKey !== "number" &&
|
|
|
|
typeof sizeOrKey !== "string" &&
|
|
|
|
!isArrayBufferView(sizeOrKey) &&
|
|
|
|
!isAnyArrayBuffer(sizeOrKey)
|
|
|
|
) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"sizeOrKey",
|
|
|
|
["number", "string", "ArrayBuffer", "Buffer", "TypedArray", "DataView"],
|
|
|
|
sizeOrKey,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof sizeOrKey === "number") {
|
|
|
|
validateInt32(sizeOrKey, "sizeOrKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
keyEncoding &&
|
|
|
|
!Buffer.isEncoding(keyEncoding as BinaryToTextEncoding) &&
|
|
|
|
keyEncoding !== "buffer"
|
|
|
|
) {
|
|
|
|
genEncoding = generator;
|
|
|
|
generator = keyEncoding;
|
|
|
|
keyEncoding = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const encoding = getDefaultEncoding();
|
|
|
|
keyEncoding = keyEncoding || encoding;
|
|
|
|
genEncoding = genEncoding || encoding;
|
|
|
|
|
|
|
|
if (typeof sizeOrKey !== "number") {
|
|
|
|
sizeOrKey = toBuf(sizeOrKey as string, keyEncoding as string);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!generator) {
|
|
|
|
generator = DH_GENERATOR;
|
|
|
|
} else if (typeof generator === "number") {
|
|
|
|
validateInt32(generator, "generator");
|
|
|
|
} else if (typeof generator === "string") {
|
|
|
|
generator = toBuf(generator, genEncoding as string);
|
|
|
|
} else if (!isArrayBufferView(generator) && !isAnyArrayBuffer(generator)) {
|
|
|
|
throw new ERR_INVALID_ARG_TYPE(
|
|
|
|
"generator",
|
|
|
|
["number", "string", "ArrayBuffer", "Buffer", "TypedArray", "DataView"],
|
|
|
|
generator,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
notImplemented("crypto.DiffieHellman");
|
|
|
|
}
|
|
|
|
|
|
|
|
computeSecret(otherPublicKey: ArrayBufferView): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: ArrayBufferView,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
_otherPublicKey: ArrayBufferView | string,
|
|
|
|
_inputEncoding?: BinaryToTextEncoding,
|
|
|
|
_outputEncoding?: BinaryToTextEncoding,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.computeSecret");
|
|
|
|
}
|
|
|
|
|
|
|
|
generateKeys(): Buffer;
|
|
|
|
generateKeys(encoding: BinaryToTextEncoding): string;
|
|
|
|
generateKeys(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.generateKeys");
|
|
|
|
}
|
|
|
|
|
|
|
|
getGenerator(): Buffer;
|
|
|
|
getGenerator(encoding: BinaryToTextEncoding): string;
|
|
|
|
getGenerator(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getGenerator");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrime(): Buffer;
|
|
|
|
getPrime(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPrime(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPrime");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrivateKey(): Buffer;
|
|
|
|
getPrivateKey(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPrivateKey(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPrivateKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicKey(): Buffer;
|
|
|
|
getPublicKey(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPublicKey(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPublicKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
setPrivateKey(privateKey: ArrayBufferView): void;
|
|
|
|
setPrivateKey(privateKey: string, encoding: BufferEncoding): void;
|
|
|
|
setPrivateKey(
|
|
|
|
_privateKey: ArrayBufferView | string,
|
|
|
|
_encoding?: BufferEncoding,
|
|
|
|
) {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.setPrivateKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
setPublicKey(publicKey: ArrayBufferView): void;
|
|
|
|
setPublicKey(publicKey: string, encoding: BufferEncoding): void;
|
|
|
|
setPublicKey(
|
|
|
|
_publicKey: ArrayBufferView | string,
|
|
|
|
_encoding?: BufferEncoding,
|
|
|
|
) {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.setPublicKey");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DiffieHellmanGroup {
|
|
|
|
verifyError!: number;
|
|
|
|
|
|
|
|
constructor(_name: string) {
|
|
|
|
notImplemented("crypto.DiffieHellmanGroup");
|
|
|
|
}
|
|
|
|
|
|
|
|
computeSecret(otherPublicKey: ArrayBufferView): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: ArrayBufferView,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
_otherPublicKey: ArrayBufferView | string,
|
|
|
|
_inputEncoding?: BinaryToTextEncoding,
|
|
|
|
_outputEncoding?: BinaryToTextEncoding,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.computeSecret");
|
|
|
|
}
|
|
|
|
|
|
|
|
generateKeys(): Buffer;
|
|
|
|
generateKeys(encoding: BinaryToTextEncoding): string;
|
|
|
|
generateKeys(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.generateKeys");
|
|
|
|
}
|
|
|
|
|
|
|
|
getGenerator(): Buffer;
|
|
|
|
getGenerator(encoding: BinaryToTextEncoding): string;
|
|
|
|
getGenerator(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getGenerator");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrime(): Buffer;
|
|
|
|
getPrime(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPrime(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPrime");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrivateKey(): Buffer;
|
|
|
|
getPrivateKey(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPrivateKey(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPrivateKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicKey(): Buffer;
|
|
|
|
getPublicKey(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPublicKey(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.DiffieHellman.prototype.getPublicKey");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ECDH {
|
|
|
|
constructor(curve: string) {
|
|
|
|
validateString(curve, "curve");
|
|
|
|
|
|
|
|
notImplemented("crypto.ECDH");
|
|
|
|
}
|
|
|
|
|
|
|
|
static convertKey(
|
|
|
|
_key: BinaryLike,
|
|
|
|
_curve: string,
|
|
|
|
_inputEncoding?: BinaryToTextEncoding,
|
|
|
|
_outputEncoding?: "latin1" | "hex" | "base64" | "base64url",
|
|
|
|
_format?: "uncompressed" | "compressed" | "hybrid",
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.convertKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
computeSecret(otherPublicKey: ArrayBufferView): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
): Buffer;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: ArrayBufferView,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
otherPublicKey: string,
|
|
|
|
inputEncoding: BinaryToTextEncoding,
|
|
|
|
outputEncoding: BinaryToTextEncoding,
|
|
|
|
): string;
|
|
|
|
computeSecret(
|
|
|
|
_otherPublicKey: ArrayBufferView | string,
|
|
|
|
_inputEncoding?: BinaryToTextEncoding,
|
|
|
|
_outputEncoding?: BinaryToTextEncoding,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.computeSecret");
|
|
|
|
}
|
|
|
|
|
|
|
|
generateKeys(): Buffer;
|
|
|
|
generateKeys(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
|
|
|
|
generateKeys(
|
|
|
|
_encoding?: BinaryToTextEncoding,
|
|
|
|
_format?: ECDHKeyFormat,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.generateKeys");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPrivateKey(): Buffer;
|
|
|
|
getPrivateKey(encoding: BinaryToTextEncoding): string;
|
|
|
|
getPrivateKey(_encoding?: BinaryToTextEncoding): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.getPrivateKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicKey(): Buffer;
|
|
|
|
getPublicKey(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
|
|
|
|
getPublicKey(
|
|
|
|
_encoding?: BinaryToTextEncoding,
|
|
|
|
_format?: ECDHKeyFormat,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.getPublicKey");
|
|
|
|
}
|
|
|
|
|
|
|
|
setPrivateKey(privateKey: ArrayBufferView): void;
|
|
|
|
setPrivateKey(privateKey: string, encoding: BinaryToTextEncoding): void;
|
|
|
|
setPrivateKey(
|
|
|
|
_privateKey: ArrayBufferView | string,
|
|
|
|
_encoding?: BinaryToTextEncoding,
|
|
|
|
): Buffer | string {
|
|
|
|
notImplemented("crypto.ECDH.prototype.setPrivateKey");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function diffieHellman(_options: {
|
|
|
|
privateKey: KeyObject;
|
|
|
|
publicKey: KeyObject;
|
|
|
|
}): Buffer {
|
|
|
|
notImplemented("crypto.diffieHellman");
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
DiffieHellman,
|
|
|
|
DiffieHellmanGroup,
|
|
|
|
ECDH,
|
|
|
|
diffieHellman,
|
|
|
|
};
|