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

refactor using browser field to make tests runnable in node.js #24

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
555 changes: 1 addition & 554 deletions dgram.js

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions fs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
/**
* @notice This is a rexports of `fs/index.js` so consumers will
* need to only `import * as fs from '@socketsupply/io/fs.js'`
*/
export * from './fs/index.js'
export * from 'fs'
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export * as ipc from './ipc.js'
export * as net from './net.js'
export * as os from './os.js'
export * as crypto from './crypto.js'
export { default as process } from './process.js'
export * as process from './process.js'
export * as stream from './stream.js'
export * as util from './util.js'
184 changes: 1 addition & 183 deletions os.js
Original file line number Diff line number Diff line change
@@ -1,184 +1,2 @@
/**
* @module OS
*
* This module provides normalized system information from all the major
* operating systems.
*/

import { toProperCase } from './util.js'
import * as ipc from './ipc.js'

const UNKNOWN = 'unknown'

const cache = {
arch: UNKNOWN,
type: UNKNOWN,
platform: UNKNOWN
}

export function arch () {
let value = UNKNOWN

if (cache.arch !== UNKNOWN) {
return cache.arch
}

if (typeof window !== 'object') {
if (typeof process === 'object' && typeof process.arch === 'string') {
return process.arch
}
}

if (typeof window === 'object') {
value = (
window.process?.arch ||
ipc.sendSync('getPlatformArch')?.data ||
UNKNOWN
)
}

if (value === 'arm64') {
return value
}

cache.arch = value
.replace('x86_64', 'x64')
.replace('x86', 'ia32')
.replace(/arm.*/, 'arm')

return cache.arch
}

export function networkInterfaces () {
const { ipv4, ipv6 } = ipc.sendSync('getNetworkInterfaces')?.data || {}
const interfaces = {}

for (const type in ipv4) {
const address = ipv4[type]
const family = 'IPv4'

let internal = false
let netmask = '255.255.255.0'
let cidr = `${address}/24`
let mac = null

if (address === '127.0.0.1' || address === '0.0.0.0') {
internal = true
mac = '00:00:00:00:00:00'

if (address === '127.0.0.1') {
cidr = '127.0.0.1/8'
netmask = '255.0.0.0'
} else {
cidr = '0.0.0.0/0'
netmask = '0.0.0.0'
}
}

interfaces[type] = interfaces[type] || []
interfaces[type].push({
address,
netmask,
internal,
family,
cidr,
mac
})
}

for (const type in ipv6) {
const address = ipv6[type]
const family = 'IPv6'

let internal = false
let netmask = 'ffff:ffff:ffff:ffff::'
let cidr = `${address}/64`
let mac = null

if (address === '::1') {
internal = true
netmask = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
cidr = '::1/128'
mac = '00:00:00:00:00:00'
}

interfaces[type] = interfaces[type] || []
interfaces[type].push({
address,
netmask,
internal,
family,
cidr,
mac
})
}

return interfaces
}

export function platform () {
let value = UNKNOWN

if (cache.platform !== UNKNOWN) {
return cache.platform
}

if (typeof window !== 'object') {
if (typeof process === 'object' && typeof process.platform === 'string') {
return process.platform
}
}

if (typeof window === 'object') {
value = (
window.process?.os ||
ipc.sendSync('getPlatformOS')?.data ||
window.process?.platform ||
UNKNOWN
)
}

cache.platform = value.replace(/^mac/i, 'darwin')

return cache.platform
}

export function type () {
let value = 'unknown'

if (cache.type !== UNKNOWN) {
return cache.type
}

if (typeof window !== 'object') {
switch (platform()) {
case 'linux': return 'Linux'
case 'mac': case 'darwin': return 'Darwin'
case 'win32': return 'Windows' // Windows_NT?
}
}

if (typeof window == 'object') {
value = (
window.process?.platform ||
ipc.sendSync('getPlatformType')?.data ||
UNKNOWN
)
}

if (value !== UNKNOWN) {
value = toProperCase(value)
}

cache.type = value

return cache.type
}

export const EOL = (() => {
if (/^win/i.test(type())) {
return '\r\n'
}

return '\n'
})()
export * from 'os'
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"esbuild": "0.14.49",
"tapzero": "*"
},
"browser": {
"fs": "./sdk/fs.js",
"os": "./sdk/os.js",
"dgram": "./sdk/dgram.js",
"process": "./sdk/process.js"
},
"dependencies": {
"buffer": "6.0.3"
}
Expand Down
36 changes: 1 addition & 35 deletions process.js
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
import { EventEmitter } from './events.js'
import { send } from './ipc.js'

let didEmitExitEvent = false

export function homedir () {
process.env.HOME || ''
}

export function exit (code) {
if (!didEmitExitEvent) {
didEmitExitEvent = true
queueMicrotask(() => process.emit('exit', code))
}

send('exit', { value: code || 0 })
}

const parent = typeof window === 'object' ? window : globalThis
const isNode = parent?.process?.versions?.node
const process = isNode
? globalThis.process
: Object.create(null, Object.getOwnPropertyDescriptors({
...EventEmitter.prototype,
homedir,
argv0: parent?.process?.argv?.[0],
exit,
...parent?.process,
}))

if (!isNode) {
EventEmitter.call(process)
}

export default process
export * from 'process'
Loading