-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a 2nd example of creating a custom method for the server
- Loading branch information
1 parent
5efdead
commit d191897
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Custom method | ||
|
||
This is an example to show how to make a custom method. | ||
|
||
This example focus on the `TRACE` method, to send to a remote user all modified/created resources since the last request. | ||
|
||
The `ts` file and the `js` file are the same thing. The `js` file displays the example in JavaScript while the `ts` file displays the example in TypeScript. | ||
|
||
Note : The methods which can be added are limited to [the supported methods](https://nodejs.org/api/http.html#http_http_methods) in the `http` module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const webdav = require('webdav-server').v2; | ||
|
||
// Server instantiation | ||
const server = new webdav.WebDAVServer(); | ||
|
||
let folderNotify = { }; | ||
server.method('TRACE', { | ||
unchunked(ctx, data, cb) | ||
{ | ||
if(!ctx.user) | ||
{ | ||
ctx.setCode(webdav.HTTPCodes.Unauthorized); | ||
return cb(); | ||
} | ||
|
||
const path = ctx.requested.path.toString(true); | ||
const name = ctx.user.username; | ||
let lastCheck = 0; | ||
if(!folderNotify[name]) | ||
folderNotify[name] = {}; | ||
if(folderNotify[name][path]) | ||
lastCheck = folderNotify[name][path]; | ||
|
||
const list = []; | ||
|
||
ctx.getResource((e, r) => { | ||
r.readDir((e, files) => { | ||
let nb = files.length + 1; | ||
const go = () => { | ||
if(--nb === 0) | ||
{ | ||
ctx.setCode(webdav.HTTPCodes.OK); | ||
ctx.response.write(JSON.stringify(list)); | ||
folderNotify[name][path] = Date.now(); | ||
cb(); | ||
} | ||
} | ||
go(); | ||
|
||
files.forEach((file) => { | ||
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => { | ||
if(date >= lastCheck) | ||
list.push({ | ||
path: path + file, | ||
name: file | ||
}); | ||
|
||
go(); | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}); | ||
|
||
server.start((s) => console.log('Ready on port', s.address().port)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { v2 as webdav } from 'webdav-server' | ||
import * as request from 'request' | ||
|
||
// Server instantiation | ||
const server = new webdav.WebDAVServer(); | ||
|
||
let folderNotify = { }; | ||
server.method('TRACE', { | ||
unchunked(ctx : webdav.HTTPRequestContext, data : Buffer, cb : () => void) | ||
{ | ||
if(!ctx.user) | ||
{ | ||
ctx.setCode(webdav.HTTPCodes.Unauthorized); | ||
return cb(); | ||
} | ||
|
||
const path = ctx.requested.path.toString(true); | ||
const name = ctx.user.username; | ||
let lastCheck = 0; | ||
if(!folderNotify[name]) | ||
folderNotify[name] = {}; | ||
if(folderNotify[name][path]) | ||
lastCheck = folderNotify[name][path]; | ||
|
||
const list = []; | ||
|
||
ctx.getResource((e, r) => { | ||
r.readDir((e, files) => { | ||
let nb = files.length + 1; | ||
const go = () => { | ||
if(--nb === 0) | ||
{ | ||
ctx.setCode(webdav.HTTPCodes.OK); | ||
ctx.response.write(JSON.stringify(list)); | ||
folderNotify[name][path] = Date.now(); | ||
cb(); | ||
} | ||
} | ||
go(); | ||
|
||
files.forEach((file) => { | ||
r.fs.lastModifiedDate(ctx, r.path.toString(true) + file, (e, date) => { | ||
if(date >= lastCheck) | ||
list.push({ | ||
path: path + file, | ||
name: file | ||
}); | ||
|
||
go(); | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}); | ||
|
||
server.start((s) => console.log('Ready on port', s.address().port)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "custom-method", | ||
"version": "1.0.0", | ||
"description": "Example to show how to make a custom method.", | ||
"main": "index.js", | ||
"author": "Adrien Castex <[email protected]>", | ||
"license": "Unlicense", | ||
"dependencies": { | ||
"webdav-server": "^2.0.0" | ||
} | ||
} |