Skip to content

Commit

Permalink
Fix file url
Browse files Browse the repository at this point in the history
  • Loading branch information
w3nl committed Oct 11, 2021
1 parent 453fbbc commit f3b17a5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"name": "@hckrnews/local-fetch",
"version": "1.0.1",
"version": "1.2.0",
"description": "Local Fetch",
"files": [
"src/index.js"
"src/index.js",
"src/is-url.js",
"src/get-path.js"
],
"main": "src/index.js",
"repository": {
Expand Down
41 changes: 41 additions & 0 deletions src/__tests__/get-path.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect, describe, it } from '@jest/globals'
import getPath from '../get-path.js'

const testCases = [
{
description: 'Location is local',
target: './src/__fixtures__/example.json',
expectedResult: './src/__fixtures__/example.json',
expectedType: String
},
{
description: 'Location is local',
target: '/src/__fixtures__/example.json',
expectedResult: '/src/__fixtures__/example.json',
expectedType: String
},
{
description: 'Location is local',
target: 'file:///src/__fixtures__/example.json',
expectedResult: 'file:///src/__fixtures__/example.json',
expectedType: URL
},
{
description: 'Location is local',
target: 'file://localhost/etc/fstab',
expectedResult: 'file:///etc/fstab',
expectedType: URL
}
]

describe.each(testCases)(
'Test the Fetcher helper with test cases',
({ description, target, expectedResult, expectedType }) => {
it(description, async () => {
const results = getPath(target)

expect(results.constructor).toEqual(expectedType)
expect(results.toString()).toBe(expectedResult)
})
}
)
36 changes: 36 additions & 0 deletions src/__tests__/is-url.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, describe, it } from '@jest/globals'
import isURL from '../is-url.js'

const testCases = [
{
description: 'Location is local',
target: './test.json',
expectedResult: false
},
{
description: 'Location is local',
target: '/local/test.json',
expectedResult: false
},
{
description: 'Location is local',
target: 'file:///foo.txt',
expectedResult: true
},
{
description: 'Location is local',
target: 'file://localhost/etc/fstab',
expectedResult: true
}
]

describe.each(testCases)(
'Test the Fetcher helper with test cases',
({ description, target, expectedResult }) => {
it(description, async () => {
const results = isURL(target)

expect(results).toEqual(expectedResult)
})
}
)
8 changes: 8 additions & 0 deletions src/get-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import isURL from './is-url.js'

const getPath = location =>
isURL(location)
? new URL(location)
: location

export default getPath
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs'
import getPath from './get-path.js'

const localFetch = async (location) => {
const rawdata = fs.readFileSync(location)
const stream = fs.createReadStream(location)
const path = getPath(location)
const rawdata = fs.readFileSync(path)
const stream = fs.createReadStream(path)
return {
body: stream,
text: async () => rawdata.toString('ascii', 0, rawdata.length),
Expand Down
5 changes: 5 additions & 0 deletions src/is-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const reRemote = /^\w[\w.+-]+:\/\//

const isRemote = target => reRemote.test(target)

export default isRemote

0 comments on commit f3b17a5

Please sign in to comment.