-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
index.ts
108 lines (97 loc) · 3.37 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (c) 2013 - 2021 node-coap contributors.
*
* node-coap is licensed under an MIT +no-false-attribs license.
* All rights not explicitly granted in the MIT license are reserved.
* See the included LICENSE file for more details.
*/
import Agent from './lib/agent'
import Server from './lib/server'
import IncomingMessage from './lib/incoming_message'
import OutgoingMessage from './lib/outgoing_message'
import ObserveReadStream from './lib/observe_read_stream'
import ObserveWriteStream from './lib/observe_write_stream'
import { parameters, refreshTiming, defaultTiming } from './lib/parameters'
import { isIPv6 } from 'net'
import { registerOption, registerFormat, ignoreOption } from './lib/option_converter'
import type { CoapServerOptions, requestListener, CoapRequestParams, ParametersUpdate, AgentOptions, CoapPacket, Option, OptionValue } from './models/models'
export let globalAgent = new Agent({ type: 'udp4' })
export let globalAgentIPv6 = new Agent({ type: 'udp6' })
export function setGlobalAgent (agent: Agent): void {
globalAgent = agent
}
export function setGlobalAgentV6 (agent: Agent): void {
globalAgentIPv6 = agent
}
export function createServer (options?: CoapServerOptions | typeof requestListener, listener?: typeof requestListener): Server {
return new Server(options, listener)
}
function _getHostname (url: URL): string {
const hostname = url.hostname
// Remove brackets from literal IPv6 addresses
if (hostname.startsWith('[') && hostname.endsWith(']')) {
return hostname.substring(1, hostname.length - 1)
}
return hostname
}
function _getQueryParamsFromSearch (url: URL): string | undefined {
if (url.search != null) {
return url.search.substring(1)
}
}
function _getPort (url: URL): number {
if (url.port !== '') {
return parseInt(url.port)
} else {
return parameters.coapPort
}
}
function _parseUrl (url: string): CoapRequestParams {
const requestParams: CoapRequestParams = {}
const parsedUrl = new URL(url)
requestParams.hostname = _getHostname(parsedUrl)
requestParams.query = _getQueryParamsFromSearch(parsedUrl)
requestParams.port = _getPort(parsedUrl)
requestParams.pathname = parsedUrl.pathname
return requestParams
}
export function request (requestParams: CoapRequestParams | string): OutgoingMessage {
let agent: Agent
if (typeof requestParams === 'string') {
requestParams = _parseUrl(requestParams)
}
const ipv6 = isIPv6(requestParams.hostname ?? requestParams.host ?? '')
if (requestParams.agent != null && requestParams.agent !== false) {
agent = requestParams.agent
} else if (requestParams.agent === false && !ipv6) {
agent = new Agent({ type: 'udp4' })
} else if (requestParams.agent === false && ipv6) {
agent = new Agent({ type: 'udp6' })
} else if (ipv6) {
agent = globalAgentIPv6
} else {
agent = globalAgent
}
return agent.request(requestParams)
}
export {
parameters,
refreshTiming as updateTiming,
defaultTiming,
registerOption,
registerFormat,
ignoreOption,
IncomingMessage,
OutgoingMessage,
ObserveReadStream,
ObserveWriteStream,
Agent,
Server,
type ParametersUpdate,
type CoapRequestParams,
type AgentOptions,
type CoapPacket,
type Option,
type OptionValue,
type CoapServerOptions
}