Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 7277256

Browse files
committed
refactor to make tests runnable in node.js
1 parent 8a247c4 commit 7277256

26 files changed

+1502
-807
lines changed

dgram.js

Lines changed: 1 addition & 554 deletions
Large diffs are not rendered by default.

fs.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
/**
2-
* @notice This is a rexports of `fs/index.js` so consumers will
3-
* need to only `import * as fs from '@socketsupply/io/fs.js'`
4-
*/
5-
export * from './fs/index.js'
1+
export * from 'fs'

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ export * as ipc from './ipc.js'
1212
export * as net from './net.js'
1313
export * as os from './os.js'
1414
export * as crypto from './crypto.js'
15-
export { default as process } from './process.js'
15+
export * as process from './process.js'
1616
export * as stream from './stream.js'
1717
export * as util from './util.js'

os.js

Lines changed: 1 addition & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,184 +1,2 @@
1-
/**
2-
* @module OS
3-
*
4-
* This module provides normalized system information from all the major
5-
* operating systems.
6-
*/
71

8-
import { toProperCase } from './util.js'
9-
import * as ipc from './ipc.js'
10-
11-
const UNKNOWN = 'unknown'
12-
13-
const cache = {
14-
arch: UNKNOWN,
15-
type: UNKNOWN,
16-
platform: UNKNOWN
17-
}
18-
19-
export function arch () {
20-
let value = UNKNOWN
21-
22-
if (cache.arch !== UNKNOWN) {
23-
return cache.arch
24-
}
25-
26-
if (typeof window !== 'object') {
27-
if (typeof process === 'object' && typeof process.arch === 'string') {
28-
return process.arch
29-
}
30-
}
31-
32-
if (typeof window === 'object') {
33-
value = (
34-
window.process?.arch ||
35-
ipc.sendSync('getPlatformArch')?.data ||
36-
UNKNOWN
37-
)
38-
}
39-
40-
if (value === 'arm64') {
41-
return value
42-
}
43-
44-
cache.arch = value
45-
.replace('x86_64', 'x64')
46-
.replace('x86', 'ia32')
47-
.replace(/arm.*/, 'arm')
48-
49-
return cache.arch
50-
}
51-
52-
export function networkInterfaces () {
53-
const { ipv4, ipv6 } = ipc.sendSync('getNetworkInterfaces')?.data || {}
54-
const interfaces = {}
55-
56-
for (const type in ipv4) {
57-
const address = ipv4[type]
58-
const family = 'IPv4'
59-
60-
let internal = false
61-
let netmask = '255.255.255.0'
62-
let cidr = `${address}/24`
63-
let mac = null
64-
65-
if (address === '127.0.0.1' || address === '0.0.0.0') {
66-
internal = true
67-
mac = '00:00:00:00:00:00'
68-
69-
if (address === '127.0.0.1') {
70-
cidr = '127.0.0.1/8'
71-
netmask = '255.0.0.0'
72-
} else {
73-
cidr = '0.0.0.0/0'
74-
netmask = '0.0.0.0'
75-
}
76-
}
77-
78-
interfaces[type] = interfaces[type] || []
79-
interfaces[type].push({
80-
address,
81-
netmask,
82-
internal,
83-
family,
84-
cidr,
85-
mac
86-
})
87-
}
88-
89-
for (const type in ipv6) {
90-
const address = ipv6[type]
91-
const family = 'IPv6'
92-
93-
let internal = false
94-
let netmask = 'ffff:ffff:ffff:ffff::'
95-
let cidr = `${address}/64`
96-
let mac = null
97-
98-
if (address === '::1') {
99-
internal = true
100-
netmask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
101-
cidr = '::1/128'
102-
mac = '00:00:00:00:00:00'
103-
}
104-
105-
interfaces[type] = interfaces[type] || []
106-
interfaces[type].push({
107-
address,
108-
netmask,
109-
internal,
110-
family,
111-
cidr,
112-
mac
113-
})
114-
}
115-
116-
return interfaces
117-
}
118-
119-
export function platform () {
120-
let value = UNKNOWN
121-
122-
if (cache.platform !== UNKNOWN) {
123-
return cache.platform
124-
}
125-
126-
if (typeof window !== 'object') {
127-
if (typeof process === 'object' && typeof process.platform === 'string') {
128-
return process.platform
129-
}
130-
}
131-
132-
if (typeof window === 'object') {
133-
value = (
134-
window.process?.os ||
135-
ipc.sendSync('getPlatformOS')?.data ||
136-
window.process?.platform ||
137-
UNKNOWN
138-
)
139-
}
140-
141-
cache.platform = value.replace(/^mac/i, 'darwin')
142-
143-
return cache.platform
144-
}
145-
146-
export function type () {
147-
let value = 'unknown'
148-
149-
if (cache.type !== UNKNOWN) {
150-
return cache.type
151-
}
152-
153-
if (typeof window !== 'object') {
154-
switch (platform()) {
155-
case 'linux': return 'Linux'
156-
case 'mac': case 'darwin': return 'Darwin'
157-
case 'win32': return 'Windows' // Windows_NT?
158-
}
159-
}
160-
161-
if (typeof window == 'object') {
162-
value = (
163-
window.process?.platform ||
164-
ipc.sendSync('getPlatformType')?.data ||
165-
UNKNOWN
166-
)
167-
}
168-
169-
if (value !== UNKNOWN) {
170-
value = toProperCase(value)
171-
}
172-
173-
cache.type = value
174-
175-
return cache.type
176-
}
177-
178-
export const EOL = (() => {
179-
if (/^win/i.test(type())) {
180-
return '\r\n'
181-
}
182-
183-
return '\n'
184-
})()
2+
export * from 'os'

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
"esbuild": "0.14.49",
3131
"tapzero": "*"
3232
},
33+
"browser": {
34+
"fs": "./sdk/fs.js",
35+
"os": "./sdk/os.js",
36+
"dgram": "./sdk/dgram.js",
37+
"process": "./sdk/process.js"
38+
},
3339
"dependencies": {
3440
"buffer": "6.0.3"
3541
}

process.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1 @@
1-
import { EventEmitter } from './events.js'
2-
import { send } from './ipc.js'
3-
4-
let didEmitExitEvent = false
5-
6-
export function homedir () {
7-
process.env.HOME || ''
8-
}
9-
10-
export function exit (code) {
11-
if (!didEmitExitEvent) {
12-
didEmitExitEvent = true
13-
queueMicrotask(() => process.emit('exit', code))
14-
}
15-
16-
send('exit', { value: code || 0 })
17-
}
18-
19-
const parent = typeof window === 'object' ? window : globalThis
20-
const isNode = parent?.process?.versions?.node
21-
const process = isNode
22-
? globalThis.process
23-
: Object.create(null, Object.getOwnPropertyDescriptors({
24-
...EventEmitter.prototype,
25-
homedir,
26-
argv0: parent?.process?.argv?.[0],
27-
exit,
28-
...parent?.process,
29-
}))
30-
31-
if (!isNode) {
32-
EventEmitter.call(process)
33-
}
34-
35-
export default process
1+
export * from 'process'

0 commit comments

Comments
 (0)